Skip to contentSkip to conversation

QMP/HMP: introduce 'dumpdtb'

PATCH Superseded Daniel Henrique Barboza 6 participants 22 messages Sep 4, 2022 – Sep 8, 2022

Subsystems (3)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#1 lore raw

PATCH v6 1/14 hw/arm: do not free machine->fdt in arm_load_dtb()

In reply to Daniel Henrique Barboza

At this moment, arm_load_dtb() can free machine->fdt when binfo->dtb_filename is NULL. If there's no 'dtb_filename', 'fdt' will be retrieved by binfo->get_dtb(). If get_dtb() returns machine->fdt, as is the case of machvirt_dtb() from hw/arm/virt.c, fdt now has a pointer to machine->fdt. And, in that case, the existing g_free(fdt) at the end of arm_load_dtb() will make machine->fdt point to an invalid memory region.

This is not an issue right now because there's no code that access machine->fdt after arm_load_dtb(), but we're going to add a QMP/HMP FDT command that will rely on machine->fdt being valid.

Instead of freeing 'fdt' at the end of arm_load_dtb(), assign it to machine->fdt. This will allow the FDT of ARM machines that relies on arm_load_dtb() to be accessed later on.

Since all ARM machines allocates the FDT only once, we don't need to worry about leaking the existing FDT during a machine reset (which is something that other machines have to look after, e.g. the ppc64 pSeries machine).

---
 hw/arm/boot.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/arm/boot.c +2 -1
index ada2717f76..60bbfba37f 100644
@@ -684,7 +684,8 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
      */
     rom_add_blob_fixed_as("dtb", fdt, size, addr, as);
 
-    g_free(fdt);
+    /* Set ms->fdt for 'dumpdtb' QMP/HMP command */
+    ms->fdt = fdt;
 
     return size;
 
Signature · 1 line
2.37.2
#0 lore raw

v6 cover QMP/HMP: introduce 'dumpdtb'

Hi,

This new version contains changes suggested by Zoltan in the v5 review.

Changes from v5:
- shortened the 'machine->fdt' comment in all relevant patches
- patch 6:
- changed sam460ex_load_device_tree() to receive a MachineState pointer
- v5 link: https://lists.gnu.org/archive/html/qemu-devel/2022-09/msg00476.html

Daniel Henrique Barboza (14):
hw/arm: do not free machine->fdt in arm_load_dtb() hw/microblaze: set machine->fdt in microblaze_load_dtb() hw/nios2: set machine->fdt in nios2_load_dtb() hw/ppc: set machine->fdt in ppce500_load_device_tree() hw/ppc: set machine->fdt in bamboo_load_device_tree() hw/ppc: set machine->fdt in sam460ex_load_device_tree() hw/ppc: set machine->fdt in xilinx_load_device_tree() hw/ppc: set machine->fdt in pegasos2_machine_reset() hw/ppc: set machine->fdt in pnv_reset()
hw/ppc: set machine->fdt in spapr machine hw/riscv: set machine->fdt in sifive_u_machine_init() hw/riscv: set machine->fdt in spike_board_init() hw/xtensa: set machine->fdt in xtfpga_init() qmp/hmp, device_tree.c: introduce dumpdtb

 hmp-commands.hx              | 15 +++++++++++++++
 hw/arm/boot.c                |  3 ++-
 hw/microblaze/boot.c         |  8 +++++++-
 hw/microblaze/meson.build    |  2 +-
 hw/nios2/boot.c              |  8 +++++++-
 hw/nios2/meson.build         |  2 +-
 hw/ppc/e500.c                | 13 ++++++++++++-
 hw/ppc/pegasos2.c            |  4 ++++
 hw/ppc/pnv.c                 |  8 +++++++-
 hw/ppc/ppc440_bamboo.c       | 11 ++++++++++-
 hw/ppc/sam460ex.c            | 21 +++++++++++----------
 hw/ppc/spapr.c               |  3 +++
 hw/ppc/spapr_hcall.c         |  8 ++++++++
 hw/ppc/virtex_ml507.c        |  8 +++++++-
 hw/riscv/sifive_u.c          |  3 +++
 hw/riscv/spike.c             |  6 ++++++
 hw/xtensa/meson.build        |  2 +-
 hw/xtensa/xtfpga.c           |  6 +++++-
 include/sysemu/device_tree.h |  1 +
 monitor/misc.c               |  1 +
 qapi/machine.json            | 18 ++++++++++++++++++
 softmmu/device_tree.c        | 31 +++++++++++++++++++++++++++++++
 22 files changed, 161 insertions(+), 21 deletions(-)
Signature · 1 line
2.37.2
#11 lore raw

PATCH v6 9/14 hw/ppc: set machine->fdt in pnv_reset()

This will enable support for the 'dumpdtb' QMP/HMP command for all powernv machines.

Cc Cédric Le Goater <[email protected]>
Cc Frederic Barrat <[email protected]>
---
 hw/ppc/pnv.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/pnv.c +7 -1
index 354aa289d1..6a20c4811f 100644
@@ -678,7 +678,13 @@ static void pnv_reset(MachineState *machine)
     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
     cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
 
-    g_free(fdt);
+    /*
+     * Set machine->fdt for 'dumpdtb' QMP/HMP command. Free
+     * the existing machine->fdt to avoid leaking it during
+     * a reset.
+     */
+    g_free(machine->fdt);
+    machine->fdt = fdt;
 }
 
 static ISABus *pnv_chip_power8_isa_create(PnvChip *chip, Error **errp)
Signature · 1 line
2.37.2
#2 lore raw

PATCH v6 2/14 hw/microblaze: set machine->fdt in microblaze_load_dtb()

In reply to Daniel Henrique Barboza

This will enable support for 'dumpdtb' QMP/HMP command for all microblaze machines that uses microblaze_load_dtb().

---
 hw/microblaze/boot.c      | 8 +++++++-
 hw/microblaze/meson.build | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)
hw/microblaze/boot.c +7 -1
index 8b92a9801a..c8eff7b6fc 100644
@@ -39,6 +39,8 @@
 
 #include "boot.h"
 
+#include <libfdt.h>
+
 static struct
 {
     void (*machine_cpu_reset)(MicroBlazeCPU *);
@@ -72,6 +74,7 @@ static int microblaze_load_dtb(hwaddr addr,
                                const char *kernel_cmdline,
                                const char *dtb_filename)
 {
+    MachineState *machine = MACHINE(qdev_get_machine());
     int fdt_size;
     void *fdt = NULL;
     int r;
@@ -100,7 +103,10 @@ static int microblaze_load_dtb(hwaddr addr,
     }
 
     cpu_physical_memory_write(addr, fdt, fdt_size);
-    g_free(fdt);
+
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = fdt;
+
     return fdt_size;
 }
 
hw/microblaze/meson.build +1 -1
index bb9e4eb8f4..a38a397872 100644
@@ -1,5 +1,5 @@
 microblaze_ss = ss.source_set()
-microblaze_ss.add(files('boot.c'))
+microblaze_ss.add(files('boot.c'), fdt)
 microblaze_ss.add(when: 'CONFIG_PETALOGIX_S3ADSP1800', if_true: files('petalogix_s3adsp1800_mmu.c'))
 microblaze_ss.add(when: 'CONFIG_PETALOGIX_ML605', if_true: files('petalogix_ml605_mmu.c'))
 microblaze_ss.add(when: 'CONFIG_XLNX_ZYNQMP_PMU', if_true: files('xlnx-zynqmp-pmu.c'))
Signature · 1 line
2.37.2
#4 lore raw

PATCH v6 4/14 hw/ppc: set machine->fdt in ppce500_load_device_tree()

In reply to Daniel Henrique Barboza

This will enable support for 'dumpdtb' QMP/HMP command for the e500 machine.

---
 hw/ppc/e500.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/ppc/e500.c +12 -1
index 32495d0123..ea5f947824 100644
@@ -47,6 +47,8 @@
 #include "hw/i2c/i2c.h"
 #include "hw/irq.h"
 
+#include <libfdt.h>
+
 #define EPAPR_MAGIC                (0x45504150)
 #define DTC_LOAD_PAD               0x1800000
 #define DTC_PAD_MASK               0xFFFFF
@@ -600,7 +602,16 @@ done:
         cpu_physical_memory_write(addr, fdt, fdt_size);
     }
     ret = fdt_size;
-    g_free(fdt);
+
+    /*
+     * Update the machine->fdt pointer to enable support for the
+     * 'dumpdtb' QMP/HMP command.
+     *
+     * The FDT is re-created during reset, so free machine->fdt
+     * to avoid leaking the old FDT.
+     */
+    g_free(machine->fdt);
+    machine->fdt = fdt;
 
 out:
     g_free(pci_map);
Signature · 1 line
2.37.2
#3 lore raw

PATCH v6 3/14 hw/nios2: set machine->fdt in nios2_load_dtb()

In reply to Daniel Henrique Barboza

This will enable support for 'dumpdtb' QMP/HMP command for all nios2 machines that uses nios2_load_dtb().

---
 hw/nios2/boot.c      | 8 +++++++-
 hw/nios2/meson.build | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)
hw/nios2/boot.c +7 -1
index 21cbffff47..b30a7b1efb 100644
@@ -43,6 +43,8 @@
 
 #include "boot.h"
 
+#include <libfdt.h>
+
 #define NIOS2_MAGIC    0x534f494e
 
 static struct nios2_boot_info {
@@ -81,6 +83,7 @@ static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
 static int nios2_load_dtb(struct nios2_boot_info bi, const uint32_t ramsize,
                           const char *kernel_cmdline, const char *dtb_filename)
 {
+    MachineState *machine = MACHINE(qdev_get_machine());
     int fdt_size;
     void *fdt = NULL;
     int r;
@@ -113,7 +116,10 @@ static int nios2_load_dtb(struct nios2_boot_info bi, const uint32_t ramsize,
     }
 
     cpu_physical_memory_write(bi.fdt, fdt, fdt_size);
-    g_free(fdt);
+
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = fdt;
+
     return fdt_size;
 }
 
hw/nios2/meson.build +1 -1
index 6c58e8082b..22277bd6c5 100644
@@ -1,5 +1,5 @@
 nios2_ss = ss.source_set()
-nios2_ss.add(files('boot.c'))
+nios2_ss.add(files('boot.c'), fdt)
 nios2_ss.add(when: 'CONFIG_NIOS2_10M50', if_true: files('10m50_devboard.c'))
 nios2_ss.add(when: 'CONFIG_NIOS2_GENERIC_NOMMU', if_true: files('generic_nommu.c'))
 
Signature · 1 line
2.37.2
#15 lore raw

PATCH v6 11/14 hw/riscv: set machine->fdt in sifive_u_machine_init()

In reply to Daniel Henrique Barboza

This will enable support for 'dumpdtb' QMP/HMP command for the sifive_u machine.

---
 hw/riscv/sifive_u.c | 3 +++
 1 file changed, 3 insertions(+)
hw/riscv/sifive_u.c +3 -0
index e4c814a3ea..b139824aab 100644
@@ -634,6 +634,9 @@ static void sifive_u_machine_init(MachineState *machine)
         start_addr_hi32 = (uint64_t)start_addr >> 32;
     }
 
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = s->fdt;
+
     /* reset vector */
     uint32_t reset_vec[12] = {
         s->msel,                       /* MSEL pin state */
Signature · 1 line
2.37.2
#14 lore raw

PATCH v6 10/14 hw/ppc: set machine->fdt in spapr machine

In reply to Daniel Henrique Barboza

The pSeries machine never bothered with the common machine->fdt attribute. We do all the FDT related work using spapr->fdt_blob.

We're going to introduce a QMP/HMP command to dump the FDT, which will rely on setting machine->fdt properly to work across all machine archs/types.

Let's set machine->fdt in two places where we manipulate the FDT:
spapr_machine_reset() and CAS. There are other places where the FDT is manipulated in the pSeries machines, most notably the hotplug/unplug path. For now we'll acknowledge that we won't have the most accurate representation of the FDT, depending on the current machine state, when using this QMP/HMP fdt command. Making the internal FDT representation always match the actual FDT representation that the guest is using is a problem for another day.

spapr->fdt_blob is left untouched for now. To replace it with machine->fdt, since we're migrating spapr->fdt_blob, we would need to migrate machine->fdt as well. This is something that we would like to to do keep our code simpler but it's also a work we'll leave for later.

---
 hw/ppc/spapr.c       | 3 +++
 hw/ppc/spapr_hcall.c | 8 ++++++++
 2 files changed, 11 insertions(+)
hw/ppc/spapr.c +3 -0
index fb790b61e4..170bbfd199 100644
@@ -1713,6 +1713,9 @@ static void spapr_machine_reset(MachineState *machine)
     spapr->fdt_initial_size = spapr->fdt_size;
     spapr->fdt_blob = fdt;
 
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = fdt;
+
     /* Set up the entry state */
     first_ppc_cpu->env.gpr[5] = 0;
 
hw/ppc/spapr_hcall.c +8 -0
index a8d4a6bcf0..891206e893 100644
@@ -1256,6 +1256,14 @@ target_ulong do_client_architecture_support(PowerPCCPU *cpu,
     spapr->fdt_initial_size = spapr->fdt_size;
     spapr->fdt_blob = fdt;
 
+    /*
+     * Set the machine->fdt pointer again since we just freed
+     * it above (by freeing spapr->fdt_blob). We set this
+     * pointer to enable support for the 'dumpdtb' QMP/HMP
+     * command.
+     */
+    MACHINE(spapr)->fdt = fdt;
+
     return H_SUCCESS;
 }
 
Signature · 1 line
2.37.2
#5 lore raw

PATCH v6 5/14 hw/ppc: set machine->fdt in bamboo_load_device_tree()

In reply to Daniel Henrique Barboza

This will enable support for 'dumpdtb' QMP/HMP command for the bamboo machine.

---
 hw/ppc/ppc440_bamboo.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
hw/ppc/ppc440_bamboo.c +10 -1
index ea945a1c99..aa5c901b09 100644
@@ -34,6 +34,8 @@
 #include "hw/qdev-properties.h"
 #include "qapi/error.h"
 
+#include <libfdt.h>
+
 #define BINARY_DEVICE_TREE_FILE "bamboo.dtb"
 
 /* from u-boot */
@@ -62,6 +64,7 @@ static int bamboo_load_device_tree(hwaddr addr,
                                      hwaddr initrd_size,
                                      const char *kernel_cmdline)
 {
+    MachineState *machine = MACHINE(qdev_get_machine());
     int ret = -1;
     uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(ramsize) };
     char *filename;
@@ -119,7 +122,13 @@ static int bamboo_load_device_tree(hwaddr addr,
                           tb_freq);
 
     rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
-    g_free(fdt);
+
+    /*
+     * Update the machine->fdt pointer to enable support for
+     * 'dumpdtb' QMP/HMP command.
+     */
+    machine->fdt = fdt;
+
     return 0;
 }
 
Signature · 1 line
2.37.2
#10 lore raw

PATCH v6 8/14 hw/ppc: set machine->fdt in pegasos2_machine_reset()

In reply to Daniel Henrique Barboza

We'll introduce a QMP/HMP command that requires machine->fdt to be set properly.

---
 hw/ppc/pegasos2.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/ppc/pegasos2.c +4 -0
index 61f4263953..ecf682b148 100644
@@ -331,6 +331,10 @@ static void pegasos2_machine_reset(MachineState *machine)
 
     vof_build_dt(fdt, pm->vof);
     vof_client_open_store(fdt, pm->vof, "/chosen", "stdout", "/failsafe");
+
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = fdt;
+
     pm->cpu->vhyp = PPC_VIRTUAL_HYPERVISOR(machine);
 }
 
Signature · 1 line
2.37.2
#16 lore raw

PATCH v6 12/14 hw/riscv: set machine->fdt in spike_board_init()

In reply to Daniel Henrique Barboza

This will enable support for the 'dumpdtb' QMP/HMP command for the spike machine.

---
 hw/riscv/spike.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/riscv/spike.c +6 -0
index e41b6aa9f0..a980e568a4 100644
@@ -40,6 +40,8 @@
 #include "sysemu/device_tree.h"
 #include "sysemu/sysemu.h"
 
+#include <libfdt.h>
+
 static const MemMapEntry spike_memmap[] = {
     [SPIKE_MROM] =     {     0x1000,     0xf000 },
     [SPIKE_HTIF] =     {  0x1000000,     0x1000 },
@@ -304,6 +306,10 @@ static void spike_board_init(MachineState *machine)
     /* Compute the fdt load address in dram */
     fdt_load_addr = riscv_load_fdt(memmap[SPIKE_DRAM].base,
                                    machine->ram_size, s->fdt);
+
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = s->fdt;
+
     /* load the reset vector */
     riscv_setup_rom_reset_vec(machine, &s->soc[0], memmap[SPIKE_DRAM].base,
                               memmap[SPIKE_MROM].base,
Signature · 1 line
2.37.2
#6 lore raw

PATCH v6 6/14 hw/ppc: set machine->fdt in sam460ex_load_device_tree()

In reply to Daniel Henrique Barboza

This will enable support for 'dumpdtb' QMP/HMP command for the sam460ex machine.

Setting machine->fdt requires a MachineState pointer to be used inside sam460ex_load_device_tree(). Let's change the function to receive this pointer from the caller. 'ramsize' and 'kernel_cmdline' can be retrieved directly from the 'machine' pointer.

---
 hw/ppc/sam460ex.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
hw/ppc/sam460ex.c +11 -10
index 850bb3b817..5d09d3c6ab 100644
@@ -131,13 +131,12 @@ static int sam460ex_load_uboot(void)
     return 0;
 }
 
-static int sam460ex_load_device_tree(hwaddr addr,
-                                     uint32_t ramsize,
+static int sam460ex_load_device_tree(MachineState *machine,
+                                     hwaddr addr,
                                      hwaddr initrd_base,
-                                     hwaddr initrd_size,
-                                     const char *kernel_cmdline)
+                                     hwaddr initrd_size)
 {
-    uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(ramsize) };
+    uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(machine->ram_size) };
     char *filename;
     int fdt_size;
     void *fdt;
@@ -171,7 +170,8 @@ static int sam460ex_load_device_tree(hwaddr addr,
     qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
                           (initrd_base + initrd_size));
 
-    qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", kernel_cmdline);
+    qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
+                            machine->kernel_cmdline);
 
     /* Copy data from the host device tree into the guest. Since the guest can
      * directly access the timebase without host involvement, we must expose
@@ -208,7 +208,9 @@ static int sam460ex_load_device_tree(hwaddr addr,
                               EBC_FREQ);
 
     rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
-    g_free(fdt);
+
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = fdt;
 
     return fdt_size;
 }
@@ -496,9 +498,8 @@ static void sam460ex_init(MachineState *machine)
     if (machine->kernel_filename) {
         int dt_size;
 
-        dt_size = sam460ex_load_device_tree(FDT_ADDR, machine->ram_size,
-                                    RAMDISK_ADDR, initrd_size,
-                                    machine->kernel_cmdline);
+        dt_size = sam460ex_load_device_tree(machine, FDT_ADDR,
+                                            RAMDISK_ADDR, initrd_size);
 
         boot_info->dt_base = FDT_ADDR;
         boot_info->dt_size = dt_size;
Signature · 1 line
2.37.2
#17 lore raw

PATCH v6 13/14 hw/xtensa: set machine->fdt in xtfpga_init()

In reply to Daniel Henrique Barboza

This will enable support for the 'dumpdtb' QMP/HMP command for all xtensa machines that uses a FDT.

---
 hw/xtensa/meson.build | 2 +-
 hw/xtensa/xtfpga.c    | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
hw/xtensa/meson.build +1 -1
index 1d5835df4b..ebba51cc74 100644
@@ -6,6 +6,6 @@ xtensa_ss.add(files(
 ))
 xtensa_ss.add(when: 'CONFIG_XTENSA_SIM', if_true: files('sim.c'))
 xtensa_ss.add(when: 'CONFIG_XTENSA_VIRT', if_true: files('virt.c'))
-xtensa_ss.add(when: 'CONFIG_XTENSA_XTFPGA', if_true: files('xtfpga.c'))
+xtensa_ss.add(when: 'CONFIG_XTENSA_XTFPGA', if_true: [files('xtfpga.c'), fdt])
 
 hw_arch += {'xtensa': xtensa_ss}
hw/xtensa/xtfpga.c +5 -1
index 2a5556a35f..867427c3d9 100644
@@ -50,6 +50,8 @@
 #include "hw/xtensa/mx_pic.h"
 #include "migration/vmstate.h"
 
+#include <libfdt.h>
+
 typedef struct XtfpgaFlashDesc {
     hwaddr base;
     size_t size;
@@ -377,7 +379,9 @@ static void xtfpga_init(const XtfpgaBoardDesc *board, MachineState *machine)
             cur_tagptr = put_tag(cur_tagptr, BP_TAG_FDT,
                                  sizeof(dtb_addr), &dtb_addr);
             cur_lowmem = QEMU_ALIGN_UP(cur_lowmem + fdt_size, 4 * KiB);
-            g_free(fdt);
+
+            /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+            machine->fdt = fdt;
         }
 #else
         if (dtb_filename) {
Signature · 1 line
2.37.2
#9 lore raw

PATCH v6 7/14 hw/ppc: set machine->fdt in xilinx_load_device_tree()

In reply to Daniel Henrique Barboza

This will enable support for 'dumpdtb' QMP/HMP command for the virtex_ml507 machine.

---
 hw/ppc/virtex_ml507.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/virtex_ml507.c +7 -1
index 493ea0c19f..d30f186979 100644
@@ -45,6 +45,8 @@
 #include "hw/qdev-properties.h"
 #include "ppc405.h"
 
+#include <libfdt.h>
+
 #define EPAPR_MAGIC    (0x45504150)
 #define FLASH_SIZE     (16 * MiB)
 
@@ -150,6 +152,7 @@ static int xilinx_load_device_tree(hwaddr addr,
                                       hwaddr initrd_size,
                                       const char *kernel_cmdline)
 {
+    MachineState *machine = MACHINE(qdev_get_machine());
     char *path;
     int fdt_size;
     void *fdt = NULL;
@@ -194,7 +197,10 @@ static int xilinx_load_device_tree(hwaddr addr,
     if (r < 0)
         fprintf(stderr, "couldn't set /chosen/bootargs\n");
     cpu_physical_memory_write(addr, fdt, fdt_size);
-    g_free(fdt);
+
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = fdt;
+
     return fdt_size;
 }
 
Signature · 1 line
2.37.2
#18 lore raw

PATCH v6 14/14 qmp/hmp, device_tree.c: introduce dumpdtb

In reply to Daniel Henrique Barboza

To save the FDT blob we have the '-machine dumpdtb=<file>' property. With this property set, the machine saves the FDT in <file> and exit. The created file can then be converted to plain text dts format using 'dtc'.

There's nothing particularly sophisticated into saving the FDT that can't be done with the machine at any state, as long as the machine has a valid FDT to be saved.

The 'dumpdtb' command receives a 'filename' paramenter and, if a valid FDT is available, it'll save it in a file 'filename'. In short, this is a '-machine dumpdtb' that can be fired on demand via QMP/HMP.

A valid FDT consists of a FDT that was created using libfdt being retrieved via 'current_machine->fdt' in device_tree.c. This condition is met by most FDT users in QEMU.

This command will always be executed in-band (i.e. holding BQL), avoiding potential race conditions with machines that might change the FDT during runtime (e.g. PowerPC 'pseries' machine).

Cc Dr. David Alan Gilbert <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Alistair Francis <[email protected]>
Cc David Gibson <[email protected]>
---
 hmp-commands.hx              | 15 +++++++++++++++
 include/sysemu/device_tree.h |  1 +
 monitor/misc.c               |  1 +
 qapi/machine.json            | 18 ++++++++++++++++++
 softmmu/device_tree.c        | 31 +++++++++++++++++++++++++++++++
 5 files changed, 66 insertions(+)
hmp-commands.hx +15 -0
index 182e639d14..9a3e57504f 100644
@@ -1800,3 +1800,18 @@ ERST
                       "\n\t\t\t\t\t limit on a specified virtual cpu",
         .cmd        = hmp_cancel_vcpu_dirty_limit,
     },
+
+#if defined(CONFIG_FDT)
+SRST
+``dumpdtb`` *filename*
+  Save the FDT in the 'filename' file to be decoded using dtc.
+  Requires 'libfdt' support.
+ERST
+    {
+        .name       = "dumpdtb",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the FDT in the 'filename' file to be decoded using dtc",
+        .cmd        = hmp_dumpdtb,
+    },
+#endif
include/sysemu/device_tree.h +1 -0
index ef060a9759..e7c5441f56 100644
@@ -136,6 +136,7 @@ int qemu_fdt_add_path(void *fdt, const char *path);
     } while (0)
 
 void qemu_fdt_dumpdtb(void *fdt, int size);
+void hmp_dumpdtb(Monitor *mon, const QDict *qdict);
 
 /**
  * qemu_fdt_setprop_sized_cells_from_array:
monitor/misc.c +1 -0
index 3d2312ba8d..e7dd63030b 100644
@@ -49,6 +49,7 @@
 #include "sysemu/blockdev.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/tpm.h"
+#include "sysemu/device_tree.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qerror.h"
 #include "qapi/qmp/qstring.h"
qapi/machine.json +18 -0
index 6afd1936b0..f968a5d343 100644
@@ -1664,3 +1664,21 @@
      '*size': 'size',
      '*max-size': 'size',
      '*slots': 'uint64' } }
+
+##
+# @dumpdtb:
+#
+# Save the FDT in dtb format. Requires 'libfdt' support.
+#
+# @filename: name of the FDT file to be created
+#
+# Since: 7.2
+#
+# Example:
+#   {"execute": "dumpdtb"}
+#    "arguments": { "filename": "/tmp/fdt.dtb" } }
+#
+##
+{ 'command': 'dumpdtb',
+  'data': { 'filename': 'str' },
+  'if': 'CONFIG_FDT' }
softmmu/device_tree.c +31 -0
index 6ca3fad285..cdd41b6de6 100644
@@ -26,6 +26,9 @@
 #include "hw/loader.h"
 #include "hw/boards.h"
 #include "qemu/config-file.h"
+#include "qapi/qapi-commands-machine.h"
+#include "qapi/qmp/qdict.h"
+#include "monitor/hmp.h"
 
 #include <libfdt.h>
 
@@ -643,3 +646,31 @@ out:
     g_free(propcells);
     return ret;
 }
+
+void qmp_dumpdtb(const char *filename, Error **errp)
+{
+    g_autoptr(GError) err = NULL;
+    int size;
+
+    if (!current_machine->fdt) {
+        error_setg(errp, "Unable to find the machine FDT");
+        return;
+    }
+
+    size = fdt_totalsize(current_machine->fdt);
+
+    if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
+        error_setg(errp, "Error saving FDT to file %s: %s",
+                   filename, err->message);
+    }
+}
+
+void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
+{
+    const char *filename = qdict_get_str(qdict, "filename");
+    Error *local_err = NULL;
+
+    qmp_dumpdtb(filename, &local_err);
+
+    hmp_handle_error(mon, local_err);
+}
Signature · 1 line
2.37.2
Reviewed-by on v6 6/14
#7 lore raw

Re: [PATCH v6 06/14] hw/ppc: set machine->fdt in sam460ex_load_device_tree()

In reply to Daniel Henrique Barboza
Quoted 10 lines · Daniel Henrique Barboza

This will enable support for 'dumpdtb' QMP/HMP command for the sam460ex machine.

Setting machine->fdt requires a MachineState pointer to be used inside sam460ex_load_device_tree(). Let's change the function to receive this pointer from the caller. 'ramsize' and 'kernel_cmdline' can be retrieved directly from the 'machine' pointer.

but could you please do the same at least for bamboo and virtex too to keep these consistent? (and to simplify them a little.)

Regards,
BALATON Zoltan

Quoted 60 lines

---
hw/ppc/sam460ex.c | 21 +++++++++++----------

1 file changed, 11 insertions(+), 10 deletions(-)
hw/ppc/sam460ex.c +11 -10
index 850bb3b817..5d09d3c6ab 100644
@@ -131,13 +131,12 @@ static int sam460ex_load_uboot(void)
    return 0;
 }
 
-static int sam460ex_load_device_tree(hwaddr addr,
-                                     uint32_t ramsize,
+static int sam460ex_load_device_tree(MachineState *machine,
+                                     hwaddr addr,
                                     hwaddr initrd_base,
-                                     hwaddr initrd_size,
-                                     const char *kernel_cmdline)
+                                     hwaddr initrd_size)
 {
-    uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(ramsize) };
+    uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(machine->ram_size) };
    char *filename;
    int fdt_size;
    void *fdt;
@@ -171,7 +170,8 @@ static int sam460ex_load_device_tree(hwaddr addr,
    qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
                          (initrd_base + initrd_size));
 
-    qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", kernel_cmdline);
+    qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
+                            machine->kernel_cmdline);
 
    /* Copy data from the host device tree into the guest. Since the guest can
     * directly access the timebase without host involvement, we must expose
@@ -208,7 +208,9 @@ static int sam460ex_load_device_tree(hwaddr addr,
                              EBC_FREQ);
 
    rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
-    g_free(fdt);
+
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = fdt;
 
    return fdt_size;
 }
@@ -496,9 +498,8 @@ static void sam460ex_init(MachineState *machine)
    if (machine->kernel_filename) {
        int dt_size;
 
-        dt_size = sam460ex_load_device_tree(FDT_ADDR, machine->ram_size,
-                                    RAMDISK_ADDR, initrd_size,
-                                    machine->kernel_cmdline);
+        dt_size = sam460ex_load_device_tree(machine, FDT_ADDR,
+                                            RAMDISK_ADDR, initrd_size);
 
        boot_info->dt_base = FDT_ADDR;
        boot_info->dt_size = dt_size;
#8 lore raw

Re: [PATCH v6 06/14] hw/ppc: set machine->fdt in sam460ex_load_device_tree()

Quoted 9 lines · BALATON Zoltan
Quoted 8 lines · Daniel Henrique Barboza

This will enable support for 'dumpdtb' QMP/HMP command for the sam460ex machine.

Setting machine->fdt requires a MachineState pointer to be used inside sam460ex_load_device_tree(). Let's change the function to receive this pointer from the caller. 'ramsize' and 'kernel_cmdline' can be retrieved directly from the 'machine' pointer.

Quoted 6 lines
Quoted 2 lines

but could you please do the same at least for bamboo and virtex too to keep these consistent? (and to simplify them a little.)

I'll wait for more reviews in the other patches then I'll repost with these changes in bamboo and virtex.

Thanks,

Daniel

Quoted 64 lines

Regards,
BALATON Zoltan

Quoted 60 lines

---
hw/ppc/sam460ex.c | 21 +++++++++++----------

1 file changed, 11 insertions(+), 10 deletions(-)
hw/ppc/sam460ex.c +11 -10
index 850bb3b817..5d09d3c6ab 100644
@@ -131,13 +131,12 @@ static int sam460ex_load_uboot(void)
     return 0;
 }
 
-static int sam460ex_load_device_tree(hwaddr addr,
-                                     uint32_t ramsize,
+static int sam460ex_load_device_tree(MachineState *machine,
+                                     hwaddr addr,
                                      hwaddr initrd_base,
-                                     hwaddr initrd_size,
-                                     const char *kernel_cmdline)
+                                     hwaddr initrd_size)
 {
-    uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(ramsize) };
+    uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(machine->ram_size) };
     char *filename;
     int fdt_size;
     void *fdt;
@@ -171,7 +170,8 @@ static int sam460ex_load_device_tree(hwaddr addr,
     qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
                           (initrd_base + initrd_size));
 
-    qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", kernel_cmdline);
+    qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
+                            machine->kernel_cmdline);
 
     /* Copy data from the host device tree into the guest. Since the guest can
      * directly access the timebase without host involvement, we must expose
@@ -208,7 +208,9 @@ static int sam460ex_load_device_tree(hwaddr addr,
                               EBC_FREQ);
 
     rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
-    g_free(fdt);
+
+    /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = fdt;
 
     return fdt_size;
 }
@@ -496,9 +498,8 @@ static void sam460ex_init(MachineState *machine)
     if (machine->kernel_filename) {
         int dt_size;
 
-        dt_size = sam460ex_load_device_tree(FDT_ADDR, machine->ram_size,
-                                    RAMDISK_ADDR, initrd_size,
-                                    machine->kernel_cmdline);
+        dt_size = sam460ex_load_device_tree(machine, FDT_ADDR,
+                                            RAMDISK_ADDR, initrd_size);
 
         boot_info->dt_base = FDT_ADDR;
         boot_info->dt_size = dt_size;
#19 lore raw

Re: [PATCH v6 14/14] qmp/hmp, device_tree.c: introduce dumpdtb

In reply to Daniel Henrique Barboza
Quoted 39 lines · Daniel Henrique Barboza

To save the FDT blob we have the '-machine dumpdtb=<file>' property. With this property set, the machine saves the FDT in <file> and exit. The created file can then be converted to plain text dts format using 'dtc'.

There's nothing particularly sophisticated into saving the FDT that can't be done with the machine at any state, as long as the machine has a valid FDT to be saved.

The 'dumpdtb' command receives a 'filename' paramenter and, if a valid FDT is available, it'll save it in a file 'filename'. In short, this is a '-machine dumpdtb' that can be fired on demand via QMP/HMP.

A valid FDT consists of a FDT that was created using libfdt being retrieved via 'current_machine->fdt' in device_tree.c. This condition is met by most FDT users in QEMU.

This command will always be executed in-band (i.e. holding BQL), avoiding potential race conditions with machines that might change the FDT during runtime (e.g. PowerPC 'pseries' machine).

Cc Dr. David Alan Gilbert <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Alistair Francis <[email protected]>
Cc David Gibson <[email protected]>
---
 hmp-commands.hx              | 15 +++++++++++++++
 include/sysemu/device_tree.h |  1 +
 monitor/misc.c               |  1 +
 qapi/machine.json            | 18 ++++++++++++++++++
 softmmu/device_tree.c        | 31 +++++++++++++++++++++++++++++++
 5 files changed, 66 insertions(+)
hmp-commands.hx +0 -0
index 182e639d14..9a3e57504f 100644

hmp-commands.hx ERST · line 1808

Quoted 9 lines
hmp-commands.hx +6 -0
@@ -1800,3 +1800,18 @@ ERST
                       "\n\t\t\t\t\t limit on a specified virtual cpu",
         .cmd        = hmp_cancel_vcpu_dirty_limit,
     },
+
+#if defined(CONFIG_FDT)
+SRST
+``dumpdtb`` *filename*
+  Save the FDT in the 'filename' file to be decoded using dtc.
+  Requires 'libfdt' support.

Does the #if suppress the documentation snippet when !defined(CONFIG_FDT)?

If yes, the "Requires" line is redundant.

Other conditional commands don't have such lines.

Quoted 38 lines
hmp-commands.hx +9 -0
+ERST
+    {
+        .name       = "dumpdtb",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the FDT in the 'filename' file to be decoded using dtc",
+        .cmd        = hmp_dumpdtb,
+    },
+#endif
include/sysemu/device_tree.h +1 -0
index ef060a9759..e7c5441f56 100644
@@ -136,6 +136,7 @@ int qemu_fdt_add_path(void *fdt, const char *path);
     } while (0)
 
 void qemu_fdt_dumpdtb(void *fdt, int size);
+void hmp_dumpdtb(Monitor *mon, const QDict *qdict);
 
 /**
  * qemu_fdt_setprop_sized_cells_from_array:
monitor/misc.c +1 -0
index 3d2312ba8d..e7dd63030b 100644
@@ -49,6 +49,7 @@
 #include "sysemu/blockdev.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/tpm.h"
+#include "sysemu/device_tree.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qerror.h"
 #include "qapi/qmp/qstring.h"
qapi/machine.json +0 -0
index 6afd1936b0..f968a5d343 100644

qapi/machine.json line 1671

Quoted 8 lines
qapi/machine.json +5 -0
@@ -1664,3 +1664,21 @@
      '*size': 'size',
      '*max-size': 'size',
      '*slots': 'uint64' } }
+
+##
+# @dumpdtb:
+#
+# Save the FDT in dtb format. Requires 'libfdt' support.

We rarely document conditionals like this. It's kind of redundant with the

If

CONFIG_FDT

part in the generated documentation. Aside: this "If" part could certainly be improved.

qapi/machine.json line 1679

Quoted 8 lines
qapi/machine.json +8 -0
+#
+# @filename: name of the FDT file to be created
+#
+# Since: 7.2
+#
+# Example:
+#   {"execute": "dumpdtb"}
+#    "arguments": { "filename": "/tmp/fdt.dtb" } }

Excuse my nitpicking... Creating files with a fixed filename in /tmp is a bad habit, and this example promotes it. We have similar examples elsewhere. Still, I'd prefer not to add more.

Quoted 21 lines
qapi/machine.json +5 -0
+#
+##
+{ 'command': 'dumpdtb',
+  'data': { 'filename': 'str' },
+  'if': 'CONFIG_FDT' }
softmmu/device_tree.c +3 -0
index 6ca3fad285..cdd41b6de6 100644
@@ -26,6 +26,9 @@
 #include "hw/loader.h"
 #include "hw/boards.h"
 #include "qemu/config-file.h"
+#include "qapi/qapi-commands-machine.h"
+#include "qapi/qmp/qdict.h"
+#include "monitor/hmp.h"
 
 #include <libfdt.h>
 
@@ -643,3 +646,31 @@ out:
     g_free(propcells);

softmmu/device_tree.c out: · line 656

Quoted 10 lines
softmmu/device_tree.c +8 -0
     return ret;
 }
+
+void qmp_dumpdtb(const char *filename, Error **errp)
+{
+    g_autoptr(GError) err = NULL;
+    int size;
+
+    if (!current_machine->fdt) {
+        error_setg(errp, "Unable to find the machine FDT");

Perhaps: "this machine doesn't have an FDT".

Quoted 20 lines
softmmu/device_tree.c +20 -0
+        return;
+    }
+
+    size = fdt_totalsize(current_machine->fdt);
+
+    if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
+        error_setg(errp, "Error saving FDT to file %s: %s",
+                   filename, err->message);
+    }
+}
+
+void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
+{
+    const char *filename = qdict_get_str(qdict, "filename");
+    Error *local_err = NULL;
+
+    qmp_dumpdtb(filename, &local_err);
+
+    hmp_handle_error(mon, local_err);
+}
#20 lore raw

Re: [PATCH v6 14/14] qmp/hmp, device_tree.c: introduce dumpdtb

Quoted 54 lines · Markus Armbruster
Quoted 39 lines · Daniel Henrique Barboza

To save the FDT blob we have the '-machine dumpdtb=<file>' property. With this property set, the machine saves the FDT in <file> and exit. The created file can then be converted to plain text dts format using 'dtc'.

There's nothing particularly sophisticated into saving the FDT that can't be done with the machine at any state, as long as the machine has a valid FDT to be saved.

The 'dumpdtb' command receives a 'filename' paramenter and, if a valid FDT is available, it'll save it in a file 'filename'. In short, this is a '-machine dumpdtb' that can be fired on demand via QMP/HMP.

A valid FDT consists of a FDT that was created using libfdt being retrieved via 'current_machine->fdt' in device_tree.c. This condition is met by most FDT users in QEMU.

This command will always be executed in-band (i.e. holding BQL), avoiding potential race conditions with machines that might change the FDT during runtime (e.g. PowerPC 'pseries' machine).

Cc Dr. David Alan Gilbert <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Alistair Francis <[email protected]>
Cc David Gibson <[email protected]>
---
  hmp-commands.hx              | 15 +++++++++++++++
  include/sysemu/device_tree.h |  1 +
  monitor/misc.c               |  1 +
  qapi/machine.json            | 18 ++++++++++++++++++
  softmmu/device_tree.c        | 31 +++++++++++++++++++++++++++++++
  5 files changed, 66 insertions(+)
hmp-commands.hx +0 -0
index 182e639d14..9a3e57504f 100644

hmp-commands.hx ERST · line 1808

Quoted 9 lines
hmp-commands.hx +6 -0
@@ -1800,3 +1800,18 @@ ERST
                        "\n\t\t\t\t\t limit on a specified virtual cpu",
          .cmd        = hmp_cancel_vcpu_dirty_limit,
      },
+
+#if defined(CONFIG_FDT)
+SRST
+``dumpdtb`` *filename*
+  Save the FDT in the 'filename' file to be decoded using dtc.
+  Requires 'libfdt' support.

Does the #if suppress the documentation snippet when !defined(CONFIG_FDT)?

Quoted 3 lines

If yes, the "Requires" line is redundant.

Other conditional commands don't have such lines.

I tested using a target that doesn't rely on libfdt (in this case I picked the tricore softmmu) and disabled FDT support in ./configure:

configure --target-list=tricore-softmmu --enable-fdt=disabled

This didn't prevent the 'dumpdtb' section from appearing in the generated docs at build/docs/manual/system/monitor.html. So I guess the documentation isn't being suppressed by 'CONFIG_FDT'.

That said, I can remove the "Requires libfdt" observations to be in line with other commands, and then we can then discuss in separate whether the documentation should follow these preprocessing macros.

Thanks,

Daniel

Quoted 126 lines
Quoted 39 lines
hmp-commands.hx +9 -0
+ERST
+    {
+        .name       = "dumpdtb",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the FDT in the 'filename' file to be decoded using dtc",
+        .cmd        = hmp_dumpdtb,
+    },
+#endif
include/sysemu/device_tree.h +1 -0
index ef060a9759..e7c5441f56 100644
@@ -136,6 +136,7 @@ int qemu_fdt_add_path(void *fdt, const char *path);
      } while (0)
 
  void qemu_fdt_dumpdtb(void *fdt, int size);
+void hmp_dumpdtb(Monitor *mon, const QDict *qdict);
 
  /**
   * qemu_fdt_setprop_sized_cells_from_array:
monitor/misc.c +1 -0
index 3d2312ba8d..e7dd63030b 100644
@@ -49,6 +49,7 @@
  #include "sysemu/blockdev.h"
  #include "sysemu/sysemu.h"
  #include "sysemu/tpm.h"
+#include "sysemu/device_tree.h"
  #include "qapi/qmp/qdict.h"
  #include "qapi/qmp/qerror.h"
  #include "qapi/qmp/qstring.h"
qapi/machine.json +0 -0
index 6afd1936b0..f968a5d343 100644

qapi/machine.json line 1671

Quoted 8 lines
qapi/machine.json +5 -0
@@ -1664,3 +1664,21 @@
       '*size': 'size',
       '*max-size': 'size',
       '*slots': 'uint64' } }
+
+##
+# @dumpdtb:
+#
+# Save the FDT in dtb format. Requires 'libfdt' support.

We rarely document conditionals like this. It's kind of redundant with the

If

CONFIG_FDT

part in the generated documentation. Aside: this "If" part could certainly be improved.

qapi/machine.json line 1679

Quoted 8 lines
qapi/machine.json +8 -0
+#
+# @filename: name of the FDT file to be created
+#
+# Since: 7.2
+#
+# Example:
+#   {"execute": "dumpdtb"}
+#    "arguments": { "filename": "/tmp/fdt.dtb" } }

Excuse my nitpicking... Creating files with a fixed filename in /tmp is a bad habit, and this example promotes it. We have similar examples elsewhere. Still, I'd prefer not to add more.

Quoted 21 lines
qapi/machine.json +5 -0
+#
+##
+{ 'command': 'dumpdtb',
+  'data': { 'filename': 'str' },
+  'if': 'CONFIG_FDT' }
softmmu/device_tree.c +3 -0
index 6ca3fad285..cdd41b6de6 100644
@@ -26,6 +26,9 @@
  #include "hw/loader.h"
  #include "hw/boards.h"
  #include "qemu/config-file.h"
+#include "qapi/qapi-commands-machine.h"
+#include "qapi/qmp/qdict.h"
+#include "monitor/hmp.h"
 
  #include <libfdt.h>
 
@@ -643,3 +646,31 @@ out:
      g_free(propcells);

softmmu/device_tree.c out: · line 656

Quoted 10 lines
softmmu/device_tree.c +8 -0
      return ret;
  }
+
+void qmp_dumpdtb(const char *filename, Error **errp)
+{
+    g_autoptr(GError) err = NULL;
+    int size;
+
+    if (!current_machine->fdt) {
+        error_setg(errp, "Unable to find the machine FDT");

Perhaps: "this machine doesn't have an FDT".

Quoted 20 lines
softmmu/device_tree.c +20 -0
+        return;
+    }
+
+    size = fdt_totalsize(current_machine->fdt);
+
+    if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
+        error_setg(errp, "Error saving FDT to file %s: %s",
+                   filename, err->message);
+    }
+}
+
+void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
+{
+    const char *filename = qdict_get_str(qdict, "filename");
+    Error *local_err = NULL;
+
+    qmp_dumpdtb(filename, &local_err);
+
+    hmp_handle_error(mon, local_err);
+}
Reviewed-by on v6 9/14
#12 lore raw

Re: [PATCH v6 09/14] hw/ppc: set machine->fdt in pnv_reset()

In reply to Daniel Henrique Barboza
Quoted 6 lines · Daniel Henrique Barboza

This will enable support for the 'dumpdtb' QMP/HMP command for all powernv machines.

Cc Cédric Le Goater <[email protected]>
Cc Frederic Barrat <[email protected]>

Thanks,

C.

Quoted 23 lines
---
  hw/ppc/pnv.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/pnv.c +7 -1
index 354aa289d1..6a20c4811f 100644
@@ -678,7 +678,13 @@ static void pnv_reset(MachineState *machine)
      qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
      cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
 
-    g_free(fdt);
+    /*
+     * Set machine->fdt for 'dumpdtb' QMP/HMP command. Free
+     * the existing machine->fdt to avoid leaking it during
+     * a reset.
+     */
+    g_free(machine->fdt);
+    machine->fdt = fdt;
  }
 
  static ISABus *pnv_chip_power8_isa_create(PnvChip *chip, Error **errp)
Reviewed-by on v6 9/14
#13 lore raw

Re: [PATCH v6 09/14] hw/ppc: set machine->fdt in pnv_reset()

In reply to Daniel Henrique Barboza
Quoted 7 lines · Daniel Henrique Barboza

This will enable support for the 'dumpdtb' QMP/HMP command for all powernv machines.

Cc Cédric Le Goater <[email protected]>
Cc Frederic Barrat <[email protected]>

---

LGTM

Fred

Quoted 22 lines
  hw/ppc/pnv.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/pnv.c +7 -1
index 354aa289d1..6a20c4811f 100644
@@ -678,7 +678,13 @@ static void pnv_reset(MachineState *machine)
      qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
      cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
 
-    g_free(fdt);
+    /*
+     * Set machine->fdt for 'dumpdtb' QMP/HMP command. Free
+     * the existing machine->fdt to avoid leaking it during
+     * a reset.
+     */
+    g_free(machine->fdt);
+    machine->fdt = fdt;
  }
 
  static ISABus *pnv_chip_power8_isa_create(PnvChip *chip, Error **errp)
Acked-by on v6 14/14
#21 lore raw

Re: [PATCH v6 14/14] qmp/hmp, device_tree.c: introduce dumpdtb

In reply to Daniel Henrique Barboza
Quoted 39 lines · * Daniel Henrique Barboza ([email protected]) wrote:

To save the FDT blob we have the '-machine dumpdtb=<file>' property. With this property set, the machine saves the FDT in <file> and exit. The created file can then be converted to plain text dts format using 'dtc'.

There's nothing particularly sophisticated into saving the FDT that can't be done with the machine at any state, as long as the machine has a valid FDT to be saved.

The 'dumpdtb' command receives a 'filename' paramenter and, if a valid FDT is available, it'll save it in a file 'filename'. In short, this is a '-machine dumpdtb' that can be fired on demand via QMP/HMP.

A valid FDT consists of a FDT that was created using libfdt being retrieved via 'current_machine->fdt' in device_tree.c. This condition is met by most FDT users in QEMU.

This command will always be executed in-band (i.e. holding BQL), avoiding potential race conditions with machines that might change the FDT during runtime (e.g. PowerPC 'pseries' machine).

Cc Dr. David Alan Gilbert <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Alistair Francis <[email protected]>
Cc David Gibson <[email protected]>
---
 hmp-commands.hx              | 15 +++++++++++++++
 include/sysemu/device_tree.h |  1 +
 monitor/misc.c               |  1 +
 qapi/machine.json            | 18 ++++++++++++++++++
 softmmu/device_tree.c        | 31 +++++++++++++++++++++++++++++++
 5 files changed, 66 insertions(+)
hmp-commands.hx +0 -0
index 182e639d14..9a3e57504f 100644

hmp-commands.hx ERST · line 1809

Quoted 10 lines
hmp-commands.hx +7 -0
@@ -1800,3 +1800,18 @@ ERST
                       "\n\t\t\t\t\t limit on a specified virtual cpu",
         .cmd        = hmp_cancel_vcpu_dirty_limit,
     },
+
+#if defined(CONFIG_FDT)
+SRST
+``dumpdtb`` *filename*
+  Save the FDT in the 'filename' file to be decoded using dtc.
+  Requires 'libfdt' support.
+ERST

Can you please put the SRST/ERST below the { }'s please; most of the hmp-commands.hx has it that way around but it looks like the diry rate/limit set at the bottom have them flipped which we need to fix.

hmp-commands.hx ERST · line 1817

Quoted 8 lines
hmp-commands.hx +8 -0
+    {
+        .name       = "dumpdtb",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the FDT in the 'filename' file to be decoded using dtc",
+        .cmd        = hmp_dumpdtb,
+    },
+#endif

Other than the flip,

Quoted 99 lines
include/sysemu/device_tree.h +1 -0
index ef060a9759..e7c5441f56 100644
@@ -136,6 +136,7 @@ int qemu_fdt_add_path(void *fdt, const char *path);
     } while (0)
 
 void qemu_fdt_dumpdtb(void *fdt, int size);
+void hmp_dumpdtb(Monitor *mon, const QDict *qdict);
 
 /**
  * qemu_fdt_setprop_sized_cells_from_array:
monitor/misc.c +1 -0
index 3d2312ba8d..e7dd63030b 100644
@@ -49,6 +49,7 @@
 #include "sysemu/blockdev.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/tpm.h"
+#include "sysemu/device_tree.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qerror.h"
 #include "qapi/qmp/qstring.h"
qapi/machine.json +18 -0
index 6afd1936b0..f968a5d343 100644
@@ -1664,3 +1664,21 @@
      '*size': 'size',
      '*max-size': 'size',
      '*slots': 'uint64' } }
+
+##
+# @dumpdtb:
+#
+# Save the FDT in dtb format. Requires 'libfdt' support.
+#
+# @filename: name of the FDT file to be created
+#
+# Since: 7.2
+#
+# Example:
+#   {"execute": "dumpdtb"}
+#    "arguments": { "filename": "/tmp/fdt.dtb" } }
+#
+##
+{ 'command': 'dumpdtb',
+  'data': { 'filename': 'str' },
+  'if': 'CONFIG_FDT' }
softmmu/device_tree.c +31 -0
index 6ca3fad285..cdd41b6de6 100644
@@ -26,6 +26,9 @@
 #include "hw/loader.h"
 #include "hw/boards.h"
 #include "qemu/config-file.h"
+#include "qapi/qapi-commands-machine.h"
+#include "qapi/qmp/qdict.h"
+#include "monitor/hmp.h"
 
 #include <libfdt.h>
 
@@ -643,3 +646,31 @@ out:
     g_free(propcells);
     return ret;
 }
+
+void qmp_dumpdtb(const char *filename, Error **errp)
+{
+    g_autoptr(GError) err = NULL;
+    int size;
+
+    if (!current_machine->fdt) {
+        error_setg(errp, "Unable to find the machine FDT");
+        return;
+    }
+
+    size = fdt_totalsize(current_machine->fdt);
+
+    if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
+        error_setg(errp, "Error saving FDT to file %s: %s",
+                   filename, err->message);
+    }
+}
+
+void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
+{
+    const char *filename = qdict_get_str(qdict, "filename");
+    Error *local_err = NULL;
+
+    qmp_dumpdtb(filename, &local_err);
+
+    hmp_handle_error(mon, local_err);
+}
Signature · 1 line
2.37.2
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK