ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-x86_64 / uaccess.h
1 #ifndef __X86_64_UACCESS_H
2 #define __X86_64_UACCESS_H
3
4 /*
5  * User space memory access functions
6  */
7 #include <linux/config.h>
8 #include <linux/compiler.h>
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/prefetch.h>
12 #include <asm/page.h>
13
14 #define VERIFY_READ 0
15 #define VERIFY_WRITE 1
16
17 /*
18  * The fs value determines whether argument validity checking should be
19  * performed or not.  If get_fs() == USER_DS, checking is performed, with
20  * get_fs() == KERNEL_DS, checking is bypassed.
21  *
22  * For historical reasons, these macros are grossly misnamed.
23  */
24
25 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
26
27 #define KERNEL_DS       MAKE_MM_SEG(0xFFFFFFFFFFFFFFFF)
28 #define USER_DS         MAKE_MM_SEG(PAGE_OFFSET)
29
30 #define get_ds()        (KERNEL_DS)
31 #define get_fs()        (current_thread_info()->addr_limit)
32 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
33
34 #define segment_eq(a,b) ((a).seg == (b).seg)
35
36 #define __addr_ok(addr) (!((unsigned long)(addr) & (current_thread_info()->addr_limit.seg)))
37
38 /*
39  * Uhhuh, this needs 65-bit arithmetic. We have a carry..
40  */
41 #define __range_not_ok(addr,size) ({ \
42         unsigned long flag,sum; \
43         asm("# range_ok\n\r" \
44                 "addq %3,%1 ; sbbq %0,%0 ; cmpq %1,%4 ; sbbq $0,%0"  \
45                 :"=&r" (flag), "=r" (sum) \
46                 :"1" (addr),"g" ((long)(size)),"g" (current_thread_info()->addr_limit.seg)); \
47         flag; })
48
49 #define access_ok(type,addr,size) (__range_not_ok(addr,size) == 0)
50
51 extern inline int verify_area(int type, const void * addr, unsigned long size)
52 {
53         return access_ok(type,addr,size) ? 0 : -EFAULT;
54 }
55
56
57 /*
58  * The exception table consists of pairs of addresses: the first is the
59  * address of an instruction that is allowed to fault, and the second is
60  * the address at which the program should continue.  No registers are
61  * modified, so it is entirely up to the continuation code to figure out
62  * what to do.
63  *
64  * All the routines below use bits of fixup code that are out of line
65  * with the main instruction path.  This means when everything is well,
66  * we don't even have to jump over them.  Further, they do not intrude
67  * on our cache or tlb entries.
68  */
69
70 struct exception_table_entry
71 {
72         unsigned long insn, fixup;
73 };
74
75
76 /*
77  * These are the main single-value transfer routines.  They automatically
78  * use the right size if we just have the right pointer type.
79  *
80  * This gets kind of ugly. We want to return _two_ values in "get_user()"
81  * and yet we don't want to do any pointers, because that is too much
82  * of a performance impact. Thus we have a few rather ugly macros here,
83  * and hide all the ugliness from the user.
84  *
85  * The "__xxx" versions of the user access functions are versions that
86  * do not verify the address space, that must have been done previously
87  * with a separate "access_ok()" call (this is used when we do multiple
88  * accesses to the same area of user memory).
89  */
90
91 extern void __get_user_1(void);
92 extern void __get_user_2(void);
93 extern void __get_user_4(void);
94 extern void __get_user_8(void);
95
96 #define __get_user_x(size,ret,x,ptr) \
97         __asm__ __volatile__("call __get_user_" #size \
98                 :"=a" (ret),"=d" (x) \
99                 :"0" (ptr) \
100                 :"rbx")
101
102 /* Careful: we have to cast the result to the type of the pointer for sign reasons */
103 #define get_user(x,ptr)                                                 \
104 ({      long __val_gu;                                                  \
105         int __ret_gu;                                                   \
106         switch(sizeof (*(ptr))) {                                       \
107         case 1:  __get_user_x(1,__ret_gu,__val_gu,ptr); break;          \
108         case 2:  __get_user_x(2,__ret_gu,__val_gu,ptr); break;          \
109         case 4:  __get_user_x(4,__ret_gu,__val_gu,ptr); break;          \
110         case 8:  __get_user_x(8,__ret_gu,__val_gu,ptr); break;          \
111         default: __get_user_bad(); break;                               \
112         }                                                               \
113         (x) = (__typeof__(*(ptr)))__val_gu;                             \
114         __ret_gu;                                                       \
115 })
116
117 extern void __put_user_1(void);
118 extern void __put_user_2(void);
119 extern void __put_user_4(void);
120 extern void __put_user_8(void);
121
122 extern void __put_user_bad(void);
123
124 #define __put_user_x(size,ret,x,ptr)                                    \
125         __asm__ __volatile__("call __put_user_" #size                   \
126                 :"=a" (ret)                                             \
127                 :"0" (ptr),"d" (x)                                      \
128                 :"rbx")
129
130 #define put_user(x,ptr)                                                 \
131   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
132
133 #define __get_user(x,ptr) \
134   __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
135 #define __put_user(x,ptr) \
136   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
137
138 #define __put_user_nocheck(x,ptr,size)                  \
139 ({                                                      \
140         int __pu_err;                                   \
141         __put_user_size((x),(ptr),(size),__pu_err);     \
142         __pu_err;                                       \
143 })
144
145
146 #define __put_user_check(x,ptr,size)                    \
147 ({                                                      \
148         int __pu_err = -EFAULT;                         \
149         __typeof__(*(ptr)) *__pu_addr = (ptr);          \
150         if (likely(access_ok(VERIFY_WRITE,__pu_addr,size)))     \
151                 __put_user_size((x),__pu_addr,(size),__pu_err); \
152         __pu_err;                                       \
153 })
154
155 #define __put_user_size(x,ptr,size,retval)                              \
156 do {                                                                    \
157         retval = 0;                                                     \
158         switch (size) {                                                 \
159           case 1: __put_user_asm(x,ptr,retval,"b","b","iq",-EFAULT); break;\
160           case 2: __put_user_asm(x,ptr,retval,"w","w","ir",-EFAULT); break;\
161           case 4: __put_user_asm(x,ptr,retval,"l","k","ir",-EFAULT); break;\
162           case 8: __put_user_asm(x,ptr,retval,"q","","ir",-EFAULT); break;\
163           default: __put_user_bad();                                    \
164         }                                                               \
165 } while (0)
166
167 /* FIXME: this hack is definitely wrong -AK */
168 struct __large_struct { unsigned long buf[100]; };
169 #define __m(x) (*(struct __large_struct *)(x))
170
171 /*
172  * Tell gcc we read from memory instead of writing: this is because
173  * we do not write to any memory gcc knows about, so there are no
174  * aliasing issues.
175  */
176 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errno)        \
177         __asm__ __volatile__(                                   \
178                 "1:     mov"itype" %"rtype"1,%2\n"              \
179                 "2:\n"                                          \
180                 ".section .fixup,\"ax\"\n"                      \
181                 "3:     mov %3,%0\n"                            \
182                 "       jmp 2b\n"                               \
183                 ".previous\n"                                   \
184                 ".section __ex_table,\"a\"\n"                   \
185                 "       .align 8\n"                             \
186                 "       .quad 1b,3b\n"                          \
187                 ".previous"                                     \
188                 : "=r"(err)                                     \
189                 : ltype (x), "m"(__m(addr)), "i"(errno), "0"(err))
190
191
192 #define __get_user_nocheck(x,ptr,size)                          \
193 ({                                                              \
194         int __gu_err;                                           \
195         long __gu_val;                                          \
196         __get_user_size(__gu_val,(ptr),(size),__gu_err);        \
197         (x) = (__typeof__(*(ptr)))__gu_val;                     \
198         __gu_err;                                               \
199 })
200
201 extern int __get_user_bad(void);
202
203 #define __get_user_size(x,ptr,size,retval)                              \
204 do {                                                                    \
205         retval = 0;                                                     \
206         switch (size) {                                                 \
207           case 1: __get_user_asm(x,ptr,retval,"b","b","=q",-EFAULT); break;\
208           case 2: __get_user_asm(x,ptr,retval,"w","w","=r",-EFAULT); break;\
209           case 4: __get_user_asm(x,ptr,retval,"l","k","=r",-EFAULT); break;\
210           case 8: __get_user_asm(x,ptr,retval,"q","","=r",-EFAULT); break;\
211           default: (x) = __get_user_bad();                              \
212         }                                                               \
213 } while (0)
214
215 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errno)        \
216         __asm__ __volatile__(                                   \
217                 "1:     mov"itype" %2,%"rtype"1\n"              \
218                 "2:\n"                                          \
219                 ".section .fixup,\"ax\"\n"                      \
220                 "3:     mov %3,%0\n"                            \
221                 "       xor"itype" %"rtype"1,%"rtype"1\n"       \
222                 "       jmp 2b\n"                               \
223                 ".previous\n"                                   \
224                 ".section __ex_table,\"a\"\n"                   \
225                 "       .align 8\n"                             \
226                 "       .quad 1b,3b\n"                          \
227                 ".previous"                                     \
228                 : "=r"(err), ltype (x)                          \
229                 : "m"(__m(addr)), "i"(errno), "0"(err))
230
231 /*
232  * Copy To/From Userspace
233  */
234
235 /* Handles exceptions in both to and from, but doesn't do access_ok */
236 extern unsigned long copy_user_generic(void *to, const void *from, unsigned len); 
237
238 extern unsigned long copy_to_user(void *to, const void *from, unsigned len); 
239 extern unsigned long copy_from_user(void *to, const void *from, unsigned len); 
240 extern unsigned long copy_in_user(void *to, const void *from, unsigned len); 
241
242 static inline int __copy_from_user(void *dst, const void *src, unsigned size) 
243
244        int ret = 0;
245         if (!__builtin_constant_p(size))
246                 return copy_user_generic(dst,src,size);
247         switch (size) { 
248         case 1:__get_user_asm(*(u8*)dst,(u8 *)src,ret,"b","b","=q",1); 
249                 return ret;
250         case 2:__get_user_asm(*(u16*)dst,(u16*)src,ret,"w","w","=r",2);
251                 return ret;
252         case 4:__get_user_asm(*(u32*)dst,(u32*)src,ret,"l","k","=r",4);
253                 return ret;
254         case 8:__get_user_asm(*(u64*)dst,(u64*)src,ret,"q","","=r",8);
255                 return ret; 
256         case 10:
257                 __get_user_asm(*(u64*)dst,(u64*)src,ret,"q","","=r",16);
258                 if (unlikely(ret)) return ret;
259                 __get_user_asm(*(u16*)(8+(char*)dst),(u16*)(8+(char*)src),ret,"w","w","=r",2);
260                 return ret; 
261         case 16:
262                 __get_user_asm(*(u64*)dst,(u64*)src,ret,"q","","=r",16);
263                 if (unlikely(ret)) return ret;
264                 __get_user_asm(*(u64*)(8+(char*)dst),(u64*)(8+(char*)src),ret,"q","","=r",8);
265                 return ret; 
266         default:
267                 return copy_user_generic(dst,src,size); 
268         }
269 }       
270
271 static inline int __copy_to_user(void *dst, const void *src, unsigned size) 
272
273        int ret = 0;
274         if (!__builtin_constant_p(size))
275                 return copy_user_generic(dst,src,size);
276         switch (size) { 
277         case 1:__put_user_asm(*(u8*)src,(u8 *)dst,ret,"b","b","iq",1); 
278                 return ret;
279         case 2:__put_user_asm(*(u16*)src,(u16*)dst,ret,"w","w","ir",2);
280                 return ret;
281         case 4:__put_user_asm(*(u32*)src,(u32*)dst,ret,"l","k","ir",4);
282                 return ret;
283         case 8:__put_user_asm(*(u64*)src,(u64*)dst,ret,"q","","ir",8);
284                 return ret; 
285         case 10:
286                 __put_user_asm(*(u64*)src,(u64*)dst,ret,"q","","ir",10);
287                 if (unlikely(ret)) return ret;
288                 asm("":::"memory");
289                 __put_user_asm(4[(u16*)src],4+(u16*)dst,ret,"w","w","ir",2);
290                 return ret; 
291         case 16:
292                 __put_user_asm(*(u64*)src,(u64*)dst,ret,"q","","ir",16);
293                 if (unlikely(ret)) return ret;
294                 asm("":::"memory");
295                 __put_user_asm(1[(u64*)src],1+(u64*)dst,ret,"q","","ir",8);
296                 return ret; 
297         default:
298                 return copy_user_generic(dst,src,size); 
299         }
300 }       
301
302
303 static inline int __copy_in_user(void *dst, const void *src, unsigned size) 
304
305        int ret = 0;
306         if (!__builtin_constant_p(size))
307                 return copy_user_generic(dst,src,size);
308         switch (size) { 
309         case 1: { 
310                 u8 tmp;
311                 __get_user_asm(tmp,(u8 *)src,ret,"b","b","=q",1); 
312                 if (likely(!ret))
313                         __put_user_asm(tmp,(u8 *)dst,ret,"b","b","iq",1); 
314                 return ret;
315         }
316         case 2: { 
317                 u16 tmp;
318                 __get_user_asm(tmp,(u16 *)src,ret,"w","w","=r",2); 
319                 if (likely(!ret))
320                         __put_user_asm(tmp,(u16 *)dst,ret,"w","w","ir",2); 
321                 return ret;
322         }
323
324         case 4: { 
325                 u32 tmp;
326                 __get_user_asm(tmp,(u32 *)src,ret,"l","k","=r",4); 
327                 if (likely(!ret))
328                         __put_user_asm(tmp,(u32 *)dst,ret,"l","k","ir",4); 
329                 return ret;
330         }
331         case 8: { 
332                 u64 tmp;
333                 __get_user_asm(tmp,(u64 *)src,ret,"q","","=r",8); 
334                 if (likely(!ret))
335                         __put_user_asm(tmp,(u64 *)dst,ret,"q","","ir",8); 
336                 return ret;
337         }
338         default:
339                 return copy_user_generic(dst,src,size); 
340         }
341 }       
342
343 long strncpy_from_user(char *dst, const char *src, long count);
344 long __strncpy_from_user(char *dst, const char *src, long count);
345 long strnlen_user(const char *str, long n);
346 long strlen_user(const char *str);
347 unsigned long clear_user(void *mem, unsigned long len);
348 unsigned long __clear_user(void *mem, unsigned long len);
349
350 #endif /* __X86_64_UACCESS_H */