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 #ifndef __ARCH_X86_PREDECODER_HH__
59 #define __ARCH_X86_PREDECODER_HH__
61 #include "arch/x86/types.hh"
62 #include "base/bitfield.hh"
63 #include "base/misc.hh"
64 #include "base/trace.hh"
65 #include "sim/host.hh"
74 //These are defined and documented in predecoder_tables.cc
75 static const uint8_t Prefixes[256];
76 static const uint8_t UsesModRM[2][256];
77 static const uint8_t ImmediateType[2][256];
78 static const uint8_t ImmediateTypeToSize[3][10];
82 //The bytes to be predecoded
84 //The pc of the start of fetchChunk
86 //The offset into fetchChunk of current processing
88 //The extended machine instruction being generated
93 //Whether or not we're out of bytes
95 //Whether we've completed generating an ExtMachInst
97 //The size of the displacement value
99 int displacementCollected;
100 //The size of the immediate value
102 int immediateCollected;
104 //These are local to some of the states. I need to turn the states
105 //into inline functions to clean things up a bit.
108 MachInst partialDisp;
122 Predecoder(ThreadContext * _tc) :
123 tc(_tc), basePC(0), offset(0),
124 outOfBytes(true), emiIsReady(false),
128 ThreadContext * getTC()
133 void setTC(ThreadContext * _tc)
142 while(!emiIsReady && !outOfBytes)
144 uint8_t nextByte = (fetchChunk >> (offset * 8)) & 0xff;
148 uint8_t prefix = Prefixes[nextByte];
153 //Operand size override prefixes
154 case OperandSizeOverride:
155 DPRINTF(Predecoder, "Found operand size override prefix.\n");
157 case AddressSizeOverride:
158 DPRINTF(Predecoder, "Found address size override prefix.\n");
160 //Segment override prefixes
162 DPRINTF(Predecoder, "Found cs segment override.\n");
165 DPRINTF(Predecoder, "Found ds segment override.\n");
168 DPRINTF(Predecoder, "Found es segment override.\n");
171 DPRINTF(Predecoder, "Found fs segment override.\n");
174 DPRINTF(Predecoder, "Found gs segment override.\n");
177 DPRINTF(Predecoder, "Found ss segment override.\n");
180 DPRINTF(Predecoder, "Found lock prefix.\n");
183 DPRINTF(Predecoder, "Found rep prefix.\n");
186 DPRINTF(Predecoder, "Found repne prefix.\n");
189 DPRINTF(Predecoder, "Found Rex prefix %#x.\n", nextByte);
190 emi.rexPrefix = nextByte;
197 panic("Unrecognized prefix %#x\n", nextByte);
202 assert(emi.numOpcodes < 2);
205 DPRINTF(Predecoder, "Found two byte opcode.\n");
209 immediateCollected = 0;
210 displacementCollected = 0;
212 emi.displacement = 0;
213 int immType = ImmediateType[
214 emi.numOpcodes - 1][nextByte];
216 immediateSize = ImmediateTypeToSize[0][immType];
217 else if(!(emi.rexPrefix & 0x4)) //32 bit mode
218 immediateSize = ImmediateTypeToSize[1][immType];
220 immediateSize = ImmediateTypeToSize[2][immType];
221 DPRINTF(Predecoder, "Found opcode %#x.\n", nextByte);
222 if (UsesModRM[emi.numOpcodes - 1][nextByte]) {
224 } else if(immediateSize) {
234 DPRINTF(Predecoder, "Found modrm byte %#x.\n", nextByte);
235 if (0) {//in 16 bit mode
236 //figure out 16 bit displacement size
237 if(nextByte & 0xC7 == 0x06 ||
238 nextByte & 0xC0 == 0x80)
239 displacementSize = 2;
240 else if(nextByte & 0xC0 == 0x40)
241 displacementSize = 1;
243 displacementSize = 0;
245 //figure out 32/64 bit displacement size
246 if(nextByte & 0xC7 == 0x05 ||
247 nextByte & 0xC0 == 0x80)
248 displacementSize = 4;
249 else if(nextByte & 0xC0 == 0x40)
250 displacementSize = 2;
252 displacementSize = 0;
254 //If there's an SIB, get that next.
255 //There is no SIB in 16 bit mode.
256 if(nextByte & 0x7 == 4 &&
257 nextByte & 0xC0 != 0xC0) {
258 // && in 32/64 bit mode)
260 } else if(displacementSize) {
261 state = Displacement;
262 } else if(immediateSize) {
268 //The ModRM byte is consumed no matter what
272 DPRINTF(Predecoder, "Found SIB byte %#x.\n", nextByte);
274 if(displacementSize) {
275 state = Displacement;
276 } else if(immediateSize) {
284 //Gather up the displacement, or at least as much of it
287 //Figure out how many bytes we still need to get for the
289 toGet = displacementSize - displacementCollected;
290 //Figure out how many bytes are left in our "buffer"
291 remaining = sizeof(MachInst) - offset;
292 //Get as much as we need, up to the amount available.
293 toGet = toGet > remaining ? remaining : toGet;
295 //Shift the bytes we want to be all the way to the right
296 partialDisp = fetchChunk >> (offset * 8);
297 //Mask off what we don't want
298 partialDisp &= mask(toGet * 8);
299 //Shift it over to overlay with our displacement.
300 partialDisp <<= (displacementCollected * 8);
301 //Put it into our displacement
302 emi.displacement |= partialDisp;
303 //Update how many bytes we've collected.
304 displacementCollected += toGet;
306 DPRINTF(Predecoder, "Collecting %d byte displacement, got %d bytes.\n",
307 displacementSize, displacementCollected);
309 if(displacementSize == displacementCollected) {
310 //Sign extend the displacement
311 switch(displacementSize)
314 emi.displacement = sext<8>(emi.displacement);
317 emi.displacement = sext<16>(emi.displacement);
320 emi.displacement = sext<32>(emi.displacement);
323 panic("Undefined displacement size!\n");
325 DPRINTF(Predecoder, "Collected displacement %#x.\n",
336 //Gather up the displacement, or at least as much of it
339 //Figure out how many bytes we still need to get for the
341 toGet = immediateSize - immediateCollected;
342 //Figure out how many bytes are left in our "buffer"
343 remaining = sizeof(MachInst) - offset;
344 //Get as much as we need, up to the amount available.
345 toGet = toGet > remaining ? remaining : toGet;
347 //Shift the bytes we want to be all the way to the right
348 partialDisp = fetchChunk >> (offset * 8);
349 //Mask off what we don't want
350 partialDisp &= mask(toGet * 8);
351 //Shift it over to overlay with our immediate.
352 partialDisp <<= (immediateCollected * 8);
353 //Put it into our immediate
354 emi.immediate |= partialDisp;
355 //Update how many bytes we've collected.
356 immediateCollected += toGet;
358 DPRINTF(Predecoder, "Collecting %d byte immediate, got %d bytes.\n",
359 immediateSize, immediateCollected);
361 if(immediateSize == immediateCollected)
363 DPRINTF(Predecoder, "Collected immediate %#x.\n",
370 panic("Unrecognized state! %d\n", state);
372 if(offset == sizeof(MachInst))
377 //Use this to give data to the predecoder. This should be used
378 //when there is control flow.
379 void moreBytes(Addr currPC, Addr off, MachInst data)
384 assert(off < sizeof(MachInst));
389 //Use this to give data to the predecoder. This should be used
390 //when instructions are executed in order.
391 void moreBytes(MachInst machInst)
393 moreBytes(basePC + sizeof(machInst), 0, machInst);
401 bool extMachInstReady()
406 //This returns a constant reference to the ExtMachInst to avoid a copy
407 const ExtMachInst & getExtMachInst()
416 #endif // __ARCH_X86_PREDECODER_HH__