Adding upstream version 4.00~pre53+dfsg.
[syslinux-debian/hramrach.git] / com32 / sysdump / cpuid.c
blob372a70db6af216cc9ce3ad568f0ba81ea8d28be7
1 /*
2 * Dump CPUID information
3 */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <com32.h>
9 #include <sys/cpu.h>
10 #include "sysdump.h"
11 #include "backend.h"
13 struct cpuid_data {
14 uint32_t eax, ebx, ecx, edx;
17 struct cpuid_info {
18 uint32_t eax, ecx;
19 struct cpuid_data data;
22 static void get_cpuid(uint32_t eax, uint32_t ecx, struct cpuid_data *data)
24 asm("pushl %%ebx ; cpuid ; movl %%ebx,%1 ; popl %%ebx"
25 : "=a" (data->eax), "=r" (data->ebx),
26 "=c" (data->ecx), "=d" (data->edx)
27 : "a" (eax), "c" (ecx));
30 #define CPUID_CHUNK 128
32 void dump_cpuid(struct backend *be)
34 struct cpuid_info *buf = NULL;
35 int nentry, nalloc;
36 uint32_t region;
37 struct cpuid_data base_leaf;
38 uint32_t base, leaf, count;
39 struct cpuid_data invalid_leaf;
40 struct cpuid_data data;
42 if (!cpu_has_eflag(EFLAGS_ID))
43 return;
45 printf("Dumping CPUID... ");
47 nentry = nalloc = 0;
49 /* Find out what the CPU returns for invalid leaves */
50 get_cpuid(0, 0, &base_leaf);
51 get_cpuid(base_leaf.eax+1, 0, &invalid_leaf);
53 for (region = 0 ; region <= 0xffff ; region++) {
54 base = region << 16;
56 get_cpuid(base, 0, &base_leaf);
57 if (region && !memcmp(&base_leaf, &invalid_leaf, sizeof base_leaf))
58 continue;
60 if ((base_leaf.eax ^ base) & 0xffff0000)
61 continue;
63 for (leaf = base ; leaf <= base_leaf.eax ; leaf++) {
64 get_cpuid(leaf, 0, &data);
65 count = 0;
67 do {
68 if (nentry >= nalloc) {
69 nalloc += CPUID_CHUNK;
70 buf = realloc(buf, nalloc*sizeof *buf);
71 if (!buf)
72 return; /* FAILED */
74 buf[nentry].eax = leaf;
75 buf[nentry].ecx = count;
76 buf[nentry].data = data;
77 nentry++;
78 count++;
80 get_cpuid(leaf, count, &data);
81 } while (memcmp(&data, &buf[nentry-1].data, sizeof data) &&
82 (data.eax | data.ebx | data.ecx | data.edx));
86 if (nentry)
87 cpio_writefile(be, "cpuid", buf, nentry*sizeof *buf);
88 free(buf);
90 printf("done.\n");