1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Kernel-based Virtual Machine driver for Linux 4 * 5 * AMD SVM support 6 * 7 * Copyright (C) 2006 Qumranet, Inc. 8 * Copyright 2010 Red Hat, Inc. and/or its affiliates. 9 * 10 * Authors: 11 * Yaniv Kamay <yaniv@qumranet.com> 12 * Avi Kivity <avi@qumranet.com> 13 */ 14 15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16 17 #include <linux/kvm_types.h> 18 #include <linux/kvm_host.h> 19 #include <linux/kernel.h> 20 21 #include <asm/msr-index.h> 22 #include <asm/debugreg.h> 23 24 #include "kvm_emulate.h" 25 #include "trace.h" 26 #include "mmu.h" 27 #include "x86.h" 28 #include "smm.h" 29 #include "cpuid.h" 30 #include "lapic.h" 31 #include "svm.h" 32 #include "hyperv.h" 33 34 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK 35 36 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, 37 struct x86_exception *fault) 38 { 39 struct vcpu_svm *svm = to_svm(vcpu); 40 struct vmcb *vmcb = svm->vmcb; 41 42 if (vmcb->control.exit_code != SVM_EXIT_NPF) { 43 /* 44 * TODO: track the cause of the nested page fault, and 45 * correctly fill in the high bits of exit_info_1. 46 */ 47 vmcb->control.exit_code = SVM_EXIT_NPF; 48 vmcb->control.exit_code_hi = 0; 49 vmcb->control.exit_info_1 = (1ULL << 32); 50 vmcb->control.exit_info_2 = fault->address; 51 } 52 53 vmcb->control.exit_info_1 &= ~0xffffffffULL; 54 vmcb->control.exit_info_1 |= fault->error_code; 55 56 nested_svm_vmexit(svm); 57 } 58 59 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) 60 { 61 struct vcpu_svm *svm = to_svm(vcpu); 62 u64 cr3 = svm->nested.ctl.nested_cr3; 63 u64 pdpte; 64 int ret; 65 66 /* 67 * Note, nCR3 is "assumed" to be 32-byte aligned, i.e. the CPU ignores 68 * nCR3[4:0] when loading PDPTEs from memory. 69 */ 70 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte, 71 (cr3 & GENMASK(11, 5)) + index * 8, 8); 72 if (ret) 73 return 0; 74 return pdpte; 75 } 76 77 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) 78 { 79 struct vcpu_svm *svm = to_svm(vcpu); 80 81 return svm->nested.ctl.nested_cr3; 82 } 83 84 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) 85 { 86 struct vcpu_svm *svm = to_svm(vcpu); 87 88 WARN_ON(mmu_is_nested(vcpu)); 89 90 vcpu->arch.mmu = &vcpu->arch.guest_mmu; 91 92 /* 93 * The NPT format depends on L1's CR4 and EFER, which is in vmcb01. Note, 94 * when called via KVM_SET_NESTED_STATE, that state may _not_ match current 95 * vCPU state. CR0.WP is explicitly ignored, while CR0.PG is required. 96 */ 97 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4, 98 svm->vmcb01.ptr->save.efer, 99 svm->nested.ctl.nested_cr3); 100 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3; 101 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; 102 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; 103 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; 104 } 105 106 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) 107 { 108 vcpu->arch.mmu = &vcpu->arch.root_mmu; 109 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; 110 } 111 112 static bool nested_vmcb_needs_vls_intercept(struct vcpu_svm *svm) 113 { 114 if (!guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_V_VMSAVE_VMLOAD)) 115 return true; 116 117 if (!nested_npt_enabled(svm)) 118 return true; 119 120 if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK)) 121 return true; 122 123 return false; 124 } 125 126 void recalc_intercepts(struct vcpu_svm *svm) 127 { 128 struct vmcb_control_area *c, *h; 129 struct vmcb_ctrl_area_cached *g; 130 unsigned int i; 131 132 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 133 134 if (!is_guest_mode(&svm->vcpu)) 135 return; 136 137 c = &svm->vmcb->control; 138 h = &svm->vmcb01.ptr->control; 139 g = &svm->nested.ctl; 140 141 for (i = 0; i < MAX_INTERCEPT; i++) 142 c->intercepts[i] = h->intercepts[i]; 143 144 if (g->int_ctl & V_INTR_MASKING_MASK) { 145 /* 146 * If L2 is active and V_INTR_MASKING is enabled in vmcb12, 147 * disable intercept of CR8 writes as L2's CR8 does not affect 148 * any interrupt KVM may want to inject. 149 * 150 * Similarly, disable intercept of virtual interrupts (used to 151 * detect interrupt windows) if the saved RFLAGS.IF is '0', as 152 * the effective RFLAGS.IF for L1 interrupts will never be set 153 * while L2 is running (L2's RFLAGS.IF doesn't affect L1 IRQs). 154 */ 155 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE); 156 if (!(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)) 157 vmcb_clr_intercept(c, INTERCEPT_VINTR); 158 } 159 160 /* 161 * We want to see VMMCALLs from a nested guest only when Hyper-V L2 TLB 162 * flush feature is enabled. 163 */ 164 if (!nested_svm_l2_tlb_flush_enabled(&svm->vcpu)) 165 vmcb_clr_intercept(c, INTERCEPT_VMMCALL); 166 167 for (i = 0; i < MAX_INTERCEPT; i++) 168 c->intercepts[i] |= g->intercepts[i]; 169 170 /* If SMI is not intercepted, ignore guest SMI intercept as well */ 171 if (!intercept_smi) 172 vmcb_clr_intercept(c, INTERCEPT_SMI); 173 174 if (nested_vmcb_needs_vls_intercept(svm)) { 175 /* 176 * If the virtual VMLOAD/VMSAVE is not enabled for the L2, 177 * we must intercept these instructions to correctly 178 * emulate them in case L1 doesn't intercept them. 179 */ 180 vmcb_set_intercept(c, INTERCEPT_VMLOAD); 181 vmcb_set_intercept(c, INTERCEPT_VMSAVE); 182 } else { 183 WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK)); 184 } 185 } 186 187 /* 188 * This array (and its actual size) holds the set of offsets (indexing by chunk 189 * size) to process when merging vmcb12's MSRPM with vmcb01's MSRPM. Note, the 190 * set of MSRs for which interception is disabled in vmcb01 is per-vCPU, e.g. 191 * based on CPUID features. This array only tracks MSRs that *might* be passed 192 * through to the guest. 193 * 194 * Hardcode the capacity of the array based on the maximum number of _offsets_. 195 * MSRs are batched together, so there are fewer offsets than MSRs. 196 */ 197 static int nested_svm_msrpm_merge_offsets[7] __ro_after_init; 198 static int nested_svm_nr_msrpm_merge_offsets __ro_after_init; 199 typedef unsigned long nsvm_msrpm_merge_t; 200 201 int __init nested_svm_init_msrpm_merge_offsets(void) 202 { 203 static const u32 merge_msrs[] __initconst = { 204 MSR_STAR, 205 MSR_IA32_SYSENTER_CS, 206 MSR_IA32_SYSENTER_EIP, 207 MSR_IA32_SYSENTER_ESP, 208 #ifdef CONFIG_X86_64 209 MSR_GS_BASE, 210 MSR_FS_BASE, 211 MSR_KERNEL_GS_BASE, 212 MSR_LSTAR, 213 MSR_CSTAR, 214 MSR_SYSCALL_MASK, 215 #endif 216 MSR_IA32_SPEC_CTRL, 217 MSR_IA32_PRED_CMD, 218 MSR_IA32_FLUSH_CMD, 219 MSR_IA32_APERF, 220 MSR_IA32_MPERF, 221 MSR_IA32_LASTBRANCHFROMIP, 222 MSR_IA32_LASTBRANCHTOIP, 223 MSR_IA32_LASTINTFROMIP, 224 MSR_IA32_LASTINTTOIP, 225 }; 226 int i, j; 227 228 for (i = 0; i < ARRAY_SIZE(merge_msrs); i++) { 229 int bit_nr = svm_msrpm_bit_nr(merge_msrs[i]); 230 u32 offset; 231 232 if (WARN_ON(bit_nr < 0)) 233 return -EIO; 234 235 /* 236 * Merging is done in chunks to reduce the number of accesses 237 * to L1's bitmap. 238 */ 239 offset = bit_nr / BITS_PER_BYTE / sizeof(nsvm_msrpm_merge_t); 240 241 for (j = 0; j < nested_svm_nr_msrpm_merge_offsets; j++) { 242 if (nested_svm_msrpm_merge_offsets[j] == offset) 243 break; 244 } 245 246 if (j < nested_svm_nr_msrpm_merge_offsets) 247 continue; 248 249 if (WARN_ON(j >= ARRAY_SIZE(nested_svm_msrpm_merge_offsets))) 250 return -EIO; 251 252 nested_svm_msrpm_merge_offsets[j] = offset; 253 nested_svm_nr_msrpm_merge_offsets++; 254 } 255 256 return 0; 257 } 258 259 /* 260 * Merge L0's (KVM) and L1's (Nested VMCB) MSR permission bitmaps. The function 261 * is optimized in that it only merges the parts where KVM MSR permission bitmap 262 * may contain zero bits. 263 */ 264 static bool nested_svm_merge_msrpm(struct kvm_vcpu *vcpu) 265 { 266 struct vcpu_svm *svm = to_svm(vcpu); 267 nsvm_msrpm_merge_t *msrpm02 = svm->nested.msrpm; 268 nsvm_msrpm_merge_t *msrpm01 = svm->msrpm; 269 int i; 270 271 /* 272 * MSR bitmap update can be skipped when: 273 * - MSR bitmap for L1 hasn't changed. 274 * - Nested hypervisor (L1) is attempting to launch the same L2 as 275 * before. 276 * - Nested hypervisor (L1) is using Hyper-V emulation interface and 277 * tells KVM (L0) there were no changes in MSR bitmap for L2. 278 */ 279 #ifdef CONFIG_KVM_HYPERV 280 if (!svm->nested.force_msr_bitmap_recalc) { 281 struct hv_vmcb_enlightenments *hve = &svm->nested.ctl.hv_enlightenments; 282 283 if (kvm_hv_hypercall_enabled(vcpu) && 284 hve->hv_enlightenments_control.msr_bitmap && 285 (svm->nested.ctl.clean & BIT(HV_VMCB_NESTED_ENLIGHTENMENTS))) 286 goto set_msrpm_base_pa; 287 } 288 #endif 289 290 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT))) 291 return true; 292 293 for (i = 0; i < nested_svm_nr_msrpm_merge_offsets; i++) { 294 const int p = nested_svm_msrpm_merge_offsets[i]; 295 nsvm_msrpm_merge_t l1_val; 296 gpa_t gpa; 297 298 gpa = svm->nested.ctl.msrpm_base_pa + (p * sizeof(l1_val)); 299 300 if (kvm_vcpu_read_guest(vcpu, gpa, &l1_val, sizeof(l1_val))) 301 return false; 302 303 msrpm02[p] = msrpm01[p] | l1_val; 304 } 305 306 svm->nested.force_msr_bitmap_recalc = false; 307 308 #ifdef CONFIG_KVM_HYPERV 309 set_msrpm_base_pa: 310 #endif 311 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); 312 313 return true; 314 } 315 316 /* 317 * Bits 11:0 of bitmap address are ignored by hardware 318 */ 319 static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size) 320 { 321 u64 addr = PAGE_ALIGN(pa); 322 323 return kvm_vcpu_is_legal_gpa(vcpu, addr) && 324 kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1); 325 } 326 327 static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu, 328 struct vmcb_ctrl_area_cached *control) 329 { 330 if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN))) 331 return false; 332 333 if (CC(control->asid == 0)) 334 return false; 335 336 if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled)) 337 return false; 338 339 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa, 340 MSRPM_SIZE))) 341 return false; 342 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa, 343 IOPM_SIZE))) 344 return false; 345 346 if (CC((control->int_ctl & V_NMI_ENABLE_MASK) && 347 !vmcb12_is_intercept(control, INTERCEPT_NMI))) { 348 return false; 349 } 350 351 return true; 352 } 353 354 /* Common checks that apply to both L1 and L2 state. */ 355 static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu, 356 struct vmcb_save_area_cached *save) 357 { 358 if (CC(!(save->efer & EFER_SVME))) 359 return false; 360 361 if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) || 362 CC(save->cr0 & ~0xffffffffULL)) 363 return false; 364 365 if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7))) 366 return false; 367 368 /* 369 * These checks are also performed by KVM_SET_SREGS, 370 * except that EFER.LMA is not checked by SVM against 371 * CR0.PG && EFER.LME. 372 */ 373 if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) { 374 if (CC(!(save->cr4 & X86_CR4_PAE)) || 375 CC(!(save->cr0 & X86_CR0_PE)) || 376 CC(!kvm_vcpu_is_legal_cr3(vcpu, save->cr3))) 377 return false; 378 } 379 380 /* Note, SVM doesn't have any additional restrictions on CR4. */ 381 if (CC(!__kvm_is_valid_cr4(vcpu, save->cr4))) 382 return false; 383 384 if (CC(!kvm_valid_efer(vcpu, save->efer))) 385 return false; 386 387 return true; 388 } 389 390 static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu) 391 { 392 struct vcpu_svm *svm = to_svm(vcpu); 393 struct vmcb_save_area_cached *save = &svm->nested.save; 394 395 return __nested_vmcb_check_save(vcpu, save); 396 } 397 398 static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu) 399 { 400 struct vcpu_svm *svm = to_svm(vcpu); 401 struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl; 402 403 return __nested_vmcb_check_controls(vcpu, ctl); 404 } 405 406 static 407 void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu, 408 struct vmcb_ctrl_area_cached *to, 409 struct vmcb_control_area *from) 410 { 411 unsigned int i; 412 413 for (i = 0; i < MAX_INTERCEPT; i++) 414 to->intercepts[i] = from->intercepts[i]; 415 416 to->iopm_base_pa = from->iopm_base_pa; 417 to->msrpm_base_pa = from->msrpm_base_pa; 418 to->tsc_offset = from->tsc_offset; 419 to->tlb_ctl = from->tlb_ctl; 420 to->int_ctl = from->int_ctl; 421 to->int_vector = from->int_vector; 422 to->int_state = from->int_state; 423 to->exit_code = from->exit_code; 424 to->exit_code_hi = from->exit_code_hi; 425 to->exit_info_1 = from->exit_info_1; 426 to->exit_info_2 = from->exit_info_2; 427 to->exit_int_info = from->exit_int_info; 428 to->exit_int_info_err = from->exit_int_info_err; 429 to->nested_ctl = from->nested_ctl; 430 to->event_inj = from->event_inj; 431 to->event_inj_err = from->event_inj_err; 432 to->next_rip = from->next_rip; 433 to->nested_cr3 = from->nested_cr3; 434 to->virt_ext = from->virt_ext; 435 to->pause_filter_count = from->pause_filter_count; 436 to->pause_filter_thresh = from->pause_filter_thresh; 437 438 /* Copy asid here because nested_vmcb_check_controls will check it. */ 439 to->asid = from->asid; 440 to->msrpm_base_pa &= ~0x0fffULL; 441 to->iopm_base_pa &= ~0x0fffULL; 442 443 #ifdef CONFIG_KVM_HYPERV 444 /* Hyper-V extensions (Enlightened VMCB) */ 445 if (kvm_hv_hypercall_enabled(vcpu)) { 446 to->clean = from->clean; 447 memcpy(&to->hv_enlightenments, &from->hv_enlightenments, 448 sizeof(to->hv_enlightenments)); 449 } 450 #endif 451 } 452 453 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm, 454 struct vmcb_control_area *control) 455 { 456 __nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control); 457 } 458 459 static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to, 460 struct vmcb_save_area *from) 461 { 462 /* 463 * Copy only fields that are validated, as we need them 464 * to avoid TOC/TOU races. 465 */ 466 to->efer = from->efer; 467 to->cr0 = from->cr0; 468 to->cr3 = from->cr3; 469 to->cr4 = from->cr4; 470 471 to->dr6 = from->dr6; 472 to->dr7 = from->dr7; 473 } 474 475 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm, 476 struct vmcb_save_area *save) 477 { 478 __nested_copy_vmcb_save_to_cache(&svm->nested.save, save); 479 } 480 481 /* 482 * Synchronize fields that are written by the processor, so that 483 * they can be copied back into the vmcb12. 484 */ 485 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm) 486 { 487 u32 mask; 488 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj; 489 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err; 490 491 /* Only a few fields of int_ctl are written by the processor. */ 492 mask = V_IRQ_MASK | V_TPR_MASK; 493 /* 494 * Don't sync vmcb02 V_IRQ back to vmcb12 if KVM (L0) is intercepting 495 * virtual interrupts in order to request an interrupt window, as KVM 496 * has usurped vmcb02's int_ctl. If an interrupt window opens before 497 * the next VM-Exit, svm_clear_vintr() will restore vmcb12's int_ctl. 498 * If no window opens, V_IRQ will be correctly preserved in vmcb12's 499 * int_ctl (because it was never recognized while L2 was running). 500 */ 501 if (svm_is_intercept(svm, INTERCEPT_VINTR) && 502 !test_bit(INTERCEPT_VINTR, (unsigned long *)svm->nested.ctl.intercepts)) 503 mask &= ~V_IRQ_MASK; 504 505 if (nested_vgif_enabled(svm)) 506 mask |= V_GIF_MASK; 507 508 if (nested_vnmi_enabled(svm)) 509 mask |= V_NMI_BLOCKING_MASK | V_NMI_PENDING_MASK; 510 511 svm->nested.ctl.int_ctl &= ~mask; 512 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask; 513 } 514 515 /* 516 * Transfer any event that L0 or L1 wanted to inject into L2 to 517 * EXIT_INT_INFO. 518 */ 519 static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm, 520 struct vmcb *vmcb12) 521 { 522 struct kvm_vcpu *vcpu = &svm->vcpu; 523 u32 exit_int_info = 0; 524 unsigned int nr; 525 526 if (vcpu->arch.exception.injected) { 527 nr = vcpu->arch.exception.vector; 528 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT; 529 530 if (vcpu->arch.exception.has_error_code) { 531 exit_int_info |= SVM_EVTINJ_VALID_ERR; 532 vmcb12->control.exit_int_info_err = 533 vcpu->arch.exception.error_code; 534 } 535 536 } else if (vcpu->arch.nmi_injected) { 537 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; 538 539 } else if (vcpu->arch.interrupt.injected) { 540 nr = vcpu->arch.interrupt.nr; 541 exit_int_info = nr | SVM_EVTINJ_VALID; 542 543 if (vcpu->arch.interrupt.soft) 544 exit_int_info |= SVM_EVTINJ_TYPE_SOFT; 545 else 546 exit_int_info |= SVM_EVTINJ_TYPE_INTR; 547 } 548 549 vmcb12->control.exit_int_info = exit_int_info; 550 } 551 552 static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu) 553 { 554 /* Handle pending Hyper-V TLB flush requests */ 555 kvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled); 556 557 /* 558 * TODO: optimize unconditional TLB flush/MMU sync. A partial list of 559 * things to fix before this can be conditional: 560 * 561 * - Flush TLBs for both L1 and L2 remote TLB flush 562 * - Honor L1's request to flush an ASID on nested VMRUN 563 * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*] 564 * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN 565 * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST 566 * 567 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested 568 * NPT guest-physical mappings on VMRUN. 569 */ 570 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu); 571 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); 572 } 573 574 /* 575 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true 576 * if we are emulating VM-Entry into a guest with NPT enabled. 577 */ 578 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, 579 bool nested_npt, bool reload_pdptrs) 580 { 581 if (CC(!kvm_vcpu_is_legal_cr3(vcpu, cr3))) 582 return -EINVAL; 583 584 if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) && 585 CC(!load_pdptrs(vcpu, cr3))) 586 return -EINVAL; 587 588 vcpu->arch.cr3 = cr3; 589 590 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */ 591 kvm_init_mmu(vcpu); 592 593 if (!nested_npt) 594 kvm_mmu_new_pgd(vcpu, cr3); 595 596 return 0; 597 } 598 599 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm) 600 { 601 if (!svm->nested.vmcb02.ptr) 602 return; 603 604 /* FIXME: merge g_pat from vmcb01 and vmcb12. */ 605 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat; 606 } 607 608 static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12) 609 { 610 bool new_vmcb12 = false; 611 struct vmcb *vmcb01 = svm->vmcb01.ptr; 612 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr; 613 struct kvm_vcpu *vcpu = &svm->vcpu; 614 615 nested_vmcb02_compute_g_pat(svm); 616 617 /* Load the nested guest state */ 618 if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) { 619 new_vmcb12 = true; 620 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa; 621 svm->nested.force_msr_bitmap_recalc = true; 622 } 623 624 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) { 625 vmcb02->save.es = vmcb12->save.es; 626 vmcb02->save.cs = vmcb12->save.cs; 627 vmcb02->save.ss = vmcb12->save.ss; 628 vmcb02->save.ds = vmcb12->save.ds; 629 vmcb02->save.cpl = vmcb12->save.cpl; 630 vmcb_mark_dirty(vmcb02, VMCB_SEG); 631 } 632 633 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) { 634 vmcb02->save.gdtr = vmcb12->save.gdtr; 635 vmcb02->save.idtr = vmcb12->save.idtr; 636 vmcb_mark_dirty(vmcb02, VMCB_DT); 637 } 638 639 if (guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK) && 640 (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_CET)))) { 641 vmcb02->save.s_cet = vmcb12->save.s_cet; 642 vmcb02->save.isst_addr = vmcb12->save.isst_addr; 643 vmcb02->save.ssp = vmcb12->save.ssp; 644 vmcb_mark_dirty(vmcb02, VMCB_CET); 645 } 646 647 kvm_set_rflags(vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED); 648 649 svm_set_efer(vcpu, svm->nested.save.efer); 650 651 svm_set_cr0(vcpu, svm->nested.save.cr0); 652 svm_set_cr4(vcpu, svm->nested.save.cr4); 653 654 svm->vcpu.arch.cr2 = vmcb12->save.cr2; 655 656 kvm_rax_write(vcpu, vmcb12->save.rax); 657 kvm_rsp_write(vcpu, vmcb12->save.rsp); 658 kvm_rip_write(vcpu, vmcb12->save.rip); 659 660 /* In case we don't even reach vcpu_run, the fields are not updated */ 661 vmcb02->save.rax = vmcb12->save.rax; 662 vmcb02->save.rsp = vmcb12->save.rsp; 663 vmcb02->save.rip = vmcb12->save.rip; 664 665 /* These bits will be set properly on the first execution when new_vmc12 is true */ 666 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) { 667 vmcb02->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1; 668 svm->vcpu.arch.dr6 = svm->nested.save.dr6 | DR6_ACTIVE_LOW; 669 vmcb_mark_dirty(vmcb02, VMCB_DR); 670 } 671 672 if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && 673 (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) { 674 /* 675 * Reserved bits of DEBUGCTL are ignored. Be consistent with 676 * svm_set_msr's definition of reserved bits. 677 */ 678 svm_copy_lbrs(vmcb02, vmcb12); 679 vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS; 680 } else { 681 svm_copy_lbrs(vmcb02, vmcb01); 682 } 683 svm_update_lbrv(&svm->vcpu); 684 } 685 686 static inline bool is_evtinj_soft(u32 evtinj) 687 { 688 u32 type = evtinj & SVM_EVTINJ_TYPE_MASK; 689 u8 vector = evtinj & SVM_EVTINJ_VEC_MASK; 690 691 if (!(evtinj & SVM_EVTINJ_VALID)) 692 return false; 693 694 if (type == SVM_EVTINJ_TYPE_SOFT) 695 return true; 696 697 return type == SVM_EVTINJ_TYPE_EXEPT && kvm_exception_is_soft(vector); 698 } 699 700 static bool is_evtinj_nmi(u32 evtinj) 701 { 702 u32 type = evtinj & SVM_EVTINJ_TYPE_MASK; 703 704 if (!(evtinj & SVM_EVTINJ_VALID)) 705 return false; 706 707 return type == SVM_EVTINJ_TYPE_NMI; 708 } 709 710 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, 711 unsigned long vmcb12_rip, 712 unsigned long vmcb12_csbase) 713 { 714 u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK; 715 u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK; 716 717 struct kvm_vcpu *vcpu = &svm->vcpu; 718 struct vmcb *vmcb01 = svm->vmcb01.ptr; 719 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr; 720 u32 pause_count12; 721 u32 pause_thresh12; 722 723 nested_svm_transition_tlb_flush(vcpu); 724 725 /* Enter Guest-Mode */ 726 enter_guest_mode(vcpu); 727 728 /* 729 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2, 730 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes. 731 */ 732 733 if (guest_cpu_cap_has(vcpu, X86_FEATURE_VGIF) && 734 (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK)) 735 int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK); 736 else 737 int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK); 738 739 if (vnmi) { 740 if (vmcb01->control.int_ctl & V_NMI_PENDING_MASK) { 741 svm->vcpu.arch.nmi_pending++; 742 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); 743 } 744 if (nested_vnmi_enabled(svm)) 745 int_ctl_vmcb12_bits |= (V_NMI_PENDING_MASK | 746 V_NMI_ENABLE_MASK | 747 V_NMI_BLOCKING_MASK); 748 } 749 750 /* Copied from vmcb01. msrpm_base can be overwritten later. */ 751 vmcb02->control.nested_ctl = vmcb01->control.nested_ctl; 752 vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa; 753 vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa; 754 755 /* 756 * Stash vmcb02's counter if the guest hasn't moved past the guilty 757 * instruction; otherwise, reset the counter to '0'. 758 * 759 * In order to detect if L2 has made forward progress or not, track the 760 * RIP at which a bus lock has occurred on a per-vmcb12 basis. If RIP 761 * is changed, guest has clearly made forward progress, bus_lock_counter 762 * still remained '1', so reset bus_lock_counter to '0'. Eg. In the 763 * scenario, where a buslock happened in L1 before VMRUN, the bus lock 764 * firmly happened on an instruction in the past. Even if vmcb01's 765 * counter is still '1', (because the guilty instruction got patched), 766 * the vCPU has clearly made forward progress and so KVM should reset 767 * vmcb02's counter to '0'. 768 * 769 * If the RIP hasn't changed, stash the bus lock counter at nested VMRUN 770 * to prevent the same guilty instruction from triggering a VM-Exit. Eg. 771 * if userspace rate-limits the vCPU, then it's entirely possible that 772 * L1's tick interrupt is pending by the time userspace re-runs the 773 * vCPU. If KVM unconditionally clears the counter on VMRUN, then when 774 * L1 re-enters L2, the same instruction will trigger a VM-Exit and the 775 * entire cycle start over. 776 */ 777 if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip)) 778 vmcb02->control.bus_lock_counter = 1; 779 else 780 vmcb02->control.bus_lock_counter = 0; 781 782 /* Done at vmrun: asid. */ 783 784 /* Also overwritten later if necessary. */ 785 vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; 786 787 /* nested_cr3. */ 788 if (nested_npt_enabled(svm)) 789 nested_svm_init_mmu_context(vcpu); 790 791 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( 792 vcpu->arch.l1_tsc_offset, 793 svm->nested.ctl.tsc_offset, 794 svm->tsc_ratio_msr); 795 796 vmcb02->control.tsc_offset = vcpu->arch.tsc_offset; 797 798 if (guest_cpu_cap_has(vcpu, X86_FEATURE_TSCRATEMSR) && 799 svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) 800 nested_svm_update_tsc_ratio_msr(vcpu); 801 802 vmcb02->control.int_ctl = 803 (svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) | 804 (vmcb01->control.int_ctl & int_ctl_vmcb01_bits); 805 806 vmcb02->control.int_vector = svm->nested.ctl.int_vector; 807 vmcb02->control.int_state = svm->nested.ctl.int_state; 808 vmcb02->control.event_inj = svm->nested.ctl.event_inj; 809 vmcb02->control.event_inj_err = svm->nested.ctl.event_inj_err; 810 811 /* 812 * next_rip is consumed on VMRUN as the return address pushed on the 813 * stack for injected soft exceptions/interrupts. If nrips is exposed 814 * to L1, take it verbatim from vmcb12. If nrips is supported in 815 * hardware but not exposed to L1, stuff the actual L2 RIP to emulate 816 * what a nrips=0 CPU would do (L1 is responsible for advancing RIP 817 * prior to injecting the event). 818 */ 819 if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS)) 820 vmcb02->control.next_rip = svm->nested.ctl.next_rip; 821 else if (boot_cpu_has(X86_FEATURE_NRIPS)) 822 vmcb02->control.next_rip = vmcb12_rip; 823 824 svm->nmi_l1_to_l2 = is_evtinj_nmi(vmcb02->control.event_inj); 825 if (is_evtinj_soft(vmcb02->control.event_inj)) { 826 svm->soft_int_injected = true; 827 svm->soft_int_csbase = vmcb12_csbase; 828 svm->soft_int_old_rip = vmcb12_rip; 829 if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS)) 830 svm->soft_int_next_rip = svm->nested.ctl.next_rip; 831 else 832 svm->soft_int_next_rip = vmcb12_rip; 833 } 834 835 /* LBR_CTL_ENABLE_MASK is controlled by svm_update_lbrv() */ 836 837 if (!nested_vmcb_needs_vls_intercept(svm)) 838 vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; 839 840 if (guest_cpu_cap_has(vcpu, X86_FEATURE_PAUSEFILTER)) 841 pause_count12 = svm->nested.ctl.pause_filter_count; 842 else 843 pause_count12 = 0; 844 if (guest_cpu_cap_has(vcpu, X86_FEATURE_PFTHRESHOLD)) 845 pause_thresh12 = svm->nested.ctl.pause_filter_thresh; 846 else 847 pause_thresh12 = 0; 848 if (kvm_pause_in_guest(svm->vcpu.kvm)) { 849 /* use guest values since host doesn't intercept PAUSE */ 850 vmcb02->control.pause_filter_count = pause_count12; 851 vmcb02->control.pause_filter_thresh = pause_thresh12; 852 853 } else { 854 /* start from host values otherwise */ 855 vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count; 856 vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh; 857 858 /* ... but ensure filtering is disabled if so requested. */ 859 if (vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) { 860 if (!pause_count12) 861 vmcb02->control.pause_filter_count = 0; 862 if (!pause_thresh12) 863 vmcb02->control.pause_filter_thresh = 0; 864 } 865 } 866 867 /* 868 * Merge guest and host intercepts - must be called with vcpu in 869 * guest-mode to take effect. 870 */ 871 recalc_intercepts(svm); 872 } 873 874 static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb) 875 { 876 /* 877 * Some VMCB state is shared between L1 and L2 and thus has to be 878 * moved at the time of nested vmrun and vmexit. 879 * 880 * VMLOAD/VMSAVE state would also belong in this category, but KVM 881 * always performs VMLOAD and VMSAVE from the VMCB01. 882 */ 883 to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl; 884 } 885 886 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, 887 struct vmcb *vmcb12, bool from_vmrun) 888 { 889 struct vcpu_svm *svm = to_svm(vcpu); 890 int ret; 891 892 trace_kvm_nested_vmenter(svm->vmcb->save.rip, 893 vmcb12_gpa, 894 vmcb12->save.rip, 895 vmcb12->control.int_ctl, 896 vmcb12->control.event_inj, 897 vmcb12->control.nested_ctl, 898 vmcb12->control.nested_cr3, 899 vmcb12->save.cr3, 900 KVM_ISA_SVM); 901 902 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff, 903 vmcb12->control.intercepts[INTERCEPT_CR] >> 16, 904 vmcb12->control.intercepts[INTERCEPT_EXCEPTION], 905 vmcb12->control.intercepts[INTERCEPT_WORD3], 906 vmcb12->control.intercepts[INTERCEPT_WORD4], 907 vmcb12->control.intercepts[INTERCEPT_WORD5]); 908 909 910 svm->nested.vmcb12_gpa = vmcb12_gpa; 911 912 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr); 913 914 nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr); 915 916 svm_switch_vmcb(svm, &svm->nested.vmcb02); 917 nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base); 918 nested_vmcb02_prepare_save(svm, vmcb12); 919 920 ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3, 921 nested_npt_enabled(svm), from_vmrun); 922 if (ret) 923 return ret; 924 925 if (!from_vmrun) 926 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); 927 928 svm_set_gif(svm, true); 929 930 if (kvm_vcpu_apicv_active(vcpu)) 931 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu); 932 933 nested_svm_hv_update_vm_vp_ids(vcpu); 934 935 return 0; 936 } 937 938 int nested_svm_vmrun(struct kvm_vcpu *vcpu) 939 { 940 struct vcpu_svm *svm = to_svm(vcpu); 941 int ret; 942 struct vmcb *vmcb12; 943 struct kvm_host_map map; 944 u64 vmcb12_gpa; 945 struct vmcb *vmcb01 = svm->vmcb01.ptr; 946 947 if (!svm->nested.hsave_msr) { 948 kvm_inject_gp(vcpu, 0); 949 return 1; 950 } 951 952 if (is_smm(vcpu)) { 953 kvm_queue_exception(vcpu, UD_VECTOR); 954 return 1; 955 } 956 957 /* This fails when VP assist page is enabled but the supplied GPA is bogus */ 958 ret = kvm_hv_verify_vp_assist(vcpu); 959 if (ret) { 960 kvm_inject_gp(vcpu, 0); 961 return ret; 962 } 963 964 vmcb12_gpa = svm->vmcb->save.rax; 965 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map); 966 if (ret == -EINVAL) { 967 kvm_inject_gp(vcpu, 0); 968 return 1; 969 } else if (ret) { 970 return kvm_skip_emulated_instruction(vcpu); 971 } 972 973 ret = kvm_skip_emulated_instruction(vcpu); 974 975 vmcb12 = map.hva; 976 977 if (WARN_ON_ONCE(!svm->nested.initialized)) 978 return -EINVAL; 979 980 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control); 981 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save); 982 983 if (!nested_vmcb_check_save(vcpu) || 984 !nested_vmcb_check_controls(vcpu)) { 985 vmcb12->control.exit_code = SVM_EXIT_ERR; 986 vmcb12->control.exit_code_hi = 0; 987 vmcb12->control.exit_info_1 = 0; 988 vmcb12->control.exit_info_2 = 0; 989 goto out; 990 } 991 992 /* 993 * Since vmcb01 is not in use, we can use it to store some of the L1 994 * state. 995 */ 996 vmcb01->save.efer = vcpu->arch.efer; 997 vmcb01->save.cr0 = kvm_read_cr0(vcpu); 998 vmcb01->save.cr4 = vcpu->arch.cr4; 999 vmcb01->save.rflags = kvm_get_rflags(vcpu); 1000 vmcb01->save.rip = kvm_rip_read(vcpu); 1001 1002 if (!npt_enabled) 1003 vmcb01->save.cr3 = kvm_read_cr3(vcpu); 1004 1005 svm->nested.nested_run_pending = 1; 1006 1007 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true)) 1008 goto out_exit_err; 1009 1010 if (nested_svm_merge_msrpm(vcpu)) 1011 goto out; 1012 1013 out_exit_err: 1014 svm->nested.nested_run_pending = 0; 1015 svm->nmi_l1_to_l2 = false; 1016 svm->soft_int_injected = false; 1017 1018 svm->vmcb->control.exit_code = SVM_EXIT_ERR; 1019 svm->vmcb->control.exit_code_hi = 0; 1020 svm->vmcb->control.exit_info_1 = 0; 1021 svm->vmcb->control.exit_info_2 = 0; 1022 1023 nested_svm_vmexit(svm); 1024 1025 out: 1026 kvm_vcpu_unmap(vcpu, &map); 1027 1028 return ret; 1029 } 1030 1031 /* Copy state save area fields which are handled by VMRUN */ 1032 void svm_copy_vmrun_state(struct vmcb_save_area *to_save, 1033 struct vmcb_save_area *from_save) 1034 { 1035 to_save->es = from_save->es; 1036 to_save->cs = from_save->cs; 1037 to_save->ss = from_save->ss; 1038 to_save->ds = from_save->ds; 1039 to_save->gdtr = from_save->gdtr; 1040 to_save->idtr = from_save->idtr; 1041 to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED; 1042 to_save->efer = from_save->efer; 1043 to_save->cr0 = from_save->cr0; 1044 to_save->cr3 = from_save->cr3; 1045 to_save->cr4 = from_save->cr4; 1046 to_save->rax = from_save->rax; 1047 to_save->rsp = from_save->rsp; 1048 to_save->rip = from_save->rip; 1049 to_save->cpl = 0; 1050 1051 if (kvm_cpu_cap_has(X86_FEATURE_SHSTK)) { 1052 to_save->s_cet = from_save->s_cet; 1053 to_save->isst_addr = from_save->isst_addr; 1054 to_save->ssp = from_save->ssp; 1055 } 1056 } 1057 1058 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb) 1059 { 1060 to_vmcb->save.fs = from_vmcb->save.fs; 1061 to_vmcb->save.gs = from_vmcb->save.gs; 1062 to_vmcb->save.tr = from_vmcb->save.tr; 1063 to_vmcb->save.ldtr = from_vmcb->save.ldtr; 1064 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; 1065 to_vmcb->save.star = from_vmcb->save.star; 1066 to_vmcb->save.lstar = from_vmcb->save.lstar; 1067 to_vmcb->save.cstar = from_vmcb->save.cstar; 1068 to_vmcb->save.sfmask = from_vmcb->save.sfmask; 1069 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; 1070 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; 1071 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; 1072 } 1073 1074 int nested_svm_vmexit(struct vcpu_svm *svm) 1075 { 1076 struct kvm_vcpu *vcpu = &svm->vcpu; 1077 struct vmcb *vmcb01 = svm->vmcb01.ptr; 1078 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr; 1079 struct vmcb *vmcb12; 1080 struct kvm_host_map map; 1081 int rc; 1082 1083 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map); 1084 if (rc) { 1085 if (rc == -EINVAL) 1086 kvm_inject_gp(vcpu, 0); 1087 return 1; 1088 } 1089 1090 vmcb12 = map.hva; 1091 1092 /* Exit Guest-Mode */ 1093 leave_guest_mode(vcpu); 1094 svm->nested.vmcb12_gpa = 0; 1095 WARN_ON_ONCE(svm->nested.nested_run_pending); 1096 1097 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); 1098 1099 /* in case we halted in L2 */ 1100 kvm_set_mp_state(vcpu, KVM_MP_STATE_RUNNABLE); 1101 1102 /* Give the current vmcb to the guest */ 1103 1104 vmcb12->save.es = vmcb02->save.es; 1105 vmcb12->save.cs = vmcb02->save.cs; 1106 vmcb12->save.ss = vmcb02->save.ss; 1107 vmcb12->save.ds = vmcb02->save.ds; 1108 vmcb12->save.gdtr = vmcb02->save.gdtr; 1109 vmcb12->save.idtr = vmcb02->save.idtr; 1110 vmcb12->save.efer = svm->vcpu.arch.efer; 1111 vmcb12->save.cr0 = kvm_read_cr0(vcpu); 1112 vmcb12->save.cr3 = kvm_read_cr3(vcpu); 1113 vmcb12->save.cr2 = vmcb02->save.cr2; 1114 vmcb12->save.cr4 = svm->vcpu.arch.cr4; 1115 vmcb12->save.rflags = kvm_get_rflags(vcpu); 1116 vmcb12->save.rip = kvm_rip_read(vcpu); 1117 vmcb12->save.rsp = kvm_rsp_read(vcpu); 1118 vmcb12->save.rax = kvm_rax_read(vcpu); 1119 vmcb12->save.dr7 = vmcb02->save.dr7; 1120 vmcb12->save.dr6 = svm->vcpu.arch.dr6; 1121 vmcb12->save.cpl = vmcb02->save.cpl; 1122 1123 if (guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK)) { 1124 vmcb12->save.s_cet = vmcb02->save.s_cet; 1125 vmcb12->save.isst_addr = vmcb02->save.isst_addr; 1126 vmcb12->save.ssp = vmcb02->save.ssp; 1127 } 1128 1129 vmcb12->control.int_state = vmcb02->control.int_state; 1130 vmcb12->control.exit_code = vmcb02->control.exit_code; 1131 vmcb12->control.exit_code_hi = vmcb02->control.exit_code_hi; 1132 vmcb12->control.exit_info_1 = vmcb02->control.exit_info_1; 1133 vmcb12->control.exit_info_2 = vmcb02->control.exit_info_2; 1134 1135 if (vmcb12->control.exit_code != SVM_EXIT_ERR) 1136 nested_save_pending_event_to_vmcb12(svm, vmcb12); 1137 1138 if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS)) 1139 vmcb12->control.next_rip = vmcb02->control.next_rip; 1140 1141 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl; 1142 vmcb12->control.event_inj = svm->nested.ctl.event_inj; 1143 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err; 1144 1145 if (!kvm_pause_in_guest(vcpu->kvm)) { 1146 vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count; 1147 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS); 1148 1149 } 1150 1151 /* 1152 * Invalidate bus_lock_rip unless KVM is still waiting for the guest 1153 * to make forward progress before re-enabling bus lock detection. 1154 */ 1155 if (!vmcb02->control.bus_lock_counter) 1156 svm->nested.ctl.bus_lock_rip = INVALID_GPA; 1157 1158 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr); 1159 1160 kvm_nested_vmexit_handle_ibrs(vcpu); 1161 1162 svm_switch_vmcb(svm, &svm->vmcb01); 1163 1164 /* 1165 * Rules for synchronizing int_ctl bits from vmcb02 to vmcb01: 1166 * 1167 * V_IRQ, V_IRQ_VECTOR, V_INTR_PRIO_MASK, V_IGN_TPR: If L1 doesn't 1168 * intercept interrupts, then KVM will use vmcb02's V_IRQ (and related 1169 * flags) to detect interrupt windows for L1 IRQs (even if L1 uses 1170 * virtual interrupt masking). Raise KVM_REQ_EVENT to ensure that 1171 * KVM re-requests an interrupt window if necessary, which implicitly 1172 * copies this bits from vmcb02 to vmcb01. 1173 * 1174 * V_TPR: If L1 doesn't use virtual interrupt masking, then L1's vTPR 1175 * is stored in vmcb02, but its value doesn't need to be copied from/to 1176 * vmcb01 because it is copied from/to the virtual APIC's TPR register 1177 * on each VM entry/exit. 1178 * 1179 * V_GIF: If nested vGIF is not used, KVM uses vmcb02's V_GIF for L1's 1180 * V_GIF. However, GIF is architecturally clear on each VM exit, thus 1181 * there is no need to copy V_GIF from vmcb02 to vmcb01. 1182 */ 1183 if (!nested_exit_on_intr(svm)) 1184 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); 1185 1186 if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && 1187 (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) 1188 svm_copy_lbrs(vmcb12, vmcb02); 1189 else 1190 svm_copy_lbrs(vmcb01, vmcb02); 1191 1192 svm_update_lbrv(vcpu); 1193 1194 if (vnmi) { 1195 if (vmcb02->control.int_ctl & V_NMI_BLOCKING_MASK) 1196 vmcb01->control.int_ctl |= V_NMI_BLOCKING_MASK; 1197 else 1198 vmcb01->control.int_ctl &= ~V_NMI_BLOCKING_MASK; 1199 1200 if (vcpu->arch.nmi_pending) { 1201 vcpu->arch.nmi_pending--; 1202 vmcb01->control.int_ctl |= V_NMI_PENDING_MASK; 1203 } else { 1204 vmcb01->control.int_ctl &= ~V_NMI_PENDING_MASK; 1205 } 1206 } 1207 1208 /* 1209 * On vmexit the GIF is set to false and 1210 * no event can be injected in L1. 1211 */ 1212 svm_set_gif(svm, false); 1213 vmcb01->control.exit_int_info = 0; 1214 1215 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset; 1216 if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) { 1217 vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset; 1218 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS); 1219 } 1220 1221 if (kvm_caps.has_tsc_control && 1222 vcpu->arch.tsc_scaling_ratio != vcpu->arch.l1_tsc_scaling_ratio) { 1223 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; 1224 svm_write_tsc_multiplier(vcpu); 1225 } 1226 1227 svm->nested.ctl.nested_cr3 = 0; 1228 1229 /* 1230 * Restore processor state that had been saved in vmcb01 1231 */ 1232 kvm_set_rflags(vcpu, vmcb01->save.rflags); 1233 svm_set_efer(vcpu, vmcb01->save.efer); 1234 svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE); 1235 svm_set_cr4(vcpu, vmcb01->save.cr4); 1236 kvm_rax_write(vcpu, vmcb01->save.rax); 1237 kvm_rsp_write(vcpu, vmcb01->save.rsp); 1238 kvm_rip_write(vcpu, vmcb01->save.rip); 1239 1240 svm->vcpu.arch.dr7 = DR7_FIXED_1; 1241 kvm_update_dr7(&svm->vcpu); 1242 1243 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code, 1244 vmcb12->control.exit_info_1, 1245 vmcb12->control.exit_info_2, 1246 vmcb12->control.exit_int_info, 1247 vmcb12->control.exit_int_info_err, 1248 KVM_ISA_SVM); 1249 1250 kvm_vcpu_unmap(vcpu, &map); 1251 1252 nested_svm_transition_tlb_flush(vcpu); 1253 1254 nested_svm_uninit_mmu_context(vcpu); 1255 1256 rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true); 1257 if (rc) 1258 return 1; 1259 1260 /* 1261 * Drop what we picked up for L2 via svm_complete_interrupts() so it 1262 * doesn't end up in L1. 1263 */ 1264 svm->vcpu.arch.nmi_injected = false; 1265 kvm_clear_exception_queue(vcpu); 1266 kvm_clear_interrupt_queue(vcpu); 1267 1268 /* 1269 * If we are here following the completion of a VMRUN that 1270 * is being single-stepped, queue the pending #DB intercept 1271 * right now so that it an be accounted for before we execute 1272 * L1's next instruction. 1273 */ 1274 if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF)) 1275 kvm_queue_exception(&(svm->vcpu), DB_VECTOR); 1276 1277 /* 1278 * Un-inhibit the AVIC right away, so that other vCPUs can start 1279 * to benefit from it right away. 1280 */ 1281 if (kvm_apicv_activated(vcpu->kvm)) 1282 __kvm_vcpu_update_apicv(vcpu); 1283 1284 return 0; 1285 } 1286 1287 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu) 1288 { 1289 struct vcpu_svm *svm = to_svm(vcpu); 1290 1291 if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SHUTDOWN)) 1292 return; 1293 1294 kvm_clear_request(KVM_REQ_TRIPLE_FAULT, vcpu); 1295 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN); 1296 } 1297 1298 int svm_allocate_nested(struct vcpu_svm *svm) 1299 { 1300 struct page *vmcb02_page; 1301 1302 if (svm->nested.initialized) 1303 return 0; 1304 1305 vmcb02_page = snp_safe_alloc_page(); 1306 if (!vmcb02_page) 1307 return -ENOMEM; 1308 svm->nested.vmcb02.ptr = page_address(vmcb02_page); 1309 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT); 1310 1311 svm->nested.msrpm = svm_vcpu_alloc_msrpm(); 1312 if (!svm->nested.msrpm) 1313 goto err_free_vmcb02; 1314 1315 svm->nested.initialized = true; 1316 return 0; 1317 1318 err_free_vmcb02: 1319 __free_page(vmcb02_page); 1320 return -ENOMEM; 1321 } 1322 1323 void svm_free_nested(struct vcpu_svm *svm) 1324 { 1325 if (!svm->nested.initialized) 1326 return; 1327 1328 if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr)) 1329 svm_switch_vmcb(svm, &svm->vmcb01); 1330 1331 svm_vcpu_free_msrpm(svm->nested.msrpm); 1332 svm->nested.msrpm = NULL; 1333 1334 __free_page(virt_to_page(svm->nested.vmcb02.ptr)); 1335 svm->nested.vmcb02.ptr = NULL; 1336 1337 /* 1338 * When last_vmcb12_gpa matches the current vmcb12 gpa, 1339 * some vmcb12 fields are not loaded if they are marked clean 1340 * in the vmcb12, since in this case they are up to date already. 1341 * 1342 * When the vmcb02 is freed, this optimization becomes invalid. 1343 */ 1344 svm->nested.last_vmcb12_gpa = INVALID_GPA; 1345 1346 svm->nested.initialized = false; 1347 } 1348 1349 void svm_leave_nested(struct kvm_vcpu *vcpu) 1350 { 1351 struct vcpu_svm *svm = to_svm(vcpu); 1352 1353 if (is_guest_mode(vcpu)) { 1354 svm->nested.nested_run_pending = 0; 1355 svm->nested.vmcb12_gpa = INVALID_GPA; 1356 1357 leave_guest_mode(vcpu); 1358 1359 svm_switch_vmcb(svm, &svm->vmcb01); 1360 1361 nested_svm_uninit_mmu_context(vcpu); 1362 vmcb_mark_all_dirty(svm->vmcb); 1363 1364 if (kvm_apicv_activated(vcpu->kvm)) 1365 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu); 1366 } 1367 1368 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); 1369 } 1370 1371 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) 1372 { 1373 gpa_t base = svm->nested.ctl.msrpm_base_pa; 1374 int write, bit_nr; 1375 u8 value, mask; 1376 u32 msr; 1377 1378 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT))) 1379 return NESTED_EXIT_HOST; 1380 1381 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; 1382 bit_nr = svm_msrpm_bit_nr(msr); 1383 write = svm->vmcb->control.exit_info_1 & 1; 1384 1385 if (bit_nr < 0) 1386 return NESTED_EXIT_DONE; 1387 1388 if (kvm_vcpu_read_guest(&svm->vcpu, base + bit_nr / BITS_PER_BYTE, 1389 &value, sizeof(value))) 1390 return NESTED_EXIT_DONE; 1391 1392 mask = BIT(write) << (bit_nr & (BITS_PER_BYTE - 1)); 1393 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 1394 } 1395 1396 static int nested_svm_intercept_ioio(struct vcpu_svm *svm) 1397 { 1398 unsigned port, size, iopm_len; 1399 u16 val, mask; 1400 u8 start_bit; 1401 u64 gpa; 1402 1403 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT))) 1404 return NESTED_EXIT_HOST; 1405 1406 port = svm->vmcb->control.exit_info_1 >> 16; 1407 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> 1408 SVM_IOIO_SIZE_SHIFT; 1409 gpa = svm->nested.ctl.iopm_base_pa + (port / 8); 1410 start_bit = port % 8; 1411 iopm_len = (start_bit + size > 8) ? 2 : 1; 1412 mask = (0xf >> (4 - size)) << start_bit; 1413 val = 0; 1414 1415 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) 1416 return NESTED_EXIT_DONE; 1417 1418 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 1419 } 1420 1421 static int nested_svm_intercept(struct vcpu_svm *svm) 1422 { 1423 u32 exit_code = svm->vmcb->control.exit_code; 1424 int vmexit = NESTED_EXIT_HOST; 1425 1426 switch (exit_code) { 1427 case SVM_EXIT_MSR: 1428 vmexit = nested_svm_exit_handled_msr(svm); 1429 break; 1430 case SVM_EXIT_IOIO: 1431 vmexit = nested_svm_intercept_ioio(svm); 1432 break; 1433 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { 1434 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code)) 1435 vmexit = NESTED_EXIT_DONE; 1436 break; 1437 } 1438 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { 1439 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code)) 1440 vmexit = NESTED_EXIT_DONE; 1441 break; 1442 } 1443 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { 1444 /* 1445 * Host-intercepted exceptions have been checked already in 1446 * nested_svm_exit_special. There is nothing to do here, 1447 * the vmexit is injected by svm_check_nested_events. 1448 */ 1449 vmexit = NESTED_EXIT_DONE; 1450 break; 1451 } 1452 case SVM_EXIT_ERR: { 1453 vmexit = NESTED_EXIT_DONE; 1454 break; 1455 } 1456 default: { 1457 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code)) 1458 vmexit = NESTED_EXIT_DONE; 1459 } 1460 } 1461 1462 return vmexit; 1463 } 1464 1465 int nested_svm_exit_handled(struct vcpu_svm *svm) 1466 { 1467 int vmexit; 1468 1469 vmexit = nested_svm_intercept(svm); 1470 1471 if (vmexit == NESTED_EXIT_DONE) 1472 nested_svm_vmexit(svm); 1473 1474 return vmexit; 1475 } 1476 1477 int nested_svm_check_permissions(struct kvm_vcpu *vcpu) 1478 { 1479 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) { 1480 kvm_queue_exception(vcpu, UD_VECTOR); 1481 return 1; 1482 } 1483 1484 if (to_svm(vcpu)->vmcb->save.cpl) { 1485 kvm_inject_gp(vcpu, 0); 1486 return 1; 1487 } 1488 1489 return 0; 1490 } 1491 1492 static bool nested_svm_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector, 1493 u32 error_code) 1494 { 1495 struct vcpu_svm *svm = to_svm(vcpu); 1496 1497 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(vector)); 1498 } 1499 1500 static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu) 1501 { 1502 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit; 1503 struct vcpu_svm *svm = to_svm(vcpu); 1504 struct vmcb *vmcb = svm->vmcb; 1505 1506 vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + ex->vector; 1507 vmcb->control.exit_code_hi = 0; 1508 1509 if (ex->has_error_code) 1510 vmcb->control.exit_info_1 = ex->error_code; 1511 1512 /* 1513 * EXITINFO2 is undefined for all exception intercepts other 1514 * than #PF. 1515 */ 1516 if (ex->vector == PF_VECTOR) { 1517 if (ex->has_payload) 1518 vmcb->control.exit_info_2 = ex->payload; 1519 else 1520 vmcb->control.exit_info_2 = vcpu->arch.cr2; 1521 } else if (ex->vector == DB_VECTOR) { 1522 /* See kvm_check_and_inject_events(). */ 1523 kvm_deliver_exception_payload(vcpu, ex); 1524 1525 if (vcpu->arch.dr7 & DR7_GD) { 1526 vcpu->arch.dr7 &= ~DR7_GD; 1527 kvm_update_dr7(vcpu); 1528 } 1529 } else { 1530 WARN_ON(ex->has_payload); 1531 } 1532 1533 nested_svm_vmexit(svm); 1534 } 1535 1536 static inline bool nested_exit_on_init(struct vcpu_svm *svm) 1537 { 1538 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT); 1539 } 1540 1541 static int svm_check_nested_events(struct kvm_vcpu *vcpu) 1542 { 1543 struct kvm_lapic *apic = vcpu->arch.apic; 1544 struct vcpu_svm *svm = to_svm(vcpu); 1545 /* 1546 * Only a pending nested run blocks a pending exception. If there is a 1547 * previously injected event, the pending exception occurred while said 1548 * event was being delivered and thus needs to be handled. 1549 */ 1550 bool block_nested_exceptions = svm->nested.nested_run_pending; 1551 /* 1552 * New events (not exceptions) are only recognized at instruction 1553 * boundaries. If an event needs reinjection, then KVM is handling a 1554 * VM-Exit that occurred _during_ instruction execution; new events are 1555 * blocked until the instruction completes. 1556 */ 1557 bool block_nested_events = block_nested_exceptions || 1558 kvm_event_needs_reinjection(vcpu); 1559 1560 if (lapic_in_kernel(vcpu) && 1561 test_bit(KVM_APIC_INIT, &apic->pending_events)) { 1562 if (block_nested_events) 1563 return -EBUSY; 1564 if (!nested_exit_on_init(svm)) 1565 return 0; 1566 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT); 1567 return 0; 1568 } 1569 1570 if (vcpu->arch.exception_vmexit.pending) { 1571 if (block_nested_exceptions) 1572 return -EBUSY; 1573 nested_svm_inject_exception_vmexit(vcpu); 1574 return 0; 1575 } 1576 1577 if (vcpu->arch.exception.pending) { 1578 if (block_nested_exceptions) 1579 return -EBUSY; 1580 return 0; 1581 } 1582 1583 #ifdef CONFIG_KVM_SMM 1584 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) { 1585 if (block_nested_events) 1586 return -EBUSY; 1587 if (!nested_exit_on_smi(svm)) 1588 return 0; 1589 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI); 1590 return 0; 1591 } 1592 #endif 1593 1594 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) { 1595 if (block_nested_events) 1596 return -EBUSY; 1597 if (!nested_exit_on_nmi(svm)) 1598 return 0; 1599 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI); 1600 return 0; 1601 } 1602 1603 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) { 1604 if (block_nested_events) 1605 return -EBUSY; 1606 if (!nested_exit_on_intr(svm)) 1607 return 0; 1608 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); 1609 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR); 1610 return 0; 1611 } 1612 1613 return 0; 1614 } 1615 1616 int nested_svm_exit_special(struct vcpu_svm *svm) 1617 { 1618 u32 exit_code = svm->vmcb->control.exit_code; 1619 struct kvm_vcpu *vcpu = &svm->vcpu; 1620 1621 switch (exit_code) { 1622 case SVM_EXIT_INTR: 1623 case SVM_EXIT_NMI: 1624 case SVM_EXIT_NPF: 1625 return NESTED_EXIT_HOST; 1626 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { 1627 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); 1628 1629 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] & 1630 excp_bits) 1631 return NESTED_EXIT_HOST; 1632 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR && 1633 svm->vcpu.arch.apf.host_apf_flags) 1634 /* Trap async PF even if not shadowing */ 1635 return NESTED_EXIT_HOST; 1636 break; 1637 } 1638 case SVM_EXIT_VMMCALL: 1639 /* Hyper-V L2 TLB flush hypercall is handled by L0 */ 1640 if (guest_hv_cpuid_has_l2_tlb_flush(vcpu) && 1641 nested_svm_l2_tlb_flush_enabled(vcpu) && 1642 kvm_hv_is_tlb_flush_hcall(vcpu)) 1643 return NESTED_EXIT_HOST; 1644 break; 1645 default: 1646 break; 1647 } 1648 1649 return NESTED_EXIT_CONTINUE; 1650 } 1651 1652 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu) 1653 { 1654 struct vcpu_svm *svm = to_svm(vcpu); 1655 1656 vcpu->arch.tsc_scaling_ratio = 1657 kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio, 1658 svm->tsc_ratio_msr); 1659 svm_write_tsc_multiplier(vcpu); 1660 } 1661 1662 /* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */ 1663 static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst, 1664 struct vmcb_ctrl_area_cached *from) 1665 { 1666 unsigned int i; 1667 1668 memset(dst, 0, sizeof(struct vmcb_control_area)); 1669 1670 for (i = 0; i < MAX_INTERCEPT; i++) 1671 dst->intercepts[i] = from->intercepts[i]; 1672 1673 dst->iopm_base_pa = from->iopm_base_pa; 1674 dst->msrpm_base_pa = from->msrpm_base_pa; 1675 dst->tsc_offset = from->tsc_offset; 1676 dst->asid = from->asid; 1677 dst->tlb_ctl = from->tlb_ctl; 1678 dst->int_ctl = from->int_ctl; 1679 dst->int_vector = from->int_vector; 1680 dst->int_state = from->int_state; 1681 dst->exit_code = from->exit_code; 1682 dst->exit_code_hi = from->exit_code_hi; 1683 dst->exit_info_1 = from->exit_info_1; 1684 dst->exit_info_2 = from->exit_info_2; 1685 dst->exit_int_info = from->exit_int_info; 1686 dst->exit_int_info_err = from->exit_int_info_err; 1687 dst->nested_ctl = from->nested_ctl; 1688 dst->event_inj = from->event_inj; 1689 dst->event_inj_err = from->event_inj_err; 1690 dst->next_rip = from->next_rip; 1691 dst->nested_cr3 = from->nested_cr3; 1692 dst->virt_ext = from->virt_ext; 1693 dst->pause_filter_count = from->pause_filter_count; 1694 dst->pause_filter_thresh = from->pause_filter_thresh; 1695 /* 'clean' and 'hv_enlightenments' are not changed by KVM */ 1696 } 1697 1698 static int svm_get_nested_state(struct kvm_vcpu *vcpu, 1699 struct kvm_nested_state __user *user_kvm_nested_state, 1700 u32 user_data_size) 1701 { 1702 struct vcpu_svm *svm; 1703 struct vmcb_control_area *ctl; 1704 unsigned long r; 1705 struct kvm_nested_state kvm_state = { 1706 .flags = 0, 1707 .format = KVM_STATE_NESTED_FORMAT_SVM, 1708 .size = sizeof(kvm_state), 1709 }; 1710 struct vmcb __user *user_vmcb = (struct vmcb __user *) 1711 &user_kvm_nested_state->data.svm[0]; 1712 1713 if (!vcpu) 1714 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE; 1715 1716 svm = to_svm(vcpu); 1717 1718 if (user_data_size < kvm_state.size) 1719 goto out; 1720 1721 /* First fill in the header and copy it out. */ 1722 if (is_guest_mode(vcpu)) { 1723 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa; 1724 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE; 1725 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; 1726 1727 if (svm->nested.nested_run_pending) 1728 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; 1729 } 1730 1731 if (gif_set(svm)) 1732 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET; 1733 1734 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) 1735 return -EFAULT; 1736 1737 if (!is_guest_mode(vcpu)) 1738 goto out; 1739 1740 /* 1741 * Copy over the full size of the VMCB rather than just the size 1742 * of the structs. 1743 */ 1744 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE)) 1745 return -EFAULT; 1746 1747 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); 1748 if (!ctl) 1749 return -ENOMEM; 1750 1751 nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl); 1752 r = copy_to_user(&user_vmcb->control, ctl, 1753 sizeof(user_vmcb->control)); 1754 kfree(ctl); 1755 if (r) 1756 return -EFAULT; 1757 1758 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save, 1759 sizeof(user_vmcb->save))) 1760 return -EFAULT; 1761 out: 1762 return kvm_state.size; 1763 } 1764 1765 static int svm_set_nested_state(struct kvm_vcpu *vcpu, 1766 struct kvm_nested_state __user *user_kvm_nested_state, 1767 struct kvm_nested_state *kvm_state) 1768 { 1769 struct vcpu_svm *svm = to_svm(vcpu); 1770 struct vmcb __user *user_vmcb = (struct vmcb __user *) 1771 &user_kvm_nested_state->data.svm[0]; 1772 struct vmcb_control_area *ctl; 1773 struct vmcb_save_area *save; 1774 struct vmcb_save_area_cached save_cached; 1775 struct vmcb_ctrl_area_cached ctl_cached; 1776 unsigned long cr0; 1777 int ret; 1778 1779 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) > 1780 KVM_STATE_NESTED_SVM_VMCB_SIZE); 1781 1782 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM) 1783 return -EINVAL; 1784 1785 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE | 1786 KVM_STATE_NESTED_RUN_PENDING | 1787 KVM_STATE_NESTED_GIF_SET)) 1788 return -EINVAL; 1789 1790 /* 1791 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's 1792 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. 1793 */ 1794 if (!(vcpu->arch.efer & EFER_SVME)) { 1795 /* GIF=1 and no guest mode are required if SVME=0. */ 1796 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET) 1797 return -EINVAL; 1798 } 1799 1800 /* SMM temporarily disables SVM, so we cannot be in guest mode. */ 1801 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) 1802 return -EINVAL; 1803 1804 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) { 1805 svm_leave_nested(vcpu); 1806 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET)); 1807 return 0; 1808 } 1809 1810 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa)) 1811 return -EINVAL; 1812 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE) 1813 return -EINVAL; 1814 1815 ctl = memdup_user(&user_vmcb->control, sizeof(*ctl)); 1816 if (IS_ERR(ctl)) 1817 return PTR_ERR(ctl); 1818 1819 save = memdup_user(&user_vmcb->save, sizeof(*save)); 1820 if (IS_ERR(save)) { 1821 kfree(ctl); 1822 return PTR_ERR(save); 1823 } 1824 1825 ret = -EINVAL; 1826 __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl); 1827 if (!__nested_vmcb_check_controls(vcpu, &ctl_cached)) 1828 goto out_free; 1829 1830 /* 1831 * Processor state contains L2 state. Check that it is 1832 * valid for guest mode (see nested_vmcb_check_save). 1833 */ 1834 cr0 = kvm_read_cr0(vcpu); 1835 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW)) 1836 goto out_free; 1837 1838 /* 1839 * Validate host state saved from before VMRUN (see 1840 * nested_svm_check_permissions). 1841 */ 1842 __nested_copy_vmcb_save_to_cache(&save_cached, save); 1843 if (!(save->cr0 & X86_CR0_PG) || 1844 !(save->cr0 & X86_CR0_PE) || 1845 (save->rflags & X86_EFLAGS_VM) || 1846 !__nested_vmcb_check_save(vcpu, &save_cached)) 1847 goto out_free; 1848 1849 1850 /* 1851 * All checks done, we can enter guest mode. Userspace provides 1852 * vmcb12.control, which will be combined with L1 and stored into 1853 * vmcb02, and the L1 save state which we store in vmcb01. 1854 * L2 registers if needed are moved from the current VMCB to VMCB02. 1855 */ 1856 1857 if (is_guest_mode(vcpu)) 1858 svm_leave_nested(vcpu); 1859 else 1860 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save; 1861 1862 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET)); 1863 1864 svm->nested.nested_run_pending = 1865 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); 1866 1867 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa; 1868 1869 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save); 1870 nested_copy_vmcb_control_to_cache(svm, ctl); 1871 1872 svm_switch_vmcb(svm, &svm->nested.vmcb02); 1873 nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.cs.base); 1874 1875 /* 1876 * While the nested guest CR3 is already checked and set by 1877 * KVM_SET_SREGS, it was set when nested state was yet loaded, 1878 * thus MMU might not be initialized correctly. 1879 * Set it again to fix this. 1880 */ 1881 1882 ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3, 1883 nested_npt_enabled(svm), false); 1884 if (WARN_ON_ONCE(ret)) 1885 goto out_free; 1886 1887 svm->nested.force_msr_bitmap_recalc = true; 1888 1889 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); 1890 ret = 0; 1891 out_free: 1892 kfree(save); 1893 kfree(ctl); 1894 1895 return ret; 1896 } 1897 1898 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu) 1899 { 1900 if (WARN_ON(!is_guest_mode(vcpu))) 1901 return true; 1902 1903 if (!vcpu->arch.pdptrs_from_userspace && 1904 !nested_npt_enabled(to_svm(vcpu)) && is_pae_paging(vcpu)) 1905 /* 1906 * Reload the guest's PDPTRs since after a migration 1907 * the guest CR3 might be restored prior to setting the nested 1908 * state which can lead to a load of wrong PDPTRs. 1909 */ 1910 if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3))) 1911 return false; 1912 1913 if (!nested_svm_merge_msrpm(vcpu)) { 1914 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 1915 vcpu->run->internal.suberror = 1916 KVM_INTERNAL_ERROR_EMULATION; 1917 vcpu->run->internal.ndata = 0; 1918 return false; 1919 } 1920 1921 if (kvm_hv_verify_vp_assist(vcpu)) 1922 return false; 1923 1924 return true; 1925 } 1926 1927 struct kvm_x86_nested_ops svm_nested_ops = { 1928 .leave_nested = svm_leave_nested, 1929 .is_exception_vmexit = nested_svm_is_exception_vmexit, 1930 .check_events = svm_check_nested_events, 1931 .triple_fault = nested_svm_triple_fault, 1932 .get_nested_state_pages = svm_get_nested_state_pages, 1933 .get_state = svm_get_nested_state, 1934 .set_state = svm_set_nested_state, 1935 .hv_inject_synthetic_vmexit_post_tlb_flush = svm_hv_inject_synthetic_vmexit_post_tlb_flush, 1936 }; 1937