Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp2527572rdg; Mon, 14 Aug 2023 05:38:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHYlBW5zavs6xo9s9GXxBozlFXRMSEeH4VtieqX+BAY1RG3dZrQYF9XFrgLQmDbJmnqqpcD X-Received: by 2002:a17:902:c404:b0:1bd:cde4:8b3a with SMTP id k4-20020a170902c40400b001bdcde48b3amr8081472plk.23.1692016681914; Mon, 14 Aug 2023 05:38:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692016681; cv=none; d=google.com; s=arc-20160816; b=fBby7uHZVCLECeZZpnIm8r5YTgwyckUMwacCKYpCGvM38fFWQSm707XE8gSq1lozao NLacF00C++e9F+spXfdY5DjCQb7IXtWPqEl6TGHacnAC2dbuPVboQ8Zepv649H4STAye fR53+C5nUgnEIeuo6GwkgjHjTsikHG2Jf2PL62/aHkvSKrqMOerHWdrzlOy6txVXmUtW o5Y0t54yvsmVihuMwGdcbYh/0PQdMCoGEKJQDtSN9k4Vv9Koth2NEWmq8rfE7AX7M8oE +ssirNXKbTr7vqBLtZxteZmwazFTpFOqUg5okSjunqIDdecUzO8/laMkNicoFvcY2mhU 3wiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=61GfPmVPqdJQe4FEL90WRoo6PIXr9V7sVFEuh/ye+sc=; fh=DvD9bWv0QfYvJ3c1TOyVFnexb+PklxLMx4mXX5CNcBw=; b=eXMWlQDB4UhBEdIFoo2wc1TWlHr08zS5fLZ9FMRD7TSlMp+uDXWpxgJOhgRH6kfRMv RLzn8wjFarCw0JBwSXyTmPpHIAGQ+5fraYhw3/4JcWXBPfFh0+gTRAIOp9FM7Q0qESP0 wtEoRohhEVF7/NrhibCQav2nw1XSFt6qqyGOWlMNxhknuC65o7zcZ9vNcZPFQTbVfbcd /CP29l/xhwkFko+dtFfElJbRf7ojebrIGra+jiItEPoeepeWS8P/pdB/c2DkWsOJANci oHHpa/AEUB5SxSYrmWBeNVsDHkMYw6eD0UXdKnTP4dkURoMCwzY01Z0IK9o+aDpUc+gS QV1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=B7hUH7NJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y15-20020a17090322cf00b001bba7713547si8171167plg.151.2023.08.14.05.37.48; Mon, 14 Aug 2023 05:38:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=B7hUH7NJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231511AbjHNLwL (ORCPT + 99 others); Mon, 14 Aug 2023 07:52:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229928AbjHNLvm (ORCPT ); Mon, 14 Aug 2023 07:51:42 -0400 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59B13EA; Mon, 14 Aug 2023 04:51:41 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id d2e1a72fcca58-68842ebdcf7so174169b3a.0; Mon, 14 Aug 2023 04:51:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692013901; x=1692618701; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=61GfPmVPqdJQe4FEL90WRoo6PIXr9V7sVFEuh/ye+sc=; b=B7hUH7NJhTrBM477jFwBthlaB/D7bYwffvUYtQI3xqNIlGwtkH/pyT95qNZq0t4bYz DbSKjk8lt+QNdG99hQXLG9xHptdi1Amxx14vHOB0GZnVEncCgXoG99a17ZNLkzYJtovv S0wRgW7bCjvkdmKHHB+TGAKU2TgVi7rJCDuyFysPkCSQfA9/q4K0Gr8RYCUujXEJa5uC dJKIEv7eem2kRt/vvk+u4M90xyyxi+2FMD+7f0qO8d8aEac7+SeC8oDyC6ggRYOsyJ9r YgznWQvRUTh7FKcVafARVVlRlivGjyAkOqheGe/47Aq45yg3Dk+R8emC5XNYzdit5qkk lftQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692013901; x=1692618701; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=61GfPmVPqdJQe4FEL90WRoo6PIXr9V7sVFEuh/ye+sc=; b=coNgWgmtwqH+WGYX+uwYPaPGj9nQnygnOoQPNrhS2TpuOvhIHNJZeLrIB9uUaaigrC d3oc+s0hyaBoslgvzXjaWUAAtVcJudbP3tCD1NpdRBejzy2xA632f2nDOTe58Ls8p7V0 s7qXIyU8DFzZwoBkirolDxev3aP9yYQ38zyTWHW6BilxQDSPgBH9Q0IMmGnvP1tenTUU XU/yac959u+FrkHCyNE9oLhPS3fL70PxPzf7DYFJ40BYA6jZTaUrWCFc7mzF1v3yA7Lr cCKiIW61QWEPIV7EPeMQ4Io2HKiWPWnVM8WrsQ/WTUcO7EkxNDMFhVFrEuCwaS7oGFw1 PjdA== X-Gm-Message-State: AOJu0Yz2Fr6QQG4xkTiAr5t3uMY08JCSAa/o+Kyha8Do4ykwWctZ994g tHl0q36qxd/WkWXD739QYAo= X-Received: by 2002:a05:6a20:4325:b0:13f:cd07:2b60 with SMTP id h37-20020a056a20432500b0013fcd072b60mr14322356pzk.1.1692013900876; Mon, 14 Aug 2023 04:51:40 -0700 (PDT) Received: from CLOUDLIANG-MB2.tencent.com ([103.7.29.32]) by smtp.gmail.com with ESMTPSA id x7-20020a63b207000000b0055386b1415dsm8407848pge.51.2023.08.14.04.51.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 04:51:40 -0700 (PDT) From: Jinrong Liang X-Google-Original-From: Jinrong Liang To: Sean Christopherson Cc: Paolo Bonzini , Like Xu , David Matlack , Aaron Lewis , Vitaly Kuznetsov , Wanpeng Li , Jinrong Liang , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 06/11] KVM: selftests: Test consistency of CPUID with num of fixed counters Date: Mon, 14 Aug 2023 19:51:03 +0800 Message-Id: <20230814115108.45741-7-cloudliang@tencent.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814115108.45741-1-cloudliang@tencent.com> References: <20230814115108.45741-1-cloudliang@tencent.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jinrong Liang Add test to check if non-existent counters can be accessed in guest after determining the number of Intel generic performance counters by CPUID. Per SDM, fixed-function performance counter 'i' is supported if ECX[i] || (EDX[4:0] > i). KVM doesn't emulate more counters than it can support. Co-developed-by: Like Xu Signed-off-by: Like Xu Signed-off-by: Jinrong Liang --- .../kvm/x86_64/pmu_basic_functionality_test.c | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c b/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c index b86033e51d5c..db1c1230700a 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c @@ -212,10 +212,43 @@ static void test_oob_gp_counter(uint8_t eax_gp_num, uint8_t offset, kvm_vm_free(vm); } +static void intel_test_oob_fixed_ctr(uint8_t edx_fixed_num, + uint32_t fixed_bitmask, uint64_t expected) +{ + uint8_t idx = edx_fixed_num; + struct kvm_vcpu *vcpu; + uint64_t msr_val = 0; + struct kvm_vm *vm; + bool visible; + + vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_wr_and_rd_msrs); + + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK, + fixed_bitmask); + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS, + edx_fixed_num); + + visible = fixed_counter_is_supported(vcpu, idx); + + /* KVM doesn't emulate more fixed counters than it can support. */ + if (idx >= kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS)) + visible = false; + + vcpu_args_set(vcpu, 3, MSR_CORE_PERF_FIXED_CTR0, idx, 1); + if (visible) { + while (run_vcpu(vcpu, &msr_val) != UCALL_DONE) + TEST_ASSERT_EQ(expected, msr_val); + } + + kvm_vm_free(vm); +} + static void intel_test_counters_num(void) { + uint8_t nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS); uint8_t nr_gp_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS); unsigned int i; + uint32_t ecx; TEST_REQUIRE(nr_gp_counters > 2); @@ -239,6 +272,14 @@ static void intel_test_counters_num(void) if (!perf_caps[i]) test_oob_gp_counter(0, 2, perf_caps[i], 0); } + + for (ecx = 0; + ecx <= kvm_cpu_property(X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK) + 1; + ecx++) { + intel_test_oob_fixed_ctr(0, ecx, GP_VECTOR); + intel_test_oob_fixed_ctr(nr_fixed_counters, ecx, GP_VECTOR); + intel_test_oob_fixed_ctr(nr_fixed_counters + 1, ecx, GP_VECTOR); + } } int main(int argc, char *argv[]) -- 2.39.3