Browse Source

perf: refine csr accessibility checking

1. support zicntr and zihpm performance extensions
   zicntr defines the unprivileged cycle/time/instret
   zihpm defines the unprivileged hpmcounter3-31

2. the accessibility are controlled only by
   mcounteren/scounteren/hcounteren for access in different privilege
   modes

Signed-off-by: Chih-Min Chao <chihmin.chao@sifive.com>
pull/926/head
Chih-Min Chao 4 years ago
parent
commit
53a3002e8c
  1. 5
      riscv/csrs.cc
  2. 33
      riscv/processor.cc
  3. 2
      riscv/processor.h

5
riscv/csrs.cc

@ -926,8 +926,6 @@ bool counter_proxy_csr_t::myenable(csr_t_p counteren) const noexcept {
}
void counter_proxy_csr_t::verify_permissions(insn_t insn, bool write) const {
proxy_csr_t::verify_permissions(insn, write);
const bool mctr_ok = (state->prv < PRV_M) ? myenable(state->mcounteren) : true;
const bool hctr_ok = state->v ? myenable(state->hcounteren) : true;
const bool sctr_ok = (proc->extension_enabled('S') && state->prv < PRV_S) ? myenable(state->scounteren) : true;
@ -942,6 +940,9 @@ void counter_proxy_csr_t::verify_permissions(insn_t insn, bool write) const {
else
throw trap_illegal_instruction(insn.bits());
}
if (write)
throw trap_illegal_instruction(insn.bits());
}

33
riscv/processor.cc

@ -207,6 +207,10 @@ isa_parser_t::isa_parser_t(const char* str)
const char* all_subsets = "mafdqchpv";
max_isa = reg_t(2) << 62;
// enable zicntr and zihpm unconditionally for backward compatibility
extension_table[EXT_ZICNTR] = true;
extension_table[EXT_ZIHPM] = true;
if (isa_string.compare(0, 4, "rv32") == 0)
max_xlen = 32, max_isa = reg_t(1) << 30;
else if (isa_string.compare(0, 4, "rv64") == 0)
@ -341,8 +345,10 @@ isa_parser_t::isa_parser_t(const char* str)
} else if (ext_str == "zicbom") {
extension_table[EXT_ZICBOM] = true;
} else if (ext_str == "zicboz") {
extension_table[EXT_ZICBOZ] = true;
extension_table[EXT_ZICBOZ] = true;
} else if (ext_str == "zicbop") {
} else if (ext_str == "zicntr") {
} else if (ext_str == "zihpm") {
} else if (ext_str[0] == 'x') {
max_isa |= 1L << ('x' - 'a');
extension_table[toupper('x')] = true;
@ -424,14 +430,18 @@ void state_t::reset(processor_t* const proc, reg_t max_isa)
csrmap[CSR_MCAUSE] = mcause = std::make_shared<cause_csr_t>(proc, CSR_MCAUSE);
csrmap[CSR_MINSTRET] = minstret = std::make_shared<wide_counter_csr_t>(proc, CSR_MINSTRET);
csrmap[CSR_MCYCLE] = mcycle = std::make_shared<wide_counter_csr_t>(proc, CSR_MCYCLE);
csrmap[CSR_INSTRET] = std::make_shared<counter_proxy_csr_t>(proc, CSR_INSTRET, minstret);
csrmap[CSR_CYCLE] = std::make_shared<counter_proxy_csr_t>(proc, CSR_CYCLE, mcycle);
if (proc->extension_enabled_const(EXT_ZICNTR)) {
csrmap[CSR_INSTRET] = std::make_shared<counter_proxy_csr_t>(proc, CSR_INSTRET, minstret);
csrmap[CSR_CYCLE] = std::make_shared<counter_proxy_csr_t>(proc, CSR_CYCLE, mcycle);
}
if (xlen == 32) {
counter_top_csr_t_p minstreth, mcycleh;
csrmap[CSR_MINSTRETH] = minstreth = std::make_shared<counter_top_csr_t>(proc, CSR_MINSTRETH, minstret);
csrmap[CSR_MCYCLEH] = mcycleh = std::make_shared<counter_top_csr_t>(proc, CSR_MCYCLEH, mcycle);
csrmap[CSR_INSTRETH] = std::make_shared<counter_proxy_csr_t>(proc, CSR_INSTRETH, minstreth);
csrmap[CSR_CYCLEH] = std::make_shared<counter_proxy_csr_t>(proc, CSR_CYCLEH, mcycleh);
if (proc->extension_enabled_const(EXT_ZICNTR)) {
csrmap[CSR_INSTRETH] = std::make_shared<counter_proxy_csr_t>(proc, CSR_INSTRETH, minstreth);
csrmap[CSR_CYCLEH] = std::make_shared<counter_proxy_csr_t>(proc, CSR_CYCLEH, mcycleh);
}
}
for (reg_t i=3; i<=31; ++i) {
const reg_t which_mevent = CSR_MHPMEVENT3 + i - 3;
@ -441,15 +451,20 @@ void state_t::reset(processor_t* const proc, reg_t max_isa)
const reg_t which_counterh = CSR_HPMCOUNTER3H + i - 3;
auto mevent = std::make_shared<const_csr_t>(proc, which_mevent, 0);
auto mcounter = std::make_shared<const_csr_t>(proc, which_mcounter, 0);
auto counter = std::make_shared<counter_proxy_csr_t>(proc, which_counter, mcounter);
csrmap[which_mevent] = mevent;
csrmap[which_mcounter] = mcounter;
csrmap[which_counter] = counter;
if (proc->extension_enabled_const(EXT_ZICNTR) && proc->extension_enabled_const(EXT_ZIHPM)) {
auto counter = std::make_shared<counter_proxy_csr_t>(proc, which_counter, mcounter);
csrmap[which_counter] = counter;
}
if (xlen == 32) {
auto mcounterh = std::make_shared<const_csr_t>(proc, which_mcounterh, 0);
auto counterh = std::make_shared<counter_proxy_csr_t>(proc, which_counterh, mcounterh);
csrmap[which_mcounterh] = mcounterh;
csrmap[which_counterh] = counterh;
if (proc->extension_enabled_const(EXT_ZICNTR) && proc->extension_enabled_const(EXT_ZIHPM)) {
auto counterh = std::make_shared<counter_proxy_csr_t>(proc, which_counterh, mcounterh);
csrmap[which_counterh] = counterh;
}
}
}
csrmap[CSR_MCOUNTINHIBIT] = std::make_shared<const_csr_t>(proc, CSR_MCOUNTINHIBIT, 0);

2
riscv/processor.h

@ -280,6 +280,8 @@ typedef enum {
EXT_ZHINXMIN,
EXT_ZICBOM,
EXT_ZICBOZ,
EXT_ZICNTR,
EXT_ZIHPM,
EXT_XZBP,
EXT_XZBS,
EXT_XZBE,

Loading…
Cancel
Save