Skip to content

Commit 1f466df

Browse files
authored
Merge pull request #1522 from ucb-bar/device-plugin-api
Fix Spike --device option to pass on args to downstream plugins
2 parents d94fe56 + b98e922 commit 1f466df

File tree

8 files changed

+34
-11
lines changed

8 files changed

+34
-11
lines changed

ci-tests/testlib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main()
2929
hartids,
3030
false,
3131
4);
32-
std::vector<const device_factory_t*> plugin_devices;
32+
std::vector<device_factory_t*> plugin_devices;
3333
std::vector<std::string> htif_args {"pk", "hello"};
3434
debug_module_config_t dm_config = {
3535
.progbufsize = 2,

riscv/abstract_device.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99
#include <map>
1010
#include <stdexcept>
11+
#include <vector>
1112

1213
class sim_t;
1314

@@ -26,10 +27,15 @@ class device_factory_t {
2627
virtual abstract_device_t* parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base) const = 0;
2728
virtual std::string generate_dts(const sim_t* sim) const = 0;
2829
virtual ~device_factory_t() {}
30+
void set_sargs(std::vector<std::string> sargs) { this->sargs = sargs; }
31+
std::vector<std::string> get_sargs() { return sargs; }
32+
33+
protected:
34+
std::vector<std::string> sargs;
2935
};
3036

3137
// Type for holding all registered MMIO plugins by name.
32-
using mmio_device_map_t = std::map<std::string, const device_factory_t*>;
38+
using mmio_device_map_t = std::map<std::string, device_factory_t*>;
3339

3440
mmio_device_map_t& mmio_device_map();
3541

@@ -40,8 +46,8 @@ mmio_device_map_t& mmio_device_map();
4046
std::string str(#name); \
4147
if (!mmio_device_map().emplace(str, this).second) throw std::runtime_error("Plugin \"" + str + "\" already registered"); \
4248
}; \
43-
name##_t* parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base) const override { return parse(fdt, sim, base); } \
49+
name##_t* parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base) const override { return parse(fdt, sim, base, sargs); } \
4450
std::string generate_dts(const sim_t* sim) const override { return generate(sim); } \
45-
}; const device_factory_t *name##_factory = new name##_factory_t();
51+
}; device_factory_t *name##_factory = new name##_factory_t();
4652

4753
#endif

riscv/clint.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ void clint_t::tick(reg_t rtc_ticks)
116116
}
117117
}
118118

119-
clint_t* clint_parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base) {
119+
clint_t* clint_parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base,
120+
const std::vector<std::string>& sargs) {
120121
if (fdt_parse_clint(fdt, base, "riscv,clint0") == 0)
121122
return new clint_t(sim,
122123
sim->CPU_HZ / sim->INSNS_PER_RTC_TICK,

riscv/ns16550.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ std::string ns16550_generate_dts(const sim_t* sim)
341341
return s.str();
342342
}
343343

344-
ns16550_t* ns16550_parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base)
344+
ns16550_t* ns16550_parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base, const std::vector<std::string>& sargs)
345345
{
346346
uint32_t ns16550_shift, ns16550_io_width, ns16550_int_id;
347347
if (fdt_parse_ns16550(fdt, base,

riscv/plic.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ std::string plic_generate_dts(const sim_t* sim)
415415
return s.str();
416416
}
417417

418-
plic_t* plic_parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base)
418+
plic_t* plic_parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base, const std::vector<std::string>& sargs)
419419
{
420420
uint32_t plic_ndev;
421421
if (fdt_parse_plic(fdt, base, &plic_ndev, "riscv,plic0") == 0)

riscv/sim.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern device_factory_t* ns16550_factory;
3838

3939
sim_t::sim_t(const cfg_t *cfg, bool halted,
4040
std::vector<std::pair<reg_t, abstract_mem_t*>> mems,
41-
std::vector<const device_factory_t*> plugin_device_factories,
41+
std::vector<device_factory_t*> plugin_device_factories,
4242
const std::vector<std::string>& args,
4343
const debug_module_config_t &dm_config,
4444
const char *log_path,

riscv/sim.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class sim_t : public htif_t, public simif_t
2727
public:
2828
sim_t(const cfg_t *cfg, bool halted,
2929
std::vector<std::pair<reg_t, abstract_mem_t*>> mems,
30-
std::vector<const device_factory_t*> plugin_device_factories,
30+
std::vector<device_factory_t*> plugin_device_factories,
3131
const std::vector<std::string>& args,
3232
const debug_module_config_t &dm_config, const char *log_path,
3333
bool dtb_enabled, const char *dtb_file,

spike_main/spike.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "extension.h"
1111
#include <dlfcn.h>
1212
#include <fesvr/option_parser.h>
13+
#include <stdexcept>
1314
#include <stdio.h>
1415
#include <stdlib.h>
1516
#include <vector>
@@ -18,6 +19,7 @@
1819
#include <fstream>
1920
#include <limits>
2021
#include <cinttypes>
22+
#include <sstream>
2123
#include "../VERSION"
2224

2325
static void help(int exit_code = 1)
@@ -332,7 +334,7 @@ int main(int argc, char** argv)
332334
bool dtb_enabled = true;
333335
const char* kernel = NULL;
334336
reg_t kernel_offset, kernel_size;
335-
std::vector<const device_factory_t*> plugin_device_factories;
337+
std::vector<device_factory_t*> plugin_device_factories;
336338
std::unique_ptr<icache_sim_t> ic;
337339
std::unique_ptr<dcache_sim_t> dc;
338340
std::unique_ptr<cache_sim_t> l2;
@@ -374,10 +376,24 @@ int main(int argc, char** argv)
374376
/*default_trigger_count=*/4);
375377

376378
auto const device_parser = [&plugin_device_factories](const char *s) {
377-
const std::string name(s);
379+
const std::string device_args(s);
380+
std::vector<std::string> parsed_args;
381+
std::stringstream sstr(device_args);
382+
while (sstr.good()) {
383+
std::string substr;
384+
getline(sstr, substr, ',');
385+
parsed_args.push_back(substr);
386+
}
387+
if (parsed_args.empty()) throw std::runtime_error("Plugin argument is empty.");
388+
389+
const std::string name = parsed_args[0];
378390
if (name.empty()) throw std::runtime_error("Plugin name is empty.");
391+
379392
auto it = mmio_device_map().find(name);
380393
if (it == mmio_device_map().end()) throw std::runtime_error("Plugin \"" + name + "\" not found in loaded extlibs.");
394+
395+
parsed_args.erase(parsed_args.begin());
396+
it->second->set_sargs(parsed_args);
381397
plugin_device_factories.push_back(it->second);
382398
};
383399

0 commit comments

Comments
 (0)