uaccess.h 13 KB

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