uaccess.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifndef __PARISC_UACCESS_H
  2. #define __PARISC_UACCESS_H
  3. /*
  4. * User space memory access functions
  5. */
  6. #include <asm/page.h>
  7. #include <asm/cache.h>
  8. #include <asm/errno.h>
  9. #include <asm-generic/uaccess-unaligned.h>
  10. #include <linux/bug.h>
  11. #define VERIFY_READ 0
  12. #define VERIFY_WRITE 1
  13. #define KERNEL_DS ((mm_segment_t){0})
  14. #define USER_DS ((mm_segment_t){1})
  15. #define segment_eq(a, b) ((a).seg == (b).seg)
  16. #define get_ds() (KERNEL_DS)
  17. #define get_fs() (current_thread_info()->addr_limit)
  18. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  19. /*
  20. * Note that since kernel addresses are in a separate address space on
  21. * parisc, we don't need to do anything for access_ok().
  22. * We just let the page fault handler do the right thing. This also means
  23. * that put_user is the same as __put_user, etc.
  24. */
  25. static inline long access_ok(int type, const void __user * addr,
  26. unsigned long size)
  27. {
  28. return 1;
  29. }
  30. #define put_user __put_user
  31. #define get_user __get_user
  32. #if !defined(CONFIG_64BIT)
  33. #define LDD_KERNEL(ptr) BUILD_BUG()
  34. #define LDD_USER(ptr) BUILD_BUG()
  35. #define STD_KERNEL(x, ptr) __put_kernel_asm64(x, ptr)
  36. #define STD_USER(x, ptr) __put_user_asm64(x, ptr)
  37. #define ASM_WORD_INSN ".word\t"
  38. #else
  39. #define LDD_KERNEL(ptr) __get_kernel_asm("ldd", ptr)
  40. #define LDD_USER(ptr) __get_user_asm("ldd", ptr)
  41. #define STD_KERNEL(x, ptr) __put_kernel_asm("std", x, ptr)
  42. #define STD_USER(x, ptr) __put_user_asm("std", x, ptr)
  43. #define ASM_WORD_INSN ".dword\t"
  44. #endif
  45. /*
  46. * The exception table contains two values: the first is an address
  47. * for an instruction that is allowed to fault, and the second is
  48. * the address to the fixup routine. Even on a 64bit kernel we could
  49. * use a 32bit (unsigned int) address here.
  50. */
  51. struct exception_table_entry {
  52. unsigned long insn; /* address of insn that is allowed to fault. */
  53. unsigned long fixup; /* fixup routine */
  54. };
  55. #define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
  56. ".section __ex_table,\"aw\"\n" \
  57. ASM_WORD_INSN #fault_addr ", " #except_addr "\n\t" \
  58. ".previous\n"
  59. /*
  60. * The page fault handler stores, in a per-cpu area, the following information
  61. * if a fixup routine is available.
  62. */
  63. struct exception_data {
  64. unsigned long fault_ip;
  65. unsigned long fault_space;
  66. unsigned long fault_addr;
  67. };
  68. #define __get_user(x, ptr) \
  69. ({ \
  70. register long __gu_err __asm__ ("r8") = 0; \
  71. register long __gu_val __asm__ ("r9") = 0; \
  72. \
  73. if (segment_eq(get_fs(), KERNEL_DS)) { \
  74. switch (sizeof(*(ptr))) { \
  75. case 1: __get_kernel_asm("ldb", ptr); break; \
  76. case 2: __get_kernel_asm("ldh", ptr); break; \
  77. case 4: __get_kernel_asm("ldw", ptr); break; \
  78. case 8: LDD_KERNEL(ptr); break; \
  79. default: BUILD_BUG(); break; \
  80. } \
  81. } \
  82. else { \
  83. switch (sizeof(*(ptr))) { \
  84. case 1: __get_user_asm("ldb", ptr); break; \
  85. case 2: __get_user_asm("ldh", ptr); break; \
  86. case 4: __get_user_asm("ldw", ptr); break; \
  87. case 8: LDD_USER(ptr); break; \
  88. default: BUILD_BUG(); break; \
  89. } \
  90. } \
  91. \
  92. (x) = (__force __typeof__(*(ptr))) __gu_val; \
  93. __gu_err; \
  94. })
  95. #define __get_kernel_asm(ldx, ptr) \
  96. __asm__("\n1:\t" ldx "\t0(%2),%0\n\t" \
  97. ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_get_user_skip_1)\
  98. : "=r"(__gu_val), "=r"(__gu_err) \
  99. : "r"(ptr), "1"(__gu_err) \
  100. : "r1");
  101. #define __get_user_asm(ldx, ptr) \
  102. __asm__("\n1:\t" ldx "\t0(%%sr3,%2),%0\n\t" \
  103. ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_get_user_skip_1)\
  104. : "=r"(__gu_val), "=r"(__gu_err) \
  105. : "r"(ptr), "1"(__gu_err) \
  106. : "r1");
  107. #define __put_user(x, ptr) \
  108. ({ \
  109. register long __pu_err __asm__ ("r8") = 0; \
  110. __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x); \
  111. \
  112. if (segment_eq(get_fs(), KERNEL_DS)) { \
  113. switch (sizeof(*(ptr))) { \
  114. case 1: __put_kernel_asm("stb", __x, ptr); break; \
  115. case 2: __put_kernel_asm("sth", __x, ptr); break; \
  116. case 4: __put_kernel_asm("stw", __x, ptr); break; \
  117. case 8: STD_KERNEL(__x, ptr); break; \
  118. default: BUILD_BUG(); break; \
  119. } \
  120. } \
  121. else { \
  122. switch (sizeof(*(ptr))) { \
  123. case 1: __put_user_asm("stb", __x, ptr); break; \
  124. case 2: __put_user_asm("sth", __x, ptr); break; \
  125. case 4: __put_user_asm("stw", __x, ptr); break; \
  126. case 8: STD_USER(__x, ptr); break; \
  127. default: BUILD_BUG(); break; \
  128. } \
  129. } \
  130. \
  131. __pu_err; \
  132. })
  133. /*
  134. * The "__put_user/kernel_asm()" macros tell gcc they read from memory
  135. * instead of writing. This is because they do not write to any memory
  136. * gcc knows about, so there are no aliasing issues. These macros must
  137. * also be aware that "fixup_put_user_skip_[12]" are executed in the
  138. * context of the fault, and any registers used there must be listed
  139. * as clobbers. In this case only "r1" is used by the current routines.
  140. * r8/r9 are already listed as err/val.
  141. */
  142. #define __put_kernel_asm(stx, x, ptr) \
  143. __asm__ __volatile__ ( \
  144. "\n1:\t" stx "\t%2,0(%1)\n\t" \
  145. ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_1)\
  146. : "=r"(__pu_err) \
  147. : "r"(ptr), "r"(x), "0"(__pu_err) \
  148. : "r1")
  149. #define __put_user_asm(stx, x, ptr) \
  150. __asm__ __volatile__ ( \
  151. "\n1:\t" stx "\t%2,0(%%sr3,%1)\n\t" \
  152. ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_1)\
  153. : "=r"(__pu_err) \
  154. : "r"(ptr), "r"(x), "0"(__pu_err) \
  155. : "r1")
  156. #if !defined(CONFIG_64BIT)
  157. #define __put_kernel_asm64(__val, ptr) do { \
  158. __asm__ __volatile__ ( \
  159. "\n1:\tstw %2,0(%1)" \
  160. "\n2:\tstw %R2,4(%1)\n\t" \
  161. ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_2)\
  162. ASM_EXCEPTIONTABLE_ENTRY(2b, fixup_put_user_skip_1)\
  163. : "=r"(__pu_err) \
  164. : "r"(ptr), "r"(__val), "0"(__pu_err) \
  165. : "r1"); \
  166. } while (0)
  167. #define __put_user_asm64(__val, ptr) do { \
  168. __asm__ __volatile__ ( \
  169. "\n1:\tstw %2,0(%%sr3,%1)" \
  170. "\n2:\tstw %R2,4(%%sr3,%1)\n\t" \
  171. ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_2)\
  172. ASM_EXCEPTIONTABLE_ENTRY(2b, fixup_put_user_skip_1)\
  173. : "=r"(__pu_err) \
  174. : "r"(ptr), "r"(__val), "0"(__pu_err) \
  175. : "r1"); \
  176. } while (0)
  177. #endif /* !defined(CONFIG_64BIT) */
  178. /*
  179. * Complex access routines -- external declarations
  180. */
  181. extern unsigned long lcopy_to_user(void __user *, const void *, unsigned long);
  182. extern unsigned long lcopy_from_user(void *, const void __user *, unsigned long);
  183. extern unsigned long lcopy_in_user(void __user *, const void __user *, unsigned long);
  184. extern long strncpy_from_user(char *, const char __user *, long);
  185. extern unsigned lclear_user(void __user *, unsigned long);
  186. extern long lstrnlen_user(const char __user *, long);
  187. /*
  188. * Complex access routines -- macros
  189. */
  190. #define user_addr_max() (~0UL)
  191. #define strnlen_user lstrnlen_user
  192. #define strlen_user(str) lstrnlen_user(str, 0x7fffffffL)
  193. #define clear_user lclear_user
  194. #define __clear_user lclear_user
  195. unsigned long copy_to_user(void __user *dst, const void *src, unsigned long len);
  196. #define __copy_to_user copy_to_user
  197. unsigned long __copy_from_user(void *dst, const void __user *src, unsigned long len);
  198. unsigned long copy_in_user(void __user *dst, const void __user *src, unsigned long len);
  199. #define __copy_in_user copy_in_user
  200. #define __copy_to_user_inatomic __copy_to_user
  201. #define __copy_from_user_inatomic __copy_from_user
  202. extern void copy_from_user_overflow(void)
  203. #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
  204. __compiletime_error("copy_from_user() buffer size is not provably correct")
  205. #else
  206. __compiletime_warning("copy_from_user() buffer size is not provably correct")
  207. #endif
  208. ;
  209. static inline unsigned long __must_check copy_from_user(void *to,
  210. const void __user *from,
  211. unsigned long n)
  212. {
  213. int sz = __compiletime_object_size(to);
  214. int ret = -EFAULT;
  215. if (likely(sz == -1 || !__builtin_constant_p(n) || sz >= n))
  216. ret = __copy_from_user(to, from, n);
  217. else
  218. copy_from_user_overflow();
  219. return ret;
  220. }
  221. struct pt_regs;
  222. int fixup_exception(struct pt_regs *regs);
  223. #endif /* __PARISC_UACCESS_H */