coreboot/util/msrtool/msrtool.c
<<
>>
Prefs
   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
  33static uint8_t cpu = DEFAULT_CPU;
  34
  35uint8_t targets_found = 0;
  36const struct targetdef **targets = NULL;
  37const struct sysdef *sys = NULL;
  38uint8_t reserved = 0, verbose = 0, quiet = 0;
  39
  40struct pci_access *pacc = NULL;
  41
  42static 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 },
  47        { TARGET_EOT }
  48};
  49
  50static struct sysdef allsystems[] = {
  51        { "linux", "Linux with /dev/cpu/*/msr", linux_probe, linux_open, linux_close, linux_rdmsr },
  52        { "darwin", "Mac OS X with DirectHW", darwin_probe, darwin_open, darwin_close, darwin_rdmsr },
  53        { "freebsd", "FreeBSD with /dev/cpuctl*", freebsd_probe, freebsd_open, freebsd_close, freebsd_rdmsr },
  54        { SYSTEM_EOT }
  55};
  56
  57static 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");
  81}
  82
  83static void *add_target(const struct targetdef *t) {
  84        void *tmp;
  85        tmp = realloc(targets, (targets_found + 2) * sizeof(struct targetdef *));
  86        if (NULL == tmp) {
  87                perror("realloc");
  88                return tmp;
  89        }
  90        targets = tmp;
  91        targets[targets_found++] = t;
  92        targets[targets_found] = NULL;
  93        return targets;
  94}
  95
  96static int found_system() {
  97        if (!sys || (sys && !sys->name)) {
  98                fprintf(stderr, "Unable to detect the current operating system!\n");
  99                fprintf(stderr, "On Linux, please run 'modprobe msr' and try again.\n");
 100                fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
 101                fprintf(stderr, "\n");
 102        }
 103        return (sys && sys->name);
 104}
 105
 106int do_stream(const char *streamfn, uint8_t ignoreinput) {
 107        char tmpfn[20], line[256];
 108        uint8_t tn;
 109        size_t start, len;
 110        int ret = 1;
 111        int fdout = -1;
 112        FILE *fin = NULL, *fout = NULL;
 113        uint32_t addr, linenum;
 114        struct msr m = MSR1(0);
 115
 116        if (0 == strcmp(streamfn, "-")) {
 117                fin = stdin;
 118                fout = stdout;
 119        } else {
 120                if (!ignoreinput) {
 121                        if (NULL == (fin = fopen(streamfn, "r"))) {
 122                                perror("fopen()");
 123                                return 1;
 124                        }
 125                        if (snprintf(tmpfn, sizeof(tmpfn), "msrtoolXXXXXX") >= sizeof(tmpfn)) {
 126                                perror("snprintf");
 127                                return 1;
 128                        }
 129                        if (-1 == (fdout = mkstemp(tmpfn))) {
 130                                perror("mkstemp");
 131                                return 1;
 132                        }
 133                        if (NULL == (fout = fdopen(fdout, "w"))) {
 134                                perror("fdopen");
 135                                return 1;
 136                        }
 137                } else {
 138                        if (NULL == (fout = fopen(streamfn, "w"))) {
 139                                perror("fopen");
 140                                return 1;
 141                        }
 142                }
 143        }
 144
 145        if (!found_system())
 146                goto done;
 147        if (!sys->open(cpu, SYS_RDONLY))
 148                goto done;
 149        if (ignoreinput) {
 150                for (tn = 0; tn < targets_found; tn++)
 151                        if (dumpmsrdefsvals(fout, targets[tn], cpu)) {
 152                                ret = 1;
 153                                break;
 154                        }
 155        } else {
 156                for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
 157                        start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
 158                        if (1 == sscanf(line + start, "%8x", &addr)) {
 159                                if (!sys->rdmsr(cpu, addr, &m))
 160                                        goto done;
 161                                fprintf(fout, "0x%08x 0x%08x%08x\n", addr, m.hi, m.lo);
 162                                continue;
 163                        }
 164                        while (1) {
 165                                fprintf(fout, "%s", line);
 166                                len = strlen(line);
 167                                if (NULL != strchr("\r\n", line[len - 1]))
 168                                        break;
 169                                if (NULL == fgets(line, sizeof(line), fin))
 170                                        goto read_done;
 171                        }
 172                }
 173read_done:
 174                if (!feof(fin)) {
 175                        fprintf(stderr, "%s:%d: fgets: %s\n", streamfn, linenum, strerror(errno));
 176                        goto done;
 177                }
 178        }
 179        ret = 0;
 180done:
 181        sys->close(cpu);
 182        if (strcmp(streamfn, "-")) {
 183                if (ret)
 184                        unlink(tmpfn);
 185                else if (!ignoreinput)
 186                        rename(tmpfn, streamfn);
 187        }
 188        if (!ignoreinput)
 189                fclose(fin);
 190        fclose(fout);
 191        return ret;
 192}
 193
 194int do_diff(const char *difffn) {
 195        char tmpfn[20], line[512], *m1start, *m2start;
 196        size_t len;
 197        int ret = 1, tmp, m1pos, sys_opened = 0;
 198        FILE *fin = NULL, *fout = stdout;
 199        uint8_t rev = 0;
 200        uint32_t addr, linenum;
 201        struct msr m1 = MSR1(0), m2 = MSR1(0);
 202
 203        if (':' == difffn[0]) {
 204                rev = 1;
 205                ++difffn;
 206        }
 207        if (0 == strcmp(difffn, "-"))
 208                fin = stdin;
 209        else if (NULL == (fin = fopen(difffn, "r"))) {
 210                perror("fopen()");
 211                return 1;
 212        }
 213
 214        for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
 215                tmp = strncmp("0x", line, 2) ? 0 : 2;
 216                if (sscanf(line + tmp, "%8x %n%*x", &addr, &m1pos) < 1)
 217                        continue;
 218                m1start = line + tmp + m1pos;
 219                for (len = strlen(m1start) - 1; NULL != strchr("\r\n", m1start[len]); --len)
 220                        m1start[len] = 0;
 221                if (!str2msr(m1start, &m1, &m2start)) {
 222                        fprintf(stderr, "%s:%d: invalid MSR1 value '%s'\n", difffn, linenum, m1start);
 223                        continue;
 224                }
 225                while (' ' == *m2start)
 226                        ++m2start;
 227                if (!str2msr(m2start, &m2, NULL)) {
 228                        fprintf(stderr, "%s:%d: invalid MSR2 value '%s' - reading from hardware!\n", difffn, linenum, m2start);
 229                        if (!sys_opened) {
 230                                if (!found_system())
 231                                        goto done;
 232                                sys_opened = sys->open(cpu, SYS_RDONLY);
 233                                if (!sys_opened)
 234                                        goto done;
 235                        }
 236                        if (!sys->rdmsr(cpu, addr, &m2))
 237                                goto done;
 238                }
 239                if (diff_msr(fout, addr, rev ? m2 : m1, rev ? m1 : m2))
 240                        fprintf(fout, "\n");
 241        }
 242        if (!feof(fin))
 243                fprintf(stderr, "%s:%d: fgets: %s\n", difffn, linenum, strerror(errno));
 244        else
 245                ret = 0;
 246done:
 247        if (sys_opened)
 248                sys->close(cpu);
 249        if (strcmp(difffn, "-")) {
 250                if (ret)
 251                        unlink(tmpfn);
 252                else
 253                        rename(tmpfn, difffn);
 254                fclose(fin);
 255                fclose(fout);
 256        }
 257        return ret;
 258}
 259
 260int main(int argc, char *argv[]) {
 261        char c;
 262        int ret = 1;
 263        const struct sysdef *s;
 264        const struct targetdef *t;
 265        uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
 266        uint32_t addr = 0;
 267        const char *streamfn = NULL, *difffn = NULL;
 268        struct msr msrval = MSR2(-1, -1);
 269        while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
 270                switch (c) {
 271                case 'h':
 272                        syntax(argv);
 273                        return 0;
 274                case 'q':
 275                        quiet = 1;
 276                        break;
 277                case 'v':
 278                        ++verbose;
 279                        break;
 280                case 'r':
 281                        reserved = 1;
 282                        break;
 283                case 'k':
 284                        listknown = 1;
 285                        break;
 286                case 'l':
 287                        listmsrs = 1;
 288                        break;
 289                case 'c':
 290                        cpu = atoi(optarg);
 291                        break;
 292                case 'm':
 293                        for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
 294                                if (!strcmp(s->name, optarg)) {
 295                                        sys = s;
 296                                        break;
 297                                }
 298                        break;
 299                case 't':
 300                        for (t = alltargets; !TARGET_ISEOT(*t); t++)
 301                                if (!strcmp(t->name, optarg)) {
 302                                        add_target(t);
 303                                        break;
 304                                }
 305                        break;
 306                case 'i':
 307                        input = 1;
 308                        addr = msraddrbyname(optarg);
 309                        optarg = strchr(optarg, '=');
 310                        if (NULL == optarg) {
 311                                fprintf(stderr, "missing value in -i argument!\n");
 312                                break;
 313                        }
 314                        if (!str2msr(++optarg, &msrval, NULL))
 315                                fprintf(stderr, "invalid value in -i argument!\n");
 316                        break;
 317                case 's':
 318                        streamfn = optarg;
 319                        break;
 320                case 'd':
 321                        difffn = optarg;
 322                        break;
 323                default:
 324                        break;
 325                }
 326
 327        printf_quiet("msrtool %s\n", VERSION);
 328
 329        pacc = pci_alloc();
 330        if (NULL == pacc) {
 331                fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n");
 332                return 1;
 333        }
 334        pci_init(pacc);
 335        pci_scan_bus(pacc);
 336
 337        if (!sys && !input && !listknown)
 338                for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
 339                        printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
 340                        if (!sys->probe(sys))
 341                                continue;
 342                        printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
 343                        break;
 344                }
 345
 346        if (targets)
 347                for (tn = 0; tn < targets_found; tn++)
 348                        printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
 349        else
 350                for (t = alltargets; !TARGET_ISEOT(*t); t++) {
 351                        printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
 352                        if (!t->probe(t))
 353                                continue;
 354                        printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
 355                        add_target(t);
 356                }
 357
 358        printf_quiet("\n");
 359        fflush(stdout);
 360
 361        if (listknown) {
 362                printf("Known systems:\n");
 363                for (s = allsystems; s->name; s++)
 364                        printf("%s: %s\n", s->name, s->prettyname);
 365                printf("\nKnown targets:\n");
 366                for (t = alltargets; t->name; t++) {
 367                        if (listmsrs && alltargets != t)
 368                                printf("\n");
 369                        printf("%s: %s\n", t->name, t->prettyname);
 370                        if (listmsrs)
 371                                dumpmsrdefs(t);
 372                }
 373                printf("\n");
 374                return 0;
 375        }
 376
 377        if (!targets_found || !targets) {
 378                fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
 379                fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
 380                fprintf(stderr, "\n");
 381                return 1;
 382        }
 383
 384        if (input) {
 385                decodemsr(cpu, addr, msrval);
 386                return 0;
 387        }
 388
 389        if (listmsrs) {
 390                if (streamfn)
 391                        return do_stream(streamfn, 1);
 392                for (tn = 0; tn < targets_found; tn++) {
 393                        if (tn)
 394                                printf("\n");
 395                        dumpmsrdefs(targets[tn]);
 396                }
 397                printf("\n");
 398                return 0;
 399        }
 400
 401        if (streamfn)
 402                return do_stream(streamfn, 0);
 403
 404        if (difffn) {
 405                ret = do_diff(difffn);
 406                goto done;
 407        }
 408
 409        if (optind == argc) {
 410                syntax(argv);
 411                printf("\nNo mode or address(es) specified!\n");
 412                goto done;
 413        }
 414
 415        if (!found_system())
 416                return 1;
 417        if (!sys->open(cpu, SYS_RDONLY))
 418                return 1;
 419
 420        for (; optind < argc; optind++) {
 421                addr = msraddrbyname(argv[optind]);
 422                if (!sys->rdmsr(cpu, addr, &msrval))
 423                        break;
 424                decodemsr(cpu, addr, msrval);
 425        }
 426        ret = 0;
 427done:
 428        sys->close(cpu);
 429        return ret;
 430}
 431
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.