gem5  v21.2.0.0
faults.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 The Hewlett-Packard Development Company
3  * All rights reserved.
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2003-2007 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #include "arch/x86/faults.hh"
42 
43 #include "arch/x86/generated/decoder.hh"
45 #include "arch/x86/mmu.hh"
46 #include "base/loader/symtab.hh"
47 #include "base/trace.hh"
48 #include "cpu/thread_context.hh"
49 #include "debug/Faults.hh"
50 #include "sim/full_system.hh"
51 #include "sim/process.hh"
52 
53 namespace gem5
54 {
55 
56 namespace X86ISA
57 {
58 
59 void
61 {
62  if (!FullSystem) {
63  FaultBase::invoke(tc, inst);
64  return;
65  }
66 
67  PCState pc = tc->pcState().as<PCState>();
68  DPRINTF(Faults, "RIP %#x: vector %d: %s\n", pc.pc(), vector, describe());
69  using namespace X86ISAInst::rom_labels;
70  HandyM5Reg m5reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
71  MicroPC entry;
72  if (m5reg.mode == LongMode) {
73  entry = isSoft() ? extern_label_longModeSoftInterrupt :
74  extern_label_longModeInterrupt;
75  } else {
76  entry = extern_label_legacyModeInterrupt;
77  }
78  tc->setIntReg(INTREG_MICRO(1), vector);
79  tc->setIntReg(INTREG_MICRO(7), pc.pc());
80  if (errorCode != (uint64_t)(-1)) {
81  if (m5reg.mode == LongMode) {
82  entry = extern_label_longModeInterruptWithError;
83  } else {
84  panic("Legacy mode interrupts with error codes "
85  "aren't implemented.");
86  }
87  // Software interrupts shouldn't have error codes. If one
88  // does, there would need to be microcode to set it up.
89  assert(!isSoft());
91  }
92  pc.upc(romMicroPC(entry));
93  pc.nupc(romMicroPC(entry) + 1);
94  tc->pcState(pc);
95 }
96 
97 std::string
99 {
100  std::stringstream ss;
101  ccprintf(ss, "%s", mnemonic());
102  if (errorCode != (uint64_t)(-1))
103  ccprintf(ss, "(%#x)", errorCode);
104 
105  return ss.str();
106 }
107 
108 void
110 {
111  // This is the same as a fault, but it happens -after- the
112  // instruction.
114 }
115 
116 void
118 {
119  panic("Abort exception!");
120 }
121 
122 void
124 {
125  if (FullSystem) {
126  X86Fault::invoke(tc, inst);
127  } else {
128  auto *xsi = static_cast<X86StaticInst *>(inst.get());
129  panic("Unrecognized/invalid instruction executed:\n %s",
130  xsi->machInst);
131  }
132 }
133 
134 void
136 {
137  if (FullSystem) {
138  // Invalidate any matching TLB entries before handling the page fault.
139  tc->getMMUPtr()->demapPage(addr, 0);
140  HandyM5Reg m5reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
142  // If something bad happens while trying to enter the page fault
143  // handler, I'm pretty sure that's a double fault and then all
144  // bets are off. That means it should be safe to update this
145  // state now.
146  if (m5reg.mode == LongMode)
148  else
149  tc->setMiscReg(MISCREG_CR2, (uint32_t)addr);
150  } else if (!tc->getProcessPtr()->fixupFault(addr)) {
151  PageFaultErrorCode code = errorCode;
152  const char *modeStr = "";
153  if (code.fetch)
154  modeStr = "execute";
155  else if (code.write)
156  modeStr = "write";
157  else
158  modeStr = "read";
159 
160  // print information about what we are panic'ing on
161  if (!inst) {
162  panic("Tried to %s unmapped address %#x.", modeStr, addr);
163  } else {
164  panic("Tried to %s unmapped address %#x.\nPC: %#x, Instr: %s",
165  modeStr, addr, tc->pcState(),
166  inst->disassemble(tc->pcState().instAddr(),
168  }
169  }
170 }
171 
172 std::string
174 {
175  std::stringstream ss;
176  ccprintf(ss, "%s at %#x", X86FaultBase::describe(), addr);
177  return ss.str();
178 }
179 
180 void
182 {
183  DPRINTF(Faults, "Init interrupt.\n");
184  // The otherwise unmodified integer registers should be set to 0.
185  for (int index = 0; index < NUM_ARCH_INTREGS; index++) {
186  tc->setIntReg(index, 0);
187  }
188 
189  CR0 cr0 = tc->readMiscReg(MISCREG_CR0);
190  CR0 newCR0 = 1 << 4;
191  newCR0.cd = cr0.cd;
192  newCR0.nw = cr0.nw;
193  tc->setMiscReg(MISCREG_CR0, newCR0);
194  tc->setMiscReg(MISCREG_CR2, 0);
195  tc->setMiscReg(MISCREG_CR3, 0);
196  tc->setMiscReg(MISCREG_CR4, 0);
197 
198  tc->setMiscReg(MISCREG_RFLAGS, 0x0000000000000002ULL);
199 
200  tc->setMiscReg(MISCREG_EFER, 0);
201 
202  SegAttr dataAttr = 0;
203  dataAttr.dpl = 0;
204  dataAttr.unusable = 0;
205  dataAttr.defaultSize = 0;
206  dataAttr.longMode = 0;
207  dataAttr.avl = 0;
208  dataAttr.granularity = 0;
209  dataAttr.present = 1;
210  dataAttr.type = 3;
211  dataAttr.writable = 1;
212  dataAttr.readable = 1;
213  dataAttr.expandDown = 0;
214  dataAttr.system = 1;
215 
216  for (int seg = 0; seg != NUM_SEGMENTREGS; seg++) {
217  tc->setMiscReg(MISCREG_SEG_SEL(seg), 0);
220  tc->setMiscReg(MISCREG_SEG_LIMIT(seg), 0xffff);
221  tc->setMiscReg(MISCREG_SEG_ATTR(seg), dataAttr);
222  }
223 
224  SegAttr codeAttr = 0;
225  codeAttr.dpl = 0;
226  codeAttr.unusable = 0;
227  codeAttr.defaultSize = 0;
228  codeAttr.longMode = 0;
229  codeAttr.avl = 0;
230  codeAttr.granularity = 0;
231  codeAttr.present = 1;
232  codeAttr.type = 10;
233  codeAttr.writable = 0;
234  codeAttr.readable = 1;
235  codeAttr.expandDown = 0;
236  codeAttr.system = 1;
237 
238  tc->setMiscReg(MISCREG_CS, 0xf000);
240  0x00000000ffff0000ULL);
242  0x00000000ffff0000ULL);
243  // This has the base value pre-added.
244  tc->setMiscReg(MISCREG_CS_LIMIT, 0xffffffff);
245  tc->setMiscReg(MISCREG_CS_ATTR, codeAttr);
246 
247  PCState pc(0x000000000000fff0ULL + tc->readMiscReg(MISCREG_CS_BASE));
248  tc->pcState(pc);
249 
251  tc->setMiscReg(MISCREG_TSG_LIMIT, 0xffff);
252 
254  tc->setMiscReg(MISCREG_IDTR_LIMIT, 0xffff);
255 
256  SegAttr tslAttr = 0;
257  tslAttr.present = 1;
258  tslAttr.type = 2; // LDT
259  tc->setMiscReg(MISCREG_TSL, 0);
261  tc->setMiscReg(MISCREG_TSL_LIMIT, 0xffff);
262  tc->setMiscReg(MISCREG_TSL_ATTR, tslAttr);
263 
264  SegAttr trAttr = 0;
265  trAttr.present = 1;
266  trAttr.type = 3; // Busy 16-bit TSS
267  tc->setMiscReg(MISCREG_TR, 0);
268  tc->setMiscReg(MISCREG_TR_BASE, 0);
269  tc->setMiscReg(MISCREG_TR_LIMIT, 0xffff);
270  tc->setMiscReg(MISCREG_TR_ATTR, trAttr);
271 
272  // This value should be the family/model/stepping of the processor.
273  // (page 418). It should be consistent with the value from CPUID, but
274  // the actual value probably doesn't matter much.
275  tc->setIntReg(INTREG_RDX, 0);
276 
277  tc->setMiscReg(MISCREG_DR0, 0);
278  tc->setMiscReg(MISCREG_DR1, 0);
279  tc->setMiscReg(MISCREG_DR2, 0);
280  tc->setMiscReg(MISCREG_DR3, 0);
281 
282  tc->setMiscReg(MISCREG_DR6, 0x00000000ffff0ff0ULL);
283  tc->setMiscReg(MISCREG_DR7, 0x0000000000000400ULL);
284 
285  tc->setMiscReg(MISCREG_MXCSR, 0x1f80);
286 
287  // Flag all elements on the x87 stack as empty.
288  tc->setMiscReg(MISCREG_FTW, 0xFFFF);
289 
290  // Update the handy M5 Reg.
291  tc->setMiscReg(MISCREG_M5_REG, 0);
292  MicroPC entry = X86ISAInst::rom_labels::extern_label_initIntHalt;
293  pc.upc(romMicroPC(entry));
294  pc.nupc(romMicroPC(entry) + 1);
295  tc->pcState(pc);
296 }
297 
298 void
300 {
301  DPRINTF(Faults, "Startup interrupt with vector %#x.\n", vector);
302  HandyM5Reg m5Reg = tc->readMiscReg(MISCREG_M5_REG);
303  if (m5Reg.mode != LegacyMode || m5Reg.submode != RealMode) {
304  panic("Startup IPI recived outside of real mode. "
305  "Don't know what to do. %d, %d", m5Reg.mode, m5Reg.submode);
306  }
307 
308  tc->setMiscReg(MISCREG_CS, vector << 8);
309  tc->setMiscReg(MISCREG_CS_BASE, vector << 12);
311  // This has the base value pre-added.
312  tc->setMiscReg(MISCREG_CS_LIMIT, 0xffff);
313 
315 }
316 
317 } // namespace X86ISA
318 } // namespace gem5
gem5::ThreadContext::setIntReg
virtual void setIntReg(RegIndex reg_idx, RegVal val)=0
gem5::X86ISA::pc
Bitfield< 19 > pc
Definition: misc.hh:811
gem5::PCStateBase::instAddr
Addr instAddr() const
Returns the memory address of the instruction this PC points to.
Definition: pcstate.hh:107
gem5::ThreadContext::readMiscReg
virtual RegVal readMiscReg(RegIndex misc_reg)=0
gem5::X86ISA::MISCREG_M5_REG
@ MISCREG_M5_REG
Definition: misc.hh:143
gem5::X86ISA::MISCREG_SEG_SEL
static MiscRegIndex MISCREG_SEG_SEL(int index)
Definition: misc.hh:511
gem5::X86ISA::MISCREG_TR
@ MISCREG_TR
Definition: misc.hh:313
gem5::X86ISA::MISCREG_DR1
@ MISCREG_DR1
Definition: misc.hh:131
mmu.hh
gem5::X86ISA::MISCREG_TSL
@ MISCREG_TSL
Definition: misc.hh:309
static_inst.hh
gem5::X86ISA::MISCREG_DR3
@ MISCREG_DR3
Definition: misc.hh:133
gem5::X86ISA::MISCREG_TSG_LIMIT
@ MISCREG_TSG_LIMIT
Definition: misc.hh:360
gem5::X86ISA::MISCREG_SEG_LIMIT
static MiscRegIndex MISCREG_SEG_LIMIT(int index)
Definition: misc.hh:532
gem5::PCStateBase::as
Target & as()
Definition: pcstate.hh:72
gem5::X86ISA::X86FaultBase::vector
uint8_t vector
Definition: faults.hh:61
gem5::X86ISA::X86FaultBase::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition: faults.cc:60
gem5::ThreadContext::getMMUPtr
virtual BaseMMU * getMMUPtr()=0
gem5::ThreadContext::pcState
virtual const PCStateBase & pcState() const =0
gem5::X86ISA::MISCREG_SEG_ATTR
static MiscRegIndex MISCREG_SEG_ATTR(int index)
Definition: misc.hh:539
gem5::X86ISA::InvalidOpcode::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition: faults.cc:123
gem5::X86ISA::MISCREG_TR_BASE
@ MISCREG_TR_BASE
Definition: misc.hh:329
gem5::RefCountingPtr::get
T * get() const
Directly access the pointer itself without taking a reference.
Definition: refcnt.hh:227
gem5::X86ISA::InitInterrupt::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition: faults.cc:181
gem5::X86ISA::MISCREG_DR2
@ MISCREG_DR2
Definition: misc.hh:132
faults.hh
gem5::X86ISA::MISCREG_IDTR_BASE
@ MISCREG_IDTR_BASE
Definition: misc.hh:330
gem5::X86ISA::X86FaultBase::errorCode
uint64_t errorCode
Definition: faults.hh:62
gem5::X86ISA::MISCREG_CR3
@ MISCREG_CR3
Definition: misc.hh:114
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
gem5::RefCountingPtr< StaticInst >
gem5::Process::fixupFault
bool fixupFault(Addr vaddr)
Attempt to fix up a fault at vaddr by allocating a page on the stack.
Definition: process.cc:365
gem5::X86ISA::MISCREG_CR2
@ MISCREG_CR2
Definition: misc.hh:113
gem5::MicroPC
uint16_t MicroPC
Definition: types.hh:149
gem5::X86ISA::MISCREG_CS_BASE
@ MISCREG_CS_BASE
Definition: misc.hh:319
gem5::X86ISA::MISCREG_IDTR_LIMIT
@ MISCREG_IDTR_LIMIT
Definition: misc.hh:364
gem5::X86ISA::MISCREG_DR7
@ MISCREG_DR7
Definition: misc.hh:137
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:94
gem5::X86ISA::NUM_SEGMENTREGS
@ NUM_SEGMENTREGS
Definition: segment.hh:65
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
process.hh
gem5::X86ISA::MISCREG_DR0
@ MISCREG_DR0
Definition: misc.hh:130
gem5::X86ISA::MISCREG_CR4
@ MISCREG_CR4
Definition: misc.hh:115
gem5::BaseMMU::demapPage
void demapPage(Addr vaddr, uint64_t asn)
Definition: mmu.cc:97
gem5::X86ISA::MISCREG_CS_ATTR
@ MISCREG_CS_ATTR
Definition: misc.hh:369
gem5::X86ISA::MISCREG_EFER
@ MISCREG_EFER
Definition: misc.hh:251
ss
std::stringstream ss
Definition: trace.test.cc:45
gem5::X86ISA::MISCREG_SEG_BASE
static MiscRegIndex MISCREG_SEG_BASE(int index)
Definition: misc.hh:518
gem5::X86ISA::MISCREG_CS_LIMIT
@ MISCREG_CS_LIMIT
Definition: misc.hh:353
gem5::X86ISA::MISCREG_RFLAGS
@ MISCREG_RFLAGS
Definition: misc.hh:140
gem5::X86ISA::MISCREG_TSL_ATTR
@ MISCREG_TSL_ATTR
Definition: misc.hh:375
gem5::ThreadContext::readMiscRegNoEffect
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
gem5::X86ISA::MISCREG_TSL_BASE
@ MISCREG_TSL_BASE
Definition: misc.hh:325
gem5::X86ISA::PageFault::describe
virtual std::string describe() const
Definition: faults.cc:173
gem5::X86ISA::MISCREG_DR6
@ MISCREG_DR6
Definition: misc.hh:136
gem5::X86ISA::MISCREG_MXCSR
@ MISCREG_MXCSR
Definition: misc.hh:386
gem5::X86ISA::StartupInterrupt::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition: faults.cc:299
gem5::FaultBase::invoke
virtual void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr)
Definition: faults.cc:58
full_system.hh
gem5::X86ISA::INTREG_MICRO
static IntRegIndex INTREG_MICRO(int index)
Definition: int.hh:175
gem5::X86ISA::MISCREG_CS
@ MISCREG_CS
Definition: misc.hh:303
gem5::X86ISA::MISCREG_CS_EFF_BASE
@ MISCREG_CS_EFF_BASE
Definition: misc.hh:337
gem5::ThreadContext::getProcessPtr
virtual Process * getProcessPtr()=0
gem5::FullSystem
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition: root.cc:220
gem5::X86ISA::MISCREG_TSG_BASE
@ MISCREG_TSG_BASE
Definition: misc.hh:326
gem5::X86ISA::MISCREG_TR_LIMIT
@ MISCREG_TR_LIMIT
Definition: misc.hh:363
gem5::X86ISA::X86StaticInst
Base class for all X86 static instructions.
Definition: static_inst.hh:99
gem5::StaticInst::disassemble
virtual const std::string & disassemble(Addr pc, const loader::SymbolTable *symtab=nullptr) const
Return string representation of disassembled instruction.
Definition: static_inst.cc:60
gem5::X86ISA::index
Bitfield< 5, 3 > index
Definition: types.hh:98
gem5::X86ISA::MISCREG_SEG_EFF_BASE
static MiscRegIndex MISCREG_SEG_EFF_BASE(int index)
Definition: misc.hh:525
gem5::ThreadContext::setMiscReg
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
gem5::X86ISA::seg
Bitfield< 2, 0 > seg
Definition: types.hh:87
gem5::X86ISA::MISCREG_CR0
@ MISCREG_CR0
Definition: misc.hh:111
gem5::X86ISA::PCState
Definition: pcstate.hh:50
gem5::X86ISA::RealMode
@ RealMode
Definition: types.hh:202
gem5::X86ISA::X86FaultBase::isSoft
virtual bool isSoft()
Definition: faults.hh:73
gem5::X86ISA::MISCREG_TR_ATTR
@ MISCREG_TR_ATTR
Definition: misc.hh:379
gem5::romMicroPC
static MicroPC romMicroPC(MicroPC upc)
Definition: types.hh:154
trace.hh
gem5::X86ISA::MISCREG_FTW
@ MISCREG_FTW
Definition: misc.hh:389
symtab.hh
gem5::X86ISA::X86Abort::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition: faults.cc:117
gem5::loader::debugSymbolTable
SymbolTable debugSymbolTable
Global unified debugging symbol table (for target).
Definition: symtab.cc:44
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: tlb.cc:60
gem5::X86ISA::MISCREG_TSL_LIMIT
@ MISCREG_TSL_LIMIT
Definition: misc.hh:359
gem5::X86ISA::X86Trap::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition: faults.cc:109
gem5::X86ISA::X86FaultBase::mnemonic
virtual const char * mnemonic() const
Definition: faults.hh:72
thread_context.hh
gem5::X86ISA::X86FaultBase::describe
virtual std::string describe() const
Definition: faults.cc:98
gem5::X86ISA::PageFault::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr)
Definition: faults.cc:135
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84

Generated on Tue Dec 21 2021 11:34:19 for gem5 by doxygen 1.8.17