Debian Codegraph @

rdmsr.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2000 Transmeta Corporation - All Rights Reserved
4 * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
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 as published by
8 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
9 * Boston MA 02110-1301, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
11 *
12 * ----------------------------------------------------------------------- */
13
14 /*
15 * rdmsr.c
16 *
17 * Utility to read an MSR.
18 */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <getopt.h>
26 #include <inttypes.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <ctype.h>
30
31 #include "version.h"
32
33 static const struct option long_options[] = {
34 {"help", 0, 0, 'h'},
35 {"version", 0, 0, 'V'},
36 {"hexadecimal", 0, 0, 'x'},
37 {"capital-hexadecimal", 0, 0, 'X'},
38 {"decimal", 0, 0, 'd'},
39 {"signed-decimal", 0, 0, 'd'},
40 {"unsigned-decimal", 0, 0, 'u'},
41 {"octal", 0, 0, 'o'},
42 {"c-language", 0, 0, 'c'},
43 {"zero-fill", 0, 0, '0'},
44 {"zero-pad", 0, 0, '0'},
45 {"raw", 0, 0, 'r'},
46 {"all", 0, 0, 'a'},
47 {"processor", 1, 0, 'p'},
48 {"cpu", 1, 0, 'p'},
49 {"bitfield", 1, 0, 'f'},
50 {0, 0, 0, 0}
51 };
52 static const char short_options[] = "hVxXdoruc0ap:f:";
53 static int doing_for_all = 0;
54
55 /* Number of decimal digits for a certain number of bits */
56 /* (int) ceil(log(2^n)/log(10)) */
57 int decdigits[] = {
58 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5,
59 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10,
60 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15,
61 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19,
62 20
63 };
64
65 #define mo_hex 0x01
66 #define mo_dec 0x02
67 #define mo_oct 0x03
68 #define mo_raw 0x04
69 #define mo_uns 0x05
70 #define mo_chx 0x06
71 #define mo_mask 0x0f
72 #define mo_fill 0x40
73 #define mo_c 0x80
74
75 const char *program;
76
77 void usage(void)
78 {
79 fprintf(stderr,
80 "Usage: %s [options] regno\n"
81 " --help -h Print this help\n"
82 " --version -V Print current version\n"
83 " --hexadecimal -x Hexadecimal output (lower case)\n"
84 " --capital-hex -X Hexadecimal output (upper case)\n"
85 " --decimal -d Signed decimal output\n"
86 " --unsigned -u Unsigned decimal output\n"
87 " --octal -o Octal output\n"
88 " --c-language -c Format output as a C language constant\n"
89 " --zero-pad -0 Output leading zeroes\n"
90 " --raw -r Raw binary output\n"
91 " --all -a all processors\n"
92 " --processor # -p Select processor number (default 0)\n"
93 " --bitfield h:l -f Output bits [h:l] only\n", program);
94 }
95
96 void rdmsr_on_cpu(uint32_t reg, int cpu);
97
98 /* filter out ".", "..", "microcode" in /dev/cpu */
99 int dir_filter(const struct dirent *dirp) {
100 if (isdigit(dirp->d_name[0]))
101 return 1;
102 else
103 return 0;
104 }
105
106 void rdmsr_on_all_cpus(uint32_t reg)
107 {
108 struct dirent **namelist;
109 int dir_entries;
110
111 dir_entries = scandir("/dev/cpu", &namelist, dir_filter, 0);
112 while (dir_entries--) {
113 rdmsr_on_cpu(reg, atoi(namelist[dir_entries]->d_name));
114 free(namelist[dir_entries]);
115 }
116 free(namelist);
117 }
118
119 unsigned int highbit = 63, lowbit = 0;
120 int mode = mo_hex;
121
122 int main(int argc, char *argv[])
123 {
124 uint32_t reg;
125 int c;
126 int cpu = 0;
127 unsigned long arg;
128 char *endarg;
129
130 program = argv[0];
131
132 while ((c =
133 getopt_long(argc, argv, short_options, long_options,
134 NULL)) != -1) {
135 switch (c) {
136 case 'h':
137 usage();
138 exit(0);
139 case 'V':
140 fprintf(stderr, "%s: version %s\n", program,
141 VERSION_STRING);
142 exit(0);
143 case 'x':
144 mode = (mode & ~mo_mask) | mo_hex;
145 break;
146 case 'X':
147 mode = (mode & ~mo_mask) | mo_chx;
148 break;
149 case 'o':
150 mode = (mode & ~mo_mask) | mo_oct;
151 break;
152 case 'd':
153 mode = (mode & ~mo_mask) | mo_dec;
154 break;
155 case 'r':
156 mode = (mode & ~mo_mask) | mo_raw;
157 break;
158 case 'u':
159 mode = (mode & ~mo_mask) | mo_uns;
160 break;
161 case 'c':
162 mode |= mo_c;
163 break;
164 case '0':
165 mode |= mo_fill;
166 break;
167 case 'a':
168 cpu = -1;
169 break;
170 case 'p':
171 arg = strtoul(optarg, &endarg, 0);
172 if (*endarg || arg > 5119) {
173 usage();
174 exit(127);
175 }
176 cpu = (int)arg;
177 break;
178 case 'f':
179 if (sscanf(optarg, "%u:%u", &highbit, &lowbit) != 2 ||
180 highbit > 63 || lowbit > highbit) {
181 usage();
182 exit(127);
183 }
184 break;
185 default:
186 usage();
187 exit(127);
188 }
189 }
190
191 if (optind != argc - 1) {
192 /* Should have exactly one argument */
193 usage();
194 exit(127);
195 }
196
•197 reg = strtoul(argv[optind], &endarg, 0);
198 if (*endarg) {
199 printf("Failed to parse register number.\n");
200 exit(127);
201 }
202
203 if (cpu == -1) {
204 doing_for_all = 1;
205 rdmsr_on_all_cpus(reg);
206 }
207 else
208 rdmsr_on_cpu(reg, cpu);
209 exit(0);
210 }
211
212 void rdmsr_on_cpu(uint32_t reg, int cpu)
213 {
214 uint64_t data;
215 int fd;
216 char *pat;
217 int width;
218 char msr_file_name[64];
219 unsigned int bits;
220
221 sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
222 fd = open(msr_file_name, O_RDONLY);
223 if (fd < 0) {
224 if (errno == ENXIO) {
225 if (doing_for_all)
226 return;
227 fprintf(stderr, "rdmsr: No CPU %d\n", cpu);
228 exit(2);
229 } else if (errno == EIO) {
230 fprintf(stderr, "rdmsr: CPU %d doesn't support MSRs\n",
231 cpu);
232 exit(3);
233 } else {
234 perror("rdmsr: open");
235 exit(127);
236 }
237 }
238
239 if (pread(fd, &data, sizeof data, reg) != sizeof data) {
240 if (errno == EIO) {
241 fprintf(stderr, "rdmsr: CPU %d cannot read "
242 "MSR 0x%08"PRIx32"\n",
243 cpu, reg);
244 exit(4);
245 } else {
246 perror("rdmsr: pread");
247 exit(127);
248 }
249 }
250
251 close(fd);
252
253 bits = highbit - lowbit + 1;
254 if (bits < 64) {
255 /* Show only part of register */
256 data >>= lowbit;
257 data &= (1ULL << bits) - 1;
258 }
259
260 pat = NULL;
261
262 width = 1; /* Default */
263 switch (mode) {
264 case mo_hex:
265 pat = "%*llx\n";
266 break;
267 case mo_chx:
268 pat = "%*llX\n";
269 break;
270 case mo_dec:
271 case mo_dec | mo_c:
272 case mo_dec | mo_fill | mo_c:
273 /* Make sure we get sign correct */
274 if (data & (1ULL << (bits - 1))) {
275 data &= ~(1ULL << (bits - 1));
276 data = -data;
277 }
278 pat = "%*lld\n";
279 break;
280 case mo_uns:
281 pat = "%*llu\n";
282 break;
283 case mo_oct:
284 pat = "%*llo\n";
285 break;
286 case mo_hex | mo_c:
287 pat = "0x%*llx\n";
288 break;
289 case mo_chx | mo_c:
290 pat = "0x%*llX\n";
291 break;
292 case mo_oct | mo_c:
293 pat = "0%*llo\n";
294 break;
295 case mo_uns | mo_c:
296 case mo_uns | mo_fill | mo_c:
297 pat = "%*lluU\n";
298 break;
299 case mo_hex | mo_fill:
300 pat = "%0*llx\n";
301 width = (bits + 3) / 4;
302 break;
303 case mo_chx | mo_fill:
304 pat = "%0*llX\n";
305 width = (bits + 3) / 4;
306 break;
307 case mo_dec | mo_fill:
308 /* Make sure we get sign correct */
309 if (data & (1ULL << (bits - 1))) {
310 data &= ~(1ULL << (bits - 1));
311 data = -data;
312 }
313 pat = "%0*lld\n";
314 width = decdigits[bits - 1] + 1;
315 break;
316 case mo_uns | mo_fill:
317 pat = "%0*llu\n";
318 width = decdigits[bits];
319 break;
320 case mo_oct | mo_fill:
321 pat = "%0*llo\n";
322 width = (bits + 2) / 3;
323 break;
324 case mo_hex | mo_fill | mo_c:
325 pat = "0x%0*llx\n";
326 width = (bits + 3) / 4;
327 break;
328 case mo_chx | mo_fill | mo_c:
329 pat = "0x%0*llX\n";
330 width = (bits + 3) / 4;
331 break;
332 case mo_oct | mo_fill | mo_c:
333 pat = "0%0*llo\n";
334 width = (bits + 2) / 3;
335 break;
336 case mo_raw:
337 case mo_raw | mo_fill:
338 fwrite(&data, sizeof data, 1, stdout);
339 break;
340 case mo_raw | mo_c:
341 case mo_raw | mo_fill | mo_c:
342 {
343 unsigned char *p = (unsigned char *)&data;
344 int i;
345 for (i = 0; i < sizeof data; i++) {
346 printf("%s0x%02x", i ? "," : "{",
347 (unsigned int)(*p++));
348 }
349 printf("}\n");
350 }
351 break;
352 default:
353 fprintf(stderr, "%s: Impossible case, line %d\n", program,
354 __LINE__);
355 exit(127);
356 }
357
358 if (width < 1)
359 width = 1;
360
361 if (pat) {
362 if (doing_for_all)
363 printf("CPU %d: ", cpu);
364 printf(pat, width, data);
365 }
366 return;
367 }