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 13/14] x86/boot: simplify x86/boot/cmdline.c by using __seg_fs
Date: Tue, 20 Jan 2026 11:54:05 -0800 [thread overview]
Message-ID: <20260120195407.1163051-14-hpa@zytor.com> (raw)
In-Reply-To: <20260120195407.1163051-1-hpa@zytor.com>
arch/x86/boot/cmdline.c is compiled in 16, 32, or 64-bit mode. In the
16-bit case it needs to access an out-of-data-segment pointer.
This has been handled in the past by using a *really* ugly wrapper in
the 32/64-bit code combined with an inline function in the 16-bit code.
Using __seg_fs for the real-mode case allows for a much cleaner way of
handling this difference. Futhermore, moving the code for typing and
obtaining the pointer into cmdline.c itself avoids having to create
and push an extra argument, which is particularly inefficient in the
real-mode code as it requires a range check and pushes the argument
count past 3. Furthermore, co-locating this code with the user makes it
a lot clearer what the constraints are on this code, even though it
means using #ifdef _SETUP.
Instead of limit-checking the command line to the real-mode segment
limit, limit-check it to COMMAND_LINE_SIZE (which is always less than
64K, and thus automatically incorporates the real-mode segment limit
check.)
If compiling for 32 bits, trying to add ext_cmd_line_ptr in
get_cmd_line_ptr() is futile as unsigned long, and pointers, are only
32 bits wide. Instead, limit-check the command line pointer and fail
if it is >= 4 GiB, just as the real-mode code fails if the pointer is
>= 1 MiB.
Since the kaslr code depends on get_cmd_line_ptr(), retain it as a
global function.
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
---
arch/x86/boot/boot.h | 23 +--------
arch/x86/boot/cmdline.c | 79 +++++++++++++++++++++++-------
arch/x86/boot/compressed/cmdline.c | 29 -----------
3 files changed, 64 insertions(+), 67 deletions(-)
diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h
index 584c89d0738b..8512db8b3f8e 100644
--- a/arch/x86/boot/boot.h
+++ b/arch/x86/boot/boot.h
@@ -216,27 +216,8 @@ struct biosregs {
void intcall(u8 int_no, const struct biosregs *ireg, struct biosregs *oreg);
/* cmdline.c */
-int __cmdline_find_option(unsigned long cmdline_ptr, const char *option, char *buffer, int bufsize);
-int __cmdline_find_option_bool(unsigned long cmdline_ptr, const char *option);
-static inline int cmdline_find_option(const char *option, char *buffer, int bufsize)
-{
- unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
-
- if (cmd_line_ptr >= 0x100000)
- return -1; /* inaccessible */
-
- return __cmdline_find_option(cmd_line_ptr, option, buffer, bufsize);
-}
-
-static inline int cmdline_find_option_bool(const char *option)
-{
- unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
-
- if (cmd_line_ptr >= 0x100000)
- return -1; /* inaccessible */
-
- return __cmdline_find_option_bool(cmd_line_ptr, option);
-}
+int cmdline_find_option(const char *option, char *buffer, int bufsize);
+int cmdline_find_option_bool(const char *option);
/* cpu.c, cpucheck.c */
int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr);
diff --git a/arch/x86/boot/cmdline.c b/arch/x86/boot/cmdline.c
index 21d56ae83cdf..3f31fcbed673 100644
--- a/arch/x86/boot/cmdline.c
+++ b/arch/x86/boot/cmdline.c
@@ -11,12 +11,59 @@
*/
#include "boot.h"
+#include <asm/setup.h>
+#include <asm/bootparam.h>
static inline int myisspace(u8 c)
{
return c <= ' '; /* Close enough approximation */
}
+#ifdef _SETUP
+typedef const char __seg_fs *cptr_t;
+static inline cptr_t get_cptr(void)
+{
+ /*
+ * Note: there is no reason to check ext_cmd_line_ptr here,
+ * because it falls outside of boot_params.hdr and therefore
+ * will always be zero when entering through the real-mode
+ * entry point.
+ */
+ unsigned long ptr = boot_params.hdr.cmd_line_ptr;
+
+ /*
+ * The -16 here serves two purposes:
+ * 1. It means the segbase >= 0x100000 check also doubles as
+ * a check for the command line pointer being zero.
+ * 2. It means this routine won't return a NULL pointer for
+ * a valid address; it will always return a pointer in the
+ * range 0x10-0x1f inclusive.
+ */
+ unsigned long segbase = (ptr - 16) & ~15;
+ if (segbase >= 0x100000)
+ return NULL;
+
+ set_fs(segbase >> 4);
+ return (cptr_t)(ptr - segbase);
+}
+#else
+unsigned long get_cmd_line_ptr(void)
+{
+ unsigned long ptr = boot_params_ptr->hdr.cmd_line_ptr;
+ if (sizeof(unsigned long) > 4)
+ ptr += (u64)boot_params_ptr->ext_cmd_line_ptr << 32;
+ else if (boot_params_ptr->ext_cmd_line_ptr)
+ return 0; /* Inaccessible due to pointer overflow */
+
+ return ptr;
+}
+typedef const char *cptr_t;
+static inline cptr_t get_cptr(void)
+{
+ return (cptr_t)get_cmd_line_ptr();
+}
+#endif
+
/*
* Find a non-boolean option, that is, "option=argument". In accordance
* with standard Linux practice, if this option is repeated, this returns
@@ -25,9 +72,9 @@ static inline int myisspace(u8 c)
* Returns the length of the argument (regardless of if it was
* truncated to fit in the buffer), or -1 on not found.
*/
-int __cmdline_find_option(unsigned long cmdline_ptr, const char *option, char *buffer, int bufsize)
+int cmdline_find_option(const char *option, char *buffer, int bufsize)
{
- addr_t cptr;
+ cptr_t cptr, eptr;
char c;
int len = -1;
const char *opptr = NULL;
@@ -39,13 +86,12 @@ int __cmdline_find_option(unsigned long cmdline_ptr, const char *option, char *b
st_bufcpy /* Copying this to buffer */
} state = st_wordstart;
- if (!cmdline_ptr)
- return -1; /* No command line */
+ cptr = get_cptr();
+ if (!cptr)
+ return -1; /* No command line or invalid pointer */
+ eptr = cptr + COMMAND_LINE_SIZE - 1;
- cptr = cmdline_ptr & 0xf;
- set_fs(cmdline_ptr >> 4);
-
- while (cptr < 0x10000 && (c = rdfs8(cptr++))) {
+ while (cptr < eptr && (c = *cptr++)) {
switch (state) {
case st_wordstart:
if (myisspace(c))
@@ -97,9 +143,9 @@ int __cmdline_find_option(unsigned long cmdline_ptr, const char *option, char *b
* Returns the position of that option (starts counting with 1)
* or 0 on not found
*/
-int __cmdline_find_option_bool(unsigned long cmdline_ptr, const char *option)
+int cmdline_find_option_bool(const char *option)
{
- addr_t cptr;
+ cptr_t cptr, eptr;
char c;
int pos = 0, wstart = 0;
const char *opptr = NULL;
@@ -109,14 +155,13 @@ int __cmdline_find_option_bool(unsigned long cmdline_ptr, const char *option)
st_wordskip, /* Miscompare, skip */
} state = st_wordstart;
- if (!cmdline_ptr)
- return -1; /* No command line */
-
- cptr = cmdline_ptr & 0xf;
- set_fs(cmdline_ptr >> 4);
+ cptr = get_cptr();
+ if (!cptr)
+ return -1; /* No command line or invalid pointer */
+ eptr = cptr + COMMAND_LINE_SIZE - 1;
- while (cptr < 0x10000) {
- c = rdfs8(cptr++);
+ while (cptr <= eptr) {
+ c = *cptr++;
pos++;
switch (state) {
diff --git a/arch/x86/boot/compressed/cmdline.c b/arch/x86/boot/compressed/cmdline.c
index e162d7f59cc5..0d9267a21012 100644
--- a/arch/x86/boot/compressed/cmdline.c
+++ b/arch/x86/boot/compressed/cmdline.c
@@ -1,32 +1,3 @@
// SPDX-License-Identifier: GPL-2.0
#include "misc.h"
-
-#include <asm/bootparam.h>
-
-static unsigned long fs;
-static inline void set_fs(unsigned long seg)
-{
- fs = seg << 4; /* shift it back */
-}
-typedef unsigned long addr_t;
-static inline char rdfs8(addr_t addr)
-{
- return *((char *)(fs + addr));
-}
#include "../cmdline.c"
-unsigned long get_cmd_line_ptr(void)
-{
- unsigned long cmd_line_ptr = boot_params_ptr->hdr.cmd_line_ptr;
-
- cmd_line_ptr |= (u64)boot_params_ptr->ext_cmd_line_ptr << 32;
-
- return cmd_line_ptr;
-}
-int cmdline_find_option(const char *option, char *buffer, int bufsize)
-{
- return __cmdline_find_option(get_cmd_line_ptr(), option, buffer, bufsize);
-}
-int cmdline_find_option_bool(const char *option)
-{
- return __cmdline_find_option_bool(get_cmd_line_ptr(), option);
-}
--
2.52.0
next prev 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 ` [PATCH v1 11/14] x86/boot: use __seg_fs and __seg_gs in the real-mode boot code H. Peter Anvin
2026-01-21 8:56 ` 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 ` H. Peter Anvin [this message]
2026-01-21 10:11 ` [PATCH v1 13/14] x86/boot: simplify x86/boot/cmdline.c by using __seg_fs 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-14-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®