2 * Copyright (c) 2012 Google
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.
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.
31 #ifndef __ARCH_X86_DECODER_HH__
32 #define __ARCH_X86_DECODER_HH__
37 #include "arch/x86/regs/misc.hh"
38 #include "arch/x86/types.hh"
39 #include "base/bitfield.hh"
40 #include "base/misc.hh"
41 #include "base/trace.hh"
42 #include "base/types.hh"
43 #include "cpu/decode_cache.hh"
44 #include "cpu/static_inst.hh"
45 #include "debug/Decoder.hh"
53 //These are defined and documented in decoder_tables.cc
54 static const uint8_t Prefixes[256];
55 static const uint8_t UsesModRM[2][256];
56 static const uint8_t ImmediateType[2][256];
57 static const uint8_t SizeTypeToSize[3][10];
63 std::vector<MachInst> chunks;
64 std::vector<MachInst> masks;
67 InstBytes() : lastOffset(0)
71 static InstBytes dummy;
73 //The bytes to be predecoded
77 //The pc of the start of fetchChunk
79 //The pc the current instruction started at
81 //The offset into fetchChunk of current processing
83 //The extended machine instruction being generated
96 return ((uint8_t *)&fetchChunk)[offset];
99 void getImmediate(int &collected, uint64_t ¤t, int size)
101 //Figure out how many bytes we still need to get for the
103 int toGet = size - collected;
104 //Figure out how many bytes are left in our "buffer"
105 int remaining = sizeof(MachInst) - offset;
106 //Get as much as we need, up to the amount available.
107 toGet = toGet > remaining ? remaining : toGet;
109 //Shift the bytes we want to be all the way to the right
110 uint64_t partialImm = fetchChunk >> (offset * 8);
111 //Mask off what we don't want
112 partialImm &= mask(toGet * 8);
113 //Shift it over to overlay with our displacement.
114 partialImm <<= (immediateCollected * 8);
115 //Put it into our displacement
116 current |= partialImm;
117 //Update how many bytes we've collected.
122 void updateOffsetState()
124 assert(offset <= sizeof(MachInst));
125 if (offset == sizeof(MachInst)) {
126 DPRINTF(Decoder, "At the end of a chunk, idx = %d, chunks = %d.\n",
127 chunkIdx, instBytes->chunks.size());
129 if (chunkIdx == instBytes->chunks.size()) {
133 fetchChunk = instBytes->chunks[chunkIdx];
134 basePC += sizeof(MachInst);
145 void consumeBytes(int numBytes)
151 //State machine state
153 //Whether or not we're out of bytes
155 //Whether we've completed generating an ExtMachInst
157 //The size of the displacement value
158 int displacementSize;
159 //The size of the immediate value
161 //This is how much of any immediate value we've gotten. This is used
162 //for both the actual immediate and the displacement.
163 int immediateCollected;
174 //We should never get to this state. Getting here is an error.
180 //Functions to handle each of the states
181 State doResetState();
182 State doFromCacheState();
183 State doPrefixState(uint8_t);
184 State doOpcodeState(uint8_t);
185 State doModRMState(uint8_t);
186 State doSIBState(uint8_t);
187 State doDisplacementState();
188 State doImmediateState();
191 /// Caching for decoded instruction objects.
193 typedef MiscReg CacheKey;
195 typedef DecodeCache::AddrMap<Decoder::InstBytes> DecodePages;
196 DecodePages *decodePages;
197 typedef m5::hash_map<CacheKey, DecodePages *> AddrCacheMap;
198 AddrCacheMap addrCacheMap;
200 DecodeCache::InstMap *instMap;
201 typedef m5::hash_map<CacheKey, DecodeCache::InstMap *> InstCacheMap;
202 static InstCacheMap instCacheMap;
205 Decoder() : basePC(0), origPC(0), offset(0),
206 outOfBytes(true), instDone(false),
209 memset(&emi, 0, sizeof(emi));
211 submode = SixtyFourBitMode;
212 emi.mode.mode = mode;
213 emi.mode.submode = submode;
224 void setM5Reg(HandyM5Reg m5Reg)
226 mode = (X86Mode)(uint64_t)m5Reg.mode;
227 submode = (X86SubMode)(uint64_t)m5Reg.submode;
228 emi.mode.mode = mode;
229 emi.mode.submode = submode;
232 altAddr = m5Reg.altAddr;
233 defAddr = m5Reg.defAddr;
236 AddrCacheMap::iterator amIter = addrCacheMap.find(m5Reg);
237 if (amIter != addrCacheMap.end()) {
238 decodePages = amIter->second;
240 decodePages = new DecodePages;
241 addrCacheMap[m5Reg] = decodePages;
244 InstCacheMap::iterator imIter = instCacheMap.find(m5Reg);
245 if (imIter != instCacheMap.end()) {
246 instMap = imIter->second;
248 instMap = new DecodeCache::InstMap;
249 instCacheMap[m5Reg] = instMap;
253 void takeOverFrom(Decoder *old)
256 submode = old->submode;
257 emi.mode.mode = mode;
258 emi.mode.submode = submode;
261 altAddr = old->altAddr;
262 defAddr = old->defAddr;
273 //Use this to give data to the decoder. This should be used
274 //when there is control flow.
275 void moreBytes(const PCState &pc, Addr fetchPC, MachInst data)
277 DPRINTF(Decoder, "Getting more bytes.\n");
279 offset = (fetchPC >= pc.instAddr()) ? 0 : pc.instAddr() - fetchPC;
296 updateNPC(X86ISA::PCState &nextPC)
298 if (!nextPC.size()) {
299 int size = basePC + offset - origPC;
301 "Calculating the instruction size: "
302 "basePC: %#x offset: %#x origPC: %#x size: %d\n",
303 basePC, offset, origPC, size);
305 nextPC.npc(nextPC.pc() + size);
310 StaticInstPtr decodeInst(ExtMachInst mach_inst);
312 /// Decode a machine instruction.
313 /// @param mach_inst The binary instruction to decode.
314 /// @retval A pointer to the corresponding StaticInst object.
315 StaticInstPtr decode(ExtMachInst mach_inst, Addr addr);
316 StaticInstPtr decode(X86ISA::PCState &nextPC);
319 } // namespace X86ISA
321 #endif // __ARCH_X86_DECODER_HH__