AMD: Add Secure AVIC KVM Support
cover AMD: Add Secure AVIC KVM Support
Introduction
------------
Secure AVIC is a new hardware feature in the AMD64 architecture to allow SEV-SNP guests to prevent hypervisor from generating unexpected interrupts to a vCPU or otherwise violate architectural assumptions around APIC behavior.
One of the significant differences from AVIC or emulated x2APIC is that Secure AVIC uses a guest-owned and managed APIC backing page. It also introduces additional fields in both the VMCB and the Secure AVIC backing page to aid the guest in limiting which interrupt vectors can be injected into the guest.
Guest APIC Backing Page
-----------------------
Each vCPU has a guest-allocated APIC backing page of size 4K, which maintains APIC state for that vCPU. The x2APIC MSRs are mapped at their corresposing x2APIC MMIO offset within the guest APIC backing page. All x2APIC accesses by guest or Secure AVIC hardware operate on this backing page. The backing page should be pinned and NPT entry for it should be always mapped while the corresponding vCPU is running.
MSR Accesses
------------
Secure AVIC only supports x2APIC MSR accesses. xAPIC MMIO offset based accesses are not supported.
Some of the MSR writes such as ICR writes (with shorthand equal to self), SELF_IPI, EOI, TPR writes are accelerated by Secure AVIC hardware. Other MSR writes generate a #VC exception ( VMEXIT_AVIC_NOACCEL or VMEXIT_AVIC_INCOMPLETE_IPI). The #VC exception handler reads/writes to the guest APIC backing page. As guest APIC backing page is accessible to the guest, guest can optimize APIC register access by directly reading/writing to the guest APIC backing page (instead of taking the #VC exception route). APIC msr reads are accelerated similar to AVIC, as described in table "15-22. Guest vAPIC Register Access Behavior" of APM.
In addition to the architected MSRs, following new fields are added to the guest APIC backing page which can be modified directly by the guest:
a. ALLOWED_IRR
ALLOWED_IRR vector indicates the interrupt vectors which the guest allows the hypervisor to send. The combination of host-controlled REQUESTED_IRR vectors (part of VMCB) and ALLOWED_IRR is used by hardware to update the IRR vectors of the Guest APIC backing page.
#Offset #bits Description 204h 31:0 Guest allowed vectors 0-31 214h 31:0 Guest allowed vectors 32-63
...
274h 31:0 Guest allowed vectors 224-255
ALLOWED_IRR is meant to be used specifically for vectors that the hypervisor emulates and is allowed to inject, such as IOAPIC/MSI device interrupts. Interrupt vectors used exclusively by the guest itself (like IPI vectors) should not be allowed to be injected into the guest for security reasons.
b. NMI Request
#Offset #bits Description 278h 0 Set by Guest to request Virtual NMI
Guest can set NMI_REQUEST to trigger APIC_ICR based NMIs.
APIC Registers
--------------
1. APIC ID
APIC_ID values is set by KVM and similar to x2apic, it is equal to vcpu_id for a vCPU.
2. APIC LVR
APIC Version register is expected to be read from KVM's APIC state using MSR_PROT rdmsr VMGEXIT and updated in guest APIC backing page.
3. APIC TPR
TPR writes are accelerated and not communicated to KVM. So, hypervisor does not have information about TPR value for a vCPU.
4. APIC PPR
Current state of PPR is not visible to KVM.
5. APIC SPIV
Spurious Interrupt Vector register value is not communicated to KVM.
6. APIC IRR and ISR
IRR and ISR states are visible only to guest. So, KVM cannot use these registers to determine interrupt which are pending completion.
7. APIC TMR
Trigger Mode Register state is owned by guest and not visible to KVM.
8. Timer registers - TMICT, TMCCT, TDCR
Timer registers are accessed using MSR_PROT VMGEXIT calls and not from the guest APIC backing page.
9. LVT* registers
LVT registers state is accessed from KVM APIC state for the vCPU.
Idle halt Intercept
-------------------
As hypervisor does not have access to the APIC IRR state for a Secure AVIC guest, idle halt intercept feature should be always enabled for a Secure AVIC guest. Otherwise, any pending interrupts in APIC IRR during halt vmexit would not be serviced and vCPU could get stuck in halt forever. For idle halt intercept to work APIC TPR value should not block the pending interrupts.
LAPIC Timer Support
-------------------
LAPIC timer is emulated by KVM. So, APIC_LVTT, APIC_TMICT and APIC_TDCR, APIC_TMCCT APIC registers are not read/written to the guest APIC backing page and are communicated to the hypervisor using MSR_PROT VMGEXIT.
IPI Support
-----------
Only SELF_IPI is accelerated by Secure AVIC hardware. Other IPI destination shorthands result in VMEXIT_AVIC_INCOMPLETE_IPI #VC exception. The expected guest handling for VMEXIT_AVIC_INCOMPLETE_IPI is:
- For interrupts, update APIC_IRR in target vCPUs' guest APIC backing page.
- For NMIs, update NMI_REQUEST in target vCPUs' guest backing page.
- ICR based SMI, INIT, SIPI requests are not supported.
- After updating the target vCPU's guest APIC backing page, source vCPU does a MSR_PROT VMGEXIT.
- KVM either wakes up the non-running target vCPU or sends a AVIC doorbell.
Exceptions Injection
--------------------
Secure AVIC does not support event injection for guests with Secure AVIC enabled in SEV_FEATURES. So, KVM cannot inject exceptions to Secure AVIC guests. Hardware takes care of reinjecting an interrupted exception (for example due to NPF) raised in guest on next VMRUN. VC exception is not reinjected. KVM clears all exception intercepts for Secure AVIC guest.
Interrupt Injection
-------------------
IOAPIC and MSI based device interrupts can be injected by KVM. The interrupt flow for this is:
- IOAPIC/MSI interrupts are updated in KVM's APIC_IRR state via kvm_irq_delivery_to_apic().
- in ->inject_irq() callback, all interrupts which are set in KVM's APIC_IRR are copied to RequestedIRR VMCB field and UpdateIRR bit is set.
- VMENTER moves the current value of RequestedIRR to APIC_IRR in guest APIC backing page and clears UpdateIRR.
Given that hardware clearing of RequestedIRR and UpdateIRR can race with software writes to these fields, above interrupt injection flow ensures that all RequestedIRR and UpdateIRR writes are done from the same CPU where vCPU is run.
As interrupt delivery to vCPU is managed by hardware, interrupt window is not applicable for Secure AVIC guests and interrupts are always allowed to be injected.
PIC interrupts
--------------
Legacy PIC interrupts cannot be injected as they required event_inj or VINTR injection support. Both of these are cannot be done for Secure AVIC guest.
PIT
---
PIT Reinject mode is not supported as it requires IRQ ack notification on EOI. As EOI is accelerated for edge interrupts, IRQ ack notification is not called for those interrupts.
NMI Injection
-------------
NMI injection requires ALLOWED_NMI to be set in Secure AVIC control msr by the guest. Only VNMI injection is allowed.
Open Points
-----------
- RTC_GSI requires pending EOI information to detect coalesced interrupts. As RTC_GSI is edge triggered, Secure AVIC does not forward EOI write to KVM for this interrupt. In addition, APIC_IRR and APIC_ISR states are not visible to KVM and are part of guest APIC backing page. Approach taken in this series is to disable checking of coalesced RTC_GSI interrupts for Secure AVIC, which could impact userspace.
- EOI handling for level interrupts uses KVM's unused APIC_ISR regs for tracking pending level interrupts. KVM uses its APIC_TMR state to determine level-triggered interrupts. As KVM's APIC_TMR is updated from IOAPIC redirect tables, the TMR information should be accurate and match guest APIC state. This can be cleaned up later to not use KVM's APIC_ISR state and maintained within sev code.
- Spurious Interrupt Vector Register writes are not visible to KVM. So, KVM cannot determine if the SW enabled bit is set.
- As exceptions cannot be injected by KVM, a more detailed examination of which intercepts need to be allowed for Secure AVIC guests is required.
- As KVM does not have access to the guest's APIC_IRR and APIC_ISR states, kvm_apic_pending_eoi() does not return correct information.
- External interrupts (PIC) are not supported. This breaks KVM's PIC emulation.
- PIT reinject mode is not supported.
- Current code uses KVM's vCPU APIC_IRR for interrupts which need to be injected to guest. Another approach could be to maintain pending interrupts within sev code and inject using flow similar to posted interrupts.
This series is based on top of commit f7bafceba76e ("KVM: remove kvm_arch_post_init_vm ") and is based on
git.kernel.org/pub/scm/virt/kvm/kvm.git next
Git tree is available at:
https://github.com/AMDESE/linux-kvm/tree/savic-host-latest
Qemu tree is at:
https://github.com/AMDESE/qemu/tree/secure-avic
Guest Secure AVIC support is available at:
https://lore.kernel.org/lkml/[email protected]/
This series depends on below patch series:
1. Idle Halt Intercept
https://lore.kernel.org/all/[email protected]/
2. ALLOWED_SEV_FEATURES support
https://lore.kernel.org/kvm/[email protected]/
Kishon Vijay Abraham I (5):
KVM: SEV: Do not intercept SECURE_AVIC_CONTROL MSR
KVM: SVM: Secure AVIC: Do not inject "Exceptions" for Secure AVIC
KVM: SVM/SEV: Secure AVIC: Set VGIF in VMSA area
KVM: SVM/SEV: Secure AVIC: Enable NMI support
KVM: x86: Secure AVIC: Indicate APIC is enabled by guest SW _always_
Neeraj Upadhyay (12):
KVM: x86: Convert guest_apic_protected bool to an enum type x86/cpufeatures: Add Secure AVIC CPU Feature
KVM: SVM: Add support for Secure AVIC capability in KVM
KVM: SVM: Initialize apic protected state for SAVIC guests
KVM: SVM/SEV/X86: Secure AVIC: Add support to inject interrupts
KVM: SVM/SEV/X86: Secure AVIC: Add hypervisor side IPI Delivery
Support
KVM: SVM/SEV: Do not intercept exceptions for Secure AVIC guest
KVM: SVM/SEV: Add SVM_VMGEXIT_SECURE_AVIC GHCB protocol event handling
KVM: x86: Secure AVIC: Add IOAPIC EOI support for level interrupts
KVM: x86/ioapic: Disable RTC_GSI EOI tracking for protected APIC
X86: SVM: Check injected vectors before waiting for timer expiry
KVM: SVM/SEV: Allow creating VMs with Secure AVIC enabled
Sean Christopherson (2):
KVM: TDX: Add support for find pending IRQ in a protected local APIC
KVM: x86: Assume timer IRQ was injected if APIC state is protected
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/msr-index.h | 2 + arch/x86/include/asm/svm.h | 9 +- arch/x86/include/uapi/asm/svm.h | 3 + arch/x86/kvm/ioapic.c | 8 +- arch/x86/kvm/irq.c | 6 + arch/x86/kvm/lapic.c | 23 +- arch/x86/kvm/lapic.h | 16 ++ arch/x86/kvm/svm/sev.c | 371 +++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 79 ++++-- arch/x86/kvm/svm/svm.h | 17 +- arch/x86/kvm/x86.c | 12 +- 14 files changed, 518 insertions(+), 31 deletions(-)
base-commit: f7bafceba76e9ab475b413578c1757ee18c3e44b
Signature · 1 line
2.34.1
RFC PATCH 1/19 KVM: TDX: Add support for find pending IRQ in a protected local APIC
From: Sean Christopherson <[email protected]>
Add flag and hook to KVM's local APIC management to support determining whether or not a TDX guest as a pending IRQ. For TDX vCPUs, the virtual APIC page is owned by the TDX module and cannot be accessed by KVM. As a result, registers that are virtualized by the CPU, e.g. PPR, cannot be read or written by KVM. To deliver interrupts for TDX guests, KVM must send an IRQ to the CPU on the posted interrupt notification vector. And to determine if TDX vCPU has a pending interrupt, KVM must check if there is an outstanding notification.
Return "no interrupt" in kvm_apic_has_interrupt() if the guest APIC is protected to short-circuit the various other flows that try to pull an IRQ out of the vAPIC, the only valid operation is querying _if_ an IRQ is pending, KVM can't do anything based on _which_ IRQ is pending.
Intentionally omit sanity checks from other flows, e.g. PPR update, so as not to degrade non-TDX guests with unnecessary checks. A well-behaved KVM and userspace will never reach those flows for TDX guests, but reaching them is not fatal if something does go awry.
Note, this doesn't handle interrupts that have been delivered to the vCPU but not yet recognized by the core, i.e. interrupts that are sitting in vmcs.GUEST_INTR_STATUS. Querying that state requires a SEAMCALL and will be supported in a future patch.
---
- Not intended for review. Taken as a base patch for this RFC development.
arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/irq.c | 3 +++ arch/x86/kvm/lapic.c | 3 +++ arch/x86/kvm/lapic.h | 2 ++ 5 files changed, 10 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -114,6 +114,7 @@ KVM_X86_OP_OPTIONAL(pi_start_assignment) KVM_X86_OP_OPTIONAL(apicv_pre_state_restore) KVM_X86_OP_OPTIONAL(apicv_post_state_restore) KVM_X86_OP_OPTIONAL_RET0(dy_apicv_has_pending_interrupt) +KVM_X86_OP_OPTIONAL(protected_apic_has_interrupt) KVM_X86_OP_OPTIONAL(set_hv_timer) KVM_X86_OP_OPTIONAL(cancel_hv_timer) KVM_X86_OP(setup_mce)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1830,6 +1830,7 @@ struct kvm_x86_ops {void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu);void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu); +bool (*protected_apic_has_interrupt)(struct kvm_vcpu *vcpu);int (*set_hv_timer)(struct kvm_vcpu *vcpu,u64 guest_deadline_tsc,bool *expired);
arch/x86/kvm/irq.c +3 -0
@@ -100,6 +100,9 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)if (kvm_cpu_has_extint (v))return 1 ; +if (lapic_in_kernel (v) && v->arch.apic->guest_apic_protected) +return static_call (kvm_x86_protected_apic_has_interrupt)(v); +return kvm_apic_has_interrupt (v) !=-1 ;/* LAPIC */ }EXPORT_SYMBOL_GPL (kvm_cpu_has_interrupt);
arch/x86/kvm/lapic.c +3 -0
@@ -2966,6 +2966,9 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)if (!kvm_apic_present (vcpu))return -1 ; +if (apic->guest_apic_protected) +return -1 ; +__apic_update_ppr (apic, &ppr);return apic_has_interrupt_for_ppr (apic, ppr); }
arch/x86/kvm/lapic.h +2 -0
@@ -65,6 +65,8 @@ struct kvm_lapic {bool sw_enabled;bool irr_pending;bool lvt0_in_nmi_mode; +/* Select registers in the vAPIC cannot be read/written. */ +bool guest_apic_protected;/* Number of bits set in ISR. */ s16 isr_count;/* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
Signature · 1 line
2.34.1
RFC PATCH 2/19 KVM: x86: Assume timer IRQ was injected if APIC state is protected
In reply to Neeraj UpadhyayFrom: Sean Christopherson <[email protected]>
If APIC state is protected, i.e. the vCPU is a TDX guest, assume a timer IRQ was injected when deciding whether or not to busy wait in the "timer advanced" path. The "real" vIRR is not readable/writable, so trying to query for a pending timer IRQ will return garbage.
Note, TDX can scour the PIR if it wants to be more precise and skip the "wait" call entirely.
---
- Not intended for review. Taken as a base patch for this RFC development.
arch/x86/kvm/lapic.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +10 -1
@@ -1797,8 +1797,17 @@ static void apic_update_lvtt(struct kvm_lapic *apic)static bool lapic_timer_int_injected (struct kvm_vcpu *vcpu) {struct kvm_lapic *apic = vcpu->arch.apic; -u32 reg =kvm_lapic_get_reg (apic,APIC_LVTT ); +u32 reg; +/* +* Assume a timer IRQ was "injected" if the APIC is protected. KVM's +* copy of the vIRR is bogus, it's the responsibility of the caller to +* precisely check whether or not a timer IRQ is pending. +*/ +if (apic->guest_apic_protected) +return true; + + reg =kvm_lapic_get_reg (apic,APIC_LVTT );if (kvm_apic_hw_enabled (apic)) {int vec = reg &APIC_VECTOR_MASK ;void *bitmap = apic->regs +APIC_ISR ;
Signature · 1 line
2.34.1
RFC PATCH 3/19 KVM: x86: Convert guest_apic_protected bool to an enum type
In reply to Neeraj UpadhyayConvert guest_apic_protected lapic struct member to an enum.. This allows to categorize guest APIC state protection according to the KVM interrupt delivery mechanism.
This is used to distinguish between SNP Secure AVIC's interrupt injection-based interrupt delivery and TDX's posted interrupt delivery mechanism. Use value 0 to indicate unprotected APIC so that functions like kvm_cpu_has_interrupt(), which require KVM to call an arch-specific callback to determine whether there are any interrupts that need to be delivered to the vCPU, can still use non-zero guest_apic_protected check. Subsequent patches for Secure AVIC-specific interrupt injection checks will need to use specific guest_apic_protected value.
--- arch/x86/kvm/lapic.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.h +7 -1
@@ -55,6 +55,12 @@ struct kvm_timer {bool hv_timer_in_use; }; +enum kvm_apic_protection { +APIC_STATE_UNPROTECTED , +APIC_STATE_PROTECTED_POSTED_INTR , +APIC_STATE_PROTECTED_INJECTED_INTR , +}; +struct kvm_lapic {unsigned long base_address;struct kvm_io_device dev;
@@ -66,7 +72,7 @@ struct kvm_lapic {bool irr_pending;bool lvt0_in_nmi_mode;/* Select registers in the vAPIC cannot be read/written. */ -bool guest_apic_protected; +enum kvm_apic_protection guest_apic_protected;/* Number of bits set in ISR. */ s16 isr_count;/* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
Signature · 1 line
2.34.1
RFC PATCH 4/19 x86/cpufeatures: Add Secure AVIC CPU Feature
In reply to Neeraj UpadhyayAdd CPU feature detection for Secure AVIC. The Secure AVIC feature provides hardware acceleration for performance sensitive APIC accesses and support for managing guest owned APIC state for SEV-SNP guests.
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -452,6 +452,7 @@#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_DEBUG_SWAP (19*32+14)/* "debug_swap" AMD SEV-ES full debug state swap support */ +#define X86_FEATURE_SECURE_AVIC (19*32+26)/* Secure AVIC */ #define X86_FEATURE_ALLOWED_SEV_FEATURES (19*32+27)/* Allowed SEV Features */ #define X86_FEATURE_SVSM (19*32+28)/* "svsm" SVSM present */ #define X86_FEATURE_HV_INUSE_WR_ALLOWED (19*32+30)/* Allow Write to in-use hypervisor-owned pages */
Signature · 1 line
2.34.1
RFC PATCH 5/19 KVM: SVM: Add support for Secure AVIC capability in KVM
In reply to Neeraj UpadhyayAdd support to KVM for determining if a system is capable of supporting Secure AVIC feature.
Secure AVIC feature support is determined based on:
- secure_avic module parameter is set.
- X86_FEATURE_SECURE_AVIC CPU Feature bit is set.
- SNP feature is supported.
--- arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm/sev.c | 9 +++++++++ 2 files changed, 10 insertions(+)
arch/x86/include/asm/svm.h +1 -0
@@ -292,6 +292,7 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_#define SVM_SEV_FEAT_RESTRICTED_INJECTION BIT(3)#define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4)#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) +#define SVM_SEV_FEAT_SECURE_AVIC BIT(16)#define SVM_SEV_FEAT_ALLOWED_SEV_FEATURES BIT_ULL(63)#define SVM_SEV_FEAT_INT_INJ_MODES \
arch/x86/kvm/svm/sev.c +9 -0
@@ -58,6 +58,10 @@ static bool sev_es_debug_swap_enabled = true;module_param_named (debug_swap, sev_es_debug_swap_enabled, bool,0444 );static u64 sev_supported_vmsa_features; +/* enable/disable SEV-SNP Secure AVIC support */ +bool sev_snp_savic_enabled = true; +module_param_named (secure_avic, sev_snp_savic_enabled, bool,0444 ); +#define AP_RESET_HOLD_NONE 0#define AP_RESET_HOLD_NAE_EVENT 1#define AP_RESET_HOLD_MSR_PROTO 2
@@ -2962,6 +2966,8 @@ void __init sev_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_SEV_SNP ); kvm_caps.supported_vm_types |=BIT (KVM_X86_SNP_VM ); } +if (sev_snp_savic_enabled) +kvm_cpu_cap_set (X86_FEATURE_SECURE_AVIC ); }void __init sev_hardware_setup (void )
@@ -3082,6 +3088,9 @@ void __init sev_hardware_setup(void) !cpu_feature_enabled(X86_FEATURE_NO_NESTED_DATA_BP)) sev_es_debug_swap_enabled = false; +if (!sev_snp_supported || !cpu_feature_enabled (X86_FEATURE_SECURE_AVIC )) + sev_snp_savic_enabled = false; + sev_supported_vmsa_features =0 ;if (sev_es_debug_swap_enabled) sev_supported_vmsa_features |=SVM_SEV_FEAT_DEBUG_SWAP ;
Signature · 1 line
2.34.1
RFC PATCH 6/19 KVM: SVM: Initialize apic protected state for SAVIC guests
In reply to Neeraj UpadhyayInitialize lapic's guest_apic_protected for Secure AVIC guests. This is only an initialization commit and actual support for creating Secure AVIC enabled guests and injecting interrupts would be added in later commits.
--- arch/x86/kvm/svm/svm.c | 3 +++ arch/x86/kvm/svm/svm.h | 5 +++++ 2 files changed, 8 insertions(+)
arch/x86/kvm/svm/svm.c +3 -0
@@ -1442,6 +1442,9 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)if (!vmcb01_page) goto out; +if (sev_savic_active (vcpu->kvm)) + vcpu->arch.apic->guest_apic_protected =APIC_STATE_PROTECTED_INJECTED_INTR ; +if (sev_es_guest (vcpu->kvm)) {/* * SEV-ES guests require a separate VMSA page used to contain
arch/x86/kvm/svm/svm.h +5 -0
@@ -756,6 +756,10 @@ 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); +static inline bool sev_savic_active (struct kvm *kvm) +{ +return to_kvm_sev_info (kvm)->vmsa_features &SVM_SEV_FEAT_SECURE_AVIC ; +}#else static inline struct page *snp_safe_alloc_page_node (int node,gfp_t gfp) {
@@ -786,6 +790,7 @@ static inline int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) {return 0 ; } +static inline bool sev_savic_active (struct kvm *kvm) {return false; }#endif
Signature · 1 line
2.34.1
RFC PATCH 7/19 KVM: SEV: Do not intercept SECURE_AVIC_CONTROL MSR
In reply to Neeraj UpadhyayFrom: Kishon Vijay Abraham I <[email protected]>
The SECURE_AVIC_CONTROL MSR (0xc0010138) holds the GPA of the APIC backing page and bitfields to enable Secure AVIC and NMI. This MSR is populated by the guest and the hypervisor should not intercept it so that the guest can properly set the MSR. Disable intercepting the SECURE_AVIC_CONTROL MSR for Secure AVIC enabled guests.
--- arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/kvm/svm/sev.c | 3 +++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 +- 4 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +2 -0
@@ -683,6 +683,8 @@#define MSR_AMD64_SNP_RESV_BIT 18#define MSR_AMD64_SNP_RESERVED_MASK GENMASK_ULL(63, MSR_AMD64_SNP_RESV_BIT) +#define MSR_AMD64_SECURE_AVIC_CONTROL 0xc0010138 +#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f#define MSR_AMD64_RMP_BASE 0xc0010132
arch/x86/kvm/svm/sev.c +3 -0
@@ -4573,6 +4573,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_savic_active (vcpu->kvm)) +set_msr_interception (vcpu, svm->msrpm,MSR_AMD64_SECURE_AVIC_CONTROL ,1 ,1 ); }void sev_init_vmcb (struct vcpu_svm *svm)
arch/x86/kvm/svm/svm.c +1 -0
@@ -110,6 +110,7 @@ static const struct svm_direct_access_msrs { { .index = MSR_EFER, .always = false }, { .index = MSR_IA32_CR_PAT, .always = false }, { .index = MSR_AMD64_SEV_ES_GHCB, .always = true }, + { .index = MSR_AMD64_SECURE_AVIC_CONTROL, .always = false }, { .index = MSR_TSC_AUX, .always = false }, { .index = X2APIC_MSR(APIC_ID), .always = false }, { .index = X2APIC_MSR(APIC_LVR), .always = false },
arch/x86/kvm/svm/svm.h +1 -1
@@ -44,7 +44,7 @@ static inline struct page *__sme_pa_to_page(unsigned long pa)#define IOPM_SIZE PAGE_SIZE * 3#define MSRPM_SIZE PAGE_SIZE * 2 -#define MAX_DIRECT_ACCESS_MSRS 48 +#define MAX_DIRECT_ACCESS_MSRS 49#define MSRPM_OFFSETS 32extern u32 msrpm_offsets[MSRPM_OFFSETS ]__read_mostly ;extern bool npt_enabled;
Signature · 1 line
2.34.1
RFC PATCH 8/19 KVM: SVM/SEV/X86: Secure AVIC: Add support to inject interrupts
In reply to Neeraj UpadhyayAdd support to inject interrupts from emulated devices/para-virtualized devices for Secure AVIC guests. Secure AVIC requires RequestedIRR and UpdateIRR VMCB fields to be set before VMRUN for interrupt injection.
Secure AVIC allows multiple interrupts to be injected at the same time. So on interrupt injection, the entire contents of APIC_IRR in host APIC backing page are copied to RequestedIRR. As guest PPR state is not visible in KVM, all pending interrupts in host APIC_IRR are considered as injectable. Secure AVIC HW handles re-injection itself so no explicit handling by KVM is required.
Secure AVIC does not require an IRQ window as hardware manages interrupt delivery to the guest and can detect if the guest is in a state to accept interrupts. So, short-circuit interrupt_allowed() and enable_irq_window() ops for Secure AVIC.
--- arch/x86/include/asm/svm.h | 8 ++++++-- arch/x86/kvm/irq.c | 3 +++ arch/x86/kvm/lapic.c | 13 +++++++++---- arch/x86/kvm/svm/sev.c | 39 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 13 +++++++++++++ arch/x86/kvm/svm/svm.h | 5 ++++- arch/x86/kvm/x86.c | 12 +++++++++++- 7 files changed, 85 insertions(+), 8 deletions(-)
arch/x86/include/asm/svm.h +6 -2
@@ -159,9 +159,13 @@ struct __attribute__ ((__packed__)) vmcb_control_area {u64 avic_physical_id;/* Offset 0xf8 */ u8 reserved_7[8 ];u64 vmsa_pa;/* Used for an SEV-ES guest */ -u8 reserved_8[40 ]; +u8 reserved_8[36 ]; +u8 update_irr;/* Offset 0x134 */ +u8 reserved_9[3 ];u64 allowed_sev_features;/* Offset 0x138 */ -u8 reserved_9[672 ]; +u8 reserved_10[16 ]; +u32 requested_irr[8 ];/* Offset 0x150 */ +u8 reserved_11[624 ];/* * Offset 0x3e0, 32 bytes reserved * for use by hypervisor/software.
arch/x86/kvm/irq.c +3 -0
@@ -47,6 +47,9 @@ static int pending_userspace_extint(struct kvm_vcpu *v)*/ int kvm_cpu_has_extint (struct kvm_vcpu *v) { +if (v->arch.apic->guest_apic_protected) +return 0 ; +/* * FIXME: interrupt.injected represents an interrupt whose * side-effects have already been applied (e.g. bit from IRR
arch/x86/kvm/lapic.c +9 -4
@@ -2975,11 +2975,16 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)if (!kvm_apic_present (vcpu))return -1 ; -if (apic->guest_apic_protected) +switch (apic->guest_apic_protected) { +case APIC_STATE_PROTECTED_POSTED_INTR :return -1 ; - -__apic_update_ppr (apic, &ppr); -return apic_has_interrupt_for_ppr (apic, ppr); +case APIC_STATE_PROTECTED_INJECTED_INTR : +return apic_search_irr (apic); +case APIC_STATE_UNPROTECTED : +default : +__apic_update_ppr (apic, &ppr); +return apic_has_interrupt_for_ppr (apic, ppr); + } }EXPORT_SYMBOL_GPL (kvm_apic_has_interrupt);
arch/x86/kvm/svm/sev.c +39 -0
@@ -34,6 +34,7 @@#include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "lapic.h" #define GHCB_VERSION_MAX 2ULL#define GHCB_VERSION_DEFAULT 2ULL
@@ -4986,3 +4987,41 @@ int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn)return level; } + +void sev_savic_set_requested_irr (struct vcpu_svm *svm,bool reinjected) +{ +struct kvm_lapic *apic; +bool has_interrupts; +int vec, vec_pos; +u32 val; +int i; + +/* Secure AVIC HW takes care of re-injection */ +if (reinjected) +return ; + + apic = svm->vcpu.arch.apic; + has_interrupts = false; + +for (i =0 ; i <ARRAY_SIZE (svm->vmcb->control.requested_irr); i++) { + val =__kvm_lapic_get_reg (apic->regs,APIC_IRR + i *0x10 ); +if (!val) +continue ; + has_interrupts = true; + svm->vmcb->control.requested_irr[i] |= val; +do { + vec_pos =__ffs (val); + vec = (i <<5 ) + vec_pos; +kvm_lapic_clear_vector (vec, apic->regs +APIC_IRR ); + val = val & ~BIT (vec_pos); + }while (val); + } + +if (has_interrupts) + svm->vmcb->control.update_irr |=BIT (0 ); +} + +bool sev_savic_has_pending_interrupt (struct kvm_vcpu *vcpu) +{ +return kvm_apic_has_interrupt (vcpu) !=-1 ; +}
arch/x86/kvm/svm/svm.c +13 -0
@@ -50,6 +50,8 @@#include "svm.h" #include "svm_ops.h" +#include "lapic.h" +#include "kvm_onhyperv.h" #include "svm_onhyperv.h"
@@ -3679,6 +3681,9 @@ static void svm_inject_irq(struct kvm_vcpu *vcpu, bool reinjected)struct vcpu_svm *svm =to_svm (vcpu);u32 type; +if (sev_savic_active (vcpu->kvm)) +return sev_savic_set_requested_irr (svm, reinjected); +if (vcpu->arch.interrupt.soft) {if (svm_update_soft_interrupt_rip (vcpu))return ;
@@ -3860,6 +3865,9 @@ static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) {struct vcpu_svm *svm =to_svm (vcpu); +if (sev_savic_active (vcpu->kvm)) +return 1 ; +if (svm->nested.nested_run_pending)return -EBUSY ;
@@ -3880,6 +3888,9 @@ static void svm_enable_irq_window(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); +if (sev_savic_active (vcpu->kvm)) +return ; +/* * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes * 1, because that's a separate STGI/VMRUN intercept. The next time we
@@ -5092,6 +5103,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .apicv_post_state_restore = avic_apicv_post_state_restore, .required_apicv_inhibits = AVIC_REQUIRED_APICV_INHIBITS, + .protected_apic_has_interrupt = sev_savic_has_pending_interrupt, + .get_exit_info = svm_get_exit_info, .get_entry_info = svm_get_entry_info,
arch/x86/kvm/svm/svm.h +4 -1
@@ -760,6 +760,8 @@ static inline bool sev_savic_active(struct kvm *kvm) {return to_kvm_sev_info (kvm)->vmsa_features &SVM_SEV_FEAT_SECURE_AVIC ; } +void sev_savic_set_requested_irr (struct vcpu_svm *svm,bool reinjected); +bool sev_savic_has_pending_interrupt (struct kvm_vcpu *vcpu);#else static inline struct page *snp_safe_alloc_page_node (int node,gfp_t gfp) {
@@ -791,7 +793,8 @@ static inline int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) return 0; }static inline bool sev_savic_active (struct kvm *kvm) {return false; } - +static inline void sev_savic_set_requested_irr (struct vcpu_svm *svm,bool reinjected) {} +static inline bool sev_savic_has_pending_interrupt (struct kvm_vcpu *vcpu) {return false; }#endif /* vmenter.S */
arch/x86/kvm/x86.c +11 -1
@@ -10426,7 +10426,17 @@ static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu,if (r <0 ) goto out;if (r) { -int irq =kvm_cpu_get_interrupt (vcpu); +int irq; + +/* +* Do not ack the interrupt here for APIC_STATE_PROTECTED_INJECTED_INTR. +* ->inject_irq reads the APIC_IRR state and clears it. +*/ +if (vcpu->arch.apic->guest_apic_protected == +APIC_STATE_PROTECTED_INJECTED_INTR ) + irq =kvm_apic_has_interrupt (vcpu); +else + irq =kvm_cpu_get_interrupt (vcpu);if (!WARN_ON_ONCE (irq ==-1 )) {kvm_queue_interrupt (vcpu, irq, false);
Signature · 1 line
2.34.1
RFC PATCH 9/19 KVM: SVM/SEV/X86: Secure AVIC: Add hypervisor side IPI Delivery Support
In reply to Neeraj UpadhyaySecure AVIC hardware only accelerates self IPIs. For cross-vCPU IPI, source vCPU updates the APIC_IRR of destination vCPU and then issues VMGEXIT with type "SVM_EXIT_MSR" to propagate APIC_ICR write to the hypervisor.
Hypervisor then examines the ICR data and sends doorbell to running vCPUs using AVIC Doorbell MSR or wakes up a blocking vCPU. Hypervisor then resumes the vCPU which issued the VMGEXIT.
--- arch/x86/kvm/svm/sev.c | 216 ++++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 217 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +215 -1
@@ -3496,6 +3496,89 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu)struct svm_cpu_data *sd =per_cpu_ptr (&svm_data, cpu);unsigned int asid =sev_get_asid (svm->vcpu.kvm); +/* +* It should be safe to clear sev_savic_has_pending_ipi here. +* +* Following are the scenarios possible: +* +* Scenario 1: sev_savic_has_pending_ipi is set before hlt exit of the +* target vCPU. +* +* Source vCPU Target vCPU +* +* 1. Set APIC_IRR of target +* vCPU. +* +* 2. VMGEXIT +* +* 3. Set ...has_pending_ipi +* +* savic_handle_icr_write() +* ..._has_pending_ipi = true +* +* 4. avic_ring_doorbell() +* - VS - +* +* 4. VMEXIT +* +* 5. ..._has_pending_ipi = false +* +* 6. VM entry +* +* 7. hlt exit +* +* In this case, any VM exit taken by target vCPU before hlt exit +* clears sev_savic_has_pending_ipi. On hlt exit, idle halt intercept +* would find the V_INTR set and skip hlt exit. +* +* Scenario 2: sev_savic_has_pending_ipi is set when target vCPU +* has taken hlt exit. +* +* Source vCPU Target vCPU +* +* 1. hlt exit +* +* 2. Set ...has_pending_ipi +* 3. kvm_vcpu_has_events() returns true +* and VM is reentered. +* +* vcpu_block() +* kvm_arch_vcpu_runnable() +* kvm_vcpu_has_events() +* <return true as ..._has_pending_ipi +* is set> +* +* 4. On VM entry, APIC_IRR state is re-evaluated +* and V_INTR is set and interrupt is delivered +* to vCPU. +* +* +* Scenario 3: sev_savic_has_pending_ipi is set while halt exit is happening: +* +* +* Source vCPU Target vCPU +* +* 1. hlt +* Hardware check V_INTR to determine +* if hlt exit need to be taken. No other +* exit such as intr exit can be taken +* while this sequence is being executed. +* +* 2. Set APIC_IRR of target vCPU. +* +* 3. Set ...has_pending_ipi +* 4. hlt exit taken. +* +* 5. ...has_pending_ipi being set is observed +* by target vCPU and the vCPU is resumed. +* +* In this scenario, hardware ensures that target vCPU does not take any exit +* between checking V_INTR state and halt exit. So, sev_savic_has_pending_ipi +* remains set when vCPU takes hlt exit. +*/ +if (READ_ONCE (svm->sev_savic_has_pending_ipi)) +WRITE_ONCE (svm->sev_savic_has_pending_ipi, false); +/* Assign the asid allocated with this SEV guest */ svm->asid = asid;
@@ -4303,6 +4386,129 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)return 0 ; } +static void savic_handle_icr_write (struct kvm_vcpu *kvm_vcpu,u64 icr) +{ +struct kvm *kvm = kvm_vcpu->kvm; +struct kvm_vcpu *vcpu; +u32 icr_low, icr_high; +bool in_guest_mode; +unsigned long i; + + icr_low =lower_32_bits (icr); + icr_high =upper_32_bits (icr); + +/* +* TODO: Instead of scanning all the vCPUS, get fastpath working which should +* look similar to avic_kick_target_vcpus_fast(). +*/ +kvm_for_each_vcpu (i, vcpu, kvm) { +if (!kvm_apic_match_dest (vcpu, kvm_vcpu->arch.apic, icr_low &APIC_SHORT_MASK , + icr_high, icr_low &APIC_DEST_MASK )) +continue ; + +/* +* Setting sev_savic_has_pending_ipi could result in a spurious +* wakeup from hlt (as kvm_cpu_has_interrupt() would return true) +* if destination CPU is inside guest and guest does a halt exit +* after handling the IPI. sev_savic_has_pending_ipi gets cleared +* on vm entry, so there can be at most one spurious wakeup per IPI. +* For vcpu->mode == IN_GUEST_MODE, sev_savic_has_pending_ipi need +* to be set to handle the case where destination vCPU has taken +* halt exit and the source CPU has not observed vcpu->mode != +* IN_GUEST_MODE. +*/ +WRITE_ONCE (to_svm (vcpu)->sev_savic_has_pending_ipi, true); +/* Order sev_savic_has_pending_ipi write and vcpu->mode read. */ +smp_mb (); +/* Pairs with smp_store_release in vcpu_enter_guest. */ + in_guest_mode = (smp_load_acquire (&vcpu->mode) ==IN_GUEST_MODE ); +if (in_guest_mode) { +/* +* Signal the doorbell to tell hardware to inject the IRQ. +* +* If the vCPU exits the guest before the doorbell chimes, +* below memory ordering guarantees that destination vCPU +* observes sev_savic_has_pending_ipi == true before +* blocking. +* +* Src-CPU Dest-CPU +* +* savic_handle_icr_write() +* sev_savic_has_pending_ipi = true +* smp_mb() +* smp_load_acquire(&vcpu->mode) +* +* - VS - +* vcpu->mode = OUTSIDE_GUEST_MODE +* __kvm_emulate_halt() +* kvm_cpu_has_interrupt() +* smp_mb() +* if (sev_savic_has_pending_ipi) +* return true; +* +* [S1] +* sev_savic_has_pending_ipi = true +* +* SMP_MB +* +* [L1] +* vcpu->mode +* [S2] +* vcpu->mode = OUTSIDE_GUEST_MODE +* +* +* SMP_MB +* +* [L2] sev_savic_has_pending_ipi == true +* +* exists (L1=IN_GUEST_MODE /\ L2=false) +* +* Above condition does not exit. So, if source CPU observes vcpu->mode +* = IN_GUEST_MODE (L1), sev_savic_has_pending_ipi load by destination CPU +* (L2) should observe the store (S1) from source CPU. +*/ +avic_ring_doorbell (vcpu); + }else { +/* +* Wake the vCPU if it was blocking. +* +* Memory ordering is provided by smp_mb() in rcuwait_wake_up() on +* source CPU and smp_mb() in set_current_state() inside +* kvm_vcpu_block() on dest CPU. +*/ +kvm_vcpu_kick (vcpu); + } + } +} + +static bool savic_handle_msr_exit (struct kvm_vcpu *vcpu) +{ +u32 msr, reg; + + msr =kvm_rcx_read (vcpu); + reg = (msr -APIC_BASE_MSR ) <<4 ; + +switch (reg) { +case APIC_ICR : +/* +* Only APIC_ICR wrmsr requires special handling +* for Secure AVIC guests to wake up destination +* vCPUs. +*/ +if (to_svm (vcpu)->vmcb->control.exit_info_1) { +u64 data =kvm_read_edx_eax (vcpu); + +savic_handle_icr_write (vcpu, data); +return true; + } +break ; +default : +break ; + } + +return false; +} +int sev_handle_vmgexit (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -4445,6 +4651,11 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) control->exit_info_1, control->exit_info_2); ret = -EINVAL ;break ; +case SVM_EXIT_MSR : +if (sev_savic_active (vcpu->kvm) &&savic_handle_msr_exit (vcpu)) +return 1 ; + + fallthrough;default : ret =svm_invoke_exit_handler (vcpu, exit_code); }
@@ -5023,5 +5234,8 @@ void sev_savic_set_requested_irr(struct vcpu_svm *svm, bool reinjected)bool sev_savic_has_pending_interrupt (struct kvm_vcpu *vcpu) { -return kvm_apic_has_interrupt (vcpu) !=-1 ; +/* See memory ordering description in savic_handle_icr_write(). */ +smp_mb (); +return READ_ONCE (to_svm (vcpu)->sev_savic_has_pending_ipi) || +kvm_apic_has_interrupt (vcpu) !=-1 ; }
arch/x86/kvm/svm/svm.h +2 -0
@@ -327,6 +327,8 @@ struct vcpu_svm {/* Guest GIF value, used when vGIF is not enabled */ bool guest_gif; + +bool sev_savic_has_pending_ipi; };struct svm_cpu_data {
Signature · 1 line
2.34.1
RFC PATCH 10/19 KVM: SVM: Secure AVIC: Do not inject "Exceptions" for Secure AVIC
In reply to Neeraj UpadhyayFrom: Kishon Vijay Abraham I <[email protected]>
Secure AVIC does not support injecting "Exceptions" from hypervisor. Return from svm_inject_exception() for Secure AVIC.
HW takes care of delivering exceptions initiated by guest as well as re-injecting exceptions initiated by guest (in case there's an intercept before delivering the exceptions). However exceptions cannot be explicitly injected from Hypervisor when Secure AVIC is enabled.
--- arch/x86/kvm/svm/svm.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/svm/svm.c +3 -0
@@ -463,6 +463,9 @@ static void svm_inject_exception(struct kvm_vcpu *vcpu)struct kvm_queued_exception *ex = &vcpu->arch.exception;struct vcpu_svm *svm =to_svm (vcpu); +if (sev_savic_active (vcpu->kvm)) +return ; +kvm_deliver_exception_payload (vcpu, ex); if (kvm_exception_is_soft(ex->vector) &&
Signature · 1 line
2.34.1
RFC PATCH 11/19 KVM: SVM/SEV: Do not intercept exceptions for Secure AVIC guest
In reply to Neeraj UpadhyayExceptions cannot be explicitly injected from Hypervisor to Secure AVIC guests. So, clear exceptions intercepts so that all exceptions are routed directly to such guests.
--- arch/x86/kvm/svm/sev.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +10 -1
@@ -4786,8 +4786,17 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)set_msr_interception (vcpu, svm->msrpm,MSR_EFER ,1 ,1 );set_msr_interception (vcpu, svm->msrpm,MSR_IA32_CR_PAT ,1 ,1 ); -if (sev_savic_active (vcpu->kvm)) +if (sev_savic_active (vcpu->kvm)) {set_msr_interception (vcpu, svm->msrpm,MSR_AMD64_SECURE_AVIC_CONTROL ,1 ,1 ); + +/* Clear all exception intercepts. */ +clr_exception_intercept (svm,PF_VECTOR ); +clr_exception_intercept (svm,UD_VECTOR ); +clr_exception_intercept (svm,MC_VECTOR ); +clr_exception_intercept (svm,AC_VECTOR ); +clr_exception_intercept (svm,DB_VECTOR ); +clr_exception_intercept (svm,GP_VECTOR ); + } }void sev_init_vmcb (struct vcpu_svm *svm)
Signature · 1 line
2.34.1
RFC PATCH 12/19 KVM: SVM/SEV: Secure AVIC: Set VGIF in VMSA area
In reply to Neeraj UpadhyayFrom: Kishon Vijay Abraham I <[email protected]>
Secure AVIC requires VGIF in VMSA to be set in order for interrupts to be delivered to the guest. Usually for enabling GIF, VGIF field (bit 9 of VINTR_CTRL in VMCB) is set. However Secure AVIC ignores the VGIF field in VMCB and requires VGIF field in VINTR_CTRL of VMSA to be set.
--- arch/x86/kvm/svm/sev.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/sev.c +2 -0
@@ -854,6 +854,8 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->dr6 = svm->vcpu.arch.dr6; save->sev_features = sev->vmsa_features; +if (sev_savic_active (vcpu->kvm)) + save->vintr_ctrl |=V_GIF_MASK ;/* * Skip FPU and AVX setup with KVM_SEV_ES_INIT to avoid
Signature · 1 line
2.34.1
RFC PATCH 13/19 KVM: SVM/SEV: Secure AVIC: Enable NMI support
In reply to Neeraj UpadhyayFrom: Kishon Vijay Abraham I <[email protected]>
Secure AVIC only allows vNMI flow for injecting NMI interrupts to the guest. Also, Secure AVIC hardware manages NMI delivery to the guest and can detect if the guest is in a state to accept NMI. So, update NMI injection code flow for Secure AVIC to inject V_NMI and allow NMI injection if there is no V_NMI pending. In addition, Secure AVIC requires V_NMI_ENABLE in VINTR_CTRL field of VMSA to be set. Set V_NMI_ENABLE in VINTR_CTRL field of VMSA.
--- arch/x86/kvm/svm/sev.c | 2 +- arch/x86/kvm/svm/svm.c | 56 ++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 22 deletions(-)
arch/x86/kvm/svm/sev.c +1 -1
@@ -855,7 +855,7 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->sev_features = sev->vmsa_features;if (sev_savic_active (vcpu->kvm)) - save->vintr_ctrl |=V_GIF_MASK ; + save->vintr_ctrl |=V_GIF_MASK |V_NMI_ENABLE_MASK ;/* * Skip FPU and AVX setup with KVM_SEV_ES_INIT to avoid
arch/x86/kvm/svm/svm.c +35 -21
74 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 14/19 KVM: SVM/SEV: Add SVM_VMGEXIT_SECURE_AVIC GHCB protocol event handling
In reply to Neeraj UpadhyayThis GHCB protocol event is used by the guest to notify KVM of the GPA of the APIC backing page being used by a vCPU. The APIC ID parameter is used to identify the vCPU to which the backing page action is related is assigned. An APIC ID value of 0xffff_ffff_ffff_ffff means that the backing page action is for the vCPU performing the call.
Secure AVIC requires the guest vCPU APIC backing page entry to be always present in the guest’s Nested Paginge Table (NPT) while the vCPU is running because some AVIC hardware acceleration sequences may not be restartable when Secure AVIC is enabled. If an access to the guest's APIC backing page by Secure AVIC hardware results in a nested page fault, the BUSY bit in the VMSA is set and subsequent VMRUN fails with a VMEXIT_BUSY error code. VMEXIT_BUSY is unrecoverable in this instance and the vCPU cannot be resumed post this event.
Two actions are available to the guest to notify KVM of these pages.
• SVM_VMGEXIT_SAVIC_REGISTER_BACKING_PAGE (0)
A Secure AVIC guest should use this action to inform KVM of the page-aligned GPA that will be used as the Secure AVIC backing page for the specified vCPU.
To ensure that the backing page NPT entry is present while vCPU is running, KVM does a PSMASH for the GPA if the corresponding NPT entry is of size 2M. Without PSMASH, it is possible for other allocations to be part of the same 2M page as the APIC backing page and any modifications (page state change from private to shared) to any one of those allocations would result in splitting the 2M page to 4K pages. This would result in zapping the 2M PTE while APIC backing page is potentially being accessed by Secure AVIC hardware.
Setting a Secure AVIC backing page GPA automatically clears any currently set Secure AVIC backing page GPA.
• SVM_VMGEXIT_SAVIC_UNREGISTER_BACKING_PAGE (1)
A guest may use this action to inform KVM that the previously set GPA is no longer being used as the Secure AVIC backing page for the specified vCPU. This removes the requirement on KVM to ensure that the specified GPA is always present in the NPT of the guest while the specified vCPU is running. KVM returns the GPA that was currently SET or 0 if there was no previously set GPA.
---
Initial GHCB draft spec for the new GHCB event for Secure AVIC is at:
https://lore.kernel.org/linux-coco/[email protected]/T/#u
The GHCB event has been updated to pass the action param for GPA register/unregister. The new GHCB spec will be published soon. I will share the link to the updated spec once it is available publically.
arch/x86/include/uapi/asm/svm.h | 3 ++ arch/x86/kvm/svm/sev.c | 58 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 62 insertions(+)
arch/x86/include/uapi/asm/svm.h +3 -0
@@ -117,6 +117,9 @@#define SVM_VMGEXIT_AP_CREATE 1#define SVM_VMGEXIT_AP_DESTROY 2#define SVM_VMGEXIT_SNP_RUN_VMPL 0x80000018 +#define SVM_VMGEXIT_SECURE_AVIC 0x8000001a +#define SVM_VMGEXIT_SAVIC_REGISTER_BACKING_PAGE 0 +#define SVM_VMGEXIT_SAVIC_UNREGISTER_BACKING_PAGE 1#define SVM_VMGEXIT_HV_FEATURES 0x8000fffd#define SVM_VMGEXIT_TERM_REQUEST 0x8000fffe#define SVM_VMGEXIT_TERM_REASON (reason_set, reason_code) \
arch/x86/kvm/svm/sev.c +58 -0
76 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +1 -0
@@ -329,6 +329,7 @@ struct vcpu_svm {bool guest_gif;bool sev_savic_has_pending_ipi; +gpa_t sev_savic_gpa; };struct svm_cpu_data {
Signature · 1 line
2.34.1
RFC PATCH 15/19 KVM: x86: Secure AVIC: Indicate APIC is enabled by guest SW _always_
In reply to Neeraj UpadhyayFrom: Kishon Vijay Abraham I <[email protected]>
Guest SW indicates APIC is enabled by writing to APIC_SPIV ( Bit 8: APIC Software Enable). However in the case of Secure AVIC, APIC_SPIV is not propagated to hypervisor as Secure AVIC HW itself can detect whether the Guest SW has enabled APIC or not and handle accordingly.
To handle this case where the HW handles APIC SW Enable in the hypervisor, always return 'true' from kvm_apic_sw_enabled() if Secure AVIC is active. This would let hypervisor assume Guest SW has always enabled APIC and prevent it from taking actions it usually does when Guest SW has not enabled APIC.
This is especially used when accepting interrupts to be injected to the Guest and for injecting LAPIC timer interrupts.
--- arch/x86/kvm/lapic.h | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/lapic.h +3 -0
@@ -206,6 +206,9 @@ extern struct static_key_false_deferred apic_sw_disabled;static inline bool kvm_apic_sw_enabled (struct kvm_lapic *apic) { +if (apic->guest_apic_protected ==APIC_STATE_PROTECTED_INJECTED_INTR ) +return true; +if (static_branch_unlikely (&apic_sw_disabled.key))return apic->sw_enabled;return true;
Signature · 1 line
2.34.1
RFC PATCH 16/19 KVM: x86: Secure AVIC: Add IOAPIC EOI support for level interrupts
In reply to Neeraj UpadhyaySecure AVIC accelerates EOI writes for edge-triggered interrupts. For level-triggered interrupts, EOI msr write is forwarded to hypervisor. Handle APIC_EOI msr write VMGEXIT and propagate EOI writes to IOAPIC. Current implementation reuses unused host APIC_ISR regs space to maintain information about active level-triggered interrupts. As host APIC_TMR state is updated from IOAPIC redirect entry, host APIC_TMR is used to identify level-triggered IOAPIC interrupts.
--- arch/x86/kvm/lapic.h | 5 +++++ arch/x86/kvm/svm/sev.c | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
arch/x86/kvm/lapic.h +5 -0
@@ -159,6 +159,11 @@ static inline void kvm_lapic_clear_vector(int vec, void *bitmap)clear_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); } +static inline int kvm_lapic_test_vector (int vec,void *bitmap) +{ +return test_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); +} +static inline void kvm_lapic_set_vector (int vec,void *bitmap) {set_bit (VEC_POS (vec), (bitmap) +REG_POS (vec));
arch/x86/kvm/svm/sev.c +26 -0
@@ -4491,9 +4491,27 @@ static void savic_handle_icr_write(struct kvm_vcpu *kvm_vcpu, u64 icr) } } +static int find_highest_isr (struct kvm_lapic *apic) +{ +int vec_per_reg =32 ; +int max_vec =256 ; +u32 *reg; +int vec; + +for (vec = max_vec -32 ; vec >=0 ; vec -= vec_per_reg) { + reg = apic->regs +APIC_ISR +REG_POS (vec); +if (*reg) +return __fls (*reg) + vec; + } + +return -1 ; +} +static bool savic_handle_msr_exit (struct kvm_vcpu *vcpu) { +struct kvm_lapic *apic;u32 msr, reg; +int vec; msr =kvm_rcx_read (vcpu); reg = (msr -APIC_BASE_MSR ) <<4 ;
@@ -4512,6 +4530,12 @@ static bool savic_handle_msr_exit(struct kvm_vcpu *vcpu)return true; }break ; +case APIC_EOI : + apic = vcpu->arch.apic; + vec =find_highest_isr (apic); +kvm_lapic_clear_vector (vec, apic->regs +APIC_ISR ); +kvm_apic_set_eoi_accelerated (vcpu, vec); +return true;default :break ; }
@@ -5294,6 +5318,8 @@ void sev_savic_set_requested_irr(struct vcpu_svm *svm, bool reinjected) vec = (i <<5 ) + vec_pos;kvm_lapic_clear_vector (vec, apic->regs +APIC_IRR ); val = val & ~BIT (vec_pos); +if (kvm_lapic_test_vector (vec, apic->regs +APIC_TMR )) +kvm_lapic_set_vector (vec, apic->regs +APIC_ISR ); }while (val); }
Signature · 1 line
2.34.1
RFC PATCH 17/19 KVM: x86/ioapic: Disable RTC_GSI EOI tracking for protected APIC
In reply to Neeraj UpadhyayDisable RTC_GSI pending EOI tracking for guests with protected APIC state such as Secure AVIC. RTC_GSI is a edge-triggered vector and Secure AVIC accelerates EOI write for edge-triggered interrupts. In addition, APIC_ISR state is not visible within KVM and is part of guest-owned backing page. So, lazy pending EOI checks also cannot be done. So, disable tracking of pending EOI. This means coalesced RTC interrupts cannot be detected by userspace for Secure AVIC guests.
--- arch/x86/kvm/ioapic.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/ioapic.c +7 -1
@@ -113,6 +113,9 @@ static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)struct dest_map *dest_map = &ioapic->rtc_status.dest_map;union kvm_ioapic_redirect_entry *e; +if (vcpu->arch.apic->guest_apic_protected) +return ; + e = &ioapic->redirtbl[RTC_GSI ]; if (!kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT, e->fields.dest_id,
@@ -443,6 +446,7 @@ static int ioapic_service(struct kvm_ioapic *ioapic, int irq, bool line_status) {union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];struct kvm_lapic_irq irqe; +struct kvm_vcpu *vcpu;int ret; if (entry->fields.mask ||
@@ -472,7 +476,9 @@ static int ioapic_service(struct kvm_ioapic *ioapic, int irq, bool line_status)BUG_ON (ioapic->rtc_status.pending_eoi !=0 ); ret =kvm_irq_delivery_to_apic (ioapic->kvm,NULL , &irqe, &ioapic->rtc_status.dest_map); - ioapic->rtc_status.pending_eoi = (ret <0 ?0 : ret); + vcpu =kvm_get_vcpu (ioapic->kvm,0 ); +if (!vcpu->arch.apic->guest_apic_protected) + ioapic->rtc_status.pending_eoi = (ret <0 ?0 : ret); }else ret =kvm_irq_delivery_to_apic (ioapic->kvm,NULL , &irqe,NULL );
Signature · 1 line
2.34.1
RFC PATCH 18/19 X86: SVM: Check injected vectors before waiting for timer expiry
In reply to Neeraj UpadhyayFor Secure AVIC guests, call kvm_wait_lapic_expire() only when the vector for LVTT is set in requested_IRR.
KVM always assumes a timer IRQ was injected if APIC state is protected. For Secure AVIC guests, check to-be-injected vectors in the requested_IRR to avoid unnecessary wait calls.
--- arch/x86/kvm/svm/sev.c | 8 ++++++++ arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/svm/svm.h | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +8 -0
@@ -5334,3 +5334,11 @@ bool sev_savic_has_pending_interrupt(struct kvm_vcpu *vcpu)return READ_ONCE (to_svm (vcpu)->sev_savic_has_pending_ipi) ||kvm_apic_has_interrupt (vcpu) !=-1 ; } + +bool sev_savic_timer_int_injected (struct kvm_vcpu *vcpu) +{ +u32 reg =kvm_lapic_get_reg (vcpu->arch.apic,APIC_LVTT ); +int vec = reg &APIC_VECTOR_MASK ; + +return to_svm (vcpu)->vmcb->control.requested_irr[vec /32 ] &BIT (vec %32 ); +}
arch/x86/kvm/svm/svm.c +2 -1
@@ -4292,7 +4292,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu,clgi ();kvm_load_guest_xsave_state (vcpu); -kvm_wait_lapic_expire (vcpu); +if (!sev_savic_active (vcpu->kvm) ||sev_savic_timer_int_injected (vcpu)) +kvm_wait_lapic_expire (vcpu);/* * If this vCPU has touched SPEC_CTRL, restore the guest's value if
arch/x86/kvm/svm/svm.h +2 -0
@@ -765,6 +765,7 @@ static inline bool sev_savic_active(struct kvm *kvm) }void sev_savic_set_requested_irr (struct vcpu_svm *svm,bool reinjected);bool sev_savic_has_pending_interrupt (struct kvm_vcpu *vcpu); +bool sev_savic_timer_int_injected (struct kvm_vcpu *vcpu);#else static inline struct page *snp_safe_alloc_page_node (int node,gfp_t gfp) {
@@ -798,6 +799,7 @@ static inline int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn)static inline bool sev_savic_active (struct kvm *kvm) {return false; }static inline void sev_savic_set_requested_irr (struct vcpu_svm *svm,bool reinjected) {}static inline bool sev_savic_has_pending_interrupt (struct kvm_vcpu *vcpu) {return false; } +static inline bool sev_savic_timer_int_injected (struct kvm_vcpu *vcpu) {return true; }#endif /* vmenter.S */
Signature · 1 line
2.34.1
RFC PATCH 19/19 KVM: SVM/SEV: Allow creating VMs with Secure AVIC enabled
In reply to Neeraj UpadhyayNow that all the initialization required to enable secure AVIC is done, set supported_vmsa_features to allow creating Secure AVIC VMs.
--- arch/x86/kvm/svm/sev.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/svm/sev.c +3 -0
@@ -3100,6 +3100,9 @@ void __init sev_hardware_setup(void)if (sev_es_enabled &&cpu_feature_enabled (X86_FEATURE_ALLOWED_SEV_FEATURES )) sev_supported_vmsa_features |=SVM_SEV_FEAT_ALLOWED_SEV_FEATURES ; + +if (sev_snp_savic_enabled) + sev_supported_vmsa_features |=SVM_SEV_FEAT_SECURE_AVIC ; }void sev_hardware_unsetup (void )
Signature · 1 line
2.34.1