xref: /linux/arch/x86/entry/syscall_32.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* 32-bit system call dispatch */
3 
4 #include <linux/linkage.h>
5 #include <linux/sys.h>
6 #include <linux/cache.h>
7 #include <linux/syscalls.h>
8 #include <linux/entry-common.h>
9 #include <linux/nospec.h>
10 #include <linux/uaccess.h>
11 #include <asm/apic.h>
12 #include <asm/traps.h>
13 #include <asm/cpufeature.h>
14 #include <asm/syscall.h>
15 
16 #ifdef CONFIG_IA32_EMULATION
17 #define __SYSCALL_WITH_COMPAT(nr, native, compat)	__SYSCALL(nr, compat)
18 #else
19 #define __SYSCALL_WITH_COMPAT(nr, native, compat)	__SYSCALL(nr, native)
20 #endif
21 
22 #define __SYSCALL(nr, sym) extern long __ia32_##sym(const struct pt_regs *);
23 #define __SYSCALL_NORETURN(nr, sym) extern long __noreturn __ia32_##sym(const struct pt_regs *);
24 #include <asm/syscalls_32.h>
25 #undef  __SYSCALL
26 
27 #undef  __SYSCALL_NORETURN
28 #define __SYSCALL_NORETURN __SYSCALL
29 
30 /*
31  * The sys_call_table[] is no longer used for system calls, but
32  * kernel/trace/trace_syscalls.c still wants to know the system
33  * call address.
34  */
35 #ifdef CONFIG_X86_32
36 #define __SYSCALL(nr, sym) __ia32_##sym,
37 const sys_call_ptr_t sys_call_table[] = {
38 #include <asm/syscalls_32.h>
39 };
40 #undef  __SYSCALL
41 #endif
42 
43 #define __SYSCALL(nr, sym) case nr: return __ia32_##sym(regs);
44 
45 /* The unsigned int @nr argument is intentional as it creates denser code in a 64-bit build */
46 static noinline long ia32_sys_call(const struct pt_regs *regs, unsigned int nr)
47 {
48 	switch (nr) {
49 	#include <asm/syscalls_32.h>
50 	default: return __ia32_sys_ni_syscall(regs);
51 	}
52 }
53 
54 static __always_inline long syscall_32_enter(struct pt_regs *regs)
55 {
56 	if (IS_ENABLED(CONFIG_IA32_EMULATION))
57 		current_thread_info()->status |= TS_COMPAT;
58 
59 	return (int)regs->orig_ax;
60 }
61 
62 #ifdef CONFIG_IA32_EMULATION
63 bool __ia32_enabled __ro_after_init = !IS_ENABLED(CONFIG_IA32_EMULATION_DEFAULT_DISABLED);
64 
65 static int __init ia32_emulation_override_cmdline(char *arg)
66 {
67 	return kstrtobool(arg, &__ia32_enabled);
68 }
69 early_param("ia32_emulation", ia32_emulation_override_cmdline);
70 #endif
71 
72 /*
73  * Invoke a 32-bit syscall.  Called with IRQs on in CT_STATE_KERNEL.
74  */
75 static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs, unsigned long nr)
76 {
77 	if (likely(nr < IA32_NR_syscalls)) {
78 		nr = array_index_nospec(nr, IA32_NR_syscalls);
79 		regs->ax = ia32_sys_call(regs, (unsigned int)nr);
80 	} else {
81 		/* See comment in do_syscall_64() */
82 		if (nr != -1UL)
83 			regs->ax = -ENOSYS;
84 	}
85 }
86 
87 #ifdef CONFIG_IA32_EMULATION
88 static __always_inline bool int80_is_external(void)
89 {
90 	const unsigned int offs = (0x80 / 32) * 0x10;
91 	const u32 bit = BIT(0x80 % 32);
92 
93 	/* The local APIC on XENPV guests is fake */
94 	if (cpu_feature_enabled(X86_FEATURE_XENPV))
95 		return false;
96 
97 	/*
98 	 * If vector 0x80 is set in the APIC ISR then this is an external
99 	 * interrupt. Either from broken hardware or injected by a VMM.
100 	 *
101 	 * Note: In guest mode this is only valid for secure guests where
102 	 * the secure module fully controls the vAPIC exposed to the guest.
103 	 */
104 	return apic_read(APIC_ISR + offs) & bit;
105 }
106 
107 /**
108  * do_int80_emulation - 32-bit legacy syscall C entry from asm
109  * @regs: syscall arguments in struct pt_args on the stack.
110  *
111  * This entry point can be used by 32-bit and 64-bit programs to perform
112  * 32-bit system calls.  Instances of INT $0x80 can be found inline in
113  * various programs and libraries.  It is also used by the vDSO's
114  * __kernel_vsyscall fallback for hardware that doesn't support a faster
115  * entry method.  Restarted 32-bit system calls also fall back to INT
116  * $0x80 regardless of what instruction was originally used to do the
117  * system call.
118  *
119  * This is considered a slow path.  It is not used by most libc
120  * implementations on modern hardware except during process startup.
121  *
122  * The arguments for the INT $0x80 based syscall are on stack in the
123  * pt_regs structure:
124  *   eax:				system call number
125  *   ebx, ecx, edx, esi, edi, ebp:	arg1 - arg 6
126  */
127 __visible noinstr void do_int80_emulation(struct pt_regs *regs)
128 {
129 	long nr;
130 
131 	/* Kernel does not use INT $0x80! */
132 	if (unlikely(!user_mode(regs))) {
133 		irqentry_enter(regs);
134 		instrumentation_begin();
135 		panic("Unexpected external interrupt 0x80\n");
136 	}
137 
138 	/*
139 	 * Establish kernel context for instrumentation, including for
140 	 * int80_is_external() below which calls into the APIC driver.
141 	 * Identical for soft and external interrupts.
142 	 */
143 	enter_from_user_mode_randomize_stack(regs);
144 
145 	instrumentation_begin();
146 
147 	/* Validate that this is a soft interrupt to the extent possible */
148 	if (unlikely(int80_is_external()))
149 		panic("Unexpected external interrupt 0x80\n");
150 
151 	/*
152 	 * The low level idtentry code pushed -1 into regs::orig_ax
153 	 * and regs::ax contains the syscall number.
154 	 *
155 	 * User tracing code (ptrace or signal handlers) might assume
156 	 * that the regs::orig_ax contains a 32-bit number on invoking
157 	 * a 32-bit syscall.
158 	 *
159 	 * Establish the syscall convention by saving the 32bit truncated
160 	 * syscall number in regs::orig_ax and by invalidating regs::ax.
161 	 */
162 	regs->orig_ax = regs->ax & GENMASK(31, 0);
163 	regs->ax = -ENOSYS;
164 
165 	nr = syscall_32_enter(regs);
166 
167 	local_irq_enable();
168 
169 	if (likely(syscall_enter_from_user_mode_work(regs, &nr)))
170 		do_syscall_32_irqs_on(regs, nr);
171 
172 	instrumentation_end();
173 	syscall_exit_to_user_mode(regs);
174 }
175 
176 #ifdef CONFIG_X86_FRED
177 /*
178  * A FRED-specific INT80 handler is warranted for the follwing reasons:
179  *
180  * 1) As INT instructions and hardware interrupts are separate event
181  *    types, FRED does not preclude the use of vector 0x80 for external
182  *    interrupts. As a result, the FRED setup code does not reserve
183  *    vector 0x80 and calling int80_is_external() is not merely
184  *    suboptimal but actively incorrect: it could cause a system call
185  *    to be incorrectly ignored.
186  *
187  * 2) It is called only for handling vector 0x80 of event type
188  *    EVENT_TYPE_SWINT and will never be called to handle any external
189  *    interrupt (event type EVENT_TYPE_EXTINT).
190  *
191  * 3) FRED has separate entry flows depending on if the event came from
192  *    user space or kernel space, and because the kernel does not use
193  *    INT insns, the FRED kernel entry handler fred_entry_from_kernel()
194  *    falls through to fred_bad_type() if the event type is
195  *    EVENT_TYPE_SWINT, i.e., INT insns. So if the kernel is handling
196  *    an INT insn, it can only be from a user level.
197  *
198  * 4) int80_emulation() does a CLEAR_BRANCH_HISTORY. While FRED will
199  *    likely take a different approach if it is ever needed: it
200  *    probably belongs in either fred_intx()/ fred_other() or
201  *    asm_fred_entrypoint_user(), depending on if this ought to be done
202  *    for all entries from userspace or only system
203  *    calls.
204  *
205  * 5) INT $0x80 is the fast path for 32-bit system calls under FRED.
206  */
207 DEFINE_FREDENTRY_RAW(int80_emulation)
208 {
209 	long nr;
210 
211 	enter_from_user_mode_randomize_stack(regs);
212 
213 	instrumentation_begin();
214 	/*
215 	 * FRED pushed 0 into regs::orig_ax and regs::ax contains the
216 	 * syscall number.
217 	 *
218 	 * User tracing code (ptrace or signal handlers) might assume
219 	 * that the regs::orig_ax contains a 32-bit number on invoking
220 	 * a 32-bit syscall.
221 	 *
222 	 * Establish the syscall convention by saving the 32bit truncated
223 	 * syscall number in regs::orig_ax and by invalidating regs::ax.
224 	 */
225 	regs->orig_ax = regs->ax & GENMASK(31, 0);
226 	regs->ax = -ENOSYS;
227 
228 	nr = syscall_32_enter(regs);
229 
230 	local_irq_enable();
231 	if (likely(syscall_enter_from_user_mode_work(regs, &nr)))
232 		do_syscall_32_irqs_on(regs, nr);
233 
234 	instrumentation_end();
235 	syscall_exit_to_user_mode(regs);
236 }
237 #endif /* CONFIG_X86_FRED */
238 
239 #else /* CONFIG_IA32_EMULATION */
240 
241 /* Handles int $0x80 on a 32bit kernel */
242 __visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
243 {
244 	long nr = syscall_32_enter(regs);
245 
246 	/*
247 	 * Subtlety here: if ptrace pokes something larger than 2^31-1 into
248 	 * orig_ax, the int return value truncates it. This matches
249 	 * the semantics of syscall_get_nr().
250 	 */
251 	if (likely(syscall_enter_from_user_mode_randomize_stack(regs, &nr))) {
252 		instrumentation_begin();
253 
254 		do_syscall_32_irqs_on(regs, nr);
255 
256 		instrumentation_end();
257 	}
258 	syscall_exit_to_user_mode(regs);
259 }
260 #endif /* !CONFIG_IA32_EMULATION */
261 
262 static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
263 {
264 	long nr = syscall_32_enter(regs);
265 	int res;
266 
267 	enter_from_user_mode_randomize_stack(regs);
268 
269 	instrumentation_begin();
270 	local_irq_enable();
271 	/* Fetch EBP from where the vDSO stashed it. */
272 	res = get_user(*(u32 *)&regs->bp,
273 		       (u32 __user __force *)(unsigned long)(u32)regs->sp);
274 
275 	if (res) {
276 		/* User code screwed up. */
277 		regs->ax = -EFAULT;
278 
279 		local_irq_disable();
280 		instrumentation_end();
281 		irqentry_exit_to_user_mode(regs);
282 		return false;
283 	}
284 
285 	if (likely(syscall_enter_from_user_mode_work(regs, &nr)))
286 		do_syscall_32_irqs_on(regs, nr);
287 
288 	instrumentation_end();
289 	syscall_exit_to_user_mode(regs);
290 	return true;
291 }
292 
293 /* Returns true to return using SYSEXIT/SYSRETL, or false to use IRET */
294 __visible noinstr bool do_fast_syscall_32(struct pt_regs *regs)
295 {
296 	/*
297 	 * Called using the internal vDSO SYSENTER/SYSCALL32 calling
298 	 * convention.  Adjust regs so it looks like we entered using int80.
299 	 */
300 	unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
301 					vdso32_image.sym_int80_landing_pad;
302 
303 	/*
304 	 * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
305 	 * so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
306 	 * Fix it up.
307 	 */
308 	regs->ip = landing_pad;
309 
310 	/* Invoke the syscall. If it failed, keep it simple: use IRET. */
311 	if (!__do_fast_syscall_32(regs))
312 		return false;
313 
314 	/*
315 	 * Check that the register state is valid for using SYSRETL/SYSEXIT
316 	 * to exit to userspace.  Otherwise use the slower but fully capable
317 	 * IRET exit path.
318 	 */
319 
320 	/* XEN PV guests always use the IRET path */
321 	if (cpu_feature_enabled(X86_FEATURE_XENPV))
322 		return false;
323 
324 	/* EIP must point to the VDSO landing pad */
325 	if (unlikely(regs->ip != landing_pad))
326 		return false;
327 
328 	/* CS and SS must match the values set in MSR_STAR */
329 	if (unlikely(regs->cs != __USER32_CS || regs->ss != __USER_DS))
330 		return false;
331 
332 	/* If the TF, RF, or VM flags are set, use IRET */
333 	if (unlikely(regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)))
334 		return false;
335 
336 	/* Use SYSRETL/SYSEXIT to exit to userspace */
337 	return true;
338 }
339 
340 /* Returns true to return using SYSEXIT/SYSRETL, or false to use IRET */
341 __visible noinstr bool do_SYSENTER_32(struct pt_regs *regs)
342 {
343 	/* SYSENTER loses RSP, but the vDSO saved it in RBP. */
344 	regs->sp = regs->bp;
345 
346 	/* SYSENTER clobbers EFLAGS.IF.  Assume it was set in usermode. */
347 	regs->flags |= X86_EFLAGS_IF;
348 
349 	return do_fast_syscall_32(regs);
350 }
351