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