gem5 v25.0.0.1
Loading...
Searching...
No Matches
faults.hh
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) 2024 University of Rostock
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#ifndef __ARCH_RISCV_FAULTS_HH__
33#define __ARCH_RISCV_FAULTS_HH__
34
35#include <cstdint>
36#include <string>
37
39#include "arch/riscv/isa.hh"
41#include "sim/faults.hh"
42
43namespace gem5
44{
45
46class ThreadContext;
47
48namespace RiscvISA
49{
50
57
58class RiscvFault : public FaultBase
59{
60 protected:
64
68
69 FaultName name() const override { return _name; }
70 bool isInterrupt() const { return _fault_type == FaultType::INTERRUPT; }
74
76 {
77 return isa->enableSmrnmi() && isNonMaskableInterrupt();
78 }
79
80 bool isPlainException() const {
82 }
87
88 ExceptionCode exception() const { return _code; }
89 virtual RegVal trap_value() const { return 0; }
90 virtual RegVal trap_value2() const { return 0; }
91 virtual bool mustSetGva() const { return false; }
92
93 virtual void invokeSE(ThreadContext *tc, const StaticInstPtr &inst);
94 void invoke(ThreadContext *tc, const StaticInstPtr &inst) override;
95};
96
97class Reset : public FaultBase
98{
99 private:
101
102 public:
103 Reset() : _name("reset") {}
104 FaultName name() const override { return _name; }
105
106 void invoke(ThreadContext *tc, const StaticInstPtr &inst =
107 nullStaticInstPtr) override;
108};
109
111{
112 public:
117};
118
132
133class InstFault : public RiscvFault
134{
135 protected:
137
138 public:
142
143 RegVal trap_value() const override { return _inst.instBits; }
144};
145
147{
148 protected:
150 std::string _reason;
151
152 public:
153 VirtualInstFault(std::string reason, const ExtMachInst inst)
154 : RiscvFault("VirtualInst", FaultType::OTHERS, VIRTUAL_INST),
155 _inst(inst), _reason(reason)
156 {}
157
158 RegVal trap_value() const override { return _inst.instBits; }
159};
160
162{
163 public:
165 : InstFault("Unknown instruction", inst)
166 {}
167
168 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
169};
170
172{
173 private:
174 const std::string reason;
175
176 public:
177 IllegalInstFault(std::string r, const ExtMachInst inst)
178 : InstFault("Illegal instruction", inst),
179 reason(r)
180 {}
181
182 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
183};
184
186{
187 private:
188 const std::string instName;
189
190 public:
191 UnimplementedFault(std::string name, const ExtMachInst inst)
192 : InstFault("Unimplemented instruction", inst),
194 {}
195
196 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
197};
198
200{
201 private:
202 const uint8_t frm;
203
204 public:
205 IllegalFrmFault(uint8_t r, const ExtMachInst inst)
206 : InstFault("Illegal floating-point rounding mode", inst),
207 frm(r)
208 {}
209
210 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
211};
212
214{
215 private:
218 const bool _two_stage;
219
220 public:
222 const Addr vaddr, ExceptionCode code,
223 const Addr gpaddr = 0x0, const bool two_stage = false)
224 : RiscvFault("Address", FaultType::OTHERS, code),
225 _vaddr(vaddr), _gpaddr(gpaddr), _two_stage(two_stage)
226 {}
227
228 bool mustSetGva() const override {
229 switch(_code) {
233
234 case INST_ACCESS: case LOAD_ACCESS: case STORE_ACCESS:
235 case INST_PAGE: case LOAD_PAGE: case STORE_PAGE:
237 return _two_stage;
238 default:
239 return false;
240 }
241 }
242 RegVal trap_value() const override { return _vaddr; }
243 RegVal trap_value2() const override { return _gpaddr >> 2; }
244};
245
247{
248 private:
250 const bool _virtualized;
251
252 public:
253 BreakpointFault(const PCStateBase &pc, const bool virtualized = false)
254 : RiscvFault("Breakpoint", FaultType::OTHERS, BREAKPOINT),
255 pcState(pc.as<PCState>()), _virtualized(virtualized)
256 {}
257
258 bool mustSetGva() const override { return _virtualized; }
259 RegVal trap_value() const override { return pcState.pc(); }
260 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
261};
262
264{
265 public:
266 SyscallFault(PrivilegeMode prv, const bool virtualized = false)
267 : RiscvFault("System call", FaultType::OTHERS, ECALL_USER)
268 {
269 switch (prv) {
270 case PRV_U:
272 break;
273 case PRV_S:
274 _code = virtualized ? ECALL_VIRTUAL_SUPER : ECALL_SUPER;
275 break;
276 case PRV_M:
278 break;
279 default:
280 panic("Unknown privilege mode %d.", prv);
281 break;
282 }
283 }
284
285 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
286};
287
298bool getFaultVAddr(Fault fault, Addr &va);
299
300} // namespace RiscvISA
301} // namespace gem5
302
303#endif // __ARCH_RISCV_FAULTS_HH__
RegVal trap_value() const override
Definition faults.hh:242
RegVal trap_value2() const override
Definition faults.hh:243
AddressFault(const Addr vaddr, ExceptionCode code, const Addr gpaddr=0x0, const bool two_stage=false)
Definition faults.hh:221
bool mustSetGva() const override
Definition faults.hh:228
BreakpointFault(const PCStateBase &pc, const bool virtualized=false)
Definition faults.hh:253
bool mustSetGva() const override
Definition faults.hh:258
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:317
RegVal trap_value() const override
Definition faults.hh:259
bool enableSmrnmi()
Definition isa.hh:211
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:310
IllegalFrmFault(uint8_t r, const ExtMachInst inst)
Definition faults.hh:205
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:294
IllegalInstFault(std::string r, const ExtMachInst inst)
Definition faults.hh:177
RegVal trap_value() const override
Definition faults.hh:143
InstFault(FaultName n, const ExtMachInst inst)
Definition faults.hh:139
const ExtMachInst _inst
Definition faults.hh:136
InterruptFault(ExceptionCode c)
Definition faults.hh:113
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition faults.cc:259
FaultName name() const override
Definition faults.hh:104
const FaultName _name
Definition faults.hh:100
bool isResumableNonMaskableInterrupt(ISA *isa) const
Definition faults.hh:75
virtual bool mustSetGva() const
Definition faults.hh:91
RiscvFault(FaultName n, FaultType ft, ExceptionCode c)
Definition faults.hh:65
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
const FaultType _fault_type
Definition faults.hh:62
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
const FaultName _name
Definition faults.hh:61
ExceptionCode _code
Definition faults.hh:63
bool isPlainException() const
Definition faults.hh:80
virtual RegVal trap_value() const
Definition faults.hh:89
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:325
SyscallFault(PrivilegeMode prv, const bool virtualized=false)
Definition faults.hh:266
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:304
UnimplementedFault(std::string name, const ExtMachInst inst)
Definition faults.hh:191
UnknownInstFault(const ExtMachInst inst)
Definition faults.hh:164
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:286
RegVal trap_value() const override
Definition faults.hh:158
VirtualInstFault(std::string reason, const ExtMachInst inst)
Definition faults.hh:153
ThreadContext is the external interface to all thread state for anything outside of the CPU.
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:220
Bitfield< 31 > n
Bitfield< 36 > as
Bitfield< 8 > va
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
Bitfield< 4 > pc
Bitfield< 5, 3 > c
Bitfield< 1 > r
Definition pagetable.hh:80
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
std::shared_ptr< FaultBase > Fault
Definition types.hh:249
uint64_t RegVal
Definition types.hh:173
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
const char * FaultName
Definition faults.hh:55
RefCountingPtr< StaticInst > StaticInstPtr
const StaticInstPtr nullStaticInstPtr
Statically allocated null StaticInstPtr.

Generated on Sat Oct 18 2025 08:06:39 for gem5 by doxygen 1.14.0