uaccess.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
  37. {
  38. /*
  39. * If we have used "sizeof()" for the size,
  40. * we know it won't overflow the limit (but
  41. * it might overflow the 'addr', so it's
  42. * important to subtract the size from the
  43. * limit, not add it to the address).
  44. */
  45. if (__builtin_constant_p(size))
  46. return addr > limit - size;
  47. /* Arbitrary sizes? Be careful about overflow */
  48. addr += size;
  49. if (addr < size)
  50. return true;
  51. return addr > limit;
  52. }
  53. #define __range_not_ok(addr, size, limit) \
  54. ({ \
  55. __chk_user_ptr(addr); \
  56. __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
  57. })
  58. /**
  59. * access_ok: - Checks if a user space pointer is valid
  60. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  61. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  62. * to write to a block, it is always safe to read from it.
  63. * @addr: User space pointer to start of block to check
  64. * @size: Size of block to check
  65. *
  66. * Context: User context only. This function may sleep.
  67. *
  68. * Checks if a pointer to a block of memory in user space is valid.
  69. *
  70. * Returns true (nonzero) if the memory block may be valid, false (zero)
  71. * if it is definitely invalid.
  72. *
  73. * Note that, depending on architecture, this function probably just
  74. * checks that the pointer is in the user space range - after calling
  75. * this function, memory access functions may still return -EFAULT.
  76. */
  77. #define access_ok(type, addr, size) \
  78. likely(!__range_not_ok(addr, size, user_addr_max()))
  79. /*
  80. * The exception table consists of pairs of addresses relative to the
  81. * exception table enty itself: the first is the address of an
  82. * instruction that is allowed to fault, and the second is the address
  83. * at which the program should continue. No registers are modified,
  84. * so it is entirely up to the continuation code to figure out what to
  85. * do.
  86. *
  87. * All the routines below use bits of fixup code that are out of line
  88. * with the main instruction path. This means when everything is well,
  89. * we don't even have to jump over them. Further, they do not intrude
  90. * on our cache or tlb entries.
  91. */
  92. struct exception_table_entry {
  93. int insn, fixup;
  94. };
  95. /* This is not the generic standard exception_table_entry format */
  96. #define ARCH_HAS_SORT_EXTABLE
  97. #define ARCH_HAS_SEARCH_EXTABLE
  98. extern int fixup_exception(struct pt_regs *regs);
  99. extern int early_fixup_exception(unsigned long *ip);
  100. /*
  101. * These are the main single-value transfer routines. They automatically
  102. * use the right size if we just have the right pointer type.
  103. *
  104. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  105. * and yet we don't want to do any pointers, because that is too much
  106. * of a performance impact. Thus we have a few rather ugly macros here,
  107. * and hide all the ugliness from the user.
  108. *
  109. * The "__xxx" versions of the user access functions are versions that
  110. * do not verify the address space, that must have been done previously
  111. * with a separate "access_ok()" call (this is used when we do multiple
  112. * accesses to the same area of user memory).
  113. */
  114. extern int __get_user_1(void);
  115. extern int __get_user_2(void);
  116. extern int __get_user_4(void);
  117. extern int __get_user_8(void);
  118. extern int __get_user_bad(void);
  119. /*
  120. * This is a type: either unsigned long, if the argument fits into
  121. * that type, or otherwise unsigned long long.
  122. */
  123. #define __inttype(x) \
  124. __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
  125. /**
  126. * get_user: - Get a simple variable from user space.
  127. * @x: Variable to store result.
  128. * @ptr: Source address, in user space.
  129. *
  130. * Context: User context only. This function may sleep.
  131. *
  132. * This macro copies a single simple variable from user space to kernel
  133. * space. It supports simple types like char and int, but not larger
  134. * data types like structures or arrays.
  135. *
  136. * @ptr must have pointer-to-simple-variable type, and the result of
  137. * dereferencing @ptr must be assignable to @x without a cast.
  138. *
  139. * Returns zero on success, or -EFAULT on error.
  140. * On error, the variable @x is set to zero.
  141. */
  142. /*
  143. * Careful: we have to cast the result to the type of the pointer
  144. * for sign reasons.
  145. *
  146. * The use of _ASM_DX as the register specifier is a bit of a
  147. * simplification, as gcc only cares about it as the starting point
  148. * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
  149. * (%ecx being the next register in gcc's x86 register sequence), and
  150. * %rdx on 64 bits.
  151. *
  152. * Clang/LLVM cares about the size of the register, but still wants
  153. * the base register for something that ends up being a pair.
  154. */
  155. #define get_user(x, ptr) \
  156. ({ \
  157. int __ret_gu; \
  158. register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
  159. __chk_user_ptr(ptr); \
  160. might_fault(); \
  161. asm volatile("call __get_user_%P3" \
  162. : "=a" (__ret_gu), "=r" (__val_gu) \
  163. : "0" (ptr), "i" (sizeof(*(ptr)))); \
  164. (x) = (__typeof__(*(ptr))) __val_gu; \
  165. __ret_gu; \
  166. })
  167. #define __put_user_x(size, x, ptr, __ret_pu) \
  168. asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
  169. : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  170. #ifdef CONFIG_X86_32
  171. #define __put_user_asm_u64(x, addr, err, errret) \
  172. asm volatile(ASM_STAC "\n" \
  173. "1: movl %%eax,0(%2)\n" \
  174. "2: movl %%edx,4(%2)\n" \
  175. "3: " ASM_CLAC "\n" \
  176. ".section .fixup,\"ax\"\n" \
  177. "4: movl %3,%0\n" \
  178. " jmp 3b\n" \
  179. ".previous\n" \
  180. _ASM_EXTABLE(1b, 4b) \
  181. _ASM_EXTABLE(2b, 4b) \
  182. : "=r" (err) \
  183. : "A" (x), "r" (addr), "i" (errret), "0" (err))
  184. #define __put_user_asm_ex_u64(x, addr) \
  185. asm volatile(ASM_STAC "\n" \
  186. "1: movl %%eax,0(%1)\n" \
  187. "2: movl %%edx,4(%1)\n" \
  188. "3: " ASM_CLAC "\n" \
  189. _ASM_EXTABLE_EX(1b, 2b) \
  190. _ASM_EXTABLE_EX(2b, 3b) \
  191. : : "A" (x), "r" (addr))
  192. #define __put_user_x8(x, ptr, __ret_pu) \
  193. asm volatile("call __put_user_8" : "=a" (__ret_pu) \
  194. : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
  195. #else
  196. #define __put_user_asm_u64(x, ptr, retval, errret) \
  197. __put_user_asm(x, ptr, retval, "q", "", "er", errret)
  198. #define __put_user_asm_ex_u64(x, addr) \
  199. __put_user_asm_ex(x, addr, "q", "", "er")
  200. #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
  201. #endif
  202. extern void __put_user_bad(void);
  203. /*
  204. * Strange magic calling convention: pointer in %ecx,
  205. * value in %eax(:%edx), return value in %eax. clobbers %rbx
  206. */
  207. extern void __put_user_1(void);
  208. extern void __put_user_2(void);
  209. extern void __put_user_4(void);
  210. extern void __put_user_8(void);
  211. /**
  212. * put_user: - Write a simple value into user space.
  213. * @x: Value to copy to user space.
  214. * @ptr: Destination address, in user space.
  215. *
  216. * Context: User context only. This function may sleep.
  217. *
  218. * This macro copies a single simple value from kernel space to user
  219. * space. It supports simple types like char and int, but not larger
  220. * data types like structures or arrays.
  221. *
  222. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  223. * to the result of dereferencing @ptr.
  224. *
  225. * Returns zero on success, or -EFAULT on error.
  226. */
  227. #define put_user(x, ptr) \
  228. ({ \
  229. int __ret_pu; \
  230. __typeof__(*(ptr)) __pu_val; \
  231. __chk_user_ptr(ptr); \
  232. might_fault(); \
  233. __pu_val = x; \
  234. switch (sizeof(*(ptr))) { \
  235. case 1: \
  236. __put_user_x(1, __pu_val, ptr, __ret_pu); \
  237. break; \
  238. case 2: \
  239. __put_user_x(2, __pu_val, ptr, __ret_pu); \
  240. break; \
  241. case 4: \
  242. __put_user_x(4, __pu_val, ptr, __ret_pu); \
  243. break; \
  244. case 8: \
  245. __put_user_x8(__pu_val, ptr, __ret_pu); \
  246. break; \
  247. default: \
  248. __put_user_x(X, __pu_val, ptr, __ret_pu); \
  249. break; \
  250. } \
  251. __ret_pu; \
  252. })
  253. #define __put_user_size(x, ptr, size, retval, errret) \
  254. do { \
  255. retval = 0; \
  256. __chk_user_ptr(ptr); \
  257. switch (size) { \
  258. case 1: \
  259. __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
  260. break; \
  261. case 2: \
  262. __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
  263. break; \
  264. case 4: \
  265. __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
  266. break; \
  267. case 8: \
  268. __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
  269. errret); \
  270. break; \
  271. default: \
  272. __put_user_bad(); \
  273. } \
  274. } while (0)
  275. #define __put_user_size_ex(x, ptr, size) \
  276. do { \
  277. __chk_user_ptr(ptr); \
  278. switch (size) { \
  279. case 1: \
  280. __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
  281. break; \
  282. case 2: \
  283. __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
  284. break; \
  285. case 4: \
  286. __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
  287. break; \
  288. case 8: \
  289. __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
  290. break; \
  291. default: \
  292. __put_user_bad(); \
  293. } \
  294. } while (0)
  295. #ifdef CONFIG_X86_32
  296. #define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad()
  297. #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
  298. #else
  299. #define __get_user_asm_u64(x, ptr, retval, errret) \
  300. __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
  301. #define __get_user_asm_ex_u64(x, ptr) \
  302. __get_user_asm_ex(x, ptr, "q", "", "=r")
  303. #endif
  304. #define __get_user_size(x, ptr, size, retval, errret) \
  305. do { \
  306. retval = 0; \
  307. __chk_user_ptr(ptr); \
  308. switch (size) { \
  309. case 1: \
  310. __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
  311. break; \
  312. case 2: \
  313. __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
  314. break; \
  315. case 4: \
  316. __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
  317. break; \
  318. case 8: \
  319. __get_user_asm_u64(x, ptr, retval, errret); \
  320. break; \
  321. default: \
  322. (x) = __get_user_bad(); \
  323. } \
  324. } while (0)
  325. #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  326. asm volatile(ASM_STAC "\n" \
  327. "1: mov"itype" %2,%"rtype"1\n" \
  328. "2: " ASM_CLAC "\n" \
  329. ".section .fixup,\"ax\"\n" \
  330. "3: mov %3,%0\n" \
  331. " xor"itype" %"rtype"1,%"rtype"1\n" \
  332. " jmp 2b\n" \
  333. ".previous\n" \
  334. _ASM_EXTABLE(1b, 3b) \
  335. : "=r" (err), ltype(x) \
  336. : "m" (__m(addr)), "i" (errret), "0" (err))
  337. #define __get_user_size_ex(x, ptr, size) \
  338. do { \
  339. __chk_user_ptr(ptr); \
  340. switch (size) { \
  341. case 1: \
  342. __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
  343. break; \
  344. case 2: \
  345. __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
  346. break; \
  347. case 4: \
  348. __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
  349. break; \
  350. case 8: \
  351. __get_user_asm_ex_u64(x, ptr); \
  352. break; \
  353. default: \
  354. (x) = __get_user_bad(); \
  355. } \
  356. } while (0)
  357. #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
  358. asm volatile("1: mov"itype" %1,%"rtype"0\n" \
  359. "2:\n" \
  360. _ASM_EXTABLE_EX(1b, 2b) \
  361. : ltype(x) : "m" (__m(addr)))
  362. #define __put_user_nocheck(x, ptr, size) \
  363. ({ \
  364. int __pu_err; \
  365. __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
  366. __pu_err; \
  367. })
  368. #define __get_user_nocheck(x, ptr, size) \
  369. ({ \
  370. int __gu_err; \
  371. unsigned long __gu_val; \
  372. __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
  373. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  374. __gu_err; \
  375. })
  376. /* FIXME: this hack is definitely wrong -AK */
  377. struct __large_struct { unsigned long buf[100]; };
  378. #define __m(x) (*(struct __large_struct __user *)(x))
  379. /*
  380. * Tell gcc we read from memory instead of writing: this is because
  381. * we do not write to any memory gcc knows about, so there are no
  382. * aliasing issues.
  383. */
  384. #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
  385. asm volatile(ASM_STAC "\n" \
  386. "1: mov"itype" %"rtype"1,%2\n" \
  387. "2: " ASM_CLAC "\n" \
  388. ".section .fixup,\"ax\"\n" \
  389. "3: mov %3,%0\n" \
  390. " jmp 2b\n" \
  391. ".previous\n" \
  392. _ASM_EXTABLE(1b, 3b) \
  393. : "=r"(err) \
  394. : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
  395. #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
  396. asm volatile("1: mov"itype" %"rtype"0,%1\n" \
  397. "2:\n" \
  398. _ASM_EXTABLE_EX(1b, 2b) \
  399. : : ltype(x), "m" (__m(addr)))
  400. /*
  401. * uaccess_try and catch
  402. */
  403. #define uaccess_try do { \
  404. current_thread_info()->uaccess_err = 0; \
  405. stac(); \
  406. barrier();
  407. #define uaccess_catch(err) \
  408. clac(); \
  409. (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0); \
  410. } while (0)
  411. /**
  412. * __get_user: - Get a simple variable from user space, with less checking.
  413. * @x: Variable to store result.
  414. * @ptr: Source address, in user space.
  415. *
  416. * Context: User context only. This function may sleep.
  417. *
  418. * This macro copies a single simple variable from user space to kernel
  419. * space. It supports simple types like char and int, but not larger
  420. * data types like structures or arrays.
  421. *
  422. * @ptr must have pointer-to-simple-variable type, and the result of
  423. * dereferencing @ptr must be assignable to @x without a cast.
  424. *
  425. * Caller must check the pointer with access_ok() before calling this
  426. * function.
  427. *
  428. * Returns zero on success, or -EFAULT on error.
  429. * On error, the variable @x is set to zero.
  430. */
  431. #define __get_user(x, ptr) \
  432. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  433. /**
  434. * __put_user: - Write a simple value into user space, with less checking.
  435. * @x: Value to copy to user space.
  436. * @ptr: Destination address, in user space.
  437. *
  438. * Context: User context only. This function may sleep.
  439. *
  440. * This macro copies a single simple value from kernel space to user
  441. * space. It supports simple types like char and int, but not larger
  442. * data types like structures or arrays.
  443. *
  444. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  445. * to the result of dereferencing @ptr.
  446. *
  447. * Caller must check the pointer with access_ok() before calling this
  448. * function.
  449. *
  450. * Returns zero on success, or -EFAULT on error.
  451. */
  452. #define __put_user(x, ptr) \
  453. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  454. #define __get_user_unaligned __get_user
  455. #define __put_user_unaligned __put_user
  456. /*
  457. * {get|put}_user_try and catch
  458. *
  459. * get_user_try {
  460. * get_user_ex(...);
  461. * } get_user_catch(err)
  462. */
  463. #define get_user_try uaccess_try
  464. #define get_user_catch(err) uaccess_catch(err)
  465. #define get_user_ex(x, ptr) do { \
  466. unsigned long __gue_val; \
  467. __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
  468. (x) = (__force __typeof__(*(ptr)))__gue_val; \
  469. } while (0)
  470. #define put_user_try uaccess_try
  471. #define put_user_catch(err) uaccess_catch(err)
  472. #define put_user_ex(x, ptr) \
  473. __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  474. extern unsigned long
  475. copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
  476. extern __must_check long
  477. strncpy_from_user(char *dst, const char __user *src, long count);
  478. extern __must_check long strlen_user(const char __user *str);
  479. extern __must_check long strnlen_user(const char __user *str, long n);
  480. unsigned long __must_check clear_user(void __user *mem, unsigned long len);
  481. unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
  482. extern void __cmpxchg_wrong_size(void)
  483. __compiletime_error("Bad argument size for cmpxchg");
  484. #define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
  485. ({ \
  486. int __ret = 0; \
  487. __typeof__(ptr) __uval = (uval); \
  488. __typeof__(*(ptr)) __old = (old); \
  489. __typeof__(*(ptr)) __new = (new); \
  490. switch (size) { \
  491. case 1: \
  492. { \
  493. asm volatile("\t" ASM_STAC "\n" \
  494. "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
  495. "2:\t" ASM_CLAC "\n" \
  496. "\t.section .fixup, \"ax\"\n" \
  497. "3:\tmov %3, %0\n" \
  498. "\tjmp 2b\n" \
  499. "\t.previous\n" \
  500. _ASM_EXTABLE(1b, 3b) \
  501. : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
  502. : "i" (-EFAULT), "q" (__new), "1" (__old) \
  503. : "memory" \
  504. ); \
  505. break; \
  506. } \
  507. case 2: \
  508. { \
  509. asm volatile("\t" ASM_STAC "\n" \
  510. "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
  511. "2:\t" ASM_CLAC "\n" \
  512. "\t.section .fixup, \"ax\"\n" \
  513. "3:\tmov %3, %0\n" \
  514. "\tjmp 2b\n" \
  515. "\t.previous\n" \
  516. _ASM_EXTABLE(1b, 3b) \
  517. : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
  518. : "i" (-EFAULT), "r" (__new), "1" (__old) \
  519. : "memory" \
  520. ); \
  521. break; \
  522. } \
  523. case 4: \
  524. { \
  525. asm volatile("\t" ASM_STAC "\n" \
  526. "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
  527. "2:\t" ASM_CLAC "\n" \
  528. "\t.section .fixup, \"ax\"\n" \
  529. "3:\tmov %3, %0\n" \
  530. "\tjmp 2b\n" \
  531. "\t.previous\n" \
  532. _ASM_EXTABLE(1b, 3b) \
  533. : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
  534. : "i" (-EFAULT), "r" (__new), "1" (__old) \
  535. : "memory" \
  536. ); \
  537. break; \
  538. } \
  539. case 8: \
  540. { \
  541. if (!IS_ENABLED(CONFIG_X86_64)) \
  542. __cmpxchg_wrong_size(); \
  543. \
  544. asm volatile("\t" ASM_STAC "\n" \
  545. "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
  546. "2:\t" ASM_CLAC "\n" \
  547. "\t.section .fixup, \"ax\"\n" \
  548. "3:\tmov %3, %0\n" \
  549. "\tjmp 2b\n" \
  550. "\t.previous\n" \
  551. _ASM_EXTABLE(1b, 3b) \
  552. : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
  553. : "i" (-EFAULT), "r" (__new), "1" (__old) \
  554. : "memory" \
  555. ); \
  556. break; \
  557. } \
  558. default: \
  559. __cmpxchg_wrong_size(); \
  560. } \
  561. *__uval = __old; \
  562. __ret; \
  563. })
  564. #define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
  565. ({ \
  566. access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ? \
  567. __user_atomic_cmpxchg_inatomic((uval), (ptr), \
  568. (old), (new), sizeof(*(ptr))) : \
  569. -EFAULT; \
  570. })
  571. /*
  572. * movsl can be slow when source and dest are not both 8-byte aligned
  573. */
  574. #ifdef CONFIG_X86_INTEL_USERCOPY
  575. extern struct movsl_mask {
  576. int mask;
  577. } ____cacheline_aligned_in_smp movsl_mask;
  578. #endif
  579. #define ARCH_HAS_NOCACHE_UACCESS 1
  580. #ifdef CONFIG_X86_32
  581. # include <asm/uaccess_32.h>
  582. #else
  583. # include <asm/uaccess_64.h>
  584. #endif
  585. unsigned long __must_check _copy_from_user(void *to, const void __user *from,
  586. unsigned n);
  587. unsigned long __must_check _copy_to_user(void __user *to, const void *from,
  588. unsigned n);
  589. #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
  590. # define copy_user_diag __compiletime_error
  591. #else
  592. # define copy_user_diag __compiletime_warning
  593. #endif
  594. extern void copy_user_diag("copy_from_user() buffer size is too small")
  595. copy_from_user_overflow(void);
  596. extern void copy_user_diag("copy_to_user() buffer size is too small")
  597. copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
  598. #undef copy_user_diag
  599. #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
  600. extern void
  601. __compiletime_warning("copy_from_user() buffer size is not provably correct")
  602. __copy_from_user_overflow(void) __asm__("copy_from_user_overflow");
  603. #define __copy_from_user_overflow(size, count) __copy_from_user_overflow()
  604. extern void
  605. __compiletime_warning("copy_to_user() buffer size is not provably correct")
  606. __copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
  607. #define __copy_to_user_overflow(size, count) __copy_to_user_overflow()
  608. #else
  609. static inline void
  610. __copy_from_user_overflow(int size, unsigned long count)
  611. {
  612. WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
  613. }
  614. #define __copy_to_user_overflow __copy_from_user_overflow
  615. #endif
  616. static inline unsigned long __must_check
  617. copy_from_user(void *to, const void __user *from, unsigned long n)
  618. {
  619. int sz = __compiletime_object_size(to);
  620. might_fault();
  621. /*
  622. * While we would like to have the compiler do the checking for us
  623. * even in the non-constant size case, any false positives there are
  624. * a problem (especially when DEBUG_STRICT_USER_COPY_CHECKS, but even
  625. * without - the [hopefully] dangerous looking nature of the warning
  626. * would make people go look at the respecitive call sites over and
  627. * over again just to find that there's no problem).
  628. *
  629. * And there are cases where it's just not realistic for the compiler
  630. * to prove the count to be in range. For example when multiple call
  631. * sites of a helper function - perhaps in different source files -
  632. * all doing proper range checking, yet the helper function not doing
  633. * so again.
  634. *
  635. * Therefore limit the compile time checking to the constant size
  636. * case, and do only runtime checking for non-constant sizes.
  637. */
  638. if (likely(sz < 0 || sz >= n))
  639. n = _copy_from_user(to, from, n);
  640. else if(__builtin_constant_p(n))
  641. copy_from_user_overflow();
  642. else
  643. __copy_from_user_overflow(sz, n);
  644. return n;
  645. }
  646. static inline unsigned long __must_check
  647. copy_to_user(void __user *to, const void *from, unsigned long n)
  648. {
  649. int sz = __compiletime_object_size(from);
  650. might_fault();
  651. /* See the comment in copy_from_user() above. */
  652. if (likely(sz < 0 || sz >= n))
  653. n = _copy_to_user(to, from, n);
  654. else if(__builtin_constant_p(n))
  655. copy_to_user_overflow();
  656. else
  657. __copy_to_user_overflow(sz, n);
  658. return n;
  659. }
  660. #undef __copy_from_user_overflow
  661. #undef __copy_to_user_overflow
  662. #endif /* _ASM_X86_UACCESS_H */