2 * Authors: Bjorn Wesen (bjornw@axis.com)
3 * Hans-Peter Nilsson (hp@axis.com)
6 * Revision 1.8 2001/10/29 13:01:48 bjornw
7 * Removed unused variable tmp2 in strnlen_user
9 * Revision 1.7 2001/10/02 12:44:52 hp
10 * Add support for 64-bit put_user/get_user
12 * Revision 1.6 2001/10/01 14:51:17 bjornw
13 * Added register prefixes and removed underscores
15 * Revision 1.5 2000/10/25 03:33:21 hp
16 * - Provide implementation for everything else but get_user and put_user;
17 * copying inline to/from user for constant length 0..16, 20, 24, and
18 * clearing for 0..4, 8, 12, 16, 20, 24, strncpy_from_user and strnlen_user
20 * - Constraints for destination addr in get_user cannot be memory, only reg.
21 * - Correct labels for PC at expected fault points.
22 * - Nits with assembly code.
23 * - Don't use statement expressions without value; use "do {} while (0)".
24 * - Return correct values from __generic_... functions.
26 * Revision 1.4 2000/09/12 16:28:25 bjornw
27 * * Removed comments from the get/put user asm code
28 * * Constrains for destination addr in put_user cannot be memory, only reg
30 * Revision 1.3 2000/09/12 14:30:20 bjornw
31 * MAX_ADDR_USER does not exist anymore
33 * Revision 1.2 2000/07/13 15:52:48 bjornw
34 * New user-access functions
36 * Revision 1.1.1.1 2000/07/10 16:32:31 bjornw
37 * CRIS architecture, working draft
43 /* Asm:s have been tweaked (within the domain of correctness) to give
44 satisfactory results for "gcc version 2.96 20000427 (experimental)".
48 Register $r9 is chosen for temporaries, being a call-clobbered register
49 first in line to be used (notably for local blocks), not colliding with
50 parameter registers. */
52 #ifndef _CRIS_UACCESS_H
53 #define _CRIS_UACCESS_H
56 #include <linux/sched.h>
57 #include <linux/errno.h>
58 #include <asm/processor.h>
62 #define VERIFY_WRITE 1
65 * The fs value determines whether argument validity checking should be
66 * performed or not. If get_fs() == USER_DS, checking is performed, with
67 * get_fs() == KERNEL_DS, checking is bypassed.
69 * For historical reasons, these macros are grossly misnamed.
72 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
74 /* addr_limit is the maximum accessible address for the task. we misuse
75 * the KERNEL_DS and USER_DS values to both assign and compare the
76 * addr_limit values through the equally misnamed get/set_fs macros.
80 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
81 #define USER_DS MAKE_MM_SEG(TASK_SIZE)
83 #define get_ds() (KERNEL_DS)
84 #define get_fs() (current_thread_info()->addr_limit)
85 #define set_fs(x) (current_thread_info()->addr_limit = (x))
87 #define segment_eq(a,b) ((a).seg == (b).seg)
89 #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
90 #define __user_ok(addr,size) (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
91 #define __access_ok(addr,size) (__kernel_ok || __user_ok((addr),(size)))
92 #define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))
94 /* this function will go away soon - use access_ok() instead */
95 extern inline int __deprecated verify_area(int type, const void __user * addr, unsigned long size)
97 return access_ok(type,addr,size) ? 0 : -EFAULT;
101 #include <asm/arch/uaccess.h>
104 * The exception table consists of pairs of addresses: the first is the
105 * address of an instruction that is allowed to fault, and the second is
106 * the address at which the program should continue. No registers are
107 * modified, so it is entirely up to the continuation code to figure out
110 * All the routines below use bits of fixup code that are out of line
111 * with the main instruction path. This means when everything is well,
112 * we don't even have to jump over them. Further, they do not intrude
113 * on our cache or tlb entries.
116 struct exception_table_entry
118 unsigned long insn, fixup;
122 * These are the main single-value transfer routines. They automatically
123 * use the right size if we just have the right pointer type.
125 * This gets kind of ugly. We want to return _two_ values in "get_user()"
126 * and yet we don't want to do any pointers, because that is too much
127 * of a performance impact. Thus we have a few rather ugly macros here,
128 * and hide all the ugliness from the user.
130 * The "__xxx" versions of the user access functions are versions that
131 * do not verify the address space, that must have been done previously
132 * with a separate "access_ok()" call (this is used when we do multiple
133 * accesses to the same area of user memory).
135 * As we use the same address space for kernel and user data on
136 * CRIS, we can just do these as direct assignments. (Of course, the
137 * exception handling means that it's no longer "just"...)
139 #define get_user(x,ptr) \
140 __get_user_check((x),(ptr),sizeof(*(ptr)))
141 #define put_user(x,ptr) \
142 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
144 #define __get_user(x,ptr) \
145 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
146 #define __put_user(x,ptr) \
147 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
149 extern long __put_user_bad(void);
151 #define __put_user_size(x,ptr,size,retval) \
155 case 1: __put_user_asm(x,ptr,retval,"move.b"); break; \
156 case 2: __put_user_asm(x,ptr,retval,"move.w"); break; \
157 case 4: __put_user_asm(x,ptr,retval,"move.d"); break; \
158 case 8: __put_user_asm_64(x,ptr,retval); break; \
159 default: __put_user_bad(); \
163 #define __get_user_size(x,ptr,size,retval) \
167 case 1: __get_user_asm(x,ptr,retval,"move.b"); break; \
168 case 2: __get_user_asm(x,ptr,retval,"move.w"); break; \
169 case 4: __get_user_asm(x,ptr,retval,"move.d"); break; \
170 case 8: __get_user_asm_64(x,ptr,retval); break; \
171 default: (x) = __get_user_bad(); \
175 #define __put_user_nocheck(x,ptr,size) \
178 __put_user_size((x),(ptr),(size),__pu_err); \
182 #define __put_user_check(x,ptr,size) \
184 long __pu_err = -EFAULT; \
185 __typeof__(*(ptr)) *__pu_addr = (ptr); \
186 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
187 __put_user_size((x),__pu_addr,(size),__pu_err); \
191 struct __large_struct { unsigned long buf[100]; };
192 #define __m(x) (*(struct __large_struct *)(x))
196 #define __get_user_nocheck(x,ptr,size) \
198 long __gu_err, __gu_val; \
199 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
200 (x) = (__typeof__(*(ptr)))__gu_val; \
204 #define __get_user_check(x,ptr,size) \
206 long __gu_err = -EFAULT, __gu_val = 0; \
207 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
208 if (access_ok(VERIFY_READ,__gu_addr,size)) \
209 __get_user_size(__gu_val,__gu_addr,(size),__gu_err); \
210 (x) = (__typeof__(*(ptr)))__gu_val; \
214 extern long __get_user_bad(void);
216 /* More complex functions. Most are inline, but some call functions that
217 live in lib/usercopy.c */
219 extern unsigned long __copy_user(void *to, const void *from, unsigned long n);
220 extern unsigned long __copy_user_zeroing(void *to, const void *from, unsigned long n);
221 extern unsigned long __do_clear_user(void *to, unsigned long n);
223 extern inline unsigned long
224 __generic_copy_to_user(void __user *to, const void *from, unsigned long n)
226 if (access_ok(VERIFY_WRITE, to, n))
227 return __copy_user(to,from,n);
231 extern inline unsigned long
232 __generic_copy_from_user(void *to, const void __user *from, unsigned long n)
234 if (access_ok(VERIFY_READ, from, n))
235 return __copy_user_zeroing(to,from,n);
239 extern inline unsigned long
240 __generic_clear_user(void __user *to, unsigned long n)
242 if (access_ok(VERIFY_WRITE, to, n))
243 return __do_clear_user(to,n);
248 __strncpy_from_user(char *dst, const char __user *src, long count)
250 return __do_strncpy_from_user(dst, src, count);
254 strncpy_from_user(char *dst, const char __user *src, long count)
257 if (access_ok(VERIFY_READ, src, 1))
258 res = __do_strncpy_from_user(dst, src, count);
263 /* Note that if these expand awfully if made into switch constructs, so
266 extern inline unsigned long
267 __constant_copy_from_user(void *to, const void __user *from, unsigned long n)
269 unsigned long ret = 0;
273 __asm_copy_from_user_1(to, from, ret);
275 __asm_copy_from_user_2(to, from, ret);
277 __asm_copy_from_user_3(to, from, ret);
279 __asm_copy_from_user_4(to, from, ret);
281 __asm_copy_from_user_5(to, from, ret);
283 __asm_copy_from_user_6(to, from, ret);
285 __asm_copy_from_user_7(to, from, ret);
287 __asm_copy_from_user_8(to, from, ret);
289 __asm_copy_from_user_9(to, from, ret);
291 __asm_copy_from_user_10(to, from, ret);
293 __asm_copy_from_user_11(to, from, ret);
295 __asm_copy_from_user_12(to, from, ret);
297 __asm_copy_from_user_13(to, from, ret);
299 __asm_copy_from_user_14(to, from, ret);
301 __asm_copy_from_user_15(to, from, ret);
303 __asm_copy_from_user_16(to, from, ret);
305 __asm_copy_from_user_20(to, from, ret);
307 __asm_copy_from_user_24(to, from, ret);
309 ret = __generic_copy_from_user(to, from, n);
314 /* Ditto, don't make a switch out of this. */
316 extern inline unsigned long
317 __constant_copy_to_user(void __user *to, const void *from, unsigned long n)
319 unsigned long ret = 0;
323 __asm_copy_to_user_1(to, from, ret);
325 __asm_copy_to_user_2(to, from, ret);
327 __asm_copy_to_user_3(to, from, ret);
329 __asm_copy_to_user_4(to, from, ret);
331 __asm_copy_to_user_5(to, from, ret);
333 __asm_copy_to_user_6(to, from, ret);
335 __asm_copy_to_user_7(to, from, ret);
337 __asm_copy_to_user_8(to, from, ret);
339 __asm_copy_to_user_9(to, from, ret);
341 __asm_copy_to_user_10(to, from, ret);
343 __asm_copy_to_user_11(to, from, ret);
345 __asm_copy_to_user_12(to, from, ret);
347 __asm_copy_to_user_13(to, from, ret);
349 __asm_copy_to_user_14(to, from, ret);
351 __asm_copy_to_user_15(to, from, ret);
353 __asm_copy_to_user_16(to, from, ret);
355 __asm_copy_to_user_20(to, from, ret);
357 __asm_copy_to_user_24(to, from, ret);
359 ret = __generic_copy_to_user(to, from, n);
364 /* No switch, please. */
366 extern inline unsigned long
367 __constant_clear_user(void __user *to, unsigned long n)
369 unsigned long ret = 0;
373 __asm_clear_1(to, ret);
375 __asm_clear_2(to, ret);
377 __asm_clear_3(to, ret);
379 __asm_clear_4(to, ret);
381 __asm_clear_8(to, ret);
383 __asm_clear_12(to, ret);
385 __asm_clear_16(to, ret);
387 __asm_clear_20(to, ret);
389 __asm_clear_24(to, ret);
391 ret = __generic_clear_user(to, n);
397 #define clear_user(to, n) \
398 (__builtin_constant_p(n) ? \
399 __constant_clear_user(to, n) : \
400 __generic_clear_user(to, n))
402 #define copy_from_user(to, from, n) \
403 (__builtin_constant_p(n) ? \
404 __constant_copy_from_user(to, from, n) : \
405 __generic_copy_from_user(to, from, n))
407 #define copy_to_user(to, from, n) \
408 (__builtin_constant_p(n) ? \
409 __constant_copy_to_user(to, from, n) : \
410 __generic_copy_to_user(to, from, n))
412 /* We let the __ versions of copy_from/to_user inline, because they're often
413 * used in fast paths and have only a small space overhead.
416 extern inline unsigned long
417 __generic_copy_from_user_nocheck(void *to, const void *from, unsigned long n)
419 return __copy_user_zeroing(to,from,n);
422 extern inline unsigned long
423 __generic_copy_to_user_nocheck(void *to, const void *from, unsigned long n)
425 return __copy_user(to,from,n);
428 extern inline unsigned long
429 __generic_clear_user_nocheck(void *to, unsigned long n)
431 return __do_clear_user(to,n);
434 /* without checking */
436 #define __copy_to_user(to,from,n) __generic_copy_to_user_nocheck((to),(from),(n))
437 #define __copy_from_user(to,from,n) __generic_copy_from_user_nocheck((to),(from),(n))
438 #define __copy_to_user_inatomic __copy_to_user
439 #define __copy_from_user_inatomic __copy_from_user
440 #define __clear_user(to,n) __generic_clear_user_nocheck((to),(n))
442 #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
444 #endif /* __ASSEMBLY__ */
446 #endif /* _CRIS_UACCESS_H */