gem5 v25.0.0.1
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 // previous virtualization (H-extension)
77 bool pv = misa.rvh ? virtualizationEnabled(tc) : false;
78
79 // MISCREG_PRV (mirroring mpp) cannot have PRV_HS == 2
80 // it can only be 0 (U), 1 (S), 3 (M).
81 // Consult Table 8.8, 8.9 RISCV Privileged Spec V20211203
82 if (misa.rvh && pp == PRV_HS) {
83 panic("Privilege in MISCREG_PRV is PRV_HS == 2!");
84 }
85
86 // According to riscv-privileged-v1.11, if a NMI occurs at the middle
87 // of a M-mode trap handler, the state (epc/cause) will be overwritten
88 // and is not necessary recoverable unless smrnmi enabled.
89 warn_if(!isa->enableSmrnmi() && isNonMaskableInterrupt() &&
90 pp == PRV_M && status.mie == 0,
91 "NMI overwriting M-mode trap handler state");
92
93 // Set fault handler privilege mode
95 prv = PRV_M;
96 } else if (isInterrupt()) {
97 if (pp != PRV_M && misa.rvs &&
99 prv = PRV_S;
100 // when rvh is true we know rvs is true so prv is S
101 if (misa.rvh) {
102 if (virtualizationEnabled(tc) &&
104 resetV(tc); // No delegation, go to HS (S with V = 0)
105 }
106 // otherwise handled in VS (S with V = 1)
107 }
108 }
109 } else {
110 if (pp != PRV_M && misa.rvs &&
112 prv = PRV_S;
113
114 // when rvh is true we know rvs is true so prv is S
115 if (misa.rvh) {
116 if (virtualizationEnabled(tc) &&
118 resetV(tc); // No delegation, go to HS (S with V = 0)
119 }
120 // otherwise handled in VS (S with V = 1)
121 }
122 }
123 }
124
125 // Set fault registers and status
126 MiscRegIndex cause, epc, tvec, tval;
127 switch (prv) {
128 case PRV_U:
129 panic("Delegating interrupt to user mode is removed.");
130 break;
131 case PRV_S:
132 cause = MISCREG_SCAUSE;
133 epc = MISCREG_SEPC;
134 tvec = MISCREG_STVEC;
135 tval = MISCREG_STVAL;
136
137 status.spp = pp;
138 status.spie = status.sie;
139 status.sie = 0;
140 break;
141 case PRV_M:
142 cause = is_rnmi ? MISCREG_MNCAUSE : MISCREG_MCAUSE;
143 epc = is_rnmi ? MISCREG_MNEPC : MISCREG_MEPC;
145 tval = MISCREG_MTVAL;
146
147 if (is_rnmi) {
148 nstatus.mnpp = pp;
149 } else {
150 status.mpp = pp;
151 status.mpie = status.mie;
152 status.mie = 0;
153 }
154 break;
155 default:
156 panic("Unknown privilege mode %d.", prv);
157 break;
158 }
159
160 // H-extension extra handling for invoke
161 if (misa.rvh) {
162 if (prv == PRV_M) {
163 status.mpv = pv;
164 status.gva = mustSetGva();
165 // Paragraph 8.5.2 RISCV Privileged Spec 20211203
166 if (isGuestPageFault()) {
168 }
169 // Going to M-mode for handling, disable V if it's on
170 if (virtualizationEnabled(tc)) { resetV(tc); }
171 } else if (prv == PRV_S &&
172 !virtualizationEnabled(tc)) { // essentially HS-mode
173 HSTATUS hstatus = tc->readMiscReg(MISCREG_HSTATUS);
174 hstatus.spv = pv;
175 if (pv) { // if V-bit was on
176 hstatus.spvp = status.spp;
177 hstatus.gva = mustSetGva();
178 // Paragraph 8.5.2 RISCV Privileged Spec 20211203
179 if (isGuestPageFault()) {
181 }
182 }
183 // Write changes to hstatus
184 tc->setMiscReg(MISCREG_HSTATUS, hstatus);
185 } else if (prv == PRV_S &&
186 virtualizationEnabled(tc)) { // essentially VS-mode
187 STATUS vsstatus = tc->readMiscReg(MISCREG_VSSTATUS);
188 cause = MISCREG_VSCAUSE;
189 epc = MISCREG_VSEPC;
190 tvec = MISCREG_VSTVEC;
191 tval = MISCREG_VSTVAL;
192 vsstatus.spp = pp;
193 vsstatus.spie = vsstatus.sie;
194 vsstatus.sie = 0;
195 tc->setMiscReg(MISCREG_VSSTATUS, vsstatus);
196
197 // Paragraph 8.2.2 RISCV Privileged Spec 20211203
198 switch (_code) {
201 break;
204 break;
207 break;
208 default:
209 break;
210 }
211 } else {
212 panic("Unknown case in hypervisor fault handler."
213 "prv = %d, V = %d", prv, virtualizationEnabled(tc));
214 }
215 }
216
217 // Set fault cause, privilege, and return PC
218 uint64_t _cause = _code;
219 if (isInterrupt()) {
220 _cause |= CAUSE_INTERRUPT_MASKS[pc_state.rvType()];
221 }
222 tc->setMiscReg(cause, _cause);
223 if (pc_state.zcmtSecondFetch()) {
224 tc->setMiscReg(epc, pc_state.zcmtPc());
225 } else {
226 tc->setMiscReg(epc, pc_state.instAddr());
227 }
228 tc->setMiscReg(tval, trap_value());
229 tc->setMiscReg(MISCREG_PRV, prv);
230 if (is_rnmi) {
231 tc->setMiscReg(MISCREG_MNSTATUS, nstatus);
232 } else {
234 }
235 // Temporarily mask NMI while we're in NMI handler. Otherweise, the
236 // checkNonMaskableInterrupt will always return true and we'll be
237 // stucked in an infinite loop.
239 tc->setMiscReg(MISCREG_NMIE, 0);
240 }
241
242 // Clear load reservation address
243 isa->clearLoadReservation(tc->contextId());
244
245 // Set PC to fault handler address
246 Addr addr = isa->getFaultHandlerAddr(tvec, _code, isInterrupt());
247 if (pc_state.zcmtSecondFetch()) {
248 pc_state.zcmtSecondFetch(false);
249 pc_state.zcmtPc(0);
250 }
251 pc_state.set(isa->rvSext(addr));
252 tc->pcState(pc_state);
253 } else {
254 invokeSE(tc, inst);
255 }
256}
257
258void
260{
262 STATUS status = tc->readMiscReg(MISCREG_STATUS);
263 status.mie = 0;
264 status.mprv = 0;
267
268 // Advance the PC to the implementation-defined reset vector
269 auto workload = dynamic_cast<Workload *>(tc->getSystemPtr()->workload);
270 std::unique_ptr<PCState> new_pc(dynamic_cast<PCState *>(
271 tc->getIsaPtr()->newPCState(workload->getEntry())));
272 panic_if(!new_pc, "Failed create new PCState from ISA pointer");
273 VTYPE vtype = 0;
274 vtype.vill = 1;
275 new_pc->vtype(vtype);
276 new_pc->vl(0);
277 tc->pcState(*new_pc);
278
279 auto* mmu = tc->getMMUPtr();
280 if (mmu != nullptr) {
281 mmu->reset();
282 }
283}
284
285void
287{
288 auto *rsi = static_cast<RiscvStaticInst *>(inst.get());
289 panic("Unknown instruction 0x%08x at pc %s", rsi->machInst,
290 tc->pcState());
291}
292
293void
295{
296 if (! tc->getSystemPtr()->trapToGdb(GDBSignal::ILL, tc->contextId()) ) {
297 auto *rsi = static_cast<RiscvStaticInst *>(inst.get());
298 panic("Illegal instruction 0x%08x at pc %s: %s", rsi->machInst,
299 tc->pcState(), reason.c_str());
300 }
301}
302
303void
305{
306 panic("Unimplemented instruction %s at pc %s", instName, tc->pcState());
307}
308
309void
311{
312 panic("Illegal floating-point rounding mode 0x%x at pc %s.",
313 frm, tc->pcState());
314}
315
316void
318{
319 if (! tc->getSystemPtr()->trapToGdb(GDBSignal::TRAP, tc->contextId()) ) {
320 schedRelBreak(0);
321 }
322}
323
324void
326{
327 /* Advance the PC to next instruction so - once (simulated) syscall
328 is executed - execution continues. */
329 auto pc_state = tc->pcState().as<PCState>();
330 inst->advancePC(pc_state);
331 tc->pcState(pc_state);
332
333 tc->getSystemPtr()->workload->syscall(tc);
334}
335
336bool
338{
339 auto addr_fault = dynamic_cast<AddressFault *>(fault.get());
340 if (addr_fault) {
341 va = addr_fault->trap_value();
342 return true;
343 }
344
345 auto pgt_fault = dynamic_cast<GenericPageTableFault *>(fault.get());
346 if (pgt_fault) {
347 va = pgt_fault->getFaultVAddr();
348 return true;
349 }
350
351 return false;
352}
353
354} // namespace RiscvISA
355} // 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:317
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:310
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:294
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition faults.cc:259
bool isResumableNonMaskableInterrupt(ISA *isa) const
Definition faults.hh:75
virtual bool mustSetGva() const
Definition faults.hh:91
virtual RegVal trap_value2() const
Definition faults.hh:90
bool isInterrupt() const
Definition faults.hh:70
bool isGuestPageFault() const
Definition faults.hh:83
virtual void invokeSE(ThreadContext *tc, const StaticInstPtr &inst)
Definition faults.cc:54
ExceptionCode exception() const
Definition faults.hh:88
void invoke(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:60
FaultName name() const override
Definition faults.hh:69
bool isNonMaskableInterrupt() const
Definition faults.hh:71
ExceptionCode _code
Definition faults.hh:63
virtual RegVal trap_value() const
Definition faults.hh:89
Base class for all RISC-V static instructions.
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:325
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:304
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:286
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:1681
void resetV(ExecContext *xc)
Definition isa.cc:1347
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:337
bool virtualizationEnabled(ExecContext *xc)
Definition isa.cc:1331
@ MISCREG_HSTATUS
Definition misc.hh:213
@ MISCREG_MNCAUSE
Definition misc.hh:255
@ MISCREG_STATUS
Definition misc.hh:78
@ MISCREG_MEDELEG
Definition misc.hh:150
@ MISCREG_HIDELEG
Definition misc.hh:215
@ MISCREG_MIDELEG
Definition misc.hh:151
@ MISCREG_VSSTATUS
Definition misc.hh:227
@ MISCREG_MNSTATUS
Definition misc.hh:256
@ MISCREG_VSCAUSE
Definition misc.hh:231
@ MISCREG_HEDELEG
Definition misc.hh:214
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 Sat Oct 18 2025 08:06:38 for gem5 by doxygen 1.14.0