uaccess.h 24 KB

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