uaccess.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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) \
  41. (((size) <= TASK_SIZE) && ((addr) <= TASK_SIZE-(size)))
  42. #define __access_ok(addr, size) (__kernel_ok || __user_ok((addr), (size)))
  43. #define access_ok(type, addr, size) __access_ok((unsigned long)(addr), (size))
  44. #include <arch/uaccess.h>
  45. /*
  46. * The exception table consists of pairs of addresses: the first is the
  47. * address of an instruction that is allowed to fault, and the second is
  48. * the address at which the program should continue. No registers are
  49. * modified, so it is entirely up to the continuation code to figure out
  50. * what to do.
  51. *
  52. * All the routines below use bits of fixup code that are out of line
  53. * with the main instruction path. This means when everything is well,
  54. * we don't even have to jump over them. Further, they do not intrude
  55. * on our cache or tlb entries.
  56. */
  57. struct exception_table_entry {
  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: \
  92. __put_user_asm(x, ptr, retval, "move.b"); \
  93. break; \
  94. case 2: \
  95. __put_user_asm(x, ptr, retval, "move.w"); \
  96. break; \
  97. case 4: \
  98. __put_user_asm(x, ptr, retval, "move.d"); \
  99. break; \
  100. case 8: \
  101. __put_user_asm_64(x, ptr, retval); \
  102. break; \
  103. default: \
  104. __put_user_bad(); \
  105. } \
  106. } while (0)
  107. #define __get_user_size(x, ptr, size, retval) \
  108. do { \
  109. retval = 0; \
  110. switch (size) { \
  111. case 1: \
  112. __get_user_asm(x, ptr, retval, "move.b"); \
  113. break; \
  114. case 2: \
  115. __get_user_asm(x, ptr, retval, "move.w"); \
  116. break; \
  117. case 4: \
  118. __get_user_asm(x, ptr, retval, "move.d"); \
  119. break; \
  120. case 8: \
  121. __get_user_asm_64(x, ptr, retval); \
  122. break; \
  123. default: \
  124. (x) = __get_user_bad(); \
  125. } \
  126. } while (0)
  127. #define __put_user_nocheck(x, ptr, size) \
  128. ({ \
  129. long __pu_err; \
  130. __put_user_size((x), (ptr), (size), __pu_err); \
  131. __pu_err; \
  132. })
  133. #define __put_user_check(x, ptr, size) \
  134. ({ \
  135. long __pu_err = -EFAULT; \
  136. __typeof__(*(ptr)) *__pu_addr = (ptr); \
  137. if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
  138. __put_user_size((x), __pu_addr, (size), __pu_err); \
  139. __pu_err; \
  140. })
  141. struct __large_struct { unsigned long buf[100]; };
  142. #define __m(x) (*(struct __large_struct *)(x))
  143. #define __get_user_nocheck(x, ptr, size) \
  144. ({ \
  145. long __gu_err, __gu_val; \
  146. __get_user_size(__gu_val, (ptr), (size), __gu_err); \
  147. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  148. __gu_err; \
  149. })
  150. #define __get_user_check(x, ptr, size) \
  151. ({ \
  152. long __gu_err = -EFAULT, __gu_val = 0; \
  153. const __typeof__(*(ptr)) *__gu_addr = (ptr); \
  154. if (access_ok(VERIFY_READ, __gu_addr, size)) \
  155. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  156. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  157. __gu_err; \
  158. })
  159. extern long __get_user_bad(void);
  160. /* More complex functions. Most are inline, but some call functions that
  161. live in lib/usercopy.c */
  162. extern unsigned long __copy_user(void __user *to, const void *from, unsigned long n);
  163. extern unsigned long __copy_user_zeroing(void *to, const void __user *from, unsigned long n);
  164. extern unsigned long __do_clear_user(void __user *to, unsigned long n);
  165. static inline long
  166. __strncpy_from_user(char *dst, const char __user *src, long count)
  167. {
  168. return __do_strncpy_from_user(dst, src, count);
  169. }
  170. static inline long
  171. strncpy_from_user(char *dst, const char __user *src, long count)
  172. {
  173. long res = -EFAULT;
  174. if (access_ok(VERIFY_READ, src, 1))
  175. res = __do_strncpy_from_user(dst, src, count);
  176. return res;
  177. }
  178. /* Note that these expand awfully if made into switch constructs, so
  179. don't do that. */
  180. static inline unsigned long
  181. __constant_copy_from_user(void *to, const void __user *from, unsigned long n)
  182. {
  183. unsigned long ret = 0;
  184. if (n == 0)
  185. ;
  186. else if (n == 1)
  187. __asm_copy_from_user_1(to, from, ret);
  188. else if (n == 2)
  189. __asm_copy_from_user_2(to, from, ret);
  190. else if (n == 3)
  191. __asm_copy_from_user_3(to, from, ret);
  192. else if (n == 4)
  193. __asm_copy_from_user_4(to, from, ret);
  194. else if (n == 5)
  195. __asm_copy_from_user_5(to, from, ret);
  196. else if (n == 6)
  197. __asm_copy_from_user_6(to, from, ret);
  198. else if (n == 7)
  199. __asm_copy_from_user_7(to, from, ret);
  200. else if (n == 8)
  201. __asm_copy_from_user_8(to, from, ret);
  202. else if (n == 9)
  203. __asm_copy_from_user_9(to, from, ret);
  204. else if (n == 10)
  205. __asm_copy_from_user_10(to, from, ret);
  206. else if (n == 11)
  207. __asm_copy_from_user_11(to, from, ret);
  208. else if (n == 12)
  209. __asm_copy_from_user_12(to, from, ret);
  210. else if (n == 13)
  211. __asm_copy_from_user_13(to, from, ret);
  212. else if (n == 14)
  213. __asm_copy_from_user_14(to, from, ret);
  214. else if (n == 15)
  215. __asm_copy_from_user_15(to, from, ret);
  216. else if (n == 16)
  217. __asm_copy_from_user_16(to, from, ret);
  218. else if (n == 20)
  219. __asm_copy_from_user_20(to, from, ret);
  220. else if (n == 24)
  221. __asm_copy_from_user_24(to, from, ret);
  222. else
  223. ret = __copy_user_zeroing(to, from, n);
  224. return ret;
  225. }
  226. /* Ditto, don't make a switch out of this. */
  227. static inline unsigned long
  228. __constant_copy_to_user(void __user *to, const void *from, unsigned long n)
  229. {
  230. unsigned long ret = 0;
  231. if (n == 0)
  232. ;
  233. else if (n == 1)
  234. __asm_copy_to_user_1(to, from, ret);
  235. else if (n == 2)
  236. __asm_copy_to_user_2(to, from, ret);
  237. else if (n == 3)
  238. __asm_copy_to_user_3(to, from, ret);
  239. else if (n == 4)
  240. __asm_copy_to_user_4(to, from, ret);
  241. else if (n == 5)
  242. __asm_copy_to_user_5(to, from, ret);
  243. else if (n == 6)
  244. __asm_copy_to_user_6(to, from, ret);
  245. else if (n == 7)
  246. __asm_copy_to_user_7(to, from, ret);
  247. else if (n == 8)
  248. __asm_copy_to_user_8(to, from, ret);
  249. else if (n == 9)
  250. __asm_copy_to_user_9(to, from, ret);
  251. else if (n == 10)
  252. __asm_copy_to_user_10(to, from, ret);
  253. else if (n == 11)
  254. __asm_copy_to_user_11(to, from, ret);
  255. else if (n == 12)
  256. __asm_copy_to_user_12(to, from, ret);
  257. else if (n == 13)
  258. __asm_copy_to_user_13(to, from, ret);
  259. else if (n == 14)
  260. __asm_copy_to_user_14(to, from, ret);
  261. else if (n == 15)
  262. __asm_copy_to_user_15(to, from, ret);
  263. else if (n == 16)
  264. __asm_copy_to_user_16(to, from, ret);
  265. else if (n == 20)
  266. __asm_copy_to_user_20(to, from, ret);
  267. else if (n == 24)
  268. __asm_copy_to_user_24(to, from, ret);
  269. else
  270. ret = __copy_user(to, from, n);
  271. return ret;
  272. }
  273. /* No switch, please. */
  274. static inline unsigned long
  275. __constant_clear_user(void __user *to, unsigned long n)
  276. {
  277. unsigned long ret = 0;
  278. if (n == 0)
  279. ;
  280. else if (n == 1)
  281. __asm_clear_1(to, ret);
  282. else if (n == 2)
  283. __asm_clear_2(to, ret);
  284. else if (n == 3)
  285. __asm_clear_3(to, ret);
  286. else if (n == 4)
  287. __asm_clear_4(to, ret);
  288. else if (n == 8)
  289. __asm_clear_8(to, ret);
  290. else if (n == 12)
  291. __asm_clear_12(to, ret);
  292. else if (n == 16)
  293. __asm_clear_16(to, ret);
  294. else if (n == 20)
  295. __asm_clear_20(to, ret);
  296. else if (n == 24)
  297. __asm_clear_24(to, ret);
  298. else
  299. ret = __do_clear_user(to, n);
  300. return ret;
  301. }
  302. static inline size_t clear_user(void __user *to, size_t n)
  303. {
  304. if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
  305. return n;
  306. if (__builtin_constant_p(n))
  307. return __constant_clear_user(to, n);
  308. else
  309. return __do_clear_user(to, n);
  310. }
  311. static inline size_t copy_from_user(void *to, const void __user *from, size_t n)
  312. {
  313. if (unlikely(!access_ok(VERIFY_READ, from, n))) {
  314. memset(to, 0, n);
  315. return n;
  316. }
  317. if (__builtin_constant_p(n))
  318. return __constant_copy_from_user(to, from, n);
  319. else
  320. return __copy_user_zeroing(to, from, n);
  321. }
  322. static inline size_t copy_to_user(void __user *to, const void *from, size_t n)
  323. {
  324. if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
  325. return n;
  326. if (__builtin_constant_p(n))
  327. return __constant_copy_to_user(to, from, n);
  328. else
  329. return __copy_user(to, from, n);
  330. }
  331. /* We let the __ versions of copy_from/to_user inline, because they're often
  332. * used in fast paths and have only a small space overhead.
  333. */
  334. static inline unsigned long
  335. __generic_copy_from_user_nocheck(void *to, const void __user *from,
  336. unsigned long n)
  337. {
  338. return __copy_user_zeroing(to, from, n);
  339. }
  340. static inline unsigned long
  341. __generic_copy_to_user_nocheck(void __user *to, const void *from,
  342. unsigned long n)
  343. {
  344. return __copy_user(to, from, n);
  345. }
  346. static inline unsigned long
  347. __generic_clear_user_nocheck(void __user *to, unsigned long n)
  348. {
  349. return __do_clear_user(to, n);
  350. }
  351. /* without checking */
  352. #define __copy_to_user(to, from, n) \
  353. __generic_copy_to_user_nocheck((to), (from), (n))
  354. #define __copy_from_user(to, from, n) \
  355. __generic_copy_from_user_nocheck((to), (from), (n))
  356. #define __copy_to_user_inatomic __copy_to_user
  357. #define __copy_from_user_inatomic __copy_from_user
  358. #define __clear_user(to, n) __generic_clear_user_nocheck((to), (n))
  359. #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
  360. #endif /* __ASSEMBLY__ */
  361. #endif /* _CRIS_UACCESS_H */