1 #ifndef __X86_64_UACCESS_H
2 #define __X86_64_UACCESS_H
5 * User space memory access functions
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>
15 #define VERIFY_WRITE 1
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.
22 * For historical reasons, these macros are grossly misnamed.
25 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
27 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFFFFFFFFFUL)
28 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
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))
34 #define segment_eq(a,b) ((a).seg == (b).seg)
36 #define __addr_ok(addr) (!((unsigned long)(addr) & (current_thread_info()->addr_limit.seg)))
39 * Uhhuh, this needs 65-bit arithmetic. We have a carry..
41 #define __range_not_ok(addr,size) ({ \
42 unsigned long flag,sum; \
43 __chk_user_ptr(addr); \
44 asm("# range_ok\n\r" \
45 "addq %3,%1 ; sbbq %0,%0 ; cmpq %1,%4 ; sbbq $0,%0" \
46 :"=&r" (flag), "=r" (sum) \
47 :"1" (addr),"g" ((long)(size)),"g" (current_thread_info()->addr_limit.seg)); \
50 #define access_ok(type, addr, size) (__range_not_ok(addr,size) == 0)
53 * The exception table consists of pairs of addresses: the first is the
54 * address of an instruction that is allowed to fault, and the second is
55 * the address at which the program should continue. No registers are
56 * modified, so it is entirely up to the continuation code to figure out
59 * All the routines below use bits of fixup code that are out of line
60 * with the main instruction path. This means when everything is well,
61 * we don't even have to jump over them. Further, they do not intrude
62 * on our cache or tlb entries.
65 struct exception_table_entry
67 unsigned long insn
, fixup
;
70 #define ARCH_HAS_SEARCH_EXTABLE
73 * These are the main single-value transfer routines. They automatically
74 * use the right size if we just have the right pointer type.
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.
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).
87 #define __get_user_x(size,ret,x,ptr) \
88 __asm__ __volatile__("call __get_user_" #size \
89 :"=a" (ret),"=d" (x) \
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; \
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; \
105 (x) = (__typeof__(*(ptr)))__val_gu; \
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);
115 #define __put_user_x(size,ret,x,ptr) \
116 __asm__ __volatile__("call __put_user_" #size \
121 #define put_user(x,ptr) \
122 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
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)))
129 #define __get_user_unaligned __get_user
130 #define __put_user_unaligned __put_user
132 #define __put_user_nocheck(x,ptr,size) \
135 __put_user_size((x),(ptr),(size),__pu_err); \
140 #define __put_user_check(x,ptr,size) \
143 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
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(); \
154 #define __put_user_size(x,ptr,size,retval) \
157 __chk_user_ptr(ptr); \
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(); \
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))
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
176 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errno) \
177 __asm__ __volatile__( \
178 "1: mov"itype" %"rtype"1,%2\n" \
180 ".section .fixup,\"ax\"\n" \
184 ".section __ex_table,\"a\"\n" \
189 : ltype (x), "m"(__m(addr)), "i"(errno), "0"(err))
192 #define __get_user_nocheck(x,ptr,size) \
195 unsigned long __gu_val; \
196 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
197 (x) = (__typeof__(*(ptr)))__gu_val; \
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);
207 #define __get_user_size(x,ptr,size,retval) \
210 __chk_user_ptr(ptr); \
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(); \
220 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errno) \
221 __asm__ __volatile__( \
222 "1: mov"itype" %2,%"rtype"1\n" \
224 ".section .fixup,\"ax\"\n" \
226 " xor"itype" %"rtype"1,%"rtype"1\n" \
229 ".section __ex_table,\"a\"\n" \
233 : "=r"(err), ltype (x) \
234 : "m"(__m(addr)), "i"(errno), "0"(err))
237 * Copy To/From Userspace
240 /* Handles exceptions in both to and from, but doesn't do access_ok */
241 extern unsigned long copy_user_generic(void *to
, const void *from
, unsigned len
);
243 extern unsigned long copy_to_user(void __user
*to
, const void *from
, unsigned len
);
244 extern unsigned long copy_from_user(void *to
, const void __user
*from
, unsigned len
);
245 extern unsigned long copy_in_user(void __user
*to
, const void __user
*from
, unsigned len
);
247 static inline int __copy_from_user(void *dst
, const void __user
*src
, unsigned size
)
250 if (!__builtin_constant_p(size
))
251 return copy_user_generic(dst
,(__force
void *)src
,size
);
253 case 1:__get_user_asm(*(u8
*)dst
,(u8 __user
*)src
,ret
,"b","b","=q",1);
255 case 2:__get_user_asm(*(u16
*)dst
,(u16 __user
*)src
,ret
,"w","w","=r",2);
257 case 4:__get_user_asm(*(u32
*)dst
,(u32 __user
*)src
,ret
,"l","k","=r",4);
259 case 8:__get_user_asm(*(u64
*)dst
,(u64 __user
*)src
,ret
,"q","","=r",8);
262 __get_user_asm(*(u64
*)dst
,(u64 __user
*)src
,ret
,"q","","=r",16);
263 if (unlikely(ret
)) return ret
;
264 __get_user_asm(*(u16
*)(8+(char*)dst
),(u16 __user
*)(8+(char __user
*)src
),ret
,"w","w","=r",2);
267 __get_user_asm(*(u64
*)dst
,(u64 __user
*)src
,ret
,"q","","=r",16);
268 if (unlikely(ret
)) return ret
;
269 __get_user_asm(*(u64
*)(8+(char*)dst
),(u64 __user
*)(8+(char __user
*)src
),ret
,"q","","=r",8);
272 return copy_user_generic(dst
,(__force
void *)src
,size
);
276 static inline int __copy_to_user(void __user
*dst
, const void *src
, unsigned size
)
279 if (!__builtin_constant_p(size
))
280 return copy_user_generic((__force
void *)dst
,src
,size
);
282 case 1:__put_user_asm(*(u8
*)src
,(u8 __user
*)dst
,ret
,"b","b","iq",1);
284 case 2:__put_user_asm(*(u16
*)src
,(u16 __user
*)dst
,ret
,"w","w","ir",2);
286 case 4:__put_user_asm(*(u32
*)src
,(u32 __user
*)dst
,ret
,"l","k","ir",4);
288 case 8:__put_user_asm(*(u64
*)src
,(u64 __user
*)dst
,ret
,"q","","ir",8);
291 __put_user_asm(*(u64
*)src
,(u64 __user
*)dst
,ret
,"q","","ir",10);
292 if (unlikely(ret
)) return ret
;
294 __put_user_asm(4[(u16
*)src
],4+(u16 __user
*)dst
,ret
,"w","w","ir",2);
297 __put_user_asm(*(u64
*)src
,(u64 __user
*)dst
,ret
,"q","","ir",16);
298 if (unlikely(ret
)) return ret
;
300 __put_user_asm(1[(u64
*)src
],1+(u64 __user
*)dst
,ret
,"q","","ir",8);
303 return copy_user_generic((__force
void *)dst
,src
,size
);
308 static inline int __copy_in_user(void __user
*dst
, const void __user
*src
, unsigned size
)
311 if (!__builtin_constant_p(size
))
312 return copy_user_generic((__force
void *)dst
,(__force
void *)src
,size
);
316 __get_user_asm(tmp
,(u8 __user
*)src
,ret
,"b","b","=q",1);
318 __put_user_asm(tmp
,(u8 __user
*)dst
,ret
,"b","b","iq",1);
323 __get_user_asm(tmp
,(u16 __user
*)src
,ret
,"w","w","=r",2);
325 __put_user_asm(tmp
,(u16 __user
*)dst
,ret
,"w","w","ir",2);
331 __get_user_asm(tmp
,(u32 __user
*)src
,ret
,"l","k","=r",4);
333 __put_user_asm(tmp
,(u32 __user
*)dst
,ret
,"l","k","ir",4);
338 __get_user_asm(tmp
,(u64 __user
*)src
,ret
,"q","","=r",8);
340 __put_user_asm(tmp
,(u64 __user
*)dst
,ret
,"q","","ir",8);
344 return copy_user_generic((__force
void *)dst
,(__force
void *)src
,size
);
348 long strncpy_from_user(char *dst
, const char __user
*src
, long count
);
349 long __strncpy_from_user(char *dst
, const char __user
*src
, long count
);
350 long strnlen_user(const char __user
*str
, long n
);
351 long strlen_user(const char __user
*str
);
352 unsigned long clear_user(void __user
*mem
, unsigned long len
);
353 unsigned long __clear_user(void __user
*mem
, unsigned long len
);
355 #define __copy_to_user_inatomic __copy_to_user
356 #define __copy_from_user_inatomic __copy_from_user
358 #endif /* __X86_64_UACCESS_H */