From 68efaf9967b51805a5c18534bc04ee647d6e49ac Mon Sep 17 00:00:00 2001 From: YenHaoChen Date: Thu, 11 Apr 2024 13:31:10 +0800 Subject: [PATCH] Smstateen: Implement *stateen0[59] controlling CSR stopi --- riscv/csrs.cc | 12 ++++++++++++ riscv/csrs.h | 1 + riscv/processor.cc | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/riscv/csrs.cc b/riscv/csrs.cc index 85ce48e601..f4a1c17ac1 100644 --- a/riscv/csrs.cc +++ b/riscv/csrs.cc @@ -1884,6 +1884,18 @@ nonvirtual_stopi_csr_t::nonvirtual_stopi_csr_t(processor_t* const proc, const re csr_t(proc, addr) { } +void nonvirtual_stopi_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_AIA)) + throw trap_illegal_instruction(insn.bits()); + + if (state->v && !(state->hstateen[0]->read() & HSTATEEN0_AIA)) + throw trap_virtual_instruction(insn.bits()); + } + + csr_t::verify_permissions(insn, write); +} + reg_t nonvirtual_stopi_csr_t::read() const noexcept { reg_t enabled_interrupts = state->nonvirtual_sip->read() & state->nonvirtual_sie->read() & state->mideleg->read() & ~state->hideleg->read(); if (!enabled_interrupts) diff --git a/riscv/csrs.h b/riscv/csrs.h index 295197a5e8..5a44f5762e 100644 --- a/riscv/csrs.h +++ b/riscv/csrs.h @@ -927,6 +927,7 @@ typedef std::shared_ptr mvip_csr_t_p; class nonvirtual_stopi_csr_t: public csr_t { public: nonvirtual_stopi_csr_t(processor_t* const proc, const reg_t addr); + virtual void verify_permissions(insn_t insn, bool write) const override; virtual reg_t read() const noexcept override; protected: bool unlogged_write(const reg_t val) noexcept override; diff --git a/riscv/processor.cc b/riscv/processor.cc index ee402af028..8c9d00ce16 100644 --- a/riscv/processor.cc +++ b/riscv/processor.cc @@ -686,7 +686,7 @@ void state_t::reset(processor_t* const proc, reg_t max_isa) vstopi = csrmap[CSR_VSTOPI] = std::make_shared(proc, CSR_VSTOPI); if (proc->extension_enabled_const(EXT_SSAIA)) { // Included by EXT_SMAIA csr_t_p nonvirtual_stopi = std::make_shared(proc, CSR_STOPI); - csrmap[CSR_STOPI] = std::make_shared(proc, nonvirtual_stopi, vstopi); + csrmap[CSR_STOPI] = std::make_shared(proc, nonvirtual_stopi, vstopi); csrmap[CSR_STOPEI] = std::make_shared(proc, CSR_STOPEI); auto hvien = std::make_shared(proc, CSR_HVIEN, 0, 0); auto hviprio1 = std::make_shared(proc, CSR_HVIPRIO1, 0, 0);