uaccess.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef __ASM_AVR32_UACCESS_H
  9. #define __ASM_AVR32_UACCESS_H
  10. #include <linux/errno.h>
  11. #include <linux/sched.h>
  12. #define VERIFY_READ 0
  13. #define VERIFY_WRITE 1
  14. typedef struct {
  15. unsigned int is_user_space;
  16. } mm_segment_t;
  17. /*
  18. * The fs value determines whether argument validity checking should be
  19. * performed or not. If get_fs() == USER_DS, checking is performed, with
  20. * get_fs() == KERNEL_DS, checking is bypassed.
  21. *
  22. * For historical reasons (Data Segment Register?), these macros are misnamed.
  23. */
  24. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  25. #define segment_eq(a,b) ((a).is_user_space == (b).is_user_space)
  26. #define USER_ADDR_LIMIT 0x80000000
  27. #define KERNEL_DS MAKE_MM_SEG(0)
  28. #define USER_DS MAKE_MM_SEG(1)
  29. #define get_ds() (KERNEL_DS)
  30. static inline mm_segment_t get_fs(void)
  31. {
  32. return MAKE_MM_SEG(test_thread_flag(TIF_USERSPACE));
  33. }
  34. static inline void set_fs(mm_segment_t s)
  35. {
  36. if (s.is_user_space)
  37. set_thread_flag(TIF_USERSPACE);
  38. else
  39. clear_thread_flag(TIF_USERSPACE);
  40. }
  41. /*
  42. * Test whether a block of memory is a valid user space address.
  43. * Returns 0 if the range is valid, nonzero otherwise.
  44. *
  45. * We do the following checks:
  46. * 1. Is the access from kernel space?
  47. * 2. Does (addr + size) set the carry bit?
  48. * 3. Is (addr + size) a negative number (i.e. >= 0x80000000)?
  49. *
  50. * If yes on the first check, access is granted.
  51. * If no on any of the others, access is denied.
  52. */
  53. #define __range_ok(addr, size) \
  54. (test_thread_flag(TIF_USERSPACE) \
  55. && (((unsigned long)(addr) >= 0x80000000) \
  56. || ((unsigned long)(size) > 0x80000000) \
  57. || (((unsigned long)(addr) + (unsigned long)(size)) > 0x80000000)))
  58. #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
  59. /* Generic arbitrary sized copy. Return the number of bytes NOT copied */
  60. extern __kernel_size_t __copy_user(void *to, const void *from,
  61. __kernel_size_t n);
  62. extern __kernel_size_t copy_to_user(void __user *to, const void *from,
  63. __kernel_size_t n);
  64. extern __kernel_size_t copy_from_user(void *to, const void __user *from,
  65. __kernel_size_t n);
  66. static inline __kernel_size_t __copy_to_user(void __user *to, const void *from,
  67. __kernel_size_t n)
  68. {
  69. return __copy_user((void __force *)to, from, n);
  70. }
  71. static inline __kernel_size_t __copy_from_user(void *to,
  72. const void __user *from,
  73. __kernel_size_t n)
  74. {
  75. return __copy_user(to, (const void __force *)from, n);
  76. }
  77. #define __copy_to_user_inatomic __copy_to_user
  78. #define __copy_from_user_inatomic __copy_from_user
  79. /*
  80. * put_user: - Write a simple value into user space.
  81. * @x: Value to copy to user space.
  82. * @ptr: Destination address, in user space.
  83. *
  84. * Context: User context only. This function may sleep.
  85. *
  86. * This macro copies a single simple value from kernel space to user
  87. * space. It supports simple types like char and int, but not larger
  88. * data types like structures or arrays.
  89. *
  90. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  91. * to the result of dereferencing @ptr.
  92. *
  93. * Returns zero on success, or -EFAULT on error.
  94. */
  95. #define put_user(x,ptr) \
  96. __put_user_check((x),(ptr),sizeof(*(ptr)))
  97. /*
  98. * get_user: - Get a simple variable from user space.
  99. * @x: Variable to store result.
  100. * @ptr: Source address, in user space.
  101. *
  102. * Context: User context only. This function may sleep.
  103. *
  104. * This macro copies a single simple variable from user space to kernel
  105. * space. It supports simple types like char and int, but not larger
  106. * data types like structures or arrays.
  107. *
  108. * @ptr must have pointer-to-simple-variable type, and the result of
  109. * dereferencing @ptr must be assignable to @x without a cast.
  110. *
  111. * Returns zero on success, or -EFAULT on error.
  112. * On error, the variable @x is set to zero.
  113. */
  114. #define get_user(x,ptr) \
  115. __get_user_check((x),(ptr),sizeof(*(ptr)))
  116. /*
  117. * __put_user: - Write a simple value into user space, with less checking.
  118. * @x: Value to copy to user space.
  119. * @ptr: Destination address, in user space.
  120. *
  121. * Context: User context only. This function may sleep.
  122. *
  123. * This macro copies a single simple value from kernel space to user
  124. * space. It supports simple types like char and int, but not larger
  125. * data types like structures or arrays.
  126. *
  127. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  128. * to the result of dereferencing @ptr.
  129. *
  130. * Caller must check the pointer with access_ok() before calling this
  131. * function.
  132. *
  133. * Returns zero on success, or -EFAULT on error.
  134. */
  135. #define __put_user(x,ptr) \
  136. __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
  137. /*
  138. * __get_user: - Get a simple variable from user space, with less checking.
  139. * @x: Variable to store result.
  140. * @ptr: Source address, in user space.
  141. *
  142. * Context: User context only. This function may sleep.
  143. *
  144. * This macro copies a single simple variable from user space to kernel
  145. * space. It supports simple types like char and int, but not larger
  146. * data types like structures or arrays.
  147. *
  148. * @ptr must have pointer-to-simple-variable type, and the result of
  149. * dereferencing @ptr must be assignable to @x without a cast.
  150. *
  151. * Caller must check the pointer with access_ok() before calling this
  152. * function.
  153. *
  154. * Returns zero on success, or -EFAULT on error.
  155. * On error, the variable @x is set to zero.
  156. */
  157. #define __get_user(x,ptr) \
  158. __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
  159. extern int __get_user_bad(void);
  160. extern int __put_user_bad(void);
  161. #define __get_user_nocheck(x, ptr, size) \
  162. ({ \
  163. unsigned long __gu_val = 0; \
  164. int __gu_err = 0; \
  165. \
  166. switch (size) { \
  167. case 1: __get_user_asm("ub", __gu_val, ptr, __gu_err); break; \
  168. case 2: __get_user_asm("uh", __gu_val, ptr, __gu_err); break; \
  169. case 4: __get_user_asm("w", __gu_val, ptr, __gu_err); break; \
  170. default: __gu_err = __get_user_bad(); break; \
  171. } \
  172. \
  173. x = (typeof(*(ptr)))__gu_val; \
  174. __gu_err; \
  175. })
  176. #define __get_user_check(x, ptr, size) \
  177. ({ \
  178. unsigned long __gu_val = 0; \
  179. const typeof(*(ptr)) __user * __gu_addr = (ptr); \
  180. int __gu_err = 0; \
  181. \
  182. if (access_ok(VERIFY_READ, __gu_addr, size)) { \
  183. switch (size) { \
  184. case 1: \
  185. __get_user_asm("ub", __gu_val, __gu_addr, \
  186. __gu_err); \
  187. break; \
  188. case 2: \
  189. __get_user_asm("uh", __gu_val, __gu_addr, \
  190. __gu_err); \
  191. break; \
  192. case 4: \
  193. __get_user_asm("w", __gu_val, __gu_addr, \
  194. __gu_err); \
  195. break; \
  196. default: \
  197. __gu_err = __get_user_bad(); \
  198. break; \
  199. } \
  200. } else { \
  201. __gu_err = -EFAULT; \
  202. } \
  203. x = (typeof(*(ptr)))__gu_val; \
  204. __gu_err; \
  205. })
  206. #define __get_user_asm(suffix, __gu_val, ptr, __gu_err) \
  207. asm volatile( \
  208. "1: ld." suffix " %1, %3 \n" \
  209. "2: \n" \
  210. " .section .fixup, \"ax\" \n" \
  211. "3: mov %0, %4 \n" \
  212. " rjmp 2b \n" \
  213. " .previous \n" \
  214. " .section __ex_table, \"a\" \n" \
  215. " .long 1b, 3b \n" \
  216. " .previous \n" \
  217. : "=r"(__gu_err), "=r"(__gu_val) \
  218. : "0"(__gu_err), "m"(*(ptr)), "i"(-EFAULT))
  219. #define __put_user_nocheck(x, ptr, size) \
  220. ({ \
  221. typeof(*(ptr)) __pu_val; \
  222. int __pu_err = 0; \
  223. \
  224. __pu_val = (x); \
  225. switch (size) { \
  226. case 1: __put_user_asm("b", ptr, __pu_val, __pu_err); break; \
  227. case 2: __put_user_asm("h", ptr, __pu_val, __pu_err); break; \
  228. case 4: __put_user_asm("w", ptr, __pu_val, __pu_err); break; \
  229. case 8: __put_user_asm("d", ptr, __pu_val, __pu_err); break; \
  230. default: __pu_err = __put_user_bad(); break; \
  231. } \
  232. __pu_err; \
  233. })
  234. #define __put_user_check(x, ptr, size) \
  235. ({ \
  236. typeof(*(ptr)) __pu_val; \
  237. typeof(*(ptr)) __user *__pu_addr = (ptr); \
  238. int __pu_err = 0; \
  239. \
  240. __pu_val = (x); \
  241. if (access_ok(VERIFY_WRITE, __pu_addr, size)) { \
  242. switch (size) { \
  243. case 1: \
  244. __put_user_asm("b", __pu_addr, __pu_val, \
  245. __pu_err); \
  246. break; \
  247. case 2: \
  248. __put_user_asm("h", __pu_addr, __pu_val, \
  249. __pu_err); \
  250. break; \
  251. case 4: \
  252. __put_user_asm("w", __pu_addr, __pu_val, \
  253. __pu_err); \
  254. break; \
  255. case 8: \
  256. __put_user_asm("d", __pu_addr, __pu_val, \
  257. __pu_err); \
  258. break; \
  259. default: \
  260. __pu_err = __put_user_bad(); \
  261. break; \
  262. } \
  263. } else { \
  264. __pu_err = -EFAULT; \
  265. } \
  266. __pu_err; \
  267. })
  268. #define __put_user_asm(suffix, ptr, __pu_val, __gu_err) \
  269. asm volatile( \
  270. "1: st." suffix " %1, %3 \n" \
  271. "2: \n" \
  272. " .section .fixup, \"ax\" \n" \
  273. "3: mov %0, %4 \n" \
  274. " rjmp 2b \n" \
  275. " .previous \n" \
  276. " .section __ex_table, \"a\" \n" \
  277. " .long 1b, 3b \n" \
  278. " .previous \n" \
  279. : "=r"(__gu_err), "=m"(*(ptr)) \
  280. : "0"(__gu_err), "r"(__pu_val), "i"(-EFAULT))
  281. extern __kernel_size_t clear_user(void __user *addr, __kernel_size_t size);
  282. extern __kernel_size_t __clear_user(void __user *addr, __kernel_size_t size);
  283. extern long strncpy_from_user(char *dst, const char __user *src, long count);
  284. extern long __strncpy_from_user(char *dst, const char __user *src, long count);
  285. extern long strnlen_user(const char __user *__s, long __n);
  286. extern long __strnlen_user(const char __user *__s, long __n);
  287. #define strlen_user(s) strnlen_user(s, ~0UL >> 1)
  288. struct exception_table_entry
  289. {
  290. unsigned long insn, fixup;
  291. };
  292. #endif /* __ASM_AVR32_UACCESS_H */