xref: /linux/arch/x86/kvm/svm/nested.c (revision 907afa48e9d0f24713a34135428d981e4239a3be)
1883b0a91SJoerg Roedel // SPDX-License-Identifier: GPL-2.0-only
2883b0a91SJoerg Roedel /*
3883b0a91SJoerg Roedel  * Kernel-based Virtual Machine driver for Linux
4883b0a91SJoerg Roedel  *
5883b0a91SJoerg Roedel  * AMD SVM support
6883b0a91SJoerg Roedel  *
7883b0a91SJoerg Roedel  * Copyright (C) 2006 Qumranet, Inc.
8883b0a91SJoerg Roedel  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
9883b0a91SJoerg Roedel  *
10883b0a91SJoerg Roedel  * Authors:
11883b0a91SJoerg Roedel  *   Yaniv Kamay  <yaniv@qumranet.com>
12883b0a91SJoerg Roedel  *   Avi Kivity   <avi@qumranet.com>
13883b0a91SJoerg Roedel  */
14883b0a91SJoerg Roedel 
15883b0a91SJoerg Roedel #define pr_fmt(fmt) "SVM: " fmt
16883b0a91SJoerg Roedel 
17883b0a91SJoerg Roedel #include <linux/kvm_types.h>
18883b0a91SJoerg Roedel #include <linux/kvm_host.h>
19883b0a91SJoerg Roedel #include <linux/kernel.h>
20883b0a91SJoerg Roedel 
21883b0a91SJoerg Roedel #include <asm/msr-index.h>
225679b803SPaolo Bonzini #include <asm/debugreg.h>
23883b0a91SJoerg Roedel 
24883b0a91SJoerg Roedel #include "kvm_emulate.h"
25883b0a91SJoerg Roedel #include "trace.h"
26883b0a91SJoerg Roedel #include "mmu.h"
27883b0a91SJoerg Roedel #include "x86.h"
28cc440cdaSPaolo Bonzini #include "cpuid.h"
295b672408SPaolo Bonzini #include "lapic.h"
30883b0a91SJoerg Roedel #include "svm.h"
31883b0a91SJoerg Roedel 
3211f0cbf0SSean Christopherson #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
3311f0cbf0SSean Christopherson 
34883b0a91SJoerg Roedel static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
35883b0a91SJoerg Roedel 				       struct x86_exception *fault)
36883b0a91SJoerg Roedel {
37883b0a91SJoerg Roedel 	struct vcpu_svm *svm = to_svm(vcpu);
38883b0a91SJoerg Roedel 
39883b0a91SJoerg Roedel 	if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
40883b0a91SJoerg Roedel 		/*
41883b0a91SJoerg Roedel 		 * TODO: track the cause of the nested page fault, and
42883b0a91SJoerg Roedel 		 * correctly fill in the high bits of exit_info_1.
43883b0a91SJoerg Roedel 		 */
44883b0a91SJoerg Roedel 		svm->vmcb->control.exit_code = SVM_EXIT_NPF;
45883b0a91SJoerg Roedel 		svm->vmcb->control.exit_code_hi = 0;
46883b0a91SJoerg Roedel 		svm->vmcb->control.exit_info_1 = (1ULL << 32);
47883b0a91SJoerg Roedel 		svm->vmcb->control.exit_info_2 = fault->address;
48883b0a91SJoerg Roedel 	}
49883b0a91SJoerg Roedel 
50883b0a91SJoerg Roedel 	svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
51883b0a91SJoerg Roedel 	svm->vmcb->control.exit_info_1 |= fault->error_code;
52883b0a91SJoerg Roedel 
53883b0a91SJoerg Roedel 	nested_svm_vmexit(svm);
54883b0a91SJoerg Roedel }
55883b0a91SJoerg Roedel 
56a04aead1SPaolo Bonzini static void svm_inject_page_fault_nested(struct kvm_vcpu *vcpu, struct x86_exception *fault)
57a04aead1SPaolo Bonzini {
58a04aead1SPaolo Bonzini        struct vcpu_svm *svm = to_svm(vcpu);
59a04aead1SPaolo Bonzini        WARN_ON(!is_guest_mode(vcpu));
60a04aead1SPaolo Bonzini 
61a04aead1SPaolo Bonzini        if (vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
62a04aead1SPaolo Bonzini 	   !svm->nested.nested_run_pending) {
63a04aead1SPaolo Bonzini                svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
64a04aead1SPaolo Bonzini                svm->vmcb->control.exit_code_hi = 0;
65a04aead1SPaolo Bonzini                svm->vmcb->control.exit_info_1 = fault->error_code;
66a04aead1SPaolo Bonzini                svm->vmcb->control.exit_info_2 = fault->address;
67a04aead1SPaolo Bonzini                nested_svm_vmexit(svm);
68a04aead1SPaolo Bonzini        } else {
69a04aead1SPaolo Bonzini                kvm_inject_page_fault(vcpu, fault);
70a04aead1SPaolo Bonzini        }
71a04aead1SPaolo Bonzini }
72a04aead1SPaolo Bonzini 
73883b0a91SJoerg Roedel static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
74883b0a91SJoerg Roedel {
75883b0a91SJoerg Roedel 	struct vcpu_svm *svm = to_svm(vcpu);
76e670bf68SPaolo Bonzini 	u64 cr3 = svm->nested.ctl.nested_cr3;
77883b0a91SJoerg Roedel 	u64 pdpte;
78883b0a91SJoerg Roedel 	int ret;
79883b0a91SJoerg Roedel 
802732be90SSean Christopherson 	ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
81883b0a91SJoerg Roedel 				       offset_in_page(cr3) + index * 8, 8);
82883b0a91SJoerg Roedel 	if (ret)
83883b0a91SJoerg Roedel 		return 0;
84883b0a91SJoerg Roedel 	return pdpte;
85883b0a91SJoerg Roedel }
86883b0a91SJoerg Roedel 
87883b0a91SJoerg Roedel static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
88883b0a91SJoerg Roedel {
89883b0a91SJoerg Roedel 	struct vcpu_svm *svm = to_svm(vcpu);
90883b0a91SJoerg Roedel 
91e670bf68SPaolo Bonzini 	return svm->nested.ctl.nested_cr3;
92883b0a91SJoerg Roedel }
93883b0a91SJoerg Roedel 
94883b0a91SJoerg Roedel static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
95883b0a91SJoerg Roedel {
96929d1cfaSPaolo Bonzini 	struct vcpu_svm *svm = to_svm(vcpu);
97929d1cfaSPaolo Bonzini 
98883b0a91SJoerg Roedel 	WARN_ON(mmu_is_nested(vcpu));
99883b0a91SJoerg Roedel 
100883b0a91SJoerg Roedel 	vcpu->arch.mmu = &vcpu->arch.guest_mmu;
10131e96bc6SSean Christopherson 
10231e96bc6SSean Christopherson 	/*
10331e96bc6SSean Christopherson 	 * The NPT format depends on L1's CR4 and EFER, which is in vmcb01.  Note,
10431e96bc6SSean Christopherson 	 * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
10531e96bc6SSean Christopherson 	 * vCPU state.  CR0.WP is explicitly ignored, while CR0.PG is required.
10631e96bc6SSean Christopherson 	 */
1074995a368SCathy Avery 	kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
1084995a368SCathy Avery 				svm->vmcb01.ptr->save.efer,
1090f04a2acSVitaly Kuznetsov 				svm->nested.ctl.nested_cr3);
110883b0a91SJoerg Roedel 	vcpu->arch.mmu->get_guest_pgd     = nested_svm_get_tdp_cr3;
111883b0a91SJoerg Roedel 	vcpu->arch.mmu->get_pdptr         = nested_svm_get_tdp_pdptr;
112883b0a91SJoerg Roedel 	vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
113883b0a91SJoerg Roedel 	vcpu->arch.walk_mmu              = &vcpu->arch.nested_mmu;
114883b0a91SJoerg Roedel }
115883b0a91SJoerg Roedel 
116883b0a91SJoerg Roedel static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
117883b0a91SJoerg Roedel {
118883b0a91SJoerg Roedel 	vcpu->arch.mmu = &vcpu->arch.root_mmu;
119883b0a91SJoerg Roedel 	vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
120883b0a91SJoerg Roedel }
121883b0a91SJoerg Roedel 
122883b0a91SJoerg Roedel void recalc_intercepts(struct vcpu_svm *svm)
123883b0a91SJoerg Roedel {
124e670bf68SPaolo Bonzini 	struct vmcb_control_area *c, *h, *g;
125c45ad722SBabu Moger 	unsigned int i;
126883b0a91SJoerg Roedel 
12706e7852cSJoerg Roedel 	vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
128883b0a91SJoerg Roedel 
129883b0a91SJoerg Roedel 	if (!is_guest_mode(&svm->vcpu))
130883b0a91SJoerg Roedel 		return;
131883b0a91SJoerg Roedel 
132883b0a91SJoerg Roedel 	c = &svm->vmcb->control;
1334995a368SCathy Avery 	h = &svm->vmcb01.ptr->control;
134e670bf68SPaolo Bonzini 	g = &svm->nested.ctl;
135883b0a91SJoerg Roedel 
136c45ad722SBabu Moger 	for (i = 0; i < MAX_INTERCEPT; i++)
137c45ad722SBabu Moger 		c->intercepts[i] = h->intercepts[i];
138c45ad722SBabu Moger 
139e9fd761aSPaolo Bonzini 	if (g->int_ctl & V_INTR_MASKING_MASK) {
140883b0a91SJoerg Roedel 		/* We only want the cr8 intercept bits of L1 */
14103bfeeb9SBabu Moger 		vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
14203bfeeb9SBabu Moger 		vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
143883b0a91SJoerg Roedel 
144883b0a91SJoerg Roedel 		/*
145883b0a91SJoerg Roedel 		 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
146883b0a91SJoerg Roedel 		 * affect any interrupt we may want to inject; therefore,
147883b0a91SJoerg Roedel 		 * interrupt window vmexits are irrelevant to L0.
148883b0a91SJoerg Roedel 		 */
149c62e2e94SBabu Moger 		vmcb_clr_intercept(c, INTERCEPT_VINTR);
150883b0a91SJoerg Roedel 	}
151883b0a91SJoerg Roedel 
152883b0a91SJoerg Roedel 	/* We don't want to see VMMCALLs from a nested guest */
153c62e2e94SBabu Moger 	vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
154883b0a91SJoerg Roedel 
155c45ad722SBabu Moger 	for (i = 0; i < MAX_INTERCEPT; i++)
156c45ad722SBabu Moger 		c->intercepts[i] |= g->intercepts[i];
1574b639a9fSMaxim Levitsky 
1584b639a9fSMaxim Levitsky 	/* If SMI is not intercepted, ignore guest SMI intercept as well  */
1594b639a9fSMaxim Levitsky 	if (!intercept_smi)
1604b639a9fSMaxim Levitsky 		vmcb_clr_intercept(c, INTERCEPT_SMI);
161c7dfa400SMaxim Levitsky 
162c7dfa400SMaxim Levitsky 	vmcb_set_intercept(c, INTERCEPT_VMLOAD);
163c7dfa400SMaxim Levitsky 	vmcb_set_intercept(c, INTERCEPT_VMSAVE);
164883b0a91SJoerg Roedel }
165883b0a91SJoerg Roedel 
1662f675917SPaolo Bonzini static void copy_vmcb_control_area(struct vmcb_control_area *dst,
1672f675917SPaolo Bonzini 				   struct vmcb_control_area *from)
168883b0a91SJoerg Roedel {
169c45ad722SBabu Moger 	unsigned int i;
170c45ad722SBabu Moger 
171c45ad722SBabu Moger 	for (i = 0; i < MAX_INTERCEPT; i++)
172c45ad722SBabu Moger 		dst->intercepts[i] = from->intercepts[i];
173c45ad722SBabu Moger 
174883b0a91SJoerg Roedel 	dst->iopm_base_pa         = from->iopm_base_pa;
175883b0a91SJoerg Roedel 	dst->msrpm_base_pa        = from->msrpm_base_pa;
176883b0a91SJoerg Roedel 	dst->tsc_offset           = from->tsc_offset;
1776c0238c4SPaolo Bonzini 	/* asid not copied, it is handled manually for svm->vmcb.  */
178883b0a91SJoerg Roedel 	dst->tlb_ctl              = from->tlb_ctl;
179883b0a91SJoerg Roedel 	dst->int_ctl              = from->int_ctl;
180883b0a91SJoerg Roedel 	dst->int_vector           = from->int_vector;
181883b0a91SJoerg Roedel 	dst->int_state            = from->int_state;
182883b0a91SJoerg Roedel 	dst->exit_code            = from->exit_code;
183883b0a91SJoerg Roedel 	dst->exit_code_hi         = from->exit_code_hi;
184883b0a91SJoerg Roedel 	dst->exit_info_1          = from->exit_info_1;
185883b0a91SJoerg Roedel 	dst->exit_info_2          = from->exit_info_2;
186883b0a91SJoerg Roedel 	dst->exit_int_info        = from->exit_int_info;
187883b0a91SJoerg Roedel 	dst->exit_int_info_err    = from->exit_int_info_err;
188883b0a91SJoerg Roedel 	dst->nested_ctl           = from->nested_ctl;
189883b0a91SJoerg Roedel 	dst->event_inj            = from->event_inj;
190883b0a91SJoerg Roedel 	dst->event_inj_err        = from->event_inj_err;
191883b0a91SJoerg Roedel 	dst->nested_cr3           = from->nested_cr3;
192883b0a91SJoerg Roedel 	dst->virt_ext              = from->virt_ext;
193883b0a91SJoerg Roedel 	dst->pause_filter_count   = from->pause_filter_count;
194883b0a91SJoerg Roedel 	dst->pause_filter_thresh  = from->pause_filter_thresh;
195883b0a91SJoerg Roedel }
196883b0a91SJoerg Roedel 
197883b0a91SJoerg Roedel static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
198883b0a91SJoerg Roedel {
199883b0a91SJoerg Roedel 	/*
200883b0a91SJoerg Roedel 	 * This function merges the msr permission bitmaps of kvm and the
201883b0a91SJoerg Roedel 	 * nested vmcb. It is optimized in that it only merges the parts where
202883b0a91SJoerg Roedel 	 * the kvm msr permission bitmap may contain zero bits
203883b0a91SJoerg Roedel 	 */
204883b0a91SJoerg Roedel 	int i;
205883b0a91SJoerg Roedel 
206c62e2e94SBabu Moger 	if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
207883b0a91SJoerg Roedel 		return true;
208883b0a91SJoerg Roedel 
209883b0a91SJoerg Roedel 	for (i = 0; i < MSRPM_OFFSETS; i++) {
210883b0a91SJoerg Roedel 		u32 value, p;
211883b0a91SJoerg Roedel 		u64 offset;
212883b0a91SJoerg Roedel 
213883b0a91SJoerg Roedel 		if (msrpm_offsets[i] == 0xffffffff)
214883b0a91SJoerg Roedel 			break;
215883b0a91SJoerg Roedel 
216883b0a91SJoerg Roedel 		p      = msrpm_offsets[i];
217e670bf68SPaolo Bonzini 		offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
218883b0a91SJoerg Roedel 
219883b0a91SJoerg Roedel 		if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
220883b0a91SJoerg Roedel 			return false;
221883b0a91SJoerg Roedel 
222883b0a91SJoerg Roedel 		svm->nested.msrpm[p] = svm->msrpm[p] | value;
223883b0a91SJoerg Roedel 	}
224883b0a91SJoerg Roedel 
225883b0a91SJoerg Roedel 	svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
226883b0a91SJoerg Roedel 
227883b0a91SJoerg Roedel 	return true;
228883b0a91SJoerg Roedel }
229883b0a91SJoerg Roedel 
230ee695f22SKrish Sadhukhan /*
231ee695f22SKrish Sadhukhan  * Bits 11:0 of bitmap address are ignored by hardware
232ee695f22SKrish Sadhukhan  */
233ee695f22SKrish Sadhukhan static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
234ee695f22SKrish Sadhukhan {
235ee695f22SKrish Sadhukhan 	u64 addr = PAGE_ALIGN(pa);
236ee695f22SKrish Sadhukhan 
237ee695f22SKrish Sadhukhan 	return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
238ee695f22SKrish Sadhukhan 	    kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
239ee695f22SKrish Sadhukhan }
240ee695f22SKrish Sadhukhan 
241174a921bSKrish Sadhukhan static bool nested_svm_check_tlb_ctl(struct kvm_vcpu *vcpu, u8 tlb_ctl)
242174a921bSKrish Sadhukhan {
243174a921bSKrish Sadhukhan 	/* Nested FLUSHBYASID is not supported yet.  */
244174a921bSKrish Sadhukhan 	switch(tlb_ctl) {
245174a921bSKrish Sadhukhan 		case TLB_CONTROL_DO_NOTHING:
246174a921bSKrish Sadhukhan 		case TLB_CONTROL_FLUSH_ALL_ASID:
247174a921bSKrish Sadhukhan 			return true;
248174a921bSKrish Sadhukhan 		default:
249174a921bSKrish Sadhukhan 			return false;
250174a921bSKrish Sadhukhan 	}
251174a921bSKrish Sadhukhan }
252174a921bSKrish Sadhukhan 
253ee695f22SKrish Sadhukhan static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
254ee695f22SKrish Sadhukhan 				       struct vmcb_control_area *control)
255ca46d739SPaolo Bonzini {
25611f0cbf0SSean Christopherson 	if (CC(!vmcb_is_intercept(control, INTERCEPT_VMRUN)))
257ca46d739SPaolo Bonzini 		return false;
258ca46d739SPaolo Bonzini 
25911f0cbf0SSean Christopherson 	if (CC(control->asid == 0))
260ca46d739SPaolo Bonzini 		return false;
261ca46d739SPaolo Bonzini 
26211f0cbf0SSean Christopherson 	if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
263ca46d739SPaolo Bonzini 		return false;
264ca46d739SPaolo Bonzini 
265ee695f22SKrish Sadhukhan 	if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
266ee695f22SKrish Sadhukhan 					   MSRPM_SIZE)))
267ee695f22SKrish Sadhukhan 		return false;
268ee695f22SKrish Sadhukhan 	if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
269ee695f22SKrish Sadhukhan 					   IOPM_SIZE)))
270ee695f22SKrish Sadhukhan 		return false;
271ee695f22SKrish Sadhukhan 
272174a921bSKrish Sadhukhan 	if (CC(!nested_svm_check_tlb_ctl(vcpu, control->tlb_ctl)))
273174a921bSKrish Sadhukhan 		return false;
274174a921bSKrish Sadhukhan 
275ca46d739SPaolo Bonzini 	return true;
276ca46d739SPaolo Bonzini }
277ca46d739SPaolo Bonzini 
2786906e06dSKrish Sadhukhan /* Common checks that apply to both L1 and L2 state.  */
27963129754SPaolo Bonzini static bool nested_vmcb_valid_sregs(struct kvm_vcpu *vcpu,
2806906e06dSKrish Sadhukhan 				    struct vmcb_save_area *save)
2816906e06dSKrish Sadhukhan {
2823c346c0cSPaolo Bonzini 	/*
2833c346c0cSPaolo Bonzini 	 * FIXME: these should be done after copying the fields,
2843c346c0cSPaolo Bonzini 	 * to avoid TOC/TOU races.  For these save area checks
2853c346c0cSPaolo Bonzini 	 * the possible damage is limited since kvm_set_cr0 and
2863c346c0cSPaolo Bonzini 	 * kvm_set_cr4 handle failure; EFER_SVME is an exception
2873c346c0cSPaolo Bonzini 	 * so it is force-set later in nested_prepare_vmcb_save.
2883c346c0cSPaolo Bonzini 	 */
28911f0cbf0SSean Christopherson 	if (CC(!(save->efer & EFER_SVME)))
2906906e06dSKrish Sadhukhan 		return false;
2916906e06dSKrish Sadhukhan 
29211f0cbf0SSean Christopherson 	if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
29311f0cbf0SSean Christopherson 	    CC(save->cr0 & ~0xffffffffULL))
2946906e06dSKrish Sadhukhan 		return false;
2956906e06dSKrish Sadhukhan 
29611f0cbf0SSean Christopherson 	if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
2976906e06dSKrish Sadhukhan 		return false;
2986906e06dSKrish Sadhukhan 
299*907afa48SEmanuele Giuseppe Esposito 	/*
300*907afa48SEmanuele Giuseppe Esposito 	 * These checks are also performed by KVM_SET_SREGS,
301*907afa48SEmanuele Giuseppe Esposito 	 * except that EFER.LMA is not checked by SVM against
302*907afa48SEmanuele Giuseppe Esposito 	 * CR0.PG && EFER.LME.
303*907afa48SEmanuele Giuseppe Esposito 	 */
304*907afa48SEmanuele Giuseppe Esposito 	if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
305*907afa48SEmanuele Giuseppe Esposito 		if (CC(!(save->cr4 & X86_CR4_PAE)) ||
306*907afa48SEmanuele Giuseppe Esposito 		    CC(!(save->cr0 & X86_CR0_PE)) ||
307*907afa48SEmanuele Giuseppe Esposito 		    CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
308*907afa48SEmanuele Giuseppe Esposito 			return false;
309*907afa48SEmanuele Giuseppe Esposito 	}
310*907afa48SEmanuele Giuseppe Esposito 
311*907afa48SEmanuele Giuseppe Esposito 	if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
3126906e06dSKrish Sadhukhan 		return false;
3136906e06dSKrish Sadhukhan 
31463129754SPaolo Bonzini 	if (CC(!kvm_valid_efer(vcpu, save->efer)))
3156906e06dSKrish Sadhukhan 		return false;
3166906e06dSKrish Sadhukhan 
3176906e06dSKrish Sadhukhan 	return true;
3186906e06dSKrish Sadhukhan }
3196906e06dSKrish Sadhukhan 
320bb00bd9cSVitaly Kuznetsov void nested_load_control_from_vmcb12(struct vcpu_svm *svm,
3213e06f016SPaolo Bonzini 				     struct vmcb_control_area *control)
3223e06f016SPaolo Bonzini {
323e670bf68SPaolo Bonzini 	copy_vmcb_control_area(&svm->nested.ctl, control);
3243e06f016SPaolo Bonzini 
325cc440cdaSPaolo Bonzini 	/* Copy it here because nested_svm_check_controls will check it.  */
326cc440cdaSPaolo Bonzini 	svm->nested.ctl.asid           = control->asid;
327e670bf68SPaolo Bonzini 	svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
328e670bf68SPaolo Bonzini 	svm->nested.ctl.iopm_base_pa  &= ~0x0fffULL;
3293e06f016SPaolo Bonzini }
3303e06f016SPaolo Bonzini 
3312d8a42beSPaolo Bonzini /*
3322d8a42beSPaolo Bonzini  * Synchronize fields that are written by the processor, so that
3339e8f0fbfSPaolo Bonzini  * they can be copied back into the vmcb12.
3342d8a42beSPaolo Bonzini  */
3359e8f0fbfSPaolo Bonzini void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
3362d8a42beSPaolo Bonzini {
3372d8a42beSPaolo Bonzini 	u32 mask;
3382d8a42beSPaolo Bonzini 	svm->nested.ctl.event_inj      = svm->vmcb->control.event_inj;
3392d8a42beSPaolo Bonzini 	svm->nested.ctl.event_inj_err  = svm->vmcb->control.event_inj_err;
3402d8a42beSPaolo Bonzini 
3412d8a42beSPaolo Bonzini 	/* Only a few fields of int_ctl are written by the processor.  */
3422d8a42beSPaolo Bonzini 	mask = V_IRQ_MASK | V_TPR_MASK;
3432d8a42beSPaolo Bonzini 	if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
344a284ba56SJoerg Roedel 	    svm_is_intercept(svm, INTERCEPT_VINTR)) {
3452d8a42beSPaolo Bonzini 		/*
3462d8a42beSPaolo Bonzini 		 * In order to request an interrupt window, L0 is usurping
3472d8a42beSPaolo Bonzini 		 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
3482d8a42beSPaolo Bonzini 		 * even if it was clear in L1's VMCB.  Restoring it would be
3492d8a42beSPaolo Bonzini 		 * wrong.  However, in this case V_IRQ will remain true until
3502d8a42beSPaolo Bonzini 		 * interrupt_window_interception calls svm_clear_vintr and
3512d8a42beSPaolo Bonzini 		 * restores int_ctl.  We can just leave it aside.
3522d8a42beSPaolo Bonzini 		 */
3532d8a42beSPaolo Bonzini 		mask &= ~V_IRQ_MASK;
3542d8a42beSPaolo Bonzini 	}
3552d8a42beSPaolo Bonzini 	svm->nested.ctl.int_ctl        &= ~mask;
3562d8a42beSPaolo Bonzini 	svm->nested.ctl.int_ctl        |= svm->vmcb->control.int_ctl & mask;
3572d8a42beSPaolo Bonzini }
3582d8a42beSPaolo Bonzini 
35936e2e983SPaolo Bonzini /*
36036e2e983SPaolo Bonzini  * Transfer any event that L0 or L1 wanted to inject into L2 to
36136e2e983SPaolo Bonzini  * EXIT_INT_INFO.
36236e2e983SPaolo Bonzini  */
3639e8f0fbfSPaolo Bonzini static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
3640dd16b5bSMaxim Levitsky 						struct vmcb *vmcb12)
36536e2e983SPaolo Bonzini {
36636e2e983SPaolo Bonzini 	struct kvm_vcpu *vcpu = &svm->vcpu;
36736e2e983SPaolo Bonzini 	u32 exit_int_info = 0;
36836e2e983SPaolo Bonzini 	unsigned int nr;
36936e2e983SPaolo Bonzini 
37036e2e983SPaolo Bonzini 	if (vcpu->arch.exception.injected) {
37136e2e983SPaolo Bonzini 		nr = vcpu->arch.exception.nr;
37236e2e983SPaolo Bonzini 		exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
37336e2e983SPaolo Bonzini 
37436e2e983SPaolo Bonzini 		if (vcpu->arch.exception.has_error_code) {
37536e2e983SPaolo Bonzini 			exit_int_info |= SVM_EVTINJ_VALID_ERR;
3760dd16b5bSMaxim Levitsky 			vmcb12->control.exit_int_info_err =
37736e2e983SPaolo Bonzini 				vcpu->arch.exception.error_code;
37836e2e983SPaolo Bonzini 		}
37936e2e983SPaolo Bonzini 
38036e2e983SPaolo Bonzini 	} else if (vcpu->arch.nmi_injected) {
38136e2e983SPaolo Bonzini 		exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
38236e2e983SPaolo Bonzini 
38336e2e983SPaolo Bonzini 	} else if (vcpu->arch.interrupt.injected) {
38436e2e983SPaolo Bonzini 		nr = vcpu->arch.interrupt.nr;
38536e2e983SPaolo Bonzini 		exit_int_info = nr | SVM_EVTINJ_VALID;
38636e2e983SPaolo Bonzini 
38736e2e983SPaolo Bonzini 		if (vcpu->arch.interrupt.soft)
38836e2e983SPaolo Bonzini 			exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
38936e2e983SPaolo Bonzini 		else
39036e2e983SPaolo Bonzini 			exit_int_info |= SVM_EVTINJ_TYPE_INTR;
39136e2e983SPaolo Bonzini 	}
39236e2e983SPaolo Bonzini 
3930dd16b5bSMaxim Levitsky 	vmcb12->control.exit_int_info = exit_int_info;
39436e2e983SPaolo Bonzini }
39536e2e983SPaolo Bonzini 
39662156f6cSVitaly Kuznetsov static inline bool nested_npt_enabled(struct vcpu_svm *svm)
39762156f6cSVitaly Kuznetsov {
39862156f6cSVitaly Kuznetsov 	return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
39962156f6cSVitaly Kuznetsov }
40062156f6cSVitaly Kuznetsov 
401d2e56019SSean Christopherson static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
402d2e56019SSean Christopherson {
403d2e56019SSean Christopherson 	/*
404d2e56019SSean Christopherson 	 * TODO: optimize unconditional TLB flush/MMU sync.  A partial list of
405d2e56019SSean Christopherson 	 * things to fix before this can be conditional:
406d2e56019SSean Christopherson 	 *
407d2e56019SSean Christopherson 	 *  - Flush TLBs for both L1 and L2 remote TLB flush
408d2e56019SSean Christopherson 	 *  - Honor L1's request to flush an ASID on nested VMRUN
409d2e56019SSean Christopherson 	 *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
410d2e56019SSean Christopherson 	 *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
411d2e56019SSean Christopherson 	 *  - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
412d2e56019SSean Christopherson 	 *
413d2e56019SSean Christopherson 	 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
414d2e56019SSean Christopherson 	 *     NPT guest-physical mappings on VMRUN.
415d2e56019SSean Christopherson 	 */
416d2e56019SSean Christopherson 	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
417d2e56019SSean Christopherson 	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
418d2e56019SSean Christopherson }
419d2e56019SSean Christopherson 
42062156f6cSVitaly Kuznetsov /*
421d82aaef9SVitaly Kuznetsov  * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
422d82aaef9SVitaly Kuznetsov  * if we are emulating VM-Entry into a guest with NPT enabled.
42362156f6cSVitaly Kuznetsov  */
42462156f6cSVitaly Kuznetsov static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
425b222b0b8SMaxim Levitsky 			       bool nested_npt, bool reload_pdptrs)
42662156f6cSVitaly Kuznetsov {
42711f0cbf0SSean Christopherson 	if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
428a506fdd2SVitaly Kuznetsov 		return -EINVAL;
429a506fdd2SVitaly Kuznetsov 
430b222b0b8SMaxim Levitsky 	if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
431a36dbec6SSean Christopherson 	    CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
432a506fdd2SVitaly Kuznetsov 		return -EINVAL;
433a506fdd2SVitaly Kuznetsov 
434a506fdd2SVitaly Kuznetsov 	if (!nested_npt)
435b5129100SSean Christopherson 		kvm_mmu_new_pgd(vcpu, cr3);
436a506fdd2SVitaly Kuznetsov 
437a506fdd2SVitaly Kuznetsov 	vcpu->arch.cr3 = cr3;
438a506fdd2SVitaly Kuznetsov 	kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
439a506fdd2SVitaly Kuznetsov 
440616007c8SSean Christopherson 	/* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
441c9060662SSean Christopherson 	kvm_init_mmu(vcpu);
442a506fdd2SVitaly Kuznetsov 
443a506fdd2SVitaly Kuznetsov 	return 0;
44462156f6cSVitaly Kuznetsov }
44562156f6cSVitaly Kuznetsov 
4464995a368SCathy Avery void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
4474995a368SCathy Avery {
4484995a368SCathy Avery 	if (!svm->nested.vmcb02.ptr)
4494995a368SCathy Avery 		return;
4504995a368SCathy Avery 
4514995a368SCathy Avery 	/* FIXME: merge g_pat from vmcb01 and vmcb12.  */
4524995a368SCathy Avery 	svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
4534995a368SCathy Avery }
4544995a368SCathy Avery 
4559e8f0fbfSPaolo Bonzini static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
456883b0a91SJoerg Roedel {
4578173396eSCathy Avery 	bool new_vmcb12 = false;
4588173396eSCathy Avery 
4594995a368SCathy Avery 	nested_vmcb02_compute_g_pat(svm);
4604995a368SCathy Avery 
461883b0a91SJoerg Roedel 	/* Load the nested guest state */
4628173396eSCathy Avery 	if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
4638173396eSCathy Avery 		new_vmcb12 = true;
4648173396eSCathy Avery 		svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
4658173396eSCathy Avery 	}
4668173396eSCathy Avery 
4678173396eSCathy Avery 	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
4680dd16b5bSMaxim Levitsky 		svm->vmcb->save.es = vmcb12->save.es;
4690dd16b5bSMaxim Levitsky 		svm->vmcb->save.cs = vmcb12->save.cs;
4700dd16b5bSMaxim Levitsky 		svm->vmcb->save.ss = vmcb12->save.ss;
4710dd16b5bSMaxim Levitsky 		svm->vmcb->save.ds = vmcb12->save.ds;
4724bb170a5SPaolo Bonzini 		svm->vmcb->save.cpl = vmcb12->save.cpl;
4734bb170a5SPaolo Bonzini 		vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
4748173396eSCathy Avery 	}
4754bb170a5SPaolo Bonzini 
4768173396eSCathy Avery 	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
4770dd16b5bSMaxim Levitsky 		svm->vmcb->save.gdtr = vmcb12->save.gdtr;
4780dd16b5bSMaxim Levitsky 		svm->vmcb->save.idtr = vmcb12->save.idtr;
4794bb170a5SPaolo Bonzini 		vmcb_mark_dirty(svm->vmcb, VMCB_DT);
4808173396eSCathy Avery 	}
4814bb170a5SPaolo Bonzini 
4828cce12b3SPaolo Bonzini 	kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
4833c346c0cSPaolo Bonzini 
4843c346c0cSPaolo Bonzini 	/*
4853c346c0cSPaolo Bonzini 	 * Force-set EFER_SVME even though it is checked earlier on the
4863c346c0cSPaolo Bonzini 	 * VMCB12, because the guest can flip the bit between the check
4873c346c0cSPaolo Bonzini 	 * and now.  Clearing EFER_SVME would call svm_free_nested.
4883c346c0cSPaolo Bonzini 	 */
4893c346c0cSPaolo Bonzini 	svm_set_efer(&svm->vcpu, vmcb12->save.efer | EFER_SVME);
4903c346c0cSPaolo Bonzini 
4910dd16b5bSMaxim Levitsky 	svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
4920dd16b5bSMaxim Levitsky 	svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
4934bb170a5SPaolo Bonzini 
4944bb170a5SPaolo Bonzini 	svm->vcpu.arch.cr2 = vmcb12->save.cr2;
4958173396eSCathy Avery 
4960dd16b5bSMaxim Levitsky 	kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
4970dd16b5bSMaxim Levitsky 	kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
4980dd16b5bSMaxim Levitsky 	kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
499883b0a91SJoerg Roedel 
500883b0a91SJoerg Roedel 	/* In case we don't even reach vcpu_run, the fields are not updated */
5010dd16b5bSMaxim Levitsky 	svm->vmcb->save.rax = vmcb12->save.rax;
5020dd16b5bSMaxim Levitsky 	svm->vmcb->save.rsp = vmcb12->save.rsp;
5030dd16b5bSMaxim Levitsky 	svm->vmcb->save.rip = vmcb12->save.rip;
5044bb170a5SPaolo Bonzini 
5058173396eSCathy Avery 	/* These bits will be set properly on the first execution when new_vmc12 is true */
5068173396eSCathy Avery 	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
5078cce12b3SPaolo Bonzini 		svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
5089a3ecd5eSChenyi Qiang 		svm->vcpu.arch.dr6  = vmcb12->save.dr6 | DR6_ACTIVE_LOW;
5094bb170a5SPaolo Bonzini 		vmcb_mark_dirty(svm->vmcb, VMCB_DR);
510f241d711SPaolo Bonzini 	}
5118173396eSCathy Avery }
512883b0a91SJoerg Roedel 
5139e8f0fbfSPaolo Bonzini static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
514f241d711SPaolo Bonzini {
5150f923e07SMaxim Levitsky 	const u32 int_ctl_vmcb01_bits =
5160f923e07SMaxim Levitsky 		V_INTR_MASKING_MASK | V_GIF_MASK | V_GIF_ENABLE_MASK;
5170f923e07SMaxim Levitsky 
5180f923e07SMaxim Levitsky 	const u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
5190f923e07SMaxim Levitsky 
520d2e56019SSean Christopherson 	struct kvm_vcpu *vcpu = &svm->vcpu;
52162156f6cSVitaly Kuznetsov 
5227c3ecfcdSPaolo Bonzini 	/*
5237c3ecfcdSPaolo Bonzini 	 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
5247c3ecfcdSPaolo Bonzini 	 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
5257c3ecfcdSPaolo Bonzini 	 */
5264995a368SCathy Avery 
5277c3ecfcdSPaolo Bonzini 	/*
5287c3ecfcdSPaolo Bonzini 	 * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
5297c3ecfcdSPaolo Bonzini 	 * avic_physical_id.
5307c3ecfcdSPaolo Bonzini 	 */
531feea0136SMaxim Levitsky 	WARN_ON(kvm_apicv_activated(svm->vcpu.kvm));
5327c3ecfcdSPaolo Bonzini 
5337c3ecfcdSPaolo Bonzini 	/* Copied from vmcb01.  msrpm_base can be overwritten later.  */
5347c3ecfcdSPaolo Bonzini 	svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
5357c3ecfcdSPaolo Bonzini 	svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
5367c3ecfcdSPaolo Bonzini 	svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
5377c3ecfcdSPaolo Bonzini 
5387c3ecfcdSPaolo Bonzini 	/* Done at vmrun: asid.  */
5397c3ecfcdSPaolo Bonzini 
5407c3ecfcdSPaolo Bonzini 	/* Also overwritten later if necessary.  */
5417c3ecfcdSPaolo Bonzini 	svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5427c3ecfcdSPaolo Bonzini 
5437c3ecfcdSPaolo Bonzini 	/* nested_cr3.  */
54462156f6cSVitaly Kuznetsov 	if (nested_npt_enabled(svm))
545d2e56019SSean Christopherson 		nested_svm_init_mmu_context(vcpu);
54669cb8774SPaolo Bonzini 
5475228eb96SMaxim Levitsky 	vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
5485228eb96SMaxim Levitsky 			vcpu->arch.l1_tsc_offset,
5495228eb96SMaxim Levitsky 			svm->nested.ctl.tsc_offset,
5505228eb96SMaxim Levitsky 			svm->tsc_ratio_msr);
5515228eb96SMaxim Levitsky 
5525228eb96SMaxim Levitsky 	svm->vmcb->control.tsc_offset = vcpu->arch.tsc_offset;
5535228eb96SMaxim Levitsky 
5545228eb96SMaxim Levitsky 	if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
5555228eb96SMaxim Levitsky 		WARN_ON(!svm->tsc_scaling_enabled);
5565228eb96SMaxim Levitsky 		nested_svm_update_tsc_ratio_msr(vcpu);
5575228eb96SMaxim Levitsky 	}
558883b0a91SJoerg Roedel 
55991b7130cSPaolo Bonzini 	svm->vmcb->control.int_ctl             =
5600f923e07SMaxim Levitsky 		(svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
5610f923e07SMaxim Levitsky 		(svm->vmcb01.ptr->control.int_ctl & int_ctl_vmcb01_bits);
56291b7130cSPaolo Bonzini 
563e670bf68SPaolo Bonzini 	svm->vmcb->control.int_vector          = svm->nested.ctl.int_vector;
564e670bf68SPaolo Bonzini 	svm->vmcb->control.int_state           = svm->nested.ctl.int_state;
565e670bf68SPaolo Bonzini 	svm->vmcb->control.event_inj           = svm->nested.ctl.event_inj;
566e670bf68SPaolo Bonzini 	svm->vmcb->control.event_inj_err       = svm->nested.ctl.event_inj_err;
567883b0a91SJoerg Roedel 
568d2e56019SSean Christopherson 	nested_svm_transition_tlb_flush(vcpu);
569d2e56019SSean Christopherson 
570883b0a91SJoerg Roedel 	/* Enter Guest-Mode */
571d2e56019SSean Christopherson 	enter_guest_mode(vcpu);
572883b0a91SJoerg Roedel 
573883b0a91SJoerg Roedel 	/*
574883b0a91SJoerg Roedel 	 * Merge guest and host intercepts - must be called with vcpu in
5754bb170a5SPaolo Bonzini 	 * guest-mode to take effect.
576883b0a91SJoerg Roedel 	 */
577883b0a91SJoerg Roedel 	recalc_intercepts(svm);
578f241d711SPaolo Bonzini }
579f241d711SPaolo Bonzini 
580d00b99c5SBabu Moger static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
581d00b99c5SBabu Moger {
582d00b99c5SBabu Moger 	/*
583d00b99c5SBabu Moger 	 * Some VMCB state is shared between L1 and L2 and thus has to be
584d00b99c5SBabu Moger 	 * moved at the time of nested vmrun and vmexit.
585d00b99c5SBabu Moger 	 *
586d00b99c5SBabu Moger 	 * VMLOAD/VMSAVE state would also belong in this category, but KVM
587d00b99c5SBabu Moger 	 * always performs VMLOAD and VMSAVE from the VMCB01.
588d00b99c5SBabu Moger 	 */
589d00b99c5SBabu Moger 	to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
590d00b99c5SBabu Moger }
591d00b99c5SBabu Moger 
59263129754SPaolo Bonzini int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
593e85d3e7bSMaxim Levitsky 			 struct vmcb *vmcb12, bool from_vmrun)
594f241d711SPaolo Bonzini {
59563129754SPaolo Bonzini 	struct vcpu_svm *svm = to_svm(vcpu);
596a506fdd2SVitaly Kuznetsov 	int ret;
597a506fdd2SVitaly Kuznetsov 
598954f419bSMaxim Levitsky 	trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
599954f419bSMaxim Levitsky 			       vmcb12->save.rip,
600954f419bSMaxim Levitsky 			       vmcb12->control.int_ctl,
601954f419bSMaxim Levitsky 			       vmcb12->control.event_inj,
602954f419bSMaxim Levitsky 			       vmcb12->control.nested_ctl);
603954f419bSMaxim Levitsky 
604954f419bSMaxim Levitsky 	trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
605954f419bSMaxim Levitsky 				    vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
606954f419bSMaxim Levitsky 				    vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
607954f419bSMaxim Levitsky 				    vmcb12->control.intercepts[INTERCEPT_WORD3],
608954f419bSMaxim Levitsky 				    vmcb12->control.intercepts[INTERCEPT_WORD4],
609954f419bSMaxim Levitsky 				    vmcb12->control.intercepts[INTERCEPT_WORD5]);
610954f419bSMaxim Levitsky 
611954f419bSMaxim Levitsky 
6120dd16b5bSMaxim Levitsky 	svm->nested.vmcb12_gpa = vmcb12_gpa;
6134995a368SCathy Avery 
6144995a368SCathy Avery 	WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
6154995a368SCathy Avery 
616d00b99c5SBabu Moger 	nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
6174995a368SCathy Avery 
6184995a368SCathy Avery 	svm_switch_vmcb(svm, &svm->nested.vmcb02);
6199e8f0fbfSPaolo Bonzini 	nested_vmcb02_prepare_control(svm);
6209e8f0fbfSPaolo Bonzini 	nested_vmcb02_prepare_save(svm, vmcb12);
621f241d711SPaolo Bonzini 
6220dd16b5bSMaxim Levitsky 	ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
623e85d3e7bSMaxim Levitsky 				  nested_npt_enabled(svm), from_vmrun);
624a506fdd2SVitaly Kuznetsov 	if (ret)
625a506fdd2SVitaly Kuznetsov 		return ret;
626a506fdd2SVitaly Kuznetsov 
627a04aead1SPaolo Bonzini 	if (!npt_enabled)
62863129754SPaolo Bonzini 		vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
629a04aead1SPaolo Bonzini 
630e85d3e7bSMaxim Levitsky 	if (!from_vmrun)
631e85d3e7bSMaxim Levitsky 		kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
632e85d3e7bSMaxim Levitsky 
633ffdf7f9eSPaolo Bonzini 	svm_set_gif(svm, true);
63459cd9bc5SVitaly Kuznetsov 
63559cd9bc5SVitaly Kuznetsov 	return 0;
636883b0a91SJoerg Roedel }
637883b0a91SJoerg Roedel 
63863129754SPaolo Bonzini int nested_svm_vmrun(struct kvm_vcpu *vcpu)
639883b0a91SJoerg Roedel {
64063129754SPaolo Bonzini 	struct vcpu_svm *svm = to_svm(vcpu);
641883b0a91SJoerg Roedel 	int ret;
6420dd16b5bSMaxim Levitsky 	struct vmcb *vmcb12;
643883b0a91SJoerg Roedel 	struct kvm_host_map map;
6440dd16b5bSMaxim Levitsky 	u64 vmcb12_gpa;
645883b0a91SJoerg Roedel 
646fb79f566SVitaly Kuznetsov 	if (!svm->nested.hsave_msr) {
647fb79f566SVitaly Kuznetsov 		kvm_inject_gp(vcpu, 0);
648fb79f566SVitaly Kuznetsov 		return 1;
649fb79f566SVitaly Kuznetsov 	}
650fb79f566SVitaly Kuznetsov 
65163129754SPaolo Bonzini 	if (is_smm(vcpu)) {
65263129754SPaolo Bonzini 		kvm_queue_exception(vcpu, UD_VECTOR);
6537c67f546SPaolo Bonzini 		return 1;
6547c67f546SPaolo Bonzini 	}
655883b0a91SJoerg Roedel 
6560dd16b5bSMaxim Levitsky 	vmcb12_gpa = svm->vmcb->save.rax;
65763129754SPaolo Bonzini 	ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
658883b0a91SJoerg Roedel 	if (ret == -EINVAL) {
65963129754SPaolo Bonzini 		kvm_inject_gp(vcpu, 0);
660883b0a91SJoerg Roedel 		return 1;
661883b0a91SJoerg Roedel 	} else if (ret) {
66263129754SPaolo Bonzini 		return kvm_skip_emulated_instruction(vcpu);
663883b0a91SJoerg Roedel 	}
664883b0a91SJoerg Roedel 
66563129754SPaolo Bonzini 	ret = kvm_skip_emulated_instruction(vcpu);
666883b0a91SJoerg Roedel 
6670dd16b5bSMaxim Levitsky 	vmcb12 = map.hva;
668883b0a91SJoerg Roedel 
6692fcf4876SMaxim Levitsky 	if (WARN_ON_ONCE(!svm->nested.initialized))
6702fcf4876SMaxim Levitsky 		return -EINVAL;
6712fcf4876SMaxim Levitsky 
672cb9b6a1bSPaolo Bonzini 	nested_load_control_from_vmcb12(svm, &vmcb12->control);
673cb9b6a1bSPaolo Bonzini 
674cb9b6a1bSPaolo Bonzini 	if (!nested_vmcb_valid_sregs(vcpu, &vmcb12->save) ||
675ee695f22SKrish Sadhukhan 	    !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) {
6760dd16b5bSMaxim Levitsky 		vmcb12->control.exit_code    = SVM_EXIT_ERR;
6770dd16b5bSMaxim Levitsky 		vmcb12->control.exit_code_hi = 0;
6780dd16b5bSMaxim Levitsky 		vmcb12->control.exit_info_1  = 0;
6790dd16b5bSMaxim Levitsky 		vmcb12->control.exit_info_2  = 0;
68069c9dfa2SPaolo Bonzini 		goto out;
681883b0a91SJoerg Roedel 	}
682883b0a91SJoerg Roedel 
683883b0a91SJoerg Roedel 	/*
6844995a368SCathy Avery 	 * Since vmcb01 is not in use, we can use it to store some of the L1
6854995a368SCathy Avery 	 * state.
686883b0a91SJoerg Roedel 	 */
68763129754SPaolo Bonzini 	svm->vmcb01.ptr->save.efer   = vcpu->arch.efer;
68863129754SPaolo Bonzini 	svm->vmcb01.ptr->save.cr0    = kvm_read_cr0(vcpu);
68963129754SPaolo Bonzini 	svm->vmcb01.ptr->save.cr4    = vcpu->arch.cr4;
69063129754SPaolo Bonzini 	svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
69163129754SPaolo Bonzini 	svm->vmcb01.ptr->save.rip    = kvm_rip_read(vcpu);
692883b0a91SJoerg Roedel 
6934995a368SCathy Avery 	if (!npt_enabled)
69463129754SPaolo Bonzini 		svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
695883b0a91SJoerg Roedel 
696f74f9414SPaolo Bonzini 	svm->nested.nested_run_pending = 1;
697883b0a91SJoerg Roedel 
698e85d3e7bSMaxim Levitsky 	if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
69959cd9bc5SVitaly Kuznetsov 		goto out_exit_err;
70059cd9bc5SVitaly Kuznetsov 
70159cd9bc5SVitaly Kuznetsov 	if (nested_svm_vmrun_msrpm(svm))
70259cd9bc5SVitaly Kuznetsov 		goto out;
70359cd9bc5SVitaly Kuznetsov 
70459cd9bc5SVitaly Kuznetsov out_exit_err:
705ebdb3dbaSVitaly Kuznetsov 	svm->nested.nested_run_pending = 0;
706ebdb3dbaSVitaly Kuznetsov 
707883b0a91SJoerg Roedel 	svm->vmcb->control.exit_code    = SVM_EXIT_ERR;
708883b0a91SJoerg Roedel 	svm->vmcb->control.exit_code_hi = 0;
709883b0a91SJoerg Roedel 	svm->vmcb->control.exit_info_1  = 0;
710883b0a91SJoerg Roedel 	svm->vmcb->control.exit_info_2  = 0;
711883b0a91SJoerg Roedel 
712883b0a91SJoerg Roedel 	nested_svm_vmexit(svm);
713883b0a91SJoerg Roedel 
71469c9dfa2SPaolo Bonzini out:
71563129754SPaolo Bonzini 	kvm_vcpu_unmap(vcpu, &map, true);
71669c9dfa2SPaolo Bonzini 
717883b0a91SJoerg Roedel 	return ret;
718883b0a91SJoerg Roedel }
719883b0a91SJoerg Roedel 
7200a758290SVitaly Kuznetsov /* Copy state save area fields which are handled by VMRUN */
7212bb16beaSVitaly Kuznetsov void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
7222bb16beaSVitaly Kuznetsov 			  struct vmcb_save_area *from_save)
7230a758290SVitaly Kuznetsov {
7240a758290SVitaly Kuznetsov 	to_save->es = from_save->es;
7250a758290SVitaly Kuznetsov 	to_save->cs = from_save->cs;
7260a758290SVitaly Kuznetsov 	to_save->ss = from_save->ss;
7270a758290SVitaly Kuznetsov 	to_save->ds = from_save->ds;
7280a758290SVitaly Kuznetsov 	to_save->gdtr = from_save->gdtr;
7290a758290SVitaly Kuznetsov 	to_save->idtr = from_save->idtr;
7300a758290SVitaly Kuznetsov 	to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
7310a758290SVitaly Kuznetsov 	to_save->efer = from_save->efer;
7320a758290SVitaly Kuznetsov 	to_save->cr0 = from_save->cr0;
7330a758290SVitaly Kuznetsov 	to_save->cr3 = from_save->cr3;
7340a758290SVitaly Kuznetsov 	to_save->cr4 = from_save->cr4;
7350a758290SVitaly Kuznetsov 	to_save->rax = from_save->rax;
7360a758290SVitaly Kuznetsov 	to_save->rsp = from_save->rsp;
7370a758290SVitaly Kuznetsov 	to_save->rip = from_save->rip;
7380a758290SVitaly Kuznetsov 	to_save->cpl = 0;
7390a758290SVitaly Kuznetsov }
7400a758290SVitaly Kuznetsov 
7412bb16beaSVitaly Kuznetsov void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
742883b0a91SJoerg Roedel {
743883b0a91SJoerg Roedel 	to_vmcb->save.fs = from_vmcb->save.fs;
744883b0a91SJoerg Roedel 	to_vmcb->save.gs = from_vmcb->save.gs;
745883b0a91SJoerg Roedel 	to_vmcb->save.tr = from_vmcb->save.tr;
746883b0a91SJoerg Roedel 	to_vmcb->save.ldtr = from_vmcb->save.ldtr;
747883b0a91SJoerg Roedel 	to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
748883b0a91SJoerg Roedel 	to_vmcb->save.star = from_vmcb->save.star;
749883b0a91SJoerg Roedel 	to_vmcb->save.lstar = from_vmcb->save.lstar;
750883b0a91SJoerg Roedel 	to_vmcb->save.cstar = from_vmcb->save.cstar;
751883b0a91SJoerg Roedel 	to_vmcb->save.sfmask = from_vmcb->save.sfmask;
752883b0a91SJoerg Roedel 	to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
753883b0a91SJoerg Roedel 	to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
754883b0a91SJoerg Roedel 	to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
755883b0a91SJoerg Roedel }
756883b0a91SJoerg Roedel 
757883b0a91SJoerg Roedel int nested_svm_vmexit(struct vcpu_svm *svm)
758883b0a91SJoerg Roedel {
75963129754SPaolo Bonzini 	struct kvm_vcpu *vcpu = &svm->vcpu;
7600dd16b5bSMaxim Levitsky 	struct vmcb *vmcb12;
761883b0a91SJoerg Roedel 	struct vmcb *vmcb = svm->vmcb;
762883b0a91SJoerg Roedel 	struct kvm_host_map map;
76363129754SPaolo Bonzini 	int rc;
764883b0a91SJoerg Roedel 
765cb6a32c2SSean Christopherson 	/* Triple faults in L2 should never escape. */
766cb6a32c2SSean Christopherson 	WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
767cb6a32c2SSean Christopherson 
76863129754SPaolo Bonzini 	rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
769883b0a91SJoerg Roedel 	if (rc) {
770883b0a91SJoerg Roedel 		if (rc == -EINVAL)
77163129754SPaolo Bonzini 			kvm_inject_gp(vcpu, 0);
772883b0a91SJoerg Roedel 		return 1;
773883b0a91SJoerg Roedel 	}
774883b0a91SJoerg Roedel 
7750dd16b5bSMaxim Levitsky 	vmcb12 = map.hva;
776883b0a91SJoerg Roedel 
777883b0a91SJoerg Roedel 	/* Exit Guest-Mode */
77863129754SPaolo Bonzini 	leave_guest_mode(vcpu);
7790dd16b5bSMaxim Levitsky 	svm->nested.vmcb12_gpa = 0;
7802d8a42beSPaolo Bonzini 	WARN_ON_ONCE(svm->nested.nested_run_pending);
781883b0a91SJoerg Roedel 
78263129754SPaolo Bonzini 	kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
783f2c7ef3bSMaxim Levitsky 
78438c0b192SPaolo Bonzini 	/* in case we halted in L2 */
78538c0b192SPaolo Bonzini 	svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
78638c0b192SPaolo Bonzini 
787883b0a91SJoerg Roedel 	/* Give the current vmcb to the guest */
788883b0a91SJoerg Roedel 
7890dd16b5bSMaxim Levitsky 	vmcb12->save.es     = vmcb->save.es;
7900dd16b5bSMaxim Levitsky 	vmcb12->save.cs     = vmcb->save.cs;
7910dd16b5bSMaxim Levitsky 	vmcb12->save.ss     = vmcb->save.ss;
7920dd16b5bSMaxim Levitsky 	vmcb12->save.ds     = vmcb->save.ds;
7930dd16b5bSMaxim Levitsky 	vmcb12->save.gdtr   = vmcb->save.gdtr;
7940dd16b5bSMaxim Levitsky 	vmcb12->save.idtr   = vmcb->save.idtr;
7950dd16b5bSMaxim Levitsky 	vmcb12->save.efer   = svm->vcpu.arch.efer;
79663129754SPaolo Bonzini 	vmcb12->save.cr0    = kvm_read_cr0(vcpu);
79763129754SPaolo Bonzini 	vmcb12->save.cr3    = kvm_read_cr3(vcpu);
7980dd16b5bSMaxim Levitsky 	vmcb12->save.cr2    = vmcb->save.cr2;
7990dd16b5bSMaxim Levitsky 	vmcb12->save.cr4    = svm->vcpu.arch.cr4;
80063129754SPaolo Bonzini 	vmcb12->save.rflags = kvm_get_rflags(vcpu);
80163129754SPaolo Bonzini 	vmcb12->save.rip    = kvm_rip_read(vcpu);
80263129754SPaolo Bonzini 	vmcb12->save.rsp    = kvm_rsp_read(vcpu);
80363129754SPaolo Bonzini 	vmcb12->save.rax    = kvm_rax_read(vcpu);
8040dd16b5bSMaxim Levitsky 	vmcb12->save.dr7    = vmcb->save.dr7;
8050dd16b5bSMaxim Levitsky 	vmcb12->save.dr6    = svm->vcpu.arch.dr6;
8060dd16b5bSMaxim Levitsky 	vmcb12->save.cpl    = vmcb->save.cpl;
807883b0a91SJoerg Roedel 
8080dd16b5bSMaxim Levitsky 	vmcb12->control.int_state         = vmcb->control.int_state;
8090dd16b5bSMaxim Levitsky 	vmcb12->control.exit_code         = vmcb->control.exit_code;
8100dd16b5bSMaxim Levitsky 	vmcb12->control.exit_code_hi      = vmcb->control.exit_code_hi;
8110dd16b5bSMaxim Levitsky 	vmcb12->control.exit_info_1       = vmcb->control.exit_info_1;
8120dd16b5bSMaxim Levitsky 	vmcb12->control.exit_info_2       = vmcb->control.exit_info_2;
81336e2e983SPaolo Bonzini 
8140dd16b5bSMaxim Levitsky 	if (vmcb12->control.exit_code != SVM_EXIT_ERR)
8159e8f0fbfSPaolo Bonzini 		nested_save_pending_event_to_vmcb12(svm, vmcb12);
816883b0a91SJoerg Roedel 
817883b0a91SJoerg Roedel 	if (svm->nrips_enabled)
8180dd16b5bSMaxim Levitsky 		vmcb12->control.next_rip  = vmcb->control.next_rip;
819883b0a91SJoerg Roedel 
8200dd16b5bSMaxim Levitsky 	vmcb12->control.int_ctl           = svm->nested.ctl.int_ctl;
8210dd16b5bSMaxim Levitsky 	vmcb12->control.tlb_ctl           = svm->nested.ctl.tlb_ctl;
8220dd16b5bSMaxim Levitsky 	vmcb12->control.event_inj         = svm->nested.ctl.event_inj;
8230dd16b5bSMaxim Levitsky 	vmcb12->control.event_inj_err     = svm->nested.ctl.event_inj_err;
824883b0a91SJoerg Roedel 
825d00b99c5SBabu Moger 	nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
826d00b99c5SBabu Moger 
8274995a368SCathy Avery 	svm_switch_vmcb(svm, &svm->vmcb01);
8284995a368SCathy Avery 
8294995a368SCathy Avery 	/*
8304995a368SCathy Avery 	 * On vmexit the  GIF is set to false and
8314995a368SCathy Avery 	 * no event can be injected in L1.
8324995a368SCathy Avery 	 */
8339883764aSMaxim Levitsky 	svm_set_gif(svm, false);
8344995a368SCathy Avery 	svm->vmcb->control.exit_int_info = 0;
8359883764aSMaxim Levitsky 
8367ca62d13SPaolo Bonzini 	svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
8377ca62d13SPaolo Bonzini 	if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
8387ca62d13SPaolo Bonzini 		svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
8397ca62d13SPaolo Bonzini 		vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
8407ca62d13SPaolo Bonzini 	}
84118fc6c55SPaolo Bonzini 
8425228eb96SMaxim Levitsky 	if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
8435228eb96SMaxim Levitsky 		WARN_ON(!svm->tsc_scaling_enabled);
8445228eb96SMaxim Levitsky 		vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
8455228eb96SMaxim Levitsky 		svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio);
8465228eb96SMaxim Levitsky 	}
8475228eb96SMaxim Levitsky 
848e670bf68SPaolo Bonzini 	svm->nested.ctl.nested_cr3 = 0;
849883b0a91SJoerg Roedel 
8504995a368SCathy Avery 	/*
8514995a368SCathy Avery 	 * Restore processor state that had been saved in vmcb01
8524995a368SCathy Avery 	 */
85363129754SPaolo Bonzini 	kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
85463129754SPaolo Bonzini 	svm_set_efer(vcpu, svm->vmcb->save.efer);
85563129754SPaolo Bonzini 	svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
85663129754SPaolo Bonzini 	svm_set_cr4(vcpu, svm->vmcb->save.cr4);
85763129754SPaolo Bonzini 	kvm_rax_write(vcpu, svm->vmcb->save.rax);
85863129754SPaolo Bonzini 	kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
85963129754SPaolo Bonzini 	kvm_rip_write(vcpu, svm->vmcb->save.rip);
8604995a368SCathy Avery 
8614995a368SCathy Avery 	svm->vcpu.arch.dr7 = DR7_FIXED_1;
8624995a368SCathy Avery 	kvm_update_dr7(&svm->vcpu);
863883b0a91SJoerg Roedel 
8640dd16b5bSMaxim Levitsky 	trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
8650dd16b5bSMaxim Levitsky 				       vmcb12->control.exit_info_1,
8660dd16b5bSMaxim Levitsky 				       vmcb12->control.exit_info_2,
8670dd16b5bSMaxim Levitsky 				       vmcb12->control.exit_int_info,
8680dd16b5bSMaxim Levitsky 				       vmcb12->control.exit_int_info_err,
86936e2e983SPaolo Bonzini 				       KVM_ISA_SVM);
87036e2e983SPaolo Bonzini 
87163129754SPaolo Bonzini 	kvm_vcpu_unmap(vcpu, &map, true);
872883b0a91SJoerg Roedel 
873d2e56019SSean Christopherson 	nested_svm_transition_tlb_flush(vcpu);
874d2e56019SSean Christopherson 
87563129754SPaolo Bonzini 	nested_svm_uninit_mmu_context(vcpu);
876bf7dea42SVitaly Kuznetsov 
877b222b0b8SMaxim Levitsky 	rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false, true);
878d82aaef9SVitaly Kuznetsov 	if (rc)
879d82aaef9SVitaly Kuznetsov 		return 1;
880bf7dea42SVitaly Kuznetsov 
881883b0a91SJoerg Roedel 	/*
882883b0a91SJoerg Roedel 	 * Drop what we picked up for L2 via svm_complete_interrupts() so it
883883b0a91SJoerg Roedel 	 * doesn't end up in L1.
884883b0a91SJoerg Roedel 	 */
885883b0a91SJoerg Roedel 	svm->vcpu.arch.nmi_injected = false;
88663129754SPaolo Bonzini 	kvm_clear_exception_queue(vcpu);
88763129754SPaolo Bonzini 	kvm_clear_interrupt_queue(vcpu);
888883b0a91SJoerg Roedel 
8899a7de6ecSKrish Sadhukhan 	/*
8909a7de6ecSKrish Sadhukhan 	 * If we are here following the completion of a VMRUN that
8919a7de6ecSKrish Sadhukhan 	 * is being single-stepped, queue the pending #DB intercept
8929a7de6ecSKrish Sadhukhan 	 * right now so that it an be accounted for before we execute
8939a7de6ecSKrish Sadhukhan 	 * L1's next instruction.
8949a7de6ecSKrish Sadhukhan 	 */
8959a7de6ecSKrish Sadhukhan 	if (unlikely(svm->vmcb->save.rflags & X86_EFLAGS_TF))
8969a7de6ecSKrish Sadhukhan 		kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
8979a7de6ecSKrish Sadhukhan 
898883b0a91SJoerg Roedel 	return 0;
899883b0a91SJoerg Roedel }
900883b0a91SJoerg Roedel 
901cb6a32c2SSean Christopherson static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
902cb6a32c2SSean Christopherson {
9033a87c7e0SSean Christopherson 	nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
904cb6a32c2SSean Christopherson }
905cb6a32c2SSean Christopherson 
9062fcf4876SMaxim Levitsky int svm_allocate_nested(struct vcpu_svm *svm)
9072fcf4876SMaxim Levitsky {
9084995a368SCathy Avery 	struct page *vmcb02_page;
9092fcf4876SMaxim Levitsky 
9102fcf4876SMaxim Levitsky 	if (svm->nested.initialized)
9112fcf4876SMaxim Levitsky 		return 0;
9122fcf4876SMaxim Levitsky 
9134995a368SCathy Avery 	vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
9144995a368SCathy Avery 	if (!vmcb02_page)
9152fcf4876SMaxim Levitsky 		return -ENOMEM;
9164995a368SCathy Avery 	svm->nested.vmcb02.ptr = page_address(vmcb02_page);
9174995a368SCathy Avery 	svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
9182fcf4876SMaxim Levitsky 
9192fcf4876SMaxim Levitsky 	svm->nested.msrpm = svm_vcpu_alloc_msrpm();
9202fcf4876SMaxim Levitsky 	if (!svm->nested.msrpm)
9214995a368SCathy Avery 		goto err_free_vmcb02;
9222fcf4876SMaxim Levitsky 	svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
9232fcf4876SMaxim Levitsky 
9242fcf4876SMaxim Levitsky 	svm->nested.initialized = true;
9252fcf4876SMaxim Levitsky 	return 0;
9262fcf4876SMaxim Levitsky 
9274995a368SCathy Avery err_free_vmcb02:
9284995a368SCathy Avery 	__free_page(vmcb02_page);
9292fcf4876SMaxim Levitsky 	return -ENOMEM;
9302fcf4876SMaxim Levitsky }
9312fcf4876SMaxim Levitsky 
9322fcf4876SMaxim Levitsky void svm_free_nested(struct vcpu_svm *svm)
9332fcf4876SMaxim Levitsky {
9342fcf4876SMaxim Levitsky 	if (!svm->nested.initialized)
9352fcf4876SMaxim Levitsky 		return;
9362fcf4876SMaxim Levitsky 
9372fcf4876SMaxim Levitsky 	svm_vcpu_free_msrpm(svm->nested.msrpm);
9382fcf4876SMaxim Levitsky 	svm->nested.msrpm = NULL;
9392fcf4876SMaxim Levitsky 
9404995a368SCathy Avery 	__free_page(virt_to_page(svm->nested.vmcb02.ptr));
9414995a368SCathy Avery 	svm->nested.vmcb02.ptr = NULL;
9422fcf4876SMaxim Levitsky 
943c74ad08fSMaxim Levitsky 	/*
944c74ad08fSMaxim Levitsky 	 * When last_vmcb12_gpa matches the current vmcb12 gpa,
945c74ad08fSMaxim Levitsky 	 * some vmcb12 fields are not loaded if they are marked clean
946c74ad08fSMaxim Levitsky 	 * in the vmcb12, since in this case they are up to date already.
947c74ad08fSMaxim Levitsky 	 *
948c74ad08fSMaxim Levitsky 	 * When the vmcb02 is freed, this optimization becomes invalid.
949c74ad08fSMaxim Levitsky 	 */
950c74ad08fSMaxim Levitsky 	svm->nested.last_vmcb12_gpa = INVALID_GPA;
951c74ad08fSMaxim Levitsky 
9522fcf4876SMaxim Levitsky 	svm->nested.initialized = false;
9532fcf4876SMaxim Levitsky }
9542fcf4876SMaxim Levitsky 
955c513f484SPaolo Bonzini /*
956c513f484SPaolo Bonzini  * Forcibly leave nested mode in order to be able to reset the VCPU later on.
957c513f484SPaolo Bonzini  */
958c513f484SPaolo Bonzini void svm_leave_nested(struct vcpu_svm *svm)
959c513f484SPaolo Bonzini {
96063129754SPaolo Bonzini 	struct kvm_vcpu *vcpu = &svm->vcpu;
96163129754SPaolo Bonzini 
96263129754SPaolo Bonzini 	if (is_guest_mode(vcpu)) {
963c513f484SPaolo Bonzini 		svm->nested.nested_run_pending = 0;
964c74ad08fSMaxim Levitsky 		svm->nested.vmcb12_gpa = INVALID_GPA;
965c74ad08fSMaxim Levitsky 
96663129754SPaolo Bonzini 		leave_guest_mode(vcpu);
9674995a368SCathy Avery 
968deee59baSMaxim Levitsky 		svm_switch_vmcb(svm, &svm->vmcb01);
9694995a368SCathy Avery 
97063129754SPaolo Bonzini 		nested_svm_uninit_mmu_context(vcpu);
97156fe28deSMaxim Levitsky 		vmcb_mark_all_dirty(svm->vmcb);
972c513f484SPaolo Bonzini 	}
973a7d5c7ceSPaolo Bonzini 
97463129754SPaolo Bonzini 	kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
975c513f484SPaolo Bonzini }
976c513f484SPaolo Bonzini 
977883b0a91SJoerg Roedel static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
978883b0a91SJoerg Roedel {
979883b0a91SJoerg Roedel 	u32 offset, msr, value;
980883b0a91SJoerg Roedel 	int write, mask;
981883b0a91SJoerg Roedel 
982c62e2e94SBabu Moger 	if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
983883b0a91SJoerg Roedel 		return NESTED_EXIT_HOST;
984883b0a91SJoerg Roedel 
985883b0a91SJoerg Roedel 	msr    = svm->vcpu.arch.regs[VCPU_REGS_RCX];
986883b0a91SJoerg Roedel 	offset = svm_msrpm_offset(msr);
987883b0a91SJoerg Roedel 	write  = svm->vmcb->control.exit_info_1 & 1;
988883b0a91SJoerg Roedel 	mask   = 1 << ((2 * (msr & 0xf)) + write);
989883b0a91SJoerg Roedel 
990883b0a91SJoerg Roedel 	if (offset == MSR_INVALID)
991883b0a91SJoerg Roedel 		return NESTED_EXIT_DONE;
992883b0a91SJoerg Roedel 
993883b0a91SJoerg Roedel 	/* Offset is in 32 bit units but need in 8 bit units */
994883b0a91SJoerg Roedel 	offset *= 4;
995883b0a91SJoerg Roedel 
996e670bf68SPaolo Bonzini 	if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
997883b0a91SJoerg Roedel 		return NESTED_EXIT_DONE;
998883b0a91SJoerg Roedel 
999883b0a91SJoerg Roedel 	return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1000883b0a91SJoerg Roedel }
1001883b0a91SJoerg Roedel 
1002883b0a91SJoerg Roedel static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1003883b0a91SJoerg Roedel {
1004883b0a91SJoerg Roedel 	unsigned port, size, iopm_len;
1005883b0a91SJoerg Roedel 	u16 val, mask;
1006883b0a91SJoerg Roedel 	u8 start_bit;
1007883b0a91SJoerg Roedel 	u64 gpa;
1008883b0a91SJoerg Roedel 
1009c62e2e94SBabu Moger 	if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1010883b0a91SJoerg Roedel 		return NESTED_EXIT_HOST;
1011883b0a91SJoerg Roedel 
1012883b0a91SJoerg Roedel 	port = svm->vmcb->control.exit_info_1 >> 16;
1013883b0a91SJoerg Roedel 	size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1014883b0a91SJoerg Roedel 		SVM_IOIO_SIZE_SHIFT;
1015e670bf68SPaolo Bonzini 	gpa  = svm->nested.ctl.iopm_base_pa + (port / 8);
1016883b0a91SJoerg Roedel 	start_bit = port % 8;
1017883b0a91SJoerg Roedel 	iopm_len = (start_bit + size > 8) ? 2 : 1;
1018883b0a91SJoerg Roedel 	mask = (0xf >> (4 - size)) << start_bit;
1019883b0a91SJoerg Roedel 	val = 0;
1020883b0a91SJoerg Roedel 
1021883b0a91SJoerg Roedel 	if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1022883b0a91SJoerg Roedel 		return NESTED_EXIT_DONE;
1023883b0a91SJoerg Roedel 
1024883b0a91SJoerg Roedel 	return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1025883b0a91SJoerg Roedel }
1026883b0a91SJoerg Roedel 
1027883b0a91SJoerg Roedel static int nested_svm_intercept(struct vcpu_svm *svm)
1028883b0a91SJoerg Roedel {
1029883b0a91SJoerg Roedel 	u32 exit_code = svm->vmcb->control.exit_code;
1030883b0a91SJoerg Roedel 	int vmexit = NESTED_EXIT_HOST;
1031883b0a91SJoerg Roedel 
1032883b0a91SJoerg Roedel 	switch (exit_code) {
1033883b0a91SJoerg Roedel 	case SVM_EXIT_MSR:
1034883b0a91SJoerg Roedel 		vmexit = nested_svm_exit_handled_msr(svm);
1035883b0a91SJoerg Roedel 		break;
1036883b0a91SJoerg Roedel 	case SVM_EXIT_IOIO:
1037883b0a91SJoerg Roedel 		vmexit = nested_svm_intercept_ioio(svm);
1038883b0a91SJoerg Roedel 		break;
1039883b0a91SJoerg Roedel 	case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
104003bfeeb9SBabu Moger 		if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
1041883b0a91SJoerg Roedel 			vmexit = NESTED_EXIT_DONE;
1042883b0a91SJoerg Roedel 		break;
1043883b0a91SJoerg Roedel 	}
1044883b0a91SJoerg Roedel 	case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
104530abaa88SBabu Moger 		if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
1046883b0a91SJoerg Roedel 			vmexit = NESTED_EXIT_DONE;
1047883b0a91SJoerg Roedel 		break;
1048883b0a91SJoerg Roedel 	}
1049883b0a91SJoerg Roedel 	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
10507c86663bSPaolo Bonzini 		/*
10517c86663bSPaolo Bonzini 		 * Host-intercepted exceptions have been checked already in
10527c86663bSPaolo Bonzini 		 * nested_svm_exit_special.  There is nothing to do here,
10537c86663bSPaolo Bonzini 		 * the vmexit is injected by svm_check_nested_events.
10547c86663bSPaolo Bonzini 		 */
1055883b0a91SJoerg Roedel 		vmexit = NESTED_EXIT_DONE;
1056883b0a91SJoerg Roedel 		break;
1057883b0a91SJoerg Roedel 	}
1058883b0a91SJoerg Roedel 	case SVM_EXIT_ERR: {
1059883b0a91SJoerg Roedel 		vmexit = NESTED_EXIT_DONE;
1060883b0a91SJoerg Roedel 		break;
1061883b0a91SJoerg Roedel 	}
1062883b0a91SJoerg Roedel 	default: {
1063c62e2e94SBabu Moger 		if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
1064883b0a91SJoerg Roedel 			vmexit = NESTED_EXIT_DONE;
1065883b0a91SJoerg Roedel 	}
1066883b0a91SJoerg Roedel 	}
1067883b0a91SJoerg Roedel 
1068883b0a91SJoerg Roedel 	return vmexit;
1069883b0a91SJoerg Roedel }
1070883b0a91SJoerg Roedel 
1071883b0a91SJoerg Roedel int nested_svm_exit_handled(struct vcpu_svm *svm)
1072883b0a91SJoerg Roedel {
1073883b0a91SJoerg Roedel 	int vmexit;
1074883b0a91SJoerg Roedel 
1075883b0a91SJoerg Roedel 	vmexit = nested_svm_intercept(svm);
1076883b0a91SJoerg Roedel 
1077883b0a91SJoerg Roedel 	if (vmexit == NESTED_EXIT_DONE)
1078883b0a91SJoerg Roedel 		nested_svm_vmexit(svm);
1079883b0a91SJoerg Roedel 
1080883b0a91SJoerg Roedel 	return vmexit;
1081883b0a91SJoerg Roedel }
1082883b0a91SJoerg Roedel 
108363129754SPaolo Bonzini int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1084883b0a91SJoerg Roedel {
108563129754SPaolo Bonzini 	if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
108663129754SPaolo Bonzini 		kvm_queue_exception(vcpu, UD_VECTOR);
1087883b0a91SJoerg Roedel 		return 1;
1088883b0a91SJoerg Roedel 	}
1089883b0a91SJoerg Roedel 
109063129754SPaolo Bonzini 	if (to_svm(vcpu)->vmcb->save.cpl) {
109163129754SPaolo Bonzini 		kvm_inject_gp(vcpu, 0);
1092883b0a91SJoerg Roedel 		return 1;
1093883b0a91SJoerg Roedel 	}
1094883b0a91SJoerg Roedel 
1095883b0a91SJoerg Roedel 	return 0;
1096883b0a91SJoerg Roedel }
1097883b0a91SJoerg Roedel 
10987c86663bSPaolo Bonzini static bool nested_exit_on_exception(struct vcpu_svm *svm)
1099883b0a91SJoerg Roedel {
11007c86663bSPaolo Bonzini 	unsigned int nr = svm->vcpu.arch.exception.nr;
1101883b0a91SJoerg Roedel 
11029780d51dSBabu Moger 	return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
11037c86663bSPaolo Bonzini }
1104883b0a91SJoerg Roedel 
11057c86663bSPaolo Bonzini static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
11067c86663bSPaolo Bonzini {
11077c86663bSPaolo Bonzini 	unsigned int nr = svm->vcpu.arch.exception.nr;
1108883b0a91SJoerg Roedel 
1109883b0a91SJoerg Roedel 	svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1110883b0a91SJoerg Roedel 	svm->vmcb->control.exit_code_hi = 0;
11117c86663bSPaolo Bonzini 
11127c86663bSPaolo Bonzini 	if (svm->vcpu.arch.exception.has_error_code)
11137c86663bSPaolo Bonzini 		svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
1114883b0a91SJoerg Roedel 
1115883b0a91SJoerg Roedel 	/*
1116883b0a91SJoerg Roedel 	 * EXITINFO2 is undefined for all exception intercepts other
1117883b0a91SJoerg Roedel 	 * than #PF.
1118883b0a91SJoerg Roedel 	 */
11197c86663bSPaolo Bonzini 	if (nr == PF_VECTOR) {
1120883b0a91SJoerg Roedel 		if (svm->vcpu.arch.exception.nested_apf)
1121883b0a91SJoerg Roedel 			svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1122883b0a91SJoerg Roedel 		else if (svm->vcpu.arch.exception.has_payload)
1123883b0a91SJoerg Roedel 			svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1124883b0a91SJoerg Roedel 		else
1125883b0a91SJoerg Roedel 			svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
11267c86663bSPaolo Bonzini 	} else if (nr == DB_VECTOR) {
11277c86663bSPaolo Bonzini 		/* See inject_pending_event.  */
11287c86663bSPaolo Bonzini 		kvm_deliver_exception_payload(&svm->vcpu);
11297c86663bSPaolo Bonzini 		if (svm->vcpu.arch.dr7 & DR7_GD) {
11307c86663bSPaolo Bonzini 			svm->vcpu.arch.dr7 &= ~DR7_GD;
11317c86663bSPaolo Bonzini 			kvm_update_dr7(&svm->vcpu);
11327c86663bSPaolo Bonzini 		}
11337c86663bSPaolo Bonzini 	} else
11347c86663bSPaolo Bonzini 		WARN_ON(svm->vcpu.arch.exception.has_payload);
1135883b0a91SJoerg Roedel 
11367c86663bSPaolo Bonzini 	nested_svm_vmexit(svm);
1137883b0a91SJoerg Roedel }
1138883b0a91SJoerg Roedel 
11395b672408SPaolo Bonzini static inline bool nested_exit_on_init(struct vcpu_svm *svm)
11405b672408SPaolo Bonzini {
1141c62e2e94SBabu Moger 	return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
11425b672408SPaolo Bonzini }
11435b672408SPaolo Bonzini 
114433b22172SPaolo Bonzini static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1145883b0a91SJoerg Roedel {
1146883b0a91SJoerg Roedel 	struct vcpu_svm *svm = to_svm(vcpu);
1147883b0a91SJoerg Roedel 	bool block_nested_events =
1148bd279629SPaolo Bonzini 		kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
11495b672408SPaolo Bonzini 	struct kvm_lapic *apic = vcpu->arch.apic;
11505b672408SPaolo Bonzini 
11515b672408SPaolo Bonzini 	if (lapic_in_kernel(vcpu) &&
11525b672408SPaolo Bonzini 	    test_bit(KVM_APIC_INIT, &apic->pending_events)) {
11535b672408SPaolo Bonzini 		if (block_nested_events)
11545b672408SPaolo Bonzini 			return -EBUSY;
11555b672408SPaolo Bonzini 		if (!nested_exit_on_init(svm))
11565b672408SPaolo Bonzini 			return 0;
11573a87c7e0SSean Christopherson 		nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
11585b672408SPaolo Bonzini 		return 0;
11595b672408SPaolo Bonzini 	}
1160883b0a91SJoerg Roedel 
11617c86663bSPaolo Bonzini 	if (vcpu->arch.exception.pending) {
11624020da3bSMaxim Levitsky 		/*
11634020da3bSMaxim Levitsky 		 * Only a pending nested run can block a pending exception.
11644020da3bSMaxim Levitsky 		 * Otherwise an injected NMI/interrupt should either be
11654020da3bSMaxim Levitsky 		 * lost or delivered to the nested hypervisor in the EXITINTINFO
11664020da3bSMaxim Levitsky 		 * vmcb field, while delivering the pending exception.
11674020da3bSMaxim Levitsky 		 */
11684020da3bSMaxim Levitsky 		if (svm->nested.nested_run_pending)
11697c86663bSPaolo Bonzini                         return -EBUSY;
11707c86663bSPaolo Bonzini 		if (!nested_exit_on_exception(svm))
11717c86663bSPaolo Bonzini 			return 0;
11727c86663bSPaolo Bonzini 		nested_svm_inject_exception_vmexit(svm);
11737c86663bSPaolo Bonzini 		return 0;
11747c86663bSPaolo Bonzini 	}
11757c86663bSPaolo Bonzini 
1176221e7610SPaolo Bonzini 	if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
117755714cddSPaolo Bonzini 		if (block_nested_events)
117855714cddSPaolo Bonzini 			return -EBUSY;
1179221e7610SPaolo Bonzini 		if (!nested_exit_on_smi(svm))
1180221e7610SPaolo Bonzini 			return 0;
11813a87c7e0SSean Christopherson 		nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
118255714cddSPaolo Bonzini 		return 0;
118355714cddSPaolo Bonzini 	}
118455714cddSPaolo Bonzini 
1185221e7610SPaolo Bonzini 	if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
11869c3d370aSCathy Avery 		if (block_nested_events)
11879c3d370aSCathy Avery 			return -EBUSY;
1188221e7610SPaolo Bonzini 		if (!nested_exit_on_nmi(svm))
1189221e7610SPaolo Bonzini 			return 0;
11903a87c7e0SSean Christopherson 		nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
11919c3d370aSCathy Avery 		return 0;
11929c3d370aSCathy Avery 	}
11939c3d370aSCathy Avery 
1194221e7610SPaolo Bonzini 	if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1195883b0a91SJoerg Roedel 		if (block_nested_events)
1196883b0a91SJoerg Roedel 			return -EBUSY;
1197221e7610SPaolo Bonzini 		if (!nested_exit_on_intr(svm))
1198221e7610SPaolo Bonzini 			return 0;
11993a87c7e0SSean Christopherson 		trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
12003a87c7e0SSean Christopherson 		nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1201883b0a91SJoerg Roedel 		return 0;
1202883b0a91SJoerg Roedel 	}
1203883b0a91SJoerg Roedel 
1204883b0a91SJoerg Roedel 	return 0;
1205883b0a91SJoerg Roedel }
1206883b0a91SJoerg Roedel 
1207883b0a91SJoerg Roedel int nested_svm_exit_special(struct vcpu_svm *svm)
1208883b0a91SJoerg Roedel {
1209883b0a91SJoerg Roedel 	u32 exit_code = svm->vmcb->control.exit_code;
1210883b0a91SJoerg Roedel 
1211883b0a91SJoerg Roedel 	switch (exit_code) {
1212883b0a91SJoerg Roedel 	case SVM_EXIT_INTR:
1213883b0a91SJoerg Roedel 	case SVM_EXIT_NMI:
1214883b0a91SJoerg Roedel 	case SVM_EXIT_NPF:
1215883b0a91SJoerg Roedel 		return NESTED_EXIT_HOST;
12167c86663bSPaolo Bonzini 	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
12177c86663bSPaolo Bonzini 		u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
12187c86663bSPaolo Bonzini 
12194995a368SCathy Avery 		if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
12209780d51dSBabu Moger 		    excp_bits)
12217c86663bSPaolo Bonzini 			return NESTED_EXIT_HOST;
12227c86663bSPaolo Bonzini 		else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
122368fd66f1SVitaly Kuznetsov 			 svm->vcpu.arch.apf.host_apf_flags)
1224a3535be7SPaolo Bonzini 			/* Trap async PF even if not shadowing */
1225883b0a91SJoerg Roedel 			return NESTED_EXIT_HOST;
1226883b0a91SJoerg Roedel 		break;
12277c86663bSPaolo Bonzini 	}
1228883b0a91SJoerg Roedel 	default:
1229883b0a91SJoerg Roedel 		break;
1230883b0a91SJoerg Roedel 	}
1231883b0a91SJoerg Roedel 
1232883b0a91SJoerg Roedel 	return NESTED_EXIT_CONTINUE;
1233883b0a91SJoerg Roedel }
123433b22172SPaolo Bonzini 
12355228eb96SMaxim Levitsky void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
12365228eb96SMaxim Levitsky {
12375228eb96SMaxim Levitsky 	struct vcpu_svm *svm = to_svm(vcpu);
12385228eb96SMaxim Levitsky 
12395228eb96SMaxim Levitsky 	vcpu->arch.tsc_scaling_ratio =
12405228eb96SMaxim Levitsky 		kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
12415228eb96SMaxim Levitsky 					       svm->tsc_ratio_msr);
12425228eb96SMaxim Levitsky 	svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio);
12435228eb96SMaxim Levitsky }
12445228eb96SMaxim Levitsky 
1245cc440cdaSPaolo Bonzini static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1246cc440cdaSPaolo Bonzini 				struct kvm_nested_state __user *user_kvm_nested_state,
1247cc440cdaSPaolo Bonzini 				u32 user_data_size)
1248cc440cdaSPaolo Bonzini {
1249cc440cdaSPaolo Bonzini 	struct vcpu_svm *svm;
1250cc440cdaSPaolo Bonzini 	struct kvm_nested_state kvm_state = {
1251cc440cdaSPaolo Bonzini 		.flags = 0,
1252cc440cdaSPaolo Bonzini 		.format = KVM_STATE_NESTED_FORMAT_SVM,
1253cc440cdaSPaolo Bonzini 		.size = sizeof(kvm_state),
1254cc440cdaSPaolo Bonzini 	};
1255cc440cdaSPaolo Bonzini 	struct vmcb __user *user_vmcb = (struct vmcb __user *)
1256cc440cdaSPaolo Bonzini 		&user_kvm_nested_state->data.svm[0];
1257cc440cdaSPaolo Bonzini 
1258cc440cdaSPaolo Bonzini 	if (!vcpu)
1259cc440cdaSPaolo Bonzini 		return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1260cc440cdaSPaolo Bonzini 
1261cc440cdaSPaolo Bonzini 	svm = to_svm(vcpu);
1262cc440cdaSPaolo Bonzini 
1263cc440cdaSPaolo Bonzini 	if (user_data_size < kvm_state.size)
1264cc440cdaSPaolo Bonzini 		goto out;
1265cc440cdaSPaolo Bonzini 
1266cc440cdaSPaolo Bonzini 	/* First fill in the header and copy it out.  */
1267cc440cdaSPaolo Bonzini 	if (is_guest_mode(vcpu)) {
12680dd16b5bSMaxim Levitsky 		kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1269cc440cdaSPaolo Bonzini 		kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1270cc440cdaSPaolo Bonzini 		kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1271cc440cdaSPaolo Bonzini 
1272cc440cdaSPaolo Bonzini 		if (svm->nested.nested_run_pending)
1273cc440cdaSPaolo Bonzini 			kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1274cc440cdaSPaolo Bonzini 	}
1275cc440cdaSPaolo Bonzini 
1276cc440cdaSPaolo Bonzini 	if (gif_set(svm))
1277cc440cdaSPaolo Bonzini 		kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1278cc440cdaSPaolo Bonzini 
1279cc440cdaSPaolo Bonzini 	if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1280cc440cdaSPaolo Bonzini 		return -EFAULT;
1281cc440cdaSPaolo Bonzini 
1282cc440cdaSPaolo Bonzini 	if (!is_guest_mode(vcpu))
1283cc440cdaSPaolo Bonzini 		goto out;
1284cc440cdaSPaolo Bonzini 
1285cc440cdaSPaolo Bonzini 	/*
1286cc440cdaSPaolo Bonzini 	 * Copy over the full size of the VMCB rather than just the size
1287cc440cdaSPaolo Bonzini 	 * of the structs.
1288cc440cdaSPaolo Bonzini 	 */
1289cc440cdaSPaolo Bonzini 	if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1290cc440cdaSPaolo Bonzini 		return -EFAULT;
1291cc440cdaSPaolo Bonzini 	if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1292cc440cdaSPaolo Bonzini 			 sizeof(user_vmcb->control)))
1293cc440cdaSPaolo Bonzini 		return -EFAULT;
12944995a368SCathy Avery 	if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1295cc440cdaSPaolo Bonzini 			 sizeof(user_vmcb->save)))
1296cc440cdaSPaolo Bonzini 		return -EFAULT;
1297cc440cdaSPaolo Bonzini out:
1298cc440cdaSPaolo Bonzini 	return kvm_state.size;
1299cc440cdaSPaolo Bonzini }
1300cc440cdaSPaolo Bonzini 
1301cc440cdaSPaolo Bonzini static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1302cc440cdaSPaolo Bonzini 				struct kvm_nested_state __user *user_kvm_nested_state,
1303cc440cdaSPaolo Bonzini 				struct kvm_nested_state *kvm_state)
1304cc440cdaSPaolo Bonzini {
1305cc440cdaSPaolo Bonzini 	struct vcpu_svm *svm = to_svm(vcpu);
1306cc440cdaSPaolo Bonzini 	struct vmcb __user *user_vmcb = (struct vmcb __user *)
1307cc440cdaSPaolo Bonzini 		&user_kvm_nested_state->data.svm[0];
13086ccbd29aSJoerg Roedel 	struct vmcb_control_area *ctl;
13096ccbd29aSJoerg Roedel 	struct vmcb_save_area *save;
1310dbc4739bSSean Christopherson 	unsigned long cr0;
13116ccbd29aSJoerg Roedel 	int ret;
1312cc440cdaSPaolo Bonzini 
13136ccbd29aSJoerg Roedel 	BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
13146ccbd29aSJoerg Roedel 		     KVM_STATE_NESTED_SVM_VMCB_SIZE);
13156ccbd29aSJoerg Roedel 
1316cc440cdaSPaolo Bonzini 	if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1317cc440cdaSPaolo Bonzini 		return -EINVAL;
1318cc440cdaSPaolo Bonzini 
1319cc440cdaSPaolo Bonzini 	if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1320cc440cdaSPaolo Bonzini 				 KVM_STATE_NESTED_RUN_PENDING |
1321cc440cdaSPaolo Bonzini 				 KVM_STATE_NESTED_GIF_SET))
1322cc440cdaSPaolo Bonzini 		return -EINVAL;
1323cc440cdaSPaolo Bonzini 
1324cc440cdaSPaolo Bonzini 	/*
1325cc440cdaSPaolo Bonzini 	 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1326cc440cdaSPaolo Bonzini 	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1327cc440cdaSPaolo Bonzini 	 */
1328cc440cdaSPaolo Bonzini 	if (!(vcpu->arch.efer & EFER_SVME)) {
1329cc440cdaSPaolo Bonzini 		/* GIF=1 and no guest mode are required if SVME=0.  */
1330cc440cdaSPaolo Bonzini 		if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1331cc440cdaSPaolo Bonzini 			return -EINVAL;
1332cc440cdaSPaolo Bonzini 	}
1333cc440cdaSPaolo Bonzini 
1334cc440cdaSPaolo Bonzini 	/* SMM temporarily disables SVM, so we cannot be in guest mode.  */
1335cc440cdaSPaolo Bonzini 	if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1336cc440cdaSPaolo Bonzini 		return -EINVAL;
1337cc440cdaSPaolo Bonzini 
1338cc440cdaSPaolo Bonzini 	if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1339cc440cdaSPaolo Bonzini 		svm_leave_nested(svm);
1340d5cd6f34SVitaly Kuznetsov 		svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1341d5cd6f34SVitaly Kuznetsov 		return 0;
1342cc440cdaSPaolo Bonzini 	}
1343cc440cdaSPaolo Bonzini 
1344cc440cdaSPaolo Bonzini 	if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1345cc440cdaSPaolo Bonzini 		return -EINVAL;
1346cc440cdaSPaolo Bonzini 	if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1347cc440cdaSPaolo Bonzini 		return -EINVAL;
1348cc440cdaSPaolo Bonzini 
13496ccbd29aSJoerg Roedel 	ret  = -ENOMEM;
1350eba04b20SSean Christopherson 	ctl  = kzalloc(sizeof(*ctl),  GFP_KERNEL_ACCOUNT);
1351eba04b20SSean Christopherson 	save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
13526ccbd29aSJoerg Roedel 	if (!ctl || !save)
13536ccbd29aSJoerg Roedel 		goto out_free;
13546ccbd29aSJoerg Roedel 
13556ccbd29aSJoerg Roedel 	ret = -EFAULT;
13566ccbd29aSJoerg Roedel 	if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
13576ccbd29aSJoerg Roedel 		goto out_free;
13586ccbd29aSJoerg Roedel 	if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
13596ccbd29aSJoerg Roedel 		goto out_free;
13606ccbd29aSJoerg Roedel 
13616ccbd29aSJoerg Roedel 	ret = -EINVAL;
1362ee695f22SKrish Sadhukhan 	if (!nested_vmcb_check_controls(vcpu, ctl))
13636ccbd29aSJoerg Roedel 		goto out_free;
1364cc440cdaSPaolo Bonzini 
1365cc440cdaSPaolo Bonzini 	/*
1366cc440cdaSPaolo Bonzini 	 * Processor state contains L2 state.  Check that it is
1367cb9b6a1bSPaolo Bonzini 	 * valid for guest mode (see nested_vmcb_check_save).
1368cc440cdaSPaolo Bonzini 	 */
1369cc440cdaSPaolo Bonzini 	cr0 = kvm_read_cr0(vcpu);
1370cc440cdaSPaolo Bonzini         if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
13716ccbd29aSJoerg Roedel 		goto out_free;
1372cc440cdaSPaolo Bonzini 
1373cc440cdaSPaolo Bonzini 	/*
1374cc440cdaSPaolo Bonzini 	 * Validate host state saved from before VMRUN (see
1375cc440cdaSPaolo Bonzini 	 * nested_svm_check_permissions).
1376cc440cdaSPaolo Bonzini 	 */
13776906e06dSKrish Sadhukhan 	if (!(save->cr0 & X86_CR0_PG) ||
13786906e06dSKrish Sadhukhan 	    !(save->cr0 & X86_CR0_PE) ||
13796906e06dSKrish Sadhukhan 	    (save->rflags & X86_EFLAGS_VM) ||
138063129754SPaolo Bonzini 	    !nested_vmcb_valid_sregs(vcpu, save))
13816ccbd29aSJoerg Roedel 		goto out_free;
1382cc440cdaSPaolo Bonzini 
1383cc440cdaSPaolo Bonzini 	/*
1384b222b0b8SMaxim Levitsky 	 * While the nested guest CR3 is already checked and set by
1385b222b0b8SMaxim Levitsky 	 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1386b222b0b8SMaxim Levitsky 	 * thus MMU might not be initialized correctly.
1387b222b0b8SMaxim Levitsky 	 * Set it again to fix this.
1388b222b0b8SMaxim Levitsky 	 */
1389b222b0b8SMaxim Levitsky 
1390b222b0b8SMaxim Levitsky 	ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1391b222b0b8SMaxim Levitsky 				  nested_npt_enabled(svm), false);
1392b222b0b8SMaxim Levitsky 	if (WARN_ON_ONCE(ret))
1393b222b0b8SMaxim Levitsky 		goto out_free;
1394b222b0b8SMaxim Levitsky 
1395b222b0b8SMaxim Levitsky 
1396b222b0b8SMaxim Levitsky 	/*
13974995a368SCathy Avery 	 * All checks done, we can enter guest mode. Userspace provides
13984995a368SCathy Avery 	 * vmcb12.control, which will be combined with L1 and stored into
13994995a368SCathy Avery 	 * vmcb02, and the L1 save state which we store in vmcb01.
14004995a368SCathy Avery 	 * L2 registers if needed are moved from the current VMCB to VMCB02.
1401cc440cdaSPaolo Bonzini 	 */
140281f76adaSMaxim Levitsky 
14039d290e16SMaxim Levitsky 	if (is_guest_mode(vcpu))
14049d290e16SMaxim Levitsky 		svm_leave_nested(svm);
14059d290e16SMaxim Levitsky 	else
14069d290e16SMaxim Levitsky 		svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
14079d290e16SMaxim Levitsky 
1408063ab16cSMaxim Levitsky 	svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1409063ab16cSMaxim Levitsky 
141081f76adaSMaxim Levitsky 	svm->nested.nested_run_pending =
141181f76adaSMaxim Levitsky 		!!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
141281f76adaSMaxim Levitsky 
14130dd16b5bSMaxim Levitsky 	svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1414c08f390aSPaolo Bonzini 
14152bb16beaSVitaly Kuznetsov 	svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
14169e8f0fbfSPaolo Bonzini 	nested_load_control_from_vmcb12(svm, ctl);
14174995a368SCathy Avery 
14184995a368SCathy Avery 	svm_switch_vmcb(svm, &svm->nested.vmcb02);
14199e8f0fbfSPaolo Bonzini 	nested_vmcb02_prepare_control(svm);
1420a7d5c7ceSPaolo Bonzini 	kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
14216ccbd29aSJoerg Roedel 	ret = 0;
14226ccbd29aSJoerg Roedel out_free:
14236ccbd29aSJoerg Roedel 	kfree(save);
14246ccbd29aSJoerg Roedel 	kfree(ctl);
14256ccbd29aSJoerg Roedel 
14266ccbd29aSJoerg Roedel 	return ret;
1427cc440cdaSPaolo Bonzini }
1428cc440cdaSPaolo Bonzini 
1429232f75d3SMaxim Levitsky static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1430232f75d3SMaxim Levitsky {
1431232f75d3SMaxim Levitsky 	struct vcpu_svm *svm = to_svm(vcpu);
1432232f75d3SMaxim Levitsky 
1433232f75d3SMaxim Levitsky 	if (WARN_ON(!is_guest_mode(vcpu)))
1434232f75d3SMaxim Levitsky 		return true;
1435232f75d3SMaxim Levitsky 
1436158a48ecSMaxim Levitsky 	if (!vcpu->arch.pdptrs_from_userspace &&
1437158a48ecSMaxim Levitsky 	    !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1438b222b0b8SMaxim Levitsky 		/*
1439b222b0b8SMaxim Levitsky 		 * Reload the guest's PDPTRs since after a migration
1440b222b0b8SMaxim Levitsky 		 * the guest CR3 might be restored prior to setting the nested
1441b222b0b8SMaxim Levitsky 		 * state which can lead to a load of wrong PDPTRs.
1442b222b0b8SMaxim Levitsky 		 */
1443b222b0b8SMaxim Levitsky 		if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
1444232f75d3SMaxim Levitsky 			return false;
1445232f75d3SMaxim Levitsky 
1446232f75d3SMaxim Levitsky 	if (!nested_svm_vmrun_msrpm(svm)) {
1447232f75d3SMaxim Levitsky 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1448232f75d3SMaxim Levitsky 		vcpu->run->internal.suberror =
1449232f75d3SMaxim Levitsky 			KVM_INTERNAL_ERROR_EMULATION;
1450232f75d3SMaxim Levitsky 		vcpu->run->internal.ndata = 0;
1451232f75d3SMaxim Levitsky 		return false;
1452232f75d3SMaxim Levitsky 	}
1453232f75d3SMaxim Levitsky 
1454232f75d3SMaxim Levitsky 	return true;
1455232f75d3SMaxim Levitsky }
1456232f75d3SMaxim Levitsky 
145733b22172SPaolo Bonzini struct kvm_x86_nested_ops svm_nested_ops = {
145833b22172SPaolo Bonzini 	.check_events = svm_check_nested_events,
1459cb6a32c2SSean Christopherson 	.triple_fault = nested_svm_triple_fault,
1460a7d5c7ceSPaolo Bonzini 	.get_nested_state_pages = svm_get_nested_state_pages,
1461cc440cdaSPaolo Bonzini 	.get_state = svm_get_nested_state,
1462cc440cdaSPaolo Bonzini 	.set_state = svm_set_nested_state,
146333b22172SPaolo Bonzini };
1464