Debian Codegraph @

wrmsr.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 * wrmsr.c
16 *
17 * Utility to write to 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 {"all", 0, 0, 'a'},
37 {"processor", 1, 0, 'p'},
38 {"cpu", 1, 0, 'p'},
39 {0, 0, 0, 0}
40 };
41 static const char short_options[] = "hVap:";
42 static int doing_for_all = 0;
43
44 const char *program;
45
46 void usage(void)
47 {
48 fprintf(stderr, "Usage: %s [options] regno value...\n"
49 " --help -h Print this help\n"
50 " --version -V Print current version\n"
51 " --all -a all processors\n"
52 " --processor # -p Select processor number (default 0)\n",
53 program);
54 }
55
56 void wrmsr_on_cpu(uint32_t reg, int cpu, int valcnt, char *regvals[]);
57
58 /* filter out ".", "..", "microcode" in /dev/cpu */
59 int dir_filter(const struct dirent *dirp)
60 {
61 if (isdigit(dirp->d_name[0]))
62 return 1;
63 else
64 return 0;
65 }
66
67 void wrmsr_on_all_cpus(uint32_t reg, int valcnt, char *regvals[])
68 {
69 struct dirent **namelist;
70 int dir_entries;
71
72 dir_entries = scandir("/dev/cpu", &namelist, dir_filter, 0);
73 while (dir_entries--) {
74 wrmsr_on_cpu(reg, atoi(namelist[dir_entries]->d_name),
75 valcnt, regvals);
76 free(namelist[dir_entries]);
77 }
78 free(namelist);
79 }
80
81 int main(int argc, char *argv[])
82 {
83 uint32_t reg;
84 int c;
85 int cpu = 0;
86 unsigned long arg;
87 char *endarg;
88
89 program = argv[0];
90
91 while ((c = getopt_long(argc, argv, short_options,
92 long_options, NULL)) != -1) {
93 switch (c) {
94 case 'h':
95 usage();
96 exit(0);
97 case 'V':
98 fprintf(stderr, "%s: version %s\n", program,
99 VERSION_STRING);
100 exit(0);
101 case 'a':
102 cpu = -1;
103 break;
104 case 'p':
105 arg = strtoul(optarg, &endarg, 0);
106 if (*endarg || arg > 5119) {
107 usage();
108 exit(127);
109 }
110 cpu = (int)arg;
111 break;
112 default:
113 usage();
114 exit(127);
115 }
116 }
117
118 if (optind > argc - 2) {
119 /* Should have at least two arguments */
120 usage();
121 exit(127);
122 }
123
124 reg = strtoul(argv[optind++], NULL, 0);
125
126 if (cpu == -1) {
127 doing_for_all = 1;
128 wrmsr_on_all_cpus(reg, argc - optind, &argv[optind]);
129 } else {
130 wrmsr_on_cpu(reg, cpu, argc - optind, &argv[optind]);
131 }
132
133 exit(0);
134 }
135
136 void wrmsr_on_cpu(uint32_t reg, int cpu, int valcnt, char *regvals[])
137 {
138 uint64_t data;
139 int fd;
140 char msr_file_name[64];
141
142 sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
143 fd = open(msr_file_name, O_WRONLY);
144 if (fd < 0) {
145 if (errno == ENXIO) {
146 if (doing_for_all)
147 return;
148 fprintf(stderr, "wrmsr: No CPU %d\n", cpu);
149 exit(2);
150 } else if (errno == EIO) {
151 fprintf(stderr, "wrmsr: CPU %d doesn't support MSRs\n",
152 cpu);
153 exit(3);
154 } else {
155 perror("wrmsr: open");
156 exit(127);
157 }
158 }
159
160 while (valcnt--) {
161 data = strtoull(*regvals++, NULL, 0);
162 if (pwrite(fd, &data, sizeof data, reg) != sizeof data) {
163 if (errno == EIO) {
164 fprintf(stderr,
165 "wrmsr: CPU %d cannot set MSR "
166 "0x%08"PRIx32" to 0x%016"PRIx64"\n",
167 cpu, reg, data);
168 exit(4);
169 } else {
170 perror("wrmsr: pwrite");
171 exit(127);
172 }
173 }
174 }
175
176 close(fd);
177
178 return;
179 }