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

Generated on Mon Jun 8 2020 15:34:38 for gem5 by doxygen 1.8.13