disas: QOMify sh4 specific disas setup
[qemu/ar7.git] / disas.c
blob2676e2f22a11382c82aaa59dde9a6575e352d1b4
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>
8 #include "cpu.h"
9 #include "disas/disas.h"
11 typedef struct CPUDebug {
12 struct disassemble_info info;
13 CPUState *cpu;
14 } CPUDebug;
16 /* Filled in by elfload.c. Simplistic, but will do for now. */
17 struct syminfo *syminfos = NULL;
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)
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;
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)
41 CPUDebug *s = container_of(info, CPUDebug, info);
43 cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
44 return 0;
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)
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);
62 /* This could be in a separate file, to save minuscule amounts of space
63 in statically linked executables. */
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). */
69 void
70 generic_print_address (bfd_vma addr, struct disassemble_info *info)
72 (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr);
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)
79 uint64_t mask = ~0ULL >> (64 - (sizeof(void *) * 8));
80 generic_print_address(addr & mask, info);
83 /* Just return the given address. */
85 int
86 generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
88 return 1;
91 bfd_vma bfd_getl64 (const bfd_byte *addr)
93 unsigned long long v;
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;
106 bfd_vma bfd_getl32 (const bfd_byte *addr)
108 unsigned long v;
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;
117 bfd_vma bfd_getb32 (const bfd_byte *addr)
119 unsigned long v;
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;
128 bfd_vma bfd_getl16 (const bfd_byte *addr)
130 unsigned long v;
132 v = (unsigned long) addr[0];
133 v |= (unsigned long) addr[1] << 8;
134 return (bfd_vma) v;
137 bfd_vma bfd_getb16 (const bfd_byte *addr)
139 unsigned long v;
141 v = (unsigned long) addr[0] << 24;
142 v |= (unsigned long) addr[1] << 16;
143 return (bfd_vma) v;
146 static int print_insn_objdump(bfd_vma pc, disassemble_info *info,
147 const char *prefix)
149 int i, n = info->buffer_length;
150 uint8_t *buf = g_malloc(n);
152 info->read_memory_func(pc, buf, n, info);
154 for (i = 0; i < n; ++i) {
155 if (i % 32 == 0) {
156 info->fprintf_func(info->stream, "\n%s: ", prefix);
158 info->fprintf_func(info->stream, "%02x", buf[i]);
161 g_free(buf);
162 return n;
165 static int print_insn_od_host(bfd_vma pc, disassemble_info *info)
167 return print_insn_objdump(pc, info, "OBJD-H");
170 static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
172 return print_insn_objdump(pc, info, "OBJD-T");
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
182 void target_disas(FILE *out, CPUState *cpu, target_ulong code,
183 target_ulong size, int flags)
185 CPUClass *cc = CPU_GET_CLASS(cpu);
186 target_ulong pc;
187 int count;
188 CPUDebug s;
190 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
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;
198 #ifdef TARGET_WORDS_BIGENDIAN
199 s.info.endian = BFD_ENDIAN_BIG;
200 #else
201 s.info.endian = BFD_ENDIAN_LITTLE;
202 #endif
204 if (cc->disas_set_info) {
205 cc->disas_set_info(cpu, &s.info);
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;
216 s.info.print_insn = print_insn_i386;
217 #elif defined(TARGET_PPC)
218 if ((flags >> 16) & 1) {
219 s.info.endian = BFD_ENDIAN_LITTLE;
221 if (flags & 0xFFFF) {
222 /* If we have a precise definition of the instruction set, use it. */
223 s.info.mach = flags & 0xFFFF;
224 } else {
225 #ifdef TARGET_PPC64
226 s.info.mach = bfd_mach_ppc64;
227 #else
228 s.info.mach = bfd_mach_ppc;
229 #endif
231 s.info.disassembler_options = (char *)"any";
232 s.info.print_insn = print_insn_ppc;
233 #elif defined(TARGET_MIPS)
234 #ifdef TARGET_WORDS_BIGENDIAN
235 s.info.print_insn = print_insn_big_mips;
236 #else
237 s.info.print_insn = print_insn_little_mips;
238 #endif
239 #elif defined(TARGET_ALPHA)
240 s.info.mach = bfd_mach_alpha_ev6;
241 s.info.print_insn = print_insn_alpha;
242 #endif
243 if (s.info.print_insn == NULL) {
244 s.info.print_insn = print_insn_od_target;
247 for (pc = code; size > 0; pc += count, size -= count) {
248 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
249 count = s.info.print_insn(pc, &s.info);
250 #if 0
252 int i;
253 uint8_t b;
254 fprintf(out, " {");
255 for(i = 0; i < count; i++) {
256 target_read_memory(pc + i, &b, 1, &s.info);
257 fprintf(out, " %02x", b);
259 fprintf(out, " }");
261 #endif
262 fprintf(out, "\n");
263 if (count < 0)
264 break;
265 if (size < count) {
266 fprintf(out,
267 "Disassembler disagrees with translator over instruction "
268 "decoding\n"
269 "Please report this to qemu-devel@nongnu.org\n");
270 break;
275 /* Disassemble this for me please... (debugging). */
276 void disas(FILE *out, void *code, unsigned long size)
278 uintptr_t pc;
279 int count;
280 CPUDebug s;
281 int (*print_insn)(bfd_vma pc, disassemble_info *info) = NULL;
283 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
284 s.info.print_address_func = generic_print_host_address;
286 s.info.buffer = code;
287 s.info.buffer_vma = (uintptr_t)code;
288 s.info.buffer_length = size;
290 #ifdef HOST_WORDS_BIGENDIAN
291 s.info.endian = BFD_ENDIAN_BIG;
292 #else
293 s.info.endian = BFD_ENDIAN_LITTLE;
294 #endif
295 #if defined(CONFIG_TCG_INTERPRETER)
296 print_insn = print_insn_tci;
297 #elif defined(__i386__)
298 s.info.mach = bfd_mach_i386_i386;
299 print_insn = print_insn_i386;
300 #elif defined(__x86_64__)
301 s.info.mach = bfd_mach_x86_64;
302 print_insn = print_insn_i386;
303 #elif defined(_ARCH_PPC)
304 s.info.disassembler_options = (char *)"any";
305 print_insn = print_insn_ppc;
306 #elif defined(__aarch64__) && defined(CONFIG_ARM_A64_DIS)
307 print_insn = print_insn_arm_a64;
308 #elif defined(__alpha__)
309 print_insn = print_insn_alpha;
310 #elif defined(__sparc__)
311 print_insn = print_insn_sparc;
312 s.info.mach = bfd_mach_sparc_v9b;
313 #elif defined(__arm__)
314 print_insn = print_insn_arm;
315 #elif defined(__MIPSEB__)
316 print_insn = print_insn_big_mips;
317 #elif defined(__MIPSEL__)
318 print_insn = print_insn_little_mips;
319 #elif defined(__m68k__)
320 print_insn = print_insn_m68k;
321 #elif defined(__s390__)
322 print_insn = print_insn_s390;
323 #elif defined(__hppa__)
324 print_insn = print_insn_hppa;
325 #elif defined(__ia64__)
326 print_insn = print_insn_ia64;
327 #endif
328 if (print_insn == NULL) {
329 print_insn = print_insn_od_host;
331 for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
332 fprintf(out, "0x%08" PRIxPTR ": ", pc);
333 count = print_insn(pc, &s.info);
334 fprintf(out, "\n");
335 if (count < 0)
336 break;
340 /* Look up symbol for debugging purpose. Returns "" if unknown. */
341 const char *lookup_symbol(target_ulong orig_addr)
343 const char *symbol = "";
344 struct syminfo *s;
346 for (s = syminfos; s; s = s->next) {
347 symbol = s->lookup_symbol(s, orig_addr);
348 if (symbol[0] != '\0') {
349 break;
353 return symbol;
356 #if !defined(CONFIG_USER_ONLY)
358 #include "monitor/monitor.h"
360 static int monitor_disas_is_physical;
362 static int
363 monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
364 struct disassemble_info *info)
366 CPUDebug *s = container_of(info, CPUDebug, info);
368 if (monitor_disas_is_physical) {
369 cpu_physical_memory_read(memaddr, myaddr, length);
370 } else {
371 cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
373 return 0;
376 /* Disassembler for the monitor.
377 See target_disas for a description of flags. */
378 void monitor_disas(Monitor *mon, CPUState *cpu,
379 target_ulong pc, int nb_insn, int is_physical, int flags)
381 CPUClass *cc = CPU_GET_CLASS(cpu);
382 int count, i;
383 CPUDebug s;
385 INIT_DISASSEMBLE_INFO(s.info, (FILE *)mon, monitor_fprintf);
387 s.cpu = cpu;
388 monitor_disas_is_physical = is_physical;
389 s.info.read_memory_func = monitor_read_memory;
390 s.info.print_address_func = generic_print_address;
392 s.info.buffer_vma = pc;
394 #ifdef TARGET_WORDS_BIGENDIAN
395 s.info.endian = BFD_ENDIAN_BIG;
396 #else
397 s.info.endian = BFD_ENDIAN_LITTLE;
398 #endif
400 if (cc->disas_set_info) {
401 cc->disas_set_info(cpu, &s.info);
404 #if defined(TARGET_I386)
405 if (flags == 2) {
406 s.info.mach = bfd_mach_x86_64;
407 } else if (flags == 1) {
408 s.info.mach = bfd_mach_i386_i8086;
409 } else {
410 s.info.mach = bfd_mach_i386_i386;
412 s.info.print_insn = print_insn_i386;
413 #elif defined(TARGET_ALPHA)
414 s.info.print_insn = print_insn_alpha;
415 #elif defined(TARGET_PPC)
416 if (flags & 0xFFFF) {
417 /* If we have a precise definition of the instruction set, use it. */
418 s.info.mach = flags & 0xFFFF;
419 } else {
420 #ifdef TARGET_PPC64
421 s.info.mach = bfd_mach_ppc64;
422 #else
423 s.info.mach = bfd_mach_ppc;
424 #endif
426 if ((flags >> 16) & 1) {
427 s.info.endian = BFD_ENDIAN_LITTLE;
429 s.info.print_insn = print_insn_ppc;
430 #elif defined(TARGET_MIPS)
431 #ifdef TARGET_WORDS_BIGENDIAN
432 s.info.print_insn = print_insn_big_mips;
433 #else
434 s.info.print_insn = print_insn_little_mips;
435 #endif
436 #endif
437 if (!s.info.print_insn) {
438 monitor_printf(mon, "0x" TARGET_FMT_lx
439 ": Asm output not supported on this arch\n", pc);
440 return;
443 for(i = 0; i < nb_insn; i++) {
444 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
445 count = s.info.print_insn(pc, &s.info);
446 monitor_printf(mon, "\n");
447 if (count < 0)
448 break;
449 pc += count;
452 #endif