forked from alibaba/system-register-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrasr.c
282 lines (259 loc) · 6.36 KB
/
wrasr.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* wrasr.c
*
* Utility to write data to AArch64 system register.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include "version.h"
#include "sysreg.h"
struct option long_options[] = {
{"help", 0, 0, 'h'},
{"version", 0, 0, 'V'},
{"hexadecimal", 0, 0, 'x'},
{"capital-hexadecimal", 0, 0, 'X'},
{"decimal", 0, 0, 'd'},
{"signed-decimal", 0, 0, 'd'},
{"unsigned-decimal", 0, 0, 'u'},
{"octal", 0, 0, 'o'},
{"zero-fill", 0, 0, '0'},
{"zero-pad", 0, 0, '0'},
{"register", 0, 0, 'r'},
{"processor", 1, 0, 'p'},
{"bitfield", 1, 0, 'f'},
{0, 0, 0, 0}
};
const char *program;
struct register_t *global_register;
char *TABLEPATH; /* the path for regs.table */
char *TABLENAME = ".regstable"; /* the path for regs.table */
void usage(void)
{
fprintf(stderr,
"Usage: %s [options] register\n"
" --help -h Print this help\n"
" --version -V Print current version\n"
" --hexadecimal -x Hexadecimal output (lower case)\n"
" --capital-hex -X Hexadecimal output (upper case)\n"
" --decimal -d Signed decimal output\n"
" --unsigned -u Unsigned decimal output\n"
" --octal -o Octal output\n"
" --zero-pad -0 Output leading zeroes\n"
" --processor # -p Select processor number (default 0)\n"
" --bitfield h:l -f Output bits [h:l] only\n"
" --register # -r Specify register name\n"
"\n"
" example:\n"
" Read data from the specify register:\n"
"\n"
" $rdasr -p0 -r MPIDR_EL1\n"
" 0x80000002\n"
"\n"
" Write data to the specify register:\n"
"\n"
" $wrasr -p0 -r MPIDR_EL1 <data>\n"
" 0x80000006\n",
program);
}
void islegal(struct register_t *regs)
{
int op0, op1, cn, cm, op2;
op0 = sys_reg_Op0(regs->regcode);
op1 = sys_reg_Op1(regs->regcode);
cn = sys_reg_CRn(regs->regcode);
cm = sys_reg_CRm(regs->regcode);
op2 = sys_reg_Op2(regs->regcode);
if ((op0 <= MAX_OP0) && (op1 <= MAX_OP1)
&& (op2 <= MAX_OP2) && (cn <= MAX_CN) && (cm <= MAX_CM)) {
/* legal */
return;
}
else {
/* illegal */
perror("register name error\n");
exit(127);
}
}
/*
* traverse regs.table file and rearch for destination register
*/
int search(void)
{
char name[20], sys_name[20];
int op0, op1, cn, cm, op2;
unsigned int len = 0;
FILE *fp;
if ((fp = fopen(TABLEPATH, "r")) == NULL) {
printf("%s: No such file or can not read!\n", TABLEPATH);
return -1;
}
while (!feof(fp)) {
fscanf(fp, "%s %s", name, sys_name);
if (strcmp(name, global_register->name) == 0) {
/* find destination register */
len = strlen(sys_name);
op0 = atoi(&sys_name[1]);
op1 = atoi(&sys_name[3]);
op2 = atoi(&sys_name[len - 1]);
cn = atoi(&sys_name[6]);
cm = (sys_name[len-4] == 'c') ? atoi(&sys_name[len-3]):
(atoi(&sys_name[len-4]));
global_register->regcode = sys_reg(op0, op1, cn, cm, op2);
break;
}
}
fclose(fp);
if (strcmp(name, global_register->name) != 0) {
/* Not find destination register */
printf("Not find destination register!\n");
exit(127);
}
return 0;
}
void parse(const char *regname)
{
int status;
int op0, op1, cn, cm, op2;
int len = 0;
global_register = (struct register_t *)malloc(sizeof(struct register_t));
global_register->length = strlen(regname);
global_register->name = strdup(regname);
len = global_register->length;
/* check the format of register name */
if ((regname[0] == 'S') && (regname[5] == 'c')) {
global_register->type = 0;
op0 = atoi(®name[1]);
op1 = atoi(®name[3]);
op2 = atoi(®name[len - 1]);
cn = atoi(®name[6]);
cm = (regname[len-4] == 'c') ? atoi(®name[len-3]): \
(atoi(®name[len-4]));
global_register->regcode = sys_reg(op0, op1, cn, cm, op2);
}
else {
global_register->type = 1;
//TABLEPATH = getlogin();
status = search();
if (status == -1) {
/* Not find target register in regs.table */
exit(127);
}
}
islegal(global_register);
printf("register: %s\n", global_register->name);
}
void init(void)
{
char *home = getenv("HOME");
TABLEPATH = (char *)malloc(strlen(home) + strlen(TABLENAME) + 1);
sprintf(TABLEPATH, "%s/%s", home, TABLENAME);
}
int main(int argc, char *argv[])
{
uint32_t reg;
uint64_t data;
int c, fd;
int mode = OF_HEX | OF_C;
int cpu = 0;
unsigned int highbit = 63, lowbit = 0;
unsigned long arg;
char *endarg;
char msr_file_name[64];
program = argv[0];
init();
/* delete r */
while ((c = getopt_long(argc,argv, "hVxXdoc0p:f:r:", long_options,
NULL)) != -1) {
switch (c) {
case 'h':
usage();
exit(0);
case 'V':
fprintf(stderr, "%s: %s\n", program, VERSION_STRING);
exit(0);
case 'x':
mode = (mode & ~OF_MASK) | OF_HEX;
break;
case 'X':
mode = (mode & ~OF_MASK) | OF_CHX;
break;
case 'o':
mode = (mode & ~OF_MASK) | OF_OCT;
break;
case 'd':
mode = (mode & ~OF_MASK) | OF_DEC;
break;
case 'r':
/* parse the register name */
if (strlen(optarg) == 0) {
usage();
exit(127);
}
parse(optarg);
break;
case '0':
mode |= OF_FILL;
break;
case 'p':
arg = strtoul(optarg, &endarg, 0);
if ( *endarg || arg > 255 ) {
usage();
exit(127);
}
cpu = (int)arg;
break;
case 'f':
{
if ( sscanf(optarg, "%u:%u", &highbit, &lowbit) != 2 ||
highbit > 63 || lowbit > highbit ) {
usage();
exit(127);
}
}
break;
default:
usage();
exit(127);
}
}
if ((optind != argc-1) && (argc < 4)) {
/* Should have exactly one argument */
usage();
exit(127);
}
reg = global_register->regcode;
sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
fd = open(msr_file_name, O_RDWR);
if (fd < 0) {
if (errno == ENXIO) {
fprintf(stderr, "wrasr: No CPU %d\n", cpu);
fprintf(stderr, "please enter CPU number: 0~%d\n", get_nprocs()-1);
exit(2);
}
else if (errno == EIO) {
fprintf(stderr, "wrasr: CPU %d doesn't support MSRs\n", cpu);
exit(3);
}
else {
perror("wrasr:open");
fprintf(stderr, "please enter CPU number: 0~%d\n", get_nprocs()-1);
exit(127);
}
}
/* write */
data = strtoul(argv[optind], NULL, 0);
aarch64_write_register(fd, data, sizeof data, reg);
free(global_register);
free(TABLEPATH);
close(fd);
exit(0);
}