npc: split gdbstub api into seperate file

This commit is contained in:
xinyangli 2024-08-01 18:53:17 +08:00
parent fed4ac225d
commit 85d7840804
Signed by: xin
SSH key fingerprint: SHA256:qZ/tzd8lYRtUFSrfBDBMcUqV4GHKxqeqRA3huItgvbk
15 changed files with 287 additions and 270 deletions

View file

@ -3,23 +3,21 @@
#define _NPC_COMPONENTS_H_
#include "types.h"
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <stdexcept>
#include <string>
#include <vector>
template <typename T, std::size_t nr> class _RegistersBase {
std::array<T, nr> regs;
T pc;
std::shared_ptr<spdlog::logger> logger = spdlog::stdout_color_mt("registers");
virtual T fetch_pc() const;
virtual T fetch_reg(std::size_t id) const;
@ -33,12 +31,6 @@ public:
}
};
// class MemoryFile {
// std::filesystem::path filepath;
// public:
// };
template <std::size_t n> class Memory {
paddr_t pmem_start, pmem_end;
@ -83,15 +75,13 @@ public:
template <typename Mem, typename DevMap> class MemoryMap {
std::unique_ptr<Mem> ram;
std::unique_ptr<DevMap> devices;
const std::vector<std::array<uint64_t, 2>> &trace_ranges;
std::shared_ptr<spdlog::logger> logger = spdlog::stdout_color_mt("mmap");
public:
MemoryMap(std::unique_ptr<Mem> &&ram, std::unique_ptr<DevMap> &&devices,
const std::vector<std::array<uint64_t, 2>> &trace_ranges)
: ram(std::move(ram)), devices(std::move(devices)),
trace_ranges(trace_ranges) {}
MemoryMap(std::unique_ptr<Mem> &&ram, std::unique_ptr<DevMap> &&devices)
: ram(std::move(ram)), devices(std::move(devices)) {}
void write(paddr_t waddr, word_t wdata, char wmask) {
// printf("waddr: 0x%x\n", waddr);
size_t len = (wmask & 1) + ((wmask & 2) >> 1) + ((wmask & 4) >> 2) +
((wmask & 8) >> 3);
if (ram->in_pmem(waddr)) {
@ -99,47 +89,38 @@ public:
} else if (devices->handle(waddr, (uint8_t *)&wdata, len, true)) {
}
}
word_t read(paddr_t raddr) const {
word_t res = 0;
// printf("raddr: 0x%x, in_pmem: %d\n", raddr, ram->in_pmem(raddr));
if (ram->in_pmem(raddr)) {
ram->transfer(raddr, (uint8_t *)&res, 4, false);
} else if (devices->handle(raddr, (uint8_t *)&res, 4, false)) {
}
return res;
}
void copy_to(paddr_t addr, uint8_t *buf, size_t len) const {
int copy_to(paddr_t addr, uint8_t *buf, size_t len) const {
if (ram->in_pmem(addr)) {
ram->transfer(addr, buf, len, false);
return 0;
} else {
std::cerr << "0x" << std::hex << addr << " not in pmem" << std::endl;
return EINVAL;
}
}
void copy_from(paddr_t addr, uint8_t *buf, size_t len) {
int copy_from(paddr_t addr, uint8_t *buf, size_t len) {
if (ram->in_pmem(addr)) {
ram->transfer(addr, buf, len, true);
return 0;
} else {
std::cerr << "0x" << std::hex << addr << " not in pmem" << std::endl;
return EINVAL;
}
}
void *get_pmem() { return ram->mem.data(); }
void trace(paddr_t addr, bool is_read, word_t pc = 0, word_t value = 0) {
for (auto &r : trace_ranges) {
if (r[0] <= addr && r[1] >= addr) {
std::stringstream os;
if (pc != 0)
os << "0x" << std::hex << pc << " ";
if (is_read)
os << "[R] "
<< "0x" << addr << ": 0x" << this->read(addr);
else
os << "[W] " << value << " -> "
<< "0x" << addr;
os << std::dec << std::endl;
std::cout << os.rdbuf();
break;
}
}
logger->trace("[{}] 0x{:x}", is_read ? 'R' : 'W', this->read(addr));
}
};
#endif

View file

@ -3,17 +3,14 @@
#include <CLI/App.hpp>
#include <CLI/CLI.hpp>
#include <CLI/Validators.hpp>
#include <cstddef>
#include <filesystem>
#include <utility>
struct Config {
std::filesystem::path memory_file;
std::string gdbsocket = "gdbstub-npc.sock";
bool do_debug{false};
bool interactive{false};
bool memory_file_binary = {true};
bool do_mtrace{false};
std::vector<std::array<std::size_t, 2>> mtrace_ranges{
{0x80000000, 0x8ffffffff}};
std::filesystem::path wavefile;
std::filesystem::path lib_ref;
void cli_parse(int argc, char **argv);

View file

@ -1,6 +1,11 @@
#ifndef _NPC_TYPES_H__
#define _NPC_TYPES_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <gdbstub.h>
#include <inttypes.h>
#include <stddef.h>
typedef uint32_t word_t;
typedef int32_t sword_t;
@ -16,9 +21,19 @@ typedef uint32_t paddr_t;
#define FMT_ADDR "0x%08x"
typedef uint16_t ioaddr_t;
struct Breakpoint {
size_t addr;
bp_type_t type;
};
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
#include <map>
#include <string>
#include <vector>
const std::map<std::string, int> riscv32_regs_by_name{
{"$0", 0}, {"ra", 1}, {"sp", 2}, {"gp", 3}, {"tp", 4}, {"t0", 5},
@ -27,12 +42,10 @@ const std::map<std::string, int> riscv32_regs_by_name{
{"s2", 18}, {"s3", 19}, {"s4", 20}, {"s5", 21}, {"s6", 22}, {"s7", 23},
{"s8", 24}, {"s9", 25}, {"s10", 26}, {"s11", 27}, {"t3", 28}, {"t4", 29},
{"t5", 30}, {"t6", 31}};
#endif
#include <gdbstub.h>
struct Breakpoint {
size_t addr;
bp_type_t type;
struct DbgState {
std::vector<Breakpoint> *bp;
};
#endif
#endif

View file

@ -2,6 +2,7 @@
#define _NPC_TRACER_H_
#include "components.hpp"
#include "types.h"
#include "verilated.h"
#include <filesystem>
#include <gdbstub.h>
#include <sys/types.h>
@ -47,7 +48,7 @@ public:
registers = r;
}
void eval() {
void eval(void) {
if (this->is_posedge()) {
posedge_cnt++;
}
@ -57,35 +58,34 @@ public:
if (tracer)
tracer->update();
}
void eval(int n) {
for (int i = 0; i < n; i++) {
this->eval();
const Breakpoint *stepi(const std::vector<Breakpoint> &breakpoints) {
this->eval();
this->eval();
size_t pc = registers->get_pc();
for (const auto &bp : breakpoints) {
if (pc == bp.addr) {
return &bp;
}
}
return nullptr;
}
gdb_action_t eval(const std::vector<Breakpoint> &breakpoints) {
gdb_action_t res;
const Breakpoint *cont(const std::vector<Breakpoint> &breakpoints) {
const Breakpoint *res = nullptr;
do {
this->eval();
size_t pc = registers->get_pc();
for (const auto &bp : breakpoints) {
if (pc == bp.addr) {
res.data = bp.addr;
switch (bp.type) {
default:
res.reason = gdb_action_t::ACT_BREAKPOINT;
}
return res;
}
}
} while (true);
res = stepi(breakpoints);
} while (res == nullptr);
return res;
}
void reset_eval(int n) {
extern bool g_skip_memcheck;
g_skip_memcheck = true;
this->reset = 1;
this->eval(n);
do {
this->eval();
} while (--n);
this->reset = 0;
g_skip_memcheck = false;
}

View file

@ -1,12 +1,16 @@
#ifndef _NPC_VPI_WRAPPER_H_
#define _NPC_VPI_WRAPPER_H_
#include <components.hpp>
#include <spdlog/logger.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <vpi_user.h>
template <typename T, std::size_t nr>
class _RegistersVPI : public _RegistersBase<T, nr> {
std::array<vpiHandle, nr> reg_handles;
vpiHandle pc_handle;
T vpi_get(vpiHandle vh) const {
s_vpi_value v;
v.format = vpiIntVal;