[PATCH v8 4/6] arm64: hyperv: Initialize hypervisor on boot
From: Michael Kelley
Date: Thu Feb 18 2021 - 18:19:19 EST
Add ARM64-specific code to initialize the Hyper-V
hypervisor when booting as a guest VM. Provide functions
and data structures indicating hypervisor status that
are needed by VMbus driver.
This code is built only when CONFIG_HYPERV is enabled.
Signed-off-by: Michael Kelley <mikelley@xxxxxxxxxxxxx>
---
arch/arm64/hyperv/mshyperv.c | 140 ++++++++++++++++++++++++++++++++++++++
arch/arm64/include/asm/mshyperv.h | 6 ++
arch/arm64/kernel/setup.c | 4 ++
3 files changed, 150 insertions(+)
diff --git a/arch/arm64/hyperv/mshyperv.c b/arch/arm64/hyperv/mshyperv.c
index d202b4c..c72bc66 100644
--- a/arch/arm64/hyperv/mshyperv.c
+++ b/arch/arm64/hyperv/mshyperv.c
@@ -14,6 +14,146 @@
#include <linux/types.h>
#include <linux/export.h>
#include <linux/ptrace.h>
+#include <linux/errno.h>
+#include <linux/acpi.h>
+#include <linux/version.h>
+#include <linux/log2.h>
+#include <asm/mshyperv.h>
+
+static bool hyperv_initialized;
+
+struct ms_hyperv_info ms_hyperv __ro_after_init;
+EXPORT_SYMBOL_GPL(ms_hyperv);
+
+u32 *hv_vp_index;
+EXPORT_SYMBOL_GPL(hv_vp_index);
+
+u32 hv_max_vp_index;
+EXPORT_SYMBOL_GPL(hv_max_vp_index);
+
+/*
+ * As hypercall input and output, align to a power of 2 to ensure they
+ * don't cross a page boundary. Initialize the first element of the
+ * variable size array in the input to ensure enough space is allocated.
+ */
+static struct hv_get_vp_registers_input input __initdata __aligned(
+ roundup_pow_of_two(sizeof(struct hv_get_vp_registers_input) +
+ sizeof(struct input))) = {.element[0].name0 = 1};
+static struct hv_get_vp_registers_output result __initdata __aligned(
+ roundup_pow_of_two(sizeof(struct hv_get_vp_registers_output)));
+
+void __init hyperv_early_init(void)
+{
+ u32 a, b, c, d;
+ u64 guest_id;
+
+ /*
+ * If we're in a VM on Hyper-V, the ACPI hypervisor_id field will
+ * have the string "MsHyperV".
+ */
+ if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8))
+ return;
+
+ /* Setup the guest ID */
+ guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
+ hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
+
+ /* Get the features and hints from Hyper-V */
+ __hv_get_vpreg_128(HV_REGISTER_FEATURES, &input, &result);
+ ms_hyperv.features = result.as32.a;
+ ms_hyperv.misc_features = result.as32.c;
+
+ __hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &input, &result);
+ ms_hyperv.hints = result.as32.a;
+
+ pr_info("Hyper-V: Features 0x%x, hints 0x%x, misc 0x%x\n",
+ ms_hyperv.features, ms_hyperv.hints, ms_hyperv.misc_features);
+
+ /*
+ * If Hyper-V has crash notifications, set crash_kexec_post_notifiers
+ * so that we will report the panic to Hyper-V before running kdump.
+ */
+ if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE)
+ crash_kexec_post_notifiers = true;
+
+ /* Get information about the Hyper-V host version */
+ __hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &input, &result);
+ a = result.as32.a;
+ b = result.as32.b;
+ c = result.as32.c;
+ d = result.as32.d;
+ pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
+ b >> 16, b & 0xFFFF, a, d & 0xFFFFFF, c, d >> 24);
+
+ hyperv_initialized = true;
+}
+
+static u64 hypercall_output __initdata;
+
+static int __init hyperv_init(void)
+{
+ struct hv_get_vpindex_from_apicid_input *input;
+ u64 status;
+ int i;
+
+ /*
+ * Hypercall inputs must not cross a page boundary, so allocate
+ * power of 2 size, which will be aligned to that size.
+ */
+ input = kzalloc(roundup_pow_of_two(sizeof(input->header) +
+ sizeof(input->element[0])), GFP_KERNEL);
+ if (!input)
+ return -ENOMEM;
+
+ /* Allocate and initialize percpu VP index array */
+ hv_max_vp_index = num_possible_cpus();
+ hv_vp_index = kmalloc_array(hv_max_vp_index, sizeof(*hv_vp_index),
+ GFP_KERNEL);
+ if (!hv_vp_index) {
+ kfree(input);
+ return -ENOMEM;
+ }
+
+ input->header.partitionid = HV_PARTITION_ID_SELF;
+ for (i = 0; i < hv_max_vp_index; i++) {
+ input->element[0].mpidr = cpu_logical_map(i);
+ status = hv_do_hypercall(HVCALL_VPINDEX_FROM_APICID |
+ HV_HYPERCALL_REP_COMP_1, input,
+ &hypercall_output);
+ if ((status & HV_HYPERCALL_RESULT_MASK) == HV_STATUS_SUCCESS)
+ hv_vp_index[i] = hypercall_output;
+ else {
+ pr_warn("Hyper-V: No VP index for CPU %d MPIDR %llx status %llx\n",
+ i, cpu_logical_map(i), status);
+ hv_vp_index[i] = VP_INVAL;
+ }
+ }
+
+ kfree(input);
+ return 0;
+}
+
+early_initcall(hyperv_init);
+
+/* This routine is called before kexec/kdump. It does required cleanup. */
+void hyperv_cleanup(void)
+{
+ hv_set_vpreg(HV_REGISTER_GUEST_OSID, 0);
+
+}
+EXPORT_SYMBOL_GPL(hyperv_cleanup);
+
+bool hv_is_hyperv_initialized(void)
+{
+ return hyperv_initialized;
+}
+EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
+
+bool hv_is_hibernation_supported(void)
+{
+ return false;
+}
+EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
/*
* The VMbus handler functions are no-ops on ARM64 because
diff --git a/arch/arm64/include/asm/mshyperv.h b/arch/arm64/include/asm/mshyperv.h
index d6ff2ee..55a61a8 100644
--- a/arch/arm64/include/asm/mshyperv.h
+++ b/arch/arm64/include/asm/mshyperv.h
@@ -23,6 +23,12 @@
#include <asm/hyperv-tlfs.h>
#include <clocksource/arm_arch_timer.h>
+#if IS_ENABLED(CONFIG_HYPERV)
+void __init hyperv_early_init(void);
+#else
+static inline void hyperv_early_init(void) {};
+#endif
+
/*
* Declare calls to get and set Hyper-V VP register values on ARM64, which
* requires a hypercall.
diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
index c18aacd..1df8285 100644
--- a/arch/arm64/kernel/setup.c
+++ b/arch/arm64/kernel/setup.c
@@ -49,6 +49,7 @@
#include <asm/traps.h>
#include <asm/efi.h>
#include <asm/xen/hypervisor.h>
+#include <asm/mshyperv.h>
#include <asm/mmu_context.h>
static int num_standard_resources;
@@ -340,6 +341,9 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)
if (acpi_disabled)
unflatten_device_tree();
+ /* Do after acpi_boot_table_init() so local FADT is available */
+ hyperv_early_init();
+
bootmem_init();
kasan_init();
--
1.8.3.1