Add rdmsr and wrmsr utilities
authorBarret Rhoden <brho@cs.berkeley.edu>
Mon, 7 Mar 2016 19:07:17 +0000 (14:07 -0500)
committerBarret Rhoden <brho@cs.berkeley.edu>
Mon, 7 Mar 2016 19:38:17 +0000 (14:38 -0500)
Note that wrmsr writes the same MSR value to *all* cores.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
tests/rdmsr.c [new file with mode: 0644]
tests/wrmsr.c [new file with mode: 0644]

diff --git a/tests/rdmsr.c b/tests/rdmsr.c
new file mode 100644 (file)
index 0000000..cad6a2d
--- /dev/null
@@ -0,0 +1,52 @@
+/* Copyright (c) 2016 Google Inc.
+ * Barret Rhoden <brho@cs.berkeley.edu>
+ * See LICENSE for details.
+ *
+ * Usage: rdmsr MSR
+ *
+ * This will read MSR on all cores.
+ *
+ * e.g. rdmsr 0x199 */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <unistd.h>
+#include <parlib/sysinfo.h>
+
+int main(int argc, char **argv)
+{
+       uint32_t msr;
+       int fd;
+       uint64_t *buf;
+       size_t buf_sz;
+       ssize_t ret;
+       int num_cores;
+
+       if (argc < 2) {
+               printf("Usage: %s MSR\n", argv[0]);
+               exit(-1);
+       }
+       msr = strtoul(argv[1], 0, 0);
+       num_cores = get_num_pcores();
+       fd = open("#arch/msr", O_RDWR);
+       if (fd < 0) {
+               perror("open");
+               exit(-1);
+       }
+       buf_sz = num_cores * sizeof(uint64_t);
+       buf = malloc(buf_sz);
+       assert(buf);
+       ret = pread(fd, buf, buf_sz, msr);
+       if (ret < 0) {
+               perror("pread");
+               exit(-1);
+       }
+       for (int i = 0; i < num_cores; i++)
+               printf("Core %3d, MSR 0x%08x: 0x%016llx\n", i, msr, buf[i]);
+       return 0;
+}
diff --git a/tests/wrmsr.c b/tests/wrmsr.c
new file mode 100644 (file)
index 0000000..5f6554d
--- /dev/null
@@ -0,0 +1,45 @@
+/* Copyright (c) 2016 Google Inc.
+ * Barret Rhoden <brho@cs.berkeley.edu>
+ * See LICENSE for details.
+ *
+ * Usage: wrmsr MSR VAL
+ *
+ * This will write VAL to *all cores* MSR.
+ *
+ * e.g. wrmsr 0x199 0x100002600 */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+       uint32_t msr;
+       uint64_t val;
+       int fd;
+       ssize_t ret;
+
+       if (argc < 3) {
+               printf("Usage: %s MSR VAL\n", argv[0]);
+               exit(-1);
+       }
+       msr = strtoul(argv[1], 0, 0);
+       val = strtoul(argv[2], 0, 0);
+
+       fd = open("#arch/msr", O_RDWR);
+       if (fd < 0) {
+               perror("open");
+               exit(-1);
+       }
+       ret = pwrite(fd, &val, sizeof(val), msr);
+       if (ret < 0) {
+               perror("pwrite");
+               exit(-1);
+       }
+       return 0;
+}