linux/arch/parisc/include/asm/uaccess.h
<<
>>
Prefs
   1#ifndef __PARISC_UACCESS_H
   2#define __PARISC_UACCESS_H
   3
   4/*
   5 * User space memory access functions
   6 */
   7#include <asm/page.h>
   8#include <asm/cache.h>
   9#include <asm/errno.h>
  10#include <asm-generic/uaccess-unaligned.h>
  11
  12#include <linux/bug.h>
  13#include <linux/string.h>
  14#include <linux/thread_info.h>
  15
  16#define VERIFY_READ 0
  17#define VERIFY_WRITE 1
  18
  19#define KERNEL_DS       ((mm_segment_t){0})
  20#define USER_DS         ((mm_segment_t){1})
  21
  22#define segment_eq(a, b) ((a).seg == (b).seg)
  23
  24#define get_ds()        (KERNEL_DS)
  25#define get_fs()        (current_thread_info()->addr_limit)
  26#define set_fs(x)       (current_thread_info()->addr_limit = (x))
  27
  28/*
  29 * Note that since kernel addresses are in a separate address space on
  30 * parisc, we don't need to do anything for access_ok().
  31 * We just let the page fault handler do the right thing. This also means
  32 * that put_user is the same as __put_user, etc.
  33 */
  34
  35#define access_ok(type, uaddr, size)    \
  36        ( (uaddr) == (uaddr) )
  37
  38#define put_user __put_user
  39#define get_user __get_user
  40
  41#if !defined(CONFIG_64BIT)
  42#define LDD_USER(val, ptr)      __get_user_asm64(val, ptr)
  43#define STD_USER(x, ptr)        __put_user_asm64(x, ptr)
  44#else
  45#define LDD_USER(val, ptr)      __get_user_asm(val, "ldd", ptr)
  46#define STD_USER(x, ptr)        __put_user_asm("std", x, ptr)
  47#endif
  48
  49/*
  50 * The exception table contains two values: the first is the relative offset to
  51 * the address of the instruction that is allowed to fault, and the second is
  52 * the relative offset to the address of the fixup routine. Since relative
  53 * addresses are used, 32bit values are sufficient even on 64bit kernel.
  54 */
  55
  56#define ARCH_HAS_RELATIVE_EXTABLE
  57struct exception_table_entry {
  58        int insn;       /* relative address of insn that is allowed to fault. */
  59        int fixup;      /* relative address of fixup routine */
  60};
  61
  62#define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
  63        ".section __ex_table,\"aw\"\n"                     \
  64        ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
  65        ".previous\n"
  66
  67/*
  68 * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry
  69 * (with lowest bit set) for which the fault handler in fixup_exception() will
  70 * load -EFAULT into %r8 for a read or write fault, and zeroes the target
  71 * register in case of a read fault in get_user().
  72 */
  73#define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\
  74        ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1)
  75
  76/*
  77 * The page fault handler stores, in a per-cpu area, the following information
  78 * if a fixup routine is available.
  79 */
  80struct exception_data {
  81        unsigned long fault_ip;
  82        unsigned long fault_gp;
  83        unsigned long fault_space;
  84        unsigned long fault_addr;
  85};
  86
  87/*
  88 * load_sr2() preloads the space register %%sr2 - based on the value of
  89 * get_fs() - with either a value of 0 to access kernel space (KERNEL_DS which
  90 * is 0), or with the current value of %%sr3 to access user space (USER_DS)
  91 * memory. The following __get_user_asm() and __put_user_asm() functions have
  92 * %%sr2 hard-coded to access the requested memory.
  93 */
  94#define load_sr2() \
  95        __asm__(" or,=  %0,%%r0,%%r0\n\t"       \
  96                " mfsp %%sr3,%0\n\t"            \
  97                " mtsp %0,%%sr2\n\t"            \
  98                : : "r"(get_fs()) : )
  99
 100#define __get_user_internal(val, ptr)                   \
 101({                                                      \
 102        register long __gu_err __asm__ ("r8") = 0;      \
 103                                                        \
 104        switch (sizeof(*(ptr))) {                       \
 105        case 1: __get_user_asm(val, "ldb", ptr); break; \
 106        case 2: __get_user_asm(val, "ldh", ptr); break; \
 107        case 4: __get_user_asm(val, "ldw", ptr); break; \
 108        case 8: LDD_USER(val, ptr); break;              \
 109        default: BUILD_BUG();                           \
 110        }                                               \
 111                                                        \
 112        __gu_err;                                       \
 113})
 114
 115#define __get_user(val, ptr)                            \
 116({                                                      \
 117        load_sr2();                                     \
 118        __get_user_internal(val, ptr);                  \
 119})
 120
 121#define __get_user_asm(val, ldx, ptr)                   \
 122{                                                       \
 123        register long __gu_val;                         \
 124                                                        \
 125        __asm__("1: " ldx " 0(%%sr2,%2),%0\n"           \
 126                "9:\n"                                  \
 127                ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
 128                : "=r"(__gu_val), "=r"(__gu_err)        \
 129                : "r"(ptr), "1"(__gu_err));             \
 130                                                        \
 131        (val) = (__force __typeof__(*(ptr))) __gu_val;  \
 132}
 133
 134#if !defined(CONFIG_64BIT)
 135
 136#define __get_user_asm64(val, ptr)                      \
 137{                                                       \
 138        union {                                         \
 139                unsigned long long      l;              \
 140                __typeof__(*(ptr))      t;              \
 141        } __gu_tmp;                                     \
 142                                                        \
 143        __asm__("   copy %%r0,%R0\n"                    \
 144                "1: ldw 0(%%sr2,%2),%0\n"               \
 145                "2: ldw 4(%%sr2,%2),%R0\n"              \
 146                "9:\n"                                  \
 147                ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
 148                ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
 149                : "=&r"(__gu_tmp.l), "=r"(__gu_err)     \
 150                : "r"(ptr), "1"(__gu_err));             \
 151                                                        \
 152        (val) = __gu_tmp.t;                             \
 153}
 154
 155#endif /* !defined(CONFIG_64BIT) */
 156
 157
 158#define __put_user_internal(x, ptr)                             \
 159({                                                              \
 160        register long __pu_err __asm__ ("r8") = 0;              \
 161        __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x);       \
 162                                                                \
 163        switch (sizeof(*(ptr))) {                               \
 164        case 1: __put_user_asm("stb", __x, ptr); break;         \
 165        case 2: __put_user_asm("sth", __x, ptr); break;         \
 166        case 4: __put_user_asm("stw", __x, ptr); break;         \
 167        case 8: STD_USER(__x, ptr); break;                      \
 168        default: BUILD_BUG();                                   \
 169        }                                                       \
 170                                                                \
 171        __pu_err;                                               \
 172})
 173
 174#define __put_user(x, ptr)                                      \
 175({                                                              \
 176        load_sr2();                                             \
 177        __put_user_internal(x, ptr);                            \
 178})
 179
 180
 181/*
 182 * The "__put_user/kernel_asm()" macros tell gcc they read from memory
 183 * instead of writing. This is because they do not write to any memory
 184 * gcc knows about, so there are no aliasing issues. These macros must
 185 * also be aware that fixups are executed in the context of the fault,
 186 * and any registers used there must be listed as clobbers.
 187 * r8 is already listed as err.
 188 */
 189
 190#define __put_user_asm(stx, x, ptr)                         \
 191        __asm__ __volatile__ (                              \
 192                "1: " stx " %2,0(%%sr2,%1)\n"               \
 193                "9:\n"                                      \
 194                ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)     \
 195                : "=r"(__pu_err)                            \
 196                : "r"(ptr), "r"(x), "0"(__pu_err))
 197
 198
 199#if !defined(CONFIG_64BIT)
 200
 201#define __put_user_asm64(__val, ptr) do {                   \
 202        __asm__ __volatile__ (                              \
 203                "1: stw %2,0(%%sr2,%1)\n"                   \
 204                "2: stw %R2,4(%%sr2,%1)\n"                  \
 205                "9:\n"                                      \
 206                ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)     \
 207                ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)     \
 208                : "=r"(__pu_err)                            \
 209                : "r"(ptr), "r"(__val), "0"(__pu_err));     \
 210} while (0)
 211
 212#endif /* !defined(CONFIG_64BIT) */
 213
 214
 215/*
 216 * Complex access routines -- external declarations
 217 */
 218
 219extern unsigned long lcopy_to_user(void __user *, const void *, unsigned long);
 220extern unsigned long lcopy_from_user(void *, const void __user *, unsigned long);
 221extern unsigned long lcopy_in_user(void __user *, const void __user *, unsigned long);
 222extern long strncpy_from_user(char *, const char __user *, long);
 223extern unsigned lclear_user(void __user *, unsigned long);
 224extern long lstrnlen_user(const char __user *, long);
 225/*
 226 * Complex access routines -- macros
 227 */
 228#define user_addr_max() (~0UL)
 229
 230#define strnlen_user lstrnlen_user
 231#define strlen_user(str) lstrnlen_user(str, 0x7fffffffL)
 232#define clear_user lclear_user
 233#define __clear_user lclear_user
 234
 235unsigned long __must_check __copy_to_user(void __user *dst, const void *src,
 236                                          unsigned long len);
 237unsigned long __must_check __copy_from_user(void *dst, const void __user *src,
 238                                          unsigned long len);
 239unsigned long copy_in_user(void __user *dst, const void __user *src,
 240                           unsigned long len);
 241#define __copy_in_user copy_in_user
 242#define __copy_to_user_inatomic __copy_to_user
 243#define __copy_from_user_inatomic __copy_from_user
 244
 245extern void __compiletime_error("usercopy buffer size is too small")
 246__bad_copy_user(void);
 247
 248static inline void copy_user_overflow(int size, unsigned long count)
 249{
 250        WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
 251}
 252
 253static __always_inline unsigned long __must_check
 254copy_from_user(void *to, const void __user *from, unsigned long n)
 255{
 256        int sz = __compiletime_object_size(to);
 257        unsigned long ret = n;
 258
 259        if (likely(sz < 0 || sz >= n)) {
 260                check_object_size(to, n, false);
 261                ret = __copy_from_user(to, from, n);
 262        } else if (!__builtin_constant_p(n))
 263                copy_user_overflow(sz, n);
 264        else
 265                __bad_copy_user();
 266
 267        if (unlikely(ret))
 268                memset(to + (n - ret), 0, ret);
 269
 270        return ret;
 271}
 272
 273static __always_inline unsigned long __must_check
 274copy_to_user(void __user *to, const void *from, unsigned long n)
 275{
 276        int sz = __compiletime_object_size(from);
 277
 278        if (likely(sz < 0 || sz >= n)) {
 279                check_object_size(from, n, true);
 280                n = __copy_to_user(to, from, n);
 281        } else if (!__builtin_constant_p(n))
 282                copy_user_overflow(sz, n);
 283        else
 284                __bad_copy_user();
 285
 286        return n;
 287}
 288
 289struct pt_regs;
 290int fixup_exception(struct pt_regs *regs);
 291
 292#endif /* __PARISC_UACCESS_H */
 293