]> git.proxmox.com Git - mirror_qemu.git/blame - disas.c
9pfs: fix 'Twalk' to only send error if no component walked
[mirror_qemu.git] / disas.c
CommitLineData
b9adb4a6 1/* General "disassemble this chunk" code. Used for debugging. */
d38ea87a 2#include "qemu/osdep.h"
3979fca4 3#include "disas/dis-asm.h"
b9adb4a6 4#include "elf.h"
30cc9831 5#include "qemu/qemu-print.h"
b9adb4a6 6
76cad711 7#include "disas/disas.h"
8ca80760 8#include "disas/capstone.h"
c6105c0a 9
f4359b9f
BS
10typedef struct CPUDebug {
11 struct disassemble_info info;
d49190c4 12 CPUState *cpu;
f4359b9f
BS
13} CPUDebug;
14
b9adb4a6 15/* Filled in by elfload.c. Simplistic, but will do for now. */
e80cfcfc 16struct syminfo *syminfos = NULL;
b9adb4a6 17
12b6e9b2
RH
18/*
19 * Get LENGTH bytes from info's buffer, at host address memaddr.
20 * Transfer them to myaddr.
21 */
22static int host_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
23 struct disassemble_info *info)
aa0aa4fa 24{
c6105c0a 25 if (memaddr < info->buffer_vma
12b6e9b2 26 || memaddr + length > info->buffer_vma + info->buffer_length) {
c6105c0a
FB
27 /* Out of bounds. Use EIO because GDB uses it. */
28 return EIO;
12b6e9b2 29 }
c6105c0a
FB
30 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
31 return 0;
aa0aa4fa
FB
32}
33
12b6e9b2
RH
34/*
35 * Get LENGTH bytes from info's buffer, at target address memaddr.
36 * Transfer them to myaddr.
37 */
38static int target_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
39 struct disassemble_info *info)
c6105c0a 40{
f4359b9f 41 CPUDebug *s = container_of(info, CPUDebug, info);
12b6e9b2 42 int r = cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
6766ba50 43 return r ? EIO : 0;
c6105c0a 44}
c6105c0a 45
12b6e9b2
RH
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 */
50static void perror_memory(int status, bfd_vma memaddr,
51 struct disassemble_info *info)
aa0aa4fa 52{
12b6e9b2
RH
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 }
aa0aa4fa
FB
62}
63
12b6e9b2
RH
64/* Print address in hex. */
65static void print_address(bfd_vma addr, struct disassemble_info *info)
aa0aa4fa 66{
12b6e9b2 67 info->fprintf_func(info->stream, "0x%" PRIx64, addr);
aa0aa4fa
FB
68}
69
636bd289 70/* Print address in hex, truncated to the width of a host virtual address. */
12b6e9b2 71static void host_print_address(bfd_vma addr, struct disassemble_info *info)
636bd289 72{
12b6e9b2 73 print_address((uintptr_t)addr, info);
636bd289
PM
74}
75
12b6e9b2
RH
76/* Stub prevents some fruitless earching in optabs disassemblers. */
77static int symbol_at_address(bfd_vma addr, struct disassemble_info *info)
aa0aa4fa 78{
12b6e9b2 79 return 1;
aa0aa4fa
FB
80}
81
c46ffd57
RH
82static 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
101static int print_insn_od_host(bfd_vma pc, disassemble_info *info)
102{
103 return print_insn_objdump(pc, info, "OBJD-H");
104}
105
106static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
107{
108 return print_insn_objdump(pc, info, "OBJD-T");
109}
110
12b6e9b2
RH
111static 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
122static 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;
ee3eb3a7 129#if TARGET_BIG_ENDIAN
12b6e9b2
RH
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
141static 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;
e03b5686 147#if HOST_BIG_ENDIAN
12b6e9b2
RH
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;
12b6e9b2
RH
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;
12b6e9b2
RH
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)
12b6e9b2
RH
167 s->info.cap_arch = CS_ARCH_PPC;
168# ifdef _ARCH_PPC64
169 s->info.cap_mode = CS_MODE_64;
170# endif
171#elif defined(__riscv) && defined(CONFIG_RISCV_DIS)
172#if defined(_ILP32) || (__riscv_xlen == 32)
173 s->info.print_insn = print_insn_riscv32;
174#elif defined(_LP64)
175 s->info.print_insn = print_insn_riscv64;
176#else
177#error unsupported RISC-V ABI
178#endif
a4038a00 179#elif defined(__aarch64__)
12b6e9b2 180 s->info.cap_arch = CS_ARCH_ARM64;
a4038a00
RH
181# ifdef CONFIG_ARM_A64_DIS
182 s->info.print_insn = print_insn_arm_a64;
183# endif
12b6e9b2
RH
184#elif defined(__alpha__)
185 s->info.print_insn = print_insn_alpha;
186#elif defined(__sparc__)
187 s->info.print_insn = print_insn_sparc;
188 s->info.mach = bfd_mach_sparc_v9b;
189#elif defined(__arm__)
190 /* TCG only generates code for arm mode. */
12b6e9b2
RH
191 s->info.cap_arch = CS_ARCH_ARM;
192#elif defined(__MIPSEB__)
193 s->info.print_insn = print_insn_big_mips;
194#elif defined(__MIPSEL__)
195 s->info.print_insn = print_insn_little_mips;
196#elif defined(__m68k__)
197 s->info.print_insn = print_insn_m68k;
198#elif defined(__s390__)
3d562845
RH
199 s->info.cap_arch = CS_ARCH_SYSZ;
200 s->info.cap_insn_unit = 2;
201 s->info.cap_insn_split = 6;
12b6e9b2
RH
202#elif defined(__hppa__)
203 s->info.print_insn = print_insn_hppa;
204#endif
205}
206
1d48474d 207/* Disassemble this for me please... (debugging). */
d49190c4 208void target_disas(FILE *out, CPUState *cpu, target_ulong code,
1d48474d 209 target_ulong size)
b9adb4a6 210{
c27004ec 211 target_ulong pc;
b9adb4a6 212 int count;
f4359b9f 213 CPUDebug s;
b9adb4a6 214
12b6e9b2
RH
215 initialize_debug_target(&s, cpu);
216 s.info.fprintf_func = fprintf;
217 s.info.stream = out;
f4359b9f
BS
218 s.info.buffer_vma = code;
219 s.info.buffer_length = size;
37b9de46 220
8ca80760
RH
221 if (s.info.cap_arch >= 0 && cap_disas_target(&s.info, code, size)) {
222 return;
223 }
224
2de295c5
PC
225 if (s.info.print_insn == NULL) {
226 s.info.print_insn = print_insn_od_target;
c46ffd57 227 }
c6105c0a 228
7e000c2e 229 for (pc = code; size > 0; pc += count, size -= count) {
fa15e030 230 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
2de295c5 231 count = s.info.print_insn(pc, &s.info);
c27004ec
FB
232 fprintf(out, "\n");
233 if (count < 0)
234 break;
754d00ae 235 if (size < count) {
236 fprintf(out,
237 "Disassembler disagrees with translator over instruction "
238 "decoding\n"
239 "Please report this to qemu-devel@nongnu.org\n");
240 break;
241 }
c27004ec
FB
242 }
243}
244
cbafa236
AB
245static int plugin_printf(FILE *stream, const char *fmt, ...)
246{
b71f3a68
RH
247 /* We abuse the FILE parameter to pass a GString. */
248 GString *s = (GString *)stream;
cbafa236 249 int initial_len = s->len;
b71f3a68 250 va_list va;
cbafa236
AB
251
252 va_start(va, fmt);
253 g_string_append_vprintf(s, fmt, va);
254 va_end(va);
255
256 return s->len - initial_len;
257}
258
259static void plugin_print_address(bfd_vma addr, struct disassemble_info *info)
260{
261 /* does nothing */
262}
263
264
cbafa236
AB
265/*
266 * We should only be dissembling one instruction at a time here. If
267 * there is left over it usually indicates the front end has read more
268 * bytes than it needed.
269 */
270char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size)
271{
cbafa236 272 CPUDebug s;
b71f3a68 273 GString *ds = g_string_new(NULL);
cbafa236 274
12b6e9b2
RH
275 initialize_debug_target(&s, cpu);
276 s.info.fprintf_func = plugin_printf;
b71f3a68 277 s.info.stream = (FILE *)ds; /* abuse this slot */
cbafa236
AB
278 s.info.buffer_vma = addr;
279 s.info.buffer_length = size;
280 s.info.print_address_func = plugin_print_address;
cbafa236
AB
281
282 if (s.info.cap_arch >= 0 && cap_disas_plugin(&s.info, addr, size)) {
b71f3a68
RH
283 ; /* done */
284 } else if (s.info.print_insn) {
285 s.info.print_insn(addr, &s.info);
286 } else {
287 ; /* cannot disassemble -- return empty string */
cbafa236
AB
288 }
289
b71f3a68
RH
290 /* Return the buffer, freeing the GString container. */
291 return g_string_free(ds, false);
cbafa236
AB
292}
293
c27004ec 294/* Disassemble this for me please... (debugging). */
f06176be 295void disas(FILE *out, const void *code, unsigned long size)
c27004ec 296{
b0b0f1c9 297 uintptr_t pc;
c27004ec 298 int count;
f4359b9f 299 CPUDebug s;
c27004ec 300
12b6e9b2
RH
301 initialize_debug_host(&s);
302 s.info.fprintf_func = fprintf;
303 s.info.stream = out;
f4359b9f
BS
304 s.info.buffer = code;
305 s.info.buffer_vma = (uintptr_t)code;
306 s.info.buffer_length = size;
8ca80760 307
4c389f6e 308 if (s.info.cap_arch >= 0 && cap_disas_host(&s.info, code, size)) {
8ca80760
RH
309 return;
310 }
311
12b6e9b2
RH
312 if (s.info.print_insn == NULL) {
313 s.info.print_insn = print_insn_od_host;
c46ffd57 314 }
b0b0f1c9
SW
315 for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
316 fprintf(out, "0x%08" PRIxPTR ": ", pc);
12b6e9b2 317 count = s.info.print_insn(pc, &s.info);
e5ef4ec2
AB
318 fprintf(out, "\n");
319 if (count < 0) {
320 break;
321 }
b9adb4a6 322 }
e5ef4ec2 323
b9adb4a6
FB
324}
325
326/* Look up symbol for debugging purpose. Returns "" if unknown. */
c27004ec 327const char *lookup_symbol(target_ulong orig_addr)
b9adb4a6 328{
49918a75 329 const char *symbol = "";
e80cfcfc 330 struct syminfo *s;
3b46e624 331
e80cfcfc 332 for (s = syminfos; s; s = s->next) {
49918a75
PB
333 symbol = s->lookup_symbol(s, orig_addr);
334 if (symbol[0] != '\0') {
335 break;
336 }
b9adb4a6 337 }
49918a75
PB
338
339 return symbol;
b9adb4a6 340}
9307c4c1
FB
341
342#if !defined(CONFIG_USER_ONLY)
343
83c9089e 344#include "monitor/monitor.h"
3d2cfdf1 345
9307c4c1 346static int
b8d87208 347physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
a5f1b965 348 struct disassemble_info *info)
9307c4c1 349{
f2c6abc8 350 CPUDebug *s = container_of(info, CPUDebug, info);
6766ba50 351 MemTxResult res;
f2c6abc8 352
6766ba50
PMD
353 res = address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED,
354 myaddr, length);
355 return res == MEMTX_OK ? 0 : EIO;
9307c4c1
FB
356}
357
1d48474d 358/* Disassembler for the monitor. */
d49190c4 359void monitor_disas(Monitor *mon, CPUState *cpu,
1d48474d 360 target_ulong pc, int nb_insn, int is_physical)
9307c4c1 361{
9307c4c1 362 int count, i;
f4359b9f 363 CPUDebug s;
9307c4c1 364
12b6e9b2
RH
365 initialize_debug_target(&s, cpu);
366 s.info.fprintf_func = qemu_fprintf;
367 if (is_physical) {
368 s.info.read_memory_func = physical_read_memory;
37b9de46 369 }
12b6e9b2 370 s.info.buffer_vma = pc;
37b9de46 371
8ca80760
RH
372 if (s.info.cap_arch >= 0 && cap_disas_monitor(&s.info, pc, nb_insn)) {
373 return;
374 }
375
37b9de46
PC
376 if (!s.info.print_insn) {
377 monitor_printf(mon, "0x" TARGET_FMT_lx
378 ": Asm output not supported on this arch\n", pc);
379 return;
380 }
9307c4c1
FB
381
382 for(i = 0; i < nb_insn; i++) {
376253ec 383 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
2de295c5 384 count = s.info.print_insn(pc, &s.info);
376253ec 385 monitor_printf(mon, "\n");
9307c4c1
FB
386 if (count < 0)
387 break;
388 pc += count;
389 }
390}
391#endif