|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +/* |
| 4 | + * Initialization of the interface with Microsoft's Hyper-V hypervisor, |
| 5 | + * and various low level utility routines for interacting with Hyper-V. |
| 6 | + * |
| 7 | + * Copyright (C) 2019, Microsoft, Inc. |
| 8 | + * |
| 9 | + * Author : Michael Kelley <[email protected]> |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | +#include <linux/types.h> |
| 14 | +#include <linux/log2.h> |
| 15 | +#include <linux/version.h> |
| 16 | +#include <linux/export.h> |
| 17 | +#include <linux/mm.h> |
| 18 | +#include <linux/slab.h> |
| 19 | +#include <linux/hyperv.h> |
| 20 | +#include <linux/arm-smccc.h> |
| 21 | +#include <asm-generic/bug.h> |
| 22 | +#include <asm/hyperv-tlfs.h> |
| 23 | +#include <asm/mshyperv.h> |
| 24 | + |
| 25 | + |
| 26 | +/* |
| 27 | + * hv_do_hypercall- Invoke the specified hypercall |
| 28 | + */ |
| 29 | +u64 hv_do_hypercall(u64 control, void *input, void *output) |
| 30 | +{ |
| 31 | + u64 input_address; |
| 32 | + u64 output_address; |
| 33 | + struct arm_smccc_res res; |
| 34 | + |
| 35 | + input_address = input ? virt_to_phys(input) : 0; |
| 36 | + output_address = output ? virt_to_phys(output) : 0; |
| 37 | + |
| 38 | + arm_smccc_1_1_hvc(HV_FUNC_ID, control, |
| 39 | + input_address, output_address, &res); |
| 40 | + return res.a0; |
| 41 | +} |
| 42 | +EXPORT_SYMBOL_GPL(hv_do_hypercall); |
| 43 | + |
| 44 | +/* |
| 45 | + * hv_do_fast_hypercall8 -- Invoke the specified hypercall |
| 46 | + * with arguments in registers instead of physical memory. |
| 47 | + * Avoids the overhead of virt_to_phys for simple hypercalls. |
| 48 | + */ |
| 49 | + |
| 50 | +u64 hv_do_fast_hypercall8(u16 code, u64 input) |
| 51 | +{ |
| 52 | + u64 control; |
| 53 | + struct arm_smccc_res res; |
| 54 | + |
| 55 | + control = (u64)code | HV_HYPERCALL_FAST_BIT; |
| 56 | + |
| 57 | + arm_smccc_1_1_hvc(HV_FUNC_ID, control, input, &res); |
| 58 | + return res.a0; |
| 59 | +} |
| 60 | +EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8); |
| 61 | + |
| 62 | + |
| 63 | +/* |
| 64 | + * Set a single VP register to a 64-bit value. |
| 65 | + */ |
| 66 | +void hv_set_vpreg(u32 msr, u64 value) |
| 67 | +{ |
| 68 | + struct arm_smccc_res res; |
| 69 | + |
| 70 | + arm_smccc_1_1_hvc( |
| 71 | + HV_FUNC_ID, |
| 72 | + HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT | |
| 73 | + HV_HYPERCALL_REP_COMP_1, |
| 74 | + HV_PARTITION_ID_SELF, |
| 75 | + HV_VP_INDEX_SELF, |
| 76 | + msr, |
| 77 | + 0, |
| 78 | + value, |
| 79 | + 0, |
| 80 | + &res); |
| 81 | + |
| 82 | + /* |
| 83 | + * Something is fundamentally broken in the hypervisor if |
| 84 | + * setting a VP register fails. There's really no way to |
| 85 | + * continue as a guest VM, so panic. |
| 86 | + */ |
| 87 | + BUG_ON((res.a0 & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS); |
| 88 | +} |
| 89 | +EXPORT_SYMBOL_GPL(hv_set_vpreg); |
| 90 | + |
| 91 | +/* |
| 92 | + * Get the value of a single VP register. One version |
| 93 | + * returns just 64 bits and another returns the full 128 bits. |
| 94 | + * The two versions are separate to avoid complicating the |
| 95 | + * calling sequence for the more frequently used 64 bit version. |
| 96 | + */ |
| 97 | + |
| 98 | +static void __hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *res) |
| 99 | +{ |
| 100 | + struct hv_get_vp_registers_input *input; |
| 101 | + u64 status; |
| 102 | + |
| 103 | + /* |
| 104 | + * Allocate a power of 2 size so alignment to that size is |
| 105 | + * guaranteed, since the hypercall input area must not cross |
| 106 | + * a page boundary. |
| 107 | + */ |
| 108 | + |
| 109 | + input = kzalloc(roundup_pow_of_two(sizeof(input->header) + |
| 110 | + sizeof(input->element[0])), GFP_ATOMIC); |
| 111 | + |
| 112 | + input->header.partitionid = HV_PARTITION_ID_SELF; |
| 113 | + input->header.vpindex = HV_VP_INDEX_SELF; |
| 114 | + input->header.inputvtl = 0; |
| 115 | + input->element[0].name0 = msr; |
| 116 | + input->element[0].name1 = 0; |
| 117 | + |
| 118 | + |
| 119 | + status = hv_do_hypercall( |
| 120 | + HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_REP_COMP_1, |
| 121 | + input, res); |
| 122 | + |
| 123 | + /* |
| 124 | + * Something is fundamentally broken in the hypervisor if |
| 125 | + * getting a VP register fails. There's really no way to |
| 126 | + * continue as a guest VM, so panic. |
| 127 | + */ |
| 128 | + BUG_ON((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS); |
| 129 | + |
| 130 | + kfree(input); |
| 131 | +} |
| 132 | + |
| 133 | +u64 hv_get_vpreg(u32 msr) |
| 134 | +{ |
| 135 | + struct hv_get_vp_registers_output *output; |
| 136 | + u64 result; |
| 137 | + |
| 138 | + /* |
| 139 | + * Allocate a power of 2 size so alignment to that size is |
| 140 | + * guaranteed, since the hypercall output area must not cross |
| 141 | + * a page boundary. |
| 142 | + */ |
| 143 | + output = kmalloc(roundup_pow_of_two(sizeof(*output)), GFP_ATOMIC); |
| 144 | + |
| 145 | + __hv_get_vpreg_128(msr, output); |
| 146 | + |
| 147 | + result = output->as64.low; |
| 148 | + kfree(output); |
| 149 | + return result; |
| 150 | +} |
| 151 | +EXPORT_SYMBOL_GPL(hv_get_vpreg); |
| 152 | + |
| 153 | +void hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *res) |
| 154 | +{ |
| 155 | + struct hv_get_vp_registers_output *output; |
| 156 | + |
| 157 | + /* |
| 158 | + * Allocate a power of 2 size so alignment to that size is |
| 159 | + * guaranteed, since the hypercall output area must not cross |
| 160 | + * a page boundary. |
| 161 | + */ |
| 162 | + output = kmalloc(roundup_pow_of_two(sizeof(*output)), GFP_ATOMIC); |
| 163 | + |
| 164 | + __hv_get_vpreg_128(msr, output); |
| 165 | + |
| 166 | + res->as64.low = output->as64.low; |
| 167 | + res->as64.high = output->as64.high; |
| 168 | + kfree(output); |
| 169 | +} |
| 170 | +EXPORT_SYMBOL_GPL(hv_get_vpreg_128); |
0 commit comments