Skip to contentSkip to conversation

xen/x86: Improvements to x86_capability[] handling

PATCH In review Andrew Cooper 2 participants 5 messages Oct 26, 2015

lore
Share & tools
lore
#0 lore raw

cover xen/x86: Improvements to x86_capability[] handling

More patches as a result of my cpuid levelling improvement work.

These changes are all purely mechanical and neither of these two patches have any functional change; the compiled binaries are identical other than their timestamps.

Andrew Cooper (2):
xen/x86: Helpers for cpu feature manipuation
xen/x86: Remove assumptions about the layout of x86_capabilities

 xen/arch/x86/boot/head.S         |  2 +-
 xen/arch/x86/boot/trampoline.S   |  2 +-
 xen/arch/x86/cpu/amd.c           |  2 +-
 xen/arch/x86/cpu/centaur.c       |  6 +--
 xen/arch/x86/cpu/common.c        | 22 +++++-----
 xen/arch/x86/efi/efi-boot.h      |  5 ++-
 xen/arch/x86/hvm/hvm.c           | 10 ++---
 xen/arch/x86/mpparse.c           |  6 ++-
 xen/arch/x86/traps.c             |  2 +-
 xen/arch/x86/xstate.c            |  4 +-
 xen/include/asm-x86/amd.h        | 89 +++++++++++++++++++---------------------
 xen/include/asm-x86/asm_defns.h  |  2 +-
 xen/include/asm-x86/cpufeature.h |  8 +++-
 13 files changed, 83 insertions(+), 77 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/2 xen/x86: Helpers for cpu feature manipuation

Expose them to assembly code, and replace open-coded versions.

No functional change.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/boot/head.S         |  2 +-
 xen/arch/x86/boot/trampoline.S   |  2 +-
 xen/arch/x86/cpu/common.c        |  4 +-
 xen/arch/x86/efi/efi-boot.h      |  2 +-
 xen/arch/x86/hvm/hvm.c           | 10 ++---
 xen/arch/x86/traps.c             |  2 +-
 xen/arch/x86/xstate.c            |  4 +-
 xen/include/asm-x86/amd.h        | 89 +++++++++++++++++++---------------------
 xen/include/asm-x86/asm_defns.h  |  2 +-
 xen/include/asm-x86/cpufeature.h |  7 +++-
 10 files changed, 63 insertions(+), 61 deletions(-)
xen/arch/x86/boot/head.S +1 -1
index f63b349..ac4962b 100644
@@ -142,7 +142,7 @@ __start:
         mov     %edx,sym_phys(boot_cpu_data)+CPUINFO_FEATURE_OFFSET(X86_FEATURE_LM)
 
         /* Check for availability of long mode. */
-        bt      $X86_FEATURE_LM & 0x1f,%edx
+        bt      $cpufeat_bit(X86_FEATURE_LM),%edx
         jnc     bad_cpu
 
         /* Stash TSC to calculate a good approximation of time-since-boot */
xen/arch/x86/boot/trampoline.S +1 -1
index 8b0d9c1..c8f32cd 100644
@@ -89,7 +89,7 @@ trampoline_protmode_entry:
         movl    $MSR_EFER,%ecx
         rdmsr
         or      $EFER_LME|EFER_SCE,%eax   /* Long Mode + SYSCALL/SYSRET */
-        bt      $X86_FEATURE_NX % 32,%edi /* No Execute? */
+        bt      $cpufeat_bit(X86_FEATURE_NX),%edi /* No Execute? */
         jnc     1f
         btsl    $_EFER_NX,%eax  /* No Execute     */
 1:      wrmsr
xen/arch/x86/cpu/common.c +2 -2
index f256444..a5caa84 100644
@@ -202,7 +202,7 @@ static void __init early_cpu_detect(void)
 	c->x86_mask = tfms & 15;
 	cap0 &= ~cleared_caps[0];
 	cap4 &= ~cleared_caps[4];
-	if (cap0 & (1<<19))
+	if (cap0 & cpufeat_mask(X86_FEATURE_CLFLSH))
 		c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
 	/* Leaf 0x1 capabilities filled in early for Xen. */
 	c->x86_capability[0] = cap0;
@@ -263,7 +263,7 @@ static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
 	if ( c->cpuid_level >= 0x00000007 ) {
 		u32 dummy;
 		cpuid_count(0x00000007, 0, &dummy, &ebx, &dummy, &dummy);
-		c->x86_capability[X86_FEATURE_FSGSBASE / 32] = ebx;
+		c->x86_capability[cpufeat_word(X86_FEATURE_FSGSBASE)] = ebx;
 	}
 }
 
xen/arch/x86/efi/efi-boot.h +1 -1
index 4c7f383..d8ca862 100644
@@ -225,7 +225,7 @@ static void __init noreturn efi_arch_post_exit_boot(void)
     asm volatile("pushq $0\n\tpopfq");
     rdmsrl(MSR_EFER, efer);
     efer |= EFER_SCE;
-    if ( cpuid_ext_features & (1 << (X86_FEATURE_NX & 0x1f)) )
+    if ( cpuid_ext_features & cpufeat_mask(X86_FEATURE_NX) )
         efer |= EFER_NX;
     wrmsrl(MSR_EFER, efer);
     write_cr0(X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP |
xen/arch/x86/hvm/hvm.c +5 -5
index 3fa2280..965bfbf 100644
@@ -1850,8 +1850,8 @@ static const char * hvm_efer_valid(const struct vcpu *v, uint64_t value,
     }
     else
     {
-        ext1_edx = boot_cpu_data.x86_capability[X86_FEATURE_LM / 32];
-        ext1_ecx = boot_cpu_data.x86_capability[X86_FEATURE_SVM / 32];
+        ext1_edx = boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_LM)];
+        ext1_ecx = boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_SVM)];
     }
 
     /*
@@ -1917,9 +1917,9 @@ static unsigned long hvm_cr4_guest_reserved_bits(const struct vcpu *v,
     }
     else
     {
-        leaf1_edx = boot_cpu_data.x86_capability[X86_FEATURE_VME / 32];
-        leaf1_ecx = boot_cpu_data.x86_capability[X86_FEATURE_PCID / 32];
-        leaf7_0_ebx = boot_cpu_data.x86_capability[X86_FEATURE_FSGSBASE / 32];
+        leaf1_edx = boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_VME)];
+        leaf1_ecx = boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_PCID)];
+        leaf7_0_ebx = boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_FSGSBASE)];
     }
 
     return ~(unsigned long)
xen/arch/x86/traps.c +1 -1
index 8093535..b32f696 100644
@@ -935,7 +935,7 @@ void pv_cpuid(struct cpu_user_regs *regs)
             goto unsupported;
         if ( regs->_ecx == 1 )
         {
-            a &= boot_cpu_data.x86_capability[X86_FEATURE_XSAVEOPT / 32];
+            a &= boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_XSAVEOPT)];
             if ( !cpu_has_xsaves )
                 b = c = d = 0;
         }
xen/arch/x86/xstate.c +2 -2
index 9ddff90..c6238d9 100644
@@ -326,9 +326,9 @@ void xstate_init(struct cpuinfo_x86 *c)
     eax &= (cpufeat_mask(X86_FEATURE_XSAVEOPT) |
             cpufeat_mask(X86_FEATURE_XSAVEC));
 
-    c->x86_capability[X86_FEATURE_XSAVEOPT / 32] = eax;
+    c->x86_capability[cpufeat_word(X86_FEATURE_XSAVEOPT)] = eax;
 
-    BUG_ON(eax != boot_cpu_data.x86_capability[X86_FEATURE_XSAVEOPT / 32]);
+    BUG_ON(eax != boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_XSAVEOPT)]);
 }
 
 static bool_t valid_xcr0(u64 xcr0)
xen/include/asm-x86/amd.h +43 -46

138 lines not on this page · raw message

xen/include/asm-x86/asm_defns.h +1 -1
index 7c8c2c0..d750f03 100644
@@ -137,7 +137,7 @@ void ret_from_intr(void);
 #endif
 
 #define CPUINFO_FEATURE_OFFSET(feature)           \
-        ((((feature) >> 3) & ~3) + CPUINFO_features)
+    (CPUINFO_features + (cpufeat_word(feature) * 4))
 
 #else
 
xen/include/asm-x86/cpufeature.h +6 -1
index e94b04d..e823954 100644
@@ -9,6 +9,8 @@
 #define __ASM_I386_CPUFEATURE_H
 #endif
 
+#include <xen/const.h>
+
 #define NCAPINTS	8	/* N 32-bit words worth of info */
 
 /* Intel-defined CPU features, CPUID level 0x00000001 (edx), word 0 */
@@ -160,12 +162,15 @@
 #define X86_FEATURE_ADX		(7*32+19) /* ADCX, ADOX instructions */
 #define X86_FEATURE_SMAP	(7*32+20) /* Supervisor Mode Access Prevention */
 
+#define cpufeat_word(idx)	((idx) / 32)
+#define cpufeat_bit(idx)	((idx) % 32)
+#define cpufeat_mask(idx)	(_AC(1, U) << cpufeat_bit(idx))
+
 #if !defined(__ASSEMBLY__) && !defined(X86_FEATURES_ONLY)
 #include <xen/bitops.h>
 
 #define cpu_has(c, bit)		test_bit(bit, (c)->x86_capability)
 #define boot_cpu_has(bit)	test_bit(bit, boot_cpu_data.x86_capability)
-#define cpufeat_mask(idx)       (1u << ((idx) & 31))
 
 #define CPUID_MWAIT_LEAF                5
 #define CPUID5_ECX_EXTENSIONS_SUPPORTED 0x1
Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH 1/2] xen/x86: Helpers for cpu feature manipuation

xen/arch/x86/cpu/common.c static void __init early_cpu_detect(void) · line 205

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 26.10.15 at 12:17, <[email protected]> wrote:
xen/arch/x86/cpu/common.c +1 -1
@@ -202,7 +202,7 @@ static void __init early_cpu_detect(void)
 	c->x86_mask = tfms & 15;
 	cap0 &= ~cleared_caps[0];
 	cap4 &= ~cleared_caps[4];
-	if (cap0 & (1<<19))
+	if (cap0 & cpufeat_mask(X86_FEATURE_CLFLSH))

This one's particularly well spotted! Thanks!

Jan

#3 lore raw

Re: [PATCH 1/2] xen/x86: Helpers for cpu feature manipuation

Quoted 10 lines · Jan Beulich

xen/arch/x86/cpu/common.c static void __init early_cpu_detect(void) · line 205

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 26.10.15 at 12:17, <[email protected]> wrote:
xen/arch/x86/cpu/common.c +1 -1
@@ -202,7 +202,7 @@ static void __init early_cpu_detect(void)
 	c->x86_mask = tfms & 15;
 	cap0 &= ~cleared_caps[0];
 	cap4 &= ~cleared_caps[4];
-	if (cap0 & (1<<19))
+	if (cap0 & cpufeat_mask(X86_FEATURE_CLFLSH))

This one's particularly well spotted! Thanks!

I doubt I have found all instances like this. I only found this one because of the associated changes in the subsequent patch.

I will be fixing them as I find them.

~Andrew

#4 lore raw

PATCH 2/2 xen/x86: Remove assumptions about the layout of x86_capabilities

In reply to Andrew Cooper

Future work will rearange it, invalidating these assumptions.

No functional change.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/cpu/amd.c           |  2 +-
 xen/arch/x86/cpu/centaur.c       |  6 ++----
 xen/arch/x86/cpu/common.c        | 18 ++++++++++--------
 xen/arch/x86/efi/efi-boot.h      |  3 ++-
 xen/arch/x86/mpparse.c           |  6 ++++--
 xen/include/asm-x86/cpufeature.h |  1 +
 6 files changed, 20 insertions(+), 16 deletions(-)
xen/arch/x86/cpu/amd.c +1 -1
index fd57370..74a0152 100644
@@ -444,7 +444,7 @@ static void __devinit init_amd(struct cpuinfo_x86 *c)
 
 	/* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
 	   3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway */
-	__clear_bit(0*32+31, c->x86_capability);
+	__clear_bit(X86_FEATURE_3DNOW_ALT, c->x86_capability);
 	
 	if (c->x86 == 0xf && c->x86_model < 0x14
 	    && cpu_has(c, X86_FEATURE_LAHF_LM)) {
xen/arch/x86/cpu/centaur.c +2 -4
index 66b1995..c0ac117 100644
@@ -39,10 +39,8 @@ static void init_c3(struct cpuinfo_x86 *c)
 			printk(KERN_INFO "CPU: Enabled h/w RNG\n");
 		}
 
-		/* store Centaur Extended Feature Flags as
-		 * word 5 of the CPU capability bit array
-		 */
-		c->x86_capability[5] = cpuid_edx(0xC0000001);
+		c->x86_capability[cpufeat_word(X86_FEATURE_XSTORE)]
+                    = cpuid_edx(0xC0000001);
 	}
 
 	if (c->x86 == 0x6 && c->x86_model >= 0xf) {
xen/arch/x86/cpu/common.c +10 -8
index a5caa84..653b052 100644
@@ -200,13 +200,13 @@ static void __init early_cpu_detect(void)
 	if (c->x86 >= 0x6)
 		c->x86_model += ((tfms >> 16) & 0xF) << 4;
 	c->x86_mask = tfms & 15;
-	cap0 &= ~cleared_caps[0];
-	cap4 &= ~cleared_caps[4];
+	cap0 &= ~cleared_caps[cpufeat_word(X86_FEATURE_FPU)];
+	cap4 &= ~cleared_caps[cpufeat_word(X86_FEATURE_XMM3)];
 	if (cap0 & cpufeat_mask(X86_FEATURE_CLFLSH))
 		c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
 	/* Leaf 0x1 capabilities filled in early for Xen. */
-	c->x86_capability[0] = cap0;
-	c->x86_capability[4] = cap4;
+	c->x86_capability[cpufeat_word(X86_FEATURE_FPU)] = cap0;
+	c->x86_capability[cpufeat_word(X86_FEATURE_XMM3)] = cap4;
 }
 
 static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
@@ -225,8 +225,8 @@ static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
 	
 	/* Intel-defined flags: level 0x00000001 */
 	cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
-	c->x86_capability[0] = capability;
-	c->x86_capability[4] = excap;
+	c->x86_capability[cpufeat_word(X86_FEATURE_FPU)] = capability;
+	c->x86_capability[cpufeat_word(X86_FEATURE_XMM3)] = excap;
 	c->x86 = (tfms >> 8) & 15;
 	c->x86_model = (tfms >> 4) & 15;
 	if (c->x86 == 0xf)
@@ -247,8 +247,10 @@ static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
 	c->extended_cpuid_level = cpuid_eax(0x80000000);
 	if ( (c->extended_cpuid_level & 0xffff0000) == 0x80000000 ) {
 		if ( c->extended_cpuid_level >= 0x80000001 ) {
-			c->x86_capability[1] = cpuid_edx(0x80000001);
-			c->x86_capability[6] = cpuid_ecx(0x80000001);
+			c->x86_capability[cpufeat_word(X86_FEATURE_SYSCALL)]
+				= cpuid_edx(0x80000001);
+			c->x86_capability[cpufeat_word(X86_FEATURE_LAHF_LM)]
+				= cpuid_ecx(0x80000001);
 		}
 		if ( c->extended_cpuid_level >= 0x80000004 )
 			get_model_name(c); /* Default name */
xen/arch/x86/efi/efi-boot.h +2 -1
index d8ca862..6dbb14d 100644
@@ -608,7 +608,8 @@ static void __init efi_arch_cpu(void)
     if ( cpuid_eax(0x80000000) > 0x80000000 )
     {
         cpuid_ext_features = cpuid_edx(0x80000001);
-        boot_cpu_data.x86_capability[1] = cpuid_ext_features;
+        boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_SYSCALL)]
+            = cpuid_ext_features;
     }
 }
 
xen/arch/x86/mpparse.c +4 -2
index 8609f4a..dc74e37 100644
@@ -495,7 +495,8 @@ static inline void __init construct_default_ISA_mptable(int mpc_default_type)
 	processor.mpc_cpufeature = (boot_cpu_data.x86 << 8) |
 				   (boot_cpu_data.x86_model << 4) |
 				   boot_cpu_data.x86_mask;
-	processor.mpc_featureflag = boot_cpu_data.x86_capability[0];
+	processor.mpc_featureflag =
+            boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_FPU)];
 	processor.mpc_reserved[0] = 0;
 	processor.mpc_reserved[1] = 0;
 	for (i = 0; i < 2; i++) {
@@ -799,7 +800,8 @@ int __devinit mp_register_lapic (
 	processor.mpc_cpuflag |= (boot_cpu ? CPU_BOOTPROCESSOR : 0);
 	processor.mpc_cpufeature = (boot_cpu_data.x86 << 8) | 
 		(boot_cpu_data.x86_model << 4) | boot_cpu_data.x86_mask;
-	processor.mpc_featureflag = boot_cpu_data.x86_capability[0];
+	processor.mpc_featureflag
+            = boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_FPU)];
 	processor.mpc_reserved[0] = 0;
 	processor.mpc_reserved[1] = 0;
 
xen/include/asm-x86/cpufeature.h +1 -0
index e823954..af127cf 100644
@@ -45,6 +45,7 @@
 #define X86_FEATURE_ACC		(0*32+29) /* Automatic clock control */
 #define X86_FEATURE_IA64	(0*32+30) /* IA-64 processor */
 #define X86_FEATURE_PBE		(0*32+31) /* Pending Break Enable */
+#define X86_FEATURE_3DNOW_ALT	(0*32+31) /* AMD nonstandard 3DNow (Aliases PBE) */
 
 /* AMD-defined CPU features, CPUID level 0x80000001, word 1 */
 /* Don't duplicate feature flags which are redundant with Intel! */
Signature · 1 line
2.1.4