Skip to contentSkip to conversation

x86_32/segment: Always return correctly zero-extended values from savesegment_*()

PATCH Superseded Uros Bizjak 1 participant 2 messages Apr 14 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86_32/segment: Always return correctly zero-extended values from savesegment_*()

On 32-bit x86, reading segment registers into a 32-bit type can expose undefined upper bits on older processors (e.g. Intel Quark X1000, Pentium, and earlier), where bits 31:16 are not defined.

As a result, undefined upper bits may be stored in variables (e.g. saved_fs and saved_gs in apm_32.c), structures (e.g. thread_struct.gs) and arrays (e.g. elf_gregset_t[]). Some of these are later used, for example in process_32.c::__switch_to():

	if (prev->gs | next->gs)
		loadsegment(gs, next->gs);

which results in unneccessary reloads.

Introduce __seg_return_t as an intermediate type for __savesegment_*():
u16 on CONFIG_X86_32 and unsigned long otherwise.

This ensures that __savesegment_*() returns correctly zero-extended values in all cases, avoiding propagation of undefined high bits on 32-bit systems while preserving existing behavior on 64-bit.

A follow-up patch can remove the majority of zero-extensions by addressing a long-standing XXX comment in thread_struct and reusing gsindex instead of gs for 32-bit x86 targets as well.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>

Uros Bizjak (1):
x86_32/segment: Always return correctly zero-extended values from

    savesegment_*()
 arch/x86/include/asm/segment.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/1 x86_32/segment: Always return correctly zero-extended values from savesegment_*()

On 32-bit x86, reading segment registers into a 32-bit type can expose undefined upper bits on older processors (e.g. Intel Quark X1000, Pentium, and earlier), where bits 31:16 are not defined.

Introduce __seg_return_t as an intermediate type for __savesegment_*():
u16 on CONFIG_X86_32 and unsigned long otherwise.

As a result, __savesegment_*() now returns correctly zero-extended value in all cases, avoiding propagation of undefined high bits on 32-bit systems while preserving existing behavior on 64-bit.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/segment.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/include/asm/segment.h +11 -1
index dbd90fede5e7..070ed87063e0 100644
@@ -341,13 +341,23 @@ static inline void __loadsegment_fs(u16 value)
 
 #define loadsegment(seg, val) __loadsegment_##seg(val)
 
+#ifdef CONFIG_X86_32
+/*
+ * Bits 31:16 are undefined for Intel Quark X1000 processors,
+ * Pentium, and earlier processors.
+ */
+typedef u16 __seg_return_t;
+#else
+typedef unsigned long __seg_return_t;
+#endif
+
 /*
  * Save a segment register away:
  */
 #define SAVE_SEGMENT(seg)				\
 static inline unsigned long __savesegment_##seg(void)	\
 {							\
-	unsigned long v;				\
+	__seg_return_t v;				\
 	asm volatile("movl %%" #seg ",%k0" : "=r" (v));	\
 	return v;					\
 }
Signature · 1 line
2.53.0