uaccess_mm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #ifndef __M68K_UACCESS_H
  2. #define __M68K_UACCESS_H
  3. /*
  4. * User space memory access functions
  5. */
  6. #include <linux/compiler.h>
  7. #include <linux/errno.h>
  8. #include <linux/types.h>
  9. #include <linux/sched.h>
  10. #include <asm/segment.h>
  11. #define VERIFY_READ 0
  12. #define VERIFY_WRITE 1
  13. /* We let the MMU do all checking */
  14. static inline int access_ok(int type, const void __user *addr,
  15. unsigned long size)
  16. {
  17. return 1;
  18. }
  19. /*
  20. * Not all varients of the 68k family support the notion of address spaces.
  21. * The traditional 680x0 parts do, and they use the sfc/dfc registers and
  22. * the "moves" instruction to access user space from kernel space. Other
  23. * family members like ColdFire don't support this, and only have a single
  24. * address space, and use the usual "move" instruction for user space access.
  25. *
  26. * Outside of this difference the user space access functions are the same.
  27. * So lets keep the code simple and just define in what we need to use.
  28. */
  29. #ifdef CONFIG_CPU_HAS_ADDRESS_SPACES
  30. #define MOVES "moves"
  31. #else
  32. #define MOVES "move"
  33. #endif
  34. /*
  35. * The exception table consists of pairs of addresses: the first is the
  36. * address of an instruction that is allowed to fault, and the second is
  37. * the address at which the program should continue. No registers are
  38. * modified, so it is entirely up to the continuation code to figure out
  39. * what to do.
  40. *
  41. * All the routines below use bits of fixup code that are out of line
  42. * with the main instruction path. This means when everything is well,
  43. * we don't even have to jump over them. Further, they do not intrude
  44. * on our cache or tlb entries.
  45. */
  46. struct exception_table_entry
  47. {
  48. unsigned long insn, fixup;
  49. };
  50. extern int __put_user_bad(void);
  51. extern int __get_user_bad(void);
  52. #define __put_user_asm(res, x, ptr, bwl, reg, err) \
  53. asm volatile ("\n" \
  54. "1: "MOVES"."#bwl" %2,%1\n" \
  55. "2:\n" \
  56. " .section .fixup,\"ax\"\n" \
  57. " .even\n" \
  58. "10: moveq.l %3,%0\n" \
  59. " jra 2b\n" \
  60. " .previous\n" \
  61. "\n" \
  62. " .section __ex_table,\"a\"\n" \
  63. " .align 4\n" \
  64. " .long 1b,10b\n" \
  65. " .long 2b,10b\n" \
  66. " .previous" \
  67. : "+d" (res), "=m" (*(ptr)) \
  68. : #reg (x), "i" (err))
  69. /*
  70. * These are the main single-value transfer routines. They automatically
  71. * use the right size if we just have the right pointer type.
  72. */
  73. #define __put_user(x, ptr) \
  74. ({ \
  75. typeof(*(ptr)) __pu_val = (x); \
  76. int __pu_err = 0; \
  77. __chk_user_ptr(ptr); \
  78. switch (sizeof (*(ptr))) { \
  79. case 1: \
  80. __put_user_asm(__pu_err, __pu_val, ptr, b, d, -EFAULT); \
  81. break; \
  82. case 2: \
  83. __put_user_asm(__pu_err, __pu_val, ptr, w, r, -EFAULT); \
  84. break; \
  85. case 4: \
  86. __put_user_asm(__pu_err, __pu_val, ptr, l, r, -EFAULT); \
  87. break; \
  88. case 8: \
  89. { \
  90. const void __user *__pu_ptr = (ptr); \
  91. asm volatile ("\n" \
  92. "1: "MOVES".l %2,(%1)+\n" \
  93. "2: "MOVES".l %R2,(%1)\n" \
  94. "3:\n" \
  95. " .section .fixup,\"ax\"\n" \
  96. " .even\n" \
  97. "10: movel %3,%0\n" \
  98. " jra 3b\n" \
  99. " .previous\n" \
  100. "\n" \
  101. " .section __ex_table,\"a\"\n" \
  102. " .align 4\n" \
  103. " .long 1b,10b\n" \
  104. " .long 2b,10b\n" \
  105. " .long 3b,10b\n" \
  106. " .previous" \
  107. : "+d" (__pu_err), "+a" (__pu_ptr) \
  108. : "r" (__pu_val), "i" (-EFAULT) \
  109. : "memory"); \
  110. break; \
  111. } \
  112. default: \
  113. __pu_err = __put_user_bad(); \
  114. break; \
  115. } \
  116. __pu_err; \
  117. })
  118. #define put_user(x, ptr) __put_user(x, ptr)
  119. #define __get_user_asm(res, x, ptr, type, bwl, reg, err) ({ \
  120. type __gu_val; \
  121. asm volatile ("\n" \
  122. "1: "MOVES"."#bwl" %2,%1\n" \
  123. "2:\n" \
  124. " .section .fixup,\"ax\"\n" \
  125. " .even\n" \
  126. "10: move.l %3,%0\n" \
  127. " sub.l %1,%1\n" \
  128. " jra 2b\n" \
  129. " .previous\n" \
  130. "\n" \
  131. " .section __ex_table,\"a\"\n" \
  132. " .align 4\n" \
  133. " .long 1b,10b\n" \
  134. " .previous" \
  135. : "+d" (res), "=&" #reg (__gu_val) \
  136. : "m" (*(ptr)), "i" (err)); \
  137. (x) = (__force typeof(*(ptr)))(__force unsigned long)__gu_val; \
  138. })
  139. #define __get_user(x, ptr) \
  140. ({ \
  141. int __gu_err = 0; \
  142. __chk_user_ptr(ptr); \
  143. switch (sizeof(*(ptr))) { \
  144. case 1: \
  145. __get_user_asm(__gu_err, x, ptr, u8, b, d, -EFAULT); \
  146. break; \
  147. case 2: \
  148. __get_user_asm(__gu_err, x, ptr, u16, w, r, -EFAULT); \
  149. break; \
  150. case 4: \
  151. __get_user_asm(__gu_err, x, ptr, u32, l, r, -EFAULT); \
  152. break; \
  153. /* case 8: disabled because gcc-4.1 has a broken typeof \
  154. { \
  155. const void *__gu_ptr = (ptr); \
  156. u64 __gu_val; \
  157. asm volatile ("\n" \
  158. "1: "MOVES".l (%2)+,%1\n" \
  159. "2: "MOVES".l (%2),%R1\n" \
  160. "3:\n" \
  161. " .section .fixup,\"ax\"\n" \
  162. " .even\n" \
  163. "10: move.l %3,%0\n" \
  164. " sub.l %1,%1\n" \
  165. " sub.l %R1,%R1\n" \
  166. " jra 3b\n" \
  167. " .previous\n" \
  168. "\n" \
  169. " .section __ex_table,\"a\"\n" \
  170. " .align 4\n" \
  171. " .long 1b,10b\n" \
  172. " .long 2b,10b\n" \
  173. " .previous" \
  174. : "+d" (__gu_err), "=&r" (__gu_val), \
  175. "+a" (__gu_ptr) \
  176. : "i" (-EFAULT) \
  177. : "memory"); \
  178. (x) = (__force typeof(*(ptr)))__gu_val; \
  179. break; \
  180. } */ \
  181. default: \
  182. __gu_err = __get_user_bad(); \
  183. break; \
  184. } \
  185. __gu_err; \
  186. })
  187. #define get_user(x, ptr) __get_user(x, ptr)
  188. unsigned long __generic_copy_from_user(void *to, const void __user *from, unsigned long n);
  189. unsigned long __generic_copy_to_user(void __user *to, const void *from, unsigned long n);
  190. #define __constant_copy_from_user_asm(res, to, from, tmp, n, s1, s2, s3)\
  191. asm volatile ("\n" \
  192. "1: "MOVES"."#s1" (%2)+,%3\n" \
  193. " move."#s1" %3,(%1)+\n" \
  194. "2: "MOVES"."#s2" (%2)+,%3\n" \
  195. " move."#s2" %3,(%1)+\n" \
  196. " .ifnc \""#s3"\",\"\"\n" \
  197. "3: "MOVES"."#s3" (%2)+,%3\n" \
  198. " move."#s3" %3,(%1)+\n" \
  199. " .endif\n" \
  200. "4:\n" \
  201. " .section __ex_table,\"a\"\n" \
  202. " .align 4\n" \
  203. " .long 1b,10f\n" \
  204. " .long 2b,20f\n" \
  205. " .ifnc \""#s3"\",\"\"\n" \
  206. " .long 3b,30f\n" \
  207. " .endif\n" \
  208. " .previous\n" \
  209. "\n" \
  210. " .section .fixup,\"ax\"\n" \
  211. " .even\n" \
  212. "10: clr."#s1" (%1)+\n" \
  213. "20: clr."#s2" (%1)+\n" \
  214. " .ifnc \""#s3"\",\"\"\n" \
  215. "30: clr."#s3" (%1)+\n" \
  216. " .endif\n" \
  217. " moveq.l #"#n",%0\n" \
  218. " jra 4b\n" \
  219. " .previous\n" \
  220. : "+d" (res), "+&a" (to), "+a" (from), "=&d" (tmp) \
  221. : : "memory")
  222. static __always_inline unsigned long
  223. __constant_copy_from_user(void *to, const void __user *from, unsigned long n)
  224. {
  225. unsigned long res = 0, tmp;
  226. switch (n) {
  227. case 1:
  228. __get_user_asm(res, *(u8 *)to, (u8 __user *)from, u8, b, d, 1);
  229. break;
  230. case 2:
  231. __get_user_asm(res, *(u16 *)to, (u16 __user *)from, u16, w, r, 2);
  232. break;
  233. case 3:
  234. __constant_copy_from_user_asm(res, to, from, tmp, 3, w, b,);
  235. break;
  236. case 4:
  237. __get_user_asm(res, *(u32 *)to, (u32 __user *)from, u32, l, r, 4);
  238. break;
  239. case 5:
  240. __constant_copy_from_user_asm(res, to, from, tmp, 5, l, b,);
  241. break;
  242. case 6:
  243. __constant_copy_from_user_asm(res, to, from, tmp, 6, l, w,);
  244. break;
  245. case 7:
  246. __constant_copy_from_user_asm(res, to, from, tmp, 7, l, w, b);
  247. break;
  248. case 8:
  249. __constant_copy_from_user_asm(res, to, from, tmp, 8, l, l,);
  250. break;
  251. case 9:
  252. __constant_copy_from_user_asm(res, to, from, tmp, 9, l, l, b);
  253. break;
  254. case 10:
  255. __constant_copy_from_user_asm(res, to, from, tmp, 10, l, l, w);
  256. break;
  257. case 12:
  258. __constant_copy_from_user_asm(res, to, from, tmp, 12, l, l, l);
  259. break;
  260. default:
  261. /* we limit the inlined version to 3 moves */
  262. return __generic_copy_from_user(to, from, n);
  263. }
  264. return res;
  265. }
  266. #define __constant_copy_to_user_asm(res, to, from, tmp, n, s1, s2, s3) \
  267. asm volatile ("\n" \
  268. " move."#s1" (%2)+,%3\n" \
  269. "11: "MOVES"."#s1" %3,(%1)+\n" \
  270. "12: move."#s2" (%2)+,%3\n" \
  271. "21: "MOVES"."#s2" %3,(%1)+\n" \
  272. "22:\n" \
  273. " .ifnc \""#s3"\",\"\"\n" \
  274. " move."#s3" (%2)+,%3\n" \
  275. "31: "MOVES"."#s3" %3,(%1)+\n" \
  276. "32:\n" \
  277. " .endif\n" \
  278. "4:\n" \
  279. "\n" \
  280. " .section __ex_table,\"a\"\n" \
  281. " .align 4\n" \
  282. " .long 11b,5f\n" \
  283. " .long 12b,5f\n" \
  284. " .long 21b,5f\n" \
  285. " .long 22b,5f\n" \
  286. " .ifnc \""#s3"\",\"\"\n" \
  287. " .long 31b,5f\n" \
  288. " .long 32b,5f\n" \
  289. " .endif\n" \
  290. " .previous\n" \
  291. "\n" \
  292. " .section .fixup,\"ax\"\n" \
  293. " .even\n" \
  294. "5: moveq.l #"#n",%0\n" \
  295. " jra 4b\n" \
  296. " .previous\n" \
  297. : "+d" (res), "+a" (to), "+a" (from), "=&d" (tmp) \
  298. : : "memory")
  299. static __always_inline unsigned long
  300. __constant_copy_to_user(void __user *to, const void *from, unsigned long n)
  301. {
  302. unsigned long res = 0, tmp;
  303. switch (n) {
  304. case 1:
  305. __put_user_asm(res, *(u8 *)from, (u8 __user *)to, b, d, 1);
  306. break;
  307. case 2:
  308. __put_user_asm(res, *(u16 *)from, (u16 __user *)to, w, r, 2);
  309. break;
  310. case 3:
  311. __constant_copy_to_user_asm(res, to, from, tmp, 3, w, b,);
  312. break;
  313. case 4:
  314. __put_user_asm(res, *(u32 *)from, (u32 __user *)to, l, r, 4);
  315. break;
  316. case 5:
  317. __constant_copy_to_user_asm(res, to, from, tmp, 5, l, b,);
  318. break;
  319. case 6:
  320. __constant_copy_to_user_asm(res, to, from, tmp, 6, l, w,);
  321. break;
  322. case 7:
  323. __constant_copy_to_user_asm(res, to, from, tmp, 7, l, w, b);
  324. break;
  325. case 8:
  326. __constant_copy_to_user_asm(res, to, from, tmp, 8, l, l,);
  327. break;
  328. case 9:
  329. __constant_copy_to_user_asm(res, to, from, tmp, 9, l, l, b);
  330. break;
  331. case 10:
  332. __constant_copy_to_user_asm(res, to, from, tmp, 10, l, l, w);
  333. break;
  334. case 12:
  335. __constant_copy_to_user_asm(res, to, from, tmp, 12, l, l, l);
  336. break;
  337. default:
  338. /* limit the inlined version to 3 moves */
  339. return __generic_copy_to_user(to, from, n);
  340. }
  341. return res;
  342. }
  343. #define __copy_from_user(to, from, n) \
  344. (__builtin_constant_p(n) ? \
  345. __constant_copy_from_user(to, from, n) : \
  346. __generic_copy_from_user(to, from, n))
  347. #define __copy_to_user(to, from, n) \
  348. (__builtin_constant_p(n) ? \
  349. __constant_copy_to_user(to, from, n) : \
  350. __generic_copy_to_user(to, from, n))
  351. #define __copy_to_user_inatomic __copy_to_user
  352. #define __copy_from_user_inatomic __copy_from_user
  353. #define copy_from_user(to, from, n) __copy_from_user(to, from, n)
  354. #define copy_to_user(to, from, n) __copy_to_user(to, from, n)
  355. #define user_addr_max() \
  356. (segment_eq(get_fs(), USER_DS) ? TASK_SIZE : ~0UL)
  357. extern long strncpy_from_user(char *dst, const char __user *src, long count);
  358. extern __must_check long strlen_user(const char __user *str);
  359. extern __must_check long strnlen_user(const char __user *str, long n);
  360. unsigned long __clear_user(void __user *to, unsigned long n);
  361. #define clear_user __clear_user
  362. #endif /* _M68K_UACCESS_H */