uaccess.h 9.6 KB

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