2019-05-29 07:12:31 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-07-03 12:44:23 -07:00
|
|
|
/*
|
|
|
|
* common.c - C code for kernel entry and exit
|
|
|
|
* Copyright (c) 2015 Andrew Lutomirski
|
|
|
|
*
|
|
|
|
* Based on asm and ptrace code by many authors. The code here originated
|
|
|
|
* in ptrace.c and signal.c.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/sched.h>
|
2017-02-08 10:51:37 -07:00
|
|
|
#include <linux/sched/task_stack.h>
|
2020-07-22 15:00:04 -07:00
|
|
|
#include <linux/entry-common.h>
|
2015-07-03 12:44:23 -07:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/smp.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/export.h>
|
2018-01-29 18:02:59 -07:00
|
|
|
#include <linux/nospec.h>
|
2017-06-14 18:12:01 -07:00
|
|
|
#include <linux/syscalls.h>
|
2019-04-03 09:41:52 -07:00
|
|
|
#include <linux/uaccess.h>
|
2023-06-23 04:14:09 -07:00
|
|
|
#include <linux/init.h>
|
2015-07-03 12:44:23 -07:00
|
|
|
|
2020-05-21 13:05:26 -07:00
|
|
|
#ifdef CONFIG_XEN_PV
|
|
|
|
#include <xen/xen-ops.h>
|
|
|
|
#include <xen/events.h>
|
|
|
|
#endif
|
|
|
|
|
2023-12-04 01:31:40 -07:00
|
|
|
#include <asm/apic.h>
|
2015-07-03 12:44:23 -07:00
|
|
|
#include <asm/desc.h>
|
|
|
|
#include <asm/traps.h>
|
2015-10-05 17:48:10 -07:00
|
|
|
#include <asm/vdso.h>
|
2016-01-26 14:12:04 -07:00
|
|
|
#include <asm/cpufeature.h>
|
2019-04-03 09:41:52 -07:00
|
|
|
#include <asm/fpu/api.h>
|
2019-02-18 15:42:51 -07:00
|
|
|
#include <asm/nospec-branch.h>
|
2019-11-11 15:03:23 -07:00
|
|
|
#include <asm/io_bitmap.h>
|
2020-01-23 08:27:54 -07:00
|
|
|
#include <asm/syscall.h>
|
2020-05-21 13:05:26 -07:00
|
|
|
#include <asm/irq_stack.h>
|
2015-07-03 12:44:23 -07:00
|
|
|
|
2020-07-22 15:00:01 -07:00
|
|
|
#ifdef CONFIG_X86_64
|
2021-05-18 12:13:03 -07:00
|
|
|
|
|
|
|
static __always_inline bool do_syscall_x64(struct pt_regs *regs, int nr)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Convert negative numbers to very high and thus out of range
|
|
|
|
* numbers for comparisons.
|
|
|
|
*/
|
|
|
|
unsigned int unr = nr;
|
|
|
|
|
|
|
|
if (likely(unr < NR_syscalls)) {
|
|
|
|
unr = array_index_nospec(unr, NR_syscalls);
|
2024-04-03 16:36:44 -07:00
|
|
|
regs->ax = x64_sys_call(regs, unr);
|
2021-05-18 12:13:03 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Adjust the starting offset of the table, and convert numbers
|
|
|
|
* < __X32_SYSCALL_BIT to very high and thus out of range
|
|
|
|
* numbers for comparisons.
|
|
|
|
*/
|
|
|
|
unsigned int xnr = nr - __X32_SYSCALL_BIT;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_X86_X32_ABI) && likely(xnr < X32_NR_syscalls)) {
|
|
|
|
xnr = array_index_nospec(xnr, X32_NR_syscalls);
|
2024-04-03 16:36:44 -07:00
|
|
|
regs->ax = x32_sys_call(regs, xnr);
|
2021-05-18 12:13:03 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-11 15:43:49 -07:00
|
|
|
/* Returns true to return using SYSRET, or false to use IRET */
|
|
|
|
__visible noinstr bool do_syscall_64(struct pt_regs *regs, int nr)
|
2020-07-22 15:00:01 -07:00
|
|
|
{
|
2021-04-01 16:23:45 -07:00
|
|
|
add_random_kstack_offset();
|
2020-07-22 15:00:04 -07:00
|
|
|
nr = syscall_enter_from_user_mode(regs, nr);
|
2020-07-22 15:00:01 -07:00
|
|
|
|
|
|
|
instrumentation_begin();
|
2021-05-18 12:13:03 -07:00
|
|
|
|
|
|
|
if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr) && nr != -1) {
|
|
|
|
/* Invalid system call, but still a system call. */
|
2021-05-18 12:13:02 -07:00
|
|
|
regs->ax = __x64_sys_ni_syscall(regs);
|
2016-01-28 16:11:28 -07:00
|
|
|
}
|
2021-05-18 12:13:03 -07:00
|
|
|
|
2020-03-10 06:46:27 -07:00
|
|
|
instrumentation_end();
|
2020-07-22 15:00:05 -07:00
|
|
|
syscall_exit_to_user_mode(regs);
|
2023-10-11 15:43:49 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that the register state is valid for using SYSRET to exit
|
|
|
|
* to userspace. Otherwise use the slower but fully capable IRET
|
|
|
|
* exit path.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* XEN PV guests always use the IRET path */
|
|
|
|
if (cpu_feature_enabled(X86_FEATURE_XENPV))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* SYSRET requires RCX == RIP and R11 == EFLAGS */
|
|
|
|
if (unlikely(regs->cx != regs->ip || regs->r11 != regs->flags))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* CS and SS must match the values set in MSR_STAR */
|
|
|
|
if (unlikely(regs->cs != __USER_CS || regs->ss != __USER_DS))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
|
|
|
|
* in kernel space. This essentially lets the user take over
|
|
|
|
* the kernel, since userspace controls RSP.
|
|
|
|
*
|
2023-10-11 15:43:50 -07:00
|
|
|
* TASK_SIZE_MAX covers all user-accessible addresses other than
|
|
|
|
* the deprecated vsyscall page.
|
2023-10-11 15:43:49 -07:00
|
|
|
*/
|
2023-10-11 15:43:50 -07:00
|
|
|
if (unlikely(regs->ip >= TASK_SIZE_MAX))
|
2023-10-11 15:43:49 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SYSRET cannot restore RF. It can restore TF, but unlike IRET,
|
|
|
|
* restoring TF results in a trap from userspace immediately after
|
|
|
|
* SYSRET.
|
|
|
|
*/
|
|
|
|
if (unlikely(regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Use SYSRET to exit to userspace */
|
|
|
|
return true;
|
2016-01-28 16:11:28 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-05 17:48:08 -07:00
|
|
|
#if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
|
2021-05-18 12:13:03 -07:00
|
|
|
static __always_inline int syscall_32_enter(struct pt_regs *regs)
|
2020-07-22 15:00:01 -07:00
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_IA32_EMULATION))
|
|
|
|
current_thread_info()->status |= TS_COMPAT;
|
2020-09-01 16:50:54 -07:00
|
|
|
|
2021-05-18 12:13:03 -07:00
|
|
|
return (int)regs->orig_ax;
|
2020-07-22 15:00:01 -07:00
|
|
|
}
|
|
|
|
|
2023-06-23 04:14:04 -07:00
|
|
|
#ifdef CONFIG_IA32_EMULATION
|
2023-06-23 04:14:09 -07:00
|
|
|
bool __ia32_enabled __ro_after_init = !IS_ENABLED(CONFIG_IA32_EMULATION_DEFAULT_DISABLED);
|
|
|
|
|
|
|
|
static int ia32_emulation_override_cmdline(char *arg)
|
|
|
|
{
|
|
|
|
return kstrtobool(arg, &__ia32_enabled);
|
|
|
|
}
|
|
|
|
early_param("ia32_emulation", ia32_emulation_override_cmdline);
|
2023-06-23 04:14:04 -07:00
|
|
|
#endif
|
|
|
|
|
2015-10-05 17:48:08 -07:00
|
|
|
/*
|
2023-07-25 04:08:50 -07:00
|
|
|
* Invoke a 32-bit syscall. Called with IRQs on in CT_STATE_KERNEL.
|
2015-10-05 17:48:08 -07:00
|
|
|
*/
|
2021-05-18 12:13:03 -07:00
|
|
|
static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs, int nr)
|
2015-10-05 17:48:08 -07:00
|
|
|
{
|
2021-05-18 12:13:03 -07:00
|
|
|
/*
|
|
|
|
* Convert negative numbers to very high and thus out of range
|
|
|
|
* numbers for comparisons.
|
|
|
|
*/
|
|
|
|
unsigned int unr = nr;
|
|
|
|
|
|
|
|
if (likely(unr < IA32_NR_syscalls)) {
|
|
|
|
unr = array_index_nospec(unr, IA32_NR_syscalls);
|
2024-04-03 16:36:44 -07:00
|
|
|
regs->ax = ia32_sys_call(regs, unr);
|
2021-05-18 12:13:03 -07:00
|
|
|
} else if (nr != -1) {
|
2021-05-18 12:13:02 -07:00
|
|
|
regs->ax = __ia32_sys_ni_syscall(regs);
|
2015-10-05 17:48:08 -07:00
|
|
|
}
|
|
|
|
}
|
2015-10-05 17:48:10 -07:00
|
|
|
|
2023-12-04 01:31:39 -07:00
|
|
|
#ifdef CONFIG_IA32_EMULATION
|
2023-12-04 01:31:40 -07:00
|
|
|
static __always_inline bool int80_is_external(void)
|
|
|
|
{
|
|
|
|
const unsigned int offs = (0x80 / 32) * 0x10;
|
|
|
|
const u32 bit = BIT(0x80 % 32);
|
|
|
|
|
|
|
|
/* The local APIC on XENPV guests is fake */
|
|
|
|
if (cpu_feature_enabled(X86_FEATURE_XENPV))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If vector 0x80 is set in the APIC ISR then this is an external
|
|
|
|
* interrupt. Either from broken hardware or injected by a VMM.
|
|
|
|
*
|
|
|
|
* Note: In guest mode this is only valid for secure guests where
|
|
|
|
* the secure module fully controls the vAPIC exposed to the guest.
|
|
|
|
*/
|
|
|
|
return apic_read(APIC_ISR + offs) & bit;
|
|
|
|
}
|
|
|
|
|
2023-12-04 01:31:39 -07:00
|
|
|
/**
|
2024-03-11 08:56:58 -07:00
|
|
|
* do_int80_emulation - 32-bit legacy syscall C entry from asm
|
2023-12-04 01:31:39 -07:00
|
|
|
*
|
|
|
|
* This entry point can be used by 32-bit and 64-bit programs to perform
|
|
|
|
* 32-bit system calls. Instances of INT $0x80 can be found inline in
|
|
|
|
* various programs and libraries. It is also used by the vDSO's
|
|
|
|
* __kernel_vsyscall fallback for hardware that doesn't support a faster
|
|
|
|
* entry method. Restarted 32-bit system calls also fall back to INT
|
|
|
|
* $0x80 regardless of what instruction was originally used to do the
|
|
|
|
* system call.
|
|
|
|
*
|
|
|
|
* This is considered a slow path. It is not used by most libc
|
|
|
|
* implementations on modern hardware except during process startup.
|
|
|
|
*
|
|
|
|
* The arguments for the INT $0x80 based syscall are on stack in the
|
|
|
|
* pt_regs structure:
|
|
|
|
* eax: system call number
|
|
|
|
* ebx, ecx, edx, esi, edi, ebp: arg1 - arg 6
|
|
|
|
*/
|
2024-03-11 08:56:58 -07:00
|
|
|
__visible noinstr void do_int80_emulation(struct pt_regs *regs)
|
2023-12-04 01:31:39 -07:00
|
|
|
{
|
|
|
|
int nr;
|
|
|
|
|
2023-12-04 01:31:40 -07:00
|
|
|
/* Kernel does not use INT $0x80! */
|
|
|
|
if (unlikely(!user_mode(regs))) {
|
|
|
|
irqentry_enter(regs);
|
|
|
|
instrumentation_begin();
|
|
|
|
panic("Unexpected external interrupt 0x80\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Establish kernel context for instrumentation, including for
|
|
|
|
* int80_is_external() below which calls into the APIC driver.
|
|
|
|
* Identical for soft and external interrupts.
|
|
|
|
*/
|
2023-12-04 01:31:39 -07:00
|
|
|
enter_from_user_mode(regs);
|
|
|
|
|
|
|
|
instrumentation_begin();
|
|
|
|
add_random_kstack_offset();
|
|
|
|
|
2023-12-04 01:31:40 -07:00
|
|
|
/* Validate that this is a soft interrupt to the extent possible */
|
|
|
|
if (unlikely(int80_is_external()))
|
|
|
|
panic("Unexpected external interrupt 0x80\n");
|
|
|
|
|
2023-12-04 01:31:39 -07:00
|
|
|
/*
|
|
|
|
* The low level idtentry code pushed -1 into regs::orig_ax
|
|
|
|
* and regs::ax contains the syscall number.
|
|
|
|
*
|
|
|
|
* User tracing code (ptrace or signal handlers) might assume
|
|
|
|
* that the regs::orig_ax contains a 32-bit number on invoking
|
|
|
|
* a 32-bit syscall.
|
|
|
|
*
|
|
|
|
* Establish the syscall convention by saving the 32bit truncated
|
|
|
|
* syscall number in regs::orig_ax and by invalidating regs::ax.
|
|
|
|
*/
|
|
|
|
regs->orig_ax = regs->ax & GENMASK(31, 0);
|
|
|
|
regs->ax = -ENOSYS;
|
|
|
|
|
|
|
|
nr = syscall_32_enter(regs);
|
|
|
|
|
|
|
|
local_irq_enable();
|
|
|
|
nr = syscall_enter_from_user_mode_work(regs, nr);
|
|
|
|
do_syscall_32_irqs_on(regs, nr);
|
|
|
|
|
|
|
|
instrumentation_end();
|
|
|
|
syscall_exit_to_user_mode(regs);
|
|
|
|
}
|
2024-04-17 10:47:31 -07:00
|
|
|
|
|
|
|
#ifdef CONFIG_X86_FRED
|
|
|
|
/*
|
|
|
|
* A FRED-specific INT80 handler is warranted for the follwing reasons:
|
|
|
|
*
|
|
|
|
* 1) As INT instructions and hardware interrupts are separate event
|
|
|
|
* types, FRED does not preclude the use of vector 0x80 for external
|
|
|
|
* interrupts. As a result, the FRED setup code does not reserve
|
|
|
|
* vector 0x80 and calling int80_is_external() is not merely
|
|
|
|
* suboptimal but actively incorrect: it could cause a system call
|
|
|
|
* to be incorrectly ignored.
|
|
|
|
*
|
|
|
|
* 2) It is called only for handling vector 0x80 of event type
|
|
|
|
* EVENT_TYPE_SWINT and will never be called to handle any external
|
|
|
|
* interrupt (event type EVENT_TYPE_EXTINT).
|
|
|
|
*
|
|
|
|
* 3) FRED has separate entry flows depending on if the event came from
|
|
|
|
* user space or kernel space, and because the kernel does not use
|
|
|
|
* INT insns, the FRED kernel entry handler fred_entry_from_kernel()
|
|
|
|
* falls through to fred_bad_type() if the event type is
|
|
|
|
* EVENT_TYPE_SWINT, i.e., INT insns. So if the kernel is handling
|
|
|
|
* an INT insn, it can only be from a user level.
|
|
|
|
*
|
|
|
|
* 4) int80_emulation() does a CLEAR_BRANCH_HISTORY. While FRED will
|
|
|
|
* likely take a different approach if it is ever needed: it
|
|
|
|
* probably belongs in either fred_intx()/ fred_other() or
|
|
|
|
* asm_fred_entrypoint_user(), depending on if this ought to be done
|
|
|
|
* for all entries from userspace or only system
|
|
|
|
* calls.
|
|
|
|
*
|
|
|
|
* 5) INT $0x80 is the fast path for 32-bit system calls under FRED.
|
|
|
|
*/
|
|
|
|
DEFINE_FREDENTRY_RAW(int80_emulation)
|
|
|
|
{
|
|
|
|
int nr;
|
|
|
|
|
|
|
|
enter_from_user_mode(regs);
|
|
|
|
|
|
|
|
instrumentation_begin();
|
|
|
|
add_random_kstack_offset();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FRED pushed 0 into regs::orig_ax and regs::ax contains the
|
|
|
|
* syscall number.
|
|
|
|
*
|
|
|
|
* User tracing code (ptrace or signal handlers) might assume
|
|
|
|
* that the regs::orig_ax contains a 32-bit number on invoking
|
|
|
|
* a 32-bit syscall.
|
|
|
|
*
|
|
|
|
* Establish the syscall convention by saving the 32bit truncated
|
|
|
|
* syscall number in regs::orig_ax and by invalidating regs::ax.
|
|
|
|
*/
|
|
|
|
regs->orig_ax = regs->ax & GENMASK(31, 0);
|
|
|
|
regs->ax = -ENOSYS;
|
|
|
|
|
|
|
|
nr = syscall_32_enter(regs);
|
|
|
|
|
|
|
|
local_irq_enable();
|
|
|
|
nr = syscall_enter_from_user_mode_work(regs, nr);
|
|
|
|
do_syscall_32_irqs_on(regs, nr);
|
|
|
|
|
|
|
|
instrumentation_end();
|
|
|
|
syscall_exit_to_user_mode(regs);
|
|
|
|
}
|
|
|
|
#endif
|
2023-12-04 01:31:39 -07:00
|
|
|
#else /* CONFIG_IA32_EMULATION */
|
|
|
|
|
|
|
|
/* Handles int $0x80 on a 32bit kernel */
|
2020-03-10 06:46:27 -07:00
|
|
|
__visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
|
2015-10-05 17:48:17 -07:00
|
|
|
{
|
2021-05-18 12:13:03 -07:00
|
|
|
int nr = syscall_32_enter(regs);
|
2020-03-10 06:46:27 -07:00
|
|
|
|
2021-04-01 16:23:45 -07:00
|
|
|
add_random_kstack_offset();
|
2020-09-01 16:50:54 -07:00
|
|
|
/*
|
2021-05-18 12:13:03 -07:00
|
|
|
* Subtlety here: if ptrace pokes something larger than 2^31-1 into
|
|
|
|
* orig_ax, the int return value truncates it. This matches
|
|
|
|
* the semantics of syscall_get_nr().
|
2020-09-01 16:50:54 -07:00
|
|
|
*/
|
2021-05-18 12:13:03 -07:00
|
|
|
nr = syscall_enter_from_user_mode(regs, nr);
|
2021-01-06 07:36:20 -07:00
|
|
|
instrumentation_begin();
|
2020-09-01 16:50:54 -07:00
|
|
|
|
2020-07-22 15:00:01 -07:00
|
|
|
do_syscall_32_irqs_on(regs, nr);
|
2021-01-06 07:36:20 -07:00
|
|
|
|
|
|
|
instrumentation_end();
|
2020-07-22 15:00:05 -07:00
|
|
|
syscall_exit_to_user_mode(regs);
|
2020-03-10 06:46:27 -07:00
|
|
|
}
|
2023-12-04 01:31:39 -07:00
|
|
|
#endif /* !CONFIG_IA32_EMULATION */
|
2020-03-10 06:46:27 -07:00
|
|
|
|
2020-07-22 15:00:01 -07:00
|
|
|
static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
|
2020-03-10 06:46:27 -07:00
|
|
|
{
|
2021-05-18 12:13:03 -07:00
|
|
|
int nr = syscall_32_enter(regs);
|
2020-03-10 06:46:27 -07:00
|
|
|
int res;
|
|
|
|
|
2021-04-01 16:23:45 -07:00
|
|
|
add_random_kstack_offset();
|
2020-09-01 16:50:54 -07:00
|
|
|
/*
|
|
|
|
* This cannot use syscall_enter_from_user_mode() as it has to
|
|
|
|
* fetch EBP before invoking any of the syscall entry work
|
|
|
|
* functions.
|
|
|
|
*/
|
|
|
|
syscall_enter_from_user_mode_prepare(regs);
|
|
|
|
|
2020-07-22 15:00:01 -07:00
|
|
|
instrumentation_begin();
|
2020-03-10 06:46:27 -07:00
|
|
|
/* Fetch EBP from where the vDSO stashed it. */
|
|
|
|
if (IS_ENABLED(CONFIG_X86_64)) {
|
|
|
|
/*
|
|
|
|
* Micro-optimization: the pointer we're following is
|
|
|
|
* explicitly 32 bits, so it can't be out of range.
|
|
|
|
*/
|
|
|
|
res = __get_user(*(u32 *)®s->bp,
|
|
|
|
(u32 __user __force *)(unsigned long)(u32)regs->sp);
|
|
|
|
} else {
|
|
|
|
res = get_user(*(u32 *)®s->bp,
|
|
|
|
(u32 __user __force *)(unsigned long)(u32)regs->sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
/* User code screwed up. */
|
|
|
|
regs->ax = -EFAULT;
|
2021-01-06 07:36:20 -07:00
|
|
|
|
2021-03-04 12:05:54 -07:00
|
|
|
local_irq_disable();
|
2021-06-21 04:12:34 -07:00
|
|
|
instrumentation_end();
|
2021-03-04 12:05:54 -07:00
|
|
|
irqentry_exit_to_user_mode(regs);
|
2020-03-10 06:46:27 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-18 12:13:03 -07:00
|
|
|
nr = syscall_enter_from_user_mode_work(regs, nr);
|
2020-09-01 16:50:54 -07:00
|
|
|
|
2020-03-10 06:46:27 -07:00
|
|
|
/* Now this is just like a normal syscall. */
|
2020-07-22 15:00:01 -07:00
|
|
|
do_syscall_32_irqs_on(regs, nr);
|
2021-01-06 07:36:20 -07:00
|
|
|
|
|
|
|
instrumentation_end();
|
2020-07-22 15:00:05 -07:00
|
|
|
syscall_exit_to_user_mode(regs);
|
2020-03-10 06:46:27 -07:00
|
|
|
return true;
|
2015-10-05 17:48:17 -07:00
|
|
|
}
|
|
|
|
|
2023-07-21 09:10:15 -07:00
|
|
|
/* Returns true to return using SYSEXIT/SYSRETL, or false to use IRET */
|
|
|
|
__visible noinstr bool do_fast_syscall_32(struct pt_regs *regs)
|
2015-10-05 17:48:10 -07:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Called using the internal vDSO SYSENTER/SYSCALL32 calling
|
|
|
|
* convention. Adjust regs so it looks like we entered using int80.
|
|
|
|
*/
|
|
|
|
unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
|
2020-03-10 06:46:27 -07:00
|
|
|
vdso_image_32.sym_int80_landing_pad;
|
2015-10-05 17:48:10 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
|
|
|
|
* so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
|
|
|
|
* Fix it up.
|
|
|
|
*/
|
|
|
|
regs->ip = landing_pad;
|
|
|
|
|
2020-07-22 15:00:01 -07:00
|
|
|
/* Invoke the syscall. If it failed, keep it simple: use IRET. */
|
|
|
|
if (!__do_fast_syscall_32(regs))
|
2023-07-21 09:10:15 -07:00
|
|
|
return false;
|
2015-10-05 17:48:12 -07:00
|
|
|
|
|
|
|
/*
|
2023-10-11 15:43:51 -07:00
|
|
|
* Check that the register state is valid for using SYSRETL/SYSEXIT
|
|
|
|
* to exit to userspace. Otherwise use the slower but fully capable
|
|
|
|
* IRET exit path.
|
2015-10-05 17:48:15 -07:00
|
|
|
*/
|
2023-10-11 15:43:51 -07:00
|
|
|
|
|
|
|
/* XEN PV guests always use the IRET path */
|
|
|
|
if (cpu_feature_enabled(X86_FEATURE_XENPV))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* EIP must point to the VDSO landing pad */
|
|
|
|
if (unlikely(regs->ip != landing_pad))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* CS and SS must match the values set in MSR_STAR */
|
|
|
|
if (unlikely(regs->cs != __USER32_CS || regs->ss != __USER_DS))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* If the TF, RF, or VM flags are set, use IRET */
|
|
|
|
if (unlikely(regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Use SYSRETL/SYSEXIT to exit to userspace */
|
|
|
|
return true;
|
2015-10-05 17:48:10 -07:00
|
|
|
}
|
2020-06-26 10:21:12 -07:00
|
|
|
|
2023-07-21 09:10:15 -07:00
|
|
|
/* Returns true to return using SYSEXIT/SYSRETL, or false to use IRET */
|
|
|
|
__visible noinstr bool do_SYSENTER_32(struct pt_regs *regs)
|
2020-06-26 10:21:12 -07:00
|
|
|
{
|
|
|
|
/* SYSENTER loses RSP, but the vDSO saved it in RBP. */
|
|
|
|
regs->sp = regs->bp;
|
|
|
|
|
|
|
|
/* SYSENTER clobbers EFLAGS.IF. Assume it was set in usermode. */
|
|
|
|
regs->flags |= X86_EFLAGS_IF;
|
|
|
|
|
|
|
|
return do_fast_syscall_32(regs);
|
|
|
|
}
|
2015-10-05 17:48:08 -07:00
|
|
|
#endif
|
2020-03-13 12:51:32 -07:00
|
|
|
|
|
|
|
SYSCALL_DEFINE0(ni_syscall)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2020-03-26 08:28:52 -07:00
|
|
|
|
2020-05-21 13:05:26 -07:00
|
|
|
#ifdef CONFIG_XEN_PV
|
|
|
|
#ifndef CONFIG_PREEMPTION
|
|
|
|
/*
|
|
|
|
* Some hypercalls issued by the toolstack can take many 10s of
|
|
|
|
* seconds. Allow tasks running hypercalls via the privcmd driver to
|
|
|
|
* be voluntarily preempted even if full kernel preemption is
|
|
|
|
* disabled.
|
|
|
|
*
|
|
|
|
* Such preemptible hypercalls are bracketed by
|
|
|
|
* xen_preemptible_hcall_begin() and xen_preemptible_hcall_end()
|
|
|
|
* calls.
|
|
|
|
*/
|
|
|
|
DEFINE_PER_CPU(bool, xen_in_preemptible_hcall);
|
|
|
|
EXPORT_SYMBOL_GPL(xen_in_preemptible_hcall);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In case of scheduling the flag must be cleared and restored after
|
|
|
|
* returning from schedule as the task might move to a different CPU.
|
|
|
|
*/
|
|
|
|
static __always_inline bool get_and_clear_inhcall(void)
|
|
|
|
{
|
|
|
|
bool inhcall = __this_cpu_read(xen_in_preemptible_hcall);
|
|
|
|
|
|
|
|
__this_cpu_write(xen_in_preemptible_hcall, false);
|
|
|
|
return inhcall;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline void restore_inhcall(bool inhcall)
|
|
|
|
{
|
|
|
|
__this_cpu_write(xen_in_preemptible_hcall, inhcall);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static __always_inline bool get_and_clear_inhcall(void) { return false; }
|
|
|
|
static __always_inline void restore_inhcall(bool inhcall) { }
|
|
|
|
#endif
|
|
|
|
|
2021-02-09 16:40:49 -07:00
|
|
|
static void __xen_pv_evtchn_do_upcall(struct pt_regs *regs)
|
2020-05-21 13:05:26 -07:00
|
|
|
{
|
2021-02-09 16:40:49 -07:00
|
|
|
struct pt_regs *old_regs = set_irq_regs(regs);
|
|
|
|
|
2020-05-21 13:05:26 -07:00
|
|
|
inc_irq_stat(irq_hv_callback_count);
|
|
|
|
|
2023-08-24 08:34:21 -07:00
|
|
|
xen_evtchn_do_upcall();
|
2020-05-21 13:05:26 -07:00
|
|
|
|
2021-02-09 16:40:49 -07:00
|
|
|
set_irq_regs(old_regs);
|
2020-05-21 13:05:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
__visible noinstr void xen_pv_evtchn_do_upcall(struct pt_regs *regs)
|
|
|
|
{
|
2021-02-09 16:40:49 -07:00
|
|
|
irqentry_state_t state = irqentry_enter(regs);
|
2020-07-03 10:02:58 -07:00
|
|
|
bool inhcall;
|
2020-05-21 13:05:26 -07:00
|
|
|
|
2021-06-21 04:12:35 -07:00
|
|
|
instrumentation_begin();
|
2021-02-09 16:40:49 -07:00
|
|
|
run_sysvec_on_irqstack_cond(__xen_pv_evtchn_do_upcall, regs);
|
2020-05-21 13:05:26 -07:00
|
|
|
|
|
|
|
inhcall = get_and_clear_inhcall();
|
2020-07-03 10:02:58 -07:00
|
|
|
if (inhcall && !WARN_ON_ONCE(state.exit_rcu)) {
|
2020-07-22 15:00:07 -07:00
|
|
|
irqentry_exit_cond_resched();
|
2020-05-21 13:05:26 -07:00
|
|
|
instrumentation_end();
|
|
|
|
restore_inhcall(inhcall);
|
|
|
|
} else {
|
2021-06-21 04:12:35 -07:00
|
|
|
instrumentation_end();
|
2020-07-22 15:00:08 -07:00
|
|
|
irqentry_exit(regs, state);
|
2020-05-21 13:05:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_XEN_PV */
|