bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 1 | /* General "disassemble this chunk" code. Used for debugging. */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 2 | #include "qemu/osdep.h" |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 3 | #include "qemu-common.h" |
Markus Armbruster | 3979fca | 2019-04-17 21:18:04 +0200 | [diff] [blame] | 4 | #include "disas/dis-asm.h" |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 5 | #include "elf.h" |
Markus Armbruster | 30cc983 | 2019-04-17 21:18:03 +0200 | [diff] [blame] | 6 | #include "qemu/qemu-print.h" |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 7 | |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 8 | #include "cpu.h" |
Paolo Bonzini | 76cad71 | 2012-10-24 11:12:21 +0200 | [diff] [blame] | 9 | #include "disas/disas.h" |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 10 | #include "disas/capstone.h" |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 11 | |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 12 | typedef struct CPUDebug { |
| 13 | struct disassemble_info info; |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 14 | CPUState *cpu; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 15 | } CPUDebug; |
| 16 | |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 17 | /* Filled in by elfload.c. Simplistic, but will do for now. */ |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 18 | struct syminfo *syminfos = NULL; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 19 | |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 20 | /* Get LENGTH bytes from info's buffer, at target address memaddr. |
| 21 | Transfer them to myaddr. */ |
| 22 | int |
pbrook | 3a742b7 | 2008-10-22 15:55:18 +0000 | [diff] [blame] | 23 | buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length, |
| 24 | struct disassemble_info *info) |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 25 | { |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 26 | if (memaddr < info->buffer_vma |
| 27 | || memaddr + length > info->buffer_vma + info->buffer_length) |
| 28 | /* Out of bounds. Use EIO because GDB uses it. */ |
| 29 | return EIO; |
| 30 | memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length); |
| 31 | return 0; |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 32 | } |
| 33 | |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 34 | /* Get LENGTH bytes from info's buffer, at target address memaddr. |
| 35 | Transfer them to myaddr. */ |
| 36 | static int |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 37 | target_read_memory (bfd_vma memaddr, |
| 38 | bfd_byte *myaddr, |
| 39 | int length, |
| 40 | struct disassemble_info *info) |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 41 | { |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 42 | CPUDebug *s = container_of(info, CPUDebug, info); |
| 43 | |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 44 | cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0); |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 47 | |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 48 | /* Print an error message. We can assume that this is in response to |
| 49 | an error return from buffer_read_memory. */ |
| 50 | void |
pbrook | 3a742b7 | 2008-10-22 15:55:18 +0000 | [diff] [blame] | 51 | perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info) |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 52 | { |
| 53 | if (status != EIO) |
| 54 | /* Can't happen. */ |
| 55 | (*info->fprintf_func) (info->stream, "Unknown error %d\n", status); |
| 56 | else |
| 57 | /* Actually, address between memaddr and memaddr + len was |
| 58 | out of bounds. */ |
| 59 | (*info->fprintf_func) (info->stream, |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 60 | "Address 0x%" PRIx64 " is out of bounds.\n", memaddr); |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Jim Meyering | a31f053 | 2012-05-09 05:12:04 +0000 | [diff] [blame] | 63 | /* This could be in a separate file, to save minuscule amounts of space |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 64 | in statically linked executables. */ |
| 65 | |
| 66 | /* Just print the address is hex. This is included for completeness even |
| 67 | though both GDB and objdump provide their own (to print symbolic |
| 68 | addresses). */ |
| 69 | |
| 70 | void |
pbrook | 3a742b7 | 2008-10-22 15:55:18 +0000 | [diff] [blame] | 71 | generic_print_address (bfd_vma addr, struct disassemble_info *info) |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 72 | { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 73 | (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr); |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Peter Maydell | 636bd28 | 2012-06-25 04:55:55 +0000 | [diff] [blame] | 76 | /* Print address in hex, truncated to the width of a host virtual address. */ |
| 77 | static void |
| 78 | generic_print_host_address(bfd_vma addr, struct disassemble_info *info) |
| 79 | { |
| 80 | uint64_t mask = ~0ULL >> (64 - (sizeof(void *) * 8)); |
| 81 | generic_print_address(addr & mask, info); |
| 82 | } |
| 83 | |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 84 | /* Just return the given address. */ |
| 85 | |
| 86 | int |
pbrook | 3a742b7 | 2008-10-22 15:55:18 +0000 | [diff] [blame] | 87 | generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info) |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 88 | { |
| 89 | return 1; |
| 90 | } |
| 91 | |
Aurelien Jarno | 903ec55 | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 92 | bfd_vma bfd_getl64 (const bfd_byte *addr) |
| 93 | { |
| 94 | unsigned long long v; |
| 95 | |
| 96 | v = (unsigned long long) addr[0]; |
| 97 | v |= (unsigned long long) addr[1] << 8; |
| 98 | v |= (unsigned long long) addr[2] << 16; |
| 99 | v |= (unsigned long long) addr[3] << 24; |
| 100 | v |= (unsigned long long) addr[4] << 32; |
| 101 | v |= (unsigned long long) addr[5] << 40; |
| 102 | v |= (unsigned long long) addr[6] << 48; |
| 103 | v |= (unsigned long long) addr[7] << 56; |
| 104 | return (bfd_vma) v; |
| 105 | } |
| 106 | |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 107 | bfd_vma bfd_getl32 (const bfd_byte *addr) |
| 108 | { |
| 109 | unsigned long v; |
| 110 | |
| 111 | v = (unsigned long) addr[0]; |
| 112 | v |= (unsigned long) addr[1] << 8; |
| 113 | v |= (unsigned long) addr[2] << 16; |
| 114 | v |= (unsigned long) addr[3] << 24; |
| 115 | return (bfd_vma) v; |
| 116 | } |
| 117 | |
| 118 | bfd_vma bfd_getb32 (const bfd_byte *addr) |
| 119 | { |
| 120 | unsigned long v; |
| 121 | |
| 122 | v = (unsigned long) addr[0] << 24; |
| 123 | v |= (unsigned long) addr[1] << 16; |
| 124 | v |= (unsigned long) addr[2] << 8; |
| 125 | v |= (unsigned long) addr[3]; |
| 126 | return (bfd_vma) v; |
| 127 | } |
| 128 | |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 129 | bfd_vma bfd_getl16 (const bfd_byte *addr) |
| 130 | { |
| 131 | unsigned long v; |
| 132 | |
| 133 | v = (unsigned long) addr[0]; |
| 134 | v |= (unsigned long) addr[1] << 8; |
| 135 | return (bfd_vma) v; |
| 136 | } |
| 137 | |
| 138 | bfd_vma bfd_getb16 (const bfd_byte *addr) |
| 139 | { |
| 140 | unsigned long v; |
| 141 | |
| 142 | v = (unsigned long) addr[0] << 24; |
| 143 | v |= (unsigned long) addr[1] << 16; |
| 144 | return (bfd_vma) v; |
| 145 | } |
| 146 | |
Richard Henderson | c46ffd5 | 2013-08-16 23:29:45 -0700 | [diff] [blame] | 147 | static int print_insn_objdump(bfd_vma pc, disassemble_info *info, |
| 148 | const char *prefix) |
| 149 | { |
| 150 | int i, n = info->buffer_length; |
| 151 | uint8_t *buf = g_malloc(n); |
| 152 | |
| 153 | info->read_memory_func(pc, buf, n, info); |
| 154 | |
| 155 | for (i = 0; i < n; ++i) { |
| 156 | if (i % 32 == 0) { |
| 157 | info->fprintf_func(info->stream, "\n%s: ", prefix); |
| 158 | } |
| 159 | info->fprintf_func(info->stream, "%02x", buf[i]); |
| 160 | } |
| 161 | |
| 162 | g_free(buf); |
| 163 | return n; |
| 164 | } |
| 165 | |
| 166 | static int print_insn_od_host(bfd_vma pc, disassemble_info *info) |
| 167 | { |
| 168 | return print_insn_objdump(pc, info, "OBJD-H"); |
| 169 | } |
| 170 | |
| 171 | static int print_insn_od_target(bfd_vma pc, disassemble_info *info) |
| 172 | { |
| 173 | return print_insn_objdump(pc, info, "OBJD-T"); |
| 174 | } |
| 175 | |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 176 | #ifdef CONFIG_CAPSTONE |
| 177 | /* Temporary storage for the capstone library. This will be alloced via |
| 178 | malloc with a size private to the library; thus there's no reason not |
| 179 | to share this across calls and across host vs target disassembly. */ |
| 180 | static __thread cs_insn *cap_insn; |
| 181 | |
| 182 | /* Initialize the Capstone library. */ |
| 183 | /* ??? It would be nice to cache this. We would need one handle for the |
| 184 | host and one for the target. For most targets we can reset specific |
| 185 | parameters via cs_option(CS_OPT_MODE, new_mode), but we cannot change |
| 186 | CS_ARCH_* in this way. Thus we would need to be able to close and |
| 187 | re-open the target handle with a different arch for the target in order |
| 188 | to handle AArch64 vs AArch32 mode switching. */ |
| 189 | static cs_err cap_disas_start(disassemble_info *info, csh *handle) |
| 190 | { |
| 191 | cs_mode cap_mode = info->cap_mode; |
| 192 | cs_err err; |
| 193 | |
| 194 | cap_mode += (info->endian == BFD_ENDIAN_BIG ? CS_MODE_BIG_ENDIAN |
| 195 | : CS_MODE_LITTLE_ENDIAN); |
| 196 | |
| 197 | err = cs_open(info->cap_arch, cap_mode, handle); |
| 198 | if (err != CS_ERR_OK) { |
| 199 | return err; |
| 200 | } |
| 201 | |
| 202 | /* ??? There probably ought to be a better place to put this. */ |
| 203 | if (info->cap_arch == CS_ARCH_X86) { |
| 204 | /* We don't care about errors (if for some reason the library |
| 205 | is compiled without AT&T syntax); the user will just have |
| 206 | to deal with the Intel syntax. */ |
| 207 | cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); |
| 208 | } |
| 209 | |
| 210 | /* "Disassemble" unknown insns as ".byte W,X,Y,Z". */ |
| 211 | cs_option(*handle, CS_OPT_SKIPDATA, CS_OPT_ON); |
| 212 | |
| 213 | /* Allocate temp space for cs_disasm_iter. */ |
| 214 | if (cap_insn == NULL) { |
| 215 | cap_insn = cs_malloc(*handle); |
| 216 | if (cap_insn == NULL) { |
| 217 | cs_close(handle); |
| 218 | return CS_ERR_MEM; |
| 219 | } |
| 220 | } |
| 221 | return CS_ERR_OK; |
| 222 | } |
| 223 | |
Richard Henderson | 15fa1a0 | 2017-11-07 13:19:18 +0100 | [diff] [blame] | 224 | static void cap_dump_insn_units(disassemble_info *info, cs_insn *insn, |
| 225 | int i, int n) |
| 226 | { |
| 227 | fprintf_function print = info->fprintf_func; |
| 228 | FILE *stream = info->stream; |
| 229 | |
| 230 | switch (info->cap_insn_unit) { |
| 231 | case 4: |
| 232 | if (info->endian == BFD_ENDIAN_BIG) { |
| 233 | for (; i < n; i += 4) { |
| 234 | print(stream, " %08x", ldl_be_p(insn->bytes + i)); |
| 235 | |
| 236 | } |
| 237 | } else { |
| 238 | for (; i < n; i += 4) { |
| 239 | print(stream, " %08x", ldl_le_p(insn->bytes + i)); |
| 240 | } |
| 241 | } |
| 242 | break; |
| 243 | |
| 244 | case 2: |
| 245 | if (info->endian == BFD_ENDIAN_BIG) { |
| 246 | for (; i < n; i += 2) { |
| 247 | print(stream, " %04x", lduw_be_p(insn->bytes + i)); |
| 248 | } |
| 249 | } else { |
| 250 | for (; i < n; i += 2) { |
| 251 | print(stream, " %04x", lduw_le_p(insn->bytes + i)); |
| 252 | } |
| 253 | } |
| 254 | break; |
| 255 | |
| 256 | default: |
| 257 | for (; i < n; i++) { |
| 258 | print(stream, " %02x", insn->bytes[i]); |
| 259 | } |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static void cap_dump_insn(disassemble_info *info, cs_insn *insn) |
| 265 | { |
| 266 | fprintf_function print = info->fprintf_func; |
| 267 | int i, n, split; |
| 268 | |
| 269 | print(info->stream, "0x%08" PRIx64 ": ", insn->address); |
| 270 | |
| 271 | n = insn->size; |
| 272 | split = info->cap_insn_split; |
| 273 | |
| 274 | /* Dump the first SPLIT bytes of the instruction. */ |
| 275 | cap_dump_insn_units(info, insn, 0, MIN(n, split)); |
| 276 | |
| 277 | /* Add padding up to SPLIT so that mnemonics line up. */ |
| 278 | if (n < split) { |
| 279 | int width = (split - n) / info->cap_insn_unit; |
| 280 | width *= (2 * info->cap_insn_unit + 1); |
| 281 | print(info->stream, "%*s", width, ""); |
| 282 | } |
| 283 | |
| 284 | /* Print the actual instruction. */ |
| 285 | print(info->stream, " %-8s %s\n", insn->mnemonic, insn->op_str); |
| 286 | |
| 287 | /* Dump any remaining part of the insn on subsequent lines. */ |
| 288 | for (i = split; i < n; i += split) { |
| 289 | print(info->stream, "0x%08" PRIx64 ": ", insn->address + i); |
| 290 | cap_dump_insn_units(info, insn, i, MIN(n, i + split)); |
| 291 | print(info->stream, "\n"); |
| 292 | } |
| 293 | } |
| 294 | |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 295 | /* Disassemble SIZE bytes at PC for the target. */ |
| 296 | static bool cap_disas_target(disassemble_info *info, uint64_t pc, size_t size) |
| 297 | { |
| 298 | uint8_t cap_buf[1024]; |
| 299 | csh handle; |
| 300 | cs_insn *insn; |
| 301 | size_t csize = 0; |
| 302 | |
| 303 | if (cap_disas_start(info, &handle) != CS_ERR_OK) { |
| 304 | return false; |
| 305 | } |
| 306 | insn = cap_insn; |
| 307 | |
| 308 | while (1) { |
| 309 | size_t tsize = MIN(sizeof(cap_buf) - csize, size); |
| 310 | const uint8_t *cbuf = cap_buf; |
| 311 | |
| 312 | target_read_memory(pc + csize, cap_buf + csize, tsize, info); |
| 313 | csize += tsize; |
| 314 | size -= tsize; |
| 315 | |
| 316 | while (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) { |
Richard Henderson | 15fa1a0 | 2017-11-07 13:19:18 +0100 | [diff] [blame] | 317 | cap_dump_insn(info, insn); |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* If the target memory is not consumed, go back for more... */ |
| 321 | if (size != 0) { |
| 322 | /* ... taking care to move any remaining fractional insn |
| 323 | to the beginning of the buffer. */ |
| 324 | if (csize != 0) { |
| 325 | memmove(cap_buf, cbuf, csize); |
| 326 | } |
| 327 | continue; |
| 328 | } |
| 329 | |
| 330 | /* Since the target memory is consumed, we should not have |
| 331 | a remaining fractional insn. */ |
| 332 | if (csize != 0) { |
| 333 | (*info->fprintf_func)(info->stream, |
| 334 | "Disassembler disagrees with translator " |
| 335 | "over instruction decoding\n" |
| 336 | "Please report this to qemu-devel@nongnu.org\n"); |
| 337 | } |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | cs_close(&handle); |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | /* Disassemble SIZE bytes at CODE for the host. */ |
| 346 | static bool cap_disas_host(disassemble_info *info, void *code, size_t size) |
| 347 | { |
| 348 | csh handle; |
| 349 | const uint8_t *cbuf; |
| 350 | cs_insn *insn; |
| 351 | uint64_t pc; |
| 352 | |
| 353 | if (cap_disas_start(info, &handle) != CS_ERR_OK) { |
| 354 | return false; |
| 355 | } |
| 356 | insn = cap_insn; |
| 357 | |
| 358 | cbuf = code; |
| 359 | pc = (uintptr_t)code; |
| 360 | |
| 361 | while (cs_disasm_iter(handle, &cbuf, &size, &pc, insn)) { |
Richard Henderson | 15fa1a0 | 2017-11-07 13:19:18 +0100 | [diff] [blame] | 362 | cap_dump_insn(info, insn); |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 363 | } |
| 364 | if (size != 0) { |
| 365 | (*info->fprintf_func)(info->stream, |
| 366 | "Disassembler disagrees with TCG over instruction encoding\n" |
| 367 | "Please report this to qemu-devel@nongnu.org\n"); |
| 368 | } |
| 369 | |
| 370 | cs_close(&handle); |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | #if !defined(CONFIG_USER_ONLY) |
| 375 | /* Disassemble COUNT insns at PC for the target. */ |
| 376 | static bool cap_disas_monitor(disassemble_info *info, uint64_t pc, int count) |
| 377 | { |
| 378 | uint8_t cap_buf[32]; |
| 379 | csh handle; |
| 380 | cs_insn *insn; |
| 381 | size_t csize = 0; |
| 382 | |
| 383 | if (cap_disas_start(info, &handle) != CS_ERR_OK) { |
| 384 | return false; |
| 385 | } |
| 386 | insn = cap_insn; |
| 387 | |
| 388 | while (1) { |
| 389 | /* We want to read memory for one insn, but generically we do not |
| 390 | know how much memory that is. We have a small buffer which is |
| 391 | known to be sufficient for all supported targets. Try to not |
| 392 | read beyond the page, Just In Case. For even more simplicity, |
| 393 | ignore the actual target page size and use a 1k boundary. If |
| 394 | that turns out to be insufficient, we'll come back around the |
| 395 | loop and read more. */ |
| 396 | uint64_t epc = QEMU_ALIGN_UP(pc + csize + 1, 1024); |
| 397 | size_t tsize = MIN(sizeof(cap_buf) - csize, epc - pc); |
| 398 | const uint8_t *cbuf = cap_buf; |
| 399 | |
| 400 | /* Make certain that we can make progress. */ |
| 401 | assert(tsize != 0); |
| 402 | info->read_memory_func(pc, cap_buf + csize, tsize, info); |
| 403 | csize += tsize; |
| 404 | |
| 405 | if (cs_disasm_iter(handle, &cbuf, &csize, &pc, insn)) { |
Richard Henderson | 15fa1a0 | 2017-11-07 13:19:18 +0100 | [diff] [blame] | 406 | cap_dump_insn(info, insn); |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 407 | if (--count <= 0) { |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | memmove(cap_buf, cbuf, csize); |
| 412 | } |
| 413 | |
| 414 | cs_close(&handle); |
| 415 | return true; |
| 416 | } |
| 417 | #endif /* !CONFIG_USER_ONLY */ |
| 418 | #else |
| 419 | # define cap_disas_target(i, p, s) false |
| 420 | # define cap_disas_host(i, p, s) false |
| 421 | # define cap_disas_monitor(i, p, c) false |
| 422 | #endif /* CONFIG_CAPSTONE */ |
| 423 | |
Richard Henderson | 1d48474 | 2017-09-14 08:38:35 -0700 | [diff] [blame] | 424 | /* Disassemble this for me please... (debugging). */ |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 425 | void target_disas(FILE *out, CPUState *cpu, target_ulong code, |
Richard Henderson | 1d48474 | 2017-09-14 08:38:35 -0700 | [diff] [blame] | 426 | target_ulong size) |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 427 | { |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 428 | CPUClass *cc = CPU_GET_CLASS(cpu); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 429 | target_ulong pc; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 430 | int count; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 431 | CPUDebug s; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 432 | |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 433 | INIT_DISASSEMBLE_INFO(s.info, out, fprintf); |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 434 | |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 435 | s.cpu = cpu; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 436 | s.info.read_memory_func = target_read_memory; |
| 437 | s.info.buffer_vma = code; |
| 438 | s.info.buffer_length = size; |
Peter Crosthwaite | 9504c54 | 2015-07-05 13:50:32 -0700 | [diff] [blame] | 439 | s.info.print_address_func = generic_print_address; |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 440 | s.info.cap_arch = -1; |
| 441 | s.info.cap_mode = 0; |
Richard Henderson | 15fa1a0 | 2017-11-07 13:19:18 +0100 | [diff] [blame] | 442 | s.info.cap_insn_unit = 4; |
| 443 | s.info.cap_insn_split = 4; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 444 | |
| 445 | #ifdef TARGET_WORDS_BIGENDIAN |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 446 | s.info.endian = BFD_ENDIAN_BIG; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 447 | #else |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 448 | s.info.endian = BFD_ENDIAN_LITTLE; |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 449 | #endif |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 450 | |
| 451 | if (cc->disas_set_info) { |
| 452 | cc->disas_set_info(cpu, &s.info); |
| 453 | } |
| 454 | |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 455 | if (s.info.cap_arch >= 0 && cap_disas_target(&s.info, code, size)) { |
| 456 | return; |
| 457 | } |
| 458 | |
Peter Crosthwaite | 2de295c | 2015-06-23 20:57:32 -0700 | [diff] [blame] | 459 | if (s.info.print_insn == NULL) { |
| 460 | s.info.print_insn = print_insn_od_target; |
Richard Henderson | c46ffd5 | 2013-08-16 23:29:45 -0700 | [diff] [blame] | 461 | } |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 462 | |
blueswir1 | 7e000c2 | 2009-02-13 21:44:41 +0000 | [diff] [blame] | 463 | for (pc = code; size > 0; pc += count, size -= count) { |
bellard | fa15e03 | 2005-01-31 23:32:31 +0000 | [diff] [blame] | 464 | fprintf(out, "0x" TARGET_FMT_lx ": ", pc); |
Peter Crosthwaite | 2de295c | 2015-06-23 20:57:32 -0700 | [diff] [blame] | 465 | count = s.info.print_insn(pc, &s.info); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 466 | fprintf(out, "\n"); |
| 467 | if (count < 0) |
| 468 | break; |
malc | 754d00a | 2009-04-21 22:26:22 +0000 | [diff] [blame] | 469 | if (size < count) { |
| 470 | fprintf(out, |
| 471 | "Disassembler disagrees with translator over instruction " |
| 472 | "decoding\n" |
| 473 | "Please report this to qemu-devel@nongnu.org\n"); |
| 474 | break; |
| 475 | } |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | /* Disassemble this for me please... (debugging). */ |
| 480 | void disas(FILE *out, void *code, unsigned long size) |
| 481 | { |
Stefan Weil | b0b0f1c | 2012-04-12 15:44:35 +0200 | [diff] [blame] | 482 | uintptr_t pc; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 483 | int count; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 484 | CPUDebug s; |
Richard Henderson | c46ffd5 | 2013-08-16 23:29:45 -0700 | [diff] [blame] | 485 | int (*print_insn)(bfd_vma pc, disassemble_info *info) = NULL; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 486 | |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 487 | INIT_DISASSEMBLE_INFO(s.info, out, fprintf); |
| 488 | s.info.print_address_func = generic_print_host_address; |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 489 | |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 490 | s.info.buffer = code; |
| 491 | s.info.buffer_vma = (uintptr_t)code; |
| 492 | s.info.buffer_length = size; |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 493 | s.info.cap_arch = -1; |
| 494 | s.info.cap_mode = 0; |
Richard Henderson | 15fa1a0 | 2017-11-07 13:19:18 +0100 | [diff] [blame] | 495 | s.info.cap_insn_unit = 4; |
| 496 | s.info.cap_insn_split = 4; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 497 | |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 498 | #ifdef HOST_WORDS_BIGENDIAN |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 499 | s.info.endian = BFD_ENDIAN_BIG; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 500 | #else |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 501 | s.info.endian = BFD_ENDIAN_LITTLE; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 502 | #endif |
Stefan Weil | 5826e51 | 2011-10-05 20:03:53 +0200 | [diff] [blame] | 503 | #if defined(CONFIG_TCG_INTERPRETER) |
| 504 | print_insn = print_insn_tci; |
| 505 | #elif defined(__i386__) |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 506 | s.info.mach = bfd_mach_i386_i386; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 507 | print_insn = print_insn_i386; |
Richard Henderson | b666d2a | 2017-09-14 09:50:05 -0700 | [diff] [blame] | 508 | s.info.cap_arch = CS_ARCH_X86; |
| 509 | s.info.cap_mode = CS_MODE_32; |
Richard Henderson | 15fa1a0 | 2017-11-07 13:19:18 +0100 | [diff] [blame] | 510 | s.info.cap_insn_unit = 1; |
| 511 | s.info.cap_insn_split = 8; |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 512 | #elif defined(__x86_64__) |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 513 | s.info.mach = bfd_mach_x86_64; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 514 | print_insn = print_insn_i386; |
Richard Henderson | b666d2a | 2017-09-14 09:50:05 -0700 | [diff] [blame] | 515 | s.info.cap_arch = CS_ARCH_X86; |
| 516 | s.info.cap_mode = CS_MODE_64; |
Richard Henderson | 15fa1a0 | 2017-11-07 13:19:18 +0100 | [diff] [blame] | 517 | s.info.cap_insn_unit = 1; |
| 518 | s.info.cap_insn_split = 8; |
malc | e58ffeb | 2009-01-14 18:39:49 +0000 | [diff] [blame] | 519 | #elif defined(_ARCH_PPC) |
Richard Henderson | 66d4f6a | 2013-01-31 11:16:21 -0800 | [diff] [blame] | 520 | s.info.disassembler_options = (char *)"any"; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 521 | print_insn = print_insn_ppc; |
Richard Henderson | ac22689 | 2017-09-14 10:38:40 -0700 | [diff] [blame] | 522 | s.info.cap_arch = CS_ARCH_PPC; |
| 523 | # ifdef _ARCH_PPC64 |
| 524 | s.info.cap_mode = CS_MODE_64; |
| 525 | # endif |
Alistair Francis | 91468b2 | 2018-12-19 19:20:09 +0000 | [diff] [blame] | 526 | #elif defined(__riscv) && defined(CONFIG_RISCV_DIS) |
| 527 | #if defined(_ILP32) || (__riscv_xlen == 32) |
| 528 | print_insn = print_insn_riscv32; |
| 529 | #elif defined(_LP64) |
| 530 | print_insn = print_insn_riscv64; |
| 531 | #else |
| 532 | #error unsupported RISC-V ABI |
| 533 | #endif |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 534 | #elif defined(__aarch64__) && defined(CONFIG_ARM_A64_DIS) |
| 535 | print_insn = print_insn_arm_a64; |
Richard Henderson | 110f6c7 | 2017-09-14 09:51:06 -0700 | [diff] [blame] | 536 | s.info.cap_arch = CS_ARCH_ARM64; |
bellard | a993ba8 | 2003-05-11 12:25:45 +0000 | [diff] [blame] | 537 | #elif defined(__alpha__) |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 538 | print_insn = print_insn_alpha; |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 539 | #elif defined(__sparc__) |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 540 | print_insn = print_insn_sparc; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 541 | s.info.mach = bfd_mach_sparc_v9b; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 542 | #elif defined(__arm__) |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 543 | print_insn = print_insn_arm; |
Richard Henderson | 110f6c7 | 2017-09-14 09:51:06 -0700 | [diff] [blame] | 544 | s.info.cap_arch = CS_ARCH_ARM; |
| 545 | /* TCG only generates code for arm mode. */ |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 546 | #elif defined(__MIPSEB__) |
| 547 | print_insn = print_insn_big_mips; |
| 548 | #elif defined(__MIPSEL__) |
| 549 | print_insn = print_insn_little_mips; |
bellard | 48024e4 | 2005-11-06 16:52:11 +0000 | [diff] [blame] | 550 | #elif defined(__m68k__) |
| 551 | print_insn = print_insn_m68k; |
ths | 8f860bb | 2007-07-31 23:44:21 +0000 | [diff] [blame] | 552 | #elif defined(__s390__) |
| 553 | print_insn = print_insn_s390; |
Richard Henderson | 429b31a | 2016-09-29 10:55:53 -0700 | [diff] [blame] | 554 | #elif defined(__hppa__) |
| 555 | print_insn = print_insn_hppa; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 556 | #endif |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 557 | |
| 558 | if (s.info.cap_arch >= 0 && cap_disas_host(&s.info, code, size)) { |
| 559 | return; |
| 560 | } |
| 561 | |
Richard Henderson | c46ffd5 | 2013-08-16 23:29:45 -0700 | [diff] [blame] | 562 | if (print_insn == NULL) { |
| 563 | print_insn = print_insn_od_host; |
| 564 | } |
Stefan Weil | b0b0f1c | 2012-04-12 15:44:35 +0200 | [diff] [blame] | 565 | for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) { |
| 566 | fprintf(out, "0x%08" PRIxPTR ": ", pc); |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 567 | count = print_insn(pc, &s.info); |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 568 | fprintf(out, "\n"); |
| 569 | if (count < 0) |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /* Look up symbol for debugging purpose. Returns "" if unknown. */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 575 | const char *lookup_symbol(target_ulong orig_addr) |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 576 | { |
pbrook | 49918a7 | 2008-10-22 15:11:31 +0000 | [diff] [blame] | 577 | const char *symbol = ""; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 578 | struct syminfo *s; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 579 | |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 580 | for (s = syminfos; s; s = s->next) { |
pbrook | 49918a7 | 2008-10-22 15:11:31 +0000 | [diff] [blame] | 581 | symbol = s->lookup_symbol(s, orig_addr); |
| 582 | if (symbol[0] != '\0') { |
| 583 | break; |
| 584 | } |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 585 | } |
pbrook | 49918a7 | 2008-10-22 15:11:31 +0000 | [diff] [blame] | 586 | |
| 587 | return symbol; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 588 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 589 | |
| 590 | #if !defined(CONFIG_USER_ONLY) |
| 591 | |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 592 | #include "monitor/monitor.h" |
bellard | 3d2cfdf | 2004-08-01 21:49:07 +0000 | [diff] [blame] | 593 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 594 | static int |
Richard Henderson | b8d8720 | 2017-09-19 09:40:40 -0500 | [diff] [blame] | 595 | physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length, |
blueswir1 | a5f1b96 | 2008-08-17 20:21:51 +0000 | [diff] [blame] | 596 | struct disassemble_info *info) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 597 | { |
Peter Maydell | f2c6abc | 2018-12-14 13:30:49 +0000 | [diff] [blame] | 598 | CPUDebug *s = container_of(info, CPUDebug, info); |
| 599 | |
| 600 | address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED, |
| 601 | myaddr, length); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 602 | return 0; |
| 603 | } |
| 604 | |
Richard Henderson | 1d48474 | 2017-09-14 08:38:35 -0700 | [diff] [blame] | 605 | /* Disassembler for the monitor. */ |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 606 | void monitor_disas(Monitor *mon, CPUState *cpu, |
Richard Henderson | 1d48474 | 2017-09-14 08:38:35 -0700 | [diff] [blame] | 607 | target_ulong pc, int nb_insn, int is_physical) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 608 | { |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 609 | CPUClass *cc = CPU_GET_CLASS(cpu); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 610 | int count, i; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 611 | CPUDebug s; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 612 | |
Markus Armbruster | 30cc983 | 2019-04-17 21:18:03 +0200 | [diff] [blame] | 613 | INIT_DISASSEMBLE_INFO(s.info, NULL, qemu_fprintf); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 614 | |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 615 | s.cpu = cpu; |
Richard Henderson | b8d8720 | 2017-09-19 09:40:40 -0500 | [diff] [blame] | 616 | s.info.read_memory_func |
| 617 | = (is_physical ? physical_read_memory : target_read_memory); |
Peter Crosthwaite | 9504c54 | 2015-07-05 13:50:32 -0700 | [diff] [blame] | 618 | s.info.print_address_func = generic_print_address; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 619 | s.info.buffer_vma = pc; |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 620 | s.info.cap_arch = -1; |
| 621 | s.info.cap_mode = 0; |
Richard Henderson | 15fa1a0 | 2017-11-07 13:19:18 +0100 | [diff] [blame] | 622 | s.info.cap_insn_unit = 4; |
| 623 | s.info.cap_insn_split = 4; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 624 | |
| 625 | #ifdef TARGET_WORDS_BIGENDIAN |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 626 | s.info.endian = BFD_ENDIAN_BIG; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 627 | #else |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 628 | s.info.endian = BFD_ENDIAN_LITTLE; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 629 | #endif |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 630 | |
| 631 | if (cc->disas_set_info) { |
| 632 | cc->disas_set_info(cpu, &s.info); |
| 633 | } |
| 634 | |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 635 | if (s.info.cap_arch >= 0 && cap_disas_monitor(&s.info, pc, nb_insn)) { |
| 636 | return; |
| 637 | } |
| 638 | |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 639 | if (!s.info.print_insn) { |
| 640 | monitor_printf(mon, "0x" TARGET_FMT_lx |
| 641 | ": Asm output not supported on this arch\n", pc); |
| 642 | return; |
| 643 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 644 | |
| 645 | for(i = 0; i < nb_insn; i++) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 646 | monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc); |
Peter Crosthwaite | 2de295c | 2015-06-23 20:57:32 -0700 | [diff] [blame] | 647 | count = s.info.print_insn(pc, &s.info); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 648 | monitor_printf(mon, "\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 649 | if (count < 0) |
| 650 | break; |
| 651 | pc += count; |
| 652 | } |
| 653 | } |
| 654 | #endif |