| 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"cxx . . $ `<file>/version.h`/ |
| 32 |
|
| 33 |
static const struct optioncxx . . $ option# long_optionscxx . . $ 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_optionscxx . . $ short_options.[] = "hVap:"; |
| 42 |
static int doing_for_allcxx . . $ doing_for_all. = 0; |
| 43 |
|
| 44 |
const char *programcxx . . $ program.; |
| 45 |
|
| 46 |
void usagecxx . . $ usage(49f6e7a06ebc5aa8).(void) |
| 47 |
{ |
| 48 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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 |
programcxx . . $ program.); |
| 54 |
} |
| 55 |
|
| 56 |
void wrmsr_on_cpucxx . . $ wrmsr_on_cpu(71512a53b353d8d).(uint32_tcxx . . $ uint32_t# reg, int cpu, int valcnt, char *regvals[]); |
| 57 |
|
| 58 |
/* filter out ".", "..", "microcode" in /dev/cpu */ |
| 59 |
int dir_filtercxx . . $ dir_filter(39071f79bcec8c).(const struct direntcxx . . $ dirent# *dirp) |
| 60 |
{ |
| 61 |
if (isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!(dirp->d_name[0])) |
| 62 |
return 1; |
| 63 |
else |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
void wrmsr_on_all_cpuscxx . . $ wrmsr_on_all_cpus(7069f00509ac9c79).(uint32_tcxx . . $ uint32_t# reg, int valcnt, char *regvals[]) |
| 68 |
{ |
| 69 |
struct direntcxx . . $ dirent# **namelist; |
| 70 |
int dir_entries; |
| 71 |
|
| 72 |
dir_entries = scandircxx . . $ scandir(80db5f6f22a33983).("/dev/cpu", &namelist, dir_filtercxx . . $ dir_filter(39071f79bcec8c)., 0); |
| 73 |
while (dir_entries--) { |
| 74 |
wrmsr_on_cpucxx . . $ wrmsr_on_cpu(71512a53b353d8d).(reg, atoicxx . . $ atoi(5c4306d8553b3696).(namelist[dir_entries]->d_namecxx . . $ dirent#d_name.), |
| 75 |
valcnt, regvals); |
| 76 |
freecxx . . $ free(5d22bdacc48458e8).(namelist[dir_entries]); |
| 77 |
} |
| 78 |
freecxx . . $ free(5d22bdacc48458e8).(namelist); |
| 79 |
} |
| 80 |
|
| 81 |
int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 82 |
{ |
| 83 |
uint32_tcxx . . $ uint32_t# reg; |
| 84 |
int c; |
| 85 |
int cpu = 0; |
| 86 |
unsigned long arg; |
| 87 |
char *endarg; |
| 88 |
|
| 89 |
programcxx . . $ program. = argv[0]; |
| 90 |
|
| 91 |
while ((c = getopt_longcxx . . $ getopt_long(41f665c090816971).(argc, argv, short_optionscxx . . $ short_options., |
| 92 |
long_optionscxx . . $ long_options., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)) != -1) { |
| 93 |
switch (c) { |
| 94 |
case 'h': |
| 95 |
usagecxx . . $ usage(49f6e7a06ebc5aa8).(); |
| 96 |
exitcxx . . $ exit(3ca292c338df9bb3).(0); |
| 97 |
case 'V': |
| 98 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: version %s\n", programcxx . . $ program., |
| 99 |
VERSION_STRINGcxx . . $ `version.h:3:9`!); |
| 100 |
exitcxx . . $ exit(3ca292c338df9bb3).(0); |
| 101 |
case 'a': |
| 102 |
cpu = -1; |
| 103 |
break; |
| 104 |
case 'p': |
| 105 |
arg = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., &endarg, 0); |
| 106 |
if (*endarg || arg > 5119) { |
| 107 |
usagecxx . . $ usage(49f6e7a06ebc5aa8).(); |
| 108 |
exitcxx . . $ exit(3ca292c338df9bb3).(127); |
| 109 |
} |
| 110 |
cpu = (int)arg; |
| 111 |
break; |
| 112 |
default: |
| 113 |
usagecxx . . $ usage(49f6e7a06ebc5aa8).(); |
| 114 |
exitcxx . . $ exit(3ca292c338df9bb3).(127); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if (optindcxx . . $ optind. > argc - 2) { |
| 119 |
/* Should have at least two arguments */ |
| 120 |
usagecxx . . $ usage(49f6e7a06ebc5aa8).(); |
| 121 |
exitcxx . . $ exit(3ca292c338df9bb3).(127); |
| 122 |
} |
| 123 |
|
| •124 |
reg = strtoulcxx . . $ strtoul(31746e8d6ec49860).(argv[optindcxx . . $ optind.++], &endarg, 0); |
| 125 |
if (*endarg) { |
| 126 |
printfcxx . . $ printf(581fddd72a8db8fc).("Failed to parse register number.\n"); |
| 127 |
exitcxx . . $ exit(3ca292c338df9bb3).(127); |
| 128 |
} |
| 129 |
|
| 130 |
if (cpu == -1) { |
| 131 |
doing_for_allcxx . . $ doing_for_all. = 1; |
| 132 |
wrmsr_on_all_cpuscxx . . $ wrmsr_on_all_cpus(7069f00509ac9c79).(reg, argc - optindcxx . . $ optind., &argv[optindcxx . . $ optind.]); |
| 133 |
} else { |
| 134 |
wrmsr_on_cpucxx . . $ wrmsr_on_cpu(71512a53b353d8d).(reg, cpu, argc - optindcxx . . $ optind., &argv[optindcxx . . $ optind.]); |
| 135 |
} |
| 136 |
|
| 137 |
exitcxx . . $ exit(3ca292c338df9bb3).(0); |
| 138 |
} |
| 139 |
|
| 140 |
void wrmsr_on_cpucxx . . $ wrmsr_on_cpu(71512a53b353d8d).(uint32_tcxx . . $ uint32_t# reg, int cpu, int valcnt, char *regvals[]) |
| 141 |
{ |
| 142 |
uint64_tcxx . . $ uint64_t# data; |
| 143 |
int fd; |
| 144 |
char msr_file_name[64]; |
| 145 |
|
| 146 |
sprintfcxx . . $ sprintf(26c8642d26409d6d).(msr_file_name, "/dev/cpu/%d/msr", cpu); |
| 147 |
fd = opencxx . . $ open(e156e3aa1e5c4ad1).(msr_file_name, O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`!); |
| 148 |
if (fd < 0) { |
| 149 |
if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENXIOcxx . . $ `<external>/8987895939074593727/errno-base.h:10:9`!) { |
| 150 |
if (doing_for_allcxx . . $ doing_for_all.) |
| 151 |
return; |
| 152 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "wrmsr: No CPU %d\n", cpu); |
| 153 |
exitcxx . . $ exit(3ca292c338df9bb3).(2); |
| 154 |
} else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!) { |
| 155 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "wrmsr: CPU %d doesn't support MSRs\n", |
| 156 |
cpu); |
| 157 |
exitcxx . . $ exit(3ca292c338df9bb3).(3); |
| 158 |
} else { |
| 159 |
perrorcxx . . $ perror(85c52e162fed56f9).("wrmsr: open"); |
| 160 |
exitcxx . . $ exit(3ca292c338df9bb3).(127); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
while (valcnt--) { |
| 165 |
data = strtoullcxx . . $ strtoull(fa707f2e7d9ad629).(*regvals++, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0); |
| 166 |
if (pwritecxx . . $ pwrite(6a64eb0a75db039).(fd, &data, sizeof data, reg) != sizeof data) { |
| 167 |
if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!) { |
| 168 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, |
| 169 |
"wrmsr: CPU %d cannot set MSR " |
| 170 |
"0x%08"PRIx32cxx . . $ `<external>/15933738808807975373/inttypes.h:120:10`!" to 0x%016"PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:121:10`!"\n", |
| 171 |
cpu, reg, data); |
| 172 |
exitcxx . . $ exit(3ca292c338df9bb3).(4); |
| 173 |
} else { |
| 174 |
perrorcxx . . $ perror(85c52e162fed56f9).("wrmsr: pwrite"); |
| 175 |
exitcxx . . $ exit(3ca292c338df9bb3).(127); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
closecxx . . $ close(7864480464b09eea).(fd); |
| 181 |
|
| 182 |
return; |
| 183 |
} |