1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Yaniv Kamay <yaniv@qumranet.com>
12 * Avi Kivity <avi@qumranet.com>
15 #define pr_fmt(fmt) "SVM: " fmt
17 #include <linux/kvm_types.h>
18 #include <linux/kvm_host.h>
19 #include <linux/kernel.h>
21 #include <asm/msr-index.h>
22 #include <asm/debugreg.h>
24 #include "kvm_emulate.h"
32 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
33 struct x86_exception *fault)
35 struct vcpu_svm *svm = to_svm(vcpu);
37 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
39 * TODO: track the cause of the nested page fault, and
40 * correctly fill in the high bits of exit_info_1.
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;
48 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
49 svm->vmcb->control.exit_info_1 |= fault->error_code;
51 nested_svm_vmexit(svm);
54 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
56 struct vcpu_svm *svm = to_svm(vcpu);
57 u64 cr3 = svm->nested.ctl.nested_cr3;
61 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
62 offset_in_page(cr3) + index * 8, 8);
68 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
70 struct vcpu_svm *svm = to_svm(vcpu);
72 return svm->nested.ctl.nested_cr3;
75 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
77 struct vcpu_svm *svm = to_svm(vcpu);
78 struct vmcb *hsave = svm->nested.hsave;
80 WARN_ON(mmu_is_nested(vcpu));
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;
92 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
94 vcpu->arch.mmu = &vcpu->arch.root_mmu;
95 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
98 void recalc_intercepts(struct vcpu_svm *svm)
100 struct vmcb_control_area *c, *h, *g;
103 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
105 if (!is_guest_mode(&svm->vcpu))
108 c = &svm->vmcb->control;
109 h = &svm->nested.hsave->control;
110 g = &svm->nested.ctl;
112 for (i = 0; i < MAX_INTERCEPT; i++)
113 c->intercepts[i] = h->intercepts[i];
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);
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.
125 vmcb_clr_intercept(c, INTERCEPT_VINTR);
128 /* We don't want to see VMMCALLs from a nested guest */
129 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
131 for (i = 0; i < MAX_INTERCEPT; i++)
132 c->intercepts[i] |= g->intercepts[i];
135 static void copy_vmcb_control_area(struct vmcb_control_area *dst,
136 struct vmcb_control_area *from)
140 for (i = 0; i < MAX_INTERCEPT; i++)
141 dst->intercepts[i] = from->intercepts[i];
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;
166 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
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
175 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
178 for (i = 0; i < MSRPM_OFFSETS; i++) {
182 if (msrpm_offsets[i] == 0xffffffff)
185 p = msrpm_offsets[i];
186 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
188 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
191 svm->nested.msrpm[p] = svm->msrpm[p] | value;
194 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
199 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
201 struct vcpu_svm *svm = to_svm(vcpu);
202 if (!nested_svm_vmrun_msrpm(svm)) {
203 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
204 vcpu->run->internal.suberror =
205 KVM_INTERNAL_ERROR_EMULATION;
206 vcpu->run->internal.ndata = 0;
213 static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
215 if ((vmcb_is_intercept(control, INTERCEPT_VMRUN)) == 0)
218 if (control->asid == 0)
221 if ((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
228 static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb12)
232 if ((vmcb12->save.efer & EFER_SVME) == 0)
235 if (((vmcb12->save.cr0 & X86_CR0_CD) == 0) && (vmcb12->save.cr0 & X86_CR0_NW))
238 if (!kvm_dr6_valid(vmcb12->save.dr6) || !kvm_dr7_valid(vmcb12->save.dr7))
241 vmcb12_lma = (vmcb12->save.efer & EFER_LME) && (vmcb12->save.cr0 & X86_CR0_PG);
244 if (vmcb12->save.cr4 & X86_CR4_PAE) {
245 if (vmcb12->save.cr3 & MSR_CR3_LEGACY_PAE_RESERVED_MASK)
248 if (vmcb12->save.cr3 & MSR_CR3_LEGACY_RESERVED_MASK)
252 if (!(vmcb12->save.cr4 & X86_CR4_PAE) ||
253 !(vmcb12->save.cr0 & X86_CR0_PE) ||
254 (vmcb12->save.cr3 & MSR_CR3_LONG_MBZ_MASK))
257 if (kvm_valid_cr4(&svm->vcpu, vmcb12->save.cr4))
260 return nested_vmcb_check_controls(&vmcb12->control);
263 static void load_nested_vmcb_control(struct vcpu_svm *svm,
264 struct vmcb_control_area *control)
266 copy_vmcb_control_area(&svm->nested.ctl, control);
268 /* Copy it here because nested_svm_check_controls will check it. */
269 svm->nested.ctl.asid = control->asid;
270 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
271 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
275 * Synchronize fields that are written by the processor, so that
276 * they can be copied back into the nested_vmcb.
278 void sync_nested_vmcb_control(struct vcpu_svm *svm)
281 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
282 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
284 /* Only a few fields of int_ctl are written by the processor. */
285 mask = V_IRQ_MASK | V_TPR_MASK;
286 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
287 svm_is_intercept(svm, INTERCEPT_VINTR)) {
289 * In order to request an interrupt window, L0 is usurping
290 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
291 * even if it was clear in L1's VMCB. Restoring it would be
292 * wrong. However, in this case V_IRQ will remain true until
293 * interrupt_window_interception calls svm_clear_vintr and
294 * restores int_ctl. We can just leave it aside.
298 svm->nested.ctl.int_ctl &= ~mask;
299 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
303 * Transfer any event that L0 or L1 wanted to inject into L2 to
306 static void nested_vmcb_save_pending_event(struct vcpu_svm *svm,
309 struct kvm_vcpu *vcpu = &svm->vcpu;
310 u32 exit_int_info = 0;
313 if (vcpu->arch.exception.injected) {
314 nr = vcpu->arch.exception.nr;
315 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
317 if (vcpu->arch.exception.has_error_code) {
318 exit_int_info |= SVM_EVTINJ_VALID_ERR;
319 vmcb12->control.exit_int_info_err =
320 vcpu->arch.exception.error_code;
323 } else if (vcpu->arch.nmi_injected) {
324 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
326 } else if (vcpu->arch.interrupt.injected) {
327 nr = vcpu->arch.interrupt.nr;
328 exit_int_info = nr | SVM_EVTINJ_VALID;
330 if (vcpu->arch.interrupt.soft)
331 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
333 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
336 vmcb12->control.exit_int_info = exit_int_info;
339 static inline bool nested_npt_enabled(struct vcpu_svm *svm)
341 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
345 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
346 * if we are emulating VM-Entry into a guest with NPT enabled.
348 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
351 if (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63))
354 if (!nested_npt && is_pae_paging(vcpu) &&
355 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
356 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
361 * TODO: optimize unconditional TLB flush/MMU sync here and in
362 * kvm_init_shadow_npt_mmu().
365 kvm_mmu_new_pgd(vcpu, cr3, false, false);
367 vcpu->arch.cr3 = cr3;
368 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
370 kvm_init_mmu(vcpu, false);
375 static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
377 /* Load the nested guest state */
378 svm->vmcb->save.es = vmcb12->save.es;
379 svm->vmcb->save.cs = vmcb12->save.cs;
380 svm->vmcb->save.ss = vmcb12->save.ss;
381 svm->vmcb->save.ds = vmcb12->save.ds;
382 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
383 svm->vmcb->save.idtr = vmcb12->save.idtr;
384 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags);
385 svm_set_efer(&svm->vcpu, vmcb12->save.efer);
386 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
387 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
388 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = vmcb12->save.cr2;
389 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
390 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
391 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
393 /* In case we don't even reach vcpu_run, the fields are not updated */
394 svm->vmcb->save.rax = vmcb12->save.rax;
395 svm->vmcb->save.rsp = vmcb12->save.rsp;
396 svm->vmcb->save.rip = vmcb12->save.rip;
397 svm->vmcb->save.dr7 = vmcb12->save.dr7;
398 svm->vcpu.arch.dr6 = vmcb12->save.dr6;
399 svm->vmcb->save.cpl = vmcb12->save.cpl;
402 static void nested_prepare_vmcb_control(struct vcpu_svm *svm)
404 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
406 if (nested_npt_enabled(svm))
407 nested_svm_init_mmu_context(&svm->vcpu);
409 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
410 svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
412 svm->vmcb->control.int_ctl =
413 (svm->nested.ctl.int_ctl & ~mask) |
414 (svm->nested.hsave->control.int_ctl & mask);
416 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
417 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
418 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
419 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
420 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
422 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
423 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
425 /* Enter Guest-Mode */
426 enter_guest_mode(&svm->vcpu);
429 * Merge guest and host intercepts - must be called with vcpu in
430 * guest-mode to take affect here
432 recalc_intercepts(svm);
434 vmcb_mark_all_dirty(svm->vmcb);
437 int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb12_gpa,
442 svm->nested.vmcb12_gpa = vmcb12_gpa;
443 load_nested_vmcb_control(svm, &vmcb12->control);
444 nested_prepare_vmcb_save(svm, vmcb12);
445 nested_prepare_vmcb_control(svm);
447 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
448 nested_npt_enabled(svm));
452 svm_set_gif(svm, true);
457 int nested_svm_vmrun(struct vcpu_svm *svm)
461 struct vmcb *hsave = svm->nested.hsave;
462 struct vmcb *vmcb = svm->vmcb;
463 struct kvm_host_map map;
466 if (is_smm(&svm->vcpu)) {
467 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
471 vmcb12_gpa = svm->vmcb->save.rax;
472 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb12_gpa), &map);
473 if (ret == -EINVAL) {
474 kvm_inject_gp(&svm->vcpu, 0);
477 return kvm_skip_emulated_instruction(&svm->vcpu);
480 ret = kvm_skip_emulated_instruction(&svm->vcpu);
484 if (!nested_vmcb_checks(svm, vmcb12)) {
485 vmcb12->control.exit_code = SVM_EXIT_ERR;
486 vmcb12->control.exit_code_hi = 0;
487 vmcb12->control.exit_info_1 = 0;
488 vmcb12->control.exit_info_2 = 0;
492 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
494 vmcb12->control.int_ctl,
495 vmcb12->control.event_inj,
496 vmcb12->control.nested_ctl);
498 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
499 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
500 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
501 vmcb12->control.intercepts[INTERCEPT_WORD3],
502 vmcb12->control.intercepts[INTERCEPT_WORD4],
503 vmcb12->control.intercepts[INTERCEPT_WORD5]);
505 /* Clear internal status */
506 kvm_clear_exception_queue(&svm->vcpu);
507 kvm_clear_interrupt_queue(&svm->vcpu);
510 * Save the old vmcb, so we don't need to pick what we save, but can
511 * restore everything when a VMEXIT occurs
513 hsave->save.es = vmcb->save.es;
514 hsave->save.cs = vmcb->save.cs;
515 hsave->save.ss = vmcb->save.ss;
516 hsave->save.ds = vmcb->save.ds;
517 hsave->save.gdtr = vmcb->save.gdtr;
518 hsave->save.idtr = vmcb->save.idtr;
519 hsave->save.efer = svm->vcpu.arch.efer;
520 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
521 hsave->save.cr4 = svm->vcpu.arch.cr4;
522 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
523 hsave->save.rip = kvm_rip_read(&svm->vcpu);
524 hsave->save.rsp = vmcb->save.rsp;
525 hsave->save.rax = vmcb->save.rax;
527 hsave->save.cr3 = vmcb->save.cr3;
529 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
531 copy_vmcb_control_area(&hsave->control, &vmcb->control);
533 svm->nested.nested_run_pending = 1;
535 if (enter_svm_guest_mode(svm, vmcb12_gpa, vmcb12))
538 if (nested_svm_vmrun_msrpm(svm))
542 svm->nested.nested_run_pending = 0;
544 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
545 svm->vmcb->control.exit_code_hi = 0;
546 svm->vmcb->control.exit_info_1 = 0;
547 svm->vmcb->control.exit_info_2 = 0;
549 nested_svm_vmexit(svm);
552 kvm_vcpu_unmap(&svm->vcpu, &map, true);
557 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
559 to_vmcb->save.fs = from_vmcb->save.fs;
560 to_vmcb->save.gs = from_vmcb->save.gs;
561 to_vmcb->save.tr = from_vmcb->save.tr;
562 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
563 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
564 to_vmcb->save.star = from_vmcb->save.star;
565 to_vmcb->save.lstar = from_vmcb->save.lstar;
566 to_vmcb->save.cstar = from_vmcb->save.cstar;
567 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
568 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
569 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
570 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
573 int nested_svm_vmexit(struct vcpu_svm *svm)
577 struct vmcb *hsave = svm->nested.hsave;
578 struct vmcb *vmcb = svm->vmcb;
579 struct kvm_host_map map;
581 rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
584 kvm_inject_gp(&svm->vcpu, 0);
590 /* Exit Guest-Mode */
591 leave_guest_mode(&svm->vcpu);
592 svm->nested.vmcb12_gpa = 0;
593 WARN_ON_ONCE(svm->nested.nested_run_pending);
595 /* in case we halted in L2 */
596 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
598 /* Give the current vmcb to the guest */
600 vmcb12->save.es = vmcb->save.es;
601 vmcb12->save.cs = vmcb->save.cs;
602 vmcb12->save.ss = vmcb->save.ss;
603 vmcb12->save.ds = vmcb->save.ds;
604 vmcb12->save.gdtr = vmcb->save.gdtr;
605 vmcb12->save.idtr = vmcb->save.idtr;
606 vmcb12->save.efer = svm->vcpu.arch.efer;
607 vmcb12->save.cr0 = kvm_read_cr0(&svm->vcpu);
608 vmcb12->save.cr3 = kvm_read_cr3(&svm->vcpu);
609 vmcb12->save.cr2 = vmcb->save.cr2;
610 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
611 vmcb12->save.rflags = kvm_get_rflags(&svm->vcpu);
612 vmcb12->save.rip = kvm_rip_read(&svm->vcpu);
613 vmcb12->save.rsp = kvm_rsp_read(&svm->vcpu);
614 vmcb12->save.rax = kvm_rax_read(&svm->vcpu);
615 vmcb12->save.dr7 = vmcb->save.dr7;
616 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
617 vmcb12->save.cpl = vmcb->save.cpl;
619 vmcb12->control.int_state = vmcb->control.int_state;
620 vmcb12->control.exit_code = vmcb->control.exit_code;
621 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
622 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
623 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
625 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
626 nested_vmcb_save_pending_event(svm, vmcb12);
628 if (svm->nrips_enabled)
629 vmcb12->control.next_rip = vmcb->control.next_rip;
631 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
632 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
633 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
634 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
636 vmcb12->control.pause_filter_count =
637 svm->vmcb->control.pause_filter_count;
638 vmcb12->control.pause_filter_thresh =
639 svm->vmcb->control.pause_filter_thresh;
641 /* Restore the original control entries */
642 copy_vmcb_control_area(&vmcb->control, &hsave->control);
644 /* On vmexit the GIF is set to false */
645 svm_set_gif(svm, false);
647 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
648 svm->vcpu.arch.l1_tsc_offset;
650 svm->nested.ctl.nested_cr3 = 0;
652 /* Restore selected save entries */
653 svm->vmcb->save.es = hsave->save.es;
654 svm->vmcb->save.cs = hsave->save.cs;
655 svm->vmcb->save.ss = hsave->save.ss;
656 svm->vmcb->save.ds = hsave->save.ds;
657 svm->vmcb->save.gdtr = hsave->save.gdtr;
658 svm->vmcb->save.idtr = hsave->save.idtr;
659 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
660 svm_set_efer(&svm->vcpu, hsave->save.efer);
661 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
662 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
663 kvm_rax_write(&svm->vcpu, hsave->save.rax);
664 kvm_rsp_write(&svm->vcpu, hsave->save.rsp);
665 kvm_rip_write(&svm->vcpu, hsave->save.rip);
666 svm->vmcb->save.dr7 = 0;
667 svm->vmcb->save.cpl = 0;
668 svm->vmcb->control.exit_int_info = 0;
670 vmcb_mark_all_dirty(svm->vmcb);
672 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
673 vmcb12->control.exit_info_1,
674 vmcb12->control.exit_info_2,
675 vmcb12->control.exit_int_info,
676 vmcb12->control.exit_int_info_err,
679 kvm_vcpu_unmap(&svm->vcpu, &map, true);
681 nested_svm_uninit_mmu_context(&svm->vcpu);
683 rc = nested_svm_load_cr3(&svm->vcpu, hsave->save.cr3, false);
688 svm->vmcb->save.cr3 = hsave->save.cr3;
691 * Drop what we picked up for L2 via svm_complete_interrupts() so it
692 * doesn't end up in L1.
694 svm->vcpu.arch.nmi_injected = false;
695 kvm_clear_exception_queue(&svm->vcpu);
696 kvm_clear_interrupt_queue(&svm->vcpu);
702 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
704 void svm_leave_nested(struct vcpu_svm *svm)
706 if (is_guest_mode(&svm->vcpu)) {
707 struct vmcb *hsave = svm->nested.hsave;
708 struct vmcb *vmcb = svm->vmcb;
710 svm->nested.nested_run_pending = 0;
711 leave_guest_mode(&svm->vcpu);
712 copy_vmcb_control_area(&vmcb->control, &hsave->control);
713 nested_svm_uninit_mmu_context(&svm->vcpu);
716 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, &svm->vcpu);
719 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
721 u32 offset, msr, value;
724 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
725 return NESTED_EXIT_HOST;
727 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
728 offset = svm_msrpm_offset(msr);
729 write = svm->vmcb->control.exit_info_1 & 1;
730 mask = 1 << ((2 * (msr & 0xf)) + write);
732 if (offset == MSR_INVALID)
733 return NESTED_EXIT_DONE;
735 /* Offset is in 32 bit units but need in 8 bit units */
738 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
739 return NESTED_EXIT_DONE;
741 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
744 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
746 unsigned port, size, iopm_len;
751 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
752 return NESTED_EXIT_HOST;
754 port = svm->vmcb->control.exit_info_1 >> 16;
755 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
757 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
758 start_bit = port % 8;
759 iopm_len = (start_bit + size > 8) ? 2 : 1;
760 mask = (0xf >> (4 - size)) << start_bit;
763 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
764 return NESTED_EXIT_DONE;
766 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
769 static int nested_svm_intercept(struct vcpu_svm *svm)
771 u32 exit_code = svm->vmcb->control.exit_code;
772 int vmexit = NESTED_EXIT_HOST;
776 vmexit = nested_svm_exit_handled_msr(svm);
779 vmexit = nested_svm_intercept_ioio(svm);
781 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
782 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
783 vmexit = NESTED_EXIT_DONE;
786 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
787 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
788 vmexit = NESTED_EXIT_DONE;
791 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
793 * Host-intercepted exceptions have been checked already in
794 * nested_svm_exit_special. There is nothing to do here,
795 * the vmexit is injected by svm_check_nested_events.
797 vmexit = NESTED_EXIT_DONE;
801 vmexit = NESTED_EXIT_DONE;
805 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
806 vmexit = NESTED_EXIT_DONE;
813 int nested_svm_exit_handled(struct vcpu_svm *svm)
817 vmexit = nested_svm_intercept(svm);
819 if (vmexit == NESTED_EXIT_DONE)
820 nested_svm_vmexit(svm);
825 int nested_svm_check_permissions(struct vcpu_svm *svm)
827 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
828 !is_paging(&svm->vcpu)) {
829 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
833 if (svm->vmcb->save.cpl) {
834 kvm_inject_gp(&svm->vcpu, 0);
841 static bool nested_exit_on_exception(struct vcpu_svm *svm)
843 unsigned int nr = svm->vcpu.arch.exception.nr;
845 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
848 static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
850 unsigned int nr = svm->vcpu.arch.exception.nr;
852 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
853 svm->vmcb->control.exit_code_hi = 0;
855 if (svm->vcpu.arch.exception.has_error_code)
856 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
859 * EXITINFO2 is undefined for all exception intercepts other
862 if (nr == PF_VECTOR) {
863 if (svm->vcpu.arch.exception.nested_apf)
864 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
865 else if (svm->vcpu.arch.exception.has_payload)
866 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
868 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
869 } else if (nr == DB_VECTOR) {
870 /* See inject_pending_event. */
871 kvm_deliver_exception_payload(&svm->vcpu);
872 if (svm->vcpu.arch.dr7 & DR7_GD) {
873 svm->vcpu.arch.dr7 &= ~DR7_GD;
874 kvm_update_dr7(&svm->vcpu);
877 WARN_ON(svm->vcpu.arch.exception.has_payload);
879 nested_svm_vmexit(svm);
882 static void nested_svm_smi(struct vcpu_svm *svm)
884 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
885 svm->vmcb->control.exit_info_1 = 0;
886 svm->vmcb->control.exit_info_2 = 0;
888 nested_svm_vmexit(svm);
891 static void nested_svm_nmi(struct vcpu_svm *svm)
893 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
894 svm->vmcb->control.exit_info_1 = 0;
895 svm->vmcb->control.exit_info_2 = 0;
897 nested_svm_vmexit(svm);
900 static void nested_svm_intr(struct vcpu_svm *svm)
902 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
904 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
905 svm->vmcb->control.exit_info_1 = 0;
906 svm->vmcb->control.exit_info_2 = 0;
908 nested_svm_vmexit(svm);
911 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
913 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
916 static void nested_svm_init(struct vcpu_svm *svm)
918 svm->vmcb->control.exit_code = SVM_EXIT_INIT;
919 svm->vmcb->control.exit_info_1 = 0;
920 svm->vmcb->control.exit_info_2 = 0;
922 nested_svm_vmexit(svm);
926 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
928 struct vcpu_svm *svm = to_svm(vcpu);
929 bool block_nested_events =
930 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
931 struct kvm_lapic *apic = vcpu->arch.apic;
933 if (lapic_in_kernel(vcpu) &&
934 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
935 if (block_nested_events)
937 if (!nested_exit_on_init(svm))
939 nested_svm_init(svm);
943 if (vcpu->arch.exception.pending) {
944 if (block_nested_events)
946 if (!nested_exit_on_exception(svm))
948 nested_svm_inject_exception_vmexit(svm);
952 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
953 if (block_nested_events)
955 if (!nested_exit_on_smi(svm))
961 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
962 if (block_nested_events)
964 if (!nested_exit_on_nmi(svm))
970 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
971 if (block_nested_events)
973 if (!nested_exit_on_intr(svm))
975 nested_svm_intr(svm);
982 int nested_svm_exit_special(struct vcpu_svm *svm)
984 u32 exit_code = svm->vmcb->control.exit_code;
990 return NESTED_EXIT_HOST;
991 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
992 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
994 if (get_host_vmcb(svm)->control.intercepts[INTERCEPT_EXCEPTION] &
996 return NESTED_EXIT_HOST;
997 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
998 svm->vcpu.arch.apf.host_apf_flags)
999 /* Trap async PF even if not shadowing */
1000 return NESTED_EXIT_HOST;
1007 return NESTED_EXIT_CONTINUE;
1010 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1011 struct kvm_nested_state __user *user_kvm_nested_state,
1014 struct vcpu_svm *svm;
1015 struct kvm_nested_state kvm_state = {
1017 .format = KVM_STATE_NESTED_FORMAT_SVM,
1018 .size = sizeof(kvm_state),
1020 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1021 &user_kvm_nested_state->data.svm[0];
1024 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1028 if (user_data_size < kvm_state.size)
1031 /* First fill in the header and copy it out. */
1032 if (is_guest_mode(vcpu)) {
1033 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1034 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1035 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1037 if (svm->nested.nested_run_pending)
1038 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1042 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1044 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1047 if (!is_guest_mode(vcpu))
1051 * Copy over the full size of the VMCB rather than just the size
1054 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1056 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1057 sizeof(user_vmcb->control)))
1059 if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save,
1060 sizeof(user_vmcb->save)))
1064 return kvm_state.size;
1067 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1068 struct kvm_nested_state __user *user_kvm_nested_state,
1069 struct kvm_nested_state *kvm_state)
1071 struct vcpu_svm *svm = to_svm(vcpu);
1072 struct vmcb *hsave = svm->nested.hsave;
1073 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1074 &user_kvm_nested_state->data.svm[0];
1075 struct vmcb_control_area *ctl;
1076 struct vmcb_save_area *save;
1080 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1081 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1083 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1086 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1087 KVM_STATE_NESTED_RUN_PENDING |
1088 KVM_STATE_NESTED_GIF_SET))
1092 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1093 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1095 if (!(vcpu->arch.efer & EFER_SVME)) {
1096 /* GIF=1 and no guest mode are required if SVME=0. */
1097 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1101 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1102 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1105 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1106 svm_leave_nested(svm);
1107 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1111 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1113 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1117 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1118 save = kzalloc(sizeof(*save), GFP_KERNEL);
1123 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1125 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1129 if (!nested_vmcb_check_controls(ctl))
1133 * Processor state contains L2 state. Check that it is
1134 * valid for guest mode (see nested_vmcb_checks).
1136 cr0 = kvm_read_cr0(vcpu);
1137 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1141 * Validate host state saved from before VMRUN (see
1142 * nested_svm_check_permissions).
1143 * TODO: validate reserved bits for all saved state.
1145 if (!(save->cr0 & X86_CR0_PG))
1149 * All checks done, we can enter guest mode. L1 control fields
1150 * come from the nested save state. Guest state is already
1151 * in the registers, the save area of the nested state instead
1152 * contains saved L1 state.
1154 copy_vmcb_control_area(&hsave->control, &svm->vmcb->control);
1155 hsave->save = *save;
1157 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1158 load_nested_vmcb_control(svm, ctl);
1159 nested_prepare_vmcb_control(svm);
1161 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1170 struct kvm_x86_nested_ops svm_nested_ops = {
1171 .check_events = svm_check_nested_events,
1172 .get_nested_state_pages = svm_get_nested_state_pages,
1173 .get_state = svm_get_nested_state,
1174 .set_state = svm_set_nested_state,