Skip to content

Commit 0701c9e

Browse files
Peter ZijlstraThomas Gleixner
authored andcommitted
x86/kvm/vmx: Move IRQ/NMI dispatch from KVM into x86 core
Move the VMX interrupt dispatch magic into the x86 core code. This isolates KVM from the FRED/IDT decisions and reduces the amount of EXPORT_SYMBOL_FOR_KVM(). Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Tested-by: "Verma, Vishal L" <vishal.l.verma@intel.com> Tested-by: Zhao Liu <zhao1.liu@intel.com> Tested-by: Zhao Liu <zhao1.liu@intel.com> Tested-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Binbin Wu <binbin.wu@linxu.intel.com> Acked-by: Sean Christopherson <seanjc@google.com> Link: https://patch.msgid.link/20260508091829.GO3126523@noisy.programming.kicks-ass.net
1 parent b088fe3 commit 0701c9e

12 files changed

Lines changed: 119 additions & 68 deletions

File tree

arch/x86/entry/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CFLAGS_REMOVE_syscall_64.o = $(CC_FLAGS_FTRACE)
1313
CFLAGS_syscall_32.o += -fno-stack-protector
1414
CFLAGS_syscall_64.o += -fno-stack-protector
1515

16-
obj-y := entry.o entry_$(BITS).o syscall_$(BITS).o
16+
obj-y := entry.o entry_$(BITS).o syscall_$(BITS).o common.o
1717

1818
obj-y += vdso/
1919
obj-y += vsyscall/

arch/x86/entry/common.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#include <linux/entry-common.h>
4+
#include <linux/kvm_types.h>
5+
#include <asm/fred.h>
6+
#include <asm/desc.h>
7+
8+
#if IS_ENABLED(CONFIG_KVM_INTEL)
9+
/*
10+
* On VMX, NMIs and IRQs (as configured by KVM) are acknowledged by hardware as
11+
* part of the VM-Exit, i.e. the event itself is consumed as part the VM-Exit.
12+
* x86_entry_from_kvm() is invoked by KVM to effectively forward NMIs and IRQs
13+
* to the kernel for servicing. On SVM, a.k.a. AMD, the NMI/IRQ VM-Exit is
14+
* purely a signal that an NMI/IRQ is pending, i.e. the event that triggered
15+
* the VM-Exit is held pending until it's unblocked in the host.
16+
*/
17+
noinstr void x86_entry_from_kvm(unsigned int event_type, unsigned int vector)
18+
{
19+
if (event_type == EVENT_TYPE_EXTINT) {
20+
#ifdef CONFIG_X86_64
21+
/*
22+
* Use FRED dispatch, even when running IDT. The dispatch
23+
* tables are kept in sync between FRED and IDT, and the FRED
24+
* dispatch works well with CFI.
25+
*/
26+
fred_entry_from_kvm(event_type, vector);
27+
#else
28+
idt_entry_from_kvm(vector);
29+
#endif
30+
return;
31+
}
32+
33+
WARN_ON_ONCE(event_type != EVENT_TYPE_NMI);
34+
35+
#ifdef CONFIG_X86_64
36+
if (cpu_feature_enabled(X86_FEATURE_FRED))
37+
return fred_entry_from_kvm(event_type, vector);
38+
#endif
39+
40+
/*
41+
* Notably, we must use IDT dispatch for NMI when running in IDT mode.
42+
* The FRED NMI context is significantly different and will not work
43+
* right (specifically FRED fixed the NMI recursion issue).
44+
*/
45+
idt_entry_from_kvm(vector);
46+
}
47+
EXPORT_SYMBOL_FOR_KVM(x86_entry_from_kvm);
48+
#endif

arch/x86/entry/entry.S

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,49 @@ THUNK warn_thunk_thunk, __warn_thunk
7575
#if defined(CONFIG_STACKPROTECTOR) && defined(CONFIG_SMP)
7676
EXPORT_SYMBOL(__ref_stack_chk_guard);
7777
#endif
78+
79+
#if IS_ENABLED(CONFIG_KVM_INTEL)
80+
.macro IDT_DO_EVENT_IRQOFF call_insn call_target
81+
/*
82+
* Unconditionally create a stack frame, getting the correct RSP on the
83+
* stack (for x86-64) would take two instructions anyways, and RBP can
84+
* be used to restore RSP to make objtool happy (see below).
85+
*/
86+
push %_ASM_BP
87+
mov %_ASM_SP, %_ASM_BP
88+
89+
#ifdef CONFIG_X86_64
90+
/*
91+
* Align RSP to a 16-byte boundary (to emulate CPU behavior) before
92+
* creating the synthetic interrupt stack frame for the IRQ/NMI.
93+
*/
94+
and $-16, %rsp
95+
push $__KERNEL_DS
96+
push %rbp
97+
#endif
98+
pushf
99+
push $__KERNEL_CS
100+
\call_insn \call_target
101+
102+
/*
103+
* "Restore" RSP from RBP, even though IRET has already unwound RSP to
104+
* the correct value. objtool doesn't know the callee will IRET and,
105+
* without the explicit restore, thinks the stack is getting walloped.
106+
* Using an unwind hint is problematic due to x86-64's dynamic alignment.
107+
*/
108+
leave
109+
RET
110+
.endm
111+
112+
.pushsection .text, "ax"
113+
SYM_FUNC_START(idt_do_interrupt_irqoff)
114+
IDT_DO_EVENT_IRQOFF CALL_NOSPEC _ASM_ARG1
115+
SYM_FUNC_END(idt_do_interrupt_irqoff)
116+
.popsection
117+
118+
.pushsection .noinstr.text, "ax"
119+
SYM_FUNC_START(idt_do_nmi_irqoff)
120+
IDT_DO_EVENT_IRQOFF call asm_exc_nmi_kvm_vmx
121+
SYM_FUNC_END(idt_do_nmi_irqoff)
122+
.popsection
123+
#endif

arch/x86/entry/entry_64_fred.S

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,4 @@ SYM_FUNC_START(asm_fred_entry_from_kvm)
147147
RET
148148

149149
SYM_FUNC_END(asm_fred_entry_from_kvm)
150-
EXPORT_SYMBOL_FOR_KVM(asm_fred_entry_from_kvm);
151150
#endif

arch/x86/include/asm/desc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ extern void idt_setup_traps(void);
438438
extern void idt_setup_apic_and_irq_gates(void);
439439
extern bool idt_is_f00f_address(unsigned long address);
440440

441+
extern void idt_do_interrupt_irqoff(unsigned long address);
442+
extern void idt_do_nmi_irqoff(void);
443+
extern void idt_entry_from_kvm(unsigned int vector);
444+
441445
#ifdef CONFIG_X86_64
442446
extern void idt_setup_early_pf(void);
443447
#else

arch/x86/include/asm/desc_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ struct gate_struct {
145145
typedef struct gate_struct gate_desc;
146146

147147
#ifndef _SETUP
148-
static inline unsigned long gate_offset(const gate_desc *g)
148+
static __always_inline unsigned long gate_offset(const gate_desc *g)
149149
{
150150
#ifdef CONFIG_X86_64
151151
return g->offset_low | ((unsigned long)g->offset_middle << 16) |

arch/x86/include/asm/entry-common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,6 @@ static __always_inline void arch_exit_to_user_mode(void)
9797
}
9898
#define arch_exit_to_user_mode arch_exit_to_user_mode
9999

100+
extern void x86_entry_from_kvm(unsigned int entry_type, unsigned int vector);
101+
100102
#endif

arch/x86/include/asm/fred.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ static __always_inline unsigned long fred_event_data(struct pt_regs *regs) { ret
110110
static inline void cpu_init_fred_exceptions(void) { }
111111
static inline void cpu_init_fred_rsps(void) { }
112112
static inline void fred_complete_exception_setup(void) { }
113-
static inline void fred_entry_from_kvm(unsigned int type, unsigned int vector) { }
114113
static inline void fred_sync_rsp0(unsigned long rsp0) { }
115114
static inline void fred_update_rsp0(void) { }
116115
#endif /* CONFIG_X86_FRED */

arch/x86/kernel/idt.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,21 @@ void __init idt_setup_early_pf(void)
268268
}
269269
#endif
270270

271+
#if IS_ENABLED(CONFIG_KVM_INTEL)
272+
noinstr void idt_entry_from_kvm(unsigned int vector)
273+
{
274+
if (vector == NMI_VECTOR)
275+
return idt_do_nmi_irqoff();
276+
277+
/*
278+
* Only the NMI path requires noinstr.
279+
*/
280+
instrumentation_begin();
281+
idt_do_interrupt_irqoff(gate_offset(idt_table + vector));
282+
instrumentation_end();
283+
}
284+
#endif
285+
271286
static void __init idt_map_in_cea(void)
272287
{
273288
/*

arch/x86/kernel/nmi.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,6 @@ DEFINE_IDTENTRY_RAW(exc_nmi_kvm_vmx)
614614
{
615615
exc_nmi(regs);
616616
}
617-
EXPORT_SYMBOL_FOR_KVM(asm_exc_nmi_kvm_vmx);
618617
#endif
619618

620619
#ifdef CONFIG_NMI_CHECK_CPU

0 commit comments

Comments
 (0)