mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Uros Bizjak <ubizjak@gmail.com>, Petr Mladek <pmladek@suse.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Kees Cook <kees@kernel.org>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Kiryl Shutsemau <kas@kernel.org>,
	Rick Edgecombe <rick.p.edgecombe@intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>,
	linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev,
	x86@kernel.org
Subject: [PATCH v1 11/14] x86/boot: use __seg_fs and __seg_gs in the real-mode boot code
Date: Tue, 20 Jan 2026 11:54:03 -0800	[thread overview]
Message-ID: <20260120195407.1163051-12-hpa@zytor.com> (raw)
In-Reply-To: <20260120195407.1163051-1-hpa@zytor.com>

All supported versions of gcc support __seg_fs and __seg_gs now.
All supported versions of clang support __seg_fs and __seg_gs too,
except for two bugs (as of clang 21, at least):

1. The %fs: and %gs: prefix does not get emitted in inline assembly.
2. An internal compiler error when addressing symbols directly.

However, none of these are required in the boot code. Furthermore,
this makes it possible to remove the absolute_pointer() hack in the
fs/gs access functions.

This requires adding a barrier() to a20.c, to prevent the compiler
from eliding the load from the aliased memory address.

Remove the unused memcmp_[fg]s() functions.

Finally, ds() is by necessity constant, so mark the function as such.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
---
 arch/x86/boot/a20.c  |  1 +
 arch/x86/boot/boot.h | 81 ++++++++++++++------------------------------
 2 files changed, 27 insertions(+), 55 deletions(-)

diff --git a/arch/x86/boot/a20.c b/arch/x86/boot/a20.c
index 3ab6cd8eaa31..52c3fccdcb70 100644
--- a/arch/x86/boot/a20.c
+++ b/arch/x86/boot/a20.c
@@ -63,6 +63,7 @@ static int a20_test(int loops)
 	while (loops--) {
 		wrgs32(++ctr, A20_TEST_ADDR);
 		io_delay();	/* Serialize and make delay constant */
+		barrier();	/* Compiler won't know about fs/gs overlap */
 		ok = rdfs32(A20_TEST_ADDR+0x10) ^ ctr;
 		if (ok)
 			break;
diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h
index b4eb8405ba55..4d3549ed7987 100644
--- a/arch/x86/boot/boot.h
+++ b/arch/x86/boot/boot.h
@@ -45,7 +45,7 @@ static inline void io_delay(void)
 
 /* These functions are used to reference data in other segments. */
 
-static inline u16 ds(void)
+static inline __attribute_const__ u16 ds(void)
 {
 	u16 seg;
 	asm("movw %%ds,%0" : "=rm" (seg));
@@ -54,7 +54,7 @@ static inline u16 ds(void)
 
 static inline void set_fs(u16 seg)
 {
-	asm volatile("movw %0,%%fs" : : "rm" (seg));
+	asm volatile("movw %0,%%fs" : : "rm" (seg) : "memory");
 }
 static inline u16 fs(void)
 {
@@ -65,7 +65,7 @@ static inline u16 fs(void)
 
 static inline void set_gs(u16 seg)
 {
-	asm volatile("movw %0,%%gs" : : "rm" (seg));
+	asm volatile("movw %0,%%gs" : : "rm" (seg) : "memory");
 }
 static inline u16 gs(void)
 {
@@ -76,96 +76,67 @@ static inline u16 gs(void)
 
 typedef unsigned int addr_t;
 
+/*
+ * WARNING: as of clang 21, clang has the following two bugs related
+ * to __seg_fs and __seg_gs:
+ *
+ * 1. The %fs: and %gs: prefix does not get emitted in inline assembly.
+ * 2. An internal compiler error when addressing symbols directly.
+ *
+ * Neither of those constructs are currently used in the boot code.
+ * If they ever are, and those bugs still remain, then those bugs will
+ * need to be worked around.
+ */
 static inline u8 rdfs8(addr_t addr)
 {
-	u8 *ptr = (u8 *)absolute_pointer(addr);
-	u8 v;
-	asm volatile("movb %%fs:%1,%0" : "=q" (v) : "m" (*ptr));
-	return v;
+	return *(__seg_fs const u8 *)addr;
 }
 static inline u16 rdfs16(addr_t addr)
 {
-	u16 *ptr = (u16 *)absolute_pointer(addr);
-	u16 v;
-	asm volatile("movw %%fs:%1,%0" : "=r" (v) : "m" (*ptr));
-	return v;
+	return *(__seg_fs const u16 *)addr;
 }
 static inline u32 rdfs32(addr_t addr)
 {
-	u32 *ptr = (u32 *)absolute_pointer(addr);
-	u32 v;
-	asm volatile("movl %%fs:%1,%0" : "=r" (v) : "m" (*ptr));
-	return v;
+	return *(__seg_fs const u32 *)addr;
 }
 
 static inline void wrfs8(u8 v, addr_t addr)
 {
-	u8 *ptr = (u8 *)absolute_pointer(addr);
-	asm volatile("movb %1,%%fs:%0" : "+m" (*ptr) : "qi" (v));
+	*(__seg_fs u8 *)addr = v;
 }
 static inline void wrfs16(u16 v, addr_t addr)
 {
-	u16 *ptr = (u16 *)absolute_pointer(addr);
-	asm volatile("movw %1,%%fs:%0" : "+m" (*ptr) : "ri" (v));
+	*(__seg_fs u16 *)addr = v;
 }
 static inline void wrfs32(u32 v, addr_t addr)
 {
-	u32 *ptr = (u32 *)absolute_pointer(addr);
-	asm volatile("movl %1,%%fs:%0" : "+m" (*ptr) : "ri" (v));
+	*(__seg_fs u32 *)addr = v;
 }
 
 static inline u8 rdgs8(addr_t addr)
 {
-	u8 *ptr = (u8 *)absolute_pointer(addr);
-	u8 v;
-	asm volatile("movb %%gs:%1,%0" : "=q" (v) : "m" (*ptr));
-	return v;
+	return *(__seg_gs const u8 *)addr;
 }
 static inline u16 rdgs16(addr_t addr)
 {
-	u16 *ptr = (u16 *)absolute_pointer(addr);
-	u16 v;
-	asm volatile("movw %%gs:%1,%0" : "=r" (v) : "m" (*ptr));
-	return v;
+	return *(__seg_gs const u16 *)addr;
 }
 static inline u32 rdgs32(addr_t addr)
 {
-	u32 *ptr = (u32 *)absolute_pointer(addr);
-	u32 v;
-	asm volatile("movl %%gs:%1,%0" : "=r" (v) : "m" (*ptr));
-	return v;
+	return *(__seg_gs const u32 *)addr;
 }
 
 static inline void wrgs8(u8 v, addr_t addr)
 {
-	u8 *ptr = (u8 *)absolute_pointer(addr);
-	asm volatile("movb %1,%%gs:%0" : "+m" (*ptr) : "qi" (v));
+	*(__seg_gs u8 *)addr = v;
 }
 static inline void wrgs16(u16 v, addr_t addr)
 {
-	u16 *ptr = (u16 *)absolute_pointer(addr);
-	asm volatile("movw %1,%%gs:%0" : "+m" (*ptr) : "ri" (v));
+	*(__seg_gs u16 *)addr = v;
 }
 static inline void wrgs32(u32 v, addr_t addr)
 {
-	u32 *ptr = (u32 *)absolute_pointer(addr);
-	asm volatile("movl %1,%%gs:%0" : "+m" (*ptr) : "ri" (v));
-}
-
-/* Note: these only return true/false, not a signed return value! */
-static inline bool memcmp_fs(const void *s1, addr_t s2, size_t len)
-{
-	bool diff;
-	asm volatile("fs repe cmpsb"
-		     : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len));
-	return diff;
-}
-static inline bool memcmp_gs(const void *s1, addr_t s2, size_t len)
-{
-	bool diff;
-	asm volatile("gs repe cmpsb"
-		     : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len));
-	return diff;
+	*(__seg_gs u32 *)addr = v;
 }
 
 /* Heap -- available for dynamic lists. */
-- 
2.52.0


  parent reply	other threads:[~2026-01-20 19:55 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260119192923.651588-1-hpa@zytor.com>
2026-01-20 19:53 ` [PATCH v1 00/14] x86 boot code cleanups, batch 1 H. Peter Anvin
2026-01-20 19:53   ` [PATCH v1 01/14] x86/realmode: remove I/O port paravirtualization H. Peter Anvin
2026-01-20 21:47     ` David Laight
2026-01-21 12:27     ` Kiryl Shutsemau
2026-01-28 17:01     ` Borislav Petkov
2026-01-20 19:53   ` [PATCH v1 02/14] x86/realmode: make %gs == 0 an invariant H. Peter Anvin
2026-01-20 22:40     ` David Laight
2026-01-20 22:51       ` H. Peter Anvin
2026-01-21  0:47       ` H. Peter Anvin
2026-01-21 10:03     ` Uros Bizjak
2026-01-20 19:53   ` [PATCH v1 03/14] x86/boot: use <linux/compiler.h> H. Peter Anvin
2026-01-21 10:04     ` Uros Bizjak
2026-01-20 19:53   ` [PATCH v1 04/14] x86/boot: modernize the segment structure for the header and setup H. Peter Anvin
2026-01-20 19:53   ` [PATCH v1 05/14] x86/boot: call puts() from within die() H. Peter Anvin
2026-01-21 10:07     ` Uros Bizjak
2026-01-20 19:53   ` [PATCH v1 06/14] x86/boot: add comment barriers for the different headers H. Peter Anvin
2026-01-20 19:53   ` [PATCH v1 07/14] x86/boot: factor out the 16-bit startup code from header.S H. Peter Anvin
2026-01-21 10:08     ` Uros Bizjak
2026-01-20 19:54   ` [PATCH v1 08/14] x86: make CONFIG_EFI_STUB unconditional H. Peter Anvin
2026-01-20 19:54   ` [PATCH v1 09/14] x86/boot: make the relocatable kernel unconditional H. Peter Anvin
2026-01-20 19:54   ` [PATCH v1 10/14] x86/boot: explicitly put the old command line pointer in header.S H. Peter Anvin
2026-01-20 19:54   ` H. Peter Anvin [this message]
2026-01-21  8:56     ` [PATCH v1 11/14] x86/boot: use __seg_fs and __seg_gs in the real-mode boot code Uros Bizjak
2026-01-21  9:40       ` Uros Bizjak
2026-01-21 15:13       ` H. Peter Anvin
2026-01-21 16:03         ` Uros Bizjak
2026-01-22 18:57       ` [PATCH v1 08/14] x86: make CONFIG_EFI_STUB unconditional Simon Glass
2026-01-23  0:11         ` H. Peter Anvin
2026-01-26 21:19           ` Simon Glass
2026-01-26 22:20             ` H. Peter Anvin
2026-01-27  1:44               ` Simon Glass
2026-01-27  2:39                 ` H. Peter Anvin
2026-01-27  2:54                 ` H. Peter Anvin
2026-01-27  3:14                   ` Simon Glass
2026-01-27  3:21                     ` H. Peter Anvin
2026-01-29 22:13                       ` Simon Glass
2026-01-29 22:22                         ` H. Peter Anvin
2026-01-21 16:07     ` [PATCH v1 11/14] x86/boot: use __seg_fs and __seg_gs in the real-mode boot code Uros Bizjak
2026-01-20 19:54   ` [PATCH v1 12/14] x86/boot: tweak a20.c for better code generation H. Peter Anvin
2026-01-21 10:10     ` Uros Bizjak
2026-01-21 11:49     ` David Laight
2026-01-24  3:00       ` Maciej W. Rozycki
2026-01-24  4:24         ` H. Peter Anvin
2026-01-24 23:07           ` David Laight
2026-01-24 23:16             ` H. Peter Anvin
2026-01-24 23:40               ` H. Peter Anvin
2026-01-20 19:54   ` [PATCH v1 13/14] x86/boot: simplify x86/boot/cmdline.c by using __seg_fs H. Peter Anvin
2026-01-21 10:11     ` Uros Bizjak
2026-01-20 19:54   ` [PATCH v1 14/14] compiler-gcc: Remove obsolete RELOC_HIDE() macro H. Peter Anvin
2026-01-21 10:18   ` [PATCH v1 00/14] x86 boot code cleanups, batch 1 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=20260120195407.1163051-12-hpa@zytor.com \
    --to=hpa@zytor.com \
    --cc=akpm@linux-foundation.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=kas@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=nathan@kernel.org \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=rick.p.edgecombe@intel.com \
    --cc=tglx@kernel.org \
    --cc=ubizjak@gmail.com \
    --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

all inboxes | Powered by JetHome®