Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 1 | //===-- 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 Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 11 | #include "ARMUtils.h" |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 12 | |
| 13 | using namespace lldb; |
| 14 | using 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 Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 22 | #define ARMv4 (1u << 0) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 23 | #define ARMv4T (1u << 1) |
| 24 | #define ARMv5T (1u << 2) |
| 25 | #define ARMv5TE (1u << 3) |
| 26 | #define ARMv5TEJ (1u << 4) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 27 | #define ARMv6 (1u << 5) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 28 | #define ARMv6K (1u << 6) |
| 29 | #define ARMv6T2 (1u << 7) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 30 | #define ARMv7 (1u << 8) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame^] | 31 | #define ARMv8 (1u << 9) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 32 | #define ARMvAll (0xffffffffu) |
| 33 | |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 34 | typedef enum |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 35 | { |
| 36 | eEncodingA1, |
| 37 | eEncodingA2, |
| 38 | eEncodingA3, |
| 39 | eEncodingA4, |
| 40 | eEncodingA5, |
| 41 | eEncodingT1, |
| 42 | eEncodingT2, |
| 43 | eEncodingT3, |
| 44 | eEncodingT4, |
| 45 | eEncodingT5, |
| 46 | } ARMEncoding; |
| 47 | |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 48 | typedef enum |
| 49 | { |
| 50 | eSize16, |
| 51 | eSize32 |
| 52 | } ARMInstrSize; |
| 53 | |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 54 | // Typedef for the callback function used during the emulation. |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 55 | // Pass along (ARMEncoding)encoding as the callback data. |
| 56 | typedef bool (*EmulateCallback) (EmulateInstructionARM *emulator, ARMEncoding encoding); |
| 57 | |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 58 | typedef struct |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 59 | { |
| 60 | uint32_t mask; |
| 61 | uint32_t value; |
| 62 | uint32_t variants; |
| 63 | ARMEncoding encoding; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 64 | ARMInstrSize size; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 65 | EmulateCallback callback; |
Johnny Chen | 4bee8ce | 2011-01-22 00:59:07 +0000 | [diff] [blame] | 66 | const char *name; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 67 | } ARMOpcode; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 68 | |
| 69 | static bool |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 70 | emulate_push (EmulateInstructionARM *emulator, ARMEncoding encoding) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 71 | { |
| 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 Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 110 | uint32_t registers = 0; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 111 | uint32_t Rt; // the source register |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 112 | switch (encoding) { |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 113 | case eEncodingT1: |
| 114 | registers = EmulateInstruction::UnsignedBits (opcode, 7, 0); |
| 115 | // The M bit represents LR. |
| 116 | if (EmulateInstruction::UnsignedBits (opcode, 8, 8)) |
| 117 | registers |= 0x000eu; |
| 118 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 119 | if (BitCount(registers) < 1) |
| 120 | return false; |
| 121 | break; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 122 | case eEncodingT2: |
| 123 | // Ignore bits 15 & 13. |
| 124 | registers = EmulateInstruction::UnsignedBits (opcode, 15, 0) & ~0xa000; |
| 125 | // if BitCount(registers) < 2 then UNPREDICTABLE; |
| 126 | if (BitCount(registers) < 2) |
| 127 | return false; |
| 128 | break; |
| 129 | case eEncodingT3: |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 130 | Rt = EmulateInstruction::UnsignedBits (opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 131 | // if BadReg(t) then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 132 | if (BadReg(Rt)) |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 133 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 134 | registers = (1u << Rt); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 135 | break; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 136 | case eEncodingA1: |
| 137 | registers = EmulateInstruction::UnsignedBits (opcode, 15, 0); |
Johnny Chen | a33d484 | 2011-01-24 22:25:48 +0000 | [diff] [blame] | 138 | // Instead of return false, let's handle the following case as well, |
| 139 | // which amounts to pushing one reg onto the full descending stacks. |
| 140 | // if BitCount(register_list) < 2 then SEE STMDB / STMFD; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 141 | break; |
| 142 | case eEncodingA2: |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 143 | Rt = EmulateInstruction::UnsignedBits (opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 144 | // if t == 13 then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 145 | if (Rt == dwarf_sp) |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 146 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 147 | registers = (1u << Rt); |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 148 | break; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 149 | default: |
| 150 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 151 | } |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 152 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 153 | addr_t addr = sp - sp_offset; |
| 154 | uint32_t i; |
| 155 | |
| 156 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 157 | for (i=0; i<15; ++i) |
| 158 | { |
| 159 | if (EmulateInstruction::BitIsSet (registers, 1u << i)) |
| 160 | { |
| 161 | context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number |
| 162 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 163 | uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
| 164 | if (!success) |
| 165 | return false; |
| 166 | if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
| 167 | return false; |
| 168 | addr += addr_byte_size; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (EmulateInstruction::BitIsSet (registers, 1u << 15)) |
| 173 | { |
| 174 | context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 175 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 176 | const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 177 | if (!success) |
| 178 | return false; |
| 179 | if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 184 | context.arg0 = eRegisterKindGeneric; |
| 185 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 186 | context.arg2 = sp_offset; |
| 187 | |
| 188 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
| 189 | return false; |
| 190 | } |
| 191 | return true; |
| 192 | } |
| 193 | |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 194 | // A sub operation to adjust the SP -- allocate space for local storage. |
| 195 | static bool |
| 196 | emulate_sub_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 197 | { |
| 198 | #if 0 |
| 199 | // ARM pseudo code... |
| 200 | if (ConditionPassed()) |
| 201 | { |
| 202 | EncodingSpecificOperations(); |
| 203 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 204 | if d == 15 then // Can only occur for ARM encoding |
| 205 | ALUWritePC(result); // setflags is always FALSE here |
| 206 | else |
| 207 | R[d] = result; |
| 208 | if setflags then |
| 209 | APSR.N = result<31>; |
| 210 | APSR.Z = IsZeroBit(result); |
| 211 | APSR.C = carry; |
| 212 | APSR.V = overflow; |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | bool success = false; |
| 217 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 218 | if (!success) |
| 219 | return false; |
| 220 | |
| 221 | if (emulator->ConditionPassed()) |
| 222 | { |
| 223 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 224 | if (!success) |
| 225 | return false; |
| 226 | uint32_t imm32; |
| 227 | switch (encoding) { |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame^] | 228 | case eEncodingT2: |
| 229 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 230 | break; |
| 231 | case eEncodingT3: |
| 232 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 233 | break; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 234 | case eEncodingA1: |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame^] | 235 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 236 | break; |
| 237 | default: |
| 238 | return false; |
| 239 | } |
| 240 | addr_t sp_offset = imm32; |
| 241 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 242 | |
| 243 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 244 | eRegisterKindGeneric, |
| 245 | LLDB_REGNUM_GENERIC_SP, |
| 246 | sp_offset }; |
| 247 | |
| 248 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
| 249 | return false; |
| 250 | } |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | // A store operation to the stacks that also updates the SP. |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 255 | static bool |
| 256 | emulate_str_rt_sp (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 257 | { |
| 258 | #if 0 |
| 259 | // ARM pseudo code... |
| 260 | if (ConditionPassed()) |
| 261 | { |
| 262 | EncodingSpecificOperations(); |
| 263 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 264 | address = if index then offset_addr else R[n]; |
| 265 | MemU[address,4] = if t == 15 then PCStoreValue() else R[t]; |
| 266 | if wback then R[n] = offset_addr; |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | bool success = false; |
| 271 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 272 | if (!success) |
| 273 | return false; |
| 274 | |
| 275 | if (emulator->ConditionPassed()) |
| 276 | { |
| 277 | const uint32_t addr_byte_size = emulator->GetAddressByteSize(); |
| 278 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 279 | if (!success) |
| 280 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 281 | uint32_t Rt; // the source register |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 282 | uint32_t imm12; |
| 283 | switch (encoding) { |
| 284 | case eEncodingA1: |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 285 | Rt = EmulateInstruction::UnsignedBits (opcode, 15, 12); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 286 | imm12 = EmulateInstruction::UnsignedBits (opcode, 11, 0); |
| 287 | break; |
| 288 | default: |
| 289 | return false; |
| 290 | } |
| 291 | addr_t sp_offset = imm12; |
| 292 | addr_t addr = sp - sp_offset; |
| 293 | |
| 294 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 295 | if (Rt != 15) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 296 | { |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 297 | context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number |
| 298 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 299 | uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
| 300 | if (!success) |
| 301 | return false; |
| 302 | if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
| 303 | return false; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number |
| 308 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 309 | const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 310 | if (!success) |
| 311 | return false; |
| 312 | if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 317 | context.arg0 = eRegisterKindGeneric; |
| 318 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 319 | context.arg2 = sp_offset; |
| 320 | |
| 321 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
| 322 | return false; |
| 323 | } |
| 324 | return true; |
| 325 | } |
| 326 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 327 | static ARMOpcode g_arm_opcodes[] = |
| 328 | { |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 329 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, emulate_push, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 330 | "push <registers> ; <registers> contains more than one register" }, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 331 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, emulate_push, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 332 | "push <registers> ; <registers> contains one register, <Rt>" }, |
| 333 | |
| 334 | // adjust the stack pointer |
| 335 | { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, emulate_sub_sp_imm, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame^] | 336 | "sub sp, sp, #<const>"}, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 337 | |
| 338 | // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH; |
| 339 | { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, emulate_str_rt_sp, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame^] | 340 | "str Rt, [sp, #-<imm12>]!" } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 341 | }; |
| 342 | |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 343 | static ARMOpcode g_thumb_opcodes[] = |
| 344 | { |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 345 | { 0x0000fe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, emulate_push, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 346 | "push <registers>" }, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 347 | { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_push, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 348 | "push.w <registers> ; <registers> contains more than one register" }, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 349 | { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_push, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame^] | 350 | "push.w <registers> ; <registers> contains one register, <Rt>" }, |
| 351 | |
| 352 | // adjust the stack pointer |
| 353 | { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_sub_sp_imm, |
| 354 | "sub{s}.w sp, sp, #<const>"}, |
| 355 | // adjust the stack pointer |
| 356 | { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_sub_sp_imm, |
| 357 | "subw sp, sp, #<imm12>"} |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 360 | static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode); |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 361 | static const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 362 | |
| 363 | bool |
| 364 | EmulateInstructionARM::ReadInstruction () |
| 365 | { |
| 366 | bool success = false; |
| 367 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 368 | if (success) |
| 369 | { |
| 370 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 371 | if (success) |
| 372 | { |
| 373 | Context read_inst_context = {eContextReadOpcode, 0, 0}; |
| 374 | if (m_inst_cpsr & MASK_CPSR_T) |
| 375 | { |
| 376 | m_inst_mode = eModeThumb; |
| 377 | uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success); |
| 378 | |
| 379 | if (success) |
| 380 | { |
| 381 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 382 | { |
| 383 | m_inst.opcode_type = eOpcode16; |
| 384 | m_inst.opcode.inst16 = thumb_opcode; |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | m_inst.opcode_type = eOpcode32; |
| 389 | m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | m_inst_mode = eModeARM; |
| 396 | m_inst.opcode_type = eOpcode32; |
| 397 | m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | if (!success) |
| 402 | { |
| 403 | m_inst_mode = eModeInvalid; |
| 404 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 405 | } |
| 406 | return success; |
| 407 | } |
| 408 | |
| 409 | uint32_t |
| 410 | EmulateInstructionARM::CurrentCond () |
| 411 | { |
| 412 | switch (m_inst_mode) |
| 413 | { |
| 414 | default: |
| 415 | case eModeInvalid: |
| 416 | break; |
| 417 | |
| 418 | case eModeARM: |
| 419 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 420 | |
| 421 | case eModeThumb: |
| 422 | return 0x0000000Eu; // Return always for now, we need to handl IT instructions later |
| 423 | } |
| 424 | return UINT32_MAX; // Return invalid value |
| 425 | } |
| 426 | bool |
| 427 | EmulateInstructionARM::ConditionPassed () |
| 428 | { |
| 429 | if (m_inst_cpsr == 0) |
| 430 | return false; |
| 431 | |
| 432 | const uint32_t cond = CurrentCond (); |
| 433 | |
| 434 | if (cond == UINT32_MAX) |
| 435 | return false; |
| 436 | |
| 437 | bool result = false; |
| 438 | switch (UnsignedBits(cond, 3, 1)) |
| 439 | { |
| 440 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 441 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 442 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 443 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 444 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 445 | case 5: |
| 446 | { |
| 447 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 448 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 449 | result = n == v; |
| 450 | } |
| 451 | break; |
| 452 | case 6: |
| 453 | { |
| 454 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 455 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 456 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 457 | } |
| 458 | break; |
| 459 | case 7: |
| 460 | result = true; |
| 461 | break; |
| 462 | } |
| 463 | |
| 464 | if (cond & 1) |
| 465 | result = !result; |
| 466 | return result; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | bool |
| 471 | EmulateInstructionARM::EvaluateInstruction () |
| 472 | { |
| 473 | return false; |
| 474 | } |