All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, Vitaly Kuznetsov <vkuznets@redhat.com>,
	David Matlack <dmatlack@google.com>,
	Jim Mattson <jmattson@google.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 31/42] KVM: selftests: Set input function/index in raw CPUID helper(s)
Date: Sat,  4 Jun 2022 01:20:47 +0000	[thread overview]
Message-ID: <20220604012058.1972195-32-seanjc@google.com> (raw)
In-Reply-To: <20220604012058.1972195-1-seanjc@google.com>

Set the function/index for CPUID in the helper instead of relying on the
caller to do so.  In addition to reducing the risk of consuming an
uninitialized ECX, having the function/index embedded in the call makes
it easier to understand what is being checked.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 .../selftests/kvm/include/x86_64/processor.h  | 16 +++++++++++++---
 .../selftests/kvm/lib/x86_64/processor.c      | 13 ++++---------
 tools/testing/selftests/kvm/x86_64/amx_test.c | 19 ++++---------------
 .../testing/selftests/kvm/x86_64/cpuid_test.c | 11 +++++------
 4 files changed, 26 insertions(+), 33 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
index 6c14b34014c2..eb4730bf4e77 100644
--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
@@ -402,10 +402,13 @@ static inline void outl(uint16_t port, uint32_t value)
 	__asm__ __volatile__("outl %%eax, %%dx" : : "d"(port), "a"(value));
 }
 
-static inline void cpuid(uint32_t *eax, uint32_t *ebx,
-			 uint32_t *ecx, uint32_t *edx)
+static inline void __cpuid(uint32_t function, uint32_t index,
+			   uint32_t *eax, uint32_t *ebx,
+			   uint32_t *ecx, uint32_t *edx)
 {
-	/* ecx is often an input as well as an output. */
+	*eax = function;
+	*ecx = index;
+
 	asm volatile("cpuid"
 	    : "=a" (*eax),
 	      "=b" (*ebx),
@@ -415,6 +418,13 @@ static inline void cpuid(uint32_t *eax, uint32_t *ebx,
 	    : "memory");
 }
 
+static inline void cpuid(uint32_t function,
+			 uint32_t *eax, uint32_t *ebx,
+			 uint32_t *ecx, uint32_t *edx)
+{
+	return __cpuid(function, 0, eax, ebx, ecx, edx);
+}
+
 #define SET_XMM(__var, __xmm) \
 	asm volatile("movq %0, %%"#__xmm : : "r"(__var) : #__xmm)
 
diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index 41cc320d3e34..1d92a1d9a03f 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -1285,9 +1285,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
 
 	/* Before family 17h, the HyperTransport area is just below 1T.  */
 	ht_gfn = (1 << 28) - num_ht_pages;
-	eax = 1;
-	ecx = 0;
-	cpuid(&eax, &ebx, &ecx, &edx);
+	cpuid(1, &eax, &ebx, &ecx, &edx);
 	if (x86_family(eax) < 0x17)
 		goto done;
 
@@ -1296,18 +1294,15 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
 	 * reduced due to SME by bits 11:6 of CPUID[0x8000001f].EBX.  Use
 	 * the old conservative value if MAXPHYADDR is not enumerated.
 	 */
-	eax = 0x80000000;
-	cpuid(&eax, &ebx, &ecx, &edx);
+	cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
 	max_ext_leaf = eax;
 	if (max_ext_leaf < 0x80000008)
 		goto done;
 
-	eax = 0x80000008;
-	cpuid(&eax, &ebx, &ecx, &edx);
+	cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
 	max_pfn = (1ULL << ((eax & 0xff) - vm->page_shift)) - 1;
 	if (max_ext_leaf >= 0x8000001f) {
-		eax = 0x8000001f;
-		cpuid(&eax, &ebx, &ecx, &edx);
+		cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
 		max_pfn >>= (ebx >> 6) & 0x3f;
 	}
 
diff --git a/tools/testing/selftests/kvm/x86_64/amx_test.c b/tools/testing/selftests/kvm/x86_64/amx_test.c
index bcf535646321..866a42d07d75 100644
--- a/tools/testing/selftests/kvm/x86_64/amx_test.c
+++ b/tools/testing/selftests/kvm/x86_64/amx_test.c
@@ -122,9 +122,7 @@ static inline void check_cpuid_xsave(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 
-	eax = 1;
-	ecx = 0;
-	cpuid(&eax, &ebx, &ecx, &edx);
+	cpuid(1, &eax, &ebx, &ecx, &edx);
 	if (!(ecx & CPUID_XSAVE))
 		GUEST_ASSERT(!"cpuid: no CPU xsave support!");
 	if (!(ecx & CPUID_OSXSAVE))
@@ -140,10 +138,7 @@ static bool enum_xtile_config(void)
 {
 	u32 eax, ebx, ecx, edx;
 
-	eax = TILE_CPUID;
-	ecx = TILE_PALETTE_CPUID_SUBLEAVE;
-
-	cpuid(&eax, &ebx, &ecx, &edx);
+	__cpuid(TILE_CPUID, TILE_PALETTE_CPUID_SUBLEAVE, &eax, &ebx, &ecx, &edx);
 	if (!eax || !ebx || !ecx)
 		return false;
 
@@ -165,10 +160,7 @@ static bool enum_xsave_tile(void)
 {
 	u32 eax, ebx, ecx, edx;
 
-	eax = XSTATE_CPUID;
-	ecx = XFEATURE_XTILEDATA;
-
-	cpuid(&eax, &ebx, &ecx, &edx);
+	__cpuid(XSTATE_CPUID, XFEATURE_XTILEDATA, &eax, &ebx, &ecx, &edx);
 	if (!eax || !ebx)
 		return false;
 
@@ -183,10 +175,7 @@ static bool check_xsave_size(void)
 	u32 eax, ebx, ecx, edx;
 	bool valid = false;
 
-	eax = XSTATE_CPUID;
-	ecx = XSTATE_USER_STATE_SUBLEAVE;
-
-	cpuid(&eax, &ebx, &ecx, &edx);
+	__cpuid(XSTATE_CPUID, XSTATE_USER_STATE_SUBLEAVE, &eax, &ebx, &ecx, &edx);
 	if (ebx && ebx <= XSAVE_SIZE)
 		valid = true;
 
diff --git a/tools/testing/selftests/kvm/x86_64/cpuid_test.c b/tools/testing/selftests/kvm/x86_64/cpuid_test.c
index 2b8ac307da64..a4c4a5c5762a 100644
--- a/tools/testing/selftests/kvm/x86_64/cpuid_test.c
+++ b/tools/testing/selftests/kvm/x86_64/cpuid_test.c
@@ -31,10 +31,9 @@ static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)
 	u32 eax, ebx, ecx, edx;
 
 	for (i = 0; i < guest_cpuid->nent; i++) {
-		eax = guest_cpuid->entries[i].function;
-		ecx = guest_cpuid->entries[i].index;
-
-		cpuid(&eax, &ebx, &ecx, &edx);
+		__cpuid(guest_cpuid->entries[i].function,
+			guest_cpuid->entries[i].index,
+			&eax, &ebx, &ecx, &edx);
 
 		GUEST_ASSERT(eax == guest_cpuid->entries[i].eax &&
 			     ebx == guest_cpuid->entries[i].ebx &&
@@ -46,9 +45,9 @@ static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)
 
 static void test_cpuid_40000000(struct kvm_cpuid2 *guest_cpuid)
 {
-	u32 eax = 0x40000000, ebx, ecx = 0, edx;
+	u32 eax, ebx, ecx, edx;
 
-	cpuid(&eax, &ebx, &ecx, &edx);
+	cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
 
 	GUEST_ASSERT(eax == 0x40000001);
 }
-- 
2.36.1.255.ge46751e96f-goog


  parent reply	other threads:[~2022-06-04  1:23 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-04  1:20 [PATCH 00/42] KVM: selftests: Overhaul Part II - CPUID Sean Christopherson
2022-06-04  1:20 ` [PATCH 01/42] KVM: selftests: Set KVM's supported CPUID as vCPU's CPUID during recreate Sean Christopherson
2022-06-04  1:20 ` [PATCH 02/42] KVM: sefltests: Use CPUID_XSAVE and CPUID_OSXAVE instead of X86_FEATURE_* Sean Christopherson
2022-06-04  1:20 ` [PATCH 03/42] KVM: selftests: Add framework to query KVM CPUID bits Sean Christopherson
2022-06-04  1:20 ` [PATCH 04/42] KVM: selftests: Use kvm_cpu_has() in the SEV migration test Sean Christopherson
2022-06-04  1:20 ` [PATCH 05/42] KVM: selftests: Use kvm_cpu_has() for nested SVM checks Sean Christopherson
2022-06-04  1:20 ` [PATCH 06/42] KVM: selftests: Use kvm_cpu_has() for nested VMX checks Sean Christopherson
2022-06-04  1:20 ` [PATCH 07/42] KVM: selftests: Use kvm_cpu_has() to query PDCM in PMU selftest Sean Christopherson
2022-06-04  1:20 ` [PATCH 08/42] KVM: selftests: Drop redundant vcpu_set_cpuid() from " Sean Christopherson
2022-06-04  1:20 ` [PATCH 09/42] KVM: selftests: Use kvm_cpu_has() for XSAVES in XSS MSR test Sean Christopherson
2022-06-04  1:20 ` [PATCH 10/42] KVM: selftests: Check for _both_ XTILE data and cfg in AMX test Sean Christopherson
2022-06-04  1:20 ` [PATCH 11/42] KVM: selftests: Use kvm_cpu_has() " Sean Christopherson
2022-06-04  1:20 ` [PATCH 12/42] KVM: selftests: Use kvm_cpu_has() for XSAVE in cr4_cpuid_sync_test Sean Christopherson
2022-06-04  1:20 ` [PATCH 13/42] KVM: selftests: Remove the obsolete/dead MMU role test Sean Christopherson
2022-06-04  1:20 ` [PATCH 14/42] KVM: selftests: Use kvm_cpu_has() for KVM's PV steal time Sean Christopherson
2022-06-04  1:20 ` [PATCH 15/42] KVM: selftests: Use kvm_cpu_has() for nSVM soft INT injection test Sean Christopherson
2022-06-04  1:20 ` [PATCH 16/42] KVM: selftests: Verify that kvm_cpuid2.entries layout is unchanged by KVM Sean Christopherson
2022-06-04  1:20 ` [PATCH 17/42] KVM: selftests: Split out kvm_cpuid2_size() from allocate_kvm_cpuid2() Sean Christopherson
2022-06-04  1:20 ` [PATCH 18/42] KVM: selftests: Cache CPUID in struct kvm_vcpu Sean Christopherson
2022-06-04  1:20 ` [PATCH 19/42] KVM: selftests: Don't use a static local in vcpu_get_supported_hv_cpuid() Sean Christopherson
2022-06-04  1:20 ` [PATCH 20/42] KVM: selftests: Rename and tweak get_cpuid() to get_cpuid_entry() Sean Christopherson
2022-06-04  1:20 ` [PATCH 21/42] KVM: selftests: Use get_cpuid_entry() in kvm_get_supported_cpuid_index() Sean Christopherson
2022-06-04  1:20 ` [PATCH 22/42] KVM: selftests: Add helpers to get and modify a vCPU's CPUID entries Sean Christopherson
2022-06-04  1:20 ` [PATCH 23/42] KVM: selftests: Use vm->pa_bits to generate reserved PA bits Sean Christopherson
2022-06-04  1:20 ` [PATCH 24/42] KVM: selftests: Add and use helper to set vCPU's CPUID maxphyaddr Sean Christopherson
2022-06-04  1:20 ` [PATCH 25/42] KVM: selftests: Use vcpu_get_cpuid_entry() in PV features test (sort of) Sean Christopherson
2022-06-04  1:20 ` [PATCH 26/42] KVM: selftests: Use vCPU's CPUID directly in Hyper-V test Sean Christopherson
2022-06-04  1:20 ` [PATCH 27/42] KVM: selftests: Use vcpu_get_cpuid_entry() in CPUID test Sean Christopherson
2022-06-04  1:20 ` [PATCH 28/42] KVM: selftests: Use vcpu_{set,clear}_cpuid_feature() in nVMX state test Sean Christopherson
2022-06-04  1:20 ` [PATCH 29/42] KVM: selftests: Use vcpu_clear_cpuid_feature() to clear x2APIC Sean Christopherson
2022-06-04  1:20 ` [PATCH 30/42] KVM: selftests: Make get_supported_cpuid() returns "const" Sean Christopherson
2022-06-04  1:20 ` Sean Christopherson [this message]
2022-06-04  1:20 ` [PATCH 32/42] KVM: selftests: Add this_cpu_has() to query X86_FEATURE_* via cpuid() Sean Christopherson
2022-06-04  1:20 ` [PATCH 33/42] KVM: selftests: Use this_cpu_has() in CR4/CPUID sync test Sean Christopherson
2022-06-04  1:20 ` [PATCH 34/42] KVM: selftests: Use this_cpu_has() to detect SVM support in L1 Sean Christopherson
2022-06-04  1:20 ` [PATCH 35/42] KVM: selftests: Drop unnecessary use of kvm_get_supported_cpuid_index() Sean Christopherson
2022-06-04  1:20 ` [PATCH 36/42] KVM: selftests: Rename kvm_get_supported_cpuid_index() to __..._entry() Sean Christopherson
2022-06-04  1:20 ` [PATCH 37/42] KVM: selftests: Inline "get max CPUID leaf" helpers Sean Christopherson
2022-06-04  1:20 ` [PATCH 38/42] KVM: selftests: Check KVM's supported CPUID, not host CPUID, for XFD Sean Christopherson
2022-06-04  1:20 ` [PATCH 39/42] KVM: selftests: Skip AMX test if ARCH_REQ_XCOMP_GUEST_PERM isn't supported Sean Christopherson
2022-06-04  1:20 ` [PATCH 40/42] KVM: selftests: Clean up requirements for XFD-aware XSAVE features Sean Christopherson
2022-06-04  1:20 ` [PATCH 41/42] KVM: selftests: Use the common cpuid() helper in cpu_vendor_string_is() Sean Christopherson
2022-06-04  1:20 ` [PATCH 42/42] KVM: selftests: Drop unused SVM_CPUID_FUNC macro Sean Christopherson

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=20220604012058.1972195-32-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=dmatlack@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=vkuznets@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.