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