uaccess_32.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. #define VERIFY_READ 0
  13. #define VERIFY_WRITE 1
  14. /*
  15. * The fs value determines whether argument validity checking should be
  16. * performed or not. If get_fs() == USER_DS, checking is performed, with
  17. * get_fs() == KERNEL_DS, checking is bypassed.
  18. *
  19. * For historical reasons, these macros are grossly misnamed.
  20. */
  21. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  22. #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL)
  23. #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
  24. #define get_ds() (KERNEL_DS)
  25. #define get_fs() (current_thread_info()->addr_limit)
  26. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  27. #define segment_eq(a, b) ((a).seg == (b).seg)
  28. /*
  29. * movsl can be slow when source and dest are not both 8-byte aligned
  30. */
  31. #ifdef CONFIG_X86_INTEL_USERCOPY
  32. extern struct movsl_mask {
  33. int mask;
  34. } ____cacheline_aligned_in_smp movsl_mask;
  35. #endif
  36. #define __addr_ok(addr) \
  37. ((unsigned long __force)(addr) < \
  38. (current_thread_info()->addr_limit.seg))
  39. /*
  40. * Test whether a block of memory is a valid user space address.
  41. * Returns 0 if the range is valid, nonzero otherwise.
  42. *
  43. * This is equivalent to the following test:
  44. * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
  45. *
  46. * This needs 33-bit arithmetic. We have a carry...
  47. */
  48. #define __range_not_ok(addr, size) \
  49. ({ \
  50. unsigned long flag, roksum; \
  51. __chk_user_ptr(addr); \
  52. asm("add %3,%1 ; sbb %0,%0; cmp %1,%4; sbb $0,%0" \
  53. :"=&r" (flag), "=r" (roksum) \
  54. :"1" (addr), "g" ((int)(size)), \
  55. "rm" (current_thread_info()->addr_limit.seg)); \
  56. flag; \
  57. })
  58. /**
  59. * access_ok: - Checks if a user space pointer is valid
  60. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  61. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  62. * to write to a block, it is always safe to read from it.
  63. * @addr: User space pointer to start of block to check
  64. * @size: Size of block to check
  65. *
  66. * Context: User context only. This function may sleep.
  67. *
  68. * Checks if a pointer to a block of memory in user space is valid.
  69. *
  70. * Returns true (nonzero) if the memory block may be valid, false (zero)
  71. * if it is definitely invalid.
  72. *
  73. * Note that, depending on architecture, this function probably just
  74. * checks that the pointer is in the user space range - after calling
  75. * this function, memory access functions may still return -EFAULT.
  76. */
  77. #define access_ok(type, addr, size) (likely(__range_not_ok(addr, size) == 0))
  78. /*
  79. * The exception table consists of pairs of addresses: the first is the
  80. * address of an instruction that is allowed to fault, and the second is
  81. * the address at which the program should continue. No registers are
  82. * modified, so it is entirely up to the continuation code to figure out
  83. * what to do.
  84. *
  85. * All the routines below use bits of fixup code that are out of line
  86. * with the main instruction path. This means when everything is well,
  87. * we don't even have to jump over them. Further, they do not intrude
  88. * on our cache or tlb entries.
  89. */
  90. struct exception_table_entry {
  91. unsigned long insn, fixup;
  92. };
  93. extern int fixup_exception(struct pt_regs *regs);
  94. /*
  95. * These are the main single-value transfer routines. They automatically
  96. * use the right size if we just have the right pointer type.
  97. *
  98. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  99. * and yet we don't want to do any pointers, because that is too much
  100. * of a performance impact. Thus we have a few rather ugly macros here,
  101. * and hide all the ugliness from the user.
  102. *
  103. * The "__xxx" versions of the user access functions are versions that
  104. * do not verify the address space, that must have been done previously
  105. * with a separate "access_ok()" call (this is used when we do multiple
  106. * accesses to the same area of user memory).
  107. */
  108. extern void __get_user_1(void);
  109. extern void __get_user_2(void);
  110. extern void __get_user_4(void);
  111. #define __get_user_x(size, ret, x, ptr) \
  112. asm volatile("call __get_user_" #size \
  113. :"=a" (ret),"=d" (x) \
  114. :"0" (ptr))
  115. /* Careful: we have to cast the result to the type of the pointer
  116. * for sign reasons */
  117. /**
  118. * get_user: - Get a simple variable from user space.
  119. * @x: Variable to store result.
  120. * @ptr: Source address, in user space.
  121. *
  122. * Context: User context only. This function may sleep.
  123. *
  124. * This macro copies a single simple variable from user space to kernel
  125. * space. It supports simple types like char and int, but not larger
  126. * data types like structures or arrays.
  127. *
  128. * @ptr must have pointer-to-simple-variable type, and the result of
  129. * dereferencing @ptr must be assignable to @x without a cast.
  130. *
  131. * Returns zero on success, or -EFAULT on error.
  132. * On error, the variable @x is set to zero.
  133. */
  134. #define get_user(x, ptr) \
  135. ({ \
  136. int __ret_gu; \
  137. unsigned long __val_gu; \
  138. __chk_user_ptr(ptr); \
  139. switch (sizeof(*(ptr))) { \
  140. case 1: \
  141. __get_user_x(1, __ret_gu, __val_gu, ptr); \
  142. break; \
  143. case 2: \
  144. __get_user_x(2, __ret_gu, __val_gu, ptr); \
  145. break; \
  146. case 4: \
  147. __get_user_x(4, __ret_gu, __val_gu, ptr); \
  148. break; \
  149. default: \
  150. __get_user_x(X, __ret_gu, __val_gu, ptr); \
  151. break; \
  152. } \
  153. (x) = (__typeof__(*(ptr)))__val_gu; \
  154. __ret_gu; \
  155. })
  156. extern void __put_user_bad(void);
  157. /*
  158. * Strange magic calling convention: pointer in %ecx,
  159. * value in %eax(:%edx), return value in %eax, no clobbers.
  160. */
  161. extern void __put_user_1(void);
  162. extern void __put_user_2(void);
  163. extern void __put_user_4(void);
  164. extern void __put_user_8(void);
  165. #define __put_user_x(size, x, ptr) \
  166. asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
  167. :"0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  168. #define __put_user_8(x, ptr) \
  169. asm volatile("call __put_user_8" : "=a" (__ret_pu) \
  170. : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  171. /**
  172. * put_user: - Write a simple value into user space.
  173. * @x: Value to copy to user space.
  174. * @ptr: Destination address, in user space.
  175. *
  176. * Context: User context only. This function may sleep.
  177. *
  178. * This macro copies a single simple value from kernel space to user
  179. * space. It supports simple types like char and int, but not larger
  180. * data types like structures or arrays.
  181. *
  182. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  183. * to the result of dereferencing @ptr.
  184. *
  185. * Returns zero on success, or -EFAULT on error.
  186. */
  187. #ifdef CONFIG_X86_WP_WORKS_OK
  188. #define put_user(x, ptr) \
  189. ({ \
  190. int __ret_pu; \
  191. __typeof__(*(ptr)) __pu_val; \
  192. __chk_user_ptr(ptr); \
  193. __pu_val = x; \
  194. switch (sizeof(*(ptr))) { \
  195. case 1: \
  196. __put_user_x(1, __pu_val, ptr); \
  197. break; \
  198. case 2: \
  199. __put_user_x(2, __pu_val, ptr); \
  200. break; \
  201. case 4: \
  202. __put_user_x(4, __pu_val, ptr); \
  203. break; \
  204. case 8: \
  205. __put_user_8(__pu_val, ptr); \
  206. break; \
  207. default: \
  208. __put_user_x(X, __pu_val, ptr); \
  209. break; \
  210. } \
  211. __ret_pu; \
  212. })
  213. #else
  214. #define put_user(x, ptr) \
  215. ({ \
  216. int __ret_pu; \
  217. __typeof__(*(ptr))__pus_tmp = x; \
  218. __ret_pu = 0; \
  219. if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
  220. sizeof(*(ptr))) != 0)) \
  221. __ret_pu = -EFAULT; \
  222. __ret_pu; \
  223. })
  224. #endif
  225. /**
  226. * __get_user: - Get a simple variable from user space, with less checking.
  227. * @x: Variable to store result.
  228. * @ptr: Source address, in user space.
  229. *
  230. * Context: User context only. This function may sleep.
  231. *
  232. * This macro copies a single simple variable from user space to kernel
  233. * space. It supports simple types like char and int, but not larger
  234. * data types like structures or arrays.
  235. *
  236. * @ptr must have pointer-to-simple-variable type, and the result of
  237. * dereferencing @ptr must be assignable to @x without a cast.
  238. *
  239. * Caller must check the pointer with access_ok() before calling this
  240. * function.
  241. *
  242. * Returns zero on success, or -EFAULT on error.
  243. * On error, the variable @x is set to zero.
  244. */
  245. #define __get_user(x, ptr) \
  246. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  247. /**
  248. * __put_user: - Write a simple value into user space, with less checking.
  249. * @x: Value to copy to user space.
  250. * @ptr: Destination address, in user space.
  251. *
  252. * Context: User context only. This function may sleep.
  253. *
  254. * This macro copies a single simple value from kernel space to user
  255. * space. It supports simple types like char and int, but not larger
  256. * data types like structures or arrays.
  257. *
  258. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  259. * to the result of dereferencing @ptr.
  260. *
  261. * Caller must check the pointer with access_ok() before calling this
  262. * function.
  263. *
  264. * Returns zero on success, or -EFAULT on error.
  265. */
  266. #define __put_user(x, ptr) \
  267. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  268. #define __put_user_nocheck(x, ptr, size) \
  269. ({ \
  270. long __pu_err; \
  271. __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
  272. __pu_err; \
  273. })
  274. #define __put_user_u64(x, addr, err) \
  275. asm volatile("1: movl %%eax,0(%2)\n" \
  276. "2: movl %%edx,4(%2)\n" \
  277. "3:\n" \
  278. ".section .fixup,\"ax\"\n" \
  279. "4: movl %3,%0\n" \
  280. " jmp 3b\n" \
  281. ".previous\n" \
  282. _ASM_EXTABLE(1b, 4b) \
  283. _ASM_EXTABLE(2b, 4b) \
  284. : "=r" (err) \
  285. : "A" (x), "r" (addr), "i" (-EFAULT), "0" (err))
  286. #ifdef CONFIG_X86_WP_WORKS_OK
  287. #define __put_user_size(x, ptr, size, retval, errret) \
  288. do { \
  289. retval = 0; \
  290. __chk_user_ptr(ptr); \
  291. switch (size) { \
  292. case 1: \
  293. __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
  294. break; \
  295. case 2: \
  296. __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
  297. break; \
  298. case 4: \
  299. __put_user_asm(x, ptr, retval, "l", "", "ir", errret); \
  300. break; \
  301. case 8: \
  302. __put_user_u64((__typeof__(*ptr))(x), ptr, retval); \
  303. break; \
  304. default: \
  305. __put_user_bad(); \
  306. } \
  307. } while (0)
  308. #else
  309. #define __put_user_size(x, ptr, size, retval, errret) \
  310. do { \
  311. __typeof__(*(ptr))__pus_tmp = x; \
  312. retval = 0; \
  313. \
  314. if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
  315. retval = errret; \
  316. } while (0)
  317. #endif
  318. struct __large_struct { unsigned long buf[100]; };
  319. #define __m(x) (*(struct __large_struct __user *)(x))
  320. /*
  321. * Tell gcc we read from memory instead of writing: this is because
  322. * we do not write to any memory gcc knows about, so there are no
  323. * aliasing issues.
  324. */
  325. #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  326. asm volatile("1: mov"itype" %"rtype"1,%2\n" \
  327. "2:\n" \
  328. ".section .fixup,\"ax\"\n" \
  329. "3: movl %3,%0\n" \
  330. " jmp 2b\n" \
  331. ".previous\n" \
  332. _ASM_EXTABLE(1b, 3b) \
  333. : "=r"(err) \
  334. : ltype (x), "m" (__m(addr)), "i" (errret), "0" (err))
  335. #define __get_user_nocheck(x, ptr, size) \
  336. ({ \
  337. long __gu_err; \
  338. unsigned long __gu_val; \
  339. __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
  340. (x) = (__typeof__(*(ptr)))__gu_val; \
  341. __gu_err; \
  342. })
  343. extern long __get_user_bad(void);
  344. #define __get_user_size(x, ptr, size, retval, errret) \
  345. do { \
  346. retval = 0; \
  347. __chk_user_ptr(ptr); \
  348. switch (size) { \
  349. case 1: \
  350. __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
  351. break; \
  352. case 2: \
  353. __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
  354. break; \
  355. case 4: \
  356. __get_user_asm(x, ptr, retval, "l", "", "=r", errret); \
  357. break; \
  358. default: \
  359. (x) = __get_user_bad(); \
  360. } \
  361. } while (0)
  362. #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  363. asm volatile("1: mov"itype" %2,%"rtype"1\n" \
  364. "2:\n" \
  365. ".section .fixup,\"ax\"\n" \
  366. "3: movl %3,%0\n" \
  367. " xor"itype" %"rtype"1,%"rtype"1\n" \
  368. " jmp 2b\n" \
  369. ".previous\n" \
  370. _ASM_EXTABLE(1b, 3b) \
  371. : "=r" (err), ltype (x) \
  372. : "m" (__m(addr)), "i" (errret), "0" (err))
  373. unsigned long __must_check __copy_to_user_ll
  374. (void __user *to, const void *from, unsigned long n);
  375. unsigned long __must_check __copy_from_user_ll
  376. (void *to, const void __user *from, unsigned long n);
  377. unsigned long __must_check __copy_from_user_ll_nozero
  378. (void *to, const void __user *from, unsigned long n);
  379. unsigned long __must_check __copy_from_user_ll_nocache
  380. (void *to, const void __user *from, unsigned long n);
  381. unsigned long __must_check __copy_from_user_ll_nocache_nozero
  382. (void *to, const void __user *from, unsigned long n);
  383. /**
  384. * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
  385. * @to: Destination address, in user space.
  386. * @from: Source address, in kernel space.
  387. * @n: Number of bytes to copy.
  388. *
  389. * Context: User context only.
  390. *
  391. * Copy data from kernel space to user space. Caller must check
  392. * the specified block with access_ok() before calling this function.
  393. * The caller should also make sure he pins the user space address
  394. * so that the we don't result in page fault and sleep.
  395. *
  396. * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
  397. * we return the initial request size (1, 2 or 4), as copy_*_user should do.
  398. * If a store crosses a page boundary and gets a fault, the x86 will not write
  399. * anything, so this is accurate.
  400. */
  401. static __always_inline unsigned long __must_check
  402. __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
  403. {
  404. if (__builtin_constant_p(n)) {
  405. unsigned long ret;
  406. switch (n) {
  407. case 1:
  408. __put_user_size(*(u8 *)from, (u8 __user *)to,
  409. 1, ret, 1);
  410. return ret;
  411. case 2:
  412. __put_user_size(*(u16 *)from, (u16 __user *)to,
  413. 2, ret, 2);
  414. return ret;
  415. case 4:
  416. __put_user_size(*(u32 *)from, (u32 __user *)to,
  417. 4, ret, 4);
  418. return ret;
  419. }
  420. }
  421. return __copy_to_user_ll(to, from, n);
  422. }
  423. /**
  424. * __copy_to_user: - Copy a block of data into user space, with less checking.
  425. * @to: Destination address, in user space.
  426. * @from: Source address, in kernel space.
  427. * @n: Number of bytes to copy.
  428. *
  429. * Context: User context only. This function may sleep.
  430. *
  431. * Copy data from kernel space to user space. Caller must check
  432. * the specified block with access_ok() before calling this function.
  433. *
  434. * Returns number of bytes that could not be copied.
  435. * On success, this will be zero.
  436. */
  437. static __always_inline unsigned long __must_check
  438. __copy_to_user(void __user *to, const void *from, unsigned long n)
  439. {
  440. might_sleep();
  441. return __copy_to_user_inatomic(to, from, n);
  442. }
  443. static __always_inline unsigned long
  444. __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
  445. {
  446. /* Avoid zeroing the tail if the copy fails..
  447. * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
  448. * but as the zeroing behaviour is only significant when n is not
  449. * constant, that shouldn't be a problem.
  450. */
  451. if (__builtin_constant_p(n)) {
  452. unsigned long ret;
  453. switch (n) {
  454. case 1:
  455. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  456. return ret;
  457. case 2:
  458. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  459. return ret;
  460. case 4:
  461. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  462. return ret;
  463. }
  464. }
  465. return __copy_from_user_ll_nozero(to, from, n);
  466. }
  467. /**
  468. * __copy_from_user: - Copy a block of data from user space, with less checking.
  469. * @to: Destination address, in kernel space.
  470. * @from: Source address, in user space.
  471. * @n: Number of bytes to copy.
  472. *
  473. * Context: User context only. This function may sleep.
  474. *
  475. * Copy data from user space to kernel space. Caller must check
  476. * the specified block with access_ok() before calling this function.
  477. *
  478. * Returns number of bytes that could not be copied.
  479. * On success, this will be zero.
  480. *
  481. * If some data could not be copied, this function will pad the copied
  482. * data to the requested size using zero bytes.
  483. *
  484. * An alternate version - __copy_from_user_inatomic() - may be called from
  485. * atomic context and will fail rather than sleep. In this case the
  486. * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
  487. * for explanation of why this is needed.
  488. */
  489. static __always_inline unsigned long
  490. __copy_from_user(void *to, const void __user *from, unsigned long n)
  491. {
  492. might_sleep();
  493. if (__builtin_constant_p(n)) {
  494. unsigned long ret;
  495. switch (n) {
  496. case 1:
  497. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  498. return ret;
  499. case 2:
  500. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  501. return ret;
  502. case 4:
  503. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  504. return ret;
  505. }
  506. }
  507. return __copy_from_user_ll(to, from, n);
  508. }
  509. #define ARCH_HAS_NOCACHE_UACCESS
  510. static __always_inline unsigned long __copy_from_user_nocache(void *to,
  511. const void __user *from, unsigned long n)
  512. {
  513. might_sleep();
  514. if (__builtin_constant_p(n)) {
  515. unsigned long ret;
  516. switch (n) {
  517. case 1:
  518. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  519. return ret;
  520. case 2:
  521. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  522. return ret;
  523. case 4:
  524. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  525. return ret;
  526. }
  527. }
  528. return __copy_from_user_ll_nocache(to, from, n);
  529. }
  530. static __always_inline unsigned long
  531. __copy_from_user_inatomic_nocache(void *to, const void __user *from,
  532. unsigned long n)
  533. {
  534. return __copy_from_user_ll_nocache_nozero(to, from, n);
  535. }
  536. unsigned long __must_check copy_to_user(void __user *to,
  537. const void *from, unsigned long n);
  538. unsigned long __must_check copy_from_user(void *to,
  539. const void __user *from,
  540. unsigned long n);
  541. long __must_check strncpy_from_user(char *dst, const char __user *src,
  542. long count);
  543. long __must_check __strncpy_from_user(char *dst,
  544. const char __user *src, long count);
  545. /**
  546. * strlen_user: - Get the size of a string in user space.
  547. * @str: The string to measure.
  548. *
  549. * Context: User context only. This function may sleep.
  550. *
  551. * Get the size of a NUL-terminated string in user space.
  552. *
  553. * Returns the size of the string INCLUDING the terminating NUL.
  554. * On exception, returns 0.
  555. *
  556. * If there is a limit on the length of a valid string, you may wish to
  557. * consider using strnlen_user() instead.
  558. */
  559. #define strlen_user(str) strnlen_user(str, LONG_MAX)
  560. long strnlen_user(const char __user *str, long n);
  561. unsigned long __must_check clear_user(void __user *mem, unsigned long len);
  562. unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
  563. #endif /* __i386_UACCESS_H */