From: Ard Biesheuvel <ardb+git@google.com>
To: linux-kernel@vger.kernel.org
Cc: x86@kernel.org, Ard Biesheuvel <ardb@kernel.org>,
Ingo Molnar <mingo@kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>
Subject: [RFC PATCH v2 4/6] x86/boot: Set 5-level paging CPU cap before entering C code
Date: Tue, 13 May 2025 13:12:02 +0200 [thread overview]
Message-ID: <20250513111157.717727-12-ardb+git@google.com> (raw)
In-Reply-To: <20250513111157.717727-8-ardb+git@google.com>
From: Ard Biesheuvel <ardb@kernel.org>
In order for pgtable_l5_enabled() to be reliable wherever it is used and
however early, set the associated CPU capability from asm code before
entering the startup C code.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
arch/x86/include/asm/cpufeature.h | 12 +++++++++---
arch/x86/kernel/asm-offsets.c | 8 ++++++++
arch/x86/kernel/asm-offsets_32.c | 9 ---------
arch/x86/kernel/cpu/common.c | 3 ---
arch/x86/kernel/head_64.S | 15 +++++++++++++++
5 files changed, 32 insertions(+), 15 deletions(-)
diff --git a/arch/x86/include/asm/cpufeature.h b/arch/x86/include/asm/cpufeature.h
index 893cbca37fe9..1b5de40e7bf7 100644
--- a/arch/x86/include/asm/cpufeature.h
+++ b/arch/x86/include/asm/cpufeature.h
@@ -2,10 +2,10 @@
#ifndef _ASM_X86_CPUFEATURE_H
#define _ASM_X86_CPUFEATURE_H
+#ifdef __KERNEL__
+#ifndef __ASSEMBLER__
#include <asm/processor.h>
-#if defined(__KERNEL__) && !defined(__ASSEMBLER__)
-
#include <asm/asm.h>
#include <linux/bitops.h>
#include <asm/alternative.h>
@@ -137,5 +137,11 @@ static __always_inline bool _static_cpu_has(u16 bit)
#define CPU_FEATURE_TYPEVAL boot_cpu_data.x86_vendor, boot_cpu_data.x86, \
boot_cpu_data.x86_model
-#endif /* defined(__KERNEL__) && !defined(__ASSEMBLER__) */
+#else /* !defined(__ASSEMBLER__) */
+ .macro setup_force_cpu_cap, cap:req
+ btsl $\cap % 32, boot_cpu_data+CPUINFO_x86_capability+4*(\cap / 32)(%rip)
+ btsl $\cap % 32, cpu_caps_set+4*(\cap / 32)(%rip)
+ .endm
+#endif /* !defined(__ASSEMBLER__) */
+#endif /* defined(__KERNEL__) */
#endif /* _ASM_X86_CPUFEATURE_H */
diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c
index ad4ea6fb3b6c..6259b474073b 100644
--- a/arch/x86/kernel/asm-offsets.c
+++ b/arch/x86/kernel/asm-offsets.c
@@ -33,6 +33,14 @@
static void __used common(void)
{
+ OFFSET(CPUINFO_x86, cpuinfo_x86, x86);
+ OFFSET(CPUINFO_x86_vendor, cpuinfo_x86, x86_vendor);
+ OFFSET(CPUINFO_x86_model, cpuinfo_x86, x86_model);
+ OFFSET(CPUINFO_x86_stepping, cpuinfo_x86, x86_stepping);
+ OFFSET(CPUINFO_cpuid_level, cpuinfo_x86, cpuid_level);
+ OFFSET(CPUINFO_x86_capability, cpuinfo_x86, x86_capability);
+ OFFSET(CPUINFO_x86_vendor_id, cpuinfo_x86, x86_vendor_id);
+
BLANK();
OFFSET(TASK_threadsp, task_struct, thread.sp);
#ifdef CONFIG_STACKPROTECTOR
diff --git a/arch/x86/kernel/asm-offsets_32.c b/arch/x86/kernel/asm-offsets_32.c
index 2b411cd00a4e..e0a292db97b2 100644
--- a/arch/x86/kernel/asm-offsets_32.c
+++ b/arch/x86/kernel/asm-offsets_32.c
@@ -12,15 +12,6 @@ void foo(void);
void foo(void)
{
- OFFSET(CPUINFO_x86, cpuinfo_x86, x86);
- OFFSET(CPUINFO_x86_vendor, cpuinfo_x86, x86_vendor);
- OFFSET(CPUINFO_x86_model, cpuinfo_x86, x86_model);
- OFFSET(CPUINFO_x86_stepping, cpuinfo_x86, x86_stepping);
- OFFSET(CPUINFO_cpuid_level, cpuinfo_x86, cpuid_level);
- OFFSET(CPUINFO_x86_capability, cpuinfo_x86, x86_capability);
- OFFSET(CPUINFO_x86_vendor_id, cpuinfo_x86, x86_vendor_id);
- BLANK();
-
OFFSET(PT_EBX, pt_regs, bx);
OFFSET(PT_ECX, pt_regs, cx);
OFFSET(PT_EDX, pt_regs, dx);
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index aaa6d9e51ef1..ea49322ba151 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -1672,9 +1672,6 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)
setup_clear_cpu_cap(X86_FEATURE_PCID);
#endif
- if (IS_ENABLED(CONFIG_X86_5LEVEL) && (native_read_cr4() & X86_CR4_LA57))
- setup_force_cpu_cap(X86_FEATURE_5LEVEL_PAGING);
-
detect_nopl();
}
diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S
index 069420853304..b4742942bece 100644
--- a/arch/x86/kernel/head_64.S
+++ b/arch/x86/kernel/head_64.S
@@ -27,6 +27,7 @@
#include <asm/fixmap.h>
#include <asm/smp.h>
#include <asm/thread_info.h>
+#include <asm/cpufeature.h>
/*
* We are not able to switch in one step to the final KERNEL ADDRESS SPACE
@@ -58,6 +59,20 @@ SYM_CODE_START_NOALIGN(startup_64)
*/
mov %rsi, %r15
+#ifdef CONFIG_X86_5LEVEL
+ /*
+ * Set the X86_FEATURE_5LEVEL_PAGING capability before calling into the
+ * C code, so that it is guaranteed to have a consistent view of any
+ * global pseudo-constants that are derived from pgtable_l5_enabled().
+ */
+ mov %cr4, %rax
+ btl $X86_CR4_LA57_BIT, %eax
+ jnc 0f
+
+ setup_force_cpu_cap X86_FEATURE_5LEVEL_PAGING
+0:
+#endif
+
/* Set up the stack for verify_cpu() */
leaq __top_init_kernel_stack(%rip), %rsp
--
2.49.0.1045.g170613ef41-goog
next prev parent reply other threads:[~2025-05-13 11:12 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-13 11:11 [RFC PATCH v2 0/6] x86: Robustify pgtable_l5_enabled() Ard Biesheuvel
2025-05-13 11:11 ` [RFC PATCH v2 1/6] x86/boot: Defer initialization of VM space related global variables Ard Biesheuvel
2025-05-14 8:15 ` [tip: x86/core] " tip-bot2 for Ard Biesheuvel
2025-05-13 11:12 ` [RFC PATCH v2 2/6] x86/cpu: Use a new feature flag for 5 level paging Ard Biesheuvel
2025-05-13 19:49 ` Linus Torvalds
2025-05-14 7:32 ` Kirill A. Shutemov
2025-05-14 8:04 ` Ingo Molnar
2025-05-14 8:14 ` Ard Biesheuvel
2025-05-14 8:21 ` Kirill A. Shutemov
2025-05-14 8:31 ` Ingo Molnar
2025-05-14 8:39 ` Ingo Molnar
2025-05-14 8:19 ` Kirill A. Shutemov
2025-05-14 8:33 ` Ingo Molnar
2025-05-13 11:12 ` [RFC PATCH v2 3/6] x86/cpu: Allow caps to be set arbitrarily early Ard Biesheuvel
2025-05-13 18:37 ` Brian Gerst
2025-05-14 8:17 ` Ingo Molnar
2025-05-14 9:49 ` Ard Biesheuvel
2025-05-21 13:22 ` Ard Biesheuvel
2025-05-13 11:12 ` Ard Biesheuvel [this message]
2025-05-14 8:15 ` [RFC PATCH v2 4/6] x86/boot: Set 5-level paging CPU cap before entering C code Ingo Molnar
2025-05-14 8:18 ` Ard Biesheuvel
2025-05-14 8:37 ` Ingo Molnar
2025-05-14 8:40 ` Ard Biesheuvel
2025-05-13 11:12 ` [RFC PATCH v2 5/6] x86/boot: Drop the early variant of pgtable_l5_enabled() Ard Biesheuvel
2025-05-13 11:12 ` [RFC PATCH v2 6/6] x86/boot: Drop 5-level paging related variables and early updates Ard Biesheuvel
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=20250513111157.717727-12-ardb+git@google.com \
--to=ardb+git@google.com \
--cc=ardb@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.