Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp637282rdd; Tue, 9 Jan 2024 15:15:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IHM5OwRWtBU5HuGNJEtOlF7G4qCIPnjYl8z8vXwyc4IBUbixdS2HCGuiElILYbjv5blx7SI X-Received: by 2002:a9d:4e94:0:b0:6dd:de32:6f66 with SMTP id v20-20020a9d4e94000000b006ddde326f66mr112762otk.35.1704842114716; Tue, 09 Jan 2024 15:15:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704842114; cv=none; d=google.com; s=arc-20160816; b=icw1ONeea0/B6ERcjDuCv7zsYTk3BD8od1mq9zlzg7fQvujobpwfZJLsn9VNIPb0Mi 1CvMtUDWb3mC8k7HCI2QSdm6x5LJVOEQr5MCDJm/7pfHxGHexLmHq7HU4z37SVA4UYIu it2tKzkkRXbg9w5J0ar/5VwMuwFniZPK0vscM4jxZNh//1iTedIMkkqtQL0bvS57f3Qz ok+641aDLmQUB7C3GNt+K6rNV5obG49ybdZg+9qGNidIymUfhxXCRrnFC33kRL2w8eJ9 elAO07cMWa1uGvZgIunjelfYjh1UsgkSg9OeWqKphn0NPsEBfr1sAQ6tXK7HcRjC/oVx P5Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=AWl5hfwTfsrMtEvU0KjZOppzEl6irPgMeLi04MWQvxY=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=wXDSvvSolgt1TXkTyXpQcAgJ0hlwJ2aR55Qqt1yyd9JD9QlxuUAGizkKD6H6fixGOd LcJ7s1xlNERzbgN+3H1qxyFIzzn+RBW29TmljmXUHioOcgzmzLUEcjTchnBtED7QqZB7 H+6ohJkQQmesZliUic575yNn1aaK2Hhky7t37wBaN6wamaR2VtMHWcUmHkftnWJHzQzB goF/+vanVEzue9EBzlDxuoO9XKOD6zwCZYqwLuaX+F+rCgsDb/H5NJWWGnsw6HbpswIz AOlCm0Asv17S/zGUiiZ8Ix/mDweG0mcT1ighEXkqrsAymDCZAO5iMurviDtwB+vE3wxR iacg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2YpnGuwr; spf=pass (google.com: domain of linux-kernel+bounces-21507-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21507-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id t18-20020a63b252000000b005cd887d9766si2248219pgo.166.2024.01.09.15.15.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:15:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21507-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2YpnGuwr; spf=pass (google.com: domain of linux-kernel+bounces-21507-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21507-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id AEDD4B26CB6 for ; Tue, 9 Jan 2024 23:10:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 126FF48CEA; Tue, 9 Jan 2024 23:03:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2YpnGuwr" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 47F5348789 for ; Tue, 9 Jan 2024 23:03:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5e795672280so59011857b3.3 for ; Tue, 09 Jan 2024 15:03:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704841408; x=1705446208; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=AWl5hfwTfsrMtEvU0KjZOppzEl6irPgMeLi04MWQvxY=; b=2YpnGuwrF+qdm7LkxSuWVIUIJB299h+FrbTYXHI98zwQqA76m9+iyg1z/NubegjF52 Pndlu755eHfb8wrGBDo3BsMbwcd2lUoukRGueCyo9CqJfIrJJhwJQGleF8gKcoAhIlCY qTJfW4jaVMog3RFNRVQCr2xasuvtGy+cep2Vy5xvYwSgv5F7iEGDbb0gjTBx8IxZwuGS QDCI5PKvolYYt9R+afuJ5cxIMO06jxxB4+xsyg/QWDlolrjtujCqE8O9VQz92mCAlgDm GGmbAyHaaF6xaL5FB+VbNZOR2/PJhEKKt05gVDffsd4DItC6A3uz3Ni1tRL5ktp0uxyI vbOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704841408; x=1705446208; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AWl5hfwTfsrMtEvU0KjZOppzEl6irPgMeLi04MWQvxY=; b=Z0yB5SEdApxvvmUMzhBSyzX0Vl2fF4sKXv3kv/UZHP7WwoGWwMZtZQbo7vTYmbFn8v sZq3uzfQrpZE+cxMrfxPzl2MUp9EHMsbR1EL3tdIQy3VgV9K1OdqaYrEybayhRmUKiXK jPxTtUWK/t6IggCYSSTcdVJrlKfWsyc35WQiFN5/zJ40YaFV58tDrm80MIZswi6afqNN sB/jPLVW9yH6HkzaxlUD+0XW8CU0yGsoS64e0XuSVm+4f+GGszZUekzqFn77ot5wgqzi 0WhLMBI8XKpRb/Qk1ojyydEQyncSsCoEyRwWvKnwkZdOLb62U5nlh/NqnED3meePUokW OkIQ== X-Gm-Message-State: AOJu0Yw3PkbgYCQIPXOV9cZX7SB7X5qHw0Jj6PUUghSh0on0U+QRRTp8 t8wjk3r7rMLEKcC57uaB8ppTwUwPxNtH6abDsw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:8251:0:b0:dbd:b056:b468 with SMTP id d17-20020a258251000000b00dbdb056b468mr32770ybn.7.1704841408519; Tue, 09 Jan 2024 15:03:28 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 15:02:38 -0800 In-Reply-To: <20240109230250.424295-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240109230250.424295-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240109230250.424295-19-seanjc@google.com> Subject: [PATCH v10 18/29] KVM: selftests: Test consistency of CPUID with num of gp counters From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Dapeng Mi , Jim Mattson , Jinrong Liang , Aaron Lewis , Like Xu Content-Type: text/plain; charset="UTF-8" From: Jinrong Liang Add a test to verify that KVM correctly emulates MSR-based accesses to general purpose counters based on guest CPUID, e.g. that accesses to non-existent counters #GP and accesses to existent counters succeed. Note, for compatibility reasons, KVM does not emulate #GP when MSR_P6_PERFCTR[0|1] is not present (writes should be dropped). Co-developed-by: Like Xu Signed-off-by: Like Xu Signed-off-by: Jinrong Liang Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/pmu_counters_test.c | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c index 663e8fbe7ff8..863418842ef8 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -270,9 +270,103 @@ static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities, kvm_vm_free(vm); } +/* + * Limit testing to MSRs that are actually defined by Intel (in the SDM). MSRs + * that aren't defined counter MSRs *probably* don't exist, but there's no + * guarantee that currently undefined MSR indices won't be used for something + * other than PMCs in the future. + */ +#define MAX_NR_GP_COUNTERS 8 +#define MAX_NR_FIXED_COUNTERS 3 + +#define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector) \ +__GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ + "Expected %s on " #insn "(0x%x), got vector %u", \ + expect_gp ? "#GP" : "no fault", msr, vector) \ + +#define GUEST_ASSERT_PMC_VALUE(insn, msr, val, expected) \ + __GUEST_ASSERT(val == expected_val, \ + "Expected " #insn "(0x%x) to yield 0x%lx, got 0x%lx", \ + msr, expected_val, val); + +static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters, + uint8_t nr_counters) +{ + uint8_t i; + + for (i = 0; i < nr_possible_counters; i++) { + /* + * TODO: Test a value that validates full-width writes and the + * width of the counters. + */ + const uint64_t test_val = 0xffff; + const uint32_t msr = base_msr + i; + const bool expect_success = i < nr_counters; + + /* + * KVM drops writes to MSR_P6_PERFCTR[0|1] if the counters are + * unsupported, i.e. doesn't #GP and reads back '0'. + */ + const uint64_t expected_val = expect_success ? test_val : 0; + const bool expect_gp = !expect_success && msr != MSR_P6_PERFCTR0 && + msr != MSR_P6_PERFCTR1; + uint8_t vector; + uint64_t val; + + vector = wrmsr_safe(msr, test_val); + GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector); + + vector = rdmsr_safe(msr, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDMSR, msr, expect_gp, vector); + + /* On #GP, the result of RDMSR is undefined. */ + if (!expect_gp) + GUEST_ASSERT_PMC_VALUE(RDMSR, msr, val, expected_val); + + vector = wrmsr_safe(msr, 0); + GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector); + } + GUEST_DONE(); +} + +static void guest_test_gp_counters(void) +{ + uint8_t nr_gp_counters = 0; + uint32_t base_msr; + + if (guest_get_pmu_version()) + nr_gp_counters = this_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS); + + if (this_cpu_has(X86_FEATURE_PDCM) && + rdmsr(MSR_IA32_PERF_CAPABILITIES) & PMU_CAP_FW_WRITES) + base_msr = MSR_IA32_PMC0; + else + base_msr = MSR_IA32_PERFCTR0; + + guest_rd_wr_counters(base_msr, MAX_NR_GP_COUNTERS, nr_gp_counters); +} + +static void test_gp_counters(uint8_t pmu_version, uint64_t perf_capabilities, + uint8_t nr_gp_counters) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_gp_counters, + pmu_version, perf_capabilities); + + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_GP_COUNTERS, + nr_gp_counters); + + run_vcpu(vcpu); + + kvm_vm_free(vm); +} + static void test_intel_counters(void) { uint8_t nr_arch_events = kvm_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH); + uint8_t nr_gp_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS); uint8_t pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION); unsigned int i; uint8_t v, j; @@ -336,6 +430,11 @@ static void test_intel_counters(void) for (k = 0; k < nr_arch_events; k++) test_arch_events(v, perf_caps[i], j, BIT(k)); } + + pr_info("Testing GP counters, PMU version %u, perf_caps = %lx\n", + v, perf_caps[i]); + for (j = 0; j <= nr_gp_counters; j++) + test_gp_counters(v, perf_caps[i], j); } } } -- 2.43.0.472.g3155946c3a-goog