Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@

# Directories
build/

# IDEs and LSP specific
.vscode/
__pycache__/
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include(cmake/defaults.cmake)

add_subdirectory(third_party)
add_subdirectory(scripts)
add_subdirectory(plugins)

set(hSIM_LIB
src/memory.cc src/machine.cc
Expand Down
12 changes: 12 additions & 0 deletions include/machine.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
#define HSIM_MACHINE_INCLUDED

#include <memory>
#include <string>
#include <utility>

#include "config.hh"
#include "cpu_state.hh"
#include "elf_loader.hh"
#include "executors.hh"
#include "machine_event.hh"
#include "memory.hh"

namespace hsim {
Expand All @@ -32,9 +35,18 @@ class Machine final {
}
}

void addEventConsumer(IEventConsumerHandle consumer) {
m_eventManager.attach(std::move(consumer));
}

template <typename Event, typename... Args> void notify(Args &&...args) {
m_eventManager.notify<Event>(std::forward<Args>(args)...);
}

private:
std::unique_ptr<CpuState> m_state;
Memory m_mem{};
EventManager m_eventManager;
};

} // namespace hsim
Expand Down
45 changes: 45 additions & 0 deletions include/machine_event.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef HSIM_MACHINE_EVENT_INCLUDED
#define HSIM_MACHINE_EVENT_INCLUDED

#include <list>
#include <memory>
#include <utility>

#include "support.hh"

namespace hsim {

struct MemRead {};
struct MemWrite {};
struct PreInsn {};
struct PostInsn {};

class IEventConsumer {
public:
virtual ~IEventConsumer() = default;
virtual void handle(MemRead event, Addr addr) = 0;
virtual void handle(MemWrite event, Addr addr, Word value) = 0;
virtual void handle(PreInsn event, Word insn) = 0;
virtual void handle(PostInsn event, Word insn) = 0;
};

using IEventConsumerHandle = std::unique_ptr<IEventConsumer>;
class EventManager final {
public:
template <typename Event, typename... Args> void notify(Args &&...args) {
for (auto &consumer : m_consumers) {
consumer->handle(Event{}, std::forward<Args>(args)...);
}
}
void attach(IEventConsumerHandle consumer) {
m_consumers.push_back(std::move(consumer));
}
void detach(IEventConsumerHandle consumer) { m_consumers.remove(consumer); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused a bit with thus function. How could you remove plugin by address, if you've lost it after attach() call?
e.g.:

EventManager ev;
auto consumer = std::make_unique<IntConsumer>(42);
ev.attach(std::move(consumer));
// consumer is in moved-from state hereafter
ev.detach(???); // what to pass here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused a bit with thus function. How could you remove plugin by address, if you've lost it after attach() call? e.g.:

EventManager ev;
auto consumer = std::make_unique<IntConsumer>(42);
ev.attach(std::move(consumer));
// consumer is in moved-from state hereafter
ev.detach(???); // what to pass here?

is detach necessary at all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this method


private:
std::list<IEventConsumerHandle> m_consumers;
};

} // namespace hsim

#endif // HSIM_MACHINE_EVENT_INCLUDED
68 changes: 68 additions & 0 deletions include/plugin.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef HSIM_PLUGIN_INCLUDED
#define HSIM_PLUGIN_INCLUDED

#include <filesystem>
#include <memory>
#include <string>

#include <dlfcn.h>

#include "machine_event.hh"
#include "so_loader.hh"
#include "support.hh"

namespace hsim {

class IPlugin {
public:
virtual ~IPlugin() = default;
virtual void handle(MemRead event, Addr addr) = 0;
virtual void handle(MemWrite event, Addr addr, Word value) = 0;
virtual void handle(PreInsn event, Word insn) = 0;
virtual void handle(PostInsn event, Word insn) = 0;
};

using LoadPluginFunc = IPlugin *(*)(const std::string &options);
using UnloadPluginFunc = void (*)(IPlugin *plugin);

constexpr std::string kLoadPluginFuncName = "loadPlugin";
constexpr std::string kUnloadPluginFuncName = "unloadPlugin";

// NOTE plugins should use this defines when declaring a function
#define HSIM_LOAD_PLUGIN_FUNC extern "C" hsim::IPlugin *loadPlugin
#define HSIM_UNLOAD_PLUGIN_FUNC extern "C" void unloadPlugin

class PluginConsumer : public IEventConsumer {
public:
PluginConsumer(const std::filesystem::path &path,
const std::string &options)
: m_sharedLib{path, kLazy}, m_plugin{nullptr, nullptr} {
auto loadFunc = m_sharedLib.get<LoadPluginFunc>(kLoadPluginFuncName);
auto unloadFunc =
m_sharedLib.get<UnloadPluginFunc>(kUnloadPluginFuncName);

m_plugin = std::unique_ptr<IPlugin, UnloadPluginFunc>(loadFunc(options),
unloadFunc);
}

void handle(MemRead event, Addr addr) override {
m_plugin->handle(event, addr);
}
void handle(MemWrite event, Addr addr, Word value) override {
m_plugin->handle(event, addr, value);
}
void handle(PreInsn event, Word insn) override {
m_plugin->handle(event, insn);
}
void handle(PostInsn event, Word insn) override {
m_plugin->handle(event, insn);
}

private:
SharedLib m_sharedLib;
std::unique_ptr<IPlugin, UnloadPluginFunc> m_plugin;
};

} // namespace hsim

#endif // HSIM_PLUGIN_INCLUDED
54 changes: 54 additions & 0 deletions include/so_loader.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef HSIM_SO_LOADER_INCLUDED
#define HSIM_SO_LOADER_INCLUDED

#include <cstdint>
#include <filesystem>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>

#include <dlfcn.h>

namespace hsim {

enum SharedLibMode : std::uint16_t {
kLazy = RTLD_LAZY,
kNow = RTLD_NOW,
kGlobal = RTLD_GLOBAL,
kLocal = RTLD_LOCAL,
};

template <typename T>
concept PointerT = std::is_pointer_v<T>;

class SharedLib {
private:
struct DlCloser {
void operator()(void *handle) { dlclose(handle); }
};

public:
SharedLib(const std::filesystem::path &libPath, SharedLibMode mode)
: m_handle{dlopen(libPath.c_str(), mode)} {
if (m_handle == nullptr) {
throw std::runtime_error{dlerror()};
}
}
template <PointerT T> T get(const std::string &symbol) {
void *loadedSymbol = dlsym(m_handle.get(), symbol.c_str());
if (loadedSymbol == nullptr) {
throw std::runtime_error{dlerror()};
}

return reinterpret_cast<T>(loadedSymbol);
}

private:
std::unique_ptr<void, DlCloser> m_handle;
static_assert(sizeof(m_handle) == sizeof(void *));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, it is not required by standard

};

} // namespace hsim

#endif // HSIM_SO_LOADER_INCLUDED
15 changes: 15 additions & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
add_library(simple_plugin SHARED
simple_plugin.cc
)

target_include_directories(simple_plugin
PRIVATE
${CMAKE_SOURCE_DIR}/include
)

set(PLUGIN_OUTPUT_DIR "${CMAKE_BINARY_DIR}/plugins")
file(MAKE_DIRECTORY ${PLUGIN_OUTPUT_DIR})

set_target_properties(simple_plugin PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PLUGIN_OUTPUT_DIR}
)
49 changes: 49 additions & 0 deletions plugins/simple_plugin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <string>
#include <utility>

#include "machine_event.hh"
#include "plugin.hh"
#include "support.hh"

namespace hsim {

class SimplePlugin : public hsim::IPlugin {
public:
SimplePlugin(std::string name) : m_name{std::move(name)} {
std::cout << "SimplePlugin: " << m_name << std::endl;
}

~SimplePlugin() override {
std::cout << "~SimplePlugin: " << m_name << std::endl;
}

void handle([[maybe_unused]] MemRead event, Addr addr) override {
std::cout << m_name << " MemRead: " << std::hex << addr << std::endl;
}

void handle([[maybe_unused]] MemWrite event, Addr addr,
Word value) override {
std::cout << m_name << " MemWrite: " << std::hex << addr << " " << value
<< std::endl;
}

void handle([[maybe_unused]] PreInsn event, Word insn) override {
std::cout << m_name << " PreInsn: " << std::hex << insn << std::endl;
}

void handle([[maybe_unused]] PostInsn event, Word insn) override {
std::cout << m_name << " PostInsn: " << std::hex << insn << std::endl;
}

private:
std::string m_name;
};

HSIM_LOAD_PLUGIN_FUNC(const std::string &options) {
return new SimplePlugin{options};
}

HSIM_UNLOAD_PLUGIN_FUNC(SimplePlugin *plugin) { delete plugin; }

} // namespace hsim
15 changes: 11 additions & 4 deletions src/main.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include <exception>
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <memory>
//
#include <CLI/CLI.hpp>

#include "config.hh"
#include "machine.hh"
#include "machine_event.hh"
#include "plugin.hh"

int main(int argc, char **argv) try {
CLI::App app("hSim: high Performance CPU Simulator");
Expand All @@ -14,9 +18,7 @@ int main(int argc, char **argv) try {
->required()
->check(CLI::ExistingFile);

app.add_option("--log", config.log_path, "Path to log file")
->required()
->check(CLI::ExistingFile);
app.add_option("--log", config.log_path, "Path to log file")->required();

app.add_flag("--dump-exec", config.dump_exec,
"Option to enable dump of state on execution")
Expand All @@ -32,6 +34,11 @@ int main(int argc, char **argv) try {

machine.run();

// std::filesystem::path pluginPath = "./build/plugins/libsimple_plugin.so";
// machine.addEventConsumer(
// std::make_unique<hsim::PluginConsumer>(pluginPath, "vova"));
// machine.notify<hsim::MemWrite>(0, 0);

return 0;
} catch (const std::exception &e) {
std::cout << e.what() << '\n';
Expand Down