Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 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> |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 28 | #include <pci/pci.h> |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 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 | |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 40 | struct pci_access *pacc = NULL; |
| 41 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 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 }, |
Marc Jones | 8f21076 | 2009-03-08 04:37:39 +0000 | [diff] [blame] | 45 | { "K8", "AMD K8 Family", k8_probe, k8_msrs }, |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 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 }, |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame^] | 51 | { "darwin", "OS X with DirectIO", darwin_probe, darwin_open, darwin_close, darwin_rdmsr }, |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 52 | { SYSTEM_EOT } |
| 53 | }; |
| 54 | |
| 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"); |
| 79 | } |
| 80 | |
| 81 | static void *add_target(const struct targetdef *t) { |
| 82 | void *tmp; |
| 83 | tmp = realloc(targets, (targets_found + 2) * sizeof(struct targetdef *)); |
| 84 | if (NULL == tmp) { |
| 85 | perror("realloc"); |
| 86 | return tmp; |
| 87 | } |
| 88 | targets = tmp; |
| 89 | targets[targets_found++] = t; |
| 90 | targets[targets_found] = NULL; |
| 91 | return targets; |
| 92 | } |
| 93 | |
| 94 | int do_stream(const char *streamfn, uint8_t ignoreinput) { |
| 95 | char tmpfn[20], line[256]; |
| 96 | uint8_t tn; |
| 97 | size_t start, len; |
| 98 | int ret = 1; |
| 99 | int fdout = -1; |
| 100 | FILE *fin = NULL, *fout = NULL; |
| 101 | uint32_t addr, linenum; |
| 102 | struct msr m = MSR1(0); |
| 103 | |
| 104 | if (0 == strcmp(streamfn, "-")) { |
| 105 | fin = stdin; |
| 106 | fout = stdout; |
| 107 | } else { |
| 108 | if (!ignoreinput) { |
| 109 | if (NULL == (fin = fopen(streamfn, "r"))) { |
| 110 | perror("fopen()"); |
| 111 | return 1; |
| 112 | } |
| 113 | if (snprintf(tmpfn, sizeof(tmpfn), "msrtoolXXXXXX") >= sizeof(tmpfn)) { |
| 114 | perror("snprintf"); |
| 115 | return 1; |
| 116 | } |
| 117 | if (-1 == (fdout = mkstemp(tmpfn))) { |
| 118 | perror("mkstemp"); |
| 119 | return 1; |
| 120 | } |
| 121 | if (NULL == (fout = fdopen(fdout, "w"))) { |
| 122 | perror("fdopen"); |
| 123 | return 1; |
| 124 | } |
| 125 | } else { |
| 126 | if (NULL == (fout = fopen(streamfn, "w"))) { |
| 127 | perror("fopen"); |
| 128 | return 1; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (!sys->open(cpu, SYS_RDONLY)) |
| 134 | goto done; |
| 135 | if (ignoreinput) { |
| 136 | for (tn = 0; tn < targets_found; tn++) |
| 137 | if (dumpmsrdefsvals(fout, targets[tn], cpu)) { |
| 138 | ret = 1; |
| 139 | break; |
| 140 | } |
| 141 | } else { |
| 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)) |
| 146 | goto done; |
| 147 | fprintf(fout, "0x%08x 0x%08x%08x\n", addr, m.hi, m.lo); |
| 148 | continue; |
| 149 | } |
| 150 | while (1) { |
| 151 | fprintf(fout, "%s", line); |
| 152 | len = strlen(line); |
| 153 | if (NULL != strchr("\r\n", line[len - 1])) |
| 154 | break; |
| 155 | if (NULL == fgets(line, sizeof(line), fin)) |
| 156 | goto read_done; |
| 157 | } |
| 158 | } |
| 159 | read_done: |
| 160 | if (!feof(fin)) { |
| 161 | fprintf(stderr, "%s:%d: fgets: %s\n", streamfn, linenum, strerror(errno)); |
| 162 | goto done; |
| 163 | } |
| 164 | } |
| 165 | ret = 0; |
| 166 | done: |
| 167 | sys->close(cpu); |
| 168 | if (strcmp(streamfn, "-")) { |
| 169 | if (ret) |
| 170 | unlink(tmpfn); |
| 171 | else if (!ignoreinput) |
| 172 | rename(tmpfn, streamfn); |
| 173 | } |
| 174 | if (!ignoreinput) |
| 175 | fclose(fin); |
| 176 | fclose(fout); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | int do_diff(const char *difffn) { |
| 181 | char tmpfn[20], line[512]; |
| 182 | size_t start, len; |
| 183 | int ret = 1, tmp; |
| 184 | FILE *fin = NULL, *fout = stdout; |
| 185 | uint8_t rev = 0; |
| 186 | uint32_t addr, linenum; |
| 187 | struct msr mf = MSR1(0), mhw = MSR1(0); |
| 188 | |
| 189 | if (':' == difffn[0]) { |
| 190 | rev = 1; |
| 191 | ++difffn; |
| 192 | } |
| 193 | if (0 == strcmp(difffn, "-")) |
| 194 | fin = stdin; |
| 195 | else if (NULL == (fin = fopen(difffn, "r"))) { |
| 196 | perror("fopen()"); |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | if (!sys->open(cpu, SYS_RDONLY)) |
| 201 | goto done; |
| 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) { |
| 205 | start += tmp; |
| 206 | for (len = strlen(line) - 1; NULL != strchr("\r\n", line[len]); --len) |
| 207 | line[len] = 0; |
| 208 | if (!str2msr(line + start, &mf)) { |
| 209 | fprintf(stderr, "%s:%d: invalid MSR value '%s'\n", difffn, linenum, line + start); |
| 210 | continue; |
| 211 | } |
| 212 | if (!sys->rdmsr(cpu, addr, &mhw)) |
| 213 | goto done; |
| 214 | if (diff_msr(fout, addr, rev ? mhw : mf, rev ? mf : mhw)) |
| 215 | fprintf(fout, "\n"); |
| 216 | } |
| 217 | } |
| 218 | if (!feof(fin)) |
| 219 | fprintf(stderr, "%s:%d: fgets: %s\n", difffn, linenum, strerror(errno)); |
| 220 | else |
| 221 | ret = 0; |
| 222 | done: |
| 223 | sys->close(cpu); |
| 224 | if (strcmp(difffn, "-")) { |
| 225 | if (ret) |
| 226 | unlink(tmpfn); |
| 227 | else |
| 228 | rename(tmpfn, difffn); |
| 229 | fclose(fin); |
| 230 | fclose(fout); |
| 231 | } |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | int main(int argc, char *argv[]) { |
| 236 | char c; |
| 237 | int ret = 1; |
| 238 | const struct sysdef *s; |
| 239 | const struct targetdef *t; |
| 240 | uint8_t tn, listmsrs = 0, listknown = 0, input = 0; |
| 241 | uint32_t addr = 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) |
| 245 | switch (c) { |
| 246 | case 'h': |
| 247 | syntax(argv); |
| 248 | return 0; |
| 249 | case 'q': |
| 250 | quiet = 1; |
| 251 | break; |
| 252 | case 'v': |
| 253 | ++verbose; |
| 254 | break; |
| 255 | case 'r': |
| 256 | reserved = 1; |
| 257 | break; |
| 258 | case 'k': |
| 259 | listknown = 1; |
| 260 | break; |
| 261 | case 'l': |
| 262 | listmsrs = 1; |
| 263 | break; |
| 264 | case 'c': |
| 265 | cpu = atoi(optarg); |
| 266 | break; |
| 267 | case 'm': |
| 268 | for (s = allsystems; !SYSTEM_ISEOT(*s); s++) |
| 269 | if (!strcmp(s->name, optarg)) { |
| 270 | sys = s; |
| 271 | break; |
| 272 | } |
| 273 | break; |
| 274 | case 't': |
| 275 | for (t = alltargets; !TARGET_ISEOT(*t); t++) |
| 276 | if (!strcmp(t->name, optarg)) { |
| 277 | add_target(t); |
| 278 | break; |
| 279 | } |
| 280 | break; |
| 281 | case 'i': |
| 282 | input = 1; |
Peter Stuge | 3108a12 | 2009-01-26 17:18:31 +0000 | [diff] [blame] | 283 | addr = msraddrbyname(optarg); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 284 | optarg = strchr(optarg, '='); |
| 285 | if (NULL == optarg) { |
| 286 | fprintf(stderr, "missing value in -i argument!\n"); |
| 287 | break; |
| 288 | } |
| 289 | if (!str2msr(++optarg, &msrval)) |
| 290 | fprintf(stderr, "invalid value in -i argument!\n"); |
| 291 | break; |
| 292 | case 's': |
| 293 | streamfn = optarg; |
| 294 | break; |
| 295 | case 'd': |
| 296 | difffn = optarg; |
| 297 | break; |
| 298 | default: |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | printf_quiet("msrtool %s\n", VERSION); |
| 303 | |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 304 | pacc = pci_alloc(); |
| 305 | if (NULL == pacc) { |
| 306 | fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n"); |
| 307 | return 1; |
| 308 | } |
| 309 | pci_init(pacc); |
| 310 | pci_scan_bus(pacc); |
| 311 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 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)) |
| 316 | continue; |
| 317 | printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname); |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | if (targets) |
| 322 | for (tn = 0; tn < targets_found; tn++) |
| 323 | printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname); |
| 324 | else |
| 325 | for (t = alltargets; !TARGET_ISEOT(*t); t++) { |
| 326 | printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname); |
| 327 | if (!t->probe(t)) |
| 328 | continue; |
| 329 | printf_quiet("Detected target %s: %s\n", t->name, t->prettyname); |
| 330 | add_target(t); |
| 331 | } |
| 332 | |
| 333 | printf_quiet("\n"); |
| 334 | fflush(stdout); |
| 335 | |
| 336 | if (listknown) { |
| 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) |
| 343 | printf("\n"); |
| 344 | printf("%s: %s\n", t->name, t->prettyname); |
| 345 | if (listmsrs) |
| 346 | dumpmsrdefs(t); |
| 347 | } |
| 348 | printf("\n"); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | if (sys && !sys->name) { |
| 353 | fprintf(stderr, "Unable to detect the current operating system!\n"); |
Uwe Hermann | 708ccac | 2009-04-10 21:05:56 +0000 | [diff] [blame] | 354 | fprintf(stderr, "On Linux, please do 'modprobe msr' and retry.\n"); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 355 | fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n"); |
| 356 | fprintf(stderr, "\n"); |
| 357 | } |
| 358 | |
| 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"); |
| 363 | return 1; |
| 364 | } |
| 365 | |
| 366 | if (input) { |
| 367 | decodemsr(cpu, addr, msrval); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | if (sys && !sys->name) |
| 372 | return 1; |
| 373 | |
| 374 | if (listmsrs) { |
| 375 | if (streamfn) |
| 376 | return do_stream(streamfn, 1); |
| 377 | for (tn = 0; tn < targets_found; tn++) { |
| 378 | if (tn) |
| 379 | printf("\n"); |
| 380 | dumpmsrdefs(targets[tn]); |
| 381 | } |
| 382 | printf("\n"); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | if (streamfn) |
| 387 | return do_stream(streamfn, 0); |
| 388 | |
| 389 | if (!sys->open(cpu, SYS_RDONLY)) |
| 390 | return 1; |
| 391 | |
| 392 | if (difffn) { |
| 393 | ret = do_diff(difffn); |
| 394 | goto done; |
| 395 | } |
| 396 | |
| 397 | if (optind == argc) { |
| 398 | syntax(argv); |
| 399 | printf("\nNo mode or address(es) specified!\n"); |
| 400 | goto done; |
| 401 | } |
| 402 | |
| 403 | for (; optind < argc; optind++) { |
Peter Stuge | 3108a12 | 2009-01-26 17:18:31 +0000 | [diff] [blame] | 404 | addr = msraddrbyname(argv[optind]); |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 405 | if (!sys->rdmsr(cpu, addr, &msrval)) |
| 406 | break; |
| 407 | decodemsr(cpu, addr, msrval); |
| 408 | } |
| 409 | ret = 0; |
| 410 | done: |
| 411 | sys->close(cpu); |
| 412 | return ret; |
| 413 | } |