X86: Fix segment override prefixes on instructions that use rbp/rsp and a displacement.
[gem5.git] / src / arch / x86 / predecoder.cc
1 /*
2 * Copyright (c) 2007-2008 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
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:
8 *
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.
17 *
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
23 * 1501 Page Mill Road
24 * Palo Alto, California 94304
25 *
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.
42 *
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.
54 *
55 * Authors: Gabe Black
56 */
57
58 #include "arch/x86/miscregs.hh"
59 #include "arch/x86/predecoder.hh"
60 #include "base/misc.hh"
61 #include "base/trace.hh"
62 #include "base/types.hh"
63 #include "cpu/thread_context.hh"
64
65 namespace X86ISA
66 {
67 void Predecoder::doReset()
68 {
69 origPC = basePC + offset;
70 DPRINTF(Predecoder, "Setting origPC to %#x\n", origPC);
71 emi.rex = 0;
72 emi.legacy = 0;
73 emi.opcode.num = 0;
74 emi.opcode.op = 0;
75 emi.opcode.prefixA = emi.opcode.prefixB = 0;
76
77 immediateCollected = 0;
78 emi.immediate = 0;
79 emi.displacement = 0;
80 emi.dispSize = 0;
81
82 emi.modRM = 0;
83 emi.sib = 0;
84 m5Reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
85 emi.mode.mode = m5Reg.mode;
86 emi.mode.submode = m5Reg.submode;
87 }
88
89 void Predecoder::process()
90 {
91 //This function drives the predecoder state machine.
92
93 //Some sanity checks. You shouldn't try to process more bytes if
94 //there aren't any, and you shouldn't overwrite an already
95 //predecoder ExtMachInst.
96 assert(!outOfBytes);
97 assert(!emiIsReady);
98
99 //While there's still something to do...
100 while(!emiIsReady && !outOfBytes)
101 {
102 uint8_t nextByte = getNextByte();
103 switch(state)
104 {
105 case ResetState:
106 doReset();
107 state = PrefixState;
108 case PrefixState:
109 state = doPrefixState(nextByte);
110 break;
111 case OpcodeState:
112 state = doOpcodeState(nextByte);
113 break;
114 case ModRMState:
115 state = doModRMState(nextByte);
116 break;
117 case SIBState:
118 state = doSIBState(nextByte);
119 break;
120 case DisplacementState:
121 state = doDisplacementState();
122 break;
123 case ImmediateState:
124 state = doImmediateState();
125 break;
126 case ErrorState:
127 panic("Went to the error state in the predecoder.\n");
128 default:
129 panic("Unrecognized state! %d\n", state);
130 }
131 }
132 }
133
134 //Either get a prefix and record it in the ExtMachInst, or send the
135 //state machine on to get the opcode(s).
136 Predecoder::State Predecoder::doPrefixState(uint8_t nextByte)
137 {
138 uint8_t prefix = Prefixes[nextByte];
139 State nextState = PrefixState;
140 // REX prefixes are only recognized in 64 bit mode.
141 if (prefix == RexPrefix && emi.mode.submode != SixtyFourBitMode)
142 prefix = 0;
143 if (prefix)
144 consumeByte();
145 switch(prefix)
146 {
147 //Operand size override prefixes
148 case OperandSizeOverride:
149 DPRINTF(Predecoder, "Found operand size override prefix.\n");
150 emi.legacy.op = true;
151 break;
152 case AddressSizeOverride:
153 DPRINTF(Predecoder, "Found address size override prefix.\n");
154 emi.legacy.addr = true;
155 break;
156 //Segment override prefixes
157 case CSOverride:
158 case DSOverride:
159 case ESOverride:
160 case FSOverride:
161 case GSOverride:
162 case SSOverride:
163 DPRINTF(Predecoder, "Found segment override.\n");
164 emi.legacy.seg = prefix;
165 break;
166 case Lock:
167 DPRINTF(Predecoder, "Found lock prefix.\n");
168 emi.legacy.lock = true;
169 break;
170 case Rep:
171 DPRINTF(Predecoder, "Found rep prefix.\n");
172 emi.legacy.rep = true;
173 break;
174 case Repne:
175 DPRINTF(Predecoder, "Found repne prefix.\n");
176 emi.legacy.repne = true;
177 break;
178 case RexPrefix:
179 DPRINTF(Predecoder, "Found Rex prefix %#x.\n", nextByte);
180 emi.rex = nextByte;
181 break;
182 case 0:
183 nextState = OpcodeState;
184 break;
185 default:
186 panic("Unrecognized prefix %#x\n", nextByte);
187 }
188 return nextState;
189 }
190
191 //Load all the opcodes (currently up to 2) and then figure out
192 //what immediate and/or ModRM is needed.
193 Predecoder::State Predecoder::doOpcodeState(uint8_t nextByte)
194 {
195 State nextState = ErrorState;
196 emi.opcode.num++;
197 //We can't handle 3+ byte opcodes right now
198 assert(emi.opcode.num < 3);
199 consumeByte();
200 if(emi.opcode.num == 1 && nextByte == 0x0f)
201 {
202 nextState = OpcodeState;
203 DPRINTF(Predecoder, "Found two byte opcode.\n");
204 emi.opcode.prefixA = nextByte;
205 }
206 else if(emi.opcode.num == 2 &&
207 (nextByte == 0x0f ||
208 (nextByte & 0xf8) == 0x38))
209 {
210 panic("Three byte opcodes aren't yet supported!\n");
211 nextState = OpcodeState;
212 DPRINTF(Predecoder, "Found three byte opcode.\n");
213 emi.opcode.prefixB = nextByte;
214 }
215 else
216 {
217 DPRINTF(Predecoder, "Found opcode %#x.\n", nextByte);
218 emi.opcode.op = nextByte;
219
220 //Figure out the effective operand size. This can be overriden to
221 //a fixed value at the decoder level.
222 int logOpSize;
223 if (emi.rex.w)
224 logOpSize = 3; // 64 bit operand size
225 else if (emi.legacy.op)
226 logOpSize = m5Reg.altOp;
227 else
228 logOpSize = m5Reg.defOp;
229
230 //Set the actual op size
231 emi.opSize = 1 << logOpSize;
232
233 //Figure out the effective address size. This can be overriden to
234 //a fixed value at the decoder level.
235 int logAddrSize;
236 if(emi.legacy.addr)
237 logAddrSize = m5Reg.altAddr;
238 else
239 logAddrSize = m5Reg.defAddr;
240
241 //Set the actual address size
242 emi.addrSize = 1 << logAddrSize;
243
244 //Figure out the effective stack width. This can be overriden to
245 //a fixed value at the decoder level.
246 emi.stackSize = 1 << m5Reg.stack;
247
248 //Figure out how big of an immediate we'll retreive based
249 //on the opcode.
250 int immType = ImmediateType[emi.opcode.num - 1][nextByte];
251 if (emi.opcode.num == 1 && nextByte >= 0xA0 && nextByte <= 0xA3)
252 immediateSize = SizeTypeToSize[logAddrSize - 1][immType];
253 else
254 immediateSize = SizeTypeToSize[logOpSize - 1][immType];
255
256 //Determine what to expect next
257 if (UsesModRM[emi.opcode.num - 1][nextByte]) {
258 nextState = ModRMState;
259 } else {
260 if(immediateSize) {
261 nextState = ImmediateState;
262 } else {
263 emiIsReady = true;
264 nextState = ResetState;
265 }
266 }
267 }
268 return nextState;
269 }
270
271 //Get the ModRM byte and determine what displacement, if any, there is.
272 //Also determine whether or not to get the SIB byte, displacement, or
273 //immediate next.
274 Predecoder::State Predecoder::doModRMState(uint8_t nextByte)
275 {
276 State nextState = ErrorState;
277 ModRM modRM;
278 modRM = nextByte;
279 DPRINTF(Predecoder, "Found modrm byte %#x.\n", nextByte);
280 if (m5Reg.defOp == 1) {
281 //figure out 16 bit displacement size
282 if ((modRM.mod == 0 && modRM.rm == 6) || modRM.mod == 2)
283 displacementSize = 2;
284 else if (modRM.mod == 1)
285 displacementSize = 1;
286 else
287 displacementSize = 0;
288 } else {
289 //figure out 32/64 bit displacement size
290 if ((modRM.mod == 0 && modRM.rm == 5) || modRM.mod == 2)
291 displacementSize = 4;
292 else if (modRM.mod == 1)
293 displacementSize = 1;
294 else
295 displacementSize = 0;
296 }
297
298 // The "test" instruction in group 3 needs an immediate, even though
299 // the other instructions with the same actual opcode don't.
300 if (emi.opcode.num == 1 && (modRM.reg & 0x6) == 0) {
301 if (emi.opcode.op == 0xF6)
302 immediateSize = 1;
303 else if (emi.opcode.op == 0xF7)
304 immediateSize = (emi.opSize == 8) ? 4 : emi.opSize;
305 }
306
307 //If there's an SIB, get that next.
308 //There is no SIB in 16 bit mode.
309 if (modRM.rm == 4 && modRM.mod != 3) {
310 // && in 32/64 bit mode)
311 nextState = SIBState;
312 } else if(displacementSize) {
313 nextState = DisplacementState;
314 } else if(immediateSize) {
315 nextState = ImmediateState;
316 } else {
317 emiIsReady = true;
318 nextState = ResetState;
319 }
320 //The ModRM byte is consumed no matter what
321 consumeByte();
322 emi.modRM = modRM;
323 return nextState;
324 }
325
326 //Get the SIB byte. We don't do anything with it at this point, other
327 //than storing it in the ExtMachInst. Determine if we need to get a
328 //displacement or immediate next.
329 Predecoder::State Predecoder::doSIBState(uint8_t nextByte)
330 {
331 State nextState = ErrorState;
332 emi.sib = nextByte;
333 DPRINTF(Predecoder, "Found SIB byte %#x.\n", nextByte);
334 consumeByte();
335 if (emi.modRM.mod == 0 && emi.sib.base == 5)
336 displacementSize = 4;
337 if (displacementSize) {
338 nextState = DisplacementState;
339 } else if(immediateSize) {
340 nextState = ImmediateState;
341 } else {
342 emiIsReady = true;
343 nextState = ResetState;
344 }
345 return nextState;
346 }
347
348 //Gather up the displacement, or at least as much of it
349 //as we can get.
350 Predecoder::State Predecoder::doDisplacementState()
351 {
352 State nextState = ErrorState;
353
354 getImmediate(immediateCollected,
355 emi.displacement,
356 displacementSize);
357
358 DPRINTF(Predecoder, "Collecting %d byte displacement, got %d bytes.\n",
359 displacementSize, immediateCollected);
360
361 if(displacementSize == immediateCollected) {
362 //Reset this for other immediates.
363 immediateCollected = 0;
364 //Sign extend the displacement
365 switch(displacementSize)
366 {
367 case 1:
368 emi.displacement = sext<8>(emi.displacement);
369 break;
370 case 2:
371 emi.displacement = sext<16>(emi.displacement);
372 break;
373 case 4:
374 emi.displacement = sext<32>(emi.displacement);
375 break;
376 default:
377 panic("Undefined displacement size!\n");
378 }
379 DPRINTF(Predecoder, "Collected displacement %#x.\n",
380 emi.displacement);
381 if(immediateSize) {
382 nextState = ImmediateState;
383 } else {
384 emiIsReady = true;
385 nextState = ResetState;
386 }
387
388 emi.dispSize = displacementSize;
389 }
390 else
391 nextState = DisplacementState;
392 return nextState;
393 }
394
395 //Gather up the immediate, or at least as much of it
396 //as we can get
397 Predecoder::State Predecoder::doImmediateState()
398 {
399 State nextState = ErrorState;
400
401 getImmediate(immediateCollected,
402 emi.immediate,
403 immediateSize);
404
405 DPRINTF(Predecoder, "Collecting %d byte immediate, got %d bytes.\n",
406 immediateSize, immediateCollected);
407
408 if(immediateSize == immediateCollected)
409 {
410 //Reset this for other immediates.
411 immediateCollected = 0;
412
413 //XXX Warning! The following is an observed pattern and might
414 //not always be true!
415
416 //Instructions which use 64 bit operands but 32 bit immediates
417 //need to have the immediate sign extended to 64 bits.
418 //Instructions which use true 64 bit immediates won't be
419 //affected, and instructions that use true 32 bit immediates
420 //won't notice.
421 switch(immediateSize)
422 {
423 case 4:
424 emi.immediate = sext<32>(emi.immediate);
425 break;
426 case 1:
427 emi.immediate = sext<8>(emi.immediate);
428 }
429
430 DPRINTF(Predecoder, "Collected immediate %#x.\n",
431 emi.immediate);
432 emiIsReady = true;
433 nextState = ResetState;
434 }
435 else
436 nextState = ImmediateState;
437 return nextState;
438 }
439 }