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
#124 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 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
#125 lore raw

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

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.
#126 lore raw

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

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.

#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.

#129 lore raw

PATCH 23/24 Handling of CR0.TS and #NM for Lazy FPU loading

In reply to Nadav Har'El

KVM's "Lazy FPU loading" means that sometimes L0 needs to set CR0.TS, even if a guest didn't set it. Moreover, L0 must also trap CR0.TS changes and NM exceptions, even if we have a guest hypervisor (L1) who didn't want these traps. And of course, conversely: If L1 wanted to trap these events, we must let it, even if L0 is not interested in them.

This patch fixes some existing KVM code (in update_exception_bitmap(), vmx_fpu_activate(), vmx_fpu_deactivate(), handle_cr()) to do the correct merging of L0's and L1's needs. Note that new code in introduced in previous patches already handles CR0 correctly (see prepare_vmcs_02(), prepare_vmcs_12(), and nested_vmx_vmexit()).

---

arch/x86/kvm/vmx.c +102 -5
@@ -1144,6 +1144,27 @@ static void update_exception_bitmap(stru
 		eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
 	if (vcpu->fpu_active)
 		eb &= ~(1u << NM_VECTOR);
+
+	/* When we are running a nested L2 guest and L1 specified for it a
+	 * certain exception bitmap, we must trap the same exceptions and pass
+	 * them to L1. When running L2, we will only handle the exceptions
+	 * specified above if L1 did not want them.
+	 */
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		u32 nested_eb;
+		if (to_vmx(vcpu)->nested.current_l2_page)
+			nested_eb = get_shadow_vmcs(vcpu)->exception_bitmap;
+		else {
+			if (!nested_map_current(vcpu)) {
+				to_vmx(vcpu)->fail = 1;
+				return;
+			}
+			nested_eb = get_shadow_vmcs(vcpu)->exception_bitmap;
+			nested_unmap_current(vcpu);
+		}
+		eb |= nested_eb;
+	}
+
 	vmcs_write32(EXCEPTION_BITMAP, eb);
 }
 
@@ -1488,8 +1509,25 @@ static void vmx_fpu_activate(struct kvm_
 	cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
 	cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
 	vmcs_writel(GUEST_CR0, cr0);
-	update_exception_bitmap(vcpu);
 	vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		/* While we (L0) no longer care about NM exceptions or cr0.TS
+		 * changes, our guest hypervisor (L1) might care in which case
+		 * we must trap them for it.
+		 */
+		u32 eb = vmcs_read32(EXCEPTION_BITMAP) & ~(1u << NM_VECTOR);
+		struct shadow_vmcs *vmcs12;
+		if (!nested_map_current(vcpu)) {
+			to_vmx(vcpu)->fail = 1;
+			return;
+		}
+		vmcs12 = get_shadow_vmcs(vcpu);
+		eb |= vmcs12->exception_bitmap;
+		vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
+		nested_unmap_current(vcpu);
+		vmcs_write32(EXCEPTION_BITMAP, eb);
+	} else
+		update_exception_bitmap(vcpu);
 	vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
 }
 
@@ -1497,12 +1535,24 @@ static void vmx_decache_cr0_guest_bits(s
 
 static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
 {
+	/* Note that there is no vcpu->fpu_active = 0 here. The caller must
+	 * set this *before* calling this function.
+	 */
 	vmx_decache_cr0_guest_bits(vcpu);
 	vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
-	update_exception_bitmap(vcpu);
+	vmcs_write32(EXCEPTION_BITMAP,
+		vmcs_read32(EXCEPTION_BITMAP) | (1u << NM_VECTOR));
 	vcpu->arch.cr0_guest_owned_bits = 0;
 	vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
-	vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
+	if (to_vmx(vcpu)->nested.nested_mode)
+		/* Unfortunately in nested mode we play with arch.cr0's PG
+		 * bit, so we musn't copy it all, just the relevant TS bit
+		 */
+		vmcs_writel(CR0_READ_SHADOW,
+			(vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS) |
+			(vcpu->arch.cr0 & X86_CR0_TS));
+	else
+		vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
 }
 
 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
@@ -3998,6 +4048,53 @@ vmx_patch_hypercall(struct kvm_vcpu *vcp
 	hypercall[2] = 0xc1;
 }
 
+/* called to set cr0 as approriate for a mov-to-cr0 exit. */
+static void handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
+{
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		/* When running L2, we usually do what L1 wants: it decides
+		 * which cr0 bits to intercept, we forward it cr0-change events
+		 * (see nested_vmx_exit_handled()). We only get here when a cr0
+		 * bit was changed that L1 did not ask to intercept, but L0
+		 * nevertheless did. Currently this can only happen with the TS
+		 * bit (see CR0_GUEST_HOST_MASK in prepare_vmcs_02()).
+		 * We must change only this bit in GUEST_CR0 and CR0_READ_SHADOW
+		 * and not call kvm_set_cr0 because it enforces a relationship
+		 * between the two that is specific to KVM (i.e., only the TS
+		 * bit might differ) and with which L1 might not agree.
+		 */
+		long new_cr0 = vmcs_readl(GUEST_CR0);
+		long new_cr0_rs = vmcs_readl(CR0_READ_SHADOW);
+		if (val & X86_CR0_TS) {
+			new_cr0 |= X86_CR0_TS;
+			new_cr0_rs |= X86_CR0_TS;
+			vcpu->arch.cr0 |= X86_CR0_TS;
+		} else {
+			new_cr0 &= ~X86_CR0_TS;
+			new_cr0_rs &= ~X86_CR0_TS;
+			vcpu->arch.cr0 &= ~X86_CR0_TS;
+		}
+		vmcs_writel(GUEST_CR0, new_cr0);
+		vmcs_writel(CR0_READ_SHADOW, new_cr0_rs);
+		to_vmx(vcpu)->nested.nested_run_pending = 1;
+	} else
+		kvm_set_cr0(vcpu, val);
+}
+
+/* called to set cr0 as approriate for clts instruction exit. */
+static void handle_clts(struct kvm_vcpu *vcpu)
+{
+	if (to_vmx(vcpu)->nested.nested_mode) {
+		/* As in handle_set_cr0(), we can't call vmx_set_cr0 here */
+		vmcs_writel(GUEST_CR0, vmcs_readl(GUEST_CR0) & ~X86_CR0_TS);
+		vmcs_writel(CR0_READ_SHADOW,
+				vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
+		vcpu->arch.cr0 &= ~X86_CR0_TS;
+		to_vmx(vcpu)->nested.nested_run_pending = 1;
+	} else
+		vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
+}
+
 static int handle_cr(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification, val;
@@ -4013,7 +4110,7 @@ static int handle_cr(struct kvm_vcpu *vc
 		trace_kvm_cr_write(cr, val);
 		switch (cr) {
 		case 0:
-			kvm_set_cr0(vcpu, val);
+			handle_set_cr0(vcpu, val);
 			skip_emulated_instruction(vcpu);
 			return 1;
 		case 3:
@@ -4039,7 +4136,7 @@ static int handle_cr(struct kvm_vcpu *vc
 		};
 		break;
 	case 2: /* clts */
-		vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
+		handle_clts(vcpu);
 		trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
 		skip_emulated_instruction(vcpu);
 		vmx_fpu_activate(vcpu);
#130 lore raw

PATCH 24/24 Miscellenous small corrections

In reply to Nadav Har'El

Small corrections of KVM (spelling, etc.) not directly related to nested VMX.

---

arch/x86/kvm/vmx.c +1 -1
@@ -992,7 +992,7 @@ static void vmcs_load(struct vmcs *vmcs)
 			: "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
 			: "cc", "memory");
 	if (error)
-		printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
+		printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
 		       vmcs, phys_addr);
 }
 
#131 lore raw
In reply to Nadav Har'El
Quoted 39 lines · Nadav Har'El

Hi Avi,

This is a followup of our nested VMX patches that Orit Wasserman posted in December. We've addressed most of the comments and concerns that you and others on the mailing list had with the previous patch set. We hope you'll find these patches easier to understand, and suitable for applying to KVM.

The following 24 patches implement nested VMX support. The patches enable a guest to use the VMX APIs in order to run its own nested guests. I.e., it allows running hypervisors (that use VMX) under KVM. We describe the theory behind this work, our implementation, and its performance characteristics, in IBM Research report H-0282, "The Turtles Project: Design and Implementation of Nested Virtualization", available at:

	http://bit.ly/a0o9te

The current patches support running Linux under a nested KVM using shadow page table (with bypass_guest_pf disabled). They support multiple nested hypervisors, which can run multiple guests. Only 64-bit nested hypervisors are supported. SMP is supported. Additional patches for running Windows under nested KVM, and Linux under nested VMware server, and support for nested EPT, are currently running in the lab, and will be sent as follow-on patchsets.

These patches were written by:
Abel Gordon, abelg<at> il.ibm.com
Nadav Har'El, nyh<at> il.ibm.com
Orit Wasserman, oritw<at> il.ibm.com
Ben-Ami Yassor, benami<at> il.ibm.com
Muli Ben-Yehuda, muli<at> il.ibm.com

With contributions by:
Anthony Liguori, aliguori<at> us.ibm.com
Mike Day, mdday<at> us.ibm.com

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

Quoted 8 lines

Changes since v4:
* Rebased to the current KVM tree.
* Support for lazy FPU loading.
* Implemented about 90 requests and suggestions made on the mailing list regarding the previous version of this patch set.
* Split the changes into many more, and better documented, patches.

Overall, very nice. The finer split and better documentation really help reviewing, thanks.

Let's try to get this merged quickly.

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

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

Quoted 2 lines

Overall, very nice. The finer split and better documentation really help reviewing, thanks.

Thank you for the review and all the accurate comments!

Quoted 1 line

Let's try to get this merged quickly.

I'll start fixing the individual patches and resending them individually, and when I've fixed everything I'll resubmit the whole lot. I hope that this time I can do it in a matter of days, not months.

Thanks,
Nadav.

Signature · 4 lines
Nadav Har'El                        |       Monday, Jun 14 2010, 2 Tammuz 5770
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |An egotist is a person of low taste, more
http://nadav.harel.org.il           |interested in himself than in me.
#133 lore raw
Quoted 7 lines · Nadav Har'El
Quoted 3 lines

Let's try to get this merged quickly.

I'll start fixing the individual patches and resending them individually, and when I've fixed everything I'll resubmit the whole lot. I hope that this time I can do it in a matter of days, not months.

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).

If you can post a git tree with the next round, that will make it easier for people experimenting with the patches.

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

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
#138 lore raw
In reply to Nadav Har'El
Quoted 19 lines · Nadav Har'El

Hi Avi,

This is a followup of our nested VMX patches that Orit Wasserman posted in December. We've addressed most of the comments and concerns that you and others on the mailing list had with the previous patch set. We hope you'll find these patches easier to understand, and suitable for applying to KVM.

The following 24 patches implement nested VMX support. The patches enable a guest to use the VMX APIs in order to run its own nested guests. I.e., it allows running hypervisors (that use VMX) under KVM. We describe the theory behind this work, our implementation, and its performance characteristics,
in IBM Research report H-0282, "The Turtles Project: Design and Implementation of Nested Virtualization", available at:

	http://bit.ly/a0o9te
Quoted 8 lines

The current patches support running Linux under a nested KVM using shadow page table (with bypass_guest_pf disabled). They support multiple nested hypervisors, which can run multiple guests. Only 64-bit nested hypervisors are supported. SMP is supported. Additional patches for running Windows under nested KVM, and Linux under nested VMware server, and support for nested EPT, are currently running in the lab, and will be sent as follow-on patchsets.

Nadav & All:
Thnaks for the posting and in general the patches are well written. I like the concept of VMCSxy and I feel it is pretty clear (better than my previous naming as well), but there are some confusing inside, especially for the term "shadow" which I feel quit hard.

Comments from me:
1: Basically there are 2 diferent type in VMCS, one is defined by hardware, whose layout is unknown to VMM. Another one is defined by VMM (this patch) and used for vmcs12.

The former one is using "struct vmcs" to describe its data instance, but the later one doesn't have a clear definition (or struct vmcs12?). I suggest we can have a distinguish struct for this, for example "struct sw_vmcs"(software vmcs), or "struct vvmcs" (virtual vmcs).

2: vmcsxy (vmcs12, vmcs02, vmcs01) are for instances of either "struct vmcs", or "struct sw_vmcs", but not for struct Clear distinguish between data structure and instance helps IMO.

3: We may use prefix or suffix in addition to vmcsxy to explictly state the format of that instance. For example vmcs02 in current patch is for hardware use, hence it is an instance "struct vmcs", but vmcs01 is an instance of "struct sw_vmcs". Postfix and prefix helps to make better understand.

4: Rename l2_vmcs to vmcs02, l1_shadow_vmcs to vmcs01, l1_vmcs to vmcs02, with prefix/postfix can strengthen above concept of vmcsxy.

5: guest VMPTRLD emulation. Current patch creates vmcs02 instance each time when guest VMPTRLD, and free the instance at VMCLEAR. The code may fail if the vmcs (un-vmcleared) exceeds certain threshold to avoid denial of service. That is fine, but it brings additional complexity and may pay with a lot of memory. I think we can emulate using concept of "cached vmcs" here in case L1 VMM doesn't do vmclear in time. L0 VMM can simply flush those vmcs02 to guest memory i.e. vmcs12 per need. For example if the cached vcs02 exceeds 10, we can do automatically flush.

Thx, Eddie

#139 lore raw

On Fri, Jul 09, 2010, Dong, Eddie wrote about "RE: [PATCH 0/24] Nested VMX, v5":

Quoted 4 lines

Thnaks for the posting and in general the patches are well written.
I like the concept of VMCSxy and I feel it is pretty clear (better than my previous naming as well), but there are some confusing inside, especially for the term "shadow" which I feel quit hard.

Hi, and thanks for the excellent ideas. As you saw, I indeed started to convert and converge the old terminology (including that ambiguous term "shadow") into the new names vmcs01, vmcs02, vmcs12 - names which we introduced in our technical report.
But I have not gone all the way with these changes. I should have, and I'll do it now.

Quoted 7 lines

1: Basically there are 2 diferent type in VMCS, one is defined by hardware, whose layout is unknown to VMM. Another one is defined by VMM (this patch) and used for vmcs12.
The former one is using "struct vmcs" to describe its data instance, but the later one doesn't have a clear definition (or struct vmcs12?). I suggest we can have a distinguish struct for this, for example "struct sw_vmcs" (software vmcs), or "struct vvmcs" (virtual vmcs).

I decided (but let me know if you have reservations) to use the name "struct vmcs_fields" for the memory structure that contains the long list of vmcs fields. I think this name describes the structure's content well.

As in the last version of the patches, this list of vmcs fields will not on its own be vmcs12's structure, because vmcs12, as a spec-compliant vmcs, also needs to contain a couple of additional fields in its beginning, and we also need a few more runtime fields.

Quoted 3 lines

2: vmcsxy (vmcs12, vmcs02, vmcs01) are for instances of either
"struct vmcs", or "struct sw_vmcs", but not for struct Clear distinguish between data structure and instance helps IMO.

I agree with you that using the name "vmcs12" for both the type (struct vmcs12) and instance of another type (struct vmcs_fields *vmcs12) is somewhat strange, but I can only think of two alternatives:

1. Invent a new name for "struct vmcs12", say "struct sw_vmcs" as you suggested. But I think it will just make things less clear, because we replace the self-explanatory name vmcs12 by a less clear name.

2. Stop separating "struct vmcs_fields" (formerly struct shadow_vmcs) and "struct vmcs12" which contains it and a few more fields - and instead put everything in one structure (and call that sw_vmcs or whatever). These extra fields will not be useful for vmcs01, but it's not a terrible waste (because vmcs01 already doesn't use a lot of these fields).

Personally, I find these two alternatives even less appealing than the current alternative (with "struct vmcs12" describing vmcs12's type, and it contains a struct vmcs_fields inside). What do you think?

Quoted 4 lines

3: We may use prefix or suffix in addition to vmcsxy to explictly state the format of that instance. For example vmcs02 in current patch is for hardware use, hence it is an instance "struct vmcs", but vmcs01 is an instance of "struct sw_vmcs". Postfix and prefix helps to make better understand.

I agree. After changing the old name struct shadow_vmcs to vmcs_fields, now I can use a name like vmcs01_fields for the old l1_shadow_vmcs (memory copy of vmcs01's fields) and vmcs01 for the old l1_vmcs (the actual hardware VMCS used to run L1). This is is indeed more readable, thanks.

Quoted 2 lines

4: Rename l2_vmcs to vmcs02, l1_shadow_vmcs to vmcs01, l1_vmcs to vmcs02, with prefix/postfix can strengthen above concept of vmcsxy.

Good ideas.

renamed l2_vmcs, l2_vmcs_list, and the likes, to vmcs02.

Renamed l1_shadow_vmcs to vmcs01_fields, ands l1_vmcs to vmcs01 (NOT vmcs02).

renamed l2_shadow_vmcs, l2svmcs, nested_vmcs, and the likes, to vmcs12 (I decided not to use the longer name vmcs12_fields, because I don't think it adds any clarity). I also renamed get_shadow_vmcs to get_vmcs12_fields.

Quoted 8 lines

5: guest VMPTRLD emulation. Current patch creates vmcs02 instance each time when guest VMPTRLD, and free the instance at VMCLEAR. The code may fail if the vmcs (un-vmcleared) exceeds certain threshold to avoid denial of service. That is fine, but it brings additional complexity and may pay with a lot of memory. I think we can emulate using concept of "cached vmcs" here in case L1 VMM doesn't do vmclear in time. L0 VMM can simply flush those vmcs02 to guest memory i.e. vmcs12 per need. For example if the cached vcs02 exceeds 10, we can do automatically flush.

Right. I've already discussed this idea over the list with Avi Kivity, and it is on my todo list and definitely should be done.
The current approach is simpler, because I don't need to add special code for rebuilding a forgotten vmcs02 from vmcs12 - the current prepare_vmcs02 only updates some of the fields, and I'll need to do some testing to figure out what exactly is missing for a full rebuild.

I think the current code is "good enough" as an ad-interim solution, because users that follow the spec will not forget to VMCLEAR anyway (and if they do, only they will suffer). And I wouldn't say that "a lot of memory" is involved - at worst, an L1 can now cause 256 pages, or 1 MB, to be wasted on this. More normally, an L1 will only have a few L2 guests, and only spend a few pages for this - certainly much much less than he'd spend on actually holding the L2's memory.

Thanks again for the review!

I don't want to attach the entire set of patches again now (before I respond to the rest of the review comments, from Avi, Gleb, you, and others). So in the meantime I'll include the new version of just one patch, so that you can see an example of the changes I've made to the names.

=========
Subject: [PATCH 16/26] nVMX: Implement VMLAUNCH and VMRESUME

Implement the VMLAUNCH and VMRESUME instructions, allowing a guest hypervisor to run its own guests.

---
 arch/x86/kvm/vmx.c |  200 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 197 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx.c +197 -3
@@ -279,6 +279,9 @@ struct __packed vmcs12 {
 	struct vmcs_fields fields;
 
 	bool launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
+
+	int cpu;
+	int launched;
 };
 
 /*
@@ -313,6 +316,23 @@ struct nested_vmx {
 	/* list of real (hardware) VMCS, one for each L2 guest of L1 */
 	struct list_head vmcs02_list; /* a vmcs_list */
 	int vmcs02_num;
+
+	/* Are we running a nested guest now */
+	bool nested_mode;
+	/* Level 1 state for switching to level 2 and back */
+	struct  {
+		u64 efer;
+		unsigned long cr3;
+		unsigned long cr4;
+		u64 io_bitmap_a;
+		u64 io_bitmap_b;
+		u64 msr_bitmap;
+		int cpu;
+		int launched;
+	} l1_state;
+	/* Saving the VMCS that we used for running L1 */
+	struct vmcs *vmcs01;
+	struct vmcs_fields *vmcs01_fields;
 };
 
 enum vmcs_field_type {
@@ -1383,6 +1403,19 @@ static void vmx_vcpu_load(struct kvm_vcp
 			new_offset = vmcs_read64(TSC_OFFSET) + delta;
 			vmcs_write64(TSC_OFFSET, new_offset);
 		}
+
+		if (vmx->nested.vmcs01_fields != NULL) {
+			struct vmcs_fields *vmcs01 =
+				vmx->nested.vmcs01_fields;
+			vmcs01->host_tr_base = vmcs_readl(HOST_TR_BASE);
+			vmcs01->host_gdtr_base = vmcs_readl(HOST_GDTR_BASE);
+			vmcs01->host_ia32_sysenter_esp =
+				vmcs_readl(HOST_IA32_SYSENTER_ESP);
+			if (tsc_this < vcpu->arch.host_tsc)
+				vmcs01->tsc_offset = vmcs_read64(TSC_OFFSET);
+			if (vmx->nested.nested_mode)
+				load_vmcs_host_state(vmcs01);
+		}
 	}
 }
 
@@ -2278,6 +2311,9 @@ static void free_l1_state(struct kvm_vcp
 		kfree(list_item);
 	}
 	vmx->nested.vmcs02_num = 0;
+
+	kfree(vmx->nested.vmcs01_fields);
+	vmx->nested.vmcs01_fields = NULL;
 }
 
 static void free_kvm_area(void)
@@ -4141,6 +4177,10 @@ static int handle_vmon(struct kvm_vcpu *
 	INIT_LIST_HEAD(&(vmx->nested.vmcs02_list));
 	vmx->nested.vmcs02_num = 0;
 
+	vmx->nested.vmcs01_fields = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!vmx->nested.vmcs01_fields)
+		return -ENOMEM;
+
 	vmx->nested.vmxon = true;
 
 	skip_emulated_instruction(vcpu);
@@ -4339,6 +4379,38 @@ static int handle_vmclear(struct kvm_vcp
 	return 1;
 }
 
+static int nested_vmx_run(struct kvm_vcpu *vcpu);
+
+static int handle_launch_or_resume(struct kvm_vcpu *vcpu, bool launch)
+{
+	if (!nested_vmx_check_permission(vcpu))
+		return 1;
+
+	if (to_vmx(vcpu)->nested.current_vmcs12->launch_state == launch) {
+		/* Must use VMLAUNCH for the first time, VMRESUME later */
+		set_rflags_to_vmx_fail_valid(vcpu);
+		return 1;
+	}
+
+	skip_emulated_instruction(vcpu);
+
+	nested_vmx_run(vcpu);
+	return 1;
+}
+
+/* Emulate the VMLAUNCH instruction */
+static int handle_vmlaunch(struct kvm_vcpu *vcpu)
+{
+	return handle_launch_or_resume(vcpu, true);
+}
+
+/* Emulate the VMRESUME instruction */
+static int handle_vmresume(struct kvm_vcpu *vcpu)
+{
+
+	return handle_launch_or_resume(vcpu, false);
+}
+
 static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
 					unsigned long field, u64 *ret)
 {
@@ -4869,11 +4941,11 @@ static int (*kvm_vmx_exit_handlers[])(st
 	[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,
@@ -4935,7 +5007,8 @@ static int vmx_handle_exit(struct kvm_vc
 		       "(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 &&
@@ -5756,6 +5829,127 @@ int prepare_vmcs_02(struct kvm_vcpu *vcp
 	return 0;
 }
 
+static int nested_vmx_run(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	vmx->nested.nested_mode = 1;
+	sync_cached_regs_to_vmcs(vcpu);
+	save_vmcs(vmx->nested.vmcs01_fields);
+
+	vmx->nested.l1_state.efer = vcpu->arch.efer;
+	if (!enable_ept)
+		vmx->nested.l1_state.cr3 = vcpu->arch.cr3;
+	vmx->nested.l1_state.cr4 = vcpu->arch.cr4;
+
+	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.vmcs01 = vmx->vmcs;
+	vmx->nested.l1_state.cpu = vcpu->cpu;
+	vmx->nested.l1_state.launched = vmx->launched;
+
+	vmx->vmcs = nested_get_current_vmcs(vcpu);
+	if (!vmx->vmcs) {
+		printk(KERN_ERR "Missing VMCS\n");
+		set_rflags_to_vmx_fail_valid(vcpu);
+		return 1;
+	}
+
+	vcpu->cpu = vmx->nested.current_vmcs12->cpu;
+	vmx->launched = vmx->nested.current_vmcs12->launched;
+
+	if (!vmx->nested.current_vmcs12->launch_state || !vmx->launched) {
+		vmcs_clear(vmx->vmcs);
+		vmx->launched = 0;
+		vmx->nested.current_vmcs12->launch_state = 1;
+	}
+
+	vmx_vcpu_load(vcpu, get_cpu());
+	put_cpu();
+
+	prepare_vmcs_02(vcpu,
+		get_vmcs12_fields(vcpu), vmx->nested.vmcs01_fields);
+
+	if (get_vmcs12_fields(vcpu)->vm_entry_controls &
+	    VM_ENTRY_IA32E_MODE) {
+		if (!((vcpu->arch.efer & EFER_LMA) &&
+		      (vcpu->arch.efer & EFER_LME)))
+			vcpu->arch.efer |= (EFER_LMA | EFER_LME);
+	} else {
+		if ((vcpu->arch.efer & EFER_LMA) ||
+		    (vcpu->arch.efer & EFER_LME))
+			vcpu->arch.efer = 0;
+	}
+
+	/* vmx_set_cr0() sets the cr0 that L2 will read, to be the one that L1
+	 * dictated, and takes appropriate actions for special cr0 bits (like
+	 * real mode, etc.).
+	 */
+	vmx_set_cr0(vcpu,
+		(get_vmcs12_fields(vcpu)->guest_cr0 &
+			~get_vmcs12_fields(vcpu)->cr0_guest_host_mask) |
+		(get_vmcs12_fields(vcpu)->cr0_read_shadow &
+			get_vmcs12_fields(vcpu)->cr0_guest_host_mask));
+
+	/* However, vmx_set_cr0 incorrectly enforces KVM's relationship between
+	 * GUEST_CR0 and CR0_READ_SHADOW, e.g., that the former is the same as
+	 * 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_vmcs12_fields(vcpu)->guest_cr0 |
+		(vcpu->fpu_active ? 0 : X86_CR0_TS));
+
+	vmx_set_cr4(vcpu, get_vmcs12_fields(vcpu)->guest_cr4);
+	vmcs_writel(CR4_READ_SHADOW,
+		    get_vmcs12_fields(vcpu)->cr4_read_shadow);
+
+	/* 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;
+
+	if (enable_ept && !nested_cpu_has_vmx_ept(vcpu)) {
+		vmcs_write32(GUEST_CR3, get_vmcs12_fields(vcpu)->guest_cr3);
+		vmx->vcpu.arch.cr3 = get_vmcs12_fields(vcpu)->guest_cr3;
+	} else {
+		int r;
+		kvm_set_cr3(vcpu, get_vmcs12_fields(vcpu)->guest_cr3);
+		kvm_mmu_reset_context(vcpu);
+
+		r = kvm_mmu_load(vcpu);
+		if (unlikely(r)) {
+			printk(KERN_ERR "Error in kvm_mmu_load r %d\n", r);
+			set_rflags_to_vmx_fail_valid(vcpu);
+			/* switch back to L1 */
+			vmx->nested.nested_mode = 0;
+			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();
+
+			return 1;
+		}
+	}
+
+	kvm_register_write(vcpu, VCPU_REGS_RSP,
+			   get_vmcs12_fields(vcpu)->guest_rsp);
+	kvm_register_write(vcpu, VCPU_REGS_RIP,
+			   get_vmcs12_fields(vcpu)->guest_rip);
+
+	return 1;
+}
+
 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, Jul 11 2010, 29 Tammuz 5770
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |The two most common elements in the
http://nadav.harel.org.il           |universe are hydrogen and stupidity.
#140 lore raw
Quoted 21 lines · Nadav Har'El

On Fri, Jul 09, 2010, Dong, Eddie wrote about "RE: [PATCH 0/24] Nested VMX, v5":

Quoted 4 lines

Thnaks for the posting and in general the patches are well written.
I like the concept of VMCSxy and I feel it is pretty clear (better than my previous naming as well), but there are some confusing inside, especially for the term "shadow" which I feel quit hard.

Hi, and thanks for the excellent ideas. As you saw, I indeed started to convert and converge the old terminology (including that ambiguous term "shadow") into the new names vmcs01, vmcs02, vmcs12 - names which we introduced in our technical report.
But I have not gone all the way with these changes. I should have, and I'll do it now.

Quoted 7 lines

1: Basically there are 2 diferent type in VMCS, one is defined by hardware, whose layout is unknown to VMM. Another one is defined by VMM (this patch) and used for vmcs12.
The former one is using "struct vmcs" to describe its data instance, but the later one doesn't have a clear definition (or struct vmcs12?). I suggest we can have a distinguish struct for this, for example "struct sw_vmcs" (software vmcs), or "struct vvmcs" (virtual vmcs).

Quoted 8 lines

I decided (but let me know if you have reservations) to use the name "struct vmcs_fields" for the memory structure that contains the long list of vmcs fields. I think this name describes the structure's content well.

As in the last version of the patches, this list of vmcs fields will not on its own be vmcs12's structure, because vmcs12, as a spec-compliant vmcs, also needs to contain a couple of additional fields in its beginning, and we also need a few more runtime fields.

Thinking about this - it would be perfectly legal to split the VMCS into two separate structs, right? You could have one struct that you map directly into the guest, so modifications to that struct don't trap. Of course the l1 guest shouldn't be able to modify all fields of the VMCS, so you'd still keep a second struct around with shadow fields. While at it, also add a bitmap to store the dirtyness status of your fields in, if you need that.

That way a nesting aware guest could use a PV memory write instead of the (slow) instruction emulation. That should dramatically speed up nesting vmx.

Alex

#141 lore raw

On Sun, Jul 11, 2010, Alexander Graf wrote about "Re: [PATCH 0/24] Nested VMX, v5":

Quoted 3 lines

Thinking about this - it would be perfectly legal to split the VMCS into two separate structs, right? You could have one struct that you map directly into the guest, so modifications to that struct don't trap. Of course the l1 guest shouldn't be able to modify all fields of the VMCS, so you'd still keep a second struct around with shadow fields. While at it, also add a bitmap to store the dirtyness status of your fields in, if you need that.

That way a nesting aware guest could use a PV memory write instead of the (slow) instruction emulation. That should dramatically speed up nesting vmx.

Hi,

We already tried this idea, and described the results in our tech report (see http://www.mulix.org/pubs/turtles/h-0282.pdf).

We didn't do things quite as cleanly as you suggested - we didn't split the structure and make only part of it available directly to the guest. Rather, we only did what we had to do to get the performance improvement: We modified L1 to access the VMCS directly, assuming the nested's vmcs12 structure layout, instead of calling vmread/vmwrite.

As you can see in the various benchmarks in section 4 (Evaluation) of the report, the so-called PV vmread/vmwrite method had a noticable, though perhaps not as dramatic as you hoped, effect. For example, for the kernbench benchmark, nested kvm overhead (over single-level kvm virtualization) came down from 14.5% to 10.3%, and for the specjbb benchmark, the overhead came down from 7.8% to 6.3%. In a microbenchmark less representative of real-life workloads, we were able to measure a halving of the overhead by adding the PV vmread/vmwrite.

In any case, the obvious problem with this whole idea on VMX is that it requires a modified guest hypervisor, which reduces its usefulness. This is why we didn't think we should "advertise" the ability to bypass vmread/vmwrite in L1 and write directly to the vmcs12's. But Avi Kivity already asked me to add a document about the vmcs12 internal structure, and once I've done that, I guess you can now consider it "fair" for nesting- aware L1 guest hypervisors to actually use that internal structure to modify vmcs12 directly, without vmread/vmwrite and exits.

By the way, I see on the KVM Forum 2010 schedule that Eddie Dong will be talking about "Examining KVM as Nested Virtualization Friendly Guest". I'm looking forward to reading the proceedings (unfortunately, I won't be able to travel to the actual meeting).

Nadav.

Signature · 4 lines
Nadav Har'El                        |      Sunday, Jul 11 2010, 29 Tammuz 5770
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |I used to work in a pickle factory, until
http://nadav.harel.org.il           |I got canned.
#142 lore raw
Quoted 10 lines · Nadav Har'El

In any case, the obvious problem with this whole idea on VMX is that it requires a modified guest hypervisor, which reduces its usefulness. This is why we didn't think we should "advertise" the ability to bypass vmread/vmwrite in L1 and write directly to the vmcs12's. But Avi Kivity already asked me to add a document about the vmcs12 internal structure, and once I've done that, I guess you can now consider it "fair" for nesting- aware L1 guest hypervisors to actually use that internal structure to modify vmcs12 directly, without vmread/vmwrite and exits.

No, they can't, since (for writes) L0 might cache the information and not read it again. For reads, L0 might choose to update vmcs12 on demand.

A pvvmread/write needs to communicate with L0 about what fields are valid (likely using available and dirty bitmaps).

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

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

Quoted 8 lines
Quoted 5 lines

nesting-
aware L1 guest hypervisors to actually use that internal structure to modify
vmcs12 directly, without vmread/vmwrite and exits.

No, they can't, since (for writes) L0 might cache the information and not read it again. For reads, L0 might choose to update vmcs12 on demand.

Well, in the current version of the nested code, all L0 does on a L1 vmwrite is to update the in-memory vmcs12 structure. It doesn't not update vmcs02, nor cache anything, nor remember what has changed and what hasn't. So replacing it with a direct write to the memory structure should be fine...

Of course, this situation isn't optimal, and we *should* optimize the number of unnecessary vmwrites L2 entry and exit (and we actually tried some of this in our tech report), but it's not in the current patch set. When we do these kind of optimizations, you're right that:

Quoted 2 lines

A pvvmread/write needs to communicate with L0 about what fields are valid (likely using available and dirty bitmaps).

Signature · 4 lines
Nadav Har'El                        |           Sunday, Jul 11 2010, 1 Av 5770
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |If marriage was illegal, only outlaws
http://nadav.harel.org.il           |would have in-laws.
#144 lore raw
Quoted 8 lines · Nadav Har'El

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

Quoted 6 lines
Quoted 6 lines

nesting-
aware L1 guest hypervisors to actually use that internal structure to modify
vmcs12 directly, without vmread/vmwrite and exits.

Quoted 8 lines
Quoted 3 lines

No, they can't, since (for writes) L0 might cache the information and not read it again. For reads, L0 might choose to update vmcs12 on demand.

Well, in the current version of the nested code, all L0 does on a L1 vmwrite is to update the in-memory vmcs12 structure. It doesn't not update vmcs02, nor cache anything, nor remember what has changed and what hasn't. So replacing it with a direct write to the memory structure should be fine...

Note you said "current version". What if this later changes?

So, we cannot allow a guest to access vmcs12 directly. There has to be a protocol that allows the guest to know what it can touch and what it can't (or, tell the host what the guest touched and what it hasn't). Otherwise, we lose the ability to optimize.

Quoted 9 lines

Of course, this situation isn't optimal, and we *should* optimize the number of unnecessary vmwrites L2 entry and exit (and we actually tried some of this in our tech report), but it's not in the current patch set. When we do these kind of optimizations, you're right that:

Quoted 3 lines

A pvvmread/write needs to communicate with L0 about what fields are valid (likely using available and dirty bitmaps).

It's right even before we do these optimizations, so a pv guest written before the optimizations can run on an optimized host.

Signature · 1 line
error compiling committee.c: too many arguments to function
#145 lore raw
In reply to Nadav Har'El
Quoted 10 lines · Nadav Har'El
Quoted 10 lines

1: Basically there are 2 diferent type in VMCS, one is defined by hardware, whose layout is unknown to VMM. Another one is defined by VMM (this patch) and used for vmcs12.
The former one is using "struct vmcs" to describe its data instance, but the later one doesn't have a clear definition (or struct vmcs12?). I suggest we can have a distinguish struct for this, for example "struct sw_vmcs" (software vmcs), or "struct vvmcs" (virtual vmcs).

Quoted 4 lines

I decided (but let me know if you have reservations) to use the name "struct vmcs_fields" for the memory structure that contains the long list of vmcs fields. I think this name describes the structure's content well.

I liked vvmcs myself...

Quoted 5 lines

As in the last version of the patches, this list of vmcs fields will not on its own be vmcs12's structure, because vmcs12, as a spec-compliant vmcs, also needs to contain a couple of additional fields in its beginning, and we also need a few more runtime fields.

... for the spec-compliant vmcs in L1's memory.

Quoted 8 lines
Quoted 4 lines

2: vmcsxy (vmcs12, vmcs02, vmcs01) are for instances of either
"struct vmcs", or "struct sw_vmcs", but not for struct Clear distinguish between data structure and instance helps IMO.

I agree with you that using the name "vmcs12" for both the type (struct vmcs12) and instance of another type (struct vmcs_fields *vmcs12) is somewhat strange, but I can only think of two alternatives:

Quoted 8 lines

1. Invent a new name for "struct vmcs12", say "struct sw_vmcs" as you suggested. But I think it will just make things less clear, because we replace the self-explanatory name vmcs12 by a less clear name.

2. Stop separating "struct vmcs_fields" (formerly struct shadow_vmcs) and "struct vmcs12" which contains it and a few more fields - and instead put everything in one structure (and call that sw_vmcs or whatever).

I like this.

Quoted 3 lines

These extra fields will not be useful for vmcs01, but it's not a terrible waste (because vmcs01 already doesn't use a lot of these fields).

You don't really need vmcs01 to be a vvmcs (or sw_vmcsw). IIRC you only need it when copying around vmcss, which you can avoid completely by initializing vmcs01 and vmcs02 using common initialization routines for the host part.

Quoted 4 lines

Personally, I find these two alternatives even less appealing than the current alternative (with "struct vmcs12" describing vmcs12's type, and it contains a struct vmcs_fields inside). What do you think?

IMO, vmcs_fields is artificial. As soon as you eliminate the vmcs copy, you won't have any use for it, and then you can fold it into its container.

Quoted 9 lines
Quoted 9 lines

5: guest VMPTRLD emulation. Current patch creates vmcs02 instance each time when guest VMPTRLD, and free the instance at VMCLEAR. The code may fail if the vmcs (un-vmcleared) exceeds certain threshold to avoid denial of service. That is fine, but it brings additional complexity and may pay with a lot of memory. I think we can emulate using concept of "cached vmcs" here in case L1 VMM doesn't do vmclear in time. L0 VMM can simply flush those vmcs02 to guest memory i.e. vmcs12 per need. For example if the cached vcs02 exceeds 10, we can do automatically flush.

Quoted 7 lines

Right. I've already discussed this idea over the list with Avi Kivity, and it is on my todo list and definitely should be done.
The current approach is simpler, because I don't need to add special code for rebuilding a forgotten vmcs02 from vmcs12 - the current prepare_vmcs02 only updates some of the fields, and I'll need to do some testing to figure out what exactly is missing for a full rebuild.

You already support "full rebuild" - that's what happens when you first see a vmcs, when you launch a guest.

Quoted 8 lines

I think the current code is "good enough" as an ad-interim solution, because users that follow the spec will not forget to VMCLEAR anyway (and if they do, only they will suffer). And I wouldn't say that "a lot of memory" is involved - at worst, an L1 can now cause 256 pages, or 1 MB, to be wasted on this. More normally, an L1 will only have a few L2 guests, and only spend a few pages for this - certainly much much less than he'd spend on actually holding the L2's memory.

It's perfectly legitimate for a guest to disappear a vmcs. It might swap it to disk, or move it to a separate NUMA node. While I don't expect the first, the second will probably happen sometime.

Signature · 1 line
error compiling committee.c: too many arguments to function
#146 lore raw
In reply to Nadav Har'El
Quoted 17 lines · On Sunday 13 June 2010 20:22:33 Nadav Har'El wrote:

Hi Avi,

This is a followup of our nested VMX patches that Orit Wasserman posted in December. We've addressed most of the comments and concerns that you and others on the mailing list had with the previous patch set. We hope you'll find these patches easier to understand, and suitable for applying to KVM.

The following 24 patches implement nested VMX support. The patches enable a guest to use the VMX APIs in order to run its own nested guests. I.e., it allows running hypervisors (that use VMX) under KVM. We describe the theory behind this work, our implementation, and its performance characteristics, in IBM Research report H-0282, "The Turtles Project: Design and Implementation of Nested Virtualization", available at:

	http://bit.ly/a0o9te
Quoted 7 lines

The current patches support running Linux under a nested KVM using shadow page table (with bypass_guest_pf disabled). They support multiple nested hypervisors, which can run multiple guests. Only 64-bit nested hypervisors are supported. SMP is supported. Additional patches for running Windows under nested KVM, and Linux under nested VMware server, and support for nested EPT, are currently running in the lab, and will be sent as follow-on patchsets.

Hi Nadav

Do you have a tree or code base and instruction to try this patchset? I've spent some time on it, but can't get it right...

--
regards
Yang, Sheng

Quoted 30 lines

These patches were written by:
Abel Gordon, abelg <at> il.ibm.com
Nadav Har'El, nyh <at> il.ibm.com
Orit Wasserman, oritw <at> il.ibm.com
Ben-Ami Yassor, benami <at> il.ibm.com
Muli Ben-Yehuda, muli <at> il.ibm.com

With contributions by:
Anthony Liguori, aliguori <at> us.ibm.com
Mike Day, mdday <at> us.ibm.com

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

Changes since v4:
* Rebased to the current KVM tree.
* Support for lazy FPU loading.
* Implemented about 90 requests and suggestions made on the mailing list regarding the previous version of this patch set.
* Split the changes into many more, and better documented, patches.

--
Nadav Har'El
IBM Haifa Research Lab
--
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