uaccess.h 25 KB

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