Skip to content

Commit

Permalink
Implement csr_time according to RV32 (sysprog21#114)
Browse files Browse the repository at this point in the history
Resolve CWE-468.
  • Loading branch information
EagleTw authored and 2011eric committed Jul 22, 2023
1 parent d35dcf2 commit 0c81b9f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ static inline void update_time(riscv_t *rv)
struct timeval tv;
rv_gettimeofday(&tv);

rv->csr_time = (uint64_t) tv.tv_sec * 1e6 + (uint32_t) tv.tv_usec;
uint64_t t = (uint64_t) tv.tv_sec * 1e6 + (uint32_t) tv.tv_usec;
rv->csr_time[0] = t & 0xFFFFFFFF;
rv->csr_time[1] = t >> 32;
}

#if RV32_HAS(Zicsr)
Expand Down Expand Up @@ -132,11 +134,11 @@ static uint32_t *csr_get_ptr(riscv_t *rv, uint32_t csr)
/* TIME/TIMEH - very roughly about 1 ms per tick */
case CSR_TIME: { /* Timer for RDTIME instruction */
update_time(rv);
return (uint32_t *) (&rv->csr_time) + 0;
return &rv->csr_time[0];
}
case CSR_TIMEH: { /* Upper 32 bits of time */
update_time(rv);
return (uint32_t *) (&rv->csr_time + 4);
return &rv->csr_time[1];
}
case CSR_INSTRET: /* Number of Instructions Retired Counter */
/* Number of Instructions Retired Counter, just use cycle */
Expand Down
2 changes: 1 addition & 1 deletion src/riscv_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct riscv_internal {

/* csr registers */
uint64_t csr_cycle;
uint64_t csr_time;
uint32_t csr_time[2];
uint32_t csr_mstatus;
uint32_t csr_mtvec;
uint32_t csr_misa;
Expand Down

0 comments on commit 0c81b9f

Please sign in to comment.