uaccess.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. *
  5. * This file was copied from include/asm-generic/uaccess.h
  6. */
  7. #ifndef _ASM_RISCV_UACCESS_H
  8. #define _ASM_RISCV_UACCESS_H
  9. #include <asm/pgtable.h> /* for TASK_SIZE */
  10. /*
  11. * User space memory access functions
  12. */
  13. #ifdef CONFIG_MMU
  14. #include <linux/errno.h>
  15. #include <linux/compiler.h>
  16. #include <linux/thread_info.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/extable.h>
  19. #include <asm/asm.h>
  20. #define __enable_user_access() \
  21. __asm__ __volatile__ ("csrs sstatus, %0" : : "r" (SR_SUM) : "memory")
  22. #define __disable_user_access() \
  23. __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory")
  24. /**
  25. * access_ok: - Checks if a user space pointer is valid
  26. * @addr: User space pointer to start of block to check
  27. * @size: Size of block to check
  28. *
  29. * Context: User context only. This function may sleep.
  30. *
  31. * Checks if a pointer to a block of memory in user space is valid.
  32. *
  33. * Returns true (nonzero) if the memory block may be valid, false (zero)
  34. * if it is definitely invalid.
  35. *
  36. * Note that, depending on architecture, this function probably just
  37. * checks that the pointer is in the user space range - after calling
  38. * this function, memory access functions may still return -EFAULT.
  39. */
  40. #define access_ok(addr, size) ({ \
  41. __chk_user_ptr(addr); \
  42. likely(__access_ok((unsigned long __force)(addr), (size))); \
  43. })
  44. /*
  45. * Ensure that the range [addr, addr+size) is within the process's
  46. * address space
  47. */
  48. static inline int __access_ok(unsigned long addr, unsigned long size)
  49. {
  50. return size <= TASK_SIZE && addr <= TASK_SIZE - size;
  51. }
  52. /*
  53. * The exception table consists of pairs of addresses: the first is the
  54. * address of an instruction that is allowed to fault, and the second is
  55. * the address at which the program should continue. No registers are
  56. * modified, so it is entirely up to the continuation code to figure out
  57. * what to do.
  58. *
  59. * All the routines below use bits of fixup code that are out of line
  60. * with the main instruction path. This means when everything is well,
  61. * we don't even have to jump over them. Further, they do not intrude
  62. * on our cache or tlb entries.
  63. */
  64. #define __LSW 0
  65. #define __MSW 1
  66. /*
  67. * The "__xxx" versions of the user access functions do not verify the address
  68. * space - it must have been done previously with a separate "access_ok()"
  69. * call.
  70. */
  71. #define __get_user_asm(insn, x, ptr, err) \
  72. do { \
  73. uintptr_t __tmp; \
  74. __typeof__(x) __x; \
  75. __asm__ __volatile__ ( \
  76. "1:\n" \
  77. " " insn " %1, %3\n" \
  78. "2:\n" \
  79. " .section .fixup,\"ax\"\n" \
  80. " .balign 4\n" \
  81. "3:\n" \
  82. " li %0, %4\n" \
  83. " li %1, 0\n" \
  84. " jump 2b, %2\n" \
  85. " .previous\n" \
  86. " .section __ex_table,\"a\"\n" \
  87. " .balign " RISCV_SZPTR "\n" \
  88. " " RISCV_PTR " 1b, 3b\n" \
  89. " .previous" \
  90. : "+r" (err), "=&r" (__x), "=r" (__tmp) \
  91. : "m" (*(ptr)), "i" (-EFAULT)); \
  92. (x) = __x; \
  93. } while (0)
  94. #ifdef CONFIG_64BIT
  95. #define __get_user_8(x, ptr, err) \
  96. __get_user_asm("ld", x, ptr, err)
  97. #else /* !CONFIG_64BIT */
  98. #define __get_user_8(x, ptr, err) \
  99. do { \
  100. u32 __user *__ptr = (u32 __user *)(ptr); \
  101. u32 __lo, __hi; \
  102. uintptr_t __tmp; \
  103. __asm__ __volatile__ ( \
  104. "1:\n" \
  105. " lw %1, %4\n" \
  106. "2:\n" \
  107. " lw %2, %5\n" \
  108. "3:\n" \
  109. " .section .fixup,\"ax\"\n" \
  110. " .balign 4\n" \
  111. "4:\n" \
  112. " li %0, %6\n" \
  113. " li %1, 0\n" \
  114. " li %2, 0\n" \
  115. " jump 3b, %3\n" \
  116. " .previous\n" \
  117. " .section __ex_table,\"a\"\n" \
  118. " .balign " RISCV_SZPTR "\n" \
  119. " " RISCV_PTR " 1b, 4b\n" \
  120. " " RISCV_PTR " 2b, 4b\n" \
  121. " .previous" \
  122. : "+r" (err), "=&r" (__lo), "=r" (__hi), \
  123. "=r" (__tmp) \
  124. : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
  125. "i" (-EFAULT)); \
  126. (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
  127. (((u64)__hi << 32) | __lo))); \
  128. } while (0)
  129. #endif /* CONFIG_64BIT */
  130. #define __get_user_nocheck(x, __gu_ptr, __gu_err) \
  131. do { \
  132. switch (sizeof(*__gu_ptr)) { \
  133. case 1: \
  134. __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
  135. break; \
  136. case 2: \
  137. __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
  138. break; \
  139. case 4: \
  140. __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
  141. break; \
  142. case 8: \
  143. __get_user_8((x), __gu_ptr, __gu_err); \
  144. break; \
  145. default: \
  146. BUILD_BUG(); \
  147. } \
  148. } while (0)
  149. /**
  150. * __get_user: - Get a simple variable from user space, with less checking.
  151. * @x: Variable to store result.
  152. * @ptr: Source address, in user space.
  153. *
  154. * Context: User context only. This function may sleep.
  155. *
  156. * This macro copies a single simple variable from user space to kernel
  157. * space. It supports simple types like char and int, but not larger
  158. * data types like structures or arrays.
  159. *
  160. * @ptr must have pointer-to-simple-variable type, and the result of
  161. * dereferencing @ptr must be assignable to @x without a cast.
  162. *
  163. * Caller must check the pointer with access_ok() before calling this
  164. * function.
  165. *
  166. * Returns zero on success, or -EFAULT on error.
  167. * On error, the variable @x is set to zero.
  168. */
  169. #define __get_user(x, ptr) \
  170. ({ \
  171. const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
  172. long __gu_err = 0; \
  173. \
  174. __chk_user_ptr(__gu_ptr); \
  175. \
  176. __enable_user_access(); \
  177. __get_user_nocheck(x, __gu_ptr, __gu_err); \
  178. __disable_user_access(); \
  179. \
  180. __gu_err; \
  181. })
  182. /**
  183. * get_user: - Get a simple variable from user space.
  184. * @x: Variable to store result.
  185. * @ptr: Source address, in user space.
  186. *
  187. * Context: User context only. This function may sleep.
  188. *
  189. * This macro copies a single simple variable from user space to kernel
  190. * space. It supports simple types like char and int, but not larger
  191. * data types like structures or arrays.
  192. *
  193. * @ptr must have pointer-to-simple-variable type, and the result of
  194. * dereferencing @ptr must be assignable to @x without a cast.
  195. *
  196. * Returns zero on success, or -EFAULT on error.
  197. * On error, the variable @x is set to zero.
  198. */
  199. #define get_user(x, ptr) \
  200. ({ \
  201. const __typeof__(*(ptr)) __user *__p = (ptr); \
  202. might_fault(); \
  203. access_ok(__p, sizeof(*__p)) ? \
  204. __get_user((x), __p) : \
  205. ((x) = 0, -EFAULT); \
  206. })
  207. #define __put_user_asm(insn, x, ptr, err) \
  208. do { \
  209. uintptr_t __tmp; \
  210. __typeof__(*(ptr)) __x = x; \
  211. __asm__ __volatile__ ( \
  212. "1:\n" \
  213. " " insn " %z3, %2\n" \
  214. "2:\n" \
  215. " .section .fixup,\"ax\"\n" \
  216. " .balign 4\n" \
  217. "3:\n" \
  218. " li %0, %4\n" \
  219. " jump 2b, %1\n" \
  220. " .previous\n" \
  221. " .section __ex_table,\"a\"\n" \
  222. " .balign " RISCV_SZPTR "\n" \
  223. " " RISCV_PTR " 1b, 3b\n" \
  224. " .previous" \
  225. : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
  226. : "rJ" (__x), "i" (-EFAULT)); \
  227. } while (0)
  228. #ifdef CONFIG_64BIT
  229. #define __put_user_8(x, ptr, err) \
  230. __put_user_asm("sd", x, ptr, err)
  231. #else /* !CONFIG_64BIT */
  232. #define __put_user_8(x, ptr, err) \
  233. do { \
  234. u32 __user *__ptr = (u32 __user *)(ptr); \
  235. u64 __x = (__typeof__((x)-(x)))(x); \
  236. uintptr_t __tmp; \
  237. __asm__ __volatile__ ( \
  238. "1:\n" \
  239. " sw %z4, %2\n" \
  240. "2:\n" \
  241. " sw %z5, %3\n" \
  242. "3:\n" \
  243. " .section .fixup,\"ax\"\n" \
  244. " .balign 4\n" \
  245. "4:\n" \
  246. " li %0, %6\n" \
  247. " jump 3b, %1\n" \
  248. " .previous\n" \
  249. " .section __ex_table,\"a\"\n" \
  250. " .balign " RISCV_SZPTR "\n" \
  251. " " RISCV_PTR " 1b, 4b\n" \
  252. " " RISCV_PTR " 2b, 4b\n" \
  253. " .previous" \
  254. : "+r" (err), "=r" (__tmp), \
  255. "=m" (__ptr[__LSW]), \
  256. "=m" (__ptr[__MSW]) \
  257. : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
  258. } while (0)
  259. #endif /* CONFIG_64BIT */
  260. #define __put_user_nocheck(x, __gu_ptr, __pu_err) \
  261. do { \
  262. switch (sizeof(*__gu_ptr)) { \
  263. case 1: \
  264. __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
  265. break; \
  266. case 2: \
  267. __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
  268. break; \
  269. case 4: \
  270. __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
  271. break; \
  272. case 8: \
  273. __put_user_8((x), __gu_ptr, __pu_err); \
  274. break; \
  275. default: \
  276. BUILD_BUG(); \
  277. } \
  278. } while (0)
  279. /**
  280. * __put_user: - Write a simple value into user space, with less checking.
  281. * @x: Value to copy to user space.
  282. * @ptr: Destination address, in user space.
  283. *
  284. * Context: User context only. This function may sleep.
  285. *
  286. * This macro copies a single simple value from kernel space to user
  287. * space. It supports simple types like char and int, but not larger
  288. * data types like structures or arrays.
  289. *
  290. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  291. * to the result of dereferencing @ptr. The value of @x is copied to avoid
  292. * re-ordering where @x is evaluated inside the block that enables user-space
  293. * access (thus bypassing user space protection if @x is a function).
  294. *
  295. * Caller must check the pointer with access_ok() before calling this
  296. * function.
  297. *
  298. * Returns zero on success, or -EFAULT on error.
  299. */
  300. #define __put_user(x, ptr) \
  301. ({ \
  302. __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
  303. __typeof__(*__gu_ptr) __val = (x); \
  304. long __pu_err = 0; \
  305. \
  306. __chk_user_ptr(__gu_ptr); \
  307. \
  308. __enable_user_access(); \
  309. __put_user_nocheck(__val, __gu_ptr, __pu_err); \
  310. __disable_user_access(); \
  311. \
  312. __pu_err; \
  313. })
  314. /**
  315. * put_user: - Write a simple value into user space.
  316. * @x: Value to copy to user space.
  317. * @ptr: Destination address, in user space.
  318. *
  319. * Context: User context only. This function may sleep.
  320. *
  321. * This macro copies a single simple value from kernel space to user
  322. * space. It supports simple types like char and int, but not larger
  323. * data types like structures or arrays.
  324. *
  325. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  326. * to the result of dereferencing @ptr.
  327. *
  328. * Returns zero on success, or -EFAULT on error.
  329. */
  330. #define put_user(x, ptr) \
  331. ({ \
  332. __typeof__(*(ptr)) __user *__p = (ptr); \
  333. might_fault(); \
  334. access_ok(__p, sizeof(*__p)) ? \
  335. __put_user((x), __p) : \
  336. -EFAULT; \
  337. })
  338. unsigned long __must_check __asm_copy_to_user(void __user *to,
  339. const void *from, unsigned long n);
  340. unsigned long __must_check __asm_copy_from_user(void *to,
  341. const void __user *from, unsigned long n);
  342. unsigned long __must_check __asm_copy_in_user(void __user *to,
  343. const void *from, unsigned long n);
  344. static inline unsigned long
  345. raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  346. {
  347. return __asm_copy_from_user(to, from, n);
  348. }
  349. static inline unsigned long
  350. raw_copy_to_user(void __user *to, const void *from, unsigned long n)
  351. {
  352. return __asm_copy_to_user(to, from, n);
  353. }
  354. static inline unsigned long
  355. raw_copy_in_user(void __user *to, const void *from, unsigned long n)
  356. {
  357. return __asm_copy_in_user(to, from, n);
  358. }
  359. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  360. extern long __must_check strlen_user(const char __user *str);
  361. extern long __must_check strnlen_user(const char __user *str, long n);
  362. extern
  363. unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
  364. static inline
  365. unsigned long __must_check clear_user(void __user *to, unsigned long n)
  366. {
  367. might_fault();
  368. return access_ok(to, n) ?
  369. __clear_user(to, n) : n;
  370. }
  371. /*
  372. * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults
  373. * will set "err" to -EFAULT, while successful accesses return the previous
  374. * value.
  375. */
  376. #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
  377. ({ \
  378. __typeof__(ptr) __ptr = (ptr); \
  379. __typeof__(*(ptr)) __old = (old); \
  380. __typeof__(*(ptr)) __new = (new); \
  381. __typeof__(*(ptr)) __ret; \
  382. __typeof__(err) __err = 0; \
  383. register unsigned int __rc; \
  384. __enable_user_access(); \
  385. switch (size) { \
  386. case 4: \
  387. __asm__ __volatile__ ( \
  388. "0:\n" \
  389. " lr.w" #scb " %[ret], %[ptr]\n" \
  390. " bne %[ret], %z[old], 1f\n" \
  391. " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \
  392. " bnez %[rc], 0b\n" \
  393. "1:\n" \
  394. ".section .fixup,\"ax\"\n" \
  395. ".balign 4\n" \
  396. "2:\n" \
  397. " li %[err], %[efault]\n" \
  398. " jump 1b, %[rc]\n" \
  399. ".previous\n" \
  400. ".section __ex_table,\"a\"\n" \
  401. ".balign " RISCV_SZPTR "\n" \
  402. " " RISCV_PTR " 1b, 2b\n" \
  403. ".previous\n" \
  404. : [ret] "=&r" (__ret), \
  405. [rc] "=&r" (__rc), \
  406. [ptr] "+A" (*__ptr), \
  407. [err] "=&r" (__err) \
  408. : [old] "rJ" (__old), \
  409. [new] "rJ" (__new), \
  410. [efault] "i" (-EFAULT)); \
  411. break; \
  412. case 8: \
  413. __asm__ __volatile__ ( \
  414. "0:\n" \
  415. " lr.d" #scb " %[ret], %[ptr]\n" \
  416. " bne %[ret], %z[old], 1f\n" \
  417. " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
  418. " bnez %[rc], 0b\n" \
  419. "1:\n" \
  420. ".section .fixup,\"ax\"\n" \
  421. ".balign 4\n" \
  422. "2:\n" \
  423. " li %[err], %[efault]\n" \
  424. " jump 1b, %[rc]\n" \
  425. ".previous\n" \
  426. ".section __ex_table,\"a\"\n" \
  427. ".balign " RISCV_SZPTR "\n" \
  428. " " RISCV_PTR " 1b, 2b\n" \
  429. ".previous\n" \
  430. : [ret] "=&r" (__ret), \
  431. [rc] "=&r" (__rc), \
  432. [ptr] "+A" (*__ptr), \
  433. [err] "=&r" (__err) \
  434. : [old] "rJ" (__old), \
  435. [new] "rJ" (__new), \
  436. [efault] "i" (-EFAULT)); \
  437. break; \
  438. default: \
  439. BUILD_BUG(); \
  440. } \
  441. __disable_user_access(); \
  442. (err) = __err; \
  443. __ret; \
  444. })
  445. #define HAVE_GET_KERNEL_NOFAULT
  446. #define __get_kernel_nofault(dst, src, type, err_label) \
  447. do { \
  448. long __kr_err; \
  449. \
  450. __get_user_nocheck(*((type *)(dst)), (type *)(src), __kr_err); \
  451. if (unlikely(__kr_err)) \
  452. goto err_label; \
  453. } while (0)
  454. #define __put_kernel_nofault(dst, src, type, err_label) \
  455. do { \
  456. long __kr_err; \
  457. \
  458. __put_user_nocheck(*((type *)(src)), (type *)(dst), __kr_err); \
  459. if (unlikely(__kr_err)) \
  460. goto err_label; \
  461. } while (0)
  462. #else /* CONFIG_MMU */
  463. #include <asm-generic/uaccess.h>
  464. #endif /* CONFIG_MMU */
  465. #endif /* _ASM_RISCV_UACCESS_H */