From 6367b06b8ec50af11c1651f3bb34442913005308 Mon Sep 17 00:00:00 2001 From: YenHaoChen Date: Thu, 23 Nov 2023 17:15:40 +0800 Subject: [PATCH] stimecmp: perform menvcfg.STCE permission check when accessing vstimecmp in HS-mode The spec requires menvcfg.STCE=1 on accessing stimecmp or vstimecmp in a mode other than M-mode. The previous implementation does not check the permission on accessing vstimecmp in HS-mode. This commit fixes the issue by moveing the permission check from virtualized_stimecmp_csr_t to stimecmp_csr_t, which implements the vstimecmp. --- riscv/csrs.cc | 11 +++++++++-- riscv/csrs.h | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/riscv/csrs.cc b/riscv/csrs.cc index 95b5e22348..8ef02ad131 100644 --- a/riscv/csrs.cc +++ b/riscv/csrs.cc @@ -1497,7 +1497,7 @@ virtualized_stimecmp_csr_t::virtualized_stimecmp_csr_t(processor_t* const proc, virtualized_csr_t(proc, orig, virt) { } -void virtualized_stimecmp_csr_t::verify_permissions(insn_t insn, bool write) const { +void stimecmp_csr_t::verify_permissions(insn_t insn, bool write) const { if (!(state->menvcfg->read() & MENVCFG_STCE)) { // access to (v)stimecmp with MENVCFG.STCE = 0 if (state->prv < PRV_M) @@ -1511,7 +1511,14 @@ void virtualized_stimecmp_csr_t::verify_permissions(insn_t insn, bool write) con throw trap_virtual_instruction(insn.bits()); } - virtualized_csr_t::verify_permissions(insn, write); + basic_csr_t::verify_permissions(insn, write); +} + +void virtualized_stimecmp_csr_t::verify_permissions(insn_t insn, bool write) const { + if (state->v) + virt_csr->verify_permissions(insn, write); + else + orig_csr->verify_permissions(insn, write); } scountovf_csr_t::scountovf_csr_t(processor_t* const proc, const reg_t addr): diff --git a/riscv/csrs.h b/riscv/csrs.h index 19aefca139..db28e29cbf 100644 --- a/riscv/csrs.h +++ b/riscv/csrs.h @@ -760,6 +760,7 @@ class senvcfg_csr_t final: public masked_csr_t { class stimecmp_csr_t: public basic_csr_t { public: stimecmp_csr_t(processor_t* const proc, const reg_t addr, const reg_t imask); + virtual void verify_permissions(insn_t insn, bool write) const override; protected: virtual bool unlogged_write(const reg_t val) noexcept override; private: