disas.c 11 KB

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