Added counters for interrupts taken and interrupt returns. This also allows "listeners" to be connected to each, that allows tracking or additional actions to be executed when interrupt or interrupt returns occur. PiperOrigin-RevId: 663034517 Change-Id: Ifea90082303a4e83359ff3079fa27908b60aafa6
diff --git a/riscv/riscv_state.cc b/riscv/riscv_state.cc index 26671f2..fc3ecd1 100644 --- a/riscv/riscv_state.cc +++ b/riscv/riscv_state.cc
@@ -307,7 +307,11 @@ xlen_(xlen), memory_(memory), atomic_memory_(atomic_memory), - csr_set_(new RiscVCsrSet()) { + csr_set_(new RiscVCsrSet()), + counter_interrupts_taken_("interrupts_taken", 0), + counter_interrupt_returns_("interrupt_returns", 0) { + CHECK_OK(AddCounter(&counter_interrupt_returns_)); + CHECK_OK(AddCounter(&counter_interrupts_taken_)); DataBuffer *db = nullptr; switch (xlen_) { case RiscVXlen::RV32: { @@ -692,7 +696,7 @@ Trap(/*is_interrupt*/ true, 0, *available_interrupt_code_, epc, nullptr); // Clear pending interrupt. is_interrupt_available_ = false; - ++interrupt_handler_depth_; + counter_interrupts_taken_.Increment(1); available_interrupt_code_ = InterruptCode::kNone; }
diff --git a/riscv/riscv_state.h b/riscv/riscv_state.h index e3ce03a..b9b48e8 100644 --- a/riscv/riscv_state.h +++ b/riscv/riscv_state.h
@@ -26,6 +26,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "mpact/sim/generic/arch_state.h" +#include "mpact/sim/generic/counters.h" #include "mpact/sim/generic/data_buffer.h" #include "mpact/sim/generic/instruction.h" #include "mpact/sim/generic/operand_interface.h" @@ -300,11 +301,14 @@ // Indicates that the program has returned from handling an interrupt. This // decrements the interrupt handler depth and should be called by the // implementations of mret, sret, and uret. - void SignalReturnFromInterrupt() { --interrupt_handler_depth_; } + void SignalReturnFromInterrupt() { counter_interrupt_returns_.Increment(1); } // Returns the depth of the interrupt handler currently being executed, or // zero if no interrupt handler is being executed. - int InterruptHandlerDepth() const { return interrupt_handler_depth_; } + int InterruptHandlerDepth() const { + return counter_interrupts_taken_.GetValue() - + counter_interrupt_returns_.GetValue(); + } // Accessors. void set_memory(util::MemoryInterface *memory) { memory_ = memory; } @@ -413,7 +417,6 @@ std::vector<RiscVCsrInterface *> csr_vec_; // For interrupt handling. bool is_interrupt_available_ = false; - int interrupt_handler_depth_ = 0; InterruptCode available_interrupt_code_ = InterruptCode::kNone; // By default, execute in machine mode. PrivilegeMode privilege_mode_ = PrivilegeMode::kMachine; @@ -434,6 +437,8 @@ RiscVCsrInterface *sepc_ = nullptr; RiscVCsrInterface *scause_ = nullptr; RiscVCsrInterface *sideleg_ = nullptr; + generic::SimpleCounter<int64_t> counter_interrupts_taken_; + generic::SimpleCounter<int64_t> counter_interrupt_returns_; }; } // namespace riscv