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 | |
| 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 Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 43 | #define MASK_CPSR_MODE_MASK (0x0000001fu) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 44 | #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 Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 58 | #define ARMv4 (1u << 0) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 59 | #define ARMv4T (1u << 1) |
| 60 | #define ARMv5T (1u << 2) |
| 61 | #define ARMv5TE (1u << 3) |
| 62 | #define ARMv5TEJ (1u << 4) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 63 | #define ARMv6 (1u << 5) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 64 | #define ARMv6K (1u << 6) |
| 65 | #define ARMv6T2 (1u << 7) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 66 | #define ARMv7 (1u << 8) |
| 67 | #define ARMv8 (1u << 8) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 68 | #define ARMvAll (0xffffffffu) |
| 69 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 70 | typedef enum ARMEncoding |
| 71 | { |
| 72 | eEncodingA1, |
| 73 | eEncodingA2, |
| 74 | eEncodingA3, |
| 75 | eEncodingA4, |
| 76 | eEncodingA5, |
| 77 | eEncodingT1, |
| 78 | eEncodingT2, |
| 79 | eEncodingT3, |
| 80 | eEncodingT4, |
| 81 | eEncodingT5, |
| 82 | } ARMEncoding; |
| 83 | |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame^] | 84 | // Typedef for the callback function used during the emulation. |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 85 | // Pass along (ARMEncoding)encoding as the callback data. |
| 86 | typedef bool (*EmulateCallback) (EmulateInstructionARM *emulator, ARMEncoding encoding); |
| 87 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 88 | typedef struct ARMOpcode |
| 89 | { |
| 90 | uint32_t mask; |
| 91 | uint32_t value; |
| 92 | uint32_t variants; |
| 93 | ARMEncoding encoding; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 94 | EmulateCallback callback; |
Johnny Chen | 4bee8ce | 2011-01-22 00:59:07 +0000 | [diff] [blame] | 95 | const char *name; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | static bool |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 99 | EmulateARMPushEncoding (EmulateInstructionARM *emulator, ARMEncoding encoding) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 100 | { |
| 101 | #if 0 |
| 102 | // ARM pseudo code... |
| 103 | if (ConditionPassed()) |
| 104 | { |
| 105 | EncodingSpecificOperations(); |
| 106 | NullCheckIfThumbEE(13); |
| 107 | address = SP - 4*BitCount(registers); |
| 108 | |
| 109 | for (i = 0 to 14) |
| 110 | { |
| 111 | if (registers<i> == ’1’) |
| 112 | { |
| 113 | if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1 |
| 114 | MemA[address,4] = bits(32) UNKNOWN; |
| 115 | else |
| 116 | MemA[address,4] = R[i]; |
| 117 | address = address + 4; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (registers<15> == ’1’) // Only possible for encoding A1 or A2 |
| 122 | MemA[address,4] = PCStoreValue(); |
| 123 | |
| 124 | SP = SP - 4*BitCount(registers); |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | bool success = false; |
| 129 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 130 | if (!success) |
| 131 | return false; |
| 132 | |
| 133 | if (emulator->ConditionPassed()) |
| 134 | { |
| 135 | const uint32_t addr_byte_size = emulator->GetAddressByteSize(); |
| 136 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 137 | if (!success) |
| 138 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 139 | uint32_t registers = 0; |
| 140 | switch (encoding) { |
| 141 | case eEncodingA1: |
| 142 | registers = EmulateInstruction::UnsignedBits (opcode, 15, 0); |
| 143 | break; |
| 144 | case eEncodingA2: |
| 145 | const uint32_t Rt = EmulateInstruction::UnsignedBits (opcode, 15, 12); |
| 146 | // if t == 13 then UNPREDICTABLE |
| 147 | if (Rt == dwarf_sp) |
| 148 | return false; |
| 149 | registers = (1u << Rt); |
| 150 | break; |
| 151 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 152 | addr_t sp_offset = addr_byte_size * EmulateInstruction::BitCount (registers); |
| 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 | |
| 194 | static ARMOpcode g_arm_opcodes[] = |
| 195 | { |
Johnny Chen | 4bee8ce | 2011-01-22 00:59:07 +0000 | [diff] [blame] | 196 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, EmulateARMPushEncoding, |
| 197 | "PUSH<c> <registers> ; <registers> contains more than one register" }, |
| 198 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, EmulateARMPushEncoding, |
| 199 | "PUSH<c> <registers> ; <registers> contains one register, <Rt>" } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode); |
| 203 | |
| 204 | bool |
| 205 | EmulateInstructionARM::ReadInstruction () |
| 206 | { |
| 207 | bool success = false; |
| 208 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 209 | if (success) |
| 210 | { |
| 211 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 212 | if (success) |
| 213 | { |
| 214 | Context read_inst_context = {eContextReadOpcode, 0, 0}; |
| 215 | if (m_inst_cpsr & MASK_CPSR_T) |
| 216 | { |
| 217 | m_inst_mode = eModeThumb; |
| 218 | uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success); |
| 219 | |
| 220 | if (success) |
| 221 | { |
| 222 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 223 | { |
| 224 | m_inst.opcode_type = eOpcode16; |
| 225 | m_inst.opcode.inst16 = thumb_opcode; |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | m_inst.opcode_type = eOpcode32; |
| 230 | m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | m_inst_mode = eModeARM; |
| 237 | m_inst.opcode_type = eOpcode32; |
| 238 | m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | if (!success) |
| 243 | { |
| 244 | m_inst_mode = eModeInvalid; |
| 245 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 246 | } |
| 247 | return success; |
| 248 | } |
| 249 | |
| 250 | uint32_t |
| 251 | EmulateInstructionARM::CurrentCond () |
| 252 | { |
| 253 | switch (m_inst_mode) |
| 254 | { |
| 255 | default: |
| 256 | case eModeInvalid: |
| 257 | break; |
| 258 | |
| 259 | case eModeARM: |
| 260 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 261 | |
| 262 | case eModeThumb: |
| 263 | return 0x0000000Eu; // Return always for now, we need to handl IT instructions later |
| 264 | } |
| 265 | return UINT32_MAX; // Return invalid value |
| 266 | } |
| 267 | bool |
| 268 | EmulateInstructionARM::ConditionPassed () |
| 269 | { |
| 270 | if (m_inst_cpsr == 0) |
| 271 | return false; |
| 272 | |
| 273 | const uint32_t cond = CurrentCond (); |
| 274 | |
| 275 | if (cond == UINT32_MAX) |
| 276 | return false; |
| 277 | |
| 278 | bool result = false; |
| 279 | switch (UnsignedBits(cond, 3, 1)) |
| 280 | { |
| 281 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 282 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 283 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 284 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 285 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 286 | case 5: |
| 287 | { |
| 288 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 289 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 290 | result = n == v; |
| 291 | } |
| 292 | break; |
| 293 | case 6: |
| 294 | { |
| 295 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 296 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 297 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 298 | } |
| 299 | break; |
| 300 | case 7: |
| 301 | result = true; |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | if (cond & 1) |
| 306 | result = !result; |
| 307 | return result; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | bool |
| 312 | EmulateInstructionARM::EvaluateInstruction () |
| 313 | { |
| 314 | return false; |
| 315 | } |