2 * Copyright (c) 2007-2008 The Hewlett-Packard Development Company
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "arch/x86/regs/misc.hh"
41 #include "arch/x86/predecoder.hh"
42 #include "base/misc.hh"
43 #include "base/trace.hh"
44 #include "base/types.hh"
45 #include "cpu/thread_context.hh"
46 #include "debug/Predecoder.hh"
50 void Predecoder::doReset()
52 origPC
= basePC
+ offset
;
53 DPRINTF(Predecoder
, "Setting origPC to %#x\n", origPC
);
58 emi
.opcode
.prefixA
= emi
.opcode
.prefixB
= 0;
60 immediateCollected
= 0;
67 m5Reg
= tc
->readMiscRegNoEffect(MISCREG_M5_REG
);
68 emi
.mode
.mode
= m5Reg
.mode
;
69 emi
.mode
.submode
= m5Reg
.submode
;
72 void Predecoder::process()
74 //This function drives the predecoder state machine.
76 //Some sanity checks. You shouldn't try to process more bytes if
77 //there aren't any, and you shouldn't overwrite an already
78 //predecoder ExtMachInst.
82 //While there's still something to do...
83 while(!emiIsReady
&& !outOfBytes
)
85 uint8_t nextByte
= getNextByte();
92 state
= doPrefixState(nextByte
);
95 state
= doOpcodeState(nextByte
);
98 state
= doModRMState(nextByte
);
101 state
= doSIBState(nextByte
);
103 case DisplacementState
:
104 state
= doDisplacementState();
107 state
= doImmediateState();
110 panic("Went to the error state in the predecoder.\n");
112 panic("Unrecognized state! %d\n", state
);
117 //Either get a prefix and record it in the ExtMachInst, or send the
118 //state machine on to get the opcode(s).
119 Predecoder::State
Predecoder::doPrefixState(uint8_t nextByte
)
121 uint8_t prefix
= Prefixes
[nextByte
];
122 State nextState
= PrefixState
;
123 // REX prefixes are only recognized in 64 bit mode.
124 if (prefix
== RexPrefix
&& emi
.mode
.submode
!= SixtyFourBitMode
)
130 //Operand size override prefixes
131 case OperandSizeOverride
:
132 DPRINTF(Predecoder
, "Found operand size override prefix.\n");
133 emi
.legacy
.op
= true;
135 case AddressSizeOverride
:
136 DPRINTF(Predecoder
, "Found address size override prefix.\n");
137 emi
.legacy
.addr
= true;
139 //Segment override prefixes
146 DPRINTF(Predecoder
, "Found segment override.\n");
147 emi
.legacy
.seg
= prefix
;
150 DPRINTF(Predecoder
, "Found lock prefix.\n");
151 emi
.legacy
.lock
= true;
154 DPRINTF(Predecoder
, "Found rep prefix.\n");
155 emi
.legacy
.rep
= true;
158 DPRINTF(Predecoder
, "Found repne prefix.\n");
159 emi
.legacy
.repne
= true;
162 DPRINTF(Predecoder
, "Found Rex prefix %#x.\n", nextByte
);
166 nextState
= OpcodeState
;
169 panic("Unrecognized prefix %#x\n", nextByte
);
174 //Load all the opcodes (currently up to 2) and then figure out
175 //what immediate and/or ModRM is needed.
176 Predecoder::State
Predecoder::doOpcodeState(uint8_t nextByte
)
178 State nextState
= ErrorState
;
180 //We can't handle 3+ byte opcodes right now
181 assert(emi
.opcode
.num
< 4);
183 if(emi
.opcode
.num
== 1 && nextByte
== 0x0f)
185 nextState
= OpcodeState
;
186 DPRINTF(Predecoder
, "Found two byte opcode.\n");
187 emi
.opcode
.prefixA
= nextByte
;
189 else if(emi
.opcode
.num
== 2 && (nextByte
== 0x38 || nextByte
== 0x3A))
191 nextState
= OpcodeState
;
192 DPRINTF(Predecoder
, "Found three byte opcode.\n");
193 emi
.opcode
.prefixB
= nextByte
;
197 DPRINTF(Predecoder
, "Found opcode %#x.\n", nextByte
);
198 emi
.opcode
.op
= nextByte
;
200 //Figure out the effective operand size. This can be overriden to
201 //a fixed value at the decoder level.
204 logOpSize
= 3; // 64 bit operand size
205 else if (emi
.legacy
.op
)
206 logOpSize
= m5Reg
.altOp
;
208 logOpSize
= m5Reg
.defOp
;
210 //Set the actual op size
211 emi
.opSize
= 1 << logOpSize
;
213 //Figure out the effective address size. This can be overriden to
214 //a fixed value at the decoder level.
217 logAddrSize
= m5Reg
.altAddr
;
219 logAddrSize
= m5Reg
.defAddr
;
221 //Set the actual address size
222 emi
.addrSize
= 1 << logAddrSize
;
224 //Figure out the effective stack width. This can be overriden to
225 //a fixed value at the decoder level.
226 emi
.stackSize
= 1 << m5Reg
.stack
;
228 //Figure out how big of an immediate we'll retreive based
230 int immType
= ImmediateType
[emi
.opcode
.num
- 1][nextByte
];
231 if (emi
.opcode
.num
== 1 && nextByte
>= 0xA0 && nextByte
<= 0xA3)
232 immediateSize
= SizeTypeToSize
[logAddrSize
- 1][immType
];
234 immediateSize
= SizeTypeToSize
[logOpSize
- 1][immType
];
236 //Determine what to expect next
237 if (UsesModRM
[emi
.opcode
.num
- 1][nextByte
]) {
238 nextState
= ModRMState
;
241 nextState
= ImmediateState
;
244 nextState
= ResetState
;
251 //Get the ModRM byte and determine what displacement, if any, there is.
252 //Also determine whether or not to get the SIB byte, displacement, or
254 Predecoder::State
Predecoder::doModRMState(uint8_t nextByte
)
256 State nextState
= ErrorState
;
259 DPRINTF(Predecoder
, "Found modrm byte %#x.\n", nextByte
);
260 if (m5Reg
.defOp
== 1) {
261 //figure out 16 bit displacement size
262 if ((modRM
.mod
== 0 && modRM
.rm
== 6) || modRM
.mod
== 2)
263 displacementSize
= 2;
264 else if (modRM
.mod
== 1)
265 displacementSize
= 1;
267 displacementSize
= 0;
269 //figure out 32/64 bit displacement size
270 if ((modRM
.mod
== 0 && modRM
.rm
== 5) || modRM
.mod
== 2)
271 displacementSize
= 4;
272 else if (modRM
.mod
== 1)
273 displacementSize
= 1;
275 displacementSize
= 0;
278 // The "test" instruction in group 3 needs an immediate, even though
279 // the other instructions with the same actual opcode don't.
280 if (emi
.opcode
.num
== 1 && (modRM
.reg
& 0x6) == 0) {
281 if (emi
.opcode
.op
== 0xF6)
283 else if (emi
.opcode
.op
== 0xF7)
284 immediateSize
= (emi
.opSize
== 8) ? 4 : emi
.opSize
;
287 //If there's an SIB, get that next.
288 //There is no SIB in 16 bit mode.
289 if (modRM
.rm
== 4 && modRM
.mod
!= 3) {
290 // && in 32/64 bit mode)
291 nextState
= SIBState
;
292 } else if(displacementSize
) {
293 nextState
= DisplacementState
;
294 } else if(immediateSize
) {
295 nextState
= ImmediateState
;
298 nextState
= ResetState
;
300 //The ModRM byte is consumed no matter what
306 //Get the SIB byte. We don't do anything with it at this point, other
307 //than storing it in the ExtMachInst. Determine if we need to get a
308 //displacement or immediate next.
309 Predecoder::State
Predecoder::doSIBState(uint8_t nextByte
)
311 State nextState
= ErrorState
;
313 DPRINTF(Predecoder
, "Found SIB byte %#x.\n", nextByte
);
315 if (emi
.modRM
.mod
== 0 && emi
.sib
.base
== 5)
316 displacementSize
= 4;
317 if (displacementSize
) {
318 nextState
= DisplacementState
;
319 } else if(immediateSize
) {
320 nextState
= ImmediateState
;
323 nextState
= ResetState
;
328 //Gather up the displacement, or at least as much of it
330 Predecoder::State
Predecoder::doDisplacementState()
332 State nextState
= ErrorState
;
334 getImmediate(immediateCollected
,
338 DPRINTF(Predecoder
, "Collecting %d byte displacement, got %d bytes.\n",
339 displacementSize
, immediateCollected
);
341 if(displacementSize
== immediateCollected
) {
342 //Reset this for other immediates.
343 immediateCollected
= 0;
344 //Sign extend the displacement
345 switch(displacementSize
)
348 emi
.displacement
= sext
<8>(emi
.displacement
);
351 emi
.displacement
= sext
<16>(emi
.displacement
);
354 emi
.displacement
= sext
<32>(emi
.displacement
);
357 panic("Undefined displacement size!\n");
359 DPRINTF(Predecoder
, "Collected displacement %#x.\n",
362 nextState
= ImmediateState
;
365 nextState
= ResetState
;
368 emi
.dispSize
= displacementSize
;
371 nextState
= DisplacementState
;
375 //Gather up the immediate, or at least as much of it
377 Predecoder::State
Predecoder::doImmediateState()
379 State nextState
= ErrorState
;
381 getImmediate(immediateCollected
,
385 DPRINTF(Predecoder
, "Collecting %d byte immediate, got %d bytes.\n",
386 immediateSize
, immediateCollected
);
388 if(immediateSize
== immediateCollected
)
390 //Reset this for other immediates.
391 immediateCollected
= 0;
393 //XXX Warning! The following is an observed pattern and might
394 //not always be true!
396 //Instructions which use 64 bit operands but 32 bit immediates
397 //need to have the immediate sign extended to 64 bits.
398 //Instructions which use true 64 bit immediates won't be
399 //affected, and instructions that use true 32 bit immediates
401 switch(immediateSize
)
404 emi
.immediate
= sext
<32>(emi
.immediate
);
407 emi
.immediate
= sext
<8>(emi
.immediate
);
410 DPRINTF(Predecoder
, "Collected immediate %#x.\n",
413 nextState
= ResetState
;
416 nextState
= ImmediateState
;