1 // SPDX-License-Identifier: GPL-2.0
4 * Low level utility routines for interacting with Hyper-V.
6 * Copyright (C) 2021, Microsoft, Inc.
11 #include <linux/types.h>
12 #include <linux/export.h>
14 #include <linux/arm-smccc.h>
15 #include <linux/module.h>
16 #include <asm-generic/bug.h>
17 #include <hyperv/hvhdk.h>
18 #include <asm/mshyperv.h>
21 * hv_do_hypercall- Invoke the specified hypercall
23 u64 hv_do_hypercall(u64 control, void *input, void *output)
25 struct arm_smccc_res res;
29 input_address = input ? virt_to_phys(input) : 0;
30 output_address = output ? virt_to_phys(output) : 0;
32 arm_smccc_1_1_hvc(HV_FUNC_ID, control,
33 input_address, output_address, &res);
36 EXPORT_SYMBOL_GPL(hv_do_hypercall);
39 * hv_do_fast_hypercall8 -- Invoke the specified hypercall
40 * with arguments in registers instead of physical memory.
41 * Avoids the overhead of virt_to_phys for simple hypercalls.
44 u64 hv_do_fast_hypercall8(u16 code, u64 input)
46 struct arm_smccc_res res;
49 control = (u64)code | HV_HYPERCALL_FAST_BIT;
51 arm_smccc_1_1_hvc(HV_FUNC_ID, control, input, &res);
54 EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8);
57 * Set a single VP register to a 64-bit value.
59 void hv_set_vpreg(u32 msr, u64 value)
61 struct arm_smccc_res res;
63 arm_smccc_1_1_hvc(HV_FUNC_ID,
64 HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
65 HV_HYPERCALL_REP_COMP_1,
75 * Something is fundamentally broken in the hypervisor if
76 * setting a VP register fails. There's really no way to
77 * continue as a guest VM, so panic.
79 BUG_ON(!hv_result_success(res.a0));
81 EXPORT_SYMBOL_GPL(hv_set_vpreg);
84 * Get the value of a single VP register. One version
85 * returns just 64 bits and another returns the full 128 bits.
86 * The two versions are separate to avoid complicating the
87 * calling sequence for the more frequently used 64 bit version.
90 void hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result)
92 struct arm_smccc_1_2_regs args;
93 struct arm_smccc_1_2_regs res;
96 args.a1 = HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
97 HV_HYPERCALL_REP_COMP_1;
98 args.a2 = HV_PARTITION_ID_SELF;
99 args.a3 = HV_VP_INDEX_SELF;
103 * Use the SMCCC 1.2 interface because the results are in registers
106 arm_smccc_1_2_hvc(&args, &res);
109 * Something is fundamentally broken in the hypervisor if
110 * getting a VP register fails. There's really no way to
111 * continue as a guest VM, so panic.
113 BUG_ON(!hv_result_success(res.a0));
115 result->as64.low = res.a6;
116 result->as64.high = res.a7;
118 EXPORT_SYMBOL_GPL(hv_get_vpreg_128);
120 u64 hv_get_vpreg(u32 msr)
122 struct hv_get_vp_registers_output output;
124 hv_get_vpreg_128(msr, &output);
126 return output.as64.low;
128 EXPORT_SYMBOL_GPL(hv_get_vpreg);
131 * hyperv_report_panic - report a panic to Hyper-V. This function uses
132 * the older version of the Hyper-V interface that admittedly doesn't
133 * pass enough information to be useful beyond just recording the
134 * occurrence of a panic. The parallel hv_kmsg_dump() uses the
135 * new interface that allows reporting 4 Kbytes of data, which is much
136 * more useful. Hyper-V on ARM64 always supports the newer interface, but
137 * we retain support for the older version because the sysadmin is allowed
138 * to disable the newer version via sysctl in case of information security
139 * concerns about the more verbose version.
141 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
143 static bool panic_reported;
146 /* Don't report a panic to Hyper-V if we're not going to panic */
147 if (in_die && !panic_on_oops)
151 * We prefer to report panic on 'die' chain as we have proper
152 * registers to report, but if we miss it (e.g. on BUG()) we need
153 * to report it on 'panic'.
155 * Calling code in the 'die' and 'panic' paths ensures that only
156 * one CPU is running this code, so no atomicity is needed.
160 panic_reported = true;
162 guest_id = hv_get_vpreg(HV_REGISTER_GUEST_OS_ID);
165 * Hyper-V provides the ability to store only 5 values.
166 * Pick the passed in error value, the guest_id, the PC,
169 hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P0, err);
170 hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P1, guest_id);
171 hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P2, regs->pc);
172 hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P3, regs->sp);
173 hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P4, 0);
176 * Let Hyper-V know there is crash data available
178 hv_set_vpreg(HV_REGISTER_GUEST_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
180 EXPORT_SYMBOL_GPL(hyperv_report_panic);