Skip to content

Commit c07f191

Browse files
committed
Merge tag 'hyperv-next-signed-20210831' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux
Pull hyperv updates from Wei Liu: - make Hyper-V code arch-agnostic (Michael Kelley) - fix sched_clock behaviour on Hyper-V (Ani Sinha) - fix a fault when Linux runs as the root partition on MSHV (Praveen Kumar) - fix VSS driver (Vitaly Kuznetsov) - cleanup (Sonia Sharma) * tag 'hyperv-next-signed-20210831' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux: hv_utils: Set the maximum packet size for VSS driver to the length of the receive buffer Drivers: hv: Enable Hyper-V code to be built on ARM64 arm64: efi: Export screen_info arm64: hyperv: Initialize hypervisor on boot arm64: hyperv: Add panic handler arm64: hyperv: Add Hyper-V hypercall and register access utilities x86/hyperv: fix root partition faults when writing to VP assist page MSR hv: hyperv.h: Remove unused inline functions drivers: hv: Decouple Hyper-V clock/timer code from VMbus drivers x86/hyperv: add comment describing TSC_INVARIANT_CONTROL MSR setting bit 0 Drivers: hv: Move Hyper-V misc functionality to arch-neutral code Drivers: hv: Add arch independent default functions for some Hyper-V handlers Drivers: hv: Make portions of Hyper-V init code be arch neutral x86/hyperv: fix for unwanted manipulation of sched_clock when TSC marked unstable asm-generic/hyperv: Add missing #include of nmi.h
2 parents 7c636d4 + 9d68cd9 commit c07f191

File tree

20 files changed

+724
-165
lines changed

20 files changed

+724
-165
lines changed

MAINTAINERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8647,6 +8647,9 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux.git
86478647
F: Documentation/ABI/stable/sysfs-bus-vmbus
86488648
F: Documentation/ABI/testing/debugfs-hyperv
86498649
F: Documentation/networking/device_drivers/ethernet/microsoft/netvsc.rst
8650+
F: arch/arm64/hyperv
8651+
F: arch/arm64/include/asm/hyperv-tlfs.h
8652+
F: arch/arm64/include/asm/mshyperv.h
86508653
F: arch/x86/hyperv
86518654
F: arch/x86/include/asm/hyperv-tlfs.h
86528655
F: arch/x86/include/asm/mshyperv.h

arch/arm64/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
obj-y += kernel/ mm/ net/
33
obj-$(CONFIG_KVM) += kvm/
44
obj-$(CONFIG_XEN) += xen/
5+
obj-$(subst m,y,$(CONFIG_HYPERV)) += hyperv/
56
obj-$(CONFIG_CRYPTO) += crypto/

arch/arm64/hyperv/Makefile

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

arch/arm64/hyperv/hv_core.c

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Low level utility routines for interacting with Hyper-V.
5+
*
6+
* Copyright (C) 2021, Microsoft, Inc.
7+
*
8+
* Author : Michael Kelley <[email protected]>
9+
*/
10+
11+
#include <linux/types.h>
12+
#include <linux/export.h>
13+
#include <linux/mm.h>
14+
#include <linux/hyperv.h>
15+
#include <linux/arm-smccc.h>
16+
#include <linux/module.h>
17+
#include <asm-generic/bug.h>
18+
#include <asm/hyperv-tlfs.h>
19+
#include <asm/mshyperv.h>
20+
21+
/*
22+
* hv_do_hypercall- Invoke the specified hypercall
23+
*/
24+
u64 hv_do_hypercall(u64 control, void *input, void *output)
25+
{
26+
struct arm_smccc_res res;
27+
u64 input_address;
28+
u64 output_address;
29+
30+
input_address = input ? virt_to_phys(input) : 0;
31+
output_address = output ? virt_to_phys(output) : 0;
32+
33+
arm_smccc_1_1_hvc(HV_FUNC_ID, control,
34+
input_address, output_address, &res);
35+
return res.a0;
36+
}
37+
EXPORT_SYMBOL_GPL(hv_do_hypercall);
38+
39+
/*
40+
* hv_do_fast_hypercall8 -- Invoke the specified hypercall
41+
* with arguments in registers instead of physical memory.
42+
* Avoids the overhead of virt_to_phys for simple hypercalls.
43+
*/
44+
45+
u64 hv_do_fast_hypercall8(u16 code, u64 input)
46+
{
47+
struct arm_smccc_res res;
48+
u64 control;
49+
50+
control = (u64)code | HV_HYPERCALL_FAST_BIT;
51+
52+
arm_smccc_1_1_hvc(HV_FUNC_ID, control, input, &res);
53+
return res.a0;
54+
}
55+
EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8);
56+
57+
/*
58+
* Set a single VP register to a 64-bit value.
59+
*/
60+
void hv_set_vpreg(u32 msr, u64 value)
61+
{
62+
struct arm_smccc_res res;
63+
64+
arm_smccc_1_1_hvc(HV_FUNC_ID,
65+
HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
66+
HV_HYPERCALL_REP_COMP_1,
67+
HV_PARTITION_ID_SELF,
68+
HV_VP_INDEX_SELF,
69+
msr,
70+
0,
71+
value,
72+
0,
73+
&res);
74+
75+
/*
76+
* Something is fundamentally broken in the hypervisor if
77+
* setting a VP register fails. There's really no way to
78+
* continue as a guest VM, so panic.
79+
*/
80+
BUG_ON(!hv_result_success(res.a0));
81+
}
82+
EXPORT_SYMBOL_GPL(hv_set_vpreg);
83+
84+
/*
85+
* Get the value of a single VP register. One version
86+
* returns just 64 bits and another returns the full 128 bits.
87+
* The two versions are separate to avoid complicating the
88+
* calling sequence for the more frequently used 64 bit version.
89+
*/
90+
91+
void hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result)
92+
{
93+
struct arm_smccc_1_2_regs args;
94+
struct arm_smccc_1_2_regs res;
95+
96+
args.a0 = HV_FUNC_ID;
97+
args.a1 = HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
98+
HV_HYPERCALL_REP_COMP_1;
99+
args.a2 = HV_PARTITION_ID_SELF;
100+
args.a3 = HV_VP_INDEX_SELF;
101+
args.a4 = msr;
102+
103+
/*
104+
* Use the SMCCC 1.2 interface because the results are in registers
105+
* beyond X0-X3.
106+
*/
107+
arm_smccc_1_2_hvc(&args, &res);
108+
109+
/*
110+
* Something is fundamentally broken in the hypervisor if
111+
* getting a VP register fails. There's really no way to
112+
* continue as a guest VM, so panic.
113+
*/
114+
BUG_ON(!hv_result_success(res.a0));
115+
116+
result->as64.low = res.a6;
117+
result->as64.high = res.a7;
118+
}
119+
EXPORT_SYMBOL_GPL(hv_get_vpreg_128);
120+
121+
u64 hv_get_vpreg(u32 msr)
122+
{
123+
struct hv_get_vp_registers_output output;
124+
125+
hv_get_vpreg_128(msr, &output);
126+
127+
return output.as64.low;
128+
}
129+
EXPORT_SYMBOL_GPL(hv_get_vpreg);
130+
131+
/*
132+
* hyperv_report_panic - report a panic to Hyper-V. This function uses
133+
* the older version of the Hyper-V interface that admittedly doesn't
134+
* pass enough information to be useful beyond just recording the
135+
* occurrence of a panic. The parallel hv_kmsg_dump() uses the
136+
* new interface that allows reporting 4 Kbytes of data, which is much
137+
* more useful. Hyper-V on ARM64 always supports the newer interface, but
138+
* we retain support for the older version because the sysadmin is allowed
139+
* to disable the newer version via sysctl in case of information security
140+
* concerns about the more verbose version.
141+
*/
142+
void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
143+
{
144+
static bool panic_reported;
145+
u64 guest_id;
146+
147+
/* Don't report a panic to Hyper-V if we're not going to panic */
148+
if (in_die && !panic_on_oops)
149+
return;
150+
151+
/*
152+
* We prefer to report panic on 'die' chain as we have proper
153+
* registers to report, but if we miss it (e.g. on BUG()) we need
154+
* to report it on 'panic'.
155+
*
156+
* Calling code in the 'die' and 'panic' paths ensures that only
157+
* one CPU is running this code, so no atomicity is needed.
158+
*/
159+
if (panic_reported)
160+
return;
161+
panic_reported = true;
162+
163+
guest_id = hv_get_vpreg(HV_REGISTER_GUEST_OSID);
164+
165+
/*
166+
* Hyper-V provides the ability to store only 5 values.
167+
* Pick the passed in error value, the guest_id, the PC,
168+
* and the SP.
169+
*/
170+
hv_set_vpreg(HV_REGISTER_CRASH_P0, err);
171+
hv_set_vpreg(HV_REGISTER_CRASH_P1, guest_id);
172+
hv_set_vpreg(HV_REGISTER_CRASH_P2, regs->pc);
173+
hv_set_vpreg(HV_REGISTER_CRASH_P3, regs->sp);
174+
hv_set_vpreg(HV_REGISTER_CRASH_P4, 0);
175+
176+
/*
177+
* Let Hyper-V know there is crash data available
178+
*/
179+
hv_set_vpreg(HV_REGISTER_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
180+
}
181+
EXPORT_SYMBOL_GPL(hyperv_report_panic);

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

arch/arm64/include/asm/hyperv-tlfs.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
/*
4+
* This file contains definitions from the Hyper-V Hypervisor Top-Level
5+
* Functional Specification (TLFS):
6+
* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs
7+
*
8+
* Copyright (C) 2021, Microsoft, Inc.
9+
*
10+
* Author : Michael Kelley <[email protected]>
11+
*/
12+
13+
#ifndef _ASM_HYPERV_TLFS_H
14+
#define _ASM_HYPERV_TLFS_H
15+
16+
#include <linux/types.h>
17+
18+
/*
19+
* All data structures defined in the TLFS that are shared between Hyper-V
20+
* and a guest VM use Little Endian byte ordering. This matches the default
21+
* byte ordering of Linux running on ARM64, so no special handling is required.
22+
*/
23+
24+
/*
25+
* These Hyper-V registers provide information equivalent to the CPUID
26+
* instruction on x86/x64.
27+
*/
28+
#define HV_REGISTER_HYPERVISOR_VERSION 0x00000100 /*CPUID 0x40000002 */
29+
#define HV_REGISTER_FEATURES 0x00000200 /*CPUID 0x40000003 */
30+
#define HV_REGISTER_ENLIGHTENMENTS 0x00000201 /*CPUID 0x40000004 */
31+
32+
/*
33+
* Group C Features. See the asm-generic version of hyperv-tlfs.h
34+
* for a description of Feature Groups.
35+
*/
36+
37+
/* Crash MSRs available */
38+
#define HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE BIT(8)
39+
40+
/* STIMER direct mode is available */
41+
#define HV_STIMER_DIRECT_MODE_AVAILABLE BIT(13)
42+
43+
/*
44+
* Synthetic register definitions equivalent to MSRs on x86/x64
45+
*/
46+
#define HV_REGISTER_CRASH_P0 0x00000210
47+
#define HV_REGISTER_CRASH_P1 0x00000211
48+
#define HV_REGISTER_CRASH_P2 0x00000212
49+
#define HV_REGISTER_CRASH_P3 0x00000213
50+
#define HV_REGISTER_CRASH_P4 0x00000214
51+
#define HV_REGISTER_CRASH_CTL 0x00000215
52+
53+
#define HV_REGISTER_GUEST_OSID 0x00090002
54+
#define HV_REGISTER_VP_INDEX 0x00090003
55+
#define HV_REGISTER_TIME_REF_COUNT 0x00090004
56+
#define HV_REGISTER_REFERENCE_TSC 0x00090017
57+
58+
#define HV_REGISTER_SINT0 0x000A0000
59+
#define HV_REGISTER_SCONTROL 0x000A0010
60+
#define HV_REGISTER_SIEFP 0x000A0012
61+
#define HV_REGISTER_SIMP 0x000A0013
62+
#define HV_REGISTER_EOM 0x000A0014
63+
64+
#define HV_REGISTER_STIMER0_CONFIG 0x000B0000
65+
#define HV_REGISTER_STIMER0_COUNT 0x000B0001
66+
67+
#include <asm-generic/hyperv-tlfs.h>
68+
69+
#endif

0 commit comments

Comments
 (0)