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 },
45 { "K8", "AMD K8 Family", k8_probe, k8_msrs },
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 },
55 static void syntax(char *argv[]) {
56 printf("syntax: %s [-hvqrkl] [-c cpu] [-m system] [-t target ...]\n", argv[0]);
57 printf("\t [-i addr=hi[:]lo] | [-s file] | [-d [:]file] | addr...\n");
58 printf(" -h\t show this help text\n");
59 printf(" -v\t be verbose\n");
60 printf(" -q\t be quiet (overrides -v)\n");
61 printf(" -r\t include [Reserved] values\n");
62 printf(" -k\t list all known systems and targets\n");
63 printf(" -l\t list MSRs and bit fields for current target(s) (-kl for ALL targets!)\n");
64 printf(" -c\t access MSRs on the specified CPU, default=%d\n", DEFAULT_CPU);
65 printf(" -m\t force a system, e.g: -m linux\n");
66 printf(" -t\t force a target, can be used multiple times, e.g: -t geodelx -t cs5536\n");
67 printf(" -i\t immediate mode\n");
68 printf("\t decode hex addr=hi:lo for the target without reading hw value\n");
69 printf("\t e.g: -i 4c00000f=f2f100ff56960004\n");
70 printf(" -s\t stream mode\n");
71 printf("\t read one MSR address per line and append current hw value to the line\n");
72 printf("\t use the filename - for stdin/stdout\n");
73 printf("\t using -l -s ignores input and will output all MSRs with values\n");
74 printf(" -d\t diff mode\n");
75 printf("\t read one address and value per line and compare with current hw value,\n");
76 printf("\t printing differences to stdout. use the filename - to read from stdin\n");
77 printf("\t use :file or :- to reverse diff, normally hw values are considered new\n");
78 printf(" addr.. direct mode, read and decode values for the given MSR address(es)\n");
81 static void *add_target(const struct targetdef *t) {
83 tmp = realloc(targets, (targets_found + 2) * sizeof(struct targetdef *));
89 targets[targets_found++] = t;
90 targets[targets_found] = NULL;
94 int do_stream(const char *streamfn, uint8_t ignoreinput) {
95 char tmpfn[20], line[256];
100 FILE *fin = NULL, *fout = NULL;
101 uint32_t addr, linenum;
102 struct msr m = MSR1(0);
104 if (0 == strcmp(streamfn, "-")) {
109 if (NULL == (fin = fopen(streamfn, "r"))) {
113 if (snprintf(tmpfn, sizeof(tmpfn), "msrtoolXXXXXX") >= sizeof(tmpfn)) {
117 if (-1 == (fdout = mkstemp(tmpfn))) {
121 if (NULL == (fout = fdopen(fdout, "w"))) {
126 if (NULL == (fout = fopen(streamfn, "w"))) {
133 if (!sys->open(cpu, SYS_RDONLY))
136 for (tn = 0; tn < targets_found; tn++)
137 if (dumpmsrdefsvals(fout, targets[tn], cpu)) {
142 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
143 start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
144 if (1 == sscanf(line + start, "%8x", &addr)) {
145 if (!sys->rdmsr(cpu, addr, &m))
147 fprintf(fout, "0x%08x 0x%08x%08x\n", addr, m.hi, m.lo);
151 fprintf(fout, "%s", line);
153 if (NULL != strchr("\r\n", line[len - 1]))
155 if (NULL == fgets(line, sizeof(line), fin))
161 fprintf(stderr, "%s:%d: fgets: %s\n", streamfn, linenum, strerror(errno));
168 if (strcmp(streamfn, "-")) {
171 else if (!ignoreinput)
172 rename(tmpfn, streamfn);
180 int do_diff(const char *difffn) {
181 char tmpfn[20], line[512];
184 FILE *fin = NULL, *fout = stdout;
186 uint32_t addr, linenum;
187 struct msr mf = MSR1(0), mhw = MSR1(0);
189 if (':' == difffn[0]) {
193 if (0 == strcmp(difffn, "-"))
195 else if (NULL == (fin = fopen(difffn, "r"))) {
200 if (!sys->open(cpu, SYS_RDONLY))
202 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
203 start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
204 if (sscanf(line + start, "%8x %n%*x", &addr, &tmp) >= 1) {
206 for (len = strlen(line) - 1; NULL != strchr("\r\n", line[len]); --len)
208 if (!str2msr(line + start, &mf)) {
209 fprintf(stderr, "%s:%d: invalid MSR value '%s'\n", difffn, linenum, line + start);
212 if (!sys->rdmsr(cpu, addr, &mhw))
214 if (diff_msr(fout, addr, rev ? mhw : mf, rev ? mf : mhw))
219 fprintf(stderr, "%s:%d: fgets: %s\n", difffn, linenum, strerror(errno));
224 if (strcmp(difffn, "-")) {
228 rename(tmpfn, difffn);
235 int main(int argc, char *argv[]) {
238 const struct sysdef *s;
239 const struct targetdef *t;
240 uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
242 const char *streamfn = NULL, *difffn = NULL;
243 struct msr msrval = MSR2(-1, -1);
244 while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
268 for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
269 if (!strcmp(s->name, optarg)) {
275 for (t = alltargets; !TARGET_ISEOT(*t); t++)
276 if (!strcmp(t->name, optarg)) {
283 addr = msraddrbyname(optarg);
284 optarg = strchr(optarg, '=');
285 if (NULL == optarg) {
286 fprintf(stderr, "missing value in -i argument!\n");
289 if (!str2msr(++optarg, &msrval))
290 fprintf(stderr, "invalid value in -i argument!\n");
302 printf_quiet("msrtool %s\n", VERSION);
306 fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n");
312 if (!sys && !input && !listknown)
313 for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
314 printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
315 if (!sys->probe(sys))
317 printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
322 for (tn = 0; tn < targets_found; tn++)
323 printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
325 for (t = alltargets; !TARGET_ISEOT(*t); t++) {
326 printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
329 printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
337 printf("Known systems:\n");
338 for (s = allsystems; s->name; s++)
339 printf("%s: %s\n", s->name, s->prettyname);
340 printf("\nKnown targets:\n");
341 for (t = alltargets; t->name; t++) {
342 if (listmsrs && alltargets != t)
344 printf("%s: %s\n", t->name, t->prettyname);
352 if (sys && !sys->name) {
353 fprintf(stderr, "Unable to detect the current operating system!\n");
354 fprintf(stderr, "On Linux, please do 'modprobe msr' and retry.\n");
355 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
356 fprintf(stderr, "\n");
359 if (!targets_found || !targets) {
360 fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
361 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
362 fprintf(stderr, "\n");
367 decodemsr(cpu, addr, msrval);
371 if (sys && !sys->name)
376 return do_stream(streamfn, 1);
377 for (tn = 0; tn < targets_found; tn++) {
380 dumpmsrdefs(targets[tn]);
387 return do_stream(streamfn, 0);
389 if (!sys->open(cpu, SYS_RDONLY))
393 ret = do_diff(difffn);
397 if (optind == argc) {
399 printf("\nNo mode or address(es) specified!\n");
403 for (; optind < argc; optind++) {
404 addr = msraddrbyname(argv[optind]);
405 if (!sys->rdmsr(cpu, addr, &msrval))
407 decodemsr(cpu, addr, msrval);