akaros/tests/rdmsr.c
<<
>>
Prefs
   1/* Copyright (c) 2016 Google Inc.
   2 * Barret Rhoden <brho@cs.berkeley.edu>
   3 * See LICENSE for details.
   4 *
   5 * Usage: rdmsr MSR
   6 *
   7 * This will read MSR on all cores.
   8 *
   9 * e.g. rdmsr 0x199 */
  10
  11#include <stdio.h>
  12#include <stdlib.h>
  13#include <stdint.h>
  14#include <sys/types.h>
  15#include <sys/stat.h>
  16#include <fcntl.h>
  17#include <assert.h>
  18#include <unistd.h>
  19#include <parlib/sysinfo.h>
  20
  21int main(int argc, char **argv)
  22{
  23        uint32_t msr;
  24        int fd;
  25        uint64_t *buf;
  26        size_t buf_sz;
  27        ssize_t ret;
  28        int num_cores;
  29
  30        if (argc < 2) {
  31                printf("Usage: %s MSR\n", argv[0]);
  32                exit(-1);
  33        }
  34        msr = strtoul(argv[1], 0, 0);
  35        num_cores = get_num_pcores();
  36        fd = open("#arch/msr", O_RDWR);
  37        if (fd < 0) {
  38                perror("open");
  39                exit(-1);
  40        }
  41        buf_sz = num_cores * sizeof(uint64_t);
  42        buf = malloc(buf_sz);
  43        assert(buf);
  44        ret = pread(fd, buf, buf_sz, msr);
  45        if (ret < 0) {
  46                perror("pread");
  47                exit(-1);
  48        }
  49        for (int i = 0; i < num_cores; i++)
  50                printf("Core %3d, MSR 0x%08x: 0x%016llx\n", i, msr, buf[i]);
  51        return 0;
  52}
  53