|
| 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 <mikelley@microsoft.com> |
| 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 | + 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 | + return hv_do_hvc(control, input_address, output_address); |
| 33 | +} |
| 34 | +EXPORT_SYMBOL_GPL(hv_do_hypercall); |
| 35 | + |
| 36 | +/* |
| 37 | + * hv_do_fast_hypercall8 -- Invoke the specified hypercall |
| 38 | + * with arguments in registers instead of physical memory. |
| 39 | + * Avoids the overhead of virt_to_phys for simple hypercalls. |
| 40 | + */ |
| 41 | +u64 hv_do_fast_hypercall8(u16 code, u64 input) |
| 42 | +{ |
| 43 | + u64 control; |
| 44 | + |
| 45 | + control = (u64)code | HV_HYPERCALL_FAST_BIT; |
| 46 | + return hv_do_hvc(control, input); |
| 47 | +} |
| 48 | +EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8); |
| 49 | + |
| 50 | +union hv_hypercall_status { |
| 51 | + u64 as_uint64; |
| 52 | + struct { |
| 53 | + u16 status; |
| 54 | + u16 reserved; |
| 55 | + u16 reps_completed; /* Low 12 bits */ |
| 56 | + u16 reserved2; |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +/* |
| 61 | + * Set a single VP register to a 64-bit value. |
| 62 | + */ |
| 63 | +void hv_set_vpreg(u32 msr, u64 value) |
| 64 | +{ |
| 65 | + union hv_hypercall_status status; |
| 66 | + |
| 67 | + status.as_uint64 = hv_do_hvc( |
| 68 | + HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT | |
| 69 | + HV_HYPERCALL_REP_COMP_1, |
| 70 | + HV_PARTITION_ID_SELF, |
| 71 | + HV_VP_INDEX_SELF, |
| 72 | + msr, |
| 73 | + 0, |
| 74 | + value, |
| 75 | + 0); |
| 76 | + |
| 77 | + /* |
| 78 | + * Something is fundamentally broken in the hypervisor if |
| 79 | + * setting a VP register fails. There's really no way to |
| 80 | + * continue as a guest VM, so panic. |
| 81 | + */ |
| 82 | + BUG_ON(status.status != HV_STATUS_SUCCESS); |
| 83 | +} |
| 84 | +EXPORT_SYMBOL_GPL(hv_set_vpreg); |
| 85 | + |
| 86 | +/* |
| 87 | + * Get the value of a single VP register. One version |
| 88 | + * returns just 64 bits and another returns the full 128 bits. |
| 89 | + * The two versions are separate to avoid complicating the |
| 90 | + * calling sequence for the more frequently used 64 bit version. |
| 91 | + */ |
| 92 | + |
| 93 | +void hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result) |
| 94 | +{ |
| 95 | + u64 status; |
| 96 | + |
| 97 | + status = hv_do_hvc_fast_get( |
| 98 | + HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT | |
| 99 | + HV_HYPERCALL_REP_COMP_1, |
| 100 | + HV_PARTITION_ID_SELF, |
| 101 | + HV_VP_INDEX_SELF, |
| 102 | + msr, |
| 103 | + result); |
| 104 | + |
| 105 | + /* |
| 106 | + * Something is fundamentally broken in the hypervisor if |
| 107 | + * getting a VP register fails. There's really no way to |
| 108 | + * continue as a guest VM, so panic. |
| 109 | + */ |
| 110 | + BUG_ON((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS); |
| 111 | +} |
| 112 | +EXPORT_SYMBOL_GPL(hv_get_vpreg_128); |
| 113 | + |
| 114 | +u64 hv_get_vpreg(u32 msr) |
| 115 | +{ |
| 116 | + struct hv_get_vp_registers_output output; |
| 117 | + |
| 118 | + hv_get_vpreg_128(msr, &output); |
| 119 | + |
| 120 | + return output.as64.low; |
| 121 | +} |
| 122 | +EXPORT_SYMBOL_GPL(hv_get_vpreg); |
| 123 | + |
| 124 | +/* |
| 125 | + * hyperv_report_panic - report a panic to Hyper-V. This function uses |
| 126 | + * the older version of the Hyper-V interface that admittedly doesn't |
| 127 | + * pass enough information to be useful beyond just recording the |
| 128 | + * occurrence of a panic. The parallel hv_kmsg_dump() uses the |
| 129 | + * new interface that allows reporting 4 Kbytes of data, which is much |
| 130 | + * more useful. Hyper-V on ARM64 always supports the newer interface, but |
| 131 | + * we retain support for the older version because the sysadmin is allowed |
| 132 | + * to disable the newer version via sysctl in case of information security |
| 133 | + * concerns about the more verbose version. |
| 134 | + */ |
| 135 | +void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die) |
| 136 | +{ |
| 137 | + static bool panic_reported; |
| 138 | + u64 guest_id; |
| 139 | + |
| 140 | + /* Don't report a panic to Hyper-V if we're not going to panic */ |
| 141 | + if (in_die && !panic_on_oops) |
| 142 | + return; |
| 143 | + |
| 144 | + /* |
| 145 | + * We prefer to report panic on 'die' chain as we have proper |
| 146 | + * registers to report, but if we miss it (e.g. on BUG()) we need |
| 147 | + * to report it on 'panic'. |
| 148 | + * |
| 149 | + * Calling code in the 'die' and 'panic' paths ensures that only |
| 150 | + * one CPU is running this code, so no atomicity is needed. |
| 151 | + */ |
| 152 | + if (panic_reported) |
| 153 | + return; |
| 154 | + panic_reported = true; |
| 155 | + |
| 156 | + guest_id = hv_get_vpreg(HV_REGISTER_GUEST_OSID); |
| 157 | + |
| 158 | + /* |
| 159 | + * Hyper-V provides the ability to store only 5 values. |
| 160 | + * Pick the passed in error value, the guest_id, and the PC. |
| 161 | + * The first two general registers are added arbitrarily. |
| 162 | + */ |
| 163 | + hv_set_vpreg(HV_REGISTER_CRASH_P0, err); |
| 164 | + hv_set_vpreg(HV_REGISTER_CRASH_P1, guest_id); |
| 165 | + hv_set_vpreg(HV_REGISTER_CRASH_P2, regs->pc); |
| 166 | + hv_set_vpreg(HV_REGISTER_CRASH_P3, regs->regs[0]); |
| 167 | + hv_set_vpreg(HV_REGISTER_CRASH_P4, regs->regs[1]); |
| 168 | + |
| 169 | + /* |
| 170 | + * Let Hyper-V know there is crash data available |
| 171 | + */ |
| 172 | + hv_set_vpreg(HV_REGISTER_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); |
| 173 | +} |
| 174 | +EXPORT_SYMBOL_GPL(hyperv_report_panic); |
0 commit comments