uaccess.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/asm-extable.h>
  10. #include <asm/pgtable.h> /* for TASK_SIZE */
  11. /*
  12. * User space memory access functions
  13. */
  14. #ifdef CONFIG_MMU
  15. #include <linux/errno.h>
  16. #include <linux/compiler.h>
  17. #include <linux/thread_info.h>
  18. #include <asm/byteorder.h>
  19. #include <asm/extable.h>
  20. #include <asm/asm.h>
  21. #include <asm-generic/access_ok.h>
  22. #define __enable_user_access() \
  23. __asm__ __volatile__ ("csrs sstatus, %0" : : "r" (SR_SUM) : "memory")
  24. #define __disable_user_access() \
  25. __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory")
  26. /*
  27. * The exception table consists of pairs of addresses: the first is the
  28. * address of an instruction that is allowed to fault, and the second is
  29. * the address at which the program should continue. No registers are
  30. * modified, so it is entirely up to the continuation code to figure out
  31. * what to do.
  32. *
  33. * All the routines below use bits of fixup code that are out of line
  34. * with the main instruction path. This means when everything is well,
  35. * we don't even have to jump over them. Further, they do not intrude
  36. * on our cache or tlb entries.
  37. */
  38. #define __LSW 0
  39. #define __MSW 1
  40. /*
  41. * The "__xxx" versions of the user access functions do not verify the address
  42. * space - it must have been done previously with a separate "access_ok()"
  43. * call.
  44. */
  45. #define __get_user_asm(insn, x, ptr, err) \
  46. do { \
  47. __typeof__(x) __x; \
  48. __asm__ __volatile__ ( \
  49. "1:\n" \
  50. " " insn " %1, %2\n" \
  51. "2:\n" \
  52. _ASM_EXTABLE_UACCESS_ERR_ZERO(1b, 2b, %0, %1) \
  53. : "+r" (err), "=&r" (__x) \
  54. : "m" (*(ptr))); \
  55. (x) = __x; \
  56. } while (0)
  57. #ifdef CONFIG_64BIT
  58. #define __get_user_8(x, ptr, err) \
  59. __get_user_asm("ld", x, ptr, err)
  60. #else /* !CONFIG_64BIT */
  61. #define __get_user_8(x, ptr, err) \
  62. do { \
  63. u32 __user *__ptr = (u32 __user *)(ptr); \
  64. u32 __lo, __hi; \
  65. __asm__ __volatile__ ( \
  66. "1:\n" \
  67. " lw %1, %3\n" \
  68. "2:\n" \
  69. " lw %2, %4\n" \
  70. "3:\n" \
  71. _ASM_EXTABLE_UACCESS_ERR_ZERO(1b, 3b, %0, %1) \
  72. _ASM_EXTABLE_UACCESS_ERR_ZERO(2b, 3b, %0, %1) \
  73. : "+r" (err), "=&r" (__lo), "=r" (__hi) \
  74. : "m" (__ptr[__LSW]), "m" (__ptr[__MSW])); \
  75. if (err) \
  76. __hi = 0; \
  77. (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
  78. (((u64)__hi << 32) | __lo))); \
  79. } while (0)
  80. #endif /* CONFIG_64BIT */
  81. #define __get_user_nocheck(x, __gu_ptr, __gu_err) \
  82. do { \
  83. switch (sizeof(*__gu_ptr)) { \
  84. case 1: \
  85. __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
  86. break; \
  87. case 2: \
  88. __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
  89. break; \
  90. case 4: \
  91. __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
  92. break; \
  93. case 8: \
  94. __get_user_8((x), __gu_ptr, __gu_err); \
  95. break; \
  96. default: \
  97. BUILD_BUG(); \
  98. } \
  99. } while (0)
  100. /**
  101. * __get_user: - Get a simple variable from user space, with less checking.
  102. * @x: Variable to store result.
  103. * @ptr: Source address, in user space.
  104. *
  105. * Context: User context only. This function may sleep.
  106. *
  107. * This macro copies a single simple variable from user space to kernel
  108. * space. It supports simple types like char and int, but not larger
  109. * data types like structures or arrays.
  110. *
  111. * @ptr must have pointer-to-simple-variable type, and the result of
  112. * dereferencing @ptr must be assignable to @x without a cast.
  113. *
  114. * Caller must check the pointer with access_ok() before calling this
  115. * function.
  116. *
  117. * Returns zero on success, or -EFAULT on error.
  118. * On error, the variable @x is set to zero.
  119. */
  120. #define __get_user(x, ptr) \
  121. ({ \
  122. const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
  123. long __gu_err = 0; \
  124. \
  125. __chk_user_ptr(__gu_ptr); \
  126. \
  127. __enable_user_access(); \
  128. __get_user_nocheck(x, __gu_ptr, __gu_err); \
  129. __disable_user_access(); \
  130. \
  131. __gu_err; \
  132. })
  133. /**
  134. * get_user: - Get a simple variable from user space.
  135. * @x: Variable to store result.
  136. * @ptr: Source address, in user space.
  137. *
  138. * Context: User context only. This function may sleep.
  139. *
  140. * This macro copies a single simple variable from user space to kernel
  141. * space. It supports simple types like char and int, but not larger
  142. * data types like structures or arrays.
  143. *
  144. * @ptr must have pointer-to-simple-variable type, and the result of
  145. * dereferencing @ptr must be assignable to @x without a cast.
  146. *
  147. * Returns zero on success, or -EFAULT on error.
  148. * On error, the variable @x is set to zero.
  149. */
  150. #define get_user(x, ptr) \
  151. ({ \
  152. const __typeof__(*(ptr)) __user *__p = (ptr); \
  153. might_fault(); \
  154. access_ok(__p, sizeof(*__p)) ? \
  155. __get_user((x), __p) : \
  156. ((x) = (__force __typeof__(x))0, -EFAULT); \
  157. })
  158. #define __put_user_asm(insn, x, ptr, err) \
  159. do { \
  160. __typeof__(*(ptr)) __x = x; \
  161. __asm__ __volatile__ ( \
  162. "1:\n" \
  163. " " insn " %z2, %1\n" \
  164. "2:\n" \
  165. _ASM_EXTABLE_UACCESS_ERR(1b, 2b, %0) \
  166. : "+r" (err), "=m" (*(ptr)) \
  167. : "rJ" (__x)); \
  168. } while (0)
  169. #ifdef CONFIG_64BIT
  170. #define __put_user_8(x, ptr, err) \
  171. __put_user_asm("sd", x, ptr, err)
  172. #else /* !CONFIG_64BIT */
  173. #define __put_user_8(x, ptr, err) \
  174. do { \
  175. u32 __user *__ptr = (u32 __user *)(ptr); \
  176. u64 __x = (__typeof__((x)-(x)))(x); \
  177. __asm__ __volatile__ ( \
  178. "1:\n" \
  179. " sw %z3, %1\n" \
  180. "2:\n" \
  181. " sw %z4, %2\n" \
  182. "3:\n" \
  183. _ASM_EXTABLE_UACCESS_ERR(1b, 3b, %0) \
  184. _ASM_EXTABLE_UACCESS_ERR(2b, 3b, %0) \
  185. : "+r" (err), \
  186. "=m" (__ptr[__LSW]), \
  187. "=m" (__ptr[__MSW]) \
  188. : "rJ" (__x), "rJ" (__x >> 32)); \
  189. } while (0)
  190. #endif /* CONFIG_64BIT */
  191. #define __put_user_nocheck(x, __gu_ptr, __pu_err) \
  192. do { \
  193. switch (sizeof(*__gu_ptr)) { \
  194. case 1: \
  195. __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
  196. break; \
  197. case 2: \
  198. __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
  199. break; \
  200. case 4: \
  201. __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
  202. break; \
  203. case 8: \
  204. __put_user_8((x), __gu_ptr, __pu_err); \
  205. break; \
  206. default: \
  207. BUILD_BUG(); \
  208. } \
  209. } while (0)
  210. /**
  211. * __put_user: - Write a simple value into user space, with less checking.
  212. * @x: Value to copy to user space.
  213. * @ptr: Destination address, in user space.
  214. *
  215. * Context: User context only. This function may sleep.
  216. *
  217. * This macro copies a single simple value from kernel space to user
  218. * space. It supports simple types like char and int, but not larger
  219. * data types like structures or arrays.
  220. *
  221. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  222. * to the result of dereferencing @ptr. The value of @x is copied to avoid
  223. * re-ordering where @x is evaluated inside the block that enables user-space
  224. * access (thus bypassing user space protection if @x is a function).
  225. *
  226. * Caller must check the pointer with access_ok() before calling this
  227. * function.
  228. *
  229. * Returns zero on success, or -EFAULT on error.
  230. */
  231. #define __put_user(x, ptr) \
  232. ({ \
  233. __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
  234. __typeof__(*__gu_ptr) __val = (x); \
  235. long __pu_err = 0; \
  236. \
  237. __chk_user_ptr(__gu_ptr); \
  238. \
  239. __enable_user_access(); \
  240. __put_user_nocheck(__val, __gu_ptr, __pu_err); \
  241. __disable_user_access(); \
  242. \
  243. __pu_err; \
  244. })
  245. /**
  246. * put_user: - Write a simple value into user space.
  247. * @x: Value to copy to user space.
  248. * @ptr: Destination address, in user space.
  249. *
  250. * Context: User context only. This function may sleep.
  251. *
  252. * This macro copies a single simple value from kernel space to user
  253. * space. It supports simple types like char and int, but not larger
  254. * data types like structures or arrays.
  255. *
  256. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  257. * to the result of dereferencing @ptr.
  258. *
  259. * Returns zero on success, or -EFAULT on error.
  260. */
  261. #define put_user(x, ptr) \
  262. ({ \
  263. __typeof__(*(ptr)) __user *__p = (ptr); \
  264. might_fault(); \
  265. access_ok(__p, sizeof(*__p)) ? \
  266. __put_user((x), __p) : \
  267. -EFAULT; \
  268. })
  269. unsigned long __must_check __asm_copy_to_user(void __user *to,
  270. const void *from, unsigned long n);
  271. unsigned long __must_check __asm_copy_from_user(void *to,
  272. const void __user *from, unsigned long n);
  273. static inline unsigned long
  274. raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  275. {
  276. return __asm_copy_from_user(to, from, n);
  277. }
  278. static inline unsigned long
  279. raw_copy_to_user(void __user *to, const void *from, unsigned long n)
  280. {
  281. return __asm_copy_to_user(to, from, n);
  282. }
  283. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  284. extern long __must_check strnlen_user(const char __user *str, long n);
  285. extern
  286. unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
  287. static inline
  288. unsigned long __must_check clear_user(void __user *to, unsigned long n)
  289. {
  290. might_fault();
  291. return access_ok(to, n) ?
  292. __clear_user(to, n) : n;
  293. }
  294. #define __get_kernel_nofault(dst, src, type, err_label) \
  295. do { \
  296. long __kr_err; \
  297. \
  298. __get_user_nocheck(*((type *)(dst)), (type *)(src), __kr_err); \
  299. if (unlikely(__kr_err)) \
  300. goto err_label; \
  301. } while (0)
  302. #define __put_kernel_nofault(dst, src, type, err_label) \
  303. do { \
  304. long __kr_err; \
  305. \
  306. __put_user_nocheck(*((type *)(src)), (type *)(dst), __kr_err); \
  307. if (unlikely(__kr_err)) \
  308. goto err_label; \
  309. } while (0)
  310. #else /* CONFIG_MMU */
  311. #include <asm-generic/uaccess.h>
  312. #endif /* CONFIG_MMU */
  313. #endif /* _ASM_RISCV_UACCESS_H */