forked from riscv/sail-riscv
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathriscv_sim.cpp
More file actions
688 lines (596 loc) · 22.9 KB
/
Copy pathriscv_sim.cpp
File metadata and controls
688 lines (596 loc) · 22.9 KB
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
#include "riscv_sim.h"
#include "CLI11.hpp"
#include "cli_options.h"
#include "config_utils.h"
#include "elf_loader.h"
#include "file_utils.h"
#include "jsoncons/config/version.hpp"
#include "jsoncons/json.hpp"
#include "riscv_callbacks_rvfi.h"
#include "riscv_callbacks_stop_at_pc.h"
#include "riscv_model_impl.h"
#ifdef SAILCOV
#include "sail_coverage.h"
#endif
#include "gdb/target_regs.h"
#include "sail_riscv_version.h"
#include "symbol_table.h"
#include "traploop_detector.h"
#include <asio.hpp>
#include <fcntl.h>
using std::chrono::duration_cast;
using std::chrono::milliseconds;
// Internal utilities.
namespace {
void print_build_info() {
std::cout << "Sail RISC-V release: " << version_info::release_version() << std::endl;
std::cout << "Sail RISC-V git: " << version_info::git_version() << std::endl;
std::cout << "Sail: " << version_info::sail_version() << std::endl;
std::cout << "C++ compiler: " << version_info::cxx_compiler_version() << std::endl;
std::cout << "CLI11: " << CLI11_VERSION << std::endl;
std::cout << "ELFIO: " << ELFIO_VERSION << std::endl;
std::cout << "JSONCONS: " << jsoncons::version() << std::endl;
std::cout << "ASIO: " << ASIO_VERSION << std::endl;
}
jsoncons::json parse_json_or_exit(const std::string &json_text, const std::string &source_desc) {
try {
return jsoncons::json::parse(json_text);
} catch (const jsoncons::json_exception &e) {
std::cerr << "JSON parse error in " << source_desc << ":\n" << e.what() << "\n\n";
exit(EXIT_FAILURE);
}
}
std::string make_path_string(const std::vector<std::string> &path) {
// This uses '/' separators for the path elements to be consistent
// with the error messages from the jsoncons schema validator.
if (path.empty()) {
return "/";
}
std::ostringstream buf;
for (const auto &seg : path) {
buf << "/" << seg;
}
return buf.str();
}
// Merge a JSON object `json_override` into an existing JSON object `base`. The
// algorithm is as follows:
//
// 1. If both `json_override` and `base` are objects we merge them as
// follows:
// a) If both objects have a single field and that key has a
// different name, this must be a union whose variant is being
// changed (e.g. `None` to `Some`). In this case we replace the
// entire `base` with `json_override`.
// Note: the key might be different due to a typo in the override,
// but that will be caught by the schema validation check.
// b) Otherwise this is a struct, or a union with an unchanged
// variant. In this case we loop through the keys of
// `json_override` and recurse for each one, merging into the
// corresponding key of `base` which must exist or an error is
// thrown (you cannot create new keys with this function).
//
// 2. If either are not objects, then we replace `base` with
// `json_override`. Additionally we check for type errors,
// e.g. replacing an object with an array.
void deep_merge_json(jsoncons::json &base, const jsoncons::json &json_override, std::vector<std::string> &path) {
if (base.type() == jsoncons::json_type::object && json_override.type() == jsoncons::json_type::object) {
// If both are objects with size 1 this must be a union that is
// changing its variant. Replace the entire object. See above
// for the effects of a typo.
if (base.size() == 1 && json_override.size() == 1 &&
base.object_range().begin()->key() != json_override.object_range().begin()->key()) {
base = json_override;
return;
}
// Otherwise this is a struct or a union with the same
// variant. Merge each key as long as it exists in the base.
for (const auto &entry : json_override.object_range()) {
const auto &key = entry.key();
const auto &value = entry.value();
path.emplace_back(key);
if (!base.contains(key)) {
std::ostringstream msg;
msg << "Cannot merge override: key at path '" << make_path_string(path)
<< "' does not exist in base object; config overrides cannot add new keys to the configuration.";
throw std::runtime_error(msg.str());
}
deep_merge_json(base[key], value, path);
path.pop_back();
}
return;
}
// At least one of the arguments is not an object. Replace the base
// with the override, but as long as the types are consistent.
auto base_type = base.type();
auto override_type = json_override.type();
if (base_type != override_type) {
std::ostringstream msg;
msg << "Cannot override config at path '" + make_path_string(path) + "' of type '" << base_type
<< "' with incompatible type '" << override_type << "'.";
throw std::runtime_error(msg.str());
}
base = json_override;
}
void write_dtb_to_rom(ModelImpl &model, const std::vector<uint8_t> &dtb) {
uint64_t addr = get_config_uint64({"memory", "dtb_address"});
uint64_t size = static_cast<uint64_t>(dtb.size());
// Overflow check for addr + size - 1
uint64_t end = addr + size - 1;
if (end < addr) {
fprintf(stderr, "DTB address/size overflow: addr=0x%0" PRIx64 ", size=0x%0" PRIx64 "\n", addr, size);
exit(EXIT_FAILURE);
}
// Validate DTB range against configured PMA memory regions.
if (!model.dtb_within_configured_pma_memory(addr, size)) {
fprintf(
stderr,
"DTB does not fit in any configured PMA memory region: "
"addr=0x%0" PRIx64 ", size=0x%0" PRIx64 " (end=0x%0" PRIx64 ")\n"
"Hint: adjust memory.dtb_address or memory.regions in the config.\n",
addr,
size,
end
);
exit(EXIT_FAILURE);
}
for (uint8_t d : dtb) {
write_mem(addr++, d);
}
}
void write_signature(const std::string &file, unsigned signature_granularity, const elf_info &elf_info) {
if (elf_info.mem_sig_start >= elf_info.mem_sig_end) {
fprintf(
stderr,
"Invalid signature region [0x%0" PRIx64 ",0x%0" PRIx64 "] to %s.\n",
elf_info.mem_sig_start,
elf_info.mem_sig_end,
file.c_str()
);
return;
}
FILE *f = fopen(file.c_str(), "w");
if (f == nullptr) {
fprintf(stderr, "Cannot open file '%s': %s\n", file.c_str(), strerror(errno));
return;
}
/* write out words depending on signature granularity in signature area */
for (uint64_t addr = elf_info.mem_sig_start; addr < elf_info.mem_sig_end; addr += signature_granularity) {
/* most-significant byte first */
for (unsigned i = signature_granularity; i > 0; --i) {
uint8_t byte = static_cast<uint8_t>(read_mem(addr + i - 1));
fprintf(f, "%02x", byte);
}
fprintf(f, "\n");
}
fclose(f);
}
void write_memory_dump(const MemoryRegion ®ion, const std::string &prefix) {
std::ostringstream file_os;
file_os << prefix << ".0x" << std::hex << region.base << ".bin";
const std::string file = file_os.str();
FILE *f = fopen(file.c_str(), "wb");
if (f == nullptr) {
fprintf(stderr, "Cannot create memory dump '%s': %s\n", file.c_str(), strerror(errno));
return;
}
// TODO: In C++23 we can use a literal suffix: 64 * 1024ZU
// (upper-case due to the clang-tidy
// `readability-uppercase-literal-suffix` check).
constexpr size_t buffer_size = static_cast<size_t>(64 * 1024);
std::vector<uint8_t> buffer(buffer_size);
uint64_t offset = 0;
while (offset < region.size) {
size_t chunk = static_cast<size_t>(std::min<uint64_t>(buffer.size(), region.size - offset));
for (size_t i = 0; i < chunk; ++i) {
buffer[i] = static_cast<uint8_t>(read_mem(region.base + offset + i));
}
if (fwrite(buffer.data(), 1, chunk, f) != chunk) {
fprintf(stderr, "Could not write memory dump '%s': %s\n", file.c_str(), strerror(errno));
break;
}
offset += chunk;
}
fclose(f);
}
void write_memory_dumps(const std::vector<MemoryRegion> ®ions, const std::string &prefix) {
for (const auto ®ion : regions) {
write_memory_dump(region, prefix);
}
}
std::string describe_config(const CLIOptions &opts) {
std::ostringstream config_desc;
if (opts.config_file.empty()) {
config_desc << "default configuration";
} else {
config_desc << "configuration in " << opts.config_file;
}
if (!opts.config_overrides.empty()) {
config_desc << " merged with ";
bool is_first = true;
for (const auto &override_path : opts.config_overrides) {
config_desc << (is_first ? "" : ", ") << override_path;
is_first = false;
}
}
return config_desc.str();
}
} // namespace
uint64_t load_sail(ModelImpl &model, const std::string &filename, bool main_file, elf_info &elf_info) {
ELF elf = ELF::open(filename);
switch (elf.architecture()) {
case Architecture::RV32:
if (model.xlen() != 32) {
fprintf(stderr, "32-bit ELF not supported by RV%" PRIi64 " model.\n", model.xlen());
exit(EXIT_FAILURE);
}
break;
case Architecture::RV64:
if (model.xlen() != 64) {
fprintf(stderr, "64-bit ELF not supported by RV%" PRIi64 " model.\n", model.xlen());
exit(EXIT_FAILURE);
}
break;
}
// Load into memory.
elf.load([](uint64_t address, const uint8_t *data, uint64_t length) {
// TODO: We could definitely improve on rts.c's memory implementation
// (which is O(N^2)) and writing one byte at a time here.
for (uint64_t i = 0; i < length; ++i) {
write_mem(address + i, data[i]);
}
});
// Load the entire symbol table.
const auto symbols = elf.symbols();
// Save reversed symbol table for log symbolization.
// If multiple symbols from different ELF files have the same value the first
// one wins.
const auto reversed_symbols = reverse_symbol_table(symbols);
elf_info.symbols.insert(reversed_symbols.begin(), reversed_symbols.end());
if (main_file) {
// Only scan for test-signature/htif symbols in the main ELF file.
const auto &tohost = symbols.find("tohost");
if (tohost == symbols.end()) {
fprintf(stderr, "Unable to locate tohost symbol; disabling HTIF.\n");
elf_info.htif_tohost_address = std::nullopt;
} else {
elf_info.htif_tohost_address = tohost->second;
fprintf(stdout, "HTIF located at 0x%0" PRIx64 "\n", *elf_info.htif_tohost_address);
}
// Locate test-signature locations if any.
const auto &begin_sig = symbols.find("begin_signature");
if (begin_sig != symbols.end()) {
fprintf(stdout, "begin_signature: 0x%0" PRIx64 "\n", begin_sig->second);
elf_info.mem_sig_start = begin_sig->second;
}
const auto &end_sig = symbols.find("end_signature");
if (end_sig != symbols.end()) {
fprintf(stdout, "end_signature: 0x%0" PRIx64 "\n", end_sig->second);
elf_info.mem_sig_end = end_sig->second;
}
}
return elf.entry();
}
void close_logs(run_info &run_info) {
if (run_info.close_term_fd) {
close(run_info.term_fd);
}
if (run_info.trace_log != stdout) {
fclose(run_info.trace_log);
}
#ifdef SAILCOV
if (sail_coverage_exit() != 0) {
fprintf(stderr, "Could not write coverage information!\n");
exit(EXIT_FAILURE);
}
#endif
}
void finish(ModelImpl &model, const CLIOptions &opts, const elf_info &elf_info, run_info &run_info) {
// Don't write a signature if there was an internal Sail exception.
if (!model.had_exception() && !opts.sig_file.empty()) {
write_signature(opts.sig_file, opts.signature_granularity, elf_info);
}
if (!opts.dump_memory_prefix.empty()) {
write_memory_dumps(model.main_memory_regions(), opts.dump_memory_prefix);
}
// `model_fini()` exits with failure if there was a Sail exception.
model.model_fini();
if (opts.do_show_times) {
auto run_end = steady_clock::now();
uint64_t init_msecs = duration_cast<milliseconds>(run_info.init_end - run_info.init_start).count();
uint64_t exec_msecs = duration_cast<milliseconds>(run_end - run_info.init_end).count();
fprintf(stderr, "Initialization: %" PRIu64 " ms\n", init_msecs);
fprintf(stderr, "Execution: %" PRIu64 " ms\n", exec_msecs);
fprintf(stderr, "Instructions: %" PRIu64 "\n", run_info.total_insns);
fprintf(stderr, "Performance: %" PRIu64 " kIPS\n", exec_msecs == 0 ? 0 : run_info.total_insns / exec_msecs);
}
close_logs(run_info);
exit(model.had_exception() ? EXIT_FAILURE : EXIT_SUCCESS);
}
void flush_logs(run_info &run_info) {
fflush(stderr);
fflush(stdout);
fflush(run_info.trace_log);
}
void run_sail(
ModelImpl &model,
const CLIOptions &opts,
std::shared_ptr<traploop_detector> loop_detector,
std::shared_ptr<stop_at_pc_callbacks> stop_at_pc,
const elf_info &elf_info,
run_info &run_info
) {
bool is_waiting = false;
// The emulator tick increments time by 1 at every step, so the number
// of steps to wait is equal to the needed increment in the time CSR.
uint64_t max_wait_steps = get_config_uint64({"platform", "max_time_to_wait"});
uint64_t wait_steps_remaining = 0;
/* initialize the step number */
mach_int step_no = 0;
uint64_t insn_cnt = 0;
uint64_t insns_per_tick = get_config_uint64({"platform", "instructions_per_tick"});
auto interval_start = steady_clock::now();
while (!model.htif_done() && !(stop_at_pc && stop_at_pc->stop_requested()) &&
(opts.insn_limit == 0 || run_info.total_insns < opts.insn_limit)) {
if (run_info.rvfi.has_value()) {
switch (run_info.rvfi->pre_step(opts.config_print_rvfi)) {
case RVFI_prestep_continue:
continue;
case RVFI_prestep_eof:
run_info.rvfi = std::nullopt;
return;
case RVFI_prestep_end_trace:
return;
case RVFI_prestep_ok:
break;
}
}
model.call_pre_step_callbacks(is_waiting);
{ /* run a Sail step */
is_waiting = model.try_step(step_no, wait_steps_remaining == 0);
std::optional<std::string> opt_str = model.string_of_current_exception();
if (opt_str.has_value()) {
fprintf(stdout, "%s\n", opt_str.value().c_str());
break;
}
if (opts.config_print_instr) {
flush_logs(run_info);
}
if (run_info.rvfi) {
run_info.rvfi->send_trace(opts.config_print_rvfi);
}
if (is_waiting) {
if (wait_steps_remaining == 0) {
wait_steps_remaining = max_wait_steps;
} else {
--wait_steps_remaining;
}
} else {
wait_steps_remaining = 0;
}
}
model.call_post_step_callbacks(is_waiting);
if (!is_waiting) {
if (opts.config_print_step) {
fprintf(run_info.trace_log, "\n");
}
step_no++;
insn_cnt++;
run_info.total_insns++;
}
if (opts.do_show_times && (run_info.total_insns & 0xfffff) == 0) {
const auto now = steady_clock::now();
const auto interval = now - interval_start;
interval_start = now;
uint64_t kips = 0x100000 / duration_cast<milliseconds>(interval).count();
fprintf(stdout, "kips: %" PRIu64 "\n", kips);
}
if (model.htif_done()) {
/* check exit code */
if (model.htif_exit_code() == 0) {
fprintf(stdout, "SUCCESS\n");
} else {
fprintf(stdout, "FAILURE: %" PRIu64 " (0x%08" PRIx64 ")\n", model.htif_exit_code(), model.htif_exit_code());
exit(EXIT_FAILURE);
}
}
if (insn_cnt == insns_per_tick) {
insn_cnt = 0;
model.tick_clock();
} else if (wait_steps_remaining > 0) {
model.tick_clock();
}
if (loop_detector->loop_detected()) {
fprintf(
stdout,
"FAILURE: possible trap loop detected with MEPC=0x%" PRIx64 " and SEPC=0x%" PRIx64 "\n",
loop_detector->mepc(),
loop_detector->sepc()
);
exit(EXIT_FAILURE);
}
}
// This is reached if there is a Sail exception, HTIF has indicated
// successful completion, or the instruction limit has been reached.
finish(model, opts, elf_info, run_info);
}
void init_logs(const CLIOptions &opts, run_info &run_info) {
if (!opts.term_log.empty()) {
run_info.term_fd = open(opts.term_log.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
if (run_info.term_fd < 0) {
fprintf(stderr, "Cannot create terminal log '%s': %s\n", opts.term_log.c_str(), strerror(errno));
exit(EXIT_FAILURE);
}
run_info.close_term_fd = true;
}
if (!opts.trace_log_path.empty()) {
run_info.trace_log = fopen(opts.trace_log_path.c_str(), "w+");
if (run_info.trace_log == nullptr) {
fprintf(stderr, "Cannot create trace log '%s': %s\n", opts.trace_log_path.c_str(), strerror(errno));
exit(EXIT_FAILURE);
}
}
#ifdef SAILCOV
if (!opts.sailcov_file.empty()) {
sail_set_coverage_file(opts.sailcov_file.c_str());
}
#endif
}
// Processes options that don't need an initialized model and gets the
// json configuration string; returns whether to continue with model
// initialization.
InitResult preinit_args(const CLIOptions &opts, std::string &config_json_string) {
if (opts.do_print_version) {
std::cout << version_info::release_version() << std::endl;
return InitResult::ExitSuccess;
}
if (opts.do_print_build_info) {
print_build_info();
return InitResult::ExitSuccess;
}
if (opts.do_print_default_config) {
printf("%s", opts.use_rv32_default ? get_default_rv32_config() : get_default_config());
return InitResult::ExitSuccess;
}
if (opts.do_print_config_schema) {
printf("%s", get_config_schema());
return InitResult::ExitSuccess;
}
if (opts.do_show_times) {
fprintf(stderr, "will show execution times on completion.\n");
}
if (!opts.term_log.empty()) {
fprintf(stderr, "using %s for terminal output.\n", opts.term_log.c_str());
}
if (!opts.sig_file.empty()) {
fprintf(stderr, "using %s for test-signature output.\n", opts.sig_file.c_str());
}
if (opts.signature_granularity != DEFAULT_SIGNATURE_GRANULARITY) {
fprintf(stderr, "setting signature-granularity to %d bytes\n", opts.signature_granularity);
}
if (!opts.trace_log_path.empty()) {
fprintf(stderr, "using %s for trace output.\n", opts.trace_log_path.c_str());
}
if (!opts.dump_memory_prefix.empty()) {
fprintf(stderr, "will dump main memory on completion using prefix '%s'.\n", opts.dump_memory_prefix.c_str());
}
if (!opts.config_file.empty()) {
config_json_string = read_file_to_string(opts.config_file);
} else {
config_json_string = opts.use_rv32_default ? get_default_rv32_config() : get_default_config();
}
// Check json config and merge overrides
const std::string base_source_desc =
opts.config_file.empty() ? "default configuration" : "configuration file " + opts.config_file;
jsoncons::json config_json = parse_json_or_exit(config_json_string, base_source_desc);
for (const auto &override_path : opts.config_overrides) {
std::string override_json_string = read_file_to_string(override_path);
jsoncons::json override_item = parse_json_or_exit(override_json_string, "override file " + override_path);
std::vector<std::string> path;
deep_merge_json(config_json, override_item, path);
}
std::ostringstream os;
os << config_json;
config_json_string = os.str();
// Always validate the schema conformance of the config.
std::string config_source_desc = describe_config(opts);
validate_config_schema(config_json, config_source_desc);
return InitResult::Continue;
}
// Configures the model, validates the configuration, processes
// options requiring a configured model and returns whether to continue with
// model simulation.
InitResult preinit_model(
const CLIOptions &opts,
ModelImpl &model,
const std::string &config_json_string,
run_info &run_info
) {
if (opts.rvfi_dii_port != 0) {
run_info.rvfi.emplace(opts.rvfi_dii_port, model);
}
if (opts.config_enable_experimental_extensions) {
fprintf(stderr, "enabling unratified extensions.\n");
model.set_enable_experimental_extensions(true);
}
// Initialize the model.
model.set_config_print_instr(opts.config_print_instr);
model.set_config_print_clint(opts.config_print_clint);
model.set_config_print_exception(opts.config_print_exception);
model.set_config_print_interrupt(opts.config_print_interrupt);
model.set_config_print_htif(opts.config_print_htif);
model.set_config_print_pma(opts.config_print_pma);
model.set_config_print_pmp(opts.config_print_pmp);
model.set_config_rvfi(run_info.rvfi.has_value());
model.set_config_use_abi_names(opts.config_use_abi_names);
model.set_config_print_step(opts.config_print_step);
sail_config_set_string(config_json_string.c_str());
// Initialize platform.
model.init_platform_constants();
model.model_init();
// Validate the configuration; exit if that's all we were asked to do
// or if the validation failed.
{
bool config_is_valid = model.config_is_valid();
const char *s = config_is_valid ? "valid" : "invalid";
if (!config_is_valid || opts.do_validate_config) {
std::string config_source_desc = describe_config(opts);
fprintf(stderr, "The %s is %s.\n", config_source_desc.c_str(), s);
return config_is_valid ? InitResult::ExitSuccess : InitResult::ExitFailure;
}
}
// Print a device tree or an ISA string only after the configuration
// is validated above.
if (opts.do_print_dts) {
fprintf(stdout, "%s", model.generate_dts().c_str());
return InitResult::ExitSuccess;
}
if (opts.do_print_isa) {
fprintf(stdout, "%s\n", model.generate_isa_string().c_str());
return InitResult::ExitSuccess;
}
if (opts.do_print_gdb_target_xml) {
fprintf(stdout, "%s", get_target_xml(model).c_str());
return InitResult::ExitSuccess;
}
// If we get here, we need to have ELF files to run (except in RVFI mode).
if (opts.elfs.empty() && !run_info.rvfi.has_value()) {
fprintf(stderr, "No elf file provided.\n");
return InitResult::ExitFailure;
}
init_logs(opts, run_info);
model.set_term_fd(run_info.term_fd);
model.set_trace_log(run_info.trace_log);
return InitResult::Continue;
}
InitResult init_model(
const CLIOptions &opts,
ModelImpl &model,
elf_info &elf_info,
run_info &run_info,
uint64_t &entry
) {
run_info.init_start = steady_clock::now();
if (run_info.rvfi.has_value()) {
if (!run_info.rvfi->setup_socket(opts.config_print_rvfi)) {
return InitResult::ExitFailure;
}
model.register_callback(std::make_shared<rvfi_callbacks>());
}
if (!opts.dtb_file.empty()) {
fprintf(stderr, "using %s as DTB file.\n", opts.dtb_file.c_str());
write_dtb_to_rom(model, read_file(opts.dtb_file));
}
entry = run_info.rvfi.has_value() ? rvfi_handler::get_entry()
: load_sail(model, opts.elfs[0], /*main_file=*/true, elf_info);
fprintf(stdout, "Entry point: 0x%" PRIx64 "\n", entry);
// Load any additional ELF files into memory. If RVFI was NOT used skip
// the first one because it was loaded above.
for (auto it = opts.elfs.cbegin() + (run_info.rvfi.has_value() ? 0 : 1); it != opts.elfs.cend(); it++) {
fprintf(stdout, "Loading additional ELF file %s.\n", it->c_str());
(void)load_sail(model, *it, /*main_file=*/false, elf_info);
}
model.set_elf_symbols(std::move(elf_info.symbols));
model.init_sail(entry, opts.config_file.c_str(), elf_info.htif_tohost_address);
run_info.init_end = steady_clock::now();
return InitResult::Continue;
}