msrtool: added support for Intel CPUs
[coreboot.git] / util / msrtool / msrtool.c
1 /*
2  * This file is part of msrtool.
3  *
4  * Copyright (c) 2008 Peter Stuge <peter@stuge.se>
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 version 2 as
8  * published by the Free Software Foundation.
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <pci/pci.h>
29
30 #include "msrtool.h"
31
32 #define DEFAULT_CPU 0
33 static uint8_t cpu = DEFAULT_CPU;
34
35 uint8_t targets_found = 0;
36 const struct targetdef **targets = NULL;
37 const struct sysdef *sys = NULL;
38 uint8_t reserved = 0, verbose = 0, quiet = 0;
39
40 struct pci_access *pacc = NULL;
41
42 static struct targetdef alltargets[] = {
43         { "geodegx2", "AMD Geode(tm) GX2", geodegx2_probe, geodegx2_msrs },
44         { "geodelx", "AMD Geode(tm) LX", geodelx_probe, geodelx_msrs },
45         { "cs5536", "AMD Geode(tm) CS5536", cs5536_probe, cs5536_msrs },
46         { "K8", "AMD K8 Family", k8_probe, k8_msrs },
47         { "intel_pentium3_early", "Intel Pentium III family", intel_pentium3_early_probe, intel_pentium3_early_msrs },
48         { "intel_pentium3", "Intel Pentium III Xeon Processor, Intel Pentium III Processor", intel_pentium3_probe, intel_pentium3_msrs },
49         { "intel_core1", "Intel Core Duo, Intel Core Solo processors", intel_core1_probe, intel_core1_msrs },
50         { "intel_core2_early", "Intel Xeon Processor 3000, 3200, 5100, 5300, 7300 series, Intel Core 2 Quad processor 6000 series, Intel Core 2 Extreme 6000    series, Intel Core 2 Duo 4000, 5000, 6000, 7000 series processors, Intel Pentium dual-core processors", intel_core2_early_probe, intel_core2_early_msrs },
51         { "intel_core2_later", "Intel Xeon Processor 5200, 5400 series, Intel Core 2 Quad       processors 8000, 9000 series", intel_core2_later_probe, intel_core2_later_msrs },
52         { "intel_pentium4_early", "Intel Xeon Processor, Intel Xeon Processor MP, Intel Pentium 4 processors", intel_pentium4_early_probe, intel_pentium4_early_msrs },
53         { "intel_pentium4_later", "Intel Xeon Processor, Intel Xeon Processor MP, Intel Pentium 4, Pentium D processors", intel_pentium4_later_probe, intel_pentium4_later_msrs },
54         { TARGET_EOT }
55 };
56
57 static struct sysdef allsystems[] = {
58         { "linux", "Linux with /dev/cpu/*/msr", linux_probe, linux_open, linux_close, linux_rdmsr },
59         { "darwin", "Mac OS X with DirectHW", darwin_probe, darwin_open, darwin_close, darwin_rdmsr },
60         { "freebsd", "FreeBSD with /dev/cpuctl*", freebsd_probe, freebsd_open, freebsd_close, freebsd_rdmsr },
61         { SYSTEM_EOT }
62 };
63
64 static void syntax(char *argv[]) {
65         printf("syntax: %s [-hvqrkl] [-c cpu] [-m system] [-t target ...]\n", argv[0]);
66         printf("\t [-i addr=hi[:]lo] | [-s file] | [-d [:]file] | addr...\n");
67         printf("  -h\t show this help text\n");
68         printf("  -v\t be verbose\n");
69         printf("  -q\t be quiet (overrides -v)\n");
70         printf("  -r\t include [Reserved] values\n");
71         printf("  -k\t list all known systems and targets\n");
72         printf("  -l\t list MSRs and bit fields for current target(s) (-kl for ALL targets!)\n");
73         printf("  -c\t access MSRs on the specified CPU, default=%d\n", DEFAULT_CPU);
74         printf("  -m\t force a system, e.g: -m linux\n");
75         printf("  -t\t force a target, can be used multiple times, e.g: -t geodelx -t cs5536\n");
76         printf("  -i\t immediate mode\n");
77         printf("\t decode hex addr=hi:lo for the target without reading hw value\n");
78         printf("\t e.g: -i 4c00000f=f2f100ff56960004\n");
79         printf("  -s\t stream mode\n");
80         printf("\t read one MSR address per line and append current hw value to the line\n");
81         printf("\t use the filename - for stdin/stdout\n");
82         printf("\t using -l -s ignores input and will output all MSRs with values\n");
83         printf("  -d\t diff mode\n");
84         printf("\t read one address and value per line and compare with current hw value,\n");
85         printf("\t printing differences to stdout. use the filename - to read from stdin\n");
86         printf("\t use :file or :- to reverse diff, normally hw values are considered new\n");
87         printf("  addr.. direct mode, read and decode values for the given MSR address(es)\n");
88 }
89
90 static void *add_target(const struct targetdef *t) {
91         void *tmp;
92         tmp = realloc(targets, (targets_found + 2) * sizeof(struct targetdef *));
93         if (NULL == tmp) {
94                 perror("realloc");
95                 return tmp;
96         }
97         targets = tmp;
98         targets[targets_found++] = t;
99         targets[targets_found] = NULL;
100         return targets;
101 }
102
103 static int found_system() {
104         if (!sys || (sys && !sys->name)) {
105                 fprintf(stderr, "Unable to detect the current operating system!\n");
106                 fprintf(stderr, "On Linux, please run 'modprobe msr' and try again.\n");
107                 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
108                 fprintf(stderr, "\n");
109         }
110         return (sys && sys->name);
111 }
112
113 int do_stream(const char *streamfn, uint8_t ignoreinput) {
114         char tmpfn[20], line[256];
115         uint8_t tn;
116         size_t start, len;
117         int ret = 1;
118         int fdout = -1;
119         FILE *fin = NULL, *fout = NULL;
120         uint32_t addr, linenum;
121         struct msr m = MSR1(0);
122
123         if (0 == strcmp(streamfn, "-")) {
124                 fin = stdin;
125                 fout = stdout;
126         } else {
127                 if (!ignoreinput) {
128                         if (NULL == (fin = fopen(streamfn, "r"))) {
129                                 perror("fopen()");
130                                 return 1;
131                         }
132                         if (snprintf(tmpfn, sizeof(tmpfn), "msrtoolXXXXXX") >= sizeof(tmpfn)) {
133                                 perror("snprintf");
134                                 return 1;
135                         }
136                         if (-1 == (fdout = mkstemp(tmpfn))) {
137                                 perror("mkstemp");
138                                 return 1;
139                         }
140                         if (NULL == (fout = fdopen(fdout, "w"))) {
141                                 perror("fdopen");
142                                 return 1;
143                         }
144                 } else {
145                         if (NULL == (fout = fopen(streamfn, "w"))) {
146                                 perror("fopen");
147                                 return 1;
148                         }
149                 }
150         }
151
152         if (!found_system())
153                 goto done;
154         if (!sys->open(cpu, SYS_RDONLY))
155                 goto done;
156         if (ignoreinput) {
157                 for (tn = 0; tn < targets_found; tn++)
158                         if (dumpmsrdefsvals(fout, targets[tn], cpu)) {
159                                 ret = 1;
160                                 break;
161                         }
162         } else {
163                 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
164                         start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
165                         if (1 == sscanf(line + start, "%8x", &addr)) {
166                                 if (!sys->rdmsr(cpu, addr, &m))
167                                         goto done;
168                                 fprintf(fout, "0x%08x 0x%08x%08x\n", addr, m.hi, m.lo);
169                                 continue;
170                         }
171                         while (1) {
172                                 fprintf(fout, "%s", line);
173                                 len = strlen(line);
174                                 if (NULL != strchr("\r\n", line[len - 1]))
175                                         break;
176                                 if (NULL == fgets(line, sizeof(line), fin))
177                                         goto read_done;
178                         }
179                 }
180 read_done:
181                 if (!feof(fin)) {
182                         fprintf(stderr, "%s:%d: fgets: %s\n", streamfn, linenum, strerror(errno));
183                         goto done;
184                 }
185         }
186         ret = 0;
187 done:
188         sys->close(cpu);
189         if (strcmp(streamfn, "-")) {
190                 if (ret)
191                         unlink(tmpfn);
192                 else if (!ignoreinput)
193                         rename(tmpfn, streamfn);
194         }
195         if (!ignoreinput)
196                 fclose(fin);
197         fclose(fout);
198         return ret;
199 }
200
201 int do_diff(const char *difffn) {
202         char tmpfn[20], line[512], *m1start, *m2start;
203         size_t len;
204         int ret = 1, tmp, m1pos, sys_opened = 0;
205         FILE *fin = NULL, *fout = stdout;
206         uint8_t rev = 0;
207         uint32_t addr, linenum;
208         struct msr m1 = MSR1(0), m2 = MSR1(0);
209
210         if (':' == difffn[0]) {
211                 rev = 1;
212                 ++difffn;
213         }
214         if (0 == strcmp(difffn, "-"))
215                 fin = stdin;
216         else if (NULL == (fin = fopen(difffn, "r"))) {
217                 perror("fopen()");
218                 return 1;
219         }
220
221         for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
222                 tmp = strncmp("0x", line, 2) ? 0 : 2;
223                 if (sscanf(line + tmp, "%8x %n%*x", &addr, &m1pos) < 1)
224                         continue;
225                 m1start = line + tmp + m1pos;
226                 for (len = strlen(m1start) - 1; NULL != strchr("\r\n", m1start[len]); --len)
227                         m1start[len] = 0;
228                 if (!str2msr(m1start, &m1, &m2start)) {
229                         fprintf(stderr, "%s:%d: invalid MSR1 value '%s'\n", difffn, linenum, m1start);
230                         continue;
231                 }
232                 while (' ' == *m2start)
233                         ++m2start;
234                 if (!str2msr(m2start, &m2, NULL)) {
235                         fprintf(stderr, "%s:%d: invalid MSR2 value '%s' - reading from hardware!\n", difffn, linenum, m2start);
236                         if (!sys_opened) {
237                                 if (!found_system())
238                                         goto done;
239                                 sys_opened = sys->open(cpu, SYS_RDONLY);
240                                 if (!sys_opened)
241                                         goto done;
242                         }
243                         if (!sys->rdmsr(cpu, addr, &m2))
244                                 goto done;
245                 }
246                 if (diff_msr(fout, addr, rev ? m2 : m1, rev ? m1 : m2))
247                         fprintf(fout, "\n");
248         }
249         if (!feof(fin))
250                 fprintf(stderr, "%s:%d: fgets: %s\n", difffn, linenum, strerror(errno));
251         else
252                 ret = 0;
253 done:
254         if (sys_opened)
255                 sys->close(cpu);
256         if (strcmp(difffn, "-")) {
257                 if (ret)
258                         unlink(tmpfn);
259                 else
260                         rename(tmpfn, difffn);
261                 fclose(fin);
262                 fclose(fout);
263         }
264         return ret;
265 }
266
267 int main(int argc, char *argv[]) {
268         char c;
269         int ret = 1;
270         const struct sysdef *s;
271         const struct targetdef *t;
272         uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
273         uint32_t addr = 0;
274         const char *streamfn = NULL, *difffn = NULL;
275         struct msr msrval = MSR2(-1, -1);
276         while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
277                 switch (c) {
278                 case 'h':
279                         syntax(argv);
280                         return 0;
281                 case 'q':
282                         quiet = 1;
283                         break;
284                 case 'v':
285                         ++verbose;
286                         break;
287                 case 'r':
288                         reserved = 1;
289                         break;
290                 case 'k':
291                         listknown = 1;
292                         break;
293                 case 'l':
294                         listmsrs = 1;
295                         break;
296                 case 'c':
297                         cpu = atoi(optarg);
298                         break;
299                 case 'm':
300                         for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
301                                 if (!strcmp(s->name, optarg)) {
302                                         sys = s;
303                                         break;
304                                 }
305                         break;
306                 case 't':
307                         for (t = alltargets; !TARGET_ISEOT(*t); t++)
308                                 if (!strcmp(t->name, optarg)) {
309                                         add_target(t);
310                                         break;
311                                 }
312                         break;
313                 case 'i':
314                         input = 1;
315                         addr = msraddrbyname(optarg);
316                         optarg = strchr(optarg, '=');
317                         if (NULL == optarg) {
318                                 fprintf(stderr, "missing value in -i argument!\n");
319                                 break;
320                         }
321                         if (!str2msr(++optarg, &msrval, NULL))
322                                 fprintf(stderr, "invalid value in -i argument!\n");
323                         break;
324                 case 's':
325                         streamfn = optarg;
326                         break;
327                 case 'd':
328                         difffn = optarg;
329                         break;
330                 default:
331                         break;
332                 }
333
334         printf_quiet("msrtool %s\n", VERSION);
335
336         pacc = pci_alloc();
337         if (NULL == pacc) {
338                 fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n");
339                 return 1;
340         }
341         pci_init(pacc);
342         pci_scan_bus(pacc);
343
344         if (!sys && !input && !listknown)
345                 for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
346                         printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
347                         if (!sys->probe(sys))
348                                 continue;
349                         printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
350                         break;
351                 }
352
353         if (targets)
354                 for (tn = 0; tn < targets_found; tn++)
355                         printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
356         else
357                 for (t = alltargets; !TARGET_ISEOT(*t); t++) {
358                         printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
359                         if (!t->probe(t))
360                                 continue;
361                         printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
362                         add_target(t);
363                 }
364
365         printf_quiet("\n");
366         fflush(stdout);
367
368         if (listknown) {
369                 printf("Known systems:\n");
370                 for (s = allsystems; s->name; s++)
371                         printf("%s: %s\n", s->name, s->prettyname);
372                 printf("\nKnown targets:\n");
373                 for (t = alltargets; t->name; t++) {
374                         if (listmsrs && alltargets != t)
375                                 printf("\n");
376                         printf("%s: %s\n", t->name, t->prettyname);
377                         if (listmsrs)
378                                 dumpmsrdefs(t);
379                 }
380                 printf("\n");
381                 return 0;
382         }
383
384         if (!targets_found || !targets) {
385                 fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
386                 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
387                 fprintf(stderr, "\n");
388                 return 1;
389         }
390
391         if (input) {
392                 decodemsr(cpu, addr, msrval);
393                 return 0;
394         }
395
396         if (listmsrs) {
397                 if (streamfn)
398                         return do_stream(streamfn, 1);
399                 for (tn = 0; tn < targets_found; tn++) {
400                         if (tn)
401                                 printf("\n");
402                         dumpmsrdefs(targets[tn]);
403                 }
404                 printf("\n");
405                 return 0;
406         }
407
408         if (streamfn)
409                 return do_stream(streamfn, 0);
410
411         if (difffn) {
412                 ret = do_diff(difffn);
413                 goto done;
414         }
415
416         if (optind == argc) {
417                 syntax(argv);
418                 printf("\nNo mode or address(es) specified!\n");
419                 goto done;
420         }
421
422         if (!found_system())
423                 return 1;
424         if (!sys->open(cpu, SYS_RDONLY))
425                 return 1;
426
427         for (; optind < argc; optind++) {
428                 addr = msraddrbyname(argv[optind]);
429                 if (!sys->rdmsr(cpu, addr, &msrval))
430                         break;
431                 decodemsr(cpu, addr, msrval);
432         }
433         ret = 0;
434 done:
435         sys->close(cpu);
436         return ret;
437 }