gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
faults.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 RISC-V Foundation
3 * Copyright (c) 2016 The University of Virginia
4 * Copyright (c) 2018 TU Dresden
5 * Copyright (c) 2020 Barkhausen Institut
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer;
12 * redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution;
15 * neither the name of the copyright holders nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "arch/riscv/faults.hh"
33
35#include "arch/riscv/isa.hh"
36#include "arch/riscv/mmu.hh"
37#include "arch/riscv/pmp.hh"
39#include "arch/riscv/utility.hh"
40#include "cpu/base.hh"
41#include "cpu/thread_context.hh"
42#include "debug/Faults.hh"
43#include "sim/debug.hh"
44#include "sim/full_system.hh"
45#include "sim/workload.hh"
46
47namespace gem5
48{
49
50namespace RiscvISA
51{
52
53void
55{
56 panic("Fault %s encountered at pc %s.", name(), tc->pcState());
57}
58
59void
61{
62 auto pc_state = tc->pcState().as<PCState>();
63
64 DPRINTFS(Faults, tc->getCpuPtr(), "Fault (%s, %u) at PC: %s\n",
65 name(), exception(), pc_state);
66
67 if (FullSystem) {
69 PrivilegeMode prv = PRV_M;
70 MISA misa = tc->readMiscRegNoEffect(MISCREG_ISA);
71 STATUS status = tc->readMiscReg(MISCREG_STATUS);
72 NSTATUS nstatus = tc->readMiscReg(MISCREG_MNSTATUS);
73 auto* isa = static_cast<RiscvISA::ISA*>(tc->getIsaPtr());
74 bool is_rnmi = isResumableNonMaskableInterrupt(isa);
75
76 // According to riscv-privileged-v1.11, if a NMI occurs at the middle
77 // of a M-mode trap handler, the state (epc/cause) will be overwritten
78 // and is not necessary recoverable unless smrnmi enabled.
79 warn_if(!isa->enableSmrnmi() && isNonMaskableInterrupt() &&
80 pp == PRV_M && status.mie == 0,
81 "NMI overwriting M-mode trap handler state");
82
83 // Set fault handler privilege mode
85 prv = PRV_M;
86 } else if (isInterrupt()) {
87 if (pp != PRV_M &&
89 prv = (misa.rvs) ? PRV_S : ((misa.rvn) ? PRV_U : PRV_M);
90 }
91 if (pp == PRV_U && misa.rvs && misa.rvn &&
93 prv = PRV_U;
94 }
95 } else {
96 if (pp != PRV_M &&
98 prv = (misa.rvs) ? PRV_S : ((misa.rvn) ? PRV_U : PRV_M);
99 }
100 if (pp == PRV_U && misa.rvs && misa.rvn &&
102 prv = PRV_U;
103 }
104 }
105
106 // Set fault registers and status
107 MiscRegIndex cause, epc, tvec, tval;
108 switch (prv) {
109 case PRV_U:
110 cause = MISCREG_UCAUSE;
111 epc = MISCREG_UEPC;
112 tvec = MISCREG_UTVEC;
113 tval = MISCREG_UTVAL;
114
115 status.upie = status.uie;
116 status.uie = 0;
117 break;
118 case PRV_S:
119 cause = MISCREG_SCAUSE;
120 epc = MISCREG_SEPC;
121 tvec = MISCREG_STVEC;
122 tval = MISCREG_STVAL;
123
124 status.spp = pp;
125 status.spie = status.sie;
126 status.sie = 0;
127 break;
128 case PRV_M:
129 cause = is_rnmi ? MISCREG_MNCAUSE : MISCREG_MCAUSE;
130 epc = is_rnmi ? MISCREG_MNEPC : MISCREG_MEPC;
132 tval = MISCREG_MTVAL;
133
134 if (is_rnmi) {
135 nstatus.mnpp = pp;
136 } else {
137 status.mpp = pp;
138 status.mpie = status.mie;
139 status.mie = 0;
140 }
141 break;
142 default:
143 panic("Unknown privilege mode %d.", prv);
144 break;
145 }
146
147 // Set fault cause, privilege, and return PC
148 uint64_t _cause = _code;
149 if (isInterrupt()) {
150 _cause |= CAUSE_INTERRUPT_MASKS[pc_state.rvType()];
151 }
152 tc->setMiscReg(cause, _cause);
153 if (pc_state.zcmtSecondFetch()) {
154 tc->setMiscReg(epc, pc_state.zcmtPc());
155 } else {
156 tc->setMiscReg(epc, pc_state.instAddr());
157 }
158 tc->setMiscReg(tval, trap_value());
159 tc->setMiscReg(MISCREG_PRV, prv);
160 if (is_rnmi) {
161 tc->setMiscReg(MISCREG_MNSTATUS, nstatus);
162 } else {
164 }
165 // Temporarily mask NMI while we're in NMI handler. Otherweise, the
166 // checkNonMaskableInterrupt will always return true and we'll be
167 // stucked in an infinite loop.
169 tc->setMiscReg(MISCREG_NMIE, 0);
170 }
171
172 // Clear load reservation address
173 isa->clearLoadReservation(tc->contextId());
174
175 // Set PC to fault handler address
176 Addr addr = isa->getFaultHandlerAddr(tvec, _code, isInterrupt());
177 if (pc_state.zcmtSecondFetch()) {
178 pc_state.zcmtSecondFetch(false);
179 pc_state.zcmtPc(0);
180 }
181 pc_state.set(isa->rvSext(addr));
182 tc->pcState(pc_state);
183 } else {
184 invokeSE(tc, inst);
185 }
186}
187
188void
190{
192 STATUS status = tc->readMiscReg(MISCREG_STATUS);
193 status.mie = 0;
194 status.mprv = 0;
197
198 // Advance the PC to the implementation-defined reset vector
199 auto workload = dynamic_cast<Workload *>(tc->getSystemPtr()->workload);
200 std::unique_ptr<PCState> new_pc(dynamic_cast<PCState *>(
201 tc->getIsaPtr()->newPCState(workload->getEntry())));
202 panic_if(!new_pc, "Failed create new PCState from ISA pointer");
203 VTYPE vtype = 0;
204 vtype.vill = 1;
205 new_pc->vtype(vtype);
206 new_pc->vl(0);
207 tc->pcState(*new_pc);
208
209 auto* mmu = tc->getMMUPtr();
210 if (mmu != nullptr) {
211 mmu->reset();
212 }
213}
214
215void
217{
218 auto *rsi = static_cast<RiscvStaticInst *>(inst.get());
219 panic("Unknown instruction 0x%08x at pc %s", rsi->machInst,
220 tc->pcState());
221}
222
223void
225{
226 if (! tc->getSystemPtr()->trapToGdb(GDBSignal::ILL, tc->contextId()) ) {
227 auto *rsi = static_cast<RiscvStaticInst *>(inst.get());
228 panic("Illegal instruction 0x%08x at pc %s: %s", rsi->machInst,
229 tc->pcState(), reason.c_str());
230 }
231}
232
233void
235{
236 panic("Unimplemented instruction %s at pc %s", instName, tc->pcState());
237}
238
239void
241{
242 panic("Illegal floating-point rounding mode 0x%x at pc %s.",
243 frm, tc->pcState());
244}
245
246void
248{
249 if (! tc->getSystemPtr()->trapToGdb(GDBSignal::TRAP, tc->contextId()) ) {
250 schedRelBreak(0);
251 }
252}
253
254void
256{
257 /* Advance the PC to next instruction so - once (simulated) syscall
258 is executed - execution continues. */
259 auto pc_state = tc->pcState().as<PCState>();
260 inst->advancePC(pc_state);
261 tc->pcState(pc_state);
262
263 tc->getSystemPtr()->workload->syscall(tc);
264}
265
266bool
268{
269 auto addr_fault = dynamic_cast<AddressFault *>(fault.get());
270 if (addr_fault) {
271 va = addr_fault->trap_value();
272 return true;
273 }
274
275 auto pgt_fault = dynamic_cast<GenericPageTableFault *>(fault.get());
276 if (pgt_fault) {
277 va = pgt_fault->getFaultVAddr();
278 return true;
279 }
280
281 return false;
282}
283
284} // namespace RiscvISA
285} // namespace gem5
#define DPRINTFS(x, s,...)
Definition trace.hh:216
virtual PCStateBase * newPCState(Addr new_inst_addr=0) const =0
Target & as()
Definition pcstate.hh:73
T * get() const
Directly access the pointer itself without taking a reference.
Definition refcnt.hh:227
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:247
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:240
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:224
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition faults.cc:189
bool isResumableNonMaskableInterrupt(ISA *isa) const
Definition faults.hh:176
bool isInterrupt() const
Definition faults.hh:171
virtual void invokeSE(ThreadContext *tc, const StaticInstPtr &inst)
Definition faults.cc:54
ExceptionCode exception() const
Definition faults.hh:180
void invoke(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:60
FaultName name() const override
Definition faults.hh:170
bool isNonMaskableInterrupt() const
Definition faults.hh:172
virtual RegVal trap_value() const
Definition faults.hh:181
Base class for all RISC-V static instructions.
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:255
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:234
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:216
virtual void advancePC(PCStateBase &pc_state) const =0
Workload * workload
OS kernel.
Definition system.hh:331
bool trapToGdb(GDBSignal signal, ContextID ctx_id) const
Definition system.cc:407
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual RegVal readMiscReg(RegIndex misc_reg)=0
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
virtual System * getSystemPtr()=0
virtual BaseISA * getIsaPtr() const =0
virtual BaseCPU * getCpuPtr()=0
virtual const PCStateBase & pcState() const =0
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
virtual BaseMMU * getMMUPtr()=0
virtual ContextID contextId() const =0
virtual void syscall(ThreadContext *tc)
Definition workload.hh:113
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition bitfield.hh:79
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:220
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:246
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition logging.hh:315
Bitfield< 5, 0 > status
Bitfield< 8 > va
const RegVal CAUSE_INTERRUPT_MASKS[enums::Num_RiscvType]
Definition misc.hh:1593
bool getFaultVAddr(Fault fault, Addr &va)
Returns true if the fault passed as a first argument was triggered by a memory access,...
Definition faults.cc:267
@ MISCREG_SIDELEG
Definition misc.hh:178
@ MISCREG_MNCAUSE
Definition misc.hh:217
@ MISCREG_STATUS
Definition misc.hh:76
@ MISCREG_MEDELEG
Definition misc.hh:148
@ MISCREG_SEDELEG
Definition misc.hh:177
@ MISCREG_MIDELEG
Definition misc.hh:149
@ MISCREG_MNSTATUS
Definition misc.hh:218
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
std::shared_ptr< FaultBase > Fault
Definition types.hh:249
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition root.cc:220
RefCountingPtr< StaticInst > StaticInstPtr
void schedRelBreak(Tick delta)
Cause the simulator to execute a breakpoint relative to the current tick.
Definition debug.cc:93
PMP header file.

Generated on Mon May 26 2025 09:18:59 for gem5 by doxygen 1.13.2