[PATCH v1 05/12] tools/x86/kcpuid: Refactor CPUID range handling for future expansion

From: Ahmed S. Darwish
Date: Thu Mar 06 2025 - 15:52:03 EST


The kcpuid code assumes only two CPUID index ranges, standard (0x0...)
and extended (0x80000000...).

Since additional CPUID index ranges will be added in further commits,
replace the "is_ext" boolean with enumeration-based range classification.

Collect all CPUID ranges in a structured array and introduce helper
macros to iterate over it. Use such helpers throughout the code.

Signed-off-by: Ahmed S. Darwish <darwi@xxxxxxxxxxxxx>
---
tools/arch/x86/kcpuid/kcpuid.c | 103 ++++++++++++++++++++-------------
1 file changed, 62 insertions(+), 41 deletions(-)

diff --git a/tools/arch/x86/kcpuid/kcpuid.c b/tools/arch/x86/kcpuid/kcpuid.c
index c5e18a397e07..6f6a394486af 100644
--- a/tools/arch/x86/kcpuid/kcpuid.c
+++ b/tools/arch/x86/kcpuid/kcpuid.c
@@ -65,19 +65,52 @@ struct cpuid_func {
int nr;
};

+enum range_index {
+ RANGE_STD = 0, /* Standard */
+ RANGE_EXT = 0x80000000, /* Extended */
+};
+
+#define CPUID_INDEX_MASK 0x80000000
+#define CPUID_FUNCTION_MASK (~CPUID_INDEX_MASK)
+
struct cpuid_range {
/* array of main leafs */
struct cpuid_func *funcs;
/* number of valid leafs */
int nr;
- bool is_ext;
+ enum range_index index;
};

-/*
- * basic: basic functions range: [0... ]
- * ext: extended functions range: [0x80000000... ]
- */
-struct cpuid_range *leafs_basic, *leafs_ext;
+static struct cpuid_range ranges[] = {
+ { .index = RANGE_STD, },
+ { .index = RANGE_EXT, },
+};
+
+static char *range_to_str(struct cpuid_range *range)
+{
+ switch (range->index) {
+ case RANGE_STD: return "Standard";
+ case RANGE_EXT: return "Extended";
+ default: return NULL;
+ }
+}
+
+#define for_each_cpuid_range(range) \
+ for (unsigned int i = 0; \
+ i < ARRAY_SIZE(ranges) && ((range) = &ranges[i]); \
+ i++)
+
+struct cpuid_range *index_to_cpuid_range(u32 index)
+{
+ struct cpuid_range *range;
+
+ for_each_cpuid_range(range) {
+ if (range->index == (index & CPUID_INDEX_MASK))
+ return range;
+ }
+
+ return NULL;
+}

static bool show_details;
static bool show_raw;
@@ -175,7 +208,7 @@ static void raw_dump_range(struct cpuid_range *range)
u32 f;
int i;

- printf("%s Leafs :\n", range->is_ext ? "Extended" : "Basic");
+ printf("%s Leafs :\n", range_to_str(range));
printf("================\n");

for (f = 0; (int)f < range->nr; f++) {
@@ -192,29 +225,19 @@ static void raw_dump_range(struct cpuid_range *range)
}

#define MAX_SUBLEAF_NUM 64
-struct cpuid_range *setup_cpuid_range(u32 input_eax)
+void setup_cpuid_range(struct cpuid_range *range)
{
u32 max_func, idx_func, subleaf, max_subleaf;
- u32 eax, ebx, ecx, edx, f = input_eax;
- struct cpuid_range *range;
+ u32 eax, ebx, ecx, edx, f;
bool allzero;

- eax = input_eax;
+ eax = f = range->index;
ebx = ecx = edx = 0;

cpuid(&eax, &ebx, &ecx, &edx);
max_func = eax;
idx_func = (max_func & 0xffff) + 1;

- range = malloc(sizeof(struct cpuid_range));
- if (!range)
- err(EXIT_FAILURE, NULL);
-
- if (input_eax & 0x80000000)
- range->is_ext = true;
- else
- range->is_ext = false;
-
range->funcs = malloc(sizeof(struct cpuid_func) * idx_func);
if (!range->funcs)
err(EXIT_FAILURE, NULL);
@@ -265,8 +288,6 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax)
}

}
-
- return range;
}

/*
@@ -325,13 +346,13 @@ static int parse_line(char *line)
/* index/main-leaf */
index = strtoull(tokens[0], NULL, 0);

- if (index & 0x80000000)
- range = leafs_ext;
- else
- range = leafs_basic;
+ /* Skip line parsing if it's not covered by known ranges */
+ range = index_to_cpuid_range(index);
+ if (!range)
+ return -1;

- index &= 0x7FFFFFFF;
/* Skip line parsing for non-existing indexes */
+ index &= CPUID_FUNCTION_MASK;
if ((int)index >= range->nr)
return -1;

@@ -521,24 +542,28 @@ static inline struct cpuid_func *index_to_func(u32 index)
struct cpuid_range *range;
u32 func_idx;

- range = (index & 0x80000000) ? leafs_ext : leafs_basic;
- func_idx = index & 0xffff;
+ range = index_to_cpuid_range(index);
+ if (!range)
+ return NULL;

+ func_idx = index & 0xffff;
if ((func_idx + 1) > (u32)range->nr) {
warnx("Invalid input index (0x%x)", index);
return NULL;
}
+
return &range->funcs[func_idx];
}

static void show_info(void)
{
+ struct cpuid_range *range;
struct cpuid_func *func;

if (show_raw) {
/* Show all of the raw output of 'cpuid' instr */
- raw_dump_range(leafs_basic);
- raw_dump_range(leafs_ext);
+ for_each_cpuid_range(range)
+ raw_dump_range(range);
return;
}

@@ -565,15 +590,8 @@ static void show_info(void)
}

printf("CPU features:\n=============\n\n");
- show_range(leafs_basic);
- show_range(leafs_ext);
-}
-
-static void setup_platform_cpuid(void)
-{
- /* Setup leafs for the basic and extended range */
- leafs_basic = setup_cpuid_range(0x0);
- leafs_ext = setup_cpuid_range(0x80000000);
+ for_each_cpuid_range(range)
+ show_range(range);
}

static void __noreturn usage(int exit_code)
@@ -649,10 +667,13 @@ static void parse_options(int argc, char *argv[])
*/
int main(int argc, char *argv[])
{
+ struct cpuid_range *range;
+
parse_options(argc, argv);

/* Setup the cpuid leafs of current platform */
- setup_platform_cpuid();
+ for_each_cpuid_range(range)
+ setup_cpuid_range(range);

/* Read and parse the 'cpuid.csv' */
parse_text();
--
2.48.1