uaccess.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PARISC_UACCESS_H
  3. #define __PARISC_UACCESS_H
  4. /*
  5. * User space memory access functions
  6. */
  7. #include <asm/page.h>
  8. #include <asm/cache.h>
  9. #include <linux/bug.h>
  10. #include <linux/string.h>
  11. #define KERNEL_DS ((mm_segment_t){0})
  12. #define USER_DS ((mm_segment_t){1})
  13. #define segment_eq(a, b) ((a).seg == (b).seg)
  14. #define get_ds() (KERNEL_DS)
  15. #define get_fs() (current_thread_info()->addr_limit)
  16. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  17. /*
  18. * Note that since kernel addresses are in a separate address space on
  19. * parisc, we don't need to do anything for access_ok().
  20. * We just let the page fault handler do the right thing. This also means
  21. * that put_user is the same as __put_user, etc.
  22. */
  23. #define access_ok(type, uaddr, size) \
  24. ( (uaddr) == (uaddr) )
  25. #define put_user __put_user
  26. #define get_user __get_user
  27. #if !defined(CONFIG_64BIT)
  28. #define LDD_USER(val, ptr) __get_user_asm64(val, ptr)
  29. #define STD_USER(x, ptr) __put_user_asm64(x, ptr)
  30. #else
  31. #define LDD_USER(val, ptr) __get_user_asm(val, "ldd", ptr)
  32. #define STD_USER(x, ptr) __put_user_asm("std", x, ptr)
  33. #endif
  34. /*
  35. * The exception table contains two values: the first is the relative offset to
  36. * the address of the instruction that is allowed to fault, and the second is
  37. * the relative offset to the address of the fixup routine. Since relative
  38. * addresses are used, 32bit values are sufficient even on 64bit kernel.
  39. */
  40. #define ARCH_HAS_RELATIVE_EXTABLE
  41. struct exception_table_entry {
  42. int insn; /* relative address of insn that is allowed to fault. */
  43. int fixup; /* relative address of fixup routine */
  44. };
  45. #define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
  46. ".section __ex_table,\"aw\"\n" \
  47. ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
  48. ".previous\n"
  49. /*
  50. * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry
  51. * (with lowest bit set) for which the fault handler in fixup_exception() will
  52. * load -EFAULT into %r8 for a read or write fault, and zeroes the target
  53. * register in case of a read fault in get_user().
  54. */
  55. #define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\
  56. ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1)
  57. /*
  58. * load_sr2() preloads the space register %%sr2 - based on the value of
  59. * get_fs() - with either a value of 0 to access kernel space (KERNEL_DS which
  60. * is 0), or with the current value of %%sr3 to access user space (USER_DS)
  61. * memory. The following __get_user_asm() and __put_user_asm() functions have
  62. * %%sr2 hard-coded to access the requested memory.
  63. */
  64. #define load_sr2() \
  65. __asm__(" or,= %0,%%r0,%%r0\n\t" \
  66. " mfsp %%sr3,%0\n\t" \
  67. " mtsp %0,%%sr2\n\t" \
  68. : : "r"(get_fs()) : )
  69. #define __get_user_internal(val, ptr) \
  70. ({ \
  71. register long __gu_err __asm__ ("r8") = 0; \
  72. \
  73. switch (sizeof(*(ptr))) { \
  74. case 1: __get_user_asm(val, "ldb", ptr); break; \
  75. case 2: __get_user_asm(val, "ldh", ptr); break; \
  76. case 4: __get_user_asm(val, "ldw", ptr); break; \
  77. case 8: LDD_USER(val, ptr); break; \
  78. default: BUILD_BUG(); \
  79. } \
  80. \
  81. __gu_err; \
  82. })
  83. #define __get_user(val, ptr) \
  84. ({ \
  85. load_sr2(); \
  86. __get_user_internal(val, ptr); \
  87. })
  88. #define __get_user_asm(val, ldx, ptr) \
  89. { \
  90. register long __gu_val; \
  91. \
  92. __asm__("1: " ldx " 0(%%sr2,%2),%0\n" \
  93. "9:\n" \
  94. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
  95. : "=r"(__gu_val), "=r"(__gu_err) \
  96. : "r"(ptr), "1"(__gu_err)); \
  97. \
  98. (val) = (__force __typeof__(*(ptr))) __gu_val; \
  99. }
  100. #if !defined(CONFIG_64BIT)
  101. #define __get_user_asm64(val, ptr) \
  102. { \
  103. union { \
  104. unsigned long long l; \
  105. __typeof__(*(ptr)) t; \
  106. } __gu_tmp; \
  107. \
  108. __asm__(" copy %%r0,%R0\n" \
  109. "1: ldw 0(%%sr2,%2),%0\n" \
  110. "2: ldw 4(%%sr2,%2),%R0\n" \
  111. "9:\n" \
  112. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
  113. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
  114. : "=&r"(__gu_tmp.l), "=r"(__gu_err) \
  115. : "r"(ptr), "1"(__gu_err)); \
  116. \
  117. (val) = __gu_tmp.t; \
  118. }
  119. #endif /* !defined(CONFIG_64BIT) */
  120. #define __put_user_internal(x, ptr) \
  121. ({ \
  122. register long __pu_err __asm__ ("r8") = 0; \
  123. __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x); \
  124. \
  125. switch (sizeof(*(ptr))) { \
  126. case 1: __put_user_asm("stb", __x, ptr); break; \
  127. case 2: __put_user_asm("sth", __x, ptr); break; \
  128. case 4: __put_user_asm("stw", __x, ptr); break; \
  129. case 8: STD_USER(__x, ptr); break; \
  130. default: BUILD_BUG(); \
  131. } \
  132. \
  133. __pu_err; \
  134. })
  135. #define __put_user(x, ptr) \
  136. ({ \
  137. load_sr2(); \
  138. __put_user_internal(x, ptr); \
  139. })
  140. /*
  141. * The "__put_user/kernel_asm()" macros tell gcc they read from memory
  142. * instead of writing. This is because they do not write to any memory
  143. * gcc knows about, so there are no aliasing issues. These macros must
  144. * also be aware that fixups are executed in the context of the fault,
  145. * and any registers used there must be listed as clobbers.
  146. * r8 is already listed as err.
  147. */
  148. #define __put_user_asm(stx, x, ptr) \
  149. __asm__ __volatile__ ( \
  150. "1: " stx " %2,0(%%sr2,%1)\n" \
  151. "9:\n" \
  152. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
  153. : "=r"(__pu_err) \
  154. : "r"(ptr), "r"(x), "0"(__pu_err))
  155. #if !defined(CONFIG_64BIT)
  156. #define __put_user_asm64(__val, ptr) do { \
  157. __asm__ __volatile__ ( \
  158. "1: stw %2,0(%%sr2,%1)\n" \
  159. "2: stw %R2,4(%%sr2,%1)\n" \
  160. "9:\n" \
  161. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
  162. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
  163. : "=r"(__pu_err) \
  164. : "r"(ptr), "r"(__val), "0"(__pu_err)); \
  165. } while (0)
  166. #endif /* !defined(CONFIG_64BIT) */
  167. /*
  168. * Complex access routines -- external declarations
  169. */
  170. extern long strncpy_from_user(char *, const char __user *, long);
  171. extern unsigned lclear_user(void __user *, unsigned long);
  172. extern long lstrnlen_user(const char __user *, long);
  173. /*
  174. * Complex access routines -- macros
  175. */
  176. #define user_addr_max() (~0UL)
  177. #define strnlen_user lstrnlen_user
  178. #define clear_user lclear_user
  179. #define __clear_user lclear_user
  180. unsigned long __must_check raw_copy_to_user(void __user *dst, const void *src,
  181. unsigned long len);
  182. unsigned long __must_check raw_copy_from_user(void *dst, const void __user *src,
  183. unsigned long len);
  184. unsigned long __must_check raw_copy_in_user(void __user *dst, const void __user *src,
  185. unsigned long len);
  186. #define INLINE_COPY_TO_USER
  187. #define INLINE_COPY_FROM_USER
  188. struct pt_regs;
  189. int fixup_exception(struct pt_regs *regs);
  190. #endif /* __PARISC_UACCESS_H */