uaccess.h 17 KB

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