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