arch: Eliminate the unused HasUnalignedMemAcc constant.
[gem5.git] / src / arch / x86 / decoder.cc
1 /*
2 * Copyright (c) 2011 Google
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "arch/x86/decoder.hh"
30
31 #include "arch/x86/regs/misc.hh"
32 #include "base/logging.hh"
33 #include "base/trace.hh"
34 #include "base/types.hh"
35 #include "debug/Decoder.hh"
36
37 namespace X86ISA
38 {
39
40 Decoder::State
41 Decoder::doResetState()
42 {
43 origPC = basePC + offset;
44 DPRINTF(Decoder, "Setting origPC to %#x\n", origPC);
45 instBytes = &decodePages->lookup(origPC);
46 chunkIdx = 0;
47
48 emi.rex = 0;
49 emi.legacy = 0;
50 emi.vex = 0;
51
52 emi.opcode.type = BadOpcode;
53 emi.opcode.op = 0;
54
55 immediateCollected = 0;
56 emi.immediate = 0;
57 emi.displacement = 0;
58 emi.dispSize = 0;
59
60 emi.modRM = 0;
61 emi.sib = 0;
62
63 if (instBytes->si) {
64 return FromCacheState;
65 } else {
66 instBytes->chunks.clear();
67 return PrefixState;
68 }
69 }
70
71 void
72 Decoder::process()
73 {
74 // This function drives the decoder state machine.
75
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 decoded
78 // ExtMachInst.
79 assert(!outOfBytes);
80 assert(!instDone);
81
82 if (state == ResetState)
83 state = doResetState();
84 if (state == FromCacheState) {
85 state = doFromCacheState();
86 } else {
87 instBytes->chunks.push_back(fetchChunk);
88 }
89
90 // While there's still something to do...
91 while (!instDone && !outOfBytes) {
92 uint8_t nextByte = getNextByte();
93 switch (state) {
94 case PrefixState:
95 state = doPrefixState(nextByte);
96 break;
97 case Vex2Of2State:
98 state = doVex2Of2State(nextByte);
99 break;
100 case Vex2Of3State:
101 state = doVex2Of3State(nextByte);
102 break;
103 case Vex3Of3State:
104 state = doVex3Of3State(nextByte);
105 break;
106 case VexOpcodeState:
107 state = doVexOpcodeState(nextByte);
108 break;
109 case OneByteOpcodeState:
110 state = doOneByteOpcodeState(nextByte);
111 break;
112 case TwoByteOpcodeState:
113 state = doTwoByteOpcodeState(nextByte);
114 break;
115 case ThreeByte0F38OpcodeState:
116 state = doThreeByte0F38OpcodeState(nextByte);
117 break;
118 case ThreeByte0F3AOpcodeState:
119 state = doThreeByte0F3AOpcodeState(nextByte);
120 break;
121 case ModRMState:
122 state = doModRMState(nextByte);
123 break;
124 case SIBState:
125 state = doSIBState(nextByte);
126 break;
127 case DisplacementState:
128 state = doDisplacementState();
129 break;
130 case ImmediateState:
131 state = doImmediateState();
132 break;
133 case ErrorState:
134 panic("Went to the error state in the decoder.\n");
135 default:
136 panic("Unrecognized state! %d\n", state);
137 }
138 }
139 }
140
141 Decoder::State
142 Decoder::doFromCacheState()
143 {
144 DPRINTF(Decoder, "Looking at cache state.\n");
145 if ((fetchChunk & instBytes->masks[chunkIdx]) !=
146 instBytes->chunks[chunkIdx]) {
147 DPRINTF(Decoder, "Decode cache miss.\n");
148 // The chached chunks didn't match what was fetched. Fall back to the
149 // predecoder.
150 instBytes->chunks[chunkIdx] = fetchChunk;
151 instBytes->chunks.resize(chunkIdx + 1);
152 instBytes->si = NULL;
153 chunkIdx = 0;
154 fetchChunk = instBytes->chunks[0];
155 offset = origPC % sizeof(MachInst);
156 basePC = origPC - offset;
157 return PrefixState;
158 } else if (chunkIdx == instBytes->chunks.size() - 1) {
159 // We matched the cache, so use its value.
160 instDone = true;
161 offset = instBytes->lastOffset;
162 if (offset == sizeof(MachInst))
163 outOfBytes = true;
164 return ResetState;
165 } else {
166 // We matched so far, but need to check more chunks.
167 chunkIdx++;
168 outOfBytes = true;
169 return FromCacheState;
170 }
171 }
172
173 // Either get a prefix and record it in the ExtMachInst, or send the
174 // state machine on to get the opcode(s).
175 Decoder::State
176 Decoder::doPrefixState(uint8_t nextByte)
177 {
178 uint8_t prefix = Prefixes[nextByte];
179 State nextState = PrefixState;
180 // REX prefixes are only recognized in 64 bit mode.
181 if (prefix == RexPrefix && emi.mode.submode != SixtyFourBitMode)
182 prefix = 0;
183 if (prefix)
184 consumeByte();
185 switch(prefix) {
186 // Operand size override prefixes
187 case OperandSizeOverride:
188 DPRINTF(Decoder, "Found operand size override prefix.\n");
189 emi.legacy.op = true;
190 break;
191 case AddressSizeOverride:
192 DPRINTF(Decoder, "Found address size override prefix.\n");
193 emi.legacy.addr = true;
194 break;
195 // Segment override prefixes
196 case CSOverride:
197 case DSOverride:
198 case ESOverride:
199 case FSOverride:
200 case GSOverride:
201 case SSOverride:
202 DPRINTF(Decoder, "Found segment override.\n");
203 emi.legacy.seg = prefix;
204 break;
205 case Lock:
206 DPRINTF(Decoder, "Found lock prefix.\n");
207 emi.legacy.lock = true;
208 break;
209 case Rep:
210 DPRINTF(Decoder, "Found rep prefix.\n");
211 emi.legacy.rep = true;
212 break;
213 case Repne:
214 DPRINTF(Decoder, "Found repne prefix.\n");
215 emi.legacy.repne = true;
216 break;
217 case RexPrefix:
218 DPRINTF(Decoder, "Found Rex prefix %#x.\n", nextByte);
219 emi.rex = nextByte;
220 break;
221 case Vex2Prefix:
222 DPRINTF(Decoder, "Found VEX two-byte prefix %#x.\n", nextByte);
223 emi.vex.present = 1;
224 nextState = Vex2Of2State;
225 break;
226 case Vex3Prefix:
227 DPRINTF(Decoder, "Found VEX three-byte prefix %#x.\n", nextByte);
228 emi.vex.present = 1;
229 nextState = Vex2Of3State;
230 break;
231 case 0:
232 nextState = OneByteOpcodeState;
233 break;
234
235 default:
236 panic("Unrecognized prefix %#x\n", nextByte);
237 }
238 return nextState;
239 }
240
241 Decoder::State
242 Decoder::doVex2Of2State(uint8_t nextByte)
243 {
244 consumeByte();
245 Vex2Of2 vex = nextByte;
246
247 emi.rex.r = !vex.r;
248
249 emi.vex.l = vex.l;
250 emi.vex.v = ~vex.v;
251
252 switch (vex.p) {
253 case 0:
254 break;
255 case 1:
256 emi.legacy.op = 1;
257 break;
258 case 2:
259 emi.legacy.rep = 1;
260 break;
261 case 3:
262 emi.legacy.repne = 1;
263 break;
264 }
265
266 emi.opcode.type = TwoByteOpcode;
267
268 return VexOpcodeState;
269 }
270
271 Decoder::State
272 Decoder::doVex2Of3State(uint8_t nextByte)
273 {
274 if (emi.mode.submode != SixtyFourBitMode && bits(nextByte, 7, 6) == 0x3) {
275 // This was actually an LDS instruction. Reroute to that path.
276 emi.vex.present = 0;
277 emi.opcode.type = OneByteOpcode;
278 emi.opcode.op = 0xC4;
279 return processOpcode(ImmediateTypeOneByte, UsesModRMOneByte,
280 nextByte >= 0xA0 && nextByte <= 0xA3);
281 }
282
283 consumeByte();
284 Vex2Of3 vex = nextByte;
285
286 emi.rex.r = !vex.r;
287 emi.rex.x = !vex.x;
288 emi.rex.b = !vex.b;
289
290 switch (vex.m) {
291 case 1:
292 emi.opcode.type = TwoByteOpcode;
293 break;
294 case 2:
295 emi.opcode.type = ThreeByte0F38Opcode;
296 break;
297 case 3:
298 emi.opcode.type = ThreeByte0F3AOpcode;
299 break;
300 default:
301 // These encodings are reserved. Pretend this was an undefined
302 // instruction so the main decoder will behave correctly, and stop
303 // trying to interpret bytes.
304 emi.opcode.type = TwoByteOpcode;
305 emi.opcode.op = 0x0B;
306 instDone = true;
307 return ResetState;
308 }
309 return Vex3Of3State;
310 }
311
312 Decoder::State
313 Decoder::doVex3Of3State(uint8_t nextByte)
314 {
315 if (emi.mode.submode != SixtyFourBitMode && bits(nextByte, 7, 6) == 0x3) {
316 // This was actually an LES instruction. Reroute to that path.
317 emi.vex.present = 0;
318 emi.opcode.type = OneByteOpcode;
319 emi.opcode.op = 0xC5;
320 return processOpcode(ImmediateTypeOneByte, UsesModRMOneByte,
321 nextByte >= 0xA0 && nextByte <= 0xA3);
322 }
323
324 consumeByte();
325 Vex3Of3 vex = nextByte;
326
327 emi.rex.w = vex.w;
328
329 emi.vex.l = vex.l;
330 emi.vex.v = ~vex.v;
331
332 switch (vex.p) {
333 case 0:
334 break;
335 case 1:
336 emi.legacy.op = 1;
337 break;
338 case 2:
339 emi.legacy.rep = 1;
340 break;
341 case 3:
342 emi.legacy.repne = 1;
343 break;
344 }
345
346 return VexOpcodeState;
347 }
348
349 Decoder::State
350 Decoder::doVexOpcodeState(uint8_t nextByte)
351 {
352 DPRINTF(Decoder, "Found VEX opcode %#x.\n", nextByte);
353
354 emi.opcode.op = nextByte;
355 consumeByte();
356
357 switch (emi.opcode.type) {
358 case TwoByteOpcode:
359 return processOpcode(ImmediateTypeTwoByte, UsesModRMTwoByte);
360 case ThreeByte0F38Opcode:
361 return processOpcode(ImmediateTypeThreeByte0F38,
362 UsesModRMThreeByte0F38);
363 case ThreeByte0F3AOpcode:
364 return processOpcode(ImmediateTypeThreeByte0F3A,
365 UsesModRMThreeByte0F3A);
366 default:
367 panic("Unrecognized opcode type %d.\n", emi.opcode.type);
368 }
369 }
370
371 // Load the first opcode byte. Determine if there are more opcode bytes, and
372 // if not, what immediate and/or ModRM is needed.
373 Decoder::State
374 Decoder::doOneByteOpcodeState(uint8_t nextByte)
375 {
376 State nextState = ErrorState;
377 consumeByte();
378
379 if (nextByte == 0x0f) {
380 DPRINTF(Decoder, "Found opcode escape byte %#x.\n", nextByte);
381 nextState = TwoByteOpcodeState;
382 } else {
383 DPRINTF(Decoder, "Found one byte opcode %#x.\n", nextByte);
384 emi.opcode.type = OneByteOpcode;
385 emi.opcode.op = nextByte;
386
387 nextState = processOpcode(ImmediateTypeOneByte, UsesModRMOneByte,
388 nextByte >= 0xA0 && nextByte <= 0xA3);
389 }
390 return nextState;
391 }
392
393 // Load the second opcode byte. Determine if there are more opcode bytes, and
394 // if not, what immediate and/or ModRM is needed.
395 Decoder::State
396 Decoder::doTwoByteOpcodeState(uint8_t nextByte)
397 {
398 State nextState = ErrorState;
399 consumeByte();
400 if (nextByte == 0x38) {
401 nextState = ThreeByte0F38OpcodeState;
402 DPRINTF(Decoder, "Found opcode escape byte %#x.\n", nextByte);
403 } else if (nextByte == 0x3a) {
404 nextState = ThreeByte0F3AOpcodeState;
405 DPRINTF(Decoder, "Found opcode escape byte %#x.\n", nextByte);
406 } else {
407 DPRINTF(Decoder, "Found two byte opcode %#x.\n", nextByte);
408 emi.opcode.type = TwoByteOpcode;
409 emi.opcode.op = nextByte;
410
411 nextState = processOpcode(ImmediateTypeTwoByte, UsesModRMTwoByte);
412 }
413 return nextState;
414 }
415
416 // Load the third opcode byte and determine what immediate and/or ModRM is
417 // needed.
418 Decoder::State
419 Decoder::doThreeByte0F38OpcodeState(uint8_t nextByte)
420 {
421 consumeByte();
422
423 DPRINTF(Decoder, "Found three byte 0F38 opcode %#x.\n", nextByte);
424 emi.opcode.type = ThreeByte0F38Opcode;
425 emi.opcode.op = nextByte;
426
427 return processOpcode(ImmediateTypeThreeByte0F38, UsesModRMThreeByte0F38);
428 }
429
430 // Load the third opcode byte and determine what immediate and/or ModRM is
431 // needed.
432 Decoder::State
433 Decoder::doThreeByte0F3AOpcodeState(uint8_t nextByte)
434 {
435 consumeByte();
436
437 DPRINTF(Decoder, "Found three byte 0F3A opcode %#x.\n", nextByte);
438 emi.opcode.type = ThreeByte0F3AOpcode;
439 emi.opcode.op = nextByte;
440
441 return processOpcode(ImmediateTypeThreeByte0F3A, UsesModRMThreeByte0F3A);
442 }
443
444 // Generic opcode processing which determines the immediate size, and whether
445 // or not there's a modrm byte.
446 Decoder::State
447 Decoder::processOpcode(ByteTable &immTable, ByteTable &modrmTable,
448 bool addrSizedImm)
449 {
450 State nextState = ErrorState;
451 const uint8_t opcode = emi.opcode.op;
452
453 // Figure out the effective operand size. This can be overriden to
454 // a fixed value at the decoder level.
455 int logOpSize;
456 if (emi.rex.w)
457 logOpSize = 3; // 64 bit operand size
458 else if (emi.legacy.op)
459 logOpSize = altOp;
460 else
461 logOpSize = defOp;
462
463 // Set the actual op size.
464 emi.opSize = 1 << logOpSize;
465
466 // Figure out the effective address size. This can be overriden to
467 // a fixed value at the decoder level.
468 int logAddrSize;
469 if (emi.legacy.addr)
470 logAddrSize = altAddr;
471 else
472 logAddrSize = defAddr;
473
474 // Set the actual address size.
475 emi.addrSize = 1 << logAddrSize;
476
477 // Figure out the effective stack width. This can be overriden to
478 // a fixed value at the decoder level.
479 emi.stackSize = 1 << stack;
480
481 // Figure out how big of an immediate we'll retreive based
482 // on the opcode.
483 int immType = immTable[opcode];
484 if (addrSizedImm)
485 immediateSize = SizeTypeToSize[logAddrSize - 1][immType];
486 else
487 immediateSize = SizeTypeToSize[logOpSize - 1][immType];
488
489 // Determine what to expect next.
490 if (modrmTable[opcode]) {
491 nextState = ModRMState;
492 } else {
493 if (immediateSize) {
494 nextState = ImmediateState;
495 } else {
496 instDone = true;
497 nextState = ResetState;
498 }
499 }
500 return nextState;
501 }
502
503 // Get the ModRM byte and determine what displacement, if any, there is.
504 // Also determine whether or not to get the SIB byte, displacement, or
505 // immediate next.
506 Decoder::State
507 Decoder::doModRMState(uint8_t nextByte)
508 {
509 State nextState = ErrorState;
510 ModRM modRM = nextByte;
511 DPRINTF(Decoder, "Found modrm byte %#x.\n", nextByte);
512 if (defOp == 1) {
513 // Figure out 16 bit displacement size.
514 if ((modRM.mod == 0 && modRM.rm == 6) || modRM.mod == 2)
515 displacementSize = 2;
516 else if (modRM.mod == 1)
517 displacementSize = 1;
518 else
519 displacementSize = 0;
520 } else {
521 // Figure out 32/64 bit displacement size.
522 if ((modRM.mod == 0 && modRM.rm == 5) || modRM.mod == 2)
523 displacementSize = 4;
524 else if (modRM.mod == 1)
525 displacementSize = 1;
526 else
527 displacementSize = 0;
528 }
529
530 // The "test" instruction in group 3 needs an immediate, even though
531 // the other instructions with the same actual opcode don't.
532 if (emi.opcode.type == OneByteOpcode && (modRM.reg & 0x6) == 0) {
533 if (emi.opcode.op == 0xF6)
534 immediateSize = 1;
535 else if (emi.opcode.op == 0xF7)
536 immediateSize = (emi.opSize == 8) ? 4 : emi.opSize;
537 }
538
539 // If there's an SIB, get that next.
540 // There is no SIB in 16 bit mode.
541 if (modRM.rm == 4 && modRM.mod != 3) {
542 // && in 32/64 bit mode)
543 nextState = SIBState;
544 } else if (displacementSize) {
545 nextState = DisplacementState;
546 } else if (immediateSize) {
547 nextState = ImmediateState;
548 } else {
549 instDone = true;
550 nextState = ResetState;
551 }
552 // The ModRM byte is consumed no matter what.
553 consumeByte();
554 emi.modRM = modRM;
555 return nextState;
556 }
557
558 // Get the SIB byte. We don't do anything with it at this point, other
559 // than storing it in the ExtMachInst. Determine if we need to get a
560 // displacement or immediate next.
561 Decoder::State
562 Decoder::doSIBState(uint8_t nextByte)
563 {
564 State nextState = ErrorState;
565 emi.sib = nextByte;
566 DPRINTF(Decoder, "Found SIB byte %#x.\n", nextByte);
567 consumeByte();
568 if (emi.modRM.mod == 0 && emi.sib.base == 5)
569 displacementSize = 4;
570 if (displacementSize) {
571 nextState = DisplacementState;
572 } else if (immediateSize) {
573 nextState = ImmediateState;
574 } else {
575 instDone = true;
576 nextState = ResetState;
577 }
578 return nextState;
579 }
580
581 // Gather up the displacement, or at least as much of it as we can get.
582 Decoder::State
583 Decoder::doDisplacementState()
584 {
585 State nextState = ErrorState;
586
587 getImmediate(immediateCollected,
588 emi.displacement,
589 displacementSize);
590
591 DPRINTF(Decoder, "Collecting %d byte displacement, got %d bytes.\n",
592 displacementSize, immediateCollected);
593
594 if (displacementSize == immediateCollected) {
595 // Reset this for other immediates.
596 immediateCollected = 0;
597 // Sign extend the displacement.
598 switch(displacementSize)
599 {
600 case 1:
601 emi.displacement = sext<8>(emi.displacement);
602 break;
603 case 2:
604 emi.displacement = sext<16>(emi.displacement);
605 break;
606 case 4:
607 emi.displacement = sext<32>(emi.displacement);
608 break;
609 default:
610 panic("Undefined displacement size!\n");
611 }
612 DPRINTF(Decoder, "Collected displacement %#x.\n",
613 emi.displacement);
614 if (immediateSize) {
615 nextState = ImmediateState;
616 } else {
617 instDone = true;
618 nextState = ResetState;
619 }
620
621 emi.dispSize = displacementSize;
622 }
623 else
624 nextState = DisplacementState;
625 return nextState;
626 }
627
628 // Gather up the immediate, or at least as much of it as we can get.
629 Decoder::State
630 Decoder::doImmediateState()
631 {
632 State nextState = ErrorState;
633
634 getImmediate(immediateCollected, emi.immediate, immediateSize);
635
636 DPRINTF(Decoder, "Collecting %d byte immediate, got %d bytes.\n",
637 immediateSize, immediateCollected);
638
639 if (immediateSize == immediateCollected) {
640 // Reset this for other immediates.
641 immediateCollected = 0;
642
643 //XXX Warning! The following is an observed pattern and might
644 // not always be true!
645
646 // Instructions which use 64 bit operands but 32 bit immediates
647 // need to have the immediate sign extended to 64 bits.
648 // Instructions which use true 64 bit immediates won't be
649 // affected, and instructions that use true 32 bit immediates
650 // won't notice.
651 switch(immediateSize) {
652 case 4:
653 emi.immediate = sext<32>(emi.immediate);
654 break;
655 case 1:
656 emi.immediate = sext<8>(emi.immediate);
657 }
658
659 DPRINTF(Decoder, "Collected immediate %#x.\n",
660 emi.immediate);
661 instDone = true;
662 nextState = ResetState;
663 } else {
664 nextState = ImmediateState;
665 }
666 return nextState;
667 }
668
669 Decoder::InstBytes Decoder::dummy;
670 Decoder::InstCacheMap Decoder::instCacheMap;
671
672 StaticInstPtr
673 Decoder::decode(ExtMachInst mach_inst, Addr addr)
674 {
675 auto iter = instMap->find(mach_inst);
676 if (iter != instMap->end())
677 return iter->second;
678
679 StaticInstPtr si = decodeInst(mach_inst);
680 (*instMap)[mach_inst] = si;
681 return si;
682 }
683
684 StaticInstPtr
685 Decoder::decode(PCState &nextPC)
686 {
687 if (!instDone)
688 return NULL;
689 instDone = false;
690 updateNPC(nextPC);
691
692 StaticInstPtr &si = instBytes->si;
693 if (si)
694 return si;
695
696 // We didn't match in the AddrMap, but we still populated an entry. Fix
697 // up its byte masks.
698 const int chunkSize = sizeof(MachInst);
699
700 instBytes->lastOffset = offset;
701
702 Addr firstBasePC = basePC - (instBytes->chunks.size() - 1) * chunkSize;
703 Addr firstOffset = origPC - firstBasePC;
704 Addr totalSize = instBytes->lastOffset - firstOffset +
705 (instBytes->chunks.size() - 1) * chunkSize;
706 int start = firstOffset;
707 instBytes->masks.clear();
708
709 while (totalSize) {
710 int end = start + totalSize;
711 end = (chunkSize < end) ? chunkSize : end;
712 int size = end - start;
713 int idx = instBytes->masks.size();
714
715 MachInst maskVal = mask(size * 8) << (start * 8);
716 assert(maskVal);
717
718 instBytes->masks.push_back(maskVal);
719 instBytes->chunks[idx] &= instBytes->masks[idx];
720 totalSize -= size;
721 start = 0;
722 }
723
724 si = decode(emi, origPC);
725 return si;
726 }
727
728 }