uaccess.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. __chk_user_ptr(ptr); \
  176. switch(sizeof(*(ptr))) { \
  177. case 1: __put_user_1(x, ptr); break; \
  178. case 2: __put_user_2(x, ptr); break; \
  179. case 4: __put_user_4(x, ptr); break; \
  180. case 8: __put_user_8(x, ptr); break; \
  181. default:__put_user_X(x, ptr); break; \
  182. } \
  183. __ret_pu; \
  184. })
  185. #else
  186. #define put_user(x,ptr) \
  187. ({ \
  188. int __ret_pu; \
  189. __typeof__(*(ptr)) __pus_tmp = x; \
  190. __ret_pu=0; \
  191. if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
  192. sizeof(*(ptr))) != 0)) \
  193. __ret_pu=-EFAULT; \
  194. __ret_pu; \
  195. })
  196. #endif
  197. /**
  198. * __get_user: - Get a simple variable from user space, with less checking.
  199. * @x: Variable to store result.
  200. * @ptr: Source address, in user space.
  201. *
  202. * Context: User context only. This function may sleep.
  203. *
  204. * This macro copies a single simple variable from user space to kernel
  205. * space. It supports simple types like char and int, but not larger
  206. * data types like structures or arrays.
  207. *
  208. * @ptr must have pointer-to-simple-variable type, and the result of
  209. * dereferencing @ptr must be assignable to @x without a cast.
  210. *
  211. * Caller must check the pointer with access_ok() before calling this
  212. * function.
  213. *
  214. * Returns zero on success, or -EFAULT on error.
  215. * On error, the variable @x is set to zero.
  216. */
  217. #define __get_user(x,ptr) \
  218. __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
  219. /**
  220. * __put_user: - Write a simple value into user space, with less checking.
  221. * @x: Value to copy to user space.
  222. * @ptr: Destination address, in user space.
  223. *
  224. * Context: User context only. This function may sleep.
  225. *
  226. * This macro copies a single simple value from kernel space to user
  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 @x must be assignable
  231. * to the result of dereferencing @ptr.
  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. */
  238. #define __put_user(x,ptr) \
  239. __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  240. #define __put_user_nocheck(x,ptr,size) \
  241. ({ \
  242. long __pu_err; \
  243. __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \
  244. __pu_err; \
  245. })
  246. #define __put_user_u64(x, addr, err) \
  247. __asm__ __volatile__( \
  248. "1: movl %%eax,0(%2)\n" \
  249. "2: movl %%edx,4(%2)\n" \
  250. "3:\n" \
  251. ".section .fixup,\"ax\"\n" \
  252. "4: movl %3,%0\n" \
  253. " jmp 3b\n" \
  254. ".previous\n" \
  255. ".section __ex_table,\"a\"\n" \
  256. " .align 4\n" \
  257. " .long 1b,4b\n" \
  258. " .long 2b,4b\n" \
  259. ".previous" \
  260. : "=r"(err) \
  261. : "A" (x), "r" (addr), "i"(-EFAULT), "0"(err))
  262. #ifdef CONFIG_X86_WP_WORKS_OK
  263. #define __put_user_size(x,ptr,size,retval,errret) \
  264. do { \
  265. retval = 0; \
  266. __chk_user_ptr(ptr); \
  267. switch (size) { \
  268. case 1: __put_user_asm(x,ptr,retval,"b","b","iq",errret);break; \
  269. case 2: __put_user_asm(x,ptr,retval,"w","w","ir",errret);break; \
  270. case 4: __put_user_asm(x,ptr,retval,"l","","ir",errret); break; \
  271. case 8: __put_user_u64((__typeof__(*ptr))(x),ptr,retval); break;\
  272. default: __put_user_bad(); \
  273. } \
  274. } while (0)
  275. #else
  276. #define __put_user_size(x,ptr,size,retval,errret) \
  277. do { \
  278. __typeof__(*(ptr)) __pus_tmp = x; \
  279. retval = 0; \
  280. \
  281. if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
  282. retval = errret; \
  283. } while (0)
  284. #endif
  285. struct __large_struct { unsigned long buf[100]; };
  286. #define __m(x) (*(struct __large_struct __user *)(x))
  287. /*
  288. * Tell gcc we read from memory instead of writing: this is because
  289. * we do not write to any memory gcc knows about, so there are no
  290. * aliasing issues.
  291. */
  292. #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  293. __asm__ __volatile__( \
  294. "1: mov"itype" %"rtype"1,%2\n" \
  295. "2:\n" \
  296. ".section .fixup,\"ax\"\n" \
  297. "3: movl %3,%0\n" \
  298. " jmp 2b\n" \
  299. ".previous\n" \
  300. ".section __ex_table,\"a\"\n" \
  301. " .align 4\n" \
  302. " .long 1b,3b\n" \
  303. ".previous" \
  304. : "=r"(err) \
  305. : ltype (x), "m"(__m(addr)), "i"(errret), "0"(err))
  306. #define __get_user_nocheck(x,ptr,size) \
  307. ({ \
  308. long __gu_err; \
  309. unsigned long __gu_val; \
  310. __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
  311. (x) = (__typeof__(*(ptr)))__gu_val; \
  312. __gu_err; \
  313. })
  314. extern long __get_user_bad(void);
  315. #define __get_user_size(x,ptr,size,retval,errret) \
  316. do { \
  317. retval = 0; \
  318. __chk_user_ptr(ptr); \
  319. switch (size) { \
  320. case 1: __get_user_asm(x,ptr,retval,"b","b","=q",errret);break; \
  321. case 2: __get_user_asm(x,ptr,retval,"w","w","=r",errret);break; \
  322. case 4: __get_user_asm(x,ptr,retval,"l","","=r",errret);break; \
  323. default: (x) = __get_user_bad(); \
  324. } \
  325. } while (0)
  326. #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  327. __asm__ __volatile__( \
  328. "1: mov"itype" %2,%"rtype"1\n" \
  329. "2:\n" \
  330. ".section .fixup,\"ax\"\n" \
  331. "3: movl %3,%0\n" \
  332. " xor"itype" %"rtype"1,%"rtype"1\n" \
  333. " jmp 2b\n" \
  334. ".previous\n" \
  335. ".section __ex_table,\"a\"\n" \
  336. " .align 4\n" \
  337. " .long 1b,3b\n" \
  338. ".previous" \
  339. : "=r"(err), ltype (x) \
  340. : "m"(__m(addr)), "i"(errret), "0"(err))
  341. unsigned long __must_check __copy_to_user_ll(void __user *to,
  342. const void *from, unsigned long n);
  343. unsigned long __must_check __copy_from_user_ll(void *to,
  344. const void __user *from, unsigned long n);
  345. /*
  346. * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
  347. * we return the initial request size (1, 2 or 4), as copy_*_user should do.
  348. * If a store crosses a page boundary and gets a fault, the x86 will not write
  349. * anything, so this is accurate.
  350. */
  351. /**
  352. * __copy_to_user: - Copy a block of data into user space, with less checking.
  353. * @to: Destination address, in user space.
  354. * @from: Source address, in kernel space.
  355. * @n: Number of bytes to copy.
  356. *
  357. * Context: User context only. This function may sleep.
  358. *
  359. * Copy data from kernel space to user space. Caller must check
  360. * the specified block with access_ok() before calling this function.
  361. *
  362. * Returns number of bytes that could not be copied.
  363. * On success, this will be zero.
  364. */
  365. static __always_inline unsigned long __must_check
  366. __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
  367. {
  368. if (__builtin_constant_p(n)) {
  369. unsigned long ret;
  370. switch (n) {
  371. case 1:
  372. __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
  373. return ret;
  374. case 2:
  375. __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
  376. return ret;
  377. case 4:
  378. __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
  379. return ret;
  380. }
  381. }
  382. return __copy_to_user_ll(to, from, n);
  383. }
  384. static __always_inline unsigned long __must_check
  385. __copy_to_user(void __user *to, const void *from, unsigned long n)
  386. {
  387. might_sleep();
  388. return __copy_to_user_inatomic(to, from, n);
  389. }
  390. /**
  391. * __copy_from_user: - Copy a block of data from user space, with less checking.
  392. * @to: Destination address, in kernel space.
  393. * @from: Source address, in user space.
  394. * @n: Number of bytes to copy.
  395. *
  396. * Context: User context only. This function may sleep.
  397. *
  398. * Copy data from user space to kernel space. Caller must check
  399. * the specified block with access_ok() before calling this function.
  400. *
  401. * Returns number of bytes that could not be copied.
  402. * On success, this will be zero.
  403. *
  404. * If some data could not be copied, this function will pad the copied
  405. * data to the requested size using zero bytes.
  406. */
  407. static __always_inline unsigned long
  408. __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
  409. {
  410. if (__builtin_constant_p(n)) {
  411. unsigned long ret;
  412. switch (n) {
  413. case 1:
  414. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  415. return ret;
  416. case 2:
  417. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  418. return ret;
  419. case 4:
  420. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  421. return ret;
  422. }
  423. }
  424. return __copy_from_user_ll(to, from, n);
  425. }
  426. static __always_inline unsigned long
  427. __copy_from_user(void *to, const void __user *from, unsigned long n)
  428. {
  429. might_sleep();
  430. return __copy_from_user_inatomic(to, from, n);
  431. }
  432. unsigned long __must_check copy_to_user(void __user *to,
  433. const void *from, unsigned long n);
  434. unsigned long __must_check copy_from_user(void *to,
  435. const void __user *from, unsigned long n);
  436. long __must_check strncpy_from_user(char *dst, const char __user *src,
  437. long count);
  438. long __must_check __strncpy_from_user(char *dst,
  439. const char __user *src, long count);
  440. /**
  441. * strlen_user: - Get the size of a string in user space.
  442. * @str: The string to measure.
  443. *
  444. * Context: User context only. This function may sleep.
  445. *
  446. * Get the size of a NUL-terminated string in user space.
  447. *
  448. * Returns the size of the string INCLUDING the terminating NUL.
  449. * On exception, returns 0.
  450. *
  451. * If there is a limit on the length of a valid string, you may wish to
  452. * consider using strnlen_user() instead.
  453. */
  454. #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
  455. long strnlen_user(const char __user *str, long n);
  456. unsigned long __must_check clear_user(void __user *mem, unsigned long len);
  457. unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
  458. #endif /* __i386_UACCESS_H */