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