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 #if IS_ENABLED(CONFIG_HYPERV) 2756 if (enlightened_vmcs) 2757 evmcs_sanitize_exec_ctrls(vmcs_conf); 2758 #endif 2759 2760 return 0; 2761 } 2762 2763 struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags) 2764 { 2765 int node = cpu_to_node(cpu); 2766 struct page *pages; 2767 struct vmcs *vmcs; 2768 2769 pages = __alloc_pages_node(node, flags, 0); 2770 if (!pages) 2771 return NULL; 2772 vmcs = page_address(pages); 2773 memset(vmcs, 0, vmcs_config.size); 2774 2775 /* KVM supports Enlightened VMCS v1 only */ 2776 if (static_branch_unlikely(&enable_evmcs)) 2777 vmcs->hdr.revision_id = KVM_EVMCS_VERSION; 2778 else 2779 vmcs->hdr.revision_id = vmcs_config.revision_id; 2780 2781 if (shadow) 2782 vmcs->hdr.shadow_vmcs = 1; 2783 return vmcs; 2784 } 2785 2786 void free_vmcs(struct vmcs *vmcs) 2787 { 2788 free_page((unsigned long)vmcs); 2789 } 2790 2791 /* 2792 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded 2793 */ 2794 void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) 2795 { 2796 if (!loaded_vmcs->vmcs) 2797 return; 2798 loaded_vmcs_clear(loaded_vmcs); 2799 free_vmcs(loaded_vmcs->vmcs); 2800 loaded_vmcs->vmcs = NULL; 2801 if (loaded_vmcs->msr_bitmap) 2802 free_page((unsigned long)loaded_vmcs->msr_bitmap); 2803 WARN_ON(loaded_vmcs->shadow_vmcs != NULL); 2804 } 2805 2806 int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) 2807 { 2808 loaded_vmcs->vmcs = alloc_vmcs(false); 2809 if (!loaded_vmcs->vmcs) 2810 return -ENOMEM; 2811 2812 vmcs_clear(loaded_vmcs->vmcs); 2813 2814 loaded_vmcs->shadow_vmcs = NULL; 2815 loaded_vmcs->hv_timer_soft_disabled = false; 2816 loaded_vmcs->cpu = -1; 2817 loaded_vmcs->launched = 0; 2818 2819 if (cpu_has_vmx_msr_bitmap()) { 2820 loaded_vmcs->msr_bitmap = (unsigned long *) 2821 __get_free_page(GFP_KERNEL_ACCOUNT); 2822 if (!loaded_vmcs->msr_bitmap) 2823 goto out_vmcs; 2824 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); 2825 } 2826 2827 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state)); 2828 memset(&loaded_vmcs->controls_shadow, 0, 2829 sizeof(struct vmcs_controls_shadow)); 2830 2831 return 0; 2832 2833 out_vmcs: 2834 free_loaded_vmcs(loaded_vmcs); 2835 return -ENOMEM; 2836 } 2837 2838 static void free_kvm_area(void) 2839 { 2840 int cpu; 2841 2842 for_each_possible_cpu(cpu) { 2843 free_vmcs(per_cpu(vmxarea, cpu)); 2844 per_cpu(vmxarea, cpu) = NULL; 2845 } 2846 } 2847 2848 static __init int alloc_kvm_area(void) 2849 { 2850 int cpu; 2851 2852 for_each_possible_cpu(cpu) { 2853 struct vmcs *vmcs; 2854 2855 vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL); 2856 if (!vmcs) { 2857 free_kvm_area(); 2858 return -ENOMEM; 2859 } 2860 2861 /* 2862 * When eVMCS is enabled, alloc_vmcs_cpu() sets 2863 * vmcs->revision_id to KVM_EVMCS_VERSION instead of 2864 * revision_id reported by MSR_IA32_VMX_BASIC. 2865 * 2866 * However, even though not explicitly documented by 2867 * TLFS, VMXArea passed as VMXON argument should 2868 * still be marked with revision_id reported by 2869 * physical CPU. 2870 */ 2871 if (static_branch_unlikely(&enable_evmcs)) 2872 vmcs->hdr.revision_id = vmcs_config.revision_id; 2873 2874 per_cpu(vmxarea, cpu) = vmcs; 2875 } 2876 return 0; 2877 } 2878 2879 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, 2880 struct kvm_segment *save) 2881 { 2882 if (!emulate_invalid_guest_state) { 2883 /* 2884 * CS and SS RPL should be equal during guest entry according 2885 * to VMX spec, but in reality it is not always so. Since vcpu 2886 * is in the middle of the transition from real mode to 2887 * protected mode it is safe to assume that RPL 0 is a good 2888 * default value. 2889 */ 2890 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) 2891 save->selector &= ~SEGMENT_RPL_MASK; 2892 save->dpl = save->selector & SEGMENT_RPL_MASK; 2893 save->s = 1; 2894 } 2895 __vmx_set_segment(vcpu, save, seg); 2896 } 2897 2898 static void enter_pmode(struct kvm_vcpu *vcpu) 2899 { 2900 unsigned long flags; 2901 struct vcpu_vmx *vmx = to_vmx(vcpu); 2902 2903 /* 2904 * Update real mode segment cache. It may be not up-to-date if segment 2905 * register was written while vcpu was in a guest mode. 2906 */ 2907 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); 2908 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); 2909 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); 2910 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); 2911 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); 2912 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); 2913 2914 vmx->rmode.vm86_active = 0; 2915 2916 __vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); 2917 2918 flags = vmcs_readl(GUEST_RFLAGS); 2919 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; 2920 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; 2921 vmcs_writel(GUEST_RFLAGS, flags); 2922 2923 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | 2924 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); 2925 2926 vmx_update_exception_bitmap(vcpu); 2927 2928 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); 2929 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); 2930 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); 2931 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); 2932 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); 2933 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); 2934 } 2935 2936 static void fix_rmode_seg(int seg, struct kvm_segment *save) 2937 { 2938 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; 2939 struct kvm_segment var = *save; 2940 2941 var.dpl = 0x3; 2942 if (seg == VCPU_SREG_CS) 2943 var.type = 0x3; 2944 2945 if (!emulate_invalid_guest_state) { 2946 var.selector = var.base >> 4; 2947 var.base = var.base & 0xffff0; 2948 var.limit = 0xffff; 2949 var.g = 0; 2950 var.db = 0; 2951 var.present = 1; 2952 var.s = 1; 2953 var.l = 0; 2954 var.unusable = 0; 2955 var.type = 0x3; 2956 var.avl = 0; 2957 if (save->base & 0xf) 2958 printk_once(KERN_WARNING "kvm: segment base is not " 2959 "paragraph aligned when entering " 2960 "protected mode (seg=%d)", seg); 2961 } 2962 2963 vmcs_write16(sf->selector, var.selector); 2964 vmcs_writel(sf->base, var.base); 2965 vmcs_write32(sf->limit, var.limit); 2966 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); 2967 } 2968 2969 static void enter_rmode(struct kvm_vcpu *vcpu) 2970 { 2971 unsigned long flags; 2972 struct vcpu_vmx *vmx = to_vmx(vcpu); 2973 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); 2974 2975 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); 2976 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); 2977 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); 2978 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); 2979 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); 2980 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); 2981 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); 2982 2983 vmx->rmode.vm86_active = 1; 2984 2985 /* 2986 * Very old userspace does not call KVM_SET_TSS_ADDR before entering 2987 * vcpu. Warn the user that an update is overdue. 2988 */ 2989 if (!kvm_vmx->tss_addr) 2990 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " 2991 "called before entering vcpu\n"); 2992 2993 vmx_segment_cache_clear(vmx); 2994 2995 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); 2996 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); 2997 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); 2998 2999 flags = vmcs_readl(GUEST_RFLAGS); 3000 vmx->rmode.save_rflags = flags; 3001 3002 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; 3003 3004 vmcs_writel(GUEST_RFLAGS, flags); 3005 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); 3006 vmx_update_exception_bitmap(vcpu); 3007 3008 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); 3009 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); 3010 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); 3011 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); 3012 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); 3013 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); 3014 } 3015 3016 int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) 3017 { 3018 struct vcpu_vmx *vmx = to_vmx(vcpu); 3019 3020 /* Nothing to do if hardware doesn't support EFER. */ 3021 if (!vmx_find_uret_msr(vmx, MSR_EFER)) 3022 return 0; 3023 3024 vcpu->arch.efer = efer; 3025 #ifdef CONFIG_X86_64 3026 if (efer & EFER_LMA) 3027 vm_entry_controls_setbit(vmx, VM_ENTRY_IA32E_MODE); 3028 else 3029 vm_entry_controls_clearbit(vmx, VM_ENTRY_IA32E_MODE); 3030 #else 3031 if (KVM_BUG_ON(efer & EFER_LMA, vcpu->kvm)) 3032 return 1; 3033 #endif 3034 3035 vmx_setup_uret_msrs(vmx); 3036 return 0; 3037 } 3038 3039 #ifdef CONFIG_X86_64 3040 3041 static void enter_lmode(struct kvm_vcpu *vcpu) 3042 { 3043 u32 guest_tr_ar; 3044 3045 vmx_segment_cache_clear(to_vmx(vcpu)); 3046 3047 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); 3048 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) { 3049 pr_debug_ratelimited("%s: tss fixup for long mode. \n", 3050 __func__); 3051 vmcs_write32(GUEST_TR_AR_BYTES, 3052 (guest_tr_ar & ~VMX_AR_TYPE_MASK) 3053 | VMX_AR_TYPE_BUSY_64_TSS); 3054 } 3055 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); 3056 } 3057 3058 static void exit_lmode(struct kvm_vcpu *vcpu) 3059 { 3060 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); 3061 } 3062 3063 #endif 3064 3065 static void vmx_flush_tlb_all(struct kvm_vcpu *vcpu) 3066 { 3067 struct vcpu_vmx *vmx = to_vmx(vcpu); 3068 3069 /* 3070 * INVEPT must be issued when EPT is enabled, irrespective of VPID, as 3071 * the CPU is not required to invalidate guest-physical mappings on 3072 * VM-Entry, even if VPID is disabled. Guest-physical mappings are 3073 * associated with the root EPT structure and not any particular VPID 3074 * (INVVPID also isn't required to invalidate guest-physical mappings). 3075 */ 3076 if (enable_ept) { 3077 ept_sync_global(); 3078 } else if (enable_vpid) { 3079 if (cpu_has_vmx_invvpid_global()) { 3080 vpid_sync_vcpu_global(); 3081 } else { 3082 vpid_sync_vcpu_single(vmx->vpid); 3083 vpid_sync_vcpu_single(vmx->nested.vpid02); 3084 } 3085 } 3086 } 3087 3088 static inline int vmx_get_current_vpid(struct kvm_vcpu *vcpu) 3089 { 3090 if (is_guest_mode(vcpu)) 3091 return nested_get_vpid02(vcpu); 3092 return to_vmx(vcpu)->vpid; 3093 } 3094 3095 static void vmx_flush_tlb_current(struct kvm_vcpu *vcpu) 3096 { 3097 struct kvm_mmu *mmu = vcpu->arch.mmu; 3098 u64 root_hpa = mmu->root.hpa; 3099 3100 /* No flush required if the current context is invalid. */ 3101 if (!VALID_PAGE(root_hpa)) 3102 return; 3103 3104 if (enable_ept) 3105 ept_sync_context(construct_eptp(vcpu, root_hpa, 3106 mmu->root_role.level)); 3107 else 3108 vpid_sync_context(vmx_get_current_vpid(vcpu)); 3109 } 3110 3111 static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr) 3112 { 3113 /* 3114 * vpid_sync_vcpu_addr() is a nop if vpid==0, see the comment in 3115 * vmx_flush_tlb_guest() for an explanation of why this is ok. 3116 */ 3117 vpid_sync_vcpu_addr(vmx_get_current_vpid(vcpu), addr); 3118 } 3119 3120 static void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu) 3121 { 3122 /* 3123 * vpid_sync_context() is a nop if vpid==0, e.g. if enable_vpid==0 or a 3124 * vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit are 3125 * required to flush GVA->{G,H}PA mappings from the TLB if vpid is 3126 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed), 3127 * i.e. no explicit INVVPID is necessary. 3128 */ 3129 vpid_sync_context(vmx_get_current_vpid(vcpu)); 3130 } 3131 3132 void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu) 3133 { 3134 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; 3135 3136 if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR)) 3137 return; 3138 3139 if (is_pae_paging(vcpu)) { 3140 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); 3141 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); 3142 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); 3143 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); 3144 } 3145 } 3146 3147 void ept_save_pdptrs(struct kvm_vcpu *vcpu) 3148 { 3149 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; 3150 3151 if (WARN_ON_ONCE(!is_pae_paging(vcpu))) 3152 return; 3153 3154 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); 3155 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); 3156 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); 3157 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); 3158 3159 kvm_register_mark_available(vcpu, VCPU_EXREG_PDPTR); 3160 } 3161 3162 #define CR3_EXITING_BITS (CPU_BASED_CR3_LOAD_EXITING | \ 3163 CPU_BASED_CR3_STORE_EXITING) 3164 3165 void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) 3166 { 3167 struct vcpu_vmx *vmx = to_vmx(vcpu); 3168 unsigned long hw_cr0, old_cr0_pg; 3169 u32 tmp; 3170 3171 old_cr0_pg = kvm_read_cr0_bits(vcpu, X86_CR0_PG); 3172 3173 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF); 3174 if (is_unrestricted_guest(vcpu)) 3175 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; 3176 else { 3177 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; 3178 if (!enable_ept) 3179 hw_cr0 |= X86_CR0_WP; 3180 3181 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) 3182 enter_pmode(vcpu); 3183 3184 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) 3185 enter_rmode(vcpu); 3186 } 3187 3188 vmcs_writel(CR0_READ_SHADOW, cr0); 3189 vmcs_writel(GUEST_CR0, hw_cr0); 3190 vcpu->arch.cr0 = cr0; 3191 kvm_register_mark_available(vcpu, VCPU_EXREG_CR0); 3192 3193 #ifdef CONFIG_X86_64 3194 if (vcpu->arch.efer & EFER_LME) { 3195 if (!old_cr0_pg && (cr0 & X86_CR0_PG)) 3196 enter_lmode(vcpu); 3197 else if (old_cr0_pg && !(cr0 & X86_CR0_PG)) 3198 exit_lmode(vcpu); 3199 } 3200 #endif 3201 3202 if (enable_ept && !is_unrestricted_guest(vcpu)) { 3203 /* 3204 * Ensure KVM has an up-to-date snapshot of the guest's CR3. If 3205 * the below code _enables_ CR3 exiting, vmx_cache_reg() will 3206 * (correctly) stop reading vmcs.GUEST_CR3 because it thinks 3207 * KVM's CR3 is installed. 3208 */ 3209 if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3)) 3210 vmx_cache_reg(vcpu, VCPU_EXREG_CR3); 3211 3212 /* 3213 * When running with EPT but not unrestricted guest, KVM must 3214 * intercept CR3 accesses when paging is _disabled_. This is 3215 * necessary because restricted guests can't actually run with 3216 * paging disabled, and so KVM stuffs its own CR3 in order to 3217 * run the guest when identity mapped page tables. 3218 * 3219 * Do _NOT_ check the old CR0.PG, e.g. to optimize away the 3220 * update, it may be stale with respect to CR3 interception, 3221 * e.g. after nested VM-Enter. 3222 * 3223 * Lastly, honor L1's desires, i.e. intercept CR3 loads and/or 3224 * stores to forward them to L1, even if KVM does not need to 3225 * intercept them to preserve its identity mapped page tables. 3226 */ 3227 if (!(cr0 & X86_CR0_PG)) { 3228 exec_controls_setbit(vmx, CR3_EXITING_BITS); 3229 } else if (!is_guest_mode(vcpu)) { 3230 exec_controls_clearbit(vmx, CR3_EXITING_BITS); 3231 } else { 3232 tmp = exec_controls_get(vmx); 3233 tmp &= ~CR3_EXITING_BITS; 3234 tmp |= get_vmcs12(vcpu)->cpu_based_vm_exec_control & CR3_EXITING_BITS; 3235 exec_controls_set(vmx, tmp); 3236 } 3237 3238 /* Note, vmx_set_cr4() consumes the new vcpu->arch.cr0. */ 3239 if ((old_cr0_pg ^ cr0) & X86_CR0_PG) 3240 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); 3241 3242 /* 3243 * When !CR0_PG -> CR0_PG, vcpu->arch.cr3 becomes active, but 3244 * GUEST_CR3 is still vmx->ept_identity_map_addr if EPT + !URG. 3245 */ 3246 if (!(old_cr0_pg & X86_CR0_PG) && (cr0 & X86_CR0_PG)) 3247 kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); 3248 } 3249 3250 /* depends on vcpu->arch.cr0 to be set to a new value */ 3251 vmx->emulation_required = vmx_emulation_required(vcpu); 3252 } 3253 3254 static int vmx_get_max_tdp_level(void) 3255 { 3256 if (cpu_has_vmx_ept_5levels()) 3257 return 5; 3258 return 4; 3259 } 3260 3261 u64 construct_eptp(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level) 3262 { 3263 u64 eptp = VMX_EPTP_MT_WB; 3264 3265 eptp |= (root_level == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4; 3266 3267 if (enable_ept_ad_bits && 3268 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) 3269 eptp |= VMX_EPTP_AD_ENABLE_BIT; 3270 eptp |= root_hpa; 3271 3272 return eptp; 3273 } 3274 3275 static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, 3276 int root_level) 3277 { 3278 struct kvm *kvm = vcpu->kvm; 3279 bool update_guest_cr3 = true; 3280 unsigned long guest_cr3; 3281 u64 eptp; 3282 3283 if (enable_ept) { 3284 eptp = construct_eptp(vcpu, root_hpa, root_level); 3285 vmcs_write64(EPT_POINTER, eptp); 3286 3287 hv_track_root_tdp(vcpu, root_hpa); 3288 3289 if (!enable_unrestricted_guest && !is_paging(vcpu)) 3290 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; 3291 else if (kvm_register_is_dirty(vcpu, VCPU_EXREG_CR3)) 3292 guest_cr3 = vcpu->arch.cr3; 3293 else /* vmcs.GUEST_CR3 is already up-to-date. */ 3294 update_guest_cr3 = false; 3295 vmx_ept_load_pdptrs(vcpu); 3296 } else { 3297 guest_cr3 = root_hpa | kvm_get_active_pcid(vcpu); 3298 } 3299 3300 if (update_guest_cr3) 3301 vmcs_writel(GUEST_CR3, guest_cr3); 3302 } 3303 3304 3305 static bool vmx_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) 3306 { 3307 /* 3308 * We operate under the default treatment of SMM, so VMX cannot be 3309 * enabled under SMM. Note, whether or not VMXE is allowed at all, 3310 * i.e. is a reserved bit, is handled by common x86 code. 3311 */ 3312 if ((cr4 & X86_CR4_VMXE) && is_smm(vcpu)) 3313 return false; 3314 3315 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) 3316 return false; 3317 3318 return true; 3319 } 3320 3321 void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) 3322 { 3323 unsigned long old_cr4 = vcpu->arch.cr4; 3324 struct vcpu_vmx *vmx = to_vmx(vcpu); 3325 /* 3326 * Pass through host's Machine Check Enable value to hw_cr4, which 3327 * is in force while we are in guest mode. Do not let guests control 3328 * this bit, even if host CR4.MCE == 0. 3329 */ 3330 unsigned long hw_cr4; 3331 3332 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); 3333 if (is_unrestricted_guest(vcpu)) 3334 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; 3335 else if (vmx->rmode.vm86_active) 3336 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; 3337 else 3338 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; 3339 3340 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { 3341 if (cr4 & X86_CR4_UMIP) { 3342 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC); 3343 hw_cr4 &= ~X86_CR4_UMIP; 3344 } else if (!is_guest_mode(vcpu) || 3345 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) { 3346 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC); 3347 } 3348 } 3349 3350 vcpu->arch.cr4 = cr4; 3351 kvm_register_mark_available(vcpu, VCPU_EXREG_CR4); 3352 3353 if (!is_unrestricted_guest(vcpu)) { 3354 if (enable_ept) { 3355 if (!is_paging(vcpu)) { 3356 hw_cr4 &= ~X86_CR4_PAE; 3357 hw_cr4 |= X86_CR4_PSE; 3358 } else if (!(cr4 & X86_CR4_PAE)) { 3359 hw_cr4 &= ~X86_CR4_PAE; 3360 } 3361 } 3362 3363 /* 3364 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in 3365 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs 3366 * to be manually disabled when guest switches to non-paging 3367 * mode. 3368 * 3369 * If !enable_unrestricted_guest, the CPU is always running 3370 * with CR0.PG=1 and CR4 needs to be modified. 3371 * If enable_unrestricted_guest, the CPU automatically 3372 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. 3373 */ 3374 if (!is_paging(vcpu)) 3375 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); 3376 } 3377 3378 vmcs_writel(CR4_READ_SHADOW, cr4); 3379 vmcs_writel(GUEST_CR4, hw_cr4); 3380 3381 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE)) 3382 kvm_update_cpuid_runtime(vcpu); 3383 } 3384 3385 void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) 3386 { 3387 struct vcpu_vmx *vmx = to_vmx(vcpu); 3388 u32 ar; 3389 3390 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { 3391 *var = vmx->rmode.segs[seg]; 3392 if (seg == VCPU_SREG_TR 3393 || var->selector == vmx_read_guest_seg_selector(vmx, seg)) 3394 return; 3395 var->base = vmx_read_guest_seg_base(vmx, seg); 3396 var->selector = vmx_read_guest_seg_selector(vmx, seg); 3397 return; 3398 } 3399 var->base = vmx_read_guest_seg_base(vmx, seg); 3400 var->limit = vmx_read_guest_seg_limit(vmx, seg); 3401 var->selector = vmx_read_guest_seg_selector(vmx, seg); 3402 ar = vmx_read_guest_seg_ar(vmx, seg); 3403 var->unusable = (ar >> 16) & 1; 3404 var->type = ar & 15; 3405 var->s = (ar >> 4) & 1; 3406 var->dpl = (ar >> 5) & 3; 3407 /* 3408 * Some userspaces do not preserve unusable property. Since usable 3409 * segment has to be present according to VMX spec we can use present 3410 * property to amend userspace bug by making unusable segment always 3411 * nonpresent. vmx_segment_access_rights() already marks nonpresent 3412 * segment as unusable. 3413 */ 3414 var->present = !var->unusable; 3415 var->avl = (ar >> 12) & 1; 3416 var->l = (ar >> 13) & 1; 3417 var->db = (ar >> 14) & 1; 3418 var->g = (ar >> 15) & 1; 3419 } 3420 3421 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) 3422 { 3423 struct kvm_segment s; 3424 3425 if (to_vmx(vcpu)->rmode.vm86_active) { 3426 vmx_get_segment(vcpu, &s, seg); 3427 return s.base; 3428 } 3429 return vmx_read_guest_seg_base(to_vmx(vcpu), seg); 3430 } 3431 3432 int vmx_get_cpl(struct kvm_vcpu *vcpu) 3433 { 3434 struct vcpu_vmx *vmx = to_vmx(vcpu); 3435 3436 if (unlikely(vmx->rmode.vm86_active)) 3437 return 0; 3438 else { 3439 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); 3440 return VMX_AR_DPL(ar); 3441 } 3442 } 3443 3444 static u32 vmx_segment_access_rights(struct kvm_segment *var) 3445 { 3446 u32 ar; 3447 3448 if (var->unusable || !var->present) 3449 ar = 1 << 16; 3450 else { 3451 ar = var->type & 15; 3452 ar |= (var->s & 1) << 4; 3453 ar |= (var->dpl & 3) << 5; 3454 ar |= (var->present & 1) << 7; 3455 ar |= (var->avl & 1) << 12; 3456 ar |= (var->l & 1) << 13; 3457 ar |= (var->db & 1) << 14; 3458 ar |= (var->g & 1) << 15; 3459 } 3460 3461 return ar; 3462 } 3463 3464 void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) 3465 { 3466 struct vcpu_vmx *vmx = to_vmx(vcpu); 3467 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; 3468 3469 vmx_segment_cache_clear(vmx); 3470 3471 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { 3472 vmx->rmode.segs[seg] = *var; 3473 if (seg == VCPU_SREG_TR) 3474 vmcs_write16(sf->selector, var->selector); 3475 else if (var->s) 3476 fix_rmode_seg(seg, &vmx->rmode.segs[seg]); 3477 return; 3478 } 3479 3480 vmcs_writel(sf->base, var->base); 3481 vmcs_write32(sf->limit, var->limit); 3482 vmcs_write16(sf->selector, var->selector); 3483 3484 /* 3485 * Fix the "Accessed" bit in AR field of segment registers for older 3486 * qemu binaries. 3487 * IA32 arch specifies that at the time of processor reset the 3488 * "Accessed" bit in the AR field of segment registers is 1. And qemu 3489 * is setting it to 0 in the userland code. This causes invalid guest 3490 * state vmexit when "unrestricted guest" mode is turned on. 3491 * Fix for this setup issue in cpu_reset is being pushed in the qemu 3492 * tree. Newer qemu binaries with that qemu fix would not need this 3493 * kvm hack. 3494 */ 3495 if (is_unrestricted_guest(vcpu) && (seg != VCPU_SREG_LDTR)) 3496 var->type |= 0x1; /* Accessed */ 3497 3498 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); 3499 } 3500 3501 static void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) 3502 { 3503 __vmx_set_segment(vcpu, var, seg); 3504 3505 to_vmx(vcpu)->emulation_required = vmx_emulation_required(vcpu); 3506 } 3507 3508 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) 3509 { 3510 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS); 3511 3512 *db = (ar >> 14) & 1; 3513 *l = (ar >> 13) & 1; 3514 } 3515 3516 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 3517 { 3518 dt->size = vmcs_read32(GUEST_IDTR_LIMIT); 3519 dt->address = vmcs_readl(GUEST_IDTR_BASE); 3520 } 3521 3522 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 3523 { 3524 vmcs_write32(GUEST_IDTR_LIMIT, dt->size); 3525 vmcs_writel(GUEST_IDTR_BASE, dt->address); 3526 } 3527 3528 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 3529 { 3530 dt->size = vmcs_read32(GUEST_GDTR_LIMIT); 3531 dt->address = vmcs_readl(GUEST_GDTR_BASE); 3532 } 3533 3534 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 3535 { 3536 vmcs_write32(GUEST_GDTR_LIMIT, dt->size); 3537 vmcs_writel(GUEST_GDTR_BASE, dt->address); 3538 } 3539 3540 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) 3541 { 3542 struct kvm_segment var; 3543 u32 ar; 3544 3545 vmx_get_segment(vcpu, &var, seg); 3546 var.dpl = 0x3; 3547 if (seg == VCPU_SREG_CS) 3548 var.type = 0x3; 3549 ar = vmx_segment_access_rights(&var); 3550 3551 if (var.base != (var.selector << 4)) 3552 return false; 3553 if (var.limit != 0xffff) 3554 return false; 3555 if (ar != 0xf3) 3556 return false; 3557 3558 return true; 3559 } 3560 3561 static bool code_segment_valid(struct kvm_vcpu *vcpu) 3562 { 3563 struct kvm_segment cs; 3564 unsigned int cs_rpl; 3565 3566 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); 3567 cs_rpl = cs.selector & SEGMENT_RPL_MASK; 3568 3569 if (cs.unusable) 3570 return false; 3571 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK)) 3572 return false; 3573 if (!cs.s) 3574 return false; 3575 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { 3576 if (cs.dpl > cs_rpl) 3577 return false; 3578 } else { 3579 if (cs.dpl != cs_rpl) 3580 return false; 3581 } 3582 if (!cs.present) 3583 return false; 3584 3585 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ 3586 return true; 3587 } 3588 3589 static bool stack_segment_valid(struct kvm_vcpu *vcpu) 3590 { 3591 struct kvm_segment ss; 3592 unsigned int ss_rpl; 3593 3594 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); 3595 ss_rpl = ss.selector & SEGMENT_RPL_MASK; 3596 3597 if (ss.unusable) 3598 return true; 3599 if (ss.type != 3 && ss.type != 7) 3600 return false; 3601 if (!ss.s) 3602 return false; 3603 if (ss.dpl != ss_rpl) /* DPL != RPL */ 3604 return false; 3605 if (!ss.present) 3606 return false; 3607 3608 return true; 3609 } 3610 3611 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) 3612 { 3613 struct kvm_segment var; 3614 unsigned int rpl; 3615 3616 vmx_get_segment(vcpu, &var, seg); 3617 rpl = var.selector & SEGMENT_RPL_MASK; 3618 3619 if (var.unusable) 3620 return true; 3621 if (!var.s) 3622 return false; 3623 if (!var.present) 3624 return false; 3625 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) { 3626 if (var.dpl < rpl) /* DPL < RPL */ 3627 return false; 3628 } 3629 3630 /* TODO: Add other members to kvm_segment_field to allow checking for other access 3631 * rights flags 3632 */ 3633 return true; 3634 } 3635 3636 static bool tr_valid(struct kvm_vcpu *vcpu) 3637 { 3638 struct kvm_segment tr; 3639 3640 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); 3641 3642 if (tr.unusable) 3643 return false; 3644 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ 3645 return false; 3646 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ 3647 return false; 3648 if (!tr.present) 3649 return false; 3650 3651 return true; 3652 } 3653 3654 static bool ldtr_valid(struct kvm_vcpu *vcpu) 3655 { 3656 struct kvm_segment ldtr; 3657 3658 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); 3659 3660 if (ldtr.unusable) 3661 return true; 3662 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ 3663 return false; 3664 if (ldtr.type != 2) 3665 return false; 3666 if (!ldtr.present) 3667 return false; 3668 3669 return true; 3670 } 3671 3672 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) 3673 { 3674 struct kvm_segment cs, ss; 3675 3676 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); 3677 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); 3678 3679 return ((cs.selector & SEGMENT_RPL_MASK) == 3680 (ss.selector & SEGMENT_RPL_MASK)); 3681 } 3682 3683 /* 3684 * Check if guest state is valid. Returns true if valid, false if 3685 * not. 3686 * We assume that registers are always usable 3687 */ 3688 bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu) 3689 { 3690 /* real mode guest state checks */ 3691 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { 3692 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) 3693 return false; 3694 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) 3695 return false; 3696 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) 3697 return false; 3698 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) 3699 return false; 3700 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) 3701 return false; 3702 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) 3703 return false; 3704 } else { 3705 /* protected mode guest state checks */ 3706 if (!cs_ss_rpl_check(vcpu)) 3707 return false; 3708 if (!code_segment_valid(vcpu)) 3709 return false; 3710 if (!stack_segment_valid(vcpu)) 3711 return false; 3712 if (!data_segment_valid(vcpu, VCPU_SREG_DS)) 3713 return false; 3714 if (!data_segment_valid(vcpu, VCPU_SREG_ES)) 3715 return false; 3716 if (!data_segment_valid(vcpu, VCPU_SREG_FS)) 3717 return false; 3718 if (!data_segment_valid(vcpu, VCPU_SREG_GS)) 3719 return false; 3720 if (!tr_valid(vcpu)) 3721 return false; 3722 if (!ldtr_valid(vcpu)) 3723 return false; 3724 } 3725 /* TODO: 3726 * - Add checks on RIP 3727 * - Add checks on RFLAGS 3728 */ 3729 3730 return true; 3731 } 3732 3733 static int init_rmode_tss(struct kvm *kvm, void __user *ua) 3734 { 3735 const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0))); 3736 u16 data; 3737 int i; 3738 3739 for (i = 0; i < 3; i++) { 3740 if (__copy_to_user(ua + PAGE_SIZE * i, zero_page, PAGE_SIZE)) 3741 return -EFAULT; 3742 } 3743 3744 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; 3745 if (__copy_to_user(ua + TSS_IOPB_BASE_OFFSET, &data, sizeof(u16))) 3746 return -EFAULT; 3747 3748 data = ~0; 3749 if (__copy_to_user(ua + RMODE_TSS_SIZE - 1, &data, sizeof(u8))) 3750 return -EFAULT; 3751 3752 return 0; 3753 } 3754 3755 static int init_rmode_identity_map(struct kvm *kvm) 3756 { 3757 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); 3758 int i, r = 0; 3759 void __user *uaddr; 3760 u32 tmp; 3761 3762 /* Protect kvm_vmx->ept_identity_pagetable_done. */ 3763 mutex_lock(&kvm->slots_lock); 3764 3765 if (likely(kvm_vmx->ept_identity_pagetable_done)) 3766 goto out; 3767 3768 if (!kvm_vmx->ept_identity_map_addr) 3769 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; 3770 3771 uaddr = __x86_set_memory_region(kvm, 3772 IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 3773 kvm_vmx->ept_identity_map_addr, 3774 PAGE_SIZE); 3775 if (IS_ERR(uaddr)) { 3776 r = PTR_ERR(uaddr); 3777 goto out; 3778 } 3779 3780 /* Set up identity-mapping pagetable for EPT in real mode */ 3781 for (i = 0; i < (PAGE_SIZE / sizeof(tmp)); i++) { 3782 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | 3783 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); 3784 if (__copy_to_user(uaddr + i * sizeof(tmp), &tmp, sizeof(tmp))) { 3785 r = -EFAULT; 3786 goto out; 3787 } 3788 } 3789 kvm_vmx->ept_identity_pagetable_done = true; 3790 3791 out: 3792 mutex_unlock(&kvm->slots_lock); 3793 return r; 3794 } 3795 3796 static void seg_setup(int seg) 3797 { 3798 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; 3799 unsigned int ar; 3800 3801 vmcs_write16(sf->selector, 0); 3802 vmcs_writel(sf->base, 0); 3803 vmcs_write32(sf->limit, 0xffff); 3804 ar = 0x93; 3805 if (seg == VCPU_SREG_CS) 3806 ar |= 0x08; /* code segment */ 3807 3808 vmcs_write32(sf->ar_bytes, ar); 3809 } 3810 3811 int allocate_vpid(void) 3812 { 3813 int vpid; 3814 3815 if (!enable_vpid) 3816 return 0; 3817 spin_lock(&vmx_vpid_lock); 3818 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); 3819 if (vpid < VMX_NR_VPIDS) 3820 __set_bit(vpid, vmx_vpid_bitmap); 3821 else 3822 vpid = 0; 3823 spin_unlock(&vmx_vpid_lock); 3824 return vpid; 3825 } 3826 3827 void free_vpid(int vpid) 3828 { 3829 if (!enable_vpid || vpid == 0) 3830 return; 3831 spin_lock(&vmx_vpid_lock); 3832 __clear_bit(vpid, vmx_vpid_bitmap); 3833 spin_unlock(&vmx_vpid_lock); 3834 } 3835 3836 static void vmx_msr_bitmap_l01_changed(struct vcpu_vmx *vmx) 3837 { 3838 /* 3839 * When KVM is a nested hypervisor on top of Hyper-V and uses 3840 * 'Enlightened MSR Bitmap' feature L0 needs to know that MSR 3841 * bitmap has changed. 3842 */ 3843 if (static_branch_unlikely(&enable_evmcs)) 3844 evmcs_touch_msr_bitmap(); 3845 3846 vmx->nested.force_msr_bitmap_recalc = true; 3847 } 3848 3849 void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) 3850 { 3851 struct vcpu_vmx *vmx = to_vmx(vcpu); 3852 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; 3853 3854 if (!cpu_has_vmx_msr_bitmap()) 3855 return; 3856 3857 vmx_msr_bitmap_l01_changed(vmx); 3858 3859 /* 3860 * Mark the desired intercept state in shadow bitmap, this is needed 3861 * for resync when the MSR filters change. 3862 */ 3863 if (is_valid_passthrough_msr(msr)) { 3864 int idx = possible_passthrough_msr_slot(msr); 3865 3866 if (idx != -ENOENT) { 3867 if (type & MSR_TYPE_R) 3868 clear_bit(idx, vmx->shadow_msr_intercept.read); 3869 if (type & MSR_TYPE_W) 3870 clear_bit(idx, vmx->shadow_msr_intercept.write); 3871 } 3872 } 3873 3874 if ((type & MSR_TYPE_R) && 3875 !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) { 3876 vmx_set_msr_bitmap_read(msr_bitmap, msr); 3877 type &= ~MSR_TYPE_R; 3878 } 3879 3880 if ((type & MSR_TYPE_W) && 3881 !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) { 3882 vmx_set_msr_bitmap_write(msr_bitmap, msr); 3883 type &= ~MSR_TYPE_W; 3884 } 3885 3886 if (type & MSR_TYPE_R) 3887 vmx_clear_msr_bitmap_read(msr_bitmap, msr); 3888 3889 if (type & MSR_TYPE_W) 3890 vmx_clear_msr_bitmap_write(msr_bitmap, msr); 3891 } 3892 3893 void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) 3894 { 3895 struct vcpu_vmx *vmx = to_vmx(vcpu); 3896 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; 3897 3898 if (!cpu_has_vmx_msr_bitmap()) 3899 return; 3900 3901 vmx_msr_bitmap_l01_changed(vmx); 3902 3903 /* 3904 * Mark the desired intercept state in shadow bitmap, this is needed 3905 * for resync when the MSR filter changes. 3906 */ 3907 if (is_valid_passthrough_msr(msr)) { 3908 int idx = possible_passthrough_msr_slot(msr); 3909 3910 if (idx != -ENOENT) { 3911 if (type & MSR_TYPE_R) 3912 set_bit(idx, vmx->shadow_msr_intercept.read); 3913 if (type & MSR_TYPE_W) 3914 set_bit(idx, vmx->shadow_msr_intercept.write); 3915 } 3916 } 3917 3918 if (type & MSR_TYPE_R) 3919 vmx_set_msr_bitmap_read(msr_bitmap, msr); 3920 3921 if (type & MSR_TYPE_W) 3922 vmx_set_msr_bitmap_write(msr_bitmap, msr); 3923 } 3924 3925 static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode) 3926 { 3927 unsigned long *msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; 3928 unsigned long read_intercept; 3929 int msr; 3930 3931 read_intercept = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; 3932 3933 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { 3934 unsigned int read_idx = msr / BITS_PER_LONG; 3935 unsigned int write_idx = read_idx + (0x800 / sizeof(long)); 3936 3937 msr_bitmap[read_idx] = read_intercept; 3938 msr_bitmap[write_idx] = ~0ul; 3939 } 3940 } 3941 3942 static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu) 3943 { 3944 struct vcpu_vmx *vmx = to_vmx(vcpu); 3945 u8 mode; 3946 3947 if (!cpu_has_vmx_msr_bitmap()) 3948 return; 3949 3950 if (cpu_has_secondary_exec_ctrls() && 3951 (secondary_exec_controls_get(vmx) & 3952 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { 3953 mode = MSR_BITMAP_MODE_X2APIC; 3954 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) 3955 mode |= MSR_BITMAP_MODE_X2APIC_APICV; 3956 } else { 3957 mode = 0; 3958 } 3959 3960 if (mode == vmx->x2apic_msr_bitmap_mode) 3961 return; 3962 3963 vmx->x2apic_msr_bitmap_mode = mode; 3964 3965 vmx_reset_x2apic_msrs(vcpu, mode); 3966 3967 /* 3968 * TPR reads and writes can be virtualized even if virtual interrupt 3969 * delivery is not in use. 3970 */ 3971 vmx_set_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW, 3972 !(mode & MSR_BITMAP_MODE_X2APIC)); 3973 3974 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { 3975 vmx_enable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_RW); 3976 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); 3977 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); 3978 if (enable_ipiv) 3979 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_ICR), MSR_TYPE_RW); 3980 } 3981 } 3982 3983 void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu) 3984 { 3985 struct vcpu_vmx *vmx = to_vmx(vcpu); 3986 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN); 3987 u32 i; 3988 3989 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_STATUS, MSR_TYPE_RW, flag); 3990 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_BASE, MSR_TYPE_RW, flag); 3991 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_MASK, MSR_TYPE_RW, flag); 3992 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_CR3_MATCH, MSR_TYPE_RW, flag); 3993 for (i = 0; i < vmx->pt_desc.num_address_ranges; i++) { 3994 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag); 3995 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag); 3996 } 3997 } 3998 3999 static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) 4000 { 4001 struct vcpu_vmx *vmx = to_vmx(vcpu); 4002 void *vapic_page; 4003 u32 vppr; 4004 int rvi; 4005 4006 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) || 4007 !nested_cpu_has_vid(get_vmcs12(vcpu)) || 4008 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn)) 4009 return false; 4010 4011 rvi = vmx_get_rvi(); 4012 4013 vapic_page = vmx->nested.virtual_apic_map.hva; 4014 vppr = *((u32 *)(vapic_page + APIC_PROCPRI)); 4015 4016 return ((rvi & 0xf0) > (vppr & 0xf0)); 4017 } 4018 4019 static void vmx_msr_filter_changed(struct kvm_vcpu *vcpu) 4020 { 4021 struct vcpu_vmx *vmx = to_vmx(vcpu); 4022 u32 i; 4023 4024 /* 4025 * Redo intercept permissions for MSRs that KVM is passing through to 4026 * the guest. Disabling interception will check the new MSR filter and 4027 * ensure that KVM enables interception if usersepace wants to filter 4028 * the MSR. MSRs that KVM is already intercepting don't need to be 4029 * refreshed since KVM is going to intercept them regardless of what 4030 * userspace wants. 4031 */ 4032 for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++) { 4033 u32 msr = vmx_possible_passthrough_msrs[i]; 4034 4035 if (!test_bit(i, vmx->shadow_msr_intercept.read)) 4036 vmx_disable_intercept_for_msr(vcpu, msr, MSR_TYPE_R); 4037 4038 if (!test_bit(i, vmx->shadow_msr_intercept.write)) 4039 vmx_disable_intercept_for_msr(vcpu, msr, MSR_TYPE_W); 4040 } 4041 4042 /* PT MSRs can be passed through iff PT is exposed to the guest. */ 4043 if (vmx_pt_mode_is_host_guest()) 4044 pt_update_intercept_for_msr(vcpu); 4045 } 4046 4047 static inline void kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, 4048 int pi_vec) 4049 { 4050 #ifdef CONFIG_SMP 4051 if (vcpu->mode == IN_GUEST_MODE) { 4052 /* 4053 * The vector of the virtual has already been set in the PIR. 4054 * Send a notification event to deliver the virtual interrupt 4055 * unless the vCPU is the currently running vCPU, i.e. the 4056 * event is being sent from a fastpath VM-Exit handler, in 4057 * which case the PIR will be synced to the vIRR before 4058 * re-entering the guest. 4059 * 4060 * When the target is not the running vCPU, the following 4061 * possibilities emerge: 4062 * 4063 * Case 1: vCPU stays in non-root mode. Sending a notification 4064 * event posts the interrupt to the vCPU. 4065 * 4066 * Case 2: vCPU exits to root mode and is still runnable. The 4067 * PIR will be synced to the vIRR before re-entering the guest. 4068 * Sending a notification event is ok as the host IRQ handler 4069 * will ignore the spurious event. 4070 * 4071 * Case 3: vCPU exits to root mode and is blocked. vcpu_block() 4072 * has already synced PIR to vIRR and never blocks the vCPU if 4073 * the vIRR is not empty. Therefore, a blocked vCPU here does 4074 * not wait for any requested interrupts in PIR, and sending a 4075 * notification event also results in a benign, spurious event. 4076 */ 4077 4078 if (vcpu != kvm_get_running_vcpu()) 4079 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); 4080 return; 4081 } 4082 #endif 4083 /* 4084 * The vCPU isn't in the guest; wake the vCPU in case it is blocking, 4085 * otherwise do nothing as KVM will grab the highest priority pending 4086 * IRQ via ->sync_pir_to_irr() in vcpu_enter_guest(). 4087 */ 4088 kvm_vcpu_wake_up(vcpu); 4089 } 4090 4091 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, 4092 int vector) 4093 { 4094 struct vcpu_vmx *vmx = to_vmx(vcpu); 4095 4096 if (is_guest_mode(vcpu) && 4097 vector == vmx->nested.posted_intr_nv) { 4098 /* 4099 * If a posted intr is not recognized by hardware, 4100 * we will accomplish it in the next vmentry. 4101 */ 4102 vmx->nested.pi_pending = true; 4103 kvm_make_request(KVM_REQ_EVENT, vcpu); 4104 4105 /* 4106 * This pairs with the smp_mb_*() after setting vcpu->mode in 4107 * vcpu_enter_guest() to guarantee the vCPU sees the event 4108 * request if triggering a posted interrupt "fails" because 4109 * vcpu->mode != IN_GUEST_MODE. The extra barrier is needed as 4110 * the smb_wmb() in kvm_make_request() only ensures everything 4111 * done before making the request is visible when the request 4112 * is visible, it doesn't ensure ordering between the store to 4113 * vcpu->requests and the load from vcpu->mode. 4114 */ 4115 smp_mb__after_atomic(); 4116 4117 /* the PIR and ON have been set by L1. */ 4118 kvm_vcpu_trigger_posted_interrupt(vcpu, POSTED_INTR_NESTED_VECTOR); 4119 return 0; 4120 } 4121 return -1; 4122 } 4123 /* 4124 * Send interrupt to vcpu via posted interrupt way. 4125 * 1. If target vcpu is running(non-root mode), send posted interrupt 4126 * notification to vcpu and hardware will sync PIR to vIRR atomically. 4127 * 2. If target vcpu isn't running(root mode), kick it to pick up the 4128 * interrupt from PIR in next vmentry. 4129 */ 4130 static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) 4131 { 4132 struct vcpu_vmx *vmx = to_vmx(vcpu); 4133 int r; 4134 4135 r = vmx_deliver_nested_posted_interrupt(vcpu, vector); 4136 if (!r) 4137 return 0; 4138 4139 /* Note, this is called iff the local APIC is in-kernel. */ 4140 if (!vcpu->arch.apic->apicv_active) 4141 return -1; 4142 4143 if (pi_test_and_set_pir(vector, &vmx->pi_desc)) 4144 return 0; 4145 4146 /* If a previous notification has sent the IPI, nothing to do. */ 4147 if (pi_test_and_set_on(&vmx->pi_desc)) 4148 return 0; 4149 4150 /* 4151 * The implied barrier in pi_test_and_set_on() pairs with the smp_mb_*() 4152 * after setting vcpu->mode in vcpu_enter_guest(), thus the vCPU is 4153 * guaranteed to see PID.ON=1 and sync the PIR to IRR if triggering a 4154 * posted interrupt "fails" because vcpu->mode != IN_GUEST_MODE. 4155 */ 4156 kvm_vcpu_trigger_posted_interrupt(vcpu, POSTED_INTR_VECTOR); 4157 return 0; 4158 } 4159 4160 static void vmx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode, 4161 int trig_mode, int vector) 4162 { 4163 struct kvm_vcpu *vcpu = apic->vcpu; 4164 4165 if (vmx_deliver_posted_interrupt(vcpu, vector)) { 4166 kvm_lapic_set_irr(vector, apic); 4167 kvm_make_request(KVM_REQ_EVENT, vcpu); 4168 kvm_vcpu_kick(vcpu); 4169 } else { 4170 trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode, 4171 trig_mode, vector); 4172 } 4173 } 4174 4175 /* 4176 * Set up the vmcs's constant host-state fields, i.e., host-state fields that 4177 * will not change in the lifetime of the guest. 4178 * Note that host-state that does change is set elsewhere. E.g., host-state 4179 * that is set differently for each CPU is set in vmx_vcpu_load(), not here. 4180 */ 4181 void vmx_set_constant_host_state(struct vcpu_vmx *vmx) 4182 { 4183 u32 low32, high32; 4184 unsigned long tmpl; 4185 unsigned long cr0, cr3, cr4; 4186 4187 cr0 = read_cr0(); 4188 WARN_ON(cr0 & X86_CR0_TS); 4189 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ 4190 4191 /* 4192 * Save the most likely value for this task's CR3 in the VMCS. 4193 * We can't use __get_current_cr3_fast() because we're not atomic. 4194 */ 4195 cr3 = __read_cr3(); 4196 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ 4197 vmx->loaded_vmcs->host_state.cr3 = cr3; 4198 4199 /* Save the most likely value for this task's CR4 in the VMCS. */ 4200 cr4 = cr4_read_shadow(); 4201 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ 4202 vmx->loaded_vmcs->host_state.cr4 = cr4; 4203 4204 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ 4205 #ifdef CONFIG_X86_64 4206 /* 4207 * Load null selectors, so we can avoid reloading them in 4208 * vmx_prepare_switch_to_host(), in case userspace uses 4209 * the null selectors too (the expected case). 4210 */ 4211 vmcs_write16(HOST_DS_SELECTOR, 0); 4212 vmcs_write16(HOST_ES_SELECTOR, 0); 4213 #else 4214 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ 4215 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ 4216 #endif 4217 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ 4218 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ 4219 4220 vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */ 4221 4222 vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */ 4223 4224 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); 4225 vmcs_write32(HOST_IA32_SYSENTER_CS, low32); 4226 4227 /* 4228 * SYSENTER is used for 32-bit system calls on either 32-bit or 4229 * 64-bit kernels. It is always zero If neither is allowed, otherwise 4230 * vmx_vcpu_load_vmcs loads it with the per-CPU entry stack (and may 4231 * have already done so!). 4232 */ 4233 if (!IS_ENABLED(CONFIG_IA32_EMULATION) && !IS_ENABLED(CONFIG_X86_32)) 4234 vmcs_writel(HOST_IA32_SYSENTER_ESP, 0); 4235 4236 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); 4237 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ 4238 4239 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { 4240 rdmsr(MSR_IA32_CR_PAT, low32, high32); 4241 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); 4242 } 4243 4244 if (cpu_has_load_ia32_efer()) 4245 vmcs_write64(HOST_IA32_EFER, host_efer); 4246 } 4247 4248 void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) 4249 { 4250 struct kvm_vcpu *vcpu = &vmx->vcpu; 4251 4252 vcpu->arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS & 4253 ~vcpu->arch.cr4_guest_rsvd_bits; 4254 if (!enable_ept) { 4255 vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_TLBFLUSH_BITS; 4256 vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_PDPTR_BITS; 4257 } 4258 if (is_guest_mode(&vmx->vcpu)) 4259 vcpu->arch.cr4_guest_owned_bits &= 4260 ~get_vmcs12(vcpu)->cr4_guest_host_mask; 4261 vmcs_writel(CR4_GUEST_HOST_MASK, ~vcpu->arch.cr4_guest_owned_bits); 4262 } 4263 4264 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) 4265 { 4266 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; 4267 4268 if (!kvm_vcpu_apicv_active(&vmx->vcpu)) 4269 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; 4270 4271 if (!enable_vnmi) 4272 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; 4273 4274 if (!enable_preemption_timer) 4275 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; 4276 4277 return pin_based_exec_ctrl; 4278 } 4279 4280 static u32 vmx_vmentry_ctrl(void) 4281 { 4282 u32 vmentry_ctrl = vmcs_config.vmentry_ctrl; 4283 4284 if (vmx_pt_mode_is_system()) 4285 vmentry_ctrl &= ~(VM_ENTRY_PT_CONCEAL_PIP | 4286 VM_ENTRY_LOAD_IA32_RTIT_CTL); 4287 /* 4288 * IA32e mode, and loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically. 4289 */ 4290 vmentry_ctrl &= ~(VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | 4291 VM_ENTRY_LOAD_IA32_EFER | 4292 VM_ENTRY_IA32E_MODE); 4293 4294 if (cpu_has_perf_global_ctrl_bug()) 4295 vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; 4296 4297 return vmentry_ctrl; 4298 } 4299 4300 static u32 vmx_vmexit_ctrl(void) 4301 { 4302 u32 vmexit_ctrl = vmcs_config.vmexit_ctrl; 4303 4304 /* 4305 * Not used by KVM and never set in vmcs01 or vmcs02, but emulated for 4306 * nested virtualization and thus allowed to be set in vmcs12. 4307 */ 4308 vmexit_ctrl &= ~(VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER | 4309 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER); 4310 4311 if (vmx_pt_mode_is_system()) 4312 vmexit_ctrl &= ~(VM_EXIT_PT_CONCEAL_PIP | 4313 VM_EXIT_CLEAR_IA32_RTIT_CTL); 4314 4315 if (cpu_has_perf_global_ctrl_bug()) 4316 vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; 4317 4318 /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */ 4319 return vmexit_ctrl & 4320 ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | VM_EXIT_LOAD_IA32_EFER); 4321 } 4322 4323 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) 4324 { 4325 struct vcpu_vmx *vmx = to_vmx(vcpu); 4326 4327 if (is_guest_mode(vcpu)) { 4328 vmx->nested.update_vmcs01_apicv_status = true; 4329 return; 4330 } 4331 4332 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx)); 4333 4334 if (kvm_vcpu_apicv_active(vcpu)) { 4335 secondary_exec_controls_setbit(vmx, 4336 SECONDARY_EXEC_APIC_REGISTER_VIRT | 4337 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); 4338 if (enable_ipiv) 4339 tertiary_exec_controls_setbit(vmx, TERTIARY_EXEC_IPI_VIRT); 4340 } else { 4341 secondary_exec_controls_clearbit(vmx, 4342 SECONDARY_EXEC_APIC_REGISTER_VIRT | 4343 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); 4344 if (enable_ipiv) 4345 tertiary_exec_controls_clearbit(vmx, TERTIARY_EXEC_IPI_VIRT); 4346 } 4347 4348 vmx_update_msr_bitmap_x2apic(vcpu); 4349 } 4350 4351 static u32 vmx_exec_control(struct vcpu_vmx *vmx) 4352 { 4353 u32 exec_control = vmcs_config.cpu_based_exec_ctrl; 4354 4355 /* 4356 * Not used by KVM, but fully supported for nesting, i.e. are allowed in 4357 * vmcs12 and propagated to vmcs02 when set in vmcs12. 4358 */ 4359 exec_control &= ~(CPU_BASED_RDTSC_EXITING | 4360 CPU_BASED_USE_IO_BITMAPS | 4361 CPU_BASED_MONITOR_TRAP_FLAG | 4362 CPU_BASED_PAUSE_EXITING); 4363 4364 /* INTR_WINDOW_EXITING and NMI_WINDOW_EXITING are toggled dynamically */ 4365 exec_control &= ~(CPU_BASED_INTR_WINDOW_EXITING | 4366 CPU_BASED_NMI_WINDOW_EXITING); 4367 4368 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) 4369 exec_control &= ~CPU_BASED_MOV_DR_EXITING; 4370 4371 if (!cpu_need_tpr_shadow(&vmx->vcpu)) 4372 exec_control &= ~CPU_BASED_TPR_SHADOW; 4373 4374 #ifdef CONFIG_X86_64 4375 if (exec_control & CPU_BASED_TPR_SHADOW) 4376 exec_control &= ~(CPU_BASED_CR8_LOAD_EXITING | 4377 CPU_BASED_CR8_STORE_EXITING); 4378 else 4379 exec_control |= CPU_BASED_CR8_STORE_EXITING | 4380 CPU_BASED_CR8_LOAD_EXITING; 4381 #endif 4382 /* No need to intercept CR3 access or INVPLG when using EPT. */ 4383 if (enable_ept) 4384 exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | 4385 CPU_BASED_CR3_STORE_EXITING | 4386 CPU_BASED_INVLPG_EXITING); 4387 if (kvm_mwait_in_guest(vmx->vcpu.kvm)) 4388 exec_control &= ~(CPU_BASED_MWAIT_EXITING | 4389 CPU_BASED_MONITOR_EXITING); 4390 if (kvm_hlt_in_guest(vmx->vcpu.kvm)) 4391 exec_control &= ~CPU_BASED_HLT_EXITING; 4392 return exec_control; 4393 } 4394 4395 static u64 vmx_tertiary_exec_control(struct vcpu_vmx *vmx) 4396 { 4397 u64 exec_control = vmcs_config.cpu_based_3rd_exec_ctrl; 4398 4399 /* 4400 * IPI virtualization relies on APICv. Disable IPI virtualization if 4401 * APICv is inhibited. 4402 */ 4403 if (!enable_ipiv || !kvm_vcpu_apicv_active(&vmx->vcpu)) 4404 exec_control &= ~TERTIARY_EXEC_IPI_VIRT; 4405 4406 return exec_control; 4407 } 4408 4409 /* 4410 * Adjust a single secondary execution control bit to intercept/allow an 4411 * instruction in the guest. This is usually done based on whether or not a 4412 * feature has been exposed to the guest in order to correctly emulate faults. 4413 */ 4414 static inline void 4415 vmx_adjust_secondary_exec_control(struct vcpu_vmx *vmx, u32 *exec_control, 4416 u32 control, bool enabled, bool exiting) 4417 { 4418 /* 4419 * If the control is for an opt-in feature, clear the control if the 4420 * feature is not exposed to the guest, i.e. not enabled. If the 4421 * control is opt-out, i.e. an exiting control, clear the control if 4422 * the feature _is_ exposed to the guest, i.e. exiting/interception is 4423 * disabled for the associated instruction. Note, the caller is 4424 * responsible presetting exec_control to set all supported bits. 4425 */ 4426 if (enabled == exiting) 4427 *exec_control &= ~control; 4428 4429 /* 4430 * Update the nested MSR settings so that a nested VMM can/can't set 4431 * controls for features that are/aren't exposed to the guest. 4432 */ 4433 if (nested) { 4434 /* 4435 * All features that can be added or removed to VMX MSRs must 4436 * be supported in the first place for nested virtualization. 4437 */ 4438 if (WARN_ON_ONCE(!(vmcs_config.nested.secondary_ctls_high & control))) 4439 enabled = false; 4440 4441 if (enabled) 4442 vmx->nested.msrs.secondary_ctls_high |= control; 4443 else 4444 vmx->nested.msrs.secondary_ctls_high &= ~control; 4445 } 4446 } 4447 4448 /* 4449 * Wrapper macro for the common case of adjusting a secondary execution control 4450 * based on a single guest CPUID bit, with a dedicated feature bit. This also 4451 * verifies that the control is actually supported by KVM and hardware. 4452 */ 4453 #define vmx_adjust_sec_exec_control(vmx, exec_control, name, feat_name, ctrl_name, exiting) \ 4454 ({ \ 4455 bool __enabled; \ 4456 \ 4457 if (cpu_has_vmx_##name()) { \ 4458 __enabled = guest_cpuid_has(&(vmx)->vcpu, \ 4459 X86_FEATURE_##feat_name); \ 4460 vmx_adjust_secondary_exec_control(vmx, exec_control, \ 4461 SECONDARY_EXEC_##ctrl_name, __enabled, exiting); \ 4462 } \ 4463 }) 4464 4465 /* More macro magic for ENABLE_/opt-in versus _EXITING/opt-out controls. */ 4466 #define vmx_adjust_sec_exec_feature(vmx, exec_control, lname, uname) \ 4467 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, ENABLE_##uname, false) 4468 4469 #define vmx_adjust_sec_exec_exiting(vmx, exec_control, lname, uname) \ 4470 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, uname##_EXITING, true) 4471 4472 static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) 4473 { 4474 struct kvm_vcpu *vcpu = &vmx->vcpu; 4475 4476 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; 4477 4478 if (vmx_pt_mode_is_system()) 4479 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX); 4480 if (!cpu_need_virtualize_apic_accesses(vcpu)) 4481 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; 4482 if (vmx->vpid == 0) 4483 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; 4484 if (!enable_ept) { 4485 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; 4486 enable_unrestricted_guest = 0; 4487 } 4488 if (!enable_unrestricted_guest) 4489 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; 4490 if (kvm_pause_in_guest(vmx->vcpu.kvm)) 4491 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; 4492 if (!kvm_vcpu_apicv_active(vcpu)) 4493 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | 4494 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); 4495 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; 4496 4497 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, 4498 * in vmx_set_cr4. */ 4499 exec_control &= ~SECONDARY_EXEC_DESC; 4500 4501 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD 4502 (handle_vmptrld). 4503 We can NOT enable shadow_vmcs here because we don't have yet 4504 a current VMCS12 4505 */ 4506 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; 4507 4508 /* 4509 * PML is enabled/disabled when dirty logging of memsmlots changes, but 4510 * it needs to be set here when dirty logging is already active, e.g. 4511 * if this vCPU was created after dirty logging was enabled. 4512 */ 4513 if (!vcpu->kvm->arch.cpu_dirty_logging_count) 4514 exec_control &= ~SECONDARY_EXEC_ENABLE_PML; 4515 4516 if (cpu_has_vmx_xsaves()) { 4517 /* Exposing XSAVES only when XSAVE is exposed */ 4518 bool xsaves_enabled = 4519 boot_cpu_has(X86_FEATURE_XSAVE) && 4520 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && 4521 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); 4522 4523 vcpu->arch.xsaves_enabled = xsaves_enabled; 4524 4525 vmx_adjust_secondary_exec_control(vmx, &exec_control, 4526 SECONDARY_EXEC_XSAVES, 4527 xsaves_enabled, false); 4528 } 4529 4530 /* 4531 * RDPID is also gated by ENABLE_RDTSCP, turn on the control if either 4532 * feature is exposed to the guest. This creates a virtualization hole 4533 * if both are supported in hardware but only one is exposed to the 4534 * guest, but letting the guest execute RDTSCP or RDPID when either one 4535 * is advertised is preferable to emulating the advertised instruction 4536 * in KVM on #UD, and obviously better than incorrectly injecting #UD. 4537 */ 4538 if (cpu_has_vmx_rdtscp()) { 4539 bool rdpid_or_rdtscp_enabled = 4540 guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) || 4541 guest_cpuid_has(vcpu, X86_FEATURE_RDPID); 4542 4543 vmx_adjust_secondary_exec_control(vmx, &exec_control, 4544 SECONDARY_EXEC_ENABLE_RDTSCP, 4545 rdpid_or_rdtscp_enabled, false); 4546 } 4547 vmx_adjust_sec_exec_feature(vmx, &exec_control, invpcid, INVPCID); 4548 4549 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdrand, RDRAND); 4550 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdseed, RDSEED); 4551 4552 vmx_adjust_sec_exec_control(vmx, &exec_control, waitpkg, WAITPKG, 4553 ENABLE_USR_WAIT_PAUSE, false); 4554 4555 if (!vcpu->kvm->arch.bus_lock_detection_enabled) 4556 exec_control &= ~SECONDARY_EXEC_BUS_LOCK_DETECTION; 4557 4558 if (!kvm_notify_vmexit_enabled(vcpu->kvm)) 4559 exec_control &= ~SECONDARY_EXEC_NOTIFY_VM_EXITING; 4560 4561 return exec_control; 4562 } 4563 4564 static inline int vmx_get_pid_table_order(struct kvm *kvm) 4565 { 4566 return get_order(kvm->arch.max_vcpu_ids * sizeof(*to_kvm_vmx(kvm)->pid_table)); 4567 } 4568 4569 static int vmx_alloc_ipiv_pid_table(struct kvm *kvm) 4570 { 4571 struct page *pages; 4572 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); 4573 4574 if (!irqchip_in_kernel(kvm) || !enable_ipiv) 4575 return 0; 4576 4577 if (kvm_vmx->pid_table) 4578 return 0; 4579 4580 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, vmx_get_pid_table_order(kvm)); 4581 if (!pages) 4582 return -ENOMEM; 4583 4584 kvm_vmx->pid_table = (void *)page_address(pages); 4585 return 0; 4586 } 4587 4588 static int vmx_vcpu_precreate(struct kvm *kvm) 4589 { 4590 return vmx_alloc_ipiv_pid_table(kvm); 4591 } 4592 4593 #define VMX_XSS_EXIT_BITMAP 0 4594 4595 static void init_vmcs(struct vcpu_vmx *vmx) 4596 { 4597 struct kvm *kvm = vmx->vcpu.kvm; 4598 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); 4599 4600 if (nested) 4601 nested_vmx_set_vmcs_shadowing_bitmap(); 4602 4603 if (cpu_has_vmx_msr_bitmap()) 4604 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); 4605 4606 vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA); /* 22.3.1.5 */ 4607 4608 /* Control */ 4609 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx)); 4610 4611 exec_controls_set(vmx, vmx_exec_control(vmx)); 4612 4613 if (cpu_has_secondary_exec_ctrls()) 4614 secondary_exec_controls_set(vmx, vmx_secondary_exec_control(vmx)); 4615 4616 if (cpu_has_tertiary_exec_ctrls()) 4617 tertiary_exec_controls_set(vmx, vmx_tertiary_exec_control(vmx)); 4618 4619 if (enable_apicv && lapic_in_kernel(&vmx->vcpu)) { 4620 vmcs_write64(EOI_EXIT_BITMAP0, 0); 4621 vmcs_write64(EOI_EXIT_BITMAP1, 0); 4622 vmcs_write64(EOI_EXIT_BITMAP2, 0); 4623 vmcs_write64(EOI_EXIT_BITMAP3, 0); 4624 4625 vmcs_write16(GUEST_INTR_STATUS, 0); 4626 4627 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); 4628 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); 4629 } 4630 4631 if (vmx_can_use_ipiv(&vmx->vcpu)) { 4632 vmcs_write64(PID_POINTER_TABLE, __pa(kvm_vmx->pid_table)); 4633 vmcs_write16(LAST_PID_POINTER_INDEX, kvm->arch.max_vcpu_ids - 1); 4634 } 4635 4636 if (!kvm_pause_in_guest(kvm)) { 4637 vmcs_write32(PLE_GAP, ple_gap); 4638 vmx->ple_window = ple_window; 4639 vmx->ple_window_dirty = true; 4640 } 4641 4642 if (kvm_notify_vmexit_enabled(kvm)) 4643 vmcs_write32(NOTIFY_WINDOW, kvm->arch.notify_window); 4644 4645 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); 4646 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); 4647 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ 4648 4649 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ 4650 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ 4651 vmx_set_constant_host_state(vmx); 4652 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ 4653 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ 4654 4655 if (cpu_has_vmx_vmfunc()) 4656 vmcs_write64(VM_FUNCTION_CONTROL, 0); 4657 4658 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); 4659 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); 4660 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); 4661 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); 4662 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); 4663 4664 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) 4665 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); 4666 4667 vm_exit_controls_set(vmx, vmx_vmexit_ctrl()); 4668 4669 /* 22.2.1, 20.8.1 */ 4670 vm_entry_controls_set(vmx, vmx_vmentry_ctrl()); 4671 4672 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; 4673 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits); 4674 4675 set_cr4_guest_host_mask(vmx); 4676 4677 if (vmx->vpid != 0) 4678 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); 4679 4680 if (cpu_has_vmx_xsaves()) 4681 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); 4682 4683 if (enable_pml) { 4684 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); 4685 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); 4686 } 4687 4688 vmx_write_encls_bitmap(&vmx->vcpu, NULL); 4689 4690 if (vmx_pt_mode_is_host_guest()) { 4691 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc)); 4692 /* Bit[6~0] are forced to 1, writes are ignored. */ 4693 vmx->pt_desc.guest.output_mask = 0x7F; 4694 vmcs_write64(GUEST_IA32_RTIT_CTL, 0); 4695 } 4696 4697 vmcs_write32(GUEST_SYSENTER_CS, 0); 4698 vmcs_writel(GUEST_SYSENTER_ESP, 0); 4699 vmcs_writel(GUEST_SYSENTER_EIP, 0); 4700 vmcs_write64(GUEST_IA32_DEBUGCTL, 0); 4701 4702 if (cpu_has_vmx_tpr_shadow()) { 4703 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); 4704 if (cpu_need_tpr_shadow(&vmx->vcpu)) 4705 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 4706 __pa(vmx->vcpu.arch.apic->regs)); 4707 vmcs_write32(TPR_THRESHOLD, 0); 4708 } 4709 4710 vmx_setup_uret_msrs(vmx); 4711 } 4712 4713 static void __vmx_vcpu_reset(struct kvm_vcpu *vcpu) 4714 { 4715 struct vcpu_vmx *vmx = to_vmx(vcpu); 4716 4717 init_vmcs(vmx); 4718 4719 if (nested) 4720 memcpy(&vmx->nested.msrs, &vmcs_config.nested, sizeof(vmx->nested.msrs)); 4721 4722 vcpu_setup_sgx_lepubkeyhash(vcpu); 4723 4724 vmx->nested.posted_intr_nv = -1; 4725 vmx->nested.vmxon_ptr = INVALID_GPA; 4726 vmx->nested.current_vmptr = INVALID_GPA; 4727 vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID; 4728 4729 vcpu->arch.microcode_version = 0x100000000ULL; 4730 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED; 4731 4732 /* 4733 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR 4734 * or POSTED_INTR_WAKEUP_VECTOR. 4735 */ 4736 vmx->pi_desc.nv = POSTED_INTR_VECTOR; 4737 vmx->pi_desc.sn = 1; 4738 } 4739 4740 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) 4741 { 4742 struct vcpu_vmx *vmx = to_vmx(vcpu); 4743 4744 if (!init_event) 4745 __vmx_vcpu_reset(vcpu); 4746 4747 vmx->rmode.vm86_active = 0; 4748 vmx->spec_ctrl = 0; 4749 4750 vmx->msr_ia32_umwait_control = 0; 4751 4752 vmx->hv_deadline_tsc = -1; 4753 kvm_set_cr8(vcpu, 0); 4754 4755 vmx_segment_cache_clear(vmx); 4756 kvm_register_mark_available(vcpu, VCPU_EXREG_SEGMENTS); 4757 4758 seg_setup(VCPU_SREG_CS); 4759 vmcs_write16(GUEST_CS_SELECTOR, 0xf000); 4760 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); 4761 4762 seg_setup(VCPU_SREG_DS); 4763 seg_setup(VCPU_SREG_ES); 4764 seg_setup(VCPU_SREG_FS); 4765 seg_setup(VCPU_SREG_GS); 4766 seg_setup(VCPU_SREG_SS); 4767 4768 vmcs_write16(GUEST_TR_SELECTOR, 0); 4769 vmcs_writel(GUEST_TR_BASE, 0); 4770 vmcs_write32(GUEST_TR_LIMIT, 0xffff); 4771 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); 4772 4773 vmcs_write16(GUEST_LDTR_SELECTOR, 0); 4774 vmcs_writel(GUEST_LDTR_BASE, 0); 4775 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); 4776 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); 4777 4778 vmcs_writel(GUEST_GDTR_BASE, 0); 4779 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); 4780 4781 vmcs_writel(GUEST_IDTR_BASE, 0); 4782 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); 4783 4784 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); 4785 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); 4786 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); 4787 if (kvm_mpx_supported()) 4788 vmcs_write64(GUEST_BNDCFGS, 0); 4789 4790 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ 4791 4792 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); 4793 4794 vpid_sync_context(vmx->vpid); 4795 4796 vmx_update_fb_clear_dis(vcpu, vmx); 4797 } 4798 4799 static void vmx_enable_irq_window(struct kvm_vcpu *vcpu) 4800 { 4801 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING); 4802 } 4803 4804 static void vmx_enable_nmi_window(struct kvm_vcpu *vcpu) 4805 { 4806 if (!enable_vnmi || 4807 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { 4808 vmx_enable_irq_window(vcpu); 4809 return; 4810 } 4811 4812 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING); 4813 } 4814 4815 static void vmx_inject_irq(struct kvm_vcpu *vcpu, bool reinjected) 4816 { 4817 struct vcpu_vmx *vmx = to_vmx(vcpu); 4818 uint32_t intr; 4819 int irq = vcpu->arch.interrupt.nr; 4820 4821 trace_kvm_inj_virq(irq, vcpu->arch.interrupt.soft, reinjected); 4822 4823 ++vcpu->stat.irq_injections; 4824 if (vmx->rmode.vm86_active) { 4825 int inc_eip = 0; 4826 if (vcpu->arch.interrupt.soft) 4827 inc_eip = vcpu->arch.event_exit_inst_len; 4828 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip); 4829 return; 4830 } 4831 intr = irq | INTR_INFO_VALID_MASK; 4832 if (vcpu->arch.interrupt.soft) { 4833 intr |= INTR_TYPE_SOFT_INTR; 4834 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 4835 vmx->vcpu.arch.event_exit_inst_len); 4836 } else 4837 intr |= INTR_TYPE_EXT_INTR; 4838 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); 4839 4840 vmx_clear_hlt(vcpu); 4841 } 4842 4843 static void vmx_inject_nmi(struct kvm_vcpu *vcpu) 4844 { 4845 struct vcpu_vmx *vmx = to_vmx(vcpu); 4846 4847 if (!enable_vnmi) { 4848 /* 4849 * Tracking the NMI-blocked state in software is built upon 4850 * finding the next open IRQ window. This, in turn, depends on 4851 * well-behaving guests: They have to keep IRQs disabled at 4852 * least as long as the NMI handler runs. Otherwise we may 4853 * cause NMI nesting, maybe breaking the guest. But as this is 4854 * highly unlikely, we can live with the residual risk. 4855 */ 4856 vmx->loaded_vmcs->soft_vnmi_blocked = 1; 4857 vmx->loaded_vmcs->vnmi_blocked_time = 0; 4858 } 4859 4860 ++vcpu->stat.nmi_injections; 4861 vmx->loaded_vmcs->nmi_known_unmasked = false; 4862 4863 if (vmx->rmode.vm86_active) { 4864 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0); 4865 return; 4866 } 4867 4868 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 4869 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); 4870 4871 vmx_clear_hlt(vcpu); 4872 } 4873 4874 bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) 4875 { 4876 struct vcpu_vmx *vmx = to_vmx(vcpu); 4877 bool masked; 4878 4879 if (!enable_vnmi) 4880 return vmx->loaded_vmcs->soft_vnmi_blocked; 4881 if (vmx->loaded_vmcs->nmi_known_unmasked) 4882 return false; 4883 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; 4884 vmx->loaded_vmcs->nmi_known_unmasked = !masked; 4885 return masked; 4886 } 4887 4888 void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) 4889 { 4890 struct vcpu_vmx *vmx = to_vmx(vcpu); 4891 4892 if (!enable_vnmi) { 4893 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { 4894 vmx->loaded_vmcs->soft_vnmi_blocked = masked; 4895 vmx->loaded_vmcs->vnmi_blocked_time = 0; 4896 } 4897 } else { 4898 vmx->loaded_vmcs->nmi_known_unmasked = !masked; 4899 if (masked) 4900 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 4901 GUEST_INTR_STATE_NMI); 4902 else 4903 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, 4904 GUEST_INTR_STATE_NMI); 4905 } 4906 } 4907 4908 bool vmx_nmi_blocked(struct kvm_vcpu *vcpu) 4909 { 4910 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu)) 4911 return false; 4912 4913 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) 4914 return true; 4915 4916 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 4917 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI | 4918 GUEST_INTR_STATE_NMI)); 4919 } 4920 4921 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection) 4922 { 4923 if (to_vmx(vcpu)->nested.nested_run_pending) 4924 return -EBUSY; 4925 4926 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */ 4927 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu)) 4928 return -EBUSY; 4929 4930 return !vmx_nmi_blocked(vcpu); 4931 } 4932 4933 bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu) 4934 { 4935 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) 4936 return false; 4937 4938 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) || 4939 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 4940 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); 4941 } 4942 4943 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) 4944 { 4945 if (to_vmx(vcpu)->nested.nested_run_pending) 4946 return -EBUSY; 4947 4948 /* 4949 * An IRQ must not be injected into L2 if it's supposed to VM-Exit, 4950 * e.g. if the IRQ arrived asynchronously after checking nested events. 4951 */ 4952 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) 4953 return -EBUSY; 4954 4955 return !vmx_interrupt_blocked(vcpu); 4956 } 4957 4958 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) 4959 { 4960 void __user *ret; 4961 4962 if (enable_unrestricted_guest) 4963 return 0; 4964 4965 mutex_lock(&kvm->slots_lock); 4966 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, 4967 PAGE_SIZE * 3); 4968 mutex_unlock(&kvm->slots_lock); 4969 4970 if (IS_ERR(ret)) 4971 return PTR_ERR(ret); 4972 4973 to_kvm_vmx(kvm)->tss_addr = addr; 4974 4975 return init_rmode_tss(kvm, ret); 4976 } 4977 4978 static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) 4979 { 4980 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; 4981 return 0; 4982 } 4983 4984 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) 4985 { 4986 switch (vec) { 4987 case BP_VECTOR: 4988 /* 4989 * Update instruction length as we may reinject the exception 4990 * from user space while in guest debugging mode. 4991 */ 4992 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = 4993 vmcs_read32(VM_EXIT_INSTRUCTION_LEN); 4994 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) 4995 return false; 4996 fallthrough; 4997 case DB_VECTOR: 4998 return !(vcpu->guest_debug & 4999 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)); 5000 case DE_VECTOR: 5001 case OF_VECTOR: 5002 case BR_VECTOR: 5003 case UD_VECTOR: 5004 case DF_VECTOR: 5005 case SS_VECTOR: 5006 case GP_VECTOR: 5007 case MF_VECTOR: 5008 return true; 5009 } 5010 return false; 5011 } 5012 5013 static int handle_rmode_exception(struct kvm_vcpu *vcpu, 5014 int vec, u32 err_code) 5015 { 5016 /* 5017 * Instruction with address size override prefix opcode 0x67 5018 * Cause the #SS fault with 0 error code in VM86 mode. 5019 */ 5020 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { 5021 if (kvm_emulate_instruction(vcpu, 0)) { 5022 if (vcpu->arch.halt_request) { 5023 vcpu->arch.halt_request = 0; 5024 return kvm_emulate_halt_noskip(vcpu); 5025 } 5026 return 1; 5027 } 5028 return 0; 5029 } 5030 5031 /* 5032 * Forward all other exceptions that are valid in real mode. 5033 * FIXME: Breaks guest debugging in real mode, needs to be fixed with 5034 * the required debugging infrastructure rework. 5035 */ 5036 kvm_queue_exception(vcpu, vec); 5037 return 1; 5038 } 5039 5040 static int handle_machine_check(struct kvm_vcpu *vcpu) 5041 { 5042 /* handled by vmx_vcpu_run() */ 5043 return 1; 5044 } 5045 5046 /* 5047 * If the host has split lock detection disabled, then #AC is 5048 * unconditionally injected into the guest, which is the pre split lock 5049 * detection behaviour. 5050 * 5051 * If the host has split lock detection enabled then #AC is 5052 * only injected into the guest when: 5053 * - Guest CPL == 3 (user mode) 5054 * - Guest has #AC detection enabled in CR0 5055 * - Guest EFLAGS has AC bit set 5056 */ 5057 bool vmx_guest_inject_ac(struct kvm_vcpu *vcpu) 5058 { 5059 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) 5060 return true; 5061 5062 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) && 5063 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC); 5064 } 5065 5066 static int handle_exception_nmi(struct kvm_vcpu *vcpu) 5067 { 5068 struct vcpu_vmx *vmx = to_vmx(vcpu); 5069 struct kvm_run *kvm_run = vcpu->run; 5070 u32 intr_info, ex_no, error_code; 5071 unsigned long cr2, dr6; 5072 u32 vect_info; 5073 5074 vect_info = vmx->idt_vectoring_info; 5075 intr_info = vmx_get_intr_info(vcpu); 5076 5077 if (is_machine_check(intr_info) || is_nmi(intr_info)) 5078 return 1; /* handled by handle_exception_nmi_irqoff() */ 5079 5080 /* 5081 * Queue the exception here instead of in handle_nm_fault_irqoff(). 5082 * This ensures the nested_vmx check is not skipped so vmexit can 5083 * be reflected to L1 (when it intercepts #NM) before reaching this 5084 * point. 5085 */ 5086 if (is_nm_fault(intr_info)) { 5087 kvm_queue_exception(vcpu, NM_VECTOR); 5088 return 1; 5089 } 5090 5091 if (is_invalid_opcode(intr_info)) 5092 return handle_ud(vcpu); 5093 5094 error_code = 0; 5095 if (intr_info & INTR_INFO_DELIVER_CODE_MASK) 5096 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); 5097 5098 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { 5099 WARN_ON_ONCE(!enable_vmware_backdoor); 5100 5101 /* 5102 * VMware backdoor emulation on #GP interception only handles 5103 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero 5104 * error code on #GP. 5105 */ 5106 if (error_code) { 5107 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); 5108 return 1; 5109 } 5110 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP); 5111 } 5112 5113 /* 5114 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing 5115 * MMIO, it is better to report an internal error. 5116 * See the comments in vmx_handle_exit. 5117 */ 5118 if ((vect_info & VECTORING_INFO_VALID_MASK) && 5119 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { 5120 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 5121 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; 5122 vcpu->run->internal.ndata = 4; 5123 vcpu->run->internal.data[0] = vect_info; 5124 vcpu->run->internal.data[1] = intr_info; 5125 vcpu->run->internal.data[2] = error_code; 5126 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu; 5127 return 0; 5128 } 5129 5130 if (is_page_fault(intr_info)) { 5131 cr2 = vmx_get_exit_qual(vcpu); 5132 if (enable_ept && !vcpu->arch.apf.host_apf_flags) { 5133 /* 5134 * EPT will cause page fault only if we need to 5135 * detect illegal GPAs. 5136 */ 5137 WARN_ON_ONCE(!allow_smaller_maxphyaddr); 5138 kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code); 5139 return 1; 5140 } else 5141 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); 5142 } 5143 5144 ex_no = intr_info & INTR_INFO_VECTOR_MASK; 5145 5146 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) 5147 return handle_rmode_exception(vcpu, ex_no, error_code); 5148 5149 switch (ex_no) { 5150 case DB_VECTOR: 5151 dr6 = vmx_get_exit_qual(vcpu); 5152 if (!(vcpu->guest_debug & 5153 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { 5154 /* 5155 * If the #DB was due to ICEBP, a.k.a. INT1, skip the 5156 * instruction. ICEBP generates a trap-like #DB, but 5157 * despite its interception control being tied to #DB, 5158 * is an instruction intercept, i.e. the VM-Exit occurs 5159 * on the ICEBP itself. Use the inner "skip" helper to 5160 * avoid single-step #DB and MTF updates, as ICEBP is 5161 * higher priority. Note, skipping ICEBP still clears 5162 * STI and MOVSS blocking. 5163 * 5164 * For all other #DBs, set vmcs.PENDING_DBG_EXCEPTIONS.BS 5165 * if single-step is enabled in RFLAGS and STI or MOVSS 5166 * blocking is active, as the CPU doesn't set the bit 5167 * on VM-Exit due to #DB interception. VM-Entry has a 5168 * consistency check that a single-step #DB is pending 5169 * in this scenario as the previous instruction cannot 5170 * have toggled RFLAGS.TF 0=>1 (because STI and POP/MOV 5171 * don't modify RFLAGS), therefore the one instruction 5172 * delay when activating single-step breakpoints must 5173 * have already expired. Note, the CPU sets/clears BS 5174 * as appropriate for all other VM-Exits types. 5175 */ 5176 if (is_icebp(intr_info)) 5177 WARN_ON(!skip_emulated_instruction(vcpu)); 5178 else if ((vmx_get_rflags(vcpu) & X86_EFLAGS_TF) && 5179 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 5180 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS))) 5181 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 5182 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS) | DR6_BS); 5183 5184 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); 5185 return 1; 5186 } 5187 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; 5188 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); 5189 fallthrough; 5190 case BP_VECTOR: 5191 /* 5192 * Update instruction length as we may reinject #BP from 5193 * user space while in guest debugging mode. Reading it for 5194 * #DB as well causes no harm, it is not used in that case. 5195 */ 5196 vmx->vcpu.arch.event_exit_inst_len = 5197 vmcs_read32(VM_EXIT_INSTRUCTION_LEN); 5198 kvm_run->exit_reason = KVM_EXIT_DEBUG; 5199 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); 5200 kvm_run->debug.arch.exception = ex_no; 5201 break; 5202 case AC_VECTOR: 5203 if (vmx_guest_inject_ac(vcpu)) { 5204 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); 5205 return 1; 5206 } 5207 5208 /* 5209 * Handle split lock. Depending on detection mode this will 5210 * either warn and disable split lock detection for this 5211 * task or force SIGBUS on it. 5212 */ 5213 if (handle_guest_split_lock(kvm_rip_read(vcpu))) 5214 return 1; 5215 fallthrough; 5216 default: 5217 kvm_run->exit_reason = KVM_EXIT_EXCEPTION; 5218 kvm_run->ex.exception = ex_no; 5219 kvm_run->ex.error_code = error_code; 5220 break; 5221 } 5222 return 0; 5223 } 5224 5225 static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu) 5226 { 5227 ++vcpu->stat.irq_exits; 5228 return 1; 5229 } 5230 5231 static int handle_triple_fault(struct kvm_vcpu *vcpu) 5232 { 5233 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; 5234 vcpu->mmio_needed = 0; 5235 return 0; 5236 } 5237 5238 static int handle_io(struct kvm_vcpu *vcpu) 5239 { 5240 unsigned long exit_qualification; 5241 int size, in, string; 5242 unsigned port; 5243 5244 exit_qualification = vmx_get_exit_qual(vcpu); 5245 string = (exit_qualification & 16) != 0; 5246 5247 ++vcpu->stat.io_exits; 5248 5249 if (string) 5250 return kvm_emulate_instruction(vcpu, 0); 5251 5252 port = exit_qualification >> 16; 5253 size = (exit_qualification & 7) + 1; 5254 in = (exit_qualification & 8) != 0; 5255 5256 return kvm_fast_pio(vcpu, size, port, in); 5257 } 5258 5259 static void 5260 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) 5261 { 5262 /* 5263 * Patch in the VMCALL instruction: 5264 */ 5265 hypercall[0] = 0x0f; 5266 hypercall[1] = 0x01; 5267 hypercall[2] = 0xc1; 5268 } 5269 5270 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ 5271 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) 5272 { 5273 if (is_guest_mode(vcpu)) { 5274 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 5275 unsigned long orig_val = val; 5276 5277 /* 5278 * We get here when L2 changed cr0 in a way that did not change 5279 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), 5280 * but did change L0 shadowed bits. So we first calculate the 5281 * effective cr0 value that L1 would like to write into the 5282 * hardware. It consists of the L2-owned bits from the new 5283 * value combined with the L1-owned bits from L1's guest_cr0. 5284 */ 5285 val = (val & ~vmcs12->cr0_guest_host_mask) | 5286 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); 5287 5288 if (!nested_guest_cr0_valid(vcpu, val)) 5289 return 1; 5290 5291 if (kvm_set_cr0(vcpu, val)) 5292 return 1; 5293 vmcs_writel(CR0_READ_SHADOW, orig_val); 5294 return 0; 5295 } else { 5296 if (to_vmx(vcpu)->nested.vmxon && 5297 !nested_host_cr0_valid(vcpu, val)) 5298 return 1; 5299 5300 return kvm_set_cr0(vcpu, val); 5301 } 5302 } 5303 5304 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) 5305 { 5306 if (is_guest_mode(vcpu)) { 5307 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 5308 unsigned long orig_val = val; 5309 5310 /* analogously to handle_set_cr0 */ 5311 val = (val & ~vmcs12->cr4_guest_host_mask) | 5312 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); 5313 if (kvm_set_cr4(vcpu, val)) 5314 return 1; 5315 vmcs_writel(CR4_READ_SHADOW, orig_val); 5316 return 0; 5317 } else 5318 return kvm_set_cr4(vcpu, val); 5319 } 5320 5321 static int handle_desc(struct kvm_vcpu *vcpu) 5322 { 5323 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); 5324 return kvm_emulate_instruction(vcpu, 0); 5325 } 5326 5327 static int handle_cr(struct kvm_vcpu *vcpu) 5328 { 5329 unsigned long exit_qualification, val; 5330 int cr; 5331 int reg; 5332 int err; 5333 int ret; 5334 5335 exit_qualification = vmx_get_exit_qual(vcpu); 5336 cr = exit_qualification & 15; 5337 reg = (exit_qualification >> 8) & 15; 5338 switch ((exit_qualification >> 4) & 3) { 5339 case 0: /* mov to cr */ 5340 val = kvm_register_read(vcpu, reg); 5341 trace_kvm_cr_write(cr, val); 5342 switch (cr) { 5343 case 0: 5344 err = handle_set_cr0(vcpu, val); 5345 return kvm_complete_insn_gp(vcpu, err); 5346 case 3: 5347 WARN_ON_ONCE(enable_unrestricted_guest); 5348 5349 err = kvm_set_cr3(vcpu, val); 5350 return kvm_complete_insn_gp(vcpu, err); 5351 case 4: 5352 err = handle_set_cr4(vcpu, val); 5353 return kvm_complete_insn_gp(vcpu, err); 5354 case 8: { 5355 u8 cr8_prev = kvm_get_cr8(vcpu); 5356 u8 cr8 = (u8)val; 5357 err = kvm_set_cr8(vcpu, cr8); 5358 ret = kvm_complete_insn_gp(vcpu, err); 5359 if (lapic_in_kernel(vcpu)) 5360 return ret; 5361 if (cr8_prev <= cr8) 5362 return ret; 5363 /* 5364 * TODO: we might be squashing a 5365 * KVM_GUESTDBG_SINGLESTEP-triggered 5366 * KVM_EXIT_DEBUG here. 5367 */ 5368 vcpu->run->exit_reason = KVM_EXIT_SET_TPR; 5369 return 0; 5370 } 5371 } 5372 break; 5373 case 2: /* clts */ 5374 KVM_BUG(1, vcpu->kvm, "Guest always owns CR0.TS"); 5375 return -EIO; 5376 case 1: /*mov from cr*/ 5377 switch (cr) { 5378 case 3: 5379 WARN_ON_ONCE(enable_unrestricted_guest); 5380 5381 val = kvm_read_cr3(vcpu); 5382 kvm_register_write(vcpu, reg, val); 5383 trace_kvm_cr_read(cr, val); 5384 return kvm_skip_emulated_instruction(vcpu); 5385 case 8: 5386 val = kvm_get_cr8(vcpu); 5387 kvm_register_write(vcpu, reg, val); 5388 trace_kvm_cr_read(cr, val); 5389 return kvm_skip_emulated_instruction(vcpu); 5390 } 5391 break; 5392 case 3: /* lmsw */ 5393 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; 5394 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); 5395 kvm_lmsw(vcpu, val); 5396 5397 return kvm_skip_emulated_instruction(vcpu); 5398 default: 5399 break; 5400 } 5401 vcpu->run->exit_reason = 0; 5402 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", 5403 (int)(exit_qualification >> 4) & 3, cr); 5404 return 0; 5405 } 5406 5407 static int handle_dr(struct kvm_vcpu *vcpu) 5408 { 5409 unsigned long exit_qualification; 5410 int dr, dr7, reg; 5411 int err = 1; 5412 5413 exit_qualification = vmx_get_exit_qual(vcpu); 5414 dr = exit_qualification & DEBUG_REG_ACCESS_NUM; 5415 5416 /* First, if DR does not exist, trigger UD */ 5417 if (!kvm_require_dr(vcpu, dr)) 5418 return 1; 5419 5420 if (vmx_get_cpl(vcpu) > 0) 5421 goto out; 5422 5423 dr7 = vmcs_readl(GUEST_DR7); 5424 if (dr7 & DR7_GD) { 5425 /* 5426 * As the vm-exit takes precedence over the debug trap, we 5427 * need to emulate the latter, either for the host or the 5428 * guest debugging itself. 5429 */ 5430 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { 5431 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_ACTIVE_LOW; 5432 vcpu->run->debug.arch.dr7 = dr7; 5433 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); 5434 vcpu->run->debug.arch.exception = DB_VECTOR; 5435 vcpu->run->exit_reason = KVM_EXIT_DEBUG; 5436 return 0; 5437 } else { 5438 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD); 5439 return 1; 5440 } 5441 } 5442 5443 if (vcpu->guest_debug == 0) { 5444 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING); 5445 5446 /* 5447 * No more DR vmexits; force a reload of the debug registers 5448 * and reenter on this instruction. The next vmexit will 5449 * retrieve the full state of the debug registers. 5450 */ 5451 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; 5452 return 1; 5453 } 5454 5455 reg = DEBUG_REG_ACCESS_REG(exit_qualification); 5456 if (exit_qualification & TYPE_MOV_FROM_DR) { 5457 unsigned long val; 5458 5459 kvm_get_dr(vcpu, dr, &val); 5460 kvm_register_write(vcpu, reg, val); 5461 err = 0; 5462 } else { 5463 err = kvm_set_dr(vcpu, dr, kvm_register_read(vcpu, reg)); 5464 } 5465 5466 out: 5467 return kvm_complete_insn_gp(vcpu, err); 5468 } 5469 5470 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) 5471 { 5472 get_debugreg(vcpu->arch.db[0], 0); 5473 get_debugreg(vcpu->arch.db[1], 1); 5474 get_debugreg(vcpu->arch.db[2], 2); 5475 get_debugreg(vcpu->arch.db[3], 3); 5476 get_debugreg(vcpu->arch.dr6, 6); 5477 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); 5478 5479 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; 5480 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING); 5481 5482 /* 5483 * exc_debug expects dr6 to be cleared after it runs, avoid that it sees 5484 * a stale dr6 from the guest. 5485 */ 5486 set_debugreg(DR6_RESERVED, 6); 5487 } 5488 5489 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) 5490 { 5491 vmcs_writel(GUEST_DR7, val); 5492 } 5493 5494 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) 5495 { 5496 kvm_apic_update_ppr(vcpu); 5497 return 1; 5498 } 5499 5500 static int handle_interrupt_window(struct kvm_vcpu *vcpu) 5501 { 5502 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING); 5503 5504 kvm_make_request(KVM_REQ_EVENT, vcpu); 5505 5506 ++vcpu->stat.irq_window_exits; 5507 return 1; 5508 } 5509 5510 static int handle_invlpg(struct kvm_vcpu *vcpu) 5511 { 5512 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5513 5514 kvm_mmu_invlpg(vcpu, exit_qualification); 5515 return kvm_skip_emulated_instruction(vcpu); 5516 } 5517 5518 static int handle_apic_access(struct kvm_vcpu *vcpu) 5519 { 5520 if (likely(fasteoi)) { 5521 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5522 int access_type, offset; 5523 5524 access_type = exit_qualification & APIC_ACCESS_TYPE; 5525 offset = exit_qualification & APIC_ACCESS_OFFSET; 5526 /* 5527 * Sane guest uses MOV to write EOI, with written value 5528 * not cared. So make a short-circuit here by avoiding 5529 * heavy instruction emulation. 5530 */ 5531 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && 5532 (offset == APIC_EOI)) { 5533 kvm_lapic_set_eoi(vcpu); 5534 return kvm_skip_emulated_instruction(vcpu); 5535 } 5536 } 5537 return kvm_emulate_instruction(vcpu, 0); 5538 } 5539 5540 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) 5541 { 5542 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5543 int vector = exit_qualification & 0xff; 5544 5545 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ 5546 kvm_apic_set_eoi_accelerated(vcpu, vector); 5547 return 1; 5548 } 5549 5550 static int handle_apic_write(struct kvm_vcpu *vcpu) 5551 { 5552 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5553 5554 /* 5555 * APIC-write VM-Exit is trap-like, KVM doesn't need to advance RIP and 5556 * hardware has done any necessary aliasing, offset adjustments, etc... 5557 * for the access. I.e. the correct value has already been written to 5558 * the vAPIC page for the correct 16-byte chunk. KVM needs only to 5559 * retrieve the register value and emulate the access. 5560 */ 5561 u32 offset = exit_qualification & 0xff0; 5562 5563 kvm_apic_write_nodecode(vcpu, offset); 5564 return 1; 5565 } 5566 5567 static int handle_task_switch(struct kvm_vcpu *vcpu) 5568 { 5569 struct vcpu_vmx *vmx = to_vmx(vcpu); 5570 unsigned long exit_qualification; 5571 bool has_error_code = false; 5572 u32 error_code = 0; 5573 u16 tss_selector; 5574 int reason, type, idt_v, idt_index; 5575 5576 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); 5577 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); 5578 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); 5579 5580 exit_qualification = vmx_get_exit_qual(vcpu); 5581 5582 reason = (u32)exit_qualification >> 30; 5583 if (reason == TASK_SWITCH_GATE && idt_v) { 5584 switch (type) { 5585 case INTR_TYPE_NMI_INTR: 5586 vcpu->arch.nmi_injected = false; 5587 vmx_set_nmi_mask(vcpu, true); 5588 break; 5589 case INTR_TYPE_EXT_INTR: 5590 case INTR_TYPE_SOFT_INTR: 5591 kvm_clear_interrupt_queue(vcpu); 5592 break; 5593 case INTR_TYPE_HARD_EXCEPTION: 5594 if (vmx->idt_vectoring_info & 5595 VECTORING_INFO_DELIVER_CODE_MASK) { 5596 has_error_code = true; 5597 error_code = 5598 vmcs_read32(IDT_VECTORING_ERROR_CODE); 5599 } 5600 fallthrough; 5601 case INTR_TYPE_SOFT_EXCEPTION: 5602 kvm_clear_exception_queue(vcpu); 5603 break; 5604 default: 5605 break; 5606 } 5607 } 5608 tss_selector = exit_qualification; 5609 5610 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && 5611 type != INTR_TYPE_EXT_INTR && 5612 type != INTR_TYPE_NMI_INTR)) 5613 WARN_ON(!skip_emulated_instruction(vcpu)); 5614 5615 /* 5616 * TODO: What about debug traps on tss switch? 5617 * Are we supposed to inject them and update dr6? 5618 */ 5619 return kvm_task_switch(vcpu, tss_selector, 5620 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, 5621 reason, has_error_code, error_code); 5622 } 5623 5624 static int handle_ept_violation(struct kvm_vcpu *vcpu) 5625 { 5626 unsigned long exit_qualification; 5627 gpa_t gpa; 5628 u64 error_code; 5629 5630 exit_qualification = vmx_get_exit_qual(vcpu); 5631 5632 /* 5633 * EPT violation happened while executing iret from NMI, 5634 * "blocked by NMI" bit has to be set before next VM entry. 5635 * There are errata that may cause this bit to not be set: 5636 * AAK134, BY25. 5637 */ 5638 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && 5639 enable_vnmi && 5640 (exit_qualification & INTR_INFO_UNBLOCK_NMI)) 5641 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); 5642 5643 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); 5644 trace_kvm_page_fault(vcpu, gpa, exit_qualification); 5645 5646 /* Is it a read fault? */ 5647 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) 5648 ? PFERR_USER_MASK : 0; 5649 /* Is it a write fault? */ 5650 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) 5651 ? PFERR_WRITE_MASK : 0; 5652 /* Is it a fetch fault? */ 5653 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) 5654 ? PFERR_FETCH_MASK : 0; 5655 /* ept page table entry is present? */ 5656 error_code |= (exit_qualification & EPT_VIOLATION_RWX_MASK) 5657 ? PFERR_PRESENT_MASK : 0; 5658 5659 error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) != 0 ? 5660 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; 5661 5662 vcpu->arch.exit_qualification = exit_qualification; 5663 5664 /* 5665 * Check that the GPA doesn't exceed physical memory limits, as that is 5666 * a guest page fault. We have to emulate the instruction here, because 5667 * if the illegal address is that of a paging structure, then 5668 * EPT_VIOLATION_ACC_WRITE bit is set. Alternatively, if supported we 5669 * would also use advanced VM-exit information for EPT violations to 5670 * reconstruct the page fault error code. 5671 */ 5672 if (unlikely(allow_smaller_maxphyaddr && kvm_vcpu_is_illegal_gpa(vcpu, gpa))) 5673 return kvm_emulate_instruction(vcpu, 0); 5674 5675 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); 5676 } 5677 5678 static int handle_ept_misconfig(struct kvm_vcpu *vcpu) 5679 { 5680 gpa_t gpa; 5681 5682 if (!vmx_can_emulate_instruction(vcpu, EMULTYPE_PF, NULL, 0)) 5683 return 1; 5684 5685 /* 5686 * A nested guest cannot optimize MMIO vmexits, because we have an 5687 * nGPA here instead of the required GPA. 5688 */ 5689 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); 5690 if (!is_guest_mode(vcpu) && 5691 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { 5692 trace_kvm_fast_mmio(gpa); 5693 return kvm_skip_emulated_instruction(vcpu); 5694 } 5695 5696 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); 5697 } 5698 5699 static int handle_nmi_window(struct kvm_vcpu *vcpu) 5700 { 5701 if (KVM_BUG_ON(!enable_vnmi, vcpu->kvm)) 5702 return -EIO; 5703 5704 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING); 5705 ++vcpu->stat.nmi_window_exits; 5706 kvm_make_request(KVM_REQ_EVENT, vcpu); 5707 5708 return 1; 5709 } 5710 5711 static bool vmx_emulation_required_with_pending_exception(struct kvm_vcpu *vcpu) 5712 { 5713 struct vcpu_vmx *vmx = to_vmx(vcpu); 5714 5715 return vmx->emulation_required && !vmx->rmode.vm86_active && 5716 (kvm_is_exception_pending(vcpu) || vcpu->arch.exception.injected); 5717 } 5718 5719 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) 5720 { 5721 struct vcpu_vmx *vmx = to_vmx(vcpu); 5722 bool intr_window_requested; 5723 unsigned count = 130; 5724 5725 intr_window_requested = exec_controls_get(vmx) & 5726 CPU_BASED_INTR_WINDOW_EXITING; 5727 5728 while (vmx->emulation_required && count-- != 0) { 5729 if (intr_window_requested && !vmx_interrupt_blocked(vcpu)) 5730 return handle_interrupt_window(&vmx->vcpu); 5731 5732 if (kvm_test_request(KVM_REQ_EVENT, vcpu)) 5733 return 1; 5734 5735 if (!kvm_emulate_instruction(vcpu, 0)) 5736 return 0; 5737 5738 if (vmx_emulation_required_with_pending_exception(vcpu)) { 5739 kvm_prepare_emulation_failure_exit(vcpu); 5740 return 0; 5741 } 5742 5743 if (vcpu->arch.halt_request) { 5744 vcpu->arch.halt_request = 0; 5745 return kvm_emulate_halt_noskip(vcpu); 5746 } 5747 5748 /* 5749 * Note, return 1 and not 0, vcpu_run() will invoke 5750 * xfer_to_guest_mode() which will create a proper return 5751 * code. 5752 */ 5753 if (__xfer_to_guest_mode_work_pending()) 5754 return 1; 5755 } 5756 5757 return 1; 5758 } 5759 5760 static int vmx_vcpu_pre_run(struct kvm_vcpu *vcpu) 5761 { 5762 if (vmx_emulation_required_with_pending_exception(vcpu)) { 5763 kvm_prepare_emulation_failure_exit(vcpu); 5764 return 0; 5765 } 5766 5767 return 1; 5768 } 5769 5770 static void grow_ple_window(struct kvm_vcpu *vcpu) 5771 { 5772 struct vcpu_vmx *vmx = to_vmx(vcpu); 5773 unsigned int old = vmx->ple_window; 5774 5775 vmx->ple_window = __grow_ple_window(old, ple_window, 5776 ple_window_grow, 5777 ple_window_max); 5778 5779 if (vmx->ple_window != old) { 5780 vmx->ple_window_dirty = true; 5781 trace_kvm_ple_window_update(vcpu->vcpu_id, 5782 vmx->ple_window, old); 5783 } 5784 } 5785 5786 static void shrink_ple_window(struct kvm_vcpu *vcpu) 5787 { 5788 struct vcpu_vmx *vmx = to_vmx(vcpu); 5789 unsigned int old = vmx->ple_window; 5790 5791 vmx->ple_window = __shrink_ple_window(old, ple_window, 5792 ple_window_shrink, 5793 ple_window); 5794 5795 if (vmx->ple_window != old) { 5796 vmx->ple_window_dirty = true; 5797 trace_kvm_ple_window_update(vcpu->vcpu_id, 5798 vmx->ple_window, old); 5799 } 5800 } 5801 5802 /* 5803 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE 5804 * exiting, so only get here on cpu with PAUSE-Loop-Exiting. 5805 */ 5806 static int handle_pause(struct kvm_vcpu *vcpu) 5807 { 5808 if (!kvm_pause_in_guest(vcpu->kvm)) 5809 grow_ple_window(vcpu); 5810 5811 /* 5812 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" 5813 * VM-execution control is ignored if CPL > 0. OTOH, KVM 5814 * never set PAUSE_EXITING and just set PLE if supported, 5815 * so the vcpu must be CPL=0 if it gets a PAUSE exit. 5816 */ 5817 kvm_vcpu_on_spin(vcpu, true); 5818 return kvm_skip_emulated_instruction(vcpu); 5819 } 5820 5821 static int handle_monitor_trap(struct kvm_vcpu *vcpu) 5822 { 5823 return 1; 5824 } 5825 5826 static int handle_invpcid(struct kvm_vcpu *vcpu) 5827 { 5828 u32 vmx_instruction_info; 5829 unsigned long type; 5830 gva_t gva; 5831 struct { 5832 u64 pcid; 5833 u64 gla; 5834 } operand; 5835 int gpr_index; 5836 5837 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { 5838 kvm_queue_exception(vcpu, UD_VECTOR); 5839 return 1; 5840 } 5841 5842 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); 5843 gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); 5844 type = kvm_register_read(vcpu, gpr_index); 5845 5846 /* According to the Intel instruction reference, the memory operand 5847 * is read even if it isn't needed (e.g., for type==all) 5848 */ 5849 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), 5850 vmx_instruction_info, false, 5851 sizeof(operand), &gva)) 5852 return 1; 5853 5854 return kvm_handle_invpcid(vcpu, type, gva); 5855 } 5856 5857 static int handle_pml_full(struct kvm_vcpu *vcpu) 5858 { 5859 unsigned long exit_qualification; 5860 5861 trace_kvm_pml_full(vcpu->vcpu_id); 5862 5863 exit_qualification = vmx_get_exit_qual(vcpu); 5864 5865 /* 5866 * PML buffer FULL happened while executing iret from NMI, 5867 * "blocked by NMI" bit has to be set before next VM entry. 5868 */ 5869 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && 5870 enable_vnmi && 5871 (exit_qualification & INTR_INFO_UNBLOCK_NMI)) 5872 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 5873 GUEST_INTR_STATE_NMI); 5874 5875 /* 5876 * PML buffer already flushed at beginning of VMEXIT. Nothing to do 5877 * here.., and there's no userspace involvement needed for PML. 5878 */ 5879 return 1; 5880 } 5881 5882 static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu) 5883 { 5884 struct vcpu_vmx *vmx = to_vmx(vcpu); 5885 5886 if (!vmx->req_immediate_exit && 5887 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) { 5888 kvm_lapic_expired_hv_timer(vcpu); 5889 return EXIT_FASTPATH_REENTER_GUEST; 5890 } 5891 5892 return EXIT_FASTPATH_NONE; 5893 } 5894 5895 static int handle_preemption_timer(struct kvm_vcpu *vcpu) 5896 { 5897 handle_fastpath_preemption_timer(vcpu); 5898 return 1; 5899 } 5900 5901 /* 5902 * When nested=0, all VMX instruction VM Exits filter here. The handlers 5903 * are overwritten by nested_vmx_setup() when nested=1. 5904 */ 5905 static int handle_vmx_instruction(struct kvm_vcpu *vcpu) 5906 { 5907 kvm_queue_exception(vcpu, UD_VECTOR); 5908 return 1; 5909 } 5910 5911 #ifndef CONFIG_X86_SGX_KVM 5912 static int handle_encls(struct kvm_vcpu *vcpu) 5913 { 5914 /* 5915 * SGX virtualization is disabled. There is no software enable bit for 5916 * SGX, so KVM intercepts all ENCLS leafs and injects a #UD to prevent 5917 * the guest from executing ENCLS (when SGX is supported by hardware). 5918 */ 5919 kvm_queue_exception(vcpu, UD_VECTOR); 5920 return 1; 5921 } 5922 #endif /* CONFIG_X86_SGX_KVM */ 5923 5924 static int handle_bus_lock_vmexit(struct kvm_vcpu *vcpu) 5925 { 5926 /* 5927 * Hardware may or may not set the BUS_LOCK_DETECTED flag on BUS_LOCK 5928 * VM-Exits. Unconditionally set the flag here and leave the handling to 5929 * vmx_handle_exit(). 5930 */ 5931 to_vmx(vcpu)->exit_reason.bus_lock_detected = true; 5932 return 1; 5933 } 5934 5935 static int handle_notify(struct kvm_vcpu *vcpu) 5936 { 5937 unsigned long exit_qual = vmx_get_exit_qual(vcpu); 5938 bool context_invalid = exit_qual & NOTIFY_VM_CONTEXT_INVALID; 5939 5940 ++vcpu->stat.notify_window_exits; 5941 5942 /* 5943 * Notify VM exit happened while executing iret from NMI, 5944 * "blocked by NMI" bit has to be set before next VM entry. 5945 */ 5946 if (enable_vnmi && (exit_qual & INTR_INFO_UNBLOCK_NMI)) 5947 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 5948 GUEST_INTR_STATE_NMI); 5949 5950 if (vcpu->kvm->arch.notify_vmexit_flags & KVM_X86_NOTIFY_VMEXIT_USER || 5951 context_invalid) { 5952 vcpu->run->exit_reason = KVM_EXIT_NOTIFY; 5953 vcpu->run->notify.flags = context_invalid ? 5954 KVM_NOTIFY_CONTEXT_INVALID : 0; 5955 return 0; 5956 } 5957 5958 return 1; 5959 } 5960 5961 /* 5962 * The exit handlers return 1 if the exit was handled fully and guest execution 5963 * may resume. Otherwise they set the kvm_run parameter to indicate what needs 5964 * to be done to userspace and return 0. 5965 */ 5966 static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { 5967 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi, 5968 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, 5969 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, 5970 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, 5971 [EXIT_REASON_IO_INSTRUCTION] = handle_io, 5972 [EXIT_REASON_CR_ACCESS] = handle_cr, 5973 [EXIT_REASON_DR_ACCESS] = handle_dr, 5974 [EXIT_REASON_CPUID] = kvm_emulate_cpuid, 5975 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr, 5976 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr, 5977 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window, 5978 [EXIT_REASON_HLT] = kvm_emulate_halt, 5979 [EXIT_REASON_INVD] = kvm_emulate_invd, 5980 [EXIT_REASON_INVLPG] = handle_invlpg, 5981 [EXIT_REASON_RDPMC] = kvm_emulate_rdpmc, 5982 [EXIT_REASON_VMCALL] = kvm_emulate_hypercall, 5983 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction, 5984 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction, 5985 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction, 5986 [EXIT_REASON_VMPTRST] = handle_vmx_instruction, 5987 [EXIT_REASON_VMREAD] = handle_vmx_instruction, 5988 [EXIT_REASON_VMRESUME] = handle_vmx_instruction, 5989 [EXIT_REASON_VMWRITE] = handle_vmx_instruction, 5990 [EXIT_REASON_VMOFF] = handle_vmx_instruction, 5991 [EXIT_REASON_VMON] = handle_vmx_instruction, 5992 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, 5993 [EXIT_REASON_APIC_ACCESS] = handle_apic_access, 5994 [EXIT_REASON_APIC_WRITE] = handle_apic_write, 5995 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, 5996 [EXIT_REASON_WBINVD] = kvm_emulate_wbinvd, 5997 [EXIT_REASON_XSETBV] = kvm_emulate_xsetbv, 5998 [EXIT_REASON_TASK_SWITCH] = handle_task_switch, 5999 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, 6000 [EXIT_REASON_GDTR_IDTR] = handle_desc, 6001 [EXIT_REASON_LDTR_TR] = handle_desc, 6002 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, 6003 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, 6004 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, 6005 [EXIT_REASON_MWAIT_INSTRUCTION] = kvm_emulate_mwait, 6006 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, 6007 [EXIT_REASON_MONITOR_INSTRUCTION] = kvm_emulate_monitor, 6008 [EXIT_REASON_INVEPT] = handle_vmx_instruction, 6009 [EXIT_REASON_INVVPID] = handle_vmx_instruction, 6010 [EXIT_REASON_RDRAND] = kvm_handle_invalid_op, 6011 [EXIT_REASON_RDSEED] = kvm_handle_invalid_op, 6012 [EXIT_REASON_PML_FULL] = handle_pml_full, 6013 [EXIT_REASON_INVPCID] = handle_invpcid, 6014 [EXIT_REASON_VMFUNC] = handle_vmx_instruction, 6015 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, 6016 [EXIT_REASON_ENCLS] = handle_encls, 6017 [EXIT_REASON_BUS_LOCK] = handle_bus_lock_vmexit, 6018 [EXIT_REASON_NOTIFY] = handle_notify, 6019 }; 6020 6021 static const int kvm_vmx_max_exit_handlers = 6022 ARRAY_SIZE(kvm_vmx_exit_handlers); 6023 6024 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, 6025 u64 *info1, u64 *info2, 6026 u32 *intr_info, u32 *error_code) 6027 { 6028 struct vcpu_vmx *vmx = to_vmx(vcpu); 6029 6030 *reason = vmx->exit_reason.full; 6031 *info1 = vmx_get_exit_qual(vcpu); 6032 if (!(vmx->exit_reason.failed_vmentry)) { 6033 *info2 = vmx->idt_vectoring_info; 6034 *intr_info = vmx_get_intr_info(vcpu); 6035 if (is_exception_with_error_code(*intr_info)) 6036 *error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); 6037 else 6038 *error_code = 0; 6039 } else { 6040 *info2 = 0; 6041 *intr_info = 0; 6042 *error_code = 0; 6043 } 6044 } 6045 6046 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) 6047 { 6048 if (vmx->pml_pg) { 6049 __free_page(vmx->pml_pg); 6050 vmx->pml_pg = NULL; 6051 } 6052 } 6053 6054 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) 6055 { 6056 struct vcpu_vmx *vmx = to_vmx(vcpu); 6057 u64 *pml_buf; 6058 u16 pml_idx; 6059 6060 pml_idx = vmcs_read16(GUEST_PML_INDEX); 6061 6062 /* Do nothing if PML buffer is empty */ 6063 if (pml_idx == (PML_ENTITY_NUM - 1)) 6064 return; 6065 6066 /* PML index always points to next available PML buffer entity */ 6067 if (pml_idx >= PML_ENTITY_NUM) 6068 pml_idx = 0; 6069 else 6070 pml_idx++; 6071 6072 pml_buf = page_address(vmx->pml_pg); 6073 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { 6074 u64 gpa; 6075 6076 gpa = pml_buf[pml_idx]; 6077 WARN_ON(gpa & (PAGE_SIZE - 1)); 6078 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); 6079 } 6080 6081 /* reset PML index */ 6082 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); 6083 } 6084 6085 static void vmx_dump_sel(char *name, uint32_t sel) 6086 { 6087 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", 6088 name, vmcs_read16(sel), 6089 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), 6090 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), 6091 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); 6092 } 6093 6094 static void vmx_dump_dtsel(char *name, uint32_t limit) 6095 { 6096 pr_err("%s limit=0x%08x, base=0x%016lx\n", 6097 name, vmcs_read32(limit), 6098 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); 6099 } 6100 6101 static void vmx_dump_msrs(char *name, struct vmx_msrs *m) 6102 { 6103 unsigned int i; 6104 struct vmx_msr_entry *e; 6105 6106 pr_err("MSR %s:\n", name); 6107 for (i = 0, e = m->val; i < m->nr; ++i, ++e) 6108 pr_err(" %2d: msr=0x%08x value=0x%016llx\n", i, e->index, e->value); 6109 } 6110 6111 void dump_vmcs(struct kvm_vcpu *vcpu) 6112 { 6113 struct vcpu_vmx *vmx = to_vmx(vcpu); 6114 u32 vmentry_ctl, vmexit_ctl; 6115 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control; 6116 u64 tertiary_exec_control; 6117 unsigned long cr4; 6118 int efer_slot; 6119 6120 if (!dump_invalid_vmcs) { 6121 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n"); 6122 return; 6123 } 6124 6125 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); 6126 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); 6127 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); 6128 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); 6129 cr4 = vmcs_readl(GUEST_CR4); 6130 6131 if (cpu_has_secondary_exec_ctrls()) 6132 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); 6133 else 6134 secondary_exec_control = 0; 6135 6136 if (cpu_has_tertiary_exec_ctrls()) 6137 tertiary_exec_control = vmcs_read64(TERTIARY_VM_EXEC_CONTROL); 6138 else 6139 tertiary_exec_control = 0; 6140 6141 pr_err("VMCS %p, last attempted VM-entry on CPU %d\n", 6142 vmx->loaded_vmcs->vmcs, vcpu->arch.last_vmentry_cpu); 6143 pr_err("*** Guest State ***\n"); 6144 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", 6145 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), 6146 vmcs_readl(CR0_GUEST_HOST_MASK)); 6147 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", 6148 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); 6149 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); 6150 if (cpu_has_vmx_ept()) { 6151 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", 6152 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); 6153 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", 6154 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); 6155 } 6156 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", 6157 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); 6158 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", 6159 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); 6160 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", 6161 vmcs_readl(GUEST_SYSENTER_ESP), 6162 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); 6163 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); 6164 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); 6165 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); 6166 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); 6167 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); 6168 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); 6169 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); 6170 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); 6171 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); 6172 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); 6173 efer_slot = vmx_find_loadstore_msr_slot(&vmx->msr_autoload.guest, MSR_EFER); 6174 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_EFER) 6175 pr_err("EFER= 0x%016llx\n", vmcs_read64(GUEST_IA32_EFER)); 6176 else if (efer_slot >= 0) 6177 pr_err("EFER= 0x%016llx (autoload)\n", 6178 vmx->msr_autoload.guest.val[efer_slot].value); 6179 else if (vmentry_ctl & VM_ENTRY_IA32E_MODE) 6180 pr_err("EFER= 0x%016llx (effective)\n", 6181 vcpu->arch.efer | (EFER_LMA | EFER_LME)); 6182 else 6183 pr_err("EFER= 0x%016llx (effective)\n", 6184 vcpu->arch.efer & ~(EFER_LMA | EFER_LME)); 6185 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PAT) 6186 pr_err("PAT = 0x%016llx\n", vmcs_read64(GUEST_IA32_PAT)); 6187 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", 6188 vmcs_read64(GUEST_IA32_DEBUGCTL), 6189 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); 6190 if (cpu_has_load_perf_global_ctrl() && 6191 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) 6192 pr_err("PerfGlobCtl = 0x%016llx\n", 6193 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); 6194 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) 6195 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); 6196 pr_err("Interruptibility = %08x ActivityState = %08x\n", 6197 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), 6198 vmcs_read32(GUEST_ACTIVITY_STATE)); 6199 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) 6200 pr_err("InterruptStatus = %04x\n", 6201 vmcs_read16(GUEST_INTR_STATUS)); 6202 if (vmcs_read32(VM_ENTRY_MSR_LOAD_COUNT) > 0) 6203 vmx_dump_msrs("guest autoload", &vmx->msr_autoload.guest); 6204 if (vmcs_read32(VM_EXIT_MSR_STORE_COUNT) > 0) 6205 vmx_dump_msrs("guest autostore", &vmx->msr_autostore.guest); 6206 6207 pr_err("*** Host State ***\n"); 6208 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", 6209 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); 6210 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", 6211 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), 6212 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), 6213 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), 6214 vmcs_read16(HOST_TR_SELECTOR)); 6215 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", 6216 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), 6217 vmcs_readl(HOST_TR_BASE)); 6218 pr_err("GDTBase=%016lx IDTBase=%016lx\n", 6219 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); 6220 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", 6221 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), 6222 vmcs_readl(HOST_CR4)); 6223 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", 6224 vmcs_readl(HOST_IA32_SYSENTER_ESP), 6225 vmcs_read32(HOST_IA32_SYSENTER_CS), 6226 vmcs_readl(HOST_IA32_SYSENTER_EIP)); 6227 if (vmexit_ctl & VM_EXIT_LOAD_IA32_EFER) 6228 pr_err("EFER= 0x%016llx\n", vmcs_read64(HOST_IA32_EFER)); 6229 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PAT) 6230 pr_err("PAT = 0x%016llx\n", vmcs_read64(HOST_IA32_PAT)); 6231 if (cpu_has_load_perf_global_ctrl() && 6232 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) 6233 pr_err("PerfGlobCtl = 0x%016llx\n", 6234 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); 6235 if (vmcs_read32(VM_EXIT_MSR_LOAD_COUNT) > 0) 6236 vmx_dump_msrs("host autoload", &vmx->msr_autoload.host); 6237 6238 pr_err("*** Control State ***\n"); 6239 pr_err("CPUBased=0x%08x SecondaryExec=0x%08x TertiaryExec=0x%016llx\n", 6240 cpu_based_exec_ctrl, secondary_exec_control, tertiary_exec_control); 6241 pr_err("PinBased=0x%08x EntryControls=%08x ExitControls=%08x\n", 6242 pin_based_exec_ctrl, vmentry_ctl, vmexit_ctl); 6243 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", 6244 vmcs_read32(EXCEPTION_BITMAP), 6245 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), 6246 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); 6247 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", 6248 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), 6249 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), 6250 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); 6251 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", 6252 vmcs_read32(VM_EXIT_INTR_INFO), 6253 vmcs_read32(VM_EXIT_INTR_ERROR_CODE), 6254 vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); 6255 pr_err(" reason=%08x qualification=%016lx\n", 6256 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); 6257 pr_err("IDTVectoring: info=%08x errcode=%08x\n", 6258 vmcs_read32(IDT_VECTORING_INFO_FIELD), 6259 vmcs_read32(IDT_VECTORING_ERROR_CODE)); 6260 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); 6261 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) 6262 pr_err("TSC Multiplier = 0x%016llx\n", 6263 vmcs_read64(TSC_MULTIPLIER)); 6264 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) { 6265 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { 6266 u16 status = vmcs_read16(GUEST_INTR_STATUS); 6267 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff); 6268 } 6269 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); 6270 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) 6271 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR)); 6272 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR)); 6273 } 6274 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) 6275 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); 6276 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) 6277 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); 6278 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) 6279 pr_err("PLE Gap=%08x Window=%08x\n", 6280 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); 6281 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) 6282 pr_err("Virtual processor ID = 0x%04x\n", 6283 vmcs_read16(VIRTUAL_PROCESSOR_ID)); 6284 } 6285 6286 /* 6287 * The guest has exited. See if we can fix it or if we need userspace 6288 * assistance. 6289 */ 6290 static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) 6291 { 6292 struct vcpu_vmx *vmx = to_vmx(vcpu); 6293 union vmx_exit_reason exit_reason = vmx->exit_reason; 6294 u32 vectoring_info = vmx->idt_vectoring_info; 6295 u16 exit_handler_index; 6296 6297 /* 6298 * Flush logged GPAs PML buffer, this will make dirty_bitmap more 6299 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before 6300 * querying dirty_bitmap, we only need to kick all vcpus out of guest 6301 * mode as if vcpus is in root mode, the PML buffer must has been 6302 * flushed already. Note, PML is never enabled in hardware while 6303 * running L2. 6304 */ 6305 if (enable_pml && !is_guest_mode(vcpu)) 6306 vmx_flush_pml_buffer(vcpu); 6307 6308 /* 6309 * KVM should never reach this point with a pending nested VM-Enter. 6310 * More specifically, short-circuiting VM-Entry to emulate L2 due to 6311 * invalid guest state should never happen as that means KVM knowingly 6312 * allowed a nested VM-Enter with an invalid vmcs12. More below. 6313 */ 6314 if (KVM_BUG_ON(vmx->nested.nested_run_pending, vcpu->kvm)) 6315 return -EIO; 6316 6317 if (is_guest_mode(vcpu)) { 6318 /* 6319 * PML is never enabled when running L2, bail immediately if a 6320 * PML full exit occurs as something is horribly wrong. 6321 */ 6322 if (exit_reason.basic == EXIT_REASON_PML_FULL) 6323 goto unexpected_vmexit; 6324 6325 /* 6326 * The host physical addresses of some pages of guest memory 6327 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC 6328 * Page). The CPU may write to these pages via their host 6329 * physical address while L2 is running, bypassing any 6330 * address-translation-based dirty tracking (e.g. EPT write 6331 * protection). 6332 * 6333 * Mark them dirty on every exit from L2 to prevent them from 6334 * getting out of sync with dirty tracking. 6335 */ 6336 nested_mark_vmcs12_pages_dirty(vcpu); 6337 6338 /* 6339 * Synthesize a triple fault if L2 state is invalid. In normal 6340 * operation, nested VM-Enter rejects any attempt to enter L2 6341 * with invalid state. However, those checks are skipped if 6342 * state is being stuffed via RSM or KVM_SET_NESTED_STATE. If 6343 * L2 state is invalid, it means either L1 modified SMRAM state 6344 * or userspace provided bad state. Synthesize TRIPLE_FAULT as 6345 * doing so is architecturally allowed in the RSM case, and is 6346 * the least awful solution for the userspace case without 6347 * risking false positives. 6348 */ 6349 if (vmx->emulation_required) { 6350 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); 6351 return 1; 6352 } 6353 6354 if (nested_vmx_reflect_vmexit(vcpu)) 6355 return 1; 6356 } 6357 6358 /* If guest state is invalid, start emulating. L2 is handled above. */ 6359 if (vmx->emulation_required) 6360 return handle_invalid_guest_state(vcpu); 6361 6362 if (exit_reason.failed_vmentry) { 6363 dump_vmcs(vcpu); 6364 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; 6365 vcpu->run->fail_entry.hardware_entry_failure_reason 6366 = exit_reason.full; 6367 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; 6368 return 0; 6369 } 6370 6371 if (unlikely(vmx->fail)) { 6372 dump_vmcs(vcpu); 6373 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; 6374 vcpu->run->fail_entry.hardware_entry_failure_reason 6375 = vmcs_read32(VM_INSTRUCTION_ERROR); 6376 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; 6377 return 0; 6378 } 6379 6380 /* 6381 * Note: 6382 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by 6383 * delivery event since it indicates guest is accessing MMIO. 6384 * The vm-exit can be triggered again after return to guest that 6385 * will cause infinite loop. 6386 */ 6387 if ((vectoring_info & VECTORING_INFO_VALID_MASK) && 6388 (exit_reason.basic != EXIT_REASON_EXCEPTION_NMI && 6389 exit_reason.basic != EXIT_REASON_EPT_VIOLATION && 6390 exit_reason.basic != EXIT_REASON_PML_FULL && 6391 exit_reason.basic != EXIT_REASON_APIC_ACCESS && 6392 exit_reason.basic != EXIT_REASON_TASK_SWITCH && 6393 exit_reason.basic != EXIT_REASON_NOTIFY)) { 6394 int ndata = 3; 6395 6396 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 6397 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; 6398 vcpu->run->internal.data[0] = vectoring_info; 6399 vcpu->run->internal.data[1] = exit_reason.full; 6400 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; 6401 if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG) { 6402 vcpu->run->internal.data[ndata++] = 6403 vmcs_read64(GUEST_PHYSICAL_ADDRESS); 6404 } 6405 vcpu->run->internal.data[ndata++] = vcpu->arch.last_vmentry_cpu; 6406 vcpu->run->internal.ndata = ndata; 6407 return 0; 6408 } 6409 6410 if (unlikely(!enable_vnmi && 6411 vmx->loaded_vmcs->soft_vnmi_blocked)) { 6412 if (!vmx_interrupt_blocked(vcpu)) { 6413 vmx->loaded_vmcs->soft_vnmi_blocked = 0; 6414 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && 6415 vcpu->arch.nmi_pending) { 6416 /* 6417 * This CPU don't support us in finding the end of an 6418 * NMI-blocked window if the guest runs with IRQs 6419 * disabled. So we pull the trigger after 1 s of 6420 * futile waiting, but inform the user about this. 6421 */ 6422 printk(KERN_WARNING "%s: Breaking out of NMI-blocked " 6423 "state on VCPU %d after 1 s timeout\n", 6424 __func__, vcpu->vcpu_id); 6425 vmx->loaded_vmcs->soft_vnmi_blocked = 0; 6426 } 6427 } 6428 6429 if (exit_fastpath != EXIT_FASTPATH_NONE) 6430 return 1; 6431 6432 if (exit_reason.basic >= kvm_vmx_max_exit_handlers) 6433 goto unexpected_vmexit; 6434 #ifdef CONFIG_RETPOLINE 6435 if (exit_reason.basic == EXIT_REASON_MSR_WRITE) 6436 return kvm_emulate_wrmsr(vcpu); 6437 else if (exit_reason.basic == EXIT_REASON_PREEMPTION_TIMER) 6438 return handle_preemption_timer(vcpu); 6439 else if (exit_reason.basic == EXIT_REASON_INTERRUPT_WINDOW) 6440 return handle_interrupt_window(vcpu); 6441 else if (exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT) 6442 return handle_external_interrupt(vcpu); 6443 else if (exit_reason.basic == EXIT_REASON_HLT) 6444 return kvm_emulate_halt(vcpu); 6445 else if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG) 6446 return handle_ept_misconfig(vcpu); 6447 #endif 6448 6449 exit_handler_index = array_index_nospec((u16)exit_reason.basic, 6450 kvm_vmx_max_exit_handlers); 6451 if (!kvm_vmx_exit_handlers[exit_handler_index]) 6452 goto unexpected_vmexit; 6453 6454 return kvm_vmx_exit_handlers[exit_handler_index](vcpu); 6455 6456 unexpected_vmexit: 6457 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", 6458 exit_reason.full); 6459 dump_vmcs(vcpu); 6460 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 6461 vcpu->run->internal.suberror = 6462 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON; 6463 vcpu->run->internal.ndata = 2; 6464 vcpu->run->internal.data[0] = exit_reason.full; 6465 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; 6466 return 0; 6467 } 6468 6469 static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) 6470 { 6471 int ret = __vmx_handle_exit(vcpu, exit_fastpath); 6472 6473 /* 6474 * Exit to user space when bus lock detected to inform that there is 6475 * a bus lock in guest. 6476 */ 6477 if (to_vmx(vcpu)->exit_reason.bus_lock_detected) { 6478 if (ret > 0) 6479 vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK; 6480 6481 vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK; 6482 return 0; 6483 } 6484 return ret; 6485 } 6486 6487 /* 6488 * Software based L1D cache flush which is used when microcode providing 6489 * the cache control MSR is not loaded. 6490 * 6491 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to 6492 * flush it is required to read in 64 KiB because the replacement algorithm 6493 * is not exactly LRU. This could be sized at runtime via topology 6494 * information but as all relevant affected CPUs have 32KiB L1D cache size 6495 * there is no point in doing so. 6496 */ 6497 static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu) 6498 { 6499 int size = PAGE_SIZE << L1D_CACHE_ORDER; 6500 6501 /* 6502 * This code is only executed when the flush mode is 'cond' or 6503 * 'always' 6504 */ 6505 if (static_branch_likely(&vmx_l1d_flush_cond)) { 6506 bool flush_l1d; 6507 6508 /* 6509 * Clear the per-vcpu flush bit, it gets set again 6510 * either from vcpu_run() or from one of the unsafe 6511 * VMEXIT handlers. 6512 */ 6513 flush_l1d = vcpu->arch.l1tf_flush_l1d; 6514 vcpu->arch.l1tf_flush_l1d = false; 6515 6516 /* 6517 * Clear the per-cpu flush bit, it gets set again from 6518 * the interrupt handlers. 6519 */ 6520 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d(); 6521 kvm_clear_cpu_l1tf_flush_l1d(); 6522 6523 if (!flush_l1d) 6524 return; 6525 } 6526 6527 vcpu->stat.l1d_flush++; 6528 6529 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { 6530 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); 6531 return; 6532 } 6533 6534 asm volatile( 6535 /* First ensure the pages are in the TLB */ 6536 "xorl %%eax, %%eax\n" 6537 ".Lpopulate_tlb:\n\t" 6538 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" 6539 "addl $4096, %%eax\n\t" 6540 "cmpl %%eax, %[size]\n\t" 6541 "jne .Lpopulate_tlb\n\t" 6542 "xorl %%eax, %%eax\n\t" 6543 "cpuid\n\t" 6544 /* Now fill the cache */ 6545 "xorl %%eax, %%eax\n" 6546 ".Lfill_cache:\n" 6547 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" 6548 "addl $64, %%eax\n\t" 6549 "cmpl %%eax, %[size]\n\t" 6550 "jne .Lfill_cache\n\t" 6551 "lfence\n" 6552 :: [flush_pages] "r" (vmx_l1d_flush_pages), 6553 [size] "r" (size) 6554 : "eax", "ebx", "ecx", "edx"); 6555 } 6556 6557 static void vmx_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) 6558 { 6559 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 6560 int tpr_threshold; 6561 6562 if (is_guest_mode(vcpu) && 6563 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) 6564 return; 6565 6566 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr; 6567 if (is_guest_mode(vcpu)) 6568 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold; 6569 else 6570 vmcs_write32(TPR_THRESHOLD, tpr_threshold); 6571 } 6572 6573 void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) 6574 { 6575 struct vcpu_vmx *vmx = to_vmx(vcpu); 6576 u32 sec_exec_control; 6577 6578 if (!lapic_in_kernel(vcpu)) 6579 return; 6580 6581 if (!flexpriority_enabled && 6582 !cpu_has_vmx_virtualize_x2apic_mode()) 6583 return; 6584 6585 /* Postpone execution until vmcs01 is the current VMCS. */ 6586 if (is_guest_mode(vcpu)) { 6587 vmx->nested.change_vmcs01_virtual_apic_mode = true; 6588 return; 6589 } 6590 6591 sec_exec_control = secondary_exec_controls_get(vmx); 6592 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | 6593 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); 6594 6595 switch (kvm_get_apic_mode(vcpu)) { 6596 case LAPIC_MODE_INVALID: 6597 WARN_ONCE(true, "Invalid local APIC state"); 6598 break; 6599 case LAPIC_MODE_DISABLED: 6600 break; 6601 case LAPIC_MODE_XAPIC: 6602 if (flexpriority_enabled) { 6603 sec_exec_control |= 6604 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; 6605 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); 6606 6607 /* 6608 * Flush the TLB, reloading the APIC access page will 6609 * only do so if its physical address has changed, but 6610 * the guest may have inserted a non-APIC mapping into 6611 * the TLB while the APIC access page was disabled. 6612 */ 6613 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); 6614 } 6615 break; 6616 case LAPIC_MODE_X2APIC: 6617 if (cpu_has_vmx_virtualize_x2apic_mode()) 6618 sec_exec_control |= 6619 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; 6620 break; 6621 } 6622 secondary_exec_controls_set(vmx, sec_exec_control); 6623 6624 vmx_update_msr_bitmap_x2apic(vcpu); 6625 } 6626 6627 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) 6628 { 6629 struct page *page; 6630 6631 /* Defer reload until vmcs01 is the current VMCS. */ 6632 if (is_guest_mode(vcpu)) { 6633 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true; 6634 return; 6635 } 6636 6637 if (!(secondary_exec_controls_get(to_vmx(vcpu)) & 6638 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) 6639 return; 6640 6641 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); 6642 if (is_error_page(page)) 6643 return; 6644 6645 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page)); 6646 vmx_flush_tlb_current(vcpu); 6647 6648 /* 6649 * Do not pin apic access page in memory, the MMU notifier 6650 * will call us again if it is migrated or swapped out. 6651 */ 6652 put_page(page); 6653 } 6654 6655 static void vmx_hwapic_isr_update(int max_isr) 6656 { 6657 u16 status; 6658 u8 old; 6659 6660 if (max_isr == -1) 6661 max_isr = 0; 6662 6663 status = vmcs_read16(GUEST_INTR_STATUS); 6664 old = status >> 8; 6665 if (max_isr != old) { 6666 status &= 0xff; 6667 status |= max_isr << 8; 6668 vmcs_write16(GUEST_INTR_STATUS, status); 6669 } 6670 } 6671 6672 static void vmx_set_rvi(int vector) 6673 { 6674 u16 status; 6675 u8 old; 6676 6677 if (vector == -1) 6678 vector = 0; 6679 6680 status = vmcs_read16(GUEST_INTR_STATUS); 6681 old = (u8)status & 0xff; 6682 if ((u8)vector != old) { 6683 status &= ~0xff; 6684 status |= (u8)vector; 6685 vmcs_write16(GUEST_INTR_STATUS, status); 6686 } 6687 } 6688 6689 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) 6690 { 6691 /* 6692 * When running L2, updating RVI is only relevant when 6693 * vmcs12 virtual-interrupt-delivery enabled. 6694 * However, it can be enabled only when L1 also 6695 * intercepts external-interrupts and in that case 6696 * we should not update vmcs02 RVI but instead intercept 6697 * interrupt. Therefore, do nothing when running L2. 6698 */ 6699 if (!is_guest_mode(vcpu)) 6700 vmx_set_rvi(max_irr); 6701 } 6702 6703 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) 6704 { 6705 struct vcpu_vmx *vmx = to_vmx(vcpu); 6706 int max_irr; 6707 bool got_posted_interrupt; 6708 6709 if (KVM_BUG_ON(!enable_apicv, vcpu->kvm)) 6710 return -EIO; 6711 6712 if (pi_test_on(&vmx->pi_desc)) { 6713 pi_clear_on(&vmx->pi_desc); 6714 /* 6715 * IOMMU can write to PID.ON, so the barrier matters even on UP. 6716 * But on x86 this is just a compiler barrier anyway. 6717 */ 6718 smp_mb__after_atomic(); 6719 got_posted_interrupt = 6720 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); 6721 } else { 6722 max_irr = kvm_lapic_find_highest_irr(vcpu); 6723 got_posted_interrupt = false; 6724 } 6725 6726 /* 6727 * Newly recognized interrupts are injected via either virtual interrupt 6728 * delivery (RVI) or KVM_REQ_EVENT. Virtual interrupt delivery is 6729 * disabled in two cases: 6730 * 6731 * 1) If L2 is running and the vCPU has a new pending interrupt. If L1 6732 * wants to exit on interrupts, KVM_REQ_EVENT is needed to synthesize a 6733 * VM-Exit to L1. If L1 doesn't want to exit, the interrupt is injected 6734 * into L2, but KVM doesn't use virtual interrupt delivery to inject 6735 * interrupts into L2, and so KVM_REQ_EVENT is again needed. 6736 * 6737 * 2) If APICv is disabled for this vCPU, assigned devices may still 6738 * attempt to post interrupts. The posted interrupt vector will cause 6739 * a VM-Exit and the subsequent entry will call sync_pir_to_irr. 6740 */ 6741 if (!is_guest_mode(vcpu) && kvm_vcpu_apicv_active(vcpu)) 6742 vmx_set_rvi(max_irr); 6743 else if (got_posted_interrupt) 6744 kvm_make_request(KVM_REQ_EVENT, vcpu); 6745 6746 return max_irr; 6747 } 6748 6749 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) 6750 { 6751 if (!kvm_vcpu_apicv_active(vcpu)) 6752 return; 6753 6754 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); 6755 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); 6756 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); 6757 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); 6758 } 6759 6760 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) 6761 { 6762 struct vcpu_vmx *vmx = to_vmx(vcpu); 6763 6764 pi_clear_on(&vmx->pi_desc); 6765 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); 6766 } 6767 6768 void vmx_do_interrupt_nmi_irqoff(unsigned long entry); 6769 6770 static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, 6771 unsigned long entry) 6772 { 6773 bool is_nmi = entry == (unsigned long)asm_exc_nmi_noist; 6774 6775 kvm_before_interrupt(vcpu, is_nmi ? KVM_HANDLING_NMI : KVM_HANDLING_IRQ); 6776 vmx_do_interrupt_nmi_irqoff(entry); 6777 kvm_after_interrupt(vcpu); 6778 } 6779 6780 static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu) 6781 { 6782 /* 6783 * Save xfd_err to guest_fpu before interrupt is enabled, so the 6784 * MSR value is not clobbered by the host activity before the guest 6785 * has chance to consume it. 6786 * 6787 * Do not blindly read xfd_err here, since this exception might 6788 * be caused by L1 interception on a platform which doesn't 6789 * support xfd at all. 6790 * 6791 * Do it conditionally upon guest_fpu::xfd. xfd_err matters 6792 * only when xfd contains a non-zero value. 6793 * 6794 * Queuing exception is done in vmx_handle_exit. See comment there. 6795 */ 6796 if (vcpu->arch.guest_fpu.fpstate->xfd) 6797 rdmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); 6798 } 6799 6800 static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx) 6801 { 6802 const unsigned long nmi_entry = (unsigned long)asm_exc_nmi_noist; 6803 u32 intr_info = vmx_get_intr_info(&vmx->vcpu); 6804 6805 /* if exit due to PF check for async PF */ 6806 if (is_page_fault(intr_info)) 6807 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags(); 6808 /* if exit due to NM, handle before interrupts are enabled */ 6809 else if (is_nm_fault(intr_info)) 6810 handle_nm_fault_irqoff(&vmx->vcpu); 6811 /* Handle machine checks before interrupts are enabled */ 6812 else if (is_machine_check(intr_info)) 6813 kvm_machine_check(); 6814 /* We need to handle NMIs before interrupts are enabled */ 6815 else if (is_nmi(intr_info)) 6816 handle_interrupt_nmi_irqoff(&vmx->vcpu, nmi_entry); 6817 } 6818 6819 static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu) 6820 { 6821 u32 intr_info = vmx_get_intr_info(vcpu); 6822 unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK; 6823 gate_desc *desc = (gate_desc *)host_idt_base + vector; 6824 6825 if (KVM_BUG(!is_external_intr(intr_info), vcpu->kvm, 6826 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info)) 6827 return; 6828 6829 handle_interrupt_nmi_irqoff(vcpu, gate_offset(desc)); 6830 vcpu->arch.at_instruction_boundary = true; 6831 } 6832 6833 static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu) 6834 { 6835 struct vcpu_vmx *vmx = to_vmx(vcpu); 6836 6837 if (vmx->emulation_required) 6838 return; 6839 6840 if (vmx->exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT) 6841 handle_external_interrupt_irqoff(vcpu); 6842 else if (vmx->exit_reason.basic == EXIT_REASON_EXCEPTION_NMI) 6843 handle_exception_nmi_irqoff(vmx); 6844 } 6845 6846 /* 6847 * The kvm parameter can be NULL (module initialization, or invocation before 6848 * VM creation). Be sure to check the kvm parameter before using it. 6849 */ 6850 static bool vmx_has_emulated_msr(struct kvm *kvm, u32 index) 6851 { 6852 switch (index) { 6853 case MSR_IA32_SMBASE: 6854 if (!IS_ENABLED(CONFIG_KVM_SMM)) 6855 return false; 6856 /* 6857 * We cannot do SMM unless we can run the guest in big 6858 * real mode. 6859 */ 6860 return enable_unrestricted_guest || emulate_invalid_guest_state; 6861 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: 6862 return nested; 6863 case MSR_AMD64_VIRT_SPEC_CTRL: 6864 case MSR_AMD64_TSC_RATIO: 6865 /* This is AMD only. */ 6866 return false; 6867 default: 6868 return true; 6869 } 6870 } 6871 6872 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) 6873 { 6874 u32 exit_intr_info; 6875 bool unblock_nmi; 6876 u8 vector; 6877 bool idtv_info_valid; 6878 6879 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; 6880 6881 if (enable_vnmi) { 6882 if (vmx->loaded_vmcs->nmi_known_unmasked) 6883 return; 6884 6885 exit_intr_info = vmx_get_intr_info(&vmx->vcpu); 6886 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; 6887 vector = exit_intr_info & INTR_INFO_VECTOR_MASK; 6888 /* 6889 * SDM 3: 27.7.1.2 (September 2008) 6890 * Re-set bit "block by NMI" before VM entry if vmexit caused by 6891 * a guest IRET fault. 6892 * SDM 3: 23.2.2 (September 2008) 6893 * Bit 12 is undefined in any of the following cases: 6894 * If the VM exit sets the valid bit in the IDT-vectoring 6895 * information field. 6896 * If the VM exit is due to a double fault. 6897 */ 6898 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && 6899 vector != DF_VECTOR && !idtv_info_valid) 6900 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, 6901 GUEST_INTR_STATE_NMI); 6902 else 6903 vmx->loaded_vmcs->nmi_known_unmasked = 6904 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) 6905 & GUEST_INTR_STATE_NMI); 6906 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) 6907 vmx->loaded_vmcs->vnmi_blocked_time += 6908 ktime_to_ns(ktime_sub(ktime_get(), 6909 vmx->loaded_vmcs->entry_time)); 6910 } 6911 6912 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, 6913 u32 idt_vectoring_info, 6914 int instr_len_field, 6915 int error_code_field) 6916 { 6917 u8 vector; 6918 int type; 6919 bool idtv_info_valid; 6920 6921 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; 6922 6923 vcpu->arch.nmi_injected = false; 6924 kvm_clear_exception_queue(vcpu); 6925 kvm_clear_interrupt_queue(vcpu); 6926 6927 if (!idtv_info_valid) 6928 return; 6929 6930 kvm_make_request(KVM_REQ_EVENT, vcpu); 6931 6932 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; 6933 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; 6934 6935 switch (type) { 6936 case INTR_TYPE_NMI_INTR: 6937 vcpu->arch.nmi_injected = true; 6938 /* 6939 * SDM 3: 27.7.1.2 (September 2008) 6940 * Clear bit "block by NMI" before VM entry if a NMI 6941 * delivery faulted. 6942 */ 6943 vmx_set_nmi_mask(vcpu, false); 6944 break; 6945 case INTR_TYPE_SOFT_EXCEPTION: 6946 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); 6947 fallthrough; 6948 case INTR_TYPE_HARD_EXCEPTION: 6949 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { 6950 u32 err = vmcs_read32(error_code_field); 6951 kvm_requeue_exception_e(vcpu, vector, err); 6952 } else 6953 kvm_requeue_exception(vcpu, vector); 6954 break; 6955 case INTR_TYPE_SOFT_INTR: 6956 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); 6957 fallthrough; 6958 case INTR_TYPE_EXT_INTR: 6959 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); 6960 break; 6961 default: 6962 break; 6963 } 6964 } 6965 6966 static void vmx_complete_interrupts(struct vcpu_vmx *vmx) 6967 { 6968 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, 6969 VM_EXIT_INSTRUCTION_LEN, 6970 IDT_VECTORING_ERROR_CODE); 6971 } 6972 6973 static void vmx_cancel_injection(struct kvm_vcpu *vcpu) 6974 { 6975 __vmx_complete_interrupts(vcpu, 6976 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), 6977 VM_ENTRY_INSTRUCTION_LEN, 6978 VM_ENTRY_EXCEPTION_ERROR_CODE); 6979 6980 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); 6981 } 6982 6983 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) 6984 { 6985 int i, nr_msrs; 6986 struct perf_guest_switch_msr *msrs; 6987 struct kvm_pmu *pmu = vcpu_to_pmu(&vmx->vcpu); 6988 6989 pmu->host_cross_mapped_mask = 0; 6990 if (pmu->pebs_enable & pmu->global_ctrl) 6991 intel_pmu_cross_mapped_check(pmu); 6992 6993 /* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ 6994 msrs = perf_guest_get_msrs(&nr_msrs, (void *)pmu); 6995 if (!msrs) 6996 return; 6997 6998 for (i = 0; i < nr_msrs; i++) 6999 if (msrs[i].host == msrs[i].guest) 7000 clear_atomic_switch_msr(vmx, msrs[i].msr); 7001 else 7002 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, 7003 msrs[i].host, false); 7004 } 7005 7006 static void vmx_update_hv_timer(struct kvm_vcpu *vcpu) 7007 { 7008 struct vcpu_vmx *vmx = to_vmx(vcpu); 7009 u64 tscl; 7010 u32 delta_tsc; 7011 7012 if (vmx->req_immediate_exit) { 7013 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0); 7014 vmx->loaded_vmcs->hv_timer_soft_disabled = false; 7015 } else if (vmx->hv_deadline_tsc != -1) { 7016 tscl = rdtsc(); 7017 if (vmx->hv_deadline_tsc > tscl) 7018 /* set_hv_timer ensures the delta fits in 32-bits */ 7019 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> 7020 cpu_preemption_timer_multi); 7021 else 7022 delta_tsc = 0; 7023 7024 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); 7025 vmx->loaded_vmcs->hv_timer_soft_disabled = false; 7026 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) { 7027 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1); 7028 vmx->loaded_vmcs->hv_timer_soft_disabled = true; 7029 } 7030 } 7031 7032 void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp) 7033 { 7034 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) { 7035 vmx->loaded_vmcs->host_state.rsp = host_rsp; 7036 vmcs_writel(HOST_RSP, host_rsp); 7037 } 7038 } 7039 7040 void noinstr vmx_spec_ctrl_restore_host(struct vcpu_vmx *vmx, 7041 unsigned int flags) 7042 { 7043 u64 hostval = this_cpu_read(x86_spec_ctrl_current); 7044 7045 if (!cpu_feature_enabled(X86_FEATURE_MSR_SPEC_CTRL)) 7046 return; 7047 7048 if (flags & VMX_RUN_SAVE_SPEC_CTRL) 7049 vmx->spec_ctrl = __rdmsr(MSR_IA32_SPEC_CTRL); 7050 7051 /* 7052 * If the guest/host SPEC_CTRL values differ, restore the host value. 7053 * 7054 * For legacy IBRS, the IBRS bit always needs to be written after 7055 * transitioning from a less privileged predictor mode, regardless of 7056 * whether the guest/host values differ. 7057 */ 7058 if (cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS) || 7059 vmx->spec_ctrl != hostval) 7060 native_wrmsrl(MSR_IA32_SPEC_CTRL, hostval); 7061 7062 barrier_nospec(); 7063 } 7064 7065 static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu) 7066 { 7067 switch (to_vmx(vcpu)->exit_reason.basic) { 7068 case EXIT_REASON_MSR_WRITE: 7069 return handle_fastpath_set_msr_irqoff(vcpu); 7070 case EXIT_REASON_PREEMPTION_TIMER: 7071 return handle_fastpath_preemption_timer(vcpu); 7072 default: 7073 return EXIT_FASTPATH_NONE; 7074 } 7075 } 7076 7077 static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu, 7078 struct vcpu_vmx *vmx, 7079 unsigned long flags) 7080 { 7081 guest_state_enter_irqoff(); 7082 7083 /* L1D Flush includes CPU buffer clear to mitigate MDS */ 7084 if (static_branch_unlikely(&vmx_l1d_should_flush)) 7085 vmx_l1d_flush(vcpu); 7086 else if (static_branch_unlikely(&mds_user_clear)) 7087 mds_clear_cpu_buffers(); 7088 else if (static_branch_unlikely(&mmio_stale_data_clear) && 7089 kvm_arch_has_assigned_device(vcpu->kvm)) 7090 mds_clear_cpu_buffers(); 7091 7092 vmx_disable_fb_clear(vmx); 7093 7094 if (vcpu->arch.cr2 != native_read_cr2()) 7095 native_write_cr2(vcpu->arch.cr2); 7096 7097 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, 7098 flags); 7099 7100 vcpu->arch.cr2 = native_read_cr2(); 7101 7102 vmx_enable_fb_clear(vmx); 7103 7104 guest_state_exit_irqoff(); 7105 } 7106 7107 static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu) 7108 { 7109 struct vcpu_vmx *vmx = to_vmx(vcpu); 7110 unsigned long cr3, cr4; 7111 7112 /* Record the guest's net vcpu time for enforced NMI injections. */ 7113 if (unlikely(!enable_vnmi && 7114 vmx->loaded_vmcs->soft_vnmi_blocked)) 7115 vmx->loaded_vmcs->entry_time = ktime_get(); 7116 7117 /* 7118 * Don't enter VMX if guest state is invalid, let the exit handler 7119 * start emulation until we arrive back to a valid state. Synthesize a 7120 * consistency check VM-Exit due to invalid guest state and bail. 7121 */ 7122 if (unlikely(vmx->emulation_required)) { 7123 vmx->fail = 0; 7124 7125 vmx->exit_reason.full = EXIT_REASON_INVALID_STATE; 7126 vmx->exit_reason.failed_vmentry = 1; 7127 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_1); 7128 vmx->exit_qualification = ENTRY_FAIL_DEFAULT; 7129 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_2); 7130 vmx->exit_intr_info = 0; 7131 return EXIT_FASTPATH_NONE; 7132 } 7133 7134 trace_kvm_entry(vcpu); 7135 7136 if (vmx->ple_window_dirty) { 7137 vmx->ple_window_dirty = false; 7138 vmcs_write32(PLE_WINDOW, vmx->ple_window); 7139 } 7140 7141 /* 7142 * We did this in prepare_switch_to_guest, because it needs to 7143 * be within srcu_read_lock. 7144 */ 7145 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync); 7146 7147 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP)) 7148 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); 7149 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP)) 7150 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); 7151 vcpu->arch.regs_dirty = 0; 7152 7153 /* 7154 * Refresh vmcs.HOST_CR3 if necessary. This must be done immediately 7155 * prior to VM-Enter, as the kernel may load a new ASID (PCID) any time 7156 * it switches back to the current->mm, which can occur in KVM context 7157 * when switching to a temporary mm to patch kernel code, e.g. if KVM 7158 * toggles a static key while handling a VM-Exit. 7159 */ 7160 cr3 = __get_current_cr3_fast(); 7161 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { 7162 vmcs_writel(HOST_CR3, cr3); 7163 vmx->loaded_vmcs->host_state.cr3 = cr3; 7164 } 7165 7166 cr4 = cr4_read_shadow(); 7167 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { 7168 vmcs_writel(HOST_CR4, cr4); 7169 vmx->loaded_vmcs->host_state.cr4 = cr4; 7170 } 7171 7172 /* When KVM_DEBUGREG_WONT_EXIT, dr6 is accessible in guest. */ 7173 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) 7174 set_debugreg(vcpu->arch.dr6, 6); 7175 7176 /* When single-stepping over STI and MOV SS, we must clear the 7177 * corresponding interruptibility bits in the guest state. Otherwise 7178 * vmentry fails as it then expects bit 14 (BS) in pending debug 7179 * exceptions being set, but that's not correct for the guest debugging 7180 * case. */ 7181 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) 7182 vmx_set_interrupt_shadow(vcpu, 0); 7183 7184 kvm_load_guest_xsave_state(vcpu); 7185 7186 pt_guest_enter(vmx); 7187 7188 atomic_switch_perf_msrs(vmx); 7189 if (intel_pmu_lbr_is_enabled(vcpu)) 7190 vmx_passthrough_lbr_msrs(vcpu); 7191 7192 if (enable_preemption_timer) 7193 vmx_update_hv_timer(vcpu); 7194 7195 kvm_wait_lapic_expire(vcpu); 7196 7197 /* The actual VMENTER/EXIT is in the .noinstr.text section. */ 7198 vmx_vcpu_enter_exit(vcpu, vmx, __vmx_vcpu_run_flags(vmx)); 7199 7200 /* All fields are clean at this point */ 7201 if (static_branch_unlikely(&enable_evmcs)) { 7202 current_evmcs->hv_clean_fields |= 7203 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; 7204 7205 current_evmcs->hv_vp_id = kvm_hv_get_vpindex(vcpu); 7206 } 7207 7208 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ 7209 if (vmx->host_debugctlmsr) 7210 update_debugctlmsr(vmx->host_debugctlmsr); 7211 7212 #ifndef CONFIG_X86_64 7213 /* 7214 * The sysexit path does not restore ds/es, so we must set them to 7215 * a reasonable value ourselves. 7216 * 7217 * We can't defer this to vmx_prepare_switch_to_host() since that 7218 * function may be executed in interrupt context, which saves and 7219 * restore segments around it, nullifying its effect. 7220 */ 7221 loadsegment(ds, __USER_DS); 7222 loadsegment(es, __USER_DS); 7223 #endif 7224 7225 vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET; 7226 7227 pt_guest_exit(vmx); 7228 7229 kvm_load_host_xsave_state(vcpu); 7230 7231 if (is_guest_mode(vcpu)) { 7232 /* 7233 * Track VMLAUNCH/VMRESUME that have made past guest state 7234 * checking. 7235 */ 7236 if (vmx->nested.nested_run_pending && 7237 !vmx->exit_reason.failed_vmentry) 7238 ++vcpu->stat.nested_run; 7239 7240 vmx->nested.nested_run_pending = 0; 7241 } 7242 7243 vmx->idt_vectoring_info = 0; 7244 7245 if (unlikely(vmx->fail)) { 7246 vmx->exit_reason.full = 0xdead; 7247 return EXIT_FASTPATH_NONE; 7248 } 7249 7250 vmx->exit_reason.full = vmcs_read32(VM_EXIT_REASON); 7251 if (unlikely((u16)vmx->exit_reason.basic == EXIT_REASON_MCE_DURING_VMENTRY)) 7252 kvm_machine_check(); 7253 7254 if (likely(!vmx->exit_reason.failed_vmentry)) 7255 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); 7256 7257 trace_kvm_exit(vcpu, KVM_ISA_VMX); 7258 7259 if (unlikely(vmx->exit_reason.failed_vmentry)) 7260 return EXIT_FASTPATH_NONE; 7261 7262 vmx->loaded_vmcs->launched = 1; 7263 7264 vmx_recover_nmi_blocking(vmx); 7265 vmx_complete_interrupts(vmx); 7266 7267 if (is_guest_mode(vcpu)) 7268 return EXIT_FASTPATH_NONE; 7269 7270 return vmx_exit_handlers_fastpath(vcpu); 7271 } 7272 7273 static void vmx_vcpu_free(struct kvm_vcpu *vcpu) 7274 { 7275 struct vcpu_vmx *vmx = to_vmx(vcpu); 7276 7277 if (enable_pml) 7278 vmx_destroy_pml_buffer(vmx); 7279 free_vpid(vmx->vpid); 7280 nested_vmx_free_vcpu(vcpu); 7281 free_loaded_vmcs(vmx->loaded_vmcs); 7282 } 7283 7284 static int vmx_vcpu_create(struct kvm_vcpu *vcpu) 7285 { 7286 struct vmx_uret_msr *tsx_ctrl; 7287 struct vcpu_vmx *vmx; 7288 int i, err; 7289 7290 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0); 7291 vmx = to_vmx(vcpu); 7292 7293 INIT_LIST_HEAD(&vmx->pi_wakeup_list); 7294 7295 err = -ENOMEM; 7296 7297 vmx->vpid = allocate_vpid(); 7298 7299 /* 7300 * If PML is turned on, failure on enabling PML just results in failure 7301 * of creating the vcpu, therefore we can simplify PML logic (by 7302 * avoiding dealing with cases, such as enabling PML partially on vcpus 7303 * for the guest), etc. 7304 */ 7305 if (enable_pml) { 7306 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); 7307 if (!vmx->pml_pg) 7308 goto free_vpid; 7309 } 7310 7311 for (i = 0; i < kvm_nr_uret_msrs; ++i) 7312 vmx->guest_uret_msrs[i].mask = -1ull; 7313 if (boot_cpu_has(X86_FEATURE_RTM)) { 7314 /* 7315 * TSX_CTRL_CPUID_CLEAR is handled in the CPUID interception. 7316 * Keep the host value unchanged to avoid changing CPUID bits 7317 * under the host kernel's feet. 7318 */ 7319 tsx_ctrl = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL); 7320 if (tsx_ctrl) 7321 tsx_ctrl->mask = ~(u64)TSX_CTRL_CPUID_CLEAR; 7322 } 7323 7324 err = alloc_loaded_vmcs(&vmx->vmcs01); 7325 if (err < 0) 7326 goto free_pml; 7327 7328 /* 7329 * Use Hyper-V 'Enlightened MSR Bitmap' feature when KVM runs as a 7330 * nested (L1) hypervisor and Hyper-V in L0 supports it. Enable the 7331 * feature only for vmcs01, KVM currently isn't equipped to realize any 7332 * performance benefits from enabling it for vmcs02. 7333 */ 7334 if (IS_ENABLED(CONFIG_HYPERV) && static_branch_unlikely(&enable_evmcs) && 7335 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { 7336 struct hv_enlightened_vmcs *evmcs = (void *)vmx->vmcs01.vmcs; 7337 7338 evmcs->hv_enlightenments_control.msr_bitmap = 1; 7339 } 7340 7341 /* The MSR bitmap starts with all ones */ 7342 bitmap_fill(vmx->shadow_msr_intercept.read, MAX_POSSIBLE_PASSTHROUGH_MSRS); 7343 bitmap_fill(vmx->shadow_msr_intercept.write, MAX_POSSIBLE_PASSTHROUGH_MSRS); 7344 7345 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_TSC, MSR_TYPE_R); 7346 #ifdef CONFIG_X86_64 7347 vmx_disable_intercept_for_msr(vcpu, MSR_FS_BASE, MSR_TYPE_RW); 7348 vmx_disable_intercept_for_msr(vcpu, MSR_GS_BASE, MSR_TYPE_RW); 7349 vmx_disable_intercept_for_msr(vcpu, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); 7350 #endif 7351 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); 7352 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); 7353 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); 7354 if (kvm_cstate_in_guest(vcpu->kvm)) { 7355 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C1_RES, MSR_TYPE_R); 7356 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R); 7357 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R); 7358 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R); 7359 } 7360 7361 vmx->loaded_vmcs = &vmx->vmcs01; 7362 7363 if (cpu_need_virtualize_apic_accesses(vcpu)) { 7364 err = kvm_alloc_apic_access_page(vcpu->kvm); 7365 if (err) 7366 goto free_vmcs; 7367 } 7368 7369 if (enable_ept && !enable_unrestricted_guest) { 7370 err = init_rmode_identity_map(vcpu->kvm); 7371 if (err) 7372 goto free_vmcs; 7373 } 7374 7375 if (vmx_can_use_ipiv(vcpu)) 7376 WRITE_ONCE(to_kvm_vmx(vcpu->kvm)->pid_table[vcpu->vcpu_id], 7377 __pa(&vmx->pi_desc) | PID_TABLE_ENTRY_VALID); 7378 7379 return 0; 7380 7381 free_vmcs: 7382 free_loaded_vmcs(vmx->loaded_vmcs); 7383 free_pml: 7384 vmx_destroy_pml_buffer(vmx); 7385 free_vpid: 7386 free_vpid(vmx->vpid); 7387 return err; 7388 } 7389 7390 #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" 7391 #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" 7392 7393 static int vmx_vm_init(struct kvm *kvm) 7394 { 7395 if (!ple_gap) 7396 kvm->arch.pause_in_guest = true; 7397 7398 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) { 7399 switch (l1tf_mitigation) { 7400 case L1TF_MITIGATION_OFF: 7401 case L1TF_MITIGATION_FLUSH_NOWARN: 7402 /* 'I explicitly don't care' is set */ 7403 break; 7404 case L1TF_MITIGATION_FLUSH: 7405 case L1TF_MITIGATION_FLUSH_NOSMT: 7406 case L1TF_MITIGATION_FULL: 7407 /* 7408 * Warn upon starting the first VM in a potentially 7409 * insecure environment. 7410 */ 7411 if (sched_smt_active()) 7412 pr_warn_once(L1TF_MSG_SMT); 7413 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER) 7414 pr_warn_once(L1TF_MSG_L1D); 7415 break; 7416 case L1TF_MITIGATION_FULL_FORCE: 7417 /* Flush is enforced */ 7418 break; 7419 } 7420 } 7421 return 0; 7422 } 7423 7424 static int __init vmx_check_processor_compat(void) 7425 { 7426 struct vmcs_config vmcs_conf; 7427 struct vmx_capability vmx_cap; 7428 7429 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) || 7430 !this_cpu_has(X86_FEATURE_VMX)) { 7431 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id()); 7432 return -EIO; 7433 } 7434 7435 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) 7436 return -EIO; 7437 if (nested) 7438 nested_vmx_setup_ctls_msrs(&vmcs_conf, vmx_cap.ept); 7439 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { 7440 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", 7441 smp_processor_id()); 7442 return -EIO; 7443 } 7444 return 0; 7445 } 7446 7447 static u8 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) 7448 { 7449 u8 cache; 7450 7451 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in 7452 * memory aliases with conflicting memory types and sometimes MCEs. 7453 * We have to be careful as to what are honored and when. 7454 * 7455 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to 7456 * UC. The effective memory type is UC or WC depending on guest PAT. 7457 * This was historically the source of MCEs and we want to be 7458 * conservative. 7459 * 7460 * When there is no need to deal with noncoherent DMA (e.g., no VT-d 7461 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The 7462 * EPT memory type is set to WB. The effective memory type is forced 7463 * WB. 7464 * 7465 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The 7466 * EPT memory type is used to emulate guest CD/MTRR. 7467 */ 7468 7469 if (is_mmio) 7470 return MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; 7471 7472 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) 7473 return (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) | VMX_EPT_IPAT_BIT; 7474 7475 if (kvm_read_cr0(vcpu) & X86_CR0_CD) { 7476 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) 7477 cache = MTRR_TYPE_WRBACK; 7478 else 7479 cache = MTRR_TYPE_UNCACHABLE; 7480 7481 return (cache << VMX_EPT_MT_EPTE_SHIFT) | VMX_EPT_IPAT_BIT; 7482 } 7483 7484 return kvm_mtrr_get_guest_memory_type(vcpu, gfn) << VMX_EPT_MT_EPTE_SHIFT; 7485 } 7486 7487 static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx, u32 new_ctl) 7488 { 7489 /* 7490 * These bits in the secondary execution controls field 7491 * are dynamic, the others are mostly based on the hypervisor 7492 * architecture and the guest's CPUID. Do not touch the 7493 * dynamic bits. 7494 */ 7495 u32 mask = 7496 SECONDARY_EXEC_SHADOW_VMCS | 7497 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | 7498 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | 7499 SECONDARY_EXEC_DESC; 7500 7501 u32 cur_ctl = secondary_exec_controls_get(vmx); 7502 7503 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask)); 7504 } 7505 7506 /* 7507 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits 7508 * (indicating "allowed-1") if they are supported in the guest's CPUID. 7509 */ 7510 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) 7511 { 7512 struct vcpu_vmx *vmx = to_vmx(vcpu); 7513 struct kvm_cpuid_entry2 *entry; 7514 7515 vmx->nested.msrs.cr0_fixed1 = 0xffffffff; 7516 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; 7517 7518 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ 7519 if (entry && (entry->_reg & (_cpuid_mask))) \ 7520 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ 7521 } while (0) 7522 7523 entry = kvm_find_cpuid_entry(vcpu, 0x1); 7524 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME)); 7525 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME)); 7526 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC)); 7527 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE)); 7528 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE)); 7529 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE)); 7530 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE)); 7531 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE)); 7532 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR)); 7533 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM)); 7534 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX)); 7535 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX)); 7536 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID)); 7537 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE)); 7538 7539 entry = kvm_find_cpuid_entry_index(vcpu, 0x7, 0); 7540 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE)); 7541 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP)); 7542 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP)); 7543 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU)); 7544 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP)); 7545 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57)); 7546 7547 #undef cr4_fixed1_update 7548 } 7549 7550 static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) 7551 { 7552 struct vcpu_vmx *vmx = to_vmx(vcpu); 7553 struct kvm_cpuid_entry2 *best = NULL; 7554 int i; 7555 7556 for (i = 0; i < PT_CPUID_LEAVES; i++) { 7557 best = kvm_find_cpuid_entry_index(vcpu, 0x14, i); 7558 if (!best) 7559 return; 7560 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax; 7561 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx; 7562 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx; 7563 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx; 7564 } 7565 7566 /* Get the number of configurable Address Ranges for filtering */ 7567 vmx->pt_desc.num_address_ranges = intel_pt_validate_cap(vmx->pt_desc.caps, 7568 PT_CAP_num_address_ranges); 7569 7570 /* Initialize and clear the no dependency bits */ 7571 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS | 7572 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC | 7573 RTIT_CTL_BRANCH_EN); 7574 7575 /* 7576 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise 7577 * will inject an #GP 7578 */ 7579 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering)) 7580 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN; 7581 7582 /* 7583 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and 7584 * PSBFreq can be set 7585 */ 7586 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc)) 7587 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC | 7588 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ); 7589 7590 /* 7591 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn and MTCFreq can be set 7592 */ 7593 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc)) 7594 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN | 7595 RTIT_CTL_MTC_RANGE); 7596 7597 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */ 7598 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite)) 7599 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW | 7600 RTIT_CTL_PTW_EN); 7601 7602 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */ 7603 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace)) 7604 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN; 7605 7606 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */ 7607 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output)) 7608 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA; 7609 7610 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabricEn can be set */ 7611 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys)) 7612 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN; 7613 7614 /* unmask address range configure area */ 7615 for (i = 0; i < vmx->pt_desc.num_address_ranges; i++) 7616 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4)); 7617 } 7618 7619 static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) 7620 { 7621 struct vcpu_vmx *vmx = to_vmx(vcpu); 7622 7623 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */ 7624 vcpu->arch.xsaves_enabled = false; 7625 7626 vmx_setup_uret_msrs(vmx); 7627 7628 if (cpu_has_secondary_exec_ctrls()) 7629 vmcs_set_secondary_exec_control(vmx, 7630 vmx_secondary_exec_control(vmx)); 7631 7632 if (nested_vmx_allowed(vcpu)) 7633 vmx->msr_ia32_feature_control_valid_bits |= 7634 FEAT_CTL_VMX_ENABLED_INSIDE_SMX | 7635 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; 7636 else 7637 vmx->msr_ia32_feature_control_valid_bits &= 7638 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX | 7639 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX); 7640 7641 if (nested_vmx_allowed(vcpu)) 7642 nested_vmx_cr_fixed1_bits_update(vcpu); 7643 7644 if (boot_cpu_has(X86_FEATURE_INTEL_PT) && 7645 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT)) 7646 update_intel_pt_cfg(vcpu); 7647 7648 if (boot_cpu_has(X86_FEATURE_RTM)) { 7649 struct vmx_uret_msr *msr; 7650 msr = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL); 7651 if (msr) { 7652 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM); 7653 vmx_set_guest_uret_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE); 7654 } 7655 } 7656 7657 if (kvm_cpu_cap_has(X86_FEATURE_XFD)) 7658 vmx_set_intercept_for_msr(vcpu, MSR_IA32_XFD_ERR, MSR_TYPE_R, 7659 !guest_cpuid_has(vcpu, X86_FEATURE_XFD)); 7660 7661 7662 set_cr4_guest_host_mask(vmx); 7663 7664 vmx_write_encls_bitmap(vcpu, NULL); 7665 if (guest_cpuid_has(vcpu, X86_FEATURE_SGX)) 7666 vmx->msr_ia32_feature_control_valid_bits |= FEAT_CTL_SGX_ENABLED; 7667 else 7668 vmx->msr_ia32_feature_control_valid_bits &= ~FEAT_CTL_SGX_ENABLED; 7669 7670 if (guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC)) 7671 vmx->msr_ia32_feature_control_valid_bits |= 7672 FEAT_CTL_SGX_LC_ENABLED; 7673 else 7674 vmx->msr_ia32_feature_control_valid_bits &= 7675 ~FEAT_CTL_SGX_LC_ENABLED; 7676 7677 /* Refresh #PF interception to account for MAXPHYADDR changes. */ 7678 vmx_update_exception_bitmap(vcpu); 7679 } 7680 7681 static u64 vmx_get_perf_capabilities(void) 7682 { 7683 u64 perf_cap = PMU_CAP_FW_WRITES; 7684 struct x86_pmu_lbr lbr; 7685 u64 host_perf_cap = 0; 7686 7687 if (!enable_pmu) 7688 return 0; 7689 7690 if (boot_cpu_has(X86_FEATURE_PDCM)) 7691 rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap); 7692 7693 x86_perf_get_lbr(&lbr); 7694 if (lbr.nr) 7695 perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT; 7696 7697 if (vmx_pebs_supported()) { 7698 perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK; 7699 if ((perf_cap & PERF_CAP_PEBS_FORMAT) < 4) 7700 perf_cap &= ~PERF_CAP_PEBS_BASELINE; 7701 } 7702 7703 return perf_cap; 7704 } 7705 7706 static __init void vmx_set_cpu_caps(void) 7707 { 7708 kvm_set_cpu_caps(); 7709 7710 /* CPUID 0x1 */ 7711 if (nested) 7712 kvm_cpu_cap_set(X86_FEATURE_VMX); 7713 7714 /* CPUID 0x7 */ 7715 if (kvm_mpx_supported()) 7716 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX); 7717 if (!cpu_has_vmx_invpcid()) 7718 kvm_cpu_cap_clear(X86_FEATURE_INVPCID); 7719 if (vmx_pt_mode_is_host_guest()) 7720 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT); 7721 if (vmx_pebs_supported()) { 7722 kvm_cpu_cap_check_and_set(X86_FEATURE_DS); 7723 kvm_cpu_cap_check_and_set(X86_FEATURE_DTES64); 7724 } 7725 7726 if (!enable_pmu) 7727 kvm_cpu_cap_clear(X86_FEATURE_PDCM); 7728 kvm_caps.supported_perf_cap = vmx_get_perf_capabilities(); 7729 7730 if (!enable_sgx) { 7731 kvm_cpu_cap_clear(X86_FEATURE_SGX); 7732 kvm_cpu_cap_clear(X86_FEATURE_SGX_LC); 7733 kvm_cpu_cap_clear(X86_FEATURE_SGX1); 7734 kvm_cpu_cap_clear(X86_FEATURE_SGX2); 7735 } 7736 7737 if (vmx_umip_emulated()) 7738 kvm_cpu_cap_set(X86_FEATURE_UMIP); 7739 7740 /* CPUID 0xD.1 */ 7741 kvm_caps.supported_xss = 0; 7742 if (!cpu_has_vmx_xsaves()) 7743 kvm_cpu_cap_clear(X86_FEATURE_XSAVES); 7744 7745 /* CPUID 0x80000001 and 0x7 (RDPID) */ 7746 if (!cpu_has_vmx_rdtscp()) { 7747 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP); 7748 kvm_cpu_cap_clear(X86_FEATURE_RDPID); 7749 } 7750 7751 if (cpu_has_vmx_waitpkg()) 7752 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG); 7753 } 7754 7755 static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu) 7756 { 7757 to_vmx(vcpu)->req_immediate_exit = true; 7758 } 7759 7760 static int vmx_check_intercept_io(struct kvm_vcpu *vcpu, 7761 struct x86_instruction_info *info) 7762 { 7763 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 7764 unsigned short port; 7765 bool intercept; 7766 int size; 7767 7768 if (info->intercept == x86_intercept_in || 7769 info->intercept == x86_intercept_ins) { 7770 port = info->src_val; 7771 size = info->dst_bytes; 7772 } else { 7773 port = info->dst_val; 7774 size = info->src_bytes; 7775 } 7776 7777 /* 7778 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction 7779 * VM-exits depend on the 'unconditional IO exiting' VM-execution 7780 * control. 7781 * 7782 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps. 7783 */ 7784 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) 7785 intercept = nested_cpu_has(vmcs12, 7786 CPU_BASED_UNCOND_IO_EXITING); 7787 else 7788 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size); 7789 7790 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */ 7791 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; 7792 } 7793 7794 static int vmx_check_intercept(struct kvm_vcpu *vcpu, 7795 struct x86_instruction_info *info, 7796 enum x86_intercept_stage stage, 7797 struct x86_exception *exception) 7798 { 7799 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 7800 7801 switch (info->intercept) { 7802 /* 7803 * RDPID causes #UD if disabled through secondary execution controls. 7804 * Because it is marked as EmulateOnUD, we need to intercept it here. 7805 * Note, RDPID is hidden behind ENABLE_RDTSCP. 7806 */ 7807 case x86_intercept_rdpid: 7808 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_RDTSCP)) { 7809 exception->vector = UD_VECTOR; 7810 exception->error_code_valid = false; 7811 return X86EMUL_PROPAGATE_FAULT; 7812 } 7813 break; 7814 7815 case x86_intercept_in: 7816 case x86_intercept_ins: 7817 case x86_intercept_out: 7818 case x86_intercept_outs: 7819 return vmx_check_intercept_io(vcpu, info); 7820 7821 case x86_intercept_lgdt: 7822 case x86_intercept_lidt: 7823 case x86_intercept_lldt: 7824 case x86_intercept_ltr: 7825 case x86_intercept_sgdt: 7826 case x86_intercept_sidt: 7827 case x86_intercept_sldt: 7828 case x86_intercept_str: 7829 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC)) 7830 return X86EMUL_CONTINUE; 7831 7832 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */ 7833 break; 7834 7835 /* TODO: check more intercepts... */ 7836 default: 7837 break; 7838 } 7839 7840 return X86EMUL_UNHANDLEABLE; 7841 } 7842 7843 #ifdef CONFIG_X86_64 7844 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ 7845 static inline int u64_shl_div_u64(u64 a, unsigned int shift, 7846 u64 divisor, u64 *result) 7847 { 7848 u64 low = a << shift, high = a >> (64 - shift); 7849 7850 /* To avoid the overflow on divq */ 7851 if (high >= divisor) 7852 return 1; 7853 7854 /* Low hold the result, high hold rem which is discarded */ 7855 asm("divq %2\n\t" : "=a" (low), "=d" (high) : 7856 "rm" (divisor), "0" (low), "1" (high)); 7857 *result = low; 7858 7859 return 0; 7860 } 7861 7862 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc, 7863 bool *expired) 7864 { 7865 struct vcpu_vmx *vmx; 7866 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; 7867 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer; 7868 7869 vmx = to_vmx(vcpu); 7870 tscl = rdtsc(); 7871 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); 7872 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; 7873 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, 7874 ktimer->timer_advance_ns); 7875 7876 if (delta_tsc > lapic_timer_advance_cycles) 7877 delta_tsc -= lapic_timer_advance_cycles; 7878 else 7879 delta_tsc = 0; 7880 7881 /* Convert to host delta tsc if tsc scaling is enabled */ 7882 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio && 7883 delta_tsc && u64_shl_div_u64(delta_tsc, 7884 kvm_caps.tsc_scaling_ratio_frac_bits, 7885 vcpu->arch.l1_tsc_scaling_ratio, &delta_tsc)) 7886 return -ERANGE; 7887 7888 /* 7889 * If the delta tsc can't fit in the 32 bit after the multi shift, 7890 * we can't use the preemption timer. 7891 * It's possible that it fits on later vmentries, but checking 7892 * on every vmentry is costly so we just use an hrtimer. 7893 */ 7894 if (delta_tsc >> (cpu_preemption_timer_multi + 32)) 7895 return -ERANGE; 7896 7897 vmx->hv_deadline_tsc = tscl + delta_tsc; 7898 *expired = !delta_tsc; 7899 return 0; 7900 } 7901 7902 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) 7903 { 7904 to_vmx(vcpu)->hv_deadline_tsc = -1; 7905 } 7906 #endif 7907 7908 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) 7909 { 7910 if (!kvm_pause_in_guest(vcpu->kvm)) 7911 shrink_ple_window(vcpu); 7912 } 7913 7914 void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu) 7915 { 7916 struct vcpu_vmx *vmx = to_vmx(vcpu); 7917 7918 if (is_guest_mode(vcpu)) { 7919 vmx->nested.update_vmcs01_cpu_dirty_logging = true; 7920 return; 7921 } 7922 7923 /* 7924 * Note, cpu_dirty_logging_count can be changed concurrent with this 7925 * code, but in that case another update request will be made and so 7926 * the guest will never run with a stale PML value. 7927 */ 7928 if (vcpu->kvm->arch.cpu_dirty_logging_count) 7929 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_ENABLE_PML); 7930 else 7931 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_ENABLE_PML); 7932 } 7933 7934 static void vmx_setup_mce(struct kvm_vcpu *vcpu) 7935 { 7936 if (vcpu->arch.mcg_cap & MCG_LMCE_P) 7937 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= 7938 FEAT_CTL_LMCE_ENABLED; 7939 else 7940 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= 7941 ~FEAT_CTL_LMCE_ENABLED; 7942 } 7943 7944 #ifdef CONFIG_KVM_SMM 7945 static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection) 7946 { 7947 /* we need a nested vmexit to enter SMM, postpone if run is pending */ 7948 if (to_vmx(vcpu)->nested.nested_run_pending) 7949 return -EBUSY; 7950 return !is_smm(vcpu); 7951 } 7952 7953 static int vmx_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) 7954 { 7955 struct vcpu_vmx *vmx = to_vmx(vcpu); 7956 7957 /* 7958 * TODO: Implement custom flows for forcing the vCPU out/in of L2 on 7959 * SMI and RSM. Using the common VM-Exit + VM-Enter routines is wrong 7960 * SMI and RSM only modify state that is saved and restored via SMRAM. 7961 * E.g. most MSRs are left untouched, but many are modified by VM-Exit 7962 * and VM-Enter, and thus L2's values may be corrupted on SMI+RSM. 7963 */ 7964 vmx->nested.smm.guest_mode = is_guest_mode(vcpu); 7965 if (vmx->nested.smm.guest_mode) 7966 nested_vmx_vmexit(vcpu, -1, 0, 0); 7967 7968 vmx->nested.smm.vmxon = vmx->nested.vmxon; 7969 vmx->nested.vmxon = false; 7970 vmx_clear_hlt(vcpu); 7971 return 0; 7972 } 7973 7974 static int vmx_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) 7975 { 7976 struct vcpu_vmx *vmx = to_vmx(vcpu); 7977 int ret; 7978 7979 if (vmx->nested.smm.vmxon) { 7980 vmx->nested.vmxon = true; 7981 vmx->nested.smm.vmxon = false; 7982 } 7983 7984 if (vmx->nested.smm.guest_mode) { 7985 ret = nested_vmx_enter_non_root_mode(vcpu, false); 7986 if (ret) 7987 return ret; 7988 7989 vmx->nested.nested_run_pending = 1; 7990 vmx->nested.smm.guest_mode = false; 7991 } 7992 return 0; 7993 } 7994 7995 static void vmx_enable_smi_window(struct kvm_vcpu *vcpu) 7996 { 7997 /* RSM will cause a vmexit anyway. */ 7998 } 7999 #endif 8000 8001 static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu) 8002 { 8003 return to_vmx(vcpu)->nested.vmxon && !is_guest_mode(vcpu); 8004 } 8005 8006 static void vmx_migrate_timers(struct kvm_vcpu *vcpu) 8007 { 8008 if (is_guest_mode(vcpu)) { 8009 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer; 8010 8011 if (hrtimer_try_to_cancel(timer) == 1) 8012 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); 8013 } 8014 } 8015 8016 static void vmx_hardware_unsetup(void) 8017 { 8018 kvm_set_posted_intr_wakeup_handler(NULL); 8019 8020 if (nested) 8021 nested_vmx_hardware_unsetup(); 8022 8023 free_kvm_area(); 8024 } 8025 8026 static bool vmx_check_apicv_inhibit_reasons(enum kvm_apicv_inhibit reason) 8027 { 8028 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) | 8029 BIT(APICV_INHIBIT_REASON_ABSENT) | 8030 BIT(APICV_INHIBIT_REASON_HYPERV) | 8031 BIT(APICV_INHIBIT_REASON_BLOCKIRQ) | 8032 BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | 8033 BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED); 8034 8035 return supported & BIT(reason); 8036 } 8037 8038 static void vmx_vm_destroy(struct kvm *kvm) 8039 { 8040 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); 8041 8042 free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm)); 8043 } 8044 8045 static struct kvm_x86_ops vmx_x86_ops __initdata = { 8046 .name = "kvm_intel", 8047 8048 .hardware_unsetup = vmx_hardware_unsetup, 8049 8050 .hardware_enable = vmx_hardware_enable, 8051 .hardware_disable = vmx_hardware_disable, 8052 .has_emulated_msr = vmx_has_emulated_msr, 8053 8054 .vm_size = sizeof(struct kvm_vmx), 8055 .vm_init = vmx_vm_init, 8056 .vm_destroy = vmx_vm_destroy, 8057 8058 .vcpu_precreate = vmx_vcpu_precreate, 8059 .vcpu_create = vmx_vcpu_create, 8060 .vcpu_free = vmx_vcpu_free, 8061 .vcpu_reset = vmx_vcpu_reset, 8062 8063 .prepare_switch_to_guest = vmx_prepare_switch_to_guest, 8064 .vcpu_load = vmx_vcpu_load, 8065 .vcpu_put = vmx_vcpu_put, 8066 8067 .update_exception_bitmap = vmx_update_exception_bitmap, 8068 .get_msr_feature = vmx_get_msr_feature, 8069 .get_msr = vmx_get_msr, 8070 .set_msr = vmx_set_msr, 8071 .get_segment_base = vmx_get_segment_base, 8072 .get_segment = vmx_get_segment, 8073 .set_segment = vmx_set_segment, 8074 .get_cpl = vmx_get_cpl, 8075 .get_cs_db_l_bits = vmx_get_cs_db_l_bits, 8076 .set_cr0 = vmx_set_cr0, 8077 .is_valid_cr4 = vmx_is_valid_cr4, 8078 .set_cr4 = vmx_set_cr4, 8079 .set_efer = vmx_set_efer, 8080 .get_idt = vmx_get_idt, 8081 .set_idt = vmx_set_idt, 8082 .get_gdt = vmx_get_gdt, 8083 .set_gdt = vmx_set_gdt, 8084 .set_dr7 = vmx_set_dr7, 8085 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, 8086 .cache_reg = vmx_cache_reg, 8087 .get_rflags = vmx_get_rflags, 8088 .set_rflags = vmx_set_rflags, 8089 .get_if_flag = vmx_get_if_flag, 8090 8091 .flush_tlb_all = vmx_flush_tlb_all, 8092 .flush_tlb_current = vmx_flush_tlb_current, 8093 .flush_tlb_gva = vmx_flush_tlb_gva, 8094 .flush_tlb_guest = vmx_flush_tlb_guest, 8095 8096 .vcpu_pre_run = vmx_vcpu_pre_run, 8097 .vcpu_run = vmx_vcpu_run, 8098 .handle_exit = vmx_handle_exit, 8099 .skip_emulated_instruction = vmx_skip_emulated_instruction, 8100 .update_emulated_instruction = vmx_update_emulated_instruction, 8101 .set_interrupt_shadow = vmx_set_interrupt_shadow, 8102 .get_interrupt_shadow = vmx_get_interrupt_shadow, 8103 .patch_hypercall = vmx_patch_hypercall, 8104 .inject_irq = vmx_inject_irq, 8105 .inject_nmi = vmx_inject_nmi, 8106 .inject_exception = vmx_inject_exception, 8107 .cancel_injection = vmx_cancel_injection, 8108 .interrupt_allowed = vmx_interrupt_allowed, 8109 .nmi_allowed = vmx_nmi_allowed, 8110 .get_nmi_mask = vmx_get_nmi_mask, 8111 .set_nmi_mask = vmx_set_nmi_mask, 8112 .enable_nmi_window = vmx_enable_nmi_window, 8113 .enable_irq_window = vmx_enable_irq_window, 8114 .update_cr8_intercept = vmx_update_cr8_intercept, 8115 .set_virtual_apic_mode = vmx_set_virtual_apic_mode, 8116 .set_apic_access_page_addr = vmx_set_apic_access_page_addr, 8117 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, 8118 .load_eoi_exitmap = vmx_load_eoi_exitmap, 8119 .apicv_post_state_restore = vmx_apicv_post_state_restore, 8120 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons, 8121 .hwapic_irr_update = vmx_hwapic_irr_update, 8122 .hwapic_isr_update = vmx_hwapic_isr_update, 8123 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt, 8124 .sync_pir_to_irr = vmx_sync_pir_to_irr, 8125 .deliver_interrupt = vmx_deliver_interrupt, 8126 .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt, 8127 8128 .set_tss_addr = vmx_set_tss_addr, 8129 .set_identity_map_addr = vmx_set_identity_map_addr, 8130 .get_mt_mask = vmx_get_mt_mask, 8131 8132 .get_exit_info = vmx_get_exit_info, 8133 8134 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid, 8135 8136 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, 8137 8138 .get_l2_tsc_offset = vmx_get_l2_tsc_offset, 8139 .get_l2_tsc_multiplier = vmx_get_l2_tsc_multiplier, 8140 .write_tsc_offset = vmx_write_tsc_offset, 8141 .write_tsc_multiplier = vmx_write_tsc_multiplier, 8142 8143 .load_mmu_pgd = vmx_load_mmu_pgd, 8144 8145 .check_intercept = vmx_check_intercept, 8146 .handle_exit_irqoff = vmx_handle_exit_irqoff, 8147 8148 .request_immediate_exit = vmx_request_immediate_exit, 8149 8150 .sched_in = vmx_sched_in, 8151 8152 .cpu_dirty_log_size = PML_ENTITY_NUM, 8153 .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging, 8154 8155 .nested_ops = &vmx_nested_ops, 8156 8157 .pi_update_irte = vmx_pi_update_irte, 8158 .pi_start_assignment = vmx_pi_start_assignment, 8159 8160 #ifdef CONFIG_X86_64 8161 .set_hv_timer = vmx_set_hv_timer, 8162 .cancel_hv_timer = vmx_cancel_hv_timer, 8163 #endif 8164 8165 .setup_mce = vmx_setup_mce, 8166 8167 #ifdef CONFIG_KVM_SMM 8168 .smi_allowed = vmx_smi_allowed, 8169 .enter_smm = vmx_enter_smm, 8170 .leave_smm = vmx_leave_smm, 8171 .enable_smi_window = vmx_enable_smi_window, 8172 #endif 8173 8174 .can_emulate_instruction = vmx_can_emulate_instruction, 8175 .apic_init_signal_blocked = vmx_apic_init_signal_blocked, 8176 .migrate_timers = vmx_migrate_timers, 8177 8178 .msr_filter_changed = vmx_msr_filter_changed, 8179 .complete_emulated_msr = kvm_complete_insn_gp, 8180 8181 .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, 8182 }; 8183 8184 static unsigned int vmx_handle_intel_pt_intr(void) 8185 { 8186 struct kvm_vcpu *vcpu = kvm_get_running_vcpu(); 8187 8188 /* '0' on failure so that the !PT case can use a RET0 static call. */ 8189 if (!vcpu || !kvm_handling_nmi_from_guest(vcpu)) 8190 return 0; 8191 8192 kvm_make_request(KVM_REQ_PMI, vcpu); 8193 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT, 8194 (unsigned long *)&vcpu->arch.pmu.global_status); 8195 return 1; 8196 } 8197 8198 static __init void vmx_setup_user_return_msrs(void) 8199 { 8200 8201 /* 8202 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm 8203 * will emulate SYSCALL in legacy mode if the vendor string in guest 8204 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To 8205 * support this emulation, MSR_STAR is included in the list for i386, 8206 * but is never loaded into hardware. MSR_CSTAR is also never loaded 8207 * into hardware and is here purely for emulation purposes. 8208 */ 8209 const u32 vmx_uret_msrs_list[] = { 8210 #ifdef CONFIG_X86_64 8211 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, 8212 #endif 8213 MSR_EFER, MSR_TSC_AUX, MSR_STAR, 8214 MSR_IA32_TSX_CTRL, 8215 }; 8216 int i; 8217 8218 BUILD_BUG_ON(ARRAY_SIZE(vmx_uret_msrs_list) != MAX_NR_USER_RETURN_MSRS); 8219 8220 for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i) 8221 kvm_add_user_return_msr(vmx_uret_msrs_list[i]); 8222 } 8223 8224 static void __init vmx_setup_me_spte_mask(void) 8225 { 8226 u64 me_mask = 0; 8227 8228 /* 8229 * kvm_get_shadow_phys_bits() returns shadow_phys_bits. Use 8230 * the former to avoid exposing shadow_phys_bits. 8231 * 8232 * On pre-MKTME system, boot_cpu_data.x86_phys_bits equals to 8233 * shadow_phys_bits. On MKTME and/or TDX capable systems, 8234 * boot_cpu_data.x86_phys_bits holds the actual physical address 8235 * w/o the KeyID bits, and shadow_phys_bits equals to MAXPHYADDR 8236 * reported by CPUID. Those bits between are KeyID bits. 8237 */ 8238 if (boot_cpu_data.x86_phys_bits != kvm_get_shadow_phys_bits()) 8239 me_mask = rsvd_bits(boot_cpu_data.x86_phys_bits, 8240 kvm_get_shadow_phys_bits() - 1); 8241 /* 8242 * Unlike SME, host kernel doesn't support setting up any 8243 * MKTME KeyID on Intel platforms. No memory encryption 8244 * bits should be included into the SPTE. 8245 */ 8246 kvm_mmu_set_me_spte_mask(0, me_mask); 8247 } 8248 8249 static struct kvm_x86_init_ops vmx_init_ops __initdata; 8250 8251 static __init int hardware_setup(void) 8252 { 8253 unsigned long host_bndcfgs; 8254 struct desc_ptr dt; 8255 int r; 8256 8257 store_idt(&dt); 8258 host_idt_base = dt.address; 8259 8260 vmx_setup_user_return_msrs(); 8261 8262 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0) 8263 return -EIO; 8264 8265 if (cpu_has_perf_global_ctrl_bug()) 8266 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " 8267 "does not work properly. Using workaround\n"); 8268 8269 if (boot_cpu_has(X86_FEATURE_NX)) 8270 kvm_enable_efer_bits(EFER_NX); 8271 8272 if (boot_cpu_has(X86_FEATURE_MPX)) { 8273 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs); 8274 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost"); 8275 } 8276 8277 if (!cpu_has_vmx_mpx()) 8278 kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | 8279 XFEATURE_MASK_BNDCSR); 8280 8281 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || 8282 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) 8283 enable_vpid = 0; 8284 8285 if (!cpu_has_vmx_ept() || 8286 !cpu_has_vmx_ept_4levels() || 8287 !cpu_has_vmx_ept_mt_wb() || 8288 !cpu_has_vmx_invept_global()) 8289 enable_ept = 0; 8290 8291 /* NX support is required for shadow paging. */ 8292 if (!enable_ept && !boot_cpu_has(X86_FEATURE_NX)) { 8293 pr_err_ratelimited("kvm: NX (Execute Disable) not supported\n"); 8294 return -EOPNOTSUPP; 8295 } 8296 8297 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) 8298 enable_ept_ad_bits = 0; 8299 8300 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) 8301 enable_unrestricted_guest = 0; 8302 8303 if (!cpu_has_vmx_flexpriority()) 8304 flexpriority_enabled = 0; 8305 8306 if (!cpu_has_virtual_nmis()) 8307 enable_vnmi = 0; 8308 8309 #ifdef CONFIG_X86_SGX_KVM 8310 if (!cpu_has_vmx_encls_vmexit()) 8311 enable_sgx = false; 8312 #endif 8313 8314 /* 8315 * set_apic_access_page_addr() is used to reload apic access 8316 * page upon invalidation. No need to do anything if not 8317 * using the APIC_ACCESS_ADDR VMCS field. 8318 */ 8319 if (!flexpriority_enabled) 8320 vmx_x86_ops.set_apic_access_page_addr = NULL; 8321 8322 if (!cpu_has_vmx_tpr_shadow()) 8323 vmx_x86_ops.update_cr8_intercept = NULL; 8324 8325 #if IS_ENABLED(CONFIG_HYPERV) 8326 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH 8327 && enable_ept) { 8328 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb; 8329 vmx_x86_ops.tlb_remote_flush_with_range = 8330 hv_remote_flush_tlb_with_range; 8331 } 8332 #endif 8333 8334 if (!cpu_has_vmx_ple()) { 8335 ple_gap = 0; 8336 ple_window = 0; 8337 ple_window_grow = 0; 8338 ple_window_max = 0; 8339 ple_window_shrink = 0; 8340 } 8341 8342 if (!cpu_has_vmx_apicv()) 8343 enable_apicv = 0; 8344 if (!enable_apicv) 8345 vmx_x86_ops.sync_pir_to_irr = NULL; 8346 8347 if (!enable_apicv || !cpu_has_vmx_ipiv()) 8348 enable_ipiv = false; 8349 8350 if (cpu_has_vmx_tsc_scaling()) 8351 kvm_caps.has_tsc_control = true; 8352 8353 kvm_caps.max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; 8354 kvm_caps.tsc_scaling_ratio_frac_bits = 48; 8355 kvm_caps.has_bus_lock_exit = cpu_has_vmx_bus_lock_detection(); 8356 kvm_caps.has_notify_vmexit = cpu_has_notify_vmexit(); 8357 8358 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ 8359 8360 if (enable_ept) 8361 kvm_mmu_set_ept_masks(enable_ept_ad_bits, 8362 cpu_has_vmx_ept_execute_only()); 8363 8364 /* 8365 * Setup shadow_me_value/shadow_me_mask to include MKTME KeyID 8366 * bits to shadow_zero_check. 8367 */ 8368 vmx_setup_me_spte_mask(); 8369 8370 kvm_configure_mmu(enable_ept, 0, vmx_get_max_tdp_level(), 8371 ept_caps_to_lpage_level(vmx_capability.ept)); 8372 8373 /* 8374 * Only enable PML when hardware supports PML feature, and both EPT 8375 * and EPT A/D bit features are enabled -- PML depends on them to work. 8376 */ 8377 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) 8378 enable_pml = 0; 8379 8380 if (!enable_pml) 8381 vmx_x86_ops.cpu_dirty_log_size = 0; 8382 8383 if (!cpu_has_vmx_preemption_timer()) 8384 enable_preemption_timer = false; 8385 8386 if (enable_preemption_timer) { 8387 u64 use_timer_freq = 5000ULL * 1000 * 1000; 8388 8389 cpu_preemption_timer_multi = 8390 vmcs_config.misc & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; 8391 8392 if (tsc_khz) 8393 use_timer_freq = (u64)tsc_khz * 1000; 8394 use_timer_freq >>= cpu_preemption_timer_multi; 8395 8396 /* 8397 * KVM "disables" the preemption timer by setting it to its max 8398 * value. Don't use the timer if it might cause spurious exits 8399 * at a rate faster than 0.1 Hz (of uninterrupted guest time). 8400 */ 8401 if (use_timer_freq > 0xffffffffu / 10) 8402 enable_preemption_timer = false; 8403 } 8404 8405 if (!enable_preemption_timer) { 8406 vmx_x86_ops.set_hv_timer = NULL; 8407 vmx_x86_ops.cancel_hv_timer = NULL; 8408 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit; 8409 } 8410 8411 kvm_caps.supported_mce_cap |= MCG_LMCE_P; 8412 kvm_caps.supported_mce_cap |= MCG_CMCI_P; 8413 8414 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST) 8415 return -EINVAL; 8416 if (!enable_ept || !enable_pmu || !cpu_has_vmx_intel_pt()) 8417 pt_mode = PT_MODE_SYSTEM; 8418 if (pt_mode == PT_MODE_HOST_GUEST) 8419 vmx_init_ops.handle_intel_pt_intr = vmx_handle_intel_pt_intr; 8420 else 8421 vmx_init_ops.handle_intel_pt_intr = NULL; 8422 8423 setup_default_sgx_lepubkeyhash(); 8424 8425 if (nested) { 8426 nested_vmx_setup_ctls_msrs(&vmcs_config, vmx_capability.ept); 8427 8428 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers); 8429 if (r) 8430 return r; 8431 } 8432 8433 vmx_set_cpu_caps(); 8434 8435 r = alloc_kvm_area(); 8436 if (r && nested) 8437 nested_vmx_hardware_unsetup(); 8438 8439 kvm_set_posted_intr_wakeup_handler(pi_wakeup_handler); 8440 8441 return r; 8442 } 8443 8444 static struct kvm_x86_init_ops vmx_init_ops __initdata = { 8445 .cpu_has_kvm_support = cpu_has_kvm_support, 8446 .disabled_by_bios = vmx_disabled_by_bios, 8447 .check_processor_compatibility = vmx_check_processor_compat, 8448 .hardware_setup = hardware_setup, 8449 .handle_intel_pt_intr = NULL, 8450 8451 .runtime_ops = &vmx_x86_ops, 8452 .pmu_ops = &intel_pmu_ops, 8453 }; 8454 8455 static void vmx_cleanup_l1d_flush(void) 8456 { 8457 if (vmx_l1d_flush_pages) { 8458 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); 8459 vmx_l1d_flush_pages = NULL; 8460 } 8461 /* Restore state so sysfs ignores VMX */ 8462 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; 8463 } 8464 8465 static void vmx_exit(void) 8466 { 8467 #ifdef CONFIG_KEXEC_CORE 8468 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); 8469 synchronize_rcu(); 8470 #endif 8471 8472 kvm_exit(); 8473 8474 #if IS_ENABLED(CONFIG_HYPERV) 8475 if (static_branch_unlikely(&enable_evmcs)) { 8476 int cpu; 8477 struct hv_vp_assist_page *vp_ap; 8478 /* 8479 * Reset everything to support using non-enlightened VMCS 8480 * access later (e.g. when we reload the module with 8481 * enlightened_vmcs=0) 8482 */ 8483 for_each_online_cpu(cpu) { 8484 vp_ap = hv_get_vp_assist_page(cpu); 8485 8486 if (!vp_ap) 8487 continue; 8488 8489 vp_ap->nested_control.features.directhypercall = 0; 8490 vp_ap->current_nested_vmcs = 0; 8491 vp_ap->enlighten_vmentry = 0; 8492 } 8493 8494 static_branch_disable(&enable_evmcs); 8495 } 8496 #endif 8497 vmx_cleanup_l1d_flush(); 8498 8499 allow_smaller_maxphyaddr = false; 8500 } 8501 module_exit(vmx_exit); 8502 8503 static int __init vmx_init(void) 8504 { 8505 int r, cpu; 8506 8507 #if IS_ENABLED(CONFIG_HYPERV) 8508 /* 8509 * Enlightened VMCS usage should be recommended and the host needs 8510 * to support eVMCS v1 or above. We can also disable eVMCS support 8511 * with module parameter. 8512 */ 8513 if (enlightened_vmcs && 8514 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && 8515 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= 8516 KVM_EVMCS_VERSION) { 8517 8518 /* Check that we have assist pages on all online CPUs */ 8519 for_each_online_cpu(cpu) { 8520 if (!hv_get_vp_assist_page(cpu)) { 8521 enlightened_vmcs = false; 8522 break; 8523 } 8524 } 8525 8526 if (enlightened_vmcs) { 8527 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); 8528 static_branch_enable(&enable_evmcs); 8529 } 8530 8531 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) 8532 vmx_x86_ops.enable_l2_tlb_flush 8533 = hv_enable_l2_tlb_flush; 8534 8535 } else { 8536 enlightened_vmcs = false; 8537 } 8538 #endif 8539 8540 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx), 8541 __alignof__(struct vcpu_vmx), THIS_MODULE); 8542 if (r) 8543 return r; 8544 8545 /* 8546 * Must be called after kvm_init() so enable_ept is properly set 8547 * up. Hand the parameter mitigation value in which was stored in 8548 * the pre module init parser. If no parameter was given, it will 8549 * contain 'auto' which will be turned into the default 'cond' 8550 * mitigation mode. 8551 */ 8552 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param); 8553 if (r) { 8554 vmx_exit(); 8555 return r; 8556 } 8557 8558 vmx_setup_fb_clear_ctrl(); 8559 8560 for_each_possible_cpu(cpu) { 8561 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); 8562 8563 pi_init_cpu(cpu); 8564 } 8565 8566 #ifdef CONFIG_KEXEC_CORE 8567 rcu_assign_pointer(crash_vmclear_loaded_vmcss, 8568 crash_vmclear_local_loaded_vmcss); 8569 #endif 8570 vmx_check_vmcs12_offsets(); 8571 8572 /* 8573 * Shadow paging doesn't have a (further) performance penalty 8574 * from GUEST_MAXPHYADDR < HOST_MAXPHYADDR so enable it 8575 * by default 8576 */ 8577 if (!enable_ept) 8578 allow_smaller_maxphyaddr = true; 8579 8580 return 0; 8581 } 8582 module_init(vmx_init); 8583