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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
|
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/ip.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 ENABLE_SPIKE
#include "tv_spike_intf.h"
#else
struct tv_spike_t;
#endif
const char *RV64ISA = "RV64IMAC";
const char *RV32ISA = "RV32IMAC";
/* 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;
static bool do_show_times = false;
struct tv_spike_t *s = NULL;
char *term_log = NULL;
char *dtb_file = NULL;
unsigned char *dtb = NULL;
size_t dtb_len = 0;
#ifdef RVFI_DII
static bool rvfi_dii = false;
static int rvfi_dii_port;
static int rvfi_dii_sock;
#endif
unsigned char *spike_dtb = NULL;
size_t spike_dtb_len = 0;
char *sig_file = NULL;
uint64_t mem_sig_start = 0;
uint64_t mem_sig_end = 0;
bool config_print_instr = true;
bool config_print_reg = true;
bool config_print_mem_access = true;
bool config_print_platform = true;
void set_config_print(char *var, bool val) {
if (var == NULL || strcmp("all", var) == 0) {
config_print_instr = val;
config_print_mem_access = val;
config_print_reg = val;
config_print_platform = val;
} else if (strcmp("instr", var) == 0) {
config_print_instr = val;
} else if (strcmp("reg", var) == 0) {
config_print_reg = val;
} else if (strcmp("mem", var) == 0) {
config_print_mem_access = val;
} else if (strcmp("platform", var) == 0) {
config_print_platform = val;
} else {
fprintf(stderr, "Unknown trace category: '%s' (should be instr|reg|mem|platform|all)\n", var);
exit(1);
}
}
struct timeval init_start, init_end, run_end;
int total_insns = 0;
int insn_limit = 0;
static struct option options[] = {
{"enable-dirty-update", no_argument, 0, 'd'},
{"enable-misaligned", no_argument, 0, 'm'},
{"enable-pmp", no_argument, 0, 'P'},
{"ram-size", required_argument, 0, 'z'},
{"disable-compressed", no_argument, 0, 'C'},
{"disable-writable-misa", no_argument, 0, 'I'},
{"mtval-has-illegal-inst-bits", no_argument, 0, 'i'},
{"device-tree-blob", required_argument, 0, 'b'},
{"terminal-log", required_argument, 0, 't'},
{"show-times", required_argument, 0, 'p'},
{"report-arch", no_argument, 0, 'a'},
{"test-signature", required_argument, 0, 'T'},
#ifdef RVFI_DII
{"rvfi-dii", required_argument, 0, 'r'},
#endif
{"help", no_argument, 0, 'h'},
{"trace", optional_argument, 0, 'v'},
{"no-trace", optional_argument, 0, 'V'},
{"inst-limit", required_argument, 0, 'l'},
{0, 0, 0, 0}
};
static void print_usage(const char *argv0, int ec)
{
#ifdef RVFI_DII
fprintf(stdout, "Usage: %s [options] <elf_file>\n %s [options] -r <port>\n", argv0, argv0);
#else
fprintf(stdout, "Usage: %s [options] <elf_file>\n", argv0);
#endif
struct option *opt = options;
while (opt->name) {
fprintf(stdout, "\t -%c\t --%s\n", (char)opt->val, opt->name);
opt++;
}
exit(ec);
}
static void report_arch(void)
{
fprintf(stdout, "RV%" PRIu64 "\n", zxlen_val);
exit(0);
}
static bool is_32bit_model(void)
{
return zxlen_val == 32;
}
static void dump_dts(void)
{
#ifdef ENABLE_SPIKE
size_t dts_len = 0;
const char *isa = is_32bit_model() ? RV32ISA : RV64ISA;
struct tv_spike_t *s = tv_init(isa, rv_ram_size, 0);
tv_get_dts(s, NULL, &dts_len);
if (dts_len > 0) {
unsigned char *dts = (unsigned char *)malloc(dts_len + 1);
dts[dts_len] = '\0';
tv_get_dts(s, dts, &dts_len);
fprintf(stdout, "%s\n", dts);
}
#else
fprintf(stdout, "Spike linkage is currently needed to generate DTS.\n");
#endif
exit(0);
}
static void read_dtb(const char *path)
{
int fd = open(path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Unable to read DTB file %s: %s\n", path, strerror(errno));
exit(1);
}
struct stat st;
if (fstat(fd, &st) < 0) {
fprintf(stderr, "Unable to stat DTB file %s: %s\n", path, strerror(errno));
exit(1);
}
char *m = (char *)mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (m == MAP_FAILED) {
fprintf(stderr, "Unable to map DTB file %s: %s\n", path, strerror(errno));
exit(1);
}
dtb = (unsigned char *)malloc(st.st_size);
if (dtb == NULL) {
fprintf(stderr, "Cannot allocate DTB from file %s!\n", path);
exit(1);
}
memcpy(dtb, m, st.st_size);
dtb_len = st.st_size;
munmap(m, st.st_size);
close(fd);
fprintf(stdout, "Read %" PRIi64 " bytes of DTB from %s.\n", dtb_len, path);
}
char *process_args(int argc, char **argv)
{
int c;
uint64_t ram_size = 0;
while(true) {
c = getopt_long(argc, argv,
"a"
"d"
"m"
"C"
"I"
"i"
"s"
"p"
"z:"
"b:"
"t:"
"h"
"r:"
"T:"
"V::"
"v::"
"l:"
, options, NULL);
if (c == -1) break;
switch (c) {
case 'a':
report_arch();
break;
case 'd':
fprintf(stderr, "enabling dirty update.\n");
rv_enable_dirty_update = true;
break;
case 'm':
fprintf(stderr, "enabling misaligned access.\n");
rv_enable_misaligned = true;
break;
case 'P':
fprintf(stderr, "enabling PMP support.\n");
rv_enable_pmp = true;
break;
case 'C':
fprintf(stderr, "disabling RVC compressed instructions.\n");
rv_enable_rvc = false;
break;
case 'I':
fprintf(stderr, "disabling writable misa CSR.\n");
rv_enable_writable_misa = false;
break;
case 'i':
fprintf(stderr, "enabling storing illegal instruction bits in mtval.\n");
rv_mtval_has_illegal_inst_bits = true;
break;
case 's':
do_dump_dts = true;
break;
case 'p':
fprintf(stderr, "will show execution times on completion.\n");
do_show_times = true;
break;
case 'z':
ram_size = atol(optarg);
if (ram_size) {
fprintf(stderr, "setting ram-size to %" PRIu64 " MB\n", ram_size);
rv_ram_size = ram_size << 20;
} else {
fprintf(stderr, "invalid ram-size '%s' provided.\n", optarg);
exit(1);
}
break;
case 'b':
dtb_file = strdup(optarg);
fprintf(stderr, "using %s as DTB file.\n", dtb_file);
break;
case 't':
term_log = strdup(optarg);
fprintf(stderr, "using %s for terminal output.\n", term_log);
break;
case 'T':
sig_file = strdup(optarg);
fprintf(stderr, "using %s for test-signature output.\n", sig_file);
break;
case 'h':
print_usage(argv[0], 0);
break;
#ifdef RVFI_DII
case 'r':
rvfi_dii = true;
rvfi_dii_port = atoi(optarg);
fprintf(stderr, "using %d as RVFI port.\n", rvfi_dii_port);
break;
#endif
case 'V':
set_config_print(optarg, false);
break;
case 'v':
set_config_print(optarg, true);
break;
case 'l':
insn_limit = atoi(optarg);
break;
case '?':
print_usage(argv[0], 1);
break;
}
}
if (do_dump_dts) dump_dts();
#ifdef RVFI_DII
if (optind > argc || (optind == argc && !rvfi_dii)) print_usage(argv[0], 0);
#else
if (optind >= argc) {
fprintf(stderr, "No elf file provided.\n");
print_usage(argv[0], 0);
}
#endif
if (dtb_file) read_dtb(dtb_file);
#ifdef RVFI_DII
if (!rvfi_dii)
#endif
fprintf(stdout, "Running file %s.\n", argv[optind]);
return argv[optind];
}
void check_elf(bool is32bit)
{
if (is32bit) {
if (zxlen_val != 32) {
fprintf(stderr, "32-bit ELF not supported by RV%" PRIu64 " model.\n", zxlen_val);
exit(1);
}
} else {
if (zxlen_val != 64) {
fprintf(stderr, "64-bit ELF not supported by RV%" PRIu64 " model.\n", zxlen_val);
exit(1);
}
}
}
uint64_t load_sail(char *f)
{
bool is32bit;
uint64_t entry;
uint64_t begin_sig, end_sig;
load_elf(f, &is32bit, &entry);
check_elf(is32bit);
fprintf(stdout, "ELF Entry @ 0x%" PRIx64 "\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 0x%0" PRIx64 "\n", rv_htif_tohost);
/* locate test-signature locations if any */
if (!lookup_sym(f, "begin_signature", &begin_sig)) {
fprintf(stdout, "begin_signature: 0x%0" PRIx64 "\n", begin_sig);
mem_sig_start = begin_sig;
}
if (!lookup_sym(f, "end_signature", &end_sig)) {
fprintf(stdout, "end_signature: 0x%0" PRIx64 "\n", end_sig);
mem_sig_end = end_sig;
}
return entry;
}
void init_spike(const char *f, uint64_t entry, uint64_t ram_size)
{
#ifdef ENABLE_SPIKE
bool mismatch = false;
const char *isa = is_32bit_model() ? RV32ISA : RV64ISA;
s = tv_init(isa, ram_size, 1);
if (tv_is_dirty_enabled(s) != rv_enable_dirty_update) {
mismatch = true;
fprintf(stderr, "inconsistent enable-dirty-update setting: spike %s, sail %s\n",
tv_is_dirty_enabled(s) ? "on" : "off",
rv_enable_dirty_update ? "on" : "off");
}
if (tv_is_misaligned_enabled(s) != rv_enable_misaligned) {
mismatch = true;
fprintf(stderr, "inconsistent enable-misaligned-access setting: spike %s, sail %s\n",
tv_is_misaligned_enabled(s) ? "on" : "off",
rv_enable_misaligned ? "on" : "off");
}
if (tv_ram_size(s) != rv_ram_size) {
mismatch = true;
fprintf(stderr, "inconsistent ram-size setting: spike 0x%" PRIx64 ", sail 0x%" PRIx64 "\n",
tv_ram_size(s), rv_ram_size);
}
if (mismatch) exit(1);
/* The initialization order below matters. */
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);
/* get DTB from spike */
tv_get_dtb(s, NULL, &spike_dtb_len);
if (spike_dtb_len > 0) {
spike_dtb = (unsigned char *)malloc(spike_dtb_len + 1);
spike_dtb[spike_dtb_len] = '\0';
if (!tv_get_dtb(s, spike_dtb, &spike_dtb_len)) {
fprintf(stderr, "Got %" PRIu64 " bytes of dtb at %p\n", spike_dtb_len, spike_dtb);
} else {
fprintf(stderr, "Error getting DTB from Spike.\n");
exit(1);
}
} else {
fprintf(stderr, "No DTB available from Spike.\n");
}
#else
s = NULL;
#endif
}
void tick_spike()
{
#ifdef ENABLE_SPIKE
tv_tick_clock(s);
tv_step_io(s);
#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
is_32bit_model() ?
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]);
if (dtb && dtb_len) {
for (size_t i = 0; i < dtb_len; i++)
write_mem(addr++, dtb[i]);
}
#ifdef ENABLE_SPIKE
if (dtb && dtb_len) {
// Ensure that Spike's DTB matches the one provided.
bool matched = dtb_len == spike_dtb_len;
if (matched) {
for (size_t i = 0; i < dtb_len; i++)
matched = matched && (dtb[i] == spike_dtb[i]);
}
if (!matched) {
fprintf(stderr, "Provided DTB does not match Spike's!\n");
exit(1);
}
} else {
if (spike_dtb_len > 0) {
// Use the DTB from Spike.
for (size_t i = 0; i < spike_dtb_len; i++)
write_mem(addr++, spike_dtb[i]);
} else {
fprintf(stderr, "Running without rom device tree.\n");
}
}
#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 preinit_sail()
{
model_init();
}
void init_sail(uint64_t elf_entry)
{
zinit_model(UNIT);
#ifdef RVFI_DII
if (rvfi_dii) {
zext_rvfi_init(UNIT);
rv_ram_base = UINT64_C(0x80000000);
rv_ram_size = UINT64_C(0x10000);
rv_rom_base = UINT64_C(0);
rv_rom_size = UINT64_C(0);
rv_clint_base = UINT64_C(0);
rv_clint_size = UINT64_C(0);
rv_htif_tohost = UINT64_C(0);
zPC = elf_entry;
} else
#endif
init_sail_reset_vector(elf_entry);
// this is probably unnecessary now; remove
if (!rv_enable_rvc) z_set_Misa_C(&zmisa, 0);
}
/* reinitialize to clear state and memory, typically across tests runs */
void reinit_sail(uint64_t elf_entry)
{
model_fini();
model_init();
init_sail(elf_entry);
}
int init_check(struct tv_spike_t *s)
{
int passed = 1;
#ifdef ENABLE_SPIKE
passed &= tv_check_csr(s, CSR_MISA, zmisa.zMisa_chunk_0);
#endif
return passed;
}
void write_signature(const char *file)
{
if (mem_sig_start >= mem_sig_end) {
fprintf(stderr, "Invalid signature region [0x%0" PRIx64 ",0x%0" PRIx64 "] to %s.\n",
mem_sig_start, mem_sig_end, file);
return;
}
FILE *f = fopen(file, "w");
if (!f) {
fprintf(stderr, "Cannot open file '%s': %s\n", file, strerror(errno));
return;
}
/* write out words in signature area */
for (uint64_t addr = mem_sig_start; addr < mem_sig_end; addr += 4) {
/* most-significant byte first */
for (int i = 3; i >= 0; i--) {
uint8_t byte = (uint8_t) read_mem(addr+i);
fprintf(f, "%02x", byte);
}
fprintf(f, "\n");
}
fclose(f);
}
void finish(int ec)
{
if (sig_file) write_signature(sig_file);
model_fini();
#ifdef ENABLE_SPIKE
tv_free(s);
#endif
if (gettimeofday(&run_end, NULL) < 0) {
fprintf(stderr, "Cannot gettimeofday: %s\n", strerror(errno));
exit(1);
}
if (do_show_times) {
int init_msecs = (init_end.tv_sec - init_start.tv_sec)*1000 + (init_end.tv_usec - init_start.tv_usec)/1000;
int exec_msecs = (run_end.tv_sec - init_end.tv_sec)*1000 + (run_end.tv_usec - init_end.tv_usec)/1000;
double Kips = ((double)total_insns)/((double)exec_msecs);
fprintf(stderr, "Initialization: %d msecs\n", init_msecs);
fprintf(stderr, "Execution: %d msecs\n", exec_msecs);
fprintf(stderr, "Instructions: %d\n", total_insns);
fprintf(stderr, "Perf: %.3f Kips\n", Kips);
}
exit(ec);
}
int compare_states(struct tv_spike_t *s)
{
int passed = 1;
#ifdef ENABLE_SPIKE
#define TV_CHECK(reg, spike_reg, sail_reg) \
passed &= tv_check_ ## reg(s, spike_reg, sail_reg);
// 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);
TV_CHECK(gpr, 1, zx1);
TV_CHECK(gpr, 2, zx2);
TV_CHECK(gpr, 3, zx3);
TV_CHECK(gpr, 4, zx4);
TV_CHECK(gpr, 5, zx5);
TV_CHECK(gpr, 6, zx6);
TV_CHECK(gpr, 7, zx7);
TV_CHECK(gpr, 8, zx8);
TV_CHECK(gpr, 9, zx9);
TV_CHECK(gpr, 10, zx10);
TV_CHECK(gpr, 11, zx11);
TV_CHECK(gpr, 12, zx12);
TV_CHECK(gpr, 13, zx13);
TV_CHECK(gpr, 14, zx14);
TV_CHECK(gpr, 15, zx15);
TV_CHECK(gpr, 15, zx15);
TV_CHECK(gpr, 16, zx16);
TV_CHECK(gpr, 17, zx17);
TV_CHECK(gpr, 18, zx18);
TV_CHECK(gpr, 19, zx19);
TV_CHECK(gpr, 20, zx20);
TV_CHECK(gpr, 21, zx21);
TV_CHECK(gpr, 22, zx22);
TV_CHECK(gpr, 23, zx23);
TV_CHECK(gpr, 24, zx24);
TV_CHECK(gpr, 25, zx25);
TV_CHECK(gpr, 25, zx25);
TV_CHECK(gpr, 26, zx26);
TV_CHECK(gpr, 27, zx27);
TV_CHECK(gpr, 28, zx28);
TV_CHECK(gpr, 29, zx29);
TV_CHECK(gpr, 30, zx30);
TV_CHECK(gpr, 31, zx31);
/* some selected CSRs for now */
TV_CHECK(csr, CSR_MCAUSE, zmcause.zMcause_chunk_0);
TV_CHECK(csr, CSR_MEPC, zmepc);
TV_CHECK(csr, CSR_MTVAL, zmtval);
TV_CHECK(csr, CSR_MSTATUS, zmstatus);
TV_CHECK(csr, CSR_SCAUSE, zscause.zMcause_chunk_0);
TV_CHECK(csr, CSR_SEPC, zsepc);
TV_CHECK(csr, CSR_STVAL, zstval);
#undef TV_CHECK
#endif
return passed;
}
void flush_logs(void)
{
if(config_print_instr) {
fprintf(stderr, "\n");
fflush(stderr);
fprintf(stdout, "\n");
fflush(stdout);
}
}
#ifdef RVFI_DII
void rvfi_send_trace(void) {
sail_bits packet;
CREATE(lbits)(&packet);
zrvfi_get_exec_packet(&packet, UNIT);
if (packet.len % 8 != 0) {
fprintf(stderr, "RVFI-DII trace packet not byte aligned: %d\n", (int)packet.len);
exit(1);
}
unsigned char bytes[packet.len / 8];
/* mpz_export might not write all of the null bytes */
memset(bytes, 0, sizeof(bytes));
mpz_export(bytes, NULL, -1, 1, 0, 0, *(packet.bits));
if (write(rvfi_dii_sock, bytes, packet.len / 8) == -1) {
fprintf(stderr, "Writing RVFI DII trace failed: %s", strerror(errno));
exit(1);
}
KILL(lbits)(&packet);
}
#endif
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;
#ifdef RVFI_DII
bool need_instr = true;
#endif
struct timeval interval_start;
if (gettimeofday(&interval_start, NULL) < 0) {
fprintf(stderr, "Cannot gettimeofday: %s\n", strerror(errno));
exit(1);
}
while (!zhtif_done && (insn_limit == 0 || total_insns < insn_limit)) {
#ifdef RVFI_DII
if (rvfi_dii) {
mach_bits instr_bits;
int res = read(rvfi_dii_sock, &instr_bits, sizeof(instr_bits));
if (res == 0) {
rvfi_dii = false;
return;
}
if (res < sizeof(instr_bits)) {
fprintf(stderr, "Reading RVFI DII command failed: insufficient input");
exit(1);
}
if (res == -1) {
fprintf(stderr, "Reading RVFI DII command failed: %s", strerror(errno));
exit(1);
}
zrvfi_set_instr_packet(instr_bits);
zrvfi_zzero_exec_packet(UNIT);
mach_bits cmd = zrvfi_get_cmd(UNIT);
switch (cmd) {
case 0: /* EndOfTrace */
zrvfi_halt_exec_packet(UNIT);
rvfi_send_trace();
return;
case 1: /* Instruction */
break;
default:
fprintf(stderr, "Unknown RVFI-DII command: %d\n", (int)cmd);
exit(1);
}
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();
KILL(sail_int)(&sail_step);
rvfi_send_trace();
} else /* if (!rvfi_dii) */
#endif
{ /* 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();
KILL(sail_int)(&sail_step);
}
if (stepped) {
step_no++;
insn_cnt++;
total_insns++;
}
if (do_show_times && (total_insns & 0xfffff) == 0) {
uint64_t start_us = 1000000 * ((uint64_t) interval_start.tv_sec) + ((uint64_t)interval_start.tv_usec);
if (gettimeofday(&interval_start, NULL) < 0) {
fprintf(stderr, "Cannot gettimeofday: %s\n", strerror(errno));
exit(1);
}
uint64_t end_us = 1000000 * ((uint64_t) interval_start.tv_sec) + ((uint64_t)interval_start.tv_usec);
fprintf(stdout, "kips: %" PRIu64 "\n", ((uint64_t)1000) * 0x100000 / (end_us - start_us));
}
#ifdef ENABLE_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 %" PRIi64 "), but not Spike!\n", zhtif_exit_code);
exit(1);
}
} else {
if (spike_done) {
fprintf(stdout, "Spike done, but not Sail!\n");
exit(1);
}
}
if (!compare_states(s)) {
diverged = true;
break;
}
#endif
if (zhtif_done) {
/* check exit code */
if (zhtif_exit_code == 0)
fprintf(stdout, "SUCCESS\n");
else
fprintf(stdout, "FAILURE: %" PRIi64 "\n", zhtif_exit_code);
}
if (insn_cnt == rv_insns_per_tick) {
insn_cnt = 0;
ztick_clock(UNIT);
ztick_platform(UNIT);
tick_spike();
}
}
dump_state:
if (diverged) {
/* TODO */
}
finish(diverged);
step_exception:
fprintf(stderr, "Sail exception!");
goto dump_state;
}
void init_logs()
{
#ifdef ENABLE_SPIKE
// The Spike interface uses stdout for terminal output, and stderr for logs.
// Do the same here.
if (dup2(1, 2) < 0) {
fprintf(stderr, "Unable to dup 1 -> 2: %s\n", strerror(errno));
exit(1);
}
#endif
if (term_log != NULL && (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);
}
}
int main(int argc, char **argv)
{
// Initialize model so that we can check or report its architecture.
preinit_sail();
char *file = process_args(argc, argv);
init_logs();
if (gettimeofday(&init_start, NULL) < 0) {
fprintf(stderr, "Cannot gettimeofday: %s\n", strerror(errno));
exit(1);
}
#ifdef RVFI_DII
uint64_t entry;
if (rvfi_dii) {
entry = 0x80000000;
int listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sock == -1) {
fprintf(stderr, "Unable to create socket: %s", strerror(errno));
return 1;
}
int opt = 1;
if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
fprintf(stderr, "Unable to set reuseaddr on socket: %s", strerror(errno));
return 1;
}
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = INADDR_ANY,
.sin_port = htons(rvfi_dii_port)
};
if (bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
fprintf(stderr, "Unable to set bind socket: %s", strerror(errno));
return 1;
}
if (listen(listen_sock, 1) == -1) {
fprintf(stderr, "Unable to listen on socket: %s", strerror(errno));
return 1;
}
printf("Waiting for connection\n");
rvfi_dii_sock = accept(listen_sock, NULL, NULL);
if (rvfi_dii_sock == -1) {
fprintf(stderr, "Unable to accept connection on socket: %s", strerror(errno));
return 1;
}
close(listen_sock);
printf("Connected\n");
} else
entry = load_sail(file);
#else
uint64_t entry = load_sail(file);
#endif
/* initialize spike before sail so that we can access the device-tree blob,
* until we roll our own.
*/
init_spike(file, entry, rv_ram_size);
init_sail(entry);
if (!init_check(s)) finish(1);
if (gettimeofday(&init_end, NULL) < 0) {
fprintf(stderr, "Cannot gettimeofday: %s\n", strerror(errno));
exit(1);
}
do {
run_sail();
#ifndef RVFI_DII
} while (0);
#else
if (rvfi_dii) {
/* Reset for next test */
reinit_sail(entry);
}
} while (rvfi_dii);
#endif
model_fini();
flush_logs();
}
|