uaccess_mm.h 10 KB

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