xref: /openbmc/linux/arch/arm64/hyperv/hv_core.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
157d276bbSMichael Kelley // SPDX-License-Identifier: GPL-2.0
257d276bbSMichael Kelley 
357d276bbSMichael Kelley /*
457d276bbSMichael Kelley  * Low level utility routines for interacting with Hyper-V.
557d276bbSMichael Kelley  *
657d276bbSMichael Kelley  * Copyright (C) 2021, Microsoft, Inc.
757d276bbSMichael Kelley  *
857d276bbSMichael Kelley  * Author : Michael Kelley <mikelley@microsoft.com>
957d276bbSMichael Kelley  */
1057d276bbSMichael Kelley 
1157d276bbSMichael Kelley #include <linux/types.h>
1257d276bbSMichael Kelley #include <linux/export.h>
1357d276bbSMichael Kelley #include <linux/mm.h>
1457d276bbSMichael Kelley #include <linux/hyperv.h>
1557d276bbSMichael Kelley #include <linux/arm-smccc.h>
1657d276bbSMichael Kelley #include <linux/module.h>
1757d276bbSMichael Kelley #include <asm-generic/bug.h>
1857d276bbSMichael Kelley #include <asm/hyperv-tlfs.h>
1957d276bbSMichael Kelley #include <asm/mshyperv.h>
2057d276bbSMichael Kelley 
2157d276bbSMichael Kelley /*
2257d276bbSMichael Kelley  * hv_do_hypercall- Invoke the specified hypercall
2357d276bbSMichael Kelley  */
hv_do_hypercall(u64 control,void * input,void * output)2457d276bbSMichael Kelley u64 hv_do_hypercall(u64 control, void *input, void *output)
2557d276bbSMichael Kelley {
2657d276bbSMichael Kelley 	struct arm_smccc_res	res;
2757d276bbSMichael Kelley 	u64			input_address;
2857d276bbSMichael Kelley 	u64			output_address;
2957d276bbSMichael Kelley 
3057d276bbSMichael Kelley 	input_address = input ? virt_to_phys(input) : 0;
3157d276bbSMichael Kelley 	output_address = output ? virt_to_phys(output) : 0;
3257d276bbSMichael Kelley 
3357d276bbSMichael Kelley 	arm_smccc_1_1_hvc(HV_FUNC_ID, control,
3457d276bbSMichael Kelley 			  input_address, output_address, &res);
3557d276bbSMichael Kelley 	return res.a0;
3657d276bbSMichael Kelley }
3757d276bbSMichael Kelley EXPORT_SYMBOL_GPL(hv_do_hypercall);
3857d276bbSMichael Kelley 
3957d276bbSMichael Kelley /*
4057d276bbSMichael Kelley  * hv_do_fast_hypercall8 -- Invoke the specified hypercall
4157d276bbSMichael Kelley  * with arguments in registers instead of physical memory.
4257d276bbSMichael Kelley  * Avoids the overhead of virt_to_phys for simple hypercalls.
4357d276bbSMichael Kelley  */
4457d276bbSMichael Kelley 
hv_do_fast_hypercall8(u16 code,u64 input)4557d276bbSMichael Kelley u64 hv_do_fast_hypercall8(u16 code, u64 input)
4657d276bbSMichael Kelley {
4757d276bbSMichael Kelley 	struct arm_smccc_res	res;
4857d276bbSMichael Kelley 	u64			control;
4957d276bbSMichael Kelley 
5057d276bbSMichael Kelley 	control = (u64)code | HV_HYPERCALL_FAST_BIT;
5157d276bbSMichael Kelley 
5257d276bbSMichael Kelley 	arm_smccc_1_1_hvc(HV_FUNC_ID, control, input, &res);
5357d276bbSMichael Kelley 	return res.a0;
5457d276bbSMichael Kelley }
5557d276bbSMichael Kelley EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8);
5657d276bbSMichael Kelley 
5757d276bbSMichael Kelley /*
5857d276bbSMichael Kelley  * Set a single VP register to a 64-bit value.
5957d276bbSMichael Kelley  */
hv_set_vpreg(u32 msr,u64 value)6057d276bbSMichael Kelley void hv_set_vpreg(u32 msr, u64 value)
6157d276bbSMichael Kelley {
6257d276bbSMichael Kelley 	struct arm_smccc_res res;
6357d276bbSMichael Kelley 
6457d276bbSMichael Kelley 	arm_smccc_1_1_hvc(HV_FUNC_ID,
6557d276bbSMichael Kelley 		HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
6657d276bbSMichael Kelley 			HV_HYPERCALL_REP_COMP_1,
6757d276bbSMichael Kelley 		HV_PARTITION_ID_SELF,
6857d276bbSMichael Kelley 		HV_VP_INDEX_SELF,
6957d276bbSMichael Kelley 		msr,
7057d276bbSMichael Kelley 		0,
7157d276bbSMichael Kelley 		value,
7257d276bbSMichael Kelley 		0,
7357d276bbSMichael Kelley 		&res);
7457d276bbSMichael Kelley 
7557d276bbSMichael Kelley 	/*
7657d276bbSMichael Kelley 	 * Something is fundamentally broken in the hypervisor if
7757d276bbSMichael Kelley 	 * setting a VP register fails. There's really no way to
7857d276bbSMichael Kelley 	 * continue as a guest VM, so panic.
7957d276bbSMichael Kelley 	 */
8057d276bbSMichael Kelley 	BUG_ON(!hv_result_success(res.a0));
8157d276bbSMichael Kelley }
8257d276bbSMichael Kelley EXPORT_SYMBOL_GPL(hv_set_vpreg);
8357d276bbSMichael Kelley 
8457d276bbSMichael Kelley /*
8557d276bbSMichael Kelley  * Get the value of a single VP register.  One version
8657d276bbSMichael Kelley  * returns just 64 bits and another returns the full 128 bits.
8757d276bbSMichael Kelley  * The two versions are separate to avoid complicating the
8857d276bbSMichael Kelley  * calling sequence for the more frequently used 64 bit version.
8957d276bbSMichael Kelley  */
9057d276bbSMichael Kelley 
hv_get_vpreg_128(u32 msr,struct hv_get_vp_registers_output * result)9157d276bbSMichael Kelley void hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result)
9257d276bbSMichael Kelley {
9357d276bbSMichael Kelley 	struct arm_smccc_1_2_regs args;
9457d276bbSMichael Kelley 	struct arm_smccc_1_2_regs res;
9557d276bbSMichael Kelley 
9657d276bbSMichael Kelley 	args.a0 = HV_FUNC_ID;
9757d276bbSMichael Kelley 	args.a1 = HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
9857d276bbSMichael Kelley 			HV_HYPERCALL_REP_COMP_1;
9957d276bbSMichael Kelley 	args.a2 = HV_PARTITION_ID_SELF;
10057d276bbSMichael Kelley 	args.a3 = HV_VP_INDEX_SELF;
10157d276bbSMichael Kelley 	args.a4 = msr;
10257d276bbSMichael Kelley 
10357d276bbSMichael Kelley 	/*
10457d276bbSMichael Kelley 	 * Use the SMCCC 1.2 interface because the results are in registers
10557d276bbSMichael Kelley 	 * beyond X0-X3.
10657d276bbSMichael Kelley 	 */
10757d276bbSMichael Kelley 	arm_smccc_1_2_hvc(&args, &res);
10857d276bbSMichael Kelley 
10957d276bbSMichael Kelley 	/*
11057d276bbSMichael Kelley 	 * Something is fundamentally broken in the hypervisor if
11157d276bbSMichael Kelley 	 * getting a VP register fails. There's really no way to
11257d276bbSMichael Kelley 	 * continue as a guest VM, so panic.
11357d276bbSMichael Kelley 	 */
11457d276bbSMichael Kelley 	BUG_ON(!hv_result_success(res.a0));
11557d276bbSMichael Kelley 
11657d276bbSMichael Kelley 	result->as64.low = res.a6;
11757d276bbSMichael Kelley 	result->as64.high = res.a7;
11857d276bbSMichael Kelley }
11957d276bbSMichael Kelley EXPORT_SYMBOL_GPL(hv_get_vpreg_128);
12057d276bbSMichael Kelley 
hv_get_vpreg(u32 msr)12157d276bbSMichael Kelley u64 hv_get_vpreg(u32 msr)
12257d276bbSMichael Kelley {
12357d276bbSMichael Kelley 	struct hv_get_vp_registers_output output;
12457d276bbSMichael Kelley 
12557d276bbSMichael Kelley 	hv_get_vpreg_128(msr, &output);
12657d276bbSMichael Kelley 
12757d276bbSMichael Kelley 	return output.as64.low;
12857d276bbSMichael Kelley }
12957d276bbSMichael Kelley EXPORT_SYMBOL_GPL(hv_get_vpreg);
130*512c1117SMichael Kelley 
131*512c1117SMichael Kelley /*
132*512c1117SMichael Kelley  * hyperv_report_panic - report a panic to Hyper-V.  This function uses
133*512c1117SMichael Kelley  * the older version of the Hyper-V interface that admittedly doesn't
134*512c1117SMichael Kelley  * pass enough information to be useful beyond just recording the
135*512c1117SMichael Kelley  * occurrence of a panic. The parallel hv_kmsg_dump() uses the
136*512c1117SMichael Kelley  * new interface that allows reporting 4 Kbytes of data, which is much
137*512c1117SMichael Kelley  * more useful. Hyper-V on ARM64 always supports the newer interface, but
138*512c1117SMichael Kelley  * we retain support for the older version because the sysadmin is allowed
139*512c1117SMichael Kelley  * to disable the newer version via sysctl in case of information security
140*512c1117SMichael Kelley  * concerns about the more verbose version.
141*512c1117SMichael Kelley  */
hyperv_report_panic(struct pt_regs * regs,long err,bool in_die)142*512c1117SMichael Kelley void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
143*512c1117SMichael Kelley {
144*512c1117SMichael Kelley 	static bool	panic_reported;
145*512c1117SMichael Kelley 	u64		guest_id;
146*512c1117SMichael Kelley 
147*512c1117SMichael Kelley 	/* Don't report a panic to Hyper-V if we're not going to panic */
148*512c1117SMichael Kelley 	if (in_die && !panic_on_oops)
149*512c1117SMichael Kelley 		return;
150*512c1117SMichael Kelley 
151*512c1117SMichael Kelley 	/*
152*512c1117SMichael Kelley 	 * We prefer to report panic on 'die' chain as we have proper
153*512c1117SMichael Kelley 	 * registers to report, but if we miss it (e.g. on BUG()) we need
154*512c1117SMichael Kelley 	 * to report it on 'panic'.
155*512c1117SMichael Kelley 	 *
156*512c1117SMichael Kelley 	 * Calling code in the 'die' and 'panic' paths ensures that only
157*512c1117SMichael Kelley 	 * one CPU is running this code, so no atomicity is needed.
158*512c1117SMichael Kelley 	 */
159*512c1117SMichael Kelley 	if (panic_reported)
160*512c1117SMichael Kelley 		return;
161*512c1117SMichael Kelley 	panic_reported = true;
162*512c1117SMichael Kelley 
163*512c1117SMichael Kelley 	guest_id = hv_get_vpreg(HV_REGISTER_GUEST_OSID);
164*512c1117SMichael Kelley 
165*512c1117SMichael Kelley 	/*
166*512c1117SMichael Kelley 	 * Hyper-V provides the ability to store only 5 values.
167*512c1117SMichael Kelley 	 * Pick the passed in error value, the guest_id, the PC,
168*512c1117SMichael Kelley 	 * and the SP.
169*512c1117SMichael Kelley 	 */
170*512c1117SMichael Kelley 	hv_set_vpreg(HV_REGISTER_CRASH_P0, err);
171*512c1117SMichael Kelley 	hv_set_vpreg(HV_REGISTER_CRASH_P1, guest_id);
172*512c1117SMichael Kelley 	hv_set_vpreg(HV_REGISTER_CRASH_P2, regs->pc);
173*512c1117SMichael Kelley 	hv_set_vpreg(HV_REGISTER_CRASH_P3, regs->sp);
174*512c1117SMichael Kelley 	hv_set_vpreg(HV_REGISTER_CRASH_P4, 0);
175*512c1117SMichael Kelley 
176*512c1117SMichael Kelley 	/*
177*512c1117SMichael Kelley 	 * Let Hyper-V know there is crash data available
178*512c1117SMichael Kelley 	 */
179*512c1117SMichael Kelley 	hv_set_vpreg(HV_REGISTER_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
180*512c1117SMichael Kelley }
181*512c1117SMichael Kelley EXPORT_SYMBOL_GPL(hyperv_report_panic);
182