Skip to content

Commit 9bbb888

Browse files
kelleymhliuw
authored andcommitted
arm64: hyperv: Initialize hypervisor on boot
Add ARM64-specific code to initialize the Hyper-V hypervisor when booting as a guest VM. This code is built only when CONFIG_HYPERV is enabled. Signed-off-by: Michael Kelley <[email protected]> Acked-by: Marc Zyngier <[email protected]> Acked-by: Mark Rutland <[email protected]> Acked-by: Catalin Marinas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Wei Liu <[email protected]>
1 parent 512c111 commit 9bbb888

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

arch/arm64/hyperv/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# SPDX-License-Identifier: GPL-2.0
2-
obj-y := hv_core.o
2+
obj-y := hv_core.o mshyperv.o

arch/arm64/hyperv/mshyperv.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Core routines for interacting with Microsoft's Hyper-V hypervisor,
5+
* including hypervisor initialization.
6+
*
7+
* Copyright (C) 2021, Microsoft, Inc.
8+
*
9+
* Author : Michael Kelley <[email protected]>
10+
*/
11+
12+
#include <linux/types.h>
13+
#include <linux/acpi.h>
14+
#include <linux/export.h>
15+
#include <linux/errno.h>
16+
#include <linux/version.h>
17+
#include <linux/cpuhotplug.h>
18+
#include <asm/mshyperv.h>
19+
20+
static bool hyperv_initialized;
21+
22+
static int __init hyperv_init(void)
23+
{
24+
struct hv_get_vp_registers_output result;
25+
u32 a, b, c, d;
26+
u64 guest_id;
27+
int ret;
28+
29+
/*
30+
* Allow for a kernel built with CONFIG_HYPERV to be running in
31+
* a non-Hyper-V environment, including on DT instead of ACPI.
32+
* In such cases, do nothing and return success.
33+
*/
34+
if (acpi_disabled)
35+
return 0;
36+
37+
if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8))
38+
return 0;
39+
40+
/* Setup the guest ID */
41+
guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
42+
hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
43+
44+
/* Get the features and hints from Hyper-V */
45+
hv_get_vpreg_128(HV_REGISTER_FEATURES, &result);
46+
ms_hyperv.features = result.as32.a;
47+
ms_hyperv.priv_high = result.as32.b;
48+
ms_hyperv.misc_features = result.as32.c;
49+
50+
hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &result);
51+
ms_hyperv.hints = result.as32.a;
52+
53+
pr_info("Hyper-V: privilege flags low 0x%x, high 0x%x, hints 0x%x, misc 0x%x\n",
54+
ms_hyperv.features, ms_hyperv.priv_high, ms_hyperv.hints,
55+
ms_hyperv.misc_features);
56+
57+
/* Get information about the Hyper-V host version */
58+
hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &result);
59+
a = result.as32.a;
60+
b = result.as32.b;
61+
c = result.as32.c;
62+
d = result.as32.d;
63+
pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
64+
b >> 16, b & 0xFFFF, a, d & 0xFFFFFF, c, d >> 24);
65+
66+
ret = hv_common_init();
67+
if (ret)
68+
return ret;
69+
70+
ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "arm64/hyperv_init:online",
71+
hv_common_cpu_init, hv_common_cpu_die);
72+
if (ret < 0) {
73+
hv_common_free();
74+
return ret;
75+
}
76+
77+
hyperv_initialized = true;
78+
return 0;
79+
}
80+
81+
early_initcall(hyperv_init);
82+
83+
bool hv_is_hyperv_initialized(void)
84+
{
85+
return hyperv_initialized;
86+
}
87+
EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);

0 commit comments

Comments
 (0)