1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * KVM Microsoft Hyper-V emulation 4 * 5 * derived from arch/x86/kvm/x86.c 6 * 7 * Copyright (C) 2006 Qumranet, Inc. 8 * Copyright (C) 2008 Qumranet, Inc. 9 * Copyright IBM Corporation, 2008 10 * Copyright 2010 Red Hat, Inc. and/or its affiliates. 11 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com> 12 * 13 * Authors: 14 * Avi Kivity <avi@qumranet.com> 15 * Yaniv Kamay <yaniv@qumranet.com> 16 * Amit Shah <amit.shah@qumranet.com> 17 * Ben-Ami Yassour <benami@il.ibm.com> 18 * Andrey Smetanin <asmetanin@virtuozzo.com> 19 */ 20 21 #ifndef __ARCH_X86_KVM_HYPERV_H__ 22 #define __ARCH_X86_KVM_HYPERV_H__ 23 24 #include <linux/kvm_host.h> 25 26 #include "regs.h" 27 28 #ifdef CONFIG_KVM_HYPERV 29 30 /* "Hv#1" signature */ 31 #define HYPERV_CPUID_SIGNATURE_EAX 0x31237648 32 33 /* 34 * The #defines related to the synthetic debugger are required by KDNet, but 35 * they are not documented in the Hyper-V TLFS because the synthetic debugger 36 * functionality has been deprecated and is subject to removal in future 37 * versions of Windows. 38 */ 39 #define HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS 0x40000080 40 #define HYPERV_CPUID_SYNDBG_INTERFACE 0x40000081 41 #define HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES 0x40000082 42 43 /* 44 * Hyper-V synthetic debugger platform capabilities 45 * These are HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES.EAX bits. 46 */ 47 #define HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING BIT(1) 48 49 /* Hyper-V Synthetic debug options MSR */ 50 #define HV_X64_MSR_SYNDBG_CONTROL 0x400000F1 51 #define HV_X64_MSR_SYNDBG_STATUS 0x400000F2 52 #define HV_X64_MSR_SYNDBG_SEND_BUFFER 0x400000F3 53 #define HV_X64_MSR_SYNDBG_RECV_BUFFER 0x400000F4 54 #define HV_X64_MSR_SYNDBG_PENDING_BUFFER 0x400000F5 55 #define HV_X64_MSR_SYNDBG_OPTIONS 0x400000FF 56 57 /* Hyper-V HV_X64_MSR_SYNDBG_OPTIONS bits */ 58 #define HV_X64_SYNDBG_OPTION_USE_HCALLS BIT(2) 59 60 static inline struct kvm_hv *to_kvm_hv(struct kvm *kvm) 61 { 62 return &kvm->arch.hyperv; 63 } 64 65 static inline struct kvm_vcpu_hv *to_hv_vcpu(struct kvm_vcpu *vcpu) 66 { 67 return vcpu->arch.hyperv; 68 } 69 70 static inline struct kvm_vcpu_hv_synic *to_hv_synic(struct kvm_vcpu *vcpu) 71 { 72 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 73 74 return &hv_vcpu->synic; 75 } 76 77 static inline struct kvm_vcpu *hv_synic_to_vcpu(struct kvm_vcpu_hv_synic *synic) 78 { 79 struct kvm_vcpu_hv *hv_vcpu = container_of(synic, struct kvm_vcpu_hv, synic); 80 81 return hv_vcpu->vcpu; 82 } 83 84 static inline struct kvm_hv_syndbg *to_hv_syndbg(struct kvm_vcpu *vcpu) 85 { 86 return &vcpu->kvm->arch.hyperv.hv_syndbg; 87 } 88 89 static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu) 90 { 91 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 92 93 return hv_vcpu ? hv_vcpu->vp_index : vcpu->vcpu_idx; 94 } 95 96 int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host); 97 int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host); 98 99 static inline bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu) 100 { 101 return vcpu->arch.hyperv_enabled && to_kvm_hv(vcpu->kvm)->hv_guest_os_id; 102 } 103 104 int kvm_hv_hypercall(struct kvm_vcpu *vcpu); 105 106 void kvm_hv_irq_routing_update(struct kvm *kvm); 107 int kvm_hv_synic_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, 108 int irq_source_id, int level, bool line_status); 109 void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector); 110 int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages); 111 112 static inline bool kvm_hv_synic_has_vector(struct kvm_vcpu *vcpu, int vector) 113 { 114 return to_hv_vcpu(vcpu) && test_bit(vector, to_hv_synic(vcpu)->vec_bitmap); 115 } 116 117 static inline bool kvm_hv_synic_auto_eoi_set(struct kvm_vcpu *vcpu, int vector) 118 { 119 return to_hv_vcpu(vcpu) && 120 test_bit(vector, to_hv_synic(vcpu)->auto_eoi_bitmap); 121 } 122 123 void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu); 124 125 bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu); 126 int kvm_hv_get_assist_page(struct kvm_vcpu *vcpu); 127 128 static inline struct kvm_vcpu_hv_stimer *to_hv_stimer(struct kvm_vcpu *vcpu, 129 int timer_index) 130 { 131 return &to_hv_vcpu(vcpu)->stimer[timer_index]; 132 } 133 134 static inline struct kvm_vcpu *hv_stimer_to_vcpu(struct kvm_vcpu_hv_stimer *stimer) 135 { 136 struct kvm_vcpu_hv *hv_vcpu; 137 138 hv_vcpu = container_of(stimer - stimer->index, struct kvm_vcpu_hv, 139 stimer[0]); 140 return hv_vcpu->vcpu; 141 } 142 143 static inline bool kvm_hv_has_stimer_pending(struct kvm_vcpu *vcpu) 144 { 145 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 146 147 if (!hv_vcpu) 148 return false; 149 150 return !bitmap_empty(hv_vcpu->stimer_pending_bitmap, 151 HV_SYNIC_STIMER_COUNT); 152 } 153 154 /* 155 * With HV_ACCESS_TSC_INVARIANT feature, invariant TSC (CPUID.80000007H:EDX[8]) 156 * is only observed after HV_X64_MSR_TSC_INVARIANT_CONTROL was written to. 157 */ 158 static inline bool kvm_hv_invtsc_suppressed(struct kvm_vcpu *vcpu) 159 { 160 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 161 162 /* 163 * If Hyper-V's invariant TSC control is not exposed to the guest, 164 * the invariant TSC CPUID flag is not suppressed, Windows guests were 165 * observed to be able to handle it correctly. Going forward, VMMs are 166 * encouraged to enable Hyper-V's invariant TSC control when invariant 167 * TSC CPUID flag is set to make KVM's behavior match genuine Hyper-V. 168 */ 169 if (!hv_vcpu || 170 !(hv_vcpu->cpuid_cache.features_eax & HV_ACCESS_TSC_INVARIANT)) 171 return false; 172 173 /* 174 * If Hyper-V's invariant TSC control is exposed to the guest, KVM is 175 * responsible for suppressing the invariant TSC CPUID flag if the 176 * Hyper-V control is not enabled. 177 */ 178 return !(to_kvm_hv(vcpu->kvm)->hv_invtsc_control & HV_EXPOSE_INVARIANT_TSC); 179 } 180 181 void kvm_hv_process_stimers(struct kvm_vcpu *vcpu); 182 183 void kvm_hv_setup_tsc_page(struct kvm *kvm, 184 struct pvclock_vcpu_time_info *hv_clock); 185 void kvm_hv_request_tsc_page_update(struct kvm *kvm); 186 187 void kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu); 188 189 void kvm_hv_init_vm(struct kvm *kvm); 190 void kvm_hv_destroy_vm(struct kvm *kvm); 191 int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu); 192 void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu, bool hyperv_enabled); 193 int kvm_hv_set_enforce_cpuid(struct kvm_vcpu *vcpu, bool enforce); 194 int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args); 195 int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, 196 struct kvm_cpuid_entry2 __user *entries); 197 198 static inline struct kvm_vcpu_hv_tlb_flush_fifo *kvm_hv_get_tlb_flush_fifo(struct kvm_vcpu *vcpu, 199 bool is_guest_mode) 200 { 201 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 202 int i = is_guest_mode ? HV_L2_TLB_FLUSH_FIFO : 203 HV_L1_TLB_FLUSH_FIFO; 204 205 return &hv_vcpu->tlb_flush_fifo[i]; 206 } 207 208 static inline void kvm_hv_vcpu_purge_flush_tlb(struct kvm_vcpu *vcpu) 209 { 210 struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo; 211 212 if (!to_hv_vcpu(vcpu) || !kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu)) 213 return; 214 215 tlb_flush_fifo = kvm_hv_get_tlb_flush_fifo(vcpu, is_guest_mode(vcpu)); 216 217 kfifo_reset_out(&tlb_flush_fifo->entries); 218 } 219 220 static inline bool guest_hv_cpuid_has_l2_tlb_flush(struct kvm_vcpu *vcpu) 221 { 222 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 223 224 return hv_vcpu && 225 (hv_vcpu->cpuid_cache.nested_eax & HV_X64_NESTED_DIRECT_FLUSH); 226 } 227 228 static inline bool kvm_hv_is_tlb_flush_hcall(struct kvm_vcpu *vcpu) 229 { 230 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 231 u16 code; 232 233 if (!hv_vcpu) 234 return false; 235 236 code = is_64_bit_hypercall(vcpu) ? kvm_rcx_read_raw(vcpu) : 237 kvm_eax_read(vcpu); 238 239 return (code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE || 240 code == HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST || 241 code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX || 242 code == HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX); 243 } 244 245 static inline int kvm_hv_verify_vp_assist(struct kvm_vcpu *vcpu) 246 { 247 if (!to_hv_vcpu(vcpu)) 248 return 0; 249 250 if (!kvm_hv_assist_page_enabled(vcpu)) 251 return 0; 252 253 return kvm_hv_get_assist_page(vcpu); 254 } 255 256 static inline void kvm_hv_nested_transtion_tlb_flush(struct kvm_vcpu *vcpu, 257 bool tdp_enabled) 258 { 259 /* 260 * KVM_REQ_HV_TLB_FLUSH flushes entries from either L1's VP_ID or 261 * L2's VP_ID upon request from the guest. Make sure we check for 262 * pending entries in the right FIFO upon L1/L2 transition as these 263 * requests are put by other vCPUs asynchronously. 264 */ 265 if (to_hv_vcpu(vcpu) && tdp_enabled) 266 kvm_make_request(KVM_REQ_HV_TLB_FLUSH, vcpu); 267 } 268 269 int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu); 270 #else /* CONFIG_KVM_HYPERV */ 271 static inline void kvm_hv_setup_tsc_page(struct kvm *kvm, 272 struct pvclock_vcpu_time_info *hv_clock) {} 273 static inline void kvm_hv_request_tsc_page_update(struct kvm *kvm) {} 274 static inline void kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu) {} 275 static inline void kvm_hv_init_vm(struct kvm *kvm) {} 276 static inline void kvm_hv_destroy_vm(struct kvm *kvm) {} 277 static inline int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) 278 { 279 return 0; 280 } 281 static inline void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) {} 282 static inline bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu) 283 { 284 return false; 285 } 286 static inline int kvm_hv_hypercall(struct kvm_vcpu *vcpu) 287 { 288 return HV_STATUS_ACCESS_DENIED; 289 } 290 static inline void kvm_hv_vcpu_purge_flush_tlb(struct kvm_vcpu *vcpu) {} 291 static inline bool kvm_hv_synic_has_vector(struct kvm_vcpu *vcpu, int vector) 292 { 293 return false; 294 } 295 static inline bool kvm_hv_synic_auto_eoi_set(struct kvm_vcpu *vcpu, int vector) 296 { 297 return false; 298 } 299 static inline void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) {} 300 static inline bool kvm_hv_invtsc_suppressed(struct kvm_vcpu *vcpu) 301 { 302 return false; 303 } 304 static inline void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu, bool hyperv_enabled) {} 305 static inline bool kvm_hv_has_stimer_pending(struct kvm_vcpu *vcpu) 306 { 307 return false; 308 } 309 static inline int kvm_hv_verify_vp_assist(struct kvm_vcpu *vcpu) 310 { 311 return 0; 312 } 313 static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu) 314 { 315 return vcpu->vcpu_idx; 316 } 317 static inline void kvm_hv_nested_transtion_tlb_flush(struct kvm_vcpu *vcpu, bool tdp_enabled) {} 318 #endif /* CONFIG_KVM_HYPERV */ 319 320 #endif /* __ARCH_X86_KVM_HYPERV_H__ */ 321