uaccess.h 10 KB

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