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/miscregs.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"
49 void Predecoder::doReset()
51 origPC
= basePC
+ offset
;
52 DPRINTF(Predecoder
, "Setting origPC to %#x\n", origPC
);
57 emi
.opcode
.prefixA
= emi
.opcode
.prefixB
= 0;
59 immediateCollected
= 0;
66 m5Reg
= tc
->readMiscRegNoEffect(MISCREG_M5_REG
);
67 emi
.mode
.mode
= m5Reg
.mode
;
68 emi
.mode
.submode
= m5Reg
.submode
;
71 void Predecoder::process()
73 //This function drives the predecoder state machine.
75 //Some sanity checks. You shouldn't try to process more bytes if
76 //there aren't any, and you shouldn't overwrite an already
77 //predecoder ExtMachInst.
81 //While there's still something to do...
82 while(!emiIsReady
&& !outOfBytes
)
84 uint8_t nextByte
= getNextByte();
91 state
= doPrefixState(nextByte
);
94 state
= doOpcodeState(nextByte
);
97 state
= doModRMState(nextByte
);
100 state
= doSIBState(nextByte
);
102 case DisplacementState
:
103 state
= doDisplacementState();
106 state
= doImmediateState();
109 panic("Went to the error state in the predecoder.\n");
111 panic("Unrecognized state! %d\n", state
);
116 //Either get a prefix and record it in the ExtMachInst, or send the
117 //state machine on to get the opcode(s).
118 Predecoder::State
Predecoder::doPrefixState(uint8_t nextByte
)
120 uint8_t prefix
= Prefixes
[nextByte
];
121 State nextState
= PrefixState
;
122 // REX prefixes are only recognized in 64 bit mode.
123 if (prefix
== RexPrefix
&& emi
.mode
.submode
!= SixtyFourBitMode
)
129 //Operand size override prefixes
130 case OperandSizeOverride
:
131 DPRINTF(Predecoder
, "Found operand size override prefix.\n");
132 emi
.legacy
.op
= true;
134 case AddressSizeOverride
:
135 DPRINTF(Predecoder
, "Found address size override prefix.\n");
136 emi
.legacy
.addr
= true;
138 //Segment override prefixes
145 DPRINTF(Predecoder
, "Found segment override.\n");
146 emi
.legacy
.seg
= prefix
;
149 DPRINTF(Predecoder
, "Found lock prefix.\n");
150 emi
.legacy
.lock
= true;
153 DPRINTF(Predecoder
, "Found rep prefix.\n");
154 emi
.legacy
.rep
= true;
157 DPRINTF(Predecoder
, "Found repne prefix.\n");
158 emi
.legacy
.repne
= true;
161 DPRINTF(Predecoder
, "Found Rex prefix %#x.\n", nextByte
);
165 nextState
= OpcodeState
;
168 panic("Unrecognized prefix %#x\n", nextByte
);
173 //Load all the opcodes (currently up to 2) and then figure out
174 //what immediate and/or ModRM is needed.
175 Predecoder::State
Predecoder::doOpcodeState(uint8_t nextByte
)
177 State nextState
= ErrorState
;
179 //We can't handle 3+ byte opcodes right now
180 assert(emi
.opcode
.num
< 4);
182 if(emi
.opcode
.num
== 1 && nextByte
== 0x0f)
184 nextState
= OpcodeState
;
185 DPRINTF(Predecoder
, "Found two byte opcode.\n");
186 emi
.opcode
.prefixA
= nextByte
;
188 else if(emi
.opcode
.num
== 2 && (nextByte
== 0x38 || nextByte
== 0x3F))
190 nextState
= OpcodeState
;
191 DPRINTF(Predecoder
, "Found three byte opcode.\n");
192 emi
.opcode
.prefixB
= nextByte
;
196 DPRINTF(Predecoder
, "Found opcode %#x.\n", nextByte
);
197 emi
.opcode
.op
= nextByte
;
199 //Figure out the effective operand size. This can be overriden to
200 //a fixed value at the decoder level.
203 logOpSize
= 3; // 64 bit operand size
204 else if (emi
.legacy
.op
)
205 logOpSize
= m5Reg
.altOp
;
207 logOpSize
= m5Reg
.defOp
;
209 //Set the actual op size
210 emi
.opSize
= 1 << logOpSize
;
212 //Figure out the effective address size. This can be overriden to
213 //a fixed value at the decoder level.
216 logAddrSize
= m5Reg
.altAddr
;
218 logAddrSize
= m5Reg
.defAddr
;
220 //Set the actual address size
221 emi
.addrSize
= 1 << logAddrSize
;
223 //Figure out the effective stack width. This can be overriden to
224 //a fixed value at the decoder level.
225 emi
.stackSize
= 1 << m5Reg
.stack
;
227 //Figure out how big of an immediate we'll retreive based
229 int immType
= ImmediateType
[emi
.opcode
.num
- 1][nextByte
];
230 if (emi
.opcode
.num
== 1 && nextByte
>= 0xA0 && nextByte
<= 0xA3)
231 immediateSize
= SizeTypeToSize
[logAddrSize
- 1][immType
];
233 immediateSize
= SizeTypeToSize
[logOpSize
- 1][immType
];
235 //Determine what to expect next
236 if (UsesModRM
[emi
.opcode
.num
- 1][nextByte
]) {
237 nextState
= ModRMState
;
240 nextState
= ImmediateState
;
243 nextState
= ResetState
;
250 //Get the ModRM byte and determine what displacement, if any, there is.
251 //Also determine whether or not to get the SIB byte, displacement, or
253 Predecoder::State
Predecoder::doModRMState(uint8_t nextByte
)
255 State nextState
= ErrorState
;
258 DPRINTF(Predecoder
, "Found modrm byte %#x.\n", nextByte
);
259 if (m5Reg
.defOp
== 1) {
260 //figure out 16 bit displacement size
261 if ((modRM
.mod
== 0 && modRM
.rm
== 6) || modRM
.mod
== 2)
262 displacementSize
= 2;
263 else if (modRM
.mod
== 1)
264 displacementSize
= 1;
266 displacementSize
= 0;
268 //figure out 32/64 bit displacement size
269 if ((modRM
.mod
== 0 && modRM
.rm
== 5) || modRM
.mod
== 2)
270 displacementSize
= 4;
271 else if (modRM
.mod
== 1)
272 displacementSize
= 1;
274 displacementSize
= 0;
277 // The "test" instruction in group 3 needs an immediate, even though
278 // the other instructions with the same actual opcode don't.
279 if (emi
.opcode
.num
== 1 && (modRM
.reg
& 0x6) == 0) {
280 if (emi
.opcode
.op
== 0xF6)
282 else if (emi
.opcode
.op
== 0xF7)
283 immediateSize
= (emi
.opSize
== 8) ? 4 : emi
.opSize
;
286 //If there's an SIB, get that next.
287 //There is no SIB in 16 bit mode.
288 if (modRM
.rm
== 4 && modRM
.mod
!= 3) {
289 // && in 32/64 bit mode)
290 nextState
= SIBState
;
291 } else if(displacementSize
) {
292 nextState
= DisplacementState
;
293 } else if(immediateSize
) {
294 nextState
= ImmediateState
;
297 nextState
= ResetState
;
299 //The ModRM byte is consumed no matter what
305 //Get the SIB byte. We don't do anything with it at this point, other
306 //than storing it in the ExtMachInst. Determine if we need to get a
307 //displacement or immediate next.
308 Predecoder::State
Predecoder::doSIBState(uint8_t nextByte
)
310 State nextState
= ErrorState
;
312 DPRINTF(Predecoder
, "Found SIB byte %#x.\n", nextByte
);
314 if (emi
.modRM
.mod
== 0 && emi
.sib
.base
== 5)
315 displacementSize
= 4;
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(immediateCollected
,
337 DPRINTF(Predecoder
, "Collecting %d byte displacement, got %d bytes.\n",
338 displacementSize
, immediateCollected
);
340 if(displacementSize
== immediateCollected
) {
341 //Reset this for other immediates.
342 immediateCollected
= 0;
343 //Sign extend the displacement
344 switch(displacementSize
)
347 emi
.displacement
= sext
<8>(emi
.displacement
);
350 emi
.displacement
= sext
<16>(emi
.displacement
);
353 emi
.displacement
= sext
<32>(emi
.displacement
);
356 panic("Undefined displacement size!\n");
358 DPRINTF(Predecoder
, "Collected displacement %#x.\n",
361 nextState
= ImmediateState
;
364 nextState
= ResetState
;
367 emi
.dispSize
= displacementSize
;
370 nextState
= DisplacementState
;
374 //Gather up the immediate, or at least as much of it
376 Predecoder::State
Predecoder::doImmediateState()
378 State nextState
= ErrorState
;
380 getImmediate(immediateCollected
,
384 DPRINTF(Predecoder
, "Collecting %d byte immediate, got %d bytes.\n",
385 immediateSize
, immediateCollected
);
387 if(immediateSize
== immediateCollected
)
389 //Reset this for other immediates.
390 immediateCollected
= 0;
392 //XXX Warning! The following is an observed pattern and might
393 //not always be true!
395 //Instructions which use 64 bit operands but 32 bit immediates
396 //need to have the immediate sign extended to 64 bits.
397 //Instructions which use true 64 bit immediates won't be
398 //affected, and instructions that use true 32 bit immediates
400 switch(immediateSize
)
403 emi
.immediate
= sext
<32>(emi
.immediate
);
406 emi
.immediate
= sext
<8>(emi
.immediate
);
409 DPRINTF(Predecoder
, "Collected immediate %#x.\n",
412 nextState
= ResetState
;
415 nextState
= ImmediateState
;