uaccess.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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/prefetch.h>
  10. #include <linux/string.h>
  11. #include <asm/asm.h>
  12. #include <asm/page.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(PAGE_OFFSET)
  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 __addr_ok(addr) \
  30. ((unsigned long __force)(addr) < \
  31. (current_thread_info()->addr_limit.seg))
  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) \
  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" (current_thread_info()->addr_limit.seg)); \
  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) (likely(__range_not_ok(addr, size) == 0))
  71. /*
  72. * The exception table consists of pairs of addresses: the first is the
  73. * address of an instruction that is allowed to fault, and the second is
  74. * the address at which the program should continue. No registers are
  75. * modified, so it is entirely up to the continuation code to figure out
  76. * what to do.
  77. *
  78. * All the routines below use bits of fixup code that are out of line
  79. * with the main instruction path. This means when everything is well,
  80. * we don't even have to jump over them. Further, they do not intrude
  81. * on our cache or tlb entries.
  82. */
  83. struct exception_table_entry {
  84. unsigned long insn, fixup;
  85. };
  86. extern int fixup_exception(struct pt_regs *regs);
  87. /*
  88. * These are the main single-value transfer routines. They automatically
  89. * use the right size if we just have the right pointer type.
  90. *
  91. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  92. * and yet we don't want to do any pointers, because that is too much
  93. * of a performance impact. Thus we have a few rather ugly macros here,
  94. * and hide all the ugliness from the user.
  95. *
  96. * The "__xxx" versions of the user access functions are versions that
  97. * do not verify the address space, that must have been done previously
  98. * with a separate "access_ok()" call (this is used when we do multiple
  99. * accesses to the same area of user memory).
  100. */
  101. extern int __get_user_1(void);
  102. extern int __get_user_2(void);
  103. extern int __get_user_4(void);
  104. extern int __get_user_8(void);
  105. extern int __get_user_bad(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
  111. * 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. #ifdef CONFIG_X86_32
  130. #define __get_user_8(__ret_gu, __val_gu, ptr) \
  131. __get_user_x(X, __ret_gu, __val_gu, ptr)
  132. #else
  133. #define __get_user_8(__ret_gu, __val_gu, ptr) \
  134. __get_user_x(8, __ret_gu, __val_gu, ptr)
  135. #endif
  136. #define get_user(x, ptr) \
  137. ({ \
  138. int __ret_gu; \
  139. unsigned long __val_gu; \
  140. __chk_user_ptr(ptr); \
  141. might_fault(); \
  142. switch (sizeof(*(ptr))) { \
  143. case 1: \
  144. __get_user_x(1, __ret_gu, __val_gu, ptr); \
  145. break; \
  146. case 2: \
  147. __get_user_x(2, __ret_gu, __val_gu, ptr); \
  148. break; \
  149. case 4: \
  150. __get_user_x(4, __ret_gu, __val_gu, ptr); \
  151. break; \
  152. case 8: \
  153. __get_user_8(__ret_gu, __val_gu, ptr); \
  154. break; \
  155. default: \
  156. __get_user_x(X, __ret_gu, __val_gu, ptr); \
  157. break; \
  158. } \
  159. (x) = (__typeof__(*(ptr)))__val_gu; \
  160. __ret_gu; \
  161. })
  162. #define __put_user_x(size, x, ptr, __ret_pu) \
  163. asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
  164. : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  165. #ifdef CONFIG_X86_32
  166. #define __put_user_asm_u64(x, addr, err, errret) \
  167. asm volatile("1: movl %%eax,0(%2)\n" \
  168. "2: movl %%edx,4(%2)\n" \
  169. "3:\n" \
  170. ".section .fixup,\"ax\"\n" \
  171. "4: movl %3,%0\n" \
  172. " jmp 3b\n" \
  173. ".previous\n" \
  174. _ASM_EXTABLE(1b, 4b) \
  175. _ASM_EXTABLE(2b, 4b) \
  176. : "=r" (err) \
  177. : "A" (x), "r" (addr), "i" (errret), "0" (err))
  178. #define __put_user_asm_ex_u64(x, addr) \
  179. asm volatile("1: movl %%eax,0(%1)\n" \
  180. "2: movl %%edx,4(%1)\n" \
  181. "3:\n" \
  182. _ASM_EXTABLE(1b, 2b - 1b) \
  183. _ASM_EXTABLE(2b, 3b - 2b) \
  184. : : "A" (x), "r" (addr))
  185. #define __put_user_x8(x, ptr, __ret_pu) \
  186. asm volatile("call __put_user_8" : "=a" (__ret_pu) \
  187. : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  188. #else
  189. #define __put_user_asm_u64(x, ptr, retval, errret) \
  190. __put_user_asm(x, ptr, retval, "q", "", "Zr", errret)
  191. #define __put_user_asm_ex_u64(x, addr) \
  192. __put_user_asm_ex(x, addr, "q", "", "Zr")
  193. #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
  194. #endif
  195. extern void __put_user_bad(void);
  196. /*
  197. * Strange magic calling convention: pointer in %ecx,
  198. * value in %eax(:%edx), return value in %eax. clobbers %rbx
  199. */
  200. extern void __put_user_1(void);
  201. extern void __put_user_2(void);
  202. extern void __put_user_4(void);
  203. extern void __put_user_8(void);
  204. #ifdef CONFIG_X86_WP_WORKS_OK
  205. /**
  206. * put_user: - Write a simple value into user space.
  207. * @x: Value to copy to user space.
  208. * @ptr: Destination address, in user space.
  209. *
  210. * Context: User context only. This function may sleep.
  211. *
  212. * This macro copies a single simple value from kernel space to user
  213. * space. It supports simple types like char and int, but not larger
  214. * data types like structures or arrays.
  215. *
  216. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  217. * to the result of dereferencing @ptr.
  218. *
  219. * Returns zero on success, or -EFAULT on error.
  220. */
  221. #define put_user(x, ptr) \
  222. ({ \
  223. int __ret_pu; \
  224. __typeof__(*(ptr)) __pu_val; \
  225. __chk_user_ptr(ptr); \
  226. might_fault(); \
  227. __pu_val = x; \
  228. switch (sizeof(*(ptr))) { \
  229. case 1: \
  230. __put_user_x(1, __pu_val, ptr, __ret_pu); \
  231. break; \
  232. case 2: \
  233. __put_user_x(2, __pu_val, ptr, __ret_pu); \
  234. break; \
  235. case 4: \
  236. __put_user_x(4, __pu_val, ptr, __ret_pu); \
  237. break; \
  238. case 8: \
  239. __put_user_x8(__pu_val, ptr, __ret_pu); \
  240. break; \
  241. default: \
  242. __put_user_x(X, __pu_val, ptr, __ret_pu); \
  243. break; \
  244. } \
  245. __ret_pu; \
  246. })
  247. #define __put_user_size(x, ptr, size, retval, errret) \
  248. do { \
  249. retval = 0; \
  250. __chk_user_ptr(ptr); \
  251. switch (size) { \
  252. case 1: \
  253. __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
  254. break; \
  255. case 2: \
  256. __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
  257. break; \
  258. case 4: \
  259. __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
  260. break; \
  261. case 8: \
  262. __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
  263. errret); \
  264. break; \
  265. default: \
  266. __put_user_bad(); \
  267. } \
  268. } while (0)
  269. #define __put_user_size_ex(x, ptr, size) \
  270. do { \
  271. __chk_user_ptr(ptr); \
  272. switch (size) { \
  273. case 1: \
  274. __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
  275. break; \
  276. case 2: \
  277. __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
  278. break; \
  279. case 4: \
  280. __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
  281. break; \
  282. case 8: \
  283. __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
  284. break; \
  285. default: \
  286. __put_user_bad(); \
  287. } \
  288. } while (0)
  289. #else
  290. #define __put_user_size(x, ptr, size, retval, errret) \
  291. do { \
  292. __typeof__(*(ptr))__pus_tmp = x; \
  293. retval = 0; \
  294. \
  295. if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
  296. retval = errret; \
  297. } while (0)
  298. #define put_user(x, ptr) \
  299. ({ \
  300. int __ret_pu; \
  301. __typeof__(*(ptr))__pus_tmp = x; \
  302. __ret_pu = 0; \
  303. if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
  304. sizeof(*(ptr))) != 0)) \
  305. __ret_pu = -EFAULT; \
  306. __ret_pu; \
  307. })
  308. #endif
  309. #ifdef CONFIG_X86_32
  310. #define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad()
  311. #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
  312. #else
  313. #define __get_user_asm_u64(x, ptr, retval, errret) \
  314. __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
  315. #define __get_user_asm_ex_u64(x, ptr) \
  316. __get_user_asm_ex(x, ptr, "q", "", "=r")
  317. #endif
  318. #define __get_user_size(x, ptr, size, retval, errret) \
  319. do { \
  320. retval = 0; \
  321. __chk_user_ptr(ptr); \
  322. switch (size) { \
  323. case 1: \
  324. __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
  325. break; \
  326. case 2: \
  327. __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
  328. break; \
  329. case 4: \
  330. __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
  331. break; \
  332. case 8: \
  333. __get_user_asm_u64(x, ptr, retval, errret); \
  334. break; \
  335. default: \
  336. (x) = __get_user_bad(); \
  337. } \
  338. } while (0)
  339. #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  340. asm volatile("1: mov"itype" %2,%"rtype"1\n" \
  341. "2:\n" \
  342. ".section .fixup,\"ax\"\n" \
  343. "3: mov %3,%0\n" \
  344. " xor"itype" %"rtype"1,%"rtype"1\n" \
  345. " jmp 2b\n" \
  346. ".previous\n" \
  347. _ASM_EXTABLE(1b, 3b) \
  348. : "=r" (err), ltype(x) \
  349. : "m" (__m(addr)), "i" (errret), "0" (err))
  350. #define __get_user_size_ex(x, ptr, size) \
  351. do { \
  352. __chk_user_ptr(ptr); \
  353. switch (size) { \
  354. case 1: \
  355. __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
  356. break; \
  357. case 2: \
  358. __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
  359. break; \
  360. case 4: \
  361. __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
  362. break; \
  363. case 8: \
  364. __get_user_asm_ex_u64(x, ptr); \
  365. break; \
  366. default: \
  367. (x) = __get_user_bad(); \
  368. } \
  369. } while (0)
  370. #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
  371. asm volatile("1: mov"itype" %1,%"rtype"0\n" \
  372. "2:\n" \
  373. _ASM_EXTABLE(1b, 2b - 1b) \
  374. : ltype(x) : "m" (__m(addr)))
  375. #define __put_user_nocheck(x, ptr, size) \
  376. ({ \
  377. int __pu_err; \
  378. __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
  379. __pu_err; \
  380. })
  381. #define __get_user_nocheck(x, ptr, size) \
  382. ({ \
  383. int __gu_err; \
  384. unsigned long __gu_val; \
  385. __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
  386. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  387. __gu_err; \
  388. })
  389. /* FIXME: this hack is definitely wrong -AK */
  390. struct __large_struct { unsigned long buf[100]; };
  391. #define __m(x) (*(struct __large_struct __user *)(x))
  392. /*
  393. * Tell gcc we read from memory instead of writing: this is because
  394. * we do not write to any memory gcc knows about, so there are no
  395. * aliasing issues.
  396. */
  397. #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  398. asm volatile("1: mov"itype" %"rtype"1,%2\n" \
  399. "2:\n" \
  400. ".section .fixup,\"ax\"\n" \
  401. "3: mov %3,%0\n" \
  402. " jmp 2b\n" \
  403. ".previous\n" \
  404. _ASM_EXTABLE(1b, 3b) \
  405. : "=r"(err) \
  406. : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
  407. #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
  408. asm volatile("1: mov"itype" %"rtype"0,%1\n" \
  409. "2:\n" \
  410. _ASM_EXTABLE(1b, 2b - 1b) \
  411. : : ltype(x), "m" (__m(addr)))
  412. /*
  413. * uaccess_try and catch
  414. */
  415. #define uaccess_try do { \
  416. int prev_err = current_thread_info()->uaccess_err; \
  417. current_thread_info()->uaccess_err = 0; \
  418. barrier();
  419. #define uaccess_catch(err) \
  420. (err) |= current_thread_info()->uaccess_err; \
  421. current_thread_info()->uaccess_err = prev_err; \
  422. } while (0)
  423. /**
  424. * __get_user: - Get a simple variable from user space, with less checking.
  425. * @x: Variable to store result.
  426. * @ptr: Source address, in user space.
  427. *
  428. * Context: User context only. This function may sleep.
  429. *
  430. * This macro copies a single simple variable from user space to kernel
  431. * space. It supports simple types like char and int, but not larger
  432. * data types like structures or arrays.
  433. *
  434. * @ptr must have pointer-to-simple-variable type, and the result of
  435. * dereferencing @ptr must be assignable to @x without a cast.
  436. *
  437. * Caller must check the pointer with access_ok() before calling this
  438. * function.
  439. *
  440. * Returns zero on success, or -EFAULT on error.
  441. * On error, the variable @x is set to zero.
  442. */
  443. #define __get_user(x, ptr) \
  444. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  445. /**
  446. * __put_user: - Write a simple value into user space, with less checking.
  447. * @x: Value to copy to user space.
  448. * @ptr: Destination address, in user space.
  449. *
  450. * Context: User context only. This function may sleep.
  451. *
  452. * This macro copies a single simple value from kernel space to user
  453. * space. It supports simple types like char and int, but not larger
  454. * data types like structures or arrays.
  455. *
  456. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  457. * to the result of dereferencing @ptr.
  458. *
  459. * Caller must check the pointer with access_ok() before calling this
  460. * function.
  461. *
  462. * Returns zero on success, or -EFAULT on error.
  463. */
  464. #define __put_user(x, ptr) \
  465. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  466. #define __get_user_unaligned __get_user
  467. #define __put_user_unaligned __put_user
  468. /*
  469. * {get|put}_user_try and catch
  470. *
  471. * get_user_try {
  472. * get_user_ex(...);
  473. * } get_user_catch(err)
  474. */
  475. #define get_user_try uaccess_try
  476. #define get_user_catch(err) uaccess_catch(err)
  477. #define get_user_ex(x, ptr) do { \
  478. unsigned long __gue_val; \
  479. __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
  480. (x) = (__force __typeof__(*(ptr)))__gue_val; \
  481. } while (0)
  482. #ifdef CONFIG_X86_WP_WORKS_OK
  483. #define put_user_try uaccess_try
  484. #define put_user_catch(err) uaccess_catch(err)
  485. #define put_user_ex(x, ptr) \
  486. __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  487. #else /* !CONFIG_X86_WP_WORKS_OK */
  488. #define put_user_try do { \
  489. int __uaccess_err = 0;
  490. #define put_user_catch(err) \
  491. (err) |= __uaccess_err; \
  492. } while (0)
  493. #define put_user_ex(x, ptr) do { \
  494. __uaccess_err |= __put_user(x, ptr); \
  495. } while (0)
  496. #endif /* CONFIG_X86_WP_WORKS_OK */
  497. /*
  498. * movsl can be slow when source and dest are not both 8-byte aligned
  499. */
  500. #ifdef CONFIG_X86_INTEL_USERCOPY
  501. extern struct movsl_mask {
  502. int mask;
  503. } ____cacheline_aligned_in_smp movsl_mask;
  504. #endif
  505. #define ARCH_HAS_NOCACHE_UACCESS 1
  506. #ifdef CONFIG_X86_32
  507. # include "uaccess_32.h"
  508. #else
  509. # define ARCH_HAS_SEARCH_EXTABLE
  510. # include "uaccess_64.h"
  511. #endif
  512. #endif /* _ASM_X86_UACCESS_H */