|
| 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 | + struct arm_smccc_res res; |
| 27 | + u64 input_address; |
| 28 | + u64 output_address; |
| 29 | + |
| 30 | + input_address = input ? virt_to_phys(input) : 0; |
| 31 | + output_address = output ? virt_to_phys(output) : 0; |
| 32 | + |
| 33 | + arm_smccc_1_1_hvc(HV_FUNC_ID, control, |
| 34 | + input_address, output_address, &res); |
| 35 | + return res.a0; |
| 36 | +} |
| 37 | +EXPORT_SYMBOL_GPL(hv_do_hypercall); |
| 38 | + |
| 39 | +/* |
| 40 | + * hv_do_fast_hypercall8 -- Invoke the specified hypercall |
| 41 | + * with arguments in registers instead of physical memory. |
| 42 | + * Avoids the overhead of virt_to_phys for simple hypercalls. |
| 43 | + */ |
| 44 | + |
| 45 | +u64 hv_do_fast_hypercall8(u16 code, u64 input) |
| 46 | +{ |
| 47 | + struct arm_smccc_res res; |
| 48 | + u64 control; |
| 49 | + |
| 50 | + control = (u64)code | HV_HYPERCALL_FAST_BIT; |
| 51 | + |
| 52 | + arm_smccc_1_1_hvc(HV_FUNC_ID, control, input, &res); |
| 53 | + return res.a0; |
| 54 | +} |
| 55 | +EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8); |
| 56 | + |
| 57 | +/* |
| 58 | + * Set a single VP register to a 64-bit value. |
| 59 | + */ |
| 60 | +void hv_set_vpreg(u32 msr, u64 value) |
| 61 | +{ |
| 62 | + struct arm_smccc_res res; |
| 63 | + |
| 64 | + arm_smccc_1_1_hvc( |
| 65 | + HV_FUNC_ID, |
| 66 | + HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT | |
| 67 | + HV_HYPERCALL_REP_COMP_1, |
| 68 | + HV_PARTITION_ID_SELF, |
| 69 | + HV_VP_INDEX_SELF, |
| 70 | + msr, |
| 71 | + 0, |
| 72 | + value, |
| 73 | + 0, |
| 74 | + &res); |
| 75 | + |
| 76 | + /* |
| 77 | + * Something is fundamentally broken in the hypervisor if |
| 78 | + * setting a VP register fails. There's really no way to |
| 79 | + * continue as a guest VM, so panic. |
| 80 | + */ |
| 81 | + BUG_ON((res.a0 & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS); |
| 82 | +} |
| 83 | +EXPORT_SYMBOL_GPL(hv_set_vpreg); |
| 84 | + |
| 85 | +/* |
| 86 | + * Get the value of a single VP register. One version |
| 87 | + * returns just 64 bits and another returns the full 128 bits. |
| 88 | + * The two versions are separate to avoid complicating the |
| 89 | + * calling sequence for the more frequently used 64 bit version. |
| 90 | + */ |
| 91 | + |
| 92 | +void hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result) |
| 93 | +{ |
| 94 | + struct arm_smccc_res res; |
| 95 | + |
| 96 | + arm_smccc_1_1_hvc_reg("r5", "r6", "r7", |
| 97 | + HV_FUNC_ID, |
| 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 | + 0, |
| 104 | + &res); |
| 105 | + |
| 106 | + /* |
| 107 | + * Something is fundamentally broken in the hypervisor if |
| 108 | + * getting a VP register fails. There's really no way to |
| 109 | + * continue as a guest VM, so panic. |
| 110 | + */ |
| 111 | + BUG_ON((res.a0 & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS); |
| 112 | + |
| 113 | + result->as64.low = res.a2; /* r6 */ |
| 114 | + result->as64.high = res.a3; /* r7 */ |
| 115 | +} |
| 116 | +EXPORT_SYMBOL_GPL(hv_get_vpreg_128); |
| 117 | + |
| 118 | +u64 hv_get_vpreg(u32 msr) |
| 119 | +{ |
| 120 | + struct hv_get_vp_registers_output output; |
| 121 | + |
| 122 | + hv_get_vpreg_128(msr, &output); |
| 123 | + |
| 124 | + return output.as64.low; |
| 125 | +} |
| 126 | +EXPORT_SYMBOL_GPL(hv_get_vpreg); |
0 commit comments