From: Uros Bizjak <ubizjak@gmail.com>
To: x86@kernel.org, linux-kernel@vger.kernel.org
Cc: Uros Bizjak <ubizjak@gmail.com>, "H. Peter Anvin" <hpa@zytor.com>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@kernel.org>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>
Subject: [PATCH RESEND -tip v2 6/7] x86/segment: Introduce savesegment_mem16() helper to write segment selectors to memory
Date: Tue, 28 Apr 2026 18:03:40 +0200 [thread overview]
Message-ID: <20260428160443.3593331-6-ubizjak@gmail.com> (raw)
In-Reply-To: <20260428160443.3593331-1-ubizjak@gmail.com>
Introduce a new helper, savesegment_mem16(), 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 savesegment_mem16() 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>
---
v2: Rename storesegment() to savesegment_mem16() (Ingo).
---
arch/x86/include/asm/segment.h | 31 ++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
index 070ed87063e0..51a1fcdc7215 100644
--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -354,7 +354,7 @@ typedef unsigned long __seg_return_t;
/*
* Save a segment register away:
*/
-#define SAVE_SEGMENT(seg) \
+#define SAVE_SEGMENT_VAR(seg) \
static inline unsigned long __savesegment_##seg(void) \
{ \
__seg_return_t v; \
@@ -362,6 +362,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)
@@ -369,10 +379,29 @@ 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())
+/*
+ * savesegment_mem16(seg, loc) - Store a segment register directly
+ * to u16 (or compatible) 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) location.
+ */
+#define savesegment_mem16(seg, loc) __savesegment_##seg##_ptr(&(loc))
+
#endif /* !__ASSEMBLER__ */
#endif /* __KERNEL__ */
--
2.53.0
next prev parent reply other threads:[~2026-04-28 16:05 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-28 16:03 [PATCH RESEND -tip v2 1/7] x86_32/segment: Always return correctly zero-extended values from savesegment_*() Uros Bizjak
2026-04-28 16:03 ` [PATCH RESEND -tip v2 2/7] x86/insn-eval: Fix signedness bug in segment selector handling Uros Bizjak
2026-04-28 16:03 ` [PATCH RESEND -tip v2 3/7] x86/ptrace: Use savesegment() in get_segment_reg() instead of inline asm Uros Bizjak
2026-04-28 16:03 ` [PATCH RESEND -tip v2 4/7] x86/ptrace: Use unsigned short for segment values in get_segment_reg() Uros Bizjak
2026-04-28 16:03 ` [PATCH RESEND -tip v2 5/7] x86/kexec: store segment registers directly to memory in crash_setup_regs() Uros Bizjak
2026-04-28 16:03 ` Uros Bizjak [this message]
2026-04-28 16:03 ` [PATCH RESEND -tip v2 7/7] x86/process: Use savesegment_mem16() when saving segment selectors Uros Bizjak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260428160443.3593331-6-ubizjak@gmail.com \
--to=ubizjak@gmail.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox