Skip to contentSkip to conversation

KVM: nVMX: Fix read/write to MSR_IA32_FEATURE_CONTROL

PATCH Superseded Arthur Chunqi Li 2 participants 8 messages Jul 5, 2013 – Jul 7, 2013

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: nVMX: Fix read/write to MSR_IA32_FEATURE_CONTROL

Fix read/write to IA32_FEATURE_CONTROL MSR in nested environment.

This patch simulate this MSR in nested_vmx and the default value is 0x0. BIOS should set it to 0x5 before VMXON. After setting the lock bit, write to it will cause #GP(0).

---
 arch/x86/kvm/vmx.c |   25 +++++++++++++++++++++----
 arch/x86/kvm/x86.c |    3 ++-
 2 files changed, 23 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +21 -4
index 260a919..5e3d44e 100644
@@ -373,6 +373,7 @@ struct nested_vmx {
 	 * we must keep them pinned while L2 runs.
 	 */
 	struct page *apic_access_page;
+	u64 msr_ia32_feature_control;
 };
 
 #define POSTED_INTR_ON  0
@@ -2277,8 +2278,11 @@ static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
 
 	switch (msr_index) {
 	case MSR_IA32_FEATURE_CONTROL:
-		*pdata = 0;
-		break;
+		if (nested_vmx_allowed(vcpu)){
+			*pdata = to_vmx(vcpu)->nested.msr_ia32_feature_control;
+			break;
+		}
+		return 0;
 	case MSR_IA32_VMX_BASIC:
 		/*
 		 * This MSR reports some information about VMX support. We
@@ -2356,9 +2360,13 @@ static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
 	if (!nested_vmx_allowed(vcpu))
 		return 0;
 
-	if (msr_index == MSR_IA32_FEATURE_CONTROL)
-		/* TODO: the right thing. */
+	if (msr_index == MSR_IA32_FEATURE_CONTROL){
+		if (to_vmx(vcpu)->nested.msr_ia32_feature_control
+				& FEATURE_CONTROL_LOCKED)
+			return 0;
+		to_vmx(vcpu)->nested.msr_ia32_feature_control = data;
 		return 1;
+	}
 	/*
 	 * No need to treat VMX capability MSRs specially: If we don't handle
 	 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
@@ -5595,6 +5603,15 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 		skip_emulated_instruction(vcpu);
 		return 1;
 	}
+
+#define VMXON_NEEDED_FEATURES \
+	  (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
+	if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
+			!= VMXON_NEEDED_FEATURES) {
+		kvm_inject_gp(vcpu, 0);
+		return 1;
+	}
+
 	if (enable_shadow_vmcs) {
 		shadow_vmcs = alloc_vmcs();
 		if (!shadow_vmcs)
arch/x86/kvm/x86.c +2 -1
index e8ba99c..2d4eb8e 100644
@@ -850,7 +850,8 @@ static u32 msrs_to_save[] = {
 #ifdef CONFIG_X86_64
 	MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
-	MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
+	MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
+	MSR_IA32_FEATURE_CONTROL
 };
 
 static unsigned num_msrs_to_save;
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 7 lines · Arthur Chunqi Li

Fix read/write to IA32_FEATURE_CONTROL MSR in nested environment.

This patch simulate this MSR in nested_vmx and the default value is 0x0. BIOS should set it to 0x5 before VMXON. After setting the lock bit, write to it will cause #GP(0).

The patch is heavily based on Nadav's one. You need to preserve authorship. Add From: and Nadav's Sign-off-by.

Quoted 32 lines
---
 arch/x86/kvm/vmx.c |   25 +++++++++++++++++++++----
 arch/x86/kvm/x86.c |    3 ++-
 2 files changed, 23 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +6 -2
index 260a919..5e3d44e 100644
@@ -373,6 +373,7 @@ struct nested_vmx {
 	 * we must keep them pinned while L2 runs.
 	 */
 	struct page *apic_access_page;
+	u64 msr_ia32_feature_control;
 };
 
 #define POSTED_INTR_ON  0
@@ -2277,8 +2278,11 @@ static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
 
 	switch (msr_index) {
 	case MSR_IA32_FEATURE_CONTROL:
-		*pdata = 0;
-		break;
+		if (nested_vmx_allowed(vcpu)){
+			*pdata = to_vmx(vcpu)->nested.msr_ia32_feature_control;
+			break;
+		}
+		return 0;
 	case MSR_IA32_VMX_BASIC:
 		/*
 		 * This MSR reports some information about VMX support. We

arch/x86/kvm/vmx.c static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) · line 2367

Quoted 10 lines
arch/x86/kvm/vmx.c +5 -2
@@ -2356,9 +2360,13 @@ static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
 	if (!nested_vmx_allowed(vcpu))
 		return 0;
 
-	if (msr_index == MSR_IA32_FEATURE_CONTROL)
-		/* TODO: the right thing. */
+	if (msr_index == MSR_IA32_FEATURE_CONTROL){
+		if (to_vmx(vcpu)->nested.msr_ia32_feature_control
+				& FEATURE_CONTROL_LOCKED)
+			return 0;
+		to_vmx(vcpu)->nested.msr_ia32_feature_control = data;

You need to allow setting it from userspace, otherwise reset will not clear it.

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

Quoted 11 lines
arch/x86/kvm/vmx.c +3 -0
 		return 1;
+	}
 	/*
 	 * No need to treat VMX capability MSRs specially: If we don't handle
 	 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
@@ -5595,6 +5603,15 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
 		skip_emulated_instruction(vcpu);
 		return 1;
 	}
+
+#define VMXON_NEEDED_FEATURES \

Avi asked to use const u64 here.

Quoted 26 lines
arch/x86/kvm/vmx.c +7 -0
+	  (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
+	if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
+			!= VMXON_NEEDED_FEATURES) {
+		kvm_inject_gp(vcpu, 0);
+		return 1;
+	}
+
 	if (enable_shadow_vmcs) {
 		shadow_vmcs = alloc_vmcs();
 		if (!shadow_vmcs)
arch/x86/kvm/x86.c +2 -1
index e8ba99c..2d4eb8e 100644
@@ -850,7 +850,8 @@ static u32 msrs_to_save[] = {
 #ifdef CONFIG_X86_64
 	MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
-	MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
+	MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
+	MSR_IA32_FEATURE_CONTROL
 };
 
 static unsigned num_msrs_to_save;
Signature · 1 line
1.7.9.5

--
Gleb.

#2 lore raw
Quoted 46 lines · Gleb Natapov
Quoted 7 lines · Arthur Chunqi Li

Fix read/write to IA32_FEATURE_CONTROL MSR in nested environment.

This patch simulate this MSR in nested_vmx and the default value is 0x0. BIOS should set it to 0x5 before VMXON. After setting the lock bit, write to it will cause #GP(0).

The patch is heavily based on Nadav's one. You need to preserve authorship. Add From: and Nadav's Sign-off-by.

Quoted 35 lines
---
 arch/x86/kvm/vmx.c |   25 +++++++++++++++++++++----
 arch/x86/kvm/x86.c |    3 ++-
 2 files changed, 23 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +6 -2
index 260a919..5e3d44e 100644
@@ -373,6 +373,7 @@ struct nested_vmx {
       * we must keep them pinned while L2 runs.
       */
      struct page *apic_access_page;
+     u64 msr_ia32_feature_control;
 };
 
 #define POSTED_INTR_ON  0
@@ -2277,8 +2278,11 @@ static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
 
      switch (msr_index) {
      case MSR_IA32_FEATURE_CONTROL:
-             *pdata = 0;
-             break;
+             if (nested_vmx_allowed(vcpu)){
+                     *pdata = to_vmx(vcpu)->nested.msr_ia32_feature_control;
+                     break;
+             }
+             return 0;
      case MSR_IA32_VMX_BASIC:
              /*
               * This MSR reports some information about VMX support. We
@@ -2356,9 +2360,13 @@ static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
      if (!nested_vmx_allowed(vcpu))
              return 0;
 
Quoted 9 lines

arch/x86/kvm/vmx.c static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) · line 2367

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -2
-     if (msr_index == MSR_IA32_FEATURE_CONTROL)
-             /* TODO: the right thing. */
+     if (msr_index == MSR_IA32_FEATURE_CONTROL){
+             if (to_vmx(vcpu)->nested.msr_ia32_feature_control
+                             & FEATURE_CONTROL_LOCKED)
+                     return 0;
+             to_vmx(vcpu)->nested.msr_ia32_feature_control = data;

You need to allow setting it from userspace, otherwise reset will not clear it.

As to my memory, if the lock bit of IA32_FEATURE_CONTROL is set, this MSR cannot be set or reset. So what do you mean setting it from userspace?

Quoted 43 lines

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

Quoted 12 lines
arch/x86/kvm/vmx.c +3 -0
              return 1;
+     }
      /*
       * No need to treat VMX capability MSRs specially: If we don't handle
       * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
@@ -5595,6 +5603,15 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
              skip_emulated_instruction(vcpu);
              return 1;
      }
+
+#define VMXON_NEEDED_FEATURES \

Avi asked to use const u64 here.

Quoted 25 lines
arch/x86/kvm/vmx.c +7 -0
+       (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
+     if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
+                     != VMXON_NEEDED_FEATURES) {
+             kvm_inject_gp(vcpu, 0);
+             return 1;
+     }
+
      if (enable_shadow_vmcs) {
              shadow_vmcs = alloc_vmcs();
              if (!shadow_vmcs)
arch/x86/kvm/x86.c +2 -2
index e8ba99c..2d4eb8e 100644
@@ -850,7 +850,8 @@ static u32 msrs_to_save[] = {
 #ifdef CONFIG_X86_64
      MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
-     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
+     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
+     MSR_IA32_FEATURE_CONTROL
 };
 
 static unsigned num_msrs_to_save;
--
Quoted 1 line

1.7.9.5

--
Gleb.

#3 lore raw
Quoted 56 lines · Arthur Chunqi Li
Quoted 46 lines · Gleb Natapov
Quoted 7 lines · Arthur Chunqi Li

Fix read/write to IA32_FEATURE_CONTROL MSR in nested environment.

This patch simulate this MSR in nested_vmx and the default value is 0x0. BIOS should set it to 0x5 before VMXON. After setting the lock bit, write to it will cause #GP(0).

The patch is heavily based on Nadav's one. You need to preserve authorship. Add From: and Nadav's Sign-off-by.

Quoted 35 lines
---
 arch/x86/kvm/vmx.c |   25 +++++++++++++++++++++----
 arch/x86/kvm/x86.c |    3 ++-
 2 files changed, 23 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +6 -2
index 260a919..5e3d44e 100644
@@ -373,6 +373,7 @@ struct nested_vmx {
       * we must keep them pinned while L2 runs.
       */
      struct page *apic_access_page;
+     u64 msr_ia32_feature_control;
 };
 
 #define POSTED_INTR_ON  0
@@ -2277,8 +2278,11 @@ static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
 
      switch (msr_index) {
      case MSR_IA32_FEATURE_CONTROL:
-             *pdata = 0;
-             break;
+             if (nested_vmx_allowed(vcpu)){
+                     *pdata = to_vmx(vcpu)->nested.msr_ia32_feature_control;
+                     break;
+             }
+             return 0;
      case MSR_IA32_VMX_BASIC:
              /*
               * This MSR reports some information about VMX support. We
@@ -2356,9 +2360,13 @@ static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
      if (!nested_vmx_allowed(vcpu))
              return 0;
 
Quoted 9 lines

arch/x86/kvm/vmx.c static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) · line 2367

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -2
-     if (msr_index == MSR_IA32_FEATURE_CONTROL)
-             /* TODO: the right thing. */
+     if (msr_index == MSR_IA32_FEATURE_CONTROL){
+             if (to_vmx(vcpu)->nested.msr_ia32_feature_control
+                             & FEATURE_CONTROL_LOCKED)
+                     return 0;
+             to_vmx(vcpu)->nested.msr_ia32_feature_control = data;

You need to allow setting it from userspace, otherwise reset will not clear it.

Quoted 3 lines

As to my memory, if the lock bit of IA32_FEATURE_CONTROL is set, this MSR cannot be set or reset. So what do you mean setting it from userspace?

It can be reset by power cycling CPU obviously, so when userspace emulates power cycle reset it has to be able to zero the MSR. BTW you need to write corespondent QEMU patch to handle this MSR during reset and migration.

Quoted 37 lines
Quoted 37 lines

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

Quoted 12 lines
arch/x86/kvm/vmx.c +3 -0
              return 1;
+     }
      /*
       * No need to treat VMX capability MSRs specially: If we don't handle
       * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
@@ -5595,6 +5603,15 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
              skip_emulated_instruction(vcpu);
              return 1;
      }
+
+#define VMXON_NEEDED_FEATURES \

Avi asked to use const u64 here.

Quoted 23 lines
arch/x86/kvm/vmx.c +7 -0
+       (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
+     if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
+                     != VMXON_NEEDED_FEATURES) {
+             kvm_inject_gp(vcpu, 0);
+             return 1;
+     }
+
      if (enable_shadow_vmcs) {
              shadow_vmcs = alloc_vmcs();
              if (!shadow_vmcs)
arch/x86/kvm/x86.c +2 -1
index e8ba99c..2d4eb8e 100644
@@ -850,7 +850,8 @@ static u32 msrs_to_save[] = {
 #ifdef CONFIG_X86_64
      MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
-     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
+     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
+     MSR_IA32_FEATURE_CONTROL
 };
 
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
arch/x86/kvm/x86.c +0 -1
 static unsigned num_msrs_to_save;
--

1.7.9.5

--
Gleb.

--
Gleb.

#4 lore raw
Quoted 57 lines · Gleb Natapov
Quoted 56 lines · Arthur Chunqi Li
Quoted 55 lines · Gleb Natapov
Quoted 7 lines · Arthur Chunqi Li

Fix read/write to IA32_FEATURE_CONTROL MSR in nested environment.

This patch simulate this MSR in nested_vmx and the default value is 0x0. BIOS should set it to 0x5 before VMXON. After setting the lock bit, write to it will cause #GP(0).

The patch is heavily based on Nadav's one. You need to preserve authorship. Add From: and Nadav's Sign-off-by.

Quoted 32 lines
---
 arch/x86/kvm/vmx.c |   25 +++++++++++++++++++++----
 arch/x86/kvm/x86.c |    3 ++-
 2 files changed, 23 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +6 -2
index 260a919..5e3d44e 100644
@@ -373,6 +373,7 @@ struct nested_vmx {
       * we must keep them pinned while L2 runs.
       */
      struct page *apic_access_page;
+     u64 msr_ia32_feature_control;
 };
 
 #define POSTED_INTR_ON  0
@@ -2277,8 +2278,11 @@ static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
 
      switch (msr_index) {
      case MSR_IA32_FEATURE_CONTROL:
-             *pdata = 0;
-             break;
+             if (nested_vmx_allowed(vcpu)){
+                     *pdata = to_vmx(vcpu)->nested.msr_ia32_feature_control;
+                     break;
+             }
+             return 0;
      case MSR_IA32_VMX_BASIC:
              /*
               * This MSR reports some information about VMX support. We

arch/x86/kvm/vmx.c static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) · line 2367

Quoted 10 lines
arch/x86/kvm/vmx.c +5 -2
@@ -2356,9 +2360,13 @@ static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
      if (!nested_vmx_allowed(vcpu))
              return 0;
 
-     if (msr_index == MSR_IA32_FEATURE_CONTROL)
-             /* TODO: the right thing. */
+     if (msr_index == MSR_IA32_FEATURE_CONTROL){
+             if (to_vmx(vcpu)->nested.msr_ia32_feature_control
+                             & FEATURE_CONTROL_LOCKED)
+                     return 0;
+             to_vmx(vcpu)->nested.msr_ia32_feature_control = data;

You need to allow setting it from userspace, otherwise reset will not clear it.

Quoted 6 lines
Quoted 3 lines

As to my memory, if the lock bit of IA32_FEATURE_CONTROL is set, this MSR cannot be set or reset. So what do you mean setting it from userspace?

It can be reset by power cycling CPU obviously, so when userspace emulates power cycle reset it has to be able to zero the MSR. BTW you need to write corespondent QEMU patch to handle this MSR during reset and migration.

What is the current call trace when QEMU do system_reset? Will it trigger vmx_vcpu_reset() in vmx.c?

Quoted 45 lines
Quoted 38 lines
Quoted 38 lines

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

Quoted 13 lines
arch/x86/kvm/vmx.c +3 -0
              return 1;
+     }
      /*
       * No need to treat VMX capability MSRs specially: If we don't handle
       * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
@@ -5595,6 +5603,15 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
              skip_emulated_instruction(vcpu);
              return 1;
      }
+
+#define VMXON_NEEDED_FEATURES \

Avi asked to use const u64 here.

Quoted 23 lines
arch/x86/kvm/vmx.c +7 -0
+       (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
+     if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
+                     != VMXON_NEEDED_FEATURES) {
+             kvm_inject_gp(vcpu, 0);
+             return 1;
+     }
+
      if (enable_shadow_vmcs) {
              shadow_vmcs = alloc_vmcs();
              if (!shadow_vmcs)
arch/x86/kvm/x86.c +2 -1
index e8ba99c..2d4eb8e 100644
@@ -850,7 +850,8 @@ static u32 msrs_to_save[] = {
 #ifdef CONFIG_X86_64
      MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
-     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
+     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
+     MSR_IA32_FEATURE_CONTROL
 };
 
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
arch/x86/kvm/x86.c +0 -1
 static unsigned num_msrs_to_save;
--

1.7.9.5

--
Gleb.

Quoted 2 lines

--
Gleb.

--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China

#5 lore raw
Quoted 58 lines · Arthur Chunqi Li
Quoted 57 lines · Gleb Natapov
Quoted 56 lines · Arthur Chunqi Li
Quoted 55 lines · Gleb Natapov
Quoted 7 lines · Arthur Chunqi Li

Fix read/write to IA32_FEATURE_CONTROL MSR in nested environment.

This patch simulate this MSR in nested_vmx and the default value is 0x0. BIOS should set it to 0x5 before VMXON. After setting the lock bit, write to it will cause #GP(0).

The patch is heavily based on Nadav's one. You need to preserve authorship. Add From: and Nadav's Sign-off-by.

Quoted 32 lines
---
 arch/x86/kvm/vmx.c |   25 +++++++++++++++++++++----
 arch/x86/kvm/x86.c |    3 ++-
 2 files changed, 23 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +6 -2
index 260a919..5e3d44e 100644
@@ -373,6 +373,7 @@ struct nested_vmx {
       * we must keep them pinned while L2 runs.
       */
      struct page *apic_access_page;
+     u64 msr_ia32_feature_control;
 };
 
 #define POSTED_INTR_ON  0
@@ -2277,8 +2278,11 @@ static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
 
      switch (msr_index) {
      case MSR_IA32_FEATURE_CONTROL:
-             *pdata = 0;
-             break;
+             if (nested_vmx_allowed(vcpu)){
+                     *pdata = to_vmx(vcpu)->nested.msr_ia32_feature_control;
+                     break;
+             }
+             return 0;
      case MSR_IA32_VMX_BASIC:
              /*
               * This MSR reports some information about VMX support. We

arch/x86/kvm/vmx.c static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) · line 2367

Quoted 10 lines
arch/x86/kvm/vmx.c +5 -2
@@ -2356,9 +2360,13 @@ static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
      if (!nested_vmx_allowed(vcpu))
              return 0;
 
-     if (msr_index == MSR_IA32_FEATURE_CONTROL)
-             /* TODO: the right thing. */
+     if (msr_index == MSR_IA32_FEATURE_CONTROL){
+             if (to_vmx(vcpu)->nested.msr_ia32_feature_control
+                             & FEATURE_CONTROL_LOCKED)
+                     return 0;
+             to_vmx(vcpu)->nested.msr_ia32_feature_control = data;

You need to allow setting it from userspace, otherwise reset will not clear it.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

As to my memory, if the lock bit of IA32_FEATURE_CONTROL is set, this MSR cannot be set or reset. So what do you mean setting it from userspace?

It can be reset by power cycling CPU obviously, so when userspace emulates power cycle reset it has to be able to zero the MSR. BTW you need to write corespondent QEMU patch to handle this MSR during reset and migration.

What is the current call trace when QEMU do system_reset? Will it trigger vmx_vcpu_reset() in vmx.c?

It will not. QEMU inits vcpu state in userspace and write it into the kernel with set_regs/set_srges ioctls.

Quoted 50 lines
Quoted 45 lines
Quoted 38 lines
Quoted 38 lines

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

Quoted 13 lines
arch/x86/kvm/vmx.c +3 -0
              return 1;
+     }
      /*
       * No need to treat VMX capability MSRs specially: If we don't handle
       * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
@@ -5595,6 +5603,15 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
              skip_emulated_instruction(vcpu);
              return 1;
      }
+
+#define VMXON_NEEDED_FEATURES \

Avi asked to use const u64 here.

Quoted 23 lines
arch/x86/kvm/vmx.c +7 -0
+       (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
+     if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
+                     != VMXON_NEEDED_FEATURES) {
+             kvm_inject_gp(vcpu, 0);
+             return 1;
+     }
+
      if (enable_shadow_vmcs) {
              shadow_vmcs = alloc_vmcs();
              if (!shadow_vmcs)
arch/x86/kvm/x86.c +2 -1
index e8ba99c..2d4eb8e 100644
@@ -850,7 +850,8 @@ static u32 msrs_to_save[] = {
 #ifdef CONFIG_X86_64
      MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
-     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
+     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
+     MSR_IA32_FEATURE_CONTROL
 };
 
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
arch/x86/kvm/x86.c +0 -1
 static unsigned num_msrs_to_save;
--

1.7.9.5

--
Gleb.

Quoted 2 lines

--
Gleb.

Quoted 6 lines

--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China

--
Gleb.

#6 lore raw
Quoted 62 lines · Gleb Natapov
Quoted 61 lines · Arthur Chunqi Li
Quoted 60 lines · Gleb Natapov
Quoted 59 lines · Arthur Chunqi Li
Quoted 46 lines · Gleb Natapov
Quoted 7 lines · Arthur Chunqi Li

Fix read/write to IA32_FEATURE_CONTROL MSR in nested environment.

This patch simulate this MSR in nested_vmx and the default value is 0x0. BIOS should set it to 0x5 before VMXON. After setting the lock bit, write to it will cause #GP(0).

The patch is heavily based on Nadav's one. You need to preserve authorship. Add From: and Nadav's Sign-off-by.

Quoted 35 lines
---
 arch/x86/kvm/vmx.c |   25 +++++++++++++++++++++----
 arch/x86/kvm/x86.c |    3 ++-
 2 files changed, 23 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +6 -2
index 260a919..5e3d44e 100644
@@ -373,6 +373,7 @@ struct nested_vmx {
       * we must keep them pinned while L2 runs.
       */
      struct page *apic_access_page;
+     u64 msr_ia32_feature_control;
 };
 
 #define POSTED_INTR_ON  0
@@ -2277,8 +2278,11 @@ static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
 
      switch (msr_index) {
      case MSR_IA32_FEATURE_CONTROL:
-             *pdata = 0;
-             break;
+             if (nested_vmx_allowed(vcpu)){
+                     *pdata = to_vmx(vcpu)->nested.msr_ia32_feature_control;
+                     break;
+             }
+             return 0;
      case MSR_IA32_VMX_BASIC:
              /*
               * This MSR reports some information about VMX support. We
@@ -2356,9 +2360,13 @@ static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
      if (!nested_vmx_allowed(vcpu))
              return 0;
 
Quoted 9 lines

arch/x86/kvm/vmx.c static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) · line 2367

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -2
-     if (msr_index == MSR_IA32_FEATURE_CONTROL)
-             /* TODO: the right thing. */
+     if (msr_index == MSR_IA32_FEATURE_CONTROL){
+             if (to_vmx(vcpu)->nested.msr_ia32_feature_control
+                             & FEATURE_CONTROL_LOCKED)
+                     return 0;
+             to_vmx(vcpu)->nested.msr_ia32_feature_control = data;

You need to allow setting it from userspace, otherwise reset will not clear it.

As to my memory, if the lock bit of IA32_FEATURE_CONTROL is set, this MSR cannot be set or reset. So what do you mean setting it from userspace?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

It can be reset by power cycling CPU obviously, so when userspace emulates power cycle reset it has to be able to zero the MSR. BTW you need to write corespondent QEMU patch to handle this MSR during reset and migration.

What is the current call trace when QEMU do system_reset? Will it trigger vmx_vcpu_reset() in vmx.c?

It will not. QEMU inits vcpu state in userspace and write it into the kernel with set_regs/set_srges ioctls.

So how will this achieve isolation and protection? I mean if QEMU can reset this MSR when reboot, can user in OS also reset it? Actually, after setting to lock bit, it cannot be set until next reboot.

Quoted 60 lines
Quoted 51 lines
Quoted 46 lines
Quoted 39 lines
Quoted 39 lines
Quoted 9 lines
arch/x86/kvm/vmx.c +1 -0
              return 1;
+     }
      /*
       * No need to treat VMX capability MSRs specially: If we don't handle
       * them, handle_wrmsr will #GP(0), which is correct (they are readonly)

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

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
@@ -5595,6 +5603,15 @@ static int handle_vmon(struct kvm_vcpu *vcpu)
              skip_emulated_instruction(vcpu);
              return 1;
      }
+
+#define VMXON_NEEDED_FEATURES \

Avi asked to use const u64 here.

Quoted 23 lines
arch/x86/kvm/vmx.c +7 -0
+       (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
+     if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
+                     != VMXON_NEEDED_FEATURES) {
+             kvm_inject_gp(vcpu, 0);
+             return 1;
+     }
+
      if (enable_shadow_vmcs) {
              shadow_vmcs = alloc_vmcs();
              if (!shadow_vmcs)
arch/x86/kvm/x86.c +2 -1
index e8ba99c..2d4eb8e 100644
@@ -850,7 +850,8 @@ static u32 msrs_to_save[] = {
 #ifdef CONFIG_X86_64
      MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
-     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
+     MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
+     MSR_IA32_FEATURE_CONTROL
 };
 
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
arch/x86/kvm/x86.c +0 -1
 static unsigned num_msrs_to_save;
--

1.7.9.5

--
Gleb.

Quoted 2 lines

--
Gleb.

Quoted 6 lines

--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China

--
Gleb.

#7 lore raw
Quoted 66 lines · Arthur Chunqi Li
Quoted 65 lines · Gleb Natapov
Quoted 64 lines · Arthur Chunqi Li
Quoted 63 lines · Gleb Natapov
Quoted 56 lines · Arthur Chunqi Li
Quoted 46 lines · Gleb Natapov
Quoted 7 lines · Arthur Chunqi Li

Fix read/write to IA32_FEATURE_CONTROL MSR in nested environment.

This patch simulate this MSR in nested_vmx and the default value is 0x0. BIOS should set it to 0x5 before VMXON. After setting the lock bit, write to it will cause #GP(0).

The patch is heavily based on Nadav's one. You need to preserve authorship. Add From: and Nadav's Sign-off-by.

Quoted 35 lines
---
 arch/x86/kvm/vmx.c |   25 +++++++++++++++++++++----
 arch/x86/kvm/x86.c |    3 ++-
 2 files changed, 23 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +6 -2
index 260a919..5e3d44e 100644
@@ -373,6 +373,7 @@ struct nested_vmx {
       * we must keep them pinned while L2 runs.
       */
      struct page *apic_access_page;
+     u64 msr_ia32_feature_control;
 };
 
 #define POSTED_INTR_ON  0
@@ -2277,8 +2278,11 @@ static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
 
      switch (msr_index) {
      case MSR_IA32_FEATURE_CONTROL:
-             *pdata = 0;
-             break;
+             if (nested_vmx_allowed(vcpu)){
+                     *pdata = to_vmx(vcpu)->nested.msr_ia32_feature_control;
+                     break;
+             }
+             return 0;
      case MSR_IA32_VMX_BASIC:
              /*
               * This MSR reports some information about VMX support. We
@@ -2356,9 +2360,13 @@ static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
      if (!nested_vmx_allowed(vcpu))
              return 0;
 
Quoted 9 lines

arch/x86/kvm/vmx.c static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) · line 2367

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -2
-     if (msr_index == MSR_IA32_FEATURE_CONTROL)
-             /* TODO: the right thing. */
+     if (msr_index == MSR_IA32_FEATURE_CONTROL){
+             if (to_vmx(vcpu)->nested.msr_ia32_feature_control
+                             & FEATURE_CONTROL_LOCKED)
+                     return 0;
+             to_vmx(vcpu)->nested.msr_ia32_feature_control = data;

You need to allow setting it from userspace, otherwise reset will not clear it.

Quoted 3 lines

As to my memory, if the lock bit of IA32_FEATURE_CONTROL is set, this MSR cannot be set or reset. So what do you mean setting it from userspace?

It can be reset by power cycling CPU obviously, so when userspace emulates power cycle reset it has to be able to zero the MSR. BTW you need to write corespondent QEMU patch to handle this MSR during reset and migration.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

What is the current call trace when QEMU do system_reset? Will it trigger vmx_vcpu_reset() in vmx.c?

It will not. QEMU inits vcpu state in userspace and write it into the kernel with set_regs/set_srges ioctls.

So how will this achieve isolation and protection? I mean if QEMU can reset this MSR when reboot, can user in OS also reset it? Actually, after setting to lock bit, it cannot be set until next reboot.

There is a way to tell if msr write is done by QEMU or a guest OS.

--
Gleb.