uaccess_32.h 15 KB

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