kvm.vger.kernel.org archive mirror
 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, drjones@redhat.com, dmatlack@google.com,
	Vishal Annapurve <vannapurve@google.com>
Subject: [V1 PATCH 3/5] selftests: kvm: x86: Execute vmcall/vmmcall according to CPU type
Date: Sat,  3 Sep 2022 01:28:47 +0000	[thread overview]
Message-ID: <20220903012849.938069-4-vannapurve@google.com> (raw)
In-Reply-To: <20220903012849.938069-1-vannapurve@google.com>

Modify following APIs for x86 implementation:
1) kvm_arch_main : Query the cpu vendor and cache the value in advance.
2) kvm_arch_post_vm_load: Populate cpu type in the guest memory so that
	guest doesn't need to execute cpuid instruction again.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Vishal Annapurve <vannapurve@google.com>
---
 .../testing/selftests/kvm/lib/x86_64/processor.c  | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index e22cfc4bf284..ac104653ab43 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -19,6 +19,7 @@
 #define MAX_NR_CPUID_ENTRIES 100
 
 vm_vaddr_t exception_handlers;
+static int is_cpu_amd = -1;
 
 static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
 {
@@ -1019,7 +1020,7 @@ static bool cpu_vendor_string_is(const char *vendor)
 
 bool is_intel_cpu(void)
 {
-	return cpu_vendor_string_is("GenuineIntel");
+	return (is_cpu_amd == 0);
 }
 
 /*
@@ -1027,7 +1028,7 @@ bool is_intel_cpu(void)
  */
 bool is_amd_cpu(void)
 {
-	return cpu_vendor_string_is("AuthenticAMD");
+	return (is_cpu_amd == 1);
 }
 
 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
@@ -1182,9 +1183,15 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
 {
 	uint64_t r;
 
-	asm volatile("vmcall"
+	if (is_amd_cpu())
+		asm volatile("vmmcall"
 		     : "=a"(r)
 		     : "a"(nr), "b"(a0), "c"(a1), "d"(a2), "S"(a3));
+	else
+		asm volatile("vmcall"
+		     : "=a"(r)
+		     : "a"(nr), "b"(a0), "c"(a1), "d"(a2), "S"(a3));
+
 	return r;
 }
 
@@ -1314,8 +1321,10 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm)
 
 void kvm_arch_main(void)
 {
+	is_cpu_amd = cpu_vendor_string_is("AuthenticAMD") ? 1 : 0;
 }
 
 void kvm_arch_post_vm_load(struct kvm_vm *vm)
 {
+	sync_global_to_guest(vm, is_cpu_amd);
 }
-- 
2.37.2.789.g6183377224-goog


  parent reply	other threads:[~2022-09-03  1:29 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-03  1:28 [V1 PATCH 0/5] Execute hypercalls from guests according to cpu type Vishal Annapurve
2022-09-03  1:28 ` [V1 PATCH 1/5] selftests: kvm: move common startup logic to kvm_util.c Vishal Annapurve
2022-09-08 21:09   ` David Matlack
2022-09-14 19:09     ` Vishal Annapurve
2022-09-03  1:28 ` [V1 PATCH 2/5] selftests: kvm: Introduce kvm_arch_main and helpers Vishal Annapurve
2022-09-05  7:46   ` Andrew Jones
2022-09-06 22:46     ` Vishal Annapurve
2022-09-08 21:21       ` David Matlack
2022-09-09  7:01         ` Andrew Jones
2022-09-14 19:13           ` Vishal Annapurve
2022-09-08 21:27   ` David Matlack
2022-09-03  1:28 ` Vishal Annapurve [this message]
2022-09-08 22:54   ` [V1 PATCH 3/5] selftests: kvm: x86: Execute vmcall/vmmcall according to CPU type David Matlack
2022-09-08 22:57     ` David Matlack
2022-09-14 19:12     ` Vishal Annapurve
2022-09-03  1:28 ` [V1 PATCH 4/5] selftests: kvm: delete svm_vmcall_test Vishal Annapurve
2022-09-03  1:28 ` [V1 PATCH 5/5] selftests: kvm: Execute vmcall/vmmcall as per cpu type 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=20220903012849.938069-4-vannapurve@google.com \
    --to=vannapurve@google.com \
    --cc=bgardon@google.com \
    --cc=dmatlack@google.com \
    --cc=drjones@redhat.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=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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).