Skip to contentSkip to conversation

kvm-unit-test: nVMX: Test Selector and Base Address fields of Guest Segment registers

PATCH Superseded Krish Sadhukhan 4 participants 14 messages Apr 15, 2020 – Apr 28, 2020

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover kvm-unit-test: nVMX: Test Selector and Base Address fields of Guest Segment registers

v1 -> v2:
1. A KVM fix has been added via patch# 1. This fix is required to test the unsetting of "unrestricted guest" VM-execution control in vmcs12.
2. The kvm-unit-test has been enhanced by adding more combinations of the conditions mentioned in the vmentry checks being tested here.

[PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution [PATCH 2/2 v2] kvm-unit-tests: nVMX: Test Selector and Base Address fields of Guest Segment

 arch/x86/kvm/vmx/nested.c | 3 +++
 1 file changed, 3 insertions(+)

Krish Sadhukhan (1):
nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

 Makefile            |   2 +-
 lib/x86/processor.h |   1 +
 x86/vmx_tests.c     | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 203 insertions(+), 1 deletion(-)

Krish Sadhukhan (1):
nVMX: Test Selector and Base Address fields of Guest Segment Registers on vmentry of nested guests

#1 lore raw

PATCH v2 1/2 KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Currently, prepare_vmcs02_early() does not check if the "unrestricted guest" VM-execution control in vmcs12 is turned off and leaves the corresponding bit on in vmcs02. Due to this setting, vmentry checks which are supposed to render the nested guest state as invalid when this VM-execution control is not set, are passing in hardware.

This patch turns off the "unrestricted guest" VM-execution control in vmcs02 if vmcs12 has turned it off.

---
 arch/x86/kvm/vmx/nested.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/vmx/nested.c +3 -0
index cbc9ea2de28f..4fa5f8b51c82 100644
@@ -2224,6 +2224,9 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
 			vmcs_write16(GUEST_INTR_STATUS,
 				vmcs12->guest_intr_status);
 
+		if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
+			exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
+
 		secondary_exec_controls_set(vmx, exec_control);
 	}
 
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 20 lines · Krish Sadhukhan

Currently, prepare_vmcs02_early() does not check if the "unrestricted guest" VM-execution control in vmcs12 is turned off and leaves the corresponding bit on in vmcs02. Due to this setting, vmentry checks which are supposed to render the nested guest state as invalid when this VM-execution control is not set, are passing in hardware.

This patch turns off the "unrestricted guest" VM-execution control in vmcs02 if vmcs12 has turned it off.

---
 arch/x86/kvm/vmx/nested.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/vmx/nested.c +0 -0
index cbc9ea2de28f..4fa5f8b51c82 100644

arch/x86/kvm/vmx/nested.c static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) · line 2228

Quoted 5 lines
arch/x86/kvm/vmx/nested.c +2 -0
@@ -2224,6 +2224,9 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
 			vmcs_write16(GUEST_INTR_STATUS,
 				vmcs12->guest_intr_status);
 
+		if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
+			exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;

Has anyone worked through all the flows to verify this won't break any assumptions with respect to enable_unrestricted_guest? I would be (pleasantly) surprised if this was sufficient to run L2 without unrestricted guest when it's enabled for L1, e.g. vmx_set_cr0() looks suspect.

Quoted 7 lines
arch/x86/kvm/vmx/nested.c +1 -0
+
 		secondary_exec_controls_set(vmx, exec_control);
 	}
 
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 25 lines · Sean Christopherson
Quoted 23 lines · Krish Sadhukhan

Currently, prepare_vmcs02_early() does not check if the "unrestricted guest" VM-execution control in vmcs12 is turned off and leaves the corresponding bit on in vmcs02. Due to this setting, vmentry checks which are supposed to render the nested guest state as invalid when this VM-execution control is not set, are passing in hardware.

This patch turns off the "unrestricted guest" VM-execution control in vmcs02 if vmcs12 has turned it off.

---
 arch/x86/kvm/vmx/nested.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/vmx/nested.c +0 -0
index cbc9ea2de28f..4fa5f8b51c82 100644
@@ -2224,6 +2224,9 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
                      vmcs_write16(GUEST_INTR_STATUS,
                              vmcs12->guest_intr_status);
 
Quoted 2 lines

arch/x86/kvm/vmx/nested.c static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) · line 2228

Quoted 2 lines
arch/x86/kvm/vmx/nested.c +2 -0
+             if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
+                     exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;

Better, I think, would be to add SECONDARY_EXEC_UNRESTRICTED_GUEST to the mask here:

/* Take the following fields only from vmcs12 */ exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
SECONDARY_EXEC_ENABLE_INVPCID | SECONDARY_EXEC_RDTSCP |
SECONDARY_EXEC_XSAVES | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | SECONDARY_EXEC_APIC_REGISTER_VIRT | SECONDARY_EXEC_ENABLE_VMFUNC);

Quoted 5 lines

Has anyone worked through all the flows to verify this won't break any assumptions with respect to enable_unrestricted_guest? I would be (pleasantly) surprised if this was sufficient to run L2 without unrestricted guest when it's enabled for L1, e.g. vmx_set_cr0() looks suspect.

I think you're right to be concerned.

maintainer
#4 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 7 lines · Jim Mattson
Quoted 5 lines

Has anyone worked through all the flows to verify this won't break any assumptions with respect to enable_unrestricted_guest? I would be (pleasantly) surprised if this was sufficient to run L2 without unrestricted guest when it's enabled for L1, e.g. vmx_set_cr0() looks suspect.

I think you're right to be concerned.

Thirded, but it shouldn't be too hard. Basically, enable_unrestricted_guest must be moved into loaded_vmcs for this to work. It may be more work to write the test cases for L2 real mode <-> protected mode switch, which do not entirely fit into the vmx_tests.c framework (but with the v2 tests it should not be hard to adapt).

Paolo

#5 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Jim Mattson
Quoted 5 lines

Has anyone worked through all the flows to verify this won't break any assumptions with respect to enable_unrestricted_guest? I would be (pleasantly) surprised if this was sufficient to run L2 without unrestricted guest when it's enabled for L1, e.g. vmx_set_cr0() looks suspect.

Quoted 6 lines
Quoted 1 line

I think you're right to be concerned.

Thirded, but it shouldn't be too hard. Basically, enable_unrestricted_guest must be moved into loaded_vmcs for this to work. It may be more work to write the test cases for L2 real mode <-> protected mode switch, which do not entirely fit into the vmx_tests.c framework (but with the v2 tests it should not be hard to adapt).

OK, I will move enable_unrestricted_guest  to loaded_vmcs.

I also see that enable_ept controls the setting of enable_unrestricted_guest. Perhaps both need to be moved to loaded_vmcs ?

About testing, I am thinking the test will first vmlaunch L2 in real mode or in protected mode, then vmexit on vmcall and then vmresume in the other mode. Is that how the test should flow ?

Quoted 3 lines

Paolo

#6 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 9 lines · Krish Sadhukhan
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Jim Mattson
Quoted 5 lines

Has anyone worked through all the flows to verify this won't break any assumptions with respect to enable_unrestricted_guest? I would be (pleasantly) surprised if this was sufficient to run L2 without unrestricted guest when it's enabled for L1, e.g. vmx_set_cr0() looks suspect.

I think you're right to be concerned.

Quoted 8 lines
Quoted 5 lines

Thirded, but it shouldn't be too hard. Basically, enable_unrestricted_guest must be moved into loaded_vmcs for this to work. It may be more work to write the test cases for L2 real mode <-> protected mode switch, which do not entirely fit into the vmx_tests.c framework (but with the v2 tests it should not be hard to adapt).

OK, I will move enable_unrestricted_guest  to loaded_vmcs.

Hmm, enable_unrestricted_guest doesn't _need_ to be moved to loaded_vmcs, L1 can never diverge from enable_unrestricted_guest. E.g. the main control variable can stay global, we just need a flag in nested_vmx to override the main control. A simple wrapper can then take care of the check, e.g.

static inline bool is_unrestricted_guest(struct kvm_vcpu *vcpu)

  {
	return enable_unrestricted_guest && (!is_guest_mode(vcpu) ||
	       to_vmx(vcpu)->nested.unrestricted_guest);

}

Putting the flag in loaded_vmcs might be more performant? My guess is it'd be in the noise, at which point I'd rather have it be clear the override is only possible/necessary for nested guests.

Quoted 2 lines

I also see that enable_ept controls the setting of enable_unrestricted_guest. Perhaps both need to be moved to loaded_vmcs ?

No, letting L1 disable EPT in L0 would be pure insanity, and the overall paging mode of L2 is already reflected in the MMU.

The dependency on EPT is that VMX requires paging of some form and unrestricted guest allows entering non-root with CR0.PG=0, i.e. requires EPT be enabled.

Quoted 7 lines

About testing, I am thinking the test will first vmlaunch L2 in real mode or in protected mode, then vmexit on vmcall and then vmresume in the other mode. Is that how the test should flow ?

Quoted 3 lines

Paolo

maintainer
#7 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 10 lines · Sean Christopherson

static inline bool is_unrestricted_guest(struct kvm_vcpu *vcpu)

  {
	return enable_unrestricted_guest && (!is_guest_mode(vcpu) ||
	       to_vmx(vcpu)->nested.unrestricted_guest);

}

Putting the flag in loaded_vmcs might be more performant? My guess is it'd be in the noise, at which point I'd rather have it be clear the override is only possible/necessary for nested guests.

Even better: you can use secondary_exec_controls_get, which does get the flag from the loaded_vmcs :) but without actually having to add one.

Quoted 5 lines
Quoted 2 lines

I also see that enable_ept controls the setting of enable_unrestricted_guest. Perhaps both need to be moved to loaded_vmcs ?

No, letting L1 disable EPT in L0 would be pure insanity, and the overall paging mode of L2 is already reflected in the MMU.

Absolutely. Unrestricted guest requires EPT, but EPT is invisible to the guest. (Currently EPT requires guest MAXPHYADDR = host MAXPHYADDR, in the sense that the guest can detect that the host is lying about MAXPHYADDR; but that is really a bug that I hope will be fixed in 5.8, relaxing the requirement to guest MAXPHYADDR <= host PHYADDR).

Paolo

Quoted 3 lines

The dependency on EPT is that VMX requires paging of some form and unrestricted guest allows entering non-root with CR0.PG=0, i.e. requires EPT be enabled.

#8 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Sean Christopherson

static inline bool is_unrestricted_guest(struct kvm_vcpu *vcpu)

  {
	return enable_unrestricted_guest && (!is_guest_mode(vcpu) ||
	       to_vmx(vcpu)->nested.unrestricted_guest);

}

Quoted 6 lines
Quoted 3 lines

Putting the flag in loaded_vmcs might be more performant? My guess is it'd be in the noise, at which point I'd rather have it be clear the override is only possible/necessary for nested guests.

Even better: you can use secondary_exec_controls_get, which does get the flag from the loaded_vmcs :) but without actually having to add one.

I keep forgetting we have those shadows. Definitely the best solution.

#9 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

In reply to Paolo Bonzini
Quoted 15 lines · Paolo Bonzini
Quoted 9 lines · Sean Christopherson
   static inline bool is_unrestricted_guest(struct kvm_vcpu *vcpu)
   {
	return enable_unrestricted_guest && (!is_guest_mode(vcpu) ||
	       to_vmx(vcpu)->nested.unrestricted_guest);
   }

Putting the flag in loaded_vmcs might be more performant? My guess is it'd be in the noise, at which point I'd rather have it be clear the override is only possible/necessary for nested guests.

Even better: you can use secondary_exec_controls_get, which does get the flag from the loaded_vmcs :) but without actually having to add one.

Quoted 2 lines
Quoted 2 lines

I also see that enable_ept controls the setting of enable_unrestricted_guest. Perhaps both need to be moved to loaded_vmcs ?

Quoted 7 lines
Quoted 2 lines

No, letting L1 disable EPT in L0 would be pure insanity, and the overall paging mode of L2 is already reflected in the MMU.

Absolutely. Unrestricted guest requires EPT, but EPT is invisible to the guest. (Currently EPT requires guest MAXPHYADDR = host MAXPHYADDR, in the sense that the guest can detect that the host is lying about MAXPHYADDR; but that is really a bug that I hope will be fixed in 5.8, relaxing the requirement to guest MAXPHYADDR <= host PHYADDR).

Should EPT for the nested guest be set up in the normal way (PML4E -> PDPTE-> PDE -> PTE) when GUEST_CR0.PE is zero ? Or does it have to be a special set up like only the PTEs are needed because no protection and no paging are used ?

Quoted 5 lines

Paolo

Quoted 3 lines

The dependency on EPT is that VMX requires paging of some form and unrestricted guest allows entering non-root with CR0.PG=0, i.e. requires EPT be enabled.

maintainer
#10 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 11 lines · Krish Sadhukhan
Quoted 6 lines
Quoted 1 line

Absolutely.  Unrestricted guest requires EPT, but EPT is invisible to the guest.  (Currently EPT requires guest MAXPHYADDR = host MAXPHYADDR, in the sense that the guest can detect that the host is lying about MAXPHYADDR; but that is really a bug that I hope will be fixed in 5.8, relaxing the requirement to guest MAXPHYADDR <= host PHYADDR).

Should EPT for the nested guest be set up in the normal way (PML4E -> PDPTE-> PDE -> PTE) when GUEST_CR0.PE is zero ? Or does it have to be a special set up like only the PTEs are needed because no protection and no paging are used ?

I don't understand. When EPT is in use, the vmcs02 CR3 is simply set to the vmcs12 CR3.

Paolo

#11 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Krish Sadhukhan
Quoted 5 lines

Absolutely.  Unrestricted guest requires EPT, but EPT is invisible to the guest.  (Currently EPT requires guest MAXPHYADDR = host MAXPHYADDR, in the sense that the guest can detect that the host is lying about MAXPHYADDR; but that is really a bug that I hope will be fixed in 5.8, relaxing the requirement to guest MAXPHYADDR <= host PHYADDR).

Quoted 6 lines
Quoted 4 lines

Should EPT for the nested guest be set up in the normal way (PML4E -> PDPTE-> PDE -> PTE) when GUEST_CR0.PE is zero ? Or does it have to be a special set up like only the PTEs are needed because no protection and no paging are used ?

I don't understand. When EPT is in use, the vmcs02 CR3 is simply set to the vmcs12 CR3.

Sorry, I should have framed my question in a better way.

My question is  how should L1 in the test code set up EPTP for L2 when L2 is an unrestricted guest with no protection (GUEST_CR0.PE = 0) and no paging (GUEST_CR0.PG = 0) ? Should EPTP in test code be set up in the same way as when L2 is an unrestricted guest with protection and paging enabled ?

Getting confused by legacy 16-bit Real Mode and an unrestricted guest in Real Mode. :-)

Quoted 2 lines

Paolo

#12 lore raw

Re: [PATCH 1/2 v2] KVM: nVMX: KVM needs to unset "unrestricted guest" VM-execution control in vmcs02 if vmcs12 doesn't set it

Quoted 17 lines · Krish Sadhukhan
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Krish Sadhukhan
Quoted 5 lines

Absolutely. Unrestricted guest requires EPT, but EPT is invisible to the guest. (Currently EPT requires guest MAXPHYADDR = host MAXPHYADDR, in the sense that the guest can detect that the host is lying about MAXPHYADDR; but that is really a bug that I hope will be fixed in 5.8, relaxing the requirement to guest MAXPHYADDR <= host PHYADDR).

Quoted 6 lines
Quoted 4 lines

Should EPT for the nested guest be set up in the normal way (PML4E -> PDPTE-> PDE -> PTE) when GUEST_CR0.PE is zero ? Or does it have to be a special set up like only the PTEs are needed because no protection and no paging are used ?

I don't understand. When EPT is in use, the vmcs02 CR3 is simply set to the vmcs12 CR3.

Quoted 7 lines

Sorry, I should have framed my question in a better way.

My question is how should L1 in the test code set up EPTP for L2 when L2 is an unrestricted guest with no protection (GUEST_CR0.PE = 0) and no paging (GUEST_CR0.PG = 0) ? Should EPTP in test code be set up in the same way as when L2 is an unrestricted guest with protection and paging enabled ?

The EPT maps guest physical addresses to host physical addresses. That mapping is the same regardless of whether the guest is using paging or not.

#13 lore raw

PATCH v2 2/2 kvm-unit-tests: nVMX: Test Selector and Base Address fields of Guest Segment Registers on vmentry of nested guests

In reply to Krish Sadhukhan

According to section "Checks on Guest Segment Registers" in Intel SDM vol 3C, the following checks are performed on the Guest Segment Registers on vmentry of nested guests:

Selector fields:
— TR. The TI flag (bit 2) must be 0.
— LDTR. If LDTR is usable, the TI flag (bit 2) must be 0. — SS. If the guest will not be virtual-8086 and the "unrestricted
guest" VM-execution control is 0, the RPL (bits 1:0) must equal the RPL of the selector field for CS.1

Base-address fields:
— CS, SS, DS, ES, FS, GS. If the guest will be virtual-8086, the
address must be the selector field shifted left 4 bits (multiplied by 16).
— The following checks are performed on processors that support Intel
64 architecture:
TR, FS, GS. The address must be canonical.
LDTR. If LDTR is usable, the address must be canonical. CS. Bits 63:32 of the address must be zero.
SS, DS, ES. If the register is usable, bits 63:32 of the address must be zero.

---
 Makefile            |   2 +-
 lib/x86/processor.h |   1 +
 x86/vmx_tests.c     | 201 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 203 insertions(+), 1 deletion(-)
Makefile +1 -1
index 767b6c6..159eb64 100644
@@ -51,7 +51,7 @@ cc-option = $(shell if $(CC) -Werror $(1) -S -o /dev/null -xc /dev/null \
 
 COMMON_CFLAGS += -g $(autodepend-flags) -fno-strict-aliasing
 COMMON_CFLAGS += -Wall -Wwrite-strings -Wempty-body -Wuninitialized
-COMMON_CFLAGS += -Wignored-qualifiers -Werror
+COMMON_CFLAGS += -Wignored-qualifiers -Werror -Wno-unused-function
 
 frame-pointer-flag=-f$(if $(KEEP_FRAME_POINTER),no-,)omit-frame-pointer
 fomit_frame_pointer := $(call cc-option, $(frame-pointer-flag), "")
lib/x86/processor.h +1 -0
index 3adddee..fe4f482 100644
@@ -60,6 +60,7 @@
 #define X86_EFLAGS_OF    0x00000800
 #define X86_EFLAGS_IOPL  0x00003000
 #define X86_EFLAGS_NT    0x00004000
+#define X86_EFLAGS_VM    0x00020000
 #define X86_EFLAGS_AC    0x00040000
 
 #define X86_EFLAGS_ALU (X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | \
x86/vmx_tests.c +201 -0

219 lines not on this page · raw message

Signature · 1 line
2.20.1