Skip to content

Commit de7c9e8

Browse files
authored
Make config settings non-global (#1535)
Move the `config_..` settings into `CLIOptions` and `ModelImpl` so they are no longer global.
1 parent 84f6541 commit de7c9e8

File tree

6 files changed

+147
-87
lines changed

6 files changed

+147
-87
lines changed

c_emulator/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ add_library(riscv_model
3030
elf_loader.h
3131
riscv_callbacks_if.cpp
3232
riscv_callbacks_if.h
33-
riscv_config.h
3433
riscv_platform_if.cpp
3534
riscv_platform_if.h
3635
riscv_model_impl.cpp

c_emulator/riscv_callbacks_log.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "riscv_callbacks_log.h"
2-
#include "riscv_config.h"
32
#include "sail_riscv_model.h"
43
#include <inttypes.h>
54
#include <stdlib.h>

c_emulator/riscv_config.h

Lines changed: 0 additions & 14 deletions
This file was deleted.

c_emulator/riscv_model_impl.cpp

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <unistd.h>
66

77
#include "riscv_callbacks_if.h"
8-
#include "riscv_config.h"
98
#include "symbol_table.h"
109

1110
int term_fd = 1; // set during startup
@@ -226,36 +225,76 @@ unit ModelImpl::print_log_instr(const_sail_string s, uint64_t pc) {
226225
}
227226

228227
unit ModelImpl::print_step(unit) {
229-
if (config_print_step) {
228+
if (m_config_print_step) {
230229
fprintf(trace_log, "\n");
231230
}
232231
return UNIT;
233232
}
234233

235234
bool ModelImpl::get_config_print_instr(unit) {
236-
return config_print_instr;
235+
return m_config_print_instr;
237236
}
238237

239238
bool ModelImpl::get_config_print_clint(unit) {
240-
return config_print_clint;
239+
return m_config_print_clint;
241240
}
241+
242242
bool ModelImpl::get_config_print_exception(unit) {
243-
return config_print_exception;
243+
return m_config_print_exception;
244244
}
245+
245246
bool ModelImpl::get_config_print_interrupt(unit) {
246-
return config_print_interrupt;
247+
return m_config_print_interrupt;
247248
}
249+
248250
bool ModelImpl::get_config_print_htif(unit) {
249-
return config_print_htif;
251+
return m_config_print_htif;
250252
}
253+
251254
bool ModelImpl::get_config_print_pma(unit) {
252-
return config_print_pma;
255+
return m_config_print_pma;
253256
}
254257

255258
bool ModelImpl::get_config_rvfi(unit) {
256-
return config_enable_rvfi;
259+
return m_config_rvfi;
257260
}
258261

259262
bool ModelImpl::get_config_use_abi_names(unit) {
260-
return config_use_abi_names;
263+
return m_config_use_abi_names;
264+
}
265+
266+
void ModelImpl::set_config_print_instr(bool on) {
267+
m_config_print_instr = on;
268+
}
269+
270+
void ModelImpl::set_config_print_clint(bool on) {
271+
m_config_print_clint = on;
272+
}
273+
274+
void ModelImpl::set_config_print_exception(bool on) {
275+
m_config_print_exception = on;
276+
}
277+
278+
void ModelImpl::set_config_print_interrupt(bool on) {
279+
m_config_print_interrupt = on;
280+
}
281+
282+
void ModelImpl::set_config_print_htif(bool on) {
283+
m_config_print_htif = on;
284+
}
285+
286+
void ModelImpl::set_config_print_pma(bool on) {
287+
m_config_print_pma = on;
288+
}
289+
290+
void ModelImpl::set_config_rvfi(bool on) {
291+
m_config_rvfi = on;
292+
}
293+
294+
void ModelImpl::set_config_use_abi_names(bool on) {
295+
m_config_use_abi_names = on;
296+
}
297+
298+
void ModelImpl::set_config_print_step(bool on) {
299+
m_config_print_step = on;
261300
}

c_emulator/riscv_model_impl.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ class ModelImpl final : public hart::Model {
2424
void set_enable_experimental_extensions(bool en);
2525
void set_reservation_set_size_exp(uint64_t exponent);
2626

27+
void set_config_print_instr(bool on);
28+
void set_config_print_clint(bool on);
29+
void set_config_print_exception(bool on);
30+
void set_config_print_interrupt(bool on);
31+
void set_config_print_htif(bool on);
32+
void set_config_print_pma(bool on);
33+
void set_config_rvfi(bool on);
34+
void set_config_use_abi_names(bool on);
35+
36+
void set_config_print_step(bool on);
37+
2738
private:
2839
// These functions are called by the Sail code.
2940

@@ -79,6 +90,17 @@ class ModelImpl final : public hart::Model {
7990
bool get_config_rvfi(unit) override;
8091
bool get_config_use_abi_names(unit) override;
8192

93+
bool m_config_print_instr = false;
94+
bool m_config_print_clint = false;
95+
bool m_config_print_exception = false;
96+
bool m_config_print_interrupt = false;
97+
bool m_config_print_htif = false;
98+
bool m_config_print_pma = false;
99+
bool m_config_rvfi = false;
100+
bool m_config_use_abi_names = false;
101+
102+
bool m_config_print_step = false;
103+
82104
// TODO: Probably better with std::shared_ptr<callbacks_if>.
83105
std::vector<callbacks_if *> m_callbacks;
84106

c_emulator/riscv_sim.cpp

Lines changed: 76 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,6 @@ ModelImpl g_model;
6868

6969
FILE *trace_log = stdout;
7070

71-
bool config_print_instr = false;
72-
bool config_print_reg = false;
73-
bool config_print_mem_access = false;
74-
bool config_print_clint = false;
75-
bool config_print_exception = false;
76-
bool config_print_interrupt = false;
77-
bool config_print_htif = false;
78-
bool config_print_pma = false;
79-
bool config_print_rvfi = false;
80-
bool config_print_step = false;
81-
bool config_print_ptw = false;
82-
83-
bool config_use_abi_names = false;
84-
bool config_enable_rvfi = false;
85-
86-
bool config_enable_experimental_extensions = false;
87-
8871
static void print_dts() {
8972
char *dts = nullptr;
9073
g_model.zgenerate_dts(&dts, UNIT);
@@ -140,6 +123,22 @@ struct CLIOptions {
140123
#ifdef SAILCOV
141124
std::string sailcov_file;
142125
#endif
126+
127+
bool config_print_instr = false;
128+
bool config_print_reg = false;
129+
bool config_print_mem_access = false;
130+
bool config_print_clint = false;
131+
bool config_print_exception = false;
132+
bool config_print_interrupt = false;
133+
bool config_print_htif = false;
134+
bool config_print_pma = false;
135+
bool config_print_rvfi = false;
136+
bool config_print_step = false;
137+
bool config_print_ptw = false;
138+
139+
bool config_use_abi_names = false;
140+
141+
bool config_enable_experimental_extensions = false;
143142
};
144143

145144
// Parse CLI options. This calls `exit()` on failure.
@@ -164,10 +163,10 @@ static CLIOptions parse_cli(int argc, char **argv) {
164163
app.add_flag("--print-isa-string", opts.do_print_isa, "Print ISA string");
165164
app.add_flag(
166165
"--enable-experimental-extensions",
167-
config_enable_experimental_extensions,
166+
opts.config_enable_experimental_extensions,
168167
"Enable experimental extensions"
169168
);
170-
app.add_flag("--use-abi-names", config_use_abi_names, "Use ABI register names in trace log");
169+
app.add_flag("--use-abi-names", opts.config_use_abi_names, "Use ABI register names in trace log");
171170

172171
app.add_option("--device-tree-blob", opts.dtb_file, "Device tree blob file")
173172
->check(CLI::ExistingFile)
@@ -186,44 +185,44 @@ static CLIOptions parse_cli(int argc, char **argv) {
186185
app.add_option("--sailcov-file", sailcov_file, "Sail coverage output file")->option_text("<file>");
187186
#endif
188187

189-
app.add_flag("--trace-instr", config_print_instr, "Enable trace output for instruction execution");
190-
app.add_flag("--trace-ptw", config_print_ptw, "Enable trace output for Page Table walk");
191-
app.add_flag("--trace-reg", config_print_reg, "Enable trace output for register access");
192-
app.add_flag("--trace-mem", config_print_mem_access, "Enable trace output for memory accesses");
193-
app.add_flag("--trace-rvfi", config_print_rvfi, "Enable trace output for RVFI");
194-
app.add_flag("--trace-clint", config_print_clint, "Enable trace output for CLINT memory accesses and status");
195-
app.add_flag("--trace-exception", config_print_exception, "Enable trace output for exceptions");
196-
app.add_flag("--trace-interrupt", config_print_interrupt, "Enable trace output for interrupts");
197-
app.add_flag("--trace-htif", config_print_htif, "Enable trace output for HTIF operations");
198-
app.add_flag("--trace-pma", config_print_pma, "Enable trace output for PMA checks");
188+
app.add_flag("--trace-instr", opts.config_print_instr, "Enable trace output for instruction execution");
189+
app.add_flag("--trace-ptw", opts.config_print_ptw, "Enable trace output for Page Table walk");
190+
app.add_flag("--trace-reg", opts.config_print_reg, "Enable trace output for register access");
191+
app.add_flag("--trace-mem", opts.config_print_mem_access, "Enable trace output for memory accesses");
192+
app.add_flag("--trace-rvfi", opts.config_print_rvfi, "Enable trace output for RVFI");
193+
app.add_flag("--trace-clint", opts.config_print_clint, "Enable trace output for CLINT memory accesses and status");
194+
app.add_flag("--trace-exception", opts.config_print_exception, "Enable trace output for exceptions");
195+
app.add_flag("--trace-interrupt", opts.config_print_interrupt, "Enable trace output for interrupts");
196+
app.add_flag("--trace-htif", opts.config_print_htif, "Enable trace output for HTIF operations");
197+
app.add_flag("--trace-pma", opts.config_print_pma, "Enable trace output for PMA checks");
199198
app.add_flag_callback(
200199
"--trace-platform",
201-
[] {
202-
config_print_clint = true;
203-
config_print_exception = true;
204-
config_print_interrupt = true;
205-
config_print_htif = true;
206-
config_print_pma = true;
200+
[&opts] {
201+
opts.config_print_clint = true;
202+
opts.config_print_exception = true;
203+
opts.config_print_interrupt = true;
204+
opts.config_print_htif = true;
205+
opts.config_print_pma = true;
207206
},
208207
"Enable trace output for platform-level events (MMIO, interrupts, "
209208
"exceptions, CLINT, HTIF, PMA)"
210209
);
211-
app.add_flag("--trace-step", config_print_step, "Add a blank line between steps in the trace output");
210+
app.add_flag("--trace-step", opts.config_print_step, "Add a blank line between steps in the trace output");
212211

213212
app.add_flag_callback(
214213
"--trace-all",
215-
[] {
216-
config_print_instr = true;
217-
config_print_reg = true;
218-
config_print_mem_access = true;
219-
config_print_rvfi = true;
220-
config_print_clint = true;
221-
config_print_exception = true;
222-
config_print_interrupt = true;
223-
config_print_htif = true;
224-
config_print_pma = true;
225-
config_print_step = true;
226-
config_print_ptw = true;
214+
[&opts] {
215+
opts.config_print_instr = true;
216+
opts.config_print_reg = true;
217+
opts.config_print_mem_access = true;
218+
opts.config_print_rvfi = true;
219+
opts.config_print_clint = true;
220+
opts.config_print_exception = true;
221+
opts.config_print_interrupt = true;
222+
opts.config_print_htif = true;
223+
opts.config_print_pma = true;
224+
opts.config_print_step = true;
225+
opts.config_print_ptw = true;
227226
},
228227
"Enable all trace output"
229228
);
@@ -433,11 +432,9 @@ void finish(const CLIOptions &opts) {
433432
}
434433

435434
void flush_logs() {
436-
if (config_print_instr) {
437-
fflush(stderr);
438-
fflush(stdout);
439-
fflush(trace_log);
440-
}
435+
fflush(stderr);
436+
fflush(stdout);
437+
fflush(trace_log);
441438
}
442439

443440
void run_sail(const CLIOptions &opts) {
@@ -454,7 +451,7 @@ void run_sail(const CLIOptions &opts) {
454451

455452
while (!g_model.zhtif_done && (opts.insn_limit == 0 || total_insns < opts.insn_limit)) {
456453
if (rvfi.has_value()) {
457-
switch (rvfi->pre_step(config_print_rvfi)) {
454+
switch (rvfi->pre_step(opts.config_print_rvfi)) {
458455
case RVFI_prestep_continue:
459456
continue;
460457
case RVFI_prestep_eof:
@@ -477,17 +474,19 @@ void run_sail(const CLIOptions &opts) {
477474
if (g_model.have_exception) {
478475
break;
479476
}
480-
flush_logs();
477+
if (opts.config_print_instr) {
478+
flush_logs();
479+
}
481480
KILL(sail_int)(&sail_step);
482481
if (rvfi) {
483-
rvfi->send_trace(config_print_rvfi);
482+
rvfi->send_trace(opts.config_print_rvfi);
484483
}
485484
}
486485

487486
g_model.call_post_step_callbacks(is_waiting);
488487

489488
if (!is_waiting) {
490-
if (config_print_step) {
489+
if (opts.config_print_step) {
491490
fprintf(trace_log, "\n");
492491
}
493492
step_no++;
@@ -569,7 +568,6 @@ int inner_main(int argc, char **argv) {
569568
return EXIT_SUCCESS;
570569
}
571570
if (opts.rvfi_dii_port != 0) {
572-
config_enable_rvfi = true;
573571
rvfi.emplace(opts.rvfi_dii_port, g_model);
574572
}
575573
if (opts.do_show_times) {
@@ -584,14 +582,25 @@ int inner_main(int argc, char **argv) {
584582
if (opts.signature_granularity != DEFAULT_SIGNATURE_GRANULARITY) {
585583
fprintf(stderr, "setting signature-granularity to %d bytes\n", opts.signature_granularity);
586584
}
587-
if (config_enable_experimental_extensions) {
585+
if (opts.config_enable_experimental_extensions) {
588586
fprintf(stderr, "enabling unratified extensions.\n");
589587
g_model.set_enable_experimental_extensions(true);
590588
}
591589
if (!opts.trace_log_path.empty()) {
592590
fprintf(stderr, "using %s for trace output.\n", opts.trace_log_path.c_str());
593591
}
594592

593+
g_model.set_config_print_instr(opts.config_print_instr);
594+
g_model.set_config_print_clint(opts.config_print_clint);
595+
g_model.set_config_print_exception(opts.config_print_exception);
596+
g_model.set_config_print_interrupt(opts.config_print_interrupt);
597+
g_model.set_config_print_htif(opts.config_print_htif);
598+
g_model.set_config_print_pma(opts.config_print_pma);
599+
g_model.set_config_rvfi(rvfi.has_value());
600+
g_model.set_config_use_abi_names(opts.config_use_abi_names);
601+
602+
g_model.set_config_print_step(opts.config_print_step);
603+
595604
// Always validate the schema conformance of the config.
596605
validate_config_schema(opts.config_file);
597606

@@ -640,13 +649,19 @@ int inner_main(int argc, char **argv) {
640649
}
641650

642651
init_logs(opts);
643-
log_callbacks log_cbs(config_print_reg, config_print_mem_access, config_print_ptw, config_use_abi_names, trace_log);
652+
log_callbacks log_cbs(
653+
opts.config_print_reg,
654+
opts.config_print_mem_access,
655+
opts.config_print_ptw,
656+
opts.config_use_abi_names,
657+
trace_log
658+
);
644659
g_model.register_callback(&log_cbs);
645660

646661
init_start = steady_clock::now();
647662

648663
if (rvfi) {
649-
if (!rvfi->setup_socket(config_print_rvfi)) {
664+
if (!rvfi->setup_socket(opts.config_print_rvfi)) {
650665
return 1;
651666
}
652667
g_model.register_callback(&rvfi_cbs);

0 commit comments

Comments
 (0)