uaccess.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* MN10300 userspace access functions
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #ifndef _ASM_UACCESS_H
  12. #define _ASM_UACCESS_H
  13. /*
  14. * User space memory access functions
  15. */
  16. #include <linux/thread_info.h>
  17. #include <linux/kernel.h>
  18. #include <asm/page.h>
  19. #include <asm/errno.h>
  20. #define VERIFY_READ 0
  21. #define VERIFY_WRITE 1
  22. /*
  23. * The fs value determines whether argument validity checking should be
  24. * performed or not. If get_fs() == USER_DS, checking is performed, with
  25. * get_fs() == KERNEL_DS, checking is bypassed.
  26. *
  27. * For historical reasons, these macros are grossly misnamed.
  28. */
  29. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  30. #define KERNEL_XDS MAKE_MM_SEG(0xBFFFFFFF)
  31. #define KERNEL_DS MAKE_MM_SEG(0x9FFFFFFF)
  32. #define USER_DS MAKE_MM_SEG(TASK_SIZE)
  33. #define get_ds() (KERNEL_DS)
  34. #define get_fs() (current_thread_info()->addr_limit)
  35. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  36. #define __kernel_ds_p() (current_thread_info()->addr_limit.seg == 0x9FFFFFFF)
  37. #define segment_eq(a, b) ((a).seg == (b).seg)
  38. #define __addr_ok(addr) \
  39. ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
  40. /*
  41. * check that a range of addresses falls within the current address limit
  42. */
  43. static inline int ___range_ok(unsigned long addr, unsigned int size)
  44. {
  45. int flag = 1, tmp;
  46. asm(" add %3,%1 \n" /* set C-flag if addr + size > 4Gb */
  47. " bcs 0f \n"
  48. " cmp %4,%1 \n" /* jump if addr+size>limit (error) */
  49. " bhi 0f \n"
  50. " clr %0 \n" /* mark okay */
  51. "0: \n"
  52. : "=r"(flag), "=&r"(tmp)
  53. : "1"(addr), "ir"(size),
  54. "r"(current_thread_info()->addr_limit.seg), "0"(flag)
  55. : "cc"
  56. );
  57. return flag;
  58. }
  59. #define __range_ok(addr, size) ___range_ok((unsigned long)(addr), (u32)(size))
  60. #define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
  61. #define __access_ok(addr, size) (__range_ok((addr), (size)) == 0)
  62. static inline int verify_area(int type, const void *addr, unsigned long size)
  63. {
  64. return access_ok(type, addr, size) ? 0 : -EFAULT;
  65. }
  66. /*
  67. * The exception table consists of pairs of addresses: the first is the
  68. * address of an instruction that is allowed to fault, and the second is
  69. * the address at which the program should continue. No registers are
  70. * modified, so it is entirely up to the continuation code to figure out
  71. * what to do.
  72. *
  73. * All the routines below use bits of fixup code that are out of line
  74. * with the main instruction path. This means when everything is well,
  75. * we don't even have to jump over them. Further, they do not intrude
  76. * on our cache or tlb entries.
  77. */
  78. struct exception_table_entry
  79. {
  80. unsigned long insn, fixup;
  81. };
  82. /* Returns 0 if exception not found and fixup otherwise. */
  83. extern int fixup_exception(struct pt_regs *regs);
  84. #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
  85. #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
  86. /*
  87. * The "__xxx" versions do not do address space checking, useful when
  88. * doing multiple accesses to the same area (the user has to do the
  89. * checks by hand with "access_ok()")
  90. */
  91. #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  92. #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  93. /*
  94. * The "xxx_ret" versions return constant specified in third argument, if
  95. * something bad happens. These macros can be optimized for the
  96. * case of just returning from the function xxx_ret is used.
  97. */
  98. #define put_user_ret(x, ptr, ret) \
  99. ({ if (put_user((x), (ptr))) return (ret); })
  100. #define get_user_ret(x, ptr, ret) \
  101. ({ if (get_user((x), (ptr))) return (ret); })
  102. #define __put_user_ret(x, ptr, ret) \
  103. ({ if (__put_user((x), (ptr))) return (ret); })
  104. #define __get_user_ret(x, ptr, ret) \
  105. ({ if (__get_user((x), (ptr))) return (ret); })
  106. struct __large_struct { unsigned long buf[100]; };
  107. #define __m(x) (*(struct __large_struct *)(x))
  108. #define __get_user_nocheck(x, ptr, size) \
  109. ({ \
  110. unsigned long __gu_addr; \
  111. int __gu_err; \
  112. __gu_addr = (unsigned long) (ptr); \
  113. switch (size) { \
  114. case 1: { \
  115. unsigned char __gu_val; \
  116. __get_user_asm("bu"); \
  117. (x) = *(__force __typeof__(*(ptr))*) &__gu_val; \
  118. break; \
  119. } \
  120. case 2: { \
  121. unsigned short __gu_val; \
  122. __get_user_asm("hu"); \
  123. (x) = *(__force __typeof__(*(ptr))*) &__gu_val; \
  124. break; \
  125. } \
  126. case 4: { \
  127. unsigned int __gu_val; \
  128. __get_user_asm(""); \
  129. (x) = *(__force __typeof__(*(ptr))*) &__gu_val; \
  130. break; \
  131. } \
  132. default: \
  133. __get_user_unknown(); \
  134. break; \
  135. } \
  136. __gu_err; \
  137. })
  138. #define __get_user_check(x, ptr, size) \
  139. ({ \
  140. const __typeof__(ptr) __guc_ptr = (ptr); \
  141. int _e; \
  142. if (likely(__access_ok((unsigned long) __guc_ptr, (size)))) \
  143. _e = __get_user_nocheck((x), __guc_ptr, (size)); \
  144. else { \
  145. _e = -EFAULT; \
  146. (x) = (__typeof__(x))0; \
  147. } \
  148. _e; \
  149. })
  150. #define __get_user_asm(INSN) \
  151. ({ \
  152. asm volatile( \
  153. "1:\n" \
  154. " mov"INSN" %2,%1\n" \
  155. " mov 0,%0\n" \
  156. "2:\n" \
  157. " .section .fixup,\"ax\"\n" \
  158. "3:\n\t" \
  159. " mov %3,%0\n" \
  160. " jmp 2b\n" \
  161. " .previous\n" \
  162. " .section __ex_table,\"a\"\n" \
  163. " .balign 4\n" \
  164. " .long 1b, 3b\n" \
  165. " .previous" \
  166. : "=&r" (__gu_err), "=&r" (__gu_val) \
  167. : "m" (__m(__gu_addr)), "i" (-EFAULT)); \
  168. })
  169. extern int __get_user_unknown(void);
  170. #define __put_user_nocheck(x, ptr, size) \
  171. ({ \
  172. union { \
  173. __typeof__(*(ptr)) val; \
  174. u32 bits[2]; \
  175. } __pu_val; \
  176. unsigned long __pu_addr; \
  177. int __pu_err; \
  178. __pu_val.val = (x); \
  179. __pu_addr = (unsigned long) (ptr); \
  180. switch (size) { \
  181. case 1: __put_user_asm("bu"); break; \
  182. case 2: __put_user_asm("hu"); break; \
  183. case 4: __put_user_asm("" ); break; \
  184. case 8: __put_user_asm8(); break; \
  185. default: __pu_err = __put_user_unknown(); break; \
  186. } \
  187. __pu_err; \
  188. })
  189. #define __put_user_check(x, ptr, size) \
  190. ({ \
  191. union { \
  192. __typeof__(*(ptr)) val; \
  193. u32 bits[2]; \
  194. } __pu_val; \
  195. unsigned long __pu_addr; \
  196. int __pu_err; \
  197. __pu_val.val = (x); \
  198. __pu_addr = (unsigned long) (ptr); \
  199. if (likely(__access_ok(__pu_addr, size))) { \
  200. switch (size) { \
  201. case 1: __put_user_asm("bu"); break; \
  202. case 2: __put_user_asm("hu"); break; \
  203. case 4: __put_user_asm("" ); break; \
  204. case 8: __put_user_asm8(); break; \
  205. default: __pu_err = __put_user_unknown(); break; \
  206. } \
  207. } \
  208. else { \
  209. __pu_err = -EFAULT; \
  210. } \
  211. __pu_err; \
  212. })
  213. #define __put_user_asm(INSN) \
  214. ({ \
  215. asm volatile( \
  216. "1:\n" \
  217. " mov"INSN" %1,%2\n" \
  218. " mov 0,%0\n" \
  219. "2:\n" \
  220. " .section .fixup,\"ax\"\n" \
  221. "3:\n" \
  222. " mov %3,%0\n" \
  223. " jmp 2b\n" \
  224. " .previous\n" \
  225. " .section __ex_table,\"a\"\n" \
  226. " .balign 4\n" \
  227. " .long 1b, 3b\n" \
  228. " .previous" \
  229. : "=&r" (__pu_err) \
  230. : "r" (__pu_val.val), "m" (__m(__pu_addr)), \
  231. "i" (-EFAULT) \
  232. ); \
  233. })
  234. #define __put_user_asm8() \
  235. ({ \
  236. asm volatile( \
  237. "1: mov %1,%3 \n" \
  238. "2: mov %2,%4 \n" \
  239. " mov 0,%0 \n" \
  240. "3: \n" \
  241. " .section .fixup,\"ax\" \n" \
  242. "4: \n" \
  243. " mov %5,%0 \n" \
  244. " jmp 3b \n" \
  245. " .previous \n" \
  246. " .section __ex_table,\"a\"\n" \
  247. " .balign 4 \n" \
  248. " .long 1b, 4b \n" \
  249. " .long 2b, 4b \n" \
  250. " .previous \n" \
  251. : "=&r" (__pu_err) \
  252. : "r" (__pu_val.bits[0]), "r" (__pu_val.bits[1]), \
  253. "m" (__m(__pu_addr)), "m" (__m(__pu_addr+4)), \
  254. "i" (-EFAULT) \
  255. ); \
  256. })
  257. extern int __put_user_unknown(void);
  258. /*
  259. * Copy To/From Userspace
  260. */
  261. /* Generic arbitrary sized copy. */
  262. #define __copy_user(to, from, size) \
  263. do { \
  264. if (size) { \
  265. void *__to = to; \
  266. const void *__from = from; \
  267. int w; \
  268. asm volatile( \
  269. "0: movbu (%0),%3;\n" \
  270. "1: movbu %3,(%1);\n" \
  271. " inc %0;\n" \
  272. " inc %1;\n" \
  273. " add -1,%2;\n" \
  274. " bne 0b;\n" \
  275. "2:\n" \
  276. " .section .fixup,\"ax\"\n" \
  277. "3: jmp 2b\n" \
  278. " .previous\n" \
  279. " .section __ex_table,\"a\"\n" \
  280. " .balign 4\n" \
  281. " .long 0b,3b\n" \
  282. " .long 1b,3b\n" \
  283. " .previous\n" \
  284. : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
  285. : "0"(__from), "1"(__to), "2"(size) \
  286. : "cc", "memory"); \
  287. } \
  288. } while (0)
  289. #define __copy_user_zeroing(to, from, size) \
  290. do { \
  291. if (size) { \
  292. void *__to = to; \
  293. const void *__from = from; \
  294. int w; \
  295. asm volatile( \
  296. "0: movbu (%0),%3;\n" \
  297. "1: movbu %3,(%1);\n" \
  298. " inc %0;\n" \
  299. " inc %1;\n" \
  300. " add -1,%2;\n" \
  301. " bne 0b;\n" \
  302. "2:\n" \
  303. " .section .fixup,\"ax\"\n" \
  304. "3:\n" \
  305. " mov %2,%0\n" \
  306. " clr %3\n" \
  307. "4: movbu %3,(%1);\n" \
  308. " inc %1;\n" \
  309. " add -1,%2;\n" \
  310. " bne 4b;\n" \
  311. " mov %0,%2\n" \
  312. " jmp 2b\n" \
  313. " .previous\n" \
  314. " .section __ex_table,\"a\"\n" \
  315. " .balign 4\n" \
  316. " .long 0b,3b\n" \
  317. " .long 1b,3b\n" \
  318. " .previous\n" \
  319. : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
  320. : "0"(__from), "1"(__to), "2"(size) \
  321. : "cc", "memory"); \
  322. } \
  323. } while (0)
  324. /* We let the __ versions of copy_from/to_user inline, because they're often
  325. * used in fast paths and have only a small space overhead.
  326. */
  327. static inline
  328. unsigned long __generic_copy_from_user_nocheck(void *to, const void *from,
  329. unsigned long n)
  330. {
  331. __copy_user_zeroing(to, from, n);
  332. return n;
  333. }
  334. static inline
  335. unsigned long __generic_copy_to_user_nocheck(void *to, const void *from,
  336. unsigned long n)
  337. {
  338. __copy_user(to, from, n);
  339. return n;
  340. }
  341. #if 0
  342. #error "don't use - these macros don't increment to & from pointers"
  343. /* Optimize just a little bit when we know the size of the move. */
  344. #define __constant_copy_user(to, from, size) \
  345. do { \
  346. asm volatile( \
  347. " mov %0,a0;\n" \
  348. "0: movbu (%1),d3;\n" \
  349. "1: movbu d3,(%2);\n" \
  350. " add -1,a0;\n" \
  351. " bne 0b;\n" \
  352. "2:;" \
  353. ".section .fixup,\"ax\"\n" \
  354. "3: jmp 2b\n" \
  355. ".previous\n" \
  356. ".section __ex_table,\"a\"\n" \
  357. " .balign 4\n" \
  358. " .long 0b,3b\n" \
  359. " .long 1b,3b\n" \
  360. ".previous" \
  361. : \
  362. : "d"(size), "d"(to), "d"(from) \
  363. : "d3", "a0"); \
  364. } while (0)
  365. /* Optimize just a little bit when we know the size of the move. */
  366. #define __constant_copy_user_zeroing(to, from, size) \
  367. do { \
  368. asm volatile( \
  369. " mov %0,a0;\n" \
  370. "0: movbu (%1),d3;\n" \
  371. "1: movbu d3,(%2);\n" \
  372. " add -1,a0;\n" \
  373. " bne 0b;\n" \
  374. "2:;" \
  375. ".section .fixup,\"ax\"\n" \
  376. "3: jmp 2b\n" \
  377. ".previous\n" \
  378. ".section __ex_table,\"a\"\n" \
  379. " .balign 4\n" \
  380. " .long 0b,3b\n" \
  381. " .long 1b,3b\n" \
  382. ".previous" \
  383. : \
  384. : "d"(size), "d"(to), "d"(from) \
  385. : "d3", "a0"); \
  386. } while (0)
  387. static inline
  388. unsigned long __constant_copy_to_user(void *to, const void *from,
  389. unsigned long n)
  390. {
  391. if (access_ok(VERIFY_WRITE, to, n))
  392. __constant_copy_user(to, from, n);
  393. return n;
  394. }
  395. static inline
  396. unsigned long __constant_copy_from_user(void *to, const void *from,
  397. unsigned long n)
  398. {
  399. if (access_ok(VERIFY_READ, from, n))
  400. __constant_copy_user_zeroing(to, from, n);
  401. return n;
  402. }
  403. static inline
  404. unsigned long __constant_copy_to_user_nocheck(void *to, const void *from,
  405. unsigned long n)
  406. {
  407. __constant_copy_user(to, from, n);
  408. return n;
  409. }
  410. static inline
  411. unsigned long __constant_copy_from_user_nocheck(void *to, const void *from,
  412. unsigned long n)
  413. {
  414. __constant_copy_user_zeroing(to, from, n);
  415. return n;
  416. }
  417. #endif
  418. extern unsigned long __generic_copy_to_user(void __user *, const void *,
  419. unsigned long);
  420. extern unsigned long __generic_copy_from_user(void *, const void __user *,
  421. unsigned long);
  422. #define __copy_to_user_inatomic(to, from, n) \
  423. __generic_copy_to_user_nocheck((to), (from), (n))
  424. #define __copy_from_user_inatomic(to, from, n) \
  425. __generic_copy_from_user_nocheck((to), (from), (n))
  426. #define __copy_to_user(to, from, n) \
  427. ({ \
  428. might_sleep(); \
  429. __copy_to_user_inatomic((to), (from), (n)); \
  430. })
  431. #define __copy_from_user(to, from, n) \
  432. ({ \
  433. might_sleep(); \
  434. __copy_from_user_inatomic((to), (from), (n)); \
  435. })
  436. #define copy_to_user(to, from, n) __generic_copy_to_user((to), (from), (n))
  437. #define copy_from_user(to, from, n) __generic_copy_from_user((to), (from), (n))
  438. extern long strncpy_from_user(char *dst, const char __user *src, long count);
  439. extern long __strncpy_from_user(char *dst, const char __user *src, long count);
  440. extern long strnlen_user(const char __user *str, long n);
  441. #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
  442. extern unsigned long clear_user(void __user *mem, unsigned long len);
  443. extern unsigned long __clear_user(void __user *mem, unsigned long len);
  444. #endif /* _ASM_UACCESS_H */