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