xref: /linux/arch/x86/include/asm/uaccess.h (revision 1ac2e6ca44e13a087eb7438d284f887097ee7e84)
11965aae3SH. Peter Anvin #ifndef _ASM_X86_UACCESS_H
21965aae3SH. Peter Anvin #define _ASM_X86_UACCESS_H
3bb898558SAl Viro /*
4bb898558SAl Viro  * User space memory access functions
5bb898558SAl Viro  */
6bb898558SAl Viro #include <linux/errno.h>
7bb898558SAl Viro #include <linux/compiler.h>
8bb898558SAl Viro #include <linux/thread_info.h>
9bb898558SAl Viro #include <linux/string.h>
10bb898558SAl Viro #include <asm/asm.h>
11bb898558SAl Viro #include <asm/page.h>
12bb898558SAl Viro 
13bb898558SAl Viro #define VERIFY_READ 0
14bb898558SAl Viro #define VERIFY_WRITE 1
15bb898558SAl Viro 
16bb898558SAl Viro /*
17bb898558SAl Viro  * The fs value determines whether argument validity checking should be
18bb898558SAl Viro  * performed or not.  If get_fs() == USER_DS, checking is performed, with
19bb898558SAl Viro  * get_fs() == KERNEL_DS, checking is bypassed.
20bb898558SAl Viro  *
21bb898558SAl Viro  * For historical reasons, these macros are grossly misnamed.
22bb898558SAl Viro  */
23bb898558SAl Viro 
24bb898558SAl Viro #define MAKE_MM_SEG(s)	((mm_segment_t) { (s) })
25bb898558SAl Viro 
26bb898558SAl Viro #define KERNEL_DS	MAKE_MM_SEG(-1UL)
279063c61fSLinus Torvalds #define USER_DS 	MAKE_MM_SEG(TASK_SIZE_MAX)
28bb898558SAl Viro 
29bb898558SAl Viro #define get_ds()	(KERNEL_DS)
30bb898558SAl Viro #define get_fs()	(current_thread_info()->addr_limit)
31bb898558SAl Viro #define set_fs(x)	(current_thread_info()->addr_limit = (x))
32bb898558SAl Viro 
33bb898558SAl Viro #define segment_eq(a, b)	((a).seg == (b).seg)
34bb898558SAl Viro 
35bb898558SAl Viro #define __addr_ok(addr)					\
36bb898558SAl Viro 	((unsigned long __force)(addr) <		\
37bb898558SAl Viro 	 (current_thread_info()->addr_limit.seg))
38bb898558SAl Viro 
39bb898558SAl Viro /*
40bb898558SAl Viro  * Test whether a block of memory is a valid user space address.
41bb898558SAl Viro  * Returns 0 if the range is valid, nonzero otherwise.
42bb898558SAl Viro  *
43bb898558SAl Viro  * This is equivalent to the following test:
4426afb7c6SJiri Olsa  * (u33)addr + (u33)size > (u33)current->addr_limit.seg (u65 for x86_64)
45bb898558SAl Viro  *
46bb898558SAl Viro  * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry...
47bb898558SAl Viro  */
48bb898558SAl Viro 
49bb898558SAl Viro #define __range_not_ok(addr, size)					\
50bb898558SAl Viro ({									\
51bb898558SAl Viro 	unsigned long flag, roksum;					\
52bb898558SAl Viro 	__chk_user_ptr(addr);						\
53bb898558SAl Viro 	asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0"		\
54bb898558SAl Viro 	    : "=&r" (flag), "=r" (roksum)				\
55bb898558SAl Viro 	    : "1" (addr), "g" ((long)(size)),				\
56bb898558SAl Viro 	      "rm" (current_thread_info()->addr_limit.seg));		\
57bb898558SAl Viro 	flag;								\
58bb898558SAl Viro })
59bb898558SAl Viro 
60bb898558SAl Viro /**
61bb898558SAl Viro  * access_ok: - Checks if a user space pointer is valid
62bb898558SAl Viro  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
63bb898558SAl Viro  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
64bb898558SAl Viro  *        to write to a block, it is always safe to read from it.
65bb898558SAl Viro  * @addr: User space pointer to start of block to check
66bb898558SAl Viro  * @size: Size of block to check
67bb898558SAl Viro  *
68bb898558SAl Viro  * Context: User context only.  This function may sleep.
69bb898558SAl Viro  *
70bb898558SAl Viro  * Checks if a pointer to a block of memory in user space is valid.
71bb898558SAl Viro  *
72bb898558SAl Viro  * Returns true (nonzero) if the memory block may be valid, false (zero)
73bb898558SAl Viro  * if it is definitely invalid.
74bb898558SAl Viro  *
75bb898558SAl Viro  * Note that, depending on architecture, this function probably just
76bb898558SAl Viro  * checks that the pointer is in the user space range - after calling
77bb898558SAl Viro  * this function, memory access functions may still return -EFAULT.
78bb898558SAl Viro  */
79bb898558SAl Viro #define access_ok(type, addr, size) (likely(__range_not_ok(addr, size) == 0))
80bb898558SAl Viro 
81bb898558SAl Viro /*
82bb898558SAl Viro  * The exception table consists of pairs of addresses: the first is the
83bb898558SAl Viro  * address of an instruction that is allowed to fault, and the second is
84bb898558SAl Viro  * the address at which the program should continue.  No registers are
85bb898558SAl Viro  * modified, so it is entirely up to the continuation code to figure out
86bb898558SAl Viro  * what to do.
87bb898558SAl Viro  *
88bb898558SAl Viro  * All the routines below use bits of fixup code that are out of line
89bb898558SAl Viro  * with the main instruction path.  This means when everything is well,
90bb898558SAl Viro  * we don't even have to jump over them.  Further, they do not intrude
91bb898558SAl Viro  * on our cache or tlb entries.
92bb898558SAl Viro  */
93bb898558SAl Viro 
94bb898558SAl Viro struct exception_table_entry {
95bb898558SAl Viro 	unsigned long insn, fixup;
96bb898558SAl Viro };
97bb898558SAl Viro 
98bb898558SAl Viro extern int fixup_exception(struct pt_regs *regs);
99bb898558SAl Viro 
100bb898558SAl Viro /*
101bb898558SAl Viro  * These are the main single-value transfer routines.  They automatically
102bb898558SAl Viro  * use the right size if we just have the right pointer type.
103bb898558SAl Viro  *
104bb898558SAl Viro  * This gets kind of ugly. We want to return _two_ values in "get_user()"
105bb898558SAl Viro  * and yet we don't want to do any pointers, because that is too much
106bb898558SAl Viro  * of a performance impact. Thus we have a few rather ugly macros here,
107bb898558SAl Viro  * and hide all the ugliness from the user.
108bb898558SAl Viro  *
109bb898558SAl Viro  * The "__xxx" versions of the user access functions are versions that
110bb898558SAl Viro  * do not verify the address space, that must have been done previously
111bb898558SAl Viro  * with a separate "access_ok()" call (this is used when we do multiple
112bb898558SAl Viro  * accesses to the same area of user memory).
113bb898558SAl Viro  */
114bb898558SAl Viro 
115bb898558SAl Viro extern int __get_user_1(void);
116bb898558SAl Viro extern int __get_user_2(void);
117bb898558SAl Viro extern int __get_user_4(void);
118bb898558SAl Viro extern int __get_user_8(void);
119bb898558SAl Viro extern int __get_user_bad(void);
120bb898558SAl Viro 
121bb898558SAl Viro #define __get_user_x(size, ret, x, ptr)		      \
122bb898558SAl Viro 	asm volatile("call __get_user_" #size	      \
123bb898558SAl Viro 		     : "=a" (ret), "=d" (x)	      \
124bb898558SAl Viro 		     : "0" (ptr))		      \
125bb898558SAl Viro 
126bb898558SAl Viro /* Careful: we have to cast the result to the type of the pointer
127bb898558SAl Viro  * for sign reasons */
128bb898558SAl Viro 
129bb898558SAl Viro /**
130bb898558SAl Viro  * get_user: - Get a simple variable from user space.
131bb898558SAl Viro  * @x:   Variable to store result.
132bb898558SAl Viro  * @ptr: Source address, in user space.
133bb898558SAl Viro  *
134bb898558SAl Viro  * Context: User context only.  This function may sleep.
135bb898558SAl Viro  *
136bb898558SAl Viro  * This macro copies a single simple variable from user space to kernel
137bb898558SAl Viro  * space.  It supports simple types like char and int, but not larger
138bb898558SAl Viro  * data types like structures or arrays.
139bb898558SAl Viro  *
140bb898558SAl Viro  * @ptr must have pointer-to-simple-variable type, and the result of
141bb898558SAl Viro  * dereferencing @ptr must be assignable to @x without a cast.
142bb898558SAl Viro  *
143bb898558SAl Viro  * Returns zero on success, or -EFAULT on error.
144bb898558SAl Viro  * On error, the variable @x is set to zero.
145bb898558SAl Viro  */
146bb898558SAl Viro #ifdef CONFIG_X86_32
147bb898558SAl Viro #define __get_user_8(__ret_gu, __val_gu, ptr)				\
148bb898558SAl Viro 		__get_user_x(X, __ret_gu, __val_gu, ptr)
149bb898558SAl Viro #else
150bb898558SAl Viro #define __get_user_8(__ret_gu, __val_gu, ptr)				\
151bb898558SAl Viro 		__get_user_x(8, __ret_gu, __val_gu, ptr)
152bb898558SAl Viro #endif
153bb898558SAl Viro 
154bb898558SAl Viro #define get_user(x, ptr)						\
155bb898558SAl Viro ({									\
156bb898558SAl Viro 	int __ret_gu;							\
157bb898558SAl Viro 	unsigned long __val_gu;						\
158bb898558SAl Viro 	__chk_user_ptr(ptr);						\
159d1a76187SIngo Molnar 	might_fault();							\
160bb898558SAl Viro 	switch (sizeof(*(ptr))) {					\
161bb898558SAl Viro 	case 1:								\
162bb898558SAl Viro 		__get_user_x(1, __ret_gu, __val_gu, ptr);		\
163bb898558SAl Viro 		break;							\
164bb898558SAl Viro 	case 2:								\
165bb898558SAl Viro 		__get_user_x(2, __ret_gu, __val_gu, ptr);		\
166bb898558SAl Viro 		break;							\
167bb898558SAl Viro 	case 4:								\
168bb898558SAl Viro 		__get_user_x(4, __ret_gu, __val_gu, ptr);		\
169bb898558SAl Viro 		break;							\
170bb898558SAl Viro 	case 8:								\
171bb898558SAl Viro 		__get_user_8(__ret_gu, __val_gu, ptr);			\
172bb898558SAl Viro 		break;							\
173bb898558SAl Viro 	default:							\
174bb898558SAl Viro 		__get_user_x(X, __ret_gu, __val_gu, ptr);		\
175bb898558SAl Viro 		break;							\
176bb898558SAl Viro 	}								\
177bb898558SAl Viro 	(x) = (__typeof__(*(ptr)))__val_gu;				\
178bb898558SAl Viro 	__ret_gu;							\
179bb898558SAl Viro })
180bb898558SAl Viro 
181bb898558SAl Viro #define __put_user_x(size, x, ptr, __ret_pu)			\
182bb898558SAl Viro 	asm volatile("call __put_user_" #size : "=a" (__ret_pu)	\
183bb898558SAl Viro 		     : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
184bb898558SAl Viro 
185bb898558SAl Viro 
186bb898558SAl Viro 
187bb898558SAl Viro #ifdef CONFIG_X86_32
18818114f61SHiroshi Shimamoto #define __put_user_asm_u64(x, addr, err, errret)			\
189bb898558SAl Viro 	asm volatile("1:	movl %%eax,0(%2)\n"			\
190bb898558SAl Viro 		     "2:	movl %%edx,4(%2)\n"			\
191bb898558SAl Viro 		     "3:\n"						\
192bb898558SAl Viro 		     ".section .fixup,\"ax\"\n"				\
193bb898558SAl Viro 		     "4:	movl %3,%0\n"				\
194bb898558SAl Viro 		     "	jmp 3b\n"					\
195bb898558SAl Viro 		     ".previous\n"					\
196bb898558SAl Viro 		     _ASM_EXTABLE(1b, 4b)				\
197bb898558SAl Viro 		     _ASM_EXTABLE(2b, 4b)				\
198bb898558SAl Viro 		     : "=r" (err)					\
19918114f61SHiroshi Shimamoto 		     : "A" (x), "r" (addr), "i" (errret), "0" (err))
200bb898558SAl Viro 
201fe40c0afSHiroshi Shimamoto #define __put_user_asm_ex_u64(x, addr)					\
202fe40c0afSHiroshi Shimamoto 	asm volatile("1:	movl %%eax,0(%1)\n"			\
203fe40c0afSHiroshi Shimamoto 		     "2:	movl %%edx,4(%1)\n"			\
204fe40c0afSHiroshi Shimamoto 		     "3:\n"						\
205fe40c0afSHiroshi Shimamoto 		     _ASM_EXTABLE(1b, 2b - 1b)				\
206fe40c0afSHiroshi Shimamoto 		     _ASM_EXTABLE(2b, 3b - 2b)				\
207fe40c0afSHiroshi Shimamoto 		     : : "A" (x), "r" (addr))
208fe40c0afSHiroshi Shimamoto 
209bb898558SAl Viro #define __put_user_x8(x, ptr, __ret_pu)				\
210bb898558SAl Viro 	asm volatile("call __put_user_8" : "=a" (__ret_pu)	\
211bb898558SAl Viro 		     : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
212bb898558SAl Viro #else
21318114f61SHiroshi Shimamoto #define __put_user_asm_u64(x, ptr, retval, errret) \
214ebe119cdSH. Peter Anvin 	__put_user_asm(x, ptr, retval, "q", "", "er", errret)
215fe40c0afSHiroshi Shimamoto #define __put_user_asm_ex_u64(x, addr)	\
216ebe119cdSH. Peter Anvin 	__put_user_asm_ex(x, addr, "q", "", "er")
217bb898558SAl Viro #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
218bb898558SAl Viro #endif
219bb898558SAl Viro 
220bb898558SAl Viro extern void __put_user_bad(void);
221bb898558SAl Viro 
222bb898558SAl Viro /*
223bb898558SAl Viro  * Strange magic calling convention: pointer in %ecx,
224bb898558SAl Viro  * value in %eax(:%edx), return value in %eax. clobbers %rbx
225bb898558SAl Viro  */
226bb898558SAl Viro extern void __put_user_1(void);
227bb898558SAl Viro extern void __put_user_2(void);
228bb898558SAl Viro extern void __put_user_4(void);
229bb898558SAl Viro extern void __put_user_8(void);
230bb898558SAl Viro 
231bb898558SAl Viro #ifdef CONFIG_X86_WP_WORKS_OK
232bb898558SAl Viro 
233bb898558SAl Viro /**
234bb898558SAl Viro  * put_user: - Write a simple value into user space.
235bb898558SAl Viro  * @x:   Value to copy to user space.
236bb898558SAl Viro  * @ptr: Destination address, in user space.
237bb898558SAl Viro  *
238bb898558SAl Viro  * Context: User context only.  This function may sleep.
239bb898558SAl Viro  *
240bb898558SAl Viro  * This macro copies a single simple value from kernel space to user
241bb898558SAl Viro  * space.  It supports simple types like char and int, but not larger
242bb898558SAl Viro  * data types like structures or arrays.
243bb898558SAl Viro  *
244bb898558SAl Viro  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
245bb898558SAl Viro  * to the result of dereferencing @ptr.
246bb898558SAl Viro  *
247bb898558SAl Viro  * Returns zero on success, or -EFAULT on error.
248bb898558SAl Viro  */
249bb898558SAl Viro #define put_user(x, ptr)					\
250bb898558SAl Viro ({								\
251bb898558SAl Viro 	int __ret_pu;						\
252bb898558SAl Viro 	__typeof__(*(ptr)) __pu_val;				\
253bb898558SAl Viro 	__chk_user_ptr(ptr);					\
254d1a76187SIngo Molnar 	might_fault();						\
255bb898558SAl Viro 	__pu_val = x;						\
256bb898558SAl Viro 	switch (sizeof(*(ptr))) {				\
257bb898558SAl Viro 	case 1:							\
258bb898558SAl Viro 		__put_user_x(1, __pu_val, ptr, __ret_pu);	\
259bb898558SAl Viro 		break;						\
260bb898558SAl Viro 	case 2:							\
261bb898558SAl Viro 		__put_user_x(2, __pu_val, ptr, __ret_pu);	\
262bb898558SAl Viro 		break;						\
263bb898558SAl Viro 	case 4:							\
264bb898558SAl Viro 		__put_user_x(4, __pu_val, ptr, __ret_pu);	\
265bb898558SAl Viro 		break;						\
266bb898558SAl Viro 	case 8:							\
267bb898558SAl Viro 		__put_user_x8(__pu_val, ptr, __ret_pu);		\
268bb898558SAl Viro 		break;						\
269bb898558SAl Viro 	default:						\
270bb898558SAl Viro 		__put_user_x(X, __pu_val, ptr, __ret_pu);	\
271bb898558SAl Viro 		break;						\
272bb898558SAl Viro 	}							\
273bb898558SAl Viro 	__ret_pu;						\
274bb898558SAl Viro })
275bb898558SAl Viro 
276bb898558SAl Viro #define __put_user_size(x, ptr, size, retval, errret)			\
277bb898558SAl Viro do {									\
278bb898558SAl Viro 	retval = 0;							\
279bb898558SAl Viro 	__chk_user_ptr(ptr);						\
280bb898558SAl Viro 	switch (size) {							\
281bb898558SAl Viro 	case 1:								\
282bb898558SAl Viro 		__put_user_asm(x, ptr, retval, "b", "b", "iq", errret);	\
283bb898558SAl Viro 		break;							\
284bb898558SAl Viro 	case 2:								\
285bb898558SAl Viro 		__put_user_asm(x, ptr, retval, "w", "w", "ir", errret);	\
286bb898558SAl Viro 		break;							\
287bb898558SAl Viro 	case 4:								\
288bb898558SAl Viro 		__put_user_asm(x, ptr, retval, "l", "k", "ir", errret);	\
289bb898558SAl Viro 		break;							\
290bb898558SAl Viro 	case 8:								\
29118114f61SHiroshi Shimamoto 		__put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval,	\
29218114f61SHiroshi Shimamoto 				   errret);				\
293bb898558SAl Viro 		break;							\
294bb898558SAl Viro 	default:							\
295bb898558SAl Viro 		__put_user_bad();					\
296bb898558SAl Viro 	}								\
297bb898558SAl Viro } while (0)
298bb898558SAl Viro 
299fe40c0afSHiroshi Shimamoto #define __put_user_size_ex(x, ptr, size)				\
300fe40c0afSHiroshi Shimamoto do {									\
301fe40c0afSHiroshi Shimamoto 	__chk_user_ptr(ptr);						\
302fe40c0afSHiroshi Shimamoto 	switch (size) {							\
303fe40c0afSHiroshi Shimamoto 	case 1:								\
304fe40c0afSHiroshi Shimamoto 		__put_user_asm_ex(x, ptr, "b", "b", "iq");		\
305fe40c0afSHiroshi Shimamoto 		break;							\
306fe40c0afSHiroshi Shimamoto 	case 2:								\
307fe40c0afSHiroshi Shimamoto 		__put_user_asm_ex(x, ptr, "w", "w", "ir");		\
308fe40c0afSHiroshi Shimamoto 		break;							\
309fe40c0afSHiroshi Shimamoto 	case 4:								\
310fe40c0afSHiroshi Shimamoto 		__put_user_asm_ex(x, ptr, "l", "k", "ir");		\
311fe40c0afSHiroshi Shimamoto 		break;							\
312fe40c0afSHiroshi Shimamoto 	case 8:								\
313fe40c0afSHiroshi Shimamoto 		__put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr);	\
314fe40c0afSHiroshi Shimamoto 		break;							\
315fe40c0afSHiroshi Shimamoto 	default:							\
316fe40c0afSHiroshi Shimamoto 		__put_user_bad();					\
317fe40c0afSHiroshi Shimamoto 	}								\
318fe40c0afSHiroshi Shimamoto } while (0)
319fe40c0afSHiroshi Shimamoto 
320bb898558SAl Viro #else
321bb898558SAl Viro 
322bb898558SAl Viro #define __put_user_size(x, ptr, size, retval, errret)			\
323bb898558SAl Viro do {									\
324bb898558SAl Viro 	__typeof__(*(ptr))__pus_tmp = x;				\
325bb898558SAl Viro 	retval = 0;							\
326bb898558SAl Viro 									\
327bb898558SAl Viro 	if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0))	\
328bb898558SAl Viro 		retval = errret;					\
329bb898558SAl Viro } while (0)
330bb898558SAl Viro 
331bb898558SAl Viro #define put_user(x, ptr)					\
332bb898558SAl Viro ({								\
333bb898558SAl Viro 	int __ret_pu;						\
334bb898558SAl Viro 	__typeof__(*(ptr))__pus_tmp = x;			\
335bb898558SAl Viro 	__ret_pu = 0;						\
336bb898558SAl Viro 	if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp,		\
337bb898558SAl Viro 				       sizeof(*(ptr))) != 0))	\
338bb898558SAl Viro 		__ret_pu = -EFAULT;				\
339bb898558SAl Viro 	__ret_pu;						\
340bb898558SAl Viro })
341bb898558SAl Viro #endif
342bb898558SAl Viro 
343bb898558SAl Viro #ifdef CONFIG_X86_32
344bb898558SAl Viro #define __get_user_asm_u64(x, ptr, retval, errret)	(x) = __get_user_bad()
345fe40c0afSHiroshi Shimamoto #define __get_user_asm_ex_u64(x, ptr)			(x) = __get_user_bad()
346bb898558SAl Viro #else
347bb898558SAl Viro #define __get_user_asm_u64(x, ptr, retval, errret) \
348bb898558SAl Viro 	 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
349fe40c0afSHiroshi Shimamoto #define __get_user_asm_ex_u64(x, ptr) \
350fe40c0afSHiroshi Shimamoto 	 __get_user_asm_ex(x, ptr, "q", "", "=r")
351bb898558SAl Viro #endif
352bb898558SAl Viro 
353bb898558SAl Viro #define __get_user_size(x, ptr, size, retval, errret)			\
354bb898558SAl Viro do {									\
355bb898558SAl Viro 	retval = 0;							\
356bb898558SAl Viro 	__chk_user_ptr(ptr);						\
357bb898558SAl Viro 	switch (size) {							\
358bb898558SAl Viro 	case 1:								\
359bb898558SAl Viro 		__get_user_asm(x, ptr, retval, "b", "b", "=q", errret);	\
360bb898558SAl Viro 		break;							\
361bb898558SAl Viro 	case 2:								\
362bb898558SAl Viro 		__get_user_asm(x, ptr, retval, "w", "w", "=r", errret);	\
363bb898558SAl Viro 		break;							\
364bb898558SAl Viro 	case 4:								\
365bb898558SAl Viro 		__get_user_asm(x, ptr, retval, "l", "k", "=r", errret);	\
366bb898558SAl Viro 		break;							\
367bb898558SAl Viro 	case 8:								\
368bb898558SAl Viro 		__get_user_asm_u64(x, ptr, retval, errret);		\
369bb898558SAl Viro 		break;							\
370bb898558SAl Viro 	default:							\
371bb898558SAl Viro 		(x) = __get_user_bad();					\
372bb898558SAl Viro 	}								\
373bb898558SAl Viro } while (0)
374bb898558SAl Viro 
375bb898558SAl Viro #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)	\
376bb898558SAl Viro 	asm volatile("1:	mov"itype" %2,%"rtype"1\n"		\
377bb898558SAl Viro 		     "2:\n"						\
378bb898558SAl Viro 		     ".section .fixup,\"ax\"\n"				\
379bb898558SAl Viro 		     "3:	mov %3,%0\n"				\
380bb898558SAl Viro 		     "	xor"itype" %"rtype"1,%"rtype"1\n"		\
381bb898558SAl Viro 		     "	jmp 2b\n"					\
382bb898558SAl Viro 		     ".previous\n"					\
383bb898558SAl Viro 		     _ASM_EXTABLE(1b, 3b)				\
384bb898558SAl Viro 		     : "=r" (err), ltype(x)				\
385bb898558SAl Viro 		     : "m" (__m(addr)), "i" (errret), "0" (err))
386bb898558SAl Viro 
387fe40c0afSHiroshi Shimamoto #define __get_user_size_ex(x, ptr, size)				\
388fe40c0afSHiroshi Shimamoto do {									\
389fe40c0afSHiroshi Shimamoto 	__chk_user_ptr(ptr);						\
390fe40c0afSHiroshi Shimamoto 	switch (size) {							\
391fe40c0afSHiroshi Shimamoto 	case 1:								\
392fe40c0afSHiroshi Shimamoto 		__get_user_asm_ex(x, ptr, "b", "b", "=q");		\
393fe40c0afSHiroshi Shimamoto 		break;							\
394fe40c0afSHiroshi Shimamoto 	case 2:								\
395fe40c0afSHiroshi Shimamoto 		__get_user_asm_ex(x, ptr, "w", "w", "=r");		\
396fe40c0afSHiroshi Shimamoto 		break;							\
397fe40c0afSHiroshi Shimamoto 	case 4:								\
398fe40c0afSHiroshi Shimamoto 		__get_user_asm_ex(x, ptr, "l", "k", "=r");		\
399fe40c0afSHiroshi Shimamoto 		break;							\
400fe40c0afSHiroshi Shimamoto 	case 8:								\
401fe40c0afSHiroshi Shimamoto 		__get_user_asm_ex_u64(x, ptr);				\
402fe40c0afSHiroshi Shimamoto 		break;							\
403fe40c0afSHiroshi Shimamoto 	default:							\
404fe40c0afSHiroshi Shimamoto 		(x) = __get_user_bad();					\
405fe40c0afSHiroshi Shimamoto 	}								\
406fe40c0afSHiroshi Shimamoto } while (0)
407fe40c0afSHiroshi Shimamoto 
408fe40c0afSHiroshi Shimamoto #define __get_user_asm_ex(x, addr, itype, rtype, ltype)			\
409fe40c0afSHiroshi Shimamoto 	asm volatile("1:	mov"itype" %1,%"rtype"0\n"		\
410fe40c0afSHiroshi Shimamoto 		     "2:\n"						\
411fe40c0afSHiroshi Shimamoto 		     _ASM_EXTABLE(1b, 2b - 1b)				\
412fe40c0afSHiroshi Shimamoto 		     : ltype(x) : "m" (__m(addr)))
413fe40c0afSHiroshi Shimamoto 
414bb898558SAl Viro #define __put_user_nocheck(x, ptr, size)			\
415bb898558SAl Viro ({								\
41616855f87SHiroshi Shimamoto 	int __pu_err;						\
417bb898558SAl Viro 	__put_user_size((x), (ptr), (size), __pu_err, -EFAULT);	\
418bb898558SAl Viro 	__pu_err;						\
419bb898558SAl Viro })
420bb898558SAl Viro 
421bb898558SAl Viro #define __get_user_nocheck(x, ptr, size)				\
422bb898558SAl Viro ({									\
42316855f87SHiroshi Shimamoto 	int __gu_err;							\
424bb898558SAl Viro 	unsigned long __gu_val;						\
425bb898558SAl Viro 	__get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT);	\
426bb898558SAl Viro 	(x) = (__force __typeof__(*(ptr)))__gu_val;			\
427bb898558SAl Viro 	__gu_err;							\
428bb898558SAl Viro })
429bb898558SAl Viro 
430bb898558SAl Viro /* FIXME: this hack is definitely wrong -AK */
431bb898558SAl Viro struct __large_struct { unsigned long buf[100]; };
432bb898558SAl Viro #define __m(x) (*(struct __large_struct __user *)(x))
433bb898558SAl Viro 
434bb898558SAl Viro /*
435bb898558SAl Viro  * Tell gcc we read from memory instead of writing: this is because
436bb898558SAl Viro  * we do not write to any memory gcc knows about, so there are no
437bb898558SAl Viro  * aliasing issues.
438bb898558SAl Viro  */
439bb898558SAl Viro #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)	\
440bb898558SAl Viro 	asm volatile("1:	mov"itype" %"rtype"1,%2\n"		\
441bb898558SAl Viro 		     "2:\n"						\
442bb898558SAl Viro 		     ".section .fixup,\"ax\"\n"				\
443bb898558SAl Viro 		     "3:	mov %3,%0\n"				\
444bb898558SAl Viro 		     "	jmp 2b\n"					\
445bb898558SAl Viro 		     ".previous\n"					\
446bb898558SAl Viro 		     _ASM_EXTABLE(1b, 3b)				\
447bb898558SAl Viro 		     : "=r"(err)					\
448bb898558SAl Viro 		     : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
449fe40c0afSHiroshi Shimamoto 
450fe40c0afSHiroshi Shimamoto #define __put_user_asm_ex(x, addr, itype, rtype, ltype)			\
451fe40c0afSHiroshi Shimamoto 	asm volatile("1:	mov"itype" %"rtype"0,%1\n"		\
452fe40c0afSHiroshi Shimamoto 		     "2:\n"						\
453fe40c0afSHiroshi Shimamoto 		     _ASM_EXTABLE(1b, 2b - 1b)				\
454fe40c0afSHiroshi Shimamoto 		     : : ltype(x), "m" (__m(addr)))
455fe40c0afSHiroshi Shimamoto 
456fe40c0afSHiroshi Shimamoto /*
457fe40c0afSHiroshi Shimamoto  * uaccess_try and catch
458fe40c0afSHiroshi Shimamoto  */
459fe40c0afSHiroshi Shimamoto #define uaccess_try	do {						\
460fe40c0afSHiroshi Shimamoto 	int prev_err = current_thread_info()->uaccess_err;		\
461fe40c0afSHiroshi Shimamoto 	current_thread_info()->uaccess_err = 0;				\
462fe40c0afSHiroshi Shimamoto 	barrier();
463fe40c0afSHiroshi Shimamoto 
464fe40c0afSHiroshi Shimamoto #define uaccess_catch(err)						\
465fe40c0afSHiroshi Shimamoto 	(err) |= current_thread_info()->uaccess_err;			\
466fe40c0afSHiroshi Shimamoto 	current_thread_info()->uaccess_err = prev_err;			\
467fe40c0afSHiroshi Shimamoto } while (0)
468fe40c0afSHiroshi Shimamoto 
469bb898558SAl Viro /**
470bb898558SAl Viro  * __get_user: - Get a simple variable from user space, with less checking.
471bb898558SAl Viro  * @x:   Variable to store result.
472bb898558SAl Viro  * @ptr: Source address, in user space.
473bb898558SAl Viro  *
474bb898558SAl Viro  * Context: User context only.  This function may sleep.
475bb898558SAl Viro  *
476bb898558SAl Viro  * This macro copies a single simple variable from user space to kernel
477bb898558SAl Viro  * space.  It supports simple types like char and int, but not larger
478bb898558SAl Viro  * data types like structures or arrays.
479bb898558SAl Viro  *
480bb898558SAl Viro  * @ptr must have pointer-to-simple-variable type, and the result of
481bb898558SAl Viro  * dereferencing @ptr must be assignable to @x without a cast.
482bb898558SAl Viro  *
483bb898558SAl Viro  * Caller must check the pointer with access_ok() before calling this
484bb898558SAl Viro  * function.
485bb898558SAl Viro  *
486bb898558SAl Viro  * Returns zero on success, or -EFAULT on error.
487bb898558SAl Viro  * On error, the variable @x is set to zero.
488bb898558SAl Viro  */
489bb898558SAl Viro 
490bb898558SAl Viro #define __get_user(x, ptr)						\
491bb898558SAl Viro 	__get_user_nocheck((x), (ptr), sizeof(*(ptr)))
492fe40c0afSHiroshi Shimamoto 
493bb898558SAl Viro /**
494bb898558SAl Viro  * __put_user: - Write a simple value into user space, with less checking.
495bb898558SAl Viro  * @x:   Value to copy to user space.
496bb898558SAl Viro  * @ptr: Destination address, in user space.
497bb898558SAl Viro  *
498bb898558SAl Viro  * Context: User context only.  This function may sleep.
499bb898558SAl Viro  *
500bb898558SAl Viro  * This macro copies a single simple value from kernel space to user
501bb898558SAl Viro  * space.  It supports simple types like char and int, but not larger
502bb898558SAl Viro  * data types like structures or arrays.
503bb898558SAl Viro  *
504bb898558SAl Viro  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
505bb898558SAl Viro  * to the result of dereferencing @ptr.
506bb898558SAl Viro  *
507bb898558SAl Viro  * Caller must check the pointer with access_ok() before calling this
508bb898558SAl Viro  * function.
509bb898558SAl Viro  *
510bb898558SAl Viro  * Returns zero on success, or -EFAULT on error.
511bb898558SAl Viro  */
512bb898558SAl Viro 
513bb898558SAl Viro #define __put_user(x, ptr)						\
514bb898558SAl Viro 	__put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
515bb898558SAl Viro 
516bb898558SAl Viro #define __get_user_unaligned __get_user
517bb898558SAl Viro #define __put_user_unaligned __put_user
518bb898558SAl Viro 
519bb898558SAl Viro /*
520fe40c0afSHiroshi Shimamoto  * {get|put}_user_try and catch
521fe40c0afSHiroshi Shimamoto  *
522fe40c0afSHiroshi Shimamoto  * get_user_try {
523fe40c0afSHiroshi Shimamoto  *	get_user_ex(...);
524fe40c0afSHiroshi Shimamoto  * } get_user_catch(err)
525fe40c0afSHiroshi Shimamoto  */
526fe40c0afSHiroshi Shimamoto #define get_user_try		uaccess_try
527fe40c0afSHiroshi Shimamoto #define get_user_catch(err)	uaccess_catch(err)
528fe40c0afSHiroshi Shimamoto 
529fe40c0afSHiroshi Shimamoto #define get_user_ex(x, ptr)	do {					\
530fe40c0afSHiroshi Shimamoto 	unsigned long __gue_val;					\
531fe40c0afSHiroshi Shimamoto 	__get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr))));	\
532fe40c0afSHiroshi Shimamoto 	(x) = (__force __typeof__(*(ptr)))__gue_val;			\
533fe40c0afSHiroshi Shimamoto } while (0)
534fe40c0afSHiroshi Shimamoto 
535019a1369SHiroshi Shimamoto #ifdef CONFIG_X86_WP_WORKS_OK
536019a1369SHiroshi Shimamoto 
537019a1369SHiroshi Shimamoto #define put_user_try		uaccess_try
538019a1369SHiroshi Shimamoto #define put_user_catch(err)	uaccess_catch(err)
539019a1369SHiroshi Shimamoto 
540fe40c0afSHiroshi Shimamoto #define put_user_ex(x, ptr)						\
541fe40c0afSHiroshi Shimamoto 	__put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
542fe40c0afSHiroshi Shimamoto 
543019a1369SHiroshi Shimamoto #else /* !CONFIG_X86_WP_WORKS_OK */
544019a1369SHiroshi Shimamoto 
545019a1369SHiroshi Shimamoto #define put_user_try		do {		\
546019a1369SHiroshi Shimamoto 	int __uaccess_err = 0;
547019a1369SHiroshi Shimamoto 
548019a1369SHiroshi Shimamoto #define put_user_catch(err)			\
549019a1369SHiroshi Shimamoto 	(err) |= __uaccess_err;			\
550019a1369SHiroshi Shimamoto } while (0)
551019a1369SHiroshi Shimamoto 
552019a1369SHiroshi Shimamoto #define put_user_ex(x, ptr)	do {		\
553019a1369SHiroshi Shimamoto 	__uaccess_err |= __put_user(x, ptr);	\
554019a1369SHiroshi Shimamoto } while (0)
555019a1369SHiroshi Shimamoto 
556019a1369SHiroshi Shimamoto #endif /* CONFIG_X86_WP_WORKS_OK */
557019a1369SHiroshi Shimamoto 
558*1ac2e6caSRobert Richter extern unsigned long
559*1ac2e6caSRobert Richter copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
560*1ac2e6caSRobert Richter 
561fe40c0afSHiroshi Shimamoto /*
562bb898558SAl Viro  * movsl can be slow when source and dest are not both 8-byte aligned
563bb898558SAl Viro  */
564bb898558SAl Viro #ifdef CONFIG_X86_INTEL_USERCOPY
565bb898558SAl Viro extern struct movsl_mask {
566bb898558SAl Viro 	int mask;
567bb898558SAl Viro } ____cacheline_aligned_in_smp movsl_mask;
568bb898558SAl Viro #endif
569bb898558SAl Viro 
570bb898558SAl Viro #define ARCH_HAS_NOCACHE_UACCESS 1
571bb898558SAl Viro 
572bb898558SAl Viro #ifdef CONFIG_X86_32
573bb898558SAl Viro # include "uaccess_32.h"
574bb898558SAl Viro #else
575bb898558SAl Viro # include "uaccess_64.h"
576bb898558SAl Viro #endif
577bb898558SAl Viro 
5781965aae3SH. Peter Anvin #endif /* _ASM_X86_UACCESS_H */
579bb898558SAl Viro 
580