x86/fred: Fixup fault on ERETU by jumping to fred_entrypoint_user
Subsystems (10)
PATCH v9 30/36 x86/fred: Fixup fault on ERETU by jumping to fred_entrypoint_user
If the stack frame contains an invalid user context (e.g. due to invalid SS, a non-canonical RIP, etc.) the ERETU instruction will trap (#SS or #GP).
From a Linux point of view, this really should be considered a user space failure, so use the standard fault fixup mechanism to intercept the fault, fix up the exception frame, and redirect execution to fred_entrypoint_user. The end result is that it appears just as if the hardware had taken the exception immediately after completing the transition to user space.
---
Changes since v8:
* Reflect the FRED spec 5.0 change that ERETS and ERETU add 8 to %rsp before popping the return context from the stack.
Changes since v6:
* Add a comment to explain why it is safe to write to the previous FRED stack frame. (Lai Jiangshan).
Changes since v5:
* Move the NMI bit from an invalid stack frame, which caused ERETU to fault, to the fault handler's stack frame, thus to unblock NMI ASAP if NMI is blocked (Lai Jiangshan).
--- arch/x86/entry/entry_64_fred.S | 5 +- arch/x86/include/asm/extable_fixup_types.h | 4 +- arch/x86/mm/extable.c | 79 ++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-)
arch/x86/entry/entry_64_fred.S +4 -1
@@ -3,6 +3,7 @@ * The actual FRED entry points. */ +#include <asm/asm.h> #include <asm/fred.h> #include "calling.h"
@@ -34,7 +35,9 @@ SYM_CODE_START_NOALIGN(fred_entrypoint_user) call fred_entry_from_user SYM_INNER_LABEL(fred_exit_user, SYM_L_GLOBAL) FRED_EXIT - ERETU +1: ERETU + + _ASM_EXTABLE_TYPE(1b, fred_entrypoint_user, EX_TYPE_ERETU) SYM_CODE_END(fred_entrypoint_user) .fill fred_entrypoint_kernel - ., 1, 0xcc
arch/x86/include/asm/extable_fixup_types.h +3 -1
@@ -64,6 +64,8 @@#define EX_TYPE_UCOPY_LEN4 (EX_TYPE_UCOPY_LEN | EX_DATA_IMM(4))#define EX_TYPE_UCOPY_LEN8 (EX_TYPE_UCOPY_LEN | EX_DATA_IMM(8)) -#define EX_TYPE_ZEROPAD 20/* longword load with zeropad on fault */ +#define EX_TYPE_ZEROPAD 20/* longword load with zeropad on fault */ + +#define EX_TYPE_ERETU 21#endif
arch/x86/mm/extable.c +79 -0
97 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v9 31/36 x86/traps: Export external_interrupt() for handling IRQ in IRQ induced VM exits
Export external_interrupt() for handling IRQ in IRQ induced VM exits.
--- arch/x86/kernel/traps.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kernel/traps.c +5 -0
@@ -1560,6 +1560,11 @@ int external_interrupt(struct pt_regs *regs) return 0; } +#if IS_ENABLED (CONFIG_KVM_INTEL ) +/* For KVM VMX to handle IRQs in IRQ induced VM exits. */ +EXPORT_SYMBOL_GPL (external_interrupt); +#endif +#endif /* CONFIG_X86_64 */ void __init trap_init (void )
Signature · 1 line
2.34.1
PATCH v9 32/36 x86/fred: Export fred_entrypoint_kernel() for handling NMI in NMI induced VM exits
In reply to Xin LiExport fred_entrypoint_kernel() for handling NMI in NMI induced VM exits.
--- arch/x86/entry/entry_64_fred.S | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/entry/entry_64_fred.S +2 -0
@@ -4,6 +4,7 @@ */ #include <asm/asm.h> +#include <asm/export.h> #include <asm/fred.h> #include "calling.h"
@@ -54,3 +55,4 @@ SYM_CODE_START_NOALIGN(fred_entrypoint_kernel) FRED_EXIT ERETS SYM_CODE_END(fred_entrypoint_kernel) +EXPORT_SYMBOL(fred_entrypoint_kernel)
Signature · 1 line
2.34.1
PATCH v9 33/36 KVM: VMX: Add VMX_DO_FRED_EVENT_IRQOFF for IRQ/NMI handling
In reply to Xin LiCompared to an IDT stack frame, a FRED stack frame has extra 16 bytes of information pushed at the regular stack top and 8 bytes of error code _always_ pushed at the regular stack bottom, add VMX_DO_FRED_EVENT_IRQOFF to generate FRED stack frames with event type and vector properly set. Thus, IRQ/NMI can be handled with the existing approach when FRED is enabled.
For IRQ handling, general purpose registers are pushed to the stack to form a pt_regs structure, which is then used to call external_interrupt(). As a result, IRQ handling no longer re-enters the noinstr code.
---
Changes since v8:
* Add a new macro VMX_DO_FRED_EVENT_IRQOFF for FRED instead of refactoring VMX_DO_EVENT_IRQOFF (Sean Christopherson).
* Do NOT use a trampoline, just LEA+PUSH the return RIP, PUSH the error code, and jump to the FRED kernel entry point for NMI or call external_interrupt() for IRQs (Sean Christopherson).
* Call external_interrupt() only when FRED is enabled, and convert the non-FRED handling to external_interrupt() after FRED lands (Sean Christopherson).
--- arch/x86/kvm/vmx/vmenter.S | 88 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 19 ++++++-- 2 files changed, 104 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmenter.S +88 -0
@@ -2,12 +2,14 @@ #include <linux/linkage.h> #include <asm/asm.h> #include <asm/bitsperlong.h> +#include <asm/fred.h> #include <asm/kvm_vcpu_regs.h> #include <asm/nospec-branch.h> #include <asm/percpu.h> #include <asm/segment.h> #include "kvm-asm-offsets.h" #include "run_flags.h" +#include "../../entry/calling.h" #define WORD_SIZE (BITS_PER_LONG / 8)
@@ -31,6 +33,80 @@ #define VCPU_R15 __VCPU_REGS_R15 * WORD_SIZE #endif +#ifdef CONFIG_X86_FRED +.macro VMX_DO_FRED_EVENT_IRQOFF branch_insn branch_target nmi=0 + /* + * Unconditionally create a stack frame, getting the correct RSP on the + * stack (for x86-64) would take two instructions anyways, and RBP can + * be used to restore RSP to make objtool happy (see below). + */ + push %_ASM_BP + mov %_ASM_SP, %_ASM_BP + + /* + * Don't check the FRED stack level, the call stack leading to this + * helper is effectively constant and shallow (relatively speaking). + * + * Emulate the FRED-defined redzone and stack alignment. + */ + sub $(FRED_CONFIG_REDZONE_AMOUNT << 6), %rsp + and $FRED_STACK_FRAME_RSP_MASK, %rsp + + /* + * A FRED stack frame has extra 16 bytes of information pushed at the + * regular stack top compared to an IDT stack frame. + */ + push $0 /* Reserved by FRED, must be 0 */ + push $0 /* FRED event data, 0 for NMI and external interrupts */ + + shl $32, %rdi /* FRED event type and vector */ + .if \nmi + bts $FRED_SSX_NMI_BIT, %rdi /* Set the NMI bit */ + .endif + bts $FRED_SSX_64_BIT_MODE_BIT, %rdi /* Set the 64-bit mode */ + or $__KERNEL_DS, %rdi + push %rdi + push %rbp + pushf + mov $__KERNEL_CS, %rax + push %rax + + /* + * Unlike the IDT event delivery, FRED _always_ pushes an error code + * after pushing the return RIP, thus the CALL instruction CANNOT be + * used here to push the return RIP, otherwise there is no chance to + * push an error code before invoking the IRQ/NMI handler. + * + * Use LEA to get the return RIP and push it, then push an error code. + */ + lea 1f(%rip), %rax + push %rax + push $0 /* FRED error code, 0 for NMI and external interrupts */ + + .if \nmi == 0 + PUSH_REGS + mov %rsp, %rdi + .endif + + \branch_insn \branch_target + + .if \nmi == 0 + POP_REGS + .endif + +1: + /* + * "Restore" RSP from RBP, even though IRET has already unwound RSP to + * the correct value. objtool doesn't know the callee will IRET and, + * without the explicit restore, thinks the stack is getting walloped. + * Using an unwind hint is problematic due to x86-64's dynamic alignment. + */ + mov %_ASM_BP, %_ASM_SP + pop %_ASM_BP + RET +.endm +#endif + .macro VMX_DO_EVENT_IRQOFF call_insn call_target /* * Unconditionally create a stack frame, getting the correct RSP on the
@@ -299,6 +375,12 @@ SYM_INNER_LABEL_ALIGN(vmx_vmexit, SYM_L_GLOBAL) SYM_FUNC_END(__vmx_vcpu_run) +#ifdef CONFIG_X86_FRED +SYM_FUNC_START(vmx_do_fred_nmi_irqoff) + VMX_DO_FRED_EVENT_IRQOFF jmp fred_entrypoint_kernel nmi=1 +SYM_FUNC_END(vmx_do_fred_nmi_irqoff) +#endif + SYM_FUNC_START(vmx_do_nmi_irqoff) VMX_DO_EVENT_IRQOFF call asm_exc_nmi_kvm_vmx SYM_FUNC_END(vmx_do_nmi_irqoff)
@@ -357,6 +439,12 @@ SYM_FUNC_START(vmread_error_trampoline) SYM_FUNC_END(vmread_error_trampoline) #endif +#ifdef CONFIG_X86_FRED +SYM_FUNC_START(vmx_do_fred_interrupt_irqoff) + VMX_DO_FRED_EVENT_IRQOFF call external_interrupt +SYM_FUNC_END(vmx_do_fred_interrupt_irqoff) +#endif + SYM_FUNC_START(vmx_do_interrupt_irqoff) VMX_DO_EVENT_IRQOFF CALL_NOSPEC _ASM_ARG1 SYM_FUNC_END(vmx_do_interrupt_irqoff)
arch/x86/kvm/vmx/vmx.c +16 -3
@@ -6890,6 +6890,14 @@ static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)memset (vmx->pi_desc.pir,0 ,sizeof (vmx->pi_desc.pir)); } +#ifdef CONFIG_X86_FRED +void vmx_do_fred_interrupt_irqoff (unsigned int vector); +void vmx_do_fred_nmi_irqoff (unsigned int vector); +#else +#define vmx_do_fred_interrupt_irqoff (x) BUG() +#define vmx_do_fred_nmi_irqoff (x) BUG() +#endif +void vmx_do_interrupt_irqoff (unsigned long entry);void vmx_do_nmi_irqoff (void );
@@ -6932,14 +6940,16 @@ static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu) {u32 intr_info =vmx_get_intr_info (vcpu);unsigned int vector = intr_info &INTR_INFO_VECTOR_MASK ; -gate_desc *desc = (gate_desc *)host_idt_base + vector;if (KVM_BUG (!is_external_intr (intr_info), vcpu->kvm,"unexpected VM-Exit interrupt info: 0x%x" , intr_info))return ;kvm_before_interrupt (vcpu,KVM_HANDLING_IRQ ); -vmx_do_interrupt_irqoff (gate_offset (desc)); +if (cpu_feature_enabled (X86_FEATURE_FRED )) +vmx_do_fred_interrupt_irqoff (vector);/* Event type is 0 */ +else +vmx_do_interrupt_irqoff (gate_offset ((gate_desc *)host_idt_base + vector));kvm_after_interrupt (vcpu); vcpu->arch.at_instruction_boundary = true;
@@ -7225,7 +7235,10 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,if ((u16 )vmx->exit_reason.basic ==EXIT_REASON_EXCEPTION_NMI &&is_nmi (vmx_get_intr_info (vcpu))) {kvm_before_interrupt (vcpu,KVM_HANDLING_NMI ); -vmx_do_nmi_irqoff (); +if (cpu_feature_enabled (X86_FEATURE_FRED )) +vmx_do_fred_nmi_irqoff ((EVENT_TYPE_NMI <<16 ) |NMI_VECTOR ); +else +vmx_do_nmi_irqoff ();kvm_after_interrupt (vcpu); }
Signature · 1 line
2.34.1
PATCH v9 34/36 x86/syscall: Split IDT syscall setup code into idt_syscall_init()
In reply to Xin LiSplit IDT syscall setup code into idt_syscall_init() to make it cleaner to add FRED syscall setup code.
--- arch/x86/kernel/cpu/common.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/common.c +10 -3
@@ -2027,10 +2027,8 @@ static void wrmsrl_cstar(unsigned long val)wrmsrl (MSR_CSTAR , val); } -/* May not be marked __init: used by software suspend */ -void syscall_init (void ) +static inline void idt_syscall_init (void ) { -wrmsr (MSR_STAR ,0 , (__USER32_CS <<16 ) | __KERNEL_CS);wrmsrl (MSR_LSTAR , (unsigned long )entry_SYSCALL_64);#ifdef CONFIG_IA32_EMULATION
@@ -2064,6 +2062,15 @@ void syscall_init(void) X86_EFLAGS_AC|X86_EFLAGS_ID); } +/* May not be marked __init: used by software suspend */ +void syscall_init (void ) +{ +/* The default user and kernel segments */ +wrmsr (MSR_STAR ,0 , (__USER32_CS <<16 ) | __KERNEL_CS); + +idt_syscall_init (); +} +#else /* CONFIG_X86_64 */ #ifdef CONFIG_STACKPROTECTOR
Signature · 1 line
2.34.1
PATCH v9 35/36 x86/fred: FRED initialization code
In reply to Xin LiFrom: "H. Peter Anvin (Intel)" <[email protected]>
The code to initialize FRED when it's available and _not_ disabled.
cpu_init_fred_exceptions() is the core function to initialize FRED, which
1. Sets up FRED entrypoints for events happening in ring 0 and 3.
2. Sets up a default stack for event handling.
3. Sets up dedicated event stacks for DB/NMI/MC/DF, equivalent to the IDT IST stacks.
4. Forces 32-bit system calls to use "int $0x80" only.
5. Enables FRED and invalidtes IDT.
When the FRED is used, cpu_init_exception_handling() initializes FRED through calling cpu_init_fred_exceptions(), otherwise it sets up TSS IST and loads IDT.
As FRED uses the ring 3 FRED entrypoint for SYSCALL and SYSENTER, it skips setting up SYSCALL/SYSENTER related MSRs, e.g., MSR_LSTAR.
---
Changes since v8:
* Move this patch after all required changes are in place (Thomas Gleixner).
Changes since v5:
* Add a comment for FRED stack level settings (Lai Jiangshan).
* Define #DB/NMI/#MC/#DF stack levels using macros.
--- arch/x86/include/asm/fred.h | 28 ++++++++++++++++ arch/x86/include/asm/traps.h | 4 ++- arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 28 +++++++++++++--- arch/x86/kernel/fred.c | 64 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/irqinit.c | 7 +++- arch/x86/kernel/traps.c | 11 ++++++- 7 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 arch/x86/kernel/fred.c
arch/x86/include/asm/fred.h +28 -0
@@ -68,6 +68,19 @@#define FRED_SSX_64_BIT_MODE_BIT 57#define FRED_SSX_64_BIT_MODE _BITUL(FRED_SSX_64_BIT_MODE_BIT) +/* #DB in the kernel would imply the use of a kernel debugger. */ +#define FRED_DB_STACK_LEVEL 1 +#define FRED_NMI_STACK_LEVEL 2 +#define FRED_MC_STACK_LEVEL 2 +/* +* #DF is the highest level because a #DF means "something went wrong +* *while delivering an exception*." The number of cases for which that +* can happen with FRED is drastically reduced and basically amounts to +* "the stack you pointed me to is broken." Thus, always change stacks +* on #DF, which means it should be at the highest level. +*/ +#define FRED_DF_STACK_LEVEL 3 +/* * FRED event delivery establishes a full supervisor context by * saving the essential information about an event to a FRED
@@ -122,8 +135,23 @@ DECLARE_FRED_HANDLER(fred_exc_double_fault); extern asmlinkage __visible void fred_entrypoint_user(void); extern asmlinkage __visible void fred_entrypoint_kernel(void); +void cpu_init_fred_exceptions (void ); +void fred_setup_apic (void ); +#endif /* __ASSEMBLY__ */ +#else +#ifndef __ASSEMBLY__ +static inline void cpu_init_fred_exceptions (void ) +{ +BUG (); +} + +static inline void fred_setup_apic (void ) +{ +BUG (); +} +#endif #endif /* CONFIG_X86_FRED */ #endif /* ASM_X86_FRED_H */
arch/x86/include/asm/traps.h +3 -1
@@ -49,6 +49,7 @@ void __noreturn handle_stack_overflow(struct pt_regs *regs,#ifdef CONFIG_X86_64 inline void set_sysvec_handler (unsigned int i,system_interrupt_handler func); +bool is_sysvec_used (unsigned int i);static inline void sysvec_setup_fred (unsigned int vector,system_interrupt_handler func) {
@@ -63,7 +64,8 @@ static inline void sysvec_setup_fred(unsigned int vector, system_interrupt_handl#define sysvec_install (vector, func) { \ sysvec_setup_fred(vector, func); \ - alloc_intr_gate(vector, asm_##func); \ + if (!cpu_feature_enabled(X86_FEATURE_FRED)) \ + alloc_intr_gate(vector, asm_##func); \ }int external_interrupt (struct pt_regs *regs);
arch/x86/kernel/Makefile +1 -0
@@ -48,6 +48,7 @@ obj-y += platform-quirks.o obj-y += process_$(BITS).o signal.o signal_$(BITS).o obj-y += traps.o idt.o irq.o irq_$(BITS).o dumpstack_$(BITS).o obj-y += time.o ioport.o dumpstack.o nmi.o +obj-$(CONFIG_X86_FRED) += fred.o obj-$(CONFIG_MODIFY_LDT_SYSCALL) += ldt.o obj-y += setup.o x86_init.o i8259.o irqinit.o obj-$(CONFIG_JUMP_LABEL) += jump_label.o
arch/x86/kernel/cpu/common.c +23 -5
@@ -62,6 +62,7 @@#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/cpu_device_id.h> +#include <asm/fred.h> #include <asm/uv/uv.h> #include <asm/set_memory.h> #include <asm/traps.h>
@@ -2062,13 +2063,24 @@ static inline void idt_syscall_init(void) X86_EFLAGS_AC|X86_EFLAGS_ID); } +static inline void fred_syscall_init (void ) +{ +/* Both sysexit and sysret cause #UD when FRED is enabled */ +wrmsrl_safe (MSR_IA32_SYSENTER_CS , (u64 )GDT_ENTRY_INVALID_SEG ); +wrmsrl_safe (MSR_IA32_SYSENTER_ESP ,0ULL ); +wrmsrl_safe (MSR_IA32_SYSENTER_EIP ,0ULL ); +} +/* May not be marked __init: used by software suspend */ void syscall_init (void ) {/* The default user and kernel segments */ wrmsr (MSR_STAR ,0 , (__USER32_CS <<16 ) | __KERNEL_CS); -idt_syscall_init (); +if (cpu_feature_enabled (X86_FEATURE_FRED )) +fred_syscall_init (); +else +idt_syscall_init (); }#else /* CONFIG_X86_64 */
@@ -2184,8 +2196,6 @@ void cpu_init_exception_handling(void)/* paranoid_entry() gets the CPU number from the GDT */ setup_getcpu (cpu); -/* IST vectors need TSS to be set up. */ -tss_setup_ist (tss);tss_setup_io_bitmap (tss);set_tss_desc (cpu, &get_cpu_entry_area (cpu)->tss.x86_tss);
@@ -2194,8 +2204,16 @@ void cpu_init_exception_handling(void)/* GHCB needs to be setup to handle #VC. */ setup_ghcb (); -/* Finally load the IDT */ -load_current_idt (); +if (cpu_feature_enabled (X86_FEATURE_FRED )) { +/* Set up FRED exception handling */ +cpu_init_fred_exceptions (); + }else { +/* IST vectors need TSS to be set up. */ +tss_setup_ist (tss); + +/* Finally load the IDT */ +load_current_idt (); + } }/*
arch/x86/kernel/fred.c new file +64 -0
64 lines not on this page · raw message
arch/x86/kernel/irqinit.c +6 -1
@@ -28,6 +28,7 @@#include <asm/setup.h> #include <asm/i8259.h> #include <asm/traps.h> +#include <asm/fred.h> #include <asm/prom.h> /*
@@ -96,7 +97,11 @@ void __init native_init_IRQ(void)/* Execute any quirks before the call gates are initialised: */ x86_init.irqs.pre_vector_init (); -idt_setup_apic_and_irq_gates (); +if (cpu_feature_enabled (X86_FEATURE_FRED )) +fred_setup_apic (); +else +idt_setup_apic_and_irq_gates (); +lapic_assign_system_vectors ();if (!acpi_ioapic && !of_ioapic &&nr_legacy_irqs ()) {
arch/x86/kernel/traps.c +10 -1
@@ -1542,6 +1542,12 @@ void set_sysvec_handler(unsigned int i, system_interrupt_handler func) system_interrupt_handlers[i] = func; } +bool is_sysvec_used (unsigned int i) +{ +BUG_ON (i >=NR_SYSTEM_VECTORS ); +return system_interrupt_handlers[i] != dispatch_table_spurious_interrupt; +} +int external_interrupt (struct pt_regs *regs) {unsigned int vector = regs->vector;
@@ -1577,7 +1583,10 @@ void __init trap_init(void)/* Initialize TSS before setting up traps so ISTs work */ cpu_init_exception_handling (); +/* Setup traps as cpu_init() might #GP */ -idt_setup_traps (); +if (!cpu_feature_enabled (X86_FEATURE_FRED )) +idt_setup_traps (); +cpu_init (); }
Signature · 1 line
2.34.1
PATCH v9 36/36 x86/fred: Disable FRED by default in its early stage
In reply to Xin LiDisable FRED by default in its early stage.
To enable FRED, a new kernel command line option "fred" needs to be added.
---
Changes since v7:
* Add a log message when FRED is enabled.
--- Documentation/admin-guide/kernel-parameters.txt | 4 ++++ arch/x86/kernel/cpu/common.c | 3 +++ arch/x86/kernel/fred.c | 3 +++ 3 files changed, 10 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +4 -0
@@ -1513,6 +1513,10 @@ Warning: use of this parameter will taint the kernel and may cause unknown problems. + fred + Forcefully enable flexible return and event delivery, + which is otherwise disabled by default. + ftrace=[tracer] [FTRACE] will set and start the specified tracer as early as possible in order to facilitate early
arch/x86/kernel/cpu/common.c +3 -0
@@ -1451,6 +1451,9 @@ static void __init cpu_parse_early_param(void)char *argptr = arg, *opt;int arglen, taint =0 ; +if (!cmdline_find_option_bool (boot_command_line,"fred" )) +setup_clear_cpu_cap (X86_FEATURE_FRED ); +#ifdef CONFIG_X86_32 if (cmdline_find_option_bool (boot_command_line,"no387" )) #ifdef CONFIG_MATH_EMULATION
arch/x86/kernel/fred.c +3 -0
@@ -8,6 +8,9 @@void cpu_init_fred_exceptions (void ) { +/* When FRED is enabled by default, this log message may not needed */ +pr_info ("Initialize FRED on CPU%d\n" ,smp_processor_id ()); + wrmsrl(MSR_IA32_FRED_CONFIG, /* Reserve for CALL emulation */ FRED_CONFIG_REDZONE |
Signature · 1 line
2.34.1