uaccess.h 21 KB

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