gem5  v21.0.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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/registers.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 class BaseCPU;
45 class ThreadContext;
46 
47 namespace RiscvISA {
48 
49 /*
50  * This is based on version 1.10 of the RISC-V privileged ISA reference,
51  * chapter 3.1.14.
52  */
53 class Interrupts : public BaseInterrupts
54 {
55  private:
56  std::bitset<NumInterruptTypes> ip;
57  std::bitset<NumInterruptTypes> ie;
58 
59  public:
60  using Params = RiscvInterruptsParams;
61 
62  Interrupts(const Params &p) : BaseInterrupts(p), ip(0), ie(0) {}
63 
64  std::bitset<NumInterruptTypes>
65  globalMask() const
66  {
67  INTERRUPT mask = 0;
69  INTERRUPT mideleg = tc->readMiscReg(MISCREG_MIDELEG);
70  INTERRUPT sideleg = tc->readMiscReg(MISCREG_SIDELEG);
72  switch (prv) {
73  case PRV_U:
74  mask.mei = (!sideleg.mei) | (sideleg.mei & status.uie);
75  mask.mti = (!sideleg.mti) | (sideleg.mti & status.uie);
76  mask.msi = (!sideleg.msi) | (sideleg.msi & status.uie);
77  mask.sei = (!sideleg.sei) | (sideleg.sei & status.uie);
78  mask.sti = (!sideleg.sti) | (sideleg.sti & status.uie);
79  mask.ssi = (!sideleg.ssi) | (sideleg.ssi & status.uie);
80  if (status.uie)
81  mask.uei = mask.uti = mask.usi = 1;
82  break;
83  case PRV_S:
84  mask.mei = (!mideleg.mei) | (mideleg.mei & status.sie);
85  mask.mti = (!mideleg.mti) | (mideleg.mti & status.sie);
86  mask.msi = (!mideleg.msi) | (mideleg.msi & status.sie);
87  if (status.sie)
88  mask.sei = mask.sti = mask.ssi = 1;
89  mask.uei = mask.uti = mask.usi = 0;
90  break;
91  case PRV_M:
92  if (status.mie)
93  mask.mei = mask.mti = mask.msi = 1;
94  mask.sei = mask.sti = mask.ssi = 0;
95  mask.uei = mask.uti = mask.usi = 0;
96  break;
97  default:
98  panic("Unknown privilege mode %d.", prv);
99  break;
100  }
101 
102  return std::bitset<NumInterruptTypes>(mask);
103  }
104 
105  bool checkInterrupt(int num) const { return ip[num] && ie[num]; }
106  bool checkInterrupts() const
107  {
108  return (ip & ie & globalMask()).any();
109  }
110 
111  Fault
113  {
114  assert(checkInterrupts());
115  std::bitset<NumInterruptTypes> mask = globalMask();
116  const std::vector<int> interrupt_order {
120  };
121  for (const int &id : interrupt_order)
122  if (checkInterrupt(id) && mask[id])
123  return std::make_shared<InterruptFault>(id);
124  return NoFault;
125  }
126 
127  void updateIntrInfo() {}
128 
129  void
130  post(int int_num, int index)
131  {
132  DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index);
133  ip[int_num] = true;
134  }
135 
136  void
137  clear(int int_num, int index)
138  {
139  DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index);
140  ip[int_num] = false;
141  }
142 
143  void
145  {
146  DPRINTF(Interrupt, "All interrupts cleared\n");
147  ip = 0;
148  }
149 
150  uint64_t readIP() const { return (uint64_t)ip.to_ulong(); }
151  uint64_t readIE() const { return (uint64_t)ie.to_ulong(); }
152  void setIP(const uint64_t& val) { ip = val; }
153  void setIE(const uint64_t& val) { ie = val; }
154 
155  void
157  {
158  unsigned long ip_ulong = ip.to_ulong();
159  unsigned long ie_ulong = ie.to_ulong();
160  SERIALIZE_SCALAR(ip_ulong);
161  SERIALIZE_SCALAR(ie_ulong);
162  }
163 
164  void
166  {
167  unsigned long ip_ulong;
168  unsigned long ie_ulong;
169  UNSERIALIZE_SCALAR(ip_ulong);
170  ip = ip_ulong;
171  UNSERIALIZE_SCALAR(ie_ulong);
172  ie = ie_ulong;
173  }
174 };
175 
176 } // namespace RiscvISA
177 
178 #endif // __ARCH_RISCV_INTERRUPT_HH__
RiscvISA::MISCREG_PRV
@ MISCREG_PRV
Definition: registers.hh:154
ArmISA::status
Bitfield< 5, 0 > status
Definition: miscregs_types.hh:417
RiscvISA::Interrupts::unserialize
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: interrupts.hh:165
RiscvISA::Interrupts::clear
void clear(int int_num, int index)
Definition: interrupts.hh:137
RiscvISA::INT_TIMER_USER
@ INT_TIMER_USER
Definition: faults.hh:83
RiscvISA::INT_SOFTWARE_USER
@ INT_SOFTWARE_USER
Definition: faults.hh:80
RiscvISA::Interrupts::setIE
void setIE(const uint64_t &val)
Definition: interrupts.hh:153
faults.hh
RiscvISA::INT_EXT_MACHINE
@ INT_EXT_MACHINE
Definition: faults.hh:88
UNSERIALIZE_SCALAR
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:591
RiscvISA::Interrupts::readIP
uint64_t readIP() const
Definition: interrupts.hh:150
RiscvISA::Interrupts::updateIntrInfo
void updateIntrInfo()
Definition: interrupts.hh:127
RiscvISA::PRV_S
@ PRV_S
Definition: isa.hh:59
RiscvISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
std::vector< int >
RiscvISA::MISCREG_SIDELEG
@ MISCREG_SIDELEG
Definition: registers.hh:262
RiscvISA::INT_TIMER_MACHINE
@ INT_TIMER_MACHINE
Definition: faults.hh:85
RiscvISA::MISCREG_STATUS
@ MISCREG_STATUS
Definition: registers.hh:160
RiscvISA
Definition: fs_workload.cc:37
RiscvISA::INT_TIMER_SUPER
@ INT_TIMER_SUPER
Definition: faults.hh:84
RiscvISA::index
Bitfield< 30, 0 > index
Definition: pra_constants.hh:44
RiscvISA::Interrupts::checkInterrupts
bool checkInterrupts() const
Definition: interrupts.hh:106
RiscvISA::INT_EXT_USER
@ INT_EXT_USER
Definition: faults.hh:86
cp
Definition: cprintf.cc:37
RiscvISA::Interrupts::clearAll
void clearAll()
Definition: interrupts.hh:144
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
RiscvISA::INT_SOFTWARE_MACHINE
@ INT_SOFTWARE_MACHINE
Definition: faults.hh:82
sim_object.hh
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:237
Fault
std::shared_ptr< FaultBase > Fault
Definition: types.hh:246
RiscvISA::Interrupts::globalMask
std::bitset< NumInterruptTypes > globalMask() const
Definition: interrupts.hh:65
RiscvISA::Interrupts::readIE
uint64_t readIE() const
Definition: interrupts.hh:151
RiscvISA::Interrupts::Interrupts
Interrupts(const Params &p)
Definition: interrupts.hh:62
RiscvISA::Interrupts::setIP
void setIP(const uint64_t &val)
Definition: interrupts.hh:152
RiscvISA::PrivilegeMode
PrivilegeMode
Definition: isa.hh:56
RiscvISA::PRV_U
@ PRV_U
Definition: isa.hh:58
RiscvISA::INT_SOFTWARE_SUPER
@ INT_SOFTWARE_SUPER
Definition: faults.hh:81
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
NoFault
constexpr decltype(nullptr) NoFault
Definition: types.hh:251
BaseInterrupts::Params
BaseInterruptsParams Params
Definition: interrupts.hh:44
SERIALIZE_SCALAR
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:584
RiscvISA::Interrupts::getInterrupt
Fault getInterrupt()
Definition: interrupts.hh:112
registers.hh
BaseCPU
Definition: base.hh:104
RiscvISA::mask
mask
Definition: pra_constants.hh:70
RiscvISA::Interrupts::post
void post(int int_num, int index)
Definition: interrupts.hh:130
ThreadContext::readMiscReg
virtual RegVal readMiscReg(RegIndex misc_reg)=0
interrupts.hh
RiscvISA::Interrupts::ip
std::bitset< NumInterruptTypes > ip
Definition: interrupts.hh:56
RiscvISA::Interrupts::checkInterrupt
bool checkInterrupt(int num) const
Definition: interrupts.hh:105
logging.hh
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:64
RiscvISA::Interrupts::serialize
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: interrupts.hh:156
RiscvISA::Interrupts
Definition: interrupts.hh:53
BaseInterrupts
Definition: interrupts.hh:38
BaseInterrupts::tc
ThreadContext * tc
Definition: interrupts.hh:41
CheckpointIn
Definition: serialize.hh:68
RiscvISA::PRV_M
@ PRV_M
Definition: isa.hh:60
thread_context.hh
RiscvISA::INT_EXT_SUPER
@ INT_EXT_SUPER
Definition: faults.hh:87
RiscvISA::Interrupts::ie
std::bitset< NumInterruptTypes > ie
Definition: interrupts.hh:57
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
RiscvISA::MISCREG_MIDELEG
@ MISCREG_MIDELEG
Definition: registers.hh:233

Generated on Tue Jun 22 2021 15:28:19 for gem5 by doxygen 1.8.17