From: "Ahmed S. Darwish" <darwi@linutronix.de>
To: Borislav Petkov <bp@alien8.de>, Ingo Molnar <mingo@redhat.com>,
Dave Hansen <dave.hansen@linux.intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>,
Andrew Cooper <andrew.cooper3@citrix.com>,
"H. Peter Anvin" <hpa@zytor.com>,
David Woodhouse <dwmw2@infradead.org>,
Sean Christopherson <seanjc@google.com>,
Peter Zijlstra <peterz@infradead.org>,
Sohil Mehta <sohil.mehta@intel.com>,
John Ogness <john.ogness@linutronix.de>,
x86@kernel.org, x86-cpuid@lists.linux.dev,
LKML <linux-kernel@vger.kernel.org>,
"Ahmed S. Darwish" <darwi@linutronix.de>
Subject: [PATCH v4 14/34] x86/cpuid: Introduce a CPUID leaf x86 vendor table
Date: Fri, 15 Aug 2025 09:02:07 +0200 [thread overview]
Message-ID: <20250815070227.19981-15-darwi@linutronix.de> (raw)
In-Reply-To: <20250815070227.19981-1-darwi@linutronix.de>
For the CPUID parser, introduce a table listing vendor-specific CPUID
leaves. Not all CPUID leaves should be queried on all x86 vendors, so the
parser will only enumerate such leaves if the boot machine's x86 vendor
is listed as supported.
This provides the following benefits:
(a) Even when a CPUID leaf falls within the CPU's standard or extended
maximum leaf range, querying architecturally unsupported and reserved
CPUID leaves may trigger new kernel boot behaviors or subtle bugs,
especially on legacy machines.
(b) Associating x86 vendor information with CPUID leaves will enable
the CPUID parser to emit (lightweight) error messages when malformed
CPUID leaf output is detected. This is due to the parser now being
more certain that the queried leaf is valid on the machine.
(c) Attaching x86 vendor information to CPUID leaves will relieve
call-sites, especially drivers, from ugly x86 vendor checks before
querying a CPUID leaf. If the CPUID parsers API like cpuid_leaf() or
cpuid_subleaf() return NULL, it willy simply implies the leaf is
simply unavailable (or should not be queried) on the current machine.
Split the CPUID parsing table into an "early boot" table and a standard
one. The early boot phase parses only CPUID(0x0) and CPUID(0x1), where
they will be needed to identify the CPU's x86 vendor.
Once the kernel saves the vendor info to the CPU's capability structure,
invoke the CPUID parser again to parse the rest of the CPUID leaves. In
that second phase, the parser assumes that "boot_cpu_data.x86_vendor" is
valid and uses it for CPUID leaf x86 vendor validity checks.
For each vendor-specific CPUID leaf, build its list of matching x86
vendors using CPP varargs. Encoding this as bitflags was not doable,
since the x86 vendor IDs are just raw monotonic numbers from 0 (Intel) to
11 (Vortex).
Keep the CPUID parser's vendor-specific leaf table empty for now. Leaves
like CPUID(0x2), CPUID(0x4), CPUID(0x16), and CPUID(0x8000001d) will be
added to the vendor table once their support is actually added to the
parser.
Signed-off-by: Ahmed S. Darwish <darwi@linutronix.de>
---
arch/x86/kernel/cpu/common.c | 3 +-
arch/x86/kernel/cpu/cpuid_parser.c | 108 ++++++++++++++++++++++++-----
arch/x86/kernel/cpu/cpuid_parser.h | 51 +++++++++++++-
3 files changed, 142 insertions(+), 20 deletions(-)
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 6b5a4dd2f33e..048b285e7741 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -1725,9 +1725,10 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)
/* cyrix could have cpuid enabled via c_identify()*/
if (cpuid_feature()) {
- cpuid_parser_scan_cpu(c);
+ cpuid_parser_early_scan_cpu(c);
cpu_detect(c);
get_cpu_vendor(c);
+ cpuid_parser_scan_cpu(c);
intel_unlock_cpuid_leafs(c);
get_cpu_cap(c);
setup_force_cpu_cap(X86_FEATURE_CPUID);
diff --git a/arch/x86/kernel/cpu/cpuid_parser.c b/arch/x86/kernel/cpu/cpuid_parser.c
index be9c8571f886..84d70a432212 100644
--- a/arch/x86/kernel/cpu/cpuid_parser.c
+++ b/arch/x86/kernel/cpu/cpuid_parser.c
@@ -12,6 +12,10 @@
#include "cpuid_parser.h"
+static const struct cpuid_vendor_entry cpuid_vendor_entries[] = {
+ CPUID_VENDOR_ENTRIES
+};
+
/*
* Leaf read functions:
*/
@@ -55,10 +59,24 @@ static void cpuid_read_0x80000000(const struct cpuid_parse_entry *e, struct cpui
*
* Since these tables reference the leaf read functions above, they must be
* defined afterwards.
+ *
+ * At early boot, only leaves at CPUID_EARLY_PARSE_ENTRIES should be parsed.
*/
-static const struct cpuid_parse_entry cpuid_parse_entries[] = {
- CPUID_PARSE_ENTRIES
+static const struct cpuid_parse_entry cpuid_early_parse_entries[] = {
+ CPUID_EARLY_PARSE_ENTRIES
+};
+
+static const struct cpuid_parse_entry cpuid_common_parse_entries[] = {
+ CPUID_COMMON_PARSE_ENTRIES
+};
+
+static const struct {
+ const struct cpuid_parse_entry *table;
+ int nr_entries;
+} cpuid_parser_phases[] = {
+ { cpuid_early_parse_entries, ARRAY_SIZE(cpuid_early_parse_entries) },
+ { cpuid_common_parse_entries, ARRAY_SIZE(cpuid_common_parse_entries) },
};
/*
@@ -89,6 +107,32 @@ static bool cpuid_leaf_in_range(const struct cpuid_table *t, unsigned int leaf)
cpuid_range_valid(t, leaf, CPUID_EXT_START, CPUID_EXT_END);
}
+static bool cpuid_leaf_matches_vendor(unsigned int leaf, u8 cpu_vendor)
+{
+ const struct cpuid_parse_entry *p = cpuid_early_parse_entries;
+ const struct cpuid_vendor_entry *v = cpuid_vendor_entries;
+
+ /* Leaves in the early boot parser table are vendor agnostic */
+ for (int i = 0; i < ARRAY_SIZE(cpuid_early_parse_entries); i++, p++)
+ if (p->leaf == leaf)
+ return true;
+
+ /* Leaves in the vendor table must pass a CPU vendor check */
+ for (int i = 0; i < ARRAY_SIZE(cpuid_vendor_entries); i++, v++) {
+ if (v->leaf != leaf)
+ continue;
+
+ for (unsigned int j = 0; j < v->nvendors; j++)
+ if (cpu_vendor == v->vendors[j])
+ return true;
+
+ return false;
+ }
+
+ /* Remaining leaves are vendor agnostic */
+ return true;
+}
+
static void
cpuid_fill_table(struct cpuid_table *t, const struct cpuid_parse_entry entries[], unsigned int nr_entries)
{
@@ -103,28 +147,21 @@ cpuid_fill_table(struct cpuid_table *t, const struct cpuid_parse_entry entries[]
if (!cpuid_leaf_in_range(t, entry->leaf))
continue;
+ if (!cpuid_leaf_matches_vendor(entry->leaf, boot_cpu_data.x86_vendor))
+ continue;
+
WARN_ON_ONCE(output.info->nr_entries != 0);
entry->read(entry, &output);
}
}
-/*
- * Exported APIs:
- */
-
-/**
- * cpuid_parser_scan_cpu() - Populate current CPU's CPUID table
- * @c: CPU capability structure associated with the current CPU
- *
- * Populate the CPUID table embedded within @c with parsed CPUID data. Since all CPUID
- * instructions are invoked locally, this must be called on the CPU associated with @c.
- */
-void cpuid_parser_scan_cpu(struct cpuinfo_x86 *c)
+static void __cpuid_parser_scan_cpu(struct cpuinfo_x86 *c, bool early_boot)
{
+ int nphases = early_boot ? 1 : ARRAY_SIZE(cpuid_parser_phases);
struct cpuid_table *table = &c->cpuid;
/*
- * For correctness, clear the CPUID table first.
+ * After early boot, clear the CPUID table first.
*
* This is due to the CPUID parser APIs at <asm/cpuid/api.h> using leaf->nr_entries
* as a leaf validity check: non-zero means that the CPUID leaf's cached output is
@@ -140,7 +177,44 @@ void cpuid_parser_scan_cpu(struct cpuinfo_x86 *c)
* parsed (leaving stale leaf "nr_entries" fields behind.) The table must thus be
* also cleared.
*/
- memset(table, 0, sizeof(*table));
+ if (!early_boot)
+ memset(table, 0, sizeof(*table));
+
+ for (int i = 0; i < nphases; i++)
+ cpuid_fill_table(table, cpuid_parser_phases[i].table, cpuid_parser_phases[i].nr_entries);
+}
+
+/*
+ * Exported APIs:
+ */
+
+/**
+ * cpuid_parser_scan_cpu() - Populate the current CPU's CPUID table
+ * @c: CPU capability structure for the current CPU
+ *
+ * Populate the CPUID table embedded within @c with parsed CPUID data. Since all CPUID
+ * instructions are invoked locally, this must be run on the CPU associated with @c.
+ *
+ * cpuid_parser_early_scan_cpu() must've been called, at least once, beforehand.
+ */
+void cpuid_parser_scan_cpu(struct cpuinfo_x86 *c)
+{
+ __cpuid_parser_scan_cpu(c, false);
+}
- cpuid_fill_table(table, cpuid_parse_entries, ARRAY_SIZE(cpuid_common_parse_entries));
+/**
+ * cpuid_parser_early_scan_cpu() - Populate primary CPU's CPUID table on early boot
+ * @c: CPU capability structure associated with the current CPU
+ *
+ * Populate the CPUID table embedded within @c with parsed CPUID data.
+ *
+ * This must be called at early boot, so that the boot code can identify the CPU's
+ * x86 vendor. Only CPUID(0x0) and CPUID(0x1) are parsed.
+ *
+ * After saving the x86 vendor info in the boot CPU's capability structure,
+ * cpuid_parser_scan_cpu() must be called to complete the CPU's CPUID table.
+ */
+void __init cpuid_parser_early_scan_cpu(struct cpuinfo_x86 *c)
+{
+ __cpuid_parser_scan_cpu(c, true);
}
diff --git a/arch/x86/kernel/cpu/cpuid_parser.h b/arch/x86/kernel/cpu/cpuid_parser.h
index c2b78badd67e..5d7a05e4b9cd 100644
--- a/arch/x86/kernel/cpu/cpuid_parser.h
+++ b/arch/x86/kernel/cpu/cpuid_parser.h
@@ -96,10 +96,57 @@ struct cpuid_parse_entry {
* CPUID parser tables:
*/
-#define CPUID_PARSE_ENTRIES \
+/*
+ * Early-boot CPUID leaves (to be parsed before x86 vendor detection)
+ *
+ * These leaves must be parsed at early boot to identify the x86 vendor. The
+ * parser treats them as universally valid across all vendors.
+ *
+ * At early boot, only leaves in this table must be parsed. For all other
+ * leaves, the CPUID parser will assume that "boot_cpu_data.x86_vendor" is
+ * properly set beforehand.
+ *
+ * Note: If these entries are to be modified, please adapt the kernel-doc of
+ * cpuid_parser_early_scan_cpu() accordingly.
+ */
+#define CPUID_EARLY_PARSE_ENTRIES \
/* Leaf Subleaf Reader function */ \
CPUID_PARSE_ENTRY(0x0, 0, generic), \
CPUID_PARSE_ENTRY(0x1, 0, generic), \
- CPUID_PARSE_ENTRY(0x80000000, 0, 0x80000000),
+
+/*
+ * Common CPUID leaves
+ *
+ * These leaves can be parsed once basic x86 vendor detection is in place.
+ * Further vendor-agnostic leaves, which are not needed at early boot, are also
+ * listed here.
+ *
+ * For vendor-specific leaves, a matching entry must be added to the CPUID leaf
+ * vendor table later defined. Leaves which are here, but without a matching
+ * vendor entry, are treated by the CPUID parser as valid for all x86 vendors.
+ */
+#define CPUID_COMMON_PARSE_ENTRIES \
+ /* Leaf Subleaf Reader function */ \
+ CPUID_PARSE_ENTRY(0x80000000, 0, 0x80000000), \
+
+/*
+ * CPUID leaf vendor table:
+ */
+
+struct cpuid_vendor_entry {
+ unsigned int leaf;
+ u8 vendors[X86_VENDOR_NUM];
+ u8 nvendors;
+};
+
+#define CPUID_VENDOR_ENTRY(_leaf, ...) \
+ { \
+ .leaf = _leaf, \
+ .vendors = { __VA_ARGS__ }, \
+ .nvendors = (sizeof((u8[]){__VA_ARGS__})/sizeof(u8)), \
+ }
+
+#define CPUID_VENDOR_ENTRIES \
+ /* Leaf Vendor list */ \
#endif /* _ARCH_X86_CPUID_PARSER_H */
--
2.50.1
next prev parent reply other threads:[~2025-08-15 7:05 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-15 7:01 [PATCH v4 00/34] x86: Introduce a centralized CPUID data model Ahmed S. Darwish
2025-08-15 7:01 ` [PATCH v4 01/34] x86/cpuid: Remove transitional <asm/cpuid.h> header Ahmed S. Darwish
2025-08-15 10:11 ` [tip: x86/urgent] " tip-bot2 for Ahmed S. Darwish
2025-08-15 15:22 ` tip-bot2 for Ahmed S. Darwish
2025-08-15 7:01 ` [PATCH v4 02/34] ASoC: Intel: avs: Include CPUID header at file scope Ahmed S. Darwish
2025-08-18 14:56 ` Borislav Petkov
2025-08-18 15:14 ` Ahmed S. Darwish
2025-08-15 7:01 ` [PATCH v4 03/34] treewide: Explicitly include the x86 CPUID headers Ahmed S. Darwish
2025-08-15 7:01 ` [PATCH v4 04/34] x86/cpu: <asm/processor.h>: Do not include the CPUID API header Ahmed S. Darwish
2025-08-15 7:01 ` [PATCH v4 05/34] x86/cpuid: Rename cpuid_leaf()/cpuid_subleaf() APIs Ahmed S. Darwish
2025-08-15 7:01 ` [PATCH v4 06/34] x86/cpuid: Introduce <asm/cpuid/leaf_types.h> Ahmed S. Darwish
2025-08-25 14:18 ` Borislav Petkov
2025-08-25 14:56 ` Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 07/34] x86/cpuid: Introduce a centralized CPUID data model Ahmed S. Darwish
2025-08-15 15:34 ` Sean Christopherson
2025-08-18 13:49 ` Ahmed S. Darwish
2025-08-18 18:44 ` Sean Christopherson
2025-08-18 19:54 ` Ahmed S. Darwish
2025-08-18 21:29 ` Sean Christopherson
2025-08-19 13:10 ` Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 08/34] x86/cpuid: Introduce a centralized CPUID parser Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 09/34] x86/cpu: Use parsed CPUID(0x0) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 10/34] x86/lib: Add CPUID(0x1) CPU family and model calculation Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 11/34] x86/cpu: Use parsed CPUID(0x1) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 12/34] x86/cpuid: Parse CPUID(0x80000000) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 13/34] x86/cpu: Use parsed CPUID(0x80000000) Ahmed S. Darwish
2025-08-15 7:02 ` Ahmed S. Darwish [this message]
2025-08-15 7:02 ` [PATCH v4 15/34] x86/cpuid: Introduce CPUID parser debugfs interface Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 16/34] x86/cpuid: Parse CPUID(0x2) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 17/34] x86/cpuid: Warn once on invalid CPUID(0x2) iteration count Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 18/34] x86/cpuid: Introduce parsed CPUID(0x2) API Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 19/34] x86/cpu: Use parsed CPUID(0x2) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 20/34] x86/cacheinfo: " Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 21/34] x86/cpuid: Remove direct CPUID(0x2) query API Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 22/34] x86/cpuid: Parse 'deterministic cache parameters' CPUID leaves Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 23/34] x86/cacheinfo: Pass a 'struct cpuinfo_x86' refrence to CPUID(0x4) code Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 24/34] x86/cacheinfo: Use parsed CPUID(0x4) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 25/34] x86/cacheinfo: Use parsed CPUID(0x8000001d) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 26/34] x86/cpuid: Parse CPUID(0x80000005) and CPUID(0x80000006) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 27/34] x86/cacheinfo: Use auto-generated data types Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 28/34] x86/cacheinfo: Use parsed CPUID(0x80000005) and CPUID(0x80000006) Ahmed S. Darwish
2025-08-15 15:25 ` Dave Hansen
2025-08-18 15:38 ` Ahmed S. Darwish
2025-08-18 15:52 ` David Woodhouse
2025-08-18 17:00 ` Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 29/34] x86/amd_nb: Trickle down 'struct cpuinfo_x86' reference Ahmed S. Darwish
2025-08-18 2:54 ` kernel test robot
2025-08-18 14:47 ` Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 30/34] x86/cpuid: Use parsed CPUID(0x80000006) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 31/34] x86/cpu: Rescan CPUID table after PSN disable Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 32/34] x86/cpu: Rescan CPUID table after unlocking full CPUID range Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 33/34] x86/cpuid: Parse CPUID(0x16) Ahmed S. Darwish
2025-08-15 7:02 ` [PATCH v4 34/34] x86/tsc: Use parsed CPUID(0x16) Ahmed S. Darwish
2025-08-16 9:59 ` [PATCH v4 00/34] x86: Introduce a centralized CPUID data model David Woodhouse
2025-08-18 16:37 ` Ahmed S. Darwish
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=20250815070227.19981-15-darwi@linutronix.de \
--to=darwi@linutronix.de \
--cc=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=dwmw2@infradead.org \
--cc=hpa@zytor.com \
--cc=john.ogness@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=seanjc@google.com \
--cc=sohil.mehta@intel.com \
--cc=tglx@linutronix.de \
--cc=x86-cpuid@lists.linux.dev \
--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®