blob: b87eb4ba8f9d6e20ff345eda0fcc78e88a807df5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __X86_64_UACCESS_H
2#define __X86_64_UACCESS_H
3
4/*
5 * User space memory access functions
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/compiler.h>
8#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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) ({ \
Tilman Schmidt16cf5b32007-02-10 01:45:41 -080040 unsigned long flag,roksum; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 __chk_user_ptr(addr); \
42 asm("# range_ok\n\r" \
43 "addq %3,%1 ; sbbq %0,%0 ; cmpq %1,%4 ; sbbq $0,%0" \
Tilman Schmidt16cf5b32007-02-10 01:45:41 -080044 :"=&r" (flag), "=r" (roksum) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 :"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
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
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
63struct exception_table_entry
64{
65 unsigned long insn, fixup;
66};
67
Harvey Harrison85f2adf2008-01-30 13:31:42 +010068extern int fixup_exception(struct pt_regs *regs);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#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) \
Andi Kleen383d0792006-09-26 10:52:40 +020088 asm volatile("call __get_user_" #size \
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 :"=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 } \
Al Virofdd33962007-07-26 17:35:39 +0100105 (x) = (__force typeof(*(ptr)))__val_gu; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 __ret_gu; \
107})
108
109extern void __put_user_1(void);
110extern void __put_user_2(void);
111extern void __put_user_4(void);
112extern void __put_user_8(void);
113extern void __put_user_bad(void);
114
115#define __put_user_x(size,ret,x,ptr) \
Andi Kleen383d0792006-09-26 10:52:40 +0200116 asm volatile("call __put_user_" #size \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 :"=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; \
Andi Kleen383d0792006-09-26 10:52:40 +0200143 typeof(*(ptr)) __user *__pu_addr = (ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 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) \
155do { \
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;\
Roland McGrath5ad0d382007-01-25 17:19:51 -0800162 case 8: __put_user_asm(x,ptr,retval,"q","","Zr",-EFAULT); break;\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 default: __put_user_bad(); \
164 } \
165} while (0)
166
167/* FIXME: this hack is definitely wrong -AK */
168struct __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) \
Andi Kleen383d0792006-09-26 10:52:40 +0200177 asm volatile( \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 "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" \
H. Peter Anvin71713eee2008-02-04 16:47:59 +0100184 _ASM_EXTABLE(1b,3b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 : "=r"(err) \
186 : ltype (x), "m"(__m(addr)), "i"(errno), "0"(err))
187
188
189#define __get_user_nocheck(x,ptr,size) \
190({ \
191 int __gu_err; \
192 unsigned long __gu_val; \
193 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
Al Virofdd33962007-07-26 17:35:39 +0100194 (x) = (__force typeof(*(ptr)))__gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 __gu_err; \
196})
197
198extern int __get_user_1(void);
199extern int __get_user_2(void);
200extern int __get_user_4(void);
201extern int __get_user_8(void);
202extern int __get_user_bad(void);
203
204#define __get_user_size(x,ptr,size,retval) \
205do { \
206 retval = 0; \
207 __chk_user_ptr(ptr); \
208 switch (size) { \
209 case 1: __get_user_asm(x,ptr,retval,"b","b","=q",-EFAULT); break;\
210 case 2: __get_user_asm(x,ptr,retval,"w","w","=r",-EFAULT); break;\
211 case 4: __get_user_asm(x,ptr,retval,"l","k","=r",-EFAULT); break;\
212 case 8: __get_user_asm(x,ptr,retval,"q","","=r",-EFAULT); break;\
213 default: (x) = __get_user_bad(); \
214 } \
215} while (0)
216
217#define __get_user_asm(x, addr, err, itype, rtype, ltype, errno) \
Andi Kleen383d0792006-09-26 10:52:40 +0200218 asm volatile( \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 "1: mov"itype" %2,%"rtype"1\n" \
220 "2:\n" \
221 ".section .fixup,\"ax\"\n" \
222 "3: mov %3,%0\n" \
223 " xor"itype" %"rtype"1,%"rtype"1\n" \
224 " jmp 2b\n" \
225 ".previous\n" \
H. Peter Anvin71713eee2008-02-04 16:47:59 +0100226 _ASM_EXTABLE(1b,3b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 : "=r"(err), ltype (x) \
228 : "m"(__m(addr)), "i"(errno), "0"(err))
229
230/*
231 * Copy To/From Userspace
232 */
233
234/* Handles exceptions in both to and from, but doesn't do access_ok */
Andi Kleen95912002006-09-26 10:52:39 +0200235__must_check unsigned long
236copy_user_generic(void *to, const void *from, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Andi Kleen95912002006-09-26 10:52:39 +0200238__must_check unsigned long
239copy_to_user(void __user *to, const void *from, unsigned len);
240__must_check unsigned long
241copy_from_user(void *to, const void __user *from, unsigned len);
242__must_check unsigned long
243copy_in_user(void __user *to, const void __user *from, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Andi Kleen95912002006-09-26 10:52:39 +0200245static __always_inline __must_check
246int __copy_from_user(void *dst, const void __user *src, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Andi Kleen383d0792006-09-26 10:52:40 +0200248 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!__builtin_constant_p(size))
250 return copy_user_generic(dst,(__force void *)src,size);
251 switch (size) {
252 case 1:__get_user_asm(*(u8*)dst,(u8 __user *)src,ret,"b","b","=q",1);
253 return ret;
254 case 2:__get_user_asm(*(u16*)dst,(u16 __user *)src,ret,"w","w","=r",2);
255 return ret;
256 case 4:__get_user_asm(*(u32*)dst,(u32 __user *)src,ret,"l","k","=r",4);
257 return ret;
258 case 8:__get_user_asm(*(u64*)dst,(u64 __user *)src,ret,"q","","=r",8);
259 return ret;
260 case 10:
261 __get_user_asm(*(u64*)dst,(u64 __user *)src,ret,"q","","=r",16);
262 if (unlikely(ret)) return ret;
263 __get_user_asm(*(u16*)(8+(char*)dst),(u16 __user *)(8+(char __user *)src),ret,"w","w","=r",2);
264 return ret;
265 case 16:
266 __get_user_asm(*(u64*)dst,(u64 __user *)src,ret,"q","","=r",16);
267 if (unlikely(ret)) return ret;
268 __get_user_asm(*(u64*)(8+(char*)dst),(u64 __user *)(8+(char __user *)src),ret,"q","","=r",8);
269 return ret;
270 default:
271 return copy_user_generic(dst,(__force void *)src,size);
272 }
273}
274
Andi Kleen95912002006-09-26 10:52:39 +0200275static __always_inline __must_check
276int __copy_to_user(void __user *dst, const void *src, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Andi Kleen383d0792006-09-26 10:52:40 +0200278 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!__builtin_constant_p(size))
280 return copy_user_generic((__force void *)dst,src,size);
281 switch (size) {
282 case 1:__put_user_asm(*(u8*)src,(u8 __user *)dst,ret,"b","b","iq",1);
283 return ret;
284 case 2:__put_user_asm(*(u16*)src,(u16 __user *)dst,ret,"w","w","ir",2);
285 return ret;
286 case 4:__put_user_asm(*(u32*)src,(u32 __user *)dst,ret,"l","k","ir",4);
287 return ret;
288 case 8:__put_user_asm(*(u64*)src,(u64 __user *)dst,ret,"q","","ir",8);
289 return ret;
290 case 10:
291 __put_user_asm(*(u64*)src,(u64 __user *)dst,ret,"q","","ir",10);
292 if (unlikely(ret)) return ret;
293 asm("":::"memory");
294 __put_user_asm(4[(u16*)src],4+(u16 __user *)dst,ret,"w","w","ir",2);
295 return ret;
296 case 16:
297 __put_user_asm(*(u64*)src,(u64 __user *)dst,ret,"q","","ir",16);
298 if (unlikely(ret)) return ret;
299 asm("":::"memory");
300 __put_user_asm(1[(u64*)src],1+(u64 __user *)dst,ret,"q","","ir",8);
301 return ret;
302 default:
303 return copy_user_generic((__force void *)dst,src,size);
304 }
305}
306
Andi Kleen95912002006-09-26 10:52:39 +0200307static __always_inline __must_check
308int __copy_in_user(void __user *dst, const void __user *src, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Andi Kleen383d0792006-09-26 10:52:40 +0200310 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!__builtin_constant_p(size))
312 return copy_user_generic((__force void *)dst,(__force void *)src,size);
313 switch (size) {
314 case 1: {
315 u8 tmp;
316 __get_user_asm(tmp,(u8 __user *)src,ret,"b","b","=q",1);
317 if (likely(!ret))
318 __put_user_asm(tmp,(u8 __user *)dst,ret,"b","b","iq",1);
319 return ret;
320 }
321 case 2: {
322 u16 tmp;
323 __get_user_asm(tmp,(u16 __user *)src,ret,"w","w","=r",2);
324 if (likely(!ret))
325 __put_user_asm(tmp,(u16 __user *)dst,ret,"w","w","ir",2);
326 return ret;
327 }
328
329 case 4: {
330 u32 tmp;
331 __get_user_asm(tmp,(u32 __user *)src,ret,"l","k","=r",4);
332 if (likely(!ret))
333 __put_user_asm(tmp,(u32 __user *)dst,ret,"l","k","ir",4);
334 return ret;
335 }
336 case 8: {
337 u64 tmp;
338 __get_user_asm(tmp,(u64 __user *)src,ret,"q","","=r",8);
339 if (likely(!ret))
340 __put_user_asm(tmp,(u64 __user *)dst,ret,"q","","ir",8);
341 return ret;
342 }
343 default:
344 return copy_user_generic((__force void *)dst,(__force void *)src,size);
345 }
346}
347
Andi Kleen95912002006-09-26 10:52:39 +0200348__must_check long
349strncpy_from_user(char *dst, const char __user *src, long count);
350__must_check long
351__strncpy_from_user(char *dst, const char __user *src, long count);
352__must_check long strnlen_user(const char __user *str, long n);
353__must_check long __strnlen_user(const char __user *str, long n);
354__must_check long strlen_user(const char __user *str);
355__must_check unsigned long clear_user(void __user *mem, unsigned long len);
356__must_check unsigned long __clear_user(void __user *mem, unsigned long len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Andi Kleen95912002006-09-26 10:52:39 +0200358__must_check long __copy_from_user_inatomic(void *dst, const void __user *src, unsigned size);
Andi Kleenb8858082006-09-30 01:47:55 +0200359
360static __must_check __always_inline int
361__copy_to_user_inatomic(void __user *dst, const void *src, unsigned size)
362{
363 return copy_user_generic((__force void *)dst, src, size);
364}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Andi Kleen0812a572007-02-13 13:26:19 +0100366#define ARCH_HAS_NOCACHE_UACCESS 1
367extern long __copy_user_nocache(void *dst, const void __user *src, unsigned size, int zerorest);
368
369static inline int __copy_from_user_nocache(void *dst, const void __user *src, unsigned size)
370{
371 might_sleep();
Al Viroecb75242007-03-14 09:20:20 +0000372 return __copy_user_nocache(dst, src, size, 1);
Andi Kleen0812a572007-02-13 13:26:19 +0100373}
374
375static inline int __copy_from_user_inatomic_nocache(void *dst, const void __user *src, unsigned size)
376{
Al Viroecb75242007-03-14 09:20:20 +0000377 return __copy_user_nocache(dst, src, size, 0);
Andi Kleen0812a572007-02-13 13:26:19 +0100378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#endif /* __X86_64_UACCESS_H */