1 
2 #ifndef __X86_UACCESS_H__
3 #define __X86_UACCESS_H__
4 
5 #include <xen/compiler.h>
6 #include <xen/errno.h>
7 #include <xen/prefetch.h>
8 #include <asm/asm_defns.h>
9 #include <asm/page.h>
10 
11 #include <asm/x86_64/uaccess.h>
12 
13 unsigned copy_to_user(void *to, const void *from, unsigned len);
14 unsigned clear_user(void *to, unsigned len);
15 unsigned copy_from_user(void *to, const void *from, unsigned len);
16 /* Handles exceptions in both to and from, but doesn't do access_ok */
17 unsigned __copy_to_user_ll(void __user*to, const void *from, unsigned n);
18 unsigned __copy_from_user_ll(void *to, const void __user *from, unsigned n);
19 
20 extern long __get_user_bad(void);
21 extern void __put_user_bad(void);
22 
23 /**
24  * get_user: - Get a simple variable from user space.
25  * @x:   Variable to store result.
26  * @ptr: Source address, in user space.
27  *
28  * Context: User context only.  This function may sleep.
29  *
30  * This macro copies a single simple variable from user space to kernel
31  * space.  It supports simple types like char and int, but not larger
32  * data types like structures or arrays.
33  *
34  * @ptr must have pointer-to-simple-variable type, and the result of
35  * dereferencing @ptr must be assignable to @x without a cast.
36  *
37  * Returns zero on success, or -EFAULT on error.
38  * On error, the variable @x is set to zero.
39  */
40 #define get_user(x,ptr)	\
41   __get_user_check((x),(ptr),sizeof(*(ptr)))
42 
43 /**
44  * put_user: - Write a simple value into user space.
45  * @x:   Value to copy to user space.
46  * @ptr: Destination address, in user space.
47  *
48  * Context: User context only.  This function may sleep.
49  *
50  * This macro copies a single simple value from kernel space to user
51  * space.  It supports simple types like char and int, but not larger
52  * data types like structures or arrays.
53  *
54  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
55  * to the result of dereferencing @ptr.
56  *
57  * Returns zero on success, or -EFAULT on error.
58  */
59 #define put_user(x,ptr)							\
60   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
61 
62 /**
63  * __get_user: - Get a simple variable from user space, with less checking.
64  * @x:   Variable to store result.
65  * @ptr: Source address, in user space.
66  *
67  * Context: User context only.  This function may sleep.
68  *
69  * This macro copies a single simple variable from user space to kernel
70  * space.  It supports simple types like char and int, but not larger
71  * data types like structures or arrays.
72  *
73  * @ptr must have pointer-to-simple-variable type, and the result of
74  * dereferencing @ptr must be assignable to @x without a cast.
75  *
76  * Caller must check the pointer with access_ok() before calling this
77  * function.
78  *
79  * Returns zero on success, or -EFAULT on error.
80  * On error, the variable @x is set to zero.
81  */
82 #define __get_user(x,ptr) \
83   __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
84 
85 /**
86  * __put_user: - Write a simple value into user space, with less checking.
87  * @x:   Value to copy to user space.
88  * @ptr: Destination address, in user space.
89  *
90  * Context: User context only.  This function may sleep.
91  *
92  * This macro copies a single simple value from kernel space to user
93  * space.  It supports simple types like char and int, but not larger
94  * data types like structures or arrays.
95  *
96  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
97  * to the result of dereferencing @ptr.
98  *
99  * Caller must check the pointer with access_ok() before calling this
100  * function.
101  *
102  * Returns zero on success, or -EFAULT on error.
103  */
104 #define __put_user(x,ptr) \
105   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
106 
107 #define __put_user_nocheck(x, ptr, size)				\
108 ({									\
109 	int err_; 							\
110 	__put_user_size(x, ptr, size, err_, -EFAULT);			\
111 	err_;								\
112 })
113 
114 #define __put_user_check(x, ptr, size)					\
115 ({									\
116 	__typeof__(*(ptr)) __user *ptr_ = (ptr);			\
117 	__typeof__(size) size_ = (size);				\
118 	access_ok(ptr_, size_) ? __put_user_nocheck(x, ptr_, size_)	\
119 			       : -EFAULT;				\
120 })
121 
122 #define __get_user_nocheck(x, ptr, size)				\
123 ({									\
124 	int err_; 							\
125 	__get_user_size(x, ptr, size, err_, -EFAULT);			\
126 	err_;								\
127 })
128 
129 #define __get_user_check(x, ptr, size)					\
130 ({									\
131 	__typeof__(*(ptr)) __user *ptr_ = (ptr);			\
132 	__typeof__(size) size_ = (size);				\
133 	access_ok(ptr_, size_) ? __get_user_nocheck(x, ptr_, size_)	\
134 			       : -EFAULT;				\
135 })
136 
137 struct __large_struct { unsigned long buf[100]; };
138 #define __m(x) (*(const struct __large_struct *)(x))
139 
140 /*
141  * Tell gcc we read from memory instead of writing: this is because
142  * we do not write to any memory gcc knows about, so there are no
143  * aliasing issues.
144  */
145 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)	\
146 	stac();								\
147 	__asm__ __volatile__(						\
148 		"1:	mov"itype" %"rtype"1,%2\n"			\
149 		"2:\n"							\
150 		".section .fixup,\"ax\"\n"				\
151 		"3:	mov %3,%0\n"					\
152 		"	jmp 2b\n"					\
153 		".previous\n"						\
154 		_ASM_EXTABLE(1b, 3b)					\
155 		: "=r"(err)						\
156 		: ltype (x), "m"(__m(addr)), "i"(errret), "0"(err));	\
157 	clac()
158 
159 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)	\
160 	stac();								\
161 	__asm__ __volatile__(						\
162 		"1:	mov"itype" %2,%"rtype"1\n"			\
163 		"2:\n"							\
164 		".section .fixup,\"ax\"\n"				\
165 		"3:	mov %3,%0\n"					\
166 		"	xor"itype" %"rtype"1,%"rtype"1\n"		\
167 		"	jmp 2b\n"					\
168 		".previous\n"						\
169 		_ASM_EXTABLE(1b, 3b)					\
170 		: "=r"(err), ltype (x)					\
171 		: "m"(__m(addr)), "i"(errret), "0"(err));		\
172 	clac()
173 
174 /**
175  * __copy_to_user: - Copy a block of data into user space, with less checking
176  * @to:   Destination address, in user space.
177  * @from: Source address, in kernel space.
178  * @n:    Number of bytes to copy.
179  *
180  * Context: User context only.  This function may sleep.
181  *
182  * Copy data from kernel space to user space.  Caller must check
183  * the specified block with access_ok() before calling this function.
184  *
185  * Returns number of bytes that could not be copied.
186  * On success, this will be zero.
187  */
188 static always_inline unsigned long
__copy_to_user(void __user * to,const void * from,unsigned long n)189 __copy_to_user(void __user *to, const void *from, unsigned long n)
190 {
191     if (__builtin_constant_p(n)) {
192         unsigned long ret;
193 
194         switch (n) {
195         case 1:
196             __put_user_size(*(const u8 *)from, (u8 __user *)to, 1, ret, 1);
197             return ret;
198         case 2:
199             __put_user_size(*(const u16 *)from, (u16 __user *)to, 2, ret, 2);
200             return ret;
201         case 4:
202             __put_user_size(*(const u32 *)from, (u32 __user *)to, 4, ret, 4);
203             return ret;
204         case 8:
205             __put_user_size(*(const u64 *)from, (u64 __user *)to, 8, ret, 8);
206             return ret;
207         }
208     }
209     return __copy_to_user_ll(to, from, n);
210 }
211 
212 /**
213  * __copy_from_user: - Copy a block of data from user space, with less checking
214  * @to:   Destination address, in kernel space.
215  * @from: Source address, in user space.
216  * @n:    Number of bytes to copy.
217  *
218  * Context: User context only.  This function may sleep.
219  *
220  * Copy data from user space to kernel space.  Caller must check
221  * the specified block with access_ok() before calling this function.
222  *
223  * Returns number of bytes that could not be copied.
224  * On success, this will be zero.
225  *
226  * If some data could not be copied, this function will pad the copied
227  * data to the requested size using zero bytes.
228  */
229 static always_inline unsigned long
__copy_from_user(void * to,const void __user * from,unsigned long n)230 __copy_from_user(void *to, const void __user *from, unsigned long n)
231 {
232     if (__builtin_constant_p(n)) {
233         unsigned long ret;
234 
235         switch (n) {
236         case 1:
237             __get_user_size(*(u8 *)to, from, 1, ret, 1);
238             return ret;
239         case 2:
240             __get_user_size(*(u16 *)to, from, 2, ret, 2);
241             return ret;
242         case 4:
243             __get_user_size(*(u32 *)to, from, 4, ret, 4);
244             return ret;
245         case 8:
246             __get_user_size(*(u64*)to, from, 8, ret, 8);
247             return ret;
248         }
249     }
250     return __copy_from_user_ll(to, from, n);
251 }
252 
253 /*
254  * The exception table consists of pairs of addresses: the first is the
255  * address of an instruction that is allowed to fault, and the second is
256  * the address at which the program should continue.  No registers are
257  * modified, so it is entirely up to the continuation code to figure out
258  * what to do.
259  *
260  * All the routines below use bits of fixup code that are out of line
261  * with the main instruction path.  This means when everything is well,
262  * we don't even have to jump over them.  Further, they do not intrude
263  * on our cache or tlb entries.
264  */
265 
266 struct exception_table_entry
267 {
268 	s32 addr, cont;
269 };
270 extern struct exception_table_entry __start___ex_table[];
271 extern struct exception_table_entry __stop___ex_table[];
272 extern struct exception_table_entry __start___pre_ex_table[];
273 extern struct exception_table_entry __stop___pre_ex_table[];
274 
275 union stub_exception_token {
276     struct {
277         uint16_t ec;
278         uint8_t trapnr;
279     } fields;
280     unsigned long raw;
281 };
282 
283 extern unsigned long search_exception_table(const struct cpu_user_regs *regs);
284 extern void sort_exception_tables(void);
285 extern void sort_exception_table(struct exception_table_entry *start,
286                                  const struct exception_table_entry *stop);
287 
288 #endif /* __X86_UACCESS_H__ */
289