uaccess_32.h 18 KB

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