uaccess.h 22 KB

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