uaccess.h 17 KB

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