uaccess.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * include/asm-xtensa/uaccess.h
  3. *
  4. * User space memory access functions
  5. *
  6. * These routines provide basic accessing functions to the user memory
  7. * space for the kernel. This header file provides functions such as:
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. *
  13. * Copyright (C) 2001 - 2005 Tensilica Inc.
  14. */
  15. #ifndef _XTENSA_UACCESS_H
  16. #define _XTENSA_UACCESS_H
  17. #include <linux/prefetch.h>
  18. #include <asm/types.h>
  19. #include <asm/extable.h>
  20. #include <asm-generic/access_ok.h>
  21. /*
  22. * These are the main single-value transfer routines. They
  23. * automatically use the right size if we just have the right pointer
  24. * type.
  25. *
  26. * This gets kind of ugly. We want to return _two_ values in
  27. * "get_user()" and yet we don't want to do any pointers, because that
  28. * is too much of a performance impact. Thus we have a few rather ugly
  29. * macros here, and hide all the uglyness from the user.
  30. *
  31. * Careful to not
  32. * (a) re-use the arguments for side effects (sizeof is ok)
  33. * (b) require any knowledge of processes at this stage
  34. */
  35. #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
  36. #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
  37. /*
  38. * The "__xxx" versions of the user access functions are versions that
  39. * do not verify the address space, that must have been done previously
  40. * with a separate "access_ok()" call (this is used when we do multiple
  41. * accesses to the same area of user memory).
  42. */
  43. #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  44. #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  45. extern long __put_user_bad(void);
  46. #define __put_user_nocheck(x, ptr, size) \
  47. ({ \
  48. long __pu_err; \
  49. __put_user_size((x), (ptr), (size), __pu_err); \
  50. __pu_err; \
  51. })
  52. #define __put_user_check(x, ptr, size) \
  53. ({ \
  54. long __pu_err = -EFAULT; \
  55. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  56. if (access_ok(__pu_addr, size)) \
  57. __put_user_size((x), __pu_addr, (size), __pu_err); \
  58. __pu_err; \
  59. })
  60. #define __put_user_size(x, ptr, size, retval) \
  61. do { \
  62. int __cb; \
  63. retval = 0; \
  64. switch (size) { \
  65. case 1: __put_user_asm(x, ptr, retval, 1, "s8i", __cb); break; \
  66. case 2: __put_user_asm(x, ptr, retval, 2, "s16i", __cb); break; \
  67. case 4: __put_user_asm(x, ptr, retval, 4, "s32i", __cb); break; \
  68. case 8: { \
  69. __typeof__(*ptr) __v64 = x; \
  70. retval = __copy_to_user(ptr, &__v64, 8) ? -EFAULT : 0; \
  71. break; \
  72. } \
  73. default: __put_user_bad(); \
  74. } \
  75. } while (0)
  76. /*
  77. * Consider a case of a user single load/store would cause both an
  78. * unaligned exception and an MMU-related exception (unaligned
  79. * exceptions happen first):
  80. *
  81. * User code passes a bad variable ptr to a system call.
  82. * Kernel tries to access the variable.
  83. * Unaligned exception occurs.
  84. * Unaligned exception handler tries to make aligned accesses.
  85. * Double exception occurs for MMU-related cause (e.g., page not mapped).
  86. * do_page_fault() thinks the fault address belongs to the kernel, not the
  87. * user, and panics.
  88. *
  89. * The kernel currently prohibits user unaligned accesses. We use the
  90. * __check_align_* macros to check for unaligned addresses before
  91. * accessing user space so we don't crash the kernel. Both
  92. * __put_user_asm and __get_user_asm use these alignment macros, so
  93. * macro-specific labels such as 0f, 1f, %0, %2, and %3 must stay in
  94. * sync.
  95. */
  96. #define __check_align_1 ""
  97. #define __check_align_2 \
  98. " _bbci.l %[mem] * 0, 1f \n" \
  99. " movi %[err], %[efault] \n" \
  100. " _j 2f \n"
  101. #define __check_align_4 \
  102. " _bbsi.l %[mem] * 0, 0f \n" \
  103. " _bbci.l %[mem] * 0 + 1, 1f \n" \
  104. "0: movi %[err], %[efault] \n" \
  105. " _j 2f \n"
  106. /*
  107. * We don't tell gcc that we are accessing memory, but this is OK
  108. * because we do not write to any memory gcc knows about, so there
  109. * are no aliasing issues.
  110. *
  111. * WARNING: If you modify this macro at all, verify that the
  112. * __check_align_* macros still work.
  113. */
  114. #define __put_user_asm(x_, addr_, err_, align, insn, cb)\
  115. __asm__ __volatile__( \
  116. __check_align_##align \
  117. "1: "insn" %[x], %[mem] \n" \
  118. "2: \n" \
  119. " .section .fixup,\"ax\" \n" \
  120. " .align 4 \n" \
  121. " .literal_position \n" \
  122. "5: \n" \
  123. " movi %[tmp], 2b \n" \
  124. " movi %[err], %[efault] \n" \
  125. " jx %[tmp] \n" \
  126. " .previous \n" \
  127. " .section __ex_table,\"a\" \n" \
  128. " .long 1b, 5b \n" \
  129. " .previous" \
  130. :[err] "+r"(err_), [tmp] "=r"(cb), [mem] "=m"(*(addr_)) \
  131. :[x] "r"(x_), [efault] "i"(-EFAULT))
  132. #define __get_user_nocheck(x, ptr, size) \
  133. ({ \
  134. long __gu_err; \
  135. __get_user_size((x), (ptr), (size), __gu_err); \
  136. __gu_err; \
  137. })
  138. #define __get_user_check(x, ptr, size) \
  139. ({ \
  140. long __gu_err = -EFAULT; \
  141. const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  142. if (access_ok(__gu_addr, size)) \
  143. __get_user_size((x), __gu_addr, (size), __gu_err); \
  144. else \
  145. (x) = (__typeof__(*(ptr)))0; \
  146. __gu_err; \
  147. })
  148. extern long __get_user_bad(void);
  149. #define __get_user_size(x, ptr, size, retval) \
  150. do { \
  151. int __cb; \
  152. retval = 0; \
  153. switch (size) { \
  154. case 1: __get_user_asm(x, ptr, retval, 1, "l8ui", __cb); break;\
  155. case 2: __get_user_asm(x, ptr, retval, 2, "l16ui", __cb); break;\
  156. case 4: __get_user_asm(x, ptr, retval, 4, "l32i", __cb); break;\
  157. case 8: { \
  158. u64 __x; \
  159. if (unlikely(__copy_from_user(&__x, ptr, 8))) { \
  160. retval = -EFAULT; \
  161. (x) = (__typeof__(*(ptr)))0; \
  162. } else { \
  163. (x) = *(__force __typeof__(*(ptr)) *)&__x; \
  164. } \
  165. break; \
  166. } \
  167. default: \
  168. (x) = (__typeof__(*(ptr)))0; \
  169. __get_user_bad(); \
  170. } \
  171. } while (0)
  172. /*
  173. * WARNING: If you modify this macro at all, verify that the
  174. * __check_align_* macros still work.
  175. */
  176. #define __get_user_asm(x_, addr_, err_, align, insn, cb) \
  177. do { \
  178. u32 __x = 0; \
  179. __asm__ __volatile__( \
  180. __check_align_##align \
  181. "1: "insn" %[x], %[mem] \n" \
  182. "2: \n" \
  183. " .section .fixup,\"ax\" \n" \
  184. " .align 4 \n" \
  185. " .literal_position \n" \
  186. "5: \n" \
  187. " movi %[tmp], 2b \n" \
  188. " movi %[err], %[efault] \n" \
  189. " jx %[tmp] \n" \
  190. " .previous \n" \
  191. " .section __ex_table,\"a\" \n" \
  192. " .long 1b, 5b \n" \
  193. " .previous" \
  194. :[err] "+r"(err_), [tmp] "=r"(cb), [x] "+r"(__x) \
  195. :[mem] "m"(*(addr_)), [efault] "i"(-EFAULT)); \
  196. (x_) = (__force __typeof__(*(addr_)))__x; \
  197. } while (0)
  198. /*
  199. * Copy to/from user space
  200. */
  201. extern unsigned __xtensa_copy_user(void *to, const void *from, unsigned n);
  202. static inline unsigned long
  203. raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  204. {
  205. prefetchw(to);
  206. return __xtensa_copy_user(to, (__force const void *)from, n);
  207. }
  208. static inline unsigned long
  209. raw_copy_to_user(void __user *to, const void *from, unsigned long n)
  210. {
  211. prefetch(from);
  212. return __xtensa_copy_user((__force void *)to, from, n);
  213. }
  214. #define INLINE_COPY_FROM_USER
  215. #define INLINE_COPY_TO_USER
  216. /*
  217. * We need to return the number of bytes not cleared. Our memset()
  218. * returns zero if a problem occurs while accessing user-space memory.
  219. * In that event, return no memory cleared. Otherwise, zero for
  220. * success.
  221. */
  222. static inline unsigned long
  223. __xtensa_clear_user(void __user *addr, unsigned long size)
  224. {
  225. if (!__memset((void __force *)addr, 0, size))
  226. return size;
  227. return 0;
  228. }
  229. static inline unsigned long
  230. clear_user(void __user *addr, unsigned long size)
  231. {
  232. if (access_ok(addr, size))
  233. return __xtensa_clear_user(addr, size);
  234. return size ? -EFAULT : 0;
  235. }
  236. #define __clear_user __xtensa_clear_user
  237. #ifdef CONFIG_ARCH_HAS_STRNCPY_FROM_USER
  238. extern long __strncpy_user(char *dst, const char __user *src, long count);
  239. static inline long
  240. strncpy_from_user(char *dst, const char __user *src, long count)
  241. {
  242. if (access_ok(src, 1))
  243. return __strncpy_user(dst, src, count);
  244. return -EFAULT;
  245. }
  246. #else
  247. long strncpy_from_user(char *dst, const char __user *src, long count);
  248. #endif
  249. /*
  250. * Return the size of a string (including the ending 0!)
  251. */
  252. extern long __strnlen_user(const char __user *str, long len);
  253. static inline long strnlen_user(const char __user *str, long len)
  254. {
  255. if (!access_ok(str, 1))
  256. return 0;
  257. return __strnlen_user(str, len);
  258. }
  259. #endif /* _XTENSA_UACCESS_H */