uaccess.h 13 KB

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