From nobody Fri Nov 29 17:40:03 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 17B881CB52A for ; Wed, 18 Sep 2024 20:54:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726692844; cv=none; b=G4F28MiKgphK0VrjG1pJK2O2Xlz6VE1VqY6k4Y+EHNPKzTOz1xciGvOvVtd3NgRrmU709m66zjJ1ZiBLDj5ekCwet/JFN72PcJFAmD6pZaQW/qLdx8hYzDvT5jBepvrY1YylUuNts74ZGMd3sRHUc0HvCEtN+gwWchAww1C3hU0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726692844; c=relaxed/simple; bh=aSdFYlUtz6P5Eo1j6ju7Ac/ao+lpyvnXe6lnLzmL3WA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Gxc9nvbrs9VLzDt/qDhmZPR2we0hBYIM4n4eEkv/JD5ZFPgAQqTwkhgpaEqeHLKjAj/oiEZQUhSfaOlWF5QI4VIEKJwJyHS38Lc5y3YBzPVPTSJwfq4k6uHH9QR82h40GlaRCYzzZ/nmu7OirbzDrFYDxBjqX16g1396wL9/sbU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cfbogPtS; arc=none smtp.client-ip=209.85.219.202 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--coltonlewis.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cfbogPtS" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1dc0585fbfso291998276.3 for ; Wed, 18 Sep 2024 13:54:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726692842; x=1727297642; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OkrE8WmvzpBAiiKTV2LkBvEEkr6B/d2pJWlGO3Ml/wo=; b=cfbogPtSj8ie6NDwzOBjkw9KUvVueBVQFtgEXEPTJ+ZK3iic5Mlg8KrD8udtheKggD OOpHHBYUPzlUD3pyA68SwubY74nrvzHj4eWW4ywCKRHNax4ACrRs4GTtlVenEgx3pbVP 1D9YLCV1w6wk7FpnI+5G9++9cXC4wPp06Ih880t2hPJiuG8dOmErdchgWtsQ5z7jGaGs fYXPhcoV8UxMeYORpNdcxMrcGXd+3olnZWNMJ5HQa1e6ArbT1Rs1rOGy5vWcaHWc8n4C Yf+Tgon+Qbncf/MpxDUQrnx0eekfRSZ0YDf05kaLWhcTb8EZPpejr0RyjW6g9YhuSyyv 72Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726692842; x=1727297642; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OkrE8WmvzpBAiiKTV2LkBvEEkr6B/d2pJWlGO3Ml/wo=; b=WdAIwYBRIcwaHQ0la4MSvR6KmOyQcgKucztW0ljUNZ7iipdA8/CL7UKOt/Jn0mZteb nbi8McSS4DK9NPTj7clLkaFqP0WCC3Xe7fscjoSvyvOH76cr51deJorYOCAWRwm9qwWQ wbk8+aOmtvJjrkZnpmpHLOJirTeQSddHpxWeLNKc4I1RWAtwOM6EyI9GU9XY2ea1cvA1 Jn/59YGlFaVfIE9alKQXBiO+m4jMx1cL5ou+/66NHcJV9/NyO91MkIrfMqpaZ2uQIx8q qRQ7XJtyvqKQRAm7M0lz61GDZSOH+60cpBHpYaAMNadICsyo3RoHKB2WXiNfCqirQPRk ShPg== X-Forwarded-Encrypted: i=1; AJvYcCUW/NZDk0zjh+O+gI21bIpuyD89rKBHKzY+3x0ddtLLpabCvmEuRjwNHOlmQNInAMlCCMzzadm0uZNHCCc=@vger.kernel.org X-Gm-Message-State: AOJu0YzOe+zkax3Uv0z3IWbp3i2AbeaDf8OQjEz9adHHhHzmvE+mz/kF knUCYhTDVZ6vHmRGrCAXdLIhoknP/yQ8SIXMe/XglwKq4trSQyG/GSe5qtSbLwEx5eeoHOU4gSt iPsxrWzPO/opO3KlpgDU3XA== X-Google-Smtp-Source: AGHT+IGglH1MwiuQb6nf3SblZvhLMFNMZKCm/U6KRL3GGVpCYBWH8kXimB5ATPz5Ij0DZlhI64+AMN+To08XeyX1Gw== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:11b:3898:ac11:fa18]) (user=coltonlewis job=sendgmr) by 2002:a25:a1a3:0:b0:e1a:6bf9:aa83 with SMTP id 3f1490d57ef6-e1d9db9e12fmr70673276.3.1726692842109; Wed, 18 Sep 2024 13:54:02 -0700 (PDT) Date: Wed, 18 Sep 2024 20:53:17 +0000 In-Reply-To: <20240918205319.3517569-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240918205319.3517569-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.662.g92d0881bb0-goog Message-ID: <20240918205319.3517569-5-coltonlewis@google.com> Subject: [PATCH v2 4/6] KVM: x86: selftests: Test read/write core counters From: Colton Lewis To: kvm@vger.kernel.org Cc: Mingwei Zhang , Jinrong Liang , Jim Mattson , Aaron Lewis , Sean Christopherson , Paolo Bonzini , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Colton Lewis Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Run a basic test to ensure we can write an arbitrary value to the core counters and read it back. Signed-off-by: Colton Lewis --- .../selftests/kvm/x86_64/pmu_counters_test.c | 54 +++++++++++++++++++ 1 file changed, 54 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 5b240585edc5..79ca7d608e00 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -641,11 +641,65 @@ static uint8_t nr_core_counters(void) return AMD_NR_CORE_EXT_COUNTERS; =20 return AMD_NR_CORE_COUNTERS; +} + +static uint8_t guest_nr_core_counters(void) +{ + uint8_t nr_counters =3D this_cpu_property(X86_PROPERTY_NUM_PERF_CTR_CORE); + bool core_ext =3D this_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + + if (nr_counters !=3D 0) + return nr_counters; + + if (core_ext) + return AMD_NR_CORE_EXT_COUNTERS; + + return AMD_NR_CORE_COUNTERS; + +} =20 +static void guest_test_rdwr_core_counters(void) +{ + bool core_ext =3D this_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + uint8_t nr_counters =3D guest_nr_core_counters(); + uint8_t i; + uint32_t esel_msr_base =3D core_ext ? MSR_F15H_PERF_CTL : MSR_K7_EVNTSEL0; + uint32_t cnt_msr_base =3D core_ext ? MSR_F15H_PERF_CTR : MSR_K7_PERFCTR0; + uint32_t msr_step =3D core_ext ? 2 : 1; + + for (i =3D 0; i < AMD_NR_CORE_EXT_COUNTERS; i++) { + uint64_t test_val =3D 0xffff; + uint32_t esel_msr =3D esel_msr_base + msr_step * i; + uint32_t cnt_msr =3D cnt_msr_base + msr_step * i; + bool expect_gp =3D !(i < nr_counters); + uint8_t vector; + uint64_t val; + + /* Test event selection register. */ + vector =3D wrmsr_safe(esel_msr, test_val); + GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, esel_msr, expect_gp, vector); + + vector =3D rdmsr_safe(esel_msr, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDMSR, esel_msr, expect_gp, vector); + + if (!expect_gp) + GUEST_ASSERT_PMC_VALUE(RDMSR, esel_msr, val, test_val); + + /* Test counter register. */ + vector =3D wrmsr_safe(cnt_msr, test_val); + GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, cnt_msr, expect_gp, vector); + + vector =3D rdmsr_safe(cnt_msr, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDMSR, cnt_msr, expect_gp, vector); + + if (!expect_gp) + GUEST_ASSERT_PMC_VALUE(RDMSR, cnt_msr, val, test_val); + } } =20 static void guest_test_core_counters(void) { + guest_test_rdwr_core_counters(); GUEST_DONE(); } =20 --=20 2.46.0.662.g92d0881bb0-goog