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 { "geodelx", "AMD Geode(tm) LX", geodelx_probe, geodelx_msrs },
44 { "cs5536", "AMD Geode(tm) CS5536", cs5536_probe, cs5536_msrs },
48 static struct sysdef allsystems[] = {
49 { "linux", "Linux with /dev/cpu/*/msr", linux_probe, linux_open, linux_close, linux_rdmsr },
53 static void syntax(char *argv[]) {
54 printf("syntax: %s [-hvqrkl] [-c cpu] [-m system] [-t target ...]\n", argv[0]);
55 printf("\t [-i addr=hi[:]lo] | [-s file] | [-d [:]file] | addr...\n");
56 printf(" -h\t show this help text\n");
57 printf(" -v\t be verbose\n");
58 printf(" -q\t be quiet (overrides -v)\n");
59 printf(" -r\t include [Reserved] values\n");
60 printf(" -k\t list all known systems and targets\n");
61 printf(" -l\t list MSRs and bit fields for current target(s) (-kl for ALL targets!)\n");
62 printf(" -c\t access MSRs on the specified CPU, default=%d\n", DEFAULT_CPU);
63 printf(" -m\t force a system, e.g: -m linux\n");
64 printf(" -t\t force a target, can be used multiple times, e.g: -t geodelx -t cs5536\n");
65 printf(" -i\t immediate mode\n");
66 printf("\t decode hex addr=hi:lo for the target without reading hw value\n");
67 printf("\t e.g: -i 4c00000f=f2f100ff56960004\n");
68 printf(" -s\t stream mode\n");
69 printf("\t read one MSR address per line and append current hw value to the line\n");
70 printf("\t use the filename - for stdin/stdout\n");
71 printf("\t using -l -s ignores input and will output all MSRs with values\n");
72 printf(" -d\t diff mode\n");
73 printf("\t read one address and value per line and compare with current hw value,\n");
74 printf("\t printing differences to stdout. use the filename - to read from stdin\n");
75 printf("\t use :file or :- to reverse diff, normally hw values are considered new\n");
76 printf(" addr.. direct mode, read and decode values for the given MSR address(es)\n");
79 static void *add_target(const struct targetdef *t) {
81 tmp = realloc(targets, (targets_found + 2) * sizeof(struct targetdef *));
87 targets[targets_found++] = t;
88 targets[targets_found] = NULL;
92 int do_stream(const char *streamfn, uint8_t ignoreinput) {
93 char tmpfn[20], line[256];
98 FILE *fin = NULL, *fout = NULL;
99 uint32_t addr, linenum;
100 struct msr m = MSR1(0);
102 if (0 == strcmp(streamfn, "-")) {
107 if (NULL == (fin = fopen(streamfn, "r"))) {
111 if (snprintf(tmpfn, sizeof(tmpfn), "msrtoolXXXXXX") >= sizeof(tmpfn)) {
115 if (-1 == (fdout = mkstemp(tmpfn))) {
119 if (NULL == (fout = fdopen(fdout, "w"))) {
124 if (NULL == (fout = fopen(streamfn, "w"))) {
131 if (!sys->open(cpu, SYS_RDONLY))
134 for (tn = 0; tn < targets_found; tn++)
135 if (dumpmsrdefsvals(fout, targets[tn], cpu)) {
140 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
141 start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
142 if (1 == sscanf(line + start, "%8x", &addr)) {
143 if (!sys->rdmsr(cpu, addr, &m))
145 fprintf(fout, "0x%08x 0x%08x%08x\n", addr, m.hi, m.lo);
149 fprintf(fout, "%s", line);
151 if (NULL != strchr("\r\n", line[len - 1]))
153 if (NULL == fgets(line, sizeof(line), fin))
159 fprintf(stderr, "%s:%d: fgets: %s\n", streamfn, linenum, strerror(errno));
166 if (strcmp(streamfn, "-")) {
169 else if (!ignoreinput)
170 rename(tmpfn, streamfn);
178 int do_diff(const char *difffn) {
179 char tmpfn[20], line[512];
182 FILE *fin = NULL, *fout = stdout;
184 uint32_t addr, linenum;
185 struct msr mf = MSR1(0), mhw = MSR1(0);
187 if (':' == difffn[0]) {
191 if (0 == strcmp(difffn, "-"))
193 else if (NULL == (fin = fopen(difffn, "r"))) {
198 if (!sys->open(cpu, SYS_RDONLY))
200 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
201 start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
202 if (sscanf(line + start, "%8x %n%*x", &addr, &tmp) >= 1) {
204 for (len = strlen(line) - 1; NULL != strchr("\r\n", line[len]); --len)
206 if (!str2msr(line + start, &mf)) {
207 fprintf(stderr, "%s:%d: invalid MSR value '%s'\n", difffn, linenum, line + start);
210 if (!sys->rdmsr(cpu, addr, &mhw))
212 if (diff_msr(fout, addr, rev ? mhw : mf, rev ? mf : mhw))
217 fprintf(stderr, "%s:%d: fgets: %s\n", difffn, linenum, strerror(errno));
222 if (strcmp(difffn, "-")) {
226 rename(tmpfn, difffn);
233 int main(int argc, char *argv[]) {
236 const struct sysdef *s;
237 const struct targetdef *t;
238 uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
240 const char *streamfn = NULL, *difffn = NULL;
241 struct msr msrval = MSR2(-1, -1);
242 while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
266 for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
267 if (!strcmp(s->name, optarg)) {
273 for (t = alltargets; !TARGET_ISEOT(*t); t++)
274 if (!strcmp(t->name, optarg)) {
281 addr = strtoul(optarg, NULL, 16);
282 optarg = strchr(optarg, '=');
283 if (NULL == optarg) {
284 fprintf(stderr, "missing value in -i argument!\n");
287 if (!str2msr(++optarg, &msrval))
288 fprintf(stderr, "invalid value in -i argument!\n");
300 printf_quiet("msrtool %s\n", VERSION);
304 fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n");
310 if (!sys && !input && !listknown)
311 for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
312 printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
313 if (!sys->probe(sys))
315 printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
320 for (tn = 0; tn < targets_found; tn++)
321 printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
323 for (t = alltargets; !TARGET_ISEOT(*t); t++) {
324 printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
327 printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
335 printf("Known systems:\n");
336 for (s = allsystems; s->name; s++)
337 printf("%s: %s\n", s->name, s->prettyname);
338 printf("\nKnown targets:\n");
339 for (t = alltargets; t->name; t++) {
340 if (listmsrs && alltargets != t)
342 printf("%s: %s\n", t->name, t->prettyname);
350 if (sys && !sys->name) {
351 fprintf(stderr, "Unable to detect the current operating system!\n");
352 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
353 fprintf(stderr, "\n");
356 if (!targets_found || !targets) {
357 fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
358 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
359 fprintf(stderr, "\n");
364 decodemsr(cpu, addr, msrval);
368 if (sys && !sys->name)
373 return do_stream(streamfn, 1);
374 for (tn = 0; tn < targets_found; tn++) {
377 dumpmsrdefs(targets[tn]);
384 return do_stream(streamfn, 0);
386 if (!sys->open(cpu, SYS_RDONLY))
390 ret = do_diff(difffn);
394 if (optind == argc) {
396 printf("\nNo mode or address(es) specified!\n");
400 for (; optind < argc; optind++) {
401 addr = strtoul(argv[optind], NULL, 16);
402 if (!sys->rdmsr(cpu, addr, &msrval))
404 decodemsr(cpu, addr, msrval);