ARM: move kernel func event to correct location.
[gem5.git] / src / arch / x86 / predecoder.hh
1 /*
2 * Copyright (c) 2007 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
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.
13 *
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.
24 *
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.
36 *
37 * Authors: Gabe Black
38 */
39
40 #ifndef __ARCH_X86_PREDECODER_HH__
41 #define __ARCH_X86_PREDECODER_HH__
42
43 #include <cassert>
44
45 #include "arch/x86/regs/misc.hh"
46 #include "arch/x86/types.hh"
47 #include "base/bitfield.hh"
48 #include "base/misc.hh"
49 #include "base/trace.hh"
50 #include "base/types.hh"
51 #include "debug/Predecoder.hh"
52
53 class ThreadContext;
54
55 namespace X86ISA
56 {
57 class Predecoder
58 {
59 private:
60 //These are defined and documented in predecoder_tables.cc
61 static const uint8_t Prefixes[256];
62 static const uint8_t UsesModRM[2][256];
63 static const uint8_t ImmediateType[2][256];
64 static const uint8_t SizeTypeToSize[3][10];
65
66 protected:
67 ThreadContext * tc;
68 //The bytes to be predecoded
69 MachInst fetchChunk;
70 //The pc of the start of fetchChunk
71 Addr basePC;
72 //The pc the current instruction started at
73 Addr origPC;
74 //The offset into fetchChunk of current processing
75 int offset;
76 //The extended machine instruction being generated
77 ExtMachInst emi;
78 HandyM5Reg m5Reg;
79
80 inline uint8_t getNextByte()
81 {
82 return ((uint8_t *)&fetchChunk)[offset];
83 }
84
85 void getImmediate(int &collected, uint64_t &current, int size)
86 {
87 //Figure out how many bytes we still need to get for the
88 //immediate.
89 int toGet = size - collected;
90 //Figure out how many bytes are left in our "buffer"
91 int remaining = sizeof(MachInst) - offset;
92 //Get as much as we need, up to the amount available.
93 toGet = toGet > remaining ? remaining : toGet;
94
95 //Shift the bytes we want to be all the way to the right
96 uint64_t partialImm = fetchChunk >> (offset * 8);
97 //Mask off what we don't want
98 partialImm &= mask(toGet * 8);
99 //Shift it over to overlay with our displacement.
100 partialImm <<= (immediateCollected * 8);
101 //Put it into our displacement
102 current |= partialImm;
103 //Update how many bytes we've collected.
104 collected += toGet;
105 consumeBytes(toGet);
106 }
107
108 inline void consumeByte()
109 {
110 offset++;
111 assert(offset <= sizeof(MachInst));
112 if(offset == sizeof(MachInst))
113 outOfBytes = true;
114 }
115
116 inline void consumeBytes(int numBytes)
117 {
118 offset += numBytes;
119 assert(offset <= sizeof(MachInst));
120 if(offset == sizeof(MachInst))
121 outOfBytes = true;
122 }
123
124 void doReset();
125
126 //State machine state
127 protected:
128 //Whether or not we're out of bytes
129 bool outOfBytes;
130 //Whether we've completed generating an ExtMachInst
131 bool emiIsReady;
132 //The size of the displacement value
133 int displacementSize;
134 //The size of the immediate value
135 int immediateSize;
136 //This is how much of any immediate value we've gotten. This is used
137 //for both the actual immediate and the displacement.
138 int immediateCollected;
139
140 enum State {
141 ResetState,
142 PrefixState,
143 OpcodeState,
144 ModRMState,
145 SIBState,
146 DisplacementState,
147 ImmediateState,
148 //We should never get to this state. Getting here is an error.
149 ErrorState
150 };
151
152 State state;
153
154 //Functions to handle each of the states
155 State doPrefixState(uint8_t);
156 State doOpcodeState(uint8_t);
157 State doModRMState(uint8_t);
158 State doSIBState(uint8_t);
159 State doDisplacementState();
160 State doImmediateState();
161
162 public:
163 Predecoder(ThreadContext * _tc) :
164 tc(_tc), basePC(0), origPC(0), offset(0),
165 outOfBytes(true), emiIsReady(false),
166 state(ResetState)
167 {
168 emi.mode.mode = LongMode;
169 emi.mode.submode = SixtyFourBitMode;
170 m5Reg = 0;
171 }
172
173 void reset()
174 {
175 state = ResetState;
176 }
177
178 ThreadContext * getTC()
179 {
180 return tc;
181 }
182
183 void setTC(ThreadContext * _tc)
184 {
185 tc = _tc;
186 }
187
188 void process();
189
190 //Use this to give data to the predecoder. This should be used
191 //when there is control flow.
192 void moreBytes(const PCState &pc, Addr fetchPC, MachInst data)
193 {
194 DPRINTF(Predecoder, "Getting more bytes.\n");
195 basePC = fetchPC;
196 offset = (fetchPC >= pc.instAddr()) ? 0 : pc.instAddr() - fetchPC;
197 fetchChunk = data;
198 outOfBytes = false;
199 process();
200 }
201
202 bool needMoreBytes()
203 {
204 return outOfBytes;
205 }
206
207 bool extMachInstReady()
208 {
209 return emiIsReady;
210 }
211
212 int
213 getInstSize()
214 {
215 int size = basePC + offset - origPC;
216 DPRINTF(Predecoder,
217 "Calculating the instruction size: "
218 "basePC: %#x offset: %#x origPC: %#x size: %d\n",
219 basePC, offset, origPC, size);
220 return size;
221 }
222
223 //This returns a constant reference to the ExtMachInst to avoid a copy
224 const ExtMachInst &
225 getExtMachInst(X86ISA::PCState &nextPC)
226 {
227 assert(emiIsReady);
228 emiIsReady = false;
229 if (!nextPC.size()) {
230 Addr size = getInstSize();
231 nextPC.size(size);
232 nextPC.npc(nextPC.pc() + size);
233 }
234 return emi;
235 }
236 };
237 };
238
239 #endif // __ARCH_X86_PREDECODER_HH__