uaccess_32.h 18 KB

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