gem5  v20.1.0.0
isa.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "arch/x86/isa.hh"
30 
31 #include "arch/x86/decoder.hh"
32 #include "arch/x86/tlb.hh"
33 #include "cpu/base.hh"
34 #include "cpu/thread_context.hh"
35 #include "params/X86ISA.hh"
36 #include "sim/serialize.hh"
37 
38 namespace X86ISA
39 {
40 
41 void
42 ISA::updateHandyM5Reg(Efer efer, CR0 cr0,
43  SegAttr csAttr, SegAttr ssAttr, RFLAGS rflags)
44 {
45  HandyM5Reg m5reg = 0;
46  if (efer.lma) {
47  m5reg.mode = LongMode;
48  if (csAttr.longMode)
49  m5reg.submode = SixtyFourBitMode;
50  else
51  m5reg.submode = CompatabilityMode;
52  } else {
53  m5reg.mode = LegacyMode;
54  if (cr0.pe) {
55  if (rflags.vm)
56  m5reg.submode = Virtual8086Mode;
57  else
58  m5reg.submode = ProtectedMode;
59  } else {
60  m5reg.submode = RealMode;
61  }
62  }
63  m5reg.cpl = csAttr.dpl;
64  m5reg.paging = cr0.pg;
65  m5reg.prot = cr0.pe;
66 
67  // Compute the default and alternate operand size.
68  if (m5reg.submode == SixtyFourBitMode || csAttr.defaultSize) {
69  m5reg.defOp = 2;
70  m5reg.altOp = 1;
71  } else {
72  m5reg.defOp = 1;
73  m5reg.altOp = 2;
74  }
75 
76  // Compute the default and alternate address size.
77  if (m5reg.submode == SixtyFourBitMode) {
78  m5reg.defAddr = 3;
79  m5reg.altAddr = 2;
80  } else if (csAttr.defaultSize) {
81  m5reg.defAddr = 2;
82  m5reg.altAddr = 1;
83  } else {
84  m5reg.defAddr = 1;
85  m5reg.altAddr = 2;
86  }
87 
88  // Compute the stack size
89  if (m5reg.submode == SixtyFourBitMode) {
90  m5reg.stack = 3;
91  } else if (ssAttr.defaultSize) {
92  m5reg.stack = 2;
93  } else {
94  m5reg.stack = 1;
95  }
96 
97  regVal[MISCREG_M5_REG] = m5reg;
98  if (tc)
99  tc->getDecoderPtr()->setM5Reg(m5reg);
100 }
101 
102 void
104 {
105  // Blank everything. 0 might not be an appropriate value for some things,
106  // but it is for most.
107  memset(regVal, 0, NumMiscRegs * sizeof(RegVal));
108 
109  // If some state should be non-zero after a reset, set those values here.
110  regVal[MISCREG_CR0] = 0x0000000060000010ULL;
111 
112  regVal[MISCREG_MTRRCAP] = 0x0508;
113 
114  regVal[MISCREG_MCG_CAP] = 0x104;
115 
116  regVal[MISCREG_PAT] = 0x0007040600070406ULL;
117 
118  regVal[MISCREG_SYSCFG] = 0x20601;
119 
120  regVal[MISCREG_TOP_MEM] = 0x4000000;
121 
122  regVal[MISCREG_DR6] = (mask(8) << 4) | (mask(16) << 16);
123  regVal[MISCREG_DR7] = 1 << 10;
124 
125  LocalApicBase lApicBase = 0;
126  lApicBase.base = 0xFEE00000 >> 12;
127  lApicBase.enable = 1;
128  // The "bsp" bit will be set when this register is read, since then we'll
129  // have a ThreadContext to check the contextId from.
130  regVal[MISCREG_APIC_BASE] = lApicBase;
131 }
132 
134 {
135  clear();
136 }
137 
138 const X86ISAParams *
139 ISA::params() const
140 {
141  return dynamic_cast<const Params *>(_params);
142 }
143 
144 RegVal
145 ISA::readMiscRegNoEffect(int miscReg) const
146 {
147  // Make sure we're not dealing with an illegal control register.
148  // Instructions should filter out these indexes, and nothing else should
149  // attempt to read them directly.
150  assert(isValidMiscReg(miscReg));
151 
152  return regVal[miscReg];
153 }
154 
155 RegVal
156 ISA::readMiscReg(int miscReg)
157 {
158  if (miscReg == MISCREG_TSC) {
159  return regVal[MISCREG_TSC] + tc->getCpuPtr()->curCycle();
160  }
161 
162  if (miscReg == MISCREG_FSW) {
163  RegVal fsw = regVal[MISCREG_FSW];
165  return insertBits(fsw, 13, 11, top);
166  }
167 
168  if (miscReg == MISCREG_APIC_BASE) {
169  LocalApicBase base = regVal[MISCREG_APIC_BASE];
170  base.bsp = (tc->contextId() == 0);
171  return base;
172  }
173 
174  return readMiscRegNoEffect(miscReg);
175 }
176 
177 void
179 {
180  // Make sure we're not dealing with an illegal control register.
181  // Instructions should filter out these indexes, and nothing else should
182  // attempt to write to them directly.
183  assert(isValidMiscReg(miscReg));
184 
185  HandyM5Reg m5Reg = regVal[MISCREG_M5_REG];
186  int reg_width = 64;
187  switch (miscReg) {
188  case MISCREG_X87_TOP:
189  reg_width = 3;
190  break;
191  case MISCREG_FTW:
192  reg_width = 8;
193  break;
194  case MISCREG_FSW:
195  case MISCREG_FCW:
196  case MISCREG_FOP:
197  reg_width = 16;
198  break;
199  case MISCREG_MXCSR:
200  reg_width = 32;
201  break;
202  case MISCREG_FISEG:
203  case MISCREG_FOSEG:
204  if (m5Reg.submode != SixtyFourBitMode)
205  reg_width = 16;
206  break;
207  case MISCREG_FIOFF:
208  case MISCREG_FOOFF:
209  if (m5Reg.submode != SixtyFourBitMode)
210  reg_width = 32;
211  break;
212  default:
213  break;
214  }
215 
216  regVal[miscReg] = val & mask(reg_width);
217 }
218 
219 void
221 {
222  RegVal newVal = val;
223  switch(miscReg)
224  {
225  case MISCREG_CR0:
226  {
227  CR0 toggled = regVal[miscReg] ^ val;
228  CR0 newCR0 = val;
229  Efer efer = regVal[MISCREG_EFER];
230  if (toggled.pg && efer.lme) {
231  if (newCR0.pg) {
232  //Turning on long mode
233  efer.lma = 1;
234  regVal[MISCREG_EFER] = efer;
235  } else {
236  //Turning off long mode
237  efer.lma = 0;
238  regVal[MISCREG_EFER] = efer;
239  }
240  }
241  if (toggled.pg) {
242  dynamic_cast<TLB *>(tc->getITBPtr())->flushAll();
243  dynamic_cast<TLB *>(tc->getDTBPtr())->flushAll();
244  }
245  //This must always be 1.
246  newCR0.et = 1;
247  newVal = newCR0;
249  newCR0,
253  }
254  break;
255  case MISCREG_CR2:
256  break;
257  case MISCREG_CR3:
258  dynamic_cast<TLB *>(tc->getITBPtr())->flushNonGlobal();
259  dynamic_cast<TLB *>(tc->getDTBPtr())->flushNonGlobal();
260  break;
261  case MISCREG_CR4:
262  {
263  CR4 toggled = regVal[miscReg] ^ val;
264  if (toggled.pae || toggled.pse || toggled.pge) {
265  dynamic_cast<TLB *>(tc->getITBPtr())->flushAll();
266  dynamic_cast<TLB *>(tc->getDTBPtr())->flushAll();
267  }
268  }
269  break;
270  case MISCREG_CR8:
271  break;
272  case MISCREG_CS_ATTR:
273  {
274  SegAttr toggled = regVal[miscReg] ^ val;
275  SegAttr newCSAttr = val;
276  if (toggled.longMode) {
277  if (newCSAttr.longMode) {
282  } else {
287  }
288  }
291  newCSAttr,
294  }
295  break;
296  case MISCREG_SS_ATTR:
300  val,
302  break;
303  // These segments always actually use their bases, or in other words
304  // their effective bases must stay equal to their actual bases.
305  case MISCREG_FS_BASE:
306  case MISCREG_GS_BASE:
307  case MISCREG_HS_BASE:
308  case MISCREG_TSL_BASE:
309  case MISCREG_TSG_BASE:
310  case MISCREG_TR_BASE:
311  case MISCREG_IDTR_BASE:
313  break;
314  // These segments ignore their bases in 64 bit mode.
315  // their effective bases must stay equal to their actual bases.
316  case MISCREG_ES_BASE:
317  case MISCREG_CS_BASE:
318  case MISCREG_SS_BASE:
319  case MISCREG_DS_BASE:
320  {
321  Efer efer = regVal[MISCREG_EFER];
322  SegAttr csAttr = regVal[MISCREG_CS_ATTR];
323  if (!efer.lma || !csAttr.longMode) // Check for non 64 bit mode.
324  regVal[MISCREG_SEG_EFF_BASE(miscReg -
326  }
327  break;
328  case MISCREG_TSC:
329  regVal[MISCREG_TSC] = val - tc->getCpuPtr()->curCycle();
330  return;
331  case MISCREG_DR0:
332  case MISCREG_DR1:
333  case MISCREG_DR2:
334  case MISCREG_DR3:
335  /* These should eventually set up breakpoints. */
336  break;
337  case MISCREG_DR4:
338  miscReg = MISCREG_DR6;
340  case MISCREG_DR6:
341  {
342  DR6 dr6 = regVal[MISCREG_DR6];
343  DR6 newDR6 = val;
344  dr6.b0 = newDR6.b0;
345  dr6.b1 = newDR6.b1;
346  dr6.b2 = newDR6.b2;
347  dr6.b3 = newDR6.b3;
348  dr6.bd = newDR6.bd;
349  dr6.bs = newDR6.bs;
350  dr6.bt = newDR6.bt;
351  newVal = dr6;
352  }
353  break;
354  case MISCREG_DR5:
355  miscReg = MISCREG_DR7;
357  case MISCREG_DR7:
358  {
359  DR7 dr7 = regVal[MISCREG_DR7];
360  DR7 newDR7 = val;
361  dr7.l0 = newDR7.l0;
362  dr7.g0 = newDR7.g0;
363  if (dr7.l0 || dr7.g0) {
364  panic("Debug register breakpoints not implemented.\n");
365  } else {
366  /* Disable breakpoint 0. */
367  }
368  dr7.l1 = newDR7.l1;
369  dr7.g1 = newDR7.g1;
370  if (dr7.l1 || dr7.g1) {
371  panic("Debug register breakpoints not implemented.\n");
372  } else {
373  /* Disable breakpoint 1. */
374  }
375  dr7.l2 = newDR7.l2;
376  dr7.g2 = newDR7.g2;
377  if (dr7.l2 || dr7.g2) {
378  panic("Debug register breakpoints not implemented.\n");
379  } else {
380  /* Disable breakpoint 2. */
381  }
382  dr7.l3 = newDR7.l3;
383  dr7.g3 = newDR7.g3;
384  if (dr7.l3 || dr7.g3) {
385  panic("Debug register breakpoints not implemented.\n");
386  } else {
387  /* Disable breakpoint 3. */
388  }
389  dr7.gd = newDR7.gd;
390  dr7.rw0 = newDR7.rw0;
391  dr7.len0 = newDR7.len0;
392  dr7.rw1 = newDR7.rw1;
393  dr7.len1 = newDR7.len1;
394  dr7.rw2 = newDR7.rw2;
395  dr7.len2 = newDR7.len2;
396  dr7.rw3 = newDR7.rw3;
397  dr7.len3 = newDR7.len3;
398  }
399  break;
400  case MISCREG_M5_REG:
401  // Writing anything to the m5reg with side effects makes it update
402  // based on the current values of the relevant registers. The actual
403  // value written is discarded.
409  return;
410  default:
411  break;
412  }
413  setMiscRegNoEffect(miscReg, newVal);
414 }
415 
416 void
418 {
420 }
421 
422 void
424 {
431 }
432 
433 void
435 {
437  tc->getDecoderPtr()->setM5Reg(regVal[MISCREG_M5_REG]);
438 }
439 
440 }
441 
442 X86ISA::ISA *
443 X86ISAParams::create()
444 {
445  return new X86ISA::ISA(this);
446 }
X86ISA::MISCREG_FOSEG
@ MISCREG_FOSEG
Definition: misc.hh:387
X86ISA::MISCREG_M5_REG
@ MISCREG_M5_REG
Definition: misc.hh:137
X86ISA::ISA::setMiscRegNoEffect
void setMiscRegNoEffect(int miscReg, RegVal val)
Definition: isa.cc:178
X86ISA::MISCREG_MCG_CAP
@ MISCREG_MCG_CAP
Definition: misc.hh:151
X86ISA::Virtual8086Mode
@ Virtual8086Mode
Definition: types.hh:193
X86ISA::MISCREG_FOP
@ MISCREG_FOP
Definition: misc.hh:389
X86ISA::MISCREG_APIC_BASE
@ MISCREG_APIC_BASE
Definition: misc.hh:393
X86ISA::MISCREG_GS_BASE
@ MISCREG_GS_BASE
Definition: misc.hh:317
X86ISA::MISCREG_MTRRCAP
@ MISCREG_MTRRCAP
Definition: misc.hh:145
serialize.hh
X86ISA::MISCREG_ES_BASE
@ MISCREG_ES_BASE
Definition: misc.hh:312
X86ISA::MISCREG_X87_TOP
@ MISCREG_X87_TOP
Definition: misc.hh:377
BaseISA::setThreadContext
virtual void setThreadContext(ThreadContext *_tc)
Definition: isa.hh:59
X86ISA::MISCREG_FISEG
@ MISCREG_FISEG
Definition: misc.hh:385
X86ISA::ISA::readMiscReg
RegVal readMiscReg(int miscReg)
Definition: isa.cc:156
X86ISA::ISA
Definition: isa.hh:50
X86ISA::NumMiscRegs
const int NumMiscRegs
Definition: registers.hh:55
X86ISA::ISA::clear
void clear()
Definition: isa.cc:103
X86ISA::CompatabilityMode
@ CompatabilityMode
Definition: types.hh:191
X86ISA::MISCREG_CR2
@ MISCREG_CR2
Definition: misc.hh:107
X86ISA::ISA::updateHandyM5Reg
void updateHandyM5Reg(Efer efer, CR0 cr0, SegAttr csAttr, SegAttr ssAttr, RFLAGS rflags)
Definition: isa.cc:42
X86ISA::MISCREG_ES_EFF_BASE
@ MISCREG_ES_EFF_BASE
Definition: misc.hh:330
top
Definition: test.h:61
X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
X86ISA::ISA::setMiscReg
void setMiscReg(int miscReg, RegVal val)
Definition: isa.cc:220
X86ISA::TLB
Definition: tlb.hh:57
X86ISA::ISA::setThreadContext
void setThreadContext(ThreadContext *_tc) override
Definition: isa.cc:434
X86ISA::MISCREG_EFER
@ MISCREG_EFER
Definition: misc.hh:245
X86ISA::ISA::ISA
ISA(Params *p)
Definition: isa.cc:133
X86ISA::MISCREG_DR1
@ MISCREG_DR1
Definition: misc.hh:125
X86ISA::ISA::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: isa.cc:417
decoder.hh
X86ISA::MISCREG_TR_BASE
@ MISCREG_TR_BASE
Definition: misc.hh:323
X86ISA::MISCREG_DS_EFF_BASE
@ MISCREG_DS_EFF_BASE
Definition: misc.hh:333
X86ISA::ProtectedMode
@ ProtectedMode
Definition: types.hh:192
cp
Definition: cprintf.cc:40
X86ISA::MISCREG_DR5
@ MISCREG_DR5
Definition: misc.hh:129
X86ISA::MISCREG_CR4
@ MISCREG_CR4
Definition: misc.hh:109
X86ISA::MISCREG_SS_ATTR
@ MISCREG_SS_ATTR
Definition: misc.hh:364
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
X86ISA::MISCREG_TOP_MEM
@ MISCREG_TOP_MEM
Definition: misc.hh:283
X86ISA::MISCREG_RFLAGS
@ MISCREG_RFLAGS
Definition: misc.hh:134
BaseISA::tc
ThreadContext * tc
Definition: isa.hh:52
X86ISA::MISCREG_HS_BASE
@ MISCREG_HS_BASE
Definition: misc.hh:318
X86ISA::MISCREG_DR7
@ MISCREG_DR7
Definition: misc.hh:131
X86ISA::MISCREG_DS_BASE
@ MISCREG_DS_BASE
Definition: misc.hh:315
X86ISA::MISCREG_SS_EFF_BASE
@ MISCREG_SS_EFF_BASE
Definition: misc.hh:332
X86ISA::RealMode
@ RealMode
Definition: types.hh:194
X86ISA::MISCREG_CS_EFF_BASE
@ MISCREG_CS_EFF_BASE
Definition: misc.hh:331
X86ISA::isValidMiscReg
static bool isValidMiscReg(int index)
Definition: misc.hh:402
M5_FALLTHROUGH
#define M5_FALLTHROUGH
Definition: compiler.hh:84
ThreadContext::contextId
virtual ContextID contextId() const =0
X86ISA::ISA::regVal
RegVal regVal[NUM_MISCREGS]
Definition: isa.hh:53
SERIALIZE_ARRAY
#define SERIALIZE_ARRAY(member, size)
Definition: serialize.hh:832
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
X86ISA::MISCREG_DR4
@ MISCREG_DR4
Definition: misc.hh:128
X86ISA::ISA::params
const Params * params() const
Definition: isa.cc:139
X86ISA
This is exposed globally, independent of the ISA.
Definition: acpi.hh:55
X86ISA::MISCREG_CS_ATTR
@ MISCREG_CS_ATTR
Definition: misc.hh:363
X86ISA::ISA::Params
X86ISAParams Params
Definition: isa.hh:60
X86ISA::MISCREG_DR0
@ MISCREG_DR0
Definition: misc.hh:124
X86ISA::MISCREG_DR3
@ MISCREG_DR3
Definition: misc.hh:127
ThreadContext::getITBPtr
virtual BaseTLB * getITBPtr()=0
isa.hh
X86ISA::MISCREG_FTW
@ MISCREG_FTW
Definition: misc.hh:383
X86ISA::MISCREG_CR3
@ MISCREG_CR3
Definition: misc.hh:108
X86ISA::MISCREG_SYSCFG
@ MISCREG_SYSCFG
Definition: misc.hh:271
X86ISA::MISCREG_DR2
@ MISCREG_DR2
Definition: misc.hh:126
X86ISA::ISA::readMiscRegNoEffect
RegVal readMiscRegNoEffect(int miscReg) const
Definition: isa.cc:145
X86ISA::MISCREG_SEG_EFF_BASE
static MiscRegIndex MISCREG_SEG_EFF_BASE(int index)
Definition: misc.hh:519
X86ISA::MISCREG_DR6
@ MISCREG_DR6
Definition: misc.hh:130
base.hh
X86ISA::p
Bitfield< 0 > p
Definition: pagetable.hh:151
UNSERIALIZE_ARRAY
#define UNSERIALIZE_ARRAY(member, size)
Definition: serialize.hh:840
SimObject::_params
const SimObjectParams * _params
Cached copy of the object parameters.
Definition: sim_object.hh:110
insertBits
T insertBits(T val, int first, int last, B bit_val)
Returns val with bits first to last set to the LSBs of bit_val.
Definition: bitfield.hh:147
X86ISA::MISCREG_FIOFF
@ MISCREG_FIOFF
Definition: misc.hh:386
X86ISA::MISCREG_CR0
@ MISCREG_CR0
Definition: misc.hh:105
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
ThreadContext::getDecoderPtr
virtual TheISA::Decoder * getDecoderPtr()=0
X86ISA::mask
mask
Definition: misc.hh:796
X86ISA::MISCREG_IDTR_BASE
@ MISCREG_IDTR_BASE
Definition: misc.hh:324
X86ISA::MISCREG_MXCSR
@ MISCREG_MXCSR
Definition: misc.hh:380
X86ISA::SixtyFourBitMode
@ SixtyFourBitMode
Definition: types.hh:190
tlb.hh
X86ISA::MISCREG_TSL_BASE
@ MISCREG_TSL_BASE
Definition: misc.hh:319
X86ISA::MISCREG_PAT
@ MISCREG_PAT
Definition: misc.hh:196
X86ISA::MISCREG_FSW
@ MISCREG_FSW
Definition: misc.hh:382
X86ISA::ISA::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: isa.cc:423
CheckpointIn
Definition: serialize.hh:67
X86ISA::MISCREG_CS_BASE
@ MISCREG_CS_BASE
Definition: misc.hh:313
X86ISA::MISCREG_CR8
@ MISCREG_CR8
Definition: misc.hh:113
ThreadContext::getCpuPtr
virtual BaseCPU * getCpuPtr()=0
X86ISA::MISCREG_SEG_BASE_BASE
@ MISCREG_SEG_BASE_BASE
Definition: misc.hh:311
X86ISA::MISCREG_FOOFF
@ MISCREG_FOOFF
Definition: misc.hh:388
BaseISA
Definition: isa.hh:47
X86ISA::MISCREG_FS_BASE
@ MISCREG_FS_BASE
Definition: misc.hh:316
thread_context.hh
ULL
#define ULL(N)
uint64_t constant
Definition: types.hh:50
X86ISA::MISCREG_TSC
@ MISCREG_TSC
Definition: misc.hh:143
RegVal
uint64_t RegVal
Definition: types.hh:168
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
X86ISA::MISCREG_SS_BASE
@ MISCREG_SS_BASE
Definition: misc.hh:314
X86ISA::MISCREG_FCW
@ MISCREG_FCW
Definition: misc.hh:381

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