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 0,%1\n" \
  160. " mov %3,%0\n" \
  161. " jmp 2b\n" \
  162. " .previous\n" \
  163. " .section __ex_table,\"a\"\n" \
  164. " .balign 4\n" \
  165. " .long 1b, 3b\n" \
  166. " .previous" \
  167. : "=&r" (__gu_err), "=&r" (__gu_val) \
  168. : "m" (__m(__gu_addr)), "i" (-EFAULT)); \
  169. })
  170. extern int __get_user_unknown(void);
  171. #define __put_user_nocheck(x, ptr, size) \
  172. ({ \
  173. union { \
  174. __typeof__(*(ptr)) val; \
  175. u32 bits[2]; \
  176. } __pu_val; \
  177. unsigned long __pu_addr; \
  178. int __pu_err; \
  179. __pu_val.val = (x); \
  180. __pu_addr = (unsigned long) (ptr); \
  181. switch (size) { \
  182. case 1: __put_user_asm("bu"); break; \
  183. case 2: __put_user_asm("hu"); break; \
  184. case 4: __put_user_asm("" ); break; \
  185. case 8: __put_user_asm8(); break; \
  186. default: __pu_err = __put_user_unknown(); break; \
  187. } \
  188. __pu_err; \
  189. })
  190. #define __put_user_check(x, ptr, size) \
  191. ({ \
  192. union { \
  193. __typeof__(*(ptr)) val; \
  194. u32 bits[2]; \
  195. } __pu_val; \
  196. unsigned long __pu_addr; \
  197. int __pu_err; \
  198. __pu_val.val = (x); \
  199. __pu_addr = (unsigned long) (ptr); \
  200. if (likely(__access_ok(__pu_addr, size))) { \
  201. switch (size) { \
  202. case 1: __put_user_asm("bu"); break; \
  203. case 2: __put_user_asm("hu"); break; \
  204. case 4: __put_user_asm("" ); break; \
  205. case 8: __put_user_asm8(); break; \
  206. default: __pu_err = __put_user_unknown(); break; \
  207. } \
  208. } \
  209. else { \
  210. __pu_err = -EFAULT; \
  211. } \
  212. __pu_err; \
  213. })
  214. #define __put_user_asm(INSN) \
  215. ({ \
  216. asm volatile( \
  217. "1:\n" \
  218. " mov"INSN" %1,%2\n" \
  219. " mov 0,%0\n" \
  220. "2:\n" \
  221. " .section .fixup,\"ax\"\n" \
  222. "3:\n" \
  223. " mov %3,%0\n" \
  224. " jmp 2b\n" \
  225. " .previous\n" \
  226. " .section __ex_table,\"a\"\n" \
  227. " .balign 4\n" \
  228. " .long 1b, 3b\n" \
  229. " .previous" \
  230. : "=&r" (__pu_err) \
  231. : "r" (__pu_val.val), "m" (__m(__pu_addr)), \
  232. "i" (-EFAULT) \
  233. ); \
  234. })
  235. #define __put_user_asm8() \
  236. ({ \
  237. asm volatile( \
  238. "1: mov %1,%3 \n" \
  239. "2: mov %2,%4 \n" \
  240. " mov 0,%0 \n" \
  241. "3: \n" \
  242. " .section .fixup,\"ax\" \n" \
  243. "4: \n" \
  244. " mov %5,%0 \n" \
  245. " jmp 3b \n" \
  246. " .previous \n" \
  247. " .section __ex_table,\"a\"\n" \
  248. " .balign 4 \n" \
  249. " .long 1b, 4b \n" \
  250. " .long 2b, 4b \n" \
  251. " .previous \n" \
  252. : "=&r" (__pu_err) \
  253. : "r" (__pu_val.bits[0]), "r" (__pu_val.bits[1]), \
  254. "m" (__m(__pu_addr)), "m" (__m(__pu_addr+4)), \
  255. "i" (-EFAULT) \
  256. ); \
  257. })
  258. extern int __put_user_unknown(void);
  259. /*
  260. * Copy To/From Userspace
  261. */
  262. /* Generic arbitrary sized copy. */
  263. #define __copy_user(to, from, size) \
  264. do { \
  265. if (size) { \
  266. void *__to = to; \
  267. const void *__from = from; \
  268. int w; \
  269. asm volatile( \
  270. "0: movbu (%0),%3;\n" \
  271. "1: movbu %3,(%1);\n" \
  272. " inc %0;\n" \
  273. " inc %1;\n" \
  274. " add -1,%2;\n" \
  275. " bne 0b;\n" \
  276. "2:\n" \
  277. " .section .fixup,\"ax\"\n" \
  278. "3: jmp 2b\n" \
  279. " .previous\n" \
  280. " .section __ex_table,\"a\"\n" \
  281. " .balign 4\n" \
  282. " .long 0b,3b\n" \
  283. " .long 1b,3b\n" \
  284. " .previous\n" \
  285. : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
  286. : "0"(__from), "1"(__to), "2"(size) \
  287. : "cc", "memory"); \
  288. } \
  289. } while (0)
  290. #define __copy_user_zeroing(to, from, size) \
  291. do { \
  292. if (size) { \
  293. void *__to = to; \
  294. const void *__from = from; \
  295. int w; \
  296. asm volatile( \
  297. "0: movbu (%0),%3;\n" \
  298. "1: movbu %3,(%1);\n" \
  299. " inc %0;\n" \
  300. " inc %1;\n" \
  301. " add -1,%2;\n" \
  302. " bne 0b;\n" \
  303. "2:\n" \
  304. " .section .fixup,\"ax\"\n" \
  305. "3:\n" \
  306. " mov %2,%0\n" \
  307. " clr %3\n" \
  308. "4: movbu %3,(%1);\n" \
  309. " inc %1;\n" \
  310. " add -1,%2;\n" \
  311. " bne 4b;\n" \
  312. " mov %0,%2\n" \
  313. " jmp 2b\n" \
  314. " .previous\n" \
  315. " .section __ex_table,\"a\"\n" \
  316. " .balign 4\n" \
  317. " .long 0b,3b\n" \
  318. " .long 1b,3b\n" \
  319. " .previous\n" \
  320. : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
  321. : "0"(__from), "1"(__to), "2"(size) \
  322. : "cc", "memory"); \
  323. } \
  324. } while (0)
  325. /* We let the __ versions of copy_from/to_user inline, because they're often
  326. * used in fast paths and have only a small space overhead.
  327. */
  328. static inline
  329. unsigned long __generic_copy_from_user_nocheck(void *to, const void *from,
  330. unsigned long n)
  331. {
  332. __copy_user_zeroing(to, from, n);
  333. return n;
  334. }
  335. static inline
  336. unsigned long __generic_copy_to_user_nocheck(void *to, const void *from,
  337. unsigned long n)
  338. {
  339. __copy_user(to, from, n);
  340. return n;
  341. }
  342. #if 0
  343. #error "don't use - these macros don't increment to & from pointers"
  344. /* Optimize just a little bit when we know the size of the move. */
  345. #define __constant_copy_user(to, from, size) \
  346. do { \
  347. asm volatile( \
  348. " mov %0,a0;\n" \
  349. "0: movbu (%1),d3;\n" \
  350. "1: movbu d3,(%2);\n" \
  351. " add -1,a0;\n" \
  352. " bne 0b;\n" \
  353. "2:;" \
  354. ".section .fixup,\"ax\"\n" \
  355. "3: jmp 2b\n" \
  356. ".previous\n" \
  357. ".section __ex_table,\"a\"\n" \
  358. " .balign 4\n" \
  359. " .long 0b,3b\n" \
  360. " .long 1b,3b\n" \
  361. ".previous" \
  362. : \
  363. : "d"(size), "d"(to), "d"(from) \
  364. : "d3", "a0"); \
  365. } while (0)
  366. /* Optimize just a little bit when we know the size of the move. */
  367. #define __constant_copy_user_zeroing(to, from, size) \
  368. do { \
  369. asm volatile( \
  370. " mov %0,a0;\n" \
  371. "0: movbu (%1),d3;\n" \
  372. "1: movbu d3,(%2);\n" \
  373. " add -1,a0;\n" \
  374. " bne 0b;\n" \
  375. "2:;" \
  376. ".section .fixup,\"ax\"\n" \
  377. "3: jmp 2b\n" \
  378. ".previous\n" \
  379. ".section __ex_table,\"a\"\n" \
  380. " .balign 4\n" \
  381. " .long 0b,3b\n" \
  382. " .long 1b,3b\n" \
  383. ".previous" \
  384. : \
  385. : "d"(size), "d"(to), "d"(from) \
  386. : "d3", "a0"); \
  387. } while (0)
  388. static inline
  389. unsigned long __constant_copy_to_user(void *to, const void *from,
  390. unsigned long n)
  391. {
  392. if (access_ok(VERIFY_WRITE, to, n))
  393. __constant_copy_user(to, from, n);
  394. return n;
  395. }
  396. static inline
  397. unsigned long __constant_copy_from_user(void *to, const void *from,
  398. unsigned long n)
  399. {
  400. if (access_ok(VERIFY_READ, from, n))
  401. __constant_copy_user_zeroing(to, from, n);
  402. return n;
  403. }
  404. static inline
  405. unsigned long __constant_copy_to_user_nocheck(void *to, const void *from,
  406. unsigned long n)
  407. {
  408. __constant_copy_user(to, from, n);
  409. return n;
  410. }
  411. static inline
  412. unsigned long __constant_copy_from_user_nocheck(void *to, const void *from,
  413. unsigned long n)
  414. {
  415. __constant_copy_user_zeroing(to, from, n);
  416. return n;
  417. }
  418. #endif
  419. extern unsigned long __generic_copy_to_user(void __user *, const void *,
  420. unsigned long);
  421. extern unsigned long __generic_copy_from_user(void *, const void __user *,
  422. unsigned long);
  423. #define __copy_to_user_inatomic(to, from, n) \
  424. __generic_copy_to_user_nocheck((to), (from), (n))
  425. #define __copy_from_user_inatomic(to, from, n) \
  426. __generic_copy_from_user_nocheck((to), (from), (n))
  427. #define __copy_to_user(to, from, n) \
  428. ({ \
  429. might_fault(); \
  430. __copy_to_user_inatomic((to), (from), (n)); \
  431. })
  432. #define __copy_from_user(to, from, n) \
  433. ({ \
  434. might_fault(); \
  435. __copy_from_user_inatomic((to), (from), (n)); \
  436. })
  437. #define copy_to_user(to, from, n) __generic_copy_to_user((to), (from), (n))
  438. #define copy_from_user(to, from, n) __generic_copy_from_user((to), (from), (n))
  439. extern long strncpy_from_user(char *dst, const char __user *src, long count);
  440. extern long __strncpy_from_user(char *dst, const char __user *src, long count);
  441. extern long strnlen_user(const char __user *str, long n);
  442. #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
  443. extern unsigned long clear_user(void __user *mem, unsigned long len);
  444. extern unsigned long __clear_user(void __user *mem, unsigned long len);
  445. #endif /* _ASM_UACCESS_H */