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