| 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++], &endarg, 0); |
| 125 |
if (*endarg) { |
| 126 |
printf("Failed to parse register number.\n"); |
| 127 |
exit(127); |
| 128 |
} |
| 129 |
|
| 130 |
if (cpu == -1) { |
| 131 |
doing_for_all = 1; |
| 132 |
wrmsr_on_all_cpus(reg, argc - optind, &argv[optind]); |
| 133 |
} else { |
| 134 |
wrmsr_on_cpu(reg, cpu, argc - optind, &argv[optind]); |
| 135 |
} |
| 136 |
|
| 137 |
exit(0); |
| 138 |
} |
| 139 |
|
| 140 |
void wrmsr_on_cpu(uint32_t reg, int cpu, int valcnt, char *regvals[]) |
| 141 |
{ |
| 142 |
uint64_t data; |
| 143 |
int fd; |
| 144 |
char msr_file_name[64]; |
| 145 |
|
| 146 |
sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu); |
| 147 |
fd = open(msr_file_name, O_WRONLY); |
| 148 |
if (fd < 0) { |
| 149 |
if (errno == ENXIO) { |
| 150 |
if (doing_for_all) |
| 151 |
return; |
| 152 |
fprintf(stderr, "wrmsr: No CPU %d\n", cpu); |
| 153 |
exit(2); |
| 154 |
} else if (errno == EIO) { |
| 155 |
fprintf(stderr, "wrmsr: CPU %d doesn't support MSRs\n", |
| 156 |
cpu); |
| 157 |
exit(3); |
| 158 |
} else { |
| 159 |
perror("wrmsr: open"); |
| 160 |
exit(127); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
while (valcnt--) { |
| 165 |
data = strtoull(*regvals++, NULL, 0); |
| 166 |
if (pwrite(fd, &data, sizeof data, reg) != sizeof data) { |
| 167 |
if (errno == EIO) { |
| 168 |
fprintf(stderr, |
| 169 |
"wrmsr: CPU %d cannot set MSR " |
| 170 |
"0x%08"PRIx32" to 0x%016"PRIx64"\n", |
| 171 |
cpu, reg, data); |
| 172 |
exit(4); |
| 173 |
} else { |
| 174 |
perror("wrmsr: pwrite"); |
| 175 |
exit(127); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
close(fd); |
| 181 |
|
| 182 |
return; |
| 183 |
} |