From: "Mi, Dapeng" <dapeng1.mi@linux.intel.com>
To: Xiong Zhang <xiong.y.zhang@intel.com>, kvm@vger.kernel.org
Cc: seanjc@google.com, like.xu.linux@gmail.com, zhiyuan.lv@intel.com,
zhenyu.z.wang@intel.com, kan.liang@intel.com
Subject: Re: [PATCH 9/9] KVM: selftests: Add fixed counters enumeration test case
Date: Mon, 11 Sep 2023 11:03:43 +0800 [thread overview]
Message-ID: <f94d2d9c-3f20-b024-bc7d-cb1611eae86b@linux.intel.com> (raw)
In-Reply-To: <20230901072809.640175-10-xiong.y.zhang@intel.com>
On 9/1/2023 3:28 PM, Xiong Zhang wrote:
> vPMU v5 adds fixed counter enumeration, which allows user space to
> specify which fixed counters are supported through emulated
> CPUID.0Ah.ECX.
>
> This commit adds a test case which specify the max fixed counter
> supported only, so guest can access the max fixed counter only, #GP
> exception will be happen once guest access other fixed counters.
>
> Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.com>
> ---
> .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 84 +++++++++++++++++++
> 1 file changed, 84 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c
> index ebbcb0a3f743..e37dc39164fe 100644
> --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c
> +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c
since we added new test cases in this file, this file is not just test
'perf_capablities' anymore, we may change the file name
vmx_pmu_caps_test.c to a more generic name like "vmx_pmu_test.c" or
something else.
> @@ -18,6 +18,8 @@
> #include "kvm_util.h"
> #include "vmx.h"
>
> +uint8_t fixed_counter_num;
> +
> union perf_capabilities {
> struct {
> u64 lbr_format:6;
> @@ -233,6 +235,86 @@ static void test_lbr_perf_capabilities(union perf_capabilities host_cap)
> kvm_vm_free(vm);
> }
>
> +static void guest_v5_code(void)
> +{
> + uint8_t vector, i;
> + uint64_t val;
> +
> + for (i = 0; i < fixed_counter_num; i++) {
> + vector = rdmsr_safe(MSR_CORE_PERF_FIXED_CTR0 + i, &val);
> +
> + /*
> + * Only the max fixed counter is supported, #GP will be generated
> + * when guest access other fixed counters.
> + */
> + if (i == fixed_counter_num - 1)
> + __GUEST_ASSERT(vector != GP_VECTOR,
> + "Max Fixed counter is accessible, but get #GP");
> + else
> + __GUEST_ASSERT(vector == GP_VECTOR,
> + "Fixed counter isn't accessible, but access is ok");
> + }
> +
> + GUEST_DONE();
> +}
> +
> +#define PMU_NR_FIXED_COUNTERS_MASK 0x1f
> +
> +static void test_fixed_counter_enumeration(void)
> +{
> + struct kvm_vcpu *vcpu;
> + struct kvm_vm *vm;
> + int r;
> + struct kvm_cpuid_entry2 *ent;
> + struct ucall uc;
> + uint32_t fixed_counter_bit_mask;
> +
> + if (kvm_cpu_property(X86_PROPERTY_PMU_VERSION) != 5)
> + return;
We'd better check if the version is less than 5 here, since we might
have higher version than 5 in the future.
> +
> + vm = vm_create_with_one_vcpu(&vcpu, guest_v5_code);
> + vm_init_descriptor_tables(vm);
> + vcpu_init_descriptor_tables(vcpu);
> +
> + ent = vcpu_get_cpuid_entry(vcpu, 0xa);
> + fixed_counter_num = ent->edx & PMU_NR_FIXED_COUNTERS_MASK;
> + TEST_ASSERT(fixed_counter_num > 0, "fixed counter isn't supported");
> + fixed_counter_bit_mask = (1ul << fixed_counter_num) - 1;
> + TEST_ASSERT(ent->ecx == fixed_counter_bit_mask,
> + "cpuid.0xa.ecx != %x", fixed_counter_bit_mask);
> +
> + /* Fixed counter 0 isn't in ecx, but in edx, set_cpuid should be error. */
> + ent->ecx &= ~0x1;
> + r = __vcpu_set_cpuid(vcpu);
> + TEST_ASSERT(r, "Setting in-consistency cpuid.0xa.ecx and edx success");
> +
> + if (fixed_counter_num == 1) {
> + kvm_vm_free(vm);
> + return;
> + }
> +
> + /* Support the max Fixed Counter only */
> + ent->ecx = 1UL << (fixed_counter_num - 1);
> + ent->edx &= ~(u32)PMU_NR_FIXED_COUNTERS_MASK;
> +
> + r = __vcpu_set_cpuid(vcpu);
> + TEST_ASSERT(!r, "Setting modified cpuid.0xa.ecx and edx failed");
> +
> + vcpu_run(vcpu);
> +
> + switch (get_ucall(vcpu, &uc)) {
> + case UCALL_ABORT:
> + REPORT_GUEST_ASSERT(uc);
> + break;
> + case UCALL_DONE:
> + break;
> + default:
> + TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
> + }
> +
> + kvm_vm_free(vm);
> +}
> +
> int main(int argc, char *argv[])
> {
> union perf_capabilities host_cap;
> @@ -253,4 +335,6 @@ int main(int argc, char *argv[])
> test_immutable_perf_capabilities(host_cap);
> test_guest_wrmsr_perf_capabilities(host_cap);
> test_lbr_perf_capabilities(host_cap);
> +
> + test_fixed_counter_enumeration();
> }
next prev parent reply other threads:[~2023-09-11 3:03 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-01 7:28 [PATCH 0/9] Upgrade vPMU version to 5 Xiong Zhang
2023-09-01 7:28 ` [PATCH 1/9] KVM: x86/PMU: Don't release vLBR caused by PMI Xiong Zhang
2023-09-06 9:47 ` Mi, Dapeng
2023-09-12 11:54 ` Like Xu
2023-09-13 6:00 ` Zhang, Xiong Y
2023-09-01 7:28 ` [PATCH 2/9] KVM: x85/pmu: Add Streamlined FREEZE_LBR_ON_PMI for vPMU v4 Xiong Zhang
2023-09-06 9:49 ` Mi, Dapeng
2023-09-01 7:28 ` [PATCH 3/9] KVM: x86/pmu: Add PERF_GLOBAL_STATUS_SET MSR emulation Xiong Zhang
2023-09-01 7:28 ` [PATCH 4/9] KVM: x86/pmu: Add MSR_PERF_GLOBAL_INUSE emulation Xiong Zhang
2023-09-12 11:41 ` Like Xu
2023-09-13 5:11 ` Zhang, Xiong Y
2023-09-01 7:28 ` [PATCH 5/9] KVM: x86/pmu: Check CPUID.0AH.ECX consistency Xiong Zhang
2023-09-06 9:44 ` Mi, Dapeng
2023-09-12 0:45 ` Zhang, Xiong Y
2023-09-12 11:31 ` Like Xu
2023-09-13 4:25 ` Zhang, Xiong Y
2023-09-01 7:28 ` [PATCH 6/9] KVM: x86/pmu: Add Intel PMU supported fixed counters mask Xiong Zhang
2023-09-06 10:08 ` Mi, Dapeng
2023-09-01 7:28 ` [PATCH 7/9] KVM: x86/pmu: Add fixed counter enumeration for pmu v5 Xiong Zhang
2023-09-12 11:24 ` Like Xu
2023-09-13 4:11 ` Zhang, Xiong Y
2023-09-01 7:28 ` [PATCH 8/9] KVM: x86/pmu: Upgrade pmu version to 5 on intel processor Xiong Zhang
2023-09-12 11:19 ` Like Xu
2023-09-13 3:34 ` Zhang, Xiong Y
2023-09-01 7:28 ` [PATCH 9/9] KVM: selftests: Add fixed counters enumeration test case Xiong Zhang
2023-09-11 3:03 ` Mi, Dapeng [this message]
2023-09-12 0:35 ` Zhang, Xiong Y
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f94d2d9c-3f20-b024-bc7d-cb1611eae86b@linux.intel.com \
--to=dapeng1.mi@linux.intel.com \
--cc=kan.liang@intel.com \
--cc=kvm@vger.kernel.org \
--cc=like.xu.linux@gmail.com \
--cc=seanjc@google.com \
--cc=xiong.y.zhang@intel.com \
--cc=zhenyu.z.wang@intel.com \
--cc=zhiyuan.lv@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox