Skip to content

Commit

Permalink
Support GoldenMem store log
Browse files Browse the repository at this point in the history
Now we has 3 Memory, DUT REF Golden. When Squash replay, DUT will
transmit unsquashed DiffState which are already generated and stored,
and we will reset REF and Golden Memory to process these states.

Overall, DUT memory doesn't need to restore. REF and Golden Memory will
be restored by store_log.
  • Loading branch information
klin02 committed Feb 28, 2024
1 parent a7ad420 commit c750746
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 13 deletions.
3 changes: 1 addition & 2 deletions config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ extern unsigned long EMU_FLASH_SIZE;
// whether to check l2tlb response
// #define DEBUG_L2TLB

// whether to enable REF record origin data of memory and restore
// whether to enable REF/GoldenMemory record origin data of memory and restore
#ifdef CONFIG_DIFFTEST_SQUASH_REPLAY
#define ENABLE_STORE_LOG
#endif // CONFIG_DIFFTEST_SQUASH_REPLAY
Expand Down Expand Up @@ -157,7 +157,6 @@ extern unsigned long EMU_FLASH_SIZE;
#define DEBUG_MEM_BASE 0x38020000
#endif


// -----------------------------------------------------------------------
// Do not touch
// -----------------------------------------------------------------------
Expand Down
15 changes: 10 additions & 5 deletions src/test/csrc/difftest/difftest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,17 @@ Difftest::~Difftest() {
}
#ifdef CONFIG_DIFFTEST_SQUASH_REPLAY
free(state_ss);
if (proxy_ss) {
free(proxy_ss);
if (proxy_reg_ss) {
free(proxy_reg_ss);
}
#endif // CONFIG_DIFFTEST_SQUASH_REPLAY
}

void Difftest::update_nemuproxy(int coreid, size_t ram_size = 0) {
proxy = new REF_PROXY(coreid, ram_size);
#ifdef CONFIG_DIFFTEST_SQUASH_REPLAY
proxy_ss = (REF_PROXY*)malloc(sizeof(REF_PROXY));
proxy_reg_size = proxy->get_reg_size();
proxy_reg_ss = (uint8_t*)malloc(proxy_reg_size);
#endif // CONFIG_DIFFTEST_SQUASH_REPLAY
}

Expand All @@ -185,20 +186,23 @@ bool Difftest::squash_check() {

void Difftest::squash_snapshot() {
memcpy(state_ss, state, sizeof(DiffState));
memcpy(proxy_ss, proxy, sizeof(REF_PROXY));
memcpy(proxy_reg_ss, &proxy->regs_int, proxy_reg_size);
proxy->ref_csrcpy(squash_csr_buf, REF_TO_DUT);
proxy->ref_store_log_reset();
proxy->set_store_log(true);
goldenmem_store_log_reset();
goldenmem_set_store_log(true);
}

void Difftest::squash_replay() {
inReplay = true;
replay_idx = squash_idx;
memcpy(state, state_ss, sizeof(DiffState));
memcpy(proxy, proxy_ss, sizeof(REF_PROXY));
memcpy(&proxy->regs_int, proxy_reg_ss, proxy_reg_size);
proxy->ref_regcpy(&proxy->regs_int, DUT_TO_REF, false);
proxy->ref_csrcpy(squash_csr_buf, DUT_TO_REF);
proxy->ref_store_log_restore();
goldenmem_store_log_restore();
difftest_squash_replay(replay_idx);
}
#endif // CONFIG_DIFFTEST_SQUASH_REPLAY
Expand All @@ -217,6 +221,7 @@ int Difftest::step() {
squash_snapshot();
} else {
proxy->set_store_log(false);
goldenmem_set_store_log(false);
}
#endif // CONFIG_DIFFTEST_SQUASH_REPLAY

Expand Down
3 changes: 2 additions & 1 deletion src/test/csrc/difftest/difftest.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ class Difftest {
int replay_idx;

DiffState *state_ss = NULL;
REF_PROXY *proxy_ss = NULL;
int proxy_reg_size = 0;
uint8_t *proxy_reg_ss = NULL;
uint64_t squash_csr_buf[4096];
bool squash_check();
void squash_snapshot();
Expand Down
48 changes: 45 additions & 3 deletions src/test/csrc/difftest/goldenmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,44 @@ static inline void pmem_write(uint64_t addr, word_t data, int len) {
}
}

// Golden Memory Store Log
#ifdef ENABLE_STORE_LOG
#define GOLDENMEM_STORE_LOG_SIZE 1024
struct store_log {
uint64_t addr;
word_t org_data;
} goldenmem_store_log_buf[GOLDENMEM_STORE_LOG_SIZE];

bool goldenmem_store_log_enable = false;
int goldenmem_store_log_ptr = 0;

void goldenmem_set_store_log(bool enable) {
goldenmem_store_log_enable = enable;
}

void goldenmem_store_log_reset() {
goldenmem_store_log_ptr = 0;
}

void pmem_record_store(uint64_t addr) {
if (goldenmem_store_log_enable) {
// align to 8 byte
addr = (addr >> 3) << 3;
word_t rdata = pmem_read(addr, 8);
goldenmem_store_log_buf[goldenmem_store_log_ptr].addr = addr;
goldenmem_store_log_buf[goldenmem_store_log_ptr].org_data = rdata;
++goldenmem_store_log_ptr;
if (goldenmem_store_log_ptr >= GOLDENMEM_STORE_LOG_SIZE) assert(0);
}
}

void goldenmem_store_log_restore() {
for (int i = goldenmem_store_log_ptr - 1; i >= 0; i--) {
pmem_write(goldenmem_store_log_buf[i].addr, goldenmem_store_log_buf[i].org_data, 8);
}
}
#endif // ENABLE_STORE_LOG

/* Memory accessing interfaces */

inline word_t paddr_read(uint64_t addr, int len) {
Expand All @@ -109,6 +147,10 @@ inline word_t paddr_read(uint64_t addr, int len) {
}

inline void paddr_write(uint64_t addr, word_t data, int len) {
if (in_pmem(addr)) pmem_write(addr, data, len);
else panic("write not in pmem!");
}
if (in_pmem(addr)) {
#ifdef ENABLE_STORE_LOG
if (goldenmem_store_log_enable) pmem_record_store(addr);
#endif // ENABLE_STORE_LOG
pmem_write(addr, data, len);
} else panic("write not in pmem!");
}
5 changes: 5 additions & 0 deletions src/test/csrc/difftest/goldenmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ void paddr_write(uint64_t addr, word_t data, int len);
bool is_sfence_safe(uint64_t addr, int len);
bool in_pmem(uint64_t addr);

#ifdef ENABLE_STORE_LOG
void goldenmem_set_store_log(bool enable);
void goldenmem_store_log_reset();
void goldenmem_store_log_restore();
#endif
#endif
4 changes: 2 additions & 2 deletions src/test/csrc/difftest/refproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ AbstractRefProxy::AbstractRefProxy(int coreid, size_t ram_size, const char *env,
ref_set_ramsize(ram_size);
}

#ifdef CONFIG_DIFFTEST_SQUASH_REPLAY
#ifdef ENABLE_STORE_LOG
check_and_assert(ref_store_log_reset);
check_and_assert(ref_store_log_restore);
#endif // CONFIG_DIFFTEST_SQUASH_REPLAY
#endif // ENABLE_STORE_LOG

ref_init();
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/csrc/difftest/refproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ class RefProxy : public AbstractRefProxy {
}
#endif // ENABLE_STORE_LOG

inline int get_reg_size() {
return sizeof(DifftestArchIntRegState) + sizeof(DifftestCSRState) + sizeof(uint64_t)
#ifdef CONFIG_DIFFTEST_ARCHFPREGSTATE
+ sizeof(DifftestArchFpRegState)
#endif // CONFIG_DIFFTEST_ARCHFPREGSTATE
#ifdef CONFIG_DIFFTEST_ARCHVECREGSTATE
+ sizeof(DifftestArchVecRegState)
#endif // CONFIG_DIFFTEST_ARCHVECREGSTATE
#ifdef CONFIG_DIFFTEST_VECCSRSTATE
+ sizeof(DifftestVecCSRState)
#endif // CONFIG_DIFFTEST_VECCSRSTATE
;
}

inline int get_status() {
return ref_status ? ref_status() : 0;
}
Expand Down

0 comments on commit c750746

Please sign in to comment.