mini fix to reliably compile inteltool on darwin, and on Linux both on x86/x86_64.
[coreboot.git] / util / inteltool / cpu.c
1 /*
2  * inteltool - dump all registers on an Intel CPU + chipset based system.
3  *
4  * Copyright (C) 2008 by coresystems GmbH 
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "inteltool.h"
28
29 int fd_msr;
30
31 unsigned int cpuid(unsigned int op)
32 {
33         unsigned int ret;
34         unsigned int dummy2, dummy3, dummy4;
35 #if DARWIN
36         asm volatile ( 
37                 "pushl %%ebx    \n"
38                 "cpuid          \n"
39                 "movl %%ebx, %1 \n"
40                 "popl %%ebx     \n"
41                 : "=a" (ret), "=r" (dummy2), "=c" (dummy3), "=d" (dummy4)
42                 : "a" (op)
43                 : "cc"
44         );
45 #else
46         asm volatile ( 
47                 "cpuid"
48                 : "=a" (ret), "=b" (dummy2), "=c" (dummy3), "=d" (dummy4)
49                 : "a" (op)
50                 : "cc"
51         );
52 #endif
53         return ret;
54 }
55
56 #ifndef DARWIN
57 int msr_readerror = 0;
58
59 msr_t rdmsr(int addr)
60 {
61         uint8_t buf[8];
62         msr_t msr = { 0xffffffff, 0xffffffff };
63
64         if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
65                 perror("Could not lseek() to MSR");
66                 close(fd_msr);
67                 exit(1);
68         }
69
70         if (read(fd_msr, buf, 8) == 8) {
71                 msr.lo = *(uint32_t *)buf;
72                 msr.hi = *(uint32_t *)(buf + 4);
73
74                 return msr;
75         }
76
77         if (errno == 5) {
78                 printf(" (*)"); // Not all bits of the MSR could be read
79                 msr_readerror = 1;
80         } else {
81                 // A severe error.
82                 perror("Could not read() MSR");
83                 close(fd_msr);
84                 exit(1);
85         }
86
87         return msr;
88 }
89 #endif
90
91 int print_intel_core_msrs(void)
92 {
93         unsigned int i, core, id;
94         msr_t msr;
95
96 #define IA32_PLATFORM_ID                0x0017
97 #define EBL_CR_POWERON                  0x002a
98 #define FSB_CLK_STS                     0x00cd
99 #define IA32_TIME_STAMP_COUNTER         0x0010
100 #define IA32_APIC_BASE                  0x001b
101
102         typedef struct {
103                 int number;
104                 char *name;
105         } msr_entry_t;
106
107         static const msr_entry_t model6ex_global_msrs[] = {
108                 { 0x0017, "IA32_PLATFORM_ID" },
109                 { 0x002a, "EBL_CR_POWERON" },
110                 { 0x00cd, "FSB_CLOCK_STS" },
111                 { 0x00ce, "FSB_CLOCK_VCC" },
112                 { 0x00e2, "CLOCK_CST_CONFIG_CONTROL" },
113                 { 0x00e3, "PMG_IO_BASE_ADDR" },
114                 { 0x00e4, "PMG_IO_CAPTURE_ADDR" },
115                 { 0x00ee, "EXT_CONFIG" },
116                 { 0x011e, "BBL_CR_CTL3" },
117                 { 0x0194, "CLOCK_FLEX_MAX" },
118                 { 0x0198, "IA32_PERF_STATUS" },
119                 { 0x01a0, "IA32_MISC_ENABLES" },
120                 { 0x01aa, "PIC_SENS_CFG" },
121                 { 0x0400, "IA32_MC0_CTL" },
122                 { 0x0401, "IA32_MC0_STATUS" },
123                 { 0x0402, "IA32_MC0_ADDR" },
124                 //{ 0x0403, "IA32_MC0_MISC" }, // Seems to be RO
125                 { 0x040c, "IA32_MC4_CTL" },
126                 { 0x040d, "IA32_MC4_STATUS" },
127                 { 0x040e, "IA32_MC4_ADDR" },
128                 //{ 0x040f, "IA32_MC4_MISC" } // Seems to be RO
129         };
130
131         static const msr_entry_t model6ex_per_core_msrs[] = {
132                 { 0x0010, "IA32_TIME_STAMP_COUNTER" },
133                 { 0x001b, "IA32_APIC_BASE" },
134                 { 0x003a, "IA32_FEATURE_CONTROL" },
135                 { 0x003f, "IA32_TEMPERATURE_OFFSET" },
136                 //{ 0x0079, "IA32_BIOS_UPDT_TRIG" }, // Seems to be RO
137                 { 0x008b, "IA32_BIOS_SIGN_ID" },
138                 { 0x00e7, "IA32_MPERF" },
139                 { 0x00e8, "IA32_APERF" },
140                 { 0x00fe, "IA32_MTRRCAP" },
141                 { 0x015f, "DTS_CAL_CTRL" },
142                 { 0x0179, "IA32_MCG_CAP" },
143                 { 0x017a, "IA32_MCG_STATUS" },
144                 { 0x0199, "IA32_PERF_CONTROL" },
145                 { 0x019a, "IA32_CLOCK_MODULATION" },
146                 { 0x019b, "IA32_THERM_INTERRUPT" },
147                 { 0x019c, "IA32_THERM_STATUS" },
148                 { 0x019d, "GV_THERM" },
149                 { 0x01d9, "IA32_DEBUGCTL" },
150                 { 0x0200, "IA32_MTRR_PHYSBASE0" },
151                 { 0x0201, "IA32_MTRR_PHYSMASK0" },
152                 { 0x0202, "IA32_MTRR_PHYSBASE1" },
153                 { 0x0203, "IA32_MTRR_PHYSMASK1" },
154                 { 0x0204, "IA32_MTRR_PHYSBASE2" },
155                 { 0x0205, "IA32_MTRR_PHYSMASK2" },
156                 { 0x0206, "IA32_MTRR_PHYSBASE3" },
157                 { 0x0207, "IA32_MTRR_PHYSMASK3" },
158                 { 0x0208, "IA32_MTRR_PHYSBASE4" },
159                 { 0x0209, "IA32_MTRR_PHYSMASK4" },
160                 { 0x020a, "IA32_MTRR_PHYSBASE5" },
161                 { 0x020b, "IA32_MTRR_PHYSMASK5" },
162                 { 0x020c, "IA32_MTRR_PHYSBASE6" },
163                 { 0x020d, "IA32_MTRR_PHYSMASK6" },
164                 { 0x020e, "IA32_MTRR_PHYSBASE7" },
165                 { 0x020f, "IA32_MTRR_PHYSMASK7" },
166                 { 0x0250, "IA32_MTRR_FIX64K_00000" },
167                 { 0x0258, "IA32_MTRR_FIX16K_80000" },
168                 { 0x0259, "IA32_MTRR_FIX16K_A0000" },
169                 { 0x0268, "IA32_MTRR_FIX4K_C0000" },
170                 { 0x0269, "IA32_MTRR_FIX4K_C8000" },
171                 { 0x026a, "IA32_MTRR_FIX4K_D0000" },
172                 { 0x026b, "IA32_MTRR_FIX4K_D8000" },
173                 { 0x026c, "IA32_MTRR_FIX4K_E0000" },
174                 { 0x026d, "IA32_MTRR_FIX4K_E8000" },
175                 { 0x026e, "IA32_MTRR_FIX4K_F0000" },
176                 { 0x026f, "IA32_MTRR_FIX4K_F8000" },
177                 { 0x02ff, "IA32_MTRR_DEF_TYPE" },
178                 //{ 0x00c000080, "IA32_CR_EFER" }, // Seems to be RO
179         };
180
181         static const msr_entry_t model6fx_global_msrs[] = {
182                 { 0x0017, "IA32_PLATFORM_ID" },
183                 { 0x002a, "EBL_CR_POWERON" },
184                 { 0x003f, "IA32_TEMPERATURE_OFFSET" },
185                 { 0x00a8, "EMTTM_CR_TABLE0" },
186                 { 0x00a9, "EMTTM_CR_TABLE1" },
187                 { 0x00aa, "EMTTM_CR_TABLE2" },
188                 { 0x00ab, "EMTTM_CR_TABLE3" },
189                 { 0x00ac, "EMTTM_CR_TABLE4" },
190                 { 0x00ad, "EMTTM_CR_TABLE5" },
191                 { 0x00cd, "FSB_CLOCK_STS" },
192                 { 0x00e2, "PMG_CST_CONFIG_CONTROL" },
193                 { 0x00e3, "PMG_IO_BASE_ADDR" },
194                 { 0x00e4, "PMG_IO_CAPTURE_ADDR" },
195                 { 0x00ee, "EXT_CONFIG" },
196                 { 0x011e, "BBL_CR_CTL3" },
197                 { 0x0194, "CLOCK_FLEX_MAX" },
198                 { 0x0198, "IA32_PERF_STATUS" },
199                 { 0x01a0, "IA32_MISC_ENABLES" },
200                 { 0x01aa, "PIC_SENS_CFG" },
201                 { 0x0400, "IA32_MC0_CTL" },
202                 { 0x0401, "IA32_MC0_STATUS" },
203                 { 0x0402, "IA32_MC0_ADDR" },
204                 //{ 0x0403, "IA32_MC0_MISC" }, // Seems to be RO
205                 { 0x040c, "IA32_MC4_CTL" },
206                 { 0x040d, "IA32_MC4_STATUS" },
207                 { 0x040e, "IA32_MC4_ADDR" },
208                 //{ 0x040f, "IA32_MC4_MISC" } // Seems to be RO
209         };
210
211         static const msr_entry_t model6fx_per_core_msrs[] = {
212                 { 0x0010, "IA32_TIME_STAMP_COUNTER" },
213                 { 0x001b, "IA32_APIC_BASE" },
214                 { 0x003a, "IA32_FEATURE_CONTROL" },
215                 //{ 0x0079, "IA32_BIOS_UPDT_TRIG" }, // Seems to be RO
216                 { 0x008b, "IA32_BIOS_SIGN_ID" },
217                 { 0x00e1, "SMM_CST_MISC_INFO" },
218                 { 0x00e7, "IA32_MPERF" },
219                 { 0x00e8, "IA32_APERF" },
220                 { 0x00fe, "IA32_MTRRCAP" },
221                 { 0x0179, "IA32_MCG_CAP" },
222                 { 0x017a, "IA32_MCG_STATUS" },
223                 { 0x0199, "IA32_PERF_CONTROL" },
224                 { 0x019a, "IA32_THERM_CTL" },
225                 { 0x019b, "IA32_THERM_INTERRUPT" },
226                 { 0x019c, "IA32_THERM_STATUS" },
227                 { 0x019d, "MSR_THERM2_CTL" },
228                 { 0x01d9, "IA32_DEBUGCTL" },
229                 { 0x0200, "IA32_MTRR_PHYSBASE0" },
230                 { 0x0201, "IA32_MTRR_PHYSMASK0" },
231                 { 0x0202, "IA32_MTRR_PHYSBASE1" },
232                 { 0x0203, "IA32_MTRR_PHYSMASK1" },
233                 { 0x0204, "IA32_MTRR_PHYSBASE2" },
234                 { 0x0205, "IA32_MTRR_PHYSMASK2" },
235                 { 0x0206, "IA32_MTRR_PHYSBASE3" },
236                 { 0x0207, "IA32_MTRR_PHYSMASK3" },
237                 { 0x0208, "IA32_MTRR_PHYSBASE4" },
238                 { 0x0209, "IA32_MTRR_PHYSMASK4" },
239                 { 0x020a, "IA32_MTRR_PHYSBASE5" },
240                 { 0x020b, "IA32_MTRR_PHYSMASK5" },
241                 { 0x020c, "IA32_MTRR_PHYSBASE6" },
242                 { 0x020d, "IA32_MTRR_PHYSMASK6" },
243                 { 0x020e, "IA32_MTRR_PHYSBASE7" },
244                 { 0x020f, "IA32_MTRR_PHYSMASK7" },
245                 { 0x0250, "IA32_MTRR_FIX64K_00000" },
246                 { 0x0258, "IA32_MTRR_FIX16K_80000" },
247                 { 0x0259, "IA32_MTRR_FIX16K_A0000" },
248                 { 0x0268, "IA32_MTRR_FIX4K_C0000" },
249                 { 0x0269, "IA32_MTRR_FIX4K_C8000" },
250                 { 0x026a, "IA32_MTRR_FIX4K_D0000" },
251                 { 0x026b, "IA32_MTRR_FIX4K_D8000" },
252                 { 0x026c, "IA32_MTRR_FIX4K_E0000" },
253                 { 0x026d, "IA32_MTRR_FIX4K_E8000" },
254                 { 0x026e, "IA32_MTRR_FIX4K_F0000" },
255                 { 0x026f, "IA32_MTRR_FIX4K_F8000" },
256                 { 0x02ff, "IA32_MTRR_DEF_TYPE" },
257                 //{ 0x00c000080, "IA32_CR_EFER" }, // Seems to be RO
258         };
259
260         typedef struct {
261                 unsigned int model;
262                 const msr_entry_t *global_msrs;
263                 unsigned int num_global_msrs;
264                 const msr_entry_t *per_core_msrs;
265                 unsigned int num_per_core_msrs;
266         } cpu_t;
267
268         cpu_t cpulist[] = {
269                 { 0x006e0, model6ex_global_msrs, ARRAY_SIZE(model6ex_global_msrs), model6ex_per_core_msrs, ARRAY_SIZE(model6ex_per_core_msrs) },
270                 { 0x006f0, model6fx_global_msrs, ARRAY_SIZE(model6fx_global_msrs), model6fx_per_core_msrs, ARRAY_SIZE(model6fx_per_core_msrs) },
271         };
272
273         cpu_t *cpu = NULL;
274
275         /* Get CPU family and model, not the stepping 
276          * (TODO: extended family/model)
277          */
278         id = cpuid(1) & 0xff0;
279         for (i = 0; i < ARRAY_SIZE(cpulist); i++) {
280                 if(cpulist[i].model == id) {
281                         cpu = &cpulist[i];
282                         break;
283                 }
284         }
285
286         if (!cpu) {
287                 printf("Error: Dumping MSRs on this CPU (0x%06x) is not (yet) supported.\n", id);
288                 return -1;
289         }
290
291 #ifndef DARWIN
292         fd_msr = open("/dev/cpu/0/msr", O_RDWR);
293         if (fd_msr < 0) {
294                 perror("Error while opening /dev/cpu/0/msr");
295                 printf("Did you run 'modprobe msr'?\n");
296                 return -1;
297         }
298 #endif
299
300         printf("\n===================== SHARED MSRs (All Cores) =====================\n");
301
302         for (i = 0; i < cpu->num_global_msrs; i++) {
303                 msr = rdmsr(cpu->global_msrs[i].number);
304                 printf(" MSR 0x%08X = 0x%08X:0x%08X (%s)\n",
305                        cpu->global_msrs[i].number, msr.hi, msr.lo,
306                        cpu->global_msrs[i].name);
307         }
308
309         close(fd_msr);
310
311         for (core = 0; core < 8; core++) {
312 #ifndef DARWIN
313                 char msrfilename[64];
314                 memset(msrfilename, 0, 64);
315                 sprintf(msrfilename, "/dev/cpu/%d/msr", core);
316
317                 fd_msr = open(msrfilename, O_RDWR);
318
319                 /* If the file is not there, we're probably through. No error,
320                  * since we successfully opened /dev/cpu/0/msr before.
321                  */
322                 if (fd_msr < 0)
323                         break;
324 #endif
325                 printf("\n====================== UNIQUE MSRs  (core %d) ======================\n", core);
326
327                 for (i = 0; i < cpu->num_per_core_msrs; i++) {
328                         msr = rdmsr(cpu->per_core_msrs[i].number);
329                         printf(" MSR 0x%08X = 0x%08X:0x%08X (%s)\n",
330                                cpu->per_core_msrs[i].number, msr.hi, msr.lo,
331                                cpu->per_core_msrs[i].name);
332                 }
333 #ifndef DARWIN
334                 close(fd_msr);
335 #endif
336         }
337
338 #ifndef DARWIN
339         if (msr_readerror)
340                 printf("\n(*) Some MSRs could not be read. The marked values are unreliable.\n");
341 #endif
342         return 0;
343 }
344
345