uaccess.h 13 KB

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