uaccess.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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(x,p) \
  65. ({ \
  66. long __e = -EFAULT; \
  67. if(likely(access_ok(VERIFY_READ, p, sizeof(*p)))) { \
  68. __e = __get_user(x,p); \
  69. } else \
  70. x = 0; \
  71. __e; \
  72. })
  73. #define __get_user(x,ptr) \
  74. ({ \
  75. long __gu_err = 0; \
  76. __get_user_err((x),(ptr),__gu_err); \
  77. __gu_err; \
  78. })
  79. #define __get_user_error(x,ptr,err) \
  80. ({ \
  81. __get_user_err((x),(ptr),err); \
  82. (void) 0; \
  83. })
  84. #define __get_user_err(x,ptr,err) \
  85. do { \
  86. unsigned long __gu_addr = (unsigned long)(ptr); \
  87. unsigned long __gu_val; \
  88. __chk_user_ptr(ptr); \
  89. switch (sizeof(*(ptr))) { \
  90. case 1: \
  91. __get_user_asm("lbi",__gu_val,__gu_addr,err); \
  92. break; \
  93. case 2: \
  94. __get_user_asm("lhi",__gu_val,__gu_addr,err); \
  95. break; \
  96. case 4: \
  97. __get_user_asm("lwi",__gu_val,__gu_addr,err); \
  98. break; \
  99. case 8: \
  100. __get_user_asm_dword(__gu_val,__gu_addr,err); \
  101. break; \
  102. default: \
  103. BUILD_BUG(); \
  104. break; \
  105. } \
  106. (x) = (__typeof__(*(ptr)))__gu_val; \
  107. } while (0)
  108. #define __get_user_asm(inst,x,addr,err) \
  109. asm volatile( \
  110. "1: "inst" %1,[%2]\n" \
  111. "2:\n" \
  112. " .section .fixup,\"ax\"\n" \
  113. " .align 2\n" \
  114. "3: move %0, %3\n" \
  115. " move %1, #0\n" \
  116. " b 2b\n" \
  117. " .previous\n" \
  118. " .section __ex_table,\"a\"\n" \
  119. " .align 3\n" \
  120. " .long 1b, 3b\n" \
  121. " .previous" \
  122. : "+r" (err), "=&r" (x) \
  123. : "r" (addr), "i" (-EFAULT) \
  124. : "cc")
  125. #ifdef __NDS32_EB__
  126. #define __gu_reg_oper0 "%H1"
  127. #define __gu_reg_oper1 "%L1"
  128. #else
  129. #define __gu_reg_oper0 "%L1"
  130. #define __gu_reg_oper1 "%H1"
  131. #endif
  132. #define __get_user_asm_dword(x, addr, err) \
  133. asm volatile( \
  134. "\n1:\tlwi " __gu_reg_oper0 ",[%2]\n" \
  135. "\n2:\tlwi " __gu_reg_oper1 ",[%2+4]\n" \
  136. "3:\n" \
  137. " .section .fixup,\"ax\"\n" \
  138. " .align 2\n" \
  139. "4: move %0, %3\n" \
  140. " b 3b\n" \
  141. " .previous\n" \
  142. " .section __ex_table,\"a\"\n" \
  143. " .align 3\n" \
  144. " .long 1b, 4b\n" \
  145. " .long 2b, 4b\n" \
  146. " .previous" \
  147. : "+r"(err), "=&r"(x) \
  148. : "r"(addr), "i"(-EFAULT) \
  149. : "cc")
  150. #define put_user(x,p) \
  151. ({ \
  152. long __e = -EFAULT; \
  153. if(likely(access_ok(VERIFY_WRITE, p, sizeof(*p)))) { \
  154. __e = __put_user(x,p); \
  155. } \
  156. __e; \
  157. })
  158. #define __put_user(x,ptr) \
  159. ({ \
  160. long __pu_err = 0; \
  161. __put_user_err((x),(ptr),__pu_err); \
  162. __pu_err; \
  163. })
  164. #define __put_user_error(x,ptr,err) \
  165. ({ \
  166. __put_user_err((x),(ptr),err); \
  167. (void) 0; \
  168. })
  169. #define __put_user_err(x,ptr,err) \
  170. do { \
  171. unsigned long __pu_addr = (unsigned long)(ptr); \
  172. __typeof__(*(ptr)) __pu_val = (x); \
  173. __chk_user_ptr(ptr); \
  174. switch (sizeof(*(ptr))) { \
  175. case 1: \
  176. __put_user_asm("sbi",__pu_val,__pu_addr,err); \
  177. break; \
  178. case 2: \
  179. __put_user_asm("shi",__pu_val,__pu_addr,err); \
  180. break; \
  181. case 4: \
  182. __put_user_asm("swi",__pu_val,__pu_addr,err); \
  183. break; \
  184. case 8: \
  185. __put_user_asm_dword(__pu_val,__pu_addr,err); \
  186. break; \
  187. default: \
  188. BUILD_BUG(); \
  189. break; \
  190. } \
  191. } while (0)
  192. #define __put_user_asm(inst,x,addr,err) \
  193. asm volatile( \
  194. "1: "inst" %1,[%2]\n" \
  195. "2:\n" \
  196. " .section .fixup,\"ax\"\n" \
  197. " .align 2\n" \
  198. "3: move %0, %3\n" \
  199. " b 2b\n" \
  200. " .previous\n" \
  201. " .section __ex_table,\"a\"\n" \
  202. " .align 3\n" \
  203. " .long 1b, 3b\n" \
  204. " .previous" \
  205. : "+r" (err) \
  206. : "r" (x), "r" (addr), "i" (-EFAULT) \
  207. : "cc")
  208. #ifdef __NDS32_EB__
  209. #define __pu_reg_oper0 "%H2"
  210. #define __pu_reg_oper1 "%L2"
  211. #else
  212. #define __pu_reg_oper0 "%L2"
  213. #define __pu_reg_oper1 "%H2"
  214. #endif
  215. #define __put_user_asm_dword(x, addr, err) \
  216. asm volatile( \
  217. "\n1:\tswi " __pu_reg_oper0 ",[%1]\n" \
  218. "\n2:\tswi " __pu_reg_oper1 ",[%1+4]\n" \
  219. "3:\n" \
  220. " .section .fixup,\"ax\"\n" \
  221. " .align 2\n" \
  222. "4: move %0, %3\n" \
  223. " b 3b\n" \
  224. " .previous\n" \
  225. " .section __ex_table,\"a\"\n" \
  226. " .align 3\n" \
  227. " .long 1b, 4b\n" \
  228. " .long 2b, 4b\n" \
  229. " .previous" \
  230. : "+r"(err) \
  231. : "r"(addr), "r"(x), "i"(-EFAULT) \
  232. : "cc")
  233. extern unsigned long __arch_clear_user(void __user * addr, unsigned long n);
  234. extern long strncpy_from_user(char *dest, const char __user * src, long count);
  235. extern __must_check long strlen_user(const char __user * str);
  236. extern __must_check long strnlen_user(const char __user * str, long n);
  237. extern unsigned long __arch_copy_from_user(void *to, const void __user * from,
  238. unsigned long n);
  239. extern unsigned long __arch_copy_to_user(void __user * to, const void *from,
  240. unsigned long n);
  241. #define raw_copy_from_user __arch_copy_from_user
  242. #define raw_copy_to_user __arch_copy_to_user
  243. #define INLINE_COPY_FROM_USER
  244. #define INLINE_COPY_TO_USER
  245. static inline unsigned long clear_user(void __user * to, unsigned long n)
  246. {
  247. if (access_ok(VERIFY_WRITE, to, n))
  248. n = __arch_clear_user(to, n);
  249. return n;
  250. }
  251. static inline unsigned long __clear_user(void __user * to, unsigned long n)
  252. {
  253. return __arch_clear_user(to, n);
  254. }
  255. #endif /* _ASMNDS32_UACCESS_H */