gem5  v21.2.1.1
interrupts.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Google
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef __ARCH_RISCV_INTERRUPT_HH__
30 #define __ARCH_RISCV_INTERRUPT_HH__
31 
32 #include <bitset>
33 #include <memory>
34 
36 #include "arch/riscv/faults.hh"
37 #include "arch/riscv/regs/misc.hh"
38 #include "base/logging.hh"
39 #include "cpu/thread_context.hh"
40 #include "debug/Interrupt.hh"
41 #include "params/RiscvInterrupts.hh"
42 #include "sim/sim_object.hh"
43 
44 namespace gem5
45 {
46 
47 class BaseCPU;
48 class ThreadContext;
49 
50 namespace RiscvISA {
51 
52 /*
53  * This is based on version 1.10 of the RISC-V privileged ISA reference,
54  * chapter 3.1.14.
55  */
56 class Interrupts : public BaseInterrupts
57 {
58  private:
59  std::bitset<NumInterruptTypes> ip;
60  std::bitset<NumInterruptTypes> ie;
61 
62  public:
63  using Params = RiscvInterruptsParams;
64 
65  Interrupts(const Params &p) : BaseInterrupts(p), ip(0), ie(0) {}
66 
67  std::bitset<NumInterruptTypes>
68  globalMask() const
69  {
70  INTERRUPT mask = 0;
75  switch (prv) {
76  case PRV_U:
77  mask.mei = (!sideleg.mei) | (sideleg.mei & status.uie);
78  mask.mti = (!sideleg.mti) | (sideleg.mti & status.uie);
79  mask.msi = (!sideleg.msi) | (sideleg.msi & status.uie);
80  mask.sei = (!sideleg.sei) | (sideleg.sei & status.uie);
81  mask.sti = (!sideleg.sti) | (sideleg.sti & status.uie);
82  mask.ssi = (!sideleg.ssi) | (sideleg.ssi & status.uie);
83  if (status.uie)
84  mask.uei = mask.uti = mask.usi = 1;
85  break;
86  case PRV_S:
87  mask.mei = (!mideleg.mei) | (mideleg.mei & status.sie);
88  mask.mti = (!mideleg.mti) | (mideleg.mti & status.sie);
89  mask.msi = (!mideleg.msi) | (mideleg.msi & status.sie);
90  if (status.sie)
91  mask.sei = mask.sti = mask.ssi = 1;
92  mask.uei = mask.uti = mask.usi = 0;
93  break;
94  case PRV_M:
95  if (status.mie)
96  mask.mei = mask.mti = mask.msi = 1;
97  mask.sei = mask.sti = mask.ssi = 0;
98  mask.uei = mask.uti = mask.usi = 0;
99  break;
100  default:
101  panic("Unknown privilege mode %d.", prv);
102  break;
103  }
104 
105  return std::bitset<NumInterruptTypes>(mask);
106  }
107 
108  bool
110  {
112  }
113 
114  bool checkInterrupt(int num) const { return ip[num] && ie[num]; }
115  bool checkInterrupts() const
116  {
117  return checkNonMaskableInterrupt() || (ip & ie & globalMask()).any();
118  }
119 
120  Fault
122  {
123  assert(checkInterrupts());
125  return std::make_shared<NonMaskableInterruptFault>();
126  std::bitset<NumInterruptTypes> mask = globalMask();
127  const std::vector<int> interrupt_order {
131  };
132  for (const int &id : interrupt_order)
133  if (checkInterrupt(id) && mask[id])
134  return std::make_shared<InterruptFault>(id);
135  return NoFault;
136  }
137 
138  void updateIntrInfo() {}
139 
140  void
141  post(int int_num, int index)
142  {
143  DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index);
144  ip[int_num] = true;
145  }
146 
147  void
148  clear(int int_num, int index)
149  {
150  DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index);
151  ip[int_num] = false;
152  }
153 
156 
157  void
159  {
160  DPRINTF(Interrupt, "All interrupts cleared\n");
161  ip = 0;
162  clearNMI();
163  }
164 
165  uint64_t readIP() const { return (uint64_t)ip.to_ulong(); }
166  uint64_t readIE() const { return (uint64_t)ie.to_ulong(); }
167  void setIP(const uint64_t& val) { ip = val; }
168  void setIE(const uint64_t& val) { ie = val; }
169 
170  void
172  {
173  unsigned long ip_ulong = ip.to_ulong();
174  unsigned long ie_ulong = ie.to_ulong();
175  SERIALIZE_SCALAR(ip_ulong);
176  SERIALIZE_SCALAR(ie_ulong);
177  }
178 
179  void
181  {
182  unsigned long ip_ulong;
183  unsigned long ie_ulong;
184  UNSERIALIZE_SCALAR(ip_ulong);
185  ip = ip_ulong;
186  UNSERIALIZE_SCALAR(ie_ulong);
187  ie = ie_ulong;
188  }
189 };
190 
191 } // namespace RiscvISA
192 } // namespace gem5
193 
194 #endif // __ARCH_RISCV_INTERRUPT_HH__
gem5::RiscvISA::PRV_S
@ PRV_S
Definition: isa.hh:56
gem5::RiscvISA::MISCREG_STATUS
@ MISCREG_STATUS
Definition: misc.hh:71
gem5::RiscvISA::INT_EXT_MACHINE
@ INT_EXT_MACHINE
Definition: faults.hh:95
gem5::RiscvISA::FaultType::INTERRUPT
@ INTERRUPT
gem5::RiscvISA::Interrupts::clear
void clear(int int_num, int index)
Definition: interrupts.hh:148
gem5::RiscvISA::Interrupts::readIE
uint64_t readIE() const
Definition: interrupts.hh:166
gem5::ThreadContext::readMiscReg
virtual RegVal readMiscReg(RegIndex misc_reg)=0
gem5::RiscvISA::INT_TIMER_USER
@ INT_TIMER_USER
Definition: faults.hh:90
gem5::NoFault
constexpr decltype(nullptr) NoFault
Definition: types.hh:260
gem5::RiscvISA::INT_TIMER_SUPER
@ INT_TIMER_SUPER
Definition: faults.hh:91
gem5::RiscvISA::PRV_M
@ PRV_M
Definition: isa.hh:57
faults.hh
gem5::RiscvISA::Interrupts::serialize
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: interrupts.hh:171
gem5::RiscvISA::Interrupts::ip
std::bitset< NumInterruptTypes > ip
Definition: interrupts.hh:59
gem5::RiscvISA::MISCREG_SIDELEG
@ MISCREG_SIDELEG
Definition: misc.hh:173
UNSERIALIZE_SCALAR
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:575
gem5::BaseInterrupts::tc
ThreadContext * tc
Definition: interrupts.hh:44
gem5::RiscvISA::Interrupts::checkInterrupt
bool checkInterrupt(int num) const
Definition: interrupts.hh:114
gem5::RiscvISA::INT_EXT_USER
@ INT_EXT_USER
Definition: faults.hh:93
gem5::RiscvISA::Interrupts::getInterrupt
Fault getInterrupt()
Definition: interrupts.hh:121
gem5::BaseInterrupts
Definition: interrupts.hh:41
gem5::CheckpointIn
Definition: serialize.hh:68
gem5::RiscvISA::Interrupts::checkInterrupts
bool checkInterrupts() const
Definition: interrupts.hh:115
gem5::RiscvISA::Interrupts::setIE
void setIE(const uint64_t &val)
Definition: interrupts.hh:168
gem5::RiscvISA::PrivilegeMode
PrivilegeMode
Definition: isa.hh:53
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:775
gem5::RiscvISA::INT_SOFTWARE_USER
@ INT_SOFTWARE_USER
Definition: faults.hh:87
std::vector< int >
misc.hh
gem5::RiscvISA::MISCREG_NMIE
@ MISCREG_NMIE
Definition: misc.hh:196
gem5::RiscvISA::INT_EXT_SUPER
@ INT_EXT_SUPER
Definition: faults.hh:94
gem5::RiscvISA::index
Bitfield< 30, 0 > index
Definition: pra_constants.hh:47
gem5::RiscvISA::Interrupts::clearAll
void clearAll()
Definition: interrupts.hh:158
gem5::RiscvISA::Interrupts::clearNMI
void clearNMI()
Definition: interrupts.hh:155
gem5::Fault
std::shared_ptr< FaultBase > Fault
Definition: types.hh:255
gem5::RiscvISA::INT_TIMER_MACHINE
@ INT_TIMER_MACHINE
Definition: faults.hh:92
sim_object.hh
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
gem5::RiscvISA::Interrupts::postNMI
void postNMI()
Definition: interrupts.hh:154
gem5::RiscvISA::Interrupts::updateIntrInfo
void updateIntrInfo()
Definition: interrupts.hh:138
gem5::RiscvISA::INT_SOFTWARE_SUPER
@ INT_SOFTWARE_SUPER
Definition: faults.hh:88
gem5::RiscvISA::Interrupts::globalMask
std::bitset< NumInterruptTypes > globalMask() const
Definition: interrupts.hh:68
gem5::RiscvISA::Interrupts::unserialize
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: interrupts.hh:180
gem5::RiscvISA::Interrupts::Interrupts
Interrupts(const Params &p)
Definition: interrupts.hh:65
gem5::RiscvISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::RiscvISA::mask
mask
Definition: pra_constants.hh:73
gem5::RiscvISA::Interrupts
Definition: interrupts.hh:56
gem5::RiscvISA::INT_SOFTWARE_MACHINE
@ INT_SOFTWARE_MACHINE
Definition: faults.hh:89
SERIALIZE_SCALAR
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:568
gem5::RiscvISA::Interrupts::setIP
void setIP(const uint64_t &val)
Definition: interrupts.hh:167
gem5::RiscvISA::Interrupts::readIP
uint64_t readIP() const
Definition: interrupts.hh:165
gem5::RiscvISA::Interrupts::ie
std::bitset< NumInterruptTypes > ie
Definition: interrupts.hh:60
gem5::ThreadContext::setMiscReg
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
interrupts.hh
logging.hh
gem5::RiscvISA::Interrupts::checkNonMaskableInterrupt
bool checkNonMaskableInterrupt() const
Definition: interrupts.hh:109
gem5::CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:66
gem5::RiscvISA::MISCREG_MIDELEG
@ MISCREG_MIDELEG
Definition: misc.hh:144
gem5::RiscvISA::Interrupts::post
void post(int int_num, int index)
Definition: interrupts.hh:141
gem5::RiscvISA::PRV_U
@ PRV_U
Definition: isa.hh:55
gem5::BaseInterrupts::Params
BaseInterruptsParams Params
Definition: interrupts.hh:47
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: tlb.cc:60
gem5::RiscvISA::MISCREG_PRV
@ MISCREG_PRV
Definition: misc.hh:65
gem5::ArmISA::Interrupt
Definition: faults.hh:556
thread_context.hh
gem5::RiscvISA::MISCREG_NMIP
@ MISCREG_NMIP
Definition: misc.hh:198
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
gem5::ArmISA::status
Bitfield< 5, 0 > status
Definition: misc_types.hh:423

Generated on Wed May 4 2022 12:13:46 for gem5 by doxygen 1.8.17