Browse Source

hw: Centralize handling of -machine dumpdtb option

Currently we handle the 'dumpdtb' machine sub-option ad-hoc in every
board model that has an FDT.  It's up to the board code to make sure
it calls qemu_fdt_dumpdtb() in the right place.

This means we're inconsistent and often just ignore the user's
command line argument:
 * if the board doesn't have an FDT at all
 * if the board supports FDT, but there happens not to be one
   present (usually because of a missing -fdt option)

This isn't very helpful because it gives the user no clue why their
option was ignored.

However, in order to support the QMP/HMP dumpdtb commands we require
now that every FDT machine stores a pointer to the FDT in
MachineState::fdt.  This means we can handle -machine dumpdtb
centrally by calling the qmp_dumpdtb() function, unifying its
handling with the QMP/HMP commands.  All the board code calls to
qemu_fdt_dumpdtb() can then be removed.

For this commit we retain the existing behaviour that if there
is no FDT we silently ignore the -machine dumpdtb option.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Peter Maydell 6 months ago
parent
commit
8fd2518ef2

+ 0 - 2
hw/arm/boot.c

@@ -661,8 +661,6 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
         binfo->modify_dtb(binfo, fdt);
         binfo->modify_dtb(binfo, fdt);
     }
     }
 
 
-    qemu_fdt_dumpdtb(fdt, size);
-
     /* Put the DTB into the memory map as a ROM image: this will ensure
     /* Put the DTB into the memory map as a ROM image: this will ensure
      * the DTB is copied again upon reset, even if addr points into RAM.
      * the DTB is copied again upon reset, even if addr points into RAM.
      */
      */

+ 25 - 0
hw/core/machine.c

@@ -19,6 +19,7 @@
 #include "qemu/error-report.h"
 #include "qemu/error-report.h"
 #include "qapi/error.h"
 #include "qapi/error.h"
 #include "qapi/qapi-visit-machine.h"
 #include "qapi/qapi-visit-machine.h"
+#include "qapi/qapi-commands-machine.h"
 #include "qemu/madvise.h"
 #include "qemu/madvise.h"
 #include "qom/object_interfaces.h"
 #include "qom/object_interfaces.h"
 #include "system/cpus.h"
 #include "system/cpus.h"
@@ -1696,6 +1697,24 @@ void qemu_remove_machine_init_done_notifier(Notifier *notify)
     notifier_remove(notify);
     notifier_remove(notify);
 }
 }
 
 
+static void handle_machine_dumpdtb(MachineState *ms)
+{
+    if (!ms->dumpdtb) {
+        return;
+    }
+    if (!ms->fdt) {
+        /* Silently ignore dumpdtb option if there is nothing to dump */
+        return;
+    }
+#ifdef CONFIG_FDT
+    qmp_dumpdtb(ms->dumpdtb, &error_fatal);
+    exit(0);
+#else
+    error_report("This machine doesn't have an FDT");
+    exit(1);
+#endif
+}
+
 void qdev_machine_creation_done(void)
 void qdev_machine_creation_done(void)
 {
 {
     cpu_synchronize_all_post_init();
     cpu_synchronize_all_post_init();
@@ -1712,6 +1731,12 @@ void qdev_machine_creation_done(void)
     phase_advance(PHASE_MACHINE_READY);
     phase_advance(PHASE_MACHINE_READY);
     qdev_assert_realized_properly();
     qdev_assert_realized_properly();
 
 
+    /*
+     * If the user used -machine dumpdtb=file.dtb to request that we
+     * dump the DTB to a file,  do it now, and exit.
+     */
+    handle_machine_dumpdtb(current_machine);
+
     /* TODO: once all bus devices are qdevified, this should be done
     /* TODO: once all bus devices are qdevified, this should be done
      * when bus is created by qdev.c */
      * when bus is created by qdev.c */
     /*
     /*

+ 0 - 1
hw/loongarch/virt-fdt-build.c

@@ -527,7 +527,6 @@ void virt_fdt_setup(LoongArchVirtMachineState *lvms)
      * Put the FDT into the memory map as a ROM image: this will ensure
      * Put the FDT into the memory map as a ROM image: this will ensure
      * the FDT is copied again upon reset, even if addr points into RAM.
      * the FDT is copied again upon reset, even if addr points into RAM.
      */
      */
-    qemu_fdt_dumpdtb(machine->fdt, lvms->fdt_size);
     rom_add_blob_fixed_as("fdt", machine->fdt, lvms->fdt_size, FDT_BASE,
     rom_add_blob_fixed_as("fdt", machine->fdt, lvms->fdt_size, FDT_BASE,
                           &address_space_memory);
                           &address_space_memory);
     qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,
     qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,

+ 0 - 1
hw/mips/boston.c

@@ -395,7 +395,6 @@ static void *boston_fdt_filter(void *opaque, const void *fdt_orig,
                         1, ram_high_sz);
                         1, ram_high_sz);
 
 
     fdt = g_realloc(fdt, fdt_totalsize(fdt));
     fdt = g_realloc(fdt, fdt_totalsize(fdt));
-    qemu_fdt_dumpdtb(fdt, fdt_sz);
 
 
     s->fdt_base = *load_addr;
     s->fdt_base = *load_addr;
 
 

+ 0 - 1
hw/openrisc/boot.c

@@ -109,7 +109,6 @@ uint32_t openrisc_load_fdt(MachineState *ms, void *fdt,
     /* Should only fail if we've built a corrupted tree */
     /* Should only fail if we've built a corrupted tree */
     g_assert(ret == 0);
     g_assert(ret == 0);
     /* copy in the device tree */
     /* copy in the device tree */
-    qemu_fdt_dumpdtb(fdt, fdtsize);
 
 
     /* Save FDT for dumpdtb monitor command */
     /* Save FDT for dumpdtb monitor command */
     ms->fdt = fdt;
     ms->fdt = fdt;

+ 0 - 1
hw/ppc/e500.c

@@ -658,7 +658,6 @@ static int ppce500_load_device_tree(PPCE500MachineState *pms,
 
 
 done:
 done:
     if (!dry_run) {
     if (!dry_run) {
-        qemu_fdt_dumpdtb(fdt, fdt_size);
         cpu_physical_memory_write(addr, fdt, fdt_size);
         cpu_physical_memory_write(addr, fdt, fdt_size);
 
 
         /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
         /* Set machine->fdt for 'dumpdtb' QMP/HMP command */

+ 0 - 1
hw/ppc/pegasos2.c

@@ -417,7 +417,6 @@ static void pegasos2_machine_reset(MachineState *machine, ResetType type)
     d[1] = cpu_to_be64(pm->kernel_size - (pm->kernel_entry - pm->kernel_addr));
     d[1] = cpu_to_be64(pm->kernel_size - (pm->kernel_entry - pm->kernel_addr));
     qemu_fdt_setprop(fdt, "/chosen", "qemu,boot-kernel", d, sizeof(d));
     qemu_fdt_setprop(fdt, "/chosen", "qemu,boot-kernel", d, sizeof(d));
 
 
-    qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
     g_free(pm->fdt_blob);
     g_free(pm->fdt_blob);
     pm->fdt_blob = fdt;
     pm->fdt_blob = fdt;
 
 

+ 0 - 1
hw/ppc/pnv.c

@@ -744,7 +744,6 @@ static void pnv_reset(MachineState *machine, ResetType type)
         _FDT((fdt_pack(fdt)));
         _FDT((fdt_pack(fdt)));
     }
     }
 
 
-    qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
     cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
     cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
 
 
     /* Update machine->fdt with latest fdt */
     /* Update machine->fdt with latest fdt */

+ 0 - 1
hw/ppc/spapr.c

@@ -1760,7 +1760,6 @@ static void spapr_machine_reset(MachineState *machine, ResetType type)
                                   0, fdt_addr, 0);
                                   0, fdt_addr, 0);
         cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
         cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
     }
     }
-    qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
 
 
     g_free(spapr->fdt_blob);
     g_free(spapr->fdt_blob);
     spapr->fdt_size = fdt_totalsize(fdt);
     spapr->fdt_size = fdt_totalsize(fdt);

+ 0 - 2
hw/riscv/boot.c

@@ -374,8 +374,6 @@ void riscv_load_fdt(hwaddr fdt_addr, void *fdt)
     uint32_t fdtsize = fdt_totalsize(fdt);
     uint32_t fdtsize = fdt_totalsize(fdt);
 
 
     /* copy in the device tree */
     /* copy in the device tree */
-    qemu_fdt_dumpdtb(fdt, fdtsize);
-
     rom_add_blob_fixed_as("fdt", fdt, fdtsize, fdt_addr,
     rom_add_blob_fixed_as("fdt", fdt, fdtsize, fdt_addr,
                           &address_space_memory);
                           &address_space_memory);
     qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,
     qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,

+ 0 - 2
include/system/device_tree.h

@@ -133,8 +133,6 @@ int qemu_fdt_add_path(void *fdt, const char *path);
                          sizeof(qdt_tmp));                                    \
                          sizeof(qdt_tmp));                                    \
     } while (0)
     } while (0)
 
 
-void qemu_fdt_dumpdtb(void *fdt, int size);
-
 /**
 /**
  * qemu_fdt_setprop_sized_cells_from_array:
  * qemu_fdt_setprop_sized_cells_from_array:
  * @fdt: device tree blob
  * @fdt: device tree blob

+ 0 - 15
system/device_tree.c

@@ -594,21 +594,6 @@ int qemu_fdt_add_path(void *fdt, const char *path)
     return retval;
     return retval;
 }
 }
 
 
-void qemu_fdt_dumpdtb(void *fdt, int size)
-{
-    const char *dumpdtb = current_machine->dumpdtb;
-
-    if (dumpdtb) {
-        /* Dump the dtb to a file and quit */
-        if (g_file_set_contents(dumpdtb, fdt, size, NULL)) {
-            info_report("dtb dumped to %s. Exiting.", dumpdtb);
-            exit(0);
-        }
-        error_report("%s: Failed dumping dtb to %s", __func__, dumpdtb);
-        exit(1);
-    }
-}
-
 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
                                             const char *node_path,
                                             const char *node_path,
                                             const char *property,
                                             const char *property,