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) "SVM: " 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 "cpuid.h" 29 #include "lapic.h" 30 #include "svm.h" 31 32 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, 33 struct x86_exception *fault) 34 { 35 struct vcpu_svm *svm = to_svm(vcpu); 36 37 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) { 38 /* 39 * TODO: track the cause of the nested page fault, and 40 * correctly fill in the high bits of exit_info_1. 41 */ 42 svm->vmcb->control.exit_code = SVM_EXIT_NPF; 43 svm->vmcb->control.exit_code_hi = 0; 44 svm->vmcb->control.exit_info_1 = (1ULL << 32); 45 svm->vmcb->control.exit_info_2 = fault->address; 46 } 47 48 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL; 49 svm->vmcb->control.exit_info_1 |= fault->error_code; 50 51 nested_svm_vmexit(svm); 52 } 53 54 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) 55 { 56 struct vcpu_svm *svm = to_svm(vcpu); 57 u64 cr3 = svm->nested.ctl.nested_cr3; 58 u64 pdpte; 59 int ret; 60 61 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte, 62 offset_in_page(cr3) + index * 8, 8); 63 if (ret) 64 return 0; 65 return pdpte; 66 } 67 68 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) 69 { 70 struct vcpu_svm *svm = to_svm(vcpu); 71 72 return svm->nested.ctl.nested_cr3; 73 } 74 75 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) 76 { 77 struct vcpu_svm *svm = to_svm(vcpu); 78 struct vmcb *hsave = svm->nested.hsave; 79 80 WARN_ON(mmu_is_nested(vcpu)); 81 82 vcpu->arch.mmu = &vcpu->arch.guest_mmu; 83 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, hsave->save.cr4, hsave->save.efer, 84 svm->nested.ctl.nested_cr3); 85 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3; 86 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; 87 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; 88 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu); 89 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; 90 } 91 92 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) 93 { 94 vcpu->arch.mmu = &vcpu->arch.root_mmu; 95 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; 96 } 97 98 void recalc_intercepts(struct vcpu_svm *svm) 99 { 100 struct vmcb_control_area *c, *h, *g; 101 unsigned int i; 102 103 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 104 105 if (!is_guest_mode(&svm->vcpu)) 106 return; 107 108 c = &svm->vmcb->control; 109 h = &svm->nested.hsave->control; 110 g = &svm->nested.ctl; 111 112 for (i = 0; i < MAX_INTERCEPT; i++) 113 c->intercepts[i] = h->intercepts[i]; 114 115 if (g->int_ctl & V_INTR_MASKING_MASK) { 116 /* We only want the cr8 intercept bits of L1 */ 117 vmcb_clr_intercept(c, INTERCEPT_CR8_READ); 118 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE); 119 120 /* 121 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not 122 * affect any interrupt we may want to inject; therefore, 123 * interrupt window vmexits are irrelevant to L0. 124 */ 125 vmcb_clr_intercept(c, INTERCEPT_VINTR); 126 } 127 128 /* We don't want to see VMMCALLs from a nested guest */ 129 vmcb_clr_intercept(c, INTERCEPT_VMMCALL); 130 131 for (i = 0; i < MAX_INTERCEPT; i++) 132 c->intercepts[i] |= g->intercepts[i]; 133 } 134 135 static void copy_vmcb_control_area(struct vmcb_control_area *dst, 136 struct vmcb_control_area *from) 137 { 138 unsigned int i; 139 140 for (i = 0; i < MAX_INTERCEPT; i++) 141 dst->intercepts[i] = from->intercepts[i]; 142 143 dst->iopm_base_pa = from->iopm_base_pa; 144 dst->msrpm_base_pa = from->msrpm_base_pa; 145 dst->tsc_offset = from->tsc_offset; 146 /* asid not copied, it is handled manually for svm->vmcb. */ 147 dst->tlb_ctl = from->tlb_ctl; 148 dst->int_ctl = from->int_ctl; 149 dst->int_vector = from->int_vector; 150 dst->int_state = from->int_state; 151 dst->exit_code = from->exit_code; 152 dst->exit_code_hi = from->exit_code_hi; 153 dst->exit_info_1 = from->exit_info_1; 154 dst->exit_info_2 = from->exit_info_2; 155 dst->exit_int_info = from->exit_int_info; 156 dst->exit_int_info_err = from->exit_int_info_err; 157 dst->nested_ctl = from->nested_ctl; 158 dst->event_inj = from->event_inj; 159 dst->event_inj_err = from->event_inj_err; 160 dst->nested_cr3 = from->nested_cr3; 161 dst->virt_ext = from->virt_ext; 162 dst->pause_filter_count = from->pause_filter_count; 163 dst->pause_filter_thresh = from->pause_filter_thresh; 164 } 165 166 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) 167 { 168 /* 169 * This function merges the msr permission bitmaps of kvm and the 170 * nested vmcb. It is optimized in that it only merges the parts where 171 * the kvm msr permission bitmap may contain zero bits 172 */ 173 int i; 174 175 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT))) 176 return true; 177 178 for (i = 0; i < MSRPM_OFFSETS; i++) { 179 u32 value, p; 180 u64 offset; 181 182 if (msrpm_offsets[i] == 0xffffffff) 183 break; 184 185 p = msrpm_offsets[i]; 186 offset = svm->nested.ctl.msrpm_base_pa + (p * 4); 187 188 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) 189 return false; 190 191 svm->nested.msrpm[p] = svm->msrpm[p] | value; 192 } 193 194 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); 195 196 return true; 197 } 198 199 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu) 200 { 201 struct vcpu_svm *svm = to_svm(vcpu); 202 203 if (WARN_ON(!is_guest_mode(vcpu))) 204 return true; 205 206 if (!nested_svm_vmrun_msrpm(svm)) { 207 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 208 vcpu->run->internal.suberror = 209 KVM_INTERNAL_ERROR_EMULATION; 210 vcpu->run->internal.ndata = 0; 211 return false; 212 } 213 214 return true; 215 } 216 217 static bool nested_vmcb_check_controls(struct vmcb_control_area *control) 218 { 219 if ((vmcb_is_intercept(control, INTERCEPT_VMRUN)) == 0) 220 return false; 221 222 if (control->asid == 0) 223 return false; 224 225 if ((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && 226 !npt_enabled) 227 return false; 228 229 return true; 230 } 231 232 static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb12) 233 { 234 bool vmcb12_lma; 235 236 if ((vmcb12->save.efer & EFER_SVME) == 0) 237 return false; 238 239 if (((vmcb12->save.cr0 & X86_CR0_CD) == 0) && (vmcb12->save.cr0 & X86_CR0_NW)) 240 return false; 241 242 if (!kvm_dr6_valid(vmcb12->save.dr6) || !kvm_dr7_valid(vmcb12->save.dr7)) 243 return false; 244 245 vmcb12_lma = (vmcb12->save.efer & EFER_LME) && (vmcb12->save.cr0 & X86_CR0_PG); 246 247 if (!vmcb12_lma) { 248 if (vmcb12->save.cr4 & X86_CR4_PAE) { 249 if (vmcb12->save.cr3 & MSR_CR3_LEGACY_PAE_RESERVED_MASK) 250 return false; 251 } else { 252 if (vmcb12->save.cr3 & MSR_CR3_LEGACY_RESERVED_MASK) 253 return false; 254 } 255 } else { 256 if (!(vmcb12->save.cr4 & X86_CR4_PAE) || 257 !(vmcb12->save.cr0 & X86_CR0_PE) || 258 (vmcb12->save.cr3 & MSR_CR3_LONG_MBZ_MASK)) 259 return false; 260 } 261 if (!kvm_is_valid_cr4(&svm->vcpu, vmcb12->save.cr4)) 262 return false; 263 264 return nested_vmcb_check_controls(&vmcb12->control); 265 } 266 267 static void load_nested_vmcb_control(struct vcpu_svm *svm, 268 struct vmcb_control_area *control) 269 { 270 copy_vmcb_control_area(&svm->nested.ctl, control); 271 272 /* Copy it here because nested_svm_check_controls will check it. */ 273 svm->nested.ctl.asid = control->asid; 274 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL; 275 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL; 276 } 277 278 /* 279 * Synchronize fields that are written by the processor, so that 280 * they can be copied back into the nested_vmcb. 281 */ 282 void sync_nested_vmcb_control(struct vcpu_svm *svm) 283 { 284 u32 mask; 285 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj; 286 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err; 287 288 /* Only a few fields of int_ctl are written by the processor. */ 289 mask = V_IRQ_MASK | V_TPR_MASK; 290 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) && 291 svm_is_intercept(svm, INTERCEPT_VINTR)) { 292 /* 293 * In order to request an interrupt window, L0 is usurping 294 * svm->vmcb->control.int_ctl and possibly setting V_IRQ 295 * even if it was clear in L1's VMCB. Restoring it would be 296 * wrong. However, in this case V_IRQ will remain true until 297 * interrupt_window_interception calls svm_clear_vintr and 298 * restores int_ctl. We can just leave it aside. 299 */ 300 mask &= ~V_IRQ_MASK; 301 } 302 svm->nested.ctl.int_ctl &= ~mask; 303 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask; 304 } 305 306 /* 307 * Transfer any event that L0 or L1 wanted to inject into L2 to 308 * EXIT_INT_INFO. 309 */ 310 static void nested_vmcb_save_pending_event(struct vcpu_svm *svm, 311 struct vmcb *vmcb12) 312 { 313 struct kvm_vcpu *vcpu = &svm->vcpu; 314 u32 exit_int_info = 0; 315 unsigned int nr; 316 317 if (vcpu->arch.exception.injected) { 318 nr = vcpu->arch.exception.nr; 319 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT; 320 321 if (vcpu->arch.exception.has_error_code) { 322 exit_int_info |= SVM_EVTINJ_VALID_ERR; 323 vmcb12->control.exit_int_info_err = 324 vcpu->arch.exception.error_code; 325 } 326 327 } else if (vcpu->arch.nmi_injected) { 328 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; 329 330 } else if (vcpu->arch.interrupt.injected) { 331 nr = vcpu->arch.interrupt.nr; 332 exit_int_info = nr | SVM_EVTINJ_VALID; 333 334 if (vcpu->arch.interrupt.soft) 335 exit_int_info |= SVM_EVTINJ_TYPE_SOFT; 336 else 337 exit_int_info |= SVM_EVTINJ_TYPE_INTR; 338 } 339 340 vmcb12->control.exit_int_info = exit_int_info; 341 } 342 343 static inline bool nested_npt_enabled(struct vcpu_svm *svm) 344 { 345 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE; 346 } 347 348 /* 349 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true 350 * if we are emulating VM-Entry into a guest with NPT enabled. 351 */ 352 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, 353 bool nested_npt) 354 { 355 if (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)) 356 return -EINVAL; 357 358 if (!nested_npt && is_pae_paging(vcpu) && 359 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) { 360 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) 361 return -EINVAL; 362 } 363 364 /* 365 * TODO: optimize unconditional TLB flush/MMU sync here and in 366 * kvm_init_shadow_npt_mmu(). 367 */ 368 if (!nested_npt) 369 kvm_mmu_new_pgd(vcpu, cr3, false, false); 370 371 vcpu->arch.cr3 = cr3; 372 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); 373 374 kvm_init_mmu(vcpu, false); 375 376 return 0; 377 } 378 379 static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *vmcb12) 380 { 381 /* Load the nested guest state */ 382 svm->vmcb->save.es = vmcb12->save.es; 383 svm->vmcb->save.cs = vmcb12->save.cs; 384 svm->vmcb->save.ss = vmcb12->save.ss; 385 svm->vmcb->save.ds = vmcb12->save.ds; 386 svm->vmcb->save.gdtr = vmcb12->save.gdtr; 387 svm->vmcb->save.idtr = vmcb12->save.idtr; 388 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED); 389 svm_set_efer(&svm->vcpu, vmcb12->save.efer); 390 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0); 391 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4); 392 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = vmcb12->save.cr2; 393 kvm_rax_write(&svm->vcpu, vmcb12->save.rax); 394 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp); 395 kvm_rip_write(&svm->vcpu, vmcb12->save.rip); 396 397 /* In case we don't even reach vcpu_run, the fields are not updated */ 398 svm->vmcb->save.rax = vmcb12->save.rax; 399 svm->vmcb->save.rsp = vmcb12->save.rsp; 400 svm->vmcb->save.rip = vmcb12->save.rip; 401 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1; 402 svm->vcpu.arch.dr6 = vmcb12->save.dr6 | DR6_FIXED_1 | DR6_RTM; 403 svm->vmcb->save.cpl = vmcb12->save.cpl; 404 } 405 406 static void nested_prepare_vmcb_control(struct vcpu_svm *svm) 407 { 408 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK; 409 410 if (nested_npt_enabled(svm)) 411 nested_svm_init_mmu_context(&svm->vcpu); 412 413 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset = 414 svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset; 415 416 svm->vmcb->control.int_ctl = 417 (svm->nested.ctl.int_ctl & ~mask) | 418 (svm->nested.hsave->control.int_ctl & mask); 419 420 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext; 421 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector; 422 svm->vmcb->control.int_state = svm->nested.ctl.int_state; 423 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj; 424 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err; 425 426 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count; 427 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh; 428 429 /* Enter Guest-Mode */ 430 enter_guest_mode(&svm->vcpu); 431 432 /* 433 * Merge guest and host intercepts - must be called with vcpu in 434 * guest-mode to take affect here 435 */ 436 recalc_intercepts(svm); 437 438 vmcb_mark_all_dirty(svm->vmcb); 439 } 440 441 int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb12_gpa, 442 struct vmcb *vmcb12) 443 { 444 int ret; 445 446 svm->nested.vmcb12_gpa = vmcb12_gpa; 447 load_nested_vmcb_control(svm, &vmcb12->control); 448 nested_prepare_vmcb_save(svm, vmcb12); 449 nested_prepare_vmcb_control(svm); 450 451 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3, 452 nested_npt_enabled(svm)); 453 if (ret) 454 return ret; 455 456 svm_set_gif(svm, true); 457 458 return 0; 459 } 460 461 int nested_svm_vmrun(struct vcpu_svm *svm) 462 { 463 int ret; 464 struct vmcb *vmcb12; 465 struct vmcb *hsave = svm->nested.hsave; 466 struct vmcb *vmcb = svm->vmcb; 467 struct kvm_host_map map; 468 u64 vmcb12_gpa; 469 470 if (is_smm(&svm->vcpu)) { 471 kvm_queue_exception(&svm->vcpu, UD_VECTOR); 472 return 1; 473 } 474 475 vmcb12_gpa = svm->vmcb->save.rax; 476 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb12_gpa), &map); 477 if (ret == -EINVAL) { 478 kvm_inject_gp(&svm->vcpu, 0); 479 return 1; 480 } else if (ret) { 481 return kvm_skip_emulated_instruction(&svm->vcpu); 482 } 483 484 ret = kvm_skip_emulated_instruction(&svm->vcpu); 485 486 vmcb12 = map.hva; 487 488 if (WARN_ON_ONCE(!svm->nested.initialized)) 489 return -EINVAL; 490 491 if (!nested_vmcb_checks(svm, vmcb12)) { 492 vmcb12->control.exit_code = SVM_EXIT_ERR; 493 vmcb12->control.exit_code_hi = 0; 494 vmcb12->control.exit_info_1 = 0; 495 vmcb12->control.exit_info_2 = 0; 496 goto out; 497 } 498 499 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa, 500 vmcb12->save.rip, 501 vmcb12->control.int_ctl, 502 vmcb12->control.event_inj, 503 vmcb12->control.nested_ctl); 504 505 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff, 506 vmcb12->control.intercepts[INTERCEPT_CR] >> 16, 507 vmcb12->control.intercepts[INTERCEPT_EXCEPTION], 508 vmcb12->control.intercepts[INTERCEPT_WORD3], 509 vmcb12->control.intercepts[INTERCEPT_WORD4], 510 vmcb12->control.intercepts[INTERCEPT_WORD5]); 511 512 /* Clear internal status */ 513 kvm_clear_exception_queue(&svm->vcpu); 514 kvm_clear_interrupt_queue(&svm->vcpu); 515 516 /* 517 * Save the old vmcb, so we don't need to pick what we save, but can 518 * restore everything when a VMEXIT occurs 519 */ 520 hsave->save.es = vmcb->save.es; 521 hsave->save.cs = vmcb->save.cs; 522 hsave->save.ss = vmcb->save.ss; 523 hsave->save.ds = vmcb->save.ds; 524 hsave->save.gdtr = vmcb->save.gdtr; 525 hsave->save.idtr = vmcb->save.idtr; 526 hsave->save.efer = svm->vcpu.arch.efer; 527 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); 528 hsave->save.cr4 = svm->vcpu.arch.cr4; 529 hsave->save.rflags = kvm_get_rflags(&svm->vcpu); 530 hsave->save.rip = kvm_rip_read(&svm->vcpu); 531 hsave->save.rsp = vmcb->save.rsp; 532 hsave->save.rax = vmcb->save.rax; 533 if (npt_enabled) 534 hsave->save.cr3 = vmcb->save.cr3; 535 else 536 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); 537 538 copy_vmcb_control_area(&hsave->control, &vmcb->control); 539 540 svm->nested.nested_run_pending = 1; 541 542 if (enter_svm_guest_mode(svm, vmcb12_gpa, vmcb12)) 543 goto out_exit_err; 544 545 if (nested_svm_vmrun_msrpm(svm)) 546 goto out; 547 548 out_exit_err: 549 svm->nested.nested_run_pending = 0; 550 551 svm->vmcb->control.exit_code = SVM_EXIT_ERR; 552 svm->vmcb->control.exit_code_hi = 0; 553 svm->vmcb->control.exit_info_1 = 0; 554 svm->vmcb->control.exit_info_2 = 0; 555 556 nested_svm_vmexit(svm); 557 558 out: 559 kvm_vcpu_unmap(&svm->vcpu, &map, true); 560 561 return ret; 562 } 563 564 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) 565 { 566 to_vmcb->save.fs = from_vmcb->save.fs; 567 to_vmcb->save.gs = from_vmcb->save.gs; 568 to_vmcb->save.tr = from_vmcb->save.tr; 569 to_vmcb->save.ldtr = from_vmcb->save.ldtr; 570 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; 571 to_vmcb->save.star = from_vmcb->save.star; 572 to_vmcb->save.lstar = from_vmcb->save.lstar; 573 to_vmcb->save.cstar = from_vmcb->save.cstar; 574 to_vmcb->save.sfmask = from_vmcb->save.sfmask; 575 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; 576 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; 577 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; 578 } 579 580 int nested_svm_vmexit(struct vcpu_svm *svm) 581 { 582 int rc; 583 struct vmcb *vmcb12; 584 struct vmcb *hsave = svm->nested.hsave; 585 struct vmcb *vmcb = svm->vmcb; 586 struct kvm_host_map map; 587 588 rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map); 589 if (rc) { 590 if (rc == -EINVAL) 591 kvm_inject_gp(&svm->vcpu, 0); 592 return 1; 593 } 594 595 vmcb12 = map.hva; 596 597 /* Exit Guest-Mode */ 598 leave_guest_mode(&svm->vcpu); 599 svm->nested.vmcb12_gpa = 0; 600 WARN_ON_ONCE(svm->nested.nested_run_pending); 601 602 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, &svm->vcpu); 603 604 /* in case we halted in L2 */ 605 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE; 606 607 /* Give the current vmcb to the guest */ 608 609 vmcb12->save.es = vmcb->save.es; 610 vmcb12->save.cs = vmcb->save.cs; 611 vmcb12->save.ss = vmcb->save.ss; 612 vmcb12->save.ds = vmcb->save.ds; 613 vmcb12->save.gdtr = vmcb->save.gdtr; 614 vmcb12->save.idtr = vmcb->save.idtr; 615 vmcb12->save.efer = svm->vcpu.arch.efer; 616 vmcb12->save.cr0 = kvm_read_cr0(&svm->vcpu); 617 vmcb12->save.cr3 = kvm_read_cr3(&svm->vcpu); 618 vmcb12->save.cr2 = vmcb->save.cr2; 619 vmcb12->save.cr4 = svm->vcpu.arch.cr4; 620 vmcb12->save.rflags = kvm_get_rflags(&svm->vcpu); 621 vmcb12->save.rip = kvm_rip_read(&svm->vcpu); 622 vmcb12->save.rsp = kvm_rsp_read(&svm->vcpu); 623 vmcb12->save.rax = kvm_rax_read(&svm->vcpu); 624 vmcb12->save.dr7 = vmcb->save.dr7; 625 vmcb12->save.dr6 = svm->vcpu.arch.dr6; 626 vmcb12->save.cpl = vmcb->save.cpl; 627 628 vmcb12->control.int_state = vmcb->control.int_state; 629 vmcb12->control.exit_code = vmcb->control.exit_code; 630 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi; 631 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1; 632 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2; 633 634 if (vmcb12->control.exit_code != SVM_EXIT_ERR) 635 nested_vmcb_save_pending_event(svm, vmcb12); 636 637 if (svm->nrips_enabled) 638 vmcb12->control.next_rip = vmcb->control.next_rip; 639 640 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl; 641 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl; 642 vmcb12->control.event_inj = svm->nested.ctl.event_inj; 643 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err; 644 645 vmcb12->control.pause_filter_count = 646 svm->vmcb->control.pause_filter_count; 647 vmcb12->control.pause_filter_thresh = 648 svm->vmcb->control.pause_filter_thresh; 649 650 /* Restore the original control entries */ 651 copy_vmcb_control_area(&vmcb->control, &hsave->control); 652 653 /* On vmexit the GIF is set to false */ 654 svm_set_gif(svm, false); 655 656 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset = 657 svm->vcpu.arch.l1_tsc_offset; 658 659 svm->nested.ctl.nested_cr3 = 0; 660 661 /* Restore selected save entries */ 662 svm->vmcb->save.es = hsave->save.es; 663 svm->vmcb->save.cs = hsave->save.cs; 664 svm->vmcb->save.ss = hsave->save.ss; 665 svm->vmcb->save.ds = hsave->save.ds; 666 svm->vmcb->save.gdtr = hsave->save.gdtr; 667 svm->vmcb->save.idtr = hsave->save.idtr; 668 kvm_set_rflags(&svm->vcpu, hsave->save.rflags); 669 kvm_set_rflags(&svm->vcpu, hsave->save.rflags | X86_EFLAGS_FIXED); 670 svm_set_efer(&svm->vcpu, hsave->save.efer); 671 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); 672 svm_set_cr4(&svm->vcpu, hsave->save.cr4); 673 kvm_rax_write(&svm->vcpu, hsave->save.rax); 674 kvm_rsp_write(&svm->vcpu, hsave->save.rsp); 675 kvm_rip_write(&svm->vcpu, hsave->save.rip); 676 svm->vmcb->save.dr7 = DR7_FIXED_1; 677 svm->vmcb->save.cpl = 0; 678 svm->vmcb->control.exit_int_info = 0; 679 680 vmcb_mark_all_dirty(svm->vmcb); 681 682 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code, 683 vmcb12->control.exit_info_1, 684 vmcb12->control.exit_info_2, 685 vmcb12->control.exit_int_info, 686 vmcb12->control.exit_int_info_err, 687 KVM_ISA_SVM); 688 689 kvm_vcpu_unmap(&svm->vcpu, &map, true); 690 691 nested_svm_uninit_mmu_context(&svm->vcpu); 692 693 rc = nested_svm_load_cr3(&svm->vcpu, hsave->save.cr3, false); 694 if (rc) 695 return 1; 696 697 if (npt_enabled) 698 svm->vmcb->save.cr3 = hsave->save.cr3; 699 700 /* 701 * Drop what we picked up for L2 via svm_complete_interrupts() so it 702 * doesn't end up in L1. 703 */ 704 svm->vcpu.arch.nmi_injected = false; 705 kvm_clear_exception_queue(&svm->vcpu); 706 kvm_clear_interrupt_queue(&svm->vcpu); 707 708 return 0; 709 } 710 711 int svm_allocate_nested(struct vcpu_svm *svm) 712 { 713 struct page *hsave_page; 714 715 if (svm->nested.initialized) 716 return 0; 717 718 hsave_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); 719 if (!hsave_page) 720 return -ENOMEM; 721 svm->nested.hsave = page_address(hsave_page); 722 723 svm->nested.msrpm = svm_vcpu_alloc_msrpm(); 724 if (!svm->nested.msrpm) 725 goto err_free_hsave; 726 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm); 727 728 svm->nested.initialized = true; 729 return 0; 730 731 err_free_hsave: 732 __free_page(hsave_page); 733 return -ENOMEM; 734 } 735 736 void svm_free_nested(struct vcpu_svm *svm) 737 { 738 if (!svm->nested.initialized) 739 return; 740 741 svm_vcpu_free_msrpm(svm->nested.msrpm); 742 svm->nested.msrpm = NULL; 743 744 __free_page(virt_to_page(svm->nested.hsave)); 745 svm->nested.hsave = NULL; 746 747 svm->nested.initialized = false; 748 } 749 750 /* 751 * Forcibly leave nested mode in order to be able to reset the VCPU later on. 752 */ 753 void svm_leave_nested(struct vcpu_svm *svm) 754 { 755 if (is_guest_mode(&svm->vcpu)) { 756 struct vmcb *hsave = svm->nested.hsave; 757 struct vmcb *vmcb = svm->vmcb; 758 759 svm->nested.nested_run_pending = 0; 760 leave_guest_mode(&svm->vcpu); 761 copy_vmcb_control_area(&vmcb->control, &hsave->control); 762 nested_svm_uninit_mmu_context(&svm->vcpu); 763 vmcb_mark_all_dirty(svm->vmcb); 764 } 765 766 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, &svm->vcpu); 767 } 768 769 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) 770 { 771 u32 offset, msr, value; 772 int write, mask; 773 774 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT))) 775 return NESTED_EXIT_HOST; 776 777 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; 778 offset = svm_msrpm_offset(msr); 779 write = svm->vmcb->control.exit_info_1 & 1; 780 mask = 1 << ((2 * (msr & 0xf)) + write); 781 782 if (offset == MSR_INVALID) 783 return NESTED_EXIT_DONE; 784 785 /* Offset is in 32 bit units but need in 8 bit units */ 786 offset *= 4; 787 788 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4)) 789 return NESTED_EXIT_DONE; 790 791 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 792 } 793 794 static int nested_svm_intercept_ioio(struct vcpu_svm *svm) 795 { 796 unsigned port, size, iopm_len; 797 u16 val, mask; 798 u8 start_bit; 799 u64 gpa; 800 801 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT))) 802 return NESTED_EXIT_HOST; 803 804 port = svm->vmcb->control.exit_info_1 >> 16; 805 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> 806 SVM_IOIO_SIZE_SHIFT; 807 gpa = svm->nested.ctl.iopm_base_pa + (port / 8); 808 start_bit = port % 8; 809 iopm_len = (start_bit + size > 8) ? 2 : 1; 810 mask = (0xf >> (4 - size)) << start_bit; 811 val = 0; 812 813 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) 814 return NESTED_EXIT_DONE; 815 816 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; 817 } 818 819 static int nested_svm_intercept(struct vcpu_svm *svm) 820 { 821 u32 exit_code = svm->vmcb->control.exit_code; 822 int vmexit = NESTED_EXIT_HOST; 823 824 switch (exit_code) { 825 case SVM_EXIT_MSR: 826 vmexit = nested_svm_exit_handled_msr(svm); 827 break; 828 case SVM_EXIT_IOIO: 829 vmexit = nested_svm_intercept_ioio(svm); 830 break; 831 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { 832 if (vmcb_is_intercept(&svm->nested.ctl, exit_code)) 833 vmexit = NESTED_EXIT_DONE; 834 break; 835 } 836 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { 837 if (vmcb_is_intercept(&svm->nested.ctl, exit_code)) 838 vmexit = NESTED_EXIT_DONE; 839 break; 840 } 841 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { 842 /* 843 * Host-intercepted exceptions have been checked already in 844 * nested_svm_exit_special. There is nothing to do here, 845 * the vmexit is injected by svm_check_nested_events. 846 */ 847 vmexit = NESTED_EXIT_DONE; 848 break; 849 } 850 case SVM_EXIT_ERR: { 851 vmexit = NESTED_EXIT_DONE; 852 break; 853 } 854 default: { 855 if (vmcb_is_intercept(&svm->nested.ctl, exit_code)) 856 vmexit = NESTED_EXIT_DONE; 857 } 858 } 859 860 return vmexit; 861 } 862 863 int nested_svm_exit_handled(struct vcpu_svm *svm) 864 { 865 int vmexit; 866 867 vmexit = nested_svm_intercept(svm); 868 869 if (vmexit == NESTED_EXIT_DONE) 870 nested_svm_vmexit(svm); 871 872 return vmexit; 873 } 874 875 int nested_svm_check_permissions(struct vcpu_svm *svm) 876 { 877 if (!(svm->vcpu.arch.efer & EFER_SVME) || 878 !is_paging(&svm->vcpu)) { 879 kvm_queue_exception(&svm->vcpu, UD_VECTOR); 880 return 1; 881 } 882 883 if (svm->vmcb->save.cpl) { 884 kvm_inject_gp(&svm->vcpu, 0); 885 return 1; 886 } 887 888 return 0; 889 } 890 891 static bool nested_exit_on_exception(struct vcpu_svm *svm) 892 { 893 unsigned int nr = svm->vcpu.arch.exception.nr; 894 895 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr)); 896 } 897 898 static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm) 899 { 900 unsigned int nr = svm->vcpu.arch.exception.nr; 901 902 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; 903 svm->vmcb->control.exit_code_hi = 0; 904 905 if (svm->vcpu.arch.exception.has_error_code) 906 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code; 907 908 /* 909 * EXITINFO2 is undefined for all exception intercepts other 910 * than #PF. 911 */ 912 if (nr == PF_VECTOR) { 913 if (svm->vcpu.arch.exception.nested_apf) 914 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; 915 else if (svm->vcpu.arch.exception.has_payload) 916 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload; 917 else 918 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; 919 } else if (nr == DB_VECTOR) { 920 /* See inject_pending_event. */ 921 kvm_deliver_exception_payload(&svm->vcpu); 922 if (svm->vcpu.arch.dr7 & DR7_GD) { 923 svm->vcpu.arch.dr7 &= ~DR7_GD; 924 kvm_update_dr7(&svm->vcpu); 925 } 926 } else 927 WARN_ON(svm->vcpu.arch.exception.has_payload); 928 929 nested_svm_vmexit(svm); 930 } 931 932 static void nested_svm_smi(struct vcpu_svm *svm) 933 { 934 svm->vmcb->control.exit_code = SVM_EXIT_SMI; 935 svm->vmcb->control.exit_info_1 = 0; 936 svm->vmcb->control.exit_info_2 = 0; 937 938 nested_svm_vmexit(svm); 939 } 940 941 static void nested_svm_nmi(struct vcpu_svm *svm) 942 { 943 svm->vmcb->control.exit_code = SVM_EXIT_NMI; 944 svm->vmcb->control.exit_info_1 = 0; 945 svm->vmcb->control.exit_info_2 = 0; 946 947 nested_svm_vmexit(svm); 948 } 949 950 static void nested_svm_intr(struct vcpu_svm *svm) 951 { 952 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); 953 954 svm->vmcb->control.exit_code = SVM_EXIT_INTR; 955 svm->vmcb->control.exit_info_1 = 0; 956 svm->vmcb->control.exit_info_2 = 0; 957 958 nested_svm_vmexit(svm); 959 } 960 961 static inline bool nested_exit_on_init(struct vcpu_svm *svm) 962 { 963 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT); 964 } 965 966 static void nested_svm_init(struct vcpu_svm *svm) 967 { 968 svm->vmcb->control.exit_code = SVM_EXIT_INIT; 969 svm->vmcb->control.exit_info_1 = 0; 970 svm->vmcb->control.exit_info_2 = 0; 971 972 nested_svm_vmexit(svm); 973 } 974 975 976 static int svm_check_nested_events(struct kvm_vcpu *vcpu) 977 { 978 struct vcpu_svm *svm = to_svm(vcpu); 979 bool block_nested_events = 980 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending; 981 struct kvm_lapic *apic = vcpu->arch.apic; 982 983 if (lapic_in_kernel(vcpu) && 984 test_bit(KVM_APIC_INIT, &apic->pending_events)) { 985 if (block_nested_events) 986 return -EBUSY; 987 if (!nested_exit_on_init(svm)) 988 return 0; 989 nested_svm_init(svm); 990 return 0; 991 } 992 993 if (vcpu->arch.exception.pending) { 994 if (block_nested_events) 995 return -EBUSY; 996 if (!nested_exit_on_exception(svm)) 997 return 0; 998 nested_svm_inject_exception_vmexit(svm); 999 return 0; 1000 } 1001 1002 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) { 1003 if (block_nested_events) 1004 return -EBUSY; 1005 if (!nested_exit_on_smi(svm)) 1006 return 0; 1007 nested_svm_smi(svm); 1008 return 0; 1009 } 1010 1011 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) { 1012 if (block_nested_events) 1013 return -EBUSY; 1014 if (!nested_exit_on_nmi(svm)) 1015 return 0; 1016 nested_svm_nmi(svm); 1017 return 0; 1018 } 1019 1020 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) { 1021 if (block_nested_events) 1022 return -EBUSY; 1023 if (!nested_exit_on_intr(svm)) 1024 return 0; 1025 nested_svm_intr(svm); 1026 return 0; 1027 } 1028 1029 return 0; 1030 } 1031 1032 int nested_svm_exit_special(struct vcpu_svm *svm) 1033 { 1034 u32 exit_code = svm->vmcb->control.exit_code; 1035 1036 switch (exit_code) { 1037 case SVM_EXIT_INTR: 1038 case SVM_EXIT_NMI: 1039 case SVM_EXIT_NPF: 1040 return NESTED_EXIT_HOST; 1041 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { 1042 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); 1043 1044 if (get_host_vmcb(svm)->control.intercepts[INTERCEPT_EXCEPTION] & 1045 excp_bits) 1046 return NESTED_EXIT_HOST; 1047 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR && 1048 svm->vcpu.arch.apf.host_apf_flags) 1049 /* Trap async PF even if not shadowing */ 1050 return NESTED_EXIT_HOST; 1051 break; 1052 } 1053 default: 1054 break; 1055 } 1056 1057 return NESTED_EXIT_CONTINUE; 1058 } 1059 1060 static int svm_get_nested_state(struct kvm_vcpu *vcpu, 1061 struct kvm_nested_state __user *user_kvm_nested_state, 1062 u32 user_data_size) 1063 { 1064 struct vcpu_svm *svm; 1065 struct kvm_nested_state kvm_state = { 1066 .flags = 0, 1067 .format = KVM_STATE_NESTED_FORMAT_SVM, 1068 .size = sizeof(kvm_state), 1069 }; 1070 struct vmcb __user *user_vmcb = (struct vmcb __user *) 1071 &user_kvm_nested_state->data.svm[0]; 1072 1073 if (!vcpu) 1074 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE; 1075 1076 svm = to_svm(vcpu); 1077 1078 if (user_data_size < kvm_state.size) 1079 goto out; 1080 1081 /* First fill in the header and copy it out. */ 1082 if (is_guest_mode(vcpu)) { 1083 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa; 1084 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE; 1085 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; 1086 1087 if (svm->nested.nested_run_pending) 1088 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; 1089 } 1090 1091 if (gif_set(svm)) 1092 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET; 1093 1094 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) 1095 return -EFAULT; 1096 1097 if (!is_guest_mode(vcpu)) 1098 goto out; 1099 1100 /* 1101 * Copy over the full size of the VMCB rather than just the size 1102 * of the structs. 1103 */ 1104 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE)) 1105 return -EFAULT; 1106 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl, 1107 sizeof(user_vmcb->control))) 1108 return -EFAULT; 1109 if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save, 1110 sizeof(user_vmcb->save))) 1111 return -EFAULT; 1112 1113 out: 1114 return kvm_state.size; 1115 } 1116 1117 static int svm_set_nested_state(struct kvm_vcpu *vcpu, 1118 struct kvm_nested_state __user *user_kvm_nested_state, 1119 struct kvm_nested_state *kvm_state) 1120 { 1121 struct vcpu_svm *svm = to_svm(vcpu); 1122 struct vmcb *hsave = svm->nested.hsave; 1123 struct vmcb __user *user_vmcb = (struct vmcb __user *) 1124 &user_kvm_nested_state->data.svm[0]; 1125 struct vmcb_control_area *ctl; 1126 struct vmcb_save_area *save; 1127 int ret; 1128 u32 cr0; 1129 1130 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) > 1131 KVM_STATE_NESTED_SVM_VMCB_SIZE); 1132 1133 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM) 1134 return -EINVAL; 1135 1136 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE | 1137 KVM_STATE_NESTED_RUN_PENDING | 1138 KVM_STATE_NESTED_GIF_SET)) 1139 return -EINVAL; 1140 1141 /* 1142 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's 1143 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. 1144 */ 1145 if (!(vcpu->arch.efer & EFER_SVME)) { 1146 /* GIF=1 and no guest mode are required if SVME=0. */ 1147 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET) 1148 return -EINVAL; 1149 } 1150 1151 /* SMM temporarily disables SVM, so we cannot be in guest mode. */ 1152 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) 1153 return -EINVAL; 1154 1155 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) { 1156 svm_leave_nested(svm); 1157 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET)); 1158 return 0; 1159 } 1160 1161 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa)) 1162 return -EINVAL; 1163 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE) 1164 return -EINVAL; 1165 1166 ret = -ENOMEM; 1167 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); 1168 save = kzalloc(sizeof(*save), GFP_KERNEL); 1169 if (!ctl || !save) 1170 goto out_free; 1171 1172 ret = -EFAULT; 1173 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl))) 1174 goto out_free; 1175 if (copy_from_user(save, &user_vmcb->save, sizeof(*save))) 1176 goto out_free; 1177 1178 ret = -EINVAL; 1179 if (!nested_vmcb_check_controls(ctl)) 1180 goto out_free; 1181 1182 /* 1183 * Processor state contains L2 state. Check that it is 1184 * valid for guest mode (see nested_vmcb_checks). 1185 */ 1186 cr0 = kvm_read_cr0(vcpu); 1187 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW)) 1188 goto out_free; 1189 1190 /* 1191 * Validate host state saved from before VMRUN (see 1192 * nested_svm_check_permissions). 1193 * TODO: validate reserved bits for all saved state. 1194 */ 1195 if (!(save->cr0 & X86_CR0_PG)) 1196 goto out_free; 1197 1198 /* 1199 * All checks done, we can enter guest mode. L1 control fields 1200 * come from the nested save state. Guest state is already 1201 * in the registers, the save area of the nested state instead 1202 * contains saved L1 state. 1203 */ 1204 1205 svm->nested.nested_run_pending = 1206 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); 1207 1208 copy_vmcb_control_area(&hsave->control, &svm->vmcb->control); 1209 hsave->save = *save; 1210 1211 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa; 1212 load_nested_vmcb_control(svm, ctl); 1213 nested_prepare_vmcb_control(svm); 1214 1215 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); 1216 ret = 0; 1217 out_free: 1218 kfree(save); 1219 kfree(ctl); 1220 1221 return ret; 1222 } 1223 1224 struct kvm_x86_nested_ops svm_nested_ops = { 1225 .check_events = svm_check_nested_events, 1226 .get_nested_state_pages = svm_get_nested_state_pages, 1227 .get_state = svm_get_nested_state, 1228 .set_state = svm_set_nested_state, 1229 }; 1230