Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /*
0004  * Low level utility routines for interacting with Hyper-V.
0005  *
0006  * Copyright (C) 2021, Microsoft, Inc.
0007  *
0008  * Author : Michael Kelley <mikelley@microsoft.com>
0009  */
0010 
0011 #include <linux/types.h>
0012 #include <linux/export.h>
0013 #include <linux/mm.h>
0014 #include <linux/hyperv.h>
0015 #include <linux/arm-smccc.h>
0016 #include <linux/module.h>
0017 #include <asm-generic/bug.h>
0018 #include <asm/hyperv-tlfs.h>
0019 #include <asm/mshyperv.h>
0020 
0021 /*
0022  * hv_do_hypercall- Invoke the specified hypercall
0023  */
0024 u64 hv_do_hypercall(u64 control, void *input, void *output)
0025 {
0026     struct arm_smccc_res    res;
0027     u64         input_address;
0028     u64         output_address;
0029 
0030     input_address = input ? virt_to_phys(input) : 0;
0031     output_address = output ? virt_to_phys(output) : 0;
0032 
0033     arm_smccc_1_1_hvc(HV_FUNC_ID, control,
0034               input_address, output_address, &res);
0035     return res.a0;
0036 }
0037 EXPORT_SYMBOL_GPL(hv_do_hypercall);
0038 
0039 /*
0040  * hv_do_fast_hypercall8 -- Invoke the specified hypercall
0041  * with arguments in registers instead of physical memory.
0042  * Avoids the overhead of virt_to_phys for simple hypercalls.
0043  */
0044 
0045 u64 hv_do_fast_hypercall8(u16 code, u64 input)
0046 {
0047     struct arm_smccc_res    res;
0048     u64         control;
0049 
0050     control = (u64)code | HV_HYPERCALL_FAST_BIT;
0051 
0052     arm_smccc_1_1_hvc(HV_FUNC_ID, control, input, &res);
0053     return res.a0;
0054 }
0055 EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8);
0056 
0057 /*
0058  * Set a single VP register to a 64-bit value.
0059  */
0060 void hv_set_vpreg(u32 msr, u64 value)
0061 {
0062     struct arm_smccc_res res;
0063 
0064     arm_smccc_1_1_hvc(HV_FUNC_ID,
0065         HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
0066             HV_HYPERCALL_REP_COMP_1,
0067         HV_PARTITION_ID_SELF,
0068         HV_VP_INDEX_SELF,
0069         msr,
0070         0,
0071         value,
0072         0,
0073         &res);
0074 
0075     /*
0076      * Something is fundamentally broken in the hypervisor if
0077      * setting a VP register fails. There's really no way to
0078      * continue as a guest VM, so panic.
0079      */
0080     BUG_ON(!hv_result_success(res.a0));
0081 }
0082 EXPORT_SYMBOL_GPL(hv_set_vpreg);
0083 
0084 /*
0085  * Get the value of a single VP register.  One version
0086  * returns just 64 bits and another returns the full 128 bits.
0087  * The two versions are separate to avoid complicating the
0088  * calling sequence for the more frequently used 64 bit version.
0089  */
0090 
0091 void hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result)
0092 {
0093     struct arm_smccc_1_2_regs args;
0094     struct arm_smccc_1_2_regs res;
0095 
0096     args.a0 = HV_FUNC_ID;
0097     args.a1 = HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
0098             HV_HYPERCALL_REP_COMP_1;
0099     args.a2 = HV_PARTITION_ID_SELF;
0100     args.a3 = HV_VP_INDEX_SELF;
0101     args.a4 = msr;
0102 
0103     /*
0104      * Use the SMCCC 1.2 interface because the results are in registers
0105      * beyond X0-X3.
0106      */
0107     arm_smccc_1_2_hvc(&args, &res);
0108 
0109     /*
0110      * Something is fundamentally broken in the hypervisor if
0111      * getting a VP register fails. There's really no way to
0112      * continue as a guest VM, so panic.
0113      */
0114     BUG_ON(!hv_result_success(res.a0));
0115 
0116     result->as64.low = res.a6;
0117     result->as64.high = res.a7;
0118 }
0119 EXPORT_SYMBOL_GPL(hv_get_vpreg_128);
0120 
0121 u64 hv_get_vpreg(u32 msr)
0122 {
0123     struct hv_get_vp_registers_output output;
0124 
0125     hv_get_vpreg_128(msr, &output);
0126 
0127     return output.as64.low;
0128 }
0129 EXPORT_SYMBOL_GPL(hv_get_vpreg);
0130 
0131 /*
0132  * hyperv_report_panic - report a panic to Hyper-V.  This function uses
0133  * the older version of the Hyper-V interface that admittedly doesn't
0134  * pass enough information to be useful beyond just recording the
0135  * occurrence of a panic. The parallel hv_kmsg_dump() uses the
0136  * new interface that allows reporting 4 Kbytes of data, which is much
0137  * more useful. Hyper-V on ARM64 always supports the newer interface, but
0138  * we retain support for the older version because the sysadmin is allowed
0139  * to disable the newer version via sysctl in case of information security
0140  * concerns about the more verbose version.
0141  */
0142 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
0143 {
0144     static bool panic_reported;
0145     u64     guest_id;
0146 
0147     /* Don't report a panic to Hyper-V if we're not going to panic */
0148     if (in_die && !panic_on_oops)
0149         return;
0150 
0151     /*
0152      * We prefer to report panic on 'die' chain as we have proper
0153      * registers to report, but if we miss it (e.g. on BUG()) we need
0154      * to report it on 'panic'.
0155      *
0156      * Calling code in the 'die' and 'panic' paths ensures that only
0157      * one CPU is running this code, so no atomicity is needed.
0158      */
0159     if (panic_reported)
0160         return;
0161     panic_reported = true;
0162 
0163     guest_id = hv_get_vpreg(HV_REGISTER_GUEST_OSID);
0164 
0165     /*
0166      * Hyper-V provides the ability to store only 5 values.
0167      * Pick the passed in error value, the guest_id, the PC,
0168      * and the SP.
0169      */
0170     hv_set_vpreg(HV_REGISTER_CRASH_P0, err);
0171     hv_set_vpreg(HV_REGISTER_CRASH_P1, guest_id);
0172     hv_set_vpreg(HV_REGISTER_CRASH_P2, regs->pc);
0173     hv_set_vpreg(HV_REGISTER_CRASH_P3, regs->sp);
0174     hv_set_vpreg(HV_REGISTER_CRASH_P4, 0);
0175 
0176     /*
0177      * Let Hyper-V know there is crash data available
0178      */
0179     hv_set_vpreg(HV_REGISTER_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
0180 }
0181 EXPORT_SYMBOL_GPL(hyperv_report_panic);