Skip to contentSkip to conversation

Nested VMX support v3

PATCH In review [email protected] 4 participants 34 messages Oct 15, 2009 – Nov 9, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Avi,
We have addressed all of the comments, please apply.

The following patches implement nested VMX support. The patches enable a guest to use the VMX APIs in order to run its own nested guest (i.e., enable running other hypervisors which use VMX under KVM). The current patches support running Linux under a nested KVM using shadow page table (with bypass_guest_pf disabled). SMP support was fixed. Reworking EPT support to mesh cleanly with the current shadow paging design per Avi's comments is a work-in-progress.

The current patches only support a single nested hypervisor, which can only run a single guest (multiple guests are work in progress). Only 64-bit nested hypervisors are supported.

Additional patches for running Windows under nested KVM, and Linux under nested VMware server(!), are currently running in the lab. We are in the process of forward-porting those patches to -tip.

This patches were written by:
Orit Wasserman, [email protected]
Ben-Ami Yassor, [email protected]
Abel Gordon, [email protected]
Muli Ben-Yehuda, [email protected]

With contributions by:
Anthony Liguori, [email protected]
Mike Day, [email protected]

This work was inspired by the nested SVM support by Alexander Graf and Joerg Roedel.

Changes since v2:
Added check to nested_vmx_get_msr.
Static initialization of the vmcs_field_to_offset_table array. Use the memory allocated by L1 for VMCS12 to store the shadow vmcs. Some optimization to the prepare_vmcs_12 function.

vpid allocation will be updated with the multiguest support (work in progress). We are working on fixing the cr0.TS handling, it works for nested kvm by not for vmware server.

#1 lore raw

PATCH 1/5 Nested VMX patch 1 implements vmon and vmoff

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/svm.c |    3 -
 arch/x86/kvm/vmx.c |  217 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c |    6 +-
 arch/x86/kvm/x86.h |    2 +
 4 files changed, 222 insertions(+), 6 deletions(-)
arch/x86/kvm/svm.c +0 -3
index 2df9b45..3c1f22a 100644
@@ -124,9 +124,6 @@ static int npt = 1;
 
 module_param(npt, int, S_IRUGO);
 
-static int nested = 1;
-module_param(nested, int, S_IRUGO);
-
 static void svm_flush_tlb(struct kvm_vcpu *vcpu);
 static void svm_complete_interrupts(struct vcpu_svm *svm);
 
arch/x86/kvm/vmx.c +215 -2

271 lines not on this page · raw message

arch/x86/kvm/x86.c +5 -1
index 8b3a169..9c39092 100644
@@ -87,6 +87,10 @@ EXPORT_SYMBOL_GPL(kvm_x86_ops);
 int ignore_msrs = 0;
 module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
 
+int nested = 1;
+EXPORT_SYMBOL_GPL(nested);
+module_param(nested, int, S_IRUGO);
+
 struct kvm_stats_debugfs_item debugfs_entries[] = {
 	{ "pf_fixed", VCPU_STAT(pf_fixed) },
 	{ "pf_guest", VCPU_STAT(pf_guest) },
@@ -373,7 +377,7 @@ void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
 		return;
 	}
 
-	if (cr4 & X86_CR4_VMXE) {
+	if (cr4 & X86_CR4_VMXE && !nested) {
 		printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
 		kvm_inject_gp(vcpu, 0);
 		return;
arch/x86/kvm/x86.h +2 -0
index 5eadea5..57204cb 100644
@@ -35,4 +35,6 @@ static inline bool kvm_exception_is_soft(unsigned int nr)
 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
                                              u32 function, u32 index);
 
+extern int nested;
+
 #endif
Signature · 1 line
1.6.0.4
#2 lore raw

PATCH 2/5 Nested VMX patch 2 implements vmclear

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 65 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +65 -5
index 71bd91a..411cbdb 100644
@@ -61,15 +61,26 @@ module_param_named(unrestricted_guest,
 static int __read_mostly emulate_invalid_guest_state = 0;
 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
 
-struct vmcs {
-	u32 revision_id;
-	u32 abort;
-	char data[0];
+struct __attribute__ ((__packed__)) level_state {
+	/* Has the level1 guest done vmclear? */
+	bool vmclear;
 };
 
 struct nested_vmx {
 	/* Has the level1 guest done vmxon? */
 	bool vmxon;
+
+	/*
+	 * Level 2 state : includes vmcs,registers and
+	 * a copy of vmcs12 for vmread/vmwrite
+	 */
+	struct level_state *l2_state;
+};
+
+struct vmcs {
+	u32 revision_id;
+	u32 abort;
+	char data[0];
 };
 
 struct vcpu_vmx {
@@ -186,6 +197,8 @@ static struct kvm_vmx_segment_field {
 
 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
 
+static int create_l2_state(struct kvm_vcpu *vcpu);
+
 /*
  * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
  * away by decrementing the array size.
@@ -1293,6 +1306,30 @@ static void vmclear_local_vcpus(void)
 		__vcpu_clear(vmx);
 }
 
+struct level_state *create_state(void)
+{
+	struct level_state *state = NULL;
+
+	state = kzalloc(sizeof(struct level_state), GFP_KERNEL);
+	if (!state) {
+		printk(KERN_INFO "Error create level state\n");
+		return NULL;
+	}
+	return state;
+}
+
+int create_l2_state(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!vmx->nested.l2_state) {
+		vmx->nested.l2_state = create_state();
+		if (!vmx->nested.l2_state)
+			return -ENOMEM;
+	}
+
+	return 0;
+}
 
 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  * tricks.
@@ -3261,6 +3298,27 @@ static int handle_vmx_insn(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static void clear_rflags_cf_zf(struct kvm_vcpu *vcpu)
+{
+	unsigned long rflags;
+	rflags = vmx_get_rflags(vcpu);
+	rflags &= ~(X86_EFLAGS_CF | X86_EFLAGS_ZF);
+	vmx_set_rflags(vcpu, rflags);
+}
+
+static int handle_vmclear(struct kvm_vcpu *vcpu)
+{
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	to_vmx(vcpu)->nested.l2_state->vmclear = 1;
+
+	skip_emulated_instruction(vcpu);
+	clear_rflags_cf_zf(vcpu);
+
+	return 1;
+}
+
 static int handle_vmoff(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -3310,6 +3368,8 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 
 	vmx->nested.vmxon = 1;
 
+	create_l2_state(vcpu);
+
 	skip_emulated_instruction(vcpu);
 	return 1;
 }
@@ -3582,7 +3642,7 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_HLT]                     = handle_halt,
 	[EXIT_REASON_INVLPG]		      = handle_invlpg,
 	[EXIT_REASON_VMCALL]                  = handle_vmcall,
-	[EXIT_REASON_VMCLEAR]	              = handle_vmx_insn,
+	[EXIT_REASON_VMCLEAR]	              = handle_vmclear,
 	[EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
 	[EXIT_REASON_VMPTRLD]                 = handle_vmx_insn,
 	[EXIT_REASON_VMPTRST]                 = handle_vmx_insn,
Signature · 1 line
1.6.0.4
#3 lore raw

PATCH 3/5 Nested VMX patch 3 implements vmptrld and vmptrst

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c |  468 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 arch/x86/kvm/x86.c |    3 +-
 2 files changed, 459 insertions(+), 12 deletions(-)
arch/x86/kvm/vmx.c +457 -11
index 411cbdb..8c186e0 100644
@@ -61,20 +61,168 @@ module_param_named(unrestricted_guest,
 static int __read_mostly emulate_invalid_guest_state = 0;
 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
 
+
+struct __attribute__ ((__packed__)) shadow_vmcs {
+	u32 revision_id;
+	u32 abort;
+	u16 virtual_processor_id;
+	u16 guest_es_selector;
+	u16 guest_cs_selector;
+	u16 guest_ss_selector;
+	u16 guest_ds_selector;
+	u16 guest_fs_selector;
+	u16 guest_gs_selector;
+	u16 guest_ldtr_selector;
+	u16 guest_tr_selector;
+	u16 host_es_selector;
+	u16 host_cs_selector;
+	u16 host_ss_selector;
+	u16 host_ds_selector;
+	u16 host_fs_selector;
+	u16 host_gs_selector;
+	u16 host_tr_selector;
+	u64 io_bitmap_a;
+	u64 io_bitmap_b;
+	u64 msr_bitmap;
+	u64 vm_exit_msr_store_addr;
+	u64 vm_exit_msr_load_addr;
+	u64 vm_entry_msr_load_addr;
+	u64 tsc_offset;
+	u64 virtual_apic_page_addr;
+	u64 apic_access_addr;
+	u64 ept_pointer;
+	u64 guest_physical_address;
+	u64 vmcs_link_pointer;
+	u64 guest_ia32_debugctl;
+	u64 guest_ia32_pat;
+	u64 guest_pdptr0;
+	u64 guest_pdptr1;
+	u64 guest_pdptr2;
+	u64 guest_pdptr3;
+	u64 host_ia32_pat;
+	u32 pin_based_vm_exec_control;
+	u32 cpu_based_vm_exec_control;
+	u32 exception_bitmap;
+	u32 page_fault_error_code_mask;
+	u32 page_fault_error_code_match;
+	u32 cr3_target_count;
+	u32 vm_exit_controls;
+	u32 vm_exit_msr_store_count;
+	u32 vm_exit_msr_load_count;
+	u32 vm_entry_controls;
+	u32 vm_entry_msr_load_count;
+	u32 vm_entry_intr_info_field;
+	u32 vm_entry_exception_error_code;
+	u32 vm_entry_instruction_len;
+	u32 tpr_threshold;
+	u32 secondary_vm_exec_control;
+	u32 vm_instruction_error;
+	u32 vm_exit_reason;
+	u32 vm_exit_intr_info;
+	u32 vm_exit_intr_error_code;
+	u32 idt_vectoring_info_field;
+	u32 idt_vectoring_error_code;
+	u32 vm_exit_instruction_len;
+	u32 vmx_instruction_info;
+	u32 guest_es_limit;
+	u32 guest_cs_limit;
+	u32 guest_ss_limit;
+	u32 guest_ds_limit;
+	u32 guest_fs_limit;
+	u32 guest_gs_limit;
+	u32 guest_ldtr_limit;
+	u32 guest_tr_limit;
+	u32 guest_gdtr_limit;
+	u32 guest_idtr_limit;
+	u32 guest_es_ar_bytes;
+	u32 guest_cs_ar_bytes;
+	u32 guest_ss_ar_bytes;
+	u32 guest_ds_ar_bytes;
+	u32 guest_fs_ar_bytes;
+	u32 guest_gs_ar_bytes;
+	u32 guest_ldtr_ar_bytes;
+	u32 guest_tr_ar_bytes;
+	u32 guest_interruptibility_info;
+	u32 guest_activity_state;
+	u32 guest_sysenter_cs;
+	u32 host_ia32_sysenter_cs;
+	unsigned long cr0_guest_host_mask;
+	unsigned long cr4_guest_host_mask;
+	unsigned long cr0_read_shadow;
+	unsigned long cr4_read_shadow;
+	unsigned long cr3_target_value0;
+	unsigned long cr3_target_value1;
+	unsigned long cr3_target_value2;
+	unsigned long cr3_target_value3;
+	unsigned long exit_qualification;
+	unsigned long guest_linear_address;
+	unsigned long guest_cr0;
+	unsigned long guest_cr3;
+	unsigned long guest_cr4;
+	unsigned long guest_es_base;
+	unsigned long guest_cs_base;
+	unsigned long guest_ss_base;
+	unsigned long guest_ds_base;
+	unsigned long guest_fs_base;
+	unsigned long guest_gs_base;
+	unsigned long guest_ldtr_base;
+	unsigned long guest_tr_base;
+	unsigned long guest_gdtr_base;
+	unsigned long guest_idtr_base;
+	unsigned long guest_dr7;
+	unsigned long guest_rsp;
+	unsigned long guest_rip;
+	unsigned long guest_rflags;
+	unsigned long guest_pending_dbg_exceptions;
+	unsigned long guest_sysenter_esp;
+	unsigned long guest_sysenter_eip;
+	unsigned long host_cr0;
+	unsigned long host_cr3;
+	unsigned long host_cr4;
+	unsigned long host_fs_base;
+	unsigned long host_gs_base;
+	unsigned long host_tr_base;
+	unsigned long host_gdtr_base;
+	unsigned long host_idtr_base;
+	unsigned long host_ia32_sysenter_esp;
+	unsigned long host_ia32_sysenter_eip;
+	unsigned long host_rsp;
+	unsigned long host_rip;
+};
+
 struct __attribute__ ((__packed__)) level_state {
 	/* Has the level1 guest done vmclear? */
 	bool vmclear;
+	u16 vpid;
+	u64 shadow_efer;
+	unsigned long cr2;
+	unsigned long cr3;
+	unsigned long cr4;
+	unsigned long cr8;
+
+	u64 io_bitmap_a;
+	u64 io_bitmap_b;
+	u64 msr_bitmap;
+
+	struct shadow_vmcs *shadow_vmcs;
+
+	struct vmcs *vmcs;
+	int cpu;
+	int launched;
 };
 
 struct nested_vmx {
 	/* Has the level1 guest done vmxon? */
 	bool vmxon;
-
+	/* What is the location of the  vmcs l1 keeps for l2? (in level1 gpa) */
+	u64 vmptr;
 	/*
 	 * Level 2 state : includes vmcs,registers and
 	 * a copy of vmcs12 for vmread/vmwrite
 	 */
 	struct level_state *l2_state;
+	/* Level 1 state for switching to level 2 and back */
+	struct level_state *l1_state;
 };
 
 struct vmcs {
@@ -140,6 +288,25 @@ static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
 	return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+static struct page *nested_get_page(struct kvm_vcpu *vcpu,
+				    u64 vmcs_addr)
+{
+	struct page *vmcs_page = NULL;
+
+	down_read(&current->mm->mmap_sem);
+	vmcs_page = gfn_to_page(vcpu->kvm, vmcs_addr >> PAGE_SHIFT);
+	up_read(&current->mm->mmap_sem);
+
+	if (is_error_page(vmcs_page)) {
+		printk(KERN_ERR "%s error allocating page \n", __func__);
+		kvm_release_page_clean(vmcs_page);
+		return NULL;
+	}
+
+	return vmcs_page;
+
+}
+
 static int init_rmode(struct kvm *kvm);
 static u64 construct_eptp(unsigned long root_hpa);
 
@@ -197,6 +364,8 @@ static struct kvm_vmx_segment_field {
 
 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
 
+static int nested_vmx_check_permission(struct kvm_vcpu *vcpu);
+static int create_l1_state(struct kvm_vcpu *vcpu);
 static int create_l2_state(struct kvm_vcpu *vcpu);
 
 /*
@@ -715,6 +884,24 @@ static void vmx_load_host_state(struct vcpu_vmx *vmx)
 	preempt_enable();
 }
 
+
+static int vmptrld(struct kvm_vcpu *vcpu,
+		   u64 phys_addr)
+{
+	u8 error;
+
+	asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
+		      : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
+		      : "cc");
+	if (error) {
+		printk(KERN_ERR "kvm: %s vmptrld %llx failed\n",
+		       __func__, phys_addr);
+		return 1;
+	}
+
+	return 0;
+}
+
 /*
  * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  * vcpu mutex is already taken.
@@ -736,15 +923,8 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 	}
 
 	if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
-		u8 error;
-
 		per_cpu(current_vmcs, cpu) = vmx->vmcs;
-		asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
-			      : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
-			      : "cc");
-		if (error)
-			printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
-			       vmx->vmcs, phys_addr);
+		vmptrld(vcpu, phys_addr);
 	}
 
 	if (vcpu->cpu != cpu) {
@@ -1318,6 +1498,28 @@ struct level_state *create_state(void)
 	return state;
 }
 
+int create_l1_state(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!vmx->nested.l1_state) {
+		vmx->nested.l1_state = create_state();
+		if (!vmx->nested.l1_state)
+			return -ENOMEM;
+	} else
+		return 0;
+
+	vmx->nested.l1_state->shadow_vmcs = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!vmx->nested.l1_state->shadow_vmcs) {
+		printk(KERN_INFO "%s error creating shadow vmcs\n",
+		       __func__);
+		kfree(vmx->nested.l1_state);
+		return -ENOMEM;
+	}
+	return 0;
+}
+
+static struct vmcs *alloc_vmcs(void);
 int create_l2_state(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -1326,11 +1528,53 @@ int create_l2_state(struct kvm_vcpu *vcpu)
 		vmx->nested.l2_state = create_state();
 		if (!vmx->nested.l2_state)
 			return -ENOMEM;
+	} else
+		return 0;
+
+	vmx->nested.l2_state->vmcs = alloc_vmcs();
+	if (!vmx->nested.l2_state->vmcs) {
+		printk(KERN_ERR "%s error in creating level 2 vmcs", __func__);
+		kfree(vmx->nested.l2_state);
+		return -ENOMEM;
 	}
 
+	if (cpu_has_vmx_msr_bitmap())
+		vmx->nested.l2_state->msr_bitmap = vmcs_read64(MSR_BITMAP);
+	else
+		vmx->nested.l2_state->msr_bitmap = 0;
+
+	vmx->nested.l2_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+	vmx->nested.l2_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+
 	return 0;
 }
 
+int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
+			struct kvm_vcpu *vcpu);
+
+int read_guest_vmcs_gpa(struct kvm_vcpu *vcpu, u64 *gentry)
+{
+
+	int r = 0;
+
+	r = kvm_read_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX], gentry,
+				sizeof(u64), vcpu);
+	if (r) {
+		printk(KERN_ERR "%s cannot read guest vmcs addr %lx : %d\n",
+		       __func__, vcpu->arch.regs[VCPU_REGS_RAX], r);
+		return r;
+	}
+
+	if (!IS_ALIGNED(*gentry, PAGE_SIZE)) {
+		printk(KERN_DEBUG "%s addr %llx not aligned\n",
+		       __func__, *gentry);
+		return 1;
+	}
+
+	return 0;
+}
+
+
 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  * tricks.
  */
@@ -3374,6 +3618,66 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_vmptrld(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct page *vmcs_page;
+	u64 guest_vmcs_addr;
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (read_guest_vmcs_gpa(vcpu, &guest_vmcs_addr))
+		return 1;
+
+	if (create_l1_state(vcpu)) {
+		printk(KERN_ERR "%s create_l1_state failed\n", __func__);
+		return 1;
+	}
+
+	if (create_l2_state(vcpu)) {
+		printk(KERN_ERR "%s create_l2_state failed\n", __func__);
+		return 1;
+	}
+
+	if (vmx->nested.vmptr != guest_vmcs_addr) {
+		/* checking vmptr address */
+		vmcs_page = nested_get_page(vcpu, guest_vmcs_addr);
+		if (vmcs_page == NULL)
+			return 1;
+
+		vmx->nested.vmptr = guest_vmcs_addr;
+
+		kvm_release_page_clean(vmcs_page);
+	}
+
+	clear_rflags_cf_zf(vcpu);
+	skip_emulated_instruction(vcpu);
+	return 1;
+}
+
+int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
+			 struct kvm_vcpu *vcpu);
+
+static int handle_vmptrst(struct kvm_vcpu *vcpu)
+{
+	int r = 0;
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	r = kvm_write_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX],
+				 (void *)&to_vmx(vcpu)->nested.vmptr,
+				 sizeof(u64), vcpu);
+	if (r) {
+		printk(KERN_INFO "%s failed to write vmptr\n", __func__);
+		return 1;
+	}
+	clear_rflags_cf_zf(vcpu);
+	skip_emulated_instruction(vcpu);
+	return 1;
+}
+
 static int handle_invlpg(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -3644,8 +3948,8 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_VMCALL]                  = handle_vmcall,
 	[EXIT_REASON_VMCLEAR]	              = handle_vmclear,
 	[EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
-	[EXIT_REASON_VMPTRLD]                 = handle_vmx_insn,
-	[EXIT_REASON_VMPTRST]                 = handle_vmx_insn,
+	[EXIT_REASON_VMPTRLD]                 = handle_vmptrld,
+	[EXIT_REASON_VMPTRST]                 = handle_vmptrst,
 	[EXIT_REASON_VMREAD]                  = handle_vmx_insn,
 	[EXIT_REASON_VMRESUME]                = handle_vmx_insn,
 	[EXIT_REASON_VMWRITE]                 = handle_vmx_insn,
@@ -4183,6 +4487,148 @@ static bool vmx_gb_page_enable(void)
 	return false;
 }
 
+void save_vmcs(struct shadow_vmcs *dst)
+{
+	dst->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
+	dst->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
+	dst->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
+	dst->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
+	dst->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
+	dst->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
+	dst->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
+	dst->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
+	dst->host_es_selector = vmcs_read16(HOST_ES_SELECTOR);
+	dst->host_cs_selector = vmcs_read16(HOST_CS_SELECTOR);
+	dst->host_ss_selector = vmcs_read16(HOST_SS_SELECTOR);
+	dst->host_ds_selector = vmcs_read16(HOST_DS_SELECTOR);
+	dst->host_fs_selector = vmcs_read16(HOST_FS_SELECTOR);
+	dst->host_gs_selector = vmcs_read16(HOST_GS_SELECTOR);
+	dst->host_tr_selector = vmcs_read16(HOST_TR_SELECTOR);
+	dst->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+	dst->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+	if (cpu_has_vmx_msr_bitmap())
+		dst->msr_bitmap = vmcs_read64(MSR_BITMAP);
+
+	dst->vm_exit_msr_store_addr = vmcs_read64(VM_EXIT_MSR_STORE_ADDR);
+	dst->vm_exit_msr_load_addr = vmcs_read64(VM_EXIT_MSR_LOAD_ADDR);
+	dst->vm_entry_msr_load_addr = vmcs_read64(VM_ENTRY_MSR_LOAD_ADDR);
+	dst->tsc_offset = vmcs_read64(TSC_OFFSET);
+	dst->virtual_apic_page_addr = vmcs_read64(VIRTUAL_APIC_PAGE_ADDR);
+	dst->apic_access_addr = vmcs_read64(APIC_ACCESS_ADDR);
+	if (enable_ept)
+		dst->ept_pointer = vmcs_read64(EPT_POINTER);
+
+	dst->guest_physical_address = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
+	dst->vmcs_link_pointer = vmcs_read64(VMCS_LINK_POINTER);
+	dst->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+	if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
+		dst->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
+	if (enable_ept) {
+		dst->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
+		dst->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
+		dst->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
+		dst->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
+	}
+	dst->pin_based_vm_exec_control = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
+	dst->cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
+	dst->exception_bitmap = vmcs_read32(EXCEPTION_BITMAP);
+	dst->page_fault_error_code_mask =
+		vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK);
+	dst->page_fault_error_code_match =
+		vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH);
+	dst->cr3_target_count = vmcs_read32(CR3_TARGET_COUNT);
+	dst->vm_exit_controls = vmcs_read32(VM_EXIT_CONTROLS);
+	dst->vm_exit_msr_store_count = vmcs_read32(VM_EXIT_MSR_STORE_COUNT);
+	dst->vm_exit_msr_load_count = vmcs_read32(VM_EXIT_MSR_LOAD_COUNT);
+	dst->vm_entry_controls = vmcs_read32(VM_ENTRY_CONTROLS);
+	dst->vm_entry_msr_load_count = vmcs_read32(VM_ENTRY_MSR_LOAD_COUNT);
+	dst->vm_entry_intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
+	dst->vm_entry_exception_error_code =
+		vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE);
+	dst->vm_entry_instruction_len = vmcs_read32(VM_ENTRY_INSTRUCTION_LEN);
+	dst->tpr_threshold = vmcs_read32(TPR_THRESHOLD);
+	dst->secondary_vm_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
+	if (enable_vpid && dst->secondary_vm_exec_control &
+	    SECONDARY_EXEC_ENABLE_VPID)
+		dst->virtual_processor_id = vmcs_read16(VIRTUAL_PROCESSOR_ID);
+	dst->vm_instruction_error = vmcs_read32(VM_INSTRUCTION_ERROR);
+	dst->vm_exit_reason  = vmcs_read32(VM_EXIT_REASON);
+	dst->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+	dst->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
+	dst->idt_vectoring_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
+	dst->idt_vectoring_error_code = vmcs_read32(IDT_VECTORING_ERROR_CODE);
+	dst->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
+	dst->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
+	dst->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
+	dst->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
+	dst->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
+	dst->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
+	dst->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
+	dst->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
+	dst->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
+	dst->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
+	dst->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
+	dst->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
+	dst->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
+	dst->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
+	dst->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
+	dst->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
+	dst->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
+	dst->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
+	dst->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
+	dst->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
+	dst->guest_interruptibility_info =
+		vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+	dst->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
+	dst->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
+	dst->host_ia32_sysenter_cs = vmcs_read32(HOST_IA32_SYSENTER_CS);
+	dst->cr0_guest_host_mask = vmcs_readl(CR0_GUEST_HOST_MASK);
+	dst->cr4_guest_host_mask = vmcs_readl(CR4_GUEST_HOST_MASK);
+	dst->cr0_read_shadow = vmcs_readl(CR0_READ_SHADOW);
+	dst->cr4_read_shadow = vmcs_readl(CR4_READ_SHADOW);
+	dst->cr3_target_value0 = vmcs_readl(CR3_TARGET_VALUE0);
+	dst->cr3_target_value1 = vmcs_readl(CR3_TARGET_VALUE1);
+	dst->cr3_target_value2 = vmcs_readl(CR3_TARGET_VALUE2);
+	dst->cr3_target_value3 = vmcs_readl(CR3_TARGET_VALUE3);
+	dst->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	dst->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
+	dst->guest_cr0 = vmcs_readl(GUEST_CR0);
+	dst->guest_cr3 = vmcs_readl(GUEST_CR3);
+	dst->guest_cr4 = vmcs_readl(GUEST_CR4);
+	dst->guest_es_base = vmcs_readl(GUEST_ES_BASE);
+	dst->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
+	dst->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
+	dst->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
+	dst->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
+	dst->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
+	dst->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
+	dst->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
+	dst->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
+	dst->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
+	dst->guest_dr7 = vmcs_readl(GUEST_DR7);
+	dst->guest_rsp = vmcs_readl(GUEST_RSP);
+	dst->guest_rip = vmcs_readl(GUEST_RIP);
+	dst->guest_rflags = vmcs_readl(GUEST_RFLAGS);
+	dst->guest_pending_dbg_exceptions =
+		vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
+	dst->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
+	dst->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
+	dst->host_cr0 = vmcs_readl(HOST_CR0);
+	dst->host_cr3 = vmcs_readl(HOST_CR3);
+	dst->host_cr4 = vmcs_readl(HOST_CR4);
+	dst->host_fs_base = vmcs_readl(HOST_FS_BASE);
+	dst->host_gs_base = vmcs_readl(HOST_GS_BASE);
+	dst->host_tr_base = vmcs_readl(HOST_TR_BASE);
+	dst->host_gdtr_base = vmcs_readl(HOST_GDTR_BASE);
+	dst->host_idtr_base = vmcs_readl(HOST_IDTR_BASE);
+	dst->host_ia32_sysenter_esp = vmcs_readl(HOST_IA32_SYSENTER_ESP);
+	dst->host_ia32_sysenter_eip = vmcs_readl(HOST_IA32_SYSENTER_EIP);
+	dst->host_rsp = vmcs_readl(HOST_RSP);
+	dst->host_rip = vmcs_readl(HOST_RIP);
+	if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT)
+		dst->host_ia32_pat = vmcs_read64(HOST_IA32_PAT);
+}
+
 static struct kvm_x86_ops vmx_x86_ops = {
 	.cpu_has_kvm_support = cpu_has_kvm_support,
 	.disabled_by_bios = vmx_disabled_by_bios,
arch/x86/kvm/x86.c +2 -1
index 9c39092..74eb888 100644
@@ -2473,6 +2473,7 @@ static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
 out:
 	return r;
 }
+EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
 
 static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
 				struct kvm_vcpu *vcpu)
@@ -2503,7 +2504,7 @@ static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
 out:
 	return r;
 }
-
+EXPORT_SYMBOL_GPL(kvm_write_guest_virt);
 
 static int emulator_read_emulated(unsigned long addr,
 				  void *val,
Signature · 1 line
1.6.0.4
#4 lore raw

PATCH 4/5 Nested VMX patch 4 implements vmread and vmwrite

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c |  591 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 589 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +589 -2
index 8c186e0..6a4c252 100644
@@ -225,6 +225,21 @@ struct nested_vmx {
 	struct level_state *l1_state;
 };
 
+enum vmcs_field_type {
+	VMCS_FIELD_TYPE_U16 = 0,
+	VMCS_FIELD_TYPE_U64 = 1,
+	VMCS_FIELD_TYPE_U32 = 2,
+	VMCS_FIELD_TYPE_ULONG = 3
+};
+
+#define VMCS_FIELD_LENGTH_OFFSET 13
+#define VMCS_FIELD_LENGTH_MASK 0x6000
+
+static inline int vmcs_field_length(unsigned long field)
+{
+	return (VMCS_FIELD_LENGTH_MASK & field) >> 13;
+}
+
 struct vmcs {
 	u32 revision_id;
 	u32 abort;
@@ -288,6 +303,404 @@ static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
 	return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+#define SHADOW_VMCS_OFFSET(x) offsetof(struct shadow_vmcs, x)
+
+static unsigned short vmcs_field_to_offset_table[HOST_RIP+1] = {
+
+	[VIRTUAL_PROCESSOR_ID] =
+		SHADOW_VMCS_OFFSET(virtual_processor_id),
+	[GUEST_ES_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_es_selector),
+	[GUEST_CS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_cs_selector),
+	[GUEST_SS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_ss_selector),
+	[GUEST_DS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_ds_selector),
+	[GUEST_FS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_fs_selector),
+	[GUEST_GS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_gs_selector),
+	[GUEST_LDTR_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_ldtr_selector),
+	[GUEST_TR_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_tr_selector),
+	[HOST_ES_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_es_selector),
+	[HOST_CS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_cs_selector),
+	[HOST_SS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_ss_selector),
+	[HOST_DS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_ds_selector),
+	[HOST_FS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_fs_selector),
+	[HOST_GS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_gs_selector),
+	[HOST_TR_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_tr_selector),
+	[IO_BITMAP_A] =
+		SHADOW_VMCS_OFFSET(io_bitmap_a),
+	[IO_BITMAP_A_HIGH] =
+		SHADOW_VMCS_OFFSET(io_bitmap_a)+4,
+	[IO_BITMAP_B] =
+		SHADOW_VMCS_OFFSET(io_bitmap_b),
+	[IO_BITMAP_B_HIGH] =
+		SHADOW_VMCS_OFFSET(io_bitmap_b)+4,
+	[MSR_BITMAP] =
+		SHADOW_VMCS_OFFSET(msr_bitmap),
+	[MSR_BITMAP_HIGH] =
+		SHADOW_VMCS_OFFSET(msr_bitmap)+4,
+	[VM_EXIT_MSR_STORE_ADDR] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_store_addr),
+	[VM_EXIT_MSR_STORE_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_store_addr)+4,
+	[VM_EXIT_MSR_LOAD_ADDR] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_load_addr),
+	[VM_EXIT_MSR_LOAD_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_load_addr)+4,
+	[VM_ENTRY_MSR_LOAD_ADDR] =
+		SHADOW_VMCS_OFFSET(vm_entry_msr_load_addr),
+	[VM_ENTRY_MSR_LOAD_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(vm_entry_msr_load_addr)+4,
+	[TSC_OFFSET] =
+		SHADOW_VMCS_OFFSET(tsc_offset),
+	[TSC_OFFSET_HIGH] =
+		SHADOW_VMCS_OFFSET(tsc_offset)+4,
+	[VIRTUAL_APIC_PAGE_ADDR] =
+		SHADOW_VMCS_OFFSET(virtual_apic_page_addr),
+	[VIRTUAL_APIC_PAGE_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(virtual_apic_page_addr)+4,
+	[APIC_ACCESS_ADDR] =
+		SHADOW_VMCS_OFFSET(apic_access_addr),
+	[APIC_ACCESS_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(apic_access_addr)+4,
+	[EPT_POINTER] =
+		SHADOW_VMCS_OFFSET(ept_pointer),
+	[EPT_POINTER_HIGH] =
+		SHADOW_VMCS_OFFSET(ept_pointer)+4,
+	[GUEST_PHYSICAL_ADDRESS] =
+		SHADOW_VMCS_OFFSET(guest_physical_address),
+	[GUEST_PHYSICAL_ADDRESS_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_physical_address)+4,
+	[VMCS_LINK_POINTER] =
+		SHADOW_VMCS_OFFSET(vmcs_link_pointer),
+	[VMCS_LINK_POINTER_HIGH] =
+		SHADOW_VMCS_OFFSET(vmcs_link_pointer)+4,
+	[GUEST_IA32_DEBUGCTL] =
+		SHADOW_VMCS_OFFSET(guest_ia32_debugctl),
+	[GUEST_IA32_DEBUGCTL_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_ia32_debugctl)+4,
+	[GUEST_IA32_PAT] =
+		SHADOW_VMCS_OFFSET(guest_ia32_pat),
+	[GUEST_IA32_PAT_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_ia32_pat)+4,
+	[GUEST_PDPTR0] =
+		SHADOW_VMCS_OFFSET(guest_pdptr0),
+	[GUEST_PDPTR0_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_pdptr0)+4,
+	[GUEST_PDPTR1] =
+		SHADOW_VMCS_OFFSET(guest_pdptr1),
+	[GUEST_PDPTR1_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_pdptr1)+4,
+	[GUEST_PDPTR2] =
+		SHADOW_VMCS_OFFSET(guest_pdptr2),
+	[GUEST_PDPTR2_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_pdptr2)+4,
+	[GUEST_PDPTR3] =
+		SHADOW_VMCS_OFFSET(guest_pdptr3),
+	[GUEST_PDPTR3_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_pdptr3)+4,
+	[HOST_IA32_PAT] =
+		SHADOW_VMCS_OFFSET(host_ia32_pat),
+	[HOST_IA32_PAT_HIGH] =
+		SHADOW_VMCS_OFFSET(host_ia32_pat)+4,
+	[PIN_BASED_VM_EXEC_CONTROL] =
+		SHADOW_VMCS_OFFSET(pin_based_vm_exec_control),
+	[CPU_BASED_VM_EXEC_CONTROL] =
+		SHADOW_VMCS_OFFSET(cpu_based_vm_exec_control),
+	[EXCEPTION_BITMAP] =
+		SHADOW_VMCS_OFFSET(exception_bitmap),
+	[PAGE_FAULT_ERROR_CODE_MASK] =
+		SHADOW_VMCS_OFFSET(page_fault_error_code_mask),
+	[PAGE_FAULT_ERROR_CODE_MATCH] =
+		SHADOW_VMCS_OFFSET(page_fault_error_code_match),
+	[CR3_TARGET_COUNT] =
+		SHADOW_VMCS_OFFSET(cr3_target_count),
+	[VM_EXIT_CONTROLS] =
+		SHADOW_VMCS_OFFSET(vm_exit_controls),
+	[VM_EXIT_MSR_STORE_COUNT] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_store_count),
+	[VM_EXIT_MSR_LOAD_COUNT] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_load_count),
+	[VM_ENTRY_CONTROLS] =
+		SHADOW_VMCS_OFFSET(vm_entry_controls),
+	[VM_ENTRY_MSR_LOAD_COUNT] =
+		SHADOW_VMCS_OFFSET(vm_entry_msr_load_count),
+	[VM_ENTRY_INTR_INFO_FIELD] =
+		SHADOW_VMCS_OFFSET(vm_entry_intr_info_field),
+	[VM_ENTRY_EXCEPTION_ERROR_CODE] =
+		SHADOW_VMCS_OFFSET(vm_entry_exception_error_code),
+	[VM_ENTRY_INSTRUCTION_LEN] =
+		SHADOW_VMCS_OFFSET(vm_entry_instruction_len),
+	[TPR_THRESHOLD] =
+		SHADOW_VMCS_OFFSET(tpr_threshold),
+	[SECONDARY_VM_EXEC_CONTROL] =
+		SHADOW_VMCS_OFFSET(secondary_vm_exec_control),
+	[VM_INSTRUCTION_ERROR] =
+		SHADOW_VMCS_OFFSET(vm_instruction_error),
+	[VM_EXIT_REASON] =
+		SHADOW_VMCS_OFFSET(vm_exit_reason),
+	[VM_EXIT_INTR_INFO] =
+		SHADOW_VMCS_OFFSET(vm_exit_intr_info),
+	[VM_EXIT_INTR_ERROR_CODE] =
+		SHADOW_VMCS_OFFSET(vm_exit_intr_error_code),
+	[IDT_VECTORING_INFO_FIELD] =
+		SHADOW_VMCS_OFFSET(idt_vectoring_info_field),
+	[IDT_VECTORING_ERROR_CODE] =
+		SHADOW_VMCS_OFFSET(idt_vectoring_error_code),
+	[VM_EXIT_INSTRUCTION_LEN] =
+		SHADOW_VMCS_OFFSET(vm_exit_instruction_len),
+	[VMX_INSTRUCTION_INFO] =
+		SHADOW_VMCS_OFFSET(vmx_instruction_info),
+	[GUEST_ES_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_es_limit),
+	[GUEST_CS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_cs_limit),
+	[GUEST_SS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_ss_limit),
+	[GUEST_DS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_ds_limit),
+	[GUEST_FS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_fs_limit),
+	[GUEST_GS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_gs_limit),
+	[GUEST_LDTR_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_ldtr_limit),
+	[GUEST_TR_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_tr_limit),
+	[GUEST_GDTR_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_gdtr_limit),
+	[GUEST_IDTR_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_idtr_limit),
+	[GUEST_ES_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_es_ar_bytes),
+	[GUEST_CS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_cs_ar_bytes),
+	[GUEST_SS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_ss_ar_bytes),
+	[GUEST_DS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_ds_ar_bytes),
+	[GUEST_FS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_fs_ar_bytes),
+	[GUEST_GS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_gs_ar_bytes),
+	[GUEST_LDTR_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_ldtr_ar_bytes),
+	[GUEST_TR_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_tr_ar_bytes),
+	[GUEST_INTERRUPTIBILITY_INFO] =
+		SHADOW_VMCS_OFFSET(guest_interruptibility_info),
+	[GUEST_ACTIVITY_STATE] =
+		SHADOW_VMCS_OFFSET(guest_activity_state),
+	[GUEST_SYSENTER_CS] =
+		SHADOW_VMCS_OFFSET(guest_sysenter_cs),
+	[HOST_IA32_SYSENTER_CS] =
+		SHADOW_VMCS_OFFSET(host_ia32_sysenter_cs),
+	[CR0_GUEST_HOST_MASK] =
+		SHADOW_VMCS_OFFSET(cr0_guest_host_mask),
+	[CR4_GUEST_HOST_MASK] =
+		SHADOW_VMCS_OFFSET(cr4_guest_host_mask),
+	[CR0_READ_SHADOW] =
+		SHADOW_VMCS_OFFSET(cr0_read_shadow),
+	[CR4_READ_SHADOW] =
+		SHADOW_VMCS_OFFSET(cr4_read_shadow),
+	[CR3_TARGET_VALUE0] =
+		SHADOW_VMCS_OFFSET(cr3_target_value0),
+	[CR3_TARGET_VALUE1] =
+		SHADOW_VMCS_OFFSET(cr3_target_value1),
+	[CR3_TARGET_VALUE2] =
+		SHADOW_VMCS_OFFSET(cr3_target_value2),
+	[CR3_TARGET_VALUE3] =
+		SHADOW_VMCS_OFFSET(cr3_target_value3),
+	[EXIT_QUALIFICATION] =
+		SHADOW_VMCS_OFFSET(exit_qualification),
+	[GUEST_LINEAR_ADDRESS] =
+		SHADOW_VMCS_OFFSET(guest_linear_address),
+	[GUEST_CR0] =
+		SHADOW_VMCS_OFFSET(guest_cr0),
+	[GUEST_CR3] =
+		SHADOW_VMCS_OFFSET(guest_cr3),
+	[GUEST_CR4] =
+		SHADOW_VMCS_OFFSET(guest_cr4),
+	[GUEST_ES_BASE] =
+		SHADOW_VMCS_OFFSET(guest_es_base),
+	[GUEST_CS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_cs_base),
+	[GUEST_SS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_ss_base),
+	[GUEST_DS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_ds_base),
+	[GUEST_FS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_fs_base),
+	[GUEST_GS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_gs_base),
+	[GUEST_LDTR_BASE] =
+		SHADOW_VMCS_OFFSET(guest_ldtr_base),
+	[GUEST_TR_BASE] =
+		SHADOW_VMCS_OFFSET(guest_tr_base),
+	[GUEST_GDTR_BASE] =
+		SHADOW_VMCS_OFFSET(guest_gdtr_base),
+	[GUEST_IDTR_BASE] =
+		SHADOW_VMCS_OFFSET(guest_idtr_base),
+	[GUEST_DR7] =
+		SHADOW_VMCS_OFFSET(guest_dr7),
+	[GUEST_RSP] =
+		SHADOW_VMCS_OFFSET(guest_rsp),
+	[GUEST_RIP] =
+		SHADOW_VMCS_OFFSET(guest_rip),
+	[GUEST_RFLAGS] =
+		SHADOW_VMCS_OFFSET(guest_rflags),
+	[GUEST_PENDING_DBG_EXCEPTIONS] =
+		SHADOW_VMCS_OFFSET(guest_pending_dbg_exceptions),
+	[GUEST_SYSENTER_ESP] =
+		SHADOW_VMCS_OFFSET(guest_sysenter_esp),
+	[GUEST_SYSENTER_EIP] =
+		SHADOW_VMCS_OFFSET(guest_sysenter_eip),
+	[HOST_CR0] =
+		SHADOW_VMCS_OFFSET(host_cr0),
+	[HOST_CR3] =
+		SHADOW_VMCS_OFFSET(host_cr3),
+	[HOST_CR4] =
+		SHADOW_VMCS_OFFSET(host_cr4),
+	[HOST_FS_BASE] =
+		SHADOW_VMCS_OFFSET(host_fs_base),
+	[HOST_GS_BASE] =
+		SHADOW_VMCS_OFFSET(host_gs_base),
+	[HOST_TR_BASE] =
+		SHADOW_VMCS_OFFSET(host_tr_base),
+	[HOST_GDTR_BASE] =
+		SHADOW_VMCS_OFFSET(host_gdtr_base),
+	[HOST_IDTR_BASE] =
+		SHADOW_VMCS_OFFSET(host_idtr_base),
+	[HOST_IA32_SYSENTER_ESP] =
+		SHADOW_VMCS_OFFSET(host_ia32_sysenter_esp),
+	[HOST_IA32_SYSENTER_EIP] =
+		SHADOW_VMCS_OFFSET(host_ia32_sysenter_eip),
+	[HOST_RSP] =
+		SHADOW_VMCS_OFFSET(host_rsp),
+	[HOST_RIP] =
+		SHADOW_VMCS_OFFSET(host_rip),
+};
+
+static inline unsigned short vmcs_field_to_offset(unsigned long field)
+{
+
+	if (field > HOST_RIP || vmcs_field_to_offset_table[field] == 0) {
+		printk(KERN_ERR "invalid vmcs encoding 0x%lx\n", field);
+		return -1;
+	}
+
+	return vmcs_field_to_offset_table[field];
+}
+
+static inline unsigned long nested_vmcs_readl(struct kvm_vcpu *vcpu,
+					      unsigned long field)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long *entry;
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return -1;
+	}
+
+	entry = (unsigned long *)((char *)(vmx->nested.l2_state->shadow_vmcs) +
+				 vmcs_field_to_offset(field));
+	return *entry;
+}
+
+static inline u16 nested_vmcs_read16(struct kvm_vcpu *vcpu,
+				     unsigned long field)
+{
+	return nested_vmcs_readl(vcpu, field);
+}
+
+static inline u32 nested_vmcs_read32(struct kvm_vcpu *vcpu, unsigned long field)
+{
+	return nested_vmcs_readl(vcpu, field);
+}
+
+static inline u64 nested_vmcs_read64(struct kvm_vcpu *vcpu, unsigned long field)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	u64 *entry;
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return -1;
+	}
+
+	entry = (u64 *)((char *)(vmx->nested.l2_state->shadow_vmcs) +
+				 vmcs_field_to_offset(field));
+	return *entry;
+}
+
+static inline void nested_vmcs_writel(struct kvm_vcpu *vcpu,
+				      unsigned long field, unsigned long value)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long entry =
+		(unsigned long)(vmx->nested.l2_state->shadow_vmcs);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return;
+	}
+	entry += vmcs_field_to_offset(field);
+	*(unsigned long *)entry = value;
+}
+
+static inline void nested_vmcs_write16(struct kvm_vcpu *vcpu,
+				       unsigned long field, u16 value)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long entry =
+		(unsigned long)(vmx->nested.l2_state->shadow_vmcs);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return;
+	}
+	entry += vmcs_field_to_offset(field);
+	*(u16 *)entry = value;
+}
+
+static inline void nested_vmcs_write32(struct kvm_vcpu *vcpu,
+				       unsigned long field, u32 value)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long entry =
+		(unsigned long)(vmx->nested.l2_state->shadow_vmcs);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return;
+	}
+	entry += vmcs_field_to_offset(field);
+	*(u32 *)entry = value;
+}
+
+static inline void nested_vmcs_write64(struct kvm_vcpu *vcpu,
+				       unsigned long field, u64 value)
+{
+#ifdef CONFIG_X86_64
+	nested_vmcs_writel(vcpu, field, value);
+#else /* nested: 32 bit not actually tested */
+	nested_vmcs_writel(vcpu, field, value);
+	nested_vmcs_writel(vcpu, field+1, value >> 32);
+#endif
+}
+
 static struct page *nested_get_page(struct kvm_vcpu *vcpu,
 				    u64 vmcs_addr)
 {
@@ -307,6 +720,50 @@ static struct page *nested_get_page(struct kvm_vcpu *vcpu,
 
 }
 
+static int nested_map_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct page *vmcs_page = nested_get_page(vcpu, vmx->nested.vmptr);
+
+	if (vmcs_page == NULL) {
+		printk(KERN_INFO "%s: failure in nested_get_page\n",__func__);
+		return 0;
+	}
+
+	if (vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_INFO "%s: shadow vmcs already mapped\n",__func__);
+		return 0;
+	}
+
+	vmx->nested.l2_state->shadow_vmcs = kmap_atomic(vmcs_page, KM_USER0);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_INFO "%s: error in kmap_atomic\n",__func__);
+		return 0;
+	}
+
+	return 1;
+}
+
+static void nested_unmap_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+	struct page *page;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk("Shadow vmcs already unmapped\n");
+		return;
+	}
+
+	page = kmap_atomic_to_page(vmx->nested.l2_state->shadow_vmcs);
+
+	kunmap_atomic(vmx->nested.l2_state->shadow_vmcs, KM_USER0);
+
+	kvm_release_page_dirty(page);
+
+	vmx->nested.l2_state->shadow_vmcs = NULL;
+}
+
 static int init_rmode(struct kvm *kvm);
 static u64 construct_eptp(unsigned long root_hpa);
 
@@ -3550,6 +4007,26 @@ static void clear_rflags_cf_zf(struct kvm_vcpu *vcpu)
 	vmx_set_rflags(vcpu, rflags);
 }
 
+static void set_rflags_to_vmx_fail_invalid(struct kvm_vcpu *vcpu)
+{
+	unsigned long rflags;
+	rflags = vmx_get_rflags(vcpu);
+	rflags |= X86_EFLAGS_CF;
+	rflags &= ~X86_EFLAGS_PF & ~X86_EFLAGS_AF & ~X86_EFLAGS_ZF &
+		~X86_EFLAGS_SF & ~X86_EFLAGS_OF;
+	vmx_set_rflags(vcpu, rflags);
+}
+
+static void set_rflags_to_vmx_fail_valid(struct kvm_vcpu *vcpu)
+{
+	unsigned long rflags;
+	rflags = vmx_get_rflags(vcpu);
+	rflags |= X86_EFLAGS_ZF;
+	rflags &= ~X86_EFLAGS_PF & ~X86_EFLAGS_AF & ~X86_EFLAGS_CF &
+		~X86_EFLAGS_SF & ~X86_EFLAGS_OF;
+	vmx_set_rflags(vcpu, rflags);
+}
+
 static int handle_vmclear(struct kvm_vcpu *vcpu)
 {
 	if (!nested_vmx_check_permission(vcpu))
@@ -3563,6 +4040,116 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_vmread(struct kvm_vcpu *vcpu)
+{
+#ifndef CONFIG_X86_64
+	u64 value;
+#endif
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
+		set_rflags_to_vmx_fail_invalid(vcpu);
+		return 1;
+	}
+
+	switch (vmcs_field_length(vcpu->arch.regs[VCPU_REGS_RDX])) {
+	case VMCS_FIELD_TYPE_U16:
+		vcpu->arch.regs[VCPU_REGS_RAX] =
+			nested_vmcs_read16(vcpu,
+					   vcpu->arch.regs[VCPU_REGS_RDX]);
+		break;
+	case VMCS_FIELD_TYPE_U32:
+		vcpu->arch.regs[VCPU_REGS_RAX] =
+			nested_vmcs_read32(vcpu,
+					   vcpu->arch.regs[VCPU_REGS_RDX]);
+		break;
+	case VMCS_FIELD_TYPE_U64:
+#ifdef CONFIG_X86_64
+		vcpu->arch.regs[VCPU_REGS_RAX] =
+		nested_vmcs_read64(vcpu,
+					   vcpu->arch.regs[VCPU_REGS_RDX]);
+#else /* nested: 32 bit not actually tested */
+		value =  nested_vmcs_read64(vcpu,
+					    vcpu->arch.regs[VCPU_REGS_RDX]);
+		vcpu->arch.regs[VCPU_REGS_RAX] = value;
+		vcpu->arch.regs[VCPU_REGS_RBX] = value >> 32;
+#endif
+	break;
+	case VMCS_FIELD_TYPE_ULONG:
+		vcpu->arch.regs[VCPU_REGS_RAX] =
+			nested_vmcs_readl(vcpu,
+					  vcpu->arch.regs[VCPU_REGS_RDX]);
+		break;
+	default:
+		printk(KERN_INFO "%s invalid field\n", __func__);
+		set_rflags_to_vmx_fail_valid(vcpu);
+		vmcs_write32(VM_INSTRUCTION_ERROR, 12);
+		nested_unmap_shadow_vmcs(vcpu);
+		return 1;
+	}
+
+	clear_rflags_cf_zf(vcpu);
+	skip_emulated_instruction(vcpu);
+	nested_unmap_shadow_vmcs(vcpu);
+	return 1;
+}
+
+static int handle_vmwrite(struct kvm_vcpu *vcpu)
+{
+#ifndef CONFIG_X86_64
+	u64 value ;
+#endif
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
+		set_rflags_to_vmx_fail_invalid(vcpu);
+		return 1;
+	}
+
+	switch (vmcs_field_length(vcpu->arch.regs[VCPU_REGS_RDX])) {
+	case VMCS_FIELD_TYPE_U16:
+		nested_vmcs_write16(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+				    vcpu->arch.regs[VCPU_REGS_RAX]);
+		break;
+	case VMCS_FIELD_TYPE_U32:
+		nested_vmcs_write32(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+				    vcpu->arch.regs[VCPU_REGS_RAX]);
+		break;
+	case VMCS_FIELD_TYPE_U64:
+#ifdef CONFIG_X86_64
+		nested_vmcs_write64(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+				    vcpu->arch.regs[VCPU_REGS_RAX]);
+#else /* nested: 32 bit not actually tested */
+		value =  vcpu->arch.regs[VCPU_REGS_RAX] |
+			(vcpu->arch.regs[VCPU_REGS_RBX] << 32);
+		nested_vmcs_write64(vcpu,
+				    vcpu->arch.regs[VCPU_REGS_RDX], value);
+#endif
+		break;
+	case VMCS_FIELD_TYPE_ULONG:
+		nested_vmcs_writel(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+				   vcpu->arch.regs[VCPU_REGS_RAX]);
+		break;
+	default:
+		printk(KERN_INFO "%s invalid field\n", __func__);
+		set_rflags_to_vmx_fail_valid(vcpu);
+		vmcs_write32(VM_INSTRUCTION_ERROR, 12);
+		nested_unmap_shadow_vmcs(vcpu);
+		return 1;
+	}
+
+	clear_rflags_cf_zf(vcpu);
+	skip_emulated_instruction(vcpu);
+	nested_unmap_shadow_vmcs(vcpu);
+	return 1;
+}
+
 static int handle_vmoff(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -3950,9 +4537,9 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
 	[EXIT_REASON_VMPTRLD]                 = handle_vmptrld,
 	[EXIT_REASON_VMPTRST]                 = handle_vmptrst,
-	[EXIT_REASON_VMREAD]                  = handle_vmx_insn,
+	[EXIT_REASON_VMREAD]                  = handle_vmread,
 	[EXIT_REASON_VMRESUME]                = handle_vmx_insn,
-	[EXIT_REASON_VMWRITE]                 = handle_vmx_insn,
+	[EXIT_REASON_VMWRITE]                 = handle_vmwrite,
 	[EXIT_REASON_VMOFF]                   = handle_vmoff,
 	[EXIT_REASON_VMON]                    = handle_vmon,
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
Signature · 1 line
1.6.0.4
#5 lore raw

PATCH 5/5 Nested VMX patch 5 implements vmlaunch and vmresume

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c | 1173 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 1148 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx.c +1148 -25
index 6a4c252..e814029 100644
@@ -209,6 +209,7 @@ struct __attribute__ ((__packed__)) level_state {
 	struct vmcs *vmcs;
 	int cpu;
 	int launched;
+	bool first_launch;
 };
 
 struct nested_vmx {
@@ -216,6 +217,12 @@ struct nested_vmx {
 	bool vmxon;
 	/* What is the location of the  vmcs l1 keeps for l2? (in level1 gpa) */
 	u64 vmptr;
+	/* Are we running nested guest */
+	bool nested_mode;
+	/* L1 requested VMLAUNCH or VMRESUME but we didn't run L2 yet */
+	bool nested_run_pending;
+	/* flag indicating if there was a valid IDT after exiting from l2 */
+	bool nested_valid_idt;
 	/*
 	 * Level 2 state : includes vmcs,registers and
 	 * a copy of vmcs12 for vmread/vmwrite
@@ -240,6 +247,10 @@ static inline int vmcs_field_length(unsigned long field)
 	return (VMCS_FIELD_LENGTH_MASK & field) >> 13;
 }
 
+#define NESTED_VM_EXIT_CONTROLS_MASK (~(VM_EXIT_LOAD_IA32_PAT | \
+					VM_EXIT_SAVE_IA32_PAT))
+#define NESTED_VM_ENTRY_CONTROLS_MASK (~(VM_ENTRY_LOAD_IA32_PAT | \
+					 VM_ENTRY_IA32E_MODE))
 struct vmcs {
 	u32 revision_id;
 	u32 abort;
@@ -303,6 +314,12 @@ static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
 	return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+static inline struct shadow_vmcs *get_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+	WARN_ON(!to_vmx(vcpu)->nested.l2_state->shadow_vmcs);
+	return to_vmx(vcpu)->nested.l2_state->shadow_vmcs;
+}
+
 #define SHADOW_VMCS_OFFSET(x) offsetof(struct shadow_vmcs, x)
 
 static unsigned short vmcs_field_to_offset_table[HOST_RIP+1] = {
@@ -822,8 +839,16 @@ static struct kvm_vmx_segment_field {
 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
 
 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu);
+static int nested_vmx_check_exception(struct vcpu_vmx *vmx, unsigned nr,
+				      bool has_error_code, u32 error_code);
+static int nested_vmx_intr(struct kvm_vcpu *vcpu);
 static int create_l1_state(struct kvm_vcpu *vcpu);
 static int create_l2_state(struct kvm_vcpu *vcpu);
+static int launch_guest(struct kvm_vcpu *vcpu);
+static int nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu);
+static int nested_vmx_exit_handled(struct kvm_vcpu *vcpu, bool kvm_override);
+static int nested_vmx_vmexit(struct kvm_vcpu *vcpu,
+			     bool is_interrupt);
 
 /*
  * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
@@ -940,6 +965,18 @@ static inline bool cpu_has_vmx_ept_2m_page(void)
 	return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
 }
 
+static inline int is_exception(u32 intr_info)
+{
+	return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
+		== (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
+}
+
+static inline int is_nmi(u32 intr_info)
+{
+	return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
+		== (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
+}
+
 static inline int cpu_has_vmx_invept_individual_addr(void)
 {
 	return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
@@ -990,6 +1027,51 @@ static inline bool report_flexpriority(void)
 	return flexpriority_enabled;
 }
 
+static inline int nested_cpu_has_vmx_tpr_shadow(struct  kvm_vcpu *vcpu)
+{
+	return cpu_has_vmx_tpr_shadow() &&
+		get_shadow_vmcs(vcpu)->cpu_based_vm_exec_control &
+		CPU_BASED_TPR_SHADOW;
+}
+
+static inline int nested_cpu_has_secondary_exec_ctrls(struct kvm_vcpu *vcpu)
+{
+	return cpu_has_secondary_exec_ctrls() &&
+		get_shadow_vmcs(vcpu)->cpu_based_vm_exec_control &
+		CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
+}
+
+static inline bool nested_vm_need_virtualize_apic_accesses(struct kvm_vcpu
+							   *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->secondary_vm_exec_control &
+		SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
+}
+
+static inline int nested_cpu_has_vmx_ept(struct kvm_vcpu *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->
+		secondary_vm_exec_control & SECONDARY_EXEC_ENABLE_EPT;
+}
+
+static inline int nested_cpu_has_vmx_vpid(struct kvm_vcpu *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->secondary_vm_exec_control &
+		SECONDARY_EXEC_ENABLE_VPID;
+}
+
+static inline int nested_cpu_has_vmx_pat(struct kvm_vcpu *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->vm_entry_controls &
+		VM_ENTRY_LOAD_IA32_PAT;
+}
+
+static inline int nested_cpu_has_vmx_msr_bitmap(struct kvm_vcpu *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->cpu_based_vm_exec_control &
+		CPU_BASED_USE_MSR_BITMAPS;
+}
+
 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
 {
 	int i;
@@ -1501,6 +1583,9 @@ static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	u32 intr_info = nr | INTR_INFO_VALID_MASK;
 
+	if (nested_vmx_check_exception(vmx, nr, has_error_code, error_code))
+		return;
+
 	if (has_error_code) {
 		vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
 		intr_info |= INTR_INFO_DELIVER_CODE_MASK;
@@ -1943,6 +2028,200 @@ static void vmclear_local_vcpus(void)
 		__vcpu_clear(vmx);
 }
 
+void prepare_vmcs_12(struct kvm_vcpu *vcpu)
+{
+	struct shadow_vmcs *l2_shadow_vmcs =
+		get_shadow_vmcs(vcpu);
+
+	l2_shadow_vmcs->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
+	l2_shadow_vmcs->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
+	l2_shadow_vmcs->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
+	l2_shadow_vmcs->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
+	l2_shadow_vmcs->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
+	l2_shadow_vmcs->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
+	l2_shadow_vmcs->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
+	l2_shadow_vmcs->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
+
+	l2_shadow_vmcs->tsc_offset = vmcs_read64(TSC_OFFSET);
+	l2_shadow_vmcs->guest_physical_address =
+		vmcs_read64(GUEST_PHYSICAL_ADDRESS);
+	l2_shadow_vmcs->vmcs_link_pointer = vmcs_read64(VMCS_LINK_POINTER);
+	l2_shadow_vmcs->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+	if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
+		l2_shadow_vmcs->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
+	l2_shadow_vmcs->cr3_target_count = vmcs_read32(CR3_TARGET_COUNT);
+	l2_shadow_vmcs->vm_entry_intr_info_field =
+		vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
+	l2_shadow_vmcs->vm_entry_exception_error_code =
+		vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE);
+	l2_shadow_vmcs->vm_entry_instruction_len =
+		vmcs_read32(VM_ENTRY_INSTRUCTION_LEN);
+	l2_shadow_vmcs->vm_instruction_error =
+		vmcs_read32(VM_INSTRUCTION_ERROR);
+	l2_shadow_vmcs->vm_exit_reason  = vmcs_read32(VM_EXIT_REASON);
+	l2_shadow_vmcs->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+	l2_shadow_vmcs->vm_exit_intr_error_code =
+		vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
+	l2_shadow_vmcs->idt_vectoring_info_field =
+		vmcs_read32(IDT_VECTORING_INFO_FIELD);
+	l2_shadow_vmcs->idt_vectoring_error_code =
+		vmcs_read32(IDT_VECTORING_ERROR_CODE);
+	l2_shadow_vmcs->vm_exit_instruction_len =
+		vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
+	l2_shadow_vmcs->vmx_instruction_info =
+		vmcs_read32(VMX_INSTRUCTION_INFO);
+	l2_shadow_vmcs->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
+	l2_shadow_vmcs->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
+	l2_shadow_vmcs->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
+	l2_shadow_vmcs->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
+	l2_shadow_vmcs->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
+	l2_shadow_vmcs->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
+	l2_shadow_vmcs->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
+	l2_shadow_vmcs->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
+	l2_shadow_vmcs->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
+	l2_shadow_vmcs->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
+	l2_shadow_vmcs->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
+	l2_shadow_vmcs->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
+	l2_shadow_vmcs->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
+	l2_shadow_vmcs->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
+	l2_shadow_vmcs->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
+	l2_shadow_vmcs->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
+	l2_shadow_vmcs->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
+	l2_shadow_vmcs->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
+	l2_shadow_vmcs->guest_interruptibility_info =
+		vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+	l2_shadow_vmcs->guest_activity_state =
+		vmcs_read32(GUEST_ACTIVITY_STATE);
+	l2_shadow_vmcs->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
+
+	l2_shadow_vmcs->cr0_read_shadow = vmcs_readl(CR0_READ_SHADOW);
+	l2_shadow_vmcs->cr4_read_shadow = vmcs_readl(CR4_READ_SHADOW);
+	l2_shadow_vmcs->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	l2_shadow_vmcs->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
+	l2_shadow_vmcs->guest_cr0 = vmcs_readl(GUEST_CR0);
+
+	l2_shadow_vmcs->guest_cr4 = vmcs_readl(GUEST_CR4);
+	l2_shadow_vmcs->guest_es_base = vmcs_readl(GUEST_ES_BASE);
+	l2_shadow_vmcs->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
+	l2_shadow_vmcs->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
+	l2_shadow_vmcs->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
+	l2_shadow_vmcs->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
+	l2_shadow_vmcs->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
+	l2_shadow_vmcs->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
+	l2_shadow_vmcs->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
+	l2_shadow_vmcs->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
+	l2_shadow_vmcs->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
+	l2_shadow_vmcs->guest_dr7 = vmcs_readl(GUEST_DR7);
+	l2_shadow_vmcs->guest_rsp = vmcs_readl(GUEST_RSP);
+	l2_shadow_vmcs->guest_rip = vmcs_readl(GUEST_RIP);
+	l2_shadow_vmcs->guest_rflags = vmcs_readl(GUEST_RFLAGS);
+	l2_shadow_vmcs->guest_pending_dbg_exceptions =
+		vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
+	l2_shadow_vmcs->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
+	l2_shadow_vmcs->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
+}
+
+int load_vmcs_common(struct shadow_vmcs *src)
+{
+	vmcs_write16(GUEST_ES_SELECTOR, src->guest_es_selector);
+	vmcs_write16(GUEST_CS_SELECTOR, src->guest_cs_selector);
+	vmcs_write16(GUEST_SS_SELECTOR, src->guest_ss_selector);
+	vmcs_write16(GUEST_DS_SELECTOR, src->guest_ds_selector);
+	vmcs_write16(GUEST_FS_SELECTOR, src->guest_fs_selector);
+	vmcs_write16(GUEST_GS_SELECTOR, src->guest_gs_selector);
+	vmcs_write16(GUEST_LDTR_SELECTOR, src->guest_ldtr_selector);
+	vmcs_write16(GUEST_TR_SELECTOR, src->guest_tr_selector);
+
+	vmcs_write64(VMCS_LINK_POINTER, src->vmcs_link_pointer);
+	vmcs_write64(GUEST_IA32_DEBUGCTL, src->guest_ia32_debugctl);
+
+	if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
+		vmcs_write64(GUEST_IA32_PAT, src->guest_ia32_pat);
+
+	vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->vm_entry_msr_load_count);
+	vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, src->vm_entry_intr_info_field);
+	vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
+		     src->vm_entry_exception_error_code);
+	vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, src->vm_entry_instruction_len);
+
+	vmcs_write32(GUEST_ES_LIMIT, src->guest_es_limit);
+	vmcs_write32(GUEST_CS_LIMIT, src->guest_cs_limit);
+	vmcs_write32(GUEST_SS_LIMIT, src->guest_ss_limit);
+	vmcs_write32(GUEST_DS_LIMIT, src->guest_ds_limit);
+	vmcs_write32(GUEST_FS_LIMIT, src->guest_fs_limit);
+	vmcs_write32(GUEST_GS_LIMIT, src->guest_gs_limit);
+	vmcs_write32(GUEST_LDTR_LIMIT, src->guest_ldtr_limit);
+	vmcs_write32(GUEST_TR_LIMIT, src->guest_tr_limit);
+	vmcs_write32(GUEST_GDTR_LIMIT, src->guest_gdtr_limit);
+	vmcs_write32(GUEST_IDTR_LIMIT, src->guest_idtr_limit);
+	vmcs_write32(GUEST_ES_AR_BYTES, src->guest_es_ar_bytes);
+	vmcs_write32(GUEST_CS_AR_BYTES, src->guest_cs_ar_bytes);
+	vmcs_write32(GUEST_SS_AR_BYTES, src->guest_ss_ar_bytes);
+	vmcs_write32(GUEST_DS_AR_BYTES, src->guest_ds_ar_bytes);
+	vmcs_write32(GUEST_FS_AR_BYTES, src->guest_fs_ar_bytes);
+	vmcs_write32(GUEST_GS_AR_BYTES, src->guest_gs_ar_bytes);
+	vmcs_write32(GUEST_LDTR_AR_BYTES, src->guest_ldtr_ar_bytes);
+	vmcs_write32(GUEST_TR_AR_BYTES, src->guest_tr_ar_bytes);
+	vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
+		     src->guest_interruptibility_info);
+	vmcs_write32(GUEST_ACTIVITY_STATE, src->guest_activity_state);
+	vmcs_write32(GUEST_SYSENTER_CS, src->guest_sysenter_cs);
+
+	vmcs_writel(GUEST_ES_BASE, src->guest_es_base);
+	vmcs_writel(GUEST_CS_BASE, src->guest_cs_base);
+	vmcs_writel(GUEST_SS_BASE, src->guest_ss_base);
+	vmcs_writel(GUEST_DS_BASE, src->guest_ds_base);
+	vmcs_writel(GUEST_FS_BASE, src->guest_fs_base);
+	vmcs_writel(GUEST_GS_BASE, src->guest_gs_base);
+	vmcs_writel(GUEST_LDTR_BASE, src->guest_ldtr_base);
+	vmcs_writel(GUEST_TR_BASE, src->guest_tr_base);
+	vmcs_writel(GUEST_GDTR_BASE, src->guest_gdtr_base);
+	vmcs_writel(GUEST_IDTR_BASE, src->guest_idtr_base);
+	vmcs_writel(GUEST_DR7, src->guest_dr7);
+	vmcs_writel(GUEST_RSP, src->guest_rsp);
+	vmcs_writel(GUEST_RIP, src->guest_rip);
+	vmcs_writel(GUEST_RFLAGS, src->guest_rflags);
+	vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
+		    src->guest_pending_dbg_exceptions);
+	vmcs_writel(GUEST_SYSENTER_ESP, src->guest_sysenter_esp);
+	vmcs_writel(GUEST_SYSENTER_EIP, src->guest_sysenter_eip);
+
+	return 0;
+}
+
+int load_vmcs_host_state(struct shadow_vmcs *src)
+{
+	vmcs_write16(HOST_ES_SELECTOR, src->host_es_selector);
+	vmcs_write16(HOST_CS_SELECTOR, src->host_cs_selector);
+	vmcs_write16(HOST_SS_SELECTOR, src->host_ss_selector);
+	vmcs_write16(HOST_DS_SELECTOR, src->host_ds_selector);
+	vmcs_write16(HOST_FS_SELECTOR, src->host_fs_selector);
+	vmcs_write16(HOST_GS_SELECTOR, src->host_gs_selector);
+	vmcs_write16(HOST_TR_SELECTOR, src->host_tr_selector);
+
+	vmcs_write64(TSC_OFFSET, src->tsc_offset);
+
+	if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT)
+		vmcs_write64(HOST_IA32_PAT, src->host_ia32_pat);
+
+	vmcs_write32(HOST_IA32_SYSENTER_CS, src->host_ia32_sysenter_cs);
+
+	vmcs_writel(HOST_CR0, src->host_cr0);
+	vmcs_writel(HOST_CR3, src->host_cr3);
+	vmcs_writel(HOST_CR4, src->host_cr4);
+	vmcs_writel(HOST_FS_BASE, src->host_fs_base);
+	vmcs_writel(HOST_GS_BASE, src->host_gs_base);
+	vmcs_writel(HOST_TR_BASE, src->host_tr_base);
+	vmcs_writel(HOST_GDTR_BASE, src->host_gdtr_base);
+	vmcs_writel(HOST_IDTR_BASE, src->host_idtr_base);
+	vmcs_writel(HOST_RSP, src->host_rsp);
+	vmcs_writel(HOST_RIP, src->host_rip);
+	vmcs_writel(HOST_IA32_SYSENTER_ESP, src->host_ia32_sysenter_esp);
+	vmcs_writel(HOST_IA32_SYSENTER_EIP, src->host_ia32_sysenter_eip);
+
+	return 0;
+}
+
 struct level_state *create_state(void)
 {
 	struct level_state *state = NULL;
@@ -2003,6 +2282,8 @@ int create_l2_state(struct kvm_vcpu *vcpu)
 	vmx->nested.l2_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
 	vmx->nested.l2_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
 
+	vmx->nested.l2_state->first_launch = true;
+
 	return 0;
 }
 
@@ -3393,6 +3674,14 @@ static void enable_irq_window(struct kvm_vcpu *vcpu)
 {
 	u32 cpu_based_vm_exec_control;
 
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		if (kvm_cpu_has_interrupt(vcpu)) {
+			if (nested_vmx_intr(vcpu))
+				return;
+		}
+		return;
+	}
+
 	cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
 	cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
 	vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
@@ -3448,6 +3737,10 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 
+	if (vmx->nested.nested_mode) {
+		return;
+	}
+
 	if (!cpu_has_virtual_nmis()) {
 		/*
 		 * Tracking the NMI-blocked state in software is built upon
@@ -3489,6 +3782,13 @@ static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
 
 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
 {
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		if (kvm_cpu_has_interrupt(vcpu)) {
+			if (!nested_vmx_intr(vcpu))
+				return 0;
+		}
+	}
+
 	return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
 		!(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
 			(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
@@ -3993,12 +4293,6 @@ static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
-static int handle_vmx_insn(struct kvm_vcpu *vcpu)
-{
-	kvm_queue_exception(vcpu, UD_VECTOR);
-	return 1;
-}
-
 static void clear_rflags_cf_zf(struct kvm_vcpu *vcpu)
 {
 	unsigned long rflags;
@@ -4040,6 +4334,27 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_vmlaunch(struct kvm_vcpu *vcpu)
+{
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+	if (!to_vmx(vcpu)->nested.l2_state->vmclear)
+		return 1;
+
+	return launch_guest(vcpu);
+}
+
+static int handle_vmresume(struct kvm_vcpu *vcpu)
+{
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (to_vmx(vcpu)->nested.l2_state->vmclear)
+		return 1;
+
+	return launch_guest(vcpu);
+}
+
 static int handle_vmread(struct kvm_vcpu *vcpu)
 {
 #ifndef CONFIG_X86_64
@@ -4050,7 +4365,6 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
 		return 1;
 
 	if (!nested_map_shadow_vmcs(vcpu)) {
-		printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
 		set_rflags_to_vmx_fail_invalid(vcpu);
 		return 1;
 	}
@@ -4107,7 +4421,6 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)
 		return 1;
 
 	if (!nested_map_shadow_vmcs(vcpu)) {
-		printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
 		set_rflags_to_vmx_fail_invalid(vcpu);
 		return 1;
 	}
@@ -4137,16 +4450,16 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)
 				   vcpu->arch.regs[VCPU_REGS_RAX]);
 		break;
 	default:
+		nested_unmap_shadow_vmcs(vcpu);
 		printk(KERN_INFO "%s invalid field\n", __func__);
 		set_rflags_to_vmx_fail_valid(vcpu);
 		vmcs_write32(VM_INSTRUCTION_ERROR, 12);
-		nested_unmap_shadow_vmcs(vcpu);
 		return 1;
 	}
 
+	nested_unmap_shadow_vmcs(vcpu);
 	clear_rflags_cf_zf(vcpu);
 	skip_emulated_instruction(vcpu);
-	nested_unmap_shadow_vmcs(vcpu);
 	return 1;
 }
 
@@ -4208,7 +4521,6 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 static int handle_vmptrld(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	struct page *vmcs_page;
 	u64 guest_vmcs_addr;
 
 	if (!nested_vmx_check_permission(vcpu))
@@ -4228,14 +4540,7 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)
 	}
 
 	if (vmx->nested.vmptr != guest_vmcs_addr) {
-		/* checking vmptr address */
-		vmcs_page = nested_get_page(vcpu, guest_vmcs_addr);
-		if (vmcs_page == NULL)
-			return 1;
-
 		vmx->nested.vmptr = guest_vmcs_addr;
-
-		kvm_release_page_clean(vmcs_page);
 	}
 
 	clear_rflags_cf_zf(vcpu);
@@ -4534,11 +4839,11 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_INVLPG]		      = handle_invlpg,
 	[EXIT_REASON_VMCALL]                  = handle_vmcall,
 	[EXIT_REASON_VMCLEAR]	              = handle_vmclear,
-	[EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
+	[EXIT_REASON_VMLAUNCH]                = handle_vmlaunch,
 	[EXIT_REASON_VMPTRLD]                 = handle_vmptrld,
 	[EXIT_REASON_VMPTRST]                 = handle_vmptrst,
 	[EXIT_REASON_VMREAD]                  = handle_vmread,
-	[EXIT_REASON_VMRESUME]                = handle_vmx_insn,
+	[EXIT_REASON_VMRESUME]                = handle_vmresume,
 	[EXIT_REASON_VMWRITE]                 = handle_vmwrite,
 	[EXIT_REASON_VMOFF]                   = handle_vmoff,
 	[EXIT_REASON_VMON]                    = handle_vmon,
@@ -4566,6 +4871,17 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 
 	trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
 
+	if (exit_reason == EXIT_REASON_VMLAUNCH ||
+	    exit_reason == EXIT_REASON_VMRESUME)
+		vmx->nested.nested_run_pending = 1;
+	else
+		vmx->nested.nested_run_pending = 0;
+
+	if (vmx->nested.nested_mode && nested_vmx_exit_handled(vcpu, true)) {
+		nested_vmx_vmexit(vcpu, false);
+		return 1;
+	}
+
 	/* If we need to emulate an MMIO from handle_invalid_guest_state
 	 * we just return 0 */
 	if (vmx->emulation_required && emulate_invalid_guest_state) {
@@ -4585,7 +4901,6 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 			= vmcs_read32(VM_INSTRUCTION_ERROR);
 		return 0;
 	}
-
 	if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
 			(exit_reason != EXIT_REASON_EXCEPTION_NMI &&
 			exit_reason != EXIT_REASON_EPT_VIOLATION &&
@@ -4593,8 +4908,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 		printk(KERN_WARNING "%s: unexpected, valid vectoring info "
 		       "(0x%x) and exit reason is 0x%x\n",
 		       __func__, vectoring_info, exit_reason);
-
-	if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
+	if (!vmx->nested.nested_mode && unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
 		if (vmx_interrupt_allowed(vcpu)) {
 			vmx->soft_vnmi_blocked = 0;
 		} else if (vmx->vnmi_blocked_time > 1000000000LL &&
@@ -4641,10 +4955,13 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
 	int type;
 	bool idtv_info_valid;
 
-	exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
-
 	vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
+	if (vmx->nested.nested_mode)
+		return;
+
+	exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
 	/* Handle machine checks before interrupts are enabled */
 	if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
 	    || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
@@ -4747,6 +5064,60 @@ static void fixup_rmode_irq(struct vcpu_vmx *vmx)
 		| vmx->rmode.irq.vector;
 }
 
+static int nested_handle_valid_idt(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int irq;
+	int type;
+	int errCodeValid;
+	u32 idt_vectoring_info;
+	u32 guest_intr;
+	bool nmi_window_open;
+	bool interrupt_window_open;
+
+	if (vmx->nested.nested_mode && vmx->nested.nested_valid_idt) {
+		idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
+		irq  = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
+		type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
+		errCodeValid = idt_vectoring_info &
+			VECTORING_INFO_DELIVER_CODE_MASK;
+
+		guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+		nmi_window_open =
+			!(guest_intr & (GUEST_INTR_STATE_STI |
+					GUEST_INTR_STATE_MOV_SS |
+					GUEST_INTR_STATE_NMI));
+
+		interrupt_window_open =
+			((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
+			 !(guest_intr & (GUEST_INTR_STATE_STI |
+					 GUEST_INTR_STATE_MOV_SS)));
+
+		if (type == INTR_TYPE_EXT_INTR && !interrupt_window_open) {
+			printk(KERN_INFO "IDT ignored, l2 interrupt window closed!\n");
+			return 0;
+		}
+
+		if (type == INTR_TYPE_NMI_INTR && !nmi_window_open) {
+			printk(KERN_INFO "IDT ignored, l2 nmi window closed!\n");
+			return 0;
+		}
+
+		vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
+			irq | type | INTR_INFO_VALID_MASK | errCodeValid);
+
+
+		vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
+			     vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
+
+		if (errCodeValid)
+			vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
+				     vmcs_read32(IDT_VECTORING_ERROR_CODE));
+	}
+
+	return 1;
+}
+
 #ifdef CONFIG_X86_64
 #define R "r"
 #define Q "q"
@@ -4758,6 +5129,26 @@ static void fixup_rmode_irq(struct vcpu_vmx *vmx)
 static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int r;
+
+	if (vmx->nested.nested_mode) {
+		r = nested_handle_valid_idt(vcpu);
+		if (!r) {
+			vmx->fail = 1;
+			return;
+		}
+
+		if (!nested_map_shadow_vmcs(vcpu)) {
+			vmx->fail = 1;
+			return;
+		}
+
+		vmcs_write32(EXCEPTION_BITMAP, get_shadow_vmcs(vcpu)->
+			     exception_bitmap |
+			     vmx->nested.l1_state->shadow_vmcs->exception_bitmap);
+
+		nested_unmap_shadow_vmcs(vcpu);
+	}
 
 	if (enable_ept && is_paging(vcpu)) {
 		vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
@@ -4896,6 +5287,10 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	get_debugreg(vcpu->arch.dr6, 6);
 
 	vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
+
+	vmx->nested.nested_valid_idt = vmx->nested.nested_mode &&
+		(vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
+
 	if (vmx->rmode.irq.pending)
 		fixup_rmode_irq(vmx);
 
@@ -4984,6 +5379,11 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 			goto free_vmcs;
 	}
 
+	vmx->nested.vmptr = 0;
+
+	vmx->nested.l1_state = NULL;
+	vmx->nested.l2_state = NULL;
+
 	return &vmx->vcpu;
 
 free_vmcs:
@@ -5215,6 +5615,729 @@ void save_vmcs(struct shadow_vmcs *dst)
 	if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT)
 		dst->host_ia32_pat = vmcs_read64(HOST_IA32_PAT);
 }
+int prepare_vmcs_02(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct shadow_vmcs *src = get_shadow_vmcs(vcpu);
+	u32 exec_control;
+
+	if (!src) {
+		printk(KERN_INFO "%s: Error no shadow vmcs\n", __func__);
+		return 1;
+	}
+
+	load_vmcs_common(src);
+
+	if (vmx->nested.l2_state->first_launch) {
+		if (cpu_has_vmx_vpid() && vmx->nested.l2_state->vpid != 0)
+			vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.l2_state->vpid);
+
+		if (vmx->nested.l2_state->io_bitmap_a)
+			vmcs_write64(IO_BITMAP_A, vmx->nested.l2_state->io_bitmap_a);
+
+		if (vmx->nested.l2_state->io_bitmap_b)
+			vmcs_write64(IO_BITMAP_B, vmx->nested.l2_state->io_bitmap_b);
+
+		if (vmx->nested.l2_state->msr_bitmap)
+			vmcs_write64(MSR_BITMAP, vmx->nested.l2_state->msr_bitmap);
+
+		if (src->vm_entry_msr_load_count > 0) {
+			struct page *page;
+
+			page = nested_get_page(vcpu,
+					       src->vm_entry_msr_load_addr);
+			if (!page)
+				return 1;
+
+			vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, page_to_phys(page));
+
+			kvm_release_page_clean(page);
+		}
+
+		if (nested_cpu_has_vmx_tpr_shadow(vcpu)) {
+			struct page *page;
+
+			page = nested_get_page(vcpu,
+					       src->virtual_apic_page_addr);
+			if (!page)
+				return 1;
+
+			vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, page_to_phys(page));
+
+			kvm_release_page_clean(page);
+		}
+
+		if (nested_vm_need_virtualize_apic_accesses(vcpu)) {
+			struct page *page =
+				nested_get_page(vcpu, src->apic_access_addr);
+			if (!page)
+				return 1;
+
+			vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
+			kvm_release_page_clean(page);
+		}
+
+		vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
+			     (vmx->nested.l1_state->shadow_vmcs->pin_based_vm_exec_control |
+			      src->pin_based_vm_exec_control));
+
+		vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
+			     (vmx->nested.l1_state->shadow_vmcs->page_fault_error_code_mask &
+			      src->page_fault_error_code_mask));
+
+		vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
+			     (vmx->nested.l1_state->shadow_vmcs->page_fault_error_code_match &
+			      src->page_fault_error_code_match));
+
+		if (cpu_has_secondary_exec_ctrls()) {
+
+			exec_control =
+				vmx->nested.l1_state->shadow_vmcs->secondary_vm_exec_control;
+
+			if (nested_cpu_has_secondary_exec_ctrls(vcpu)) {
+
+				exec_control |= src->secondary_vm_exec_control;
+
+				if (!vm_need_virtualize_apic_accesses(vcpu->kvm) ||
+				    !nested_vm_need_virtualize_apic_accesses(vcpu))
+					exec_control &=
+						~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
+			}
+
+			vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
+		}
+
+		load_vmcs_host_state(vmx->nested.l1_state->shadow_vmcs);
+
+		vmx->nested.l2_state->first_launch = false;
+	}
+
+	if (vm_need_tpr_shadow(vcpu->kvm) &&
+	    nested_cpu_has_vmx_tpr_shadow(vcpu))
+		vmcs_write32(TPR_THRESHOLD, src->tpr_threshold);
+
+	if (enable_ept) {
+		if (!nested_cpu_has_vmx_ept(vcpu)) {
+			vmcs_write64(EPT_POINTER,
+				     vmx->nested.l1_state->shadow_vmcs->ept_pointer);
+			vmcs_write64(GUEST_PDPTR0,
+				     vmx->nested.l1_state->shadow_vmcs->guest_pdptr0);
+			vmcs_write64(GUEST_PDPTR1,
+				     vmx->nested.l1_state->shadow_vmcs->guest_pdptr1);
+			vmcs_write64(GUEST_PDPTR2,
+				     vmx->nested.l1_state->shadow_vmcs->guest_pdptr2);
+			vmcs_write64(GUEST_PDPTR3,
+				     vmx->nested.l1_state->shadow_vmcs->guest_pdptr3);
+		}
+	}
+
+	exec_control = vmx->nested.l1_state->shadow_vmcs->cpu_based_vm_exec_control;
+
+	exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
+
+	exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
+
+	exec_control &= ~CPU_BASED_TPR_SHADOW;
+
+	exec_control |= src->cpu_based_vm_exec_control;
+
+	if (!vm_need_tpr_shadow(vcpu->kvm) ||
+	    src->virtual_apic_page_addr == 0) {
+		exec_control &= ~CPU_BASED_TPR_SHADOW;
+#ifdef CONFIG_X86_64
+		exec_control |= CPU_BASED_CR8_STORE_EXITING |
+			CPU_BASED_CR8_LOAD_EXITING;
+#endif
+	} else if (exec_control & CPU_BASED_TPR_SHADOW) {
+
+#ifdef CONFIG_X86_64
+		exec_control &= ~CPU_BASED_CR8_STORE_EXITING;
+		exec_control &= ~CPU_BASED_CR8_LOAD_EXITING;
+#endif
+	}
+
+	vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
+
+	vmcs_write32(EXCEPTION_BITMAP,
+		     (vmx->nested.l1_state->shadow_vmcs->exception_bitmap |
+		      src->exception_bitmap));
+
+	vmcs_write32(VM_EXIT_CONTROLS,
+		     ((vmx->nested.l1_state->shadow_vmcs->vm_exit_controls &
+		       NESTED_VM_EXIT_CONTROLS_MASK) | src->vm_exit_controls));
+
+	vmcs_write32(VM_ENTRY_CONTROLS,
+		     (vmx->nested.l1_state->shadow_vmcs->vm_entry_controls &
+		      NESTED_VM_ENTRY_CONTROLS_MASK) | src->vm_entry_controls);
+
+	vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->vm_entry_msr_load_count);
+
+	vmcs_writel(CR0_GUEST_HOST_MASK,
+		    (vmx->nested.l1_state->shadow_vmcs->cr0_guest_host_mask  &
+		     src->cr0_guest_host_mask));
+	vmcs_writel(CR4_GUEST_HOST_MASK,
+		    (vmx->nested.l1_state->shadow_vmcs->cr4_guest_host_mask  &
+		     src->cr4_guest_host_mask));
+
+	return 0;
+}
+
+int switch_back_vmcs(struct kvm_vcpu *vcpu)
+{
+	struct shadow_vmcs *src = to_vmx(vcpu)->nested.l1_state->shadow_vmcs;
+
+	if (enable_vpid && src->virtual_processor_id != 0)
+		vmcs_write16(VIRTUAL_PROCESSOR_ID, src->virtual_processor_id);
+
+	vmcs_write64(IO_BITMAP_A, src->io_bitmap_a);
+	vmcs_write64(IO_BITMAP_B, src->io_bitmap_b);
+
+	if (cpu_has_vmx_msr_bitmap())
+		vmcs_write64(MSR_BITMAP, src->msr_bitmap);
+
+	vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, src->virtual_apic_page_addr);
+
+	if (vm_need_virtualize_apic_accesses(vcpu->kvm))
+		vmcs_write64(APIC_ACCESS_ADDR,
+			     src->apic_access_addr);
+
+	if (enable_ept) {
+		vmcs_write64(EPT_POINTER, src->ept_pointer);
+		vmcs_write64(GUEST_PDPTR0, src->guest_pdptr0);
+		vmcs_write64(GUEST_PDPTR1, src->guest_pdptr1);
+		vmcs_write64(GUEST_PDPTR2, src->guest_pdptr2);
+		vmcs_write64(GUEST_PDPTR3, src->guest_pdptr3);
+	}
+
+	vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, src->pin_based_vm_exec_control);
+	vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, src->cpu_based_vm_exec_control);
+	vmcs_write32(EXCEPTION_BITMAP, src->exception_bitmap);
+	vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
+		     src->page_fault_error_code_mask);
+	vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
+		     src->page_fault_error_code_match);
+	vmcs_write32(VM_EXIT_CONTROLS, src->vm_exit_controls);
+	vmcs_write32(VM_ENTRY_CONTROLS, src->vm_entry_controls);
+	vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->vm_entry_msr_load_count);
+
+	if (cpu_has_secondary_exec_ctrls())
+		vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
+			     src->secondary_vm_exec_control);
+
+	load_vmcs_common(src);
+
+	load_vmcs_host_state(to_vmx(vcpu)->nested.l1_state->shadow_vmcs);
+
+	return 0;
+}
+
+void sync_cached_regs_to_vmcs(struct kvm_vcpu *vcpu)
+{
+	unsigned long mask;
+
+	if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
+		vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
+	if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
+		vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
+
+	mask = ~((1 << VCPU_REGS_RSP) | (1 << VCPU_REGS_RIP));
+
+	if (vcpu->arch.regs_dirty & mask) {
+		printk(KERN_INFO "WARNING: dirty cached registers regs_dirty 0x%x mask 0x%lx\n",
+		       vcpu->arch.regs_dirty, mask);
+		WARN_ON(1);
+	}
+
+	vcpu->arch.regs_dirty = 0;
+}
+
+static int nested_vmx_run(struct kvm_vcpu *vcpu)
+{
+	/* verify that l1 has done vmptrld for l2 earlier */
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int initial_pfu_active = vcpu->fpu_active;
+	int r = 0;
+
+	if (vmx->nested.nested_mode) {
+		printk(KERN_INFO "Nested guest already running\n");
+		set_rflags_to_vmx_fail_valid(vcpu);
+		return 1;
+	}
+
+
+	vmx->nested.nested_mode = 1;
+
+	vcpu->arch.exception.pending = false;
+
+	sync_cached_regs_to_vmcs(vcpu);
+
+	save_vmcs(vmx->nested.l1_state->shadow_vmcs);
+
+	vmx->nested.l1_state->shadow_efer = vcpu->arch.shadow_efer;
+	if (!enable_ept)
+		vmx->nested.l1_state->cr3 = vcpu->arch.cr3;
+	vmx->nested.l1_state->cr4 = vcpu->arch.cr4;
+
+	if (enable_vpid) {
+		if (vmx->nested.l2_state->vpid == 0) {
+			allocate_vpid(vmx);
+			vmx->nested.l2_state->vpid = vmx->vpid;
+		}
+	}
+
+	if (cpu_has_vmx_msr_bitmap())
+		vmx->nested.l1_state->msr_bitmap = vmcs_read64(MSR_BITMAP);
+	else
+		vmx->nested.l1_state->msr_bitmap = 0;
+
+	vmx->nested.l1_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+	vmx->nested.l1_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+	vmx->nested.l1_state->vmcs = vmx->vmcs;
+	vmx->nested.l1_state->cpu = vcpu->cpu;
+	vmx->nested.l1_state->launched = vmx->launched;
+
+	vmx->vmcs = vmx->nested.l2_state->vmcs;
+	vcpu->cpu = vmx->nested.l2_state->cpu;
+	vmx->launched = vmx->nested.l2_state->launched;
+
+	if (vmx->nested.l2_state->vmclear || !vmx->launched) {
+		vmcs_clear(vmx->vmcs);
+		vmx->launched = 0;
+		vmx->nested.l2_state->vmclear = 0;
+	}
+
+	vmx_vcpu_load(vcpu, get_cpu());
+	put_cpu();
+
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		set_rflags_to_vmx_fail_valid(vcpu);
+		return 1;
+	}
+
+	prepare_vmcs_02(vcpu);
+
+	if (get_shadow_vmcs(vcpu)->vm_entry_controls &
+	    VM_ENTRY_IA32E_MODE) {
+		if (!((vcpu->arch.shadow_efer & EFER_LMA) &&
+		      (vcpu->arch.shadow_efer & EFER_LME)))
+			vcpu->arch.shadow_efer |= (EFER_LMA | EFER_LME);
+	} else {
+		if ((vcpu->arch.shadow_efer & EFER_LMA) ||
+		    (vcpu->arch.shadow_efer & EFER_LME))
+			vcpu->arch.shadow_efer = 0;
+	}
+
+	vmx_set_cr0(vcpu, get_shadow_vmcs(vcpu)->guest_cr0);
+	vmcs_writel(CR0_READ_SHADOW,
+		    get_shadow_vmcs(vcpu)->cr0_read_shadow);
+	vmx_set_cr4(vcpu, get_shadow_vmcs(vcpu)->guest_cr4);
+	vmcs_writel(CR4_READ_SHADOW,
+		    get_shadow_vmcs(vcpu)->cr4_read_shadow);
+
+	vcpu->arch.cr0 |= X86_CR0_PG;
+
+	if (enable_ept && !nested_cpu_has_vmx_ept(vcpu)) {
+		vmcs_write32(GUEST_CR3, get_shadow_vmcs(vcpu)->guest_cr3);
+		vmx->vcpu.arch.cr3 = get_shadow_vmcs(vcpu)->guest_cr3;
+	} else {
+		kvm_set_cr3(vcpu, get_shadow_vmcs(vcpu)->guest_cr3);
+		kvm_mmu_reset_context(vcpu);
+
+		nested_unmap_shadow_vmcs(vcpu);
+
+		r = kvm_mmu_load(vcpu);
+		if (unlikely(r)) {
+			printk(KERN_ERR "Error in kvm_mmu_load r %d\n", r);
+			nested_vmx_vmexit(vcpu, false);
+			set_rflags_to_vmx_fail_valid(vcpu);
+			return 1;
+		}
+
+		nested_map_shadow_vmcs(vcpu);
+	}
+
+	kvm_register_write(vcpu, VCPU_REGS_RSP,
+			   get_shadow_vmcs(vcpu)->guest_rsp);
+	kvm_register_write(vcpu, VCPU_REGS_RIP,
+			   get_shadow_vmcs(vcpu)->guest_rip);
+
+	vmcs_write32(EXCEPTION_BITMAP,
+		     (vmx->nested.l1_state->shadow_vmcs->exception_bitmap |
+		      get_shadow_vmcs(vcpu)->exception_bitmap));
+
+	nested_unmap_shadow_vmcs(vcpu);
+
+	if (initial_pfu_active)
+		vmx_fpu_activate(vcpu);
+
+	return 1;
+}
+
+static int launch_guest(struct kvm_vcpu *vcpu)
+{
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	skip_emulated_instruction(vcpu);
+
+	nested_vmx_run(vcpu);
+
+	return 1;
+}
+
+static int nested_vmx_vmexit(struct kvm_vcpu *vcpu,
+			     bool is_interrupt)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int initial_pfu_active = vcpu->fpu_active;
+
+	if (!vmx->nested.nested_mode) {
+		printk(KERN_INFO "WARNING: %s called but not in nested mode\n",
+		       __func__);
+		return 0;
+	}
+
+	save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
+
+	sync_cached_regs_to_vmcs(vcpu);
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		printk(KERN_INFO "Error mapping shadow vmcs\n");
+		set_rflags_to_vmx_fail_valid(vcpu);
+		return 1;
+	}
+
+	prepare_vmcs_12(vcpu);
+	if (is_interrupt)
+		get_shadow_vmcs(vcpu)->vm_exit_reason =
+			EXIT_REASON_EXTERNAL_INTERRUPT;
+
+	vmx->nested.l2_state->launched = vmx->launched;
+	vmx->nested.l2_state->cpu = vcpu->cpu;
+
+	nested_unmap_shadow_vmcs(vcpu);
+
+	vmx->vmcs = vmx->nested.l1_state->vmcs;
+	vcpu->cpu = vmx->nested.l1_state->cpu;
+	vmx->launched = vmx->nested.l1_state->launched;
+
+	vmx_vcpu_load(vcpu, get_cpu());
+	put_cpu();
+
+	vcpu->arch.exception.pending = false;
+
+	vcpu->arch.shadow_efer = vmx->nested.l1_state->shadow_efer;
+	vmx_set_cr0(vcpu, vmx->nested.l1_state->shadow_vmcs->cr0_read_shadow);
+	vmx_set_cr4(vcpu, vmx->nested.l1_state->cr4);
+
+	if (enable_ept) {
+		vcpu->arch.cr3 = vmx->nested.l1_state->shadow_vmcs->guest_cr3;
+		vmcs_write32(GUEST_CR3, vmx->nested.l1_state->shadow_vmcs->guest_cr3);
+	} else {
+		kvm_set_cr3(vcpu, vmx->nested.l1_state->cr3);
+	}
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		printk(KERN_INFO "Error mapping shadow vmcs\n");
+		set_rflags_to_vmx_fail_valid(vcpu);
+		return 1;
+	}
+
+	switch_back_vmcs(vcpu);
+
+	nested_unmap_shadow_vmcs(vcpu);
+
+	kvm_register_write(vcpu, VCPU_REGS_RSP,
+			   vmx->nested.l1_state->shadow_vmcs->guest_rsp);
+	kvm_register_write(vcpu, VCPU_REGS_RIP,
+			   vmx->nested.l1_state->shadow_vmcs->guest_rip);
+
+	vmx->nested.nested_mode = 0;
+
+	kvm_mmu_reset_context(vcpu);
+	kvm_mmu_load(vcpu);
+
+	if (unlikely(vmx->fail)) {
+		vmx->fail = 0;
+		set_rflags_to_vmx_fail_valid(vcpu);
+	} else
+		clear_rflags_cf_zf(vcpu);
+
+	if (initial_pfu_active)
+		vmx_fpu_activate(vcpu);
+
+	return 0;
+}
+
+static int nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu)
+{
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		struct page *msr_page = NULL;
+		u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
+		u32 exit_code = vmcs_read32(VM_EXIT_REASON);
+		struct shadow_vmcs *l2svmcs = get_shadow_vmcs(vcpu);
+
+		if (!cpu_has_vmx_msr_bitmap()
+		    || !nested_cpu_has_vmx_msr_bitmap(vcpu))
+			return 1;
+
+		msr_page = nested_get_page(vcpu,
+					   l2svmcs->msr_bitmap);
+
+		if (!msr_page) {
+			printk(KERN_INFO "%s error in nested_get_page\n",
+			       __func__);
+			return 0;
+		}
+
+		switch (exit_code) {
+		case EXIT_REASON_MSR_READ:
+			if (msr_index <= 0x1fff) {
+				if (test_bit(msr_index,
+					     (unsigned long *)(msr_page +
+							       0x000)))
+					return 1;
+			} else if ((msr_index >= 0xc0000000) &&
+				   (msr_index <= 0xc0001fff)) {
+				msr_index &= 0x1fff;
+				if (test_bit(msr_index,
+					     (unsigned long *)(msr_page +
+							       0x400)))
+					return 1;
+			}
+			break;
+		case EXIT_REASON_MSR_WRITE:
+			if (msr_index <= 0x1fff) {
+				if (test_bit(msr_index,
+					     (unsigned long *)(msr_page +
+							       0x800)))
+						return 1;
+			} else if ((msr_index >= 0xc0000000) &&
+				   (msr_index <= 0xc0001fff)) {
+				msr_index &= 0x1fff;
+				if (test_bit(msr_index,
+					     (unsigned long *)(msr_page +
+							       0xc00)))
+					return 1;
+			}
+			break;
+		}
+	}
+
+	return 0;
+}
+
+static int nested_vmx_exit_handled(struct kvm_vcpu *vcpu, bool kvm_override)
+{
+	u32 exit_code = vmcs_read32(VM_EXIT_REASON);
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+	struct shadow_vmcs *l2svmcs;
+
+	int r = 0;
+
+	if (vmx->nested.nested_run_pending)
+		return 0;
+
+	if (unlikely(vmx->fail)) {
+		printk(KERN_INFO "%s failed vm entry %x\n",
+		       __func__, vmcs_read32(VM_INSTRUCTION_ERROR));
+		return 1;
+	}
+
+	if (kvm_override) {
+		switch (exit_code) {
+		case EXIT_REASON_EXTERNAL_INTERRUPT:
+			return 0;
+		case EXIT_REASON_EXCEPTION_NMI:
+			if (!is_exception(intr_info))
+				return 0;
+
+			if (is_page_fault(intr_info) && (!enable_ept))
+				return 0;
+
+			break;
+		case EXIT_REASON_EPT_VIOLATION:
+			if (enable_ept)
+				return 0;
+
+			break;
+		}
+	}
+
+
+	if (!nested_map_shadow_vmcs(vcpu))
+		return 0;
+	l2svmcs = get_shadow_vmcs(vcpu);
+
+	switch (exit_code) {
+	case EXIT_REASON_INVLPG:
+		if (l2svmcs->cpu_based_vm_exec_control &
+		    CPU_BASED_INVLPG_EXITING)
+			r = 1;
+		break;
+	case EXIT_REASON_MSR_READ:
+	case EXIT_REASON_MSR_WRITE:
+		r = nested_vmx_exit_handled_msr(vcpu);
+		break;
+	case EXIT_REASON_CR_ACCESS: {
+		unsigned long exit_qualification =
+			vmcs_readl(EXIT_QUALIFICATION);
+		int cr = exit_qualification & 15;
+		int reg = (exit_qualification >> 8) & 15;
+		unsigned long val = kvm_register_read(vcpu, reg);
+
+		switch ((exit_qualification >> 4) & 3) {
+		case 0: /* mov to cr */
+			switch (cr) {
+			case 0:
+				if (l2svmcs->cr0_guest_host_mask &
+				    (val ^ l2svmcs->cr0_read_shadow))
+					r = 1;
+				break;
+			case 3:
+				if (l2svmcs->cpu_based_vm_exec_control &
+				    CPU_BASED_CR3_LOAD_EXITING)
+					r = 1;
+				break;
+			case 4:
+				if (l2svmcs->cr4_guest_host_mask &
+				    (l2svmcs->cr4_read_shadow ^ val))
+					r = 1;
+				break;
+			case 8:
+				if (l2svmcs->cpu_based_vm_exec_control &
+				    CPU_BASED_CR8_LOAD_EXITING)
+					r = 1;
+				break;
+			}
+			break;
+		case 2: /* clts */
+			if (l2svmcs->cr0_guest_host_mask &
+			    (val ^ l2svmcs->cr0_read_shadow))
+				r = 1;
+			break;
+		case 1: /*mov from cr*/
+			switch (cr) {
+			case 0:
+				r = 1;
+			case 3:
+				if (l2svmcs->cpu_based_vm_exec_control &
+				    CPU_BASED_CR3_STORE_EXITING)
+					r = 1;
+				break;
+			case 4:
+				r = 1;
+				break;
+			case 8:
+				if (l2svmcs->cpu_based_vm_exec_control &
+				    CPU_BASED_CR8_STORE_EXITING)
+					r = 1;
+				break;
+			}
+			break;
+		case 3: /* lmsw */
+			if (l2svmcs->cr0_guest_host_mask &
+			    (val ^ l2svmcs->cr0_read_shadow))
+				r = 1;
+			break;
+		}
+		break;
+	}
+	case EXIT_REASON_DR_ACCESS: {
+		if (l2svmcs->cpu_based_vm_exec_control &
+		    CPU_BASED_MOV_DR_EXITING)
+			r = 1;
+		break;
+	}
+
+	case EXIT_REASON_EXCEPTION_NMI: {
+
+		if (is_external_interrupt(intr_info) &&
+		    (l2svmcs->pin_based_vm_exec_control &
+		     PIN_BASED_EXT_INTR_MASK))
+			r = 1;
+		else if (is_nmi(intr_info) &&
+		    (l2svmcs->pin_based_vm_exec_control &
+		     PIN_BASED_NMI_EXITING))
+			r = 1;
+		else if (is_exception(intr_info) &&
+		    (l2svmcs->exception_bitmap &
+		     (1u << (intr_info & INTR_INFO_VECTOR_MASK))))
+			r = 1;
+		else if (is_page_fault(intr_info))
+			r = 1;
+		break;
+	}
+
+	case EXIT_REASON_EXTERNAL_INTERRUPT:
+		if (l2svmcs->pin_based_vm_exec_control &
+		    PIN_BASED_EXT_INTR_MASK)
+			r = 1;
+		break;
+	default:
+		r = 1;
+	}
+	nested_unmap_shadow_vmcs(vcpu);
+
+	return r;
+}
+
+static int nested_vmx_check_exception(struct vcpu_vmx *vmx, unsigned nr,
+				      bool has_error_code, u32 error_code)
+{
+	if (vmx->nested.nested_mode) {
+		if (nested_vmx_exit_handled(&vmx->vcpu, false)) {
+			nested_vmx_vmexit(&vmx->vcpu, false);
+			if (!nested_map_shadow_vmcs(&vmx->vcpu))
+				return 1;
+			get_shadow_vmcs(&vmx->vcpu)->vm_exit_reason =
+				EXIT_REASON_EXCEPTION_NMI;
+			get_shadow_vmcs(&vmx->vcpu)->vm_exit_intr_info =
+				(nr | INTR_TYPE_HARD_EXCEPTION
+				 | (has_error_code ?
+				    INTR_INFO_DELIVER_CODE_MASK : 0)
+				 | INTR_INFO_VALID_MASK);
+
+			if (has_error_code)
+				get_shadow_vmcs(&vmx->vcpu)->
+					vm_exit_intr_error_code = error_code;
+			nested_unmap_shadow_vmcs(&vmx->vcpu);
+			return 1;
+		}
+	}
+	return 0;
+}
+
+static int nested_vmx_intr(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (vmx->nested.nested_mode) {
+
+		if (!nested_map_shadow_vmcs(vcpu))
+			return 0;
+
+		if (get_shadow_vmcs(vcpu)->pin_based_vm_exec_control &
+		    PIN_BASED_EXT_INTR_MASK) {
+
+			if (vmx->nested.nested_run_pending) {
+				nested_unmap_shadow_vmcs(vcpu);
+				return 0;
+			}
+
+			nested_unmap_shadow_vmcs(vcpu);
+			nested_vmx_vmexit(vcpu, true);
+			return 1;		
+		}
+
+		nested_unmap_shadow_vmcs(vcpu);
+
+	}
+
+	return 0;
+}
 
 static struct kvm_x86_ops vmx_x86_ops = {
 	.cpu_has_kvm_support = cpu_has_kvm_support,
Signature · 1 line
1.6.0.4
#6 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 364 lines · On Thu, Oct 15, 2009 at 04:41:46PM +0200, [email protected] wrote:

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c | 1173 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 1148 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx.c +281 -0
index 6a4c252..e814029 100644
@@ -209,6 +209,7 @@ struct __attribute__ ((__packed__)) level_state {
 	struct vmcs *vmcs;
 	int cpu;
 	int launched;
+	bool first_launch;
 };
 
 struct nested_vmx {
@@ -216,6 +217,12 @@ struct nested_vmx {
 	bool vmxon;
 	/* What is the location of the  vmcs l1 keeps for l2? (in level1 gpa) */
 	u64 vmptr;
+	/* Are we running nested guest */
+	bool nested_mode;
+	/* L1 requested VMLAUNCH or VMRESUME but we didn't run L2 yet */
+	bool nested_run_pending;
+	/* flag indicating if there was a valid IDT after exiting from l2 */
+	bool nested_valid_idt;
 	/*
 	 * Level 2 state : includes vmcs,registers and
 	 * a copy of vmcs12 for vmread/vmwrite
@@ -240,6 +247,10 @@ static inline int vmcs_field_length(unsigned long field)
 	return (VMCS_FIELD_LENGTH_MASK & field) >> 13;
 }
 
+#define NESTED_VM_EXIT_CONTROLS_MASK (~(VM_EXIT_LOAD_IA32_PAT | \
+					VM_EXIT_SAVE_IA32_PAT))
+#define NESTED_VM_ENTRY_CONTROLS_MASK (~(VM_ENTRY_LOAD_IA32_PAT | \
+					 VM_ENTRY_IA32E_MODE))
 struct vmcs {
 	u32 revision_id;
 	u32 abort;
@@ -303,6 +314,12 @@ static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
 	return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+static inline struct shadow_vmcs *get_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+	WARN_ON(!to_vmx(vcpu)->nested.l2_state->shadow_vmcs);
+	return to_vmx(vcpu)->nested.l2_state->shadow_vmcs;
+}
+
 #define SHADOW_VMCS_OFFSET(x) offsetof(struct shadow_vmcs, x)
 
 static unsigned short vmcs_field_to_offset_table[HOST_RIP+1] = {
@@ -822,8 +839,16 @@ static struct kvm_vmx_segment_field {
 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
 
 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu);
+static int nested_vmx_check_exception(struct vcpu_vmx *vmx, unsigned nr,
+				      bool has_error_code, u32 error_code);
+static int nested_vmx_intr(struct kvm_vcpu *vcpu);
 static int create_l1_state(struct kvm_vcpu *vcpu);
 static int create_l2_state(struct kvm_vcpu *vcpu);
+static int launch_guest(struct kvm_vcpu *vcpu);
+static int nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu);
+static int nested_vmx_exit_handled(struct kvm_vcpu *vcpu, bool kvm_override);
+static int nested_vmx_vmexit(struct kvm_vcpu *vcpu,
+			     bool is_interrupt);
 
 /*
  * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
@@ -940,6 +965,18 @@ static inline bool cpu_has_vmx_ept_2m_page(void)
 	return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
 }
 
+static inline int is_exception(u32 intr_info)
+{
+	return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
+		== (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
+}
+
+static inline int is_nmi(u32 intr_info)
+{
+	return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
+		== (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
+}
+
 static inline int cpu_has_vmx_invept_individual_addr(void)
 {
 	return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
@@ -990,6 +1027,51 @@ static inline bool report_flexpriority(void)
 	return flexpriority_enabled;
 }
 
+static inline int nested_cpu_has_vmx_tpr_shadow(struct  kvm_vcpu *vcpu)
+{
+	return cpu_has_vmx_tpr_shadow() &&
+		get_shadow_vmcs(vcpu)->cpu_based_vm_exec_control &
+		CPU_BASED_TPR_SHADOW;
+}
+
+static inline int nested_cpu_has_secondary_exec_ctrls(struct kvm_vcpu *vcpu)
+{
+	return cpu_has_secondary_exec_ctrls() &&
+		get_shadow_vmcs(vcpu)->cpu_based_vm_exec_control &
+		CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
+}
+
+static inline bool nested_vm_need_virtualize_apic_accesses(struct kvm_vcpu
+							   *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->secondary_vm_exec_control &
+		SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
+}
+
+static inline int nested_cpu_has_vmx_ept(struct kvm_vcpu *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->
+		secondary_vm_exec_control & SECONDARY_EXEC_ENABLE_EPT;
+}
+
+static inline int nested_cpu_has_vmx_vpid(struct kvm_vcpu *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->secondary_vm_exec_control &
+		SECONDARY_EXEC_ENABLE_VPID;
+}
+
+static inline int nested_cpu_has_vmx_pat(struct kvm_vcpu *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->vm_entry_controls &
+		VM_ENTRY_LOAD_IA32_PAT;
+}
+
+static inline int nested_cpu_has_vmx_msr_bitmap(struct kvm_vcpu *vcpu)
+{
+	return get_shadow_vmcs(vcpu)->cpu_based_vm_exec_control &
+		CPU_BASED_USE_MSR_BITMAPS;
+}
+
 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
 {
 	int i;
@@ -1501,6 +1583,9 @@ static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	u32 intr_info = nr | INTR_INFO_VALID_MASK;
 
+	if (nested_vmx_check_exception(vmx, nr, has_error_code, error_code))
+		return;
+
 	if (has_error_code) {
 		vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
 		intr_info |= INTR_INFO_DELIVER_CODE_MASK;
@@ -1943,6 +2028,200 @@ static void vmclear_local_vcpus(void)
 		__vcpu_clear(vmx);
 }
 
+void prepare_vmcs_12(struct kvm_vcpu *vcpu)
+{
+	struct shadow_vmcs *l2_shadow_vmcs =
+		get_shadow_vmcs(vcpu);
+
+	l2_shadow_vmcs->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
+	l2_shadow_vmcs->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
+	l2_shadow_vmcs->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
+	l2_shadow_vmcs->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
+	l2_shadow_vmcs->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
+	l2_shadow_vmcs->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
+	l2_shadow_vmcs->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
+	l2_shadow_vmcs->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
+
+	l2_shadow_vmcs->tsc_offset = vmcs_read64(TSC_OFFSET);
+	l2_shadow_vmcs->guest_physical_address =
+		vmcs_read64(GUEST_PHYSICAL_ADDRESS);
+	l2_shadow_vmcs->vmcs_link_pointer = vmcs_read64(VMCS_LINK_POINTER);
+	l2_shadow_vmcs->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+	if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
+		l2_shadow_vmcs->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
+	l2_shadow_vmcs->cr3_target_count = vmcs_read32(CR3_TARGET_COUNT);
+	l2_shadow_vmcs->vm_entry_intr_info_field =
+		vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
+	l2_shadow_vmcs->vm_entry_exception_error_code =
+		vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE);
+	l2_shadow_vmcs->vm_entry_instruction_len =
+		vmcs_read32(VM_ENTRY_INSTRUCTION_LEN);
+	l2_shadow_vmcs->vm_instruction_error =
+		vmcs_read32(VM_INSTRUCTION_ERROR);
+	l2_shadow_vmcs->vm_exit_reason  = vmcs_read32(VM_EXIT_REASON);
+	l2_shadow_vmcs->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+	l2_shadow_vmcs->vm_exit_intr_error_code =
+		vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
+	l2_shadow_vmcs->idt_vectoring_info_field =
+		vmcs_read32(IDT_VECTORING_INFO_FIELD);
+	l2_shadow_vmcs->idt_vectoring_error_code =
+		vmcs_read32(IDT_VECTORING_ERROR_CODE);
+	l2_shadow_vmcs->vm_exit_instruction_len =
+		vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
+	l2_shadow_vmcs->vmx_instruction_info =
+		vmcs_read32(VMX_INSTRUCTION_INFO);
+	l2_shadow_vmcs->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
+	l2_shadow_vmcs->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
+	l2_shadow_vmcs->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
+	l2_shadow_vmcs->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
+	l2_shadow_vmcs->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
+	l2_shadow_vmcs->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
+	l2_shadow_vmcs->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
+	l2_shadow_vmcs->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
+	l2_shadow_vmcs->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
+	l2_shadow_vmcs->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
+	l2_shadow_vmcs->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
+	l2_shadow_vmcs->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
+	l2_shadow_vmcs->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
+	l2_shadow_vmcs->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
+	l2_shadow_vmcs->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
+	l2_shadow_vmcs->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
+	l2_shadow_vmcs->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
+	l2_shadow_vmcs->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
+	l2_shadow_vmcs->guest_interruptibility_info =
+		vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+	l2_shadow_vmcs->guest_activity_state =
+		vmcs_read32(GUEST_ACTIVITY_STATE);
+	l2_shadow_vmcs->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
+
+	l2_shadow_vmcs->cr0_read_shadow = vmcs_readl(CR0_READ_SHADOW);
+	l2_shadow_vmcs->cr4_read_shadow = vmcs_readl(CR4_READ_SHADOW);
+	l2_shadow_vmcs->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	l2_shadow_vmcs->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
+	l2_shadow_vmcs->guest_cr0 = vmcs_readl(GUEST_CR0);
+
+	l2_shadow_vmcs->guest_cr4 = vmcs_readl(GUEST_CR4);
+	l2_shadow_vmcs->guest_es_base = vmcs_readl(GUEST_ES_BASE);
+	l2_shadow_vmcs->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
+	l2_shadow_vmcs->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
+	l2_shadow_vmcs->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
+	l2_shadow_vmcs->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
+	l2_shadow_vmcs->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
+	l2_shadow_vmcs->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
+	l2_shadow_vmcs->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
+	l2_shadow_vmcs->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
+	l2_shadow_vmcs->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
+	l2_shadow_vmcs->guest_dr7 = vmcs_readl(GUEST_DR7);
+	l2_shadow_vmcs->guest_rsp = vmcs_readl(GUEST_RSP);
+	l2_shadow_vmcs->guest_rip = vmcs_readl(GUEST_RIP);
+	l2_shadow_vmcs->guest_rflags = vmcs_readl(GUEST_RFLAGS);
+	l2_shadow_vmcs->guest_pending_dbg_exceptions =
+		vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
+	l2_shadow_vmcs->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
+	l2_shadow_vmcs->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
+}
+
+int load_vmcs_common(struct shadow_vmcs *src)
+{
+	vmcs_write16(GUEST_ES_SELECTOR, src->guest_es_selector);
+	vmcs_write16(GUEST_CS_SELECTOR, src->guest_cs_selector);
+	vmcs_write16(GUEST_SS_SELECTOR, src->guest_ss_selector);
+	vmcs_write16(GUEST_DS_SELECTOR, src->guest_ds_selector);
+	vmcs_write16(GUEST_FS_SELECTOR, src->guest_fs_selector);
+	vmcs_write16(GUEST_GS_SELECTOR, src->guest_gs_selector);
+	vmcs_write16(GUEST_LDTR_SELECTOR, src->guest_ldtr_selector);
+	vmcs_write16(GUEST_TR_SELECTOR, src->guest_tr_selector);
+
+	vmcs_write64(VMCS_LINK_POINTER, src->vmcs_link_pointer);
+	vmcs_write64(GUEST_IA32_DEBUGCTL, src->guest_ia32_debugctl);
+
+	if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
+		vmcs_write64(GUEST_IA32_PAT, src->guest_ia32_pat);
+
+	vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->vm_entry_msr_load_count);
+	vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, src->vm_entry_intr_info_field);
+	vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
+		     src->vm_entry_exception_error_code);
+	vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, src->vm_entry_instruction_len);
+
+	vmcs_write32(GUEST_ES_LIMIT, src->guest_es_limit);
+	vmcs_write32(GUEST_CS_LIMIT, src->guest_cs_limit);
+	vmcs_write32(GUEST_SS_LIMIT, src->guest_ss_limit);
+	vmcs_write32(GUEST_DS_LIMIT, src->guest_ds_limit);
+	vmcs_write32(GUEST_FS_LIMIT, src->guest_fs_limit);
+	vmcs_write32(GUEST_GS_LIMIT, src->guest_gs_limit);
+	vmcs_write32(GUEST_LDTR_LIMIT, src->guest_ldtr_limit);
+	vmcs_write32(GUEST_TR_LIMIT, src->guest_tr_limit);
+	vmcs_write32(GUEST_GDTR_LIMIT, src->guest_gdtr_limit);
+	vmcs_write32(GUEST_IDTR_LIMIT, src->guest_idtr_limit);
+	vmcs_write32(GUEST_ES_AR_BYTES, src->guest_es_ar_bytes);
+	vmcs_write32(GUEST_CS_AR_BYTES, src->guest_cs_ar_bytes);
+	vmcs_write32(GUEST_SS_AR_BYTES, src->guest_ss_ar_bytes);
+	vmcs_write32(GUEST_DS_AR_BYTES, src->guest_ds_ar_bytes);
+	vmcs_write32(GUEST_FS_AR_BYTES, src->guest_fs_ar_bytes);
+	vmcs_write32(GUEST_GS_AR_BYTES, src->guest_gs_ar_bytes);
+	vmcs_write32(GUEST_LDTR_AR_BYTES, src->guest_ldtr_ar_bytes);
+	vmcs_write32(GUEST_TR_AR_BYTES, src->guest_tr_ar_bytes);
+	vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
+		     src->guest_interruptibility_info);
+	vmcs_write32(GUEST_ACTIVITY_STATE, src->guest_activity_state);
+	vmcs_write32(GUEST_SYSENTER_CS, src->guest_sysenter_cs);
+
+	vmcs_writel(GUEST_ES_BASE, src->guest_es_base);
+	vmcs_writel(GUEST_CS_BASE, src->guest_cs_base);
+	vmcs_writel(GUEST_SS_BASE, src->guest_ss_base);
+	vmcs_writel(GUEST_DS_BASE, src->guest_ds_base);
+	vmcs_writel(GUEST_FS_BASE, src->guest_fs_base);
+	vmcs_writel(GUEST_GS_BASE, src->guest_gs_base);
+	vmcs_writel(GUEST_LDTR_BASE, src->guest_ldtr_base);
+	vmcs_writel(GUEST_TR_BASE, src->guest_tr_base);
+	vmcs_writel(GUEST_GDTR_BASE, src->guest_gdtr_base);
+	vmcs_writel(GUEST_IDTR_BASE, src->guest_idtr_base);
+	vmcs_writel(GUEST_DR7, src->guest_dr7);
+	vmcs_writel(GUEST_RSP, src->guest_rsp);
+	vmcs_writel(GUEST_RIP, src->guest_rip);
+	vmcs_writel(GUEST_RFLAGS, src->guest_rflags);
+	vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
+		    src->guest_pending_dbg_exceptions);
+	vmcs_writel(GUEST_SYSENTER_ESP, src->guest_sysenter_esp);
+	vmcs_writel(GUEST_SYSENTER_EIP, src->guest_sysenter_eip);
+
+	return 0;
+}
+
+int load_vmcs_host_state(struct shadow_vmcs *src)
+{
+	vmcs_write16(HOST_ES_SELECTOR, src->host_es_selector);
+	vmcs_write16(HOST_CS_SELECTOR, src->host_cs_selector);
+	vmcs_write16(HOST_SS_SELECTOR, src->host_ss_selector);
+	vmcs_write16(HOST_DS_SELECTOR, src->host_ds_selector);
+	vmcs_write16(HOST_FS_SELECTOR, src->host_fs_selector);
+	vmcs_write16(HOST_GS_SELECTOR, src->host_gs_selector);
+	vmcs_write16(HOST_TR_SELECTOR, src->host_tr_selector);
+
+	vmcs_write64(TSC_OFFSET, src->tsc_offset);
+
+	if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT)
+		vmcs_write64(HOST_IA32_PAT, src->host_ia32_pat);
+
+	vmcs_write32(HOST_IA32_SYSENTER_CS, src->host_ia32_sysenter_cs);
+
+	vmcs_writel(HOST_CR0, src->host_cr0);
+	vmcs_writel(HOST_CR3, src->host_cr3);
+	vmcs_writel(HOST_CR4, src->host_cr4);
+	vmcs_writel(HOST_FS_BASE, src->host_fs_base);
+	vmcs_writel(HOST_GS_BASE, src->host_gs_base);
+	vmcs_writel(HOST_TR_BASE, src->host_tr_base);
+	vmcs_writel(HOST_GDTR_BASE, src->host_gdtr_base);
+	vmcs_writel(HOST_IDTR_BASE, src->host_idtr_base);
+	vmcs_writel(HOST_RSP, src->host_rsp);
+	vmcs_writel(HOST_RIP, src->host_rip);
+	vmcs_writel(HOST_IA32_SYSENTER_ESP, src->host_ia32_sysenter_esp);
+	vmcs_writel(HOST_IA32_SYSENTER_EIP, src->host_ia32_sysenter_eip);
+
+	return 0;
+}
+
 struct level_state *create_state(void)
 {
 	struct level_state *state = NULL;
@@ -2003,6 +2282,8 @@ int create_l2_state(struct kvm_vcpu *vcpu)
 	vmx->nested.l2_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
 	vmx->nested.l2_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
 
+	vmx->nested.l2_state->first_launch = true;
+
 	return 0;
 }
 

arch/x86/kvm/vmx.c static void enable_irq_window(struct kvm_vcpu *vcpu) · line 3678

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
@@ -3393,6 +3674,14 @@ static void enable_irq_window(struct kvm_vcpu *vcpu)
 {
 	u32 cpu_based_vm_exec_control;
 
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		if (kvm_cpu_has_interrupt(vcpu)) {

Why interrupt will not be present during the call to enable_irq_window()?

Quoted 21 lines
arch/x86/kvm/vmx.c +10 -0
+			if (nested_vmx_intr(vcpu))
+				return;
+		}
+		return;
+	}
+
 	cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
 	cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
 	vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
@@ -3448,6 +3737,10 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 
+	if (vmx->nested.nested_mode) {
+		return;
+	}
+
 	if (!cpu_has_virtual_nmis()) {
 		/*
 		 * Tracking the NMI-blocked state in software is built upon

arch/x86/kvm/vmx.c static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) · line 3791

Quoted 10 lines
arch/x86/kvm/vmx.c +7 -0
@@ -3489,6 +3782,13 @@ static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
 
 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
 {
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		if (kvm_cpu_has_interrupt(vcpu)) {
+			if (!nested_vmx_intr(vcpu))
+				return 0;
+		}
+	}
+

Same as above. kvm_cpu_has_interrupt() should alway return true here. More interesting question: why not return 0 here if in nested mode and PIN_BASED_EXT_INTR_MASK is not set and let enable_irq_window() to do nested exit. This is what svm does as far as I see.

Quoted 31 lines
arch/x86/kvm/vmx.c +11 -6
 	return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
 		!(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
 			(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
@@ -3993,12 +4293,6 @@ static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
-static int handle_vmx_insn(struct kvm_vcpu *vcpu)
-{
-	kvm_queue_exception(vcpu, UD_VECTOR);
-	return 1;
-}
-
 static void clear_rflags_cf_zf(struct kvm_vcpu *vcpu)
 {
 	unsigned long rflags;
@@ -4040,6 +4334,27 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_vmlaunch(struct kvm_vcpu *vcpu)
+{
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+	if (!to_vmx(vcpu)->nested.l2_state->vmclear)
+		return 1;
+
+	return launch_guest(vcpu);
+}
+
+static int handle_vmresume(struct kvm_vcpu *vcpu)

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4357

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+{
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (to_vmx(vcpu)->nested.l2_state->vmclear)
+		return 1;
+
+	return launch_guest(vcpu);
+}
+

handle_vmlaunch() and handle_vmresume() are looking suspiciously similar may be move vmclear checking logic into launch_guest()? It will get additional parameter: expected value of vmclear.

arch/x86/kvm/vmx.c static int handle_vmread(struct kvm_vcpu *vcpu) · line 4370

Quoted 11 lines
arch/x86/kvm/vmx.c +0 -1
 static int handle_vmread(struct kvm_vcpu *vcpu)
 {
 #ifndef CONFIG_X86_64
@@ -4050,7 +4365,6 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
 		return 1;
 
 	if (!nested_map_shadow_vmcs(vcpu)) {
-		printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
 		set_rflags_to_vmx_fail_invalid(vcpu);
 		return 1;
 	}

Remove from a patch that add it if you don't need this. Also all prinks that can be triggered by a guest should be removed/changed to debug output.

Quoted 9 lines
arch/x86/kvm/vmx.c +0 -1
@@ -4107,7 +4421,6 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)
 		return 1;
 
 	if (!nested_map_shadow_vmcs(vcpu)) {
-		printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
 		set_rflags_to_vmx_fail_invalid(vcpu);
 		return 1;
 	}

arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu *vcpu) · line 4458

Quoted 10 lines
arch/x86/kvm/vmx.c +1 -1
@@ -4137,16 +4450,16 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)
 				   vcpu->arch.regs[VCPU_REGS_RAX]);
 		break;
 	default:
+		nested_unmap_shadow_vmcs(vcpu);
 		printk(KERN_INFO "%s invalid field\n", __func__);
 		set_rflags_to_vmx_fail_valid(vcpu);
 		vmcs_write32(VM_INSTRUCTION_ERROR, 12);
-		nested_unmap_shadow_vmcs(vcpu);
 		return 1;
 	}

Why this is here and not in the patch that introduce the function?

arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu *vcpu) · line 4464

Quoted 7 lines
arch/x86/kvm/vmx.c +1 -1
+	nested_unmap_shadow_vmcs(vcpu);
 	clear_rflags_cf_zf(vcpu);
 	skip_emulated_instruction(vcpu);
-	nested_unmap_shadow_vmcs(vcpu);
 	return 1;
 }

Same.

Quoted 12 lines
arch/x86/kvm/vmx.c +0 -1
@@ -4208,7 +4521,6 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 static int handle_vmptrld(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	struct page *vmcs_page;
 	u64 guest_vmcs_addr;
 
 	if (!nested_vmx_check_permission(vcpu))
@@ -4228,14 +4540,7 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)
 	}
 

arch/x86/kvm/vmx.c static int handle_vmptrld(struct kvm_vcpu *vcpu) · line 4544

Quoted 10 lines
arch/x86/kvm/vmx.c +0 -7
 	if (vmx->nested.vmptr != guest_vmcs_addr) {
-		/* checking vmptr address */
-		vmcs_page = nested_get_page(vcpu, guest_vmcs_addr);
-		if (vmcs_page == NULL)
-			return 1;
-
 		vmx->nested.vmptr = guest_vmcs_addr;
-
-		kvm_release_page_clean(vmcs_page);
 	}

Same.

Quoted 35 lines
arch/x86/kvm/vmx.c +13 -2
 	clear_rflags_cf_zf(vcpu);
@@ -4534,11 +4839,11 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_INVLPG]		      = handle_invlpg,
 	[EXIT_REASON_VMCALL]                  = handle_vmcall,
 	[EXIT_REASON_VMCLEAR]	              = handle_vmclear,
-	[EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
+	[EXIT_REASON_VMLAUNCH]                = handle_vmlaunch,
 	[EXIT_REASON_VMPTRLD]                 = handle_vmptrld,
 	[EXIT_REASON_VMPTRST]                 = handle_vmptrst,
 	[EXIT_REASON_VMREAD]                  = handle_vmread,
-	[EXIT_REASON_VMRESUME]                = handle_vmx_insn,
+	[EXIT_REASON_VMRESUME]                = handle_vmresume,
 	[EXIT_REASON_VMWRITE]                 = handle_vmwrite,
 	[EXIT_REASON_VMOFF]                   = handle_vmoff,
 	[EXIT_REASON_VMON]                    = handle_vmon,
@@ -4566,6 +4871,17 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 
 	trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
 
+	if (exit_reason == EXIT_REASON_VMLAUNCH ||
+	    exit_reason == EXIT_REASON_VMRESUME)
+		vmx->nested.nested_run_pending = 1;
+	else
+		vmx->nested.nested_run_pending = 0;
+
+	if (vmx->nested.nested_mode && nested_vmx_exit_handled(vcpu, true)) {
+		nested_vmx_vmexit(vcpu, false);
+		return 1;
+	}
+
 	/* If we need to emulate an MMIO from handle_invalid_guest_state
 	 * we just return 0 */
 	if (vmx->emulation_required && emulate_invalid_guest_state) {

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 4588

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -1
@@ -4585,7 +4901,6 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 			= vmcs_read32(VM_INSTRUCTION_ERROR);
 		return 0;
 	}
-

No spurious line deletions please.

Quoted 14 lines
arch/x86/kvm/vmx.c +1 -2
 	if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
 			(exit_reason != EXIT_REASON_EXCEPTION_NMI &&
 			exit_reason != EXIT_REASON_EPT_VIOLATION &&
@@ -4593,8 +4908,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 		printk(KERN_WARNING "%s: unexpected, valid vectoring info "
 		       "(0x%x) and exit reason is 0x%x\n",
 		       __func__, vectoring_info, exit_reason);
-
-	if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
+	if (!vmx->nested.nested_mode && unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
 		if (vmx_interrupt_allowed(vcpu)) {
 			vmx->soft_vnmi_blocked = 0;
 		} else if (vmx->vnmi_blocked_time > 1000000000LL &&

arch/x86/kvm/vmx.c static void vmx_complete_interrupts(struct vcpu_vmx *vmx) · line 4962

Quoted 10 lines
arch/x86/kvm/vmx.c +3 -2
@@ -4641,10 +4955,13 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
 	int type;
 	bool idtv_info_valid;
 
-	exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
-
 	vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
+	if (vmx->nested.nested_mode)
+		return;
+

Why return here? What the function does that should not be done in nested mode?

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx *vmx) · line 5068

Quoted 11 lines
arch/x86/kvm/vmx.c +4 -0
+	exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
 	/* Handle machine checks before interrupts are enabled */
 	if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
 	    || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
@@ -4747,6 +5064,60 @@ static void fixup_rmode_irq(struct vcpu_vmx *vmx)
 		| vmx->rmode.irq.vector;
 }
 
+static int nested_handle_valid_idt(struct kvm_vcpu *vcpu)
+{

It seems by this function you are trying to bypass general event reinjection logic. Why?

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx *vmx) · line 5078

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int irq;
+	int type;
+	int errCodeValid;
+	u32 idt_vectoring_info;
+	u32 guest_intr;
+	bool nmi_window_open;
+	bool interrupt_window_open;
+
+	if (vmx->nested.nested_mode && vmx->nested.nested_valid_idt) {

Caller already checked nested_mode why recheck?

Quoted 342 lines
arch/x86/kvm/vmx.c +314 -0
+		idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
+		irq  = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
+		type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
+		errCodeValid = idt_vectoring_info &
+			VECTORING_INFO_DELIVER_CODE_MASK;
+
+		guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+		nmi_window_open =
+			!(guest_intr & (GUEST_INTR_STATE_STI |
+					GUEST_INTR_STATE_MOV_SS |
+					GUEST_INTR_STATE_NMI));
+
+		interrupt_window_open =
+			((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
+			 !(guest_intr & (GUEST_INTR_STATE_STI |
+					 GUEST_INTR_STATE_MOV_SS)));
+
+		if (type == INTR_TYPE_EXT_INTR && !interrupt_window_open) {
+			printk(KERN_INFO "IDT ignored, l2 interrupt window closed!\n");
+			return 0;
+		}
+
+		if (type == INTR_TYPE_NMI_INTR && !nmi_window_open) {
+			printk(KERN_INFO "IDT ignored, l2 nmi window closed!\n");
+			return 0;
+		}
+
+		vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
+			irq | type | INTR_INFO_VALID_MASK | errCodeValid);
+
+
+		vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
+			     vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
+
+		if (errCodeValid)
+			vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
+				     vmcs_read32(IDT_VECTORING_ERROR_CODE));
+	}
+
+	return 1;
+}
+
 #ifdef CONFIG_X86_64
 #define R "r"
 #define Q "q"
@@ -4758,6 +5129,26 @@ static void fixup_rmode_irq(struct vcpu_vmx *vmx)
 static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int r;
+
+	if (vmx->nested.nested_mode) {
+		r = nested_handle_valid_idt(vcpu);
+		if (!r) {
+			vmx->fail = 1;
+			return;
+		}
+
+		if (!nested_map_shadow_vmcs(vcpu)) {
+			vmx->fail = 1;
+			return;
+		}
+
+		vmcs_write32(EXCEPTION_BITMAP, get_shadow_vmcs(vcpu)->
+			     exception_bitmap |
+			     vmx->nested.l1_state->shadow_vmcs->exception_bitmap);
+
+		nested_unmap_shadow_vmcs(vcpu);
+	}
 
 	if (enable_ept && is_paging(vcpu)) {
 		vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
@@ -4896,6 +5287,10 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	get_debugreg(vcpu->arch.dr6, 6);
 
 	vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
+
+	vmx->nested.nested_valid_idt = vmx->nested.nested_mode &&
+		(vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
+
 	if (vmx->rmode.irq.pending)
 		fixup_rmode_irq(vmx);
 
@@ -4984,6 +5379,11 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 			goto free_vmcs;
 	}
 
+	vmx->nested.vmptr = 0;
+
+	vmx->nested.l1_state = NULL;
+	vmx->nested.l2_state = NULL;
+
 	return &vmx->vcpu;
 
 free_vmcs:
@@ -5215,6 +5615,729 @@ void save_vmcs(struct shadow_vmcs *dst)
 	if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT)
 		dst->host_ia32_pat = vmcs_read64(HOST_IA32_PAT);
 }
+int prepare_vmcs_02(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct shadow_vmcs *src = get_shadow_vmcs(vcpu);
+	u32 exec_control;
+
+	if (!src) {
+		printk(KERN_INFO "%s: Error no shadow vmcs\n", __func__);
+		return 1;
+	}
+
+	load_vmcs_common(src);
+
+	if (vmx->nested.l2_state->first_launch) {
+		if (cpu_has_vmx_vpid() && vmx->nested.l2_state->vpid != 0)
+			vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.l2_state->vpid);
+
+		if (vmx->nested.l2_state->io_bitmap_a)
+			vmcs_write64(IO_BITMAP_A, vmx->nested.l2_state->io_bitmap_a);
+
+		if (vmx->nested.l2_state->io_bitmap_b)
+			vmcs_write64(IO_BITMAP_B, vmx->nested.l2_state->io_bitmap_b);
+
+		if (vmx->nested.l2_state->msr_bitmap)
+			vmcs_write64(MSR_BITMAP, vmx->nested.l2_state->msr_bitmap);
+
+		if (src->vm_entry_msr_load_count > 0) {
+			struct page *page;
+
+			page = nested_get_page(vcpu,
+					       src->vm_entry_msr_load_addr);
+			if (!page)
+				return 1;
+
+			vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, page_to_phys(page));
+
+			kvm_release_page_clean(page);
+		}
+
+		if (nested_cpu_has_vmx_tpr_shadow(vcpu)) {
+			struct page *page;
+
+			page = nested_get_page(vcpu,
+					       src->virtual_apic_page_addr);
+			if (!page)
+				return 1;
+
+			vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, page_to_phys(page));
+
+			kvm_release_page_clean(page);
+		}
+
+		if (nested_vm_need_virtualize_apic_accesses(vcpu)) {
+			struct page *page =
+				nested_get_page(vcpu, src->apic_access_addr);
+			if (!page)
+				return 1;
+
+			vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
+			kvm_release_page_clean(page);
+		}
+
+		vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
+			     (vmx->nested.l1_state->shadow_vmcs->pin_based_vm_exec_control |
+			      src->pin_based_vm_exec_control));
+
+		vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
+			     (vmx->nested.l1_state->shadow_vmcs->page_fault_error_code_mask &
+			      src->page_fault_error_code_mask));
+
+		vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
+			     (vmx->nested.l1_state->shadow_vmcs->page_fault_error_code_match &
+			      src->page_fault_error_code_match));
+
+		if (cpu_has_secondary_exec_ctrls()) {
+
+			exec_control =
+				vmx->nested.l1_state->shadow_vmcs->secondary_vm_exec_control;
+
+			if (nested_cpu_has_secondary_exec_ctrls(vcpu)) {
+
+				exec_control |= src->secondary_vm_exec_control;
+
+				if (!vm_need_virtualize_apic_accesses(vcpu->kvm) ||
+				    !nested_vm_need_virtualize_apic_accesses(vcpu))
+					exec_control &=
+						~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
+			}
+
+			vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
+		}
+
+		load_vmcs_host_state(vmx->nested.l1_state->shadow_vmcs);
+
+		vmx->nested.l2_state->first_launch = false;
+	}
+
+	if (vm_need_tpr_shadow(vcpu->kvm) &&
+	    nested_cpu_has_vmx_tpr_shadow(vcpu))
+		vmcs_write32(TPR_THRESHOLD, src->tpr_threshold);
+
+	if (enable_ept) {
+		if (!nested_cpu_has_vmx_ept(vcpu)) {
+			vmcs_write64(EPT_POINTER,
+				     vmx->nested.l1_state->shadow_vmcs->ept_pointer);
+			vmcs_write64(GUEST_PDPTR0,
+				     vmx->nested.l1_state->shadow_vmcs->guest_pdptr0);
+			vmcs_write64(GUEST_PDPTR1,
+				     vmx->nested.l1_state->shadow_vmcs->guest_pdptr1);
+			vmcs_write64(GUEST_PDPTR2,
+				     vmx->nested.l1_state->shadow_vmcs->guest_pdptr2);
+			vmcs_write64(GUEST_PDPTR3,
+				     vmx->nested.l1_state->shadow_vmcs->guest_pdptr3);
+		}
+	}
+
+	exec_control = vmx->nested.l1_state->shadow_vmcs->cpu_based_vm_exec_control;
+
+	exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
+
+	exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
+
+	exec_control &= ~CPU_BASED_TPR_SHADOW;
+
+	exec_control |= src->cpu_based_vm_exec_control;
+
+	if (!vm_need_tpr_shadow(vcpu->kvm) ||
+	    src->virtual_apic_page_addr == 0) {
+		exec_control &= ~CPU_BASED_TPR_SHADOW;
+#ifdef CONFIG_X86_64
+		exec_control |= CPU_BASED_CR8_STORE_EXITING |
+			CPU_BASED_CR8_LOAD_EXITING;
+#endif
+	} else if (exec_control & CPU_BASED_TPR_SHADOW) {
+
+#ifdef CONFIG_X86_64
+		exec_control &= ~CPU_BASED_CR8_STORE_EXITING;
+		exec_control &= ~CPU_BASED_CR8_LOAD_EXITING;
+#endif
+	}
+
+	vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
+
+	vmcs_write32(EXCEPTION_BITMAP,
+		     (vmx->nested.l1_state->shadow_vmcs->exception_bitmap |
+		      src->exception_bitmap));
+
+	vmcs_write32(VM_EXIT_CONTROLS,
+		     ((vmx->nested.l1_state->shadow_vmcs->vm_exit_controls &
+		       NESTED_VM_EXIT_CONTROLS_MASK) | src->vm_exit_controls));
+
+	vmcs_write32(VM_ENTRY_CONTROLS,
+		     (vmx->nested.l1_state->shadow_vmcs->vm_entry_controls &
+		      NESTED_VM_ENTRY_CONTROLS_MASK) | src->vm_entry_controls);
+
+	vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->vm_entry_msr_load_count);
+
+	vmcs_writel(CR0_GUEST_HOST_MASK,
+		    (vmx->nested.l1_state->shadow_vmcs->cr0_guest_host_mask  &
+		     src->cr0_guest_host_mask));
+	vmcs_writel(CR4_GUEST_HOST_MASK,
+		    (vmx->nested.l1_state->shadow_vmcs->cr4_guest_host_mask  &
+		     src->cr4_guest_host_mask));
+
+	return 0;
+}
+
+int switch_back_vmcs(struct kvm_vcpu *vcpu)
+{
+	struct shadow_vmcs *src = to_vmx(vcpu)->nested.l1_state->shadow_vmcs;
+
+	if (enable_vpid && src->virtual_processor_id != 0)
+		vmcs_write16(VIRTUAL_PROCESSOR_ID, src->virtual_processor_id);
+
+	vmcs_write64(IO_BITMAP_A, src->io_bitmap_a);
+	vmcs_write64(IO_BITMAP_B, src->io_bitmap_b);
+
+	if (cpu_has_vmx_msr_bitmap())
+		vmcs_write64(MSR_BITMAP, src->msr_bitmap);
+
+	vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, src->virtual_apic_page_addr);
+
+	if (vm_need_virtualize_apic_accesses(vcpu->kvm))
+		vmcs_write64(APIC_ACCESS_ADDR,
+			     src->apic_access_addr);
+
+	if (enable_ept) {
+		vmcs_write64(EPT_POINTER, src->ept_pointer);
+		vmcs_write64(GUEST_PDPTR0, src->guest_pdptr0);
+		vmcs_write64(GUEST_PDPTR1, src->guest_pdptr1);
+		vmcs_write64(GUEST_PDPTR2, src->guest_pdptr2);
+		vmcs_write64(GUEST_PDPTR3, src->guest_pdptr3);
+	}
+
+	vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, src->pin_based_vm_exec_control);
+	vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, src->cpu_based_vm_exec_control);
+	vmcs_write32(EXCEPTION_BITMAP, src->exception_bitmap);
+	vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
+		     src->page_fault_error_code_mask);
+	vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
+		     src->page_fault_error_code_match);
+	vmcs_write32(VM_EXIT_CONTROLS, src->vm_exit_controls);
+	vmcs_write32(VM_ENTRY_CONTROLS, src->vm_entry_controls);
+	vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->vm_entry_msr_load_count);
+
+	if (cpu_has_secondary_exec_ctrls())
+		vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
+			     src->secondary_vm_exec_control);
+
+	load_vmcs_common(src);
+
+	load_vmcs_host_state(to_vmx(vcpu)->nested.l1_state->shadow_vmcs);
+
+	return 0;
+}
+
+void sync_cached_regs_to_vmcs(struct kvm_vcpu *vcpu)
+{
+	unsigned long mask;
+
+	if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
+		vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
+	if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
+		vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
+
+	mask = ~((1 << VCPU_REGS_RSP) | (1 << VCPU_REGS_RIP));
+
+	if (vcpu->arch.regs_dirty & mask) {
+		printk(KERN_INFO "WARNING: dirty cached registers regs_dirty 0x%x mask 0x%lx\n",
+		       vcpu->arch.regs_dirty, mask);
+		WARN_ON(1);
+	}
+
+	vcpu->arch.regs_dirty = 0;
+}
+
+static int nested_vmx_run(struct kvm_vcpu *vcpu)
+{
+	/* verify that l1 has done vmptrld for l2 earlier */
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int initial_pfu_active = vcpu->fpu_active;
+	int r = 0;
+

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5870

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	if (vmx->nested.nested_mode) {
+		printk(KERN_INFO "Nested guest already running\n");
+		set_rflags_to_vmx_fail_valid(vcpu);
+		return 1;
+	}
+
+
+	vmx->nested.nested_mode = 1;
+
+	vcpu->arch.exception.pending = false;

Why need this?

Quoted 20 lines
arch/x86/kvm/vmx.c +20 -0
+
+	sync_cached_regs_to_vmcs(vcpu);
+
+	save_vmcs(vmx->nested.l1_state->shadow_vmcs);
+
+	vmx->nested.l1_state->shadow_efer = vcpu->arch.shadow_efer;
+	if (!enable_ept)
+		vmx->nested.l1_state->cr3 = vcpu->arch.cr3;
+	vmx->nested.l1_state->cr4 = vcpu->arch.cr4;
+
+	if (enable_vpid) {
+		if (vmx->nested.l2_state->vpid == 0) {
+			allocate_vpid(vmx);
+			vmx->nested.l2_state->vpid = vmx->vpid;
+		}
+	}
+
+	if (cpu_has_vmx_msr_bitmap())
+		vmx->nested.l1_state->msr_bitmap = vmcs_read64(MSR_BITMAP);
+	else

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5900

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+		vmx->nested.l1_state->msr_bitmap = 0;
+
+	vmx->nested.l1_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+	vmx->nested.l1_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+	vmx->nested.l1_state->vmcs = vmx->vmcs;
+	vmx->nested.l1_state->cpu = vcpu->cpu;
+	vmx->nested.l1_state->launched = vmx->launched;
+
+	vmx->vmcs = vmx->nested.l2_state->vmcs;
+	vcpu->cpu = vmx->nested.l2_state->cpu;

Who initialize vmx->nested.l2_state->cpu before first launch? Why have different cpu for l1 and l2 guest? It seems like this is global vcpu thread property.

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5902

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+	vmx->launched = vmx->nested.l2_state->launched;
+

Can you explain why ->launched logic is needed?

Quoted 4 lines
arch/x86/kvm/vmx.c +4 -0
+	if (vmx->nested.l2_state->vmclear || !vmx->launched) {
+		vmcs_clear(vmx->vmcs);
+		vmx->launched = 0;
+		vmx->nested.l2_state->vmclear = 0;

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5916

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	}
+
+	vmx_vcpu_load(vcpu, get_cpu());
+	put_cpu();
+
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		set_rflags_to_vmx_fail_valid(vcpu);
+		return 1;
+	}

No cleanup on error. Is looks like we are on an l2 vmcs at this point.

Quoted 81 lines
arch/x86/kvm/vmx.c +81 -0
+
+	prepare_vmcs_02(vcpu);
+
+	if (get_shadow_vmcs(vcpu)->vm_entry_controls &
+	    VM_ENTRY_IA32E_MODE) {
+		if (!((vcpu->arch.shadow_efer & EFER_LMA) &&
+		      (vcpu->arch.shadow_efer & EFER_LME)))
+			vcpu->arch.shadow_efer |= (EFER_LMA | EFER_LME);
+	} else {
+		if ((vcpu->arch.shadow_efer & EFER_LMA) ||
+		    (vcpu->arch.shadow_efer & EFER_LME))
+			vcpu->arch.shadow_efer = 0;
+	}
+
+	vmx_set_cr0(vcpu, get_shadow_vmcs(vcpu)->guest_cr0);
+	vmcs_writel(CR0_READ_SHADOW,
+		    get_shadow_vmcs(vcpu)->cr0_read_shadow);
+	vmx_set_cr4(vcpu, get_shadow_vmcs(vcpu)->guest_cr4);
+	vmcs_writel(CR4_READ_SHADOW,
+		    get_shadow_vmcs(vcpu)->cr4_read_shadow);
+
+	vcpu->arch.cr0 |= X86_CR0_PG;
+
+	if (enable_ept && !nested_cpu_has_vmx_ept(vcpu)) {
+		vmcs_write32(GUEST_CR3, get_shadow_vmcs(vcpu)->guest_cr3);
+		vmx->vcpu.arch.cr3 = get_shadow_vmcs(vcpu)->guest_cr3;
+	} else {
+		kvm_set_cr3(vcpu, get_shadow_vmcs(vcpu)->guest_cr3);
+		kvm_mmu_reset_context(vcpu);
+
+		nested_unmap_shadow_vmcs(vcpu);
+
+		r = kvm_mmu_load(vcpu);
+		if (unlikely(r)) {
+			printk(KERN_ERR "Error in kvm_mmu_load r %d\n", r);
+			nested_vmx_vmexit(vcpu, false);
+			set_rflags_to_vmx_fail_valid(vcpu);
+			return 1;
+		}
+
+		nested_map_shadow_vmcs(vcpu);
+	}
+
+	kvm_register_write(vcpu, VCPU_REGS_RSP,
+			   get_shadow_vmcs(vcpu)->guest_rsp);
+	kvm_register_write(vcpu, VCPU_REGS_RIP,
+			   get_shadow_vmcs(vcpu)->guest_rip);
+
+	vmcs_write32(EXCEPTION_BITMAP,
+		     (vmx->nested.l1_state->shadow_vmcs->exception_bitmap |
+		      get_shadow_vmcs(vcpu)->exception_bitmap));
+
+	nested_unmap_shadow_vmcs(vcpu);
+
+	if (initial_pfu_active)
+		vmx_fpu_activate(vcpu);
+
+	return 1;
+}
+
+static int launch_guest(struct kvm_vcpu *vcpu)
+{
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	skip_emulated_instruction(vcpu);
+
+	nested_vmx_run(vcpu);
+
+	return 1;
+}
+
+static int nested_vmx_vmexit(struct kvm_vcpu *vcpu,
+			     bool is_interrupt)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int initial_pfu_active = vcpu->fpu_active;
+
+	if (!vmx->nested.nested_mode) {
+		printk(KERN_INFO "WARNING: %s called but not in nested mode\n",
+		       __func__);

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6007

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+		return 0;
+	}
+
+	save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
+
+	sync_cached_regs_to_vmcs(vcpu);
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		printk(KERN_INFO "Error mapping shadow vmcs\n");
+		set_rflags_to_vmx_fail_valid(vcpu);

Error during vmexit should set abort flag, not change flags.

Quoted 11 lines
arch/x86/kvm/vmx.c +11 -0
+		return 1;
+	}
+
+	prepare_vmcs_12(vcpu);
+	if (is_interrupt)
+		get_shadow_vmcs(vcpu)->vm_exit_reason =
+			EXIT_REASON_EXTERNAL_INTERRUPT;
+
+	vmx->nested.l2_state->launched = vmx->launched;
+	vmx->nested.l2_state->cpu = vcpu->cpu;
+

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6028

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	nested_unmap_shadow_vmcs(vcpu);
+
+	vmx->vmcs = vmx->nested.l1_state->vmcs;
+	vcpu->cpu = vmx->nested.l1_state->cpu;
+	vmx->launched = vmx->nested.l1_state->launched;
+
+	vmx_vcpu_load(vcpu, get_cpu());
+	put_cpu();
+
+	vcpu->arch.exception.pending = false;

Why need this?

Quoted 5 lines
arch/x86/kvm/vmx.c +5 -0
+
+	vcpu->arch.shadow_efer = vmx->nested.l1_state->shadow_efer;
+	vmx_set_cr0(vcpu, vmx->nested.l1_state->shadow_vmcs->cr0_read_shadow);
+	vmx_set_cr4(vcpu, vmx->nested.l1_state->cr4);
+

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6043

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	if (enable_ept) {
+		vcpu->arch.cr3 = vmx->nested.l1_state->shadow_vmcs->guest_cr3;
+		vmcs_write32(GUEST_CR3, vmx->nested.l1_state->shadow_vmcs->guest_cr3);
+	} else {
+		kvm_set_cr3(vcpu, vmx->nested.l1_state->cr3);
+	}
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		printk(KERN_INFO "Error mapping shadow vmcs\n");
+		set_rflags_to_vmx_fail_valid(vcpu);

Abort not flags.

Quoted 264 lines
arch/x86/kvm/vmx.c +264 -0
+		return 1;
+	}
+
+	switch_back_vmcs(vcpu);
+
+	nested_unmap_shadow_vmcs(vcpu);
+
+	kvm_register_write(vcpu, VCPU_REGS_RSP,
+			   vmx->nested.l1_state->shadow_vmcs->guest_rsp);
+	kvm_register_write(vcpu, VCPU_REGS_RIP,
+			   vmx->nested.l1_state->shadow_vmcs->guest_rip);
+
+	vmx->nested.nested_mode = 0;
+
+	kvm_mmu_reset_context(vcpu);
+	kvm_mmu_load(vcpu);
+
+	if (unlikely(vmx->fail)) {
+		vmx->fail = 0;
+		set_rflags_to_vmx_fail_valid(vcpu);
+	} else
+		clear_rflags_cf_zf(vcpu);
+
+	if (initial_pfu_active)
+		vmx_fpu_activate(vcpu);
+
+	return 0;
+}
+
+static int nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu)
+{
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		struct page *msr_page = NULL;
+		u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
+		u32 exit_code = vmcs_read32(VM_EXIT_REASON);
+		struct shadow_vmcs *l2svmcs = get_shadow_vmcs(vcpu);
+
+		if (!cpu_has_vmx_msr_bitmap()
+		    || !nested_cpu_has_vmx_msr_bitmap(vcpu))
+			return 1;
+
+		msr_page = nested_get_page(vcpu,
+					   l2svmcs->msr_bitmap);
+
+		if (!msr_page) {
+			printk(KERN_INFO "%s error in nested_get_page\n",
+			       __func__);
+			return 0;
+		}
+
+		switch (exit_code) {
+		case EXIT_REASON_MSR_READ:
+			if (msr_index <= 0x1fff) {
+				if (test_bit(msr_index,
+					     (unsigned long *)(msr_page +
+							       0x000)))
+					return 1;
+			} else if ((msr_index >= 0xc0000000) &&
+				   (msr_index <= 0xc0001fff)) {
+				msr_index &= 0x1fff;
+				if (test_bit(msr_index,
+					     (unsigned long *)(msr_page +
+							       0x400)))
+					return 1;
+			}
+			break;
+		case EXIT_REASON_MSR_WRITE:
+			if (msr_index <= 0x1fff) {
+				if (test_bit(msr_index,
+					     (unsigned long *)(msr_page +
+							       0x800)))
+						return 1;
+			} else if ((msr_index >= 0xc0000000) &&
+				   (msr_index <= 0xc0001fff)) {
+				msr_index &= 0x1fff;
+				if (test_bit(msr_index,
+					     (unsigned long *)(msr_page +
+							       0xc00)))
+					return 1;
+			}
+			break;
+		}
+	}
+
+	return 0;
+}
+
+static int nested_vmx_exit_handled(struct kvm_vcpu *vcpu, bool kvm_override)
+{
+	u32 exit_code = vmcs_read32(VM_EXIT_REASON);
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+	struct shadow_vmcs *l2svmcs;
+
+	int r = 0;
+
+	if (vmx->nested.nested_run_pending)
+		return 0;
+
+	if (unlikely(vmx->fail)) {
+		printk(KERN_INFO "%s failed vm entry %x\n",
+		       __func__, vmcs_read32(VM_INSTRUCTION_ERROR));
+		return 1;
+	}
+
+	if (kvm_override) {
+		switch (exit_code) {
+		case EXIT_REASON_EXTERNAL_INTERRUPT:
+			return 0;
+		case EXIT_REASON_EXCEPTION_NMI:
+			if (!is_exception(intr_info))
+				return 0;
+
+			if (is_page_fault(intr_info) && (!enable_ept))
+				return 0;
+
+			break;
+		case EXIT_REASON_EPT_VIOLATION:
+			if (enable_ept)
+				return 0;
+
+			break;
+		}
+	}
+
+
+	if (!nested_map_shadow_vmcs(vcpu))
+		return 0;
+	l2svmcs = get_shadow_vmcs(vcpu);
+
+	switch (exit_code) {
+	case EXIT_REASON_INVLPG:
+		if (l2svmcs->cpu_based_vm_exec_control &
+		    CPU_BASED_INVLPG_EXITING)
+			r = 1;
+		break;
+	case EXIT_REASON_MSR_READ:
+	case EXIT_REASON_MSR_WRITE:
+		r = nested_vmx_exit_handled_msr(vcpu);
+		break;
+	case EXIT_REASON_CR_ACCESS: {
+		unsigned long exit_qualification =
+			vmcs_readl(EXIT_QUALIFICATION);
+		int cr = exit_qualification & 15;
+		int reg = (exit_qualification >> 8) & 15;
+		unsigned long val = kvm_register_read(vcpu, reg);
+
+		switch ((exit_qualification >> 4) & 3) {
+		case 0: /* mov to cr */
+			switch (cr) {
+			case 0:
+				if (l2svmcs->cr0_guest_host_mask &
+				    (val ^ l2svmcs->cr0_read_shadow))
+					r = 1;
+				break;
+			case 3:
+				if (l2svmcs->cpu_based_vm_exec_control &
+				    CPU_BASED_CR3_LOAD_EXITING)
+					r = 1;
+				break;
+			case 4:
+				if (l2svmcs->cr4_guest_host_mask &
+				    (l2svmcs->cr4_read_shadow ^ val))
+					r = 1;
+				break;
+			case 8:
+				if (l2svmcs->cpu_based_vm_exec_control &
+				    CPU_BASED_CR8_LOAD_EXITING)
+					r = 1;
+				break;
+			}
+			break;
+		case 2: /* clts */
+			if (l2svmcs->cr0_guest_host_mask &
+			    (val ^ l2svmcs->cr0_read_shadow))
+				r = 1;
+			break;
+		case 1: /*mov from cr*/
+			switch (cr) {
+			case 0:
+				r = 1;
+			case 3:
+				if (l2svmcs->cpu_based_vm_exec_control &
+				    CPU_BASED_CR3_STORE_EXITING)
+					r = 1;
+				break;
+			case 4:
+				r = 1;
+				break;
+			case 8:
+				if (l2svmcs->cpu_based_vm_exec_control &
+				    CPU_BASED_CR8_STORE_EXITING)
+					r = 1;
+				break;
+			}
+			break;
+		case 3: /* lmsw */
+			if (l2svmcs->cr0_guest_host_mask &
+			    (val ^ l2svmcs->cr0_read_shadow))
+				r = 1;
+			break;
+		}
+		break;
+	}
+	case EXIT_REASON_DR_ACCESS: {
+		if (l2svmcs->cpu_based_vm_exec_control &
+		    CPU_BASED_MOV_DR_EXITING)
+			r = 1;
+		break;
+	}
+
+	case EXIT_REASON_EXCEPTION_NMI: {
+
+		if (is_external_interrupt(intr_info) &&
+		    (l2svmcs->pin_based_vm_exec_control &
+		     PIN_BASED_EXT_INTR_MASK))
+			r = 1;
+		else if (is_nmi(intr_info) &&
+		    (l2svmcs->pin_based_vm_exec_control &
+		     PIN_BASED_NMI_EXITING))
+			r = 1;
+		else if (is_exception(intr_info) &&
+		    (l2svmcs->exception_bitmap &
+		     (1u << (intr_info & INTR_INFO_VECTOR_MASK))))
+			r = 1;
+		else if (is_page_fault(intr_info))
+			r = 1;
+		break;
+	}
+
+	case EXIT_REASON_EXTERNAL_INTERRUPT:
+		if (l2svmcs->pin_based_vm_exec_control &
+		    PIN_BASED_EXT_INTR_MASK)
+			r = 1;
+		break;
+	default:
+		r = 1;
+	}
+	nested_unmap_shadow_vmcs(vcpu);
+
+	return r;
+}
+
+static int nested_vmx_check_exception(struct vcpu_vmx *vmx, unsigned nr,
+				      bool has_error_code, u32 error_code)
+{
+	if (vmx->nested.nested_mode) {
+		if (nested_vmx_exit_handled(&vmx->vcpu, false)) {
+			nested_vmx_vmexit(&vmx->vcpu, false);
+			if (!nested_map_shadow_vmcs(&vmx->vcpu))
+				return 1;
+			get_shadow_vmcs(&vmx->vcpu)->vm_exit_reason =
+				EXIT_REASON_EXCEPTION_NMI;
+			get_shadow_vmcs(&vmx->vcpu)->vm_exit_intr_info =
+				(nr | INTR_TYPE_HARD_EXCEPTION
+				 | (has_error_code ?
+				    INTR_INFO_DELIVER_CODE_MASK : 0)
+				 | INTR_INFO_VALID_MASK);
+
+			if (has_error_code)
+				get_shadow_vmcs(&vmx->vcpu)->
+					vm_exit_intr_error_code = error_code;
+			nested_unmap_shadow_vmcs(&vmx->vcpu);
+			return 1;

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6317

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+		}
+	}
+	return 0;
+}
+
+static int nested_vmx_intr(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (vmx->nested.nested_mode) {

This function is called only after checking nested_mode. Why recheck?

Quoted 33 lines
arch/x86/kvm/vmx.c +23 -0
+
+		if (!nested_map_shadow_vmcs(vcpu))
+			return 0;
+
+		if (get_shadow_vmcs(vcpu)->pin_based_vm_exec_control &
+		    PIN_BASED_EXT_INTR_MASK) {
+
+			if (vmx->nested.nested_run_pending) {
+				nested_unmap_shadow_vmcs(vcpu);
+				return 0;
+			}
+
+			nested_unmap_shadow_vmcs(vcpu);
+			nested_vmx_vmexit(vcpu, true);
+			return 1;		
+		}
+
+		nested_unmap_shadow_vmcs(vcpu);
+
+	}
+
+	return 0;
+}
 
 static struct kvm_x86_ops vmx_x86_ops = {
 	.cpu_has_kvm_support = cpu_has_kvm_support,
Signature · 6 lines
1.6.0.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
Gleb.

#7 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Gleb Natapov <[email protected]> wrote on 19/10/2009 19:29:39:

Quoted 41 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

19/10/2009 19:29

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 4 lines · On Thu, Oct 15, 2009 at 04:41:46PM +0200, [email protected] wrote:

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c | 1173 +++++++++++++++++++++++++++++++++++++++

+++++++++++--

Quoted 13 lines
 1 files changed, 1148 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx.c +1 -0
index 6a4c252..e814029 100644
@@ -209,6 +209,7 @@ struct __attribute__ ((__packed__)) level_state {
    struct vmcs *vmcs;
    int cpu;
    int launched;
+   bool first_launch;
 };
 
Quoted 4 lines

arch/x86/kvm/vmx.c struct nested_vmx { · line 218

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -0
 struct nested_vmx {
@@ -216,6 +217,12 @@ struct nested_vmx {
    bool vmxon;
    /* What is the location of the  vmcs l1 keeps for l2? (in level1

gpa) */

Quoted 6 lines

arch/x86/kvm/vmx.c struct nested_vmx {

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
    u64 vmptr;
+   /* Are we running nested guest */
+   bool nested_mode;
+   /* L1 requested VMLAUNCH or VMRESUME but we didn't run L2 yet */
+   bool nested_run_pending;
+   /* flag indicating if there was a valid IDT after exiting from l2

*/

Quoted 9 lines

arch/x86/kvm/vmx.c static inline int vmcs_field_length(unsigned

Quoted 5 lines
arch/x86/kvm/vmx.c +1 -0
+   bool nested_valid_idt;
    /*
     * Level 2 state : includes vmcs,registers and
     * a copy of vmcs12 for vmread/vmwrite
@@ -240,6 +247,10 @@ static inline int vmcs_field_length(unsigned

long field)

Quoted 3 lines
arch/x86/kvm/vmx.c +0 -0
    return (VMCS_FIELD_LENGTH_MASK & field) >> 13;
 }
 
Quoted 13 lines

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct

Quoted 8 lines
arch/x86/kvm/vmx.c +4 -0
+#define NESTED_VM_EXIT_CONTROLS_MASK (~(VM_EXIT_LOAD_IA32_PAT | \
+               VM_EXIT_SAVE_IA32_PAT))
+#define NESTED_VM_ENTRY_CONTROLS_MASK (~(VM_ENTRY_LOAD_IA32_PAT | \
+                VM_ENTRY_IA32E_MODE))
 struct vmcs {
    u32 revision_id;
    u32 abort;
@@ -303,6 +314,12 @@ static inline struct vcpu_vmx *to_vmx(struct

kvm_vcpu *vcpu)

Quoted 4 lines
arch/x86/kvm/vmx.c +1 -0
    return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+static inline struct shadow_vmcs *get_shadow_vmcs(struct kvm_vcpu

*vcpu)

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx.c +5 -0
+{
+   WARN_ON(!to_vmx(vcpu)->nested.l2_state->shadow_vmcs);
+   return to_vmx(vcpu)->nested.l2_state->shadow_vmcs;
+}
+
 #define SHADOW_VMCS_OFFSET(x) offsetof(struct shadow_vmcs, x)
 
Quoted 6 lines

arch/x86/kvm/vmx.c static struct kvm_vmx_segment_field { · line 842

Quoted 6 lines
arch/x86/kvm/vmx.c +1 -0
 static unsigned short vmcs_field_to_offset_table[HOST_RIP+1] = {
@@ -822,8 +839,16 @@ static struct kvm_vmx_segment_field {
 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
 
 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu);
+static int nested_vmx_check_exception(struct vcpu_vmx *vmx, unsigned

nr,

Quoted 13 lines

arch/x86/kvm/vmx.c static struct kvm_vmx_segment_field {

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -0
+                  bool has_error_code, u32 error_code);
+static int nested_vmx_intr(struct kvm_vcpu *vcpu);
 static int create_l1_state(struct kvm_vcpu *vcpu);
 static int create_l2_state(struct kvm_vcpu *vcpu);
+static int launch_guest(struct kvm_vcpu *vcpu);
+static int nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu);
+static int nested_vmx_exit_handled(struct kvm_vcpu *vcpu, bool

kvm_override);

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
+static int nested_vmx_vmexit(struct kvm_vcpu *vcpu,
+              bool is_interrupt);
 
 /*
  * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize

it

Quoted 7 lines

arch/x86/kvm/vmx.c static inline bool cpu_has_vmx_ept_2m_page(void) · line 970

Quoted 7 lines
arch/x86/kvm/vmx.c +3 -0
@@ -940,6 +965,18 @@ static inline bool cpu_has_vmx_ept_2m_page(void)
    return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
 }
 
+static inline int is_exception(u32 intr_info)
+{
+   return (intr_info & (INTR_INFO_INTR_TYPE_MASK |

INTR_INFO_VALID_MASK))

Quoted 6 lines

arch/x86/kvm/vmx.c static inline bool cpu_has_vmx_ept_2m_page(void)

Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+      == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
+}
+
+static inline int is_nmi(u32 intr_info)
+{
+   return (intr_info & (INTR_INFO_INTR_TYPE_MASK |

INTR_INFO_VALID_MASK))

Quoted 11 lines

arch/x86/kvm/vmx.c static inline bool report_flexpriority(void) · line 1030

Quoted 11 lines
arch/x86/kvm/vmx.c +4 -0
+      == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
+}
+
 static inline int cpu_has_vmx_invept_individual_addr(void)
 {
    return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
@@ -990,6 +1027,51 @@ static inline bool report_flexpriority(void)
    return flexpriority_enabled;
 }
 
+static inline int nested_cpu_has_vmx_tpr_shadow(struct  kvm_vcpu

*vcpu)

Quoted 8 lines

arch/x86/kvm/vmx.c static inline bool report_flexpriority(void)

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+{
+   return cpu_has_vmx_tpr_shadow() &&
+      get_shadow_vmcs(vcpu)->cpu_based_vm_exec_control &
+      CPU_BASED_TPR_SHADOW;
+}
+
+static inline int nested_cpu_has_secondary_exec_ctrls(struct

kvm_vcpu *vcpu)

Quoted 7 lines

arch/x86/kvm/vmx.c static inline bool report_flexpriority(void)

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+{
+   return cpu_has_secondary_exec_ctrls() &&
+      get_shadow_vmcs(vcpu)->cpu_based_vm_exec_control &
+      CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
+}
+
+static inline bool nested_vm_need_virtualize_apic_accesses(struct

kvm_vcpu

Quoted 27 lines
Quoted 27 lines
arch/x86/kvm/vmx.c +27 -0
+                        *vcpu)
+{
+   return get_shadow_vmcs(vcpu)->secondary_vm_exec_control &
+      SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
+}
+
+static inline int nested_cpu_has_vmx_ept(struct kvm_vcpu *vcpu)
+{
+   return get_shadow_vmcs(vcpu)->
+      secondary_vm_exec_control & SECONDARY_EXEC_ENABLE_EPT;
+}
+
+static inline int nested_cpu_has_vmx_vpid(struct kvm_vcpu *vcpu)
+{
+   return get_shadow_vmcs(vcpu)->secondary_vm_exec_control &
+      SECONDARY_EXEC_ENABLE_VPID;
+}
+
+static inline int nested_cpu_has_vmx_pat(struct kvm_vcpu *vcpu)
+{
+   return get_shadow_vmcs(vcpu)->vm_entry_controls &
+      VM_ENTRY_LOAD_IA32_PAT;
+}
+
+static inline int nested_cpu_has_vmx_msr_bitmap(struct kvm_vcpu *vcpu)
+{
+   return get_shadow_vmcs(vcpu)->cpu_based_vm_exec_control &
Quoted 12 lines

arch/x86/kvm/vmx.c static void vmx_queue_exception(struct

Quoted 7 lines
arch/x86/kvm/vmx.c +3 -0
+      CPU_BASED_USE_MSR_BITMAPS;
+}
+
 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
 {
    int i;
@@ -1501,6 +1583,9 @@ static void vmx_queue_exception(struct

kvm_vcpu *vcpu, unsigned nr,

Quoted 4 lines
arch/x86/kvm/vmx.c +1 -0
    struct vcpu_vmx *vmx = to_vmx(vcpu);
    u32 intr_info = nr | INTR_INFO_VALID_MASK;
 
+   if (nested_vmx_check_exception(vmx, nr, has_error_code,

error_code))

Quoted 9 lines
Quoted 9 lines
arch/x86/kvm/vmx.c +2 -0
+      return;
+
    if (has_error_code) {
       vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
       intr_info |= INTR_INFO_DELIVER_CODE_MASK;
@@ -1943,6 +2028,200 @@ static void vmclear_local_vcpus(void)
       __vcpu_clear(vmx);
 }
 
Quoted 12 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void)

Quoted 12 lines
arch/x86/kvm/vmx.c +12 -0
+void prepare_vmcs_12(struct kvm_vcpu *vcpu)
+{
+   struct shadow_vmcs *l2_shadow_vmcs =
+      get_shadow_vmcs(vcpu);
+
+   l2_shadow_vmcs->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
+   l2_shadow_vmcs->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
+   l2_shadow_vmcs->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
+   l2_shadow_vmcs->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
+   l2_shadow_vmcs->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
+   l2_shadow_vmcs->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
+   l2_shadow_vmcs->guest_ldtr_selector = vmcs_read16

(GUEST_LDTR_SELECTOR);

Quoted 7 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void)

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+   l2_shadow_vmcs->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
+
+   l2_shadow_vmcs->tsc_offset = vmcs_read64(TSC_OFFSET);
+   l2_shadow_vmcs->guest_physical_address =
+      vmcs_read64(GUEST_PHYSICAL_ADDRESS);
+   l2_shadow_vmcs->vmcs_link_pointer = vmcs_read64(VMCS_LINK_POINTER);
+   l2_shadow_vmcs->guest_ia32_debugctl = vmcs_read64

(GUEST_IA32_DEBUGCTL);

Quoted 32 lines
Quoted 32 lines
arch/x86/kvm/vmx.c +32 -0
+   if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
+      l2_shadow_vmcs->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
+   l2_shadow_vmcs->cr3_target_count = vmcs_read32(CR3_TARGET_COUNT);
+   l2_shadow_vmcs->vm_entry_intr_info_field =
+      vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
+   l2_shadow_vmcs->vm_entry_exception_error_code =
+      vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE);
+   l2_shadow_vmcs->vm_entry_instruction_len =
+      vmcs_read32(VM_ENTRY_INSTRUCTION_LEN);
+   l2_shadow_vmcs->vm_instruction_error =
+      vmcs_read32(VM_INSTRUCTION_ERROR);
+   l2_shadow_vmcs->vm_exit_reason  = vmcs_read32(VM_EXIT_REASON);
+   l2_shadow_vmcs->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+   l2_shadow_vmcs->vm_exit_intr_error_code =
+      vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
+   l2_shadow_vmcs->idt_vectoring_info_field =
+      vmcs_read32(IDT_VECTORING_INFO_FIELD);
+   l2_shadow_vmcs->idt_vectoring_error_code =
+      vmcs_read32(IDT_VECTORING_ERROR_CODE);
+   l2_shadow_vmcs->vm_exit_instruction_len =
+      vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
+   l2_shadow_vmcs->vmx_instruction_info =
+      vmcs_read32(VMX_INSTRUCTION_INFO);
+   l2_shadow_vmcs->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
+   l2_shadow_vmcs->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
+   l2_shadow_vmcs->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
+   l2_shadow_vmcs->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
+   l2_shadow_vmcs->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
+   l2_shadow_vmcs->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
+   l2_shadow_vmcs->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
+   l2_shadow_vmcs->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
+   l2_shadow_vmcs->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+   l2_shadow_vmcs->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
+   l2_shadow_vmcs->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
+   l2_shadow_vmcs->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
+   l2_shadow_vmcs->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
+   l2_shadow_vmcs->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
+   l2_shadow_vmcs->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
+   l2_shadow_vmcs->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
+   l2_shadow_vmcs->guest_ldtr_ar_bytes = vmcs_read32

(GUEST_LDTR_AR_BYTES);

Quoted 10 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void)

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+   l2_shadow_vmcs->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
+   l2_shadow_vmcs->guest_interruptibility_info =
+      vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+   l2_shadow_vmcs->guest_activity_state =
+      vmcs_read32(GUEST_ACTIVITY_STATE);
+   l2_shadow_vmcs->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
+
+   l2_shadow_vmcs->cr0_read_shadow = vmcs_readl(CR0_READ_SHADOW);
+   l2_shadow_vmcs->cr4_read_shadow = vmcs_readl(CR4_READ_SHADOW);
+   l2_shadow_vmcs->exit_qualification = vmcs_readl

(EXIT_QUALIFICATION);

Quoted 1 line

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2100

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+   l2_shadow_vmcs->guest_linear_address = vmcs_readl

(GUEST_LINEAR_ADDRESS);

Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/vmx.c +12 -0
+   l2_shadow_vmcs->guest_cr0 = vmcs_readl(GUEST_CR0);
+
+   l2_shadow_vmcs->guest_cr4 = vmcs_readl(GUEST_CR4);
+   l2_shadow_vmcs->guest_es_base = vmcs_readl(GUEST_ES_BASE);
+   l2_shadow_vmcs->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
+   l2_shadow_vmcs->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
+   l2_shadow_vmcs->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
+   l2_shadow_vmcs->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
+   l2_shadow_vmcs->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
+   l2_shadow_vmcs->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
+   l2_shadow_vmcs->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
+   l2_shadow_vmcs->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+   l2_shadow_vmcs->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
+   l2_shadow_vmcs->guest_dr7 = vmcs_readl(GUEST_DR7);
+   l2_shadow_vmcs->guest_rsp = vmcs_readl(GUEST_RSP);
+   l2_shadow_vmcs->guest_rip = vmcs_readl(GUEST_RIP);
+   l2_shadow_vmcs->guest_rflags = vmcs_readl(GUEST_RFLAGS);
+   l2_shadow_vmcs->guest_pending_dbg_exceptions =
+      vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
+   l2_shadow_vmcs->guest_sysenter_esp = vmcs_readl

(GUEST_SYSENTER_ESP);

Quoted 1 line

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2121

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+   l2_shadow_vmcs->guest_sysenter_eip = vmcs_readl

(GUEST_SYSENTER_EIP);

Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/vmx.c +12 -0
+}
+
+int load_vmcs_common(struct shadow_vmcs *src)
+{
+   vmcs_write16(GUEST_ES_SELECTOR, src->guest_es_selector);
+   vmcs_write16(GUEST_CS_SELECTOR, src->guest_cs_selector);
+   vmcs_write16(GUEST_SS_SELECTOR, src->guest_ss_selector);
+   vmcs_write16(GUEST_DS_SELECTOR, src->guest_ds_selector);
+   vmcs_write16(GUEST_FS_SELECTOR, src->guest_fs_selector);
+   vmcs_write16(GUEST_GS_SELECTOR, src->guest_gs_selector);
+   vmcs_write16(GUEST_LDTR_SELECTOR, src->guest_ldtr_selector);
+   vmcs_write16(GUEST_TR_SELECTOR, src->guest_tr_selector);
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+
+   vmcs_write64(VMCS_LINK_POINTER, src->vmcs_link_pointer);
+   vmcs_write64(GUEST_IA32_DEBUGCTL, src->guest_ia32_debugctl);
+
+   if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
+      vmcs_write64(GUEST_IA32_PAT, src->guest_ia32_pat);
+
+   vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->

vm_entry_msr_load_count);

Quoted 1 line

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2142

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+   vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, src->

vm_entry_intr_info_field);

Quoted 3 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void)

Quoted 3 lines
arch/x86/kvm/vmx.c +3 -0
+   vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
+           src->vm_entry_exception_error_code);
+   vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, src->

vm_entry_instruction_len);

Quoted 86 lines
Quoted 86 lines
arch/x86/kvm/vmx.c +79 -0
+
+   vmcs_write32(GUEST_ES_LIMIT, src->guest_es_limit);
+   vmcs_write32(GUEST_CS_LIMIT, src->guest_cs_limit);
+   vmcs_write32(GUEST_SS_LIMIT, src->guest_ss_limit);
+   vmcs_write32(GUEST_DS_LIMIT, src->guest_ds_limit);
+   vmcs_write32(GUEST_FS_LIMIT, src->guest_fs_limit);
+   vmcs_write32(GUEST_GS_LIMIT, src->guest_gs_limit);
+   vmcs_write32(GUEST_LDTR_LIMIT, src->guest_ldtr_limit);
+   vmcs_write32(GUEST_TR_LIMIT, src->guest_tr_limit);
+   vmcs_write32(GUEST_GDTR_LIMIT, src->guest_gdtr_limit);
+   vmcs_write32(GUEST_IDTR_LIMIT, src->guest_idtr_limit);
+   vmcs_write32(GUEST_ES_AR_BYTES, src->guest_es_ar_bytes);
+   vmcs_write32(GUEST_CS_AR_BYTES, src->guest_cs_ar_bytes);
+   vmcs_write32(GUEST_SS_AR_BYTES, src->guest_ss_ar_bytes);
+   vmcs_write32(GUEST_DS_AR_BYTES, src->guest_ds_ar_bytes);
+   vmcs_write32(GUEST_FS_AR_BYTES, src->guest_fs_ar_bytes);
+   vmcs_write32(GUEST_GS_AR_BYTES, src->guest_gs_ar_bytes);
+   vmcs_write32(GUEST_LDTR_AR_BYTES, src->guest_ldtr_ar_bytes);
+   vmcs_write32(GUEST_TR_AR_BYTES, src->guest_tr_ar_bytes);
+   vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
+           src->guest_interruptibility_info);
+   vmcs_write32(GUEST_ACTIVITY_STATE, src->guest_activity_state);
+   vmcs_write32(GUEST_SYSENTER_CS, src->guest_sysenter_cs);
+
+   vmcs_writel(GUEST_ES_BASE, src->guest_es_base);
+   vmcs_writel(GUEST_CS_BASE, src->guest_cs_base);
+   vmcs_writel(GUEST_SS_BASE, src->guest_ss_base);
+   vmcs_writel(GUEST_DS_BASE, src->guest_ds_base);
+   vmcs_writel(GUEST_FS_BASE, src->guest_fs_base);
+   vmcs_writel(GUEST_GS_BASE, src->guest_gs_base);
+   vmcs_writel(GUEST_LDTR_BASE, src->guest_ldtr_base);
+   vmcs_writel(GUEST_TR_BASE, src->guest_tr_base);
+   vmcs_writel(GUEST_GDTR_BASE, src->guest_gdtr_base);
+   vmcs_writel(GUEST_IDTR_BASE, src->guest_idtr_base);
+   vmcs_writel(GUEST_DR7, src->guest_dr7);
+   vmcs_writel(GUEST_RSP, src->guest_rsp);
+   vmcs_writel(GUEST_RIP, src->guest_rip);
+   vmcs_writel(GUEST_RFLAGS, src->guest_rflags);
+   vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
+          src->guest_pending_dbg_exceptions);
+   vmcs_writel(GUEST_SYSENTER_ESP, src->guest_sysenter_esp);
+   vmcs_writel(GUEST_SYSENTER_EIP, src->guest_sysenter_eip);
+
+   return 0;
+}
+
+int load_vmcs_host_state(struct shadow_vmcs *src)
+{
+   vmcs_write16(HOST_ES_SELECTOR, src->host_es_selector);
+   vmcs_write16(HOST_CS_SELECTOR, src->host_cs_selector);
+   vmcs_write16(HOST_SS_SELECTOR, src->host_ss_selector);
+   vmcs_write16(HOST_DS_SELECTOR, src->host_ds_selector);
+   vmcs_write16(HOST_FS_SELECTOR, src->host_fs_selector);
+   vmcs_write16(HOST_GS_SELECTOR, src->host_gs_selector);
+   vmcs_write16(HOST_TR_SELECTOR, src->host_tr_selector);
+
+   vmcs_write64(TSC_OFFSET, src->tsc_offset);
+
+   if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT)
+      vmcs_write64(HOST_IA32_PAT, src->host_ia32_pat);
+
+   vmcs_write32(HOST_IA32_SYSENTER_CS, src->host_ia32_sysenter_cs);
+
+   vmcs_writel(HOST_CR0, src->host_cr0);
+   vmcs_writel(HOST_CR3, src->host_cr3);
+   vmcs_writel(HOST_CR4, src->host_cr4);
+   vmcs_writel(HOST_FS_BASE, src->host_fs_base);
+   vmcs_writel(HOST_GS_BASE, src->host_gs_base);
+   vmcs_writel(HOST_TR_BASE, src->host_tr_base);
+   vmcs_writel(HOST_GDTR_BASE, src->host_gdtr_base);
+   vmcs_writel(HOST_IDTR_BASE, src->host_idtr_base);
+   vmcs_writel(HOST_RSP, src->host_rsp);
+   vmcs_writel(HOST_RIP, src->host_rip);
+   vmcs_writel(HOST_IA32_SYSENTER_ESP, src->host_ia32_sysenter_esp);
+   vmcs_writel(HOST_IA32_SYSENTER_EIP, src->host_ia32_sysenter_eip);
+
+   return 0;
+}
+
 struct level_state *create_state(void)
 {
    struct level_state *state = NULL;
@@ -2003,6 +2282,8 @@ int create_l2_state(struct kvm_vcpu *vcpu)
    vmx->nested.l2_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
    vmx->nested.l2_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
 
Quoted 6 lines

arch/x86/kvm/vmx.c static void enable_irq_window(struct kvm_vcpu

Quoted 6 lines
arch/x86/kvm/vmx.c +2 -0
+   vmx->nested.l2_state->first_launch = true;
+
    return 0;
 }
 
@@ -3393,6 +3674,14 @@ static void enable_irq_window(struct kvm_vcpu

*vcpu)

Quoted 7 lines

arch/x86/kvm/vmx.c static void enable_irq_window(struct kvm_vcpu *vcpu) · line 3678

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
 {
    u32 cpu_based_vm_exec_control;
 
+   if (to_vmx(vcpu)->nested.nested_mode) {
+      if (kvm_cpu_has_interrupt(vcpu)) {

Why interrupt will not be present during the call to enable_irq_window()?

I will look into it.

Quoted 11 lines

arch/x86/kvm/vmx.c static void vmx_inject_nmi(struct kvm_vcpu

Quoted 11 lines
arch/x86/kvm/vmx.c +6 -0
+         if (nested_vmx_intr(vcpu))
+            return;
+      }
+      return;
+   }
+
    cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
    cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
    vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
@@ -3448,6 +3737,10 @@ static void vmx_inject_nmi(struct kvm_vcpu

*vcpu)

Quoted 11 lines

arch/x86/kvm/vmx.c static int vmx_nmi_allowed(struct kvm_vcpu

Quoted 11 lines
arch/x86/kvm/vmx.c +4 -0
 {
    struct vcpu_vmx *vmx = to_vmx(vcpu);
 
+   if (vmx->nested.nested_mode) {
+      return;
+   }
+
    if (!cpu_has_virtual_nmis()) {
       /*
        * Tracking the NMI-blocked state in software is built upon
@@ -3489,6 +3782,13 @@ static int vmx_nmi_allowed(struct kvm_vcpu

*vcpu)

Quoted 10 lines

arch/x86/kvm/vmx.c static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) · line 3791

Quoted 10 lines
arch/x86/kvm/vmx.c +7 -0
 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
 {
+   if (to_vmx(vcpu)->nested.nested_mode) {
+      if (kvm_cpu_has_interrupt(vcpu)) {
+         if (!nested_vmx_intr(vcpu))
+            return 0;
+      }
+   }
+
Quoted 4 lines

Same as above. kvm_cpu_has_interrupt() should alway return true here. More interesting question: why not return 0 here if in nested mode and PIN_BASED_EXT_INTR_MASK is not set and let enable_irq_window() to do nested exit. This is what svm does as far as I see.

I will look into it.

Quoted 38 lines

arch/x86/kvm/vmx.c static int nested_vmx_check_permission

Quoted 5 lines
arch/x86/kvm/vmx.c +0 -0
    return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
       !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
          (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
@@ -3993,12 +4293,6 @@ static int nested_vmx_check_permission

(struct kvm_vcpu *vcpu)

Quoted 32 lines
arch/x86/kvm/vmx.c +16 -6
    return 1;
 }
 
-static int handle_vmx_insn(struct kvm_vcpu *vcpu)
-{
-   kvm_queue_exception(vcpu, UD_VECTOR);
-   return 1;
-}
-
 static void clear_rflags_cf_zf(struct kvm_vcpu *vcpu)
 {
    unsigned long rflags;
@@ -4040,6 +4334,27 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)
    return 1;
 }
 
+static int handle_vmlaunch(struct kvm_vcpu *vcpu)
+{
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+   if (!to_vmx(vcpu)->nested.l2_state->vmclear)
+      return 1;
+
+   return launch_guest(vcpu);
+}
+
+static int handle_vmresume(struct kvm_vcpu *vcpu)
+{
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+
+   if (to_vmx(vcpu)->nested.l2_state->vmclear)
Quoted 8 lines

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4346

Quoted 5 lines
arch/x86/kvm/vmx.c +5 -0
+      return 1;
+
+   return launch_guest(vcpu);
+}
+

handle_vmlaunch() and handle_vmresume() are looking suspiciously similar may be move vmclear checking logic into launch_guest()? It will get additional parameter: expected value of vmclear.

Ok.

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx.c +0 -0
 static int handle_vmread(struct kvm_vcpu *vcpu)
 {
 #ifndef CONFIG_X86_64
@@ -4050,7 +4365,6 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
       return 1;
 
Quoted 8 lines

arch/x86/kvm/vmx.c static int handle_vmread(struct kvm_vcpu *vcpu) · line 4370

Quoted 5 lines
arch/x86/kvm/vmx.c +0 -1
    if (!nested_map_shadow_vmcs(vcpu)) {
-      printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
       set_rflags_to_vmx_fail_invalid(vcpu);
       return 1;
    }

Remove from a patch that add it if you don't need this. Also all prinks that can be triggered by a guest should be removed/changed to debug output.

Ok.

Quoted 10 lines

arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu

Quoted 10 lines
arch/x86/kvm/vmx.c +0 -1
@@ -4107,7 +4421,6 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)
       return 1;
 
    if (!nested_map_shadow_vmcs(vcpu)) {
-      printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
       set_rflags_to_vmx_fail_invalid(vcpu);
       return 1;
    }
@@ -4137,16 +4450,16 @@ static int handle_vmwrite(struct kvm_vcpu

*vcpu)

Quoted 11 lines

arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu *vcpu) · line 4458

Quoted 10 lines
arch/x86/kvm/vmx.c +1 -1
                vcpu->arch.regs[VCPU_REGS_RAX]);
       break;
    default:
+      nested_unmap_shadow_vmcs(vcpu);
       printk(KERN_INFO "%s invalid field\n", __func__);
       set_rflags_to_vmx_fail_valid(vcpu);
       vmcs_write32(VM_INSTRUCTION_ERROR, 12);
-      nested_unmap_shadow_vmcs(vcpu);
       return 1;
    }

Why this is here and not in the patch that introduce the function?

I will move it there.

Quoted 9 lines

arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu *vcpu) · line 4464

Quoted 8 lines
arch/x86/kvm/vmx.c +1 -1
+   nested_unmap_shadow_vmcs(vcpu);
    clear_rflags_cf_zf(vcpu);
    skip_emulated_instruction(vcpu);
-   nested_unmap_shadow_vmcs(vcpu);
    return 1;
 }

Same.

I will move it there.

Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/vmx.c +0 -1
@@ -4208,7 +4521,6 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 static int handle_vmptrld(struct kvm_vcpu *vcpu)
 {
    struct vcpu_vmx *vmx = to_vmx(vcpu);
-   struct page *vmcs_page;
    u64 guest_vmcs_addr;
 
    if (!nested_vmx_check_permission(vcpu))
@@ -4228,14 +4540,7 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)
    }
 
Quoted 11 lines

arch/x86/kvm/vmx.c static int handle_vmptrld(struct kvm_vcpu *vcpu) · line 4544

Quoted 10 lines
arch/x86/kvm/vmx.c +0 -7
    if (vmx->nested.vmptr != guest_vmcs_addr) {
-      /* checking vmptr address */
-      vmcs_page = nested_get_page(vcpu, guest_vmcs_addr);
-      if (vmcs_page == NULL)
-         return 1;
-
       vmx->nested.vmptr = guest_vmcs_addr;
-
-      kvm_release_page_clean(vmcs_page);
    }

Same.

I will move it there.

Quoted 5 lines

arch/x86/kvm/vmx.c static int (*kvm_vmx_exit_handlers[])

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -0
    clear_rflags_cf_zf(vcpu);
@@ -4534,11 +4839,11 @@ static int (*kvm_vmx_exit_handlers[])

(struct kvm_vcpu *vcpu) = {

Quoted 14 lines

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu

Quoted 14 lines
arch/x86/kvm/vmx.c +2 -2
    [EXIT_REASON_INVLPG]            = handle_invlpg,
    [EXIT_REASON_VMCALL]                  = handle_vmcall,
    [EXIT_REASON_VMCLEAR]                 = handle_vmclear,
-   [EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
+   [EXIT_REASON_VMLAUNCH]                = handle_vmlaunch,
    [EXIT_REASON_VMPTRLD]                 = handle_vmptrld,
    [EXIT_REASON_VMPTRST]                 = handle_vmptrst,
    [EXIT_REASON_VMREAD]                  = handle_vmread,
-   [EXIT_REASON_VMRESUME]                = handle_vmx_insn,
+   [EXIT_REASON_VMRESUME]                = handle_vmresume,
    [EXIT_REASON_VMWRITE]                 = handle_vmwrite,
    [EXIT_REASON_VMOFF]                   = handle_vmoff,
    [EXIT_REASON_VMON]                    = handle_vmon,
@@ -4566,6 +4871,17 @@ static int vmx_handle_exit(struct kvm_vcpu

*vcpu)

Quoted 10 lines

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu)

Quoted 10 lines
arch/x86/kvm/vmx.c +7 -0
    trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
 
+   if (exit_reason == EXIT_REASON_VMLAUNCH ||
+       exit_reason == EXIT_REASON_VMRESUME)
+      vmx->nested.nested_run_pending = 1;
+   else
+      vmx->nested.nested_run_pending = 0;
+
+   if (vmx->nested.nested_mode && nested_vmx_exit_handled(vcpu, true))

{

Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/vmx.c +4 -0
+      nested_vmx_vmexit(vcpu, false);
+      return 1;
+   }
+
    /* If we need to emulate an MMIO from handle_invalid_guest_state
Quoted 8 lines

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 4588

Quoted 7 lines
arch/x86/kvm/vmx.c +0 -1
     * we just return 0 */
    if (vmx->emulation_required && emulate_invalid_guest_state) {
@@ -4585,7 +4901,6 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
          = vmcs_read32(VM_INSTRUCTION_ERROR);
       return 0;
    }
-

No spurious line deletions please.

I will fix it.

Quoted 24 lines

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 4911

Quoted 11 lines
arch/x86/kvm/vmx.c +1 -2
    if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
          (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
          exit_reason != EXIT_REASON_EPT_VIOLATION &&
@@ -4593,8 +4908,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
       printk(KERN_WARNING "%s: unexpected, valid vectoring info "
              "(0x%x) and exit reason is 0x%x\n",
              __func__, vectoring_info, exit_reason);
-
-   if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
+   if (!vmx->nested.nested_mode && unlikely(!cpu_has_virtual_nmis

() && vmx->soft_vnmi_blocked)) {

arch/x86/kvm/vmx.c static void vmx_complete_interrupts(struct

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -0
       if (vmx_interrupt_allowed(vcpu)) {
          vmx->soft_vnmi_blocked = 0;
       } else if (vmx->vnmi_blocked_time > 1000000000LL &&
@@ -4641,10 +4955,13 @@ static void vmx_complete_interrupts(struct

vcpu_vmx *vmx)

Quoted 7 lines
arch/x86/kvm/vmx.c +0 -2
    int type;
    bool idtv_info_valid;
 
-   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
-
    vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
Quoted 5 lines

arch/x86/kvm/vmx.c static void vmx_complete_interrupts(struct vcpu_vmx *vmx) · line 4962

Quoted 3 lines
arch/x86/kvm/vmx.c +3 -0
+   if (vmx->nested.nested_mode)
+      return;
+

Why return here? What the function does that should not be done in nested mode?

In nested mode L0 injects an interrupt to L2 only in one scenario, if there is an IDT_VALID event and L0 decides to run L2 again and not to switch back to L1.
In all other cases the injection is handled by L1.

Quoted 7 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
+   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
    /* Handle machine checks before interrupts are enabled */
    if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
        || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
@@ -4747,6 +5064,60 @@ static void fixup_rmode_irq(struct vcpu_vmx

*vmx)

Quoted 7 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx *vmx) · line 5068

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
       | vmx->rmode.irq.vector;
 }
 
+static int nested_handle_valid_idt(struct kvm_vcpu *vcpu)
+{

It seems by this function you are trying to bypass general event reinjection logic. Why?

See above.

Quoted 11 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx *vmx) · line 5078

Quoted 11 lines
arch/x86/kvm/vmx.c +10 -0
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   int irq;
+   int type;
+   int errCodeValid;
+   u32 idt_vectoring_info;
+   u32 guest_intr;
+   bool nmi_window_open;
+   bool interrupt_window_open;
+
+   if (vmx->nested.nested_mode && vmx->nested.nested_valid_idt) {
Quoted 1 line

Caller already checked nested_mode why recheck?

I will remove it.

Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/vmx.c +11 -0
+      idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
+      irq  = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
+      type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
+      errCodeValid = idt_vectoring_info &
+         VECTORING_INFO_DELIVER_CODE_MASK;
+
+      guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+      nmi_window_open =
+         !(guest_intr & (GUEST_INTR_STATE_STI |
+               GUEST_INTR_STATE_MOV_SS |
+               GUEST_INTR_STATE_NMI));
Quoted 8 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx *vmx)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+
+      interrupt_window_open =
+         ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
+          !(guest_intr & (GUEST_INTR_STATE_STI |
+                GUEST_INTR_STATE_MOV_SS)));
+
+      if (type == INTR_TYPE_EXT_INTR && !interrupt_window_open) {
+         printk(KERN_INFO "IDT ignored, l2 interrupt window

closed!\n");

Quoted 19 lines
Quoted 19 lines
arch/x86/kvm/vmx.c +19 -0
+         return 0;
+      }
+
+      if (type == INTR_TYPE_NMI_INTR && !nmi_window_open) {
+         printk(KERN_INFO "IDT ignored, l2 nmi window closed!\n");
+         return 0;
+      }
+
+      vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
+         irq | type | INTR_INFO_VALID_MASK | errCodeValid);
+
+
+      vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
+              vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
+
+      if (errCodeValid)
+         vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
+                 vmcs_read32(IDT_VECTORING_ERROR_CODE));
+   }
Quoted 8 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx

Quoted 8 lines
arch/x86/kvm/vmx.c +4 -0
+
+   return 1;
+}
+
 #ifdef CONFIG_X86_64
 #define R "r"
 #define Q "q"
@@ -4758,6 +5129,26 @@ static void fixup_rmode_irq(struct vcpu_vmx

*vmx)

Quoted 62 lines

arch/x86/kvm/vmx.c static struct kvm_vcpu *vmx_create_vcpu

Quoted 38 lines
arch/x86/kvm/vmx.c +24 -0
 static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
 {
    struct vcpu_vmx *vmx = to_vmx(vcpu);
+   int r;
+
+   if (vmx->nested.nested_mode) {
+      r = nested_handle_valid_idt(vcpu);
+      if (!r) {
+         vmx->fail = 1;
+         return;
+      }
+
+      if (!nested_map_shadow_vmcs(vcpu)) {
+         vmx->fail = 1;
+         return;
+      }
+
+      vmcs_write32(EXCEPTION_BITMAP, get_shadow_vmcs(vcpu)->
+              exception_bitmap |
+              vmx->nested.l1_state->shadow_vmcs->exception_bitmap);
+
+      nested_unmap_shadow_vmcs(vcpu);
+   }
 
    if (enable_ept && is_paging(vcpu)) {
       vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
@@ -4896,6 +5287,10 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
    get_debugreg(vcpu->arch.dr6, 6);
 
    vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
+
+   vmx->nested.nested_valid_idt = vmx->nested.nested_mode &&
+      (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
+
    if (vmx->rmode.irq.pending)
       fixup_rmode_irq(vmx);
 
@@ -4984,6 +5379,11 @@ static struct kvm_vcpu *vmx_create_vcpu

(struct kvm *kvm, unsigned int id)

Quoted 23 lines
arch/x86/kvm/vmx.c +13 -0
          goto free_vmcs;
    }
 
+   vmx->nested.vmptr = 0;
+
+   vmx->nested.l1_state = NULL;
+   vmx->nested.l2_state = NULL;
+
    return &vmx->vcpu;
 
 free_vmcs:
@@ -5215,6 +5615,729 @@ void save_vmcs(struct shadow_vmcs *dst)
    if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT)
       dst->host_ia32_pat = vmcs_read64(HOST_IA32_PAT);
 }
+int prepare_vmcs_02(struct kvm_vcpu *vcpu)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   struct shadow_vmcs *src = get_shadow_vmcs(vcpu);
+   u32 exec_control;
+
+   if (!src) {
+      printk(KERN_INFO "%s: Error no shadow vmcs\n", __func__);
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+      return 1;
+   }
+
+   load_vmcs_common(src);
+
+   if (vmx->nested.l2_state->first_launch) {
+      if (cpu_has_vmx_vpid() && vmx->nested.l2_state->vpid != 0)
+         vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.l2_state->

vpid);

Quoted 59 lines
Quoted 38 lines
arch/x86/kvm/vmx.c +38 -0
+
+      if (vmx->nested.l2_state->io_bitmap_a)
+         vmcs_write64(IO_BITMAP_A, vmx->nested.l2_state->io_bitmap_a);
+
+      if (vmx->nested.l2_state->io_bitmap_b)
+         vmcs_write64(IO_BITMAP_B, vmx->nested.l2_state->io_bitmap_b);
+
+      if (vmx->nested.l2_state->msr_bitmap)
+         vmcs_write64(MSR_BITMAP, vmx->nested.l2_state->msr_bitmap);
+
+      if (src->vm_entry_msr_load_count > 0) {
+         struct page *page;
+
+         page = nested_get_page(vcpu,
+                      src->vm_entry_msr_load_addr);
+         if (!page)
+            return 1;
+
+         vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, page_to_phys(page));
+
+         kvm_release_page_clean(page);
+      }
+
+      if (nested_cpu_has_vmx_tpr_shadow(vcpu)) {
+         struct page *page;
+
+         page = nested_get_page(vcpu,
+                      src->virtual_apic_page_addr);
+         if (!page)
+            return 1;
+
+         vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, page_to_phys(page));
+
+         kvm_release_page_clean(page);
+      }
+
+      if (nested_vm_need_virtualize_apic_accesses(vcpu)) {
+         struct page *page =

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5683

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+            nested_get_page(vcpu, src->apic_access_addr);
+         if (!page)
+            return 1;
+
+         vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
+         kvm_release_page_clean(page);
+      }
+
+      vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
+

(vmx->nested.l1_state->shadow_vmcs->pin_based_vm_exec_control |

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5687

Quoted 4 lines
arch/x86/kvm/vmx.c +4 -0
+               src->pin_based_vm_exec_control));
+
+      vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
+

(vmx->nested.l1_state->shadow_vmcs->page_fault_error_code_mask &

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5691

Quoted 4 lines
arch/x86/kvm/vmx.c +4 -0
+               src->page_fault_error_code_mask));
+
+      vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
+

(vmx->nested.l1_state->shadow_vmcs->page_fault_error_code_match &

Quoted 6 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+               src->page_fault_error_code_match));
+
+      if (cpu_has_secondary_exec_ctrls()) {
+
+         exec_control =
+            vmx->nested.l1_state->shadow_vmcs->

secondary_vm_exec_control;

Quoted 71 lines
Quoted 29 lines
arch/x86/kvm/vmx.c +29 -0
+
+         if (nested_cpu_has_secondary_exec_ctrls(vcpu)) {
+
+            exec_control |= src->secondary_vm_exec_control;
+
+            if (!vm_need_virtualize_apic_accesses(vcpu->kvm) ||
+                !nested_vm_need_virtualize_apic_accesses(vcpu))
+               exec_control &=
+                  ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
+         }
+
+         vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
+      }
+
+      load_vmcs_host_state(vmx->nested.l1_state->shadow_vmcs);
+
+      vmx->nested.l2_state->first_launch = false;
+   }
+
+   if (vm_need_tpr_shadow(vcpu->kvm) &&
+       nested_cpu_has_vmx_tpr_shadow(vcpu))
+      vmcs_write32(TPR_THRESHOLD, src->tpr_threshold);
+
+   if (enable_ept) {
+      if (!nested_cpu_has_vmx_ept(vcpu)) {
+         vmcs_write64(EPT_POINTER,
+                 vmx->nested.l1_state->shadow_vmcs->ept_pointer);
+         vmcs_write64(GUEST_PDPTR0,
+                 vmx->nested.l1_state->shadow_vmcs->guest_pdptr0);

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+         vmcs_write64(GUEST_PDPTR1,
+                 vmx->nested.l1_state->shadow_vmcs->guest_pdptr1);
+         vmcs_write64(GUEST_PDPTR2,
+                 vmx->nested.l1_state->shadow_vmcs->guest_pdptr2);
+         vmcs_write64(GUEST_PDPTR3,
+                 vmx->nested.l1_state->shadow_vmcs->guest_pdptr3);
+      }
+   }
+
+   exec_control =

vmx->nested.l1_state->shadow_vmcs->cpu_based_vm_exec_control;

Quoted 31 lines
arch/x86/kvm/vmx.c +31 -0
+
+   exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
+
+   exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
+
+   exec_control &= ~CPU_BASED_TPR_SHADOW;
+
+   exec_control |= src->cpu_based_vm_exec_control;
+
+   if (!vm_need_tpr_shadow(vcpu->kvm) ||
+       src->virtual_apic_page_addr == 0) {
+      exec_control &= ~CPU_BASED_TPR_SHADOW;
+#ifdef CONFIG_X86_64
+      exec_control |= CPU_BASED_CR8_STORE_EXITING |
+         CPU_BASED_CR8_LOAD_EXITING;
+#endif
+   } else if (exec_control & CPU_BASED_TPR_SHADOW) {
+
+#ifdef CONFIG_X86_64
+      exec_control &= ~CPU_BASED_CR8_STORE_EXITING;
+      exec_control &= ~CPU_BASED_CR8_LOAD_EXITING;
+#endif
+   }
+
+   vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
+
+   vmcs_write32(EXCEPTION_BITMAP,
+           (vmx->nested.l1_state->shadow_vmcs->exception_bitmap |
+            src->exception_bitmap));
+
+   vmcs_write32(VM_EXIT_CONTROLS,
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+           ((vmx->nested.l1_state->shadow_vmcs->vm_exit_controls &
+             NESTED_VM_EXIT_CONTROLS_MASK) | src->vm_exit_controls));
+
+   vmcs_write32(VM_ENTRY_CONTROLS,
+           (vmx->nested.l1_state->shadow_vmcs->vm_entry_controls &
+            NESTED_VM_ENTRY_CONTROLS_MASK) | src->vm_entry_controls);
+
+   vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->

vm_entry_msr_load_count);

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+
+   vmcs_writel(CR0_GUEST_HOST_MASK,
+          (vmx->nested.l1_state->shadow_vmcs->cr0_guest_host_mask  &
+           src->cr0_guest_host_mask));
+   vmcs_writel(CR4_GUEST_HOST_MASK,
+          (vmx->nested.l1_state->shadow_vmcs->cr4_guest_host_mask  &
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+           src->cr4_guest_host_mask));
+
+   return 0;
+}
+
+int switch_back_vmcs(struct kvm_vcpu *vcpu)
+{
+   struct shadow_vmcs *src = to_vmx(vcpu)->nested.l1_state->

shadow_vmcs;

Quoted 17 lines
Quoted 17 lines
arch/x86/kvm/vmx.c +17 -0
+
+   if (enable_vpid && src->virtual_processor_id != 0)
+      vmcs_write16(VIRTUAL_PROCESSOR_ID, src->virtual_processor_id);
+
+   vmcs_write64(IO_BITMAP_A, src->io_bitmap_a);
+   vmcs_write64(IO_BITMAP_B, src->io_bitmap_b);
+
+   if (cpu_has_vmx_msr_bitmap())
+      vmcs_write64(MSR_BITMAP, src->msr_bitmap);
+
+   vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, src->virtual_apic_page_addr);
+
+   if (vm_need_virtualize_apic_accesses(vcpu->kvm))
+      vmcs_write64(APIC_ACCESS_ADDR,
+              src->apic_access_addr);
+
+   if (enable_ept) {
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+      vmcs_write64(EPT_POINTER, src->ept_pointer);
+      vmcs_write64(GUEST_PDPTR0, src->guest_pdptr0);
+      vmcs_write64(GUEST_PDPTR1, src->guest_pdptr1);
+      vmcs_write64(GUEST_PDPTR2, src->guest_pdptr2);
+      vmcs_write64(GUEST_PDPTR3, src->guest_pdptr3);
+   }
+
+   vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, src->

pin_based_vm_exec_control);

Quoted 1 line

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5813

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+   vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, src->

cpu_based_vm_exec_control);

Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+   vmcs_write32(EXCEPTION_BITMAP, src->exception_bitmap);
+   vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
+           src->page_fault_error_code_mask);
+   vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
+           src->page_fault_error_code_match);
+   vmcs_write32(VM_EXIT_CONTROLS, src->vm_exit_controls);
+   vmcs_write32(VM_ENTRY_CONTROLS, src->vm_entry_controls);
+   vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->

vm_entry_msr_load_count);

Quoted 9 lines
Quoted 9 lines
arch/x86/kvm/vmx.c +9 -0
+
+   if (cpu_has_secondary_exec_ctrls())
+      vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
+              src->secondary_vm_exec_control);
+
+   load_vmcs_common(src);
+
+   load_vmcs_host_state(to_vmx(vcpu)->nested.l1_state->shadow_vmcs);
+
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+   return 0;
+}
+
+void sync_cached_regs_to_vmcs(struct kvm_vcpu *vcpu)
+{
+   unsigned long mask;
+
+   if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->

arch.regs_dirty))

Quoted 2 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5840

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+      vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
+   if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->

arch.regs_dirty))

Quoted 24 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+      vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
+
+   mask = ~((1 << VCPU_REGS_RSP) | (1 << VCPU_REGS_RIP));
+
+   if (vcpu->arch.regs_dirty & mask) {
+      printk(KERN_INFO "WARNING: dirty cached registers

regs_dirty 0x%x mask 0x%lx\n",

Quoted 17 lines
arch/x86/kvm/vmx.c +17 -0
+             vcpu->arch.regs_dirty, mask);
+      WARN_ON(1);
+   }
+
+   vcpu->arch.regs_dirty = 0;
+}
+
+static int nested_vmx_run(struct kvm_vcpu *vcpu)
+{
+   /* verify that l1 has done vmptrld for l2 earlier */
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   int initial_pfu_active = vcpu->fpu_active;
+   int r = 0;
+
+   if (vmx->nested.nested_mode) {
+      printk(KERN_INFO "Nested guest already running\n");
+      set_rflags_to_vmx_fail_valid(vcpu);
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5870

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+      return 1;
+   }
+
+
+   vmx->nested.nested_mode = 1;
+
+   vcpu->arch.exception.pending = false;

Why need this?

I will look into it.

Quoted 24 lines
Quoted 24 lines
arch/x86/kvm/vmx.c +23 -0
+
+   sync_cached_regs_to_vmcs(vcpu);
+
+   save_vmcs(vmx->nested.l1_state->shadow_vmcs);
+
+   vmx->nested.l1_state->shadow_efer = vcpu->arch.shadow_efer;
+   if (!enable_ept)
+      vmx->nested.l1_state->cr3 = vcpu->arch.cr3;
+   vmx->nested.l1_state->cr4 = vcpu->arch.cr4;
+
+   if (enable_vpid) {
+      if (vmx->nested.l2_state->vpid == 0) {
+         allocate_vpid(vmx);
+         vmx->nested.l2_state->vpid = vmx->vpid;
+      }
+   }
+
+   if (cpu_has_vmx_msr_bitmap())
+      vmx->nested.l1_state->msr_bitmap = vmcs_read64(MSR_BITMAP);
+   else
+      vmx->nested.l1_state->msr_bitmap = 0;
+
+   vmx->nested.l1_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5900

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+   vmx->nested.l1_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+   vmx->nested.l1_state->vmcs = vmx->vmcs;
+   vmx->nested.l1_state->cpu = vcpu->cpu;
+   vmx->nested.l1_state->launched = vmx->launched;
+
+   vmx->vmcs = vmx->nested.l2_state->vmcs;
+   vcpu->cpu = vmx->nested.l2_state->cpu;

Who initialize vmx->nested.l2_state->cpu before first launch?

I will fix it.

Quoted 2 lines

Why have different cpu for l1 and l2 guest? It seems like this is global vcpu thread property.

Lets look at this scenario L1 was running on cpu 0 than it launched L2 (at cpu 0) ,
L2 run and did something that caused switching to userspace . When kvm returns to running L2 we
switch to cpu 1, now vcpu->cpu=1.
When we switch back to running L1 we need to handle this switch and so we update vcpu->cpu back to 0 and than call vmx_vcpu_load
that will handle it.

Quoted 4 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5902

Quoted 3 lines
arch/x86/kvm/vmx.c +2 -0
+   vmx->launched = vmx->nested.l2_state->launched;
+

Can you explain why ->launched logic is needed?

It is possible L1 called vmlaunch but we didn't actually run L2 (for example there was an interrupt and
enable_irq_window switched back to L1 before running L2). L1 thinks the vmlaunch was successful and call vmresume in the next time but KVM needs to call vmlaunch for L2.

Quoted 16 lines
Quoted 5 lines
arch/x86/kvm/vmx.c +4 -0
+   if (vmx->nested.l2_state->vmclear || !vmx->launched) {
+      vmcs_clear(vmx->vmcs);
+      vmx->launched = 0;
+      vmx->nested.l2_state->vmclear = 0;

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5916

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+   }
+
+   vmx_vcpu_load(vcpu, get_cpu());
+   put_cpu();
+
+
+   if (!nested_map_shadow_vmcs(vcpu)) {
+      set_rflags_to_vmx_fail_valid(vcpu);
+      return 1;
+   }

No cleanup on error. Is looks like we are on an l2 vmcs at this point.

I will fix it.

Quoted 85 lines
Quoted 85 lines
arch/x86/kvm/vmx.c +84 -0
+
+   prepare_vmcs_02(vcpu);
+
+   if (get_shadow_vmcs(vcpu)->vm_entry_controls &
+       VM_ENTRY_IA32E_MODE) {
+      if (!((vcpu->arch.shadow_efer & EFER_LMA) &&
+            (vcpu->arch.shadow_efer & EFER_LME)))
+         vcpu->arch.shadow_efer |= (EFER_LMA | EFER_LME);
+   } else {
+      if ((vcpu->arch.shadow_efer & EFER_LMA) ||
+          (vcpu->arch.shadow_efer & EFER_LME))
+         vcpu->arch.shadow_efer = 0;
+   }
+
+   vmx_set_cr0(vcpu, get_shadow_vmcs(vcpu)->guest_cr0);
+   vmcs_writel(CR0_READ_SHADOW,
+          get_shadow_vmcs(vcpu)->cr0_read_shadow);
+   vmx_set_cr4(vcpu, get_shadow_vmcs(vcpu)->guest_cr4);
+   vmcs_writel(CR4_READ_SHADOW,
+          get_shadow_vmcs(vcpu)->cr4_read_shadow);
+
+   vcpu->arch.cr0 |= X86_CR0_PG;
+
+   if (enable_ept && !nested_cpu_has_vmx_ept(vcpu)) {
+      vmcs_write32(GUEST_CR3, get_shadow_vmcs(vcpu)->guest_cr3);
+      vmx->vcpu.arch.cr3 = get_shadow_vmcs(vcpu)->guest_cr3;
+   } else {
+      kvm_set_cr3(vcpu, get_shadow_vmcs(vcpu)->guest_cr3);
+      kvm_mmu_reset_context(vcpu);
+
+      nested_unmap_shadow_vmcs(vcpu);
+
+      r = kvm_mmu_load(vcpu);
+      if (unlikely(r)) {
+         printk(KERN_ERR "Error in kvm_mmu_load r %d\n", r);
+         nested_vmx_vmexit(vcpu, false);
+         set_rflags_to_vmx_fail_valid(vcpu);
+         return 1;
+      }
+
+      nested_map_shadow_vmcs(vcpu);
+   }
+
+   kvm_register_write(vcpu, VCPU_REGS_RSP,
+            get_shadow_vmcs(vcpu)->guest_rsp);
+   kvm_register_write(vcpu, VCPU_REGS_RIP,
+            get_shadow_vmcs(vcpu)->guest_rip);
+
+   vmcs_write32(EXCEPTION_BITMAP,
+           (vmx->nested.l1_state->shadow_vmcs->exception_bitmap |
+            get_shadow_vmcs(vcpu)->exception_bitmap));
+
+   nested_unmap_shadow_vmcs(vcpu);
+
+   if (initial_pfu_active)
+      vmx_fpu_activate(vcpu);
+
+   return 1;
+}
+
+static int launch_guest(struct kvm_vcpu *vcpu)
+{
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+
+   skip_emulated_instruction(vcpu);
+
+   nested_vmx_run(vcpu);
+
+   return 1;
+}
+
+static int nested_vmx_vmexit(struct kvm_vcpu *vcpu,
+              bool is_interrupt)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   int initial_pfu_active = vcpu->fpu_active;
+
+   if (!vmx->nested.nested_mode) {
+      printk(KERN_INFO "WARNING: %s called but not in nested mode\n",
+             __func__);
+      return 0;
+   }
+
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6007

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+   save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
+
+   sync_cached_regs_to_vmcs(vcpu);
+
+   if (!nested_map_shadow_vmcs(vcpu)) {
+      printk(KERN_INFO "Error mapping shadow vmcs\n");
+      set_rflags_to_vmx_fail_valid(vcpu);

Error during vmexit should set abort flag, not change flags.

I think this is more a vmlaunch/vmresume error (in the code that switch back to L1).

Quoted 15 lines
Quoted 15 lines
arch/x86/kvm/vmx.c +14 -0
+      return 1;
+   }
+
+   prepare_vmcs_12(vcpu);
+   if (is_interrupt)
+      get_shadow_vmcs(vcpu)->vm_exit_reason =
+         EXIT_REASON_EXTERNAL_INTERRUPT;
+
+   vmx->nested.l2_state->launched = vmx->launched;
+   vmx->nested.l2_state->cpu = vcpu->cpu;
+
+   nested_unmap_shadow_vmcs(vcpu);
+
+   vmx->vmcs = vmx->nested.l1_state->vmcs;
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6028

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+   vcpu->cpu = vmx->nested.l1_state->cpu;
+   vmx->launched = vmx->nested.l1_state->launched;
+
+   vmx_vcpu_load(vcpu, get_cpu());
+   put_cpu();
+
+   vcpu->arch.exception.pending = false;

Why need this?

I will remove it.

Quoted 4 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 4 lines
arch/x86/kvm/vmx.c +3 -0
+
+   vcpu->arch.shadow_efer = vmx->nested.l1_state->shadow_efer;
+   vmx_set_cr0(vcpu, vmx->nested.l1_state->shadow_vmcs->

cr0_read_shadow);

Quoted 6 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 5 lines
arch/x86/kvm/vmx.c +5 -0
+   vmx_set_cr4(vcpu, vmx->nested.l1_state->cr4);
+
+   if (enable_ept) {
+      vcpu->arch.cr3 = vmx->nested.l1_state->shadow_vmcs->guest_cr3;
+      vmcs_write32(GUEST_CR3,

vmx->nested.l1_state->shadow_vmcs->guest_cr3);

Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6043

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+   } else {
+      kvm_set_cr3(vcpu, vmx->nested.l1_state->cr3);
+   }
+
+   if (!nested_map_shadow_vmcs(vcpu)) {
+      printk(KERN_INFO "Error mapping shadow vmcs\n");
+      set_rflags_to_vmx_fail_valid(vcpu);

Abort not flags.

See above.

Quoted 238 lines
Quoted 79 lines
arch/x86/kvm/vmx.c +78 -0
+      return 1;
+   }
+
+   switch_back_vmcs(vcpu);
+
+   nested_unmap_shadow_vmcs(vcpu);
+
+   kvm_register_write(vcpu, VCPU_REGS_RSP,
+            vmx->nested.l1_state->shadow_vmcs->guest_rsp);
+   kvm_register_write(vcpu, VCPU_REGS_RIP,
+            vmx->nested.l1_state->shadow_vmcs->guest_rip);
+
+   vmx->nested.nested_mode = 0;
+
+   kvm_mmu_reset_context(vcpu);
+   kvm_mmu_load(vcpu);
+
+   if (unlikely(vmx->fail)) {
+      vmx->fail = 0;
+      set_rflags_to_vmx_fail_valid(vcpu);
+   } else
+      clear_rflags_cf_zf(vcpu);
+
+   if (initial_pfu_active)
+      vmx_fpu_activate(vcpu);
+
+   return 0;
+}
+
+static int nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu)
+{
+   if (to_vmx(vcpu)->nested.nested_mode) {
+      struct page *msr_page = NULL;
+      u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
+      u32 exit_code = vmcs_read32(VM_EXIT_REASON);
+      struct shadow_vmcs *l2svmcs = get_shadow_vmcs(vcpu);
+
+      if (!cpu_has_vmx_msr_bitmap()
+          || !nested_cpu_has_vmx_msr_bitmap(vcpu))
+         return 1;
+
+      msr_page = nested_get_page(vcpu,
+                  l2svmcs->msr_bitmap);
+
+      if (!msr_page) {
+         printk(KERN_INFO "%s error in nested_get_page\n",
+                __func__);
+         return 0;
+      }
+
+      switch (exit_code) {
+      case EXIT_REASON_MSR_READ:
+         if (msr_index <= 0x1fff) {
+            if (test_bit(msr_index,
+                    (unsigned long *)(msr_page +
+                            0x000)))
+               return 1;
+         } else if ((msr_index >= 0xc0000000) &&
+               (msr_index <= 0xc0001fff)) {
+            msr_index &= 0x1fff;
+            if (test_bit(msr_index,
+                    (unsigned long *)(msr_page +
+                            0x400)))
+               return 1;
+         }
+         break;
+      case EXIT_REASON_MSR_WRITE:
+         if (msr_index <= 0x1fff) {
+            if (test_bit(msr_index,
+                    (unsigned long *)(msr_page +
+                            0x800)))
+                  return 1;
+         } else if ((msr_index >= 0xc0000000) &&
+               (msr_index <= 0xc0001fff)) {
+            msr_index &= 0x1fff;
+            if (test_bit(msr_index,
+                    (unsigned long *)(msr_page +
+                            0xc00)))

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+               return 1;
+         }
+         break;
+      }
+   }
+
+   return 0;
+}
+
+static int nested_vmx_exit_handled(struct kvm_vcpu *vcpu, bool

kvm_override)

Quoted 148 lines
arch/x86/kvm/vmx.c +148 -0
+{
+   u32 exit_code = vmcs_read32(VM_EXIT_REASON);
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+   struct shadow_vmcs *l2svmcs;
+
+   int r = 0;
+
+   if (vmx->nested.nested_run_pending)
+      return 0;
+
+   if (unlikely(vmx->fail)) {
+      printk(KERN_INFO "%s failed vm entry %x\n",
+             __func__, vmcs_read32(VM_INSTRUCTION_ERROR));
+      return 1;
+   }
+
+   if (kvm_override) {
+      switch (exit_code) {
+      case EXIT_REASON_EXTERNAL_INTERRUPT:
+         return 0;
+      case EXIT_REASON_EXCEPTION_NMI:
+         if (!is_exception(intr_info))
+            return 0;
+
+         if (is_page_fault(intr_info) && (!enable_ept))
+            return 0;
+
+         break;
+      case EXIT_REASON_EPT_VIOLATION:
+         if (enable_ept)
+            return 0;
+
+         break;
+      }
+   }
+
+
+   if (!nested_map_shadow_vmcs(vcpu))
+      return 0;
+   l2svmcs = get_shadow_vmcs(vcpu);
+
+   switch (exit_code) {
+   case EXIT_REASON_INVLPG:
+      if (l2svmcs->cpu_based_vm_exec_control &
+          CPU_BASED_INVLPG_EXITING)
+         r = 1;
+      break;
+   case EXIT_REASON_MSR_READ:
+   case EXIT_REASON_MSR_WRITE:
+      r = nested_vmx_exit_handled_msr(vcpu);
+      break;
+   case EXIT_REASON_CR_ACCESS: {
+      unsigned long exit_qualification =
+         vmcs_readl(EXIT_QUALIFICATION);
+      int cr = exit_qualification & 15;
+      int reg = (exit_qualification >> 8) & 15;
+      unsigned long val = kvm_register_read(vcpu, reg);
+
+      switch ((exit_qualification >> 4) & 3) {
+      case 0: /* mov to cr */
+         switch (cr) {
+         case 0:
+            if (l2svmcs->cr0_guest_host_mask &
+                (val ^ l2svmcs->cr0_read_shadow))
+               r = 1;
+            break;
+         case 3:
+            if (l2svmcs->cpu_based_vm_exec_control &
+                CPU_BASED_CR3_LOAD_EXITING)
+               r = 1;
+            break;
+         case 4:
+            if (l2svmcs->cr4_guest_host_mask &
+                (l2svmcs->cr4_read_shadow ^ val))
+               r = 1;
+            break;
+         case 8:
+            if (l2svmcs->cpu_based_vm_exec_control &
+                CPU_BASED_CR8_LOAD_EXITING)
+               r = 1;
+            break;
+         }
+         break;
+      case 2: /* clts */
+         if (l2svmcs->cr0_guest_host_mask &
+             (val ^ l2svmcs->cr0_read_shadow))
+            r = 1;
+         break;
+      case 1: /*mov from cr*/
+         switch (cr) {
+         case 0:
+            r = 1;
+         case 3:
+            if (l2svmcs->cpu_based_vm_exec_control &
+                CPU_BASED_CR3_STORE_EXITING)
+               r = 1;
+            break;
+         case 4:
+            r = 1;
+            break;
+         case 8:
+            if (l2svmcs->cpu_based_vm_exec_control &
+                CPU_BASED_CR8_STORE_EXITING)
+               r = 1;
+            break;
+         }
+         break;
+      case 3: /* lmsw */
+         if (l2svmcs->cr0_guest_host_mask &
+             (val ^ l2svmcs->cr0_read_shadow))
+            r = 1;
+         break;
+      }
+      break;
+   }
+   case EXIT_REASON_DR_ACCESS: {
+      if (l2svmcs->cpu_based_vm_exec_control &
+          CPU_BASED_MOV_DR_EXITING)
+         r = 1;
+      break;
+   }
+
+   case EXIT_REASON_EXCEPTION_NMI: {
+
+      if (is_external_interrupt(intr_info) &&
+          (l2svmcs->pin_based_vm_exec_control &
+           PIN_BASED_EXT_INTR_MASK))
+         r = 1;
+      else if (is_nmi(intr_info) &&
+          (l2svmcs->pin_based_vm_exec_control &
+           PIN_BASED_NMI_EXITING))
+         r = 1;
+      else if (is_exception(intr_info) &&
+          (l2svmcs->exception_bitmap &
+           (1u << (intr_info & INTR_INFO_VECTOR_MASK))))
+         r = 1;
+      else if (is_page_fault(intr_info))
+         r = 1;
+      break;
+   }
+
+   case EXIT_REASON_EXTERNAL_INTERRUPT:
+      if (l2svmcs->pin_based_vm_exec_control &
+          PIN_BASED_EXT_INTR_MASK)
+         r = 1;
+      break;
+   default:
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+      r = 1;
+   }
+   nested_unmap_shadow_vmcs(vcpu);
+
+   return r;
+}
+
+static int nested_vmx_check_exception(struct vcpu_vmx *vmx, unsigned

nr,

Quoted 23 lines
Quoted 23 lines
arch/x86/kvm/vmx.c +23 -0
+                  bool has_error_code, u32 error_code)
+{
+   if (vmx->nested.nested_mode) {
+      if (nested_vmx_exit_handled(&vmx->vcpu, false)) {
+         nested_vmx_vmexit(&vmx->vcpu, false);
+         if (!nested_map_shadow_vmcs(&vmx->vcpu))
+            return 1;
+         get_shadow_vmcs(&vmx->vcpu)->vm_exit_reason =
+            EXIT_REASON_EXCEPTION_NMI;
+         get_shadow_vmcs(&vmx->vcpu)->vm_exit_intr_info =
+            (nr | INTR_TYPE_HARD_EXCEPTION
+             | (has_error_code ?
+                INTR_INFO_DELIVER_CODE_MASK : 0)
+             | INTR_INFO_VALID_MASK);
+
+         if (has_error_code)
+            get_shadow_vmcs(&vmx->vcpu)->
+               vm_exit_intr_error_code = error_code;
+         nested_unmap_shadow_vmcs(&vmx->vcpu);
+         return 1;
+      }
+   }
+   return 0;
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6317

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+}
+
+static int nested_vmx_intr(struct kvm_vcpu *vcpu)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+   if (vmx->nested.nested_mode) {

This function is called only after checking nested_mode. Why recheck?

I will remove it.

Quoted 37 lines
Quoted 30 lines
arch/x86/kvm/vmx.c +23 -1
+
+      if (!nested_map_shadow_vmcs(vcpu))
+         return 0;
+
+      if (get_shadow_vmcs(vcpu)->pin_based_vm_exec_control &
+          PIN_BASED_EXT_INTR_MASK) {
+
+         if (vmx->nested.nested_run_pending) {
+            nested_unmap_shadow_vmcs(vcpu);
+            return 0;
+         }
+
+         nested_unmap_shadow_vmcs(vcpu);
+         nested_vmx_vmexit(vcpu, true);
+         return 1;
+      }
+
+      nested_unmap_shadow_vmcs(vcpu);
+
+   }
+
+   return 0;
+}
 
 static struct kvm_x86_ops vmx_x86_ops = {
    .cpu_has_kvm_support = cpu_has_kvm_support,
--

1.6.0.4

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
Gleb.

#8 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 14 lines · Orit Wasserman
Quoted 9 lines

Review static void vmx_complete_interrupts(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -4641,10 +4955,13 @@ static void vmx_complete_interrupts(struct

vcpu_vmx *vmx)

Quoted 7 lines
arch/x86/kvm/vmx.c +0 -2
    int type;
    bool idtv_info_valid;
 
-   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
-
    vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
Quoted 5 lines

arch/x86/kvm/vmx.c static void vmx_complete_interrupts(struct vcpu_vmx *vmx) · line 4962

Quoted 3 lines
arch/x86/kvm/vmx.c +3 -0
+   if (vmx->nested.nested_mode)
+      return;
+

Why return here? What the function does that should not be done in nested mode?

Quoted 4 lines

In nested mode L0 injects an interrupt to L2 only in one scenario, if there is an IDT_VALID event and L0 decides to run L2 again and not to switch back to L1.
In all other cases the injection is handled by L1.

This is exactly the kind of scenario that is handled by vmx_complete_interrupts(). (vmx|svm)_complete_interrups() store pending event in arch agnostic way and re-injection is handled by x86.c You bypass this logic by inserting return here and introducing nested_handle_valid_idt() function below.

Quoted 8 lines
Quoted 7 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
+   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
    /* Handle machine checks before interrupts are enabled */
    if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
        || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
@@ -4747,6 +5064,60 @@ static void fixup_rmode_irq(struct vcpu_vmx

*vmx)

Quoted 8 lines
Quoted 7 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx *vmx) · line 5068

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
       | vmx->rmode.irq.vector;
 }
 
+static int nested_handle_valid_idt(struct kvm_vcpu *vcpu)
+{

It seems by this function you are trying to bypass general event reinjection logic. Why?

See above.

The logic implemented by this function is handled in x86.c in arch agnostic way. Is there something wrong with this?

Quoted 8 lines
Quoted 3 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5902

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+   vmx->launched = vmx->nested.l2_state->launched;
+

Can you explain why ->launched logic is needed?

It is possible L1 called vmlaunch but we didn't actually run L2 (for example there was an interrupt and
enable_irq_window switched back to L1 before running L2). L1 thinks the vmlaunch was successful and call vmresume in the next time but KVM needs to call vmlaunch for L2.

handle_vmlauch() and handle_vmresume() are exactly the same. Why KVM needs to run one and not the other?

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/vmx.c +12 -0
+static int nested_vmx_vmexit(struct kvm_vcpu *vcpu,
+              bool is_interrupt)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   int initial_pfu_active = vcpu->fpu_active;
+
+   if (!vmx->nested.nested_mode) {
+      printk(KERN_INFO "WARNING: %s called but not in nested mode\n",
+             __func__);
+      return 0;
+   }
+
Quoted 8 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6007

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+   save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
+
+   sync_cached_regs_to_vmcs(vcpu);
+
+   if (!nested_map_shadow_vmcs(vcpu)) {
+      printk(KERN_INFO "Error mapping shadow vmcs\n");
+      set_rflags_to_vmx_fail_valid(vcpu);

Error during vmexit should set abort flag, not change flags.

Quoted 2 lines

I think this is more a vmlaunch/vmresume error (in the code that switch back to L1).

How is this vmlaunch/vmresume error? This function is called to exit from L2 guest while on L2 vcms. It is called asynchronously in respect to L2 guest and you modify L2 guest rflags register at unpredictable place here.

--
Gleb.

#9 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Gleb Natapov <[email protected]> wrote on 22/10/2009 11:04:58:

Quoted 16 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

Abel Gordon/Haifa/IBM@IBMIL, [email protected], Ben-Ami Yassour1/ Haifa/IBM@IBMIL, [email protected], [email protected], Muli Ben- Yehuda/Haifa/IBM@IBMIL

Date:

Quoted 23 lines

22/10/2009 11:05

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 16 lines · Orit Wasserman
Quoted 9 lines

Review static void vmx_complete_interrupts(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -4641,10 +4955,13 @@ static void vmx_complete_interrupts(struct

vcpu_vmx *vmx)

Quoted 7 lines
arch/x86/kvm/vmx.c +0 -2
    int type;
    bool idtv_info_valid;
 
-   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
-
    vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
Quoted 5 lines

arch/x86/kvm/vmx.c static void vmx_complete_interrupts(struct vcpu_vmx *vmx) · line 4962

Quoted 3 lines
arch/x86/kvm/vmx.c +3 -0
+   if (vmx->nested.nested_mode)
+      return;
+

Why return here? What the function does that should not be done in nested mode?

In nested mode L0 injects an interrupt to L2 only in one scenario, if there is an IDT_VALID event and L0 decides to run L2 again and not

to

Quoted 7 lines
Quoted 2 lines

switch back to L1.
In all other cases the injection is handled by L1.

This is exactly the kind of scenario that is handled by vmx_complete_interrupts(). (vmx|svm)_complete_interrups() store pending event in arch agnostic way and re-injection is handled by x86.c You bypass this logic by inserting return here and introducing nested_handle_valid_idt() function below.

The only location we can truly know if we are switching to L1 is in vmx_vcpu_run
because enable_irq_window (that is called after handling the exit) can decide to
switch to L1 because of an interrupt.
In order to simplify our code it was simpler to bypass vmx_complete_interrupts when it is called (after
running L2) and to add nested_handle_valid_idt just before running L2.

Quoted 11 lines
Quoted 11 lines
Quoted 7 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
+   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
    /* Handle machine checks before interrupts are enabled */
    if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
        || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
@@ -4747,6 +5064,60 @@ static void fixup_rmode_irq(struct vcpu_vmx

*vmx)

Quoted 3 lines
Quoted 3 lines
arch/x86/kvm/vmx.c +0 -0
       | vmx->rmode.irq.vector;
 }
 
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx *vmx) · line 5068

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+static int nested_handle_valid_idt(struct kvm_vcpu *vcpu)
+{

It seems by this function you are trying to bypass general event reinjection logic. Why?

See above.

The logic implemented by this function is handled in x86.c in arch agnostic way. Is there something wrong with this?

See my comment before

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 5902

Quoted 3 lines
arch/x86/kvm/vmx.c +2 -0
+   vmx->launched = vmx->nested.l2_state->launched;
+

Can you explain why ->launched logic is needed?

It is possible L1 called vmlaunch but we didn't actually run L2 (for example there was an interrupt and
enable_irq_window switched back to L1 before running L2). L1 thinks the vmlaunch was successful and call vmresume in the next time but KVM needs to call vmlaunch for L2.

handle_vmlauch() and handle_vmresume() are exactly the same. Why KVM

needs

Quoted 1 line

to run one and not the other?

Yes they are very similar (almost the same code) the only difference is the check of vmclear,
we need to emulate the vmx hardware behavior for those two commands and check VMC12 state.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst)

Quoted 9 lines
arch/x86/kvm/vmx.c +8 -0
+static int nested_vmx_vmexit(struct kvm_vcpu *vcpu,
+              bool is_interrupt)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   int initial_pfu_active = vcpu->fpu_active;
+
+   if (!vmx->nested.nested_mode) {
+      printk(KERN_INFO "WARNING: %s called but not in nested mode

\n",

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

arch/x86/kvm/vmx.c void save_vmcs(struct shadow_vmcs *dst) · line 6007

Quoted 11 lines
arch/x86/kvm/vmx.c +11 -0
+             __func__);
+      return 0;
+   }
+
+   save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
+
+   sync_cached_regs_to_vmcs(vcpu);
+
+   if (!nested_map_shadow_vmcs(vcpu)) {
+      printk(KERN_INFO "Error mapping shadow vmcs\n");
+      set_rflags_to_vmx_fail_valid(vcpu);

Error during vmexit should set abort flag, not change flags.

Quoted 6 lines
Quoted 2 lines

I think this is more a vmlaunch/vmresume error (in the code that switch back to L1).

How is this vmlaunch/vmresume error? This function is called to exit from L2 guest while on L2 vcms. It is called asynchronously in respect to L2 guest and you modify L2 guest rflags register at unpredictable place here.

OK.

Quoted 3 lines

--
Gleb.

#10 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 51 lines · Orit Wasserman

Gleb Natapov <[email protected]> wrote on 22/10/2009 11:04:58:

Quoted 16 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

Abel Gordon/Haifa/IBM@IBMIL, [email protected], Ben-Ami Yassour1/ Haifa/IBM@IBMIL, [email protected], [email protected], Muli Ben- Yehuda/Haifa/IBM@IBMIL

Date:

Quoted 23 lines

22/10/2009 11:05

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 16 lines · Orit Wasserman
Quoted 9 lines

Review static void vmx_complete_interrupts(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -4641,10 +4955,13 @@ static void vmx_complete_interrupts(struct

vcpu_vmx *vmx)

Quoted 7 lines
arch/x86/kvm/vmx.c +0 -2
    int type;
    bool idtv_info_valid;
 
-   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
-
    vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
Quoted 5 lines

arch/x86/kvm/vmx.c static void vmx_complete_interrupts(struct vcpu_vmx *vmx) · line 4962

Quoted 3 lines
arch/x86/kvm/vmx.c +3 -0
+   if (vmx->nested.nested_mode)
+      return;
+

Why return here? What the function does that should not be done in nested mode?

In nested mode L0 injects an interrupt to L2 only in one scenario, if there is an IDT_VALID event and L0 decides to run L2 again and not

to

Quoted 7 lines
Quoted 2 lines

switch back to L1.
In all other cases the injection is handled by L1.

This is exactly the kind of scenario that is handled by vmx_complete_interrupts(). (vmx|svm)_complete_interrups() store pending event in arch agnostic way and re-injection is handled by x86.c You bypass this logic by inserting return here and introducing nested_handle_valid_idt() function below.

Quoted 5 lines

The only location we can truly know if we are switching to L1 is in vmx_vcpu_run
because enable_irq_window (that is called after handling the exit) can decide to
switch to L1 because of an interrupt.

enable_irq_window() will be called after L2 VMCS will be setup for event re-injection by previous call to inject_pending_event(). As far as I can see this should work for interrupt injection. For exception we should probably require l2 guest to re execute faulted instruction for now like svm does.

Quoted 14 lines

In order to simplify our code it was simpler to bypass vmx_complete_interrupts when it is called (after
running L2) and to add nested_handle_valid_idt just before running L2.

Quoted 11 lines
Quoted 11 lines
Quoted 7 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
+   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
    /* Handle machine checks before interrupts are enabled */
    if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
        || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
@@ -4747,6 +5064,60 @@ static void fixup_rmode_irq(struct vcpu_vmx

*vmx)

Quoted 3 lines
Quoted 3 lines
arch/x86/kvm/vmx.c +0 -0
       | vmx->rmode.irq.vector;
 }
 
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx *vmx) · line 5068

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+static int nested_handle_valid_idt(struct kvm_vcpu *vcpu)
+{

It seems by this function you are trying to bypass general event reinjection logic. Why?

See above.

The logic implemented by this function is handled in x86.c in arch agnostic way. Is there something wrong with this?

See my comment before

Sometimes it is wrong to reinject events from L0 to L2 directly. If L2 was not able to handle event because its IDT is not mapped by L1 shadow page table we should generate PF vmexit with valid idt vectoring info to L1 and let L1 handle event reinjection.

--
Gleb.

#11 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Gleb Natapov <[email protected]> wrote on 25/10/2009 11:44:31:

Quoted 43 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

Abel Gordon/Haifa/IBM@IBMIL, [email protected], Ben-Ami Yassour1/ Haifa/IBM@IBMIL, [email protected], [email protected], Muli Ben- Yehuda/Haifa/IBM@IBMIL

Date:

25/10/2009 11:44

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 20 lines · Orit Wasserman

Gleb Natapov <[email protected]> wrote on 22/10/2009 11:04:58:

Quoted 16 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

Abel Gordon/Haifa/IBM@IBMIL, [email protected], Ben-Ami Yassour1/ Haifa/IBM@IBMIL, [email protected], [email protected], Muli Ben- Yehuda/Haifa/IBM@IBMIL

Date:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

22/10/2009 11:05

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 1 line · Orit Wasserman
Quoted 1 line

Review static void vmx_complete_interrupts

Quoted 1 line
(quoted diff) +0 -0
@@ -4641,10 +4955,13 @@ static void vmx_complete_interrupts

(struct

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

vcpu_vmx *vmx)

Quoted 3 lines
arch/x86/kvm/vmx.c +0 -0
    int type;
    bool idtv_info_valid;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_complete_interrupts(struct vcpu_vmx *vmx) · line 4962

Quoted 7 lines
arch/x86/kvm/vmx.c +3 -2
-   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
-
    vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
+   if (vmx->nested.nested_mode)
+      return;
+

Why return here? What the function does that should not be done

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

nested mode?

In nested mode L0 injects an interrupt to L2 only in one scenario, if there is an IDT_VALID event and L0 decides to run L2 again and

not

Quoted 13 lines
Quoted 8 lines

to

Quoted 7 lines
Quoted 2 lines

switch back to L1.
In all other cases the injection is handled by L1.

This is exactly the kind of scenario that is handled by vmx_complete_interrupts(). (vmx|svm)_complete_interrups() store pending event in arch agnostic way and re-injection is handled by x86.c You bypass this logic by inserting return here and introducing nested_handle_valid_idt() function below.

Quoted 5 lines

The only location we can truly know if we are switching to L1 is in vmx_vcpu_run
because enable_irq_window (that is called after handling the exit) can decide to
switch to L1 because of an interrupt.

Quoted 5 lines

enable_irq_window() will be called after L2 VMCS will be setup for event re-injection by previous call to inject_pending_event(). As far as I can see this should work for interrupt injection. For exception we should probably require l2 guest to re execute faulted instruction for now like svm does.

The main issue is that L0 doesn't inject events to L2 but L1 hypervisor (we want to keep the nested hypervisor semantics as
much as possible). Only if the event was caused by the fact that L2 is a nested guest
and L1 can't handle it L0 will re-inject and event to L2, for example IDT event
with page fault that is caused by a missing entry in SPT02 (the shadow page table L0 create for L2).
In this case when vmx_complete_intterupts is called L0 doesn't know if the page fault should be handled by it or
by L1 (it is decided later when handling the exit).
In most other cases , L0 will switch to L1 and L1 will decide if there will be re-injection
(depends on the L1 hypervisor logic) and update L2 VMCS accordingly.

Quoted 11 lines
Quoted 11 lines

In order to simplify our code it was simpler to bypass vmx_complete_interrupts when it is called (after
running L2) and to add nested_handle_valid_idt just before running L2.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
+   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
    /* Handle machine checks before interrupts are enabled */
    if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
        || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
@@ -4747,6 +5064,60 @@ static void fixup_rmode_irq(struct

vcpu_vmx

Quoted 19 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

*vmx)

Quoted 3 lines
Quoted 3 lines
arch/x86/kvm/vmx.c +0 -0
       | vmx->rmode.irq.vector;
 }
 
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

arch/x86/kvm/vmx.c static void fixup_rmode_irq(struct vcpu_vmx *vmx) · line 5068

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+static int nested_handle_valid_idt(struct kvm_vcpu *vcpu)
+{

It seems by this function you are trying to bypass general event reinjection logic. Why?

See above.

The logic implemented by this function is handled in x86.c in arch agnostic way. Is there something wrong with this?

See my comment before

Sometimes it is wrong to reinject events from L0 to L2 directly. If L2 was not able to handle event because its IDT is not mapped by L1 shadow page table we should generate PF vmexit with valid idt vectoring info to L1 and let L1 handle event reinjection.

--
Gleb.

#12 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 91 lines · Orit Wasserman

Gleb Natapov <[email protected]> wrote on 25/10/2009 11:44:31:

Quoted 43 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

Abel Gordon/Haifa/IBM@IBMIL, [email protected], Ben-Ami Yassour1/ Haifa/IBM@IBMIL, [email protected], [email protected], Muli Ben- Yehuda/Haifa/IBM@IBMIL

Date:

25/10/2009 11:44

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 20 lines · Orit Wasserman

Gleb Natapov <[email protected]> wrote on 22/10/2009 11:04:58:

Quoted 16 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

Abel Gordon/Haifa/IBM@IBMIL, [email protected], Ben-Ami Yassour1/ Haifa/IBM@IBMIL, [email protected], [email protected], Muli Ben- Yehuda/Haifa/IBM@IBMIL

Date:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

22/10/2009 11:05

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 1 line · Orit Wasserman
Quoted 1 line

Review static void vmx_complete_interrupts

Quoted 1 line
(quoted diff) +0 -0
@@ -4641,10 +4955,13 @@ static void vmx_complete_interrupts

(struct

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

vcpu_vmx *vmx)

Quoted 3 lines
(quoted diff) +0 -0
    int type;
    bool idtv_info_valid;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review static void vmx_complete_interrupts · line 4962

Quoted 7 lines
(quoted diff) +3 -2
-   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
-
    vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
+   if (vmx->nested.nested_mode)
+      return;
+

Why return here? What the function does that should not be done

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

nested mode?

In nested mode L0 injects an interrupt to L2 only in one scenario, if there is an IDT_VALID event and L0 decides to run L2 again and

not

Quoted 13 lines
Quoted 8 lines

to

Quoted 7 lines
Quoted 2 lines

switch back to L1.
In all other cases the injection is handled by L1.

This is exactly the kind of scenario that is handled by vmx_complete_interrupts(). (vmx|svm)_complete_interrups() store pending event in arch agnostic way and re-injection is handled by x86.c You bypass this logic by inserting return here and introducing nested_handle_valid_idt() function below.

Quoted 5 lines

The only location we can truly know if we are switching to L1 is in vmx_vcpu_run
because enable_irq_window (that is called after handling the exit) can decide to
switch to L1 because of an interrupt.

Quoted 5 lines

enable_irq_window() will be called after L2 VMCS will be setup for event re-injection by previous call to inject_pending_event(). As far as I can see this should work for interrupt injection. For exception we should probably require l2 guest to re execute faulted instruction for now like svm does.

Quoted 11 lines

The main issue is that L0 doesn't inject events to L2 but L1 hypervisor (we want to keep the nested hypervisor semantics as
much as possible). Only if the event was caused by the fact that L2 is a nested guest
and L1 can't handle it L0 will re-inject and event to L2, for example IDT event
with page fault that is caused by a missing entry in SPT02 (the shadow page table L0 create for L2).
In this case when vmx_complete_intterupts is called L0 doesn't know if the page fault should be handled by it or
by L1 (it is decided later when handling the exit).

So what? When it will be decided that L2 exit is needed pending event will be transfered into L2's idt_vectoring_info. Otherwise event will be reinfected by usual mechanism. BTW I don't see where you current code setup L2's idt_vectoring_info if it is decided that L1 should handle event reinjection.

Quoted 8 lines

In most other cases , L0 will switch to L1 and L1 will decide if there will be re-injection
(depends on the L1 hypervisor logic) and update L2 VMCS accordingly.

Quoted 5 lines
Quoted 5 lines

In order to simplify our code it was simpler to bypass vmx_complete_interrupts when it is called (after
running L2) and to add nested_handle_valid_idt just before running L2.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 26 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Review static void fixup_rmode_irq(struct

Quoted 6 lines
(quoted diff) +2 -0
+   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
    /* Handle machine checks before interrupts are enabled */
    if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
        || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
@@ -4747,6 +5064,60 @@ static void fixup_rmode_irq(struct

vcpu_vmx

Quoted 19 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

*vmx)

Quoted 3 lines
Quoted 3 lines
(quoted diff) +0 -0
       | vmx->rmode.irq.vector;
 }
 
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

Review static void fixup_rmode_irq(struct · line 5068

Quoted 2 lines
(quoted diff) +2 -0
+static int nested_handle_valid_idt(struct kvm_vcpu *vcpu)
+{

It seems by this function you are trying to bypass general event reinjection logic. Why?

See above.

The logic implemented by this function is handled in x86.c in arch agnostic way. Is there something wrong with this?

See my comment before

Sometimes it is wrong to reinject events from L0 to L2 directly. If L2 was not able to handle event because its IDT is not mapped by L1 shadow page table we should generate PF vmexit with valid idt vectoring info to L1 and let L1 handle event reinjection.

--
Gleb.

--
Gleb.

#13 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Gleb Natapov <[email protected]> wrote on 29/10/2009 19:31:05:

Quoted 10 lines

[image removed]

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Gleb Natapov

to:

Orit Wasserman

Quoted 49 lines

29/10/2009 19:31

Cc:

Abel Gordon, aliguori, Ben-Ami Yassour1, kvm, mdday, Muli Ben-Yehuda

Quoted 42 lines · Orit Wasserman

Gleb Natapov <[email protected]> wrote on 25/10/2009 11:44:31:

Quoted 38 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

Abel Gordon/Haifa/IBM@IBMIL, [email protected], Ben-Ami Yassour1/ Haifa/IBM@IBMIL, [email protected], [email protected], Muli Ben- Yehuda/Haifa/IBM@IBMIL

Date:

25/10/2009 11:44

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 15 lines · Orit Wasserman

Gleb Natapov <[email protected]> wrote on 22/10/2009 11:04:58:

Quoted 11 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

Abel Gordon/Haifa/IBM@IBMIL, [email protected], Ben-Ami

Yassour1/

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Haifa/IBM@IBMIL, [email protected], [email protected], Muli Ben- Yehuda/Haifa/IBM@IBMIL

Date:

22/10/2009 11:05

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and

vmresume

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines · Orit Wasserman
Quoted 1 line
Quoted 1 line

Review static void vmx_complete_interrupts

Quoted 1 line
(quoted diff) +0 -0
@@ -4641,10 +4955,13 @@ static void vmx_complete_interrupts

(struct

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

vcpu_vmx *vmx)

Quoted 3 lines
(quoted diff) +0 -0
    int type;
    bool idtv_info_valid;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review static void vmx_complete_interrupts · line 4962

Quoted 7 lines
(quoted diff) +3 -2
-   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
-
    vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
+   if (vmx->nested.nested_mode)
+      return;
+

Why return here? What the function does that should not be

done

Quoted 3 lines
Quoted 3 lines

in

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

nested mode?

In nested mode L0 injects an interrupt to L2 only in one

scenario,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

if there is an IDT_VALID event and L0 decides to run L2 again

and

Quoted 8 lines
Quoted 8 lines

not

Quoted 7 lines
Quoted 7 lines

to

Quoted 6 lines
Quoted 2 lines

switch back to L1.
In all other cases the injection is handled by L1.

This is exactly the kind of scenario that is handled by vmx_complete_interrupts(). (vmx|svm)_complete_interrups() store pending event in arch agnostic way and re-injection is handled by x86.c You bypass this logic by inserting return here and

introducing

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

nested_handle_valid_idt() function below.

The only location we can truly know if we are switching to L1 is in vmx_vcpu_run
because enable_irq_window (that is called after handling the exit)

can

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

decide to
switch to L1 because of an interrupt.

enable_irq_window() will be called after L2 VMCS will be setup for

event

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

re-injection by previous call to inject_pending_event(). As far as I can see this should work for interrupt injection. For exception we should probably require l2 guest to re execute faulted instruction

for

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

now like svm does.

The main issue is that L0 doesn't inject events to L2 but L1 hypervisor

(we

Quoted 2 lines
Quoted 2 lines

want to keep the nested hypervisor semantics as
much as possible). Only if the event was caused by the fact that L2 is

a

Quoted 2 lines
Quoted 2 lines

nested guest
and L1 can't handle it L0 will re-inject and event to L2, for example

IDT

Quoted 2 lines
Quoted 2 lines

event
with page fault that is caused by a missing entry in SPT02 (the shadow

page

Quoted 2 lines
Quoted 2 lines

table L0 create for L2).
In this case when vmx_complete_intterupts is called L0 doesn't know if

the

Quoted 7 lines
Quoted 2 lines

page fault should be handled by it or
by L1 (it is decided later when handling the exit).

So what? When it will be decided that L2 exit is needed pending event will be transfered into L2's idt_vectoring_info. Otherwise event will be reinfected by usual mechanism. BTW I don't see where you current code setup L2's idt_vectoring_info if it is decided that L1 should handle event re-injection.

Suppose we are executing an L2 guest and we got an exit. There are 2 possible scenarios here:
A) The L2 exit will be handled by the L1 guest hypervisor. In this case when we switch to L1 the IDT vectoring info field is copied from vmcs(02) to vmcs(12) in prepare_vmcs_12 (part of the nested_vmx_vmexit path). Now is under responsibility of L1 to deal with the IDT and do the corresponding logic.
B) The L2 exit will be handled only by L0. In this case we never switch to L1. L0 handles the exit and resume L2. Any pending event in vmcs(02) idt vectoring info field is injected to l2 when L0 resumes it.

KVM handles IDT in at the end of vmx_vcpu_run, calling vmx_complete_interrupts. The decision to switch or not switch to L1 is made in the following points:
1) nested_vmx_check_exception (called from vmx_queue_exception)
2) nested_vmx_intr (called from vmx_interrupt_allowed and enable_irq_window)
3) vmx_handle_exit

Quoted 1 line

From x86 perspective the flow looks as follow:

vcpu_enter_guest {
1
2
run (includes vmx_complete_interrupts)
3
}

All these functions are called after vmx_vcpu_run finished and vmx_complete_interrupts was already executed. This impede us to re-use regular non-nested IDT hadling because we still don't know there if the IDT pending event must be injected or not. That's the reason we added the function nested_handle_valid_idt which is called at the beginning of vmx_vcpu_run. So now, the flow from x86 perspective will look like:
vcpu_enter_guest {
1
2
check_nested_idt (injects pending IDT to L2 if necessary. only case B) run
3
}

Quoted 1 line
Quoted 1 line

In most other cases , L0 will switch to L1 and L1 will decide if there

will

Quoted 6 lines
Quoted 6 lines

be re-injection
(depends on the L1 hypervisor logic) and update L2 VMCS accordingly.

Quoted 4 lines
Quoted 4 lines

In order to simplify our code it was simpler to bypass vmx_complete_interrupts when it is called (after running L2) and to add nested_handle_valid_idt just before running

L2.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Review static void vmx_complete_interrupts · line 4965

Quoted 4 lines
(quoted diff) +2 -0
+   exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
    /* Handle machine checks before interrupts are enabled

*/

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Review static void vmx_complete_interrupts · line 4966

Quoted 1 line
(quoted diff) +0 -0
    if ((vmx->exit_reason ==

EXIT_REASON_MCE_DURING_VMENTRY)

Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Review static void fixup_rmode_irq(struct

Quoted 2 lines
(quoted diff) +0 -0
        || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
@@ -4747,6 +5064,60 @@ static void fixup_rmode_irq(struct

vcpu_vmx

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

*vmx)

Quoted 6 lines

Review static void fixup_rmode_irq(struct · line 5068

Quoted 5 lines
(quoted diff) +2 -0
       | vmx->rmode.irq.vector;
 }
 
+static int nested_handle_valid_idt(struct kvm_vcpu *vcpu)
+{

It seems by this function you are trying to bypass general

event

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line

reinjection logic. Why?

See above.

The logic implemented by this function is handled in x86.c in

arch

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line

agnostic way. Is there something wrong with this?

See my comment before

Sometimes it is wrong to reinject events from L0 to L2 directly. If

L2

Quoted 1 line
Quoted 1 line
Quoted 1 line

was not able to handle event because its IDT is not mapped by L1

shadow

Quoted 1 line
Quoted 1 line
Quoted 1 line

page table we should generate PF vmexit with valid idt vectoring info

to

Quoted 1 line
Quoted 1 line
Quoted 1 line

L1 and let L1 handle event reinjection.

According to above explanation I think this is what we are doing in the required case (A). Are we missing something ?

Abel.

#14 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

In reply to Orit Wasserman
Quoted 7 lines · Orit Wasserman

It is possible L1 called vmlaunch but we didn't actually run L2 (for example there was an interrupt and
enable_irq_window switched back to L1 before running L2). L1 thinks the vmlaunch was successful and call vmresume in the next time but KVM needs to call vmlaunch for L2.

Is it really possible? If vmlaunch is started, it should complete unconditionally for L1. The irq window should be recalculated based on the guest vmcs "exit on external interrupt" and guest eflags/cr8 etc.

Signature · 1 line
error compiling committee.c: too many arguments to function
#15 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

In reply to [email protected]
Quoted 19 lines · On 10/15/2009 11:41 PM, [email protected] wrote:

From: Orit Wasserman<[email protected]>

---
  arch/x86/kvm/vmx.c | 1173 ++++++++++++++++++++++++++++++++++++++++++++++++++--
  1 files changed, 1148 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx.c +1 -0
index 6a4c252..e814029 100644
@@ -209,6 +209,7 @@ struct __attribute__ ((__packed__)) level_state {
  	struct vmcs *vmcs;
  	int cpu;
  	int launched;
+	bool first_launch;
  };
 
  struct nested_vmx {

arch/x86/kvm/vmx.c struct nested_vmx { · line 226

Quoted 10 lines
arch/x86/kvm/vmx.c +6 -0
@@ -216,6 +217,12 @@ struct nested_vmx {
  	bool vmxon;
  	/* What is the location of the  vmcs l1 keeps for l2? (in level1 gpa) */
  	u64 vmptr;
+	/* Are we running nested guest */
+	bool nested_mode;
+	/* L1 requested VMLAUNCH or VMRESUME but we didn't run L2 yet */
+	bool nested_run_pending;
+	/* flag indicating if there was a valid IDT after exiting from l2 */
+	bool nested_valid_idt;
 

Did you mean valid_idt_vectoring_info?

No need to prefix everything with nested_ inside nested_vmx.

Quoted 9 lines
arch/x86/kvm/vmx.c +8 -0
+void prepare_vmcs_12(struct kvm_vcpu *vcpu)
+{
+	struct shadow_vmcs *l2_shadow_vmcs =
+		get_shadow_vmcs(vcpu);
+
+	l2_shadow_vmcs->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
+	l2_shadow_vmcs->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
+	l2_shadow_vmcs->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2048

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	l2_shadow_vmcs->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
+	l2_shadow_vmcs->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
+	l2_shadow_vmcs->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
+	l2_shadow_vmcs->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
+	l2_shadow_vmcs->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
+
+	l2_shadow_vmcs->tsc_offset = vmcs_read64(TSC_OFFSET);
+	l2_shadow_vmcs->guest_physical_address =
+		vmcs_read64(GUEST_PHYSICAL_ADDRESS);
+	l2_shadow_vmcs->vmcs_link_pointer = vmcs_read64(VMCS_LINK_POINTER);

Physical addresses need translation, no?

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2103

Quoted 4 lines
arch/x86/kvm/vmx.c +3 -0
+	l2_shadow_vmcs->guest_cr0 = vmcs_readl(GUEST_CR0);
+
+	l2_shadow_vmcs->guest_cr4 = vmcs_readl(GUEST_CR4);

We don't allow the guest to modify these, so no need to read them. If you do, you need to remove the bits that we modify.

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2135

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+
+int load_vmcs_common(struct shadow_vmcs *src)
+{
+
+	vmcs_write64(VMCS_LINK_POINTER, src->vmcs_link_pointer);

Why load this?

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2136

Quoted 2 lines
arch/x86/kvm/vmx.c +1 -0
+	vmcs_write64(GUEST_IA32_DEBUGCTL, src->guest_ia32_debugctl);

I think some features there are dangerous.

arch/x86/kvm/vmx.c

Quoted 2 lines
arch/x86/kvm/vmx.c +1 -0
+	vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->vm_entry_msr_load_count);

Need to verify? Also need to validate the loaded MSRs and run them through kvm_set_msr() instead of letting the cpu do it.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#16 lore raw

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Avi Kivity <[email protected]> wrote on 20/10/2009 06:56:39:

Quoted 41 lines

From:

Avi Kivity <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

20/10/2009 06:56

Subject:

Re: [PATCH 5/5] Nested VMX patch 5 implements vmlaunch and vmresume

Quoted 4 lines · On 10/15/2009 11:41 PM, [email protected] wrote:

From: Orit Wasserman<[email protected]>

---
  arch/x86/kvm/vmx.c | 1173 ++++++++++++++++++++++++++++++++++++++

++++++++++++--

Quoted 13 lines
  1 files changed, 1148 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx.c +1 -0
index 6a4c252..e814029 100644
@@ -209,6 +209,7 @@ struct __attribute__ ((__packed__)) level_state {
     struct vmcs *vmcs;
     int cpu;
     int launched;
+   bool first_launch;
  };
 
Quoted 11 lines

arch/x86/kvm/vmx.c struct nested_vmx { · line 218

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -0
  struct nested_vmx {
@@ -216,6 +217,12 @@ struct nested_vmx {
     bool vmxon;
     /* What is the location of the  vmcs l1 keeps for l2? (in

level1 gpa) */

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
     u64 vmptr;
+   /* Are we running nested guest */
+   bool nested_mode;
+   /* L1 requested VMLAUNCH or VMRESUME but we didn't run L2 yet */
+   bool nested_run_pending;
+   /* flag indicating if there was a valid IDT after exiting from l2

*/

Quoted 4 lines

arch/x86/kvm/vmx.c struct nested_vmx { · line 226

Quoted 2 lines
arch/x86/kvm/vmx.c +1 -0
+   bool nested_valid_idt;
 

Did you mean valid_idt_vectoring_info?

yes.

Quoted 2 lines

No need to prefix everything with nested_ inside nested_vmx.

OK.

Quoted 13 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void)

Quoted 13 lines
arch/x86/kvm/vmx.c +12 -0
+void prepare_vmcs_12(struct kvm_vcpu *vcpu)
+{
+   struct shadow_vmcs *l2_shadow_vmcs =
+      get_shadow_vmcs(vcpu);
+
+   l2_shadow_vmcs->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
+   l2_shadow_vmcs->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
+   l2_shadow_vmcs->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
+   l2_shadow_vmcs->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
+   l2_shadow_vmcs->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
+   l2_shadow_vmcs->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
+   l2_shadow_vmcs->guest_ldtr_selector = vmcs_read16

(GUEST_LDTR_SELECTOR);

Quoted 9 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2048

Quoted 7 lines
arch/x86/kvm/vmx.c +6 -0
+   l2_shadow_vmcs->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
+
+   l2_shadow_vmcs->tsc_offset = vmcs_read64(TSC_OFFSET);
+   l2_shadow_vmcs->guest_physical_address =
+      vmcs_read64(GUEST_PHYSICAL_ADDRESS);
+   l2_shadow_vmcs->vmcs_link_pointer = vmcs_read64(VMCS_LINK_POINTER);

Physical addresses need translation, no?

If you are referring to GUEST_PHYSICAL_ADDRESS than there is no need for translation for L1.
It need to stay in L2 physical address.

Quoted 8 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2103

Quoted 5 lines
arch/x86/kvm/vmx.c +3 -0
+   l2_shadow_vmcs->guest_cr0 = vmcs_readl(GUEST_CR0);
+
+   l2_shadow_vmcs->guest_cr4 = vmcs_readl(GUEST_CR4);

We don't allow the guest to modify these, so no need to read them. If you do, you need to remove the bits that we modify.

You are correct for example CR0.TS , it will be fixed in the next set of patches.

Quoted 9 lines

arch/x86/kvm/vmx.c static void vmclear_local_vcpus(void) · line 2135

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -0
+
+int load_vmcs_common(struct shadow_vmcs *src)
+{
+
+   vmcs_write64(VMCS_LINK_POINTER, src->vmcs_link_pointer);

Why load this?

At the moment it is not used , maybe in the future. I can add a check to see if it was changed.

Quoted 5 lines
Quoted 3 lines

+ vmcs_write64(GUEST_IA32_DEBUGCTL, src->guest_ia32_debugctl);

I think some features there are dangerous.

I will look into it.

Quoted 2 lines
Quoted 2 lines

+ vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, src->

vm_entry_msr_load_count);

Quoted 4 lines
Quoted 1 line

Need to verify? Also need to validate the loaded MSRs and run them through kvm_set_msr() instead of letting the cpu do it.

I will add the checks.

Quoted 5 lines

--
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.

#17 lore raw

Re: [PATCH 4/5] Nested VMX patch 4 implements vmread and vmwrite

In reply to [email protected]
Quoted 455 lines · On Thu, Oct 15, 2009 at 04:41:45PM +0200, [email protected] wrote:

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c |  591 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 589 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +427 -0
index 8c186e0..6a4c252 100644
@@ -225,6 +225,21 @@ struct nested_vmx {
 	struct level_state *l1_state;
 };
 
+enum vmcs_field_type {
+	VMCS_FIELD_TYPE_U16 = 0,
+	VMCS_FIELD_TYPE_U64 = 1,
+	VMCS_FIELD_TYPE_U32 = 2,
+	VMCS_FIELD_TYPE_ULONG = 3
+};
+
+#define VMCS_FIELD_LENGTH_OFFSET 13
+#define VMCS_FIELD_LENGTH_MASK 0x6000
+
+static inline int vmcs_field_length(unsigned long field)
+{
+	return (VMCS_FIELD_LENGTH_MASK & field) >> 13;
+}
+
 struct vmcs {
 	u32 revision_id;
 	u32 abort;
@@ -288,6 +303,404 @@ static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
 	return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+#define SHADOW_VMCS_OFFSET(x) offsetof(struct shadow_vmcs, x)
+
+static unsigned short vmcs_field_to_offset_table[HOST_RIP+1] = {
+
+	[VIRTUAL_PROCESSOR_ID] =
+		SHADOW_VMCS_OFFSET(virtual_processor_id),
+	[GUEST_ES_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_es_selector),
+	[GUEST_CS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_cs_selector),
+	[GUEST_SS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_ss_selector),
+	[GUEST_DS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_ds_selector),
+	[GUEST_FS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_fs_selector),
+	[GUEST_GS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_gs_selector),
+	[GUEST_LDTR_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_ldtr_selector),
+	[GUEST_TR_SELECTOR] =
+		SHADOW_VMCS_OFFSET(guest_tr_selector),
+	[HOST_ES_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_es_selector),
+	[HOST_CS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_cs_selector),
+	[HOST_SS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_ss_selector),
+	[HOST_DS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_ds_selector),
+	[HOST_FS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_fs_selector),
+	[HOST_GS_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_gs_selector),
+	[HOST_TR_SELECTOR] =
+		SHADOW_VMCS_OFFSET(host_tr_selector),
+	[IO_BITMAP_A] =
+		SHADOW_VMCS_OFFSET(io_bitmap_a),
+	[IO_BITMAP_A_HIGH] =
+		SHADOW_VMCS_OFFSET(io_bitmap_a)+4,
+	[IO_BITMAP_B] =
+		SHADOW_VMCS_OFFSET(io_bitmap_b),
+	[IO_BITMAP_B_HIGH] =
+		SHADOW_VMCS_OFFSET(io_bitmap_b)+4,
+	[MSR_BITMAP] =
+		SHADOW_VMCS_OFFSET(msr_bitmap),
+	[MSR_BITMAP_HIGH] =
+		SHADOW_VMCS_OFFSET(msr_bitmap)+4,
+	[VM_EXIT_MSR_STORE_ADDR] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_store_addr),
+	[VM_EXIT_MSR_STORE_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_store_addr)+4,
+	[VM_EXIT_MSR_LOAD_ADDR] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_load_addr),
+	[VM_EXIT_MSR_LOAD_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_load_addr)+4,
+	[VM_ENTRY_MSR_LOAD_ADDR] =
+		SHADOW_VMCS_OFFSET(vm_entry_msr_load_addr),
+	[VM_ENTRY_MSR_LOAD_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(vm_entry_msr_load_addr)+4,
+	[TSC_OFFSET] =
+		SHADOW_VMCS_OFFSET(tsc_offset),
+	[TSC_OFFSET_HIGH] =
+		SHADOW_VMCS_OFFSET(tsc_offset)+4,
+	[VIRTUAL_APIC_PAGE_ADDR] =
+		SHADOW_VMCS_OFFSET(virtual_apic_page_addr),
+	[VIRTUAL_APIC_PAGE_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(virtual_apic_page_addr)+4,
+	[APIC_ACCESS_ADDR] =
+		SHADOW_VMCS_OFFSET(apic_access_addr),
+	[APIC_ACCESS_ADDR_HIGH] =
+		SHADOW_VMCS_OFFSET(apic_access_addr)+4,
+	[EPT_POINTER] =
+		SHADOW_VMCS_OFFSET(ept_pointer),
+	[EPT_POINTER_HIGH] =
+		SHADOW_VMCS_OFFSET(ept_pointer)+4,
+	[GUEST_PHYSICAL_ADDRESS] =
+		SHADOW_VMCS_OFFSET(guest_physical_address),
+	[GUEST_PHYSICAL_ADDRESS_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_physical_address)+4,
+	[VMCS_LINK_POINTER] =
+		SHADOW_VMCS_OFFSET(vmcs_link_pointer),
+	[VMCS_LINK_POINTER_HIGH] =
+		SHADOW_VMCS_OFFSET(vmcs_link_pointer)+4,
+	[GUEST_IA32_DEBUGCTL] =
+		SHADOW_VMCS_OFFSET(guest_ia32_debugctl),
+	[GUEST_IA32_DEBUGCTL_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_ia32_debugctl)+4,
+	[GUEST_IA32_PAT] =
+		SHADOW_VMCS_OFFSET(guest_ia32_pat),
+	[GUEST_IA32_PAT_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_ia32_pat)+4,
+	[GUEST_PDPTR0] =
+		SHADOW_VMCS_OFFSET(guest_pdptr0),
+	[GUEST_PDPTR0_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_pdptr0)+4,
+	[GUEST_PDPTR1] =
+		SHADOW_VMCS_OFFSET(guest_pdptr1),
+	[GUEST_PDPTR1_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_pdptr1)+4,
+	[GUEST_PDPTR2] =
+		SHADOW_VMCS_OFFSET(guest_pdptr2),
+	[GUEST_PDPTR2_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_pdptr2)+4,
+	[GUEST_PDPTR3] =
+		SHADOW_VMCS_OFFSET(guest_pdptr3),
+	[GUEST_PDPTR3_HIGH] =
+		SHADOW_VMCS_OFFSET(guest_pdptr3)+4,
+	[HOST_IA32_PAT] =
+		SHADOW_VMCS_OFFSET(host_ia32_pat),
+	[HOST_IA32_PAT_HIGH] =
+		SHADOW_VMCS_OFFSET(host_ia32_pat)+4,
+	[PIN_BASED_VM_EXEC_CONTROL] =
+		SHADOW_VMCS_OFFSET(pin_based_vm_exec_control),
+	[CPU_BASED_VM_EXEC_CONTROL] =
+		SHADOW_VMCS_OFFSET(cpu_based_vm_exec_control),
+	[EXCEPTION_BITMAP] =
+		SHADOW_VMCS_OFFSET(exception_bitmap),
+	[PAGE_FAULT_ERROR_CODE_MASK] =
+		SHADOW_VMCS_OFFSET(page_fault_error_code_mask),
+	[PAGE_FAULT_ERROR_CODE_MATCH] =
+		SHADOW_VMCS_OFFSET(page_fault_error_code_match),
+	[CR3_TARGET_COUNT] =
+		SHADOW_VMCS_OFFSET(cr3_target_count),
+	[VM_EXIT_CONTROLS] =
+		SHADOW_VMCS_OFFSET(vm_exit_controls),
+	[VM_EXIT_MSR_STORE_COUNT] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_store_count),
+	[VM_EXIT_MSR_LOAD_COUNT] =
+		SHADOW_VMCS_OFFSET(vm_exit_msr_load_count),
+	[VM_ENTRY_CONTROLS] =
+		SHADOW_VMCS_OFFSET(vm_entry_controls),
+	[VM_ENTRY_MSR_LOAD_COUNT] =
+		SHADOW_VMCS_OFFSET(vm_entry_msr_load_count),
+	[VM_ENTRY_INTR_INFO_FIELD] =
+		SHADOW_VMCS_OFFSET(vm_entry_intr_info_field),
+	[VM_ENTRY_EXCEPTION_ERROR_CODE] =
+		SHADOW_VMCS_OFFSET(vm_entry_exception_error_code),
+	[VM_ENTRY_INSTRUCTION_LEN] =
+		SHADOW_VMCS_OFFSET(vm_entry_instruction_len),
+	[TPR_THRESHOLD] =
+		SHADOW_VMCS_OFFSET(tpr_threshold),
+	[SECONDARY_VM_EXEC_CONTROL] =
+		SHADOW_VMCS_OFFSET(secondary_vm_exec_control),
+	[VM_INSTRUCTION_ERROR] =
+		SHADOW_VMCS_OFFSET(vm_instruction_error),
+	[VM_EXIT_REASON] =
+		SHADOW_VMCS_OFFSET(vm_exit_reason),
+	[VM_EXIT_INTR_INFO] =
+		SHADOW_VMCS_OFFSET(vm_exit_intr_info),
+	[VM_EXIT_INTR_ERROR_CODE] =
+		SHADOW_VMCS_OFFSET(vm_exit_intr_error_code),
+	[IDT_VECTORING_INFO_FIELD] =
+		SHADOW_VMCS_OFFSET(idt_vectoring_info_field),
+	[IDT_VECTORING_ERROR_CODE] =
+		SHADOW_VMCS_OFFSET(idt_vectoring_error_code),
+	[VM_EXIT_INSTRUCTION_LEN] =
+		SHADOW_VMCS_OFFSET(vm_exit_instruction_len),
+	[VMX_INSTRUCTION_INFO] =
+		SHADOW_VMCS_OFFSET(vmx_instruction_info),
+	[GUEST_ES_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_es_limit),
+	[GUEST_CS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_cs_limit),
+	[GUEST_SS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_ss_limit),
+	[GUEST_DS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_ds_limit),
+	[GUEST_FS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_fs_limit),
+	[GUEST_GS_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_gs_limit),
+	[GUEST_LDTR_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_ldtr_limit),
+	[GUEST_TR_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_tr_limit),
+	[GUEST_GDTR_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_gdtr_limit),
+	[GUEST_IDTR_LIMIT] =
+		SHADOW_VMCS_OFFSET(guest_idtr_limit),
+	[GUEST_ES_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_es_ar_bytes),
+	[GUEST_CS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_cs_ar_bytes),
+	[GUEST_SS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_ss_ar_bytes),
+	[GUEST_DS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_ds_ar_bytes),
+	[GUEST_FS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_fs_ar_bytes),
+	[GUEST_GS_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_gs_ar_bytes),
+	[GUEST_LDTR_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_ldtr_ar_bytes),
+	[GUEST_TR_AR_BYTES] =
+		SHADOW_VMCS_OFFSET(guest_tr_ar_bytes),
+	[GUEST_INTERRUPTIBILITY_INFO] =
+		SHADOW_VMCS_OFFSET(guest_interruptibility_info),
+	[GUEST_ACTIVITY_STATE] =
+		SHADOW_VMCS_OFFSET(guest_activity_state),
+	[GUEST_SYSENTER_CS] =
+		SHADOW_VMCS_OFFSET(guest_sysenter_cs),
+	[HOST_IA32_SYSENTER_CS] =
+		SHADOW_VMCS_OFFSET(host_ia32_sysenter_cs),
+	[CR0_GUEST_HOST_MASK] =
+		SHADOW_VMCS_OFFSET(cr0_guest_host_mask),
+	[CR4_GUEST_HOST_MASK] =
+		SHADOW_VMCS_OFFSET(cr4_guest_host_mask),
+	[CR0_READ_SHADOW] =
+		SHADOW_VMCS_OFFSET(cr0_read_shadow),
+	[CR4_READ_SHADOW] =
+		SHADOW_VMCS_OFFSET(cr4_read_shadow),
+	[CR3_TARGET_VALUE0] =
+		SHADOW_VMCS_OFFSET(cr3_target_value0),
+	[CR3_TARGET_VALUE1] =
+		SHADOW_VMCS_OFFSET(cr3_target_value1),
+	[CR3_TARGET_VALUE2] =
+		SHADOW_VMCS_OFFSET(cr3_target_value2),
+	[CR3_TARGET_VALUE3] =
+		SHADOW_VMCS_OFFSET(cr3_target_value3),
+	[EXIT_QUALIFICATION] =
+		SHADOW_VMCS_OFFSET(exit_qualification),
+	[GUEST_LINEAR_ADDRESS] =
+		SHADOW_VMCS_OFFSET(guest_linear_address),
+	[GUEST_CR0] =
+		SHADOW_VMCS_OFFSET(guest_cr0),
+	[GUEST_CR3] =
+		SHADOW_VMCS_OFFSET(guest_cr3),
+	[GUEST_CR4] =
+		SHADOW_VMCS_OFFSET(guest_cr4),
+	[GUEST_ES_BASE] =
+		SHADOW_VMCS_OFFSET(guest_es_base),
+	[GUEST_CS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_cs_base),
+	[GUEST_SS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_ss_base),
+	[GUEST_DS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_ds_base),
+	[GUEST_FS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_fs_base),
+	[GUEST_GS_BASE] =
+		SHADOW_VMCS_OFFSET(guest_gs_base),
+	[GUEST_LDTR_BASE] =
+		SHADOW_VMCS_OFFSET(guest_ldtr_base),
+	[GUEST_TR_BASE] =
+		SHADOW_VMCS_OFFSET(guest_tr_base),
+	[GUEST_GDTR_BASE] =
+		SHADOW_VMCS_OFFSET(guest_gdtr_base),
+	[GUEST_IDTR_BASE] =
+		SHADOW_VMCS_OFFSET(guest_idtr_base),
+	[GUEST_DR7] =
+		SHADOW_VMCS_OFFSET(guest_dr7),
+	[GUEST_RSP] =
+		SHADOW_VMCS_OFFSET(guest_rsp),
+	[GUEST_RIP] =
+		SHADOW_VMCS_OFFSET(guest_rip),
+	[GUEST_RFLAGS] =
+		SHADOW_VMCS_OFFSET(guest_rflags),
+	[GUEST_PENDING_DBG_EXCEPTIONS] =
+		SHADOW_VMCS_OFFSET(guest_pending_dbg_exceptions),
+	[GUEST_SYSENTER_ESP] =
+		SHADOW_VMCS_OFFSET(guest_sysenter_esp),
+	[GUEST_SYSENTER_EIP] =
+		SHADOW_VMCS_OFFSET(guest_sysenter_eip),
+	[HOST_CR0] =
+		SHADOW_VMCS_OFFSET(host_cr0),
+	[HOST_CR3] =
+		SHADOW_VMCS_OFFSET(host_cr3),
+	[HOST_CR4] =
+		SHADOW_VMCS_OFFSET(host_cr4),
+	[HOST_FS_BASE] =
+		SHADOW_VMCS_OFFSET(host_fs_base),
+	[HOST_GS_BASE] =
+		SHADOW_VMCS_OFFSET(host_gs_base),
+	[HOST_TR_BASE] =
+		SHADOW_VMCS_OFFSET(host_tr_base),
+	[HOST_GDTR_BASE] =
+		SHADOW_VMCS_OFFSET(host_gdtr_base),
+	[HOST_IDTR_BASE] =
+		SHADOW_VMCS_OFFSET(host_idtr_base),
+	[HOST_IA32_SYSENTER_ESP] =
+		SHADOW_VMCS_OFFSET(host_ia32_sysenter_esp),
+	[HOST_IA32_SYSENTER_EIP] =
+		SHADOW_VMCS_OFFSET(host_ia32_sysenter_eip),
+	[HOST_RSP] =
+		SHADOW_VMCS_OFFSET(host_rsp),
+	[HOST_RIP] =
+		SHADOW_VMCS_OFFSET(host_rip),
+};
+
+static inline unsigned short vmcs_field_to_offset(unsigned long field)
+{
+
+	if (field > HOST_RIP || vmcs_field_to_offset_table[field] == 0) {
+		printk(KERN_ERR "invalid vmcs encoding 0x%lx\n", field);
+		return -1;
+	}
+
+	return vmcs_field_to_offset_table[field];
+}
+
+static inline unsigned long nested_vmcs_readl(struct kvm_vcpu *vcpu,
+					      unsigned long field)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long *entry;
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return -1;
+	}
+
+	entry = (unsigned long *)((char *)(vmx->nested.l2_state->shadow_vmcs) +
+				 vmcs_field_to_offset(field));
+	return *entry;
+}
+
+static inline u16 nested_vmcs_read16(struct kvm_vcpu *vcpu,
+				     unsigned long field)
+{
+	return nested_vmcs_readl(vcpu, field);
+}
+
+static inline u32 nested_vmcs_read32(struct kvm_vcpu *vcpu, unsigned long field)
+{
+	return nested_vmcs_readl(vcpu, field);
+}
+
+static inline u64 nested_vmcs_read64(struct kvm_vcpu *vcpu, unsigned long field)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	u64 *entry;
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return -1;
+	}
+
+	entry = (u64 *)((char *)(vmx->nested.l2_state->shadow_vmcs) +
+				 vmcs_field_to_offset(field));
+	return *entry;
+}
+
+static inline void nested_vmcs_writel(struct kvm_vcpu *vcpu,
+				      unsigned long field, unsigned long value)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long entry =
+		(unsigned long)(vmx->nested.l2_state->shadow_vmcs);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return;
+	}
+	entry += vmcs_field_to_offset(field);
+	*(unsigned long *)entry = value;
+}
+
+static inline void nested_vmcs_write16(struct kvm_vcpu *vcpu,
+				       unsigned long field, u16 value)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long entry =
+		(unsigned long)(vmx->nested.l2_state->shadow_vmcs);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return;
+	}
+	entry += vmcs_field_to_offset(field);
+	*(u16 *)entry = value;
+}
+
+static inline void nested_vmcs_write32(struct kvm_vcpu *vcpu,
+				       unsigned long field, u32 value)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long entry =
+		(unsigned long)(vmx->nested.l2_state->shadow_vmcs);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+		return;
+	}
+	entry += vmcs_field_to_offset(field);
+	*(u32 *)entry = value;
+}
+
+static inline void nested_vmcs_write64(struct kvm_vcpu *vcpu,
+				       unsigned long field, u64 value)
+{
+#ifdef CONFIG_X86_64
+	nested_vmcs_writel(vcpu, field, value);
+#else /* nested: 32 bit not actually tested */
+	nested_vmcs_writel(vcpu, field, value);
+	nested_vmcs_writel(vcpu, field+1, value >> 32);
+#endif
+}
+
 static struct page *nested_get_page(struct kvm_vcpu *vcpu,
 				    u64 vmcs_addr)
 {
@@ -307,6 +720,50 @@ static struct page *nested_get_page(struct kvm_vcpu *vcpu,
 
 }
 
+static int nested_map_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct page *vmcs_page = nested_get_page(vcpu, vmx->nested.vmptr);
+
+	if (vmcs_page == NULL) {
+		printk(KERN_INFO "%s: failure in nested_get_page\n",__func__);
+		return 0;
+	}
+
+	if (vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_INFO "%s: shadow vmcs already mapped\n",__func__);
+		return 0;
+	}

arch/x86/kvm/vmx.c static struct page *nested_get_page(struct kvm_vcpu *vcpu, · line 746

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+
+	vmx->nested.l2_state->shadow_vmcs = kmap_atomic(vmcs_page, KM_USER0);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_INFO "%s: error in kmap_atomic\n",__func__);
+		return 0;
+	}
+
+	return 1;
+}

Cleanup after error is non-existent in this function.

Quoted 51 lines
arch/x86/kvm/vmx.c +40 -0
+
+static void nested_unmap_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+	struct page *page;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk("Shadow vmcs already unmapped\n");
+		return;
+	}
+
+	page = kmap_atomic_to_page(vmx->nested.l2_state->shadow_vmcs);
+
+	kunmap_atomic(vmx->nested.l2_state->shadow_vmcs, KM_USER0);
+
+	kvm_release_page_dirty(page);
+
+	vmx->nested.l2_state->shadow_vmcs = NULL;
+}
+
 static int init_rmode(struct kvm *kvm);
 static u64 construct_eptp(unsigned long root_hpa);
 
@@ -3550,6 +4007,26 @@ static void clear_rflags_cf_zf(struct kvm_vcpu *vcpu)
 	vmx_set_rflags(vcpu, rflags);
 }
 
+static void set_rflags_to_vmx_fail_invalid(struct kvm_vcpu *vcpu)
+{
+	unsigned long rflags;
+	rflags = vmx_get_rflags(vcpu);
+	rflags |= X86_EFLAGS_CF;
+	rflags &= ~X86_EFLAGS_PF & ~X86_EFLAGS_AF & ~X86_EFLAGS_ZF &
+		~X86_EFLAGS_SF & ~X86_EFLAGS_OF;
+	vmx_set_rflags(vcpu, rflags);
+}
+
+static void set_rflags_to_vmx_fail_valid(struct kvm_vcpu *vcpu)
+{
+	unsigned long rflags;
+	rflags = vmx_get_rflags(vcpu);
+	rflags |= X86_EFLAGS_ZF;
+	rflags &= ~X86_EFLAGS_PF & ~X86_EFLAGS_AF & ~X86_EFLAGS_CF &
+		~X86_EFLAGS_SF & ~X86_EFLAGS_OF;
+	vmx_set_rflags(vcpu, rflags);
+}
+
 static int handle_vmclear(struct kvm_vcpu *vcpu)
 {
 	if (!nested_vmx_check_permission(vcpu))

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4047

Quoted 8 lines
arch/x86/kvm/vmx.c +5 -0
@@ -3563,6 +4040,116 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_vmread(struct kvm_vcpu *vcpu)
+{
+#ifndef CONFIG_X86_64
+	u64 value;
+#endif

Can you move this to where it's used to save #ifdef here?

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4060

Quoted 13 lines
arch/x86/kvm/vmx.c +13 -0
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
+		set_rflags_to_vmx_fail_invalid(vcpu);
+		return 1;
+	}
+
+	switch (vmcs_field_length(vcpu->arch.regs[VCPU_REGS_RDX])) {
+	case VMCS_FIELD_TYPE_U16:
+		vcpu->arch.regs[VCPU_REGS_RAX] =

Once again only vmread %rdx,%rax is implemented. No operand decoding.

Quoted 62 lines
arch/x86/kvm/vmx.c +62 -0
+			nested_vmcs_read16(vcpu,
+					   vcpu->arch.regs[VCPU_REGS_RDX]);
+		break;
+	case VMCS_FIELD_TYPE_U32:
+		vcpu->arch.regs[VCPU_REGS_RAX] =
+			nested_vmcs_read32(vcpu,
+					   vcpu->arch.regs[VCPU_REGS_RDX]);
+		break;
+	case VMCS_FIELD_TYPE_U64:
+#ifdef CONFIG_X86_64
+		vcpu->arch.regs[VCPU_REGS_RAX] =
+		nested_vmcs_read64(vcpu,
+					   vcpu->arch.regs[VCPU_REGS_RDX]);
+#else /* nested: 32 bit not actually tested */
+		value =  nested_vmcs_read64(vcpu,
+					    vcpu->arch.regs[VCPU_REGS_RDX]);
+		vcpu->arch.regs[VCPU_REGS_RAX] = value;
+		vcpu->arch.regs[VCPU_REGS_RBX] = value >> 32;
+#endif
+	break;
+	case VMCS_FIELD_TYPE_ULONG:
+		vcpu->arch.regs[VCPU_REGS_RAX] =
+			nested_vmcs_readl(vcpu,
+					  vcpu->arch.regs[VCPU_REGS_RDX]);
+		break;
+	default:
+		printk(KERN_INFO "%s invalid field\n", __func__);
+		set_rflags_to_vmx_fail_valid(vcpu);
+		vmcs_write32(VM_INSTRUCTION_ERROR, 12);
+		nested_unmap_shadow_vmcs(vcpu);
+		return 1;
+	}
+
+	clear_rflags_cf_zf(vcpu);
+	skip_emulated_instruction(vcpu);
+	nested_unmap_shadow_vmcs(vcpu);
+	return 1;
+}
+
+static int handle_vmwrite(struct kvm_vcpu *vcpu)
+{
+#ifndef CONFIG_X86_64
+	u64 value ;
+#endif
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
+		set_rflags_to_vmx_fail_invalid(vcpu);
+		return 1;
+	}
+
+	switch (vmcs_field_length(vcpu->arch.regs[VCPU_REGS_RDX])) {
+	case VMCS_FIELD_TYPE_U16:
+		nested_vmcs_write16(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+				    vcpu->arch.regs[VCPU_REGS_RAX]);
+		break;
+	case VMCS_FIELD_TYPE_U32:
+		nested_vmcs_write32(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+				    vcpu->arch.regs[VCPU_REGS_RAX]);

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4132

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+		break;
+	case VMCS_FIELD_TYPE_U64:
+#ifdef CONFIG_X86_64
+		nested_vmcs_write64(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+				    vcpu->arch.regs[VCPU_REGS_RAX]);
+#else /* nested: 32 bit not actually tested */
+		value =  vcpu->arch.regs[VCPU_REGS_RAX] |
+			(vcpu->arch.regs[VCPU_REGS_RBX] << 32);
+		nested_vmcs_write64(vcpu,
+				    vcpu->arch.regs[VCPU_REGS_RDX], value);

Why not open code part of nested_vmcs_write64 and get rid of #ifdef there?:

	nested_vmcs_writel(vcpu, vcpu->arch.regs[VCPU_REGS_RDX], vcpu->arch.regs[VCPU_REGS_RAX]);
	nested_vmcs_writel(vcpu, vcpu->arch.regs[VCPU_REGS_RDX]+1, vcpu->arch.regs[VCPU_REGS_RBX]);
Quoted 42 lines
arch/x86/kvm/vmx.c +22 -2
+#endif
+		break;
+	case VMCS_FIELD_TYPE_ULONG:
+		nested_vmcs_writel(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+				   vcpu->arch.regs[VCPU_REGS_RAX]);
+		break;
+	default:
+		printk(KERN_INFO "%s invalid field\n", __func__);
+		set_rflags_to_vmx_fail_valid(vcpu);
+		vmcs_write32(VM_INSTRUCTION_ERROR, 12);
+		nested_unmap_shadow_vmcs(vcpu);
+		return 1;
+	}
+
+	clear_rflags_cf_zf(vcpu);
+	skip_emulated_instruction(vcpu);
+	nested_unmap_shadow_vmcs(vcpu);
+	return 1;
+}
+
 static int handle_vmoff(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -3950,9 +4537,9 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
 	[EXIT_REASON_VMPTRLD]                 = handle_vmptrld,
 	[EXIT_REASON_VMPTRST]                 = handle_vmptrst,
-	[EXIT_REASON_VMREAD]                  = handle_vmx_insn,
+	[EXIT_REASON_VMREAD]                  = handle_vmread,
 	[EXIT_REASON_VMRESUME]                = handle_vmx_insn,
-	[EXIT_REASON_VMWRITE]                 = handle_vmx_insn,
+	[EXIT_REASON_VMWRITE]                 = handle_vmwrite,
 	[EXIT_REASON_VMOFF]                   = handle_vmoff,
 	[EXIT_REASON_VMON]                    = handle_vmon,
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
Signature · 6 lines
1.6.0.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
Gleb.

#18 lore raw

Re: [PATCH 4/5] Nested VMX patch 4 implements vmread and vmwrite

Gleb Natapov <[email protected]> wrote on 19/10/2009 15:17:20:

Quoted 366 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

19/10/2009 15:17

Subject:

Re: [PATCH 4/5] Nested VMX patch 4 implements vmread and vmwrite

Quoted 4 lines · On Thu, Oct 15, 2009 at 04:41:45PM +0200, [email protected] wrote:

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c |  591 +++++++++++++++++++++++++++++++++++++++

++++++++++++-

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct

Quoted 29 lines
 1 files changed, 589 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +15 -0
index 8c186e0..6a4c252 100644
@@ -225,6 +225,21 @@ struct nested_vmx {
    struct level_state *l1_state;
 };
 
+enum vmcs_field_type {
+   VMCS_FIELD_TYPE_U16 = 0,
+   VMCS_FIELD_TYPE_U64 = 1,
+   VMCS_FIELD_TYPE_U32 = 2,
+   VMCS_FIELD_TYPE_ULONG = 3
+};
+
+#define VMCS_FIELD_LENGTH_OFFSET 13
+#define VMCS_FIELD_LENGTH_MASK 0x6000
+
+static inline int vmcs_field_length(unsigned long field)
+{
+   return (VMCS_FIELD_LENGTH_MASK & field) >> 13;
+}
+
 struct vmcs {
    u32 revision_id;
    u32 abort;
@@ -288,6 +303,404 @@ static inline struct vcpu_vmx *to_vmx(struct

kvm_vcpu *vcpu)

Quoted 308 lines
arch/x86/kvm/vmx.c +305 -0
    return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+#define SHADOW_VMCS_OFFSET(x) offsetof(struct shadow_vmcs, x)
+
+static unsigned short vmcs_field_to_offset_table[HOST_RIP+1] = {
+
+   [VIRTUAL_PROCESSOR_ID] =
+      SHADOW_VMCS_OFFSET(virtual_processor_id),
+   [GUEST_ES_SELECTOR] =
+      SHADOW_VMCS_OFFSET(guest_es_selector),
+   [GUEST_CS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(guest_cs_selector),
+   [GUEST_SS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(guest_ss_selector),
+   [GUEST_DS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(guest_ds_selector),
+   [GUEST_FS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(guest_fs_selector),
+   [GUEST_GS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(guest_gs_selector),
+   [GUEST_LDTR_SELECTOR] =
+      SHADOW_VMCS_OFFSET(guest_ldtr_selector),
+   [GUEST_TR_SELECTOR] =
+      SHADOW_VMCS_OFFSET(guest_tr_selector),
+   [HOST_ES_SELECTOR] =
+      SHADOW_VMCS_OFFSET(host_es_selector),
+   [HOST_CS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(host_cs_selector),
+   [HOST_SS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(host_ss_selector),
+   [HOST_DS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(host_ds_selector),
+   [HOST_FS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(host_fs_selector),
+   [HOST_GS_SELECTOR] =
+      SHADOW_VMCS_OFFSET(host_gs_selector),
+   [HOST_TR_SELECTOR] =
+      SHADOW_VMCS_OFFSET(host_tr_selector),
+   [IO_BITMAP_A] =
+      SHADOW_VMCS_OFFSET(io_bitmap_a),
+   [IO_BITMAP_A_HIGH] =
+      SHADOW_VMCS_OFFSET(io_bitmap_a)+4,
+   [IO_BITMAP_B] =
+      SHADOW_VMCS_OFFSET(io_bitmap_b),
+   [IO_BITMAP_B_HIGH] =
+      SHADOW_VMCS_OFFSET(io_bitmap_b)+4,
+   [MSR_BITMAP] =
+      SHADOW_VMCS_OFFSET(msr_bitmap),
+   [MSR_BITMAP_HIGH] =
+      SHADOW_VMCS_OFFSET(msr_bitmap)+4,
+   [VM_EXIT_MSR_STORE_ADDR] =
+      SHADOW_VMCS_OFFSET(vm_exit_msr_store_addr),
+   [VM_EXIT_MSR_STORE_ADDR_HIGH] =
+      SHADOW_VMCS_OFFSET(vm_exit_msr_store_addr)+4,
+   [VM_EXIT_MSR_LOAD_ADDR] =
+      SHADOW_VMCS_OFFSET(vm_exit_msr_load_addr),
+   [VM_EXIT_MSR_LOAD_ADDR_HIGH] =
+      SHADOW_VMCS_OFFSET(vm_exit_msr_load_addr)+4,
+   [VM_ENTRY_MSR_LOAD_ADDR] =
+      SHADOW_VMCS_OFFSET(vm_entry_msr_load_addr),
+   [VM_ENTRY_MSR_LOAD_ADDR_HIGH] =
+      SHADOW_VMCS_OFFSET(vm_entry_msr_load_addr)+4,
+   [TSC_OFFSET] =
+      SHADOW_VMCS_OFFSET(tsc_offset),
+   [TSC_OFFSET_HIGH] =
+      SHADOW_VMCS_OFFSET(tsc_offset)+4,
+   [VIRTUAL_APIC_PAGE_ADDR] =
+      SHADOW_VMCS_OFFSET(virtual_apic_page_addr),
+   [VIRTUAL_APIC_PAGE_ADDR_HIGH] =
+      SHADOW_VMCS_OFFSET(virtual_apic_page_addr)+4,
+   [APIC_ACCESS_ADDR] =
+      SHADOW_VMCS_OFFSET(apic_access_addr),
+   [APIC_ACCESS_ADDR_HIGH] =
+      SHADOW_VMCS_OFFSET(apic_access_addr)+4,
+   [EPT_POINTER] =
+      SHADOW_VMCS_OFFSET(ept_pointer),
+   [EPT_POINTER_HIGH] =
+      SHADOW_VMCS_OFFSET(ept_pointer)+4,
+   [GUEST_PHYSICAL_ADDRESS] =
+      SHADOW_VMCS_OFFSET(guest_physical_address),
+   [GUEST_PHYSICAL_ADDRESS_HIGH] =
+      SHADOW_VMCS_OFFSET(guest_physical_address)+4,
+   [VMCS_LINK_POINTER] =
+      SHADOW_VMCS_OFFSET(vmcs_link_pointer),
+   [VMCS_LINK_POINTER_HIGH] =
+      SHADOW_VMCS_OFFSET(vmcs_link_pointer)+4,
+   [GUEST_IA32_DEBUGCTL] =
+      SHADOW_VMCS_OFFSET(guest_ia32_debugctl),
+   [GUEST_IA32_DEBUGCTL_HIGH] =
+      SHADOW_VMCS_OFFSET(guest_ia32_debugctl)+4,
+   [GUEST_IA32_PAT] =
+      SHADOW_VMCS_OFFSET(guest_ia32_pat),
+   [GUEST_IA32_PAT_HIGH] =
+      SHADOW_VMCS_OFFSET(guest_ia32_pat)+4,
+   [GUEST_PDPTR0] =
+      SHADOW_VMCS_OFFSET(guest_pdptr0),
+   [GUEST_PDPTR0_HIGH] =
+      SHADOW_VMCS_OFFSET(guest_pdptr0)+4,
+   [GUEST_PDPTR1] =
+      SHADOW_VMCS_OFFSET(guest_pdptr1),
+   [GUEST_PDPTR1_HIGH] =
+      SHADOW_VMCS_OFFSET(guest_pdptr1)+4,
+   [GUEST_PDPTR2] =
+      SHADOW_VMCS_OFFSET(guest_pdptr2),
+   [GUEST_PDPTR2_HIGH] =
+      SHADOW_VMCS_OFFSET(guest_pdptr2)+4,
+   [GUEST_PDPTR3] =
+      SHADOW_VMCS_OFFSET(guest_pdptr3),
+   [GUEST_PDPTR3_HIGH] =
+      SHADOW_VMCS_OFFSET(guest_pdptr3)+4,
+   [HOST_IA32_PAT] =
+      SHADOW_VMCS_OFFSET(host_ia32_pat),
+   [HOST_IA32_PAT_HIGH] =
+      SHADOW_VMCS_OFFSET(host_ia32_pat)+4,
+   [PIN_BASED_VM_EXEC_CONTROL] =
+      SHADOW_VMCS_OFFSET(pin_based_vm_exec_control),
+   [CPU_BASED_VM_EXEC_CONTROL] =
+      SHADOW_VMCS_OFFSET(cpu_based_vm_exec_control),
+   [EXCEPTION_BITMAP] =
+      SHADOW_VMCS_OFFSET(exception_bitmap),
+   [PAGE_FAULT_ERROR_CODE_MASK] =
+      SHADOW_VMCS_OFFSET(page_fault_error_code_mask),
+   [PAGE_FAULT_ERROR_CODE_MATCH] =
+      SHADOW_VMCS_OFFSET(page_fault_error_code_match),
+   [CR3_TARGET_COUNT] =
+      SHADOW_VMCS_OFFSET(cr3_target_count),
+   [VM_EXIT_CONTROLS] =
+      SHADOW_VMCS_OFFSET(vm_exit_controls),
+   [VM_EXIT_MSR_STORE_COUNT] =
+      SHADOW_VMCS_OFFSET(vm_exit_msr_store_count),
+   [VM_EXIT_MSR_LOAD_COUNT] =
+      SHADOW_VMCS_OFFSET(vm_exit_msr_load_count),
+   [VM_ENTRY_CONTROLS] =
+      SHADOW_VMCS_OFFSET(vm_entry_controls),
+   [VM_ENTRY_MSR_LOAD_COUNT] =
+      SHADOW_VMCS_OFFSET(vm_entry_msr_load_count),
+   [VM_ENTRY_INTR_INFO_FIELD] =
+      SHADOW_VMCS_OFFSET(vm_entry_intr_info_field),
+   [VM_ENTRY_EXCEPTION_ERROR_CODE] =
+      SHADOW_VMCS_OFFSET(vm_entry_exception_error_code),
+   [VM_ENTRY_INSTRUCTION_LEN] =
+      SHADOW_VMCS_OFFSET(vm_entry_instruction_len),
+   [TPR_THRESHOLD] =
+      SHADOW_VMCS_OFFSET(tpr_threshold),
+   [SECONDARY_VM_EXEC_CONTROL] =
+      SHADOW_VMCS_OFFSET(secondary_vm_exec_control),
+   [VM_INSTRUCTION_ERROR] =
+      SHADOW_VMCS_OFFSET(vm_instruction_error),
+   [VM_EXIT_REASON] =
+      SHADOW_VMCS_OFFSET(vm_exit_reason),
+   [VM_EXIT_INTR_INFO] =
+      SHADOW_VMCS_OFFSET(vm_exit_intr_info),
+   [VM_EXIT_INTR_ERROR_CODE] =
+      SHADOW_VMCS_OFFSET(vm_exit_intr_error_code),
+   [IDT_VECTORING_INFO_FIELD] =
+      SHADOW_VMCS_OFFSET(idt_vectoring_info_field),
+   [IDT_VECTORING_ERROR_CODE] =
+      SHADOW_VMCS_OFFSET(idt_vectoring_error_code),
+   [VM_EXIT_INSTRUCTION_LEN] =
+      SHADOW_VMCS_OFFSET(vm_exit_instruction_len),
+   [VMX_INSTRUCTION_INFO] =
+      SHADOW_VMCS_OFFSET(vmx_instruction_info),
+   [GUEST_ES_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_es_limit),
+   [GUEST_CS_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_cs_limit),
+   [GUEST_SS_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_ss_limit),
+   [GUEST_DS_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_ds_limit),
+   [GUEST_FS_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_fs_limit),
+   [GUEST_GS_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_gs_limit),
+   [GUEST_LDTR_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_ldtr_limit),
+   [GUEST_TR_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_tr_limit),
+   [GUEST_GDTR_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_gdtr_limit),
+   [GUEST_IDTR_LIMIT] =
+      SHADOW_VMCS_OFFSET(guest_idtr_limit),
+   [GUEST_ES_AR_BYTES] =
+      SHADOW_VMCS_OFFSET(guest_es_ar_bytes),
+   [GUEST_CS_AR_BYTES] =
+      SHADOW_VMCS_OFFSET(guest_cs_ar_bytes),
+   [GUEST_SS_AR_BYTES] =
+      SHADOW_VMCS_OFFSET(guest_ss_ar_bytes),
+   [GUEST_DS_AR_BYTES] =
+      SHADOW_VMCS_OFFSET(guest_ds_ar_bytes),
+   [GUEST_FS_AR_BYTES] =
+      SHADOW_VMCS_OFFSET(guest_fs_ar_bytes),
+   [GUEST_GS_AR_BYTES] =
+      SHADOW_VMCS_OFFSET(guest_gs_ar_bytes),
+   [GUEST_LDTR_AR_BYTES] =
+      SHADOW_VMCS_OFFSET(guest_ldtr_ar_bytes),
+   [GUEST_TR_AR_BYTES] =
+      SHADOW_VMCS_OFFSET(guest_tr_ar_bytes),
+   [GUEST_INTERRUPTIBILITY_INFO] =
+      SHADOW_VMCS_OFFSET(guest_interruptibility_info),
+   [GUEST_ACTIVITY_STATE] =
+      SHADOW_VMCS_OFFSET(guest_activity_state),
+   [GUEST_SYSENTER_CS] =
+      SHADOW_VMCS_OFFSET(guest_sysenter_cs),
+   [HOST_IA32_SYSENTER_CS] =
+      SHADOW_VMCS_OFFSET(host_ia32_sysenter_cs),
+   [CR0_GUEST_HOST_MASK] =
+      SHADOW_VMCS_OFFSET(cr0_guest_host_mask),
+   [CR4_GUEST_HOST_MASK] =
+      SHADOW_VMCS_OFFSET(cr4_guest_host_mask),
+   [CR0_READ_SHADOW] =
+      SHADOW_VMCS_OFFSET(cr0_read_shadow),
+   [CR4_READ_SHADOW] =
+      SHADOW_VMCS_OFFSET(cr4_read_shadow),
+   [CR3_TARGET_VALUE0] =
+      SHADOW_VMCS_OFFSET(cr3_target_value0),
+   [CR3_TARGET_VALUE1] =
+      SHADOW_VMCS_OFFSET(cr3_target_value1),
+   [CR3_TARGET_VALUE2] =
+      SHADOW_VMCS_OFFSET(cr3_target_value2),
+   [CR3_TARGET_VALUE3] =
+      SHADOW_VMCS_OFFSET(cr3_target_value3),
+   [EXIT_QUALIFICATION] =
+      SHADOW_VMCS_OFFSET(exit_qualification),
+   [GUEST_LINEAR_ADDRESS] =
+      SHADOW_VMCS_OFFSET(guest_linear_address),
+   [GUEST_CR0] =
+      SHADOW_VMCS_OFFSET(guest_cr0),
+   [GUEST_CR3] =
+      SHADOW_VMCS_OFFSET(guest_cr3),
+   [GUEST_CR4] =
+      SHADOW_VMCS_OFFSET(guest_cr4),
+   [GUEST_ES_BASE] =
+      SHADOW_VMCS_OFFSET(guest_es_base),
+   [GUEST_CS_BASE] =
+      SHADOW_VMCS_OFFSET(guest_cs_base),
+   [GUEST_SS_BASE] =
+      SHADOW_VMCS_OFFSET(guest_ss_base),
+   [GUEST_DS_BASE] =
+      SHADOW_VMCS_OFFSET(guest_ds_base),
+   [GUEST_FS_BASE] =
+      SHADOW_VMCS_OFFSET(guest_fs_base),
+   [GUEST_GS_BASE] =
+      SHADOW_VMCS_OFFSET(guest_gs_base),
+   [GUEST_LDTR_BASE] =
+      SHADOW_VMCS_OFFSET(guest_ldtr_base),
+   [GUEST_TR_BASE] =
+      SHADOW_VMCS_OFFSET(guest_tr_base),
+   [GUEST_GDTR_BASE] =
+      SHADOW_VMCS_OFFSET(guest_gdtr_base),
+   [GUEST_IDTR_BASE] =
+      SHADOW_VMCS_OFFSET(guest_idtr_base),
+   [GUEST_DR7] =
+      SHADOW_VMCS_OFFSET(guest_dr7),
+   [GUEST_RSP] =
+      SHADOW_VMCS_OFFSET(guest_rsp),
+   [GUEST_RIP] =
+      SHADOW_VMCS_OFFSET(guest_rip),
+   [GUEST_RFLAGS] =
+      SHADOW_VMCS_OFFSET(guest_rflags),
+   [GUEST_PENDING_DBG_EXCEPTIONS] =
+      SHADOW_VMCS_OFFSET(guest_pending_dbg_exceptions),
+   [GUEST_SYSENTER_ESP] =
+      SHADOW_VMCS_OFFSET(guest_sysenter_esp),
+   [GUEST_SYSENTER_EIP] =
+      SHADOW_VMCS_OFFSET(guest_sysenter_eip),
+   [HOST_CR0] =
+      SHADOW_VMCS_OFFSET(host_cr0),
+   [HOST_CR3] =
+      SHADOW_VMCS_OFFSET(host_cr3),
+   [HOST_CR4] =
+      SHADOW_VMCS_OFFSET(host_cr4),
+   [HOST_FS_BASE] =
+      SHADOW_VMCS_OFFSET(host_fs_base),
+   [HOST_GS_BASE] =
+      SHADOW_VMCS_OFFSET(host_gs_base),
+   [HOST_TR_BASE] =
+      SHADOW_VMCS_OFFSET(host_tr_base),
+   [HOST_GDTR_BASE] =
+      SHADOW_VMCS_OFFSET(host_gdtr_base),
+   [HOST_IDTR_BASE] =
+      SHADOW_VMCS_OFFSET(host_idtr_base),
+   [HOST_IA32_SYSENTER_ESP] =
+      SHADOW_VMCS_OFFSET(host_ia32_sysenter_esp),
+   [HOST_IA32_SYSENTER_EIP] =
+      SHADOW_VMCS_OFFSET(host_ia32_sysenter_eip),
+   [HOST_RSP] =
+      SHADOW_VMCS_OFFSET(host_rsp),
+   [HOST_RIP] =
+      SHADOW_VMCS_OFFSET(host_rip),
+};
+
+static inline unsigned short vmcs_field_to_offset(unsigned long field)
+{
+
+   if (field > HOST_RIP || vmcs_field_to_offset_table[field] == 0) {
+      printk(KERN_ERR "invalid vmcs encoding 0x%lx\n", field);
+      return -1;
+   }
+
+   return vmcs_field_to_offset_table[field];
+}
+
+static inline unsigned long nested_vmcs_readl(struct kvm_vcpu *vcpu,
+                     unsigned long field)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
Quoted 8 lines

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+   unsigned long *entry;
+
+   if (!vmx->nested.l2_state->shadow_vmcs) {
+      printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+      return -1;
+   }
+
+   entry = (unsigned long *)((char *)(vmx->nested.l2_state->

shadow_vmcs) +

Quoted 95 lines

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)

Quoted 11 lines
arch/x86/kvm/vmx.c +11 -0
+             vmcs_field_to_offset(field));
+   return *entry;
+}
+
+static inline u16 nested_vmcs_read16(struct kvm_vcpu *vcpu,
+                 unsigned long field)
+{
+   return nested_vmcs_readl(vcpu, field);
+}
+
+static inline u32 nested_vmcs_read32(struct kvm_vcpu *vcpu,

unsigned long field)

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)

Quoted 5 lines
arch/x86/kvm/vmx.c +5 -0
+{
+   return nested_vmcs_readl(vcpu, field);
+}
+
+static inline u64 nested_vmcs_read64(struct kvm_vcpu *vcpu,

unsigned long field)

arch/x86/kvm/vmx.c static struct page *nested_get_page(struct

Quoted 73 lines
arch/x86/kvm/vmx.c +69 -0
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   u64 *entry;
+   if (!vmx->nested.l2_state->shadow_vmcs) {
+      printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+      return -1;
+   }
+
+   entry = (u64 *)((char *)(vmx->nested.l2_state->shadow_vmcs) +
+             vmcs_field_to_offset(field));
+   return *entry;
+}
+
+static inline void nested_vmcs_writel(struct kvm_vcpu *vcpu,
+                  unsigned long field, unsigned long value)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   unsigned long entry =
+      (unsigned long)(vmx->nested.l2_state->shadow_vmcs);
+
+   if (!vmx->nested.l2_state->shadow_vmcs) {
+      printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+      return;
+   }
+   entry += vmcs_field_to_offset(field);
+   *(unsigned long *)entry = value;
+}
+
+static inline void nested_vmcs_write16(struct kvm_vcpu *vcpu,
+                   unsigned long field, u16 value)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   unsigned long entry =
+      (unsigned long)(vmx->nested.l2_state->shadow_vmcs);
+
+   if (!vmx->nested.l2_state->shadow_vmcs) {
+      printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+      return;
+   }
+   entry += vmcs_field_to_offset(field);
+   *(u16 *)entry = value;
+}
+
+static inline void nested_vmcs_write32(struct kvm_vcpu *vcpu,
+                   unsigned long field, u32 value)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   unsigned long entry =
+      (unsigned long)(vmx->nested.l2_state->shadow_vmcs);
+
+   if (!vmx->nested.l2_state->shadow_vmcs) {
+      printk(KERN_ERR "%s invalid nested vmcs\n", __func__);
+      return;
+   }
+   entry += vmcs_field_to_offset(field);
+   *(u32 *)entry = value;
+}
+
+static inline void nested_vmcs_write64(struct kvm_vcpu *vcpu,
+                   unsigned long field, u64 value)
+{
+#ifdef CONFIG_X86_64
+   nested_vmcs_writel(vcpu, field, value);
+#else /* nested: 32 bit not actually tested */
+   nested_vmcs_writel(vcpu, field, value);
+   nested_vmcs_writel(vcpu, field+1, value >> 32);
+#endif
+}
+
 static struct page *nested_get_page(struct kvm_vcpu *vcpu,
                 u64 vmcs_addr)
 {
@@ -307,6 +720,50 @@ static struct page *nested_get_page(struct

kvm_vcpu *vcpu,

Quoted 3 lines
arch/x86/kvm/vmx.c +0 -0
 }
 
Quoted 16 lines
Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+static int nested_map_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   struct page *vmcs_page = nested_get_page(vcpu, vmx->nested.vmptr);
+
+   if (vmcs_page == NULL) {

arch/x86/kvm/vmx.c static struct page *nested_get_page(struct kvm_vcpu *vcpu,

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+      printk(KERN_INFO "%s: failure in nested_get_page\n",__func__);
+      return 0;
+   }
+
+   if (vmx->nested.l2_state->shadow_vmcs) {
+      printk(KERN_INFO "%s: shadow vmcs already mapped\n",__func__);
+      return 0;
+   }
+
+   vmx->nested.l2_state->shadow_vmcs = kmap_atomic(vmcs_page,

KM_USER0);

Quoted 9 lines

arch/x86/kvm/vmx.c static struct page *nested_get_page(struct kvm_vcpu *vcpu, · line 746

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+
+   if (!vmx->nested.l2_state->shadow_vmcs) {
+      printk(KERN_INFO "%s: error in kmap_atomic\n",__func__);
+      return 0;
+   }
+
+   return 1;
+}

Cleanup after error is non-existent in this function.

I will fix it.

Quoted 24 lines
Quoted 24 lines
arch/x86/kvm/vmx.c +20 -0
+
+static void nested_unmap_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+   struct page *page;
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+   if (!vmx->nested.l2_state->shadow_vmcs) {
+      printk("Shadow vmcs already unmapped\n");
+      return;
+   }
+
+   page = kmap_atomic_to_page(vmx->nested.l2_state->shadow_vmcs);
+
+   kunmap_atomic(vmx->nested.l2_state->shadow_vmcs, KM_USER0);
+
+   kvm_release_page_dirty(page);
+
+   vmx->nested.l2_state->shadow_vmcs = NULL;
+}
+
 static int init_rmode(struct kvm *kvm);
 static u64 construct_eptp(unsigned long root_hpa);
 
Quoted 1 line

arch/x86/kvm/vmx.c static void clear_rflags_cf_zf(struct kvm_vcpu

Quoted 1 line
arch/x86/kvm/vmx.c +0 -0
@@ -3550,6 +4007,26 @@ static void clear_rflags_cf_zf(struct kvm_vcpu

*vcpu)

Quoted 19 lines
Quoted 19 lines
arch/x86/kvm/vmx.c +16 -0
    vmx_set_rflags(vcpu, rflags);
 }
 
+static void set_rflags_to_vmx_fail_invalid(struct kvm_vcpu *vcpu)
+{
+   unsigned long rflags;
+   rflags = vmx_get_rflags(vcpu);
+   rflags |= X86_EFLAGS_CF;
+   rflags &= ~X86_EFLAGS_PF & ~X86_EFLAGS_AF & ~X86_EFLAGS_ZF &
+      ~X86_EFLAGS_SF & ~X86_EFLAGS_OF;
+   vmx_set_rflags(vcpu, rflags);
+}
+
+static void set_rflags_to_vmx_fail_valid(struct kvm_vcpu *vcpu)
+{
+   unsigned long rflags;
+   rflags = vmx_get_rflags(vcpu);
+   rflags |= X86_EFLAGS_ZF;
+   rflags &= ~X86_EFLAGS_PF & ~X86_EFLAGS_AF & ~X86_EFLAGS_CF &
Quoted 8 lines

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu

Quoted 8 lines
arch/x86/kvm/vmx.c +4 -0
+      ~X86_EFLAGS_SF & ~X86_EFLAGS_OF;
+   vmx_set_rflags(vcpu, rflags);
+}
+
 static int handle_vmclear(struct kvm_vcpu *vcpu)
 {
    if (!nested_vmx_check_permission(vcpu))
@@ -3563,6 +4040,116 @@ static int handle_vmclear(struct kvm_vcpu

*vcpu)

Quoted 9 lines

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4047

Quoted 8 lines
arch/x86/kvm/vmx.c +5 -0
    return 1;
 }
 
+static int handle_vmread(struct kvm_vcpu *vcpu)
+{
+#ifndef CONFIG_X86_64
+   u64 value;
+#endif

Can you move this to where it's used to save #ifdef here?

I will fix it.

Quoted 15 lines
Quoted 4 lines
arch/x86/kvm/vmx.c +3 -0
+
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4060

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+
+   if (!nested_map_shadow_vmcs(vcpu)) {
+      printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
+      set_rflags_to_vmx_fail_invalid(vcpu);
+      return 1;
+   }
+
+   switch (vmcs_field_length(vcpu->arch.regs[VCPU_REGS_RDX])) {
+   case VMCS_FIELD_TYPE_U16:
+      vcpu->arch.regs[VCPU_REGS_RAX] =

Once again only vmread %rdx,%rax is implemented. No operand decoding.

Will be added.

Quoted 75 lines
Quoted 63 lines
arch/x86/kvm/vmx.c +62 -0
+         nested_vmcs_read16(vcpu,
+                  vcpu->arch.regs[VCPU_REGS_RDX]);
+      break;
+   case VMCS_FIELD_TYPE_U32:
+      vcpu->arch.regs[VCPU_REGS_RAX] =
+         nested_vmcs_read32(vcpu,
+                  vcpu->arch.regs[VCPU_REGS_RDX]);
+      break;
+   case VMCS_FIELD_TYPE_U64:
+#ifdef CONFIG_X86_64
+      vcpu->arch.regs[VCPU_REGS_RAX] =
+      nested_vmcs_read64(vcpu,
+                  vcpu->arch.regs[VCPU_REGS_RDX]);
+#else /* nested: 32 bit not actually tested */
+      value =  nested_vmcs_read64(vcpu,
+                   vcpu->arch.regs[VCPU_REGS_RDX]);
+      vcpu->arch.regs[VCPU_REGS_RAX] = value;
+      vcpu->arch.regs[VCPU_REGS_RBX] = value >> 32;
+#endif
+   break;
+   case VMCS_FIELD_TYPE_ULONG:
+      vcpu->arch.regs[VCPU_REGS_RAX] =
+         nested_vmcs_readl(vcpu,
+                 vcpu->arch.regs[VCPU_REGS_RDX]);
+      break;
+   default:
+      printk(KERN_INFO "%s invalid field\n", __func__);
+      set_rflags_to_vmx_fail_valid(vcpu);
+      vmcs_write32(VM_INSTRUCTION_ERROR, 12);
+      nested_unmap_shadow_vmcs(vcpu);
+      return 1;
+   }
+
+   clear_rflags_cf_zf(vcpu);
+   skip_emulated_instruction(vcpu);
+   nested_unmap_shadow_vmcs(vcpu);
+   return 1;
+}
+
+static int handle_vmwrite(struct kvm_vcpu *vcpu)
+{
+#ifndef CONFIG_X86_64
+   u64 value ;
+#endif
+
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+
+   if (!nested_map_shadow_vmcs(vcpu)) {
+      printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
+      set_rflags_to_vmx_fail_invalid(vcpu);
+      return 1;
+   }
+
+   switch (vmcs_field_length(vcpu->arch.regs[VCPU_REGS_RDX])) {
+   case VMCS_FIELD_TYPE_U16:
+      nested_vmcs_write16(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+                vcpu->arch.regs[VCPU_REGS_RAX]);
+      break;
+   case VMCS_FIELD_TYPE_U32:
+      nested_vmcs_write32(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+                vcpu->arch.regs[VCPU_REGS_RAX]);

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4132

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+      break;
+   case VMCS_FIELD_TYPE_U64:
+#ifdef CONFIG_X86_64
+      nested_vmcs_write64(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+                vcpu->arch.regs[VCPU_REGS_RAX]);
+#else /* nested: 32 bit not actually tested */
+      value =  vcpu->arch.regs[VCPU_REGS_RAX] |
+         (vcpu->arch.regs[VCPU_REGS_RBX] << 32);
+      nested_vmcs_write64(vcpu,
+                vcpu->arch.regs[VCPU_REGS_RDX], value);

Why not open code part of nested_vmcs_write64 and get rid of #ifdef there?:

Quoted 4 lines
   nested_vmcs_writel(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
vcpu->arch.regs[VCPU_REGS_RAX]);
   nested_vmcs_writel(vcpu, vcpu->arch.regs[VCPU_REGS_RDX]+1,

vcpu->arch.regs[VCPU_REGS_RBX]);

I will change it.

Quoted 47 lines

arch/x86/kvm/vmx.c static int (*kvm_vmx_exit_handlers[])(struct

Quoted 25 lines
arch/x86/kvm/vmx.c +20 -0
+#endif
+      break;
+   case VMCS_FIELD_TYPE_ULONG:
+      nested_vmcs_writel(vcpu, vcpu->arch.regs[VCPU_REGS_RDX],
+               vcpu->arch.regs[VCPU_REGS_RAX]);
+      break;
+   default:
+      printk(KERN_INFO "%s invalid field\n", __func__);
+      set_rflags_to_vmx_fail_valid(vcpu);
+      vmcs_write32(VM_INSTRUCTION_ERROR, 12);
+      nested_unmap_shadow_vmcs(vcpu);
+      return 1;
+   }
+
+   clear_rflags_cf_zf(vcpu);
+   skip_emulated_instruction(vcpu);
+   nested_unmap_shadow_vmcs(vcpu);
+   return 1;
+}
+
 static int handle_vmoff(struct kvm_vcpu *vcpu)
 {
    struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -3950,9 +4537,9 @@ static int (*kvm_vmx_exit_handlers[])(struct

kvm_vcpu *vcpu) = {

Quoted 14 lines
arch/x86/kvm/vmx.c +2 -3
    [EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
    [EXIT_REASON_VMPTRLD]                 = handle_vmptrld,
    [EXIT_REASON_VMPTRST]                 = handle_vmptrst,
-   [EXIT_REASON_VMREAD]                  = handle_vmx_insn,
+   [EXIT_REASON_VMREAD]                  = handle_vmread,
    [EXIT_REASON_VMRESUME]                = handle_vmx_insn,
-   [EXIT_REASON_VMWRITE]                 = handle_vmx_insn,
+   [EXIT_REASON_VMWRITE]                 = handle_vmwrite,
    [EXIT_REASON_VMOFF]                   = handle_vmoff,
    [EXIT_REASON_VMON]                    = handle_vmon,
    [EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
--

1.6.0.4

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
Gleb.

#19 lore raw

Re: [PATCH 4/5] Nested VMX patch 4 implements vmread and vmwrite

In reply to [email protected]
Quoted 7 lines · On 10/15/2009 11:41 PM, [email protected] wrote:
arch/x86/kvm/vmx.c +5 -0
+static int nested_map_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct page *vmcs_page = nested_get_page(vcpu, vmx->nested.vmptr);
+

arch/x86/kvm/vmx.c static struct page *nested_get_page(struct kvm_vcpu *vcpu, · line 737

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	if (vmcs_page == NULL) {
+		printk(KERN_INFO "%s: failure in nested_get_page\n",__func__);
+		return 0;
+	}
+
+	if (vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_INFO "%s: shadow vmcs already mapped\n",__func__);
+		return 0;
+	}
+

Consider dropping shadow_vmcs from l2_state and just passing it everywhere. Less convenient but safer.

arch/x86/kvm/vmx.c static struct page *nested_get_page(struct kvm_vcpu *vcpu, · line 743

Quoted 7 lines
arch/x86/kvm/vmx.c +6 -0
+	vmx->nested.l2_state->shadow_vmcs = kmap_atomic(vmcs_page, KM_USER0);
+
+	if (!vmx->nested.l2_state->shadow_vmcs) {
+		printk(KERN_INFO "%s: error in kmap_atomic\n",__func__);
+		return 0;
+	}

kmap_atomic() can't fail.

Quoted 6 lines
arch/x86/kvm/vmx.c +4 -0
+static int handle_vmread(struct kvm_vcpu *vcpu)
+{
+#ifndef CONFIG_X86_64
+	u64 value;

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4056

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+#endif
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (!nested_map_shadow_vmcs(vcpu)) {
+		printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
+		set_rflags_to_vmx_fail_invalid(vcpu);
+		return 1;
+	}

return an error.

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4063

Quoted 8 lines
arch/x86/kvm/vmx.c +7 -0
+
+	switch (vmcs_field_length(vcpu->arch.regs[VCPU_REGS_RDX])) {
+	case VMCS_FIELD_TYPE_U16:
+		vcpu->arch.regs[VCPU_REGS_RAX] =
+			nested_vmcs_read16(vcpu,
+					   vcpu->arch.regs[VCPU_REGS_RDX]);
+		break;

Use the emulator to decode operands.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#20 lore raw

Re: [PATCH 4/5] Nested VMX patch 4 implements vmread and vmwrite

Avi Kivity <[email protected]> wrote on 20/10/2009 06:44:41:

Quoted 35 lines

From:

Avi Kivity <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

20/10/2009 06:44

Subject:

Re: [PATCH 4/5] Nested VMX patch 4 implements vmread and vmwrite

Quoted 12 lines · On 10/15/2009 11:41 PM, [email protected] wrote:
arch/x86/kvm/vmx.c +11 -0
+static int nested_map_shadow_vmcs(struct kvm_vcpu *vcpu)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   struct page *vmcs_page = nested_get_page(vcpu, vmx->nested.vmptr);
+
+   if (vmcs_page == NULL) {
+      printk(KERN_INFO "%s: failure in nested_get_page\n",__func__);
+      return 0;
+   }
+
+   if (vmx->nested.l2_state->shadow_vmcs) {
Quoted 8 lines

arch/x86/kvm/vmx.c static struct page *nested_get_page(struct kvm_vcpu *vcpu, · line 737

Quoted 5 lines
arch/x86/kvm/vmx.c +4 -0
+      printk(KERN_INFO "%s: shadow vmcs already mapped\n",__func__);
+      return 0;
+   }
+

Consider dropping shadow_vmcs from l2_state and just passing it everywhere. Less convenient but safer.

I will think about it, it is called from many places ...

Quoted 2 lines
Quoted 2 lines

+ vmx->nested.l2_state->shadow_vmcs = kmap_atomic(vmcs_page,

KM_USER0);

Quoted 8 lines

arch/x86/kvm/vmx.c static struct page *nested_get_page(struct kvm_vcpu *vcpu, · line 743

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+
+   if (!vmx->nested.l2_state->shadow_vmcs) {
+      printk(KERN_INFO "%s: error in kmap_atomic\n",__func__);
+      return 0;
+   }

kmap_atomic() can't fail.

I will remove the check.

Quoted 10 lines
Quoted 10 lines
arch/x86/kvm/vmx.c +9 -0
+static int handle_vmread(struct kvm_vcpu *vcpu)
+{
+#ifndef CONFIG_X86_64
+   u64 value;
+#endif
+
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+
Quoted 8 lines

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4056

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+   if (!nested_map_shadow_vmcs(vcpu)) {
+      printk(KERN_INFO "%s invalid shadow vmcs\n", __func__);
+      set_rflags_to_vmx_fail_invalid(vcpu);
+      return 1;
+   }

return an error.

OK.

Quoted 11 lines

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 4063

Quoted 9 lines
arch/x86/kvm/vmx.c +7 -0
+
+   switch (vmcs_field_length(vcpu->arch.regs[VCPU_REGS_RDX])) {
+   case VMCS_FIELD_TYPE_U16:
+      vcpu->arch.regs[VCPU_REGS_RAX] =
+         nested_vmcs_read16(vcpu,
+                  vcpu->arch.regs[VCPU_REGS_RDX]);
+      break;

Use the emulator to decode operands.

OK.

Quoted 6 lines

--
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.

#21 lore raw

Re: [PATCH 3/5] Nested VMX patch 3 implements vmptrld and vmptrst

In reply to [email protected]
Quoted 305 lines · On Thu, Oct 15, 2009 at 04:41:44PM +0200, [email protected] wrote:

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c |  468 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 arch/x86/kvm/x86.c |    3 +-
 2 files changed, 459 insertions(+), 12 deletions(-)
arch/x86/kvm/vmx.c +223 -9
index 411cbdb..8c186e0 100644
@@ -61,20 +61,168 @@ module_param_named(unrestricted_guest,
 static int __read_mostly emulate_invalid_guest_state = 0;
 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
 
+
+struct __attribute__ ((__packed__)) shadow_vmcs {
+	u32 revision_id;
+	u32 abort;
+	u16 virtual_processor_id;
+	u16 guest_es_selector;
+	u16 guest_cs_selector;
+	u16 guest_ss_selector;
+	u16 guest_ds_selector;
+	u16 guest_fs_selector;
+	u16 guest_gs_selector;
+	u16 guest_ldtr_selector;
+	u16 guest_tr_selector;
+	u16 host_es_selector;
+	u16 host_cs_selector;
+	u16 host_ss_selector;
+	u16 host_ds_selector;
+	u16 host_fs_selector;
+	u16 host_gs_selector;
+	u16 host_tr_selector;
+	u64 io_bitmap_a;
+	u64 io_bitmap_b;
+	u64 msr_bitmap;
+	u64 vm_exit_msr_store_addr;
+	u64 vm_exit_msr_load_addr;
+	u64 vm_entry_msr_load_addr;
+	u64 tsc_offset;
+	u64 virtual_apic_page_addr;
+	u64 apic_access_addr;
+	u64 ept_pointer;
+	u64 guest_physical_address;
+	u64 vmcs_link_pointer;
+	u64 guest_ia32_debugctl;
+	u64 guest_ia32_pat;
+	u64 guest_pdptr0;
+	u64 guest_pdptr1;
+	u64 guest_pdptr2;
+	u64 guest_pdptr3;
+	u64 host_ia32_pat;
+	u32 pin_based_vm_exec_control;
+	u32 cpu_based_vm_exec_control;
+	u32 exception_bitmap;
+	u32 page_fault_error_code_mask;
+	u32 page_fault_error_code_match;
+	u32 cr3_target_count;
+	u32 vm_exit_controls;
+	u32 vm_exit_msr_store_count;
+	u32 vm_exit_msr_load_count;
+	u32 vm_entry_controls;
+	u32 vm_entry_msr_load_count;
+	u32 vm_entry_intr_info_field;
+	u32 vm_entry_exception_error_code;
+	u32 vm_entry_instruction_len;
+	u32 tpr_threshold;
+	u32 secondary_vm_exec_control;
+	u32 vm_instruction_error;
+	u32 vm_exit_reason;
+	u32 vm_exit_intr_info;
+	u32 vm_exit_intr_error_code;
+	u32 idt_vectoring_info_field;
+	u32 idt_vectoring_error_code;
+	u32 vm_exit_instruction_len;
+	u32 vmx_instruction_info;
+	u32 guest_es_limit;
+	u32 guest_cs_limit;
+	u32 guest_ss_limit;
+	u32 guest_ds_limit;
+	u32 guest_fs_limit;
+	u32 guest_gs_limit;
+	u32 guest_ldtr_limit;
+	u32 guest_tr_limit;
+	u32 guest_gdtr_limit;
+	u32 guest_idtr_limit;
+	u32 guest_es_ar_bytes;
+	u32 guest_cs_ar_bytes;
+	u32 guest_ss_ar_bytes;
+	u32 guest_ds_ar_bytes;
+	u32 guest_fs_ar_bytes;
+	u32 guest_gs_ar_bytes;
+	u32 guest_ldtr_ar_bytes;
+	u32 guest_tr_ar_bytes;
+	u32 guest_interruptibility_info;
+	u32 guest_activity_state;
+	u32 guest_sysenter_cs;
+	u32 host_ia32_sysenter_cs;
+	unsigned long cr0_guest_host_mask;
+	unsigned long cr4_guest_host_mask;
+	unsigned long cr0_read_shadow;
+	unsigned long cr4_read_shadow;
+	unsigned long cr3_target_value0;
+	unsigned long cr3_target_value1;
+	unsigned long cr3_target_value2;
+	unsigned long cr3_target_value3;
+	unsigned long exit_qualification;
+	unsigned long guest_linear_address;
+	unsigned long guest_cr0;
+	unsigned long guest_cr3;
+	unsigned long guest_cr4;
+	unsigned long guest_es_base;
+	unsigned long guest_cs_base;
+	unsigned long guest_ss_base;
+	unsigned long guest_ds_base;
+	unsigned long guest_fs_base;
+	unsigned long guest_gs_base;
+	unsigned long guest_ldtr_base;
+	unsigned long guest_tr_base;
+	unsigned long guest_gdtr_base;
+	unsigned long guest_idtr_base;
+	unsigned long guest_dr7;
+	unsigned long guest_rsp;
+	unsigned long guest_rip;
+	unsigned long guest_rflags;
+	unsigned long guest_pending_dbg_exceptions;
+	unsigned long guest_sysenter_esp;
+	unsigned long guest_sysenter_eip;
+	unsigned long host_cr0;
+	unsigned long host_cr3;
+	unsigned long host_cr4;
+	unsigned long host_fs_base;
+	unsigned long host_gs_base;
+	unsigned long host_tr_base;
+	unsigned long host_gdtr_base;
+	unsigned long host_idtr_base;
+	unsigned long host_ia32_sysenter_esp;
+	unsigned long host_ia32_sysenter_eip;
+	unsigned long host_rsp;
+	unsigned long host_rip;
+};
+
 struct __attribute__ ((__packed__)) level_state {
 	/* Has the level1 guest done vmclear? */
 	bool vmclear;
+	u16 vpid;
+	u64 shadow_efer;
+	unsigned long cr2;
+	unsigned long cr3;
+	unsigned long cr4;
+	unsigned long cr8;
+
+	u64 io_bitmap_a;
+	u64 io_bitmap_b;
+	u64 msr_bitmap;
+
+	struct shadow_vmcs *shadow_vmcs;
+
+	struct vmcs *vmcs;
+	int cpu;
+	int launched;
 };
 
 struct nested_vmx {
 	/* Has the level1 guest done vmxon? */
 	bool vmxon;
-
+	/* What is the location of the  vmcs l1 keeps for l2? (in level1 gpa) */
+	u64 vmptr;
 	/*
 	 * Level 2 state : includes vmcs,registers and
 	 * a copy of vmcs12 for vmread/vmwrite
 	 */
 	struct level_state *l2_state;
+	/* Level 1 state for switching to level 2 and back */
+	struct level_state *l1_state;
 };
 
 struct vmcs {
@@ -140,6 +288,25 @@ static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
 	return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+static struct page *nested_get_page(struct kvm_vcpu *vcpu,
+				    u64 vmcs_addr)
+{
+	struct page *vmcs_page = NULL;
+
+	down_read(&current->mm->mmap_sem);
+	vmcs_page = gfn_to_page(vcpu->kvm, vmcs_addr >> PAGE_SHIFT);
+	up_read(&current->mm->mmap_sem);
+
+	if (is_error_page(vmcs_page)) {
+		printk(KERN_ERR "%s error allocating page \n", __func__);
+		kvm_release_page_clean(vmcs_page);
+		return NULL;
+	}
+
+	return vmcs_page;
+
+}
+
 static int init_rmode(struct kvm *kvm);
 static u64 construct_eptp(unsigned long root_hpa);
 
@@ -197,6 +364,8 @@ static struct kvm_vmx_segment_field {
 
 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
 
+static int nested_vmx_check_permission(struct kvm_vcpu *vcpu);
+static int create_l1_state(struct kvm_vcpu *vcpu);
 static int create_l2_state(struct kvm_vcpu *vcpu);
 
 /*
@@ -715,6 +884,24 @@ static void vmx_load_host_state(struct vcpu_vmx *vmx)
 	preempt_enable();
 }
 
+
+static int vmptrld(struct kvm_vcpu *vcpu,
+		   u64 phys_addr)
+{
+	u8 error;
+
+	asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
+		      : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
+		      : "cc");
+	if (error) {
+		printk(KERN_ERR "kvm: %s vmptrld %llx failed\n",
+		       __func__, phys_addr);
+		return 1;
+	}
+
+	return 0;
+}
+
 /*
  * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  * vcpu mutex is already taken.
@@ -736,15 +923,8 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 	}
 
 	if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
-		u8 error;
-
 		per_cpu(current_vmcs, cpu) = vmx->vmcs;
-		asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
-			      : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
-			      : "cc");
-		if (error)
-			printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
-			       vmx->vmcs, phys_addr);
+		vmptrld(vcpu, phys_addr);
 	}
 
 	if (vcpu->cpu != cpu) {
@@ -1318,6 +1498,28 @@ struct level_state *create_state(void)
 	return state;
 }
 
+int create_l1_state(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!vmx->nested.l1_state) {
+		vmx->nested.l1_state = create_state();
+		if (!vmx->nested.l1_state)
+			return -ENOMEM;
+	} else
+		return 0;
+
+	vmx->nested.l1_state->shadow_vmcs = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!vmx->nested.l1_state->shadow_vmcs) {
+		printk(KERN_INFO "%s error creating shadow vmcs\n",
+		       __func__);
+		kfree(vmx->nested.l1_state);
+		return -ENOMEM;
+	}
+	return 0;
+}
+
+static struct vmcs *alloc_vmcs(void);
 int create_l2_state(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -1326,11 +1528,53 @@ int create_l2_state(struct kvm_vcpu *vcpu)
 		vmx->nested.l2_state = create_state();
 		if (!vmx->nested.l2_state)
 			return -ENOMEM;
+	} else
+		return 0;
+
+	vmx->nested.l2_state->vmcs = alloc_vmcs();
+	if (!vmx->nested.l2_state->vmcs) {
+		printk(KERN_ERR "%s error in creating level 2 vmcs", __func__);
+		kfree(vmx->nested.l2_state);
+		return -ENOMEM;
 	}
 
+	if (cpu_has_vmx_msr_bitmap())
+		vmx->nested.l2_state->msr_bitmap = vmcs_read64(MSR_BITMAP);
+	else
+		vmx->nested.l2_state->msr_bitmap = 0;

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1554

Quoted 10 lines
arch/x86/kvm/vmx.c +7 -0
+
+	vmx->nested.l2_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+	vmx->nested.l2_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+
 	return 0;
 }
 
+int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
+			struct kvm_vcpu *vcpu);
+

Move to header.

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1556

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+int read_guest_vmcs_gpa(struct kvm_vcpu *vcpu, u64 *gentry)
+{

Please make all local functions static. Here and in all other patches.

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1561

Quoted 5 lines
arch/x86/kvm/vmx.c +5 -0
+
+	int r = 0;
+
+	r = kvm_read_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX], gentry,
+				sizeof(u64), vcpu);

vmptrld operand can be not only in RAX but in other registers too or in memory.

Quoted 34 lines
arch/x86/kvm/vmx.c +27 -0
+	if (r) {
+		printk(KERN_ERR "%s cannot read guest vmcs addr %lx : %d\n",
+		       __func__, vcpu->arch.regs[VCPU_REGS_RAX], r);
+		return r;
+	}
+
+	if (!IS_ALIGNED(*gentry, PAGE_SIZE)) {
+		printk(KERN_DEBUG "%s addr %llx not aligned\n",
+		       __func__, *gentry);
+		return 1;
+	}
+
+	return 0;
+}
+
+
 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  * tricks.
  */
@@ -3374,6 +3618,66 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_vmptrld(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct page *vmcs_page;
+	u64 guest_vmcs_addr;
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (read_guest_vmcs_gpa(vcpu, &guest_vmcs_addr))
+		return 1;

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3641

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+
+	if (create_l1_state(vcpu)) {
+		printk(KERN_ERR "%s create_l1_state failed\n", __func__);
+		return 1;
+	}
+
+	if (create_l2_state(vcpu)) {
+		printk(KERN_ERR "%s create_l2_state failed\n", __func__);
+		return 1;
+	}

create_l2_state() is called on vmxon. As far as I can see this is nop here.

Quoted 9 lines
arch/x86/kvm/vmx.c +9 -0
+
+	if (vmx->nested.vmptr != guest_vmcs_addr) {
+		/* checking vmptr address */
+		vmcs_page = nested_get_page(vcpu, guest_vmcs_addr);
+		if (vmcs_page == NULL)
+			return 1;
+
+		vmx->nested.vmptr = guest_vmcs_addr;
+

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3660

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+		kvm_release_page_clean(vmcs_page);
+	}
+
+	clear_rflags_cf_zf(vcpu);
+	skip_emulated_instruction(vcpu);
+	return 1;
+}
+
+int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
+			 struct kvm_vcpu *vcpu);

Move to header.

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3671

Quoted 11 lines
arch/x86/kvm/vmx.c +11 -0
+
+static int handle_vmptrst(struct kvm_vcpu *vcpu)
+{
+	int r = 0;
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	r = kvm_write_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX],
+				 (void *)&to_vmx(vcpu)->nested.vmptr,
+				 sizeof(u64), vcpu);

Same as vmptrld. Why are you assuming RAX?

Quoted 24 lines
arch/x86/kvm/vmx.c +11 -2
+	if (r) {
+		printk(KERN_INFO "%s failed to write vmptr\n", __func__);
+		return 1;
+	}
+	clear_rflags_cf_zf(vcpu);
+	skip_emulated_instruction(vcpu);
+	return 1;
+}
+
 static int handle_invlpg(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -3644,8 +3948,8 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_VMCALL]                  = handle_vmcall,
 	[EXIT_REASON_VMCLEAR]	              = handle_vmclear,
 	[EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
-	[EXIT_REASON_VMPTRLD]                 = handle_vmx_insn,
-	[EXIT_REASON_VMPTRST]                 = handle_vmx_insn,
+	[EXIT_REASON_VMPTRLD]                 = handle_vmptrld,
+	[EXIT_REASON_VMPTRST]                 = handle_vmptrst,
 	[EXIT_REASON_VMREAD]                  = handle_vmx_insn,
 	[EXIT_REASON_VMRESUME]                = handle_vmx_insn,
 	[EXIT_REASON_VMWRITE]                 = handle_vmx_insn,

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4491

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
@@ -4183,6 +4487,148 @@ static bool vmx_gb_page_enable(void)
 	return false;
 }
 
+void save_vmcs(struct shadow_vmcs *dst)
+{

Not used by this patch. May be introduce in the patch that uses it.

Quoted 171 lines
arch/x86/kvm/vmx.c +140 -0
+	dst->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
+	dst->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
+	dst->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
+	dst->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
+	dst->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
+	dst->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
+	dst->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
+	dst->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
+	dst->host_es_selector = vmcs_read16(HOST_ES_SELECTOR);
+	dst->host_cs_selector = vmcs_read16(HOST_CS_SELECTOR);
+	dst->host_ss_selector = vmcs_read16(HOST_SS_SELECTOR);
+	dst->host_ds_selector = vmcs_read16(HOST_DS_SELECTOR);
+	dst->host_fs_selector = vmcs_read16(HOST_FS_SELECTOR);
+	dst->host_gs_selector = vmcs_read16(HOST_GS_SELECTOR);
+	dst->host_tr_selector = vmcs_read16(HOST_TR_SELECTOR);
+	dst->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+	dst->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+	if (cpu_has_vmx_msr_bitmap())
+		dst->msr_bitmap = vmcs_read64(MSR_BITMAP);
+
+	dst->vm_exit_msr_store_addr = vmcs_read64(VM_EXIT_MSR_STORE_ADDR);
+	dst->vm_exit_msr_load_addr = vmcs_read64(VM_EXIT_MSR_LOAD_ADDR);
+	dst->vm_entry_msr_load_addr = vmcs_read64(VM_ENTRY_MSR_LOAD_ADDR);
+	dst->tsc_offset = vmcs_read64(TSC_OFFSET);
+	dst->virtual_apic_page_addr = vmcs_read64(VIRTUAL_APIC_PAGE_ADDR);
+	dst->apic_access_addr = vmcs_read64(APIC_ACCESS_ADDR);
+	if (enable_ept)
+		dst->ept_pointer = vmcs_read64(EPT_POINTER);
+
+	dst->guest_physical_address = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
+	dst->vmcs_link_pointer = vmcs_read64(VMCS_LINK_POINTER);
+	dst->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+	if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
+		dst->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
+	if (enable_ept) {
+		dst->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
+		dst->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
+		dst->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
+		dst->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
+	}
+	dst->pin_based_vm_exec_control = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
+	dst->cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
+	dst->exception_bitmap = vmcs_read32(EXCEPTION_BITMAP);
+	dst->page_fault_error_code_mask =
+		vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK);
+	dst->page_fault_error_code_match =
+		vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH);
+	dst->cr3_target_count = vmcs_read32(CR3_TARGET_COUNT);
+	dst->vm_exit_controls = vmcs_read32(VM_EXIT_CONTROLS);
+	dst->vm_exit_msr_store_count = vmcs_read32(VM_EXIT_MSR_STORE_COUNT);
+	dst->vm_exit_msr_load_count = vmcs_read32(VM_EXIT_MSR_LOAD_COUNT);
+	dst->vm_entry_controls = vmcs_read32(VM_ENTRY_CONTROLS);
+	dst->vm_entry_msr_load_count = vmcs_read32(VM_ENTRY_MSR_LOAD_COUNT);
+	dst->vm_entry_intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
+	dst->vm_entry_exception_error_code =
+		vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE);
+	dst->vm_entry_instruction_len = vmcs_read32(VM_ENTRY_INSTRUCTION_LEN);
+	dst->tpr_threshold = vmcs_read32(TPR_THRESHOLD);
+	dst->secondary_vm_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
+	if (enable_vpid && dst->secondary_vm_exec_control &
+	    SECONDARY_EXEC_ENABLE_VPID)
+		dst->virtual_processor_id = vmcs_read16(VIRTUAL_PROCESSOR_ID);
+	dst->vm_instruction_error = vmcs_read32(VM_INSTRUCTION_ERROR);
+	dst->vm_exit_reason  = vmcs_read32(VM_EXIT_REASON);
+	dst->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+	dst->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
+	dst->idt_vectoring_info_field = vmcs_read32(IDT_VECTORING_INFO_FIELD);
+	dst->idt_vectoring_error_code = vmcs_read32(IDT_VECTORING_ERROR_CODE);
+	dst->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
+	dst->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
+	dst->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
+	dst->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
+	dst->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
+	dst->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
+	dst->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
+	dst->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
+	dst->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
+	dst->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
+	dst->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
+	dst->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
+	dst->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
+	dst->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
+	dst->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
+	dst->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
+	dst->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
+	dst->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
+	dst->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
+	dst->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
+	dst->guest_interruptibility_info =
+		vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+	dst->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
+	dst->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
+	dst->host_ia32_sysenter_cs = vmcs_read32(HOST_IA32_SYSENTER_CS);
+	dst->cr0_guest_host_mask = vmcs_readl(CR0_GUEST_HOST_MASK);
+	dst->cr4_guest_host_mask = vmcs_readl(CR4_GUEST_HOST_MASK);
+	dst->cr0_read_shadow = vmcs_readl(CR0_READ_SHADOW);
+	dst->cr4_read_shadow = vmcs_readl(CR4_READ_SHADOW);
+	dst->cr3_target_value0 = vmcs_readl(CR3_TARGET_VALUE0);
+	dst->cr3_target_value1 = vmcs_readl(CR3_TARGET_VALUE1);
+	dst->cr3_target_value2 = vmcs_readl(CR3_TARGET_VALUE2);
+	dst->cr3_target_value3 = vmcs_readl(CR3_TARGET_VALUE3);
+	dst->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	dst->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
+	dst->guest_cr0 = vmcs_readl(GUEST_CR0);
+	dst->guest_cr3 = vmcs_readl(GUEST_CR3);
+	dst->guest_cr4 = vmcs_readl(GUEST_CR4);
+	dst->guest_es_base = vmcs_readl(GUEST_ES_BASE);
+	dst->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
+	dst->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
+	dst->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
+	dst->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
+	dst->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
+	dst->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
+	dst->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
+	dst->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
+	dst->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
+	dst->guest_dr7 = vmcs_readl(GUEST_DR7);
+	dst->guest_rsp = vmcs_readl(GUEST_RSP);
+	dst->guest_rip = vmcs_readl(GUEST_RIP);
+	dst->guest_rflags = vmcs_readl(GUEST_RFLAGS);
+	dst->guest_pending_dbg_exceptions =
+		vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
+	dst->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
+	dst->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
+	dst->host_cr0 = vmcs_readl(HOST_CR0);
+	dst->host_cr3 = vmcs_readl(HOST_CR3);
+	dst->host_cr4 = vmcs_readl(HOST_CR4);
+	dst->host_fs_base = vmcs_readl(HOST_FS_BASE);
+	dst->host_gs_base = vmcs_readl(HOST_GS_BASE);
+	dst->host_tr_base = vmcs_readl(HOST_TR_BASE);
+	dst->host_gdtr_base = vmcs_readl(HOST_GDTR_BASE);
+	dst->host_idtr_base = vmcs_readl(HOST_IDTR_BASE);
+	dst->host_ia32_sysenter_esp = vmcs_readl(HOST_IA32_SYSENTER_ESP);
+	dst->host_ia32_sysenter_eip = vmcs_readl(HOST_IA32_SYSENTER_EIP);
+	dst->host_rsp = vmcs_readl(HOST_RSP);
+	dst->host_rip = vmcs_readl(HOST_RIP);
+	if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT)
+		dst->host_ia32_pat = vmcs_read64(HOST_IA32_PAT);
+}
+
 static struct kvm_x86_ops vmx_x86_ops = {
 	.cpu_has_kvm_support = cpu_has_kvm_support,
 	.disabled_by_bios = vmx_disabled_by_bios,
arch/x86/kvm/x86.c +2 -1
index 9c39092..74eb888 100644
@@ -2473,6 +2473,7 @@ static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
 out:
 	return r;
 }
+EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
 
 static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
 				struct kvm_vcpu *vcpu)
@@ -2503,7 +2504,7 @@ static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
 out:
 	return r;
 }
-
+EXPORT_SYMBOL_GPL(kvm_write_guest_virt);
 
 static int emulator_read_emulated(unsigned long addr,
 				  void *val,
Signature · 6 lines
1.6.0.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
Gleb.

#22 lore raw

Re: [PATCH 3/5] Nested VMX patch 3 implements vmptrld and vmptrst

Gleb Natapov <[email protected]> wrote on 19/10/2009 13:17:41:

Quoted 189 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

19/10/2009 13:17

Subject:

Re: [PATCH 3/5] Nested VMX patch 3 implements vmptrld and vmptrst

Quoted 4 lines · On Thu, Oct 15, 2009 at 04:41:44PM +0200, [email protected] wrote:

From: Orit Wasserman <[email protected]>

---
 arch/x86/kvm/vmx.c |  468 +++++++++++++++++++++++++++++++++++++++

+++++++++++--

Quoted 161 lines
 arch/x86/kvm/x86.c |    3 +-
 2 files changed, 459 insertions(+), 12 deletions(-)
arch/x86/kvm/vmx.c +145 -0
index 411cbdb..8c186e0 100644
@@ -61,20 +61,168 @@ module_param_named(unrestricted_guest,
 static int __read_mostly emulate_invalid_guest_state = 0;
 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
 
+
+struct __attribute__ ((__packed__)) shadow_vmcs {
+   u32 revision_id;
+   u32 abort;
+   u16 virtual_processor_id;
+   u16 guest_es_selector;
+   u16 guest_cs_selector;
+   u16 guest_ss_selector;
+   u16 guest_ds_selector;
+   u16 guest_fs_selector;
+   u16 guest_gs_selector;
+   u16 guest_ldtr_selector;
+   u16 guest_tr_selector;
+   u16 host_es_selector;
+   u16 host_cs_selector;
+   u16 host_ss_selector;
+   u16 host_ds_selector;
+   u16 host_fs_selector;
+   u16 host_gs_selector;
+   u16 host_tr_selector;
+   u64 io_bitmap_a;
+   u64 io_bitmap_b;
+   u64 msr_bitmap;
+   u64 vm_exit_msr_store_addr;
+   u64 vm_exit_msr_load_addr;
+   u64 vm_entry_msr_load_addr;
+   u64 tsc_offset;
+   u64 virtual_apic_page_addr;
+   u64 apic_access_addr;
+   u64 ept_pointer;
+   u64 guest_physical_address;
+   u64 vmcs_link_pointer;
+   u64 guest_ia32_debugctl;
+   u64 guest_ia32_pat;
+   u64 guest_pdptr0;
+   u64 guest_pdptr1;
+   u64 guest_pdptr2;
+   u64 guest_pdptr3;
+   u64 host_ia32_pat;
+   u32 pin_based_vm_exec_control;
+   u32 cpu_based_vm_exec_control;
+   u32 exception_bitmap;
+   u32 page_fault_error_code_mask;
+   u32 page_fault_error_code_match;
+   u32 cr3_target_count;
+   u32 vm_exit_controls;
+   u32 vm_exit_msr_store_count;
+   u32 vm_exit_msr_load_count;
+   u32 vm_entry_controls;
+   u32 vm_entry_msr_load_count;
+   u32 vm_entry_intr_info_field;
+   u32 vm_entry_exception_error_code;
+   u32 vm_entry_instruction_len;
+   u32 tpr_threshold;
+   u32 secondary_vm_exec_control;
+   u32 vm_instruction_error;
+   u32 vm_exit_reason;
+   u32 vm_exit_intr_info;
+   u32 vm_exit_intr_error_code;
+   u32 idt_vectoring_info_field;
+   u32 idt_vectoring_error_code;
+   u32 vm_exit_instruction_len;
+   u32 vmx_instruction_info;
+   u32 guest_es_limit;
+   u32 guest_cs_limit;
+   u32 guest_ss_limit;
+   u32 guest_ds_limit;
+   u32 guest_fs_limit;
+   u32 guest_gs_limit;
+   u32 guest_ldtr_limit;
+   u32 guest_tr_limit;
+   u32 guest_gdtr_limit;
+   u32 guest_idtr_limit;
+   u32 guest_es_ar_bytes;
+   u32 guest_cs_ar_bytes;
+   u32 guest_ss_ar_bytes;
+   u32 guest_ds_ar_bytes;
+   u32 guest_fs_ar_bytes;
+   u32 guest_gs_ar_bytes;
+   u32 guest_ldtr_ar_bytes;
+   u32 guest_tr_ar_bytes;
+   u32 guest_interruptibility_info;
+   u32 guest_activity_state;
+   u32 guest_sysenter_cs;
+   u32 host_ia32_sysenter_cs;
+   unsigned long cr0_guest_host_mask;
+   unsigned long cr4_guest_host_mask;
+   unsigned long cr0_read_shadow;
+   unsigned long cr4_read_shadow;
+   unsigned long cr3_target_value0;
+   unsigned long cr3_target_value1;
+   unsigned long cr3_target_value2;
+   unsigned long cr3_target_value3;
+   unsigned long exit_qualification;
+   unsigned long guest_linear_address;
+   unsigned long guest_cr0;
+   unsigned long guest_cr3;
+   unsigned long guest_cr4;
+   unsigned long guest_es_base;
+   unsigned long guest_cs_base;
+   unsigned long guest_ss_base;
+   unsigned long guest_ds_base;
+   unsigned long guest_fs_base;
+   unsigned long guest_gs_base;
+   unsigned long guest_ldtr_base;
+   unsigned long guest_tr_base;
+   unsigned long guest_gdtr_base;
+   unsigned long guest_idtr_base;
+   unsigned long guest_dr7;
+   unsigned long guest_rsp;
+   unsigned long guest_rip;
+   unsigned long guest_rflags;
+   unsigned long guest_pending_dbg_exceptions;
+   unsigned long guest_sysenter_esp;
+   unsigned long guest_sysenter_eip;
+   unsigned long host_cr0;
+   unsigned long host_cr3;
+   unsigned long host_cr4;
+   unsigned long host_fs_base;
+   unsigned long host_gs_base;
+   unsigned long host_tr_base;
+   unsigned long host_gdtr_base;
+   unsigned long host_idtr_base;
+   unsigned long host_ia32_sysenter_esp;
+   unsigned long host_ia32_sysenter_eip;
+   unsigned long host_rsp;
+   unsigned long host_rip;
+};
+
 struct __attribute__ ((__packed__)) level_state {
    /* Has the level1 guest done vmclear? */
    bool vmclear;
+   u16 vpid;
+   u64 shadow_efer;
+   unsigned long cr2;
+   unsigned long cr3;
+   unsigned long cr4;
+   unsigned long cr8;
+
+   u64 io_bitmap_a;
+   u64 io_bitmap_b;
+   u64 msr_bitmap;
+
+   struct shadow_vmcs *shadow_vmcs;
+
+   struct vmcs *vmcs;
+   int cpu;
+   int launched;
 };
 
Quoted 5 lines

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest,

Quoted 5 lines
arch/x86/kvm/vmx.c +1 -1
 struct nested_vmx {
    /* Has the level1 guest done vmxon? */
    bool vmxon;
-
+   /* What is the location of the  vmcs l1 keeps for l2? (in level1

gpa) */

Quoted 40 lines

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct

Quoted 12 lines
arch/x86/kvm/vmx.c +3 -0
+   u64 vmptr;
    /*
     * Level 2 state : includes vmcs,registers and
     * a copy of vmcs12 for vmread/vmwrite
     */
    struct level_state *l2_state;
+   /* Level 1 state for switching to level 2 and back */
+   struct level_state *l1_state;
 };
 
 struct vmcs {
@@ -140,6 +288,25 @@ static inline struct vcpu_vmx *to_vmx(struct

kvm_vcpu *vcpu)

Quoted 27 lines
arch/x86/kvm/vmx.c +19 -0
    return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+static struct page *nested_get_page(struct kvm_vcpu *vcpu,
+                u64 vmcs_addr)
+{
+   struct page *vmcs_page = NULL;
+
+   down_read(&current->mm->mmap_sem);
+   vmcs_page = gfn_to_page(vcpu->kvm, vmcs_addr >> PAGE_SHIFT);
+   up_read(&current->mm->mmap_sem);
+
+   if (is_error_page(vmcs_page)) {
+      printk(KERN_ERR "%s error allocating page \n", __func__);
+      kvm_release_page_clean(vmcs_page);
+      return NULL;
+   }
+
+   return vmcs_page;
+
+}
+
 static int init_rmode(struct kvm *kvm);
 static u64 construct_eptp(unsigned long root_hpa);
 
@@ -197,6 +364,8 @@ static struct kvm_vmx_segment_field {
 
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_load_host_state(struct vcpu_vmx

Quoted 8 lines
arch/x86/kvm/vmx.c +2 -0
 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
 
+static int nested_vmx_check_permission(struct kvm_vcpu *vcpu);
+static int create_l1_state(struct kvm_vcpu *vcpu);
 static int create_l2_state(struct kvm_vcpu *vcpu);
 
 /*
@@ -715,6 +884,24 @@ static void vmx_load_host_state(struct vcpu_vmx

*vmx)

Quoted 15 lines
Quoted 15 lines
arch/x86/kvm/vmx.c +12 -0
    preempt_enable();
 }
 
+
+static int vmptrld(struct kvm_vcpu *vcpu,
+         u64 phys_addr)
+{
+   u8 error;
+
+   asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
+            : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
+            : "cc");
+   if (error) {
+      printk(KERN_ERR "kvm: %s vmptrld %llx failed\n",
+             __func__, phys_addr);
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_load_host_state(struct vcpu_vmx *vmx)

Quoted 8 lines
arch/x86/kvm/vmx.c +6 -0
+      return 1;
+   }
+
+   return 0;
+}
+
 /*
  * Switches to specified vcpu, until a matching vcpu_put(), but

assumes

Quoted 82 lines

arch/x86/kvm/vmx.c static void vmx_vcpu_load(struct kvm_vcpu

Quoted 2 lines
arch/x86/kvm/vmx.c +0 -0
  * vcpu mutex is already taken.
@@ -736,15 +923,8 @@ static void vmx_vcpu_load(struct kvm_vcpu

*vcpu, int cpu)

Quoted 63 lines
arch/x86/kvm/vmx.c +35 -8
    }
 
    if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
-      u8 error;
-
       per_cpu(current_vmcs, cpu) = vmx->vmcs;
-      asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
-               : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
-               : "cc");
-      if (error)
-         printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
-                vmx->vmcs, phys_addr);
+      vmptrld(vcpu, phys_addr);
    }
 
    if (vcpu->cpu != cpu) {
@@ -1318,6 +1498,28 @@ struct level_state *create_state(void)
    return state;
 }
 
+int create_l1_state(struct kvm_vcpu *vcpu)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+   if (!vmx->nested.l1_state) {
+      vmx->nested.l1_state = create_state();
+      if (!vmx->nested.l1_state)
+         return -ENOMEM;
+   } else
+      return 0;
+
+   vmx->nested.l1_state->shadow_vmcs = kzalloc(PAGE_SIZE, GFP_KERNEL);
+   if (!vmx->nested.l1_state->shadow_vmcs) {
+      printk(KERN_INFO "%s error creating shadow vmcs\n",
+             __func__);
+      kfree(vmx->nested.l1_state);
+      return -ENOMEM;
+   }
+   return 0;
+}
+
+static struct vmcs *alloc_vmcs(void);
 int create_l2_state(struct kvm_vcpu *vcpu)
 {
    struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -1326,11 +1528,53 @@ int create_l2_state(struct kvm_vcpu *vcpu)
       vmx->nested.l2_state = create_state();
       if (!vmx->nested.l2_state)
          return -ENOMEM;
+   } else
+      return 0;
+
+   vmx->nested.l2_state->vmcs = alloc_vmcs();
+   if (!vmx->nested.l2_state->vmcs) {
+      printk(KERN_ERR "%s error in creating level 2 vmcs", __func__);
+      kfree(vmx->nested.l2_state);
+      return -ENOMEM;
    }
 
+   if (cpu_has_vmx_msr_bitmap())
+      vmx->nested.l2_state->msr_bitmap = vmcs_read64(MSR_BITMAP);
+   else
+      vmx->nested.l2_state->msr_bitmap = 0;

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1554

Quoted 10 lines
arch/x86/kvm/vmx.c +7 -0
+
+   vmx->nested.l2_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+   vmx->nested.l2_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+
    return 0;
 }
 
+int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
+         struct kvm_vcpu *vcpu);
+

Move to header.

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1556

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+int read_guest_vmcs_gpa(struct kvm_vcpu *vcpu, u64 *gentry)
+{

Please make all local functions static. Here and in all other patches.

Quoted 6 lines

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1561

Quoted 5 lines
arch/x86/kvm/vmx.c +5 -0
+
+   int r = 0;
+
+   r = kvm_read_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX], gentry,
+            sizeof(u64), vcpu);

vmptrld operand can be not only in RAX but in other registers too orin

memory.
For nested KVM proposes RAX was enough.
For running nested VMWare we added code that handles all the possible options, I will merge it into the nested KVM patches.

Quoted 60 lines
Quoted 35 lines
arch/x86/kvm/vmx.c +27 -0
+   if (r) {
+      printk(KERN_ERR "%s cannot read guest vmcs addr %lx : %d\n",
+             __func__, vcpu->arch.regs[VCPU_REGS_RAX], r);
+      return r;
+   }
+
+   if (!IS_ALIGNED(*gentry, PAGE_SIZE)) {
+      printk(KERN_DEBUG "%s addr %llx not aligned\n",
+             __func__, *gentry);
+      return 1;
+   }
+
+   return 0;
+}
+
+
 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  * tricks.
  */
@@ -3374,6 +3618,66 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
    return 1;
 }
 
+static int handle_vmptrld(struct kvm_vcpu *vcpu)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   struct page *vmcs_page;
+   u64 guest_vmcs_addr;
+
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+
+   if (read_guest_vmcs_gpa(vcpu, &guest_vmcs_addr))
+      return 1;

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3641

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+
+   if (create_l1_state(vcpu)) {
+      printk(KERN_ERR "%s create_l1_state failed\n", __func__);
+      return 1;
+   }
+
+   if (create_l2_state(vcpu)) {
+      printk(KERN_ERR "%s create_l2_state failed\n", __func__);
+      return 1;
+   }

create_l2_state() is called on vmxon. As far as I can see this is nop here.

Quoted 12 lines
arch/x86/kvm/vmx.c +12 -0
+
+   if (vmx->nested.vmptr != guest_vmcs_addr) {
+      /* checking vmptr address */
+      vmcs_page = nested_get_page(vcpu, guest_vmcs_addr);
+      if (vmcs_page == NULL)
+         return 1;
+
+      vmx->nested.vmptr = guest_vmcs_addr;
+
+      kvm_release_page_clean(vmcs_page);
+   }
+
Quoted 8 lines

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3660

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+   clear_rflags_cf_zf(vcpu);
+   skip_emulated_instruction(vcpu);
+   return 1;
+}
+
+int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
+          struct kvm_vcpu *vcpu);

Move to header.

ok.

Quoted 13 lines

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3671

Quoted 12 lines
arch/x86/kvm/vmx.c +11 -0
+
+static int handle_vmptrst(struct kvm_vcpu *vcpu)
+{
+   int r = 0;
+
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+
+   r = kvm_write_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX],
+             (void *)&to_vmx(vcpu)->nested.vmptr,
+             sizeof(u64), vcpu);

Same as vmptrld. Why are you assuming RAX?

See above

Quoted 66 lines

arch/x86/kvm/vmx.c static int (*kvm_vmx_exit_handlers[])(struct

Quoted 14 lines
arch/x86/kvm/vmx.c +9 -0
+   if (r) {
+      printk(KERN_INFO "%s failed to write vmptr\n", __func__);
+      return 1;
+   }
+   clear_rflags_cf_zf(vcpu);
+   skip_emulated_instruction(vcpu);
+   return 1;
+}
+
 static int handle_invlpg(struct kvm_vcpu *vcpu)
 {
    unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -3644,8 +3948,8 @@ static int (*kvm_vmx_exit_handlers[])(struct

kvm_vcpu *vcpu) = {

Quoted 11 lines
arch/x86/kvm/vmx.c +2 -2
    [EXIT_REASON_VMCALL]                  = handle_vmcall,
    [EXIT_REASON_VMCLEAR]                 = handle_vmclear,
    [EXIT_REASON_VMLAUNCH]                = handle_vmx_insn,
-   [EXIT_REASON_VMPTRLD]                 = handle_vmx_insn,
-   [EXIT_REASON_VMPTRST]                 = handle_vmx_insn,
+   [EXIT_REASON_VMPTRLD]                 = handle_vmptrld,
+   [EXIT_REASON_VMPTRST]                 = handle_vmptrst,
    [EXIT_REASON_VMREAD]                  = handle_vmx_insn,
    [EXIT_REASON_VMRESUME]                = handle_vmx_insn,
    [EXIT_REASON_VMWRITE]                 = handle_vmx_insn,

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4491

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
@@ -4183,6 +4487,148 @@ static bool vmx_gb_page_enable(void)
    return false;
 }
 
+void save_vmcs(struct shadow_vmcs *dst)
+{

Not used by this patch. May be introduce in the patch that uses it.

Quoted 33 lines
arch/x86/kvm/vmx.c +33 -0
+   dst->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
+   dst->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
+   dst->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
+   dst->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
+   dst->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
+   dst->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
+   dst->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
+   dst->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
+   dst->host_es_selector = vmcs_read16(HOST_ES_SELECTOR);
+   dst->host_cs_selector = vmcs_read16(HOST_CS_SELECTOR);
+   dst->host_ss_selector = vmcs_read16(HOST_SS_SELECTOR);
+   dst->host_ds_selector = vmcs_read16(HOST_DS_SELECTOR);
+   dst->host_fs_selector = vmcs_read16(HOST_FS_SELECTOR);
+   dst->host_gs_selector = vmcs_read16(HOST_GS_SELECTOR);
+   dst->host_tr_selector = vmcs_read16(HOST_TR_SELECTOR);
+   dst->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+   dst->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+   if (cpu_has_vmx_msr_bitmap())
+      dst->msr_bitmap = vmcs_read64(MSR_BITMAP);
+
+   dst->vm_exit_msr_store_addr = vmcs_read64(VM_EXIT_MSR_STORE_ADDR);
+   dst->vm_exit_msr_load_addr = vmcs_read64(VM_EXIT_MSR_LOAD_ADDR);
+   dst->vm_entry_msr_load_addr = vmcs_read64(VM_ENTRY_MSR_LOAD_ADDR);
+   dst->tsc_offset = vmcs_read64(TSC_OFFSET);
+   dst->virtual_apic_page_addr = vmcs_read64(VIRTUAL_APIC_PAGE_ADDR);
+   dst->apic_access_addr = vmcs_read64(APIC_ACCESS_ADDR);
+   if (enable_ept)
+      dst->ept_pointer = vmcs_read64(EPT_POINTER);
+
+   dst->guest_physical_address = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
+   dst->vmcs_link_pointer = vmcs_read64(VMCS_LINK_POINTER);
+   dst->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+   if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Quoted 8 lines

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+      dst->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
+   if (enable_ept) {
+      dst->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
+      dst->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
+      dst->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
+      dst->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
+   }
+   dst->pin_based_vm_exec_control = vmcs_read32

(PIN_BASED_VM_EXEC_CONTROL);

Quoted 1 line

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4533

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+   dst->cpu_based_vm_exec_control = vmcs_read32

(CPU_BASED_VM_EXEC_CONTROL);

Quoted 8 lines

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void)

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+   dst->exception_bitmap = vmcs_read32(EXCEPTION_BITMAP);
+   dst->page_fault_error_code_mask =
+      vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK);
+   dst->page_fault_error_code_match =
+      vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH);
+   dst->cr3_target_count = vmcs_read32(CR3_TARGET_COUNT);
+   dst->vm_exit_controls = vmcs_read32(VM_EXIT_CONTROLS);
+   dst->vm_exit_msr_store_count = vmcs_read32

(VM_EXIT_MSR_STORE_COUNT);

Quoted 3 lines

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void)

Quoted 3 lines
arch/x86/kvm/vmx.c +3 -0
+   dst->vm_exit_msr_load_count = vmcs_read32(VM_EXIT_MSR_LOAD_COUNT);
+   dst->vm_entry_controls = vmcs_read32(VM_ENTRY_CONTROLS);
+   dst->vm_entry_msr_load_count = vmcs_read32

(VM_ENTRY_MSR_LOAD_COUNT);

Quoted 1 line

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4545

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+   dst->vm_entry_intr_info_field = vmcs_read32

(VM_ENTRY_INTR_INFO_FIELD);

Quoted 3 lines

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void)

Quoted 3 lines
arch/x86/kvm/vmx.c +3 -0
+   dst->vm_entry_exception_error_code =
+      vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE);
+   dst->vm_entry_instruction_len = vmcs_read32

(VM_ENTRY_INSTRUCTION_LEN);

Quoted 2 lines

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4550

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+   dst->tpr_threshold = vmcs_read32(TPR_THRESHOLD);
+   dst->secondary_vm_exec_control = vmcs_read32

(SECONDARY_VM_EXEC_CONTROL);

Quoted 7 lines

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void)

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+   if (enable_vpid && dst->secondary_vm_exec_control &
+       SECONDARY_EXEC_ENABLE_VPID)
+      dst->virtual_processor_id = vmcs_read16(VIRTUAL_PROCESSOR_ID);
+   dst->vm_instruction_error = vmcs_read32(VM_INSTRUCTION_ERROR);
+   dst->vm_exit_reason  = vmcs_read32(VM_EXIT_REASON);
+   dst->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+   dst->vm_exit_intr_error_code = vmcs_read32

(VM_EXIT_INTR_ERROR_CODE);

Quoted 1 line

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4558

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+   dst->idt_vectoring_info_field = vmcs_read32

(IDT_VECTORING_INFO_FIELD);

Quoted 1 line

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4559

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+   dst->idt_vectoring_error_code = vmcs_read32

(IDT_VECTORING_ERROR_CODE);

Quoted 1 line

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4560

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+   dst->vm_exit_instruction_len = vmcs_read32

(VM_EXIT_INSTRUCTION_LEN);

Quoted 72 lines
Quoted 72 lines
arch/x86/kvm/vmx.c +71 -0
+   dst->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
+   dst->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
+   dst->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
+   dst->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
+   dst->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
+   dst->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
+   dst->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
+   dst->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
+   dst->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
+   dst->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
+   dst->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
+   dst->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
+   dst->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
+   dst->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
+   dst->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
+   dst->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
+   dst->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
+   dst->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
+   dst->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
+   dst->guest_interruptibility_info =
+      vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+   dst->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
+   dst->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
+   dst->host_ia32_sysenter_cs = vmcs_read32(HOST_IA32_SYSENTER_CS);
+   dst->cr0_guest_host_mask = vmcs_readl(CR0_GUEST_HOST_MASK);
+   dst->cr4_guest_host_mask = vmcs_readl(CR4_GUEST_HOST_MASK);
+   dst->cr0_read_shadow = vmcs_readl(CR0_READ_SHADOW);
+   dst->cr4_read_shadow = vmcs_readl(CR4_READ_SHADOW);
+   dst->cr3_target_value0 = vmcs_readl(CR3_TARGET_VALUE0);
+   dst->cr3_target_value1 = vmcs_readl(CR3_TARGET_VALUE1);
+   dst->cr3_target_value2 = vmcs_readl(CR3_TARGET_VALUE2);
+   dst->cr3_target_value3 = vmcs_readl(CR3_TARGET_VALUE3);
+   dst->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+   dst->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
+   dst->guest_cr0 = vmcs_readl(GUEST_CR0);
+   dst->guest_cr3 = vmcs_readl(GUEST_CR3);
+   dst->guest_cr4 = vmcs_readl(GUEST_CR4);
+   dst->guest_es_base = vmcs_readl(GUEST_ES_BASE);
+   dst->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
+   dst->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
+   dst->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
+   dst->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
+   dst->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
+   dst->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
+   dst->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
+   dst->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
+   dst->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
+   dst->guest_dr7 = vmcs_readl(GUEST_DR7);
+   dst->guest_rsp = vmcs_readl(GUEST_RSP);
+   dst->guest_rip = vmcs_readl(GUEST_RIP);
+   dst->guest_rflags = vmcs_readl(GUEST_RFLAGS);
+   dst->guest_pending_dbg_exceptions =
+      vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
+   dst->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
+   dst->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
+   dst->host_cr0 = vmcs_readl(HOST_CR0);
+   dst->host_cr3 = vmcs_readl(HOST_CR3);
+   dst->host_cr4 = vmcs_readl(HOST_CR4);
+   dst->host_fs_base = vmcs_readl(HOST_FS_BASE);
+   dst->host_gs_base = vmcs_readl(HOST_GS_BASE);
+   dst->host_tr_base = vmcs_readl(HOST_TR_BASE);
+   dst->host_gdtr_base = vmcs_readl(HOST_GDTR_BASE);
+   dst->host_idtr_base = vmcs_readl(HOST_IDTR_BASE);
+   dst->host_ia32_sysenter_esp = vmcs_readl(HOST_IA32_SYSENTER_ESP);
+   dst->host_ia32_sysenter_eip = vmcs_readl(HOST_IA32_SYSENTER_EIP);
+   dst->host_rsp = vmcs_readl(HOST_RSP);
+   dst->host_rip = vmcs_readl(HOST_RIP);
+   if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT)
+      dst->host_ia32_pat = vmcs_read64(HOST_IA32_PAT);
+}
+
 static struct kvm_x86_ops vmx_x86_ops = {
Quoted 14 lines

arch/x86/kvm/x86.c static int kvm_read_guest_virt(gva_t addr,

Quoted 7 lines
arch/x86/kvm/vmx.c +0 -0
    .cpu_has_kvm_support = cpu_has_kvm_support,
    .disabled_by_bios = vmx_disabled_by_bios,
arch/x86/kvm/x86.c +0 -0
index 9c39092..74eb888 100644
@@ -2473,6 +2473,7 @@ static int kvm_read_guest_virt(gva_t addr,

void *val, unsigned int bytes,

Quoted 6 lines
arch/x86/kvm/x86.c +1 -0
 out:
    return r;
 }
+EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
 
 static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int

bytes,

Quoted 21 lines

arch/x86/kvm/x86.c static int kvm_write_guest_virt(gva_t addr,

Quoted 2 lines
arch/x86/kvm/x86.c +0 -0
             struct kvm_vcpu *vcpu)
@@ -2503,7 +2504,7 @@ static int kvm_write_guest_virt(gva_t addr,

void *val, unsigned int bytes,

Quoted 11 lines
arch/x86/kvm/x86.c +1 -2
 out:
    return r;
 }
-
+EXPORT_SYMBOL_GPL(kvm_write_guest_virt);
 
 static int emulator_read_emulated(unsigned long addr,
               void *val,
--

1.6.0.4

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
Gleb.

#23 lore raw

Re: [PATCH 3/5] Nested VMX patch 3 implements vmptrld and vmptrst

In reply to [email protected]

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) · line 298

Quoted 8 lines · On Thu, Oct 15, 2009 at 04:41:44PM +0200, [email protected] wrote:
arch/x86/kvm/vmx.c +8 -0
+static struct page *nested_get_page(struct kvm_vcpu *vcpu,
+				    u64 vmcs_addr)
+{
+	struct page *vmcs_page = NULL;
+
+	down_read(&current->mm->mmap_sem);
+	vmcs_page = gfn_to_page(vcpu->kvm, vmcs_addr >> PAGE_SHIFT);
+	up_read(&current->mm->mmap_sem);

Why are you taking mmap_sem here? gup_fast() takes it if required.

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) · line 309

Quoted 11 lines
arch/x86/kvm/vmx.c +11 -0
+
+	if (is_error_page(vmcs_page)) {
+		printk(KERN_ERR "%s error allocating page \n", __func__);
+		kvm_release_page_clean(vmcs_page);
+		return NULL;
+	}
+
+	return vmcs_page;
+
+}
+

--
Gleb.

#24 lore raw

Re: [PATCH 3/5] Nested VMX patch 3 implements vmptrld and vmptrst

Gleb Natapov <[email protected]> wrote on 19/10/2009 14:59:53:

Quoted 23 lines

From:

Gleb Natapov <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

19/10/2009 15:00

Subject:

Re: [PATCH 3/5] Nested VMX patch 3 implements vmptrld and vmptrst

On Thu, Oct 15, 2009 at 04:41:44PM +0200, [email protected] wrote:

Quoted 9 lines

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) · line 298

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+static struct page *nested_get_page(struct kvm_vcpu *vcpu,
+                u64 vmcs_addr)
+{
+   struct page *vmcs_page = NULL;
+
+   down_read(&current->mm->mmap_sem);
+   vmcs_page = gfn_to_page(vcpu->kvm, vmcs_addr >> PAGE_SHIFT);
+   up_read(&current->mm->mmap_sem);

Why are you taking mmap_sem here? gup_fast() takes it if required.

I will remove it.

Quoted 15 lines

arch/x86/kvm/vmx.c static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) · line 309

Quoted 12 lines
arch/x86/kvm/vmx.c +11 -0
+
+   if (is_error_page(vmcs_page)) {
+      printk(KERN_ERR "%s error allocating page \n", __func__);
+      kvm_release_page_clean(vmcs_page);
+      return NULL;
+   }
+
+   return vmcs_page;
+
+}
+

--
Gleb.

#25 lore raw

Re: [PATCH 3/5] Nested VMX patch 3 implements vmptrld and vmptrst

In reply to [email protected]

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest, · line 65

Quoted 4 lines · On 10/15/2009 11:41 PM, [email protected] wrote:
arch/x86/kvm/vmx.c +2 -0
+
+struct __attribute__ ((__packed__)) shadow_vmcs {

Since this is in guest memory, we need it packed so the binary format is preserved across migration. Please add a comment so it isn't changed (at least without changing the revision_id).

vmclear state should be here, that will help multiguest support.

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest, · line 218

Quoted 8 lines

struct nested_vmx {
/* Has the level1 guest done vmxon? */
bool vmxon;

arch/x86/kvm/vmx.c +2 -1
-
+	/* What is the location of the  vmcs l1 keeps for l2? (in level1 gpa) */
+	u64 vmptr;

Need to expose it for live migration.

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest, · line 225

Quoted 8 lines

/*
* Level 2 state : includes vmcs,registers and
* a copy of vmcs12 for vmread/vmwrite */
struct level_state *l2_state;

arch/x86/kvm/vmx.c +2 -0
+	/* Level 1 state for switching to level 2 and back */
+	struct level_state *l1_state;

This creates a ton of duplication.

Some of the data is completely unnecessary, for example we can recalculate cr0 from HOST_CR0 and GUEST_CR0.

Quoted 27 lines
arch/x86/kvm/vmx.c +18 -2
+
+static int vmptrld(struct kvm_vcpu *vcpu,
+		   u64 phys_addr)
+{
+	u8 error;
+
+	asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
+		      : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
+		      : "cc");
+	if (error) {
+		printk(KERN_ERR "kvm: %s vmptrld %llx failed\n",
+		       __func__, phys_addr);
+		return 1;
+	}
+
+	return 0;
+}
+
  /*
   * Switches to specified vcpu, until a matching vcpu_put(), but assumes
   * vcpu mutex is already taken.
@@ -736,15 +923,8 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  	}
 
  	if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
-		u8 error;
-

arch/x86/kvm/vmx.c static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 929

Quoted 10 lines
arch/x86/kvm/vmx.c +1 -6
  		per_cpu(current_vmcs, cpu) = vmx->vmcs;
-		asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
-			      : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
-			      : "cc");
-		if (error)
-			printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
-			       vmx->vmcs, phys_addr);
+		vmptrld(vcpu, phys_addr);
  	}
 

This part of the patch is no longer needed.

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1548

Quoted 9 lines
arch/x86/kvm/vmx.c +8 -0
+	if (cpu_has_vmx_msr_bitmap())
+		vmx->nested.l2_state->msr_bitmap = vmcs_read64(MSR_BITMAP);
+	else
+		vmx->nested.l2_state->msr_bitmap = 0;
+
+	vmx->nested.l2_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+	vmx->nested.l2_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+

This no longer works, since we don't load the guest vmcs.

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1553

Quoted 3 lines
arch/x86/kvm/vmx.c +2 -0
+int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
+			struct kvm_vcpu *vcpu);

Isn't this in a header somewhere?

Quoted 14 lines
arch/x86/kvm/vmx.c +13 -0
+
+int read_guest_vmcs_gpa(struct kvm_vcpu *vcpu, u64 *gentry)
+{
+
+	int r = 0;
+
+	r = kvm_read_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX], gentry,
+				sizeof(u64), vcpu);
+	if (r) {
+		printk(KERN_ERR "%s cannot read guest vmcs addr %lx : %d\n",
+		       __func__, vcpu->arch.regs[VCPU_REGS_RAX], r);
+		return r;
+	}

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1576

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+
+	if (!IS_ALIGNED(*gentry, PAGE_SIZE)) {
+		printk(KERN_DEBUG "%s addr %llx not aligned\n",
+		       __func__, *gentry);
+		return 1;
+	}
+
+	return 0;
+}
+

Should go through the emulator to evaluate arguments.

Quoted 12 lines
arch/x86/kvm/vmx.c +11 -0
+static int handle_vmptrld(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct page *vmcs_page;
+	u64 guest_vmcs_addr;
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (read_guest_vmcs_gpa(vcpu,&guest_vmcs_addr))
+		return 1;

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3641

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+
+	if (create_l1_state(vcpu)) {
+		printk(KERN_ERR "%s create_l1_state failed\n", __func__);
+		return 1;
+	}
+
+	if (create_l2_state(vcpu)) {
+		printk(KERN_ERR "%s create_l2_state failed\n", __func__);
+		return 1;
+	}

return errors here, so we see the problem.

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3671

Quoted 12 lines
arch/x86/kvm/vmx.c +11 -0
+
+static int handle_vmptrst(struct kvm_vcpu *vcpu)
+{
+	int r = 0;
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	r = kvm_write_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX],
+				 (void *)&to_vmx(vcpu)->nested.vmptr,
+				 sizeof(u64), vcpu);

Emulator again.

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4508

Quoted 6 lines
arch/x86/kvm/vmx.c +4 -0
+void save_vmcs(struct shadow_vmcs *dst)
+{
 
+	dst->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+	dst->io_bitmap_b = vmcs_read64(IO_BITMAP_B);

These (and many others) can never change due to a nested guest running, so no need to save them.

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4516

Quoted 2 lines
arch/x86/kvm/vmx.c +1 -0
+	dst->virtual_apic_page_addr = vmcs_read64(VIRTUAL_APIC_PAGE_ADDR);

In general, you need to translate host physical addresses to guest physical addresses.

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4520

Quoted 5 lines
arch/x86/kvm/vmx.c +4 -0
+	dst->apic_access_addr = vmcs_read64(APIC_ACCESS_ADDR);
+	if (enable_ept)
+		dst->ept_pointer = vmcs_read64(EPT_POINTER);
+

Not all hosts support these features.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#26 lore raw

Re: [PATCH 3/5] Nested VMX patch 3 implements vmptrld and vmptrst

Avi Kivity <[email protected]> wrote on 20/10/2009 06:24:33:

Quoted 24 lines

From:

Avi Kivity <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

20/10/2009 06:24

Subject:

Re: [PATCH 3/5] Nested VMX patch 3 implements vmptrld and vmptrst

Quoted 1 line · On 10/15/2009 11:41 PM, [email protected] wrote:
Quoted 7 lines

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest, · line 65

Quoted 3 lines
arch/x86/kvm/vmx.c +2 -0
+
+struct __attribute__ ((__packed__)) shadow_vmcs {

Since this is in guest memory, we need it packed so the binary format is preserved across migration. Please add a comment so it isn't changed (at least without changing the revision_id).

I will add a comment.

Quoted 2 lines

vmclear state should be here, that will help multiguest support.

Working on it ...

Quoted 7 lines

Review

Quoted 7 lines

  struct nested_vmx {
     /* Has the level1 guest done vmxon? */
     bool vmxon;
(quoted diff) +1 -1
-
+   /* What is the location of the  vmcs l1 keeps for l2? (in level1

gpa) */

Quoted 4 lines
Quoted 2 lines

+ u64 vmptr;

Need to expose it for live migration.

I will look into it.

Quoted 10 lines

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest, · line 225

Quoted 9 lines
     /*
      * Level 2 state : includes vmcs,registers and
      * a copy of vmcs12 for vmread/vmwrite
      */
     struct level_state *l2_state;
arch/x86/kvm/vmx.c +2 -0
+   /* Level 1 state for switching to level 2 and back */
+   struct level_state *l1_state;
Quoted 4 lines

This creates a ton of duplication.

Some of the data is completely unnecessary, for example we can recalculate cr0 from HOST_CR0 and GUEST_CR0.

I will look into it .

Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/vmx.c +12 -0
+
+static int vmptrld(struct kvm_vcpu *vcpu,
+         u64 phys_addr)
+{
+   u8 error;
+
+   asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
+            : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
+            : "cc");
+   if (error) {
+      printk(KERN_ERR "kvm: %s vmptrld %llx failed\n",
+             __func__, phys_addr);
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_load_host_state(struct vcpu_vmx *vmx)

Quoted 8 lines
arch/x86/kvm/vmx.c +6 -0
+      return 1;
+   }
+
+   return 0;
+}
+
  /*
   * Switches to specified vcpu, until a matching vcpu_put(), but

assumes

Quoted 19 lines

Review static void vmx_vcpu_load(struct kvm_vcpu

Quoted 2 lines

* vcpu mutex is already taken.

(quoted diff) +0 -0
@@ -736,15 +923,8 @@ static void vmx_vcpu_load(struct kvm_vcpu

*vcpu, int cpu)

Quoted 5 lines
arch/x86/kvm/vmx.c +0 -2
     }
 
     if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
-      u8 error;
-

arch/x86/kvm/vmx.c static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 929

Quoted 10 lines
arch/x86/kvm/vmx.c +1 -6
        per_cpu(current_vmcs, cpu) = vmx->vmcs;
-      asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
-               : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
-               : "cc");
-      if (error)
-         printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
-                vmx->vmcs, phys_addr);
+      vmptrld(vcpu, phys_addr);
     }
 
Quoted 1 line

This part of the patch is no longer needed.

I will remove it.

Quoted 11 lines

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1548

Quoted 9 lines
arch/x86/kvm/vmx.c +8 -0
+   if (cpu_has_vmx_msr_bitmap())
+      vmx->nested.l2_state->msr_bitmap = vmcs_read64(MSR_BITMAP);
+   else
+      vmx->nested.l2_state->msr_bitmap = 0;
+
+   vmx->nested.l2_state->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+   vmx->nested.l2_state->io_bitmap_b = vmcs_read64(IO_BITMAP_B);
+

This no longer works, since we don't load the guest vmcs.

I will look into it.

Quoted 6 lines

arch/x86/kvm/vmx.c int create_l2_state(struct kvm_vcpu *vcpu) · line 1553

Quoted 4 lines
arch/x86/kvm/vmx.c +2 -0
+int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
+         struct kvm_vcpu *vcpu);

Isn't this in a header somewhere?

I will move it into the header.

Quoted 19 lines
Quoted 19 lines
arch/x86/kvm/vmx.c +18 -0
+
+int read_guest_vmcs_gpa(struct kvm_vcpu *vcpu, u64 *gentry)
+{
+
+   int r = 0;
+
+   r = kvm_read_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX], gentry,
+            sizeof(u64), vcpu);
+   if (r) {
+      printk(KERN_ERR "%s cannot read guest vmcs addr %lx : %d\n",
+             __func__, vcpu->arch.regs[VCPU_REGS_RAX], r);
+      return r;
+   }
+
+   if (!IS_ALIGNED(*gentry, PAGE_SIZE)) {
+      printk(KERN_DEBUG "%s addr %llx not aligned\n",
+             __func__, *gentry);
+      return 1;
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_load_host_state(struct vcpu_vmx *vmx) · line 904

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+   }
+
+   return 0;
+}
+

Should go through the emulator to evaluate arguments.

OK.

Quoted 17 lines
Quoted 17 lines
arch/x86/kvm/vmx.c +16 -0
+static int handle_vmptrld(struct kvm_vcpu *vcpu)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   struct page *vmcs_page;
+   u64 guest_vmcs_addr;
+
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+
+   if (read_guest_vmcs_gpa(vcpu,&guest_vmcs_addr))
+      return 1;
+
+   if (create_l1_state(vcpu)) {
+      printk(KERN_ERR "%s create_l1_state failed\n", __func__);
+      return 1;
+   }
Quoted 8 lines

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3641

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+
+   if (create_l2_state(vcpu)) {
+      printk(KERN_ERR "%s create_l2_state failed\n", __func__);
+      return 1;
+   }

return errors here, so we see the problem.

OK.

Quoted 15 lines

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3671

Quoted 13 lines
arch/x86/kvm/vmx.c +11 -0
+
+static int handle_vmptrst(struct kvm_vcpu *vcpu)
+{
+   int r = 0;
+
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+
+   r = kvm_write_guest_virt(vcpu->arch.regs[VCPU_REGS_RAX],
+             (void *)&to_vmx(vcpu)->nested.vmptr,
+             sizeof(u64), vcpu);

Emulator again.

OK.

Quoted 11 lines

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4508

Quoted 7 lines
arch/x86/kvm/vmx.c +4 -0
+void save_vmcs(struct shadow_vmcs *dst)
+{
 
+   dst->io_bitmap_a = vmcs_read64(IO_BITMAP_A);
+   dst->io_bitmap_b = vmcs_read64(IO_BITMAP_B);

These (and many others) can never change due to a nested guest running, so no need to save them.

Quoted 5 lines
Quoted 2 lines

+ dst->virtual_apic_page_addr = vmcs_read64(VIRTUAL_APIC_PAGE_ADDR);

In general, you need to translate host physical addresses to guest physical addresses.

If this is VMCS12 than it is already guest physical address.

Quoted 8 lines

arch/x86/kvm/vmx.c static bool vmx_gb_page_enable(void) · line 4520

Quoted 6 lines
arch/x86/kvm/vmx.c +4 -0
+   dst->apic_access_addr = vmcs_read64(APIC_ACCESS_ADDR);
+   if (enable_ept)
+      dst->ept_pointer = vmcs_read64(EPT_POINTER);
+

Not all hosts support these features.

I will add a check.

Quoted 5 lines

--
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.

#27 lore raw

Re: [PATCH 2/5] Nested VMX patch 2 implements vmclear

In reply to [email protected]
Quoted 13 lines · On 10/15/2009 11:41 PM, [email protected] wrote:

From: Orit Wasserman<[email protected]>

---
  arch/x86/kvm/vmx.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++---
  1 files changed, 65 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +0 -0
index 71bd91a..411cbdb 100644
@@ -61,15 +61,26 @@ module_param_named(unrestricted_guest,
  static int __read_mostly emulate_invalid_guest_state = 0;
  module_param(emulate_invalid_guest_state, bool, S_IRUGO);

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest, · line 68

Quoted 10 lines
arch/x86/kvm/vmx.c +3 -4
 
-struct vmcs {
-	u32 revision_id;
-	u32 abort;
-	char data[0];
+struct __attribute__ ((__packed__)) level_state {
+	/* Has the level1 guest done vmclear? */
+	bool vmclear;
  };
 

Why __packed__?

Quoted 8 lines
arch/x86/kvm/vmx.c +4 -0
  struct nested_vmx {
  	/* Has the level1 guest done vmxon? */
  	bool vmxon;
+
+	/*
+	 * Level 2 state : includes vmcs,registers and
+	 * a copy of vmcs12 for vmread/vmwrite

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest, · line 85

Quoted 10 lines
arch/x86/kvm/vmx.c +8 -0
+	 */
+	struct level_state *l2_state;
+};
+
+struct vmcs {
+	u32 revision_id;
+	u32 abort;
+	char data[0];
  };
 

Why move struct vmcs around?

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu) · line 3373

Quoted 11 lines
arch/x86/kvm/vmx.c +3 -0
+
  static int handle_vmoff(struct kvm_vcpu *vcpu)
  {
  	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -3310,6 +3368,8 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 
  	vmx->nested.vmxon = 1;
 
+	create_l2_state(vcpu);
+
 

Need to check return code.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#28 lore raw

Re: [PATCH 2/5] Nested VMX patch 2 implements vmclear

Avi Kivity <[email protected]> wrote on 20/10/2009 06:06:40:

Quoted 38 lines

From:

Avi Kivity <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

20/10/2009 06:06

Subject:

Re: [PATCH 2/5] Nested VMX patch 2 implements vmclear

Quoted 4 lines · On 10/15/2009 11:41 PM, [email protected] wrote:

From: Orit Wasserman<[email protected]>

---
  arch/x86/kvm/vmx.c |   70 ++++++++++++++++++++++++++++++++++++++

++++++++++---

Quoted 10 lines
  1 files changed, 65 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +0 -0
index 71bd91a..411cbdb 100644
@@ -61,15 +61,26 @@ module_param_named(unrestricted_guest,
  static int __read_mostly emulate_invalid_guest_state = 0;
  module_param(emulate_invalid_guest_state, bool, S_IRUGO);
 
Quoted 11 lines

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest, · line 68

Quoted 9 lines
arch/x86/kvm/vmx.c +3 -4
-struct vmcs {
-   u32 revision_id;
-   u32 abort;
-   char data[0];
+struct __attribute__ ((__packed__)) level_state {
+   /* Has the level1 guest done vmclear? */
+   bool vmclear;
  };
 

Why __packed__?

I will remove it.

Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/vmx.c +8 -0
  struct nested_vmx {
     /* Has the level1 guest done vmxon? */
     bool vmxon;
+
+   /*
+    * Level 2 state : includes vmcs,registers and
+    * a copy of vmcs12 for vmread/vmwrite
+    */
+   struct level_state *l2_state;
+};
+
Quoted 8 lines

arch/x86/kvm/vmx.c module_param_named(unrestricted_guest, · line 85

Quoted 6 lines
arch/x86/kvm/vmx.c +4 -0
+struct vmcs {
+   u32 revision_id;
+   u32 abort;
+   char data[0];
  };
 

Why move struct vmcs around?

I will fix it.

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx.c +1 -0
+
  static int handle_vmoff(struct kvm_vcpu *vcpu)
  {
     struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -3310,6 +3368,8 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 
Quoted 7 lines

arch/x86/kvm/vmx.c static int handle_vmon(struct kvm_vcpu *vcpu)

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
     vmx->nested.vmxon = 1;
 
+   create_l2_state(vcpu);
+
 

Need to check return code.

I will add the check.

Quoted 6 lines

--
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.

#29 lore raw

Re: [PATCH 1/5] Nested VMX patch 1 implements vmon and vmoff

In reply to [email protected]
Quoted 10 lines · On 10/15/2009 11:41 PM, [email protected] wrote:

/*
+ * Handles msr read for nested virtualization
+ */

arch/x86/kvm/vmx.c +5 -0
+static int nested_vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index,
+			      u64 *pdata)
+{
+	u64 vmx_msr = 0;
+

arch/x86/kvm/vmx.c static void guest_write_tsc(u64 guest_tsc, u64 host_tsc) · line 1017

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	switch (msr_index) {
+	case MSR_IA32_FEATURE_CONTROL:
+		*pdata = 0;
+		break;
+	case MSR_IA32_VMX_BASIC:
+		*pdata = 0;
+		rdmsrl(MSR_IA32_VMX_BASIC, vmx_msr);
+		*pdata = (vmx_msr&  0x00ffffcfffffffff);
+		break;
+

This (and the rest of the msrs) must be controllable from userspace. Otherwise a live migration from a newer host to an older host would break.

Quoted 13 lines

/*
+ * Writes msr value for nested virtualization
+ * Returns 0 on success, non-0 otherwise.
+ */

arch/x86/kvm/vmx.c +7 -0
+static int nested_vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
+{
+	switch (msr_index) {
+	case MSR_IA32_FEATURE_CONTROL:
+		if ((data&  (FEATURE_CONTROL_LOCKED |
+			     FEATURE_CONTROL_VMXON_ENABLED))
+		    != (FEATURE_CONTROL_LOCKED |

arch/x86/kvm/vmx.c static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) · line 1141

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+			FEATURE_CONTROL_VMXON_ENABLED))
+			return 1;
+		break;
+	default:
+		return 1;
+	}
+
+	return 0;
+}
+

Need to export this msr to userspace for live migration. See msrs_to_save[].

Quoted 6 lines
arch/x86/kvm/vmx.c +4 -0
+/*
+ * Check to see if vcpu can execute vmx command
+ * Inject the corrseponding exception
+ */

arch/x86/kvm/vmx.c static int handle_vmcall(struct kvm_vcpu *vcpu) · line 3235

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
+{
+	struct kvm_segment cs;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct kvm_msr_entry *msr;
+
+	vmx_get_segment(vcpu,&cs, VCPU_SREG_CS);
+
+	if (!vmx->nested.vmxon) {
+		printk(KERN_DEBUG "%s: vmx not on\n", __func__);

pr_debug

arch/x86/kvm/vmx.c static int handle_vmcall(struct kvm_vcpu *vcpu)

Quoted 9 lines
arch/x86/kvm/vmx.c +8 -0
+		kvm_queue_exception(vcpu, UD_VECTOR);
+		return 0;
+	}
+
+	msr = find_msr_entry(vmx, MSR_EFER);
+
+	if ((vmx_get_rflags(vcpu)&  X86_EFLAGS_VM) ||
+		 ((msr->data&  EFER_LMA)&&  !cs.l)) {

is_long_mode()

Quoted 33 lines
  static int handle_vmx_insn(struct kvm_vcpu *vcpu)
  {
  	kvm_queue_exception(vcpu, UD_VECTOR);
  	return 1;
  }
arch/x86/kvm/vmx.c +26 -0
+static int handle_vmoff(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	vmx->nested.vmxon = 0;
+
+	skip_emulated_instruction(vcpu);
+	return 1;
+}
+
+static int handle_vmon(struct kvm_vcpu *vcpu)
+{
+	struct kvm_segment cs;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!nested) {
+		printk(KERN_DEBUG "%s: nested vmx not enabled\n", __func__);
+		kvm_queue_exception(vcpu, UD_VECTOR);
+		return 1;
+	}
+
+	vmx_get_segment(vcpu,&cs, VCPU_SREG_CS);
+

arch/x86/kvm/vmx.c static int handle_vmcall(struct kvm_vcpu *vcpu)

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	if (!(vcpu->arch.cr4&  X86_CR4_VMXE) ||
+	    !(vcpu->arch.cr0&  X86_CR0_PE) ||
+	    (vmx_get_rflags(vcpu)&  X86_EFLAGS_VM)) {
+		kvm_queue_exception(vcpu, UD_VECTOR);
+		printk(KERN_INFO "%s invalid register state\n", __func__);
+		return 1;
+	}
+#ifdef CONFIG_X86_64
+	if (((find_msr_entry(to_vmx(vcpu),
+			     MSR_EFER)->data&  EFER_LMA)&&  !cs.l)) {

is_long_mode(), and you can avoid the #ifdef.

VMXON is supposed to block INIT, please add that (in a separate patch).

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#30 lore raw

Re: [PATCH 1/5] Nested VMX patch 1 implements vmon and vmoff

Avi Kivity <[email protected]> wrote on 20/10/2009 06:00:50:

Quoted 38 lines

From:

Avi Kivity <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

20/10/2009 06:02

Subject:

Re: [PATCH 1/5] Nested VMX patch 1 implements vmon and vmoff

Quoted 15 lines · On 10/15/2009 11:41 PM, [email protected] wrote:

/*
+ * Handles msr read for nested virtualization
+ */

arch/x86/kvm/vmx.c +11 -0
+static int nested_vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index,
+               u64 *pdata)
+{
+   u64 vmx_msr = 0;
+
+   switch (msr_index) {
+   case MSR_IA32_FEATURE_CONTROL:
+      *pdata = 0;
+      break;
+   case MSR_IA32_VMX_BASIC:
+      *pdata = 0;
Quoted 8 lines

arch/x86/kvm/vmx.c static void guest_write_tsc(u64 guest_tsc, u64 host_tsc) · line 1017

Quoted 5 lines
arch/x86/kvm/vmx.c +4 -0
+      rdmsrl(MSR_IA32_VMX_BASIC, vmx_msr);
+      *pdata = (vmx_msr&  0x00ffffcfffffffff);
+      break;
+

This (and the rest of the msrs) must be controllable from userspace. Otherwise a live migration from a newer host to an older host would

break.
OK.

Quoted 20 lines
Quoted 7 lines

/*
+ * Writes msr value for nested virtualization
+ * Returns 0 on success, non-0 otherwise.
+ */
+static int nested_vmx_set_msr(struct kvm_vcpu *vcpu, u32

msr_index, u64 data)

Quoted 12 lines
arch/x86/kvm/vmx.c +12 -0
+{
+   switch (msr_index) {
+   case MSR_IA32_FEATURE_CONTROL:
+      if ((data&  (FEATURE_CONTROL_LOCKED |
+              FEATURE_CONTROL_VMXON_ENABLED))
+          != (FEATURE_CONTROL_LOCKED |
+         FEATURE_CONTROL_VMXON_ENABLED))
+         return 1;
+      break;
+   default:
+      return 1;
+   }
Quoted 8 lines

arch/x86/kvm/vmx.c static void guest_write_tsc(u64 guest_tsc, u64 host_tsc) · line 1065

Quoted 5 lines
arch/x86/kvm/vmx.c +4 -0
+
+   return 0;
+}
+

Need to export this msr to userspace for live migration. See msrs_to_save[].

OK.

Quoted 11 lines
Quoted 11 lines
arch/x86/kvm/vmx.c +9 -0
+/*
+ * Check to see if vcpu can execute vmx command
+ * Inject the corrseponding exception
+ */
+static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
+{
+   struct kvm_segment cs;
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+   struct kvm_msr_entry *msr;
Quoted 8 lines

arch/x86/kvm/vmx.c static int handle_vmcall(struct kvm_vcpu *vcpu) · line 3235

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+
+   vmx_get_segment(vcpu,&cs, VCPU_SREG_CS);
+
+   if (!vmx->nested.vmxon) {
+      printk(KERN_DEBUG "%s: vmx not on\n", __func__);

pr_debug

I will change it.

Quoted 12 lines

arch/x86/kvm/vmx.c static int handle_vmcall(struct kvm_vcpu *vcpu)

Quoted 10 lines
arch/x86/kvm/vmx.c +8 -0
+      kvm_queue_exception(vcpu, UD_VECTOR);
+      return 0;
+   }
+
+   msr = find_msr_entry(vmx, MSR_EFER);
+
+   if ((vmx_get_rflags(vcpu)&  X86_EFLAGS_VM) ||
+       ((msr->data&  EFER_LMA)&&  !cs.l)) {

is_long_mode()

I will change it.

Quoted 38 lines
Quoted 38 lines
  static int handle_vmx_insn(struct kvm_vcpu *vcpu)
  {
     kvm_queue_exception(vcpu, UD_VECTOR);
     return 1;
  }
arch/x86/kvm/vmx.c +31 -0
+static int handle_vmoff(struct kvm_vcpu *vcpu)
+{
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+   if (!nested_vmx_check_permission(vcpu))
+      return 1;
+
+   vmx->nested.vmxon = 0;
+
+   skip_emulated_instruction(vcpu);
+   return 1;
+}
+
+static int handle_vmon(struct kvm_vcpu *vcpu)
+{
+   struct kvm_segment cs;
+   struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+   if (!nested) {
+      printk(KERN_DEBUG "%s: nested vmx not enabled\n", __func__);
+      kvm_queue_exception(vcpu, UD_VECTOR);
+      return 1;
+   }
+
+   vmx_get_segment(vcpu,&cs, VCPU_SREG_CS);
+
+   if (!(vcpu->arch.cr4&  X86_CR4_VMXE) ||
+       !(vcpu->arch.cr0&  X86_CR0_PE) ||
+       (vmx_get_rflags(vcpu)&  X86_EFLAGS_VM)) {
+      kvm_queue_exception(vcpu, UD_VECTOR);
+      printk(KERN_INFO "%s invalid register state\n", __func__);
Quoted 8 lines

arch/x86/kvm/vmx.c static int handle_vmcall(struct kvm_vcpu *vcpu)

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+      return 1;
+   }
+#ifdef CONFIG_X86_64
+   if (((find_msr_entry(to_vmx(vcpu),
+              MSR_EFER)->data&  EFER_LMA)&&  !cs.l)) {

is_long_mode(), and you can avoid the #ifdef.

I will change it.

Quoted 3 lines

VMXON is supposed to block INIT, please add that (in a separate patch).

I will add it.

Quoted 5 lines

--
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.

#32 lore raw
In reply to [email protected]
Quoted 10 lines · On 10/15/2009 11:41 PM, [email protected] wrote:

Avi,
We have addressed all of the comments, please apply.

The following patches implement nested VMX support. The patches enable a guest to use the VMX APIs in order to run its own nested guest (i.e., enable running other hypervisors which use VMX under KVM). The current patches support running Linux under a nested KVM using shadow page table (with bypass_guest_pf disabled). SMP support was fixed. Reworking EPT support to mesh cleanly with the current shadow paging design per Avi's comments is a work-in-progress.

Why is bypass_guest_pf disabled?

Quoted 4 lines

The current patches only support a single nested hypervisor, which can only run a single guest (multiple guests are work in progress). Only 64-bit nested hypervisors are supported.

Multiple guests and 32-bit support are merge requirements. As far as I can tell there shouldn't be anything special required to support them?

Quoted 4 lines

vpid allocation will be updated with the multiguest support (work in progress). We are working on fixing the cr0.TS handling, it works for nested kvm by not for vmware server.

Please either drop or fix vpid before merging. What's wrong with cr0.ts? I'd like to see that fixed as well.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#33 lore raw

Avi Kivity <[email protected]> wrote on 20/10/2009 05:30:34:

Quoted 27 lines

From:

Avi Kivity <[email protected]>

To:

Orit Wasserman/Haifa/IBM@IBMIL

Cc:

[email protected], Ben-Ami Yassour1/Haifa/IBM@IBMIL, Abel Gordon/ Haifa/IBM@IBMIL, Muli Ben-Yehuda/Haifa/IBM@IBMIL, [email protected], [email protected]

Date:

20/10/2009 05:30

Subject:

Re: Nested VMX support v3

Quoted 4 lines · On 10/15/2009 11:41 PM, [email protected] wrote:

Avi,
We have addressed all of the comments, please apply.

The following patches implement nested VMX support. The patches

Quoted 9 lines

enable a guest

Quoted 1 line

to use the VMX APIs in order to run its own nested guest (i.e.,

enable running

Quoted 1 line

other hypervisors which use VMX under KVM). The current patches

support running

Quoted 2 lines

Linux under a nested KVM using shadow page table (with bypass_guest_pf disabled). SMP support was fixed. Reworking EPT support to mesh

cleanly with

Quoted 1 line

the current shadow paging design per Avi's comments is a

work-in-progress.

Quoted 3 lines
Quoted 1 line

Why is bypass_guest_pf disabled?

It was not implemented.
We need to modify the walk_addr code to handle the sptes that have invalid content (used only for the bypass_guest_pf
optimization) and identify them as not present. Maybe remove some other validity checks too.

Quoted 4 lines
Quoted 2 lines

The current patches only support a single nested hypervisor, which

can only run

Quoted 1 line

a single guest (multiple guests are work in progress). Only 64-bit

nested

Quoted 5 lines
Quoted 2 lines

hypervisors are supported.

Multiple guests and 32-bit support are merge requirements. As far as I can tell there shouldn't be anything special required to support them?

Ok.

Quoted 5 lines
Quoted 3 lines

vpid allocation will be updated with the multiguest support (work

in progress).

Quoted 1 line

We are working on fixing the cr0.TS handling, it works for nested kvm

by not

Quoted 5 lines
Quoted 2 lines

for vmware server.

Please either drop or fix vpid before merging. What's wrong with cr0.ts? I'd like to see that fixed as well.

Ok.

Quoted 5 lines

--
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.