uaccess.h 9.7 KB

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