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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "elf.h"
#include "sail.h"
#include "rts.h"
#include "riscv_platform.h"
#include "riscv_platform_impl.h"
#include "riscv_sail.h"
#ifdef SPIKE
#include "tv_spike_intf.h"
#else
struct tv_spike_t;
#endif
/* Selected CSRs from riscv-isa-sim/riscv/encoding.h */
#define CSR_STVEC 0x105
#define CSR_SEPC 0x141
#define CSR_SCAUSE 0x142
#define CSR_STVAL 0x143
#define CSR_MSTATUS 0x300
#define CSR_MISA 0x301
#define CSR_MEDELEG 0x302
#define CSR_MIDELEG 0x303
#define CSR_MIE 0x304
#define CSR_MTVEC 0x305
#define CSR_MEPC 0x341
#define CSR_MCAUSE 0x342
#define CSR_MTVAL 0x343
#define CSR_MIP 0x344
static bool do_dump_dts = false;
struct tv_spike_t *s = NULL;
char *term_log = NULL;
static struct option options[] = {
{"enable-dirty", no_argument, 0, 'd'},
{"enable-misaligned", no_argument, 0, 'm'},
{"dump-dts", no_argument, 0, 's'},
{"terminal-log", required_argument, 0, 't'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
static void print_usage(const char *argv0, int ec)
{
fprintf(stdout, "Usage: %s [options] <elf_file>\n", argv0);
struct option *opt = options;
while (opt->name) {
fprintf(stdout, "\t -%c\t %s\n", (char)opt->val, opt->name);
opt++;
}
exit(ec);
}
char *process_args(int argc, char **argv)
{
int c, idx = 1;
while(true) {
c = getopt_long(argc, argv, "dmst:v:h", options, &idx);
if (c == -1) break;
switch (c) {
case 'd':
rv_enable_dirty_update = true;
break;
case 'm':
rv_enable_misaligned = true;
break;
case 's':
do_dump_dts = true;
break;
case 't':
term_log = strdup(optarg);
break;
case 'h':
print_usage(argv[0], 0);
break;
default:
fprintf(stderr, "Unrecognized optchar %c\n", c);
print_usage(argv[0], 1);
}
}
if (idx >= argc) print_usage(argv[0], 0);
if (term_log == NULL) term_log = strdup("term.log");
return argv[idx];
}
uint64_t load_sail(char *f)
{
bool is32bit;
uint64_t entry;
load_elf(f, &is32bit, &entry);
if (is32bit) {
fprintf(stderr, "32-bit RISC-V not yet supported.\n");
exit(1);
}
fprintf(stdout, "ELF Entry @ %lx\n", entry);
/* locate htif ports */
if (lookup_sym(f, "tohost", &rv_htif_tohost) < 0) {
fprintf(stderr, "Unable to locate htif tohost port.\n");
exit(1);
}
fprintf(stderr, "tohost located at %0" PRIx64 "\n", rv_htif_tohost);
return entry;
}
void init_spike(const char *f, uint64_t entry)
{
#ifdef SPIKE
s = tv_init("RV64IMAC");
tv_set_verbose(s, 1);
tv_set_dtb_in_rom(s, 1);
tv_load_elf(s, f);
tv_reset(s);
/* sync the insns per tick */
rv_insns_per_tick = tv_get_insns_per_tick(s);
#else
s = NULL;
#endif
}
void init_sail_reset_vector(uint64_t entry)
{
#define RST_VEC_SIZE 8
uint32_t reset_vec[RST_VEC_SIZE] = {
0x297, // auipc t0,0x0
0x28593 + (RST_VEC_SIZE * 4 << 20), // addi a1, t0, &dtb
0xf1402573, // csrr a0, mhartid
SAIL_XLEN == 32 ?
0x0182a283u : // lw t0,24(t0)
0x0182b283u, // ld t0,24(t0)
0x28067, // jr t0
0,
(uint32_t) (entry & 0xffffffff),
(uint32_t) (entry >> 32)
};
rv_rom_base = DEFAULT_RSTVEC;
uint64_t addr = rv_rom_base;
for (int i = 0; i < sizeof(reset_vec); i++)
write_mem(addr++, (uint64_t)((char *)reset_vec)[i]);
#ifdef SPIKE
const unsigned char *dtb = NULL;
int dtb_len;
tv_get_dtb(s, &dtb, &dtb_len);
fprintf(stderr, "Got %d bytes of dtb at %p\n", dtb_len, dtb);
for (int i = 0; i < dtb_len; i++)
write_mem(addr++, dtb[i]);
#else
fprintf(stderr, "Running without rom device tree.\n");
/* TODO: write DTB */
#endif
/* zero-fill to page boundary */
const int align = 0x1000;
uint64_t rom_end = (addr + align -1)/align * align;
for (int i = addr; i < rom_end; i++)
write_mem(addr++, 0);
/* set rom size */
rv_rom_size = rom_end - rv_rom_base;
/* boot at reset vector */
zPC = rv_rom_base;
}
void init_sail(uint64_t elf_entry)
{
model_init();
zinit_platform(UNIT);
zinit_sys(UNIT);
init_sail_reset_vector(elf_entry);
}
int init_check(struct tv_spike_t *s)
{
int passed = 1;
#ifdef SPIKE
passed &= tv_check_csr(s, CSR_MISA, zmisa.zMisa_chunk_0);
#endif
return passed;
}
void finish(int ec)
{
model_fini();
#ifdef SPIKE
tv_free(s);
#endif
exit(ec);
}
int compare_states(struct tv_spike_t *s)
{
int passed = 1;
#ifdef SPIKE
// fix default C enum map for cur_privilege
uint8_t priv = (zcur_privilege == 2) ? 3 : zcur_privilege;
passed &= tv_check_priv(s, priv);
passed &= tv_check_pc(s, zPC);
passed &= tv_check_gpr(s, 1, zx1);
passed &= tv_check_gpr(s, 2, zx2);
passed &= tv_check_gpr(s, 3, zx3);
passed &= tv_check_gpr(s, 4, zx4);
passed &= tv_check_gpr(s, 5, zx5);
passed &= tv_check_gpr(s, 6, zx6);
passed &= tv_check_gpr(s, 7, zx7);
passed &= tv_check_gpr(s, 8, zx8);
passed &= tv_check_gpr(s, 9, zx9);
passed &= tv_check_gpr(s, 10, zx10);
passed &= tv_check_gpr(s, 11, zx11);
passed &= tv_check_gpr(s, 12, zx12);
passed &= tv_check_gpr(s, 13, zx13);
passed &= tv_check_gpr(s, 14, zx14);
passed &= tv_check_gpr(s, 15, zx15);
passed &= tv_check_gpr(s, 15, zx15);
passed &= tv_check_gpr(s, 16, zx16);
passed &= tv_check_gpr(s, 17, zx17);
passed &= tv_check_gpr(s, 18, zx18);
passed &= tv_check_gpr(s, 19, zx19);
passed &= tv_check_gpr(s, 20, zx20);
passed &= tv_check_gpr(s, 21, zx21);
passed &= tv_check_gpr(s, 22, zx22);
passed &= tv_check_gpr(s, 23, zx23);
passed &= tv_check_gpr(s, 24, zx24);
passed &= tv_check_gpr(s, 25, zx25);
passed &= tv_check_gpr(s, 25, zx25);
passed &= tv_check_gpr(s, 26, zx26);
passed &= tv_check_gpr(s, 27, zx27);
passed &= tv_check_gpr(s, 28, zx28);
passed &= tv_check_gpr(s, 29, zx29);
passed &= tv_check_gpr(s, 30, zx30);
passed &= tv_check_gpr(s, 31, zx31);
/* some selected CSRs for now */
passed &= tv_check_csr(s, CSR_MCAUSE, zmcause.zMcause_chunk_0);
passed &= tv_check_csr(s, CSR_MEPC, zmepc);
passed &= tv_check_csr(s, CSR_MTVAL, zmtval);
passed &= tv_check_csr(s, CSR_SCAUSE, zscause.zMcause_chunk_0);
passed &= tv_check_csr(s, CSR_SEPC, zsepc);
passed &= tv_check_csr(s, CSR_STVAL, zstval);
#endif
return passed;
}
void flush_logs(void)
{
fprintf(stderr, "\n");
fflush(stderr);
fprintf(stdout, "\n");
fflush(stdout);
}
void run_sail(void)
{
bool spike_done;
bool stepped;
bool diverged = false;
/* initialize the step number */
mach_int step_no = 0;
int insn_cnt = 0;
while (!zhtif_done) {
{ /* run a Sail step */
sail_int sail_step;
CREATE(sail_int)(&sail_step);
CONVERT_OF(sail_int, mach_int)(&sail_step, step_no);
stepped = zstep(sail_step);
if (have_exception) goto step_exception;
flush_logs();
}
if (stepped) {
step_no++;
insn_cnt++;
}
#ifdef SPIKE
{ /* run a Spike step */
tv_step(s);
spike_done = tv_is_done(s);
flush_logs();
}
if (zhtif_done) {
if (!spike_done) {
fprintf(stdout, "Sail done (exit-code %ld), but not Spike!\n", zhtif_exit_code);
exit(1);
}
/* check exit code */
if (zhtif_exit_code == 0)
fprintf(stdout, "SUCCESS\n");
else
fprintf(stdout, "FAILURE: %ld\n", zhtif_exit_code);
} else {
if (spike_done) {
fprintf(stdout, "Spike done, but not Sail!\n");
exit(1);
}
if (!compare_states(s)) {
diverged = true;
break;
}
}
#endif
if (insn_cnt == rv_insns_per_tick) {
insn_cnt = 0;
ztick_clock(UNIT);
ztick_platform(UNIT);
#ifdef SPIKE
tv_tick_clock(s);
tv_step_io(s);
#endif
}
}
dump_state:
if (diverged) {
/* TODO */
}
finish(diverged);
step_exception:
fprintf(stderr, "Sail exception!");
goto dump_state;
}
void init_logs()
{
#ifdef SPIKE
// The Spike interface uses stdout for terminal output, and stderr for logs.
// Do the same here.
int logfd;
if (dup2(1, 2) < 0) {
fprintf(stderr, "Unable to dup 1 -> 2: %s\n", strerror(errno));
exit(1);
}
if ((term_fd = open(term_log, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR)) < 0) {
fprintf(stderr, "Cannot create terminal log '%s': %s\n", term_log, strerror(errno));
exit(1);
}
#endif
}
int main(int argc, char **argv)
{
char *file = process_args(argc, argv);
init_logs();
uint64_t entry = load_sail(file);
/* initialize spike before sail so that we can access the device-tree blob,
* until we roll our own.
*/
init_spike(file, entry);
init_sail(entry);
if (!init_check(s)) finish(1);
run_sail();
flush_logs();
}
|