]> git.ipfire.org Git - thirdparty/qemu.git/blob - disas.c
target/i386: Convert to disas_set_info hook
[thirdparty/qemu.git] / disas.c
1 /* General "disassemble this chunk" code. Used for debugging. */
2 #include "qemu/osdep.h"
3 #include "qemu-common.h"
4 #include "disas/bfd.h"
5 #include "elf.h"
6
7 #include "cpu.h"
8 #include "disas/disas.h"
9
10 typedef struct CPUDebug {
11 struct disassemble_info info;
12 CPUState *cpu;
13 } CPUDebug;
14
15 /* Filled in by elfload.c. Simplistic, but will do for now. */
16 struct syminfo *syminfos = NULL;
17
18 /* Get LENGTH bytes from info's buffer, at target address memaddr.
19 Transfer them to myaddr. */
20 int
21 buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
22 struct disassemble_info *info)
23 {
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;
30 }
31
32 /* Get LENGTH bytes from info's buffer, at target address memaddr.
33 Transfer them to myaddr. */
34 static int
35 target_read_memory (bfd_vma memaddr,
36 bfd_byte *myaddr,
37 int length,
38 struct disassemble_info *info)
39 {
40 CPUDebug *s = container_of(info, CPUDebug, info);
41
42 cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
43 return 0;
44 }
45
46 /* Print an error message. We can assume that this is in response to
47 an error return from buffer_read_memory. */
48 void
49 perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info)
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,
58 "Address 0x%" PRIx64 " is out of bounds.\n", memaddr);
59 }
60
61 /* This could be in a separate file, to save minuscule amounts of space
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
68 void
69 generic_print_address (bfd_vma addr, struct disassemble_info *info)
70 {
71 (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr);
72 }
73
74 /* Print address in hex, truncated to the width of a host virtual address. */
75 static void
76 generic_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
82 /* Just return the given address. */
83
84 int
85 generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
86 {
87 return 1;
88 }
89
90 bfd_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
105 bfd_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
116 bfd_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
127 bfd_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
136 bfd_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
145 static 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
164 static int print_insn_od_host(bfd_vma pc, disassemble_info *info)
165 {
166 return print_insn_objdump(pc, info, "OBJD-H");
167 }
168
169 static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
170 {
171 return print_insn_objdump(pc, info, "OBJD-T");
172 }
173
174 /* Disassemble this for me please... (debugging). 'flags' has the following
175 values:
176 i386 - 1 means 16 bit code, 2 means 64 bit code
177 ppc - bits 0:15 specify (optionally) the machine instruction set;
178 bit 16 indicates little endian.
179 other targets - unused
180 */
181 void target_disas(FILE *out, CPUState *cpu, target_ulong code,
182 target_ulong size, int flags)
183 {
184 CPUClass *cc = CPU_GET_CLASS(cpu);
185 target_ulong pc;
186 int count;
187 CPUDebug s;
188
189 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
190
191 s.cpu = cpu;
192 s.info.read_memory_func = target_read_memory;
193 s.info.buffer_vma = code;
194 s.info.buffer_length = size;
195 s.info.print_address_func = generic_print_address;
196
197 #ifdef TARGET_WORDS_BIGENDIAN
198 s.info.endian = BFD_ENDIAN_BIG;
199 #else
200 s.info.endian = BFD_ENDIAN_LITTLE;
201 #endif
202
203 if (cc->disas_set_info) {
204 cc->disas_set_info(cpu, &s.info);
205 }
206
207 #if defined(TARGET_PPC)
208 if ((flags >> 16) & 1) {
209 s.info.endian = BFD_ENDIAN_LITTLE;
210 }
211 if (flags & 0xFFFF) {
212 /* If we have a precise definition of the instruction set, use it. */
213 s.info.mach = flags & 0xFFFF;
214 } else {
215 #ifdef TARGET_PPC64
216 s.info.mach = bfd_mach_ppc64;
217 #else
218 s.info.mach = bfd_mach_ppc;
219 #endif
220 }
221 s.info.disassembler_options = (char *)"any";
222 s.info.print_insn = print_insn_ppc;
223 #endif
224 if (s.info.print_insn == NULL) {
225 s.info.print_insn = print_insn_od_target;
226 }
227
228 for (pc = code; size > 0; pc += count, size -= count) {
229 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
230 count = s.info.print_insn(pc, &s.info);
231 #if 0
232 {
233 int i;
234 uint8_t b;
235 fprintf(out, " {");
236 for(i = 0; i < count; i++) {
237 target_read_memory(pc + i, &b, 1, &s.info);
238 fprintf(out, " %02x", b);
239 }
240 fprintf(out, " }");
241 }
242 #endif
243 fprintf(out, "\n");
244 if (count < 0)
245 break;
246 if (size < count) {
247 fprintf(out,
248 "Disassembler disagrees with translator over instruction "
249 "decoding\n"
250 "Please report this to qemu-devel@nongnu.org\n");
251 break;
252 }
253 }
254 }
255
256 /* Disassemble this for me please... (debugging). */
257 void disas(FILE *out, void *code, unsigned long size)
258 {
259 uintptr_t pc;
260 int count;
261 CPUDebug s;
262 int (*print_insn)(bfd_vma pc, disassemble_info *info) = NULL;
263
264 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
265 s.info.print_address_func = generic_print_host_address;
266
267 s.info.buffer = code;
268 s.info.buffer_vma = (uintptr_t)code;
269 s.info.buffer_length = size;
270
271 #ifdef HOST_WORDS_BIGENDIAN
272 s.info.endian = BFD_ENDIAN_BIG;
273 #else
274 s.info.endian = BFD_ENDIAN_LITTLE;
275 #endif
276 #if defined(CONFIG_TCG_INTERPRETER)
277 print_insn = print_insn_tci;
278 #elif defined(__i386__)
279 s.info.mach = bfd_mach_i386_i386;
280 print_insn = print_insn_i386;
281 #elif defined(__x86_64__)
282 s.info.mach = bfd_mach_x86_64;
283 print_insn = print_insn_i386;
284 #elif defined(_ARCH_PPC)
285 s.info.disassembler_options = (char *)"any";
286 print_insn = print_insn_ppc;
287 #elif defined(__aarch64__) && defined(CONFIG_ARM_A64_DIS)
288 print_insn = print_insn_arm_a64;
289 #elif defined(__alpha__)
290 print_insn = print_insn_alpha;
291 #elif defined(__sparc__)
292 print_insn = print_insn_sparc;
293 s.info.mach = bfd_mach_sparc_v9b;
294 #elif defined(__arm__)
295 print_insn = print_insn_arm;
296 #elif defined(__MIPSEB__)
297 print_insn = print_insn_big_mips;
298 #elif defined(__MIPSEL__)
299 print_insn = print_insn_little_mips;
300 #elif defined(__m68k__)
301 print_insn = print_insn_m68k;
302 #elif defined(__s390__)
303 print_insn = print_insn_s390;
304 #elif defined(__hppa__)
305 print_insn = print_insn_hppa;
306 #endif
307 if (print_insn == NULL) {
308 print_insn = print_insn_od_host;
309 }
310 for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
311 fprintf(out, "0x%08" PRIxPTR ": ", pc);
312 count = print_insn(pc, &s.info);
313 fprintf(out, "\n");
314 if (count < 0)
315 break;
316 }
317 }
318
319 /* Look up symbol for debugging purpose. Returns "" if unknown. */
320 const char *lookup_symbol(target_ulong orig_addr)
321 {
322 const char *symbol = "";
323 struct syminfo *s;
324
325 for (s = syminfos; s; s = s->next) {
326 symbol = s->lookup_symbol(s, orig_addr);
327 if (symbol[0] != '\0') {
328 break;
329 }
330 }
331
332 return symbol;
333 }
334
335 #if !defined(CONFIG_USER_ONLY)
336
337 #include "monitor/monitor.h"
338
339 static int monitor_disas_is_physical;
340
341 static int
342 monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
343 struct disassemble_info *info)
344 {
345 CPUDebug *s = container_of(info, CPUDebug, info);
346
347 if (monitor_disas_is_physical) {
348 cpu_physical_memory_read(memaddr, myaddr, length);
349 } else {
350 cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
351 }
352 return 0;
353 }
354
355 /* Disassembler for the monitor.
356 See target_disas for a description of flags. */
357 void monitor_disas(Monitor *mon, CPUState *cpu,
358 target_ulong pc, int nb_insn, int is_physical, int flags)
359 {
360 CPUClass *cc = CPU_GET_CLASS(cpu);
361 int count, i;
362 CPUDebug s;
363
364 INIT_DISASSEMBLE_INFO(s.info, (FILE *)mon, monitor_fprintf);
365
366 s.cpu = cpu;
367 monitor_disas_is_physical = is_physical;
368 s.info.read_memory_func = monitor_read_memory;
369 s.info.print_address_func = generic_print_address;
370
371 s.info.buffer_vma = pc;
372
373 #ifdef TARGET_WORDS_BIGENDIAN
374 s.info.endian = BFD_ENDIAN_BIG;
375 #else
376 s.info.endian = BFD_ENDIAN_LITTLE;
377 #endif
378
379 if (cc->disas_set_info) {
380 cc->disas_set_info(cpu, &s.info);
381 }
382
383 #if defined(TARGET_PPC)
384 if (flags & 0xFFFF) {
385 /* If we have a precise definition of the instruction set, use it. */
386 s.info.mach = flags & 0xFFFF;
387 } else {
388 #ifdef TARGET_PPC64
389 s.info.mach = bfd_mach_ppc64;
390 #else
391 s.info.mach = bfd_mach_ppc;
392 #endif
393 }
394 if ((flags >> 16) & 1) {
395 s.info.endian = BFD_ENDIAN_LITTLE;
396 }
397 s.info.print_insn = print_insn_ppc;
398 #endif
399 if (!s.info.print_insn) {
400 monitor_printf(mon, "0x" TARGET_FMT_lx
401 ": Asm output not supported on this arch\n", pc);
402 return;
403 }
404
405 for(i = 0; i < nb_insn; i++) {
406 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
407 count = s.info.print_insn(pc, &s.info);
408 monitor_printf(mon, "\n");
409 if (count < 0)
410 break;
411 pc += count;
412 }
413 }
414 #endif