uaccess.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. *
  5. * Derived from MIPS:
  6. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle
  7. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  8. * Copyright (C) 2007 Maciej W. Rozycki
  9. * Copyright (C) 2014, Imagination Technologies Ltd.
  10. */
  11. #ifndef _ASM_UACCESS_H
  12. #define _ASM_UACCESS_H
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/extable.h>
  16. #include <asm/pgtable.h>
  17. #include <asm-generic/extable.h>
  18. #include <asm-generic/access_ok.h>
  19. extern u64 __ua_limit;
  20. #define __UA_ADDR ".dword"
  21. #define __UA_LA "la.abs"
  22. #define __UA_LIMIT __ua_limit
  23. /*
  24. * get_user: - Get a simple variable from user space.
  25. * @x: Variable to store result.
  26. * @ptr: Source address, in user space.
  27. *
  28. * Context: User context only. This function may sleep if pagefaults are
  29. * enabled.
  30. *
  31. * This macro copies a single simple variable from user space to kernel
  32. * space. It supports simple types like char and int, but not larger
  33. * data types like structures or arrays.
  34. *
  35. * @ptr must have pointer-to-simple-variable type, and the result of
  36. * dereferencing @ptr must be assignable to @x without a cast.
  37. *
  38. * Returns zero on success, or -EFAULT on error.
  39. * On error, the variable @x is set to zero.
  40. */
  41. #define get_user(x, ptr) \
  42. ({ \
  43. const __typeof__(*(ptr)) __user *__p = (ptr); \
  44. \
  45. might_fault(); \
  46. access_ok(__p, sizeof(*__p)) ? __get_user((x), __p) : \
  47. ((x) = 0, -EFAULT); \
  48. })
  49. /*
  50. * put_user: - Write a simple value into user space.
  51. * @x: Value to copy to user space.
  52. * @ptr: Destination address, in user space.
  53. *
  54. * Context: User context only. This function may sleep if pagefaults are
  55. * enabled.
  56. *
  57. * This macro copies a single simple value from kernel space to user
  58. * space. It supports simple types like char and int, but not larger
  59. * data types like structures or arrays.
  60. *
  61. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  62. * to the result of dereferencing @ptr.
  63. *
  64. * Returns zero on success, or -EFAULT on error.
  65. */
  66. #define put_user(x, ptr) \
  67. ({ \
  68. __typeof__(*(ptr)) __user *__p = (ptr); \
  69. \
  70. might_fault(); \
  71. access_ok(__p, sizeof(*__p)) ? __put_user((x), __p) : -EFAULT; \
  72. })
  73. /*
  74. * __get_user: - Get a simple variable from user space, with less checking.
  75. * @x: Variable to store result.
  76. * @ptr: Source address, in user space.
  77. *
  78. * Context: User context only. This function may sleep if pagefaults are
  79. * enabled.
  80. *
  81. * This macro copies a single simple variable from user space to kernel
  82. * space. It supports simple types like char and int, but not larger
  83. * data types like structures or arrays.
  84. *
  85. * @ptr must have pointer-to-simple-variable type, and the result of
  86. * dereferencing @ptr must be assignable to @x without a cast.
  87. *
  88. * Caller must check the pointer with access_ok() before calling this
  89. * function.
  90. *
  91. * Returns zero on success, or -EFAULT on error.
  92. * On error, the variable @x is set to zero.
  93. */
  94. #define __get_user(x, ptr) \
  95. ({ \
  96. int __gu_err = 0; \
  97. \
  98. __chk_user_ptr(ptr); \
  99. __get_user_common((x), sizeof(*(ptr)), ptr); \
  100. __gu_err; \
  101. })
  102. /*
  103. * __put_user: - Write a simple value into user space, with less checking.
  104. * @x: Value to copy to user space.
  105. * @ptr: Destination address, in user space.
  106. *
  107. * Context: User context only. This function may sleep if pagefaults are
  108. * enabled.
  109. *
  110. * This macro copies a single simple value from kernel space to user
  111. * space. It supports simple types like char and int, but not larger
  112. * data types like structures or arrays.
  113. *
  114. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  115. * to the result of dereferencing @ptr.
  116. *
  117. * Caller must check the pointer with access_ok() before calling this
  118. * function.
  119. *
  120. * Returns zero on success, or -EFAULT on error.
  121. */
  122. #define __put_user(x, ptr) \
  123. ({ \
  124. int __pu_err = 0; \
  125. __typeof__(*(ptr)) __pu_val; \
  126. \
  127. __pu_val = (x); \
  128. __chk_user_ptr(ptr); \
  129. __put_user_common(ptr, sizeof(*(ptr))); \
  130. __pu_err; \
  131. })
  132. struct __large_struct { unsigned long buf[100]; };
  133. #define __m(x) (*(struct __large_struct __user *)(x))
  134. #define __get_user_common(val, size, ptr) \
  135. do { \
  136. switch (size) { \
  137. case 1: __get_data_asm(val, "ld.b", ptr); break; \
  138. case 2: __get_data_asm(val, "ld.h", ptr); break; \
  139. case 4: __get_data_asm(val, "ld.w", ptr); break; \
  140. case 8: __get_data_asm(val, "ld.d", ptr); break; \
  141. default: BUILD_BUG(); break; \
  142. } \
  143. } while (0)
  144. #define __get_kernel_common(val, size, ptr) __get_user_common(val, size, ptr)
  145. #define __get_data_asm(val, insn, ptr) \
  146. { \
  147. long __gu_tmp; \
  148. \
  149. __asm__ __volatile__( \
  150. "1: " insn " %1, %2 \n" \
  151. "2: \n" \
  152. " .section .fixup,\"ax\" \n" \
  153. "3: li.w %0, %3 \n" \
  154. " move %1, $zero \n" \
  155. " b 2b \n" \
  156. " .previous \n" \
  157. " .section __ex_table,\"a\" \n" \
  158. " "__UA_ADDR "\t1b, 3b \n" \
  159. " .previous \n" \
  160. : "+r" (__gu_err), "=r" (__gu_tmp) \
  161. : "m" (__m(ptr)), "i" (-EFAULT)); \
  162. \
  163. (val) = (__typeof__(*(ptr))) __gu_tmp; \
  164. }
  165. #define __put_user_common(ptr, size) \
  166. do { \
  167. switch (size) { \
  168. case 1: __put_data_asm("st.b", ptr); break; \
  169. case 2: __put_data_asm("st.h", ptr); break; \
  170. case 4: __put_data_asm("st.w", ptr); break; \
  171. case 8: __put_data_asm("st.d", ptr); break; \
  172. default: BUILD_BUG(); break; \
  173. } \
  174. } while (0)
  175. #define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
  176. #define __put_data_asm(insn, ptr) \
  177. { \
  178. __asm__ __volatile__( \
  179. "1: " insn " %z2, %1 # __put_user_asm\n" \
  180. "2: \n" \
  181. " .section .fixup,\"ax\" \n" \
  182. "3: li.w %0, %3 \n" \
  183. " b 2b \n" \
  184. " .previous \n" \
  185. " .section __ex_table,\"a\" \n" \
  186. " " __UA_ADDR " 1b, 3b \n" \
  187. " .previous \n" \
  188. : "+r" (__pu_err), "=m" (__m(ptr)) \
  189. : "Jr" (__pu_val), "i" (-EFAULT)); \
  190. }
  191. #define __get_kernel_nofault(dst, src, type, err_label) \
  192. do { \
  193. int __gu_err = 0; \
  194. \
  195. __get_kernel_common(*((type *)(dst)), sizeof(type), \
  196. (__force type *)(src)); \
  197. if (unlikely(__gu_err)) \
  198. goto err_label; \
  199. } while (0)
  200. #define __put_kernel_nofault(dst, src, type, err_label) \
  201. do { \
  202. type __pu_val; \
  203. int __pu_err = 0; \
  204. \
  205. __pu_val = *(__force type *)(src); \
  206. __put_kernel_common(((type *)(dst)), sizeof(type)); \
  207. if (unlikely(__pu_err)) \
  208. goto err_label; \
  209. } while (0)
  210. extern unsigned long __copy_user(void *to, const void *from, __kernel_size_t n);
  211. static inline unsigned long __must_check
  212. raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  213. {
  214. return __copy_user(to, (__force const void *)from, n);
  215. }
  216. static inline unsigned long __must_check
  217. raw_copy_to_user(void __user *to, const void *from, unsigned long n)
  218. {
  219. return __copy_user((__force void *)to, from, n);
  220. }
  221. #define INLINE_COPY_FROM_USER
  222. #define INLINE_COPY_TO_USER
  223. /*
  224. * __clear_user: - Zero a block of memory in user space, with less checking.
  225. * @addr: Destination address, in user space.
  226. * @size: Number of bytes to zero.
  227. *
  228. * Zero a block of memory in user space. Caller must check
  229. * the specified block with access_ok() before calling this function.
  230. *
  231. * Returns number of bytes that could not be cleared.
  232. * On success, this will be zero.
  233. */
  234. extern unsigned long __clear_user(void __user *addr, __kernel_size_t size);
  235. #define clear_user(addr, n) \
  236. ({ \
  237. void __user *__cl_addr = (addr); \
  238. unsigned long __cl_size = (n); \
  239. if (__cl_size && access_ok(__cl_addr, __cl_size)) \
  240. __cl_size = __clear_user(__cl_addr, __cl_size); \
  241. __cl_size; \
  242. })
  243. extern long strncpy_from_user(char *to, const char __user *from, long n);
  244. extern long strnlen_user(const char __user *str, long n);
  245. #endif /* _ASM_UACCESS_H */