uaccess_32.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #ifndef __i386_UACCESS_H
  2. #define __i386_UACCESS_H
  3. /*
  4. * User space memory access functions
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/thread_info.h>
  8. #include <linux/prefetch.h>
  9. #include <linux/string.h>
  10. #include <asm/asm.h>
  11. #include <asm/page.h>
  12. /*
  13. * movsl can be slow when source and dest are not both 8-byte aligned
  14. */
  15. #ifdef CONFIG_X86_INTEL_USERCOPY
  16. extern struct movsl_mask {
  17. int mask;
  18. } ____cacheline_aligned_in_smp movsl_mask;
  19. #endif
  20. extern void __put_user_bad(void);
  21. /*
  22. * Strange magic calling convention: pointer in %ecx,
  23. * value in %eax(:%edx), return value in %eax, no clobbers.
  24. */
  25. extern void __put_user_1(void);
  26. extern void __put_user_2(void);
  27. extern void __put_user_4(void);
  28. extern void __put_user_8(void);
  29. #define __put_user_x(size, x, ptr) \
  30. asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
  31. :"0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  32. #define __put_user_8(x, ptr) \
  33. asm volatile("call __put_user_8" : "=a" (__ret_pu) \
  34. : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  35. /**
  36. * put_user: - Write a simple value into user space.
  37. * @x: Value to copy to user space.
  38. * @ptr: Destination address, in user space.
  39. *
  40. * Context: User context only. This function may sleep.
  41. *
  42. * This macro copies a single simple value from kernel space to user
  43. * space. It supports simple types like char and int, but not larger
  44. * data types like structures or arrays.
  45. *
  46. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  47. * to the result of dereferencing @ptr.
  48. *
  49. * Returns zero on success, or -EFAULT on error.
  50. */
  51. #ifdef CONFIG_X86_WP_WORKS_OK
  52. #define put_user(x, ptr) \
  53. ({ \
  54. int __ret_pu; \
  55. __typeof__(*(ptr)) __pu_val; \
  56. __chk_user_ptr(ptr); \
  57. __pu_val = x; \
  58. switch (sizeof(*(ptr))) { \
  59. case 1: \
  60. __put_user_x(1, __pu_val, ptr); \
  61. break; \
  62. case 2: \
  63. __put_user_x(2, __pu_val, ptr); \
  64. break; \
  65. case 4: \
  66. __put_user_x(4, __pu_val, ptr); \
  67. break; \
  68. case 8: \
  69. __put_user_8(__pu_val, ptr); \
  70. break; \
  71. default: \
  72. __put_user_x(X, __pu_val, ptr); \
  73. break; \
  74. } \
  75. __ret_pu; \
  76. })
  77. #else
  78. #define put_user(x, ptr) \
  79. ({ \
  80. int __ret_pu; \
  81. __typeof__(*(ptr))__pus_tmp = x; \
  82. __ret_pu = 0; \
  83. if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
  84. sizeof(*(ptr))) != 0)) \
  85. __ret_pu = -EFAULT; \
  86. __ret_pu; \
  87. })
  88. #endif
  89. /**
  90. * __get_user: - Get a simple variable from user space, with less checking.
  91. * @x: Variable to store result.
  92. * @ptr: Source address, in user space.
  93. *
  94. * Context: User context only. This function may sleep.
  95. *
  96. * This macro copies a single simple variable from user space to kernel
  97. * space. It supports simple types like char and int, but not larger
  98. * data types like structures or arrays.
  99. *
  100. * @ptr must have pointer-to-simple-variable type, and the result of
  101. * dereferencing @ptr must be assignable to @x without a cast.
  102. *
  103. * Caller must check the pointer with access_ok() before calling this
  104. * function.
  105. *
  106. * Returns zero on success, or -EFAULT on error.
  107. * On error, the variable @x is set to zero.
  108. */
  109. #define __get_user(x, ptr) \
  110. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  111. /**
  112. * __put_user: - Write a simple value into user space, with less checking.
  113. * @x: Value to copy to user space.
  114. * @ptr: Destination address, in user space.
  115. *
  116. * Context: User context only. This function may sleep.
  117. *
  118. * This macro copies a single simple value from kernel space to user
  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 @x must be assignable
  123. * to the result of dereferencing @ptr.
  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. */
  130. #define __put_user(x, ptr) \
  131. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  132. #define __put_user_nocheck(x, ptr, size) \
  133. ({ \
  134. long __pu_err; \
  135. __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
  136. __pu_err; \
  137. })
  138. #define __put_user_u64(x, addr, err) \
  139. asm volatile("1: movl %%eax,0(%2)\n" \
  140. "2: movl %%edx,4(%2)\n" \
  141. "3:\n" \
  142. ".section .fixup,\"ax\"\n" \
  143. "4: movl %3,%0\n" \
  144. " jmp 3b\n" \
  145. ".previous\n" \
  146. _ASM_EXTABLE(1b, 4b) \
  147. _ASM_EXTABLE(2b, 4b) \
  148. : "=r" (err) \
  149. : "A" (x), "r" (addr), "i" (-EFAULT), "0" (err))
  150. #ifdef CONFIG_X86_WP_WORKS_OK
  151. #define __put_user_size(x, ptr, size, retval, errret) \
  152. do { \
  153. retval = 0; \
  154. __chk_user_ptr(ptr); \
  155. switch (size) { \
  156. case 1: \
  157. __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
  158. break; \
  159. case 2: \
  160. __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
  161. break; \
  162. case 4: \
  163. __put_user_asm(x, ptr, retval, "l", "", "ir", errret); \
  164. break; \
  165. case 8: \
  166. __put_user_u64((__typeof__(*ptr))(x), ptr, retval); \
  167. break; \
  168. default: \
  169. __put_user_bad(); \
  170. } \
  171. } while (0)
  172. #else
  173. #define __put_user_size(x, ptr, size, retval, errret) \
  174. do { \
  175. __typeof__(*(ptr))__pus_tmp = x; \
  176. retval = 0; \
  177. \
  178. if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
  179. retval = errret; \
  180. } while (0)
  181. #endif
  182. struct __large_struct { unsigned long buf[100]; };
  183. #define __m(x) (*(struct __large_struct __user *)(x))
  184. /*
  185. * Tell gcc we read from memory instead of writing: this is because
  186. * we do not write to any memory gcc knows about, so there are no
  187. * aliasing issues.
  188. */
  189. #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  190. asm volatile("1: mov"itype" %"rtype"1,%2\n" \
  191. "2:\n" \
  192. ".section .fixup,\"ax\"\n" \
  193. "3: movl %3,%0\n" \
  194. " jmp 2b\n" \
  195. ".previous\n" \
  196. _ASM_EXTABLE(1b, 3b) \
  197. : "=r"(err) \
  198. : ltype (x), "m" (__m(addr)), "i" (errret), "0" (err))
  199. #define __get_user_nocheck(x, ptr, size) \
  200. ({ \
  201. long __gu_err; \
  202. unsigned long __gu_val; \
  203. __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
  204. (x) = (__typeof__(*(ptr)))__gu_val; \
  205. __gu_err; \
  206. })
  207. #define __get_user_size(x, ptr, size, retval, errret) \
  208. do { \
  209. retval = 0; \
  210. __chk_user_ptr(ptr); \
  211. switch (size) { \
  212. case 1: \
  213. __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
  214. break; \
  215. case 2: \
  216. __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
  217. break; \
  218. case 4: \
  219. __get_user_asm(x, ptr, retval, "l", "", "=r", errret); \
  220. break; \
  221. default: \
  222. (x) = __get_user_bad(); \
  223. } \
  224. } while (0)
  225. #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  226. asm volatile("1: mov"itype" %2,%"rtype"1\n" \
  227. "2:\n" \
  228. ".section .fixup,\"ax\"\n" \
  229. "3: movl %3,%0\n" \
  230. " xor"itype" %"rtype"1,%"rtype"1\n" \
  231. " jmp 2b\n" \
  232. ".previous\n" \
  233. _ASM_EXTABLE(1b, 3b) \
  234. : "=r" (err), ltype (x) \
  235. : "m" (__m(addr)), "i" (errret), "0" (err))
  236. unsigned long __must_check __copy_to_user_ll
  237. (void __user *to, const void *from, unsigned long n);
  238. unsigned long __must_check __copy_from_user_ll
  239. (void *to, const void __user *from, unsigned long n);
  240. unsigned long __must_check __copy_from_user_ll_nozero
  241. (void *to, const void __user *from, unsigned long n);
  242. unsigned long __must_check __copy_from_user_ll_nocache
  243. (void *to, const void __user *from, unsigned long n);
  244. unsigned long __must_check __copy_from_user_ll_nocache_nozero
  245. (void *to, const void __user *from, unsigned long n);
  246. /**
  247. * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
  248. * @to: Destination address, in user space.
  249. * @from: Source address, in kernel space.
  250. * @n: Number of bytes to copy.
  251. *
  252. * Context: User context only.
  253. *
  254. * Copy data from kernel space to user space. Caller must check
  255. * the specified block with access_ok() before calling this function.
  256. * The caller should also make sure he pins the user space address
  257. * so that the we don't result in page fault and sleep.
  258. *
  259. * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
  260. * we return the initial request size (1, 2 or 4), as copy_*_user should do.
  261. * If a store crosses a page boundary and gets a fault, the x86 will not write
  262. * anything, so this is accurate.
  263. */
  264. static __always_inline unsigned long __must_check
  265. __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
  266. {
  267. if (__builtin_constant_p(n)) {
  268. unsigned long ret;
  269. switch (n) {
  270. case 1:
  271. __put_user_size(*(u8 *)from, (u8 __user *)to,
  272. 1, ret, 1);
  273. return ret;
  274. case 2:
  275. __put_user_size(*(u16 *)from, (u16 __user *)to,
  276. 2, ret, 2);
  277. return ret;
  278. case 4:
  279. __put_user_size(*(u32 *)from, (u32 __user *)to,
  280. 4, ret, 4);
  281. return ret;
  282. }
  283. }
  284. return __copy_to_user_ll(to, from, n);
  285. }
  286. /**
  287. * __copy_to_user: - Copy a block of data into user space, with less checking.
  288. * @to: Destination address, in user space.
  289. * @from: Source address, in kernel space.
  290. * @n: Number of bytes to copy.
  291. *
  292. * Context: User context only. This function may sleep.
  293. *
  294. * Copy data from kernel space to user space. Caller must check
  295. * the specified block with access_ok() before calling this function.
  296. *
  297. * Returns number of bytes that could not be copied.
  298. * On success, this will be zero.
  299. */
  300. static __always_inline unsigned long __must_check
  301. __copy_to_user(void __user *to, const void *from, unsigned long n)
  302. {
  303. might_sleep();
  304. return __copy_to_user_inatomic(to, from, n);
  305. }
  306. static __always_inline unsigned long
  307. __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
  308. {
  309. /* Avoid zeroing the tail if the copy fails..
  310. * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
  311. * but as the zeroing behaviour is only significant when n is not
  312. * constant, that shouldn't be a problem.
  313. */
  314. if (__builtin_constant_p(n)) {
  315. unsigned long ret;
  316. switch (n) {
  317. case 1:
  318. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  319. return ret;
  320. case 2:
  321. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  322. return ret;
  323. case 4:
  324. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  325. return ret;
  326. }
  327. }
  328. return __copy_from_user_ll_nozero(to, from, n);
  329. }
  330. /**
  331. * __copy_from_user: - Copy a block of data from user space, with less checking.
  332. * @to: Destination address, in kernel space.
  333. * @from: Source address, in user space.
  334. * @n: Number of bytes to copy.
  335. *
  336. * Context: User context only. This function may sleep.
  337. *
  338. * Copy data from user space to kernel space. Caller must check
  339. * the specified block with access_ok() before calling this function.
  340. *
  341. * Returns number of bytes that could not be copied.
  342. * On success, this will be zero.
  343. *
  344. * If some data could not be copied, this function will pad the copied
  345. * data to the requested size using zero bytes.
  346. *
  347. * An alternate version - __copy_from_user_inatomic() - may be called from
  348. * atomic context and will fail rather than sleep. In this case the
  349. * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
  350. * for explanation of why this is needed.
  351. */
  352. static __always_inline unsigned long
  353. __copy_from_user(void *to, const void __user *from, unsigned long n)
  354. {
  355. might_sleep();
  356. if (__builtin_constant_p(n)) {
  357. unsigned long ret;
  358. switch (n) {
  359. case 1:
  360. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  361. return ret;
  362. case 2:
  363. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  364. return ret;
  365. case 4:
  366. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  367. return ret;
  368. }
  369. }
  370. return __copy_from_user_ll(to, from, n);
  371. }
  372. #define ARCH_HAS_NOCACHE_UACCESS
  373. static __always_inline unsigned long __copy_from_user_nocache(void *to,
  374. const void __user *from, unsigned long n)
  375. {
  376. might_sleep();
  377. if (__builtin_constant_p(n)) {
  378. unsigned long ret;
  379. switch (n) {
  380. case 1:
  381. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  382. return ret;
  383. case 2:
  384. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  385. return ret;
  386. case 4:
  387. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  388. return ret;
  389. }
  390. }
  391. return __copy_from_user_ll_nocache(to, from, n);
  392. }
  393. static __always_inline unsigned long
  394. __copy_from_user_inatomic_nocache(void *to, const void __user *from,
  395. unsigned long n)
  396. {
  397. return __copy_from_user_ll_nocache_nozero(to, from, n);
  398. }
  399. unsigned long __must_check copy_to_user(void __user *to,
  400. const void *from, unsigned long n);
  401. unsigned long __must_check copy_from_user(void *to,
  402. const void __user *from,
  403. unsigned long n);
  404. long __must_check strncpy_from_user(char *dst, const char __user *src,
  405. long count);
  406. long __must_check __strncpy_from_user(char *dst,
  407. const char __user *src, long count);
  408. /**
  409. * strlen_user: - Get the size of a string in user space.
  410. * @str: The string to measure.
  411. *
  412. * Context: User context only. This function may sleep.
  413. *
  414. * Get the size of a NUL-terminated string in user space.
  415. *
  416. * Returns the size of the string INCLUDING the terminating NUL.
  417. * On exception, returns 0.
  418. *
  419. * If there is a limit on the length of a valid string, you may wish to
  420. * consider using strnlen_user() instead.
  421. */
  422. #define strlen_user(str) strnlen_user(str, LONG_MAX)
  423. long strnlen_user(const char __user *str, long n);
  424. unsigned long __must_check clear_user(void __user *mem, unsigned long len);
  425. unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
  426. #endif /* __i386_UACCESS_H */