From: Wei Huang Subject: [PATCH V4 2/4] KVM: x86/vPMU: Create vPMU interface for VMX and SVM Date: Fri, 5 Jun 2015 01:20:14 -0400 Message-ID: <1433481616-24884-3-git-send-email-wei@redhat.com> References: <1433481616-24884-1-git-send-email-wei@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE To: kvm@vger.kernel.org, pbonzini@redhat.com, gleb@kernel.org, rkrcmar@redhat.com, joro@8bytes.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:55025 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751463AbbFEFUU (ORCPT ); Fri, 5 Jun 2015 01:20:20 -0400 In-Reply-To: <1433481616-24884-1-git-send-email-wei@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: This patch splits existing vPMU code into a common vPMU interface (pmc.= c) and Intel specific vPMU code (pmu_intel.c) using the following steps: - Part of arechitectural vPMU code is extracted and moved to pmu_intel.= c file. They are hooked up with the newly-defined intel_pmu_ops, which = will be called from pmu interface; - Create a dummy pmu_amd.c file for AMD SVM with empty functions; All architectural vPMU functions are now called via PMU function dispat= cher (kvm_pmu_ops). This function dispatcher is initialized by calling kvm_x86_ops->get_pmu_ops() at the beginning. Also note that Intel and A= MD modules are now generated by combinig their corresponding arch files (vmx.c/svm.c) and pmu files (pmu_intel.c/pmu_amd.c). Tested-by: Radim Kr=C4=8Dm=C3=A1=C5=99 Signed-off-by: Wei Huang --- arch/x86/include/asm/kvm_host.h | 16 +- arch/x86/kvm/Makefile | 4 +- arch/x86/kvm/cpuid.c | 3 +- arch/x86/kvm/pmu.c | 552 +++++++++++---------------------= -------- arch/x86/kvm/pmu.h | 99 +++++++ arch/x86/kvm/pmu_amd.c | 97 +++++++ arch/x86/kvm/pmu_intel.c | 360 ++++++++++++++++++++++++++ arch/x86/kvm/svm.c | 8 + arch/x86/kvm/vmx.c | 8 + arch/x86/kvm/x86.c | 25 +- 10 files changed, 749 insertions(+), 423 deletions(-) create mode 100644 arch/x86/kvm/pmu.h create mode 100644 arch/x86/kvm/pmu_amd.c create mode 100644 arch/x86/kvm/pmu_intel.c diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm= _host.h index 4f0ca20..26060b2 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -855,6 +855,8 @@ struct kvm_x86_ops { void (*enable_log_dirty_pt_masked)(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t offset, unsigned long mask); + + struct kvm_pmu_ops *(*get_pmu_ops)(void); }; =20 struct kvm_arch_async_pf { @@ -865,6 +867,7 @@ struct kvm_arch_async_pf { }; =20 extern struct kvm_x86_ops *kvm_x86_ops; +extern struct kvm_pmu_ops *kvm_pmu_ops; =20 static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment) @@ -1188,17 +1191,4 @@ extern bool kvm_find_async_pf_gfn(struct kvm_vcp= u *vcpu, gfn_t gfn); void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err); =20 int kvm_is_in_guest(void); - -void kvm_pmu_init(struct kvm_vcpu *vcpu); -void kvm_pmu_destroy(struct kvm_vcpu *vcpu); -void kvm_pmu_reset(struct kvm_vcpu *vcpu); -void kvm_pmu_cpuid_update(struct kvm_vcpu *vcpu); -bool kvm_pmu_msr(struct kvm_vcpu *vcpu, u32 msr); -int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data); -int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info); -int kvm_pmu_check_pmc(struct kvm_vcpu *vcpu, unsigned pmc); -int kvm_pmu_read_pmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data); -void kvm_handle_pmu_event(struct kvm_vcpu *vcpu); -void kvm_deliver_pmi(struct kvm_vcpu *vcpu); - #endif /* _ASM_X86_KVM_HOST_H */ diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile index 16e8f96..c1c99d7 100644 --- a/arch/x86/kvm/Makefile +++ b/arch/x86/kvm/Makefile @@ -14,8 +14,8 @@ kvm-$(CONFIG_KVM_ASYNC_PF) +=3D $(KVM)/async_pf.o kvm-y +=3D x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT) +=3D assigned-dev.o iommu.o -kvm-intel-y +=3D vmx.o -kvm-amd-y +=3D svm.o +kvm-intel-y +=3D vmx.o pmu_intel.o +kvm-amd-y +=3D svm.o pmu_amd.o =20 obj-$(CONFIG_KVM) +=3D kvm.o obj-$(CONFIG_KVM_INTEL) +=3D kvm-intel.o diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 59b69f6..d75ee44 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -22,6 +22,7 @@ #include "lapic.h" #include "mmu.h" #include "trace.h" +#include "pmu.h" =20 static u32 xstate_required_size(u64 xstate_bv, bool compacted) { @@ -107,7 +108,7 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu) /* Update physical-address width */ vcpu->arch.maxphyaddr =3D cpuid_query_maxphyaddr(vcpu); =20 - kvm_pmu_cpuid_update(vcpu); + kvm_pmu_refresh(vcpu); return 0; } =20 diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 3af3404..1d5e8d2 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -1,17 +1,16 @@ /* * Kernel-based Virtual Machine -- Performance Monitoring Unit support * - * Copyright 2011 Red Hat, Inc. and/or its affiliates. + * Copyright 2015 Red Hat, Inc. and/or its affiliates. * * Authors: * Avi Kivity * Gleb Natapov + * Wei Huang * * This work is licensed under the terms of the GNU GPL, version 2. S= ee * the COPYING file in the top-level directory. - * */ - #include #include #include @@ -19,83 +18,41 @@ #include "x86.h" #include "cpuid.h" #include "lapic.h" +#include "pmu.h" + +/* NOTE: + * - Each perf counter is defined as "struct kvm_pmc"; + * - There are two types of perf counters: general purpose (gp) and fi= xed. + * gp counters are stored in gp_counters[] and fixed counters are st= ored + * in fixed_counters[] respectively. Both of them are part of "struc= t + * kvm_pmu"; + * - pmu.c understands the difference between gp counters and fixed co= unters. + * However AMD doesn't support fixed-counters; + * - There are three types of index to access perf counters (PMC): + * 1. MSR (named msr): For example Intel has MSR_IA32_PERFCTRn and= AMD + * has MSR_K7_PERFCTRn. + * 2. MSR Index (named idx): This normally is used by RDPMC instru= ction. + * For instance AMD RDPMC instruction uses 0000_0003h in ECX to= access + * C001_0007h (MSR_K7_PERCTR3). Intel has a similar mechanism, = except + * that it also supports fixed counters. idx can be used to as = index to + * gp and fixed counters. + * 3. Global PMC Index (named pmc): pmc is an index specific to PM= U + * code. Each pmc, stored in kvm_pmc.idx field, is unique acros= s + * all perf counters (both gp and fixed). The mapping relations= hip + * between pmc and perf counters is as the following: + * * Intel: [0 .. INTEL_PMC_MAX_GENERIC-1] <=3D> gp counters + * [INTEL_PMC_IDX_FIXED .. INTEL_PMC_IDX_FIXED + 2] <=3D= > fixed + * * AMD: [0 .. AMD64_NUM_COUNTERS-1] <=3D> gp counters + */ =20 -static struct kvm_event_hw_type_mapping intel_arch_events[] =3D { - /* Index must match CPUID 0x0A.EBX bit vector */ - [0] =3D { 0x3c, 0x00, PERF_COUNT_HW_CPU_CYCLES }, - [1] =3D { 0xc0, 0x00, PERF_COUNT_HW_INSTRUCTIONS }, - [2] =3D { 0x3c, 0x01, PERF_COUNT_HW_BUS_CYCLES }, - [3] =3D { 0x2e, 0x4f, PERF_COUNT_HW_CACHE_REFERENCES }, - [4] =3D { 0x2e, 0x41, PERF_COUNT_HW_CACHE_MISSES }, - [5] =3D { 0xc4, 0x00, PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, - [6] =3D { 0xc5, 0x00, PERF_COUNT_HW_BRANCH_MISSES }, - [7] =3D { 0x00, 0x30, PERF_COUNT_HW_REF_CPU_CYCLES }, -}; - -/* mapping between fixed pmc index and intel_arch_events array */ -static int fixed_pmc_events[] =3D {1, 0, 7}; - -static bool pmc_is_gp(struct kvm_pmc *pmc) -{ - return pmc->type =3D=3D KVM_PMC_GP; -} - -static inline u64 pmc_bitmask(struct kvm_pmc *pmc) -{ - struct kvm_pmu *pmu =3D &pmc->vcpu->arch.pmu; - - return pmu->counter_bitmask[pmc->type]; -} - -static inline bool pmc_enabled(struct kvm_pmc *pmc) -{ - struct kvm_pmu *pmu =3D &pmc->vcpu->arch.pmu; - return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl); -} - -static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr, - u32 base) -{ - if (msr >=3D base && msr < base + pmu->nr_arch_gp_counters) - return &pmu->gp_counters[msr - base]; - return NULL; -} - -static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 m= sr) -{ - int base =3D MSR_CORE_PERF_FIXED_CTR0; - if (msr >=3D base && msr < base + pmu->nr_arch_fixed_counters) - return &pmu->fixed_counters[msr - base]; - return NULL; -} - -static inline struct kvm_pmc *get_fixed_pmc_idx(struct kvm_pmu *pmu, i= nt idx) -{ - return get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + idx); -} - -static struct kvm_pmc *global_idx_to_pmc(struct kvm_pmu *pmu, int idx) -{ - if (idx < INTEL_PMC_IDX_FIXED) - return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + idx, MSR_P6_EVNTSEL0); - else - return get_fixed_pmc_idx(pmu, idx - INTEL_PMC_IDX_FIXED); -} - -void kvm_deliver_pmi(struct kvm_vcpu *vcpu) -{ - if (vcpu->arch.apic) - kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC); -} +struct kvm_pmu_ops *kvm_pmu_ops; =20 -static void trigger_pmi(struct irq_work *irq_work) +static void kvm_pmi_trigger_fn(struct irq_work *irq_work) { - struct kvm_pmu *pmu =3D container_of(irq_work, struct kvm_pmu, - irq_work); - struct kvm_vcpu *vcpu =3D container_of(pmu, struct kvm_vcpu, - arch.pmu); + struct kvm_pmu *pmu =3D container_of(irq_work, struct kvm_pmu, irq_wo= rk); + struct kvm_vcpu *vcpu =3D pmu_to_vcpu(pmu); =20 - kvm_deliver_pmi(vcpu); + kvm_pmu_deliver_pmi(vcpu); } =20 static void kvm_perf_overflow(struct perf_event *perf_event, @@ -103,26 +60,32 @@ static void kvm_perf_overflow(struct perf_event *p= erf_event, struct pt_regs *regs) { struct kvm_pmc *pmc =3D perf_event->overflow_handler_context; - struct kvm_pmu *pmu =3D &pmc->vcpu->arch.pmu; - if (!test_and_set_bit(pmc->idx, (unsigned long *)&pmu->reprogram_pmi)= ) { + struct kvm_pmu *pmu =3D pmc_to_pmu(pmc); + + if (!test_and_set_bit(pmc->idx, + (unsigned long *)&pmu->reprogram_pmi)) { __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); kvm_make_request(KVM_REQ_PMU, pmc->vcpu); } } =20 static void kvm_perf_overflow_intr(struct perf_event *perf_event, - struct perf_sample_data *data, struct pt_regs *regs) + struct perf_sample_data *data, + struct pt_regs *regs) { struct kvm_pmc *pmc =3D perf_event->overflow_handler_context; - struct kvm_pmu *pmu =3D &pmc->vcpu->arch.pmu; - if (!test_and_set_bit(pmc->idx, (unsigned long *)&pmu->reprogram_pmi)= ) { + struct kvm_pmu *pmu =3D pmc_to_pmu(pmc); + + if (!test_and_set_bit(pmc->idx, + (unsigned long *)&pmu->reprogram_pmi)) { __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); kvm_make_request(KVM_REQ_PMU, pmc->vcpu); + /* * Inject PMI. If vcpu was in a guest mode during NMI PMI * can be ejected on a guest mode re-entry. Otherwise we can't * be sure that vcpu wasn't executing hlt instruction at the - * time of vmexit and is not going to re-enter guest mode until, + * time of vmexit and is not going to re-enter guest mode until * woken up. So we should wake it, but this is impossible from * NMI context. Do it from irq work instead. */ @@ -133,33 +96,10 @@ static void kvm_perf_overflow_intr(struct perf_eve= nt *perf_event, } } =20 -static u64 read_pmc(struct kvm_pmc *pmc) -{ - u64 counter, enabled, running; - - counter =3D pmc->counter; - - if (pmc->perf_event) - counter +=3D perf_event_read_value(pmc->perf_event, - &enabled, &running); - - /* FIXME: Scaling needed? */ - - return counter & pmc_bitmask(pmc); -} - -static void stop_counter(struct kvm_pmc *pmc) -{ - if (pmc->perf_event) { - pmc->counter =3D read_pmc(pmc); - perf_event_release_kernel(pmc->perf_event); - pmc->perf_event =3D NULL; - } -} - -static void reprogram_counter(struct kvm_pmc *pmc, u32 type, - unsigned config, bool exclude_user, bool exclude_kernel, - bool intr, bool in_tx, bool in_tx_cp) +static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, + unsigned config, bool exclude_user, + bool exclude_kernel, bool intr, + bool in_tx, bool in_tx_cp) { struct perf_event *event; struct perf_event_attr attr =3D { @@ -172,6 +112,7 @@ static void reprogram_counter(struct kvm_pmc *pmc, = u32 type, .exclude_kernel =3D exclude_kernel, .config =3D config, }; + if (in_tx) attr.config |=3D HSW_IN_TX; if (in_tx_cp) @@ -183,8 +124,8 @@ static void reprogram_counter(struct kvm_pmc *pmc, = u32 type, intr ? kvm_perf_overflow_intr : kvm_perf_overflow, pmc); if (IS_ERR(event)) { - printk_once("kvm: pmu event creation failed %ld\n", - PTR_ERR(event)); + printk_once("kvm_pmu: event creation failed %ld\n", + PTR_ERR(event)); return; } =20 @@ -192,24 +133,7 @@ static void reprogram_counter(struct kvm_pmc *pmc,= u32 type, clear_bit(pmc->idx, (unsigned long*)&pmc->vcpu->arch.pmu.reprogram_pm= i); } =20 -static unsigned find_arch_event(struct kvm_pmu *pmu, u8 event_select, - u8 unit_mask) -{ - int i; - - for (i =3D 0; i < ARRAY_SIZE(intel_arch_events); i++) - if (intel_arch_events[i].eventsel =3D=3D event_select - && intel_arch_events[i].unit_mask =3D=3D unit_mask - && (pmu->available_event_types & (1 << i))) - break; - - if (i =3D=3D ARRAY_SIZE(intel_arch_events)) - return PERF_COUNT_HW_MAX; - - return intel_arch_events[i].event_type; -} - -static void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) +void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) { unsigned config, type =3D PERF_TYPE_RAW; u8 event_select, unit_mask; @@ -219,21 +143,22 @@ static void reprogram_gp_counter(struct kvm_pmc *= pmc, u64 eventsel) =20 pmc->eventsel =3D eventsel; =20 - stop_counter(pmc); + pmc_stop_counter(pmc); =20 - if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_enabled(pmc)) + if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc= )) return; =20 event_select =3D eventsel & ARCH_PERFMON_EVENTSEL_EVENT; unit_mask =3D (eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8; =20 if (!(eventsel & (ARCH_PERFMON_EVENTSEL_EDGE | - ARCH_PERFMON_EVENTSEL_INV | - ARCH_PERFMON_EVENTSEL_CMASK | - HSW_IN_TX | - HSW_IN_TX_CHECKPOINTED))) { - config =3D find_arch_event(&pmc->vcpu->arch.pmu, event_select, - unit_mask); + ARCH_PERFMON_EVENTSEL_INV | + ARCH_PERFMON_EVENTSEL_CMASK | + HSW_IN_TX | + HSW_IN_TX_CHECKPOINTED))) { + config =3D kvm_pmu_ops->find_arch_event(&pmc->vcpu->arch.pmu, + event_select, + unit_mask); if (config !=3D PERF_COUNT_HW_MAX) type =3D PERF_TYPE_HARDWARE; } @@ -241,56 +166,36 @@ static void reprogram_gp_counter(struct kvm_pmc *= pmc, u64 eventsel) if (type =3D=3D PERF_TYPE_RAW) config =3D eventsel & X86_RAW_EVENT_MASK; =20 - reprogram_counter(pmc, type, config, - !(eventsel & ARCH_PERFMON_EVENTSEL_USR), - !(eventsel & ARCH_PERFMON_EVENTSEL_OS), - eventsel & ARCH_PERFMON_EVENTSEL_INT, - (eventsel & HSW_IN_TX), - (eventsel & HSW_IN_TX_CHECKPOINTED)); + pmc_reprogram_counter(pmc, type, config, + !(eventsel & ARCH_PERFMON_EVENTSEL_USR), + !(eventsel & ARCH_PERFMON_EVENTSEL_OS), + eventsel & ARCH_PERFMON_EVENTSEL_INT, + (eventsel & HSW_IN_TX), + (eventsel & HSW_IN_TX_CHECKPOINTED)); } +EXPORT_SYMBOL_GPL(reprogram_gp_counter); =20 -static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 en_pmi, in= t idx) +void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx) { - unsigned en =3D en_pmi & 0x3; - bool pmi =3D en_pmi & 0x8; + unsigned en_field =3D ctrl & 0x3; + bool pmi =3D ctrl & 0x8; =20 - stop_counter(pmc); + pmc_stop_counter(pmc); =20 - if (!en || !pmc_enabled(pmc)) + if (!en_field || !pmc_is_enabled(pmc)) return; =20 - reprogram_counter(pmc, PERF_TYPE_HARDWARE, - intel_arch_events[fixed_pmc_events[idx]].event_type, - !(en & 0x2), /* exclude user */ - !(en & 0x1), /* exclude kernel */ - pmi, false, false); -} - -static inline u8 fixed_en_pmi(u64 ctrl, int idx) -{ - return (ctrl >> (idx * 4)) & 0xf; -} - -static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) -{ - int i; - - for (i =3D 0; i < pmu->nr_arch_fixed_counters; i++) { - u8 en_pmi =3D fixed_en_pmi(data, i); - struct kvm_pmc *pmc =3D get_fixed_pmc_idx(pmu, i); - - if (fixed_en_pmi(pmu->fixed_ctr_ctrl, i) =3D=3D en_pmi) - continue; - - reprogram_fixed_counter(pmc, en_pmi, i); - } - - pmu->fixed_ctr_ctrl =3D data; + pmc_reprogram_counter(pmc, PERF_TYPE_HARDWARE, + kvm_pmu_ops->find_fixed_event(idx), + !(en_field & 0x2), /* exclude user */ + !(en_field & 0x1), /* exclude kernel */ + pmi, false, false); } +EXPORT_SYMBOL_GPL(reprogram_fixed_counter); =20 -static void reprogram_idx(struct kvm_pmu *pmu, int idx) +void reprogram_counter(struct kvm_pmu *pmu, int pmc_idx) { - struct kvm_pmc *pmc =3D global_idx_to_pmc(pmu, idx); + struct kvm_pmc *pmc =3D kvm_pmu_ops->pmc_idx_to_pmc(pmu, pmc_idx); =20 if (!pmc) return; @@ -298,251 +203,104 @@ static void reprogram_idx(struct kvm_pmu *pmu, = int idx) if (pmc_is_gp(pmc)) reprogram_gp_counter(pmc, pmc->eventsel); else { - int fidx =3D idx - INTEL_PMC_IDX_FIXED; - reprogram_fixed_counter(pmc, - fixed_en_pmi(pmu->fixed_ctr_ctrl, fidx), fidx); + int idx =3D pmc_idx - INTEL_PMC_IDX_FIXED; + u8 ctrl =3D fixed_ctrl_field(pmu->fixed_ctr_ctrl, idx); + + reprogram_fixed_counter(pmc, ctrl, idx); } } +EXPORT_SYMBOL_GPL(reprogram_counter); =20 -static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data) +void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) { + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + u64 bitmask; int bit; - u64 diff =3D pmu->global_ctrl ^ data; - - pmu->global_ctrl =3D data; =20 - for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX) - reprogram_idx(pmu, bit); -} - -bool kvm_pmu_msr(struct kvm_vcpu *vcpu, u32 msr) -{ - struct kvm_pmu *pmu =3D &vcpu->arch.pmu; - int ret; - - switch (msr) { - case MSR_CORE_PERF_FIXED_CTR_CTRL: - case MSR_CORE_PERF_GLOBAL_STATUS: - case MSR_CORE_PERF_GLOBAL_CTRL: - case MSR_CORE_PERF_GLOBAL_OVF_CTRL: - ret =3D pmu->version > 1; - break; - default: - ret =3D get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) - || get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) - || get_fixed_pmc(pmu, msr); - break; - } - return ret; -} + bitmask =3D pmu->reprogram_pmi; =20 -int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data) -{ - struct kvm_pmu *pmu =3D &vcpu->arch.pmu; - struct kvm_pmc *pmc; + for_each_set_bit(bit, (unsigned long *)&bitmask, X86_PMC_IDX_MAX) { + struct kvm_pmc *pmc =3D kvm_pmu_ops->pmc_idx_to_pmc(pmu, bit); =20 - switch (index) { - case MSR_CORE_PERF_FIXED_CTR_CTRL: - *data =3D pmu->fixed_ctr_ctrl; - return 0; - case MSR_CORE_PERF_GLOBAL_STATUS: - *data =3D pmu->global_status; - return 0; - case MSR_CORE_PERF_GLOBAL_CTRL: - *data =3D pmu->global_ctrl; - return 0; - case MSR_CORE_PERF_GLOBAL_OVF_CTRL: - *data =3D pmu->global_ovf_ctrl; - return 0; - default: - if ((pmc =3D get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) || - (pmc =3D get_fixed_pmc(pmu, index))) { - *data =3D read_pmc(pmc); - return 0; - } else if ((pmc =3D get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) { - *data =3D pmc->eventsel; - return 0; + if (unlikely(!pmc || !pmc->perf_event)) { + clear_bit(bit, (unsigned long *)&pmu->reprogram_pmi); + continue; } - } - return 1; -} =20 -int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) -{ - struct kvm_pmu *pmu =3D &vcpu->arch.pmu; - struct kvm_pmc *pmc; - u32 index =3D msr_info->index; - u64 data =3D msr_info->data; - - switch (index) { - case MSR_CORE_PERF_FIXED_CTR_CTRL: - if (pmu->fixed_ctr_ctrl =3D=3D data) - return 0; - if (!(data & 0xfffffffffffff444ull)) { - reprogram_fixed_counters(pmu, data); - return 0; - } - break; - case MSR_CORE_PERF_GLOBAL_STATUS: - if (msr_info->host_initiated) { - pmu->global_status =3D data; - return 0; - } - break; /* RO MSR */ - case MSR_CORE_PERF_GLOBAL_CTRL: - if (pmu->global_ctrl =3D=3D data) - return 0; - if (!(data & pmu->global_ctrl_mask)) { - global_ctrl_changed(pmu, data); - return 0; - } - break; - case MSR_CORE_PERF_GLOBAL_OVF_CTRL: - if (!(data & (pmu->global_ctrl_mask & ~(3ull<<62)))) { - if (!msr_info->host_initiated) - pmu->global_status &=3D ~data; - pmu->global_ovf_ctrl =3D data; - return 0; - } - break; - default: - if ((pmc =3D get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) || - (pmc =3D get_fixed_pmc(pmu, index))) { - if (!msr_info->host_initiated) - data =3D (s64)(s32)data; - pmc->counter +=3D data - read_pmc(pmc); - return 0; - } else if ((pmc =3D get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) { - if (data =3D=3D pmc->eventsel) - return 0; - if (!(data & pmu->reserved_bits)) { - reprogram_gp_counter(pmc, data); - return 0; - } - } + reprogram_counter(pmu, bit); } - return 1; } =20 -int kvm_pmu_check_pmc(struct kvm_vcpu *vcpu, unsigned pmc) +/* check if idx is a valid index to access PMU */ +int kvm_pmu_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx) { - struct kvm_pmu *pmu =3D &vcpu->arch.pmu; - bool fixed =3D pmc & (1u << 30); - pmc &=3D ~(3u << 30); - return (!fixed && pmc >=3D pmu->nr_arch_gp_counters) || - (fixed && pmc >=3D pmu->nr_arch_fixed_counters); + return kvm_pmu_ops->is_valid_msr_idx(vcpu, idx); } =20 -int kvm_pmu_read_pmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data) +int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) { - struct kvm_pmu *pmu =3D &vcpu->arch.pmu; - bool fast_mode =3D pmc & (1u << 31); - bool fixed =3D pmc & (1u << 30); - struct kvm_pmc *counters; - u64 ctr; - - pmc &=3D ~(3u << 30); - if (!fixed && pmc >=3D pmu->nr_arch_gp_counters) - return 1; - if (fixed && pmc >=3D pmu->nr_arch_fixed_counters) + bool fast_mode =3D idx & (1u << 31); + struct kvm_pmc *pmc; + u64 ctr_val; + + pmc =3D kvm_pmu_ops->msr_idx_to_pmc(vcpu, idx); + if (!pmc) return 1; - counters =3D fixed ? pmu->fixed_counters : pmu->gp_counters; - ctr =3D read_pmc(&counters[pmc]); + + ctr_val =3D pmc_read_counter(pmc); if (fast_mode) - ctr =3D (u32)ctr; - *data =3D ctr; + ctr_val =3D (u32)ctr_val; =20 + *data =3D ctr_val; return 0; } =20 -void kvm_pmu_cpuid_update(struct kvm_vcpu *vcpu) +void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu) { - struct kvm_pmu *pmu =3D &vcpu->arch.pmu; - struct kvm_cpuid_entry2 *entry; - union cpuid10_eax eax; - union cpuid10_edx edx; - - pmu->nr_arch_gp_counters =3D 0; - pmu->nr_arch_fixed_counters =3D 0; - pmu->counter_bitmask[KVM_PMC_GP] =3D 0; - pmu->counter_bitmask[KVM_PMC_FIXED] =3D 0; - pmu->version =3D 0; - pmu->reserved_bits =3D 0xffffffff00200000ull; - - entry =3D kvm_find_cpuid_entry(vcpu, 0xa, 0); - if (!entry) - return; - eax.full =3D entry->eax; - edx.full =3D entry->edx; - - pmu->version =3D eax.split.version_id; - if (!pmu->version) - return; - - pmu->nr_arch_gp_counters =3D min_t(int, eax.split.num_counters, - INTEL_PMC_MAX_GENERIC); - pmu->counter_bitmask[KVM_PMC_GP] =3D ((u64)1 << eax.split.bit_width) = - 1; - pmu->available_event_types =3D ~entry->ebx & - ((1ull << eax.split.mask_length) - 1); - - if (pmu->version =3D=3D 1) { - pmu->nr_arch_fixed_counters =3D 0; - } else { - pmu->nr_arch_fixed_counters =3D - min_t(int, edx.split.num_counters_fixed, - INTEL_PMC_MAX_FIXED); - pmu->counter_bitmask[KVM_PMC_FIXED] =3D - ((u64)1 << edx.split.bit_width_fixed) - 1; - } + if (vcpu->arch.apic) + kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC); +} =20 - pmu->global_ctrl =3D ((1 << pmu->nr_arch_gp_counters) - 1) | - (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED)= ; - pmu->global_ctrl_mask =3D ~pmu->global_ctrl; +bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) +{ + return kvm_pmu_ops->is_valid_msr(vcpu, msr); +} =20 - entry =3D kvm_find_cpuid_entry(vcpu, 7, 0); - if (entry && - (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) = && - (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) - pmu->reserved_bits ^=3D HSW_IN_TX|HSW_IN_TX_CHECKPOINTED; +int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data) +{ + return kvm_pmu_ops->get_msr(vcpu, msr, data); } =20 -void kvm_pmu_init(struct kvm_vcpu *vcpu) +int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { - int i; - struct kvm_pmu *pmu =3D &vcpu->arch.pmu; + return kvm_pmu_ops->set_msr(vcpu, msr_info); +} =20 - memset(pmu, 0, sizeof(*pmu)); - for (i =3D 0; i < INTEL_PMC_MAX_GENERIC; i++) { - pmu->gp_counters[i].type =3D KVM_PMC_GP; - pmu->gp_counters[i].vcpu =3D vcpu; - pmu->gp_counters[i].idx =3D i; - } - for (i =3D 0; i < INTEL_PMC_MAX_FIXED; i++) { - pmu->fixed_counters[i].type =3D KVM_PMC_FIXED; - pmu->fixed_counters[i].vcpu =3D vcpu; - pmu->fixed_counters[i].idx =3D i + INTEL_PMC_IDX_FIXED; - } - init_irq_work(&pmu->irq_work, trigger_pmi); - kvm_pmu_cpuid_update(vcpu); +/* refresh PMU settings. This function generally is called when underl= ying + * settings are changed (such as changes of PMU CPUID by guest VMs), w= hich + * should rarely happen. + */ +void kvm_pmu_refresh(struct kvm_vcpu *vcpu) +{ + kvm_pmu_ops->refresh(vcpu); } =20 void kvm_pmu_reset(struct kvm_vcpu *vcpu) { - struct kvm_pmu *pmu =3D &vcpu->arch.pmu; - int i; + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); =20 irq_work_sync(&pmu->irq_work); - for (i =3D 0; i < INTEL_PMC_MAX_GENERIC; i++) { - struct kvm_pmc *pmc =3D &pmu->gp_counters[i]; - stop_counter(pmc); - pmc->counter =3D pmc->eventsel =3D 0; - } + kvm_pmu_ops->reset(vcpu); +} =20 - for (i =3D 0; i < INTEL_PMC_MAX_FIXED; i++) - stop_counter(&pmu->fixed_counters[i]); +void kvm_pmu_init(struct kvm_vcpu *vcpu) +{ + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); =20 - pmu->fixed_ctr_ctrl =3D pmu->global_ctrl =3D pmu->global_status =3D - pmu->global_ovf_ctrl =3D 0; + memset(pmu, 0, sizeof(*pmu)); + kvm_pmu_ops->init(vcpu); + init_irq_work(&pmu->irq_work, kvm_pmi_trigger_fn); + kvm_pmu_refresh(vcpu); } =20 void kvm_pmu_destroy(struct kvm_vcpu *vcpu) @@ -550,22 +308,20 @@ void kvm_pmu_destroy(struct kvm_vcpu *vcpu) kvm_pmu_reset(vcpu); } =20 -void kvm_handle_pmu_event(struct kvm_vcpu *vcpu) +/* called before using any PMU functions above */ +int kvm_pmu_arch_init(struct kvm_x86_ops *x86_ops) { - struct kvm_pmu *pmu =3D &vcpu->arch.pmu; - u64 bitmask; - int bit; - - bitmask =3D pmu->reprogram_pmi; + if (x86_ops =3D=3D NULL) + return -EINVAL; =20 - for_each_set_bit(bit, (unsigned long *)&bitmask, X86_PMC_IDX_MAX) { - struct kvm_pmc *pmc =3D global_idx_to_pmc(pmu, bit); + kvm_pmu_ops =3D x86_ops->get_pmu_ops(); + if (kvm_pmu_ops =3D=3D NULL) + return -EINVAL; =20 - if (unlikely(!pmc || !pmc->perf_event)) { - clear_bit(bit, (unsigned long *)&pmu->reprogram_pmi); - continue; - } + return 0; +} =20 - reprogram_idx(pmu, bit); - } +void kvm_pmu_arch_exit(void) +{ + kvm_pmu_ops =3D NULL; } diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h new file mode 100644 index 0000000..ff25042 --- /dev/null +++ b/arch/x86/kvm/pmu.h @@ -0,0 +1,99 @@ +#ifndef __KVM_X86_PMU_H +#define __KVM_X86_PMU_H + +#define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) +#define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.p= mu)) +#define pmc_to_pmu(pmc) (&(pmc)->vcpu->arch.pmu) + +/* retrieve the control fields of IA32_FIXED_CTR_CTRL */ +#define fixed_ctrl_field(ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0= xf) + +static inline u64 pmc_bitmask(struct kvm_pmc *pmc) +{ + struct kvm_pmu *pmu =3D pmc_to_pmu(pmc); + + return pmu->counter_bitmask[pmc->type]; +} + +static inline u64 pmc_read_counter(struct kvm_pmc *pmc) +{ + u64 counter, enabled, running; + + counter =3D pmc->counter; + if (pmc->perf_event) + counter +=3D perf_event_read_value(pmc->perf_event, + &enabled, &running); + /* FIXME: Scaling needed? */ + return counter & pmc_bitmask(pmc); +} + +static inline void pmc_stop_counter(struct kvm_pmc *pmc) +{ + if (pmc->perf_event) { + pmc->counter =3D pmc_read_counter(pmc); + perf_event_release_kernel(pmc->perf_event); + pmc->perf_event =3D NULL; + } +} + +static inline bool pmc_is_gp(struct kvm_pmc *pmc) +{ + return pmc->type =3D=3D KVM_PMC_GP; +} + +static inline bool pmc_is_fixed(struct kvm_pmc *pmc) +{ + return pmc->type =3D=3D KVM_PMC_FIXED; +} + +static inline bool pmc_is_enabled(struct kvm_pmc *pmc) +{ + return kvm_pmu_ops->pmc_enabled(pmc); +} + +/* returns general purpose PMC with the specified MSR. Note that it ca= n be + * used for both PERFCTRn and EVNTSELn; that is why it accepts base as= a + * paramenter to tell them apart. + */ +static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr, + u32 base) +{ + if (msr >=3D base && msr < base + pmu->nr_arch_gp_counters) + return &pmu->gp_counters[msr - base]; + + return NULL; +} + +/* returns fixed PMC with the specified MSR */ +static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 m= sr) +{ + int base =3D MSR_CORE_PERF_FIXED_CTR0; + + if (msr >=3D base && msr < base + pmu->nr_arch_fixed_counters) + return &pmu->fixed_counters[msr - base]; + + return NULL; +} + +void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel); +void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int fixed_i= dx); +void reprogram_counter(struct kvm_pmu *pmu, int pmc_idx); + +void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu); +void kvm_pmu_handle_event(struct kvm_vcpu *vcpu); +int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data); +int kvm_pmu_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx); +bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr); +int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data); +int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info); +void kvm_pmu_refresh(struct kvm_vcpu *vcpu); +void kvm_pmu_reset(struct kvm_vcpu *vcpu); +void kvm_pmu_init(struct kvm_vcpu *vcpu); +void kvm_pmu_destroy(struct kvm_vcpu *vcpu); + +int kvm_pmu_arch_init(struct kvm_x86_ops *x86_ops); +void kvm_pmu_arch_exit(void); + +extern struct kvm_pmu_ops intel_pmu_ops; +extern struct kvm_pmu_ops amd_pmu_ops; +#endif /* __KVM_X86_PMU_H */ diff --git a/arch/x86/kvm/pmu_amd.c b/arch/x86/kvm/pmu_amd.c new file mode 100644 index 0000000..388f637 --- /dev/null +++ b/arch/x86/kvm/pmu_amd.c @@ -0,0 +1,97 @@ +/* + * KVM PMU support for AMD + * + * Copyright 2015, Red Hat, Inc. and/or its affiliates. + * + * Author: + * Wei Huang + * + * This work is licensed under the terms of the GNU GPL, version 2. S= ee + * the COPYING file in the top-level directory. + * + * Implementation is based on pmu_intel.c file + */ +#include +#include +#include +#include "x86.h" +#include "cpuid.h" +#include "lapic.h" +#include "pmu.h" + +static unsigned amd_find_arch_event(struct kvm_pmu *pmu, + u8 event_select, + u8 unit_mask) +{ + return PERF_COUNT_HW_MAX; +} + +/* return PERF_COUNT_HW_MAX as AMD doesn't have fixed events */ +static unsigned amd_find_fixed_event(int idx) +{ + return PERF_COUNT_HW_MAX; +} + +static bool amd_pmc_enabled(struct kvm_pmc *pmc) +{ + return false; +} + +static struct kvm_pmc *amd_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc= _idx) +{ + return NULL; +} + +/* returns 0 if idx's corresponding MSR exists; otherwise returns 1. *= / +static int amd_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx) +{ + return 1; +} + +/* idx is the ECX register of RDPMC instruction */ +static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, unsig= ned idx) +{ + return NULL; +} + +static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) +{ + return false; +} + +static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data) +{ + return 1; +} + +static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr= _info) +{ + return 1; +} + +static void amd_pmu_refresh(struct kvm_vcpu *vcpu) +{ +} + +static void amd_pmu_init(struct kvm_vcpu *vcpu) +{ +} + +static void amd_pmu_reset(struct kvm_vcpu *vcpu) +{ +} + +struct kvm_pmu_ops amd_pmu_ops =3D { + .find_arch_event =3D amd_find_arch_event, + .find_fixed_event =3D amd_find_fixed_event, + .pmc_enabled =3D amd_pmc_enabled, + .pmc_idx_to_pmc =3D amd_pmc_idx_to_pmc, + .msr_idx_to_pmc =3D amd_msr_idx_to_pmc, + .is_valid_msr_idx =3D amd_is_valid_msr_idx, + .is_valid_msr =3D amd_is_valid_msr, + .get_msr =3D amd_pmu_get_msr, + .set_msr =3D amd_pmu_set_msr, + .refresh =3D amd_pmu_refresh, + .init =3D amd_pmu_init, + .reset =3D amd_pmu_reset, +}; diff --git a/arch/x86/kvm/pmu_intel.c b/arch/x86/kvm/pmu_intel.c new file mode 100644 index 0000000..51b4729 --- /dev/null +++ b/arch/x86/kvm/pmu_intel.c @@ -0,0 +1,360 @@ +/* + * KVM PMU support for Intel CPUs + * + * Copyright 2011 Red Hat, Inc. and/or its affiliates. + * + * Authors: + * Avi Kivity + * Gleb Natapov + * + * This work is licensed under the terms of the GNU GPL, version 2. S= ee + * the COPYING file in the top-level directory. + * + */ +#include +#include +#include +#include +#include "x86.h" +#include "cpuid.h" +#include "lapic.h" +#include "pmu.h" + +static struct kvm_event_hw_type_mapping intel_arch_events[] =3D { + /* Index must match CPUID 0x0A.EBX bit vector */ + [0] =3D { 0x3c, 0x00, PERF_COUNT_HW_CPU_CYCLES }, + [1] =3D { 0xc0, 0x00, PERF_COUNT_HW_INSTRUCTIONS }, + [2] =3D { 0x3c, 0x01, PERF_COUNT_HW_BUS_CYCLES }, + [3] =3D { 0x2e, 0x4f, PERF_COUNT_HW_CACHE_REFERENCES }, + [4] =3D { 0x2e, 0x41, PERF_COUNT_HW_CACHE_MISSES }, + [5] =3D { 0xc4, 0x00, PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, + [6] =3D { 0xc5, 0x00, PERF_COUNT_HW_BRANCH_MISSES }, + [7] =3D { 0x00, 0x30, PERF_COUNT_HW_REF_CPU_CYCLES }, +}; + +/* mapping between fixed pmc index and intel_arch_events array */ +static int fixed_pmc_events[] =3D {1, 0, 7}; + +static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) +{ + int i; + + for (i =3D 0; i < pmu->nr_arch_fixed_counters; i++) { + u8 new_ctrl =3D fixed_ctrl_field(data, i); + u8 old_ctrl =3D fixed_ctrl_field(pmu->fixed_ctr_ctrl, i); + struct kvm_pmc *pmc; + + pmc =3D get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + i); + + if (old_ctrl =3D=3D new_ctrl) + continue; + + reprogram_fixed_counter(pmc, new_ctrl, i); + } + + pmu->fixed_ctr_ctrl =3D data; +} + +/* function is called when global control register has been updated. *= / +static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data) +{ + int bit; + u64 diff =3D pmu->global_ctrl ^ data; + + pmu->global_ctrl =3D data; + + for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX) { + reprogram_counter(pmu, bit); + } +} + +static unsigned intel_find_arch_event(struct kvm_pmu *pmu, + u8 event_select, + u8 unit_mask) +{ + int i; + + for (i =3D 0; i < ARRAY_SIZE(intel_arch_events); i++) + if (intel_arch_events[i].eventsel =3D=3D event_select + && intel_arch_events[i].unit_mask =3D=3D unit_mask + && (pmu->available_event_types & (1 << i))) + break; + + if (i =3D=3D ARRAY_SIZE(intel_arch_events)) + return PERF_COUNT_HW_MAX; + + return intel_arch_events[i].event_type; +} + +static unsigned intel_find_fixed_event(int idx) +{ + + if (idx >=3D ARRAY_SIZE(fixed_pmc_events)) + return PERF_COUNT_HW_MAX; + + return intel_arch_events[fixed_pmc_events[idx]].event_type; +} + +/* check if a PMC is enabled by comparising it with globl_ctrl bits. *= / +static bool intel_pmc_enabled(struct kvm_pmc *pmc) +{ + struct kvm_pmu *pmu =3D pmc_to_pmu(pmc); + + return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl); +} + +static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int p= mc_idx) +{ + if (pmc_idx < INTEL_PMC_IDX_FIXED) + return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + pmc_idx, + MSR_P6_EVNTSEL0); + else { + u32 idx =3D pmc_idx - INTEL_PMC_IDX_FIXED; + + return get_fixed_pmc(pmu, idx + MSR_CORE_PERF_FIXED_CTR0); + } +} + +/* returns 0 if idx's corresponding MSR exists; otherwise returns 1. *= / +static int intel_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx) +{ + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + bool fixed =3D idx & (1u << 30); + + idx &=3D ~(3u << 30); + + return (!fixed && idx >=3D pmu->nr_arch_gp_counters) || + (fixed && idx >=3D pmu->nr_arch_fixed_counters); +} + +static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu, + unsigned idx) +{ + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + bool fixed =3D idx & (1u << 30); + struct kvm_pmc *counters; + + idx &=3D ~(3u << 30); + if (!fixed && idx >=3D pmu->nr_arch_gp_counters) + return NULL; + if (fixed && idx >=3D pmu->nr_arch_fixed_counters) + return NULL; + counters =3D fixed ? pmu->fixed_counters : pmu->gp_counters; + + return &counters[idx]; +} + +static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) +{ + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + int ret; + + switch (msr) { + case MSR_CORE_PERF_FIXED_CTR_CTRL: + case MSR_CORE_PERF_GLOBAL_STATUS: + case MSR_CORE_PERF_GLOBAL_CTRL: + case MSR_CORE_PERF_GLOBAL_OVF_CTRL: + ret =3D pmu->version > 1; + break; + default: + ret =3D get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) || + get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) || + get_fixed_pmc(pmu, msr); + break; + } + + return ret; +} + +static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data= ) +{ + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + struct kvm_pmc *pmc; + + switch (msr) { + case MSR_CORE_PERF_FIXED_CTR_CTRL: + *data =3D pmu->fixed_ctr_ctrl; + return 0; + case MSR_CORE_PERF_GLOBAL_STATUS: + *data =3D pmu->global_status; + return 0; + case MSR_CORE_PERF_GLOBAL_CTRL: + *data =3D pmu->global_ctrl; + return 0; + case MSR_CORE_PERF_GLOBAL_OVF_CTRL: + *data =3D pmu->global_ovf_ctrl; + return 0; + default: + if ((pmc =3D get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || + (pmc =3D get_fixed_pmc(pmu, msr))) { + *data =3D pmc_read_counter(pmc); + return 0; + } else if ((pmc =3D get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) { + *data =3D pmc->eventsel; + return 0; + } + } + + return 1; +} + +static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *m= sr_info) +{ + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + struct kvm_pmc *pmc; + u32 msr =3D msr_info->index; + u64 data =3D msr_info->data; + + switch (msr) { + case MSR_CORE_PERF_FIXED_CTR_CTRL: + if (pmu->fixed_ctr_ctrl =3D=3D data) + return 0; + if (!(data & 0xfffffffffffff444ull)) { + reprogram_fixed_counters(pmu, data); + return 0; + } + break; + case MSR_CORE_PERF_GLOBAL_STATUS: + if (msr_info->host_initiated) { + pmu->global_status =3D data; + return 0; + } + break; /* RO MSR */ + case MSR_CORE_PERF_GLOBAL_CTRL: + if (pmu->global_ctrl =3D=3D data) + return 0; + if (!(data & pmu->global_ctrl_mask)) { + global_ctrl_changed(pmu, data); + return 0; + } + break; + case MSR_CORE_PERF_GLOBAL_OVF_CTRL: + if (!(data & (pmu->global_ctrl_mask & ~(3ull<<62)))) { + if (!msr_info->host_initiated) + pmu->global_status &=3D ~data; + pmu->global_ovf_ctrl =3D data; + return 0; + } + break; + default: + if ((pmc =3D get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || + (pmc =3D get_fixed_pmc(pmu, msr))) { + if (!msr_info->host_initiated) + data =3D (s64)(s32)data; + pmc->counter +=3D data - pmc_read_counter(pmc); + return 0; + } else if ((pmc =3D get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) { + if (data =3D=3D pmc->eventsel) + return 0; + if (!(data & pmu->reserved_bits)) { + reprogram_gp_counter(pmc, data); + return 0; + } + } + } + + return 1; +} + +static void intel_pmu_refresh(struct kvm_vcpu *vcpu) +{ + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + struct kvm_cpuid_entry2 *entry; + union cpuid10_eax eax; + union cpuid10_edx edx; + + pmu->nr_arch_gp_counters =3D 0; + pmu->nr_arch_fixed_counters =3D 0; + pmu->counter_bitmask[KVM_PMC_GP] =3D 0; + pmu->counter_bitmask[KVM_PMC_FIXED] =3D 0; + pmu->version =3D 0; + pmu->reserved_bits =3D 0xffffffff00200000ull; + + entry =3D kvm_find_cpuid_entry(vcpu, 0xa, 0); + if (!entry) + return; + eax.full =3D entry->eax; + edx.full =3D entry->edx; + + pmu->version =3D eax.split.version_id; + if (!pmu->version) + return; + + pmu->nr_arch_gp_counters =3D min_t(int, eax.split.num_counters, + INTEL_PMC_MAX_GENERIC); + pmu->counter_bitmask[KVM_PMC_GP] =3D ((u64)1 << eax.split.bit_width) = - 1; + pmu->available_event_types =3D ~entry->ebx & + ((1ull << eax.split.mask_length) - 1); + + if (pmu->version =3D=3D 1) { + pmu->nr_arch_fixed_counters =3D 0; + } else { + pmu->nr_arch_fixed_counters =3D + min_t(int, edx.split.num_counters_fixed, + INTEL_PMC_MAX_FIXED); + pmu->counter_bitmask[KVM_PMC_FIXED] =3D + ((u64)1 << edx.split.bit_width_fixed) - 1; + } + + pmu->global_ctrl =3D ((1 << pmu->nr_arch_gp_counters) - 1) | + (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED)= ; + pmu->global_ctrl_mask =3D ~pmu->global_ctrl; + + entry =3D kvm_find_cpuid_entry(vcpu, 7, 0); + if (entry && + (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) = && + (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) + pmu->reserved_bits ^=3D HSW_IN_TX|HSW_IN_TX_CHECKPOINTED; +} + +static void intel_pmu_init(struct kvm_vcpu *vcpu) +{ + int i; + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + + for (i =3D 0; i < INTEL_PMC_MAX_GENERIC; i++) { + pmu->gp_counters[i].type =3D KVM_PMC_GP; + pmu->gp_counters[i].vcpu =3D vcpu; + pmu->gp_counters[i].idx =3D i; + } + + for (i =3D 0; i < INTEL_PMC_MAX_FIXED; i++) { + pmu->fixed_counters[i].type =3D KVM_PMC_FIXED; + pmu->fixed_counters[i].vcpu =3D vcpu; + pmu->fixed_counters[i].idx =3D i + INTEL_PMC_IDX_FIXED; + } +} + +static void intel_pmu_reset(struct kvm_vcpu *vcpu) +{ + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + int i; + + for (i =3D 0; i < INTEL_PMC_MAX_GENERIC; i++) { + struct kvm_pmc *pmc =3D &pmu->gp_counters[i]; + + pmc_stop_counter(pmc); + pmc->counter =3D pmc->eventsel =3D 0; + } + + for (i =3D 0; i < INTEL_PMC_MAX_FIXED; i++) + pmc_stop_counter(&pmu->fixed_counters[i]); + + pmu->fixed_ctr_ctrl =3D pmu->global_ctrl =3D pmu->global_status =3D + pmu->global_ovf_ctrl =3D 0; +} + +struct kvm_pmu_ops intel_pmu_ops =3D { + .find_arch_event =3D intel_find_arch_event, + .find_fixed_event =3D intel_find_fixed_event, + .pmc_enabled =3D intel_pmc_enabled, + .pmc_idx_to_pmc =3D intel_pmc_idx_to_pmc, + .msr_idx_to_pmc =3D intel_msr_idx_to_pmc, + .is_valid_msr_idx =3D intel_is_valid_msr_idx, + .is_valid_msr =3D intel_is_valid_msr, + .get_msr =3D intel_pmu_get_msr, + .set_msr =3D intel_pmu_set_msr, + .refresh =3D intel_pmu_refresh, + .init =3D intel_pmu_init, + .reset =3D intel_pmu_reset, +}; diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index ce741b8..e2c2df6 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -21,6 +21,7 @@ #include "kvm_cache_regs.h" #include "x86.h" #include "cpuid.h" +#include "pmu.h" =20 #include #include @@ -4337,6 +4338,11 @@ static void svm_sched_in(struct kvm_vcpu *vcpu, = int cpu) { } =20 +static struct kvm_pmu_ops *svm_get_pmu_ops(void) +{ + return &amd_pmu_ops; +} + static struct kvm_x86_ops svm_x86_ops =3D { .cpu_has_kvm_support =3D has_svm, .disabled_by_bios =3D is_disabled, @@ -4439,6 +4445,8 @@ static struct kvm_x86_ops svm_x86_ops =3D { .handle_external_intr =3D svm_handle_external_intr, =20 .sched_in =3D svm_sched_in, + + .get_pmu_ops =3D svm_get_pmu_ops, }; =20 static int __init svm_init(void) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 8c14d6a..f12e3cd 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -48,6 +48,7 @@ #include =20 #include "trace.h" +#include "pmu.h" =20 #define __ex(x) __kvm_handle_fault_on_reboot(x) #define __ex_clear(x, reg) \ @@ -10127,6 +10128,11 @@ static void vmx_enable_log_dirty_pt_masked(str= uct kvm *kvm, kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); } =20 +static struct kvm_pmu_ops *vmx_get_pmu_ops(void) +{ + return &intel_pmu_ops; +} + static struct kvm_x86_ops vmx_x86_ops =3D { .cpu_has_kvm_support =3D cpu_has_kvm_support, .disabled_by_bios =3D vmx_disabled_by_bios, @@ -10240,6 +10246,8 @@ static struct kvm_x86_ops vmx_x86_ops =3D { .slot_disable_log_dirty =3D vmx_slot_disable_log_dirty, .flush_log_dirty =3D vmx_flush_log_dirty, .enable_log_dirty_pt_masked =3D vmx_enable_log_dirty_pt_masked, + + .get_pmu_ops =3D vmx_get_pmu_ops, }; =20 static int __init vmx_init(void) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b8cb1d0..1bd1165 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -28,6 +28,7 @@ #include "x86.h" #include "cpuid.h" #include "assigned-dev.h" +#include "pmu.h" =20 #include #include @@ -910,7 +911,7 @@ bool kvm_rdpmc(struct kvm_vcpu *vcpu) u64 data; int err; =20 - err =3D kvm_pmu_read_pmc(vcpu, ecx, &data); + err =3D kvm_pmu_rdpmc(vcpu, ecx, &data); if (err) return err; kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data); @@ -2290,7 +2291,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, str= uct msr_data *msr_info) pr =3D true; case MSR_P6_EVNTSEL0: case MSR_P6_EVNTSEL1: - if (kvm_pmu_msr(vcpu, msr)) + if (kvm_pmu_is_valid_msr(vcpu, msr)) return kvm_pmu_set_msr(vcpu, msr_info); =20 if (pr || data !=3D 0) @@ -2336,7 +2337,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, str= uct msr_data *msr_info) default: if (msr && (msr =3D=3D vcpu->kvm->arch.xen_hvm_config.msr)) return xen_hvm_config(vcpu, data); - if (kvm_pmu_msr(vcpu, msr)) + if (kvm_pmu_is_valid_msr(vcpu, msr)) return kvm_pmu_set_msr(vcpu, msr_info); if (!ignore_msrs) { vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", @@ -2530,7 +2531,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32= msr, u64 *pdata) case MSR_P6_PERFCTR1: case MSR_P6_EVNTSEL0: case MSR_P6_EVNTSEL1: - if (kvm_pmu_msr(vcpu, msr)) + if (kvm_pmu_is_valid_msr(vcpu, msr)) return kvm_pmu_get_msr(vcpu, msr, pdata); data =3D 0; break; @@ -2653,7 +2654,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32= msr, u64 *pdata) data =3D vcpu->arch.osvw.status; break; default: - if (kvm_pmu_msr(vcpu, msr)) + if (kvm_pmu_is_valid_msr(vcpu, msr)) return kvm_pmu_get_msr(vcpu, msr, pdata); if (!ignore_msrs) { vcpu_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); @@ -4943,13 +4944,13 @@ static int emulator_set_msr(struct x86_emulate_= ctxt *ctxt, static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt, u32 pmc) { - return kvm_pmu_check_pmc(emul_to_vcpu(ctxt), pmc); + return kvm_pmu_is_valid_msr_idx(emul_to_vcpu(ctxt), pmc); } =20 static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt, u32 pmc, u64 *pdata) { - return kvm_pmu_read_pmc(emul_to_vcpu(ctxt), pmc, pdata); + return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata); } =20 static void emulator_halt(struct x86_emulate_ctxt *ctxt) @@ -5807,6 +5808,11 @@ int kvm_arch_init(void *opaque) =20 kvm_timer_init(); =20 + /* init pointers of PMU operations */ + r =3D kvm_pmu_arch_init(ops); + if (r) + goto out_free_percpu; + perf_register_guest_info_callbacks(&kvm_guest_cbs); =20 if (cpu_has_xsave) @@ -5836,6 +5842,7 @@ void kvm_arch_exit(void) #ifdef CONFIG_X86_64 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier); #endif + kvm_pmu_arch_exit(); kvm_x86_ops =3D NULL; kvm_mmu_module_exit(); free_percpu(shared_msrs); @@ -6252,9 +6259,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu= ) if (kvm_check_request(KVM_REQ_NMI, vcpu)) process_nmi(vcpu); if (kvm_check_request(KVM_REQ_PMU, vcpu)) - kvm_handle_pmu_event(vcpu); + kvm_pmu_handle_event(vcpu); if (kvm_check_request(KVM_REQ_PMI, vcpu)) - kvm_deliver_pmi(vcpu); + kvm_pmu_deliver_pmi(vcpu); if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu)) vcpu_scan_ioapic(vcpu); if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu)) --=20 1.8.3.1