Set license using %license
[platform/upstream/x86info.git] / rdmsr.c
1 /*
2  *  (C) 2001 Dave Jones.
3  *
4  *  Licensed under the terms of the GNU GPL License version 2.
5  *
6  *  Contributions by Arjan van de Ven & Philipp Rumpf.
7  *
8  *  Routines for reading MSRs.
9  */
10
11 #define _LARGEFILE64_SOURCE
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <fcntl.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include "x86info.h"
19
20 #if defined(__FreeBSD__)
21 # include <sys/ioctl.h>
22 # include <cpu.h>
23 #endif
24
25 #if defined(__FreeBSD__)
26
27 int read_msr(int cpu, unsigned int idx, unsigned long long *val)
28 {
29         char cpuname[16];
30         unsigned char buffer[8];
31         unsigned long lo, hi;
32         int fh;
33         static int nodriver=0;
34         cpu_msr_args_t args;
35
36         if (nodriver==1)
37                 return 0;
38
39         (void)snprintf(cpuname, sizeof(cpuname), "/dev/cpu%d", cpu);
40
41         fh = open(cpuname, O_RDONLY);
42         if (fh==-1) {
43                 perror(cpuname);
44                 nodriver=1;
45                 return 0;
46         }
47
48         args.msr = idx;
49         if (ioctl(fh, CPU_RDMSR, &args) != 0) {
50                 if (close(fh) == -1) {
51                         perror("close");
52                         exit(EXIT_FAILURE);
53                 }
54
55                 return 0;
56         }
57
58         *val = args.data;
59
60         if (close(fh)==-1) {
61                 perror("close");
62                 exit(EXIT_FAILURE);
63         }
64         return 1;
65 }
66
67 #else /* !__FreeBSD__ */
68
69 int read_msr(int cpu, unsigned int idx, unsigned long long *val)
70 {
71         char cpuname[16];
72         unsigned char buffer[8];
73         unsigned long long lo, hi;
74         int fh;
75         static int nodriver=0;
76         unsigned int *ptr = (unsigned int *) buffer;
77
78         if (nodriver==1)
79                 return 0;
80
81         (void)snprintf(cpuname, sizeof(cpuname), "/dev/cpu/%d/msr", cpu);
82
83         fh = open(cpuname, O_RDONLY);
84         if (fh == -1) {
85                 perror(cpuname);
86                 nodriver=1;
87                 return 0;
88         }
89
90         if (lseek64(fh, (off64_t) idx, SEEK_CUR) == -1) {
91                 perror("lseek");
92                 exit(EXIT_FAILURE);
93         }
94
95         if (fh != -1) {
96                 if (read(fh, &buffer[0], 8) != 8) {
97                         if (close(fh) == -1) {
98                                 perror("close");
99                                 exit(EXIT_FAILURE);
100                         }
101                         return 0;
102                 }
103
104                 lo = *ptr;
105                 hi = *(++ptr);
106                 *val = (hi << 32) | lo;
107         }
108         if (close(fh) == -1) {
109                 perror("close");
110                 exit(EXIT_FAILURE);
111         }
112         return 1;
113 }
114
115 #endif /* __FreeBSD__ */
116
117
118 void dumpmsr(int cpu, unsigned int msr, int size)
119 {
120         unsigned long long val=0;
121
122         if (read_msr(cpu, msr, &val) == 1) {
123                 if (size == 32){
124                         printf("MSR: 0x%08x=0x%08lx : ", msr, (unsigned long) val);
125                         binary32(val);
126                 }
127                 if (size == 64) {
128                         printf("MSR: 0x%08x=0x%016llx : ", msr, val);
129                         binary64(val);
130                 }
131                 return;
132         }
133         printf("Couldn't read MSR 0x%x\n", msr);
134 }
135
136 void dumpmsr_bin(int cpu, unsigned int msr, int size)
137 {
138         unsigned long long val=0;
139
140         if (read_msr(cpu, msr, &val) == 1) {
141                 if (size == 32)
142                         binary32(val);
143                 if (size == 64)
144                         binary64(val);
145                 return;
146         }
147         printf("Couldn't read MSR 0x%x\n", msr);
148 }