2 * Copyright (c) 2003-2006, 2008 The Regents of The University of Michigan
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.
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.
32 * Copyright (c) 2007-2008 The Hewlett-Packard Development Company
33 * All rights reserved.
35 * Redistribution and use of this software in source and binary forms,
36 * with or without modification, are permitted provided that the
37 * following conditions are met:
39 * The software must be used only for Non-Commercial Use which means any
40 * use which is NOT directed to receiving any direct monetary
41 * compensation for, or commercial advantage from such use. Illustrative
42 * examples of non-commercial use are academic research, personal study,
43 * teaching, education and corporate research & development.
44 * Illustrative examples of commercial use are distributing products for
45 * commercial advantage and providing services using the software for
46 * commercial advantage.
48 * If you wish to use this software or functionality therein that may be
49 * covered by patents for commercial use, please contact:
50 * Director of Intellectual Property Licensing
51 * Office of Strategy and Technology
52 * Hewlett-Packard Company
54 * Palo Alto, California 94304
56 * Redistributions of source code must retain the above copyright notice,
57 * this list of conditions and the following disclaimer. Redistributions
58 * in binary form must reproduce the above copyright notice, this list of
59 * conditions and the following disclaimer in the documentation and/or
60 * other materials provided with the distribution. Neither the name of
61 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
62 * contributors may be used to endorse or promote products derived from
63 * this software without specific prior written permission. No right of
64 * sublicense is granted herewith. Derivatives of the software and
65 * output created using the software may be prepared, but only for
66 * Non-Commercial Uses. Derivatives of the software may be shared with
67 * others provided: (i) the others agree to abide by the list of
68 * conditions herein which includes the Non-Commercial Use restrictions;
69 * and (ii) such Derivatives of the software include the above copyright
70 * notice to acknowledge the contribution from this software where
71 * applicable, this list of conditions and the disclaimer below.
73 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
74 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
75 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
76 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
77 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
78 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
79 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
80 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
81 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
82 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
83 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88 #include "arch/x86/miscregfile.hh"
89 #include "arch/x86/tlb.hh"
90 #include "cpu/base.hh"
91 #include "cpu/thread_context.hh"
92 #include "sim/serialize.hh"
94 using namespace X86ISA
;
99 void MiscRegFile::updateHandyM5Reg(Efer efer
, CR0 cr0
,
100 SegAttr csAttr
, SegAttr ssAttr
, RFLAGS rflags
)
104 m5reg
.mode
= LongMode
;
106 m5reg
.submode
= SixtyFourBitMode
;
108 m5reg
.submode
= CompatabilityMode
;
110 m5reg
.mode
= LegacyMode
;
113 m5reg
.submode
= Virtual8086Mode
;
115 m5reg
.submode
= ProtectedMode
;
117 m5reg
.submode
= RealMode
;
120 m5reg
.cpl
= csAttr
.dpl
;
121 m5reg
.paging
= cr0
.pg
;
124 // Compute the default and alternate operand size.
125 if (m5reg
.submode
== SixtyFourBitMode
|| csAttr
.defaultSize
) {
133 // Compute the default and alternate address size.
134 if (m5reg
.submode
== SixtyFourBitMode
) {
137 } else if (csAttr
.defaultSize
) {
145 // Compute the stack size
146 if (m5reg
.submode
== SixtyFourBitMode
) {
148 } else if (ssAttr
.defaultSize
) {
154 regVal
[MISCREG_M5_REG
] = m5reg
;
157 void MiscRegFile::clear()
159 // Blank everything. 0 might not be an appropriate value for some things,
160 // but it is for most.
161 memset(regVal
, 0, NumMiscRegs
* sizeof(MiscReg
));
162 regVal
[MISCREG_DR6
] = (mask(8) << 4) | (mask(16) << 16);
163 regVal
[MISCREG_DR7
] = 1 << 10;
166 MiscReg
MiscRegFile::readRegNoEffect(MiscRegIndex miscReg
)
168 // Make sure we're not dealing with an illegal control register.
169 // Instructions should filter out these indexes, and nothing else should
170 // attempt to read them directly.
171 assert( miscReg
!= MISCREG_CR1
&&
172 !(miscReg
> MISCREG_CR4
&&
173 miscReg
< MISCREG_CR8
) &&
174 !(miscReg
> MISCREG_CR8
&&
175 miscReg
<= MISCREG_CR15
));
177 return regVal
[miscReg
];
180 MiscReg
MiscRegFile::readReg(MiscRegIndex miscReg
, ThreadContext
* tc
)
182 if (miscReg
== MISCREG_TSC
) {
183 return regVal
[MISCREG_TSC
] + tc
->getCpuPtr()->curCycle();
185 return readRegNoEffect(miscReg
);
188 void MiscRegFile::setRegNoEffect(MiscRegIndex miscReg
, const MiscReg
&val
)
190 // Make sure we're not dealing with an illegal control register.
191 // Instructions should filter out these indexes, and nothing else should
192 // attempt to write to them directly.
193 assert( miscReg
!= MISCREG_CR1
&&
194 !(miscReg
> MISCREG_CR4
&&
195 miscReg
< MISCREG_CR8
) &&
196 !(miscReg
> MISCREG_CR8
&&
197 miscReg
<= MISCREG_CR15
));
198 regVal
[miscReg
] = val
;
201 void MiscRegFile::setReg(MiscRegIndex miscReg
,
202 const MiscReg
&val
, ThreadContext
* tc
)
204 MiscReg newVal
= val
;
209 CR0 toggled
= regVal
[miscReg
] ^ val
;
211 Efer efer
= regVal
[MISCREG_EFER
];
212 if (toggled
.pg
&& efer
.lme
) {
214 //Turning on long mode
216 regVal
[MISCREG_EFER
] = efer
;
218 //Turning off long mode
220 regVal
[MISCREG_EFER
] = efer
;
224 tc
->getITBPtr()->invalidateAll();
225 tc
->getDTBPtr()->invalidateAll();
227 //This must always be 1.
230 updateHandyM5Reg(regVal
[MISCREG_EFER
],
232 regVal
[MISCREG_CS_ATTR
],
233 regVal
[MISCREG_SS_ATTR
],
234 regVal
[MISCREG_RFLAGS
]);
240 tc
->getITBPtr()->invalidateNonGlobal();
241 tc
->getDTBPtr()->invalidateNonGlobal();
245 CR4 toggled
= regVal
[miscReg
] ^ val
;
246 if (toggled
.pae
|| toggled
.pse
|| toggled
.pge
) {
247 tc
->getITBPtr()->invalidateAll();
248 tc
->getDTBPtr()->invalidateAll();
254 case MISCREG_CS_ATTR
:
256 SegAttr toggled
= regVal
[miscReg
] ^ val
;
257 SegAttr newCSAttr
= val
;
258 if (toggled
.longMode
) {
259 if (newCSAttr
.longMode
) {
260 regVal
[MISCREG_ES_EFF_BASE
] = 0;
261 regVal
[MISCREG_CS_EFF_BASE
] = 0;
262 regVal
[MISCREG_SS_EFF_BASE
] = 0;
263 regVal
[MISCREG_DS_EFF_BASE
] = 0;
265 regVal
[MISCREG_ES_EFF_BASE
] = regVal
[MISCREG_ES_BASE
];
266 regVal
[MISCREG_CS_EFF_BASE
] = regVal
[MISCREG_CS_BASE
];
267 regVal
[MISCREG_SS_EFF_BASE
] = regVal
[MISCREG_SS_BASE
];
268 regVal
[MISCREG_DS_EFF_BASE
] = regVal
[MISCREG_DS_BASE
];
271 updateHandyM5Reg(regVal
[MISCREG_EFER
],
274 regVal
[MISCREG_SS_ATTR
],
275 regVal
[MISCREG_RFLAGS
]);
278 case MISCREG_SS_ATTR
:
279 updateHandyM5Reg(regVal
[MISCREG_EFER
],
281 regVal
[MISCREG_CS_ATTR
],
283 regVal
[MISCREG_RFLAGS
]);
285 // These segments always actually use their bases, or in other words
286 // their effective bases must stay equal to their actual bases.
287 case MISCREG_FS_BASE
:
288 case MISCREG_GS_BASE
:
289 case MISCREG_HS_BASE
:
290 case MISCREG_TSL_BASE
:
291 case MISCREG_TSG_BASE
:
292 case MISCREG_TR_BASE
:
293 case MISCREG_IDTR_BASE
:
294 regVal
[MISCREG_SEG_EFF_BASE(miscReg
- MISCREG_SEG_BASE_BASE
)] = val
;
296 // These segments ignore their bases in 64 bit mode.
297 // their effective bases must stay equal to their actual bases.
298 case MISCREG_ES_BASE
:
299 case MISCREG_CS_BASE
:
300 case MISCREG_SS_BASE
:
301 case MISCREG_DS_BASE
:
303 Efer efer
= regVal
[MISCREG_EFER
];
304 SegAttr csAttr
= regVal
[MISCREG_CS_ATTR
];
305 if (!efer
.lma
|| !csAttr
.longMode
) // Check for non 64 bit mode.
306 regVal
[MISCREG_SEG_EFF_BASE(miscReg
-
307 MISCREG_SEG_BASE_BASE
)] = val
;
311 regVal
[MISCREG_TSC
] = val
- tc
->getCpuPtr()->curCycle();
317 /* These should eventually set up breakpoints. */
320 miscReg
= MISCREG_DR6
;
321 /* Fall through to have the same effects as DR6. */
324 DR6 dr6
= regVal
[MISCREG_DR6
];
337 miscReg
= MISCREG_DR7
;
338 /* Fall through to have the same effects as DR7. */
341 DR7 dr7
= regVal
[MISCREG_DR7
];
345 if (dr7
.l0
|| dr7
.g0
) {
346 panic("Debug register breakpoints not implemented.\n");
348 /* Disable breakpoint 0. */
352 if (dr7
.l1
|| dr7
.g1
) {
353 panic("Debug register breakpoints not implemented.\n");
355 /* Disable breakpoint 1. */
359 if (dr7
.l2
|| dr7
.g2
) {
360 panic("Debug register breakpoints not implemented.\n");
362 /* Disable breakpoint 2. */
366 if (dr7
.l3
|| dr7
.g3
) {
367 panic("Debug register breakpoints not implemented.\n");
369 /* Disable breakpoint 3. */
372 dr7
.rw0
= newDR7
.rw0
;
373 dr7
.len0
= newDR7
.len0
;
374 dr7
.rw1
= newDR7
.rw1
;
375 dr7
.len1
= newDR7
.len1
;
376 dr7
.rw2
= newDR7
.rw2
;
377 dr7
.len2
= newDR7
.len2
;
378 dr7
.rw3
= newDR7
.rw3
;
379 dr7
.len3
= newDR7
.len3
;
383 // Writing anything to the m5reg with side effects makes it update
384 // based on the current values of the relevant registers. The actual
385 // value written is discarded.
386 updateHandyM5Reg(regVal
[MISCREG_EFER
],
388 regVal
[MISCREG_CS_ATTR
],
389 regVal
[MISCREG_SS_ATTR
],
390 regVal
[MISCREG_RFLAGS
]);
395 setRegNoEffect(miscReg
, newVal
);
398 void MiscRegFile::serialize(std::ostream
& os
)
400 SERIALIZE_ARRAY(regVal
, NumMiscRegs
);
403 void MiscRegFile::unserialize(Checkpoint
* cp
, const std::string
& section
)
405 UNSERIALIZE_ARRAY(regVal
, NumMiscRegs
);