]> git.proxmox.com Git - mirror_qemu.git/blame - disas.c
iotests: use virtio aliases for 067
[mirror_qemu.git] / disas.c
CommitLineData
b9adb4a6 1/* General "disassemble this chunk" code. Used for debugging. */
d38ea87a 2#include "qemu/osdep.h"
37b9de46 3#include "qemu-common.h"
76cad711 4#include "disas/bfd.h"
b9adb4a6
FB
5#include "elf.h"
6
c6105c0a 7#include "cpu.h"
76cad711 8#include "disas/disas.h"
c6105c0a 9
f4359b9f
BS
10typedef struct CPUDebug {
11 struct disassemble_info info;
d49190c4 12 CPUState *cpu;
f4359b9f
BS
13} CPUDebug;
14
b9adb4a6 15/* Filled in by elfload.c. Simplistic, but will do for now. */
e80cfcfc 16struct syminfo *syminfos = NULL;
b9adb4a6 17
aa0aa4fa
FB
18/* Get LENGTH bytes from info's buffer, at target address memaddr.
19 Transfer them to myaddr. */
20int
3a742b76
PB
21buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
22 struct disassemble_info *info)
aa0aa4fa 23{
c6105c0a
FB
24 if (memaddr < info->buffer_vma
25 || memaddr + length > info->buffer_vma + info->buffer_length)
26 /* Out of bounds. Use EIO because GDB uses it. */
27 return EIO;
28 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
29 return 0;
aa0aa4fa
FB
30}
31
c6105c0a
FB
32/* Get LENGTH bytes from info's buffer, at target address memaddr.
33 Transfer them to myaddr. */
34static int
c27004ec
FB
35target_read_memory (bfd_vma memaddr,
36 bfd_byte *myaddr,
37 int length,
38 struct disassemble_info *info)
c6105c0a 39{
f4359b9f
BS
40 CPUDebug *s = container_of(info, CPUDebug, info);
41
d49190c4 42 cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
c6105c0a
FB
43 return 0;
44}
c6105c0a 45
aa0aa4fa
FB
46/* Print an error message. We can assume that this is in response to
47 an error return from buffer_read_memory. */
48void
3a742b76 49perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info)
aa0aa4fa
FB
50{
51 if (status != EIO)
52 /* Can't happen. */
53 (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
54 else
55 /* Actually, address between memaddr and memaddr + len was
56 out of bounds. */
57 (*info->fprintf_func) (info->stream,
26a76461 58 "Address 0x%" PRIx64 " is out of bounds.\n", memaddr);
aa0aa4fa
FB
59}
60
a31f0531 61/* This could be in a separate file, to save minuscule amounts of space
aa0aa4fa
FB
62 in statically linked executables. */
63
64/* Just print the address is hex. This is included for completeness even
65 though both GDB and objdump provide their own (to print symbolic
66 addresses). */
67
68void
3a742b76 69generic_print_address (bfd_vma addr, struct disassemble_info *info)
aa0aa4fa 70{
26a76461 71 (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr);
aa0aa4fa
FB
72}
73
636bd289
PM
74/* Print address in hex, truncated to the width of a host virtual address. */
75static void
76generic_print_host_address(bfd_vma addr, struct disassemble_info *info)
77{
78 uint64_t mask = ~0ULL >> (64 - (sizeof(void *) * 8));
79 generic_print_address(addr & mask, info);
80}
81
aa0aa4fa
FB
82/* Just return the given address. */
83
84int
3a742b76 85generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
aa0aa4fa
FB
86{
87 return 1;
88}
89
903ec55c
AJ
90bfd_vma bfd_getl64 (const bfd_byte *addr)
91{
92 unsigned long long v;
93
94 v = (unsigned long long) addr[0];
95 v |= (unsigned long long) addr[1] << 8;
96 v |= (unsigned long long) addr[2] << 16;
97 v |= (unsigned long long) addr[3] << 24;
98 v |= (unsigned long long) addr[4] << 32;
99 v |= (unsigned long long) addr[5] << 40;
100 v |= (unsigned long long) addr[6] << 48;
101 v |= (unsigned long long) addr[7] << 56;
102 return (bfd_vma) v;
103}
104
aa0aa4fa
FB
105bfd_vma bfd_getl32 (const bfd_byte *addr)
106{
107 unsigned long v;
108
109 v = (unsigned long) addr[0];
110 v |= (unsigned long) addr[1] << 8;
111 v |= (unsigned long) addr[2] << 16;
112 v |= (unsigned long) addr[3] << 24;
113 return (bfd_vma) v;
114}
115
116bfd_vma bfd_getb32 (const bfd_byte *addr)
117{
118 unsigned long v;
119
120 v = (unsigned long) addr[0] << 24;
121 v |= (unsigned long) addr[1] << 16;
122 v |= (unsigned long) addr[2] << 8;
123 v |= (unsigned long) addr[3];
124 return (bfd_vma) v;
125}
126
6af0bf9c
FB
127bfd_vma bfd_getl16 (const bfd_byte *addr)
128{
129 unsigned long v;
130
131 v = (unsigned long) addr[0];
132 v |= (unsigned long) addr[1] << 8;
133 return (bfd_vma) v;
134}
135
136bfd_vma bfd_getb16 (const bfd_byte *addr)
137{
138 unsigned long v;
139
140 v = (unsigned long) addr[0] << 24;
141 v |= (unsigned long) addr[1] << 16;
142 return (bfd_vma) v;
143}
144
c46ffd57
RH
145static int print_insn_objdump(bfd_vma pc, disassemble_info *info,
146 const char *prefix)
147{
148 int i, n = info->buffer_length;
149 uint8_t *buf = g_malloc(n);
150
151 info->read_memory_func(pc, buf, n, info);
152
153 for (i = 0; i < n; ++i) {
154 if (i % 32 == 0) {
155 info->fprintf_func(info->stream, "\n%s: ", prefix);
156 }
157 info->fprintf_func(info->stream, "%02x", buf[i]);
158 }
159
160 g_free(buf);
161 return n;
162}
163
164static int print_insn_od_host(bfd_vma pc, disassemble_info *info)
165{
166 return print_insn_objdump(pc, info, "OBJD-H");
167}
168
169static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
170{
171 return print_insn_objdump(pc, info, "OBJD-T");
172}
173
e91c8a77 174/* Disassemble this for me please... (debugging). 'flags' has the following
c2d551ff 175 values:
e99722f6 176 i386 - 1 means 16 bit code, 2 means 64 bit code
e13951f8
TM
177 ppc - bits 0:15 specify (optionally) the machine instruction set;
178 bit 16 indicates little endian.
c2d551ff
FB
179 other targets - unused
180 */
d49190c4 181void target_disas(FILE *out, CPUState *cpu, target_ulong code,
f4359b9f 182 target_ulong size, int flags)
b9adb4a6 183{
37b9de46 184 CPUClass *cc = CPU_GET_CLASS(cpu);
c27004ec 185 target_ulong pc;
b9adb4a6 186 int count;
f4359b9f 187 CPUDebug s;
b9adb4a6 188
f4359b9f 189 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
b9adb4a6 190
d49190c4 191 s.cpu = cpu;
f4359b9f 192 s.info.read_memory_func = target_read_memory;
f7478a92 193 s.info.read_memory_inner_func = NULL;
f4359b9f
BS
194 s.info.buffer_vma = code;
195 s.info.buffer_length = size;
9504c544 196 s.info.print_address_func = generic_print_address;
c27004ec
FB
197
198#ifdef TARGET_WORDS_BIGENDIAN
f4359b9f 199 s.info.endian = BFD_ENDIAN_BIG;
c27004ec 200#else
f4359b9f 201 s.info.endian = BFD_ENDIAN_LITTLE;
c27004ec 202#endif
37b9de46
PC
203
204 if (cc->disas_set_info) {
205 cc->disas_set_info(cpu, &s.info);
206 }
207
c27004ec 208#if defined(TARGET_I386)
f4359b9f
BS
209 if (flags == 2) {
210 s.info.mach = bfd_mach_x86_64;
211 } else if (flags == 1) {
212 s.info.mach = bfd_mach_i386_i8086;
213 } else {
214 s.info.mach = bfd_mach_i386_i386;
215 }
2de295c5 216 s.info.print_insn = print_insn_i386;
c27004ec 217#elif defined(TARGET_PPC)
e13951f8 218 if ((flags >> 16) & 1) {
f4359b9f
BS
219 s.info.endian = BFD_ENDIAN_LITTLE;
220 }
237c0af0 221 if (flags & 0xFFFF) {
e13951f8 222 /* If we have a precise definition of the instruction set, use it. */
f4359b9f 223 s.info.mach = flags & 0xFFFF;
237c0af0 224 } else {
a2458627 225#ifdef TARGET_PPC64
f4359b9f 226 s.info.mach = bfd_mach_ppc64;
a2458627 227#else
f4359b9f 228 s.info.mach = bfd_mach_ppc;
a2458627 229#endif
237c0af0 230 }
88770fec 231 s.info.disassembler_options = (char *)"any";
2de295c5 232 s.info.print_insn = print_insn_ppc;
c6105c0a 233#endif
2de295c5
PC
234 if (s.info.print_insn == NULL) {
235 s.info.print_insn = print_insn_od_target;
c46ffd57 236 }
c6105c0a 237
7e000c2e 238 for (pc = code; size > 0; pc += count, size -= count) {
fa15e030 239 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
2de295c5 240 count = s.info.print_insn(pc, &s.info);
c27004ec
FB
241#if 0
242 {
243 int i;
244 uint8_t b;
245 fprintf(out, " {");
246 for(i = 0; i < count; i++) {
f4359b9f 247 target_read_memory(pc + i, &b, 1, &s.info);
c27004ec
FB
248 fprintf(out, " %02x", b);
249 }
250 fprintf(out, " }");
251 }
252#endif
253 fprintf(out, "\n");
254 if (count < 0)
255 break;
754d00ae 256 if (size < count) {
257 fprintf(out,
258 "Disassembler disagrees with translator over instruction "
259 "decoding\n"
260 "Please report this to qemu-devel@nongnu.org\n");
261 break;
262 }
c27004ec
FB
263 }
264}
265
266/* Disassemble this for me please... (debugging). */
267void disas(FILE *out, void *code, unsigned long size)
268{
b0b0f1c9 269 uintptr_t pc;
c27004ec 270 int count;
f4359b9f 271 CPUDebug s;
c46ffd57 272 int (*print_insn)(bfd_vma pc, disassemble_info *info) = NULL;
c27004ec 273
f4359b9f
BS
274 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
275 s.info.print_address_func = generic_print_host_address;
c27004ec 276
f4359b9f
BS
277 s.info.buffer = code;
278 s.info.buffer_vma = (uintptr_t)code;
279 s.info.buffer_length = size;
b9adb4a6 280
e2542fe2 281#ifdef HOST_WORDS_BIGENDIAN
f4359b9f 282 s.info.endian = BFD_ENDIAN_BIG;
b9adb4a6 283#else
f4359b9f 284 s.info.endian = BFD_ENDIAN_LITTLE;
b9adb4a6 285#endif
5826e519
SW
286#if defined(CONFIG_TCG_INTERPRETER)
287 print_insn = print_insn_tci;
288#elif defined(__i386__)
f4359b9f 289 s.info.mach = bfd_mach_i386_i386;
c27004ec 290 print_insn = print_insn_i386;
bc51c5c9 291#elif defined(__x86_64__)
f4359b9f 292 s.info.mach = bfd_mach_x86_64;
c27004ec 293 print_insn = print_insn_i386;
e58ffeb3 294#elif defined(_ARCH_PPC)
66d4f6a3 295 s.info.disassembler_options = (char *)"any";
c27004ec 296 print_insn = print_insn_ppc;
999b53ec
CF
297#elif defined(__aarch64__) && defined(CONFIG_ARM_A64_DIS)
298 print_insn = print_insn_arm_a64;
a993ba85 299#elif defined(__alpha__)
c27004ec 300 print_insn = print_insn_alpha;
aa0aa4fa 301#elif defined(__sparc__)
c27004ec 302 print_insn = print_insn_sparc;
f4359b9f 303 s.info.mach = bfd_mach_sparc_v9b;
5fafdf24 304#elif defined(__arm__)
c27004ec 305 print_insn = print_insn_arm;
6af0bf9c
FB
306#elif defined(__MIPSEB__)
307 print_insn = print_insn_big_mips;
308#elif defined(__MIPSEL__)
309 print_insn = print_insn_little_mips;
48024e4a
FB
310#elif defined(__m68k__)
311 print_insn = print_insn_m68k;
8f860bb8
TS
312#elif defined(__s390__)
313 print_insn = print_insn_s390;
429b31a2
RH
314#elif defined(__hppa__)
315 print_insn = print_insn_hppa;
b9adb4a6 316#endif
c46ffd57
RH
317 if (print_insn == NULL) {
318 print_insn = print_insn_od_host;
319 }
b0b0f1c9
SW
320 for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
321 fprintf(out, "0x%08" PRIxPTR ": ", pc);
f4359b9f 322 count = print_insn(pc, &s.info);
b9adb4a6
FB
323 fprintf(out, "\n");
324 if (count < 0)
325 break;
326 }
327}
328
329/* Look up symbol for debugging purpose. Returns "" if unknown. */
c27004ec 330const char *lookup_symbol(target_ulong orig_addr)
b9adb4a6 331{
49918a75 332 const char *symbol = "";
e80cfcfc 333 struct syminfo *s;
3b46e624 334
e80cfcfc 335 for (s = syminfos; s; s = s->next) {
49918a75
PB
336 symbol = s->lookup_symbol(s, orig_addr);
337 if (symbol[0] != '\0') {
338 break;
339 }
b9adb4a6 340 }
49918a75
PB
341
342 return symbol;
b9adb4a6 343}
9307c4c1
FB
344
345#if !defined(CONFIG_USER_ONLY)
346
83c9089e 347#include "monitor/monitor.h"
3d2cfdf1 348
9307c4c1
FB
349static int monitor_disas_is_physical;
350
351static int
a5f1b965
BS
352monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
353 struct disassemble_info *info)
9307c4c1 354{
f4359b9f
BS
355 CPUDebug *s = container_of(info, CPUDebug, info);
356
9307c4c1 357 if (monitor_disas_is_physical) {
54f7b4a3 358 cpu_physical_memory_read(memaddr, myaddr, length);
9307c4c1 359 } else {
d49190c4 360 cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
9307c4c1
FB
361 }
362 return 0;
363}
364
1c38f843
TM
365/* Disassembler for the monitor.
366 See target_disas for a description of flags. */
d49190c4 367void monitor_disas(Monitor *mon, CPUState *cpu,
6a00d601 368 target_ulong pc, int nb_insn, int is_physical, int flags)
9307c4c1 369{
37b9de46 370 CPUClass *cc = CPU_GET_CLASS(cpu);
9307c4c1 371 int count, i;
f4359b9f 372 CPUDebug s;
9307c4c1 373
f4359b9f 374 INIT_DISASSEMBLE_INFO(s.info, (FILE *)mon, monitor_fprintf);
9307c4c1 375
d49190c4 376 s.cpu = cpu;
9307c4c1 377 monitor_disas_is_physical = is_physical;
f4359b9f 378 s.info.read_memory_func = monitor_read_memory;
9504c544 379 s.info.print_address_func = generic_print_address;
9307c4c1 380
f4359b9f 381 s.info.buffer_vma = pc;
9307c4c1
FB
382
383#ifdef TARGET_WORDS_BIGENDIAN
f4359b9f 384 s.info.endian = BFD_ENDIAN_BIG;
9307c4c1 385#else
f4359b9f 386 s.info.endian = BFD_ENDIAN_LITTLE;
9307c4c1 387#endif
37b9de46
PC
388
389 if (cc->disas_set_info) {
390 cc->disas_set_info(cpu, &s.info);
391 }
392
9307c4c1 393#if defined(TARGET_I386)
f4359b9f
BS
394 if (flags == 2) {
395 s.info.mach = bfd_mach_x86_64;
396 } else if (flags == 1) {
397 s.info.mach = bfd_mach_i386_i8086;
398 } else {
399 s.info.mach = bfd_mach_i386_i386;
400 }
2de295c5 401 s.info.print_insn = print_insn_i386;
9307c4c1 402#elif defined(TARGET_PPC)
1c38f843
TM
403 if (flags & 0xFFFF) {
404 /* If we have a precise definition of the instruction set, use it. */
405 s.info.mach = flags & 0xFFFF;
406 } else {
a2458627 407#ifdef TARGET_PPC64
1c38f843 408 s.info.mach = bfd_mach_ppc64;
a2458627 409#else
1c38f843 410 s.info.mach = bfd_mach_ppc;
a2458627 411#endif
1c38f843
TM
412 }
413 if ((flags >> 16) & 1) {
414 s.info.endian = BFD_ENDIAN_LITTLE;
415 }
2de295c5 416 s.info.print_insn = print_insn_ppc;
9307c4c1 417#endif
37b9de46
PC
418 if (!s.info.print_insn) {
419 monitor_printf(mon, "0x" TARGET_FMT_lx
420 ": Asm output not supported on this arch\n", pc);
421 return;
422 }
9307c4c1
FB
423
424 for(i = 0; i < nb_insn; i++) {
376253ec 425 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
2de295c5 426 count = s.info.print_insn(pc, &s.info);
376253ec 427 monitor_printf(mon, "\n");
9307c4c1
FB
428 if (count < 0)
429 break;
430 pc += count;
431 }
432}
433#endif