]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
objtool: Add Function to get the name of a CPU feature
authorAlexandre Chartre <alexandre.chartre@oracle.com>
Fri, 21 Nov 2025 09:53:36 +0000 (10:53 +0100)
committerPeter Zijlstra <peterz@infradead.org>
Mon, 24 Nov 2025 19:39:47 +0000 (20:39 +0100)
Add a function to get the name of a CPU feature. The function is
architecture dependent and currently only implemented for x86. The
feature names are automatically generated from the cpufeatures.h
include file.

Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
Link: https://patch.msgid.link/20251121095340.464045-27-alexandre.chartre@oracle.com
tools/arch/x86/tools/gen-cpu-feature-names-x86.awk [new file with mode: 0644]
tools/objtool/.gitignore
tools/objtool/Makefile
tools/objtool/arch/loongarch/special.c
tools/objtool/arch/powerpc/special.c
tools/objtool/arch/x86/Build
tools/objtool/arch/x86/special.c
tools/objtool/include/objtool/special.h

diff --git a/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk b/tools/arch/x86/tools/gen-cpu-feature-names-x86.awk
new file mode 100644 (file)
index 0000000..cc4c7a3
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/awk -f
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright (c) 2025, Oracle and/or its affiliates.
+#
+# Usage: awk -f gen-cpu-feature-names-x86.awk cpufeatures.h > cpu-feature-names.c
+#
+
+BEGIN {
+       print "/* cpu feature name array generated from cpufeatures.h */"
+       print "/* Do not change this code. */"
+       print
+       print "static const char *cpu_feature_names[(NCAPINTS+NBUGINTS)*32] = {"
+
+       value_expr = "\\([0-9*+ ]+\\)"
+}
+
+/^#define X86_FEATURE_/ {
+       if (match($0, value_expr)) {
+               value = substr($0, RSTART + 1, RLENGTH - 2)
+               print "\t[" value "] = \"" $2 "\","
+       }
+}
+
+/^#define X86_BUG_/ {
+       if (match($0, value_expr)) {
+               value = substr($0, RSTART + 1, RLENGTH - 2)
+               print "\t[NCAPINTS*32+(" value ")] = \"" $2 "\","
+       }
+}
+
+END {
+       print "};"
+}
index 759303657bd7c5b35f2747d85bc1a611bc9fe1e7..73d883128511f2c791649e546e6a29c206b27698 100644 (file)
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
+arch/x86/lib/cpu-feature-names.c
 arch/x86/lib/inat-tables.c
 /objtool
 feature
index df793ca6fc1a1e0d1b92dded559f5e8b44c9c617..66397d755fe4b6a35d9acfc7d0a9ca27ab319f5b 100644 (file)
@@ -125,6 +125,7 @@ $(LIBSUBCMD)-clean:
 clean: $(LIBSUBCMD)-clean
        $(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL)
        $(Q)find $(OUTPUT) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
+       $(Q)$(RM) $(OUTPUT)arch/x86/lib/cpu-feature-names.c $(OUTPUT)fixdep
        $(Q)$(RM) $(OUTPUT)arch/x86/lib/inat-tables.c $(OUTPUT)fixdep
        $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.objtool
        $(Q)$(RM) -r -- $(OUTPUT)feature
index a80b75f7b061f76ec789ef7a9f8c63f4aa6a7d9f..aba774109437fc11a3b9e64e347b5e2971347d60 100644 (file)
@@ -194,3 +194,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
 
        return rodata_reloc;
 }
+
+const char *arch_cpu_feature_name(int feature_number)
+{
+       return NULL;
+}
index 51610689abf72a8b1bb3ca2716aeb933653d8e91..8f9bf61ca089904ab0fea947062625686b9b79b3 100644 (file)
@@ -18,3 +18,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
 {
        exit(-1);
 }
+
+const char *arch_cpu_feature_name(int feature_number)
+{
+       return NULL;
+}
index 3dedb2fd8f3a0c3d7502e2536167f5e895fe1d44..febee0b8ee0b771a5ac6704f3fec90f23511064b 100644 (file)
@@ -1,5 +1,5 @@
-objtool-y += special.o
 objtool-y += decode.o
+objtool-y += special.o
 objtool-y += orc.o
 
 inat_tables_script = ../arch/x86/tools/gen-insn-attr-x86.awk
@@ -12,3 +12,14 @@ $(OUTPUT)arch/x86/lib/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
 $(OUTPUT)arch/x86/decode.o: $(OUTPUT)arch/x86/lib/inat-tables.c
 
 CFLAGS_decode.o += -I$(OUTPUT)arch/x86/lib
+
+cpu_features = ../arch/x86/include/asm/cpufeatures.h
+cpu_features_script = ../arch/x86/tools/gen-cpu-feature-names-x86.awk
+
+$(OUTPUT)arch/x86/lib/cpu-feature-names.c: $(cpu_features_script) $(cpu_features)
+       $(call rule_mkdir)
+       $(Q)$(call echo-cmd,gen)$(AWK) -f $(cpu_features_script) $(cpu_features) > $@
+
+$(OUTPUT)arch/x86/special.o: $(OUTPUT)arch/x86/lib/cpu-feature-names.c
+
+CFLAGS_special.o += -I$(OUTPUT)arch/x86/lib
index 09300761f1085bbde60185e5ac62edd026ccd467..e817a3fff449113c6b85aff092e1d3d6c0dc2289 100644 (file)
@@ -4,6 +4,10 @@
 #include <objtool/special.h>
 #include <objtool/builtin.h>
 #include <objtool/warn.h>
+#include <asm/cpufeatures.h>
+
+/* cpu feature name array generated from cpufeatures.h */
+#include "cpu-feature-names.c"
 
 void arch_handle_alternative(struct special_alt *alt)
 {
@@ -134,3 +138,9 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
        *table_size = 0;
        return rodata_reloc;
 }
+
+const char *arch_cpu_feature_name(int feature_number)
+{
+       return (feature_number < ARRAY_SIZE(cpu_feature_names)) ?
+               cpu_feature_names[feature_number] : NULL;
+}
index b22410745e4a1a42b58013ed46469e7324ef79b6..121c3761899c14d756aa0ab3bd5214ebb1129d41 100644 (file)
@@ -38,4 +38,6 @@ bool arch_support_alt_relocation(struct special_alt *special_alt,
 struct reloc *arch_find_switch_table(struct objtool_file *file,
                                     struct instruction *insn,
                                     unsigned long *table_size);
+const char *arch_cpu_feature_name(int feature_number);
+
 #endif /* _SPECIAL_H */