uaccess.h 8.8 KB

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