uaccess.h 13 KB

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