Skip to contentSkip to conversation

dump: Make most of it target agnostic (build once)

PATCH Superseded Philippe Mathieu-Daudé 3 participants 10 messages Feb 24, 2023 – Feb 25, 2023 Arm/Amlogic Meson SoC support

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover dump: Make most of it target agnostic (build once)

Since v4:
- more unused headers removed
- KISS, use a bit of #ifdef'ry to avoid a stub file

Thanks to Richard help, we can now build dump.o once for all targets, keeping win_dump.o for x86* targets.

Philippe Mathieu-Daudé (5):
dump: Replace tswapN() -> cpu_to_dumpN()
dump: Replace TARGET_PAGE_SIZE -> qemu_target_page_size()
dump: Clean included headers
dump: Introduce win_dump_available()
dump: Add create_win_dump() stub for non-x86 targets

 dump/dump-hmp-cmds.c |  2 +-
 dump/dump.c          | 35 +++++++++++++----------------------
 dump/meson.build     |  4 ++--
 dump/win_dump.c      | 38 ++++++++++++++++++++++++++++----------
 dump/win_dump.h      |  5 ++++-
 5 files changed, 48 insertions(+), 36 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH v5 1/5 dump: Replace tswapN() -> cpu_to_dumpN()

All uses of tswap in that file are wrong, and should be using cpu_to_dumpN, which correctly tests the endianness of the output.

---
 dump/dump.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
dump/dump.c +4 -4
index 279b07f09b..7101169ecb 100644
@@ -907,13 +907,13 @@ static void get_note_sizes(DumpState *s, const void *note,
     if (dump_is_64bit(s)) {
         const Elf64_Nhdr *hdr = note;
         note_head_sz = sizeof(Elf64_Nhdr);
-        name_sz = tswap64(hdr->n_namesz);
-        desc_sz = tswap64(hdr->n_descsz);
+        name_sz = cpu_to_dump64(s, hdr->n_namesz);
+        desc_sz = cpu_to_dump64(s, hdr->n_descsz);
     } else {
         const Elf32_Nhdr *hdr = note;
         note_head_sz = sizeof(Elf32_Nhdr);
-        name_sz = tswap32(hdr->n_namesz);
-        desc_sz = tswap32(hdr->n_descsz);
+        name_sz = cpu_to_dump32(s, hdr->n_namesz);
+        desc_sz = cpu_to_dump32(s, hdr->n_descsz);
     }
 
     if (note_head_size) {
Signature · 1 line
2.38.1
#2 lore raw

PATCH v5 2/5 dump: Replace TARGET_PAGE_SIZE -> qemu_target_page_size()

In reply to Philippe Mathieu-Daudé

TARGET_PAGE_SIZE is target specific. In preparation of making dump.c target-agnostic, replace the compile-time TARGET_PAGE_SIZE definition by runtime qemu_target_page_size().

---
 dump/dump.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
dump/dump.c +2 -1
index 7101169ecb..3784a9054d 100644
@@ -15,6 +15,7 @@
 #include "qemu/cutils.h"
 #include "elf.h"
 #include "exec/hwaddr.h"
+#include "exec/target_page.h"
 #include "monitor/monitor.h"
 #include "sysemu/kvm.h"
 #include "sysemu/dump.h"
@@ -1859,7 +1860,7 @@ static void dump_init(DumpState *s, int fd, bool has_format,
     }
 
     if (!s->dump_info.page_size) {
-        s->dump_info.page_size = TARGET_PAGE_SIZE;
+        s->dump_info.page_size = qemu_target_page_size();
     }
 
     s->note_size = cpu_get_note_size(s->dump_info.d_class,
Signature · 1 line
2.38.1
#3 lore raw

PATCH v5 3/5 dump: Clean included headers

In reply to Philippe Mathieu-Daudé

"qemu/win_dump_defs.h" is only required by win_dump.c, but win_dump.h requires "sysemu/dump.h" which declares the DumpState type. Remove various unused headers.

---
 dump/dump-hmp-cmds.c |  2 +-
 dump/dump.c          |  6 ++----
 dump/win_dump.c      | 15 +++++----------
 dump/win_dump.h      |  2 +-
 4 files changed, 9 insertions(+), 16 deletions(-)
dump/dump-hmp-cmds.c +1 -1
index e5053b04cd..b038785fee 100644
@@ -1,5 +1,5 @@
 /*
- * Human Monitor Interface commands
+ * Windows crashdump (Human Monitor Interface commands)
  *
  * This work is licensed under the terms of the GNU GPL, version 2 or later.
  * See the COPYING file in the top-level directory.
dump/dump.c +2 -4
index 3784a9054d..a84f78738a 100644
@@ -14,22 +14,20 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "elf.h"
-#include "exec/hwaddr.h"
+#include "qemu/bswap.h"
 #include "exec/target_page.h"
 #include "monitor/monitor.h"
-#include "sysemu/kvm.h"
 #include "sysemu/dump.h"
-#include "sysemu/memory_mapping.h"
 #include "sysemu/runstate.h"
 #include "sysemu/cpus.h"
 #include "qapi/error.h"
 #include "qapi/qapi-commands-dump.h"
 #include "qapi/qapi-events-dump.h"
 #include "qapi/qmp/qerror.h"
-#include "qemu/error-report.h"
 #include "qemu/main-loop.h"
 #include "hw/misc/vmcoreinfo.h"
 #include "migration/blocker.h"
+#include "hw/core/cpu.h"
 
 #ifdef TARGET_X86_64
 #include "win_dump.h"
dump/win_dump.c +5 -10
index f20b6051b6..ba7fa404fe 100644
@@ -1,5 +1,5 @@
 /*
- * Windows crashdump
+ * Windows crashdump (target specific implementations)
  *
  * Copyright (c) 2018 Virtuozzo International GmbH
  *
@@ -9,19 +9,14 @@
  */
 
 #include "qemu/osdep.h"
-#include "qemu/cutils.h"
-#include "elf.h"
-#include "exec/hwaddr.h"
-#include "monitor/monitor.h"
-#include "sysemu/kvm.h"
 #include "sysemu/dump.h"
-#include "sysemu/memory_mapping.h"
-#include "sysemu/cpus.h"
 #include "qapi/error.h"
 #include "qapi/qmp/qerror.h"
-#include "qemu/error-report.h"
-#include "hw/misc/vmcoreinfo.h"
+#include "exec/cpu-defs.h"
+#include "hw/core/cpu.h"
+#include "qemu/win_dump_defs.h"
 #include "win_dump.h"
+#include "cpu.h"
 
 static size_t win_dump_ptr_size(bool x64)
 {
dump/win_dump.h +1 -1
index b8c25348f4..56f63683c3 100644
@@ -11,7 +11,7 @@
 #ifndef WIN_DUMP_H
 #define WIN_DUMP_H
 
-#include "qemu/win_dump_defs.h"
+#include "sysemu/dump.h"
 
 void create_win_dump(DumpState *s, Error **errp);
 
Signature · 1 line
2.38.1
#4 lore raw

PATCH v5 4/5 dump: Introduce win_dump_available()

In reply to Philippe Mathieu-Daudé

Remove a pair of TARGET_X86_64 #ifdef'ry by introducing the win_dump_available() method. Doing so we can build win_dump.c on any target, and simplify the meson rule.

---
 dump/dump.c      | 16 +++++-----------
 dump/meson.build |  2 +-
 dump/win_dump.c  | 18 ++++++++++++++++++
 dump/win_dump.h  |  3 +++
 4 files changed, 27 insertions(+), 12 deletions(-)
dump/dump.c +5 -11
index a84f78738a..0c96c6e735 100644
@@ -28,10 +28,7 @@
 #include "hw/misc/vmcoreinfo.h"
 #include "migration/blocker.h"
 #include "hw/core/cpu.h"
-
-#ifdef TARGET_X86_64
 #include "win_dump.h"
-#endif
 
 #include <zlib.h>
 #ifdef CONFIG_LZO
@@ -2128,12 +2125,10 @@ void qmp_dump_guest_memory(bool paging, const char *file,
     }
 #endif
 
-#ifndef TARGET_X86_64
-    if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
-        error_setg(errp, "Windows dump is only available for x86-64");
+    if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
+        && !win_dump_available(errp)) {
         return;
     }
-#endif
 
 #if !defined(WIN32)
     if (strstart(file, "fd:", &p)) {
@@ -2215,10 +2210,9 @@ DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
     QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
 #endif
 
-    /* Windows dump is available only if target is x86_64 */
-#ifdef TARGET_X86_64
-    QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
-#endif
+    if (win_dump_available(NULL)) {
+        QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
+    }
 
     return cap;
 }
dump/meson.build +1 -1
index 2eff29c3ea..f13b29a849 100644
@@ -1,4 +1,4 @@
 softmmu_ss.add(files('dump-hmp-cmds.c'))
 
 specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files('dump.c'), snappy, lzo])
-specific_ss.add(when: ['CONFIG_SOFTMMU', 'TARGET_X86_64'], if_true: files('win_dump.c'))
+specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: files('win_dump.c'))
dump/win_dump.c +18 -0
index ba7fa404fe..ff9c5bd339 100644
@@ -18,6 +18,13 @@
 #include "win_dump.h"
 #include "cpu.h"
 
+#if defined(TARGET_X86_64)
+
+bool win_dump_available(Error **errp)
+{
+    return true;
+}
+
 static size_t win_dump_ptr_size(bool x64)
 {
     return x64 ? sizeof(uint64_t) : sizeof(uint32_t);
@@ -470,3 +477,14 @@ out_cr3:
 
     return;
 }
+
+#else /* !TARGET_X86_64 */
+
+bool win_dump_available(Error **errp)
+{
+    error_setg(errp, "Windows dump is only available for x86-64");
+
+    return false;
+}
+
+#endif
dump/win_dump.h +3 -0
index 56f63683c3..c9b49f87dc 100644
@@ -13,6 +13,9 @@
 
 #include "sysemu/dump.h"
 
+/* Check Windows dump availability for the current target */
+bool win_dump_available(Error **errp);
+
 void create_win_dump(DumpState *s, Error **errp);
 
 #endif /* WIN_DUMP_H */
Signature · 1 line
2.38.1
#5 lore raw

Re: [PATCH v5 4/5] dump: Introduce win_dump_available()

Quoted 3 lines · Philippe Mathieu-Daudé

Remove a pair of TARGET_X86_64 #ifdef'ry by introducing the win_dump_available() method. Doing so we can build win_dump.c on any target, and simplify the meson rule.

Nit: The commit description is a little bit misleading now since you introduce the #ifdef in win_dump.c instead. Maybe rather something like:

To make dump.c less target dependent, move the TARGET_X86_64 #ifdef'ry from dump.c to win_dump.c (and introduce a win_dump_available() method there). By doing so we can build win_dump.c on any target, and simplify the meson rule.

Anyway, you can keep my Reviewed-by here.

Thomas

#6 lore raw

PATCH v5 5/5 dump: Add create_win_dump() stub for non-x86 targets

In reply to Philippe Mathieu-Daudé

Implement the non-x86 create_win_dump(). We can remove the last TARGET_X86_64 #ifdef'ry in dump.c, which thus becomes target-independent. Update meson accordingly.

---
 dump/dump.c      | 2 --
 dump/meson.build | 2 +-
 dump/win_dump.c  | 5 +++++
 3 files changed, 6 insertions(+), 3 deletions(-)
dump/dump.c +0 -2
index 0c96c6e735..7260558852 100644
@@ -2020,9 +2020,7 @@ static void dump_process(DumpState *s, Error **errp)
     DumpQueryResult *result = NULL;
 
     if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
-#ifdef TARGET_X86_64
         create_win_dump(s, errp);
-#endif
     } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
         create_kdump_vmcore(s, errp);
     } else {
dump/meson.build +1 -1
index f13b29a849..7b116f1bd7 100644
@@ -1,4 +1,4 @@
 softmmu_ss.add(files('dump-hmp-cmds.c'))
 
-specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files('dump.c'), snappy, lzo])
+softmmu_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files('dump.c'), snappy, lzo])
 specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: files('win_dump.c'))
dump/win_dump.c +5 -0
index ff9c5bd339..0152f7330a 100644
@@ -487,4 +487,9 @@ bool win_dump_available(Error **errp)
     return false;
 }
 
+void create_win_dump(DumpState *s, Error **errp)
+{
+    win_dump_available(errp);
+}
+
 #endif
Signature · 1 line
2.38.1
#7 lore raw

Re: [PATCH v5 5/5] dump: Add create_win_dump() stub for non-x86 targets

Quoted 42 lines · Philippe Mathieu-Daudé

Implement the non-x86 create_win_dump(). We can remove the last TARGET_X86_64 #ifdef'ry in dump.c, which thus becomes target-independent. Update meson accordingly.

---
  dump/dump.c      | 2 --
  dump/meson.build | 2 +-
  dump/win_dump.c  | 5 +++++
  3 files changed, 6 insertions(+), 3 deletions(-)
dump/dump.c +0 -2
index 0c96c6e735..7260558852 100644
@@ -2020,9 +2020,7 @@ static void dump_process(DumpState *s, Error **errp)
      DumpQueryResult *result = NULL;
 
      if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
-#ifdef TARGET_X86_64
          create_win_dump(s, errp);
-#endif
      } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
          create_kdump_vmcore(s, errp);
      } else {
dump/meson.build +1 -1
index f13b29a849..7b116f1bd7 100644
@@ -1,4 +1,4 @@
  softmmu_ss.add(files('dump-hmp-cmds.c'))
 
-specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files('dump.c'), snappy, lzo])
+softmmu_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files('dump.c'), snappy, lzo])
  specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: files('win_dump.c'))
dump/win_dump.c +0 -0
index ff9c5bd339..0152f7330a 100644

dump/win_dump.c bool win_dump_available(Error **errp) · line 495

Quoted 9 lines
dump/win_dump.c +5 -0
@@ -487,4 +487,9 @@ bool win_dump_available(Error **errp)
      return false;
  }
 
+void create_win_dump(DumpState *s, Error **errp)
+{
+    win_dump_available(errp);
+}
+
  #endif

Looks good now, indeed! Thanks for tackling this!

Thomas

#8 lore raw

Re: [PATCH v5 5/5] dump: Add create_win_dump() stub for non-x86 targets

In reply to Philippe Mathieu-Daudé

dump/meson.build line 3

Quoted 2 lines · Philippe Mathieu-Daudé
dump/meson.build +1 -1
-specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files('dump.c'), snappy, lzo])
+softmmu_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files('dump.c'), snappy, lzo])

You can remove the when: as well, yes?

r~

#9 lore raw

Re: [PATCH v5 5/5] dump: Add create_win_dump() stub for non-x86 targets

Quoted 7 lines · Richard Henderson
Quoted 4 lines · Philippe Mathieu-Daudé

-specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files('dump.c'), snappy, lzo])
+softmmu_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files('dump.c'), snappy, lzo])

You can remove the when: as well, yes?

Good point!