blob: e4fa24b99cc36827c8356c8c1b8acdd530d4fcb1 [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 Chen4baf2e32011-01-24 18:24:53 +000011#include "ARMUtils.h"
Greg Clayton64c84432011-01-21 22:02:52 +000012
13using namespace lldb;
14using namespace lldb_private;
15
16// ARM constants used during decoding
17#define REG_RD 0
18#define LDM_REGLIST 1
19#define PC_REG 15
20#define PC_REGLIST_BIT 0x8000
21
22// ARM conditions
23#define COND_EQ 0x0
24#define COND_NE 0x1
25#define COND_CS 0x2
26#define COND_HS 0x2
27#define COND_CC 0x3
28#define COND_LO 0x3
29#define COND_MI 0x4
30#define COND_PL 0x5
31#define COND_VS 0x6
32#define COND_VC 0x7
33#define COND_HI 0x8
34#define COND_LS 0x9
35#define COND_GE 0xA
36#define COND_LT 0xB
37#define COND_GT 0xC
38#define COND_LE 0xD
39#define COND_AL 0xE
40#define COND_UNCOND 0xF
41
42
Johnny Chen251af6a2011-01-21 22:47:25 +000043#define MASK_CPSR_MODE_MASK (0x0000001fu)
Greg Clayton64c84432011-01-21 22:02:52 +000044#define MASK_CPSR_T (1u << 5)
45#define MASK_CPSR_F (1u << 6)
46#define MASK_CPSR_I (1u << 7)
47#define MASK_CPSR_A (1u << 8)
48#define MASK_CPSR_E (1u << 9)
49#define MASK_CPSR_GE_MASK (0x000f0000u)
50#define MASK_CPSR_J (1u << 24)
51#define MASK_CPSR_Q (1u << 27)
52#define MASK_CPSR_V (1u << 28)
53#define MASK_CPSR_C (1u << 29)
54#define MASK_CPSR_Z (1u << 30)
55#define MASK_CPSR_N (1u << 31)
56
57
Johnny Chen251af6a2011-01-21 22:47:25 +000058#define ARMv4 (1u << 0)
Greg Clayton64c84432011-01-21 22:02:52 +000059#define ARMv4T (1u << 1)
60#define ARMv5T (1u << 2)
61#define ARMv5TE (1u << 3)
62#define ARMv5TEJ (1u << 4)
Johnny Chen251af6a2011-01-21 22:47:25 +000063#define ARMv6 (1u << 5)
Greg Clayton64c84432011-01-21 22:02:52 +000064#define ARMv6K (1u << 6)
65#define ARMv6T2 (1u << 7)
Johnny Chen251af6a2011-01-21 22:47:25 +000066#define ARMv7 (1u << 8)
67#define ARMv8 (1u << 8)
Greg Clayton64c84432011-01-21 22:02:52 +000068#define ARMvAll (0xffffffffu)
69
Johnny Chen7dc60e12011-01-24 19:46:32 +000070typedef enum
Greg Clayton64c84432011-01-21 22:02:52 +000071{
72 eEncodingA1,
73 eEncodingA2,
74 eEncodingA3,
75 eEncodingA4,
76 eEncodingA5,
77 eEncodingT1,
78 eEncodingT2,
79 eEncodingT3,
80 eEncodingT4,
81 eEncodingT5,
82} ARMEncoding;
83
Johnny Chen7dc60e12011-01-24 19:46:32 +000084typedef enum
85{
86 eSize16,
87 eSize32
88} ARMInstrSize;
89
Johnny Chen4baf2e32011-01-24 18:24:53 +000090// Typedef for the callback function used during the emulation.
Johnny Chen3c75c762011-01-22 00:47:08 +000091// Pass along (ARMEncoding)encoding as the callback data.
92typedef bool (*EmulateCallback) (EmulateInstructionARM *emulator, ARMEncoding encoding);
93
Johnny Chen7dc60e12011-01-24 19:46:32 +000094typedef struct
Greg Clayton64c84432011-01-21 22:02:52 +000095{
96 uint32_t mask;
97 uint32_t value;
98 uint32_t variants;
99 ARMEncoding encoding;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000100 ARMInstrSize size;
Greg Clayton64c84432011-01-21 22:02:52 +0000101 EmulateCallback callback;
Johnny Chen4bee8ce2011-01-22 00:59:07 +0000102 const char *name;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000103} ARMOpcode;
Greg Clayton64c84432011-01-21 22:02:52 +0000104
105static bool
Johnny Chen3c75c762011-01-22 00:47:08 +0000106EmulateARMPushEncoding (EmulateInstructionARM *emulator, ARMEncoding encoding)
Greg Clayton64c84432011-01-21 22:02:52 +0000107{
108#if 0
109 // ARM pseudo code...
110 if (ConditionPassed())
111 {
112 EncodingSpecificOperations();
113 NullCheckIfThumbEE(13);
114 address = SP - 4*BitCount(registers);
115
116 for (i = 0 to 14)
117 {
118 if (registers<i> == 1’)
119 {
120 if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1
121 MemA[address,4] = bits(32) UNKNOWN;
122 else
123 MemA[address,4] = R[i];
124 address = address + 4;
125 }
126 }
127
128 if (registers<15> == 1’) // Only possible for encoding A1 or A2
129 MemA[address,4] = PCStoreValue();
130
131 SP = SP - 4*BitCount(registers);
132 }
133#endif
134
135 bool success = false;
136 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
137 if (!success)
138 return false;
139
140 if (emulator->ConditionPassed())
141 {
142 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
143 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
144 if (!success)
145 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000146 uint32_t registers = 0;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000147 uint32_t t; // UInt(Rt)
Johnny Chen3c75c762011-01-22 00:47:08 +0000148 switch (encoding) {
Johnny Chen7dc60e12011-01-24 19:46:32 +0000149 case eEncodingT2:
150 // Ignore bits 15 & 13.
151 registers = EmulateInstruction::UnsignedBits (opcode, 15, 0) & ~0xa000;
152 // if BitCount(registers) < 2 then UNPREDICTABLE;
153 if (BitCount(registers) < 2)
154 return false;
155 break;
156 case eEncodingT3:
157 t = EmulateInstruction::UnsignedBits (opcode, 15, 12);
158 // if BadReg(t) then UNPREDICTABLE;
159 if (BadReg(t))
160 return false;
161 registers = (1u << t);
162 break;
Johnny Chen3c75c762011-01-22 00:47:08 +0000163 case eEncodingA1:
164 registers = EmulateInstruction::UnsignedBits (opcode, 15, 0);
165 break;
166 case eEncodingA2:
Johnny Chen7dc60e12011-01-24 19:46:32 +0000167 t = EmulateInstruction::UnsignedBits (opcode, 15, 12);
168 // if t == 13 then UNPREDICTABLE;
169 if (t == dwarf_sp)
Johnny Chen3c75c762011-01-22 00:47:08 +0000170 return false;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000171 registers = (1u << t);
Johnny Chen3c75c762011-01-22 00:47:08 +0000172 break;
173 }
Greg Clayton64c84432011-01-21 22:02:52 +0000174 addr_t sp_offset = addr_byte_size * EmulateInstruction::BitCount (registers);
175 addr_t addr = sp - sp_offset;
176 uint32_t i;
177
178 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
179 for (i=0; i<15; ++i)
180 {
181 if (EmulateInstruction::BitIsSet (registers, 1u << i))
182 {
183 context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number
184 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
185 uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
186 if (!success)
187 return false;
188 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
189 return false;
190 addr += addr_byte_size;
191 }
192 }
193
194 if (EmulateInstruction::BitIsSet (registers, 1u << 15))
195 {
196 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
Johnny Chen3c75c762011-01-22 00:47:08 +0000197 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton64c84432011-01-21 22:02:52 +0000198 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
199 if (!success)
200 return false;
201 if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
202 return false;
203 }
204
205 context.type = EmulateInstruction::eContextAdjustStackPointer;
206 context.arg0 = eRegisterKindGeneric;
207 context.arg1 = LLDB_REGNUM_GENERIC_SP;
208 context.arg2 = sp_offset;
209
210 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
211 return false;
212 }
213 return true;
214}
215
216static ARMOpcode g_arm_opcodes[] =
217{
Johnny Chen7dc60e12011-01-24 19:46:32 +0000218 { 0xffff0000, 0xe8ad0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, EmulateARMPushEncoding,
Johnny Chen4bee8ce2011-01-22 00:59:07 +0000219 "PUSH<c> <registers> ; <registers> contains more than one register" },
Johnny Chen7dc60e12011-01-24 19:46:32 +0000220 { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, EmulateARMPushEncoding,
221 "PUSH<c> <registers> ; <registers> contains one register, <Rt>" },
222 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, EmulateARMPushEncoding,
223 "PUSH<c> <registers> ; <registers> contains more than one register" },
224 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, EmulateARMPushEncoding,
Johnny Chen4bee8ce2011-01-22 00:59:07 +0000225 "PUSH<c> <registers> ; <registers> contains one register, <Rt>" }
Greg Clayton64c84432011-01-21 22:02:52 +0000226};
227
228static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
229
230bool
231EmulateInstructionARM::ReadInstruction ()
232{
233 bool success = false;
234 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
235 if (success)
236 {
237 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
238 if (success)
239 {
240 Context read_inst_context = {eContextReadOpcode, 0, 0};
241 if (m_inst_cpsr & MASK_CPSR_T)
242 {
243 m_inst_mode = eModeThumb;
244 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
245
246 if (success)
247 {
248 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
249 {
250 m_inst.opcode_type = eOpcode16;
251 m_inst.opcode.inst16 = thumb_opcode;
252 }
253 else
254 {
255 m_inst.opcode_type = eOpcode32;
256 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
257 }
258 }
259 }
260 else
261 {
262 m_inst_mode = eModeARM;
263 m_inst.opcode_type = eOpcode32;
264 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
265 }
266 }
267 }
268 if (!success)
269 {
270 m_inst_mode = eModeInvalid;
271 m_inst_pc = LLDB_INVALID_ADDRESS;
272 }
273 return success;
274}
275
276uint32_t
277EmulateInstructionARM::CurrentCond ()
278{
279 switch (m_inst_mode)
280 {
281 default:
282 case eModeInvalid:
283 break;
284
285 case eModeARM:
286 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
287
288 case eModeThumb:
289 return 0x0000000Eu; // Return always for now, we need to handl IT instructions later
290 }
291 return UINT32_MAX; // Return invalid value
292}
293bool
294EmulateInstructionARM::ConditionPassed ()
295{
296 if (m_inst_cpsr == 0)
297 return false;
298
299 const uint32_t cond = CurrentCond ();
300
301 if (cond == UINT32_MAX)
302 return false;
303
304 bool result = false;
305 switch (UnsignedBits(cond, 3, 1))
306 {
307 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
308 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
309 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
310 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
311 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
312 case 5:
313 {
314 bool n = (m_inst_cpsr & MASK_CPSR_N);
315 bool v = (m_inst_cpsr & MASK_CPSR_V);
316 result = n == v;
317 }
318 break;
319 case 6:
320 {
321 bool n = (m_inst_cpsr & MASK_CPSR_N);
322 bool v = (m_inst_cpsr & MASK_CPSR_V);
323 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
324 }
325 break;
326 case 7:
327 result = true;
328 break;
329 }
330
331 if (cond & 1)
332 result = !result;
333 return result;
334}
335
336
337bool
338EmulateInstructionARM::EvaluateInstruction ()
339{
340 return false;
341}