Made RiscVPmP more generic. Can be used from other RiscV similar architectures. PiperOrigin-RevId: 660441394 Change-Id: I47dbd633753247cefc2b4de2e99e768fc10a9389
diff --git a/riscv/BUILD b/riscv/BUILD index 7aca829..0c52d15 100644 --- a/riscv/BUILD +++ b/riscv/BUILD
@@ -50,7 +50,6 @@ "riscv_csr.cc", "riscv_minstret.cc", "riscv_misa.cc", - "riscv_pmp.cc", "riscv_register.cc", "riscv_sim_csrs.cc", "riscv_state.cc",
diff --git a/riscv/riscv_pmp.cc b/riscv/riscv_pmp.cc deleted file mode 100644 index 82ee93e..0000000 --- a/riscv/riscv_pmp.cc +++ /dev/null
@@ -1,115 +0,0 @@ -// Copyright 2024 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "riscv/riscv_pmp.h" - -#include <cstdint> - -#include "absl/log/log.h" -#include "absl/strings/str_cat.h" -#include "riscv/riscv_csr.h" -#include "riscv/riscv_state.h" - -namespace mpact::sim::riscv { - -// Helper function used in constructors to create the PMP CSRs. -template <typename T> -void CreatePmpCsrs(RiscVState *state, RiscVPmp *pmp) { - // Create the PMP configuration registers. Each configuration register - // contains XLEN/8 configuration entries for a total of 16 configuration - // entries. In the case of XLEN=64, there are only two configuration registers - // pmp_cfg_[0] and pmp_cfg_[2]. - pmp->pmp_cfg_[0] = - new RiscVSimpleCsr<T>("pmpcfg0", RiscVCsrEnum::kPmpCfg0, state); - pmp->pmp_cfg_[1] = - sizeof(T) == 8 - ? nullptr - : new RiscVSimpleCsr<T>("pmpcfg1", RiscVCsrEnum::kPmpCfg1, state); - pmp->pmp_cfg_[2] = - new RiscVSimpleCsr<T>("pmpcfg2", RiscVCsrEnum::kPmpCfg2, state); - pmp->pmp_cfg_[3] = - sizeof(T) == 8 - ? nullptr - : new RiscVSimpleCsr<T>("pmpcfg3", RiscVCsrEnum::kPmpCfg3, state); - for (auto *pmp_cfg : pmp->pmp_cfg_) { - if (pmp_cfg == nullptr) continue; - auto status = state->csr_set()->AddCsr(pmp_cfg); - if (!status.ok()) { - LOG(ERROR) << absl::StrCat("Failed to add PMP configuration register: '", - pmp_cfg->name(), "': ", status.message()); - } - } - // Create the 16 PMP address registers. - pmp->pmp_addr_[0] = - new RiscVSimpleCsr<T>("pmpaddr0", RiscVCsrEnum::kPmpAddr0, state); - pmp->pmp_addr_[1] = - new RiscVSimpleCsr<T>("pmpaddr1", RiscVCsrEnum::kPmpAddr1, state); - pmp->pmp_addr_[2] = - new RiscVSimpleCsr<T>("pmpaddr2", RiscVCsrEnum::kPmpAddr2, state); - pmp->pmp_addr_[3] = - new RiscVSimpleCsr<T>("pmpaddr3", RiscVCsrEnum::kPmpAddr3, state); - pmp->pmp_addr_[4] = - new RiscVSimpleCsr<T>("pmpaddr4", RiscVCsrEnum::kPmpAddr4, state); - pmp->pmp_addr_[5] = - new RiscVSimpleCsr<T>("pmpaddr5", RiscVCsrEnum::kPmpAddr5, state); - pmp->pmp_addr_[6] = - new RiscVSimpleCsr<T>("pmpaddr6", RiscVCsrEnum::kPmpAddr6, state); - pmp->pmp_addr_[7] = - new RiscVSimpleCsr<T>("pmpaddr7", RiscVCsrEnum::kPmpAddr7, state); - pmp->pmp_addr_[8] = - new RiscVSimpleCsr<T>("pmpaddr8", RiscVCsrEnum::kPmpAddr8, state); - pmp->pmp_addr_[9] = - new RiscVSimpleCsr<T>("pmpaddr9", RiscVCsrEnum::kPmpAddr9, state); - pmp->pmp_addr_[10] = - new RiscVSimpleCsr<T>("pmpaddr10", RiscVCsrEnum::kPmpAddr10, state); - pmp->pmp_addr_[11] = - new RiscVSimpleCsr<T>("pmpaddr11", RiscVCsrEnum::kPmpAddr11, state); - pmp->pmp_addr_[12] = - new RiscVSimpleCsr<T>("pmpaddr12", RiscVCsrEnum::kPmpAddr12, state); - pmp->pmp_addr_[13] = - new RiscVSimpleCsr<T>("pmpaddr13", RiscVCsrEnum::kPmpAddr13, state); - pmp->pmp_addr_[14] = - new RiscVSimpleCsr<T>("pmpaddr14", RiscVCsrEnum::kPmpAddr14, state); - pmp->pmp_addr_[15] = - new RiscVSimpleCsr<T>("pmpaddr15", RiscVCsrEnum::kPmpAddr15, state); - for (auto *pmp_addr : pmp->pmp_addr_) { - auto status = state->csr_set()->AddCsr(pmp_addr); - if (!status.ok()) { - LOG(ERROR) << absl::StrCat("Failed to add PMP address register: '", - pmp_addr->name(), "': ", status.message()); - } - } -} - -RiscVPmp::RiscVPmp(RiscVState *state) : state_(state) { - if (state_->xlen() == RiscVXlen::RV32) { - CreatePmpCsrs<uint32_t>(state, this); - } else if (state_->xlen() == RiscVXlen::RV64) { - CreatePmpCsrs<uint64_t>(state, this); - } else { - LOG(FATAL) << "Illegal value for xlen"; - } -} - -RiscVPmp::~RiscVPmp() { - // Destruct the CSRs. - for (auto *pmp_cfg : pmp_cfg_) { - delete pmp_cfg; - } - for (auto *pmp_addr : pmp_addr_) { - delete pmp_addr; - } -} - -} // namespace mpact::sim::riscv
diff --git a/riscv/riscv_pmp.h b/riscv/riscv_pmp.h index dc46ae6..fd09220 100644 --- a/riscv/riscv_pmp.h +++ b/riscv/riscv_pmp.h
@@ -15,8 +15,11 @@ #ifndef THIRD_PARTY_MPACT_RISCV_RISCV_PMP_H_ #define THIRD_PARTY_MPACT_RISCV_RISCV_PMP_H_ +#include "absl/log/log.h" +#include "absl/strings/str_cat.h" +#include "mpact/sim/generic/arch_state.h" +#include "mpact/sim/generic/type_helpers.h" #include "riscv/riscv_csr.h" -#include "riscv/riscv_state.h" // This file defines the RiscV Physical Memory Protection management class. For // now, this class only creates the CSRs and does not model the actual PMP @@ -30,7 +33,8 @@ namespace mpact::sim::riscv { -class RiscVPmp; +using ::mpact::sim::generic::operator*; // NOLINT: used below (clang error). +using ::mpact::sim::generic::ArchState; enum class PmpCfgBits { kRead = 1 << 0, @@ -41,25 +45,74 @@ kLock = 1 << 8, }; -template <typename T> -void CreatePmpCsrs(RiscVState* state, RiscVPmp* pmp); - class RiscVPmp { - friend void CreatePmpCsrs<uint32_t>(RiscVState* state, RiscVPmp* pmp); - friend void CreatePmpCsrs<uint64_t>(RiscVState* state, RiscVPmp* pmp); - public: - RiscVPmp(RiscVState* state); + explicit RiscVPmp(ArchState* state) : state_(state) {} RiscVPmp(const RiscVPmp&) = delete; RiscVPmp& operator=(const RiscVPmp&) = delete; - virtual ~RiscVPmp(); + + template <typename T, typename E> + void CreatePmpCsrs(RiscVCsrSet* csr_set); + + virtual ~RiscVPmp() { + for (auto* pmp_cfg : pmp_cfg_) delete pmp_cfg; + for (auto* pmp_addr : pmp_addr_) delete pmp_addr; + } private: - RiscVState* state_; + ArchState* state_; RiscVCsrInterface* pmp_addr_[16]; RiscVCsrInterface* pmp_cfg_[4]; }; +template <typename T, typename E> +void RiscVPmp::CreatePmpCsrs(RiscVCsrSet* csr_set) { + // Create the PMP configuration registers. Each configuration register + // contains XLEN/8 configuration entries for a total of 16 configuration + // entries. In the case of XLEN=64, there are only two configuration registers + // pmp_cfg_[0] and pmp_cfg_[2]. + pmp_cfg_[0] = new RiscVSimpleCsr<T>("pmpcfg0", *E::kPmpCfg0, state_); + pmp_cfg_[1] = sizeof(T) == 8 + ? nullptr + : new RiscVSimpleCsr<T>("pmpcfg1", *E::kPmpCfg1, state_); + pmp_cfg_[2] = new RiscVSimpleCsr<T>("pmpcfg2", *E::kPmpCfg2, state_); + pmp_cfg_[3] = sizeof(T) == 8 + ? nullptr + : new RiscVSimpleCsr<T>("pmpcfg3", *E::kPmpCfg3, state_); + for (auto* pmp_cfg : pmp_cfg_) { + if (pmp_cfg == nullptr) continue; + auto status = csr_set->AddCsr(pmp_cfg); + if (!status.ok()) { + LOG(ERROR) << absl::StrCat("Failed to add PMP configuration register: '", + pmp_cfg->name(), "': ", status.message()); + } + } + // Create the 16 PMP address registers. + pmp_addr_[0] = new RiscVSimpleCsr<T>("pmpaddr0", *E::kPmpAddr0, state_); + pmp_addr_[1] = new RiscVSimpleCsr<T>("pmpaddr1", *E::kPmpAddr1, state_); + pmp_addr_[2] = new RiscVSimpleCsr<T>("pmpaddr2", *E::kPmpAddr2, state_); + pmp_addr_[3] = new RiscVSimpleCsr<T>("pmpaddr3", *E::kPmpAddr3, state_); + pmp_addr_[4] = new RiscVSimpleCsr<T>("pmpaddr4", *E::kPmpAddr4, state_); + pmp_addr_[5] = new RiscVSimpleCsr<T>("pmpaddr5", *E::kPmpAddr5, state_); + pmp_addr_[6] = new RiscVSimpleCsr<T>("pmpaddr6", *E::kPmpAddr6, state_); + pmp_addr_[7] = new RiscVSimpleCsr<T>("pmpaddr7", *E::kPmpAddr7, state_); + pmp_addr_[8] = new RiscVSimpleCsr<T>("pmpaddr8", *E::kPmpAddr8, state_); + pmp_addr_[9] = new RiscVSimpleCsr<T>("pmpaddr9", *E::kPmpAddr9, state_); + pmp_addr_[10] = new RiscVSimpleCsr<T>("pmpaddr10", *E::kPmpAddr10, state_); + pmp_addr_[11] = new RiscVSimpleCsr<T>("pmpaddr11", *E::kPmpAddr11, state_); + pmp_addr_[12] = new RiscVSimpleCsr<T>("pmpaddr12", *E::kPmpAddr12, state_); + pmp_addr_[13] = new RiscVSimpleCsr<T>("pmpaddr13", *E::kPmpAddr13, state_); + pmp_addr_[14] = new RiscVSimpleCsr<T>("pmpaddr14", *E::kPmpAddr14, state_); + pmp_addr_[15] = new RiscVSimpleCsr<T>("pmpaddr15", *E::kPmpAddr15, state_); + for (auto* pmp_addr : pmp_addr_) { + auto status = csr_set->AddCsr(pmp_addr); + if (!status.ok()) { + LOG(ERROR) << absl::StrCat("Failed to add PMP address register: '", + pmp_addr->name(), "': ", status.message()); + } + } +} + } // namespace mpact::sim::riscv #endif // THIRD_PARTY_MPACT_RISCV_RISCV_PMP_H_
diff --git a/riscv/riscv_state.cc b/riscv/riscv_state.cc index 0229e4e..26671f2 100644 --- a/riscv/riscv_state.cc +++ b/riscv/riscv_state.cc
@@ -16,7 +16,6 @@ #include <algorithm> #include <cstdint> -#include <iostream> #include <limits> #include <string> #include <vector> @@ -31,6 +30,7 @@ #include "riscv/riscv_csr.h" #include "riscv/riscv_minstret.h" #include "riscv/riscv_misa.h" +#include "riscv/riscv_pmp.h" #include "riscv/riscv_register.h" #include "riscv/riscv_sim_csrs.h" #include "riscv/riscv_xip_xie.h" @@ -283,6 +283,10 @@ CsrInfo<T>::kUstatusRMask, CsrInfo<T>::kUstatusWMask, state), nullptr); + // PMP CSRs + state->pmp_ = new RiscVPmp(state); + state->pmp_->CreatePmpCsrs<T, RiscVCsrEnum>(state->csr_set()); + // Simulator CSRs // Access current privilege mode. @@ -361,6 +365,7 @@ delete pc_src_operand_; delete pc_dst_operand_; delete csr_set_; + delete pmp_; for (auto *csr : csr_vec_) { delete csr; }
diff --git a/riscv/riscv_state.h b/riscv/riscv_state.h index 92ba310..e3ce03a 100644 --- a/riscv/riscv_state.h +++ b/riscv/riscv_state.h
@@ -183,6 +183,7 @@ void CreateCsrs(RiscVState *, std::vector<RiscVCsrInterface *> &); class RiscVFPState; +class RiscVPmp; // Class that extends ArchState with RiscV specific methods. These methods // implement RiscV specific memory operations, memory/IO fencing, system @@ -421,6 +422,7 @@ RiscVMIsa *misa_ = nullptr; RiscVMIp *mip_ = nullptr; RiscVMIe *mie_ = nullptr; + RiscVPmp *pmp_ = nullptr; RiscVCsrInterface *mtvec_ = nullptr; RiscVCsrInterface *mepc_ = nullptr; RiscVCsrInterface *mcause_ = nullptr;
diff --git a/riscv/test/riscv_pmp_test.cc b/riscv/test/riscv_pmp_test.cc index d607a81..959420f 100644 --- a/riscv/test/riscv_pmp_test.cc +++ b/riscv/test/riscv_pmp_test.cc
@@ -1,5 +1,7 @@ #include "riscv/riscv_pmp.h" +#include <cstdint> + #include "absl/strings/str_cat.h" #include "googlemock/include/gmock/gmock.h" #include "mpact/sim/generic/type_helpers.h" @@ -18,6 +20,7 @@ TEST(RiscVPmpTest, CreatePmpCsrs32) { RiscVState state("test", RiscVXlen::RV32, nullptr, nullptr); RiscVPmp pmp(&state); + pmp.CreatePmpCsrs<uint32_t, RiscVCsrEnum>(state.csr_set()); for (int i = 0; i < 4; ++i) { EXPECT_TRUE( state.csr_set()->GetCsr(absl::StrCat("pmpcfg", i)).status().ok()); @@ -36,6 +39,7 @@ TEST(RiscVPmpTest, CreatePmpCsrs64) { RiscVState state("test", RiscVXlen::RV64, nullptr, nullptr); RiscVPmp pmp(&state); + pmp.CreatePmpCsrs<uint64_t, RiscVCsrEnum>(state.csr_set()); for (int i = 0; i < 4; ++i) { // For RV64, only the even numbered PMP configuration registers are created. if (i & 0x1) {