uaccess.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/extable.h>
  18. #include <asm/asm-extable.h>
  19. #include <asm-generic/access_ok.h>
  20. extern u64 __ua_limit;
  21. #define __UA_ADDR ".dword"
  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. _ASM_EXTABLE_UACCESS_ERR_ZERO(1b, 2b, %0, %1) \
  153. : "+r" (__gu_err), "=r" (__gu_tmp) \
  154. : "m" (__m(ptr))); \
  155. \
  156. (val) = (__typeof__(*(ptr))) __gu_tmp; \
  157. }
  158. #define __put_user_common(ptr, size) \
  159. do { \
  160. switch (size) { \
  161. case 1: __put_data_asm("st.b", ptr); break; \
  162. case 2: __put_data_asm("st.h", ptr); break; \
  163. case 4: __put_data_asm("st.w", ptr); break; \
  164. case 8: __put_data_asm("st.d", ptr); break; \
  165. default: BUILD_BUG(); break; \
  166. } \
  167. } while (0)
  168. #define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
  169. #define __put_data_asm(insn, ptr) \
  170. { \
  171. __asm__ __volatile__( \
  172. "1: " insn " %z2, %1 # __put_user_asm\n" \
  173. "2: \n" \
  174. _ASM_EXTABLE_UACCESS_ERR(1b, 2b, %0) \
  175. : "+r" (__pu_err), "=m" (__m(ptr)) \
  176. : "Jr" (__pu_val)); \
  177. }
  178. #define __get_kernel_nofault(dst, src, type, err_label) \
  179. do { \
  180. int __gu_err = 0; \
  181. \
  182. __get_kernel_common(*((type *)(dst)), sizeof(type), \
  183. (__force type *)(src)); \
  184. if (unlikely(__gu_err)) \
  185. goto err_label; \
  186. } while (0)
  187. #define __put_kernel_nofault(dst, src, type, err_label) \
  188. do { \
  189. type __pu_val; \
  190. int __pu_err = 0; \
  191. \
  192. __pu_val = *(__force type *)(src); \
  193. __put_kernel_common(((type *)(dst)), sizeof(type)); \
  194. if (unlikely(__pu_err)) \
  195. goto err_label; \
  196. } while (0)
  197. extern unsigned long __copy_user(void *to, const void *from, __kernel_size_t n);
  198. static inline unsigned long __must_check
  199. raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  200. {
  201. return __copy_user(to, (__force const void *)from, n);
  202. }
  203. static inline unsigned long __must_check
  204. raw_copy_to_user(void __user *to, const void *from, unsigned long n)
  205. {
  206. return __copy_user((__force void *)to, from, n);
  207. }
  208. #define INLINE_COPY_FROM_USER
  209. #define INLINE_COPY_TO_USER
  210. /*
  211. * __clear_user: - Zero a block of memory in user space, with less checking.
  212. * @addr: Destination address, in user space.
  213. * @size: Number of bytes to zero.
  214. *
  215. * Zero a block of memory in user space. Caller must check
  216. * the specified block with access_ok() before calling this function.
  217. *
  218. * Returns number of bytes that could not be cleared.
  219. * On success, this will be zero.
  220. */
  221. extern unsigned long __clear_user(void __user *addr, __kernel_size_t size);
  222. #define clear_user(addr, n) \
  223. ({ \
  224. void __user *__cl_addr = (addr); \
  225. unsigned long __cl_size = (n); \
  226. if (__cl_size && access_ok(__cl_addr, __cl_size)) \
  227. __cl_size = __clear_user(__cl_addr, __cl_size); \
  228. __cl_size; \
  229. })
  230. extern long strncpy_from_user(char *to, const char __user *from, long n);
  231. extern long strnlen_user(const char __user *str, long n);
  232. #endif /* _ASM_UACCESS_H */