123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
#include <linux/linkage.h>
#include <linux/sys.h>
#include <linux/cache.h>
#include <linux/syscalls.h>
#include <linux/entry-common.h>
#include <linux/nospec.h>
#include <linux/uaccess.h>
#include <asm/apic.h>
#include <asm/traps.h>
#include <asm/cpufeature.h>
#include <asm/syscall.h>
#ifdef CONFIG_IA32_EMULATION
#define __SYSCALL_WITH_COMPAT(nr, native, compat) __SYSCALL(nr, compat)
#else
#define __SYSCALL_WITH_COMPAT(nr, native, compat) __SYSCALL(nr, native)
#endif
#define __SYSCALL(nr, sym) extern long __ia32_##sym(const struct pt_regs *);
#define __SYSCALL_NORETURN(nr, sym) extern long __noreturn __ia32_##sym(const struct pt_regs *);
#include <asm/syscalls_32.h>
#undef __SYSCALL
#undef __SYSCALL_NORETURN
#define __SYSCALL_NORETURN __SYSCALL
#ifdef CONFIG_X86_32
#define __SYSCALL(nr, sym) __ia32_##sym,
const sys_call_ptr_t sys_call_table[] = {
#include <asm/syscalls_32.h>
};
#undef __SYSCALL
#endif
#define __SYSCALL(nr, sym) case nr: return __ia32_##sym(regs);
static noinline long ia32_sys_call(const struct pt_regs *regs, unsigned int nr)
{
switch (nr) {
#include <asm/syscalls_32.h>
default: return __ia32_sys_ni_syscall(regs);
}
}
static __always_inline long syscall_32_enter(struct pt_regs *regs)
{
if (IS_ENABLED(CONFIG_IA32_EMULATION))
current_thread_info()->status |= TS_COMPAT;
return (int)regs->orig_ax;
}
#ifdef CONFIG_IA32_EMULATION
bool __ia32_enabled __ro_after_init = !IS_ENABLED(CONFIG_IA32_EMULATION_DEFAULT_DISABLED);
static int __init ia32_emulation_override_cmdline(char *arg)
{
return kstrtobool(arg, &__ia32_enabled);
}
early_param("ia32_emulation", ia32_emulation_override_cmdline);
#endif
static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs, unsigned long nr)
{
if (likely(nr < IA32_NR_syscalls)) {
nr = array_index_nospec(nr, IA32_NR_syscalls);
regs->ax = ia32_sys_call(regs, (unsigned int)nr);
} else {
if (nr != -1UL)
regs->ax = -ENOSYS;
}
}
#ifdef CONFIG_IA32_EMULATION
static __always_inline bool int80_is_external(void)
{
const unsigned int offs = (0x80 / 32) * 0x10;
const u32 bit = BIT(0x80 % 32);
if (cpu_feature_enabled(X86_FEATURE_XENPV))
return false;
return apic_read(APIC_ISR + offs) & bit;
}
__visible noinstr void do_int80_emulation(struct pt_regs *regs)
{
long nr;
if (unlikely(!user_mode(regs))) {
irqentry_enter(regs);
instrumentation_begin();
panic("Unexpected external interrupt 0x80\n");
}
enter_from_user_mode_randomize_stack(regs);
instrumentation_begin();
if (unlikely(int80_is_external()))
panic("Unexpected external interrupt 0x80\n");
regs->orig_ax = regs->ax & GENMASK(31, 0);
regs->ax = -ENOSYS;
nr = syscall_32_enter(regs);
local_irq_enable();
if (likely(syscall_enter_from_user_mode_work(regs, &nr)))
do_syscall_32_irqs_on(regs, nr);
instrumentation_end();
syscall_exit_to_user_mode(regs);
}
#ifdef CONFIG_X86_FRED
DEFINE_FREDENTRY_RAW(int80_emulation)
{
long nr;
enter_from_user_mode_randomize_stack(regs);
instrumentation_begin();
regs->orig_ax = regs->ax & GENMASK(31, 0);
regs->ax = -ENOSYS;
nr = syscall_32_enter(regs);
local_irq_enable();
if (likely(syscall_enter_from_user_mode_work(regs, &nr)))
do_syscall_32_irqs_on(regs, nr);
instrumentation_end();
syscall_exit_to_user_mode(regs);
}
#endif /* CONFIG_X86_FRED */
#else /* CONFIG_IA32_EMULATION */
__visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
{
long nr = syscall_32_enter(regs);
if (likely(syscall_enter_from_user_mode_randomize_stack(regs, &nr))) {
instrumentation_begin();
do_syscall_32_irqs_on(regs, nr);
instrumentation_end();
}
syscall_exit_to_user_mode(regs);
}
#endif /* !CONFIG_IA32_EMULATION */
static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
{
long nr = syscall_32_enter(regs);
int res;
enter_from_user_mode_randomize_stack(regs);
instrumentation_begin();
local_irq_enable();
res = get_user(*(u32 *)®s->bp,
(u32 __user __force *)(unsigned long)(u32)regs->sp);
if (res) {
regs->ax = -EFAULT;
local_irq_disable();
instrumentation_end();
irqentry_exit_to_user_mode(regs);
return false;
}
if (likely(syscall_enter_from_user_mode_work(regs, &nr)))
do_syscall_32_irqs_on(regs, nr);
instrumentation_end();
syscall_exit_to_user_mode(regs);
return true;
}
__visible noinstr bool do_fast_syscall_32(struct pt_regs *regs)
{
unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
vdso32_image.sym_int80_landing_pad;
regs->ip = landing_pad;
if (!__do_fast_syscall_32(regs))
return false;
if (cpu_feature_enabled(X86_FEATURE_XENPV))
return false;
if (unlikely(regs->ip != landing_pad))
return false;
if (unlikely(regs->cs != __USER32_CS || regs->ss != __USER_DS))
return false;
if (unlikely(regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)))
return false;
return true;
}
__visible noinstr bool do_SYSENTER_32(struct pt_regs *regs)
{
regs->sp = regs->bp;
regs->flags |= X86_EFLAGS_IF;
return do_fast_syscall_32(regs);
}