[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250506050437.10264-8-darwi@linutronix.de>
Date: Tue, 6 May 2025 07:04:18 +0200
From: "Ahmed S. Darwish" <darwi@...utronix.de>
To: Ingo Molnar <mingo@...hat.com>,
Borislav Petkov <bp@...en8.de>,
Dave Hansen <dave.hansen@...ux.intel.com>
Cc: Thomas Gleixner <tglx@...utronix.de>,
Andrew Cooper <andrew.cooper3@...rix.com>,
"H. Peter Anvin" <hpa@...or.com>,
John Ogness <john.ogness@...utronix.de>,
x86@...nel.org,
x86-cpuid@...ts.linux.dev,
LKML <linux-kernel@...r.kernel.org>,
"Ahmed S. Darwish" <darwi@...utronix.de>
Subject: [PATCH v1 07/26] x86/cpuid: Introduce debugfs 'x86/scanned_cpuid/[0-ncpus]'
Introduce the debugfs files 'x86/scanned_cpuid/[0-ncpus]' to dump the
scanned CPUID table for each CPU.
While dumping the tables, for each cached CPUID leaf/subleaf entry, run
the corresponding CPUID instruction on the target CPU. Compare the live
hardware output with the cached register values. If a cached register
differs, mark its cached value output entry with an asterisk.
This should help with tricky bug reports in the future, if/when the
scanned CPUID tables get (unexpectedly) out of sync with actual hardware
state. It also simplifies the development and testing of adding new
CPUID leaves and custom read functions to the CPUID scanner.
Note, add an extern 'cpuid_common_scan_entries[]' declaration to the
"cpuid_scanner.h" internal header to allow the debugfs code to access the
CPUID scan entries directly.
Signed-off-by: Ahmed S. Darwish <darwi@...utronix.de>
---
arch/x86/kernel/cpu/Makefile | 1 +
arch/x86/kernel/cpu/cpuid_debugfs.c | 98 +++++++++++++++++++++++++++++
arch/x86/kernel/cpu/cpuid_scanner.c | 6 +-
arch/x86/kernel/cpu/cpuid_scanner.h | 3 +
4 files changed, 106 insertions(+), 2 deletions(-)
create mode 100644 arch/x86/kernel/cpu/cpuid_debugfs.c
diff --git a/arch/x86/kernel/cpu/Makefile b/arch/x86/kernel/cpu/Makefile
index 994539fd0e17..eb9cd1dee58e 100644
--- a/arch/x86/kernel/cpu/Makefile
+++ b/arch/x86/kernel/cpu/Makefile
@@ -62,6 +62,7 @@ obj-$(CONFIG_HYPERVISOR_GUEST) += vmware.o hypervisor.o mshyperv.o
obj-$(CONFIG_ACRN_GUEST) += acrn.o
obj-$(CONFIG_DEBUG_FS) += debugfs.o
+obj-$(CONFIG_DEBUG_FS) += cpuid_debugfs.o
obj-$(CONFIG_X86_BUS_LOCK_DETECT) += bus_lock.o
diff --git a/arch/x86/kernel/cpu/cpuid_debugfs.c b/arch/x86/kernel/cpu/cpuid_debugfs.c
new file mode 100644
index 000000000000..f6ef93b0a403
--- /dev/null
+++ b/arch/x86/kernel/cpu/cpuid_debugfs.c
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * CPUID scanner debugfs entries: x86/scanned_cpuid/[0-ncpus]
+ *
+ * Dump each CPU's scanned CPUID table and compare cached values against
+ * current CPUID output. Mark changed entries with an asterisk.
+ */
+
+#include <linux/debugfs.h>
+#include <linux/init.h>
+#include <linux/smp.h>
+#include <linux/types.h>
+
+#include <asm/cpuid.h>
+#include <asm/cpuid/internal_api.h>
+#include <asm/percpu.h>
+#include <asm/processor.h>
+
+#include "cpuid_scanner.h"
+
+static void cpuid_this_cpu(void *info)
+{
+ struct cpuid_regs *regs = info;
+
+ __cpuid(®s->eax, ®s->ebx, ®s->ecx, ®s->edx);
+};
+
+static void
+cpuid_show_leaf(struct seq_file *m, uintptr_t cpu_id, const struct leaf_query_info *info,
+ const struct cpuid_regs *cached, const struct cpuid_scan_entry *entry)
+{
+ for (int j = 0; j < info->nr_entries; j++) {
+ u32 subleaf = entry->subleaf + j;
+ struct cpuid_regs regs = {
+ .eax = entry->leaf,
+ .ecx = subleaf,
+ };
+
+ smp_call_function_single(cpu_id, cpuid_this_cpu, ®s, true);
+
+ seq_printf(m, "Leaf 0x%08x, subleaf %u:\n", entry->leaf, subleaf);
+
+ seq_printf(m, "cached: EAX=0x%08x%s\tEBX=0x%08x%s\tECX=0x%08x%s\tEDX=0x%08x%s\n",
+ cached[j].eax, cached[j].eax == regs.eax ? "" : "*",
+ cached[j].ebx, cached[j].ebx == regs.ebx ? "" : "*",
+ cached[j].ecx, cached[j].ecx == regs.ecx ? "" : "*",
+ cached[j].edx, cached[j].edx == regs.edx ? "" : "*");
+ seq_printf(m, "actual: EAX=0x%08x\tEBX=0x%08x\tECX=0x%08x\tEDX=0x%08x\n",
+ regs.eax, regs.ebx, regs.ecx, regs.edx);
+ }
+}
+
+static int cpuid_debug_show(struct seq_file *m, void *p)
+{
+ uintptr_t cpu_id = (uintptr_t)m->private;
+ const struct cpuinfo_x86 *c = per_cpu_ptr(&cpu_info, cpu_id);
+ const struct cpuid_leaves *leaves = &c->cpuid_table.leaves;
+ const struct cpuid_scan_entry *entry = cpuid_common_scan_entries;
+
+ for (unsigned int i = 0; i < cpuid_common_scan_entries_size; i++, entry++) {
+ const struct leaf_query_info *info = cpuid_leaves_info_p(leaves, entry->info_offs);
+ const struct cpuid_regs *leaf = cpuid_leaves_leaf_p(leaves, entry->leaf_offs);
+
+ cpuid_show_leaf(m, cpu_id, info, leaf, entry);
+ }
+
+ return 0;
+}
+
+static int cpuid_debug_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, cpuid_debug_show, inode->i_private);
+}
+
+static const struct file_operations cpuid_ops = {
+ .open = cpuid_debug_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
+static __init int cpuid_init_debugfs(void)
+{
+ struct dentry *base, *dir;
+ uintptr_t cpu_id;
+ char cpu_name[24];
+
+ base = debugfs_create_dir("scanned_cpuid", arch_debugfs_dir);
+ dir = debugfs_create_dir("cpus", base);
+
+ for_each_possible_cpu(cpu_id) {
+ scnprintf(cpu_name, sizeof(cpu_name), "%lu", cpu_id);
+ debugfs_create_file(cpu_name, 0444, dir, (void *)cpu_id, &cpuid_ops);
+ }
+
+ return 0;
+}
+late_initcall(cpuid_init_debugfs);
diff --git a/arch/x86/kernel/cpu/cpuid_scanner.c b/arch/x86/kernel/cpu/cpuid_scanner.c
index ec45c441bde5..7200dd66939f 100644
--- a/arch/x86/kernel/cpu/cpuid_scanner.c
+++ b/arch/x86/kernel/cpu/cpuid_scanner.c
@@ -62,10 +62,12 @@ static bool cpuid_leaf_valid(const struct cpuid_leaves *l, unsigned int leaf)
cpuid_range_valid(l, leaf, CPUID_EXT_START, CPUID_EXT_END);
}
-static const struct cpuid_scan_entry cpuid_common_scan_entries[] = {
+const struct cpuid_scan_entry cpuid_common_scan_entries[] = {
CPUID_SCAN_ENTRIES
};
+const int cpuid_common_scan_entries_size = ARRAY_SIZE(cpuid_common_scan_entries);
+
static void cpuid_scan(const struct cpuid_scan_info *info)
{
const struct cpuid_scan_entry *entry = info->entries;
@@ -97,7 +99,7 @@ void cpuid_scan_cpu(struct cpuinfo_x86 *c)
const struct cpuid_scan_info info = {
.cpuid_table = &c->cpuid_table,
.entries = cpuid_common_scan_entries,
- .nr_entries = ARRAY_SIZE(cpuid_common_scan_entries),
+ .nr_entries = cpuid_common_scan_entries_size,
};
cpuid_scan(&info);
diff --git a/arch/x86/kernel/cpu/cpuid_scanner.h b/arch/x86/kernel/cpu/cpuid_scanner.h
index b3e4676ab7f7..02bb223b406a 100644
--- a/arch/x86/kernel/cpu/cpuid_scanner.h
+++ b/arch/x86/kernel/cpu/cpuid_scanner.h
@@ -106,4 +106,7 @@ struct cpuid_scan_info {
unsigned int nr_entries;
};
+extern const struct cpuid_scan_entry cpuid_common_scan_entries[];
+extern const int cpuid_common_scan_entries_size;
+
#endif /* _ARCH_X86_CPUID_SCANNER_H */
--
2.49.0
Powered by blists - more mailing lists