Skip to contentSkip to conversation

KVM: SVM: Use do_machine_check to pass MCE to the host

RFC PATCH In review Uros Bizjak 3 participants 5 messages Apr 11, 2020 – Apr 14, 2020

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

RFC PATCH 1/1 KVM: SVM: Use do_machine_check to pass MCE to the host

Use do_machine_check instead of INT $12 to pass MCE to the host, the same approach VMX uses.

On a related note, there is no reason to limit the use of do_machine_check to 64 bit targets, as is currently done for VMX. MCE handling works for both target families.

The patch is only compile tested, for both, 64 and 32 bit targets, someone should test the passing of the exception by injecting some MCEs into the guest.

For future non-RFC patch, kvm_machine_check should be moved to some appropriate header file.

Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Sean Christopherson <[email protected]>
---
 arch/x86/kvm/svm/svm.c | 26 +++++++++++++++++++++-----
 arch/x86/kvm/vmx/vmx.c |  2 +-
 2 files changed, 22 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/svm.c +21 -5
index 061d19e69c73..cd773f6261e3 100644
@@ -33,6 +33,7 @@
 #include <asm/debugreg.h>
 #include <asm/kvm_para.h>
 #include <asm/irq_remapping.h>
+#include <asm/mce.h>
 #include <asm/spec-ctrl.h>
 #include <asm/cpu_device_id.h>
 
@@ -1839,6 +1840,25 @@ static bool is_erratum_383(void)
 	return true;
 }
 
+/*
+ * Trigger machine check on the host. We assume all the MSRs are already set up
+ * by the CPU and that we still run on the same CPU as the MCE occurred on.
+ * We pass a fake environment to the machine check handler because we want
+ * the guest to be always treated like user space, no matter what context
+ * it used internally.
+ */
+static void kvm_machine_check(void)
+{
+#if defined(CONFIG_X86_MCE)
+	struct pt_regs regs = {
+		.cs = 3, /* Fake ring 3 no matter what the guest ran on */
+		.flags = X86_EFLAGS_IF,
+	};
+
+	do_machine_check(&regs, 0);
+#endif
+}
+
 static void svm_handle_mce(struct vcpu_svm *svm)
 {
 	if (is_erratum_383()) {
@@ -1857,11 +1877,7 @@ static void svm_handle_mce(struct vcpu_svm *svm)
 	 * On an #MC intercept the MCE handler is not called automatically in
 	 * the host. So do it by hand here.
 	 */
-	asm volatile (
-		"int $0x12\n");
-	/* not sure if we ever come back to this point */
-
-	return;
+	kvm_machine_check();
 }
 
 static int mc_interception(struct vcpu_svm *svm)
arch/x86/kvm/vmx/vmx.c +1 -1
index 8959514eaf0f..01330096ff3e 100644
@@ -4572,7 +4572,7 @@ static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  */
 static void kvm_machine_check(void)
 {
-#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
+#if defined(CONFIG_X86_MCE)
 	struct pt_regs regs = {
 		.cs = 3, /* Fake ring 3 no matter what the guest ran on */
 		.flags = X86_EFLAGS_IF,
Signature · 1 line
2.25.2
#1 lore raw
Quoted 6 lines · Uros Bizjak

Use do_machine_check instead of INT $12 to pass MCE to the host, the same approach VMX uses.

On a related note, there is no reason to limit the use of do_machine_check to 64 bit targets, as is currently done for VMX. MCE handling works for both target families.

...

arch/x86/kvm/vmx/vmx.c static int handle_rmode_exception(struct kvm_vcpu *vcpu, · line 4575

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +1 -1
index 8959514eaf0f..01330096ff3e 100644
@@ -4572,7 +4572,7 @@ static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  */
 static void kvm_machine_check(void)
 {
-#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
+#if defined(CONFIG_X86_MCE)

This VMX change needs to be a separate patch, and it arguably warrants:

Fixes a0861c02a981 ("KVM: Add VT-x machine check support")
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +0 -0
 	struct pt_regs regs = {
 		.cs = 3, /* Fake ring 3 no matter what the guest ran on */
 		.flags = X86_EFLAGS_IF,
Signature · 1 line
2.25.2
maintainer
#2 lore raw
In reply to Uros Bizjak
Quoted 64 lines · Uros Bizjak

Use do_machine_check instead of INT $12 to pass MCE to the host, the same approach VMX uses.

On a related note, there is no reason to limit the use of do_machine_check to 64 bit targets, as is currently done for VMX. MCE handling works for both target families.

The patch is only compile tested, for both, 64 and 32 bit targets, someone should test the passing of the exception by injecting some MCEs into the guest.

For future non-RFC patch, kvm_machine_check should be moved to some appropriate header file.

Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Sean Christopherson <[email protected]>
---
 arch/x86/kvm/svm/svm.c | 26 +++++++++++++++++++++-----
 arch/x86/kvm/vmx/vmx.c |  2 +-
 2 files changed, 22 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/svm.c +20 -0
index 061d19e69c73..cd773f6261e3 100644
@@ -33,6 +33,7 @@
 #include <asm/debugreg.h>
 #include <asm/kvm_para.h>
 #include <asm/irq_remapping.h>
+#include <asm/mce.h>
 #include <asm/spec-ctrl.h>
 #include <asm/cpu_device_id.h>
 
@@ -1839,6 +1840,25 @@ static bool is_erratum_383(void)
 	return true;
 }
 
+/*
+ * Trigger machine check on the host. We assume all the MSRs are already set up
+ * by the CPU and that we still run on the same CPU as the MCE occurred on.
+ * We pass a fake environment to the machine check handler because we want
+ * the guest to be always treated like user space, no matter what context
+ * it used internally.
+ */
+static void kvm_machine_check(void)
+{
+#if defined(CONFIG_X86_MCE)
+	struct pt_regs regs = {
+		.cs = 3, /* Fake ring 3 no matter what the guest ran on */
+		.flags = X86_EFLAGS_IF,
+	};
+
+	do_machine_check(&regs, 0);
+#endif
+}
+
 static void svm_handle_mce(struct vcpu_svm *svm)
 {
 	if (is_erratum_383()) {
@@ -1857,11 +1877,7 @@ static void svm_handle_mce(struct vcpu_svm *svm)
 	 * On an #MC intercept the MCE handler is not called automatically in
 	 * the host. So do it by hand here.

arch/x86/kvm/svm/svm.c static void svm_handle_mce(struct vcpu_svm *svm) · line 1883

Quoted 10 lines
arch/x86/kvm/svm/svm.c +1 -5
 	 */
-	asm volatile (
-		"int $0x12\n");
-	/* not sure if we ever come back to this point */
-
-	return;
+	kvm_machine_check();
 }
 
 static int mc_interception(struct vcpu_svm *svm)

Looks good, but please move kvm_machine_check() to x86.c instead.

Paolo

#3 lore raw
Quoted 75 lines · Paolo Bonzini
Quoted 73 lines · Uros Bizjak

Use do_machine_check instead of INT $12 to pass MCE to the host, the same approach VMX uses.

On a related note, there is no reason to limit the use of do_machine_check to 64 bit targets, as is currently done for VMX. MCE handling works for both target families.

The patch is only compile tested, for both, 64 and 32 bit targets, someone should test the passing of the exception by injecting some MCEs into the guest.

For future non-RFC patch, kvm_machine_check should be moved to some appropriate header file.

Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Sean Christopherson <[email protected]>
---
 arch/x86/kvm/svm/svm.c | 26 +++++++++++++++++++++-----
 arch/x86/kvm/vmx/vmx.c |  2 +-
 2 files changed, 22 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/svm.c +21 -5
index 061d19e69c73..cd773f6261e3 100644
@@ -33,6 +33,7 @@
 #include <asm/debugreg.h>
 #include <asm/kvm_para.h>
 #include <asm/irq_remapping.h>
+#include <asm/mce.h>
 #include <asm/spec-ctrl.h>
 #include <asm/cpu_device_id.h>
 
@@ -1839,6 +1840,25 @@ static bool is_erratum_383(void)
      return true;
 }
 
+/*
+ * Trigger machine check on the host. We assume all the MSRs are already set up
+ * by the CPU and that we still run on the same CPU as the MCE occurred on.
+ * We pass a fake environment to the machine check handler because we want
+ * the guest to be always treated like user space, no matter what context
+ * it used internally.
+ */
+static void kvm_machine_check(void)
+{
+#if defined(CONFIG_X86_MCE)
+     struct pt_regs regs = {
+             .cs = 3, /* Fake ring 3 no matter what the guest ran on */
+             .flags = X86_EFLAGS_IF,
+     };
+
+     do_machine_check(&regs, 0);
+#endif
+}
+
 static void svm_handle_mce(struct vcpu_svm *svm)
 {
      if (is_erratum_383()) {
@@ -1857,11 +1877,7 @@ static void svm_handle_mce(struct vcpu_svm *svm)
       * On an #MC intercept the MCE handler is not called automatically in
       * the host. So do it by hand here.
       */
-     asm volatile (
-             "int $0x12\n");
-     /* not sure if we ever come back to this point */
-
-     return;
+     kvm_machine_check();
 }
 
Quoted 3 lines

arch/x86/kvm/svm/svm.c static void svm_handle_mce(struct vcpu_svm *svm) · line 1883

Quoted 1 line
arch/x86/kvm/svm/svm.c +0 -0
 static int mc_interception(struct vcpu_svm *svm)

Looks good, but please move kvm_machine_check() to x86.c instead.

Will do, after the confirmation that the patch works for AMD hosts.

OTOH, the function is just a simple wrapper around do_machine_check, so I was thinking to move it to a kvm_host.h header as a static inline. This way, we could save a call to a wrapper function.

Uros.

maintainer
#4 lore raw
Quoted 1 line · Uros Bizjak

Will do, after the confirmation that the patch works for AMD hosts.

Ok, I will test it (I found an AMD machine with EINJ support). In the meanwhile I queued the VMX part.

Quoted 3 lines

OTOH, the function is just a simple wrapper around do_machine_check, so I was thinking to move it to a kvm_host.h header as a static inline. This way, we could save a call to a wrapper function.

Yes, that works too.

Paolo