From patchwork Tue Aug 13 16:42:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colton Lewis X-Patchwork-Id: 13762301 Received: from mail-il1-f202.google.com (mail-il1-f202.google.com [209.85.166.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 B506019EEB4 for ; Tue, 13 Aug 2024 16:43:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567397; cv=none; b=oX6SxByi/19gE2QPC+mouGxnvNJov6gNRdo6mEtnkLKevQn3H9YaCXKteYnj5R1pVQDzlwH7Z+3+DYWVTLYaPveUCKyogOb/1HB+1kBMGzn68/OUj5Btv34vAf25Ern7S6Z0w2j0Uin1q8ZDtRbTPQAFX+R6lY2862zXfmXWMbM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567397; c=relaxed/simple; bh=wp8ilGtEC5HoVPNr1Q63fyFx7A1XCnL/1USICxculdg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BUYZwLPwsfguwZHUtwHdZGRvSJBzREoFfYYIq0AYg1iO6YhqSgXt1/jeWFutiYHFJWsMEweUu97JIDr1jFBFSMA4NAx2XsQq6+jMmtldNdgCTHSlXIrVVA1LA0/kXq0si5ejRHcvoAK5DQMLBelk22FSOWVxUEK3OAR4mRRJhIo= 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=D4z3MiRZ; arc=none smtp.client-ip=209.85.166.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="D4z3MiRZ" Received: by mail-il1-f202.google.com with SMTP id e9e14a558f8ab-39b3245b20eso352995ab.1 for ; Tue, 13 Aug 2024 09:43:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723567395; x=1724172195; 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=mPGqkkq2aWYbtCMuKwdj8IcAE9Lnnrt5gTRInxMoxq0=; b=D4z3MiRZgRBJyccZgvgioIeaZ3cmrjCMQOS4nws5pEylP3Ppo+cWGGRr3Y7yaJKBTS KK4+W8pHBtvVdyp10VzMVU5sogJi+NnvUcV9Zgl5iXgtVm4yL+69kd25j7foC27tB8Iz RhaKIYriLikFbWEul2mJdfjtdD5kA4aMm5ry+BzunkdB3hmZv+N90mhzh+ccYGcbcBXy HSq/uJmFFULSe62uRHAHJ4GFquOY1PwuE8Hn86lOBoD/O9ib0KA/Eipj/Iu0am69BVdb vhlGwe3IPRu7MIhhOaCDVBOXhgHul1cKc/Kot7D8aXhAeCI9sb3C+Y725LrKaz/mIxM+ LBZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723567395; x=1724172195; 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=mPGqkkq2aWYbtCMuKwdj8IcAE9Lnnrt5gTRInxMoxq0=; b=vIeDFwdt+LJZhrim1oe4Uxxx4fHPlg/axzjVo6v4XgJC1DrXOvhf98QfQvzspIj84+ WuzgjckiyF+y3B0UTPM/jUcnX/I6pnzjjajBi6ArgA3ktVLf+iTXUBlixNdzDPwxssyg OKq+9SGrzn2rKQOKgggeQBC5/0Bkphmmj4wMwnFJgPMVntHHX+hb75D0LW3VgVuGrqKV vKG5f6fprIFLgoYg6fC3Zj/DXUY/MYuB0wM+j/omEhMPqewiKb2ZfmmL7/H0yHH8EZ81 6JOXtyhXirhCVb5CT9t0Zs5NZSEssrIyfgRB5CWoZRRhAocGH4kuYihugWCB90gJXize zvdQ== X-Forwarded-Encrypted: i=1; AJvYcCXaggRaB5ZX3nBHswvGk6cZhAA7qN+KtZOKgBAzbccGBCJ19A2DPH6HEXYRSDxVDvXJUnPGJTDj/QIKoeN2gO5+KFIPXPj9H/O7UJZ93sPJ X-Gm-Message-State: AOJu0Yyhi3nWo2ILRlwokFAAa9iPGvM8UMbOEBwsu9s+ZA2o5OFUzbyV ot18FRsowFsOxmSH0Xs8TW3u+pCWrTw7FjqQyHwMtg0Pfjco0ceuvMNEI75LypwS4lN3Z69/jpn X9Nz/D+dX5zEpB7ftKYIf4w== X-Google-Smtp-Source: AGHT+IGOK7b+04+ACDC0udDbVs6jtOtkFxc+ukVGF/fKymKvDK2AbE6djZbUj+9o0USFjjugygisisFgJDSnlR9fvg== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:14ce]) (user=coltonlewis job=sendgmr) by 2002:a92:ce0c:0:b0:39c:2cf2:c0f9 with SMTP id e9e14a558f8ab-39c48dd3987mr538985ab.3.1723567394939; Tue, 13 Aug 2024 09:43:14 -0700 (PDT) Date: Tue, 13 Aug 2024 16:42:39 +0000 In-Reply-To: <20240813164244.751597-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813164244.751597-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813164244.751597-2-coltonlewis@google.com> Subject: [PATCH 1/6] KVM: x86: selftests: Fix typos in macro variable use 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 Without the leading underscore, these variables are referencing a variable in the calling scope. It only worked before by accident because all calling scopes had a variable with the right name. Signed-off-by: Colton Lewis Reviewed-by: Mingwei Zhang --- tools/testing/selftests/kvm/x86_64/pmu_counters_test.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 698cb36989db..0e305e43a93b 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -174,7 +174,7 @@ do { \ #define GUEST_TEST_EVENT(_idx, _event, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \ do { \ - wrmsr(pmc_msr, 0); \ + wrmsr(_pmc_msr, 0); \ \ if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt .", FEP); \ @@ -331,9 +331,9 @@ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ expect_gp ? "#GP" : "no fault", msr, vector) \ #define GUEST_ASSERT_PMC_VALUE(insn, msr, val, expected) \ - __GUEST_ASSERT(val == expected_val, \ + __GUEST_ASSERT(val == expected, \ "Expected " #insn "(0x%x) to yield 0x%lx, got 0x%lx", \ - msr, expected_val, val); + msr, expected, val); static void guest_test_rdpmc(uint32_t rdpmc_idx, bool expect_success, uint64_t expected_val) From patchwork Tue Aug 13 16:42:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colton Lewis X-Patchwork-Id: 13762302 Received: from mail-il1-f201.google.com (mail-il1-f201.google.com [209.85.166.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 607661CA9F for ; Tue, 13 Aug 2024 16:43:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567397; cv=none; b=kZ40YHvjJHiyiiehULQ1DlJ6P2adby0A1Fm2N/HhBodg8NQ94A4KisT0Folaq98tuwPh+sPoznTb9g7eG/YttR5cduHhf3Otfe5ANSXu9IXXOMShNZcdLzuO+xMNgZsXIJdRcChPdORDxR9kpmOI71K6dZrLjPeYBOO8aZnJ+ro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567397; c=relaxed/simple; bh=+8Jotfh4jL/fBqNPpnZwk5SliEZbhTClP2DNYMZh29I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ioaqzeafZRciPGs2DmN0/aZ38dwuOiN0BEF+cKnbDCWw/32/OUd4WKd69jWm8NZ0gWQHoI+KtVCiuCWhp+PJRkcVPWUpr8okYKEHeRifLYK9LwXgHTNfSZ67uxSf7DV34fVEjKI+HFVSVZnhyq3YYJfViJKijjyX4OSFcksa2YY= 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=0o6diiGw; arc=none smtp.client-ip=209.85.166.201 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="0o6diiGw" Received: by mail-il1-f201.google.com with SMTP id e9e14a558f8ab-39915b8e08dso81603925ab.0 for ; Tue, 13 Aug 2024 09:43:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723567395; x=1724172195; 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=O4hjHwnogfh1FVF5nIyMDpPAAJmvxrrYhDd4vDZwnNI=; b=0o6diiGwJcbDZnsecGRejYYWJJq5uZL6FGcnkSpSBVeV89y4DNGAMxf2i9C6MbzIJ7 e4nsJDgRY1rBnERGfp6z01Ai5GHTcG7Ss3cXrlPp4ACfkQelQbvj+3euO9MS5SO6dZdA jQ0D9q7yl9x537VO64bVxmyGNsLl29yv5KAydNOTnTp5kgeQsdXx/84XUn5XuQReyc4h wUxzo9u1Jf3QhZwwJHSPHmk+NhhH9XgjKonnNDzmdV0lY2FAHbpqvom5jJGroTxW1RiL Xn0Uu3/5HSd/qf46rebeDAahH3Lea9H2hcvQAJ6VRKzbvpt7DvLx9ddujzOpYWZHYewO YFxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723567395; x=1724172195; 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=O4hjHwnogfh1FVF5nIyMDpPAAJmvxrrYhDd4vDZwnNI=; b=e7QAm+FppIkThdOmAG4tGufVja1qtRVnXyrdjspjeZoCY7wpG1xv3jUckj62hlODjc jj+Qlz6wCuoTIQ9bTBmdp358+55sM6RNwCBAUYHVdGD+ClVGmlho2GfJHX4bRS5jHGOC NqPGejnAwrE3eX4kmdnums/hWjGcQ1AF1P76ic9Hoe4qSwMHwX8RmRENbIe46aQo0HEa AtWc46Z4x649sfGvF7acHsVkW/+DQva81CtYsgJyTeM+R+6pvCEfUmjRVlpe6zhvrlLL USj6YC6oWrlg2b2f0cPTJgn22oSVbGjzuFXLNYuLC9E83DpJP75JuGfffpne8CrZ+xZN J4LA== X-Forwarded-Encrypted: i=1; AJvYcCUH8AymTZm1Rj7lnHzpjh87xGUB+9cUSC4I4xCpNFnOpXvi4hmSmINCIdTRFcNW+C8exRA6jSqPGlIeNumedU4=@vger.kernel.org X-Gm-Message-State: AOJu0YzWwmMK3i7dUmzMCYhke3JuEzVkK3ReP0OiYpccuC1vWTjluzQG lWwAJO8UhH0CPQziX30TLCggksyWOzc9hytLzny46oIjn+q3jp1g3gz67yP7fGm2jWQMdM/ciiw ZSJdghbJ9DCt07ezuZ79FpQ== X-Google-Smtp-Source: AGHT+IH9sFuwwZm2IzNPjr75f0WTGkljO5daIy5Ohio798p0uPQPFiZA8ni/huUlI+/Kd2HlFr4VhkfAuQWlzAKSFw== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:14ce]) (user=coltonlewis job=sendgmr) by 2002:a05:6e02:12e1:b0:39a:e800:eec9 with SMTP id e9e14a558f8ab-39d124ce491mr169325ab.4.1723567395730; Tue, 13 Aug 2024 09:43:15 -0700 (PDT) Date: Tue, 13 Aug 2024 16:42:40 +0000 In-Reply-To: <20240813164244.751597-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813164244.751597-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813164244.751597-3-coltonlewis@google.com> Subject: [PATCH 2/6] KVM: x86: selftests: Define AMD PMU CPUID leaves 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 This defined the CPUID calls to determine what extensions and properties are available. AMD reference manual names listed below. * PerfCtrExtCore (six core counters instead of four) * PerfCtrExtNB (four counters for northbridge events) * PerfCtrExtL2I (four counters for L2 cache events) * PerfMonV2 (support for registers to control multiple counters with a single register write) * LbrAndPmcFreeze (support for freezing last branch recorded stack on performance counter overflow) * NumPerfCtrCore (number of core counters) * NumPerfCtrNB (number of northbridge counters) Signed-off-by: Colton Lewis --- tools/testing/selftests/kvm/include/x86_64/processor.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index a0c1440017bb..9d87b5f8974f 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -183,6 +183,9 @@ struct kvm_x86_cpu_feature { #define X86_FEATURE_GBPAGES KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 26) #define X86_FEATURE_RDTSCP KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 27) #define X86_FEATURE_LM KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 29) +#define X86_FEATURE_PERF_CTR_EXT_CORE KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 23) +#define X86_FEATURE_PERF_CTR_EXT_NB KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 24) +#define X86_FEATURE_PERF_CTR_EXT_L2I KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 28) #define X86_FEATURE_INVTSC KVM_X86_CPU_FEATURE(0x80000007, 0, EDX, 8) #define X86_FEATURE_RDPRU KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 4) #define X86_FEATURE_AMD_IBPB KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 12) @@ -195,6 +198,8 @@ struct kvm_x86_cpu_feature { #define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16) #define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1) #define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3) +#define X86_FEATURE_PERF_MON_V2 KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 0) +#define X86_FEATURE_PERF_LBR_PMC_FREEZE KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 2) /* * KVM defined paravirt features. @@ -281,6 +286,8 @@ struct kvm_x86_cpu_property { #define X86_PROPERTY_GUEST_MAX_PHY_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 16, 23) #define X86_PROPERTY_SEV_C_BIT KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 0, 5) #define X86_PROPERTY_PHYS_ADDR_REDUCTION KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 6, 11) +#define X86_PROPERTY_NUM_PERF_CTR_CORE KVM_X86_CPU_PROPERTY(0x80000022, 0, EBX, 0, 3) +#define X86_PROPERTY_NUM_PERF_CTR_NB KVM_X86_CPU_PROPERTY(0x80000022, 0, EBX, 10, 15) #define X86_PROPERTY_MAX_CENTAUR_LEAF KVM_X86_CPU_PROPERTY(0xC0000000, 0, EAX, 0, 31) From patchwork Tue Aug 13 16:42:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colton Lewis X-Patchwork-Id: 13762303 Received: from mail-il1-f202.google.com (mail-il1-f202.google.com [209.85.166.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 A7FDE1A2C04 for ; Tue, 13 Aug 2024 16:43:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567399; cv=none; b=jhisWqCMHQXx1wUzUEiVXt6lI3MKSFN7MESBiuc8rjQjrr07a/meK3P5l0/m9qgDQBwtq1fIQk9SdbhY10s03UQ98FTwiGIp1LIrUWxhBR4x8ucPMGNJ6Ffg+U6ngZQ1dfU3Sx2x/ZX2vvwhtk4f3QH5feuU0DfBRWgEw3xpbQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567399; c=relaxed/simple; bh=v4fPQ3bf3I89lDkNGIzHum+PJC8y+wKwn575q9cvuzY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VyQoQ1xyO9bnZCkEwqQzGRYJvR0BkVnEYZU0l1GAxJ/fGTI0nLoG7BkPkesX7+p5fx6Ydodywl2hjs+PHRw5/JlxAGpozN8v29liw6610sgilslN5DHFI3J7i5JTqvMscPQ9N6JOitP7gi9Snx8zloGNT14d7cnDMCkQ2Ix5NEQ= 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=u2D+223B; arc=none smtp.client-ip=209.85.166.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="u2D+223B" Received: by mail-il1-f202.google.com with SMTP id e9e14a558f8ab-39b3245b20eso353195ab.1 for ; Tue, 13 Aug 2024 09:43:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723567397; x=1724172197; 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=sgL70S4+dG2Qyapd3eVQQ0HKCLvTkvReAD20xuDL9hw=; b=u2D+223BOzJcj3lHQT94BggUC+J/x+/lLEf/e700/ZVgWABGEfoBF3a8dC+XK2Gsfr 9OmoCFTrw7Hx5KvNOiugsX7v+dWtPYi5N0e1Ij2o6ZduBBkup4OWnFQ8gHE8U2m3LYOr 1bs5eMJaaVJYDhk8WhDqvULKIY5gcdDG/RvW3melPnbj4HFeCfl+fIeJJxXW+JOZhDZ4 c8+CztPJ5rLIl0h34su/CulEEYNkVMQLjsiT8NOjwZuTMt1pBPMex73AG6SW+85JXrh+ hupR3MF3/Le8rHk13XeXdog0ujIl08oz6WtMidwqGdpKFayuZBVvdz8dXctVPUc8mEyS f4BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723567397; x=1724172197; 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=sgL70S4+dG2Qyapd3eVQQ0HKCLvTkvReAD20xuDL9hw=; b=wGUb+EQc4LuzjFtHT4s+r/WB7ts9FnIHxCeGa92lB6X/sVJfQy44DxJMrx/ZsMYFj+ afUHlLX5s95ODu8R8xjUlxFwNEaM53XhVZ4CTzETucyG/tGeZanFWBrmhX/t4ukGWbzQ Dj1scvH71os5ubYX2v0nptwJ2yvrepGlrNxS+X+zUtS6WpqQeFac1pM3mLdTtWh00WeT MESZuz+NkDk35AwKtuG7lbqXU8hTZ0R1fqNimEkxOAyN455v2JnpbmOhgQD6AC1NEDDc Fft/54x0TJfpQPxJcwhZVuM33Q9G3CZMsdK01oyIhbjvN/FksiDvnQ/xJPef8zti+Q+t S8gw== X-Forwarded-Encrypted: i=1; AJvYcCVUdwn6pBXaEOf5/bMJVHaOX5N8LoP2k1NalNmOxxez8EXBYVG+flCLbCZADMgilkaFKvO4Rq6LVRgufV7J0/E+CWY1EwLf//NfVFI5fQOw X-Gm-Message-State: AOJu0Yy+mpsttSuYRdwUjxJSj9ifsUdPg1Jp5RZYeoVSpLmXhuhqMziR QCNLczNsecDG3d6WkNJlvK5fmepuNQ0hpaRs0bPRVRRmg/xlcoHrLAtuUGcDuUf/m2bB0L2UDF1 ydHL5wukpihyai6ySHholbg== X-Google-Smtp-Source: AGHT+IEMzS4g6YBWOuvRkL9fDIVltMciftLvn5LDHPHYvvupM7c70l0d4kLcwSXFjGfv1320K+mL4WWI1BSR3GYh6g== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:14ce]) (user=coltonlewis job=sendgmr) by 2002:a05:6e02:cb1:b0:376:3fad:bb7c with SMTP id e9e14a558f8ab-39c48bd96d4mr529865ab.1.1723567396876; Tue, 13 Aug 2024 09:43:16 -0700 (PDT) Date: Tue, 13 Aug 2024 16:42:41 +0000 In-Reply-To: <20240813164244.751597-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813164244.751597-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813164244.751597-4-coltonlewis@google.com> Subject: [PATCH 3/6] KVM: x86: selftests: Set up AMD VM in pmu_counters_test 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 Branch in main() depending on if the CPU is Intel or AMD. They are subject to vastly different requirements because the AMD PMU lacks many properties defined by the Intel PMU including the entire CPUID 0xa function where Intel stores all the PMU properties. AMD lacks this as well as any consistent notion of PMU versions as Intel does. Every feature is a separate flag and they aren't the same features as Intel. Set up a VM for testing core AMD counters and ensure proper CPUID features are set. Signed-off-by: Colton Lewis --- .../selftests/kvm/x86_64/pmu_counters_test.c | 80 ++++++++++++++++--- 1 file changed, 68 insertions(+), 12 deletions(-) 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 0e305e43a93b..a11df073331a 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -33,7 +33,7 @@ static uint8_t kvm_pmu_version; static bool kvm_has_perf_caps; -static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, +static struct kvm_vm *intel_pmu_vm_create(struct kvm_vcpu **vcpu, void *guest_code, uint8_t pmu_version, uint64_t perf_capabilities) @@ -303,7 +303,7 @@ static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities, if (!pmu_version) return; - vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_arch_events, + vm = intel_pmu_vm_create(&vcpu, guest_test_arch_events, pmu_version, perf_capabilities); vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH, @@ -463,7 +463,7 @@ static void test_gp_counters(uint8_t pmu_version, uint64_t perf_capabilities, struct kvm_vcpu *vcpu; struct kvm_vm *vm; - vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_gp_counters, + vm = intel_pmu_vm_create(&vcpu, guest_test_gp_counters, pmu_version, perf_capabilities); vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_GP_COUNTERS, @@ -530,7 +530,7 @@ static void test_fixed_counters(uint8_t pmu_version, uint64_t perf_capabilities, struct kvm_vcpu *vcpu; struct kvm_vm *vm; - vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_fixed_counters, + vm = intel_pmu_vm_create(&vcpu, guest_test_fixed_counters, pmu_version, perf_capabilities); vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK, @@ -627,18 +627,74 @@ static void test_intel_counters(void) } } -int main(int argc, char *argv[]) +static uint8_t nr_core_counters(void) { - TEST_REQUIRE(kvm_is_pmu_enabled()); + const uint8_t nr_counters = kvm_cpu_property(X86_PROPERTY_NUM_PERF_CTR_CORE); + const bool core_ext = kvm_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + /* The default numbers promised if the property is 0 */ + const uint8_t amd_nr_core_ext_counters = 6; + const uint8_t amd_nr_core_counters = 4; + + if (nr_counters != 0) + return nr_counters; + + if (core_ext) + return amd_nr_core_ext_counters; + + return amd_nr_core_counters; +} + +static void guest_test_core_counters(void) +{ + GUEST_DONE(); +} - TEST_REQUIRE(host_cpu_is_intel); - TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION)); - TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0); +static void test_core_counters(void) +{ + uint8_t nr_counters = nr_core_counters(); + bool core_ext = kvm_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + bool perf_mon_v2 = kvm_cpu_has(X86_FEATURE_PERF_MON_V2); + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; - kvm_pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION); - kvm_has_perf_caps = kvm_cpu_has(X86_FEATURE_PDCM); + vm = vm_create_with_one_vcpu(&vcpu, guest_test_core_counters); - test_intel_counters(); + /* This property may not be there in older underlying CPUs, + * but it simplifies the test code for it to be set + * unconditionally. + */ + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_NUM_PERF_CTR_CORE, nr_counters); + if (core_ext) + vcpu_set_cpuid_feature(vcpu, X86_FEATURE_PERF_CTR_EXT_CORE); + if (perf_mon_v2) + vcpu_set_cpuid_feature(vcpu, X86_FEATURE_PERF_MON_V2); + + pr_info("Testing core counters: CoreExt = %u, PerfMonV2 = %u, NumCounters = %u\n", + core_ext, perf_mon_v2, nr_counters); + run_vcpu(vcpu); + + kvm_vm_free(vm); +} + +static void test_amd_counters(void) +{ + test_core_counters(); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_is_pmu_enabled()); + + if (host_cpu_is_intel) { + TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION)); + TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0); + kvm_pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION); + kvm_has_perf_caps = kvm_cpu_has(X86_FEATURE_PDCM); + test_intel_counters(); + } else if (host_cpu_is_amd) { + /* AMD CPUs don't have the same properties to look at. */ + test_amd_counters(); + } return 0; } From patchwork Tue Aug 13 16:42:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colton Lewis X-Patchwork-Id: 13762304 Received: from mail-io1-f74.google.com (mail-io1-f74.google.com [209.85.166.74]) (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 9F1981A2C38 for ; Tue, 13 Aug 2024 16:43:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567400; cv=none; b=Q+fdOERwJBWRaK/kvrvgpzyQ136+AfmiQgBGhZjXAJnN0FpGv/ETNRfWXPY17HqZibaGkz9Qfg4e7qISHyDDzlzFLucBXcs5Gcr8WNJpfxc6s7Mwd4T38NN0FEf8Zu1uJzW9xXnhesfBY3qccmmHS4AWFG6q9hA8rafMKebAws4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567400; c=relaxed/simple; bh=EmQwFVXc+If+aLGuw6XYo0hnZj9qP7zy1jdY/Fa8mXY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Wym+0uh50M6SRAEuFJfW3/B6IvEQZOA39eoc2cnE3OvbQ9AysNotVhqtHRBYdmeXw/o8axzTKKqboABxO/ByMkdroOLAnKLgOjpcaH5Y88axtdgdvc+ItqhPrBAJoAdp0S2cWgLaEeGTbrwPEcLA1CLU55GpLlz1HLKx4a1qyaY= 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=wgq/oa8C; arc=none smtp.client-ip=209.85.166.74 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="wgq/oa8C" Received: by mail-io1-f74.google.com with SMTP id ca18e2360f4ac-824cad19812so152010539f.1 for ; Tue, 13 Aug 2024 09:43:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723567398; x=1724172198; 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=KMXWLKxA64xiZoChdgelcLbcEELaiOp5iXK2pYOqGww=; b=wgq/oa8CzpznRkJ3fQlssZ+hpHCRLMtN4Ur7G5L9St59BxI4w5TrGXBzmcvtmZziQX xmzYW+KG5wNDl5B2acbzvPkQMans45M/50ahgEa6mE5B+akdAiYJEvEsj0s58/BeqElY v7+Wu7U5Khep1/SyWVBLZblkHIRwkj2ivrkvdFqyzmdd+JifD7bT7HPI80S802U1uNks ScUp9SpBlUcxEP+gf//yr7I1zyLL2NFQupBxdX8BquV0hTGDIOPi827QekYPnNJMH8SI +AEVu4DDv5g3b4L9jO0tooRVtn26eVglZBnTZQqjh1E4s71lmHh/eKhklKmUNAxsDI12 S5rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723567398; x=1724172198; 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=KMXWLKxA64xiZoChdgelcLbcEELaiOp5iXK2pYOqGww=; b=l9JS7w4bCjpS0mk8B2fL2lzZtFgZec89aZWEBjPUVcaROF+1XNq6GqqzXOhzbNjuyc 9qVcXIf9o7eLD467EznXxix10lZg8rEogCl17ycpa0wYMXieYUA0QgWvACgvkVl10VE4 sR76Dt7wce9pccm8XBivX/D0KcCrF2bSiXrKnR16pZSk2rpE/XjJQMr+aAgF+NIHn4+h iXb66yT3tbI8ED2r1MtccXXfle+4EbkT6JOyJC38EeQHawYg7INsvptBrngu6crras8c LPM2DdcOEn1fq39Eu2SqWTpDFWhCxFk0XGoz+wJoNMG0ffgyxxRptwx/Bo1XHfUtNukw RyfA== X-Forwarded-Encrypted: i=1; AJvYcCXgu7NG5isYyV+O5RlwZzbJF3DaIjugyWAvZlqp6upJykCeoegSOh8l+NtPGT21aNZD03ClITNg33ErkIbyjeFwrFKmAr2pG/xZ0FnIKsKW X-Gm-Message-State: AOJu0YzyTN102nL/5Q0po7LsvALzwEZQnqGzvy9eHnAdXvYlHLZNvfg+ 5MiGACCCwZfc9sfnaWTs5C7MZj4VefyNinUryeqdg4+G83XD1DUM8ugdG4xugZWH2oEMHRCI0/b ZpHMeCySS5f/p701EUCyu/A== X-Google-Smtp-Source: AGHT+IHZlNl2VWw0yLBBiEzDHzM6lXzhOGB9jiE+hED44NQa7g5LUmhhs7tT6Z7SIWGU3cSf3l3odnU3v1xRfdLb5A== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:14ce]) (user=coltonlewis job=sendgmr) by 2002:a05:6602:1602:b0:809:b914:a53a with SMTP id ca18e2360f4ac-824dabf020fmr282939f.0.1723567397854; Tue, 13 Aug 2024 09:43:17 -0700 (PDT) Date: Tue, 13 Aug 2024 16:42:42 +0000 In-Reply-To: <20240813164244.751597-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813164244.751597-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813164244.751597-5-coltonlewis@google.com> Subject: [PATCH 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 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 | 41 +++++++++++++++++++ 1 file changed, 41 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 a11df073331a..9620fc33d26e 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -324,6 +324,7 @@ static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities, */ #define MAX_NR_GP_COUNTERS 8 #define MAX_NR_FIXED_COUNTERS 3 +#define MAX_NR_CORE_COUNTERS 6 #define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector) \ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ @@ -644,8 +645,48 @@ static uint8_t nr_core_counters(void) return amd_nr_core_counters; } +static void guest_test_rdwr_core_counters(void) +{ + bool core_ext = this_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + uint8_t nr_counters = this_cpu_property(X86_PROPERTY_NUM_PERF_CTR_CORE); + uint8_t i; + uint32_t esel_msr_base = core_ext ? MSR_F15H_PERF_CTL : MSR_K7_EVNTSEL0; + uint32_t cnt_msr_base = core_ext ? MSR_F15H_PERF_CTR : MSR_K7_PERFCTR0; + uint32_t msr_step = core_ext ? 2 : 1; + + for (i = 0; i < MAX_NR_CORE_COUNTERS; i++) { + uint64_t test_val = 0xffff; + uint32_t esel_msr = esel_msr_base + msr_step * i; + uint32_t cnt_msr = cnt_msr_base + msr_step * i; + bool expect_gp = !(i < nr_counters); + uint8_t vector; + uint64_t val; + + /* Test event selection register. */ + vector = wrmsr_safe(esel_msr, test_val); + GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, esel_msr, expect_gp, vector); + + vector = 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 = wrmsr_safe(cnt_msr, test_val); + GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, cnt_msr, expect_gp, vector); + + vector = 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); + } +} + static void guest_test_core_counters(void) { + guest_test_rdwr_core_counters(); GUEST_DONE(); } From patchwork Tue Aug 13 16:42:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colton Lewis X-Patchwork-Id: 13762305 Received: from mail-il1-f201.google.com (mail-il1-f201.google.com [209.85.166.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 5B85E1A38D7 for ; Tue, 13 Aug 2024 16:43:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567401; cv=none; b=U05YXZm9x45jiUF+/ZgQnjWQCd9FCFJHyQyOc8X8nEqh/MjaRtoDEfWTcIr+MF1RC+yfnBmykXWh5BhyLzvNwMjEqsyWj+2Z2h2QE8jRE2yjl5pvam4RIEQSD3rW/upsm1XR0502XMFyrei36s/ea4a3MuiCHKeGK7B1YP0wgCo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567401; c=relaxed/simple; bh=BzUrNU2ZraWOWTk3Iv7hgsDb8BFWgPE59A3h3IKeVYE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FUXfy5plI1GC8c7MxWtnEd2obM2oY7XgEQQd64hwq3aQPmiVquqtzE0aKrN/8ruzrdYcfDzSICaWPSi0KbhktLDUPfOkU4vx5OOCUjF+bAWmXRsW5E1E0WiWxN/OImpDPvwcBh8A1kUjTD9ShHiqo4g53+yLdWkXuuhHBXD7RiE= 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=VrzOUOEH; arc=none smtp.client-ip=209.85.166.201 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="VrzOUOEH" Received: by mail-il1-f201.google.com with SMTP id e9e14a558f8ab-39915b8e08dso81604925ab.0 for ; Tue, 13 Aug 2024 09:43:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723567398; x=1724172198; 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=IzizhsPZKqLhOYK5bcLEvi2PU+wjRDK8IZyA0qEp6YM=; b=VrzOUOEHmrqS26UeI9aa6DFkSDvoY7PrSBtJ5meZaZ2yinAohhuuSd7PTPYpu941Pw gaQkL/r6sDh/3sHyBvQxsbNaoLZ2CIl9VMbrTo3/OeOMiBXYSZAGeJz4gzhMyVDqb1Uq JEftC4PBbmPPlsEPdGvf2cHvWx5J2UA1V/t6OlvTlt/af7EaB32AKZ/mJh6V6bWZGcQH W0CgztozWTeCXQ9CFU//NaUEiBOxCcd66lbGgefz1lMf5RlKXRTvR9Zu5bT+laCyL9ot EMp40RvFYVs2wKs/m3HWUzzXE0b6IljU4g6bEfUN+DSzRRFix8BbxF7ocnxuBHFCBq34 80nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723567398; x=1724172198; 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=IzizhsPZKqLhOYK5bcLEvi2PU+wjRDK8IZyA0qEp6YM=; b=q/Og0AdywGg4MM+1rWLpd//WPlL6kiE2AEJZVtSbOb/8pzIN/LzP1yV8ieLEgTIIPY Nk5lv8oEUStb1VdqRkJ2vj7Gf+ALn7pQ7pJvaG0g4+mKgbVqfRX9juLdZa+EablvilsP +Ti6JpfPPk0cW5f1VmWVKfCBcjoPbEPbL1JLg1MYQ4RVxDd7sg0lN3xUofGMGnqRlpvV HY6Az/BtqSABtsvdwMWszZ1c3D4H5oBW5zxij7vxzT/KC7LDbqwg3WT6PBxoIL5uEGd5 oK5jff6KYVpjhTVlMP4Rv7o6Jzg40RmcWPjh6ZZ+Y7eESxjIJ9jTsdiVP318wtrqRYJS 0weA== X-Forwarded-Encrypted: i=1; AJvYcCXLtykY4m80SGPYua+1wglAdxYzoh2tauntTcIF4qx8DX0VOheLWCUlCSf/cmVQKQtRhvduj0I8PFEpiVFIPe3seMPONakfW6B4jHSuc1Ny X-Gm-Message-State: AOJu0Yz52gDzXDsPGzydhQDK7OBU84yr+XIjLSegMPtU7zV2CUrF+J9a 2Lo17xTFbQHMn4w5GBVIEp6H0i8C5EpSCHdWAoYdHkCTp2Fc0N8uRLG6uiR73YJNec3xyN2PiTG aoKx6XNl+bwCmXpNPsPGYIw== X-Google-Smtp-Source: AGHT+IGPy3YYdvJsVhd7Cm67IXVaqYPxT1CYYkGdmWcZNua9lh4lwNUJwrMH4rshgZzNfM+CSzuFOiFv8IYTpJVxUQ== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:14ce]) (user=coltonlewis job=sendgmr) by 2002:a05:6e02:b46:b0:397:fa4e:3df0 with SMTP id e9e14a558f8ab-39d124c23bbmr102925ab.3.1723567398720; Tue, 13 Aug 2024 09:43:18 -0700 (PDT) Date: Tue, 13 Aug 2024 16:42:43 +0000 In-Reply-To: <20240813164244.751597-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813164244.751597-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813164244.751597-6-coltonlewis@google.com> Subject: [PATCH 5/6] KVM: x86: selftests: Test core events 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 Test events on core counters by iterating through every combination of events in amd_pmu_zen_events with every core counter. For each combination, calculate the appropriate register addresses for the event selection/control register and the counter register. The base addresses and layout schemes change depending on whether we have the CoreExt feature. To do the testing, reuse GUEST_TEST_EVENT to run a standard known workload. Decouple it from guest_assert_event_count (now guest_assert_intel_event_count) to generalize to AMD. Then assert the most specific detail that can be reasonably known about the counter result. Exact count is defined and known for some events and for other events merely asserted to be nonzero. Note on exact counts: AMD counts one more branch than Intel for the same workload. Though I can't confirm a reason, the only thing it could be is the boundary of the loop instruction being counted differently. Presumably, when the counter reaches 0 and execution continues to the next instruction, AMD counts this as a branch and Intel doesn't. Signed-off-by: Colton Lewis --- .../selftests/kvm/x86_64/pmu_counters_test.c | 87 ++++++++++++++++--- 1 file changed, 77 insertions(+), 10 deletions(-) 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 9620fc33d26e..fae078b444b3 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -29,6 +29,9 @@ /* Total number of instructions retired within the measured section. */ #define NUM_INSNS_RETIRED (NUM_LOOPS * NUM_INSNS_PER_LOOP + NUM_EXTRA_INSNS) +/* AMD counting one extra branch. Probably at loop boundary condition. */ +#define NUM_BRANCH_INSNS_RETIRED_AMD (NUM_LOOPS+1) +#define NUM_INSNS_RETIRED_AMD (NUM_INSNS_RETIRED+1) static uint8_t kvm_pmu_version; static bool kvm_has_perf_caps; @@ -98,7 +101,7 @@ static uint8_t guest_get_pmu_version(void) * Sanity check that in all cases, the event doesn't count when it's disabled, * and that KVM correctly emulates the write of an arbitrary value. */ -static void guest_assert_event_count(uint8_t idx, +static void guest_assert_intel_event_count(uint8_t idx, struct kvm_x86_pmu_feature event, uint32_t pmc, uint32_t pmc_msr) { @@ -140,6 +143,33 @@ static void guest_assert_event_count(uint8_t idx, GUEST_ASSERT_EQ(_rdpmc(pmc), 0xdead); } +static void guest_assert_amd_event_count(uint8_t evt_idx, uint8_t cnt_idx, uint32_t pmc_msr) +{ + uint64_t count; + uint64_t count_pmc; + + count = rdmsr(pmc_msr); + count_pmc = _rdpmc(cnt_idx); + GUEST_ASSERT_EQ(count, count_pmc); + + switch (evt_idx) { + case AMD_ZEN_CORE_CYCLES_INDEX: + GUEST_ASSERT_NE(count, 0); + break; + case AMD_ZEN_INSTRUCTIONS_INDEX: + GUEST_ASSERT_EQ(count, NUM_INSNS_RETIRED_AMD); + break; + case AMD_ZEN_BRANCHES_INDEX: + GUEST_ASSERT_EQ(count, NUM_BRANCH_INSNS_RETIRED_AMD); + break; + case AMD_ZEN_BRANCH_MISSES_INDEX: + GUEST_ASSERT_NE(count, 0); + break; + default: + break; + } + +} /* * Enable and disable the PMC in a monolithic asm blob to ensure that the * compiler can't insert _any_ code into the measured sequence. Note, ECX @@ -172,28 +202,29 @@ do { \ ); \ } while (0) -#define GUEST_TEST_EVENT(_idx, _event, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \ +#define GUEST_TEST_EVENT(_pmc_msr, _ctrl_msr, _ctrl_value, FEP) \ do { \ wrmsr(_pmc_msr, 0); \ \ if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt .", FEP); \ + GUEST_MEASURE_EVENT(_ctrl_msr, _ctrl_value, "clflushopt .", FEP); \ else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush .", FEP); \ + GUEST_MEASURE_EVENT(_ctrl_msr, _ctrl_value, "clflush .", FEP); \ else \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", FEP); \ - \ - guest_assert_event_count(_idx, _event, _pmc, _pmc_msr); \ + GUEST_MEASURE_EVENT(_ctrl_msr, _ctrl_value, "nop", FEP); \ } while (0) static void __guest_test_arch_event(uint8_t idx, struct kvm_x86_pmu_feature event, uint32_t pmc, uint32_t pmc_msr, uint32_t ctrl_msr, uint64_t ctrl_msr_value) { - GUEST_TEST_EVENT(idx, event, pmc, pmc_msr, ctrl_msr, ctrl_msr_value, ""); + GUEST_TEST_EVENT(pmc_msr, ctrl_msr, ctrl_msr_value, ""); + guest_assert_intel_event_count(idx, event, pmc, pmc_msr); - if (is_forced_emulation_enabled) - GUEST_TEST_EVENT(idx, event, pmc, pmc_msr, ctrl_msr, ctrl_msr_value, KVM_FEP); + if (is_forced_emulation_enabled) { + GUEST_TEST_EVENT(pmc_msr, ctrl_msr, ctrl_msr_value, KVM_FEP); + guest_assert_intel_event_count(idx, event, pmc, pmc_msr); + } } #define X86_PMU_FEATURE_NULL \ @@ -684,9 +715,45 @@ static void guest_test_rdwr_core_counters(void) } } +static void __guest_test_core_event(uint8_t event_idx, uint8_t counter_idx) +{ + /* One fortunate area of actual compatibility! This register + * layout is the same for both AMD and Intel. + */ + uint64_t eventsel = ARCH_PERFMON_EVENTSEL_OS | + ARCH_PERFMON_EVENTSEL_ENABLE | + amd_pmu_zen_events[event_idx]; + bool core_ext = this_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + uint64_t esel_msr_base = core_ext ? MSR_F15H_PERF_CTL : MSR_K7_EVNTSEL0; + uint64_t cnt_msr_base = core_ext ? MSR_F15H_PERF_CTR : MSR_K7_PERFCTR0; + uint64_t msr_step = core_ext ? 2 : 1; + uint64_t esel_msr = esel_msr_base + msr_step * counter_idx; + uint64_t cnt_msr = cnt_msr_base + msr_step * counter_idx; + + GUEST_TEST_EVENT(cnt_msr, esel_msr, eventsel, ""); + guest_assert_amd_event_count(event_idx, counter_idx, cnt_msr); + + if (is_forced_emulation_enabled) { + GUEST_TEST_EVENT(cnt_msr, esel_msr, eventsel, KVM_FEP); + guest_assert_amd_event_count(event_idx, counter_idx, cnt_msr); + } + +} + +static void guest_test_core_events(void) +{ + uint8_t nr_counters = this_cpu_property(X86_PROPERTY_NUM_PERF_CTR_CORE); + + for (uint8_t i = 0; i < NR_AMD_ZEN_EVENTS; i++) { + for (uint8_t j = 0; j < nr_counters; j++) + __guest_test_core_event(i, j); + } +} + static void guest_test_core_counters(void) { guest_test_rdwr_core_counters(); + guest_test_core_events(); GUEST_DONE(); } From patchwork Tue Aug 13 16:42:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colton Lewis X-Patchwork-Id: 13762306 Received: from mail-il1-f202.google.com (mail-il1-f202.google.com [209.85.166.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 3F27D1A38FD for ; Tue, 13 Aug 2024 16:43:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567401; cv=none; b=NmFiKp0pp3/eTETPrTgp2Ko/RKb+DqYjsVDNnuBAYrcj/chKVkZY9SctV+Kx7J0PGRfSP1DNqS8FdMj7W1tnhnPKOM7oE75Kbjsv1+NkLO3rhp935qPf/Dlkxpx2+sJwaEYDOxsMxeN+BvDsSWys/T2nBZMoLe63xKGUKOQ4Lhk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723567401; c=relaxed/simple; bh=hbna4WT6ykCjoJ+xsQHxPsp0+nTsv9npl8xnbl9uBlQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RRe0PH5AShOrhtV8M/plxTZMzXmygO2pM2tkVA+b8aJ+uCf5fBze50dtvSJUyf5GqEm8ANJ+gLXYR1n16oWFGanDYCDwcLtfqiSNPZDuak2OprLunKZ32l7/g9W1io62zisTA5fxAEhjgq3mVGaJtcx36rep/T6GqYuXqkGIhzg= 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=rOFsY0Cb; arc=none smtp.client-ip=209.85.166.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="rOFsY0Cb" Received: by mail-il1-f202.google.com with SMTP id e9e14a558f8ab-39b349a0234so532955ab.0 for ; Tue, 13 Aug 2024 09:43:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723567399; x=1724172199; 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=sxu2u9J8ROI353JNXDar5TUy9JdYSVQrvfs197UIlhk=; b=rOFsY0CbrKWoExUJ7k1S/esI+MCxXotPFCIVRATYPJREzR3Qqg/Ni/ZG2NdIeEqV2d LkLwJse4o/Tq6QXq0QUATpBToMyhuF6jSsjY3Vlk/98w0NCA1YzPawbSrHwBvdHLDqFE uCuHHEEpSv7BewGMXEy3Nm1P85v7tQXQeUBT+kzHUgPMn8qORkdG2Pc5SMDnsFezGR8T 4jduXxv07An/hvjfiw0CvCofgGx9A9RvDhq3tb+rs/CWgyspM44NQUKCgw1n9GoTGI7t EeZxebzTckK6nM67levHLVB89oLKW3YjF5sqUyzHpA0RoirzM1zHMRK1iJIi3UfqDnLO ydJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723567399; x=1724172199; 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=sxu2u9J8ROI353JNXDar5TUy9JdYSVQrvfs197UIlhk=; b=K4M7YncEA71m4gdIJ2jtWc5m1p6EkpHbdIYfYmUGoZ+i9czNT4KzVrh7hKXmzfels8 t75OeVEUPjt2r4QUTf1OqPVZ4cBQgjGwAyPAtrab9htgQCfqSfExFuAFAn4IxVB0FqfN oHIqBGLhAYk5p4UIzk2MPOKrm38vFmLSeDYjNIQSOovmVeTrUFBNKK+/2k2LF6IZRb5G 4mPZ2lla4QhE/+I+Mj7k7hCh/rTEKjHRbUG/PcKIXOalqn4GLuFNUp2r8IjjKEQQiEaJ shqnGkFIN5rX8JITdPZ3ACnt64CteE7B1CjPypIQcY0xh89dPphA+4j0MEORDN/TplBF Dacw== X-Forwarded-Encrypted: i=1; AJvYcCWhugBmuK30FvKwX/7CjYIBqRUHNGxNs9iP5IbwpYZAxeLzNYmQvdPn3CeiwW6FZblPbRqIu2zZzD9uVSNoPeDLz4FE7UmdxvUtzNlAhWga X-Gm-Message-State: AOJu0YyBCCc1IHjT+q7Us6nmOPvrf3ZpjREyDhstmHLkC2Fni2p9gvqA tzYHTdtu9ElaDOKCS536FMfR3Y7nkLV+s1MkKeOGK9XTLi+EeNWkUaZ/83OrYDVG4RZDbyWFW2U 0SRZ1aODUvNM716dhbAV+PQ== X-Google-Smtp-Source: AGHT+IElI/1oLpRVwVZMfLBr3yjf0cJI9FMrVKLvemCl2STD5dQWccho2EdHMGhA6wjGDeXQ60Glka2UTwaKewegew== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:14ce]) (user=coltonlewis job=sendgmr) by 2002:a05:6638:9818:b0:4c2:31f5:3137 with SMTP id 8926c6da1cb9f-4caaf459bcfmr5907173.0.1723567399664; Tue, 13 Aug 2024 09:43:19 -0700 (PDT) Date: Tue, 13 Aug 2024 16:42:44 +0000 In-Reply-To: <20240813164244.751597-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813164244.751597-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813164244.751597-7-coltonlewis@google.com> Subject: [PATCH 6/6] KVM: x86: selftests: Test PerfMonV2 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 Test PerfMonV2, which defines global registers to enable multiple performance counters with a single MSR write, in its own function. If the feature is available, ensure the global control register has the ability to start and stop the performance counters and the global status register correctly flags an overflow by the associated counter. Signed-off-by: Colton Lewis --- .../selftests/kvm/x86_64/pmu_counters_test.c | 53 +++++++++++++++++++ 1 file changed, 53 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 fae078b444b3..a6aa37ee460a 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -750,10 +750,63 @@ static void guest_test_core_events(void) } } +static void guest_test_perf_mon_v2(void) +{ + uint64_t i; + uint64_t eventsel = ARCH_PERFMON_EVENTSEL_OS | + ARCH_PERFMON_EVENTSEL_ENABLE | + AMD_ZEN_CORE_CYCLES; + bool core_ext = this_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + uint64_t sel_msr_base = core_ext ? MSR_F15H_PERF_CTL : MSR_K7_EVNTSEL0; + uint64_t cnt_msr_base = core_ext ? MSR_F15H_PERF_CTR : MSR_K7_PERFCTR0; + uint64_t msr_step = core_ext ? 2 : 1; + uint8_t nr_counters = this_cpu_property(X86_PROPERTY_NUM_PERF_CTR_CORE); + bool perf_mon_v2 = this_cpu_has(X86_FEATURE_PERF_MON_V2); + uint64_t sel_msr; + uint64_t cnt_msr; + + if (!perf_mon_v2) + return; + + for (i = 0; i < nr_counters; i++) { + sel_msr = sel_msr_base + msr_step * i; + cnt_msr = cnt_msr_base + msr_step * i; + + /* Ensure count stays 0 when global register disables counter. */ + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, 0); + wrmsr(sel_msr, eventsel); + wrmsr(cnt_msr, 0); + __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS})); + GUEST_ASSERT(!_rdpmc(i)); + + /* Ensure counter is >0 when global register enables counter. */ + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, BIT_ULL(i)); + __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS})); + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, 0); + GUEST_ASSERT(_rdpmc(i)); + + /* Ensure global status register flags a counter overflow. */ + wrmsr(cnt_msr, -1); + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR, 0xff); + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, BIT_ULL(i)); + __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS})); + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, 0); + GUEST_ASSERT(rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS) & + BIT_ULL(i)); + + /* Ensure global status register flag is cleared correctly. */ + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR, BIT_ULL(i)); + GUEST_ASSERT(!(rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS) & + BIT_ULL(i))); + } +} + + static void guest_test_core_counters(void) { guest_test_rdwr_core_counters(); guest_test_core_events(); + guest_test_perf_mon_v2(); GUEST_DONE(); }