2
0

disas.c 11 KB

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