mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vipin Sharma <vipinsh@google.com>
To: seanjc@google.com, pbonzini@redhat.com, vkuznets@redhat.com
Cc: dmatlack@google.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, Vipin Sharma <vipinsh@google.com>
Subject: [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace
Date: Fri,  4 Nov 2022 21:57:04 -0700	[thread overview]
Message-ID: <20221105045704.2315186-7-vipinsh@google.com> (raw)
In-Reply-To: <20221105045704.2315186-1-vipinsh@google.com>

Hyper-V extended hypercalls by default exit to userspace. Verify
userspace gets the call, update the result and then guest verifies
result it received.

Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
 tools/testing/selftests/kvm/.gitignore        |  1 +
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../kvm/x86_64/hyperv_extended_hcalls.c       | 90 +++++++++++++++++++
 3 files changed, 92 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c

diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
index 2f0d705db9db..ffe06dd1cc6e 100644
--- a/tools/testing/selftests/kvm/.gitignore
+++ b/tools/testing/selftests/kvm/.gitignore
@@ -24,6 +24,7 @@
 /x86_64/kvm_pv_test
 /x86_64/hyperv_clock
 /x86_64/hyperv_cpuid
+/x86_64/hyperv_extended_hcalls
 /x86_64/hyperv_features
 /x86_64/hyperv_svm_test
 /x86_64/max_vcpuid_cap_test
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index 0172eb6cb6ee..366345099363 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -85,6 +85,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/emulator_error_test
 TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
+TEST_GEN_PROGS_x86_64 += x86_64/hyperv_extended_hcalls
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_features
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_svm_test
 TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test
diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
new file mode 100644
index 000000000000..d378877235d4
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test Hyper-V extended hypercalls
+ *
+ * Copyright 2020 Google LLC
+ * Author: Vipin Sharma <vipinsh@google.com>
+ */
+
+#include "kvm_util.h"
+#include "processor.h"
+#include "hyperv.h"
+
+/* Any value is fine */
+#define EXT_CAPABILITIES 0xbull
+
+static void guest_code(vm_vaddr_t pgs_gpa, vm_vaddr_t output_pg_gva)
+{
+	uint64_t res, vector;
+	uint64_t *output_gva;
+
+	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
+	wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
+
+	output_gva = (uint64_t *)output_pg_gva;
+
+	vector = hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, pgs_gpa,
+			   pgs_gpa + 4096, &res);
+
+	GUEST_ASSERT_1(!vector, vector);
+	GUEST_ASSERT_2(res == HV_STATUS_SUCCESS, res, HV_STATUS_SUCCESS);
+
+	/* TLFS states output will be a uint64_t value */
+	GUEST_ASSERT_2(*output_gva == EXT_CAPABILITIES, *output_gva,
+		       EXT_CAPABILITIES);
+
+	GUEST_DONE();
+}
+
+static void guest_extended_hcall_test(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_run *run;
+	struct kvm_vm *vm;
+	struct ucall uc;
+	vm_vaddr_t hcall_page;
+	uint64_t *outval;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+	run = vcpu->run;
+	vcpu_enable_cap(vcpu, KVM_CAP_HYPERV_ENFORCE_CPUID, 1);
+	vcpu_set_hv_cpuid(vcpu);
+
+	/* Hypercall input/output */
+	hcall_page = vm_vaddr_alloc_pages(vm, 2);
+	memset(addr_gva2hva(vm, hcall_page), 0x0, 2 * getpagesize());
+	vcpu_args_set(vcpu, 2, addr_gva2gpa(vm, hcall_page), hcall_page + 4096);
+
+	vcpu_run(vcpu);
+
+	TEST_ASSERT((run->exit_reason == KVM_EXIT_HYPERV),
+		    "unexpected exit reason: %u (%s)", run->exit_reason,
+		    exit_reason_str(run->exit_reason));
+
+	outval = addr_gpa2hva(vm, run->hyperv.u.hcall.params[1]);
+	*outval = EXT_CAPABILITIES;
+	run->hyperv.u.hcall.result = HV_STATUS_SUCCESS;
+
+	vcpu_run(vcpu);
+
+	TEST_ASSERT((run->exit_reason == KVM_EXIT_IO),
+		    "unexpected exit reason: %u (%s)", run->exit_reason,
+		    exit_reason_str(run->exit_reason));
+
+	switch (get_ucall(vcpu, &uc)) {
+	case UCALL_ABORT:
+		REPORT_GUEST_ASSERT_2(uc, "arg1 = %ld, arg2 = %ld");
+		break;
+	case UCALL_DONE:
+		break;
+	default:
+		TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
+	}
+
+	kvm_vm_free(vm);
+}
+
+int main(void)
+{
+	guest_extended_hcall_test();
+}
-- 
2.38.1.273.g43a17bfeac-goog


  parent reply	other threads:[~2022-11-05  4:57 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-05  4:56 [PATCH 0/6] Add Hyper-v extended hypercall support in KVM Vipin Sharma
2022-11-05  4:56 ` [PATCH 1/6] KVM: x86: hyper-v: Use common code for hypercall userspace exit Vipin Sharma
2022-11-05  4:57 ` [PATCH 2/6] KVM: x86: hyper-v: Add extended hypercall support in Hyper-v Vipin Sharma
2022-11-05  4:57 ` [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement Vipin Sharma
2022-11-07 18:27   ` David Matlack
2022-11-08  1:46     ` Vipin Sharma
2022-11-05  4:57 ` [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common Vipin Sharma
2022-11-07 19:08   ` David Matlack
2022-11-08  1:45     ` Vipin Sharma
2022-11-08 17:56       ` David Matlack
2022-11-09 13:48   ` Vitaly Kuznetsov
2022-11-09 18:52     ` Vipin Sharma
2022-11-09 20:18       ` Sean Christopherson
2022-11-10 10:02       ` Vitaly Kuznetsov
2022-11-05  4:57 ` [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h Vipin Sharma
2022-11-07 18:30   ` David Matlack
2022-11-08  1:48     ` Vipin Sharma
2022-11-08 17:40       ` David Matlack
2022-11-09 13:46   ` Vitaly Kuznetsov
2022-11-05  4:57 ` Vipin Sharma [this message]
2022-11-07 19:01   ` [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace David Matlack
2022-11-08  2:04     ` Vipin Sharma
2022-11-08 17:44       ` David Matlack

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=20221105045704.2315186-7-vipinsh@google.com \
    --to=vipinsh@google.com \
    --cc=dmatlack@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®