Skip to contentSkip to conversation

Fix AMD threshold register definitions and activate vmce_amd_* functions

PATCH In review Aravind Gopalakrishnan 2 participants 7 messages Feb 5, 2014 – Feb 7, 2014

lore
Share & tools
lore
#0 lore raw

v3 cover Fix AMD threshold register definitions and activate vmce_amd_* functions

Patch 1: Deals with correcting AMD threshold register definitions.
Patch 2: Fixes mask in vmce.c to allow vmce_amd_* functions to handle access to
AMD thresholding registers.
Patch 3: Verify presence of AMD extended block of MISC registers before we decide to emulate(or not)

Changes in V2:
- Correct time skew on the V1 patch.
Changes in V3:
- Use #defines for the masks (per C.Egger and Boris.O comments)
- Reword commit message
- Rework code to care for the differences in BKDG wrt to presence of extended MC4 MISC registers (per Jan comments)

Aravind Gopalakrishnan (3):
hvm,svm: Update AMD Thresholding MSR definitions mcheck,vmce: Allow vmce_amd_* functions to handle AMD thresolding
MSRs
mcheck, mce_amd: Verify presence of extended AMD_MC4_MISC registers

 xen/arch/x86/cpu/mcheck/amd_f10.c |   64 +++++++++++++++++--------------------
 xen/arch/x86/cpu/mcheck/mce_amd.h |    3 ++
 xen/arch/x86/cpu/mcheck/vmce.c    |    6 ++--
 xen/arch/x86/cpu/mcheck/vmce.h    |    3 ++
 xen/arch/x86/hvm/svm/svm.c        |   10 +++---
 xen/include/asm-x86/msr-index.h   |    6 ++--
 6 files changed, 48 insertions(+), 44 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v3 1/3 hvm, svm: Update AMD Thresholding MSR definitions

Defining MSR 0xC000040A is needless as it is marked reserved from Fam15 onwards and zeroed in Fam10

Also, according to BKDG, MSR 0x413 is the first of the thresholding registers; MSR 0xC0000408 and MSR 0xC0000409 are second and third respectively. So rework the #define's accordingly and use MSR_IA32_MCx_MISC() to define MSR 0x413

Fam10 BKDG reference:
http://support.amd.com/TechDocs/31116.pdf

Fam15 Model 00h-0fh BKDG reference:
http://support.amd.com/TechDocs/42301_15h_Mod_00h-0Fh_BKDG.pdf

---
 xen/arch/x86/hvm/svm/svm.c      |   10 ++++++----
 xen/include/asm-x86/msr-index.h |    6 +++---
 2 files changed, 9 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/svm/svm.c +6 -4
index 406d394..155c66e 100644
@@ -1461,8 +1461,9 @@ static int svm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
         *msr_content = v->arch.hvm_svm.guest_sysenter_eip;
         break;
 
-    case MSR_IA32_MCx_MISC(4): /* Threshold register */
-    case MSR_F10_MC4_MISC1 ... MSR_F10_MC4_MISC3:
+    case MSR_F10_MC4_MISC1: /* Threshold register */
+    case MSR_F10_MC4_MISC2:
+    case MSR_F10_MC4_MISC3:
         /*
          * MCA/MCE: We report that the threshold register is unavailable
          * for OS use (locked by the BIOS).
@@ -1660,8 +1661,9 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
         vpmu_do_wrmsr(msr, msr_content);
         break;
 
-    case MSR_IA32_MCx_MISC(4): /* Threshold register */
-    case MSR_F10_MC4_MISC1 ... MSR_F10_MC4_MISC3:
+    case MSR_F10_MC4_MISC1: /* Threshold register */
+    case MSR_F10_MC4_MISC2:
+    case MSR_F10_MC4_MISC3:
         /*
          * MCA/MCE: Threshold register is reported to be locked, so we ignore
          * all write accesses. This behaviour matches real HW, so guests should
xen/include/asm-x86/msr-index.h +3 -3
index fc9fbc6..e5ffbf2 100644
@@ -219,9 +219,9 @@
 #define AMD64_NB_CFG_CF8_EXT_ENABLE_BIT	46
 
 /* AMD Family10h machine check MSRs */
-#define MSR_F10_MC4_MISC1		0xc0000408
-#define MSR_F10_MC4_MISC2		0xc0000409
-#define MSR_F10_MC4_MISC3		0xc000040A
+#define MSR_F10_MC4_MISC1		MSR_IA32_MCx_MISC(4)
+#define MSR_F10_MC4_MISC2		0xc0000408
+#define MSR_F10_MC4_MISC3		0xc0000409
 
 /* AMD Family10h Bus Unit MSRs */
 #define MSR_F10_BU_CFG 		0xc0011023
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [PATCH 1/3 V3] hvm, svm: Update AMD Thresholding MSR definitions

Quoted 17 lines
Quoted 1 line
Quoted 1 line · Aravind Gopalakrishnan
xen/arch/x86/hvm/svm/svm.c +3 -2
@@ -1461,8 +1461,9 @@ static int svm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
         *msr_content = v->arch.hvm_svm.guest_sysenter_eip;
         break;
 
-    case MSR_IA32_MCx_MISC(4): /* Threshold register */
-    case MSR_F10_MC4_MISC1 ... MSR_F10_MC4_MISC3:
+    case MSR_F10_MC4_MISC1: /* Threshold register */
+    case MSR_F10_MC4_MISC2:
+    case MSR_F10_MC4_MISC3:
         /*
          * MCA/MCE: We report that the threshold register is unavailable
          * for OS use (locked by the BIOS).
@@ -1660,8 +1661,9 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
         vpmu_do_wrmsr(msr, msr_content);

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 1669

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +3 -2
         break;
 
-    case MSR_IA32_MCx_MISC(4): /* Threshold register */
-    case MSR_F10_MC4_MISC1 ... MSR_F10_MC4_MISC3:
+    case MSR_F10_MC4_MISC1: /* Threshold register */
+    case MSR_F10_MC4_MISC2:
+    case MSR_F10_MC4_MISC3:
         /*
          * MCA/MCE: Threshold register is reported to be locked, so we ignore
          * all write accesses. This behaviour matches real HW, so guests should

While both of these changes are fine with the code as it currently is, they both rely on behavior that is sub-optimal and would hence need to be changed: Neither should MSR reads blindly read the hardware MSR as a fallback, nor should MSR write be blindly ignored. Yet with removing 0xC000040A from the explicit list of registers handled, a guest OS accessing this MSR (knowing it exists on Fam10) would start to see unexpected #GP faults as soon as the respective default cases would get corrected. And I assume you agree that correcting these default cases should not require the author to be particularly knowledgeable about AMD CPU family differences.

Jan

#3 lore raw

Re: [PATCH 1/3 V3] hvm, svm: Update AMD Thresholding MSR definitions

Quoted 31 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Aravind Gopalakrishnan
xen/arch/x86/hvm/svm/svm.c +3 -2
@@ -1461,8 +1461,9 @@ static int svm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
         *msr_content = v->arch.hvm_svm.guest_sysenter_eip;
         break;
 
-    case MSR_IA32_MCx_MISC(4): /* Threshold register */
-    case MSR_F10_MC4_MISC1 ... MSR_F10_MC4_MISC3:
+    case MSR_F10_MC4_MISC1: /* Threshold register */
+    case MSR_F10_MC4_MISC2:
+    case MSR_F10_MC4_MISC3:
         /*
          * MCA/MCE: We report that the threshold register is unavailable
          * for OS use (locked by the BIOS).
@@ -1660,8 +1661,9 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
         vpmu_do_wrmsr(msr, msr_content);

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 1669

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +3 -2
         break;
 
-    case MSR_IA32_MCx_MISC(4): /* Threshold register */
-    case MSR_F10_MC4_MISC1 ... MSR_F10_MC4_MISC3:
+    case MSR_F10_MC4_MISC1: /* Threshold register */
+    case MSR_F10_MC4_MISC2:
+    case MSR_F10_MC4_MISC3:
         /*
          * MCA/MCE: Threshold register is reported to be locked, so we ignore
          * all write accesses. This behaviour matches real HW, so guests should

While both of these changes are fine with the code as it currently is, they both rely on behavior that is sub-optimal and would hence need to be changed: Neither should MSR reads blindly read the

Quoted 9 lines

hardware MSR as a fallback, nor should MSR write be blindly ignored. Yet with removing 0xC000040A from the explicit list of registers handled, a guest OS accessing this MSR (knowing it exists on Fam10) would start to see unexpected #GP faults as soon as the respective default cases would get corrected. And I assume you agree that correcting these default cases should not require the author to be particularly knowledgeable about AMD CPU family differences.

So, just to be safe, it is better I don't touch this piece of code here.. (you can ignore patch 1 of the series basically)

I'll condense the remaining changes into a single patch and send it out separately.

Thanks
-Aravind.

#4 lore raw

PATCH v3 2/3 mcheck, vmce: Allow vmce_amd_* functions to handle AMD thresolding MSRs

In reply to Aravind Gopalakrishnan

vmce_amd_[rd|wr]msr functions can handle accesses to AMD thresholding registers. But due to this statement here:
switch ( msr & (MSR_IA32_MC0_CTL | 3) )
we are wrongly masking off top two bits and bit 4 which meant the register accesses never made it to vmce_amd_* functions.

We correct this problem by modifying the mask in this patch to allow AMD thresholding registers to fall to 'default' case which in turn allows vmce_amd_* functions to handle access to the registers.

---
 xen/arch/x86/cpu/mcheck/vmce.c |    6 ++++--
 xen/arch/x86/cpu/mcheck/vmce.h |    3 +++
 2 files changed, 7 insertions(+), 2 deletions(-)
xen/arch/x86/cpu/mcheck/vmce.c +4 -2
index f6c35db..841bd46 100644
@@ -107,7 +107,8 @@ static int bank_mce_rdmsr(const struct vcpu *v, uint32_t msr, uint64_t *val)
 
     *val = 0;
 
-    switch ( msr & (MSR_IA32_MC0_CTL | 3) )
+    switch ( msr & (MSR_IA32_MC0_CTL | AMD_MC4_MISC1_INCLUDE_MASK |
+                    AMD_MC4_EXTENDED_MISC_INCLUDE_MASK) )
     {
     case MSR_IA32_MC0_CTL:
         /* stick all 1's to MCi_CTL */
@@ -210,7 +211,8 @@ static int bank_mce_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
     int ret = 1;
     unsigned int bank = (msr - MSR_IA32_MC0_CTL) / 4;
 
-    switch ( msr & (MSR_IA32_MC0_CTL | 3) )
+    switch ( msr & (MSR_IA32_MC0_CTL | AMD_MC4_MISC1_INCLUDE_MASK |
+                    AMD_MC4_EXTENDED_MISC_INCLUDE_MASK) )
     {
     case MSR_IA32_MC0_CTL:
         /*
xen/arch/x86/cpu/mcheck/vmce.h +3 -0
index 6b2c95a..5e9b091 100644
@@ -8,6 +8,9 @@ int vmce_init(struct cpuinfo_x86 *c);
 #define dom0_vmce_enabled() (dom0 && dom0->max_vcpus && dom0->vcpu[0] \
         && guest_enabled_event(dom0->vcpu[0], VIRQ_MCA))
 
+#define AMD_MC4_MISC1_INCLUDE_MASK          0x13
+#define AMD_MC4_EXTENDED_MISC_INCLUDE_MASK  0xc0000000
+
 int unmmap_broken_page(struct domain *d, mfn_t mfn, unsigned long gfn);
 
 int vmce_intel_rdmsr(const struct vcpu *, uint32_t msr, uint64_t *val);
Signature · 1 line
1.7.9.5
#5 lore raw

Re: [PATCH 2/3 V3] mcheck, vmce: Allow vmce_amd_* functions to handle AMD thresolding MSRs

xen/arch/x86/cpu/mcheck/vmce.h int vmce_init(struct cpuinfo_x86 *c); · line 11

Quoted 8 lines
Quoted 1 line
Quoted 1 line · Aravind Gopalakrishnan
xen/arch/x86/cpu/mcheck/vmce.h +1 -0
@@ -8,6 +8,9 @@ int vmce_init(struct cpuinfo_x86 *c);
 #define dom0_vmce_enabled() (dom0 && dom0->max_vcpus && dom0->vcpu[0] \
         && guest_enabled_event(dom0->vcpu[0], VIRQ_MCA))
 
+#define AMD_MC4_MISC1_INCLUDE_MASK          0x13

As said before, this ought to remain 3, and has nothing to do with AMD. No need to have a variable for this at all.

xen/arch/x86/cpu/mcheck/vmce.h int vmce_init(struct cpuinfo_x86 *c); · line 12

Quoted 1 line
xen/arch/x86/cpu/mcheck/vmce.h +1 -0
+#define AMD_MC4_EXTENDED_MISC_INCLUDE_MASK  0xc0000000

For this one I meanwhile think that the better mask would be -MSR_IA32_MC0_CTL (i.e. 0xfffffc00). Which then would also eliminate the need for a separate manifest constant.

Jan

#6 lore raw

PATCH v3 3/3 mcheck, mce_amd: Verify presence of extended AMD_MC4_MISC registers

In reply to Aravind Gopalakrishnan

MSR 0x413 is present in all families from F10 onwards. But the extended block of MC4 MISC registers do not exist always. In this patch, we rework the vmce_amd_[wr|rd]msr functions to return #GP to guest if register does not exist in HW.

If they do, then we continue current policy of blocking access as this bank is not emulated. So for reads - return value = 0 and for writes, do nothing.

---
 xen/arch/x86/cpu/mcheck/amd_f10.c |   64 +++++++++++++++++--------------------
 xen/arch/x86/cpu/mcheck/mce_amd.h |    3 ++
 2 files changed, 32 insertions(+), 35 deletions(-)
xen/arch/x86/cpu/mcheck/amd_f10.c +29 -35
index 61319dc..b1ccda4 100644
@@ -102,46 +102,40 @@ enum mcheck_type amd_f10_mcheck_init(struct cpuinfo_x86 *c)
 	return mcheck_amd_famXX;
 }
 
+/* check for AMD MC4 extended MISC register presence */
+static inline int amd_thresholding_reg_present(uint32_t msr)
+{
+    uint64_t val;
+    rdmsr_safe(msr, val);
+    if ( val & (AMD_MC4_MISC_VAL_MASK | AMD_MC4_MISC_CNTP_MASK) )
+        return 1;
+
+    return 0;
+}
+
 /* amd specific MCA MSR */
 int vmce_amd_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
 {
-	switch (msr) {
-	case MSR_F10_MC4_MISC1: /* DRAM error type */
-		v->arch.vmce.bank[1].mci_misc = val; 
-		mce_printk(MCE_VERBOSE, "MCE: wr msr %#"PRIx64"\n", val);
-		break;
-	case MSR_F10_MC4_MISC2: /* Link error type */
-	case MSR_F10_MC4_MISC3: /* L3 cache error type */
-		/* ignore write: we do not emulate link and l3 cache errors
-		 * to the guest.
-		 */
-		mce_printk(MCE_VERBOSE, "MCE: wr msr %#"PRIx64"\n", val);
-		break;
-	default:
-		return 0;
-	}
-
-	return 1;
+    if ( msr != MSR_F10_MC4_MISC1 )
+    {
+        /* If not present, #GP fault, else do nothing as we don't emulate */
+        if ( !amd_thresholding_reg_present(msr) )
+            return -1;
+    }
+
+    mce_printk(MCE_VERBOSE, "MCE: wr msr %#"PRIx64"\n", val);
+    return 1;
 }
 
 int vmce_amd_rdmsr(const struct vcpu *v, uint32_t msr, uint64_t *val)
 {
-	switch (msr) {
-	case MSR_F10_MC4_MISC1: /* DRAM error type */
-		*val = v->arch.vmce.bank[1].mci_misc;
-		mce_printk(MCE_VERBOSE, "MCE: rd msr %#"PRIx64"\n", *val);
-		break;
-	case MSR_F10_MC4_MISC2: /* Link error type */
-	case MSR_F10_MC4_MISC3: /* L3 cache error type */
-		/* we do not emulate link and l3 cache
-		 * errors to the guest.
-		 */
-		*val = 0;
-		mce_printk(MCE_VERBOSE, "MCE: rd msr %#"PRIx64"\n", *val);
-		break;
-	default:
-		return 0;
-	}
-
-	return 1;
+    if ( msr != MSR_F10_MC4_MISC1 )
+    {
+        /* If not present, #GP fault, else assign '0' as we don't emulate */
+        if ( !amd_thresholding_reg_present(msr) )
+            return -1;
+    }
+
+    *val = 0;
+    return 1;
 }
xen/arch/x86/cpu/mcheck/mce_amd.h +3 -0
index 5d047e7..a6024fb 100644
@@ -1,6 +1,9 @@
 #ifndef _MCHECK_AMD_H
 #define _MCHECK_AMD_H
 
+#define AMD_MC4_MISC_VAL_MASK           (1ULL << 63)
+#define AMD_MC4_MISC_CNTP_MASK          (1ULL << 62)
+
 enum mcheck_type amd_k8_mcheck_init(struct cpuinfo_x86 *c);
 enum mcheck_type amd_f10_mcheck_init(struct cpuinfo_x86 *c);
 
Signature · 1 line
1.7.9.5