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