Skip to contentSkip to conversation

x86, cpu: Expand ->x86_capability flags with bugs bitvector

RFC PATCH In review Borislav Petkov 2 participants 6 messages Feb 11, 2013 – Feb 13, 2013

Subsystems (3)
lore
Share & tools
lore

Borislav Petkov

author maintainer
#0 lore raw

cover x86, cpu: Expand ->x86_capability flags with bugs bitvector

From: Borislav Petkov <[email protected]>

Hi,

so this is a rough first version to collect some initial comments. It is lightly tested in kvm.

Thanks.

Borislav Petkov (4):
x86, cpu: Expand cpufeature facility to include cpu bugs x86, cpu: Convert F00F bug detection
x86, cpu: Convert FDIV bug detection
x86, cpu: Convert Cyrix coma bug detection

 arch/x86/include/asm/cpufeature.h | 38 ++++++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/processor.h  |  6 +-----
 arch/x86/kernel/alternative.c     |  2 +-
 arch/x86/kernel/cpu/bugs.c        |  5 +++--
 arch/x86/kernel/cpu/common.c      |  4 ++++
 arch/x86/kernel/cpu/cyrix.c       |  5 +++--
 arch/x86/kernel/cpu/intel.c       |  4 ++--
 arch/x86/kernel/cpu/proc.c        |  6 +++---
 arch/x86/mm/fault.c               |  2 +-
 9 files changed, 56 insertions(+), 16 deletions(-)
Signature · 1 line
1.8.1.3.535.ga923c31
author maintainer
#1 lore raw

RFC PATCH 1/4 x86, cpu: Expand cpufeature facility to include cpu bugs

From: Borislav Petkov <[email protected]>

We add another 32-bit vector at the end of the ->x86_capability bitvector which collects bugs present in CPUs. After all, a CPU bug is a kind of a capability, albeit a strange one.

---
 arch/x86/include/asm/cpufeature.h | 34 ++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/processor.h  |  2 +-
 arch/x86/kernel/alternative.c     |  2 +-
 arch/x86/kernel/cpu/common.c      |  4 ++++
 4 files changed, 40 insertions(+), 2 deletions(-)
arch/x86/include/asm/cpufeature.h +34 -0
index 2d9075e863a0..68daf877dad5 100644
@@ -9,6 +9,7 @@
 #endif
 
 #define NCAPINTS	10	/* N 32-bit words worth of info */
+#define NBUGINTS	1	/* N 32-bit bug flags */
 
 /*
  * Note: If the comment begins with a quoted string, that string is used
@@ -399,6 +400,39 @@ static __always_inline __pure bool __static_cpu_has(u16 bit)
 #define static_cpu_has(bit) boot_cpu_has(bit)
 #endif
 
+#define __BUG_CHECK_BIT(bit)					\
+({								\
+	WARN_ON(bit >> 5 < NCAPINTS);				\
+	bit;							\
+})
+
+#define cpu_has_bug(c, bit)					\
+({								\
+	unsigned __bit = __BUG_CHECK_BIT((bit));		\
+	cpu_has(c, __bit);					\
+})
+
+#define boot_cpu_has_bug(bit) \
+	cpu_has_bug(&boot_cpu_data, (bit))
+
+#define static_cpu_has_bug(bit)					\
+({								\
+	unsigned __bit = __BUG_CHECK_BIT((bit));		\
+	static_cpu_has(__bit);					\
+})
+
+#define set_cpu_bug(c, bit)					\
+({								\
+	unsigned __bit = __BUG_CHECK_BIT((bit));		\
+	set_cpu_cap(c, __bit);					\
+})
+
+#define clear_cpu_bug(c, bit)					\
+({								\
+	unsigned __bit = __BUG_CHECK_BIT((bit));		\
+	clear_cpu_cap(c, __bit);				\
+})
+
 #endif /* defined(__KERNEL__) && !defined(__ASSEMBLY__) */
 
 #endif /* _ASM_X86_CPUFEATURE_H */
arch/x86/include/asm/processor.h +1 -1
index 439f27a41ee8..67721c634bf0 100644
@@ -108,7 +108,7 @@ struct cpuinfo_x86 {
 	__u32			extended_cpuid_level;
 	/* Maximum supported CPUID level, -1=no CPUID: */
 	int			cpuid_level;
-	__u32			x86_capability[NCAPINTS];
+	__u32			x86_capability[NCAPINTS + NBUGINTS];
 	char			x86_vendor_id[16];
 	char			x86_model_id[64];
 	/* in KB - valid for CPUS which support this call: */
arch/x86/kernel/alternative.c +1 -1
index ef5ccca79a6c..c15cf9a25e27 100644
@@ -271,7 +271,7 @@ void __init_or_module apply_alternatives(struct alt_instr *start,
 		replacement = (u8 *)&a->repl_offset + a->repl_offset;
 		BUG_ON(a->replacementlen > a->instrlen);
 		BUG_ON(a->instrlen > sizeof(insnbuf));
-		BUG_ON(a->cpuid >= NCAPINTS*32);
+		BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32);
 		if (!boot_cpu_has(a->cpuid))
 			continue;
 
arch/x86/kernel/cpu/common.c +4 -0
index d814772c5bed..22018f70a671 100644
@@ -920,6 +920,10 @@ static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
 		/* AND the already accumulated flags with these */
 		for (i = 0; i < NCAPINTS; i++)
 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
+
+		/* OR, i.e. replicate the bug flags */
+		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
+			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
 	}
 
 	/* Init Machine Check Exception if available. */
Signature · 1 line
1.8.1.3.535.ga923c31
#2 lore raw

Re: [RFC PATCH 1/4] x86, cpu: Expand cpufeature facility to include cpu bugs

arch/x86/include/asm/cpufeature.h static __always_inline __pure bool __static_cpu_has(u16 bit) · line 408

Quoted 7 lines · Borislav Petkov
arch/x86/include/asm/cpufeature.h +6 -0
+#define __BUG_CHECK_BIT(bit)					\
+({								\
+	WARN_ON(bit >> 5 < NCAPINTS);				\
+	bit;							\
+})
+

Do we need this? Either way, if we do, I would suggest doing something like:

	if (__builtin_constant_p(bit))
		bad_bug_number();

... and flag bad_bug_number as a compile time error, since the vast majority of the time the bit number will be constant.

However, I don't think it is necessary. In order for this to ever trigger someone must have known they were testing for a bug, and yet not used the X86_BUG_ macros, which seems very unlikely.

Quoted 3 lines

#define X86_BUG_F00F (NCAPINTS*32+ 0) /* Intel F00F bug */
#define X86_BUG_FDIV (NCAPINTS*32+ 1) /* FPU FDIV bug */
+#define X86_BUG_COMA (NCAPINTS*32+ 2) /* Cyrix 6x86 coma */

Just to make it a bit cleaner once we have more than one word of bug tests, I would suggest macroizing this:

#define X86_BUG(x) (NCAPINTS*32 + (x))

... and then just ...

#define X86_BUG_F00F X86_BUG(0)
#define X86_BUG_FDIV X86_BUG(1)

... and so on.

The only reason we *don't* do that with the features is that they tend to come chunkwise in the form of CPUID words.

-hpa

author maintainer
#3 lore raw

RFC PATCH 2/4 x86, cpu: Convert F00F bug detection

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

... to using the new facility and drop the cpuinfo_x86 member.

---
 arch/x86/include/asm/cpufeature.h | 2 ++
 arch/x86/include/asm/processor.h  | 1 -
 arch/x86/kernel/cpu/intel.c       | 4 ++--
 arch/x86/kernel/cpu/proc.c        | 2 +-
 arch/x86/mm/fault.c               | 2 +-
 5 files changed, 6 insertions(+), 5 deletions(-)
arch/x86/include/asm/cpufeature.h +2 -0
index 68daf877dad5..22107c57c0e4 100644
@@ -216,6 +216,8 @@
 #define X86_FEATURE_ADX		(9*32+19) /* The ADCX and ADOX instructions */
 #define X86_FEATURE_SMAP	(9*32+20) /* Supervisor Mode Access Prevention */
 
+#define X86_BUG_F00F		(NCAPINTS*32+ 0) /* Intel F00F bug */
+
 #if defined(__KERNEL__) && !defined(__ASSEMBLY__)
 
 #include <asm/asm.h>
arch/x86/include/asm/processor.h +0 -1
index 67721c634bf0..60b21f132ae4 100644
@@ -93,7 +93,6 @@ struct cpuinfo_x86 {
 	char			hard_math;
 	char			rfu;
 	char			fdiv_bug;
-	char			f00f_bug;
 	char			coma_bug;
 	char			pad0;
 #else
arch/x86/kernel/cpu/intel.c +2 -2
index 1905ce98bee0..1acdd42d86d1 100644
@@ -209,11 +209,11 @@ static void __cpuinit intel_workarounds(struct cpuinfo_x86 *c)
 	 * system.
 	 * Note that the workaround only should be initialized once...
 	 */
-	c->f00f_bug = 0;
+	clear_cpu_bug(c, X86_BUG_F00F);
 	if (!paravirt_enabled() && c->x86 == 5) {
 		static int f00f_workaround_enabled;
 
-		c->f00f_bug = 1;
+		set_cpu_bug(c, X86_BUG_F00F);
 		if (!f00f_workaround_enabled) {
 			trap_init_f00f_bug();
 			printk(KERN_NOTICE "Intel Pentium with F0 0F bug - workaround enabled.\n");
arch/x86/kernel/cpu/proc.c +1 -1
index 3286a92e662a..debb8826589b 100644
@@ -37,7 +37,7 @@ static void show_cpuinfo_misc(struct seq_file *m, struct cpuinfo_x86 *c)
 		   "wp\t\t: %s\n",
 		   c->fdiv_bug ? "yes" : "no",
 		   c->hlt_works_ok ? "no" : "yes",
-		   c->f00f_bug ? "yes" : "no",
+		   static_cpu_has_bug(X86_BUG_F00F) ? "yes" : "no",
 		   c->coma_bug ? "yes" : "no",
 		   c->hard_math ? "yes" : "no",
 		   c->hard_math ? "yes" : "no",
arch/x86/mm/fault.c +1 -1
index fb674fd3fc22..aaaf6931ff0b 100644
@@ -555,7 +555,7 @@ static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
 	/*
 	 * Pentium F0 0F C7 C8 bug workaround:
 	 */
-	if (boot_cpu_data.f00f_bug) {
+	if (boot_cpu_has_bug(X86_BUG_F00F)) {
 		nr = (address - idt_descr.address) >> 3;
 
 		if (nr == 6) {
Signature · 1 line
1.8.1.3.535.ga923c31
author maintainer
#4 lore raw

RFC PATCH 3/4 x86, cpu: Convert FDIV bug detection

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

... to the new facility.

---
 arch/x86/include/asm/cpufeature.h | 1 +
 arch/x86/include/asm/processor.h  | 1 -
 arch/x86/kernel/cpu/bugs.c        | 5 +++--
 arch/x86/kernel/cpu/proc.c        | 2 +-
 4 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/cpufeature.h +1 -0
index 22107c57c0e4..6be6fab3dced 100644
@@ -217,6 +217,7 @@
 #define X86_FEATURE_SMAP	(9*32+20) /* Supervisor Mode Access Prevention */
 
 #define X86_BUG_F00F		(NCAPINTS*32+ 0) /* Intel F00F bug */
+#define X86_BUG_FDIV		(NCAPINTS*32+ 1) /* FPU FDIV bug */
 
 #if defined(__KERNEL__) && !defined(__ASSEMBLY__)
 
arch/x86/include/asm/processor.h +0 -1
index 60b21f132ae4..d18dedf333aa 100644
@@ -92,7 +92,6 @@ struct cpuinfo_x86 {
 	char			hlt_works_ok;
 	char			hard_math;
 	char			rfu;
-	char			fdiv_bug;
 	char			coma_bug;
 	char			pad0;
 #else
arch/x86/kernel/cpu/bugs.c +3 -2
index 92dfec986a48..3ca8ab0001bc 100644
@@ -84,9 +84,10 @@ static void __init check_fpu(void)
 
 	kernel_fpu_end();
 
-	boot_cpu_data.fdiv_bug = fdiv_bug;
-	if (boot_cpu_data.fdiv_bug)
+	if (fdiv_bug) {
+		set_cpu_bug(&boot_cpu_data, X86_BUG_FDIV);
 		pr_warn("Hmm, FPU with FDIV bug\n");
+	}
 }
 
 static void __init check_hlt(void)
arch/x86/kernel/cpu/proc.c +1 -1
index debb8826589b..de41600664da 100644
@@ -35,7 +35,7 @@ static void show_cpuinfo_misc(struct seq_file *m, struct cpuinfo_x86 *c)
 		   "fpu_exception\t: %s\n"
 		   "cpuid level\t: %d\n"
 		   "wp\t\t: %s\n",
-		   c->fdiv_bug ? "yes" : "no",
+		   static_cpu_has_bug(X86_BUG_FDIV) ? "yes" : "no",
 		   c->hlt_works_ok ? "no" : "yes",
 		   static_cpu_has_bug(X86_BUG_F00F) ? "yes" : "no",
 		   c->coma_bug ? "yes" : "no",
Signature · 1 line
1.8.1.3.535.ga923c31
author maintainer
#5 lore raw

RFC PATCH 4/4 x86, cpu: Convert Cyrix coma bug detection

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

... to the new facility. Drop the padding too since it becomes unnecessary now.

---
 arch/x86/include/asm/cpufeature.h | 1 +
 arch/x86/include/asm/processor.h  | 2 --
 arch/x86/kernel/cpu/cyrix.c       | 5 +++--
 arch/x86/kernel/cpu/proc.c        | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/include/asm/cpufeature.h +1 -0
index 6be6fab3dced..62b9affc0948 100644
@@ -218,6 +218,7 @@
 
 #define X86_BUG_F00F		(NCAPINTS*32+ 0) /* Intel F00F bug */
 #define X86_BUG_FDIV		(NCAPINTS*32+ 1) /* FPU FDIV bug */
+#define X86_BUG_COMA		(NCAPINTS*32+ 2) /* Cyrix 6x86 coma */
 
 #if defined(__KERNEL__) && !defined(__ASSEMBLY__)
 
arch/x86/include/asm/processor.h +0 -2
index d18dedf333aa..c7f1066bd93a 100644
@@ -92,8 +92,6 @@ struct cpuinfo_x86 {
 	char			hlt_works_ok;
 	char			hard_math;
 	char			rfu;
-	char			coma_bug;
-	char			pad0;
 #else
 	/* Number of 4K pages in DTLB/ITLB combined(in pages): */
 	int			x86_tlbsize;
arch/x86/kernel/cpu/cyrix.c +3 -2
index 4fbd384fb645..ef060edeb68e 100644
@@ -249,7 +249,7 @@ static void __cpuinit init_cyrix(struct cpuinfo_x86 *c)
 		/* Emulate MTRRs using Cyrix's ARRs. */
 		set_cpu_cap(c, X86_FEATURE_CYRIX_ARR);
 		/* 6x86's contain this bug */
-		c->coma_bug = 1;
+		set_cpu_bug(c, X86_BUG_COMA);
 		break;
 
 	case 4: /* MediaGX/GXm or Geode GXM/GXLV/GX1 */
@@ -317,7 +317,8 @@ static void __cpuinit init_cyrix(struct cpuinfo_x86 *c)
 			/* Enable MMX extensions (App note 108) */
 			setCx86_old(CX86_CCR7, getCx86_old(CX86_CCR7)|1);
 		} else {
-			c->coma_bug = 1;      /* 6x86MX, it has the bug. */
+			/* 6x86MX, it has the bug. */
+			set_cpu_bug(c, X86_BUG_COMA);
 		}
 		tmp = (!(dir0_lsn & 7) || dir0_lsn & 1) ? 2 : 0;
 		Cx86_cb[tmp] = cyrix_model_mult2[dir0_lsn & 7];
arch/x86/kernel/cpu/proc.c +1 -1
index de41600664da..7497d1eb6053 100644
@@ -38,7 +38,7 @@ static void show_cpuinfo_misc(struct seq_file *m, struct cpuinfo_x86 *c)
 		   static_cpu_has_bug(X86_BUG_FDIV) ? "yes" : "no",
 		   c->hlt_works_ok ? "no" : "yes",
 		   static_cpu_has_bug(X86_BUG_F00F) ? "yes" : "no",
-		   c->coma_bug ? "yes" : "no",
+		   static_cpu_has_bug(X86_BUG_COMA) ? "yes" : "no",
 		   c->hard_math ? "yes" : "no",
 		   c->hard_math ? "yes" : "no",
 		   c->cpuid_level,
Signature · 1 line
1.8.1.3.535.ga923c31