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 | 8584c92 | 2011-01-26 01:18:52 +0000 | [diff] [blame] | 11 | #include "ARMDefines.h" |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 12 | #include "ARMUtils.h" |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace lldb; |
| 15 | using namespace lldb_private; |
| 16 | |
| 17 | // ARM constants used during decoding |
| 18 | #define REG_RD 0 |
| 19 | #define LDM_REGLIST 1 |
| 20 | #define PC_REG 15 |
| 21 | #define PC_REGLIST_BIT 0x8000 |
| 22 | |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 23 | #define ARMv4 (1u << 0) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 24 | #define ARMv4T (1u << 1) |
| 25 | #define ARMv5T (1u << 2) |
| 26 | #define ARMv5TE (1u << 3) |
| 27 | #define ARMv5TEJ (1u << 4) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 28 | #define ARMv6 (1u << 5) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 29 | #define ARMv6K (1u << 6) |
| 30 | #define ARMv6T2 (1u << 7) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 31 | #define ARMv7 (1u << 8) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 32 | #define ARMv8 (1u << 9) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 33 | #define ARMvAll (0xffffffffu) |
| 34 | |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 35 | typedef enum |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 36 | { |
| 37 | eEncodingA1, |
| 38 | eEncodingA2, |
| 39 | eEncodingA3, |
| 40 | eEncodingA4, |
| 41 | eEncodingA5, |
| 42 | eEncodingT1, |
| 43 | eEncodingT2, |
| 44 | eEncodingT3, |
| 45 | eEncodingT4, |
| 46 | eEncodingT5, |
| 47 | } ARMEncoding; |
| 48 | |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 49 | typedef enum |
| 50 | { |
| 51 | eSize16, |
| 52 | eSize32 |
| 53 | } ARMInstrSize; |
| 54 | |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 55 | // Typedef for the callback function used during the emulation. |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 56 | // Pass along (ARMEncoding)encoding as the callback data. |
| 57 | typedef bool (*EmulateCallback) (EmulateInstructionARM *emulator, ARMEncoding encoding); |
| 58 | |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 59 | typedef struct |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 60 | { |
| 61 | uint32_t mask; |
| 62 | uint32_t value; |
| 63 | uint32_t variants; |
| 64 | ARMEncoding encoding; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 65 | ARMInstrSize size; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 66 | EmulateCallback callback; |
Johnny Chen | 4bee8ce | 2011-01-22 00:59:07 +0000 | [diff] [blame] | 67 | const char *name; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 68 | } ARMOpcode; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 69 | |
| 70 | static bool |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 71 | emulate_push (EmulateInstructionARM *emulator, ARMEncoding encoding) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 72 | { |
| 73 | #if 0 |
| 74 | // ARM pseudo code... |
| 75 | if (ConditionPassed()) |
| 76 | { |
| 77 | EncodingSpecificOperations(); |
| 78 | NullCheckIfThumbEE(13); |
| 79 | address = SP - 4*BitCount(registers); |
| 80 | |
| 81 | for (i = 0 to 14) |
| 82 | { |
| 83 | if (registers<i> == ’1’) |
| 84 | { |
| 85 | if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1 |
| 86 | MemA[address,4] = bits(32) UNKNOWN; |
| 87 | else |
| 88 | MemA[address,4] = R[i]; |
| 89 | address = address + 4; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (registers<15> == ’1’) // Only possible for encoding A1 or A2 |
| 94 | MemA[address,4] = PCStoreValue(); |
| 95 | |
| 96 | SP = SP - 4*BitCount(registers); |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | bool success = false; |
| 101 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 102 | if (!success) |
| 103 | return false; |
| 104 | |
| 105 | if (emulator->ConditionPassed()) |
| 106 | { |
| 107 | const uint32_t addr_byte_size = emulator->GetAddressByteSize(); |
| 108 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 109 | if (!success) |
| 110 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 111 | uint32_t registers = 0; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 112 | uint32_t Rt; // the source register |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 113 | switch (encoding) { |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 114 | case eEncodingT1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 115 | registers = Bits32(opcode, 7, 0); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 116 | // The M bit represents LR. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 117 | if (Bits32(opcode, 8, 8)) |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 118 | registers |= 0x000eu; |
| 119 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 120 | if (BitCount(registers) < 1) |
| 121 | return false; |
| 122 | break; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 123 | case eEncodingT2: |
| 124 | // Ignore bits 15 & 13. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 125 | registers = Bits32(opcode, 15, 0) & ~0xa000; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 126 | // if BitCount(registers) < 2 then UNPREDICTABLE; |
| 127 | if (BitCount(registers) < 2) |
| 128 | return false; |
| 129 | break; |
| 130 | case eEncodingT3: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 131 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 132 | // if BadReg(t) then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 133 | if (BadReg(Rt)) |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 134 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 135 | registers = (1u << Rt); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 136 | break; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 137 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 138 | registers = Bits32(opcode, 15, 0); |
Johnny Chen | a33d484 | 2011-01-24 22:25:48 +0000 | [diff] [blame] | 139 | // Instead of return false, let's handle the following case as well, |
| 140 | // which amounts to pushing one reg onto the full descending stacks. |
| 141 | // if BitCount(register_list) < 2 then SEE STMDB / STMFD; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 142 | break; |
| 143 | case eEncodingA2: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 144 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 145 | // if t == 13 then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 146 | if (Rt == dwarf_sp) |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 147 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 148 | registers = (1u << Rt); |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 149 | break; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 150 | default: |
| 151 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 152 | } |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 153 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 154 | addr_t addr = sp - sp_offset; |
| 155 | uint32_t i; |
| 156 | |
| 157 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 158 | for (i=0; i<15; ++i) |
| 159 | { |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 160 | if (BitIsSet (registers, 1u << i)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 161 | { |
| 162 | context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number |
| 163 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 164 | uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
| 165 | if (!success) |
| 166 | return false; |
| 167 | if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
| 168 | return false; |
| 169 | addr += addr_byte_size; |
| 170 | } |
| 171 | } |
| 172 | |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 173 | if (BitIsSet (registers, 1u << 15)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 174 | { |
| 175 | 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] | 176 | 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] | 177 | const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 178 | if (!success) |
| 179 | return false; |
| 180 | if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 185 | context.arg0 = eRegisterKindGeneric; |
| 186 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 187 | context.arg2 = sp_offset; |
| 188 | |
| 189 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
| 190 | return false; |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 195 | // A sub operation to adjust the SP -- allocate space for local storage. |
| 196 | static bool |
| 197 | emulate_sub_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 198 | { |
| 199 | #if 0 |
| 200 | // ARM pseudo code... |
| 201 | if (ConditionPassed()) |
| 202 | { |
| 203 | EncodingSpecificOperations(); |
| 204 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 205 | if d == 15 then // Can only occur for ARM encoding |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame^] | 206 | ALUWritePC(result); // setflags is always FALSE here |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 207 | else |
| 208 | R[d] = result; |
| 209 | if setflags then |
| 210 | APSR.N = result<31>; |
| 211 | APSR.Z = IsZeroBit(result); |
| 212 | APSR.C = carry; |
| 213 | APSR.V = overflow; |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | bool success = false; |
| 218 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 219 | if (!success) |
| 220 | return false; |
| 221 | |
| 222 | if (emulator->ConditionPassed()) |
| 223 | { |
| 224 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 225 | if (!success) |
| 226 | return false; |
| 227 | uint32_t imm32; |
| 228 | switch (encoding) { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 229 | case eEncodingT1: |
| 230 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 231 | case eEncodingT2: |
| 232 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 233 | break; |
| 234 | case eEncodingT3: |
| 235 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 236 | break; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 237 | case eEncodingA1: |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 238 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 239 | break; |
| 240 | default: |
| 241 | return false; |
| 242 | } |
| 243 | addr_t sp_offset = imm32; |
| 244 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 245 | |
| 246 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 247 | eRegisterKindGeneric, |
| 248 | LLDB_REGNUM_GENERIC_SP, |
| 249 | sp_offset }; |
| 250 | |
| 251 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
| 252 | return false; |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | // A store operation to the stacks that also updates the SP. |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 258 | static bool |
| 259 | emulate_str_rt_sp (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 260 | { |
| 261 | #if 0 |
| 262 | // ARM pseudo code... |
| 263 | if (ConditionPassed()) |
| 264 | { |
| 265 | EncodingSpecificOperations(); |
| 266 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 267 | address = if index then offset_addr else R[n]; |
| 268 | MemU[address,4] = if t == 15 then PCStoreValue() else R[t]; |
| 269 | if wback then R[n] = offset_addr; |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | bool success = false; |
| 274 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 275 | if (!success) |
| 276 | return false; |
| 277 | |
| 278 | if (emulator->ConditionPassed()) |
| 279 | { |
| 280 | const uint32_t addr_byte_size = emulator->GetAddressByteSize(); |
| 281 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 282 | if (!success) |
| 283 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 284 | uint32_t Rt; // the source register |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 285 | uint32_t imm12; |
| 286 | switch (encoding) { |
| 287 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 288 | Rt = Bits32(opcode, 15, 12); |
| 289 | imm12 = Bits32(opcode, 11, 0); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 290 | break; |
| 291 | default: |
| 292 | return false; |
| 293 | } |
| 294 | addr_t sp_offset = imm12; |
| 295 | addr_t addr = sp - sp_offset; |
| 296 | |
| 297 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 298 | if (Rt != 15) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 299 | { |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 300 | context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number |
| 301 | 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] | 302 | uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
| 303 | if (!success) |
| 304 | return false; |
| 305 | if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
| 306 | return false; |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number |
| 311 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 312 | const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 313 | if (!success) |
| 314 | return false; |
| 315 | if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 320 | context.arg0 = eRegisterKindGeneric; |
| 321 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 322 | context.arg2 = sp_offset; |
| 323 | |
| 324 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
| 325 | return false; |
| 326 | } |
| 327 | return true; |
| 328 | } |
| 329 | |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame^] | 330 | static bool |
| 331 | emulate_vpush (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 332 | { |
| 333 | #if 0 |
| 334 | // ARM pseudo code... |
| 335 | if (ConditionPassed()) |
| 336 | { |
| 337 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 338 | address = SP - imm32; |
| 339 | SP = SP - imm32; |
| 340 | if single_regs then |
| 341 | for r = 0 to regs-1 |
| 342 | MemA[address,4] = S[d+r]; address = address+4; |
| 343 | else |
| 344 | for r = 0 to regs-1 |
| 345 | // Store as two word-aligned words in the correct order for current endianness. |
| 346 | MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>; |
| 347 | MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>; |
| 348 | address = address+8; |
| 349 | } |
| 350 | #endif |
| 351 | |
| 352 | bool success = false; |
| 353 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 354 | if (!success) |
| 355 | return false; |
| 356 | |
| 357 | if (emulator->ConditionPassed()) |
| 358 | { |
| 359 | const uint32_t addr_byte_size = emulator->GetAddressByteSize(); |
| 360 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 361 | if (!success) |
| 362 | return false; |
| 363 | bool single_regs; |
| 364 | uint32_t d; // UInt(Vd:D) starting register |
| 365 | uint32_t imm32; // stack offset |
| 366 | uint32_t regs; // number of registers |
| 367 | switch (encoding) { |
| 368 | case eEncodingT1: |
| 369 | case eEncodingA1: |
| 370 | single_regs = false; |
| 371 | d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22); |
| 372 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 373 | // If UInt(imm8) is odd, see "FSTMX". |
| 374 | regs = Bits32(opcode, 7, 0) / 2; |
| 375 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 376 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 377 | return false; |
| 378 | break; |
| 379 | case eEncodingT2: |
| 380 | case eEncodingA2: |
| 381 | single_regs = true; |
| 382 | d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22); |
| 383 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 384 | regs = Bits32(opcode, 7, 0); |
| 385 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 386 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 387 | return false; |
| 388 | break; |
| 389 | default: |
| 390 | return false; |
| 391 | } |
| 392 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 393 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 394 | addr_t sp_offset = imm32; |
| 395 | addr_t addr = sp - sp_offset; |
| 396 | uint32_t i; |
| 397 | |
| 398 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 399 | for (i=d; i<regs; ++i) |
| 400 | { |
| 401 | context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number |
| 402 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 403 | // uint64_t to accommodate 64-bit registers. |
| 404 | uint64_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
| 405 | if (!success) |
| 406 | return false; |
| 407 | if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size)) |
| 408 | return false; |
| 409 | addr += reg_byte_size; |
| 410 | } |
| 411 | |
| 412 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 413 | context.arg0 = eRegisterKindGeneric; |
| 414 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 415 | context.arg2 = sp_offset; |
| 416 | |
| 417 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
| 418 | return false; |
| 419 | } |
| 420 | return true; |
| 421 | } |
| 422 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 423 | static ARMOpcode g_arm_opcodes[] = |
| 424 | { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 425 | // push register(s) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 426 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, emulate_push, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 427 | "push <registers> ; <registers> contains more than one register" }, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 428 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, emulate_push, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 429 | "push <registers> ; <registers> contains one register, <Rt>" }, |
| 430 | |
| 431 | // adjust the stack pointer |
| 432 | { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, emulate_sub_sp_imm, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 433 | "sub sp, sp, #<const>"}, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 434 | |
| 435 | // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH; |
| 436 | { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, emulate_str_rt_sp, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame^] | 437 | "str Rt, [sp, #-<imm12>]!" }, |
| 438 | |
| 439 | // vector push consecutive extension register(s) |
| 440 | { 0x0fbf0f00, 0x0d2d0b00, ARMv6T2|ARMv7, eEncodingA1, eSize32, emulate_vpush, |
| 441 | "vpush.64 <list>"}, |
| 442 | { 0x0fbf0f00, 0x0d2d0a00, ARMv6T2|ARMv7, eEncodingA2, eSize32, emulate_vpush, |
| 443 | "vpush.32 <list>"} |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 446 | static ARMOpcode g_thumb_opcodes[] = |
| 447 | { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 448 | // push register(s) |
| 449 | { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, emulate_push, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 450 | "push <registers>" }, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 451 | { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_push, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 452 | "push.w <registers> ; <registers> contains more than one register" }, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 453 | { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_push, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 454 | "push.w <registers> ; <registers> contains one register, <Rt>" }, |
| 455 | |
| 456 | // adjust the stack pointer |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 457 | { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, emulate_sub_sp_imm, |
| 458 | "sub{s} sp, sp, #<imm>"}, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 459 | { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_sub_sp_imm, |
| 460 | "sub{s}.w sp, sp, #<const>"}, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 461 | { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_sub_sp_imm, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame^] | 462 | "subw sp, sp, #<imm12>"}, |
| 463 | |
| 464 | // vector push consecutive extension register(s) |
| 465 | { 0xffbf0f00, 0xed2d0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, emulate_vpush, |
| 466 | "vpush.64 <list>"}, |
| 467 | { 0xffbf0f00, 0xed2d0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_vpush, |
| 468 | "vpush.32 <list>"} |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 469 | }; |
| 470 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 471 | 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] | 472 | 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] | 473 | |
| 474 | bool |
| 475 | EmulateInstructionARM::ReadInstruction () |
| 476 | { |
| 477 | bool success = false; |
| 478 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 479 | if (success) |
| 480 | { |
| 481 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 482 | if (success) |
| 483 | { |
| 484 | Context read_inst_context = {eContextReadOpcode, 0, 0}; |
| 485 | if (m_inst_cpsr & MASK_CPSR_T) |
| 486 | { |
| 487 | m_inst_mode = eModeThumb; |
| 488 | uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success); |
| 489 | |
| 490 | if (success) |
| 491 | { |
| 492 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 493 | { |
| 494 | m_inst.opcode_type = eOpcode16; |
| 495 | m_inst.opcode.inst16 = thumb_opcode; |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | m_inst.opcode_type = eOpcode32; |
| 500 | m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success); |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | m_inst_mode = eModeARM; |
| 507 | m_inst.opcode_type = eOpcode32; |
| 508 | m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | if (!success) |
| 513 | { |
| 514 | m_inst_mode = eModeInvalid; |
| 515 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 516 | } |
| 517 | return success; |
| 518 | } |
| 519 | |
| 520 | uint32_t |
| 521 | EmulateInstructionARM::CurrentCond () |
| 522 | { |
| 523 | switch (m_inst_mode) |
| 524 | { |
| 525 | default: |
| 526 | case eModeInvalid: |
| 527 | break; |
| 528 | |
| 529 | case eModeARM: |
| 530 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 531 | |
| 532 | case eModeThumb: |
| 533 | return 0x0000000Eu; // Return always for now, we need to handl IT instructions later |
| 534 | } |
| 535 | return UINT32_MAX; // Return invalid value |
| 536 | } |
| 537 | bool |
| 538 | EmulateInstructionARM::ConditionPassed () |
| 539 | { |
| 540 | if (m_inst_cpsr == 0) |
| 541 | return false; |
| 542 | |
| 543 | const uint32_t cond = CurrentCond (); |
| 544 | |
| 545 | if (cond == UINT32_MAX) |
| 546 | return false; |
| 547 | |
| 548 | bool result = false; |
| 549 | switch (UnsignedBits(cond, 3, 1)) |
| 550 | { |
| 551 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 552 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 553 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 554 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 555 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 556 | case 5: |
| 557 | { |
| 558 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 559 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 560 | result = n == v; |
| 561 | } |
| 562 | break; |
| 563 | case 6: |
| 564 | { |
| 565 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 566 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 567 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 568 | } |
| 569 | break; |
| 570 | case 7: |
| 571 | result = true; |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | if (cond & 1) |
| 576 | result = !result; |
| 577 | return result; |
| 578 | } |
| 579 | |
| 580 | |
| 581 | bool |
| 582 | EmulateInstructionARM::EvaluateInstruction () |
| 583 | { |
| 584 | return false; |
| 585 | } |