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::reset()
67 origPC
= basePC
+ offset
;
68 DPRINTF(Predecoder
, "Setting origPC to %#x\n", origPC
);
73 immediateCollected
= 0;
75 displacementCollected
= 0;
83 void Predecoder::process()
85 //This function drives the predecoder state machine.
87 //Some sanity checks. You shouldn't try to process more bytes if
88 //there aren't any, and you shouldn't overwrite an already
89 //predecoder ExtMachInst.
93 //While there's still something to do...
94 while(!emiIsReady
&& !outOfBytes
)
96 uint8_t nextByte
= getNextByte();
103 state
= doPrefixState(nextByte
);
106 state
= doOpcodeState(nextByte
);
109 state
= doModRMState(nextByte
);
112 state
= doSIBState(nextByte
);
114 case DisplacementState
:
115 state
= doDisplacementState();
118 state
= doImmediateState();
121 panic("Went to the error state in the predecoder.\n");
123 panic("Unrecognized state! %d\n", state
);
128 //Either get a prefix and record it in the ExtMachInst, or send the
129 //state machine on to get the opcode(s).
130 Predecoder::State
Predecoder::doPrefixState(uint8_t nextByte
)
132 uint8_t prefix
= Prefixes
[nextByte
];
133 State nextState
= PrefixState
;
138 //Operand size override prefixes
139 case OperandSizeOverride
:
140 DPRINTF(Predecoder
, "Found operand size override prefix.\n");
141 emi
.legacy
.op
= true;
143 case AddressSizeOverride
:
144 DPRINTF(Predecoder
, "Found address size override prefix.\n");
145 emi
.legacy
.addr
= true;
147 //Segment override prefixes
154 DPRINTF(Predecoder
, "Found segment override.\n");
155 emi
.legacy
.seg
= prefix
;
158 DPRINTF(Predecoder
, "Found lock prefix.\n");
159 emi
.legacy
.lock
= true;
162 DPRINTF(Predecoder
, "Found rep prefix.\n");
163 emi
.legacy
.rep
= true;
166 DPRINTF(Predecoder
, "Found repne prefix.\n");
167 emi
.legacy
.repne
= true;
170 DPRINTF(Predecoder
, "Found Rex prefix %#x.\n", nextByte
);
174 nextState
= OpcodeState
;
177 panic("Unrecognized prefix %#x\n", nextByte
);
182 //Load all the opcodes (currently up to 2) and then figure out
183 //what immediate and/or ModRM is needed.
184 Predecoder::State
Predecoder::doOpcodeState(uint8_t nextByte
)
186 State nextState
= ErrorState
;
188 //We can't handle 3+ byte opcodes right now
189 assert(emi
.opcode
.num
< 3);
191 if(emi
.opcode
.num
== 1 && nextByte
== 0x0f)
193 nextState
= OpcodeState
;
194 DPRINTF(Predecoder
, "Found two byte opcode.\n");
195 emi
.opcode
.prefixA
= nextByte
;
197 else if(emi
.opcode
.num
== 2 &&
199 (nextByte
& 0xf8) == 0x38))
201 panic("Three byte opcodes aren't yet supported!\n");
202 nextState
= OpcodeState
;
203 DPRINTF(Predecoder
, "Found three byte opcode.\n");
204 emi
.opcode
.prefixB
= nextByte
;
208 DPRINTF(Predecoder
, "Found opcode %#x.\n", nextByte
);
209 emi
.opcode
.op
= nextByte
;
211 //Figure out the effective operand size. This can be overriden to
212 //a fixed value at the decoder level.
214 if(/*FIXME long mode*/1)
217 logOpSize
= 3; // 64 bit operand size
218 else if(emi
.legacy
.op
)
219 logOpSize
= 1; // 16 bit operand size
221 logOpSize
= 2; // 32 bit operand size
223 else if(/*FIXME default 32*/1)
226 logOpSize
= 1; // 16 bit operand size
228 logOpSize
= 2; // 32 bit operand size
230 else // 16 bit default operand size
233 logOpSize
= 2; // 32 bit operand size
235 logOpSize
= 1; // 16 bit operand size
238 //Figure out how big of an immediate we'll retreive based
240 int immType
= ImmediateType
[emi
.opcode
.num
- 1][nextByte
];
241 immediateSize
= SizeTypeToSize
[logOpSize
- 1][immType
];
243 //Set the actual op size
244 emi
.opSize
= 1 << logOpSize
;
246 //Determine what to expect next
247 if (UsesModRM
[emi
.opcode
.num
- 1][nextByte
]) {
248 nextState
= ModRMState
;
251 nextState
= ImmediateState
;
254 nextState
= ResetState
;
261 //Get the ModRM byte and determine what displacement, if any, there is.
262 //Also determine whether or not to get the SIB byte, displacement, or
264 Predecoder::State
Predecoder::doModRMState(uint8_t nextByte
)
266 State nextState
= ErrorState
;
269 DPRINTF(Predecoder
, "Found modrm byte %#x.\n", nextByte
);
270 if (0) {//FIXME in 16 bit mode
271 //figure out 16 bit displacement size
272 if(modRM
.mod
== 0 && modRM
.rm
== 6 || modRM
.mod
== 2)
273 displacementSize
= 2;
274 else if(modRM
.mod
== 1)
275 displacementSize
= 1;
277 displacementSize
= 0;
279 //figure out 32/64 bit displacement size
280 if(modRM
.mod
== 0 && (modRM
.rm
== 4 || modRM
.rm
== 5)
282 displacementSize
= 4;
283 else if(modRM
.mod
== 1)
284 displacementSize
= 1;
286 displacementSize
= 0;
288 //If there's an SIB, get that next.
289 //There is no SIB in 16 bit mode.
290 if(modRM
.rm
== 4 && modRM
.mod
!= 3) {
291 // && in 32/64 bit mode)
292 nextState
= SIBState
;
293 } else if(displacementSize
) {
294 nextState
= DisplacementState
;
295 } else if(immediateSize
) {
296 nextState
= ImmediateState
;
299 nextState
= ResetState
;
301 //The ModRM byte is consumed no matter what
307 //Get the SIB byte. We don't do anything with it at this point, other
308 //than storing it in the ExtMachInst. Determine if we need to get a
309 //displacement or immediate next.
310 Predecoder::State
Predecoder::doSIBState(uint8_t nextByte
)
312 State nextState
= ErrorState
;
314 DPRINTF(Predecoder
, "Found SIB byte %#x.\n", nextByte
);
316 if(displacementSize
) {
317 nextState
= DisplacementState
;
318 } else if(immediateSize
) {
319 nextState
= ImmediateState
;
322 nextState
= ResetState
;
327 //Gather up the displacement, or at least as much of it
329 Predecoder::State
Predecoder::doDisplacementState()
331 State nextState
= ErrorState
;
333 getImmediate(displacementCollected
,
337 DPRINTF(Predecoder
, "Collecting %d byte displacement, got %d bytes.\n",
338 displacementSize
, displacementCollected
);
340 if(displacementSize
== displacementCollected
) {
341 //Sign extend the displacement
342 switch(displacementSize
)
345 emi
.displacement
= sext
<8>(emi
.displacement
);
348 emi
.displacement
= sext
<16>(emi
.displacement
);
351 emi
.displacement
= sext
<32>(emi
.displacement
);
354 panic("Undefined displacement size!\n");
356 DPRINTF(Predecoder
, "Collected displacement %#x.\n",
359 nextState
= ImmediateState
;
362 nextState
= ResetState
;
366 nextState
= DisplacementState
;
370 //Gather up the immediate, or at least as much of it
372 Predecoder::State
Predecoder::doImmediateState()
374 State nextState
= ErrorState
;
376 getImmediate(immediateCollected
,
380 DPRINTF(Predecoder
, "Collecting %d byte immediate, got %d bytes.\n",
381 immediateSize
, immediateCollected
);
383 if(immediateSize
== immediateCollected
)
385 //XXX Warning! The following is an observed pattern and might
386 //not always be true!
388 //Instructions which use 64 bit operands but 32 bit immediates
389 //need to have the immediate sign extended to 64 bits.
390 //Instructions which use true 64 bit immediates won't be
391 //affected, and instructions that use true 32 bit immediates
393 switch(immediateSize
)
396 emi
.immediate
= sext
<32>(emi
.immediate
);
399 emi
.immediate
= sext
<8>(emi
.immediate
);
402 DPRINTF(Predecoder
, "Collected immediate %#x.\n",
405 nextState
= ResetState
;
408 nextState
= ImmediateState
;