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) |
| 31 | #define ARMv8 (1u << 8) |
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 | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 70 | EmulateARMPushEncoding (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 | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 111 | uint32_t t; // t = UInt(Rt) |
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: |
| 130 | t = EmulateInstruction::UnsignedBits (opcode, 15, 12); |
| 131 | // if BadReg(t) then UNPREDICTABLE; |
| 132 | if (BadReg(t)) |
| 133 | return false; |
| 134 | registers = (1u << t); |
| 135 | break; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 136 | case eEncodingA1: |
| 137 | registers = EmulateInstruction::UnsignedBits (opcode, 15, 0); |
| 138 | break; |
| 139 | case eEncodingA2: |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 140 | t = EmulateInstruction::UnsignedBits (opcode, 15, 12); |
| 141 | // if t == 13 then UNPREDICTABLE; |
| 142 | if (t == dwarf_sp) |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 143 | return false; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 144 | registers = (1u << t); |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 145 | break; |
| 146 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 147 | addr_t sp_offset = addr_byte_size * EmulateInstruction::BitCount (registers); |
| 148 | addr_t addr = sp - sp_offset; |
| 149 | uint32_t i; |
| 150 | |
| 151 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 152 | for (i=0; i<15; ++i) |
| 153 | { |
| 154 | if (EmulateInstruction::BitIsSet (registers, 1u << i)) |
| 155 | { |
| 156 | context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number |
| 157 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 158 | uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
| 159 | if (!success) |
| 160 | return false; |
| 161 | if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
| 162 | return false; |
| 163 | addr += addr_byte_size; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (EmulateInstruction::BitIsSet (registers, 1u << 15)) |
| 168 | { |
| 169 | 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] | 170 | 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] | 171 | const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 172 | if (!success) |
| 173 | return false; |
| 174 | if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 179 | context.arg0 = eRegisterKindGeneric; |
| 180 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 181 | context.arg2 = sp_offset; |
| 182 | |
| 183 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
| 184 | return false; |
| 185 | } |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | static ARMOpcode g_arm_opcodes[] = |
| 190 | { |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 191 | { 0x0000fe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, EmulateARMPushEncoding, |
Johnny Chen | d6873ed | 2011-01-24 22:02:46 +0000 | [diff] [blame^] | 192 | "push<c> <registers>" }, |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 193 | { 0xffff0000, 0xe8ad0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, EmulateARMPushEncoding, |
Johnny Chen | d6873ed | 2011-01-24 22:02:46 +0000 | [diff] [blame^] | 194 | "push<c>.w <registers> ; <registers> contains more than one register" }, |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 195 | { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, EmulateARMPushEncoding, |
Johnny Chen | d6873ed | 2011-01-24 22:02:46 +0000 | [diff] [blame^] | 196 | "push<c>.w <registers> ; <registers> contains one register, <Rt>" }, |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 197 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, EmulateARMPushEncoding, |
Johnny Chen | d6873ed | 2011-01-24 22:02:46 +0000 | [diff] [blame^] | 198 | "push<c> <registers> ; <registers> contains more than one register" }, |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 199 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, EmulateARMPushEncoding, |
Johnny Chen | d6873ed | 2011-01-24 22:02:46 +0000 | [diff] [blame^] | 200 | "push<c> <registers> ; <registers> contains one register, <Rt>" } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode); |
| 204 | |
| 205 | bool |
| 206 | EmulateInstructionARM::ReadInstruction () |
| 207 | { |
| 208 | bool success = false; |
| 209 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 210 | if (success) |
| 211 | { |
| 212 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 213 | if (success) |
| 214 | { |
| 215 | Context read_inst_context = {eContextReadOpcode, 0, 0}; |
| 216 | if (m_inst_cpsr & MASK_CPSR_T) |
| 217 | { |
| 218 | m_inst_mode = eModeThumb; |
| 219 | uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success); |
| 220 | |
| 221 | if (success) |
| 222 | { |
| 223 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 224 | { |
| 225 | m_inst.opcode_type = eOpcode16; |
| 226 | m_inst.opcode.inst16 = thumb_opcode; |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | m_inst.opcode_type = eOpcode32; |
| 231 | m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | m_inst_mode = eModeARM; |
| 238 | m_inst.opcode_type = eOpcode32; |
| 239 | m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | if (!success) |
| 244 | { |
| 245 | m_inst_mode = eModeInvalid; |
| 246 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 247 | } |
| 248 | return success; |
| 249 | } |
| 250 | |
| 251 | uint32_t |
| 252 | EmulateInstructionARM::CurrentCond () |
| 253 | { |
| 254 | switch (m_inst_mode) |
| 255 | { |
| 256 | default: |
| 257 | case eModeInvalid: |
| 258 | break; |
| 259 | |
| 260 | case eModeARM: |
| 261 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 262 | |
| 263 | case eModeThumb: |
| 264 | return 0x0000000Eu; // Return always for now, we need to handl IT instructions later |
| 265 | } |
| 266 | return UINT32_MAX; // Return invalid value |
| 267 | } |
| 268 | bool |
| 269 | EmulateInstructionARM::ConditionPassed () |
| 270 | { |
| 271 | if (m_inst_cpsr == 0) |
| 272 | return false; |
| 273 | |
| 274 | const uint32_t cond = CurrentCond (); |
| 275 | |
| 276 | if (cond == UINT32_MAX) |
| 277 | return false; |
| 278 | |
| 279 | bool result = false; |
| 280 | switch (UnsignedBits(cond, 3, 1)) |
| 281 | { |
| 282 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 283 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 284 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 285 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 286 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 287 | case 5: |
| 288 | { |
| 289 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 290 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 291 | result = n == v; |
| 292 | } |
| 293 | break; |
| 294 | case 6: |
| 295 | { |
| 296 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 297 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 298 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 299 | } |
| 300 | break; |
| 301 | case 7: |
| 302 | result = true; |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | if (cond & 1) |
| 307 | result = !result; |
| 308 | return result; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | bool |
| 313 | EmulateInstructionARM::EvaluateInstruction () |
| 314 | { |
| 315 | return false; |
| 316 | } |