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