1 #ifndef __X86_64_UACCESS_H
2 #define __X86_64_UACCESS_H
3 
4 /*
5  * User space memory access functions
6  */
7 #include <linux/config.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(0xFFFFFFFFFFFFFFFF)
27 #define USER_DS		MAKE_MM_SEG(PAGE_OFFSET)
28 
29 #define get_ds()	(KERNEL_DS)
30 #define get_fs()	(current->addr_limit)
31 #define set_fs(x)	(current->addr_limit = (x))
32 
33 #define segment_eq(a,b)	((a).seg == (b).seg)
34 
35 #define __addr_ok(addr) (!((unsigned long)(addr) & (current->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 	asm("# range_ok\n\r" \
43 		"addq %3,%1 ; sbbq %0,%0 ; cmpq %1,%4 ; sbbq $0,%0"  \
44 		:"=&r" (flag), "=r" (sum) \
45 		:"1" (addr),"g" ((long)(size)),"g" (current->addr_limit.seg)); \
46 	flag; })
47 
48 #define access_ok(type,addr,size) (__range_not_ok(addr,size) == 0)
49 
verify_area(int type,const void * addr,unsigned long size)50 extern inline int verify_area(int type, const void * addr, unsigned long size)
51 {
52 	return access_ok(type,addr,size) ? 0 : -EFAULT;
53 }
54 
55 
56 /*
57  * The exception table consists of pairs of addresses: the first is the
58  * address of an instruction that is allowed to fault, and the second is
59  * the address at which the program should continue.  No registers are
60  * modified, so it is entirely up to the continuation code to figure out
61  * what to do.
62  *
63  * All the routines below use bits of fixup code that are out of line
64  * with the main instruction path.  This means when everything is well,
65  * we don't even have to jump over them.  Further, they do not intrude
66  * on our cache or tlb entries.
67  */
68 
69 struct exception_table_entry
70 {
71 	unsigned long insn, fixup;
72 };
73 
74 
75 /*
76  * These are the main single-value transfer routines.  They automatically
77  * use the right size if we just have the right pointer type.
78  *
79  * This gets kind of ugly. We want to return _two_ values in "get_user()"
80  * and yet we don't want to do any pointers, because that is too much
81  * of a performance impact. Thus we have a few rather ugly macros here,
82  * and hide all the ugliness from the user.
83  *
84  * The "__xxx" versions of the user access functions are versions that
85  * do not verify the address space, that must have been done previously
86  * with a separate "access_ok()" call (this is used when we do multiple
87  * accesses to the same area of user memory).
88  */
89 
90 extern void __get_user_1(void);
91 extern void __get_user_2(void);
92 extern void __get_user_4(void);
93 extern void __get_user_8(void);
94 
95 #define __get_user_x(size,ret,x,ptr) \
96 	__asm__ __volatile__("call __get_user_" #size \
97 		:"=a" (ret),"=d" (x) \
98 		:"0" (ptr) \
99 		:"rbx")
100 
101 /* Careful: we have to cast the result to the type of the pointer for sign reasons */
102 #define get_user(x,ptr)							\
103 ({	long __val_gu;							\
104 	int __ret_gu; 							\
105 	switch(sizeof (*(ptr))) {					\
106 	case 1:  __get_user_x(1,__ret_gu,__val_gu,ptr); break;		\
107 	case 2:  __get_user_x(2,__ret_gu,__val_gu,ptr); break;		\
108 	case 4:  __get_user_x(4,__ret_gu,__val_gu,ptr); break;		\
109 	case 8:  __get_user_x(8,__ret_gu,__val_gu,ptr); break;		\
110 	default: __get_user_bad(); break;				\
111 	}								\
112 	(x) = (__typeof__(*(ptr)))__val_gu;				\
113 	__ret_gu;							\
114 })
115 
116 extern void __put_user_1(void);
117 extern void __put_user_2(void);
118 extern void __put_user_4(void);
119 extern void __put_user_8(void);
120 
121 extern void __put_user_bad(void);
122 
123 #define __put_user_x(size,ret,x,ptr)					\
124 	__asm__ __volatile__("call __put_user_" #size			\
125 		:"=a" (ret)						\
126 		:"0" (ptr),"d" (x)					\
127 		:"rbx")
128 
129 #define put_user(x,ptr)							\
130   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
131 
132 #define __get_user(x,ptr) \
133   __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
134 #define __put_user(x,ptr) \
135   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
136 
137 #define __put_user_nocheck(x,ptr,size)			\
138 ({							\
139 	int __pu_err;					\
140 	__put_user_size((x),(ptr),(size),__pu_err);	\
141 	__pu_err;					\
142 })
143 
144 
145 #define __put_user_check(x,ptr,size)			\
146 ({							\
147 	int __pu_err = -EFAULT;				\
148 	__typeof__(*(ptr)) *__pu_addr = (ptr);		\
149 	if (access_ok(VERIFY_WRITE,__pu_addr,size))	\
150 		__put_user_size((x),__pu_addr,(size),__pu_err);	\
151 	__pu_err;					\
152 })
153 
154 #define __put_user_size(x,ptr,size,retval)				\
155 do {									\
156 	retval = 0;							\
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 */
167 struct __large_struct { unsigned long buf[100]; };
168 #define __m(x) (*(struct __large_struct *)(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)	\
176 	__asm__ __volatile__(				\
177 		"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 	long __gu_val;						\
195 	__get_user_size(__gu_val,(ptr),(size),__gu_err);	\
196 	(x) = (__typeof__(*(ptr)))__gu_val;			\
197 	__gu_err;						\
198 })
199 
200 extern int __get_user_bad(void);
201 
202 #define __get_user_size(x,ptr,size,retval)				\
203 do {									\
204 	retval = 0;							\
205 	switch (size) {							\
206 	  case 1: __get_user_asm(x,ptr,retval,"b","b","=q",-EFAULT); break;\
207 	  case 2: __get_user_asm(x,ptr,retval,"w","w","=r",-EFAULT); break;\
208 	  case 4: __get_user_asm(x,ptr,retval,"l","k","=r",-EFAULT); break;\
209 	  case 8: __get_user_asm(x,ptr,retval,"q","","=r",-EFAULT); break;\
210 	  default: (x) = __get_user_bad();				\
211 	}								\
212 } while (0)
213 
214 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errno)	\
215 	__asm__ __volatile__(					\
216 		"1:	mov"itype" %2,%"rtype"1\n"		\
217 		"2:\n"						\
218 		".section .fixup,\"ax\"\n"			\
219 		"3:	mov %3,%0\n"				\
220 		"	xor"itype" %"rtype"1,%"rtype"1\n"	\
221 		"	jmp 2b\n"				\
222 		".previous\n"					\
223 		".section __ex_table,\"a\"\n"			\
224 		"	.align 8\n"				\
225 		"	.quad 1b,3b\n"				\
226 		".previous"					\
227 		: "=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 */
235 extern unsigned long copy_user_generic(void *to, const void *from, unsigned len);
236 
237 extern unsigned long copy_to_user(void *to, const void *from, unsigned len);
238 extern unsigned long copy_from_user(void *to, const void *from, unsigned len);
239 extern unsigned long copy_in_user(void *to, const void *from, unsigned len);
240 
__copy_from_user(void * dst,const void * src,unsigned size)241 static inline int __copy_from_user(void *dst, const void *src, unsigned size)
242 {
243 	if (!__builtin_constant_p(size))
244 		return copy_user_generic(dst,src,size);
245 	int ret = 0;
246 	switch (size) {
247 	case 1:__get_user_asm(*(u8*)dst,(u8 *)src,ret,"b","b","=q",1);
248 		return ret;
249 	case 2:__get_user_asm(*(u16*)dst,(u16*)src,ret,"w","w","=r",2);
250 		return ret;
251 	case 4:__get_user_asm(*(u32*)dst,(u32*)src,ret,"l","k","=r",4);
252 		return ret;
253 	case 8:__get_user_asm(*(u64*)dst,(u64*)src,ret,"q","","=r",8);
254 		return ret;
255 	case 10:
256 	       	__get_user_asm(*(u64*)dst,(u64*)src,ret,"q","","=r",16);
257 		if (ret) return ret;
258 		__get_user_asm(*(u16*)(8+(char*)dst),(u16*)(8+(char*)src),ret,"w","w","=r",2);
259 		return ret;
260 	case 16:
261 		__get_user_asm(*(u64*)dst,(u64*)src,ret,"q","","=r",16);
262 		if (ret) return ret;
263 		__get_user_asm(*(u64*)(8+(char*)dst),(u64*)(8+(char*)src),ret,"q","","=r",8);
264 		return ret;
265 	default:
266 		return copy_user_generic(dst,src,size);
267 	}
268 }
269 
__copy_to_user(void * dst,const void * src,unsigned size)270 static inline int __copy_to_user(void *dst, const void *src, unsigned size)
271 {
272 	if (!__builtin_constant_p(size))
273 		return copy_user_generic(dst,src,size);
274 	int ret = 0;
275 	switch (size) {
276 	case 1:__put_user_asm(*(u8*)src,(u8 *)dst,ret,"b","b","iq",1);
277 		return ret;
278 	case 2:__put_user_asm(*(u16*)src,(u16*)dst,ret,"w","w","ir",2);
279 		return ret;
280 	case 4:__put_user_asm(*(u32*)src,(u32*)dst,ret,"l","k","ir",4);
281 		return ret;
282 	case 8:__put_user_asm(*(u64*)src,(u64*)dst,ret,"q","","ir",8);
283 		return ret;
284 	case 10:
285 		__put_user_asm(*(u64*)src,(u64*)dst,ret,"q","","ir",10);
286 		if (ret) return ret;
287 		asm("":::"memory");
288 		__put_user_asm(4[(u16*)src],4+(u16*)dst,ret,"w","w","ir",2);
289 		return ret;
290 	case 16:
291 		__put_user_asm(*(u64*)src,(u64*)dst,ret,"q","","ir",16);
292 		if (ret) return ret;
293 		asm("":::"memory");
294 		__put_user_asm(1[(u64*)src],1+(u64*)dst,ret,"q","","ir",8);
295 		return ret;
296 	default:
297 		return copy_user_generic(dst,src,size);
298 	}
299 }
300 
__copy_in_user(void * dst,const void * src,unsigned size)301 static inline int __copy_in_user(void *dst, const void *src, unsigned size)
302 {
303        int ret = 0;
304        if (!__builtin_constant_p(size))
305 		return copy_user_generic(dst,src,size);
306        switch (size) {
307        case 1: {
308 	       u8 tmp;
309 	       __get_user_asm(tmp,(u8 *)src,ret,"b","b","=q",1);
310 	       if (likely(!ret))
311 		       __put_user_asm(tmp,(u8 *)dst,ret,"b","b","iq",1);
312 	       return ret;
313        }
314        case 2: {
315 	       u16 tmp;
316 	       __get_user_asm(tmp,(u16 *)src,ret,"w","w","=r",2);
317 	       if (likely(!ret))
318 		       __put_user_asm(tmp,(u16 *)dst,ret,"w","w","ir",2);
319 	       return ret;
320        }
321 
322        case 4: {
323 	       u32 tmp;
324 	       __get_user_asm(tmp,(u32 *)src,ret,"l","k","=r",4);
325 	       if (likely(!ret))
326 		       __put_user_asm(tmp,(u32 *)dst,ret,"l","k","ir",4);
327 	       return ret;
328        }
329        case 8: {
330 	       u64 tmp;
331 	       __get_user_asm(tmp,(u64 *)src,ret,"q","","=r",8);
332 	       if (likely(!ret))
333 		       __put_user_asm(tmp,(u64 *)dst,ret,"q","","ir",8);
334 	       return ret;
335        }
336        default:
337 	       return copy_user_generic(dst,src,size);
338        }
339 }
340 
341 long strncpy_from_user(char *dst, const char *src, long count);
342 long __strncpy_from_user(char *dst, const char *src, long count);
343 long strnlen_user(const char *str, long n);
344 long strlen_user(const char *str);
345 unsigned long clear_user(void *mem, unsigned long len);
346 unsigned long __clear_user(void *mem, unsigned long len);
347 
348 extern unsigned long search_exception_table(unsigned long);
349 
350 #endif /* __X86_64_UACCESS_H */
351