]> git.proxmox.com Git - qemu.git/blame - disas.c
qtest/ide-test: Add simple DMA read/write test case
[qemu.git] / disas.c
CommitLineData
b9adb4a6 1/* General "disassemble this chunk" code. Used for debugging. */
5bbe9299 2#include "config.h"
76cad711 3#include "disas/bfd.h"
b9adb4a6 4#include "elf.h"
aa0aa4fa 5#include <errno.h>
b9adb4a6 6
c6105c0a 7#include "cpu.h"
76cad711 8#include "disas/disas.h"
c6105c0a 9
f4359b9f
BS
10typedef struct CPUDebug {
11 struct disassemble_info info;
12 CPUArchState *env;
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
42 cpu_memory_rw_debug(s->env, 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 target virtual address. */
75static void
76generic_print_target_address(bfd_vma addr, struct disassemble_info *info)
77{
78 uint64_t mask = ~0ULL >> (64 - TARGET_VIRT_ADDR_SPACE_BITS);
79 generic_print_address(addr & mask, info);
80}
81
82/* Print address in hex, truncated to the width of a host virtual address. */
83static void
84generic_print_host_address(bfd_vma addr, struct disassemble_info *info)
85{
86 uint64_t mask = ~0ULL >> (64 - (sizeof(void *) * 8));
87 generic_print_address(addr & mask, info);
88}
89
aa0aa4fa
FB
90/* Just return the given address. */
91
92int
3a742b76 93generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
aa0aa4fa
FB
94{
95 return 1;
96}
97
903ec55c
AJ
98bfd_vma bfd_getl64 (const bfd_byte *addr)
99{
100 unsigned long long v;
101
102 v = (unsigned long long) addr[0];
103 v |= (unsigned long long) addr[1] << 8;
104 v |= (unsigned long long) addr[2] << 16;
105 v |= (unsigned long long) addr[3] << 24;
106 v |= (unsigned long long) addr[4] << 32;
107 v |= (unsigned long long) addr[5] << 40;
108 v |= (unsigned long long) addr[6] << 48;
109 v |= (unsigned long long) addr[7] << 56;
110 return (bfd_vma) v;
111}
112
aa0aa4fa
FB
113bfd_vma bfd_getl32 (const bfd_byte *addr)
114{
115 unsigned long v;
116
117 v = (unsigned long) addr[0];
118 v |= (unsigned long) addr[1] << 8;
119 v |= (unsigned long) addr[2] << 16;
120 v |= (unsigned long) addr[3] << 24;
121 return (bfd_vma) v;
122}
123
124bfd_vma bfd_getb32 (const bfd_byte *addr)
125{
126 unsigned long v;
127
128 v = (unsigned long) addr[0] << 24;
129 v |= (unsigned long) addr[1] << 16;
130 v |= (unsigned long) addr[2] << 8;
131 v |= (unsigned long) addr[3];
132 return (bfd_vma) v;
133}
134
6af0bf9c
FB
135bfd_vma bfd_getl16 (const bfd_byte *addr)
136{
137 unsigned long v;
138
139 v = (unsigned long) addr[0];
140 v |= (unsigned long) addr[1] << 8;
141 return (bfd_vma) v;
142}
143
144bfd_vma bfd_getb16 (const bfd_byte *addr)
145{
146 unsigned long v;
147
148 v = (unsigned long) addr[0] << 24;
149 v |= (unsigned long) addr[1] << 16;
150 return (bfd_vma) v;
151}
152
c2d551ff
FB
153#ifdef TARGET_ARM
154static int
155print_insn_thumb1(bfd_vma pc, disassemble_info *info)
156{
157 return print_insn_arm(pc | 1, info);
158}
159#endif
160
e91c8a77 161/* Disassemble this for me please... (debugging). 'flags' has the following
c2d551ff 162 values:
e99722f6 163 i386 - 1 means 16 bit code, 2 means 64 bit code
d8fd2954 164 arm - bit 0 = thumb, bit 1 = reverse endian
6a00d601 165 ppc - nonzero means little endian
c2d551ff
FB
166 other targets - unused
167 */
f4359b9f
BS
168void target_disas(FILE *out, CPUArchState *env, target_ulong code,
169 target_ulong size, int flags)
b9adb4a6 170{
c27004ec 171 target_ulong pc;
b9adb4a6 172 int count;
f4359b9f 173 CPUDebug s;
b9adb4a6
FB
174 int (*print_insn)(bfd_vma pc, disassemble_info *info);
175
f4359b9f 176 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
b9adb4a6 177
f4359b9f
BS
178 s.env = env;
179 s.info.read_memory_func = target_read_memory;
180 s.info.buffer_vma = code;
181 s.info.buffer_length = size;
182 s.info.print_address_func = generic_print_target_address;
c27004ec
FB
183
184#ifdef TARGET_WORDS_BIGENDIAN
f4359b9f 185 s.info.endian = BFD_ENDIAN_BIG;
c27004ec 186#else
f4359b9f 187 s.info.endian = BFD_ENDIAN_LITTLE;
c27004ec
FB
188#endif
189#if defined(TARGET_I386)
f4359b9f
BS
190 if (flags == 2) {
191 s.info.mach = bfd_mach_x86_64;
192 } else if (flags == 1) {
193 s.info.mach = bfd_mach_i386_i8086;
194 } else {
195 s.info.mach = bfd_mach_i386_i386;
196 }
c27004ec
FB
197 print_insn = print_insn_i386;
198#elif defined(TARGET_ARM)
d8fd2954
PB
199 if (flags & 1) {
200 print_insn = print_insn_thumb1;
201 } else {
202 print_insn = print_insn_arm;
203 }
204 if (flags & 2) {
205#ifdef TARGET_WORDS_BIGENDIAN
f4359b9f 206 s.info.endian = BFD_ENDIAN_LITTLE;
d8fd2954 207#else
f4359b9f 208 s.info.endian = BFD_ENDIAN_BIG;
d8fd2954
PB
209#endif
210 }
c27004ec
FB
211#elif defined(TARGET_SPARC)
212 print_insn = print_insn_sparc;
3475187d 213#ifdef TARGET_SPARC64
f4359b9f 214 s.info.mach = bfd_mach_sparc_v9b;
3b46e624 215#endif
c27004ec 216#elif defined(TARGET_PPC)
f4359b9f
BS
217 if (flags >> 16) {
218 s.info.endian = BFD_ENDIAN_LITTLE;
219 }
237c0af0
JM
220 if (flags & 0xFFFF) {
221 /* If we have a precise definitions of the instructions set, use it */
f4359b9f 222 s.info.mach = flags & 0xFFFF;
237c0af0 223 } else {
a2458627 224#ifdef TARGET_PPC64
f4359b9f 225 s.info.mach = bfd_mach_ppc64;
a2458627 226#else
f4359b9f 227 s.info.mach = bfd_mach_ppc;
a2458627 228#endif
237c0af0 229 }
88770fec 230 s.info.disassembler_options = (char *)"any";
c27004ec 231 print_insn = print_insn_ppc;
e6e5906b
PB
232#elif defined(TARGET_M68K)
233 print_insn = print_insn_m68k;
6af0bf9c 234#elif defined(TARGET_MIPS)
76b3030c 235#ifdef TARGET_WORDS_BIGENDIAN
6af0bf9c 236 print_insn = print_insn_big_mips;
76b3030c
FB
237#else
238 print_insn = print_insn_little_mips;
239#endif
fdf9b3e8 240#elif defined(TARGET_SH4)
f4359b9f 241 s.info.mach = bfd_mach_sh4;
fdf9b3e8 242 print_insn = print_insn_sh;
eddf68a6 243#elif defined(TARGET_ALPHA)
f4359b9f 244 s.info.mach = bfd_mach_alpha_ev6;
eddf68a6 245 print_insn = print_insn_alpha;
a25fd137 246#elif defined(TARGET_CRIS)
b09cd072 247 if (flags != 32) {
f4359b9f 248 s.info.mach = bfd_mach_cris_v0_v10;
b09cd072
EI
249 print_insn = print_insn_crisv10;
250 } else {
f4359b9f 251 s.info.mach = bfd_mach_cris_v32;
b09cd072
EI
252 print_insn = print_insn_crisv32;
253 }
db500609 254#elif defined(TARGET_S390X)
f4359b9f 255 s.info.mach = bfd_mach_s390_64;
db500609 256 print_insn = print_insn_s390;
e90e390c 257#elif defined(TARGET_MICROBLAZE)
f4359b9f 258 s.info.mach = bfd_arch_microblaze;
e90e390c 259 print_insn = print_insn_microblaze;
bd86a88e
AG
260#elif defined(TARGET_MOXIE)
261 s.info.mach = bfd_arch_moxie;
262 print_insn = print_insn_moxie;
79368f49 263#elif defined(TARGET_LM32)
f4359b9f 264 s.info.mach = bfd_mach_lm32;
79368f49 265 print_insn = print_insn_lm32;
c27004ec 266#else
b8076a74
FB
267 fprintf(out, "0x" TARGET_FMT_lx
268 ": Asm output not supported on this arch\n", code);
c27004ec 269 return;
c6105c0a
FB
270#endif
271
7e000c2e 272 for (pc = code; size > 0; pc += count, size -= count) {
fa15e030 273 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
f4359b9f 274 count = print_insn(pc, &s.info);
c27004ec
FB
275#if 0
276 {
277 int i;
278 uint8_t b;
279 fprintf(out, " {");
280 for(i = 0; i < count; i++) {
f4359b9f 281 target_read_memory(pc + i, &b, 1, &s.info);
c27004ec
FB
282 fprintf(out, " %02x", b);
283 }
284 fprintf(out, " }");
285 }
286#endif
287 fprintf(out, "\n");
288 if (count < 0)
289 break;
754d00ae 290 if (size < count) {
291 fprintf(out,
292 "Disassembler disagrees with translator over instruction "
293 "decoding\n"
294 "Please report this to qemu-devel@nongnu.org\n");
295 break;
296 }
c27004ec
FB
297 }
298}
299
300/* Disassemble this for me please... (debugging). */
301void disas(FILE *out, void *code, unsigned long size)
302{
b0b0f1c9 303 uintptr_t pc;
c27004ec 304 int count;
f4359b9f 305 CPUDebug s;
c27004ec
FB
306 int (*print_insn)(bfd_vma pc, disassemble_info *info);
307
f4359b9f
BS
308 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
309 s.info.print_address_func = generic_print_host_address;
c27004ec 310
f4359b9f
BS
311 s.info.buffer = code;
312 s.info.buffer_vma = (uintptr_t)code;
313 s.info.buffer_length = size;
b9adb4a6 314
e2542fe2 315#ifdef HOST_WORDS_BIGENDIAN
f4359b9f 316 s.info.endian = BFD_ENDIAN_BIG;
b9adb4a6 317#else
f4359b9f 318 s.info.endian = BFD_ENDIAN_LITTLE;
b9adb4a6 319#endif
5826e519
SW
320#if defined(CONFIG_TCG_INTERPRETER)
321 print_insn = print_insn_tci;
322#elif defined(__i386__)
f4359b9f 323 s.info.mach = bfd_mach_i386_i386;
c27004ec 324 print_insn = print_insn_i386;
bc51c5c9 325#elif defined(__x86_64__)
f4359b9f 326 s.info.mach = bfd_mach_x86_64;
c27004ec 327 print_insn = print_insn_i386;
e58ffeb3 328#elif defined(_ARCH_PPC)
66d4f6a3 329 s.info.disassembler_options = (char *)"any";
c27004ec 330 print_insn = print_insn_ppc;
a993ba85 331#elif defined(__alpha__)
c27004ec 332 print_insn = print_insn_alpha;
aa0aa4fa 333#elif defined(__sparc__)
c27004ec 334 print_insn = print_insn_sparc;
f4359b9f 335 s.info.mach = bfd_mach_sparc_v9b;
5fafdf24 336#elif defined(__arm__)
c27004ec 337 print_insn = print_insn_arm;
6af0bf9c
FB
338#elif defined(__MIPSEB__)
339 print_insn = print_insn_big_mips;
340#elif defined(__MIPSEL__)
341 print_insn = print_insn_little_mips;
48024e4a
FB
342#elif defined(__m68k__)
343 print_insn = print_insn_m68k;
8f860bb8
TS
344#elif defined(__s390__)
345 print_insn = print_insn_s390;
f54b3f92
AJ
346#elif defined(__hppa__)
347 print_insn = print_insn_hppa;
903ec55c
AJ
348#elif defined(__ia64__)
349 print_insn = print_insn_ia64;
b9adb4a6 350#else
b8076a74
FB
351 fprintf(out, "0x%lx: Asm output not supported on this arch\n",
352 (long) code);
c27004ec 353 return;
b9adb4a6 354#endif
b0b0f1c9
SW
355 for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
356 fprintf(out, "0x%08" PRIxPTR ": ", pc);
f4359b9f 357 count = print_insn(pc, &s.info);
b9adb4a6
FB
358 fprintf(out, "\n");
359 if (count < 0)
360 break;
361 }
362}
363
364/* Look up symbol for debugging purpose. Returns "" if unknown. */
c27004ec 365const char *lookup_symbol(target_ulong orig_addr)
b9adb4a6 366{
49918a75 367 const char *symbol = "";
e80cfcfc 368 struct syminfo *s;
3b46e624 369
e80cfcfc 370 for (s = syminfos; s; s = s->next) {
49918a75
PB
371 symbol = s->lookup_symbol(s, orig_addr);
372 if (symbol[0] != '\0') {
373 break;
374 }
b9adb4a6 375 }
49918a75
PB
376
377 return symbol;
b9adb4a6 378}
9307c4c1
FB
379
380#if !defined(CONFIG_USER_ONLY)
381
83c9089e 382#include "monitor/monitor.h"
3d2cfdf1 383
9307c4c1
FB
384static int monitor_disas_is_physical;
385
386static int
a5f1b965
BS
387monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
388 struct disassemble_info *info)
9307c4c1 389{
f4359b9f
BS
390 CPUDebug *s = container_of(info, CPUDebug, info);
391
9307c4c1 392 if (monitor_disas_is_physical) {
54f7b4a3 393 cpu_physical_memory_read(memaddr, myaddr, length);
9307c4c1 394 } else {
f4359b9f 395 cpu_memory_rw_debug(s->env, memaddr,myaddr, length, 0);
9307c4c1
FB
396 }
397 return 0;
398}
399
8b7968f7
SW
400static int GCC_FMT_ATTR(2, 3)
401monitor_fprintf(FILE *stream, const char *fmt, ...)
3d2cfdf1
FB
402{
403 va_list ap;
404 va_start(ap, fmt);
376253ec 405 monitor_vprintf((Monitor *)stream, fmt, ap);
3d2cfdf1
FB
406 va_end(ap);
407 return 0;
408}
409
9349b4f9 410void monitor_disas(Monitor *mon, CPUArchState *env,
6a00d601 411 target_ulong pc, int nb_insn, int is_physical, int flags)
9307c4c1 412{
9307c4c1 413 int count, i;
f4359b9f 414 CPUDebug s;
9307c4c1
FB
415 int (*print_insn)(bfd_vma pc, disassemble_info *info);
416
f4359b9f 417 INIT_DISASSEMBLE_INFO(s.info, (FILE *)mon, monitor_fprintf);
9307c4c1 418
f4359b9f 419 s.env = env;
9307c4c1 420 monitor_disas_is_physical = is_physical;
f4359b9f
BS
421 s.info.read_memory_func = monitor_read_memory;
422 s.info.print_address_func = generic_print_target_address;
9307c4c1 423
f4359b9f 424 s.info.buffer_vma = pc;
9307c4c1
FB
425
426#ifdef TARGET_WORDS_BIGENDIAN
f4359b9f 427 s.info.endian = BFD_ENDIAN_BIG;
9307c4c1 428#else
f4359b9f 429 s.info.endian = BFD_ENDIAN_LITTLE;
9307c4c1
FB
430#endif
431#if defined(TARGET_I386)
f4359b9f
BS
432 if (flags == 2) {
433 s.info.mach = bfd_mach_x86_64;
434 } else if (flags == 1) {
435 s.info.mach = bfd_mach_i386_i8086;
436 } else {
437 s.info.mach = bfd_mach_i386_i386;
438 }
9307c4c1
FB
439 print_insn = print_insn_i386;
440#elif defined(TARGET_ARM)
441 print_insn = print_insn_arm;
cbd669da
TS
442#elif defined(TARGET_ALPHA)
443 print_insn = print_insn_alpha;
9307c4c1
FB
444#elif defined(TARGET_SPARC)
445 print_insn = print_insn_sparc;
682c4f15 446#ifdef TARGET_SPARC64
f4359b9f 447 s.info.mach = bfd_mach_sparc_v9b;
682c4f15 448#endif
9307c4c1 449#elif defined(TARGET_PPC)
a2458627 450#ifdef TARGET_PPC64
f4359b9f 451 s.info.mach = bfd_mach_ppc64;
a2458627 452#else
f4359b9f 453 s.info.mach = bfd_mach_ppc;
a2458627 454#endif
9307c4c1 455 print_insn = print_insn_ppc;
e6e5906b
PB
456#elif defined(TARGET_M68K)
457 print_insn = print_insn_m68k;
6af0bf9c 458#elif defined(TARGET_MIPS)
76b3030c 459#ifdef TARGET_WORDS_BIGENDIAN
6af0bf9c 460 print_insn = print_insn_big_mips;
76b3030c
FB
461#else
462 print_insn = print_insn_little_mips;
463#endif
b4e1f077 464#elif defined(TARGET_SH4)
f4359b9f 465 s.info.mach = bfd_mach_sh4;
b4e1f077 466 print_insn = print_insn_sh;
db500609 467#elif defined(TARGET_S390X)
f4359b9f 468 s.info.mach = bfd_mach_s390_64;
db500609 469 print_insn = print_insn_s390;
bd86a88e
AG
470#elif defined(TARGET_MOXIE)
471 s.info.mach = bfd_arch_moxie;
472 print_insn = print_insn_moxie;
79368f49 473#elif defined(TARGET_LM32)
f4359b9f 474 s.info.mach = bfd_mach_lm32;
79368f49 475 print_insn = print_insn_lm32;
9307c4c1 476#else
376253ec
AL
477 monitor_printf(mon, "0x" TARGET_FMT_lx
478 ": Asm output not supported on this arch\n", pc);
9307c4c1
FB
479 return;
480#endif
481
482 for(i = 0; i < nb_insn; i++) {
376253ec 483 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
f4359b9f 484 count = print_insn(pc, &s.info);
376253ec 485 monitor_printf(mon, "\n");
9307c4c1
FB
486 if (count < 0)
487 break;
488 pc += count;
489 }
490}
491#endif