Add SEV-SNP restricted injection hypervisor support
v2 cover Add SEV-SNP restricted injection hypervisor support
Hi all,
This is a v2 of the restricted injection hypervisor support patches.
The previous version was submitted here:
https://lore.kernel.org/r/[email protected].
Since the previous submission, one issue reported by the kernel test robot was fixed.
All comments and review feedback are appreciated.
Thanks.
Changelog:
v1
Operating systems may not handle unexpected interrupt or exception sequences. A malicious hypervisor can inject random interrupt or exception sequences, putting guest drivers or guest OS kernels into an unexpected state, which could lead to security issues.
To address this concern, SEV-SNP restricts the injection of interrupts and exceptions to those only allowed by the guest. Restricted Injection disables all hypervisor-based interrupt queuing and event injection for all vectors, allowing only a single vector, #HV (28), which is reserved for SNP guest use but is never generated by hardware. #HV is only permitted to be injected into VMSAs that execute with Restricted Injection.
Guests operating with Restricted Injection are expected to communicate with the hypervisor about events via a software-managed para-virtualization interface. This interface can utilize #HV injection as a doorbell to inform the guest that new events have occurred. This patch set implements Restricted Injection on the KVM side directly into VMPL0.
Overview:
The GHCB 2.0 specification[1] defines #HV doorbell page and the #HV doorbell page NAE event allows for an SEV-SNP guest to register a doorbell page for use with the hypervisor injection exception (#HV). When Restricted Injection is active, only #HV exceptions can be injected into the guest, and the hypervisor follows the GHCB #HV doorbell communication to inject the exception or interrupt. Restricted Injection can be enabled by setting the bit in vmsa_features.
The patchset is rebased on the kvm/next (commit 15e1c3d65975524c5c792fcd59f7d89f00402261).
Testing:
The patchset has been tested with the sev-snp guest, ovmf and qemu supporting restricted injection.
Four test sets:
1.ls -lr /
2.apt update
3.fio
4.perf
Thanks
Melody
Melody Wang (6):
x86/sev: Define the #HV doorbell page structure
KVM: SVM: Add support for the SEV-SNP #HV doorbell page NAE event
KVM: SVM: Inject #HV when restricted injection is active
KVM: SVM: Inject NMIs when restricted injection is active
KVM: SVM: Inject MCEs when restricted injection is active
KVM: SVM: Enable restricted injection for an SEV-SNP guest
arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/sev-common.h | 1 + arch/x86/include/asm/svm.h | 41 +++++ arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/include/uapi/asm/svm.h | 5 + arch/x86/kvm/svm/sev.c | 277 ++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.c | 44 ++++- arch/x86/kvm/svm/svm.h | 26 ++- arch/x86/kvm/vmx/main.c | 1 + arch/x86/kvm/vmx/vmx.c | 5 + arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 8 + 14 files changed, 408 insertions(+), 5 deletions(-)
Signature · 3 lines
2.34.1 [1] https://www.amd.com/content/dam/amd/en/documents/epyc-technical-docs/specifications/56421.pdf
PATCH v2 1/6 x86/sev: Define the #HV doorbell page structure
Restricted injection is a feature which enforces additional interrupt and event injection security protections for a SEV-SNP guest. It disables all hypervisor-based interrupt queuing and event injection of all vectors except a new exception vector, #HV (28), which is reserved for SNP guest use, but never generated by hardware. #HV is only allowed to be injected into VMSAs that execute with Restricted Injection.
The guests running with the SNP restricted injection feature active limit the host to ringing a doorbell with a #HV exception.
Define two fields in the #HV doorbell page: a pending event field, and an EOI assist.
Create the structure definition for the #HV doorbell page as per GHCB specification.
--- arch/x86/include/asm/svm.h | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
arch/x86/include/asm/svm.h +41 -0
@@ -516,6 +516,47 @@ struct ghcb {u32 ghcb_usage; } __packed; +/* +* Hypervisor doorbell page: +* +* Used when restricted injection is enabled for a VM. One page in size that +* is shared between the guest and hypervisor to communicate exception and +* interrupt events. +*/ +struct hvdb_events { +/* First 64 bytes of HV doorbell page defined in GHCB specification */ +union { +struct { +/* Interrupt vector being injected */ +u8 vector; + +/* Non-maskable event field (NMI, etc.) */ +u8 nm_events; + }; + +struct { +/* Non-maskable event indicators */ +u16 reserved1:8 , + nmi:1 , + mce:1 , + reserved2:5 , + no_further_signal:1 ; + }; + +u16 pending_events; + }; + +u8 no_eoi_required; + +u8 reserved3[61 ]; +}; + +struct hvdb { +struct hvdb_events events; + +/* Remainder of the page is for software use */ +u8 reserved[PAGE_SIZE -sizeof (struct hvdb_events )]; +};#define EXPECTED_VMCB_SAVE_AREA_SIZE 744#define EXPECTED_GHCB_SAVE_AREA_SIZE 1032
Signature · 1 line
2.34.1
PATCH v2 2/6 KVM: SVM: Add support for the SEV-SNP #HV doorbell page NAE event
In reply to Melody WangTo support the SEV-SNP Restricted Injection feature, the SEV-SNP guest must register a #HV doorbell page for use with the #HV.
The #HV doorbell page NAE event allows the guest to register a #HV doorbell page. The NAE event consists of four actions: GET_PREFERRED, SET, QUERY, CLEAR. Implement the NAE event as per GHCB specification.
--- arch/x86/include/uapi/asm/svm.h | 5 +++ arch/x86/kvm/svm/sev.c | 73 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 2 + 3 files changed, 80 insertions(+)
arch/x86/include/uapi/asm/svm.h +5 -0
@@ -115,6 +115,11 @@#define SVM_VMGEXIT_AP_CREATE_ON_INIT 0#define SVM_VMGEXIT_AP_CREATE 1#define SVM_VMGEXIT_AP_DESTROY 2 +#define SVM_VMGEXIT_HVDB_PAGE 0x80000014 +#define SVM_VMGEXIT_HVDB_GET_PREFERRED 0 +#define SVM_VMGEXIT_HVDB_SET 1 +#define SVM_VMGEXIT_HVDB_QUERY 2 +#define SVM_VMGEXIT_HVDB_CLEAR 3#define SVM_VMGEXIT_SNP_RUN_VMPL 0x80000018#define SVM_VMGEXIT_HV_FEATURES 0x8000fffd#define SVM_VMGEXIT_TERM_REQUEST 0x8000fffe
arch/x86/kvm/svm/sev.c +73 -0
97 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +2 -0
@@ -225,6 +225,8 @@ struct vcpu_sev_es_state {gpa_t snp_vmsa_gpa;bool snp_ap_waiting_for_reset;bool snp_has_guest_vmsa; + +gpa_t hvdb_gpa; };struct vcpu_svm {
Signature · 1 line
2.34.1
PATCH v2 3/6 KVM: SVM: Inject #HV when restricted injection is active
In reply to Melody WangWhen restricted injection is active, only #HV exceptions can be injected into the SEV-SNP guest.
Detect that restricted injection feature is active for the guest, and then follow the #HV doorbell communication from the GHCB specification to inject the interrupt or exception.
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 153 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 19 +++- arch/x86/kvm/svm/svm.h | 21 ++++- 4 files changed, 190 insertions(+), 4 deletions(-)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -35,6 +35,7 @@#define MC_VECTOR 18#define XM_VECTOR 19#define VE_VECTOR 20 +#define HV_VECTOR 28/* Select x86 specific features in <linux/kvm.h> */ #define __KVM_HAVE_PIT
arch/x86/kvm/svm/sev.c +153 -0
156 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +16 -3
@@ -447,6 +447,9 @@ static int svm_update_soft_interrupt_rip(struct kvm_vcpu *vcpu) svm->soft_int_old_rip = old_rip; svm->soft_int_next_rip = rip; +if (sev_snp_queue_exception (vcpu)) +return 0 ; +if (nrips)kvm_rip_write (vcpu, old_rip);
@@ -467,6 +470,9 @@ static void svm_inject_exception(struct kvm_vcpu *vcpu) svm_update_soft_interrupt_rip(vcpu))return ; +if (sev_snp_queue_exception (vcpu)) +return ; + svm->vmcb->control.event_inj = ex->vector |SVM_EVTINJ_VALID | (ex->has_error_code ?SVM_EVTINJ_VALID_ERR :0 )
@@ -3662,10 +3668,12 @@ static void svm_inject_irq(struct kvm_vcpu *vcpu, bool reinjected)trace_kvm_inj_virq (vcpu->arch.interrupt.nr, vcpu->arch.interrupt.soft, reinjected); - ++vcpu->stat.irq_injections; - svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | -SVM_EVTINJ_VALID | type; +if (!sev_snp_inject (INJECT_IRQ , vcpu)) + svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | +SVM_EVTINJ_VALID | type; + + ++vcpu->stat.irq_injections; } void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
@@ -3810,6 +3818,9 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)if (!gif_set (svm))return true; +if (sev_snp_is_rinj_active (vcpu)) +return sev_snp_blocked (INJECT_IRQ , vcpu); +if (is_guest_mode (vcpu)) {/* As long as interrupts are being delivered... */ if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
@@ -4128,6 +4139,8 @@ static void svm_cancel_injection(struct kvm_vcpu *vcpu)struct vcpu_svm *svm =to_svm (vcpu);struct vmcb_control_area *control = &svm->vmcb->control; +sev_snp_cancel_injection (vcpu); + control->exit_int_info = control->event_inj; control->exit_int_info_err = control->event_inj_err; control->event_inj =0 ;
arch/x86/kvm/svm/svm.h +20 -1
@@ -41,6 +41,10 @@ extern bool x2avic_enabled;extern bool vnmi;extern int lbrv; +enum inject_type { +INJECT_IRQ , +}; +/* * Clean bits in VMCB. * VMCB_ALL_CLEAN_MASK might also need to
@@ -751,6 +755,17 @@ void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu);int sev_gmem_prepare (struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order);void sev_gmem_invalidate (kvm_pfn_t start,kvm_pfn_t end);int sev_private_max_mapping_level (struct kvm *kvm,kvm_pfn_t pfn); +bool sev_snp_queue_exception (struct kvm_vcpu *vcpu); +bool sev_snp_inject (enum inject_type type,struct kvm_vcpu *vcpu); +void sev_snp_cancel_injection (struct kvm_vcpu *vcpu); +bool sev_snp_blocked (enum inject_type type,struct kvm_vcpu *vcpu); +static inline bool sev_snp_is_rinj_active (struct kvm_vcpu *vcpu) +{ +struct kvm_sev_info *sev = &to_kvm_svm (vcpu->kvm)->sev_info; + +return sev_snp_guest (vcpu->kvm) && + (sev->vmsa_features &SVM_SEV_FEAT_RESTRICTED_INJECTION ); +};#else static inline struct page *snp_safe_alloc_page_node (int node,gfp_t gfp) {
@@ -781,7 +796,11 @@ static inline int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) {return 0 ; } - +static inline bool sev_snp_queue_exception (struct kvm_vcpu *vcpu) {return false; } +static inline bool sev_snp_inject (enum inject_type type,struct kvm_vcpu *vcpu) {return false; } +static inline void sev_snp_cancel_injection (struct kvm_vcpu *vcpu) {} +static inline bool sev_snp_blocked (enum inject_type type,struct kvm_vcpu *vcpu) {return false; } +static inline bool sev_snp_is_rinj_active (struct kvm_vcpu *vcpu) {return false; }#endif /* vmenter.S */
Signature · 1 line
2.34.1
Re: [PATCH v2 3/6] KVM: SVM: Inject #HV when restricted injection is active
Hello
arch/x86/kvm/svm/sev.c int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) · line 5071
Quoted 9 lines · Melody Wang
arch/x86/kvm/svm/sev.c +9
-0+static bool __sev_snp_inject (enum inject_type type,struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +struct kvm_host_map hvdb_map; +struct hvdb *hvdb; + + hvdb =map_hvdb (vcpu, &hvdb_map); +if (!hvdb) +return false;
old_vector = hvdb->events.vector;
arch/x86/kvm/svm/sev.c int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) · line 5075
Quoted 4 lines
arch/x86/kvm/svm/sev.c +4
-0+ + hvdb->events.vector = vcpu->arch.interrupt.nr; + +prepare_hv_injection (svm, hvdb);
The specification recommends saving an HV injection on the following condition:
if (!(type == INJECT_IRQ && old_vector))
prepare_hv_injection(svm, hvdb)If PendingEvent.Vector was previously non-zero, because the guest has not yet chosen to acknowledge the interrupt, the previous vector number can be overwritten by the new vector number without sending another #HV (regardless of the value of PendingEvent.NoFurtherSignal), because the guest should already have been informed that a vector was pending.
Quoted 6 lines
arch/x86/kvm/svm/sev.c +6
-0+ +unmap_hvdb (vcpu, &hvdb_map); + +return true; +} +
PATCH v2 4/6 KVM: SVM: Inject NMIs when restricted injection is active
In reply to Melody WangWhen restricted injection is active, only #HV exceptions can be injected into the SEV-SNP guest.
Detect that restricted injection feature is active for the guest, and then follow the #HV doorbell communication from the GHCB specification to inject NMIs.
--- arch/x86/kvm/svm/sev.c | 19 ++++++++++++++++--- arch/x86/kvm/svm/svm.c | 8 ++++++++ arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 25 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +16 -3
@@ -5070,7 +5070,10 @@ static bool __sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu)if (!hvdb)return false; - hvdb->events.vector = vcpu->arch.interrupt.nr; +if (type ==INJECT_NMI ) + hvdb->events.nmi =1 ; +else + hvdb->events.vector = vcpu->arch.interrupt.nr;prepare_hv_injection (svm, hvdb);
@@ -5148,10 +5151,17 @@ void sev_snp_cancel_injection(struct kvm_vcpu *vcpu)/* Copy info back into event_inj field (replaces #HV) */ svm->vmcb->control.event_inj =SVM_EVTINJ_VALID ; +/* +* KVM only injects a single event each time (prepare_hv_injection), +* so when events.nmi is true, the vector will be zero +*/ if (hvdb->events.vector) svm->vmcb->control.event_inj |= hvdb->events.vector |SVM_EVTINJ_TYPE_INTR ; +if (hvdb->events.nmi) + svm->vmcb->control.event_inj |=SVM_EVTINJ_TYPE_NMI ; + hvdb->events.pending_events =0 ; out:
@@ -5169,8 +5179,11 @@ bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu)if (!hvdb)return true; -/* Indicate interrupts blocked based on guest acknowledgment */ - blocked = !!hvdb->events.vector; +/* Indicate NMIs and interrupts blocked based on guest acknowledgment */ +if (type ==INJECT_NMI ) + blocked = hvdb->events.nmi; +else + blocked = !!hvdb->events.vector;unmap_hvdb (vcpu, &hvdb_map);
arch/x86/kvm/svm/svm.c +8 -0
@@ -3602,6 +3602,9 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); +if (sev_snp_inject (INJECT_NMI , vcpu)) + goto status; + svm->vmcb->control.event_inj =SVM_EVTINJ_VALID |SVM_EVTINJ_TYPE_NMI ;if (svm->nmi_l1_to_l2)
@@ -3616,6 +3619,8 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu) svm->nmi_masked = true;svm_set_iret_intercept (svm); } + +status: ++vcpu->stat.nmi_injections; }
@@ -3786,6 +3791,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)if (!gif_set (svm))return true; +if (sev_snp_is_rinj_active (vcpu)) +return sev_snp_blocked (INJECT_NMI , vcpu); +if (is_guest_mode (vcpu) &&nested_exit_on_nmi (svm))return false;
arch/x86/kvm/svm/svm.h +1 -0
@@ -43,6 +43,7 @@ extern int lbrv;enum inject_type {INJECT_IRQ , +INJECT_NMI , };/*
Signature · 1 line
2.34.1
PATCH v2 5/6 KVM: SVM: Inject MCEs when restricted injection is active
In reply to Melody WangWhen restricted injection is active, only #HV exceptions can be injected into the SEV-SNP guest.
Detect that restricted injection feature is active for the guest, and then follow the #HV doorbell communication from the GHCB specification to inject the MCEs.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/sev.c | 16 ++++++++++++++-- arch/x86/kvm/svm/svm.c | 17 +++++++++++++++++ arch/x86/kvm/svm/svm.h | 2 ++ arch/x86/kvm/vmx/main.c | 1 + arch/x86/kvm/vmx/vmx.c | 5 +++++ arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 8 ++++++++ 9 files changed, 50 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -76,6 +76,7 @@ KVM_X86_OP(inject_exception) KVM_X86_OP(cancel_injection) KVM_X86_OP(interrupt_allowed) KVM_X86_OP(nmi_allowed) +KVM_X86_OP_OPTIONAL(mce_allowed) KVM_X86_OP(get_nmi_mask) KVM_X86_OP(set_nmi_mask) KVM_X86_OP(enable_nmi_window)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1715,6 +1715,7 @@ struct kvm_x86_ops {void (*cancel_injection)(struct kvm_vcpu *vcpu);int (*interrupt_allowed)(struct kvm_vcpu *vcpu,bool for_injection);int (*nmi_allowed)(struct kvm_vcpu *vcpu,bool for_injection); +int (*mce_allowed)(struct kvm_vcpu *vcpu);bool (*get_nmi_mask)(struct kvm_vcpu *vcpu);void (*set_nmi_mask)(struct kvm_vcpu *vcpu,bool masked);/* Whether or not a virtual NMI is pending in hardware. */
arch/x86/kvm/svm/sev.c +14 -2
@@ -5072,6 +5072,8 @@ static bool __sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu)if (type ==INJECT_NMI ) hvdb->events.nmi =1 ; +else if (type ==INJECT_MCE ) + hvdb->events.mce =1 ;else hvdb->events.vector = vcpu->arch.interrupt.nr;
@@ -5089,6 +5091,11 @@ bool sev_snp_queue_exception(struct kvm_vcpu *vcpu)if (!sev_snp_is_rinj_active (vcpu))return false; +if (vcpu->arch.exception.vector ==MC_VECTOR ) { +if (__sev_snp_inject (INJECT_MCE , vcpu)) +return true; + } +/* * Restricted injection is enabled, only #HV is supported. * If the vector is not HV_VECTOR, do not inject the exception,
@@ -5153,7 +5160,7 @@ void sev_snp_cancel_injection(struct kvm_vcpu *vcpu)/* * KVM only injects a single event each time (prepare_hv_injection), -* so when events.nmi is true, the vector will be zero +* so when events.nmi is true, the mce and vector will be zero */ if (hvdb->events.vector) svm->vmcb->control.event_inj |= hvdb->events.vector |
@@ -5162,6 +5169,9 @@ void sev_snp_cancel_injection(struct kvm_vcpu *vcpu)if (hvdb->events.nmi) svm->vmcb->control.event_inj |=SVM_EVTINJ_TYPE_NMI ; +if (hvdb->events.mce) + svm->vmcb->control.event_inj |=MC_VECTOR |SVM_EVTINJ_TYPE_EXEPT ; + hvdb->events.pending_events =0 ; out:
@@ -5179,9 +5189,11 @@ bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu)if (!hvdb)return true; -/* Indicate NMIs and interrupts blocked based on guest acknowledgment */ +/* Indicate NMIs, MCEs and interrupts blocked based on guest acknowledgment */ if (type ==INJECT_NMI ) blocked = hvdb->events.nmi; +else if (type ==INJECT_MCE ) + blocked = hvdb->events.mce;else blocked = !!hvdb->events.vector;
arch/x86/kvm/svm/svm.c +17 -0
@@ -3867,6 +3867,22 @@ static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)return 1 ; } +bool svm_mce_blocked (struct kvm_vcpu *vcpu) +{ +if (sev_snp_is_rinj_active (vcpu)) +return sev_snp_blocked (INJECT_MCE , vcpu); + +return false; +} + +static int svm_mce_allowed (struct kvm_vcpu *vcpu) +{ +if (svm_mce_blocked (vcpu)) +return 0 ; + +return 1 ; +} +static void svm_enable_irq_window (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -5066,6 +5082,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .cancel_injection = svm_cancel_injection, .interrupt_allowed = svm_interrupt_allowed, .nmi_allowed = svm_nmi_allowed, + .mce_allowed = svm_mce_allowed, .get_nmi_mask = svm_get_nmi_mask, .set_nmi_mask = svm_set_nmi_mask, .enable_nmi_window = svm_enable_nmi_window,
arch/x86/kvm/svm/svm.h +2 -0
@@ -44,6 +44,7 @@ extern int lbrv;enum inject_type {INJECT_IRQ ,INJECT_NMI , +INJECT_MCE , };/*
@@ -602,6 +603,7 @@ void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);void disable_nmi_singlestep (struct vcpu_svm *svm);bool svm_smi_blocked (struct kvm_vcpu *vcpu);bool svm_nmi_blocked (struct kvm_vcpu *vcpu); +bool svm_mce_blocked (struct kvm_vcpu *vcpu);bool svm_interrupt_blocked (struct kvm_vcpu *vcpu);void svm_set_gif (struct vcpu_svm *svm,bool value);int svm_invoke_exit_handler (struct kvm_vcpu *vcpu,u64 exit_code);
arch/x86/kvm/vmx/main.c +1 -0
@@ -84,6 +84,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .cancel_injection = vmx_cancel_injection, .interrupt_allowed = vmx_interrupt_allowed, .nmi_allowed = vmx_nmi_allowed, + .mce_allowed = vmx_mce_allowed, .get_nmi_mask = vmx_get_nmi_mask, .set_nmi_mask = vmx_set_nmi_mask, .enable_nmi_window = vmx_enable_nmi_window,
arch/x86/kvm/vmx/vmx.c +5 -0
@@ -5093,6 +5093,11 @@ int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)return !vmx_interrupt_blocked (vcpu); } +int vmx_mce_allowed (struct kvm_vcpu *vcpu) +{ +return 1 ; +} +int vmx_set_tss_addr (struct kvm *kvm,unsigned int addr) {void __user *ret;
arch/x86/kvm/vmx/x86_ops.h +1 -0
@@ -92,6 +92,7 @@ void vmx_inject_exception(struct kvm_vcpu *vcpu);void vmx_cancel_injection (struct kvm_vcpu *vcpu);int vmx_interrupt_allowed (struct kvm_vcpu *vcpu,bool for_injection);int vmx_nmi_allowed (struct kvm_vcpu *vcpu,bool for_injection); +int vmx_mce_allowed (struct kvm_vcpu *vcpu);bool vmx_get_nmi_mask (struct kvm_vcpu *vcpu);void vmx_set_nmi_mask (struct kvm_vcpu *vcpu,bool masked);void vmx_enable_nmi_window (struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +8 -0
@@ -10485,6 +10485,12 @@ static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu, } } +if (vcpu->arch.exception.vector ==MC_VECTOR ) { + r =static_call (kvm_x86_mce_allowed)(vcpu); +if (!r) + goto out_except; + } +kvm_inject_exception (vcpu); vcpu->arch.exception.pending = false;
@@ -10493,6 +10499,8 @@ static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu, can_inject = false; } +out_except: +/* Don't inject interrupts if the user asked to avoid doing so */ if (vcpu->guest_debug &KVM_GUESTDBG_BLOCKIRQ )return 0 ;
Signature · 1 line
2.34.1
Re: [PATCH v2 5/6] KVM: SVM: Inject MCEs when restricted injection is active
Hello
arch/x86/kvm/svm/sev.c void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) · line 5164
Quoted 7 lines · Melody Wang
arch/x86/kvm/svm/sev.c +1
-1@@ -5153,7 +5160,7 @@ void sev_snp_cancel_injection(struct kvm_vcpu *vcpu)/* * KVM only injects a single event each time (prepare_hv_injection), -* so when events.nmi is true, the vector will be zero +* so when events.nmi is true, the mce and vector will be zero */
This comment seems ambiguous, and in the following code, events.nmi/mce/vector appears to be able to be true simultaneously, rather than being mutually exclusive.
Quoted 8 lines
arch/x86/kvm/svm/sev.c +2
-0if (hvdb->events.vector) svm->vmcb->control.event_inj |= hvdb->events.vector |@@ -5162,6 +5169,9 @@ void sev_snp_cancel_injection(struct kvm_vcpu *vcpu)if (hvdb->events.nmi) svm->vmcb->control.event_inj |=SVM_EVTINJ_TYPE_NMI ; +if (hvdb->events.mce) + svm->vmcb->control.event_inj |=MC_VECTOR |SVM_EVTINJ_TYPE_EXEPT ;
PATCH v2 6/6 KVM: SVM: Enable restricted injection for an SEV-SNP guest
In reply to Melody WangEnable the restricted injection in an SEV-SNP guest by setting the restricted injection bit in the VMSA SEV features field (SEV_FEATURES[3]) from QEMU.
Add the restricted injection supporting the hypervisor advertised features.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/sev-common.h | 1 + arch/x86/kvm/svm/sev.c | 26 +++++++++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -446,6 +446,7 @@#define X86_FEATURE_SEV_SNP (19*32+ 4)/* "sev_snp" AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9)/* Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10)/* AMD hardware-enforced cache coherency */ +#define X86_FEATURE_RESTRICTED_INJECTION (19*32+12)/* AMD SEV Restricted Injection */ #define X86_FEATURE_DEBUG_SWAP (19*32+14)/* "debug_swap" AMD SEV-ES full debug state swap support */ #define X86_FEATURE_SVSM (19*32+28)/* "svsm" SVSM present */
arch/x86/include/asm/sev-common.h +1 -0
@@ -136,6 +136,7 @@ enum psc_op {#define GHCB_HV_FT_SNP BIT_ULL(0)#define GHCB_HV_FT_SNP_AP_CREATION BIT_ULL(1) +#define GHCB_HV_FT_SNP_RINJ (BIT_ULL(2) | GHCB_HV_FT_SNP_AP_CREATION)#define GHCB_HV_FT_SNP_MULTI_VMPL BIT_ULL(5)/*
arch/x86/kvm/svm/sev.c +25 -1
@@ -39,7 +39,9 @@#define GHCB_VERSION_DEFAULT 2ULL#define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION) +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | \ + GHCB_HV_FT_SNP_AP_CREATION | \ + GHCB_HV_FT_SNP_RINJ)/* enable/disable SEV support */ static bool sev_enabled = true;
@@ -56,6 +58,10 @@ module_param_named(sev_snp, sev_snp_enabled, bool, 0444);/* enable/disable SEV-ES DebugSwap support */ static bool sev_es_debug_swap_enabled = true;module_param_named (debug_swap, sev_es_debug_swap_enabled, bool,0444 ); + +/* enable/disable SEV-SNP Restricted Injection support */ +static bool sev_snp_restricted_injection_enabled = true; +module_param_named (restricted_injection, sev_snp_restricted_injection_enabled, bool,0444 );static u64 sev_supported_vmsa_features;#define AP_RESET_HOLD_NONE 0
@@ -3079,6 +3085,12 @@ void __init sev_hardware_setup(void) sev_supported_vmsa_features =0 ;if (sev_es_debug_swap_enabled) sev_supported_vmsa_features |=SVM_SEV_FEAT_DEBUG_SWAP ; + +if (!sev_snp_enabled || !cpu_feature_enabled (X86_FEATURE_RESTRICTED_INJECTION )) + sev_snp_restricted_injection_enabled = false; + +if (sev_snp_restricted_injection_enabled) + sev_supported_vmsa_features |=SVM_SEV_FEAT_RESTRICTED_INJECTION ; }void sev_hardware_unsetup (void )
@@ -4556,6 +4568,15 @@ void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm)sev_es_vcpu_after_set_cpuid (svm); } +static void sev_snp_init_vmcb (struct vcpu_svm *svm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (svm->vcpu.kvm)->sev_info; + +/* V_NMI is not supported when Restricted Injection is enabled */ +if (sev->vmsa_features &SVM_SEV_FEAT_RESTRICTED_INJECTION ) + svm->vmcb->control.int_ctl &= ~V_NMI_ENABLE_MASK ; +} +static void sev_es_init_vmcb (struct vcpu_svm *svm) {struct vmcb *vmcb = svm->vmcb01.ptr;
@@ -4613,6 +4634,9 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)/* Clear intercepts on selected MSRs */ set_msr_interception (vcpu, svm->msrpm,MSR_EFER ,1 ,1 );set_msr_interception (vcpu, svm->msrpm,MSR_IA32_CR_PAT ,1 ,1 ); + +if (sev_snp_guest (vcpu->kvm)) +sev_snp_init_vmcb (svm); }void sev_init_vmcb (struct vcpu_svm *svm)
Signature · 1 line
2.34.1