uaccess.h 12 KB

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