nested.c 43.5 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
				       struct x86_exception *fault)
{
	struct vcpu_svm *svm = to_svm(vcpu);

	if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
		/*
		 * TODO: track the cause of the nested page fault, and
		 * correctly fill in the high bits of exit_info_1.
		 */
		svm->vmcb->control.exit_code = SVM_EXIT_NPF;
		svm->vmcb->control.exit_code_hi = 0;
		svm->vmcb->control.exit_info_1 = (1ULL << 32);
		svm->vmcb->control.exit_info_2 = fault->address;
	}

	svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
	svm->vmcb->control.exit_info_1 |= fault->error_code;

	nested_svm_vmexit(svm);
}

57 58 59 60 61
static void svm_inject_page_fault_nested(struct kvm_vcpu *vcpu, struct x86_exception *fault)
{
       struct vcpu_svm *svm = to_svm(vcpu);
       WARN_ON(!is_guest_mode(vcpu));

62 63 64
	if (vmcb12_is_intercept(&svm->nested.ctl,
				INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
	    !svm->nested.nested_run_pending) {
65 66 67 68 69 70 71 72 73 74
               svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
               svm->vmcb->control.exit_code_hi = 0;
               svm->vmcb->control.exit_info_1 = fault->error_code;
               svm->vmcb->control.exit_info_2 = fault->address;
               nested_svm_vmexit(svm);
       } else {
               kvm_inject_page_fault(vcpu, fault);
       }
}

75 76 77
static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
{
	struct vcpu_svm *svm = to_svm(vcpu);
78
	u64 cr3 = svm->nested.ctl.nested_cr3;
79 80 81
	u64 pdpte;
	int ret;

82
	ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
83 84 85 86 87 88 89 90 91 92
				       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);

93
	return svm->nested.ctl.nested_cr3;
94 95 96 97
}

static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
{
98 99
	struct vcpu_svm *svm = to_svm(vcpu);

100 101 102
	WARN_ON(mmu_is_nested(vcpu));

	vcpu->arch.mmu = &vcpu->arch.guest_mmu;
103 104 105 106 107 108

	/*
	 * 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.
	 */
109 110
	kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
				svm->vmcb01.ptr->save.efer,
111
				svm->nested.ctl.nested_cr3);
112 113 114 115 116 117 118 119 120 121 122 123 124 125
	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;
}

void recalc_intercepts(struct vcpu_svm *svm)
{
126 127
	struct vmcb_control_area *c, *h;
	struct vmcb_ctrl_area_cached *g;
128
	unsigned int i;
129

130
	vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
131 132 133 134 135

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

	c = &svm->vmcb->control;
136
	h = &svm->vmcb01.ptr->control;
137
	g = &svm->nested.ctl;
138

139 140 141
	for (i = 0; i < MAX_INTERCEPT; i++)
		c->intercepts[i] = h->intercepts[i];

P
Paolo Bonzini 已提交
142
	if (g->int_ctl & V_INTR_MASKING_MASK) {
143
		/* We only want the cr8 intercept bits of L1 */
144 145
		vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
		vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
146 147 148 149 150 151

		/*
		 * 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.
		 */
152
		vmcb_clr_intercept(c, INTERCEPT_VINTR);
153 154 155
	}

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

158 159
	for (i = 0; i < MAX_INTERCEPT; i++)
		c->intercepts[i] |= g->intercepts[i];
160 161 162 163

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

	vmcb_set_intercept(c, INTERCEPT_VMLOAD);
	vmcb_set_intercept(c, INTERCEPT_VMSAVE);
167 168
}

169 170 171 172 173
/*
 * 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.
 */
174 175
static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
{
176 177 178 179
	struct hv_enlightenments *hve =
		(struct hv_enlightenments *)svm->nested.ctl.reserved_sw;
	int i;

180
	/*
181 182 183 184 185 186
	 * 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.
187
	 */
188 189 190 191 192
	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;
193

194
	if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
195 196 197 198 199 200 201 202 203 204
		return true;

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

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

		p      = msrpm_offsets[i];
205
		offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
206 207 208 209 210 211 212

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

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

213 214
	svm->nested.force_msr_bitmap_recalc = false;

215
set_msrpm_base_pa:
216 217 218 219 220
	svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));

	return true;
}

221 222 223 224 225 226 227 228 229 230 231
/*
 * 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);
}

232 233 234 235 236 237 238 239 240 241 242 243
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;
	}
}

244
static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
245
					 struct vmcb_ctrl_area_cached *control)
246
{
247
	if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
248 249
		return false;

250
	if (CC(control->asid == 0))
251 252
		return false;

253
	if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
254 255
		return false;

256 257 258 259 260 261 262
	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;

263 264 265
	if (CC(!nested_svm_check_tlb_ctl(vcpu, control->tlb_ctl)))
		return false;

266 267 268
	return true;
}

269
/* Common checks that apply to both L1 and L2 state.  */
270 271
static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
				     struct vmcb_save_area_cached *save)
272
{
273
	if (CC(!(save->efer & EFER_SVME)))
274 275
		return false;

276 277
	if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
	    CC(save->cr0 & ~0xffffffffULL))
278 279
		return false;

280
	if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
281 282
		return false;

283 284 285 286 287 288 289 290 291 292 293 294 295
	/*
	 * 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)))
296
		return false;
297

298
	if (CC(!kvm_valid_efer(vcpu, save->efer)))
299 300 301 302 303
		return false;

	return true;
}

304 305 306 307 308 309 310 311
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);
}

312 313 314
static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
{
	struct vcpu_svm *svm = to_svm(vcpu);
315
	struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
316 317 318 319

	return __nested_vmcb_check_controls(vcpu, ctl);
}

320
static
321 322
void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
					 struct vmcb_ctrl_area_cached *to,
323
					 struct vmcb_control_area *from)
324
{
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	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;
	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;
355 356 357 358 359 360 361

	/* 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));
	}
362
}
363

364 365 366
void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
				       struct vmcb_control_area *control)
{
367
	__nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control);
368 369
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
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);
}

392 393
/*
 * Synchronize fields that are written by the processor, so that
394
 * they can be copied back into the vmcb12.
395
 */
396
void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
397 398 399 400 401 402 403 404
{
	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) &&
405
	    svm_is_intercept(svm, INTERCEPT_VINTR)) {
406 407 408 409 410 411 412 413 414 415 416 417 418 419
		/*
		 * 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;
	}
	svm->nested.ctl.int_ctl        &= ~mask;
	svm->nested.ctl.int_ctl        |= svm->vmcb->control.int_ctl & mask;
}

420 421 422 423
/*
 * Transfer any event that L0 or L1 wanted to inject into L2 to
 * EXIT_INT_INFO.
 */
424 425
static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
						struct vmcb *vmcb12)
426 427 428 429 430 431 432 433 434 435 436
{
	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;
437
			vmcb12->control.exit_int_info_err =
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
				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;
	}

454
	vmcb12->control.exit_int_info = exit_int_info;
455 456
}

457 458 459 460 461
static inline bool nested_npt_enabled(struct vcpu_svm *svm)
{
	return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
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);
}

481
/*
482 483
 * 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.
484 485
 */
static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
486
			       bool nested_npt, bool reload_pdptrs)
487
{
488
	if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
489 490
		return -EINVAL;

491
	if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
492
	    CC(!load_pdptrs(vcpu, cr3)))
493
		return -EINVAL;
494 495 496

	vcpu->arch.cr3 = cr3;

497
	/* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
498
	kvm_init_mmu(vcpu);
499

500 501 502
	if (!nested_npt)
		kvm_mmu_new_pgd(vcpu, cr3);

503
	return 0;
504 505
}

506 507 508 509 510 511 512 513 514
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;
}

515
static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
516
{
517 518
	bool new_vmcb12 = false;

519 520
	nested_vmcb02_compute_g_pat(svm);

521
	/* Load the nested guest state */
522 523 524
	if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
		new_vmcb12 = true;
		svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
525
		svm->nested.force_msr_bitmap_recalc = true;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	}

	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
		svm->vmcb->save.es = vmcb12->save.es;
		svm->vmcb->save.cs = vmcb12->save.cs;
		svm->vmcb->save.ss = vmcb12->save.ss;
		svm->vmcb->save.ds = vmcb12->save.ds;
		svm->vmcb->save.cpl = vmcb12->save.cpl;
		vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
	}

	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
		svm->vmcb->save.gdtr = vmcb12->save.gdtr;
		svm->vmcb->save.idtr = vmcb12->save.idtr;
		vmcb_mark_dirty(svm->vmcb, VMCB_DT);
	}
542

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

545
	svm_set_efer(&svm->vcpu, svm->nested.save.efer);
546

547 548
	svm_set_cr0(&svm->vcpu, svm->nested.save.cr0);
	svm_set_cr4(&svm->vcpu, svm->nested.save.cr4);
549 550

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

552 553 554
	kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
	kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
	kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
555 556

	/* In case we don't even reach vcpu_run, the fields are not updated */
557 558 559
	svm->vmcb->save.rax = vmcb12->save.rax;
	svm->vmcb->save.rsp = vmcb12->save.rsp;
	svm->vmcb->save.rip = vmcb12->save.rip;
560

561 562
	/* 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))) {
563 564
		svm->vmcb->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
		svm->vcpu.arch.dr6  = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
565 566
		vmcb_mark_dirty(svm->vmcb, VMCB_DR);
	}
567
}
568

569
static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
570
{
571 572 573 574 575
	const u32 int_ctl_vmcb01_bits =
		V_INTR_MASKING_MASK | V_GIF_MASK | V_GIF_ENABLE_MASK;

	const u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;

576
	struct kvm_vcpu *vcpu = &svm->vcpu;
577

578 579 580 581 582 583 584 585 586
	/*
	 * 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.
	 */

	/*
	 * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
	 * avic_physical_id.
	 */
587
	WARN_ON(kvm_apicv_activated(svm->vcpu.kvm));
588 589 590 591 592 593 594 595 596 597

	/* Copied from vmcb01.  msrpm_base can be overwritten later.  */
	svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
	svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
	svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;

	/* Done at vmrun: asid.  */

	/* Also overwritten later if necessary.  */
	svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
598

599
	/* nested_cr3.  */
600
	if (nested_npt_enabled(svm))
601
		nested_svm_init_mmu_context(vcpu);
602

603 604 605 606 607 608 609 610 611 612 613
	vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
			vcpu->arch.l1_tsc_offset,
			svm->nested.ctl.tsc_offset,
			svm->tsc_ratio_msr);

	svm->vmcb->control.tsc_offset = vcpu->arch.tsc_offset;

	if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
		WARN_ON(!svm->tsc_scaling_enabled);
		nested_svm_update_tsc_ratio_msr(vcpu);
	}
614

615
	svm->vmcb->control.int_ctl             =
616 617
		(svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
		(svm->vmcb01.ptr->control.int_ctl & int_ctl_vmcb01_bits);
618

619 620 621 622
	svm->vmcb->control.int_vector          = svm->nested.ctl.int_vector;
	svm->vmcb->control.int_state           = svm->nested.ctl.int_state;
	svm->vmcb->control.event_inj           = svm->nested.ctl.event_inj;
	svm->vmcb->control.event_inj_err       = svm->nested.ctl.event_inj_err;
623

624 625
	nested_svm_transition_tlb_flush(vcpu);

626
	/* Enter Guest-Mode */
627
	enter_guest_mode(vcpu);
628 629

	/*
630 631
	 * Merge guest and host intercepts - must be called with vcpu in
	 * guest-mode to take effect.
632 633
	 */
	recalc_intercepts(svm);
634 635
}

636 637 638 639 640 641 642 643 644 645 646 647
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;
}

648
int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
649
			 struct vmcb *vmcb12, bool from_vmrun)
650
{
651
	struct vcpu_svm *svm = to_svm(vcpu);
652 653
	int ret;

654 655 656 657 658 659 660 661 662 663 664 665 666 667
	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]);


668
	svm->nested.vmcb12_gpa = vmcb12_gpa;
669 670 671

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

672
	nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
673 674

	svm_switch_vmcb(svm, &svm->nested.vmcb02);
675 676
	nested_vmcb02_prepare_control(svm);
	nested_vmcb02_prepare_save(svm, vmcb12);
677

678
	ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
679
				  nested_npt_enabled(svm), from_vmrun);
680 681 682
	if (ret)
		return ret;

683
	if (!npt_enabled)
684
		vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
685

686 687 688
	if (!from_vmrun)
		kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);

P
Paolo Bonzini 已提交
689
	svm_set_gif(svm, true);
690 691

	return 0;
692 693
}

694
int nested_svm_vmrun(struct kvm_vcpu *vcpu)
695
{
696
	struct vcpu_svm *svm = to_svm(vcpu);
697
	int ret;
698
	struct vmcb *vmcb12;
699
	struct kvm_host_map map;
700
	u64 vmcb12_gpa;
701

702 703 704 705 706
	if (!svm->nested.hsave_msr) {
		kvm_inject_gp(vcpu, 0);
		return 1;
	}

707 708
	if (is_smm(vcpu)) {
		kvm_queue_exception(vcpu, UD_VECTOR);
709 710
		return 1;
	}
711

712
	vmcb12_gpa = svm->vmcb->save.rax;
713
	ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
714
	if (ret == -EINVAL) {
715
		kvm_inject_gp(vcpu, 0);
716 717
		return 1;
	} else if (ret) {
718
		return kvm_skip_emulated_instruction(vcpu);
719 720
	}

721
	ret = kvm_skip_emulated_instruction(vcpu);
722

723
	vmcb12 = map.hva;
724

725 726 727
	if (WARN_ON_ONCE(!svm->nested.initialized))
		return -EINVAL;

728
	nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
729
	nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
730

731
	if (!nested_vmcb_check_save(vcpu) ||
732
	    !nested_vmcb_check_controls(vcpu)) {
733 734 735 736
		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;
737
		goto out;
738 739 740
	}

	/*
741 742
	 * Since vmcb01 is not in use, we can use it to store some of the L1
	 * state.
743
	 */
744 745 746 747 748
	svm->vmcb01.ptr->save.efer   = vcpu->arch.efer;
	svm->vmcb01.ptr->save.cr0    = kvm_read_cr0(vcpu);
	svm->vmcb01.ptr->save.cr4    = vcpu->arch.cr4;
	svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
	svm->vmcb01.ptr->save.rip    = kvm_rip_read(vcpu);
749 750

	if (!npt_enabled)
751
		svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
752

753
	svm->nested.nested_run_pending = 1;
754

755
	if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
756
		goto out_exit_err;
757

758 759
	if (nested_svm_vmrun_msrpm(svm))
		goto out;
760

761 762 763 764 765 766 767 768 769
out_exit_err:
	svm->nested.nested_run_pending = 0;

	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);
770

771
out:
772
	kvm_vcpu_unmap(vcpu, &map, true);
773

774 775 776
	return ret;
}

777
/* Copy state save area fields which are handled by VMRUN */
778 779
void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
			  struct vmcb_save_area *from_save)
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
{
	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;
}

798
void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
{
	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)
{
816
	struct kvm_vcpu *vcpu = &svm->vcpu;
817
	struct vmcb *vmcb12;
818 819
	struct vmcb *vmcb = svm->vmcb;
	struct kvm_host_map map;
820
	int rc;
821

822 823 824
	/* Triple faults in L2 should never escape. */
	WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));

825
	rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
826 827
	if (rc) {
		if (rc == -EINVAL)
828
			kvm_inject_gp(vcpu, 0);
829 830 831
		return 1;
	}

832
	vmcb12 = map.hva;
833 834

	/* Exit Guest-Mode */
835
	leave_guest_mode(vcpu);
836
	svm->nested.vmcb12_gpa = 0;
837
	WARN_ON_ONCE(svm->nested.nested_run_pending);
838

839
	kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
840

841 842 843
	/* in case we halted in L2 */
	svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;

844 845
	/* Give the current vmcb to the guest */

846 847 848 849 850 851 852
	vmcb12->save.es     = vmcb->save.es;
	vmcb12->save.cs     = vmcb->save.cs;
	vmcb12->save.ss     = vmcb->save.ss;
	vmcb12->save.ds     = vmcb->save.ds;
	vmcb12->save.gdtr   = vmcb->save.gdtr;
	vmcb12->save.idtr   = vmcb->save.idtr;
	vmcb12->save.efer   = svm->vcpu.arch.efer;
853 854
	vmcb12->save.cr0    = kvm_read_cr0(vcpu);
	vmcb12->save.cr3    = kvm_read_cr3(vcpu);
855 856
	vmcb12->save.cr2    = vmcb->save.cr2;
	vmcb12->save.cr4    = svm->vcpu.arch.cr4;
857 858 859 860
	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);
861 862 863 864 865 866 867 868 869 870 871
	vmcb12->save.dr7    = vmcb->save.dr7;
	vmcb12->save.dr6    = svm->vcpu.arch.dr6;
	vmcb12->save.cpl    = vmcb->save.cpl;

	vmcb12->control.int_state         = vmcb->control.int_state;
	vmcb12->control.exit_code         = vmcb->control.exit_code;
	vmcb12->control.exit_code_hi      = vmcb->control.exit_code_hi;
	vmcb12->control.exit_info_1       = vmcb->control.exit_info_1;
	vmcb12->control.exit_info_2       = vmcb->control.exit_info_2;

	if (vmcb12->control.exit_code != SVM_EXIT_ERR)
872
		nested_save_pending_event_to_vmcb12(svm, vmcb12);
873 874

	if (svm->nrips_enabled)
875
		vmcb12->control.next_rip  = vmcb->control.next_rip;
876

877 878 879 880
	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;
881

882 883
	nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);

884
	svm_switch_vmcb(svm, &svm->vmcb01);
885

886 887 888 889
	/*
	 * On vmexit the  GIF is set to false and
	 * no event can be injected in L1.
	 */
890
	svm_set_gif(svm, false);
891
	svm->vmcb->control.exit_int_info = 0;
892

893 894 895 896 897
	svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
	if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
		svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
		vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
	}
898

899 900 901 902 903 904
	if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
		WARN_ON(!svm->tsc_scaling_enabled);
		vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
		svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio);
	}

905
	svm->nested.ctl.nested_cr3 = 0;
906

907 908 909
	/*
	 * Restore processor state that had been saved in vmcb01
	 */
910 911 912 913 914 915 916
	kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
	svm_set_efer(vcpu, svm->vmcb->save.efer);
	svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
	svm_set_cr4(vcpu, svm->vmcb->save.cr4);
	kvm_rax_write(vcpu, svm->vmcb->save.rax);
	kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
	kvm_rip_write(vcpu, svm->vmcb->save.rip);
917 918 919

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

921 922 923 924 925
	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,
926 927
				       KVM_ISA_SVM);

928
	kvm_vcpu_unmap(vcpu, &map, true);
929

930 931
	nested_svm_transition_tlb_flush(vcpu);

932
	nested_svm_uninit_mmu_context(vcpu);
933

934
	rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false, true);
935 936
	if (rc)
		return 1;
937

938 939 940 941 942
	/*
	 * 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;
943 944
	kvm_clear_exception_queue(vcpu);
	kvm_clear_interrupt_queue(vcpu);
945 946 947 948 949 950 951 952 953

	/*
	 * 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.
	 */
	if (unlikely(svm->vmcb->save.rflags & X86_EFLAGS_TF))
		kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
954 955 956 957

	return 0;
}

958 959
static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
{
960
	nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
961 962
}

963 964
int svm_allocate_nested(struct vcpu_svm *svm)
{
965
	struct page *vmcb02_page;
966 967 968 969

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

970 971
	vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
	if (!vmcb02_page)
972
		return -ENOMEM;
973 974
	svm->nested.vmcb02.ptr = page_address(vmcb02_page);
	svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
975 976 977

	svm->nested.msrpm = svm_vcpu_alloc_msrpm();
	if (!svm->nested.msrpm)
978
		goto err_free_vmcb02;
979 980 981 982 983
	svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);

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

984 985
err_free_vmcb02:
	__free_page(vmcb02_page);
986 987 988 989 990 991 992 993 994 995 996
	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;

997 998
	__free_page(virt_to_page(svm->nested.vmcb02.ptr));
	svm->nested.vmcb02.ptr = NULL;
999

1000 1001 1002 1003 1004 1005 1006 1007 1008
	/*
	 * 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;

1009 1010 1011
	svm->nested.initialized = false;
}

1012 1013 1014
/*
 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
 */
1015
void svm_leave_nested(struct kvm_vcpu *vcpu)
1016
{
1017
	struct vcpu_svm *svm = to_svm(vcpu);
1018 1019

	if (is_guest_mode(vcpu)) {
1020
		svm->nested.nested_run_pending = 0;
1021 1022
		svm->nested.vmcb12_gpa = INVALID_GPA;

1023
		leave_guest_mode(vcpu);
1024

1025
		svm_switch_vmcb(svm, &svm->vmcb01);
1026

1027
		nested_svm_uninit_mmu_context(vcpu);
1028
		vmcb_mark_all_dirty(svm->vmcb);
1029
	}
1030

1031
	kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1032 1033
}

1034 1035 1036 1037 1038
static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
{
	u32 offset, msr, value;
	int write, mask;

1039
	if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
		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;

1053
	if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
		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;

1066
	if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1067 1068 1069 1070 1071
		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;
1072
	gpa  = svm->nested.ctl.iopm_base_pa + (port / 8);
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	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: {
1097
		if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1098 1099 1100 1101
			vmexit = NESTED_EXIT_DONE;
		break;
	}
	case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1102
		if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1103 1104 1105 1106
			vmexit = NESTED_EXIT_DONE;
		break;
	}
	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1107 1108 1109 1110 1111 1112
		/*
		 * 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;
1113 1114 1115 1116 1117 1118 1119
		break;
	}
	case SVM_EXIT_ERR: {
		vmexit = NESTED_EXIT_DONE;
		break;
	}
	default: {
1120
		if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
			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;
}

1140
int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1141
{
1142 1143
	if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
		kvm_queue_exception(vcpu, UD_VECTOR);
1144 1145 1146
		return 1;
	}

1147 1148
	if (to_svm(vcpu)->vmcb->save.cpl) {
		kvm_inject_gp(vcpu, 0);
1149 1150 1151 1152 1153 1154
		return 1;
	}

	return 0;
}

1155
static bool nested_exit_on_exception(struct vcpu_svm *svm)
1156
{
1157
	unsigned int nr = svm->vcpu.arch.exception.nr;
1158

1159
	return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
1160
}
1161

1162 1163 1164
static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
{
	unsigned int nr = svm->vcpu.arch.exception.nr;
1165 1166 1167

	svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
	svm->vmcb->control.exit_code_hi = 0;
1168 1169 1170

	if (svm->vcpu.arch.exception.has_error_code)
		svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
1171 1172 1173 1174 1175

	/*
	 * EXITINFO2 is undefined for all exception intercepts other
	 * than #PF.
	 */
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
	if (nr == PF_VECTOR) {
		if (svm->vcpu.arch.exception.nested_apf)
			svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
		else if (svm->vcpu.arch.exception.has_payload)
			svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
		else
			svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
	} 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);
1192

1193
	nested_svm_vmexit(svm);
1194 1195
}

1196 1197
static inline bool nested_exit_on_init(struct vcpu_svm *svm)
{
1198
	return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1199 1200
}

1201
static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1202 1203 1204
{
	struct vcpu_svm *svm = to_svm(vcpu);
	bool block_nested_events =
P
Paolo Bonzini 已提交
1205
		kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
1206 1207 1208 1209 1210 1211 1212 1213
	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;
1214
		nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1215 1216
		return 0;
	}
1217

1218
	if (vcpu->arch.exception.pending) {
1219 1220 1221 1222 1223 1224 1225
		/*
		 * 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)
1226 1227 1228 1229 1230 1231 1232
                        return -EBUSY;
		if (!nested_exit_on_exception(svm))
			return 0;
		nested_svm_inject_exception_vmexit(svm);
		return 0;
	}

1233
	if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1234 1235
		if (block_nested_events)
			return -EBUSY;
1236 1237
		if (!nested_exit_on_smi(svm))
			return 0;
1238
		nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1239 1240 1241
		return 0;
	}

1242
	if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1243 1244
		if (block_nested_events)
			return -EBUSY;
1245 1246
		if (!nested_exit_on_nmi(svm))
			return 0;
1247
		nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1248 1249 1250
		return 0;
	}

1251
	if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1252 1253
		if (block_nested_events)
			return -EBUSY;
1254 1255
		if (!nested_exit_on_intr(svm))
			return 0;
1256 1257
		trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
		nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
		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:
1272 1273 1274 1275
		return NESTED_EXIT_HOST;
	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
		u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);

1276 1277
		if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
		    excp_bits)
1278
			return NESTED_EXIT_HOST;
1279
		else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1280
			 svm->vcpu.arch.apf.host_apf_flags)
1281
			/* Trap async PF even if not shadowing */
1282 1283
			return NESTED_EXIT_HOST;
		break;
1284
	}
1285 1286 1287 1288 1289 1290
	default:
		break;
	}

	return NESTED_EXIT_CONTINUE;
}
1291

1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
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);
	svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio);
}

1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
/* 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;
	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;
1334
	/* 'clean' and 'reserved_sw' are not changed by KVM */
1335 1336
}

1337 1338 1339 1340 1341
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;
1342 1343
	struct vmcb_control_area *ctl;
	unsigned long r;
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	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)) {
1362
		kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
		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;
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394

	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)
1395
		return -EFAULT;
1396

1397
	if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
			 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];
1411 1412
	struct vmcb_control_area *ctl;
	struct vmcb_save_area *save;
1413
	struct vmcb_save_area_cached save_cached;
1414
	struct vmcb_ctrl_area_cached ctl_cached;
1415
	unsigned long cr0;
1416
	int ret;
1417

1418 1419 1420
	BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
		     KVM_STATE_NESTED_SVM_VMCB_SIZE);

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
	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)) {
1444
		svm_leave_nested(vcpu);
1445 1446
		svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
		return 0;
1447 1448 1449 1450 1451 1452 1453
	}

	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;

1454
	ret  = -ENOMEM;
1455 1456
	ctl  = kzalloc(sizeof(*ctl),  GFP_KERNEL_ACCOUNT);
	save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
	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;
1467
	__nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
1468
	if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
1469
		goto out_free;
1470 1471 1472

	/*
	 * Processor state contains L2 state.  Check that it is
1473
	 * valid for guest mode (see nested_vmcb_check_save).
1474 1475 1476
	 */
	cr0 = kvm_read_cr0(vcpu);
        if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1477
		goto out_free;
1478 1479 1480 1481 1482

	/*
	 * Validate host state saved from before VMRUN (see
	 * nested_svm_check_permissions).
	 */
1483
	__nested_copy_vmcb_save_to_cache(&save_cached, save);
1484 1485 1486
	if (!(save->cr0 & X86_CR0_PG) ||
	    !(save->cr0 & X86_CR0_PE) ||
	    (save->rflags & X86_EFLAGS_VM) ||
1487
	    !__nested_vmcb_check_save(vcpu, &save_cached))
1488
		goto out_free;
1489

1490

1491
	/*
1492 1493 1494 1495
	 * 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.
1496
	 */
1497

1498
	if (is_guest_mode(vcpu))
1499
		svm_leave_nested(vcpu);
1500 1501 1502
	else
		svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;

1503 1504
	svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));

1505 1506 1507
	svm->nested.nested_run_pending =
		!!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);

1508
	svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1509

1510
	svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
1511
	nested_copy_vmcb_control_to_cache(svm, ctl);
1512 1513

	svm_switch_vmcb(svm, &svm->nested.vmcb02);
1514
	nested_vmcb02_prepare_control(svm);
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527

	/*
	 * 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;

1528
	svm->nested.force_msr_bitmap_recalc = true;
1529

1530
	kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1531 1532 1533 1534 1535 1536
	ret = 0;
out_free:
	kfree(save);
	kfree(ctl);

	return ret;
1537 1538
}

1539 1540 1541 1542 1543 1544 1545
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;

1546 1547
	if (!vcpu->arch.pdptrs_from_userspace &&
	    !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1548 1549 1550 1551 1552
		/*
		 * 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.
		 */
1553
		if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
1554
			return false;
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566

	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;
}

1567
struct kvm_x86_nested_ops svm_nested_ops = {
1568
	.leave_nested = svm_leave_nested,
1569
	.check_events = svm_check_nested_events,
1570
	.triple_fault = nested_svm_triple_fault,
1571
	.get_nested_state_pages = svm_get_nested_state_pages,
1572 1573
	.get_state = svm_get_nested_state,
	.set_state = svm_set_nested_state,
1574
};