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>
9 #include <linux/prefetch.h>
13 #define VERIFY_WRITE 1
16 * The fs value determines whether argument validity checking should be
17 * performed or not. If get_fs() == USER_DS, checking is performed, with
18 * get_fs() == KERNEL_DS, checking is bypassed.
20 * For historical reasons, these macros are grossly misnamed.
23 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
26 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
27 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
29 #define get_ds() (KERNEL_DS)
30 #define get_fs() (current->addr_limit)
31 #define set_fs(x) (current->addr_limit = (x))
33 #define segment_eq(a,b) ((a).seg == (b).seg)
35 extern int __verify_write(const void *, unsigned long);
37 #define __addr_ok(addr) ((unsigned long)(addr) < (current->addr_limit.seg))
40 * Uhhuh, this needs 33-bit arithmetic. We have a carry..
42 #define __range_ok(addr,size) ({ \
43 unsigned long flag,sum; \
44 asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
45 :"=&r" (flag), "=r" (sum) \
46 :"1" (addr),"g" ((int)(size)),"g" (current->addr_limit.seg)); \
49 #ifdef CONFIG_X86_WP_WORKS_OK
51 #define access_ok(type,addr,size) (__range_ok(addr,size) == 0)
55 #define access_ok(type,addr,size) ( (__range_ok(addr,size) == 0) && \
56 ((type) == VERIFY_READ || boot_cpu_data.wp_works_ok || \
57 segment_eq(get_fs(),KERNEL_DS) || \
58 __verify_write((void *)(addr),(size))))
62 static inline int verify_area(int type, const void * addr, unsigned long size)
64 return access_ok(type,addr,size) ? 0 : -EFAULT;
69 * The exception table consists of pairs of addresses: the first is the
70 * address of an instruction that is allowed to fault, and the second is
71 * the address at which the program should continue. No registers are
72 * modified, so it is entirely up to the continuation code to figure out
75 * All the routines below use bits of fixup code that are out of line
76 * with the main instruction path. This means when everything is well,
77 * we don't even have to jump over them. Further, they do not intrude
78 * on our cache or tlb entries.
81 struct exception_table_entry
83 unsigned long insn, fixup;
86 /* Returns 0 if exception not found and fixup otherwise. */
87 extern unsigned long search_exception_table(unsigned long);
91 * These are the main single-value transfer routines. They automatically
92 * use the right size if we just have the right pointer type.
94 * This gets kind of ugly. We want to return _two_ values in "get_user()"
95 * and yet we don't want to do any pointers, because that is too much
96 * of a performance impact. Thus we have a few rather ugly macros here,
97 * and hide all the uglyness from the user.
99 * The "__xxx" versions of the user access functions are versions that
100 * do not verify the address space, that must have been done previously
101 * with a separate "access_ok()" call (this is used when we do multiple
102 * accesses to the same area of user memory).
105 extern void __get_user_1(void);
106 extern void __get_user_2(void);
107 extern void __get_user_4(void);
109 #define __get_user_x(size,ret,x,ptr) \
110 __asm__ __volatile__("call __get_user_" #size \
111 :"=a" (ret),"=d" (x) \
114 /* Careful: we have to cast the result to the type of the pointer for sign reasons */
115 #define get_user(x,ptr) \
116 ({ int __ret_gu,__val_gu; \
117 switch(sizeof (*(ptr))) { \
118 case 1: __get_user_x(1,__ret_gu,__val_gu,ptr); break; \
119 case 2: __get_user_x(2,__ret_gu,__val_gu,ptr); break; \
120 case 4: __get_user_x(4,__ret_gu,__val_gu,ptr); break; \
121 default: __get_user_x(X,__ret_gu,__val_gu,ptr); break; \
123 (x) = (__typeof__(*(ptr)))__val_gu; \
127 extern void __put_user_1(void);
128 extern void __put_user_2(void);
129 extern void __put_user_4(void);
130 extern void __put_user_8(void);
132 extern void __put_user_bad(void);
134 #define put_user(x,ptr) \
135 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
137 #define __get_user(x,ptr) \
138 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
139 #define __put_user(x,ptr) \
140 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
142 #define __put_user_nocheck(x,ptr,size) \
145 __put_user_size((x),(ptr),(size),__pu_err); \
150 #define __put_user_check(x,ptr,size) \
152 long __pu_err = -EFAULT; \
153 __typeof__(*(ptr)) *__pu_addr = (ptr); \
154 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
155 __put_user_size((x),__pu_addr,(size),__pu_err); \
159 #define __put_user_u64(x, addr, err) \
160 __asm__ __volatile__( \
161 "1: movl %%eax,0(%2)\n" \
162 "2: movl %%edx,4(%2)\n" \
164 ".section .fixup,\"ax\"\n" \
168 ".section __ex_table,\"a\"\n" \
174 : "A" (x), "r" (addr), "i"(-EFAULT), "0"(err))
176 #define __put_user_size(x,ptr,size,retval) \
180 case 1: __put_user_asm(x,ptr,retval,"b","b","iq"); break; \
181 case 2: __put_user_asm(x,ptr,retval,"w","w","ir"); break; \
182 case 4: __put_user_asm(x,ptr,retval,"l","","ir"); break; \
183 case 8: __put_user_u64(x,ptr,retval); break; \
184 default: __put_user_bad(); \
188 struct __large_struct { unsigned long buf[100]; };
189 #define __m(x) (*(struct __large_struct *)(x))
192 * Tell gcc we read from memory instead of writing: this is because
193 * we do not write to any memory gcc knows about, so there are no
196 #define __put_user_asm(x, addr, err, itype, rtype, ltype) \
197 __asm__ __volatile__( \
198 "1: mov"itype" %"rtype"1,%2\n" \
200 ".section .fixup,\"ax\"\n" \
204 ".section __ex_table,\"a\"\n" \
209 : ltype (x), "m"(__m(addr)), "i"(-EFAULT), "0"(err))
212 #define __get_user_nocheck(x,ptr,size) \
214 long __gu_err, __gu_val; \
215 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
216 (x) = (__typeof__(*(ptr)))__gu_val; \
220 extern long __get_user_bad(void);
222 #define __get_user_size(x,ptr,size,retval) \
226 case 1: __get_user_asm(x,ptr,retval,"b","b","=q"); break; \
227 case 2: __get_user_asm(x,ptr,retval,"w","w","=r"); break; \
228 case 4: __get_user_asm(x,ptr,retval,"l","","=r"); break; \
229 default: (x) = __get_user_bad(); \
233 #define __get_user_asm(x, addr, err, itype, rtype, ltype) \
234 __asm__ __volatile__( \
235 "1: mov"itype" %2,%"rtype"1\n" \
237 ".section .fixup,\"ax\"\n" \
239 " xor"itype" %"rtype"1,%"rtype"1\n" \
242 ".section __ex_table,\"a\"\n" \
246 : "=r"(err), ltype (x) \
247 : "m"(__m(addr)), "i"(-EFAULT), "0"(err))
251 * Copy To/From Userspace
254 /* Generic arbitrary sized copy. */
255 #define __copy_user(to,from,size) \
258 __asm__ __volatile__( \
263 ".section .fixup,\"ax\"\n" \
264 "3: lea 0(%3,%0,4),%0\n" \
267 ".section __ex_table,\"a\"\n" \
272 : "=&c"(size), "=&D" (__d0), "=&S" (__d1) \
273 : "r"(size & 3), "0"(size / 4), "1"(to), "2"(from) \
277 #define __copy_user_zeroing(to,from,size) \
280 __asm__ __volatile__( \
285 ".section .fixup,\"ax\"\n" \
286 "3: lea 0(%3,%0,4),%0\n" \
289 " xorl %%eax,%%eax\n" \
295 ".section __ex_table,\"a\"\n" \
300 : "=&c"(size), "=&D" (__d0), "=&S" (__d1) \
301 : "r"(size & 3), "0"(size / 4), "1"(to), "2"(from) \
305 /* We let the __ versions of copy_from/to_user inline, because they're often
306 * used in fast paths and have only a small space overhead.
308 static inline unsigned long
309 __generic_copy_from_user_nocheck(void *to, const void *from, unsigned long n)
311 __copy_user_zeroing(to,from,n);
315 static inline unsigned long
316 __generic_copy_to_user_nocheck(void *to, const void *from, unsigned long n)
318 __copy_user(to,from,n);
323 /* Optimize just a little bit when we know the size of the move. */
324 #define __constant_copy_user(to, from, size) \
327 switch (size & 3) { \
329 __asm__ __volatile__( \
332 ".section .fixup,\"ax\"\n" \
336 ".section __ex_table,\"a\"\n" \
340 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
341 : "1"(from), "2"(to), "0"(size/4) \
345 __asm__ __volatile__( \
349 ".section .fixup,\"ax\"\n" \
354 ".section __ex_table,\"a\"\n" \
359 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
360 : "1"(from), "2"(to), "0"(size/4) \
364 __asm__ __volatile__( \
368 ".section .fixup,\"ax\"\n" \
373 ".section __ex_table,\"a\"\n" \
378 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
379 : "1"(from), "2"(to), "0"(size/4) \
383 __asm__ __volatile__( \
388 ".section .fixup,\"ax\"\n" \
394 ".section __ex_table,\"a\"\n" \
400 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
401 : "1"(from), "2"(to), "0"(size/4) \
407 /* Optimize just a little bit when we know the size of the move. */
408 #define __constant_copy_user_zeroing(to, from, size) \
411 switch (size & 3) { \
413 __asm__ __volatile__( \
416 ".section .fixup,\"ax\"\n" \
419 " xorl %%eax,%%eax\n" \
426 ".section __ex_table,\"a\"\n" \
430 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
431 : "1"(from), "2"(to), "0"(size/4) \
435 __asm__ __volatile__( \
439 ".section .fixup,\"ax\"\n" \
442 " xorl %%eax,%%eax\n" \
451 " xorl %%eax,%%eax\n" \
457 ".section __ex_table,\"a\"\n" \
462 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
463 : "1"(from), "2"(to), "0"(size/4) \
467 __asm__ __volatile__( \
471 ".section .fixup,\"ax\"\n" \
474 " xorl %%eax,%%eax\n" \
483 " xorl %%eax,%%eax\n" \
489 ".section __ex_table,\"a\"\n" \
494 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
495 : "1"(from), "2"(to), "0"(size/4) \
499 __asm__ __volatile__( \
504 ".section .fixup,\"ax\"\n" \
507 " xorl %%eax,%%eax\n" \
517 " xorl %%eax,%%eax\n" \
524 " xorl %%eax,%%eax\n" \
530 ".section __ex_table,\"a\"\n" \
536 : "=c"(size), "=&S" (__d0), "=&D" (__d1)\
537 : "1"(from), "2"(to), "0"(size/4) \
543 unsigned long __generic_copy_to_user(void *, const void *, unsigned long);
544 unsigned long __generic_copy_from_user(void *, const void *, unsigned long);
546 static inline unsigned long
547 __constant_copy_to_user(void *to, const void *from, unsigned long n)
550 if (access_ok(VERIFY_WRITE, to, n))
551 __constant_copy_user(to,from,n);
555 static inline unsigned long
556 __constant_copy_from_user(void *to, const void *from, unsigned long n)
558 if (access_ok(VERIFY_READ, from, n))
559 __constant_copy_user_zeroing(to,from,n);
565 static inline unsigned long
566 __constant_copy_to_user_nocheck(void *to, const void *from, unsigned long n)
568 __constant_copy_user(to,from,n);
572 static inline unsigned long
573 __constant_copy_from_user_nocheck(void *to, const void *from, unsigned long n)
575 __constant_copy_user_zeroing(to,from,n);
579 #define copy_to_user(to,from,n) \
580 (__builtin_constant_p(n) ? \
581 __constant_copy_to_user((to),(from),(n)) : \
582 __generic_copy_to_user((to),(from),(n)))
584 #define copy_from_user(to,from,n) \
585 (__builtin_constant_p(n) ? \
586 __constant_copy_from_user((to),(from),(n)) : \
587 __generic_copy_from_user((to),(from),(n)))
589 #define __copy_to_user(to,from,n) \
590 (__builtin_constant_p(n) ? \
591 __constant_copy_to_user_nocheck((to),(from),(n)) : \
592 __generic_copy_to_user_nocheck((to),(from),(n)))
594 #define __copy_from_user(to,from,n) \
595 (__builtin_constant_p(n) ? \
596 __constant_copy_from_user_nocheck((to),(from),(n)) : \
597 __generic_copy_from_user_nocheck((to),(from),(n)))
599 long strncpy_from_user(char *dst, const char *src, long count);
600 long __strncpy_from_user(char *dst, const char *src, long count);
601 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
602 long strnlen_user(const char *str, long n);
603 unsigned long clear_user(void *mem, unsigned long len);
604 unsigned long __clear_user(void *mem, unsigned long len);
606 #endif /* __i386_UACCESS_H */