kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [kvm-unit-tests PATCH 0/3] x86: hyper-v: Add overlay page tests
@ 2021-06-10 17:36 Siddharth Chandrasekaran
  2021-06-10 17:36 ` [kvm-unit-tests PATCH 1/3] x86: Move hyperv helpers into libs/x86 Siddharth Chandrasekaran
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Siddharth Chandrasekaran @ 2021-06-10 17:36 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Siddharth Chandrasekaran, Siddharth Chandrasekaran,
	Evgeny Iakovlev, Liran Alon, Ioannis Aslanidis, kvm

Patch series [1] starts treating hypercall code page as an overlay page
(along with the existing synic event and message pages). Add KVM unit
tests to make sure the underlying page contents are intact with various
overlay workflows.

While at it, promote hyperv.h to lib/x86 and expose hv_hypercall() from
there so future tests can use it to do hypercalls.

[1]: https://www.spinics.net/lists/kvm/msg244569.html

~ Sid.

Siddharth Chandrasekaran (3):
  x86: Move hyperv helpers into libs/x86
  x86: Move hyper-v hypercall related methods to lib/x86/
  x86: Add hyper-v overlay page tests

 x86/Makefile.common       |  8 +---
 {x86 => lib/x86}/hyperv.h |  4 ++
 {x86 => lib/x86}/hyperv.c | 51 +++++++++++++++++++++
 x86/hyperv_connections.c  | 60 ++----------------------
 x86/hyperv_overlay.c      | 96 +++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg         |  5 ++
 6 files changed, 163 insertions(+), 61 deletions(-)
 rename {x86 => lib/x86}/hyperv.h (97%)
 rename {x86 => lib/x86}/hyperv.c (63%)
 create mode 100644 x86/hyperv_overlay.c

-- 
2.17.1



Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [kvm-unit-tests PATCH 1/3] x86: Move hyperv helpers into libs/x86
  2021-06-10 17:36 [kvm-unit-tests PATCH 0/3] x86: hyper-v: Add overlay page tests Siddharth Chandrasekaran
@ 2021-06-10 17:36 ` Siddharth Chandrasekaran
  2021-06-10 17:36 ` [kvm-unit-tests PATCH 2/3] x86: Move hyper-v hypercall related methods to lib/x86/ Siddharth Chandrasekaran
  2021-06-10 17:36 ` [kvm-unit-tests PATCH 3/3] x86: Add hyper-v overlay page tests Siddharth Chandrasekaran
  2 siblings, 0 replies; 4+ messages in thread
From: Siddharth Chandrasekaran @ 2021-06-10 17:36 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Siddharth Chandrasekaran, Siddharth Chandrasekaran,
	Evgeny Iakovlev, Liran Alon, Ioannis Aslanidis, kvm

Move hyperv.c and hyperv.h into into libs/x86/ as it appears to be a
better place for them.

Signed-off-by: Siddharth Chandrasekaran <sidcha@amazon.de>
---
 x86/Makefile.common       | 7 +------
 {x86 => lib/x86}/hyperv.h | 1 -
 {x86 => lib/x86}/hyperv.c | 0
 3 files changed, 1 insertion(+), 7 deletions(-)
 rename {x86 => lib/x86}/hyperv.h (99%)
 rename {x86 => lib/x86}/hyperv.c (100%)

diff --git a/x86/Makefile.common b/x86/Makefile.common
index 52bb7aa..802f8c1 100644
--- a/x86/Makefile.common
+++ b/x86/Makefile.common
@@ -22,6 +22,7 @@ cflatobjs += lib/x86/acpi.o
 cflatobjs += lib/x86/stack.o
 cflatobjs += lib/x86/fault_test.o
 cflatobjs += lib/x86/delay.o
+cflatobjs += lib/x86/hyperv.o
 
 OBJDIRS += lib/x86
 
@@ -74,12 +75,6 @@ $(TEST_DIR)/realmode.o: bits = $(if $(call cc-option,-m16,""),16,32)
 
 $(TEST_DIR)/kvmclock_test.elf: $(TEST_DIR)/kvmclock.o
 
-$(TEST_DIR)/hyperv_synic.elf: $(TEST_DIR)/hyperv.o
-
-$(TEST_DIR)/hyperv_stimer.elf: $(TEST_DIR)/hyperv.o
-
-$(TEST_DIR)/hyperv_connections.elf: $(TEST_DIR)/hyperv.o
-
 arch_clean:
 	$(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat $(TEST_DIR)/*.elf \
 	$(TEST_DIR)/.*.d lib/x86/.*.d \
diff --git a/x86/hyperv.h b/lib/x86/hyperv.h
similarity index 99%
rename from x86/hyperv.h
rename to lib/x86/hyperv.h
index e3803e0..38de0d2 100644
--- a/x86/hyperv.h
+++ b/lib/x86/hyperv.h
@@ -213,5 +213,4 @@ struct hv_reference_tsc_page {
         int64_t tsc_offset;
 };
 
-
 #endif
diff --git a/x86/hyperv.c b/lib/x86/hyperv.c
similarity index 100%
rename from x86/hyperv.c
rename to lib/x86/hyperv.c
-- 
2.17.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [kvm-unit-tests PATCH 2/3] x86: Move hyper-v hypercall related methods to lib/x86/
  2021-06-10 17:36 [kvm-unit-tests PATCH 0/3] x86: hyper-v: Add overlay page tests Siddharth Chandrasekaran
  2021-06-10 17:36 ` [kvm-unit-tests PATCH 1/3] x86: Move hyperv helpers into libs/x86 Siddharth Chandrasekaran
@ 2021-06-10 17:36 ` Siddharth Chandrasekaran
  2021-06-10 17:36 ` [kvm-unit-tests PATCH 3/3] x86: Add hyper-v overlay page tests Siddharth Chandrasekaran
  2 siblings, 0 replies; 4+ messages in thread
From: Siddharth Chandrasekaran @ 2021-06-10 17:36 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Siddharth Chandrasekaran, Siddharth Chandrasekaran,
	Evgeny Iakovlev, Liran Alon, Ioannis Aslanidis, kvm

Some future tests that we are about to write need to perform hypercalls;
move do_hypercall() and hypercall page setup methods to a more
accessible location: libs/x86/hyperv.c.

Signed-off-by: Siddharth Chandrasekaran <sidcha@amazon.de>
---
 lib/x86/hyperv.h         |  4 +++
 lib/x86/hyperv.c         | 51 ++++++++++++++++++++++++++++++++++
 x86/hyperv_connections.c | 60 ++++------------------------------------
 3 files changed, 60 insertions(+), 55 deletions(-)

diff --git a/lib/x86/hyperv.h b/lib/x86/hyperv.h
index 38de0d2..889f5a6 100644
--- a/lib/x86/hyperv.h
+++ b/lib/x86/hyperv.h
@@ -213,4 +213,8 @@ struct hv_reference_tsc_page {
         int64_t tsc_offset;
 };
 
+void hv_setup_hypercall(void);
+void hv_teardown_hypercall(void);
+u64 hv_hypercall(u16 code, u64 arg, bool fast);
+
 #endif
diff --git a/lib/x86/hyperv.c b/lib/x86/hyperv.c
index 60f7645..e7c3351 100644
--- a/lib/x86/hyperv.c
+++ b/lib/x86/hyperv.c
@@ -1,6 +1,7 @@
 #include "hyperv.h"
 #include "asm/io.h"
 #include "smp.h"
+#include "alloc_page.h"
 
 enum {
     HV_TEST_DEV_SINT_ROUTE_CREATE = 1,
@@ -68,3 +69,53 @@ void evt_conn_destroy(u8 sint, u8 conn_id)
     sint_disable(sint);
     synic_ctl(HV_TEST_DEV_EVT_CONN_DESTROY, 0, 0, conn_id);
 }
+
+static void *hypercall_page;
+
+void hv_setup_hypercall(void)
+{
+	u64 guestid = (0x8f00ull << 48);
+
+	hypercall_page = alloc_page();
+	if (!hypercall_page)
+		report_abort("failed to allocate hypercall page");
+
+	wrmsr(HV_X64_MSR_GUEST_OS_ID, guestid);
+
+	wrmsr(HV_X64_MSR_HYPERCALL,
+	      (u64)virt_to_phys(hypercall_page) | HV_X64_MSR_HYPERCALL_ENABLE);
+}
+
+void hv_teardown_hypercall(void)
+{
+	wrmsr(HV_X64_MSR_HYPERCALL, 0);
+	wrmsr(HV_X64_MSR_GUEST_OS_ID, 0);
+	free_page(hypercall_page);
+}
+
+u64 hv_hypercall(u16 code, u64 arg, bool fast)
+{
+	u64 ret;
+	u64 ctl = code;
+	if (fast)
+		ctl |= HV_HYPERCALL_FAST;
+
+	asm volatile ("call *%[hcall_page]"
+#ifdef __x86_64__
+		      "\n mov $0,%%r8"
+		      : "=a"(ret)
+		      : "c"(ctl), "d"(arg),
+#else
+		      : "=A"(ret)
+		      : "A"(ctl),
+			"b" ((u32)(arg >> 32)), "c" ((u32)arg),
+			"D"(0), "S"(0),
+#endif
+		      [hcall_page] "m" (hypercall_page)
+#ifdef __x86_64__
+		      : "r8"
+#endif
+		     );
+
+	return ret;
+}
diff --git a/x86/hyperv_connections.c b/x86/hyperv_connections.c
index 6e8ac32..1650f01 100644
--- a/x86/hyperv_connections.c
+++ b/x86/hyperv_connections.c
@@ -38,56 +38,6 @@ static void sint_isr(isr_regs_t *regs)
 	atomic_inc(&hv_vcpus[smp_id()].sint_received);
 }
 
-static void *hypercall_page;
-
-static void setup_hypercall(void)
-{
-	u64 guestid = (0x8f00ull << 48);
-
-	hypercall_page = alloc_page();
-	if (!hypercall_page)
-		report_abort("failed to allocate hypercall page");
-
-	wrmsr(HV_X64_MSR_GUEST_OS_ID, guestid);
-
-	wrmsr(HV_X64_MSR_HYPERCALL,
-	      (u64)virt_to_phys(hypercall_page) | HV_X64_MSR_HYPERCALL_ENABLE);
-}
-
-static void teardown_hypercall(void)
-{
-	wrmsr(HV_X64_MSR_HYPERCALL, 0);
-	wrmsr(HV_X64_MSR_GUEST_OS_ID, 0);
-	free_page(hypercall_page);
-}
-
-static u64 do_hypercall(u16 code, u64 arg, bool fast)
-{
-	u64 ret;
-	u64 ctl = code;
-	if (fast)
-		ctl |= HV_HYPERCALL_FAST;
-
-	asm volatile ("call *%[hcall_page]"
-#ifdef __x86_64__
-		      "\n mov $0,%%r8"
-		      : "=a"(ret)
-		      : "c"(ctl), "d"(arg),
-#else
-		      : "=A"(ret)
-		      : "A"(ctl),
-			"b" ((u32)(arg >> 32)), "c" ((u32)arg),
-			"D"(0), "S"(0),
-#endif
-		      [hcall_page] "m" (hypercall_page)
-#ifdef __x86_64__
-		      : "r8"
-#endif
-		     );
-
-	return ret;
-}
-
 static void setup_cpu(void *ctx)
 {
 	int vcpu;
@@ -147,7 +97,7 @@ static void do_msg(void *ctx)
 
 	msg->payload[0]++;
 	atomic_set(&hv->sint_received, 0);
-	hv->hvcall_status = do_hypercall(HVCALL_POST_MESSAGE,
+	hv->hvcall_status = hv_hypercall(HVCALL_POST_MESSAGE,
 					 virt_to_phys(msg), 0);
 	atomic_inc(&ncpus_done);
 }
@@ -200,7 +150,7 @@ static void do_evt(void *ctx)
 	struct hv_vcpu *hv = &hv_vcpus[vcpu];
 
 	atomic_set(&hv->sint_received, 0);
-	hv->hvcall_status = do_hypercall(HVCALL_SIGNAL_EVENT,
+	hv->hvcall_status = hv_hypercall(HVCALL_SIGNAL_EVENT,
 					 hv->evt_conn, 1);
 	atomic_inc(&ncpus_done);
 }
@@ -279,9 +229,9 @@ int main(int ac, char **av)
 	handle_irq(MSG_VEC, sint_isr);
 	handle_irq(EVT_VEC, sint_isr);
 
-	setup_hypercall();
+	hv_setup_hypercall();
 
-	if (do_hypercall(HVCALL_SIGNAL_EVENT, 0x1234, 1) ==
+	if (hv_hypercall(HVCALL_SIGNAL_EVENT, 0x1234, 1) ==
 	    HV_STATUS_INVALID_HYPERCALL_CODE) {
 		report_skip("Hyper-V SynIC connections are not supported");
 		goto summary;
@@ -325,7 +275,7 @@ int main(int ac, char **av)
 	for (i = 0; i < ncpus; i++)
 		on_cpu(i, teardown_cpu, NULL);
 
-	teardown_hypercall();
+	hv_teardown_hypercall();
 
 summary:
 	return report_summary();
-- 
2.17.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [kvm-unit-tests PATCH 3/3] x86: Add hyper-v overlay page tests
  2021-06-10 17:36 [kvm-unit-tests PATCH 0/3] x86: hyper-v: Add overlay page tests Siddharth Chandrasekaran
  2021-06-10 17:36 ` [kvm-unit-tests PATCH 1/3] x86: Move hyperv helpers into libs/x86 Siddharth Chandrasekaran
  2021-06-10 17:36 ` [kvm-unit-tests PATCH 2/3] x86: Move hyper-v hypercall related methods to lib/x86/ Siddharth Chandrasekaran
@ 2021-06-10 17:36 ` Siddharth Chandrasekaran
  2 siblings, 0 replies; 4+ messages in thread
From: Siddharth Chandrasekaran @ 2021-06-10 17:36 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Siddharth Chandrasekaran, Siddharth Chandrasekaran,
	Evgeny Iakovlev, Liran Alon, Ioannis Aslanidis, kvm

Patch series [1] starts treating hypercall code page as an overlay page
(along with the existing synic event and message pages). Add KVM unit
tests to make sure the underlying page contents are intact with various
overlay workflows.

[1]: https://www.spinics.net/lists/kvm/msg244569.html

Signed-off-by: Siddharth Chandrasekaran <sidcha@amazon.de>
---
 x86/Makefile.common  |  1 +
 lib/x86/hyperv.h     |  1 +
 x86/hyperv_overlay.c | 96 ++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg    |  5 +++
 4 files changed, 103 insertions(+)
 create mode 100644 x86/hyperv_overlay.c

diff --git a/x86/Makefile.common b/x86/Makefile.common
index 802f8c1..cb41992 100644
--- a/x86/Makefile.common
+++ b/x86/Makefile.common
@@ -61,6 +61,7 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/init.flat $(TEST_DIR)/smap.flat \
                $(TEST_DIR)/hyperv_synic.flat $(TEST_DIR)/hyperv_stimer.flat \
                $(TEST_DIR)/hyperv_connections.flat \
+               $(TEST_DIR)/hyperv_overlay.flat \
                $(TEST_DIR)/umip.flat $(TEST_DIR)/tsx-ctrl.flat
 
 test_cases: $(tests-common) $(tests)
diff --git a/lib/x86/hyperv.h b/lib/x86/hyperv.h
index 889f5a6..e207c69 100644
--- a/lib/x86/hyperv.h
+++ b/lib/x86/hyperv.h
@@ -52,6 +52,7 @@
 #define HV_X64_MSR_STIMER3_CONFIG               0x400000B6
 #define HV_X64_MSR_STIMER3_COUNT                0x400000B7
 
+#define HV_OVERLAY_ENABLE                       (1ULL << 0)
 #define HV_SYNIC_CONTROL_ENABLE                 (1ULL << 0)
 #define HV_SYNIC_SIMP_ENABLE                    (1ULL << 0)
 #define HV_SYNIC_SIEFP_ENABLE                   (1ULL << 0)
diff --git a/x86/hyperv_overlay.c b/x86/hyperv_overlay.c
new file mode 100644
index 0000000..4472f64
--- /dev/null
+++ b/x86/hyperv_overlay.c
@@ -0,0 +1,96 @@
+#include "vm.h"
+#include "hyperv.h"
+#include "alloc_page.h"
+
+/**
+ * Test if the underlying GPA contents are preserved when an
+ * overlay is mounted there.
+ */
+static int test_underlay_intact(void *page, u64 msr)
+{
+	int i;
+	u64 gpa = (u64)virt_to_phys(page);
+
+	memset(page, 0xAA, PAGE_SIZE);
+
+	/* Enable overlay */
+	wrmsr(msr, gpa | HV_OVERLAY_ENABLE);
+
+	/* Write to overlay */
+	memset(page, 0x55, PAGE_SIZE);
+
+	/* Disable overlay */
+	wrmsr(msr, 0);
+
+	for (i = 0; i < PAGE_SIZE; i++)
+		if (((u8 *)page)[i] != 0xAA)
+			return -1;
+
+	return 0;
+}
+
+/**
+ * Test if Guest OS ID reset unmounts hypercall overlay and
+ * exposes the underlying page.
+ */
+static int test_guest_os_id_reset(void *page)
+{
+	int i;
+	u64 gpa = (u64)virt_to_phys(page);
+
+	memset(page, 0xAA, PAGE_SIZE);
+
+	/* Enable overlay */
+	wrmsr(HV_X64_MSR_HYPERCALL, gpa | HV_OVERLAY_ENABLE);
+
+	/* Write to overlay */
+	memset(page, 0x55, PAGE_SIZE);
+
+	/* Guest OS ID reset forces overlay unmap */
+	wrmsr(HV_X64_MSR_GUEST_OS_ID, 0);
+
+	for (i = 0; i < PAGE_SIZE; i++)
+		if (((u8 *)page)[i] != 0xAA)
+			return -1;
+
+	return 0;
+}
+
+int main(int ac, char **av)
+{
+	int rc;
+	void *page;
+	u64 guestid = (0x8f00ull << 48);
+
+	setup_vm();
+
+	page = alloc_page();
+	if (!page)
+		report_abort("Failed to allocate page for overlay tests");
+
+	rc = test_underlay_intact(page, HV_X64_MSR_HYPERCALL);
+	report(rc != 0, "Hypercall page before guest OS ID write");
+
+	wrmsr(HV_X64_MSR_GUEST_OS_ID, guestid);
+	rc = test_underlay_intact(page, HV_X64_MSR_HYPERCALL);
+	report(rc == 0, "Hypercall page after guest OS ID write");
+
+	rc = test_guest_os_id_reset(page);
+	report(rc == 0, "Guest OS ID reset removes hcall overlay");
+
+	if (!synic_supported()) {
+		report_skip("Hyper-V SynIC is not supported");
+		goto summary;
+	}
+
+	rc = test_underlay_intact(page, HV_X64_MSR_SIMP);
+	report(rc == 0, "SynIC message page");
+
+	rc = test_underlay_intact(page, HV_X64_MSR_SIEFP);
+	report(rc == 0, "SynIC event page");
+
+	free_page(page);
+
+summary:
+	return report_summary();
+}
diff --git a/x86/unittests.cfg b/x86/unittests.cfg
index d5efab0..03f7d57 100644
--- a/x86/unittests.cfg
+++ b/x86/unittests.cfg
@@ -377,6 +377,11 @@ arch = x86_64
 groups = hyperv
 check = /sys/devices/system/clocksource/clocksource0/current_clocksource=tsc
 
+[hyperv_overlay]
+file = hyperv_overlay.flat
+extra_params = -cpu kvm64,hv_vpindex,hv_synic
+groups = hyperv
+
 [intel_iommu]
 file = intel-iommu.flat
 arch = x86_64
-- 
2.17.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879




^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-10 17:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-10 17:36 [kvm-unit-tests PATCH 0/3] x86: hyper-v: Add overlay page tests Siddharth Chandrasekaran
2021-06-10 17:36 ` [kvm-unit-tests PATCH 1/3] x86: Move hyperv helpers into libs/x86 Siddharth Chandrasekaran
2021-06-10 17:36 ` [kvm-unit-tests PATCH 2/3] x86: Move hyper-v hypercall related methods to lib/x86/ Siddharth Chandrasekaran
2021-06-10 17:36 ` [kvm-unit-tests PATCH 3/3] x86: Add hyper-v overlay page tests Siddharth Chandrasekaran

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).