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