This commit adds PMP CSRs to the simulator.

Actual PMP functionality is not added at this point. Adding these CSRs
eliminates warnings that otherwise will be shown when there registers are
accessed.

A full implementation of PMP checking will be added later provided there is
enough interest.

PiperOrigin-RevId: 657616571
Change-Id: I28a23dfec0b9de48be13d1dd2cd8f018990c22a6
diff --git a/riscv/BUILD b/riscv/BUILD
index c623198..107a49e 100644
--- a/riscv/BUILD
+++ b/riscv/BUILD
@@ -50,6 +50,7 @@
         "riscv_csr.cc",
         "riscv_minstret.cc",
         "riscv_misa.cc",
+        "riscv_pmp.cc",
         "riscv_register.cc",
         "riscv_sim_csrs.cc",
         "riscv_state.cc",
@@ -61,6 +62,7 @@
         "riscv_csr.h",
         "riscv_minstret.h",
         "riscv_misa.h",
+        "riscv_pmp.h",
         "riscv_register.h",
         "riscv_register_aliases.h",
         "riscv_sim_csrs.h",
diff --git a/riscv/riscv_csr.h b/riscv/riscv_csr.h
index c479f29..01dec93 100644
--- a/riscv/riscv_csr.h
+++ b/riscv/riscv_csr.h
@@ -112,6 +112,28 @@
   kMTval = 0x343,     // Machine bad address or instruction.
   kMIp = 0x344,       // Machine interrupt pending.
 
+  // PMP registers.
+  kPmpCfg0 = 0x3a0,    // PMP configuration register 0.
+  kPmpCfg1 = 0x3a1,    // PMP configuration register 1 (only for RV32).
+  kPmpCfg2 = 0x3a2,    // PMP configuration register 2.
+  kPmpCfg3 = 0x3a3,    // PMP configuration register 3 (only for RV32).
+  kPmpAddr0 = 0x3b0,   // PMP address register 0.
+  kPmpAddr1 = 0x3b1,   // PMP address register 1.
+  kPmpAddr2 = 0x3b2,   // PMP address register 2.
+  kPmpAddr3 = 0x3b3,   // PMP address register 3.
+  kPmpAddr4 = 0x3b4,   // PMP address register 4.
+  kPmpAddr5 = 0x3b5,   // PMP address register 5.
+  kPmpAddr6 = 0x3b6,   // PMP address register 6.
+  kPmpAddr7 = 0x3b7,   // PMP address register 7.
+  kPmpAddr8 = 0x3b8,   // PMP address register 8.
+  kPmpAddr9 = 0x3b9,   // PMP address register 9.
+  kPmpAddr10 = 0x3ba,  // PMP address register 10.
+  kPmpAddr11 = 0x3bb,  // PMP address register 11.
+  kPmpAddr12 = 0x3bc,  // PMP address register 12.
+  kPmpAddr13 = 0x3bd,  // PMP address register 13.
+  kPmpAddr14 = 0x3be,  // PMP address register 14.
+  kPmpAddr15 = 0x3bf,  // PMP address register 15.
+
   // Ignoring machine memory protection for now.
 
   kMCycle = 0xb00,    // Machine cycle counter.
diff --git a/riscv/riscv_pmp.cc b/riscv/riscv_pmp.cc
new file mode 100644
index 0000000..82ee93e
--- /dev/null
+++ b/riscv/riscv_pmp.cc
@@ -0,0 +1,115 @@
+// 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
new file mode 100644
index 0000000..dc46ae6
--- /dev/null
+++ b/riscv/riscv_pmp.h
@@ -0,0 +1,65 @@
+// 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.
+
+#ifndef THIRD_PARTY_MPACT_RISCV_RISCV_PMP_H_
+#define THIRD_PARTY_MPACT_RISCV_RISCV_PMP_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
+// functionality. In the future, these classes may be extended to model the
+// actual PMP functionality.
+//
+// The CSRs are created internal to the class, and therefore are owned, and
+// subsequently destructed by the destructor. This is done so that in the future
+// it is easier to replace the CSRs with new PMP specific CSR classes that will
+// be able to (re)configure PMP checking upon CSR writes.
+
+namespace mpact::sim::riscv {
+
+class RiscVPmp;
+
+enum class PmpCfgBits {
+  kRead = 1 << 0,
+  kWrite = 1 << 1,
+  kExecute = 1 << 2,
+  kAtomic = 3 << 3,
+  kNone = 3 << 5,
+  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);
+  RiscVPmp(const RiscVPmp&) = delete;
+  RiscVPmp& operator=(const RiscVPmp&) = delete;
+  virtual ~RiscVPmp();
+
+ private:
+  RiscVState* state_;
+  RiscVCsrInterface* pmp_addr_[16];
+  RiscVCsrInterface* pmp_cfg_[4];
+};
+
+}  // namespace mpact::sim::riscv
+
+#endif  // THIRD_PARTY_MPACT_RISCV_RISCV_PMP_H_
diff --git a/riscv/riscv_state.h b/riscv/riscv_state.h
index 78ea3d6..92ba310 100644
--- a/riscv/riscv_state.h
+++ b/riscv/riscv_state.h
@@ -30,7 +30,6 @@
 #include "mpact/sim/generic/instruction.h"
 #include "mpact/sim/generic/operand_interface.h"
 #include "mpact/sim/generic/ref_count.h"
-#include "mpact/sim/util/memory/flat_demand_memory.h"
 #include "mpact/sim/util/memory/memory_interface.h"
 #include "riscv/riscv_csr.h"
 #include "riscv/riscv_misa.h"
diff --git a/riscv/test/BUILD b/riscv/test/BUILD
index 0cbd011..5f7d02c 100644
--- a/riscv/test/BUILD
+++ b/riscv/test/BUILD
@@ -683,3 +683,15 @@
         "@com_google_mpact-sim//mpact/sim/generic:instruction",
     ],
 )
+
+cc_test(
+    name = "riscv_pmp_test",
+    size = "small",
+    srcs = ["riscv_pmp_test.cc"],
+    deps = [
+        "//riscv:riscv_state",
+        "@com_google_absl//absl/strings",
+        "@com_google_googletest//:gtest_main",
+        "@com_google_mpact-sim//mpact/sim/generic:type_helpers",
+    ],
+)
diff --git a/riscv/test/riscv_pmp_test.cc b/riscv/test/riscv_pmp_test.cc
new file mode 100644
index 0000000..d607a81
--- /dev/null
+++ b/riscv/test/riscv_pmp_test.cc
@@ -0,0 +1,61 @@
+#include "riscv/riscv_pmp.h"
+
+#include "absl/strings/str_cat.h"
+#include "googlemock/include/gmock/gmock.h"
+#include "mpact/sim/generic/type_helpers.h"
+#include "riscv/riscv_csr.h"
+#include "riscv/riscv_state.h"
+
+namespace {
+
+using ::mpact::sim::riscv::RiscVCsrEnum;
+using ::mpact::sim::riscv::RiscVPmp;
+using ::mpact::sim::riscv::RiscVState;
+using ::mpact::sim::riscv::RiscVXlen;
+using ::mpact::sim::generic::operator*;  // NOLINT: used below (clang error).
+
+// Test that the expected PMP CSRs are created.
+TEST(RiscVPmpTest, CreatePmpCsrs32) {
+  RiscVState state("test", RiscVXlen::RV32, nullptr, nullptr);
+  RiscVPmp pmp(&state);
+  for (int i = 0; i < 4; ++i) {
+    EXPECT_TRUE(
+        state.csr_set()->GetCsr(absl::StrCat("pmpcfg", i)).status().ok());
+    EXPECT_TRUE(
+        state.csr_set()->GetCsr(*RiscVCsrEnum::kPmpCfg0 + i).status().ok());
+  }
+  for (int i = 0; i < 16; ++i) {
+    EXPECT_TRUE(
+        state.csr_set()->GetCsr(absl::StrCat("pmpaddr", i)).status().ok());
+    EXPECT_TRUE(
+        state.csr_set()->GetCsr(*RiscVCsrEnum::kPmpAddr0 + i).status().ok());
+  }
+}
+
+// Test that the expected PMP CSRs are created.
+TEST(RiscVPmpTest, CreatePmpCsrs64) {
+  RiscVState state("test", RiscVXlen::RV64, nullptr, nullptr);
+  RiscVPmp pmp(&state);
+  for (int i = 0; i < 4; ++i) {
+    // For RV64, only the even numbered PMP configuration registers are created.
+    if (i & 0x1) {
+      EXPECT_FALSE(
+          state.csr_set()->GetCsr(absl::StrCat("pmpcfg", i)).status().ok());
+      EXPECT_FALSE(
+          state.csr_set()->GetCsr(*RiscVCsrEnum::kPmpCfg0 + i).status().ok());
+    } else {
+      EXPECT_TRUE(
+          state.csr_set()->GetCsr(absl::StrCat("pmpcfg", i)).status().ok());
+      EXPECT_TRUE(
+          state.csr_set()->GetCsr(*RiscVCsrEnum::kPmpCfg0 + i).status().ok());
+    }
+  }
+  for (int i = 0; i < 16; ++i) {
+    EXPECT_TRUE(
+        state.csr_set()->GetCsr(absl::StrCat("pmpaddr", i)).status().ok());
+    EXPECT_TRUE(
+        state.csr_set()->GetCsr(*RiscVCsrEnum::kPmpAddr0 + i).status().ok());
+  }
+}
+
+}  // namespace