forked from jborza/emuriscv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.c
337 lines (308 loc) · 9.71 KB
/
monitor.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "monitor.h"
#include <string.h>
#include <stdio.h>
#include "memory.h"
#include <stdlib.h>
#include "disassembler/disassembler.h"
#include "debug_symbols.h"
#include "loadsave.h"
static char* reg_name[32] = {
"zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2",
"s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5",
"a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7",
"s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6"
};
static char* privilege_name[4] = {
"U", "S", "?", "M"
};
// monitor usage:
// x Displays memory at the specified virtual address using the specified format.
// xp Displays memory at the specified physical address using the specified format.
// xp /3i $eip
#define UNKNOWN_REGISTER -1
int find_register(char* name) {
char xname[4];
for (int i = 0; i < REGISTERS; i++) {
sprintf(xname, "x%d", i);
if (strcmp(xname, name) == 0 || strcmp(reg_name[i], name) == 0)
return i;
}
return UNKNOWN_REGISTER;
}
word get_register_value(State* state, char* name) {
if (strcmp(name, "pc") == 0) {
return state->pc;
}
int register_index = find_register(name);
if (register_index == UNKNOWN_REGISTER)
{
printf("Unknown register: %s\n", name);
return 0;
}
return state->x[register_index];
}
void dump_registers(State* state) {
char xname[4];
for (int i = 0; i < REGISTERS; i++) {
sprintf(xname, "x%d", i);
printf("%5s (%3s) : 0x%08x ", reg_name[i], xname, state->x[i]);
if (i % 4 == 3)
printf("\n");
}
printf("pc: %08x", state->pc);
printf(" priv: %s", privilege_name[state->privilege]);
printf(" cycles: %ld", state->instruction_counter);
printf("\n");
}
int dump_data_as_string_check_terminated(word data);
void dump_value(word data) {
printf("%08x | %ld | ", data, data);
dump_data_as_string_check_terminated(data);
printf("\n");
}
void dump_register(State* state, char* name) {
if (strcmp(name, "pc") == 0) {
printf("pc: ");
dump_value(state->pc);
return;
}
int register_index = find_register(name);
if (register_index == UNKNOWN_REGISTER)
{
printf("Unknown register: %s\n", name);
return;
}
printf("%5s (x%d): ", reg_name[register_index], register_index);
dump_value(state->x[register_index]);
}
void dump_one_memory_value(word address, word data) {
printf("0x%08x: ", address);
dump_value(data);
}
void dump_memory_physical(State* state, word start_address, size_t count) {
//data_format: 'x' for hex, 'd' for decimal, 'u' for unsigned decimal, 'o' for octal, 'c' for char and 'i' for (disassembled) processor instructions
for (word address = start_address; address < start_address + count * WORD_SIZE; address += WORD_SIZE) {
word data = read_word_physical(state, address);
dump_one_memory_value(address, data);
}
}
void dump_memory_virtual(State* state, word start_address, size_t count) {
MemoryTarget memory_target;
for (word address = start_address; address < start_address + count * WORD_SIZE; address += WORD_SIZE) {
int read_status = get_memory_target(state, address, LOAD, &memory_target);
if (read_status != TRANSLATE_OK)
printf("Cannot translate virtual address %08x\n", address);
word data = read_word(state, address);
dump_one_memory_value(address, data);
}
}
void dump_backtrace(State* state) {
MemoryTarget memory_target;
for (int i = 0; i < PC_HISTORY_DEPTH; i++) {
word address = state->pc_history[i];
int read_status = get_memory_target(state, address, LOAD, &memory_target);
if (read_status != TRANSLATE_OK)
printf("Cannot translate virtual address %08x\n", address);
word data = read_word(state, address);
printf("%8x:\t%08x\t\t", address, data);
disassemble_op(&data, address);
}
}
void disassemble_word(State* state, word start_address, size_t count) {
MemoryTarget memory_target;
for (word address = start_address; address < start_address + count * WORD_SIZE; address += WORD_SIZE) {
int read_status = get_memory_target(state, address, LOAD, &memory_target);
if (read_status != TRANSLATE_OK)
printf("Cannot translate virtual address %08x\n", address);
word data = read_word(state, address);
printf("%8x:\t%08x\t\t", address, data);
disassemble_op(&data, address);
}
}
int dump_data_as_string_check_terminated(word data) {
unsigned char bytes[5];
bytes[4] = 0;
bytes[3] = (data >> 24) & 0xFF;
bytes[2] = (data >> 16) & 0xFF;
bytes[1] = (data >> 8) & 0xFF;
bytes[0] = data & 0xFF;
printf("%s", bytes);
return (bytes[0] == 0 || bytes[1] == 0 || bytes[2] == 0 || bytes[3] == 0);
}
void dump_string_virtual(State* state, word start_address) {
MemoryTarget memory_target;
//read until \0
printf("0x%08x: ", start_address);
for (word address = start_address; ; address += WORD_SIZE) {
int read_status = get_memory_target(state, address, LOAD, &memory_target);
if (read_status != TRANSLATE_OK) {
printf("Cannot translate virtual address %08x\n", address);
break;
}
word data = read_word(state, address);
if (dump_data_as_string_check_terminated(data)) {
printf("\n");
break;
}
}
}
void write_memory_virtual(State* state, word address, word value) {
MemoryTarget memory_target;
int read_status = get_memory_target(state, address, LOAD, &memory_target);
if (read_status != TRANSLATE_OK)
printf("Cannot translate virtual address %08x\n", address);
write_word(state, address, value);
printf("ok.\n");
}
word get_address(char* token) {
return strtoul(token, NULL, 16);
}
word get_value_hex(char* token) {
return strtoul(token, NULL, 16);
}
word get_repeat(size_t token_count, char** tokens) {
size_t repeat = 1;
if (token_count > 2)
repeat = atoi(tokens[2]);
return repeat;
}
int run_monitor_loop(State* state) {
printf("(emuriscv) ");
char buffer[64];
char* tokens[10];
if (fgets(buffer, 64, stdin) == NULL)
return -1;
buffer[strcspn(buffer, "\n")] = 0;
char* line = _strdup(buffer);
if (strlen(line) == 0)
return 0;
size_t token_count = 0;
for (token_count = 0; token_count < 10; token_count++) {
char* arg = token_count == 0 ? line : NULL;
tokens[token_count] = strtok(arg, " ");
if (tokens[token_count] == NULL)
break;
}
if (strcmp(tokens[0], "help") == 0 || strcmp(tokens[0], "?") == 0) {
printf("help:\n");
printf("regs - dump registers\n");
printf("reg $reg - dump one register $reg (format: x7 or t0)\n");
printf("x $addr [n] - dump n words from virtual address $addr\n");
printf("xp $addr [n] - dump n words from physical address $addr\n");
printf("xs $addr [n] - print a null terminated string from physical address $addr\n");
printf("w $addr $val - write 1 words with value $val to from virtual address $addr\n");
printf("p $reg [n] - dump n words from virtual address pointed to by $reg\n");
printf("pp $reg [n] - dump n words from physical address pointed to by $reg\n");
printf("d $addr [n] - disassembler n words from virtual address $addr\n");
printf("dr $reg [n] - disassembler n words from virtual address pointed to by $reg\n");
printf("step [n] - do n instructions\n");
printf("bt - print and disassemble the backtrace\n");
printf("sym - prints the current symbol\n");
printf("save [file] - save the state to a file\n");
printf("load [file] - load the state from a file\n");
printf("q - quit\n");
}
else if (strcmp(tokens[0], "q") == 0) {
return 1;
}
else if (strcmp(tokens[0], "regs") == 0) {
dump_registers(state);
}
else if (strcmp(tokens[0], "reg") == 0) {
if (token_count > 1)
dump_register(state, tokens[1]);
}
else if (strcmp(tokens[0], "p") == 0) {
if (token_count > 1) {
word address = get_register_value(state, tokens[1]);
size_t repeat = get_repeat(token_count, tokens);
dump_memory_virtual(state, address, repeat);
}
}
else if (strcmp(tokens[0], "pp") == 0) {
if (token_count > 1) {
word address = get_register_value(state, tokens[1]);
size_t repeat = get_repeat(token_count, tokens);
dump_memory_physical(state, address, repeat);
}
}
else if (strcmp(tokens[0], "x") == 0) {
if (token_count > 1) {
word address = get_address(tokens[1]);
size_t repeat = get_repeat(token_count, tokens);
dump_memory_virtual(state, address, repeat);
}
}
else if (strcmp(tokens[0], "xs") == 0) {
if (token_count > 1) {
word address = get_address(tokens[1]);
dump_string_virtual(state, address);
}
}
else if (strcmp(tokens[0], "xp") == 0) {
word address = strtoul(tokens[1], NULL, 16);
size_t repeat = 1;
if (token_count > 2)
repeat = atoi(tokens[2]);
dump_memory_physical(state, address, repeat);
}
else if (strcmp(tokens[0], "w") == 0) {
if (token_count > 2) {
word address = get_address(tokens[1]);
word value = get_value_hex(tokens[2]);
write_memory_virtual(state, address, value);
}
}
else if (strcmp(tokens[0], "d") == 0) {
if (token_count > 1) {
word address = get_address(tokens[1]);
size_t repeat = get_repeat(token_count, tokens);
disassemble_word(state, address, repeat);
}
}
else if (strcmp(tokens[0], "dr") == 0) {
if (token_count > 1) {
word address = get_register_value(state, tokens[1]);
size_t repeat = get_repeat(token_count, tokens);
disassemble_word(state, address, repeat);
}
}
else if (strcmp(tokens[0], "step") == 0) {
if (token_count > 1) {
size_t repeat = 1;
if (token_count > 1)
repeat = atoi(tokens[1]);
for (size_t i = 0; i < repeat; i++) {
//TODO hack, should be probably somewhere else
emulate_op(state);
printf(".");
}
printf("\n");
}
}
else if (strcmp(tokens[0], "bt") == 0) {
dump_backtrace(state);
}
else if (strcmp(tokens[0], "sym") == 0) {
symbol* symbol = get_symbol(symbol_list, state->pc);
printf(" %s \n", symbol->name);
}
else if (strcmp(tokens[0], "save") == 0) {
if (token_count > 1) {
save(state, tokens[1]);
}
}
else if (strcmp(tokens[0], "load") == 0) {
if (token_count > 1) {
load(state, tokens[1]);
}
}
return 0;
}
void run_monitor(State* state) {
for (;;)
{
if (run_monitor_loop(state) != 0)
return;
}
}