/arch/mn10300/include/asm/uaccess.h

http://github.com/mirrors/linux · C++ Header · 480 lines · 381 code · 49 blank · 50 comment · 19 complexity · 8d2d47bacb359dc643613b4e5e272030 MD5 · raw file

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