public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] x86/segment: Introduce storesegment() helper to write segment selectors to memory
@ 2026-03-31  6:38 Uros Bizjak
  2026-03-31  6:38 ` [PATCH 2/2] x86/process: Use storesegment() when saving segment selectors Uros Bizjak
  2026-03-31  6:56 ` [PATCH 1/2] x86/segment: Introduce storesegment() helper to write segment selectors to memory Ingo Molnar
  0 siblings, 2 replies; 8+ messages in thread
From: Uros Bizjak @ 2026-03-31  6:38 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Uros Bizjak, H. Peter Anvin, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen

Introduce a new helper, storesegment(), that stores a segment selector
directly into a u16 (or compatible) memory location without using an
intermediate general-purpose register.

To support this, split the existing SAVE_SEGMENT macro into two parts:

SAVE_SEGMENT_VAR(): retains the current behavior of reading a segment
register into an unsigned long via a register.
SAVE_SEGMENT_PTR(): adds a new variant that writes the 16-bit selector
directly to memory.

The combined SAVE_SEGMENT() macro now generates both helpers for each
segment register.

The new storesegment() interface is preferred over savesegment() when
the value only needs to be stored (e.g. into a struct field), avoiding
an unnecessary register move and making the intent clearer.

No functional change for existing users of savesegment().

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Suggested-by: "H. Peter Anvin" <hpa@zytor.com>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
---
 arch/x86/include/asm/segment.h | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
index dbd90fede5e7..b9728c4acd46 100644
--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -344,7 +344,7 @@ static inline void __loadsegment_fs(u16 value)
 /*
  * Save a segment register away:
  */
-#define SAVE_SEGMENT(seg)				\
+#define SAVE_SEGMENT_VAR(seg)				\
 static inline unsigned long __savesegment_##seg(void)	\
 {							\
 	unsigned long v;				\
@@ -352,6 +352,16 @@ static inline unsigned long __savesegment_##seg(void)	\
 	return v;					\
 }
 
+#define SAVE_SEGMENT_PTR(seg)				\
+static inline void __savesegment_##seg##_ptr(u16 *p)	\
+{							\
+	asm volatile("movw %%" #seg ",%0" : "=m" (*p));	\
+}
+
+#define SAVE_SEGMENT(seg)				\
+	SAVE_SEGMENT_VAR(seg)				\
+	SAVE_SEGMENT_PTR(seg)
+
 SAVE_SEGMENT(cs)
 SAVE_SEGMENT(ss)
 SAVE_SEGMENT(ds)
@@ -359,10 +369,28 @@ SAVE_SEGMENT(es)
 SAVE_SEGMENT(fs)
 SAVE_SEGMENT(gs)
 
+#undef SAVE_SEGMENT_VAR
+#undef SAVE_SEGMENT_PTR
 #undef SAVE_SEGMENT
 
+/*
+ * savesegment(seg, var) - Read a segment register into an unsigned long.
+ *
+ * Reads the segment selector via a general-purpose register into an
+ * unsigned long. Preferred when the value is needed in a register for
+ * subsequent arithmetic or comparison.
+ */
 #define savesegment(seg, var) ((var) = __savesegment_##seg())
 
+/*
+ * storesegment(seg, loc) - Store a segment register directly to a u16 location.
+ *
+ * Writes the 16-bit segment selector directly to memory, bypassing any
+ * intermediate general-purpose register. Preferred over savesegment()
+ * for simply saving a segment register to a u16 (or compatible) field.
+ */
+#define storesegment(seg, loc) __savesegment_##seg##_ptr(&(loc))
+
 #endif /* !__ASSEMBLER__ */
 #endif /* __KERNEL__ */
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-04-01  7:02 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-31  6:38 [PATCH 1/2] x86/segment: Introduce storesegment() helper to write segment selectors to memory Uros Bizjak
2026-03-31  6:38 ` [PATCH 2/2] x86/process: Use storesegment() when saving segment selectors Uros Bizjak
2026-03-31  6:56 ` [PATCH 1/2] x86/segment: Introduce storesegment() helper to write segment selectors to memory Ingo Molnar
2026-03-31  9:53   ` Uros Bizjak
2026-03-31  9:59     ` Uros Bizjak
2026-04-01  6:40     ` Ingo Molnar
2026-04-01  6:59       ` Uros Bizjak
2026-04-01  7:02         ` Ingo Molnar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox