1 /* $Id: uaccess.h,v 1.21 2000/01/08 16:38:23 anton Exp $
2 * uaccess.h: User space memore access functions.
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <asm/vac-ops.h>
14 #include <asm/a.out.h>
19 /* Sparc is not segmented, however we need to be able to fool verify_area()
20 * when doing system calls from kernel mode legitimately.
22 * "For historical reasons, these macros are grossly misnamed." -Linus
25 #define KERNEL_DS ((mm_segment_t) { 0 })
26 #define USER_DS ((mm_segment_t) { -1 })
29 #define VERIFY_WRITE 1
31 #define get_ds() (KERNEL_DS)
32 #define get_fs() (current->thread.current_ds)
33 #define set_fs(val) ((current->thread.current_ds) = (val))
35 #define segment_eq(a,b) ((a).seg == (b).seg)
37 /* We have there a nice not-mapped page at PAGE_OFFSET - PAGE_SIZE, so that this test
38 * can be fairly lightweight.
39 * No one can read/write anything from userland in the kernel space by setting
40 * large size and address near to PAGE_OFFSET - a fault will break his intentions.
42 #define __user_ok(addr,size) ((addr) < STACK_TOP)
43 #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
44 #define __access_ok(addr,size) (__user_ok((addr) & get_fs().seg,(size)))
45 #define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))
47 extern inline int verify_area(int type
, const void * addr
, unsigned long size
)
49 return access_ok(type
,addr
,size
)?0:-EFAULT
;
53 * The exception table consists of pairs of addresses: the first is the
54 * address of an instruction that is allowed to fault, and the second is
55 * the address at which the program should continue. No registers are
56 * modified, so it is entirely up to the continuation code to figure out
59 * All the routines below use bits of fixup code that are out of line
60 * with the main instruction path. This means when everything is well,
61 * we don't even have to jump over them. Further, they do not intrude
62 * on our cache or tlb entries.
64 * There is a special way how to put a range of potentially faulting
65 * insns (like twenty ldd/std's with now intervening other instructions)
66 * You specify address of first in insn and 0 in fixup and in the next
67 * exception_table_entry you specify last potentially faulting insn + 1
68 * and in fixup the routine which should handle the fault.
69 * That fixup code will get
70 * (faulting_insn_address - first_insn_in_the_range_address)/4
71 * in %g2 (ie. index of the faulting instruction in the range).
74 struct exception_table_entry
76 unsigned long insn
, fixup
;
79 /* Returns 0 if exception not found and fixup otherwise. */
80 extern unsigned long search_exception_table(unsigned long, unsigned long *);
82 extern void __ret_efault(void);
84 /* Uh, these should become the main single-value transfer routines..
85 * They automatically use the right size if we just have the right
88 * This gets kind of ugly. We want to return _two_ values in "get_user()"
89 * and yet we don't want to do any pointers, because that is too much
90 * of a performance impact. Thus we have a few rather ugly macros here,
91 * and hide all the uglyness from the user.
93 #define put_user(x,ptr) ({ \
94 unsigned long __pu_addr = (unsigned long)(ptr); \
95 __put_user_check((__typeof__(*(ptr)))(x),__pu_addr,sizeof(*(ptr))); })
97 #define put_user_ret(x,ptr,retval) ({ \
98 unsigned long __pu_addr = (unsigned long)(ptr); \
99 __put_user_check_ret((__typeof__(*(ptr)))(x),__pu_addr,sizeof(*(ptr)),retval); })
101 #define get_user(x,ptr) ({ \
102 unsigned long __gu_addr = (unsigned long)(ptr); \
103 __get_user_check((x),__gu_addr,sizeof(*(ptr)),__typeof__(*(ptr))); })
105 #define get_user_ret(x,ptr,retval) ({ \
106 unsigned long __gu_addr = (unsigned long)(ptr); \
107 __get_user_check_ret((x),__gu_addr,sizeof(*(ptr)),__typeof__(*(ptr)),retval); })
110 * The "__xxx" versions do not do address space checking, useful when
111 * doing multiple accesses to the same area (the user has to do the
112 * checks by hand with "access_ok()")
114 #define __put_user(x,ptr) __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
115 #define __put_user_ret(x,ptr,retval) __put_user_nocheck_ret((x),(ptr),sizeof(*(ptr)),retval)
116 #define __get_user(x,ptr) __get_user_nocheck((x),(ptr),sizeof(*(ptr)),__typeof__(*(ptr)))
117 #define __get_user_ret(x,ptr,retval) __get_user_nocheck_ret((x),(ptr),sizeof(*(ptr)),__typeof__(*(ptr)),retval)
119 struct __large_struct
{ unsigned long buf
[100]; };
120 #define __m(x) ((struct __large_struct *)(x))
122 #define __put_user_check(x,addr,size) ({ \
123 register int __pu_ret; \
124 if (__access_ok(addr,size)) { \
126 case 1: __put_user_asm(x,b,addr,__pu_ret); break; \
127 case 2: __put_user_asm(x,h,addr,__pu_ret); break; \
128 case 4: __put_user_asm(x,,addr,__pu_ret); break; \
129 default: __pu_ret = __put_user_bad(); break; \
130 } } else { __pu_ret = -EFAULT; } __pu_ret; })
132 #define __put_user_check_ret(x,addr,size,retval) ({ \
133 register int __foo __asm__ ("l1"); \
134 if (__access_ok(addr,size)) { \
136 case 1: __put_user_asm_ret(x,b,addr,retval,__foo); break; \
137 case 2: __put_user_asm_ret(x,h,addr,retval,__foo); break; \
138 case 4: __put_user_asm_ret(x,,addr,retval,__foo); break; \
139 default: if (__put_user_bad()) return retval; break; \
140 } } else return retval; })
142 #define __put_user_nocheck(x,addr,size) ({ \
143 register int __pu_ret; \
145 case 1: __put_user_asm(x,b,addr,__pu_ret); break; \
146 case 2: __put_user_asm(x,h,addr,__pu_ret); break; \
147 case 4: __put_user_asm(x,,addr,__pu_ret); break; \
148 default: __pu_ret = __put_user_bad(); break; \
151 #define __put_user_nocheck_ret(x,addr,size,retval) ({ \
152 register int __foo __asm__ ("l1"); \
154 case 1: __put_user_asm_ret(x,b,addr,retval,__foo); break; \
155 case 2: __put_user_asm_ret(x,h,addr,retval,__foo); break; \
156 case 4: __put_user_asm_ret(x,,addr,retval,__foo); break; \
157 default: if (__put_user_bad()) return retval; break; \
160 #define __put_user_asm(x,size,addr,ret) \
161 __asm__ __volatile__( \
162 "/* Put user asm, inline. */\n" \
163 "1:\t" "st"#size " %1, %2\n\t" \
166 ".section .fixup,#alloc,#execinstr\n\t" \
172 ".section __ex_table,#alloc\n\t" \
176 : "=&r" (ret) : "r" (x), "m" (*__m(addr)), \
179 #define __put_user_asm_ret(x,size,addr,ret,foo) \
180 if (__builtin_constant_p(ret) && ret == -EFAULT) \
181 __asm__ __volatile__( \
182 "/* Put user asm ret, inline. */\n" \
183 "1:\t" "st"#size " %1, %2\n\n\t" \
184 ".section __ex_table,#alloc\n\t" \
186 ".word 1b, __ret_efault\n\n\t" \
188 : "=r" (foo) : "r" (x), "m" (*__m(addr))); \
190 __asm__ __volatile( \
191 "/* Put user asm ret, inline. */\n" \
192 "1:\t" "st"#size " %1, %2\n\n\t" \
193 ".section .fixup,#alloc,#execinstr\n\t" \
197 " restore %%g0, %3, %%o0\n\t" \
199 ".section __ex_table,#alloc\n\t" \
201 ".word 1b, 3b\n\n\t" \
203 : "=r" (foo) : "r" (x), "m" (*__m(addr)), "i" (ret))
205 extern int __put_user_bad(void);
207 #define __get_user_check(x,addr,size,type) ({ \
208 register int __gu_ret; \
209 register unsigned long __gu_val; \
210 if (__access_ok(addr,size)) { \
212 case 1: __get_user_asm(__gu_val,ub,addr,__gu_ret); break; \
213 case 2: __get_user_asm(__gu_val,uh,addr,__gu_ret); break; \
214 case 4: __get_user_asm(__gu_val,,addr,__gu_ret); break; \
215 default: __gu_val = 0; __gu_ret = __get_user_bad(); break; \
216 } } else { __gu_val = 0; __gu_ret = -EFAULT; } x = (type) __gu_val; __gu_ret; })
218 #define __get_user_check_ret(x,addr,size,type,retval) ({ \
219 register unsigned long __gu_val __asm__ ("l1"); \
220 if (__access_ok(addr,size)) { \
222 case 1: __get_user_asm_ret(__gu_val,ub,addr,retval); break; \
223 case 2: __get_user_asm_ret(__gu_val,uh,addr,retval); break; \
224 case 4: __get_user_asm_ret(__gu_val,,addr,retval); break; \
225 default: if (__get_user_bad()) return retval; \
226 } x = (type) __gu_val; } else return retval; })
228 #define __get_user_nocheck(x,addr,size,type) ({ \
229 register int __gu_ret; \
230 register unsigned long __gu_val; \
232 case 1: __get_user_asm(__gu_val,ub,addr,__gu_ret); break; \
233 case 2: __get_user_asm(__gu_val,uh,addr,__gu_ret); break; \
234 case 4: __get_user_asm(__gu_val,,addr,__gu_ret); break; \
235 default: __gu_val = 0; __gu_ret = __get_user_bad(); break; \
236 } x = (type) __gu_val; __gu_ret; })
238 #define __get_user_nocheck_ret(x,addr,size,type,retval) ({ \
239 register unsigned long __gu_val __asm__ ("l1"); \
241 case 1: __get_user_asm_ret(__gu_val,ub,addr,retval); break; \
242 case 2: __get_user_asm_ret(__gu_val,uh,addr,retval); break; \
243 case 4: __get_user_asm_ret(__gu_val,,addr,retval); break; \
244 default: if (__get_user_bad()) return retval; \
245 } x = (type) __gu_val; })
247 #define __get_user_asm(x,size,addr,ret) \
248 __asm__ __volatile__( \
249 "/* Get user asm, inline. */\n" \
250 "1:\t" "ld"#size " %2, %1\n\t" \
253 ".section .fixup,#alloc,#execinstr\n\t" \
258 " mov %3, %0\n\n\t" \
260 ".section __ex_table,#alloc\n\t" \
262 ".word 1b, 3b\n\n\t" \
264 : "=&r" (ret), "=&r" (x) : "m" (*__m(addr)), \
267 #define __get_user_asm_ret(x,size,addr,retval) \
268 if (__builtin_constant_p(retval) && retval == -EFAULT) \
269 __asm__ __volatile__( \
270 "/* Get user asm ret, inline. */\n" \
271 "1:\t" "ld"#size " %1, %0\n\n\t" \
272 ".section __ex_table,#alloc\n\t" \
274 ".word 1b,__ret_efault\n\n\t" \
276 : "=&r" (x) : "m" (*__m(addr))); \
278 __asm__ __volatile__( \
279 "/* Get user asm ret, inline. */\n" \
280 "1:\t" "ld"#size " %1, %0\n\n\t" \
281 ".section .fixup,#alloc,#execinstr\n\t" \
285 " restore %%g0, %2, %%o0\n\n\t" \
287 ".section __ex_table,#alloc\n\t" \
289 ".word 1b, 3b\n\n\t" \
291 : "=&r" (x) : "m" (*__m(addr)), "i" (retval))
293 extern int __get_user_bad(void);
295 extern __kernel_size_t
__copy_user(void *to
, void *from
, __kernel_size_t size
);
297 #define copy_to_user(to,from,n) ({ \
298 void *__copy_to = (void *) (to); \
299 __kernel_size_t __copy_size = (__kernel_size_t) (n); \
300 __kernel_size_t __copy_res; \
301 if(__copy_size && __access_ok((unsigned long)__copy_to, __copy_size)) { \
302 __copy_res = __copy_user(__copy_to, (void *) (from), __copy_size); \
303 } else __copy_res = __copy_size; \
306 #define copy_to_user_ret(to,from,n,retval) ({ \
307 if (copy_to_user(to,from,n)) \
311 #define __copy_to_user(to,from,n) \
312 __copy_user((void *)(to), \
315 #define __copy_to_user_ret(to,from,n,retval) ({ \
316 if (__copy_to_user(to,from,n)) \
320 #define copy_from_user(to,from,n) ({ \
321 void *__copy_to = (void *) (to); \
322 void *__copy_from = (void *) (from); \
323 __kernel_size_t __copy_size = (__kernel_size_t) (n); \
324 __kernel_size_t __copy_res; \
325 if(__copy_size && __access_ok((unsigned long)__copy_from, __copy_size)) { \
326 __copy_res = __copy_user(__copy_to, __copy_from, __copy_size); \
327 } else __copy_res = __copy_size; \
330 #define copy_from_user_ret(to,from,n,retval) ({ \
331 if (copy_from_user(to,from,n)) \
335 #define __copy_from_user(to,from,n) \
336 __copy_user((void *)(to), \
339 #define __copy_from_user_ret(to,from,n,retval) ({ \
340 if (__copy_from_user(to,from,n)) \
344 extern __inline__ __kernel_size_t
__clear_user(void *addr
, __kernel_size_t size
)
347 __asm__
__volatile__ ("
348 .section __ex_table,#alloc
356 " : "=r" (ret
) : "r" (addr
), "r" (size
) :
357 "o0", "o1", "o2", "o3", "o4", "o5", "o7",
358 "g1", "g2", "g3", "g4", "g5", "g7", "cc");
362 #define clear_user(addr,n) ({ \
363 void *__clear_addr = (void *) (addr); \
364 __kernel_size_t __clear_size = (__kernel_size_t) (n); \
365 __kernel_size_t __clear_res; \
366 if(__clear_size && __access_ok((unsigned long)__clear_addr, __clear_size)) { \
367 __clear_res = __clear_user(__clear_addr, __clear_size); \
368 } else __clear_res = __clear_size; \
371 #define clear_user_ret(addr,size,retval) ({ \
372 if (clear_user(addr,size)) \
376 extern int __strncpy_from_user(unsigned long dest
, unsigned long src
, int count
);
378 #define strncpy_from_user(dest,src,count) ({ \
379 unsigned long __sfu_src = (unsigned long) (src); \
380 int __sfu_count = (int) (count); \
381 long __sfu_res = -EFAULT; \
382 if(__access_ok(__sfu_src, __sfu_count)) { \
383 __sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
386 extern int __strlen_user(const char *);
387 extern int __strnlen_user(const char *, long len
);
389 extern __inline__
int strlen_user(const char *str
)
391 if(!access_ok(VERIFY_READ
, str
, 0))
394 return __strlen_user(str
);
397 extern __inline__
int strnlen_user(const char *str
, long len
)
399 if(!access_ok(VERIFY_READ
, str
, 0))
402 return __strnlen_user(str
, len
);
405 #endif /* __ASSEMBLY__ */
407 #endif /* _ASM_UACCESS_H */