All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vishal Annapurve <vannapurve@google.com>
To: x86@kernel.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Cc: pbonzini@redhat.com, shuah@kernel.org, bgardon@google.com,
	seanjc@google.com, oupton@google.com, peterx@redhat.com,
	vkuznets@redhat.com, dmatlack@google.com, pgonda@google.com,
	andrew.jones@linux.dev, Vishal Annapurve <vannapurve@google.com>
Subject: [V3 PATCH 1/2] KVM: selftests: x86: Cache the cpu vendor type
Date: Thu, 22 Dec 2022 23:04:57 +0000	[thread overview]
Message-ID: <20221222230458.3828342-2-vannapurve@google.com> (raw)
In-Reply-To: <20221222230458.3828342-1-vannapurve@google.com>

Query cpuid once per guest selftest and store the cpu vendor type so that
subsequent queries can reuse the cached cpu vendor type.

Signed-off-by: Vishal Annapurve <vannapurve@google.com>
---
 .../selftests/kvm/lib/x86_64/processor.c      | 33 ++++++++++++++++---
 1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index 097cef430774..1e93bb3cb058 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -20,6 +20,9 @@
 
 vm_vaddr_t exception_handlers;
 
+static bool is_cpu_vendor_intel;
+static bool is_cpu_vendor_amd;
+
 static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
 {
 	fprintf(stream, "%*srax: 0x%.16llx rbx: 0x%.16llx "
@@ -1017,18 +1020,36 @@ void kvm_x86_state_cleanup(struct kvm_x86_state *state)
 	free(state);
 }
 
-static bool cpu_vendor_string_is(const char *vendor)
+static void check_cpu_vendor(void)
 {
-	const uint32_t *chunk = (const uint32_t *)vendor;
+	const char *intel_id = "GenuineIntel";
+	const uint32_t *intel_id_chunks = (const uint32_t *)intel_id;
+	const char *amd_id = "AuthenticAMD";
+	const uint32_t *amd_id_chunks = (const uint32_t *)amd_id;
+	static bool cpu_vendor_checked;
 	uint32_t eax, ebx, ecx, edx;
 
+	if (cpu_vendor_checked)
+		return;
+
 	cpuid(0, &eax, &ebx, &ecx, &edx);
-	return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]);
+
+	if (ebx == intel_id_chunks[0] && edx == intel_id_chunks[1] &&
+		ecx == intel_id_chunks[2])
+		is_cpu_vendor_intel = true;
+	else {
+		if (ebx == amd_id_chunks[0] && edx == amd_id_chunks[1] &&
+			ecx == amd_id_chunks[2])
+			is_cpu_vendor_amd = true;
+	}
+	cpu_vendor_checked = true;
 }
 
 bool is_intel_cpu(void)
 {
-	return cpu_vendor_string_is("GenuineIntel");
+	check_cpu_vendor();
+
+	return is_cpu_vendor_intel;
 }
 
 /*
@@ -1036,7 +1057,9 @@ bool is_intel_cpu(void)
  */
 bool is_amd_cpu(void)
 {
-	return cpu_vendor_string_is("AuthenticAMD");
+	check_cpu_vendor();
+
+	return is_cpu_vendor_amd;
 }
 
 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
-- 
2.39.0.314.g84b9a713c41-goog


  reply	other threads:[~2022-12-22 23:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-22 23:04 [V3 PATCH 0/2] Execute hypercalls from guests according to cpu Vishal Annapurve
2022-12-22 23:04 ` Vishal Annapurve [this message]
2022-12-23  0:24   ` [V3 PATCH 1/2] KVM: selftests: x86: Cache the cpu vendor type Sean Christopherson
2022-12-23  2:45     ` Vishal Annapurve
2022-12-22 23:04 ` [V3 PATCH 2/2] KVM: selftests: x86: Add native hypercall support Vishal Annapurve
2022-12-23  0:37   ` Sean Christopherson
2022-12-23 17:37 ` [V3 PATCH 0/2] Execute hypercalls from guests according to cpu Paolo Bonzini
2022-12-23 22:36   ` Vishal Annapurve

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=20221222230458.3828342-2-vannapurve@google.com \
    --to=vannapurve@google.com \
    --cc=andrew.jones@linux.dev \
    --cc=bgardon@google.com \
    --cc=dmatlack@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=oupton@google.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=pgonda@google.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=vkuznets@redhat.com \
    --cc=x86@kernel.org \
    /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.