Skip to contentSkip to conversation

Nested VMX, v5

PATCH In review Nadav Har'El 8 participants 147 messages Jun 13, 2010 – Oct 17, 2010 Page 2 of 2

Subsystems (4)
lore
Share & tools
lore
#110 lore raw

Re: [PATCH 18/24] Exiting from L2 to L1

In reply to Avi Kivity

On Mon, Jun 14, 2010, Avi Kivity wrote about "Re: [PATCH 18/24] Exiting from L2 to L1":

Quoted 4 lines

Review

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

IIUC vpids are not exposed to the guest yet? So the VPID should not change between guest and nested guest.

Right. Removed.

Quoted 11 lines

Review

Quoted 5 lines
(quoted diff) +3 -0
+
+	vmcs_write64(IO_BITMAP_A, src->io_bitmap_a);
+	vmcs_write64(IO_BITMAP_B, src->io_bitmap_b);

Why change the I/O bitmap?
...
Or the msr bitmap? After all, we're switching the entire vmcs?
...
Why write all these? What could have changed them?

You were right - most of these copies were utterly useless, and apparently remained in our code since prehistory (when the same hardware vmcs was reused for both L1 and L2). The great thing is that this removes several dozens of vmwrites from the L2->L1 exit path in one fell swoop. In fact, the whole function switch_back_vmcs() is now gone. Thanks for spotting this!

Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +5 -0
+	vmx_set_cr0(vcpu,
+		(vmx->nested.l1_shadow_vmcs->cr0_guest_host_mask&
+		vmx->nested.l1_shadow_vmcs->cr0_read_shadow) |
+		(~vmx->nested.l1_shadow_vmcs->cr0_guest_host_mask&
+		vmx->nested.l1_shadow_vmcs->guest_cr0));

Helper wanted.

Done. The new helper looks like this:

static inline unsigned long guest_readable_cr0(struct vmcs_fields *fields)

{
	return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
		(fields->cr0_read_shadow & fields->cr0_guest_host_mask);
}

And is used in two places in the code (the above place, and another one).

Quoted 11 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+		vmcs_write64(GUEST_PDPTR3, src->guest_pdptr3);
+	}

A kvm_set_cr3(src->host_cr3) should do all that and more, no?

Review

Quoted 4 lines
(quoted diff) +3 -0
+
+	vmx_set_cr4(vcpu, vmx->nested.l1_state.cr4);
+

Again, the kvm_set_crx() versions have more meat.

I have to admit, I still don't understand this part of the code completely. The fact that kvm_set_cr4 does more than vmx_set_cr4 doesn't always mean that we want (or need) to do those things. In particular:

Quoted 12 lines

Review

Quoted 9 lines
(quoted diff) +3 -0
+	if (enable_ept) {
+		vcpu->arch.cr3 = vmx->nested.l1_shadow_vmcs->guest_cr3;
+		vmcs_write32(GUEST_CR3, 

vmx->nested.l1_shadow_vmcs->guest_cr3);

(quoted diff) +3 -0
+	} else {
+		kvm_set_cr3(vcpu, vmx->nested.l1_state.cr3);
+	}

kvm_set_cr3() will load the PDPTRs in the EPT case (correctly in case the nested guest was able to corrupted the guest's PDPT).

kvm_set_cr3 calls vmx_set_cr3 which calls ept_load_pdptrs which assumes that vcpu->arch.pdptrs[] is correct. I am guessing (but am not yet completely sure) that this code tried to avoid assuming that this cache is up-to-date. Again, I still need to better understand this part of the code before I can correct it (because, as the saying goes, "if it ain't broken, don't fix it" - or at least fix it carefully).

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+	kvm_mmu_reset_context(vcpu);
+	kvm_mmu_load(vcpu);

kvm_mmu_load() unneeded, usually.

Again, I'll need to look into this deeper and report back. In the meantime, attached below is the current version of this patch.

Thanks,
Nadav.

Subject: [PATCH 19/26] nVMX: Exiting from L2 to L1

This patch implements nested_vmx_vmexit(), called when the nested L2 guest exits and we want to run its L1 parent and let it handle this exit.

Note that this will not necessarily be called on every L2 exit. L0 may decide to handle a particular exit on its own, without L1's involvement; In that case, L0 will handle the exit, and resume running L2, without running L1 and without calling nested_vmx_vmexit(). The logic for deciding whether to handle a particular exit in L1 or in L0, i.e., whether to call nested_vmx_vmexit(), will appear in the next patch.

---
 arch/x86/kvm/vmx.c |  242 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 241 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx.c +241 -1
@@ -4970,9 +4970,13 @@ static void vmx_complete_interrupts(stru
 	int type;
 	bool idtv_info_valid;
 
+	vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
+
+	if (vmx->nested.nested_mode)
+		return;
+
 	exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
 
-	vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
 
 	/* Handle machine checks before interrupts are enabled */
 	if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
@@ -5961,6 +5965,242 @@ static int nested_vmx_run(struct kvm_vcp
 	return 1;
 }
 
+/*
+ * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
+ * because L2 may have changed some cr0 bits directly (see CRO_GUEST_HOST_MASK)
+ * without L0 trapping the change and updating vmcs12.
+ * This function returns the value we should put in vmcs12.guest_cr0. It's not
+ * enough to just return the current (vmcs02) GUEST_CR0. This may not be the
+ * guest cr0 that L1 thought it was giving its L2 guest - it is possible that
+ * L1 wished to allow its guest to set a cr0 bit directly, but we (L0) asked
+ * to trap this change and instead set just the read shadow. If this is the
+ * case, we need to copy these read-shadow bits back to vmcs12.guest_cr0, where
+ * L1 believes they already are.
+ */
+static inline unsigned long
+vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs_fields *vmcs12)
+{
+	unsigned long guest_cr0_bits =
+		vcpu->arch.cr0_guest_owned_bits | vmcs12->cr0_guest_host_mask;
+	return (vmcs_readl(GUEST_CR0) & guest_cr0_bits) |
+		(vmcs_readl(CR0_READ_SHADOW) & ~guest_cr0_bits);
+}
+
+static inline unsigned long
+vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs_fields *vmcs12)
+{
+	unsigned long guest_cr4_bits =
+		vcpu->arch.cr4_guest_owned_bits | vmcs12->cr4_guest_host_mask;
+	return (vmcs_readl(GUEST_CR4) & guest_cr4_bits) |
+		(vmcs_readl(CR4_READ_SHADOW) & ~guest_cr4_bits);
+}
+
+/*
+ * prepare_vmcs12 is called when the nested L2 guest exits and we want to
+ * prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), and this
+ * function updates it to reflect the changes to the guest state while L2 was
+ * running (and perhaps made some exits which were handled directly by L0
+ * without going back to L1), and to reflect the exit reason.
+ * Note that we do not have to copy here all VMCS fields, just those that
+ * could have changed by the L2 guest or the exit - i.e., the guest-state and
+ * exit-information fields only. Other fields are modified by L1 with VMWRITE,
+ * which already writes to vmcs12 directly.
+ */
+void prepare_vmcs12(struct kvm_vcpu *vcpu)
+{
+	struct vmcs_fields *vmcs12 = get_vmcs12_fields(vcpu);
+
+	/* update guest state fields: */
+	vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
+	vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
+
+	vmcs12->guest_dr7 = vmcs_readl(GUEST_DR7);
+	vmcs12->guest_rsp = vmcs_readl(GUEST_RSP);
+	vmcs12->guest_rip = vmcs_readl(GUEST_RIP);
+	vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
+
+	vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
+	vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
+	vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
+	vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
+	vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
+	vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
+	vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
+	vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
+	vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
+	vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
+	vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
+	vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
+	vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
+	vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
+	vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
+	vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
+	vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
+	vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
+	vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
+	vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
+	vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
+	vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
+	vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
+	vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
+	vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
+	vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
+	vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
+	vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
+	vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
+	vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
+	vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
+	vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
+	vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
+	vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
+	vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
+	vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
+
+	/* TODO: These cannot have changed unless we have MSR bitmaps and
+	 * the relevant bit asks not to trap the change */
+	vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+	if (vmcs_config.vmentry_ctrl & VM_EXIT_SAVE_IA32_PAT)
+		vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
+	vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
+	vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
+	vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
+
+	vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
+	vmcs12->guest_interruptibility_info =
+		vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
+	vmcs12->guest_pending_dbg_exceptions =
+		vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
+	vmcs12->vmcs_link_pointer = vmcs_read64(VMCS_LINK_POINTER);
+
+	/* update exit information fields: */
+
+	vmcs12->vm_exit_reason  = vmcs_read32(VM_EXIT_REASON);
+	vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+
+	if(enable_ept){
+		vmcs12->guest_physical_address =
+			vmcs_read64(GUEST_PHYSICAL_ADDRESS);
+		vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
+	}
+
+	vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+	vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
+	vmcs12->idt_vectoring_info_field =
+		vmcs_read32(IDT_VECTORING_INFO_FIELD);
+	vmcs12->idt_vectoring_error_code =
+		vmcs_read32(IDT_VECTORING_ERROR_CODE);
+	vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
+	vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
+
+	/* clear vm-entry fields which are to be cleared on exit */
+	if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
+		vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
+}
+
+static int nested_vmx_vmexit(struct kvm_vcpu *vcpu, bool is_interrupt)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int efer_offset;
+	struct vmcs_fields *vmcs01 = vmx->nested.vmcs01_fields;
+
+	if (!vmx->nested.nested_mode) {
+		printk(KERN_INFO "WARNING: %s called but not in nested mode\n",
+		       __func__);
+		return 0;
+	}
+
+	sync_cached_regs_to_vmcs(vcpu);
+
+	prepare_vmcs12(vcpu);
+	if (is_interrupt)
+		get_vmcs12_fields(vcpu)->vm_exit_reason =
+			EXIT_REASON_EXTERNAL_INTERRUPT;
+
+	vmx->nested.current_vmcs12->launched = vmx->launched;
+	vmx->nested.current_vmcs12->cpu = vcpu->cpu;
+
+	vmx->vmcs = vmx->nested.vmcs01;
+	vcpu->cpu = vmx->nested.l1_state.cpu;
+	vmx->launched = vmx->nested.l1_state.launched;
+
+	vmx_vcpu_load(vcpu, get_cpu());
+	put_cpu();
+
+	vcpu->arch.efer = vmx->nested.l1_state.efer;
+	if ((vcpu->arch.efer & EFER_LMA) &&
+	    !(vcpu->arch.efer & EFER_SCE))
+		vcpu->arch.efer |= EFER_SCE;
+
+	efer_offset = __find_msr_index(vmx, MSR_EFER);
+	if (update_transition_efer(vmx, efer_offset))
+		wrmsrl(MSR_EFER, vmx->guest_msrs[efer_offset].data);
+	
+	/*
+	 * L2 perhaps switched to real mode and set vmx->rmode, but we're back
+	 * in L1 and as it is running VMX, it can't be in real mode.
+	 */
+	vmx->rmode.vm86_active = 0;
+
+	/*
+	 * We're running a regular L1 guest again, so we do the regular KVM
+	 * thing: run vmx_set_cr0 with the cr0 bits the guest thinks it has.
+	 * vmx_set_cr0 might use slightly different bits on the new guest_cr0
+	 * it sets, e.g., add TS when !fpu_active.
+	 * Note that vmx_set_cr0 refers to rmode and efer set above.
+	 */
+	vmx_set_cr0(vcpu, guest_readable_cr0(vmcs01));
+	/*
+	 * If we did fpu_activate()/fpu_deactive() during l2's run, we need to
+	 * apply the same changes to l1's vmcs. We just set cr0 correctly, but
+	 * now we need to also update cr0_guest_host_mask and exception_bitmap.
+	 */
+	vmcs_write32(EXCEPTION_BITMAP,
+		(vmx->nested.vmcs01_fields->exception_bitmap &
+			~(1u<<NM_VECTOR)) |
+			(vcpu->fpu_active ? 0 : (1u<<NM_VECTOR)));
+	vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
+	vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
+
+
+	vmx_set_cr4(vcpu, vmx->nested.l1_state.cr4);
+
+	if (enable_ept) {
+		vcpu->arch.cr3 = vmcs01->guest_cr3;
+		vmcs_write32(GUEST_CR3, vmcs01->guest_cr3);
+		vmcs_write64(EPT_POINTER, vmcs01->ept_pointer);
+		vmcs_write64(GUEST_PDPTR0, vmcs01->guest_pdptr0);
+		vmcs_write64(GUEST_PDPTR1, vmcs01->guest_pdptr1);
+		vmcs_write64(GUEST_PDPTR2, vmcs01->guest_pdptr2);
+		vmcs_write64(GUEST_PDPTR3, vmcs01->guest_pdptr3);
+	} else {
+		kvm_set_cr3(vcpu, vmx->nested.l1_state.cr3);
+	}
+
+	kvm_register_write(vcpu, VCPU_REGS_RSP,
+			   vmx->nested.vmcs01_fields->guest_rsp);
+	kvm_register_write(vcpu, VCPU_REGS_RIP,
+			   vmx->nested.vmcs01_fields->guest_rip);
+
+	vmx->nested.nested_mode = 0;
+
+	kvm_mmu_reset_context(vcpu);
+	kvm_mmu_load(vcpu);
+
+	if (unlikely(vmx->fail)) {
+		/*
+		 * When L1 launches L2 and then we (L0) fail to launch L2,
+		 * we nested_vmx_vmexit back to L1, but now should let it know
+		 * that the VMLAUNCH failed - with the same error that we
+		 * got when launching L2.
+		 */
+		vmx->fail = 0;
+		nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
+	} else
+		nested_vmx_succeed(vcpu);
+
+	return 0;
+}
+
 static struct kvm_x86_ops vmx_x86_ops = {
 	.cpu_has_kvm_support = cpu_has_kvm_support,
 	.disabled_by_bios = vmx_disabled_by_bios,
Signature · 4 lines
Nadav Har'El                        |       Sunday, Sep 12 2010, 4 Tishri 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Anyone is entitled to their own opinions.
http://nadav.harel.org.il           |No one is entitled to their own facts.
#113 lore raw

Re: [PATCH 19/24] Deciding if L0 or L1 should handle an L2 exit

In reply to Avi Kivity

On Mon, Jun 14, 2010, Avi Kivity wrote about "Re: [PATCH 19/24] Deciding if L0 or L1 should handle an L2 exit":

Quoted 9 lines · Nadav Har'El
Quoted 5 lines

This patch contains the logic of whether an L2 exit should be handled by L0 and then L2 should be resumed, or whether L1 should be run to handle this exit (using the nested_vmx_vmexit() function of the previous patch). The basic idea is to let L1 handle the exit only if it actually asked to trap this sort of event. For example, when L2 exits on a change to CR0,

...

Quoted 2 lines
(quoted diff) +0 -0
@@ -3819,6 +3841,8 @@ static int handle_exception(struct kvm_v
 
Quoted 10 lines

Review static int handle_exception(struct kvm_v · line 3848

Quoted 7 lines
(quoted diff) +2 -0
 	if (is_no_device(intr_info)) {
 		vmx_fpu_activate(vcpu);
+		if (vmx->nested.nested_mode)
+			vmx->nested.nested_run_pending = 1;
 		return 1;
 	}
 

Isn't this true for many other exceptions? #UD which we emulate (but the guest doesn't trap), page faults which we handle completely...

I was trying to think why nested_run_pending=1 (forcing us to run L2 next) is necessary in the specific case of #NM, and couldn't think of any convincing reason. Sure, in most cases we would like to continue running L2 after L0 serviced this exception that L1 didn't care about, but in the rare cases where L1 should run next (especially, user-space injecting an interrupt), what's so wrong with L1 running next?
And, like you said, if it's important for #NM, why not for #PF or other things?

Anyway, the code appears to run correctly also without this setting, so I'm guessing that it's an historic artifact, from older code which was written before the days of the lazy fpu loading. So I'm removing it.

Good catch. I was aware of this peculiar case in the code (and even documented it in the patch's description), but should have stopped to think if it is really such a special case, or simply an error. And now I believe it was nothing more than an error.

Quoted 8 lines
Quoted 7 lines
(quoted diff) +7 -0
+/* Return 1 if we should exit from L2 to L1 to handle a CR access exit,
+ * rather than handle it ourselves in L0. I.e., check if L1 wanted to
+ * intercept (via guest_host_mask etc.) the current event.
+ */
+static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
+	struct shadow_vmcs *l2svmcs)
+{

...

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+		case 8:
+			if (l2svmcs->cpu_based_vm_exec_control&
+			    CPU_BASED_CR8_LOAD_EXITING)
+				return 1;

Should check TPR threshold here too if enabled.

I'll return to this issue in another mail. This one is getting long enough.

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+	case 3: /* lmsw */
+		if (l2svmcs->cr0_guest_host_mask&
+		    (val ^ l2svmcs->cr0_read_shadow))
+			return 1;

Need to mask off bit 0 (cr0.pe) of val, since lmsw can't clear it.

Right. Also, lmsw only works on the first 4 bits of cr0: The first bit, it can only turn on (like you said), and the next 3 bits, it can change at will. Any other attempted changes to cr0 through lmsw should be ignored, and not cause exits. So I changed the code to this:

		if (vmcs12->cr0_guest_host_mask & 0xe &
		    (val ^ vmcs12->cr0_read_shadow))
			return 1;
		if ((vmcs12->cr0_guest_host_mask & 0x1) &&
		    !(vmcs12->cr0_read_shadow & 0x1) &&
		    (val & 0x1))
		    	return 1;

I wonder if there's a less ugly way to write the same thing...

This LMSW is so 80s :( I wonder who's using it these days, and specifically if it would bother anyone if lmsw suddenly acquired new "abilities" to change bits it never could... Oh, the things that we do for backward compatibility :-)

Quoted 6 lines
Quoted 5 lines

+/* Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
+ * should handle it ourselves in L0. Only call this when in nested_mode (L2).
+ */
+static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu, bool afterexit)

...

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +5 -0
+	case EXIT_REASON_EXCEPTION_NMI:
+		if (is_external_interrupt(intr_info)&&
+		    (l2svmcs->pin_based_vm_exec_control&
+		     PIN_BASED_EXT_INTR_MASK))
+			r = 1;

A real external interrupt should never be handled by the guest, only a virtual external interrupt.

You've opened a whole can of worms ;-) But it's very good that you did.

It appears that this nested_vmx_exit_handled() was called for two completely different reasons, with different "afterexit" parameter (if you remember, this flag had the puzzling name "kvm_override" in a previous version). On normal exits, it is called with afterexit=1 and did exactly what you wanted, i.e., never handle in the guest:

		case EXIT_REASON_EXTERNAL_INTERRUPT:
			return 0;

The case which you saw was only relevant for the other place this function is called - in exception injection. But most of the code in this function was irrelevant and/or plain wrong in this case.

This part of the code was just terrible, and I couldn't leave it like this, even if it was working (I'm sure you'll agree). So I now completely rewrote this function to become two separate functions, with (hopefully) no irrelevant or wrong code.

Here is the new version of the two relevant patches - this one and the one dealing with exception injection:

Subject: [PATCH 20/26] nVMX: Deciding if L0 or L1 should handle an L2 exit

This patch contains the logic of whether an L2 exit should be handled by L0 and then L2 should be resumed, or whether L1 should be run to handle this exit (using the nested_vmx_vmexit() function of the previous patch).

The basic idea is to let L1 handle the exit only if it actually asked to trap this sort of event. For example, when L2 exits on a change to CR0, we check L1's CR0_GUEST_HOST_MASK to see if L1 expressed interest in any bit which changed; If it did, we exit to L1. But if it didn't it means that it is we (L0) that wished to trap this event, so we handle it ourselves.

The next two patches add additional logic of what to do when an interrupt or exception is injected: Does L0 need to do it, should we exit to L1 to do it, or should we resume L2 and keep the exception to be injected later.

We keep a new flag, "nested_run_pending", which can override the decision of which should run next, L1 or L2. nested_run_pending=1 means that we *must* run L2 next, not L1. This is necessary in several situations where had L1 run on bare metal it would not have expected to be resumed at this stage. One example is when L1 did a VMLAUNCH of L2 and therefore expects L2 to be run. Nested_run_pending is especially intended to avoid switching to L1 in the injection decision-point described above.

---
 arch/x86/kvm/vmx.c |  204 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 204 insertions(+)
arch/x86/kvm/vmx.c +204 -0
@@ -338,6 +338,8 @@ struct nested_vmx {
 	/* Saving the VMCS that we used for running L1 */
 	struct vmcs *vmcs01;
 	struct vmcs_fields *vmcs01_fields;
+	/* L2 must run next, and mustn't decide to exit to L1. */
+	bool nested_run_pending;
 };
 
 struct vcpu_vmx {
@@ -848,6 +850,20 @@ static inline bool nested_cpu_has_vmx_ep
 		SECONDARY_EXEC_ENABLE_EPT);
 }
 
+static inline bool nested_cpu_has_vmx_msr_bitmap(struct kvm_vcpu *vcpu)
+{
+	return get_vmcs12_fields(vcpu)->cpu_based_vm_exec_control &
+		CPU_BASED_USE_MSR_BITMAPS;
+}
+
+static inline bool 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 int nested_vmx_vmexit(struct kvm_vcpu *vcpu, bool is_interrupt);
+
 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
 {
 	int i;
@@ -4879,6 +4895,182 @@ static const int kvm_vmx_max_exit_handle
 	ARRAY_SIZE(kvm_vmx_exit_handlers);
 
 /*
+ * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
+ * rather than handle it ourselves in L0. I.e., check L1's MSR bitmap whether
+ * it expressed interest in the current event (read or write a specific MSR).
+ */
+static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
+	struct vmcs_fields *vmcs12, u32 exit_reason)
+{
+	u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
+	struct page *msr_bitmap_page;
+	void *va;
+	bool ret;
+
+	if (!cpu_has_vmx_msr_bitmap() || !nested_cpu_has_vmx_msr_bitmap(vcpu))
+		return 1;
+
+	msr_bitmap_page = nested_get_page(vcpu, vmcs12->msr_bitmap);
+	if (!msr_bitmap_page) {
+		printk(KERN_INFO "%s error in nested_get_page\n", __func__);
+		return 0;
+	}
+
+	va = kmap_atomic(msr_bitmap_page, KM_USER1);
+	if (exit_reason == EXIT_REASON_MSR_WRITE)
+		va += 0x800;
+	if (msr_index >= 0xc0000000) {
+		msr_index -= 0xc0000000;
+		va += 0x400;
+	}
+	if (msr_index > 0x1fff)
+		return 0;
+	ret = test_bit(msr_index, va);
+	kunmap_atomic(va, KM_USER1);
+	return ret;
+}
+
+/*
+ * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
+ * rather than handle it ourselves in L0. I.e., check if L1 wanted to
+ * intercept (via guest_host_mask etc.) the current event.
+ */
+static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
+	struct vmcs_fields *vmcs12)
+{
+	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 (vmcs12->cr0_guest_host_mask &
+			    (val ^ vmcs12->cr0_read_shadow))
+				return 1;
+			break;
+		case 3:
+			if (vmcs12->cpu_based_vm_exec_control &
+			    CPU_BASED_CR3_LOAD_EXITING)
+				return 1;
+			break;
+		case 4:
+			if (vmcs12->cr4_guest_host_mask &
+			    (vmcs12->cr4_read_shadow ^ val))
+				return 1;
+			break;
+		case 8:
+			if (vmcs12->cpu_based_vm_exec_control &
+			    CPU_BASED_CR8_LOAD_EXITING)
+				return 1;
+			// TODO: missing else if control & CPU_BASED_TPR_SHADOW
+			//   then set tpr shadow and if below tpr_threshold,
+			//   exit.
+			break;
+		}
+		break;
+	case 2: /* clts */
+		if (vmcs12->cr0_guest_host_mask & X86_CR0_TS)
+			return 1;
+		break;
+	case 1: /* mov from cr */
+		switch (cr) {
+		case 0:
+			return 1;
+		case 3:
+			if (vmcs12->cpu_based_vm_exec_control &
+			    CPU_BASED_CR3_STORE_EXITING)
+				return 1;
+			break;
+		case 4:
+			return 1;
+			break;
+		case 8:
+			if (vmcs12->cpu_based_vm_exec_control &
+			    CPU_BASED_CR8_STORE_EXITING)
+				return 1;
+			break;
+		}
+		break;
+	case 3: /* lmsw */
+		/*
+		 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
+		 * cr0. Other attempted changes are ignored, with no exit.
+		 */
+		if (vmcs12->cr0_guest_host_mask & 0xe &
+		    (val ^ vmcs12->cr0_read_shadow))
+			return 1;
+		if ((vmcs12->cr0_guest_host_mask & 0x1) &&
+		    !(vmcs12->cr0_read_shadow & 0x1) &&
+		    (val & 0x1))
+		    	return 1;
+		break;
+	}
+	return 0;
+}
+
+/*
+ * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
+ * should handle it ourselves in L0 (and then continue L2). Only call this
+ * when in nested_mode (L2).
+ */
+static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
+{
+	u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
+	u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	struct vmcs_fields *vmcs12 = get_vmcs12_fields(vcpu);
+
+	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;
+	}
+
+	switch (exit_reason) {
+	case EXIT_REASON_EXTERNAL_INTERRUPT:
+		return 0;
+	case EXIT_REASON_EXCEPTION_NMI:
+		if (!is_exception(intr_info))
+			return 0;
+		else if (is_page_fault(intr_info) && (!enable_ept))
+			return 0;
+		return (vmcs12->exception_bitmap &
+				(1u << (intr_info & INTR_INFO_VECTOR_MASK)));
+	case EXIT_REASON_EPT_VIOLATION:
+		return 0;
+	case EXIT_REASON_INVLPG:
+		return (vmcs12->cpu_based_vm_exec_control &
+				CPU_BASED_INVLPG_EXITING);
+	case EXIT_REASON_MSR_READ:
+	case EXIT_REASON_MSR_WRITE:
+		return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
+	case EXIT_REASON_CR_ACCESS:
+		return nested_vmx_exit_handled_cr(vcpu, vmcs12);
+	case EXIT_REASON_DR_ACCESS:
+		return (vmcs12->cpu_based_vm_exec_control &
+				CPU_BASED_MOV_DR_EXITING);
+	default:
+		/*
+		 * One particularly interesting case that is covered here is an
+		 * exit caused by L2 running a VMX instruction. L2 is guest
+		 * mode in L1's world, and according to the VMX spec running a
+		 * VMX instruction in guest mode should cause an exit to root
+		 * mode, i.e., to L1. This is why we need to return r=1 for
+		 * those exit reasons too. This enables further nesting: Like
+		 * L0 emulates VMX for L1, we now allow L1 to emulate VMX for
+		 * L2, who will then be able to run L3.
+		 */
+		return 1;
+	}
+}
+
+/*
  * The guest has exited.  See if we can fix it or if we need userspace
  * assistance.
  */
@@ -4894,6 +5086,17 @@ static int vmx_handle_exit(struct kvm_vc
 	if (vmx->emulation_required && emulate_invalid_guest_state)
 		return handle_invalid_guest_state(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)) {
+		nested_vmx_vmexit(vcpu, false);
+		return 1;
+	}
+
 	/* Access CR3 don't cause VMExit in paging mode, so we need
 	 * to sync with guest real CR3. */
 	if (enable_ept && is_paging(vcpu))
@@ -5941,6 +6144,7 @@ static int nested_vmx_run(struct kvm_vcp
 		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);
 			nested_vmx_failValid(vcpu,
 				VMXERR_VMRESUME_CORRUPTED_VMCS /* ? */);
 			/* switch back to L1 */

Subject: [PATCH 22/26] nVMX: Correct handling of exception injection

Similar to the previous patch, but concerning injection of exceptions rather than external interrupts.

---
 arch/x86/kvm/vmx.c |   26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
arch/x86/kvm/vmx.c +26 -0
@@ -1507,6 +1507,25 @@ static void skip_emulated_instruction(st
 	vmx_set_interrupt_shadow(vcpu, 0);
 }
 
+/*
+ * KVM wants to inject page-faults which it got to the guest. This function
+ * checks whether in a nested guest, we need to inject them to L1 or L2.
+ * This function assumes it is called with the exit reason in vmcs02 being
+ * a #PF exception (this is the only case in which KVM injects a #PF when L2
+ * is running).
+ */
+static int nested_pf_handled(struct kvm_vcpu *vcpu)
+{
+	struct vmcs_fields *vmcs12 = get_vmcs12_fields(vcpu);
+
+	/* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
+	if (!(vmcs12->exception_bitmap & PF_VECTOR))
+		return 0;
+
+	nested_vmx_vmexit(vcpu, false);
+	return 1;
+}
+
 static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
 				bool has_error_code, u32 error_code,
 				bool reinject)
@@ -1514,6 +1533,10 @@ static void vmx_queue_exception(struct k
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	u32 intr_info = nr | INTR_INFO_VALID_MASK;
 
+	if (nr == PF_VECTOR && vmx->nested.nested_mode &&
+		nested_pf_handled(vcpu))
+		return;
+
 	if (has_error_code) {
 		vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
 		intr_info |= INTR_INFO_DELIVER_CODE_MASK;
@@ -3554,6 +3577,9 @@ static void vmx_inject_nmi(struct kvm_vc
 {
 	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
Signature · 4 lines
Nadav Har'El                        |      Tuesday, Sep 14 2010, 6 Tishri 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |echo '[q]sa[ln0=aln256%Pln256/snlbx]
http://nadav.harel.org.il           |sb3135071790101768542287578439snlbxq'|dc
#117 lore raw

Re: [PATCH 20/24] Correct handling of interrupt injection

In reply to Avi Kivity

On Mon, Jun 14, 2010, Avi Kivity wrote about "Re: [PATCH 20/24] Correct handling of interrupt injection":

Quoted 8 lines

..

Quoted 4 lines

However, properly doing what is described above requires invasive changes to
the flow of the existing code, which we elected not to do in this stage. Instead we do something more simplistic and less efficient: we modify

...

That's a little sad.

I agree. I'd like to change this code to do the proper thing (as I explained in the patch's description), but as I said this will require some invasive changes to existing KVM code outside vmx.c.
So seeing that this existing code also works, and despite hurting performance a bit there are much more pressing performance issues (namely, the need for nested EPT) - with your permission I'd like to postpone fixing this issue.

Quoted 4 lines
Quoted 4 lines

static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) {

(quoted diff) +2 -0
+	if (to_vmx(vcpu)->nested.nested_mode&&  nested_exit_on_intr(vcpu)) {
+		if (to_vmx(vcpu)->nested.nested_run_pending)
Quoted 8 lines

arch/x86/kvm/vmx.c static int nested_vmx_vmexit(struct kvm_ · line 3747

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+			return 0;
+		nested_vmx_vmexit(vcpu, true);
+		/* fall through to normal code, but now in L1, not L2 */
+	}
+

What exit is reported here?

When nested_vmx_vmexit is called with the second parameter true, as above,
it modifies the (vmcs12) exit reason to be EXIT_REASON_EXTERNAL_INTERRUPT.
A hack, but it does the right thing in this case because L1 doesn't even get a chance to care about this exit reason before it exits again (as I tried to explain in the patch's description).

Signature · 4 lines
Nadav Har'El                        |     Thursday, Sep 16 2010, 8 Tishri 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Debugging: the art of removing bugs.
http://nadav.harel.org.il           |Programming: the art of inserting them.
#96 lore raw

Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME

In reply to Gleb Natapov

On Thu, Jun 17, 2010, Gleb Natapov wrote about "Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME":

Quoted 6 lines

arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcp · line 4399

Quoted 3 lines
arch/x86/kvm/vmx.c +3 -0
+static int handle_launch_or_resume(struct kvm_vcpu *vcpu, bool launch)
+{
+	if (!nested_vmx_check_permission(vcpu))

...
Should also check MOV SS blocking. Why Intel decided that vm entry should fail in this case? How knows, but spec says so.

Thanks. Added the check:

if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_MOV_SS){
	nested_vmx_failValid(vcpu,
		VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
	skip_emulated_instruction(vcpu);
	return 1;
}

Like you, I don't understand why this test is at all necessary...

Signature · 4 lines
Nadav Har'El                        |     Thursday, Sep 16 2010, 8 Tishri 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Help Microsoft stamp out piracy. Give
http://nadav.harel.org.il           |Linux to a friend today!
#121 lore raw

Re: [PATCH 22/24] Correct handling of idt vectoring info

In reply to Gleb Natapov

On Thu, Jun 17, 2010, Gleb Natapov wrote about "Re: [PATCH 22/24] Correct handling of idt vectoring info":

Quoted 6 lines · Nadav Har'El
Quoted 2 lines

In the normal non-nested case, the idt_vectoring_info case is treated after the exit. However, in the nested case a decision of whether to return to L2

This is not correct. On the normal non-nested case the idt_vectoring_info is parsed into vmx/svm independent data structure (which is saved/restored during VM migartion) after exit. The reinjection happens on vmentry path.

You're right that perhaps I overstated the difference between the nested and normal case. In both cases, the code at the end of vmx_vcpu_run just remembers what to do, and the actual injection (namely, setting the vmcs) happens in the beginning of the next entry. I'll change the wording of the patch's description to make it more accurate.

Quoted 5 lines

Why can't you do that using existing exception/nmi/interrupt queues that we have, but instead you effectively disable vmx_complete_interrupts() by patch 18 when in nested mode and add logically same code in this patch. I.e after exit you save info about idt event into nested_vmx and reinject it on vm entry.

This is an interesting point.

The basic problem is that (as I explained in the patch's description) when L2 exits to L1 with idt vectoring info, L0 should *not* do its normal thing of injecting the event - it should basically do nothing, and just leave the IDT_VECTORING_INFO_FIELD in vmcs12 for L1 to find and act upon. So in this case we must eliminate the normal decision that KVM would make to inject the event.

Perhaps it would have been possible to leave the decision as-is (i.e., not change vmx_complete_interrupts()), but instead disable the injection itself in inject_pending_event() (in x86.c, not vmx.c) or in all of vmx_queue_exception, vmx_set_nmi and vmx_set_irq. But I'm not sure this will be a cleaner patch (and I'd especially like to avoid nested-specific changes in x86.c), and I'm pretty sure that however I change this code, it's bound to break in subtle ways. The current patch took some blood, toil, tears and sweat (well, maybe everything except the blood...) of my coworkers to get right :-)

Nadav.

Signature · 4 lines
Nadav Har'El                        |      Sunday, Sep 19 2010, 12 Tishri 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Life is what happens to you while you're
http://nadav.harel.org.il           |busy making other plans. - John Lennon
#122 lore raw

Re: [PATCH 22/24] Correct handling of idt vectoring info

Quoted 6 lines · Nadav Har'El
Quoted 5 lines

Why can't you do that using existing exception/nmi/interrupt queues that we have, but instead you effectively disable vmx_complete_interrupts() by patch 18 when in nested mode and add logically same code in this patch. I.e after exit you save info about idt event into nested_vmx and reinject it on vm entry.

Quoted 9 lines

This is an interesting point.

The basic problem is that (as I explained in the patch's description) when L2 exits to L1 with idt vectoring info, L0 should *not* do its normal thing of injecting the event - it should basically do nothing, and just leave the IDT_VECTORING_INFO_FIELD in vmcs12 for L1 to find and act upon. So in this case we must eliminate the normal decision that KVM would make to inject the event.

But your code disables normal path even if L0 is the one who should handle exit and re-inject event into L2. Look at what nested SVM is doing. It is checking in handle_exit() if vmexit should cause vmexit into L1 and if so they bypass regular code path by emulating exit instead, but if L0 should handle the vmexit it uses regular code path.

Quoted 10 lines

Perhaps it would have been possible to leave the decision as-is (i.e., not change vmx_complete_interrupts()), but instead disable the injection itself in inject_pending_event() (in x86.c, not vmx.c) or in all of vmx_queue_exception, vmx_set_nmi and vmx_set_irq. But I'm not sure this will be a cleaner patch (and I'd especially like to avoid nested-specific changes in x86.c), and I'm pretty sure that however I change this code, it's bound to break in subtle ways. The current patch took some blood, toil, tears and sweat (well, maybe everything except the blood...) of my coworkers to get right :-)

Look at how SVM did it. VMX shouldn't be different.

--
Gleb.

#123 lore raw

Re: [PATCH 22/24] Correct handling of idt vectoring info

On Mon, Sep 20, 2010, Gleb Natapov wrote about "Re: [PATCH 22/24] Correct handling of idt vectoring info":

Quoted 5 lines

But your code disables normal path even if L0 is the one who should handle exit and re-inject event into L2. Look at what nested SVM is doing. It is checking in handle_exit() if vmexit should cause vmexit into L1 and if so they bypass regular code path by emulating exit instead, but if L0 should handle the vmexit it uses regular code path.

I wish it could be this simple. In vmx.c, we unfortunately don't have one such decision point (of when to exit into L1), but two: one is the exit handling (like in svm), but there's another one in the injection path (vmx_queue_exception): namely, when KVM decides to injects a #PF (because the guest, not it, should have gotten this #PF), we also need to exit to L1, and we only discover this in the entry path, not the exit path.

We could have changed the code to do this special PF handling not in the entrance but rather at the point at the exit when this event is being queued. We probably should. But I'm afraid that this would require quite a bit of changes in the non-nested vmx (and possibly x86) code, which we wanted to avoid making. I'm also afraid that I don't understand all the reasons that brought to the current situation :-(

Quoted 1 line

Look at how SVM did it. VMX shouldn't be different.

I'm afraid I know very little about the SVM architecture. Does SVM even have a parallel of the IDT_VECTORING_INFO that this patch is trying to address?

I agree that the nested SVM's handle_exit() looks cleaner that the parallel code in nested VMX. The root of all evil is that second exit decision point in the injection phase, and I'll think some more if I can find a way to avoid it without rocking the foundations too much.

Signature · 4 lines
Nadav Har'El                        |      Monday, Sep 20 2010, 12 Tishri 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Bore, n.: A person who talks when you
http://nadav.harel.org.il           |wish him to listen.
#124 lore raw

Re: [PATCH 22/24] Correct handling of idt vectoring info

Quoted 7 lines · Nadav Har'El

On Mon, Sep 20, 2010, Gleb Natapov wrote about "Re: [PATCH 22/24] Correct handling of idt vectoring info":

Quoted 5 lines

But your code disables normal path even if L0 is the one who should handle exit and re-inject event into L2. Look at what nested SVM is doing. It is checking in handle_exit() if vmexit should cause vmexit into L1 and if so they bypass regular code path by emulating exit instead, but if L0 should handle the vmexit it uses regular code path.

Quoted 6 lines

I wish it could be this simple. In vmx.c, we unfortunately don't have one such decision point (of when to exit into L1), but two: one is the exit handling (like in svm), but there's another one in the injection path (vmx_queue_exception): namely, when KVM decides to injects a #PF (because the guest, not it, should have gotten this #PF), we also need to exit to L1, and we only discover this in the entry path, not the exit path.

This is not specific to #PF, we queue other exceptions as well, for example #GP and #UD. They all need to be checked against EXCEPTION_BITMAP.

Quoted 6 lines

We could have changed the code to do this special PF handling not in the entrance but rather at the point at the exit when this event is being queued. We probably should. But I'm afraid that this would require quite a bit of changes in the non-nested vmx (and possibly x86) code, which we wanted to avoid making. I'm also afraid that I don't understand all the reasons that brought to the current situation :-(

Maybe add a queue (like the exception queue) to hold those pending exits?

Then kvm_queue_exception() could check of an intercept and queue a vmexit instead.

Quoted 4 lines
Quoted 1 line

Look at how SVM did it. VMX shouldn't be different.

I'm afraid I know very little about the SVM architecture. Does SVM even have a parallel of the IDT_VECTORING_INFO that this patch is trying to address?

It does. exit_int_info.

Quoted 5 lines

I agree that the nested SVM's handle_exit() looks cleaner that the parallel code in nested VMX. The root of all evil is that second exit decision point in the injection phase, and I'll think some more if I can find a way to avoid it without rocking the foundations too much.

I think svm needs it too.

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

Re: [PATCH 22/24] Correct handling of idt vectoring info

In reply to Nadav Har'El
Quoted 7 lines · Nadav Har'El

On Mon, Sep 20, 2010, Gleb Natapov wrote about "Re: [PATCH 22/24] Correct handling of idt vectoring info":

Quoted 5 lines

But your code disables normal path even if L0 is the one who should handle exit and re-inject event into L2. Look at what nested SVM is doing. It is checking in handle_exit() if vmexit should cause vmexit into L1 and if so they bypass regular code path by emulating exit instead, but if L0 should handle the vmexit it uses regular code path.

Quoted 7 lines

I wish it could be this simple. In vmx.c, we unfortunately don't have one such decision point (of when to exit into L1), but two: one is the exit handling (like in svm), but there's another one in the injection path (vmx_queue_exception): namely, when KVM decides to injects a #PF (because the guest, not it, should have gotten this #PF), we also need to exit to L1, and we only discover this in the entry path, not the exit path.

SVM has exactly same problem. What they do is in svm_queue_exception() they check if exception should generate vmexit and if so they set svm->nested.exit_required flag. They skip next vmentry if the flag is set and proceed directly to handle_exit() where they check this flag one again and emulate nested vmexit. Their nested vmexit emulation clears exception/interrupt queue.

Quoted 7 lines

We could have changed the code to do this special PF handling not in the entrance but rather at the point at the exit when this event is being queued. We probably should. But I'm afraid that this would require quite a bit of changes in the non-nested vmx (and possibly x86) code, which we wanted to avoid making. I'm also afraid that I don't understand all the reasons that brought to the current situation :-(

Nested SVM managed to do it without to much hassle. There is reinject logic in svm_queue_exception() that I still do not understand, but the same logic should be applicable to VMX too since SVM and VMX way of doing virtualization is very similar.

Quoted 4 lines
Quoted 1 line

Look at how SVM did it. VMX shouldn't be different.

I'm afraid I know very little about the SVM architecture. Does SVM even have a parallel of the IDT_VECTORING_INFO that this patch is trying to address?

Exactly same. The way events injection work in SVM and VMX are similar. This allow us to maintain most of the logic in common code.

Quoted 6 lines

I agree that the nested SVM's handle_exit() looks cleaner that the parallel code in nested VMX. The root of all evil is that second exit decision point in the injection phase, and I'll think some more if I can find a way to avoid it without rocking the foundations too much.

--
Gleb.

#125 lore raw

Re: [PATCH 22/24] Correct handling of idt vectoring info

In reply to Avi Kivity

On Mon, Sep 20, 2010, Avi Kivity wrote about "Re: [PATCH 22/24] Correct handling of idt vectoring info":

Quoted 5 lines
Quoted 3 lines

I'm afraid I know very little about the SVM architecture. Does SVM even have
a parallel of the IDT_VECTORING_INFO that this patch is trying to address?

It does. exit_int_info.

Thanks. I guess I need to do some serious reading on this subject. I guessed that exit_int_info was more of a parallel of VMX's vm_exit_intr_info field and not idt_vectoring_info, but I guess I was wrong.

Quoted 7 lines
Quoted 5 lines

I agree that the nested SVM's handle_exit() looks cleaner that the parallel code in nested VMX. The root of all evil is that second exit decision point in the injection phase, and I'll think some more if I can find a way to avoid it without rocking the foundations too much.

I think svm needs it too.

Can you please clarify? I didn't understand what "it" refers to here.

Thanks,
Nadav.

Signature · 4 lines
Nadav Har'El                        |    Thursday, Sep 23 2010, 15 Tishri 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Birthdays are good for you - the more you
http://nadav.harel.org.il           |have the longer you live.
#91 lore raw

Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME

In reply to Avi Kivity

On Mon, Jun 14, 2010, Avi Kivity wrote about "Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME":

Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/vmx.c +7 -0
+	vmx_set_cr0(vcpu,
+		(get_shadow_vmcs(vcpu)->guest_cr0&
+			~get_shadow_vmcs(vcpu)->cr0_guest_host_mask) |
+		(get_shadow_vmcs(vcpu)->cr0_read_shadow&
+			get_shadow_vmcs(vcpu)->cr0_guest_host_mask));
+
+	/* However, vmx_set_cr0 incorrectly enforces KVM's relationship 

between

(quoted diff) +1 -0
+	 * GUEST_CR0 and CR0_READ_SHADOW, e.g., that the former is the same 

as

arch/x86/kvm/vmx.c +2 -0
+	 * the latter with with TS added if !fpu_active. We need to take the
+	 * actual GUEST_CR0 that L1 wanted, just with added TS if !fpu_active
Quoted 8 lines

arch/x86/kvm/vmx.c int prepare_vmcs_02(struct kvm_vcpu *vcp · line 5935

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+	 * like KVM wants (for the "lazy fpu" feature, to avoid the costly
+	 * restoration of fpu registers until the FPU is really used).
+	 */
+	vmcs_writel(GUEST_CR0, get_shadow_vmcs(vcpu)->guest_cr0 |
+		(vcpu->fpu_active ? 0 : X86_CR0_TS));

Please update vmx_set_cr0() instead.

How would you like that I do that?
I could split vmx_set_cr0(vcpu, cr0) into a __vmx_set_cr0(vcpu, cr0, hw_cr0) and vmx_set_cr0 that calls it. Is this what you had in mind? Won't it be a little ugly? I agree, though, that it will avoid the vmwriting GUEST_CR0 twice in the nested case.

Quoted 10 lines

arch/x86/kvm/vmx.c int prepare_vmcs_02(struct kvm_vcpu *vcp · line 5946

Quoted 7 lines
arch/x86/kvm/vmx.c +6 -0
+	/* we have to set the X86_CR0_PG bit of the cached cr0, because
+	 * kvm_mmu_reset_context enables paging only if X86_CR0_PG is set in
+	 * CR0 (we need the paging so that KVM treat this guest as a paging
+	 * guest so we can easly forward page faults to L1.)
+	 */
+	vcpu->arch.cr0 |= X86_CR0_PG;

Since this version doesn't support unrestricted nested guests, cr0.pg will be already set or we will have failed vmentry.

I believe without this "hack", things didn't work properly during boot of L2, when cr0_read_shadow.pg was not yet set. I think PG is set in guest_cr0, but not in cr0_read_shadow, which is what vcpu->arch.cr0 caches.

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	if (enable_ept&&  !nested_cpu_has_vmx_ept(vcpu)) {

We don't support nested ept yet, yes?

Right. It seems like this (and a couple of other places) were left from our internal codebase (which did have nested ept). I'll clean it up.

Signature · 4 lines
Nadav Har'El                        |      Monday, Sep 20 2010, 12 Tishri 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |The two rules for success are: 1. Never
http://nadav.harel.org.il           |tell them everything you know.
#92 lore raw

Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME

Quoted 20 lines · Nadav Har'El

On Mon, Jun 14, 2010, Avi Kivity wrote about "Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME":

Quoted 19 lines
Quoted 18 lines
arch/x86/kvm/vmx.c +7 -0
+	vmx_set_cr0(vcpu,
+		(get_shadow_vmcs(vcpu)->guest_cr0&
+			~get_shadow_vmcs(vcpu)->cr0_guest_host_mask) |
+		(get_shadow_vmcs(vcpu)->cr0_read_shadow&
+			get_shadow_vmcs(vcpu)->cr0_guest_host_mask));
+
+	/* However, vmx_set_cr0 incorrectly enforces KVM's relationship

between

(quoted diff) +1 -0
+	 * GUEST_CR0 and CR0_READ_SHADOW, e.g., that the former is the same

as

arch/x86/kvm/vmx.c +7 -0
+	 * the latter with with TS added if !fpu_active. We need to take the
+	 * actual GUEST_CR0 that L1 wanted, just with added TS if !fpu_active
+	 * like KVM wants (for the "lazy fpu" feature, to avoid the costly
+	 * restoration of fpu registers until the FPU is really used).
+	 */
+	vmcs_writel(GUEST_CR0, get_shadow_vmcs(vcpu)->guest_cr0 |
+		(vcpu->fpu_active ? 0 : X86_CR0_TS));
Quoted 7 lines
Quoted 1 line

Please update vmx_set_cr0() instead.

How would you like that I do that?
I could split vmx_set_cr0(vcpu, cr0) into a __vmx_set_cr0(vcpu, cr0, hw_cr0) and vmx_set_cr0 that calls it. Is this what you had in mind? Won't it be a little ugly? I agree, though, that it will avoid the vmwriting GUEST_CR0 twice in the nested case.

Just move the extra calculations into vmx_set_cr0(). Check if you're in nested mode, and if so apply cr0_guest_host_mask.

The vmlaunch/vmresume code becomes kvm_set_cr0(vcpu, get_shadow_vmcs(vcpu)->guest_cr0).

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
arch/x86/kvm/vmx.c +6 -0
+	/* we have to set the X86_CR0_PG bit of the cached cr0, because
+	 * kvm_mmu_reset_context enables paging only if X86_CR0_PG is set in
+	 * CR0 (we need the paging so that KVM treat this guest as a paging
+	 * guest so we can easly forward page faults to L1.)
+	 */
+	vcpu->arch.cr0 |= X86_CR0_PG;
Quoted 6 lines
Quoted 2 lines

Since this version doesn't support unrestricted nested guests, cr0.pg will be already set or we will have failed vmentry.

I believe without this "hack", things didn't work properly during boot of L2, when cr0_read_shadow.pg was not yet set. I think PG is set in guest_cr0, but not in cr0_read_shadow, which is what vcpu->arch.cr0 caches.

I don't see how vcpu->arch.cr0 can cache cr0_read_shadow. All the mmu calculations depend on vcpu->arch.cr0, which must be what the processor uses for translations. cr0_read_shadow is only use to emulate read access to cr0 (note we need to both update the real CR0_READ_SHADOW, and to consider the virtual CR0_READ_SHADOW when emulating).

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

Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME

On Sun, Sep 26, 2010, Avi Kivity wrote about "Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME":

Quoted 1 line

I don't see how vcpu->arch.cr0 can cache cr0_read_shadow.

But this is precisely what the (unpatched) vmx_set_cr0 code does:
If you look at it, it takes a parameter "cr0" and builds an additional variable "hw_cr0".
"cr0" gets written into CR0_READ_SHADOW, while "hw_cr0" gets written into GUEST_CR0.
vcpu->arch.cr0 gets a copy of "cr0", not of "hw_cr0", i.e., it is a cache of CR0_READ_SHADOW, not of GUEST_CR0.

Or am I missing something?

Thanks,
Nadav.

Signature · 4 lines
Nadav Har'El                        |      Sunday, Sep 26 2010, 18 Tishri 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |What's the greatest world-wide use of
http://nadav.harel.org.il           |cowhide? To hold cows together.
#94 lore raw

Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME

Quoted 11 lines · Nadav Har'El

On Sun, Sep 26, 2010, Avi Kivity wrote about "Re: [PATCH 16/24] Implement VMLAUNCH and VMRESUME":

Quoted 1 line

I don't see how vcpu->arch.cr0 can cache cr0_read_shadow.

But this is precisely what the (unpatched) vmx_set_cr0 code does:
If you look at it, it takes a parameter "cr0" and builds an additional variable "hw_cr0".
"cr0" gets written into CR0_READ_SHADOW, while "hw_cr0" gets written into GUEST_CR0.
vcpu->arch.cr0 gets a copy of "cr0", not of "hw_cr0", i.e., it is a cache of CR0_READ_SHADOW, not of GUEST_CR0.

Quoted 2 lines

Or am I missing something?

In vmx, cr0 is split into two registers, CR0_READ_SHADOW and GUEST_CR0. nvmx needs to split vCR0_READ_SHADOW and vGUEST_CR0 into three.

vCR0_READ_SHADOW can be assigned directly to CR0_READ_SHADOW.
vGUEST_CR0 can be copied to vcpu->arch.cr0 so the mmu acts according to the mode L1 thinks it places L2 into (but not what L2 thinks it is in). vGUEST_CR0, appropriately munged (by ORing it with KVM_VM_CR0_ALWAYS_ON and doing the TS games) is assigned to GUEST_CR0.

We need to audit all code that touches vcpu->arch.cr0; but I think this split is the easiest one. The only code that needs to change is the cr0/lmsw emulation code (writes need to consider vCR0_GUEST_HOST_MASK).

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

Re: [PATCH 22/24] Correct handling of idt vectoring info

In reply to Nadav Har'El
Quoted 10 lines · Nadav Har'El

On Mon, Sep 20, 2010, Avi Kivity wrote about "Re: [PATCH 22/24] Correct handling of idt vectoring info":

Quoted 5 lines
Quoted 3 lines

I'm afraid I know very little about the SVM architecture. Does SVM even have
a parallel of the IDT_VECTORING_INFO that this patch is trying to address?

It does. exit_int_info.

Thanks. I guess I need to do some serious reading on this subject. I guessed that exit_int_info was more of a parallel of VMX's vm_exit_intr_info field and not idt_vectoring_info, but I guess I was wrong.

svm has separate intercepts for every exception, so it doesn't need the vector field of vm_exit_intr_info_field. The error code is stored in exit_info_1, cr2 (for #PF) in exit_info_2.

Quoted 11 lines
Quoted 7 lines
Quoted 5 lines

I agree that the nested SVM's handle_exit() looks cleaner that the parallel code in nested VMX. The root of all evil is that second exit decision point in the injection phase, and I'll think some more if I can find a way to avoid it without rocking the foundations too much.

I think svm needs it too.

Can you please clarify? I didn't understand what "it" refers to here.

Sorry, it was a week ago.

In general I think both svm and vmx need to go through the exception/interrupt queues. That is, if you exit with IDT_VECTORING_INFO_VALID, you unpack it into the queue as a pending exception, and when you enter again you load it into VM_ENTRY_INTR_INFO_FIELD. That's a bit of work, but it reduces the amount of code paths when L0 needs to inject an exception into L2 (like in emulation) - all it has to do is to queue it into the generic exception queue.

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

Re: [PATCH 22/24] Correct handling of idt vectoring info

Quoted 30 lines · Avi Kivity
Quoted 10 lines · Nadav Har'El

On Mon, Sep 20, 2010, Avi Kivity wrote about "Re: [PATCH 22/24] Correct handling of idt vectoring info":

Quoted 5 lines
Quoted 3 lines

I'm afraid I know very little about the SVM architecture. Does SVM even have
a parallel of the IDT_VECTORING_INFO that this patch is trying to address?

It does. exit_int_info.

Thanks. I guess I need to do some serious reading on this subject. I guessed that exit_int_info was more of a parallel of VMX's vm_exit_intr_info field and not idt_vectoring_info, but I guess I was wrong.

svm has separate intercepts for every exception, so it doesn't need the vector field of vm_exit_intr_info_field. The error code is stored in exit_info_1, cr2 (for #PF) in exit_info_2.

Quoted 11 lines
Quoted 7 lines
Quoted 5 lines

I agree that the nested SVM's handle_exit() looks cleaner that the parallel code in nested VMX. The root of all evil is that second exit decision point in the injection phase, and I'll think some more if I can find a way to avoid it without rocking the foundations too much.

I think svm needs it too.

Can you please clarify? I didn't understand what "it" refers to here.

Sorry, it was a week ago.

Quoted 9 lines

In general I think both svm and vmx need to go through the exception/interrupt queues. That is, if you exit with IDT_VECTORING_INFO_VALID, you unpack it into the queue as a pending exception, and when you enter again you load it into VM_ENTRY_INTR_INFO_FIELD. That's a bit of work, but it reduces the amount of code paths when L0 needs to inject an exception into L2 (like in emulation) - all it has to do is to queue it into the generic exception queue.

And if L0 needs to reinject event directly into L2 it just uses regular L0 code path instead of ah-hoc nested_handle_valid_idt_vectoring_info() function.

--
Gleb.

#134 lore raw
In reply to Avi Kivity

On Tue, Jun 15, 2010, Avi Kivity wrote about "Re: [PATCH 0/24] Nested VMX, v5":

Quoted 2 lines

I've tried to test the patches, but I see a vm-entry failure code 7 on the very first vmentry. Guest is Fedora 12 x86-64 (2.6.32.9-70.fc12).

Hi, as you can see, I posted a new set of patches, which apply to the current trunk. Can you please give it another try? Thanks!

Please make sure you follow the instructions in the introduction to the patch. In short, try running the L0 kernel with the "nosmp" option, give the "-cpu host" option to qemu, and the "nested=1 ept=0 vpid=0" options to the kvm-intel module in L0.

Thanks,
Nadav.

Signature · 4 lines
Nadav Har'El                        |      Sunday, Oct 17 2010, 9 Heshvan 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |This space is for sale - inquire inside.
http://nadav.harel.org.il           |
#135 lore raw
Quoted 9 lines · Nadav Har'El

On Tue, Jun 15, 2010, Avi Kivity wrote about "Re: [PATCH 0/24] Nested VMX, v5":

Quoted 2 lines

I've tried to test the patches, but I see a vm-entry failure code 7 on the very first vmentry. Guest is Fedora 12 x86-64 (2.6.32.9-70.fc12).

Hi, as you can see, I posted a new set of patches, which apply to the current trunk. Can you please give it another try? Thanks!

Please make sure you follow the instructions in the introduction to the patch. In short, try running the L0 kernel with the "nosmp" option,

What are the problems with smp?

Quoted 2 lines

give the
"-cpu host" option to qemu,

Why is this needed?

Quoted 2 lines

and the "nested=1 ept=0 vpid=0" options to the
kvm-intel module in L0.

Why are those needed? Seems trivial to support a nonept guest on an ept host - all you do is switch cr3 during vmentry and vmexit.

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

On Sun, Oct 17, 2010, Avi Kivity wrote about "Re: [PATCH 0/24] Nested VMX, v5":

Quoted 2 lines
Quoted 1 line

patch. In short, try running the L0 kernel with the "nosmp" option,

What are the problems with smp?

Unfortunately, there appears to be a bug which causes KVM with nested VMX to hang when SMP is enabled, even if you don't try to use more than one CPU for the guest. I still need to debug this to figure out why.

Quoted 4 lines
Quoted 2 lines

give the
"-cpu host" option to qemu,

Why is this needed?

Qemu has a list of cpu types, and for each type it lists its features. The problem is that Qemu doesn't list the "VMX" feature for any of the CPUs, even those (like core 2 duo). I have a trivial patch to qemu to add the "VMX" feature to those CPUs, which is harmless even if KVM doesn't support nested VMX (qemu will drop features which KVM doesn't support). But until I send such a patch to qemu, the easiest workaround is just to use "-cpu host" - which will (among other things) tell qemu to emulate a machine which has vmx, just like the host does.

(I also explained this in the intro to v6 of the patch).

Quoted 6 lines
Quoted 3 lines

and the "nested=1 ept=0 vpid=0" options to the
kvm-intel module in L0.

Why are those needed? Seems trivial to support a nonept guest on an ept host - all you do is switch cr3 during vmentry and vmexit.

nested=1 is needed because you asked for it *not* to be the default :-)

You're right, ept=1 on the host *could* be supported even before nested ept is supported (this is the mode we called "shadow on ept" in the paper). But at the moment, I believe it doesn't work correctly. I'll add making this case work to my TODO list.

I'm not sure why vpid=0 is needed (but I verified that you get a failed entry if you don't use it). I understood that there was some discussion on what is the proper way to do nested vpid, and that in the meantime it isn't supported, but I agree that it should have been possible to use vpid normally to run L1's but avoid using it when running L2's. Again, I'll need to debug this issue to understand how difficult it would be to fix this case.

Nadav.

Signature · 4 lines
Nadav Har'El                        |      Sunday, Oct 17 2010, 9 Heshvan 5771
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Strike not only while the iron is hot,
http://nadav.harel.org.il           |make the iron hot by striking it.
#137 lore raw
Quoted 7 lines · Nadav Har'El

On Sun, Oct 17, 2010, Avi Kivity wrote about "Re: [PATCH 0/24] Nested VMX, v5":

Quoted 2 lines
Quoted 1 line

patch. In short, try running the L0 kernel with the "nosmp" option,

What are the problems with smp?

Unfortunately, there appears to be a bug which causes KVM with nested VMX to hang when SMP is enabled, even if you don't try to use more than one CPU for the guest. I still need to debug this to figure out why.

Well, that seems pretty critical.

Quoted 14 lines
Quoted 4 lines
Quoted 2 lines

give the
"-cpu host" option to qemu,

Why is this needed?

Qemu has a list of cpu types, and for each type it lists its features. The problem is that Qemu doesn't list the "VMX" feature for any of the CPUs, even those (like core 2 duo). I have a trivial patch to qemu to add the "VMX" feature to those CPUs, which is harmless even if KVM doesn't support nested VMX (qemu will drop features which KVM doesn't support). But until I send such a patch to qemu, the easiest workaround is just to use "-cpu host" - which will (among other things) tell qemu to emulate a machine which has vmx, just like the host does.

Quoted 1 line

(I also explained this in the intro to v6 of the patch).

Ok. I think we can get that patch merged, just so you don't have to re-explain it over and over again. Please post it to qemu-devel.

Quoted 14 lines
Quoted 6 lines
Quoted 3 lines

and the "nested=1 ept=0 vpid=0" options to the
kvm-intel module in L0.

Why are those needed? Seems trivial to support a nonept guest on an ept host - all you do is switch cr3 during vmentry and vmexit.

nested=1 is needed because you asked for it *not* to be the default :-)

You're right, ept=1 on the host *could* be supported even before nested ept is supported (this is the mode we called "shadow on ept" in the paper). But at the moment, I believe it doesn't work correctly. I'll add making this case work to my TODO list.

Quoted 6 lines

I'm not sure why vpid=0 is needed (but I verified that you get a failed entry if you don't use it). I understood that there was some discussion on what is the proper way to do nested vpid, and that in the meantime it isn't supported, but I agree that it should have been possible to use vpid normally to run L1's but avoid using it when running L2's. Again, I'll need to debug this issue to understand how difficult it would be to fix this case.

My feeling is the smp and vpid failures are due to bugs. vpid=0 in particular forces a tlb flush on every exit which might mask your true bug. smp might be due to host vcpu migration. Are we vmclearing the right vmcs?

ept=1 may not be due to a bug per se, but my feeling is that it should be very easy to implement. In particular nsvm started out on npt (but not nnpt) and had issues with shadow-on-shadow (IIRC).

Signature · 1 line
error compiling committee.c: too many arguments to function