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