nested.c 47.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Kernel-based Virtual Machine driver for Linux
 *
 * AMD SVM support
 *
 * Copyright (C) 2006 Qumranet, Inc.
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * Authors:
 *   Yaniv Kamay  <yaniv@qumranet.com>
 *   Avi Kivity   <avi@qumranet.com>
 */

#define pr_fmt(fmt) "SVM: " fmt

#include <linux/kvm_types.h>
#include <linux/kvm_host.h>
#include <linux/kernel.h>

#include <asm/msr-index.h>
22
#include <asm/debugreg.h>
23 24 25 26 27

#include "kvm_emulate.h"
#include "trace.h"
#include "mmu.h"
#include "x86.h"
28
#include "cpuid.h"
29
#include "lapic.h"
30
#include "svm.h"
31
#include "hyperv.h"
32

33 34
#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK

35 36 37 38
static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
				       struct x86_exception *fault)
{
	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 44 45
		/*
		 * TODO: track the cause of the nested page fault, and
		 * correctly fill in the high bits of exit_info_1.
		 */
46 47 48 49
		vmcb->control.exit_code = SVM_EXIT_NPF;
		vmcb->control.exit_code_hi = 0;
		vmcb->control.exit_info_1 = (1ULL << 32);
		vmcb->control.exit_info_2 = fault->address;
50 51
	}

52 53
	vmcb->control.exit_info_1 &= ~0xffffffffULL;
	vmcb->control.exit_info_1 |= fault->error_code;
54 55 56 57

	nested_svm_vmexit(svm);
}

58 59
static bool nested_svm_handle_page_fault_workaround(struct kvm_vcpu *vcpu,
						    struct x86_exception *fault)
60
{
61 62 63
	struct vcpu_svm *svm = to_svm(vcpu);
	struct vmcb *vmcb = svm->vmcb;

64
 	WARN_ON(!is_guest_mode(vcpu));
65

66 67
	if (vmcb12_is_intercept(&svm->nested.ctl,
				INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
68 69
	    !WARN_ON_ONCE(svm->nested.nested_run_pending)) {
	     	vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
70 71 72 73
		vmcb->control.exit_code_hi = 0;
		vmcb->control.exit_info_1 = fault->error_code;
		vmcb->control.exit_info_2 = fault->address;
		nested_svm_vmexit(svm);
74
		return true;
75
	}
76 77

	return false;
78 79
}

80 81 82
static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
{
	struct vcpu_svm *svm = to_svm(vcpu);
83
	u64 cr3 = svm->nested.ctl.nested_cr3;
84 85 86
	u64 pdpte;
	int ret;

87
	ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
88 89 90 91 92 93 94 95 96 97
				       offset_in_page(cr3) + index * 8, 8);
	if (ret)
		return 0;
	return pdpte;
}

static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
{
	struct vcpu_svm *svm = to_svm(vcpu);

98
	return svm->nested.ctl.nested_cr3;
99 100 101 102
}

static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
{
103 104
	struct vcpu_svm *svm = to_svm(vcpu);

105 106 107
	WARN_ON(mmu_is_nested(vcpu));

	vcpu->arch.mmu = &vcpu->arch.guest_mmu;
108 109 110 111 112 113

	/*
	 * The NPT format depends on L1's CR4 and EFER, which is in vmcb01.  Note,
	 * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
	 * vCPU state.  CR0.WP is explicitly ignored, while CR0.PG is required.
	 */
114 115
	kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
				svm->vmcb01.ptr->save.efer,
116
				svm->nested.ctl.nested_cr3);
117 118 119 120 121 122 123 124 125 126 127 128
	vcpu->arch.mmu->get_guest_pgd     = nested_svm_get_tdp_cr3;
	vcpu->arch.mmu->get_pdptr         = nested_svm_get_tdp_pdptr;
	vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
	vcpu->arch.walk_mmu              = &vcpu->arch.nested_mmu;
}

static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
{
	vcpu->arch.mmu = &vcpu->arch.root_mmu;
	vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142
static bool nested_vmcb_needs_vls_intercept(struct vcpu_svm *svm)
{
	if (!svm->v_vmload_vmsave_enabled)
		return true;

	if (!nested_npt_enabled(svm))
		return true;

	if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK))
		return true;

	return false;
}

143 144
void recalc_intercepts(struct vcpu_svm *svm)
{
145 146
	struct vmcb_control_area *c, *h;
	struct vmcb_ctrl_area_cached *g;
147
	unsigned int i;
148

149
	vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
150 151 152 153 154

	if (!is_guest_mode(&svm->vcpu))
		return;

	c = &svm->vmcb->control;
155
	h = &svm->vmcb01.ptr->control;
156
	g = &svm->nested.ctl;
157

158 159 160
	for (i = 0; i < MAX_INTERCEPT; i++)
		c->intercepts[i] = h->intercepts[i];

P
Paolo Bonzini 已提交
161
	if (g->int_ctl & V_INTR_MASKING_MASK) {
162
		/* We only want the cr8 intercept bits of L1 */
163 164
		vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
		vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
165 166 167 168 169 170

		/*
		 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
		 * affect any interrupt we may want to inject; therefore,
		 * interrupt window vmexits are irrelevant to L0.
		 */
171
		vmcb_clr_intercept(c, INTERCEPT_VINTR);
172 173 174
	}

	/* We don't want to see VMMCALLs from a nested guest */
175
	vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
176

177 178
	for (i = 0; i < MAX_INTERCEPT; i++)
		c->intercepts[i] |= g->intercepts[i];
179 180 181 182

	/* If SMI is not intercepted, ignore guest SMI intercept as well  */
	if (!intercept_smi)
		vmcb_clr_intercept(c, INTERCEPT_SMI);
183

184 185 186 187 188 189 190 191 192 193 194
	if (nested_vmcb_needs_vls_intercept(svm)) {
		/*
		 * If the virtual VMLOAD/VMSAVE is not enabled for the L2,
		 * we must intercept these instructions to correctly
		 * emulate them in case L1 doesn't intercept them.
		 */
		vmcb_set_intercept(c, INTERCEPT_VMLOAD);
		vmcb_set_intercept(c, INTERCEPT_VMSAVE);
	} else {
		WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK));
	}
195 196
}

197 198 199 200 201
/*
 * Merge L0's (KVM) and L1's (Nested VMCB) MSR permission bitmaps. The function
 * is optimized in that it only merges the parts where KVM MSR permission bitmap
 * may contain zero bits.
 */
202 203
static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
{
204 205 206 207
	struct hv_enlightenments *hve =
		(struct hv_enlightenments *)svm->nested.ctl.reserved_sw;
	int i;

208
	/*
209 210 211 212 213 214
	 * MSR bitmap update can be skipped when:
	 * - MSR bitmap for L1 hasn't changed.
	 * - Nested hypervisor (L1) is attempting to launch the same L2 as
	 *   before.
	 * - Nested hypervisor (L1) is using Hyper-V emulation interface and
	 * tells KVM (L0) there were no changes in MSR bitmap for L2.
215
	 */
216 217 218 219 220
	if (!svm->nested.force_msr_bitmap_recalc &&
	    kvm_hv_hypercall_enabled(&svm->vcpu) &&
	    hve->hv_enlightenments_control.msr_bitmap &&
	    (svm->nested.ctl.clean & BIT(VMCB_HV_NESTED_ENLIGHTENMENTS)))
		goto set_msrpm_base_pa;
221

222
	if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
223 224 225 226 227 228 229 230 231 232
		return true;

	for (i = 0; i < MSRPM_OFFSETS; i++) {
		u32 value, p;
		u64 offset;

		if (msrpm_offsets[i] == 0xffffffff)
			break;

		p      = msrpm_offsets[i];
233
		offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
234 235 236 237 238 239 240

		if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
			return false;

		svm->nested.msrpm[p] = svm->msrpm[p] | value;
	}

241 242
	svm->nested.force_msr_bitmap_recalc = false;

243
set_msrpm_base_pa:
244 245 246 247 248
	svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));

	return true;
}

249 250 251 252 253 254 255 256 257 258 259
/*
 * Bits 11:0 of bitmap address are ignored by hardware
 */
static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
{
	u64 addr = PAGE_ALIGN(pa);

	return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
	    kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
}

260 261 262 263 264 265 266 267 268 269 270 271
static bool nested_svm_check_tlb_ctl(struct kvm_vcpu *vcpu, u8 tlb_ctl)
{
	/* Nested FLUSHBYASID is not supported yet.  */
	switch(tlb_ctl) {
		case TLB_CONTROL_DO_NOTHING:
		case TLB_CONTROL_FLUSH_ALL_ASID:
			return true;
		default:
			return false;
	}
}

272
static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
273
					 struct vmcb_ctrl_area_cached *control)
274
{
275
	if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
276 277
		return false;

278
	if (CC(control->asid == 0))
279 280
		return false;

281
	if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
282 283
		return false;

284 285 286 287 288 289 290
	if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
					   MSRPM_SIZE)))
		return false;
	if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
					   IOPM_SIZE)))
		return false;

291 292 293
	if (CC(!nested_svm_check_tlb_ctl(vcpu, control->tlb_ctl)))
		return false;

294 295 296
	return true;
}

297
/* Common checks that apply to both L1 and L2 state.  */
298 299
static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
				     struct vmcb_save_area_cached *save)
300
{
301
	if (CC(!(save->efer & EFER_SVME)))
302 303
		return false;

304 305
	if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
	    CC(save->cr0 & ~0xffffffffULL))
306 307
		return false;

308
	if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
309 310
		return false;

311 312 313 314 315 316 317 318 319 320 321 322 323
	/*
	 * These checks are also performed by KVM_SET_SREGS,
	 * except that EFER.LMA is not checked by SVM against
	 * CR0.PG && EFER.LME.
	 */
	if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
		if (CC(!(save->cr4 & X86_CR4_PAE)) ||
		    CC(!(save->cr0 & X86_CR0_PE)) ||
		    CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
			return false;
	}

	if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
324
		return false;
325

326
	if (CC(!kvm_valid_efer(vcpu, save->efer)))
327 328 329 330 331
		return false;

	return true;
}

332 333 334 335 336 337 338 339
static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu)
{
	struct vcpu_svm *svm = to_svm(vcpu);
	struct vmcb_save_area_cached *save = &svm->nested.save;

	return __nested_vmcb_check_save(vcpu, save);
}

340 341 342
static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
{
	struct vcpu_svm *svm = to_svm(vcpu);
343
	struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
344 345 346 347

	return __nested_vmcb_check_controls(vcpu, ctl);
}

348
static
349 350
void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
					 struct vmcb_ctrl_area_cached *to,
351
					 struct vmcb_control_area *from)
352
{
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	unsigned int i;

	for (i = 0; i < MAX_INTERCEPT; i++)
		to->intercepts[i] = from->intercepts[i];

	to->iopm_base_pa        = from->iopm_base_pa;
	to->msrpm_base_pa       = from->msrpm_base_pa;
	to->tsc_offset          = from->tsc_offset;
	to->tlb_ctl             = from->tlb_ctl;
	to->int_ctl             = from->int_ctl;
	to->int_vector          = from->int_vector;
	to->int_state           = from->int_state;
	to->exit_code           = from->exit_code;
	to->exit_code_hi        = from->exit_code_hi;
	to->exit_info_1         = from->exit_info_1;
	to->exit_info_2         = from->exit_info_2;
	to->exit_int_info       = from->exit_int_info;
	to->exit_int_info_err   = from->exit_int_info_err;
	to->nested_ctl          = from->nested_ctl;
	to->event_inj           = from->event_inj;
	to->event_inj_err       = from->event_inj_err;
374
	to->next_rip            = from->next_rip;
375 376 377 378 379 380 381 382 383
	to->nested_cr3          = from->nested_cr3;
	to->virt_ext            = from->virt_ext;
	to->pause_filter_count  = from->pause_filter_count;
	to->pause_filter_thresh = from->pause_filter_thresh;

	/* Copy asid here because nested_vmcb_check_controls will check it.  */
	to->asid           = from->asid;
	to->msrpm_base_pa &= ~0x0fffULL;
	to->iopm_base_pa  &= ~0x0fffULL;
384 385 386 387 388 389 390

	/* Hyper-V extensions (Enlightened VMCB) */
	if (kvm_hv_hypercall_enabled(vcpu)) {
		to->clean = from->clean;
		memcpy(to->reserved_sw, from->reserved_sw,
		       sizeof(struct hv_enlightenments));
	}
391
}
392

393 394 395
void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
				       struct vmcb_control_area *control)
{
396
	__nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control);
397 398
}

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
					     struct vmcb_save_area *from)
{
	/*
	 * Copy only fields that are validated, as we need them
	 * to avoid TOC/TOU races.
	 */
	to->efer = from->efer;
	to->cr0 = from->cr0;
	to->cr3 = from->cr3;
	to->cr4 = from->cr4;

	to->dr6 = from->dr6;
	to->dr7 = from->dr7;
}

void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
				    struct vmcb_save_area *save)
{
	__nested_copy_vmcb_save_to_cache(&svm->nested.save, save);
}

421 422
/*
 * Synchronize fields that are written by the processor, so that
423
 * they can be copied back into the vmcb12.
424
 */
425
void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
426 427 428 429 430 431 432 433
{
	u32 mask;
	svm->nested.ctl.event_inj      = svm->vmcb->control.event_inj;
	svm->nested.ctl.event_inj_err  = svm->vmcb->control.event_inj_err;

	/* Only a few fields of int_ctl are written by the processor.  */
	mask = V_IRQ_MASK | V_TPR_MASK;
	if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
434
	    svm_is_intercept(svm, INTERCEPT_VINTR)) {
435 436 437 438 439 440 441 442 443 444
		/*
		 * In order to request an interrupt window, L0 is usurping
		 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
		 * even if it was clear in L1's VMCB.  Restoring it would be
		 * wrong.  However, in this case V_IRQ will remain true until
		 * interrupt_window_interception calls svm_clear_vintr and
		 * restores int_ctl.  We can just leave it aside.
		 */
		mask &= ~V_IRQ_MASK;
	}
445 446 447 448

	if (nested_vgif_enabled(svm))
		mask |= V_GIF_MASK;

449 450 451 452
	svm->nested.ctl.int_ctl        &= ~mask;
	svm->nested.ctl.int_ctl        |= svm->vmcb->control.int_ctl & mask;
}

453 454 455 456
/*
 * Transfer any event that L0 or L1 wanted to inject into L2 to
 * EXIT_INT_INFO.
 */
457 458
static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
						struct vmcb *vmcb12)
459 460 461 462 463 464 465 466 467 468 469
{
	struct kvm_vcpu *vcpu = &svm->vcpu;
	u32 exit_int_info = 0;
	unsigned int nr;

	if (vcpu->arch.exception.injected) {
		nr = vcpu->arch.exception.nr;
		exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;

		if (vcpu->arch.exception.has_error_code) {
			exit_int_info |= SVM_EVTINJ_VALID_ERR;
470
			vmcb12->control.exit_int_info_err =
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
				vcpu->arch.exception.error_code;
		}

	} else if (vcpu->arch.nmi_injected) {
		exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;

	} else if (vcpu->arch.interrupt.injected) {
		nr = vcpu->arch.interrupt.nr;
		exit_int_info = nr | SVM_EVTINJ_VALID;

		if (vcpu->arch.interrupt.soft)
			exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
		else
			exit_int_info |= SVM_EVTINJ_TYPE_INTR;
	}

487
	vmcb12->control.exit_int_info = exit_int_info;
488 489
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
{
	/*
	 * TODO: optimize unconditional TLB flush/MMU sync.  A partial list of
	 * things to fix before this can be conditional:
	 *
	 *  - Flush TLBs for both L1 and L2 remote TLB flush
	 *  - Honor L1's request to flush an ASID on nested VMRUN
	 *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
	 *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
	 *  - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
	 *
	 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
	 *     NPT guest-physical mappings on VMRUN.
	 */
	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
}

509
/*
510 511
 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
 * if we are emulating VM-Entry into a guest with NPT enabled.
512 513
 */
static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
514
			       bool nested_npt, bool reload_pdptrs)
515
{
516
	if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
517 518
		return -EINVAL;

519
	if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
520
	    CC(!load_pdptrs(vcpu, cr3)))
521
		return -EINVAL;
522 523 524

	vcpu->arch.cr3 = cr3;

525
	/* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
526
	kvm_init_mmu(vcpu);
527

528 529 530
	if (!nested_npt)
		kvm_mmu_new_pgd(vcpu, cr3);

531
	return 0;
532 533
}

534 535 536 537 538 539 540 541 542
void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
{
	if (!svm->nested.vmcb02.ptr)
		return;

	/* FIXME: merge g_pat from vmcb01 and vmcb12.  */
	svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
}

543
static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
544
{
545
	bool new_vmcb12 = false;
546
	struct vmcb *vmcb01 = svm->vmcb01.ptr;
547
	struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
548

549 550
	nested_vmcb02_compute_g_pat(svm);

551
	/* Load the nested guest state */
552 553 554
	if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
		new_vmcb12 = true;
		svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
555
		svm->nested.force_msr_bitmap_recalc = true;
556 557 558
	}

	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
559 560 561 562 563 564
		vmcb02->save.es = vmcb12->save.es;
		vmcb02->save.cs = vmcb12->save.cs;
		vmcb02->save.ss = vmcb12->save.ss;
		vmcb02->save.ds = vmcb12->save.ds;
		vmcb02->save.cpl = vmcb12->save.cpl;
		vmcb_mark_dirty(vmcb02, VMCB_SEG);
565 566 567
	}

	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
568 569 570
		vmcb02->save.gdtr = vmcb12->save.gdtr;
		vmcb02->save.idtr = vmcb12->save.idtr;
		vmcb_mark_dirty(vmcb02, VMCB_DT);
571
	}
572

P
Paolo Bonzini 已提交
573
	kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
574

575
	svm_set_efer(&svm->vcpu, svm->nested.save.efer);
576

577 578
	svm_set_cr0(&svm->vcpu, svm->nested.save.cr0);
	svm_set_cr4(&svm->vcpu, svm->nested.save.cr4);
579 580

	svm->vcpu.arch.cr2 = vmcb12->save.cr2;
581

582 583 584
	kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
	kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
	kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
585 586

	/* In case we don't even reach vcpu_run, the fields are not updated */
587 588 589
	vmcb02->save.rax = vmcb12->save.rax;
	vmcb02->save.rsp = vmcb12->save.rsp;
	vmcb02->save.rip = vmcb12->save.rip;
590

591 592
	/* These bits will be set properly on the first execution when new_vmc12 is true */
	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
593
		vmcb02->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
594
		svm->vcpu.arch.dr6  = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
595
		vmcb_mark_dirty(vmcb02, VMCB_DR);
596
	}
597

598 599 600 601 602 603 604 605 606 607
	if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
		/*
		 * Reserved bits of DEBUGCTL are ignored.  Be consistent with
		 * svm_set_msr's definition of reserved bits.
		 */
		svm_copy_lbrs(vmcb02, vmcb12);
		vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
		svm_update_lbrv(&svm->vcpu);

	} else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
608
		svm_copy_lbrs(vmcb02, vmcb01);
609
	}
610
}
611

612 613 614 615 616 617 618 619
static inline bool is_evtinj_soft(u32 evtinj)
{
	u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
	u8 vector = evtinj & SVM_EVTINJ_VEC_MASK;

	if (!(evtinj & SVM_EVTINJ_VALID))
		return false;

620 621 622
	if (type == SVM_EVTINJ_TYPE_SOFT)
		return true;

623 624 625
	return type == SVM_EVTINJ_TYPE_EXEPT && kvm_exception_is_soft(vector);
}

626 627 628 629 630 631 632 633 634 635
static bool is_evtinj_nmi(u32 evtinj)
{
	u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;

	if (!(evtinj & SVM_EVTINJ_VALID))
		return false;

	return type == SVM_EVTINJ_TYPE_NMI;
}

636 637
static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
					  unsigned long vmcb12_rip)
638
{
639 640
	u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;
	u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
641

642
	struct kvm_vcpu *vcpu = &svm->vcpu;
643 644
	struct vmcb *vmcb01 = svm->vmcb01.ptr;
	struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
645

646 647 648 649 650
	/*
	 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
	 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
	 */

651 652 653 654 655
	if (svm->vgif_enabled && (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK))
		int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
	else
		int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);

656
	/* Copied from vmcb01.  msrpm_base can be overwritten later.  */
657 658 659
	vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
	vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
	vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa;
660 661 662 663

	/* Done at vmrun: asid.  */

	/* Also overwritten later if necessary.  */
664
	vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
665

666
	/* nested_cr3.  */
667
	if (nested_npt_enabled(svm))
668
		nested_svm_init_mmu_context(vcpu);
669

670 671 672 673 674
	vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
			vcpu->arch.l1_tsc_offset,
			svm->nested.ctl.tsc_offset,
			svm->tsc_ratio_msr);

675
	vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
676

677
	if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) {
678 679 680
		WARN_ON(!svm->tsc_scaling_enabled);
		nested_svm_update_tsc_ratio_msr(vcpu);
	}
681

682
	vmcb02->control.int_ctl             =
683
		(svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
684
		(vmcb01->control.int_ctl & int_ctl_vmcb01_bits);
685

686 687 688 689
	vmcb02->control.int_vector          = svm->nested.ctl.int_vector;
	vmcb02->control.int_state           = svm->nested.ctl.int_state;
	vmcb02->control.event_inj           = svm->nested.ctl.event_inj;
	vmcb02->control.event_inj_err       = svm->nested.ctl.event_inj_err;
690

691 692 693 694 695 696 697 698 699 700 701 702 703
	/*
	 * next_rip is consumed on VMRUN as the return address pushed on the
	 * stack for injected soft exceptions/interrupts.  If nrips is exposed
	 * to L1, take it verbatim from vmcb12.  If nrips is supported in
	 * hardware but not exposed to L1, stuff the actual L2 RIP to emulate
	 * what a nrips=0 CPU would do (L1 is responsible for advancing RIP
	 * prior to injecting the event).
	 */
	if (svm->nrips_enabled)
		vmcb02->control.next_rip    = svm->nested.ctl.next_rip;
	else if (boot_cpu_has(X86_FEATURE_NRIPS))
		vmcb02->control.next_rip    = vmcb12_rip;

704
	svm->nmi_l1_to_l2 = is_evtinj_nmi(vmcb02->control.event_inj);
705 706 707 708 709 710 711 712 713 714
	if (is_evtinj_soft(vmcb02->control.event_inj)) {
		svm->soft_int_injected = true;
		svm->soft_int_csbase = svm->vmcb->save.cs.base;
		svm->soft_int_old_rip = vmcb12_rip;
		if (svm->nrips_enabled)
			svm->soft_int_next_rip = svm->nested.ctl.next_rip;
		else
			svm->soft_int_next_rip = vmcb12_rip;
	}

715 716
	vmcb02->control.virt_ext            = vmcb01->control.virt_ext &
					      LBR_CTL_ENABLE_MASK;
717 718 719
	if (svm->lbrv_enabled)
		vmcb02->control.virt_ext  |=
			(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
720

721
	if (!nested_vmcb_needs_vls_intercept(svm))
722
		vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
723

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	if (kvm_pause_in_guest(svm->vcpu.kvm)) {
		/* use guest values since host doesn't use them */
		vmcb02->control.pause_filter_count =
				svm->pause_filter_enabled ?
				svm->nested.ctl.pause_filter_count : 0;

		vmcb02->control.pause_filter_thresh =
				svm->pause_threshold_enabled ?
				svm->nested.ctl.pause_filter_thresh : 0;

	} else if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
		/* use host values when guest doesn't use them */
		vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
		vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
	} else {
		/*
		 * Intercept every PAUSE otherwise and
		 * ignore both host and guest values
		 */
		vmcb02->control.pause_filter_count = 0;
		vmcb02->control.pause_filter_thresh = 0;
	}

747 748
	nested_svm_transition_tlb_flush(vcpu);

749
	/* Enter Guest-Mode */
750
	enter_guest_mode(vcpu);
751 752

	/*
753 754
	 * Merge guest and host intercepts - must be called with vcpu in
	 * guest-mode to take effect.
755 756
	 */
	recalc_intercepts(svm);
757 758
}

759 760 761 762 763 764 765 766 767 768 769 770
static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
{
	/*
	 * Some VMCB state is shared between L1 and L2 and thus has to be
	 * moved at the time of nested vmrun and vmexit.
	 *
	 * VMLOAD/VMSAVE state would also belong in this category, but KVM
	 * always performs VMLOAD and VMSAVE from the VMCB01.
	 */
	to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
}

771
int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
772
			 struct vmcb *vmcb12, bool from_vmrun)
773
{
774
	struct vcpu_svm *svm = to_svm(vcpu);
775 776
	int ret;

777 778 779 780 781 782 783 784 785 786 787 788 789 790
	trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
			       vmcb12->save.rip,
			       vmcb12->control.int_ctl,
			       vmcb12->control.event_inj,
			       vmcb12->control.nested_ctl);

	trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
				    vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
				    vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
				    vmcb12->control.intercepts[INTERCEPT_WORD3],
				    vmcb12->control.intercepts[INTERCEPT_WORD4],
				    vmcb12->control.intercepts[INTERCEPT_WORD5]);


791
	svm->nested.vmcb12_gpa = vmcb12_gpa;
792 793 794

	WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);

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);
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 804 805
	if (ret)
		return ret;

806 807 808
	if (!from_vmrun)
		kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);

P
Paolo Bonzini 已提交
809
	svm_set_gif(svm, true);
810

811 812 813
	if (kvm_vcpu_apicv_active(vcpu))
		kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);

814
	return 0;
815 816
}

817
int nested_svm_vmrun(struct kvm_vcpu *vcpu)
818
{
819
	struct vcpu_svm *svm = to_svm(vcpu);
820
	int ret;
821
	struct vmcb *vmcb12;
822
	struct kvm_host_map map;
823
	u64 vmcb12_gpa;
824
	struct vmcb *vmcb01 = svm->vmcb01.ptr;
825

826 827 828 829 830
	if (!svm->nested.hsave_msr) {
		kvm_inject_gp(vcpu, 0);
		return 1;
	}

831 832
	if (is_smm(vcpu)) {
		kvm_queue_exception(vcpu, UD_VECTOR);
833 834
		return 1;
	}
835

836
	vmcb12_gpa = svm->vmcb->save.rax;
837
	ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
838
	if (ret == -EINVAL) {
839
		kvm_inject_gp(vcpu, 0);
840 841
		return 1;
	} else if (ret) {
842
		return kvm_skip_emulated_instruction(vcpu);
843 844
	}

845
	ret = kvm_skip_emulated_instruction(vcpu);
846

847
	vmcb12 = map.hva;
848

849 850 851
	if (WARN_ON_ONCE(!svm->nested.initialized))
		return -EINVAL;

852
	nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
853
	nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
854

855
	if (!nested_vmcb_check_save(vcpu) ||
856
	    !nested_vmcb_check_controls(vcpu)) {
857 858 859 860
		vmcb12->control.exit_code    = SVM_EXIT_ERR;
		vmcb12->control.exit_code_hi = 0;
		vmcb12->control.exit_info_1  = 0;
		vmcb12->control.exit_info_2  = 0;
861
		goto out;
862 863 864
	}

	/*
865 866
	 * Since vmcb01 is not in use, we can use it to store some of the L1
	 * state.
867
	 */
868 869 870 871 872
	vmcb01->save.efer   = vcpu->arch.efer;
	vmcb01->save.cr0    = kvm_read_cr0(vcpu);
	vmcb01->save.cr4    = vcpu->arch.cr4;
	vmcb01->save.rflags = kvm_get_rflags(vcpu);
	vmcb01->save.rip    = kvm_rip_read(vcpu);
873 874

	if (!npt_enabled)
875
		vmcb01->save.cr3 = kvm_read_cr3(vcpu);
876

877
	svm->nested.nested_run_pending = 1;
878

879
	if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
880
		goto out_exit_err;
881

882 883
	if (nested_svm_vmrun_msrpm(svm))
		goto out;
884

885 886
out_exit_err:
	svm->nested.nested_run_pending = 0;
887
	svm->nmi_l1_to_l2 = false;
888
	svm->soft_int_injected = false;
889 890 891 892 893 894 895

	svm->vmcb->control.exit_code    = SVM_EXIT_ERR;
	svm->vmcb->control.exit_code_hi = 0;
	svm->vmcb->control.exit_info_1  = 0;
	svm->vmcb->control.exit_info_2  = 0;

	nested_svm_vmexit(svm);
896

897
out:
898
	kvm_vcpu_unmap(vcpu, &map, true);
899

900 901 902
	return ret;
}

903
/* Copy state save area fields which are handled by VMRUN */
904 905
void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
			  struct vmcb_save_area *from_save)
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
{
	to_save->es = from_save->es;
	to_save->cs = from_save->cs;
	to_save->ss = from_save->ss;
	to_save->ds = from_save->ds;
	to_save->gdtr = from_save->gdtr;
	to_save->idtr = from_save->idtr;
	to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
	to_save->efer = from_save->efer;
	to_save->cr0 = from_save->cr0;
	to_save->cr3 = from_save->cr3;
	to_save->cr4 = from_save->cr4;
	to_save->rax = from_save->rax;
	to_save->rsp = from_save->rsp;
	to_save->rip = from_save->rip;
	to_save->cpl = 0;
}

924
void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
{
	to_vmcb->save.fs = from_vmcb->save.fs;
	to_vmcb->save.gs = from_vmcb->save.gs;
	to_vmcb->save.tr = from_vmcb->save.tr;
	to_vmcb->save.ldtr = from_vmcb->save.ldtr;
	to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
	to_vmcb->save.star = from_vmcb->save.star;
	to_vmcb->save.lstar = from_vmcb->save.lstar;
	to_vmcb->save.cstar = from_vmcb->save.cstar;
	to_vmcb->save.sfmask = from_vmcb->save.sfmask;
	to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
	to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
	to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
}

int nested_svm_vmexit(struct vcpu_svm *svm)
{
942
	struct kvm_vcpu *vcpu = &svm->vcpu;
943 944
	struct vmcb *vmcb01 = svm->vmcb01.ptr;
	struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
945
	struct vmcb *vmcb12;
946
	struct kvm_host_map map;
947
	int rc;
948

949
	rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
950 951
	if (rc) {
		if (rc == -EINVAL)
952
			kvm_inject_gp(vcpu, 0);
953 954 955
		return 1;
	}

956
	vmcb12 = map.hva;
957 958

	/* Exit Guest-Mode */
959
	leave_guest_mode(vcpu);
960
	svm->nested.vmcb12_gpa = 0;
961
	WARN_ON_ONCE(svm->nested.nested_run_pending);
962

963
	kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
964

965 966 967
	/* in case we halted in L2 */
	svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;

968 969
	/* Give the current vmcb to the guest */

970 971 972 973 974 975
	vmcb12->save.es     = vmcb02->save.es;
	vmcb12->save.cs     = vmcb02->save.cs;
	vmcb12->save.ss     = vmcb02->save.ss;
	vmcb12->save.ds     = vmcb02->save.ds;
	vmcb12->save.gdtr   = vmcb02->save.gdtr;
	vmcb12->save.idtr   = vmcb02->save.idtr;
976
	vmcb12->save.efer   = svm->vcpu.arch.efer;
977 978
	vmcb12->save.cr0    = kvm_read_cr0(vcpu);
	vmcb12->save.cr3    = kvm_read_cr3(vcpu);
979
	vmcb12->save.cr2    = vmcb02->save.cr2;
980
	vmcb12->save.cr4    = svm->vcpu.arch.cr4;
981 982 983 984
	vmcb12->save.rflags = kvm_get_rflags(vcpu);
	vmcb12->save.rip    = kvm_rip_read(vcpu);
	vmcb12->save.rsp    = kvm_rsp_read(vcpu);
	vmcb12->save.rax    = kvm_rax_read(vcpu);
985
	vmcb12->save.dr7    = vmcb02->save.dr7;
986
	vmcb12->save.dr6    = svm->vcpu.arch.dr6;
987
	vmcb12->save.cpl    = vmcb02->save.cpl;
988

989 990 991 992 993
	vmcb12->control.int_state         = vmcb02->control.int_state;
	vmcb12->control.exit_code         = vmcb02->control.exit_code;
	vmcb12->control.exit_code_hi      = vmcb02->control.exit_code_hi;
	vmcb12->control.exit_info_1       = vmcb02->control.exit_info_1;
	vmcb12->control.exit_info_2       = vmcb02->control.exit_info_2;
994 995

	if (vmcb12->control.exit_code != SVM_EXIT_ERR)
996
		nested_save_pending_event_to_vmcb12(svm, vmcb12);
997 998

	if (svm->nrips_enabled)
999
		vmcb12->control.next_rip  = vmcb02->control.next_rip;
1000

1001 1002 1003 1004
	vmcb12->control.int_ctl           = svm->nested.ctl.int_ctl;
	vmcb12->control.tlb_ctl           = svm->nested.ctl.tlb_ctl;
	vmcb12->control.event_inj         = svm->nested.ctl.event_inj;
	vmcb12->control.event_inj_err     = svm->nested.ctl.event_inj_err;
1005

1006 1007 1008
	if (!kvm_pause_in_guest(vcpu->kvm) && vmcb02->control.pause_filter_count)
		vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;

1009 1010
	nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);

1011
	svm_switch_vmcb(svm, &svm->vmcb01);
1012

1013 1014 1015 1016
	if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
		svm_copy_lbrs(vmcb12, vmcb02);
		svm_update_lbrv(vcpu);
	} else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
1017 1018 1019 1020
		svm_copy_lbrs(vmcb01, vmcb02);
		svm_update_lbrv(vcpu);
	}

1021 1022 1023 1024
	/*
	 * On vmexit the  GIF is set to false and
	 * no event can be injected in L1.
	 */
1025
	svm_set_gif(svm, false);
1026
	vmcb01->control.exit_int_info = 0;
1027

1028
	svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
1029 1030 1031
	if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
		vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset;
		vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
1032
	}
1033

1034
	if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) {
1035 1036
		WARN_ON(!svm->tsc_scaling_enabled);
		vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
1037
		__svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1038 1039
	}

1040
	svm->nested.ctl.nested_cr3 = 0;
1041

1042 1043 1044
	/*
	 * Restore processor state that had been saved in vmcb01
	 */
1045 1046 1047 1048 1049 1050 1051
	kvm_set_rflags(vcpu, vmcb01->save.rflags);
	svm_set_efer(vcpu, vmcb01->save.efer);
	svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE);
	svm_set_cr4(vcpu, vmcb01->save.cr4);
	kvm_rax_write(vcpu, vmcb01->save.rax);
	kvm_rsp_write(vcpu, vmcb01->save.rsp);
	kvm_rip_write(vcpu, vmcb01->save.rip);
1052 1053 1054

	svm->vcpu.arch.dr7 = DR7_FIXED_1;
	kvm_update_dr7(&svm->vcpu);
1055

1056 1057 1058 1059 1060
	trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
				       vmcb12->control.exit_info_1,
				       vmcb12->control.exit_info_2,
				       vmcb12->control.exit_int_info,
				       vmcb12->control.exit_int_info_err,
1061 1062
				       KVM_ISA_SVM);

1063
	kvm_vcpu_unmap(vcpu, &map, true);
1064

1065 1066
	nested_svm_transition_tlb_flush(vcpu);

1067
	nested_svm_uninit_mmu_context(vcpu);
1068

1069
	rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true);
1070 1071
	if (rc)
		return 1;
1072

1073 1074 1075 1076 1077
	/*
	 * Drop what we picked up for L2 via svm_complete_interrupts() so it
	 * doesn't end up in L1.
	 */
	svm->vcpu.arch.nmi_injected = false;
1078 1079
	kvm_clear_exception_queue(vcpu);
	kvm_clear_interrupt_queue(vcpu);
1080 1081 1082 1083 1084 1085 1086

	/*
	 * If we are here following the completion of a VMRUN that
	 * is being single-stepped, queue the pending #DB intercept
	 * right now so that it an be accounted for before we execute
	 * L1's next instruction.
	 */
1087
	if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF))
1088
		kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
1089

1090 1091 1092 1093 1094 1095 1096
	/*
	 * Un-inhibit the AVIC right away, so that other vCPUs can start
	 * to benefit from it right away.
	 */
	if (kvm_apicv_activated(vcpu->kvm))
		kvm_vcpu_update_apicv(vcpu);

1097 1098 1099
	return 0;
}

1100 1101
static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
{
1102
	nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
1103 1104
}

1105 1106
int svm_allocate_nested(struct vcpu_svm *svm)
{
1107
	struct page *vmcb02_page;
1108 1109 1110 1111

	if (svm->nested.initialized)
		return 0;

1112 1113
	vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
	if (!vmcb02_page)
1114
		return -ENOMEM;
1115 1116
	svm->nested.vmcb02.ptr = page_address(vmcb02_page);
	svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
1117 1118 1119

	svm->nested.msrpm = svm_vcpu_alloc_msrpm();
	if (!svm->nested.msrpm)
1120
		goto err_free_vmcb02;
1121 1122 1123 1124 1125
	svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);

	svm->nested.initialized = true;
	return 0;

1126 1127
err_free_vmcb02:
	__free_page(vmcb02_page);
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	return -ENOMEM;
}

void svm_free_nested(struct vcpu_svm *svm)
{
	if (!svm->nested.initialized)
		return;

	svm_vcpu_free_msrpm(svm->nested.msrpm);
	svm->nested.msrpm = NULL;

1139 1140
	__free_page(virt_to_page(svm->nested.vmcb02.ptr));
	svm->nested.vmcb02.ptr = NULL;
1141

1142 1143 1144 1145 1146 1147 1148 1149 1150
	/*
	 * When last_vmcb12_gpa matches the current vmcb12 gpa,
	 * some vmcb12 fields are not loaded if they are marked clean
	 * in the vmcb12, since in this case they are up to date already.
	 *
	 * When the vmcb02 is freed, this optimization becomes invalid.
	 */
	svm->nested.last_vmcb12_gpa = INVALID_GPA;

1151 1152 1153
	svm->nested.initialized = false;
}

1154 1155 1156
/*
 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
 */
1157
void svm_leave_nested(struct kvm_vcpu *vcpu)
1158
{
1159
	struct vcpu_svm *svm = to_svm(vcpu);
1160 1161

	if (is_guest_mode(vcpu)) {
1162
		svm->nested.nested_run_pending = 0;
1163 1164
		svm->nested.vmcb12_gpa = INVALID_GPA;

1165
		leave_guest_mode(vcpu);
1166

1167
		svm_switch_vmcb(svm, &svm->vmcb01);
1168

1169
		nested_svm_uninit_mmu_context(vcpu);
1170
		vmcb_mark_all_dirty(svm->vmcb);
1171
	}
1172

1173
	kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1174 1175
}

1176 1177 1178 1179 1180
static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
{
	u32 offset, msr, value;
	int write, mask;

1181
	if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
		return NESTED_EXIT_HOST;

	msr    = svm->vcpu.arch.regs[VCPU_REGS_RCX];
	offset = svm_msrpm_offset(msr);
	write  = svm->vmcb->control.exit_info_1 & 1;
	mask   = 1 << ((2 * (msr & 0xf)) + write);

	if (offset == MSR_INVALID)
		return NESTED_EXIT_DONE;

	/* Offset is in 32 bit units but need in 8 bit units */
	offset *= 4;

1195
	if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
		return NESTED_EXIT_DONE;

	return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
}

static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
{
	unsigned port, size, iopm_len;
	u16 val, mask;
	u8 start_bit;
	u64 gpa;

1208
	if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1209 1210 1211 1212 1213
		return NESTED_EXIT_HOST;

	port = svm->vmcb->control.exit_info_1 >> 16;
	size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
		SVM_IOIO_SIZE_SHIFT;
1214
	gpa  = svm->nested.ctl.iopm_base_pa + (port / 8);
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
	start_bit = port % 8;
	iopm_len = (start_bit + size > 8) ? 2 : 1;
	mask = (0xf >> (4 - size)) << start_bit;
	val = 0;

	if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
		return NESTED_EXIT_DONE;

	return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
}

static int nested_svm_intercept(struct vcpu_svm *svm)
{
	u32 exit_code = svm->vmcb->control.exit_code;
	int vmexit = NESTED_EXIT_HOST;

	switch (exit_code) {
	case SVM_EXIT_MSR:
		vmexit = nested_svm_exit_handled_msr(svm);
		break;
	case SVM_EXIT_IOIO:
		vmexit = nested_svm_intercept_ioio(svm);
		break;
	case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1239
		if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1240 1241 1242 1243
			vmexit = NESTED_EXIT_DONE;
		break;
	}
	case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1244
		if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1245 1246 1247 1248
			vmexit = NESTED_EXIT_DONE;
		break;
	}
	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1249 1250 1251 1252 1253 1254
		/*
		 * Host-intercepted exceptions have been checked already in
		 * nested_svm_exit_special.  There is nothing to do here,
		 * the vmexit is injected by svm_check_nested_events.
		 */
		vmexit = NESTED_EXIT_DONE;
1255 1256 1257 1258 1259 1260 1261
		break;
	}
	case SVM_EXIT_ERR: {
		vmexit = NESTED_EXIT_DONE;
		break;
	}
	default: {
1262
		if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
			vmexit = NESTED_EXIT_DONE;
	}
	}

	return vmexit;
}

int nested_svm_exit_handled(struct vcpu_svm *svm)
{
	int vmexit;

	vmexit = nested_svm_intercept(svm);

	if (vmexit == NESTED_EXIT_DONE)
		nested_svm_vmexit(svm);

	return vmexit;
}

1282
int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1283
{
1284 1285
	if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
		kvm_queue_exception(vcpu, UD_VECTOR);
1286 1287 1288
		return 1;
	}

1289 1290
	if (to_svm(vcpu)->vmcb->save.cpl) {
		kvm_inject_gp(vcpu, 0);
1291 1292 1293 1294 1295 1296
		return 1;
	}

	return 0;
}

1297
static bool nested_exit_on_exception(struct vcpu_svm *svm)
1298
{
1299
	unsigned int nr = svm->vcpu.arch.exception.nr;
1300

1301
	return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
1302
}
1303

1304 1305 1306
static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
{
	unsigned int nr = svm->vcpu.arch.exception.nr;
1307
	struct vmcb *vmcb = svm->vmcb;
1308

1309 1310
	vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
	vmcb->control.exit_code_hi = 0;
1311 1312

	if (svm->vcpu.arch.exception.has_error_code)
1313
		vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
1314 1315 1316 1317 1318

	/*
	 * EXITINFO2 is undefined for all exception intercepts other
	 * than #PF.
	 */
1319 1320
	if (nr == PF_VECTOR) {
		if (svm->vcpu.arch.exception.nested_apf)
1321
			vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1322
		else if (svm->vcpu.arch.exception.has_payload)
1323
			vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1324
		else
1325
			vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1326 1327 1328 1329 1330 1331 1332 1333 1334
	} else if (nr == DB_VECTOR) {
		/* See inject_pending_event.  */
		kvm_deliver_exception_payload(&svm->vcpu);
		if (svm->vcpu.arch.dr7 & DR7_GD) {
			svm->vcpu.arch.dr7 &= ~DR7_GD;
			kvm_update_dr7(&svm->vcpu);
		}
	} else
		WARN_ON(svm->vcpu.arch.exception.has_payload);
1335

1336
	nested_svm_vmexit(svm);
1337 1338
}

1339 1340
static inline bool nested_exit_on_init(struct vcpu_svm *svm)
{
1341
	return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1342 1343
}

1344
static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1345 1346 1347
{
	struct vcpu_svm *svm = to_svm(vcpu);
	bool block_nested_events =
P
Paolo Bonzini 已提交
1348
		kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
1349 1350 1351 1352 1353 1354 1355 1356
	struct kvm_lapic *apic = vcpu->arch.apic;

	if (lapic_in_kernel(vcpu) &&
	    test_bit(KVM_APIC_INIT, &apic->pending_events)) {
		if (block_nested_events)
			return -EBUSY;
		if (!nested_exit_on_init(svm))
			return 0;
1357
		nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1358 1359
		return 0;
	}
1360

1361
	if (vcpu->arch.exception.pending) {
1362 1363 1364 1365 1366 1367 1368
		/*
		 * Only a pending nested run can block a pending exception.
		 * Otherwise an injected NMI/interrupt should either be
		 * lost or delivered to the nested hypervisor in the EXITINTINFO
		 * vmcb field, while delivering the pending exception.
		 */
		if (svm->nested.nested_run_pending)
1369 1370 1371 1372 1373 1374 1375
                        return -EBUSY;
		if (!nested_exit_on_exception(svm))
			return 0;
		nested_svm_inject_exception_vmexit(svm);
		return 0;
	}

1376
	if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1377 1378
		if (block_nested_events)
			return -EBUSY;
1379 1380
		if (!nested_exit_on_smi(svm))
			return 0;
1381
		nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1382 1383 1384
		return 0;
	}

1385
	if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1386 1387
		if (block_nested_events)
			return -EBUSY;
1388 1389
		if (!nested_exit_on_nmi(svm))
			return 0;
1390
		nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1391 1392 1393
		return 0;
	}

1394
	if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1395 1396
		if (block_nested_events)
			return -EBUSY;
1397 1398
		if (!nested_exit_on_intr(svm))
			return 0;
1399 1400
		trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
		nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
		return 0;
	}

	return 0;
}

int nested_svm_exit_special(struct vcpu_svm *svm)
{
	u32 exit_code = svm->vmcb->control.exit_code;

	switch (exit_code) {
	case SVM_EXIT_INTR:
	case SVM_EXIT_NMI:
	case SVM_EXIT_NPF:
1415 1416 1417 1418
		return NESTED_EXIT_HOST;
	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
		u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);

1419 1420
		if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
		    excp_bits)
1421
			return NESTED_EXIT_HOST;
1422
		else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1423
			 svm->vcpu.arch.apf.host_apf_flags)
1424
			/* Trap async PF even if not shadowing */
1425 1426
			return NESTED_EXIT_HOST;
		break;
1427
	}
1428 1429 1430 1431 1432 1433
	default:
		break;
	}

	return NESTED_EXIT_CONTINUE;
}
1434

1435 1436 1437 1438 1439 1440 1441
void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
{
	struct vcpu_svm *svm = to_svm(vcpu);

	vcpu->arch.tsc_scaling_ratio =
		kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
					       svm->tsc_ratio_msr);
1442
	__svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1443 1444
}

1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
/* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
					      struct vmcb_ctrl_area_cached *from)
{
	unsigned int i;

	memset(dst, 0, sizeof(struct vmcb_control_area));

	for (i = 0; i < MAX_INTERCEPT; i++)
		dst->intercepts[i] = from->intercepts[i];

	dst->iopm_base_pa         = from->iopm_base_pa;
	dst->msrpm_base_pa        = from->msrpm_base_pa;
	dst->tsc_offset           = from->tsc_offset;
	dst->asid                 = from->asid;
	dst->tlb_ctl              = from->tlb_ctl;
	dst->int_ctl              = from->int_ctl;
	dst->int_vector           = from->int_vector;
	dst->int_state            = from->int_state;
	dst->exit_code            = from->exit_code;
	dst->exit_code_hi         = from->exit_code_hi;
	dst->exit_info_1          = from->exit_info_1;
	dst->exit_info_2          = from->exit_info_2;
	dst->exit_int_info        = from->exit_int_info;
	dst->exit_int_info_err    = from->exit_int_info_err;
	dst->nested_ctl           = from->nested_ctl;
	dst->event_inj            = from->event_inj;
	dst->event_inj_err        = from->event_inj_err;
1473
	dst->next_rip             = from->next_rip;
1474 1475 1476 1477
	dst->nested_cr3           = from->nested_cr3;
	dst->virt_ext              = from->virt_ext;
	dst->pause_filter_count   = from->pause_filter_count;
	dst->pause_filter_thresh  = from->pause_filter_thresh;
1478
	/* 'clean' and 'reserved_sw' are not changed by KVM */
1479 1480
}

1481 1482 1483 1484 1485
static int svm_get_nested_state(struct kvm_vcpu *vcpu,
				struct kvm_nested_state __user *user_kvm_nested_state,
				u32 user_data_size)
{
	struct vcpu_svm *svm;
1486 1487
	struct vmcb_control_area *ctl;
	unsigned long r;
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
	struct kvm_nested_state kvm_state = {
		.flags = 0,
		.format = KVM_STATE_NESTED_FORMAT_SVM,
		.size = sizeof(kvm_state),
	};
	struct vmcb __user *user_vmcb = (struct vmcb __user *)
		&user_kvm_nested_state->data.svm[0];

	if (!vcpu)
		return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;

	svm = to_svm(vcpu);

	if (user_data_size < kvm_state.size)
		goto out;

	/* First fill in the header and copy it out.  */
	if (is_guest_mode(vcpu)) {
1506
		kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
		kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
		kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;

		if (svm->nested.nested_run_pending)
			kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
	}

	if (gif_set(svm))
		kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;

	if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
		return -EFAULT;

	if (!is_guest_mode(vcpu))
		goto out;

	/*
	 * Copy over the full size of the VMCB rather than just the size
	 * of the structs.
	 */
	if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
		return -EFAULT;
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538

	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
	if (!ctl)
		return -ENOMEM;

	nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
	r = copy_to_user(&user_vmcb->control, ctl,
			 sizeof(user_vmcb->control));
	kfree(ctl);
	if (r)
1539
		return -EFAULT;
1540

1541
	if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
			 sizeof(user_vmcb->save)))
		return -EFAULT;
out:
	return kvm_state.size;
}

static int svm_set_nested_state(struct kvm_vcpu *vcpu,
				struct kvm_nested_state __user *user_kvm_nested_state,
				struct kvm_nested_state *kvm_state)
{
	struct vcpu_svm *svm = to_svm(vcpu);
	struct vmcb __user *user_vmcb = (struct vmcb __user *)
		&user_kvm_nested_state->data.svm[0];
1555 1556
	struct vmcb_control_area *ctl;
	struct vmcb_save_area *save;
1557
	struct vmcb_save_area_cached save_cached;
1558
	struct vmcb_ctrl_area_cached ctl_cached;
1559
	unsigned long cr0;
1560
	int ret;
1561

1562 1563 1564
	BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
		     KVM_STATE_NESTED_SVM_VMCB_SIZE);

1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
	if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
		return -EINVAL;

	if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
				 KVM_STATE_NESTED_RUN_PENDING |
				 KVM_STATE_NESTED_GIF_SET))
		return -EINVAL;

	/*
	 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
	 */
	if (!(vcpu->arch.efer & EFER_SVME)) {
		/* GIF=1 and no guest mode are required if SVME=0.  */
		if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
			return -EINVAL;
	}

	/* SMM temporarily disables SVM, so we cannot be in guest mode.  */
	if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
		return -EINVAL;

	if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1588
		svm_leave_nested(vcpu);
1589 1590
		svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
		return 0;
1591 1592 1593 1594 1595 1596 1597
	}

	if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
		return -EINVAL;
	if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
		return -EINVAL;

1598
	ret  = -ENOMEM;
1599 1600
	ctl  = kzalloc(sizeof(*ctl),  GFP_KERNEL_ACCOUNT);
	save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
	if (!ctl || !save)
		goto out_free;

	ret = -EFAULT;
	if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
		goto out_free;
	if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
		goto out_free;

	ret = -EINVAL;
1611
	__nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
1612
	if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
1613
		goto out_free;
1614 1615 1616

	/*
	 * Processor state contains L2 state.  Check that it is
1617
	 * valid for guest mode (see nested_vmcb_check_save).
1618 1619 1620
	 */
	cr0 = kvm_read_cr0(vcpu);
        if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1621
		goto out_free;
1622 1623 1624 1625 1626

	/*
	 * Validate host state saved from before VMRUN (see
	 * nested_svm_check_permissions).
	 */
1627
	__nested_copy_vmcb_save_to_cache(&save_cached, save);
1628 1629 1630
	if (!(save->cr0 & X86_CR0_PG) ||
	    !(save->cr0 & X86_CR0_PE) ||
	    (save->rflags & X86_EFLAGS_VM) ||
1631
	    !__nested_vmcb_check_save(vcpu, &save_cached))
1632
		goto out_free;
1633

1634

1635
	/*
1636 1637 1638 1639
	 * All checks done, we can enter guest mode. Userspace provides
	 * vmcb12.control, which will be combined with L1 and stored into
	 * vmcb02, and the L1 save state which we store in vmcb01.
	 * L2 registers if needed are moved from the current VMCB to VMCB02.
1640
	 */
1641

1642
	if (is_guest_mode(vcpu))
1643
		svm_leave_nested(vcpu);
1644 1645 1646
	else
		svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;

1647 1648
	svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));

1649 1650 1651
	svm->nested.nested_run_pending =
		!!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);

1652
	svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1653

1654
	svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
1655
	nested_copy_vmcb_control_to_cache(svm, ctl);
1656 1657

	svm_switch_vmcb(svm, &svm->nested.vmcb02);
1658
	nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip);
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671

	/*
	 * While the nested guest CR3 is already checked and set by
	 * KVM_SET_SREGS, it was set when nested state was yet loaded,
	 * thus MMU might not be initialized correctly.
	 * Set it again to fix this.
	 */

	ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
				  nested_npt_enabled(svm), false);
	if (WARN_ON_ONCE(ret))
		goto out_free;

1672
	svm->nested.force_msr_bitmap_recalc = true;
1673

1674
	kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1675 1676 1677 1678 1679 1680
	ret = 0;
out_free:
	kfree(save);
	kfree(ctl);

	return ret;
1681 1682
}

1683 1684 1685 1686 1687 1688 1689
static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
{
	struct vcpu_svm *svm = to_svm(vcpu);

	if (WARN_ON(!is_guest_mode(vcpu)))
		return true;

1690 1691
	if (!vcpu->arch.pdptrs_from_userspace &&
	    !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1692 1693 1694 1695 1696
		/*
		 * Reload the guest's PDPTRs since after a migration
		 * the guest CR3 might be restored prior to setting the nested
		 * state which can lead to a load of wrong PDPTRs.
		 */
1697
		if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
1698
			return false;
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710

	if (!nested_svm_vmrun_msrpm(svm)) {
		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
		vcpu->run->internal.suberror =
			KVM_INTERNAL_ERROR_EMULATION;
		vcpu->run->internal.ndata = 0;
		return false;
	}

	return true;
}

1711
struct kvm_x86_nested_ops svm_nested_ops = {
1712
	.leave_nested = svm_leave_nested,
1713
	.check_events = svm_check_nested_events,
1714
	.handle_page_fault_workaround = nested_svm_handle_page_fault_workaround,
1715
	.triple_fault = nested_svm_triple_fault,
1716
	.get_nested_state_pages = svm_get_nested_state_pages,
1717 1718
	.get_state = svm_get_nested_state,
	.set_state = svm_set_nested_state,
1719
};