uaccess.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 TASK_SIZE_MAX DEFAULT_TASK_SIZE
  12. #include <asm/pgtable.h>
  13. #include <asm-generic/access_ok.h>
  14. #define put_user __put_user
  15. #define get_user __get_user
  16. #if !defined(CONFIG_64BIT)
  17. #define LDD_USER(sr, val, ptr) __get_user_asm64(sr, val, ptr)
  18. #define STD_USER(sr, x, ptr) __put_user_asm64(sr, x, ptr)
  19. #else
  20. #define LDD_USER(sr, val, ptr) __get_user_asm(sr, val, "ldd", ptr)
  21. #define STD_USER(sr, x, ptr) __put_user_asm(sr, "std", x, ptr)
  22. #endif
  23. /*
  24. * The exception table contains two values: the first is the relative offset to
  25. * the address of the instruction that is allowed to fault, and the second is
  26. * the relative offset to the address of the fixup routine. Since relative
  27. * addresses are used, 32bit values are sufficient even on 64bit kernel.
  28. */
  29. #define ARCH_HAS_RELATIVE_EXTABLE
  30. struct exception_table_entry {
  31. int insn; /* relative address of insn that is allowed to fault. */
  32. int fixup; /* relative address of fixup routine */
  33. };
  34. #define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
  35. ".section __ex_table,\"aw\"\n" \
  36. ".align 4\n" \
  37. ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
  38. ".previous\n"
  39. /*
  40. * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry
  41. * (with lowest bit set) for which the fault handler in fixup_exception() will
  42. * load -EFAULT into %r29 for a read or write fault, and zeroes the target
  43. * register in case of a read fault in get_user().
  44. */
  45. #define ASM_EXCEPTIONTABLE_REG 29
  46. #define ASM_EXCEPTIONTABLE_VAR(__variable) \
  47. register long __variable __asm__ ("r29") = 0
  48. #define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\
  49. ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1)
  50. #define __get_user_internal(sr, val, ptr) \
  51. ({ \
  52. ASM_EXCEPTIONTABLE_VAR(__gu_err); \
  53. \
  54. switch (sizeof(*(ptr))) { \
  55. case 1: __get_user_asm(sr, val, "ldb", ptr); break; \
  56. case 2: __get_user_asm(sr, val, "ldh", ptr); break; \
  57. case 4: __get_user_asm(sr, val, "ldw", ptr); break; \
  58. case 8: LDD_USER(sr, val, ptr); break; \
  59. default: BUILD_BUG(); \
  60. } \
  61. \
  62. __gu_err; \
  63. })
  64. #define __get_user(val, ptr) \
  65. ({ \
  66. __get_user_internal(SR_USER, val, ptr); \
  67. })
  68. #define __get_user_asm(sr, val, ldx, ptr) \
  69. { \
  70. register long __gu_val; \
  71. \
  72. __asm__("1: " ldx " 0(%%sr%2,%3),%0\n" \
  73. "9:\n" \
  74. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
  75. : "=r"(__gu_val), "+r"(__gu_err) \
  76. : "i"(sr), "r"(ptr)); \
  77. \
  78. (val) = (__force __typeof__(*(ptr))) __gu_val; \
  79. }
  80. #define __get_kernel_nofault(dst, src, type, err_label) \
  81. { \
  82. type __z; \
  83. long __err; \
  84. __err = __get_user_internal(SR_KERNEL, __z, (type *)(src)); \
  85. if (unlikely(__err)) \
  86. goto err_label; \
  87. else \
  88. *(type *)(dst) = __z; \
  89. }
  90. #if !defined(CONFIG_64BIT)
  91. #define __get_user_asm64(sr, val, ptr) \
  92. { \
  93. union { \
  94. unsigned long long l; \
  95. __typeof__(*(ptr)) t; \
  96. } __gu_tmp; \
  97. \
  98. __asm__(" copy %%r0,%R0\n" \
  99. "1: ldw 0(%%sr%2,%3),%0\n" \
  100. "2: ldw 4(%%sr%2,%3),%R0\n" \
  101. "9:\n" \
  102. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
  103. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
  104. : "=&r"(__gu_tmp.l), "+r"(__gu_err) \
  105. : "i"(sr), "r"(ptr)); \
  106. \
  107. (val) = __gu_tmp.t; \
  108. }
  109. #endif /* !defined(CONFIG_64BIT) */
  110. #define __put_user_internal(sr, x, ptr) \
  111. ({ \
  112. ASM_EXCEPTIONTABLE_VAR(__pu_err); \
  113. \
  114. switch (sizeof(*(ptr))) { \
  115. case 1: __put_user_asm(sr, "stb", x, ptr); break; \
  116. case 2: __put_user_asm(sr, "sth", x, ptr); break; \
  117. case 4: __put_user_asm(sr, "stw", x, ptr); break; \
  118. case 8: STD_USER(sr, x, ptr); break; \
  119. default: BUILD_BUG(); \
  120. } \
  121. \
  122. __pu_err; \
  123. })
  124. #define __put_user(x, ptr) \
  125. ({ \
  126. __typeof__(&*(ptr)) __ptr = ptr; \
  127. __typeof__(*(__ptr)) __x = (__typeof__(*(__ptr)))(x); \
  128. __put_user_internal(SR_USER, __x, __ptr); \
  129. })
  130. #define __put_kernel_nofault(dst, src, type, err_label) \
  131. { \
  132. type __z = *(type *)(src); \
  133. long __err; \
  134. __err = __put_user_internal(SR_KERNEL, __z, (type *)(dst)); \
  135. if (unlikely(__err)) \
  136. goto err_label; \
  137. }
  138. /*
  139. * The "__put_user/kernel_asm()" macros tell gcc they read from memory
  140. * instead of writing. This is because they do not write to any memory
  141. * gcc knows about, so there are no aliasing issues. These macros must
  142. * also be aware that fixups are executed in the context of the fault,
  143. * and any registers used there must be listed as clobbers.
  144. * The register holding the possible EFAULT error (ASM_EXCEPTIONTABLE_REG)
  145. * is already listed as input and output register.
  146. */
  147. #define __put_user_asm(sr, stx, x, ptr) \
  148. __asm__ __volatile__ ( \
  149. "1: " stx " %1,0(%%sr%2,%3)\n" \
  150. "9:\n" \
  151. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
  152. : "+r"(__pu_err) \
  153. : "r"(x), "i"(sr), "r"(ptr))
  154. #if !defined(CONFIG_64BIT)
  155. #define __put_user_asm64(sr, __val, ptr) do { \
  156. __asm__ __volatile__ ( \
  157. "1: stw %1,0(%%sr%2,%3)\n" \
  158. "2: stw %R1,4(%%sr%2,%3)\n" \
  159. "9:\n" \
  160. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
  161. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
  162. : "+r"(__pu_err) \
  163. : "r"(__val), "i"(sr), "r"(ptr)); \
  164. } while (0)
  165. #endif /* !defined(CONFIG_64BIT) */
  166. /*
  167. * Complex access routines -- external declarations
  168. */
  169. extern long strncpy_from_user(char *, const char __user *, long);
  170. extern __must_check unsigned lclear_user(void __user *, unsigned long);
  171. extern __must_check long strnlen_user(const char __user *src, long n);
  172. /*
  173. * Complex access routines -- macros
  174. */
  175. #define clear_user lclear_user
  176. #define __clear_user lclear_user
  177. unsigned long __must_check raw_copy_to_user(void __user *dst, const void *src,
  178. unsigned long len);
  179. unsigned long __must_check raw_copy_from_user(void *dst, const void __user *src,
  180. unsigned long len);
  181. #define INLINE_COPY_TO_USER
  182. #define INLINE_COPY_FROM_USER
  183. struct pt_regs;
  184. int fixup_exception(struct pt_regs *regs);
  185. #endif /* __PARISC_UACCESS_H */