gem5  v20.1.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"
44 #include "arch/x86/isa_traits.hh"
45 #include "base/loader/symtab.hh"
46 #include "base/trace.hh"
47 #include "cpu/thread_context.hh"
48 #include "debug/Faults.hh"
49 #include "sim/full_system.hh"
50 #include "sim/process.hh"
51 
52 namespace X86ISA
53 {
54 
55 void
57 {
58  if (!FullSystem) {
59  FaultBase::invoke(tc, inst);
60  return;
61  }
62 
63  PCState pcState = tc->pcState();
64  Addr pc = pcState.pc();
65  DPRINTF(Faults, "RIP %#x: vector %d: %s\n", pc, vector, describe());
66  using namespace X86ISAInst::RomLabels;
67  HandyM5Reg m5reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
68  MicroPC entry;
69  if (m5reg.mode == LongMode) {
70  entry = isSoft() ? extern_label_longModeSoftInterrupt :
71  extern_label_longModeInterrupt;
72  } else {
73  entry = extern_label_legacyModeInterrupt;
74  }
75  tc->setIntReg(INTREG_MICRO(1), vector);
76  tc->setIntReg(INTREG_MICRO(7), pc);
77  if (errorCode != (uint64_t)(-1)) {
78  if (m5reg.mode == LongMode) {
79  entry = extern_label_longModeInterruptWithError;
80  } else {
81  panic("Legacy mode interrupts with error codes "
82  "aren't implemented.");
83  }
84  // Software interrupts shouldn't have error codes. If one
85  // does, there would need to be microcode to set it up.
86  assert(!isSoft());
88  }
89  pcState.upc(romMicroPC(entry));
90  pcState.nupc(romMicroPC(entry) + 1);
91  tc->pcState(pcState);
92 }
93 
94 std::string
96 {
97  std::stringstream ss;
98  ccprintf(ss, "%s", mnemonic());
99  if (errorCode != (uint64_t)(-1))
100  ccprintf(ss, "(%#x)", errorCode);
101 
102  return ss.str();
103 }
104 
105 void
107 {
109  if (!FullSystem)
110  return;
111 
112  // This is the same as a fault, but it happens -after- the
113  // instruction.
114  PCState pc = tc->pcState();
115  pc.uEnd();
116 }
117 
118 void
120 {
121  panic("Abort exception!");
122 }
123 
124 void
126 {
127  if (FullSystem) {
128  X86Fault::invoke(tc, inst);
129  } else {
130  panic("Unrecognized/invalid instruction executed:\n %s",
131  inst->machInst);
132  }
133 }
134 
135 void
137 {
138  if (FullSystem) {
139  // Invalidate any matching TLB entries before handling the page fault.
140  tc->getITBPtr()->demapPage(addr, 0);
141  tc->getDTBPtr()->demapPage(addr, 0);
142  HandyM5Reg m5reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
144  // If something bad happens while trying to enter the page fault
145  // handler, I'm pretty sure that's a double fault and then all
146  // bets are off. That means it should be safe to update this
147  // state now.
148  if (m5reg.mode == LongMode)
150  else
151  tc->setMiscReg(MISCREG_CR2, (uint32_t)addr);
152  } else if (!tc->getProcessPtr()->fixupFault(addr)) {
153  PageFaultErrorCode code = errorCode;
154  const char *modeStr = "";
155  if (code.fetch)
156  modeStr = "execute";
157  else if (code.write)
158  modeStr = "write";
159  else
160  modeStr = "read";
161 
162  // print information about what we are panic'ing on
163  if (!inst) {
164  panic("Tried to %s unmapped address %#x.", modeStr, addr);
165  } else {
166  panic("Tried to %s unmapped address %#x.\nPC: %#x, Instr: %s",
167  modeStr, addr, tc->pcState().pc(),
168  inst->disassemble(tc->pcState().pc(),
170  }
171  }
172 }
173 
174 std::string
176 {
177  std::stringstream ss;
178  ccprintf(ss, "%s at %#x", X86FaultBase::describe(), addr);
179  return ss.str();
180 }
181 
182 void
184 {
185  DPRINTF(Faults, "Init interrupt.\n");
186  // The otherwise unmodified integer registers should be set to 0.
187  for (int index = 0; index < NUM_INTREGS; index++) {
188  tc->setIntReg(index, 0);
189  }
190 
191  CR0 cr0 = tc->readMiscReg(MISCREG_CR0);
192  CR0 newCR0 = 1 << 4;
193  newCR0.cd = cr0.cd;
194  newCR0.nw = cr0.nw;
195  tc->setMiscReg(MISCREG_CR0, newCR0);
196  tc->setMiscReg(MISCREG_CR2, 0);
197  tc->setMiscReg(MISCREG_CR3, 0);
198  tc->setMiscReg(MISCREG_CR4, 0);
199 
200  tc->setMiscReg(MISCREG_RFLAGS, 0x0000000000000002ULL);
201 
202  tc->setMiscReg(MISCREG_EFER, 0);
203 
204  SegAttr dataAttr = 0;
205  dataAttr.dpl = 0;
206  dataAttr.unusable = 0;
207  dataAttr.defaultSize = 0;
208  dataAttr.longMode = 0;
209  dataAttr.avl = 0;
210  dataAttr.granularity = 0;
211  dataAttr.present = 1;
212  dataAttr.type = 3;
213  dataAttr.writable = 1;
214  dataAttr.readable = 1;
215  dataAttr.expandDown = 0;
216  dataAttr.system = 1;
217 
218  for (int seg = 0; seg != NUM_SEGMENTREGS; seg++) {
219  tc->setMiscReg(MISCREG_SEG_SEL(seg), 0);
222  tc->setMiscReg(MISCREG_SEG_LIMIT(seg), 0xffff);
223  tc->setMiscReg(MISCREG_SEG_ATTR(seg), dataAttr);
224  }
225 
226  SegAttr codeAttr = 0;
227  codeAttr.dpl = 0;
228  codeAttr.unusable = 0;
229  codeAttr.defaultSize = 0;
230  codeAttr.longMode = 0;
231  codeAttr.avl = 0;
232  codeAttr.granularity = 0;
233  codeAttr.present = 1;
234  codeAttr.type = 10;
235  codeAttr.writable = 0;
236  codeAttr.readable = 1;
237  codeAttr.expandDown = 0;
238  codeAttr.system = 1;
239 
240  tc->setMiscReg(MISCREG_CS, 0xf000);
242  0x00000000ffff0000ULL);
244  0x00000000ffff0000ULL);
245  // This has the base value pre-added.
246  tc->setMiscReg(MISCREG_CS_LIMIT, 0xffffffff);
247  tc->setMiscReg(MISCREG_CS_ATTR, codeAttr);
248 
249  PCState pc(0x000000000000fff0ULL + tc->readMiscReg(MISCREG_CS_BASE));
250  tc->pcState(pc);
251 
253  tc->setMiscReg(MISCREG_TSG_LIMIT, 0xffff);
254 
256  tc->setMiscReg(MISCREG_IDTR_LIMIT, 0xffff);
257 
258  SegAttr tslAttr = 0;
259  tslAttr.present = 1;
260  tslAttr.type = 2; // LDT
261  tc->setMiscReg(MISCREG_TSL, 0);
263  tc->setMiscReg(MISCREG_TSL_LIMIT, 0xffff);
264  tc->setMiscReg(MISCREG_TSL_ATTR, tslAttr);
265 
266  SegAttr trAttr = 0;
267  trAttr.present = 1;
268  trAttr.type = 3; // Busy 16-bit TSS
269  tc->setMiscReg(MISCREG_TR, 0);
270  tc->setMiscReg(MISCREG_TR_BASE, 0);
271  tc->setMiscReg(MISCREG_TR_LIMIT, 0xffff);
272  tc->setMiscReg(MISCREG_TR_ATTR, trAttr);
273 
274  // This value should be the family/model/stepping of the processor.
275  // (page 418). It should be consistent with the value from CPUID, but
276  // the actual value probably doesn't matter much.
277  tc->setIntReg(INTREG_RDX, 0);
278 
279  tc->setMiscReg(MISCREG_DR0, 0);
280  tc->setMiscReg(MISCREG_DR1, 0);
281  tc->setMiscReg(MISCREG_DR2, 0);
282  tc->setMiscReg(MISCREG_DR3, 0);
283 
284  tc->setMiscReg(MISCREG_DR6, 0x00000000ffff0ff0ULL);
285  tc->setMiscReg(MISCREG_DR7, 0x0000000000000400ULL);
286 
287  tc->setMiscReg(MISCREG_MXCSR, 0x1f80);
288 
289  // Flag all elements on the x87 stack as empty.
290  tc->setMiscReg(MISCREG_FTW, 0xFFFF);
291 
292  // Update the handy M5 Reg.
293  tc->setMiscReg(MISCREG_M5_REG, 0);
294  MicroPC entry = X86ISAInst::RomLabels::extern_label_initIntHalt;
295  pc.upc(romMicroPC(entry));
296  pc.nupc(romMicroPC(entry) + 1);
297  tc->pcState(pc);
298 }
299 
300 void
302 {
303  DPRINTF(Faults, "Startup interrupt with vector %#x.\n", vector);
304  HandyM5Reg m5Reg = tc->readMiscReg(MISCREG_M5_REG);
305  if (m5Reg.mode != LegacyMode || m5Reg.submode != RealMode) {
306  panic("Startup IPI recived outside of real mode. "
307  "Don't know what to do. %d, %d", m5Reg.mode, m5Reg.submode);
308  }
309 
310  tc->setMiscReg(MISCREG_CS, vector << 8);
311  tc->setMiscReg(MISCREG_CS_BASE, vector << 12);
313  // This has the base value pre-added.
314  tc->setMiscReg(MISCREG_CS_LIMIT, 0xffff);
315 
317 }
318 
319 } // namespace X86ISA
X86ISA::MISCREG_M5_REG
@ MISCREG_M5_REG
Definition: misc.hh:137
ThreadContext::readMiscRegNoEffect
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
X86ISA::X86FaultBase::vector
uint8_t vector
Definition: faults.hh:57
X86ISA::MISCREG_TSG_LIMIT
@ MISCREG_TSG_LIMIT
Definition: misc.hh:354
X86ISA::StartupInterrupt::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=StaticInst::nullStaticInstPtr) override
Definition: faults.cc:301
X86ISA::MISCREG_TSL
@ MISCREG_TSL
Definition: misc.hh:303
romMicroPC
static MicroPC romMicroPC(MicroPC upc)
Definition: types.hh:149
X86ISA::MISCREG_TR_LIMIT
@ MISCREG_TR_LIMIT
Definition: misc.hh:357
GenericISA::UPCState::upc
MicroPC upc() const
Definition: types.hh:215
X86ISA::MISCREG_CS
@ MISCREG_CS
Definition: misc.hh:297
X86ISA::X86FaultBase::mnemonic
virtual const char * mnemonic() const
Definition: faults.hh:68
ThreadContext::setIntReg
virtual void setIntReg(RegIndex reg_idx, RegVal val)=0
X86ISA::MISCREG_SEG_LIMIT
static MiscRegIndex MISCREG_SEG_LIMIT(int index)
Definition: misc.hh:526
X86ISA::MISCREG_CR2
@ MISCREG_CR2
Definition: misc.hh:107
FaultBase::invoke
virtual void invoke(ThreadContext *tc, const StaticInstPtr &inst=StaticInst::nullStaticInstPtr)
Definition: faults.cc:54
X86ISA::X86FaultBase::describe
virtual std::string describe() const
Definition: faults.cc:95
FullSystem
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition: root.cc:132
ThreadContext::getProcessPtr
virtual Process * getProcessPtr()=0
X86ISA::MISCREG_EFER
@ MISCREG_EFER
Definition: misc.hh:245
X86ISA::MISCREG_DR1
@ MISCREG_DR1
Definition: misc.hh:125
faults.hh
X86ISA::MISCREG_TR_ATTR
@ MISCREG_TR_ATTR
Definition: misc.hh:373
X86ISA::MISCREG_SEG_SEL
static MiscRegIndex MISCREG_SEG_SEL(int index)
Definition: misc.hh:505
X86ISA::MISCREG_TR_BASE
@ MISCREG_TR_BASE
Definition: misc.hh:323
X86ISA::X86Trap::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=StaticInst::nullStaticInstPtr) override
Definition: faults.cc:106
Loader::debugSymbolTable
SymbolTable debugSymbolTable
Global unified debugging symbol table (for target).
Definition: symtab.cc:47
X86ISA::MISCREG_CR4
@ MISCREG_CR4
Definition: misc.hh:109
X86ISA::index
Bitfield< 5, 3 > index
Definition: types.hh:93
ArmISA::ss
Bitfield< 21 > ss
Definition: miscregs_types.hh:56
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
X86ISA::MISCREG_RFLAGS
@ MISCREG_RFLAGS
Definition: misc.hh:134
X86ISA::MISCREG_DR7
@ MISCREG_DR7
Definition: misc.hh:131
process.hh
X86ISA::RealMode
@ RealMode
Definition: types.hh:194
X86ISA::MISCREG_CS_EFF_BASE
@ MISCREG_CS_EFF_BASE
Definition: misc.hh:331
X86ISA::InitInterrupt::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=StaticInst::nullStaticInstPtr) override
Definition: faults.cc:183
isa_traits.hh
X86ISA::INTREG_MICRO
static IntRegIndex INTREG_MICRO(int index)
Definition: int.hh:154
X86ISA::PCState
Definition: types.hh:287
X86ISA::PageFault::describe
virtual std::string describe() const
Definition: faults.cc:175
X86ISA::MISCREG_SEG_ATTR
static MiscRegIndex MISCREG_SEG_ATTR(int index)
Definition: misc.hh:533
X86ISA
This is exposed globally, independent of the ISA.
Definition: acpi.hh:55
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
X86ISA::MISCREG_CS_ATTR
@ MISCREG_CS_ATTR
Definition: misc.hh:363
X86ISA::MISCREG_DR0
@ MISCREG_DR0
Definition: misc.hh:124
X86ISA::MISCREG_DR3
@ MISCREG_DR3
Definition: misc.hh:127
ThreadContext::getITBPtr
virtual BaseTLB * getITBPtr()=0
full_system.hh
X86ISA::MISCREG_FTW
@ MISCREG_FTW
Definition: misc.hh:383
X86ISA::MISCREG_CR3
@ MISCREG_CR3
Definition: misc.hh:108
ThreadContext::pcState
virtual TheISA::PCState pcState() const =0
X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:79
X86ISA::MISCREG_DR2
@ MISCREG_DR2
Definition: misc.hh:126
X86ISA::MISCREG_SEG_EFF_BASE
static MiscRegIndex MISCREG_SEG_EFF_BASE(int index)
Definition: misc.hh:519
BaseTLB::demapPage
virtual void demapPage(Addr vaddr, uint64_t asn)=0
X86ISA::MISCREG_DR6
@ MISCREG_DR6
Definition: misc.hh:130
X86ISA::X86FaultBase::errorCode
uint64_t errorCode
Definition: faults.hh:58
X86ISA::NUM_SEGMENTREGS
@ NUM_SEGMENTREGS
Definition: segment.hh:62
GenericISA::UPCState::nupc
MicroPC nupc() const
Definition: types.hh:218
StaticInst::machInst
const ExtMachInst machInst
The binary machine instruction.
Definition: static_inst.hh:243
X86ISA::PageFault::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=StaticInst::nullStaticInstPtr)
Definition: faults.cc:136
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:121
ArmISA::NUM_INTREGS
@ NUM_INTREGS
Definition: intregs.hh:123
X86ISA::MISCREG_CS_LIMIT
@ MISCREG_CS_LIMIT
Definition: misc.hh:347
ThreadContext::readMiscReg
virtual RegVal readMiscReg(RegIndex misc_reg)=0
X86ISA::MISCREG_CR0
@ MISCREG_CR0
Definition: misc.hh:105
X86ISA::X86Abort::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=StaticInst::nullStaticInstPtr) override
Definition: faults.cc:119
ThreadContext::setMiscReg
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:127
X86ISA::MISCREG_TSL_LIMIT
@ MISCREG_TSL_LIMIT
Definition: misc.hh:353
X86ISA::MISCREG_TR
@ MISCREG_TR
Definition: misc.hh:307
GenericISA::SimplePCState::pc
Addr pc() const
Definition: types.hh:158
X86ISA::MISCREG_IDTR_BASE
@ MISCREG_IDTR_BASE
Definition: misc.hh:324
X86ISA::InvalidOpcode::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=StaticInst::nullStaticInstPtr) override
Definition: faults.cc:125
X86ISA::MISCREG_SEG_BASE
static MiscRegIndex MISCREG_SEG_BASE(int index)
Definition: misc.hh:512
Process::fixupFault
bool fixupFault(Addr vaddr)
Attempt to fix up a fault at vaddr by allocating a page on the stack.
Definition: process.cc:348
RefCountingPtr< StaticInst >
X86ISA::MISCREG_IDTR_LIMIT
@ MISCREG_IDTR_LIMIT
Definition: misc.hh:358
trace.hh
X86ISA::MISCREG_MXCSR
@ MISCREG_MXCSR
Definition: misc.hh:380
symtab.hh
X86ISA::MISCREG_TSL_BASE
@ MISCREG_TSL_BASE
Definition: misc.hh:319
MicroPC
uint16_t MicroPC
Definition: types.hh:144
X86ISA::X86FaultBase::invoke
void invoke(ThreadContext *tc, const StaticInstPtr &inst=StaticInst::nullStaticInstPtr) override
Definition: faults.cc:56
X86ISA::X86FaultBase::isSoft
virtual bool isSoft()
Definition: faults.hh:69
X86ISA::pc
Bitfield< 19 > pc
Definition: misc.hh:805
X86ISA::MISCREG_CS_BASE
@ MISCREG_CS_BASE
Definition: misc.hh:313
X86ISA::MISCREG_TSL_ATTR
@ MISCREG_TSL_ATTR
Definition: misc.hh:369
thread_context.hh
X86ISA::seg
Bitfield< 2, 0 > seg
Definition: types.hh:82
ThreadContext::getDTBPtr
virtual BaseTLB * getDTBPtr()=0
X86ISA::MISCREG_TSG_BASE
@ MISCREG_TSG_BASE
Definition: misc.hh:320
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171

Generated on Wed Sep 30 2020 14:01:59 for gem5 by doxygen 1.8.17