blob: 19f99178fe83735cc17bff14516f529c17e479c3 [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>
9#include <linux/sched.h>
10#include <linux/prefetch.h>
11#include <asm/page.h>
12
13#define VERIFY_READ 0
14#define VERIFY_WRITE 1
15
16/*
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.
20 *
21 * For historical reasons, these macros are grossly misnamed.
22 */
23
24#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
25
26#define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFFFFFFFFFUL)
27#define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
28
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))
32
33#define segment_eq(a,b) ((a).seg == (b).seg)
34
35#define __addr_ok(addr) (!((unsigned long)(addr) & (current_thread_info()->addr_limit.seg)))
36
37/*
38 * Uhhuh, this needs 65-bit arithmetic. We have a carry..
39 */
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)); \
47 flag; })
48
49#define access_ok(type, addr, size) (__range_not_ok(addr,size) == 0)
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
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
56 * what to do.
57 *
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.
62 */
63
64struct exception_table_entry
65{
66 unsigned long insn, fixup;
67};
68
69#define ARCH_HAS_SEARCH_EXTABLE
70
71/*
72 * These are the main single-value transfer routines. They automatically
73 * use the right size if we just have the right pointer type.
74 *
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.
79 *
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).
84 */
85
86#define __get_user_x(size,ret,x,ptr) \
Andi Kleen383d0792006-09-26 10:52:40 +020087 asm volatile("call __get_user_" #size \
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 :"=a" (ret),"=d" (x) \
89 :"c" (ptr) \
90 :"r8")
91
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; \
95 int __ret_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; \
103 } \
Andi Kleen383d0792006-09-26 10:52:40 +0200104 (x) = (typeof(*(ptr)))__val_gu; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 __ret_gu; \
106})
107
108extern void __put_user_1(void);
109extern void __put_user_2(void);
110extern void __put_user_4(void);
111extern void __put_user_8(void);
112extern void __put_user_bad(void);
113
114#define __put_user_x(size,ret,x,ptr) \
Andi Kleen383d0792006-09-26 10:52:40 +0200115 asm volatile("call __put_user_" #size \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 :"=a" (ret) \
117 :"c" (ptr),"d" (x) \
118 :"r8")
119
120#define put_user(x,ptr) \
121 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
122
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)))
127
128#define __get_user_unaligned __get_user
129#define __put_user_unaligned __put_user
130
131#define __put_user_nocheck(x,ptr,size) \
132({ \
133 int __pu_err; \
134 __put_user_size((x),(ptr),(size),__pu_err); \
135 __pu_err; \
136})
137
138
139#define __put_user_check(x,ptr,size) \
140({ \
141 int __pu_err; \
Andi Kleen383d0792006-09-26 10:52:40 +0200142 typeof(*(ptr)) __user *__pu_addr = (ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 switch (size) { \
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(); \
149 } \
150 __pu_err; \
151})
152
153#define __put_user_size(x,ptr,size,retval) \
154do { \
155 retval = 0; \
156 __chk_user_ptr(ptr); \
157 switch (size) { \
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(); \
163 } \
164} while (0)
165
166/* FIXME: this hack is definitely wrong -AK */
167struct __large_struct { unsigned long buf[100]; };
168#define __m(x) (*(struct __large_struct __user *)(x))
169
170/*
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
173 * aliasing issues.
174 */
175#define __put_user_asm(x, addr, err, itype, rtype, ltype, errno) \
Andi Kleen383d0792006-09-26 10:52:40 +0200176 asm volatile( \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 "1: mov"itype" %"rtype"1,%2\n" \
178 "2:\n" \
179 ".section .fixup,\"ax\"\n" \
180 "3: mov %3,%0\n" \
181 " jmp 2b\n" \
182 ".previous\n" \
183 ".section __ex_table,\"a\"\n" \
184 " .align 8\n" \
185 " .quad 1b,3b\n" \
186 ".previous" \
187 : "=r"(err) \
188 : ltype (x), "m"(__m(addr)), "i"(errno), "0"(err))
189
190
191#define __get_user_nocheck(x,ptr,size) \
192({ \
193 int __gu_err; \
194 unsigned long __gu_val; \
195 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
Andi Kleen383d0792006-09-26 10:52:40 +0200196 (x) = (typeof(*(ptr)))__gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 __gu_err; \
198})
199
200extern int __get_user_1(void);
201extern int __get_user_2(void);
202extern int __get_user_4(void);
203extern int __get_user_8(void);
204extern int __get_user_bad(void);
205
206#define __get_user_size(x,ptr,size,retval) \
207do { \
208 retval = 0; \
209 __chk_user_ptr(ptr); \
210 switch (size) { \
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(); \
216 } \
217} while (0)
218
219#define __get_user_asm(x, addr, err, itype, rtype, ltype, errno) \
Andi Kleen383d0792006-09-26 10:52:40 +0200220 asm volatile( \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 "1: mov"itype" %2,%"rtype"1\n" \
222 "2:\n" \
223 ".section .fixup,\"ax\"\n" \
224 "3: mov %3,%0\n" \
225 " xor"itype" %"rtype"1,%"rtype"1\n" \
226 " jmp 2b\n" \
227 ".previous\n" \
228 ".section __ex_table,\"a\"\n" \
229 " .align 8\n" \
230 " .quad 1b,3b\n" \
231 ".previous" \
232 : "=r"(err), ltype (x) \
233 : "m"(__m(addr)), "i"(errno), "0"(err))
234
235/*
236 * Copy To/From Userspace
237 */
238
239/* Handles exceptions in both to and from, but doesn't do access_ok */
Andi Kleen95912002006-09-26 10:52:39 +0200240__must_check unsigned long
241copy_user_generic(void *to, const void *from, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Andi Kleen95912002006-09-26 10:52:39 +0200243__must_check unsigned long
244copy_to_user(void __user *to, const void *from, unsigned len);
245__must_check unsigned long
246copy_from_user(void *to, const void __user *from, unsigned len);
247__must_check unsigned long
248copy_in_user(void __user *to, const void __user *from, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Andi Kleen95912002006-09-26 10:52:39 +0200250static __always_inline __must_check
251int __copy_from_user(void *dst, const void __user *src, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Andi Kleen383d0792006-09-26 10:52:40 +0200253 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!__builtin_constant_p(size))
255 return copy_user_generic(dst,(__force void *)src,size);
256 switch (size) {
257 case 1:__get_user_asm(*(u8*)dst,(u8 __user *)src,ret,"b","b","=q",1);
258 return ret;
259 case 2:__get_user_asm(*(u16*)dst,(u16 __user *)src,ret,"w","w","=r",2);
260 return ret;
261 case 4:__get_user_asm(*(u32*)dst,(u32 __user *)src,ret,"l","k","=r",4);
262 return ret;
263 case 8:__get_user_asm(*(u64*)dst,(u64 __user *)src,ret,"q","","=r",8);
264 return ret;
265 case 10:
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);
269 return ret;
270 case 16:
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);
274 return ret;
275 default:
276 return copy_user_generic(dst,(__force void *)src,size);
277 }
278}
279
Andi Kleen95912002006-09-26 10:52:39 +0200280static __always_inline __must_check
281int __copy_to_user(void __user *dst, const void *src, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Andi Kleen383d0792006-09-26 10:52:40 +0200283 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!__builtin_constant_p(size))
285 return copy_user_generic((__force void *)dst,src,size);
286 switch (size) {
287 case 1:__put_user_asm(*(u8*)src,(u8 __user *)dst,ret,"b","b","iq",1);
288 return ret;
289 case 2:__put_user_asm(*(u16*)src,(u16 __user *)dst,ret,"w","w","ir",2);
290 return ret;
291 case 4:__put_user_asm(*(u32*)src,(u32 __user *)dst,ret,"l","k","ir",4);
292 return ret;
293 case 8:__put_user_asm(*(u64*)src,(u64 __user *)dst,ret,"q","","ir",8);
294 return ret;
295 case 10:
296 __put_user_asm(*(u64*)src,(u64 __user *)dst,ret,"q","","ir",10);
297 if (unlikely(ret)) return ret;
298 asm("":::"memory");
299 __put_user_asm(4[(u16*)src],4+(u16 __user *)dst,ret,"w","w","ir",2);
300 return ret;
301 case 16:
302 __put_user_asm(*(u64*)src,(u64 __user *)dst,ret,"q","","ir",16);
303 if (unlikely(ret)) return ret;
304 asm("":::"memory");
305 __put_user_asm(1[(u64*)src],1+(u64 __user *)dst,ret,"q","","ir",8);
306 return ret;
307 default:
308 return copy_user_generic((__force void *)dst,src,size);
309 }
310}
311
Andi Kleen95912002006-09-26 10:52:39 +0200312static __always_inline __must_check
313int __copy_in_user(void __user *dst, const void __user *src, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Andi Kleen383d0792006-09-26 10:52:40 +0200315 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!__builtin_constant_p(size))
317 return copy_user_generic((__force void *)dst,(__force void *)src,size);
318 switch (size) {
319 case 1: {
320 u8 tmp;
321 __get_user_asm(tmp,(u8 __user *)src,ret,"b","b","=q",1);
322 if (likely(!ret))
323 __put_user_asm(tmp,(u8 __user *)dst,ret,"b","b","iq",1);
324 return ret;
325 }
326 case 2: {
327 u16 tmp;
328 __get_user_asm(tmp,(u16 __user *)src,ret,"w","w","=r",2);
329 if (likely(!ret))
330 __put_user_asm(tmp,(u16 __user *)dst,ret,"w","w","ir",2);
331 return ret;
332 }
333
334 case 4: {
335 u32 tmp;
336 __get_user_asm(tmp,(u32 __user *)src,ret,"l","k","=r",4);
337 if (likely(!ret))
338 __put_user_asm(tmp,(u32 __user *)dst,ret,"l","k","ir",4);
339 return ret;
340 }
341 case 8: {
342 u64 tmp;
343 __get_user_asm(tmp,(u64 __user *)src,ret,"q","","=r",8);
344 if (likely(!ret))
345 __put_user_asm(tmp,(u64 __user *)dst,ret,"q","","ir",8);
346 return ret;
347 }
348 default:
349 return copy_user_generic((__force void *)dst,(__force void *)src,size);
350 }
351}
352
Andi Kleen95912002006-09-26 10:52:39 +0200353__must_check long
354strncpy_from_user(char *dst, const char __user *src, long count);
355__must_check long
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Andi Kleen95912002006-09-26 10:52:39 +0200363__must_check long __copy_from_user_inatomic(void *dst, const void __user *src, unsigned size);
Andi Kleenb8858082006-09-30 01:47:55 +0200364
365static __must_check __always_inline int
366__copy_to_user_inatomic(void __user *dst, const void *src, unsigned size)
367{
368 return copy_user_generic((__force void *)dst, src, size);
369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371#endif /* __X86_64_UACCESS_H */