uaccess.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. * 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. ".section __ex_table,\"a\"\n" \
  258. " .align 4\n" \
  259. " .long 1b,4b\n" \
  260. " .long 2b,4b\n" \
  261. ".previous" \
  262. : "=r"(err) \
  263. : "A" (x), "r" (addr), "i"(-EFAULT), "0"(err))
  264. #ifdef CONFIG_X86_WP_WORKS_OK
  265. #define __put_user_size(x,ptr,size,retval,errret) \
  266. do { \
  267. retval = 0; \
  268. __chk_user_ptr(ptr); \
  269. switch (size) { \
  270. case 1: __put_user_asm(x,ptr,retval,"b","b","iq",errret);break; \
  271. case 2: __put_user_asm(x,ptr,retval,"w","w","ir",errret);break; \
  272. case 4: __put_user_asm(x,ptr,retval,"l","","ir",errret); break; \
  273. case 8: __put_user_u64((__typeof__(*ptr))(x),ptr,retval); break;\
  274. default: __put_user_bad(); \
  275. } \
  276. } while (0)
  277. #else
  278. #define __put_user_size(x,ptr,size,retval,errret) \
  279. do { \
  280. __typeof__(*(ptr)) __pus_tmp = x; \
  281. retval = 0; \
  282. \
  283. if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
  284. retval = errret; \
  285. } while (0)
  286. #endif
  287. struct __large_struct { unsigned long buf[100]; };
  288. #define __m(x) (*(struct __large_struct __user *)(x))
  289. /*
  290. * Tell gcc we read from memory instead of writing: this is because
  291. * we do not write to any memory gcc knows about, so there are no
  292. * aliasing issues.
  293. */
  294. #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  295. __asm__ __volatile__( \
  296. "1: mov"itype" %"rtype"1,%2\n" \
  297. "2:\n" \
  298. ".section .fixup,\"ax\"\n" \
  299. "3: movl %3,%0\n" \
  300. " jmp 2b\n" \
  301. ".previous\n" \
  302. ".section __ex_table,\"a\"\n" \
  303. " .align 4\n" \
  304. " .long 1b,3b\n" \
  305. ".previous" \
  306. : "=r"(err) \
  307. : ltype (x), "m"(__m(addr)), "i"(errret), "0"(err))
  308. #define __get_user_nocheck(x,ptr,size) \
  309. ({ \
  310. long __gu_err; \
  311. unsigned long __gu_val; \
  312. __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
  313. (x) = (__typeof__(*(ptr)))__gu_val; \
  314. __gu_err; \
  315. })
  316. extern long __get_user_bad(void);
  317. #define __get_user_size(x,ptr,size,retval,errret) \
  318. do { \
  319. retval = 0; \
  320. __chk_user_ptr(ptr); \
  321. switch (size) { \
  322. case 1: __get_user_asm(x,ptr,retval,"b","b","=q",errret);break; \
  323. case 2: __get_user_asm(x,ptr,retval,"w","w","=r",errret);break; \
  324. case 4: __get_user_asm(x,ptr,retval,"l","","=r",errret);break; \
  325. default: (x) = __get_user_bad(); \
  326. } \
  327. } while (0)
  328. #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  329. __asm__ __volatile__( \
  330. "1: mov"itype" %2,%"rtype"1\n" \
  331. "2:\n" \
  332. ".section .fixup,\"ax\"\n" \
  333. "3: movl %3,%0\n" \
  334. " xor"itype" %"rtype"1,%"rtype"1\n" \
  335. " jmp 2b\n" \
  336. ".previous\n" \
  337. ".section __ex_table,\"a\"\n" \
  338. " .align 4\n" \
  339. " .long 1b,3b\n" \
  340. ".previous" \
  341. : "=r"(err), ltype (x) \
  342. : "m"(__m(addr)), "i"(errret), "0"(err))
  343. unsigned long __must_check __copy_to_user_ll(void __user *to,
  344. const void *from, unsigned long n);
  345. unsigned long __must_check __copy_from_user_ll(void *to,
  346. const void __user *from, unsigned long n);
  347. /*
  348. * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
  349. * we return the initial request size (1, 2 or 4), as copy_*_user should do.
  350. * If a store crosses a page boundary and gets a fault, the x86 will not write
  351. * anything, so this is accurate.
  352. */
  353. /**
  354. * __copy_to_user: - Copy a block of data into user space, with less checking.
  355. * @to: Destination address, in user space.
  356. * @from: Source address, in kernel space.
  357. * @n: Number of bytes to copy.
  358. *
  359. * Context: User context only. This function may sleep.
  360. *
  361. * Copy data from kernel space to user space. Caller must check
  362. * the specified block with access_ok() before calling this function.
  363. *
  364. * Returns number of bytes that could not be copied.
  365. * On success, this will be zero.
  366. */
  367. static __always_inline unsigned long __must_check
  368. __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
  369. {
  370. if (__builtin_constant_p(n)) {
  371. unsigned long ret;
  372. switch (n) {
  373. case 1:
  374. __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
  375. return ret;
  376. case 2:
  377. __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
  378. return ret;
  379. case 4:
  380. __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
  381. return ret;
  382. }
  383. }
  384. return __copy_to_user_ll(to, from, n);
  385. }
  386. static __always_inline unsigned long __must_check
  387. __copy_to_user(void __user *to, const void *from, unsigned long n)
  388. {
  389. might_sleep();
  390. return __copy_to_user_inatomic(to, from, n);
  391. }
  392. /**
  393. * __copy_from_user: - Copy a block of data from user space, with less checking.
  394. * @to: Destination address, in kernel space.
  395. * @from: Source address, in user space.
  396. * @n: Number of bytes to copy.
  397. *
  398. * Context: User context only. This function may sleep.
  399. *
  400. * Copy data from user space to kernel space. Caller must check
  401. * the specified block with access_ok() before calling this function.
  402. *
  403. * Returns number of bytes that could not be copied.
  404. * On success, this will be zero.
  405. *
  406. * If some data could not be copied, this function will pad the copied
  407. * data to the requested size using zero bytes.
  408. */
  409. static __always_inline unsigned long
  410. __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
  411. {
  412. if (__builtin_constant_p(n)) {
  413. unsigned long ret;
  414. switch (n) {
  415. case 1:
  416. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  417. return ret;
  418. case 2:
  419. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  420. return ret;
  421. case 4:
  422. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  423. return ret;
  424. }
  425. }
  426. return __copy_from_user_ll(to, from, n);
  427. }
  428. static __always_inline unsigned long
  429. __copy_from_user(void *to, const void __user *from, unsigned long n)
  430. {
  431. might_sleep();
  432. return __copy_from_user_inatomic(to, from, n);
  433. }
  434. unsigned long __must_check copy_to_user(void __user *to,
  435. const void *from, unsigned long n);
  436. unsigned long __must_check copy_from_user(void *to,
  437. const void __user *from, unsigned long n);
  438. long __must_check strncpy_from_user(char *dst, const char __user *src,
  439. long count);
  440. long __must_check __strncpy_from_user(char *dst,
  441. const char __user *src, long count);
  442. /**
  443. * strlen_user: - Get the size of a string in user space.
  444. * @str: The string to measure.
  445. *
  446. * Context: User context only. This function may sleep.
  447. *
  448. * Get the size of a NUL-terminated string in user space.
  449. *
  450. * Returns the size of the string INCLUDING the terminating NUL.
  451. * On exception, returns 0.
  452. *
  453. * If there is a limit on the length of a valid string, you may wish to
  454. * consider using strnlen_user() instead.
  455. */
  456. #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
  457. long strnlen_user(const char __user *str, long n);
  458. unsigned long __must_check clear_user(void __user *mem, unsigned long len);
  459. unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
  460. #endif /* __i386_UACCESS_H */