Skip to contentSkip to conversation

Make the core disassembler functions target-independent

RFC PATCH In review Thomas Huth 2 participants 6 messages May 8, 2023 – May 9, 2023 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover Make the core disassembler functions target-independent

Move disas.c into the target-independent source set, so that we only have to compile this code once instead multiple times (one time for each target).

Marked as RFC since we have to replace the target_ulongs here with hwaddr, and the TARGET_FMT_lx with HWADDR_FMT_plx, which is a little bit ugly ... what's your opinion?

Thomas Huth (2):
disas: Move softmmu specific code to separate file
disas: Move disas.c into the target-independent source set

 meson.build            |  4 +-
 disas/disas-internal.h | 20 +++++++++
 include/disas/disas.h  | 21 +++------
 include/hw/elf_ops.h   |  2 +-
 bsd-user/elfload.c     |  2 +-
 disas.c                | 98 ++++++++----------------------------------
 disas/disas-mon.c      | 65 ++++++++++++++++++++++++++++
 linux-user/elfload.c   |  2 +-
 disas/meson.build      |  2 +
 9 files changed, 116 insertions(+), 100 deletions(-)
 create mode 100644 disas/disas-internal.h
 create mode 100644 disas/disas-mon.c
Signature · 1 line
2.31.1
#1 lore raw

RFC PATCH 1/2 disas: Move softmmu specific code to separate file

We'd like to move disas.c into the common code source set, where CONFIG_USER_ONLY is not available anymore. So we have to move the related code into a separate file instead.

While doing the movement, change the type of the "pc" parameter of the monitor_disas() function to "hwaddr" instead of "target_ulong", so we can add the file to the generic softmmu_ss source set instead of the specific_ss source set.

---
 disas/disas-internal.h | 20 +++++++++++
 include/disas/disas.h  |  6 ++--
 disas.c                | 76 ++++--------------------------------------
 disas/disas-mon.c      | 65 ++++++++++++++++++++++++++++++++++++
 disas/meson.build      |  2 ++
 5 files changed, 96 insertions(+), 73 deletions(-)
 create mode 100644 disas/disas-internal.h
 create mode 100644 disas/disas-mon.c
disas/disas-internal.h new file +20 -0
new file mode 100644
index 0000000000..354c01307b
@@ -0,0 +1,20 @@
+/*
+ * Definitions used internally in the disassembly code
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef DISAS_INTERNAL_H
+#define DISAS_INTERNAL_H
+
+#include "disas/dis-asm.h"
+
+typedef struct CPUDebug {
+    struct disassemble_info info;
+    CPUState *cpu;
+} CPUDebug;
+
+void disas_initialize_debug_target(CPUDebug *s, CPUState *cpu);
+int disas_gstring_printf(FILE *stream, const char *fmt, ...) G_GNUC_PRINTF(2, 3);
+
+#endif
include/disas/disas.h +3 -3
index d363e95ede..6c04428620 100644
@@ -11,15 +11,15 @@ void disas(FILE *out, const void *code, unsigned long size);
 void target_disas(FILE *out, CPUState *cpu, target_ulong code,
                   target_ulong size);
 
-void monitor_disas(Monitor *mon, CPUState *cpu,
-                   target_ulong pc, int nb_insn, int is_physical);
-
 char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size);
 
 /* Look up symbol for debugging purpose.  Returns "" if unknown. */
 const char *lookup_symbol(target_ulong orig_addr);
 #endif
 
+void monitor_disas(Monitor *mon, CPUState *cpu,
+                   hwaddr pc, int nb_insn, int is_physical);
+
 struct syminfo;
 struct elf32_sym;
 struct elf64_sym;
disas.c +6 -70

114 lines not on this page · raw message

disas/disas-mon.c new file +65 -0

65 lines not on this page · raw message

disas/meson.build +2 -0
index c865bdd882..ca6dd81ecf 100644
@@ -11,3 +11,5 @@ common_ss.add(when: 'CONFIG_SH4_DIS', if_true: files('sh4.c'))
 common_ss.add(when: 'CONFIG_SPARC_DIS', if_true: files('sparc.c'))
 common_ss.add(when: 'CONFIG_XTENSA_DIS', if_true: files('xtensa.c'))
 common_ss.add(when: capstone, if_true: files('capstone.c'))
+
+softmmu_ss.add(files('disas-mon.c'))
Signature · 1 line
2.31.1
#2 lore raw

RFC PATCH 2/2 disas: Move disas.c into the target-independent source set

In reply to Thomas Huth

By using hwaddr instead of target_ulong and by tweaking some other spots, we can turn this code into target-independent code for compiling it only once and not multiple times during the build process.

---
 meson.build           |  4 ++--
 include/disas/disas.h | 15 +++------------
 include/hw/elf_ops.h  |  2 +-
 bsd-user/elfload.c    |  2 +-
 disas.c               | 22 ++++++++++++----------
 linux-user/elfload.c  |  2 +-
 6 files changed, 20 insertions(+), 27 deletions(-)
meson.build +2 -2
index 229eb585f7..73ceed037f 100644
@@ -3152,8 +3152,8 @@ specific_ss.add(files('cpu.c'))
 
 subdir('softmmu')
 
-common_ss.add(capstone)
-specific_ss.add(files('disas.c'), capstone)
+common_ss.add(files('disas.c'), capstone)
+specific_ss.add(capstone)
 
 # Work around a gcc bug/misfeature wherein constant propagation looks
 # through an alias:
include/disas/disas.h +3 -12
index 6c04428620..5132ebf982 100644
@@ -3,19 +3,14 @@
 
 #include "exec/hwaddr.h"
 
-#ifdef NEED_CPU_H
-#include "cpu.h"
-
 /* Disassemble this for me please... (debugging). */
 void disas(FILE *out, const void *code, unsigned long size);
-void target_disas(FILE *out, CPUState *cpu, target_ulong code,
-                  target_ulong size);
+void target_disas(FILE *out, CPUState *cpu, hwaddr code, long size);
 
 char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size);
 
 /* Look up symbol for debugging purpose.  Returns "" if unknown. */
-const char *lookup_symbol(target_ulong orig_addr);
-#endif
+const char *lookup_symbol(hwaddr orig_addr);
 
 void monitor_disas(Monitor *mon, CPUState *cpu,
                    hwaddr pc, int nb_insn, int is_physical);
@@ -24,11 +19,7 @@ struct syminfo;
 struct elf32_sym;
 struct elf64_sym;
 
-#if defined(CONFIG_USER_ONLY)
-typedef const char *(*lookup_symbol_t)(struct syminfo *s, target_ulong orig_addr);
-#else
-typedef const char *(*lookup_symbol_t)(struct syminfo *s, hwaddr orig_addr);
-#endif
+typedef const char *(*lookup_symbol_t)(struct syminfo *s, uint64_t orig_addr);
 
 struct syminfo {
     lookup_symbol_t lookup_symbol;
include/hw/elf_ops.h +1 -1
index dffb0e73d2..5e2af4d504 100644
@@ -81,7 +81,7 @@ static int glue(symfind, SZ)(const void *s0, const void *s1)
 }
 
 static const char *glue(lookup_symbol, SZ)(struct syminfo *s,
-                                           hwaddr orig_addr)
+                                           uint64_t orig_addr)
 {
     struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
     struct elf_sym *sym;
bsd-user/elfload.c +1 -1
index fbcdc94b96..7c784518ed 100644
@@ -363,7 +363,7 @@ static int symfind(const void *s0, const void *s1)
     return result;
 }
 
-static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
+static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr)
 {
 #if ELF_CLASS == ELFCLASS32
     struct elf_sym *syms = s->disas_symtab.elf32;
disas.c +12 -10
index 4f4a07d611..31edbd2e70 100644
@@ -3,6 +3,9 @@
 #include "disas/disas-internal.h"
 #include "elf.h"
 #include "qemu/qemu-print.h"
+#include "exec/cpu-common.h"
+#include "exec/memory.h"
+#include "hw/core/cpu.h"
 
 #include "disas/disas.h"
 #include "disas/capstone.h"
@@ -121,11 +124,11 @@ void disas_initialize_debug_target(CPUDebug *s, CPUState *cpu)
     s->cpu = cpu;
     s->info.read_memory_func = target_read_memory;
     s->info.print_address_func = print_address;
-#if TARGET_BIG_ENDIAN
-    s->info.endian = BFD_ENDIAN_BIG;
-#else
-    s->info.endian = BFD_ENDIAN_LITTLE;
-#endif
+    if (target_words_bigendian()) {
+        s->info.endian = BFD_ENDIAN_BIG;
+    } else {
+        s->info.endian =  BFD_ENDIAN_LITTLE;
+    }
 
     CPUClass *cc = CPU_GET_CLASS(cpu);
     if (cc->disas_set_info) {
@@ -199,10 +202,9 @@ static void initialize_debug_host(CPUDebug *s)
 }
 
 /* Disassemble this for me please... (debugging).  */
-void target_disas(FILE *out, CPUState *cpu, target_ulong code,
-                  target_ulong size)
+void target_disas(FILE *out, CPUState *cpu, hwaddr code, long size)
 {
-    target_ulong pc;
+    hwaddr pc;
     int count;
     CPUDebug s;
 
@@ -221,7 +223,7 @@ void target_disas(FILE *out, CPUState *cpu, target_ulong code,
     }
 
     for (pc = code; size > 0; pc += count, size -= count) {
-	fprintf(out, "0x" TARGET_FMT_lx ":  ", pc);
+        fprintf(out, "0x" HWADDR_FMT_plx ":  ", pc);
 	count = s.info.print_insn(pc, &s.info);
 	fprintf(out, "\n");
 	if (count < 0)
@@ -318,7 +320,7 @@ void disas(FILE *out, const void *code, unsigned long size)
 }
 
 /* Look up symbol for debugging purpose.  Returns "" if unknown. */
-const char *lookup_symbol(target_ulong orig_addr)
+const char *lookup_symbol(hwaddr orig_addr)
 {
     const char *symbol = "";
     struct syminfo *s;
linux-user/elfload.c +1 -1
index 703f7434a0..48c9e910a8 100644
@@ -3338,7 +3338,7 @@ static int symfind(const void *s0, const void *s1)
     return result;
 }
 
-static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
+static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr)
 {
 #if ELF_CLASS == ELFCLASS32
     struct elf_sym *syms = s->disas_symtab.elf32;
Signature · 1 line
2.31.1
#3 lore raw
In reply to Thomas Huth
Quoted 11 lines · Thomas Huth

Move disas.c into the target-independent source set, so that we only have to compile this code once instead multiple times (one time for each target).

Marked as RFC since we have to replace the target_ulongs here with hwaddr, and the TARGET_FMT_lx with HWADDR_FMT_plx, which is a little bit ugly ... what's your opinion?

Thomas Huth (2):
disas: Move softmmu specific code to separate file
disas: Move disas.c into the target-independent source set

Patches 79-83 from

https://patchew.org/QEMU/[email protected]/

do the same thing, using uint64_t instead of hwaddr (it's not).

r~

#4 lore raw
Quoted 13 lines · Richard Henderson
Quoted 11 lines · Thomas Huth

Move disas.c into the target-independent source set, so that we only have to compile this code once instead multiple times (one time for each target).

Marked as RFC since we have to replace the target_ulongs here with hwaddr, and the TARGET_FMT_lx with HWADDR_FMT_plx, which is a little bit ugly ... what's your opinion?

Thomas Huth (2):
   disas: Move softmmu specific code to separate file
   disas: Move disas.c into the target-independent source set

Quoted 5 lines

Patches 79-83 from

https://patchew.org/QEMU/[email protected]/

do the same thing, using uint64_t instead of hwaddr (it's not).

Ah, great, so I can scratch this from my list :-)

But FYI, you missed to update bsd-user/elfload.c in the patch that updates linux-user/elfload.c.

Thomas

#5 lore raw
In reply to Richard Henderson
Quoted 13 lines · Richard Henderson
Quoted 11 lines · Thomas Huth

Move disas.c into the target-independent source set, so that we only have to compile this code once instead multiple times (one time for each target).

Marked as RFC since we have to replace the target_ulongs here with hwaddr, and the TARGET_FMT_lx with HWADDR_FMT_plx, which is a little bit ugly ... what's your opinion?

Thomas Huth (2):
   disas: Move softmmu specific code to separate file
   disas: Move disas.c into the target-independent source set

Quoted 5 lines

Patches 79-83 from

https://patchew.org/QEMU/[email protected]/

do the same thing, using uint64_t instead of hwaddr (it's not).

I've just realized that only the choice of hwaddr/uint64_t overlap; I failed to take this all of the way to building disas once.

I will integrate our two sets.

r~