xref: /linux/arch/x86/lib/getuser.S (revision 0d6ccfe6b319d56da63b7d7cfbcecd92780a680d)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * __get_user functions.
4 *
5 * (C) Copyright 1998 Linus Torvalds
6 * (C) Copyright 2005 Andi Kleen
7 * (C) Copyright 2008 Glauber Costa
8 *
9 * These functions have a non-standard call interface
10 * to make them more efficient, especially as they
11 * return an error value in addition to the "real"
12 * return value.
13 */
14
15/*
16 * __get_user_X
17 *
18 * Inputs:	%[r|e]ax contains the address.
19 *
20 * Outputs:	%[r|e]ax is error code (0 or -EFAULT)
21 *		%[r|e]dx contains zero-extended value
22 *		%ecx contains the high half for 32-bit __get_user_8
23 *
24 *
25 * These functions should not modify any other registers,
26 * as they get called from within inline assembly.
27 */
28
29#include <linux/export.h>
30#include <linux/linkage.h>
31#include <asm/page_types.h>
32#include <asm/errno.h>
33#include <asm/asm-offsets.h>
34#include <asm/thread_info.h>
35#include <asm/asm.h>
36#include <asm/smap.h>
37
38#define ASM_BARRIER_NOSPEC ALTERNATIVE "", "lfence", X86_FEATURE_LFENCE_RDTSC
39
40.macro check_range size:req
41.if IS_ENABLED(CONFIG_X86_64)
42	mov %rax, %rdx
43	sar $63, %rdx
44	or %rdx, %rax
45.else
46	cmp $TASK_SIZE_MAX-\size+1, %eax
47	jae .Lbad_get_user
48	sbb %edx, %edx		/* array_index_mask_nospec() */
49	and %edx, %eax
50.endif
51.endm
52
53.macro UACCESS op src dst
541:	\op \src,\dst
55	_ASM_EXTABLE_UA(1b, __get_user_handle_exception)
56.endm
57
58
59	.text
60SYM_FUNC_START(__get_user_1)
61	check_range size=1
62	ASM_STAC
63	UACCESS movzbl (%_ASM_AX),%edx
64	xor %eax,%eax
65	ASM_CLAC
66	RET
67SYM_FUNC_END(__get_user_1)
68EXPORT_SYMBOL(__get_user_1)
69
70SYM_FUNC_START(__get_user_2)
71	check_range size=2
72	ASM_STAC
73	UACCESS movzwl (%_ASM_AX),%edx
74	xor %eax,%eax
75	ASM_CLAC
76	RET
77SYM_FUNC_END(__get_user_2)
78EXPORT_SYMBOL(__get_user_2)
79
80SYM_FUNC_START(__get_user_4)
81	check_range size=4
82	ASM_STAC
83	UACCESS movl (%_ASM_AX),%edx
84	xor %eax,%eax
85	ASM_CLAC
86	RET
87SYM_FUNC_END(__get_user_4)
88EXPORT_SYMBOL(__get_user_4)
89
90SYM_FUNC_START(__get_user_8)
91	check_range size=8
92	ASM_STAC
93#ifdef CONFIG_X86_64
94	UACCESS movq (%_ASM_AX),%rdx
95#else
96	xor %ecx,%ecx
97	UACCESS movl (%_ASM_AX),%edx
98	UACCESS movl 4(%_ASM_AX),%ecx
99#endif
100	xor %eax,%eax
101	ASM_CLAC
102	RET
103SYM_FUNC_END(__get_user_8)
104EXPORT_SYMBOL(__get_user_8)
105
106/* .. and the same for __get_user, just without the range checks */
107SYM_FUNC_START(__get_user_nocheck_1)
108	ASM_STAC
109	ASM_BARRIER_NOSPEC
110	UACCESS movzbl (%_ASM_AX),%edx
111	xor %eax,%eax
112	ASM_CLAC
113	RET
114SYM_FUNC_END(__get_user_nocheck_1)
115EXPORT_SYMBOL(__get_user_nocheck_1)
116
117SYM_FUNC_START(__get_user_nocheck_2)
118	ASM_STAC
119	ASM_BARRIER_NOSPEC
120	UACCESS movzwl (%_ASM_AX),%edx
121	xor %eax,%eax
122	ASM_CLAC
123	RET
124SYM_FUNC_END(__get_user_nocheck_2)
125EXPORT_SYMBOL(__get_user_nocheck_2)
126
127SYM_FUNC_START(__get_user_nocheck_4)
128	ASM_STAC
129	ASM_BARRIER_NOSPEC
130	UACCESS movl (%_ASM_AX),%edx
131	xor %eax,%eax
132	ASM_CLAC
133	RET
134SYM_FUNC_END(__get_user_nocheck_4)
135EXPORT_SYMBOL(__get_user_nocheck_4)
136
137SYM_FUNC_START(__get_user_nocheck_8)
138	ASM_STAC
139	ASM_BARRIER_NOSPEC
140#ifdef CONFIG_X86_64
141	UACCESS movq (%_ASM_AX),%rdx
142#else
143	xor %ecx,%ecx
144	UACCESS movl (%_ASM_AX),%edx
145	UACCESS movl 4(%_ASM_AX),%ecx
146#endif
147	xor %eax,%eax
148	ASM_CLAC
149	RET
150SYM_FUNC_END(__get_user_nocheck_8)
151EXPORT_SYMBOL(__get_user_nocheck_8)
152
153
154SYM_CODE_START_LOCAL(__get_user_handle_exception)
155	ASM_CLAC
156.Lbad_get_user:
157	xor %edx,%edx
158	mov $(-EFAULT),%_ASM_AX
159	RET
160SYM_CODE_END(__get_user_handle_exception)
161