uaccess.h 8.9 KB

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