gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Gabe Black
29  */
30 
31 #include "arch/x86/isa.hh"
32 
33 #include "arch/x86/decoder.hh"
34 #include "arch/x86/tlb.hh"
35 #include "cpu/base.hh"
36 #include "cpu/thread_context.hh"
37 #include "params/X86ISA.hh"
38 #include "sim/serialize.hh"
39 
40 namespace X86ISA
41 {
42 
43 void
44 ISA::updateHandyM5Reg(Efer efer, CR0 cr0,
45  SegAttr csAttr, SegAttr ssAttr, RFLAGS rflags,
46  ThreadContext *tc)
47 {
48  HandyM5Reg m5reg = 0;
49  if (efer.lma) {
50  m5reg.mode = LongMode;
51  if (csAttr.longMode)
52  m5reg.submode = SixtyFourBitMode;
53  else
54  m5reg.submode = CompatabilityMode;
55  } else {
56  m5reg.mode = LegacyMode;
57  if (cr0.pe) {
58  if (rflags.vm)
59  m5reg.submode = Virtual8086Mode;
60  else
61  m5reg.submode = ProtectedMode;
62  } else {
63  m5reg.submode = RealMode;
64  }
65  }
66  m5reg.cpl = csAttr.dpl;
67  m5reg.paging = cr0.pg;
68  m5reg.prot = cr0.pe;
69 
70  // Compute the default and alternate operand size.
71  if (m5reg.submode == SixtyFourBitMode || csAttr.defaultSize) {
72  m5reg.defOp = 2;
73  m5reg.altOp = 1;
74  } else {
75  m5reg.defOp = 1;
76  m5reg.altOp = 2;
77  }
78 
79  // Compute the default and alternate address size.
80  if (m5reg.submode == SixtyFourBitMode) {
81  m5reg.defAddr = 3;
82  m5reg.altAddr = 2;
83  } else if (csAttr.defaultSize) {
84  m5reg.defAddr = 2;
85  m5reg.altAddr = 1;
86  } else {
87  m5reg.defAddr = 1;
88  m5reg.altAddr = 2;
89  }
90 
91  // Compute the stack size
92  if (m5reg.submode == SixtyFourBitMode) {
93  m5reg.stack = 3;
94  } else if (ssAttr.defaultSize) {
95  m5reg.stack = 2;
96  } else {
97  m5reg.stack = 1;
98  }
99 
100  regVal[MISCREG_M5_REG] = m5reg;
101  if (tc)
102  tc->getDecoderPtr()->setM5Reg(m5reg);
103 }
104 
105 void
107 {
108  // Blank everything. 0 might not be an appropriate value for some things,
109  // but it is for most.
110  memset(regVal, 0, NumMiscRegs * sizeof(RegVal));
111 
112  // If some state should be non-zero after a reset, set those values here.
113  regVal[MISCREG_CR0] = 0x0000000060000010ULL;
114 
115  regVal[MISCREG_MTRRCAP] = 0x0508;
116 
117  regVal[MISCREG_MCG_CAP] = 0x104;
118 
119  regVal[MISCREG_PAT] = 0x0007040600070406ULL;
120 
121  regVal[MISCREG_SYSCFG] = 0x20601;
122 
123  regVal[MISCREG_TOP_MEM] = 0x4000000;
124 
125  regVal[MISCREG_DR6] = (mask(8) << 4) | (mask(16) << 16);
126  regVal[MISCREG_DR7] = 1 << 10;
127 
128  LocalApicBase lApicBase = 0;
129  lApicBase.base = 0xFEE00000 >> 12;
130  lApicBase.enable = 1;
131  // The "bsp" bit will be set when this register is read, since then we'll
132  // have a ThreadContext to check the contextId from.
133  regVal[MISCREG_APIC_BASE] = lApicBase;
134 }
135 
137 {
138  clear();
139 }
140 
141 const X86ISAParams *
142 ISA::params() const
143 {
144  return dynamic_cast<const Params *>(_params);
145 }
146 
147 RegVal
148 ISA::readMiscRegNoEffect(int miscReg) const
149 {
150  // Make sure we're not dealing with an illegal control register.
151  // Instructions should filter out these indexes, and nothing else should
152  // attempt to read them directly.
153  assert(isValidMiscReg(miscReg));
154 
155  return regVal[miscReg];
156 }
157 
158 RegVal
159 ISA::readMiscReg(int miscReg, ThreadContext * tc)
160 {
161  if (miscReg == MISCREG_TSC) {
162  return regVal[MISCREG_TSC] + tc->getCpuPtr()->curCycle();
163  }
164 
165  if (miscReg == MISCREG_FSW) {
166  RegVal fsw = regVal[MISCREG_FSW];
168  return insertBits(fsw, 13, 11, top);
169  }
170 
171  if (miscReg == MISCREG_APIC_BASE) {
172  LocalApicBase base = regVal[MISCREG_APIC_BASE];
173  base.bsp = (tc->contextId() == 0);
174  return base;
175  }
176 
177  return readMiscRegNoEffect(miscReg);
178 }
179 
180 void
182 {
183  // Make sure we're not dealing with an illegal control register.
184  // Instructions should filter out these indexes, and nothing else should
185  // attempt to write to them directly.
186  assert(isValidMiscReg(miscReg));
187 
188  HandyM5Reg m5Reg = regVal[MISCREG_M5_REG];
189  int reg_width = 64;
190  switch (miscReg) {
191  case MISCREG_X87_TOP:
192  reg_width = 3;
193  break;
194  case MISCREG_FTW:
195  reg_width = 8;
196  break;
197  case MISCREG_FSW:
198  case MISCREG_FCW:
199  case MISCREG_FOP:
200  reg_width = 16;
201  break;
202  case MISCREG_MXCSR:
203  reg_width = 32;
204  break;
205  case MISCREG_FISEG:
206  case MISCREG_FOSEG:
207  if (m5Reg.submode != SixtyFourBitMode)
208  reg_width = 16;
209  break;
210  case MISCREG_FIOFF:
211  case MISCREG_FOOFF:
212  if (m5Reg.submode != SixtyFourBitMode)
213  reg_width = 32;
214  break;
215  default:
216  break;
217  }
218 
219  regVal[miscReg] = val & mask(reg_width);
220 }
221 
222 void
224 {
225  RegVal newVal = val;
226  switch(miscReg)
227  {
228  case MISCREG_CR0:
229  {
230  CR0 toggled = regVal[miscReg] ^ val;
231  CR0 newCR0 = val;
232  Efer efer = regVal[MISCREG_EFER];
233  if (toggled.pg && efer.lme) {
234  if (newCR0.pg) {
235  //Turning on long mode
236  efer.lma = 1;
237  regVal[MISCREG_EFER] = efer;
238  } else {
239  //Turning off long mode
240  efer.lma = 0;
241  regVal[MISCREG_EFER] = efer;
242  }
243  }
244  if (toggled.pg) {
245  dynamic_cast<TLB *>(tc->getITBPtr())->flushAll();
246  dynamic_cast<TLB *>(tc->getDTBPtr())->flushAll();
247  }
248  //This must always be 1.
249  newCR0.et = 1;
250  newVal = newCR0;
252  newCR0,
256  tc);
257  }
258  break;
259  case MISCREG_CR2:
260  break;
261  case MISCREG_CR3:
262  dynamic_cast<TLB *>(tc->getITBPtr())->flushNonGlobal();
263  dynamic_cast<TLB *>(tc->getDTBPtr())->flushNonGlobal();
264  break;
265  case MISCREG_CR4:
266  {
267  CR4 toggled = regVal[miscReg] ^ val;
268  if (toggled.pae || toggled.pse || toggled.pge) {
269  dynamic_cast<TLB *>(tc->getITBPtr())->flushAll();
270  dynamic_cast<TLB *>(tc->getDTBPtr())->flushAll();
271  }
272  }
273  break;
274  case MISCREG_CR8:
275  break;
276  case MISCREG_CS_ATTR:
277  {
278  SegAttr toggled = regVal[miscReg] ^ val;
279  SegAttr newCSAttr = val;
280  if (toggled.longMode) {
281  if (newCSAttr.longMode) {
286  } else {
291  }
292  }
295  newCSAttr,
298  tc);
299  }
300  break;
301  case MISCREG_SS_ATTR:
305  val,
307  tc);
308  break;
309  // These segments always actually use their bases, or in other words
310  // their effective bases must stay equal to their actual bases.
311  case MISCREG_FS_BASE:
312  case MISCREG_GS_BASE:
313  case MISCREG_HS_BASE:
314  case MISCREG_TSL_BASE:
315  case MISCREG_TSG_BASE:
316  case MISCREG_TR_BASE:
317  case MISCREG_IDTR_BASE:
319  break;
320  // These segments ignore their bases in 64 bit mode.
321  // their effective bases must stay equal to their actual bases.
322  case MISCREG_ES_BASE:
323  case MISCREG_CS_BASE:
324  case MISCREG_SS_BASE:
325  case MISCREG_DS_BASE:
326  {
327  Efer efer = regVal[MISCREG_EFER];
328  SegAttr csAttr = regVal[MISCREG_CS_ATTR];
329  if (!efer.lma || !csAttr.longMode) // Check for non 64 bit mode.
330  regVal[MISCREG_SEG_EFF_BASE(miscReg -
332  }
333  break;
334  case MISCREG_TSC:
335  regVal[MISCREG_TSC] = val - tc->getCpuPtr()->curCycle();
336  return;
337  case MISCREG_DR0:
338  case MISCREG_DR1:
339  case MISCREG_DR2:
340  case MISCREG_DR3:
341  /* These should eventually set up breakpoints. */
342  break;
343  case MISCREG_DR4:
344  miscReg = MISCREG_DR6;
346  case MISCREG_DR6:
347  {
348  DR6 dr6 = regVal[MISCREG_DR6];
349  DR6 newDR6 = val;
350  dr6.b0 = newDR6.b0;
351  dr6.b1 = newDR6.b1;
352  dr6.b2 = newDR6.b2;
353  dr6.b3 = newDR6.b3;
354  dr6.bd = newDR6.bd;
355  dr6.bs = newDR6.bs;
356  dr6.bt = newDR6.bt;
357  newVal = dr6;
358  }
359  break;
360  case MISCREG_DR5:
361  miscReg = MISCREG_DR7;
363  case MISCREG_DR7:
364  {
365  DR7 dr7 = regVal[MISCREG_DR7];
366  DR7 newDR7 = val;
367  dr7.l0 = newDR7.l0;
368  dr7.g0 = newDR7.g0;
369  if (dr7.l0 || dr7.g0) {
370  panic("Debug register breakpoints not implemented.\n");
371  } else {
372  /* Disable breakpoint 0. */
373  }
374  dr7.l1 = newDR7.l1;
375  dr7.g1 = newDR7.g1;
376  if (dr7.l1 || dr7.g1) {
377  panic("Debug register breakpoints not implemented.\n");
378  } else {
379  /* Disable breakpoint 1. */
380  }
381  dr7.l2 = newDR7.l2;
382  dr7.g2 = newDR7.g2;
383  if (dr7.l2 || dr7.g2) {
384  panic("Debug register breakpoints not implemented.\n");
385  } else {
386  /* Disable breakpoint 2. */
387  }
388  dr7.l3 = newDR7.l3;
389  dr7.g3 = newDR7.g3;
390  if (dr7.l3 || dr7.g3) {
391  panic("Debug register breakpoints not implemented.\n");
392  } else {
393  /* Disable breakpoint 3. */
394  }
395  dr7.gd = newDR7.gd;
396  dr7.rw0 = newDR7.rw0;
397  dr7.len0 = newDR7.len0;
398  dr7.rw1 = newDR7.rw1;
399  dr7.len1 = newDR7.len1;
400  dr7.rw2 = newDR7.rw2;
401  dr7.len2 = newDR7.len2;
402  dr7.rw3 = newDR7.rw3;
403  dr7.len3 = newDR7.len3;
404  }
405  break;
406  case MISCREG_M5_REG:
407  // Writing anything to the m5reg with side effects makes it update
408  // based on the current values of the relevant registers. The actual
409  // value written is discarded.
410  updateHandyM5Reg(regVal[MISCREG_EFER],
411  regVal[MISCREG_CR0],
412  regVal[MISCREG_CS_ATTR],
414  regVal[MISCREG_RFLAGS],
415  tc);
416  return;
417  default:
418  break;
419  }
420  setMiscRegNoEffect(miscReg, newVal);
421 }
422 
423 void
425 {
427 }
428 
429 void
431 {
438  NULL);
439 }
440 
441 void
443 {
444  tc->getDecoderPtr()->setM5Reg(regVal[MISCREG_M5_REG]);
445 }
446 
447 }
448 
449 X86ISA::ISA *
450 X86ISAParams::create()
451 {
452  return new X86ISA::ISA(this);
453 }
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
RegVal readMiscReg(int miscReg, ThreadContext *tc)
Definition: isa.cc:159
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: isa.cc:424
virtual TheISA::Decoder * getDecoderPtr()=0
Definition: test.h:61
virtual BaseTLB * getDTBPtr()=0
const Params * params() const
Definition: isa.cc:142
void updateHandyM5Reg(Efer efer, CR0 cr0, SegAttr csAttr, SegAttr ssAttr, RFLAGS rflags, ThreadContext *tc)
Definition: isa.cc:44
uint64_t RegVal
Definition: types.hh:168
virtual BaseCPU * getCpuPtr()=0
Definition: cprintf.cc:42
static bool isValidMiscReg(int index)
Definition: misc.hh:404
ThreadContext is the external interface to all thread state for anything outside of the CPU...
Bitfield< 63 > val
Definition: misc.hh:771
void clear()
Definition: isa.cc:106
void setMiscReg(int miscReg, RegVal val, ThreadContext *tc)
Definition: isa.cc:223
RegVal readMiscRegNoEffect(int miscReg) const
Definition: isa.cc:148
#define M5_FALLTHROUGH
Definition: compiler.hh:86
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: isa.cc:430
mask
Definition: misc.hh:798
void setMiscRegNoEffect(int miscReg, RegVal val)
Definition: isa.cc:181
Bitfield< 51, 12 > base
Definition: pagetable.hh:142
#define SERIALIZE_ARRAY(member, size)
Definition: serialize.hh:658
virtual BaseTLB * getITBPtr()=0
Cycles curCycle() const
Determine the current cycle, corresponding to a tick aligned to a clock edge.
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:132
#define ULL(N)
uint64_t constant
Definition: types.hh:50
RegVal regVal[NUM_MISCREGS]
Definition: isa.hh:55
#define UNSERIALIZE_ARRAY(member, size)
Definition: serialize.hh:661
std::ostream CheckpointOut
Definition: serialize.hh:68
This is exposed globally, independent of the ISA.
Definition: acpi.hh:57
const SimObjectParams * _params
Cached copy of the object parameters.
Definition: sim_object.hh:110
virtual ContextID contextId() const =0
static MiscRegIndex MISCREG_SEG_EFF_BASE(int index)
Definition: misc.hh:521
Definition: isa.hh:35
Bitfield< 0 > p
Definition: pagetable.hh:152
X86ISAParams Params
Definition: isa.hh:61
ISA(Params *p)
Definition: isa.cc:136
const int NumMiscRegs
Definition: registers.hh:57
virtual void startup()
startup() is the final initialization call before simulation.
Definition: sim_object.cc:99

Generated on Fri Feb 28 2020 16:26:56 for gem5 by doxygen 1.8.13