KVM: SEV: Add PMC virtualization and filtering for confidential guests
cover KVM: SEV: Add PMC virtualization and filtering for confidential guests
The hardware-switched mediated PMU series [1] disallows PMC virtualization for SEV-ES and SEV-SNP guests: their event-selects live in the encrypted VMSA, so KVM can neither read them nor intercept their writes, and couldn't apply the PMU event filter to them at all.
Hardware-assisted PMC filtering [2] closes that gap. Hardware traps a guest write that enables an event missing from the VMCB's allow list, and VMRUN fails if an enabled counter's event isn't listed, so an SEV-ES or SEV-SNP guest can't count an event KVM hasn't approved.
This RFC proposes a new GHCB protocol through which the guest shares its event-selects with KVM, so that KVM can keep the guest from counting events the host doesn't allow. Comments on the protocol design are welcome.
Overview
--------
Patches 1, 2 and 4 add definitions used by both the guest and the host:
the SevPerfCtrVirt feature bit, the VMSA fields for PMC virtualization, and the proposed GHCB PMC-filter protocol.
Guest patches:
- Patch 3 adds PMC virtualization support for SEV-SNP guests, and seeds PerfCntrGlobalCtl in the VMSAs of SEV-SNP APs.
- Patch 5 forwards trapped event-select writes to the hypervisor.
Host (KVM) patches:
- Patch 6 adds the hardware-switched PMU for SEV-ES and SEV-SNP guests.
- Patch 7 filters the event-selects these guests forward.
- Patch 8 fails PMU event filter changes once the VM has vCPUs.
- Patch 9 advertises PMC virtualization only when hardware-assisted PMC filtering is enabled.
Design
------
- KVM can neither read an SEV-ES or SEV-SNP guest's event-selects nor intercept their writes. On a trapped write, the guest's #VC handler forwards the write with SVM_VMGEXIT_PMC_FILTER, RCX and EDX:EAX holding the write, and the scratch buffer every event-select. KVM emulates the write and runs the reprogramming synchronously, and returns the filtered values in EDX:EAX and the buffer. The guest programs them, honoring only bits being cleared.
- KVM adds an event to the allow list only when its filter allows it. When the list is full, KVM rebuilds it from the enabled event-selects in the buffer. As these are guest-supplied, only events the filter allows are listed. A guest that lies about them can only make its own VMRUN fail.
- SVM_SEV_FEAT_PERFCTR_VIRT is advertised for SEV-ES and SEV-SNP guests, whose kernel discovers PMC virtualization through SEV_STATUS bit 13, and hardware-assisted PMC filtering is supported.
Limitations
-----------
- Support for PMC virtualization for SEV-ES and SEV-SNP guests depends on PMC filtering.
Testing
-------
- SEV, SEV-ES and SEV-SNP guests with perfctr-virt and hardware-assisted PMC filtering, with a PMU event filter denying events 0xC0 and 0xC2: denied events never count and allowed events do.
Questions for discussion
------------------------
- For SEV-ES and SEV-SNP guests, KVM reprograms the counters right away in the SVM_VMGEXIT_PMC_FILTER handler, instead of just before VM-entry as for other guests. Is that OK, or is there a better place to do it?
- KVM can't see the event-selects of an SEV-ES or SEV-SNP guest, so it can't apply a new filter to counters that are already set up. So KVM_SET_PMU_EVENT_FILTER fails with EBUSY once the VM has vCPUs. The filter can only be set before vCPUs are created. Is this acceptable?
These patches are based on
branch: kvm-x86/next (https://github.com/kvm-x86/linux.git) Base commit: 6cf46b37b0356 (kvm-x86-next-2026.08.07)
+ hardware-switched PMU series [1]
+ hardware-assisted PMC filtering series [2]
[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/T/
Manali Shukla (5):
x86/sev: Define the GHCB PMC-filter protocol
x86/sev: Forward trapped PMC event-select writes to the hypervisor
KVM: SEV: Filter PMC event-selects forwarded by SEV-ES and SNP guests
KVM: x86/pmu: Disallow PMU event filter changes for protected guests
KVM: SEV: Enable hardware-assisted PMC filtering for SEV-ES and SNP
guests
Sandipan Das (4):
x86/cpufeatures: Add SevPerfCtrVirt feature bit
KVM: SEV: Add VMSA fields for PMC virtualization x86/sev: Add PMC virtualization support for SNP guests
KVM: SEV: Add support for hardware-switched PMU
arch/x86/boot/compressed/sev.c | 5 +- arch/x86/coco/core.c | 3 ++ arch/x86/coco/sev/core.c | 35 ++++++++++++ arch/x86/coco/sev/internal.h | 1 + arch/x86/coco/sev/vc-handle.c | 68 +++++++++++++++++++++++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/msr-index.h | 3 +- arch/x86/include/asm/sev-common.h | 12 +++++ arch/x86/include/asm/svm.h | 13 ++++- arch/x86/include/uapi/asm/svm.h | 2 + arch/x86/kvm/pmu.c | 18 ++++++- arch/x86/kvm/pmu.h | 1 + arch/x86/kvm/svm/pmu.c | 85 ++++++++++++++++++++++++++++-- arch/x86/kvm/svm/sev.c | 83 ++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.c | 18 ++++--- arch/x86/kvm/svm/svm.h | 6 +++ include/linux/cc_platform.h | 8 +++ 17 files changed, 344 insertions(+), 18 deletions(-)
base-commit: 6cf46b37b0356b3408f8ba949e10462cc71b533d
Signature · 1 line
2.43.0
RFC PATCH 1/9 x86/cpufeatures: Add SevPerfCtrVirt feature bit
From: Sandipan Das <[email protected]>
Define a feature flag for bit 20 of CPUID leaf 0x8000001F EAX which indicates support for PMC virtualization in SEV-ES and SEV-SNP guests. This is an extension of X86_FEATURE_PERFCTR_VIRT meant for use with confidential guests that do not want the hypervisor to have visibility into the guest PMU state.
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -467,6 +467,7 @@#define X86_FEATURE_V_TSC_AUX (19*32+ 9)/* Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10)/* hardware-enforced cache coherency */ #define X86_FEATURE_DEBUG_SWAP (19*32+14)/* "debug_swap" SEV-ES full debug state swap support */ +#define X86_FEATURE_SEV_PERFCTR_VIRT (19*32+20)/* SEV-ES PMC virtualization support */ #define X86_FEATURE_RMPREAD (19*32+21)/* RMPREAD instruction */ #define X86_FEATURE_SEGMENTED_RMP (19*32+23)/* Segmented RMP support */ #define X86_FEATURE_ALLOWED_SEV_FEATURES (19*32+27)/* Allowed SEV Features */
Signature · 1 line
2.43.0
RFC PATCH 2/9 KVM: SEV: Add VMSA fields for PMC virtualization
In reply to Manali ShuklaFrom: Sandipan Das <[email protected]>
When PMC virtualization (X86_FEATURE_SEV_PERFCTR_VIRT) is supported and the feature is enabled, additional save slots are available in the VMSA for the following MSRs.
* Performance Counter Global Control (MSR 0xc0000301) (Swap Type C)
* Performance Counter Global Status (MSR 0xc0000300) (Swap Type A)
* Performance Event Select (MSR 0xc0010200..0xc001020a) (Swap Type C)
* Performance Event Counter (MSR 0xc0010201..0xc001020b) (Swap Type C)
Define the additional VMSA fields that will be used by hardware to save and restore the guest PMU state.
--- arch/x86/include/asm/svm.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/include/asm/svm.h +10 -2
@@ -407,7 +407,11 @@ struct sev_es_save_area {u8 cpl;u8 reserved_0xcc[4 ];u64 efer; -u8 reserved_0xd8[104 ]; +u8 reserved_0xd8[8 ]; +struct { +u64 perf_ctl; +u64 perf_ctr; + } __packed pmc[6];u64 xss;u64 cr4;u64 cr3;
@@ -424,7 +428,10 @@ struct sev_es_save_area {u64 dr1_addr_mask;u64 dr2_addr_mask;u64 dr3_addr_mask; -u8 reserved_0x1c0[24 ]; +u8 reserved_0x1c0[8 ]; +u64 perf_cntr_global_status; +u32 perf_cntr_global_control; +u8 reserved_0x1d4[4 ];u64 rsp;u64 s_cet;u64 ssp;
@@ -597,6 +604,7 @@ static inline void __unused_size_checks(void)BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0xcc );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0xd8 );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x1c0 ); +BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x1d4 );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x248 );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x298 );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x300 );
Signature · 1 line
2.43.0
RFC PATCH 3/9 x86/sev: Add PMC virtualization support for SNP guests
In reply to Manali ShuklaFrom: Sandipan Das <[email protected]>
Redefine the previously reserved bit 13 of the SEV_STATUS MSR as MSR_AMD64_SNP_PMC_VIRT, to advertise the availability of PMC virtualization on supported processors. Without this, the guest will refuse to boot if the hypervisor enables PMC virtualization.
Enabling PMC virtualization requires guest-side support since the guest must seed the reset state of PerfCntrGlobalCtl in the VMSA of each AP it brings up. Otherwise the AP's counters come up globally disabled.
The boot processor's VMSA is prepared by the hypervisor, but AP VMSAs are built by the guest. Initialize PerfCntrGlobalCtl for APs in wakeup_cpu_via_vmgexit() with all of the available counters enabled.
[Manali: Cache the number of counters, capped to what the VMSA holds, and
don't build an invalid mask if CPUID reports none]
--- arch/x86/boot/compressed/sev.c | 5 +++-- arch/x86/coco/sev/core.c | 35 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/msr-index.h | 3 ++- 3 files changed, 40 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/sev.c +3 -2
@@ -185,7 +185,7 @@ bool sev_es_check_ghcb_fault(unsigned long address)MSR_AMD64_SNP_SECURE_TSC | \MSR_AMD64_SNP_VMGEXIT_PARAM | \MSR_AMD64_SNP_VMSA_REG_PROT | \ -MSR_AMD64_SNP_RESERVED_BIT13 | \ +MSR_AMD64_SNP_PMC_VIRT | \MSR_AMD64_SNP_RESERVED_BIT15 | \MSR_AMD64_SNP_SECURE_AVIC | \ MSR_AMD64_SNP_RESERVED_BITS19_22 | \
@@ -204,7 +204,8 @@ bool sev_es_check_ghcb_fault(unsigned long address)*/ #define SNP_FEATURES_IMPL (MSR_AMD64_SNP_DEBUG_SWAP | \ MSR_AMD64_SNP_SECURE_TSC | \ - SNP_FEATURE_SECURE_AVIC) + SNP_FEATURE_SECURE_AVIC | \ + MSR_AMD64_SNP_PMC_VIRT)u64 snp_get_unsupported_features (u64 status) {
arch/x86/coco/sev/core.c +35 -0
@@ -44,6 +44,7 @@#include <asm/cpuid/api.h> #include <asm/cmdline.h> #include <asm/msr.h> +#include <asm/perf_event.h> #include "internal.h"
@@ -85,6 +86,7 @@ static const char * const sev_status_feat_names[] = { [MSR_AMD64_SNP_VMPL_SSS_BIT] = "VmplSSS", [MSR_AMD64_SNP_SECURE_TSC_BIT] = "SecureTSC", [MSR_AMD64_SNP_VMGEXIT_PARAM_BIT] = "VMGExitParam", + [MSR_AMD64_SNP_PMC_VIRT_BIT] = "PMCVirt", [MSR_AMD64_SNP_IBS_VIRT_BIT] = "IBSVirt", [MSR_AMD64_SNP_VMSA_REG_PROT_BIT] = "VMSARegProt", [MSR_AMD64_SNP_SMT_PROT_BIT] = "SMTProt",
@@ -754,6 +756,36 @@ static void *snp_alloc_vmsa_page(int cpu)return page_address (p +1 ); } +static unsigned int sev_pmc_nr_gp_counters__ro_after_init ; + +static int __init sev_pmc_init (void ) +{ +union cpuid_0x80000022_ebx ebx; + +if (!(sev_status &MSR_AMD64_SNP_PMC_VIRT )) +return 0 ; + +if (cpu_feature_enabled (X86_FEATURE_PERFMON_V2 )) { + ebx.full =cpuid_ebx (0x80000022 ); + sev_pmc_nr_gp_counters = min_t(unsigned int, ebx.split.num_core_pmc, +AMD64_NUM_COUNTERS_CORE ); + }else if (cpu_feature_enabled (X86_FEATURE_PERFCTR_CORE )) { + sev_pmc_nr_gp_counters =AMD64_NUM_COUNTERS_CORE ; + }else { + sev_pmc_nr_gp_counters =AMD64_NUM_COUNTERS ; + } + +return 0 ; +} +early_initcall (sev_pmc_init); + +static u64 snp_get_perf_global_ctl (void ) +{ +unsigned int nr = sev_pmc_nr_gp_counters; + +return nr ?GENMASK_ULL (nr -1 ,0 ) :0 ; +} +static int wakeup_cpu_via_vmgexit (u32 apic_id,unsigned long start_ip,unsigned int cpu) {struct sev_es_save_area *cur_vmsa, *vmsa;
@@ -849,6 +881,9 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip, unsigned vmsa->vmpl = snp_vmpl; vmsa->sev_features = sev_status >>2 ; +if (sev_status &MSR_AMD64_SNP_PMC_VIRT ) + vmsa->perf_cntr_global_control =snp_get_perf_global_ctl (); +/* Populate AP's TSC scale/offset to get accurate TSC values. */ if (cc_platform_has (CC_ATTR_GUEST_SNP_SECURE_TSC )) { vmsa->tsc_scale = snp_tsc_scale;
arch/x86/include/asm/msr-index.h +2 -1
@@ -735,7 +735,8 @@#define MSR_AMD64_SNP_SECURE_TSC BIT_ULL(MSR_AMD64_SNP_SECURE_TSC_BIT)#define MSR_AMD64_SNP_VMGEXIT_PARAM_BIT 12#define MSR_AMD64_SNP_VMGEXIT_PARAM BIT_ULL(MSR_AMD64_SNP_VMGEXIT_PARAM_BIT) -#define MSR_AMD64_SNP_RESERVED_BIT13 BIT_ULL(13) +#define MSR_AMD64_SNP_PMC_VIRT_BIT 13 +#define MSR_AMD64_SNP_PMC_VIRT BIT_ULL(MSR_AMD64_SNP_PMC_VIRT_BIT)#define MSR_AMD64_SNP_IBS_VIRT_BIT 14#define MSR_AMD64_SNP_IBS_VIRT BIT_ULL(MSR_AMD64_SNP_IBS_VIRT_BIT)#define MSR_AMD64_SNP_RESERVED_BIT15 BIT_ULL(15)
Signature · 1 line
2.43.0
RFC PATCH 4/9 x86/sev: Define the GHCB PMC-filter protocol
In reply to Manali ShuklaWith hardware-assisted PMC filtering, a write that enables an event missing from the hypervisor's allow list raises a #VC in an SEV-ES or SNP guest. The hypervisor can't read the guest's event-selects from the encrypted VMSA, so it can't vet the write on its own.
Add a NAE event for the guest to forward the write: RCX holds the MSR, EDX:EAX the value, and the scratch buffer every event-select. The hypervisor returns the value to program in EDX:EAX, and the value of each event-select in the buffer.
--- arch/x86/include/asm/sev-common.h | 12 ++++++++++++ arch/x86/include/uapi/asm/svm.h | 2 ++ 2 files changed, 14 insertions(+)
arch/x86/include/asm/sev-common.h +12 -0
@@ -176,6 +176,18 @@ struct snp_psc_desc {struct psc_entry entries[VMGEXIT_PSC_MAX_ENTRY ]; } __packed; +/* SVM_VMGEXIT_PMC_FILTER buffer, one entry per event-select. */ +struct perf_ctl_entry { +u64 perf_ctl_idx; +u64 perf_ctl_val; +} __packed; + +struct perf_ctls { +u8 num_counters; +u8 reserved[7 ]; +struct perf_ctl_entry counters[]; +} __packed; +#define GHCB_MSR_TERM_REQ 0x100#define GHCB_MSR_TERM_REASON_SET_POS 12#define GHCB_MSR_TERM_REASON_SET_MASK 0xf
arch/x86/include/uapi/asm/svm.h +2 -0
@@ -122,6 +122,7 @@#define SVM_VMGEXIT_SAVIC_REGISTER_GPA 0#define SVM_VMGEXIT_SAVIC_UNREGISTER_GPA 1#define SVM_VMGEXIT_SAVIC_SELF_GPA ~0ULL +#define SVM_VMGEXIT_PMC_FILTER 0x8000001eull#define SVM_VMGEXIT_HV_FEATURES 0x8000fffdull#define SVM_VMGEXIT_TERM_REQUEST 0x8000fffeull#define SVM_VMGEXIT_TERM_REASON (reason_set, reason_code) \
@@ -245,6 +246,7 @@ { SVM_VMGEXIT_GUEST_REQUEST, "vmgexit_guest_request" }, \ { SVM_VMGEXIT_EXT_GUEST_REQUEST, "vmgexit_ext_guest_request" }, \ { SVM_VMGEXIT_AP_CREATION, "vmgexit_ap_creation" }, \ + { SVM_VMGEXIT_PMC_FILTER, "vmgexit_pmc_filter" }, \ { SVM_VMGEXIT_HV_FEATURES, "vmgexit_hypervisor_feature" }, \ { SVM_EXIT_ERR, "invalid_guest_state" }
Signature · 1 line
2.43.0
RFC PATCH 5/9 x86/sev: Forward trapped PMC event-select writes to the hypervisor
In reply to Manali ShuklaWith PMC virtualization, an SEV-ES or SNP guest writes its event-selects directly, and the hypervisor filters events in hardware: a write that enables an event missing from its allow list raises a #VC. Forward such writes with SVM_VMGEXIT_PMC_FILTER, along with every event-select, and program the value the hypervisor returns.
Write the requested value ANDed with the value the hypervisor returns. This way the hypervisor can only turn bits off, e.g. ENABLE for a denied event, and never turn bits on. Turning ENABLE back on for a denied event would trap again inside the #VC handler.
On a PerfCntrGlobalCtl write, first write each event-select ANDed with its returned value, which turns off the counters with denied events, then write PerfCntrGlobalCtl.
--- arch/x86/coco/core.c | 3 ++ arch/x86/coco/sev/core.c | 2 +- arch/x86/coco/sev/internal.h | 1 + arch/x86/coco/sev/vc-handle.c | 68 +++++++++++++++++++++++++++++++++++ include/linux/cc_platform.h | 8 +++++ 5 files changed, 81 insertions(+), 1 deletion(-)
arch/x86/coco/core.c +3 -0
@@ -107,6 +107,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)case CC_ATTR_SNP_SECURE_AVIC :return sev_status &MSR_AMD64_SNP_SECURE_AVIC ; +case CC_ATTR_GUEST_PMC_VIRT : +return sev_status &MSR_AMD64_SNP_PMC_VIRT ; +default :return false; }
arch/x86/coco/sev/core.c +1 -1
@@ -756,7 +756,7 @@ static void *snp_alloc_vmsa_page(int cpu)return page_address (p +1 ); } -static unsigned int sev_pmc_nr_gp_counters__ro_after_init ; +unsigned int sev_pmc_nr_gp_counters__ro_after_init ;static int __init sev_pmc_init (void ) {
arch/x86/coco/sev/internal.h +1 -0
@@ -6,6 +6,7 @@extern u64 sev_hv_features;extern u64 sev_secrets_pa; +extern unsigned int sev_pmc_nr_gp_counters;/* #VC handler runtime per-CPU data */ struct sev_es_runtime_data {
arch/x86/coco/sev/vc-handle.c +68 -0
86 lines not on this page · raw message
include/linux/cc_platform.h +8 -0
@@ -104,6 +104,14 @@ enum cc_attr {* to run SEV-SNP guests with full Secure AVIC capabilities. */ CC_ATTR_SNP_SECURE_AVIC , + +/** +* @CC_ATTR_GUEST_PMC_VIRT: SEV-ES/SNP PMC virtualization is active. +* +* The hypervisor filters PMC events in hardware, trapped event-select +* writes must be forwarded to it. +*/ + CC_ATTR_GUEST_PMC_VIRT, };#ifdef CONFIG_ARCH_HAS_CC_PLATFORM
Signature · 1 line
2.43.0
RFC PATCH 6/9 KVM: SEV: Add support for hardware-switched PMU
In reply to Manali ShuklaFrom: Sandipan Das <[email protected]>
Extend hardware-switched mediated PMU support to SEV-ES and SEV-SNP guests. As the guest PMU state resides in the encrypted VMSA, PMC virtualization has to be negotiated with the guest through a new SEV feature bit, SVM_SEV_FEAT_PERFCTR_VIRT. Don't advertise it yet: a guest with PMC virtualization programs its event-selects directly and would bypass the PMU event filter.
The requested VMSA features are only known once the guest has been initialized, so set the KVM_MEDIATED_PMU_CAP_HW_SWITCHED capability in the guest init path, gated on the feature bit.
Once the VMSA is encrypted, KVM can no longer access the guest PMU state. Hence:
* Don't load or save the guest PMU state in the VMCB, it lives in the VMSA.
* Do not intercept RDPMC, as it cannot be emulated after the VMSA is encrypted.
[Manali: Keep PMC filtering off and PERFCTR_VIRT unadvertised until KVM
can filter the guest's events, don't WARN on a protected vendor state]
--- arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm/pmu.c | 8 ++++++++ arch/x86/kvm/svm/sev.c | 28 ++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.c | 3 +++ arch/x86/kvm/svm/svm.h | 1 + 5 files changed, 39 insertions(+), 2 deletions(-)
arch/x86/include/asm/svm.h +1 -0
@@ -316,6 +316,7 @@ static_assert((X2AVIC_4K_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AV#define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4)#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5)#define SVM_SEV_FEAT_SECURE_TSC BIT(9) +#define SVM_SEV_FEAT_PERFCTR_VIRT BIT(11)#define VMCB_ALLOWED_SEV_FEATURES_VALID BIT_ULL(63)
arch/x86/kvm/svm/pmu.c +8 -0
@@ -198,6 +198,10 @@ static int amd_get_vendor_state(struct kvm_vcpu *vcpu, u32 msr)struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct kvm_pmc *pmc; +/* A protected guest's PMU state is in the VMSA, there's nothing to do. */ +if (vcpu->arch.guest_state_protected) +return 0 ; +/* MSR_PERF_CNTR_GLOBAL_* */ switch (msr) {case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS :
@@ -233,6 +237,10 @@ static int amd_set_vendor_state(struct kvm_vcpu *vcpu, u32 msr)struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct kvm_pmc *pmc; +/* A protected guest's PMU state is in the VMSA, there's nothing to do. */ +if (vcpu->arch.guest_state_protected) +return 0 ; +/* MSR_PERF_CNTR_GLOBAL_* */ switch (msr) {case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS :
arch/x86/kvm/svm/sev.c +26 -2
@@ -212,6 +212,14 @@ static bool snp_is_secure_tsc_enabled(struct kvm *kvm) !WARN_ON_ONCE (!sev_snp_guest (kvm)); } +static bool sev_es_is_perfctr_virt_enabled (struct kvm *kvm) +{ +struct kvm_sev_info *sev =to_kvm_sev_info (kvm); + +return (sev->vmsa_features &SVM_SEV_FEAT_PERFCTR_VIRT ) && + !WARN_ON_ONCE (!sev_es_guest (kvm)); +} +/* Must be called with the sev_bitmap_lock held */ static bool __sev_recycle_asids (unsigned int min_asid,unsigned int max_asid) {
@@ -534,9 +542,11 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp,if (snp_active) sev->vmsa_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +if (es_active && !sev_es_is_perfctr_virt_enabled (kvm)) +kvm_clr_mediated_pmu_caps (kvm,KVM_MEDIATED_PMU_CAP_HW_SWITCHED ); +if (es_active) -kvm_clr_mediated_pmu_caps (kvm,KVM_MEDIATED_PMU_CAP_HW_SWITCHED | -KVM_MEDIATED_PMU_CAP_HW_FILTERED ); +kvm_clr_mediated_pmu_caps (kvm,KVM_MEDIATED_PMU_CAP_HW_FILTERED ); ret =sev_asid_new (sev, vm_type);if (ret)
@@ -4744,6 +4754,20 @@ void sev_es_recalc_msr_intercepts(struct kvm_vcpu *vcpu) !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)); } +void sev_es_recalc_instruction_intercepts (struct kvm_vcpu *vcpu) +{ +if (!kvm_vcpu_has_mediated_pmu_caps (vcpu,KVM_MEDIATED_PMU_CAP_HW_SWITCHED )) +return ; + +/* +* RDPMC cannot be emulated after the VMSA is encrypted. If a guest has +* fewer counters than the host, the extra counters will continue being +* readable via RDPMC (no #GP) but they will remain in the init state. +* Access to the extra PERF_CTLs and PERF_CTRs remains intercepted. +*/ +svm_clr_intercept (to_svm (vcpu),INTERCEPT_RDPMC ); +} +void sev_vcpu_after_set_cpuid (struct vcpu_svm *svm) {struct kvm_vcpu *vcpu = &svm->vcpu;
arch/x86/kvm/svm/svm.c +3 -0
@@ -1148,6 +1148,9 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu)svm_set_intercept (svm,INTERCEPT_RDPMC ); elsesvm_clr_intercept (svm ,INTERCEPT_RDPMC ); + +if (is_sev_es_guest (vcpu)) +sev_es_recalc_instruction_intercepts (vcpu); }static void svm_recalc_intercepts (struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +1 -0
@@ -1001,6 +1001,7 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event);void sev_vcpu_after_set_cpuid (struct vcpu_svm *svm);int sev_es_string_io (struct vcpu_svm *svm,int size,unsigned int port,int in);void sev_es_recalc_msr_intercepts (struct kvm_vcpu *vcpu); +void sev_es_recalc_instruction_intercepts (struct kvm_vcpu *vcpu);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_switch_to_guest (struct vcpu_svm *svm,struct sev_es_save_area *hostsa);void sev_es_unmap_ghcb (struct vcpu_svm *svm);
Signature · 1 line
2.43.0
RFC PATCH 7/9 KVM: SEV: Filter PMC event-selects forwarded by SEV-ES and SNP guests
In reply to Manali ShuklaKVM can't intercept a protected guest's event-select writes, and can't read or write the VMSA copy, so the guest forwards writes that trapped on a missing allow-list entry with SVM_VMGEXIT_PMC_FILTER, along with all of its event-selects.
Emulate the write and run the resulting reprogramming synchronously, so the event filter decision is returned in the scratch buffer and RAX/RDX for the guest to program. As for other guests, a full allow list is rebuilt from the enabled event-selects, but as these are guest-supplied, only events the filter allows are listed.
Now that KVM tracks the guest's event-selects, skip counting emulated instructions, KVM can't update the counters in the VMSA.
--- arch/x86/kvm/pmu.c | 7 +++- arch/x86/kvm/pmu.h | 1 + arch/x86/kvm/svm/pmu.c | 81 ++++++++++++++++++++++++++++++++++++++---- arch/x86/kvm/svm/sev.c | 54 ++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 3 ++ 6 files changed, 140 insertions(+), 7 deletions(-)
arch/x86/kvm/pmu.c +6 -1
@@ -573,7 +573,7 @@ static bool is_fixed_event_allowed(struct kvm_x86_pmu_event_filter *filter,return true; } -static bool pmc_is_event_allowed (struct kvm_pmc *pmc) +bool pmc_is_event_allowed (struct kvm_pmc *pmc) {struct kvm_x86_pmu_event_filter *filter;struct kvm *kvm = pmc->vcpu->kvm;
@@ -587,6 +587,7 @@ static bool pmc_is_event_allowed(struct kvm_pmc *pmc)return is_fixed_event_allowed (filter, pmc->idx); } +EXPORT_SYMBOL_FOR_KVM_INTERNAL (pmc_is_event_allowed);static void kvm_mediated_pmu_refresh_event_filter (struct kvm_pmc *pmc) {
@@ -1209,6 +1210,10 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,BUILD_BUG_ON (sizeof (pmu->global_ctrl) *BITS_PER_BYTE !=X86_PMC_IDX_MAX ); +/* KVM can't access a protected guest's counters. */ +if (vcpu->arch.guest_state_protected) +return ; +/* * The guest can reprogram allowed events without trapping. Don't count, * or raise a PMI, for a counter the guest has since repurposed.
arch/x86/kvm/pmu.h +1 -0
@@ -175,6 +175,7 @@ static inline u64 pmc_read_counter(struct kvm_pmc *pmc) }void pmc_write_counter (struct kvm_pmc *pmc,u64 val); +bool pmc_is_event_allowed (struct kvm_pmc *pmc);static inline bool pmc_is_gp (struct kvm_pmc *pmc) {
arch/x86/kvm/svm/pmu.c +75 -6
127 lines not on this page · raw message
arch/x86/kvm/svm/sev.c +54 -0
78 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +1 -0
@@ -2851,6 +2851,7 @@ static bool sev_es_prevent_msr_access(struct kvm_vcpu *vcpu, {return is_sev_es_guest (vcpu) && vcpu->arch.guest_state_protected && msr_info->index !=MSR_IA32_XSS && + !to_svm (vcpu)->sev_es.pmc_filter_pending && !msr_write_intercepted (to_svm (vcpu), msr_info->index); }
arch/x86/kvm/svm/svm.h +3 -0
@@ -282,6 +282,9 @@ struct vcpu_sev_es_state {gpa_t snp_guest_vmsa_gpa;bool snp_ap_waiting_for_reset;bool snp_has_guest_vmsa; + +/* Handling SVM_VMGEXIT_PMC_FILTER, the scratch area holds event-selects. */ +bool pmc_filter_pending; };struct vcpu_svm {
Signature · 1 line
2.43.0
RFC PATCH 8/9 KVM: x86/pmu: Disallow PMU event filter changes for protected guests
In reply to Manali ShuklaWith hardware-assisted PMC filtering, a protected guest programs its event-selects itself, and KVM can't read or change them. If the PMU event filter changes while the guest runs, KVM can't stop an event the new filter denies, or restart one the old filter denied.
So fail filter changes with EBUSY once the VM has vCPUs. The guest then always runs with the filter it started with.
--- arch/x86/kvm/pmu.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
arch/x86/kvm/pmu.c +11 -0
@@ -1388,6 +1388,17 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp) goto cleanup;mutex_lock (&kvm->lock); +/* +* A protected guest's event-selects are in its encrypted VMSA, KVM +* can't re-vet the counters it runs against a new filter. +*/ +if (kvm->arch.has_protected_state && + (kvm->arch.mediated_pmu_caps &KVM_MEDIATED_PMU_CAP_HW_FILTERED ) && + kvm->created_vcpus) { +mutex_unlock (&kvm->lock); + r = -EBUSY ; + goto cleanup; + } filter =rcu_replace_pointer (kvm->arch.pmu_event_filter, filter,mutex_is_locked (&kvm->lock));mutex_unlock (&kvm->lock);
Signature · 1 line
2.43.0
RFC PATCH 9/9 KVM: SEV: Enable hardware-assisted PMC filtering for SEV-ES and SNP guests
In reply to Manali ShuklaWithout filtering, a guest with PMC virtualization programs its event-selects itself, so the PMU event filter doesn't apply to it. Only advertise SVM_SEV_FEAT_PERFCTR_VIRT when the hardware supports PMC filtering. SEV-ES and SNP guests then forward trapped event-select writes with SVM_VMGEXIT_PMC_FILTER for KVM to filter.
sev_hardware_setup() now checks vpmc, so call it after vpmc gets its final value.
--- arch/x86/kvm/svm/sev.c | 11 +++++++---- arch/x86/kvm/svm/svm.c | 14 +++++++------- arch/x86/kvm/svm/svm.h | 2 ++ 3 files changed, 16 insertions(+), 11 deletions(-)
arch/x86/kvm/svm/sev.c +7 -4
@@ -544,10 +544,8 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, sev->vmsa_features |=SVM_SEV_FEAT_SNP_ACTIVE ;if (es_active && !sev_es_is_perfctr_virt_enabled (kvm)) -kvm_clr_mediated_pmu_caps (kvm,KVM_MEDIATED_PMU_CAP_HW_SWITCHED ); - -if (es_active) -kvm_clr_mediated_pmu_caps (kvm,KVM_MEDIATED_PMU_CAP_HW_FILTERED ); +kvm_clr_mediated_pmu_caps (kvm,KVM_MEDIATED_PMU_CAP_HW_SWITCHED | +KVM_MEDIATED_PMU_CAP_HW_FILTERED ); ret =sev_asid_new (sev, vm_type);if (ret)
@@ -3258,6 +3256,11 @@ void __init sev_hardware_setup(void)if (sev_snp_enabled && tsc_khz &&cpu_feature_enabled (X86_FEATURE_SNP_SECURE_TSC )) sev_supported_vmsa_features |=SVM_SEV_FEAT_SECURE_TSC ; + +/* KVM must filter the events of a guest with PMC virtualization. */ +if (sev_es_enabled &&svm_pmc_filter_supported () && +cpu_feature_enabled (X86_FEATURE_SEV_PERFCTR_VIRT )) + sev_supported_vmsa_features |=SVM_SEV_FEAT_PERFCTR_VIRT ; }void sev_hardware_unsetup (void )
arch/x86/kvm/svm/svm.c +7 -7
@@ -186,7 +186,7 @@ module_param(vpmc, bool, 0444);* TODO: Support nested. vmcb02 doesn't enable PMC filtering, so L2 could * program denied events through vmcb01's passthrough event-selects. */ -static bool svm_pmc_filter_supported (void ) +bool svm_pmc_filter_supported (void ) {return vpmc && !nested &&cpu_feature_enabled (X86_FEATURE_PMCFILTER ); }
@@ -5779,12 +5779,6 @@ static __init int svm_hardware_setup(void) iopm_base =__sme_set (__pa (iopm_va)); -/* -* Note, SEV setup consumes npt_enabled and enable_mmio_caching (which -* may be modified by svm_adjust_mmio_mask()), as well as nrips. -*/ -sev_hardware_setup (); -svm_hv_hardware_setup (); enable_apicv =avic_hardware_setup ();
@@ -5842,6 +5836,12 @@ static __init int svm_hardware_setup(void)if (svm_pmc_filter_supported ())pr_info ("PMC filtering virtualization supported\n" ); +/* +* Note, SEV setup consumes npt_enabled and enable_mmio_caching (which +* may be modified by svm_adjust_mmio_mask()), as well as nrips and vpmc. +*/ +sev_hardware_setup (); +svm_set_cpu_caps (); kvm_caps.inapplicable_quirks &= ~KVM_X86_QUIRK_CD_NW_CLEARED ;
arch/x86/kvm/svm/svm.h +2 -0
@@ -867,6 +867,8 @@ static inline void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu,#define SVM_PMC_FILTER_MASK (AMD64_EVENTSEL_EVENT | ARCH_PERFMON_EVENTSEL_UMASK) +bool svm_pmc_filter_supported (void ); +int svm_skip_emulated_instruction (struct kvm_vcpu *vcpu);/* nested.c */
Signature · 1 line
2.43.0