gem5 [DEVELOP-FOR-25.0]
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
38#include "arch/riscv/isa.hh"
40#include "sim/faults.hh"
41
42namespace gem5
43{
44
45class ThreadContext;
46
47namespace RiscvISA
48{
49
50enum FloatException : uint64_t
51{
57};
58
59/*
60 * In RISC-V, exception and interrupt codes share some values. They can be
61 * differentiated by an 'Interrupt' flag that is enabled for interrupt faults
62 * but not exceptions. The full fault cause can be computed by placing the
63 * exception (or interrupt) code in the least significant bits of the CAUSE
64 * CSR and then setting the highest bit of CAUSE with the 'Interrupt' flag.
65 * For more details on exception causes, see Chapter 3.1.20 of the RISC-V
66 * privileged specification v 1.10. Codes are enumerated in Table 3.6.
67 */
68enum ExceptionCode : uint64_t
69{
87
146 // INT_NMI does not exist in the spec, it's a modeling artifact for NMI. We
147 // intentionally set it to be NumInterruptTypes so it can never conflict
148 // with any real INT_NUM in used.
150};
151
158
159class RiscvFault : public FaultBase
160{
161 protected:
165
169
170 FaultName name() const override { return _name; }
171 bool isInterrupt() const { return _fault_type == FaultType::INTERRUPT; }
177 {
178 return isa->enableSmrnmi() && isNonMaskableInterrupt();
179 }
180 ExceptionCode exception() const { return _code; }
181 virtual RegVal trap_value() const { return 0; }
182
183 virtual void invokeSE(ThreadContext *tc, const StaticInstPtr &inst);
184 void invoke(ThreadContext *tc, const StaticInstPtr &inst) override;
185};
186
187class Reset : public FaultBase
188{
189 private:
191
192 public:
193 Reset() : _name("reset") {}
194 FaultName name() const override { return _name; }
195
196 void invoke(ThreadContext *tc, const StaticInstPtr &inst =
197 nullStaticInstPtr) override;
198};
199
201{
202 public:
207};
208
222
223class InstFault : public RiscvFault
224{
225 protected:
227
228 public:
232
233 RegVal trap_value() const override { return _inst.instBits; }
234};
235
237{
238 public:
240 : InstFault("Unknown instruction", inst)
241 {}
242
243 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
244};
245
247{
248 private:
249 const std::string reason;
250
251 public:
252 IllegalInstFault(std::string r, const ExtMachInst inst)
253 : InstFault("Illegal instruction", inst),
254 reason(r)
255 {}
256
257 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
258};
259
261{
262 private:
263 const std::string instName;
264
265 public:
266 UnimplementedFault(std::string name, const ExtMachInst inst)
267 : InstFault("Unimplemented instruction", inst),
269 {}
270
271 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
272};
273
275{
276 private:
277 const uint8_t frm;
278
279 public:
280 IllegalFrmFault(uint8_t r, const ExtMachInst inst)
281 : InstFault("Illegal floating-point rounding mode", inst),
282 frm(r)
283 {}
284
285 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
286};
287
289{
290 private:
291 const Addr _addr;
292
293 public:
295 : RiscvFault("Address", FaultType::OTHERS, code), _addr(addr)
296 {}
297
298 RegVal trap_value() const override { return _addr; }
299};
300
302{
303 private:
305
306 public:
308 : RiscvFault("Breakpoint", FaultType::OTHERS, BREAKPOINT),
309 pcState(pc.as<PCState>())
310 {}
311
312 RegVal trap_value() const override { return pcState.pc(); }
313 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
314};
315
317{
318 public:
320 : RiscvFault("System call", FaultType::OTHERS, ECALL_USER)
321 {
322 switch (prv) {
323 case PRV_U:
325 break;
326 case PRV_S:
328 break;
329 case PRV_M:
331 break;
332 default:
333 panic("Unknown privilege mode %d.", prv);
334 break;
335 }
336 }
337
338 void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
339};
340
351bool getFaultVAddr(Fault fault, Addr &va);
352
353} // namespace RiscvISA
354} // namespace gem5
355
356#endif // __ARCH_RISCV_FAULTS_HH__
RegVal trap_value() const override
Definition faults.hh:298
AddressFault(const Addr addr, ExceptionCode code)
Definition faults.hh:294
BreakpointFault(const PCStateBase &pc)
Definition faults.hh:307
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:247
RegVal trap_value() const override
Definition faults.hh:312
bool enableSmrnmi()
Definition isa.hh:200
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:240
IllegalFrmFault(uint8_t r, const ExtMachInst inst)
Definition faults.hh:280
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:224
IllegalInstFault(std::string r, const ExtMachInst inst)
Definition faults.hh:252
RegVal trap_value() const override
Definition faults.hh:233
InstFault(FaultName n, const ExtMachInst inst)
Definition faults.hh:229
const ExtMachInst _inst
Definition faults.hh:226
InterruptFault(ExceptionCode c)
Definition faults.hh:203
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition faults.cc:189
FaultName name() const override
Definition faults.hh:194
const FaultName _name
Definition faults.hh:190
bool isResumableNonMaskableInterrupt(ISA *isa) const
Definition faults.hh:176
RiscvFault(FaultName n, FaultType ft, ExceptionCode c)
Definition faults.hh:166
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
const FaultType _fault_type
Definition faults.hh:163
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
const FaultName _name
Definition faults.hh:162
virtual RegVal trap_value() const
Definition faults.hh:181
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:255
SyscallFault(PrivilegeMode prv)
Definition faults.hh:319
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:234
UnimplementedFault(std::string name, const ExtMachInst inst)
Definition faults.hh:266
UnknownInstFault(const ExtMachInst inst)
Definition faults.hh:239
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition faults.cc:216
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:267
Bitfield< 4 > pc
Bitfield< 5, 3 > c
@ STORE_ADDR_MISALIGNED
Definition faults.hh:76
@ AMO_ADDR_MISALIGNED
Definition faults.hh:77
@ INST_ADDR_MISALIGNED
Definition faults.hh:70
@ INT_TIMER_MACHINE
Definition faults.hh:93
@ INT_SOFTWARE_SUPER
Definition faults.hh:89
@ INT_SOFTWARE_MACHINE
Definition faults.hh:90
@ INT_SOFTWARE_USER
Definition faults.hh:88
@ LOAD_ADDR_MISALIGNED
Definition faults.hh:74
Bitfield< 1 > r
Definition pagetable.hh:78
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 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 Mon May 26 2025 09:19:01 for gem5 by doxygen 1.13.2