1e22d3c48SThomas Huth /*
2e22d3c48SThomas Huth * Functions related to disassembly from the monitor
3e22d3c48SThomas Huth *
4e22d3c48SThomas Huth * SPDX-License-Identifier: GPL-2.0-or-later
5e22d3c48SThomas Huth */
6e22d3c48SThomas Huth
7e22d3c48SThomas Huth #include "qemu/osdep.h"
8e22d3c48SThomas Huth #include "disas-internal.h"
9e22d3c48SThomas Huth #include "disas/disas.h"
10e22d3c48SThomas Huth #include "exec/memory.h"
11e22d3c48SThomas Huth #include "hw/core/cpu.h"
12e22d3c48SThomas Huth #include "monitor/monitor.h"
13e22d3c48SThomas Huth
14*4c833c60SRichard Henderson /*
15*4c833c60SRichard Henderson * Get LENGTH bytes from info's buffer, at target address memaddr.
16*4c833c60SRichard Henderson * Transfer them to myaddr.
17*4c833c60SRichard Henderson */
18*4c833c60SRichard Henderson static int
virtual_read_memory(bfd_vma memaddr,bfd_byte * myaddr,int length,struct disassemble_info * info)19*4c833c60SRichard Henderson virtual_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
20*4c833c60SRichard Henderson struct disassemble_info *info)
21*4c833c60SRichard Henderson {
22*4c833c60SRichard Henderson CPUDebug *s = container_of(info, CPUDebug, info);
23*4c833c60SRichard Henderson int r = cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
24*4c833c60SRichard Henderson return r ? EIO : 0;
25*4c833c60SRichard Henderson }
26*4c833c60SRichard Henderson
27e22d3c48SThomas Huth static int
physical_read_memory(bfd_vma memaddr,bfd_byte * myaddr,int length,struct disassemble_info * info)28e22d3c48SThomas Huth physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
29e22d3c48SThomas Huth struct disassemble_info *info)
30e22d3c48SThomas Huth {
31e22d3c48SThomas Huth CPUDebug *s = container_of(info, CPUDebug, info);
32e22d3c48SThomas Huth MemTxResult res;
33e22d3c48SThomas Huth
34e22d3c48SThomas Huth res = address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED,
35e22d3c48SThomas Huth myaddr, length);
36e22d3c48SThomas Huth return res == MEMTX_OK ? 0 : EIO;
37e22d3c48SThomas Huth }
38e22d3c48SThomas Huth
39e22d3c48SThomas Huth /* Disassembler for the monitor. */
monitor_disas(Monitor * mon,CPUState * cpu,uint64_t pc,int nb_insn,bool is_physical)40e22d3c48SThomas Huth void monitor_disas(Monitor *mon, CPUState *cpu, uint64_t pc,
41e22d3c48SThomas Huth int nb_insn, bool is_physical)
42e22d3c48SThomas Huth {
43e22d3c48SThomas Huth int count, i;
44e22d3c48SThomas Huth CPUDebug s;
45e22d3c48SThomas Huth g_autoptr(GString) ds = g_string_new("");
46e22d3c48SThomas Huth
47e22d3c48SThomas Huth disas_initialize_debug_target(&s, cpu);
48e22d3c48SThomas Huth s.info.fprintf_func = disas_gstring_printf;
49e22d3c48SThomas Huth s.info.stream = (FILE *)ds; /* abuse this slot */
5013af3af1SRichard Henderson s.info.show_opcodes = true;
51e22d3c48SThomas Huth
52e22d3c48SThomas Huth if (is_physical) {
53e22d3c48SThomas Huth s.info.read_memory_func = physical_read_memory;
54*4c833c60SRichard Henderson } else {
55*4c833c60SRichard Henderson s.info.read_memory_func = virtual_read_memory;
56e22d3c48SThomas Huth }
57e22d3c48SThomas Huth s.info.buffer_vma = pc;
58e22d3c48SThomas Huth
59e22d3c48SThomas Huth if (s.info.cap_arch >= 0 && cap_disas_monitor(&s.info, pc, nb_insn)) {
60e22d3c48SThomas Huth monitor_puts(mon, ds->str);
61e22d3c48SThomas Huth return;
62e22d3c48SThomas Huth }
63e22d3c48SThomas Huth
64e22d3c48SThomas Huth if (!s.info.print_insn) {
65e22d3c48SThomas Huth monitor_printf(mon, "0x%08" PRIx64
66e22d3c48SThomas Huth ": Asm output not supported on this arch\n", pc);
67e22d3c48SThomas Huth return;
68e22d3c48SThomas Huth }
69e22d3c48SThomas Huth
70e22d3c48SThomas Huth for (i = 0; i < nb_insn; i++) {
71e22d3c48SThomas Huth g_string_append_printf(ds, "0x%08" PRIx64 ": ", pc);
72e22d3c48SThomas Huth count = s.info.print_insn(pc, &s.info);
73e22d3c48SThomas Huth g_string_append_c(ds, '\n');
74e22d3c48SThomas Huth if (count < 0) {
75e22d3c48SThomas Huth break;
76e22d3c48SThomas Huth }
77e22d3c48SThomas Huth pc += count;
78e22d3c48SThomas Huth }
79e22d3c48SThomas Huth
80e22d3c48SThomas Huth monitor_puts(mon, ds->str);
81e22d3c48SThomas Huth }
82