1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Kernel-based Virtual Machine driver for Linux 4 * 5 * This module enables machines with Intel VT-x extensions to run virtual 6 * machines without emulation or binary translation. 7 * 8 * Copyright (C) 2006 Qumranet, Inc. 9 * Copyright 2010 Red Hat, Inc. and/or its affiliates. 10 * 11 * Authors: 12 * Avi Kivity <avi@qumranet.com> 13 * Yaniv Kamay <yaniv@qumranet.com> 14 */ 15 16 #include <linux/highmem.h> 17 #include <linux/hrtimer.h> 18 #include <linux/kernel.h> 19 #include <linux/kvm_host.h> 20 #include <linux/module.h> 21 #include <linux/moduleparam.h> 22 #include <linux/mod_devicetable.h> 23 #include <linux/mm.h> 24 #include <linux/objtool.h> 25 #include <linux/sched.h> 26 #include <linux/sched/smt.h> 27 #include <linux/slab.h> 28 #include <linux/tboot.h> 29 #include <linux/trace_events.h> 30 #include <linux/entry-kvm.h> 31 32 #include <asm/apic.h> 33 #include <asm/asm.h> 34 #include <asm/cpu.h> 35 #include <asm/cpu_device_id.h> 36 #include <asm/debugreg.h> 37 #include <asm/desc.h> 38 #include <asm/fpu/api.h> 39 #include <asm/fpu/xstate.h> 40 #include <asm/idtentry.h> 41 #include <asm/io.h> 42 #include <asm/irq_remapping.h> 43 #include <asm/kexec.h> 44 #include <asm/perf_event.h> 45 #include <asm/mmu_context.h> 46 #include <asm/mshyperv.h> 47 #include <asm/mwait.h> 48 #include <asm/spec-ctrl.h> 49 #include <asm/virtext.h> 50 #include <asm/vmx.h> 51 52 #include "capabilities.h" 53 #include "cpuid.h" 54 #include "hyperv.h" 55 #include "kvm_onhyperv.h" 56 #include "irq.h" 57 #include "kvm_cache_regs.h" 58 #include "lapic.h" 59 #include "mmu.h" 60 #include "nested.h" 61 #include "pmu.h" 62 #include "sgx.h" 63 #include "trace.h" 64 #include "vmcs.h" 65 #include "vmcs12.h" 66 #include "vmx.h" 67 #include "x86.h" 68 #include "smm.h" 69 70 MODULE_AUTHOR("Qumranet"); 71 MODULE_LICENSE("GPL"); 72 73 #ifdef MODULE 74 static const struct x86_cpu_id vmx_cpu_id[] = { 75 X86_MATCH_FEATURE(X86_FEATURE_VMX, NULL), 76 {} 77 }; 78 MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); 79 #endif 80 81 bool __read_mostly enable_vpid = 1; 82 module_param_named(vpid, enable_vpid, bool, 0444); 83 84 static bool __read_mostly enable_vnmi = 1; 85 module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); 86 87 bool __read_mostly flexpriority_enabled = 1; 88 module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); 89 90 bool __read_mostly enable_ept = 1; 91 module_param_named(ept, enable_ept, bool, S_IRUGO); 92 93 bool __read_mostly enable_unrestricted_guest = 1; 94 module_param_named(unrestricted_guest, 95 enable_unrestricted_guest, bool, S_IRUGO); 96 97 bool __read_mostly enable_ept_ad_bits = 1; 98 module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); 99 100 static bool __read_mostly emulate_invalid_guest_state = true; 101 module_param(emulate_invalid_guest_state, bool, S_IRUGO); 102 103 static bool __read_mostly fasteoi = 1; 104 module_param(fasteoi, bool, S_IRUGO); 105 106 module_param(enable_apicv, bool, S_IRUGO); 107 108 bool __read_mostly enable_ipiv = true; 109 module_param(enable_ipiv, bool, 0444); 110 111 /* 112 * If nested=1, nested virtualization is supported, i.e., guests may use 113 * VMX and be a hypervisor for its own guests. If nested=0, guests may not 114 * use VMX instructions. 115 */ 116 static bool __read_mostly nested = 1; 117 module_param(nested, bool, S_IRUGO); 118 119 bool __read_mostly enable_pml = 1; 120 module_param_named(pml, enable_pml, bool, S_IRUGO); 121 122 static bool __read_mostly error_on_inconsistent_vmcs_config = true; 123 module_param(error_on_inconsistent_vmcs_config, bool, 0444); 124 125 static bool __read_mostly dump_invalid_vmcs = 0; 126 module_param(dump_invalid_vmcs, bool, 0644); 127 128 #define MSR_BITMAP_MODE_X2APIC 1 129 #define MSR_BITMAP_MODE_X2APIC_APICV 2 130 131 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL 132 133 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ 134 static int __read_mostly cpu_preemption_timer_multi; 135 static bool __read_mostly enable_preemption_timer = 1; 136 #ifdef CONFIG_X86_64 137 module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); 138 #endif 139 140 extern bool __read_mostly allow_smaller_maxphyaddr; 141 module_param(allow_smaller_maxphyaddr, bool, S_IRUGO); 142 143 #define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD) 144 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE 145 #define KVM_VM_CR0_ALWAYS_ON \ 146 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) 147 148 #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE 149 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) 150 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) 151 152 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) 153 154 #define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \ 155 RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \ 156 RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \ 157 RTIT_STATUS_BYTECNT)) 158 159 /* 160 * List of MSRs that can be directly passed to the guest. 161 * In addition to these x2apic and PT MSRs are handled specially. 162 */ 163 static u32 vmx_possible_passthrough_msrs[MAX_POSSIBLE_PASSTHROUGH_MSRS] = { 164 MSR_IA32_SPEC_CTRL, 165 MSR_IA32_PRED_CMD, 166 MSR_IA32_TSC, 167 #ifdef CONFIG_X86_64 168 MSR_FS_BASE, 169 MSR_GS_BASE, 170 MSR_KERNEL_GS_BASE, 171 MSR_IA32_XFD, 172 MSR_IA32_XFD_ERR, 173 #endif 174 MSR_IA32_SYSENTER_CS, 175 MSR_IA32_SYSENTER_ESP, 176 MSR_IA32_SYSENTER_EIP, 177 MSR_CORE_C1_RES, 178 MSR_CORE_C3_RESIDENCY, 179 MSR_CORE_C6_RESIDENCY, 180 MSR_CORE_C7_RESIDENCY, 181 }; 182 183 /* 184 * These 2 parameters are used to config the controls for Pause-Loop Exiting: 185 * ple_gap: upper bound on the amount of time between two successive 186 * executions of PAUSE in a loop. Also indicate if ple enabled. 187 * According to test, this time is usually smaller than 128 cycles. 188 * ple_window: upper bound on the amount of time a guest is allowed to execute 189 * in a PAUSE loop. Tests indicate that most spinlocks are held for 190 * less than 2^12 cycles 191 * Time is measured based on a counter that runs at the same rate as the TSC, 192 * refer SDM volume 3b section 21.6.13 & 22.1.3. 193 */ 194 static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP; 195 module_param(ple_gap, uint, 0444); 196 197 static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; 198 module_param(ple_window, uint, 0444); 199 200 /* Default doubles per-vcpu window every exit. */ 201 static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW; 202 module_param(ple_window_grow, uint, 0444); 203 204 /* Default resets per-vcpu window every exit to ple_window. */ 205 static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; 206 module_param(ple_window_shrink, uint, 0444); 207 208 /* Default is to compute the maximum so we can never overflow. */ 209 static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; 210 module_param(ple_window_max, uint, 0444); 211 212 /* Default is SYSTEM mode, 1 for host-guest mode */ 213 int __read_mostly pt_mode = PT_MODE_SYSTEM; 214 module_param(pt_mode, int, S_IRUGO); 215 216 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush); 217 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond); 218 static DEFINE_MUTEX(vmx_l1d_flush_mutex); 219 220 /* Storage for pre module init parameter parsing */ 221 static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO; 222 223 static const struct { 224 const char *option; 225 bool for_parse; 226 } vmentry_l1d_param[] = { 227 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true}, 228 [VMENTER_L1D_FLUSH_NEVER] = {"never", true}, 229 [VMENTER_L1D_FLUSH_COND] = {"cond", true}, 230 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true}, 231 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false}, 232 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false}, 233 }; 234 235 #define L1D_CACHE_ORDER 4 236 static void *vmx_l1d_flush_pages; 237 238 /* Control for disabling CPU Fill buffer clear */ 239 static bool __read_mostly vmx_fb_clear_ctrl_available; 240 241 static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf) 242 { 243 struct page *page; 244 unsigned int i; 245 246 if (!boot_cpu_has_bug(X86_BUG_L1TF)) { 247 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED; 248 return 0; 249 } 250 251 if (!enable_ept) { 252 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED; 253 return 0; 254 } 255 256 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) { 257 u64 msr; 258 259 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr); 260 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) { 261 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED; 262 return 0; 263 } 264 } 265 266 /* If set to auto use the default l1tf mitigation method */ 267 if (l1tf == VMENTER_L1D_FLUSH_AUTO) { 268 switch (l1tf_mitigation) { 269 case L1TF_MITIGATION_OFF: 270 l1tf = VMENTER_L1D_FLUSH_NEVER; 271 break; 272 case L1TF_MITIGATION_FLUSH_NOWARN: 273 case L1TF_MITIGATION_FLUSH: 274 case L1TF_MITIGATION_FLUSH_NOSMT: 275 l1tf = VMENTER_L1D_FLUSH_COND; 276 break; 277 case L1TF_MITIGATION_FULL: 278 case L1TF_MITIGATION_FULL_FORCE: 279 l1tf = VMENTER_L1D_FLUSH_ALWAYS; 280 break; 281 } 282 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) { 283 l1tf = VMENTER_L1D_FLUSH_ALWAYS; 284 } 285 286 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages && 287 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) { 288 /* 289 * This allocation for vmx_l1d_flush_pages is not tied to a VM 290 * lifetime and so should not be charged to a memcg. 291 */ 292 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER); 293 if (!page) 294 return -ENOMEM; 295 vmx_l1d_flush_pages = page_address(page); 296 297 /* 298 * Initialize each page with a different pattern in 299 * order to protect against KSM in the nested 300 * virtualization case. 301 */ 302 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) { 303 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1, 304 PAGE_SIZE); 305 } 306 } 307 308 l1tf_vmx_mitigation = l1tf; 309 310 if (l1tf != VMENTER_L1D_FLUSH_NEVER) 311 static_branch_enable(&vmx_l1d_should_flush); 312 else 313 static_branch_disable(&vmx_l1d_should_flush); 314 315 if (l1tf == VMENTER_L1D_FLUSH_COND) 316 static_branch_enable(&vmx_l1d_flush_cond); 317 else 318 static_branch_disable(&vmx_l1d_flush_cond); 319 return 0; 320 } 321 322 static int vmentry_l1d_flush_parse(const char *s) 323 { 324 unsigned int i; 325 326 if (s) { 327 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) { 328 if (vmentry_l1d_param[i].for_parse && 329 sysfs_streq(s, vmentry_l1d_param[i].option)) 330 return i; 331 } 332 } 333 return -EINVAL; 334 } 335 336 static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp) 337 { 338 int l1tf, ret; 339 340 l1tf = vmentry_l1d_flush_parse(s); 341 if (l1tf < 0) 342 return l1tf; 343 344 if (!boot_cpu_has(X86_BUG_L1TF)) 345 return 0; 346 347 /* 348 * Has vmx_init() run already? If not then this is the pre init 349 * parameter parsing. In that case just store the value and let 350 * vmx_init() do the proper setup after enable_ept has been 351 * established. 352 */ 353 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) { 354 vmentry_l1d_flush_param = l1tf; 355 return 0; 356 } 357 358 mutex_lock(&vmx_l1d_flush_mutex); 359 ret = vmx_setup_l1d_flush(l1tf); 360 mutex_unlock(&vmx_l1d_flush_mutex); 361 return ret; 362 } 363 364 static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp) 365 { 366 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param))) 367 return sprintf(s, "???\n"); 368 369 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option); 370 } 371 372 static void vmx_setup_fb_clear_ctrl(void) 373 { 374 u64 msr; 375 376 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES) && 377 !boot_cpu_has_bug(X86_BUG_MDS) && 378 !boot_cpu_has_bug(X86_BUG_TAA)) { 379 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr); 380 if (msr & ARCH_CAP_FB_CLEAR_CTRL) 381 vmx_fb_clear_ctrl_available = true; 382 } 383 } 384 385 static __always_inline void vmx_disable_fb_clear(struct vcpu_vmx *vmx) 386 { 387 u64 msr; 388 389 if (!vmx->disable_fb_clear) 390 return; 391 392 msr = __rdmsr(MSR_IA32_MCU_OPT_CTRL); 393 msr |= FB_CLEAR_DIS; 394 native_wrmsrl(MSR_IA32_MCU_OPT_CTRL, msr); 395 /* Cache the MSR value to avoid reading it later */ 396 vmx->msr_ia32_mcu_opt_ctrl = msr; 397 } 398 399 static __always_inline void vmx_enable_fb_clear(struct vcpu_vmx *vmx) 400 { 401 if (!vmx->disable_fb_clear) 402 return; 403 404 vmx->msr_ia32_mcu_opt_ctrl &= ~FB_CLEAR_DIS; 405 native_wrmsrl(MSR_IA32_MCU_OPT_CTRL, vmx->msr_ia32_mcu_opt_ctrl); 406 } 407 408 static void vmx_update_fb_clear_dis(struct kvm_vcpu *vcpu, struct vcpu_vmx *vmx) 409 { 410 vmx->disable_fb_clear = vmx_fb_clear_ctrl_available; 411 412 /* 413 * If guest will not execute VERW, there is no need to set FB_CLEAR_DIS 414 * at VMEntry. Skip the MSR read/write when a guest has no use case to 415 * execute VERW. 416 */ 417 if ((vcpu->arch.arch_capabilities & ARCH_CAP_FB_CLEAR) || 418 ((vcpu->arch.arch_capabilities & ARCH_CAP_MDS_NO) && 419 (vcpu->arch.arch_capabilities & ARCH_CAP_TAA_NO) && 420 (vcpu->arch.arch_capabilities & ARCH_CAP_PSDP_NO) && 421 (vcpu->arch.arch_capabilities & ARCH_CAP_FBSDP_NO) && 422 (vcpu->arch.arch_capabilities & ARCH_CAP_SBDR_SSDP_NO))) 423 vmx->disable_fb_clear = false; 424 } 425 426 static const struct kernel_param_ops vmentry_l1d_flush_ops = { 427 .set = vmentry_l1d_flush_set, 428 .get = vmentry_l1d_flush_get, 429 }; 430 module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644); 431 432 static u32 vmx_segment_access_rights(struct kvm_segment *var); 433 434 void vmx_vmexit(void); 435 436 #define vmx_insn_failed(fmt...) \ 437 do { \ 438 WARN_ONCE(1, fmt); \ 439 pr_warn_ratelimited(fmt); \ 440 } while (0) 441 442 void vmread_error(unsigned long field, bool fault) 443 { 444 if (fault) 445 kvm_spurious_fault(); 446 else 447 vmx_insn_failed("kvm: vmread failed: field=%lx\n", field); 448 } 449 450 noinline void vmwrite_error(unsigned long field, unsigned long value) 451 { 452 vmx_insn_failed("kvm: vmwrite failed: field=%lx val=%lx err=%u\n", 453 field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); 454 } 455 456 noinline void vmclear_error(struct vmcs *vmcs, u64 phys_addr) 457 { 458 vmx_insn_failed("kvm: vmclear failed: %p/%llx err=%u\n", 459 vmcs, phys_addr, vmcs_read32(VM_INSTRUCTION_ERROR)); 460 } 461 462 noinline void vmptrld_error(struct vmcs *vmcs, u64 phys_addr) 463 { 464 vmx_insn_failed("kvm: vmptrld failed: %p/%llx err=%u\n", 465 vmcs, phys_addr, vmcs_read32(VM_INSTRUCTION_ERROR)); 466 } 467 468 noinline void invvpid_error(unsigned long ext, u16 vpid, gva_t gva) 469 { 470 vmx_insn_failed("kvm: invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n", 471 ext, vpid, gva); 472 } 473 474 noinline void invept_error(unsigned long ext, u64 eptp, gpa_t gpa) 475 { 476 vmx_insn_failed("kvm: invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n", 477 ext, eptp, gpa); 478 } 479 480 static DEFINE_PER_CPU(struct vmcs *, vmxarea); 481 DEFINE_PER_CPU(struct vmcs *, current_vmcs); 482 /* 483 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed 484 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. 485 */ 486 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); 487 488 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); 489 static DEFINE_SPINLOCK(vmx_vpid_lock); 490 491 struct vmcs_config vmcs_config; 492 struct vmx_capability vmx_capability; 493 494 #define VMX_SEGMENT_FIELD(seg) \ 495 [VCPU_SREG_##seg] = { \ 496 .selector = GUEST_##seg##_SELECTOR, \ 497 .base = GUEST_##seg##_BASE, \ 498 .limit = GUEST_##seg##_LIMIT, \ 499 .ar_bytes = GUEST_##seg##_AR_BYTES, \ 500 } 501 502 static const struct kvm_vmx_segment_field { 503 unsigned selector; 504 unsigned base; 505 unsigned limit; 506 unsigned ar_bytes; 507 } kvm_vmx_segment_fields[] = { 508 VMX_SEGMENT_FIELD(CS), 509 VMX_SEGMENT_FIELD(DS), 510 VMX_SEGMENT_FIELD(ES), 511 VMX_SEGMENT_FIELD(FS), 512 VMX_SEGMENT_FIELD(GS), 513 VMX_SEGMENT_FIELD(SS), 514 VMX_SEGMENT_FIELD(TR), 515 VMX_SEGMENT_FIELD(LDTR), 516 }; 517 518 static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx) 519 { 520 vmx->segment_cache.bitmask = 0; 521 } 522 523 static unsigned long host_idt_base; 524 525 #if IS_ENABLED(CONFIG_HYPERV) 526 static bool __read_mostly enlightened_vmcs = true; 527 module_param(enlightened_vmcs, bool, 0444); 528 529 static int hv_enable_l2_tlb_flush(struct kvm_vcpu *vcpu) 530 { 531 struct hv_enlightened_vmcs *evmcs; 532 struct hv_partition_assist_pg **p_hv_pa_pg = 533 &to_kvm_hv(vcpu->kvm)->hv_pa_pg; 534 /* 535 * Synthetic VM-Exit is not enabled in current code and so All 536 * evmcs in singe VM shares same assist page. 537 */ 538 if (!*p_hv_pa_pg) 539 *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL_ACCOUNT); 540 541 if (!*p_hv_pa_pg) 542 return -ENOMEM; 543 544 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs; 545 546 evmcs->partition_assist_page = 547 __pa(*p_hv_pa_pg); 548 evmcs->hv_vm_id = (unsigned long)vcpu->kvm; 549 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1; 550 551 return 0; 552 } 553 554 #endif /* IS_ENABLED(CONFIG_HYPERV) */ 555 556 /* 557 * Comment's format: document - errata name - stepping - processor name. 558 * Refer from 559 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp 560 */ 561 static u32 vmx_preemption_cpu_tfms[] = { 562 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ 563 0x000206E6, 564 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ 565 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ 566 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ 567 0x00020652, 568 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ 569 0x00020655, 570 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ 571 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ 572 /* 573 * 320767.pdf - AAP86 - B1 - 574 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile 575 */ 576 0x000106E5, 577 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ 578 0x000106A0, 579 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ 580 0x000106A1, 581 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ 582 0x000106A4, 583 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ 584 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ 585 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ 586 0x000106A5, 587 /* Xeon E3-1220 V2 */ 588 0x000306A8, 589 }; 590 591 static inline bool cpu_has_broken_vmx_preemption_timer(void) 592 { 593 u32 eax = cpuid_eax(0x00000001), i; 594 595 /* Clear the reserved bits */ 596 eax &= ~(0x3U << 14 | 0xfU << 28); 597 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) 598 if (eax == vmx_preemption_cpu_tfms[i]) 599 return true; 600 601 return false; 602 } 603 604 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) 605 { 606 return flexpriority_enabled && lapic_in_kernel(vcpu); 607 } 608 609 static int possible_passthrough_msr_slot(u32 msr) 610 { 611 u32 i; 612 613 for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++) 614 if (vmx_possible_passthrough_msrs[i] == msr) 615 return i; 616 617 return -ENOENT; 618 } 619 620 static bool is_valid_passthrough_msr(u32 msr) 621 { 622 bool r; 623 624 switch (msr) { 625 case 0x800 ... 0x8ff: 626 /* x2APIC MSRs. These are handled in vmx_update_msr_bitmap_x2apic() */ 627 return true; 628 case MSR_IA32_RTIT_STATUS: 629 case MSR_IA32_RTIT_OUTPUT_BASE: 630 case MSR_IA32_RTIT_OUTPUT_MASK: 631 case MSR_IA32_RTIT_CR3_MATCH: 632 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: 633 /* PT MSRs. These are handled in pt_update_intercept_for_msr() */ 634 case MSR_LBR_SELECT: 635 case MSR_LBR_TOS: 636 case MSR_LBR_INFO_0 ... MSR_LBR_INFO_0 + 31: 637 case MSR_LBR_NHM_FROM ... MSR_LBR_NHM_FROM + 31: 638 case MSR_LBR_NHM_TO ... MSR_LBR_NHM_TO + 31: 639 case MSR_LBR_CORE_FROM ... MSR_LBR_CORE_FROM + 8: 640 case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8: 641 /* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */ 642 return true; 643 } 644 645 r = possible_passthrough_msr_slot(msr) != -ENOENT; 646 647 WARN(!r, "Invalid MSR %x, please adapt vmx_possible_passthrough_msrs[]", msr); 648 649 return r; 650 } 651 652 struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr) 653 { 654 int i; 655 656 i = kvm_find_user_return_msr(msr); 657 if (i >= 0) 658 return &vmx->guest_uret_msrs[i]; 659 return NULL; 660 } 661 662 static int vmx_set_guest_uret_msr(struct vcpu_vmx *vmx, 663 struct vmx_uret_msr *msr, u64 data) 664 { 665 unsigned int slot = msr - vmx->guest_uret_msrs; 666 int ret = 0; 667 668 if (msr->load_into_hardware) { 669 preempt_disable(); 670 ret = kvm_set_user_return_msr(slot, data, msr->mask); 671 preempt_enable(); 672 } 673 if (!ret) 674 msr->data = data; 675 return ret; 676 } 677 678 #ifdef CONFIG_KEXEC_CORE 679 static void crash_vmclear_local_loaded_vmcss(void) 680 { 681 int cpu = raw_smp_processor_id(); 682 struct loaded_vmcs *v; 683 684 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), 685 loaded_vmcss_on_cpu_link) 686 vmcs_clear(v->vmcs); 687 } 688 #endif /* CONFIG_KEXEC_CORE */ 689 690 static void __loaded_vmcs_clear(void *arg) 691 { 692 struct loaded_vmcs *loaded_vmcs = arg; 693 int cpu = raw_smp_processor_id(); 694 695 if (loaded_vmcs->cpu != cpu) 696 return; /* vcpu migration can race with cpu offline */ 697 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) 698 per_cpu(current_vmcs, cpu) = NULL; 699 700 vmcs_clear(loaded_vmcs->vmcs); 701 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) 702 vmcs_clear(loaded_vmcs->shadow_vmcs); 703 704 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); 705 706 /* 707 * Ensure all writes to loaded_vmcs, including deleting it from its 708 * current percpu list, complete before setting loaded_vmcs->cpu to 709 * -1, otherwise a different cpu can see loaded_vmcs->cpu == -1 first 710 * and add loaded_vmcs to its percpu list before it's deleted from this 711 * cpu's list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs(). 712 */ 713 smp_wmb(); 714 715 loaded_vmcs->cpu = -1; 716 loaded_vmcs->launched = 0; 717 } 718 719 void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) 720 { 721 int cpu = loaded_vmcs->cpu; 722 723 if (cpu != -1) 724 smp_call_function_single(cpu, 725 __loaded_vmcs_clear, loaded_vmcs, 1); 726 } 727 728 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, 729 unsigned field) 730 { 731 bool ret; 732 u32 mask = 1 << (seg * SEG_FIELD_NR + field); 733 734 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) { 735 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS); 736 vmx->segment_cache.bitmask = 0; 737 } 738 ret = vmx->segment_cache.bitmask & mask; 739 vmx->segment_cache.bitmask |= mask; 740 return ret; 741 } 742 743 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) 744 { 745 u16 *p = &vmx->segment_cache.seg[seg].selector; 746 747 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) 748 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); 749 return *p; 750 } 751 752 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) 753 { 754 ulong *p = &vmx->segment_cache.seg[seg].base; 755 756 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) 757 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); 758 return *p; 759 } 760 761 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) 762 { 763 u32 *p = &vmx->segment_cache.seg[seg].limit; 764 765 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) 766 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); 767 return *p; 768 } 769 770 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) 771 { 772 u32 *p = &vmx->segment_cache.seg[seg].ar; 773 774 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) 775 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); 776 return *p; 777 } 778 779 void vmx_update_exception_bitmap(struct kvm_vcpu *vcpu) 780 { 781 u32 eb; 782 783 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | 784 (1u << DB_VECTOR) | (1u << AC_VECTOR); 785 /* 786 * Guest access to VMware backdoor ports could legitimately 787 * trigger #GP because of TSS I/O permission bitmap. 788 * We intercept those #GP and allow access to them anyway 789 * as VMware does. 790 */ 791 if (enable_vmware_backdoor) 792 eb |= (1u << GP_VECTOR); 793 if ((vcpu->guest_debug & 794 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == 795 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) 796 eb |= 1u << BP_VECTOR; 797 if (to_vmx(vcpu)->rmode.vm86_active) 798 eb = ~0; 799 if (!vmx_need_pf_intercept(vcpu)) 800 eb &= ~(1u << PF_VECTOR); 801 802 /* When we are running a nested L2 guest and L1 specified for it a 803 * certain exception bitmap, we must trap the same exceptions and pass 804 * them to L1. When running L2, we will only handle the exceptions 805 * specified above if L1 did not want them. 806 */ 807 if (is_guest_mode(vcpu)) 808 eb |= get_vmcs12(vcpu)->exception_bitmap; 809 else { 810 int mask = 0, match = 0; 811 812 if (enable_ept && (eb & (1u << PF_VECTOR))) { 813 /* 814 * If EPT is enabled, #PF is currently only intercepted 815 * if MAXPHYADDR is smaller on the guest than on the 816 * host. In that case we only care about present, 817 * non-reserved faults. For vmcs02, however, PFEC_MASK 818 * and PFEC_MATCH are set in prepare_vmcs02_rare. 819 */ 820 mask = PFERR_PRESENT_MASK | PFERR_RSVD_MASK; 821 match = PFERR_PRESENT_MASK; 822 } 823 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, mask); 824 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, match); 825 } 826 827 /* 828 * Disabling xfd interception indicates that dynamic xfeatures 829 * might be used in the guest. Always trap #NM in this case 830 * to save guest xfd_err timely. 831 */ 832 if (vcpu->arch.xfd_no_write_intercept) 833 eb |= (1u << NM_VECTOR); 834 835 vmcs_write32(EXCEPTION_BITMAP, eb); 836 } 837 838 /* 839 * Check if MSR is intercepted for currently loaded MSR bitmap. 840 */ 841 static bool msr_write_intercepted(struct vcpu_vmx *vmx, u32 msr) 842 { 843 if (!(exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS)) 844 return true; 845 846 return vmx_test_msr_bitmap_write(vmx->loaded_vmcs->msr_bitmap, msr); 847 } 848 849 unsigned int __vmx_vcpu_run_flags(struct vcpu_vmx *vmx) 850 { 851 unsigned int flags = 0; 852 853 if (vmx->loaded_vmcs->launched) 854 flags |= VMX_RUN_VMRESUME; 855 856 /* 857 * If writes to the SPEC_CTRL MSR aren't intercepted, the guest is free 858 * to change it directly without causing a vmexit. In that case read 859 * it after vmexit and store it in vmx->spec_ctrl. 860 */ 861 if (!msr_write_intercepted(vmx, MSR_IA32_SPEC_CTRL)) 862 flags |= VMX_RUN_SAVE_SPEC_CTRL; 863 864 return flags; 865 } 866 867 static __always_inline void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, 868 unsigned long entry, unsigned long exit) 869 { 870 vm_entry_controls_clearbit(vmx, entry); 871 vm_exit_controls_clearbit(vmx, exit); 872 } 873 874 int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr) 875 { 876 unsigned int i; 877 878 for (i = 0; i < m->nr; ++i) { 879 if (m->val[i].index == msr) 880 return i; 881 } 882 return -ENOENT; 883 } 884 885 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) 886 { 887 int i; 888 struct msr_autoload *m = &vmx->msr_autoload; 889 890 switch (msr) { 891 case MSR_EFER: 892 if (cpu_has_load_ia32_efer()) { 893 clear_atomic_switch_msr_special(vmx, 894 VM_ENTRY_LOAD_IA32_EFER, 895 VM_EXIT_LOAD_IA32_EFER); 896 return; 897 } 898 break; 899 case MSR_CORE_PERF_GLOBAL_CTRL: 900 if (cpu_has_load_perf_global_ctrl()) { 901 clear_atomic_switch_msr_special(vmx, 902 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, 903 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); 904 return; 905 } 906 break; 907 } 908 i = vmx_find_loadstore_msr_slot(&m->guest, msr); 909 if (i < 0) 910 goto skip_guest; 911 --m->guest.nr; 912 m->guest.val[i] = m->guest.val[m->guest.nr]; 913 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); 914 915 skip_guest: 916 i = vmx_find_loadstore_msr_slot(&m->host, msr); 917 if (i < 0) 918 return; 919 920 --m->host.nr; 921 m->host.val[i] = m->host.val[m->host.nr]; 922 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); 923 } 924 925 static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, 926 unsigned long entry, unsigned long exit, 927 unsigned long guest_val_vmcs, unsigned long host_val_vmcs, 928 u64 guest_val, u64 host_val) 929 { 930 vmcs_write64(guest_val_vmcs, guest_val); 931 if (host_val_vmcs != HOST_IA32_EFER) 932 vmcs_write64(host_val_vmcs, host_val); 933 vm_entry_controls_setbit(vmx, entry); 934 vm_exit_controls_setbit(vmx, exit); 935 } 936 937 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, 938 u64 guest_val, u64 host_val, bool entry_only) 939 { 940 int i, j = 0; 941 struct msr_autoload *m = &vmx->msr_autoload; 942 943 switch (msr) { 944 case MSR_EFER: 945 if (cpu_has_load_ia32_efer()) { 946 add_atomic_switch_msr_special(vmx, 947 VM_ENTRY_LOAD_IA32_EFER, 948 VM_EXIT_LOAD_IA32_EFER, 949 GUEST_IA32_EFER, 950 HOST_IA32_EFER, 951 guest_val, host_val); 952 return; 953 } 954 break; 955 case MSR_CORE_PERF_GLOBAL_CTRL: 956 if (cpu_has_load_perf_global_ctrl()) { 957 add_atomic_switch_msr_special(vmx, 958 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, 959 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, 960 GUEST_IA32_PERF_GLOBAL_CTRL, 961 HOST_IA32_PERF_GLOBAL_CTRL, 962 guest_val, host_val); 963 return; 964 } 965 break; 966 case MSR_IA32_PEBS_ENABLE: 967 /* PEBS needs a quiescent period after being disabled (to write 968 * a record). Disabling PEBS through VMX MSR swapping doesn't 969 * provide that period, so a CPU could write host's record into 970 * guest's memory. 971 */ 972 wrmsrl(MSR_IA32_PEBS_ENABLE, 0); 973 } 974 975 i = vmx_find_loadstore_msr_slot(&m->guest, msr); 976 if (!entry_only) 977 j = vmx_find_loadstore_msr_slot(&m->host, msr); 978 979 if ((i < 0 && m->guest.nr == MAX_NR_LOADSTORE_MSRS) || 980 (j < 0 && m->host.nr == MAX_NR_LOADSTORE_MSRS)) { 981 printk_once(KERN_WARNING "Not enough msr switch entries. " 982 "Can't add msr %x\n", msr); 983 return; 984 } 985 if (i < 0) { 986 i = m->guest.nr++; 987 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); 988 } 989 m->guest.val[i].index = msr; 990 m->guest.val[i].value = guest_val; 991 992 if (entry_only) 993 return; 994 995 if (j < 0) { 996 j = m->host.nr++; 997 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); 998 } 999 m->host.val[j].index = msr; 1000 m->host.val[j].value = host_val; 1001 } 1002 1003 static bool update_transition_efer(struct vcpu_vmx *vmx) 1004 { 1005 u64 guest_efer = vmx->vcpu.arch.efer; 1006 u64 ignore_bits = 0; 1007 int i; 1008 1009 /* Shadow paging assumes NX to be available. */ 1010 if (!enable_ept) 1011 guest_efer |= EFER_NX; 1012 1013 /* 1014 * LMA and LME handled by hardware; SCE meaningless outside long mode. 1015 */ 1016 ignore_bits |= EFER_SCE; 1017 #ifdef CONFIG_X86_64 1018 ignore_bits |= EFER_LMA | EFER_LME; 1019 /* SCE is meaningful only in long mode on Intel */ 1020 if (guest_efer & EFER_LMA) 1021 ignore_bits &= ~(u64)EFER_SCE; 1022 #endif 1023 1024 /* 1025 * On EPT, we can't emulate NX, so we must switch EFER atomically. 1026 * On CPUs that support "load IA32_EFER", always switch EFER 1027 * atomically, since it's faster than switching it manually. 1028 */ 1029 if (cpu_has_load_ia32_efer() || 1030 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { 1031 if (!(guest_efer & EFER_LMA)) 1032 guest_efer &= ~EFER_LME; 1033 if (guest_efer != host_efer) 1034 add_atomic_switch_msr(vmx, MSR_EFER, 1035 guest_efer, host_efer, false); 1036 else 1037 clear_atomic_switch_msr(vmx, MSR_EFER); 1038 return false; 1039 } 1040 1041 i = kvm_find_user_return_msr(MSR_EFER); 1042 if (i < 0) 1043 return false; 1044 1045 clear_atomic_switch_msr(vmx, MSR_EFER); 1046 1047 guest_efer &= ~ignore_bits; 1048 guest_efer |= host_efer & ignore_bits; 1049 1050 vmx->guest_uret_msrs[i].data = guest_efer; 1051 vmx->guest_uret_msrs[i].mask = ~ignore_bits; 1052 1053 return true; 1054 } 1055 1056 #ifdef CONFIG_X86_32 1057 /* 1058 * On 32-bit kernels, VM exits still load the FS and GS bases from the 1059 * VMCS rather than the segment table. KVM uses this helper to figure 1060 * out the current bases to poke them into the VMCS before entry. 1061 */ 1062 static unsigned long segment_base(u16 selector) 1063 { 1064 struct desc_struct *table; 1065 unsigned long v; 1066 1067 if (!(selector & ~SEGMENT_RPL_MASK)) 1068 return 0; 1069 1070 table = get_current_gdt_ro(); 1071 1072 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { 1073 u16 ldt_selector = kvm_read_ldt(); 1074 1075 if (!(ldt_selector & ~SEGMENT_RPL_MASK)) 1076 return 0; 1077 1078 table = (struct desc_struct *)segment_base(ldt_selector); 1079 } 1080 v = get_desc_base(&table[selector >> 3]); 1081 return v; 1082 } 1083 #endif 1084 1085 static inline bool pt_can_write_msr(struct vcpu_vmx *vmx) 1086 { 1087 return vmx_pt_mode_is_host_guest() && 1088 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN); 1089 } 1090 1091 static inline bool pt_output_base_valid(struct kvm_vcpu *vcpu, u64 base) 1092 { 1093 /* The base must be 128-byte aligned and a legal physical address. */ 1094 return kvm_vcpu_is_legal_aligned_gpa(vcpu, base, 128); 1095 } 1096 1097 static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range) 1098 { 1099 u32 i; 1100 1101 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status); 1102 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); 1103 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); 1104 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); 1105 for (i = 0; i < addr_range; i++) { 1106 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]); 1107 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]); 1108 } 1109 } 1110 1111 static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range) 1112 { 1113 u32 i; 1114 1115 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status); 1116 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); 1117 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); 1118 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); 1119 for (i = 0; i < addr_range; i++) { 1120 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]); 1121 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]); 1122 } 1123 } 1124 1125 static void pt_guest_enter(struct vcpu_vmx *vmx) 1126 { 1127 if (vmx_pt_mode_is_system()) 1128 return; 1129 1130 /* 1131 * GUEST_IA32_RTIT_CTL is already set in the VMCS. 1132 * Save host state before VM entry. 1133 */ 1134 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); 1135 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { 1136 wrmsrl(MSR_IA32_RTIT_CTL, 0); 1137 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.num_address_ranges); 1138 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.num_address_ranges); 1139 } 1140 } 1141 1142 static void pt_guest_exit(struct vcpu_vmx *vmx) 1143 { 1144 if (vmx_pt_mode_is_system()) 1145 return; 1146 1147 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { 1148 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.num_address_ranges); 1149 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.num_address_ranges); 1150 } 1151 1152 /* 1153 * KVM requires VM_EXIT_CLEAR_IA32_RTIT_CTL to expose PT to the guest, 1154 * i.e. RTIT_CTL is always cleared on VM-Exit. Restore it if necessary. 1155 */ 1156 if (vmx->pt_desc.host.ctl) 1157 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); 1158 } 1159 1160 void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel, 1161 unsigned long fs_base, unsigned long gs_base) 1162 { 1163 if (unlikely(fs_sel != host->fs_sel)) { 1164 if (!(fs_sel & 7)) 1165 vmcs_write16(HOST_FS_SELECTOR, fs_sel); 1166 else 1167 vmcs_write16(HOST_FS_SELECTOR, 0); 1168 host->fs_sel = fs_sel; 1169 } 1170 if (unlikely(gs_sel != host->gs_sel)) { 1171 if (!(gs_sel & 7)) 1172 vmcs_write16(HOST_GS_SELECTOR, gs_sel); 1173 else 1174 vmcs_write16(HOST_GS_SELECTOR, 0); 1175 host->gs_sel = gs_sel; 1176 } 1177 if (unlikely(fs_base != host->fs_base)) { 1178 vmcs_writel(HOST_FS_BASE, fs_base); 1179 host->fs_base = fs_base; 1180 } 1181 if (unlikely(gs_base != host->gs_base)) { 1182 vmcs_writel(HOST_GS_BASE, gs_base); 1183 host->gs_base = gs_base; 1184 } 1185 } 1186 1187 void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) 1188 { 1189 struct vcpu_vmx *vmx = to_vmx(vcpu); 1190 struct vmcs_host_state *host_state; 1191 #ifdef CONFIG_X86_64 1192 int cpu = raw_smp_processor_id(); 1193 #endif 1194 unsigned long fs_base, gs_base; 1195 u16 fs_sel, gs_sel; 1196 int i; 1197 1198 vmx->req_immediate_exit = false; 1199 1200 /* 1201 * Note that guest MSRs to be saved/restored can also be changed 1202 * when guest state is loaded. This happens when guest transitions 1203 * to/from long-mode by setting MSR_EFER.LMA. 1204 */ 1205 if (!vmx->guest_uret_msrs_loaded) { 1206 vmx->guest_uret_msrs_loaded = true; 1207 for (i = 0; i < kvm_nr_uret_msrs; ++i) { 1208 if (!vmx->guest_uret_msrs[i].load_into_hardware) 1209 continue; 1210 1211 kvm_set_user_return_msr(i, 1212 vmx->guest_uret_msrs[i].data, 1213 vmx->guest_uret_msrs[i].mask); 1214 } 1215 } 1216 1217 if (vmx->nested.need_vmcs12_to_shadow_sync) 1218 nested_sync_vmcs12_to_shadow(vcpu); 1219 1220 if (vmx->guest_state_loaded) 1221 return; 1222 1223 host_state = &vmx->loaded_vmcs->host_state; 1224 1225 /* 1226 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not 1227 * allow segment selectors with cpl > 0 or ti == 1. 1228 */ 1229 host_state->ldt_sel = kvm_read_ldt(); 1230 1231 #ifdef CONFIG_X86_64 1232 savesegment(ds, host_state->ds_sel); 1233 savesegment(es, host_state->es_sel); 1234 1235 gs_base = cpu_kernelmode_gs_base(cpu); 1236 if (likely(is_64bit_mm(current->mm))) { 1237 current_save_fsgs(); 1238 fs_sel = current->thread.fsindex; 1239 gs_sel = current->thread.gsindex; 1240 fs_base = current->thread.fsbase; 1241 vmx->msr_host_kernel_gs_base = current->thread.gsbase; 1242 } else { 1243 savesegment(fs, fs_sel); 1244 savesegment(gs, gs_sel); 1245 fs_base = read_msr(MSR_FS_BASE); 1246 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE); 1247 } 1248 1249 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); 1250 #else 1251 savesegment(fs, fs_sel); 1252 savesegment(gs, gs_sel); 1253 fs_base = segment_base(fs_sel); 1254 gs_base = segment_base(gs_sel); 1255 #endif 1256 1257 vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base); 1258 vmx->guest_state_loaded = true; 1259 } 1260 1261 static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx) 1262 { 1263 struct vmcs_host_state *host_state; 1264 1265 if (!vmx->guest_state_loaded) 1266 return; 1267 1268 host_state = &vmx->loaded_vmcs->host_state; 1269 1270 ++vmx->vcpu.stat.host_state_reload; 1271 1272 #ifdef CONFIG_X86_64 1273 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); 1274 #endif 1275 if (host_state->ldt_sel || (host_state->gs_sel & 7)) { 1276 kvm_load_ldt(host_state->ldt_sel); 1277 #ifdef CONFIG_X86_64 1278 load_gs_index(host_state->gs_sel); 1279 #else 1280 loadsegment(gs, host_state->gs_sel); 1281 #endif 1282 } 1283 if (host_state->fs_sel & 7) 1284 loadsegment(fs, host_state->fs_sel); 1285 #ifdef CONFIG_X86_64 1286 if (unlikely(host_state->ds_sel | host_state->es_sel)) { 1287 loadsegment(ds, host_state->ds_sel); 1288 loadsegment(es, host_state->es_sel); 1289 } 1290 #endif 1291 invalidate_tss_limit(); 1292 #ifdef CONFIG_X86_64 1293 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); 1294 #endif 1295 load_fixmap_gdt(raw_smp_processor_id()); 1296 vmx->guest_state_loaded = false; 1297 vmx->guest_uret_msrs_loaded = false; 1298 } 1299 1300 #ifdef CONFIG_X86_64 1301 static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx) 1302 { 1303 preempt_disable(); 1304 if (vmx->guest_state_loaded) 1305 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); 1306 preempt_enable(); 1307 return vmx->msr_guest_kernel_gs_base; 1308 } 1309 1310 static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data) 1311 { 1312 preempt_disable(); 1313 if (vmx->guest_state_loaded) 1314 wrmsrl(MSR_KERNEL_GS_BASE, data); 1315 preempt_enable(); 1316 vmx->msr_guest_kernel_gs_base = data; 1317 } 1318 #endif 1319 1320 void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu, 1321 struct loaded_vmcs *buddy) 1322 { 1323 struct vcpu_vmx *vmx = to_vmx(vcpu); 1324 bool already_loaded = vmx->loaded_vmcs->cpu == cpu; 1325 struct vmcs *prev; 1326 1327 if (!already_loaded) { 1328 loaded_vmcs_clear(vmx->loaded_vmcs); 1329 local_irq_disable(); 1330 1331 /* 1332 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to 1333 * this cpu's percpu list, otherwise it may not yet be deleted 1334 * from its previous cpu's percpu list. Pairs with the 1335 * smb_wmb() in __loaded_vmcs_clear(). 1336 */ 1337 smp_rmb(); 1338 1339 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, 1340 &per_cpu(loaded_vmcss_on_cpu, cpu)); 1341 local_irq_enable(); 1342 } 1343 1344 prev = per_cpu(current_vmcs, cpu); 1345 if (prev != vmx->loaded_vmcs->vmcs) { 1346 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; 1347 vmcs_load(vmx->loaded_vmcs->vmcs); 1348 1349 /* 1350 * No indirect branch prediction barrier needed when switching 1351 * the active VMCS within a vCPU, unless IBRS is advertised to 1352 * the vCPU. To minimize the number of IBPBs executed, KVM 1353 * performs IBPB on nested VM-Exit (a single nested transition 1354 * may switch the active VMCS multiple times). 1355 */ 1356 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev)) 1357 indirect_branch_prediction_barrier(); 1358 } 1359 1360 if (!already_loaded) { 1361 void *gdt = get_current_gdt_ro(); 1362 1363 /* 1364 * Flush all EPTP/VPID contexts, the new pCPU may have stale 1365 * TLB entries from its previous association with the vCPU. 1366 */ 1367 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 1368 1369 /* 1370 * Linux uses per-cpu TSS and GDT, so set these when switching 1371 * processors. See 22.2.4. 1372 */ 1373 vmcs_writel(HOST_TR_BASE, 1374 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); 1375 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ 1376 1377 if (IS_ENABLED(CONFIG_IA32_EMULATION) || IS_ENABLED(CONFIG_X86_32)) { 1378 /* 22.2.3 */ 1379 vmcs_writel(HOST_IA32_SYSENTER_ESP, 1380 (unsigned long)(cpu_entry_stack(cpu) + 1)); 1381 } 1382 1383 vmx->loaded_vmcs->cpu = cpu; 1384 } 1385 } 1386 1387 /* 1388 * Switches to specified vcpu, until a matching vcpu_put(), but assumes 1389 * vcpu mutex is already taken. 1390 */ 1391 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 1392 { 1393 struct vcpu_vmx *vmx = to_vmx(vcpu); 1394 1395 vmx_vcpu_load_vmcs(vcpu, cpu, NULL); 1396 1397 vmx_vcpu_pi_load(vcpu, cpu); 1398 1399 vmx->host_debugctlmsr = get_debugctlmsr(); 1400 } 1401 1402 static void vmx_vcpu_put(struct kvm_vcpu *vcpu) 1403 { 1404 vmx_vcpu_pi_put(vcpu); 1405 1406 vmx_prepare_switch_to_host(to_vmx(vcpu)); 1407 } 1408 1409 bool vmx_emulation_required(struct kvm_vcpu *vcpu) 1410 { 1411 return emulate_invalid_guest_state && !vmx_guest_state_valid(vcpu); 1412 } 1413 1414 unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) 1415 { 1416 struct vcpu_vmx *vmx = to_vmx(vcpu); 1417 unsigned long rflags, save_rflags; 1418 1419 if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) { 1420 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS); 1421 rflags = vmcs_readl(GUEST_RFLAGS); 1422 if (vmx->rmode.vm86_active) { 1423 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; 1424 save_rflags = vmx->rmode.save_rflags; 1425 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; 1426 } 1427 vmx->rflags = rflags; 1428 } 1429 return vmx->rflags; 1430 } 1431 1432 void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) 1433 { 1434 struct vcpu_vmx *vmx = to_vmx(vcpu); 1435 unsigned long old_rflags; 1436 1437 if (is_unrestricted_guest(vcpu)) { 1438 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS); 1439 vmx->rflags = rflags; 1440 vmcs_writel(GUEST_RFLAGS, rflags); 1441 return; 1442 } 1443 1444 old_rflags = vmx_get_rflags(vcpu); 1445 vmx->rflags = rflags; 1446 if (vmx->rmode.vm86_active) { 1447 vmx->rmode.save_rflags = rflags; 1448 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; 1449 } 1450 vmcs_writel(GUEST_RFLAGS, rflags); 1451 1452 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM) 1453 vmx->emulation_required = vmx_emulation_required(vcpu); 1454 } 1455 1456 static bool vmx_get_if_flag(struct kvm_vcpu *vcpu) 1457 { 1458 return vmx_get_rflags(vcpu) & X86_EFLAGS_IF; 1459 } 1460 1461 u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) 1462 { 1463 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); 1464 int ret = 0; 1465 1466 if (interruptibility & GUEST_INTR_STATE_STI) 1467 ret |= KVM_X86_SHADOW_INT_STI; 1468 if (interruptibility & GUEST_INTR_STATE_MOV_SS) 1469 ret |= KVM_X86_SHADOW_INT_MOV_SS; 1470 1471 return ret; 1472 } 1473 1474 void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) 1475 { 1476 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); 1477 u32 interruptibility = interruptibility_old; 1478 1479 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); 1480 1481 if (mask & KVM_X86_SHADOW_INT_MOV_SS) 1482 interruptibility |= GUEST_INTR_STATE_MOV_SS; 1483 else if (mask & KVM_X86_SHADOW_INT_STI) 1484 interruptibility |= GUEST_INTR_STATE_STI; 1485 1486 if ((interruptibility != interruptibility_old)) 1487 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); 1488 } 1489 1490 static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data) 1491 { 1492 struct vcpu_vmx *vmx = to_vmx(vcpu); 1493 unsigned long value; 1494 1495 /* 1496 * Any MSR write that attempts to change bits marked reserved will 1497 * case a #GP fault. 1498 */ 1499 if (data & vmx->pt_desc.ctl_bitmask) 1500 return 1; 1501 1502 /* 1503 * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will 1504 * result in a #GP unless the same write also clears TraceEn. 1505 */ 1506 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) && 1507 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN)) 1508 return 1; 1509 1510 /* 1511 * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit 1512 * and FabricEn would cause #GP, if 1513 * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0 1514 */ 1515 if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) && 1516 !(data & RTIT_CTL_FABRIC_EN) && 1517 !intel_pt_validate_cap(vmx->pt_desc.caps, 1518 PT_CAP_single_range_output)) 1519 return 1; 1520 1521 /* 1522 * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that 1523 * utilize encodings marked reserved will cause a #GP fault. 1524 */ 1525 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods); 1526 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) && 1527 !test_bit((data & RTIT_CTL_MTC_RANGE) >> 1528 RTIT_CTL_MTC_RANGE_OFFSET, &value)) 1529 return 1; 1530 value = intel_pt_validate_cap(vmx->pt_desc.caps, 1531 PT_CAP_cycle_thresholds); 1532 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) && 1533 !test_bit((data & RTIT_CTL_CYC_THRESH) >> 1534 RTIT_CTL_CYC_THRESH_OFFSET, &value)) 1535 return 1; 1536 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods); 1537 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) && 1538 !test_bit((data & RTIT_CTL_PSB_FREQ) >> 1539 RTIT_CTL_PSB_FREQ_OFFSET, &value)) 1540 return 1; 1541 1542 /* 1543 * If ADDRx_CFG is reserved or the encodings is >2 will 1544 * cause a #GP fault. 1545 */ 1546 value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET; 1547 if ((value && (vmx->pt_desc.num_address_ranges < 1)) || (value > 2)) 1548 return 1; 1549 value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET; 1550 if ((value && (vmx->pt_desc.num_address_ranges < 2)) || (value > 2)) 1551 return 1; 1552 value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET; 1553 if ((value && (vmx->pt_desc.num_address_ranges < 3)) || (value > 2)) 1554 return 1; 1555 value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET; 1556 if ((value && (vmx->pt_desc.num_address_ranges < 4)) || (value > 2)) 1557 return 1; 1558 1559 return 0; 1560 } 1561 1562 static bool vmx_can_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type, 1563 void *insn, int insn_len) 1564 { 1565 /* 1566 * Emulation of instructions in SGX enclaves is impossible as RIP does 1567 * not point at the failing instruction, and even if it did, the code 1568 * stream is inaccessible. Inject #UD instead of exiting to userspace 1569 * so that guest userspace can't DoS the guest simply by triggering 1570 * emulation (enclaves are CPL3 only). 1571 */ 1572 if (to_vmx(vcpu)->exit_reason.enclave_mode) { 1573 kvm_queue_exception(vcpu, UD_VECTOR); 1574 return false; 1575 } 1576 return true; 1577 } 1578 1579 static int skip_emulated_instruction(struct kvm_vcpu *vcpu) 1580 { 1581 union vmx_exit_reason exit_reason = to_vmx(vcpu)->exit_reason; 1582 unsigned long rip, orig_rip; 1583 u32 instr_len; 1584 1585 /* 1586 * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on 1587 * undefined behavior: Intel's SDM doesn't mandate the VMCS field be 1588 * set when EPT misconfig occurs. In practice, real hardware updates 1589 * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors 1590 * (namely Hyper-V) don't set it due to it being undefined behavior, 1591 * i.e. we end up advancing IP with some random value. 1592 */ 1593 if (!static_cpu_has(X86_FEATURE_HYPERVISOR) || 1594 exit_reason.basic != EXIT_REASON_EPT_MISCONFIG) { 1595 instr_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); 1596 1597 /* 1598 * Emulating an enclave's instructions isn't supported as KVM 1599 * cannot access the enclave's memory or its true RIP, e.g. the 1600 * vmcs.GUEST_RIP points at the exit point of the enclave, not 1601 * the RIP that actually triggered the VM-Exit. But, because 1602 * most instructions that cause VM-Exit will #UD in an enclave, 1603 * most instruction-based VM-Exits simply do not occur. 1604 * 1605 * There are a few exceptions, notably the debug instructions 1606 * INT1ICEBRK and INT3, as they are allowed in debug enclaves 1607 * and generate #DB/#BP as expected, which KVM might intercept. 1608 * But again, the CPU does the dirty work and saves an instr 1609 * length of zero so VMMs don't shoot themselves in the foot. 1610 * WARN if KVM tries to skip a non-zero length instruction on 1611 * a VM-Exit from an enclave. 1612 */ 1613 if (!instr_len) 1614 goto rip_updated; 1615 1616 WARN(exit_reason.enclave_mode, 1617 "KVM: skipping instruction after SGX enclave VM-Exit"); 1618 1619 orig_rip = kvm_rip_read(vcpu); 1620 rip = orig_rip + instr_len; 1621 #ifdef CONFIG_X86_64 1622 /* 1623 * We need to mask out the high 32 bits of RIP if not in 64-bit 1624 * mode, but just finding out that we are in 64-bit mode is 1625 * quite expensive. Only do it if there was a carry. 1626 */ 1627 if (unlikely(((rip ^ orig_rip) >> 31) == 3) && !is_64_bit_mode(vcpu)) 1628 rip = (u32)rip; 1629 #endif 1630 kvm_rip_write(vcpu, rip); 1631 } else { 1632 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP)) 1633 return 0; 1634 } 1635 1636 rip_updated: 1637 /* skipping an emulated instruction also counts */ 1638 vmx_set_interrupt_shadow(vcpu, 0); 1639 1640 return 1; 1641 } 1642 1643 /* 1644 * Recognizes a pending MTF VM-exit and records the nested state for later 1645 * delivery. 1646 */ 1647 static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu) 1648 { 1649 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 1650 struct vcpu_vmx *vmx = to_vmx(vcpu); 1651 1652 if (!is_guest_mode(vcpu)) 1653 return; 1654 1655 /* 1656 * Per the SDM, MTF takes priority over debug-trap exceptions besides 1657 * TSS T-bit traps and ICEBP (INT1). KVM doesn't emulate T-bit traps 1658 * or ICEBP (in the emulator proper), and skipping of ICEBP after an 1659 * intercepted #DB deliberately avoids single-step #DB and MTF updates 1660 * as ICEBP is higher priority than both. As instruction emulation is 1661 * completed at this point (i.e. KVM is at the instruction boundary), 1662 * any #DB exception pending delivery must be a debug-trap of lower 1663 * priority than MTF. Record the pending MTF state to be delivered in 1664 * vmx_check_nested_events(). 1665 */ 1666 if (nested_cpu_has_mtf(vmcs12) && 1667 (!vcpu->arch.exception.pending || 1668 vcpu->arch.exception.vector == DB_VECTOR) && 1669 (!vcpu->arch.exception_vmexit.pending || 1670 vcpu->arch.exception_vmexit.vector == DB_VECTOR)) { 1671 vmx->nested.mtf_pending = true; 1672 kvm_make_request(KVM_REQ_EVENT, vcpu); 1673 } else { 1674 vmx->nested.mtf_pending = false; 1675 } 1676 } 1677 1678 static int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu) 1679 { 1680 vmx_update_emulated_instruction(vcpu); 1681 return skip_emulated_instruction(vcpu); 1682 } 1683 1684 static void vmx_clear_hlt(struct kvm_vcpu *vcpu) 1685 { 1686 /* 1687 * Ensure that we clear the HLT state in the VMCS. We don't need to 1688 * explicitly skip the instruction because if the HLT state is set, 1689 * then the instruction is already executing and RIP has already been 1690 * advanced. 1691 */ 1692 if (kvm_hlt_in_guest(vcpu->kvm) && 1693 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) 1694 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); 1695 } 1696 1697 static void vmx_inject_exception(struct kvm_vcpu *vcpu) 1698 { 1699 struct kvm_queued_exception *ex = &vcpu->arch.exception; 1700 u32 intr_info = ex->vector | INTR_INFO_VALID_MASK; 1701 struct vcpu_vmx *vmx = to_vmx(vcpu); 1702 1703 kvm_deliver_exception_payload(vcpu, ex); 1704 1705 if (ex->has_error_code) { 1706 /* 1707 * Despite the error code being architecturally defined as 32 1708 * bits, and the VMCS field being 32 bits, Intel CPUs and thus 1709 * VMX don't actually supporting setting bits 31:16. Hardware 1710 * will (should) never provide a bogus error code, but AMD CPUs 1711 * do generate error codes with bits 31:16 set, and so KVM's 1712 * ABI lets userspace shove in arbitrary 32-bit values. Drop 1713 * the upper bits to avoid VM-Fail, losing information that 1714 * does't really exist is preferable to killing the VM. 1715 */ 1716 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, (u16)ex->error_code); 1717 intr_info |= INTR_INFO_DELIVER_CODE_MASK; 1718 } 1719 1720 if (vmx->rmode.vm86_active) { 1721 int inc_eip = 0; 1722 if (kvm_exception_is_soft(ex->vector)) 1723 inc_eip = vcpu->arch.event_exit_inst_len; 1724 kvm_inject_realmode_interrupt(vcpu, ex->vector, inc_eip); 1725 return; 1726 } 1727 1728 WARN_ON_ONCE(vmx->emulation_required); 1729 1730 if (kvm_exception_is_soft(ex->vector)) { 1731 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1732 vmx->vcpu.arch.event_exit_inst_len); 1733 intr_info |= INTR_TYPE_SOFT_EXCEPTION; 1734 } else 1735 intr_info |= INTR_TYPE_HARD_EXCEPTION; 1736 1737 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); 1738 1739 vmx_clear_hlt(vcpu); 1740 } 1741 1742 static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr, 1743 bool load_into_hardware) 1744 { 1745 struct vmx_uret_msr *uret_msr; 1746 1747 uret_msr = vmx_find_uret_msr(vmx, msr); 1748 if (!uret_msr) 1749 return; 1750 1751 uret_msr->load_into_hardware = load_into_hardware; 1752 } 1753 1754 /* 1755 * Configuring user return MSRs to automatically save, load, and restore MSRs 1756 * that need to be shoved into hardware when running the guest. Note, omitting 1757 * an MSR here does _NOT_ mean it's not emulated, only that it will not be 1758 * loaded into hardware when running the guest. 1759 */ 1760 static void vmx_setup_uret_msrs(struct vcpu_vmx *vmx) 1761 { 1762 #ifdef CONFIG_X86_64 1763 bool load_syscall_msrs; 1764 1765 /* 1766 * The SYSCALL MSRs are only needed on long mode guests, and only 1767 * when EFER.SCE is set. 1768 */ 1769 load_syscall_msrs = is_long_mode(&vmx->vcpu) && 1770 (vmx->vcpu.arch.efer & EFER_SCE); 1771 1772 vmx_setup_uret_msr(vmx, MSR_STAR, load_syscall_msrs); 1773 vmx_setup_uret_msr(vmx, MSR_LSTAR, load_syscall_msrs); 1774 vmx_setup_uret_msr(vmx, MSR_SYSCALL_MASK, load_syscall_msrs); 1775 #endif 1776 vmx_setup_uret_msr(vmx, MSR_EFER, update_transition_efer(vmx)); 1777 1778 vmx_setup_uret_msr(vmx, MSR_TSC_AUX, 1779 guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP) || 1780 guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDPID)); 1781 1782 /* 1783 * hle=0, rtm=0, tsx_ctrl=1 can be found with some combinations of new 1784 * kernel and old userspace. If those guests run on a tsx=off host, do 1785 * allow guests to use TSX_CTRL, but don't change the value in hardware 1786 * so that TSX remains always disabled. 1787 */ 1788 vmx_setup_uret_msr(vmx, MSR_IA32_TSX_CTRL, boot_cpu_has(X86_FEATURE_RTM)); 1789 1790 /* 1791 * The set of MSRs to load may have changed, reload MSRs before the 1792 * next VM-Enter. 1793 */ 1794 vmx->guest_uret_msrs_loaded = false; 1795 } 1796 1797 u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu) 1798 { 1799 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 1800 1801 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) 1802 return vmcs12->tsc_offset; 1803 1804 return 0; 1805 } 1806 1807 u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu) 1808 { 1809 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 1810 1811 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING) && 1812 nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING)) 1813 return vmcs12->tsc_multiplier; 1814 1815 return kvm_caps.default_tsc_scaling_ratio; 1816 } 1817 1818 static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) 1819 { 1820 vmcs_write64(TSC_OFFSET, offset); 1821 } 1822 1823 static void vmx_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier) 1824 { 1825 vmcs_write64(TSC_MULTIPLIER, multiplier); 1826 } 1827 1828 /* 1829 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX 1830 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for 1831 * all guests if the "nested" module option is off, and can also be disabled 1832 * for a single guest by disabling its VMX cpuid bit. 1833 */ 1834 bool nested_vmx_allowed(struct kvm_vcpu *vcpu) 1835 { 1836 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); 1837 } 1838 1839 /* 1840 * Userspace is allowed to set any supported IA32_FEATURE_CONTROL regardless of 1841 * guest CPUID. Note, KVM allows userspace to set "VMX in SMX" to maintain 1842 * backwards compatibility even though KVM doesn't support emulating SMX. And 1843 * because userspace set "VMX in SMX", the guest must also be allowed to set it, 1844 * e.g. if the MSR is left unlocked and the guest does a RMW operation. 1845 */ 1846 #define KVM_SUPPORTED_FEATURE_CONTROL (FEAT_CTL_LOCKED | \ 1847 FEAT_CTL_VMX_ENABLED_INSIDE_SMX | \ 1848 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX | \ 1849 FEAT_CTL_SGX_LC_ENABLED | \ 1850 FEAT_CTL_SGX_ENABLED | \ 1851 FEAT_CTL_LMCE_ENABLED) 1852 1853 static inline bool is_vmx_feature_control_msr_valid(struct vcpu_vmx *vmx, 1854 struct msr_data *msr) 1855 { 1856 uint64_t valid_bits; 1857 1858 /* 1859 * Ensure KVM_SUPPORTED_FEATURE_CONTROL is updated when new bits are 1860 * exposed to the guest. 1861 */ 1862 WARN_ON_ONCE(vmx->msr_ia32_feature_control_valid_bits & 1863 ~KVM_SUPPORTED_FEATURE_CONTROL); 1864 1865 if (!msr->host_initiated && 1866 (vmx->msr_ia32_feature_control & FEAT_CTL_LOCKED)) 1867 return false; 1868 1869 if (msr->host_initiated) 1870 valid_bits = KVM_SUPPORTED_FEATURE_CONTROL; 1871 else 1872 valid_bits = vmx->msr_ia32_feature_control_valid_bits; 1873 1874 return !(msr->data & ~valid_bits); 1875 } 1876 1877 static int vmx_get_msr_feature(struct kvm_msr_entry *msr) 1878 { 1879 switch (msr->index) { 1880 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: 1881 if (!nested) 1882 return 1; 1883 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); 1884 default: 1885 return KVM_MSR_RET_INVALID; 1886 } 1887 } 1888 1889 /* 1890 * Reads an msr value (of 'msr_info->index') into 'msr_info->data'. 1891 * Returns 0 on success, non-0 otherwise. 1892 * Assumes vcpu_load() was already called. 1893 */ 1894 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) 1895 { 1896 struct vcpu_vmx *vmx = to_vmx(vcpu); 1897 struct vmx_uret_msr *msr; 1898 u32 index; 1899 1900 switch (msr_info->index) { 1901 #ifdef CONFIG_X86_64 1902 case MSR_FS_BASE: 1903 msr_info->data = vmcs_readl(GUEST_FS_BASE); 1904 break; 1905 case MSR_GS_BASE: 1906 msr_info->data = vmcs_readl(GUEST_GS_BASE); 1907 break; 1908 case MSR_KERNEL_GS_BASE: 1909 msr_info->data = vmx_read_guest_kernel_gs_base(vmx); 1910 break; 1911 #endif 1912 case MSR_EFER: 1913 return kvm_get_msr_common(vcpu, msr_info); 1914 case MSR_IA32_TSX_CTRL: 1915 if (!msr_info->host_initiated && 1916 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR)) 1917 return 1; 1918 goto find_uret_msr; 1919 case MSR_IA32_UMWAIT_CONTROL: 1920 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx)) 1921 return 1; 1922 1923 msr_info->data = vmx->msr_ia32_umwait_control; 1924 break; 1925 case MSR_IA32_SPEC_CTRL: 1926 if (!msr_info->host_initiated && 1927 !guest_has_spec_ctrl_msr(vcpu)) 1928 return 1; 1929 1930 msr_info->data = to_vmx(vcpu)->spec_ctrl; 1931 break; 1932 case MSR_IA32_SYSENTER_CS: 1933 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); 1934 break; 1935 case MSR_IA32_SYSENTER_EIP: 1936 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); 1937 break; 1938 case MSR_IA32_SYSENTER_ESP: 1939 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); 1940 break; 1941 case MSR_IA32_BNDCFGS: 1942 if (!kvm_mpx_supported() || 1943 (!msr_info->host_initiated && 1944 !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) 1945 return 1; 1946 msr_info->data = vmcs_read64(GUEST_BNDCFGS); 1947 break; 1948 case MSR_IA32_MCG_EXT_CTL: 1949 if (!msr_info->host_initiated && 1950 !(vmx->msr_ia32_feature_control & 1951 FEAT_CTL_LMCE_ENABLED)) 1952 return 1; 1953 msr_info->data = vcpu->arch.mcg_ext_ctl; 1954 break; 1955 case MSR_IA32_FEAT_CTL: 1956 msr_info->data = vmx->msr_ia32_feature_control; 1957 break; 1958 case MSR_IA32_SGXLEPUBKEYHASH0 ... MSR_IA32_SGXLEPUBKEYHASH3: 1959 if (!msr_info->host_initiated && 1960 !guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC)) 1961 return 1; 1962 msr_info->data = to_vmx(vcpu)->msr_ia32_sgxlepubkeyhash 1963 [msr_info->index - MSR_IA32_SGXLEPUBKEYHASH0]; 1964 break; 1965 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: 1966 if (!nested_vmx_allowed(vcpu)) 1967 return 1; 1968 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, 1969 &msr_info->data)) 1970 return 1; 1971 /* 1972 * Enlightened VMCS v1 doesn't have certain VMCS fields but 1973 * instead of just ignoring the features, different Hyper-V 1974 * versions are either trying to use them and fail or do some 1975 * sanity checking and refuse to boot. Filter all unsupported 1976 * features out. 1977 */ 1978 if (!msr_info->host_initiated && guest_cpuid_has_evmcs(vcpu)) 1979 nested_evmcs_filter_control_msr(vcpu, msr_info->index, 1980 &msr_info->data); 1981 break; 1982 case MSR_IA32_RTIT_CTL: 1983 if (!vmx_pt_mode_is_host_guest()) 1984 return 1; 1985 msr_info->data = vmx->pt_desc.guest.ctl; 1986 break; 1987 case MSR_IA32_RTIT_STATUS: 1988 if (!vmx_pt_mode_is_host_guest()) 1989 return 1; 1990 msr_info->data = vmx->pt_desc.guest.status; 1991 break; 1992 case MSR_IA32_RTIT_CR3_MATCH: 1993 if (!vmx_pt_mode_is_host_guest() || 1994 !intel_pt_validate_cap(vmx->pt_desc.caps, 1995 PT_CAP_cr3_filtering)) 1996 return 1; 1997 msr_info->data = vmx->pt_desc.guest.cr3_match; 1998 break; 1999 case MSR_IA32_RTIT_OUTPUT_BASE: 2000 if (!vmx_pt_mode_is_host_guest() || 2001 (!intel_pt_validate_cap(vmx->pt_desc.caps, 2002 PT_CAP_topa_output) && 2003 !intel_pt_validate_cap(vmx->pt_desc.caps, 2004 PT_CAP_single_range_output))) 2005 return 1; 2006 msr_info->data = vmx->pt_desc.guest.output_base; 2007 break; 2008 case MSR_IA32_RTIT_OUTPUT_MASK: 2009 if (!vmx_pt_mode_is_host_guest() || 2010 (!intel_pt_validate_cap(vmx->pt_desc.caps, 2011 PT_CAP_topa_output) && 2012 !intel_pt_validate_cap(vmx->pt_desc.caps, 2013 PT_CAP_single_range_output))) 2014 return 1; 2015 msr_info->data = vmx->pt_desc.guest.output_mask; 2016 break; 2017 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: 2018 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; 2019 if (!vmx_pt_mode_is_host_guest() || 2020 (index >= 2 * vmx->pt_desc.num_address_ranges)) 2021 return 1; 2022 if (index % 2) 2023 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2]; 2024 else 2025 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2]; 2026 break; 2027 case MSR_IA32_DEBUGCTLMSR: 2028 msr_info->data = vmcs_read64(GUEST_IA32_DEBUGCTL); 2029 break; 2030 default: 2031 find_uret_msr: 2032 msr = vmx_find_uret_msr(vmx, msr_info->index); 2033 if (msr) { 2034 msr_info->data = msr->data; 2035 break; 2036 } 2037 return kvm_get_msr_common(vcpu, msr_info); 2038 } 2039 2040 return 0; 2041 } 2042 2043 static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu, 2044 u64 data) 2045 { 2046 #ifdef CONFIG_X86_64 2047 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM)) 2048 return (u32)data; 2049 #endif 2050 return (unsigned long)data; 2051 } 2052 2053 static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated) 2054 { 2055 u64 debugctl = 0; 2056 2057 if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) && 2058 (host_initiated || guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))) 2059 debugctl |= DEBUGCTLMSR_BUS_LOCK_DETECT; 2060 2061 if ((kvm_caps.supported_perf_cap & PMU_CAP_LBR_FMT) && 2062 (host_initiated || intel_pmu_lbr_is_enabled(vcpu))) 2063 debugctl |= DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI; 2064 2065 return debugctl; 2066 } 2067 2068 /* 2069 * Writes msr value into the appropriate "register". 2070 * Returns 0 on success, non-0 otherwise. 2071 * Assumes vcpu_load() was already called. 2072 */ 2073 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) 2074 { 2075 struct vcpu_vmx *vmx = to_vmx(vcpu); 2076 struct vmx_uret_msr *msr; 2077 int ret = 0; 2078 u32 msr_index = msr_info->index; 2079 u64 data = msr_info->data; 2080 u32 index; 2081 2082 switch (msr_index) { 2083 case MSR_EFER: 2084 ret = kvm_set_msr_common(vcpu, msr_info); 2085 break; 2086 #ifdef CONFIG_X86_64 2087 case MSR_FS_BASE: 2088 vmx_segment_cache_clear(vmx); 2089 vmcs_writel(GUEST_FS_BASE, data); 2090 break; 2091 case MSR_GS_BASE: 2092 vmx_segment_cache_clear(vmx); 2093 vmcs_writel(GUEST_GS_BASE, data); 2094 break; 2095 case MSR_KERNEL_GS_BASE: 2096 vmx_write_guest_kernel_gs_base(vmx, data); 2097 break; 2098 case MSR_IA32_XFD: 2099 ret = kvm_set_msr_common(vcpu, msr_info); 2100 /* 2101 * Always intercepting WRMSR could incur non-negligible 2102 * overhead given xfd might be changed frequently in 2103 * guest context switch. Disable write interception 2104 * upon the first write with a non-zero value (indicating 2105 * potential usage on dynamic xfeatures). Also update 2106 * exception bitmap to trap #NM for proper virtualization 2107 * of guest xfd_err. 2108 */ 2109 if (!ret && data) { 2110 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_XFD, 2111 MSR_TYPE_RW); 2112 vcpu->arch.xfd_no_write_intercept = true; 2113 vmx_update_exception_bitmap(vcpu); 2114 } 2115 break; 2116 #endif 2117 case MSR_IA32_SYSENTER_CS: 2118 if (is_guest_mode(vcpu)) 2119 get_vmcs12(vcpu)->guest_sysenter_cs = data; 2120 vmcs_write32(GUEST_SYSENTER_CS, data); 2121 break; 2122 case MSR_IA32_SYSENTER_EIP: 2123 if (is_guest_mode(vcpu)) { 2124 data = nested_vmx_truncate_sysenter_addr(vcpu, data); 2125 get_vmcs12(vcpu)->guest_sysenter_eip = data; 2126 } 2127 vmcs_writel(GUEST_SYSENTER_EIP, data); 2128 break; 2129 case MSR_IA32_SYSENTER_ESP: 2130 if (is_guest_mode(vcpu)) { 2131 data = nested_vmx_truncate_sysenter_addr(vcpu, data); 2132 get_vmcs12(vcpu)->guest_sysenter_esp = data; 2133 } 2134 vmcs_writel(GUEST_SYSENTER_ESP, data); 2135 break; 2136 case MSR_IA32_DEBUGCTLMSR: { 2137 u64 invalid; 2138 2139 invalid = data & ~vmx_get_supported_debugctl(vcpu, msr_info->host_initiated); 2140 if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) { 2141 if (report_ignored_msrs) 2142 vcpu_unimpl(vcpu, "%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n", 2143 __func__, data); 2144 data &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); 2145 invalid &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); 2146 } 2147 2148 if (invalid) 2149 return 1; 2150 2151 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls & 2152 VM_EXIT_SAVE_DEBUG_CONTROLS) 2153 get_vmcs12(vcpu)->guest_ia32_debugctl = data; 2154 2155 vmcs_write64(GUEST_IA32_DEBUGCTL, data); 2156 if (intel_pmu_lbr_is_enabled(vcpu) && !to_vmx(vcpu)->lbr_desc.event && 2157 (data & DEBUGCTLMSR_LBR)) 2158 intel_pmu_create_guest_lbr_event(vcpu); 2159 return 0; 2160 } 2161 case MSR_IA32_BNDCFGS: 2162 if (!kvm_mpx_supported() || 2163 (!msr_info->host_initiated && 2164 !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) 2165 return 1; 2166 if (is_noncanonical_address(data & PAGE_MASK, vcpu) || 2167 (data & MSR_IA32_BNDCFGS_RSVD)) 2168 return 1; 2169 2170 if (is_guest_mode(vcpu) && 2171 ((vmx->nested.msrs.entry_ctls_high & VM_ENTRY_LOAD_BNDCFGS) || 2172 (vmx->nested.msrs.exit_ctls_high & VM_EXIT_CLEAR_BNDCFGS))) 2173 get_vmcs12(vcpu)->guest_bndcfgs = data; 2174 2175 vmcs_write64(GUEST_BNDCFGS, data); 2176 break; 2177 case MSR_IA32_UMWAIT_CONTROL: 2178 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx)) 2179 return 1; 2180 2181 /* The reserved bit 1 and non-32 bit [63:32] should be zero */ 2182 if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32))) 2183 return 1; 2184 2185 vmx->msr_ia32_umwait_control = data; 2186 break; 2187 case MSR_IA32_SPEC_CTRL: 2188 if (!msr_info->host_initiated && 2189 !guest_has_spec_ctrl_msr(vcpu)) 2190 return 1; 2191 2192 if (kvm_spec_ctrl_test_value(data)) 2193 return 1; 2194 2195 vmx->spec_ctrl = data; 2196 if (!data) 2197 break; 2198 2199 /* 2200 * For non-nested: 2201 * When it's written (to non-zero) for the first time, pass 2202 * it through. 2203 * 2204 * For nested: 2205 * The handling of the MSR bitmap for L2 guests is done in 2206 * nested_vmx_prepare_msr_bitmap. We should not touch the 2207 * vmcs02.msr_bitmap here since it gets completely overwritten 2208 * in the merging. We update the vmcs01 here for L1 as well 2209 * since it will end up touching the MSR anyway now. 2210 */ 2211 vmx_disable_intercept_for_msr(vcpu, 2212 MSR_IA32_SPEC_CTRL, 2213 MSR_TYPE_RW); 2214 break; 2215 case MSR_IA32_TSX_CTRL: 2216 if (!msr_info->host_initiated && 2217 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR)) 2218 return 1; 2219 if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR)) 2220 return 1; 2221 goto find_uret_msr; 2222 case MSR_IA32_PRED_CMD: 2223 if (!msr_info->host_initiated && 2224 !guest_has_pred_cmd_msr(vcpu)) 2225 return 1; 2226 2227 if (data & ~PRED_CMD_IBPB) 2228 return 1; 2229 if (!boot_cpu_has(X86_FEATURE_IBPB)) 2230 return 1; 2231 if (!data) 2232 break; 2233 2234 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); 2235 2236 /* 2237 * For non-nested: 2238 * When it's written (to non-zero) for the first time, pass 2239 * it through. 2240 * 2241 * For nested: 2242 * The handling of the MSR bitmap for L2 guests is done in 2243 * nested_vmx_prepare_msr_bitmap. We should not touch the 2244 * vmcs02.msr_bitmap here since it gets completely overwritten 2245 * in the merging. 2246 */ 2247 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W); 2248 break; 2249 case MSR_IA32_CR_PAT: 2250 if (!kvm_pat_valid(data)) 2251 return 1; 2252 2253 if (is_guest_mode(vcpu) && 2254 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) 2255 get_vmcs12(vcpu)->guest_ia32_pat = data; 2256 2257 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { 2258 vmcs_write64(GUEST_IA32_PAT, data); 2259 vcpu->arch.pat = data; 2260 break; 2261 } 2262 ret = kvm_set_msr_common(vcpu, msr_info); 2263 break; 2264 case MSR_IA32_MCG_EXT_CTL: 2265 if ((!msr_info->host_initiated && 2266 !(to_vmx(vcpu)->msr_ia32_feature_control & 2267 FEAT_CTL_LMCE_ENABLED)) || 2268 (data & ~MCG_EXT_CTL_LMCE_EN)) 2269 return 1; 2270 vcpu->arch.mcg_ext_ctl = data; 2271 break; 2272 case MSR_IA32_FEAT_CTL: 2273 if (!is_vmx_feature_control_msr_valid(vmx, msr_info)) 2274 return 1; 2275 2276 vmx->msr_ia32_feature_control = data; 2277 if (msr_info->host_initiated && data == 0) 2278 vmx_leave_nested(vcpu); 2279 2280 /* SGX may be enabled/disabled by guest's firmware */ 2281 vmx_write_encls_bitmap(vcpu, NULL); 2282 break; 2283 case MSR_IA32_SGXLEPUBKEYHASH0 ... MSR_IA32_SGXLEPUBKEYHASH3: 2284 /* 2285 * On real hardware, the LE hash MSRs are writable before 2286 * the firmware sets bit 0 in MSR 0x7a ("activating" SGX), 2287 * at which point SGX related bits in IA32_FEATURE_CONTROL 2288 * become writable. 2289 * 2290 * KVM does not emulate SGX activation for simplicity, so 2291 * allow writes to the LE hash MSRs if IA32_FEATURE_CONTROL 2292 * is unlocked. This is technically not architectural 2293 * behavior, but it's close enough. 2294 */ 2295 if (!msr_info->host_initiated && 2296 (!guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC) || 2297 ((vmx->msr_ia32_feature_control & FEAT_CTL_LOCKED) && 2298 !(vmx->msr_ia32_feature_control & FEAT_CTL_SGX_LC_ENABLED)))) 2299 return 1; 2300 vmx->msr_ia32_sgxlepubkeyhash 2301 [msr_index - MSR_IA32_SGXLEPUBKEYHASH0] = data; 2302 break; 2303 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: 2304 if (!msr_info->host_initiated) 2305 return 1; /* they are read-only */ 2306 if (!nested_vmx_allowed(vcpu)) 2307 return 1; 2308 return vmx_set_vmx_msr(vcpu, msr_index, data); 2309 case MSR_IA32_RTIT_CTL: 2310 if (!vmx_pt_mode_is_host_guest() || 2311 vmx_rtit_ctl_check(vcpu, data) || 2312 vmx->nested.vmxon) 2313 return 1; 2314 vmcs_write64(GUEST_IA32_RTIT_CTL, data); 2315 vmx->pt_desc.guest.ctl = data; 2316 pt_update_intercept_for_msr(vcpu); 2317 break; 2318 case MSR_IA32_RTIT_STATUS: 2319 if (!pt_can_write_msr(vmx)) 2320 return 1; 2321 if (data & MSR_IA32_RTIT_STATUS_MASK) 2322 return 1; 2323 vmx->pt_desc.guest.status = data; 2324 break; 2325 case MSR_IA32_RTIT_CR3_MATCH: 2326 if (!pt_can_write_msr(vmx)) 2327 return 1; 2328 if (!intel_pt_validate_cap(vmx->pt_desc.caps, 2329 PT_CAP_cr3_filtering)) 2330 return 1; 2331 vmx->pt_desc.guest.cr3_match = data; 2332 break; 2333 case MSR_IA32_RTIT_OUTPUT_BASE: 2334 if (!pt_can_write_msr(vmx)) 2335 return 1; 2336 if (!intel_pt_validate_cap(vmx->pt_desc.caps, 2337 PT_CAP_topa_output) && 2338 !intel_pt_validate_cap(vmx->pt_desc.caps, 2339 PT_CAP_single_range_output)) 2340 return 1; 2341 if (!pt_output_base_valid(vcpu, data)) 2342 return 1; 2343 vmx->pt_desc.guest.output_base = data; 2344 break; 2345 case MSR_IA32_RTIT_OUTPUT_MASK: 2346 if (!pt_can_write_msr(vmx)) 2347 return 1; 2348 if (!intel_pt_validate_cap(vmx->pt_desc.caps, 2349 PT_CAP_topa_output) && 2350 !intel_pt_validate_cap(vmx->pt_desc.caps, 2351 PT_CAP_single_range_output)) 2352 return 1; 2353 vmx->pt_desc.guest.output_mask = data; 2354 break; 2355 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: 2356 if (!pt_can_write_msr(vmx)) 2357 return 1; 2358 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; 2359 if (index >= 2 * vmx->pt_desc.num_address_ranges) 2360 return 1; 2361 if (is_noncanonical_address(data, vcpu)) 2362 return 1; 2363 if (index % 2) 2364 vmx->pt_desc.guest.addr_b[index / 2] = data; 2365 else 2366 vmx->pt_desc.guest.addr_a[index / 2] = data; 2367 break; 2368 case MSR_IA32_PERF_CAPABILITIES: 2369 if (data && !vcpu_to_pmu(vcpu)->version) 2370 return 1; 2371 if (data & PMU_CAP_LBR_FMT) { 2372 if ((data & PMU_CAP_LBR_FMT) != 2373 (kvm_caps.supported_perf_cap & PMU_CAP_LBR_FMT)) 2374 return 1; 2375 if (!cpuid_model_is_consistent(vcpu)) 2376 return 1; 2377 } 2378 if (data & PERF_CAP_PEBS_FORMAT) { 2379 if ((data & PERF_CAP_PEBS_MASK) != 2380 (kvm_caps.supported_perf_cap & PERF_CAP_PEBS_MASK)) 2381 return 1; 2382 if (!guest_cpuid_has(vcpu, X86_FEATURE_DS)) 2383 return 1; 2384 if (!guest_cpuid_has(vcpu, X86_FEATURE_DTES64)) 2385 return 1; 2386 if (!cpuid_model_is_consistent(vcpu)) 2387 return 1; 2388 } 2389 ret = kvm_set_msr_common(vcpu, msr_info); 2390 break; 2391 2392 default: 2393 find_uret_msr: 2394 msr = vmx_find_uret_msr(vmx, msr_index); 2395 if (msr) 2396 ret = vmx_set_guest_uret_msr(vmx, msr, data); 2397 else 2398 ret = kvm_set_msr_common(vcpu, msr_info); 2399 } 2400 2401 /* FB_CLEAR may have changed, also update the FB_CLEAR_DIS behavior */ 2402 if (msr_index == MSR_IA32_ARCH_CAPABILITIES) 2403 vmx_update_fb_clear_dis(vcpu, vmx); 2404 2405 return ret; 2406 } 2407 2408 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) 2409 { 2410 unsigned long guest_owned_bits; 2411 2412 kvm_register_mark_available(vcpu, reg); 2413 2414 switch (reg) { 2415 case VCPU_REGS_RSP: 2416 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); 2417 break; 2418 case VCPU_REGS_RIP: 2419 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); 2420 break; 2421 case VCPU_EXREG_PDPTR: 2422 if (enable_ept) 2423 ept_save_pdptrs(vcpu); 2424 break; 2425 case VCPU_EXREG_CR0: 2426 guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; 2427 2428 vcpu->arch.cr0 &= ~guest_owned_bits; 2429 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits; 2430 break; 2431 case VCPU_EXREG_CR3: 2432 /* 2433 * When intercepting CR3 loads, e.g. for shadowing paging, KVM's 2434 * CR3 is loaded into hardware, not the guest's CR3. 2435 */ 2436 if (!(exec_controls_get(to_vmx(vcpu)) & CPU_BASED_CR3_LOAD_EXITING)) 2437 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); 2438 break; 2439 case VCPU_EXREG_CR4: 2440 guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; 2441 2442 vcpu->arch.cr4 &= ~guest_owned_bits; 2443 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits; 2444 break; 2445 default: 2446 KVM_BUG_ON(1, vcpu->kvm); 2447 break; 2448 } 2449 } 2450 2451 static __init int cpu_has_kvm_support(void) 2452 { 2453 return cpu_has_vmx(); 2454 } 2455 2456 static __init int vmx_disabled_by_bios(void) 2457 { 2458 return !boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) || 2459 !boot_cpu_has(X86_FEATURE_VMX); 2460 } 2461 2462 static int kvm_cpu_vmxon(u64 vmxon_pointer) 2463 { 2464 u64 msr; 2465 2466 cr4_set_bits(X86_CR4_VMXE); 2467 2468 asm_volatile_goto("1: vmxon %[vmxon_pointer]\n\t" 2469 _ASM_EXTABLE(1b, %l[fault]) 2470 : : [vmxon_pointer] "m"(vmxon_pointer) 2471 : : fault); 2472 return 0; 2473 2474 fault: 2475 WARN_ONCE(1, "VMXON faulted, MSR_IA32_FEAT_CTL (0x3a) = 0x%llx\n", 2476 rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr) ? 0xdeadbeef : msr); 2477 cr4_clear_bits(X86_CR4_VMXE); 2478 2479 return -EFAULT; 2480 } 2481 2482 static int vmx_hardware_enable(void) 2483 { 2484 int cpu = raw_smp_processor_id(); 2485 u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); 2486 int r; 2487 2488 if (cr4_read_shadow() & X86_CR4_VMXE) 2489 return -EBUSY; 2490 2491 /* 2492 * This can happen if we hot-added a CPU but failed to allocate 2493 * VP assist page for it. 2494 */ 2495 if (static_branch_unlikely(&enable_evmcs) && 2496 !hv_get_vp_assist_page(cpu)) 2497 return -EFAULT; 2498 2499 intel_pt_handle_vmx(1); 2500 2501 r = kvm_cpu_vmxon(phys_addr); 2502 if (r) { 2503 intel_pt_handle_vmx(0); 2504 return r; 2505 } 2506 2507 if (enable_ept) 2508 ept_sync_global(); 2509 2510 return 0; 2511 } 2512 2513 static void vmclear_local_loaded_vmcss(void) 2514 { 2515 int cpu = raw_smp_processor_id(); 2516 struct loaded_vmcs *v, *n; 2517 2518 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), 2519 loaded_vmcss_on_cpu_link) 2520 __loaded_vmcs_clear(v); 2521 } 2522 2523 static void vmx_hardware_disable(void) 2524 { 2525 vmclear_local_loaded_vmcss(); 2526 2527 if (cpu_vmxoff()) 2528 kvm_spurious_fault(); 2529 2530 intel_pt_handle_vmx(0); 2531 } 2532 2533 /* 2534 * There is no X86_FEATURE for SGX yet, but anyway we need to query CPUID 2535 * directly instead of going through cpu_has(), to ensure KVM is trapping 2536 * ENCLS whenever it's supported in hardware. It does not matter whether 2537 * the host OS supports or has enabled SGX. 2538 */ 2539 static bool cpu_has_sgx(void) 2540 { 2541 return cpuid_eax(0) >= 0x12 && (cpuid_eax(0x12) & BIT(0)); 2542 } 2543 2544 /* 2545 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they 2546 * can't be used due to errata where VM Exit may incorrectly clear 2547 * IA32_PERF_GLOBAL_CTRL[34:32]. Work around the errata by using the 2548 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL. 2549 */ 2550 static bool cpu_has_perf_global_ctrl_bug(void) 2551 { 2552 if (boot_cpu_data.x86 == 0x6) { 2553 switch (boot_cpu_data.x86_model) { 2554 case INTEL_FAM6_NEHALEM_EP: /* AAK155 */ 2555 case INTEL_FAM6_NEHALEM: /* AAP115 */ 2556 case INTEL_FAM6_WESTMERE: /* AAT100 */ 2557 case INTEL_FAM6_WESTMERE_EP: /* BC86,AAY89,BD102 */ 2558 case INTEL_FAM6_NEHALEM_EX: /* BA97 */ 2559 return true; 2560 default: 2561 break; 2562 } 2563 } 2564 2565 return false; 2566 } 2567 2568 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, 2569 u32 msr, u32 *result) 2570 { 2571 u32 vmx_msr_low, vmx_msr_high; 2572 u32 ctl = ctl_min | ctl_opt; 2573 2574 rdmsr(msr, vmx_msr_low, vmx_msr_high); 2575 2576 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ 2577 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ 2578 2579 /* Ensure minimum (required) set of control bits are supported. */ 2580 if (ctl_min & ~ctl) 2581 return -EIO; 2582 2583 *result = ctl; 2584 return 0; 2585 } 2586 2587 static __init u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) 2588 { 2589 u64 allowed; 2590 2591 rdmsrl(msr, allowed); 2592 2593 return ctl_opt & allowed; 2594 } 2595 2596 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, 2597 struct vmx_capability *vmx_cap) 2598 { 2599 u32 vmx_msr_low, vmx_msr_high; 2600 u32 _pin_based_exec_control = 0; 2601 u32 _cpu_based_exec_control = 0; 2602 u32 _cpu_based_2nd_exec_control = 0; 2603 u64 _cpu_based_3rd_exec_control = 0; 2604 u32 _vmexit_control = 0; 2605 u32 _vmentry_control = 0; 2606 u64 misc_msr; 2607 int i; 2608 2609 /* 2610 * LOAD/SAVE_DEBUG_CONTROLS are absent because both are mandatory. 2611 * SAVE_IA32_PAT and SAVE_IA32_EFER are absent because KVM always 2612 * intercepts writes to PAT and EFER, i.e. never enables those controls. 2613 */ 2614 struct { 2615 u32 entry_control; 2616 u32 exit_control; 2617 } const vmcs_entry_exit_pairs[] = { 2618 { VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL }, 2619 { VM_ENTRY_LOAD_IA32_PAT, VM_EXIT_LOAD_IA32_PAT }, 2620 { VM_ENTRY_LOAD_IA32_EFER, VM_EXIT_LOAD_IA32_EFER }, 2621 { VM_ENTRY_LOAD_BNDCFGS, VM_EXIT_CLEAR_BNDCFGS }, 2622 { VM_ENTRY_LOAD_IA32_RTIT_CTL, VM_EXIT_CLEAR_IA32_RTIT_CTL }, 2623 }; 2624 2625 memset(vmcs_conf, 0, sizeof(*vmcs_conf)); 2626 2627 if (adjust_vmx_controls(KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL, 2628 KVM_OPTIONAL_VMX_CPU_BASED_VM_EXEC_CONTROL, 2629 MSR_IA32_VMX_PROCBASED_CTLS, 2630 &_cpu_based_exec_control)) 2631 return -EIO; 2632 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { 2633 if (adjust_vmx_controls(KVM_REQUIRED_VMX_SECONDARY_VM_EXEC_CONTROL, 2634 KVM_OPTIONAL_VMX_SECONDARY_VM_EXEC_CONTROL, 2635 MSR_IA32_VMX_PROCBASED_CTLS2, 2636 &_cpu_based_2nd_exec_control)) 2637 return -EIO; 2638 } 2639 #ifndef CONFIG_X86_64 2640 if (!(_cpu_based_2nd_exec_control & 2641 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) 2642 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; 2643 #endif 2644 2645 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) 2646 _cpu_based_2nd_exec_control &= ~( 2647 SECONDARY_EXEC_APIC_REGISTER_VIRT | 2648 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | 2649 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); 2650 2651 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, 2652 &vmx_cap->ept, &vmx_cap->vpid); 2653 2654 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) && 2655 vmx_cap->ept) { 2656 pr_warn_once("EPT CAP should not exist if not support " 2657 "1-setting enable EPT VM-execution control\n"); 2658 2659 if (error_on_inconsistent_vmcs_config) 2660 return -EIO; 2661 2662 vmx_cap->ept = 0; 2663 } 2664 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && 2665 vmx_cap->vpid) { 2666 pr_warn_once("VPID CAP should not exist if not support " 2667 "1-setting enable VPID VM-execution control\n"); 2668 2669 if (error_on_inconsistent_vmcs_config) 2670 return -EIO; 2671 2672 vmx_cap->vpid = 0; 2673 } 2674 2675 if (!cpu_has_sgx()) 2676 _cpu_based_2nd_exec_control &= ~SECONDARY_EXEC_ENCLS_EXITING; 2677 2678 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_TERTIARY_CONTROLS) 2679 _cpu_based_3rd_exec_control = 2680 adjust_vmx_controls64(KVM_OPTIONAL_VMX_TERTIARY_VM_EXEC_CONTROL, 2681 MSR_IA32_VMX_PROCBASED_CTLS3); 2682 2683 if (adjust_vmx_controls(KVM_REQUIRED_VMX_VM_EXIT_CONTROLS, 2684 KVM_OPTIONAL_VMX_VM_EXIT_CONTROLS, 2685 MSR_IA32_VMX_EXIT_CTLS, 2686 &_vmexit_control)) 2687 return -EIO; 2688 2689 if (adjust_vmx_controls(KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL, 2690 KVM_OPTIONAL_VMX_PIN_BASED_VM_EXEC_CONTROL, 2691 MSR_IA32_VMX_PINBASED_CTLS, 2692 &_pin_based_exec_control)) 2693 return -EIO; 2694 2695 if (cpu_has_broken_vmx_preemption_timer()) 2696 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; 2697 if (!(_cpu_based_2nd_exec_control & 2698 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) 2699 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; 2700 2701 if (adjust_vmx_controls(KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS, 2702 KVM_OPTIONAL_VMX_VM_ENTRY_CONTROLS, 2703 MSR_IA32_VMX_ENTRY_CTLS, 2704 &_vmentry_control)) 2705 return -EIO; 2706 2707 for (i = 0; i < ARRAY_SIZE(vmcs_entry_exit_pairs); i++) { 2708 u32 n_ctrl = vmcs_entry_exit_pairs[i].entry_control; 2709 u32 x_ctrl = vmcs_entry_exit_pairs[i].exit_control; 2710 2711 if (!(_vmentry_control & n_ctrl) == !(_vmexit_control & x_ctrl)) 2712 continue; 2713 2714 pr_warn_once("Inconsistent VM-Entry/VM-Exit pair, entry = %x, exit = %x\n", 2715 _vmentry_control & n_ctrl, _vmexit_control & x_ctrl); 2716 2717 if (error_on_inconsistent_vmcs_config) 2718 return -EIO; 2719 2720 _vmentry_control &= ~n_ctrl; 2721 _vmexit_control &= ~x_ctrl; 2722 } 2723 2724 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); 2725 2726 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ 2727 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) 2728 return -EIO; 2729 2730 #ifdef CONFIG_X86_64 2731 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ 2732 if (vmx_msr_high & (1u<<16)) 2733 return -EIO; 2734 #endif 2735 2736 /* Require Write-Back (WB) memory type for VMCS accesses. */ 2737 if (((vmx_msr_high >> 18) & 15) != 6) 2738 return -EIO; 2739 2740 rdmsrl(MSR_IA32_VMX_MISC, misc_msr); 2741 2742 vmcs_conf->size = vmx_msr_high & 0x1fff; 2743 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; 2744 2745 vmcs_conf->revision_id = vmx_msr_low; 2746 2747 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; 2748 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; 2749 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; 2750 vmcs_conf->cpu_based_3rd_exec_ctrl = _cpu_based_3rd_exec_control; 2751 vmcs_conf->vmexit_ctrl = _vmexit_control; 2752 vmcs_conf->vmentry_ctrl = _vmentry_control; 2753 vmcs_conf->misc = misc_msr; 2754 2755 return 0; 2756 } 2757 2758 struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags) 2759 { 2760 int node = cpu_to_node(cpu); 2761 struct page *pages; 2762 struct vmcs *vmcs; 2763 2764 pages = __alloc_pages_node(node, flags, 0); 2765 if (!pages) 2766 return NULL; 2767 vmcs = page_address(pages); 2768 memset(vmcs, 0, vmcs_config.size); 2769 2770 /* KVM supports Enlightened VMCS v1 only */ 2771 if (static_branch_unlikely(&enable_evmcs)) 2772 vmcs->hdr.revision_id = KVM_EVMCS_VERSION; 2773 else 2774 vmcs->hdr.revision_id = vmcs_config.revision_id; 2775 2776 if (shadow) 2777 vmcs->hdr.shadow_vmcs = 1; 2778 return vmcs; 2779 } 2780 2781 void free_vmcs(struct vmcs *vmcs) 2782 { 2783 free_page((unsigned long)vmcs); 2784 } 2785 2786 /* 2787 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded 2788 */ 2789 void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) 2790 { 2791 if (!loaded_vmcs->vmcs) 2792 return; 2793 loaded_vmcs_clear(loaded_vmcs); 2794 free_vmcs(loaded_vmcs->vmcs); 2795 loaded_vmcs->vmcs = NULL; 2796 if (loaded_vmcs->msr_bitmap) 2797 free_page((unsigned long)loaded_vmcs->msr_bitmap); 2798 WARN_ON(loaded_vmcs->shadow_vmcs != NULL); 2799 } 2800 2801 int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) 2802 { 2803 loaded_vmcs->vmcs = alloc_vmcs(false); 2804 if (!loaded_vmcs->vmcs) 2805 return -ENOMEM; 2806 2807 vmcs_clear(loaded_vmcs->vmcs); 2808 2809 loaded_vmcs->shadow_vmcs = NULL; 2810 loaded_vmcs->hv_timer_soft_disabled = false; 2811 loaded_vmcs->cpu = -1; 2812 loaded_vmcs->launched = 0; 2813 2814 if (cpu_has_vmx_msr_bitmap()) { 2815 loaded_vmcs->msr_bitmap = (unsigned long *) 2816 __get_free_page(GFP_KERNEL_ACCOUNT); 2817 if (!loaded_vmcs->msr_bitmap) 2818 goto out_vmcs; 2819 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); 2820 } 2821 2822 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state)); 2823 memset(&loaded_vmcs->controls_shadow, 0, 2824 sizeof(struct vmcs_controls_shadow)); 2825 2826 return 0; 2827 2828 out_vmcs: 2829 free_loaded_vmcs(loaded_vmcs); 2830 return -ENOMEM; 2831 } 2832 2833 static void free_kvm_area(void) 2834 { 2835 int cpu; 2836 2837 for_each_possible_cpu(cpu) { 2838 free_vmcs(per_cpu(vmxarea, cpu)); 2839 per_cpu(vmxarea, cpu) = NULL; 2840 } 2841 } 2842 2843 static __init int alloc_kvm_area(void) 2844 { 2845 int cpu; 2846 2847 for_each_possible_cpu(cpu) { 2848 struct vmcs *vmcs; 2849 2850 vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL); 2851 if (!vmcs) { 2852 free_kvm_area(); 2853 return -ENOMEM; 2854 } 2855 2856 /* 2857 * When eVMCS is enabled, alloc_vmcs_cpu() sets 2858 * vmcs->revision_id to KVM_EVMCS_VERSION instead of 2859 * revision_id reported by MSR_IA32_VMX_BASIC. 2860 * 2861 * However, even though not explicitly documented by 2862 * TLFS, VMXArea passed as VMXON argument should 2863 * still be marked with revision_id reported by 2864 * physical CPU. 2865 */ 2866 if (static_branch_unlikely(&enable_evmcs)) 2867 vmcs->hdr.revision_id = vmcs_config.revision_id; 2868 2869 per_cpu(vmxarea, cpu) = vmcs; 2870 } 2871 return 0; 2872 } 2873 2874 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, 2875 struct kvm_segment *save) 2876 { 2877 if (!emulate_invalid_guest_state) { 2878 /* 2879 * CS and SS RPL should be equal during guest entry according 2880 * to VMX spec, but in reality it is not always so. Since vcpu 2881 * is in the middle of the transition from real mode to 2882 * protected mode it is safe to assume that RPL 0 is a good 2883 * default value. 2884 */ 2885 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) 2886 save->selector &= ~SEGMENT_RPL_MASK; 2887 save->dpl = save->selector & SEGMENT_RPL_MASK; 2888 save->s = 1; 2889 } 2890 __vmx_set_segment(vcpu, save, seg); 2891 } 2892 2893 static void enter_pmode(struct kvm_vcpu *vcpu) 2894 { 2895 unsigned long flags; 2896 struct vcpu_vmx *vmx = to_vmx(vcpu); 2897 2898 /* 2899 * Update real mode segment cache. It may be not up-to-date if segment 2900 * register was written while vcpu was in a guest mode. 2901 */ 2902 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); 2903 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); 2904 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); 2905 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); 2906 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); 2907 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); 2908 2909 vmx->rmode.vm86_active = 0; 2910 2911 __vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); 2912 2913 flags = vmcs_readl(GUEST_RFLAGS); 2914 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; 2915 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; 2916 vmcs_writel(GUEST_RFLAGS, flags); 2917 2918 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | 2919 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); 2920 2921 vmx_update_exception_bitmap(vcpu); 2922 2923 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); 2924 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); 2925 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); 2926 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); 2927 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); 2928 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); 2929 } 2930 2931 static void fix_rmode_seg(int seg, struct kvm_segment *save) 2932 { 2933 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; 2934 struct kvm_segment var = *save; 2935 2936 var.dpl = 0x3; 2937 if (seg == VCPU_SREG_CS) 2938 var.type = 0x3; 2939 2940 if (!emulate_invalid_guest_state) { 2941 var.selector = var.base >> 4; 2942 var.base = var.base & 0xffff0; 2943 var.limit = 0xffff; 2944 var.g = 0; 2945 var.db = 0; 2946 var.present = 1; 2947 var.s = 1; 2948 var.l = 0; 2949 var.unusable = 0; 2950 var.type = 0x3; 2951 var.avl = 0; 2952 if (save->base & 0xf) 2953 printk_once(KERN_WARNING "kvm: segment base is not " 2954 "paragraph aligned when entering " 2955 "protected mode (seg=%d)", seg); 2956 } 2957 2958 vmcs_write16(sf->selector, var.selector); 2959 vmcs_writel(sf->base, var.base); 2960 vmcs_write32(sf->limit, var.limit); 2961 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); 2962 } 2963 2964 static void enter_rmode(struct kvm_vcpu *vcpu) 2965 { 2966 unsigned long flags; 2967 struct vcpu_vmx *vmx = to_vmx(vcpu); 2968 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); 2969 2970 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); 2971 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); 2972 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); 2973 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); 2974 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); 2975 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); 2976 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); 2977 2978 vmx->rmode.vm86_active = 1; 2979 2980 /* 2981 * Very old userspace does not call KVM_SET_TSS_ADDR before entering 2982 * vcpu. Warn the user that an update is overdue. 2983 */ 2984 if (!kvm_vmx->tss_addr) 2985 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " 2986 "called before entering vcpu\n"); 2987 2988 vmx_segment_cache_clear(vmx); 2989 2990 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); 2991 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); 2992 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); 2993 2994 flags = vmcs_readl(GUEST_RFLAGS); 2995 vmx->rmode.save_rflags = flags; 2996 2997 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; 2998 2999 vmcs_writel(GUEST_RFLAGS, flags); 3000 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); 3001 vmx_update_exception_bitmap(vcpu); 3002 3003 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); 3004 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); 3005 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); 3006 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); 3007 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); 3008 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); 3009 } 3010 3011 int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) 3012 { 3013 struct vcpu_vmx *vmx = to_vmx(vcpu); 3014 3015 /* Nothing to do if hardware doesn't support EFER. */ 3016 if (!vmx_find_uret_msr(vmx, MSR_EFER)) 3017 return 0; 3018 3019 vcpu->arch.efer = efer; 3020 #ifdef CONFIG_X86_64 3021 if (efer & EFER_LMA) 3022 vm_entry_controls_setbit(vmx, VM_ENTRY_IA32E_MODE); 3023 else 3024 vm_entry_controls_clearbit(vmx, VM_ENTRY_IA32E_MODE); 3025 #else 3026 if (KVM_BUG_ON(efer & EFER_LMA, vcpu->kvm)) 3027 return 1; 3028 #endif 3029 3030 vmx_setup_uret_msrs(vmx); 3031 return 0; 3032 } 3033 3034 #ifdef CONFIG_X86_64 3035 3036 static void enter_lmode(struct kvm_vcpu *vcpu) 3037 { 3038 u32 guest_tr_ar; 3039 3040 vmx_segment_cache_clear(to_vmx(vcpu)); 3041 3042 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); 3043 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) { 3044 pr_debug_ratelimited("%s: tss fixup for long mode. \n", 3045 __func__); 3046 vmcs_write32(GUEST_TR_AR_BYTES, 3047 (guest_tr_ar & ~VMX_AR_TYPE_MASK) 3048 | VMX_AR_TYPE_BUSY_64_TSS); 3049 } 3050 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); 3051 } 3052 3053 static void exit_lmode(struct kvm_vcpu *vcpu) 3054 { 3055 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); 3056 } 3057 3058 #endif 3059 3060 static void vmx_flush_tlb_all(struct kvm_vcpu *vcpu) 3061 { 3062 struct vcpu_vmx *vmx = to_vmx(vcpu); 3063 3064 /* 3065 * INVEPT must be issued when EPT is enabled, irrespective of VPID, as 3066 * the CPU is not required to invalidate guest-physical mappings on 3067 * VM-Entry, even if VPID is disabled. Guest-physical mappings are 3068 * associated with the root EPT structure and not any particular VPID 3069 * (INVVPID also isn't required to invalidate guest-physical mappings). 3070 */ 3071 if (enable_ept) { 3072 ept_sync_global(); 3073 } else if (enable_vpid) { 3074 if (cpu_has_vmx_invvpid_global()) { 3075 vpid_sync_vcpu_global(); 3076 } else { 3077 vpid_sync_vcpu_single(vmx->vpid); 3078 vpid_sync_vcpu_single(vmx->nested.vpid02); 3079 } 3080 } 3081 } 3082 3083 static inline int vmx_get_current_vpid(struct kvm_vcpu *vcpu) 3084 { 3085 if (is_guest_mode(vcpu)) 3086 return nested_get_vpid02(vcpu); 3087 return to_vmx(vcpu)->vpid; 3088 } 3089 3090 static void vmx_flush_tlb_current(struct kvm_vcpu *vcpu) 3091 { 3092 struct kvm_mmu *mmu = vcpu->arch.mmu; 3093 u64 root_hpa = mmu->root.hpa; 3094 3095 /* No flush required if the current context is invalid. */ 3096 if (!VALID_PAGE(root_hpa)) 3097 return; 3098 3099 if (enable_ept) 3100 ept_sync_context(construct_eptp(vcpu, root_hpa, 3101 mmu->root_role.level)); 3102 else 3103 vpid_sync_context(vmx_get_current_vpid(vcpu)); 3104 } 3105 3106 static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr) 3107 { 3108 /* 3109 * vpid_sync_vcpu_addr() is a nop if vpid==0, see the comment in 3110 * vmx_flush_tlb_guest() for an explanation of why this is ok. 3111 */ 3112 vpid_sync_vcpu_addr(vmx_get_current_vpid(vcpu), addr); 3113 } 3114 3115 static void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu) 3116 { 3117 /* 3118 * vpid_sync_context() is a nop if vpid==0, e.g. if enable_vpid==0 or a 3119 * vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit are 3120 * required to flush GVA->{G,H}PA mappings from the TLB if vpid is 3121 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed), 3122 * i.e. no explicit INVVPID is necessary. 3123 */ 3124 vpid_sync_context(vmx_get_current_vpid(vcpu)); 3125 } 3126 3127 void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu) 3128 { 3129 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; 3130 3131 if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR)) 3132 return; 3133 3134 if (is_pae_paging(vcpu)) { 3135 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); 3136 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); 3137 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); 3138 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); 3139 } 3140 } 3141 3142 void ept_save_pdptrs(struct kvm_vcpu *vcpu) 3143 { 3144 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; 3145 3146 if (WARN_ON_ONCE(!is_pae_paging(vcpu))) 3147 return; 3148 3149 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); 3150 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); 3151 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); 3152 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); 3153 3154 kvm_register_mark_available(vcpu, VCPU_EXREG_PDPTR); 3155 } 3156 3157 #define CR3_EXITING_BITS (CPU_BASED_CR3_LOAD_EXITING | \ 3158 CPU_BASED_CR3_STORE_EXITING) 3159 3160 void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) 3161 { 3162 struct vcpu_vmx *vmx = to_vmx(vcpu); 3163 unsigned long hw_cr0, old_cr0_pg; 3164 u32 tmp; 3165 3166 old_cr0_pg = kvm_read_cr0_bits(vcpu, X86_CR0_PG); 3167 3168 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF); 3169 if (is_unrestricted_guest(vcpu)) 3170 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; 3171 else { 3172 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; 3173 if (!enable_ept) 3174 hw_cr0 |= X86_CR0_WP; 3175 3176 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) 3177 enter_pmode(vcpu); 3178 3179 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) 3180 enter_rmode(vcpu); 3181 } 3182 3183 vmcs_writel(CR0_READ_SHADOW, cr0); 3184 vmcs_writel(GUEST_CR0, hw_cr0); 3185 vcpu->arch.cr0 = cr0; 3186 kvm_register_mark_available(vcpu, VCPU_EXREG_CR0); 3187 3188 #ifdef CONFIG_X86_64 3189 if (vcpu->arch.efer & EFER_LME) { 3190 if (!old_cr0_pg && (cr0 & X86_CR0_PG)) 3191 enter_lmode(vcpu); 3192 else if (old_cr0_pg && !(cr0 & X86_CR0_PG)) 3193 exit_lmode(vcpu); 3194 } 3195 #endif 3196 3197 if (enable_ept && !is_unrestricted_guest(vcpu)) { 3198 /* 3199 * Ensure KVM has an up-to-date snapshot of the guest's CR3. If 3200 * the below code _enables_ CR3 exiting, vmx_cache_reg() will 3201 * (correctly) stop reading vmcs.GUEST_CR3 because it thinks 3202 * KVM's CR3 is installed. 3203 */ 3204 if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3)) 3205 vmx_cache_reg(vcpu, VCPU_EXREG_CR3); 3206 3207 /* 3208 * When running with EPT but not unrestricted guest, KVM must 3209 * intercept CR3 accesses when paging is _disabled_. This is 3210 * necessary because restricted guests can't actually run with 3211 * paging disabled, and so KVM stuffs its own CR3 in order to 3212 * run the guest when identity mapped page tables. 3213 * 3214 * Do _NOT_ check the old CR0.PG, e.g. to optimize away the 3215 * update, it may be stale with respect to CR3 interception, 3216 * e.g. after nested VM-Enter. 3217 * 3218 * Lastly, honor L1's desires, i.e. intercept CR3 loads and/or 3219 * stores to forward them to L1, even if KVM does not need to 3220 * intercept them to preserve its identity mapped page tables. 3221 */ 3222 if (!(cr0 & X86_CR0_PG)) { 3223 exec_controls_setbit(vmx, CR3_EXITING_BITS); 3224 } else if (!is_guest_mode(vcpu)) { 3225 exec_controls_clearbit(vmx, CR3_EXITING_BITS); 3226 } else { 3227 tmp = exec_controls_get(vmx); 3228 tmp &= ~CR3_EXITING_BITS; 3229 tmp |= get_vmcs12(vcpu)->cpu_based_vm_exec_control & CR3_EXITING_BITS; 3230 exec_controls_set(vmx, tmp); 3231 } 3232 3233 /* Note, vmx_set_cr4() consumes the new vcpu->arch.cr0. */ 3234 if ((old_cr0_pg ^ cr0) & X86_CR0_PG) 3235 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); 3236 3237 /* 3238 * When !CR0_PG -> CR0_PG, vcpu->arch.cr3 becomes active, but 3239 * GUEST_CR3 is still vmx->ept_identity_map_addr if EPT + !URG. 3240 */ 3241 if (!(old_cr0_pg & X86_CR0_PG) && (cr0 & X86_CR0_PG)) 3242 kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); 3243 } 3244 3245 /* depends on vcpu->arch.cr0 to be set to a new value */ 3246 vmx->emulation_required = vmx_emulation_required(vcpu); 3247 } 3248 3249 static int vmx_get_max_tdp_level(void) 3250 { 3251 if (cpu_has_vmx_ept_5levels()) 3252 return 5; 3253 return 4; 3254 } 3255 3256 u64 construct_eptp(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level) 3257 { 3258 u64 eptp = VMX_EPTP_MT_WB; 3259 3260 eptp |= (root_level == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4; 3261 3262 if (enable_ept_ad_bits && 3263 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) 3264 eptp |= VMX_EPTP_AD_ENABLE_BIT; 3265 eptp |= root_hpa; 3266 3267 return eptp; 3268 } 3269 3270 static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, 3271 int root_level) 3272 { 3273 struct kvm *kvm = vcpu->kvm; 3274 bool update_guest_cr3 = true; 3275 unsigned long guest_cr3; 3276 u64 eptp; 3277 3278 if (enable_ept) { 3279 eptp = construct_eptp(vcpu, root_hpa, root_level); 3280 vmcs_write64(EPT_POINTER, eptp); 3281 3282 hv_track_root_tdp(vcpu, root_hpa); 3283 3284 if (!enable_unrestricted_guest && !is_paging(vcpu)) 3285 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; 3286 else if (kvm_register_is_dirty(vcpu, VCPU_EXREG_CR3)) 3287 guest_cr3 = vcpu->arch.cr3; 3288 else /* vmcs.GUEST_CR3 is already up-to-date. */ 3289 update_guest_cr3 = false; 3290 vmx_ept_load_pdptrs(vcpu); 3291 } else { 3292 guest_cr3 = root_hpa | kvm_get_active_pcid(vcpu); 3293 } 3294 3295 if (update_guest_cr3) 3296 vmcs_writel(GUEST_CR3, guest_cr3); 3297 } 3298 3299 3300 static bool vmx_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) 3301 { 3302 /* 3303 * We operate under the default treatment of SMM, so VMX cannot be 3304 * enabled under SMM. Note, whether or not VMXE is allowed at all, 3305 * i.e. is a reserved bit, is handled by common x86 code. 3306 */ 3307 if ((cr4 & X86_CR4_VMXE) && is_smm(vcpu)) 3308 return false; 3309 3310 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) 3311 return false; 3312 3313 return true; 3314 } 3315 3316 void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) 3317 { 3318 unsigned long old_cr4 = vcpu->arch.cr4; 3319 struct vcpu_vmx *vmx = to_vmx(vcpu); 3320 /* 3321 * Pass through host's Machine Check Enable value to hw_cr4, which 3322 * is in force while we are in guest mode. Do not let guests control 3323 * this bit, even if host CR4.MCE == 0. 3324 */ 3325 unsigned long hw_cr4; 3326 3327 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); 3328 if (is_unrestricted_guest(vcpu)) 3329 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; 3330 else if (vmx->rmode.vm86_active) 3331 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; 3332 else 3333 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; 3334 3335 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { 3336 if (cr4 & X86_CR4_UMIP) { 3337 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC); 3338 hw_cr4 &= ~X86_CR4_UMIP; 3339 } else if (!is_guest_mode(vcpu) || 3340 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) { 3341 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC); 3342 } 3343 } 3344 3345 vcpu->arch.cr4 = cr4; 3346 kvm_register_mark_available(vcpu, VCPU_EXREG_CR4); 3347 3348 if (!is_unrestricted_guest(vcpu)) { 3349 if (enable_ept) { 3350 if (!is_paging(vcpu)) { 3351 hw_cr4 &= ~X86_CR4_PAE; 3352 hw_cr4 |= X86_CR4_PSE; 3353 } else if (!(cr4 & X86_CR4_PAE)) { 3354 hw_cr4 &= ~X86_CR4_PAE; 3355 } 3356 } 3357 3358 /* 3359 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in 3360 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs 3361 * to be manually disabled when guest switches to non-paging 3362 * mode. 3363 * 3364 * If !enable_unrestricted_guest, the CPU is always running 3365 * with CR0.PG=1 and CR4 needs to be modified. 3366 * If enable_unrestricted_guest, the CPU automatically 3367 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. 3368 */ 3369 if (!is_paging(vcpu)) 3370 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); 3371 } 3372 3373 vmcs_writel(CR4_READ_SHADOW, cr4); 3374 vmcs_writel(GUEST_CR4, hw_cr4); 3375 3376 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE)) 3377 kvm_update_cpuid_runtime(vcpu); 3378 } 3379 3380 void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) 3381 { 3382 struct vcpu_vmx *vmx = to_vmx(vcpu); 3383 u32 ar; 3384 3385 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { 3386 *var = vmx->rmode.segs[seg]; 3387 if (seg == VCPU_SREG_TR 3388 || var->selector == vmx_read_guest_seg_selector(vmx, seg)) 3389 return; 3390 var->base = vmx_read_guest_seg_base(vmx, seg); 3391 var->selector = vmx_read_guest_seg_selector(vmx, seg); 3392 return; 3393 } 3394 var->base = vmx_read_guest_seg_base(vmx, seg); 3395 var->limit = vmx_read_guest_seg_limit(vmx, seg); 3396 var->selector = vmx_read_guest_seg_selector(vmx, seg); 3397 ar = vmx_read_guest_seg_ar(vmx, seg); 3398 var->unusable = (ar >> 16) & 1; 3399 var->type = ar & 15; 3400 var->s = (ar >> 4) & 1; 3401 var->dpl = (ar >> 5) & 3; 3402 /* 3403 * Some userspaces do not preserve unusable property. Since usable 3404 * segment has to be present according to VMX spec we can use present 3405 * property to amend userspace bug by making unusable segment always 3406 * nonpresent. vmx_segment_access_rights() already marks nonpresent 3407 * segment as unusable. 3408 */ 3409 var->present = !var->unusable; 3410 var->avl = (ar >> 12) & 1; 3411 var->l = (ar >> 13) & 1; 3412 var->db = (ar >> 14) & 1; 3413 var->g = (ar >> 15) & 1; 3414 } 3415 3416 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) 3417 { 3418 struct kvm_segment s; 3419 3420 if (to_vmx(vcpu)->rmode.vm86_active) { 3421 vmx_get_segment(vcpu, &s, seg); 3422 return s.base; 3423 } 3424 return vmx_read_guest_seg_base(to_vmx(vcpu), seg); 3425 } 3426 3427 int vmx_get_cpl(struct kvm_vcpu *vcpu) 3428 { 3429 struct vcpu_vmx *vmx = to_vmx(vcpu); 3430 3431 if (unlikely(vmx->rmode.vm86_active)) 3432 return 0; 3433 else { 3434 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); 3435 return VMX_AR_DPL(ar); 3436 } 3437 } 3438 3439 static u32 vmx_segment_access_rights(struct kvm_segment *var) 3440 { 3441 u32 ar; 3442 3443 if (var->unusable || !var->present) 3444 ar = 1 << 16; 3445 else { 3446 ar = var->type & 15; 3447 ar |= (var->s & 1) << 4; 3448 ar |= (var->dpl & 3) << 5; 3449 ar |= (var->present & 1) << 7; 3450 ar |= (var->avl & 1) << 12; 3451 ar |= (var->l & 1) << 13; 3452 ar |= (var->db & 1) << 14; 3453 ar |= (var->g & 1) << 15; 3454 } 3455 3456 return ar; 3457 } 3458 3459 void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) 3460 { 3461 struct vcpu_vmx *vmx = to_vmx(vcpu); 3462 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; 3463 3464 vmx_segment_cache_clear(vmx); 3465 3466 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { 3467 vmx->rmode.segs[seg] = *var; 3468 if (seg == VCPU_SREG_TR) 3469 vmcs_write16(sf->selector, var->selector); 3470 else if (var->s) 3471 fix_rmode_seg(seg, &vmx->rmode.segs[seg]); 3472 return; 3473 } 3474 3475 vmcs_writel(sf->base, var->base); 3476 vmcs_write32(sf->limit, var->limit); 3477 vmcs_write16(sf->selector, var->selector); 3478 3479 /* 3480 * Fix the "Accessed" bit in AR field of segment registers for older 3481 * qemu binaries. 3482 * IA32 arch specifies that at the time of processor reset the 3483 * "Accessed" bit in the AR field of segment registers is 1. And qemu 3484 * is setting it to 0 in the userland code. This causes invalid guest 3485 * state vmexit when "unrestricted guest" mode is turned on. 3486 * Fix for this setup issue in cpu_reset is being pushed in the qemu 3487 * tree. Newer qemu binaries with that qemu fix would not need this 3488 * kvm hack. 3489 */ 3490 if (is_unrestricted_guest(vcpu) && (seg != VCPU_SREG_LDTR)) 3491 var->type |= 0x1; /* Accessed */ 3492 3493 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); 3494 } 3495 3496 static void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) 3497 { 3498 __vmx_set_segment(vcpu, var, seg); 3499 3500 to_vmx(vcpu)->emulation_required = vmx_emulation_required(vcpu); 3501 } 3502 3503 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) 3504 { 3505 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS); 3506 3507 *db = (ar >> 14) & 1; 3508 *l = (ar >> 13) & 1; 3509 } 3510 3511 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 3512 { 3513 dt->size = vmcs_read32(GUEST_IDTR_LIMIT); 3514 dt->address = vmcs_readl(GUEST_IDTR_BASE); 3515 } 3516 3517 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 3518 { 3519 vmcs_write32(GUEST_IDTR_LIMIT, dt->size); 3520 vmcs_writel(GUEST_IDTR_BASE, dt->address); 3521 } 3522 3523 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 3524 { 3525 dt->size = vmcs_read32(GUEST_GDTR_LIMIT); 3526 dt->address = vmcs_readl(GUEST_GDTR_BASE); 3527 } 3528 3529 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 3530 { 3531 vmcs_write32(GUEST_GDTR_LIMIT, dt->size); 3532 vmcs_writel(GUEST_GDTR_BASE, dt->address); 3533 } 3534 3535 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) 3536 { 3537 struct kvm_segment var; 3538 u32 ar; 3539 3540 vmx_get_segment(vcpu, &var, seg); 3541 var.dpl = 0x3; 3542 if (seg == VCPU_SREG_CS) 3543 var.type = 0x3; 3544 ar = vmx_segment_access_rights(&var); 3545 3546 if (var.base != (var.selector << 4)) 3547 return false; 3548 if (var.limit != 0xffff) 3549 return false; 3550 if (ar != 0xf3) 3551 return false; 3552 3553 return true; 3554 } 3555 3556 static bool code_segment_valid(struct kvm_vcpu *vcpu) 3557 { 3558 struct kvm_segment cs; 3559 unsigned int cs_rpl; 3560 3561 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); 3562 cs_rpl = cs.selector & SEGMENT_RPL_MASK; 3563 3564 if (cs.unusable) 3565 return false; 3566 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK)) 3567 return false; 3568 if (!cs.s) 3569 return false; 3570 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { 3571 if (cs.dpl > cs_rpl) 3572 return false; 3573 } else { 3574 if (cs.dpl != cs_rpl) 3575 return false; 3576 } 3577 if (!cs.present) 3578 return false; 3579 3580 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ 3581 return true; 3582 } 3583 3584 static bool stack_segment_valid(struct kvm_vcpu *vcpu) 3585 { 3586 struct kvm_segment ss; 3587 unsigned int ss_rpl; 3588 3589 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); 3590 ss_rpl = ss.selector & SEGMENT_RPL_MASK; 3591 3592 if (ss.unusable) 3593 return true; 3594 if (ss.type != 3 && ss.type != 7) 3595 return false; 3596 if (!ss.s) 3597 return false; 3598 if (ss.dpl != ss_rpl) /* DPL != RPL */ 3599 return false; 3600 if (!ss.present) 3601 return false; 3602 3603 return true; 3604 } 3605 3606 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) 3607 { 3608 struct kvm_segment var; 3609 unsigned int rpl; 3610 3611 vmx_get_segment(vcpu, &var, seg); 3612 rpl = var.selector & SEGMENT_RPL_MASK; 3613 3614 if (var.unusable) 3615 return true; 3616 if (!var.s) 3617 return false; 3618 if (!var.present) 3619 return false; 3620 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) { 3621 if (var.dpl < rpl) /* DPL < RPL */ 3622 return false; 3623 } 3624 3625 /* TODO: Add other members to kvm_segment_field to allow checking for other access 3626 * rights flags 3627 */ 3628 return true; 3629 } 3630 3631 static bool tr_valid(struct kvm_vcpu *vcpu) 3632 { 3633 struct kvm_segment tr; 3634 3635 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); 3636 3637 if (tr.unusable) 3638 return false; 3639 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ 3640 return false; 3641 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ 3642 return false; 3643 if (!tr.present) 3644 return false; 3645 3646 return true; 3647 } 3648 3649 static bool ldtr_valid(struct kvm_vcpu *vcpu) 3650 { 3651 struct kvm_segment ldtr; 3652 3653 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); 3654 3655 if (ldtr.unusable) 3656 return true; 3657 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ 3658 return false; 3659 if (ldtr.type != 2) 3660 return false; 3661 if (!ldtr.present) 3662 return false; 3663 3664 return true; 3665 } 3666 3667 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) 3668 { 3669 struct kvm_segment cs, ss; 3670 3671 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); 3672 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); 3673 3674 return ((cs.selector & SEGMENT_RPL_MASK) == 3675 (ss.selector & SEGMENT_RPL_MASK)); 3676 } 3677 3678 /* 3679 * Check if guest state is valid. Returns true if valid, false if 3680 * not. 3681 * We assume that registers are always usable 3682 */ 3683 bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu) 3684 { 3685 /* real mode guest state checks */ 3686 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { 3687 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) 3688 return false; 3689 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) 3690 return false; 3691 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) 3692 return false; 3693 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) 3694 return false; 3695 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) 3696 return false; 3697 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) 3698 return false; 3699 } else { 3700 /* protected mode guest state checks */ 3701 if (!cs_ss_rpl_check(vcpu)) 3702 return false; 3703 if (!code_segment_valid(vcpu)) 3704 return false; 3705 if (!stack_segment_valid(vcpu)) 3706 return false; 3707 if (!data_segment_valid(vcpu, VCPU_SREG_DS)) 3708 return false; 3709 if (!data_segment_valid(vcpu, VCPU_SREG_ES)) 3710 return false; 3711 if (!data_segment_valid(vcpu, VCPU_SREG_FS)) 3712 return false; 3713 if (!data_segment_valid(vcpu, VCPU_SREG_GS)) 3714 return false; 3715 if (!tr_valid(vcpu)) 3716 return false; 3717 if (!ldtr_valid(vcpu)) 3718 return false; 3719 } 3720 /* TODO: 3721 * - Add checks on RIP 3722 * - Add checks on RFLAGS 3723 */ 3724 3725 return true; 3726 } 3727 3728 static int init_rmode_tss(struct kvm *kvm, void __user *ua) 3729 { 3730 const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0))); 3731 u16 data; 3732 int i; 3733 3734 for (i = 0; i < 3; i++) { 3735 if (__copy_to_user(ua + PAGE_SIZE * i, zero_page, PAGE_SIZE)) 3736 return -EFAULT; 3737 } 3738 3739 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; 3740 if (__copy_to_user(ua + TSS_IOPB_BASE_OFFSET, &data, sizeof(u16))) 3741 return -EFAULT; 3742 3743 data = ~0; 3744 if (__copy_to_user(ua + RMODE_TSS_SIZE - 1, &data, sizeof(u8))) 3745 return -EFAULT; 3746 3747 return 0; 3748 } 3749 3750 static int init_rmode_identity_map(struct kvm *kvm) 3751 { 3752 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); 3753 int i, r = 0; 3754 void __user *uaddr; 3755 u32 tmp; 3756 3757 /* Protect kvm_vmx->ept_identity_pagetable_done. */ 3758 mutex_lock(&kvm->slots_lock); 3759 3760 if (likely(kvm_vmx->ept_identity_pagetable_done)) 3761 goto out; 3762 3763 if (!kvm_vmx->ept_identity_map_addr) 3764 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; 3765 3766 uaddr = __x86_set_memory_region(kvm, 3767 IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 3768 kvm_vmx->ept_identity_map_addr, 3769 PAGE_SIZE); 3770 if (IS_ERR(uaddr)) { 3771 r = PTR_ERR(uaddr); 3772 goto out; 3773 } 3774 3775 /* Set up identity-mapping pagetable for EPT in real mode */ 3776 for (i = 0; i < (PAGE_SIZE / sizeof(tmp)); i++) { 3777 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | 3778 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); 3779 if (__copy_to_user(uaddr + i * sizeof(tmp), &tmp, sizeof(tmp))) { 3780 r = -EFAULT; 3781 goto out; 3782 } 3783 } 3784 kvm_vmx->ept_identity_pagetable_done = true; 3785 3786 out: 3787 mutex_unlock(&kvm->slots_lock); 3788 return r; 3789 } 3790 3791 static void seg_setup(int seg) 3792 { 3793 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; 3794 unsigned int ar; 3795 3796 vmcs_write16(sf->selector, 0); 3797 vmcs_writel(sf->base, 0); 3798 vmcs_write32(sf->limit, 0xffff); 3799 ar = 0x93; 3800 if (seg == VCPU_SREG_CS) 3801 ar |= 0x08; /* code segment */ 3802 3803 vmcs_write32(sf->ar_bytes, ar); 3804 } 3805 3806 static int alloc_apic_access_page(struct kvm *kvm) 3807 { 3808 struct page *page; 3809 void __user *hva; 3810 int ret = 0; 3811 3812 mutex_lock(&kvm->slots_lock); 3813 if (kvm->arch.apic_access_memslot_enabled) 3814 goto out; 3815 hva = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 3816 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); 3817 if (IS_ERR(hva)) { 3818 ret = PTR_ERR(hva); 3819 goto out; 3820 } 3821 3822 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); 3823 if (is_error_page(page)) { 3824 ret = -EFAULT; 3825 goto out; 3826 } 3827 3828 /* 3829 * Do not pin the page in memory, so that memory hot-unplug 3830 * is able to migrate it. 3831 */ 3832 put_page(page); 3833 kvm->arch.apic_access_memslot_enabled = true; 3834 out: 3835 mutex_unlock(&kvm->slots_lock); 3836 return ret; 3837 } 3838 3839 int allocate_vpid(void) 3840 { 3841 int vpid; 3842 3843 if (!enable_vpid) 3844 return 0; 3845 spin_lock(&vmx_vpid_lock); 3846 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); 3847 if (vpid < VMX_NR_VPIDS) 3848 __set_bit(vpid, vmx_vpid_bitmap); 3849 else 3850 vpid = 0; 3851 spin_unlock(&vmx_vpid_lock); 3852 return vpid; 3853 } 3854 3855 void free_vpid(int vpid) 3856 { 3857 if (!enable_vpid || vpid == 0) 3858 return; 3859 spin_lock(&vmx_vpid_lock); 3860 __clear_bit(vpid, vmx_vpid_bitmap); 3861 spin_unlock(&vmx_vpid_lock); 3862 } 3863 3864 static void vmx_msr_bitmap_l01_changed(struct vcpu_vmx *vmx) 3865 { 3866 /* 3867 * When KVM is a nested hypervisor on top of Hyper-V and uses 3868 * 'Enlightened MSR Bitmap' feature L0 needs to know that MSR 3869 * bitmap has changed. 3870 */ 3871 if (static_branch_unlikely(&enable_evmcs)) 3872 evmcs_touch_msr_bitmap(); 3873 3874 vmx->nested.force_msr_bitmap_recalc = true; 3875 } 3876 3877 void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) 3878 { 3879 struct vcpu_vmx *vmx = to_vmx(vcpu); 3880 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; 3881 3882 if (!cpu_has_vmx_msr_bitmap()) 3883 return; 3884 3885 vmx_msr_bitmap_l01_changed(vmx); 3886 3887 /* 3888 * Mark the desired intercept state in shadow bitmap, this is needed 3889 * for resync when the MSR filters change. 3890 */ 3891 if (is_valid_passthrough_msr(msr)) { 3892 int idx = possible_passthrough_msr_slot(msr); 3893 3894 if (idx != -ENOENT) { 3895 if (type & MSR_TYPE_R) 3896 clear_bit(idx, vmx->shadow_msr_intercept.read); 3897 if (type & MSR_TYPE_W) 3898 clear_bit(idx, vmx->shadow_msr_intercept.write); 3899 } 3900 } 3901 3902 if ((type & MSR_TYPE_R) && 3903 !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) { 3904 vmx_set_msr_bitmap_read(msr_bitmap, msr); 3905 type &= ~MSR_TYPE_R; 3906 } 3907 3908 if ((type & MSR_TYPE_W) && 3909 !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) { 3910 vmx_set_msr_bitmap_write(msr_bitmap, msr); 3911 type &= ~MSR_TYPE_W; 3912 } 3913 3914 if (type & MSR_TYPE_R) 3915 vmx_clear_msr_bitmap_read(msr_bitmap, msr); 3916 3917 if (type & MSR_TYPE_W) 3918 vmx_clear_msr_bitmap_write(msr_bitmap, msr); 3919 } 3920 3921 void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) 3922 { 3923 struct vcpu_vmx *vmx = to_vmx(vcpu); 3924 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; 3925 3926 if (!cpu_has_vmx_msr_bitmap()) 3927 return; 3928 3929 vmx_msr_bitmap_l01_changed(vmx); 3930 3931 /* 3932 * Mark the desired intercept state in shadow bitmap, this is needed 3933 * for resync when the MSR filter changes. 3934 */ 3935 if (is_valid_passthrough_msr(msr)) { 3936 int idx = possible_passthrough_msr_slot(msr); 3937 3938 if (idx != -ENOENT) { 3939 if (type & MSR_TYPE_R) 3940 set_bit(idx, vmx->shadow_msr_intercept.read); 3941 if (type & MSR_TYPE_W) 3942 set_bit(idx, vmx->shadow_msr_intercept.write); 3943 } 3944 } 3945 3946 if (type & MSR_TYPE_R) 3947 vmx_set_msr_bitmap_read(msr_bitmap, msr); 3948 3949 if (type & MSR_TYPE_W) 3950 vmx_set_msr_bitmap_write(msr_bitmap, msr); 3951 } 3952 3953 static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode) 3954 { 3955 unsigned long *msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; 3956 unsigned long read_intercept; 3957 int msr; 3958 3959 read_intercept = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; 3960 3961 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { 3962 unsigned int read_idx = msr / BITS_PER_LONG; 3963 unsigned int write_idx = read_idx + (0x800 / sizeof(long)); 3964 3965 msr_bitmap[read_idx] = read_intercept; 3966 msr_bitmap[write_idx] = ~0ul; 3967 } 3968 } 3969 3970 static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu) 3971 { 3972 struct vcpu_vmx *vmx = to_vmx(vcpu); 3973 u8 mode; 3974 3975 if (!cpu_has_vmx_msr_bitmap()) 3976 return; 3977 3978 if (cpu_has_secondary_exec_ctrls() && 3979 (secondary_exec_controls_get(vmx) & 3980 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { 3981 mode = MSR_BITMAP_MODE_X2APIC; 3982 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) 3983 mode |= MSR_BITMAP_MODE_X2APIC_APICV; 3984 } else { 3985 mode = 0; 3986 } 3987 3988 if (mode == vmx->x2apic_msr_bitmap_mode) 3989 return; 3990 3991 vmx->x2apic_msr_bitmap_mode = mode; 3992 3993 vmx_reset_x2apic_msrs(vcpu, mode); 3994 3995 /* 3996 * TPR reads and writes can be virtualized even if virtual interrupt 3997 * delivery is not in use. 3998 */ 3999 vmx_set_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW, 4000 !(mode & MSR_BITMAP_MODE_X2APIC)); 4001 4002 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { 4003 vmx_enable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_RW); 4004 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); 4005 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); 4006 if (enable_ipiv) 4007 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_ICR), MSR_TYPE_RW); 4008 } 4009 } 4010 4011 void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu) 4012 { 4013 struct vcpu_vmx *vmx = to_vmx(vcpu); 4014 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN); 4015 u32 i; 4016 4017 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_STATUS, MSR_TYPE_RW, flag); 4018 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_BASE, MSR_TYPE_RW, flag); 4019 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_MASK, MSR_TYPE_RW, flag); 4020 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_CR3_MATCH, MSR_TYPE_RW, flag); 4021 for (i = 0; i < vmx->pt_desc.num_address_ranges; i++) { 4022 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag); 4023 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag); 4024 } 4025 } 4026 4027 static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) 4028 { 4029 struct vcpu_vmx *vmx = to_vmx(vcpu); 4030 void *vapic_page; 4031 u32 vppr; 4032 int rvi; 4033 4034 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) || 4035 !nested_cpu_has_vid(get_vmcs12(vcpu)) || 4036 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn)) 4037 return false; 4038 4039 rvi = vmx_get_rvi(); 4040 4041 vapic_page = vmx->nested.virtual_apic_map.hva; 4042 vppr = *((u32 *)(vapic_page + APIC_PROCPRI)); 4043 4044 return ((rvi & 0xf0) > (vppr & 0xf0)); 4045 } 4046 4047 static void vmx_msr_filter_changed(struct kvm_vcpu *vcpu) 4048 { 4049 struct vcpu_vmx *vmx = to_vmx(vcpu); 4050 u32 i; 4051 4052 /* 4053 * Redo intercept permissions for MSRs that KVM is passing through to 4054 * the guest. Disabling interception will check the new MSR filter and 4055 * ensure that KVM enables interception if usersepace wants to filter 4056 * the MSR. MSRs that KVM is already intercepting don't need to be 4057 * refreshed since KVM is going to intercept them regardless of what 4058 * userspace wants. 4059 */ 4060 for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++) { 4061 u32 msr = vmx_possible_passthrough_msrs[i]; 4062 4063 if (!test_bit(i, vmx->shadow_msr_intercept.read)) 4064 vmx_disable_intercept_for_msr(vcpu, msr, MSR_TYPE_R); 4065 4066 if (!test_bit(i, vmx->shadow_msr_intercept.write)) 4067 vmx_disable_intercept_for_msr(vcpu, msr, MSR_TYPE_W); 4068 } 4069 4070 /* PT MSRs can be passed through iff PT is exposed to the guest. */ 4071 if (vmx_pt_mode_is_host_guest()) 4072 pt_update_intercept_for_msr(vcpu); 4073 } 4074 4075 static inline void kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, 4076 int pi_vec) 4077 { 4078 #ifdef CONFIG_SMP 4079 if (vcpu->mode == IN_GUEST_MODE) { 4080 /* 4081 * The vector of the virtual has already been set in the PIR. 4082 * Send a notification event to deliver the virtual interrupt 4083 * unless the vCPU is the currently running vCPU, i.e. the 4084 * event is being sent from a fastpath VM-Exit handler, in 4085 * which case the PIR will be synced to the vIRR before 4086 * re-entering the guest. 4087 * 4088 * When the target is not the running vCPU, the following 4089 * possibilities emerge: 4090 * 4091 * Case 1: vCPU stays in non-root mode. Sending a notification 4092 * event posts the interrupt to the vCPU. 4093 * 4094 * Case 2: vCPU exits to root mode and is still runnable. The 4095 * PIR will be synced to the vIRR before re-entering the guest. 4096 * Sending a notification event is ok as the host IRQ handler 4097 * will ignore the spurious event. 4098 * 4099 * Case 3: vCPU exits to root mode and is blocked. vcpu_block() 4100 * has already synced PIR to vIRR and never blocks the vCPU if 4101 * the vIRR is not empty. Therefore, a blocked vCPU here does 4102 * not wait for any requested interrupts in PIR, and sending a 4103 * notification event also results in a benign, spurious event. 4104 */ 4105 4106 if (vcpu != kvm_get_running_vcpu()) 4107 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); 4108 return; 4109 } 4110 #endif 4111 /* 4112 * The vCPU isn't in the guest; wake the vCPU in case it is blocking, 4113 * otherwise do nothing as KVM will grab the highest priority pending 4114 * IRQ via ->sync_pir_to_irr() in vcpu_enter_guest(). 4115 */ 4116 kvm_vcpu_wake_up(vcpu); 4117 } 4118 4119 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, 4120 int vector) 4121 { 4122 struct vcpu_vmx *vmx = to_vmx(vcpu); 4123 4124 if (is_guest_mode(vcpu) && 4125 vector == vmx->nested.posted_intr_nv) { 4126 /* 4127 * If a posted intr is not recognized by hardware, 4128 * we will accomplish it in the next vmentry. 4129 */ 4130 vmx->nested.pi_pending = true; 4131 kvm_make_request(KVM_REQ_EVENT, vcpu); 4132 4133 /* 4134 * This pairs with the smp_mb_*() after setting vcpu->mode in 4135 * vcpu_enter_guest() to guarantee the vCPU sees the event 4136 * request if triggering a posted interrupt "fails" because 4137 * vcpu->mode != IN_GUEST_MODE. The extra barrier is needed as 4138 * the smb_wmb() in kvm_make_request() only ensures everything 4139 * done before making the request is visible when the request 4140 * is visible, it doesn't ensure ordering between the store to 4141 * vcpu->requests and the load from vcpu->mode. 4142 */ 4143 smp_mb__after_atomic(); 4144 4145 /* the PIR and ON have been set by L1. */ 4146 kvm_vcpu_trigger_posted_interrupt(vcpu, POSTED_INTR_NESTED_VECTOR); 4147 return 0; 4148 } 4149 return -1; 4150 } 4151 /* 4152 * Send interrupt to vcpu via posted interrupt way. 4153 * 1. If target vcpu is running(non-root mode), send posted interrupt 4154 * notification to vcpu and hardware will sync PIR to vIRR atomically. 4155 * 2. If target vcpu isn't running(root mode), kick it to pick up the 4156 * interrupt from PIR in next vmentry. 4157 */ 4158 static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) 4159 { 4160 struct vcpu_vmx *vmx = to_vmx(vcpu); 4161 int r; 4162 4163 r = vmx_deliver_nested_posted_interrupt(vcpu, vector); 4164 if (!r) 4165 return 0; 4166 4167 /* Note, this is called iff the local APIC is in-kernel. */ 4168 if (!vcpu->arch.apic->apicv_active) 4169 return -1; 4170 4171 if (pi_test_and_set_pir(vector, &vmx->pi_desc)) 4172 return 0; 4173 4174 /* If a previous notification has sent the IPI, nothing to do. */ 4175 if (pi_test_and_set_on(&vmx->pi_desc)) 4176 return 0; 4177 4178 /* 4179 * The implied barrier in pi_test_and_set_on() pairs with the smp_mb_*() 4180 * after setting vcpu->mode in vcpu_enter_guest(), thus the vCPU is 4181 * guaranteed to see PID.ON=1 and sync the PIR to IRR if triggering a 4182 * posted interrupt "fails" because vcpu->mode != IN_GUEST_MODE. 4183 */ 4184 kvm_vcpu_trigger_posted_interrupt(vcpu, POSTED_INTR_VECTOR); 4185 return 0; 4186 } 4187 4188 static void vmx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode, 4189 int trig_mode, int vector) 4190 { 4191 struct kvm_vcpu *vcpu = apic->vcpu; 4192 4193 if (vmx_deliver_posted_interrupt(vcpu, vector)) { 4194 kvm_lapic_set_irr(vector, apic); 4195 kvm_make_request(KVM_REQ_EVENT, vcpu); 4196 kvm_vcpu_kick(vcpu); 4197 } else { 4198 trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode, 4199 trig_mode, vector); 4200 } 4201 } 4202 4203 /* 4204 * Set up the vmcs's constant host-state fields, i.e., host-state fields that 4205 * will not change in the lifetime of the guest. 4206 * Note that host-state that does change is set elsewhere. E.g., host-state 4207 * that is set differently for each CPU is set in vmx_vcpu_load(), not here. 4208 */ 4209 void vmx_set_constant_host_state(struct vcpu_vmx *vmx) 4210 { 4211 u32 low32, high32; 4212 unsigned long tmpl; 4213 unsigned long cr0, cr3, cr4; 4214 4215 cr0 = read_cr0(); 4216 WARN_ON(cr0 & X86_CR0_TS); 4217 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ 4218 4219 /* 4220 * Save the most likely value for this task's CR3 in the VMCS. 4221 * We can't use __get_current_cr3_fast() because we're not atomic. 4222 */ 4223 cr3 = __read_cr3(); 4224 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ 4225 vmx->loaded_vmcs->host_state.cr3 = cr3; 4226 4227 /* Save the most likely value for this task's CR4 in the VMCS. */ 4228 cr4 = cr4_read_shadow(); 4229 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ 4230 vmx->loaded_vmcs->host_state.cr4 = cr4; 4231 4232 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ 4233 #ifdef CONFIG_X86_64 4234 /* 4235 * Load null selectors, so we can avoid reloading them in 4236 * vmx_prepare_switch_to_host(), in case userspace uses 4237 * the null selectors too (the expected case). 4238 */ 4239 vmcs_write16(HOST_DS_SELECTOR, 0); 4240 vmcs_write16(HOST_ES_SELECTOR, 0); 4241 #else 4242 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ 4243 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ 4244 #endif 4245 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ 4246 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ 4247 4248 vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */ 4249 4250 vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */ 4251 4252 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); 4253 vmcs_write32(HOST_IA32_SYSENTER_CS, low32); 4254 4255 /* 4256 * SYSENTER is used for 32-bit system calls on either 32-bit or 4257 * 64-bit kernels. It is always zero If neither is allowed, otherwise 4258 * vmx_vcpu_load_vmcs loads it with the per-CPU entry stack (and may 4259 * have already done so!). 4260 */ 4261 if (!IS_ENABLED(CONFIG_IA32_EMULATION) && !IS_ENABLED(CONFIG_X86_32)) 4262 vmcs_writel(HOST_IA32_SYSENTER_ESP, 0); 4263 4264 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); 4265 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ 4266 4267 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { 4268 rdmsr(MSR_IA32_CR_PAT, low32, high32); 4269 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); 4270 } 4271 4272 if (cpu_has_load_ia32_efer()) 4273 vmcs_write64(HOST_IA32_EFER, host_efer); 4274 } 4275 4276 void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) 4277 { 4278 struct kvm_vcpu *vcpu = &vmx->vcpu; 4279 4280 vcpu->arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS & 4281 ~vcpu->arch.cr4_guest_rsvd_bits; 4282 if (!enable_ept) { 4283 vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_TLBFLUSH_BITS; 4284 vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_PDPTR_BITS; 4285 } 4286 if (is_guest_mode(&vmx->vcpu)) 4287 vcpu->arch.cr4_guest_owned_bits &= 4288 ~get_vmcs12(vcpu)->cr4_guest_host_mask; 4289 vmcs_writel(CR4_GUEST_HOST_MASK, ~vcpu->arch.cr4_guest_owned_bits); 4290 } 4291 4292 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) 4293 { 4294 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; 4295 4296 if (!kvm_vcpu_apicv_active(&vmx->vcpu)) 4297 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; 4298 4299 if (!enable_vnmi) 4300 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; 4301 4302 if (!enable_preemption_timer) 4303 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; 4304 4305 return pin_based_exec_ctrl; 4306 } 4307 4308 static u32 vmx_vmentry_ctrl(void) 4309 { 4310 u32 vmentry_ctrl = vmcs_config.vmentry_ctrl; 4311 4312 if (vmx_pt_mode_is_system()) 4313 vmentry_ctrl &= ~(VM_ENTRY_PT_CONCEAL_PIP | 4314 VM_ENTRY_LOAD_IA32_RTIT_CTL); 4315 /* 4316 * IA32e mode, and loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically. 4317 */ 4318 vmentry_ctrl &= ~(VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | 4319 VM_ENTRY_LOAD_IA32_EFER | 4320 VM_ENTRY_IA32E_MODE); 4321 4322 if (cpu_has_perf_global_ctrl_bug()) 4323 vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; 4324 4325 return vmentry_ctrl; 4326 } 4327 4328 static u32 vmx_vmexit_ctrl(void) 4329 { 4330 u32 vmexit_ctrl = vmcs_config.vmexit_ctrl; 4331 4332 /* 4333 * Not used by KVM and never set in vmcs01 or vmcs02, but emulated for 4334 * nested virtualization and thus allowed to be set in vmcs12. 4335 */ 4336 vmexit_ctrl &= ~(VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER | 4337 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER); 4338 4339 if (vmx_pt_mode_is_system()) 4340 vmexit_ctrl &= ~(VM_EXIT_PT_CONCEAL_PIP | 4341 VM_EXIT_CLEAR_IA32_RTIT_CTL); 4342 4343 if (cpu_has_perf_global_ctrl_bug()) 4344 vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; 4345 4346 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */ 4347 return vmexit_ctrl & 4348 ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | VM_EXIT_LOAD_IA32_EFER); 4349 } 4350 4351 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) 4352 { 4353 struct vcpu_vmx *vmx = to_vmx(vcpu); 4354 4355 if (is_guest_mode(vcpu)) { 4356 vmx->nested.update_vmcs01_apicv_status = true; 4357 return; 4358 } 4359 4360 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx)); 4361 4362 if (kvm_vcpu_apicv_active(vcpu)) { 4363 secondary_exec_controls_setbit(vmx, 4364 SECONDARY_EXEC_APIC_REGISTER_VIRT | 4365 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); 4366 if (enable_ipiv) 4367 tertiary_exec_controls_setbit(vmx, TERTIARY_EXEC_IPI_VIRT); 4368 } else { 4369 secondary_exec_controls_clearbit(vmx, 4370 SECONDARY_EXEC_APIC_REGISTER_VIRT | 4371 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); 4372 if (enable_ipiv) 4373 tertiary_exec_controls_clearbit(vmx, TERTIARY_EXEC_IPI_VIRT); 4374 } 4375 4376 vmx_update_msr_bitmap_x2apic(vcpu); 4377 } 4378 4379 static u32 vmx_exec_control(struct vcpu_vmx *vmx) 4380 { 4381 u32 exec_control = vmcs_config.cpu_based_exec_ctrl; 4382 4383 /* 4384 * Not used by KVM, but fully supported for nesting, i.e. are allowed in 4385 * vmcs12 and propagated to vmcs02 when set in vmcs12. 4386 */ 4387 exec_control &= ~(CPU_BASED_RDTSC_EXITING | 4388 CPU_BASED_USE_IO_BITMAPS | 4389 CPU_BASED_MONITOR_TRAP_FLAG | 4390 CPU_BASED_PAUSE_EXITING); 4391 4392 /* INTR_WINDOW_EXITING and NMI_WINDOW_EXITING are toggled dynamically */ 4393 exec_control &= ~(CPU_BASED_INTR_WINDOW_EXITING | 4394 CPU_BASED_NMI_WINDOW_EXITING); 4395 4396 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) 4397 exec_control &= ~CPU_BASED_MOV_DR_EXITING; 4398 4399 if (!cpu_need_tpr_shadow(&vmx->vcpu)) 4400 exec_control &= ~CPU_BASED_TPR_SHADOW; 4401 4402 #ifdef CONFIG_X86_64 4403 if (exec_control & CPU_BASED_TPR_SHADOW) 4404 exec_control &= ~(CPU_BASED_CR8_LOAD_EXITING | 4405 CPU_BASED_CR8_STORE_EXITING); 4406 else 4407 exec_control |= CPU_BASED_CR8_STORE_EXITING | 4408 CPU_BASED_CR8_LOAD_EXITING; 4409 #endif 4410 /* No need to intercept CR3 access or INVPLG when using EPT. */ 4411 if (enable_ept) 4412 exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | 4413 CPU_BASED_CR3_STORE_EXITING | 4414 CPU_BASED_INVLPG_EXITING); 4415 if (kvm_mwait_in_guest(vmx->vcpu.kvm)) 4416 exec_control &= ~(CPU_BASED_MWAIT_EXITING | 4417 CPU_BASED_MONITOR_EXITING); 4418 if (kvm_hlt_in_guest(vmx->vcpu.kvm)) 4419 exec_control &= ~CPU_BASED_HLT_EXITING; 4420 return exec_control; 4421 } 4422 4423 static u64 vmx_tertiary_exec_control(struct vcpu_vmx *vmx) 4424 { 4425 u64 exec_control = vmcs_config.cpu_based_3rd_exec_ctrl; 4426 4427 /* 4428 * IPI virtualization relies on APICv. Disable IPI virtualization if 4429 * APICv is inhibited. 4430 */ 4431 if (!enable_ipiv || !kvm_vcpu_apicv_active(&vmx->vcpu)) 4432 exec_control &= ~TERTIARY_EXEC_IPI_VIRT; 4433 4434 return exec_control; 4435 } 4436 4437 /* 4438 * Adjust a single secondary execution control bit to intercept/allow an 4439 * instruction in the guest. This is usually done based on whether or not a 4440 * feature has been exposed to the guest in order to correctly emulate faults. 4441 */ 4442 static inline void 4443 vmx_adjust_secondary_exec_control(struct vcpu_vmx *vmx, u32 *exec_control, 4444 u32 control, bool enabled, bool exiting) 4445 { 4446 /* 4447 * If the control is for an opt-in feature, clear the control if the 4448 * feature is not exposed to the guest, i.e. not enabled. If the 4449 * control is opt-out, i.e. an exiting control, clear the control if 4450 * the feature _is_ exposed to the guest, i.e. exiting/interception is 4451 * disabled for the associated instruction. Note, the caller is 4452 * responsible presetting exec_control to set all supported bits. 4453 */ 4454 if (enabled == exiting) 4455 *exec_control &= ~control; 4456 4457 /* 4458 * Update the nested MSR settings so that a nested VMM can/can't set 4459 * controls for features that are/aren't exposed to the guest. 4460 */ 4461 if (nested) { 4462 /* 4463 * All features that can be added or removed to VMX MSRs must 4464 * be supported in the first place for nested virtualization. 4465 */ 4466 if (WARN_ON_ONCE(!(vmcs_config.nested.secondary_ctls_high & control))) 4467 enabled = false; 4468 4469 if (enabled) 4470 vmx->nested.msrs.secondary_ctls_high |= control; 4471 else 4472 vmx->nested.msrs.secondary_ctls_high &= ~control; 4473 } 4474 } 4475 4476 /* 4477 * Wrapper macro for the common case of adjusting a secondary execution control 4478 * based on a single guest CPUID bit, with a dedicated feature bit. This also 4479 * verifies that the control is actually supported by KVM and hardware. 4480 */ 4481 #define vmx_adjust_sec_exec_control(vmx, exec_control, name, feat_name, ctrl_name, exiting) \ 4482 ({ \ 4483 bool __enabled; \ 4484 \ 4485 if (cpu_has_vmx_##name()) { \ 4486 __enabled = guest_cpuid_has(&(vmx)->vcpu, \ 4487 X86_FEATURE_##feat_name); \ 4488 vmx_adjust_secondary_exec_control(vmx, exec_control, \ 4489 SECONDARY_EXEC_##ctrl_name, __enabled, exiting); \ 4490 } \ 4491 }) 4492 4493 /* More macro magic for ENABLE_/opt-in versus _EXITING/opt-out controls. */ 4494 #define vmx_adjust_sec_exec_feature(vmx, exec_control, lname, uname) \ 4495 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, ENABLE_##uname, false) 4496 4497 #define vmx_adjust_sec_exec_exiting(vmx, exec_control, lname, uname) \ 4498 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, uname##_EXITING, true) 4499 4500 static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) 4501 { 4502 struct kvm_vcpu *vcpu = &vmx->vcpu; 4503 4504 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; 4505 4506 if (vmx_pt_mode_is_system()) 4507 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX); 4508 if (!cpu_need_virtualize_apic_accesses(vcpu)) 4509 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; 4510 if (vmx->vpid == 0) 4511 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; 4512 if (!enable_ept) { 4513 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; 4514 enable_unrestricted_guest = 0; 4515 } 4516 if (!enable_unrestricted_guest) 4517 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; 4518 if (kvm_pause_in_guest(vmx->vcpu.kvm)) 4519 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; 4520 if (!kvm_vcpu_apicv_active(vcpu)) 4521 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | 4522 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); 4523 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; 4524 4525 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, 4526 * in vmx_set_cr4. */ 4527 exec_control &= ~SECONDARY_EXEC_DESC; 4528 4529 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD 4530 (handle_vmptrld). 4531 We can NOT enable shadow_vmcs here because we don't have yet 4532 a current VMCS12 4533 */ 4534 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; 4535 4536 /* 4537 * PML is enabled/disabled when dirty logging of memsmlots changes, but 4538 * it needs to be set here when dirty logging is already active, e.g. 4539 * if this vCPU was created after dirty logging was enabled. 4540 */ 4541 if (!vcpu->kvm->arch.cpu_dirty_logging_count) 4542 exec_control &= ~SECONDARY_EXEC_ENABLE_PML; 4543 4544 if (cpu_has_vmx_xsaves()) { 4545 /* Exposing XSAVES only when XSAVE is exposed */ 4546 bool xsaves_enabled = 4547 boot_cpu_has(X86_FEATURE_XSAVE) && 4548 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && 4549 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); 4550 4551 vcpu->arch.xsaves_enabled = xsaves_enabled; 4552 4553 vmx_adjust_secondary_exec_control(vmx, &exec_control, 4554 SECONDARY_EXEC_XSAVES, 4555 xsaves_enabled, false); 4556 } 4557 4558 /* 4559 * RDPID is also gated by ENABLE_RDTSCP, turn on the control if either 4560 * feature is exposed to the guest. This creates a virtualization hole 4561 * if both are supported in hardware but only one is exposed to the 4562 * guest, but letting the guest execute RDTSCP or RDPID when either one 4563 * is advertised is preferable to emulating the advertised instruction 4564 * in KVM on #UD, and obviously better than incorrectly injecting #UD. 4565 */ 4566 if (cpu_has_vmx_rdtscp()) { 4567 bool rdpid_or_rdtscp_enabled = 4568 guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) || 4569 guest_cpuid_has(vcpu, X86_FEATURE_RDPID); 4570 4571 vmx_adjust_secondary_exec_control(vmx, &exec_control, 4572 SECONDARY_EXEC_ENABLE_RDTSCP, 4573 rdpid_or_rdtscp_enabled, false); 4574 } 4575 vmx_adjust_sec_exec_feature(vmx, &exec_control, invpcid, INVPCID); 4576 4577 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdrand, RDRAND); 4578 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdseed, RDSEED); 4579 4580 vmx_adjust_sec_exec_control(vmx, &exec_control, waitpkg, WAITPKG, 4581 ENABLE_USR_WAIT_PAUSE, false); 4582 4583 if (!vcpu->kvm->arch.bus_lock_detection_enabled) 4584 exec_control &= ~SECONDARY_EXEC_BUS_LOCK_DETECTION; 4585 4586 if (!kvm_notify_vmexit_enabled(vcpu->kvm)) 4587 exec_control &= ~SECONDARY_EXEC_NOTIFY_VM_EXITING; 4588 4589 return exec_control; 4590 } 4591 4592 static inline int vmx_get_pid_table_order(struct kvm *kvm) 4593 { 4594 return get_order(kvm->arch.max_vcpu_ids * sizeof(*to_kvm_vmx(kvm)->pid_table)); 4595 } 4596 4597 static int vmx_alloc_ipiv_pid_table(struct kvm *kvm) 4598 { 4599 struct page *pages; 4600 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); 4601 4602 if (!irqchip_in_kernel(kvm) || !enable_ipiv) 4603 return 0; 4604 4605 if (kvm_vmx->pid_table) 4606 return 0; 4607 4608 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, vmx_get_pid_table_order(kvm)); 4609 if (!pages) 4610 return -ENOMEM; 4611 4612 kvm_vmx->pid_table = (void *)page_address(pages); 4613 return 0; 4614 } 4615 4616 static int vmx_vcpu_precreate(struct kvm *kvm) 4617 { 4618 return vmx_alloc_ipiv_pid_table(kvm); 4619 } 4620 4621 #define VMX_XSS_EXIT_BITMAP 0 4622 4623 static void init_vmcs(struct vcpu_vmx *vmx) 4624 { 4625 struct kvm *kvm = vmx->vcpu.kvm; 4626 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); 4627 4628 if (nested) 4629 nested_vmx_set_vmcs_shadowing_bitmap(); 4630 4631 if (cpu_has_vmx_msr_bitmap()) 4632 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); 4633 4634 vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA); /* 22.3.1.5 */ 4635 4636 /* Control */ 4637 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx)); 4638 4639 exec_controls_set(vmx, vmx_exec_control(vmx)); 4640 4641 if (cpu_has_secondary_exec_ctrls()) 4642 secondary_exec_controls_set(vmx, vmx_secondary_exec_control(vmx)); 4643 4644 if (cpu_has_tertiary_exec_ctrls()) 4645 tertiary_exec_controls_set(vmx, vmx_tertiary_exec_control(vmx)); 4646 4647 if (enable_apicv && lapic_in_kernel(&vmx->vcpu)) { 4648 vmcs_write64(EOI_EXIT_BITMAP0, 0); 4649 vmcs_write64(EOI_EXIT_BITMAP1, 0); 4650 vmcs_write64(EOI_EXIT_BITMAP2, 0); 4651 vmcs_write64(EOI_EXIT_BITMAP3, 0); 4652 4653 vmcs_write16(GUEST_INTR_STATUS, 0); 4654 4655 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); 4656 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); 4657 } 4658 4659 if (vmx_can_use_ipiv(&vmx->vcpu)) { 4660 vmcs_write64(PID_POINTER_TABLE, __pa(kvm_vmx->pid_table)); 4661 vmcs_write16(LAST_PID_POINTER_INDEX, kvm->arch.max_vcpu_ids - 1); 4662 } 4663 4664 if (!kvm_pause_in_guest(kvm)) { 4665 vmcs_write32(PLE_GAP, ple_gap); 4666 vmx->ple_window = ple_window; 4667 vmx->ple_window_dirty = true; 4668 } 4669 4670 if (kvm_notify_vmexit_enabled(kvm)) 4671 vmcs_write32(NOTIFY_WINDOW, kvm->arch.notify_window); 4672 4673 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); 4674 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); 4675 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ 4676 4677 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ 4678 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ 4679 vmx_set_constant_host_state(vmx); 4680 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ 4681 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ 4682 4683 if (cpu_has_vmx_vmfunc()) 4684 vmcs_write64(VM_FUNCTION_CONTROL, 0); 4685 4686 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); 4687 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); 4688 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); 4689 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); 4690 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); 4691 4692 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) 4693 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); 4694 4695 vm_exit_controls_set(vmx, vmx_vmexit_ctrl()); 4696 4697 /* 22.2.1, 20.8.1 */ 4698 vm_entry_controls_set(vmx, vmx_vmentry_ctrl()); 4699 4700 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; 4701 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits); 4702 4703 set_cr4_guest_host_mask(vmx); 4704 4705 if (vmx->vpid != 0) 4706 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); 4707 4708 if (cpu_has_vmx_xsaves()) 4709 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); 4710 4711 if (enable_pml) { 4712 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); 4713 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); 4714 } 4715 4716 vmx_write_encls_bitmap(&vmx->vcpu, NULL); 4717 4718 if (vmx_pt_mode_is_host_guest()) { 4719 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc)); 4720 /* Bit[6~0] are forced to 1, writes are ignored. */ 4721 vmx->pt_desc.guest.output_mask = 0x7F; 4722 vmcs_write64(GUEST_IA32_RTIT_CTL, 0); 4723 } 4724 4725 vmcs_write32(GUEST_SYSENTER_CS, 0); 4726 vmcs_writel(GUEST_SYSENTER_ESP, 0); 4727 vmcs_writel(GUEST_SYSENTER_EIP, 0); 4728 vmcs_write64(GUEST_IA32_DEBUGCTL, 0); 4729 4730 if (cpu_has_vmx_tpr_shadow()) { 4731 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); 4732 if (cpu_need_tpr_shadow(&vmx->vcpu)) 4733 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 4734 __pa(vmx->vcpu.arch.apic->regs)); 4735 vmcs_write32(TPR_THRESHOLD, 0); 4736 } 4737 4738 vmx_setup_uret_msrs(vmx); 4739 } 4740 4741 static void __vmx_vcpu_reset(struct kvm_vcpu *vcpu) 4742 { 4743 struct vcpu_vmx *vmx = to_vmx(vcpu); 4744 4745 init_vmcs(vmx); 4746 4747 if (nested) 4748 memcpy(&vmx->nested.msrs, &vmcs_config.nested, sizeof(vmx->nested.msrs)); 4749 4750 vcpu_setup_sgx_lepubkeyhash(vcpu); 4751 4752 vmx->nested.posted_intr_nv = -1; 4753 vmx->nested.vmxon_ptr = INVALID_GPA; 4754 vmx->nested.current_vmptr = INVALID_GPA; 4755 vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID; 4756 4757 vcpu->arch.microcode_version = 0x100000000ULL; 4758 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED; 4759 4760 /* 4761 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR 4762 * or POSTED_INTR_WAKEUP_VECTOR. 4763 */ 4764 vmx->pi_desc.nv = POSTED_INTR_VECTOR; 4765 vmx->pi_desc.sn = 1; 4766 } 4767 4768 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) 4769 { 4770 struct vcpu_vmx *vmx = to_vmx(vcpu); 4771 4772 if (!init_event) 4773 __vmx_vcpu_reset(vcpu); 4774 4775 vmx->rmode.vm86_active = 0; 4776 vmx->spec_ctrl = 0; 4777 4778 vmx->msr_ia32_umwait_control = 0; 4779 4780 vmx->hv_deadline_tsc = -1; 4781 kvm_set_cr8(vcpu, 0); 4782 4783 vmx_segment_cache_clear(vmx); 4784 kvm_register_mark_available(vcpu, VCPU_EXREG_SEGMENTS); 4785 4786 seg_setup(VCPU_SREG_CS); 4787 vmcs_write16(GUEST_CS_SELECTOR, 0xf000); 4788 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); 4789 4790 seg_setup(VCPU_SREG_DS); 4791 seg_setup(VCPU_SREG_ES); 4792 seg_setup(VCPU_SREG_FS); 4793 seg_setup(VCPU_SREG_GS); 4794 seg_setup(VCPU_SREG_SS); 4795 4796 vmcs_write16(GUEST_TR_SELECTOR, 0); 4797 vmcs_writel(GUEST_TR_BASE, 0); 4798 vmcs_write32(GUEST_TR_LIMIT, 0xffff); 4799 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); 4800 4801 vmcs_write16(GUEST_LDTR_SELECTOR, 0); 4802 vmcs_writel(GUEST_LDTR_BASE, 0); 4803 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); 4804 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); 4805 4806 vmcs_writel(GUEST_GDTR_BASE, 0); 4807 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); 4808 4809 vmcs_writel(GUEST_IDTR_BASE, 0); 4810 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); 4811 4812 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); 4813 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); 4814 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); 4815 if (kvm_mpx_supported()) 4816 vmcs_write64(GUEST_BNDCFGS, 0); 4817 4818 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ 4819 4820 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); 4821 4822 vpid_sync_context(vmx->vpid); 4823 4824 vmx_update_fb_clear_dis(vcpu, vmx); 4825 } 4826 4827 static void vmx_enable_irq_window(struct kvm_vcpu *vcpu) 4828 { 4829 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING); 4830 } 4831 4832 static void vmx_enable_nmi_window(struct kvm_vcpu *vcpu) 4833 { 4834 if (!enable_vnmi || 4835 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { 4836 vmx_enable_irq_window(vcpu); 4837 return; 4838 } 4839 4840 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING); 4841 } 4842 4843 static void vmx_inject_irq(struct kvm_vcpu *vcpu, bool reinjected) 4844 { 4845 struct vcpu_vmx *vmx = to_vmx(vcpu); 4846 uint32_t intr; 4847 int irq = vcpu->arch.interrupt.nr; 4848 4849 trace_kvm_inj_virq(irq, vcpu->arch.interrupt.soft, reinjected); 4850 4851 ++vcpu->stat.irq_injections; 4852 if (vmx->rmode.vm86_active) { 4853 int inc_eip = 0; 4854 if (vcpu->arch.interrupt.soft) 4855 inc_eip = vcpu->arch.event_exit_inst_len; 4856 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip); 4857 return; 4858 } 4859 intr = irq | INTR_INFO_VALID_MASK; 4860 if (vcpu->arch.interrupt.soft) { 4861 intr |= INTR_TYPE_SOFT_INTR; 4862 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 4863 vmx->vcpu.arch.event_exit_inst_len); 4864 } else 4865 intr |= INTR_TYPE_EXT_INTR; 4866 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); 4867 4868 vmx_clear_hlt(vcpu); 4869 } 4870 4871 static void vmx_inject_nmi(struct kvm_vcpu *vcpu) 4872 { 4873 struct vcpu_vmx *vmx = to_vmx(vcpu); 4874 4875 if (!enable_vnmi) { 4876 /* 4877 * Tracking the NMI-blocked state in software is built upon 4878 * finding the next open IRQ window. This, in turn, depends on 4879 * well-behaving guests: They have to keep IRQs disabled at 4880 * least as long as the NMI handler runs. Otherwise we may 4881 * cause NMI nesting, maybe breaking the guest. But as this is 4882 * highly unlikely, we can live with the residual risk. 4883 */ 4884 vmx->loaded_vmcs->soft_vnmi_blocked = 1; 4885 vmx->loaded_vmcs->vnmi_blocked_time = 0; 4886 } 4887 4888 ++vcpu->stat.nmi_injections; 4889 vmx->loaded_vmcs->nmi_known_unmasked = false; 4890 4891 if (vmx->rmode.vm86_active) { 4892 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0); 4893 return; 4894 } 4895 4896 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 4897 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); 4898 4899 vmx_clear_hlt(vcpu); 4900 } 4901 4902 bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) 4903 { 4904 struct vcpu_vmx *vmx = to_vmx(vcpu); 4905 bool masked; 4906 4907 if (!enable_vnmi) 4908 return vmx->loaded_vmcs->soft_vnmi_blocked; 4909 if (vmx->loaded_vmcs->nmi_known_unmasked) 4910 return false; 4911 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; 4912 vmx->loaded_vmcs->nmi_known_unmasked = !masked; 4913 return masked; 4914 } 4915 4916 void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) 4917 { 4918 struct vcpu_vmx *vmx = to_vmx(vcpu); 4919 4920 if (!enable_vnmi) { 4921 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { 4922 vmx->loaded_vmcs->soft_vnmi_blocked = masked; 4923 vmx->loaded_vmcs->vnmi_blocked_time = 0; 4924 } 4925 } else { 4926 vmx->loaded_vmcs->nmi_known_unmasked = !masked; 4927 if (masked) 4928 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 4929 GUEST_INTR_STATE_NMI); 4930 else 4931 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, 4932 GUEST_INTR_STATE_NMI); 4933 } 4934 } 4935 4936 bool vmx_nmi_blocked(struct kvm_vcpu *vcpu) 4937 { 4938 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu)) 4939 return false; 4940 4941 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) 4942 return true; 4943 4944 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 4945 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI | 4946 GUEST_INTR_STATE_NMI)); 4947 } 4948 4949 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection) 4950 { 4951 if (to_vmx(vcpu)->nested.nested_run_pending) 4952 return -EBUSY; 4953 4954 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */ 4955 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu)) 4956 return -EBUSY; 4957 4958 return !vmx_nmi_blocked(vcpu); 4959 } 4960 4961 bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu) 4962 { 4963 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) 4964 return false; 4965 4966 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) || 4967 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 4968 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); 4969 } 4970 4971 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) 4972 { 4973 if (to_vmx(vcpu)->nested.nested_run_pending) 4974 return -EBUSY; 4975 4976 /* 4977 * An IRQ must not be injected into L2 if it's supposed to VM-Exit, 4978 * e.g. if the IRQ arrived asynchronously after checking nested events. 4979 */ 4980 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) 4981 return -EBUSY; 4982 4983 return !vmx_interrupt_blocked(vcpu); 4984 } 4985 4986 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) 4987 { 4988 void __user *ret; 4989 4990 if (enable_unrestricted_guest) 4991 return 0; 4992 4993 mutex_lock(&kvm->slots_lock); 4994 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, 4995 PAGE_SIZE * 3); 4996 mutex_unlock(&kvm->slots_lock); 4997 4998 if (IS_ERR(ret)) 4999 return PTR_ERR(ret); 5000 5001 to_kvm_vmx(kvm)->tss_addr = addr; 5002 5003 return init_rmode_tss(kvm, ret); 5004 } 5005 5006 static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) 5007 { 5008 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; 5009 return 0; 5010 } 5011 5012 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) 5013 { 5014 switch (vec) { 5015 case BP_VECTOR: 5016 /* 5017 * Update instruction length as we may reinject the exception 5018 * from user space while in guest debugging mode. 5019 */ 5020 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = 5021 vmcs_read32(VM_EXIT_INSTRUCTION_LEN); 5022 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) 5023 return false; 5024 fallthrough; 5025 case DB_VECTOR: 5026 return !(vcpu->guest_debug & 5027 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)); 5028 case DE_VECTOR: 5029 case OF_VECTOR: 5030 case BR_VECTOR: 5031 case UD_VECTOR: 5032 case DF_VECTOR: 5033 case SS_VECTOR: 5034 case GP_VECTOR: 5035 case MF_VECTOR: 5036 return true; 5037 } 5038 return false; 5039 } 5040 5041 static int handle_rmode_exception(struct kvm_vcpu *vcpu, 5042 int vec, u32 err_code) 5043 { 5044 /* 5045 * Instruction with address size override prefix opcode 0x67 5046 * Cause the #SS fault with 0 error code in VM86 mode. 5047 */ 5048 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { 5049 if (kvm_emulate_instruction(vcpu, 0)) { 5050 if (vcpu->arch.halt_request) { 5051 vcpu->arch.halt_request = 0; 5052 return kvm_emulate_halt_noskip(vcpu); 5053 } 5054 return 1; 5055 } 5056 return 0; 5057 } 5058 5059 /* 5060 * Forward all other exceptions that are valid in real mode. 5061 * FIXME: Breaks guest debugging in real mode, needs to be fixed with 5062 * the required debugging infrastructure rework. 5063 */ 5064 kvm_queue_exception(vcpu, vec); 5065 return 1; 5066 } 5067 5068 static int handle_machine_check(struct kvm_vcpu *vcpu) 5069 { 5070 /* handled by vmx_vcpu_run() */ 5071 return 1; 5072 } 5073 5074 /* 5075 * If the host has split lock detection disabled, then #AC is 5076 * unconditionally injected into the guest, which is the pre split lock 5077 * detection behaviour. 5078 * 5079 * If the host has split lock detection enabled then #AC is 5080 * only injected into the guest when: 5081 * - Guest CPL == 3 (user mode) 5082 * - Guest has #AC detection enabled in CR0 5083 * - Guest EFLAGS has AC bit set 5084 */ 5085 bool vmx_guest_inject_ac(struct kvm_vcpu *vcpu) 5086 { 5087 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) 5088 return true; 5089 5090 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) && 5091 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC); 5092 } 5093 5094 static int handle_exception_nmi(struct kvm_vcpu *vcpu) 5095 { 5096 struct vcpu_vmx *vmx = to_vmx(vcpu); 5097 struct kvm_run *kvm_run = vcpu->run; 5098 u32 intr_info, ex_no, error_code; 5099 unsigned long cr2, dr6; 5100 u32 vect_info; 5101 5102 vect_info = vmx->idt_vectoring_info; 5103 intr_info = vmx_get_intr_info(vcpu); 5104 5105 if (is_machine_check(intr_info) || is_nmi(intr_info)) 5106 return 1; /* handled by handle_exception_nmi_irqoff() */ 5107 5108 /* 5109 * Queue the exception here instead of in handle_nm_fault_irqoff(). 5110 * This ensures the nested_vmx check is not skipped so vmexit can 5111 * be reflected to L1 (when it intercepts #NM) before reaching this 5112 * point. 5113 */ 5114 if (is_nm_fault(intr_info)) { 5115 kvm_queue_exception(vcpu, NM_VECTOR); 5116 return 1; 5117 } 5118 5119 if (is_invalid_opcode(intr_info)) 5120 return handle_ud(vcpu); 5121 5122 error_code = 0; 5123 if (intr_info & INTR_INFO_DELIVER_CODE_MASK) 5124 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); 5125 5126 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { 5127 WARN_ON_ONCE(!enable_vmware_backdoor); 5128 5129 /* 5130 * VMware backdoor emulation on #GP interception only handles 5131 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero 5132 * error code on #GP. 5133 */ 5134 if (error_code) { 5135 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); 5136 return 1; 5137 } 5138 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP); 5139 } 5140 5141 /* 5142 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing 5143 * MMIO, it is better to report an internal error. 5144 * See the comments in vmx_handle_exit. 5145 */ 5146 if ((vect_info & VECTORING_INFO_VALID_MASK) && 5147 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { 5148 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 5149 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; 5150 vcpu->run->internal.ndata = 4; 5151 vcpu->run->internal.data[0] = vect_info; 5152 vcpu->run->internal.data[1] = intr_info; 5153 vcpu->run->internal.data[2] = error_code; 5154 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu; 5155 return 0; 5156 } 5157 5158 if (is_page_fault(intr_info)) { 5159 cr2 = vmx_get_exit_qual(vcpu); 5160 if (enable_ept && !vcpu->arch.apf.host_apf_flags) { 5161 /* 5162 * EPT will cause page fault only if we need to 5163 * detect illegal GPAs. 5164 */ 5165 WARN_ON_ONCE(!allow_smaller_maxphyaddr); 5166 kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code); 5167 return 1; 5168 } else 5169 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); 5170 } 5171 5172 ex_no = intr_info & INTR_INFO_VECTOR_MASK; 5173 5174 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) 5175 return handle_rmode_exception(vcpu, ex_no, error_code); 5176 5177 switch (ex_no) { 5178 case DB_VECTOR: 5179 dr6 = vmx_get_exit_qual(vcpu); 5180 if (!(vcpu->guest_debug & 5181 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { 5182 /* 5183 * If the #DB was due to ICEBP, a.k.a. INT1, skip the 5184 * instruction. ICEBP generates a trap-like #DB, but 5185 * despite its interception control being tied to #DB, 5186 * is an instruction intercept, i.e. the VM-Exit occurs 5187 * on the ICEBP itself. Use the inner "skip" helper to 5188 * avoid single-step #DB and MTF updates, as ICEBP is 5189 * higher priority. Note, skipping ICEBP still clears 5190 * STI and MOVSS blocking. 5191 * 5192 * For all other #DBs, set vmcs.PENDING_DBG_EXCEPTIONS.BS 5193 * if single-step is enabled in RFLAGS and STI or MOVSS 5194 * blocking is active, as the CPU doesn't set the bit 5195 * on VM-Exit due to #DB interception. VM-Entry has a 5196 * consistency check that a single-step #DB is pending 5197 * in this scenario as the previous instruction cannot 5198 * have toggled RFLAGS.TF 0=>1 (because STI and POP/MOV 5199 * don't modify RFLAGS), therefore the one instruction 5200 * delay when activating single-step breakpoints must 5201 * have already expired. Note, the CPU sets/clears BS 5202 * as appropriate for all other VM-Exits types. 5203 */ 5204 if (is_icebp(intr_info)) 5205 WARN_ON(!skip_emulated_instruction(vcpu)); 5206 else if ((vmx_get_rflags(vcpu) & X86_EFLAGS_TF) && 5207 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 5208 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS))) 5209 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 5210 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS) | DR6_BS); 5211 5212 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); 5213 return 1; 5214 } 5215 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; 5216 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); 5217 fallthrough; 5218 case BP_VECTOR: 5219 /* 5220 * Update instruction length as we may reinject #BP from 5221 * user space while in guest debugging mode. Reading it for 5222 * #DB as well causes no harm, it is not used in that case. 5223 */ 5224 vmx->vcpu.arch.event_exit_inst_len = 5225 vmcs_read32(VM_EXIT_INSTRUCTION_LEN); 5226 kvm_run->exit_reason = KVM_EXIT_DEBUG; 5227 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); 5228 kvm_run->debug.arch.exception = ex_no; 5229 break; 5230 case AC_VECTOR: 5231 if (vmx_guest_inject_ac(vcpu)) { 5232 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); 5233 return 1; 5234 } 5235 5236 /* 5237 * Handle split lock. Depending on detection mode this will 5238 * either warn and disable split lock detection for this 5239 * task or force SIGBUS on it. 5240 */ 5241 if (handle_guest_split_lock(kvm_rip_read(vcpu))) 5242 return 1; 5243 fallthrough; 5244 default: 5245 kvm_run->exit_reason = KVM_EXIT_EXCEPTION; 5246 kvm_run->ex.exception = ex_no; 5247 kvm_run->ex.error_code = error_code; 5248 break; 5249 } 5250 return 0; 5251 } 5252 5253 static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu) 5254 { 5255 ++vcpu->stat.irq_exits; 5256 return 1; 5257 } 5258 5259 static int handle_triple_fault(struct kvm_vcpu *vcpu) 5260 { 5261 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; 5262 vcpu->mmio_needed = 0; 5263 return 0; 5264 } 5265 5266 static int handle_io(struct kvm_vcpu *vcpu) 5267 { 5268 unsigned long exit_qualification; 5269 int size, in, string; 5270 unsigned port; 5271 5272 exit_qualification = vmx_get_exit_qual(vcpu); 5273 string = (exit_qualification & 16) != 0; 5274 5275 ++vcpu->stat.io_exits; 5276 5277 if (string) 5278 return kvm_emulate_instruction(vcpu, 0); 5279 5280 port = exit_qualification >> 16; 5281 size = (exit_qualification & 7) + 1; 5282 in = (exit_qualification & 8) != 0; 5283 5284 return kvm_fast_pio(vcpu, size, port, in); 5285 } 5286 5287 static void 5288 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) 5289 { 5290 /* 5291 * Patch in the VMCALL instruction: 5292 */ 5293 hypercall[0] = 0x0f; 5294 hypercall[1] = 0x01; 5295 hypercall[2] = 0xc1; 5296 } 5297 5298 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ 5299 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) 5300 { 5301 if (is_guest_mode(vcpu)) { 5302 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 5303 unsigned long orig_val = val; 5304 5305 /* 5306 * We get here when L2 changed cr0 in a way that did not change 5307 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), 5308 * but did change L0 shadowed bits. So we first calculate the 5309 * effective cr0 value that L1 would like to write into the 5310 * hardware. It consists of the L2-owned bits from the new 5311 * value combined with the L1-owned bits from L1's guest_cr0. 5312 */ 5313 val = (val & ~vmcs12->cr0_guest_host_mask) | 5314 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); 5315 5316 if (!nested_guest_cr0_valid(vcpu, val)) 5317 return 1; 5318 5319 if (kvm_set_cr0(vcpu, val)) 5320 return 1; 5321 vmcs_writel(CR0_READ_SHADOW, orig_val); 5322 return 0; 5323 } else { 5324 if (to_vmx(vcpu)->nested.vmxon && 5325 !nested_host_cr0_valid(vcpu, val)) 5326 return 1; 5327 5328 return kvm_set_cr0(vcpu, val); 5329 } 5330 } 5331 5332 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) 5333 { 5334 if (is_guest_mode(vcpu)) { 5335 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 5336 unsigned long orig_val = val; 5337 5338 /* analogously to handle_set_cr0 */ 5339 val = (val & ~vmcs12->cr4_guest_host_mask) | 5340 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); 5341 if (kvm_set_cr4(vcpu, val)) 5342 return 1; 5343 vmcs_writel(CR4_READ_SHADOW, orig_val); 5344 return 0; 5345 } else 5346 return kvm_set_cr4(vcpu, val); 5347 } 5348 5349 static int handle_desc(struct kvm_vcpu *vcpu) 5350 { 5351 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); 5352 return kvm_emulate_instruction(vcpu, 0); 5353 } 5354 5355 static int handle_cr(struct kvm_vcpu *vcpu) 5356 { 5357 unsigned long exit_qualification, val; 5358 int cr; 5359 int reg; 5360 int err; 5361 int ret; 5362 5363 exit_qualification = vmx_get_exit_qual(vcpu); 5364 cr = exit_qualification & 15; 5365 reg = (exit_qualification >> 8) & 15; 5366 switch ((exit_qualification >> 4) & 3) { 5367 case 0: /* mov to cr */ 5368 val = kvm_register_read(vcpu, reg); 5369 trace_kvm_cr_write(cr, val); 5370 switch (cr) { 5371 case 0: 5372 err = handle_set_cr0(vcpu, val); 5373 return kvm_complete_insn_gp(vcpu, err); 5374 case 3: 5375 WARN_ON_ONCE(enable_unrestricted_guest); 5376 5377 err = kvm_set_cr3(vcpu, val); 5378 return kvm_complete_insn_gp(vcpu, err); 5379 case 4: 5380 err = handle_set_cr4(vcpu, val); 5381 return kvm_complete_insn_gp(vcpu, err); 5382 case 8: { 5383 u8 cr8_prev = kvm_get_cr8(vcpu); 5384 u8 cr8 = (u8)val; 5385 err = kvm_set_cr8(vcpu, cr8); 5386 ret = kvm_complete_insn_gp(vcpu, err); 5387 if (lapic_in_kernel(vcpu)) 5388 return ret; 5389 if (cr8_prev <= cr8) 5390 return ret; 5391 /* 5392 * TODO: we might be squashing a 5393 * KVM_GUESTDBG_SINGLESTEP-triggered 5394 * KVM_EXIT_DEBUG here. 5395 */ 5396 vcpu->run->exit_reason = KVM_EXIT_SET_TPR; 5397 return 0; 5398 } 5399 } 5400 break; 5401 case 2: /* clts */ 5402 KVM_BUG(1, vcpu->kvm, "Guest always owns CR0.TS"); 5403 return -EIO; 5404 case 1: /*mov from cr*/ 5405 switch (cr) { 5406 case 3: 5407 WARN_ON_ONCE(enable_unrestricted_guest); 5408 5409 val = kvm_read_cr3(vcpu); 5410 kvm_register_write(vcpu, reg, val); 5411 trace_kvm_cr_read(cr, val); 5412 return kvm_skip_emulated_instruction(vcpu); 5413 case 8: 5414 val = kvm_get_cr8(vcpu); 5415 kvm_register_write(vcpu, reg, val); 5416 trace_kvm_cr_read(cr, val); 5417 return kvm_skip_emulated_instruction(vcpu); 5418 } 5419 break; 5420 case 3: /* lmsw */ 5421 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; 5422 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); 5423 kvm_lmsw(vcpu, val); 5424 5425 return kvm_skip_emulated_instruction(vcpu); 5426 default: 5427 break; 5428 } 5429 vcpu->run->exit_reason = 0; 5430 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", 5431 (int)(exit_qualification >> 4) & 3, cr); 5432 return 0; 5433 } 5434 5435 static int handle_dr(struct kvm_vcpu *vcpu) 5436 { 5437 unsigned long exit_qualification; 5438 int dr, dr7, reg; 5439 int err = 1; 5440 5441 exit_qualification = vmx_get_exit_qual(vcpu); 5442 dr = exit_qualification & DEBUG_REG_ACCESS_NUM; 5443 5444 /* First, if DR does not exist, trigger UD */ 5445 if (!kvm_require_dr(vcpu, dr)) 5446 return 1; 5447 5448 if (vmx_get_cpl(vcpu) > 0) 5449 goto out; 5450 5451 dr7 = vmcs_readl(GUEST_DR7); 5452 if (dr7 & DR7_GD) { 5453 /* 5454 * As the vm-exit takes precedence over the debug trap, we 5455 * need to emulate the latter, either for the host or the 5456 * guest debugging itself. 5457 */ 5458 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { 5459 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_ACTIVE_LOW; 5460 vcpu->run->debug.arch.dr7 = dr7; 5461 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); 5462 vcpu->run->debug.arch.exception = DB_VECTOR; 5463 vcpu->run->exit_reason = KVM_EXIT_DEBUG; 5464 return 0; 5465 } else { 5466 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD); 5467 return 1; 5468 } 5469 } 5470 5471 if (vcpu->guest_debug == 0) { 5472 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING); 5473 5474 /* 5475 * No more DR vmexits; force a reload of the debug registers 5476 * and reenter on this instruction. The next vmexit will 5477 * retrieve the full state of the debug registers. 5478 */ 5479 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; 5480 return 1; 5481 } 5482 5483 reg = DEBUG_REG_ACCESS_REG(exit_qualification); 5484 if (exit_qualification & TYPE_MOV_FROM_DR) { 5485 unsigned long val; 5486 5487 kvm_get_dr(vcpu, dr, &val); 5488 kvm_register_write(vcpu, reg, val); 5489 err = 0; 5490 } else { 5491 err = kvm_set_dr(vcpu, dr, kvm_register_read(vcpu, reg)); 5492 } 5493 5494 out: 5495 return kvm_complete_insn_gp(vcpu, err); 5496 } 5497 5498 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) 5499 { 5500 get_debugreg(vcpu->arch.db[0], 0); 5501 get_debugreg(vcpu->arch.db[1], 1); 5502 get_debugreg(vcpu->arch.db[2], 2); 5503 get_debugreg(vcpu->arch.db[3], 3); 5504 get_debugreg(vcpu->arch.dr6, 6); 5505 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); 5506 5507 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; 5508 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING); 5509 5510 /* 5511 * exc_debug expects dr6 to be cleared after it runs, avoid that it sees 5512 * a stale dr6 from the guest. 5513 */ 5514 set_debugreg(DR6_RESERVED, 6); 5515 } 5516 5517 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) 5518 { 5519 vmcs_writel(GUEST_DR7, val); 5520 } 5521 5522 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) 5523 { 5524 kvm_apic_update_ppr(vcpu); 5525 return 1; 5526 } 5527 5528 static int handle_interrupt_window(struct kvm_vcpu *vcpu) 5529 { 5530 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING); 5531 5532 kvm_make_request(KVM_REQ_EVENT, vcpu); 5533 5534 ++vcpu->stat.irq_window_exits; 5535 return 1; 5536 } 5537 5538 static int handle_invlpg(struct kvm_vcpu *vcpu) 5539 { 5540 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5541 5542 kvm_mmu_invlpg(vcpu, exit_qualification); 5543 return kvm_skip_emulated_instruction(vcpu); 5544 } 5545 5546 static int handle_apic_access(struct kvm_vcpu *vcpu) 5547 { 5548 if (likely(fasteoi)) { 5549 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5550 int access_type, offset; 5551 5552 access_type = exit_qualification & APIC_ACCESS_TYPE; 5553 offset = exit_qualification & APIC_ACCESS_OFFSET; 5554 /* 5555 * Sane guest uses MOV to write EOI, with written value 5556 * not cared. So make a short-circuit here by avoiding 5557 * heavy instruction emulation. 5558 */ 5559 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && 5560 (offset == APIC_EOI)) { 5561 kvm_lapic_set_eoi(vcpu); 5562 return kvm_skip_emulated_instruction(vcpu); 5563 } 5564 } 5565 return kvm_emulate_instruction(vcpu, 0); 5566 } 5567 5568 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) 5569 { 5570 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5571 int vector = exit_qualification & 0xff; 5572 5573 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ 5574 kvm_apic_set_eoi_accelerated(vcpu, vector); 5575 return 1; 5576 } 5577 5578 static int handle_apic_write(struct kvm_vcpu *vcpu) 5579 { 5580 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5581 5582 /* 5583 * APIC-write VM-Exit is trap-like, KVM doesn't need to advance RIP and 5584 * hardware has done any necessary aliasing, offset adjustments, etc... 5585 * for the access. I.e. the correct value has already been written to 5586 * the vAPIC page for the correct 16-byte chunk. KVM needs only to 5587 * retrieve the register value and emulate the access. 5588 */ 5589 u32 offset = exit_qualification & 0xff0; 5590 5591 kvm_apic_write_nodecode(vcpu, offset); 5592 return 1; 5593 } 5594 5595 static int handle_task_switch(struct kvm_vcpu *vcpu) 5596 { 5597 struct vcpu_vmx *vmx = to_vmx(vcpu); 5598 unsigned long exit_qualification; 5599 bool has_error_code = false; 5600 u32 error_code = 0; 5601 u16 tss_selector; 5602 int reason, type, idt_v, idt_index; 5603 5604 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); 5605 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); 5606 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); 5607 5608 exit_qualification = vmx_get_exit_qual(vcpu); 5609 5610 reason = (u32)exit_qualification >> 30; 5611 if (reason == TASK_SWITCH_GATE && idt_v) { 5612 switch (type) { 5613 case INTR_TYPE_NMI_INTR: 5614 vcpu->arch.nmi_injected = false; 5615 vmx_set_nmi_mask(vcpu, true); 5616 break; 5617 case INTR_TYPE_EXT_INTR: 5618 case INTR_TYPE_SOFT_INTR: 5619 kvm_clear_interrupt_queue(vcpu); 5620 break; 5621 case INTR_TYPE_HARD_EXCEPTION: 5622 if (vmx->idt_vectoring_info & 5623 VECTORING_INFO_DELIVER_CODE_MASK) { 5624 has_error_code = true; 5625 error_code = 5626 vmcs_read32(IDT_VECTORING_ERROR_CODE); 5627 } 5628 fallthrough; 5629 case INTR_TYPE_SOFT_EXCEPTION: 5630 kvm_clear_exception_queue(vcpu); 5631 break; 5632 default: 5633 break; 5634 } 5635 } 5636 tss_selector = exit_qualification; 5637 5638 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && 5639 type != INTR_TYPE_EXT_INTR && 5640 type != INTR_TYPE_NMI_INTR)) 5641 WARN_ON(!skip_emulated_instruction(vcpu)); 5642 5643 /* 5644 * TODO: What about debug traps on tss switch? 5645 * Are we supposed to inject them and update dr6? 5646 */ 5647 return kvm_task_switch(vcpu, tss_selector, 5648 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, 5649 reason, has_error_code, error_code); 5650 } 5651 5652 static int handle_ept_violation(struct kvm_vcpu *vcpu) 5653 { 5654 unsigned long exit_qualification; 5655 gpa_t gpa; 5656 u64 error_code; 5657 5658 exit_qualification = vmx_get_exit_qual(vcpu); 5659 5660 /* 5661 * EPT violation happened while executing iret from NMI, 5662 * "blocked by NMI" bit has to be set before next VM entry. 5663 * There are errata that may cause this bit to not be set: 5664 * AAK134, BY25. 5665 */ 5666 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && 5667 enable_vnmi && 5668 (exit_qualification & INTR_INFO_UNBLOCK_NMI)) 5669 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); 5670 5671 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); 5672 trace_kvm_page_fault(vcpu, gpa, exit_qualification); 5673 5674 /* Is it a read fault? */ 5675 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) 5676 ? PFERR_USER_MASK : 0; 5677 /* Is it a write fault? */ 5678 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) 5679 ? PFERR_WRITE_MASK : 0; 5680 /* Is it a fetch fault? */ 5681 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) 5682 ? PFERR_FETCH_MASK : 0; 5683 /* ept page table entry is present? */ 5684 error_code |= (exit_qualification & EPT_VIOLATION_RWX_MASK) 5685 ? PFERR_PRESENT_MASK : 0; 5686 5687 error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) != 0 ? 5688 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; 5689 5690 vcpu->arch.exit_qualification = exit_qualification; 5691 5692 /* 5693 * Check that the GPA doesn't exceed physical memory limits, as that is 5694 * a guest page fault. We have to emulate the instruction here, because 5695 * if the illegal address is that of a paging structure, then 5696 * EPT_VIOLATION_ACC_WRITE bit is set. Alternatively, if supported we 5697 * would also use advanced VM-exit information for EPT violations to 5698 * reconstruct the page fault error code. 5699 */ 5700 if (unlikely(allow_smaller_maxphyaddr && kvm_vcpu_is_illegal_gpa(vcpu, gpa))) 5701 return kvm_emulate_instruction(vcpu, 0); 5702 5703 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); 5704 } 5705 5706 static int handle_ept_misconfig(struct kvm_vcpu *vcpu) 5707 { 5708 gpa_t gpa; 5709 5710 if (!vmx_can_emulate_instruction(vcpu, EMULTYPE_PF, NULL, 0)) 5711 return 1; 5712 5713 /* 5714 * A nested guest cannot optimize MMIO vmexits, because we have an 5715 * nGPA here instead of the required GPA. 5716 */ 5717 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); 5718 if (!is_guest_mode(vcpu) && 5719 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { 5720 trace_kvm_fast_mmio(gpa); 5721 return kvm_skip_emulated_instruction(vcpu); 5722 } 5723 5724 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); 5725 } 5726 5727 static int handle_nmi_window(struct kvm_vcpu *vcpu) 5728 { 5729 if (KVM_BUG_ON(!enable_vnmi, vcpu->kvm)) 5730 return -EIO; 5731 5732 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING); 5733 ++vcpu->stat.nmi_window_exits; 5734 kvm_make_request(KVM_REQ_EVENT, vcpu); 5735 5736 return 1; 5737 } 5738 5739 static bool vmx_emulation_required_with_pending_exception(struct kvm_vcpu *vcpu) 5740 { 5741 struct vcpu_vmx *vmx = to_vmx(vcpu); 5742 5743 return vmx->emulation_required && !vmx->rmode.vm86_active && 5744 (kvm_is_exception_pending(vcpu) || vcpu->arch.exception.injected); 5745 } 5746 5747 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) 5748 { 5749 struct vcpu_vmx *vmx = to_vmx(vcpu); 5750 bool intr_window_requested; 5751 unsigned count = 130; 5752 5753 intr_window_requested = exec_controls_get(vmx) & 5754 CPU_BASED_INTR_WINDOW_EXITING; 5755 5756 while (vmx->emulation_required && count-- != 0) { 5757 if (intr_window_requested && !vmx_interrupt_blocked(vcpu)) 5758 return handle_interrupt_window(&vmx->vcpu); 5759 5760 if (kvm_test_request(KVM_REQ_EVENT, vcpu)) 5761 return 1; 5762 5763 if (!kvm_emulate_instruction(vcpu, 0)) 5764 return 0; 5765 5766 if (vmx_emulation_required_with_pending_exception(vcpu)) { 5767 kvm_prepare_emulation_failure_exit(vcpu); 5768 return 0; 5769 } 5770 5771 if (vcpu->arch.halt_request) { 5772 vcpu->arch.halt_request = 0; 5773 return kvm_emulate_halt_noskip(vcpu); 5774 } 5775 5776 /* 5777 * Note, return 1 and not 0, vcpu_run() will invoke 5778 * xfer_to_guest_mode() which will create a proper return 5779 * code. 5780 */ 5781 if (__xfer_to_guest_mode_work_pending()) 5782 return 1; 5783 } 5784 5785 return 1; 5786 } 5787 5788 static int vmx_vcpu_pre_run(struct kvm_vcpu *vcpu) 5789 { 5790 if (vmx_emulation_required_with_pending_exception(vcpu)) { 5791 kvm_prepare_emulation_failure_exit(vcpu); 5792 return 0; 5793 } 5794 5795 return 1; 5796 } 5797 5798 static void grow_ple_window(struct kvm_vcpu *vcpu) 5799 { 5800 struct vcpu_vmx *vmx = to_vmx(vcpu); 5801 unsigned int old = vmx->ple_window; 5802 5803 vmx->ple_window = __grow_ple_window(old, ple_window, 5804 ple_window_grow, 5805 ple_window_max); 5806 5807 if (vmx->ple_window != old) { 5808 vmx->ple_window_dirty = true; 5809 trace_kvm_ple_window_update(vcpu->vcpu_id, 5810 vmx->ple_window, old); 5811 } 5812 } 5813 5814 static void shrink_ple_window(struct kvm_vcpu *vcpu) 5815 { 5816 struct vcpu_vmx *vmx = to_vmx(vcpu); 5817 unsigned int old = vmx->ple_window; 5818 5819 vmx->ple_window = __shrink_ple_window(old, ple_window, 5820 ple_window_shrink, 5821 ple_window); 5822 5823 if (vmx->ple_window != old) { 5824 vmx->ple_window_dirty = true; 5825 trace_kvm_ple_window_update(vcpu->vcpu_id, 5826 vmx->ple_window, old); 5827 } 5828 } 5829 5830 /* 5831 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE 5832 * exiting, so only get here on cpu with PAUSE-Loop-Exiting. 5833 */ 5834 static int handle_pause(struct kvm_vcpu *vcpu) 5835 { 5836 if (!kvm_pause_in_guest(vcpu->kvm)) 5837 grow_ple_window(vcpu); 5838 5839 /* 5840 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" 5841 * VM-execution control is ignored if CPL > 0. OTOH, KVM 5842 * never set PAUSE_EXITING and just set PLE if supported, 5843 * so the vcpu must be CPL=0 if it gets a PAUSE exit. 5844 */ 5845 kvm_vcpu_on_spin(vcpu, true); 5846 return kvm_skip_emulated_instruction(vcpu); 5847 } 5848 5849 static int handle_monitor_trap(struct kvm_vcpu *vcpu) 5850 { 5851 return 1; 5852 } 5853 5854 static int handle_invpcid(struct kvm_vcpu *vcpu) 5855 { 5856 u32 vmx_instruction_info; 5857 unsigned long type; 5858 gva_t gva; 5859 struct { 5860 u64 pcid; 5861 u64 gla; 5862 } operand; 5863 int gpr_index; 5864 5865 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { 5866 kvm_queue_exception(vcpu, UD_VECTOR); 5867 return 1; 5868 } 5869 5870 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); 5871 gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); 5872 type = kvm_register_read(vcpu, gpr_index); 5873 5874 /* According to the Intel instruction reference, the memory operand 5875 * is read even if it isn't needed (e.g., for type==all) 5876 */ 5877 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), 5878 vmx_instruction_info, false, 5879 sizeof(operand), &gva)) 5880 return 1; 5881 5882 return kvm_handle_invpcid(vcpu, type, gva); 5883 } 5884 5885 static int handle_pml_full(struct kvm_vcpu *vcpu) 5886 { 5887 unsigned long exit_qualification; 5888 5889 trace_kvm_pml_full(vcpu->vcpu_id); 5890 5891 exit_qualification = vmx_get_exit_qual(vcpu); 5892 5893 /* 5894 * PML buffer FULL happened while executing iret from NMI, 5895 * "blocked by NMI" bit has to be set before next VM entry. 5896 */ 5897 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && 5898 enable_vnmi && 5899 (exit_qualification & INTR_INFO_UNBLOCK_NMI)) 5900 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 5901 GUEST_INTR_STATE_NMI); 5902 5903 /* 5904 * PML buffer already flushed at beginning of VMEXIT. Nothing to do 5905 * here.., and there's no userspace involvement needed for PML. 5906 */ 5907 return 1; 5908 } 5909 5910 static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu) 5911 { 5912 struct vcpu_vmx *vmx = to_vmx(vcpu); 5913 5914 if (!vmx->req_immediate_exit && 5915 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) { 5916 kvm_lapic_expired_hv_timer(vcpu); 5917 return EXIT_FASTPATH_REENTER_GUEST; 5918 } 5919 5920 return EXIT_FASTPATH_NONE; 5921 } 5922 5923 static int handle_preemption_timer(struct kvm_vcpu *vcpu) 5924 { 5925 handle_fastpath_preemption_timer(vcpu); 5926 return 1; 5927 } 5928 5929 /* 5930 * When nested=0, all VMX instruction VM Exits filter here. The handlers 5931 * are overwritten by nested_vmx_setup() when nested=1. 5932 */ 5933 static int handle_vmx_instruction(struct kvm_vcpu *vcpu) 5934 { 5935 kvm_queue_exception(vcpu, UD_VECTOR); 5936 return 1; 5937 } 5938 5939 #ifndef CONFIG_X86_SGX_KVM 5940 static int handle_encls(struct kvm_vcpu *vcpu) 5941 { 5942 /* 5943 * SGX virtualization is disabled. There is no software enable bit for 5944 * SGX, so KVM intercepts all ENCLS leafs and injects a #UD to prevent 5945 * the guest from executing ENCLS (when SGX is supported by hardware). 5946 */ 5947 kvm_queue_exception(vcpu, UD_VECTOR); 5948 return 1; 5949 } 5950 #endif /* CONFIG_X86_SGX_KVM */ 5951 5952 static int handle_bus_lock_vmexit(struct kvm_vcpu *vcpu) 5953 { 5954 /* 5955 * Hardware may or may not set the BUS_LOCK_DETECTED flag on BUS_LOCK 5956 * VM-Exits. Unconditionally set the flag here and leave the handling to 5957 * vmx_handle_exit(). 5958 */ 5959 to_vmx(vcpu)->exit_reason.bus_lock_detected = true; 5960 return 1; 5961 } 5962 5963 static int handle_notify(struct kvm_vcpu *vcpu) 5964 { 5965 unsigned long exit_qual = vmx_get_exit_qual(vcpu); 5966 bool context_invalid = exit_qual & NOTIFY_VM_CONTEXT_INVALID; 5967 5968 ++vcpu->stat.notify_window_exits; 5969 5970 /* 5971 * Notify VM exit happened while executing iret from NMI, 5972 * "blocked by NMI" bit has to be set before next VM entry. 5973 */ 5974 if (enable_vnmi && (exit_qual & INTR_INFO_UNBLOCK_NMI)) 5975 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 5976 GUEST_INTR_STATE_NMI); 5977 5978 if (vcpu->kvm->arch.notify_vmexit_flags & KVM_X86_NOTIFY_VMEXIT_USER || 5979 context_invalid) { 5980 vcpu->run->exit_reason = KVM_EXIT_NOTIFY; 5981 vcpu->run->notify.flags = context_invalid ? 5982 KVM_NOTIFY_CONTEXT_INVALID : 0; 5983 return 0; 5984 } 5985 5986 return 1; 5987 } 5988 5989 /* 5990 * The exit handlers return 1 if the exit was handled fully and guest execution 5991 * may resume. Otherwise they set the kvm_run parameter to indicate what needs 5992 * to be done to userspace and return 0. 5993 */ 5994 static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { 5995 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi, 5996 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, 5997 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, 5998 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, 5999 [EXIT_REASON_IO_INSTRUCTION] = handle_io, 6000 [EXIT_REASON_CR_ACCESS] = handle_cr, 6001 [EXIT_REASON_DR_ACCESS] = handle_dr, 6002 [EXIT_REASON_CPUID] = kvm_emulate_cpuid, 6003 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr, 6004 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr, 6005 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window, 6006 [EXIT_REASON_HLT] = kvm_emulate_halt, 6007 [EXIT_REASON_INVD] = kvm_emulate_invd, 6008 [EXIT_REASON_INVLPG] = handle_invlpg, 6009 [EXIT_REASON_RDPMC] = kvm_emulate_rdpmc, 6010 [EXIT_REASON_VMCALL] = kvm_emulate_hypercall, 6011 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction, 6012 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction, 6013 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction, 6014 [EXIT_REASON_VMPTRST] = handle_vmx_instruction, 6015 [EXIT_REASON_VMREAD] = handle_vmx_instruction, 6016 [EXIT_REASON_VMRESUME] = handle_vmx_instruction, 6017 [EXIT_REASON_VMWRITE] = handle_vmx_instruction, 6018 [EXIT_REASON_VMOFF] = handle_vmx_instruction, 6019 [EXIT_REASON_VMON] = handle_vmx_instruction, 6020 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, 6021 [EXIT_REASON_APIC_ACCESS] = handle_apic_access, 6022 [EXIT_REASON_APIC_WRITE] = handle_apic_write, 6023 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, 6024 [EXIT_REASON_WBINVD] = kvm_emulate_wbinvd, 6025 [EXIT_REASON_XSETBV] = kvm_emulate_xsetbv, 6026 [EXIT_REASON_TASK_SWITCH] = handle_task_switch, 6027 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, 6028 [EXIT_REASON_GDTR_IDTR] = handle_desc, 6029 [EXIT_REASON_LDTR_TR] = handle_desc, 6030 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, 6031 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, 6032 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, 6033 [EXIT_REASON_MWAIT_INSTRUCTION] = kvm_emulate_mwait, 6034 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, 6035 [EXIT_REASON_MONITOR_INSTRUCTION] = kvm_emulate_monitor, 6036 [EXIT_REASON_INVEPT] = handle_vmx_instruction, 6037 [EXIT_REASON_INVVPID] = handle_vmx_instruction, 6038 [EXIT_REASON_RDRAND] = kvm_handle_invalid_op, 6039 [EXIT_REASON_RDSEED] = kvm_handle_invalid_op, 6040 [EXIT_REASON_PML_FULL] = handle_pml_full, 6041 [EXIT_REASON_INVPCID] = handle_invpcid, 6042 [EXIT_REASON_VMFUNC] = handle_vmx_instruction, 6043 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, 6044 [EXIT_REASON_ENCLS] = handle_encls, 6045 [EXIT_REASON_BUS_LOCK] = handle_bus_lock_vmexit, 6046 [EXIT_REASON_NOTIFY] = handle_notify, 6047 }; 6048 6049 static const int kvm_vmx_max_exit_handlers = 6050 ARRAY_SIZE(kvm_vmx_exit_handlers); 6051 6052 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, 6053 u64 *info1, u64 *info2, 6054 u32 *intr_info, u32 *error_code) 6055 { 6056 struct vcpu_vmx *vmx = to_vmx(vcpu); 6057 6058 *reason = vmx->exit_reason.full; 6059 *info1 = vmx_get_exit_qual(vcpu); 6060 if (!(vmx->exit_reason.failed_vmentry)) { 6061 *info2 = vmx->idt_vectoring_info; 6062 *intr_info = vmx_get_intr_info(vcpu); 6063 if (is_exception_with_error_code(*intr_info)) 6064 *error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); 6065 else 6066 *error_code = 0; 6067 } else { 6068 *info2 = 0; 6069 *intr_info = 0; 6070 *error_code = 0; 6071 } 6072 } 6073 6074 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) 6075 { 6076 if (vmx->pml_pg) { 6077 __free_page(vmx->pml_pg); 6078 vmx->pml_pg = NULL; 6079 } 6080 } 6081 6082 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) 6083 { 6084 struct vcpu_vmx *vmx = to_vmx(vcpu); 6085 u64 *pml_buf; 6086 u16 pml_idx; 6087 6088 pml_idx = vmcs_read16(GUEST_PML_INDEX); 6089 6090 /* Do nothing if PML buffer is empty */ 6091 if (pml_idx == (PML_ENTITY_NUM - 1)) 6092 return; 6093 6094 /* PML index always points to next available PML buffer entity */ 6095 if (pml_idx >= PML_ENTITY_NUM) 6096 pml_idx = 0; 6097 else 6098 pml_idx++; 6099 6100 pml_buf = page_address(vmx->pml_pg); 6101 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { 6102 u64 gpa; 6103 6104 gpa = pml_buf[pml_idx]; 6105 WARN_ON(gpa & (PAGE_SIZE - 1)); 6106 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); 6107 } 6108 6109 /* reset PML index */ 6110 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); 6111 } 6112 6113 static void vmx_dump_sel(char *name, uint32_t sel) 6114 { 6115 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", 6116 name, vmcs_read16(sel), 6117 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), 6118 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), 6119 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); 6120 } 6121 6122 static void vmx_dump_dtsel(char *name, uint32_t limit) 6123 { 6124 pr_err("%s limit=0x%08x, base=0x%016lx\n", 6125 name, vmcs_read32(limit), 6126 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); 6127 } 6128 6129 static void vmx_dump_msrs(char *name, struct vmx_msrs *m) 6130 { 6131 unsigned int i; 6132 struct vmx_msr_entry *e; 6133 6134 pr_err("MSR %s:\n", name); 6135 for (i = 0, e = m->val; i < m->nr; ++i, ++e) 6136 pr_err(" %2d: msr=0x%08x value=0x%016llx\n", i, e->index, e->value); 6137 } 6138 6139 void dump_vmcs(struct kvm_vcpu *vcpu) 6140 { 6141 struct vcpu_vmx *vmx = to_vmx(vcpu); 6142 u32 vmentry_ctl, vmexit_ctl; 6143 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control; 6144 u64 tertiary_exec_control; 6145 unsigned long cr4; 6146 int efer_slot; 6147 6148 if (!dump_invalid_vmcs) { 6149 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n"); 6150 return; 6151 } 6152 6153 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); 6154 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); 6155 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); 6156 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); 6157 cr4 = vmcs_readl(GUEST_CR4); 6158 6159 if (cpu_has_secondary_exec_ctrls()) 6160 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); 6161 else 6162 secondary_exec_control = 0; 6163 6164 if (cpu_has_tertiary_exec_ctrls()) 6165 tertiary_exec_control = vmcs_read64(TERTIARY_VM_EXEC_CONTROL); 6166 else 6167 tertiary_exec_control = 0; 6168 6169 pr_err("VMCS %p, last attempted VM-entry on CPU %d\n", 6170 vmx->loaded_vmcs->vmcs, vcpu->arch.last_vmentry_cpu); 6171 pr_err("*** Guest State ***\n"); 6172 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", 6173 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), 6174 vmcs_readl(CR0_GUEST_HOST_MASK)); 6175 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", 6176 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); 6177 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); 6178 if (cpu_has_vmx_ept()) { 6179 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", 6180 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); 6181 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", 6182 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); 6183 } 6184 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", 6185 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); 6186 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", 6187 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); 6188 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", 6189 vmcs_readl(GUEST_SYSENTER_ESP), 6190 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); 6191 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); 6192 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); 6193 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); 6194 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); 6195 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); 6196 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); 6197 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); 6198 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); 6199 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); 6200 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); 6201 efer_slot = vmx_find_loadstore_msr_slot(&vmx->msr_autoload.guest, MSR_EFER); 6202 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_EFER) 6203 pr_err("EFER= 0x%016llx\n", vmcs_read64(GUEST_IA32_EFER)); 6204 else if (efer_slot >= 0) 6205 pr_err("EFER= 0x%016llx (autoload)\n", 6206 vmx->msr_autoload.guest.val[efer_slot].value); 6207 else if (vmentry_ctl & VM_ENTRY_IA32E_MODE) 6208 pr_err("EFER= 0x%016llx (effective)\n", 6209 vcpu->arch.efer | (EFER_LMA | EFER_LME)); 6210 else 6211 pr_err("EFER= 0x%016llx (effective)\n", 6212 vcpu->arch.efer & ~(EFER_LMA | EFER_LME)); 6213 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PAT) 6214 pr_err("PAT = 0x%016llx\n", vmcs_read64(GUEST_IA32_PAT)); 6215 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", 6216 vmcs_read64(GUEST_IA32_DEBUGCTL), 6217 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); 6218 if (cpu_has_load_perf_global_ctrl() && 6219 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) 6220 pr_err("PerfGlobCtl = 0x%016llx\n", 6221 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); 6222 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) 6223 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); 6224 pr_err("Interruptibility = %08x ActivityState = %08x\n", 6225 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), 6226 vmcs_read32(GUEST_ACTIVITY_STATE)); 6227 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) 6228 pr_err("InterruptStatus = %04x\n", 6229 vmcs_read16(GUEST_INTR_STATUS)); 6230 if (vmcs_read32(VM_ENTRY_MSR_LOAD_COUNT) > 0) 6231 vmx_dump_msrs("guest autoload", &vmx->msr_autoload.guest); 6232 if (vmcs_read32(VM_EXIT_MSR_STORE_COUNT) > 0) 6233 vmx_dump_msrs("guest autostore", &vmx->msr_autostore.guest); 6234 6235 pr_err("*** Host State ***\n"); 6236 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", 6237 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); 6238 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", 6239 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), 6240 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), 6241 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), 6242 vmcs_read16(HOST_TR_SELECTOR)); 6243 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", 6244 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), 6245 vmcs_readl(HOST_TR_BASE)); 6246 pr_err("GDTBase=%016lx IDTBase=%016lx\n", 6247 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); 6248 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", 6249 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), 6250 vmcs_readl(HOST_CR4)); 6251 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", 6252 vmcs_readl(HOST_IA32_SYSENTER_ESP), 6253 vmcs_read32(HOST_IA32_SYSENTER_CS), 6254 vmcs_readl(HOST_IA32_SYSENTER_EIP)); 6255 if (vmexit_ctl & VM_EXIT_LOAD_IA32_EFER) 6256 pr_err("EFER= 0x%016llx\n", vmcs_read64(HOST_IA32_EFER)); 6257 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PAT) 6258 pr_err("PAT = 0x%016llx\n", vmcs_read64(HOST_IA32_PAT)); 6259 if (cpu_has_load_perf_global_ctrl() && 6260 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) 6261 pr_err("PerfGlobCtl = 0x%016llx\n", 6262 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); 6263 if (vmcs_read32(VM_EXIT_MSR_LOAD_COUNT) > 0) 6264 vmx_dump_msrs("host autoload", &vmx->msr_autoload.host); 6265 6266 pr_err("*** Control State ***\n"); 6267 pr_err("CPUBased=0x%08x SecondaryExec=0x%08x TertiaryExec=0x%016llx\n", 6268 cpu_based_exec_ctrl, secondary_exec_control, tertiary_exec_control); 6269 pr_err("PinBased=0x%08x EntryControls=%08x ExitControls=%08x\n", 6270 pin_based_exec_ctrl, vmentry_ctl, vmexit_ctl); 6271 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", 6272 vmcs_read32(EXCEPTION_BITMAP), 6273 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), 6274 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); 6275 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", 6276 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), 6277 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), 6278 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); 6279 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", 6280 vmcs_read32(VM_EXIT_INTR_INFO), 6281 vmcs_read32(VM_EXIT_INTR_ERROR_CODE), 6282 vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); 6283 pr_err(" reason=%08x qualification=%016lx\n", 6284 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); 6285 pr_err("IDTVectoring: info=%08x errcode=%08x\n", 6286 vmcs_read32(IDT_VECTORING_INFO_FIELD), 6287 vmcs_read32(IDT_VECTORING_ERROR_CODE)); 6288 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); 6289 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) 6290 pr_err("TSC Multiplier = 0x%016llx\n", 6291 vmcs_read64(TSC_MULTIPLIER)); 6292 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) { 6293 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { 6294 u16 status = vmcs_read16(GUEST_INTR_STATUS); 6295 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff); 6296 } 6297 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); 6298 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) 6299 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR)); 6300 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR)); 6301 } 6302 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) 6303 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); 6304 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) 6305 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); 6306 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) 6307 pr_err("PLE Gap=%08x Window=%08x\n", 6308 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); 6309 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) 6310 pr_err("Virtual processor ID = 0x%04x\n", 6311 vmcs_read16(VIRTUAL_PROCESSOR_ID)); 6312 } 6313 6314 /* 6315 * The guest has exited. See if we can fix it or if we need userspace 6316 * assistance. 6317 */ 6318 static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) 6319 { 6320 struct vcpu_vmx *vmx = to_vmx(vcpu); 6321 union vmx_exit_reason exit_reason = vmx->exit_reason; 6322 u32 vectoring_info = vmx->idt_vectoring_info; 6323 u16 exit_handler_index; 6324 6325 /* 6326 * Flush logged GPAs PML buffer, this will make dirty_bitmap more 6327 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before 6328 * querying dirty_bitmap, we only need to kick all vcpus out of guest 6329 * mode as if vcpus is in root mode, the PML buffer must has been 6330 * flushed already. Note, PML is never enabled in hardware while 6331 * running L2. 6332 */ 6333 if (enable_pml && !is_guest_mode(vcpu)) 6334 vmx_flush_pml_buffer(vcpu); 6335 6336 /* 6337 * KVM should never reach this point with a pending nested VM-Enter. 6338 * More specifically, short-circuiting VM-Entry to emulate L2 due to 6339 * invalid guest state should never happen as that means KVM knowingly 6340 * allowed a nested VM-Enter with an invalid vmcs12. More below. 6341 */ 6342 if (KVM_BUG_ON(vmx->nested.nested_run_pending, vcpu->kvm)) 6343 return -EIO; 6344 6345 if (is_guest_mode(vcpu)) { 6346 /* 6347 * PML is never enabled when running L2, bail immediately if a 6348 * PML full exit occurs as something is horribly wrong. 6349 */ 6350 if (exit_reason.basic == EXIT_REASON_PML_FULL) 6351 goto unexpected_vmexit; 6352 6353 /* 6354 * The host physical addresses of some pages of guest memory 6355 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC 6356 * Page). The CPU may write to these pages via their host 6357 * physical address while L2 is running, bypassing any 6358 * address-translation-based dirty tracking (e.g. EPT write 6359 * protection). 6360 * 6361 * Mark them dirty on every exit from L2 to prevent them from 6362 * getting out of sync with dirty tracking. 6363 */ 6364 nested_mark_vmcs12_pages_dirty(vcpu); 6365 6366 /* 6367 * Synthesize a triple fault if L2 state is invalid. In normal 6368 * operation, nested VM-Enter rejects any attempt to enter L2 6369 * with invalid state. However, those checks are skipped if 6370 * state is being stuffed via RSM or KVM_SET_NESTED_STATE. If 6371 * L2 state is invalid, it means either L1 modified SMRAM state 6372 * or userspace provided bad state. Synthesize TRIPLE_FAULT as 6373 * doing so is architecturally allowed in the RSM case, and is 6374 * the least awful solution for the userspace case without 6375 * risking false positives. 6376 */ 6377 if (vmx->emulation_required) { 6378 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); 6379 return 1; 6380 } 6381 6382 if (nested_vmx_reflect_vmexit(vcpu)) 6383 return 1; 6384 } 6385 6386 /* If guest state is invalid, start emulating. L2 is handled above. */ 6387 if (vmx->emulation_required) 6388 return handle_invalid_guest_state(vcpu); 6389 6390 if (exit_reason.failed_vmentry) { 6391 dump_vmcs(vcpu); 6392 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; 6393 vcpu->run->fail_entry.hardware_entry_failure_reason 6394 = exit_reason.full; 6395 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; 6396 return 0; 6397 } 6398 6399 if (unlikely(vmx->fail)) { 6400 dump_vmcs(vcpu); 6401 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; 6402 vcpu->run->fail_entry.hardware_entry_failure_reason 6403 = vmcs_read32(VM_INSTRUCTION_ERROR); 6404 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; 6405 return 0; 6406 } 6407 6408 /* 6409 * Note: 6410 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by 6411 * delivery event since it indicates guest is accessing MMIO. 6412 * The vm-exit can be triggered again after return to guest that 6413 * will cause infinite loop. 6414 */ 6415 if ((vectoring_info & VECTORING_INFO_VALID_MASK) && 6416 (exit_reason.basic != EXIT_REASON_EXCEPTION_NMI && 6417 exit_reason.basic != EXIT_REASON_EPT_VIOLATION && 6418 exit_reason.basic != EXIT_REASON_PML_FULL && 6419 exit_reason.basic != EXIT_REASON_APIC_ACCESS && 6420 exit_reason.basic != EXIT_REASON_TASK_SWITCH && 6421 exit_reason.basic != EXIT_REASON_NOTIFY)) { 6422 int ndata = 3; 6423 6424 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 6425 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; 6426 vcpu->run->internal.data[0] = vectoring_info; 6427 vcpu->run->internal.data[1] = exit_reason.full; 6428 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; 6429 if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG) { 6430 vcpu->run->internal.data[ndata++] = 6431 vmcs_read64(GUEST_PHYSICAL_ADDRESS); 6432 } 6433 vcpu->run->internal.data[ndata++] = vcpu->arch.last_vmentry_cpu; 6434 vcpu->run->internal.ndata = ndata; 6435 return 0; 6436 } 6437 6438 if (unlikely(!enable_vnmi && 6439 vmx->loaded_vmcs->soft_vnmi_blocked)) { 6440 if (!vmx_interrupt_blocked(vcpu)) { 6441 vmx->loaded_vmcs->soft_vnmi_blocked = 0; 6442 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && 6443 vcpu->arch.nmi_pending) { 6444 /* 6445 * This CPU don't support us in finding the end of an 6446 * NMI-blocked window if the guest runs with IRQs 6447 * disabled. So we pull the trigger after 1 s of 6448 * futile waiting, but inform the user about this. 6449 */ 6450 printk(KERN_WARNING "%s: Breaking out of NMI-blocked " 6451 "state on VCPU %d after 1 s timeout\n", 6452 __func__, vcpu->vcpu_id); 6453 vmx->loaded_vmcs->soft_vnmi_blocked = 0; 6454 } 6455 } 6456 6457 if (exit_fastpath != EXIT_FASTPATH_NONE) 6458 return 1; 6459 6460 if (exit_reason.basic >= kvm_vmx_max_exit_handlers) 6461 goto unexpected_vmexit; 6462 #ifdef CONFIG_RETPOLINE 6463 if (exit_reason.basic == EXIT_REASON_MSR_WRITE) 6464 return kvm_emulate_wrmsr(vcpu); 6465 else if (exit_reason.basic == EXIT_REASON_PREEMPTION_TIMER) 6466 return handle_preemption_timer(vcpu); 6467 else if (exit_reason.basic == EXIT_REASON_INTERRUPT_WINDOW) 6468 return handle_interrupt_window(vcpu); 6469 else if (exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT) 6470 return handle_external_interrupt(vcpu); 6471 else if (exit_reason.basic == EXIT_REASON_HLT) 6472 return kvm_emulate_halt(vcpu); 6473 else if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG) 6474 return handle_ept_misconfig(vcpu); 6475 #endif 6476 6477 exit_handler_index = array_index_nospec((u16)exit_reason.basic, 6478 kvm_vmx_max_exit_handlers); 6479 if (!kvm_vmx_exit_handlers[exit_handler_index]) 6480 goto unexpected_vmexit; 6481 6482 return kvm_vmx_exit_handlers[exit_handler_index](vcpu); 6483 6484 unexpected_vmexit: 6485 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", 6486 exit_reason.full); 6487 dump_vmcs(vcpu); 6488 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 6489 vcpu->run->internal.suberror = 6490 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON; 6491 vcpu->run->internal.ndata = 2; 6492 vcpu->run->internal.data[0] = exit_reason.full; 6493 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; 6494 return 0; 6495 } 6496 6497 static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) 6498 { 6499 int ret = __vmx_handle_exit(vcpu, exit_fastpath); 6500 6501 /* 6502 * Exit to user space when bus lock detected to inform that there is 6503 * a bus lock in guest. 6504 */ 6505 if (to_vmx(vcpu)->exit_reason.bus_lock_detected) { 6506 if (ret > 0) 6507 vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK; 6508 6509 vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK; 6510 return 0; 6511 } 6512 return ret; 6513 } 6514 6515 /* 6516 * Software based L1D cache flush which is used when microcode providing 6517 * the cache control MSR is not loaded. 6518 * 6519 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to 6520 * flush it is required to read in 64 KiB because the replacement algorithm 6521 * is not exactly LRU. This could be sized at runtime via topology 6522 * information but as all relevant affected CPUs have 32KiB L1D cache size 6523 * there is no point in doing so. 6524 */ 6525 static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu) 6526 { 6527 int size = PAGE_SIZE << L1D_CACHE_ORDER; 6528 6529 /* 6530 * This code is only executed when the flush mode is 'cond' or 6531 * 'always' 6532 */ 6533 if (static_branch_likely(&vmx_l1d_flush_cond)) { 6534 bool flush_l1d; 6535 6536 /* 6537 * Clear the per-vcpu flush bit, it gets set again 6538 * either from vcpu_run() or from one of the unsafe 6539 * VMEXIT handlers. 6540 */ 6541 flush_l1d = vcpu->arch.l1tf_flush_l1d; 6542 vcpu->arch.l1tf_flush_l1d = false; 6543 6544 /* 6545 * Clear the per-cpu flush bit, it gets set again from 6546 * the interrupt handlers. 6547 */ 6548 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d(); 6549 kvm_clear_cpu_l1tf_flush_l1d(); 6550 6551 if (!flush_l1d) 6552 return; 6553 } 6554 6555 vcpu->stat.l1d_flush++; 6556 6557 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { 6558 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); 6559 return; 6560 } 6561 6562 asm volatile( 6563 /* First ensure the pages are in the TLB */ 6564 "xorl %%eax, %%eax\n" 6565 ".Lpopulate_tlb:\n\t" 6566 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" 6567 "addl $4096, %%eax\n\t" 6568 "cmpl %%eax, %[size]\n\t" 6569 "jne .Lpopulate_tlb\n\t" 6570 "xorl %%eax, %%eax\n\t" 6571 "cpuid\n\t" 6572 /* Now fill the cache */ 6573 "xorl %%eax, %%eax\n" 6574 ".Lfill_cache:\n" 6575 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" 6576 "addl $64, %%eax\n\t" 6577 "cmpl %%eax, %[size]\n\t" 6578 "jne .Lfill_cache\n\t" 6579 "lfence\n" 6580 :: [flush_pages] "r" (vmx_l1d_flush_pages), 6581 [size] "r" (size) 6582 : "eax", "ebx", "ecx", "edx"); 6583 } 6584 6585 static void vmx_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) 6586 { 6587 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 6588 int tpr_threshold; 6589 6590 if (is_guest_mode(vcpu) && 6591 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) 6592 return; 6593 6594 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr; 6595 if (is_guest_mode(vcpu)) 6596 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold; 6597 else 6598 vmcs_write32(TPR_THRESHOLD, tpr_threshold); 6599 } 6600 6601 void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) 6602 { 6603 struct vcpu_vmx *vmx = to_vmx(vcpu); 6604 u32 sec_exec_control; 6605 6606 if (!lapic_in_kernel(vcpu)) 6607 return; 6608 6609 if (!flexpriority_enabled && 6610 !cpu_has_vmx_virtualize_x2apic_mode()) 6611 return; 6612 6613 /* Postpone execution until vmcs01 is the current VMCS. */ 6614 if (is_guest_mode(vcpu)) { 6615 vmx->nested.change_vmcs01_virtual_apic_mode = true; 6616 return; 6617 } 6618 6619 sec_exec_control = secondary_exec_controls_get(vmx); 6620 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | 6621 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); 6622 6623 switch (kvm_get_apic_mode(vcpu)) { 6624 case LAPIC_MODE_INVALID: 6625 WARN_ONCE(true, "Invalid local APIC state"); 6626 break; 6627 case LAPIC_MODE_DISABLED: 6628 break; 6629 case LAPIC_MODE_XAPIC: 6630 if (flexpriority_enabled) { 6631 sec_exec_control |= 6632 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; 6633 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); 6634 6635 /* 6636 * Flush the TLB, reloading the APIC access page will 6637 * only do so if its physical address has changed, but 6638 * the guest may have inserted a non-APIC mapping into 6639 * the TLB while the APIC access page was disabled. 6640 */ 6641 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); 6642 } 6643 break; 6644 case LAPIC_MODE_X2APIC: 6645 if (cpu_has_vmx_virtualize_x2apic_mode()) 6646 sec_exec_control |= 6647 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; 6648 break; 6649 } 6650 secondary_exec_controls_set(vmx, sec_exec_control); 6651 6652 vmx_update_msr_bitmap_x2apic(vcpu); 6653 } 6654 6655 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) 6656 { 6657 struct page *page; 6658 6659 /* Defer reload until vmcs01 is the current VMCS. */ 6660 if (is_guest_mode(vcpu)) { 6661 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true; 6662 return; 6663 } 6664 6665 if (!(secondary_exec_controls_get(to_vmx(vcpu)) & 6666 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) 6667 return; 6668 6669 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); 6670 if (is_error_page(page)) 6671 return; 6672 6673 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page)); 6674 vmx_flush_tlb_current(vcpu); 6675 6676 /* 6677 * Do not pin apic access page in memory, the MMU notifier 6678 * will call us again if it is migrated or swapped out. 6679 */ 6680 put_page(page); 6681 } 6682 6683 static void vmx_hwapic_isr_update(int max_isr) 6684 { 6685 u16 status; 6686 u8 old; 6687 6688 if (max_isr == -1) 6689 max_isr = 0; 6690 6691 status = vmcs_read16(GUEST_INTR_STATUS); 6692 old = status >> 8; 6693 if (max_isr != old) { 6694 status &= 0xff; 6695 status |= max_isr << 8; 6696 vmcs_write16(GUEST_INTR_STATUS, status); 6697 } 6698 } 6699 6700 static void vmx_set_rvi(int vector) 6701 { 6702 u16 status; 6703 u8 old; 6704 6705 if (vector == -1) 6706 vector = 0; 6707 6708 status = vmcs_read16(GUEST_INTR_STATUS); 6709 old = (u8)status & 0xff; 6710 if ((u8)vector != old) { 6711 status &= ~0xff; 6712 status |= (u8)vector; 6713 vmcs_write16(GUEST_INTR_STATUS, status); 6714 } 6715 } 6716 6717 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) 6718 { 6719 /* 6720 * When running L2, updating RVI is only relevant when 6721 * vmcs12 virtual-interrupt-delivery enabled. 6722 * However, it can be enabled only when L1 also 6723 * intercepts external-interrupts and in that case 6724 * we should not update vmcs02 RVI but instead intercept 6725 * interrupt. Therefore, do nothing when running L2. 6726 */ 6727 if (!is_guest_mode(vcpu)) 6728 vmx_set_rvi(max_irr); 6729 } 6730 6731 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) 6732 { 6733 struct vcpu_vmx *vmx = to_vmx(vcpu); 6734 int max_irr; 6735 bool got_posted_interrupt; 6736 6737 if (KVM_BUG_ON(!enable_apicv, vcpu->kvm)) 6738 return -EIO; 6739 6740 if (pi_test_on(&vmx->pi_desc)) { 6741 pi_clear_on(&vmx->pi_desc); 6742 /* 6743 * IOMMU can write to PID.ON, so the barrier matters even on UP. 6744 * But on x86 this is just a compiler barrier anyway. 6745 */ 6746 smp_mb__after_atomic(); 6747 got_posted_interrupt = 6748 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); 6749 } else { 6750 max_irr = kvm_lapic_find_highest_irr(vcpu); 6751 got_posted_interrupt = false; 6752 } 6753 6754 /* 6755 * Newly recognized interrupts are injected via either virtual interrupt 6756 * delivery (RVI) or KVM_REQ_EVENT. Virtual interrupt delivery is 6757 * disabled in two cases: 6758 * 6759 * 1) If L2 is running and the vCPU has a new pending interrupt. If L1 6760 * wants to exit on interrupts, KVM_REQ_EVENT is needed to synthesize a 6761 * VM-Exit to L1. If L1 doesn't want to exit, the interrupt is injected 6762 * into L2, but KVM doesn't use virtual interrupt delivery to inject 6763 * interrupts into L2, and so KVM_REQ_EVENT is again needed. 6764 * 6765 * 2) If APICv is disabled for this vCPU, assigned devices may still 6766 * attempt to post interrupts. The posted interrupt vector will cause 6767 * a VM-Exit and the subsequent entry will call sync_pir_to_irr. 6768 */ 6769 if (!is_guest_mode(vcpu) && kvm_vcpu_apicv_active(vcpu)) 6770 vmx_set_rvi(max_irr); 6771 else if (got_posted_interrupt) 6772 kvm_make_request(KVM_REQ_EVENT, vcpu); 6773 6774 return max_irr; 6775 } 6776 6777 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) 6778 { 6779 if (!kvm_vcpu_apicv_active(vcpu)) 6780 return; 6781 6782 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); 6783 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); 6784 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); 6785 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); 6786 } 6787 6788 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) 6789 { 6790 struct vcpu_vmx *vmx = to_vmx(vcpu); 6791 6792 pi_clear_on(&vmx->pi_desc); 6793 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); 6794 } 6795 6796 void vmx_do_interrupt_nmi_irqoff(unsigned long entry); 6797 6798 static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, 6799 unsigned long entry) 6800 { 6801 bool is_nmi = entry == (unsigned long)asm_exc_nmi_noist; 6802 6803 kvm_before_interrupt(vcpu, is_nmi ? KVM_HANDLING_NMI : KVM_HANDLING_IRQ); 6804 vmx_do_interrupt_nmi_irqoff(entry); 6805 kvm_after_interrupt(vcpu); 6806 } 6807 6808 static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu) 6809 { 6810 /* 6811 * Save xfd_err to guest_fpu before interrupt is enabled, so the 6812 * MSR value is not clobbered by the host activity before the guest 6813 * has chance to consume it. 6814 * 6815 * Do not blindly read xfd_err here, since this exception might 6816 * be caused by L1 interception on a platform which doesn't 6817 * support xfd at all. 6818 * 6819 * Do it conditionally upon guest_fpu::xfd. xfd_err matters 6820 * only when xfd contains a non-zero value. 6821 * 6822 * Queuing exception is done in vmx_handle_exit. See comment there. 6823 */ 6824 if (vcpu->arch.guest_fpu.fpstate->xfd) 6825 rdmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); 6826 } 6827 6828 static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx) 6829 { 6830 const unsigned long nmi_entry = (unsigned long)asm_exc_nmi_noist; 6831 u32 intr_info = vmx_get_intr_info(&vmx->vcpu); 6832 6833 /* if exit due to PF check for async PF */ 6834 if (is_page_fault(intr_info)) 6835 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags(); 6836 /* if exit due to NM, handle before interrupts are enabled */ 6837 else if (is_nm_fault(intr_info)) 6838 handle_nm_fault_irqoff(&vmx->vcpu); 6839 /* Handle machine checks before interrupts are enabled */ 6840 else if (is_machine_check(intr_info)) 6841 kvm_machine_check(); 6842 /* We need to handle NMIs before interrupts are enabled */ 6843 else if (is_nmi(intr_info)) 6844 handle_interrupt_nmi_irqoff(&vmx->vcpu, nmi_entry); 6845 } 6846 6847 static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu) 6848 { 6849 u32 intr_info = vmx_get_intr_info(vcpu); 6850 unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK; 6851 gate_desc *desc = (gate_desc *)host_idt_base + vector; 6852 6853 if (KVM_BUG(!is_external_intr(intr_info), vcpu->kvm, 6854 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info)) 6855 return; 6856 6857 handle_interrupt_nmi_irqoff(vcpu, gate_offset(desc)); 6858 vcpu->arch.at_instruction_boundary = true; 6859 } 6860 6861 static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu) 6862 { 6863 struct vcpu_vmx *vmx = to_vmx(vcpu); 6864 6865 if (vmx->emulation_required) 6866 return; 6867 6868 if (vmx->exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT) 6869 handle_external_interrupt_irqoff(vcpu); 6870 else if (vmx->exit_reason.basic == EXIT_REASON_EXCEPTION_NMI) 6871 handle_exception_nmi_irqoff(vmx); 6872 } 6873 6874 /* 6875 * The kvm parameter can be NULL (module initialization, or invocation before 6876 * VM creation). Be sure to check the kvm parameter before using it. 6877 */ 6878 static bool vmx_has_emulated_msr(struct kvm *kvm, u32 index) 6879 { 6880 switch (index) { 6881 case MSR_IA32_SMBASE: 6882 if (!IS_ENABLED(CONFIG_KVM_SMM)) 6883 return false; 6884 /* 6885 * We cannot do SMM unless we can run the guest in big 6886 * real mode. 6887 */ 6888 return enable_unrestricted_guest || emulate_invalid_guest_state; 6889 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: 6890 return nested; 6891 case MSR_AMD64_VIRT_SPEC_CTRL: 6892 case MSR_AMD64_TSC_RATIO: 6893 /* This is AMD only. */ 6894 return false; 6895 default: 6896 return true; 6897 } 6898 } 6899 6900 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) 6901 { 6902 u32 exit_intr_info; 6903 bool unblock_nmi; 6904 u8 vector; 6905 bool idtv_info_valid; 6906 6907 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; 6908 6909 if (enable_vnmi) { 6910 if (vmx->loaded_vmcs->nmi_known_unmasked) 6911 return; 6912 6913 exit_intr_info = vmx_get_intr_info(&vmx->vcpu); 6914 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; 6915 vector = exit_intr_info & INTR_INFO_VECTOR_MASK; 6916 /* 6917 * SDM 3: 27.7.1.2 (September 2008) 6918 * Re-set bit "block by NMI" before VM entry if vmexit caused by 6919 * a guest IRET fault. 6920 * SDM 3: 23.2.2 (September 2008) 6921 * Bit 12 is undefined in any of the following cases: 6922 * If the VM exit sets the valid bit in the IDT-vectoring 6923 * information field. 6924 * If the VM exit is due to a double fault. 6925 */ 6926 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && 6927 vector != DF_VECTOR && !idtv_info_valid) 6928 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 6929 GUEST_INTR_STATE_NMI); 6930 else 6931 vmx->loaded_vmcs->nmi_known_unmasked = 6932 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) 6933 & GUEST_INTR_STATE_NMI); 6934 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) 6935 vmx->loaded_vmcs->vnmi_blocked_time += 6936 ktime_to_ns(ktime_sub(ktime_get(), 6937 vmx->loaded_vmcs->entry_time)); 6938 } 6939 6940 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, 6941 u32 idt_vectoring_info, 6942 int instr_len_field, 6943 int error_code_field) 6944 { 6945 u8 vector; 6946 int type; 6947 bool idtv_info_valid; 6948 6949 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; 6950 6951 vcpu->arch.nmi_injected = false; 6952 kvm_clear_exception_queue(vcpu); 6953 kvm_clear_interrupt_queue(vcpu); 6954 6955 if (!idtv_info_valid) 6956 return; 6957 6958 kvm_make_request(KVM_REQ_EVENT, vcpu); 6959 6960 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; 6961 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; 6962 6963 switch (type) { 6964 case INTR_TYPE_NMI_INTR: 6965 vcpu->arch.nmi_injected = true; 6966 /* 6967 * SDM 3: 27.7.1.2 (September 2008) 6968 * Clear bit "block by NMI" before VM entry if a NMI 6969 * delivery faulted. 6970 */ 6971 vmx_set_nmi_mask(vcpu, false); 6972 break; 6973 case INTR_TYPE_SOFT_EXCEPTION: 6974 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); 6975 fallthrough; 6976 case INTR_TYPE_HARD_EXCEPTION: 6977 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { 6978 u32 err = vmcs_read32(error_code_field); 6979 kvm_requeue_exception_e(vcpu, vector, err); 6980 } else 6981 kvm_requeue_exception(vcpu, vector); 6982 break; 6983 case INTR_TYPE_SOFT_INTR: 6984 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); 6985 fallthrough; 6986 case INTR_TYPE_EXT_INTR: 6987 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); 6988 break; 6989 default: 6990 break; 6991 } 6992 } 6993 6994 static void vmx_complete_interrupts(struct vcpu_vmx *vmx) 6995 { 6996 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, 6997 VM_EXIT_INSTRUCTION_LEN, 6998 IDT_VECTORING_ERROR_CODE); 6999 } 7000 7001 static void vmx_cancel_injection(struct kvm_vcpu *vcpu) 7002 { 7003 __vmx_complete_interrupts(vcpu, 7004 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), 7005 VM_ENTRY_INSTRUCTION_LEN, 7006 VM_ENTRY_EXCEPTION_ERROR_CODE); 7007 7008 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); 7009 } 7010 7011 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) 7012 { 7013 int i, nr_msrs; 7014 struct perf_guest_switch_msr *msrs; 7015 struct kvm_pmu *pmu = vcpu_to_pmu(&vmx->vcpu); 7016 7017 pmu->host_cross_mapped_mask = 0; 7018 if (pmu->pebs_enable & pmu->global_ctrl) 7019 intel_pmu_cross_mapped_check(pmu); 7020 7021 /* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ 7022 msrs = perf_guest_get_msrs(&nr_msrs, (void *)pmu); 7023 if (!msrs) 7024 return; 7025 7026 for (i = 0; i < nr_msrs; i++) 7027 if (msrs[i].host == msrs[i].guest) 7028 clear_atomic_switch_msr(vmx, msrs[i].msr); 7029 else 7030 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, 7031 msrs[i].host, false); 7032 } 7033 7034 static void vmx_update_hv_timer(struct kvm_vcpu *vcpu) 7035 { 7036 struct vcpu_vmx *vmx = to_vmx(vcpu); 7037 u64 tscl; 7038 u32 delta_tsc; 7039 7040 if (vmx->req_immediate_exit) { 7041 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0); 7042 vmx->loaded_vmcs->hv_timer_soft_disabled = false; 7043 } else if (vmx->hv_deadline_tsc != -1) { 7044 tscl = rdtsc(); 7045 if (vmx->hv_deadline_tsc > tscl) 7046 /* set_hv_timer ensures the delta fits in 32-bits */ 7047 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> 7048 cpu_preemption_timer_multi); 7049 else 7050 delta_tsc = 0; 7051 7052 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); 7053 vmx->loaded_vmcs->hv_timer_soft_disabled = false; 7054 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) { 7055 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1); 7056 vmx->loaded_vmcs->hv_timer_soft_disabled = true; 7057 } 7058 } 7059 7060 void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp) 7061 { 7062 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) { 7063 vmx->loaded_vmcs->host_state.rsp = host_rsp; 7064 vmcs_writel(HOST_RSP, host_rsp); 7065 } 7066 } 7067 7068 void noinstr vmx_spec_ctrl_restore_host(struct vcpu_vmx *vmx, 7069 unsigned int flags) 7070 { 7071 u64 hostval = this_cpu_read(x86_spec_ctrl_current); 7072 7073 if (!cpu_feature_enabled(X86_FEATURE_MSR_SPEC_CTRL)) 7074 return; 7075 7076 if (flags & VMX_RUN_SAVE_SPEC_CTRL) 7077 vmx->spec_ctrl = __rdmsr(MSR_IA32_SPEC_CTRL); 7078 7079 /* 7080 * If the guest/host SPEC_CTRL values differ, restore the host value. 7081 * 7082 * For legacy IBRS, the IBRS bit always needs to be written after 7083 * transitioning from a less privileged predictor mode, regardless of 7084 * whether the guest/host values differ. 7085 */ 7086 if (cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS) || 7087 vmx->spec_ctrl != hostval) 7088 native_wrmsrl(MSR_IA32_SPEC_CTRL, hostval); 7089 7090 barrier_nospec(); 7091 } 7092 7093 static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu) 7094 { 7095 switch (to_vmx(vcpu)->exit_reason.basic) { 7096 case EXIT_REASON_MSR_WRITE: 7097 return handle_fastpath_set_msr_irqoff(vcpu); 7098 case EXIT_REASON_PREEMPTION_TIMER: 7099 return handle_fastpath_preemption_timer(vcpu); 7100 default: 7101 return EXIT_FASTPATH_NONE; 7102 } 7103 } 7104 7105 static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu, 7106 struct vcpu_vmx *vmx, 7107 unsigned long flags) 7108 { 7109 guest_state_enter_irqoff(); 7110 7111 /* L1D Flush includes CPU buffer clear to mitigate MDS */ 7112 if (static_branch_unlikely(&vmx_l1d_should_flush)) 7113 vmx_l1d_flush(vcpu); 7114 else if (static_branch_unlikely(&mds_user_clear)) 7115 mds_clear_cpu_buffers(); 7116 else if (static_branch_unlikely(&mmio_stale_data_clear) && 7117 kvm_arch_has_assigned_device(vcpu->kvm)) 7118 mds_clear_cpu_buffers(); 7119 7120 vmx_disable_fb_clear(vmx); 7121 7122 if (vcpu->arch.cr2 != native_read_cr2()) 7123 native_write_cr2(vcpu->arch.cr2); 7124 7125 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, 7126 flags); 7127 7128 vcpu->arch.cr2 = native_read_cr2(); 7129 7130 vmx_enable_fb_clear(vmx); 7131 7132 guest_state_exit_irqoff(); 7133 } 7134 7135 static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu) 7136 { 7137 struct vcpu_vmx *vmx = to_vmx(vcpu); 7138 unsigned long cr3, cr4; 7139 7140 /* Record the guest's net vcpu time for enforced NMI injections. */ 7141 if (unlikely(!enable_vnmi && 7142 vmx->loaded_vmcs->soft_vnmi_blocked)) 7143 vmx->loaded_vmcs->entry_time = ktime_get(); 7144 7145 /* 7146 * Don't enter VMX if guest state is invalid, let the exit handler 7147 * start emulation until we arrive back to a valid state. Synthesize a 7148 * consistency check VM-Exit due to invalid guest state and bail. 7149 */ 7150 if (unlikely(vmx->emulation_required)) { 7151 vmx->fail = 0; 7152 7153 vmx->exit_reason.full = EXIT_REASON_INVALID_STATE; 7154 vmx->exit_reason.failed_vmentry = 1; 7155 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_1); 7156 vmx->exit_qualification = ENTRY_FAIL_DEFAULT; 7157 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_2); 7158 vmx->exit_intr_info = 0; 7159 return EXIT_FASTPATH_NONE; 7160 } 7161 7162 trace_kvm_entry(vcpu); 7163 7164 if (vmx->ple_window_dirty) { 7165 vmx->ple_window_dirty = false; 7166 vmcs_write32(PLE_WINDOW, vmx->ple_window); 7167 } 7168 7169 /* 7170 * We did this in prepare_switch_to_guest, because it needs to 7171 * be within srcu_read_lock. 7172 */ 7173 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync); 7174 7175 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP)) 7176 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); 7177 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP)) 7178 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); 7179 vcpu->arch.regs_dirty = 0; 7180 7181 /* 7182 * Refresh vmcs.HOST_CR3 if necessary. This must be done immediately 7183 * prior to VM-Enter, as the kernel may load a new ASID (PCID) any time 7184 * it switches back to the current->mm, which can occur in KVM context 7185 * when switching to a temporary mm to patch kernel code, e.g. if KVM 7186 * toggles a static key while handling a VM-Exit. 7187 */ 7188 cr3 = __get_current_cr3_fast(); 7189 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { 7190 vmcs_writel(HOST_CR3, cr3); 7191 vmx->loaded_vmcs->host_state.cr3 = cr3; 7192 } 7193 7194 cr4 = cr4_read_shadow(); 7195 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { 7196 vmcs_writel(HOST_CR4, cr4); 7197 vmx->loaded_vmcs->host_state.cr4 = cr4; 7198 } 7199 7200 /* When KVM_DEBUGREG_WONT_EXIT, dr6 is accessible in guest. */ 7201 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) 7202 set_debugreg(vcpu->arch.dr6, 6); 7203 7204 /* When single-stepping over STI and MOV SS, we must clear the 7205 * corresponding interruptibility bits in the guest state. Otherwise 7206 * vmentry fails as it then expects bit 14 (BS) in pending debug 7207 * exceptions being set, but that's not correct for the guest debugging 7208 * case. */ 7209 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) 7210 vmx_set_interrupt_shadow(vcpu, 0); 7211 7212 kvm_load_guest_xsave_state(vcpu); 7213 7214 pt_guest_enter(vmx); 7215 7216 atomic_switch_perf_msrs(vmx); 7217 if (intel_pmu_lbr_is_enabled(vcpu)) 7218 vmx_passthrough_lbr_msrs(vcpu); 7219 7220 if (enable_preemption_timer) 7221 vmx_update_hv_timer(vcpu); 7222 7223 kvm_wait_lapic_expire(vcpu); 7224 7225 /* The actual VMENTER/EXIT is in the .noinstr.text section. */ 7226 vmx_vcpu_enter_exit(vcpu, vmx, __vmx_vcpu_run_flags(vmx)); 7227 7228 /* All fields are clean at this point */ 7229 if (static_branch_unlikely(&enable_evmcs)) { 7230 current_evmcs->hv_clean_fields |= 7231 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; 7232 7233 current_evmcs->hv_vp_id = kvm_hv_get_vpindex(vcpu); 7234 } 7235 7236 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ 7237 if (vmx->host_debugctlmsr) 7238 update_debugctlmsr(vmx->host_debugctlmsr); 7239 7240 #ifndef CONFIG_X86_64 7241 /* 7242 * The sysexit path does not restore ds/es, so we must set them to 7243 * a reasonable value ourselves. 7244 * 7245 * We can't defer this to vmx_prepare_switch_to_host() since that 7246 * function may be executed in interrupt context, which saves and 7247 * restore segments around it, nullifying its effect. 7248 */ 7249 loadsegment(ds, __USER_DS); 7250 loadsegment(es, __USER_DS); 7251 #endif 7252 7253 vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET; 7254 7255 pt_guest_exit(vmx); 7256 7257 kvm_load_host_xsave_state(vcpu); 7258 7259 if (is_guest_mode(vcpu)) { 7260 /* 7261 * Track VMLAUNCH/VMRESUME that have made past guest state 7262 * checking. 7263 */ 7264 if (vmx->nested.nested_run_pending && 7265 !vmx->exit_reason.failed_vmentry) 7266 ++vcpu->stat.nested_run; 7267 7268 vmx->nested.nested_run_pending = 0; 7269 } 7270 7271 vmx->idt_vectoring_info = 0; 7272 7273 if (unlikely(vmx->fail)) { 7274 vmx->exit_reason.full = 0xdead; 7275 return EXIT_FASTPATH_NONE; 7276 } 7277 7278 vmx->exit_reason.full = vmcs_read32(VM_EXIT_REASON); 7279 if (unlikely((u16)vmx->exit_reason.basic == EXIT_REASON_MCE_DURING_VMENTRY)) 7280 kvm_machine_check(); 7281 7282 if (likely(!vmx->exit_reason.failed_vmentry)) 7283 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); 7284 7285 trace_kvm_exit(vcpu, KVM_ISA_VMX); 7286 7287 if (unlikely(vmx->exit_reason.failed_vmentry)) 7288 return EXIT_FASTPATH_NONE; 7289 7290 vmx->loaded_vmcs->launched = 1; 7291 7292 vmx_recover_nmi_blocking(vmx); 7293 vmx_complete_interrupts(vmx); 7294 7295 if (is_guest_mode(vcpu)) 7296 return EXIT_FASTPATH_NONE; 7297 7298 return vmx_exit_handlers_fastpath(vcpu); 7299 } 7300 7301 static void vmx_vcpu_free(struct kvm_vcpu *vcpu) 7302 { 7303 struct vcpu_vmx *vmx = to_vmx(vcpu); 7304 7305 if (enable_pml) 7306 vmx_destroy_pml_buffer(vmx); 7307 free_vpid(vmx->vpid); 7308 nested_vmx_free_vcpu(vcpu); 7309 free_loaded_vmcs(vmx->loaded_vmcs); 7310 } 7311 7312 static int vmx_vcpu_create(struct kvm_vcpu *vcpu) 7313 { 7314 struct vmx_uret_msr *tsx_ctrl; 7315 struct vcpu_vmx *vmx; 7316 int i, err; 7317 7318 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0); 7319 vmx = to_vmx(vcpu); 7320 7321 INIT_LIST_HEAD(&vmx->pi_wakeup_list); 7322 7323 err = -ENOMEM; 7324 7325 vmx->vpid = allocate_vpid(); 7326 7327 /* 7328 * If PML is turned on, failure on enabling PML just results in failure 7329 * of creating the vcpu, therefore we can simplify PML logic (by 7330 * avoiding dealing with cases, such as enabling PML partially on vcpus 7331 * for the guest), etc. 7332 */ 7333 if (enable_pml) { 7334 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); 7335 if (!vmx->pml_pg) 7336 goto free_vpid; 7337 } 7338 7339 for (i = 0; i < kvm_nr_uret_msrs; ++i) 7340 vmx->guest_uret_msrs[i].mask = -1ull; 7341 if (boot_cpu_has(X86_FEATURE_RTM)) { 7342 /* 7343 * TSX_CTRL_CPUID_CLEAR is handled in the CPUID interception. 7344 * Keep the host value unchanged to avoid changing CPUID bits 7345 * under the host kernel's feet. 7346 */ 7347 tsx_ctrl = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL); 7348 if (tsx_ctrl) 7349 tsx_ctrl->mask = ~(u64)TSX_CTRL_CPUID_CLEAR; 7350 } 7351 7352 err = alloc_loaded_vmcs(&vmx->vmcs01); 7353 if (err < 0) 7354 goto free_pml; 7355 7356 /* 7357 * Use Hyper-V 'Enlightened MSR Bitmap' feature when KVM runs as a 7358 * nested (L1) hypervisor and Hyper-V in L0 supports it. Enable the 7359 * feature only for vmcs01, KVM currently isn't equipped to realize any 7360 * performance benefits from enabling it for vmcs02. 7361 */ 7362 if (IS_ENABLED(CONFIG_HYPERV) && static_branch_unlikely(&enable_evmcs) && 7363 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { 7364 struct hv_enlightened_vmcs *evmcs = (void *)vmx->vmcs01.vmcs; 7365 7366 evmcs->hv_enlightenments_control.msr_bitmap = 1; 7367 } 7368 7369 /* The MSR bitmap starts with all ones */ 7370 bitmap_fill(vmx->shadow_msr_intercept.read, MAX_POSSIBLE_PASSTHROUGH_MSRS); 7371 bitmap_fill(vmx->shadow_msr_intercept.write, MAX_POSSIBLE_PASSTHROUGH_MSRS); 7372 7373 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_TSC, MSR_TYPE_R); 7374 #ifdef CONFIG_X86_64 7375 vmx_disable_intercept_for_msr(vcpu, MSR_FS_BASE, MSR_TYPE_RW); 7376 vmx_disable_intercept_for_msr(vcpu, MSR_GS_BASE, MSR_TYPE_RW); 7377 vmx_disable_intercept_for_msr(vcpu, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); 7378 #endif 7379 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); 7380 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); 7381 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); 7382 if (kvm_cstate_in_guest(vcpu->kvm)) { 7383 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C1_RES, MSR_TYPE_R); 7384 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R); 7385 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R); 7386 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R); 7387 } 7388 7389 vmx->loaded_vmcs = &vmx->vmcs01; 7390 7391 if (cpu_need_virtualize_apic_accesses(vcpu)) { 7392 err = alloc_apic_access_page(vcpu->kvm); 7393 if (err) 7394 goto free_vmcs; 7395 } 7396 7397 if (enable_ept && !enable_unrestricted_guest) { 7398 err = init_rmode_identity_map(vcpu->kvm); 7399 if (err) 7400 goto free_vmcs; 7401 } 7402 7403 if (vmx_can_use_ipiv(vcpu)) 7404 WRITE_ONCE(to_kvm_vmx(vcpu->kvm)->pid_table[vcpu->vcpu_id], 7405 __pa(&vmx->pi_desc) | PID_TABLE_ENTRY_VALID); 7406 7407 return 0; 7408 7409 free_vmcs: 7410 free_loaded_vmcs(vmx->loaded_vmcs); 7411 free_pml: 7412 vmx_destroy_pml_buffer(vmx); 7413 free_vpid: 7414 free_vpid(vmx->vpid); 7415 return err; 7416 } 7417 7418 #define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n" 7419 #define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n" 7420 7421 static int vmx_vm_init(struct kvm *kvm) 7422 { 7423 if (!ple_gap) 7424 kvm->arch.pause_in_guest = true; 7425 7426 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) { 7427 switch (l1tf_mitigation) { 7428 case L1TF_MITIGATION_OFF: 7429 case L1TF_MITIGATION_FLUSH_NOWARN: 7430 /* 'I explicitly don't care' is set */ 7431 break; 7432 case L1TF_MITIGATION_FLUSH: 7433 case L1TF_MITIGATION_FLUSH_NOSMT: 7434 case L1TF_MITIGATION_FULL: 7435 /* 7436 * Warn upon starting the first VM in a potentially 7437 * insecure environment. 7438 */ 7439 if (sched_smt_active()) 7440 pr_warn_once(L1TF_MSG_SMT); 7441 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER) 7442 pr_warn_once(L1TF_MSG_L1D); 7443 break; 7444 case L1TF_MITIGATION_FULL_FORCE: 7445 /* Flush is enforced */ 7446 break; 7447 } 7448 } 7449 return 0; 7450 } 7451 7452 static int __init vmx_check_processor_compat(void) 7453 { 7454 struct vmcs_config vmcs_conf; 7455 struct vmx_capability vmx_cap; 7456 7457 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) || 7458 !this_cpu_has(X86_FEATURE_VMX)) { 7459 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id()); 7460 return -EIO; 7461 } 7462 7463 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) 7464 return -EIO; 7465 if (nested) 7466 nested_vmx_setup_ctls_msrs(&vmcs_conf, vmx_cap.ept); 7467 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { 7468 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", 7469 smp_processor_id()); 7470 return -EIO; 7471 } 7472 return 0; 7473 } 7474 7475 static u8 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) 7476 { 7477 u8 cache; 7478 7479 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in 7480 * memory aliases with conflicting memory types and sometimes MCEs. 7481 * We have to be careful as to what are honored and when. 7482 * 7483 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to 7484 * UC. The effective memory type is UC or WC depending on guest PAT. 7485 * This was historically the source of MCEs and we want to be 7486 * conservative. 7487 * 7488 * When there is no need to deal with noncoherent DMA (e.g., no VT-d 7489 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The 7490 * EPT memory type is set to WB. The effective memory type is forced 7491 * WB. 7492 * 7493 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The 7494 * EPT memory type is used to emulate guest CD/MTRR. 7495 */ 7496 7497 if (is_mmio) 7498 return MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; 7499 7500 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) 7501 return (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) | VMX_EPT_IPAT_BIT; 7502 7503 if (kvm_read_cr0(vcpu) & X86_CR0_CD) { 7504 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) 7505 cache = MTRR_TYPE_WRBACK; 7506 else 7507 cache = MTRR_TYPE_UNCACHABLE; 7508 7509 return (cache << VMX_EPT_MT_EPTE_SHIFT) | VMX_EPT_IPAT_BIT; 7510 } 7511 7512 return kvm_mtrr_get_guest_memory_type(vcpu, gfn) << VMX_EPT_MT_EPTE_SHIFT; 7513 } 7514 7515 static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx, u32 new_ctl) 7516 { 7517 /* 7518 * These bits in the secondary execution controls field 7519 * are dynamic, the others are mostly based on the hypervisor 7520 * architecture and the guest's CPUID. Do not touch the 7521 * dynamic bits. 7522 */ 7523 u32 mask = 7524 SECONDARY_EXEC_SHADOW_VMCS | 7525 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | 7526 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | 7527 SECONDARY_EXEC_DESC; 7528 7529 u32 cur_ctl = secondary_exec_controls_get(vmx); 7530 7531 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask)); 7532 } 7533 7534 /* 7535 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits 7536 * (indicating "allowed-1") if they are supported in the guest's CPUID. 7537 */ 7538 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) 7539 { 7540 struct vcpu_vmx *vmx = to_vmx(vcpu); 7541 struct kvm_cpuid_entry2 *entry; 7542 7543 vmx->nested.msrs.cr0_fixed1 = 0xffffffff; 7544 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; 7545 7546 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ 7547 if (entry && (entry->_reg & (_cpuid_mask))) \ 7548 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ 7549 } while (0) 7550 7551 entry = kvm_find_cpuid_entry(vcpu, 0x1); 7552 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME)); 7553 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME)); 7554 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC)); 7555 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE)); 7556 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE)); 7557 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE)); 7558 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE)); 7559 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE)); 7560 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR)); 7561 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM)); 7562 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX)); 7563 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX)); 7564 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID)); 7565 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE)); 7566 7567 entry = kvm_find_cpuid_entry_index(vcpu, 0x7, 0); 7568 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE)); 7569 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP)); 7570 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP)); 7571 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU)); 7572 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP)); 7573 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57)); 7574 7575 #undef cr4_fixed1_update 7576 } 7577 7578 static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) 7579 { 7580 struct vcpu_vmx *vmx = to_vmx(vcpu); 7581 struct kvm_cpuid_entry2 *best = NULL; 7582 int i; 7583 7584 for (i = 0; i < PT_CPUID_LEAVES; i++) { 7585 best = kvm_find_cpuid_entry_index(vcpu, 0x14, i); 7586 if (!best) 7587 return; 7588 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax; 7589 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx; 7590 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx; 7591 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx; 7592 } 7593 7594 /* Get the number of configurable Address Ranges for filtering */ 7595 vmx->pt_desc.num_address_ranges = intel_pt_validate_cap(vmx->pt_desc.caps, 7596 PT_CAP_num_address_ranges); 7597 7598 /* Initialize and clear the no dependency bits */ 7599 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS | 7600 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC | 7601 RTIT_CTL_BRANCH_EN); 7602 7603 /* 7604 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise 7605 * will inject an #GP 7606 */ 7607 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering)) 7608 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN; 7609 7610 /* 7611 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and 7612 * PSBFreq can be set 7613 */ 7614 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc)) 7615 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC | 7616 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ); 7617 7618 /* 7619 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn and MTCFreq can be set 7620 */ 7621 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc)) 7622 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN | 7623 RTIT_CTL_MTC_RANGE); 7624 7625 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */ 7626 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite)) 7627 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW | 7628 RTIT_CTL_PTW_EN); 7629 7630 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */ 7631 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace)) 7632 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN; 7633 7634 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */ 7635 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output)) 7636 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA; 7637 7638 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabricEn can be set */ 7639 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys)) 7640 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN; 7641 7642 /* unmask address range configure area */ 7643 for (i = 0; i < vmx->pt_desc.num_address_ranges; i++) 7644 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4)); 7645 } 7646 7647 static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) 7648 { 7649 struct vcpu_vmx *vmx = to_vmx(vcpu); 7650 7651 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */ 7652 vcpu->arch.xsaves_enabled = false; 7653 7654 vmx_setup_uret_msrs(vmx); 7655 7656 if (cpu_has_secondary_exec_ctrls()) 7657 vmcs_set_secondary_exec_control(vmx, 7658 vmx_secondary_exec_control(vmx)); 7659 7660 if (nested_vmx_allowed(vcpu)) 7661 vmx->msr_ia32_feature_control_valid_bits |= 7662 FEAT_CTL_VMX_ENABLED_INSIDE_SMX | 7663 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; 7664 else 7665 vmx->msr_ia32_feature_control_valid_bits &= 7666 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX | 7667 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX); 7668 7669 if (nested_vmx_allowed(vcpu)) 7670 nested_vmx_cr_fixed1_bits_update(vcpu); 7671 7672 if (boot_cpu_has(X86_FEATURE_INTEL_PT) && 7673 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT)) 7674 update_intel_pt_cfg(vcpu); 7675 7676 if (boot_cpu_has(X86_FEATURE_RTM)) { 7677 struct vmx_uret_msr *msr; 7678 msr = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL); 7679 if (msr) { 7680 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM); 7681 vmx_set_guest_uret_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE); 7682 } 7683 } 7684 7685 if (kvm_cpu_cap_has(X86_FEATURE_XFD)) 7686 vmx_set_intercept_for_msr(vcpu, MSR_IA32_XFD_ERR, MSR_TYPE_R, 7687 !guest_cpuid_has(vcpu, X86_FEATURE_XFD)); 7688 7689 7690 set_cr4_guest_host_mask(vmx); 7691 7692 vmx_write_encls_bitmap(vcpu, NULL); 7693 if (guest_cpuid_has(vcpu, X86_FEATURE_SGX)) 7694 vmx->msr_ia32_feature_control_valid_bits |= FEAT_CTL_SGX_ENABLED; 7695 else 7696 vmx->msr_ia32_feature_control_valid_bits &= ~FEAT_CTL_SGX_ENABLED; 7697 7698 if (guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC)) 7699 vmx->msr_ia32_feature_control_valid_bits |= 7700 FEAT_CTL_SGX_LC_ENABLED; 7701 else 7702 vmx->msr_ia32_feature_control_valid_bits &= 7703 ~FEAT_CTL_SGX_LC_ENABLED; 7704 7705 /* Refresh #PF interception to account for MAXPHYADDR changes. */ 7706 vmx_update_exception_bitmap(vcpu); 7707 } 7708 7709 static u64 vmx_get_perf_capabilities(void) 7710 { 7711 u64 perf_cap = PMU_CAP_FW_WRITES; 7712 struct x86_pmu_lbr lbr; 7713 u64 host_perf_cap = 0; 7714 7715 if (!enable_pmu) 7716 return 0; 7717 7718 if (boot_cpu_has(X86_FEATURE_PDCM)) 7719 rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap); 7720 7721 x86_perf_get_lbr(&lbr); 7722 if (lbr.nr) 7723 perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT; 7724 7725 if (vmx_pebs_supported()) { 7726 perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK; 7727 if ((perf_cap & PERF_CAP_PEBS_FORMAT) < 4) 7728 perf_cap &= ~PERF_CAP_PEBS_BASELINE; 7729 } 7730 7731 return perf_cap; 7732 } 7733 7734 static __init void vmx_set_cpu_caps(void) 7735 { 7736 kvm_set_cpu_caps(); 7737 7738 /* CPUID 0x1 */ 7739 if (nested) 7740 kvm_cpu_cap_set(X86_FEATURE_VMX); 7741 7742 /* CPUID 0x7 */ 7743 if (kvm_mpx_supported()) 7744 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX); 7745 if (!cpu_has_vmx_invpcid()) 7746 kvm_cpu_cap_clear(X86_FEATURE_INVPCID); 7747 if (vmx_pt_mode_is_host_guest()) 7748 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT); 7749 if (vmx_pebs_supported()) { 7750 kvm_cpu_cap_check_and_set(X86_FEATURE_DS); 7751 kvm_cpu_cap_check_and_set(X86_FEATURE_DTES64); 7752 } 7753 7754 if (!enable_pmu) 7755 kvm_cpu_cap_clear(X86_FEATURE_PDCM); 7756 kvm_caps.supported_perf_cap = vmx_get_perf_capabilities(); 7757 7758 if (!enable_sgx) { 7759 kvm_cpu_cap_clear(X86_FEATURE_SGX); 7760 kvm_cpu_cap_clear(X86_FEATURE_SGX_LC); 7761 kvm_cpu_cap_clear(X86_FEATURE_SGX1); 7762 kvm_cpu_cap_clear(X86_FEATURE_SGX2); 7763 } 7764 7765 if (vmx_umip_emulated()) 7766 kvm_cpu_cap_set(X86_FEATURE_UMIP); 7767 7768 /* CPUID 0xD.1 */ 7769 kvm_caps.supported_xss = 0; 7770 if (!cpu_has_vmx_xsaves()) 7771 kvm_cpu_cap_clear(X86_FEATURE_XSAVES); 7772 7773 /* CPUID 0x80000001 and 0x7 (RDPID) */ 7774 if (!cpu_has_vmx_rdtscp()) { 7775 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP); 7776 kvm_cpu_cap_clear(X86_FEATURE_RDPID); 7777 } 7778 7779 if (cpu_has_vmx_waitpkg()) 7780 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG); 7781 } 7782 7783 static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu) 7784 { 7785 to_vmx(vcpu)->req_immediate_exit = true; 7786 } 7787 7788 static int vmx_check_intercept_io(struct kvm_vcpu *vcpu, 7789 struct x86_instruction_info *info) 7790 { 7791 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 7792 unsigned short port; 7793 bool intercept; 7794 int size; 7795 7796 if (info->intercept == x86_intercept_in || 7797 info->intercept == x86_intercept_ins) { 7798 port = info->src_val; 7799 size = info->dst_bytes; 7800 } else { 7801 port = info->dst_val; 7802 size = info->src_bytes; 7803 } 7804 7805 /* 7806 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction 7807 * VM-exits depend on the 'unconditional IO exiting' VM-execution 7808 * control. 7809 * 7810 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps. 7811 */ 7812 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) 7813 intercept = nested_cpu_has(vmcs12, 7814 CPU_BASED_UNCOND_IO_EXITING); 7815 else 7816 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size); 7817 7818 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */ 7819 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; 7820 } 7821 7822 static int vmx_check_intercept(struct kvm_vcpu *vcpu, 7823 struct x86_instruction_info *info, 7824 enum x86_intercept_stage stage, 7825 struct x86_exception *exception) 7826 { 7827 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 7828 7829 switch (info->intercept) { 7830 /* 7831 * RDPID causes #UD if disabled through secondary execution controls. 7832 * Because it is marked as EmulateOnUD, we need to intercept it here. 7833 * Note, RDPID is hidden behind ENABLE_RDTSCP. 7834 */ 7835 case x86_intercept_rdpid: 7836 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_RDTSCP)) { 7837 exception->vector = UD_VECTOR; 7838 exception->error_code_valid = false; 7839 return X86EMUL_PROPAGATE_FAULT; 7840 } 7841 break; 7842 7843 case x86_intercept_in: 7844 case x86_intercept_ins: 7845 case x86_intercept_out: 7846 case x86_intercept_outs: 7847 return vmx_check_intercept_io(vcpu, info); 7848 7849 case x86_intercept_lgdt: 7850 case x86_intercept_lidt: 7851 case x86_intercept_lldt: 7852 case x86_intercept_ltr: 7853 case x86_intercept_sgdt: 7854 case x86_intercept_sidt: 7855 case x86_intercept_sldt: 7856 case x86_intercept_str: 7857 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC)) 7858 return X86EMUL_CONTINUE; 7859 7860 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */ 7861 break; 7862 7863 /* TODO: check more intercepts... */ 7864 default: 7865 break; 7866 } 7867 7868 return X86EMUL_UNHANDLEABLE; 7869 } 7870 7871 #ifdef CONFIG_X86_64 7872 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ 7873 static inline int u64_shl_div_u64(u64 a, unsigned int shift, 7874 u64 divisor, u64 *result) 7875 { 7876 u64 low = a << shift, high = a >> (64 - shift); 7877 7878 /* To avoid the overflow on divq */ 7879 if (high >= divisor) 7880 return 1; 7881 7882 /* Low hold the result, high hold rem which is discarded */ 7883 asm("divq %2\n\t" : "=a" (low), "=d" (high) : 7884 "rm" (divisor), "0" (low), "1" (high)); 7885 *result = low; 7886 7887 return 0; 7888 } 7889 7890 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc, 7891 bool *expired) 7892 { 7893 struct vcpu_vmx *vmx; 7894 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; 7895 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer; 7896 7897 vmx = to_vmx(vcpu); 7898 tscl = rdtsc(); 7899 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); 7900 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; 7901 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, 7902 ktimer->timer_advance_ns); 7903 7904 if (delta_tsc > lapic_timer_advance_cycles) 7905 delta_tsc -= lapic_timer_advance_cycles; 7906 else 7907 delta_tsc = 0; 7908 7909 /* Convert to host delta tsc if tsc scaling is enabled */ 7910 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio && 7911 delta_tsc && u64_shl_div_u64(delta_tsc, 7912 kvm_caps.tsc_scaling_ratio_frac_bits, 7913 vcpu->arch.l1_tsc_scaling_ratio, &delta_tsc)) 7914 return -ERANGE; 7915 7916 /* 7917 * If the delta tsc can't fit in the 32 bit after the multi shift, 7918 * we can't use the preemption timer. 7919 * It's possible that it fits on later vmentries, but checking 7920 * on every vmentry is costly so we just use an hrtimer. 7921 */ 7922 if (delta_tsc >> (cpu_preemption_timer_multi + 32)) 7923 return -ERANGE; 7924 7925 vmx->hv_deadline_tsc = tscl + delta_tsc; 7926 *expired = !delta_tsc; 7927 return 0; 7928 } 7929 7930 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) 7931 { 7932 to_vmx(vcpu)->hv_deadline_tsc = -1; 7933 } 7934 #endif 7935 7936 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) 7937 { 7938 if (!kvm_pause_in_guest(vcpu->kvm)) 7939 shrink_ple_window(vcpu); 7940 } 7941 7942 void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu) 7943 { 7944 struct vcpu_vmx *vmx = to_vmx(vcpu); 7945 7946 if (is_guest_mode(vcpu)) { 7947 vmx->nested.update_vmcs01_cpu_dirty_logging = true; 7948 return; 7949 } 7950 7951 /* 7952 * Note, cpu_dirty_logging_count can be changed concurrent with this 7953 * code, but in that case another update request will be made and so 7954 * the guest will never run with a stale PML value. 7955 */ 7956 if (vcpu->kvm->arch.cpu_dirty_logging_count) 7957 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_ENABLE_PML); 7958 else 7959 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_ENABLE_PML); 7960 } 7961 7962 static void vmx_setup_mce(struct kvm_vcpu *vcpu) 7963 { 7964 if (vcpu->arch.mcg_cap & MCG_LMCE_P) 7965 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= 7966 FEAT_CTL_LMCE_ENABLED; 7967 else 7968 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= 7969 ~FEAT_CTL_LMCE_ENABLED; 7970 } 7971 7972 #ifdef CONFIG_KVM_SMM 7973 static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection) 7974 { 7975 /* we need a nested vmexit to enter SMM, postpone if run is pending */ 7976 if (to_vmx(vcpu)->nested.nested_run_pending) 7977 return -EBUSY; 7978 return !is_smm(vcpu); 7979 } 7980 7981 static int vmx_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) 7982 { 7983 struct vcpu_vmx *vmx = to_vmx(vcpu); 7984 7985 /* 7986 * TODO: Implement custom flows for forcing the vCPU out/in of L2 on 7987 * SMI and RSM. Using the common VM-Exit + VM-Enter routines is wrong 7988 * SMI and RSM only modify state that is saved and restored via SMRAM. 7989 * E.g. most MSRs are left untouched, but many are modified by VM-Exit 7990 * and VM-Enter, and thus L2's values may be corrupted on SMI+RSM. 7991 */ 7992 vmx->nested.smm.guest_mode = is_guest_mode(vcpu); 7993 if (vmx->nested.smm.guest_mode) 7994 nested_vmx_vmexit(vcpu, -1, 0, 0); 7995 7996 vmx->nested.smm.vmxon = vmx->nested.vmxon; 7997 vmx->nested.vmxon = false; 7998 vmx_clear_hlt(vcpu); 7999 return 0; 8000 } 8001 8002 static int vmx_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) 8003 { 8004 struct vcpu_vmx *vmx = to_vmx(vcpu); 8005 int ret; 8006 8007 if (vmx->nested.smm.vmxon) { 8008 vmx->nested.vmxon = true; 8009 vmx->nested.smm.vmxon = false; 8010 } 8011 8012 if (vmx->nested.smm.guest_mode) { 8013 ret = nested_vmx_enter_non_root_mode(vcpu, false); 8014 if (ret) 8015 return ret; 8016 8017 vmx->nested.nested_run_pending = 1; 8018 vmx->nested.smm.guest_mode = false; 8019 } 8020 return 0; 8021 } 8022 8023 static void vmx_enable_smi_window(struct kvm_vcpu *vcpu) 8024 { 8025 /* RSM will cause a vmexit anyway. */ 8026 } 8027 #endif 8028 8029 static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu) 8030 { 8031 return to_vmx(vcpu)->nested.vmxon && !is_guest_mode(vcpu); 8032 } 8033 8034 static void vmx_migrate_timers(struct kvm_vcpu *vcpu) 8035 { 8036 if (is_guest_mode(vcpu)) { 8037 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer; 8038 8039 if (hrtimer_try_to_cancel(timer) == 1) 8040 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); 8041 } 8042 } 8043 8044 static void vmx_hardware_unsetup(void) 8045 { 8046 kvm_set_posted_intr_wakeup_handler(NULL); 8047 8048 if (nested) 8049 nested_vmx_hardware_unsetup(); 8050 8051 free_kvm_area(); 8052 } 8053 8054 static bool vmx_check_apicv_inhibit_reasons(enum kvm_apicv_inhibit reason) 8055 { 8056 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) | 8057 BIT(APICV_INHIBIT_REASON_ABSENT) | 8058 BIT(APICV_INHIBIT_REASON_HYPERV) | 8059 BIT(APICV_INHIBIT_REASON_BLOCKIRQ) | 8060 BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | 8061 BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED); 8062 8063 return supported & BIT(reason); 8064 } 8065 8066 static void vmx_vm_destroy(struct kvm *kvm) 8067 { 8068 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); 8069 8070 free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm)); 8071 } 8072 8073 static struct kvm_x86_ops vmx_x86_ops __initdata = { 8074 .name = "kvm_intel", 8075 8076 .hardware_unsetup = vmx_hardware_unsetup, 8077 8078 .hardware_enable = vmx_hardware_enable, 8079 .hardware_disable = vmx_hardware_disable, 8080 .has_emulated_msr = vmx_has_emulated_msr, 8081 8082 .vm_size = sizeof(struct kvm_vmx), 8083 .vm_init = vmx_vm_init, 8084 .vm_destroy = vmx_vm_destroy, 8085 8086 .vcpu_precreate = vmx_vcpu_precreate, 8087 .vcpu_create = vmx_vcpu_create, 8088 .vcpu_free = vmx_vcpu_free, 8089 .vcpu_reset = vmx_vcpu_reset, 8090 8091 .prepare_switch_to_guest = vmx_prepare_switch_to_guest, 8092 .vcpu_load = vmx_vcpu_load, 8093 .vcpu_put = vmx_vcpu_put, 8094 8095 .update_exception_bitmap = vmx_update_exception_bitmap, 8096 .get_msr_feature = vmx_get_msr_feature, 8097 .get_msr = vmx_get_msr, 8098 .set_msr = vmx_set_msr, 8099 .get_segment_base = vmx_get_segment_base, 8100 .get_segment = vmx_get_segment, 8101 .set_segment = vmx_set_segment, 8102 .get_cpl = vmx_get_cpl, 8103 .get_cs_db_l_bits = vmx_get_cs_db_l_bits, 8104 .set_cr0 = vmx_set_cr0, 8105 .is_valid_cr4 = vmx_is_valid_cr4, 8106 .set_cr4 = vmx_set_cr4, 8107 .set_efer = vmx_set_efer, 8108 .get_idt = vmx_get_idt, 8109 .set_idt = vmx_set_idt, 8110 .get_gdt = vmx_get_gdt, 8111 .set_gdt = vmx_set_gdt, 8112 .set_dr7 = vmx_set_dr7, 8113 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, 8114 .cache_reg = vmx_cache_reg, 8115 .get_rflags = vmx_get_rflags, 8116 .set_rflags = vmx_set_rflags, 8117 .get_if_flag = vmx_get_if_flag, 8118 8119 .flush_tlb_all = vmx_flush_tlb_all, 8120 .flush_tlb_current = vmx_flush_tlb_current, 8121 .flush_tlb_gva = vmx_flush_tlb_gva, 8122 .flush_tlb_guest = vmx_flush_tlb_guest, 8123 8124 .vcpu_pre_run = vmx_vcpu_pre_run, 8125 .vcpu_run = vmx_vcpu_run, 8126 .handle_exit = vmx_handle_exit, 8127 .skip_emulated_instruction = vmx_skip_emulated_instruction, 8128 .update_emulated_instruction = vmx_update_emulated_instruction, 8129 .set_interrupt_shadow = vmx_set_interrupt_shadow, 8130 .get_interrupt_shadow = vmx_get_interrupt_shadow, 8131 .patch_hypercall = vmx_patch_hypercall, 8132 .inject_irq = vmx_inject_irq, 8133 .inject_nmi = vmx_inject_nmi, 8134 .inject_exception = vmx_inject_exception, 8135 .cancel_injection = vmx_cancel_injection, 8136 .interrupt_allowed = vmx_interrupt_allowed, 8137 .nmi_allowed = vmx_nmi_allowed, 8138 .get_nmi_mask = vmx_get_nmi_mask, 8139 .set_nmi_mask = vmx_set_nmi_mask, 8140 .enable_nmi_window = vmx_enable_nmi_window, 8141 .enable_irq_window = vmx_enable_irq_window, 8142 .update_cr8_intercept = vmx_update_cr8_intercept, 8143 .set_virtual_apic_mode = vmx_set_virtual_apic_mode, 8144 .set_apic_access_page_addr = vmx_set_apic_access_page_addr, 8145 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, 8146 .load_eoi_exitmap = vmx_load_eoi_exitmap, 8147 .apicv_post_state_restore = vmx_apicv_post_state_restore, 8148 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons, 8149 .hwapic_irr_update = vmx_hwapic_irr_update, 8150 .hwapic_isr_update = vmx_hwapic_isr_update, 8151 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt, 8152 .sync_pir_to_irr = vmx_sync_pir_to_irr, 8153 .deliver_interrupt = vmx_deliver_interrupt, 8154 .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt, 8155 8156 .set_tss_addr = vmx_set_tss_addr, 8157 .set_identity_map_addr = vmx_set_identity_map_addr, 8158 .get_mt_mask = vmx_get_mt_mask, 8159 8160 .get_exit_info = vmx_get_exit_info, 8161 8162 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid, 8163 8164 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, 8165 8166 .get_l2_tsc_offset = vmx_get_l2_tsc_offset, 8167 .get_l2_tsc_multiplier = vmx_get_l2_tsc_multiplier, 8168 .write_tsc_offset = vmx_write_tsc_offset, 8169 .write_tsc_multiplier = vmx_write_tsc_multiplier, 8170 8171 .load_mmu_pgd = vmx_load_mmu_pgd, 8172 8173 .check_intercept = vmx_check_intercept, 8174 .handle_exit_irqoff = vmx_handle_exit_irqoff, 8175 8176 .request_immediate_exit = vmx_request_immediate_exit, 8177 8178 .sched_in = vmx_sched_in, 8179 8180 .cpu_dirty_log_size = PML_ENTITY_NUM, 8181 .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging, 8182 8183 .nested_ops = &vmx_nested_ops, 8184 8185 .pi_update_irte = vmx_pi_update_irte, 8186 .pi_start_assignment = vmx_pi_start_assignment, 8187 8188 #ifdef CONFIG_X86_64 8189 .set_hv_timer = vmx_set_hv_timer, 8190 .cancel_hv_timer = vmx_cancel_hv_timer, 8191 #endif 8192 8193 .setup_mce = vmx_setup_mce, 8194 8195 #ifdef CONFIG_KVM_SMM 8196 .smi_allowed = vmx_smi_allowed, 8197 .enter_smm = vmx_enter_smm, 8198 .leave_smm = vmx_leave_smm, 8199 .enable_smi_window = vmx_enable_smi_window, 8200 #endif 8201 8202 .can_emulate_instruction = vmx_can_emulate_instruction, 8203 .apic_init_signal_blocked = vmx_apic_init_signal_blocked, 8204 .migrate_timers = vmx_migrate_timers, 8205 8206 .msr_filter_changed = vmx_msr_filter_changed, 8207 .complete_emulated_msr = kvm_complete_insn_gp, 8208 8209 .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, 8210 }; 8211 8212 static unsigned int vmx_handle_intel_pt_intr(void) 8213 { 8214 struct kvm_vcpu *vcpu = kvm_get_running_vcpu(); 8215 8216 /* '0' on failure so that the !PT case can use a RET0 static call. */ 8217 if (!vcpu || !kvm_handling_nmi_from_guest(vcpu)) 8218 return 0; 8219 8220 kvm_make_request(KVM_REQ_PMI, vcpu); 8221 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT, 8222 (unsigned long *)&vcpu->arch.pmu.global_status); 8223 return 1; 8224 } 8225 8226 static __init void vmx_setup_user_return_msrs(void) 8227 { 8228 8229 /* 8230 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm 8231 * will emulate SYSCALL in legacy mode if the vendor string in guest 8232 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To 8233 * support this emulation, MSR_STAR is included in the list for i386, 8234 * but is never loaded into hardware. MSR_CSTAR is also never loaded 8235 * into hardware and is here purely for emulation purposes. 8236 */ 8237 const u32 vmx_uret_msrs_list[] = { 8238 #ifdef CONFIG_X86_64 8239 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, 8240 #endif 8241 MSR_EFER, MSR_TSC_AUX, MSR_STAR, 8242 MSR_IA32_TSX_CTRL, 8243 }; 8244 int i; 8245 8246 BUILD_BUG_ON(ARRAY_SIZE(vmx_uret_msrs_list) != MAX_NR_USER_RETURN_MSRS); 8247 8248 for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i) 8249 kvm_add_user_return_msr(vmx_uret_msrs_list[i]); 8250 } 8251 8252 static void __init vmx_setup_me_spte_mask(void) 8253 { 8254 u64 me_mask = 0; 8255 8256 /* 8257 * kvm_get_shadow_phys_bits() returns shadow_phys_bits. Use 8258 * the former to avoid exposing shadow_phys_bits. 8259 * 8260 * On pre-MKTME system, boot_cpu_data.x86_phys_bits equals to 8261 * shadow_phys_bits. On MKTME and/or TDX capable systems, 8262 * boot_cpu_data.x86_phys_bits holds the actual physical address 8263 * w/o the KeyID bits, and shadow_phys_bits equals to MAXPHYADDR 8264 * reported by CPUID. Those bits between are KeyID bits. 8265 */ 8266 if (boot_cpu_data.x86_phys_bits != kvm_get_shadow_phys_bits()) 8267 me_mask = rsvd_bits(boot_cpu_data.x86_phys_bits, 8268 kvm_get_shadow_phys_bits() - 1); 8269 /* 8270 * Unlike SME, host kernel doesn't support setting up any 8271 * MKTME KeyID on Intel platforms. No memory encryption 8272 * bits should be included into the SPTE. 8273 */ 8274 kvm_mmu_set_me_spte_mask(0, me_mask); 8275 } 8276 8277 static struct kvm_x86_init_ops vmx_init_ops __initdata; 8278 8279 static __init int hardware_setup(void) 8280 { 8281 unsigned long host_bndcfgs; 8282 struct desc_ptr dt; 8283 int r; 8284 8285 store_idt(&dt); 8286 host_idt_base = dt.address; 8287 8288 vmx_setup_user_return_msrs(); 8289 8290 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0) 8291 return -EIO; 8292 8293 if (cpu_has_perf_global_ctrl_bug()) 8294 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " 8295 "does not work properly. Using workaround\n"); 8296 8297 if (boot_cpu_has(X86_FEATURE_NX)) 8298 kvm_enable_efer_bits(EFER_NX); 8299 8300 if (boot_cpu_has(X86_FEATURE_MPX)) { 8301 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs); 8302 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost"); 8303 } 8304 8305 if (!cpu_has_vmx_mpx()) 8306 kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | 8307 XFEATURE_MASK_BNDCSR); 8308 8309 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || 8310 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) 8311 enable_vpid = 0; 8312 8313 if (!cpu_has_vmx_ept() || 8314 !cpu_has_vmx_ept_4levels() || 8315 !cpu_has_vmx_ept_mt_wb() || 8316 !cpu_has_vmx_invept_global()) 8317 enable_ept = 0; 8318 8319 /* NX support is required for shadow paging. */ 8320 if (!enable_ept && !boot_cpu_has(X86_FEATURE_NX)) { 8321 pr_err_ratelimited("kvm: NX (Execute Disable) not supported\n"); 8322 return -EOPNOTSUPP; 8323 } 8324 8325 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) 8326 enable_ept_ad_bits = 0; 8327 8328 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) 8329 enable_unrestricted_guest = 0; 8330 8331 if (!cpu_has_vmx_flexpriority()) 8332 flexpriority_enabled = 0; 8333 8334 if (!cpu_has_virtual_nmis()) 8335 enable_vnmi = 0; 8336 8337 #ifdef CONFIG_X86_SGX_KVM 8338 if (!cpu_has_vmx_encls_vmexit()) 8339 enable_sgx = false; 8340 #endif 8341 8342 /* 8343 * set_apic_access_page_addr() is used to reload apic access 8344 * page upon invalidation. No need to do anything if not 8345 * using the APIC_ACCESS_ADDR VMCS field. 8346 */ 8347 if (!flexpriority_enabled) 8348 vmx_x86_ops.set_apic_access_page_addr = NULL; 8349 8350 if (!cpu_has_vmx_tpr_shadow()) 8351 vmx_x86_ops.update_cr8_intercept = NULL; 8352 8353 #if IS_ENABLED(CONFIG_HYPERV) 8354 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH 8355 && enable_ept) { 8356 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb; 8357 vmx_x86_ops.tlb_remote_flush_with_range = 8358 hv_remote_flush_tlb_with_range; 8359 } 8360 #endif 8361 8362 if (!cpu_has_vmx_ple()) { 8363 ple_gap = 0; 8364 ple_window = 0; 8365 ple_window_grow = 0; 8366 ple_window_max = 0; 8367 ple_window_shrink = 0; 8368 } 8369 8370 if (!cpu_has_vmx_apicv()) 8371 enable_apicv = 0; 8372 if (!enable_apicv) 8373 vmx_x86_ops.sync_pir_to_irr = NULL; 8374 8375 if (!enable_apicv || !cpu_has_vmx_ipiv()) 8376 enable_ipiv = false; 8377 8378 if (cpu_has_vmx_tsc_scaling()) 8379 kvm_caps.has_tsc_control = true; 8380 8381 kvm_caps.max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; 8382 kvm_caps.tsc_scaling_ratio_frac_bits = 48; 8383 kvm_caps.has_bus_lock_exit = cpu_has_vmx_bus_lock_detection(); 8384 kvm_caps.has_notify_vmexit = cpu_has_notify_vmexit(); 8385 8386 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ 8387 8388 if (enable_ept) 8389 kvm_mmu_set_ept_masks(enable_ept_ad_bits, 8390 cpu_has_vmx_ept_execute_only()); 8391 8392 /* 8393 * Setup shadow_me_value/shadow_me_mask to include MKTME KeyID 8394 * bits to shadow_zero_check. 8395 */ 8396 vmx_setup_me_spte_mask(); 8397 8398 kvm_configure_mmu(enable_ept, 0, vmx_get_max_tdp_level(), 8399 ept_caps_to_lpage_level(vmx_capability.ept)); 8400 8401 /* 8402 * Only enable PML when hardware supports PML feature, and both EPT 8403 * and EPT A/D bit features are enabled -- PML depends on them to work. 8404 */ 8405 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) 8406 enable_pml = 0; 8407 8408 if (!enable_pml) 8409 vmx_x86_ops.cpu_dirty_log_size = 0; 8410 8411 if (!cpu_has_vmx_preemption_timer()) 8412 enable_preemption_timer = false; 8413 8414 if (enable_preemption_timer) { 8415 u64 use_timer_freq = 5000ULL * 1000 * 1000; 8416 8417 cpu_preemption_timer_multi = 8418 vmcs_config.misc & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; 8419 8420 if (tsc_khz) 8421 use_timer_freq = (u64)tsc_khz * 1000; 8422 use_timer_freq >>= cpu_preemption_timer_multi; 8423 8424 /* 8425 * KVM "disables" the preemption timer by setting it to its max 8426 * value. Don't use the timer if it might cause spurious exits 8427 * at a rate faster than 0.1 Hz (of uninterrupted guest time). 8428 */ 8429 if (use_timer_freq > 0xffffffffu / 10) 8430 enable_preemption_timer = false; 8431 } 8432 8433 if (!enable_preemption_timer) { 8434 vmx_x86_ops.set_hv_timer = NULL; 8435 vmx_x86_ops.cancel_hv_timer = NULL; 8436 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit; 8437 } 8438 8439 kvm_caps.supported_mce_cap |= MCG_LMCE_P; 8440 kvm_caps.supported_mce_cap |= MCG_CMCI_P; 8441 8442 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST) 8443 return -EINVAL; 8444 if (!enable_ept || !enable_pmu || !cpu_has_vmx_intel_pt()) 8445 pt_mode = PT_MODE_SYSTEM; 8446 if (pt_mode == PT_MODE_HOST_GUEST) 8447 vmx_init_ops.handle_intel_pt_intr = vmx_handle_intel_pt_intr; 8448 else 8449 vmx_init_ops.handle_intel_pt_intr = NULL; 8450 8451 setup_default_sgx_lepubkeyhash(); 8452 8453 if (nested) { 8454 nested_vmx_setup_ctls_msrs(&vmcs_config, vmx_capability.ept); 8455 8456 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers); 8457 if (r) 8458 return r; 8459 } 8460 8461 vmx_set_cpu_caps(); 8462 8463 r = alloc_kvm_area(); 8464 if (r && nested) 8465 nested_vmx_hardware_unsetup(); 8466 8467 kvm_set_posted_intr_wakeup_handler(pi_wakeup_handler); 8468 8469 return r; 8470 } 8471 8472 static struct kvm_x86_init_ops vmx_init_ops __initdata = { 8473 .cpu_has_kvm_support = cpu_has_kvm_support, 8474 .disabled_by_bios = vmx_disabled_by_bios, 8475 .check_processor_compatibility = vmx_check_processor_compat, 8476 .hardware_setup = hardware_setup, 8477 .handle_intel_pt_intr = NULL, 8478 8479 .runtime_ops = &vmx_x86_ops, 8480 .pmu_ops = &intel_pmu_ops, 8481 }; 8482 8483 static void vmx_cleanup_l1d_flush(void) 8484 { 8485 if (vmx_l1d_flush_pages) { 8486 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); 8487 vmx_l1d_flush_pages = NULL; 8488 } 8489 /* Restore state so sysfs ignores VMX */ 8490 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; 8491 } 8492 8493 static void vmx_exit(void) 8494 { 8495 #ifdef CONFIG_KEXEC_CORE 8496 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); 8497 synchronize_rcu(); 8498 #endif 8499 8500 kvm_exit(); 8501 8502 #if IS_ENABLED(CONFIG_HYPERV) 8503 if (static_branch_unlikely(&enable_evmcs)) { 8504 int cpu; 8505 struct hv_vp_assist_page *vp_ap; 8506 /* 8507 * Reset everything to support using non-enlightened VMCS 8508 * access later (e.g. when we reload the module with 8509 * enlightened_vmcs=0) 8510 */ 8511 for_each_online_cpu(cpu) { 8512 vp_ap = hv_get_vp_assist_page(cpu); 8513 8514 if (!vp_ap) 8515 continue; 8516 8517 vp_ap->nested_control.features.directhypercall = 0; 8518 vp_ap->current_nested_vmcs = 0; 8519 vp_ap->enlighten_vmentry = 0; 8520 } 8521 8522 static_branch_disable(&enable_evmcs); 8523 } 8524 #endif 8525 vmx_cleanup_l1d_flush(); 8526 8527 allow_smaller_maxphyaddr = false; 8528 } 8529 module_exit(vmx_exit); 8530 8531 static int __init vmx_init(void) 8532 { 8533 int r, cpu; 8534 8535 #if IS_ENABLED(CONFIG_HYPERV) 8536 /* 8537 * Enlightened VMCS usage should be recommended and the host needs 8538 * to support eVMCS v1 or above. We can also disable eVMCS support 8539 * with module parameter. 8540 */ 8541 if (enlightened_vmcs && 8542 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && 8543 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= 8544 KVM_EVMCS_VERSION) { 8545 8546 /* Check that we have assist pages on all online CPUs */ 8547 for_each_online_cpu(cpu) { 8548 if (!hv_get_vp_assist_page(cpu)) { 8549 enlightened_vmcs = false; 8550 break; 8551 } 8552 } 8553 8554 if (enlightened_vmcs) { 8555 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); 8556 static_branch_enable(&enable_evmcs); 8557 } 8558 8559 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) 8560 vmx_x86_ops.enable_l2_tlb_flush 8561 = hv_enable_l2_tlb_flush; 8562 8563 } else { 8564 enlightened_vmcs = false; 8565 } 8566 #endif 8567 8568 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx), 8569 __alignof__(struct vcpu_vmx), THIS_MODULE); 8570 if (r) 8571 return r; 8572 8573 /* 8574 * Must be called after kvm_init() so enable_ept is properly set 8575 * up. Hand the parameter mitigation value in which was stored in 8576 * the pre module init parser. If no parameter was given, it will 8577 * contain 'auto' which will be turned into the default 'cond' 8578 * mitigation mode. 8579 */ 8580 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param); 8581 if (r) { 8582 vmx_exit(); 8583 return r; 8584 } 8585 8586 vmx_setup_fb_clear_ctrl(); 8587 8588 for_each_possible_cpu(cpu) { 8589 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); 8590 8591 pi_init_cpu(cpu); 8592 } 8593 8594 #ifdef CONFIG_KEXEC_CORE 8595 rcu_assign_pointer(crash_vmclear_loaded_vmcss, 8596 crash_vmclear_local_loaded_vmcss); 8597 #endif 8598 vmx_check_vmcs12_offsets(); 8599 8600 /* 8601 * Shadow paging doesn't have a (further) performance penalty 8602 * from GUEST_MAXPHYADDR < HOST_MAXPHYADDR so enable it 8603 * by default 8604 */ 8605 if (!enable_ept) 8606 allow_smaller_maxphyaddr = true; 8607 8608 return 0; 8609 } 8610 module_init(vmx_init); 8611