From 0ea4cba1cadf2c6e7dde9090f17c74c0d7527ca5 Mon Sep 17 00:00:00 2001 From: Eyck Jentzsch Date: Tue, 12 Oct 2021 14:24:55 +0200 Subject: [PATCH] add dynamic plugin loading --- CMakeLists.txt | 16 ++++++++++----- incl/sysc/core_complex.h | 9 +++++++++ src/main.cpp | 16 +++++++++++++-- src/sysc/core_complex.cpp | 41 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5098982..6a7e14c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,11 +40,17 @@ set(LIB_SOURCES ) if(WITH_LLVM) - set(LIB_SOURCES ${LIB_SOURCES} - src/vm/llvm/fp_impl.cpp - #src/vm/llvm/vm_tgf_b.cpp - #src/vm/llvm/vm_tgf_c.cpp - ) + FILE(GLOB TGC_LLVM_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/src/vm/llvm/vm_*.cpp + ) + list(APPEND LIB_SOURCES ${TGC_LLVM_SOURCES}) +endif() + +if(WITH_TCC) + FILE(GLOB TGC_TCC_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/src/vm/tcc/vm_*.cpp + ) + list(APPEND LIB_SOURCES ${TGC_TCC_SOURCES}) endif() # Define the library diff --git a/incl/sysc/core_complex.h b/incl/sysc/core_complex.h index 4668837..33db30b 100644 --- a/incl/sysc/core_complex.h +++ b/incl/sysc/core_complex.h @@ -48,6 +48,9 @@ #include #include +namespace iss { + class vm_plugin; +} namespace sysc { class tlm_dmi_ext : public tlm::tlm_dmi { @@ -99,6 +102,8 @@ public: cci::cci_param mhartid{"mhartid", 0}; + cci::cci_param plugins{"plugins", ""}; + core_complex(sc_core::sc_module_name const& name); #else @@ -122,6 +127,8 @@ public: scml_property mhartid{"mhartid", 0}; + scml_property plugins{"plugins", ""}; + core_complex(sc_core::sc_module_name const& name) : sc_module(name) , local_irq_i{"local_irq_i", 16} @@ -185,6 +192,8 @@ protected: std::unique_ptr t2t; private: void init(); + std::vector plugin_list; + }; } /* namespace SiFive */ } /* namespace sysc */ diff --git a/src/main.cpp b/src/main.cpp index 5ccae24..358d4a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,6 +60,10 @@ using tgc_d_xrb_mac_plat_type = iss::arch::riscv_hart_mu_p #include #include +#include +#if defined(HAS_LUA) +#include +#endif namespace po = boost::program_options; @@ -172,8 +176,16 @@ int main(int argc, char *argv[]) { vm->register_plugin(*ce_plugin); plugin_list.push_back(ce_plugin); } else { - LOG(ERR) << "Unknown plugin name: " << plugin_name << ", valid names are 'ce', 'ic'" << std::endl; - return 127; + std::array a{{filename.c_str()}}; + iss::plugin::loader l(plugin_name, {{"initPlugin"}}); + auto* plugin = l.call_function("initPlugin", a.size(), a.data()); + if(plugin){ + vm->register_plugin(*plugin); + plugin_list.push_back(plugin); + } else { + LOG(ERR) << "Unknown plugin name: " << plugin_name << ", valid names are 'ce', 'ic'" << std::endl; + return 127; + } } } } diff --git a/src/sysc/core_complex.cpp b/src/sysc/core_complex.cpp index a304c82..fbe30e2 100644 --- a/src/sysc/core_complex.cpp +++ b/src/sysc/core_complex.cpp @@ -36,7 +36,8 @@ #include "iss/debugger/server.h" #include "iss/debugger/target_adapter_if.h" #include "iss/iss.h" -#include "iss/vm_types.h" +#include "iss/vm_types.h" +#include #include "sysc/core_complex.h" #ifdef CORE_TGC_B #include "iss/arch/riscv_hart_m_p.h" @@ -56,10 +57,13 @@ using tgc_d_plat_type = iss::arch::riscv_hart_mu_p; #endif -#include "scc/report.h" +#include +#include #include #include #include +#include +#include // clang-format on #define STR(X) #X @@ -380,6 +384,8 @@ void core_complex::init(){ core_complex::~core_complex(){ delete cpu; delete trc; + for (auto *p : plugin_list) + delete p; } void core_complex::trace(sc_trace_file *trf) const {} @@ -391,6 +397,37 @@ void core_complex::before_end_of_elaboration() { cpu->create_cpu(GET_PROP_VALUE(core_type), GET_PROP_VALUE(backend), GET_PROP_VALUE(gdb_server_port), GET_PROP_VALUE(mhartid)); sc_assert(cpu->vm!=nullptr); cpu->vm->setDisassEnabled(GET_PROP_VALUE(enable_disass) || trc->m_db != nullptr); + if (GET_PROP_VALUE(core_type).length()) { + auto p = util::split(GET_PROP_VALUE(plugins), ';'); + for (std::string const& opt_val : p) { + std::string plugin_name=opt_val; + std::string filename{"cycles.txt"}; + std::size_t found = opt_val.find('='); + if (found != std::string::npos) { + plugin_name = opt_val.substr(0, found); + filename = opt_val.substr(found + 1, opt_val.size()); + } + if (plugin_name == "ic") { + auto *plugin = new iss::plugin::instruction_count(filename); + cpu->vm->register_plugin(*plugin); + plugin_list.push_back(plugin); + } else if (plugin_name == "ce") { + auto *plugin = new iss::plugin::cycle_estimate(filename); + cpu->vm->register_plugin(*plugin); + plugin_list.push_back(plugin); + } else { + std::array a{{filename.c_str()}}; + iss::plugin::loader l(plugin_name, {{"initPlugin"}}); + auto* plugin = l.call_function("initPlugin", a.size(), a.data()); + if(plugin){ + cpu->vm->register_plugin(*plugin); + plugin_list.push_back(plugin); + } else + SCCERR(SCMOD) << "Unknown plugin '" << plugin_name << "' or plugin not found"; + } + } + } + } void core_complex::start_of_simulation() {