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