2 * Copyright (c) 2007 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/predecoder.hh"
59 #include "base/misc.hh"
60 #include "base/trace.hh"
61 #include "sim/host.hh"
65 void Predecoder::doReset()
67 origPC
= basePC
+ offset
;
68 DPRINTF(Predecoder
, "Setting origPC to %#x\n", origPC
);
73 emi
.opcode
.prefixA
= emi
.opcode
.prefixB
= 0;
75 immediateCollected
= 0;
84 void Predecoder::process()
86 //This function drives the predecoder state machine.
88 //Some sanity checks. You shouldn't try to process more bytes if
89 //there aren't any, and you shouldn't overwrite an already
90 //predecoder ExtMachInst.
94 //While there's still something to do...
95 while(!emiIsReady
&& !outOfBytes
)
97 uint8_t nextByte
= getNextByte();
104 state
= doPrefixState(nextByte
);
107 state
= doOpcodeState(nextByte
);
110 state
= doModRMState(nextByte
);
113 state
= doSIBState(nextByte
);
115 case DisplacementState
:
116 state
= doDisplacementState();
119 state
= doImmediateState();
122 panic("Went to the error state in the predecoder.\n");
124 panic("Unrecognized state! %d\n", state
);
129 //Either get a prefix and record it in the ExtMachInst, or send the
130 //state machine on to get the opcode(s).
131 Predecoder::State
Predecoder::doPrefixState(uint8_t nextByte
)
133 uint8_t prefix
= Prefixes
[nextByte
];
134 State nextState
= PrefixState
;
139 //Operand size override prefixes
140 case OperandSizeOverride
:
141 DPRINTF(Predecoder
, "Found operand size override prefix.\n");
142 emi
.legacy
.op
= true;
144 case AddressSizeOverride
:
145 DPRINTF(Predecoder
, "Found address size override prefix.\n");
146 emi
.legacy
.addr
= true;
148 //Segment override prefixes
155 DPRINTF(Predecoder
, "Found segment override.\n");
156 emi
.legacy
.seg
= prefix
;
159 DPRINTF(Predecoder
, "Found lock prefix.\n");
160 emi
.legacy
.lock
= true;
163 DPRINTF(Predecoder
, "Found rep prefix.\n");
164 emi
.legacy
.rep
= true;
167 DPRINTF(Predecoder
, "Found repne prefix.\n");
168 emi
.legacy
.repne
= true;
171 DPRINTF(Predecoder
, "Found Rex prefix %#x.\n", nextByte
);
175 nextState
= OpcodeState
;
178 panic("Unrecognized prefix %#x\n", nextByte
);
183 //Load all the opcodes (currently up to 2) and then figure out
184 //what immediate and/or ModRM is needed.
185 Predecoder::State
Predecoder::doOpcodeState(uint8_t nextByte
)
187 State nextState
= ErrorState
;
189 //We can't handle 3+ byte opcodes right now
190 assert(emi
.opcode
.num
< 3);
192 if(emi
.opcode
.num
== 1 && nextByte
== 0x0f)
194 nextState
= OpcodeState
;
195 DPRINTF(Predecoder
, "Found two byte opcode.\n");
196 emi
.opcode
.prefixA
= nextByte
;
198 else if(emi
.opcode
.num
== 2 &&
200 (nextByte
& 0xf8) == 0x38))
202 panic("Three byte opcodes aren't yet supported!\n");
203 nextState
= OpcodeState
;
204 DPRINTF(Predecoder
, "Found three byte opcode.\n");
205 emi
.opcode
.prefixB
= nextByte
;
209 DPRINTF(Predecoder
, "Found opcode %#x.\n", nextByte
);
210 emi
.opcode
.op
= nextByte
;
212 //Figure out the effective operand size. This can be overriden to
213 //a fixed value at the decoder level.
215 if(/*FIXME long mode*/1)
218 logOpSize
= 3; // 64 bit operand size
219 else if(emi
.legacy
.op
)
220 logOpSize
= 1; // 16 bit operand size
222 logOpSize
= 2; // 32 bit operand size
224 else if(/*FIXME default 32*/1)
227 logOpSize
= 1; // 16 bit operand size
229 logOpSize
= 2; // 32 bit operand size
231 else // 16 bit default operand size
234 logOpSize
= 2; // 32 bit operand size
236 logOpSize
= 1; // 16 bit operand size
239 //Set the actual op size
240 emi
.opSize
= 1 << logOpSize
;
242 //Figure out the effective address size. This can be overriden to
243 //a fixed value at the decoder level.
245 if(/*FIXME 64-bit mode*/1)
248 logAddrSize
= 2; // 32 bit address size
250 logAddrSize
= 3; // 64 bit address size
252 else if(/*FIXME default 32*/1)
255 logAddrSize
= 1; // 16 bit address size
257 logAddrSize
= 2; // 32 bit address size
259 else // 16 bit default operand size
262 logAddrSize
= 2; // 32 bit address size
264 logAddrSize
= 1; // 16 bit address size
267 //Set the actual address size
268 emi
.addrSize
= 1 << logAddrSize
;
270 //Figure out how big of an immediate we'll retreive based
272 int immType
= ImmediateType
[emi
.opcode
.num
- 1][nextByte
];
273 immediateSize
= SizeTypeToSize
[logOpSize
- 1][immType
];
275 //Determine what to expect next
276 if (UsesModRM
[emi
.opcode
.num
- 1][nextByte
]) {
277 nextState
= ModRMState
;
280 nextState
= ImmediateState
;
283 nextState
= ResetState
;
290 //Get the ModRM byte and determine what displacement, if any, there is.
291 //Also determine whether or not to get the SIB byte, displacement, or
293 Predecoder::State
Predecoder::doModRMState(uint8_t nextByte
)
295 State nextState
= ErrorState
;
298 DPRINTF(Predecoder
, "Found modrm byte %#x.\n", nextByte
);
299 if (0) {//FIXME in 16 bit mode
300 //figure out 16 bit displacement size
301 if(modRM
.mod
== 0 && modRM
.rm
== 6 || modRM
.mod
== 2)
302 displacementSize
= 2;
303 else if(modRM
.mod
== 1)
304 displacementSize
= 1;
306 displacementSize
= 0;
308 //figure out 32/64 bit displacement size
309 if(modRM
.mod
== 0 && modRM
.rm
== 5 || modRM
.mod
== 2)
310 displacementSize
= 4;
311 else if(modRM
.mod
== 1)
312 displacementSize
= 1;
314 displacementSize
= 0;
317 // The "test" instruction in group 3 needs an immediate, even though
318 // the other instructions with the same actual opcode don't.
319 if (emi
.opcode
.num
== 1 && (modRM
.reg
& 0x6) == 0) {
320 if (emi
.opcode
.op
== 0xF6)
322 else if (emi
.opcode
.op
== 0xF7)
323 immediateSize
= (emi
.opSize
== 8) ? 4 : emi
.opSize
;
326 //If there's an SIB, get that next.
327 //There is no SIB in 16 bit mode.
328 if (modRM
.rm
== 4 && modRM
.mod
!= 3) {
329 // && in 32/64 bit mode)
330 nextState
= SIBState
;
331 } else if(displacementSize
) {
332 nextState
= DisplacementState
;
333 } else if(immediateSize
) {
334 nextState
= ImmediateState
;
337 nextState
= ResetState
;
339 //The ModRM byte is consumed no matter what
345 //Get the SIB byte. We don't do anything with it at this point, other
346 //than storing it in the ExtMachInst. Determine if we need to get a
347 //displacement or immediate next.
348 Predecoder::State
Predecoder::doSIBState(uint8_t nextByte
)
350 State nextState
= ErrorState
;
352 DPRINTF(Predecoder
, "Found SIB byte %#x.\n", nextByte
);
354 if (emi
.modRM
.mod
== 0 && emi
.sib
.base
== 5)
355 displacementSize
= 4;
356 if (displacementSize
) {
357 nextState
= DisplacementState
;
358 } else if(immediateSize
) {
359 nextState
= ImmediateState
;
362 nextState
= ResetState
;
367 //Gather up the displacement, or at least as much of it
369 Predecoder::State
Predecoder::doDisplacementState()
371 State nextState
= ErrorState
;
373 getImmediate(immediateCollected
,
377 DPRINTF(Predecoder
, "Collecting %d byte displacement, got %d bytes.\n",
378 displacementSize
, immediateCollected
);
380 if(displacementSize
== immediateCollected
) {
381 //Reset this for other immediates.
382 immediateCollected
= 0;
383 //Sign extend the displacement
384 switch(displacementSize
)
387 emi
.displacement
= sext
<8>(emi
.displacement
);
390 emi
.displacement
= sext
<16>(emi
.displacement
);
393 emi
.displacement
= sext
<32>(emi
.displacement
);
396 panic("Undefined displacement size!\n");
398 DPRINTF(Predecoder
, "Collected displacement %#x.\n",
401 nextState
= ImmediateState
;
404 nextState
= ResetState
;
408 nextState
= DisplacementState
;
412 //Gather up the immediate, or at least as much of it
414 Predecoder::State
Predecoder::doImmediateState()
416 State nextState
= ErrorState
;
418 getImmediate(immediateCollected
,
422 DPRINTF(Predecoder
, "Collecting %d byte immediate, got %d bytes.\n",
423 immediateSize
, immediateCollected
);
425 if(immediateSize
== immediateCollected
)
427 //Reset this for other immediates.
428 immediateCollected
= 0;
430 //XXX Warning! The following is an observed pattern and might
431 //not always be true!
433 //Instructions which use 64 bit operands but 32 bit immediates
434 //need to have the immediate sign extended to 64 bits.
435 //Instructions which use true 64 bit immediates won't be
436 //affected, and instructions that use true 32 bit immediates
438 switch(immediateSize
)
441 emi
.immediate
= sext
<32>(emi
.immediate
);
444 emi
.immediate
= sext
<8>(emi
.immediate
);
447 DPRINTF(Predecoder
, "Collected immediate %#x.\n",
450 nextState
= ResetState
;
453 nextState
= ImmediateState
;