uaccess.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #ifndef __SCORE_UACCESS_H
  2. #define __SCORE_UACCESS_H
  3. #include <linux/kernel.h>
  4. #include <linux/errno.h>
  5. #include <linux/thread_info.h>
  6. #define VERIFY_READ 0
  7. #define VERIFY_WRITE 1
  8. #define get_ds() (KERNEL_DS)
  9. #define get_fs() (current_thread_info()->addr_limit)
  10. #define segment_eq(a, b) ((a).seg == (b).seg)
  11. /*
  12. * Is a address valid? This does a straighforward calculation rather
  13. * than tests.
  14. *
  15. * Address valid if:
  16. * - "addr" doesn't have any high-bits set
  17. * - AND "size" doesn't have any high-bits set
  18. * - AND "addr+size" doesn't have any high-bits set
  19. * - OR we are in kernel mode.
  20. *
  21. * __ua_size() is a trick to avoid runtime checking of positive constant
  22. * sizes; for those we already know at compile time that the size is ok.
  23. */
  24. #define __ua_size(size) \
  25. ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
  26. /*
  27. * access_ok: - Checks if a user space pointer is valid
  28. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  29. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  30. * to write to a block, it is always safe to read from it.
  31. * @addr: User space pointer to start of block to check
  32. * @size: Size of block to check
  33. *
  34. * Context: User context only. This function may sleep if pagefaults are
  35. * enabled.
  36. *
  37. * Checks if a pointer to a block of memory in user space is valid.
  38. *
  39. * Returns true (nonzero) if the memory block may be valid, false (zero)
  40. * if it is definitely invalid.
  41. *
  42. * Note that, depending on architecture, this function probably just
  43. * checks that the pointer is in the user space range - after calling
  44. * this function, memory access functions may still return -EFAULT.
  45. */
  46. #define __access_ok(addr, size) \
  47. (((long)((get_fs().seg) & \
  48. ((addr) | ((addr) + (size)) | \
  49. __ua_size(size)))) == 0)
  50. #define access_ok(type, addr, size) \
  51. likely(__access_ok((unsigned long)(addr), (size)))
  52. /*
  53. * put_user: - Write a simple value into user space.
  54. * @x: Value to copy to user space.
  55. * @ptr: Destination address, in user space.
  56. *
  57. * Context: User context only. This function may sleep if pagefaults are
  58. * enabled.
  59. *
  60. * This macro copies a single simple value from kernel space to user
  61. * space. It supports simple types like char and int, but not larger
  62. * data types like structures or arrays.
  63. *
  64. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  65. * to the result of dereferencing @ptr.
  66. *
  67. * Returns zero on success, or -EFAULT on error.
  68. */
  69. #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
  70. /*
  71. * get_user: - Get a simple variable from user space.
  72. * @x: Variable to store result.
  73. * @ptr: Source address, in user space.
  74. *
  75. * Context: User context only. This function may sleep if pagefaults are
  76. * enabled.
  77. *
  78. * This macro copies a single simple variable from user space to kernel
  79. * space. It supports simple types like char and int, but not larger
  80. * data types like structures or arrays.
  81. *
  82. * @ptr must have pointer-to-simple-variable type, and the result of
  83. * dereferencing @ptr must be assignable to @x without a cast.
  84. *
  85. * Returns zero on success, or -EFAULT on error.
  86. * On error, the variable @x is set to zero.
  87. */
  88. #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
  89. /*
  90. * __put_user: - Write a simple value into user space, with less checking.
  91. * @x: Value to copy to user space.
  92. * @ptr: Destination address, in user space.
  93. *
  94. * Context: User context only. This function may sleep if pagefaults are
  95. * enabled.
  96. *
  97. * This macro copies a single simple value from kernel space to user
  98. * space. It supports simple types like char and int, but not larger
  99. * data types like structures or arrays.
  100. *
  101. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  102. * to the result of dereferencing @ptr.
  103. *
  104. * Caller must check the pointer with access_ok() before calling this
  105. * function.
  106. *
  107. * Returns zero on success, or -EFAULT on error.
  108. */
  109. #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  110. /*
  111. * __get_user: - Get a simple variable from user space, with less checking.
  112. * @x: Variable to store result.
  113. * @ptr: Source address, in user space.
  114. *
  115. * Context: User context only. This function may sleep if pagefaults are
  116. * enabled.
  117. *
  118. * This macro copies a single simple variable from user space to kernel
  119. * space. It supports simple types like char and int, but not larger
  120. * data types like structures or arrays.
  121. *
  122. * @ptr must have pointer-to-simple-variable type, and the result of
  123. * dereferencing @ptr must be assignable to @x without a cast.
  124. *
  125. * Caller must check the pointer with access_ok() before calling this
  126. * function.
  127. *
  128. * Returns zero on success, or -EFAULT on error.
  129. * On error, the variable @x is set to zero.
  130. */
  131. #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  132. struct __large_struct { unsigned long buf[100]; };
  133. #define __m(x) (*(struct __large_struct __user *)(x))
  134. /*
  135. * Yuck. We need two variants, one for 64bit operation and one
  136. * for 32 bit mode and old iron.
  137. */
  138. extern void __get_user_unknown(void);
  139. #define __get_user_common(val, size, ptr) \
  140. do { \
  141. switch (size) { \
  142. case 1: \
  143. __get_user_asm(val, "lb", ptr); \
  144. break; \
  145. case 2: \
  146. __get_user_asm(val, "lh", ptr); \
  147. break; \
  148. case 4: \
  149. __get_user_asm(val, "lw", ptr); \
  150. break; \
  151. case 8: \
  152. if ((copy_from_user((void *)&val, ptr, 8)) == 0) \
  153. __gu_err = 0; \
  154. else \
  155. __gu_err = -EFAULT; \
  156. break; \
  157. default: \
  158. __get_user_unknown(); \
  159. break; \
  160. } \
  161. } while (0)
  162. #define __get_user_nocheck(x, ptr, size) \
  163. ({ \
  164. long __gu_err = 0; \
  165. __get_user_common((x), size, ptr); \
  166. __gu_err; \
  167. })
  168. #define __get_user_check(x, ptr, size) \
  169. ({ \
  170. long __gu_err = -EFAULT; \
  171. const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
  172. \
  173. if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
  174. __get_user_common((x), size, __gu_ptr); \
  175. \
  176. __gu_err; \
  177. })
  178. #define __get_user_asm(val, insn, addr) \
  179. { \
  180. long __gu_tmp; \
  181. \
  182. __asm__ __volatile__( \
  183. "1:" insn " %1, %3\n" \
  184. "2:\n" \
  185. ".section .fixup,\"ax\"\n" \
  186. "3:li %0, %4\n" \
  187. "j 2b\n" \
  188. ".previous\n" \
  189. ".section __ex_table,\"a\"\n" \
  190. ".word 1b, 3b\n" \
  191. ".previous\n" \
  192. : "=r" (__gu_err), "=r" (__gu_tmp) \
  193. : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
  194. \
  195. (val) = (__typeof__(*(addr))) __gu_tmp; \
  196. }
  197. /*
  198. * Yuck. We need two variants, one for 64bit operation and one
  199. * for 32 bit mode and old iron.
  200. */
  201. #define __put_user_nocheck(val, ptr, size) \
  202. ({ \
  203. __typeof__(*(ptr)) __pu_val; \
  204. long __pu_err = 0; \
  205. \
  206. __pu_val = (val); \
  207. switch (size) { \
  208. case 1: \
  209. __put_user_asm("sb", ptr); \
  210. break; \
  211. case 2: \
  212. __put_user_asm("sh", ptr); \
  213. break; \
  214. case 4: \
  215. __put_user_asm("sw", ptr); \
  216. break; \
  217. case 8: \
  218. if ((__copy_to_user((void *)ptr, &__pu_val, 8)) == 0) \
  219. __pu_err = 0; \
  220. else \
  221. __pu_err = -EFAULT; \
  222. break; \
  223. default: \
  224. __put_user_unknown(); \
  225. break; \
  226. } \
  227. __pu_err; \
  228. })
  229. #define __put_user_check(val, ptr, size) \
  230. ({ \
  231. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  232. __typeof__(*(ptr)) __pu_val = (val); \
  233. long __pu_err = -EFAULT; \
  234. \
  235. if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
  236. switch (size) { \
  237. case 1: \
  238. __put_user_asm("sb", __pu_addr); \
  239. break; \
  240. case 2: \
  241. __put_user_asm("sh", __pu_addr); \
  242. break; \
  243. case 4: \
  244. __put_user_asm("sw", __pu_addr); \
  245. break; \
  246. case 8: \
  247. if ((__copy_to_user((void *)__pu_addr, &__pu_val, 8)) == 0)\
  248. __pu_err = 0; \
  249. else \
  250. __pu_err = -EFAULT; \
  251. break; \
  252. default: \
  253. __put_user_unknown(); \
  254. break; \
  255. } \
  256. } \
  257. __pu_err; \
  258. })
  259. #define __put_user_asm(insn, ptr) \
  260. __asm__ __volatile__( \
  261. "1:" insn " %2, %3\n" \
  262. "2:\n" \
  263. ".section .fixup,\"ax\"\n" \
  264. "3:li %0, %4\n" \
  265. "j 2b\n" \
  266. ".previous\n" \
  267. ".section __ex_table,\"a\"\n" \
  268. ".word 1b, 3b\n" \
  269. ".previous\n" \
  270. : "=r" (__pu_err) \
  271. : "0" (0), "r" (__pu_val), "o" (__m(ptr)), \
  272. "i" (-EFAULT));
  273. extern void __put_user_unknown(void);
  274. extern int __copy_tofrom_user(void *to, const void *from, unsigned long len);
  275. static inline unsigned long
  276. copy_from_user(void *to, const void *from, unsigned long len)
  277. {
  278. unsigned long over;
  279. if (access_ok(VERIFY_READ, from, len))
  280. return __copy_tofrom_user(to, from, len);
  281. if ((unsigned long)from < TASK_SIZE) {
  282. over = (unsigned long)from + len - TASK_SIZE;
  283. return __copy_tofrom_user(to, from, len - over) + over;
  284. }
  285. return len;
  286. }
  287. static inline unsigned long
  288. copy_to_user(void *to, const void *from, unsigned long len)
  289. {
  290. unsigned long over;
  291. if (access_ok(VERIFY_WRITE, to, len))
  292. return __copy_tofrom_user(to, from, len);
  293. if ((unsigned long)to < TASK_SIZE) {
  294. over = (unsigned long)to + len - TASK_SIZE;
  295. return __copy_tofrom_user(to, from, len - over) + over;
  296. }
  297. return len;
  298. }
  299. #define __copy_from_user(to, from, len) \
  300. __copy_tofrom_user((to), (from), (len))
  301. #define __copy_to_user(to, from, len) \
  302. __copy_tofrom_user((to), (from), (len))
  303. static inline unsigned long
  304. __copy_to_user_inatomic(void *to, const void *from, unsigned long len)
  305. {
  306. return __copy_to_user(to, from, len);
  307. }
  308. static inline unsigned long
  309. __copy_from_user_inatomic(void *to, const void *from, unsigned long len)
  310. {
  311. return __copy_from_user(to, from, len);
  312. }
  313. #define __copy_in_user(to, from, len) __copy_from_user(to, from, len)
  314. static inline unsigned long
  315. copy_in_user(void *to, const void *from, unsigned long len)
  316. {
  317. if (access_ok(VERIFY_READ, from, len) &&
  318. access_ok(VERFITY_WRITE, to, len))
  319. return copy_from_user(to, from, len);
  320. }
  321. /*
  322. * __clear_user: - Zero a block of memory in user space, with less checking.
  323. * @to: Destination address, in user space.
  324. * @n: Number of bytes to zero.
  325. *
  326. * Zero a block of memory in user space. Caller must check
  327. * the specified block with access_ok() before calling this function.
  328. *
  329. * Returns number of bytes that could not be cleared.
  330. * On success, this will be zero.
  331. */
  332. extern unsigned long __clear_user(void __user *src, unsigned long size);
  333. static inline unsigned long clear_user(char *src, unsigned long size)
  334. {
  335. if (access_ok(VERIFY_WRITE, src, size))
  336. return __clear_user(src, size);
  337. return -EFAULT;
  338. }
  339. /*
  340. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  341. * @dst: Destination address, in kernel space. This buffer must be at
  342. * least @count bytes long.
  343. * @src: Source address, in user space.
  344. * @count: Maximum number of bytes to copy, including the trailing NUL.
  345. *
  346. * Copies a NUL-terminated string from userspace to kernel space.
  347. * Caller must check the specified block with access_ok() before calling
  348. * this function.
  349. *
  350. * On success, returns the length of the string (not including the trailing
  351. * NUL).
  352. *
  353. * If access to userspace fails, returns -EFAULT (some data may have been
  354. * copied).
  355. *
  356. * If @count is smaller than the length of the string, copies @count bytes
  357. * and returns @count.
  358. */
  359. extern int __strncpy_from_user(char *dst, const char *src, long len);
  360. static inline int strncpy_from_user(char *dst, const char *src, long len)
  361. {
  362. if (access_ok(VERIFY_READ, src, 1))
  363. return __strncpy_from_user(dst, src, len);
  364. return -EFAULT;
  365. }
  366. extern int __strlen_user(const char *src);
  367. static inline long strlen_user(const char __user *src)
  368. {
  369. return __strlen_user(src);
  370. }
  371. extern int __strnlen_user(const char *str, long len);
  372. static inline long strnlen_user(const char __user *str, long len)
  373. {
  374. if (!access_ok(VERIFY_READ, str, 0))
  375. return 0;
  376. else
  377. return __strnlen_user(str, len);
  378. }
  379. struct exception_table_entry {
  380. unsigned long insn;
  381. unsigned long fixup;
  382. };
  383. extern int fixup_exception(struct pt_regs *regs);
  384. #endif /* __SCORE_UACCESS_H */