blob: 0a16b1860d9bb642c0c6590dfa9bd36d3bed4508 [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
Johnny Chen251af6a2011-01-21 22:47:25 +000022#define ARMv4 (1u << 0)
Greg Clayton64c84432011-01-21 22:02:52 +000023#define ARMv4T (1u << 1)
24#define ARMv5T (1u << 2)
25#define ARMv5TE (1u << 3)
26#define ARMv5TEJ (1u << 4)
Johnny Chen251af6a2011-01-21 22:47:25 +000027#define ARMv6 (1u << 5)
Greg Clayton64c84432011-01-21 22:02:52 +000028#define ARMv6K (1u << 6)
29#define ARMv6T2 (1u << 7)
Johnny Chen251af6a2011-01-21 22:47:25 +000030#define ARMv7 (1u << 8)
31#define ARMv8 (1u << 8)
Greg Clayton64c84432011-01-21 22:02:52 +000032#define ARMvAll (0xffffffffu)
33
Johnny Chen7dc60e12011-01-24 19:46:32 +000034typedef enum
Greg Clayton64c84432011-01-21 22:02:52 +000035{
36 eEncodingA1,
37 eEncodingA2,
38 eEncodingA3,
39 eEncodingA4,
40 eEncodingA5,
41 eEncodingT1,
42 eEncodingT2,
43 eEncodingT3,
44 eEncodingT4,
45 eEncodingT5,
46} ARMEncoding;
47
Johnny Chen7dc60e12011-01-24 19:46:32 +000048typedef enum
49{
50 eSize16,
51 eSize32
52} ARMInstrSize;
53
Johnny Chen4baf2e32011-01-24 18:24:53 +000054// Typedef for the callback function used during the emulation.
Johnny Chen3c75c762011-01-22 00:47:08 +000055// Pass along (ARMEncoding)encoding as the callback data.
56typedef bool (*EmulateCallback) (EmulateInstructionARM *emulator, ARMEncoding encoding);
57
Johnny Chen7dc60e12011-01-24 19:46:32 +000058typedef struct
Greg Clayton64c84432011-01-21 22:02:52 +000059{
60 uint32_t mask;
61 uint32_t value;
62 uint32_t variants;
63 ARMEncoding encoding;
Johnny Chen7dc60e12011-01-24 19:46:32 +000064 ARMInstrSize size;
Greg Clayton64c84432011-01-21 22:02:52 +000065 EmulateCallback callback;
Johnny Chen4bee8ce2011-01-22 00:59:07 +000066 const char *name;
Johnny Chen7dc60e12011-01-24 19:46:32 +000067} ARMOpcode;
Greg Clayton64c84432011-01-21 22:02:52 +000068
69static bool
Johnny Chen3c75c762011-01-22 00:47:08 +000070EmulateARMPushEncoding (EmulateInstructionARM *emulator, ARMEncoding encoding)
Greg Clayton64c84432011-01-21 22:02:52 +000071{
72#if 0
73 // ARM pseudo code...
74 if (ConditionPassed())
75 {
76 EncodingSpecificOperations();
77 NullCheckIfThumbEE(13);
78 address = SP - 4*BitCount(registers);
79
80 for (i = 0 to 14)
81 {
82 if (registers<i> == 1’)
83 {
84 if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1
85 MemA[address,4] = bits(32) UNKNOWN;
86 else
87 MemA[address,4] = R[i];
88 address = address + 4;
89 }
90 }
91
92 if (registers<15> == 1’) // Only possible for encoding A1 or A2
93 MemA[address,4] = PCStoreValue();
94
95 SP = SP - 4*BitCount(registers);
96 }
97#endif
98
99 bool success = false;
100 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
101 if (!success)
102 return false;
103
104 if (emulator->ConditionPassed())
105 {
106 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
107 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
108 if (!success)
109 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000110 uint32_t registers = 0;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000111 uint32_t t; // UInt(Rt)
Johnny Chen3c75c762011-01-22 00:47:08 +0000112 switch (encoding) {
Johnny Chen7dc60e12011-01-24 19:46:32 +0000113 case eEncodingT2:
114 // Ignore bits 15 & 13.
115 registers = EmulateInstruction::UnsignedBits (opcode, 15, 0) & ~0xa000;
116 // if BitCount(registers) < 2 then UNPREDICTABLE;
117 if (BitCount(registers) < 2)
118 return false;
119 break;
120 case eEncodingT3:
121 t = EmulateInstruction::UnsignedBits (opcode, 15, 12);
122 // if BadReg(t) then UNPREDICTABLE;
123 if (BadReg(t))
124 return false;
125 registers = (1u << t);
126 break;
Johnny Chen3c75c762011-01-22 00:47:08 +0000127 case eEncodingA1:
128 registers = EmulateInstruction::UnsignedBits (opcode, 15, 0);
129 break;
130 case eEncodingA2:
Johnny Chen7dc60e12011-01-24 19:46:32 +0000131 t = EmulateInstruction::UnsignedBits (opcode, 15, 12);
132 // if t == 13 then UNPREDICTABLE;
133 if (t == dwarf_sp)
Johnny Chen3c75c762011-01-22 00:47:08 +0000134 return false;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000135 registers = (1u << t);
Johnny Chen3c75c762011-01-22 00:47:08 +0000136 break;
137 }
Greg Clayton64c84432011-01-21 22:02:52 +0000138 addr_t sp_offset = addr_byte_size * EmulateInstruction::BitCount (registers);
139 addr_t addr = sp - sp_offset;
140 uint32_t i;
141
142 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
143 for (i=0; i<15; ++i)
144 {
145 if (EmulateInstruction::BitIsSet (registers, 1u << i))
146 {
147 context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number
148 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
149 uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
150 if (!success)
151 return false;
152 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
153 return false;
154 addr += addr_byte_size;
155 }
156 }
157
158 if (EmulateInstruction::BitIsSet (registers, 1u << 15))
159 {
160 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
Johnny Chen3c75c762011-01-22 00:47:08 +0000161 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton64c84432011-01-21 22:02:52 +0000162 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
163 if (!success)
164 return false;
165 if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
166 return false;
167 }
168
169 context.type = EmulateInstruction::eContextAdjustStackPointer;
170 context.arg0 = eRegisterKindGeneric;
171 context.arg1 = LLDB_REGNUM_GENERIC_SP;
172 context.arg2 = sp_offset;
173
174 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
175 return false;
176 }
177 return true;
178}
179
180static ARMOpcode g_arm_opcodes[] =
181{
Johnny Chen7dc60e12011-01-24 19:46:32 +0000182 { 0xffff0000, 0xe8ad0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, EmulateARMPushEncoding,
Johnny Chen555c4972011-01-24 20:21:01 +0000183 "PUSH<c>.W <registers> ; <registers> contains more than one register" },
Johnny Chen7dc60e12011-01-24 19:46:32 +0000184 { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, EmulateARMPushEncoding,
Johnny Chen555c4972011-01-24 20:21:01 +0000185 "PUSH<c>.W <registers> ; <registers> contains one register, <Rt>" },
Johnny Chen7dc60e12011-01-24 19:46:32 +0000186 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, EmulateARMPushEncoding,
187 "PUSH<c> <registers> ; <registers> contains more than one register" },
188 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, EmulateARMPushEncoding,
Johnny Chen4bee8ce2011-01-22 00:59:07 +0000189 "PUSH<c> <registers> ; <registers> contains one register, <Rt>" }
Greg Clayton64c84432011-01-21 22:02:52 +0000190};
191
192static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
193
194bool
195EmulateInstructionARM::ReadInstruction ()
196{
197 bool success = false;
198 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
199 if (success)
200 {
201 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
202 if (success)
203 {
204 Context read_inst_context = {eContextReadOpcode, 0, 0};
205 if (m_inst_cpsr & MASK_CPSR_T)
206 {
207 m_inst_mode = eModeThumb;
208 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
209
210 if (success)
211 {
212 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
213 {
214 m_inst.opcode_type = eOpcode16;
215 m_inst.opcode.inst16 = thumb_opcode;
216 }
217 else
218 {
219 m_inst.opcode_type = eOpcode32;
220 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
221 }
222 }
223 }
224 else
225 {
226 m_inst_mode = eModeARM;
227 m_inst.opcode_type = eOpcode32;
228 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
229 }
230 }
231 }
232 if (!success)
233 {
234 m_inst_mode = eModeInvalid;
235 m_inst_pc = LLDB_INVALID_ADDRESS;
236 }
237 return success;
238}
239
240uint32_t
241EmulateInstructionARM::CurrentCond ()
242{
243 switch (m_inst_mode)
244 {
245 default:
246 case eModeInvalid:
247 break;
248
249 case eModeARM:
250 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
251
252 case eModeThumb:
253 return 0x0000000Eu; // Return always for now, we need to handl IT instructions later
254 }
255 return UINT32_MAX; // Return invalid value
256}
257bool
258EmulateInstructionARM::ConditionPassed ()
259{
260 if (m_inst_cpsr == 0)
261 return false;
262
263 const uint32_t cond = CurrentCond ();
264
265 if (cond == UINT32_MAX)
266 return false;
267
268 bool result = false;
269 switch (UnsignedBits(cond, 3, 1))
270 {
271 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
272 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
273 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
274 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
275 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
276 case 5:
277 {
278 bool n = (m_inst_cpsr & MASK_CPSR_N);
279 bool v = (m_inst_cpsr & MASK_CPSR_V);
280 result = n == v;
281 }
282 break;
283 case 6:
284 {
285 bool n = (m_inst_cpsr & MASK_CPSR_N);
286 bool v = (m_inst_cpsr & MASK_CPSR_V);
287 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
288 }
289 break;
290 case 7:
291 result = true;
292 break;
293 }
294
295 if (cond & 1)
296 result = !result;
297 return result;
298}
299
300
301bool
302EmulateInstructionARM::EvaluateInstruction ()
303{
304 return false;
305}