blob: ef856ffeffdfca99ca091c0206717b4ef8da1e7a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __M68K_UACCESS_H
2#define __M68K_UACCESS_H
3
4/*
5 * User space memory access functions
6 */
Roman Zippeld94af932006-06-23 02:05:00 -07007#include <linux/compiler.h>
Roman Zippeld94af932006-06-23 02:05:00 -07008#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/segment.h>
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011/* We let the MMU do all checking */
Geert Uytterhoeven70f9cac2008-05-18 20:47:07 +020012static inline int access_ok(int type, const void __user *addr,
13 unsigned long size)
14{
15 return 1;
16}
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
Greg Ungerere08d7032011-10-14 14:43:30 +100019 * Not all varients of the 68k family support the notion of address spaces.
20 * The traditional 680x0 parts do, and they use the sfc/dfc registers and
21 * the "moves" instruction to access user space from kernel space. Other
22 * family members like ColdFire don't support this, and only have a single
23 * address space, and use the usual "move" instruction for user space access.
24 *
25 * Outside of this difference the user space access functions are the same.
26 * So lets keep the code simple and just define in what we need to use.
27 */
28#ifdef CONFIG_CPU_HAS_ADDRESS_SPACES
29#define MOVES "moves"
30#else
31#define MOVES "move"
32#endif
33
Roman Zippeld94af932006-06-23 02:05:00 -070034extern int __put_user_bad(void);
35extern int __get_user_bad(void);
36
37#define __put_user_asm(res, x, ptr, bwl, reg, err) \
38asm volatile ("\n" \
Greg Ungerere08d7032011-10-14 14:43:30 +100039 "1: "MOVES"."#bwl" %2,%1\n" \
Roman Zippeld94af932006-06-23 02:05:00 -070040 "2:\n" \
41 " .section .fixup,\"ax\"\n" \
42 " .even\n" \
43 "10: moveq.l %3,%0\n" \
44 " jra 2b\n" \
45 " .previous\n" \
46 "\n" \
47 " .section __ex_table,\"a\"\n" \
48 " .align 4\n" \
49 " .long 1b,10b\n" \
50 " .long 2b,10b\n" \
51 " .previous" \
52 : "+d" (res), "=m" (*(ptr)) \
53 : #reg (x), "i" (err))
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * These are the main single-value transfer routines. They automatically
57 * use the right size if we just have the right pointer type.
58 */
59
Roman Zippeld94af932006-06-23 02:05:00 -070060#define __put_user(x, ptr) \
61({ \
62 typeof(*(ptr)) __pu_val = (x); \
63 int __pu_err = 0; \
64 __chk_user_ptr(ptr); \
65 switch (sizeof (*(ptr))) { \
66 case 1: \
67 __put_user_asm(__pu_err, __pu_val, ptr, b, d, -EFAULT); \
68 break; \
69 case 2: \
Geert Uytterhoeven631d8b62013-06-09 20:12:42 +020070 __put_user_asm(__pu_err, __pu_val, ptr, w, r, -EFAULT); \
Roman Zippeld94af932006-06-23 02:05:00 -070071 break; \
72 case 4: \
73 __put_user_asm(__pu_err, __pu_val, ptr, l, r, -EFAULT); \
74 break; \
75 case 8: \
76 { \
Al Virob9710182006-10-11 17:27:57 +010077 const void __user *__pu_ptr = (ptr); \
Roman Zippeld94af932006-06-23 02:05:00 -070078 asm volatile ("\n" \
Greg Ungerere08d7032011-10-14 14:43:30 +100079 "1: "MOVES".l %2,(%1)+\n" \
80 "2: "MOVES".l %R2,(%1)\n" \
Roman Zippeld94af932006-06-23 02:05:00 -070081 "3:\n" \
82 " .section .fixup,\"ax\"\n" \
83 " .even\n" \
84 "10: movel %3,%0\n" \
85 " jra 3b\n" \
86 " .previous\n" \
87 "\n" \
88 " .section __ex_table,\"a\"\n" \
89 " .align 4\n" \
90 " .long 1b,10b\n" \
91 " .long 2b,10b\n" \
92 " .long 3b,10b\n" \
93 " .previous" \
94 : "+d" (__pu_err), "+a" (__pu_ptr) \
95 : "r" (__pu_val), "i" (-EFAULT) \
96 : "memory"); \
97 break; \
98 } \
99 default: \
100 __pu_err = __put_user_bad(); \
101 break; \
102 } \
103 __pu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104})
Roman Zippeld94af932006-06-23 02:05:00 -0700105#define put_user(x, ptr) __put_user(x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Michael S. Tsirkin09a2f7c2014-12-12 01:56:04 +0200108#define __get_user_asm(res, x, ptr, type, bwl, reg, err) ({ \
109 type __gu_val; \
110 asm volatile ("\n" \
111 "1: "MOVES"."#bwl" %2,%1\n" \
112 "2:\n" \
113 " .section .fixup,\"ax\"\n" \
114 " .even\n" \
115 "10: move.l %3,%0\n" \
116 " sub.l %1,%1\n" \
117 " jra 2b\n" \
118 " .previous\n" \
119 "\n" \
120 " .section __ex_table,\"a\"\n" \
121 " .align 4\n" \
122 " .long 1b,10b\n" \
123 " .previous" \
124 : "+d" (res), "=&" #reg (__gu_val) \
125 : "m" (*(ptr)), "i" (err)); \
126 (x) = (__force typeof(*(ptr)))(__force unsigned long)__gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Roman Zippeld94af932006-06-23 02:05:00 -0700129#define __get_user(x, ptr) \
130({ \
131 int __gu_err = 0; \
132 __chk_user_ptr(ptr); \
133 switch (sizeof(*(ptr))) { \
134 case 1: \
135 __get_user_asm(__gu_err, x, ptr, u8, b, d, -EFAULT); \
136 break; \
137 case 2: \
Geert Uytterhoeven631d8b62013-06-09 20:12:42 +0200138 __get_user_asm(__gu_err, x, ptr, u16, w, r, -EFAULT); \
Roman Zippeld94af932006-06-23 02:05:00 -0700139 break; \
140 case 4: \
141 __get_user_asm(__gu_err, x, ptr, u32, l, r, -EFAULT); \
142 break; \
143/* case 8: disabled because gcc-4.1 has a broken typeof \
144 { \
145 const void *__gu_ptr = (ptr); \
146 u64 __gu_val; \
147 asm volatile ("\n" \
Greg Ungerere08d7032011-10-14 14:43:30 +1000148 "1: "MOVES".l (%2)+,%1\n" \
149 "2: "MOVES".l (%2),%R1\n" \
Roman Zippeld94af932006-06-23 02:05:00 -0700150 "3:\n" \
151 " .section .fixup,\"ax\"\n" \
152 " .even\n" \
153 "10: move.l %3,%0\n" \
154 " sub.l %1,%1\n" \
155 " sub.l %R1,%R1\n" \
156 " jra 3b\n" \
157 " .previous\n" \
158 "\n" \
159 " .section __ex_table,\"a\"\n" \
160 " .align 4\n" \
161 " .long 1b,10b\n" \
162 " .long 2b,10b\n" \
163 " .previous" \
164 : "+d" (__gu_err), "=&r" (__gu_val), \
165 "+a" (__gu_ptr) \
166 : "i" (-EFAULT) \
167 : "memory"); \
Michael S. Tsirkin09a2f7c2014-12-12 01:56:04 +0200168 (x) = (__force typeof(*(ptr)))__gu_val; \
Roman Zippeld94af932006-06-23 02:05:00 -0700169 break; \
170 } */ \
171 default: \
172 __gu_err = __get_user_bad(); \
173 break; \
174 } \
175 __gu_err; \
176})
177#define get_user(x, ptr) __get_user(x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Roman Zippeld94af932006-06-23 02:05:00 -0700179unsigned long __generic_copy_from_user(void *to, const void __user *from, unsigned long n);
180unsigned long __generic_copy_to_user(void __user *to, const void *from, unsigned long n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Al Viro7cefa5a2017-03-20 00:55:24 -0400182#define __suffix0
183#define __suffix1 b
184#define __suffix2 w
185#define __suffix4 l
186
187#define ____constant_copy_from_user_asm(res, to, from, tmp, n1, n2, n3, s1, s2, s3)\
Roman Zippel53617822006-06-25 05:46:53 -0700188 asm volatile ("\n" \
Greg Ungerere08d7032011-10-14 14:43:30 +1000189 "1: "MOVES"."#s1" (%2)+,%3\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700190 " move."#s1" %3,(%1)+\n" \
Al Viro7cefa5a2017-03-20 00:55:24 -0400191 " .ifnc \""#s2"\",\"\"\n" \
Greg Ungerere08d7032011-10-14 14:43:30 +1000192 "2: "MOVES"."#s2" (%2)+,%3\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700193 " move."#s2" %3,(%1)+\n" \
194 " .ifnc \""#s3"\",\"\"\n" \
Greg Ungerere08d7032011-10-14 14:43:30 +1000195 "3: "MOVES"."#s3" (%2)+,%3\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700196 " move."#s3" %3,(%1)+\n" \
197 " .endif\n" \
Al Viro7cefa5a2017-03-20 00:55:24 -0400198 " .endif\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700199 "4:\n" \
200 " .section __ex_table,\"a\"\n" \
201 " .align 4\n" \
202 " .long 1b,10f\n" \
Al Viro7cefa5a2017-03-20 00:55:24 -0400203 " .ifnc \""#s2"\",\"\"\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700204 " .long 2b,20f\n" \
205 " .ifnc \""#s3"\",\"\"\n" \
206 " .long 3b,30f\n" \
207 " .endif\n" \
Al Viro7cefa5a2017-03-20 00:55:24 -0400208 " .endif\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700209 " .previous\n" \
210 "\n" \
211 " .section .fixup,\"ax\"\n" \
212 " .even\n" \
Al Viro7cefa5a2017-03-20 00:55:24 -0400213 "10: addq.l #"#n1",%0\n" \
214 " .ifnc \""#s2"\",\"\"\n" \
215 "20: addq.l #"#n2",%0\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700216 " .ifnc \""#s3"\",\"\"\n" \
Al Viro7cefa5a2017-03-20 00:55:24 -0400217 "30: addq.l #"#n3",%0\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700218 " .endif\n" \
Al Viro7cefa5a2017-03-20 00:55:24 -0400219 " .endif\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700220 " jra 4b\n" \
221 " .previous\n" \
222 : "+d" (res), "+&a" (to), "+a" (from), "=&d" (tmp) \
223 : : "memory")
224
Al Viro7cefa5a2017-03-20 00:55:24 -0400225#define ___constant_copy_from_user_asm(res, to, from, tmp, n1, n2, n3, s1, s2, s3)\
226 ____constant_copy_from_user_asm(res, to, from, tmp, n1, n2, n3, s1, s2, s3)
227#define __constant_copy_from_user_asm(res, to, from, tmp, n1, n2, n3) \
228 ___constant_copy_from_user_asm(res, to, from, tmp, n1, n2, n3, \
229 __suffix##n1, __suffix##n2, __suffix##n3)
230
Roman Zippeld94af932006-06-23 02:05:00 -0700231static __always_inline unsigned long
Al Viro11c40f82006-01-12 01:06:24 -0800232__constant_copy_from_user(void *to, const void __user *from, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Roman Zippeld94af932006-06-23 02:05:00 -0700234 unsigned long res = 0, tmp;
235
Roman Zippeld94af932006-06-23 02:05:00 -0700236 switch (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 case 1:
Al Viro7cefa5a2017-03-20 00:55:24 -0400238 __constant_copy_from_user_asm(res, to, from, tmp, 1, 0, 0);
Roman Zippel53617822006-06-25 05:46:53 -0700239 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 case 2:
Al Viro7cefa5a2017-03-20 00:55:24 -0400241 __constant_copy_from_user_asm(res, to, from, tmp, 2, 0, 0);
Roman Zippel53617822006-06-25 05:46:53 -0700242 break;
243 case 3:
Al Viro7cefa5a2017-03-20 00:55:24 -0400244 __constant_copy_from_user_asm(res, to, from, tmp, 2, 1, 0);
Roman Zippel53617822006-06-25 05:46:53 -0700245 break;
Roman Zippeld94af932006-06-23 02:05:00 -0700246 case 4:
Al Viro7cefa5a2017-03-20 00:55:24 -0400247 __constant_copy_from_user_asm(res, to, from, tmp, 4, 0, 0);
Roman Zippel53617822006-06-25 05:46:53 -0700248 break;
249 case 5:
Al Viro7cefa5a2017-03-20 00:55:24 -0400250 __constant_copy_from_user_asm(res, to, from, tmp, 4, 1, 0);
Roman Zippel53617822006-06-25 05:46:53 -0700251 break;
252 case 6:
Al Viro7cefa5a2017-03-20 00:55:24 -0400253 __constant_copy_from_user_asm(res, to, from, tmp, 4, 2, 0);
Roman Zippel53617822006-06-25 05:46:53 -0700254 break;
255 case 7:
Al Viro7cefa5a2017-03-20 00:55:24 -0400256 __constant_copy_from_user_asm(res, to, from, tmp, 4, 2, 1);
Roman Zippel53617822006-06-25 05:46:53 -0700257 break;
258 case 8:
Al Viro7cefa5a2017-03-20 00:55:24 -0400259 __constant_copy_from_user_asm(res, to, from, tmp, 4, 4, 0);
Roman Zippel53617822006-06-25 05:46:53 -0700260 break;
261 case 9:
Al Viro7cefa5a2017-03-20 00:55:24 -0400262 __constant_copy_from_user_asm(res, to, from, tmp, 4, 4, 1);
Roman Zippel53617822006-06-25 05:46:53 -0700263 break;
264 case 10:
Al Viro7cefa5a2017-03-20 00:55:24 -0400265 __constant_copy_from_user_asm(res, to, from, tmp, 4, 4, 2);
Roman Zippel53617822006-06-25 05:46:53 -0700266 break;
267 case 12:
Al Viro7cefa5a2017-03-20 00:55:24 -0400268 __constant_copy_from_user_asm(res, to, from, tmp, 4, 4, 4);
Roman Zippel53617822006-06-25 05:46:53 -0700269 break;
270 default:
271 /* we limit the inlined version to 3 moves */
272 return __generic_copy_from_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Roman Zippeld94af932006-06-23 02:05:00 -0700274
Roman Zippeld94af932006-06-23 02:05:00 -0700275 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Roman Zippel53617822006-06-25 05:46:53 -0700278#define __constant_copy_to_user_asm(res, to, from, tmp, n, s1, s2, s3) \
279 asm volatile ("\n" \
280 " move."#s1" (%2)+,%3\n" \
Greg Ungerere08d7032011-10-14 14:43:30 +1000281 "11: "MOVES"."#s1" %3,(%1)+\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700282 "12: move."#s2" (%2)+,%3\n" \
Greg Ungerere08d7032011-10-14 14:43:30 +1000283 "21: "MOVES"."#s2" %3,(%1)+\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700284 "22:\n" \
285 " .ifnc \""#s3"\",\"\"\n" \
286 " move."#s3" (%2)+,%3\n" \
Greg Ungerere08d7032011-10-14 14:43:30 +1000287 "31: "MOVES"."#s3" %3,(%1)+\n" \
Roman Zippel53617822006-06-25 05:46:53 -0700288 "32:\n" \
289 " .endif\n" \
290 "4:\n" \
291 "\n" \
292 " .section __ex_table,\"a\"\n" \
293 " .align 4\n" \
294 " .long 11b,5f\n" \
295 " .long 12b,5f\n" \
296 " .long 21b,5f\n" \
297 " .long 22b,5f\n" \
298 " .ifnc \""#s3"\",\"\"\n" \
299 " .long 31b,5f\n" \
300 " .long 32b,5f\n" \
301 " .endif\n" \
302 " .previous\n" \
303 "\n" \
304 " .section .fixup,\"ax\"\n" \
305 " .even\n" \
306 "5: moveq.l #"#n",%0\n" \
307 " jra 4b\n" \
308 " .previous\n" \
309 : "+d" (res), "+a" (to), "+a" (from), "=&d" (tmp) \
310 : : "memory")
311
Roman Zippeld94af932006-06-23 02:05:00 -0700312static __always_inline unsigned long
Al Viro11c40f82006-01-12 01:06:24 -0800313__constant_copy_to_user(void __user *to, const void *from, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Roman Zippeld94af932006-06-23 02:05:00 -0700315 unsigned long res = 0, tmp;
316
Roman Zippeld94af932006-06-23 02:05:00 -0700317 switch (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 case 1:
Al Virob9710182006-10-11 17:27:57 +0100319 __put_user_asm(res, *(u8 *)from, (u8 __user *)to, b, d, 1);
Roman Zippel53617822006-06-25 05:46:53 -0700320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 case 2:
Geert Uytterhoeven631d8b62013-06-09 20:12:42 +0200322 __put_user_asm(res, *(u16 *)from, (u16 __user *)to, w, r, 2);
Roman Zippel53617822006-06-25 05:46:53 -0700323 break;
324 case 3:
325 __constant_copy_to_user_asm(res, to, from, tmp, 3, w, b,);
326 break;
Roman Zippeld94af932006-06-23 02:05:00 -0700327 case 4:
Al Virob9710182006-10-11 17:27:57 +0100328 __put_user_asm(res, *(u32 *)from, (u32 __user *)to, l, r, 4);
Roman Zippel53617822006-06-25 05:46:53 -0700329 break;
330 case 5:
331 __constant_copy_to_user_asm(res, to, from, tmp, 5, l, b,);
332 break;
333 case 6:
334 __constant_copy_to_user_asm(res, to, from, tmp, 6, l, w,);
335 break;
336 case 7:
337 __constant_copy_to_user_asm(res, to, from, tmp, 7, l, w, b);
338 break;
339 case 8:
340 __constant_copy_to_user_asm(res, to, from, tmp, 8, l, l,);
341 break;
342 case 9:
343 __constant_copy_to_user_asm(res, to, from, tmp, 9, l, l, b);
344 break;
345 case 10:
346 __constant_copy_to_user_asm(res, to, from, tmp, 10, l, l, w);
347 break;
348 case 12:
349 __constant_copy_to_user_asm(res, to, from, tmp, 12, l, l, l);
350 break;
351 default:
352 /* limit the inlined version to 3 moves */
353 return __generic_copy_to_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Roman Zippeld94af932006-06-23 02:05:00 -0700355
Roman Zippeld94af932006-06-23 02:05:00 -0700356 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Al Viro29be02e2017-03-21 08:50:56 -0400359static inline unsigned long
360raw_copy_from_user(void *to, const void __user *from, unsigned long n)
361{
362 if (__builtin_constant_p(n))
363 return __constant_copy_from_user(to, from, n);
364 return __generic_copy_from_user(to, from, n);
365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Al Viro7cefa5a2017-03-20 00:55:24 -0400367static inline unsigned long
Al Viro29be02e2017-03-21 08:50:56 -0400368raw_copy_to_user(void __user *to, const void *from, unsigned long n)
Al Viro7cefa5a2017-03-20 00:55:24 -0400369{
Al Viro29be02e2017-03-21 08:50:56 -0400370 if (__builtin_constant_p(n))
371 return __constant_copy_to_user(to, from, n);
372 return __generic_copy_to_user(to, from, n);
Al Viro7cefa5a2017-03-20 00:55:24 -0400373}
Al Viro29be02e2017-03-21 08:50:56 -0400374#define INLINE_COPY_FROM_USER
375#define INLINE_COPY_TO_USER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Geert Uytterhoevend8ce7262012-05-29 23:30:08 +0200377#define user_addr_max() \
Al Virodb68ce12017-03-20 21:08:07 -0400378 (uaccess_kernel() ? ~0UL : TASK_SIZE)
Geert Uytterhoevend8ce7262012-05-29 23:30:08 +0200379
380extern long strncpy_from_user(char *dst, const char __user *src, long count);
381extern __must_check long strlen_user(const char __user *str);
382extern __must_check long strnlen_user(const char __user *str, long n);
383
Geert Uytterhoeven3c46bdc2007-05-15 01:41:29 -0700384unsigned long __clear_user(void __user *to, unsigned long n);
385
386#define clear_user __clear_user
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#endif /* _M68K_UACCESS_H */