npc: split gdbstub api into seperate file
This commit is contained in:
parent
fed4ac225d
commit
85d7840804
15 changed files with 287 additions and 270 deletions
|
@ -1,6 +1,6 @@
|
|||
include(ChiselBuild)
|
||||
add_executable(V${TOPMODULE} config.cpp main.cpp)
|
||||
target_link_libraries(V${TOPMODULE} PRIVATE devices gdbstub)
|
||||
add_executable(V${TOPMODULE} config.cpp gdbstub_wrapper.cpp main.cpp)
|
||||
target_link_libraries(V${TOPMODULE} PRIVATE devices gdbstub spdlog::spdlog)
|
||||
target_include_directories(V${TOPMODULE} PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||
|
||||
verilate(
|
||||
|
@ -28,8 +28,8 @@ foreach(DIFFTEST_BINARY_FILE IN LISTS DIFFTEST_BINARY_FILES)
|
|||
unset(FILENAME)
|
||||
endforeach()
|
||||
|
||||
add_library(${TOPMODULE} SHARED config.cpp main.cpp)
|
||||
target_link_libraries(${TOPMODULE} PRIVATE devices gdbstub)
|
||||
add_library(${TOPMODULE} SHARED config.cpp gdbstub_wrapper.cpp)
|
||||
target_link_libraries(${TOPMODULE} PRIVATE devices gdbstub spdlog::spdlog)
|
||||
target_include_directories(${TOPMODULE} PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||
set_property(TARGET PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||
target_link_options(${TOPMODULE} PRIVATE -Wl,-E)
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
void Config::cli_parse(int argc, char **argv) {
|
||||
CLI::App app;
|
||||
app.add_option("-l,--listen", gdbsocket,
|
||||
"Listen to debugger at this address");
|
||||
app.add_flag("-g", do_debug, "Listen for gdb");
|
||||
app.add_option("-m,--memory", memory_file, "Content of memory")
|
||||
->required()
|
||||
->check(CLI::ExistingFile);
|
||||
app.add_flag("!--no-bin", memory_file_binary,
|
||||
"Memory file is in text format");
|
||||
|
@ -12,11 +14,6 @@ void Config::cli_parse(int argc, char **argv) {
|
|||
app.add_option("--diff-lib", lib_ref,
|
||||
"Dynamic library file of difftest reference")
|
||||
->check(CLI::ExistingFile);
|
||||
app.add_flag("--mtrace", do_mtrace, "Enable memory tracing");
|
||||
app.add_option(
|
||||
"--mtrace-range", mtrace_ranges,
|
||||
"Specify memory tracing range (default: 0x80000000-0x8fffffff)")
|
||||
->delimiter(',');
|
||||
|
||||
try {
|
||||
app.parse(argc, argv);
|
||||
|
|
196
npc/csrc/Flow/gdbstub_wrapper.cpp
Normal file
196
npc/csrc/Flow/gdbstub_wrapper.cpp
Normal file
|
@ -0,0 +1,196 @@
|
|||
extern "C" {
|
||||
#include <gdbstub.h>
|
||||
}
|
||||
#include "components.hpp"
|
||||
#include <VFlow.h>
|
||||
#include <config.hpp>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <devices.hpp>
|
||||
#include <types.h>
|
||||
#include <vl_wrapper.hpp>
|
||||
#include <vpi_user.h>
|
||||
#include <vpi_wrapper.hpp>
|
||||
|
||||
using Registers = _RegistersVPI<uint32_t, 32>;
|
||||
using VlModule = VlModuleInterfaceCommon<VFlow, Registers>;
|
||||
|
||||
// SDB::SDB<NPC::npc_interface> sdb_dut;
|
||||
bool g_skip_memcheck = false;
|
||||
VlModule *top;
|
||||
Registers *regs;
|
||||
vpiHandle pc = nullptr;
|
||||
|
||||
const size_t PMEM_START = 0x80000000;
|
||||
const size_t PMEM_END = 0x87ffffff;
|
||||
|
||||
extern "C" {
|
||||
/* === Memory Access === */
|
||||
using MMap = MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>;
|
||||
void *pmem_get() {
|
||||
static Devices::DeviceMap devices{
|
||||
new Devices::Serial(0x10000000, 0x1000),
|
||||
new Devices::RTC(0x10001000, 0x1000),
|
||||
};
|
||||
static auto pmem = new MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>(
|
||||
std::make_unique<Memory<128 * 1024>>(config.memory_file, true, PMEM_START,
|
||||
PMEM_END),
|
||||
std::make_unique<Devices::DeviceMap>(devices));
|
||||
return pmem;
|
||||
}
|
||||
|
||||
int pmem_read(int raddr) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<MMap *>(pmem);
|
||||
// TODO: Do memory difftest at memory read and write to diagnose at a finer
|
||||
// granularity
|
||||
mem->trace(raddr, true, regs->get_pc());
|
||||
if (g_skip_memcheck)
|
||||
return mem->read(PMEM_START);
|
||||
return mem->read(raddr);
|
||||
}
|
||||
|
||||
void pmem_write(int waddr, int wdata, char wmask) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<MMap *>(pmem);
|
||||
mem->trace((std::size_t)waddr, false, regs->get_pc(), wdata);
|
||||
return mem->write((std::size_t)waddr, wdata, wmask);
|
||||
}
|
||||
|
||||
/* === For gdbstub === */
|
||||
|
||||
int npc_read_mem(void *args, size_t addr, size_t len, void *val) {
|
||||
void *pmem = pmem_get();
|
||||
auto mmap = static_cast<MMap *>(pmem);
|
||||
return mmap->copy_to(addr, (uint8_t *)val, len);
|
||||
}
|
||||
|
||||
int npc_write_mem(void *args, size_t addr, size_t len, void *val) {
|
||||
void *pmem = pmem_get();
|
||||
auto mmap = static_cast<MMap *>(pmem);
|
||||
return mmap->copy_from(addr, (uint8_t *)val, len);
|
||||
}
|
||||
|
||||
int npc_read_reg(void *args, int regno, size_t *value) {
|
||||
if (regno == 32)
|
||||
*value = regs->get_pc();
|
||||
else
|
||||
*value = (*regs)[regno];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int npc_write_reg(void *args, int regno, size_t value) { return 1; }
|
||||
|
||||
inline void breakpoint_to_action(const Breakpoint *bp, gdb_action_t *res) {
|
||||
if (bp == nullptr) {
|
||||
res->reason = gdb_action_t::ACT_NONE;
|
||||
return;
|
||||
}
|
||||
switch (bp->type) {
|
||||
case BP_SOFTWARE:
|
||||
res->reason = gdb_action_t::ACT_BREAKPOINT;
|
||||
break;
|
||||
case BP_ACCESS:
|
||||
res->reason = gdb_action_t::ACT_WATCH;
|
||||
break;
|
||||
case BP_WRITE:
|
||||
res->reason = gdb_action_t::ACT_WWATCH;
|
||||
break;
|
||||
case BP_READ:
|
||||
res->reason = gdb_action_t::ACT_RWATCH;
|
||||
break;
|
||||
}
|
||||
res->data = bp->addr;
|
||||
}
|
||||
|
||||
void npc_cont(void *args, gdb_action_t *res) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
const Breakpoint *stopped_at = nullptr;
|
||||
stopped_at = top->cont(*dbg->bp);
|
||||
breakpoint_to_action(stopped_at, res);
|
||||
}
|
||||
|
||||
void npc_stepi(void *args, gdb_action_t *res) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
const Breakpoint *stopped_at = nullptr;
|
||||
stopped_at = top->stepi(*dbg->bp);
|
||||
breakpoint_to_action(stopped_at, res);
|
||||
}
|
||||
|
||||
bool npc_set_bp(void *args, size_t addr, bp_type_t type) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
for (const auto &bp : *dbg->bp) {
|
||||
if (bp.addr == addr && bp.type == type) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
dbg->bp->push_back({.addr = addr, .type = type});
|
||||
return true;
|
||||
}
|
||||
|
||||
bool npc_del_bp(void *args, size_t addr, bp_type_t type) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
for (auto it = dbg->bp->begin(); it != dbg->bp->end(); it++) {
|
||||
if (it->addr == addr && it->type == type) {
|
||||
std::swap(*it, *dbg->bp->rbegin());
|
||||
dbg->bp->pop_back();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void npc_on_interrupt(void *args) { ; }
|
||||
|
||||
void npc_init(void *args) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
void *mem = pmem_get();
|
||||
dbg->bp = new std::vector<Breakpoint>;
|
||||
|
||||
top = new VlModule;
|
||||
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
|
||||
top->setup(config.wavefile, regs);
|
||||
top->reset_eval(10);
|
||||
}
|
||||
|
||||
bool npc_do_difftest = true;
|
||||
|
||||
static gdbstub_t gdbstub_priv;
|
||||
arch_info_t npc_isa_arch_info{
|
||||
.target_desc = strdup(TARGET_RV32), .reg_num = 32, .reg_byte = 4};
|
||||
|
||||
size_t npc_dbg_state_size = sizeof(DbgState);
|
||||
|
||||
} // extern "C"
|
||||
|
||||
int gdbstub_loop() {
|
||||
DbgState dbg;
|
||||
|
||||
target_ops npc_gdbstub_ops = {.cont = npc_cont,
|
||||
.stepi = npc_stepi,
|
||||
.read_reg = npc_read_reg,
|
||||
.write_reg = npc_write_reg,
|
||||
.read_mem = npc_read_mem,
|
||||
.write_mem = npc_write_mem,
|
||||
.set_bp = npc_set_bp,
|
||||
.del_bp = npc_del_bp,
|
||||
.on_interrupt = NULL};
|
||||
|
||||
if (!config.do_debug) {
|
||||
gdb_action_t res;
|
||||
npc_init(&dbg);
|
||||
npc_cont(&dbg, &res);
|
||||
return !(res.reason == gdb_action_t::ACT_SHUTDOWN);
|
||||
}
|
||||
|
||||
if (!gdbstub_init(&gdbstub_priv, &npc_gdbstub_ops,
|
||||
(arch_info_t)npc_isa_arch_info, NULL,
|
||||
config.gdbsocket.c_str())) {
|
||||
return EINVAL;
|
||||
}
|
||||
npc_init(&dbg);
|
||||
|
||||
bool success = gdbstub_run(&gdbstub_priv, &dbg);
|
||||
// gdbstub_close(&gdbstub_priv);
|
||||
return !success;
|
||||
}
|
|
@ -1,180 +1,13 @@
|
|||
extern "C" {
|
||||
#include <gdbstub.h>
|
||||
}
|
||||
// #include "VFlow___024root.h"
|
||||
#include "components.hpp"
|
||||
#include <VFlow.h>
|
||||
#include <config.hpp>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <devices.hpp>
|
||||
#include <memory>
|
||||
#include <spdlog/cfg/env.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <types.h>
|
||||
#include <vector>
|
||||
#include <vl_wrapper.hpp>
|
||||
#include <vpi_user.h>
|
||||
#include <vpi_wrapper.hpp>
|
||||
|
||||
using Registers = _RegistersVPI<uint32_t, 32>;
|
||||
using VlModule = VlModuleInterfaceCommon<VFlow, Registers>;
|
||||
|
||||
// SDB::SDB<NPC::npc_interface> sdb_dut;
|
||||
bool g_skip_memcheck = false;
|
||||
VlModule *top;
|
||||
Registers *regs;
|
||||
vpiHandle pc = nullptr;
|
||||
|
||||
const size_t PMEM_START = 0x80000000;
|
||||
const size_t PMEM_END = 0x87ffffff;
|
||||
|
||||
struct DbgState {
|
||||
std::vector<Breakpoint> *bp;
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
|
||||
/* === Memory Access === */
|
||||
using MMap = MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>;
|
||||
void *pmem_get() {
|
||||
static Devices::DeviceMap devices{
|
||||
new Devices::Serial(0x10000000, 0x1000),
|
||||
new Devices::RTC(0x10001000, 0x1000),
|
||||
};
|
||||
static auto pmem = new MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>(
|
||||
std::make_unique<Memory<128 * 1024>>(
|
||||
"/nix/store/"
|
||||
"nv2c00y24qwz1jihfbaip9n1lskbzyb3-am-kernel-riscv32-none-elf-2024-07-"
|
||||
"10/share/am-kernels/add.bin",
|
||||
config.memory_file_binary, PMEM_START, PMEM_END),
|
||||
std::make_unique<Devices::DeviceMap>(devices), config.mtrace_ranges);
|
||||
return pmem;
|
||||
}
|
||||
|
||||
int pmem_read(int raddr) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<MMap *>(pmem);
|
||||
// TODO: Do memory difftest at memory read and write to diagnose at a finer
|
||||
// granularity
|
||||
if (config.do_mtrace)
|
||||
mem->trace(raddr, true, regs->get_pc());
|
||||
if (g_skip_memcheck)
|
||||
return mem->read(PMEM_START);
|
||||
return mem->read(raddr);
|
||||
}
|
||||
|
||||
void pmem_write(int waddr, int wdata, char wmask) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<MMap *>(pmem);
|
||||
if (config.do_mtrace)
|
||||
mem->trace((std::size_t)waddr, false, regs->get_pc(), wdata);
|
||||
return mem->write((std::size_t)waddr, wdata, wmask);
|
||||
}
|
||||
|
||||
/* === For gdbstub === */
|
||||
|
||||
int npc_read_mem(void *args, size_t addr, size_t len, void *val) {
|
||||
void *pmem = pmem_get();
|
||||
auto mmap = static_cast<MMap *>(pmem);
|
||||
mmap->copy_to(addr, (uint8_t *)val, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int npc_write_mem(void *args, size_t addr, size_t len, void *val) {
|
||||
void *pmem = pmem_get();
|
||||
auto mmap = static_cast<MMap *>(pmem);
|
||||
mmap->copy_from(addr, (uint8_t *)val, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int npc_read_reg(void *args, int regno, size_t *value) {
|
||||
if (regno == 32)
|
||||
*value = regs->get_pc();
|
||||
else
|
||||
*value = (*regs)[regno];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int npc_write_reg(void *args, int regno, size_t value) { return 1; }
|
||||
|
||||
void npc_cont(void *args, gdb_action_t *res) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
*res = top->eval(*dbg->bp);
|
||||
}
|
||||
|
||||
void npc_stepi(void *args, gdb_action_t *res) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
*res = top->eval(*dbg->bp);
|
||||
}
|
||||
|
||||
bool npc_set_bp(void *args, size_t addr, bp_type_t type) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
for (const auto &bp : *dbg->bp) {
|
||||
if (bp.addr == addr && bp.type == type) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
dbg->bp->push_back({.addr = addr, .type = type});
|
||||
return true;
|
||||
}
|
||||
|
||||
bool npc_del_bp(void *args, size_t addr, bp_type_t type) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
for (auto it = dbg->bp->begin(); it != dbg->bp->end(); it++) {
|
||||
if (it->addr == addr && it->type == type) {
|
||||
std::swap(*it, *dbg->bp->rbegin());
|
||||
dbg->bp->pop_back();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void npc_on_interrupt(void *args) { ; }
|
||||
|
||||
void npc_init(void *args) {
|
||||
DbgState *dbg = (DbgState *)args;
|
||||
dbg->bp = new std::vector<Breakpoint>;
|
||||
|
||||
top = new VlModule;
|
||||
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
|
||||
top->setup(config.wavefile, regs);
|
||||
top->reset_eval(10);
|
||||
}
|
||||
|
||||
static gdbstub_t gdbstub_priv;
|
||||
arch_info_t isa_arch_info = {
|
||||
.target_desc = strdup(TARGET_RV32), .reg_num = 32, .reg_byte = 4};
|
||||
|
||||
size_t argsize = sizeof(DbgState);
|
||||
|
||||
} // extern "C"
|
||||
|
||||
int gdbstub_loop() {
|
||||
DbgState dbg;
|
||||
|
||||
target_ops npc_gdbstub_ops = {.cont = npc_cont,
|
||||
.stepi = npc_stepi,
|
||||
.read_reg = npc_read_reg,
|
||||
.write_reg = npc_write_reg,
|
||||
.read_mem = npc_read_mem,
|
||||
.write_mem = npc_write_mem,
|
||||
.set_bp = npc_set_bp,
|
||||
.del_bp = npc_del_bp,
|
||||
.on_interrupt = NULL};
|
||||
|
||||
if (!gdbstub_init(&gdbstub_priv, &npc_gdbstub_ops, (arch_info_t)isa_arch_info,
|
||||
strdup("/tmp/gdbstub-npc.sock"))) {
|
||||
return EINVAL;
|
||||
}
|
||||
npc_init(&dbg);
|
||||
|
||||
bool success = gdbstub_run(&gdbstub_priv, &dbg);
|
||||
// gdbstub_close(&gdbstub_priv);
|
||||
return !success;
|
||||
}
|
||||
int gdbstub_loop();
|
||||
|
||||
int main(int argc, char **argv, char **env) {
|
||||
spdlog::cfg::load_env_levels();
|
||||
config.cli_parse(argc, argv);
|
||||
|
||||
spdlog::debug("Configuration parsed");
|
||||
return gdbstub_loop();
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
add_library(devices serial.cpp rtc.cpp)
|
||||
|
||||
target_include_directories(devices PUBLIC include)
|
||||
target_link_libraries(devices PRIVATE spdlog::spdlog)
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <spdlog/logger.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <utility>
|
||||
|
||||
namespace Devices {
|
||||
|
@ -55,6 +53,7 @@ public:
|
|||
|
||||
class DeviceMap {
|
||||
std::map<uint64_t, Device *> addr_to_device;
|
||||
std::shared_ptr<spdlog::logger> logger = spdlog::stdout_color_mt("devicemap");
|
||||
|
||||
public:
|
||||
DeviceMap(std::initializer_list<Device *> devices) {
|
||||
|
@ -66,8 +65,8 @@ public:
|
|||
auto it = addr_to_device.upper_bound(addr);
|
||||
if (it == addr_to_device.begin() ||
|
||||
(--it)->second->addr + it->second->len <= addr) {
|
||||
std::cerr << "Use of a unintialized device at memory addr: 0x" << std::hex
|
||||
<< addr << std::dec << std::endl;
|
||||
logger->error("Accessed an unintialized device at memory addr: 0x{:x}",
|
||||
addr);
|
||||
return false;
|
||||
}
|
||||
auto &device = it->second;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue