uaccess.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Authors: Bjorn Wesen (bjornw@axis.com)
  3. * Hans-Peter Nilsson (hp@axis.com)
  4. */
  5. /* Asm:s have been tweaked (within the domain of correctness) to give
  6. satisfactory results for "gcc version 2.96 20000427 (experimental)".
  7. Check regularly...
  8. Register $r9 is chosen for temporaries, being a call-clobbered register
  9. first in line to be used (notably for local blocks), not colliding with
  10. parameter registers. */
  11. #ifndef _CRIS_UACCESS_H
  12. #define _CRIS_UACCESS_H
  13. #ifndef __ASSEMBLY__
  14. #include <linux/sched.h>
  15. #include <linux/errno.h>
  16. #include <asm/processor.h>
  17. #include <asm/page.h>
  18. #define VERIFY_READ 0
  19. #define VERIFY_WRITE 1
  20. /*
  21. * The fs value determines whether argument validity checking should be
  22. * performed or not. If get_fs() == USER_DS, checking is performed, with
  23. * get_fs() == KERNEL_DS, checking is bypassed.
  24. *
  25. * For historical reasons, these macros are grossly misnamed.
  26. */
  27. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  28. /* addr_limit is the maximum accessible address for the task. we misuse
  29. * the KERNEL_DS and USER_DS values to both assign and compare the
  30. * addr_limit values through the equally misnamed get/set_fs macros.
  31. * (see above)
  32. */
  33. #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
  34. #define USER_DS MAKE_MM_SEG(TASK_SIZE)
  35. #define get_ds() (KERNEL_DS)
  36. #define get_fs() (current_thread_info()->addr_limit)
  37. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  38. #define segment_eq(a,b) ((a).seg == (b).seg)
  39. #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
  40. #define __user_ok(addr,size) (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
  41. #define __access_ok(addr,size) (__kernel_ok || __user_ok((addr),(size)))
  42. #define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))
  43. #include <arch/uaccess.h>
  44. /*
  45. * The exception table consists of pairs of addresses: the first is the
  46. * address of an instruction that is allowed to fault, and the second is
  47. * the address at which the program should continue. No registers are
  48. * modified, so it is entirely up to the continuation code to figure out
  49. * what to do.
  50. *
  51. * All the routines below use bits of fixup code that are out of line
  52. * with the main instruction path. This means when everything is well,
  53. * we don't even have to jump over them. Further, they do not intrude
  54. * on our cache or tlb entries.
  55. */
  56. struct exception_table_entry
  57. {
  58. unsigned long insn, fixup;
  59. };
  60. /*
  61. * These are the main single-value transfer routines. They automatically
  62. * use the right size if we just have the right pointer type.
  63. *
  64. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  65. * and yet we don't want to do any pointers, because that is too much
  66. * of a performance impact. Thus we have a few rather ugly macros here,
  67. * and hide all the ugliness from the user.
  68. *
  69. * The "__xxx" versions of the user access functions are versions that
  70. * do not verify the address space, that must have been done previously
  71. * with a separate "access_ok()" call (this is used when we do multiple
  72. * accesses to the same area of user memory).
  73. *
  74. * As we use the same address space for kernel and user data on
  75. * CRIS, we can just do these as direct assignments. (Of course, the
  76. * exception handling means that it's no longer "just"...)
  77. */
  78. #define get_user(x,ptr) \
  79. __get_user_check((x),(ptr),sizeof(*(ptr)))
  80. #define put_user(x,ptr) \
  81. __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  82. #define __get_user(x,ptr) \
  83. __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
  84. #define __put_user(x,ptr) \
  85. __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  86. extern long __put_user_bad(void);
  87. #define __put_user_size(x,ptr,size,retval) \
  88. do { \
  89. retval = 0; \
  90. switch (size) { \
  91. case 1: __put_user_asm(x,ptr,retval,"move.b"); break; \
  92. case 2: __put_user_asm(x,ptr,retval,"move.w"); break; \
  93. case 4: __put_user_asm(x,ptr,retval,"move.d"); break; \
  94. case 8: __put_user_asm_64(x,ptr,retval); break; \
  95. default: __put_user_bad(); \
  96. } \
  97. } while (0)
  98. #define __get_user_size(x,ptr,size,retval) \
  99. do { \
  100. retval = 0; \
  101. switch (size) { \
  102. case 1: __get_user_asm(x,ptr,retval,"move.b"); break; \
  103. case 2: __get_user_asm(x,ptr,retval,"move.w"); break; \
  104. case 4: __get_user_asm(x,ptr,retval,"move.d"); break; \
  105. case 8: __get_user_asm_64(x,ptr,retval); break; \
  106. default: (x) = __get_user_bad(); \
  107. } \
  108. } while (0)
  109. #define __put_user_nocheck(x,ptr,size) \
  110. ({ \
  111. long __pu_err; \
  112. __put_user_size((x),(ptr),(size),__pu_err); \
  113. __pu_err; \
  114. })
  115. #define __put_user_check(x,ptr,size) \
  116. ({ \
  117. long __pu_err = -EFAULT; \
  118. __typeof__(*(ptr)) *__pu_addr = (ptr); \
  119. if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
  120. __put_user_size((x),__pu_addr,(size),__pu_err); \
  121. __pu_err; \
  122. })
  123. struct __large_struct { unsigned long buf[100]; };
  124. #define __m(x) (*(struct __large_struct *)(x))
  125. #define __get_user_nocheck(x,ptr,size) \
  126. ({ \
  127. long __gu_err, __gu_val; \
  128. __get_user_size(__gu_val,(ptr),(size),__gu_err); \
  129. (x) = (__typeof__(*(ptr)))__gu_val; \
  130. __gu_err; \
  131. })
  132. #define __get_user_check(x,ptr,size) \
  133. ({ \
  134. long __gu_err = -EFAULT, __gu_val = 0; \
  135. const __typeof__(*(ptr)) *__gu_addr = (ptr); \
  136. if (access_ok(VERIFY_READ,__gu_addr,size)) \
  137. __get_user_size(__gu_val,__gu_addr,(size),__gu_err); \
  138. (x) = (__typeof__(*(ptr)))__gu_val; \
  139. __gu_err; \
  140. })
  141. extern long __get_user_bad(void);
  142. /* More complex functions. Most are inline, but some call functions that
  143. live in lib/usercopy.c */
  144. extern unsigned long __copy_user(void __user *to, const void *from, unsigned long n);
  145. extern unsigned long __copy_user_zeroing(void *to, const void __user *from, unsigned long n);
  146. extern unsigned long __do_clear_user(void __user *to, unsigned long n);
  147. static inline unsigned long
  148. __generic_copy_to_user(void __user *to, const void *from, unsigned long n)
  149. {
  150. if (access_ok(VERIFY_WRITE, to, n))
  151. return __copy_user(to,from,n);
  152. return n;
  153. }
  154. static inline unsigned long
  155. __generic_copy_from_user(void *to, const void __user *from, unsigned long n)
  156. {
  157. if (access_ok(VERIFY_READ, from, n))
  158. return __copy_user_zeroing(to,from,n);
  159. return n;
  160. }
  161. static inline unsigned long
  162. __generic_clear_user(void __user *to, unsigned long n)
  163. {
  164. if (access_ok(VERIFY_WRITE, to, n))
  165. return __do_clear_user(to,n);
  166. return n;
  167. }
  168. static inline long
  169. __strncpy_from_user(char *dst, const char __user *src, long count)
  170. {
  171. return __do_strncpy_from_user(dst, src, count);
  172. }
  173. static inline long
  174. strncpy_from_user(char *dst, const char __user *src, long count)
  175. {
  176. long res = -EFAULT;
  177. if (access_ok(VERIFY_READ, src, 1))
  178. res = __do_strncpy_from_user(dst, src, count);
  179. return res;
  180. }
  181. /* Note that these expand awfully if made into switch constructs, so
  182. don't do that. */
  183. static inline unsigned long
  184. __constant_copy_from_user(void *to, const void __user *from, unsigned long n)
  185. {
  186. unsigned long ret = 0;
  187. if (n == 0)
  188. ;
  189. else if (n == 1)
  190. __asm_copy_from_user_1(to, from, ret);
  191. else if (n == 2)
  192. __asm_copy_from_user_2(to, from, ret);
  193. else if (n == 3)
  194. __asm_copy_from_user_3(to, from, ret);
  195. else if (n == 4)
  196. __asm_copy_from_user_4(to, from, ret);
  197. else if (n == 5)
  198. __asm_copy_from_user_5(to, from, ret);
  199. else if (n == 6)
  200. __asm_copy_from_user_6(to, from, ret);
  201. else if (n == 7)
  202. __asm_copy_from_user_7(to, from, ret);
  203. else if (n == 8)
  204. __asm_copy_from_user_8(to, from, ret);
  205. else if (n == 9)
  206. __asm_copy_from_user_9(to, from, ret);
  207. else if (n == 10)
  208. __asm_copy_from_user_10(to, from, ret);
  209. else if (n == 11)
  210. __asm_copy_from_user_11(to, from, ret);
  211. else if (n == 12)
  212. __asm_copy_from_user_12(to, from, ret);
  213. else if (n == 13)
  214. __asm_copy_from_user_13(to, from, ret);
  215. else if (n == 14)
  216. __asm_copy_from_user_14(to, from, ret);
  217. else if (n == 15)
  218. __asm_copy_from_user_15(to, from, ret);
  219. else if (n == 16)
  220. __asm_copy_from_user_16(to, from, ret);
  221. else if (n == 20)
  222. __asm_copy_from_user_20(to, from, ret);
  223. else if (n == 24)
  224. __asm_copy_from_user_24(to, from, ret);
  225. else
  226. ret = __generic_copy_from_user(to, from, n);
  227. return ret;
  228. }
  229. /* Ditto, don't make a switch out of this. */
  230. static inline unsigned long
  231. __constant_copy_to_user(void __user *to, const void *from, unsigned long n)
  232. {
  233. unsigned long ret = 0;
  234. if (n == 0)
  235. ;
  236. else if (n == 1)
  237. __asm_copy_to_user_1(to, from, ret);
  238. else if (n == 2)
  239. __asm_copy_to_user_2(to, from, ret);
  240. else if (n == 3)
  241. __asm_copy_to_user_3(to, from, ret);
  242. else if (n == 4)
  243. __asm_copy_to_user_4(to, from, ret);
  244. else if (n == 5)
  245. __asm_copy_to_user_5(to, from, ret);
  246. else if (n == 6)
  247. __asm_copy_to_user_6(to, from, ret);
  248. else if (n == 7)
  249. __asm_copy_to_user_7(to, from, ret);
  250. else if (n == 8)
  251. __asm_copy_to_user_8(to, from, ret);
  252. else if (n == 9)
  253. __asm_copy_to_user_9(to, from, ret);
  254. else if (n == 10)
  255. __asm_copy_to_user_10(to, from, ret);
  256. else if (n == 11)
  257. __asm_copy_to_user_11(to, from, ret);
  258. else if (n == 12)
  259. __asm_copy_to_user_12(to, from, ret);
  260. else if (n == 13)
  261. __asm_copy_to_user_13(to, from, ret);
  262. else if (n == 14)
  263. __asm_copy_to_user_14(to, from, ret);
  264. else if (n == 15)
  265. __asm_copy_to_user_15(to, from, ret);
  266. else if (n == 16)
  267. __asm_copy_to_user_16(to, from, ret);
  268. else if (n == 20)
  269. __asm_copy_to_user_20(to, from, ret);
  270. else if (n == 24)
  271. __asm_copy_to_user_24(to, from, ret);
  272. else
  273. ret = __generic_copy_to_user(to, from, n);
  274. return ret;
  275. }
  276. /* No switch, please. */
  277. static inline unsigned long
  278. __constant_clear_user(void __user *to, unsigned long n)
  279. {
  280. unsigned long ret = 0;
  281. if (n == 0)
  282. ;
  283. else if (n == 1)
  284. __asm_clear_1(to, ret);
  285. else if (n == 2)
  286. __asm_clear_2(to, ret);
  287. else if (n == 3)
  288. __asm_clear_3(to, ret);
  289. else if (n == 4)
  290. __asm_clear_4(to, ret);
  291. else if (n == 8)
  292. __asm_clear_8(to, ret);
  293. else if (n == 12)
  294. __asm_clear_12(to, ret);
  295. else if (n == 16)
  296. __asm_clear_16(to, ret);
  297. else if (n == 20)
  298. __asm_clear_20(to, ret);
  299. else if (n == 24)
  300. __asm_clear_24(to, ret);
  301. else
  302. ret = __generic_clear_user(to, n);
  303. return ret;
  304. }
  305. #define clear_user(to, n) \
  306. (__builtin_constant_p(n) ? \
  307. __constant_clear_user(to, n) : \
  308. __generic_clear_user(to, n))
  309. #define copy_from_user(to, from, n) \
  310. (__builtin_constant_p(n) ? \
  311. __constant_copy_from_user(to, from, n) : \
  312. __generic_copy_from_user(to, from, n))
  313. #define copy_to_user(to, from, n) \
  314. (__builtin_constant_p(n) ? \
  315. __constant_copy_to_user(to, from, n) : \
  316. __generic_copy_to_user(to, from, n))
  317. /* We let the __ versions of copy_from/to_user inline, because they're often
  318. * used in fast paths and have only a small space overhead.
  319. */
  320. static inline unsigned long
  321. __generic_copy_from_user_nocheck(void *to, const void __user *from,
  322. unsigned long n)
  323. {
  324. return __copy_user_zeroing(to,from,n);
  325. }
  326. static inline unsigned long
  327. __generic_copy_to_user_nocheck(void __user *to, const void *from,
  328. unsigned long n)
  329. {
  330. return __copy_user(to,from,n);
  331. }
  332. static inline unsigned long
  333. __generic_clear_user_nocheck(void __user *to, unsigned long n)
  334. {
  335. return __do_clear_user(to,n);
  336. }
  337. /* without checking */
  338. #define __copy_to_user(to,from,n) __generic_copy_to_user_nocheck((to),(from),(n))
  339. #define __copy_from_user(to,from,n) __generic_copy_from_user_nocheck((to),(from),(n))
  340. #define __copy_to_user_inatomic __copy_to_user
  341. #define __copy_from_user_inatomic __copy_from_user
  342. #define __clear_user(to,n) __generic_clear_user_nocheck((to),(n))
  343. #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
  344. #endif /* __ASSEMBLY__ */
  345. #endif /* _CRIS_UACCESS_H */