Compare commits
3 commits
7c982b238b
...
8ffab061ed
Author | SHA1 | Date | |
---|---|---|---|
8ffab061ed | |||
5a97fab28f | |||
2ab098d181 |
14 changed files with 212 additions and 168 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -2,3 +2,6 @@
|
||||||
path = am-kernels
|
path = am-kernels
|
||||||
url = https://git.xinyang.life/xin/am-kernels.git
|
url = https://git.xinyang.life/xin/am-kernels.git
|
||||||
branch = dev
|
branch = dev
|
||||||
|
[submodule "diffu"]
|
||||||
|
path = diffu
|
||||||
|
url = https://git.xinyang.life/xin/diffu.git
|
||||||
|
|
1
diffu
Submodule
1
diffu
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 0c590d218be4b2df4f8f827a9fd40278c227a1ca
|
|
@ -14,9 +14,10 @@
|
||||||
***************************************************************************************/
|
***************************************************************************************/
|
||||||
|
|
||||||
#include "local-include/reg.h"
|
#include "local-include/reg.h"
|
||||||
#include "gdbstub.h"
|
|
||||||
#include "macro.h"
|
#include "macro.h"
|
||||||
|
#include <difftest-def.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <gdbstub.h>
|
||||||
#include <isa.h>
|
#include <isa.h>
|
||||||
|
|
||||||
const char *regs[] = {"$0", "ra", "sp", "gp", "tp", "t0", "t1", "t2",
|
const char *regs[] = {"$0", "ra", "sp", "gp", "tp", "t0", "t1", "t2",
|
||||||
|
@ -76,6 +77,6 @@ int isa_write_reg(void *args, int regno, size_t data) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
arch_info_t isa_arch_info = {.reg_num = 33,
|
__EXPORT arch_info_t isa_arch_info = {.reg_num = 32,
|
||||||
.reg_byte = MUXDEF(CONFIG_RV64, 8, 4),
|
.reg_byte = MUXDEF(CONFIG_RV64, 8, 4),
|
||||||
.target_desc = TARGET_RV32};
|
.target_desc = TARGET_RV32};
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
DIRS-y += src/monitor
|
DIRS-y += src/monitor
|
||||||
|
|
||||||
CXXSRC += src/monitor/gdbstub.cc
|
CXXSRC += src/monitor/gdbstub.cc
|
||||||
|
LIBS += -lgdbstub
|
||||||
|
|
|
@ -4,27 +4,27 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include <cpu/cpu.h>
|
#include <cpu/cpu.h>
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
#include <difftest-def.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <gdbstub.h>
|
#include <gdbstub.h>
|
||||||
#include <isa.h>
|
#include <isa.h>
|
||||||
#include <memory/paddr.h>
|
#include <memory/paddr.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
std::vector<breakpoint_t> *bp;
|
std::vector<breakpoint_t> *bp;
|
||||||
bool halt;
|
bool halt;
|
||||||
} DbgState;
|
} DbgState;
|
||||||
|
|
||||||
int nemu_read_mem(void *args, size_t addr, size_t len, void *val) {
|
__EXPORT int nemu_read_mem(void *args, size_t addr, size_t len, void *val) {
|
||||||
if (!in_pmem(addr))
|
if (!in_pmem(addr))
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
memcpy(val, guest_to_host(addr), len);
|
memcpy(val, guest_to_host(addr), len);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int nemu_write_mem(void *args, size_t addr, size_t len, void *val) {
|
__EXPORT int nemu_write_mem(void *args, size_t addr, size_t len, void *val) {
|
||||||
if (!in_pmem(addr))
|
if (!in_pmem(addr))
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
memcpy(guest_to_host(addr), val, len);
|
memcpy(guest_to_host(addr), val, len);
|
||||||
|
@ -35,21 +35,25 @@ static void nemu_is_stopped(gdb_action_t *act, breakpoint_t *stopped_at) {
|
||||||
switch (nemu_state.state) {
|
switch (nemu_state.state) {
|
||||||
case NEMU_RUNNING:
|
case NEMU_RUNNING:
|
||||||
nemu_state.state = NEMU_STOP;
|
nemu_state.state = NEMU_STOP;
|
||||||
switch (stopped_at->type) {
|
if (stopped_at == NULL) {
|
||||||
case BP_SOFTWARE:
|
act->reason = gdb_action_t::ACT_NONE;
|
||||||
act->reason = gdb_action_t::ACT_BREAKPOINT;
|
} else {
|
||||||
break;
|
switch (stopped_at->type) {
|
||||||
case BP_ACCESS:
|
case BP_SOFTWARE:
|
||||||
act->reason = gdb_action_t::ACT_WATCH;
|
act->reason = gdb_action_t::ACT_BREAKPOINT;
|
||||||
break;
|
break;
|
||||||
case BP_WRITE:
|
case BP_ACCESS:
|
||||||
act->reason = gdb_action_t::ACT_WWATCH;
|
act->reason = gdb_action_t::ACT_WATCH;
|
||||||
break;
|
break;
|
||||||
case BP_READ:
|
case BP_WRITE:
|
||||||
act->reason = gdb_action_t::ACT_RWATCH;
|
act->reason = gdb_action_t::ACT_WWATCH;
|
||||||
break;
|
break;
|
||||||
|
case BP_READ:
|
||||||
|
act->reason = gdb_action_t::ACT_RWATCH;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
act->data = stopped_at->addr;
|
||||||
}
|
}
|
||||||
act->data = stopped_at->addr;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -58,21 +62,21 @@ static void nemu_is_stopped(gdb_action_t *act, breakpoint_t *stopped_at) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void nemu_cont(void *args, gdb_action_t *res) {
|
__EXPORT void nemu_cont(void *args, gdb_action_t *res) {
|
||||||
DbgState *dbg_state = (DbgState *)args;
|
DbgState *dbg_state = (DbgState *)args;
|
||||||
breakpoint_t *stopped_at =
|
breakpoint_t *stopped_at =
|
||||||
cpu_exec_with_bp(-1, dbg_state->bp->data(), dbg_state->bp->size());
|
cpu_exec_with_bp(-1, dbg_state->bp->data(), dbg_state->bp->size());
|
||||||
nemu_is_stopped(res, stopped_at);
|
nemu_is_stopped(res, stopped_at);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nemu_stepi(void *args, gdb_action_t *res) {
|
__EXPORT void nemu_stepi(void *args, gdb_action_t *res) {
|
||||||
DbgState *dbg_state = (DbgState *)args;
|
DbgState *dbg_state = (DbgState *)args;
|
||||||
breakpoint_t *stopped_at =
|
breakpoint_t *stopped_at =
|
||||||
cpu_exec_with_bp(1, dbg_state->bp->data(), dbg_state->bp->size());
|
cpu_exec_with_bp(1, dbg_state->bp->data(), dbg_state->bp->size());
|
||||||
nemu_is_stopped(res, stopped_at);
|
nemu_is_stopped(res, stopped_at);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool nemu_set_bp(void *args, size_t addr, bp_type_t type) {
|
__EXPORT bool nemu_set_bp(void *args, size_t addr, bp_type_t type) {
|
||||||
DbgState *dbg_state = (DbgState *)args;
|
DbgState *dbg_state = (DbgState *)args;
|
||||||
for (const auto &bp : *dbg_state->bp) {
|
for (const auto &bp : *dbg_state->bp) {
|
||||||
if (bp.addr == addr && bp.type == type) {
|
if (bp.addr == addr && bp.type == type) {
|
||||||
|
@ -83,7 +87,7 @@ bool nemu_set_bp(void *args, size_t addr, bp_type_t type) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool nemu_del_bp(void *args, size_t addr, bp_type_t type) {
|
__EXPORT bool nemu_del_bp(void *args, size_t addr, bp_type_t type) {
|
||||||
DbgState *dbg_state = (DbgState *)args;
|
DbgState *dbg_state = (DbgState *)args;
|
||||||
for (auto it = dbg_state->bp->begin(); it != dbg_state->bp->end(); it++) {
|
for (auto it = dbg_state->bp->begin(); it != dbg_state->bp->end(); it++) {
|
||||||
if (it->addr == addr && it->type == type) {
|
if (it->addr == addr && it->type == type) {
|
||||||
|
@ -95,24 +99,44 @@ bool nemu_del_bp(void *args, size_t addr, bp_type_t type) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nemu_on_interrupt(void *args) {
|
__EXPORT void nemu_on_interrupt(void *args) {
|
||||||
// fputs("Not implemented", stderr);
|
// fputs("Not implemented", stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__EXPORT int nemu_read_reg(void *args, int regno, size_t *data) {
|
||||||
|
return isa_read_reg(args, regno, data);
|
||||||
|
}
|
||||||
|
__EXPORT int nemu_write_reg(void *args, int regno, size_t data) {
|
||||||
|
return isa_write_reg(args, regno, data);
|
||||||
|
}
|
||||||
|
__EXPORT size_t argsize = sizeof(DbgState);
|
||||||
|
|
||||||
static struct target_ops nemu_gdbstub_ops = {.cont = nemu_cont,
|
static struct target_ops nemu_gdbstub_ops = {.cont = nemu_cont,
|
||||||
.stepi = nemu_stepi,
|
.stepi = nemu_stepi,
|
||||||
.read_reg = isa_read_reg,
|
.read_reg = nemu_read_reg,
|
||||||
.write_reg = isa_write_reg,
|
.write_reg = nemu_write_reg,
|
||||||
.read_mem = nemu_read_mem,
|
.read_mem = nemu_read_mem,
|
||||||
.write_mem = nemu_write_mem,
|
.write_mem = nemu_write_mem,
|
||||||
.set_bp = nemu_set_bp,
|
.set_bp = nemu_set_bp,
|
||||||
.del_bp = nemu_del_bp,
|
.del_bp = nemu_del_bp,
|
||||||
.on_interrupt = NULL};
|
.on_interrupt = NULL};
|
||||||
static DbgState dbg;
|
static DbgState dbg;
|
||||||
extern "C" {
|
|
||||||
static gdbstub_t gdbstub_priv;
|
static gdbstub_t gdbstub_priv;
|
||||||
#define SOCKET_ADDR "127.0.0.1:1234"
|
#define SOCKET_ADDR "127.0.0.1:1234"
|
||||||
int nemu_gdbstub_init() {
|
|
||||||
|
__EXPORT void nemu_init(void *args) {
|
||||||
|
DbgState *dbg_state = (DbgState *)args;
|
||||||
|
dbg_state->bp = new std::vector<breakpoint_t>();
|
||||||
|
dbg_state->halt = 0;
|
||||||
|
Assert(dbg_state->bp != NULL, "Failed to allocate breakpoint");
|
||||||
|
|
||||||
|
void init_mem();
|
||||||
|
init_mem();
|
||||||
|
/* Perform ISA dependent initialization. */
|
||||||
|
init_isa();
|
||||||
|
}
|
||||||
|
|
||||||
|
__EXPORT int nemu_gdbstub_init() {
|
||||||
dbg.bp = new std::vector<breakpoint_t>();
|
dbg.bp = new std::vector<breakpoint_t>();
|
||||||
assert(dbg.bp);
|
assert(dbg.bp);
|
||||||
if (!gdbstub_init(&gdbstub_priv, &nemu_gdbstub_ops,
|
if (!gdbstub_init(&gdbstub_priv, &nemu_gdbstub_ops,
|
||||||
|
@ -121,7 +145,8 @@ int nemu_gdbstub_init() {
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int nemu_gdbstub_run() {
|
|
||||||
|
__EXPORT int nemu_gdbstub_run() {
|
||||||
puts("Waiting for gdb connection at " SOCKET_ADDR);
|
puts("Waiting for gdb connection at " SOCKET_ADDR);
|
||||||
bool success = gdbstub_run(&gdbstub_priv, &dbg);
|
bool success = gdbstub_run(&gdbstub_priv, &dbg);
|
||||||
gdbstub_close(&gdbstub_priv);
|
gdbstub_close(&gdbstub_priv);
|
||||||
|
|
|
@ -25,7 +25,7 @@ void init_mem();
|
||||||
void init_difftest(char *ref_so_file, long img_size, int port);
|
void init_difftest(char *ref_so_file, long img_size, int port);
|
||||||
void init_device();
|
void init_device();
|
||||||
void init_disasm(const char *triple);
|
void init_disasm(const char *triple);
|
||||||
int nemu_gdbstub_init();
|
void nemu_init();
|
||||||
|
|
||||||
static void welcome() {
|
static void welcome() {
|
||||||
Log("Trace: %s", MUXDEF(CONFIG_TRACE, ANSI_FMT("ON", ANSI_FG_GREEN),
|
Log("Trace: %s", MUXDEF(CONFIG_TRACE, ANSI_FMT("ON", ANSI_FG_GREEN),
|
||||||
|
@ -60,7 +60,8 @@ static long load_img() {
|
||||||
// Image file is searched from paths in environment variable NEMU_IMAGES_PATH if it's a relative path
|
// Image file is searched from paths in environment variable NEMU_IMAGES_PATH if it's a relative path
|
||||||
if (img_file[0] != '/') {
|
if (img_file[0] != '/') {
|
||||||
char *search_paths = getenv("NEMU_IMAGES_PATH");
|
char *search_paths = getenv("NEMU_IMAGES_PATH");
|
||||||
if(search_paths == NULL) search_paths = "./";
|
if (search_paths == NULL)
|
||||||
|
search_paths = "./";
|
||||||
search_paths = strdup(search_paths);
|
search_paths = strdup(search_paths);
|
||||||
Trace("NEMU_IMAGES_PATH=%s", search_paths);
|
Trace("NEMU_IMAGES_PATH=%s", search_paths);
|
||||||
|
|
||||||
|
@ -68,8 +69,10 @@ static long load_img() {
|
||||||
char *p_start = search_paths;
|
char *p_start = search_paths;
|
||||||
do {
|
do {
|
||||||
char *p = strchr(p_start, ':');
|
char *p = strchr(p_start, ':');
|
||||||
if (p != NULL) *p = '\0';
|
if (p != NULL)
|
||||||
else p = paths_end;
|
*p = '\0';
|
||||||
|
else
|
||||||
|
p = paths_end;
|
||||||
|
|
||||||
char *file_path = malloc(p - p_start + img_filename_len + 2);
|
char *file_path = malloc(p - p_start + img_filename_len + 2);
|
||||||
strcpy(file_path, p_start);
|
strcpy(file_path, p_start);
|
||||||
|
@ -86,7 +89,7 @@ static long load_img() {
|
||||||
|
|
||||||
Assert(fp != NULL || errno == ENOENT, "Cannot open '%s'", img_file);
|
Assert(fp != NULL || errno == ENOENT, "Cannot open '%s'", img_file);
|
||||||
p_start = p + 1;
|
p_start = p + 1;
|
||||||
} while(p_start < paths_end);
|
} while (p_start < paths_end);
|
||||||
free(search_paths);
|
free(search_paths);
|
||||||
|
|
||||||
Assert(fp, "Cannot find '%s'", img_file);
|
Assert(fp, "Cannot find '%s'", img_file);
|
||||||
|
@ -178,10 +181,11 @@ void init_monitor(int argc, char *argv[]) {
|
||||||
init_difftest(diff_so_file, img_size, difftest_port);
|
init_difftest(diff_so_file, img_size, difftest_port);
|
||||||
|
|
||||||
/* Initialize debugger */
|
/* Initialize debugger */
|
||||||
if (nemu_gdbstub_init()) {
|
// if (nemu_init()) {
|
||||||
Error("Failed to init");
|
// Error("Failed to init");
|
||||||
exit(1);
|
// exit(1);
|
||||||
}
|
// }
|
||||||
|
nemu_init();
|
||||||
|
|
||||||
// printf("elf_file: %s\n", elf_file);
|
// printf("elf_file: %s\n", elf_file);
|
||||||
if (elf_file != NULL) {
|
if (elf_file != NULL) {
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
use flake ..#npc
|
use flake ".?submodules=1#npc"
|
||||||
|
|
|
@ -42,7 +42,7 @@ if(BUILD_SIM_NVBOARD_TARGET)
|
||||||
endif()
|
endif()
|
||||||
find_package(CLI11 CONFIG REQUIRED)
|
find_package(CLI11 CONFIG REQUIRED)
|
||||||
|
|
||||||
option(ENABLE_SDB "Enable simple debugger" ON)
|
option(ENABLE_SDB "Enable simple debugger" OFF)
|
||||||
|
|
||||||
find_library(NVBOARD_LIBRARY NAMES nvboard)
|
find_library(NVBOARD_LIBRARY NAMES nvboard)
|
||||||
find_path(NVBOARD_INCLUDE_DIR NAMES nvboard.h)
|
find_path(NVBOARD_INCLUDE_DIR NAMES nvboard.h)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
include(ChiselBuild)
|
include(ChiselBuild)
|
||||||
add_executable(V${TOPMODULE} config.cpp main.cpp)
|
add_executable(V${TOPMODULE} config.cpp main.cpp)
|
||||||
target_link_libraries(V${TOPMODULE} PRIVATE sdb devices)
|
target_link_libraries(V${TOPMODULE} PRIVATE devices gdbstub)
|
||||||
target_include_directories(V${TOPMODULE} PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
target_include_directories(V${TOPMODULE} PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||||
|
|
||||||
verilate(
|
verilate(
|
||||||
|
|
|
@ -1,30 +1,25 @@
|
||||||
|
extern "C" {
|
||||||
|
#include <gdbstub.h>
|
||||||
|
}
|
||||||
#include "VFlow___024root.h"
|
#include "VFlow___024root.h"
|
||||||
#include "components.hpp"
|
#include "components.hpp"
|
||||||
#include <VFlow.h>
|
#include <VFlow.h>
|
||||||
#include <array>
|
|
||||||
#include <config.hpp>
|
#include <config.hpp>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <devices.hpp>
|
#include <devices.hpp>
|
||||||
#include <disasm.hpp>
|
|
||||||
#include <filesystem>
|
|
||||||
#include <fstream>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sdb.hpp>
|
|
||||||
#include <trm_difftest.hpp>
|
|
||||||
#include <trm_interface.hpp>
|
|
||||||
#include <types.h>
|
#include <types.h>
|
||||||
|
#include <vector>
|
||||||
#include <vl_wrapper.hpp>
|
#include <vl_wrapper.hpp>
|
||||||
#include <vpi_user.h>
|
#include <vpi_user.h>
|
||||||
#include <vpi_wrapper.hpp>
|
#include <vpi_wrapper.hpp>
|
||||||
|
|
||||||
using VlModule = VlModuleInterfaceCommon<VFlow>;
|
|
||||||
using Registers = _RegistersVPI<uint32_t, 32>;
|
using Registers = _RegistersVPI<uint32_t, 32>;
|
||||||
|
using VlModule = VlModuleInterfaceCommon<VFlow, Registers>;
|
||||||
|
|
||||||
// SDB::SDB<NPC::npc_interface> sdb_dut;
|
// SDB::SDB<NPC::npc_interface> sdb_dut;
|
||||||
using CPUState = CPUStateBase<uint32_t, 32>;
|
|
||||||
bool g_skip_memcheck = false;
|
bool g_skip_memcheck = false;
|
||||||
CPUState npc_cpu;
|
|
||||||
VlModule *top;
|
VlModule *top;
|
||||||
Registers *regs;
|
Registers *regs;
|
||||||
vpiHandle pc = nullptr;
|
vpiHandle pc = nullptr;
|
||||||
|
@ -32,7 +27,13 @@ vpiHandle pc = nullptr;
|
||||||
const size_t PMEM_START = 0x80000000;
|
const size_t PMEM_START = 0x80000000;
|
||||||
const size_t PMEM_END = 0x87ffffff;
|
const size_t PMEM_END = 0x87ffffff;
|
||||||
|
|
||||||
|
struct DbgState {
|
||||||
|
std::vector<Breakpoint> bp;
|
||||||
|
};
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
|
/* === Memory Access === */
|
||||||
using MMap = MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>;
|
using MMap = MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>;
|
||||||
void *pmem_get() {
|
void *pmem_get() {
|
||||||
static Devices::DeviceMap devices{
|
static Devices::DeviceMap devices{
|
||||||
|
@ -65,127 +66,99 @@ void pmem_write(int waddr, int wdata, char wmask) {
|
||||||
mem->trace((std::size_t)waddr, false, regs->get_pc(), wdata);
|
mem->trace((std::size_t)waddr, false, regs->get_pc(), wdata);
|
||||||
return mem->write((std::size_t)waddr, wdata, wmask);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace NPC {
|
int npc_write_mem(void *args, size_t addr, size_t len, void *val) {
|
||||||
void npc_memcpy(paddr_t addr, void *buf, size_t sz, bool direction) {
|
void *pmem = pmem_get();
|
||||||
if (direction == TRM_FROM_MACHINE) {
|
auto mmap = static_cast<MMap *>(pmem);
|
||||||
static_cast<MMap *>(pmem_get())->copy_to(addr, (uint8_t *)buf, sz);
|
mmap->copy_from(addr, (uint8_t *)val, len);
|
||||||
}
|
return 0;
|
||||||
};
|
}
|
||||||
|
|
||||||
void npc_regcpy(void *p, bool direction) {
|
int npc_read_reg(void *args, int regno, size_t *value) {
|
||||||
|
if (regno == 32)
|
||||||
|
*value = regs->get_pc();
|
||||||
|
else
|
||||||
|
*value = (*regs)[regno];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (direction == TRM_FROM_MACHINE) {
|
int npc_write_reg(void *args, int regno, size_t value) { return 1; }
|
||||||
((CPUState *)p)->pc = regs->get_pc();
|
|
||||||
for (int i = 0; i < 32; i++) {
|
void npc_cont(void *args, gdb_action_t *res) {
|
||||||
((CPUState *)p)->reg[i] = (*regs)[i];
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void npc_exec(uint64_t n) {
|
bool npc_del_bp(void *args, size_t addr, bp_type_t type) {
|
||||||
while (n--) {
|
DbgState *dbg = (DbgState *)args;
|
||||||
for (int i = 0; i < 2; i++) {
|
for (auto it = dbg->bp.begin(); it != dbg->bp.end(); it++) {
|
||||||
if (top->is_posedge()) {
|
if (it->addr == addr && it->type == type) {
|
||||||
// Posedge
|
std::swap(*it, *dbg->bp.rbegin());
|
||||||
regs->update();
|
dbg->bp.pop_back();
|
||||||
}
|
return true;
|
||||||
top->eval();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void npc_atexit(void) {
|
static target_ops npc_gdbstub_ops = {.cont = npc_cont,
|
||||||
delete top;
|
.stepi = npc_stepi,
|
||||||
delete regs;
|
.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};
|
||||||
|
|
||||||
void npc_init(int port) {
|
static gdbstub_t gdbstub_priv;
|
||||||
top = new VlModule{config.wavefile};
|
static DbgState dbg;
|
||||||
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
|
arch_info_t isa_arch_info = {
|
||||||
atexit(npc_atexit);
|
.target_desc = strdup(TARGET_RV32), .reg_num = 33, .reg_byte = 4};
|
||||||
top->reset_eval(10);
|
|
||||||
}
|
|
||||||
|
|
||||||
class DutTrmInterface : public TrmInterface {
|
int gdbstub_loop() {
|
||||||
public:
|
if (!gdbstub_init(&gdbstub_priv, &npc_gdbstub_ops, (arch_info_t)isa_arch_info,
|
||||||
DutTrmInterface(memcpy_t f_memcpy, regcpy_t f_regcpy, exec_t f_exec,
|
strdup("127.0.0.1:1234"))) {
|
||||||
init_t f_init, void *cpu_state)
|
return EINVAL;
|
||||||
: TrmInterface{f_memcpy, f_regcpy, f_exec, f_init, cpu_state} {}
|
|
||||||
word_t at(std::string name) const override {
|
|
||||||
return ((CPUState *)cpu_state)->at(name);
|
|
||||||
}
|
}
|
||||||
|
bool success = gdbstub_run(&gdbstub_priv, &dbg);
|
||||||
word_t at(paddr_t addr) const override {
|
gdbstub_close(&gdbstub_priv);
|
||||||
word_t buf;
|
return !success;
|
||||||
this->memcpy(addr, &buf, sizeof(word_t), TRM_FROM_MACHINE);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
void print(std::ostream &os) const override {
|
|
||||||
this->regcpy(cpu_state, TRM_FROM_MACHINE);
|
|
||||||
os << *(CPUState *)cpu_state << std::endl;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
DutTrmInterface npc_interface =
|
|
||||||
DutTrmInterface{&npc_memcpy, &npc_regcpy, &npc_exec, &npc_init, &npc_cpu};
|
|
||||||
}; // namespace NPC
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
word_t reg_str2val(const char *name, bool *success) {
|
|
||||||
return npc_cpu.reg_str2val(name, success);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} // extern "C"
|
||||||
|
|
||||||
int main(int argc, char **argv, char **env) {
|
int main(int argc, char **argv, char **env) {
|
||||||
config.cli_parse(argc, argv);
|
config.cli_parse(argc, argv);
|
||||||
|
|
||||||
if (config.lib_ref.empty()) {
|
top = new VlModule;
|
||||||
if (config.interactive) {
|
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
|
||||||
SDB::SDB sdb_npc{NPC::npc_interface};
|
top->setup(config.wavefile, regs);
|
||||||
sdb_npc.main_loop();
|
top->reset_eval(10);
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
NPC::npc_interface.init(0);
|
|
||||||
while (true) {
|
|
||||||
word_t inst = NPC::npc_interface.at(regs->get_pc());
|
|
||||||
if (inst == 1048691) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
NPC::npc_interface.exec(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* -- Difftest -- */
|
return gdbstub_loop();
|
||||||
std::filesystem::path ref{config.lib_ref};
|
|
||||||
RefTrmInterface ref_interface{ref};
|
|
||||||
DifftestTrmInterface diff_interface{
|
|
||||||
NPC::npc_interface, ref_interface,
|
|
||||||
static_cast<MMap *>(pmem_get())->get_pmem(), 128 * 1024};
|
|
||||||
if (config.interactive) {
|
|
||||||
SDB::SDB sdb_diff{diff_interface};
|
|
||||||
sdb_diff.main_loop();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
diff_interface.init(0);
|
|
||||||
diff_interface.exec(-1);
|
|
||||||
} catch (TrmRuntimeException &e) {
|
|
||||||
switch (e.error_code()) {
|
|
||||||
case TrmRuntimeException::EBREAK:
|
|
||||||
return 0;
|
|
||||||
case TrmRuntimeException::DIFFTEST_FAILED:
|
|
||||||
std::cout << "Difftest Failed" << std::endl;
|
|
||||||
diff_interface.print(std::cout);
|
|
||||||
return 1;
|
|
||||||
default:
|
|
||||||
std::cerr << "Unknown error happened" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,12 +20,12 @@
|
||||||
template <typename T, std::size_t nr> class _RegistersBase {
|
template <typename T, std::size_t nr> class _RegistersBase {
|
||||||
std::array<T, nr> regs;
|
std::array<T, nr> regs;
|
||||||
T pc;
|
T pc;
|
||||||
virtual T fetch_pc();
|
virtual T fetch_pc() const;
|
||||||
virtual T fetch_reg(std::size_t id);
|
virtual T fetch_reg(std::size_t id) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
T operator[](size_t id) { return fetch_reg(id); }
|
T operator[](size_t id) const { return fetch_reg(id); }
|
||||||
T get_pc() { return fetch_pc(); }
|
T get_pc() const { return fetch_pc(); }
|
||||||
void update() {
|
void update() {
|
||||||
for (int i = 0; i < regs.size(); i++) {
|
for (int i = 0; i < regs.size(); i++) {
|
||||||
regs[i] = fetch_reg(i);
|
regs[i] = fetch_reg(i);
|
||||||
|
@ -112,14 +112,14 @@ public:
|
||||||
if (ram->in_pmem(addr)) {
|
if (ram->in_pmem(addr)) {
|
||||||
ram->transfer(addr, buf, len, false);
|
ram->transfer(addr, buf, len, false);
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "Not in pmem" << std::endl;
|
std::cerr << "0x" << std::hex << addr << " not in pmem" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void copy_from(paddr_t addr, const uint8_t *buf, size_t len) {
|
void copy_from(paddr_t addr, uint8_t *buf, size_t len) {
|
||||||
if (ram->in_pmem(addr)) {
|
if (ram->in_pmem(addr)) {
|
||||||
ram->transfer(addr, buf, len, true);
|
ram->transfer(addr, buf, len, true);
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "Not in pmem" << std::endl;
|
std::cerr << "0x" << std::hex << addr << " not in pmem" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void *get_pmem() { return ram->mem.data(); }
|
void *get_pmem() { return ram->mem.data(); }
|
||||||
|
|
|
@ -29,4 +29,10 @@ const std::map<std::string, int> riscv32_regs_by_name{
|
||||||
{"t5", 30}, {"t6", 31}};
|
{"t5", 30}, {"t6", 31}};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <gdbstub.h>
|
||||||
|
struct Breakpoint {
|
||||||
|
size_t addr;
|
||||||
|
bp_type_t type;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -1,7 +1,10 @@
|
||||||
#ifndef _NPC_TRACER_H_
|
#ifndef _NPC_TRACER_H_
|
||||||
#define _NPC_TRACER_H_
|
#define _NPC_TRACER_H_
|
||||||
#include "components.hpp"
|
#include "components.hpp"
|
||||||
|
#include "types.h"
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <gdbstub.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <verilated_vcd_c.h>
|
#include <verilated_vcd_c.h>
|
||||||
|
|
||||||
template <class T> class Tracer {
|
template <class T> class Tracer {
|
||||||
|
@ -26,16 +29,24 @@ public:
|
||||||
void update() { m_trace->dump(cycle++); }
|
void update() { m_trace->dump(cycle++); }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T> class VlModuleInterfaceCommon : public T {
|
template <typename T, typename R> class VlModuleInterfaceCommon : public T {
|
||||||
uint64_t sim_time = 0;
|
uint64_t sim_time = 0;
|
||||||
uint64_t posedge_cnt = 0;
|
uint64_t posedge_cnt = 0;
|
||||||
std::unique_ptr<Tracer<T>> tracer;
|
std::unique_ptr<Tracer<T>> tracer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VlModuleInterfaceCommon<T>(std::filesystem::path wavefile) {
|
const R *registers;
|
||||||
|
VlModuleInterfaceCommon<T, R>() {
|
||||||
|
tracer = nullptr;
|
||||||
|
registers = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup(std::filesystem::path wavefile, const R *r) {
|
||||||
if (!wavefile.empty())
|
if (!wavefile.empty())
|
||||||
tracer = std::make_unique<Tracer<T>>(this, wavefile);
|
tracer = std::make_unique<Tracer<T>>(this, wavefile);
|
||||||
|
registers = r;
|
||||||
}
|
}
|
||||||
|
|
||||||
void eval() {
|
void eval() {
|
||||||
if (this->is_posedge()) {
|
if (this->is_posedge()) {
|
||||||
posedge_cnt++;
|
posedge_cnt++;
|
||||||
|
@ -51,6 +62,25 @@ public:
|
||||||
this->eval();
|
this->eval();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gdb_action_t eval(const std::vector<Breakpoint> &breakpoints) {
|
||||||
|
gdb_action_t res;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
void reset_eval(int n) {
|
void reset_eval(int n) {
|
||||||
extern bool g_skip_memcheck;
|
extern bool g_skip_memcheck;
|
||||||
g_skip_memcheck = true;
|
g_skip_memcheck = true;
|
||||||
|
|
|
@ -7,14 +7,14 @@ template <typename T, std::size_t nr>
|
||||||
class _RegistersVPI : public _RegistersBase<T, nr> {
|
class _RegistersVPI : public _RegistersBase<T, nr> {
|
||||||
std::array<vpiHandle, nr> reg_handles;
|
std::array<vpiHandle, nr> reg_handles;
|
||||||
vpiHandle pc_handle;
|
vpiHandle pc_handle;
|
||||||
T vpi_get(vpiHandle vh) {
|
T vpi_get(vpiHandle vh) const {
|
||||||
s_vpi_value v;
|
s_vpi_value v;
|
||||||
v.format = vpiIntVal;
|
v.format = vpiIntVal;
|
||||||
vpi_get_value(vh, &v);
|
vpi_get_value(vh, &v);
|
||||||
return v.value.integer;
|
return v.value.integer;
|
||||||
}
|
}
|
||||||
T fetch_pc(void) { return vpi_get(pc_handle); }
|
T fetch_pc(void) const { return vpi_get(pc_handle); }
|
||||||
T fetch_reg(std::size_t id) { return vpi_get(reg_handles[id]); }
|
T fetch_reg(std::size_t id) const { return vpi_get(reg_handles[id]); }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
_RegistersVPI<T, nr>(const std::string regs_prefix,
|
_RegistersVPI<T, nr>(const std::string regs_prefix,
|
||||||
|
|
Loading…
Reference in a new issue