uaccess.h 21 KB

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