x86: add pte accessors for the global bit
[linux-2.6] / include / asm-x86 / uaccess_64.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/compiler.h>
8 #include <linux/errno.h>
9 #include <linux/prefetch.h>
10 #include <asm/page.h>
11
12 #define VERIFY_READ 0
13 #define VERIFY_WRITE 1
14
15 /*
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.
19  *
20  * For historical reasons, these macros are grossly misnamed.
21  */
22
23 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
24
25 #define KERNEL_DS       MAKE_MM_SEG(0xFFFFFFFFFFFFFFFFUL)
26 #define USER_DS         MAKE_MM_SEG(PAGE_OFFSET)
27
28 #define get_ds()        (KERNEL_DS)
29 #define get_fs()        (current_thread_info()->addr_limit)
30 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
31
32 #define segment_eq(a,b) ((a).seg == (b).seg)
33
34 #define __addr_ok(addr) (!((unsigned long)(addr) & (current_thread_info()->addr_limit.seg)))
35
36 /*
37  * Uhhuh, this needs 65-bit arithmetic. We have a carry..
38  */
39 #define __range_not_ok(addr,size) ({ \
40         unsigned long flag,roksum; \
41         __chk_user_ptr(addr); \
42         asm("# range_ok\n\r" \
43                 "addq %3,%1 ; sbbq %0,%0 ; cmpq %1,%4 ; sbbq $0,%0"  \
44                 :"=&r" (flag), "=r" (roksum) \
45                 :"1" (addr),"g" ((long)(size)),"g" (current_thread_info()->addr_limit.seg)); \
46         flag; })
47
48 #define access_ok(type, addr, size) (__range_not_ok(addr,size) == 0)
49
50 /*
51  * The exception table consists of pairs of addresses: the first is the
52  * address of an instruction that is allowed to fault, and the second is
53  * the address at which the program should continue.  No registers are
54  * modified, so it is entirely up to the continuation code to figure out
55  * what to do.
56  *
57  * All the routines below use bits of fixup code that are out of line
58  * with the main instruction path.  This means when everything is well,
59  * we don't even have to jump over them.  Further, they do not intrude
60  * on our cache or tlb entries.
61  */
62
63 struct exception_table_entry
64 {
65         unsigned long insn, fixup;
66 };
67
68 extern int fixup_exception(struct pt_regs *regs);
69
70 #define ARCH_HAS_SEARCH_EXTABLE
71
72 /*
73  * These are the main single-value transfer routines.  They automatically
74  * use the right size if we just have the right pointer type.
75  *
76  * This gets kind of ugly. We want to return _two_ values in "get_user()"
77  * and yet we don't want to do any pointers, because that is too much
78  * of a performance impact. Thus we have a few rather ugly macros here,
79  * and hide all the ugliness from the user.
80  *
81  * The "__xxx" versions of the user access functions are versions that
82  * do not verify the address space, that must have been done previously
83  * with a separate "access_ok()" call (this is used when we do multiple
84  * accesses to the same area of user memory).
85  */
86
87 #define __get_user_x(size,ret,x,ptr) \
88         asm volatile("call __get_user_" #size \
89                 :"=a" (ret),"=d" (x) \
90                 :"c" (ptr) \
91                 :"r8")
92
93 /* Careful: we have to cast the result to the type of the pointer for sign reasons */
94 #define get_user(x,ptr)                                                 \
95 ({      unsigned long __val_gu;                                         \
96         int __ret_gu;                                                   \
97         __chk_user_ptr(ptr);                                            \
98         switch(sizeof (*(ptr))) {                                       \
99         case 1:  __get_user_x(1,__ret_gu,__val_gu,ptr); break;          \
100         case 2:  __get_user_x(2,__ret_gu,__val_gu,ptr); break;          \
101         case 4:  __get_user_x(4,__ret_gu,__val_gu,ptr); break;          \
102         case 8:  __get_user_x(8,__ret_gu,__val_gu,ptr); break;          \
103         default: __get_user_bad(); break;                               \
104         }                                                               \
105         (x) = (__force typeof(*(ptr)))__val_gu;                         \
106         __ret_gu;                                                       \
107 })
108
109 extern void __put_user_1(void);
110 extern void __put_user_2(void);
111 extern void __put_user_4(void);
112 extern void __put_user_8(void);
113 extern void __put_user_bad(void);
114
115 #define __put_user_x(size,ret,x,ptr)                                    \
116         asm volatile("call __put_user_" #size                   \
117                 :"=a" (ret)                                             \
118                 :"c" (ptr),"d" (x)                                      \
119                 :"r8")
120
121 #define put_user(x,ptr)                                                 \
122   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
123
124 #define __get_user(x,ptr) \
125   __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
126 #define __put_user(x,ptr) \
127   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
128
129 #define __get_user_unaligned __get_user
130 #define __put_user_unaligned __put_user
131
132 #define __put_user_nocheck(x,ptr,size)                  \
133 ({                                                      \
134         int __pu_err;                                   \
135         __put_user_size((x),(ptr),(size),__pu_err);     \
136         __pu_err;                                       \
137 })
138
139
140 #define __put_user_check(x,ptr,size)                    \
141 ({                                                      \
142         int __pu_err;                                   \
143         typeof(*(ptr)) __user *__pu_addr = (ptr);       \
144         switch (size) {                                 \
145         case 1: __put_user_x(1,__pu_err,x,__pu_addr); break;    \
146         case 2: __put_user_x(2,__pu_err,x,__pu_addr); break;    \
147         case 4: __put_user_x(4,__pu_err,x,__pu_addr); break;    \
148         case 8: __put_user_x(8,__pu_err,x,__pu_addr); break;    \
149         default: __put_user_bad();                      \
150         }                                               \
151         __pu_err;                                       \
152 })
153
154 #define __put_user_size(x,ptr,size,retval)                              \
155 do {                                                                    \
156         retval = 0;                                                     \
157         __chk_user_ptr(ptr);                                            \
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","","Zr",-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 __user *)(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         unsigned long __gu_val;                                 \
196         __get_user_size(__gu_val,(ptr),(size),__gu_err);        \
197         (x) = (__force typeof(*(ptr)))__gu_val;                 \
198         __gu_err;                                               \
199 })
200
201 extern int __get_user_1(void);
202 extern int __get_user_2(void);
203 extern int __get_user_4(void);
204 extern int __get_user_8(void);
205 extern int __get_user_bad(void);
206
207 #define __get_user_size(x,ptr,size,retval)                              \
208 do {                                                                    \
209         retval = 0;                                                     \
210         __chk_user_ptr(ptr);                                            \
211         switch (size) {                                                 \
212           case 1: __get_user_asm(x,ptr,retval,"b","b","=q",-EFAULT); break;\
213           case 2: __get_user_asm(x,ptr,retval,"w","w","=r",-EFAULT); break;\
214           case 4: __get_user_asm(x,ptr,retval,"l","k","=r",-EFAULT); break;\
215           case 8: __get_user_asm(x,ptr,retval,"q","","=r",-EFAULT); break;\
216           default: (x) = __get_user_bad();                              \
217         }                                                               \
218 } while (0)
219
220 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errno)        \
221         asm volatile(                                   \
222                 "1:     mov"itype" %2,%"rtype"1\n"              \
223                 "2:\n"                                          \
224                 ".section .fixup,\"ax\"\n"                      \
225                 "3:     mov %3,%0\n"                            \
226                 "       xor"itype" %"rtype"1,%"rtype"1\n"       \
227                 "       jmp 2b\n"                               \
228                 ".previous\n"                                   \
229                 ".section __ex_table,\"a\"\n"                   \
230                 "       .align 8\n"                             \
231                 "       .quad 1b,3b\n"                          \
232                 ".previous"                                     \
233                 : "=r"(err), ltype (x)                          \
234                 : "m"(__m(addr)), "i"(errno), "0"(err))
235
236 /*
237  * Copy To/From Userspace
238  */
239
240 /* Handles exceptions in both to and from, but doesn't do access_ok */
241 __must_check unsigned long
242 copy_user_generic(void *to, const void *from, unsigned len);
243
244 __must_check unsigned long
245 copy_to_user(void __user *to, const void *from, unsigned len);
246 __must_check unsigned long
247 copy_from_user(void *to, const void __user *from, unsigned len);
248 __must_check unsigned long
249 copy_in_user(void __user *to, const void __user *from, unsigned len);
250
251 static __always_inline __must_check
252 int __copy_from_user(void *dst, const void __user *src, unsigned size)
253
254         int ret = 0;
255         if (!__builtin_constant_p(size))
256                 return copy_user_generic(dst,(__force void *)src,size);
257         switch (size) { 
258         case 1:__get_user_asm(*(u8*)dst,(u8 __user *)src,ret,"b","b","=q",1); 
259                 return ret;
260         case 2:__get_user_asm(*(u16*)dst,(u16 __user *)src,ret,"w","w","=r",2);
261                 return ret;
262         case 4:__get_user_asm(*(u32*)dst,(u32 __user *)src,ret,"l","k","=r",4);
263                 return ret;
264         case 8:__get_user_asm(*(u64*)dst,(u64 __user *)src,ret,"q","","=r",8);
265                 return ret; 
266         case 10:
267                 __get_user_asm(*(u64*)dst,(u64 __user *)src,ret,"q","","=r",16);
268                 if (unlikely(ret)) return ret;
269                 __get_user_asm(*(u16*)(8+(char*)dst),(u16 __user *)(8+(char __user *)src),ret,"w","w","=r",2);
270                 return ret; 
271         case 16:
272                 __get_user_asm(*(u64*)dst,(u64 __user *)src,ret,"q","","=r",16);
273                 if (unlikely(ret)) return ret;
274                 __get_user_asm(*(u64*)(8+(char*)dst),(u64 __user *)(8+(char __user *)src),ret,"q","","=r",8);
275                 return ret; 
276         default:
277                 return copy_user_generic(dst,(__force void *)src,size); 
278         }
279 }       
280
281 static __always_inline __must_check
282 int __copy_to_user(void __user *dst, const void *src, unsigned size)
283
284         int ret = 0;
285         if (!__builtin_constant_p(size))
286                 return copy_user_generic((__force void *)dst,src,size);
287         switch (size) { 
288         case 1:__put_user_asm(*(u8*)src,(u8 __user *)dst,ret,"b","b","iq",1); 
289                 return ret;
290         case 2:__put_user_asm(*(u16*)src,(u16 __user *)dst,ret,"w","w","ir",2);
291                 return ret;
292         case 4:__put_user_asm(*(u32*)src,(u32 __user *)dst,ret,"l","k","ir",4);
293                 return ret;
294         case 8:__put_user_asm(*(u64*)src,(u64 __user *)dst,ret,"q","","ir",8);
295                 return ret; 
296         case 10:
297                 __put_user_asm(*(u64*)src,(u64 __user *)dst,ret,"q","","ir",10);
298                 if (unlikely(ret)) return ret;
299                 asm("":::"memory");
300                 __put_user_asm(4[(u16*)src],4+(u16 __user *)dst,ret,"w","w","ir",2);
301                 return ret; 
302         case 16:
303                 __put_user_asm(*(u64*)src,(u64 __user *)dst,ret,"q","","ir",16);
304                 if (unlikely(ret)) return ret;
305                 asm("":::"memory");
306                 __put_user_asm(1[(u64*)src],1+(u64 __user *)dst,ret,"q","","ir",8);
307                 return ret; 
308         default:
309                 return copy_user_generic((__force void *)dst,src,size); 
310         }
311 }       
312
313 static __always_inline __must_check
314 int __copy_in_user(void __user *dst, const void __user *src, unsigned size)
315
316         int ret = 0;
317         if (!__builtin_constant_p(size))
318                 return copy_user_generic((__force void *)dst,(__force void *)src,size);
319         switch (size) { 
320         case 1: { 
321                 u8 tmp;
322                 __get_user_asm(tmp,(u8 __user *)src,ret,"b","b","=q",1); 
323                 if (likely(!ret))
324                         __put_user_asm(tmp,(u8 __user *)dst,ret,"b","b","iq",1); 
325                 return ret;
326         }
327         case 2: { 
328                 u16 tmp;
329                 __get_user_asm(tmp,(u16 __user *)src,ret,"w","w","=r",2); 
330                 if (likely(!ret))
331                         __put_user_asm(tmp,(u16 __user *)dst,ret,"w","w","ir",2); 
332                 return ret;
333         }
334
335         case 4: { 
336                 u32 tmp;
337                 __get_user_asm(tmp,(u32 __user *)src,ret,"l","k","=r",4); 
338                 if (likely(!ret))
339                         __put_user_asm(tmp,(u32 __user *)dst,ret,"l","k","ir",4); 
340                 return ret;
341         }
342         case 8: { 
343                 u64 tmp;
344                 __get_user_asm(tmp,(u64 __user *)src,ret,"q","","=r",8); 
345                 if (likely(!ret))
346                         __put_user_asm(tmp,(u64 __user *)dst,ret,"q","","ir",8); 
347                 return ret;
348         }
349         default:
350                 return copy_user_generic((__force void *)dst,(__force void *)src,size); 
351         }
352 }       
353
354 __must_check long 
355 strncpy_from_user(char *dst, const char __user *src, long count);
356 __must_check long 
357 __strncpy_from_user(char *dst, const char __user *src, long count);
358 __must_check long strnlen_user(const char __user *str, long n);
359 __must_check long __strnlen_user(const char __user *str, long n);
360 __must_check long strlen_user(const char __user *str);
361 __must_check unsigned long clear_user(void __user *mem, unsigned long len);
362 __must_check unsigned long __clear_user(void __user *mem, unsigned long len);
363
364 __must_check long __copy_from_user_inatomic(void *dst, const void __user *src, unsigned size);
365
366 static __must_check __always_inline int
367 __copy_to_user_inatomic(void __user *dst, const void *src, unsigned size)
368 {
369         return copy_user_generic((__force void *)dst, src, size);
370 }
371
372 #define ARCH_HAS_NOCACHE_UACCESS 1
373 extern long __copy_user_nocache(void *dst, const void __user *src, unsigned size, int zerorest);
374
375 static inline int __copy_from_user_nocache(void *dst, const void __user *src, unsigned size)
376 {
377         might_sleep();
378         return __copy_user_nocache(dst, src, size, 1);
379 }
380
381 static inline int __copy_from_user_inatomic_nocache(void *dst, const void __user *src, unsigned size)
382 {
383         return __copy_user_nocache(dst, src, size, 0);
384 }
385
386 #endif /* __X86_64_UACCESS_H */