Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide accessibility control of xcontext CSRs through stateen0[57] CSR #1550

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Disallow accessing mcontext if mstateen0[57] is 0
  • Loading branch information
YenHaoChen committed Dec 25, 2023
commit a93755f7db87c30f89153a9645ae50b55629f273
16 changes: 16 additions & 0 deletions riscv/csrs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,22 @@ void counter_proxy_csr_t::verify_permissions(insn_t insn, bool write) const {
}
}

context_proxy_csr_t::context_proxy_csr_t(processor_t* const proc, const reg_t addr, csr_t_p delegate):
proxy_csr_t(proc, addr, delegate) {
}

void context_proxy_csr_t::verify_permissions(insn_t insn, bool write) const {
if (proc->extension_enabled(EXT_SMSTATEEN)) {
if ((state->prv < PRV_M) && !(state->mstateen[0]->read() & MSTATEEN0_HCONTEXT))
throw trap_illegal_instruction(insn.bits());

if (state->v && !(state->hstateen[0]->read() & HSTATEEN0_SCONTEXT))
throw trap_virtual_instruction(insn.bits());
}

proxy_csr_t::verify_permissions(insn, write);
}

mevent_csr_t::mevent_csr_t(processor_t* const proc, const reg_t addr):
basic_csr_t(proc, addr, 0) {
}
Expand Down
6 changes: 6 additions & 0 deletions riscv/csrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,12 @@ class counter_proxy_csr_t: public proxy_csr_t {
bool myenable(csr_t_p counteren) const noexcept;
};

class context_proxy_csr_t: public proxy_csr_t {
public:
context_proxy_csr_t(processor_t* const proc, const reg_t addr, csr_t_p delegate);
virtual void verify_permissions(insn_t insn, bool write) const override;
};

class mevent_csr_t: public basic_csr_t {
public:
mevent_csr_t(processor_t* const proc, const reg_t addr);
Expand Down
2 changes: 1 addition & 1 deletion riscv/processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void state_t::reset(processor_t* const proc, reg_t max_isa)
csrmap[CSR_SCONTEXT] = scontext = std::make_shared<context_csr_t>(proc, CSR_SCONTEXT, (reg_t(1) << scontext_length) - 1, 0);
unsigned hcontext_length = (xlen == 32 ? 6 : 13) + (proc->extension_enabled('H') ? 1 : 0); // debug spec suggest 7-bit (6-bit) for RV32 and 14-bit (13-bit) for RV64 with (without) H extension
csrmap[CSR_HCONTEXT] = std::make_shared<context_csr_t>(proc, CSR_HCONTEXT, (reg_t(1) << hcontext_length) - 1, 0);
csrmap[CSR_MCONTEXT] = mcontext = std::make_shared<proxy_csr_t>(proc, CSR_MCONTEXT, csrmap[CSR_HCONTEXT]);
csrmap[CSR_MCONTEXT] = mcontext = std::make_shared<context_proxy_csr_t>(proc, CSR_MCONTEXT, csrmap[CSR_HCONTEXT]);
debug_mode = false;
single_step = STEP_NONE;

Expand Down