Skip to contentSkip to conversation

QEMU: MCE: Add MCE simulation to qemu/tcg

PATCH In review Huang Ying 3 participants 7 messages Jun 23, 2009 – Jul 10, 2009 KVM

lore
Share & tools
lore

Huang Ying

author
#0 lore raw

PATCH 1/1 QEMU: MCE: Add MCE simulation to qemu/tcg

- MCE features are initialized when VCPU is intialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.
- A new interrupt mask: CPU_INTERRUPT_MCE is added to inject the MCE.

ChangeLog:

v6:

- Rebased on latest qemu.git

v5:

- Rebased on latest qemu-kvm.git

v4:

- Add savevm/loadvm support

---
 cpu-all.h               |    4 ++
 cpu-exec.c              |    4 ++
 monitor.c               |   46 +++++++++++++++++++++++++++++++
 qemu-monitor.hx         |    8 +++++
 target-i386/cpu.h       |   24 +++++++++++++++-
 target-i386/helper.c    |   70 ++++++++++++++++++++++++++++++++++++++++++++++++
 target-i386/machine.c   |   27 ++++++++++++++++++
 target-i386/op_helper.c |   34 +++++++++++++++++++++++
 8 files changed, 216 insertions(+), 1 deletion(-)
target-i386/cpu.h +23 -1
@@ -204,6 +204,7 @@
 #define CR4_DE_MASK   (1 << 3)
 #define CR4_PSE_MASK  (1 << 4)
 #define CR4_PAE_MASK  (1 << 5)
+#define CR4_MCE_MASK  (1 << 6)
 #define CR4_PGE_MASK  (1 << 7)
 #define CR4_PCE_MASK  (1 << 8)
 #define CR4_OSFXSR_SHIFT 9
@@ -250,6 +251,17 @@
 #define PG_ERROR_RSVD_MASK 0x08
 #define PG_ERROR_I_D_MASK  0x10
 
+#define MCG_CTL_P	(1UL<<8)   /* MCG_CAP register available */
+
+#define MCE_CAP_DEF	MCG_CTL_P
+#define MCE_BANKS_DEF	10
+
+#define MCG_STATUS_MCIP	(1UL<<2)   /* machine check in progress */
+
+#define MCI_STATUS_VAL	(1UL<<63)  /* valid error */
+#define MCI_STATUS_OVER	(1UL<<62)  /* previous errors lost */
+#define MCI_STATUS_UC	(1UL<<61)  /* uncorrected error */
+
 #define MSR_IA32_TSC                    0x10
 #define MSR_IA32_APICBASE               0x1b
 #define MSR_IA32_APICBASE_BSP           (1<<8)
@@ -290,6 +302,11 @@
 
 #define MSR_MTRRdefType			0x2ff
 
+#define MSR_MC0_CTL			0x400
+#define MSR_MC0_STATUS			0x401
+#define MSR_MC0_ADDR			0x402
+#define MSR_MC0_MISC			0x403
+
 #define MSR_EFER                        0xc0000080
 
 #define MSR_EFER_SCE   (1 << 0)
@@ -677,6 +694,11 @@ typedef struct CPUX86State {
     /* in order to simplify APIC support, we leave this pointer to the
        user */
     struct APICState *apic_state;
+
+    uint64 mcg_cap;
+    uint64 mcg_status;
+    uint64 mcg_ctl;
+    uint64 *mce_banks;
 } CPUX86State;
 
 CPUX86State *cpu_x86_init(const char *cpu_model);
@@ -841,7 +863,7 @@ static inline int cpu_get_time_fast(void
 #define cpu_signal_handler cpu_x86_signal_handler
 #define cpu_list x86_cpu_list
 
-#define CPU_SAVE_VERSION 9
+#define CPU_SAVE_VERSION 10
 
 /* MMU modes definitions */
 #define MMU_MODE0_SUFFIX _kernel
target-i386/op_helper.c +34 -0
@@ -3133,7 +3133,23 @@ void helper_wrmsr(void)
     case MSR_MTRRdefType:
         env->mtrr_deftype = val;
         break;
+    case MSR_MCG_STATUS:
+        env->mcg_status = val;
+        break;
+    case MSR_MCG_CTL:
+        if ((env->mcg_cap & MCG_CTL_P)
+            && (val == 0 || val == ~(uint64_t)0))
+            env->mcg_ctl = val;
+        break;
     default:
+        if ((uint32_t)ECX >= MSR_MC0_CTL
+            && (uint32_t)ECX < MSR_MC0_CTL + (4 * env->mcg_cap & 0xff)) {
+            uint32_t offset = (uint32_t)ECX - MSR_MC0_CTL;
+            if ((offset & 0x3) != 0
+                || (val == 0 || val == ~(uint64_t)0))
+                env->mce_banks[offset] = val;
+            break;
+        }
         /* XXX: exception ? */
         break;
     }
@@ -3252,7 +3268,25 @@ void helper_rdmsr(void)
             /* XXX: exception ? */
             val = 0;
         break;
+    case MSR_MCG_CAP:
+        val = env->mcg_cap;
+        break;
+    case MSR_MCG_CTL:
+        if (env->mcg_cap & MCG_CTL_P)
+            val = env->mcg_ctl;
+        else
+            val = 0;
+        break;
+    case MSR_MCG_STATUS:
+        val = env->mcg_status;
+        break;
     default:
+        if ((uint32_t)ECX >= MSR_MC0_CTL
+            && (uint32_t)ECX < MSR_MC0_CTL + (4 * env->mcg_cap & 0xff)) {
+            uint32_t offset = (uint32_t)ECX - MSR_MC0_CTL;
+            val = env->mce_banks[offset];
+            break;
+        }
         /* XXX: exception ? */
         val = 0;
         break;
target-i386/helper.c +70 -0

82 lines not on this page · raw message

monitor.c +46 -0

64 lines not on this page · raw message

cpu-all.h +4 -0
@@ -770,6 +770,7 @@ extern int use_icount;
 #define CPU_INTERRUPT_NMI    0x200 /* NMI pending. */
 #define CPU_INTERRUPT_INIT   0x400 /* INIT pending. */
 #define CPU_INTERRUPT_SIPI   0x800 /* SIPI pending. */
+#define CPU_INTERRUPT_MCE    0x1000 /* (x86 only) MCE pending. */
 
 void cpu_interrupt(CPUState *s, int mask);
 void cpu_reset_interrupt(CPUState *env, int mask);
@@ -1071,4 +1072,7 @@ extern int64_t kqemu_ret_excp_count;
 extern int64_t kqemu_ret_intr_count;
 #endif
 
+void cpu_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
+                        uint64_t mcg_status, uint64_t addr, uint64_t misc);
+
 #endif /* CPU_ALL_H */
cpu-exec.c +4 -0
@@ -400,6 +400,10 @@ int cpu_exec(CPUState *env1)
                             env->hflags2 |= HF2_NMI_MASK;
                             do_interrupt(EXCP02_NMI, 0, 0, 0, 1);
                             next_tb = 0;
+			} else if (interrupt_request & CPU_INTERRUPT_MCE) {
+                            env->interrupt_request &= ~CPU_INTERRUPT_MCE;
+                            do_interrupt(EXCP12_MCHK, 0, 0, 0, 0);
+                            next_tb = 0;
                         } else if ((interrupt_request & CPU_INTERRUPT_HARD) &&
                                    (((env->hflags2 & HF2_VINTR_MASK) && 
                                      (env->hflags2 & HF2_HIF_MASK)) ||
target-i386/machine.c +27 -0
@@ -146,6 +146,19 @@ void cpu_save(QEMUFile *f, void *opaque)
     }
     qemu_put_be64s(f, &env->tsc);
     qemu_put_be32s(f, &env->mp_state);
+
+    /* MCE */
+    qemu_put_be64s(f, &env->mcg_cap);
+    if (env->mcg_cap) {
+        qemu_put_be64s(f, &env->mcg_status);
+        qemu_put_be64s(f, &env->mcg_ctl);
+        for (i = 0; i < (env->mcg_cap & 0xff); i++) {
+            qemu_put_be64s(f, &env->mce_banks[4*i]);
+            qemu_put_be64s(f, &env->mce_banks[4*i + 1]);
+            qemu_put_be64s(f, &env->mce_banks[4*i + 2]);
+            qemu_put_be64s(f, &env->mce_banks[4*i + 3]);
+        }
+    }
 }
 
 #ifdef USE_X86LDOUBLE
@@ -332,6 +345,20 @@ int cpu_load(QEMUFile *f, void *opaque, 
         qemu_get_be32s(f, &env->mp_state);
     }
 
+    if (version_id >= 10) {
+        qemu_get_be64s(f, &env->mcg_cap);
+        if (env->mcg_cap) {
+            qemu_get_be64s(f, &env->mcg_status);
+            qemu_get_be64s(f, &env->mcg_ctl);
+            for (i = 0; i < (env->mcg_cap & 0xff); i++) {
+                qemu_get_be64s(f, &env->mce_banks[4*i]);
+                qemu_get_be64s(f, &env->mce_banks[4*i + 1]);
+                qemu_get_be64s(f, &env->mce_banks[4*i + 2]);
+                qemu_get_be64s(f, &env->mce_banks[4*i + 3]);
+            }
+        }
+    }
+
     /* XXX: ensure compatiblity for halted bit ? */
     /* XXX: compute redundant hflags bits */
     env->hflags = hflags;
qemu-monitor.hx +8 -0
@@ -611,6 +611,14 @@ policy back to @code{deny}.
 @end table
 ETEXI
 
+#if defined(TARGET_I386)
+    { "mce", "iillll", do_inject_mce, "cpu bank status mcgstatus addr misc", "inject a MCE on the given CPU"},
+#endif
+STEXI
+@item mce @var{cpu} @var{bank} @var{status} @var{mcgstatus} @var{addr} @var{misc}
+Inject an MCE on the given CPU (x86 only).
+ETEXI
+
 STEXI
 @end table
 ETEXI
#1 lore raw

Hi Huang,

Quoted 4 lines · Huang Ying

- MCE features are initialized when VCPU is intialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.
- A new interrupt mask: CPU_INTERRUPT_MCE is added to inject the MCE.

Can you post some instructions on how to test this functionality? For instance, I believe Linux is capable of delivering a SIGBUS to a process based on certain MCE's. I'm not sure what the steps would be though to create a process using a particular physical memory address, generate a MCE for that address, and then inspect that the process received the signal.

Are you testing the patches via something like that?

Regards,

Anthony Liguori

author
#2 lore raw

Hi, Anthony,

Quoted 9 lines · Anthony Liguori

Hi Huang,

Quoted 4 lines · Huang Ying

- MCE features are initialized when VCPU is intialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.
- A new interrupt mask: CPU_INTERRUPT_MCE is added to inject the MCE.

Can you post some instructions on how to test this functionality?

During development, I use QEMU monitor command "mce" to test this functionality. For corrected error, you can use /sbin/mcelog to get the error information. For uncorrected error, you will get panic. If you want to get result faster for corrected error, set the following sysfs file to a small number, which is seconds between corrected error polls.

/sys/devices/system/machinecheck/machinecheck0/check_interval

For corrected error:

mce 0 1 0x8000000000000000 0x0 0x0 0x0

For uncorrected error:

mce 0 1 0xb200000000000000 0x0 0x0 0x0

Quoted 7 lines

For
instance, I believe Linux is capable of delivering a SIGBUS to a process based on certain MCE's. I'm not sure what the steps would be though to create a process using a particular physical memory address, generate a MCE for that address, and then inspect that the process received the signal.

Are you testing the patches via something like that?

It is possible to test like that, but it is much easier to test the case I mentioned above.

Best Regards,
Huang Ying

author
#3 lore raw
In reply to Anthony Liguori

Hi, Anthony,

Quoted 8 lines · Anthony Liguori

Hi Huang,

Quoted 4 lines · Huang Ying

- MCE features are initialized when VCPU is intialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.
- A new interrupt mask: CPU_INTERRUPT_MCE is added to inject the MCE.

Quoted 7 lines

Can you post some instructions on how to test this functionality? For instance, I believe Linux is capable of delivering a SIGBUS to a process based on certain MCE's. I'm not sure what the steps would be though to create a process using a particular physical memory address, generate a MCE for that address, and then inspect that the process received the signal.

Are you testing the patches via something like that?

It seems that MCE patch has not been merged into qemu.git yet. Does it failed in some tests in stage?

Best Regards,
Huang Ying

author
#4 lore raw
In reply to Anthony Liguori
Quoted 9 lines · Anthony Liguori

Hi Huang,

Quoted 4 lines · Huang Ying

- MCE features are initialized when VCPU is intialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.
- A new interrupt mask: CPU_INTERRUPT_MCE is added to inject the MCE.

Can you post some instructions on how to test this functionality?

During development, I use QEMU monitor command "mce" to test this functionality. For corrected error, you can use /sbin/mcelog to get the error information. For uncorrected error, you will get panic. If you want to get result faster for corrected error, set the following sysfs file to a small number, which is seconds between corrected error polls.

/sys/devices/system/machinecheck/machinecheck0/check_interval

For corrected error:

mce 0 1 0x8000000000000000 0x0 0x0 0x0

For uncorrected error:

mce 0 1 0xb200000000000000 0x0 0x0 0x0

Quoted 7 lines

For
instance, I believe Linux is capable of delivering a SIGBUS to a process based on certain MCE's. I'm not sure what the steps would be though to create a process using a particular physical memory address, generate a MCE for that address, and then inspect that the process received the signal.

Are you testing the patches via something like that?

It is possible to test like that, but I think it is much easier to test the case I mentioned above.

Best Regards,
Huang Ying

#5 lore raw
In reply to Huang Ying
Quoted 3 lines · Huang Ying

- MCE features are initialized when VCPU is intialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.
- A new interrupt mask: CPU_INTERRUPT_MCE is added to inject the MCE.

This patch (now in the qemu tree) breaks compilation for me on i386:

/home/hch/work/qemu/target-i386/helper.c: In function 'cpu_inject_x86_mce':
/home/hch/work/qemu/target-i386/helper.c:1510: error: left shift count >= width of type /home/hch/work/qemu/target-i386/helper.c:1517: error: left shift count >= width of type /home/hch/work/qemu/target-i386/helper.c:1525: error: left shift count >= width of type /home/hch/work/qemu/target-i386/helper.c:1527: error: left shift count >= width of type /home/hch/work/qemu/target-i386/helper.c:1536: error: left shift count >= width of type /home/hch/work/qemu/target-i386/helper.c:1537: error: left shift count >= width of type /home/hch/work/qemu/target-i386/helper.c:1543: error: left shift count >= width of type /home/hch/work/qemu/target-i386/helper.c:1545: error: left shift count >= width of type /home/hch/work/qemu/target-i386/helper.c:1546: error: left shift count >= width of type /home/hch/work/qemu/target-i386/helper.c:1551: error: left shift count >= width of type

#6 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Huang Ying

- MCE features are initialized when VCPU is intialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.
- A new interrupt mask: CPU_INTERRUPT_MCE is added to inject the MCE.

Quoted 7 lines

This patch (now in the qemu tree) breaks compilation for me on i386:

/home/hch/work/qemu/target-i386/helper.c: In function 'cpu_inject_x86_mce':
/home/hch/work/qemu/target-i386/helper.c:1510: error: left shift count >= width of type

I pushed a fix to master.

Regards,

Anthony Liguori