Skip to content

Commit

Permalink
riscv64,system,priv: read clint for rdtime
Browse files Browse the repository at this point in the history
* rdtime may be located in the middle of a basic block. Raising an
  illegal instruction exception will also flush the tcache, since we can
  not maintain the continuity of tcache entry. This will reduce
  performance of such rdtime instruciton is executed frequently.
* To fix this, we let rdtime return the right value instead of raising
  an exception.
  • Loading branch information
sashimi-yzh committed Mar 29, 2021
1 parent 0d03ea0 commit cbda50d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/isa/riscv64/clint.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ void update_clint() {
mip->mtip = (clint_base[CLINT_MTIME] >= clint_base[CLINT_MTIMECMP]);
}

uint64_t clint_uptime() {
update_clint();
return clint_base[CLINT_MTIME];
}

static void clint_io_handler(uint32_t offset, int len, bool is_write) {
update_clint();
}
Expand Down
6 changes: 5 additions & 1 deletion src/isa/riscv64/local-include/csr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
f(sscratch , 0x140) f(sepc , 0x141) f(scause , 0x142) \
f(stval , 0x143) f(sip , 0x144) \
f(satp , 0x180) \
f(fflags , 0x001) f(frm , 0x002) f(fcsr , 0x003)
f(fflags , 0x001) f(frm , 0x002) f(fcsr , 0x003) \
f(mtime , 0xc01)

#define CSR_STRUCT_START(name) \
typedef union { \
Expand Down Expand Up @@ -220,6 +221,9 @@ CSR_STRUCT_START(fcsr)
};
CSR_STRUCT_END(fcsr)

CSR_STRUCT_START(mtime)
CSR_STRUCT_END(mtime)

#define CSRS_DECL(name, addr) extern concat(name, _t)* const name;
MAP(CSRS, CSRS_DECL)

Expand Down
4 changes: 2 additions & 2 deletions src/isa/riscv64/system/priv.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "../local-include/csr.h"
#include "../local-include/rtl.h"
#include "../local-include/intr.h"
#include <cpu/cpu.h>

int update_mmu_state();
uint64_t clint_uptime();

static word_t csr_array[4096] = {};

Expand Down Expand Up @@ -39,6 +39,7 @@ static inline word_t csr_read(word_t *src) {
else if (is_read(fcsr)) { return fcsr->val & FCSR_MASK; }
else if (is_read(fflags)) { return fcsr->fflags.val; }
else if (is_read(frm)) { return fcsr->frm; }
else if (is_read(mtime)) { return clint_uptime(); }
return *src;
}

Expand Down Expand Up @@ -76,7 +77,6 @@ word_t csrid_read(uint32_t csrid) {
}

static void csrrw(rtlreg_t *dest, const rtlreg_t *src, uint32_t csrid) {
if (csrid == 0xc01) { longjmp_exception(EX_II); } // time
word_t *csr = csr_decode(csrid);
word_t tmp = (src != NULL ? *src : 0);
if (dest != NULL) { *dest = csr_read(csr); }
Expand Down

0 comments on commit cbda50d

Please sign in to comment.