uaccess.h 10.0 KB

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