linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Kelley <mikelley@microsoft.com>
To: will@kernel.org, ardb@kernel.org, arnd@arndb.de,
	catalin.marinas@arm.com, mark.rutland@arm.com, maz@kernel.org,
	linux-arm-kernel@lists.infradead.org, gregkh@linuxfoundation.org,
	linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org,
	linux-efi@vger.kernel.org, linux-arch@vger.kernel.org,
	olaf@aepfle.de, apw@canonical.com, vkuznets@redhat.com,
	jasowang@redhat.com, marcelo.cerri@canonical.com,
	kys@microsoft.com
Cc: mikelley@microsoft.com, sunilmut@microsoft.com, boqun.feng@gmail.com
Subject: [PATCH v6 03/10] arm64: hyperv: Add hypercall and register access functions
Date: Sat, 14 Mar 2020 08:35:12 -0700	[thread overview]
Message-ID: <1584200119-18594-4-git-send-email-mikelley@microsoft.com> (raw)
In-Reply-To: <1584200119-18594-1-git-send-email-mikelley@microsoft.com>

Add ARM64-specific code to make Hyper-V hypercalls and to
access virtual processor synthetic registers via hypercalls.
Hypercalls follow the SMC Calling Convention spec v1.1.

This code is architecture dependent and is mostly driven by
architecture independent code in the VMbus driver and the
Hyper-V timer clocksource driver.

This code is built only when CONFIG_HYPERV is enabled.

Signed-off-by: Michael Kelley <mikelley@microsoft.com>
---
 MAINTAINERS                 |   1 +
 arch/arm64/Kbuild           |   1 +
 arch/arm64/hyperv/Makefile  |   2 +
 arch/arm64/hyperv/hv_core.c | 167 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 171 insertions(+)
 create mode 100644 arch/arm64/hyperv/Makefile
 create mode 100644 arch/arm64/hyperv/hv_core.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 398cfdb..64ad2bf 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -7811,6 +7811,7 @@ F:	arch/x86/kernel/cpu/mshyperv.c
 F:	arch/x86/hyperv
 F:	arch/arm64/include/asm/hyperv-tlfs.h
 F:	arch/arm64/include/asm/mshyperv.h
+F:	arch/arm64/hyperv
 F:	drivers/clocksource/hyperv_timer.c
 F:	drivers/hid/hid-hyperv.c
 F:	drivers/hv/
diff --git a/arch/arm64/Kbuild b/arch/arm64/Kbuild
index d646582..7a37608 100644
--- a/arch/arm64/Kbuild
+++ b/arch/arm64/Kbuild
@@ -3,4 +3,5 @@ obj-y			+= kernel/ mm/
 obj-$(CONFIG_NET)	+= net/
 obj-$(CONFIG_KVM)	+= kvm/
 obj-$(CONFIG_XEN)	+= xen/
+obj-$(subst m,y,$(CONFIG_HYPERV))	+= hyperv/
 obj-$(CONFIG_CRYPTO)	+= crypto/
diff --git a/arch/arm64/hyperv/Makefile b/arch/arm64/hyperv/Makefile
new file mode 100644
index 0000000..1697d30
--- /dev/null
+++ b/arch/arm64/hyperv/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-y		:= hv_core.o
diff --git a/arch/arm64/hyperv/hv_core.c b/arch/arm64/hyperv/hv_core.c
new file mode 100644
index 0000000..272c348
--- /dev/null
+++ b/arch/arm64/hyperv/hv_core.c
@@ -0,0 +1,167 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Initialization of the interface with Microsoft's Hyper-V hypervisor,
+ * and various low level utility routines for interacting with Hyper-V.
+ *
+ * Copyright (C) 2019, Microsoft, Inc.
+ *
+ * Author : Michael Kelley <mikelley@microsoft.com>
+ */
+
+
+#include <linux/types.h>
+#include <linux/version.h>
+#include <linux/export.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/hyperv.h>
+#include <linux/arm-smccc.h>
+#include <asm-generic/bug.h>
+#include <asm/hyperv-tlfs.h>
+#include <asm/mshyperv.h>
+
+
+/*
+ * hv_do_hypercall- Invoke the specified hypercall
+ */
+u64 hv_do_hypercall(u64 control, void *input, void *output)
+{
+	u64 input_address;
+	u64 output_address;
+	struct arm_smccc_res res;
+
+	input_address = input ? virt_to_phys(input) : 0;
+	output_address = output ? virt_to_phys(output) : 0;
+
+	arm_smccc_1_1_hvc(HV_FUNC_ID, control,
+			  input_address, output_address, &res);
+	return res.a0;
+}
+EXPORT_SYMBOL_GPL(hv_do_hypercall);
+
+/*
+ * hv_do_fast_hypercall8 -- Invoke the specified hypercall
+ * with arguments in registers instead of physical memory.
+ * Avoids the overhead of virt_to_phys for simple hypercalls.
+ */
+
+u64 hv_do_fast_hypercall8(u16 code, u64 input)
+{
+	u64 control;
+	struct arm_smccc_res res;
+
+	control = (u64)code | HV_HYPERCALL_FAST_BIT;
+
+	arm_smccc_1_1_hvc(HV_FUNC_ID, control, input, &res);
+	return res.a0;
+}
+EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8);
+
+
+/*
+ * Set a single VP register to a 64-bit value.
+ */
+void hv_set_vpreg(u32 msr, u64 value)
+{
+	union hv_hypercall_status status;
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_hvc(
+		HV_FUNC_ID,
+		HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
+			HV_HYPERCALL_REP_COUNT_1,
+		HV_PARTITION_ID_SELF,
+		HV_VP_INDEX_SELF,
+		msr,
+		0,
+		value,
+		0,
+		&res);
+	status.as_uint64 = res.a0;
+
+	/*
+	 * Something is fundamentally broken in the hypervisor if
+	 * setting a VP register fails. There's really no way to
+	 * continue as a guest VM, so panic.
+	 */
+	BUG_ON(status.status != HV_STATUS_SUCCESS);
+}
+EXPORT_SYMBOL_GPL(hv_set_vpreg);
+
+/*
+ * Get the value of a single VP register.  One version
+ * returns just 64 bits and another returns the full 128 bits.
+ * The two versions are separate to avoid complicating the
+ * calling sequence for the more frequently used 64 bit version.
+ */
+
+/*
+ * Input and output memory allocation sizes are rounded up to a power
+ * of 2 so kmalloc() will guarantee alignment. In turn, the alignment
+ * ensures that the allocations don't cross a page boundary, which is
+ * required by the hypercall interface.
+ */
+#define INPUTSIZE (4 * sizeof(u64))
+#define OUTPUTSIZE (2 * sizeof(u64))
+
+static void __hv_get_vpreg_128(u32 msr, struct hv_get_vp_register_output *res)
+{
+	union hv_hypercall_status		status;
+	struct hv_get_vp_register_input		*input;
+
+	BUILD_BUG_ON(sizeof(*input) > INPUTSIZE);
+
+	input = kzalloc(INPUTSIZE, GFP_ATOMIC);
+
+	input->partitionid = HV_PARTITION_ID_SELF;
+	input->vpindex = HV_VP_INDEX_SELF;
+	input->inputvtl = 0;
+	input->name0 = msr;
+	input->name1 = 0;
+
+
+	status.as_uint64 = hv_do_hypercall(
+		HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_REP_COUNT_1,
+		input, res);
+
+	/*
+	 * Something is fundamentally broken in the hypervisor if
+	 * getting a VP register fails. There's really no way to
+	 * continue as a guest VM, so panic.
+	 */
+	BUG_ON(status.status != HV_STATUS_SUCCESS);
+
+	kfree(input);
+}
+
+u64 hv_get_vpreg(u32 msr)
+{
+	struct hv_get_vp_register_output	*output;
+	u64					result;
+
+	BUILD_BUG_ON(sizeof(*output) > OUTPUTSIZE);
+	output = kmalloc(OUTPUTSIZE, GFP_ATOMIC);
+
+	__hv_get_vpreg_128(msr, output);
+
+	result = output->registervaluelow;
+	kfree(output);
+	return result;
+}
+EXPORT_SYMBOL_GPL(hv_get_vpreg);
+
+void hv_get_vpreg_128(u32 msr, struct hv_get_vp_register_output *res)
+{
+	struct hv_get_vp_register_output	*output;
+
+	BUILD_BUG_ON(sizeof(*output) > OUTPUTSIZE);
+	output = kmalloc(OUTPUTSIZE, GFP_ATOMIC);
+
+	__hv_get_vpreg_128(msr, output);
+
+	res->registervaluelow = output->registervaluelow;
+	res->registervaluehigh = output->registervaluehigh;
+	kfree(output);
+}
+EXPORT_SYMBOL_GPL(hv_get_vpreg_128);
-- 
1.8.3.1


  parent reply	other threads:[~2020-03-15  1:31 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-14 15:35 [PATCH v6 00/10] Subject: Enable Linux guests on Hyper-V on ARM64 Michael Kelley
2020-03-14 15:35 ` [PATCH v6 01/10] arm64: hyperv: Add core Hyper-V include files Michael Kelley
2020-03-15 17:31   ` Marc Zyngier
2020-03-18  0:10     ` Michael Kelley
2020-03-16  8:47   ` Arnd Bergmann
2020-03-18  0:12     ` Michael Kelley
2020-03-18 10:10       ` Arnd Bergmann
2020-03-19 21:31         ` Michael Kelley
2020-03-20 16:38           ` Arnd Bergmann
2020-03-14 15:35 ` [PATCH v6 02/10] arm/arm64: smccc-1.1: Add vendor specific owner definition Michael Kelley
2020-03-14 15:35 ` Michael Kelley [this message]
2020-03-14 15:35 ` [PATCH v6 04/10] arm64: hyperv: Add memory alloc/free functions for Hyper-V size pages Michael Kelley
2020-03-16  8:22   ` Arnd Bergmann
2020-03-16  8:30     ` gregkh
2020-03-16  8:30     ` Marc Zyngier
2020-03-16  8:32       ` Arnd Bergmann
2020-03-18  0:15         ` Michael Kelley
2020-03-18  9:57           ` Arnd Bergmann
2020-03-19 21:43             ` Michael Kelley
2020-03-20 16:28               ` Arnd Bergmann
2020-03-20 17:22                 ` Michael Kelley
2020-03-14 15:35 ` [PATCH v6 05/10] arm64: hyperv: Add interrupt handlers for VMbus and stimer Michael Kelley
2020-03-16  8:25   ` Arnd Bergmann
2020-03-18  0:16     ` Michael Kelley
2020-03-18  9:52       ` Arnd Bergmann
2020-03-14 15:35 ` [PATCH v6 06/10] arm64: hyperv: Add kexec and panic handlers Michael Kelley
2020-03-15 18:15   ` Marc Zyngier
2020-03-18  0:17     ` Michael Kelley
2020-03-14 15:35 ` [PATCH v6 07/10] arm64: hyperv: Initialize hypervisor on boot Michael Kelley
2020-03-16  8:29   ` Arnd Bergmann
2020-03-18  0:17     ` Michael Kelley
2020-03-18  9:44       ` Arnd Bergmann
2020-03-14 15:35 ` [PATCH v6 08/10] Drivers: hv: vmbus: Add hooks for per-CPU IRQ Michael Kelley
2020-03-14 15:35 ` [PATCH v6 09/10] arm64: efi: Export screen_info Michael Kelley
2020-03-16  8:20   ` Arnd Bergmann
2020-03-18  0:18     ` Michael Kelley
2020-03-18  9:26       ` Arnd Bergmann
2020-03-19 21:46         ` Michael Kelley
2020-05-13 14:26           ` Nikhil Mahale
2020-05-18  4:25             ` Nikhil Mahale
2020-05-18 12:51               ` Ard Biesheuvel
2020-05-22 11:14                 ` Nikhil Mahale
2020-05-22 11:32                   ` Ard Biesheuvel
2020-03-14 15:35 ` [PATCH v6 10/10] Drivers: hv: Enable Hyper-V code to be built on ARM64 Michael Kelley
     [not found] ` <20200318031130.5476-1-hdanton@sina.com>
2020-03-19 21:04   ` [PATCH v6 03/10] arm64: hyperv: Add hypercall and register access functions Michael Kelley

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=1584200119-18594-4-git-send-email-mikelley@microsoft.com \
    --to=mikelley@microsoft.com \
    --cc=apw@canonical.com \
    --cc=ardb@kernel.org \
    --cc=arnd@arndb.de \
    --cc=boqun.feng@gmail.com \
    --cc=catalin.marinas@arm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jasowang@redhat.com \
    --cc=kys@microsoft.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcelo.cerri@canonical.com \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=olaf@aepfle.de \
    --cc=sunilmut@microsoft.com \
    --cc=vkuznets@redhat.com \
    --cc=will@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).