uaccess.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #ifndef _ASMANDES_UACCESS_H
  4. #define _ASMANDES_UACCESS_H
  5. /*
  6. * User space memory access functions
  7. */
  8. #include <linux/sched.h>
  9. #include <asm/errno.h>
  10. #include <asm/memory.h>
  11. #include <asm/types.h>
  12. #include <linux/mm.h>
  13. #define VERIFY_READ 0
  14. #define VERIFY_WRITE 1
  15. #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
  16. /*
  17. * The exception table consists of pairs of addresses: the first is the
  18. * address of an instruction that is allowed to fault, and the second is
  19. * the address at which the program should continue. No registers are
  20. * modified, so it is entirely up to the continuation code to figure out
  21. * what to do.
  22. *
  23. * All the routines below use bits of fixup code that are out of line
  24. * with the main instruction path. This means when everything is well,
  25. * we don't even have to jump over them. Further, they do not intrude
  26. * on our cache or tlb entries.
  27. */
  28. struct exception_table_entry {
  29. unsigned long insn, fixup;
  30. };
  31. extern int fixup_exception(struct pt_regs *regs);
  32. #define KERNEL_DS ((mm_segment_t) { ~0UL })
  33. #define USER_DS ((mm_segment_t) {TASK_SIZE - 1})
  34. #define get_ds() (KERNEL_DS)
  35. #define get_fs() (current_thread_info()->addr_limit)
  36. #define user_addr_max get_fs
  37. static inline void set_fs(mm_segment_t fs)
  38. {
  39. current_thread_info()->addr_limit = fs;
  40. }
  41. #define segment_eq(a, b) ((a) == (b))
  42. #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs() -size))
  43. #define access_ok(type, addr, size) \
  44. __range_ok((unsigned long)addr, (unsigned long)size)
  45. /*
  46. * Single-value transfer routines. They automatically use the right
  47. * size if we just have the right pointer type. Note that the functions
  48. * which read from user space (*get_*) need to take care not to leak
  49. * kernel data even if the calling code is buggy and fails to check
  50. * the return value. This means zeroing out the destination variable
  51. * or buffer on error. Normally this is done out of line by the
  52. * fixup code, but there are a few places where it intrudes on the
  53. * main code path. When we only write to user space, there is no
  54. * problem.
  55. *
  56. * The "__xxx" versions of the user access functions do not verify the
  57. * address space - it must have been done previously with a separate
  58. * "access_ok()" call.
  59. *
  60. * The "xxx_error" versions set the third argument to EFAULT if an
  61. * error occurs, and leave it unchanged on success. Note that these
  62. * versions are void (ie, don't return a value as such).
  63. */
  64. #define get_user __get_user \
  65. #define __get_user(x, ptr) \
  66. ({ \
  67. long __gu_err = 0; \
  68. __get_user_check((x), (ptr), __gu_err); \
  69. __gu_err; \
  70. })
  71. #define __get_user_error(x, ptr, err) \
  72. ({ \
  73. __get_user_check((x), (ptr), (err)); \
  74. (void)0; \
  75. })
  76. #define __get_user_check(x, ptr, err) \
  77. ({ \
  78. const __typeof__(*(ptr)) __user *__p = (ptr); \
  79. might_fault(); \
  80. if (access_ok(VERIFY_READ, __p, sizeof(*__p))) { \
  81. __get_user_err((x), __p, (err)); \
  82. } else { \
  83. (x) = 0; (err) = -EFAULT; \
  84. } \
  85. })
  86. #define __get_user_err(x, ptr, err) \
  87. do { \
  88. unsigned long __gu_val; \
  89. __chk_user_ptr(ptr); \
  90. switch (sizeof(*(ptr))) { \
  91. case 1: \
  92. __get_user_asm("lbi", __gu_val, (ptr), (err)); \
  93. break; \
  94. case 2: \
  95. __get_user_asm("lhi", __gu_val, (ptr), (err)); \
  96. break; \
  97. case 4: \
  98. __get_user_asm("lwi", __gu_val, (ptr), (err)); \
  99. break; \
  100. case 8: \
  101. __get_user_asm_dword(__gu_val, (ptr), (err)); \
  102. break; \
  103. default: \
  104. BUILD_BUG(); \
  105. break; \
  106. } \
  107. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  108. } while (0)
  109. #define __get_user_asm(inst, x, addr, err) \
  110. __asm__ __volatile__ ( \
  111. "1: "inst" %1,[%2]\n" \
  112. "2:\n" \
  113. " .section .fixup,\"ax\"\n" \
  114. " .align 2\n" \
  115. "3: move %0, %3\n" \
  116. " move %1, #0\n" \
  117. " b 2b\n" \
  118. " .previous\n" \
  119. " .section __ex_table,\"a\"\n" \
  120. " .align 3\n" \
  121. " .long 1b, 3b\n" \
  122. " .previous" \
  123. : "+r" (err), "=&r" (x) \
  124. : "r" (addr), "i" (-EFAULT) \
  125. : "cc")
  126. #ifdef __NDS32_EB__
  127. #define __gu_reg_oper0 "%H1"
  128. #define __gu_reg_oper1 "%L1"
  129. #else
  130. #define __gu_reg_oper0 "%L1"
  131. #define __gu_reg_oper1 "%H1"
  132. #endif
  133. #define __get_user_asm_dword(x, addr, err) \
  134. __asm__ __volatile__ ( \
  135. "\n1:\tlwi " __gu_reg_oper0 ",[%2]\n" \
  136. "\n2:\tlwi " __gu_reg_oper1 ",[%2+4]\n" \
  137. "3:\n" \
  138. " .section .fixup,\"ax\"\n" \
  139. " .align 2\n" \
  140. "4: move %0, %3\n" \
  141. " b 3b\n" \
  142. " .previous\n" \
  143. " .section __ex_table,\"a\"\n" \
  144. " .align 3\n" \
  145. " .long 1b, 4b\n" \
  146. " .long 2b, 4b\n" \
  147. " .previous" \
  148. : "+r"(err), "=&r"(x) \
  149. : "r"(addr), "i"(-EFAULT) \
  150. : "cc")
  151. #define put_user __put_user \
  152. #define __put_user(x, ptr) \
  153. ({ \
  154. long __pu_err = 0; \
  155. __put_user_err((x), (ptr), __pu_err); \
  156. __pu_err; \
  157. })
  158. #define __put_user_error(x, ptr, err) \
  159. ({ \
  160. __put_user_err((x), (ptr), (err)); \
  161. (void)0; \
  162. })
  163. #define __put_user_check(x, ptr, err) \
  164. ({ \
  165. __typeof__(*(ptr)) __user *__p = (ptr); \
  166. might_fault(); \
  167. if (access_ok(VERIFY_WRITE, __p, sizeof(*__p))) { \
  168. __put_user_err((x), __p, (err)); \
  169. } else { \
  170. (err) = -EFAULT; \
  171. } \
  172. })
  173. #define __put_user_err(x, ptr, err) \
  174. do { \
  175. __typeof__(*(ptr)) __pu_val = (x); \
  176. __chk_user_ptr(ptr); \
  177. switch (sizeof(*(ptr))) { \
  178. case 1: \
  179. __put_user_asm("sbi", __pu_val, (ptr), (err)); \
  180. break; \
  181. case 2: \
  182. __put_user_asm("shi", __pu_val, (ptr), (err)); \
  183. break; \
  184. case 4: \
  185. __put_user_asm("swi", __pu_val, (ptr), (err)); \
  186. break; \
  187. case 8: \
  188. __put_user_asm_dword(__pu_val, (ptr), (err)); \
  189. break; \
  190. default: \
  191. BUILD_BUG(); \
  192. break; \
  193. } \
  194. } while (0)
  195. #define __put_user_asm(inst, x, addr, err) \
  196. __asm__ __volatile__ ( \
  197. "1: "inst" %1,[%2]\n" \
  198. "2:\n" \
  199. " .section .fixup,\"ax\"\n" \
  200. " .align 2\n" \
  201. "3: move %0, %3\n" \
  202. " b 2b\n" \
  203. " .previous\n" \
  204. " .section __ex_table,\"a\"\n" \
  205. " .align 3\n" \
  206. " .long 1b, 3b\n" \
  207. " .previous" \
  208. : "+r" (err) \
  209. : "r" (x), "r" (addr), "i" (-EFAULT) \
  210. : "cc")
  211. #ifdef __NDS32_EB__
  212. #define __pu_reg_oper0 "%H2"
  213. #define __pu_reg_oper1 "%L2"
  214. #else
  215. #define __pu_reg_oper0 "%L2"
  216. #define __pu_reg_oper1 "%H2"
  217. #endif
  218. #define __put_user_asm_dword(x, addr, err) \
  219. __asm__ __volatile__ ( \
  220. "\n1:\tswi " __pu_reg_oper0 ",[%1]\n" \
  221. "\n2:\tswi " __pu_reg_oper1 ",[%1+4]\n" \
  222. "3:\n" \
  223. " .section .fixup,\"ax\"\n" \
  224. " .align 2\n" \
  225. "4: move %0, %3\n" \
  226. " b 3b\n" \
  227. " .previous\n" \
  228. " .section __ex_table,\"a\"\n" \
  229. " .align 3\n" \
  230. " .long 1b, 4b\n" \
  231. " .long 2b, 4b\n" \
  232. " .previous" \
  233. : "+r"(err) \
  234. : "r"(addr), "r"(x), "i"(-EFAULT) \
  235. : "cc")
  236. extern unsigned long __arch_clear_user(void __user * addr, unsigned long n);
  237. extern long strncpy_from_user(char *dest, const char __user * src, long count);
  238. extern __must_check long strlen_user(const char __user * str);
  239. extern __must_check long strnlen_user(const char __user * str, long n);
  240. extern unsigned long __arch_copy_from_user(void *to, const void __user * from,
  241. unsigned long n);
  242. extern unsigned long __arch_copy_to_user(void __user * to, const void *from,
  243. unsigned long n);
  244. #define raw_copy_from_user __arch_copy_from_user
  245. #define raw_copy_to_user __arch_copy_to_user
  246. #define INLINE_COPY_FROM_USER
  247. #define INLINE_COPY_TO_USER
  248. static inline unsigned long clear_user(void __user * to, unsigned long n)
  249. {
  250. if (access_ok(VERIFY_WRITE, to, n))
  251. n = __arch_clear_user(to, n);
  252. return n;
  253. }
  254. static inline unsigned long __clear_user(void __user * to, unsigned long n)
  255. {
  256. return __arch_clear_user(to, n);
  257. }
  258. #endif /* _ASMNDS32_UACCESS_H */