1 // SPDX-License-Identifier: GPL-2.0 2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 3 4 #include <linux/objtool.h> 5 #include <linux/percpu.h> 6 7 #include <asm/debugreg.h> 8 #include <asm/mmu_context.h> 9 #include <asm/msr.h> 10 11 #include "x86.h" 12 #include "cpuid.h" 13 #include "hyperv.h" 14 #include "irq.h" 15 #include "mmu.h" 16 #include "nested.h" 17 #include "pmu.h" 18 #include "posted_intr.h" 19 #include "sgx.h" 20 #include "trace.h" 21 #include "vmx.h" 22 #include "smm.h" 23 #include "x86_ops.h" 24 25 static bool __read_mostly enable_shadow_vmcs = 1; 26 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); 27 28 static bool __ro_after_init warn_on_missed_cc; 29 module_param(warn_on_missed_cc, bool, 0444); 30 31 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK 32 33 /* 34 * Hyper-V requires all of these, so mark them as supported even though 35 * they are just treated the same as all-context. 36 */ 37 #define VMX_VPID_EXTENT_SUPPORTED_MASK \ 38 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ 39 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ 40 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ 41 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) 42 43 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 44 45 enum { 46 VMX_VMREAD_BITMAP, 47 VMX_VMWRITE_BITMAP, 48 VMX_BITMAP_NR 49 }; 50 static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; 51 52 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) 53 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) 54 55 struct shadow_vmcs_field { 56 u16 encoding; 57 u16 offset; 58 }; 59 static struct shadow_vmcs_field shadow_read_only_fields[] = { 60 #define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) }, 61 #include "vmcs_shadow_fields.h" 62 }; 63 static int max_shadow_read_only_fields = 64 ARRAY_SIZE(shadow_read_only_fields); 65 66 static struct shadow_vmcs_field shadow_read_write_fields[] = { 67 #define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) }, 68 #include "vmcs_shadow_fields.h" 69 }; 70 static int max_shadow_read_write_fields = 71 ARRAY_SIZE(shadow_read_write_fields); 72 73 static void init_vmcs_shadow_fields(void) 74 { 75 int i, j; 76 77 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); 78 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); 79 80 for (i = j = 0; i < max_shadow_read_only_fields; i++) { 81 struct shadow_vmcs_field entry = shadow_read_only_fields[i]; 82 u16 field = entry.encoding; 83 84 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && 85 (i + 1 == max_shadow_read_only_fields || 86 shadow_read_only_fields[i + 1].encoding != field + 1)) 87 pr_err("Missing field from shadow_read_only_field %x\n", 88 field + 1); 89 90 if (get_vmcs12_field_offset(field) < 0) 91 continue; 92 93 clear_bit(field, vmx_vmread_bitmap); 94 if (field & 1) 95 #ifdef CONFIG_X86_64 96 continue; 97 #else 98 entry.offset += sizeof(u32); 99 #endif 100 shadow_read_only_fields[j++] = entry; 101 } 102 max_shadow_read_only_fields = j; 103 104 for (i = j = 0; i < max_shadow_read_write_fields; i++) { 105 struct shadow_vmcs_field entry = shadow_read_write_fields[i]; 106 u16 field = entry.encoding; 107 108 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && 109 (i + 1 == max_shadow_read_write_fields || 110 shadow_read_write_fields[i + 1].encoding != field + 1)) 111 pr_err("Missing field from shadow_read_write_field %x\n", 112 field + 1); 113 114 WARN_ONCE(field >= GUEST_ES_AR_BYTES && 115 field <= GUEST_TR_AR_BYTES, 116 "Update vmcs12_write_any() to drop reserved bits from AR_BYTES"); 117 118 if (get_vmcs12_field_offset(field) < 0) 119 continue; 120 121 /* 122 * KVM emulates PML and the VMX preemption timer irrespective 123 * of hardware support, but shadowing their related VMCS fields 124 * requires hardware support as the CPU will reject VMWRITEs to 125 * fields that don't exist. 126 */ 127 switch (field) { 128 case GUEST_PML_INDEX: 129 if (!cpu_has_vmx_pml()) 130 continue; 131 break; 132 case VMX_PREEMPTION_TIMER_VALUE: 133 if (!cpu_has_vmx_preemption_timer()) 134 continue; 135 break; 136 default: 137 break; 138 } 139 140 clear_bit(field, vmx_vmwrite_bitmap); 141 clear_bit(field, vmx_vmread_bitmap); 142 if (field & 1) 143 #ifdef CONFIG_X86_64 144 continue; 145 #else 146 entry.offset += sizeof(u32); 147 #endif 148 shadow_read_write_fields[j++] = entry; 149 } 150 max_shadow_read_write_fields = j; 151 } 152 153 /* 154 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), 155 * set the success or error code of an emulated VMX instruction (as specified 156 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated 157 * instruction. 158 */ 159 static int nested_vmx_succeed(struct kvm_vcpu *vcpu) 160 { 161 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) 162 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | 163 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); 164 return kvm_skip_emulated_instruction(vcpu); 165 } 166 167 static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu) 168 { 169 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) 170 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | 171 X86_EFLAGS_SF | X86_EFLAGS_OF)) 172 | X86_EFLAGS_CF); 173 return kvm_skip_emulated_instruction(vcpu); 174 } 175 176 static int nested_vmx_failValid(struct kvm_vcpu *vcpu, 177 u32 vm_instruction_error) 178 { 179 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) 180 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | 181 X86_EFLAGS_SF | X86_EFLAGS_OF)) 182 | X86_EFLAGS_ZF); 183 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; 184 /* 185 * We don't need to force sync to shadow VMCS because 186 * VM_INSTRUCTION_ERROR is not shadowed. Enlightened VMCS 'shadows' all 187 * fields and thus must be synced. 188 */ 189 if (nested_vmx_is_evmptr12_set(to_vmx(vcpu))) 190 to_vmx(vcpu)->nested.need_vmcs12_to_shadow_sync = true; 191 192 return kvm_skip_emulated_instruction(vcpu); 193 } 194 195 static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error) 196 { 197 struct vcpu_vmx *vmx = to_vmx(vcpu); 198 199 /* 200 * failValid writes the error number to the current VMCS, which 201 * can't be done if there isn't a current VMCS. 202 */ 203 if (vmx->nested.current_vmptr == INVALID_GPA && 204 !nested_vmx_is_evmptr12_valid(vmx)) 205 return nested_vmx_failInvalid(vcpu); 206 207 return nested_vmx_failValid(vcpu, vm_instruction_error); 208 } 209 210 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) 211 { 212 /* TODO: not to reset guest simply here. */ 213 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); 214 pr_debug_ratelimited("nested vmx abort, indicator %d\n", indicator); 215 } 216 217 static inline bool vmx_control_verify(u32 control, u32 low, u32 high) 218 { 219 return fixed_bits_valid(control, low, high); 220 } 221 222 static inline u64 vmx_control_msr(u32 low, u32 high) 223 { 224 return low | ((u64)high << 32); 225 } 226 227 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) 228 { 229 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); 230 vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA); 231 vmx->nested.need_vmcs12_to_shadow_sync = false; 232 } 233 234 static inline void nested_release_evmcs(struct kvm_vcpu *vcpu) 235 { 236 #ifdef CONFIG_KVM_HYPERV 237 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 238 struct vcpu_vmx *vmx = to_vmx(vcpu); 239 240 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map); 241 vmx->nested.hv_evmcs = NULL; 242 vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID; 243 244 if (hv_vcpu) { 245 hv_vcpu->nested.pa_page_gpa = INVALID_GPA; 246 hv_vcpu->nested.vm_id = 0; 247 hv_vcpu->nested.vp_id = 0; 248 } 249 #endif 250 } 251 252 static bool nested_evmcs_handle_vmclear(struct kvm_vcpu *vcpu, gpa_t vmptr) 253 { 254 #ifdef CONFIG_KVM_HYPERV 255 struct vcpu_vmx *vmx = to_vmx(vcpu); 256 /* 257 * When Enlightened VMEntry is enabled on the calling CPU we treat 258 * memory area pointer by vmptr as Enlightened VMCS (as there's no good 259 * way to distinguish it from VMCS12) and we must not corrupt it by 260 * writing to the non-existent 'launch_state' field. The area doesn't 261 * have to be the currently active EVMCS on the calling CPU and there's 262 * nothing KVM has to do to transition it from 'active' to 'non-active' 263 * state. It is possible that the area will stay mapped as 264 * vmx->nested.hv_evmcs but this shouldn't be a problem. 265 */ 266 if (!guest_cpu_cap_has_evmcs(vcpu) || 267 !evmptr_is_valid(nested_get_evmptr(vcpu))) 268 return false; 269 270 if (nested_vmx_evmcs(vmx) && vmptr == vmx->nested.hv_evmcs_vmptr) 271 nested_release_evmcs(vcpu); 272 273 return true; 274 #else 275 return false; 276 #endif 277 } 278 279 static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx, 280 struct loaded_vmcs *prev) 281 { 282 struct vmcs_host_state *dest, *src; 283 284 if (unlikely(!vmx->vt.guest_state_loaded)) 285 return; 286 287 src = &prev->host_state; 288 dest = &vmx->loaded_vmcs->host_state; 289 290 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base); 291 dest->ldt_sel = src->ldt_sel; 292 #ifdef CONFIG_X86_64 293 dest->ds_sel = src->ds_sel; 294 dest->es_sel = src->es_sel; 295 #endif 296 } 297 298 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) 299 { 300 struct vcpu_vmx *vmx = to_vmx(vcpu); 301 struct loaded_vmcs *prev; 302 int cpu; 303 304 if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs)) 305 return; 306 307 cpu = get_cpu(); 308 prev = vmx->loaded_vmcs; 309 vmx->loaded_vmcs = vmcs; 310 vmx_vcpu_load_vmcs(vcpu, cpu); 311 vmx_sync_vmcs_host_state(vmx, prev); 312 put_cpu(); 313 314 kvm_clear_available_registers(vcpu, VMX_REGS_LAZY_LOAD_SET); 315 316 /* 317 * All lazily updated registers will be reloaded from VMCS12 on both 318 * vmentry and vmexit. 319 */ 320 kvm_reset_dirty_registers(vcpu); 321 } 322 323 static void nested_put_vmcs12_pages(struct kvm_vcpu *vcpu) 324 { 325 struct vcpu_vmx *vmx = to_vmx(vcpu); 326 327 kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map); 328 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map); 329 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map); 330 vmx->nested.pi_desc = NULL; 331 } 332 333 /* 334 * Free whatever needs to be freed from vmx->nested when L1 goes down, or 335 * just stops using VMX. 336 */ 337 static void free_nested(struct kvm_vcpu *vcpu) 338 { 339 struct vcpu_vmx *vmx = to_vmx(vcpu); 340 struct vmcs *shadow_vmcs; 341 342 if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01)) 343 vmx_switch_vmcs(vcpu, &vmx->vmcs01); 344 345 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) 346 return; 347 348 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); 349 350 vmx->nested.vmxon = false; 351 vmx->nested.smm.vmxon = false; 352 vmx->nested.vmxon_ptr = INVALID_GPA; 353 free_vpid(vmx->nested.vpid02); 354 vmx->nested.posted_intr_nv = -1; 355 vmx->nested.current_vmptr = INVALID_GPA; 356 if (enable_shadow_vmcs) { 357 vmx_disable_shadow_vmcs(vmx); 358 359 /* 360 * Keep the pointer visible until after VMCLEAR, so migration 361 * can clear an active shadow VMCS on the old CPU. 362 */ 363 shadow_vmcs = vmx->vmcs01.shadow_vmcs; 364 vmcs_clear(shadow_vmcs); 365 vmx->vmcs01.shadow_vmcs = NULL; 366 free_vmcs(shadow_vmcs); 367 } 368 kfree(vmx->nested.cached_vmcs12); 369 vmx->nested.cached_vmcs12 = NULL; 370 kfree(vmx->nested.cached_shadow_vmcs12); 371 vmx->nested.cached_shadow_vmcs12 = NULL; 372 373 nested_put_vmcs12_pages(vcpu); 374 375 kvm_mmu_free_roots(vcpu->kvm, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); 376 377 nested_release_evmcs(vcpu); 378 379 free_loaded_vmcs(&vmx->nested.vmcs02); 380 } 381 382 /* 383 * Ensure that the current vmcs of the logical processor is the 384 * vmcs01 of the vcpu before calling free_nested(). 385 */ 386 void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu) 387 { 388 vcpu_load(vcpu); 389 vmx_leave_nested(vcpu); 390 vcpu_put(vcpu); 391 } 392 393 #define EPTP_PA_MASK GENMASK_ULL(51, 12) 394 395 static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp) 396 { 397 return VALID_PAGE(root_hpa) && 398 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK)); 399 } 400 401 static void nested_ept_invalidate_addr(struct kvm_vcpu *vcpu, gpa_t eptp, 402 gpa_t addr) 403 { 404 unsigned long roots = 0; 405 uint i; 406 struct kvm_mmu_root_info *cached_root; 407 408 WARN_ON_ONCE(!mmu_is_nested(vcpu)); 409 410 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { 411 cached_root = &vcpu->arch.mmu->prev_roots[i]; 412 413 if (nested_ept_root_matches(cached_root->hpa, cached_root->pgd, 414 eptp)) 415 roots |= KVM_MMU_ROOT_PREVIOUS(i); 416 } 417 if (roots) 418 kvm_mmu_invalidate_addr(vcpu, &vcpu->arch.ngpa_walk, addr, roots); 419 } 420 421 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, 422 struct x86_exception *fault, 423 bool from_hardware) 424 { 425 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 426 struct vcpu_vmx *vmx = to_vmx(vcpu); 427 unsigned long exit_qualification; 428 u32 vm_exit_reason; 429 430 if (vmx->nested.pml_full) { 431 vm_exit_reason = EXIT_REASON_PML_FULL; 432 vmx->nested.pml_full = false; 433 434 /* 435 * It should be impossible to trigger a nested PML Full VM-Exit 436 * for anything other than an EPT Violation from L2. KVM *can* 437 * trigger nEPT page fault injection in response to an EPT 438 * Misconfig, e.g. if the MMIO SPTE was stale and L1's EPT 439 * tables also changed, but KVM should not treat EPT Misconfig 440 * VM-Exits as writes. 441 */ 442 WARN_ON_ONCE(vmx->vt.exit_reason.basic != EXIT_REASON_EPT_VIOLATION); 443 444 /* 445 * PML Full and EPT Violation VM-Exits both use bit 12 to report 446 * "NMI unblocking due to IRET", i.e. the bit can be propagated 447 * as-is from the original EXIT_QUALIFICATION. 448 */ 449 exit_qualification = vmx_get_exit_qual(vcpu) & INTR_INFO_UNBLOCK_NMI; 450 } else { 451 if (fault->error_code & PFERR_RSVD_MASK) { 452 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG; 453 exit_qualification = 0; 454 } else { 455 u64 mask = EPT_VIOLATION_GVA_IS_VALID | 456 EPT_VIOLATION_GVA_TRANSLATED; 457 458 if (vmx->nested.msrs.ept_caps & VMX_EPT_ADVANCED_VMEXIT_INFO_BIT) 459 mask |= EPT_VIOLATION_GVA_USER | 460 EPT_VIOLATION_GVA_WRITABLE | 461 EPT_VIOLATION_GVA_NX; 462 463 exit_qualification = fault->exit_qualification & ~mask; 464 465 /* 466 * Use the EXIT_QUALIFICATION from the VMCS if and only 467 * if the hardware VM-Exit from L2 was an EPT Violation. 468 * If the fault is synthesized, then EXIT_QUALIFICATION 469 * is stale and/or holds entirely different data. And 470 * conversely, KVM _must_ rely on EXIT_QUALIFICATION if 471 * the fault came from hardware, because KVM only sees 472 * and walks the faulting GPA. 473 */ 474 if (from_hardware) 475 exit_qualification |= vmx_get_exit_qual(vcpu) & mask; 476 else 477 exit_qualification |= fault->exit_qualification & mask; 478 479 vm_exit_reason = EXIT_REASON_EPT_VIOLATION; 480 } 481 482 /* 483 * Although the caller (kvm_inject_emulated_page_fault) would 484 * have already synced the faulting address in the shadow EPT 485 * tables for the current EPTP12, we also need to sync it for 486 * any other cached EPTP02s based on the same EP4TA, since the 487 * TLB associates mappings to the EP4TA rather than the full EPTP. 488 */ 489 nested_ept_invalidate_addr(vcpu, vmcs12->ept_pointer, 490 fault->address); 491 } 492 493 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification); 494 vmcs12->guest_physical_address = fault->address; 495 } 496 497 static inline bool nested_ept_mbec_enabled(struct kvm_vcpu *vcpu) 498 { 499 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 500 501 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC); 502 } 503 504 static void nested_ept_new_eptp(struct kvm_vcpu *vcpu) 505 { 506 struct vcpu_vmx *vmx = to_vmx(vcpu); 507 bool execonly = vmx->nested.msrs.ept_caps & VMX_EPT_EXECUTE_ONLY_BIT; 508 int ept_lpage_level = ept_caps_to_lpage_level(vmx->nested.msrs.ept_caps); 509 510 kvm_init_shadow_ept_mmu(vcpu, execonly, ept_lpage_level, 511 nested_ept_ad_enabled(vcpu), 512 nested_ept_mbec_enabled(vcpu), 513 nested_ept_get_eptp(vcpu)); 514 } 515 516 static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) 517 { 518 WARN_ON(mmu_is_nested(vcpu)); 519 520 vcpu->arch.mmu = &vcpu->arch.guest_mmu; 521 nested_ept_new_eptp(vcpu); 522 vcpu->arch.ngpa_walk.get_guest_pgd = nested_ept_get_eptp; 523 vcpu->arch.ngpa_walk.get_pdptr = kvm_pdptr_read; 524 525 vcpu->arch.ngpa_walk.inject_page_fault = nested_ept_inject_page_fault; 526 } 527 528 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) 529 { 530 vcpu->arch.mmu = &vcpu->arch.root_mmu; 531 } 532 533 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, 534 u16 error_code) 535 { 536 bool inequality, bit; 537 538 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; 539 inequality = 540 (error_code & vmcs12->page_fault_error_code_mask) != 541 vmcs12->page_fault_error_code_match; 542 return inequality ^ bit; 543 } 544 545 static bool nested_vmx_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector, 546 u32 error_code) 547 { 548 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 549 550 /* 551 * Drop bits 31:16 of the error code when performing the #PF mask+match 552 * check. All VMCS fields involved are 32 bits, but Intel CPUs never 553 * set bits 31:16 and VMX disallows setting bits 31:16 in the injected 554 * error code. Including the to-be-dropped bits in the check might 555 * result in an "impossible" or missed exit from L1's perspective. 556 */ 557 if (vector == PF_VECTOR) 558 return nested_vmx_is_page_fault_vmexit(vmcs12, (u16)error_code); 559 560 return (vmcs12->exception_bitmap & (1u << vector)); 561 } 562 563 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, 564 struct vmcs12 *vmcs12) 565 { 566 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) 567 return 0; 568 569 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) || 570 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b))) 571 return -EINVAL; 572 573 return 0; 574 } 575 576 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, 577 struct vmcs12 *vmcs12) 578 { 579 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) 580 return 0; 581 582 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap))) 583 return -EINVAL; 584 585 return 0; 586 } 587 588 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, 589 struct vmcs12 *vmcs12) 590 { 591 gpa_t vtpr_gpa = vmcs12->virtual_apic_page_addr + APIC_TASKPRI; 592 u32 vtpr; 593 594 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) 595 return 0; 596 597 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))) 598 return -EINVAL; 599 600 if (CC(!nested_cpu_has_vid(vmcs12) && vmcs12->tpr_threshold >> 4)) 601 return -EINVAL; 602 603 /* 604 * Do the illegal vTPR vs. TPR Threshold consistency check if and only 605 * if KVM is configured to WARN on missed consistency checks, otherwise 606 * it's a waste of time. KVM needs to rely on hardware to fully detect 607 * an illegal combination due to the vTPR being writable by L1 at all 608 * times (it's an in-memory value, not a VMCS field). I.e. even if the 609 * check passes now, it might fail at the actual VM-Enter. 610 * 611 * If reading guest memory fails, skip the check as KVM's de facto ABI 612 * for VMX instruction accesses to non-existent memory is to provide 613 * PCI Bus Error semantics (reads return 0xFFs), in which case the vTPR 614 * is guaranteed to greater than or equal to the threshold. 615 * 616 * Note! Deliberately use the VM-scoped API when reading guest memory, 617 * to ensure the read doesn't hit SMRAM when restoring L2 state on RSM, 618 * and only perform the check when in KVM_RUN, to avoid a false failure 619 * if userspace hasn't yet configured memslots during state restore. 620 */ 621 if (warn_on_missed_cc && vcpu->wants_to_run && 622 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW) && 623 !nested_cpu_has_vid(vmcs12) && 624 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && 625 !kvm_read_guest(vcpu->kvm, vtpr_gpa, &vtpr, sizeof(vtpr)) && 626 CC((vmcs12->tpr_threshold & GENMASK(3, 0)) > ((vtpr >> 4) & GENMASK(3, 0)))) 627 return -EINVAL; 628 629 return 0; 630 } 631 632 /* 633 * For x2APIC MSRs, ignore the vmcs01 bitmap. L1 can enable x2APIC without L1 634 * itself utilizing x2APIC. All MSRs were previously set to be intercepted, 635 * only the "disable intercept" case needs to be handled. 636 */ 637 static void nested_vmx_disable_intercept_for_x2apic_msr(unsigned long *msr_bitmap_l1, 638 unsigned long *msr_bitmap_l0, 639 u32 msr, int type) 640 { 641 if (type & MSR_TYPE_R && !vmx_test_msr_bitmap_read(msr_bitmap_l1, msr)) 642 vmx_clear_msr_bitmap_read(msr_bitmap_l0, msr); 643 644 if (type & MSR_TYPE_W && !vmx_test_msr_bitmap_write(msr_bitmap_l1, msr)) 645 vmx_clear_msr_bitmap_write(msr_bitmap_l0, msr); 646 } 647 648 static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) 649 { 650 int msr; 651 652 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { 653 unsigned word = msr / BITS_PER_LONG; 654 655 msr_bitmap[word] = ~0; 656 msr_bitmap[word + (0x800 / sizeof(long))] = ~0; 657 } 658 } 659 660 #define BUILD_NVMX_MSR_INTERCEPT_HELPER(rw) \ 661 static inline \ 662 void nested_vmx_set_msr_##rw##_intercept(struct vcpu_vmx *vmx, \ 663 unsigned long *msr_bitmap_l1, \ 664 unsigned long *msr_bitmap_l0, u32 msr) \ 665 { \ 666 if (vmx_test_msr_bitmap_##rw(vmx->vmcs01.msr_bitmap, msr) || \ 667 vmx_test_msr_bitmap_##rw(msr_bitmap_l1, msr)) \ 668 vmx_set_msr_bitmap_##rw(msr_bitmap_l0, msr); \ 669 else \ 670 vmx_clear_msr_bitmap_##rw(msr_bitmap_l0, msr); \ 671 } 672 BUILD_NVMX_MSR_INTERCEPT_HELPER(read) 673 BUILD_NVMX_MSR_INTERCEPT_HELPER(write) 674 675 static inline void nested_vmx_set_intercept_for_msr(struct vcpu_vmx *vmx, 676 unsigned long *msr_bitmap_l1, 677 unsigned long *msr_bitmap_l0, 678 u32 msr, int types) 679 { 680 if (types & MSR_TYPE_R) 681 nested_vmx_set_msr_read_intercept(vmx, msr_bitmap_l1, 682 msr_bitmap_l0, msr); 683 if (types & MSR_TYPE_W) 684 nested_vmx_set_msr_write_intercept(vmx, msr_bitmap_l1, 685 msr_bitmap_l0, msr); 686 } 687 688 #define nested_vmx_merge_msr_bitmaps(msr, type) \ 689 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, \ 690 msr_bitmap_l0, msr, type) 691 692 #define nested_vmx_merge_msr_bitmaps_read(msr) \ 693 nested_vmx_merge_msr_bitmaps(msr, MSR_TYPE_R) 694 695 #define nested_vmx_merge_msr_bitmaps_write(msr) \ 696 nested_vmx_merge_msr_bitmaps(msr, MSR_TYPE_W) 697 698 #define nested_vmx_merge_msr_bitmaps_rw(msr) \ 699 nested_vmx_merge_msr_bitmaps(msr, MSR_TYPE_RW) 700 701 static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu, 702 unsigned long *msr_bitmap_l1, 703 unsigned long *msr_bitmap_l0) 704 { 705 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 706 struct vcpu_vmx *vmx = to_vmx(vcpu); 707 int i; 708 709 /* 710 * Skip the merges if the vCPU doesn't have a mediated PMU MSR, i.e. if 711 * none of the MSRs can possibly be passed through to L1. 712 */ 713 if (!kvm_vcpu_has_mediated_pmu(vcpu)) 714 return; 715 716 for (i = 0; i < pmu->nr_arch_gp_counters; i++) { 717 nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PERFCTR0 + i); 718 nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PMC0 + i); 719 } 720 721 for (i = 0; i < pmu->nr_arch_fixed_counters; i++) 722 nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_FIXED_CTR0 + i); 723 724 nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL); 725 nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_STATUS); 726 nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_OVF_CTRL); 727 } 728 729 /* 730 * Merge L0's and L1's MSR bitmap, return false to indicate that 731 * we do not use the hardware. 732 */ 733 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, 734 struct vmcs12 *vmcs12) 735 { 736 struct vcpu_vmx *vmx = to_vmx(vcpu); 737 int msr; 738 unsigned long *msr_bitmap_l1; 739 unsigned long *msr_bitmap_l0 = vmx->nested.vmcs02.msr_bitmap; 740 struct kvm_host_map map; 741 742 /* Nothing to do if the MSR bitmap is not in use. */ 743 if (!cpu_has_vmx_msr_bitmap() || 744 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) 745 return false; 746 747 /* 748 * MSR bitmap update can be skipped when: 749 * - MSR bitmap for L1 hasn't changed. 750 * - Nested hypervisor (L1) is attempting to launch the same L2 as 751 * before. 752 * - Nested hypervisor (L1) has enabled 'Enlightened MSR Bitmap' feature 753 * and tells KVM (L0) there were no changes in MSR bitmap for L2. 754 */ 755 if (!vmx->nested.force_msr_bitmap_recalc) { 756 struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx); 757 758 if (evmcs && evmcs->hv_enlightenments_control.msr_bitmap && 759 evmcs->hv_clean_fields & HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP) 760 return true; 761 } 762 763 if (kvm_vcpu_map_readonly(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), &map)) 764 return false; 765 766 msr_bitmap_l1 = (unsigned long *)map.hva; 767 768 /* 769 * To keep the control flow simple, pay eight 8-byte writes (sixteen 770 * 4-byte writes on 32-bit systems) up front to enable intercepts for 771 * the x2APIC MSR range and selectively toggle those relevant to L2. 772 */ 773 enable_x2apic_msr_intercepts(msr_bitmap_l0); 774 775 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { 776 if (nested_cpu_has_apic_reg_virt(vmcs12)) { 777 /* 778 * L0 need not intercept reads for MSRs between 0x800 779 * and 0x8ff, it just lets the processor take the value 780 * from the virtual-APIC page; take those 256 bits 781 * directly from the L1 bitmap. 782 */ 783 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { 784 unsigned word = msr / BITS_PER_LONG; 785 786 msr_bitmap_l0[word] = msr_bitmap_l1[word]; 787 } 788 } 789 790 nested_vmx_disable_intercept_for_x2apic_msr( 791 msr_bitmap_l1, msr_bitmap_l0, 792 X2APIC_MSR(APIC_TASKPRI), 793 MSR_TYPE_R | MSR_TYPE_W); 794 795 if (nested_cpu_has_vid(vmcs12)) { 796 nested_vmx_disable_intercept_for_x2apic_msr( 797 msr_bitmap_l1, msr_bitmap_l0, 798 X2APIC_MSR(APIC_EOI), 799 MSR_TYPE_W); 800 nested_vmx_disable_intercept_for_x2apic_msr( 801 msr_bitmap_l1, msr_bitmap_l0, 802 X2APIC_MSR(APIC_SELF_IPI), 803 MSR_TYPE_W); 804 } 805 } 806 807 /* 808 * Always check vmcs01's bitmap to honor userspace MSR filters and any 809 * other runtime changes to vmcs01's bitmap, e.g. dynamic pass-through. 810 */ 811 #ifdef CONFIG_X86_64 812 nested_vmx_merge_msr_bitmaps_rw(MSR_FS_BASE); 813 nested_vmx_merge_msr_bitmaps_rw(MSR_GS_BASE); 814 nested_vmx_merge_msr_bitmaps_rw(MSR_KERNEL_GS_BASE); 815 #endif 816 nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_SPEC_CTRL); 817 nested_vmx_merge_msr_bitmaps_write(MSR_IA32_PRED_CMD); 818 nested_vmx_merge_msr_bitmaps_write(MSR_IA32_FLUSH_CMD); 819 820 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, 821 MSR_IA32_APERF, MSR_TYPE_R); 822 823 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, 824 MSR_IA32_MPERF, MSR_TYPE_R); 825 826 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, 827 MSR_IA32_U_CET, MSR_TYPE_RW); 828 829 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, 830 MSR_IA32_S_CET, MSR_TYPE_RW); 831 832 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, 833 MSR_IA32_PL0_SSP, MSR_TYPE_RW); 834 835 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, 836 MSR_IA32_PL1_SSP, MSR_TYPE_RW); 837 838 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, 839 MSR_IA32_PL2_SSP, MSR_TYPE_RW); 840 841 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, 842 MSR_IA32_PL3_SSP, MSR_TYPE_RW); 843 844 nested_vmx_merge_pmu_msr_bitmaps(vcpu, msr_bitmap_l1, msr_bitmap_l0); 845 846 kvm_vcpu_unmap(vcpu, &map); 847 848 vmx->nested.force_msr_bitmap_recalc = false; 849 850 return true; 851 } 852 853 static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, 854 struct vmcs12 *vmcs12) 855 { 856 struct vcpu_vmx *vmx = to_vmx(vcpu); 857 struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache; 858 859 if (!nested_cpu_has_shadow_vmcs(vmcs12) || 860 vmcs12->vmcs_link_pointer == INVALID_GPA) 861 return; 862 863 if (ghc->gpa != vmcs12->vmcs_link_pointer && 864 kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, 865 vmcs12->vmcs_link_pointer, VMCS12_SIZE)) 866 return; 867 868 kvm_read_guest_cached(vcpu->kvm, ghc, get_shadow_vmcs12(vcpu), 869 VMCS12_SIZE); 870 } 871 872 static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, 873 struct vmcs12 *vmcs12) 874 { 875 struct vcpu_vmx *vmx = to_vmx(vcpu); 876 struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache; 877 878 if (!nested_cpu_has_shadow_vmcs(vmcs12) || 879 vmcs12->vmcs_link_pointer == INVALID_GPA) 880 return; 881 882 if (ghc->gpa != vmcs12->vmcs_link_pointer && 883 kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, 884 vmcs12->vmcs_link_pointer, VMCS12_SIZE)) 885 return; 886 887 kvm_write_guest_cached(vcpu->kvm, ghc, get_shadow_vmcs12(vcpu), 888 VMCS12_SIZE); 889 } 890 891 /* 892 * In nested virtualization, check if L1 has set 893 * VM_EXIT_ACK_INTR_ON_EXIT 894 */ 895 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) 896 { 897 return get_vmcs12(vcpu)->vm_exit_controls & 898 VM_EXIT_ACK_INTR_ON_EXIT; 899 } 900 901 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, 902 struct vmcs12 *vmcs12) 903 { 904 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && 905 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr))) 906 return -EINVAL; 907 else 908 return 0; 909 } 910 911 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, 912 struct vmcs12 *vmcs12) 913 { 914 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && 915 !nested_cpu_has_apic_reg_virt(vmcs12) && 916 !nested_cpu_has_vid(vmcs12) && 917 !nested_cpu_has_posted_intr(vmcs12)) 918 return 0; 919 920 /* 921 * If virtualize x2apic mode is enabled, 922 * virtualize apic access must be disabled. 923 */ 924 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) && 925 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))) 926 return -EINVAL; 927 928 /* 929 * If virtual interrupt delivery is enabled, 930 * we must exit on external interrupts. 931 */ 932 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu))) 933 return -EINVAL; 934 935 /* 936 * bits 15:8 should be zero in posted_intr_nv, 937 * the descriptor address has been already checked 938 * in nested_get_vmcs12_pages. 939 * 940 * bits 5:0 of posted_intr_desc_addr should be zero. 941 */ 942 if (nested_cpu_has_posted_intr(vmcs12) && 943 (CC(!nested_cpu_has_vid(vmcs12)) || 944 CC(!nested_exit_intr_ack_set(vcpu)) || 945 CC((vmcs12->posted_intr_nv & 0xff00)) || 946 CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64)))) 947 return -EINVAL; 948 949 /* tpr shadow is needed by all apicv features. */ 950 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))) 951 return -EINVAL; 952 953 return 0; 954 } 955 956 static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu) 957 { 958 struct vcpu_vmx *vmx = to_vmx(vcpu); 959 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, 960 vmx->nested.msrs.misc_high); 961 962 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER; 963 } 964 965 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, 966 u32 count, u64 addr) 967 { 968 if (count == 0) 969 return 0; 970 971 /* 972 * Exceeding the limit results in architecturally _undefined_ behavior, 973 * i.e. KVM is allowed to do literally anything in response to a bad 974 * limit. Immediately generate a consistency check so that code that 975 * consumes the count doesn't need to worry about extreme edge cases. 976 */ 977 if (count > nested_vmx_max_atomic_switch_msrs(vcpu)) 978 return -EINVAL; 979 980 if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) || 981 !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1))) 982 return -EINVAL; 983 984 return 0; 985 } 986 987 static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, 988 struct vmcs12 *vmcs12) 989 { 990 if (CC(nested_vmx_check_msr_switch(vcpu, 991 vmcs12->vm_exit_msr_load_count, 992 vmcs12->vm_exit_msr_load_addr)) || 993 CC(nested_vmx_check_msr_switch(vcpu, 994 vmcs12->vm_exit_msr_store_count, 995 vmcs12->vm_exit_msr_store_addr))) 996 return -EINVAL; 997 998 return 0; 999 } 1000 1001 static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu, 1002 struct vmcs12 *vmcs12) 1003 { 1004 if (CC(nested_vmx_check_msr_switch(vcpu, 1005 vmcs12->vm_entry_msr_load_count, 1006 vmcs12->vm_entry_msr_load_addr))) 1007 return -EINVAL; 1008 1009 return 0; 1010 } 1011 1012 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, 1013 struct vmcs12 *vmcs12) 1014 { 1015 if (!nested_cpu_has_pml(vmcs12)) 1016 return 0; 1017 1018 if (CC(!nested_cpu_has_ept(vmcs12)) || 1019 CC(!page_address_valid(vcpu, vmcs12->pml_address))) 1020 return -EINVAL; 1021 1022 return 0; 1023 } 1024 1025 static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, 1026 struct vmcs12 *vmcs12) 1027 { 1028 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && 1029 !nested_cpu_has_ept(vmcs12))) 1030 return -EINVAL; 1031 return 0; 1032 } 1033 1034 static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, 1035 struct vmcs12 *vmcs12) 1036 { 1037 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && 1038 !nested_cpu_has_ept(vmcs12))) 1039 return -EINVAL; 1040 return 0; 1041 } 1042 1043 static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, 1044 struct vmcs12 *vmcs12) 1045 { 1046 if (!nested_cpu_has_shadow_vmcs(vmcs12)) 1047 return 0; 1048 1049 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) || 1050 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap))) 1051 return -EINVAL; 1052 1053 return 0; 1054 } 1055 1056 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, 1057 struct vmx_msr_entry *e) 1058 { 1059 /* x2APIC MSR accesses are not allowed */ 1060 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)) 1061 return -EINVAL; 1062 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */ 1063 CC(e->index == MSR_IA32_UCODE_REV)) 1064 return -EINVAL; 1065 if (CC(e->reserved != 0)) 1066 return -EINVAL; 1067 return 0; 1068 } 1069 1070 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, 1071 struct vmx_msr_entry *e) 1072 { 1073 if (CC(e->index == MSR_FS_BASE) || 1074 CC(e->index == MSR_GS_BASE) || 1075 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */ 1076 nested_vmx_msr_check_common(vcpu, e)) 1077 return -EINVAL; 1078 return 0; 1079 } 1080 1081 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, 1082 struct vmx_msr_entry *e) 1083 { 1084 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */ 1085 nested_vmx_msr_check_common(vcpu, e)) 1086 return -EINVAL; 1087 return 0; 1088 } 1089 1090 /* 1091 * Load guest's/host's msr at nested entry/exit. 1092 * return 0 for success, entry index for failure. 1093 * 1094 * One of the failure modes for MSR load/store is when a list exceeds the 1095 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch 1096 * as possible, process all valid entries before failing rather than precheck 1097 * for a capacity violation. 1098 */ 1099 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) 1100 { 1101 u32 i; 1102 struct vmx_msr_entry e; 1103 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); 1104 1105 for (i = 0; i < count; i++) { 1106 if (WARN_ON_ONCE(i >= max_msr_list_size)) 1107 goto fail; 1108 1109 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), 1110 &e, sizeof(e))) { 1111 pr_debug_ratelimited( 1112 "%s cannot read MSR entry (%u, 0x%08llx)\n", 1113 __func__, i, gpa + i * sizeof(e)); 1114 goto fail; 1115 } 1116 if (nested_vmx_load_msr_check(vcpu, &e)) { 1117 pr_debug_ratelimited( 1118 "%s check failed (%u, 0x%x, 0x%x)\n", 1119 __func__, i, e.index, e.reserved); 1120 goto fail; 1121 } 1122 if (kvm_emulate_msr_write(vcpu, e.index, e.value)) { 1123 pr_debug_ratelimited( 1124 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", 1125 __func__, i, e.index, e.value); 1126 goto fail; 1127 } 1128 } 1129 return 0; 1130 fail: 1131 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */ 1132 return i + 1; 1133 } 1134 1135 static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu, 1136 u32 msr_index, 1137 u64 *data) 1138 { 1139 struct vcpu_vmx *vmx = to_vmx(vcpu); 1140 1141 /* 1142 * If the L0 hypervisor stored a more accurate value for the TSC that 1143 * does not include the time taken for emulation of the L2->L1 1144 * VM-exit in L0, use the more accurate value. 1145 */ 1146 if (msr_index == MSR_IA32_TSC && vmx->nested.tsc_autostore_slot >= 0) { 1147 int slot = vmx->nested.tsc_autostore_slot; 1148 u64 host_tsc = vmx->msr_autostore.val[slot].value; 1149 1150 *data = kvm_read_l1_tsc(vcpu, host_tsc); 1151 return true; 1152 } 1153 1154 if (kvm_emulate_msr_read(vcpu, msr_index, data)) { 1155 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__, 1156 msr_index); 1157 return false; 1158 } 1159 return true; 1160 } 1161 1162 static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, 1163 struct vmx_msr_entry *e) 1164 { 1165 if (kvm_vcpu_read_guest(vcpu, 1166 gpa + i * sizeof(*e), 1167 e, 2 * sizeof(u32))) { 1168 pr_debug_ratelimited( 1169 "%s cannot read MSR entry (%u, 0x%08llx)\n", 1170 __func__, i, gpa + i * sizeof(*e)); 1171 return false; 1172 } 1173 if (nested_vmx_store_msr_check(vcpu, e)) { 1174 pr_debug_ratelimited( 1175 "%s check failed (%u, 0x%x, 0x%x)\n", 1176 __func__, i, e->index, e->reserved); 1177 return false; 1178 } 1179 return true; 1180 } 1181 1182 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) 1183 { 1184 u64 data; 1185 u32 i; 1186 struct vmx_msr_entry e; 1187 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); 1188 1189 for (i = 0; i < count; i++) { 1190 if (WARN_ON_ONCE(i >= max_msr_list_size)) 1191 return -EINVAL; 1192 1193 if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) 1194 return -EINVAL; 1195 1196 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data)) 1197 return -EINVAL; 1198 1199 if (kvm_vcpu_write_guest(vcpu, 1200 gpa + i * sizeof(e) + 1201 offsetof(struct vmx_msr_entry, value), 1202 &data, sizeof(data))) { 1203 pr_debug_ratelimited( 1204 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", 1205 __func__, i, e.index, data); 1206 return -EINVAL; 1207 } 1208 } 1209 return 0; 1210 } 1211 1212 static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index) 1213 { 1214 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 1215 u32 count = vmcs12->vm_exit_msr_store_count; 1216 u64 gpa = vmcs12->vm_exit_msr_store_addr; 1217 struct vmx_msr_entry e; 1218 u32 i; 1219 1220 for (i = 0; i < count; i++) { 1221 if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) 1222 return false; 1223 1224 if (e.index == msr_index) 1225 return true; 1226 } 1227 return false; 1228 } 1229 1230 /* 1231 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are 1232 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected 1233 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to 1234 * @entry_failure_code. 1235 */ 1236 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, 1237 bool nested_ept, bool reload_pdptrs, 1238 enum vm_entry_failure_code *entry_failure_code) 1239 { 1240 if (CC(!kvm_vcpu_is_legal_cr3(vcpu, cr3))) { 1241 *entry_failure_code = ENTRY_FAIL_DEFAULT; 1242 return -EINVAL; 1243 } 1244 1245 /* 1246 * If PAE paging and EPT are both on, CR3 is not used by the CPU and 1247 * must not be dereferenced. 1248 */ 1249 if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) && 1250 CC(!load_pdptrs(vcpu, cr3))) { 1251 *entry_failure_code = ENTRY_FAIL_PDPTE; 1252 return -EINVAL; 1253 } 1254 1255 vcpu->arch.cr3 = cr3; 1256 kvm_register_mark_dirty(vcpu, VCPU_REG_CR3); 1257 1258 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */ 1259 kvm_init_mmu(vcpu); 1260 1261 if (!nested_ept) 1262 kvm_mmu_new_pgd(vcpu, cr3); 1263 1264 return 0; 1265 } 1266 1267 /* 1268 * Returns if KVM is able to config CPU to tag TLB entries 1269 * populated by L2 differently than TLB entries populated 1270 * by L1. 1271 * 1272 * If L0 uses EPT, L1 and L2 run with different EPTP because 1273 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries 1274 * are tagged with different EPTP. 1275 * 1276 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged 1277 * with different VPID (L1 entries are tagged with vmx->vpid 1278 * while L2 entries are tagged with vmx->nested.vpid02). 1279 */ 1280 static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) 1281 { 1282 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 1283 1284 return enable_ept || 1285 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); 1286 } 1287 1288 static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu, 1289 struct vmcs12 *vmcs12, 1290 bool is_vmenter) 1291 { 1292 struct vcpu_vmx *vmx = to_vmx(vcpu); 1293 1294 /* Handle pending Hyper-V TLB flush requests */ 1295 kvm_hv_nested_transtion_tlb_flush(vcpu, enable_ept); 1296 1297 /* 1298 * If VPID is disabled, then guest TLB accesses use VPID=0, i.e. the 1299 * same VPID as the host, and so architecturally, linear and combined 1300 * mappings for VPID=0 must be flushed at VM-Enter and VM-Exit. KVM 1301 * emulates L2 sharing L1's VPID=0 by using vpid01 while running L2, 1302 * and so KVM must also emulate TLB flush of VPID=0, i.e. vpid01. This 1303 * is required if VPID is disabled in KVM, as a TLB flush (there are no 1304 * VPIDs) still occurs from L1's perspective, and KVM may need to 1305 * synchronize the MMU in response to the guest TLB flush. 1306 * 1307 * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use. 1308 * EPT is a special snowflake, as guest-physical mappings aren't 1309 * flushed on VPID invalidations, including VM-Enter or VM-Exit with 1310 * VPID disabled. As a result, KVM _never_ needs to sync nEPT 1311 * entries on VM-Enter because L1 can't rely on VM-Enter to flush 1312 * those mappings. 1313 */ 1314 if (!nested_cpu_has_vpid(vmcs12)) { 1315 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); 1316 return; 1317 } 1318 1319 /* L2 should never have a VPID if VPID is disabled. */ 1320 WARN_ON(!enable_vpid); 1321 1322 /* 1323 * VPID is enabled and in use by vmcs12. If vpid12 is changing, then 1324 * emulate a guest TLB flush as KVM does not track vpid12 history nor 1325 * is the VPID incorporated into the MMU context. I.e. KVM must assume 1326 * that the new vpid12 has never been used and thus represents a new 1327 * guest ASID that cannot have entries in the TLB. 1328 */ 1329 if (is_vmenter && vmcs12->virtual_processor_id != vmx->nested.last_vpid) { 1330 vmx->nested.last_vpid = vmcs12->virtual_processor_id; 1331 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); 1332 return; 1333 } 1334 1335 /* 1336 * If VPID is enabled, used by vmc12, and vpid12 is not changing but 1337 * does not have a unique TLB tag (ASID), i.e. EPT is disabled and 1338 * KVM was unable to allocate a VPID for L2, flush the current context 1339 * as the effective ASID is common to both L1 and L2. 1340 */ 1341 if (!nested_has_guest_tlb_tag(vcpu)) 1342 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); 1343 } 1344 1345 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) 1346 { 1347 superset &= mask; 1348 subset &= mask; 1349 1350 return (superset | subset) == superset; 1351 } 1352 1353 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) 1354 { 1355 const u64 feature_bits = VMX_BASIC_DUAL_MONITOR_TREATMENT | 1356 VMX_BASIC_INOUT | 1357 VMX_BASIC_TRUE_CTLS | 1358 VMX_BASIC_NO_HW_ERROR_CODE_CC; 1359 1360 const u64 reserved_bits = GENMASK_ULL(63, 57) | 1361 GENMASK_ULL(47, 45) | 1362 BIT_ULL(31); 1363 1364 u64 vmx_basic = vmcs_config.nested.basic; 1365 1366 BUILD_BUG_ON(feature_bits & reserved_bits); 1367 1368 /* 1369 * Except for 32BIT_PHYS_ADDR_ONLY, which is an anti-feature bit (has 1370 * inverted polarity), the incoming value must not set feature bits or 1371 * reserved bits that aren't allowed/supported by KVM. Fields, i.e. 1372 * multi-bit values, are explicitly checked below. 1373 */ 1374 if (!is_bitwise_subset(vmx_basic, data, feature_bits | reserved_bits)) 1375 return -EINVAL; 1376 1377 /* 1378 * KVM does not emulate a version of VMX that constrains physical 1379 * addresses of VMX structures (e.g. VMCS) to 32-bits. 1380 */ 1381 if (data & VMX_BASIC_32BIT_PHYS_ADDR_ONLY) 1382 return -EINVAL; 1383 1384 if (vmx_basic_vmcs_revision_id(vmx_basic) != 1385 vmx_basic_vmcs_revision_id(data)) 1386 return -EINVAL; 1387 1388 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) 1389 return -EINVAL; 1390 1391 vmx->nested.msrs.basic = data; 1392 return 0; 1393 } 1394 1395 static void vmx_get_control_msr(struct nested_vmx_msrs *msrs, u32 msr_index, 1396 u32 **low, u32 **high) 1397 { 1398 switch (msr_index) { 1399 case MSR_IA32_VMX_TRUE_PINBASED_CTLS: 1400 *low = &msrs->pinbased_ctls_low; 1401 *high = &msrs->pinbased_ctls_high; 1402 break; 1403 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: 1404 *low = &msrs->procbased_ctls_low; 1405 *high = &msrs->procbased_ctls_high; 1406 break; 1407 case MSR_IA32_VMX_TRUE_EXIT_CTLS: 1408 *low = &msrs->exit_ctls_low; 1409 *high = &msrs->exit_ctls_high; 1410 break; 1411 case MSR_IA32_VMX_TRUE_ENTRY_CTLS: 1412 *low = &msrs->entry_ctls_low; 1413 *high = &msrs->entry_ctls_high; 1414 break; 1415 case MSR_IA32_VMX_PROCBASED_CTLS2: 1416 *low = &msrs->secondary_ctls_low; 1417 *high = &msrs->secondary_ctls_high; 1418 break; 1419 default: 1420 BUG(); 1421 } 1422 } 1423 1424 static int 1425 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) 1426 { 1427 u32 *lowp, *highp; 1428 u64 supported; 1429 1430 vmx_get_control_msr(&vmcs_config.nested, msr_index, &lowp, &highp); 1431 1432 supported = vmx_control_msr(*lowp, *highp); 1433 1434 /* Check must-be-1 bits are still 1. */ 1435 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) 1436 return -EINVAL; 1437 1438 /* Check must-be-0 bits are still 0. */ 1439 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) 1440 return -EINVAL; 1441 1442 vmx_get_control_msr(&vmx->nested.msrs, msr_index, &lowp, &highp); 1443 *lowp = data; 1444 *highp = data >> 32; 1445 return 0; 1446 } 1447 1448 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) 1449 { 1450 const u64 feature_bits = VMX_MISC_SAVE_EFER_LMA | 1451 VMX_MISC_ACTIVITY_HLT | 1452 VMX_MISC_ACTIVITY_SHUTDOWN | 1453 VMX_MISC_ACTIVITY_WAIT_SIPI | 1454 VMX_MISC_INTEL_PT | 1455 VMX_MISC_RDMSR_IN_SMM | 1456 VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | 1457 VMX_MISC_VMXOFF_BLOCK_SMI | 1458 VMX_MISC_ZERO_LEN_INS; 1459 1460 const u64 reserved_bits = BIT_ULL(31) | GENMASK_ULL(13, 9); 1461 1462 u64 vmx_misc = vmx_control_msr(vmcs_config.nested.misc_low, 1463 vmcs_config.nested.misc_high); 1464 1465 BUILD_BUG_ON(feature_bits & reserved_bits); 1466 1467 /* 1468 * The incoming value must not set feature bits or reserved bits that 1469 * aren't allowed/supported by KVM. Fields, i.e. multi-bit values, are 1470 * explicitly checked below. 1471 */ 1472 if (!is_bitwise_subset(vmx_misc, data, feature_bits | reserved_bits)) 1473 return -EINVAL; 1474 1475 if ((vmx->nested.msrs.pinbased_ctls_high & 1476 PIN_BASED_VMX_PREEMPTION_TIMER) && 1477 vmx_misc_preemption_timer_rate(data) != 1478 vmx_misc_preemption_timer_rate(vmx_misc)) 1479 return -EINVAL; 1480 1481 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) 1482 return -EINVAL; 1483 1484 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) 1485 return -EINVAL; 1486 1487 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) 1488 return -EINVAL; 1489 1490 vmx->nested.msrs.misc_low = data; 1491 vmx->nested.msrs.misc_high = data >> 32; 1492 1493 return 0; 1494 } 1495 1496 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) 1497 { 1498 u64 vmx_ept_vpid_cap = vmx_control_msr(vmcs_config.nested.ept_caps, 1499 vmcs_config.nested.vpid_caps); 1500 1501 /* Every bit is either reserved or a feature bit. */ 1502 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) 1503 return -EINVAL; 1504 1505 vmx->nested.msrs.ept_caps = data; 1506 vmx->nested.msrs.vpid_caps = data >> 32; 1507 return 0; 1508 } 1509 1510 static u64 *vmx_get_fixed0_msr(struct nested_vmx_msrs *msrs, u32 msr_index) 1511 { 1512 switch (msr_index) { 1513 case MSR_IA32_VMX_CR0_FIXED0: 1514 return &msrs->cr0_fixed0; 1515 case MSR_IA32_VMX_CR4_FIXED0: 1516 return &msrs->cr4_fixed0; 1517 default: 1518 BUG(); 1519 } 1520 } 1521 1522 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) 1523 { 1524 const u64 *msr = vmx_get_fixed0_msr(&vmcs_config.nested, msr_index); 1525 1526 /* 1527 * 1 bits (which indicates bits which "must-be-1" during VMX operation) 1528 * must be 1 in the restored value. 1529 */ 1530 if (!is_bitwise_subset(data, *msr, -1ULL)) 1531 return -EINVAL; 1532 1533 *vmx_get_fixed0_msr(&vmx->nested.msrs, msr_index) = data; 1534 return 0; 1535 } 1536 1537 /* 1538 * Called when userspace is restoring VMX MSRs. 1539 * 1540 * Returns 0 on success, non-0 otherwise. 1541 */ 1542 int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) 1543 { 1544 struct vcpu_vmx *vmx = to_vmx(vcpu); 1545 1546 /* 1547 * Don't allow changes to the VMX capability MSRs while the vCPU 1548 * is in VMX operation. 1549 */ 1550 if (vmx->nested.vmxon) 1551 return -EBUSY; 1552 1553 switch (msr_index) { 1554 case MSR_IA32_VMX_BASIC: 1555 return vmx_restore_vmx_basic(vmx, data); 1556 case MSR_IA32_VMX_PINBASED_CTLS: 1557 case MSR_IA32_VMX_PROCBASED_CTLS: 1558 case MSR_IA32_VMX_EXIT_CTLS: 1559 case MSR_IA32_VMX_ENTRY_CTLS: 1560 /* 1561 * The "non-true" VMX capability MSRs are generated from the 1562 * "true" MSRs, so we do not support restoring them directly. 1563 * 1564 * If userspace wants to emulate VMX_BASIC[55]=0, userspace 1565 * should restore the "true" MSRs with the must-be-1 bits 1566 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND 1567 * DEFAULT SETTINGS". 1568 */ 1569 return -EINVAL; 1570 case MSR_IA32_VMX_TRUE_PINBASED_CTLS: 1571 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: 1572 case MSR_IA32_VMX_TRUE_EXIT_CTLS: 1573 case MSR_IA32_VMX_TRUE_ENTRY_CTLS: 1574 case MSR_IA32_VMX_PROCBASED_CTLS2: 1575 return vmx_restore_control_msr(vmx, msr_index, data); 1576 case MSR_IA32_VMX_MISC: 1577 return vmx_restore_vmx_misc(vmx, data); 1578 case MSR_IA32_VMX_CR0_FIXED0: 1579 case MSR_IA32_VMX_CR4_FIXED0: 1580 return vmx_restore_fixed0_msr(vmx, msr_index, data); 1581 case MSR_IA32_VMX_CR0_FIXED1: 1582 case MSR_IA32_VMX_CR4_FIXED1: 1583 /* 1584 * These MSRs are generated based on the vCPU's CPUID, so we 1585 * do not support restoring them directly. 1586 */ 1587 return -EINVAL; 1588 case MSR_IA32_VMX_EPT_VPID_CAP: 1589 return vmx_restore_vmx_ept_vpid_cap(vmx, data); 1590 case MSR_IA32_VMX_VMCS_ENUM: 1591 vmx->nested.msrs.vmcs_enum = data; 1592 return 0; 1593 case MSR_IA32_VMX_VMFUNC: 1594 if (data & ~vmcs_config.nested.vmfunc_controls) 1595 return -EINVAL; 1596 vmx->nested.msrs.vmfunc_controls = data; 1597 return 0; 1598 default: 1599 /* 1600 * The rest of the VMX capability MSRs do not support restore. 1601 */ 1602 return -EINVAL; 1603 } 1604 } 1605 1606 /* Returns 0 on success, non-0 otherwise. */ 1607 int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) 1608 { 1609 switch (msr_index) { 1610 case MSR_IA32_VMX_BASIC: 1611 *pdata = msrs->basic; 1612 break; 1613 case MSR_IA32_VMX_TRUE_PINBASED_CTLS: 1614 case MSR_IA32_VMX_PINBASED_CTLS: 1615 *pdata = vmx_control_msr( 1616 msrs->pinbased_ctls_low, 1617 msrs->pinbased_ctls_high); 1618 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) 1619 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; 1620 break; 1621 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: 1622 case MSR_IA32_VMX_PROCBASED_CTLS: 1623 *pdata = vmx_control_msr( 1624 msrs->procbased_ctls_low, 1625 msrs->procbased_ctls_high); 1626 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) 1627 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; 1628 break; 1629 case MSR_IA32_VMX_TRUE_EXIT_CTLS: 1630 case MSR_IA32_VMX_EXIT_CTLS: 1631 *pdata = vmx_control_msr( 1632 msrs->exit_ctls_low, 1633 msrs->exit_ctls_high); 1634 if (msr_index == MSR_IA32_VMX_EXIT_CTLS) 1635 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; 1636 break; 1637 case MSR_IA32_VMX_TRUE_ENTRY_CTLS: 1638 case MSR_IA32_VMX_ENTRY_CTLS: 1639 *pdata = vmx_control_msr( 1640 msrs->entry_ctls_low, 1641 msrs->entry_ctls_high); 1642 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) 1643 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; 1644 break; 1645 case MSR_IA32_VMX_MISC: 1646 *pdata = vmx_control_msr( 1647 msrs->misc_low, 1648 msrs->misc_high); 1649 break; 1650 case MSR_IA32_VMX_CR0_FIXED0: 1651 *pdata = msrs->cr0_fixed0; 1652 break; 1653 case MSR_IA32_VMX_CR0_FIXED1: 1654 *pdata = msrs->cr0_fixed1; 1655 break; 1656 case MSR_IA32_VMX_CR4_FIXED0: 1657 *pdata = msrs->cr4_fixed0; 1658 break; 1659 case MSR_IA32_VMX_CR4_FIXED1: 1660 *pdata = msrs->cr4_fixed1; 1661 break; 1662 case MSR_IA32_VMX_VMCS_ENUM: 1663 *pdata = msrs->vmcs_enum; 1664 break; 1665 case MSR_IA32_VMX_PROCBASED_CTLS2: 1666 *pdata = vmx_control_msr( 1667 msrs->secondary_ctls_low, 1668 msrs->secondary_ctls_high); 1669 break; 1670 case MSR_IA32_VMX_EPT_VPID_CAP: 1671 *pdata = msrs->ept_caps | 1672 ((u64)msrs->vpid_caps << 32); 1673 break; 1674 case MSR_IA32_VMX_VMFUNC: 1675 *pdata = msrs->vmfunc_controls; 1676 break; 1677 default: 1678 return 1; 1679 } 1680 1681 return 0; 1682 } 1683 1684 /* 1685 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have 1686 * been modified by the L1 guest. Note, "writable" in this context means 1687 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of 1688 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only" 1689 * VM-exit information fields (which are actually writable if the vCPU is 1690 * configured to support "VMWRITE to any supported field in the VMCS"). 1691 */ 1692 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) 1693 { 1694 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; 1695 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); 1696 struct shadow_vmcs_field field; 1697 unsigned long val; 1698 int i; 1699 1700 if (WARN_ON(!shadow_vmcs)) 1701 return; 1702 1703 preempt_disable(); 1704 1705 vmcs_load(shadow_vmcs); 1706 1707 for (i = 0; i < max_shadow_read_write_fields; i++) { 1708 field = shadow_read_write_fields[i]; 1709 val = __vmcs_readl(field.encoding); 1710 vmcs12_write_any(vmcs12, field.encoding, field.offset, val); 1711 } 1712 1713 vmcs_clear(shadow_vmcs); 1714 vmcs_load(vmx->loaded_vmcs->vmcs); 1715 1716 preempt_enable(); 1717 } 1718 1719 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) 1720 { 1721 const struct shadow_vmcs_field *fields[] = { 1722 shadow_read_write_fields, 1723 shadow_read_only_fields 1724 }; 1725 const int max_fields[] = { 1726 max_shadow_read_write_fields, 1727 max_shadow_read_only_fields 1728 }; 1729 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; 1730 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); 1731 struct shadow_vmcs_field field; 1732 unsigned long val; 1733 int i, q; 1734 1735 if (WARN_ON(!shadow_vmcs)) 1736 return; 1737 1738 vmcs_load(shadow_vmcs); 1739 1740 for (q = 0; q < ARRAY_SIZE(fields); q++) { 1741 for (i = 0; i < max_fields[q]; i++) { 1742 field = fields[q][i]; 1743 val = vmcs12_read_any(vmcs12, field.encoding, 1744 field.offset); 1745 __vmcs_writel(field.encoding, val); 1746 } 1747 } 1748 1749 vmcs_clear(shadow_vmcs); 1750 vmcs_load(vmx->loaded_vmcs->vmcs); 1751 } 1752 1753 static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields) 1754 { 1755 #ifdef CONFIG_KVM_HYPERV 1756 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; 1757 struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx); 1758 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(&vmx->vcpu); 1759 1760 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ 1761 vmcs12->tpr_threshold = evmcs->tpr_threshold; 1762 vmcs12->guest_rip = evmcs->guest_rip; 1763 1764 if (unlikely(!(hv_clean_fields & 1765 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ENLIGHTENMENTSCONTROL))) { 1766 hv_vcpu->nested.pa_page_gpa = evmcs->partition_assist_page; 1767 hv_vcpu->nested.vm_id = evmcs->hv_vm_id; 1768 hv_vcpu->nested.vp_id = evmcs->hv_vp_id; 1769 } 1770 1771 if (unlikely(!(hv_clean_fields & 1772 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { 1773 vmcs12->guest_rsp = evmcs->guest_rsp; 1774 vmcs12->guest_rflags = evmcs->guest_rflags; 1775 vmcs12->guest_interruptibility_info = 1776 evmcs->guest_interruptibility_info; 1777 /* 1778 * Not present in struct vmcs12: 1779 * vmcs12->guest_ssp = evmcs->guest_ssp; 1780 */ 1781 } 1782 1783 if (unlikely(!(hv_clean_fields & 1784 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { 1785 vmcs12->cpu_based_vm_exec_control = 1786 evmcs->cpu_based_vm_exec_control; 1787 } 1788 1789 if (unlikely(!(hv_clean_fields & 1790 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) { 1791 vmcs12->exception_bitmap = evmcs->exception_bitmap; 1792 } 1793 1794 if (unlikely(!(hv_clean_fields & 1795 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { 1796 vmcs12->vm_entry_controls = evmcs->vm_entry_controls; 1797 } 1798 1799 if (unlikely(!(hv_clean_fields & 1800 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { 1801 vmcs12->vm_entry_intr_info_field = 1802 evmcs->vm_entry_intr_info_field; 1803 vmcs12->vm_entry_exception_error_code = 1804 evmcs->vm_entry_exception_error_code; 1805 vmcs12->vm_entry_instruction_len = 1806 evmcs->vm_entry_instruction_len; 1807 } 1808 1809 if (unlikely(!(hv_clean_fields & 1810 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { 1811 vmcs12->host_ia32_pat = evmcs->host_ia32_pat; 1812 vmcs12->host_ia32_efer = evmcs->host_ia32_efer; 1813 vmcs12->host_cr0 = evmcs->host_cr0; 1814 vmcs12->host_cr3 = evmcs->host_cr3; 1815 vmcs12->host_cr4 = evmcs->host_cr4; 1816 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; 1817 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; 1818 vmcs12->host_rip = evmcs->host_rip; 1819 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; 1820 vmcs12->host_es_selector = evmcs->host_es_selector; 1821 vmcs12->host_cs_selector = evmcs->host_cs_selector; 1822 vmcs12->host_ss_selector = evmcs->host_ss_selector; 1823 vmcs12->host_ds_selector = evmcs->host_ds_selector; 1824 vmcs12->host_fs_selector = evmcs->host_fs_selector; 1825 vmcs12->host_gs_selector = evmcs->host_gs_selector; 1826 vmcs12->host_tr_selector = evmcs->host_tr_selector; 1827 vmcs12->host_ia32_perf_global_ctrl = evmcs->host_ia32_perf_global_ctrl; 1828 /* 1829 * Not present in struct vmcs12: 1830 * vmcs12->host_ia32_s_cet = evmcs->host_ia32_s_cet; 1831 * vmcs12->host_ssp = evmcs->host_ssp; 1832 * vmcs12->host_ia32_int_ssp_table_addr = evmcs->host_ia32_int_ssp_table_addr; 1833 */ 1834 } 1835 1836 if (unlikely(!(hv_clean_fields & 1837 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) { 1838 vmcs12->pin_based_vm_exec_control = 1839 evmcs->pin_based_vm_exec_control; 1840 vmcs12->vm_exit_controls = evmcs->vm_exit_controls; 1841 vmcs12->secondary_vm_exec_control = 1842 evmcs->secondary_vm_exec_control; 1843 } 1844 1845 if (unlikely(!(hv_clean_fields & 1846 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) { 1847 vmcs12->io_bitmap_a = evmcs->io_bitmap_a; 1848 vmcs12->io_bitmap_b = evmcs->io_bitmap_b; 1849 } 1850 1851 if (unlikely(!(hv_clean_fields & 1852 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { 1853 vmcs12->msr_bitmap = evmcs->msr_bitmap; 1854 } 1855 1856 if (unlikely(!(hv_clean_fields & 1857 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { 1858 vmcs12->guest_es_base = evmcs->guest_es_base; 1859 vmcs12->guest_cs_base = evmcs->guest_cs_base; 1860 vmcs12->guest_ss_base = evmcs->guest_ss_base; 1861 vmcs12->guest_ds_base = evmcs->guest_ds_base; 1862 vmcs12->guest_fs_base = evmcs->guest_fs_base; 1863 vmcs12->guest_gs_base = evmcs->guest_gs_base; 1864 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; 1865 vmcs12->guest_tr_base = evmcs->guest_tr_base; 1866 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; 1867 vmcs12->guest_idtr_base = evmcs->guest_idtr_base; 1868 vmcs12->guest_es_limit = evmcs->guest_es_limit; 1869 vmcs12->guest_cs_limit = evmcs->guest_cs_limit; 1870 vmcs12->guest_ss_limit = evmcs->guest_ss_limit; 1871 vmcs12->guest_ds_limit = evmcs->guest_ds_limit; 1872 vmcs12->guest_fs_limit = evmcs->guest_fs_limit; 1873 vmcs12->guest_gs_limit = evmcs->guest_gs_limit; 1874 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; 1875 vmcs12->guest_tr_limit = evmcs->guest_tr_limit; 1876 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; 1877 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; 1878 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; 1879 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; 1880 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; 1881 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; 1882 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; 1883 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; 1884 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; 1885 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; 1886 vmcs12->guest_es_selector = evmcs->guest_es_selector; 1887 vmcs12->guest_cs_selector = evmcs->guest_cs_selector; 1888 vmcs12->guest_ss_selector = evmcs->guest_ss_selector; 1889 vmcs12->guest_ds_selector = evmcs->guest_ds_selector; 1890 vmcs12->guest_fs_selector = evmcs->guest_fs_selector; 1891 vmcs12->guest_gs_selector = evmcs->guest_gs_selector; 1892 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; 1893 vmcs12->guest_tr_selector = evmcs->guest_tr_selector; 1894 } 1895 1896 if (unlikely(!(hv_clean_fields & 1897 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { 1898 vmcs12->tsc_offset = evmcs->tsc_offset; 1899 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; 1900 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; 1901 vmcs12->encls_exiting_bitmap = evmcs->encls_exiting_bitmap; 1902 vmcs12->tsc_multiplier = evmcs->tsc_multiplier; 1903 } 1904 1905 if (unlikely(!(hv_clean_fields & 1906 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { 1907 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; 1908 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; 1909 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; 1910 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; 1911 vmcs12->guest_cr0 = evmcs->guest_cr0; 1912 vmcs12->guest_cr3 = evmcs->guest_cr3; 1913 vmcs12->guest_cr4 = evmcs->guest_cr4; 1914 vmcs12->guest_dr7 = evmcs->guest_dr7; 1915 } 1916 1917 if (unlikely(!(hv_clean_fields & 1918 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { 1919 vmcs12->host_fs_base = evmcs->host_fs_base; 1920 vmcs12->host_gs_base = evmcs->host_gs_base; 1921 vmcs12->host_tr_base = evmcs->host_tr_base; 1922 vmcs12->host_gdtr_base = evmcs->host_gdtr_base; 1923 vmcs12->host_idtr_base = evmcs->host_idtr_base; 1924 vmcs12->host_rsp = evmcs->host_rsp; 1925 } 1926 1927 if (unlikely(!(hv_clean_fields & 1928 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) { 1929 vmcs12->ept_pointer = evmcs->ept_pointer; 1930 vmcs12->virtual_processor_id = evmcs->virtual_processor_id; 1931 } 1932 1933 if (unlikely(!(hv_clean_fields & 1934 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { 1935 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; 1936 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; 1937 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; 1938 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; 1939 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; 1940 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; 1941 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; 1942 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; 1943 vmcs12->guest_pending_dbg_exceptions = 1944 evmcs->guest_pending_dbg_exceptions; 1945 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; 1946 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; 1947 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; 1948 vmcs12->guest_activity_state = evmcs->guest_activity_state; 1949 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; 1950 vmcs12->guest_ia32_perf_global_ctrl = evmcs->guest_ia32_perf_global_ctrl; 1951 /* 1952 * Not present in struct vmcs12: 1953 * vmcs12->guest_ia32_s_cet = evmcs->guest_ia32_s_cet; 1954 * vmcs12->guest_ia32_lbr_ctl = evmcs->guest_ia32_lbr_ctl; 1955 * vmcs12->guest_ia32_int_ssp_table_addr = evmcs->guest_ia32_int_ssp_table_addr; 1956 */ 1957 } 1958 1959 /* 1960 * Not used? 1961 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; 1962 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; 1963 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; 1964 * vmcs12->page_fault_error_code_mask = 1965 * evmcs->page_fault_error_code_mask; 1966 * vmcs12->page_fault_error_code_match = 1967 * evmcs->page_fault_error_code_match; 1968 * vmcs12->cr3_target_count = evmcs->cr3_target_count; 1969 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; 1970 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; 1971 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; 1972 */ 1973 1974 /* 1975 * Read only fields: 1976 * vmcs12->guest_physical_address = evmcs->guest_physical_address; 1977 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; 1978 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; 1979 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; 1980 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; 1981 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; 1982 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; 1983 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; 1984 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; 1985 * vmcs12->exit_qualification = evmcs->exit_qualification; 1986 * vmcs12->guest_linear_address = evmcs->guest_linear_address; 1987 * 1988 * Not present in struct vmcs12: 1989 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; 1990 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; 1991 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; 1992 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; 1993 */ 1994 1995 return; 1996 #else /* CONFIG_KVM_HYPERV */ 1997 KVM_BUG_ON(1, vmx->vcpu.kvm); 1998 #endif /* CONFIG_KVM_HYPERV */ 1999 } 2000 2001 static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) 2002 { 2003 #ifdef CONFIG_KVM_HYPERV 2004 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; 2005 struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx); 2006 2007 /* 2008 * Should not be changed by KVM: 2009 * 2010 * evmcs->host_es_selector = vmcs12->host_es_selector; 2011 * evmcs->host_cs_selector = vmcs12->host_cs_selector; 2012 * evmcs->host_ss_selector = vmcs12->host_ss_selector; 2013 * evmcs->host_ds_selector = vmcs12->host_ds_selector; 2014 * evmcs->host_fs_selector = vmcs12->host_fs_selector; 2015 * evmcs->host_gs_selector = vmcs12->host_gs_selector; 2016 * evmcs->host_tr_selector = vmcs12->host_tr_selector; 2017 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; 2018 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; 2019 * evmcs->host_cr0 = vmcs12->host_cr0; 2020 * evmcs->host_cr3 = vmcs12->host_cr3; 2021 * evmcs->host_cr4 = vmcs12->host_cr4; 2022 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; 2023 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; 2024 * evmcs->host_rip = vmcs12->host_rip; 2025 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; 2026 * evmcs->host_fs_base = vmcs12->host_fs_base; 2027 * evmcs->host_gs_base = vmcs12->host_gs_base; 2028 * evmcs->host_tr_base = vmcs12->host_tr_base; 2029 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; 2030 * evmcs->host_idtr_base = vmcs12->host_idtr_base; 2031 * evmcs->host_rsp = vmcs12->host_rsp; 2032 * sync_vmcs02_to_vmcs12() doesn't read these: 2033 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; 2034 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; 2035 * evmcs->msr_bitmap = vmcs12->msr_bitmap; 2036 * evmcs->ept_pointer = vmcs12->ept_pointer; 2037 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; 2038 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; 2039 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; 2040 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; 2041 * evmcs->tpr_threshold = vmcs12->tpr_threshold; 2042 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; 2043 * evmcs->exception_bitmap = vmcs12->exception_bitmap; 2044 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; 2045 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; 2046 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; 2047 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; 2048 * evmcs->page_fault_error_code_mask = 2049 * vmcs12->page_fault_error_code_mask; 2050 * evmcs->page_fault_error_code_match = 2051 * vmcs12->page_fault_error_code_match; 2052 * evmcs->cr3_target_count = vmcs12->cr3_target_count; 2053 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; 2054 * evmcs->tsc_offset = vmcs12->tsc_offset; 2055 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; 2056 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; 2057 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; 2058 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; 2059 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; 2060 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; 2061 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; 2062 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; 2063 * evmcs->guest_ia32_perf_global_ctrl = vmcs12->guest_ia32_perf_global_ctrl; 2064 * evmcs->host_ia32_perf_global_ctrl = vmcs12->host_ia32_perf_global_ctrl; 2065 * evmcs->encls_exiting_bitmap = vmcs12->encls_exiting_bitmap; 2066 * evmcs->tsc_multiplier = vmcs12->tsc_multiplier; 2067 * 2068 * Not present in struct vmcs12: 2069 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; 2070 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; 2071 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; 2072 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; 2073 * evmcs->host_ia32_s_cet = vmcs12->host_ia32_s_cet; 2074 * evmcs->host_ssp = vmcs12->host_ssp; 2075 * evmcs->host_ia32_int_ssp_table_addr = vmcs12->host_ia32_int_ssp_table_addr; 2076 * evmcs->guest_ia32_s_cet = vmcs12->guest_ia32_s_cet; 2077 * evmcs->guest_ia32_lbr_ctl = vmcs12->guest_ia32_lbr_ctl; 2078 * evmcs->guest_ia32_int_ssp_table_addr = vmcs12->guest_ia32_int_ssp_table_addr; 2079 * evmcs->guest_ssp = vmcs12->guest_ssp; 2080 */ 2081 2082 evmcs->guest_es_selector = vmcs12->guest_es_selector; 2083 evmcs->guest_cs_selector = vmcs12->guest_cs_selector; 2084 evmcs->guest_ss_selector = vmcs12->guest_ss_selector; 2085 evmcs->guest_ds_selector = vmcs12->guest_ds_selector; 2086 evmcs->guest_fs_selector = vmcs12->guest_fs_selector; 2087 evmcs->guest_gs_selector = vmcs12->guest_gs_selector; 2088 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; 2089 evmcs->guest_tr_selector = vmcs12->guest_tr_selector; 2090 2091 evmcs->guest_es_limit = vmcs12->guest_es_limit; 2092 evmcs->guest_cs_limit = vmcs12->guest_cs_limit; 2093 evmcs->guest_ss_limit = vmcs12->guest_ss_limit; 2094 evmcs->guest_ds_limit = vmcs12->guest_ds_limit; 2095 evmcs->guest_fs_limit = vmcs12->guest_fs_limit; 2096 evmcs->guest_gs_limit = vmcs12->guest_gs_limit; 2097 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; 2098 evmcs->guest_tr_limit = vmcs12->guest_tr_limit; 2099 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; 2100 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; 2101 2102 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; 2103 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; 2104 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; 2105 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; 2106 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; 2107 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; 2108 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; 2109 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; 2110 2111 evmcs->guest_es_base = vmcs12->guest_es_base; 2112 evmcs->guest_cs_base = vmcs12->guest_cs_base; 2113 evmcs->guest_ss_base = vmcs12->guest_ss_base; 2114 evmcs->guest_ds_base = vmcs12->guest_ds_base; 2115 evmcs->guest_fs_base = vmcs12->guest_fs_base; 2116 evmcs->guest_gs_base = vmcs12->guest_gs_base; 2117 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; 2118 evmcs->guest_tr_base = vmcs12->guest_tr_base; 2119 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; 2120 evmcs->guest_idtr_base = vmcs12->guest_idtr_base; 2121 2122 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; 2123 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; 2124 2125 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; 2126 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; 2127 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; 2128 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; 2129 2130 evmcs->guest_pending_dbg_exceptions = 2131 vmcs12->guest_pending_dbg_exceptions; 2132 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; 2133 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; 2134 2135 evmcs->guest_activity_state = vmcs12->guest_activity_state; 2136 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; 2137 2138 evmcs->guest_cr0 = vmcs12->guest_cr0; 2139 evmcs->guest_cr3 = vmcs12->guest_cr3; 2140 evmcs->guest_cr4 = vmcs12->guest_cr4; 2141 evmcs->guest_dr7 = vmcs12->guest_dr7; 2142 2143 evmcs->guest_physical_address = vmcs12->guest_physical_address; 2144 2145 evmcs->vm_instruction_error = vmcs12->vm_instruction_error; 2146 evmcs->vm_exit_reason = vmcs12->vm_exit_reason; 2147 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; 2148 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; 2149 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; 2150 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; 2151 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; 2152 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; 2153 2154 evmcs->exit_qualification = vmcs12->exit_qualification; 2155 2156 evmcs->guest_linear_address = vmcs12->guest_linear_address; 2157 evmcs->guest_rsp = vmcs12->guest_rsp; 2158 evmcs->guest_rflags = vmcs12->guest_rflags; 2159 2160 evmcs->guest_interruptibility_info = 2161 vmcs12->guest_interruptibility_info; 2162 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; 2163 evmcs->vm_entry_controls = vmcs12->vm_entry_controls; 2164 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; 2165 evmcs->vm_entry_exception_error_code = 2166 vmcs12->vm_entry_exception_error_code; 2167 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; 2168 2169 evmcs->guest_rip = vmcs12->guest_rip; 2170 2171 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; 2172 2173 return; 2174 #else /* CONFIG_KVM_HYPERV */ 2175 KVM_BUG_ON(1, vmx->vcpu.kvm); 2176 #endif /* CONFIG_KVM_HYPERV */ 2177 } 2178 2179 /* 2180 * This is an equivalent of the nested hypervisor executing the vmptrld 2181 * instruction. 2182 */ 2183 static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld( 2184 struct kvm_vcpu *vcpu, bool from_launch) 2185 { 2186 #ifdef CONFIG_KVM_HYPERV 2187 struct vcpu_vmx *vmx = to_vmx(vcpu); 2188 bool evmcs_gpa_changed = false; 2189 u64 evmcs_gpa; 2190 2191 if (likely(!guest_cpu_cap_has_evmcs(vcpu))) 2192 return EVMPTRLD_DISABLED; 2193 2194 evmcs_gpa = nested_get_evmptr(vcpu); 2195 if (!evmptr_is_valid(evmcs_gpa)) { 2196 nested_release_evmcs(vcpu); 2197 return EVMPTRLD_DISABLED; 2198 } 2199 2200 if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) { 2201 vmx->nested.current_vmptr = INVALID_GPA; 2202 2203 nested_release_evmcs(vcpu); 2204 2205 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa), 2206 &vmx->nested.hv_evmcs_map)) 2207 return EVMPTRLD_ERROR; 2208 2209 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva; 2210 2211 /* 2212 * Currently, KVM only supports eVMCS version 1 2213 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this 2214 * value to first u32 field of eVMCS which should specify eVMCS 2215 * VersionNumber. 2216 * 2217 * Guest should be aware of supported eVMCS versions by host by 2218 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is 2219 * expected to set this CPUID leaf according to the value 2220 * returned in vmcs_version from nested_enable_evmcs(). 2221 * 2222 * However, it turns out that Microsoft Hyper-V fails to comply 2223 * to their own invented interface: When Hyper-V use eVMCS, it 2224 * just sets first u32 field of eVMCS to revision_id specified 2225 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number 2226 * which is one of the supported versions specified in 2227 * CPUID.0x4000000A.EAX[0:15]. 2228 * 2229 * To overcome Hyper-V bug, we accept here either a supported 2230 * eVMCS version or VMCS12 revision_id as valid values for first 2231 * u32 field of eVMCS. 2232 */ 2233 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && 2234 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { 2235 nested_release_evmcs(vcpu); 2236 return EVMPTRLD_VMFAIL; 2237 } 2238 2239 vmx->nested.hv_evmcs_vmptr = evmcs_gpa; 2240 2241 evmcs_gpa_changed = true; 2242 /* 2243 * Unlike normal vmcs12, enlightened vmcs12 is not fully 2244 * reloaded from guest's memory (read only fields, fields not 2245 * present in struct hv_enlightened_vmcs, ...). Make sure there 2246 * are no leftovers. 2247 */ 2248 if (from_launch) { 2249 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 2250 memset(vmcs12, 0, sizeof(*vmcs12)); 2251 vmcs12->hdr.revision_id = VMCS12_REVISION; 2252 } 2253 2254 } 2255 2256 /* 2257 * Clean fields data can't be used on VMLAUNCH and when we switch 2258 * between different L2 guests as KVM keeps a single VMCS12 per L1. 2259 */ 2260 if (from_launch || evmcs_gpa_changed) { 2261 vmx->nested.hv_evmcs->hv_clean_fields &= 2262 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; 2263 2264 vmx->nested.force_msr_bitmap_recalc = true; 2265 } 2266 2267 return EVMPTRLD_SUCCEEDED; 2268 #else 2269 return EVMPTRLD_DISABLED; 2270 #endif 2271 } 2272 2273 void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu) 2274 { 2275 struct vcpu_vmx *vmx = to_vmx(vcpu); 2276 2277 if (nested_vmx_is_evmptr12_valid(vmx)) 2278 copy_vmcs12_to_enlightened(vmx); 2279 else 2280 copy_vmcs12_to_shadow(vmx); 2281 2282 vmx->nested.need_vmcs12_to_shadow_sync = false; 2283 } 2284 2285 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) 2286 { 2287 struct vcpu_vmx *vmx = 2288 container_of(timer, struct vcpu_vmx, nested.preemption_timer); 2289 2290 vmx->nested.preemption_timer_expired = true; 2291 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); 2292 kvm_vcpu_kick(&vmx->vcpu); 2293 2294 return HRTIMER_NORESTART; 2295 } 2296 2297 static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu) 2298 { 2299 struct vcpu_vmx *vmx = to_vmx(vcpu); 2300 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 2301 2302 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >> 2303 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; 2304 2305 if (!vmx->nested.has_preemption_timer_deadline) { 2306 vmx->nested.preemption_timer_deadline = 2307 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; 2308 vmx->nested.has_preemption_timer_deadline = true; 2309 } 2310 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; 2311 } 2312 2313 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu, 2314 u64 preemption_timeout) 2315 { 2316 struct vcpu_vmx *vmx = to_vmx(vcpu); 2317 2318 /* 2319 * A timer value of zero is architecturally guaranteed to cause 2320 * a VMExit prior to executing any instructions in the guest. 2321 */ 2322 if (preemption_timeout == 0) { 2323 vmx_preemption_timer_fn(&vmx->nested.preemption_timer); 2324 return; 2325 } 2326 2327 if (vcpu->arch.virtual_tsc_khz == 0) 2328 return; 2329 2330 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; 2331 preemption_timeout *= 1000000; 2332 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); 2333 hrtimer_start(&vmx->nested.preemption_timer, 2334 ktime_add_ns(ktime_get(), preemption_timeout), 2335 HRTIMER_MODE_ABS_PINNED); 2336 } 2337 2338 static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) 2339 { 2340 if (vmx->vcpu.arch.nested_run_pending && 2341 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) 2342 return vmcs12->guest_ia32_efer; 2343 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) 2344 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); 2345 else 2346 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); 2347 } 2348 2349 static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) 2350 { 2351 struct kvm *kvm = vmx->vcpu.kvm; 2352 2353 /* 2354 * If vmcs02 hasn't been initialized, set the constant vmcs02 state 2355 * according to L0's settings (vmcs12 is irrelevant here). Host 2356 * fields that come from L0 and are not constant, e.g. HOST_CR3, 2357 * will be set as needed prior to VMLAUNCH/VMRESUME. 2358 */ 2359 if (vmx->nested.vmcs02_initialized) 2360 return; 2361 vmx->nested.vmcs02_initialized = true; 2362 2363 if (vmx->ve_info) 2364 vmcs_write64(VE_INFORMATION_ADDRESS, __pa(vmx->ve_info)); 2365 2366 /* All VMFUNCs are currently emulated through L0 vmexits. */ 2367 if (cpu_has_vmx_vmfunc()) 2368 vmcs_write64(VM_FUNCTION_CONTROL, 0); 2369 2370 if (cpu_has_vmx_posted_intr()) 2371 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); 2372 2373 if (cpu_has_vmx_msr_bitmap()) 2374 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); 2375 2376 /* 2377 * PML is emulated for L2, but never enabled in hardware as the MMU 2378 * handles A/D emulation. Disabling PML for L2 also avoids having to 2379 * deal with filtering out L2 GPAs from the buffer. 2380 */ 2381 if (enable_pml) { 2382 vmcs_write64(PML_ADDRESS, 0); 2383 vmcs_write16(GUEST_PML_INDEX, -1); 2384 } 2385 2386 if (cpu_has_vmx_encls_vmexit()) 2387 vmcs_write64(ENCLS_EXITING_BITMAP, INVALID_GPA); 2388 2389 if (kvm_notify_vmexit_enabled(kvm)) 2390 vmcs_write32(NOTIFY_WINDOW, kvm->arch.notify_window); 2391 2392 /* 2393 * Set the MSR load/store lists to match L0's settings. Only the 2394 * addresses are constant (for vmcs02), the counts can change based 2395 * on L2's behavior, e.g. switching to/from long mode. 2396 */ 2397 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.val)); 2398 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); 2399 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); 2400 2401 vmx_set_constant_host_state(vmx); 2402 } 2403 2404 static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx, 2405 struct vmcs12 *vmcs12) 2406 { 2407 prepare_vmcs02_constant_state(vmx); 2408 2409 vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA); 2410 2411 /* 2412 * If VPID is disabled, then guest TLB accesses use VPID=0, i.e. the 2413 * same VPID as the host. Emulate this behavior by using vpid01 for L2 2414 * if VPID is disabled in vmcs12. Note, if VPID is disabled, VM-Enter 2415 * and VM-Exit are architecturally required to flush VPID=0, but *only* 2416 * VPID=0. I.e. using vpid02 would be ok (so long as KVM emulates the 2417 * required flushes), but doing so would cause KVM to over-flush. E.g. 2418 * if L1 runs L2 X with VPID12=1, then runs L2 Y with VPID12 disabled, 2419 * and then runs L2 X again, then KVM can and should retain TLB entries 2420 * for VPID12=1. 2421 */ 2422 if (enable_vpid) { 2423 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) 2424 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); 2425 else 2426 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); 2427 } 2428 } 2429 2430 static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs01, 2431 struct vmcs12 *vmcs12) 2432 { 2433 u32 exec_control; 2434 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); 2435 2436 if (vmx->nested.dirty_vmcs12 || nested_vmx_is_evmptr12_valid(vmx)) 2437 prepare_vmcs02_early_rare(vmx, vmcs12); 2438 2439 /* 2440 * PIN CONTROLS 2441 */ 2442 exec_control = __pin_controls_get(vmcs01); 2443 exec_control |= (vmcs12->pin_based_vm_exec_control & 2444 ~PIN_BASED_VMX_PREEMPTION_TIMER); 2445 2446 /* Posted interrupts setting is only taken from vmcs12. */ 2447 vmx->nested.pi_pending = false; 2448 if (nested_cpu_has_posted_intr(vmcs12)) { 2449 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; 2450 } else { 2451 vmx->nested.posted_intr_nv = -1; 2452 exec_control &= ~PIN_BASED_POSTED_INTR; 2453 } 2454 pin_controls_set(vmx, exec_control); 2455 2456 /* 2457 * EXEC CONTROLS 2458 */ 2459 exec_control = __exec_controls_get(vmcs01); /* L0's desires */ 2460 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING; 2461 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING; 2462 exec_control &= ~CPU_BASED_TPR_SHADOW; 2463 exec_control |= vmcs12->cpu_based_vm_exec_control; 2464 2465 if (exec_control & CPU_BASED_TPR_SHADOW) 2466 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); 2467 #ifdef CONFIG_X86_64 2468 else 2469 exec_control |= CPU_BASED_CR8_LOAD_EXITING | 2470 CPU_BASED_CR8_STORE_EXITING; 2471 #endif 2472 2473 /* 2474 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed 2475 * for I/O port accesses. 2476 */ 2477 exec_control |= CPU_BASED_UNCOND_IO_EXITING; 2478 exec_control &= ~CPU_BASED_USE_IO_BITMAPS; 2479 2480 /* 2481 * This bit will be computed in nested_get_vmcs12_pages, because 2482 * we do not have access to L1's MSR bitmap yet. For now, keep 2483 * the same bit as before, hoping to avoid multiple VMWRITEs that 2484 * only set/clear this bit. 2485 */ 2486 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; 2487 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS; 2488 2489 exec_controls_set(vmx, exec_control); 2490 2491 /* 2492 * SECONDARY EXEC CONTROLS 2493 */ 2494 if (cpu_has_secondary_exec_ctrls()) { 2495 exec_control = __secondary_exec_controls_get(vmcs01); 2496 2497 /* Take the following fields only from vmcs12 */ 2498 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | 2499 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | 2500 SECONDARY_EXEC_ENABLE_INVPCID | 2501 SECONDARY_EXEC_ENABLE_RDTSCP | 2502 SECONDARY_EXEC_ENABLE_XSAVES | 2503 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | 2504 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | 2505 SECONDARY_EXEC_APIC_REGISTER_VIRT | 2506 SECONDARY_EXEC_ENABLE_VMFUNC | 2507 SECONDARY_EXEC_MODE_BASED_EPT_EXEC | 2508 SECONDARY_EXEC_DESC); 2509 2510 if (nested_cpu_has(vmcs12, 2511 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) 2512 exec_control |= vmcs12->secondary_vm_exec_control; 2513 2514 /* PML is emulated and never enabled in hardware for L2. */ 2515 exec_control &= ~SECONDARY_EXEC_ENABLE_PML; 2516 2517 /* VMCS shadowing for L2 is emulated for now */ 2518 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; 2519 2520 /* 2521 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4() 2522 * will not have to rewrite the controls just for this bit. 2523 */ 2524 if (vmx_umip_emulated() && (vmcs12->guest_cr4 & X86_CR4_UMIP)) 2525 exec_control |= SECONDARY_EXEC_DESC; 2526 2527 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) 2528 vmcs_write16(GUEST_INTR_STATUS, 2529 vmcs12->guest_intr_status); 2530 2531 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) 2532 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; 2533 2534 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING) 2535 vmx_write_encls_bitmap(&vmx->vcpu, vmcs12); 2536 2537 secondary_exec_controls_set(vmx, exec_control); 2538 } 2539 2540 /* 2541 * ENTRY CONTROLS 2542 * 2543 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE 2544 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate 2545 * on the related bits (if supported by the CPU) in the hope that 2546 * we can avoid VMWrites during vmx_set_efer(). 2547 * 2548 * Similarly, take vmcs01's PERF_GLOBAL_CTRL in the hope that if KVM is 2549 * loading PERF_GLOBAL_CTRL via the VMCS for L1, then KVM will want to 2550 * do the same for L2. 2551 */ 2552 exec_control = __vm_entry_controls_get(vmcs01); 2553 exec_control |= (vmcs12->vm_entry_controls & 2554 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL); 2555 exec_control &= ~(VM_ENTRY_IA32E_MODE | VM_ENTRY_LOAD_IA32_EFER); 2556 if (cpu_has_load_ia32_efer()) { 2557 if (guest_efer & EFER_LMA) 2558 exec_control |= VM_ENTRY_IA32E_MODE; 2559 if (guest_efer != kvm_host.efer) 2560 exec_control |= VM_ENTRY_LOAD_IA32_EFER; 2561 } 2562 vm_entry_controls_set(vmx, exec_control); 2563 2564 /* 2565 * EXIT CONTROLS 2566 * 2567 * L2->L1 exit controls are emulated - the hardware exit is to L0 so 2568 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER 2569 * bits may be modified by vmx_set_efer() in prepare_vmcs02(). 2570 */ 2571 exec_control = __vm_exit_controls_get(vmcs01); 2572 if (cpu_has_load_ia32_efer() && guest_efer != kvm_host.efer) 2573 exec_control |= VM_EXIT_LOAD_IA32_EFER; 2574 else 2575 exec_control &= ~VM_EXIT_LOAD_IA32_EFER; 2576 vm_exit_controls_set(vmx, exec_control); 2577 2578 /* 2579 * Interrupt/Exception Fields 2580 */ 2581 if (vmx->vcpu.arch.nested_run_pending) { 2582 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 2583 vmcs12->vm_entry_intr_info_field); 2584 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 2585 vmcs12->vm_entry_exception_error_code); 2586 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 2587 vmcs12->vm_entry_instruction_len); 2588 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 2589 vmcs12->guest_interruptibility_info); 2590 vmx->loaded_vmcs->nmi_known_unmasked = 2591 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); 2592 } else { 2593 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); 2594 } 2595 } 2596 2597 static void vmcs_read_cet_state(struct kvm_vcpu *vcpu, u64 *s_cet, 2598 u64 *ssp, u64 *ssp_tbl) 2599 { 2600 if (guest_cpu_cap_has(vcpu, X86_FEATURE_IBT) || 2601 guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK)) 2602 *s_cet = vmcs_readl(GUEST_S_CET); 2603 2604 if (guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK)) { 2605 *ssp = vmcs_readl(GUEST_SSP); 2606 *ssp_tbl = vmcs_readl(GUEST_INTR_SSP_TABLE); 2607 } 2608 } 2609 2610 static void vmcs_write_cet_state(struct kvm_vcpu *vcpu, u64 s_cet, 2611 u64 ssp, u64 ssp_tbl) 2612 { 2613 if (guest_cpu_cap_has(vcpu, X86_FEATURE_IBT) || 2614 guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK)) 2615 vmcs_writel(GUEST_S_CET, s_cet); 2616 2617 if (guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK)) { 2618 vmcs_writel(GUEST_SSP, ssp); 2619 vmcs_writel(GUEST_INTR_SSP_TABLE, ssp_tbl); 2620 } 2621 } 2622 2623 static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) 2624 { 2625 struct hv_enlightened_vmcs *hv_evmcs = nested_vmx_evmcs(vmx); 2626 2627 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & 2628 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { 2629 2630 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); 2631 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); 2632 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); 2633 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); 2634 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); 2635 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); 2636 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); 2637 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); 2638 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); 2639 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); 2640 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); 2641 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); 2642 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); 2643 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); 2644 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); 2645 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); 2646 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); 2647 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); 2648 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); 2649 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); 2650 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); 2651 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); 2652 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); 2653 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); 2654 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); 2655 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); 2656 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); 2657 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); 2658 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); 2659 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); 2660 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); 2661 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); 2662 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); 2663 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); 2664 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); 2665 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); 2666 2667 vmx_segment_cache_clear(vmx); 2668 } 2669 2670 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & 2671 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { 2672 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); 2673 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 2674 vmcs12->guest_pending_dbg_exceptions); 2675 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); 2676 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); 2677 2678 if (kvm_mpx_supported() && vmx->vcpu.arch.nested_run_pending && 2679 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) 2680 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); 2681 } 2682 2683 if (nested_cpu_has_xsaves(vmcs12)) 2684 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); 2685 2686 /* 2687 * Whether page-faults are trapped is determined by a combination of 2688 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0 2689 * doesn't care about page faults then we should set all of these to 2690 * L1's desires. However, if L0 does care about (some) page faults, it 2691 * is not easy (if at all possible?) to merge L0 and L1's desires, we 2692 * simply ask to exit on each and every L2 page fault. This is done by 2693 * setting MASK=MATCH=0 and (see below) EB.PF=1. 2694 * Note that below we don't need special code to set EB.PF beyond the 2695 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, 2696 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when 2697 * !enable_ept, EB.PF is 1, so the "or" will always be 1. 2698 */ 2699 if (vmx_need_pf_intercept(&vmx->vcpu)) { 2700 /* 2701 * TODO: if both L0 and L1 need the same MASK and MATCH, 2702 * go ahead and use it? 2703 */ 2704 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); 2705 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); 2706 } else { 2707 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask); 2708 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match); 2709 } 2710 2711 if (cpu_has_vmx_apicv()) { 2712 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); 2713 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); 2714 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); 2715 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); 2716 } 2717 2718 /* 2719 * If vmcs12 is configured to save TSC on exit via the auto-store list, 2720 * append the MSR to vmcs02's auto-store list so that KVM effectively 2721 * reads TSC at the time of VM-Exit from L2. The saved value will be 2722 * propagated to vmcs12's list on nested VM-Exit. 2723 * 2724 * Don't increment the number of MSRs in the vCPU structure, as saving 2725 * TSC is specific to this particular incarnation of vmcb02, i.e. must 2726 * not bleed into vmcs01. 2727 */ 2728 if (nested_msr_store_list_has_msr(&vmx->vcpu, MSR_IA32_TSC) && 2729 !WARN_ON_ONCE(vmx->msr_autostore.nr >= ARRAY_SIZE(vmx->msr_autostore.val))) { 2730 vmx->nested.tsc_autostore_slot = vmx->msr_autostore.nr; 2731 vmx->msr_autostore.val[vmx->msr_autostore.nr].index = MSR_IA32_TSC; 2732 2733 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.nr + 1); 2734 } else { 2735 vmx->nested.tsc_autostore_slot = -1; 2736 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.nr); 2737 } 2738 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); 2739 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); 2740 2741 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_CET_STATE) 2742 vmcs_write_cet_state(&vmx->vcpu, vmcs12->guest_s_cet, 2743 vmcs12->guest_ssp, vmcs12->guest_ssp_tbl); 2744 2745 set_cr4_guest_host_mask(vmx); 2746 } 2747 2748 /* 2749 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested 2750 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it 2751 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 2752 * guest in a way that will both be appropriate to L1's requests, and our 2753 * needs. In addition to modifying the active vmcs (which is vmcs02), this 2754 * function also has additional necessary side-effects, like setting various 2755 * vcpu->arch fields. 2756 * Returns 0 on success, 1 on failure. Invalid state exit qualification code 2757 * is assigned to entry_failure_code on failure. 2758 */ 2759 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, 2760 bool from_vmentry, 2761 enum vm_entry_failure_code *entry_failure_code) 2762 { 2763 struct vcpu_vmx *vmx = to_vmx(vcpu); 2764 struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx); 2765 bool load_guest_pdptrs_vmcs12 = false; 2766 2767 if (vmx->nested.dirty_vmcs12 || nested_vmx_is_evmptr12_valid(vmx)) { 2768 prepare_vmcs02_rare(vmx, vmcs12); 2769 vmx->nested.dirty_vmcs12 = false; 2770 2771 load_guest_pdptrs_vmcs12 = !nested_vmx_is_evmptr12_valid(vmx) || 2772 !(evmcs->hv_clean_fields & HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1); 2773 } 2774 2775 if (vcpu->arch.nested_run_pending && 2776 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { 2777 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); 2778 vmx_guest_debugctl_write(vcpu, vmcs12->guest_ia32_debugctl & 2779 vmx_get_supported_debugctl(vcpu, false)); 2780 } else { 2781 kvm_set_dr(vcpu, 7, vcpu->arch.dr7); 2782 vmx_guest_debugctl_write(vcpu, vmx->nested.pre_vmenter_debugctl); 2783 } 2784 2785 if (!vcpu->arch.nested_run_pending || 2786 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_CET_STATE)) 2787 vmcs_write_cet_state(vcpu, vmx->nested.pre_vmenter_s_cet, 2788 vmx->nested.pre_vmenter_ssp, 2789 vmx->nested.pre_vmenter_ssp_tbl); 2790 2791 if (kvm_mpx_supported() && (!vcpu->arch.nested_run_pending || 2792 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) 2793 vmcs_write64(GUEST_BNDCFGS, vmx->nested.pre_vmenter_bndcfgs); 2794 vmx_set_rflags(vcpu, vmcs12->guest_rflags); 2795 2796 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the 2797 * bitwise-or of what L1 wants to trap for L2, and what we want to 2798 * trap. Note that CR0.TS also needs updating - we do this later. 2799 */ 2800 vmx_update_exception_bitmap(vcpu); 2801 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; 2802 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); 2803 2804 if (vcpu->arch.nested_run_pending && 2805 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { 2806 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); 2807 vcpu->arch.pat = vmcs12->guest_ia32_pat; 2808 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { 2809 vmcs_write64(GUEST_IA32_PAT, vcpu->arch.pat); 2810 } 2811 2812 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( 2813 vcpu->arch.l1_tsc_offset, 2814 vmx_get_l2_tsc_offset(vcpu), 2815 vmx_get_l2_tsc_multiplier(vcpu)); 2816 2817 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( 2818 vcpu->arch.l1_tsc_scaling_ratio, 2819 vmx_get_l2_tsc_multiplier(vcpu)); 2820 2821 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); 2822 if (kvm_caps.has_tsc_control) 2823 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); 2824 2825 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true); 2826 2827 if (nested_cpu_has_ept(vmcs12)) 2828 nested_ept_init_mmu_context(vcpu); 2829 2830 /* 2831 * Override the CR0/CR4 read shadows after setting the effective guest 2832 * CR0/CR4. The common helpers also set the shadows, but they don't 2833 * account for vmcs12's cr0/4_guest_host_mask. 2834 */ 2835 vmx_set_cr0(vcpu, vmcs12->guest_cr0); 2836 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); 2837 2838 vmx_set_cr4(vcpu, vmcs12->guest_cr4); 2839 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); 2840 2841 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); 2842 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ 2843 vmx_set_efer(vcpu, vcpu->arch.efer); 2844 2845 /* 2846 * Guest state is invalid and unrestricted guest is disabled, 2847 * which means L1 attempted VMEntry to L2 with invalid state. 2848 * Fail the VMEntry. 2849 * 2850 * However when force loading the guest state (SMM exit or 2851 * loading nested state after migration, it is possible to 2852 * have invalid guest state now, which will be later fixed by 2853 * restoring L2 register state 2854 */ 2855 if (CC(from_vmentry && !vmx_guest_state_valid(vcpu))) { 2856 *entry_failure_code = ENTRY_FAIL_DEFAULT; 2857 return -EINVAL; 2858 } 2859 2860 /* Shadow page tables on either EPT or shadow page tables. */ 2861 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), 2862 from_vmentry, entry_failure_code)) 2863 return -EINVAL; 2864 2865 /* 2866 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 2867 * on nested VM-Exit, which can occur without actually running L2 and 2868 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with 2869 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the 2870 * transition to HLT instead of running L2. 2871 */ 2872 if (enable_ept) 2873 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); 2874 2875 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ 2876 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && 2877 is_pae_paging(vcpu)) { 2878 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); 2879 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); 2880 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); 2881 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); 2882 } 2883 2884 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && 2885 kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu)) && 2886 WARN_ON_ONCE(__kvm_emulate_msr_write(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, 2887 vmcs12->guest_ia32_perf_global_ctrl))) { 2888 *entry_failure_code = ENTRY_FAIL_DEFAULT; 2889 return -EINVAL; 2890 } 2891 2892 kvm_rsp_write(vcpu, vmcs12->guest_rsp); 2893 kvm_rip_write(vcpu, vmcs12->guest_rip); 2894 2895 /* 2896 * It was observed that genuine Hyper-V running in L1 doesn't reset 2897 * 'hv_clean_fields' by itself, it only sets the corresponding dirty 2898 * bits when it changes a field in eVMCS. Mark all fields as clean 2899 * here. 2900 */ 2901 if (nested_vmx_is_evmptr12_valid(vmx)) 2902 evmcs->hv_clean_fields |= HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; 2903 2904 return 0; 2905 } 2906 2907 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) 2908 { 2909 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) && 2910 nested_cpu_has_virtual_nmis(vmcs12))) 2911 return -EINVAL; 2912 2913 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) && 2914 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING))) 2915 return -EINVAL; 2916 2917 return 0; 2918 } 2919 2920 static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp) 2921 { 2922 struct vcpu_vmx *vmx = to_vmx(vcpu); 2923 2924 /* Check for memory type validity */ 2925 switch (new_eptp & VMX_EPTP_MT_MASK) { 2926 case VMX_EPTP_MT_UC: 2927 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))) 2928 return false; 2929 break; 2930 case VMX_EPTP_MT_WB: 2931 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))) 2932 return false; 2933 break; 2934 default: 2935 return false; 2936 } 2937 2938 /* Page-walk levels validity. */ 2939 switch (new_eptp & VMX_EPTP_PWL_MASK) { 2940 case VMX_EPTP_PWL_5: 2941 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT))) 2942 return false; 2943 break; 2944 case VMX_EPTP_PWL_4: 2945 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT))) 2946 return false; 2947 break; 2948 default: 2949 return false; 2950 } 2951 2952 /* Reserved bits should not be set */ 2953 if (CC(!kvm_vcpu_is_legal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f))) 2954 return false; 2955 2956 /* AD, if set, should be supported */ 2957 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) { 2958 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))) 2959 return false; 2960 } 2961 2962 return true; 2963 } 2964 2965 /* 2966 * Checks related to VM-Execution Control Fields 2967 */ 2968 static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, 2969 struct vmcs12 *vmcs12) 2970 { 2971 struct vcpu_vmx *vmx = to_vmx(vcpu); 2972 2973 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control, 2974 vmx->nested.msrs.pinbased_ctls_low, 2975 vmx->nested.msrs.pinbased_ctls_high)) || 2976 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, 2977 vmx->nested.msrs.procbased_ctls_low, 2978 vmx->nested.msrs.procbased_ctls_high))) 2979 return -EINVAL; 2980 2981 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && 2982 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control, 2983 vmx->nested.msrs.secondary_ctls_low, 2984 vmx->nested.msrs.secondary_ctls_high))) 2985 return -EINVAL; 2986 2987 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) || 2988 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) || 2989 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) || 2990 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) || 2991 nested_vmx_check_apic_access_controls(vcpu, vmcs12) || 2992 nested_vmx_check_apicv_controls(vcpu, vmcs12) || 2993 nested_vmx_check_nmi_controls(vmcs12) || 2994 nested_vmx_check_pml_controls(vcpu, vmcs12) || 2995 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) || 2996 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) || 2997 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) || 2998 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)) 2999 return -EINVAL; 3000 3001 if (!nested_cpu_has_preemption_timer(vmcs12) && 3002 nested_cpu_has_save_preemption_timer(vmcs12)) 3003 return -EINVAL; 3004 3005 if (nested_cpu_has_ept(vmcs12) && 3006 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer))) 3007 return -EINVAL; 3008 3009 if (nested_cpu_has_vmfunc(vmcs12)) { 3010 if (CC(vmcs12->vm_function_control & 3011 ~vmx->nested.msrs.vmfunc_controls)) 3012 return -EINVAL; 3013 3014 if (nested_cpu_has_eptp_switching(vmcs12)) { 3015 if (CC(!nested_cpu_has_ept(vmcs12)) || 3016 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address))) 3017 return -EINVAL; 3018 } 3019 } 3020 3021 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING) && 3022 CC(!vmcs12->tsc_multiplier)) 3023 return -EINVAL; 3024 3025 return 0; 3026 } 3027 3028 /* 3029 * Checks related to VM-Exit Control Fields 3030 */ 3031 static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, 3032 struct vmcs12 *vmcs12) 3033 { 3034 struct vcpu_vmx *vmx = to_vmx(vcpu); 3035 3036 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls, 3037 vmx->nested.msrs.exit_ctls_low, 3038 vmx->nested.msrs.exit_ctls_high)) || 3039 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))) 3040 return -EINVAL; 3041 3042 return 0; 3043 } 3044 3045 /* 3046 * Checks related to VM-Entry Control Fields 3047 */ 3048 static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, 3049 struct vmcs12 *vmcs12) 3050 { 3051 struct vcpu_vmx *vmx = to_vmx(vcpu); 3052 3053 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls, 3054 vmx->nested.msrs.entry_ctls_low, 3055 vmx->nested.msrs.entry_ctls_high))) 3056 return -EINVAL; 3057 3058 /* 3059 * From the Intel SDM, volume 3: 3060 * Fields relevant to VM-entry event injection must be set properly. 3061 * These fields are the VM-entry interruption-information field, the 3062 * VM-entry exception error code, and the VM-entry instruction length. 3063 */ 3064 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { 3065 u32 intr_info = vmcs12->vm_entry_intr_info_field; 3066 u8 vector = intr_info & INTR_INFO_VECTOR_MASK; 3067 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; 3068 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; 3069 bool urg = nested_cpu_has2(vmcs12, 3070 SECONDARY_EXEC_UNRESTRICTED_GUEST); 3071 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; 3072 3073 /* VM-entry interruption-info field: interruption type */ 3074 if (CC(intr_type == INTR_TYPE_RESERVED) || 3075 CC(intr_type == INTR_TYPE_OTHER_EVENT && 3076 !nested_cpu_supports_monitor_trap_flag(vcpu))) 3077 return -EINVAL; 3078 3079 /* VM-entry interruption-info field: vector */ 3080 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || 3081 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || 3082 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) 3083 return -EINVAL; 3084 3085 /* 3086 * Cannot deliver error code in real mode or if the interrupt 3087 * type is not hardware exception. For other cases, do the 3088 * consistency check only if the vCPU doesn't enumerate 3089 * VMX_BASIC_NO_HW_ERROR_CODE_CC. 3090 */ 3091 if (!prot_mode || intr_type != INTR_TYPE_HARD_EXCEPTION) { 3092 if (CC(has_error_code)) 3093 return -EINVAL; 3094 } else if (!nested_cpu_has_no_hw_errcode_cc(vcpu)) { 3095 if (CC(has_error_code != x86_exception_has_error_code(vector))) 3096 return -EINVAL; 3097 } 3098 3099 /* VM-entry exception error code */ 3100 if (CC(has_error_code && 3101 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))) 3102 return -EINVAL; 3103 3104 /* VM-entry interruption-info field: reserved bits */ 3105 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK)) 3106 return -EINVAL; 3107 3108 /* VM-entry instruction length */ 3109 switch (intr_type) { 3110 case INTR_TYPE_SOFT_EXCEPTION: 3111 case INTR_TYPE_SOFT_INTR: 3112 case INTR_TYPE_PRIV_SW_EXCEPTION: 3113 if (CC(vmcs12->vm_entry_instruction_len > X86_MAX_INSTRUCTION_LENGTH) || 3114 CC(vmcs12->vm_entry_instruction_len == 0 && 3115 CC(!nested_cpu_has_zero_length_injection(vcpu)))) 3116 return -EINVAL; 3117 } 3118 } 3119 3120 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12)) 3121 return -EINVAL; 3122 3123 return 0; 3124 } 3125 3126 static int nested_vmx_check_controls(struct kvm_vcpu *vcpu, 3127 struct vmcs12 *vmcs12) 3128 { 3129 if (nested_check_vm_execution_controls(vcpu, vmcs12) || 3130 nested_check_vm_exit_controls(vcpu, vmcs12) || 3131 nested_check_vm_entry_controls(vcpu, vmcs12)) 3132 return -EINVAL; 3133 3134 #ifdef CONFIG_KVM_HYPERV 3135 if (guest_cpu_cap_has_evmcs(vcpu)) 3136 return nested_evmcs_check_controls(vmcs12); 3137 #endif 3138 3139 return 0; 3140 } 3141 3142 static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu, 3143 struct vmcs12 *vmcs12) 3144 { 3145 #ifdef CONFIG_X86_64 3146 if (CC(!!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) != 3147 !!(vcpu->arch.efer & EFER_LMA))) 3148 return -EINVAL; 3149 #endif 3150 return 0; 3151 } 3152 3153 static bool is_l1_noncanonical_address_on_vmexit(u64 la, struct vmcs12 *vmcs12) 3154 { 3155 /* 3156 * Check that the given linear address is canonical after a VM exit 3157 * from L2, based on HOST_CR4.LA57 value that will be loaded for L1. 3158 */ 3159 u8 l1_address_bits_on_exit = (vmcs12->host_cr4 & X86_CR4_LA57) ? 57 : 48; 3160 3161 return !__is_canonical_address(la, l1_address_bits_on_exit); 3162 } 3163 3164 static int nested_vmx_check_cet_state_common(struct kvm_vcpu *vcpu, u64 s_cet, 3165 u64 ssp, u64 ssp_tbl) 3166 { 3167 if (CC(!kvm_is_valid_u_s_cet(vcpu, s_cet)) || CC(!IS_ALIGNED(ssp, 4)) || 3168 CC(is_noncanonical_msr_address(ssp_tbl, vcpu))) 3169 return -EINVAL; 3170 3171 return 0; 3172 } 3173 3174 static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, 3175 struct vmcs12 *vmcs12) 3176 { 3177 bool ia32e = !!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE); 3178 3179 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) || 3180 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) || 3181 CC(!kvm_vcpu_is_legal_cr3(vcpu, vmcs12->host_cr3))) 3182 return -EINVAL; 3183 3184 if (CC(vmcs12->host_cr4 & X86_CR4_CET && !(vmcs12->host_cr0 & X86_CR0_WP))) 3185 return -EINVAL; 3186 3187 if (CC(is_noncanonical_msr_address(vmcs12->host_ia32_sysenter_esp, vcpu)) || 3188 CC(is_noncanonical_msr_address(vmcs12->host_ia32_sysenter_eip, vcpu))) 3189 return -EINVAL; 3190 3191 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) && 3192 CC(!kvm_pat_valid(vmcs12->host_ia32_pat))) 3193 return -EINVAL; 3194 3195 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) && 3196 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), 3197 vmcs12->host_ia32_perf_global_ctrl))) 3198 return -EINVAL; 3199 3200 if (ia32e) { 3201 if (CC(!(vmcs12->host_cr4 & X86_CR4_PAE))) 3202 return -EINVAL; 3203 } else { 3204 if (CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) || 3205 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) || 3206 CC((vmcs12->host_rip) >> 32)) 3207 return -EINVAL; 3208 } 3209 3210 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || 3211 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || 3212 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || 3213 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || 3214 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || 3215 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || 3216 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || 3217 CC(vmcs12->host_cs_selector == 0) || 3218 CC(vmcs12->host_tr_selector == 0) || 3219 CC(vmcs12->host_ss_selector == 0 && !ia32e)) 3220 return -EINVAL; 3221 3222 if (CC(is_noncanonical_base_address(vmcs12->host_fs_base, vcpu)) || 3223 CC(is_noncanonical_base_address(vmcs12->host_gs_base, vcpu)) || 3224 CC(is_noncanonical_base_address(vmcs12->host_gdtr_base, vcpu)) || 3225 CC(is_noncanonical_base_address(vmcs12->host_idtr_base, vcpu)) || 3226 CC(is_noncanonical_base_address(vmcs12->host_tr_base, vcpu)) || 3227 CC(is_l1_noncanonical_address_on_vmexit(vmcs12->host_rip, vmcs12))) 3228 return -EINVAL; 3229 3230 /* 3231 * If the load IA32_EFER VM-exit control is 1, bits reserved in the 3232 * IA32_EFER MSR must be 0 in the field for that register. In addition, 3233 * the values of the LMA and LME bits in the field must each be that of 3234 * the host address-space size VM-exit control. 3235 */ 3236 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { 3237 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) || 3238 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) || 3239 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))) 3240 return -EINVAL; 3241 } 3242 3243 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_CET_STATE) { 3244 if (nested_vmx_check_cet_state_common(vcpu, vmcs12->host_s_cet, 3245 vmcs12->host_ssp, 3246 vmcs12->host_ssp_tbl)) 3247 return -EINVAL; 3248 3249 /* 3250 * IA32_S_CET and SSP must be canonical if the host will 3251 * enter 64-bit mode after VM-exit; otherwise, higher 3252 * 32-bits must be all 0s. 3253 */ 3254 if (ia32e) { 3255 if (CC(is_noncanonical_msr_address(vmcs12->host_s_cet, vcpu)) || 3256 CC(is_noncanonical_msr_address(vmcs12->host_ssp, vcpu))) 3257 return -EINVAL; 3258 } else { 3259 if (CC(vmcs12->host_s_cet >> 32) || CC(vmcs12->host_ssp >> 32)) 3260 return -EINVAL; 3261 } 3262 } 3263 3264 return 0; 3265 } 3266 3267 static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, 3268 struct vmcs12 *vmcs12) 3269 { 3270 struct vcpu_vmx *vmx = to_vmx(vcpu); 3271 struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache; 3272 struct vmcs_hdr hdr; 3273 3274 if (vmcs12->vmcs_link_pointer == INVALID_GPA) 3275 return 0; 3276 3277 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))) 3278 return -EINVAL; 3279 3280 if (ghc->gpa != vmcs12->vmcs_link_pointer && 3281 CC(kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, 3282 vmcs12->vmcs_link_pointer, VMCS12_SIZE))) 3283 return -EINVAL; 3284 3285 if (CC(kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr, 3286 offsetof(struct vmcs12, hdr), 3287 sizeof(hdr)))) 3288 return -EINVAL; 3289 3290 if (CC(hdr.revision_id != VMCS12_REVISION) || 3291 CC(hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))) 3292 return -EINVAL; 3293 3294 return 0; 3295 } 3296 3297 /* 3298 * Checks related to Guest Non-register State 3299 */ 3300 static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12) 3301 { 3302 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && 3303 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT && 3304 vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI)) 3305 return -EINVAL; 3306 3307 return 0; 3308 } 3309 3310 static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, 3311 struct vmcs12 *vmcs12, 3312 enum vm_entry_failure_code *entry_failure_code) 3313 { 3314 bool ia32e = !!(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE); 3315 3316 *entry_failure_code = ENTRY_FAIL_DEFAULT; 3317 3318 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) || 3319 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))) 3320 return -EINVAL; 3321 3322 if (CC(vmcs12->guest_cr4 & X86_CR4_CET && !(vmcs12->guest_cr0 & X86_CR0_WP))) 3323 return -EINVAL; 3324 3325 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { 3326 u64 debugctl = vmcs12->guest_ia32_debugctl; 3327 3328 /* 3329 * FREEZE_IN_SMM is not virtualized, but allow L1 to set it in 3330 * vmcs12's DEBUGCTL under a quirk for backwards compatibility. 3331 * Note that the quirk only relaxes the consistency check. The 3332 * vmcc02 bit is still under the control of the host. In 3333 * particular, if a host administrator decides to clear the bit, 3334 * then L1 has no say in the matter. 3335 */ 3336 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM)) 3337 debugctl &= ~DEBUGCTLMSR_FREEZE_IN_SMM; 3338 3339 if (CC(!kvm_dr7_valid(vmcs12->guest_dr7)) || 3340 CC(!vmx_is_valid_debugctl(vcpu, debugctl, false))) 3341 return -EINVAL; 3342 } 3343 3344 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) && 3345 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat))) 3346 return -EINVAL; 3347 3348 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { 3349 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR; 3350 return -EINVAL; 3351 } 3352 3353 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && 3354 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), 3355 vmcs12->guest_ia32_perf_global_ctrl))) 3356 return -EINVAL; 3357 3358 if (CC((vmcs12->guest_cr0 & (X86_CR0_PG | X86_CR0_PE)) == X86_CR0_PG)) 3359 return -EINVAL; 3360 3361 if (CC(ia32e && !(vmcs12->guest_cr4 & X86_CR4_PAE)) || 3362 CC(ia32e && !(vmcs12->guest_cr0 & X86_CR0_PG))) 3363 return -EINVAL; 3364 3365 /* 3366 * If the load IA32_EFER VM-entry control is 1, the following checks 3367 * are performed on the field for the IA32_EFER MSR: 3368 * - Bits reserved in the IA32_EFER MSR must be 0. 3369 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of 3370 * the IA-32e mode guest VM-exit control. It must also be identical 3371 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to 3372 * CR0.PG) is 1. 3373 */ 3374 if (vcpu->arch.nested_run_pending && 3375 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { 3376 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) || 3377 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) || 3378 CC(((vmcs12->guest_cr0 & X86_CR0_PG) && 3379 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))) 3380 return -EINVAL; 3381 } 3382 3383 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && 3384 (CC(is_noncanonical_msr_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) || 3385 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))) 3386 return -EINVAL; 3387 3388 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_CET_STATE) { 3389 if (nested_vmx_check_cet_state_common(vcpu, vmcs12->guest_s_cet, 3390 vmcs12->guest_ssp, 3391 vmcs12->guest_ssp_tbl)) 3392 return -EINVAL; 3393 3394 /* 3395 * Guest SSP must have 63:N bits identical, rather than 3396 * be canonical (i.e., 63:N-1 bits identical), where N is 3397 * the CPU's maximum linear-address width. Similar to 3398 * is_noncanonical_msr_address(), use the host's 3399 * linear-address width. 3400 */ 3401 if (CC(!__is_canonical_address(vmcs12->guest_ssp, max_host_virt_addr_bits() + 1))) 3402 return -EINVAL; 3403 } 3404 3405 if (nested_check_guest_non_reg_state(vmcs12)) 3406 return -EINVAL; 3407 3408 return 0; 3409 } 3410 3411 #ifdef CONFIG_KVM_HYPERV 3412 static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) 3413 { 3414 struct vcpu_vmx *vmx = to_vmx(vcpu); 3415 3416 /* 3417 * hv_evmcs may end up being not mapped after migration (when 3418 * L2 was running), map it here to make sure vmcs12 changes are 3419 * properly reflected. 3420 */ 3421 if (guest_cpu_cap_has_evmcs(vcpu) && 3422 vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) { 3423 enum nested_evmptrld_status evmptrld_status = 3424 nested_vmx_handle_enlightened_vmptrld(vcpu, false); 3425 3426 if (evmptrld_status == EVMPTRLD_VMFAIL || 3427 evmptrld_status == EVMPTRLD_ERROR) 3428 return false; 3429 3430 /* 3431 * Post migration VMCS12 always provides the most actual 3432 * information, copy it to eVMCS upon entry. 3433 */ 3434 vmx->nested.need_vmcs12_to_shadow_sync = true; 3435 } 3436 3437 return true; 3438 } 3439 #endif 3440 3441 static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) 3442 { 3443 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 3444 struct vcpu_vmx *vmx = to_vmx(vcpu); 3445 struct kvm_host_map *map; 3446 3447 if (!vcpu->arch.pdptrs_from_userspace && 3448 !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { 3449 /* 3450 * Reload the guest's PDPTRs since after a migration 3451 * the guest CR3 might be restored prior to setting the nested 3452 * state which can lead to a load of wrong PDPTRs. 3453 */ 3454 if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3))) 3455 return false; 3456 } 3457 3458 3459 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { 3460 map = &vmx->nested.apic_access_page_map; 3461 3462 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->apic_access_addr), map)) { 3463 vmcs_write64(APIC_ACCESS_ADDR, pfn_to_hpa(map->pfn)); 3464 } else { 3465 pr_debug_ratelimited("%s: no backing for APIC-access address in vmcs12\n", 3466 __func__); 3467 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 3468 vcpu->run->internal.suberror = 3469 KVM_INTERNAL_ERROR_EMULATION; 3470 vcpu->run->internal.ndata = 0; 3471 return false; 3472 } 3473 } 3474 3475 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { 3476 map = &vmx->nested.virtual_apic_map; 3477 3478 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) { 3479 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn)); 3480 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) && 3481 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) && 3482 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { 3483 /* 3484 * The processor will never use the TPR shadow, simply 3485 * clear the bit from the execution control. Such a 3486 * configuration is useless, but it happens in tests. 3487 * For any other configuration, failing the vm entry is 3488 * _not_ what the processor does but it's basically the 3489 * only possibility we have. 3490 */ 3491 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW); 3492 } else { 3493 /* 3494 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to 3495 * force VM-Entry to fail. 3496 */ 3497 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, INVALID_GPA); 3498 } 3499 } 3500 3501 if (nested_cpu_has_posted_intr(vmcs12)) { 3502 map = &vmx->nested.pi_desc_map; 3503 3504 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) { 3505 vmx->nested.pi_desc = 3506 (struct pi_desc *)(((void *)map->hva) + 3507 offset_in_page(vmcs12->posted_intr_desc_addr)); 3508 vmcs_write64(POSTED_INTR_DESC_ADDR, 3509 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr)); 3510 } else { 3511 /* 3512 * Defer the KVM_INTERNAL_EXIT until KVM tries to 3513 * access the contents of the VMCS12 posted interrupt 3514 * descriptor. (Note that KVM may do this when it 3515 * should not, per the architectural specification.) 3516 */ 3517 vmx->nested.pi_desc = NULL; 3518 pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR); 3519 } 3520 } 3521 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) 3522 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS); 3523 else 3524 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS); 3525 3526 return true; 3527 } 3528 3529 static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) 3530 { 3531 #ifdef CONFIG_KVM_HYPERV 3532 /* 3533 * Note: nested_get_evmcs_page() also updates 'vp_assist_page' copy 3534 * in 'struct kvm_vcpu_hv' in case eVMCS is in use, this is mandatory 3535 * to make nested_evmcs_l2_tlb_flush_enabled() work correctly post 3536 * migration. 3537 */ 3538 if (!nested_get_evmcs_page(vcpu)) { 3539 pr_debug_ratelimited("%s: enlightened vmptrld failed\n", 3540 __func__); 3541 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 3542 vcpu->run->internal.suberror = 3543 KVM_INTERNAL_ERROR_EMULATION; 3544 vcpu->run->internal.ndata = 0; 3545 3546 return false; 3547 } 3548 #endif 3549 3550 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu)) 3551 return false; 3552 3553 return true; 3554 } 3555 3556 static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa) 3557 { 3558 struct vmcs12 *vmcs12; 3559 struct vcpu_vmx *vmx = to_vmx(vcpu); 3560 gpa_t dst; 3561 3562 if (WARN_ON_ONCE(!is_guest_mode(vcpu))) 3563 return 0; 3564 3565 if (WARN_ON_ONCE(vmx->nested.pml_full)) 3566 return 1; 3567 3568 /* 3569 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is 3570 * set is already checked as part of A/D emulation. 3571 */ 3572 vmcs12 = get_vmcs12(vcpu); 3573 if (!nested_cpu_has_pml(vmcs12)) 3574 return 0; 3575 3576 if (vmcs12->guest_pml_index >= PML_LOG_NR_ENTRIES) { 3577 vmx->nested.pml_full = true; 3578 return 1; 3579 } 3580 3581 gpa &= ~0xFFFull; 3582 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index; 3583 3584 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa, 3585 offset_in_page(dst), sizeof(gpa))) 3586 return 0; 3587 3588 vmcs12->guest_pml_index--; 3589 3590 return 0; 3591 } 3592 3593 /* 3594 * Intel's VMX Instruction Reference specifies a common set of prerequisites 3595 * for running VMX instructions (except VMXON, whose prerequisites are 3596 * slightly different). It also specifies what exception to inject otherwise. 3597 * Note that many of these exceptions have priority over VM exits, so they 3598 * don't have to be checked again here. 3599 */ 3600 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) 3601 { 3602 if (!to_vmx(vcpu)->nested.vmxon) { 3603 kvm_queue_exception(vcpu, UD_VECTOR); 3604 return 0; 3605 } 3606 3607 if (vmx_get_cpl(vcpu)) { 3608 kvm_inject_gp(vcpu, 0); 3609 return 0; 3610 } 3611 3612 return 1; 3613 } 3614 3615 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, 3616 struct vmcs12 *vmcs12); 3617 3618 /* 3619 * If from_vmentry is false, this is being called from state restore (either RSM 3620 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. 3621 * 3622 * Returns: 3623 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode 3624 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail 3625 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit 3626 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error 3627 */ 3628 enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, 3629 bool from_vmentry) 3630 { 3631 struct vcpu_vmx *vmx = to_vmx(vcpu); 3632 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 3633 enum vm_entry_failure_code entry_failure_code; 3634 union vmx_exit_reason exit_reason = { 3635 .basic = EXIT_REASON_INVALID_STATE, 3636 .failed_vmentry = 1, 3637 }; 3638 u32 failed_index; 3639 3640 trace_kvm_nested_vmenter(kvm_rip_read(vcpu), 3641 vmx->nested.current_vmptr, 3642 vmcs12->guest_rip, 3643 vmcs12->guest_intr_status, 3644 vmcs12->vm_entry_intr_info_field, 3645 vmcs12->secondary_vm_exec_control & SECONDARY_EXEC_ENABLE_EPT, 3646 vmcs12->ept_pointer, 3647 vmcs12->guest_cr3, 3648 KVM_ISA_VMX); 3649 3650 kvm_service_local_tlb_flush_requests(vcpu); 3651 3652 if (!vcpu->arch.nested_run_pending || 3653 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) 3654 vmx->nested.pre_vmenter_debugctl = vmx_guest_debugctl_read(); 3655 if (kvm_mpx_supported() && 3656 (!vcpu->arch.nested_run_pending || 3657 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) 3658 vmx->nested.pre_vmenter_bndcfgs = vmcs_read64(GUEST_BNDCFGS); 3659 3660 if (!vcpu->arch.nested_run_pending || 3661 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_CET_STATE)) 3662 vmcs_read_cet_state(vcpu, &vmx->nested.pre_vmenter_s_cet, 3663 &vmx->nested.pre_vmenter_ssp, 3664 &vmx->nested.pre_vmenter_ssp_tbl); 3665 3666 /* 3667 * Stash L1's CR3, so that in the event of a "late" VM-Fail, i.e. a 3668 * VM-Fail detected by hardware but not KVM, KVM can unwind its 3669 * software model to the pre-VM-Entry host state. When EPT is 3670 * disabled, GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, 3671 * and so simply restoring from vmcs01.GUEST_CR3 would corrupt 3672 * vcpu->arch.cr3. 3673 */ 3674 vmx->nested.pre_vmenter_cr3 = kvm_read_cr3(vcpu); 3675 3676 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); 3677 3678 prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12); 3679 3680 if (from_vmentry) { 3681 if (unlikely(!nested_get_vmcs12_pages(vcpu))) { 3682 vmx_switch_vmcs(vcpu, &vmx->vmcs01); 3683 return NVMX_VMENTRY_KVM_INTERNAL_ERROR; 3684 } 3685 3686 if (nested_vmx_check_guest_state(vcpu, vmcs12, 3687 &entry_failure_code)) { 3688 exit_reason.basic = EXIT_REASON_INVALID_STATE; 3689 vmcs12->exit_qualification = entry_failure_code; 3690 goto vmentry_fail_vmexit; 3691 } 3692 } 3693 3694 enter_guest_mode(vcpu); 3695 3696 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) { 3697 exit_reason.basic = EXIT_REASON_INVALID_STATE; 3698 vmcs12->exit_qualification = entry_failure_code; 3699 goto vmentry_fail_vmexit_guest_mode; 3700 } 3701 3702 if (from_vmentry) { 3703 failed_index = nested_vmx_load_msr(vcpu, 3704 vmcs12->vm_entry_msr_load_addr, 3705 vmcs12->vm_entry_msr_load_count); 3706 if (failed_index) { 3707 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL; 3708 vmcs12->exit_qualification = failed_index; 3709 goto vmentry_fail_vmexit_guest_mode; 3710 } 3711 } else { 3712 /* 3713 * The MMU is not initialized to point at the right entities yet and 3714 * "get pages" would need to read data from the guest (i.e. we will 3715 * need to perform gpa to hpa translation). Request a call 3716 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs 3717 * have already been set at vmentry time and should not be reset. 3718 */ 3719 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); 3720 } 3721 3722 /* 3723 * Re-evaluate pending events if L1 had a pending IRQ/NMI/INIT/SIPI 3724 * when it executed VMLAUNCH/VMRESUME, as entering non-root mode can 3725 * effectively unblock various events, e.g. INIT/SIPI cause VM-Exit 3726 * unconditionally. Take care to pull data from vmcs01 as appropriate, 3727 * e.g. when checking for interrupt windows, as vmcs02 is now loaded. 3728 */ 3729 if ((__exec_controls_get(&vmx->vmcs01) & (CPU_BASED_INTR_WINDOW_EXITING | 3730 CPU_BASED_NMI_WINDOW_EXITING)) || 3731 kvm_apic_has_pending_init_or_sipi(vcpu) || 3732 kvm_apic_has_interrupt(vcpu)) 3733 kvm_make_request(KVM_REQ_EVENT, vcpu); 3734 3735 /* 3736 * Do not start the preemption timer hrtimer until after we know 3737 * we are successful, so that only nested_vmx_vmexit needs to cancel 3738 * the timer. 3739 */ 3740 vmx->nested.preemption_timer_expired = false; 3741 if (nested_cpu_has_preemption_timer(vmcs12)) { 3742 u64 timer_value = vmx_calc_preemption_timer_value(vcpu); 3743 vmx_start_preemption_timer(vcpu, timer_value); 3744 } 3745 3746 /* 3747 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point 3748 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet 3749 * returned as far as L1 is concerned. It will only return (and set 3750 * the success flag) when L2 exits (see nested_vmx_vmexit()). 3751 */ 3752 return NVMX_VMENTRY_SUCCESS; 3753 3754 /* 3755 * A failed consistency check that leads to a VMExit during L1's 3756 * VMEnter to L2 is a variation of a normal VMexit, as explained in 3757 * 26.7 "VM-entry failures during or after loading guest state". 3758 */ 3759 vmentry_fail_vmexit_guest_mode: 3760 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) 3761 vcpu->arch.tsc_offset -= vmcs12->tsc_offset; 3762 leave_guest_mode(vcpu); 3763 3764 vmentry_fail_vmexit: 3765 vmx_switch_vmcs(vcpu, &vmx->vmcs01); 3766 3767 if (!from_vmentry) 3768 return NVMX_VMENTRY_VMEXIT; 3769 3770 nested_put_vmcs12_pages(vcpu); 3771 3772 load_vmcs12_host_state(vcpu, vmcs12); 3773 vmcs12->vm_exit_reason = exit_reason.full; 3774 if (enable_shadow_vmcs || nested_vmx_is_evmptr12_valid(vmx)) 3775 vmx->nested.need_vmcs12_to_shadow_sync = true; 3776 return NVMX_VMENTRY_VMEXIT; 3777 } 3778 3779 /* 3780 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 3781 * for running an L2 nested guest. 3782 */ 3783 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) 3784 { 3785 struct vmcs12 *vmcs12; 3786 enum nvmx_vmentry_status status; 3787 struct vcpu_vmx *vmx = to_vmx(vcpu); 3788 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); 3789 enum nested_evmptrld_status evmptrld_status; 3790 3791 if (!nested_vmx_check_permission(vcpu)) 3792 return 1; 3793 3794 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch); 3795 if (evmptrld_status == EVMPTRLD_ERROR) { 3796 kvm_queue_exception(vcpu, UD_VECTOR); 3797 return 1; 3798 } 3799 3800 kvm_pmu_branch_retired(vcpu); 3801 3802 if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) 3803 return nested_vmx_failInvalid(vcpu); 3804 3805 if (CC(!nested_vmx_is_evmptr12_valid(vmx) && 3806 vmx->nested.current_vmptr == INVALID_GPA)) 3807 return nested_vmx_failInvalid(vcpu); 3808 3809 vmcs12 = get_vmcs12(vcpu); 3810 3811 /* 3812 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact 3813 * that there *is* a valid VMCS pointer, RFLAGS.CF is set 3814 * rather than RFLAGS.ZF, and no error number is stored to the 3815 * VM-instruction error field. 3816 */ 3817 if (CC(vmcs12->hdr.shadow_vmcs)) 3818 return nested_vmx_failInvalid(vcpu); 3819 3820 if (nested_vmx_is_evmptr12_valid(vmx)) { 3821 struct hv_enlightened_vmcs *evmcs = nested_vmx_evmcs(vmx); 3822 3823 copy_enlightened_to_vmcs12(vmx, evmcs->hv_clean_fields); 3824 /* Enlightened VMCS doesn't have launch state */ 3825 vmcs12->launch_state = !launch; 3826 } else if (enable_shadow_vmcs) { 3827 copy_shadow_to_vmcs12(vmx); 3828 } 3829 3830 /* 3831 * The nested entry process starts with enforcing various prerequisites 3832 * on vmcs12 as required by the Intel SDM, and act appropriately when 3833 * they fail: As the SDM explains, some conditions should cause the 3834 * instruction to fail, while others will cause the instruction to seem 3835 * to succeed, but return an EXIT_REASON_INVALID_STATE. 3836 * To speed up the normal (success) code path, we should avoid checking 3837 * for misconfigurations which will anyway be caught by the processor 3838 * when using the merged vmcs02. 3839 */ 3840 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)) 3841 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); 3842 3843 if (CC(vmcs12->launch_state == launch)) 3844 return nested_vmx_fail(vcpu, 3845 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS 3846 : VMXERR_VMRESUME_NONLAUNCHED_VMCS); 3847 3848 if (nested_vmx_check_controls(vcpu, vmcs12)) 3849 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); 3850 3851 if (nested_vmx_check_address_space_size(vcpu, vmcs12)) 3852 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 3853 3854 if (nested_vmx_check_host_state(vcpu, vmcs12)) 3855 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 3856 3857 /* 3858 * We're finally done with prerequisite checking, and can start with 3859 * the nested entry. 3860 */ 3861 vcpu->arch.nested_run_pending = KVM_NESTED_RUN_PENDING; 3862 vmx->nested.has_preemption_timer_deadline = false; 3863 status = nested_vmx_enter_non_root_mode(vcpu, true); 3864 if (unlikely(status != NVMX_VMENTRY_SUCCESS)) 3865 goto vmentry_failed; 3866 3867 /* Hide L1D cache contents from the nested guest. */ 3868 kvm_request_l1tf_flush_l1d(); 3869 3870 /* 3871 * Must happen outside of nested_vmx_enter_non_root_mode() as it will 3872 * also be used as part of restoring nVMX state for 3873 * snapshot restore (migration). 3874 * 3875 * In this flow, it is assumed that vmcs12 cache was 3876 * transferred as part of captured nVMX state and should 3877 * therefore not be read from guest memory (which may not 3878 * exist on destination host yet). 3879 */ 3880 nested_cache_shadow_vmcs12(vcpu, vmcs12); 3881 3882 switch (vmcs12->guest_activity_state) { 3883 case GUEST_ACTIVITY_HLT: 3884 /* 3885 * If we're entering a halted L2 vcpu and the L2 vcpu won't be 3886 * awakened by event injection or by an NMI-window VM-exit or 3887 * by an interrupt-window VM-exit, halt the vcpu. 3888 */ 3889 if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) && 3890 !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) && 3891 !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) && 3892 (vmcs12->guest_rflags & X86_EFLAGS_IF))) { 3893 vcpu->arch.nested_run_pending = 0; 3894 return kvm_emulate_halt_noskip(vcpu); 3895 } 3896 break; 3897 case GUEST_ACTIVITY_WAIT_SIPI: 3898 vcpu->arch.nested_run_pending = 0; 3899 kvm_set_mp_state(vcpu, KVM_MP_STATE_INIT_RECEIVED); 3900 break; 3901 default: 3902 break; 3903 } 3904 3905 return 1; 3906 3907 vmentry_failed: 3908 vcpu->arch.nested_run_pending = 0; 3909 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR) 3910 return 0; 3911 if (status == NVMX_VMENTRY_VMEXIT) 3912 return 1; 3913 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL); 3914 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); 3915 } 3916 3917 /* 3918 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date 3919 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK). 3920 * This function returns the new value we should put in vmcs12.guest_cr0. 3921 * It's not enough to just return the vmcs02 GUEST_CR0. Rather, 3922 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now 3923 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 3924 * didn't trap the bit, because if L1 did, so would L0). 3925 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have 3926 * been modified by L2, and L1 knows it. So just leave the old value of 3927 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 3928 * isn't relevant, because if L0 traps this bit it can set it to anything. 3929 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have 3930 * changed these bits, and therefore they need to be updated, but L0 3931 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather 3932 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. 3933 */ 3934 static inline unsigned long 3935 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) 3936 { 3937 return 3938 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | 3939 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | 3940 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | 3941 vcpu->arch.cr0_guest_owned_bits)); 3942 } 3943 3944 static inline unsigned long 3945 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) 3946 { 3947 return 3948 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | 3949 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | 3950 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | 3951 vcpu->arch.cr4_guest_owned_bits)); 3952 } 3953 3954 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, 3955 struct vmcs12 *vmcs12, 3956 u32 vm_exit_reason, u32 exit_intr_info) 3957 { 3958 u32 idt_vectoring; 3959 unsigned int nr; 3960 3961 /* 3962 * Per the SDM, VM-Exits due to double and triple faults are never 3963 * considered to occur during event delivery, even if the double/triple 3964 * fault is the result of an escalating vectoring issue. 3965 * 3966 * Note, the SDM qualifies the double fault behavior with "The original 3967 * event results in a double-fault exception". It's unclear why the 3968 * qualification exists since exits due to double fault can occur only 3969 * while vectoring a different exception (injected events are never 3970 * subject to interception), i.e. there's _always_ an original event. 3971 * 3972 * The SDM also uses NMI as a confusing example for the "original event 3973 * causes the VM exit directly" clause. NMI isn't special in any way, 3974 * the same rule applies to all events that cause an exit directly. 3975 * NMI is an odd choice for the example because NMIs can only occur on 3976 * instruction boundaries, i.e. they _can't_ occur during vectoring. 3977 */ 3978 if ((u16)vm_exit_reason == EXIT_REASON_TRIPLE_FAULT || 3979 ((u16)vm_exit_reason == EXIT_REASON_EXCEPTION_NMI && 3980 is_double_fault(exit_intr_info))) { 3981 vmcs12->idt_vectoring_info_field = 0; 3982 } else if (vcpu->arch.exception.injected) { 3983 nr = vcpu->arch.exception.vector; 3984 idt_vectoring = nr | VECTORING_INFO_VALID_MASK; 3985 3986 if (kvm_exception_is_soft(nr)) { 3987 vmcs12->vm_exit_instruction_len = 3988 vcpu->arch.event_exit_inst_len; 3989 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; 3990 } else 3991 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; 3992 3993 if (vcpu->arch.exception.has_error_code) { 3994 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; 3995 vmcs12->idt_vectoring_error_code = 3996 vcpu->arch.exception.error_code; 3997 } 3998 3999 vmcs12->idt_vectoring_info_field = idt_vectoring; 4000 } else if (vcpu->arch.nmi_injected) { 4001 vmcs12->idt_vectoring_info_field = 4002 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; 4003 } else if (vcpu->arch.interrupt.injected) { 4004 nr = vcpu->arch.interrupt.nr; 4005 idt_vectoring = nr | VECTORING_INFO_VALID_MASK; 4006 4007 if (vcpu->arch.interrupt.soft) { 4008 idt_vectoring |= INTR_TYPE_SOFT_INTR; 4009 vmcs12->vm_entry_instruction_len = 4010 vcpu->arch.event_exit_inst_len; 4011 } else 4012 idt_vectoring |= INTR_TYPE_EXT_INTR; 4013 4014 vmcs12->idt_vectoring_info_field = idt_vectoring; 4015 } else { 4016 vmcs12->idt_vectoring_info_field = 0; 4017 } 4018 } 4019 4020 static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) 4021 { 4022 struct vcpu_vmx *vmx = to_vmx(vcpu); 4023 int max_irr; 4024 void *vapic_page; 4025 u16 status; 4026 4027 if (!vmx->nested.pi_pending) 4028 return 0; 4029 4030 if (!vmx->nested.pi_desc) 4031 goto mmio_needed; 4032 4033 vmx->nested.pi_pending = false; 4034 4035 if (!pi_test_and_clear_on(vmx->nested.pi_desc)) 4036 return 0; 4037 4038 max_irr = pi_find_highest_vector(vmx->nested.pi_desc); 4039 if (max_irr > 0) { 4040 vapic_page = vmx->nested.virtual_apic_map.hva; 4041 if (!vapic_page) 4042 goto mmio_needed; 4043 4044 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, 4045 vapic_page, &max_irr); 4046 status = vmcs_read16(GUEST_INTR_STATUS); 4047 if ((u8)max_irr > ((u8)status & 0xff)) { 4048 status &= ~0xff; 4049 status |= (u8)max_irr; 4050 vmcs_write16(GUEST_INTR_STATUS, status); 4051 } 4052 } 4053 4054 kvm_vcpu_map_mark_dirty(vcpu, &vmx->nested.virtual_apic_map); 4055 kvm_vcpu_map_mark_dirty(vcpu, &vmx->nested.pi_desc_map); 4056 return 0; 4057 4058 mmio_needed: 4059 kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL); 4060 return -ENXIO; 4061 } 4062 4063 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu) 4064 { 4065 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit; 4066 u32 intr_info = ex->vector | INTR_INFO_VALID_MASK; 4067 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 4068 unsigned long exit_qual; 4069 4070 if (ex->has_payload) { 4071 exit_qual = ex->payload; 4072 } else if (ex->vector == PF_VECTOR) { 4073 exit_qual = vcpu->arch.cr2; 4074 } else if (ex->vector == DB_VECTOR) { 4075 exit_qual = vcpu->arch.dr6; 4076 exit_qual &= ~DR6_BT; 4077 exit_qual ^= DR6_ACTIVE_LOW; 4078 } else { 4079 exit_qual = 0; 4080 } 4081 4082 /* 4083 * Unlike AMD's Paged Real Mode, which reports an error code on #PF 4084 * VM-Exits even if the CPU is in Real Mode, Intel VMX never sets the 4085 * "has error code" flags on VM-Exit if the CPU is in Real Mode. 4086 */ 4087 if (ex->has_error_code && is_protmode(vcpu)) { 4088 /* 4089 * Intel CPUs do not generate error codes with bits 31:16 set, 4090 * and more importantly VMX disallows setting bits 31:16 in the 4091 * injected error code for VM-Entry. Drop the bits to mimic 4092 * hardware and avoid inducing failure on nested VM-Entry if L1 4093 * chooses to inject the exception back to L2. AMD CPUs _do_ 4094 * generate "full" 32-bit error codes, so KVM allows userspace 4095 * to inject exception error codes with bits 31:16 set. 4096 */ 4097 vmcs12->vm_exit_intr_error_code = (u16)ex->error_code; 4098 intr_info |= INTR_INFO_DELIVER_CODE_MASK; 4099 } 4100 4101 if (kvm_exception_is_soft(ex->vector)) 4102 intr_info |= INTR_TYPE_SOFT_EXCEPTION; 4103 else 4104 intr_info |= INTR_TYPE_HARD_EXCEPTION; 4105 4106 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && 4107 vmx_get_nmi_mask(vcpu)) 4108 intr_info |= INTR_INFO_UNBLOCK_NMI; 4109 4110 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); 4111 } 4112 4113 /* 4114 * Returns true if a debug trap is (likely) pending delivery. Infer the class 4115 * of a #DB (trap-like vs. fault-like) from the exception payload (to-be-DR6). 4116 * Using the payload is flawed because code breakpoints (fault-like) and data 4117 * breakpoints (trap-like) set the same bits in DR6 (breakpoint detected), i.e. 4118 * this will return false positives if a to-be-injected code breakpoint #DB is 4119 * pending (from KVM's perspective, but not "pending" across an instruction 4120 * boundary). ICEBP, a.k.a. INT1, is also not reflected here even though it 4121 * too is trap-like. 4122 * 4123 * KVM "works" despite these flaws as ICEBP isn't currently supported by the 4124 * emulator, Monitor Trap Flag is not marked pending on intercepted #DBs (the 4125 * #DB has already happened), and MTF isn't marked pending on code breakpoints 4126 * from the emulator (because such #DBs are fault-like and thus don't trigger 4127 * actions that fire on instruction retire). 4128 */ 4129 static unsigned long vmx_get_pending_dbg_trap(struct kvm_queued_exception *ex) 4130 { 4131 if (!ex->pending || ex->vector != DB_VECTOR) 4132 return 0; 4133 4134 /* General Detect #DBs are always fault-like. */ 4135 return ex->payload & ~DR6_BD; 4136 } 4137 4138 /* 4139 * Returns true if there's a pending #DB exception that is lower priority than 4140 * a pending Monitor Trap Flag VM-Exit. TSS T-flag #DBs are not emulated by 4141 * KVM, but could theoretically be injected by userspace. Note, this code is 4142 * imperfect, see above. 4143 */ 4144 static bool vmx_is_low_priority_db_trap(struct kvm_queued_exception *ex) 4145 { 4146 return vmx_get_pending_dbg_trap(ex) & ~DR6_BT; 4147 } 4148 4149 /* 4150 * Certain VM-exits set the 'pending debug exceptions' field to indicate a 4151 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM 4152 * represents these debug traps with a payload that is said to be compatible 4153 * with the 'pending debug exceptions' field, write the payload to the VMCS 4154 * field if a VM-exit is delivered before the debug trap. 4155 */ 4156 static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) 4157 { 4158 unsigned long pending_dbg; 4159 4160 pending_dbg = vmx_get_pending_dbg_trap(&vcpu->arch.exception); 4161 if (pending_dbg) 4162 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, pending_dbg); 4163 } 4164 4165 static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) 4166 { 4167 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && 4168 to_vmx(vcpu)->nested.preemption_timer_expired; 4169 } 4170 4171 static bool vmx_has_nested_events(struct kvm_vcpu *vcpu, bool for_injection) 4172 { 4173 struct vcpu_vmx *vmx = to_vmx(vcpu); 4174 void *vapic = vmx->nested.virtual_apic_map.hva; 4175 int max_irr, vppr; 4176 4177 if (nested_vmx_preemption_timer_pending(vcpu) || 4178 vmx->nested.mtf_pending) 4179 return true; 4180 4181 /* 4182 * Virtual Interrupt Delivery doesn't require manual injection. Either 4183 * the interrupt is already in GUEST_RVI and will be recognized by CPU 4184 * at VM-Entry, or there is a KVM_REQ_EVENT pending and KVM will move 4185 * the interrupt from the PIR to RVI prior to entering the guest. 4186 */ 4187 if (for_injection) 4188 return false; 4189 4190 if (!nested_cpu_has_vid(get_vmcs12(vcpu)) || 4191 __vmx_interrupt_blocked(vcpu)) 4192 return false; 4193 4194 if (!vapic) 4195 return false; 4196 4197 vppr = *((u32 *)(vapic + APIC_PROCPRI)); 4198 4199 max_irr = vmx_get_rvi(); 4200 if ((max_irr & 0xf0) > (vppr & 0xf0)) 4201 return true; 4202 4203 if (vmx->nested.pi_pending && vmx->nested.pi_desc && 4204 pi_test_on(vmx->nested.pi_desc)) { 4205 max_irr = pi_find_highest_vector(vmx->nested.pi_desc); 4206 if (max_irr > 0 && (max_irr & 0xf0) > (vppr & 0xf0)) 4207 return true; 4208 } 4209 4210 return false; 4211 } 4212 4213 /* 4214 * Per the Intel SDM's table "Priority Among Concurrent Events", with minor 4215 * edits to fill in missing examples, e.g. #DB due to split-lock accesses, 4216 * and less minor edits to splice in the priority of VMX Non-Root specific 4217 * events, e.g. MTF and NMI/INTR-window exiting. 4218 * 4219 * 1 Hardware Reset and Machine Checks 4220 * - RESET 4221 * - Machine Check 4222 * 4223 * 2 Trap on Task Switch 4224 * - T flag in TSS is set (on task switch) 4225 * 4226 * 3 External Hardware Interventions 4227 * - FLUSH 4228 * - STOPCLK 4229 * - SMI 4230 * - INIT 4231 * 4232 * 3.5 Monitor Trap Flag (MTF) VM-exit[1] 4233 * 4234 * 4 Traps on Previous Instruction 4235 * - Breakpoints 4236 * - Trap-class Debug Exceptions (#DB due to TF flag set, data/I-O 4237 * breakpoint, or #DB due to a split-lock access) 4238 * 4239 * 4.3 VMX-preemption timer expired VM-exit 4240 * 4241 * 4.6 NMI-window exiting VM-exit[2] 4242 * 4243 * 5 Nonmaskable Interrupts (NMI) 4244 * 4245 * 5.5 Interrupt-window exiting VM-exit and Virtual-interrupt delivery 4246 * 4247 * 6 Maskable Hardware Interrupts 4248 * 4249 * 7 Code Breakpoint Fault 4250 * 4251 * 8 Faults from Fetching Next Instruction 4252 * - Code-Segment Limit Violation 4253 * - Code Page Fault 4254 * - Control protection exception (missing ENDBRANCH at target of indirect 4255 * call or jump) 4256 * 4257 * 9 Faults from Decoding Next Instruction 4258 * - Instruction length > 15 bytes 4259 * - Invalid Opcode 4260 * - Coprocessor Not Available 4261 * 4262 *10 Faults on Executing Instruction 4263 * - Overflow 4264 * - Bound error 4265 * - Invalid TSS 4266 * - Segment Not Present 4267 * - Stack fault 4268 * - General Protection 4269 * - Data Page Fault 4270 * - Alignment Check 4271 * - x86 FPU Floating-point exception 4272 * - SIMD floating-point exception 4273 * - Virtualization exception 4274 * - Control protection exception 4275 * 4276 * [1] Per the "Monitor Trap Flag" section: System-management interrupts (SMIs), 4277 * INIT signals, and higher priority events take priority over MTF VM exits. 4278 * MTF VM exits take priority over debug-trap exceptions and lower priority 4279 * events. 4280 * 4281 * [2] Debug-trap exceptions and higher priority events take priority over VM exits 4282 * caused by the VMX-preemption timer. VM exits caused by the VMX-preemption 4283 * timer take priority over VM exits caused by the "NMI-window exiting" 4284 * VM-execution control and lower priority events. 4285 * 4286 * [3] Debug-trap exceptions and higher priority events take priority over VM exits 4287 * caused by "NMI-window exiting". VM exits caused by this control take 4288 * priority over non-maskable interrupts (NMIs) and lower priority events. 4289 * 4290 * [4] Virtual-interrupt delivery has the same priority as that of VM exits due to 4291 * the 1-setting of the "interrupt-window exiting" VM-execution control. Thus, 4292 * non-maskable interrupts (NMIs) and higher priority events take priority over 4293 * delivery of a virtual interrupt; delivery of a virtual interrupt takes 4294 * priority over external interrupts and lower priority events. 4295 */ 4296 static int vmx_check_nested_events(struct kvm_vcpu *vcpu) 4297 { 4298 struct kvm_lapic *apic = vcpu->arch.apic; 4299 struct vcpu_vmx *vmx = to_vmx(vcpu); 4300 /* 4301 * Only a pending nested run blocks a pending exception. If there is a 4302 * previously injected event, the pending exception occurred while said 4303 * event was being delivered and thus needs to be handled. 4304 */ 4305 bool block_nested_exceptions = vcpu->arch.nested_run_pending; 4306 /* 4307 * Events that don't require injection, i.e. that are virtualized by 4308 * hardware, aren't blocked by a pending VM-Enter as KVM doesn't need 4309 * to regain control in order to deliver the event, and hardware will 4310 * handle event ordering, e.g. with respect to injected exceptions. 4311 * 4312 * But, new events (not exceptions) are only recognized at instruction 4313 * boundaries. If an event needs reinjection, then KVM is handling a 4314 * VM-Exit that occurred _during_ instruction execution; new events, 4315 * irrespective of whether or not they're injected, are blocked until 4316 * the instruction completes. 4317 */ 4318 bool block_non_injected_events = kvm_event_needs_reinjection(vcpu); 4319 /* 4320 * Inject events are blocked by nested VM-Enter, as KVM is responsible 4321 * for managing priority between concurrent events, i.e. KVM needs to 4322 * wait until after VM-Enter completes to deliver injected events. 4323 */ 4324 bool block_nested_events = block_nested_exceptions || 4325 block_non_injected_events; 4326 4327 if (lapic_in_kernel(vcpu) && 4328 test_bit(KVM_APIC_INIT, &apic->pending_events)) { 4329 if (block_nested_events) 4330 return -EBUSY; 4331 nested_vmx_update_pending_dbg(vcpu); 4332 clear_bit(KVM_APIC_INIT, &apic->pending_events); 4333 if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED) 4334 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0); 4335 4336 /* MTF is discarded if the vCPU is in WFS. */ 4337 vmx->nested.mtf_pending = false; 4338 return 0; 4339 } 4340 4341 if (lapic_in_kernel(vcpu) && 4342 test_bit(KVM_APIC_SIPI, &apic->pending_events)) { 4343 if (block_nested_events) 4344 return -EBUSY; 4345 4346 clear_bit(KVM_APIC_SIPI, &apic->pending_events); 4347 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { 4348 nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0, 4349 apic->sipi_vector & 0xFFUL); 4350 return 0; 4351 } 4352 /* Fallthrough, the SIPI is completely ignored. */ 4353 } 4354 4355 /* 4356 * Process exceptions that are higher priority than Monitor Trap Flag: 4357 * fault-like exceptions, TSS T flag #DB (not emulated by KVM, but 4358 * could theoretically come in from userspace), and ICEBP (INT1). 4359 * 4360 * TODO: SMIs have higher priority than MTF and trap-like #DBs (except 4361 * for TSS T flag #DBs). KVM also doesn't save/restore pending MTF 4362 * across SMI/RSM as it should; that needs to be addressed in order to 4363 * prioritize SMI over MTF and trap-like #DBs. 4364 */ 4365 if (vcpu->arch.exception_vmexit.pending && 4366 !vmx_is_low_priority_db_trap(&vcpu->arch.exception_vmexit)) { 4367 if (block_nested_exceptions) 4368 return -EBUSY; 4369 4370 nested_vmx_inject_exception_vmexit(vcpu); 4371 return 0; 4372 } 4373 4374 if (vcpu->arch.exception.pending && 4375 !vmx_is_low_priority_db_trap(&vcpu->arch.exception)) { 4376 if (block_nested_exceptions) 4377 return -EBUSY; 4378 goto no_vmexit; 4379 } 4380 4381 if (vmx->nested.mtf_pending) { 4382 if (block_nested_events) 4383 return -EBUSY; 4384 nested_vmx_update_pending_dbg(vcpu); 4385 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0); 4386 return 0; 4387 } 4388 4389 if (vcpu->arch.exception_vmexit.pending) { 4390 if (block_nested_exceptions) 4391 return -EBUSY; 4392 4393 nested_vmx_inject_exception_vmexit(vcpu); 4394 return 0; 4395 } 4396 4397 if (vcpu->arch.exception.pending) { 4398 if (block_nested_exceptions) 4399 return -EBUSY; 4400 goto no_vmexit; 4401 } 4402 4403 if (nested_vmx_preemption_timer_pending(vcpu)) { 4404 if (block_nested_events) 4405 return -EBUSY; 4406 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); 4407 return 0; 4408 } 4409 4410 if (vcpu->arch.smi_pending && !is_smm(vcpu)) { 4411 if (block_nested_events) 4412 return -EBUSY; 4413 goto no_vmexit; 4414 } 4415 4416 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) { 4417 if (block_nested_events) 4418 return -EBUSY; 4419 if (!nested_exit_on_nmi(vcpu)) 4420 goto no_vmexit; 4421 4422 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, 4423 NMI_VECTOR | INTR_TYPE_NMI_INTR | 4424 INTR_INFO_VALID_MASK, 0); 4425 /* 4426 * The NMI-triggered VM exit counts as injection: 4427 * clear this one and block further NMIs. 4428 */ 4429 vcpu->arch.nmi_pending = 0; 4430 vmx_set_nmi_mask(vcpu, true); 4431 return 0; 4432 } 4433 4434 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) { 4435 int irq; 4436 4437 if (!nested_exit_on_intr(vcpu)) { 4438 if (block_nested_events) 4439 return -EBUSY; 4440 4441 goto no_vmexit; 4442 } 4443 4444 if (!nested_exit_intr_ack_set(vcpu)) { 4445 if (block_nested_events) 4446 return -EBUSY; 4447 4448 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); 4449 return 0; 4450 } 4451 4452 irq = kvm_cpu_get_extint(vcpu); 4453 if (irq != -1) { 4454 if (block_nested_events) 4455 return -EBUSY; 4456 4457 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 4458 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR | irq, 0); 4459 return 0; 4460 } 4461 4462 irq = kvm_apic_has_interrupt(vcpu); 4463 if (WARN_ON_ONCE(irq < 0)) 4464 goto no_vmexit; 4465 4466 /* 4467 * If the IRQ is L2's PI notification vector, process posted 4468 * interrupts for L2 instead of injecting VM-Exit, as the 4469 * detection/morphing architecturally occurs when the IRQ is 4470 * delivered to the CPU. Note, only interrupts that are routed 4471 * through the local APIC trigger posted interrupt processing, 4472 * and enabling posted interrupts requires ACK-on-exit. 4473 */ 4474 if (irq == vmx->nested.posted_intr_nv) { 4475 /* 4476 * Nested posted interrupts are delivered via RVI, i.e. 4477 * aren't injected by KVM, and so can be queued even if 4478 * manual event injection is disallowed. 4479 */ 4480 if (block_non_injected_events) 4481 return -EBUSY; 4482 4483 vmx->nested.pi_pending = true; 4484 kvm_apic_clear_irr(vcpu, irq); 4485 goto no_vmexit; 4486 } 4487 4488 if (block_nested_events) 4489 return -EBUSY; 4490 4491 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 4492 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR | irq, 0); 4493 4494 /* 4495 * ACK the interrupt _after_ emulating VM-Exit, as the IRQ must 4496 * be marked as in-service in vmcs01.GUEST_INTERRUPT_STATUS.SVI 4497 * if APICv is active. 4498 */ 4499 kvm_apic_ack_interrupt(vcpu, irq); 4500 return 0; 4501 } 4502 4503 no_vmexit: 4504 return vmx_complete_nested_posted_interrupt(vcpu); 4505 } 4506 4507 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) 4508 { 4509 ktime_t remaining = 4510 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); 4511 u64 value; 4512 4513 if (ktime_to_ns(remaining) <= 0) 4514 return 0; 4515 4516 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; 4517 do_div(value, 1000000); 4518 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; 4519 } 4520 4521 static bool is_vmcs12_ext_field(unsigned long field) 4522 { 4523 switch (field) { 4524 case GUEST_ES_SELECTOR: 4525 case GUEST_CS_SELECTOR: 4526 case GUEST_SS_SELECTOR: 4527 case GUEST_DS_SELECTOR: 4528 case GUEST_FS_SELECTOR: 4529 case GUEST_GS_SELECTOR: 4530 case GUEST_LDTR_SELECTOR: 4531 case GUEST_TR_SELECTOR: 4532 case GUEST_ES_LIMIT: 4533 case GUEST_CS_LIMIT: 4534 case GUEST_SS_LIMIT: 4535 case GUEST_DS_LIMIT: 4536 case GUEST_FS_LIMIT: 4537 case GUEST_GS_LIMIT: 4538 case GUEST_LDTR_LIMIT: 4539 case GUEST_TR_LIMIT: 4540 case GUEST_GDTR_LIMIT: 4541 case GUEST_IDTR_LIMIT: 4542 case GUEST_ES_AR_BYTES: 4543 case GUEST_DS_AR_BYTES: 4544 case GUEST_FS_AR_BYTES: 4545 case GUEST_GS_AR_BYTES: 4546 case GUEST_LDTR_AR_BYTES: 4547 case GUEST_TR_AR_BYTES: 4548 case GUEST_ES_BASE: 4549 case GUEST_CS_BASE: 4550 case GUEST_SS_BASE: 4551 case GUEST_DS_BASE: 4552 case GUEST_FS_BASE: 4553 case GUEST_GS_BASE: 4554 case GUEST_LDTR_BASE: 4555 case GUEST_TR_BASE: 4556 case GUEST_GDTR_BASE: 4557 case GUEST_IDTR_BASE: 4558 case GUEST_PENDING_DBG_EXCEPTIONS: 4559 case GUEST_BNDCFGS: 4560 return true; 4561 default: 4562 break; 4563 } 4564 4565 return false; 4566 } 4567 4568 static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, 4569 struct vmcs12 *vmcs12) 4570 { 4571 struct vcpu_vmx *vmx = to_vmx(vcpu); 4572 4573 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); 4574 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); 4575 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); 4576 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); 4577 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); 4578 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); 4579 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); 4580 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); 4581 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); 4582 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); 4583 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); 4584 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); 4585 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); 4586 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); 4587 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); 4588 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); 4589 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); 4590 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); 4591 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); 4592 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); 4593 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); 4594 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); 4595 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); 4596 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); 4597 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); 4598 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); 4599 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); 4600 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); 4601 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); 4602 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); 4603 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); 4604 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); 4605 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); 4606 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); 4607 vmcs12->guest_pending_dbg_exceptions = 4608 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); 4609 4610 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false; 4611 } 4612 4613 static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, 4614 struct vmcs12 *vmcs12) 4615 { 4616 struct vcpu_vmx *vmx = to_vmx(vcpu); 4617 int cpu; 4618 4619 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare) 4620 return; 4621 4622 4623 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01); 4624 4625 cpu = get_cpu(); 4626 vmx->loaded_vmcs = &vmx->nested.vmcs02; 4627 vmx_vcpu_load_vmcs(vcpu, cpu); 4628 4629 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); 4630 4631 vmx->loaded_vmcs = &vmx->vmcs01; 4632 vmx_vcpu_load_vmcs(vcpu, cpu); 4633 put_cpu(); 4634 } 4635 4636 /* 4637 * Update the guest state fields of vmcs12 to reflect changes that 4638 * occurred while L2 was running. (The "IA-32e mode guest" bit of the 4639 * VM-entry controls is also updated, since this is really a guest 4640 * state bit.) 4641 */ 4642 static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) 4643 { 4644 struct vcpu_vmx *vmx = to_vmx(vcpu); 4645 4646 if (nested_vmx_is_evmptr12_valid(vmx)) 4647 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); 4648 4649 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = 4650 !nested_vmx_is_evmptr12_valid(vmx); 4651 4652 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); 4653 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); 4654 4655 vmcs12->guest_rsp = kvm_rsp_read(vcpu); 4656 vmcs12->guest_rip = kvm_rip_read(vcpu); 4657 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); 4658 4659 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); 4660 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); 4661 4662 vmcs12->guest_interruptibility_info = 4663 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); 4664 4665 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) 4666 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; 4667 else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) 4668 vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI; 4669 else 4670 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; 4671 4672 if (nested_cpu_has_preemption_timer(vmcs12) && 4673 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER && 4674 !vcpu->arch.nested_run_pending) 4675 vmcs12->vmx_preemption_timer_value = 4676 vmx_get_preemption_timer_value(vcpu); 4677 4678 /* 4679 * In some cases (usually, nested EPT), L2 is allowed to change its 4680 * own CR3 without exiting. If it has changed it, we must keep it. 4681 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined 4682 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. 4683 * 4684 * Additionally, restore L2's PDPTR to vmcs12. 4685 */ 4686 if (enable_ept) { 4687 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); 4688 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { 4689 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); 4690 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); 4691 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); 4692 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); 4693 } 4694 } 4695 4696 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); 4697 4698 if (nested_cpu_has_vid(vmcs12)) 4699 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); 4700 4701 vmcs12->vm_entry_controls = 4702 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | 4703 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); 4704 4705 /* 4706 * Note! Save DR7, but intentionally don't grab DEBUGCTL from vmcs02. 4707 * Writes to DEBUGCTL that aren't intercepted by L1 are immediately 4708 * propagated to vmcs12 (see vmx_set_msr()), as the value loaded into 4709 * vmcs02 doesn't strictly track vmcs12. 4710 */ 4711 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) 4712 vmcs12->guest_dr7 = vcpu->arch.dr7; 4713 4714 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) 4715 vmcs12->guest_ia32_efer = vcpu->arch.efer; 4716 4717 vmcs_read_cet_state(&vmx->vcpu, &vmcs12->guest_s_cet, 4718 &vmcs12->guest_ssp, 4719 &vmcs12->guest_ssp_tbl); 4720 } 4721 4722 /* 4723 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits 4724 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), 4725 * and this function updates it to reflect the changes to the guest state while 4726 * L2 was running (and perhaps made some exits which were handled directly by L0 4727 * without going back to L1), and to reflect the exit reason. 4728 * Note that we do not have to copy here all VMCS fields, just those that 4729 * could have changed by the L2 guest or the exit - i.e., the guest-state and 4730 * exit-information fields only. Other fields are modified by L1 with VMWRITE, 4731 * which already writes to vmcs12 directly. 4732 */ 4733 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, 4734 u32 vm_exit_reason, u32 exit_intr_info, 4735 unsigned long exit_qualification, u32 exit_insn_len) 4736 { 4737 /* update exit information fields: */ 4738 vmcs12->vm_exit_reason = vm_exit_reason; 4739 if (vmx_get_exit_reason(vcpu).enclave_mode) 4740 vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE; 4741 vmcs12->exit_qualification = exit_qualification; 4742 4743 /* 4744 * On VM-Exit due to a failed VM-Entry, the VMCS isn't marked launched 4745 * and only EXIT_REASON and EXIT_QUALIFICATION are updated, all other 4746 * exit info fields are unmodified. 4747 */ 4748 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { 4749 vmcs12->launch_state = 1; 4750 4751 /* vm_entry_intr_info_field is cleared on exit. Emulate this 4752 * instead of reading the real value. */ 4753 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; 4754 4755 /* 4756 * Transfer the event that L0 or L1 may wanted to inject into 4757 * L2 to IDT_VECTORING_INFO_FIELD. 4758 */ 4759 vmcs12_save_pending_event(vcpu, vmcs12, 4760 vm_exit_reason, exit_intr_info); 4761 4762 vmcs12->vm_exit_intr_info = exit_intr_info; 4763 vmcs12->vm_exit_instruction_len = exit_insn_len; 4764 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); 4765 4766 /* 4767 * According to spec, there's no need to store the guest's 4768 * MSRs if the exit is due to a VM-entry failure that occurs 4769 * during or after loading the guest state. Since this exit 4770 * does not fall in that category, we need to save the MSRs. 4771 */ 4772 if (nested_vmx_store_msr(vcpu, 4773 vmcs12->vm_exit_msr_store_addr, 4774 vmcs12->vm_exit_msr_store_count)) 4775 nested_vmx_abort(vcpu, 4776 VMX_ABORT_SAVE_GUEST_MSR_FAIL); 4777 } 4778 } 4779 4780 /* 4781 * A part of what we need to when the nested L2 guest exits and we want to 4782 * run its L1 parent, is to reset L1's guest state to the host state specified 4783 * in vmcs12. 4784 * This function is to be called not only on normal nested exit, but also on 4785 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry 4786 * Failures During or After Loading Guest State"). 4787 * This function should be called when the active VMCS is L1's (vmcs01). 4788 */ 4789 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, 4790 struct vmcs12 *vmcs12) 4791 { 4792 enum vm_entry_failure_code ignored; 4793 struct kvm_segment seg; 4794 4795 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) 4796 vcpu->arch.efer = vmcs12->host_ia32_efer; 4797 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) 4798 vcpu->arch.efer |= (EFER_LMA | EFER_LME); 4799 else 4800 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); 4801 vmx_set_efer(vcpu, vcpu->arch.efer); 4802 4803 kvm_rsp_write(vcpu, vmcs12->host_rsp); 4804 kvm_rip_write(vcpu, vmcs12->host_rip); 4805 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); 4806 vmx_set_interrupt_shadow(vcpu, 0); 4807 4808 /* 4809 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't 4810 * actually changed, because vmx_set_cr0 refers to efer set above. 4811 * 4812 * CR0_GUEST_HOST_MASK is already set in the original vmcs01 4813 * (KVM doesn't change it); 4814 */ 4815 vcpu->arch.cr0_guest_owned_bits = vmx_l1_guest_owned_cr0_bits(); 4816 vmx_set_cr0(vcpu, vmcs12->host_cr0); 4817 4818 /* Same as above - no reason to call set_cr4_guest_host_mask(). */ 4819 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); 4820 vmx_set_cr4(vcpu, vmcs12->host_cr4); 4821 4822 nested_ept_uninit_mmu_context(vcpu); 4823 4824 /* 4825 * Only PDPTE load can fail as the value of cr3 was checked on entry and 4826 * couldn't have changed. 4827 */ 4828 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored)) 4829 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); 4830 4831 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false); 4832 4833 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); 4834 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); 4835 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); 4836 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); 4837 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); 4838 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); 4839 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); 4840 4841 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ 4842 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) 4843 vmcs_write64(GUEST_BNDCFGS, 0); 4844 4845 /* 4846 * Load CET state from host state if VM_EXIT_LOAD_CET_STATE is set. 4847 * otherwise CET state should be retained across VM-exit, i.e., 4848 * guest values should be propagated from vmcs12 to vmcs01. 4849 */ 4850 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_CET_STATE) 4851 vmcs_write_cet_state(vcpu, vmcs12->host_s_cet, vmcs12->host_ssp, 4852 vmcs12->host_ssp_tbl); 4853 else 4854 vmcs_write_cet_state(vcpu, vmcs12->guest_s_cet, vmcs12->guest_ssp, 4855 vmcs12->guest_ssp_tbl); 4856 4857 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { 4858 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); 4859 vcpu->arch.pat = vmcs12->host_ia32_pat; 4860 } 4861 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) && 4862 kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu))) 4863 WARN_ON_ONCE(__kvm_emulate_msr_write(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, 4864 vmcs12->host_ia32_perf_global_ctrl)); 4865 4866 /* Set L1 segment info according to Intel SDM 4867 27.5.2 Loading Host Segment and Descriptor-Table Registers */ 4868 seg = (struct kvm_segment) { 4869 .base = 0, 4870 .limit = 0xFFFFFFFF, 4871 .selector = vmcs12->host_cs_selector, 4872 .type = 11, 4873 .present = 1, 4874 .s = 1, 4875 .g = 1 4876 }; 4877 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) 4878 seg.l = 1; 4879 else 4880 seg.db = 1; 4881 __vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); 4882 seg = (struct kvm_segment) { 4883 .base = 0, 4884 .limit = 0xFFFFFFFF, 4885 .type = 3, 4886 .present = 1, 4887 .s = 1, 4888 .db = 1, 4889 .g = 1 4890 }; 4891 seg.selector = vmcs12->host_ds_selector; 4892 __vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); 4893 seg.selector = vmcs12->host_es_selector; 4894 __vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); 4895 seg.selector = vmcs12->host_ss_selector; 4896 __vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); 4897 seg.selector = vmcs12->host_fs_selector; 4898 seg.base = vmcs12->host_fs_base; 4899 __vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); 4900 seg.selector = vmcs12->host_gs_selector; 4901 seg.base = vmcs12->host_gs_base; 4902 __vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); 4903 seg = (struct kvm_segment) { 4904 .base = vmcs12->host_tr_base, 4905 .limit = 0x67, 4906 .selector = vmcs12->host_tr_selector, 4907 .type = 11, 4908 .present = 1 4909 }; 4910 __vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); 4911 4912 memset(&seg, 0, sizeof(seg)); 4913 seg.unusable = 1; 4914 __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR); 4915 4916 kvm_set_dr(vcpu, 7, 0x400); 4917 vmx_guest_debugctl_write(vcpu, 0); 4918 4919 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, 4920 vmcs12->vm_exit_msr_load_count)) 4921 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); 4922 4923 to_vt(vcpu)->emulation_required = vmx_emulation_required(vcpu); 4924 } 4925 4926 static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) 4927 { 4928 struct vmx_uret_msr *efer_msr; 4929 unsigned int i; 4930 4931 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) 4932 return vmcs_read64(GUEST_IA32_EFER); 4933 4934 if (cpu_has_load_ia32_efer()) 4935 return kvm_host.efer; 4936 4937 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { 4938 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) 4939 return vmx->msr_autoload.guest.val[i].value; 4940 } 4941 4942 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER); 4943 if (efer_msr) 4944 return efer_msr->data; 4945 4946 return kvm_host.efer; 4947 } 4948 4949 static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) 4950 { 4951 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 4952 struct vcpu_vmx *vmx = to_vmx(vcpu); 4953 struct vmx_msr_entry g, h; 4954 gpa_t gpa; 4955 u32 i, j; 4956 4957 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); 4958 4959 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { 4960 /* 4961 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set 4962 * as vmcs01.GUEST_DR7 contains a userspace defined value 4963 * and vcpu->arch.dr7 is not squirreled away before the 4964 * nested VMENTER (not worth adding a variable in nested_vmx). 4965 */ 4966 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) 4967 kvm_set_dr(vcpu, 7, DR7_FIXED_1); 4968 else 4969 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); 4970 } 4971 4972 /* Reload DEBUGCTL to ensure vmcs01 has a fresh FREEZE_IN_SMM value. */ 4973 vmx_reload_guest_debugctl(vcpu); 4974 4975 /* 4976 * Note that calling vmx_set_{efer,cr0,cr4} is important as they 4977 * handle a variety of side effects to KVM's software model. 4978 */ 4979 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); 4980 4981 vcpu->arch.cr0_guest_owned_bits = vmx_l1_guest_owned_cr0_bits(); 4982 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); 4983 4984 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); 4985 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); 4986 4987 nested_ept_uninit_mmu_context(vcpu); 4988 vcpu->arch.cr3 = vmx->nested.pre_vmenter_cr3; 4989 kvm_register_mark_available(vcpu, VCPU_REG_CR3); 4990 4991 /* 4992 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs 4993 * from vmcs01 (if necessary). The PDPTRs are not loaded on 4994 * VMFail, like everything else we just need to ensure our 4995 * software model is up-to-date. 4996 */ 4997 if (enable_ept && is_pae_paging(vcpu)) 4998 ept_save_pdptrs(vcpu); 4999 5000 kvm_mmu_reset_context(vcpu); 5001 5002 /* 5003 * This nasty bit of open coding is a compromise between blindly 5004 * loading L1's MSRs using the exit load lists (incorrect emulation 5005 * of VMFail), leaving the nested VM's MSRs in the software model 5006 * (incorrect behavior) and snapshotting the modified MSRs (too 5007 * expensive since the lists are unbound by hardware). For each 5008 * MSR that was (prematurely) loaded from the nested VMEntry load 5009 * list, reload it from the exit load list if it exists and differs 5010 * from the guest value. The intent is to stuff host state as 5011 * silently as possible, not to fully process the exit load list. 5012 */ 5013 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { 5014 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); 5015 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { 5016 pr_debug_ratelimited( 5017 "%s read MSR index failed (%u, 0x%08llx)\n", 5018 __func__, i, gpa); 5019 goto vmabort; 5020 } 5021 5022 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { 5023 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); 5024 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { 5025 pr_debug_ratelimited( 5026 "%s read MSR failed (%u, 0x%08llx)\n", 5027 __func__, j, gpa); 5028 goto vmabort; 5029 } 5030 if (h.index != g.index) 5031 continue; 5032 if (h.value == g.value) 5033 break; 5034 5035 if (nested_vmx_load_msr_check(vcpu, &h)) { 5036 pr_debug_ratelimited( 5037 "%s check failed (%u, 0x%x, 0x%x)\n", 5038 __func__, j, h.index, h.reserved); 5039 goto vmabort; 5040 } 5041 5042 if (kvm_emulate_msr_write(vcpu, h.index, h.value)) { 5043 pr_debug_ratelimited( 5044 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", 5045 __func__, j, h.index, h.value); 5046 goto vmabort; 5047 } 5048 } 5049 } 5050 5051 return; 5052 5053 vmabort: 5054 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); 5055 } 5056 5057 /* 5058 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 5059 * and modify vmcs12 to make it see what it would expect to see there if 5060 * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) 5061 */ 5062 void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, 5063 u32 exit_intr_info, unsigned long exit_qualification, 5064 u32 exit_insn_len) 5065 { 5066 struct vcpu_vmx *vmx = to_vmx(vcpu); 5067 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 5068 5069 /* Pending MTF traps are discarded on VM-Exit. */ 5070 vmx->nested.mtf_pending = false; 5071 5072 /* trying to cancel vmlaunch/vmresume is a bug */ 5073 kvm_warn_on_nested_run_pending(vcpu); 5074 5075 #ifdef CONFIG_KVM_HYPERV 5076 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { 5077 /* 5078 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map 5079 * Enlightened VMCS after migration and we still need to 5080 * do that when something is forcing L2->L1 exit prior to 5081 * the first L2 run. 5082 */ 5083 (void)nested_get_evmcs_page(vcpu); 5084 } 5085 #endif 5086 5087 /* Service pending TLB flush requests for L2 before switching to L1. */ 5088 kvm_service_local_tlb_flush_requests(vcpu); 5089 5090 /* 5091 * VCPU_REG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between 5092 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are 5093 * up-to-date before switching to L1. 5094 */ 5095 if (enable_ept && is_pae_paging(vcpu)) 5096 vmx_ept_load_pdptrs(vcpu); 5097 5098 leave_guest_mode(vcpu); 5099 5100 if (nested_cpu_has_preemption_timer(vmcs12)) 5101 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); 5102 5103 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) { 5104 vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; 5105 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING)) 5106 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; 5107 } 5108 5109 if (likely(!vmx->fail)) { 5110 sync_vmcs02_to_vmcs12(vcpu, vmcs12); 5111 5112 if (vm_exit_reason != -1) 5113 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason, 5114 exit_intr_info, exit_qualification, 5115 exit_insn_len); 5116 5117 /* 5118 * Must happen outside of sync_vmcs02_to_vmcs12() as it will 5119 * also be used to capture vmcs12 cache as part of 5120 * capturing nVMX state for snapshot (migration). 5121 * 5122 * Otherwise, this flush will dirty guest memory at a 5123 * point it is already assumed by user-space to be 5124 * immutable. 5125 */ 5126 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); 5127 } else { 5128 /* 5129 * The only expected VM-instruction error is "VM entry with 5130 * invalid control field(s)." Anything else indicates a 5131 * problem with L0. 5132 */ 5133 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != 5134 VMXERR_ENTRY_INVALID_CONTROL_FIELD); 5135 5136 /* VM-Fail at VM-Entry means KVM missed a consistency check. */ 5137 WARN_ON_ONCE(warn_on_missed_cc); 5138 } 5139 5140 /* 5141 * Drop events/exceptions that were queued for re-injection to L2 5142 * (picked up via vmx_complete_interrupts()), as well as exceptions 5143 * that were pending for L2. Note, this must NOT be hoisted above 5144 * prepare_vmcs12(), events/exceptions queued for re-injection need to 5145 * be captured in vmcs12 (see vmcs12_save_pending_event()). 5146 */ 5147 vcpu->arch.nmi_injected = false; 5148 kvm_clear_exception_queue(vcpu); 5149 kvm_clear_interrupt_queue(vcpu); 5150 5151 vmx_switch_vmcs(vcpu, &vmx->vmcs01); 5152 5153 kvm_nested_vmexit_handle_ibrs(vcpu); 5154 5155 /* 5156 * Update any VMCS fields that might have changed while vmcs02 was the 5157 * active VMCS. The tracking is per-vCPU, not per-VMCS. 5158 */ 5159 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.nr); 5160 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); 5161 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); 5162 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); 5163 if (kvm_caps.has_tsc_control) 5164 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); 5165 5166 nested_put_vmcs12_pages(vcpu); 5167 5168 if ((vm_exit_reason != -1) && 5169 (enable_shadow_vmcs || nested_vmx_is_evmptr12_valid(vmx))) 5170 vmx->nested.need_vmcs12_to_shadow_sync = true; 5171 5172 /* in case we halted in L2 */ 5173 kvm_set_mp_state(vcpu, KVM_MP_STATE_RUNNABLE); 5174 5175 if (likely(!vmx->fail)) { 5176 if (vm_exit_reason != -1) 5177 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, 5178 vmcs12->exit_qualification, 5179 vmcs12->idt_vectoring_info_field, 5180 vmcs12->vm_exit_intr_info, 5181 vmcs12->vm_exit_intr_error_code, 5182 KVM_ISA_VMX); 5183 5184 load_vmcs12_host_state(vcpu, vmcs12); 5185 5186 /* 5187 * Process events if an injectable IRQ or NMI is pending, even 5188 * if the event is blocked (RFLAGS.IF is cleared on VM-Exit). 5189 * If an event became pending while L2 was active, KVM needs to 5190 * either inject the event or request an IRQ/NMI window. SMIs 5191 * don't need to be processed as SMM is mutually exclusive with 5192 * non-root mode. INIT/SIPI don't need to be checked as INIT 5193 * is blocked post-VMXON, and SIPIs are ignored. 5194 */ 5195 if (kvm_cpu_has_injectable_intr(vcpu) || vcpu->arch.nmi_pending) 5196 kvm_make_request(KVM_REQ_EVENT, vcpu); 5197 return; 5198 } 5199 5200 /* 5201 * After an early L2 VM-entry failure, we're now back 5202 * in L1 which thinks it just finished a VMLAUNCH or 5203 * VMRESUME instruction, so we need to set the failure 5204 * flag and the VM-instruction error field of the VMCS 5205 * accordingly, and skip the emulated instruction. 5206 */ 5207 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); 5208 5209 /* 5210 * Restore L1's host state to KVM's software model. We're here 5211 * because a consistency check was caught by hardware, which 5212 * means some amount of guest state has been propagated to KVM's 5213 * model and needs to be unwound to the host's state. 5214 */ 5215 nested_vmx_restore_host_state(vcpu); 5216 5217 vmx->fail = 0; 5218 } 5219 5220 static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu) 5221 { 5222 kvm_clear_request(KVM_REQ_TRIPLE_FAULT, vcpu); 5223 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); 5224 } 5225 5226 /* 5227 * Decode the memory-address operand of a vmx instruction, as recorded on an 5228 * exit caused by such an instruction (run by a guest hypervisor). 5229 * On success, returns 0. When the operand is invalid, returns 1 and throws 5230 * #UD, #GP, or #SS. 5231 */ 5232 int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification, 5233 u32 vmx_instruction_info, bool wr, int len, gva_t *ret) 5234 { 5235 gva_t off; 5236 bool exn; 5237 struct kvm_segment s; 5238 5239 /* 5240 * According to Vol. 3B, "Information for VM Exits Due to Instruction 5241 * Execution", on an exit, vmx_instruction_info holds most of the 5242 * addressing components of the operand. Only the displacement part 5243 * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). 5244 * For how an actual address is calculated from all these components, 5245 * refer to Vol. 1, "Operand Addressing". 5246 */ 5247 int scaling = vmx_instruction_info & 3; 5248 int addr_size = (vmx_instruction_info >> 7) & 7; 5249 bool is_reg = vmx_instruction_info & (1u << 10); 5250 int seg_reg = (vmx_instruction_info >> 15) & 7; 5251 int index_reg = (vmx_instruction_info >> 18) & 0xf; 5252 bool index_is_valid = !(vmx_instruction_info & (1u << 22)); 5253 int base_reg = (vmx_instruction_info >> 23) & 0xf; 5254 bool base_is_valid = !(vmx_instruction_info & (1u << 27)); 5255 5256 if (is_reg) { 5257 kvm_queue_exception(vcpu, UD_VECTOR); 5258 return 1; 5259 } 5260 5261 /* Addr = segment_base + offset */ 5262 /* offset = base + [index * scale] + displacement */ 5263 off = exit_qualification; /* holds the displacement */ 5264 if (addr_size == 1) 5265 off = (gva_t)sign_extend64(off, 31); 5266 else if (addr_size == 0) 5267 off = (gva_t)sign_extend64(off, 15); 5268 if (base_is_valid) 5269 off += kvm_register_read(vcpu, base_reg); 5270 if (index_is_valid) 5271 off += kvm_register_read(vcpu, index_reg) << scaling; 5272 vmx_get_segment(vcpu, &s, seg_reg); 5273 5274 /* 5275 * The effective address, i.e. @off, of a memory operand is truncated 5276 * based on the address size of the instruction. Note that this is 5277 * the *effective address*, i.e. the address prior to accounting for 5278 * the segment's base. 5279 */ 5280 if (addr_size == 1) /* 32 bit */ 5281 off &= 0xffffffff; 5282 else if (addr_size == 0) /* 16 bit */ 5283 off &= 0xffff; 5284 5285 /* Checks for #GP/#SS exceptions. */ 5286 exn = false; 5287 if (is_long_mode(vcpu)) { 5288 /* 5289 * The virtual/linear address is never truncated in 64-bit 5290 * mode, e.g. a 32-bit address size can yield a 64-bit virtual 5291 * address when using FS/GS with a non-zero base. 5292 */ 5293 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS) 5294 *ret = s.base + off; 5295 else 5296 *ret = off; 5297 5298 *ret = vmx_get_untagged_addr(vcpu, *ret, 0); 5299 /* Long mode: #GP(0)/#SS(0) if the memory address is in a 5300 * non-canonical form. This is the only check on the memory 5301 * destination for long mode! 5302 */ 5303 exn = is_noncanonical_address(*ret, vcpu, 0); 5304 } else { 5305 /* 5306 * When not in long mode, the virtual/linear address is 5307 * unconditionally truncated to 32 bits regardless of the 5308 * address size. 5309 */ 5310 *ret = (s.base + off) & 0xffffffff; 5311 5312 /* Protected mode: apply checks for segment validity in the 5313 * following order: 5314 * - segment type check (#GP(0) may be thrown) 5315 * - usability check (#GP(0)/#SS(0)) 5316 * - limit check (#GP(0)/#SS(0)) 5317 */ 5318 if (wr) 5319 /* #GP(0) if the destination operand is located in a 5320 * read-only data segment or any code segment. 5321 */ 5322 exn = ((s.type & 0xa) == 0 || (s.type & 8)); 5323 else 5324 /* #GP(0) if the source operand is located in an 5325 * execute-only code segment 5326 */ 5327 exn = ((s.type & 0xa) == 8); 5328 if (exn) { 5329 kvm_queue_exception_e(vcpu, GP_VECTOR, 0); 5330 return 1; 5331 } 5332 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. 5333 */ 5334 exn = (s.unusable != 0); 5335 5336 /* 5337 * Protected mode: #GP(0)/#SS(0) if the memory operand is 5338 * outside the segment limit. All CPUs that support VMX ignore 5339 * limit checks for flat segments, i.e. segments with base==0, 5340 * limit==0xffffffff and of type expand-up data or code. 5341 */ 5342 if (!(s.base == 0 && s.limit == 0xffffffff && 5343 ((s.type & 8) || !(s.type & 4)))) 5344 exn = exn || ((u64)off + len - 1 > s.limit); 5345 } 5346 if (exn) { 5347 kvm_queue_exception_e(vcpu, 5348 seg_reg == VCPU_SREG_SS ? 5349 SS_VECTOR : GP_VECTOR, 5350 0); 5351 return 1; 5352 } 5353 5354 return 0; 5355 } 5356 5357 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer, 5358 int *ret) 5359 { 5360 gva_t gva; 5361 struct x86_exception e; 5362 int r; 5363 5364 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), 5365 vmcs_read32(VMX_INSTRUCTION_INFO), false, 5366 sizeof(*vmpointer), &gva)) { 5367 *ret = 1; 5368 return -EINVAL; 5369 } 5370 5371 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e); 5372 if (r != X86EMUL_CONTINUE) { 5373 *ret = kvm_handle_memory_failure(vcpu, r, &e); 5374 return -EINVAL; 5375 } 5376 5377 return 0; 5378 } 5379 5380 /* 5381 * Allocate a shadow VMCS and associate it with the currently loaded 5382 * VMCS, unless such a shadow VMCS already exists. The newly allocated 5383 * VMCS is also VMCLEARed, so that it is ready for use. 5384 */ 5385 static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) 5386 { 5387 struct vcpu_vmx *vmx = to_vmx(vcpu); 5388 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; 5389 5390 /* 5391 * KVM allocates a shadow VMCS only when L1 executes VMXON and frees it 5392 * when L1 executes VMXOFF or the vCPU is forced out of nested 5393 * operation. VMXON faults if the CPU is already post-VMXON, so it 5394 * should be impossible to already have an allocated shadow VMCS. KVM 5395 * doesn't support virtualization of VMCS shadowing, so vmcs01 should 5396 * always be the loaded VMCS. 5397 */ 5398 if (WARN_ON(loaded_vmcs != &vmx->vmcs01 || loaded_vmcs->shadow_vmcs)) 5399 return loaded_vmcs->shadow_vmcs; 5400 5401 loaded_vmcs->shadow_vmcs = alloc_vmcs(true); 5402 if (loaded_vmcs->shadow_vmcs) 5403 vmcs_clear(loaded_vmcs->shadow_vmcs); 5404 5405 return loaded_vmcs->shadow_vmcs; 5406 } 5407 5408 static int enter_vmx_operation(struct kvm_vcpu *vcpu) 5409 { 5410 struct vcpu_vmx *vmx = to_vmx(vcpu); 5411 int r; 5412 5413 r = alloc_loaded_vmcs(&vmx->nested.vmcs02); 5414 if (r < 0) 5415 goto out_vmcs02; 5416 5417 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); 5418 if (!vmx->nested.cached_vmcs12) 5419 goto out_cached_vmcs12; 5420 5421 vmx->nested.shadow_vmcs12_cache.gpa = INVALID_GPA; 5422 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); 5423 if (!vmx->nested.cached_shadow_vmcs12) 5424 goto out_cached_shadow_vmcs12; 5425 5426 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) 5427 goto out_shadow_vmcs; 5428 5429 hrtimer_setup(&vmx->nested.preemption_timer, vmx_preemption_timer_fn, CLOCK_MONOTONIC, 5430 HRTIMER_MODE_ABS_PINNED); 5431 5432 vmx->nested.vpid02 = allocate_vpid(); 5433 5434 vmx->nested.vmcs02_initialized = false; 5435 vmx->nested.vmxon = true; 5436 5437 if (vmx_pt_mode_is_host_guest()) { 5438 vmx->pt_desc.guest.ctl = 0; 5439 pt_update_intercept_for_msr(vcpu); 5440 } 5441 5442 return 0; 5443 5444 out_shadow_vmcs: 5445 kfree(vmx->nested.cached_shadow_vmcs12); 5446 5447 out_cached_shadow_vmcs12: 5448 kfree(vmx->nested.cached_vmcs12); 5449 5450 out_cached_vmcs12: 5451 free_loaded_vmcs(&vmx->nested.vmcs02); 5452 5453 out_vmcs02: 5454 return -ENOMEM; 5455 } 5456 5457 /* Emulate the VMXON instruction. */ 5458 static int handle_vmxon(struct kvm_vcpu *vcpu) 5459 { 5460 int ret; 5461 gpa_t vmptr; 5462 uint32_t revision; 5463 struct vcpu_vmx *vmx = to_vmx(vcpu); 5464 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED 5465 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; 5466 5467 /* 5468 * Manually check CR4.VMXE checks, KVM must force CR4.VMXE=1 to enter 5469 * the guest and so cannot rely on hardware to perform the check, 5470 * which has higher priority than VM-Exit (see Intel SDM's pseudocode 5471 * for VMXON). 5472 * 5473 * Rely on hardware for the other pre-VM-Exit checks, CR0.PE=1, !VM86 5474 * and !COMPATIBILITY modes. For an unrestricted guest, KVM doesn't 5475 * force any of the relevant guest state. For a restricted guest, KVM 5476 * does force CR0.PE=1, but only to also force VM86 in order to emulate 5477 * Real Mode, and so there's no need to check CR0.PE manually. 5478 */ 5479 if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_VMXE)) { 5480 kvm_queue_exception(vcpu, UD_VECTOR); 5481 return 1; 5482 } 5483 5484 /* 5485 * The CPL is checked for "not in VMX operation" and for "in VMX root", 5486 * and has higher priority than the VM-Fail due to being post-VMXON, 5487 * i.e. VMXON #GPs outside of VMX non-root if CPL!=0. In VMX non-root, 5488 * VMXON causes VM-Exit and KVM unconditionally forwards VMXON VM-Exits 5489 * from L2 to L1, i.e. there's no need to check for the vCPU being in 5490 * VMX non-root. 5491 * 5492 * Forwarding the VM-Exit unconditionally, i.e. without performing the 5493 * #UD checks (see above), is functionally ok because KVM doesn't allow 5494 * L1 to run L2 without CR4.VMXE=0, and because KVM never modifies L2's 5495 * CR0 or CR4, i.e. it's L2's responsibility to emulate #UDs that are 5496 * missed by hardware due to shadowing CR0 and/or CR4. 5497 */ 5498 if (vmx_get_cpl(vcpu)) { 5499 kvm_inject_gp(vcpu, 0); 5500 return 1; 5501 } 5502 5503 if (vmx->nested.vmxon) 5504 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); 5505 5506 /* 5507 * Invalid CR0/CR4 generates #GP. These checks are performed if and 5508 * only if the vCPU isn't already in VMX operation, i.e. effectively 5509 * have lower priority than the VM-Fail above. 5510 */ 5511 if (!nested_host_cr0_valid(vcpu, kvm_read_cr0(vcpu)) || 5512 !nested_host_cr4_valid(vcpu, kvm_read_cr4(vcpu))) { 5513 kvm_inject_gp(vcpu, 0); 5514 return 1; 5515 } 5516 5517 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) 5518 != VMXON_NEEDED_FEATURES) { 5519 kvm_inject_gp(vcpu, 0); 5520 return 1; 5521 } 5522 5523 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret)) 5524 return ret; 5525 5526 /* 5527 * SDM 3: 24.11.5 5528 * The first 4 bytes of VMXON region contain the supported 5529 * VMCS revision identifier 5530 * 5531 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; 5532 * which replaces physical address width with 32 5533 */ 5534 if (!page_address_valid(vcpu, vmptr)) 5535 return nested_vmx_failInvalid(vcpu); 5536 5537 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) || 5538 revision != VMCS12_REVISION) 5539 return nested_vmx_failInvalid(vcpu); 5540 5541 vmx->nested.vmxon_ptr = vmptr; 5542 ret = enter_vmx_operation(vcpu); 5543 if (ret) 5544 return ret; 5545 5546 return nested_vmx_succeed(vcpu); 5547 } 5548 5549 static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) 5550 { 5551 struct vcpu_vmx *vmx = to_vmx(vcpu); 5552 5553 if (vmx->nested.current_vmptr == INVALID_GPA) 5554 return; 5555 5556 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); 5557 5558 if (enable_shadow_vmcs) { 5559 /* copy to memory all shadowed fields in case 5560 they were modified */ 5561 copy_shadow_to_vmcs12(vmx); 5562 vmx_disable_shadow_vmcs(vmx); 5563 } 5564 vmx->nested.posted_intr_nv = -1; 5565 5566 /* Flush VMCS12 to guest memory */ 5567 kvm_vcpu_write_guest_page(vcpu, 5568 vmx->nested.current_vmptr >> PAGE_SHIFT, 5569 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); 5570 5571 kvm_mmu_free_roots(vcpu->kvm, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); 5572 5573 vmx->nested.current_vmptr = INVALID_GPA; 5574 } 5575 5576 /* Emulate the VMXOFF instruction */ 5577 static int handle_vmxoff(struct kvm_vcpu *vcpu) 5578 { 5579 if (!nested_vmx_check_permission(vcpu)) 5580 return 1; 5581 5582 free_nested(vcpu); 5583 5584 if (kvm_apic_has_pending_init_or_sipi(vcpu)) 5585 kvm_make_request(KVM_REQ_EVENT, vcpu); 5586 5587 return nested_vmx_succeed(vcpu); 5588 } 5589 5590 /* Emulate the VMCLEAR instruction */ 5591 static int handle_vmclear(struct kvm_vcpu *vcpu) 5592 { 5593 struct vcpu_vmx *vmx = to_vmx(vcpu); 5594 u32 zero = 0; 5595 gpa_t vmptr; 5596 int r; 5597 5598 if (!nested_vmx_check_permission(vcpu)) 5599 return 1; 5600 5601 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) 5602 return r; 5603 5604 if (!page_address_valid(vcpu, vmptr)) 5605 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); 5606 5607 if (vmptr == vmx->nested.vmxon_ptr) 5608 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); 5609 5610 if (likely(!nested_evmcs_handle_vmclear(vcpu, vmptr))) { 5611 if (vmptr == vmx->nested.current_vmptr) 5612 nested_release_vmcs12(vcpu); 5613 5614 /* 5615 * Silently ignore memory errors on VMCLEAR, Intel's pseudocode 5616 * for VMCLEAR includes a "ensure that data for VMCS referenced 5617 * by the operand is in memory" clause that guards writes to 5618 * memory, i.e. doing nothing for I/O is architecturally valid. 5619 * 5620 * FIXME: Suppress failures if and only if no memslot is found, 5621 * i.e. exit to userspace if __copy_to_user() fails. 5622 */ 5623 (void)kvm_vcpu_write_guest(vcpu, 5624 vmptr + offsetof(struct vmcs12, 5625 launch_state), 5626 &zero, sizeof(zero)); 5627 } 5628 5629 return nested_vmx_succeed(vcpu); 5630 } 5631 5632 /* Emulate the VMLAUNCH instruction */ 5633 static int handle_vmlaunch(struct kvm_vcpu *vcpu) 5634 { 5635 return nested_vmx_run(vcpu, true); 5636 } 5637 5638 /* Emulate the VMRESUME instruction */ 5639 static int handle_vmresume(struct kvm_vcpu *vcpu) 5640 { 5641 5642 return nested_vmx_run(vcpu, false); 5643 } 5644 5645 static int handle_vmread(struct kvm_vcpu *vcpu) 5646 { 5647 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) 5648 : get_vmcs12(vcpu); 5649 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5650 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); 5651 struct vcpu_vmx *vmx = to_vmx(vcpu); 5652 struct x86_exception e; 5653 unsigned long field; 5654 u64 value; 5655 gva_t gva = 0; 5656 short offset; 5657 int len, r; 5658 5659 if (!nested_vmx_check_permission(vcpu)) 5660 return 1; 5661 5662 /* Decode instruction info and find the field to read */ 5663 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); 5664 5665 if (!nested_vmx_is_evmptr12_valid(vmx)) { 5666 /* 5667 * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA, 5668 * any VMREAD sets the ALU flags for VMfailInvalid. 5669 */ 5670 if (vmx->nested.current_vmptr == INVALID_GPA || 5671 (is_guest_mode(vcpu) && 5672 get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA)) 5673 return nested_vmx_failInvalid(vcpu); 5674 5675 offset = get_vmcs12_field_offset(field); 5676 if (offset < 0) 5677 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 5678 5679 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field)) 5680 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); 5681 5682 /* Read the field, zero-extended to a u64 value */ 5683 value = vmcs12_read_any(vmcs12, field, offset); 5684 } else { 5685 /* 5686 * Hyper-V TLFS (as of 6.0b) explicitly states, that while an 5687 * enlightened VMCS is active VMREAD/VMWRITE instructions are 5688 * unsupported. Unfortunately, certain versions of Windows 11 5689 * don't comply with this requirement which is not enforced in 5690 * genuine Hyper-V. Allow VMREAD from an enlightened VMCS as a 5691 * workaround, as misbehaving guests will panic on VM-Fail. 5692 * Note, enlightened VMCS is incompatible with shadow VMCS so 5693 * all VMREADs from L2 should go to L1. 5694 */ 5695 if (WARN_ON_ONCE(is_guest_mode(vcpu))) 5696 return nested_vmx_failInvalid(vcpu); 5697 5698 offset = evmcs_field_offset(field, NULL); 5699 if (offset < 0) 5700 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 5701 5702 /* Read the field, zero-extended to a u64 value */ 5703 value = evmcs_read_any(nested_vmx_evmcs(vmx), field, offset); 5704 } 5705 5706 /* 5707 * Now copy part of this value to register or memory, as requested. 5708 * Note that the number of bits actually copied is 32 or 64 depending 5709 * on the guest's mode (32 or 64 bit), not on the given field's length. 5710 */ 5711 if (instr_info & BIT(10)) { 5712 kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value); 5713 } else { 5714 len = is_64_bit_mode(vcpu) ? 8 : 4; 5715 if (get_vmx_mem_address(vcpu, exit_qualification, 5716 instr_info, true, len, &gva)) 5717 return 1; 5718 /* _system ok, nested_vmx_check_permission has verified cpl=0 */ 5719 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e); 5720 if (r != X86EMUL_CONTINUE) 5721 return kvm_handle_memory_failure(vcpu, r, &e); 5722 } 5723 5724 return nested_vmx_succeed(vcpu); 5725 } 5726 5727 static bool is_shadow_field_rw(unsigned long field) 5728 { 5729 switch (field) { 5730 #define SHADOW_FIELD_RW(x, y) case x: 5731 #include "vmcs_shadow_fields.h" 5732 return true; 5733 default: 5734 break; 5735 } 5736 return false; 5737 } 5738 5739 static bool is_shadow_field_ro(unsigned long field) 5740 { 5741 switch (field) { 5742 #define SHADOW_FIELD_RO(x, y) case x: 5743 #include "vmcs_shadow_fields.h" 5744 return true; 5745 default: 5746 break; 5747 } 5748 return false; 5749 } 5750 5751 static int handle_vmwrite(struct kvm_vcpu *vcpu) 5752 { 5753 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) 5754 : get_vmcs12(vcpu); 5755 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 5756 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); 5757 struct vcpu_vmx *vmx = to_vmx(vcpu); 5758 struct x86_exception e; 5759 unsigned long field; 5760 short offset; 5761 gva_t gva; 5762 int len, r; 5763 5764 /* 5765 * The value to write might be 32 or 64 bits, depending on L1's long 5766 * mode, and eventually we need to write that into a field of several 5767 * possible lengths. The code below first zero-extends the value to 64 5768 * bit (value), and then copies only the appropriate number of 5769 * bits into the vmcs12 field. 5770 */ 5771 u64 value = 0; 5772 5773 if (!nested_vmx_check_permission(vcpu)) 5774 return 1; 5775 5776 /* 5777 * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA, 5778 * any VMWRITE sets the ALU flags for VMfailInvalid. 5779 */ 5780 if (vmx->nested.current_vmptr == INVALID_GPA || 5781 (is_guest_mode(vcpu) && 5782 get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA)) 5783 return nested_vmx_failInvalid(vcpu); 5784 5785 if (instr_info & BIT(10)) 5786 value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf)); 5787 else { 5788 len = is_64_bit_mode(vcpu) ? 8 : 4; 5789 if (get_vmx_mem_address(vcpu, exit_qualification, 5790 instr_info, false, len, &gva)) 5791 return 1; 5792 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e); 5793 if (r != X86EMUL_CONTINUE) 5794 return kvm_handle_memory_failure(vcpu, r, &e); 5795 } 5796 5797 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); 5798 5799 offset = get_vmcs12_field_offset(field); 5800 if (offset < 0) 5801 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 5802 5803 /* 5804 * If the vCPU supports "VMWRITE to any supported field in the 5805 * VMCS," then the "read-only" fields are actually read/write. 5806 */ 5807 if (vmcs_field_readonly(field) && 5808 !nested_cpu_has_vmwrite_any_field(vcpu)) 5809 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); 5810 5811 /* 5812 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties 5813 * vmcs12, else we may crush a field or consume a stale value. 5814 */ 5815 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) 5816 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); 5817 5818 /* 5819 * Some Intel CPUs intentionally drop the reserved bits of the AR byte 5820 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM 5821 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE 5822 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD 5823 * from L1 will return a different value than VMREAD from L2 (L1 sees 5824 * the stripped down value, L2 sees the full value as stored by KVM). 5825 */ 5826 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES) 5827 value &= 0x1f0ff; 5828 5829 vmcs12_write_any(vmcs12, field, offset, value); 5830 5831 /* 5832 * Do not track vmcs12 dirty-state if in guest-mode as we actually 5833 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated 5834 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't 5835 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path. 5836 */ 5837 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) { 5838 /* 5839 * L1 can read these fields without exiting, ensure the 5840 * shadow VMCS is up-to-date. 5841 */ 5842 if (enable_shadow_vmcs && is_shadow_field_ro(field)) { 5843 preempt_disable(); 5844 vmcs_load(vmx->vmcs01.shadow_vmcs); 5845 5846 __vmcs_writel(field, value); 5847 5848 vmcs_clear(vmx->vmcs01.shadow_vmcs); 5849 vmcs_load(vmx->loaded_vmcs->vmcs); 5850 preempt_enable(); 5851 } 5852 vmx->nested.dirty_vmcs12 = true; 5853 } 5854 5855 return nested_vmx_succeed(vcpu); 5856 } 5857 5858 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) 5859 { 5860 vmx->nested.current_vmptr = vmptr; 5861 if (enable_shadow_vmcs) { 5862 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); 5863 vmcs_write64(VMCS_LINK_POINTER, 5864 __pa(vmx->vmcs01.shadow_vmcs)); 5865 vmx->nested.need_vmcs12_to_shadow_sync = true; 5866 } 5867 vmx->nested.dirty_vmcs12 = true; 5868 vmx->nested.force_msr_bitmap_recalc = true; 5869 } 5870 5871 /* Emulate the VMPTRLD instruction */ 5872 static int handle_vmptrld(struct kvm_vcpu *vcpu) 5873 { 5874 struct vcpu_vmx *vmx = to_vmx(vcpu); 5875 gpa_t vmptr; 5876 int r; 5877 5878 if (!nested_vmx_check_permission(vcpu)) 5879 return 1; 5880 5881 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) 5882 return r; 5883 5884 if (!page_address_valid(vcpu, vmptr)) 5885 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); 5886 5887 if (vmptr == vmx->nested.vmxon_ptr) 5888 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); 5889 5890 /* Forbid normal VMPTRLD if Enlightened version was used */ 5891 if (nested_vmx_is_evmptr12_valid(vmx)) 5892 return 1; 5893 5894 if (vmx->nested.current_vmptr != vmptr) { 5895 struct gfn_to_hva_cache *ghc = &vmx->nested.vmcs12_cache; 5896 struct vmcs_hdr hdr; 5897 5898 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, vmptr, VMCS12_SIZE)) { 5899 /* 5900 * Reads from an unbacked page return all 1s, 5901 * which means that the 32 bits located at the 5902 * given physical address won't match the required 5903 * VMCS12_REVISION identifier. 5904 */ 5905 return nested_vmx_fail(vcpu, 5906 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); 5907 } 5908 5909 if (kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr, 5910 offsetof(struct vmcs12, hdr), 5911 sizeof(hdr))) { 5912 return nested_vmx_fail(vcpu, 5913 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); 5914 } 5915 5916 if (hdr.revision_id != VMCS12_REVISION || 5917 (hdr.shadow_vmcs && 5918 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { 5919 return nested_vmx_fail(vcpu, 5920 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); 5921 } 5922 5923 nested_release_vmcs12(vcpu); 5924 5925 /* 5926 * Load VMCS12 from guest memory since it is not already 5927 * cached. 5928 */ 5929 if (kvm_read_guest_cached(vcpu->kvm, ghc, vmx->nested.cached_vmcs12, 5930 VMCS12_SIZE)) { 5931 return nested_vmx_fail(vcpu, 5932 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); 5933 } 5934 5935 set_current_vmptr(vmx, vmptr); 5936 } 5937 5938 return nested_vmx_succeed(vcpu); 5939 } 5940 5941 /* Emulate the VMPTRST instruction */ 5942 static int handle_vmptrst(struct kvm_vcpu *vcpu) 5943 { 5944 unsigned long exit_qual = vmx_get_exit_qual(vcpu); 5945 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); 5946 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; 5947 struct x86_exception e; 5948 gva_t gva; 5949 int r; 5950 5951 if (!nested_vmx_check_permission(vcpu)) 5952 return 1; 5953 5954 if (unlikely(nested_vmx_is_evmptr12_valid(to_vmx(vcpu)))) 5955 return 1; 5956 5957 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, 5958 true, sizeof(gpa_t), &gva)) 5959 return 1; 5960 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ 5961 r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, 5962 sizeof(gpa_t), &e); 5963 if (r != X86EMUL_CONTINUE) 5964 return kvm_handle_memory_failure(vcpu, r, &e); 5965 5966 return nested_vmx_succeed(vcpu); 5967 } 5968 5969 /* Emulate the INVEPT instruction */ 5970 static int handle_invept(struct kvm_vcpu *vcpu) 5971 { 5972 struct vcpu_vmx *vmx = to_vmx(vcpu); 5973 u32 vmx_instruction_info, types; 5974 unsigned long type, roots_to_free; 5975 struct kvm_mmu *mmu; 5976 gva_t gva; 5977 struct x86_exception e; 5978 struct { 5979 u64 eptp, gpa; 5980 } operand; 5981 int i, r, gpr_index; 5982 5983 if (!(vmx->nested.msrs.secondary_ctls_high & 5984 SECONDARY_EXEC_ENABLE_EPT) || 5985 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { 5986 kvm_queue_exception(vcpu, UD_VECTOR); 5987 return 1; 5988 } 5989 5990 if (!nested_vmx_check_permission(vcpu)) 5991 return 1; 5992 5993 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); 5994 gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); 5995 type = kvm_register_read(vcpu, gpr_index); 5996 5997 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; 5998 5999 if (type >= 32 || !(types & (1 << type))) 6000 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); 6001 6002 /* According to the Intel VMX instruction reference, the memory 6003 * operand is read even if it isn't needed (e.g., for type==global) 6004 */ 6005 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), 6006 vmx_instruction_info, false, sizeof(operand), &gva)) 6007 return 1; 6008 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); 6009 if (r != X86EMUL_CONTINUE) 6010 return kvm_handle_memory_failure(vcpu, r, &e); 6011 6012 /* 6013 * Nested EPT roots are always held through guest_mmu, 6014 * not root_mmu. 6015 */ 6016 mmu = &vcpu->arch.guest_mmu; 6017 6018 switch (type) { 6019 case VMX_EPT_EXTENT_CONTEXT: 6020 if (!nested_vmx_check_eptp(vcpu, operand.eptp)) 6021 return nested_vmx_fail(vcpu, 6022 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); 6023 6024 roots_to_free = 0; 6025 if (nested_ept_root_matches(mmu->root.hpa, mmu->root.pgd, 6026 operand.eptp)) 6027 roots_to_free |= KVM_MMU_ROOT_CURRENT; 6028 6029 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { 6030 if (nested_ept_root_matches(mmu->prev_roots[i].hpa, 6031 mmu->prev_roots[i].pgd, 6032 operand.eptp)) 6033 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); 6034 } 6035 break; 6036 case VMX_EPT_EXTENT_GLOBAL: 6037 roots_to_free = KVM_MMU_ROOTS_ALL; 6038 break; 6039 default: 6040 BUG(); 6041 break; 6042 } 6043 6044 if (roots_to_free) 6045 kvm_mmu_free_roots(vcpu->kvm, mmu, roots_to_free); 6046 6047 return nested_vmx_succeed(vcpu); 6048 } 6049 6050 static int handle_invvpid(struct kvm_vcpu *vcpu) 6051 { 6052 struct vcpu_vmx *vmx = to_vmx(vcpu); 6053 u32 vmx_instruction_info; 6054 unsigned long type, types; 6055 gva_t gva; 6056 struct x86_exception e; 6057 struct { 6058 u64 vpid; 6059 u64 gla; 6060 } operand; 6061 u16 vpid02; 6062 int r, gpr_index; 6063 6064 if (!(vmx->nested.msrs.secondary_ctls_high & 6065 SECONDARY_EXEC_ENABLE_VPID) || 6066 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { 6067 kvm_queue_exception(vcpu, UD_VECTOR); 6068 return 1; 6069 } 6070 6071 if (!nested_vmx_check_permission(vcpu)) 6072 return 1; 6073 6074 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); 6075 gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); 6076 type = kvm_register_read(vcpu, gpr_index); 6077 6078 types = (vmx->nested.msrs.vpid_caps & 6079 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; 6080 6081 if (type >= 32 || !(types & (1 << type))) 6082 return nested_vmx_fail(vcpu, 6083 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); 6084 6085 /* according to the intel vmx instruction reference, the memory 6086 * operand is read even if it isn't needed (e.g., for type==global) 6087 */ 6088 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), 6089 vmx_instruction_info, false, sizeof(operand), &gva)) 6090 return 1; 6091 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); 6092 if (r != X86EMUL_CONTINUE) 6093 return kvm_handle_memory_failure(vcpu, r, &e); 6094 6095 if (operand.vpid >> 16) 6096 return nested_vmx_fail(vcpu, 6097 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); 6098 6099 /* 6100 * Always flush the effective vpid02, i.e. never flush the current VPID 6101 * and never explicitly flush vpid01. INVVPID targets a VPID, not a 6102 * VMCS, and so whether or not the current vmcs12 has VPID enabled is 6103 * irrelevant (and there may not be a loaded vmcs12). 6104 */ 6105 vpid02 = nested_get_vpid02(vcpu); 6106 switch (type) { 6107 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: 6108 /* 6109 * LAM doesn't apply to addresses that are inputs to TLB 6110 * invalidation. 6111 */ 6112 if (!operand.vpid || 6113 is_noncanonical_invlpg_address(operand.gla, vcpu)) 6114 return nested_vmx_fail(vcpu, 6115 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); 6116 vpid_sync_vcpu_addr(vpid02, operand.gla); 6117 break; 6118 case VMX_VPID_EXTENT_SINGLE_CONTEXT: 6119 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: 6120 if (!operand.vpid) 6121 return nested_vmx_fail(vcpu, 6122 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); 6123 vpid_sync_context(vpid02); 6124 break; 6125 case VMX_VPID_EXTENT_ALL_CONTEXT: 6126 vpid_sync_context(vpid02); 6127 break; 6128 default: 6129 WARN_ON_ONCE(1); 6130 return kvm_skip_emulated_instruction(vcpu); 6131 } 6132 6133 /* 6134 * Sync the shadow page tables if EPT is disabled, L1 is invalidating 6135 * linear mappings for L2 (tagged with L2's VPID). Free all guest 6136 * roots as VPIDs are not tracked in the MMU role. 6137 * 6138 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share 6139 * an MMU when EPT is disabled. 6140 * 6141 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. 6142 */ 6143 if (!enable_ept) 6144 kvm_mmu_free_guest_mode_roots(vcpu->kvm, &vcpu->arch.root_mmu); 6145 6146 return nested_vmx_succeed(vcpu); 6147 } 6148 6149 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, 6150 struct vmcs12 *vmcs12) 6151 { 6152 u32 index = kvm_ecx_read(vcpu); 6153 u64 new_eptp; 6154 6155 if (WARN_ON_ONCE(!nested_cpu_has_ept(vmcs12))) 6156 return 1; 6157 if (index >= VMFUNC_EPTP_ENTRIES) 6158 return 1; 6159 6160 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, 6161 &new_eptp, index * 8, 8)) 6162 return 1; 6163 6164 /* 6165 * If the (L2) guest does a vmfunc to the currently 6166 * active ept pointer, we don't have to do anything else 6167 */ 6168 if (vmcs12->ept_pointer != new_eptp) { 6169 if (!nested_vmx_check_eptp(vcpu, new_eptp)) 6170 return 1; 6171 6172 vmcs12->ept_pointer = new_eptp; 6173 nested_ept_new_eptp(vcpu); 6174 6175 if (!nested_cpu_has_vpid(vmcs12)) 6176 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); 6177 } 6178 6179 return 0; 6180 } 6181 6182 static int handle_vmfunc(struct kvm_vcpu *vcpu) 6183 { 6184 struct vcpu_vmx *vmx = to_vmx(vcpu); 6185 struct vmcs12 *vmcs12; 6186 u32 function = kvm_eax_read(vcpu); 6187 6188 /* 6189 * VMFUNC should never execute cleanly while L1 is active; KVM supports 6190 * VMFUNC for nested VMs, but not for L1. 6191 */ 6192 if (WARN_ON_ONCE(!is_guest_mode(vcpu))) { 6193 kvm_queue_exception(vcpu, UD_VECTOR); 6194 return 1; 6195 } 6196 6197 vmcs12 = get_vmcs12(vcpu); 6198 6199 /* 6200 * #UD on out-of-bounds function has priority over VM-Exit, and VMFUNC 6201 * is enabled in vmcs02 if and only if it's enabled in vmcs12. 6202 */ 6203 if (WARN_ON_ONCE((function > 63) || !nested_cpu_has_vmfunc(vmcs12))) { 6204 kvm_queue_exception(vcpu, UD_VECTOR); 6205 return 1; 6206 } 6207 6208 if (!(vmcs12->vm_function_control & BIT_ULL(function))) 6209 goto fail; 6210 6211 switch (function) { 6212 case 0: 6213 if (nested_vmx_eptp_switching(vcpu, vmcs12)) 6214 goto fail; 6215 break; 6216 default: 6217 goto fail; 6218 } 6219 return kvm_skip_emulated_instruction(vcpu); 6220 6221 fail: 6222 /* 6223 * This is effectively a reflected VM-Exit, as opposed to a synthesized 6224 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode 6225 * EXIT_REASON_VMFUNC as the exit reason. 6226 */ 6227 nested_vmx_vmexit(vcpu, vmx->vt.exit_reason.full, 6228 vmx_get_intr_info(vcpu), 6229 vmx_get_exit_qual(vcpu)); 6230 return 1; 6231 } 6232 6233 /* 6234 * Return true if an IO instruction with the specified port and size should cause 6235 * a VM-exit into L1. 6236 */ 6237 bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port, 6238 int size) 6239 { 6240 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 6241 gpa_t bitmap, last_bitmap; 6242 u8 b; 6243 6244 last_bitmap = INVALID_GPA; 6245 b = -1; 6246 6247 while (size > 0) { 6248 if (port < 0x8000) 6249 bitmap = vmcs12->io_bitmap_a; 6250 else if (port < 0x10000) 6251 bitmap = vmcs12->io_bitmap_b; 6252 else 6253 return true; 6254 bitmap += (port & 0x7fff) / 8; 6255 6256 if (last_bitmap != bitmap) 6257 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) 6258 return true; 6259 if (b & (1 << (port & 7))) 6260 return true; 6261 6262 port++; 6263 size--; 6264 last_bitmap = bitmap; 6265 } 6266 6267 return false; 6268 } 6269 6270 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, 6271 struct vmcs12 *vmcs12) 6272 { 6273 unsigned long exit_qualification; 6274 unsigned short port; 6275 int size; 6276 6277 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) 6278 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); 6279 6280 exit_qualification = vmx_get_exit_qual(vcpu); 6281 6282 port = exit_qualification >> 16; 6283 size = (exit_qualification & 7) + 1; 6284 6285 return nested_vmx_check_io_bitmaps(vcpu, port, size); 6286 } 6287 6288 /* 6289 * Return 1 if we should exit from L2 to L1 to handle an MSR access, 6290 * rather than handle it ourselves in L0. I.e., check whether L1 expressed 6291 * disinterest in the current event (read or write a specific MSR) by using an 6292 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. 6293 */ 6294 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, 6295 struct vmcs12 *vmcs12, 6296 union vmx_exit_reason exit_reason) 6297 { 6298 u32 msr_index; 6299 gpa_t bitmap; 6300 6301 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) 6302 return true; 6303 6304 if (exit_reason.basic == EXIT_REASON_MSR_READ_IMM || 6305 exit_reason.basic == EXIT_REASON_MSR_WRITE_IMM) 6306 msr_index = vmx_get_exit_qual(vcpu); 6307 else 6308 msr_index = kvm_ecx_read(vcpu); 6309 6310 /* 6311 * The MSR_BITMAP page is divided into four 1024-byte bitmaps, 6312 * for the four combinations of read/write and low/high MSR numbers. 6313 * First we need to figure out which of the four to use: 6314 */ 6315 bitmap = vmcs12->msr_bitmap; 6316 if (exit_reason.basic == EXIT_REASON_MSR_WRITE || 6317 exit_reason.basic == EXIT_REASON_MSR_WRITE_IMM) 6318 bitmap += 2048; 6319 if (msr_index >= 0xc0000000) { 6320 msr_index -= 0xc0000000; 6321 bitmap += 1024; 6322 } 6323 6324 /* Then read the msr_index'th bit from this bitmap: */ 6325 if (msr_index < 1024*8) { 6326 unsigned char b; 6327 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) 6328 return true; 6329 return 1 & (b >> (msr_index & 7)); 6330 } else 6331 return true; /* let L1 handle the wrong parameter */ 6332 } 6333 6334 /* 6335 * Return 1 if we should exit from L2 to L1 to handle a CR access exit, 6336 * rather than handle it ourselves in L0. I.e., check if L1 wanted to 6337 * intercept (via guest_host_mask etc.) the current event. 6338 */ 6339 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, 6340 struct vmcs12 *vmcs12) 6341 { 6342 unsigned long exit_qualification = vmx_get_exit_qual(vcpu); 6343 int cr = exit_qualification & 15; 6344 int reg; 6345 unsigned long val; 6346 6347 switch ((exit_qualification >> 4) & 3) { 6348 case 0: /* mov to cr */ 6349 reg = (exit_qualification >> 8) & 15; 6350 val = kvm_register_read(vcpu, reg); 6351 switch (cr) { 6352 case 0: 6353 if (vmcs12->cr0_guest_host_mask & 6354 (val ^ vmcs12->cr0_read_shadow)) 6355 return true; 6356 break; 6357 case 3: 6358 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) 6359 return true; 6360 break; 6361 case 4: 6362 if (vmcs12->cr4_guest_host_mask & 6363 (vmcs12->cr4_read_shadow ^ val)) 6364 return true; 6365 break; 6366 case 8: 6367 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) 6368 return true; 6369 break; 6370 } 6371 break; 6372 case 2: /* clts */ 6373 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && 6374 (vmcs12->cr0_read_shadow & X86_CR0_TS)) 6375 return true; 6376 break; 6377 case 1: /* mov from cr */ 6378 switch (cr) { 6379 case 3: 6380 if (vmcs12->cpu_based_vm_exec_control & 6381 CPU_BASED_CR3_STORE_EXITING) 6382 return true; 6383 break; 6384 case 8: 6385 if (vmcs12->cpu_based_vm_exec_control & 6386 CPU_BASED_CR8_STORE_EXITING) 6387 return true; 6388 break; 6389 } 6390 break; 6391 case 3: /* lmsw */ 6392 /* 6393 * lmsw can change bits 1..3 of cr0, and only set bit 0 of 6394 * cr0. Other attempted changes are ignored, with no exit. 6395 */ 6396 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; 6397 if (vmcs12->cr0_guest_host_mask & 0xe & 6398 (val ^ vmcs12->cr0_read_shadow)) 6399 return true; 6400 if ((vmcs12->cr0_guest_host_mask & 0x1) && 6401 !(vmcs12->cr0_read_shadow & 0x1) && 6402 (val & 0x1)) 6403 return true; 6404 break; 6405 } 6406 return false; 6407 } 6408 6409 static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu, 6410 struct vmcs12 *vmcs12) 6411 { 6412 u32 encls_leaf; 6413 6414 if (!guest_cpu_cap_has(vcpu, X86_FEATURE_SGX) || 6415 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING)) 6416 return false; 6417 6418 encls_leaf = kvm_eax_read(vcpu); 6419 if (encls_leaf > 62) 6420 encls_leaf = 63; 6421 return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf); 6422 } 6423 6424 static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, 6425 struct vmcs12 *vmcs12, gpa_t bitmap) 6426 { 6427 u32 vmx_instruction_info; 6428 unsigned long field; 6429 u8 b; 6430 6431 if (!nested_cpu_has_shadow_vmcs(vmcs12)) 6432 return true; 6433 6434 /* Decode instruction info and find the field to access */ 6435 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); 6436 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); 6437 6438 /* Out-of-range fields always cause a VM exit from L2 to L1 */ 6439 if (field >> 15) 6440 return true; 6441 6442 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) 6443 return true; 6444 6445 return 1 & (b >> (field & 7)); 6446 } 6447 6448 static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12) 6449 { 6450 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field; 6451 6452 if (nested_cpu_has_mtf(vmcs12)) 6453 return true; 6454 6455 /* 6456 * An MTF VM-exit may be injected into the guest by setting the 6457 * interruption-type to 7 (other event) and the vector field to 0. Such 6458 * is the case regardless of the 'monitor trap flag' VM-execution 6459 * control. 6460 */ 6461 return entry_intr_info == (INTR_INFO_VALID_MASK 6462 | INTR_TYPE_OTHER_EVENT); 6463 } 6464 6465 /* 6466 * Return true if L0 wants to handle an exit from L2 regardless of whether or not 6467 * L1 wants the exit. Only call this when in is_guest_mode (L2). 6468 */ 6469 static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, 6470 union vmx_exit_reason exit_reason) 6471 { 6472 u32 intr_info; 6473 6474 switch ((u16)exit_reason.basic) { 6475 case EXIT_REASON_EXCEPTION_NMI: 6476 intr_info = vmx_get_intr_info(vcpu); 6477 if (is_nmi(intr_info)) 6478 return true; 6479 else if (is_page_fault(intr_info)) 6480 return vcpu->arch.apf.host_apf_flags || 6481 vmx_need_pf_intercept(vcpu); 6482 else if (is_debug(intr_info) && 6483 vcpu->guest_debug & 6484 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) 6485 return true; 6486 else if (is_breakpoint(intr_info) && 6487 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) 6488 return true; 6489 else if (is_alignment_check(intr_info) && 6490 !vmx_guest_inject_ac(vcpu)) 6491 return true; 6492 else if (is_ve_fault(intr_info)) 6493 return true; 6494 return false; 6495 case EXIT_REASON_EXTERNAL_INTERRUPT: 6496 return true; 6497 case EXIT_REASON_MCE_DURING_VMENTRY: 6498 return true; 6499 case EXIT_REASON_EPT_VIOLATION: 6500 /* 6501 * L0 always deals with the EPT violation. If nested EPT is 6502 * used, and the nested mmu code discovers that the address is 6503 * missing in the guest EPT table (EPT12), the EPT violation 6504 * will be injected with nested_ept_inject_page_fault() 6505 */ 6506 return true; 6507 case EXIT_REASON_EPT_MISCONFIG: 6508 /* 6509 * L2 never uses directly L1's EPT, but rather L0's own EPT 6510 * table (shadow on EPT) or a merged EPT table that L0 built 6511 * (EPT on EPT). So any problems with the structure of the 6512 * table is L0's fault. 6513 */ 6514 return true; 6515 case EXIT_REASON_PREEMPTION_TIMER: 6516 return true; 6517 case EXIT_REASON_PML_FULL: 6518 /* 6519 * PML is emulated for an L1 VMM and should never be enabled in 6520 * vmcs02, always "handle" PML_FULL by exiting to userspace. 6521 */ 6522 return true; 6523 case EXIT_REASON_VMFUNC: 6524 /* VM functions are emulated through L2->L0 vmexits. */ 6525 return true; 6526 case EXIT_REASON_BUS_LOCK: 6527 /* 6528 * At present, bus lock VM exit is never exposed to L1. 6529 * Handle L2's bus locks in L0 directly. 6530 */ 6531 return true; 6532 #ifdef CONFIG_KVM_HYPERV 6533 case EXIT_REASON_VMCALL: 6534 /* Hyper-V L2 TLB flush hypercall is handled by L0 */ 6535 return guest_hv_cpuid_has_l2_tlb_flush(vcpu) && 6536 nested_evmcs_l2_tlb_flush_enabled(vcpu) && 6537 kvm_hv_is_tlb_flush_hcall(vcpu); 6538 #endif 6539 case EXIT_REASON_CPUID: 6540 return !kvm_is_cpuid_allowed(vcpu); 6541 default: 6542 break; 6543 } 6544 return false; 6545 } 6546 6547 /* 6548 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in 6549 * is_guest_mode (L2). 6550 */ 6551 static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, 6552 union vmx_exit_reason exit_reason) 6553 { 6554 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 6555 u32 intr_info; 6556 6557 switch ((u16)exit_reason.basic) { 6558 case EXIT_REASON_EXCEPTION_NMI: 6559 intr_info = vmx_get_intr_info(vcpu); 6560 if (is_nmi(intr_info)) 6561 return true; 6562 else if (is_page_fault(intr_info)) 6563 return true; 6564 return vmcs12->exception_bitmap & 6565 (1u << (intr_info & INTR_INFO_VECTOR_MASK)); 6566 case EXIT_REASON_EXTERNAL_INTERRUPT: 6567 return nested_exit_on_intr(vcpu); 6568 case EXIT_REASON_TRIPLE_FAULT: 6569 return true; 6570 case EXIT_REASON_INTERRUPT_WINDOW: 6571 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING); 6572 case EXIT_REASON_NMI_WINDOW: 6573 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING); 6574 case EXIT_REASON_TASK_SWITCH: 6575 return true; 6576 case EXIT_REASON_CPUID: 6577 return true; 6578 case EXIT_REASON_HLT: 6579 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); 6580 case EXIT_REASON_INVD: 6581 return true; 6582 case EXIT_REASON_INVLPG: 6583 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); 6584 case EXIT_REASON_RDPMC: 6585 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); 6586 case EXIT_REASON_RDRAND: 6587 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); 6588 case EXIT_REASON_RDSEED: 6589 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); 6590 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: 6591 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); 6592 case EXIT_REASON_VMREAD: 6593 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, 6594 vmcs12->vmread_bitmap); 6595 case EXIT_REASON_VMWRITE: 6596 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, 6597 vmcs12->vmwrite_bitmap); 6598 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: 6599 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: 6600 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: 6601 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: 6602 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: 6603 /* 6604 * VMX instructions trap unconditionally. This allows L1 to 6605 * emulate them for its L2 guest, i.e., allows 3-level nesting! 6606 */ 6607 return true; 6608 case EXIT_REASON_CR_ACCESS: 6609 return nested_vmx_exit_handled_cr(vcpu, vmcs12); 6610 case EXIT_REASON_DR_ACCESS: 6611 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); 6612 case EXIT_REASON_IO_INSTRUCTION: 6613 return nested_vmx_exit_handled_io(vcpu, vmcs12); 6614 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: 6615 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); 6616 case EXIT_REASON_MSR_READ: 6617 case EXIT_REASON_MSR_WRITE: 6618 case EXIT_REASON_MSR_READ_IMM: 6619 case EXIT_REASON_MSR_WRITE_IMM: 6620 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); 6621 case EXIT_REASON_INVALID_STATE: 6622 return true; 6623 case EXIT_REASON_MWAIT_INSTRUCTION: 6624 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); 6625 case EXIT_REASON_MONITOR_TRAP_FLAG: 6626 return nested_vmx_exit_handled_mtf(vmcs12); 6627 case EXIT_REASON_MONITOR_INSTRUCTION: 6628 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); 6629 case EXIT_REASON_PAUSE_INSTRUCTION: 6630 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || 6631 nested_cpu_has2(vmcs12, 6632 SECONDARY_EXEC_PAUSE_LOOP_EXITING); 6633 case EXIT_REASON_MCE_DURING_VMENTRY: 6634 return true; 6635 case EXIT_REASON_TPR_BELOW_THRESHOLD: 6636 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); 6637 case EXIT_REASON_APIC_ACCESS: 6638 case EXIT_REASON_APIC_WRITE: 6639 case EXIT_REASON_EOI_INDUCED: 6640 /* 6641 * The controls for "virtualize APIC accesses," "APIC- 6642 * register virtualization," and "virtual-interrupt 6643 * delivery" only come from vmcs12. 6644 */ 6645 return true; 6646 case EXIT_REASON_INVPCID: 6647 return 6648 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && 6649 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); 6650 case EXIT_REASON_WBINVD: 6651 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); 6652 case EXIT_REASON_XSETBV: 6653 return true; 6654 case EXIT_REASON_XSAVES: 6655 case EXIT_REASON_XRSTORS: 6656 /* 6657 * Always forward XSAVES/XRSTORS to L1 as KVM doesn't utilize 6658 * XSS-bitmap, and always loads vmcs02 with vmcs12's XSS-bitmap 6659 * verbatim, i.e. any exit is due to L1's bitmap. WARN if 6660 * XSAVES isn't enabled, as the CPU is supposed to inject #UD 6661 * in that case, before consulting the XSS-bitmap. 6662 */ 6663 WARN_ON_ONCE(!nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_XSAVES)); 6664 return true; 6665 case EXIT_REASON_UMWAIT: 6666 case EXIT_REASON_TPAUSE: 6667 return nested_cpu_has2(vmcs12, 6668 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE); 6669 case EXIT_REASON_ENCLS: 6670 return nested_vmx_exit_handled_encls(vcpu, vmcs12); 6671 case EXIT_REASON_NOTIFY: 6672 /* Notify VM exit is not exposed to L1 */ 6673 return false; 6674 case EXIT_REASON_SEAMCALL: 6675 case EXIT_REASON_TDCALL: 6676 /* 6677 * SEAMCALL and TDCALL unconditionally VM-Exit, but aren't 6678 * virtualized by KVM for L1 hypervisors, i.e. L1 should 6679 * never want or expect such an exit. 6680 */ 6681 return false; 6682 default: 6683 return true; 6684 } 6685 } 6686 6687 /* 6688 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was 6689 * reflected into L1. 6690 */ 6691 bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu) 6692 { 6693 struct vcpu_vmx *vmx = to_vmx(vcpu); 6694 union vmx_exit_reason exit_reason = vmx->vt.exit_reason; 6695 unsigned long exit_qual; 6696 u32 exit_intr_info; 6697 6698 kvm_warn_on_nested_run_pending(vcpu); 6699 6700 /* 6701 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM 6702 * has already loaded L2's state. 6703 */ 6704 if (unlikely(vmx->fail)) { 6705 trace_kvm_nested_vmenter_failed( 6706 "hardware VM-instruction error: ", 6707 vmcs_read32(VM_INSTRUCTION_ERROR)); 6708 exit_intr_info = 0; 6709 exit_qual = 0; 6710 goto reflect_vmexit; 6711 } 6712 6713 trace_kvm_nested_vmexit(vcpu, KVM_ISA_VMX); 6714 6715 /* If L0 (KVM) wants the exit, it trumps L1's desires. */ 6716 if (nested_vmx_l0_wants_exit(vcpu, exit_reason)) 6717 return false; 6718 6719 /* If L1 doesn't want the exit, handle it in L0. */ 6720 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason)) 6721 return false; 6722 6723 /* 6724 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For 6725 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would 6726 * need to be synthesized by querying the in-kernel LAPIC, but external 6727 * interrupts are never reflected to L1 so it's a non-issue. 6728 */ 6729 exit_intr_info = vmx_get_intr_info(vcpu); 6730 if (is_exception_with_error_code(exit_intr_info)) { 6731 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 6732 6733 vmcs12->vm_exit_intr_error_code = 6734 vmcs_read32(VM_EXIT_INTR_ERROR_CODE); 6735 } 6736 exit_qual = vmx_get_exit_qual(vcpu); 6737 6738 reflect_vmexit: 6739 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual); 6740 return true; 6741 } 6742 6743 static int vmx_get_nested_state(struct kvm_vcpu *vcpu, 6744 struct kvm_nested_state __user *user_kvm_nested_state, 6745 u32 user_data_size) 6746 { 6747 struct vcpu_vmx *vmx; 6748 struct vmcs12 *vmcs12; 6749 struct kvm_nested_state kvm_state = { 6750 .flags = 0, 6751 .format = KVM_STATE_NESTED_FORMAT_VMX, 6752 .size = sizeof(kvm_state), 6753 .hdr.vmx.flags = 0, 6754 .hdr.vmx.vmxon_pa = INVALID_GPA, 6755 .hdr.vmx.vmcs12_pa = INVALID_GPA, 6756 .hdr.vmx.preemption_timer_deadline = 0, 6757 }; 6758 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = 6759 &user_kvm_nested_state->data.vmx[0]; 6760 6761 if (!vcpu) 6762 return kvm_state.size + sizeof(*user_vmx_nested_state); 6763 6764 vmx = to_vmx(vcpu); 6765 vmcs12 = get_vmcs12(vcpu); 6766 6767 if (guest_cpu_cap_has(vcpu, X86_FEATURE_VMX) && 6768 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { 6769 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr; 6770 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr; 6771 6772 if (vmx_has_valid_vmcs12(vcpu)) { 6773 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12); 6774 6775 /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */ 6776 if (nested_vmx_is_evmptr12_set(vmx)) 6777 kvm_state.flags |= KVM_STATE_NESTED_EVMCS; 6778 6779 if (is_guest_mode(vcpu) && 6780 nested_cpu_has_shadow_vmcs(vmcs12) && 6781 vmcs12->vmcs_link_pointer != INVALID_GPA) 6782 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12); 6783 } 6784 6785 if (vmx->nested.smm.vmxon) 6786 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; 6787 6788 if (vmx->nested.smm.guest_mode) 6789 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; 6790 6791 if (is_guest_mode(vcpu)) { 6792 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; 6793 6794 if (vcpu->arch.nested_run_pending) 6795 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; 6796 6797 if (vmx->nested.mtf_pending) 6798 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING; 6799 6800 if (nested_cpu_has_preemption_timer(vmcs12) && 6801 vmx->nested.has_preemption_timer_deadline) { 6802 kvm_state.hdr.vmx.flags |= 6803 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE; 6804 kvm_state.hdr.vmx.preemption_timer_deadline = 6805 vmx->nested.preemption_timer_deadline; 6806 } 6807 } 6808 } 6809 6810 if (user_data_size < kvm_state.size) 6811 goto out; 6812 6813 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) 6814 return -EFAULT; 6815 6816 if (!vmx_has_valid_vmcs12(vcpu)) 6817 goto out; 6818 6819 /* 6820 * When running L2, the authoritative vmcs12 state is in the 6821 * vmcs02. When running L1, the authoritative vmcs12 state is 6822 * in the shadow or enlightened vmcs linked to vmcs01, unless 6823 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative 6824 * vmcs12 state is in the vmcs12 already. 6825 */ 6826 if (is_guest_mode(vcpu)) { 6827 sync_vmcs02_to_vmcs12(vcpu, vmcs12); 6828 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); 6829 } else { 6830 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); 6831 if (!vmx->nested.need_vmcs12_to_shadow_sync) { 6832 if (nested_vmx_is_evmptr12_valid(vmx)) 6833 /* 6834 * L1 hypervisor is not obliged to keep eVMCS 6835 * clean fields data always up-to-date while 6836 * not in guest mode, 'hv_clean_fields' is only 6837 * supposed to be actual upon vmentry so we need 6838 * to ignore it here and do full copy. 6839 */ 6840 copy_enlightened_to_vmcs12(vmx, 0); 6841 else if (enable_shadow_vmcs) 6842 copy_shadow_to_vmcs12(vmx); 6843 } 6844 } 6845 6846 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE); 6847 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE); 6848 6849 /* 6850 * Copy over the full allocated size of vmcs12 rather than just the size 6851 * of the struct. 6852 */ 6853 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE)) 6854 return -EFAULT; 6855 6856 if (nested_cpu_has_shadow_vmcs(vmcs12) && 6857 vmcs12->vmcs_link_pointer != INVALID_GPA) { 6858 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12, 6859 get_shadow_vmcs12(vcpu), VMCS12_SIZE)) 6860 return -EFAULT; 6861 } 6862 out: 6863 return kvm_state.size; 6864 } 6865 6866 void vmx_leave_nested(struct kvm_vcpu *vcpu) 6867 { 6868 if (is_guest_mode(vcpu)) { 6869 vcpu->arch.nested_run_pending = 0; 6870 nested_vmx_vmexit(vcpu, -1, 0, 0); 6871 } 6872 free_nested(vcpu); 6873 } 6874 6875 int nested_vmx_check_restored_vmcs12(struct kvm_vcpu *vcpu) 6876 { 6877 enum vm_entry_failure_code ignored; 6878 struct vmcs12 *vmcs12 = get_vmcs12(vcpu); 6879 6880 if (nested_cpu_has_shadow_vmcs(vmcs12) && 6881 vmcs12->vmcs_link_pointer != INVALID_GPA) { 6882 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); 6883 6884 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || 6885 !shadow_vmcs12->hdr.shadow_vmcs) 6886 return -EINVAL; 6887 } 6888 6889 if (nested_vmx_check_controls(vcpu, vmcs12) || 6890 nested_vmx_check_host_state(vcpu, vmcs12) || 6891 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) 6892 return -EINVAL; 6893 6894 return 0; 6895 } 6896 6897 static int vmx_set_nested_state(struct kvm_vcpu *vcpu, 6898 struct kvm_nested_state __user *user_kvm_nested_state, 6899 struct kvm_nested_state *kvm_state) 6900 { 6901 struct vcpu_vmx *vmx = to_vmx(vcpu); 6902 struct vmcs12 *vmcs12; 6903 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = 6904 &user_kvm_nested_state->data.vmx[0]; 6905 int ret; 6906 6907 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX) 6908 return -EINVAL; 6909 6910 if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) { 6911 if (kvm_state->hdr.vmx.smm.flags) 6912 return -EINVAL; 6913 6914 if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) 6915 return -EINVAL; 6916 6917 /* 6918 * KVM_STATE_NESTED_EVMCS used to signal that KVM should 6919 * enable eVMCS capability on vCPU. However, since then 6920 * code was changed such that flag signals vmcs12 should 6921 * be copied into eVMCS in guest memory. 6922 * 6923 * To preserve backwards compatibility, allow user 6924 * to set this flag even when there is no VMXON region. 6925 */ 6926 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS) 6927 return -EINVAL; 6928 } else { 6929 if (!guest_cpu_cap_has(vcpu, X86_FEATURE_VMX)) 6930 return -EINVAL; 6931 6932 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa)) 6933 return -EINVAL; 6934 } 6935 6936 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && 6937 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) 6938 return -EINVAL; 6939 6940 if (kvm_state->hdr.vmx.smm.flags & 6941 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) 6942 return -EINVAL; 6943 6944 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) 6945 return -EINVAL; 6946 6947 /* 6948 * SMM temporarily disables VMX, so we cannot be in guest mode, 6949 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags 6950 * must be zero. 6951 */ 6952 if (is_smm(vcpu) ? 6953 (kvm_state->flags & 6954 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING)) 6955 : kvm_state->hdr.vmx.smm.flags) 6956 return -EINVAL; 6957 6958 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && 6959 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) 6960 return -EINVAL; 6961 6962 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) && 6963 (!guest_cpu_cap_has(vcpu, X86_FEATURE_VMX) || 6964 !vmx->nested.enlightened_vmcs_enabled)) 6965 return -EINVAL; 6966 6967 vmx_leave_nested(vcpu); 6968 6969 if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) 6970 return 0; 6971 6972 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa; 6973 ret = enter_vmx_operation(vcpu); 6974 if (ret) 6975 return ret; 6976 6977 /* Empty 'VMXON' state is permitted if no VMCS loaded */ 6978 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) { 6979 /* See vmx_has_valid_vmcs12. */ 6980 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) || 6981 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) || 6982 (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA)) 6983 return -EINVAL; 6984 else 6985 return 0; 6986 } 6987 6988 if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) { 6989 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa || 6990 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa)) 6991 return -EINVAL; 6992 6993 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa); 6994 #ifdef CONFIG_KVM_HYPERV 6995 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { 6996 /* 6997 * nested_vmx_handle_enlightened_vmptrld() cannot be called 6998 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be 6999 * restored yet. EVMCS will be mapped from 7000 * nested_get_vmcs12_pages(). 7001 */ 7002 vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING; 7003 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); 7004 #endif 7005 } else { 7006 return -EINVAL; 7007 } 7008 7009 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { 7010 vmx->nested.smm.vmxon = true; 7011 vmx->nested.vmxon = false; 7012 7013 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) 7014 vmx->nested.smm.guest_mode = true; 7015 } 7016 7017 vmcs12 = get_vmcs12(vcpu); 7018 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12))) 7019 return -EFAULT; 7020 7021 if (vmcs12->hdr.revision_id != VMCS12_REVISION) 7022 return -EINVAL; 7023 7024 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) 7025 return 0; 7026 7027 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING) 7028 vcpu->arch.nested_run_pending = KVM_NESTED_RUN_PENDING_UNTRUSTED; 7029 else 7030 vcpu->arch.nested_run_pending = 0; 7031 7032 vmx->nested.mtf_pending = 7033 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING); 7034 7035 if (nested_cpu_has_shadow_vmcs(vmcs12) && 7036 vmcs12->vmcs_link_pointer != INVALID_GPA) { 7037 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); 7038 7039 ret = -EINVAL; 7040 if (kvm_state->size < 7041 sizeof(*kvm_state) + 7042 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12)) 7043 goto error_guest_mode; 7044 7045 ret = -EFAULT; 7046 if (copy_from_user(shadow_vmcs12, 7047 user_vmx_nested_state->shadow_vmcs12, 7048 sizeof(*shadow_vmcs12))) 7049 goto error_guest_mode; 7050 } 7051 7052 vmx->nested.has_preemption_timer_deadline = false; 7053 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) { 7054 vmx->nested.has_preemption_timer_deadline = true; 7055 vmx->nested.preemption_timer_deadline = 7056 kvm_state->hdr.vmx.preemption_timer_deadline; 7057 } 7058 7059 ret = nested_vmx_check_restored_vmcs12(vcpu); 7060 if (ret < 0) 7061 goto error_guest_mode; 7062 7063 vmx->nested.dirty_vmcs12 = true; 7064 vmx->nested.force_msr_bitmap_recalc = true; 7065 ret = nested_vmx_enter_non_root_mode(vcpu, false); 7066 if (ret) 7067 goto error_guest_mode; 7068 7069 if (vmx->nested.mtf_pending) 7070 kvm_make_request(KVM_REQ_EVENT, vcpu); 7071 7072 return 0; 7073 7074 error_guest_mode: 7075 vcpu->arch.nested_run_pending = 0; 7076 return ret; 7077 } 7078 7079 void nested_vmx_set_vmcs_shadowing_bitmap(void) 7080 { 7081 if (enable_shadow_vmcs) { 7082 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); 7083 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); 7084 } 7085 } 7086 7087 static u64 nested_vmx_calc_vmcs_enum_msr(void) 7088 { 7089 /* 7090 * Note these are the so called "index" of the VMCS field encoding, not 7091 * the index into vmcs12. 7092 */ 7093 unsigned int max_idx, idx; 7094 int i; 7095 7096 /* 7097 * For better or worse, KVM allows VMREAD/VMWRITE to all fields in 7098 * vmcs12, regardless of whether or not the associated feature is 7099 * exposed to L1. Simply find the field with the highest index. 7100 */ 7101 max_idx = 0; 7102 for (i = 0; i < nr_vmcs12_fields; i++) { 7103 /* The vmcs12 table is very, very sparsely populated. */ 7104 if (!vmcs12_field_offsets[i]) 7105 continue; 7106 7107 idx = vmcs_field_index(VMCS12_IDX_TO_ENC(i)); 7108 if (idx > max_idx) 7109 max_idx = idx; 7110 } 7111 7112 return (u64)max_idx << VMCS_FIELD_INDEX_SHIFT; 7113 } 7114 7115 static void nested_vmx_setup_pinbased_ctls(struct vmcs_config *vmcs_conf, 7116 struct nested_vmx_msrs *msrs) 7117 { 7118 msrs->pinbased_ctls_low = 7119 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; 7120 7121 msrs->pinbased_ctls_high = vmcs_conf->pin_based_exec_ctrl; 7122 msrs->pinbased_ctls_high &= 7123 PIN_BASED_EXT_INTR_MASK | 7124 PIN_BASED_NMI_EXITING | 7125 PIN_BASED_VIRTUAL_NMIS | 7126 (enable_apicv ? PIN_BASED_POSTED_INTR : 0); 7127 msrs->pinbased_ctls_high |= 7128 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | 7129 PIN_BASED_VMX_PREEMPTION_TIMER; 7130 } 7131 7132 static void nested_vmx_setup_exit_ctls(struct vmcs_config *vmcs_conf, 7133 struct nested_vmx_msrs *msrs) 7134 { 7135 msrs->exit_ctls_low = 7136 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; 7137 7138 msrs->exit_ctls_high = vmcs_conf->vmexit_ctrl; 7139 msrs->exit_ctls_high &= 7140 #ifdef CONFIG_X86_64 7141 VM_EXIT_HOST_ADDR_SPACE_SIZE | 7142 #endif 7143 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT | 7144 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_CET_STATE; 7145 msrs->exit_ctls_high |= 7146 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | 7147 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | 7148 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT | 7149 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; 7150 7151 if (!kvm_cpu_cap_has(X86_FEATURE_SHSTK) && 7152 !kvm_cpu_cap_has(X86_FEATURE_IBT)) 7153 msrs->exit_ctls_high &= ~VM_EXIT_LOAD_CET_STATE; 7154 7155 /* We support free control of debug control saving. */ 7156 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; 7157 } 7158 7159 static void nested_vmx_setup_entry_ctls(struct vmcs_config *vmcs_conf, 7160 struct nested_vmx_msrs *msrs) 7161 { 7162 msrs->entry_ctls_low = 7163 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; 7164 7165 msrs->entry_ctls_high = vmcs_conf->vmentry_ctrl; 7166 msrs->entry_ctls_high &= 7167 #ifdef CONFIG_X86_64 7168 VM_ENTRY_IA32E_MODE | 7169 #endif 7170 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS | 7171 VM_ENTRY_LOAD_CET_STATE; 7172 msrs->entry_ctls_high |= 7173 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER | 7174 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL); 7175 7176 if (!kvm_cpu_cap_has(X86_FEATURE_SHSTK) && 7177 !kvm_cpu_cap_has(X86_FEATURE_IBT)) 7178 msrs->entry_ctls_high &= ~VM_ENTRY_LOAD_CET_STATE; 7179 7180 /* We support free control of debug control loading. */ 7181 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; 7182 } 7183 7184 static void nested_vmx_setup_cpubased_ctls(struct vmcs_config *vmcs_conf, 7185 struct nested_vmx_msrs *msrs) 7186 { 7187 msrs->procbased_ctls_low = 7188 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; 7189 7190 msrs->procbased_ctls_high = vmcs_conf->cpu_based_exec_ctrl; 7191 msrs->procbased_ctls_high &= 7192 CPU_BASED_INTR_WINDOW_EXITING | 7193 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING | 7194 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | 7195 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | 7196 CPU_BASED_CR3_STORE_EXITING | 7197 #ifdef CONFIG_X86_64 7198 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | 7199 #endif 7200 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | 7201 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | 7202 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | 7203 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | 7204 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; 7205 /* 7206 * We can allow some features even when not supported by the 7207 * hardware. For example, L1 can specify an MSR bitmap - and we 7208 * can use it to avoid exits to L1 - even when L0 runs L2 7209 * without MSR bitmaps. 7210 */ 7211 msrs->procbased_ctls_high |= 7212 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | 7213 CPU_BASED_USE_MSR_BITMAPS; 7214 7215 /* We support free control of CR3 access interception. */ 7216 msrs->procbased_ctls_low &= 7217 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); 7218 } 7219 7220 static void nested_vmx_setup_secondary_ctls(u32 ept_caps, 7221 struct vmcs_config *vmcs_conf, 7222 struct nested_vmx_msrs *msrs) 7223 { 7224 msrs->secondary_ctls_low = 0; 7225 7226 msrs->secondary_ctls_high = vmcs_conf->cpu_based_2nd_exec_ctrl; 7227 msrs->secondary_ctls_high &= 7228 SECONDARY_EXEC_DESC | 7229 SECONDARY_EXEC_ENABLE_RDTSCP | 7230 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | 7231 SECONDARY_EXEC_WBINVD_EXITING | 7232 SECONDARY_EXEC_APIC_REGISTER_VIRT | 7233 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | 7234 SECONDARY_EXEC_RDRAND_EXITING | 7235 SECONDARY_EXEC_ENABLE_INVPCID | 7236 SECONDARY_EXEC_ENABLE_VMFUNC | 7237 SECONDARY_EXEC_RDSEED_EXITING | 7238 SECONDARY_EXEC_ENABLE_XSAVES | 7239 SECONDARY_EXEC_TSC_SCALING | 7240 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE; 7241 7242 /* 7243 * We can emulate "VMCS shadowing," even if the hardware 7244 * doesn't support it. 7245 */ 7246 msrs->secondary_ctls_high |= 7247 SECONDARY_EXEC_SHADOW_VMCS; 7248 7249 if (enable_ept) { 7250 /* nested EPT: emulate EPT also to L1 */ 7251 msrs->secondary_ctls_high |= 7252 SECONDARY_EXEC_ENABLE_EPT; 7253 msrs->ept_caps = 7254 VMX_EPT_PAGE_WALK_4_BIT | 7255 VMX_EPT_PAGE_WALK_5_BIT | 7256 VMX_EPTP_WB_BIT | 7257 VMX_EPT_INVEPT_BIT | 7258 VMX_EPT_EXECUTE_ONLY_BIT | 7259 VMX_EPT_ADVANCED_VMEXIT_INFO_BIT; 7260 7261 msrs->ept_caps &= ept_caps; 7262 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | 7263 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | 7264 VMX_EPT_1GB_PAGE_BIT; 7265 if (enable_ept_ad_bits) { 7266 msrs->secondary_ctls_high |= 7267 SECONDARY_EXEC_ENABLE_PML; 7268 msrs->ept_caps |= VMX_EPT_AD_BIT; 7269 } 7270 7271 if (enable_mbec) 7272 msrs->secondary_ctls_high |= 7273 SECONDARY_EXEC_MODE_BASED_EPT_EXEC; 7274 /* 7275 * Advertise EPTP switching irrespective of hardware support, 7276 * KVM emulates it in software so long as VMFUNC is supported. 7277 */ 7278 if (cpu_has_vmx_vmfunc()) 7279 msrs->vmfunc_controls = VMX_VMFUNC_EPTP_SWITCHING; 7280 } 7281 7282 /* 7283 * Old versions of KVM use the single-context version without 7284 * checking for support, so declare that it is supported even 7285 * though it is treated as global context. The alternative is 7286 * not failing the single-context invvpid, and it is worse. 7287 */ 7288 if (enable_vpid) { 7289 msrs->secondary_ctls_high |= 7290 SECONDARY_EXEC_ENABLE_VPID; 7291 msrs->vpid_caps = VMX_VPID_INVVPID_BIT | 7292 VMX_VPID_EXTENT_SUPPORTED_MASK; 7293 } 7294 7295 if (enable_unrestricted_guest) 7296 msrs->secondary_ctls_high |= 7297 SECONDARY_EXEC_UNRESTRICTED_GUEST; 7298 7299 if (flexpriority_enabled) 7300 msrs->secondary_ctls_high |= 7301 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; 7302 7303 if (enable_sgx) 7304 msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING; 7305 } 7306 7307 static void nested_vmx_setup_misc_data(struct vmcs_config *vmcs_conf, 7308 struct nested_vmx_msrs *msrs) 7309 { 7310 msrs->misc_low = (u32)vmcs_conf->misc & VMX_MISC_SAVE_EFER_LMA; 7311 msrs->misc_low |= 7312 VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | 7313 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | 7314 VMX_MISC_ACTIVITY_HLT | 7315 VMX_MISC_ACTIVITY_WAIT_SIPI; 7316 msrs->misc_high = 0; 7317 } 7318 7319 static void nested_vmx_setup_basic(struct nested_vmx_msrs *msrs) 7320 { 7321 /* 7322 * This MSR reports some information about VMX support. We 7323 * should return information about the VMX we emulate for the 7324 * guest, and the VMCS structure we give it - not about the 7325 * VMX support of the underlying hardware. 7326 */ 7327 msrs->basic = vmx_basic_encode_vmcs_info(VMCS12_REVISION, VMCS12_SIZE, 7328 X86_MEMTYPE_WB); 7329 7330 msrs->basic |= VMX_BASIC_TRUE_CTLS; 7331 if (cpu_has_vmx_basic_inout()) 7332 msrs->basic |= VMX_BASIC_INOUT; 7333 if (cpu_has_vmx_basic_no_hw_errcode_cc()) 7334 msrs->basic |= VMX_BASIC_NO_HW_ERROR_CODE_CC; 7335 } 7336 7337 static void nested_vmx_setup_cr_fixed(struct nested_vmx_msrs *msrs) 7338 { 7339 /* 7340 * These MSRs specify bits which the guest must keep fixed on 7341 * while L1 is in VMXON mode (in L1's root mode, or running an L2). 7342 * We picked the standard core2 setting. 7343 */ 7344 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) 7345 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE 7346 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; 7347 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; 7348 7349 /* These MSRs specify bits which the guest must keep fixed off. */ 7350 rdmsrq(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); 7351 rdmsrq(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); 7352 7353 if (vmx_umip_emulated()) 7354 msrs->cr4_fixed1 |= X86_CR4_UMIP; 7355 } 7356 7357 /* 7358 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be 7359 * returned for the various VMX controls MSRs when nested VMX is enabled. 7360 * The same values should also be used to verify that vmcs12 control fields are 7361 * valid during nested entry from L1 to L2. 7362 * Each of these control msrs has a low and high 32-bit half: A low bit is on 7363 * if the corresponding bit in the (32-bit) control field *must* be on, and a 7364 * bit in the high half is on if the corresponding bit in the control field 7365 * may be on. See also vmx_control_verify(). 7366 */ 7367 void nested_vmx_setup_ctls_msrs(struct vmcs_config *vmcs_conf, u32 ept_caps) 7368 { 7369 struct nested_vmx_msrs *msrs = &vmcs_conf->nested; 7370 7371 /* 7372 * Note that as a general rule, the high half of the MSRs (bits in 7373 * the control fields which may be 1) should be initialized by the 7374 * intersection of the underlying hardware's MSR (i.e., features which 7375 * can be supported) and the list of features we want to expose - 7376 * because they are known to be properly supported in our code. 7377 * Also, usually, the low half of the MSRs (bits which must be 1) can 7378 * be set to 0, meaning that L1 may turn off any of these bits. The 7379 * reason is that if one of these bits is necessary, it will appear 7380 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control 7381 * fields of vmcs01 and vmcs02, will turn these bits off - and 7382 * nested_vmx_l1_wants_exit() will not pass related exits to L1. 7383 * These rules have exceptions below. 7384 */ 7385 nested_vmx_setup_pinbased_ctls(vmcs_conf, msrs); 7386 7387 nested_vmx_setup_exit_ctls(vmcs_conf, msrs); 7388 7389 nested_vmx_setup_entry_ctls(vmcs_conf, msrs); 7390 7391 nested_vmx_setup_cpubased_ctls(vmcs_conf, msrs); 7392 7393 nested_vmx_setup_secondary_ctls(ept_caps, vmcs_conf, msrs); 7394 7395 nested_vmx_setup_misc_data(vmcs_conf, msrs); 7396 7397 nested_vmx_setup_basic(msrs); 7398 7399 nested_vmx_setup_cr_fixed(msrs); 7400 7401 msrs->vmcs_enum = nested_vmx_calc_vmcs_enum_msr(); 7402 } 7403 7404 void nested_vmx_hardware_unsetup(void) 7405 { 7406 int i; 7407 7408 if (enable_shadow_vmcs) { 7409 for (i = 0; i < VMX_BITMAP_NR; i++) 7410 free_page((unsigned long)vmx_bitmap[i]); 7411 } 7412 } 7413 7414 __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *)) 7415 { 7416 int i; 7417 7418 /* 7419 * Note! The set of supported vmcs12 fields is consumed by both VMX 7420 * MSR and shadow VMCS setup. 7421 */ 7422 nested_vmx_setup_vmcs12_fields(); 7423 7424 nested_vmx_setup_ctls_msrs(&vmcs_config, vmx_capability.ept); 7425 7426 if (!cpu_has_vmx_shadow_vmcs()) 7427 enable_shadow_vmcs = 0; 7428 if (enable_shadow_vmcs) { 7429 for (i = 0; i < VMX_BITMAP_NR; i++) { 7430 /* 7431 * The vmx_bitmap is not tied to a VM and so should 7432 * not be charged to a memcg. 7433 */ 7434 vmx_bitmap[i] = (unsigned long *) 7435 __get_free_page(GFP_KERNEL); 7436 if (!vmx_bitmap[i]) { 7437 nested_vmx_hardware_unsetup(); 7438 return -ENOMEM; 7439 } 7440 } 7441 7442 init_vmcs_shadow_fields(); 7443 } 7444 7445 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear; 7446 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch; 7447 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld; 7448 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst; 7449 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread; 7450 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume; 7451 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite; 7452 exit_handlers[EXIT_REASON_VMOFF] = handle_vmxoff; 7453 exit_handlers[EXIT_REASON_VMON] = handle_vmxon; 7454 exit_handlers[EXIT_REASON_INVEPT] = handle_invept; 7455 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid; 7456 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc; 7457 7458 return 0; 7459 } 7460 7461 7462 static gpa_t vmx_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, 7463 u64 access, 7464 struct x86_exception *exception, 7465 u64 pte_access) 7466 { 7467 struct kvm_pagewalk *w = &vcpu->arch.ngpa_walk; 7468 7469 if (WARN_ON_ONCE(!mmu_is_nested(vcpu))) 7470 return gpa; 7471 7472 /* 7473 * MBEC differentiates based on the effective U/S bit of 7474 * the guest page tables; not the processor CPL. 7475 */ 7476 access &= ~PFERR_USER_MASK; 7477 if ((pte_access & ACC_USER_MASK) && (access & PFERR_GUEST_FINAL_MASK)) 7478 access |= PFERR_USER_MASK; 7479 7480 return w->gva_to_gpa(vcpu, w, gpa, access, exception); 7481 } 7482 7483 struct kvm_x86_nested_ops vmx_nested_ops = { 7484 .leave_nested = vmx_leave_nested, 7485 .translate_nested_gpa = vmx_translate_nested_gpa, 7486 .is_exception_vmexit = nested_vmx_is_exception_vmexit, 7487 .check_events = vmx_check_nested_events, 7488 .has_events = vmx_has_nested_events, 7489 .triple_fault = nested_vmx_triple_fault, 7490 .get_state = vmx_get_nested_state, 7491 .set_state = vmx_set_nested_state, 7492 .get_nested_state_pages = vmx_get_nested_state_pages, 7493 .write_log_dirty = nested_vmx_write_pml_buffer, 7494 #ifdef CONFIG_KVM_HYPERV 7495 .enable_evmcs = nested_enable_evmcs, 7496 .get_evmcs_version = nested_get_evmcs_version, 7497 .hv_inject_synthetic_vmexit_post_tlb_flush = vmx_hv_inject_synthetic_vmexit_post_tlb_flush, 7498 #endif 7499 }; 7500