X86: Detect attempts to load a 32 bit kernel and panic.
[gem5.git] / src / arch / x86 / predecoder.cc
1 /*
2 * Copyright (c) 2007-2008 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 #include "arch/x86/regs/misc.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"
46
47 namespace X86ISA
48 {
49 void Predecoder::doReset()
50 {
51 origPC = basePC + offset;
52 DPRINTF(Predecoder, "Setting origPC to %#x\n", origPC);
53 emi.rex = 0;
54 emi.legacy = 0;
55 emi.opcode.num = 0;
56 emi.opcode.op = 0;
57 emi.opcode.prefixA = emi.opcode.prefixB = 0;
58
59 immediateCollected = 0;
60 emi.immediate = 0;
61 emi.displacement = 0;
62 emi.dispSize = 0;
63
64 emi.modRM = 0;
65 emi.sib = 0;
66 m5Reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
67 emi.mode.mode = m5Reg.mode;
68 emi.mode.submode = m5Reg.submode;
69 }
70
71 void Predecoder::process()
72 {
73 //This function drives the predecoder state machine.
74
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.
78 assert(!outOfBytes);
79 assert(!emiIsReady);
80
81 //While there's still something to do...
82 while(!emiIsReady && !outOfBytes)
83 {
84 uint8_t nextByte = getNextByte();
85 switch(state)
86 {
87 case ResetState:
88 doReset();
89 state = PrefixState;
90 case PrefixState:
91 state = doPrefixState(nextByte);
92 break;
93 case OpcodeState:
94 state = doOpcodeState(nextByte);
95 break;
96 case ModRMState:
97 state = doModRMState(nextByte);
98 break;
99 case SIBState:
100 state = doSIBState(nextByte);
101 break;
102 case DisplacementState:
103 state = doDisplacementState();
104 break;
105 case ImmediateState:
106 state = doImmediateState();
107 break;
108 case ErrorState:
109 panic("Went to the error state in the predecoder.\n");
110 default:
111 panic("Unrecognized state! %d\n", state);
112 }
113 }
114 }
115
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)
119 {
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)
124 prefix = 0;
125 if (prefix)
126 consumeByte();
127 switch(prefix)
128 {
129 //Operand size override prefixes
130 case OperandSizeOverride:
131 DPRINTF(Predecoder, "Found operand size override prefix.\n");
132 emi.legacy.op = true;
133 break;
134 case AddressSizeOverride:
135 DPRINTF(Predecoder, "Found address size override prefix.\n");
136 emi.legacy.addr = true;
137 break;
138 //Segment override prefixes
139 case CSOverride:
140 case DSOverride:
141 case ESOverride:
142 case FSOverride:
143 case GSOverride:
144 case SSOverride:
145 DPRINTF(Predecoder, "Found segment override.\n");
146 emi.legacy.seg = prefix;
147 break;
148 case Lock:
149 DPRINTF(Predecoder, "Found lock prefix.\n");
150 emi.legacy.lock = true;
151 break;
152 case Rep:
153 DPRINTF(Predecoder, "Found rep prefix.\n");
154 emi.legacy.rep = true;
155 break;
156 case Repne:
157 DPRINTF(Predecoder, "Found repne prefix.\n");
158 emi.legacy.repne = true;
159 break;
160 case RexPrefix:
161 DPRINTF(Predecoder, "Found Rex prefix %#x.\n", nextByte);
162 emi.rex = nextByte;
163 break;
164 case 0:
165 nextState = OpcodeState;
166 break;
167 default:
168 panic("Unrecognized prefix %#x\n", nextByte);
169 }
170 return nextState;
171 }
172
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)
176 {
177 State nextState = ErrorState;
178 emi.opcode.num++;
179 //We can't handle 3+ byte opcodes right now
180 assert(emi.opcode.num < 4);
181 consumeByte();
182 if(emi.opcode.num == 1 && nextByte == 0x0f)
183 {
184 nextState = OpcodeState;
185 DPRINTF(Predecoder, "Found two byte opcode.\n");
186 emi.opcode.prefixA = nextByte;
187 }
188 else if(emi.opcode.num == 2 && (nextByte == 0x38 || nextByte == 0x3F))
189 {
190 nextState = OpcodeState;
191 DPRINTF(Predecoder, "Found three byte opcode.\n");
192 emi.opcode.prefixB = nextByte;
193 }
194 else
195 {
196 DPRINTF(Predecoder, "Found opcode %#x.\n", nextByte);
197 emi.opcode.op = nextByte;
198
199 //Figure out the effective operand size. This can be overriden to
200 //a fixed value at the decoder level.
201 int logOpSize;
202 if (emi.rex.w)
203 logOpSize = 3; // 64 bit operand size
204 else if (emi.legacy.op)
205 logOpSize = m5Reg.altOp;
206 else
207 logOpSize = m5Reg.defOp;
208
209 //Set the actual op size
210 emi.opSize = 1 << logOpSize;
211
212 //Figure out the effective address size. This can be overriden to
213 //a fixed value at the decoder level.
214 int logAddrSize;
215 if(emi.legacy.addr)
216 logAddrSize = m5Reg.altAddr;
217 else
218 logAddrSize = m5Reg.defAddr;
219
220 //Set the actual address size
221 emi.addrSize = 1 << logAddrSize;
222
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;
226
227 //Figure out how big of an immediate we'll retreive based
228 //on the opcode.
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];
232 else
233 immediateSize = SizeTypeToSize[logOpSize - 1][immType];
234
235 //Determine what to expect next
236 if (UsesModRM[emi.opcode.num - 1][nextByte]) {
237 nextState = ModRMState;
238 } else {
239 if(immediateSize) {
240 nextState = ImmediateState;
241 } else {
242 emiIsReady = true;
243 nextState = ResetState;
244 }
245 }
246 }
247 return nextState;
248 }
249
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
252 //immediate next.
253 Predecoder::State Predecoder::doModRMState(uint8_t nextByte)
254 {
255 State nextState = ErrorState;
256 ModRM modRM;
257 modRM = nextByte;
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;
265 else
266 displacementSize = 0;
267 } else {
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;
273 else
274 displacementSize = 0;
275 }
276
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)
281 immediateSize = 1;
282 else if (emi.opcode.op == 0xF7)
283 immediateSize = (emi.opSize == 8) ? 4 : emi.opSize;
284 }
285
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;
295 } else {
296 emiIsReady = true;
297 nextState = ResetState;
298 }
299 //The ModRM byte is consumed no matter what
300 consumeByte();
301 emi.modRM = modRM;
302 return nextState;
303 }
304
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)
309 {
310 State nextState = ErrorState;
311 emi.sib = nextByte;
312 DPRINTF(Predecoder, "Found SIB byte %#x.\n", nextByte);
313 consumeByte();
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;
320 } else {
321 emiIsReady = true;
322 nextState = ResetState;
323 }
324 return nextState;
325 }
326
327 //Gather up the displacement, or at least as much of it
328 //as we can get.
329 Predecoder::State Predecoder::doDisplacementState()
330 {
331 State nextState = ErrorState;
332
333 getImmediate(immediateCollected,
334 emi.displacement,
335 displacementSize);
336
337 DPRINTF(Predecoder, "Collecting %d byte displacement, got %d bytes.\n",
338 displacementSize, immediateCollected);
339
340 if(displacementSize == immediateCollected) {
341 //Reset this for other immediates.
342 immediateCollected = 0;
343 //Sign extend the displacement
344 switch(displacementSize)
345 {
346 case 1:
347 emi.displacement = sext<8>(emi.displacement);
348 break;
349 case 2:
350 emi.displacement = sext<16>(emi.displacement);
351 break;
352 case 4:
353 emi.displacement = sext<32>(emi.displacement);
354 break;
355 default:
356 panic("Undefined displacement size!\n");
357 }
358 DPRINTF(Predecoder, "Collected displacement %#x.\n",
359 emi.displacement);
360 if(immediateSize) {
361 nextState = ImmediateState;
362 } else {
363 emiIsReady = true;
364 nextState = ResetState;
365 }
366
367 emi.dispSize = displacementSize;
368 }
369 else
370 nextState = DisplacementState;
371 return nextState;
372 }
373
374 //Gather up the immediate, or at least as much of it
375 //as we can get
376 Predecoder::State Predecoder::doImmediateState()
377 {
378 State nextState = ErrorState;
379
380 getImmediate(immediateCollected,
381 emi.immediate,
382 immediateSize);
383
384 DPRINTF(Predecoder, "Collecting %d byte immediate, got %d bytes.\n",
385 immediateSize, immediateCollected);
386
387 if(immediateSize == immediateCollected)
388 {
389 //Reset this for other immediates.
390 immediateCollected = 0;
391
392 //XXX Warning! The following is an observed pattern and might
393 //not always be true!
394
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
399 //won't notice.
400 switch(immediateSize)
401 {
402 case 4:
403 emi.immediate = sext<32>(emi.immediate);
404 break;
405 case 1:
406 emi.immediate = sext<8>(emi.immediate);
407 }
408
409 DPRINTF(Predecoder, "Collected immediate %#x.\n",
410 emi.immediate);
411 emiIsReady = true;
412 nextState = ResetState;
413 }
414 else
415 nextState = ImmediateState;
416 return nextState;
417 }
418 }