uaccess.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #ifndef _ARCH_POWERPC_UACCESS_H
  2. #define _ARCH_POWERPC_UACCESS_H
  3. #ifdef __KERNEL__
  4. #ifndef __ASSEMBLY__
  5. #include <linux/sched.h>
  6. #include <linux/errno.h>
  7. #include <asm/asm-compat.h>
  8. #include <asm/processor.h>
  9. #include <asm/page.h>
  10. #define VERIFY_READ 0
  11. #define VERIFY_WRITE 1
  12. /*
  13. * The fs value determines whether argument validity checking should be
  14. * performed or not. If get_fs() == USER_DS, checking is performed, with
  15. * get_fs() == KERNEL_DS, checking is bypassed.
  16. *
  17. * For historical reasons, these macros are grossly misnamed.
  18. *
  19. * The fs/ds values are now the highest legal address in the "segment".
  20. * This simplifies the checking in the routines below.
  21. */
  22. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  23. #define KERNEL_DS MAKE_MM_SEG(~0UL)
  24. #ifdef __powerpc64__
  25. /* We use TASK_SIZE_USER64 as TASK_SIZE is not constant */
  26. #define USER_DS MAKE_MM_SEG(TASK_SIZE_USER64 - 1)
  27. #else
  28. #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
  29. #endif
  30. #define get_ds() (KERNEL_DS)
  31. #define get_fs() (current->thread.fs)
  32. #define set_fs(val) (current->thread.fs = (val))
  33. #define segment_eq(a, b) ((a).seg == (b).seg)
  34. #define user_addr_max() (get_fs().seg)
  35. #ifdef __powerpc64__
  36. /*
  37. * This check is sufficient because there is a large enough
  38. * gap between user addresses and the kernel addresses
  39. */
  40. #define __access_ok(addr, size, segment) \
  41. (((addr) <= (segment).seg) && ((size) <= (segment).seg))
  42. #else
  43. #define __access_ok(addr, size, segment) \
  44. (((addr) <= (segment).seg) && \
  45. (((size) == 0) || (((size) - 1) <= ((segment).seg - (addr)))))
  46. #endif
  47. #define access_ok(type, addr, size) \
  48. (__chk_user_ptr(addr), (void)(type), \
  49. __access_ok((__force unsigned long)(addr), (size), get_fs()))
  50. /*
  51. * The exception table consists of pairs of addresses: the first is the
  52. * address of an instruction that is allowed to fault, and the second is
  53. * the address at which the program should continue. No registers are
  54. * modified, so it is entirely up to the continuation code to figure out
  55. * what to do.
  56. *
  57. * All the routines below use bits of fixup code that are out of line
  58. * with the main instruction path. This means when everything is well,
  59. * we don't even have to jump over them. Further, they do not intrude
  60. * on our cache or tlb entries.
  61. */
  62. struct exception_table_entry {
  63. unsigned long insn;
  64. unsigned long fixup;
  65. };
  66. /*
  67. * These are the main single-value transfer routines. They automatically
  68. * use the right size if we just have the right pointer type.
  69. *
  70. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  71. * and yet we don't want to do any pointers, because that is too much
  72. * of a performance impact. Thus we have a few rather ugly macros here,
  73. * and hide all the ugliness from the user.
  74. *
  75. * The "__xxx" versions of the user access functions are versions that
  76. * do not verify the address space, that must have been done previously
  77. * with a separate "access_ok()" call (this is used when we do multiple
  78. * accesses to the same area of user memory).
  79. *
  80. * As we use the same address space for kernel and user data on the
  81. * PowerPC, we can just do these as direct assignments. (Of course, the
  82. * exception handling means that it's no longer "just"...)
  83. *
  84. */
  85. #define get_user(x, ptr) \
  86. __get_user_check((x), (ptr), sizeof(*(ptr)))
  87. #define put_user(x, ptr) \
  88. __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  89. #define __get_user(x, ptr) \
  90. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  91. #define __put_user(x, ptr) \
  92. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  93. #define __get_user_inatomic(x, ptr) \
  94. __get_user_nosleep((x), (ptr), sizeof(*(ptr)))
  95. #define __put_user_inatomic(x, ptr) \
  96. __put_user_nosleep((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  97. #define __get_user_unaligned __get_user
  98. #define __put_user_unaligned __put_user
  99. extern long __put_user_bad(void);
  100. /*
  101. * We don't tell gcc that we are accessing memory, but this is OK
  102. * because we do not write to any memory gcc knows about, so there
  103. * are no aliasing issues.
  104. */
  105. #define __put_user_asm(x, addr, err, op) \
  106. __asm__ __volatile__( \
  107. "1: " op " %1,0(%2) # put_user\n" \
  108. "2:\n" \
  109. ".section .fixup,\"ax\"\n" \
  110. "3: li %0,%3\n" \
  111. " b 2b\n" \
  112. ".previous\n" \
  113. ".section __ex_table,\"a\"\n" \
  114. PPC_LONG_ALIGN "\n" \
  115. PPC_LONG "1b,3b\n" \
  116. ".previous" \
  117. : "=r" (err) \
  118. : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
  119. #ifdef __powerpc64__
  120. #define __put_user_asm2(x, ptr, retval) \
  121. __put_user_asm(x, ptr, retval, "std")
  122. #else /* __powerpc64__ */
  123. #define __put_user_asm2(x, addr, err) \
  124. __asm__ __volatile__( \
  125. "1: stw %1,0(%2)\n" \
  126. "2: stw %1+1,4(%2)\n" \
  127. "3:\n" \
  128. ".section .fixup,\"ax\"\n" \
  129. "4: li %0,%3\n" \
  130. " b 3b\n" \
  131. ".previous\n" \
  132. ".section __ex_table,\"a\"\n" \
  133. PPC_LONG_ALIGN "\n" \
  134. PPC_LONG "1b,4b\n" \
  135. PPC_LONG "2b,4b\n" \
  136. ".previous" \
  137. : "=r" (err) \
  138. : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
  139. #endif /* __powerpc64__ */
  140. #define __put_user_size(x, ptr, size, retval) \
  141. do { \
  142. retval = 0; \
  143. switch (size) { \
  144. case 1: __put_user_asm(x, ptr, retval, "stb"); break; \
  145. case 2: __put_user_asm(x, ptr, retval, "sth"); break; \
  146. case 4: __put_user_asm(x, ptr, retval, "stw"); break; \
  147. case 8: __put_user_asm2(x, ptr, retval); break; \
  148. default: __put_user_bad(); \
  149. } \
  150. } while (0)
  151. #define __put_user_nocheck(x, ptr, size) \
  152. ({ \
  153. long __pu_err; \
  154. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  155. if (!is_kernel_addr((unsigned long)__pu_addr)) \
  156. might_fault(); \
  157. __chk_user_ptr(ptr); \
  158. __put_user_size((x), __pu_addr, (size), __pu_err); \
  159. __pu_err; \
  160. })
  161. #define __put_user_check(x, ptr, size) \
  162. ({ \
  163. long __pu_err = -EFAULT; \
  164. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  165. might_fault(); \
  166. if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
  167. __put_user_size((x), __pu_addr, (size), __pu_err); \
  168. __pu_err; \
  169. })
  170. #define __put_user_nosleep(x, ptr, size) \
  171. ({ \
  172. long __pu_err; \
  173. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  174. __chk_user_ptr(ptr); \
  175. __put_user_size((x), __pu_addr, (size), __pu_err); \
  176. __pu_err; \
  177. })
  178. extern long __get_user_bad(void);
  179. #define __get_user_asm(x, addr, err, op) \
  180. __asm__ __volatile__( \
  181. "1: "op" %1,0(%2) # get_user\n" \
  182. "2:\n" \
  183. ".section .fixup,\"ax\"\n" \
  184. "3: li %0,%3\n" \
  185. " li %1,0\n" \
  186. " b 2b\n" \
  187. ".previous\n" \
  188. ".section __ex_table,\"a\"\n" \
  189. PPC_LONG_ALIGN "\n" \
  190. PPC_LONG "1b,3b\n" \
  191. ".previous" \
  192. : "=r" (err), "=r" (x) \
  193. : "b" (addr), "i" (-EFAULT), "0" (err))
  194. #ifdef __powerpc64__
  195. #define __get_user_asm2(x, addr, err) \
  196. __get_user_asm(x, addr, err, "ld")
  197. #else /* __powerpc64__ */
  198. #define __get_user_asm2(x, addr, err) \
  199. __asm__ __volatile__( \
  200. "1: lwz %1,0(%2)\n" \
  201. "2: lwz %1+1,4(%2)\n" \
  202. "3:\n" \
  203. ".section .fixup,\"ax\"\n" \
  204. "4: li %0,%3\n" \
  205. " li %1,0\n" \
  206. " li %1+1,0\n" \
  207. " b 3b\n" \
  208. ".previous\n" \
  209. ".section __ex_table,\"a\"\n" \
  210. PPC_LONG_ALIGN "\n" \
  211. PPC_LONG "1b,4b\n" \
  212. PPC_LONG "2b,4b\n" \
  213. ".previous" \
  214. : "=r" (err), "=&r" (x) \
  215. : "b" (addr), "i" (-EFAULT), "0" (err))
  216. #endif /* __powerpc64__ */
  217. #define __get_user_size(x, ptr, size, retval) \
  218. do { \
  219. retval = 0; \
  220. __chk_user_ptr(ptr); \
  221. if (size > sizeof(x)) \
  222. (x) = __get_user_bad(); \
  223. switch (size) { \
  224. case 1: __get_user_asm(x, ptr, retval, "lbz"); break; \
  225. case 2: __get_user_asm(x, ptr, retval, "lhz"); break; \
  226. case 4: __get_user_asm(x, ptr, retval, "lwz"); break; \
  227. case 8: __get_user_asm2(x, ptr, retval); break; \
  228. default: (x) = __get_user_bad(); \
  229. } \
  230. } while (0)
  231. #define __get_user_nocheck(x, ptr, size) \
  232. ({ \
  233. long __gu_err; \
  234. unsigned long __gu_val; \
  235. __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  236. __chk_user_ptr(ptr); \
  237. if (!is_kernel_addr((unsigned long)__gu_addr)) \
  238. might_fault(); \
  239. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  240. (x) = (__typeof__(*(ptr)))__gu_val; \
  241. __gu_err; \
  242. })
  243. #ifndef __powerpc64__
  244. #define __get_user64_nocheck(x, ptr, size) \
  245. ({ \
  246. long __gu_err; \
  247. long long __gu_val; \
  248. __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  249. __chk_user_ptr(ptr); \
  250. if (!is_kernel_addr((unsigned long)__gu_addr)) \
  251. might_fault(); \
  252. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  253. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  254. __gu_err; \
  255. })
  256. #endif /* __powerpc64__ */
  257. #define __get_user_check(x, ptr, size) \
  258. ({ \
  259. long __gu_err = -EFAULT; \
  260. unsigned long __gu_val = 0; \
  261. __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  262. might_fault(); \
  263. if (access_ok(VERIFY_READ, __gu_addr, (size))) \
  264. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  265. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  266. __gu_err; \
  267. })
  268. #define __get_user_nosleep(x, ptr, size) \
  269. ({ \
  270. long __gu_err; \
  271. unsigned long __gu_val; \
  272. __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  273. __chk_user_ptr(ptr); \
  274. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  275. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  276. __gu_err; \
  277. })
  278. /* more complex routines */
  279. extern unsigned long __copy_tofrom_user(void __user *to,
  280. const void __user *from, unsigned long size);
  281. #ifndef __powerpc64__
  282. static inline unsigned long copy_from_user(void *to,
  283. const void __user *from, unsigned long n)
  284. {
  285. if (likely(access_ok(VERIFY_READ, from, n)))
  286. return __copy_tofrom_user((__force void __user *)to, from, n);
  287. memset(to, 0, n);
  288. return n;
  289. }
  290. static inline unsigned long copy_to_user(void __user *to,
  291. const void *from, unsigned long n)
  292. {
  293. if (access_ok(VERIFY_WRITE, to, n))
  294. return __copy_tofrom_user(to, (__force void __user *)from, n);
  295. return n;
  296. }
  297. #else /* __powerpc64__ */
  298. #define __copy_in_user(to, from, size) \
  299. __copy_tofrom_user((to), (from), (size))
  300. extern unsigned long copy_from_user(void *to, const void __user *from,
  301. unsigned long n);
  302. extern unsigned long copy_to_user(void __user *to, const void *from,
  303. unsigned long n);
  304. extern unsigned long copy_in_user(void __user *to, const void __user *from,
  305. unsigned long n);
  306. #endif /* __powerpc64__ */
  307. static inline unsigned long __copy_from_user_inatomic(void *to,
  308. const void __user *from, unsigned long n)
  309. {
  310. if (__builtin_constant_p(n) && (n <= 8)) {
  311. unsigned long ret = 1;
  312. switch (n) {
  313. case 1:
  314. __get_user_size(*(u8 *)to, from, 1, ret);
  315. break;
  316. case 2:
  317. __get_user_size(*(u16 *)to, from, 2, ret);
  318. break;
  319. case 4:
  320. __get_user_size(*(u32 *)to, from, 4, ret);
  321. break;
  322. case 8:
  323. __get_user_size(*(u64 *)to, from, 8, ret);
  324. break;
  325. }
  326. if (ret == 0)
  327. return 0;
  328. }
  329. return __copy_tofrom_user((__force void __user *)to, from, n);
  330. }
  331. static inline unsigned long __copy_to_user_inatomic(void __user *to,
  332. const void *from, unsigned long n)
  333. {
  334. if (__builtin_constant_p(n) && (n <= 8)) {
  335. unsigned long ret = 1;
  336. switch (n) {
  337. case 1:
  338. __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret);
  339. break;
  340. case 2:
  341. __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret);
  342. break;
  343. case 4:
  344. __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret);
  345. break;
  346. case 8:
  347. __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret);
  348. break;
  349. }
  350. if (ret == 0)
  351. return 0;
  352. }
  353. return __copy_tofrom_user(to, (__force const void __user *)from, n);
  354. }
  355. static inline unsigned long __copy_from_user(void *to,
  356. const void __user *from, unsigned long size)
  357. {
  358. might_fault();
  359. return __copy_from_user_inatomic(to, from, size);
  360. }
  361. static inline unsigned long __copy_to_user(void __user *to,
  362. const void *from, unsigned long size)
  363. {
  364. might_fault();
  365. return __copy_to_user_inatomic(to, from, size);
  366. }
  367. extern unsigned long __clear_user(void __user *addr, unsigned long size);
  368. static inline unsigned long clear_user(void __user *addr, unsigned long size)
  369. {
  370. might_fault();
  371. if (likely(access_ok(VERIFY_WRITE, addr, size)))
  372. return __clear_user(addr, size);
  373. return size;
  374. }
  375. extern long strncpy_from_user(char *dst, const char __user *src, long count);
  376. extern __must_check long strlen_user(const char __user *str);
  377. extern __must_check long strnlen_user(const char __user *str, long n);
  378. #endif /* __ASSEMBLY__ */
  379. #endif /* __KERNEL__ */
  380. #endif /* _ARCH_POWERPC_UACCESS_H */