Skip to content

Commit cd692c4

Browse files
authored
Merge pull request #1828 from Timmmm/instruction_limit
Add instruction limit
2 parents 18f4d0f + fdbcde2 commit cd692c4

File tree

9 files changed

+56
-16
lines changed

9 files changed

+56
-16
lines changed

ci-tests/custom-csr.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ int main(int argc, char **argv) {
7676
true, // dtb_enabled
7777
nullptr, // dtb_file
7878
false, // socket_enabled
79-
nullptr); // cmd_file
79+
nullptr, // cmd_file
80+
std::nullopt); // instruction_limit
8081
sim.run();
8182
}

ci-tests/test-customext.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ int main(int argc, char **argv) {
8585
true, // dtb_enabled
8686
nullptr, // dtb_file
8787
false, // socket_enabled
88-
nullptr); // cmd_file
88+
nullptr, // cmd_file
89+
std::nullopt); // instruction_limit
8990
sim.run();
9091
}

ci-tests/testlib.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ int main(int argc, char **argv) {
3434
true,
3535
nullptr,
3636
false,
37-
nullptr);
37+
nullptr,
38+
std::nullopt);
3839
sim.run();
3940
}

fesvr/htif.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void handle_signal(int sig)
4646

4747
htif_t::htif_t()
4848
: mem(this), entry(DRAM_BASE), sig_addr(0), sig_len(0),
49-
tohost_addr(0), fromhost_addr(0), exitcode(0), stopped(false),
49+
tohost_addr(0), fromhost_addr(0), stopped(false),
5050
syscall_proxy(this)
5151
{
5252
signal(SIGINT, &handle_signal);
@@ -116,7 +116,7 @@ std::map<std::string, uint64_t> htif_t::load_payload(const std::string& payload,
116116
else
117117
throw std::runtime_error(
118118
"could not open " + payload + "; searched paths:\n" +
119-
"\t. (current directory)\n" +
119+
"\t. (current directory)\n" +
120120
"\t" + PREFIX TARGET_DIR + " (based on configured --prefix and --with-target)"
121121
);
122122
}
@@ -207,6 +207,14 @@ const char* htif_t::get_symbol(uint64_t addr)
207207
return it->second.c_str();
208208
}
209209

210+
bool htif_t::should_exit() const {
211+
return signal_exit || exitcode.has_value();
212+
}
213+
214+
void htif_t::htif_exit(int exit_code) {
215+
exitcode = exit_code;
216+
}
217+
210218
void htif_t::stop()
211219
{
212220
if (!sig_file.empty() && sig_len) // print final torture test signature
@@ -253,11 +261,11 @@ int htif_t::run()
253261
std::bind(enq_func, &fromhost_queue, std::placeholders::_1);
254262

255263
if (tohost_addr == 0) {
256-
while (!signal_exit)
264+
while (!should_exit())
257265
idle();
258266
}
259267

260-
while (!signal_exit && exitcode == 0)
268+
while (!should_exit())
261269
{
262270
uint64_t tohost;
263271

@@ -305,7 +313,7 @@ bool htif_t::done()
305313

306314
int htif_t::exit_code()
307315
{
308-
return exitcode >> 1;
316+
return exitcode.value_or(0) >> 1;
309317
}
310318

311319
void htif_t::parse_arguments(int argc, char ** argv)

fesvr/htif.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class htif_t : public chunked_memif_t
2424
virtual void start();
2525
virtual void stop();
2626

27+
// Cause the simulation to exit with the given exit code.
28+
void htif_exit(int exit_code);
29+
2730
int run();
2831
bool done();
2932
int exit_code();
@@ -76,6 +79,10 @@ class htif_t : public chunked_memif_t
7679
// Given an address, return symbol from addr2symbol map
7780
const char* get_symbol(uint64_t addr);
7881

82+
// Return true if the simulation should exit due to a signal,
83+
// or end-of-test from HTIF, or an instruction limit.
84+
bool should_exit() const;
85+
7986
private:
8087
void parse_arguments(int argc, char ** argv);
8188
void register_devices();
@@ -93,7 +100,8 @@ class htif_t : public chunked_memif_t
93100
addr_t sig_len; // torture
94101
addr_t tohost_addr;
95102
addr_t fromhost_addr;
96-
int exitcode;
103+
// Set to a value by htif_exit() when the simulation should exit.
104+
std::optional<int> exitcode;
97105
bool stopped;
98106

99107
device_list_t device_list;

fesvr/syscall.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct riscv_statx
137137
__spare2(), __spare3()
138138
#else
139139
__spare2()
140-
#endif
140+
#endif
141141
{}
142142
};
143143
#endif
@@ -221,7 +221,7 @@ void syscall_t::handle_syscall(command_t cmd)
221221

222222
reg_t syscall_t::sys_exit(reg_t code, reg_t a1, reg_t a2, reg_t a3, reg_t a4, reg_t a5, reg_t a6)
223223
{
224-
htif->exitcode = code << 1 | 1;
224+
htif->htif_exit(code << 1 | 1);
225225
return 0;
226226
}
227227

riscv/sim.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ sim_t::sim_t(const cfg_t *cfg, bool halted,
4444
const char *log_path,
4545
bool dtb_enabled, const char *dtb_file,
4646
bool socket_enabled,
47-
FILE *cmd_file) // needed for command line option --cmd
47+
FILE *cmd_file, // needed for command line option --cmd
48+
std::optional<unsigned long long> instruction_limit)
4849
: htif_t(args),
4950
cfg(cfg),
5051
mems(mems),
5152
dtb_enabled(dtb_enabled),
5253
log_file(log_path),
5354
cmd_file(cmd_file),
55+
instruction_limit(instruction_limit),
5456
sout_(nullptr),
5557
current_step(0),
5658
current_proc(0),
@@ -429,8 +431,19 @@ void sim_t::idle()
429431

430432
if (debug || ctrlc_pressed)
431433
interactive();
432-
else
434+
else {
435+
if (instruction_limit.has_value()) {
436+
if (*instruction_limit < INTERLEAVE) {
437+
// Final step.
438+
step(*instruction_limit);
439+
htif_exit(0);
440+
*instruction_limit = 0;
441+
return;
442+
}
443+
*instruction_limit -= INTERLEAVE;
444+
}
433445
step(INTERLEAVE);
446+
}
434447

435448
if (remote_bitbang)
436449
remote_bitbang->tick();

riscv/sim.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ class sim_t : public htif_t, public simif_t
3535
const debug_module_config_t &dm_config, const char *log_path,
3636
bool dtb_enabled, const char *dtb_file,
3737
bool socket_enabled,
38-
FILE *cmd_file); // needed for command line option --cmd
38+
FILE *cmd_file, // needed for command line option --cmd
39+
std::optional<unsigned long long> instruction_limit);
3940
~sim_t();
4041

41-
// run the simulation to completion
4242
int run();
4343
void set_debug(bool value);
4444
void set_histogram(bool value);
@@ -85,6 +85,8 @@ class sim_t : public htif_t, public simif_t
8585

8686
FILE *cmd_file; // pointer to debug command input file
8787

88+
std::optional<unsigned long long> instruction_limit;
89+
8890
socketif_t *socketif;
8991
std::ostream sout_; // used for socket and terminal interface
9092

spike_main/spike.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static void help(int exit_code = 1)
8484
fprintf(stderr, " --dm-no-halt-groups Debug module won't support halt groups\n");
8585
fprintf(stderr, " --dm-no-impebreak Debug module won't support implicit ebreak in program buffer\n");
8686
fprintf(stderr, " --blocksz=<size> Cache block size (B) for CMO operations(powers of 2) [default 64]\n");
87+
fprintf(stderr, " --instructions=<n> Stop after n instructions\n");
8788

8889
exit(exit_code);
8990
}
@@ -338,6 +339,7 @@ int main(int argc, char** argv)
338339
bool use_rbb = false;
339340
unsigned dmi_rti = 0;
340341
reg_t blocksz = 64;
342+
std::optional<unsigned long long> instructions;
341343
debug_module_config_t dm_config;
342344
cfg_arg_t<size_t> nprocs(1);
343345

@@ -450,6 +452,9 @@ int main(int argc, char** argv)
450452
exit(-1);
451453
}
452454
});
455+
parser.option(0, "instructions", 1, [&](const char* s){
456+
instructions = strtoull(s, 0, 0);
457+
});
453458

454459
auto argv1 = parser.parse(argv);
455460
std::vector<std::string> htif_args(argv1, (const char*const*)argv + argc);
@@ -511,7 +516,8 @@ int main(int argc, char** argv)
511516
sim_t s(&cfg, halted,
512517
mems, plugin_device_factories, htif_args, dm_config, log_path, dtb_enabled, dtb_file,
513518
socket,
514-
cmd_file);
519+
cmd_file,
520+
instructions);
515521
std::unique_ptr<remote_bitbang_t> remote_bitbang((remote_bitbang_t *) NULL);
516522
std::unique_ptr<jtag_dtm_t> jtag_dtm(
517523
new jtag_dtm_t(&s.debug_module, dmi_rti));

0 commit comments

Comments
 (0)