gem5 v23.0.0.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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) at PC: %s\n",
65 name(), pc_state);
66
67 if (FullSystem) {
69 PrivilegeMode prv = PRV_M;
70 STATUS status = tc->readMiscReg(MISCREG_STATUS);
71
72 // According to riscv-privileged-v1.11, if a NMI occurs at the middle
73 // of a M-mode trap handler, the state (epc/cause) will be overwritten
74 // and is not necessary recoverable. There's nothing we can do here so
75 // we'll just warn our user that the CPU state might be broken.
76 warn_if(isNonMaskableInterrupt() && pp == PRV_M && status.mie == 0,
77 "NMI overwriting M-mode trap handler state");
78
79 // Set fault handler privilege mode
81 prv = PRV_M;
82 } else if (isInterrupt()) {
83 if (pp != PRV_M &&
85 prv = PRV_S;
86 }
87 if (pp == PRV_U &&
89 prv = PRV_U;
90 }
91 } else {
92 if (pp != PRV_M &&
94 prv = PRV_S;
95 }
96 if (pp == PRV_U &&
98 prv = PRV_U;
99 }
100 }
101
102 // Set fault registers and status
103 MiscRegIndex cause, epc, tvec, tval;
104 switch (prv) {
105 case PRV_U:
106 cause = MISCREG_UCAUSE;
107 epc = MISCREG_UEPC;
108 tvec = MISCREG_UTVEC;
109 tval = MISCREG_UTVAL;
110
111 status.upie = status.uie;
112 status.uie = 0;
113 break;
114 case PRV_S:
115 cause = MISCREG_SCAUSE;
116 epc = MISCREG_SEPC;
117 tvec = MISCREG_STVEC;
118 tval = MISCREG_STVAL;
119
120 status.spp = pp;
121 status.spie = status.sie;
122 status.sie = 0;
123 break;
124 case PRV_M:
125 cause = MISCREG_MCAUSE;
126 epc = MISCREG_MEPC;
128 tval = MISCREG_MTVAL;
129
130 status.mpp = pp;
131 status.mpie = status.mie;
132 status.mie = 0;
133 break;
134 default:
135 panic("Unknown privilege mode %d.", prv);
136 break;
137 }
138
139 // Set fault cause, privilege, and return PC
140 uint64_t _cause = _code;
141 if (isInterrupt()) {
142 _cause |= CAUSE_INTERRUPT_MASKS[pc_state.rvType()];
143 }
144 tc->setMiscReg(cause, _cause);
145 tc->setMiscReg(epc, tc->pcState().instAddr());
146 tc->setMiscReg(tval, trap_value());
147 tc->setMiscReg(MISCREG_PRV, prv);
149 // Temporarily mask NMI while we're in NMI handler. Otherweise, the
150 // checkNonMaskableInterrupt will always return true and we'll be
151 // stucked in an infinite loop.
153 tc->setMiscReg(MISCREG_NMIE, 0);
154 }
155
156 // Clear load reservation address
158
159 // Set PC to fault handler address
160 Addr addr = mbits(tc->readMiscReg(tvec), 63, 2);
161 if (isInterrupt() && bits(tc->readMiscReg(tvec), 1, 0) == 1)
162 addr += 4 * _code;
163 pc_state.set(addr);
164 tc->pcState(pc_state);
165 } else {
166 inst->advancePC(pc_state);
167 tc->pcState(pc_state);
168 invokeSE(tc, inst);
169 }
170}
171
172void
174{
176 STATUS status = tc->readMiscReg(MISCREG_STATUS);
177 status.mie = 0;
178 status.mprv = 0;
181
182 // Advance the PC to the implementation-defined reset vector
183 auto workload = dynamic_cast<Workload *>(tc->getSystemPtr()->workload);
184 std::unique_ptr<PCState> new_pc(dynamic_cast<PCState *>(
185 tc->getIsaPtr()->newPCState(workload->getEntry())));
186 panic_if(!new_pc, "Failed create new PCState from ISA pointer");
187 tc->pcState(*new_pc);
188
189 // Reset PMP Cfg
190 auto* mmu = dynamic_cast<RiscvISA::MMU*>(tc->getMMUPtr());
191 if (mmu == nullptr) {
192 warn("MMU is not Riscv MMU instance, we can't reset PMP");
193 return;
194 }
195 mmu->getPMP()->pmpReset();
196}
197
198void
200{
201 auto *rsi = static_cast<RiscvStaticInst *>(inst.get());
202 panic("Unknown instruction 0x%08x at pc %s", rsi->machInst,
203 tc->pcState());
204}
205
206void
208{
209 auto *rsi = static_cast<RiscvStaticInst *>(inst.get());
210 panic("Illegal instruction 0x%08x at pc %s: %s", rsi->machInst,
211 tc->pcState(), reason.c_str());
212}
213
214void
216{
217 panic("Unimplemented instruction %s at pc %s", instName, tc->pcState());
218}
219
220void
222{
223 panic("Illegal floating-point rounding mode 0x%x at pc %s.",
224 frm, tc->pcState());
225}
226
227void
229{
230 schedRelBreak(0);
231}
232
233void
235{
236 tc->getSystemPtr()->workload->syscall(tc);
237}
238
239} // namespace RiscvISA
240} // namespace gem5
#define DPRINTFS(x, s,...)
Definition trace.hh:217
virtual void clearLoadReservation(ContextID cid)
Definition isa.hh:73
virtual PCStateBase * newPCState(Addr new_inst_addr=0) const =0
Target & as()
Definition pcstate.hh:72
Addr instAddr() const
Returns the memory address of the instruction this PC points to.
Definition pcstate.hh:107
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:228
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:221
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:207
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition faults.cc:173
bool isInterrupt() const
Definition faults.hh:122
virtual void invokeSE(ThreadContext *tc, const StaticInstPtr &inst)
Definition faults.cc:54
void invoke(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:60
FaultName name() const override
Definition faults.hh:121
bool isNonMaskableInterrupt() const
Definition faults.hh:123
virtual RegVal trap_value() const
Definition faults.hh:128
Base class for all RISC-V static instructions.
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:234
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:215
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:199
virtual void advancePC(PCStateBase &pc_state) const =0
Workload * workload
OS kernel.
Definition system.hh:326
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 BaseMMU * getMMUPtr()=0
virtual ContextID contextId() const =0
virtual void syscall(ThreadContext *tc)
Definition workload.hh:111
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:76
constexpr T mbits(T val, unsigned first, unsigned last)
Mask off the given bits in place like bits() but without shifting.
Definition bitfield.hh:103
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
#define warn(...)
Definition logging.hh:256
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition logging.hh:283
Bitfield< 5, 0 > status
const RegVal CAUSE_INTERRUPT_MASKS[enums::Num_RiscvType]
Definition misc.hh:920
@ MISCREG_SIDELEG
Definition misc.hh:177
@ MISCREG_STATUS
Definition misc.hh:75
@ MISCREG_MEDELEG
Definition misc.hh:147
@ MISCREG_SEDELEG
Definition misc.hh:176
@ MISCREG_MIDELEG
Definition misc.hh:148
Bitfield< 3 > addr
Definition types.hh:84
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
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
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 Jul 10 2023 15:31:58 for gem5 by doxygen 1.9.7