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