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