gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
interrupts.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Google
3 * Copyright (c) 2024 University of Rostock
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
32
33namespace gem5
34{
35
36namespace RiscvISA
37{
38
40 ip(0),
41 ie(0),
42 hvip(0),
44{
45 for (uint8_t i = 0;
46 i < p.port_local_interrupt_pins_connection_count;
47 ++i) {
48 uint8_t interruptID = p.local_interrupt_ids[i];
49 assert(interruptID <= 47);
50 std::string pinName =
51 csprintf("%s.local_interrupt_pins[%d]", p.name, i);
53 new IntSinkPin<Interrupts>(pinName,i, this, interruptID);
54 localInterruptPins.push_back(pin);
55 }
56}
57
58
59std::bitset<NumInterruptTypes>
61{
62 INTERRUPT mask = 0;
63 STATUS status = tc->readMiscReg(MISCREG_STATUS);
64 MISA misa = tc->readMiscRegNoEffect(MISCREG_ISA);
65 INTERRUPT mideleg = 0;
66 if (misa.rvs) {
67 mideleg = tc->readMiscReg(MISCREG_MIDELEG);
68 }
69 PrivilegeMode prv = (PrivilegeMode)tc->readMiscReg(MISCREG_PRV);
70 switch (prv) {
71 case PRV_U:
72 if (misa.rvh && !virtualizationEnabled(tc)) {
73 INTERRUPT hideleg = tc->readMiscReg(MISCREG_HIDELEG);
74 mask = ~mideleg | (mideleg & ~hideleg);
75 } else {
76 mask = gem5::mask(64);
77 }
78 break;
79 case PRV_S:
80 mask = ~mideleg;
81 if (misa.rvh) {
82 INTERRUPT hideleg = tc->readMiscReg(MISCREG_HIDELEG);
83 if (status.sie || virtualizationEnabled(tc)) {
84 mask |= (mideleg & ~hideleg);
85 }
87 STATUS vsstatus = tc->readMiscReg(MISCREG_VSSTATUS);
88 if (vsstatus.sie) { mask |= (mideleg & hideleg); }
89 }
90 } else {
91 if (status.sie) { mask |= mideleg; }
92 }
93 break;
94 case PRV_M:
95 if (status.mie) { mask = ~mideleg; }
96 break;
97 default:
98 panic("Unknown privilege mode %d.", prv);
99 break;
100 }
101
102 return std::bitset<NumInterruptTypes>(mask);
103}
104
105Fault
107{
108 assert(checkInterrupts());
110 return std::make_shared<NonMaskableInterruptFault>(nmi_cause);
111 std::bitset<NumInterruptTypes> mask = globalMask();
112 if (((ISA*) tc->getIsaPtr())->rvType() == RV64) {
113 const std::vector<int> interrupt_order {
126 //Table 5.1 from riscv-interrupts-1.0-RC3.pdf
127 //https://github.com/riscv/riscv-aia
132 };
133 for (const int &id : interrupt_order) {
134 if (checkInterrupt(id) && mask[id]) {
135 return std::make_shared<InterruptFault>(id);
136 }
137 }
138 } else if (((ISA*) tc->getIsaPtr())->rvType() == RV32) {
139 const std::vector<int> interrupt_order {
146 };
147 for (const int &id : interrupt_order) {
148 if (checkInterrupt(id) && mask[id]) {
149 return std::make_shared<InterruptFault>(id);
150 }
151 }
152 }
153 return NoFault;
154}
155
156void
157Interrupts::post(int int_num, int index)
158{
159 DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index);
160 if (int_num != INT_NMI) {
161 ip[int_num] = true;
162 } else {
163 postNMI();
164 }
165}
166
167void
168Interrupts::clear(int int_num, int index)
169{
170 DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index);
171 if (int_num != INT_NMI) {
172 ip[int_num] = false;
173 } else {
174 clearNMI();
175 }
176}
177
178void
180{
181 DPRINTF(Interrupt, "All interrupts cleared\n");
182 ip = 0;
183 clearNMI();
184}
185
186void
188{
189 tc->getCpuPtr()->postInterrupt(tc->threadId(), num + 16, 0);
190}
191
192void
194{
195 unsigned long ip_ulong = ip.to_ulong();
196 unsigned long hvip_ulong = hvip.to_ulong();
197 unsigned long ie_ulong = ie.to_ulong();
198 SERIALIZE_SCALAR(ip_ulong);
199 SERIALIZE_SCALAR(hvip_ulong);
200 SERIALIZE_SCALAR(ie_ulong);
201}
202
203void
205{
206 unsigned long ip_ulong;
207 unsigned long hvip_ulong;
208 unsigned long ie_ulong;
209 UNSERIALIZE_SCALAR(ip_ulong);
210 ip = ip_ulong;
211 UNSERIALIZE_SCALAR(hvip_ulong);
212 hvip = hvip_ulong;
213 UNSERIALIZE_SCALAR(ie_ulong);
214 ie = ie_ulong;
215}
216
217Port &
218Interrupts::getPort(const std::string &if_name, PortID idx)
219{
220
221 if (if_name == "local_interrupt_pins" && idx < localInterruptPins.size()) {
222 return *localInterruptPins[idx];
223 } else {
224 return BaseInterrupts::getPort(if_name, idx);
225 }
226}
227
228} // namespace RiscvISA
229
230} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
ThreadContext * tc
Definition interrupts.hh:44
BaseInterrupts(const Params &p)
Definition interrupts.hh:49
Ports are used to interface objects to each other.
Definition port.hh:62
std::vector< gem5::IntSinkPin< Interrupts > * > localInterruptPins
Definition interrupts.hh:68
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Interrupts(const Params &p)
Definition interrupts.cc:39
void clear(int int_num, int index) override
bool checkInterrupts() const override
Definition interrupts.hh:89
bool checkNonMaskableInterrupt() const
Definition interrupts.hh:80
void post(int int_num, int index) override
void serialize(CheckpointOut &cp) const override
Serialize an object.
void raiseInterruptPin(uint32_t num)
Port & getPort(const std::string &if_name, PortID idx) override
Get a port with a given name and index.
std::bitset< NumInterruptTypes > hvip
Definition interrupts.hh:66
RiscvInterruptsParams Params
Definition interrupts.hh:73
std::bitset< NumInterruptTypes > globalMask() const
Definition interrupts.cc:60
std::bitset< NumInterruptTypes > ip
Definition interrupts.hh:62
bool checkInterrupt(int num) const
Definition interrupts.hh:85
Fault getInterrupt() override
std::bitset< NumInterruptTypes > ie
Definition interrupts.hh:63
STL vector class.
Definition stl.hh:37
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:220
virtual Port & getPort(const std::string &if_name, PortID idx=InvalidPortID)
Get a port with a given name and index.
Bitfield< 3, 0 > mask
Definition pcstate.hh:63
Bitfield< 5, 0 > status
Bitfield< 0 > p
constexpr enums::RiscvType RV32
Definition pcstate.hh:56
Bitfield< 30, 0 > index
Bitfield< 2 > i
bool virtualizationEnabled(ExecContext *xc)
Definition isa.cc:1340
constexpr enums::RiscvType RV64
Definition pcstate.hh:57
@ MISCREG_STATUS
Definition misc.hh:78
@ MISCREG_HIDELEG
Definition misc.hh:215
@ MISCREG_MIDELEG
Definition misc.hh:151
@ MISCREG_VSSTATUS
Definition misc.hh:227
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
std::shared_ptr< FaultBase > Fault
Definition types.hh:249
std::ostream CheckpointOut
Definition serialize.hh:66
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition types.hh:245
std::string csprintf(const char *format, const Args &...args)
Definition cprintf.hh:161
IntSinkPinBase IntSinkPin
Definition intpin.hh:77
constexpr decltype(nullptr) NoFault
Definition types.hh:253
#define UNSERIALIZE_SCALAR(scalar)
Definition serialize.hh:575
#define SERIALIZE_SCALAR(scalar)
Definition serialize.hh:568

Generated on Mon Oct 27 2025 04:12:55 for gem5 by doxygen 1.14.0