// SPDX-License-Identifier: GPL-2.012/*3* Low level utility routines for interacting with Hyper-V.4*5* Copyright (C) 2021, Microsoft, Inc.6*7* Author : Michael Kelley <[email protected]>8*/910#include <linux/types.h>11#include <linux/export.h>12#include <linux/mm.h>13#include <linux/arm-smccc.h>14#include <linux/module.h>15#include <asm-generic/bug.h>16#include <hyperv/hvhdk.h>17#include <asm/mshyperv.h>1819/*20* hv_do_hypercall- Invoke the specified hypercall21*/22u64 hv_do_hypercall(u64 control, void *input, void *output)23{24struct arm_smccc_res res;25u64 input_address;26u64 output_address;2728input_address = input ? virt_to_phys(input) : 0;29output_address = output ? virt_to_phys(output) : 0;3031arm_smccc_1_1_hvc(HV_FUNC_ID, control,32input_address, output_address, &res);33return res.a0;34}35EXPORT_SYMBOL_GPL(hv_do_hypercall);3637/*38* hv_do_fast_hypercall8 -- Invoke the specified hypercall39* with arguments in registers instead of physical memory.40* Avoids the overhead of virt_to_phys for simple hypercalls.41*/4243u64 hv_do_fast_hypercall8(u16 code, u64 input)44{45struct arm_smccc_res res;46u64 control;4748control = (u64)code | HV_HYPERCALL_FAST_BIT;4950arm_smccc_1_1_hvc(HV_FUNC_ID, control, input, &res);51return res.a0;52}53EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8);5455/*56* hv_do_fast_hypercall16 -- Invoke the specified hypercall57* with arguments in registers instead of physical memory.58* Avoids the overhead of virt_to_phys for simple hypercalls.59*/60u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)61{62struct arm_smccc_res res;63u64 control;6465control = (u64)code | HV_HYPERCALL_FAST_BIT;6667arm_smccc_1_1_hvc(HV_FUNC_ID, control, input1, input2, &res);68return res.a0;69}70EXPORT_SYMBOL_GPL(hv_do_fast_hypercall16);7172/*73* Set a single VP register to a 64-bit value.74*/75void hv_set_vpreg(u32 msr, u64 value)76{77struct arm_smccc_res res;7879arm_smccc_1_1_hvc(HV_FUNC_ID,80HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |81HV_HYPERCALL_REP_COMP_1,82HV_PARTITION_ID_SELF,83HV_VP_INDEX_SELF,84msr,850,86value,870,88&res);8990/*91* Something is fundamentally broken in the hypervisor if92* setting a VP register fails. There's really no way to93* continue as a guest VM, so panic.94*/95BUG_ON(!hv_result_success(res.a0));96}97EXPORT_SYMBOL_GPL(hv_set_vpreg);9899/*100* Get the value of a single VP register. One version101* returns just 64 bits and another returns the full 128 bits.102* The two versions are separate to avoid complicating the103* calling sequence for the more frequently used 64 bit version.104*/105106void hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result)107{108struct arm_smccc_1_2_regs args;109struct arm_smccc_1_2_regs res;110111args.a0 = HV_FUNC_ID;112args.a1 = HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |113HV_HYPERCALL_REP_COMP_1;114args.a2 = HV_PARTITION_ID_SELF;115args.a3 = HV_VP_INDEX_SELF;116args.a4 = msr;117118/*119* Use the SMCCC 1.2 interface because the results are in registers120* beyond X0-X3.121*/122arm_smccc_1_2_hvc(&args, &res);123124/*125* Something is fundamentally broken in the hypervisor if126* getting a VP register fails. There's really no way to127* continue as a guest VM, so panic.128*/129BUG_ON(!hv_result_success(res.a0));130131result->as64.low = res.a6;132result->as64.high = res.a7;133}134EXPORT_SYMBOL_GPL(hv_get_vpreg_128);135136u64 hv_get_vpreg(u32 msr)137{138struct hv_get_vp_registers_output output;139140hv_get_vpreg_128(msr, &output);141142return output.as64.low;143}144EXPORT_SYMBOL_GPL(hv_get_vpreg);145146/*147* hyperv_report_panic - report a panic to Hyper-V. This function uses148* the older version of the Hyper-V interface that admittedly doesn't149* pass enough information to be useful beyond just recording the150* occurrence of a panic. The parallel hv_kmsg_dump() uses the151* new interface that allows reporting 4 Kbytes of data, which is much152* more useful. Hyper-V on ARM64 always supports the newer interface, but153* we retain support for the older version because the sysadmin is allowed154* to disable the newer version via sysctl in case of information security155* concerns about the more verbose version.156*/157void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)158{159static bool panic_reported;160u64 guest_id;161162/* Don't report a panic to Hyper-V if we're not going to panic */163if (in_die && !panic_on_oops)164return;165166/*167* We prefer to report panic on 'die' chain as we have proper168* registers to report, but if we miss it (e.g. on BUG()) we need169* to report it on 'panic'.170*171* Calling code in the 'die' and 'panic' paths ensures that only172* one CPU is running this code, so no atomicity is needed.173*/174if (panic_reported)175return;176panic_reported = true;177178guest_id = hv_get_vpreg(HV_REGISTER_GUEST_OS_ID);179180/*181* Hyper-V provides the ability to store only 5 values.182* Pick the passed in error value, the guest_id, the PC,183* and the SP.184*/185hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P0, err);186hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P1, guest_id);187hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P2, regs->pc);188hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P3, regs->sp);189hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P4, 0);190191/*192* Let Hyper-V know there is crash data available193*/194hv_set_vpreg(HV_REGISTER_GUEST_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);195}196EXPORT_SYMBOL_GPL(hyperv_report_panic);197198199