uaccess.h 24 KB

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