1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 /* 4 * Linux-specific definitions for managing interactions with Microsoft's 5 * Hyper-V hypervisor. The definitions in this file are architecture 6 * independent. See arch/<arch>/include/asm/mshyperv.h for definitions 7 * that are specific to architecture <arch>. 8 * 9 * Definitions that are specified in the Hyper-V Top Level Functional 10 * Spec (TLFS) should not go in this file, but should instead go in 11 * hyperv-tlfs.h. 12 * 13 * Copyright (C) 2019, Microsoft, Inc. 14 * 15 * Author : Michael Kelley <mikelley@microsoft.com> 16 */ 17 18 #ifndef _ASM_GENERIC_MSHYPERV_H 19 #define _ASM_GENERIC_MSHYPERV_H 20 21 #include <linux/types.h> 22 #include <linux/atomic.h> 23 #include <linux/bitops.h> 24 #include <linux/cpumask.h> 25 #include <linux/nmi.h> 26 #include <asm/ptrace.h> 27 #include <asm/hyperv-tlfs.h> 28 29 #define VTPM_BASE_ADDRESS 0xfed40000 30 31 struct ms_hyperv_info { 32 u32 features; 33 u32 priv_high; 34 u32 misc_features; 35 u32 hints; 36 u32 nested_features; 37 u32 max_vp_index; 38 u32 max_lp_index; 39 union { 40 u32 isolation_config_a; 41 struct { 42 u32 paravisor_present : 1; 43 u32 reserved_a1 : 31; 44 }; 45 }; 46 union { 47 u32 isolation_config_b; 48 struct { 49 u32 cvm_type : 4; 50 u32 reserved_b1 : 1; 51 u32 shared_gpa_boundary_active : 1; 52 u32 shared_gpa_boundary_bits : 6; 53 u32 reserved_b2 : 20; 54 }; 55 }; 56 u64 shared_gpa_boundary; 57 u8 vtl; 58 }; 59 extern struct ms_hyperv_info ms_hyperv; 60 extern bool hv_nested; 61 62 extern void * __percpu *hyperv_pcpu_input_arg; 63 extern void * __percpu *hyperv_pcpu_output_arg; 64 65 extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr); 66 extern u64 hv_do_fast_hypercall8(u16 control, u64 input8); 67 extern bool hv_isolation_type_snp(void); 68 extern bool hv_isolation_type_en_snp(void); 69 bool hv_isolation_type_tdx(void); 70 71 /* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */ 72 static inline int hv_result(u64 status) 73 { 74 return status & HV_HYPERCALL_RESULT_MASK; 75 } 76 77 static inline bool hv_result_success(u64 status) 78 { 79 return hv_result(status) == HV_STATUS_SUCCESS; 80 } 81 82 static inline unsigned int hv_repcomp(u64 status) 83 { 84 /* Bits [43:32] of status have 'Reps completed' data. */ 85 return (status & HV_HYPERCALL_REP_COMP_MASK) >> 86 HV_HYPERCALL_REP_COMP_OFFSET; 87 } 88 89 /* 90 * Rep hypercalls. Callers of this functions are supposed to ensure that 91 * rep_count and varhead_size comply with Hyper-V hypercall definition. 92 */ 93 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size, 94 void *input, void *output) 95 { 96 u64 control = code; 97 u64 status; 98 u16 rep_comp; 99 100 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; 101 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; 102 103 do { 104 status = hv_do_hypercall(control, input, output); 105 if (!hv_result_success(status)) 106 return status; 107 108 rep_comp = hv_repcomp(status); 109 110 control &= ~HV_HYPERCALL_REP_START_MASK; 111 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; 112 113 touch_nmi_watchdog(); 114 } while (rep_comp < rep_count); 115 116 return status; 117 } 118 119 /* Generate the guest OS identifier as described in the Hyper-V TLFS */ 120 static inline u64 hv_generate_guest_id(u64 kernel_version) 121 { 122 u64 guest_id; 123 124 guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48); 125 guest_id |= (kernel_version << 16); 126 127 return guest_id; 128 } 129 130 /* Free the message slot and signal end-of-message if required */ 131 static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type) 132 { 133 /* 134 * On crash we're reading some other CPU's message page and we need 135 * to be careful: this other CPU may already had cleared the header 136 * and the host may already had delivered some other message there. 137 * In case we blindly write msg->header.message_type we're going 138 * to lose it. We can still lose a message of the same type but 139 * we count on the fact that there can only be one 140 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages 141 * on crash. 142 */ 143 if (cmpxchg(&msg->header.message_type, old_msg_type, 144 HVMSG_NONE) != old_msg_type) 145 return; 146 147 /* 148 * The cmxchg() above does an implicit memory barrier to 149 * ensure the write to MessageType (ie set to 150 * HVMSG_NONE) happens before we read the 151 * MessagePending and EOMing. Otherwise, the EOMing 152 * will not deliver any more messages since there is 153 * no empty slot 154 */ 155 if (msg->header.message_flags.msg_pending) { 156 /* 157 * This will cause message queue rescan to 158 * possibly deliver another msg from the 159 * hypervisor 160 */ 161 hv_set_register(HV_REGISTER_EOM, 0); 162 } 163 } 164 165 void hv_setup_vmbus_handler(void (*handler)(void)); 166 void hv_remove_vmbus_handler(void); 167 void hv_setup_stimer0_handler(void (*handler)(void)); 168 void hv_remove_stimer0_handler(void); 169 170 void hv_setup_kexec_handler(void (*handler)(void)); 171 void hv_remove_kexec_handler(void); 172 void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs)); 173 void hv_remove_crash_handler(void); 174 175 extern int vmbus_interrupt; 176 extern int vmbus_irq; 177 178 extern bool hv_root_partition; 179 180 #if IS_ENABLED(CONFIG_HYPERV) 181 /* 182 * Hypervisor's notion of virtual processor ID is different from 183 * Linux' notion of CPU ID. This information can only be retrieved 184 * in the context of the calling CPU. Setup a map for easy access 185 * to this information. 186 */ 187 extern u32 *hv_vp_index; 188 extern u32 hv_max_vp_index; 189 190 extern u64 (*hv_read_reference_counter)(void); 191 192 /* Sentinel value for an uninitialized entry in hv_vp_index array */ 193 #define VP_INVAL U32_MAX 194 195 int __init hv_common_init(void); 196 void __init hv_common_free(void); 197 int hv_common_cpu_init(unsigned int cpu); 198 int hv_common_cpu_die(unsigned int cpu); 199 200 void *hv_alloc_hyperv_page(void); 201 void *hv_alloc_hyperv_zeroed_page(void); 202 void hv_free_hyperv_page(unsigned long addr); 203 204 /** 205 * hv_cpu_number_to_vp_number() - Map CPU to VP. 206 * @cpu_number: CPU number in Linux terms 207 * 208 * This function returns the mapping between the Linux processor 209 * number and the hypervisor's virtual processor number, useful 210 * in making hypercalls and such that talk about specific 211 * processors. 212 * 213 * Return: Virtual processor number in Hyper-V terms 214 */ 215 static inline int hv_cpu_number_to_vp_number(int cpu_number) 216 { 217 return hv_vp_index[cpu_number]; 218 } 219 220 static inline int __cpumask_to_vpset(struct hv_vpset *vpset, 221 const struct cpumask *cpus, 222 bool (*func)(int cpu)) 223 { 224 int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1; 225 int max_vcpu_bank = hv_max_vp_index / HV_VCPUS_PER_SPARSE_BANK; 226 227 /* vpset.valid_bank_mask can represent up to HV_MAX_SPARSE_VCPU_BANKS banks */ 228 if (max_vcpu_bank >= HV_MAX_SPARSE_VCPU_BANKS) 229 return 0; 230 231 /* 232 * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex 233 * structs are not cleared between calls, we risk flushing unneeded 234 * vCPUs otherwise. 235 */ 236 for (vcpu_bank = 0; vcpu_bank <= max_vcpu_bank; vcpu_bank++) 237 vpset->bank_contents[vcpu_bank] = 0; 238 239 /* 240 * Some banks may end up being empty but this is acceptable. 241 */ 242 for_each_cpu(cpu, cpus) { 243 if (func && func(cpu)) 244 continue; 245 vcpu = hv_cpu_number_to_vp_number(cpu); 246 if (vcpu == VP_INVAL) 247 return -1; 248 vcpu_bank = vcpu / HV_VCPUS_PER_SPARSE_BANK; 249 vcpu_offset = vcpu % HV_VCPUS_PER_SPARSE_BANK; 250 __set_bit(vcpu_offset, (unsigned long *) 251 &vpset->bank_contents[vcpu_bank]); 252 if (vcpu_bank >= nr_bank) 253 nr_bank = vcpu_bank + 1; 254 } 255 vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0); 256 return nr_bank; 257 } 258 259 /* 260 * Convert a Linux cpumask into a Hyper-V VPset. In the _skip variant, 261 * 'func' is called for each CPU present in cpumask. If 'func' returns 262 * true, that CPU is skipped -- i.e., that CPU from cpumask is *not* 263 * added to the Hyper-V VPset. If 'func' is NULL, no CPUs are 264 * skipped. 265 */ 266 static inline int cpumask_to_vpset(struct hv_vpset *vpset, 267 const struct cpumask *cpus) 268 { 269 return __cpumask_to_vpset(vpset, cpus, NULL); 270 } 271 272 static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset, 273 const struct cpumask *cpus, 274 bool (*func)(int cpu)) 275 { 276 return __cpumask_to_vpset(vpset, cpus, func); 277 } 278 279 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die); 280 bool hv_is_hyperv_initialized(void); 281 bool hv_is_hibernation_supported(void); 282 enum hv_isolation_type hv_get_isolation_type(void); 283 bool hv_is_isolation_supported(void); 284 bool hv_isolation_type_snp(void); 285 u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size); 286 void hyperv_cleanup(void); 287 bool hv_query_ext_cap(u64 cap_query); 288 void hv_setup_dma_ops(struct device *dev, bool coherent); 289 #else /* CONFIG_HYPERV */ 290 static inline bool hv_is_hyperv_initialized(void) { return false; } 291 static inline bool hv_is_hibernation_supported(void) { return false; } 292 static inline void hyperv_cleanup(void) {} 293 static inline bool hv_is_isolation_supported(void) { return false; } 294 static inline enum hv_isolation_type hv_get_isolation_type(void) 295 { 296 return HV_ISOLATION_TYPE_NONE; 297 } 298 #endif /* CONFIG_HYPERV */ 299 300 #endif 301