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