diff options
author | Rupert Swarbrick <rswarbrick@gmail.com> | 2022-02-19 00:02:11 +0000 |
---|---|---|
committer | Rupert Swarbrick <rswarbrick@gmail.com> | 2022-03-18 15:30:41 +0000 |
commit | b742ddc66fd4ccb6cec98cbd8ebb705fe93ca735 (patch) | |
tree | a5c2f32b7c7996732f72b8142971610eb68f2a67 | |
parent | 1fea2afbf46d2641d77f2db3d6108e0897431a84 (diff) | |
download | spike-b742ddc66fd4ccb6cec98cbd8ebb705fe93ca735.zip spike-b742ddc66fd4ccb6cec98cbd8ebb705fe93ca735.tar.gz spike-b742ddc66fd4ccb6cec98cbd8ebb705fe93ca735.tar.bz2 |
Initial step towards factoring out command line configuration
This commit defines a "cfg_t" structure, which currently just holds
the initrd address range. It will be augmented in future commits to
hold other configuration arguments as well.
To represent a configuration argument, we define an arg_t base class.
This holds a current value, together with a flag that tells us whether
the value has been updated from the default. The idea is that in
future we're going to use that flag when reading a DTB file: if an
argument has actually been specified on the command line, we need to
take it into account; if not, we can ignore the default and use the
DTB file's supplied value.
-rw-r--r-- | riscv/cfg.h | 38 | ||||
-rw-r--r-- | riscv/riscv.mk.in | 1 | ||||
-rw-r--r-- | riscv/sim.cc | 14 | ||||
-rw-r--r-- | riscv/sim.h | 11 | ||||
-rw-r--r-- | spike_main/spike.cc | 15 |
5 files changed, 61 insertions, 18 deletions
diff --git a/riscv/cfg.h b/riscv/cfg.h new file mode 100644 index 0000000..bee5051 --- /dev/null +++ b/riscv/cfg.h @@ -0,0 +1,38 @@ +// See LICENSE for license details. +#ifndef _RISCV_CFG_H +#define _RISCV_CFG_H + +#include "decode.h" + +template <typename T> +class cfg_arg_t { +public: + cfg_arg_t(T default_val) + : value(default_val), was_set(false) {} + + bool overridden() const { return was_set; } + + T operator()() const { return value; } + + T operator=(const T v) { + value = v; + was_set = true; + return value; + } + +private: + T value; + bool was_set; +}; + +class cfg_t +{ +public: + cfg_t(std::pair<reg_t, reg_t> default_initrd_bounds) + : initrd_bounds(default_initrd_bounds) + {} + + cfg_arg_t<std::pair<reg_t, reg_t>> initrd_bounds; +}; + +#endif diff --git a/riscv/riscv.mk.in b/riscv/riscv.mk.in index a160bb8..a803b2a 100644 --- a/riscv/riscv.mk.in +++ b/riscv/riscv.mk.in @@ -14,6 +14,7 @@ riscv_hdrs = \ devices.h \ dts.h \ mmu.h \ + cfg.h \ processor.h \ sim.h \ simif.h \ diff --git a/riscv/sim.cc b/riscv/sim.cc index 064a493..9bfa17a 100644 --- a/riscv/sim.cc +++ b/riscv/sim.cc @@ -28,9 +28,9 @@ static void handle_signal(int sig) signal(sig, &handle_signal); } -sim_t::sim_t(const char* isa_string, const char* priv, const char* varch, - size_t nprocs, bool halted, bool real_time_clint, - reg_t initrd_start, reg_t initrd_end, const char* bootargs, +sim_t::sim_t(const cfg_t *cfg, + const char* isa_string, const char* priv, const char* varch, + size_t nprocs, bool halted, bool real_time_clint, const char* bootargs, reg_t start_pc, std::vector<std::pair<reg_t, mem_t*>> mems, std::vector<std::pair<reg_t, abstract_device_t*>> plugin_devices, const std::vector<std::string>& args, @@ -44,11 +44,10 @@ sim_t::sim_t(const char* isa_string, const char* priv, const char* varch, FILE *cmd_file) // needed for command line option --cmd : htif_t(args), isa(isa_string, priv), + cfg(cfg), mems(mems), plugin_devices(plugin_devices), procs(std::max(nprocs, size_t(1))), - initrd_start(initrd_start), - initrd_end(initrd_end), bootargs(bootargs), start_pc(start_pc), dtb_file(dtb_file ? dtb_file : ""), @@ -312,7 +311,10 @@ void sim_t::make_dtb() dtb = strstream.str(); } else { - dts = make_dts(INSNS_PER_RTC_TICK, CPU_HZ, initrd_start, initrd_end, bootargs, procs, mems); + std::pair<reg_t, reg_t> initrd_bounds = cfg->initrd_bounds(); + dts = make_dts(INSNS_PER_RTC_TICK, CPU_HZ, + initrd_bounds.first, initrd_bounds.second, + bootargs, procs, mems); dtb = dts_compile(dts); } diff --git a/riscv/sim.h b/riscv/sim.h index 518d75e..31c9254 100644 --- a/riscv/sim.h +++ b/riscv/sim.h @@ -11,6 +11,7 @@ #include <boost/asio.hpp> #endif +#include "cfg.h" #include "debug_module.h" #include "devices.h" #include "log_file.h" @@ -31,9 +32,9 @@ class remote_bitbang_t; class sim_t : public htif_t, public simif_t { public: - sim_t(const char* isa, const char* priv, const char* varch, size_t _nprocs, - bool halted, bool real_time_clint, - reg_t initrd_start, reg_t initrd_end, const char* bootargs, + sim_t(const cfg_t *cfg, + const char* isa, const char* priv, const char* varch, size_t _nprocs, + bool halted, bool real_time_clint, const char* bootargs, reg_t start_pc, std::vector<std::pair<reg_t, mem_t*>> mems, std::vector<std::pair<reg_t, abstract_device_t*>> plugin_devices, const std::vector<std::string>& args, const std::vector<int> hartids, @@ -71,12 +72,12 @@ public: private: isa_parser_t isa; + const cfg_t * const cfg; std::vector<std::pair<reg_t, mem_t*>> mems; std::vector<std::pair<reg_t, abstract_device_t*>> plugin_devices; mmu_t* debug_mmu; // debug port into main memory std::vector<processor_t*> procs; - reg_t initrd_start; - reg_t initrd_end; + std::pair<reg_t, reg_t> initrd_range; const char* bootargs; reg_t start_pc; std::string dts; diff --git a/spike_main/spike.cc b/spike_main/spike.cc index efef96d..6fb952d 100644 --- a/spike_main/spike.cc +++ b/spike_main/spike.cc @@ -1,5 +1,6 @@ // See LICENSE for license details. +#include "cfg.h" #include "sim.h" #include "mmu.h" #include "remote_bitbang.h" @@ -221,8 +222,6 @@ int main(int argc, char** argv) size_t nprocs = 1; const char* kernel = NULL; reg_t kernel_offset, kernel_size; - size_t initrd_size; - reg_t initrd_start = 0, initrd_end = 0; const char* bootargs = NULL; reg_t start_pc = reg_t(-1); std::vector<std::pair<reg_t, mem_t*>> mems; @@ -254,6 +253,7 @@ int main(int argc, char** argv) .support_impebreak = true }; std::vector<int> hartids; + cfg_t cfg(/*default_initrd_bounds=*/std::make_pair((reg_t)0, (reg_t)0)); auto const hartids_parser = [&](const char *s) { std::string const str(s); @@ -409,11 +409,12 @@ int main(int argc, char** argv) } if (initrd && check_file_exists(initrd)) { - initrd_size = get_file_size(initrd); + size_t initrd_size = get_file_size(initrd); for (auto& m : mems) { if (initrd_size && (initrd_size + 0x1000) < m.second->size()) { - initrd_end = m.first + m.second->size() - 0x1000; - initrd_start = initrd_end - initrd_size; + reg_t initrd_end = m.first + m.second->size() - 0x1000; + reg_t initrd_start = initrd_end - initrd_size; + cfg.initrd_bounds = std::make_pair(initrd_start, initrd_end); read_file_bytes(initrd, 0, m.second, initrd_start - m.first, initrd_size); break; } @@ -443,8 +444,8 @@ int main(int argc, char** argv) } #endif - sim_t s(isa, priv, varch, nprocs, halted, real_time_clint, - initrd_start, initrd_end, bootargs, start_pc, mems, plugin_devices, htif_args, + sim_t s(&cfg, isa, priv, varch, nprocs, halted, real_time_clint, + bootargs, start_pc, mems, plugin_devices, htif_args, std::move(hartids), dm_config, log_path, dtb_enabled, dtb_file, #ifdef HAVE_BOOST_ASIO io_service_ptr, acceptor_ptr, |