blob: 4df4faec3acf30410400db4a963770d4cb51ba91 [file] [log] [blame]
Greg Clayton64c84432011-01-21 22:02:52 +00001//===-- EmulateInstructionARM.cpp -------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "EmulateInstructionARM.h"
Johnny Chen8584c922011-01-26 01:18:52 +000011#include "ARMDefines.h"
Johnny Chen4baf2e32011-01-24 18:24:53 +000012#include "ARMUtils.h"
Greg Clayton64c84432011-01-21 22:02:52 +000013
14using namespace lldb;
15using namespace lldb_private;
16
17// ARM constants used during decoding
18#define REG_RD 0
19#define LDM_REGLIST 1
20#define PC_REG 15
21#define PC_REGLIST_BIT 0x8000
22
Johnny Chen251af6a2011-01-21 22:47:25 +000023#define ARMv4 (1u << 0)
Greg Clayton64c84432011-01-21 22:02:52 +000024#define ARMv4T (1u << 1)
25#define ARMv5T (1u << 2)
26#define ARMv5TE (1u << 3)
27#define ARMv5TEJ (1u << 4)
Johnny Chen251af6a2011-01-21 22:47:25 +000028#define ARMv6 (1u << 5)
Greg Clayton64c84432011-01-21 22:02:52 +000029#define ARMv6K (1u << 6)
30#define ARMv6T2 (1u << 7)
Johnny Chen251af6a2011-01-21 22:47:25 +000031#define ARMv7 (1u << 8)
Johnny Chen60c0d622011-01-25 23:49:39 +000032#define ARMv8 (1u << 9)
Greg Clayton64c84432011-01-21 22:02:52 +000033#define ARMvAll (0xffffffffu)
34
Johnny Chen7dc60e12011-01-24 19:46:32 +000035typedef enum
Greg Clayton64c84432011-01-21 22:02:52 +000036{
37 eEncodingA1,
38 eEncodingA2,
39 eEncodingA3,
40 eEncodingA4,
41 eEncodingA5,
42 eEncodingT1,
43 eEncodingT2,
44 eEncodingT3,
45 eEncodingT4,
46 eEncodingT5,
47} ARMEncoding;
48
Johnny Chen7dc60e12011-01-24 19:46:32 +000049typedef enum
50{
51 eSize16,
52 eSize32
53} ARMInstrSize;
54
Johnny Chen4baf2e32011-01-24 18:24:53 +000055// Typedef for the callback function used during the emulation.
Johnny Chen3c75c762011-01-22 00:47:08 +000056// Pass along (ARMEncoding)encoding as the callback data.
57typedef bool (*EmulateCallback) (EmulateInstructionARM *emulator, ARMEncoding encoding);
58
Johnny Chen7dc60e12011-01-24 19:46:32 +000059typedef struct
Greg Clayton64c84432011-01-21 22:02:52 +000060{
61 uint32_t mask;
62 uint32_t value;
63 uint32_t variants;
64 ARMEncoding encoding;
Johnny Chen7dc60e12011-01-24 19:46:32 +000065 ARMInstrSize size;
Greg Clayton64c84432011-01-21 22:02:52 +000066 EmulateCallback callback;
Johnny Chen4bee8ce2011-01-22 00:59:07 +000067 const char *name;
Johnny Chen7dc60e12011-01-24 19:46:32 +000068} ARMOpcode;
Greg Clayton64c84432011-01-21 22:02:52 +000069
70static bool
Johnny Chence1ca772011-01-25 01:13:00 +000071emulate_push (EmulateInstructionARM *emulator, ARMEncoding encoding)
Greg Clayton64c84432011-01-21 22:02:52 +000072{
73#if 0
74 // ARM pseudo code...
75 if (ConditionPassed())
76 {
77 EncodingSpecificOperations();
78 NullCheckIfThumbEE(13);
79 address = SP - 4*BitCount(registers);
80
81 for (i = 0 to 14)
82 {
83 if (registers<i> == 1’)
84 {
85 if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1
86 MemA[address,4] = bits(32) UNKNOWN;
87 else
88 MemA[address,4] = R[i];
89 address = address + 4;
90 }
91 }
92
93 if (registers<15> == 1’) // Only possible for encoding A1 or A2
94 MemA[address,4] = PCStoreValue();
95
96 SP = SP - 4*BitCount(registers);
97 }
98#endif
99
100 bool success = false;
101 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
102 if (!success)
103 return false;
104
105 if (emulator->ConditionPassed())
106 {
107 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
108 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
109 if (!success)
110 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000111 uint32_t registers = 0;
Johnny Chen91d99862011-01-25 19:07:04 +0000112 uint32_t Rt; // the source register
Johnny Chen3c75c762011-01-22 00:47:08 +0000113 switch (encoding) {
Johnny Chenaedde1c2011-01-24 20:38:45 +0000114 case eEncodingT1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000115 registers = Bits32(opcode, 7, 0);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000116 // The M bit represents LR.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000117 if (Bits32(opcode, 8, 8))
Johnny Chenaedde1c2011-01-24 20:38:45 +0000118 registers |= 0x000eu;
119 // if BitCount(registers) < 1 then UNPREDICTABLE;
120 if (BitCount(registers) < 1)
121 return false;
122 break;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000123 case eEncodingT2:
124 // Ignore bits 15 & 13.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000125 registers = Bits32(opcode, 15, 0) & ~0xa000;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000126 // if BitCount(registers) < 2 then UNPREDICTABLE;
127 if (BitCount(registers) < 2)
128 return false;
129 break;
130 case eEncodingT3:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000131 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000132 // if BadReg(t) then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000133 if (BadReg(Rt))
Johnny Chen7dc60e12011-01-24 19:46:32 +0000134 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000135 registers = (1u << Rt);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000136 break;
Johnny Chen3c75c762011-01-22 00:47:08 +0000137 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000138 registers = Bits32(opcode, 15, 0);
Johnny Chena33d4842011-01-24 22:25:48 +0000139 // Instead of return false, let's handle the following case as well,
140 // which amounts to pushing one reg onto the full descending stacks.
141 // if BitCount(register_list) < 2 then SEE STMDB / STMFD;
Johnny Chen3c75c762011-01-22 00:47:08 +0000142 break;
143 case eEncodingA2:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000144 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000145 // if t == 13 then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000146 if (Rt == dwarf_sp)
Johnny Chen3c75c762011-01-22 00:47:08 +0000147 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000148 registers = (1u << Rt);
Johnny Chen3c75c762011-01-22 00:47:08 +0000149 break;
Johnny Chence1ca772011-01-25 01:13:00 +0000150 default:
151 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000152 }
Johnny Chence1ca772011-01-25 01:13:00 +0000153 addr_t sp_offset = addr_byte_size * BitCount (registers);
Greg Clayton64c84432011-01-21 22:02:52 +0000154 addr_t addr = sp - sp_offset;
155 uint32_t i;
156
157 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
158 for (i=0; i<15; ++i)
159 {
Johnny Chen108d5aa2011-01-26 01:00:55 +0000160 if (BitIsSet (registers, 1u << i))
Greg Clayton64c84432011-01-21 22:02:52 +0000161 {
162 context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number
163 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
164 uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
165 if (!success)
166 return false;
167 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
168 return false;
169 addr += addr_byte_size;
170 }
171 }
172
Johnny Chen108d5aa2011-01-26 01:00:55 +0000173 if (BitIsSet (registers, 1u << 15))
Greg Clayton64c84432011-01-21 22:02:52 +0000174 {
175 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
Johnny Chen3c75c762011-01-22 00:47:08 +0000176 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton64c84432011-01-21 22:02:52 +0000177 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
178 if (!success)
179 return false;
180 if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
181 return false;
182 }
183
184 context.type = EmulateInstruction::eContextAdjustStackPointer;
185 context.arg0 = eRegisterKindGeneric;
186 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000187 context.arg2 = -sp_offset;
Greg Clayton64c84432011-01-21 22:02:52 +0000188
189 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
190 return false;
191 }
192 return true;
193}
194
Johnny Chen5b442b72011-01-27 19:34:30 +0000195// Set r7 or ip to point to saved value residing within the stack.
Johnny Chenbcec3af2011-01-27 01:26:19 +0000196// ADD (SP plus immediate)
197static bool
198emulate_add_rd_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
199{
200#if 0
201 // ARM pseudo code...
202 if (ConditionPassed())
203 {
204 EncodingSpecificOperations();
205 (result, carry, overflow) = AddWithCarry(SP, imm32, 0’);
206 if d == 15 then
207 ALUWritePC(result); // setflags is always FALSE here
208 else
209 R[d] = result;
210 if setflags then
211 APSR.N = result<31>;
212 APSR.Z = IsZeroBit(result);
213 APSR.C = carry;
214 APSR.V = overflow;
215 }
216#endif
217
218 bool success = false;
219 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
220 if (!success)
221 return false;
222
223 if (emulator->ConditionPassed())
224 {
225 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
226 if (!success)
227 return false;
228 uint32_t Rd; // the destination register
229 uint32_t imm32;
230 switch (encoding) {
231 case eEncodingT1:
232 Rd = 7;
233 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32)
234 break;
235 case eEncodingA1:
236 Rd = Bits32(opcode, 15, 12);
237 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
238 break;
239 default:
240 return false;
241 }
242 addr_t sp_offset = imm32;
243 addr_t addr = sp + sp_offset; // a pointer to the stack area
244
245 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
246 eRegisterKindGeneric,
247 LLDB_REGNUM_GENERIC_SP,
248 sp_offset };
249
250 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr))
251 return false;
252 }
253 return true;
254}
255
Johnny Chen788e0552011-01-27 22:52:23 +0000256// PC relative immediate load into register, possibly followed by ADD (SP plus register).
257// LDR (literal)
258static bool
259emulate_ldr_rd_pc_rel (EmulateInstructionARM *emulator, ARMEncoding encoding)
260{
261#if 0
262 // ARM pseudo code...
263 if (ConditionPassed())
264 {
265 EncodingSpecificOperations(); NullCheckIfThumbEE(15);
266 base = Align(PC,4);
267 address = if add then (base + imm32) else (base - imm32);
268 data = MemU[address,4];
269 if t == 15 then
270 if address<1:0> == 00 then LoadWritePC(data); else UNPREDICTABLE;
271 elsif UnalignedSupport() || address<1:0> = 00 then
272 R[t] = data;
273 else // Can only apply before ARMv7
274 if CurrentInstrSet() == InstrSet_ARM then
275 R[t] = ROR(data, 8*UInt(address<1:0>));
276 else
277 R[t] = bits(32) UNKNOWN;
278 }
279#endif
280
281 bool success = false;
282 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
283 if (!success)
284 return false;
285
286 if (emulator->ConditionPassed())
287 {
288 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
289 if (!success)
290 return false;
Johnny Chen809742e2011-01-28 00:32:27 +0000291
292 // PC relative immediate load context
293 EmulateInstruction::Context context = {EmulateInstruction::eContextRegisterPlusOffset,
294 eRegisterKindGeneric,
295 LLDB_REGNUM_GENERIC_PC,
296 0};
Johnny Chen788e0552011-01-27 22:52:23 +0000297 uint32_t Rd; // the destination register
298 uint32_t imm32; // immediate offset from the PC
299 addr_t addr; // the PC relative address
300 uint32_t data; // the literal data value from the PC relative load
301 switch (encoding) {
302 case eEncodingT1:
303 Rd = Bits32(opcode, 10, 8);
304 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32);
305 addr = pc + 4 + imm32;
Johnny Chen809742e2011-01-28 00:32:27 +0000306 context.arg2 = 4 + imm32;
Johnny Chen788e0552011-01-27 22:52:23 +0000307 break;
308 default:
309 return false;
310 }
Johnny Chen809742e2011-01-28 00:32:27 +0000311 data = emulator->ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000312 if (!success)
Johnny Chen809742e2011-01-28 00:32:27 +0000313 return false;
Johnny Chen788e0552011-01-27 22:52:23 +0000314 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, data))
315 return false;
316 }
317 return true;
318}
319
Johnny Chen5b442b72011-01-27 19:34:30 +0000320// An add operation to adjust the SP.
321// ADD (SP plus register)
322static bool
323emulate_add_sp_rm (EmulateInstructionARM *emulator, ARMEncoding encoding)
324{
325#if 0
326 // ARM pseudo code...
327 if (ConditionPassed())
328 {
329 EncodingSpecificOperations();
330 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
331 (result, carry, overflow) = AddWithCarry(SP, shifted, 0’);
332 if d == 15 then
333 ALUWritePC(result); // setflags is always FALSE here
334 else
335 R[d] = result;
336 if setflags then
337 APSR.N = result<31>;
338 APSR.Z = IsZeroBit(result);
339 APSR.C = carry;
340 APSR.V = overflow;
341 }
342#endif
343
344 bool success = false;
345 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
346 if (!success)
347 return false;
348
349 if (emulator->ConditionPassed())
350 {
351 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
352 if (!success)
353 return false;
354 uint32_t Rm; // the second operand
355 switch (encoding) {
356 case eEncodingT2:
357 Rm = Bits32(opcode, 6, 3);
358 break;
359 default:
360 return false;
361 }
362 int32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
363 if (!success)
364 return false;
365
366 addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value
367
368 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
369 eRegisterKindGeneric,
370 LLDB_REGNUM_GENERIC_SP,
371 reg_value };
372
373 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
374 return false;
375 }
376 return true;
377}
378
Johnny Chen0d0148e2011-01-28 02:26:08 +0000379// Set r7 to point to some ip offset.
380// SUB (immediate)
381static bool
382emulate_sub_r7_ip_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
383{
384#if 0
385 // ARM pseudo code...
386 if (ConditionPassed())
387 {
388 EncodingSpecificOperations();
389 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
390 if d == 15 then // Can only occur for ARM encoding
391 ALUWritePC(result); // setflags is always FALSE here
392 else
393 R[d] = result;
394 if setflags then
395 APSR.N = result<31>;
396 APSR.Z = IsZeroBit(result);
397 APSR.C = carry;
398 APSR.V = overflow;
399 }
400#endif
401
402 bool success = false;
403 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
404 if (!success)
405 return false;
406
407 if (emulator->ConditionPassed())
408 {
409 const addr_t ip = emulator->ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success);
410 if (!success)
411 return false;
412 uint32_t imm32;
413 switch (encoding) {
414 case eEncodingA1:
415 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
416 break;
417 default:
418 return false;
419 }
420 addr_t ip_offset = imm32;
421 addr_t addr = ip - ip_offset; // the adjusted ip value
422
423 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
424 eRegisterKindDWARF,
425 dwarf_r12,
426 -ip_offset };
427
428 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr))
429 return false;
430 }
431 return true;
432}
433
434// Set ip to point to some stack offset.
435// SUB (SP minus immediate)
436static bool
437emulate_sub_ip_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
438{
439#if 0
440 // ARM pseudo code...
441 if (ConditionPassed())
442 {
443 EncodingSpecificOperations();
444 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
445 if d == 15 then // Can only occur for ARM encoding
446 ALUWritePC(result); // setflags is always FALSE here
447 else
448 R[d] = result;
449 if setflags then
450 APSR.N = result<31>;
451 APSR.Z = IsZeroBit(result);
452 APSR.C = carry;
453 APSR.V = overflow;
454 }
455#endif
456
457 bool success = false;
458 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
459 if (!success)
460 return false;
461
462 if (emulator->ConditionPassed())
463 {
464 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
465 if (!success)
466 return false;
467 uint32_t imm32;
468 switch (encoding) {
469 case eEncodingA1:
470 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
471 break;
472 default:
473 return false;
474 }
475 addr_t sp_offset = imm32;
476 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
477
478 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
479 eRegisterKindGeneric,
480 LLDB_REGNUM_GENERIC_SP,
481 -sp_offset };
482
483 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr))
484 return false;
485 }
486 return true;
487}
488
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000489// A sub operation to adjust the SP -- allocate space for local storage.
490static bool
491emulate_sub_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
492{
493#if 0
494 // ARM pseudo code...
495 if (ConditionPassed())
496 {
497 EncodingSpecificOperations();
498 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
499 if d == 15 then // Can only occur for ARM encoding
Johnny Chen799dfd02011-01-26 23:14:33 +0000500 ALUWritePC(result); // setflags is always FALSE here
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000501 else
502 R[d] = result;
503 if setflags then
504 APSR.N = result<31>;
505 APSR.Z = IsZeroBit(result);
506 APSR.C = carry;
507 APSR.V = overflow;
508 }
509#endif
510
511 bool success = false;
512 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
513 if (!success)
514 return false;
515
516 if (emulator->ConditionPassed())
517 {
518 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
519 if (!success)
520 return false;
521 uint32_t imm32;
522 switch (encoding) {
Johnny Chene4455022011-01-26 00:08:59 +0000523 case eEncodingT1:
524 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
Johnny Chen60c0d622011-01-25 23:49:39 +0000525 case eEncodingT2:
526 imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8)
527 break;
528 case eEncodingT3:
529 imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32)
530 break;
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000531 case eEncodingA1:
Johnny Chen60c0d622011-01-25 23:49:39 +0000532 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000533 break;
534 default:
535 return false;
536 }
537 addr_t sp_offset = imm32;
538 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
539
540 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
541 eRegisterKindGeneric,
542 LLDB_REGNUM_GENERIC_SP,
Johnny Chen5b442b72011-01-27 19:34:30 +0000543 -sp_offset };
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000544
545 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
546 return false;
547 }
548 return true;
549}
550
551// A store operation to the stacks that also updates the SP.
Johnny Chence1ca772011-01-25 01:13:00 +0000552static bool
553emulate_str_rt_sp (EmulateInstructionARM *emulator, ARMEncoding encoding)
554{
555#if 0
556 // ARM pseudo code...
557 if (ConditionPassed())
558 {
559 EncodingSpecificOperations();
560 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
561 address = if index then offset_addr else R[n];
562 MemU[address,4] = if t == 15 then PCStoreValue() else R[t];
563 if wback then R[n] = offset_addr;
564 }
565#endif
566
567 bool success = false;
568 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
569 if (!success)
570 return false;
571
572 if (emulator->ConditionPassed())
573 {
574 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
575 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
576 if (!success)
577 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000578 uint32_t Rt; // the source register
Johnny Chence1ca772011-01-25 01:13:00 +0000579 uint32_t imm12;
580 switch (encoding) {
581 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000582 Rt = Bits32(opcode, 15, 12);
583 imm12 = Bits32(opcode, 11, 0);
Johnny Chence1ca772011-01-25 01:13:00 +0000584 break;
585 default:
586 return false;
587 }
588 addr_t sp_offset = imm12;
589 addr_t addr = sp - sp_offset;
590
591 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
Johnny Chen91d99862011-01-25 19:07:04 +0000592 if (Rt != 15)
Johnny Chence1ca772011-01-25 01:13:00 +0000593 {
Johnny Chen91d99862011-01-25 19:07:04 +0000594 context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number
595 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Johnny Chence1ca772011-01-25 01:13:00 +0000596 uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
597 if (!success)
598 return false;
599 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
600 return false;
601 }
602 else
603 {
604 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
605 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
606 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
607 if (!success)
608 return false;
609 if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
610 return false;
611 }
612
613 context.type = EmulateInstruction::eContextAdjustStackPointer;
614 context.arg0 = eRegisterKindGeneric;
615 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000616 context.arg2 = -sp_offset;
Johnny Chence1ca772011-01-25 01:13:00 +0000617
618 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
619 return false;
620 }
621 return true;
622}
623
Johnny Chen799dfd02011-01-26 23:14:33 +0000624static bool
625emulate_vpush (EmulateInstructionARM *emulator, ARMEncoding encoding)
626{
627#if 0
628 // ARM pseudo code...
629 if (ConditionPassed())
630 {
631 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
632 address = SP - imm32;
633 SP = SP - imm32;
634 if single_regs then
635 for r = 0 to regs-1
636 MemA[address,4] = S[d+r]; address = address+4;
637 else
638 for r = 0 to regs-1
639 // Store as two word-aligned words in the correct order for current endianness.
640 MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>;
641 MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>;
642 address = address+8;
643 }
644#endif
645
646 bool success = false;
647 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
648 if (!success)
649 return false;
650
651 if (emulator->ConditionPassed())
652 {
653 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
654 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
655 if (!success)
656 return false;
657 bool single_regs;
658 uint32_t d; // UInt(Vd:D) starting register
659 uint32_t imm32; // stack offset
660 uint32_t regs; // number of registers
661 switch (encoding) {
662 case eEncodingT1:
663 case eEncodingA1:
664 single_regs = false;
665 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
666 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
667 // If UInt(imm8) is odd, see "FSTMX".
668 regs = Bits32(opcode, 7, 0) / 2;
669 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
670 if (regs == 0 || regs > 16 || (d + regs) > 32)
671 return false;
672 break;
673 case eEncodingT2:
674 case eEncodingA2:
675 single_regs = true;
676 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
677 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
678 regs = Bits32(opcode, 7, 0);
679 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
680 if (regs == 0 || regs > 16 || (d + regs) > 32)
681 return false;
682 break;
683 default:
684 return false;
685 }
686 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
687 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
688 addr_t sp_offset = imm32;
689 addr_t addr = sp - sp_offset;
690 uint32_t i;
691
692 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
693 for (i=d; i<regs; ++i)
694 {
695 context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number
696 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
697 // uint64_t to accommodate 64-bit registers.
698 uint64_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
699 if (!success)
700 return false;
701 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size))
702 return false;
703 addr += reg_byte_size;
704 }
705
706 context.type = EmulateInstruction::eContextAdjustStackPointer;
707 context.arg0 = eRegisterKindGeneric;
708 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000709 context.arg2 = -sp_offset;
Johnny Chen799dfd02011-01-26 23:14:33 +0000710
711 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
712 return false;
713 }
714 return true;
715}
716
Greg Clayton64c84432011-01-21 22:02:52 +0000717static ARMOpcode g_arm_opcodes[] =
718{
Johnny Chene4455022011-01-26 00:08:59 +0000719 // push register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000720 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, emulate_push, "push <registers>" },
721 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, emulate_push, "push <register>" },
722
Johnny Chen5b442b72011-01-27 19:34:30 +0000723 // set r7 to point to a stack offset
Johnny Chenbcec3af2011-01-27 01:26:19 +0000724 { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add r7, sp, #<const>" },
Johnny Chen0d0148e2011-01-28 02:26:08 +0000725 { 0x0ffff000, 0xe24c7000, ARMvAll, eEncodingA1, eSize32, emulate_sub_r7_ip_imm, "sub r7, ip, #<const>"},
Johnny Chen5b442b72011-01-27 19:34:30 +0000726 // set ip to point to a stack offset
Johnny Chenbcec3af2011-01-27 01:26:19 +0000727 { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add ip, sp, #<const>" },
Johnny Chen0d0148e2011-01-28 02:26:08 +0000728 { 0x0ffff000, 0xe24dc000, ARMvAll, eEncodingA1, eSize32, emulate_sub_ip_sp_imm, "sub ip, sp, #<const>"},
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000729
730 // adjust the stack pointer
Johnny Chenbcec3af2011-01-27 01:26:19 +0000731 { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, emulate_sub_sp_imm, "sub sp, sp, #<const>"},
Johnny Chence1ca772011-01-25 01:13:00 +0000732
733 // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH;
Johnny Chen788e0552011-01-27 22:52:23 +0000734 { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, emulate_str_rt_sp, "str Rt, [sp, #-imm12]!" },
Johnny Chen799dfd02011-01-26 23:14:33 +0000735
736 // vector push consecutive extension register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000737 { 0x0fbf0f00, 0x0d2d0b00, ARMv6T2|ARMv7, eEncodingA1, eSize32, emulate_vpush, "vpush.64 <list>"},
738 { 0x0fbf0f00, 0x0d2d0a00, ARMv6T2|ARMv7, eEncodingA2, eSize32, emulate_vpush, "vpush.32 <list>"}
Greg Clayton64c84432011-01-21 22:02:52 +0000739};
740
Johnny Chen347320d2011-01-24 23:40:59 +0000741static ARMOpcode g_thumb_opcodes[] =
742{
Johnny Chene4455022011-01-26 00:08:59 +0000743 // push register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000744 { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, emulate_push, "push <registers>" },
745 { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_push, "push.w <registers>" },
746 { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_push, "push.w <register>" },
747
Johnny Chen5b442b72011-01-27 19:34:30 +0000748 // set r7 to point to a stack offset
Johnny Chen788e0552011-01-27 22:52:23 +0000749 { 0xffffff00, 0x000af00, ARMvAll, eEncodingT1, eSize16, emulate_add_rd_sp_imm, "add r7, sp, #imm" },
750
751 // PC relative load into register (see also emulate_add_sp_rm)
752 { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, emulate_ldr_rd_pc_rel, "ldr <Rd>, [PC, #imm]"},
Johnny Chen60c0d622011-01-25 23:49:39 +0000753
754 // adjust the stack pointer
Johnny Chen5b442b72011-01-27 19:34:30 +0000755 { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, emulate_add_sp_rm, "add sp, <Rm>"},
Johnny Chen788e0552011-01-27 22:52:23 +0000756 { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, emulate_sub_sp_imm, "add sp, sp, #imm"},
Johnny Chen5b442b72011-01-27 19:34:30 +0000757 { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_sub_sp_imm, "sub.w sp, sp, #<const>"},
Johnny Chen788e0552011-01-27 22:52:23 +0000758 { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_sub_sp_imm, "subw sp, sp, #imm12"},
Johnny Chen799dfd02011-01-26 23:14:33 +0000759
760 // vector push consecutive extension register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000761 { 0xffbf0f00, 0xed2d0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, emulate_vpush, "vpush.64 <list>"},
762 { 0xffbf0f00, 0xed2d0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_vpush, "vpush.32 <list>"}
Johnny Chen347320d2011-01-24 23:40:59 +0000763};
764
Greg Clayton64c84432011-01-21 22:02:52 +0000765static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
Johnny Chen347320d2011-01-24 23:40:59 +0000766static const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode);
Greg Clayton64c84432011-01-21 22:02:52 +0000767
768bool
769EmulateInstructionARM::ReadInstruction ()
770{
771 bool success = false;
772 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
773 if (success)
774 {
775 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
776 if (success)
777 {
778 Context read_inst_context = {eContextReadOpcode, 0, 0};
779 if (m_inst_cpsr & MASK_CPSR_T)
780 {
781 m_inst_mode = eModeThumb;
782 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
783
784 if (success)
785 {
786 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
787 {
788 m_inst.opcode_type = eOpcode16;
789 m_inst.opcode.inst16 = thumb_opcode;
790 }
791 else
792 {
793 m_inst.opcode_type = eOpcode32;
794 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
795 }
796 }
797 }
798 else
799 {
800 m_inst_mode = eModeARM;
801 m_inst.opcode_type = eOpcode32;
802 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
803 }
804 }
805 }
806 if (!success)
807 {
808 m_inst_mode = eModeInvalid;
809 m_inst_pc = LLDB_INVALID_ADDRESS;
810 }
811 return success;
812}
813
814uint32_t
815EmulateInstructionARM::CurrentCond ()
816{
817 switch (m_inst_mode)
818 {
819 default:
820 case eModeInvalid:
821 break;
822
823 case eModeARM:
824 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
825
826 case eModeThumb:
827 return 0x0000000Eu; // Return always for now, we need to handl IT instructions later
828 }
829 return UINT32_MAX; // Return invalid value
830}
831bool
832EmulateInstructionARM::ConditionPassed ()
833{
834 if (m_inst_cpsr == 0)
835 return false;
836
837 const uint32_t cond = CurrentCond ();
838
839 if (cond == UINT32_MAX)
840 return false;
841
842 bool result = false;
843 switch (UnsignedBits(cond, 3, 1))
844 {
845 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
846 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
847 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
848 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
849 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
850 case 5:
851 {
852 bool n = (m_inst_cpsr & MASK_CPSR_N);
853 bool v = (m_inst_cpsr & MASK_CPSR_V);
854 result = n == v;
855 }
856 break;
857 case 6:
858 {
859 bool n = (m_inst_cpsr & MASK_CPSR_N);
860 bool v = (m_inst_cpsr & MASK_CPSR_V);
861 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
862 }
863 break;
864 case 7:
865 result = true;
866 break;
867 }
868
869 if (cond & 1)
870 result = !result;
871 return result;
872}
873
874
875bool
876EmulateInstructionARM::EvaluateInstruction ()
877{
878 return false;
879}