From patchwork Thu Jun 10 17:36:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Chandrasekaran X-Patchwork-Id: 12313667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35548C48BD1 for ; Thu, 10 Jun 2021 17:37:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 15A6161040 for ; Thu, 10 Jun 2021 17:37:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231417AbhFJRjP (ORCPT ); Thu, 10 Jun 2021 13:39:15 -0400 Received: from smtp-fw-2101.amazon.com ([72.21.196.25]:11118 "EHLO smtp-fw-2101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231368AbhFJRjO (ORCPT ); Thu, 10 Jun 2021 13:39:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1623346638; x=1654882638; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=qTWpLN7fasm/e+sl6efpA58R8TzJua9uLtnVDM0PKhU=; b=jjmdpFQugYfSz7gkboySstm9fd5vx6SEQdb9zxo9j4TxSB0/EsvxdNXJ s+51IK19T/gMUIVzXoNDz10AM1kNrcNJO1LuYv3jAc8YYKfxDRHJkDBYU 22SDZpJ1bw7PtWFRk8O+I4YWJzkTRQ7VfB5nr8QcwdW0SEDOn7yvaS1tD Y=; X-IronPort-AV: E=Sophos;i="5.83,264,1616457600"; d="scan'208";a="115049433" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1a-7d76a15f.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP; 10 Jun 2021 17:37:14 +0000 Received: from EX13D28EUC003.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1a-7d76a15f.us-east-1.amazon.com (Postfix) with ESMTPS id 36654A06C4; Thu, 10 Jun 2021 17:37:13 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.160.55) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Thu, 10 Jun 2021 17:37:09 +0000 From: Siddharth Chandrasekaran To: Paolo Bonzini CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , Subject: [kvm-unit-tests PATCH 1/3] x86: Move hyperv helpers into libs/x86 Date: Thu, 10 Jun 2021 19:36:48 +0200 Message-ID: <153c7f54e6e620ebaf4ca0610ecbbf11ecae50b2.1623346319.git.sidcha@amazon.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.160.55] X-ClientProxiedBy: EX13D36UWA001.ant.amazon.com (10.43.160.71) To EX13D28EUC003.ant.amazon.com (10.43.164.43) Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 --- 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 From patchwork Thu Jun 10 17:36:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Chandrasekaran X-Patchwork-Id: 12313669 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7D34C47094 for ; Thu, 10 Jun 2021 17:37:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BEE3261040 for ; Thu, 10 Jun 2021 17:37:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231416AbhFJRjW (ORCPT ); Thu, 10 Jun 2021 13:39:22 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:47746 "EHLO smtp-fw-9102.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231388AbhFJRjV (ORCPT ); Thu, 10 Jun 2021 13:39:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1623346645; x=1654882645; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=bUcsS2klQa9+ToA72tCeg7KuKqu3JXMHtZvcZJcVyLc=; b=aMBA8Qdbgxk9rx5UW7apzOyb0JZgAgUoVuWWxOwvZzxLYpE0gD+WD7NF ie1EZdAXbiEh9IdvW8sxJyfQzsBcy+dnqqdeJpc7c66gMUr2312HGN2+C UwPyZH+RgwB0CFMfK3E3/WO2VjN0PR/q87y1SI6eoK8kdddxDyGDkmuce g=; X-IronPort-AV: E=Sophos;i="5.83,264,1616457600"; d="scan'208";a="139518281" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-1a-821c648d.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP; 10 Jun 2021 17:37:18 +0000 Received: from EX13D28EUC003.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1a-821c648d.us-east-1.amazon.com (Postfix) with ESMTPS id B2A8FA1C5B; Thu, 10 Jun 2021 17:37:16 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.160.55) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Thu, 10 Jun 2021 17:37:12 +0000 From: Siddharth Chandrasekaran To: Paolo Bonzini CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , Subject: [kvm-unit-tests PATCH 2/3] x86: Move hyper-v hypercall related methods to lib/x86/ Date: Thu, 10 Jun 2021 19:36:49 +0200 Message-ID: <61703b2ec0d9ded05d63dd6564460c81ce76fe11.1623346319.git.sidcha@amazon.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.160.55] X-ClientProxiedBy: EX13D36UWA001.ant.amazon.com (10.43.160.71) To EX13D28EUC003.ant.amazon.com (10.43.164.43) Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 --- 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(); From patchwork Thu Jun 10 17:36:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Chandrasekaran X-Patchwork-Id: 12313671 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A443BC47094 for ; Thu, 10 Jun 2021 17:37:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8EAA5613F1 for ; Thu, 10 Jun 2021 17:37:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229941AbhFJRjZ (ORCPT ); Thu, 10 Jun 2021 13:39:25 -0400 Received: from smtp-fw-6001.amazon.com ([52.95.48.154]:33425 "EHLO smtp-fw-6001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231356AbhFJRjY (ORCPT ); Thu, 10 Jun 2021 13:39:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1623346649; x=1654882649; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=j8PEDW6aXwn4MsddbpfGm6iS7DqxIb1FjvBh0zKniUQ=; b=Eu8VZ2ugiwcstPmtCTx+O9bH9xUzbVUnRb3IT93gvSOfLPKXK1wWJQcf xchbH6ycHdsOgBU+QGEssFCKgk0J5jDqGbkdLhTLLjQRRCOX3sY5clkhq NgebX9nSkLlM2q2cuVSrl4N3joOKSV7XVTsEklZmsonQg+2Uuvn5rljjA Y=; X-IronPort-AV: E=Sophos;i="5.83,264,1616457600"; d="scan'208";a="119414386" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-c7c08562.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP; 10 Jun 2021 17:37:22 +0000 Received: from EX13D28EUC003.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-c7c08562.us-east-1.amazon.com (Postfix) with ESMTPS id 8370F24023F; Thu, 10 Jun 2021 17:37:19 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.160.55) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Thu, 10 Jun 2021 17:37:15 +0000 From: Siddharth Chandrasekaran To: Paolo Bonzini CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , Subject: [kvm-unit-tests PATCH 3/3] x86: Add hyper-v overlay page tests Date: Thu, 10 Jun 2021 19:36:50 +0200 Message-ID: <264c20d84f4acc3d61a3edcd5f9566651313bd59.1623346319.git.sidcha@amazon.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.160.55] X-ClientProxiedBy: EX13D36UWA001.ant.amazon.com (10.43.160.71) To EX13D28EUC003.ant.amazon.com (10.43.164.43) Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 --- 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