1 #ifndef __i386_UACCESS_H
2 #define __i386_UACCESS_H
5 * User space memory access functions
7 #include <linux/config.h>
8 #include <linux/sched.h>
12 #define VERIFY_WRITE 1
15 * The fs value determines whether argument validity checking should be
16 * performed or not. If get_fs() == USER_DS, checking is performed, with
17 * get_fs() == KERNEL_DS, checking is bypassed.
19 * For historical reasons, these macros are grossly misnamed.
22 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
25 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
26 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
28 #define get_ds() (KERNEL_DS)
29 #define get_fs() (current->addr_limit)
30 #define set_fs(x) (current->addr_limit = (x))
32 #define segment_eq(a,b) ((a).seg == (b).seg)
34 extern int __verify_write(const void *, unsigned long);
36 #define __addr_ok(addr) ((unsigned long)(addr) < (current->addr_limit.seg))
39 * Uhhuh, this needs 33-bit arithmetic. We have a carry..
41 #define __range_ok(addr,size) ({ \
42 unsigned long flag,sum; \
43 asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
44 :"=&r" (flag), "=r" (sum) \
45 :"1" (addr),"g" ((int)(size)),"g" (current->addr_limit.seg)); \
48 #ifdef CONFIG_X86_WP_WORKS_OK
50 #define access_ok(type,addr,size) (__range_ok(addr,size) == 0)
54 #define access_ok(type,addr,size) ( (__range_ok(addr,size) == 0) && \
55 ((type) == VERIFY_READ || boot_cpu_data.wp_works_ok || \
56 segment_eq(get_fs(),KERNEL_DS) || \
57 __verify_write((void *)(addr),(size))))
61 extern inline int verify_area(int type
, const void * addr
, unsigned long size
)
63 return access_ok(type
,addr
,size
) ? 0 : -EFAULT
;
68 * The exception table consists of pairs of addresses: the first is the
69 * address of an instruction that is allowed to fault, and the second is
70 * the address at which the program should continue. No registers are
71 * modified, so it is entirely up to the continuation code to figure out
74 * All the routines below use bits of fixup code that are out of line
75 * with the main instruction path. This means when everything is well,
76 * we don't even have to jump over them. Further, they do not intrude
77 * on our cache or tlb entries.
80 struct exception_table_entry
82 unsigned long insn
, fixup
;
85 /* Returns 0 if exception not found and fixup otherwise. */
86 extern unsigned long search_exception_table(unsigned long);
90 * These are the main single-value transfer routines. They automatically
91 * use the right size if we just have the right pointer type.
93 * This gets kind of ugly. We want to return _two_ values in "get_user()"
94 * and yet we don't want to do any pointers, because that is too much
95 * of a performance impact. Thus we have a few rather ugly macros here,
96 * and hide all the uglyness from the user.
98 * The "__xxx" versions of the user access functions are versions that
99 * do not verify the address space, that must have been done previously
100 * with a separate "access_ok()" call (this is used when we do multiple
101 * accesses to the same area of user memory).
104 extern void __get_user_1(void);
105 extern void __get_user_2(void);
106 extern void __get_user_4(void);
108 #define __get_user_x(size,ret,x,ptr) \
109 __asm__ __volatile__("call __get_user_" #size \
110 :"=a" (ret),"=d" (x) \
113 /* Careful: we have to cast the result to the type of the pointer for sign reasons */
114 #define get_user(x,ptr) \
115 ({ int __ret_gu,__val_gu; \
116 switch(sizeof (*(ptr))) { \
117 case 1: __get_user_x(1,__ret_gu,__val_gu,ptr); break; \
118 case 2: __get_user_x(2,__ret_gu,__val_gu,ptr); break; \
119 case 4: __get_user_x(4,__ret_gu,__val_gu,ptr); break; \
120 default: __get_user_x(X,__ret_gu,__val_gu,ptr); break; \
122 (x) = (__typeof__(*(ptr)))__val_gu; \
126 extern void __put_user_1(void);
127 extern void __put_user_2(void);
128 extern void __put_user_4(void);
130 extern void __put_user_bad(void);
132 #define __put_user_x(size,ret,x,ptr) \
133 __asm__ __volatile__("call __put_user_" #size \
138 #define put_user(x,ptr) \
139 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
141 #define __get_user(x,ptr) \
142 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
143 #define __put_user(x,ptr) \
144 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
146 #define __put_user_nocheck(x,ptr,size) \
149 __put_user_size((x),(ptr),(size),__pu_err); \
154 #define __put_user_check(x,ptr,size) \
156 long __pu_err = -EFAULT; \
157 __typeof__(*(ptr)) *__pu_addr = (ptr); \
158 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
159 __put_user_size((x),__pu_addr,(size),__pu_err); \
163 #define __put_user_size(x,ptr,size,retval) \
167 case 1: __put_user_asm(x,ptr,retval,"b","b","iq"); break; \
168 case 2: __put_user_asm(x,ptr,retval,"w","w","ir"); break; \
169 case 4: __put_user_asm(x,ptr,retval,"l","","ir"); break; \
170 default: __put_user_bad(); \
174 struct __large_struct
{ unsigned long buf
[100]; };
175 #define __m(x) (*(struct __large_struct *)(x))
178 * Tell gcc we read from memory instead of writing: this is because
179 * we do not write to any memory gcc knows about, so there are no
182 #define __put_user_asm(x, addr, err, itype, rtype, ltype) \
183 __asm__ __volatile__( \
184 "1: mov"itype" %"rtype"1,%2\n" \
186 ".section .fixup,\"ax\"\n" \
190 ".section __ex_table,\"a\"\n" \
195 : ltype (x), "m"(__m(addr)), "i"(-EFAULT), "0"(err))
198 #define __get_user_nocheck(x,ptr,size) \
200 long __gu_err, __gu_val; \
201 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
202 (x) = (__typeof__(*(ptr)))__gu_val; \
206 extern long __get_user_bad(void);
208 #define __get_user_size(x,ptr,size,retval) \
212 case 1: __get_user_asm(x,ptr,retval,"b","b","=q"); break; \
213 case 2: __get_user_asm(x,ptr,retval,"w","w","=r"); break; \
214 case 4: __get_user_asm(x,ptr,retval,"l","","=r"); break; \
215 default: (x) = __get_user_bad(); \
219 #define __get_user_asm(x, addr, err, itype, rtype, ltype) \
220 __asm__ __volatile__( \
221 "1: mov"itype" %2,%"rtype"1\n" \
223 ".section .fixup,\"ax\"\n" \
225 " xor"itype" %"rtype"1,%"rtype"1\n" \
228 ".section __ex_table,\"a\"\n" \
232 : "=r"(err), ltype (x) \
233 : "m"(__m(addr)), "i"(-EFAULT), "0"(err))
237 * Copy To/From Userspace
240 /* Generic arbitrary sized copy. */
241 #define __copy_user(to,from,size) \
244 __asm__ __volatile__( \
249 ".section .fixup,\"ax\"\n" \
250 "3: lea 0(%3,%0,4),%0\n" \
253 ".section __ex_table,\"a\"\n" \
258 : "=&c"(size), "=&D" (__d0), "=&S" (__d1) \
259 : "r"(size & 3), "0"(size / 4), "1"(to), "2"(from) \
263 #define __copy_user_zeroing(to,from,size) \
266 __asm__ __volatile__( \
271 ".section .fixup,\"ax\"\n" \
272 "3: lea 0(%3,%0,4),%0\n" \
275 " xorl %%eax,%%eax\n" \
281 ".section __ex_table,\"a\"\n" \
286 : "=&c"(size), "=&D" (__d0), "=&S" (__d1) \
287 : "r"(size & 3), "0"(size / 4), "1"(to), "2"(from) \
291 /* We let the __ versions of copy_from/to_user inline, because they're often
292 * used in fast paths and have only a small space overhead.
294 static inline unsigned long
295 __generic_copy_from_user_nocheck(void *to
, const void *from
, unsigned long n
)
297 __copy_user_zeroing(to
,from
,n
);
301 static inline unsigned long
302 __generic_copy_to_user_nocheck(void *to
, const void *from
, unsigned long n
)
304 __copy_user(to
,from
,n
);
309 /* Optimize just a little bit when we know the size of the move. */
310 #define __constant_copy_user(to, from, size) \
313 switch (size & 3) { \
315 __asm__ __volatile__( \
318 ".section .fixup,\"ax\"\n" \
322 ".section __ex_table,\"a\"\n" \
326 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
327 : "1"(from), "2"(to), "0"(size/4) \
331 __asm__ __volatile__( \
335 ".section .fixup,\"ax\"\n" \
340 ".section __ex_table,\"a\"\n" \
345 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
346 : "1"(from), "2"(to), "0"(size/4) \
350 __asm__ __volatile__( \
354 ".section .fixup,\"ax\"\n" \
359 ".section __ex_table,\"a\"\n" \
364 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
365 : "1"(from), "2"(to), "0"(size/4) \
369 __asm__ __volatile__( \
374 ".section .fixup,\"ax\"\n" \
380 ".section __ex_table,\"a\"\n" \
386 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
387 : "1"(from), "2"(to), "0"(size/4) \
393 /* Optimize just a little bit when we know the size of the move. */
394 #define __constant_copy_user_zeroing(to, from, size) \
397 switch (size & 3) { \
399 __asm__ __volatile__( \
402 ".section .fixup,\"ax\"\n" \
405 " xorl %%eax,%%eax\n" \
412 ".section __ex_table,\"a\"\n" \
416 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
417 : "1"(from), "2"(to), "0"(size/4) \
421 __asm__ __volatile__( \
425 ".section .fixup,\"ax\"\n" \
428 " xorl %%eax,%%eax\n" \
437 " xorl %%eax,%%eax\n" \
443 ".section __ex_table,\"a\"\n" \
448 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
449 : "1"(from), "2"(to), "0"(size/4) \
453 __asm__ __volatile__( \
457 ".section .fixup,\"ax\"\n" \
460 " xorl %%eax,%%eax\n" \
469 " xorl %%eax,%%eax\n" \
475 ".section __ex_table,\"a\"\n" \
480 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
481 : "1"(from), "2"(to), "0"(size/4) \
485 __asm__ __volatile__( \
490 ".section .fixup,\"ax\"\n" \
493 " xorl %%eax,%%eax\n" \
503 " xorl %%eax,%%eax\n" \
510 " xorl %%eax,%%eax\n" \
516 ".section __ex_table,\"a\"\n" \
522 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
523 : "1"(from), "2"(to), "0"(size/4) \
529 unsigned long __generic_copy_to_user(void *, const void *, unsigned long);
530 unsigned long __generic_copy_from_user(void *, const void *, unsigned long);
532 static inline unsigned long
533 __constant_copy_to_user(void *to
, const void *from
, unsigned long n
)
535 if (access_ok(VERIFY_WRITE
, to
, n
))
536 __constant_copy_user(to
,from
,n
);
540 static inline unsigned long
541 __constant_copy_from_user(void *to
, const void *from
, unsigned long n
)
543 if (access_ok(VERIFY_READ
, from
, n
))
544 __constant_copy_user_zeroing(to
,from
,n
);
548 static inline unsigned long
549 __constant_copy_to_user_nocheck(void *to
, const void *from
, unsigned long n
)
551 __constant_copy_user(to
,from
,n
);
555 static inline unsigned long
556 __constant_copy_from_user_nocheck(void *to
, const void *from
, unsigned long n
)
558 __constant_copy_user_zeroing(to
,from
,n
);
562 #define copy_to_user(to,from,n) \
563 (__builtin_constant_p(n) ? \
564 __constant_copy_to_user((to),(from),(n)) : \
565 __generic_copy_to_user((to),(from),(n)))
567 #define copy_from_user(to,from,n) \
568 (__builtin_constant_p(n) ? \
569 __constant_copy_from_user((to),(from),(n)) : \
570 __generic_copy_from_user((to),(from),(n)))
572 #define __copy_to_user(to,from,n) \
573 (__builtin_constant_p(n) ? \
574 __constant_copy_to_user_nocheck((to),(from),(n)) : \
575 __generic_copy_to_user_nocheck((to),(from),(n)))
577 #define __copy_from_user(to,from,n) \
578 (__builtin_constant_p(n) ? \
579 __constant_copy_from_user_nocheck((to),(from),(n)) : \
580 __generic_copy_from_user_nocheck((to),(from),(n)))
582 long strncpy_from_user(char *dst
, const char *src
, long count
);
583 long __strncpy_from_user(char *dst
, const char *src
, long count
);
584 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
585 long strnlen_user(const char *str
, long n
);
586 unsigned long clear_user(void *mem
, unsigned long len
);
587 unsigned long __clear_user(void *mem
, unsigned long len
);
589 #endif /* __i386_UACCESS_H */