2 * This file is part of msrtool.
4 * Copyright (c) 2008 Peter Stuge <peter@stuge.se>
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.
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.
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
24 #include <sys/types.h>
33 static uint8_t cpu = DEFAULT_CPU;
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;
40 struct pci_access *pacc = NULL;
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 },
50 static struct sysdef allsystems[] = {
51 { "linux", "Linux with /dev/cpu/*/msr", linux_probe, linux_open, linux_close, linux_rdmsr },
52 { "darwin", "OS X with DirectIO", darwin_probe, darwin_open, darwin_close, darwin_rdmsr },
53 { "freebsd", "FreeBSD with /dev/cpuctl*", freebsd_probe, freebsd_open, freebsd_close, freebsd_rdmsr },
57 static void syntax(char *argv[]) {
58 printf("syntax: %s [-hvqrkl] [-c cpu] [-m system] [-t target ...]\n", argv[0]);
59 printf("\t [-i addr=hi[:]lo] | [-s file] | [-d [:]file] | addr...\n");
60 printf(" -h\t show this help text\n");
61 printf(" -v\t be verbose\n");
62 printf(" -q\t be quiet (overrides -v)\n");
63 printf(" -r\t include [Reserved] values\n");
64 printf(" -k\t list all known systems and targets\n");
65 printf(" -l\t list MSRs and bit fields for current target(s) (-kl for ALL targets!)\n");
66 printf(" -c\t access MSRs on the specified CPU, default=%d\n", DEFAULT_CPU);
67 printf(" -m\t force a system, e.g: -m linux\n");
68 printf(" -t\t force a target, can be used multiple times, e.g: -t geodelx -t cs5536\n");
69 printf(" -i\t immediate mode\n");
70 printf("\t decode hex addr=hi:lo for the target without reading hw value\n");
71 printf("\t e.g: -i 4c00000f=f2f100ff56960004\n");
72 printf(" -s\t stream mode\n");
73 printf("\t read one MSR address per line and append current hw value to the line\n");
74 printf("\t use the filename - for stdin/stdout\n");
75 printf("\t using -l -s ignores input and will output all MSRs with values\n");
76 printf(" -d\t diff mode\n");
77 printf("\t read one address and value per line and compare with current hw value,\n");
78 printf("\t printing differences to stdout. use the filename - to read from stdin\n");
79 printf("\t use :file or :- to reverse diff, normally hw values are considered new\n");
80 printf(" addr.. direct mode, read and decode values for the given MSR address(es)\n");
83 static void *add_target(const struct targetdef *t) {
85 tmp = realloc(targets, (targets_found + 2) * sizeof(struct targetdef *));
91 targets[targets_found++] = t;
92 targets[targets_found] = NULL;
96 int do_stream(const char *streamfn, uint8_t ignoreinput) {
97 char tmpfn[20], line[256];
102 FILE *fin = NULL, *fout = NULL;
103 uint32_t addr, linenum;
104 struct msr m = MSR1(0);
106 if (0 == strcmp(streamfn, "-")) {
111 if (NULL == (fin = fopen(streamfn, "r"))) {
115 if (snprintf(tmpfn, sizeof(tmpfn), "msrtoolXXXXXX") >= sizeof(tmpfn)) {
119 if (-1 == (fdout = mkstemp(tmpfn))) {
123 if (NULL == (fout = fdopen(fdout, "w"))) {
128 if (NULL == (fout = fopen(streamfn, "w"))) {
135 if (!sys->open(cpu, SYS_RDONLY))
138 for (tn = 0; tn < targets_found; tn++)
139 if (dumpmsrdefsvals(fout, targets[tn], cpu)) {
144 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
145 start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
146 if (1 == sscanf(line + start, "%8x", &addr)) {
147 if (!sys->rdmsr(cpu, addr, &m))
149 fprintf(fout, "0x%08x 0x%08x%08x\n", addr, m.hi, m.lo);
153 fprintf(fout, "%s", line);
155 if (NULL != strchr("\r\n", line[len - 1]))
157 if (NULL == fgets(line, sizeof(line), fin))
163 fprintf(stderr, "%s:%d: fgets: %s\n", streamfn, linenum, strerror(errno));
170 if (strcmp(streamfn, "-")) {
173 else if (!ignoreinput)
174 rename(tmpfn, streamfn);
182 int do_diff(const char *difffn) {
183 char tmpfn[20], line[512], *m1start;
185 int ret = 1, tmp, m1pos;
186 FILE *fin = NULL, *fout = stdout;
188 uint32_t addr, linenum;
189 struct msr m1 = MSR1(0), m2 = MSR1(0);
191 if (':' == difffn[0]) {
195 if (0 == strcmp(difffn, "-"))
197 else if (NULL == (fin = fopen(difffn, "r"))) {
202 if (!sys->open(cpu, SYS_RDONLY))
204 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
205 tmp = strncmp("0x", line, 2) ? 0 : 2;
206 if (sscanf(line + tmp, "%8x %n%*x", &addr, &m1pos) < 1)
208 m1start = line + tmp + m1pos;
209 for (len = strlen(m1start) - 1; NULL != strchr("\r\n", m1start[len]); --len)
211 if (!str2msr(m1start, &m1)) {
212 fprintf(stderr, "%s:%d: invalid MSR value '%s'\n", difffn, linenum, m1start);
215 if (!sys->rdmsr(cpu, addr, &m2))
217 if (diff_msr(fout, addr, rev ? m2 : m1, rev ? m1 : m2))
221 fprintf(stderr, "%s:%d: fgets: %s\n", difffn, linenum, strerror(errno));
226 if (strcmp(difffn, "-")) {
230 rename(tmpfn, difffn);
237 int main(int argc, char *argv[]) {
240 const struct sysdef *s;
241 const struct targetdef *t;
242 uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
244 const char *streamfn = NULL, *difffn = NULL;
245 struct msr msrval = MSR2(-1, -1);
246 while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
270 for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
271 if (!strcmp(s->name, optarg)) {
277 for (t = alltargets; !TARGET_ISEOT(*t); t++)
278 if (!strcmp(t->name, optarg)) {
285 addr = msraddrbyname(optarg);
286 optarg = strchr(optarg, '=');
287 if (NULL == optarg) {
288 fprintf(stderr, "missing value in -i argument!\n");
291 if (!str2msr(++optarg, &msrval))
292 fprintf(stderr, "invalid value in -i argument!\n");
304 printf_quiet("msrtool %s\n", VERSION);
308 fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n");
314 if (!sys && !input && !listknown)
315 for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
316 printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
317 if (!sys->probe(sys))
319 printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
324 for (tn = 0; tn < targets_found; tn++)
325 printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
327 for (t = alltargets; !TARGET_ISEOT(*t); t++) {
328 printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
331 printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
339 printf("Known systems:\n");
340 for (s = allsystems; s->name; s++)
341 printf("%s: %s\n", s->name, s->prettyname);
342 printf("\nKnown targets:\n");
343 for (t = alltargets; t->name; t++) {
344 if (listmsrs && alltargets != t)
346 printf("%s: %s\n", t->name, t->prettyname);
354 if (sys && !sys->name) {
355 fprintf(stderr, "Unable to detect the current operating system!\n");
356 fprintf(stderr, "On Linux, please do 'modprobe msr' and retry.\n");
357 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
358 fprintf(stderr, "\n");
361 if (!targets_found || !targets) {
362 fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
363 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
364 fprintf(stderr, "\n");
369 decodemsr(cpu, addr, msrval);
373 if (sys && !sys->name)
378 return do_stream(streamfn, 1);
379 for (tn = 0; tn < targets_found; tn++) {
382 dumpmsrdefs(targets[tn]);
389 return do_stream(streamfn, 0);
391 if (!sys->open(cpu, SYS_RDONLY))
395 ret = do_diff(difffn);
399 if (optind == argc) {
401 printf("\nNo mode or address(es) specified!\n");
405 for (; optind < argc; optind++) {
406 addr = msraddrbyname(argv[optind]);
407 if (!sys->rdmsr(cpu, addr, &msrval))
409 decodemsr(cpu, addr, msrval);