1 // SPDX-License-Identifier: GPL-2.0
10 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
12 typedef unsigned int u32;
13 typedef unsigned long long u64;
15 char *def_csv = "/usr/share/misc/cpuid.csv";
19 /* Cover both single-bit flag and multiple-bits fields */
21 /* start and end bits */
23 /* 0 or 1 for 1-bit flag */
29 /* descriptor info for eax/ebx/ecx/edx */
31 /* number of valid entries */
33 struct bits_desc descs[32];
44 static const char * const reg_names[] = {
45 "EAX", "EBX", "ECX", "EDX",
51 u32 eax, ebx, ecx, edx;
52 struct reg_desc info[NR_REGS];
55 /* Represent one leaf (basic or extended) */
58 * Array of subleafs for this func, if there is no subleafs
59 * then the leafs[0] is the main leaf
61 struct subleaf *leafs;
66 /* array of main leafs */
67 struct cpuid_func *funcs;
68 /* number of valid leafs */
74 * basic: basic functions range: [0... ]
75 * ext: extended functions range: [0x80000000... ]
77 struct cpuid_range *leafs_basic, *leafs_ext;
81 static bool show_details;
83 static bool show_flags_only = true;
84 static u32 user_index = 0xFFFFFFFF;
85 static u32 user_sub = 0xFFFFFFFF;
88 static inline void cpuid(u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
90 /* ecx is often an input as well as an output. */
96 : "0" (*eax), "2" (*ecx));
99 static inline bool has_subleafs(u32 f)
101 if (f == 0x7 || f == 0xd)
124 static void leaf_print_raw(struct subleaf *leaf)
126 if (has_subleafs(leaf->index)) {
128 printf("0x%08x: subleafs:\n", leaf->index);
130 printf(" %2d: EAX=0x%08x, EBX=0x%08x, ECX=0x%08x, EDX=0x%08x\n",
131 leaf->sub, leaf->eax, leaf->ebx, leaf->ecx, leaf->edx);
133 printf("0x%08x: EAX=0x%08x, EBX=0x%08x, ECX=0x%08x, EDX=0x%08x\n",
134 leaf->index, leaf->eax, leaf->ebx, leaf->ecx, leaf->edx);
138 /* Return true is the input eax/ebx/ecx/edx are all zero */
139 static bool cpuid_store(struct cpuid_range *range, u32 f, int subleaf,
140 u32 a, u32 b, u32 c, u32 d)
142 struct cpuid_func *func;
143 struct subleaf *leaf;
146 if (a == 0 && b == 0 && c == 0 && d == 0)
150 * Cut off vendor-prefix from CPUID function as we're using it as an
151 * index into ->funcs.
153 func = &range->funcs[f & 0xffff];
156 func->leafs = malloc(sizeof(struct subleaf));
158 perror("malloc func leaf");
163 func->leafs = realloc(func->leafs, (s + 1) * sizeof(*leaf));
165 perror("realloc f->leafs");
170 leaf = &func->leafs[s];
182 static void raw_dump_range(struct cpuid_range *range)
187 printf("%s Leafs :\n", range->is_ext ? "Extended" : "Basic");
188 printf("================\n");
190 for (f = 0; (int)f < range->nr; f++) {
191 struct cpuid_func *func = &range->funcs[f];
197 /* Skip leaf without valid items */
201 /* First item is the main leaf, followed by all subleafs */
202 for (i = 0; i < func->nr; i++)
203 leaf_print_raw(&func->leafs[i]);
207 #define MAX_SUBLEAF_NUM 32
208 struct cpuid_range *setup_cpuid_range(u32 input_eax)
210 u32 max_func, idx_func;
212 struct cpuid_range *range;
213 u32 eax, ebx, ecx, edx;
221 cpuid(&eax, &ebx, &ecx, &edx);
223 idx_func = (max_func & 0xffff) + 1;
225 range = malloc(sizeof(struct cpuid_range));
227 perror("malloc range");
229 if (input_eax & 0x80000000)
230 range->is_ext = true;
232 range->is_ext = false;
234 range->funcs = malloc(sizeof(struct cpuid_func) * idx_func);
236 perror("malloc range->funcs");
238 range->nr = idx_func;
239 memset(range->funcs, 0, sizeof(struct cpuid_func) * idx_func);
241 for (; f <= max_func; f++) {
245 cpuid(&eax, &ebx, &ecx, &edx);
246 allzero = cpuid_store(range, f, subleaf, eax, ebx, ecx, edx);
251 if (!has_subleafs(f))
254 max_subleaf = MAX_SUBLEAF_NUM;
257 * Some can provide the exact number of subleafs,
258 * others have to be tried (0xf)
260 if (f == 0x7 || f == 0x14 || f == 0x17 || f == 0x18)
261 max_subleaf = (eax & 0xff) + 1;
266 for (subleaf = 1; subleaf < max_subleaf; subleaf++) {
270 cpuid(&eax, &ebx, &ecx, &edx);
271 allzero = cpuid_store(range, f, subleaf,
284 * The basic row format for cpuid.csv is
285 * LEAF,SUBLEAF,register_name,bits,short name,long description
288 * 0, 0, EAX, 31:0, max_basic_leafs, Max input value for supported subleafs
289 * 1, 0, ECX, 0, sse3, Streaming SIMD Extensions 3(SSE3)
291 static int parse_line(char *line)
295 struct cpuid_range *range;
296 struct cpuid_func *func;
297 struct subleaf *leaf;
312 struct reg_desc *reg;
313 struct bits_desc *bdesc;
317 /* Skip comments and NULL line */
318 if (line[0] == '#' || line[0] == '\n')
321 strncpy(buffer, line, 511);
324 for (i = 0; i < 5; i++) {
325 tokens[i] = strtok(str, ",");
330 tokens[5] = strtok(str, "\n");
334 /* index/main-leaf */
335 index = strtoull(tokens[0], NULL, 0);
337 if (index & 0x80000000)
343 /* Skip line parsing for non-existing indexes */
344 if ((int)index >= range->nr)
347 func = &range->funcs[index];
349 /* Return if the index has no valid item on this platform */
354 sub = strtoul(tokens[1], NULL, 0);
355 if ((int)sub > func->nr)
358 leaf = &func->leafs[sub];
361 if (strcasestr(buf, "EAX"))
363 else if (strcasestr(buf, "EBX"))
365 else if (strcasestr(buf, "ECX"))
367 else if (strcasestr(buf, "EDX"))
372 reg = &leaf->info[reg_index];
373 bdesc = ®->descs[reg->nr++];
375 /* bit flag or bits field */
378 end = strtok(buf, ":");
379 bdesc->end = strtoul(end, NULL, 0);
380 bdesc->start = bdesc->end;
382 /* start != NULL means it is bit fields */
383 start = strtok(NULL, ":");
385 bdesc->start = strtoul(start, NULL, 0);
387 strcpy(bdesc->simp, tokens[4]);
388 strcpy(bdesc->detail, tokens[5]);
392 printf("Warning: wrong line format:\n");
393 printf("\tline[%d]: %s\n", flines, line);
397 /* Parse csv file, and construct the array of all leafs and subleafs */
398 static void parse_text(void)
401 char *filename, *line = NULL;
408 filename = user_csv ? user_csv : def_csv;
409 file = fopen(filename, "r");
411 /* Fallback to a csv in the same dir */
412 file = fopen("./cpuid.csv", "r");
416 printf("Fail to open '%s'\n", filename);
421 ret = getline(&line, &len, file);
434 /* Decode every eax/ebx/ecx/edx */
435 static void decode_bits(u32 value, struct reg_desc *rdesc, enum cpuid_reg reg)
437 struct bits_desc *bdesc;
443 printf("\t %s: 0x%08x\n", reg_names[reg], value);
447 for (i = 0; i < rdesc->nr; i++) {
448 bdesc = &rdesc->descs[i];
450 start = bdesc->start;
453 /* single bit flag */
454 if (value & (1 << start))
455 printf("\t%-20s %s%s\n",
457 show_details ? "-" : "",
458 show_details ? bdesc->detail : ""
465 mask = ((u64)1 << (end - start + 1)) - 1;
466 printf("\t%-20s\t: 0x%-8x\t%s%s\n",
468 (value >> start) & mask,
469 show_details ? "-" : "",
470 show_details ? bdesc->detail : ""
476 static void show_leaf(struct subleaf *leaf)
482 leaf_print_raw(leaf);
485 printf("CPUID_0x%x_ECX[0x%x]:\n",
486 leaf->index, leaf->sub);
489 decode_bits(leaf->eax, &leaf->info[R_EAX], R_EAX);
490 decode_bits(leaf->ebx, &leaf->info[R_EBX], R_EBX);
491 decode_bits(leaf->ecx, &leaf->info[R_ECX], R_ECX);
492 decode_bits(leaf->edx, &leaf->info[R_EDX], R_EDX);
494 if (!show_raw && show_details)
498 static void show_func(struct cpuid_func *func)
505 for (i = 0; i < func->nr; i++)
506 show_leaf(&func->leafs[i]);
509 static void show_range(struct cpuid_range *range)
513 for (i = 0; i < range->nr; i++)
514 show_func(&range->funcs[i]);
517 static inline struct cpuid_func *index_to_func(u32 index)
519 struct cpuid_range *range;
522 range = (index & 0x80000000) ? leafs_ext : leafs_basic;
523 func_idx = index & 0xffff;
525 if ((func_idx + 1) > (u32)range->nr) {
526 printf("ERR: invalid input index (0x%x)\n", index);
529 return &range->funcs[func_idx];
532 static void show_info(void)
534 struct cpuid_func *func;
537 /* Show all of the raw output of 'cpuid' instr */
538 raw_dump_range(leafs_basic);
539 raw_dump_range(leafs_ext);
543 if (user_index != 0xFFFFFFFF) {
544 /* Only show specific leaf/subleaf info */
545 func = index_to_func(user_index);
549 /* Dump the raw data also */
552 if (user_sub != 0xFFFFFFFF) {
553 if (user_sub + 1 <= (u32)func->nr) {
554 show_leaf(&func->leafs[user_sub]);
558 printf("ERR: invalid input subleaf (0x%x)\n", user_sub);
565 printf("CPU features:\n=============\n\n");
566 show_range(leafs_basic);
567 show_range(leafs_ext);
570 static void setup_platform_cpuid(void)
572 u32 eax, ebx, ecx, edx;
575 eax = ebx = ecx = edx = 0;
576 cpuid(&eax, &ebx, &ecx, &edx);
579 if (ebx == 0x68747541)
582 /* Setup leafs for the basic and extended range */
583 leafs_basic = setup_cpuid_range(0x0);
584 leafs_ext = setup_cpuid_range(0x80000000);
587 static void usage(void)
589 printf("kcpuid [-abdfhr] [-l leaf] [-s subleaf]\n"
590 "\t-a|--all Show both bit flags and complex bit fields info\n"
591 "\t-b|--bitflags Show boolean flags only\n"
592 "\t-d|--detail Show details of the flag/fields (default)\n"
593 "\t-f|--flags Specify the cpuid csv file\n"
594 "\t-h|--help Show usage info\n"
595 "\t-l|--leaf=index Specify the leaf you want to check\n"
596 "\t-r|--raw Show raw cpuid data\n"
597 "\t-s|--subleaf=sub Specify the subleaf you want to check\n"
601 static struct option opts[] = {
602 { "all", no_argument, NULL, 'a' }, /* show both bit flags and fields */
603 { "bitflags", no_argument, NULL, 'b' }, /* only show bit flags, default on */
604 { "detail", no_argument, NULL, 'd' }, /* show detail descriptions */
605 { "file", required_argument, NULL, 'f' }, /* use user's cpuid file */
606 { "help", no_argument, NULL, 'h'}, /* show usage */
607 { "leaf", required_argument, NULL, 'l'}, /* only check a specific leaf */
608 { "raw", no_argument, NULL, 'r'}, /* show raw CPUID leaf data */
609 { "subleaf", required_argument, NULL, 's'}, /* check a specific subleaf */
613 static int parse_options(int argc, char *argv[])
617 while ((c = getopt_long(argc, argv, "abdf:hl:rs:",
621 show_flags_only = false;
624 show_flags_only = true;
638 user_index = strtoul(optarg, NULL, 0);
645 user_sub = strtoul(optarg, NULL, 0);
648 printf("%s: Invalid option '%c'\n", argv[0], optopt);
656 * Do 4 things in turn:
657 * 1. Parse user options
658 * 2. Parse and store all the CPUID leaf data supported on this platform
659 * 2. Parse the csv file, while skipping leafs which are not available
661 * 3. Print leafs info based on user options
663 int main(int argc, char *argv[])
665 if (parse_options(argc, argv))
668 /* Setup the cpuid leafs of current platform */
669 setup_platform_cpuid();
671 /* Read and parse the 'cpuid.csv' */