Debian Codegraph @

rdmsr.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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], NULL, 0);
198
199 if (cpu == -1) {
200 doing_for_all = 1;
201 rdmsr_on_all_cpus(reg);
202 }
203 else
204 rdmsr_on_cpu(reg, cpu);
205 exit(0);
206 }
207
208 void rdmsr_on_cpu(uint32_t reg, int cpu)
209 {
210 uint64_t data;
211 int fd;
212 char *pat;
213 int width;
214 char msr_file_name[64];
215 unsigned int bits;
216
217 sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
218 fd = open(msr_file_name, O_RDONLY);
219 if (fd < 0) {
220 if (errno == ENXIO) {
221 if (doing_for_all)
222 return;
223 fprintf(stderr, "rdmsr: No CPU %d\n", cpu);
224 exit(2);
225 } else if (errno == EIO) {
226 fprintf(stderr, "rdmsr: CPU %d doesn't support MSRs\n",
227 cpu);
228 exit(3);
229 } else {
230 perror("rdmsr: open");
231 exit(127);
232 }
233 }
234
235 if (pread(fd, &data, sizeof data, reg) != sizeof data) {
236 if (errno == EIO) {
237 fprintf(stderr, "rdmsr: CPU %d cannot read "
238 "MSR 0x%08"PRIx32"\n",
239 cpu, reg);
240 exit(4);
241 } else {
242 perror("rdmsr: pread");
243 exit(127);
244 }
245 }
246
247 close(fd);
248
249 bits = highbit - lowbit + 1;
250 if (bits < 64) {
251 /* Show only part of register */
252 data >>= lowbit;
253 data &= (1ULL << bits) - 1;
254 }
255
256 pat = NULL;
257
258 width = 1; /* Default */
259 switch (mode) {
260 case mo_hex:
261 pat = "%*llx\n";
262 break;
263 case mo_chx:
264 pat = "%*llX\n";
265 break;
266 case mo_dec:
267 case mo_dec | mo_c:
268 case mo_dec | mo_fill | mo_c:
269 /* Make sure we get sign correct */
270 if (data & (1ULL << (bits - 1))) {
271 data &= ~(1ULL << (bits - 1));
272 data = -data;
273 }
274 pat = "%*lld\n";
275 break;
276 case mo_uns:
277 pat = "%*llu\n";
278 break;
279 case mo_oct:
280 pat = "%*llo\n";
281 break;
282 case mo_hex | mo_c:
283 pat = "0x%*llx\n";
284 break;
285 case mo_chx | mo_c:
286 pat = "0x%*llX\n";
287 break;
288 case mo_oct | mo_c:
289 pat = "0%*llo\n";
290 break;
291 case mo_uns | mo_c:
292 case mo_uns | mo_fill | mo_c:
293 pat = "%*lluU\n";
294 break;
295 case mo_hex | mo_fill:
296 pat = "%0*llx\n";
297 width = (bits + 3) / 4;
298 break;
299 case mo_chx | mo_fill:
300 pat = "%0*llX\n";
301 width = (bits + 3) / 4;
302 break;
303 case mo_dec | mo_fill:
304 /* Make sure we get sign correct */
305 if (data & (1ULL << (bits - 1))) {
306 data &= ~(1ULL << (bits - 1));
307 data = -data;
308 }
309 pat = "%0*lld\n";
310 width = decdigits[bits - 1] + 1;
311 break;
312 case mo_uns | mo_fill:
313 pat = "%0*llu\n";
314 width = decdigits[bits];
315 break;
316 case mo_oct | mo_fill:
317 pat = "%0*llo\n";
318 width = (bits + 2) / 3;
319 break;
320 case mo_hex | mo_fill | mo_c:
321 pat = "0x%0*llx\n";
322 width = (bits + 3) / 4;
323 break;
324 case mo_chx | mo_fill | mo_c:
325 pat = "0x%0*llX\n";
326 width = (bits + 3) / 4;
327 break;
328 case mo_oct | mo_fill | mo_c:
329 pat = "0%0*llo\n";
330 width = (bits + 2) / 3;
331 break;
332 case mo_raw:
333 case mo_raw | mo_fill:
334 fwrite(&data, sizeof data, 1, stdout);
335 break;
336 case mo_raw | mo_c:
337 case mo_raw | mo_fill | mo_c:
338 {
339 unsigned char *p = (unsigned char *)&data;
340 int i;
341 for (i = 0; i < sizeof data; i++) {
342 printf("%s0x%02x", i ? "," : "{",
343 (unsigned int)(*p++));
344 }
345 printf("}\n");
346 }
347 break;
348 default:
349 fprintf(stderr, "%s: Impossible case, line %d\n", program,
350 __LINE__);
351 exit(127);
352 }
353
354 if (width < 1)
355 width = 1;
356
357 if (pat) {
358 if (doing_for_all)
359 printf("CPU %d: ", cpu);
360 printf(pat, width, data);
361 }
362 return;
363 }