]> git.proxmox.com Git - mirror_qemu.git/blob - disas.c
disas: Remove old libopcode i386 disassembler
[mirror_qemu.git] / disas.c
1 /* General "disassemble this chunk" code. Used for debugging. */
2 #include "qemu/osdep.h"
3 #include "disas/dis-asm.h"
4 #include "elf.h"
5 #include "qemu/qemu-print.h"
6
7 #include "disas/disas.h"
8 #include "disas/capstone.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 /*
19 * Get LENGTH bytes from info's buffer, at host address memaddr.
20 * Transfer them to myaddr.
21 */
22 static int host_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 }
30 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
31 return 0;
32 }
33
34 /*
35 * Get LENGTH bytes from info's buffer, at target address memaddr.
36 * Transfer them to myaddr.
37 */
38 static int target_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
39 struct disassemble_info *info)
40 {
41 CPUDebug *s = container_of(info, CPUDebug, info);
42 int r = cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
43 return r ? EIO : 0;
44 }
45
46 /*
47 * Print an error message. We can assume that this is in response to
48 * an error return from {host,target}_read_memory.
49 */
50 static void perror_memory(int status, bfd_vma memaddr,
51 struct disassemble_info *info)
52 {
53 if (status != EIO) {
54 /* Can't happen. */
55 info->fprintf_func(info->stream, "Unknown error %d\n", status);
56 } else {
57 /* Address between memaddr and memaddr + len was out of bounds. */
58 info->fprintf_func(info->stream,
59 "Address 0x%" PRIx64 " is out of bounds.\n",
60 memaddr);
61 }
62 }
63
64 /* Print address in hex. */
65 static void print_address(bfd_vma addr, struct disassemble_info *info)
66 {
67 info->fprintf_func(info->stream, "0x%" PRIx64, addr);
68 }
69
70 /* Print address in hex, truncated to the width of a host virtual address. */
71 static void host_print_address(bfd_vma addr, struct disassemble_info *info)
72 {
73 print_address((uintptr_t)addr, info);
74 }
75
76 /* Stub prevents some fruitless earching in optabs disassemblers. */
77 static int symbol_at_address(bfd_vma addr, struct disassemble_info *info)
78 {
79 return 1;
80 }
81
82 static int print_insn_objdump(bfd_vma pc, disassemble_info *info,
83 const char *prefix)
84 {
85 int i, n = info->buffer_length;
86 uint8_t *buf = g_malloc(n);
87
88 info->read_memory_func(pc, buf, n, info);
89
90 for (i = 0; i < n; ++i) {
91 if (i % 32 == 0) {
92 info->fprintf_func(info->stream, "\n%s: ", prefix);
93 }
94 info->fprintf_func(info->stream, "%02x", buf[i]);
95 }
96
97 g_free(buf);
98 return n;
99 }
100
101 static int print_insn_od_host(bfd_vma pc, disassemble_info *info)
102 {
103 return print_insn_objdump(pc, info, "OBJD-H");
104 }
105
106 static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
107 {
108 return print_insn_objdump(pc, info, "OBJD-T");
109 }
110
111 static void initialize_debug(CPUDebug *s)
112 {
113 memset(s, 0, sizeof(*s));
114 s->info.arch = bfd_arch_unknown;
115 s->info.cap_arch = -1;
116 s->info.cap_insn_unit = 4;
117 s->info.cap_insn_split = 4;
118 s->info.memory_error_func = perror_memory;
119 s->info.symbol_at_address_func = symbol_at_address;
120 }
121
122 static void initialize_debug_target(CPUDebug *s, CPUState *cpu)
123 {
124 initialize_debug(s);
125
126 s->cpu = cpu;
127 s->info.read_memory_func = target_read_memory;
128 s->info.print_address_func = print_address;
129 #if TARGET_BIG_ENDIAN
130 s->info.endian = BFD_ENDIAN_BIG;
131 #else
132 s->info.endian = BFD_ENDIAN_LITTLE;
133 #endif
134
135 CPUClass *cc = CPU_GET_CLASS(cpu);
136 if (cc->disas_set_info) {
137 cc->disas_set_info(cpu, &s->info);
138 }
139 }
140
141 static void initialize_debug_host(CPUDebug *s)
142 {
143 initialize_debug(s);
144
145 s->info.read_memory_func = host_read_memory;
146 s->info.print_address_func = host_print_address;
147 #if HOST_BIG_ENDIAN
148 s->info.endian = BFD_ENDIAN_BIG;
149 #else
150 s->info.endian = BFD_ENDIAN_LITTLE;
151 #endif
152 #if defined(CONFIG_TCG_INTERPRETER)
153 s->info.print_insn = print_insn_tci;
154 #elif defined(__i386__)
155 s->info.mach = bfd_mach_i386_i386;
156 s->info.cap_arch = CS_ARCH_X86;
157 s->info.cap_mode = CS_MODE_32;
158 s->info.cap_insn_unit = 1;
159 s->info.cap_insn_split = 8;
160 #elif defined(__x86_64__)
161 s->info.mach = bfd_mach_x86_64;
162 s->info.cap_arch = CS_ARCH_X86;
163 s->info.cap_mode = CS_MODE_64;
164 s->info.cap_insn_unit = 1;
165 s->info.cap_insn_split = 8;
166 #elif defined(_ARCH_PPC)
167 s->info.disassembler_options = (char *)"any";
168 s->info.print_insn = print_insn_ppc;
169 s->info.cap_arch = CS_ARCH_PPC;
170 # ifdef _ARCH_PPC64
171 s->info.cap_mode = CS_MODE_64;
172 # endif
173 #elif defined(__riscv) && defined(CONFIG_RISCV_DIS)
174 #if defined(_ILP32) || (__riscv_xlen == 32)
175 s->info.print_insn = print_insn_riscv32;
176 #elif defined(_LP64)
177 s->info.print_insn = print_insn_riscv64;
178 #else
179 #error unsupported RISC-V ABI
180 #endif
181 #elif defined(__aarch64__)
182 s->info.cap_arch = CS_ARCH_ARM64;
183 # ifdef CONFIG_ARM_A64_DIS
184 s->info.print_insn = print_insn_arm_a64;
185 # endif
186 #elif defined(__alpha__)
187 s->info.print_insn = print_insn_alpha;
188 #elif defined(__sparc__)
189 s->info.print_insn = print_insn_sparc;
190 s->info.mach = bfd_mach_sparc_v9b;
191 #elif defined(__arm__)
192 /* TCG only generates code for arm mode. */
193 s->info.cap_arch = CS_ARCH_ARM;
194 #elif defined(__MIPSEB__)
195 s->info.print_insn = print_insn_big_mips;
196 #elif defined(__MIPSEL__)
197 s->info.print_insn = print_insn_little_mips;
198 #elif defined(__m68k__)
199 s->info.print_insn = print_insn_m68k;
200 #elif defined(__s390__)
201 s->info.cap_arch = CS_ARCH_SYSZ;
202 s->info.cap_insn_unit = 2;
203 s->info.cap_insn_split = 6;
204 #elif defined(__hppa__)
205 s->info.print_insn = print_insn_hppa;
206 #endif
207 }
208
209 /* Disassemble this for me please... (debugging). */
210 void target_disas(FILE *out, CPUState *cpu, target_ulong code,
211 target_ulong size)
212 {
213 target_ulong pc;
214 int count;
215 CPUDebug s;
216
217 initialize_debug_target(&s, cpu);
218 s.info.fprintf_func = fprintf;
219 s.info.stream = out;
220 s.info.buffer_vma = code;
221 s.info.buffer_length = size;
222
223 if (s.info.cap_arch >= 0 && cap_disas_target(&s.info, code, size)) {
224 return;
225 }
226
227 if (s.info.print_insn == NULL) {
228 s.info.print_insn = print_insn_od_target;
229 }
230
231 for (pc = code; size > 0; pc += count, size -= count) {
232 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
233 count = s.info.print_insn(pc, &s.info);
234 fprintf(out, "\n");
235 if (count < 0)
236 break;
237 if (size < count) {
238 fprintf(out,
239 "Disassembler disagrees with translator over instruction "
240 "decoding\n"
241 "Please report this to qemu-devel@nongnu.org\n");
242 break;
243 }
244 }
245 }
246
247 static int plugin_printf(FILE *stream, const char *fmt, ...)
248 {
249 /* We abuse the FILE parameter to pass a GString. */
250 GString *s = (GString *)stream;
251 int initial_len = s->len;
252 va_list va;
253
254 va_start(va, fmt);
255 g_string_append_vprintf(s, fmt, va);
256 va_end(va);
257
258 return s->len - initial_len;
259 }
260
261 static void plugin_print_address(bfd_vma addr, struct disassemble_info *info)
262 {
263 /* does nothing */
264 }
265
266
267 /*
268 * We should only be dissembling one instruction at a time here. If
269 * there is left over it usually indicates the front end has read more
270 * bytes than it needed.
271 */
272 char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size)
273 {
274 CPUDebug s;
275 GString *ds = g_string_new(NULL);
276
277 initialize_debug_target(&s, cpu);
278 s.info.fprintf_func = plugin_printf;
279 s.info.stream = (FILE *)ds; /* abuse this slot */
280 s.info.buffer_vma = addr;
281 s.info.buffer_length = size;
282 s.info.print_address_func = plugin_print_address;
283
284 if (s.info.cap_arch >= 0 && cap_disas_plugin(&s.info, addr, size)) {
285 ; /* done */
286 } else if (s.info.print_insn) {
287 s.info.print_insn(addr, &s.info);
288 } else {
289 ; /* cannot disassemble -- return empty string */
290 }
291
292 /* Return the buffer, freeing the GString container. */
293 return g_string_free(ds, false);
294 }
295
296 /* Disassemble this for me please... (debugging). */
297 void disas(FILE *out, const void *code, unsigned long size)
298 {
299 uintptr_t pc;
300 int count;
301 CPUDebug s;
302
303 initialize_debug_host(&s);
304 s.info.fprintf_func = fprintf;
305 s.info.stream = out;
306 s.info.buffer = code;
307 s.info.buffer_vma = (uintptr_t)code;
308 s.info.buffer_length = size;
309
310 if (s.info.cap_arch >= 0 && cap_disas_host(&s.info, code, size)) {
311 return;
312 }
313
314 if (s.info.print_insn == NULL) {
315 s.info.print_insn = print_insn_od_host;
316 }
317 for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
318 fprintf(out, "0x%08" PRIxPTR ": ", pc);
319 count = s.info.print_insn(pc, &s.info);
320 fprintf(out, "\n");
321 if (count < 0) {
322 break;
323 }
324 }
325
326 }
327
328 /* Look up symbol for debugging purpose. Returns "" if unknown. */
329 const char *lookup_symbol(target_ulong orig_addr)
330 {
331 const char *symbol = "";
332 struct syminfo *s;
333
334 for (s = syminfos; s; s = s->next) {
335 symbol = s->lookup_symbol(s, orig_addr);
336 if (symbol[0] != '\0') {
337 break;
338 }
339 }
340
341 return symbol;
342 }
343
344 #if !defined(CONFIG_USER_ONLY)
345
346 #include "monitor/monitor.h"
347
348 static int
349 physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
350 struct disassemble_info *info)
351 {
352 CPUDebug *s = container_of(info, CPUDebug, info);
353 MemTxResult res;
354
355 res = address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED,
356 myaddr, length);
357 return res == MEMTX_OK ? 0 : EIO;
358 }
359
360 /* Disassembler for the monitor. */
361 void monitor_disas(Monitor *mon, CPUState *cpu,
362 target_ulong pc, int nb_insn, int is_physical)
363 {
364 int count, i;
365 CPUDebug s;
366
367 initialize_debug_target(&s, cpu);
368 s.info.fprintf_func = qemu_fprintf;
369 if (is_physical) {
370 s.info.read_memory_func = physical_read_memory;
371 }
372 s.info.buffer_vma = pc;
373
374 if (s.info.cap_arch >= 0 && cap_disas_monitor(&s.info, pc, nb_insn)) {
375 return;
376 }
377
378 if (!s.info.print_insn) {
379 monitor_printf(mon, "0x" TARGET_FMT_lx
380 ": Asm output not supported on this arch\n", pc);
381 return;
382 }
383
384 for(i = 0; i < nb_insn; i++) {
385 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
386 count = s.info.print_insn(pc, &s.info);
387 monitor_printf(mon, "\n");
388 if (count < 0)
389 break;
390 pc += count;
391 }
392 }
393 #endif