4 * Licensed under the terms of the GNU GPL License version 2.
6 * Contributions by Arjan van de Ven & Philipp Rumpf.
8 * Routines for reading MSRs.
11 #define _LARGEFILE64_SOURCE
16 #include <sys/types.h>
20 #if defined(__FreeBSD__)
21 # include <sys/ioctl.h>
25 #if defined(__FreeBSD__)
27 int read_msr(int cpu, unsigned int idx, unsigned long long *val)
30 unsigned char buffer[8];
33 static int nodriver=0;
39 (void)snprintf(cpuname, sizeof(cpuname), "/dev/cpu%d", cpu);
41 fh = open(cpuname, O_RDONLY);
49 if (ioctl(fh, CPU_RDMSR, &args) != 0) {
50 if (close(fh) == -1) {
67 #else /* !__FreeBSD__ */
69 int read_msr(int cpu, unsigned int idx, unsigned long long *val)
72 unsigned char buffer[8];
73 unsigned long long lo, hi;
75 static int nodriver=0;
76 unsigned int *ptr = (unsigned int *) buffer;
81 (void)snprintf(cpuname, sizeof(cpuname), "/dev/cpu/%d/msr", cpu);
83 fh = open(cpuname, O_RDONLY);
90 if (lseek64(fh, (off64_t) idx, SEEK_CUR) == -1) {
96 if (read(fh, &buffer[0], 8) != 8) {
97 if (close(fh) == -1) {
106 *val = (hi << 32) | lo;
108 if (close(fh) == -1) {
115 #endif /* __FreeBSD__ */
118 void dumpmsr(int cpu, unsigned int msr, int size)
120 unsigned long long val=0;
122 if (read_msr(cpu, msr, &val) == 1) {
124 printf("MSR: 0x%08x=0x%08lx : ", msr, (unsigned long) val);
128 printf("MSR: 0x%08x=0x%016llx : ", msr, val);
133 printf("Couldn't read MSR 0x%x\n", msr);
136 void dumpmsr_bin(int cpu, unsigned int msr, int size)
138 unsigned long long val=0;
140 if (read_msr(cpu, msr, &val) == 1) {
147 printf("Couldn't read MSR 0x%x\n", msr);