-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathmain.c
302 lines (264 loc) · 8.08 KB
/
main.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
/*
* rv32emu is freely redistributable under the MIT License. See the file
* "LICENSE" for information on usage and redistribution of this file.
*/
#include <assert.h>
#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "elf.h"
#include "riscv.h"
#include "utils.h"
/* enable program trace mode */
#if !RV32_HAS(SYSTEM) || (RV32_HAS(SYSTEM) && RV32_HAS(ELF_LOADER))
static bool opt_trace = false;
#endif
#if RV32_HAS(GDBSTUB)
/* enable program gdbstub mode */
static bool opt_gdbstub = false;
#endif
/* dump registers as JSON */
static bool opt_dump_regs = false;
static char *registers_out_file;
/* RISC-V arch-test */
static bool opt_arch_test = false;
static char *signature_out_file;
/* Quiet outputs */
static bool opt_quiet_outputs = false;
/* target executable */
static char *opt_prog_name;
/* target argc and argv */
static int prog_argc;
static char **prog_args;
static const char *optstr = "tgqmhpd:a:k:i:b:x:";
/* enable misaligned memory access */
static bool opt_misaligned = false;
/* dump profiling data */
static bool opt_prof_data = false;
static char *prof_out_file;
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
/* Linux kernel data */
static char *opt_kernel_img;
static char *opt_rootfs_img;
static char *opt_bootargs;
static char *opt_virtio_blk_img;
#endif
static void print_usage(const char *filename)
{
rv_log_error(
"\nRV32I[MAFC] Emulator which loads an ELF file to execute.\n"
"Usage: %s [options] [filename] [arguments]\n"
"Options:\n"
#if !RV32_HAS(SYSTEM) || (RV32_HAS(SYSTEM) && RV32_HAS(ELF_LOADER))
" -t : print executable trace\n"
#endif
#if RV32_HAS(GDBSTUB)
" -g : allow remote GDB connections (as gdbstub)\n"
#endif
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
" -k <image> : use <image> as kernel image\n"
" -i <image> : use <image> as rootfs\n"
" -x vblk:<image> : use <image> as virtio-blk disk image\n"
" -b <bootargs> : use customized <bootargs> for the kernel\n"
#endif
" -d [filename]: dump registers as JSON to the "
"given file or `-` (STDOUT)\n"
" -q : Suppress outputs other than `dump-registers`\n"
" -a [filename] : dump signature to the given file, "
"required by arch-test test\n"
" -m : enable misaligned memory access\n"
" -p : generate profiling data\n"
" -h : show this message",
filename);
}
static bool parse_args(int argc, char **args)
{
int opt;
int emu_argc = 0;
while ((opt = getopt(argc, args, optstr)) != -1) {
emu_argc++;
switch (opt) {
#if !RV32_HAS(SYSTEM) || (RV32_HAS(SYSTEM) && RV32_HAS(ELF_LOADER))
case 't':
opt_trace = true;
break;
#endif
#if RV32_HAS(GDBSTUB)
case 'g':
opt_gdbstub = true;
break;
#endif
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
case 'k':
opt_kernel_img = optarg;
emu_argc++;
break;
case 'i':
opt_rootfs_img = optarg;
emu_argc++;
break;
case 'b':
opt_bootargs = optarg;
emu_argc++;
break;
case 'x':
if (!strncmp("vblk:", optarg, 5))
opt_virtio_blk_img = optarg + 5; /* strlen("vblk:") */
else
return false;
emu_argc++;
break;
#endif
case 'q':
opt_quiet_outputs = true;
break;
case 'h':
return false;
case 'm':
opt_misaligned = true;
break;
case 'p':
opt_prof_data = true;
break;
case 'd':
opt_dump_regs = true;
registers_out_file = optarg;
emu_argc++;
break;
case 'a':
opt_arch_test = true;
signature_out_file = optarg;
emu_argc++;
break;
default:
return false;
}
}
prog_argc = argc - emu_argc - 1;
/* optind points to the first non-option string, so it should indicate the
* target program.
*/
prog_args = &args[optind];
opt_prog_name = prog_args[0];
if (opt_prof_data) {
char cwd_path[PATH_MAX] = {0};
assert(getcwd(cwd_path, PATH_MAX));
char rel_path[PATH_MAX] = {0};
memcpy(rel_path, args[0], strlen(args[0]) - 7 /* strlen("rv32emu") */);
char *prog_basename = basename(opt_prog_name);
prof_out_file = malloc(strlen(cwd_path) + 1 + strlen(rel_path) +
strlen(prog_basename) + 5 + 1);
assert(prof_out_file);
sprintf(prof_out_file, "%s/%s%s.prof", cwd_path, rel_path,
prog_basename);
}
return true;
}
static void dump_test_signature(const char *prog_name)
{
elf_t *elf = elf_new();
assert(elf && elf_open(elf, prog_name));
uint32_t start = 0, end = 0;
const struct Elf32_Sym *sym;
FILE *f = fopen(signature_out_file, "w");
if (!f) {
rv_log_fatal("Cannot open signature output file: %s",
signature_out_file);
return;
}
/* use the entire .data section as a fallback */
elf_get_data_section_range(elf, &start, &end);
/* try and access the exact signature range */
if ((sym = elf_get_symbol(elf, "begin_signature")))
start = sym->st_value;
if ((sym = elf_get_symbol(elf, "end_signature")))
end = sym->st_value;
/* dump it word by word */
for (uint32_t addr = start; addr < end; addr += 4)
fprintf(f, "%08x\n", memory_read_w(addr));
fclose(f);
elf_delete(elf);
}
/* CYCLE_PER_STEP shall be defined on different runtime */
#ifndef CYCLE_PER_STEP
#define CYCLE_PER_STEP 100
#endif
/* FIXME: MEM_SIZE shall be defined on different runtime */
#ifndef MEM_SIZE
/* Allocate 2^{19} bytes, which is ample for all selective benchmarks. */
#define MEM_SIZE 0x80000ULL
#endif
#define STACK_SIZE 0x1000 /* 4096 */
#define ARGS_OFFSET_SIZE 0x1000 /* 4096 */
/* To use rv_halt function in wasm, we have to expose RISC-V instance(rv),
* but we can add a layer to not expose the instance and make rv_halt
* callable. A small trade-off is that declaring instance as a global
* variable. rv_halt is useful when cancelling the main loop of wasm,
* see rv_step in emulate.c for more detail
*/
riscv_t *rv;
#ifdef __EMSCRIPTEN__
void indirect_rv_halt()
{
rv_halt(rv);
}
#endif
int main(int argc, char **args)
{
if (argc == 1 || !parse_args(argc, args)) {
print_usage(args[0]);
return 1;
}
int run_flag = 0;
#if !RV32_HAS(SYSTEM) || (RV32_HAS(SYSTEM) && RV32_HAS(ELF_LOADER))
run_flag |= opt_trace;
#endif
#if RV32_HAS(GDBSTUB)
run_flag |= opt_gdbstub << 1;
#endif
run_flag |= opt_prof_data << 2;
vm_attr_t attr = {
.mem_size = MEM_SIZE,
.stack_size = STACK_SIZE,
.args_offset_size = ARGS_OFFSET_SIZE,
.argc = prog_argc,
.argv = prog_args,
.log_level = LOG_TRACE,
.run_flag = run_flag,
.profile_output_file = prof_out_file,
.cycle_per_step = CYCLE_PER_STEP,
.allow_misalign = opt_misaligned,
};
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
attr.data.system.kernel = opt_kernel_img;
attr.data.system.initrd = opt_rootfs_img;
attr.data.system.bootargs = opt_bootargs;
attr.data.system.vblk_device = opt_virtio_blk_img;
#else
attr.data.user.elf_program = opt_prog_name;
#endif
/* create the RISC-V runtime */
rv = rv_create(&attr);
if (!rv) {
rv_log_fatal("Unable to create riscv emulator");
attr.exit_code = 1;
goto end;
}
rv_log_info("RISC-V emulator is created and ready to run");
rv_run(rv);
/* dump registers as JSON */
if (opt_dump_regs)
dump_registers(rv, registers_out_file);
/* dump test result in test mode */
if (opt_arch_test)
dump_test_signature(opt_prog_name);
/* finalize the RISC-V runtime */
rv_delete(rv);
rv_log_info("RISC-V emulator is destroyed");
end:
free(prof_out_file);
return attr.exit_code;
}