1 #ifndef __X86_64_UACCESS_H
2 #define __X86_64_UACCESS_H
5 * User space memory access functions
7 #include <linux/compiler.h>
8 #include <linux/errno.h>
9 #include <linux/sched.h>
10 #include <linux/prefetch.h>
14 #define VERIFY_WRITE 1
17 * The fs value determines whether argument validity checking should be
18 * performed or not. If get_fs() == USER_DS, checking is performed, with
19 * get_fs() == KERNEL_DS, checking is bypassed.
21 * For historical reasons, these macros are grossly misnamed.
24 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
26 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFFFFFFFFFUL)
27 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
29 #define get_ds() (KERNEL_DS)
30 #define get_fs() (current_thread_info()->addr_limit)
31 #define set_fs(x) (current_thread_info()->addr_limit = (x))
33 #define segment_eq(a,b) ((a).seg == (b).seg)
35 #define __addr_ok(addr) (!((unsigned long)(addr) & (current_thread_info()->addr_limit.seg)))
38 * Uhhuh, this needs 65-bit arithmetic. We have a carry..
40 #define __range_not_ok(addr,size) ({ \
41 unsigned long flag,sum; \
42 __chk_user_ptr(addr); \
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)); \
49 #define access_ok(type, addr, size) (__range_not_ok(addr,size) == 0)
52 * The exception table consists of pairs of addresses: the first is the
53 * address of an instruction that is allowed to fault, and the second is
54 * the address at which the program should continue. No registers are
55 * modified, so it is entirely up to the continuation code to figure out
58 * All the routines below use bits of fixup code that are out of line
59 * with the main instruction path. This means when everything is well,
60 * we don't even have to jump over them. Further, they do not intrude
61 * on our cache or tlb entries.
64 struct exception_table_entry
66 unsigned long insn
, fixup
;
69 #define ARCH_HAS_SEARCH_EXTABLE
72 * These are the main single-value transfer routines. They automatically
73 * use the right size if we just have the right pointer type.
75 * This gets kind of ugly. We want to return _two_ values in "get_user()"
76 * and yet we don't want to do any pointers, because that is too much
77 * of a performance impact. Thus we have a few rather ugly macros here,
78 * and hide all the ugliness from the user.
80 * The "__xxx" versions of the user access functions are versions that
81 * do not verify the address space, that must have been done previously
82 * with a separate "access_ok()" call (this is used when we do multiple
83 * accesses to the same area of user memory).
86 #define __get_user_x(size,ret,x,ptr) \
87 asm volatile("call __get_user_" #size \
88 :"=a" (ret),"=d" (x) \
92 /* Careful: we have to cast the result to the type of the pointer for sign reasons */
93 #define get_user(x,ptr) \
94 ({ unsigned long __val_gu; \
96 __chk_user_ptr(ptr); \
97 switch(sizeof (*(ptr))) { \
98 case 1: __get_user_x(1,__ret_gu,__val_gu,ptr); break; \
99 case 2: __get_user_x(2,__ret_gu,__val_gu,ptr); break; \
100 case 4: __get_user_x(4,__ret_gu,__val_gu,ptr); break; \
101 case 8: __get_user_x(8,__ret_gu,__val_gu,ptr); break; \
102 default: __get_user_bad(); break; \
104 (x) = (typeof(*(ptr)))__val_gu; \
108 extern void __put_user_1(void);
109 extern void __put_user_2(void);
110 extern void __put_user_4(void);
111 extern void __put_user_8(void);
112 extern void __put_user_bad(void);
114 #define __put_user_x(size,ret,x,ptr) \
115 asm volatile("call __put_user_" #size \
120 #define put_user(x,ptr) \
121 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
123 #define __get_user(x,ptr) \
124 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
125 #define __put_user(x,ptr) \
126 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
128 #define __get_user_unaligned __get_user
129 #define __put_user_unaligned __put_user
131 #define __put_user_nocheck(x,ptr,size) \
134 __put_user_size((x),(ptr),(size),__pu_err); \
139 #define __put_user_check(x,ptr,size) \
142 typeof(*(ptr)) __user *__pu_addr = (ptr); \
144 case 1: __put_user_x(1,__pu_err,x,__pu_addr); break; \
145 case 2: __put_user_x(2,__pu_err,x,__pu_addr); break; \
146 case 4: __put_user_x(4,__pu_err,x,__pu_addr); break; \
147 case 8: __put_user_x(8,__pu_err,x,__pu_addr); break; \
148 default: __put_user_bad(); \
153 #define __put_user_size(x,ptr,size,retval) \
156 __chk_user_ptr(ptr); \
158 case 1: __put_user_asm(x,ptr,retval,"b","b","iq",-EFAULT); break;\
159 case 2: __put_user_asm(x,ptr,retval,"w","w","ir",-EFAULT); break;\
160 case 4: __put_user_asm(x,ptr,retval,"l","k","ir",-EFAULT); break;\
161 case 8: __put_user_asm(x,ptr,retval,"q","","ir",-EFAULT); break;\
162 default: __put_user_bad(); \
166 /* FIXME: this hack is definitely wrong -AK */
167 struct __large_struct
{ unsigned long buf
[100]; };
168 #define __m(x) (*(struct __large_struct __user *)(x))
171 * Tell gcc we read from memory instead of writing: this is because
172 * we do not write to any memory gcc knows about, so there are no
175 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errno) \
177 "1: mov"itype" %"rtype"1,%2\n" \
179 ".section .fixup,\"ax\"\n" \
183 ".section __ex_table,\"a\"\n" \
188 : ltype (x), "m"(__m(addr)), "i"(errno), "0"(err))
191 #define __get_user_nocheck(x,ptr,size) \
194 unsigned long __gu_val; \
195 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
196 (x) = (typeof(*(ptr)))__gu_val; \
200 extern int __get_user_1(void);
201 extern int __get_user_2(void);
202 extern int __get_user_4(void);
203 extern int __get_user_8(void);
204 extern int __get_user_bad(void);
206 #define __get_user_size(x,ptr,size,retval) \
209 __chk_user_ptr(ptr); \
211 case 1: __get_user_asm(x,ptr,retval,"b","b","=q",-EFAULT); break;\
212 case 2: __get_user_asm(x,ptr,retval,"w","w","=r",-EFAULT); break;\
213 case 4: __get_user_asm(x,ptr,retval,"l","k","=r",-EFAULT); break;\
214 case 8: __get_user_asm(x,ptr,retval,"q","","=r",-EFAULT); break;\
215 default: (x) = __get_user_bad(); \
219 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errno) \
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"(errno), "0"(err))
236 * Copy To/From Userspace
239 /* Handles exceptions in both to and from, but doesn't do access_ok */
240 __must_check
unsigned long
241 copy_user_generic(void *to
, const void *from
, unsigned len
);
243 __must_check
unsigned long
244 copy_to_user(void __user
*to
, const void *from
, unsigned len
);
245 __must_check
unsigned long
246 copy_from_user(void *to
, const void __user
*from
, unsigned len
);
247 __must_check
unsigned long
248 copy_in_user(void __user
*to
, const void __user
*from
, unsigned len
);
250 static __always_inline __must_check
251 int __copy_from_user(void *dst
, const void __user
*src
, unsigned size
)
254 if (!__builtin_constant_p(size
))
255 return copy_user_generic(dst
,(__force
void *)src
,size
);
257 case 1:__get_user_asm(*(u8
*)dst
,(u8 __user
*)src
,ret
,"b","b","=q",1);
259 case 2:__get_user_asm(*(u16
*)dst
,(u16 __user
*)src
,ret
,"w","w","=r",2);
261 case 4:__get_user_asm(*(u32
*)dst
,(u32 __user
*)src
,ret
,"l","k","=r",4);
263 case 8:__get_user_asm(*(u64
*)dst
,(u64 __user
*)src
,ret
,"q","","=r",8);
266 __get_user_asm(*(u64
*)dst
,(u64 __user
*)src
,ret
,"q","","=r",16);
267 if (unlikely(ret
)) return ret
;
268 __get_user_asm(*(u16
*)(8+(char*)dst
),(u16 __user
*)(8+(char __user
*)src
),ret
,"w","w","=r",2);
271 __get_user_asm(*(u64
*)dst
,(u64 __user
*)src
,ret
,"q","","=r",16);
272 if (unlikely(ret
)) return ret
;
273 __get_user_asm(*(u64
*)(8+(char*)dst
),(u64 __user
*)(8+(char __user
*)src
),ret
,"q","","=r",8);
276 return copy_user_generic(dst
,(__force
void *)src
,size
);
280 static __always_inline __must_check
281 int __copy_to_user(void __user
*dst
, const void *src
, unsigned size
)
284 if (!__builtin_constant_p(size
))
285 return copy_user_generic((__force
void *)dst
,src
,size
);
287 case 1:__put_user_asm(*(u8
*)src
,(u8 __user
*)dst
,ret
,"b","b","iq",1);
289 case 2:__put_user_asm(*(u16
*)src
,(u16 __user
*)dst
,ret
,"w","w","ir",2);
291 case 4:__put_user_asm(*(u32
*)src
,(u32 __user
*)dst
,ret
,"l","k","ir",4);
293 case 8:__put_user_asm(*(u64
*)src
,(u64 __user
*)dst
,ret
,"q","","ir",8);
296 __put_user_asm(*(u64
*)src
,(u64 __user
*)dst
,ret
,"q","","ir",10);
297 if (unlikely(ret
)) return ret
;
299 __put_user_asm(4[(u16
*)src
],4+(u16 __user
*)dst
,ret
,"w","w","ir",2);
302 __put_user_asm(*(u64
*)src
,(u64 __user
*)dst
,ret
,"q","","ir",16);
303 if (unlikely(ret
)) return ret
;
305 __put_user_asm(1[(u64
*)src
],1+(u64 __user
*)dst
,ret
,"q","","ir",8);
308 return copy_user_generic((__force
void *)dst
,src
,size
);
312 static __always_inline __must_check
313 int __copy_in_user(void __user
*dst
, const void __user
*src
, unsigned size
)
316 if (!__builtin_constant_p(size
))
317 return copy_user_generic((__force
void *)dst
,(__force
void *)src
,size
);
321 __get_user_asm(tmp
,(u8 __user
*)src
,ret
,"b","b","=q",1);
323 __put_user_asm(tmp
,(u8 __user
*)dst
,ret
,"b","b","iq",1);
328 __get_user_asm(tmp
,(u16 __user
*)src
,ret
,"w","w","=r",2);
330 __put_user_asm(tmp
,(u16 __user
*)dst
,ret
,"w","w","ir",2);
336 __get_user_asm(tmp
,(u32 __user
*)src
,ret
,"l","k","=r",4);
338 __put_user_asm(tmp
,(u32 __user
*)dst
,ret
,"l","k","ir",4);
343 __get_user_asm(tmp
,(u64 __user
*)src
,ret
,"q","","=r",8);
345 __put_user_asm(tmp
,(u64 __user
*)dst
,ret
,"q","","ir",8);
349 return copy_user_generic((__force
void *)dst
,(__force
void *)src
,size
);
354 strncpy_from_user(char *dst
, const char __user
*src
, long count
);
356 __strncpy_from_user(char *dst
, const char __user
*src
, long count
);
357 __must_check
long strnlen_user(const char __user
*str
, long n
);
358 __must_check
long __strnlen_user(const char __user
*str
, long n
);
359 __must_check
long strlen_user(const char __user
*str
);
360 __must_check
unsigned long clear_user(void __user
*mem
, unsigned long len
);
361 __must_check
unsigned long __clear_user(void __user
*mem
, unsigned long len
);
363 __must_check
long __copy_from_user_inatomic(void *dst
, const void __user
*src
, unsigned size
);
364 #define __copy_to_user_inatomic copy_user_generic
366 #endif /* __X86_64_UACCESS_H */