This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
/
wrmsr.c
179 lines (157 loc) · 3.75 KB
/
wrmsr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* ----------------------------------------------------------------------- *
*
* Copyright 2000 Transmeta Corporation - All Rights Reserved
* Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston MA 02110-1301, USA; either version 2 of the License, or
* (at your option) any later version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
/*
* wrmsr.c
*
* Utility to write to an MSR.
*/
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include "version.h"
static const struct option long_options[] = {
{"help", 0, 0, 'h'},
{"version", 0, 0, 'V'},
{"all", 0, 0, 'a'},
{"processor", 1, 0, 'p'},
{"cpu", 1, 0, 'p'},
{0, 0, 0, 0}
};
static const char short_options[] = "hVap:";
static int doing_for_all = 0;
const char *program;
void usage(void)
{
fprintf(stderr, "Usage: %s [options] regno value...\n"
" --help -h Print this help\n"
" --version -V Print current version\n"
" --all -a all processors\n"
" --processor # -p Select processor number (default 0)\n",
program);
}
void wrmsr_on_cpu(uint32_t reg, int cpu, int valcnt, char *regvals[]);
/* filter out ".", "..", "microcode" in /dev/cpu */
int dir_filter(const struct dirent *dirp)
{
if (isdigit(dirp->d_name[0]))
return 1;
else
return 0;
}
void wrmsr_on_all_cpus(uint32_t reg, int valcnt, char *regvals[])
{
struct dirent **namelist;
int dir_entries;
dir_entries = scandir("/dev/cpu", &namelist, dir_filter, 0);
while (dir_entries--) {
wrmsr_on_cpu(reg, atoi(namelist[dir_entries]->d_name),
valcnt, regvals);
free(namelist[dir_entries]);
}
free(namelist);
}
int main(int argc, char *argv[])
{
uint32_t reg;
int c;
int cpu = 0;
unsigned long arg;
char *endarg;
program = argv[0];
while ((c = getopt_long(argc, argv, short_options,
long_options, NULL)) != -1) {
switch (c) {
case 'h':
usage();
exit(0);
case 'V':
fprintf(stderr, "%s: version %s\n", program,
VERSION_STRING);
exit(0);
case 'a':
cpu = -1;
break;
case 'p':
arg = strtoul(optarg, &endarg, 0);
if (*endarg || arg > 5119) {
usage();
exit(127);
}
cpu = (int)arg;
break;
default:
usage();
exit(127);
}
}
if (optind > argc - 2) {
/* Should have at least two arguments */
usage();
exit(127);
}
reg = strtoul(argv[optind++], NULL, 0);
if (cpu == -1) {
doing_for_all = 1;
wrmsr_on_all_cpus(reg, argc - optind, &argv[optind]);
} else {
wrmsr_on_cpu(reg, cpu, argc - optind, &argv[optind]);
}
exit(0);
}
void wrmsr_on_cpu(uint32_t reg, int cpu, int valcnt, char *regvals[])
{
uint64_t data;
int fd;
char msr_file_name[64];
sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
fd = open(msr_file_name, O_WRONLY);
if (fd < 0) {
if (errno == ENXIO) {
if (doing_for_all)
return;
fprintf(stderr, "wrmsr: No CPU %d\n", cpu);
exit(2);
} else if (errno == EIO) {
fprintf(stderr, "wrmsr: CPU %d doesn't support MSRs\n",
cpu);
exit(3);
} else {
perror("wrmsr: open");
exit(127);
}
}
while (valcnt--) {
data = strtoull(*regvals++, NULL, 0);
if (pwrite(fd, &data, sizeof data, reg) != sizeof data) {
if (errno == EIO) {
fprintf(stderr,
"wrmsr: CPU %d cannot set MSR "
"0x%08"PRIx32" to 0x%016"PRIx64"\n",
cpu, reg, data);
exit(4);
} else {
perror("wrmsr: pwrite");
exit(127);
}
}
}
close(fd);
return;
}