uaccess.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #ifdef __KERNEL__
  2. #ifndef _PPC_UACCESS_H
  3. #define _PPC_UACCESS_H
  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 KERNEL_DS ((mm_segment_t) { ~0UL })
  21. #define USER_DS ((mm_segment_t) { TASK_SIZE - 1 })
  22. #define get_ds() (KERNEL_DS)
  23. #define get_fs() (current->thread.fs)
  24. #define set_fs(val) (current->thread.fs = (val))
  25. #define segment_eq(a,b) ((a).seg == (b).seg)
  26. #define __access_ok(addr,size) \
  27. ((addr) <= current->thread.fs.seg \
  28. && ((size) == 0 || (size) - 1 <= current->thread.fs.seg - (addr)))
  29. #define access_ok(type, addr, size) \
  30. (__chk_user_ptr(addr),__access_ok((unsigned long)(addr),(size)))
  31. /* this function will go away soon - use access_ok() instead */
  32. extern inline int __deprecated verify_area(int type, const void __user * addr, unsigned long size)
  33. {
  34. return access_ok(type, addr, size) ? 0 : -EFAULT;
  35. }
  36. /*
  37. * The exception table consists of pairs of addresses: the first is the
  38. * address of an instruction that is allowed to fault, and the second is
  39. * the address at which the program should continue. No registers are
  40. * modified, so it is entirely up to the continuation code to figure out
  41. * what to do.
  42. *
  43. * All the routines below use bits of fixup code that are out of line
  44. * with the main instruction path. This means when everything is well,
  45. * we don't even have to jump over them. Further, they do not intrude
  46. * on our cache or tlb entries.
  47. */
  48. struct exception_table_entry
  49. {
  50. unsigned long insn, fixup;
  51. };
  52. /*
  53. * These are the main single-value transfer routines. They automatically
  54. * use the right size if we just have the right pointer type.
  55. *
  56. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  57. * and yet we don't want to do any pointers, because that is too much
  58. * of a performance impact. Thus we have a few rather ugly macros here,
  59. * and hide all the ugliness from the user.
  60. *
  61. * The "__xxx" versions of the user access functions are versions that
  62. * do not verify the address space, that must have been done previously
  63. * with a separate "access_ok()" call (this is used when we do multiple
  64. * accesses to the same area of user memory).
  65. *
  66. * As we use the same address space for kernel and user data on the
  67. * PowerPC, we can just do these as direct assignments. (Of course, the
  68. * exception handling means that it's no longer "just"...)
  69. *
  70. * The "user64" versions of the user access functions are versions that
  71. * allow access of 64-bit data. The "get_user" functions do not
  72. * properly handle 64-bit data because the value gets down cast to a long.
  73. * The "put_user" functions already handle 64-bit data properly but we add
  74. * "user64" versions for completeness
  75. */
  76. #define get_user(x,ptr) \
  77. __get_user_check((x),(ptr),sizeof(*(ptr)))
  78. #define get_user64(x,ptr) \
  79. __get_user64_check((x),(ptr),sizeof(*(ptr)))
  80. #define put_user(x,ptr) \
  81. __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  82. #define put_user64(x,ptr) put_user(x,ptr)
  83. #define __get_user(x,ptr) \
  84. __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
  85. #define __get_user64(x,ptr) \
  86. __get_user64_nocheck((x),(ptr),sizeof(*(ptr)))
  87. #define __put_user(x,ptr) \
  88. __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  89. #define __put_user64(x,ptr) __put_user(x,ptr)
  90. extern long __put_user_bad(void);
  91. #define __put_user_nocheck(x,ptr,size) \
  92. ({ \
  93. long __pu_err; \
  94. __chk_user_ptr(ptr); \
  95. __put_user_size((x),(ptr),(size),__pu_err); \
  96. __pu_err; \
  97. })
  98. #define __put_user_check(x,ptr,size) \
  99. ({ \
  100. long __pu_err = -EFAULT; \
  101. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  102. if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
  103. __put_user_size((x),__pu_addr,(size),__pu_err); \
  104. __pu_err; \
  105. })
  106. #define __put_user_size(x,ptr,size,retval) \
  107. do { \
  108. retval = 0; \
  109. switch (size) { \
  110. case 1: \
  111. __put_user_asm(x, ptr, retval, "stb"); \
  112. break; \
  113. case 2: \
  114. __put_user_asm(x, ptr, retval, "sth"); \
  115. break; \
  116. case 4: \
  117. __put_user_asm(x, ptr, retval, "stw"); \
  118. break; \
  119. case 8: \
  120. __put_user_asm2(x, ptr, retval); \
  121. break; \
  122. default: \
  123. __put_user_bad(); \
  124. } \
  125. } while (0)
  126. /*
  127. * We don't tell gcc that we are accessing memory, but this is OK
  128. * because we do not write to any memory gcc knows about, so there
  129. * are no aliasing issues.
  130. */
  131. #define __put_user_asm(x, addr, err, op) \
  132. __asm__ __volatile__( \
  133. "1: "op" %1,0(%2)\n" \
  134. "2:\n" \
  135. ".section .fixup,\"ax\"\n" \
  136. "3: li %0,%3\n" \
  137. " b 2b\n" \
  138. ".previous\n" \
  139. ".section __ex_table,\"a\"\n" \
  140. " .align 2\n" \
  141. " .long 1b,3b\n" \
  142. ".previous" \
  143. : "=r" (err) \
  144. : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
  145. #define __put_user_asm2(x, addr, err) \
  146. __asm__ __volatile__( \
  147. "1: stw %1,0(%2)\n" \
  148. "2: stw %1+1,4(%2)\n" \
  149. "3:\n" \
  150. ".section .fixup,\"ax\"\n" \
  151. "4: li %0,%3\n" \
  152. " b 3b\n" \
  153. ".previous\n" \
  154. ".section __ex_table,\"a\"\n" \
  155. " .align 2\n" \
  156. " .long 1b,4b\n" \
  157. " .long 2b,4b\n" \
  158. ".previous" \
  159. : "=r" (err) \
  160. : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
  161. #define __get_user_nocheck(x, ptr, size) \
  162. ({ \
  163. long __gu_err; \
  164. unsigned long __gu_val; \
  165. __chk_user_ptr(ptr); \
  166. __get_user_size(__gu_val, (ptr), (size), __gu_err); \
  167. (x) = (__typeof__(*(ptr)))__gu_val; \
  168. __gu_err; \
  169. })
  170. #define __get_user64_nocheck(x, ptr, size) \
  171. ({ \
  172. long __gu_err; \
  173. long long __gu_val; \
  174. __chk_user_ptr(ptr); \
  175. __get_user_size64(__gu_val, (ptr), (size), __gu_err); \
  176. (x) = (__typeof__(*(ptr)))__gu_val; \
  177. __gu_err; \
  178. })
  179. #define __get_user_check(x, ptr, size) \
  180. ({ \
  181. long __gu_err = -EFAULT; \
  182. unsigned long __gu_val = 0; \
  183. const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  184. if (access_ok(VERIFY_READ, __gu_addr, (size))) \
  185. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  186. (x) = (__typeof__(*(ptr)))__gu_val; \
  187. __gu_err; \
  188. })
  189. #define __get_user64_check(x, ptr, size) \
  190. ({ \
  191. long __gu_err = -EFAULT; \
  192. long long __gu_val = 0; \
  193. const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  194. if (access_ok(VERIFY_READ, __gu_addr, (size))) \
  195. __get_user_size64(__gu_val, __gu_addr, (size), __gu_err); \
  196. (x) = (__typeof__(*(ptr)))__gu_val; \
  197. __gu_err; \
  198. })
  199. extern long __get_user_bad(void);
  200. #define __get_user_size(x, ptr, size, retval) \
  201. do { \
  202. retval = 0; \
  203. switch (size) { \
  204. case 1: \
  205. __get_user_asm(x, ptr, retval, "lbz"); \
  206. break; \
  207. case 2: \
  208. __get_user_asm(x, ptr, retval, "lhz"); \
  209. break; \
  210. case 4: \
  211. __get_user_asm(x, ptr, retval, "lwz"); \
  212. break; \
  213. default: \
  214. x = __get_user_bad(); \
  215. } \
  216. } while (0)
  217. #define __get_user_size64(x, ptr, size, retval) \
  218. do { \
  219. retval = 0; \
  220. switch (size) { \
  221. case 1: \
  222. __get_user_asm(x, ptr, retval, "lbz"); \
  223. break; \
  224. case 2: \
  225. __get_user_asm(x, ptr, retval, "lhz"); \
  226. break; \
  227. case 4: \
  228. __get_user_asm(x, ptr, retval, "lwz"); \
  229. break; \
  230. case 8: \
  231. __get_user_asm2(x, ptr, retval); \
  232. break; \
  233. default: \
  234. x = __get_user_bad(); \
  235. } \
  236. } while (0)
  237. #define __get_user_asm(x, addr, err, op) \
  238. __asm__ __volatile__( \
  239. "1: "op" %1,0(%2)\n" \
  240. "2:\n" \
  241. ".section .fixup,\"ax\"\n" \
  242. "3: li %0,%3\n" \
  243. " li %1,0\n" \
  244. " b 2b\n" \
  245. ".previous\n" \
  246. ".section __ex_table,\"a\"\n" \
  247. " .align 2\n" \
  248. " .long 1b,3b\n" \
  249. ".previous" \
  250. : "=r"(err), "=r"(x) \
  251. : "b"(addr), "i"(-EFAULT), "0"(err))
  252. #define __get_user_asm2(x, addr, err) \
  253. __asm__ __volatile__( \
  254. "1: lwz %1,0(%2)\n" \
  255. "2: lwz %1+1,4(%2)\n" \
  256. "3:\n" \
  257. ".section .fixup,\"ax\"\n" \
  258. "4: li %0,%3\n" \
  259. " li %1,0\n" \
  260. " li %1+1,0\n" \
  261. " b 3b\n" \
  262. ".previous\n" \
  263. ".section __ex_table,\"a\"\n" \
  264. " .align 2\n" \
  265. " .long 1b,4b\n" \
  266. " .long 2b,4b\n" \
  267. ".previous" \
  268. : "=r"(err), "=&r"(x) \
  269. : "b"(addr), "i"(-EFAULT), "0"(err))
  270. /* more complex routines */
  271. extern int __copy_tofrom_user(void __user *to, const void __user *from,
  272. unsigned long size);
  273. extern inline unsigned long
  274. copy_from_user(void *to, const void __user *from, unsigned long n)
  275. {
  276. unsigned long over;
  277. if (access_ok(VERIFY_READ, from, n))
  278. return __copy_tofrom_user((__force void __user *)to, from, n);
  279. if ((unsigned long)from < TASK_SIZE) {
  280. over = (unsigned long)from + n - TASK_SIZE;
  281. return __copy_tofrom_user((__force void __user *)to, from, n - over) + over;
  282. }
  283. return n;
  284. }
  285. extern inline unsigned long
  286. copy_to_user(void __user *to, 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, n - over) + over;
  294. }
  295. return n;
  296. }
  297. static inline unsigned long __copy_from_user(void *to, const void __user *from, unsigned long size)
  298. {
  299. return __copy_tofrom_user((__force void __user *)to, from, size);
  300. }
  301. static inline unsigned long __copy_to_user(void __user *to, const void *from, unsigned long size)
  302. {
  303. return __copy_tofrom_user(to, (__force void __user *)from, size);
  304. }
  305. #define __copy_to_user_inatomic __copy_to_user
  306. #define __copy_from_user_inatomic __copy_from_user
  307. extern unsigned long __clear_user(void __user *addr, unsigned long size);
  308. extern inline unsigned long
  309. clear_user(void __user *addr, unsigned long size)
  310. {
  311. if (access_ok(VERIFY_WRITE, addr, size))
  312. return __clear_user(addr, size);
  313. if ((unsigned long)addr < TASK_SIZE) {
  314. unsigned long over = (unsigned long)addr + size - TASK_SIZE;
  315. return __clear_user(addr, size - over) + over;
  316. }
  317. return size;
  318. }
  319. extern int __strncpy_from_user(char *dst, const char __user *src, long count);
  320. extern inline long
  321. strncpy_from_user(char *dst, const char __user *src, long count)
  322. {
  323. if (access_ok(VERIFY_READ, src, 1))
  324. return __strncpy_from_user(dst, src, count);
  325. return -EFAULT;
  326. }
  327. /*
  328. * Return the size of a string (including the ending 0)
  329. *
  330. * Return 0 for error
  331. */
  332. extern int __strnlen_user(const char __user *str, long len, unsigned long top);
  333. /*
  334. * Returns the length of the string at str (including the null byte),
  335. * or 0 if we hit a page we can't access,
  336. * or something > len if we didn't find a null byte.
  337. *
  338. * The `top' parameter to __strnlen_user is to make sure that
  339. * we can never overflow from the user area into kernel space.
  340. */
  341. extern __inline__ int strnlen_user(const char __user *str, long len)
  342. {
  343. unsigned long top = current->thread.fs.seg;
  344. if ((unsigned long)str > top)
  345. return 0;
  346. return __strnlen_user(str, len, top);
  347. }
  348. #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
  349. #endif /* __ASSEMBLY__ */
  350. #endif /* _PPC_UACCESS_H */
  351. #endif /* __KERNEL__ */