2 * Copyright (c) 2007-2008 The Hewlett-Packard Development Company
5 * Redistribution and use of this software in source and binary forms,
6 * with or without modification, are permitted provided that the
7 * following conditions are met:
9 * The software must be used only for Non-Commercial Use which means any
10 * use which is NOT directed to receiving any direct monetary
11 * compensation for, or commercial advantage from such use. Illustrative
12 * examples of non-commercial use are academic research, personal study,
13 * teaching, education and corporate research & development.
14 * Illustrative examples of commercial use are distributing products for
15 * commercial advantage and providing services using the software for
16 * commercial advantage.
18 * If you wish to use this software or functionality therein that may be
19 * covered by patents for commercial use, please contact:
20 * Director of Intellectual Property Licensing
21 * Office of Strategy and Technology
22 * Hewlett-Packard Company
24 * Palo Alto, California 94304
26 * Redistributions of source code must retain the above copyright notice,
27 * this list of conditions and the following disclaimer. Redistributions
28 * in binary form must reproduce the above copyright notice, this list of
29 * conditions and the following disclaimer in the documentation and/or
30 * other materials provided with the distribution. Neither the name of
31 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
32 * contributors may be used to endorse or promote products derived from
33 * this software without specific prior written permission. No right of
34 * sublicense is granted herewith. Derivatives of the software and
35 * output created using the software may be prepared, but only for
36 * Non-Commercial Uses. Derivatives of the software may be shared with
37 * others provided: (i) the others agree to abide by the list of
38 * conditions herein which includes the Non-Commercial Use restrictions;
39 * and (ii) such Derivatives of the software include the above copyright
40 * notice to acknowledge the contribution from this software where
41 * applicable, this list of conditions and the disclaimer below.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include "arch/x86/miscregs.hh"
59 #include "arch/x86/predecoder.hh"
60 #include "base/misc.hh"
61 #include "base/trace.hh"
62 #include "base/types.hh"
63 #include "cpu/thread_context.hh"
67 void Predecoder::doReset()
69 origPC
= basePC
+ offset
;
70 DPRINTF(Predecoder
, "Setting origPC to %#x\n", origPC
);
75 emi
.opcode
.prefixA
= emi
.opcode
.prefixB
= 0;
77 immediateCollected
= 0;
84 m5Reg
= tc
->readMiscRegNoEffect(MISCREG_M5_REG
);
85 emi
.mode
.mode
= m5Reg
.mode
;
86 emi
.mode
.submode
= m5Reg
.submode
;
89 void Predecoder::process()
91 //This function drives the predecoder state machine.
93 //Some sanity checks. You shouldn't try to process more bytes if
94 //there aren't any, and you shouldn't overwrite an already
95 //predecoder ExtMachInst.
99 //While there's still something to do...
100 while(!emiIsReady
&& !outOfBytes
)
102 uint8_t nextByte
= getNextByte();
109 state
= doPrefixState(nextByte
);
112 state
= doOpcodeState(nextByte
);
115 state
= doModRMState(nextByte
);
118 state
= doSIBState(nextByte
);
120 case DisplacementState
:
121 state
= doDisplacementState();
124 state
= doImmediateState();
127 panic("Went to the error state in the predecoder.\n");
129 panic("Unrecognized state! %d\n", state
);
134 //Either get a prefix and record it in the ExtMachInst, or send the
135 //state machine on to get the opcode(s).
136 Predecoder::State
Predecoder::doPrefixState(uint8_t nextByte
)
138 uint8_t prefix
= Prefixes
[nextByte
];
139 State nextState
= PrefixState
;
140 // REX prefixes are only recognized in 64 bit mode.
141 if (prefix
== RexPrefix
&& emi
.mode
.submode
!= SixtyFourBitMode
)
147 //Operand size override prefixes
148 case OperandSizeOverride
:
149 DPRINTF(Predecoder
, "Found operand size override prefix.\n");
150 emi
.legacy
.op
= true;
152 case AddressSizeOverride
:
153 DPRINTF(Predecoder
, "Found address size override prefix.\n");
154 emi
.legacy
.addr
= true;
156 //Segment override prefixes
163 DPRINTF(Predecoder
, "Found segment override.\n");
164 emi
.legacy
.seg
= prefix
;
167 DPRINTF(Predecoder
, "Found lock prefix.\n");
168 emi
.legacy
.lock
= true;
171 DPRINTF(Predecoder
, "Found rep prefix.\n");
172 emi
.legacy
.rep
= true;
175 DPRINTF(Predecoder
, "Found repne prefix.\n");
176 emi
.legacy
.repne
= true;
179 DPRINTF(Predecoder
, "Found Rex prefix %#x.\n", nextByte
);
183 nextState
= OpcodeState
;
186 panic("Unrecognized prefix %#x\n", nextByte
);
191 //Load all the opcodes (currently up to 2) and then figure out
192 //what immediate and/or ModRM is needed.
193 Predecoder::State
Predecoder::doOpcodeState(uint8_t nextByte
)
195 State nextState
= ErrorState
;
197 //We can't handle 3+ byte opcodes right now
198 assert(emi
.opcode
.num
< 4);
200 if(emi
.opcode
.num
== 1 && nextByte
== 0x0f)
202 nextState
= OpcodeState
;
203 DPRINTF(Predecoder
, "Found two byte opcode.\n");
204 emi
.opcode
.prefixA
= nextByte
;
206 else if(emi
.opcode
.num
== 2 && (nextByte
== 0x38 || nextByte
== 0x3F))
208 nextState
= OpcodeState
;
209 DPRINTF(Predecoder
, "Found three byte opcode.\n");
210 emi
.opcode
.prefixB
= nextByte
;
214 DPRINTF(Predecoder
, "Found opcode %#x.\n", nextByte
);
215 emi
.opcode
.op
= nextByte
;
217 //Figure out the effective operand size. This can be overriden to
218 //a fixed value at the decoder level.
221 logOpSize
= 3; // 64 bit operand size
222 else if (emi
.legacy
.op
)
223 logOpSize
= m5Reg
.altOp
;
225 logOpSize
= m5Reg
.defOp
;
227 //Set the actual op size
228 emi
.opSize
= 1 << logOpSize
;
230 //Figure out the effective address size. This can be overriden to
231 //a fixed value at the decoder level.
234 logAddrSize
= m5Reg
.altAddr
;
236 logAddrSize
= m5Reg
.defAddr
;
238 //Set the actual address size
239 emi
.addrSize
= 1 << logAddrSize
;
241 //Figure out the effective stack width. This can be overriden to
242 //a fixed value at the decoder level.
243 emi
.stackSize
= 1 << m5Reg
.stack
;
245 //Figure out how big of an immediate we'll retreive based
247 int immType
= ImmediateType
[emi
.opcode
.num
- 1][nextByte
];
248 if (emi
.opcode
.num
== 1 && nextByte
>= 0xA0 && nextByte
<= 0xA3)
249 immediateSize
= SizeTypeToSize
[logAddrSize
- 1][immType
];
251 immediateSize
= SizeTypeToSize
[logOpSize
- 1][immType
];
253 //Determine what to expect next
254 if (UsesModRM
[emi
.opcode
.num
- 1][nextByte
]) {
255 nextState
= ModRMState
;
258 nextState
= ImmediateState
;
261 nextState
= ResetState
;
268 //Get the ModRM byte and determine what displacement, if any, there is.
269 //Also determine whether or not to get the SIB byte, displacement, or
271 Predecoder::State
Predecoder::doModRMState(uint8_t nextByte
)
273 State nextState
= ErrorState
;
276 DPRINTF(Predecoder
, "Found modrm byte %#x.\n", nextByte
);
277 if (m5Reg
.defOp
== 1) {
278 //figure out 16 bit displacement size
279 if ((modRM
.mod
== 0 && modRM
.rm
== 6) || modRM
.mod
== 2)
280 displacementSize
= 2;
281 else if (modRM
.mod
== 1)
282 displacementSize
= 1;
284 displacementSize
= 0;
286 //figure out 32/64 bit displacement size
287 if ((modRM
.mod
== 0 && modRM
.rm
== 5) || modRM
.mod
== 2)
288 displacementSize
= 4;
289 else if (modRM
.mod
== 1)
290 displacementSize
= 1;
292 displacementSize
= 0;
295 // The "test" instruction in group 3 needs an immediate, even though
296 // the other instructions with the same actual opcode don't.
297 if (emi
.opcode
.num
== 1 && (modRM
.reg
& 0x6) == 0) {
298 if (emi
.opcode
.op
== 0xF6)
300 else if (emi
.opcode
.op
== 0xF7)
301 immediateSize
= (emi
.opSize
== 8) ? 4 : emi
.opSize
;
304 //If there's an SIB, get that next.
305 //There is no SIB in 16 bit mode.
306 if (modRM
.rm
== 4 && modRM
.mod
!= 3) {
307 // && in 32/64 bit mode)
308 nextState
= SIBState
;
309 } else if(displacementSize
) {
310 nextState
= DisplacementState
;
311 } else if(immediateSize
) {
312 nextState
= ImmediateState
;
315 nextState
= ResetState
;
317 //The ModRM byte is consumed no matter what
323 //Get the SIB byte. We don't do anything with it at this point, other
324 //than storing it in the ExtMachInst. Determine if we need to get a
325 //displacement or immediate next.
326 Predecoder::State
Predecoder::doSIBState(uint8_t nextByte
)
328 State nextState
= ErrorState
;
330 DPRINTF(Predecoder
, "Found SIB byte %#x.\n", nextByte
);
332 if (emi
.modRM
.mod
== 0 && emi
.sib
.base
== 5)
333 displacementSize
= 4;
334 if (displacementSize
) {
335 nextState
= DisplacementState
;
336 } else if(immediateSize
) {
337 nextState
= ImmediateState
;
340 nextState
= ResetState
;
345 //Gather up the displacement, or at least as much of it
347 Predecoder::State
Predecoder::doDisplacementState()
349 State nextState
= ErrorState
;
351 getImmediate(immediateCollected
,
355 DPRINTF(Predecoder
, "Collecting %d byte displacement, got %d bytes.\n",
356 displacementSize
, immediateCollected
);
358 if(displacementSize
== immediateCollected
) {
359 //Reset this for other immediates.
360 immediateCollected
= 0;
361 //Sign extend the displacement
362 switch(displacementSize
)
365 emi
.displacement
= sext
<8>(emi
.displacement
);
368 emi
.displacement
= sext
<16>(emi
.displacement
);
371 emi
.displacement
= sext
<32>(emi
.displacement
);
374 panic("Undefined displacement size!\n");
376 DPRINTF(Predecoder
, "Collected displacement %#x.\n",
379 nextState
= ImmediateState
;
382 nextState
= ResetState
;
385 emi
.dispSize
= displacementSize
;
388 nextState
= DisplacementState
;
392 //Gather up the immediate, or at least as much of it
394 Predecoder::State
Predecoder::doImmediateState()
396 State nextState
= ErrorState
;
398 getImmediate(immediateCollected
,
402 DPRINTF(Predecoder
, "Collecting %d byte immediate, got %d bytes.\n",
403 immediateSize
, immediateCollected
);
405 if(immediateSize
== immediateCollected
)
407 //Reset this for other immediates.
408 immediateCollected
= 0;
410 //XXX Warning! The following is an observed pattern and might
411 //not always be true!
413 //Instructions which use 64 bit operands but 32 bit immediates
414 //need to have the immediate sign extended to 64 bits.
415 //Instructions which use true 64 bit immediates won't be
416 //affected, and instructions that use true 32 bit immediates
418 switch(immediateSize
)
421 emi
.immediate
= sext
<32>(emi
.immediate
);
424 emi
.immediate
= sext
<8>(emi
.immediate
);
427 DPRINTF(Predecoder
, "Collected immediate %#x.\n",
430 nextState
= ResetState
;
433 nextState
= ImmediateState
;