gem5  v22.1.0.0
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  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met: redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer;
11  * redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution;
14  * neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __ARCH_RISCV_FAULTS_HH__
32 #define __ARCH_RISCV_FAULTS_HH__
33 
34 #include <cstdint>
35 #include <string>
36 
37 #include "arch/riscv/isa.hh"
38 #include "cpu/null_static_inst.hh"
39 #include "sim/faults.hh"
40 
41 namespace gem5
42 {
43 
44 class ThreadContext;
45 
46 namespace RiscvISA
47 {
48 
49 enum FloatException : uint64_t
50 {
51  FloatInexact = 0x1,
54  FloatDivZero = 0x8,
55  FloatInvalid = 0x10
56 };
57 
58 /*
59  * In RISC-V, exception and interrupt codes share some values. They can be
60  * differentiated by an 'Interrupt' flag that is enabled for interrupt faults
61  * but not exceptions. The full fault cause can be computed by placing the
62  * exception (or interrupt) code in the least significant bits of the CAUSE
63  * CSR and then setting the highest bit of CAUSE with the 'Interrupt' flag.
64  * For more details on exception causes, see Chapter 3.1.20 of the RISC-V
65  * privileged specification v 1.10. Codes are enumerated in Table 3.6.
66  */
67 enum ExceptionCode : uint64_t
68 {
82  INST_PAGE = 12,
83  LOAD_PAGE = 13,
84  STORE_PAGE = 15,
85  AMO_PAGE = 15,
86 
97  // INT_NMI does not exist in the spec, it's a modeling artifact for NMI. We
98  // intentionally set it to be NumInterruptTypes so it can never conflict
99  // with any real INT_NUM in used.
101 };
102 
103 enum class FaultType
104 {
105  INTERRUPT,
107  OTHERS,
108 };
109 
110 class RiscvFault : public FaultBase
111 {
112  protected:
116 
118  : _name(n), _fault_type(ft), _code(c)
119  {}
120 
121  FaultName name() const override { return _name; }
122  bool isInterrupt() const { return _fault_type == FaultType::INTERRUPT; }
124  {
126  }
127  ExceptionCode exception() const { return _code; }
128  virtual RegVal trap_value() const { return 0; }
129 
130  virtual void invokeSE(ThreadContext *tc, const StaticInstPtr &inst);
131  void invoke(ThreadContext *tc, const StaticInstPtr &inst) override;
132 };
133 
134 class Reset : public FaultBase
135 {
136  private:
138 
139  public:
140  Reset() : _name("reset") {}
141  FaultName name() const override { return _name; }
142 
143  void invoke(ThreadContext *tc, const StaticInstPtr &inst =
144  nullStaticInstPtr) override;
145 };
146 
148 {
149  public:
151  : RiscvFault("interrupt", FaultType::INTERRUPT, c)
152  {}
153  InterruptFault(int c) : InterruptFault(static_cast<ExceptionCode>(c)) {}
154 };
155 
157 {
158  public:
160  : RiscvFault("non_maskable_interrupt",
162  static_cast<ExceptionCode>(0))
163  {}
164 };
165 
166 class InstFault : public RiscvFault
167 {
168  protected:
170 
171  public:
174  {}
175 
176  RegVal trap_value() const override { return _inst; }
177 };
178 
180 {
181  public:
183  : InstFault("Unknown instruction", inst)
184  {}
185 
186  void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
187 };
188 
190 {
191  private:
192  const std::string reason;
193 
194  public:
195  IllegalInstFault(std::string r, const ExtMachInst inst)
196  : InstFault("Illegal instruction", inst),
197  reason(r)
198  {}
199 
200  void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
201 };
202 
204 {
205  private:
206  const std::string instName;
207 
208  public:
209  UnimplementedFault(std::string name, const ExtMachInst inst)
210  : InstFault("Unimplemented instruction", inst),
211  instName(name)
212  {}
213 
214  void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
215 };
216 
218 {
219  private:
220  const uint8_t frm;
221 
222  public:
223  IllegalFrmFault(uint8_t r, const ExtMachInst inst)
224  : InstFault("Illegal floating-point rounding mode", inst),
225  frm(r)
226  {}
227 
228  void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
229 };
230 
231 class AddressFault : public RiscvFault
232 {
233  private:
234  const Addr _addr;
235 
236  public:
238  : RiscvFault("Address", FaultType::OTHERS, code), _addr(addr)
239  {}
240 
241  RegVal trap_value() const override { return _addr; }
242 };
243 
245 {
246  private:
248 
249  public:
251  : RiscvFault("Breakpoint", FaultType::OTHERS, BREAKPOINT),
252  pcState(pc.as<PCState>())
253  {}
254 
255  RegVal trap_value() const override { return pcState.pc(); }
256  void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
257 };
258 
259 class SyscallFault : public RiscvFault
260 {
261  public:
263  : RiscvFault("System call", FaultType::OTHERS, ECALL_USER)
264  {
265  switch (prv) {
266  case PRV_U:
267  _code = ECALL_USER;
268  break;
269  case PRV_S:
270  _code = ECALL_SUPER;
271  break;
272  case PRV_M:
274  break;
275  default:
276  panic("Unknown privilege mode %d.", prv);
277  break;
278  }
279  }
280 
281  void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override;
282 };
283 
284 } // namespace RiscvISA
285 } // namespace gem5
286 
287 #endif // __ARCH_RISCV_FAULTS_HH__
RegVal trap_value() const override
Definition: faults.hh:241
AddressFault(const Addr addr, ExceptionCode code)
Definition: faults.hh:237
BreakpointFault(const PCStateBase &pc)
Definition: faults.hh:250
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition: faults.cc:214
RegVal trap_value() const override
Definition: faults.hh:255
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition: faults.cc:207
IllegalFrmFault(uint8_t r, const ExtMachInst inst)
Definition: faults.hh:223
const std::string reason
Definition: faults.hh:192
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition: faults.cc:193
IllegalInstFault(std::string r, const ExtMachInst inst)
Definition: faults.hh:195
RegVal trap_value() const override
Definition: faults.hh:176
InstFault(FaultName n, const ExtMachInst inst)
Definition: faults.hh:172
const ExtMachInst _inst
Definition: faults.hh:169
InterruptFault(ExceptionCode c)
Definition: faults.hh:150
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition: faults.cc:169
FaultName name() const override
Definition: faults.hh:141
const FaultName _name
Definition: faults.hh:137
RiscvFault(FaultName n, FaultType ft, ExceptionCode c)
Definition: faults.hh:117
bool isInterrupt() const
Definition: faults.hh:122
virtual void invokeSE(ThreadContext *tc, const StaticInstPtr &inst)
Definition: faults.cc:52
ExceptionCode exception() const
Definition: faults.hh:127
const FaultType _fault_type
Definition: faults.hh:114
void invoke(ThreadContext *tc, const StaticInstPtr &inst) override
Definition: faults.cc:58
FaultName name() const override
Definition: faults.hh:121
bool isNonMaskableInterrupt() const
Definition: faults.hh:123
const FaultName _name
Definition: faults.hh:113
ExceptionCode _code
Definition: faults.hh:115
virtual RegVal trap_value() const
Definition: faults.hh:128
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition: faults.cc:220
SyscallFault(PrivilegeMode prv)
Definition: faults.hh:262
const std::string instName
Definition: faults.hh:206
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition: faults.cc:201
UnimplementedFault(std::string name, const ExtMachInst inst)
Definition: faults.hh:209
UnknownInstFault(const ExtMachInst inst)
Definition: faults.hh:182
void invokeSE(ThreadContext *tc, const StaticInstPtr &inst) override
Definition: faults.cc:185
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:178
Bitfield< 31 > n
Definition: misc_types.hh:462
Bitfield< 36 > as
Definition: misc_types.hh:514
@ FloatUnderflow
Definition: faults.hh:52
uint64_t ExtMachInst
Definition: types.hh:54
Bitfield< 4 > pc
Bitfield< 5, 3 > c
@ STORE_ADDR_MISALIGNED
Definition: faults.hh:75
@ AMO_ADDR_MISALIGNED
Definition: faults.hh:76
@ INT_TIMER_USER
Definition: faults.hh:90
@ INT_TIMER_SUPER
Definition: faults.hh:91
@ INST_ADDR_MISALIGNED
Definition: faults.hh:69
@ INT_TIMER_MACHINE
Definition: faults.hh:92
@ INT_SOFTWARE_SUPER
Definition: faults.hh:88
@ INT_EXT_MACHINE
Definition: faults.hh:95
@ INT_SOFTWARE_MACHINE
Definition: faults.hh:89
@ INT_SOFTWARE_USER
Definition: faults.hh:87
@ LOAD_ADDR_MISALIGNED
Definition: faults.hh:73
@ NumInterruptTypes
Definition: faults.hh:96
Bitfield< 1 > r
Definition: pagetable.hh:75
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
const char * FaultName
Definition: faults.hh:53
uint64_t RegVal
Definition: types.hh:173
const StaticInstPtr nullStaticInstPtr
Statically allocated null StaticInstPtr.

Generated on Wed Dec 21 2022 10:22:24 for gem5 by doxygen 1.9.1