uaccess.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. #ifndef _ASM_X86_UACCESS_H
  2. #define _ASM_X86_UACCESS_H
  3. /*
  4. * User space memory access functions
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/compiler.h>
  8. #include <linux/thread_info.h>
  9. #include <linux/string.h>
  10. #include <asm/asm.h>
  11. #include <asm/page.h>
  12. #include <asm/smap.h>
  13. #define VERIFY_READ 0
  14. #define VERIFY_WRITE 1
  15. /*
  16. * The fs value determines whether argument validity checking should be
  17. * performed or not. If get_fs() == USER_DS, checking is performed, with
  18. * get_fs() == KERNEL_DS, checking is bypassed.
  19. *
  20. * For historical reasons, these macros are grossly misnamed.
  21. */
  22. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  23. #define KERNEL_DS MAKE_MM_SEG(-1UL)
  24. #define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX)
  25. #define get_ds() (KERNEL_DS)
  26. #define get_fs() (current_thread_info()->addr_limit)
  27. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  28. #define segment_eq(a, b) ((a).seg == (b).seg)
  29. #define user_addr_max() (current_thread_info()->addr_limit.seg)
  30. #define __addr_ok(addr) \
  31. ((unsigned long __force)(addr) < user_addr_max())
  32. /*
  33. * Test whether a block of memory is a valid user space address.
  34. * Returns 0 if the range is valid, nonzero otherwise.
  35. *
  36. * This is equivalent to the following test:
  37. * (u33)addr + (u33)size > (u33)current->addr_limit.seg (u65 for x86_64)
  38. *
  39. * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry...
  40. */
  41. #define __range_not_ok(addr, size, limit) \
  42. ({ \
  43. unsigned long flag, roksum; \
  44. __chk_user_ptr(addr); \
  45. asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0" \
  46. : "=&r" (flag), "=r" (roksum) \
  47. : "1" (addr), "g" ((long)(size)), \
  48. "rm" (limit)); \
  49. flag; \
  50. })
  51. /**
  52. * access_ok: - Checks if a user space pointer is valid
  53. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  54. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  55. * to write to a block, it is always safe to read from it.
  56. * @addr: User space pointer to start of block to check
  57. * @size: Size of block to check
  58. *
  59. * Context: User context only. This function may sleep.
  60. *
  61. * Checks if a pointer to a block of memory in user space is valid.
  62. *
  63. * Returns true (nonzero) if the memory block may be valid, false (zero)
  64. * if it is definitely invalid.
  65. *
  66. * Note that, depending on architecture, this function probably just
  67. * checks that the pointer is in the user space range - after calling
  68. * this function, memory access functions may still return -EFAULT.
  69. */
  70. #define access_ok(type, addr, size) \
  71. (likely(__range_not_ok(addr, size, user_addr_max()) == 0))
  72. /*
  73. * The exception table consists of pairs of addresses relative to the
  74. * exception table enty itself: the first is the address of an
  75. * instruction that is allowed to fault, and the second is the address
  76. * at which the program should continue. No registers are modified,
  77. * so it is entirely up to the continuation code to figure out what to
  78. * 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. int insn, fixup;
  87. };
  88. /* This is not the generic standard exception_table_entry format */
  89. #define ARCH_HAS_SORT_EXTABLE
  90. #define ARCH_HAS_SEARCH_EXTABLE
  91. extern int fixup_exception(struct pt_regs *regs);
  92. extern int early_fixup_exception(unsigned long *ip);
  93. /*
  94. * These are the main single-value transfer routines. They automatically
  95. * use the right size if we just have the right pointer type.
  96. *
  97. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  98. * and yet we don't want to do any pointers, because that is too much
  99. * of a performance impact. Thus we have a few rather ugly macros here,
  100. * and hide all the ugliness from the user.
  101. *
  102. * The "__xxx" versions of the user access functions are versions that
  103. * do not verify the address space, that must have been done previously
  104. * with a separate "access_ok()" call (this is used when we do multiple
  105. * accesses to the same area of user memory).
  106. */
  107. extern int __get_user_1(void);
  108. extern int __get_user_2(void);
  109. extern int __get_user_4(void);
  110. extern int __get_user_8(void);
  111. extern int __get_user_bad(void);
  112. #define __get_user_x(size, ret, x, ptr) \
  113. asm volatile("call __get_user_" #size \
  114. : "=a" (ret), "=d" (x) \
  115. : "0" (ptr)) \
  116. /* Careful: we have to cast the result to the type of the pointer
  117. * for sign reasons */
  118. /**
  119. * get_user: - Get a simple variable from user space.
  120. * @x: Variable to store result.
  121. * @ptr: Source address, in user space.
  122. *
  123. * Context: User context only. This function may sleep.
  124. *
  125. * This macro copies a single simple variable from user space to kernel
  126. * space. It supports simple types like char and int, but not larger
  127. * data types like structures or arrays.
  128. *
  129. * @ptr must have pointer-to-simple-variable type, and the result of
  130. * dereferencing @ptr must be assignable to @x without a cast.
  131. *
  132. * Returns zero on success, or -EFAULT on error.
  133. * On error, the variable @x is set to zero.
  134. */
  135. #ifdef CONFIG_X86_32
  136. #define __get_user_8(ret, x, ptr) \
  137. do { \
  138. register unsigned long long __xx asm("%edx"); \
  139. asm volatile("call __get_user_8" \
  140. : "=a" (ret), "=r" (__xx) \
  141. : "0" (ptr)); \
  142. (x) = __xx; \
  143. } while (0)
  144. #else
  145. #define __get_user_8(__ret_gu, __val_gu, ptr) \
  146. __get_user_x(8, __ret_gu, __val_gu, ptr)
  147. #endif
  148. #define get_user(x, ptr) \
  149. ({ \
  150. int __ret_gu; \
  151. unsigned long __val_gu; \
  152. unsigned long long __val_gu8; \
  153. __chk_user_ptr(ptr); \
  154. might_fault(); \
  155. switch (sizeof(*(ptr))) { \
  156. case 1: \
  157. __get_user_x(1, __ret_gu, __val_gu, ptr); \
  158. break; \
  159. case 2: \
  160. __get_user_x(2, __ret_gu, __val_gu, ptr); \
  161. break; \
  162. case 4: \
  163. __get_user_x(4, __ret_gu, __val_gu, ptr); \
  164. break; \
  165. case 8: \
  166. __get_user_8(__ret_gu, __val_gu8, ptr); \
  167. break; \
  168. default: \
  169. __get_user_x(X, __ret_gu, __val_gu, ptr); \
  170. break; \
  171. } \
  172. if (sizeof(*(ptr)) == 8) \
  173. (x) = (__typeof__(*(ptr)))__val_gu8; \
  174. else \
  175. (x) = (__typeof__(*(ptr)))__val_gu; \
  176. __ret_gu; \
  177. })
  178. #define __put_user_x(size, x, ptr, __ret_pu) \
  179. asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
  180. : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  181. #ifdef CONFIG_X86_32
  182. #define __put_user_asm_u64(x, addr, err, errret) \
  183. asm volatile(ASM_STAC "\n" \
  184. "1: movl %%eax,0(%2)\n" \
  185. "2: movl %%edx,4(%2)\n" \
  186. "3: " ASM_CLAC "\n" \
  187. ".section .fixup,\"ax\"\n" \
  188. "4: movl %3,%0\n" \
  189. " jmp 3b\n" \
  190. ".previous\n" \
  191. _ASM_EXTABLE(1b, 4b) \
  192. _ASM_EXTABLE(2b, 4b) \
  193. : "=r" (err) \
  194. : "A" (x), "r" (addr), "i" (errret), "0" (err))
  195. #define __put_user_asm_ex_u64(x, addr) \
  196. asm volatile(ASM_STAC "\n" \
  197. "1: movl %%eax,0(%1)\n" \
  198. "2: movl %%edx,4(%1)\n" \
  199. "3: " ASM_CLAC "\n" \
  200. _ASM_EXTABLE_EX(1b, 2b) \
  201. _ASM_EXTABLE_EX(2b, 3b) \
  202. : : "A" (x), "r" (addr))
  203. #define __put_user_x8(x, ptr, __ret_pu) \
  204. asm volatile("call __put_user_8" : "=a" (__ret_pu) \
  205. : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  206. #else
  207. #define __put_user_asm_u64(x, ptr, retval, errret) \
  208. __put_user_asm(x, ptr, retval, "q", "", "er", errret)
  209. #define __put_user_asm_ex_u64(x, addr) \
  210. __put_user_asm_ex(x, addr, "q", "", "er")
  211. #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
  212. #endif
  213. extern void __put_user_bad(void);
  214. /*
  215. * Strange magic calling convention: pointer in %ecx,
  216. * value in %eax(:%edx), return value in %eax. clobbers %rbx
  217. */
  218. extern void __put_user_1(void);
  219. extern void __put_user_2(void);
  220. extern void __put_user_4(void);
  221. extern void __put_user_8(void);
  222. /**
  223. * put_user: - Write a simple value into user space.
  224. * @x: Value to copy to user space.
  225. * @ptr: Destination address, in user space.
  226. *
  227. * Context: User context only. This function may sleep.
  228. *
  229. * This macro copies a single simple value from kernel space to user
  230. * space. It supports simple types like char and int, but not larger
  231. * data types like structures or arrays.
  232. *
  233. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  234. * to the result of dereferencing @ptr.
  235. *
  236. * Returns zero on success, or -EFAULT on error.
  237. */
  238. #define put_user(x, ptr) \
  239. ({ \
  240. int __ret_pu; \
  241. __typeof__(*(ptr)) __pu_val; \
  242. __chk_user_ptr(ptr); \
  243. might_fault(); \
  244. __pu_val = x; \
  245. switch (sizeof(*(ptr))) { \
  246. case 1: \
  247. __put_user_x(1, __pu_val, ptr, __ret_pu); \
  248. break; \
  249. case 2: \
  250. __put_user_x(2, __pu_val, ptr, __ret_pu); \
  251. break; \
  252. case 4: \
  253. __put_user_x(4, __pu_val, ptr, __ret_pu); \
  254. break; \
  255. case 8: \
  256. __put_user_x8(__pu_val, ptr, __ret_pu); \
  257. break; \
  258. default: \
  259. __put_user_x(X, __pu_val, ptr, __ret_pu); \
  260. break; \
  261. } \
  262. __ret_pu; \
  263. })
  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: \
  270. __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
  271. break; \
  272. case 2: \
  273. __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
  274. break; \
  275. case 4: \
  276. __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
  277. break; \
  278. case 8: \
  279. __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
  280. errret); \
  281. break; \
  282. default: \
  283. __put_user_bad(); \
  284. } \
  285. } while (0)
  286. #define __put_user_size_ex(x, ptr, size) \
  287. do { \
  288. __chk_user_ptr(ptr); \
  289. switch (size) { \
  290. case 1: \
  291. __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
  292. break; \
  293. case 2: \
  294. __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
  295. break; \
  296. case 4: \
  297. __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
  298. break; \
  299. case 8: \
  300. __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
  301. break; \
  302. default: \
  303. __put_user_bad(); \
  304. } \
  305. } while (0)
  306. #ifdef CONFIG_X86_32
  307. #define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad()
  308. #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
  309. #else
  310. #define __get_user_asm_u64(x, ptr, retval, errret) \
  311. __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
  312. #define __get_user_asm_ex_u64(x, ptr) \
  313. __get_user_asm_ex(x, ptr, "q", "", "=r")
  314. #endif
  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: \
  321. __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
  322. break; \
  323. case 2: \
  324. __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
  325. break; \
  326. case 4: \
  327. __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
  328. break; \
  329. case 8: \
  330. __get_user_asm_u64(x, ptr, retval, errret); \
  331. break; \
  332. default: \
  333. (x) = __get_user_bad(); \
  334. } \
  335. } while (0)
  336. #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  337. asm volatile(ASM_STAC "\n" \
  338. "1: mov"itype" %2,%"rtype"1\n" \
  339. "2: " ASM_CLAC "\n" \
  340. ".section .fixup,\"ax\"\n" \
  341. "3: mov %3,%0\n" \
  342. " xor"itype" %"rtype"1,%"rtype"1\n" \
  343. " jmp 2b\n" \
  344. ".previous\n" \
  345. _ASM_EXTABLE(1b, 3b) \
  346. : "=r" (err), ltype(x) \
  347. : "m" (__m(addr)), "i" (errret), "0" (err))
  348. #define __get_user_size_ex(x, ptr, size) \
  349. do { \
  350. __chk_user_ptr(ptr); \
  351. switch (size) { \
  352. case 1: \
  353. __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
  354. break; \
  355. case 2: \
  356. __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
  357. break; \
  358. case 4: \
  359. __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
  360. break; \
  361. case 8: \
  362. __get_user_asm_ex_u64(x, ptr); \
  363. break; \
  364. default: \
  365. (x) = __get_user_bad(); \
  366. } \
  367. } while (0)
  368. #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
  369. asm volatile("1: mov"itype" %1,%"rtype"0\n" \
  370. "2:\n" \
  371. _ASM_EXTABLE_EX(1b, 2b) \
  372. : ltype(x) : "m" (__m(addr)))
  373. #define __put_user_nocheck(x, ptr, size) \
  374. ({ \
  375. int __pu_err; \
  376. __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
  377. __pu_err; \
  378. })
  379. #define __get_user_nocheck(x, ptr, size) \
  380. ({ \
  381. int __gu_err; \
  382. unsigned long __gu_val; \
  383. __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
  384. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  385. __gu_err; \
  386. })
  387. /* FIXME: this hack is definitely wrong -AK */
  388. struct __large_struct { unsigned long buf[100]; };
  389. #define __m(x) (*(struct __large_struct __user *)(x))
  390. /*
  391. * Tell gcc we read from memory instead of writing: this is because
  392. * we do not write to any memory gcc knows about, so there are no
  393. * aliasing issues.
  394. */
  395. #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  396. asm volatile(ASM_STAC "\n" \
  397. "1: mov"itype" %"rtype"1,%2\n" \
  398. "2: " ASM_CLAC "\n" \
  399. ".section .fixup,\"ax\"\n" \
  400. "3: mov %3,%0\n" \
  401. " jmp 2b\n" \
  402. ".previous\n" \
  403. _ASM_EXTABLE(1b, 3b) \
  404. : "=r"(err) \
  405. : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
  406. #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
  407. asm volatile("1: mov"itype" %"rtype"0,%1\n" \
  408. "2:\n" \
  409. _ASM_EXTABLE_EX(1b, 2b) \
  410. : : ltype(x), "m" (__m(addr)))
  411. /*
  412. * uaccess_try and catch
  413. */
  414. #define uaccess_try do { \
  415. current_thread_info()->uaccess_err = 0; \
  416. stac(); \
  417. barrier();
  418. #define uaccess_catch(err) \
  419. clac(); \
  420. (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0); \
  421. } while (0)
  422. /**
  423. * __get_user: - Get a simple variable from user space, with less checking.
  424. * @x: Variable to store result.
  425. * @ptr: Source address, in user space.
  426. *
  427. * Context: User context only. This function may sleep.
  428. *
  429. * This macro copies a single simple variable from user space to kernel
  430. * space. It supports simple types like char and int, but not larger
  431. * data types like structures or arrays.
  432. *
  433. * @ptr must have pointer-to-simple-variable type, and the result of
  434. * dereferencing @ptr must be assignable to @x without a cast.
  435. *
  436. * Caller must check the pointer with access_ok() before calling this
  437. * function.
  438. *
  439. * Returns zero on success, or -EFAULT on error.
  440. * On error, the variable @x is set to zero.
  441. */
  442. #define __get_user(x, ptr) \
  443. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  444. /**
  445. * __put_user: - Write a simple value into user space, with less checking.
  446. * @x: Value to copy to user space.
  447. * @ptr: Destination address, in user space.
  448. *
  449. * Context: User context only. This function may sleep.
  450. *
  451. * This macro copies a single simple value from kernel space to user
  452. * space. It supports simple types like char and int, but not larger
  453. * data types like structures or arrays.
  454. *
  455. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  456. * to the result of dereferencing @ptr.
  457. *
  458. * Caller must check the pointer with access_ok() before calling this
  459. * function.
  460. *
  461. * Returns zero on success, or -EFAULT on error.
  462. */
  463. #define __put_user(x, ptr) \
  464. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  465. #define __get_user_unaligned __get_user
  466. #define __put_user_unaligned __put_user
  467. /*
  468. * {get|put}_user_try and catch
  469. *
  470. * get_user_try {
  471. * get_user_ex(...);
  472. * } get_user_catch(err)
  473. */
  474. #define get_user_try uaccess_try
  475. #define get_user_catch(err) uaccess_catch(err)
  476. #define get_user_ex(x, ptr) do { \
  477. unsigned long __gue_val; \
  478. __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
  479. (x) = (__force __typeof__(*(ptr)))__gue_val; \
  480. } while (0)
  481. #define put_user_try uaccess_try
  482. #define put_user_catch(err) uaccess_catch(err)
  483. #define put_user_ex(x, ptr) \
  484. __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  485. extern unsigned long
  486. copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
  487. extern __must_check long
  488. strncpy_from_user(char *dst, const char __user *src, long count);
  489. extern __must_check long strlen_user(const char __user *str);
  490. extern __must_check long strnlen_user(const char __user *str, long n);
  491. unsigned long __must_check clear_user(void __user *mem, unsigned long len);
  492. unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
  493. /*
  494. * movsl can be slow when source and dest are not both 8-byte aligned
  495. */
  496. #ifdef CONFIG_X86_INTEL_USERCOPY
  497. extern struct movsl_mask {
  498. int mask;
  499. } ____cacheline_aligned_in_smp movsl_mask;
  500. #endif
  501. #define ARCH_HAS_NOCACHE_UACCESS 1
  502. #ifdef CONFIG_X86_32
  503. # include <asm/uaccess_32.h>
  504. #else
  505. # include <asm/uaccess_64.h>
  506. #endif
  507. #endif /* _ASM_X86_UACCESS_H */