Linux Audio

Check our new training course

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