All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb'
@ 2022-09-08 19:40 Daniel Henrique Barboza
  2022-09-08 19:40 ` [PATCH v7 01/14] hw/arm: do not free machine->fdt in arm_load_dtb() Daniel Henrique Barboza
                   ` (15 more replies)
  0 siblings, 16 replies; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza

Hi,

This new version implements all change requests from the v6.

- patch 5:
  - change bamboo_load_device_tree() to use a MachineState pointer
- patch 7:
  - change xilinx_load_device_tree() to use a MachineState pointer
- patch 14:
  - placed SRST/ERST below the { }'s
  - removed the '/tmp' reference in the command example
  - removed all 'Requires libfdt' references
  - changed qmp_dumpdtb() missing FDT error message to "This machine
    doesn't have a FDT"
- v6 link: https://lists.gnu.org/archive/html/qemu-devel/2022-09/msg00534.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       | 25 ++++++++++++++-----------
 hw/ppc/sam460ex.c            | 21 +++++++++++----------
 hw/ppc/spapr.c               |  3 +++
 hw/ppc/spapr_hcall.c         |  8 ++++++++
 hw/ppc/virtex_ml507.c        | 25 ++++++++++++++-----------
 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, 172 insertions(+), 41 deletions(-)

-- 
2.37.2



^ permalink raw reply	[flat|nested] 32+ messages in thread

* [PATCH v7 01/14] hw/arm: do not free machine->fdt in arm_load_dtb()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-08 19:40 ` [PATCH v7 02/14] hw/microblaze: set machine->fdt in microblaze_load_dtb() Daniel Henrique Barboza
                   ` (14 subsequent siblings)
  15 siblings, 0 replies; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-ppc, clg, Daniel Henrique Barboza, Peter Maydell, qemu-arm

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).

Cc: Peter Maydell <peter.maydell@linaro.org>
Cc: qemu-arm@nongnu.org
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/arm/boot.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/hw/arm/boot.c b/hw/arm/boot.c
index ada2717f76..60bbfba37f 100644
--- a/hw/arm/boot.c
+++ b/hw/arm/boot.c
@@ -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;
 
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 02/14] hw/microblaze: set machine->fdt in microblaze_load_dtb()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
  2022-09-08 19:40 ` [PATCH v7 01/14] hw/arm: do not free machine->fdt in arm_load_dtb() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-08 19:40 ` [PATCH v7 03/14] hw/nios2: set machine->fdt in nios2_load_dtb() Daniel Henrique Barboza
                   ` (13 subsequent siblings)
  15 siblings, 0 replies; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza, Edgar E . Iglesias

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

Cc: Edgar E. Iglesias <edgar.iglesias@gmail.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/microblaze/boot.c      | 8 +++++++-
 hw/microblaze/meson.build | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/hw/microblaze/boot.c b/hw/microblaze/boot.c
index 8b92a9801a..c8eff7b6fc 100644
--- a/hw/microblaze/boot.c
+++ b/hw/microblaze/boot.c
@@ -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;
 }
 
diff --git a/hw/microblaze/meson.build b/hw/microblaze/meson.build
index bb9e4eb8f4..a38a397872 100644
--- a/hw/microblaze/meson.build
+++ b/hw/microblaze/meson.build
@@ -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'))
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 03/14] hw/nios2: set machine->fdt in nios2_load_dtb()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
  2022-09-08 19:40 ` [PATCH v7 01/14] hw/arm: do not free machine->fdt in arm_load_dtb() Daniel Henrique Barboza
  2022-09-08 19:40 ` [PATCH v7 02/14] hw/microblaze: set machine->fdt in microblaze_load_dtb() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-22 10:54   ` Philippe Mathieu-Daudé via
  2022-09-08 19:40 ` [PATCH v7 04/14] hw/ppc: set machine->fdt in ppce500_load_device_tree() Daniel Henrique Barboza
                   ` (12 subsequent siblings)
  15 siblings, 1 reply; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-ppc, clg, Daniel Henrique Barboza, Chris Wulff, Marek Vasut

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

Cc: Chris Wulff <crwulff@gmail.com>
Cc: Marek Vasut <marex@denx.de>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/nios2/boot.c      | 8 +++++++-
 hw/nios2/meson.build | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/hw/nios2/boot.c b/hw/nios2/boot.c
index 21cbffff47..b30a7b1efb 100644
--- a/hw/nios2/boot.c
+++ b/hw/nios2/boot.c
@@ -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;
 }
 
diff --git a/hw/nios2/meson.build b/hw/nios2/meson.build
index 6c58e8082b..22277bd6c5 100644
--- a/hw/nios2/meson.build
+++ b/hw/nios2/meson.build
@@ -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'))
 
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 04/14] hw/ppc: set machine->fdt in ppce500_load_device_tree()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (2 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 03/14] hw/nios2: set machine->fdt in nios2_load_dtb() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-22 10:51   ` Philippe Mathieu-Daudé via
  2022-09-08 19:40 ` [PATCH v7 05/14] hw/ppc: set machine->fdt in bamboo_load_device_tree() Daniel Henrique Barboza
                   ` (11 subsequent siblings)
  15 siblings, 1 reply; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza

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

Cc: Cédric Le Goater <clg@kaod.org>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/ppc/e500.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/hw/ppc/e500.c b/hw/ppc/e500.c
index 32495d0123..ea5f947824 100644
--- a/hw/ppc/e500.c
+++ b/hw/ppc/e500.c
@@ -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);
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 05/14] hw/ppc: set machine->fdt in bamboo_load_device_tree()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (3 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 04/14] hw/ppc: set machine->fdt in ppce500_load_device_tree() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-08 19:55   ` BALATON Zoltan
  2022-09-08 19:40 ` [PATCH v7 06/14] hw/ppc: set machine->fdt in sam460ex_load_device_tree() Daniel Henrique Barboza
                   ` (10 subsequent siblings)
  15 siblings, 1 reply; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza

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

Setting machine->fdt requires a MachineState pointer to be used inside
bamboo_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.

Cc: Cédric Le Goater <clg@kaod.org>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/ppc/ppc440_bamboo.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/hw/ppc/ppc440_bamboo.c b/hw/ppc/ppc440_bamboo.c
index ea945a1c99..9cc58fccf9 100644
--- a/hw/ppc/ppc440_bamboo.c
+++ b/hw/ppc/ppc440_bamboo.c
@@ -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 */
@@ -56,14 +58,13 @@ static const ram_addr_t ppc440ep_sdram_bank_sizes[] = {
 
 static hwaddr entry;
 
-static int bamboo_load_device_tree(hwaddr addr,
-                                     uint32_t ramsize,
-                                     hwaddr initrd_base,
-                                     hwaddr initrd_size,
-                                     const char *kernel_cmdline)
+static int bamboo_load_device_tree(MachineState *machine,
+                                   hwaddr addr,
+                                   hwaddr initrd_base,
+                                   hwaddr initrd_size)
 {
     int ret = -1;
-    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;
@@ -98,7 +99,7 @@ static int bamboo_load_device_tree(hwaddr addr,
         fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
     }
     ret = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
-                                  kernel_cmdline);
+                                  machine->kernel_cmdline);
     if (ret < 0) {
         fprintf(stderr, "couldn't set /chosen/bootargs\n");
     }
@@ -119,7 +120,10 @@ 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);
+
+    /* Set ms->fdt for 'dumpdtb' QMP/HMP command */
+    machine->fdt = fdt;
+
     return 0;
 }
 
@@ -163,7 +167,6 @@ static void main_cpu_reset(void *opaque)
 static void bamboo_init(MachineState *machine)
 {
     const char *kernel_filename = machine->kernel_filename;
-    const char *kernel_cmdline = machine->kernel_cmdline;
     const char *initrd_filename = machine->initrd_filename;
     unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
     MemoryRegion *address_space_mem = get_system_memory();
@@ -289,8 +292,8 @@ static void bamboo_init(MachineState *machine)
 
     /* If we're loading a kernel directly, we must load the device tree too. */
     if (kernel_filename) {
-        if (bamboo_load_device_tree(FDT_ADDR, machine->ram_size, RAMDISK_ADDR,
-                                    initrd_size, kernel_cmdline) < 0) {
+        if (bamboo_load_device_tree(machine, FDT_ADDR,
+                                    RAMDISK_ADDR, initrd_size) < 0) {
             error_report("couldn't load device tree");
             exit(1);
         }
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 06/14] hw/ppc: set machine->fdt in sam460ex_load_device_tree()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (4 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 05/14] hw/ppc: set machine->fdt in bamboo_load_device_tree() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-22 10:53   ` Philippe Mathieu-Daudé via
  2022-09-08 19:40 ` [PATCH v7 07/14] hw/ppc: set machine->fdt in xilinx_load_device_tree() Daniel Henrique Barboza
                   ` (9 subsequent siblings)
  15 siblings, 1 reply; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza, BALATON Zoltan

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.

Cc: BALATON Zoltan <balaton@eik.bme.hu>
Reviewed-by: BALATON Zoltan <balaton@eik.bme.hu>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/ppc/sam460ex.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/hw/ppc/sam460ex.c b/hw/ppc/sam460ex.c
index 850bb3b817..5d09d3c6ab 100644
--- a/hw/ppc/sam460ex.c
+++ b/hw/ppc/sam460ex.c
@@ -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;
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 07/14] hw/ppc: set machine->fdt in xilinx_load_device_tree()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (5 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 06/14] hw/ppc: set machine->fdt in sam460ex_load_device_tree() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-08 19:57   ` BALATON Zoltan
  2022-09-22 10:54   ` Philippe Mathieu-Daudé via
  2022-09-08 19:40 ` [PATCH v7 08/14] hw/ppc: set machine->fdt in pegasos2_machine_reset() Daniel Henrique Barboza
                   ` (8 subsequent siblings)
  15 siblings, 2 replies; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza, Edgar E . Iglesias

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

Setting machine->fdt requires a MachineState pointer to be used inside
xilinx_load_device_tree(). Let's change the function to receive this
pointer from the caller. kernel_cmdline' can be retrieved directly from
the 'machine' pointer. 'ramsize' wasn't being used so can be removed.

Cc: Edgar E. Iglesias <edgar.iglesias@gmail.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/ppc/virtex_ml507.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/hw/ppc/virtex_ml507.c b/hw/ppc/virtex_ml507.c
index 493ea0c19f..13cace229b 100644
--- a/hw/ppc/virtex_ml507.c
+++ b/hw/ppc/virtex_ml507.c
@@ -45,6 +45,8 @@
 #include "hw/qdev-properties.h"
 #include "ppc405.h"
 
+#include <libfdt.h>
+
 #define EPAPR_MAGIC    (0x45504150)
 #define FLASH_SIZE     (16 * MiB)
 
@@ -144,11 +146,10 @@ static void main_cpu_reset(void *opaque)
 }
 
 #define BINARY_DEVICE_TREE_FILE "virtex-ml507.dtb"
-static int xilinx_load_device_tree(hwaddr addr,
-                                      uint32_t ramsize,
-                                      hwaddr initrd_base,
-                                      hwaddr initrd_size,
-                                      const char *kernel_cmdline)
+static int xilinx_load_device_tree(MachineState *machine,
+                                   hwaddr addr,
+                                   hwaddr initrd_base,
+                                   hwaddr initrd_size)
 {
     char *path;
     int fdt_size;
@@ -190,18 +191,21 @@ static int xilinx_load_device_tree(hwaddr addr,
         error_report("couldn't set /chosen/linux,initrd-end");
     }
 
-    r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", kernel_cmdline);
+    r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
+                                machine->kernel_cmdline);
     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;
 }
 
 static void virtex_init(MachineState *machine)
 {
     const char *kernel_filename = machine->kernel_filename;
-    const char *kernel_cmdline = machine->kernel_cmdline;
     hwaddr initrd_base = 0;
     int initrd_size = 0;
     MemoryRegion *address_space_mem = get_system_memory();
@@ -294,9 +298,8 @@ static void virtex_init(MachineState *machine)
         boot_info.fdt = high + (8192 * 2);
         boot_info.fdt &= ~8191;
 
-        xilinx_load_device_tree(boot_info.fdt, machine->ram_size,
-                                initrd_base, initrd_size,
-                                kernel_cmdline);
+        xilinx_load_device_tree(machine, boot_info.fdt,
+                                initrd_base, initrd_size);
     }
     env->load_info = &boot_info;
 }
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 08/14] hw/ppc: set machine->fdt in pegasos2_machine_reset()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (6 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 07/14] hw/ppc: set machine->fdt in xilinx_load_device_tree() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-08 19:58   ` BALATON Zoltan
  2022-09-08 19:40 ` [PATCH v7 09/14] hw/ppc: set machine->fdt in pnv_reset() Daniel Henrique Barboza
                   ` (7 subsequent siblings)
  15 siblings, 1 reply; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza, BALATON Zoltan

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

Cc: BALATON Zoltan <balaton@eik.bme.hu>
Cc: qemu-ppc@nongnu.org
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/ppc/pegasos2.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/hw/ppc/pegasos2.c b/hw/ppc/pegasos2.c
index 61f4263953..ecf682b148 100644
--- a/hw/ppc/pegasos2.c
+++ b/hw/ppc/pegasos2.c
@@ -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);
 }
 
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 09/14] hw/ppc: set machine->fdt in pnv_reset()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (7 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 08/14] hw/ppc: set machine->fdt in pegasos2_machine_reset() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-08 19:40 ` [PATCH v7 10/14] hw/ppc: set machine->fdt in spapr machine Daniel Henrique Barboza
                   ` (6 subsequent siblings)
  15 siblings, 0 replies; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza, Frederic Barrat

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

Reviewed-by: Cédric Le Goater <clg@kaod.org>
Reviewed-by: Frederic Barrat <fbarrat@linux.ibm.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/ppc/pnv.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/hw/ppc/pnv.c b/hw/ppc/pnv.c
index 354aa289d1..6a20c4811f 100644
--- a/hw/ppc/pnv.c
+++ b/hw/ppc/pnv.c
@@ -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)
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 10/14] hw/ppc: set machine->fdt in spapr machine
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (8 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 09/14] hw/ppc: set machine->fdt in pnv_reset() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-22 10:56   ` Philippe Mathieu-Daudé via
  2022-09-08 19:40 ` [PATCH v7 11/14] hw/riscv: set machine->fdt in sifive_u_machine_init() Daniel Henrique Barboza
                   ` (5 subsequent siblings)
  15 siblings, 1 reply; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza, David Gibson

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.

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/ppc/spapr.c       | 3 +++
 hw/ppc/spapr_hcall.c | 8 ++++++++
 2 files changed, 11 insertions(+)

diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
index fb790b61e4..170bbfd199 100644
--- a/hw/ppc/spapr.c
+++ b/hw/ppc/spapr.c
@@ -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;
 
diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c
index a8d4a6bcf0..891206e893 100644
--- a/hw/ppc/spapr_hcall.c
+++ b/hw/ppc/spapr_hcall.c
@@ -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;
 }
 
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 11/14] hw/riscv: set machine->fdt in sifive_u_machine_init()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (9 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 10/14] hw/ppc: set machine->fdt in spapr machine Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-22 10:56   ` Philippe Mathieu-Daudé via
  2022-09-08 19:40 ` [PATCH v7 12/14] hw/riscv: set machine->fdt in spike_board_init() Daniel Henrique Barboza
                   ` (4 subsequent siblings)
  15 siblings, 1 reply; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-ppc, clg, Daniel Henrique Barboza, Alistair Francis,
	Bin Meng, Palmer Dabbelt, Alistair Francis

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

Cc: Alistair Francis <Alistair.Francis@wdc.com>
Cc: Bin Meng <bin.meng@windriver.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/riscv/sifive_u.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/hw/riscv/sifive_u.c b/hw/riscv/sifive_u.c
index e4c814a3ea..b139824aab 100644
--- a/hw/riscv/sifive_u.c
+++ b/hw/riscv/sifive_u.c
@@ -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 */
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 12/14] hw/riscv: set machine->fdt in spike_board_init()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (10 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 11/14] hw/riscv: set machine->fdt in sifive_u_machine_init() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-22 10:55   ` Philippe Mathieu-Daudé via
  2022-09-08 19:40 ` [PATCH v7 13/14] hw/xtensa: set machine->fdt in xtfpga_init() Daniel Henrique Barboza
                   ` (3 subsequent siblings)
  15 siblings, 1 reply; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-ppc, clg, Daniel Henrique Barboza, Palmer Dabbelt,
	Alistair Francis, Bin Meng

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

Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Alistair Francis <alistair.francis@wdc.com>
Cc: Bin Meng <bin.meng@windriver.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/riscv/spike.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/hw/riscv/spike.c b/hw/riscv/spike.c
index 5ba34543c8..1e1d752c00 100644
--- a/hw/riscv/spike.c
+++ b/hw/riscv/spike.c
@@ -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,
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 13/14] hw/xtensa: set machine->fdt in xtfpga_init()
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (11 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 12/14] hw/riscv: set machine->fdt in spike_board_init() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-08 19:40 ` [PATCH v7 14/14] qmp/hmp, device_tree.c: introduce dumpdtb Daniel Henrique Barboza
                   ` (2 subsequent siblings)
  15 siblings, 0 replies; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg, Daniel Henrique Barboza

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

Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hw/xtensa/meson.build | 2 +-
 hw/xtensa/xtfpga.c    | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/hw/xtensa/meson.build b/hw/xtensa/meson.build
index 1d5835df4b..ebba51cc74 100644
--- a/hw/xtensa/meson.build
+++ b/hw/xtensa/meson.build
@@ -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}
diff --git a/hw/xtensa/xtfpga.c b/hw/xtensa/xtfpga.c
index 2a5556a35f..867427c3d9 100644
--- a/hw/xtensa/xtfpga.c
+++ b/hw/xtensa/xtfpga.c
@@ -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) {
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v7 14/14] qmp/hmp, device_tree.c: introduce dumpdtb
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (12 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 13/14] hw/xtensa: set machine->fdt in xtfpga_init() Daniel Henrique Barboza
@ 2022-09-08 19:40 ` Daniel Henrique Barboza
  2022-09-22 10:47   ` Philippe Mathieu-Daudé via
  2022-09-22  9:40 ` [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
  2022-09-22 11:08 ` Philippe Mathieu-Daudé via
  15 siblings, 1 reply; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-08 19:40 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-ppc, clg, Daniel Henrique Barboza, Dr . David Alan Gilbert,
	Markus Armbruster, Alistair Francis, David Gibson

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 <dgilbert@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Cc: Alistair Francis <alistair.francis@wdc.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 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(+)

diff --git a/hmp-commands.hx b/hmp-commands.hx
index 182e639d14..753669a2eb 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -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)
+    {
+        .name       = "dumpdtb",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the FDT in the 'filename' file to be decoded using dtc",
+        .cmd        = hmp_dumpdtb,
+    },
+
+SRST
+``dumpdtb`` *filename*
+  Save the FDT in the 'filename' file to be decoded using dtc.
+ERST
+#endif
diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h
index ef060a9759..e7c5441f56 100644
--- a/include/sysemu/device_tree.h
+++ b/include/sysemu/device_tree.h
@@ -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:
diff --git a/monitor/misc.c b/monitor/misc.c
index 3d2312ba8d..e7dd63030b 100644
--- a/monitor/misc.c
+++ b/monitor/misc.c
@@ -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"
diff --git a/qapi/machine.json b/qapi/machine.json
index abb2f48808..9f0c8c8374 100644
--- a/qapi/machine.json
+++ b/qapi/machine.json
@@ -1664,3 +1664,21 @@
      '*size': 'size',
      '*max-size': 'size',
      '*slots': 'uint64' } }
+
+##
+# @dumpdtb:
+#
+# Save the FDT in dtb format.
+#
+# @filename: name of the FDT file to be created
+#
+# Since: 7.2
+#
+# Example:
+#   {"execute": "dumpdtb"}
+#    "arguments": { "filename": "fdt.dtb" } }
+#
+##
+{ 'command': 'dumpdtb',
+  'data': { 'filename': 'str' },
+  'if': 'CONFIG_FDT' }
diff --git a/softmmu/device_tree.c b/softmmu/device_tree.c
index 6ca3fad285..7031dcf89d 100644
--- a/softmmu/device_tree.c
+++ b/softmmu/device_tree.c
@@ -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, "This machine doesn't have a 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);
+}
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 05/14] hw/ppc: set machine->fdt in bamboo_load_device_tree()
  2022-09-08 19:40 ` [PATCH v7 05/14] hw/ppc: set machine->fdt in bamboo_load_device_tree() Daniel Henrique Barboza
@ 2022-09-08 19:55   ` BALATON Zoltan
  0 siblings, 0 replies; 32+ messages in thread
From: BALATON Zoltan @ 2022-09-08 19:55 UTC (permalink / raw)
  To: Daniel Henrique Barboza; +Cc: qemu-devel, qemu-ppc, clg

[-- Attachment #1: Type: text/plain, Size: 3583 bytes --]

On Thu, 8 Sep 2022, Daniel Henrique Barboza wrote:
> This will enable support for 'dumpdtb' QMP/HMP command for the bamboo
> machine.
>
> Setting machine->fdt requires a MachineState pointer to be used inside
> bamboo_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.
>
> Cc: Cédric Le Goater <clg@kaod.org>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>

Reviewed-by: BALATON Zoltan <balaton@eik.bme.hu>

> ---
> hw/ppc/ppc440_bamboo.c | 25 ++++++++++++++-----------
> 1 file changed, 14 insertions(+), 11 deletions(-)
>
> diff --git a/hw/ppc/ppc440_bamboo.c b/hw/ppc/ppc440_bamboo.c
> index ea945a1c99..9cc58fccf9 100644
> --- a/hw/ppc/ppc440_bamboo.c
> +++ b/hw/ppc/ppc440_bamboo.c
> @@ -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 */
> @@ -56,14 +58,13 @@ static const ram_addr_t ppc440ep_sdram_bank_sizes[] = {
>
> static hwaddr entry;
>
> -static int bamboo_load_device_tree(hwaddr addr,
> -                                     uint32_t ramsize,
> -                                     hwaddr initrd_base,
> -                                     hwaddr initrd_size,
> -                                     const char *kernel_cmdline)
> +static int bamboo_load_device_tree(MachineState *machine,
> +                                   hwaddr addr,
> +                                   hwaddr initrd_base,
> +                                   hwaddr initrd_size)
> {
>     int ret = -1;
> -    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;
> @@ -98,7 +99,7 @@ static int bamboo_load_device_tree(hwaddr addr,
>         fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
>     }
>     ret = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
> -                                  kernel_cmdline);
> +                                  machine->kernel_cmdline);
>     if (ret < 0) {
>         fprintf(stderr, "couldn't set /chosen/bootargs\n");
>     }
> @@ -119,7 +120,10 @@ 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);
> +
> +    /* Set ms->fdt for 'dumpdtb' QMP/HMP command */
> +    machine->fdt = fdt;
> +
>     return 0;
> }
>
> @@ -163,7 +167,6 @@ static void main_cpu_reset(void *opaque)
> static void bamboo_init(MachineState *machine)
> {
>     const char *kernel_filename = machine->kernel_filename;
> -    const char *kernel_cmdline = machine->kernel_cmdline;
>     const char *initrd_filename = machine->initrd_filename;
>     unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
>     MemoryRegion *address_space_mem = get_system_memory();
> @@ -289,8 +292,8 @@ static void bamboo_init(MachineState *machine)
>
>     /* If we're loading a kernel directly, we must load the device tree too. */
>     if (kernel_filename) {
> -        if (bamboo_load_device_tree(FDT_ADDR, machine->ram_size, RAMDISK_ADDR,
> -                                    initrd_size, kernel_cmdline) < 0) {
> +        if (bamboo_load_device_tree(machine, FDT_ADDR,
> +                                    RAMDISK_ADDR, initrd_size) < 0) {
>             error_report("couldn't load device tree");
>             exit(1);
>         }
>

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 07/14] hw/ppc: set machine->fdt in xilinx_load_device_tree()
  2022-09-08 19:40 ` [PATCH v7 07/14] hw/ppc: set machine->fdt in xilinx_load_device_tree() Daniel Henrique Barboza
@ 2022-09-08 19:57   ` BALATON Zoltan
  2022-09-22 10:54   ` Philippe Mathieu-Daudé via
  1 sibling, 0 replies; 32+ messages in thread
From: BALATON Zoltan @ 2022-09-08 19:57 UTC (permalink / raw)
  To: Daniel Henrique Barboza; +Cc: qemu-devel, qemu-ppc, clg, Edgar E . Iglesias

On Thu, 8 Sep 2022, Daniel Henrique Barboza wrote:
> This will enable support for 'dumpdtb' QMP/HMP command for the
> virtex_ml507 machine.
>
> Setting machine->fdt requires a MachineState pointer to be used inside
> xilinx_load_device_tree(). Let's change the function to receive this
> pointer from the caller. kernel_cmdline' can be retrieved directly from
> the 'machine' pointer. 'ramsize' wasn't being used so can be removed.
>
> Cc: Edgar E. Iglesias <edgar.iglesias@gmail.com>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>

Reviewed-by: BALATON Zoltan <balaton@eik.bme.hu>

> ---
> hw/ppc/virtex_ml507.c | 25 ++++++++++++++-----------
> 1 file changed, 14 insertions(+), 11 deletions(-)
>
> diff --git a/hw/ppc/virtex_ml507.c b/hw/ppc/virtex_ml507.c
> index 493ea0c19f..13cace229b 100644
> --- a/hw/ppc/virtex_ml507.c
> +++ b/hw/ppc/virtex_ml507.c
> @@ -45,6 +45,8 @@
> #include "hw/qdev-properties.h"
> #include "ppc405.h"
>
> +#include <libfdt.h>
> +
> #define EPAPR_MAGIC    (0x45504150)
> #define FLASH_SIZE     (16 * MiB)
>
> @@ -144,11 +146,10 @@ static void main_cpu_reset(void *opaque)
> }
>
> #define BINARY_DEVICE_TREE_FILE "virtex-ml507.dtb"
> -static int xilinx_load_device_tree(hwaddr addr,
> -                                      uint32_t ramsize,
> -                                      hwaddr initrd_base,
> -                                      hwaddr initrd_size,
> -                                      const char *kernel_cmdline)
> +static int xilinx_load_device_tree(MachineState *machine,
> +                                   hwaddr addr,
> +                                   hwaddr initrd_base,
> +                                   hwaddr initrd_size)
> {
>     char *path;
>     int fdt_size;
> @@ -190,18 +191,21 @@ static int xilinx_load_device_tree(hwaddr addr,
>         error_report("couldn't set /chosen/linux,initrd-end");
>     }
>
> -    r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", kernel_cmdline);
> +    r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
> +                                machine->kernel_cmdline);
>     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;
> }
>
> static void virtex_init(MachineState *machine)
> {
>     const char *kernel_filename = machine->kernel_filename;
> -    const char *kernel_cmdline = machine->kernel_cmdline;
>     hwaddr initrd_base = 0;
>     int initrd_size = 0;
>     MemoryRegion *address_space_mem = get_system_memory();
> @@ -294,9 +298,8 @@ static void virtex_init(MachineState *machine)
>         boot_info.fdt = high + (8192 * 2);
>         boot_info.fdt &= ~8191;
>
> -        xilinx_load_device_tree(boot_info.fdt, machine->ram_size,
> -                                initrd_base, initrd_size,
> -                                kernel_cmdline);
> +        xilinx_load_device_tree(machine, boot_info.fdt,
> +                                initrd_base, initrd_size);
>     }
>     env->load_info = &boot_info;
> }
>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 08/14] hw/ppc: set machine->fdt in pegasos2_machine_reset()
  2022-09-08 19:40 ` [PATCH v7 08/14] hw/ppc: set machine->fdt in pegasos2_machine_reset() Daniel Henrique Barboza
@ 2022-09-08 19:58   ` BALATON Zoltan
  2022-09-22 10:54     ` Philippe Mathieu-Daudé via
  0 siblings, 1 reply; 32+ messages in thread
From: BALATON Zoltan @ 2022-09-08 19:58 UTC (permalink / raw)
  To: Daniel Henrique Barboza; +Cc: qemu-devel, qemu-ppc, clg

On Thu, 8 Sep 2022, Daniel Henrique Barboza wrote:
> We'll introduce a QMP/HMP command that requires machine->fdt to be set
> properly.
>
> Cc: BALATON Zoltan <balaton@eik.bme.hu>
> Cc: qemu-ppc@nongnu.org
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>

Reviewed-by: BALATON Zoltan <balaton@eik.bme.hu>

> ---
> hw/ppc/pegasos2.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/hw/ppc/pegasos2.c b/hw/ppc/pegasos2.c
> index 61f4263953..ecf682b148 100644
> --- a/hw/ppc/pegasos2.c
> +++ b/hw/ppc/pegasos2.c
> @@ -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);
> }
>
>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb'
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (13 preceding siblings ...)
  2022-09-08 19:40 ` [PATCH v7 14/14] qmp/hmp, device_tree.c: introduce dumpdtb Daniel Henrique Barboza
@ 2022-09-22  9:40 ` Daniel Henrique Barboza
  2022-09-22 11:08 ` Philippe Mathieu-Daudé via
  15 siblings, 0 replies; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-22  9:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, clg

Ping

We're missing just patch 14/14. I'll leave non-acked patches behind and, if
no one is strongly against it, I'll push both the dumpdtb implementation and
the ppc parts via the ppc tree.

Alistair, I can also push the riscv bits through the ppc tree if it's easier
for you.


Thanks,

Daniel

On 9/8/22 16:40, Daniel Henrique Barboza wrote:
> Hi,
> 
> This new version implements all change requests from the v6.
> 
> - patch 5:
>    - change bamboo_load_device_tree() to use a MachineState pointer
> - patch 7:
>    - change xilinx_load_device_tree() to use a MachineState pointer
> - patch 14:
>    - placed SRST/ERST below the { }'s
>    - removed the '/tmp' reference in the command example
>    - removed all 'Requires libfdt' references
>    - changed qmp_dumpdtb() missing FDT error message to "This machine
>      doesn't have a FDT"
> - v6 link: https://lists.gnu.org/archive/html/qemu-devel/2022-09/msg00534.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       | 25 ++++++++++++++-----------
>   hw/ppc/sam460ex.c            | 21 +++++++++++----------
>   hw/ppc/spapr.c               |  3 +++
>   hw/ppc/spapr_hcall.c         |  8 ++++++++
>   hw/ppc/virtex_ml507.c        | 25 ++++++++++++++-----------
>   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, 172 insertions(+), 41 deletions(-)
> 


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 14/14] qmp/hmp, device_tree.c: introduce dumpdtb
  2022-09-08 19:40 ` [PATCH v7 14/14] qmp/hmp, device_tree.c: introduce dumpdtb Daniel Henrique Barboza
@ 2022-09-22 10:47   ` Philippe Mathieu-Daudé via
  2022-09-22 11:05     ` Philippe Mathieu-Daudé via
  2022-09-22 12:29     ` Markus Armbruster
  0 siblings, 2 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 10:47 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel
  Cc: qemu-ppc, clg, Dr . David Alan Gilbert, Markus Armbruster,
	Alistair Francis, David Gibson

On 8/9/22 21:40, Daniel Henrique Barboza 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

Typo "parameter".

> 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 sentence is odd.

> 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 <dgilbert@redhat.com>
> Cc: Markus Armbruster <armbru@redhat.com>
> Cc: Alistair Francis <alistair.francis@wdc.com>
> Cc: David Gibson <david@gibson.dropbear.id.au>
> Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>   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(+)
> 
> diff --git a/hmp-commands.hx b/hmp-commands.hx
> index 182e639d14..753669a2eb 100644
> --- a/hmp-commands.hx
> +++ b/hmp-commands.hx
> @@ -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)
> +    {
> +        .name       = "dumpdtb",
> +        .args_type  = "filename:F",
> +        .params     = "filename",
> +        .help       = "save the FDT in the 'filename' file to be decoded using dtc",
> +        .cmd        = hmp_dumpdtb,
> +    },
> +
> +SRST
> +``dumpdtb`` *filename*
> +  Save the FDT in the 'filename' file to be decoded using dtc.
> +ERST
> +#endif
> diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h
> index ef060a9759..e7c5441f56 100644
> --- a/include/sysemu/device_tree.h
> +++ b/include/sysemu/device_tree.h
> @@ -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:
> diff --git a/monitor/misc.c b/monitor/misc.c
> index 3d2312ba8d..e7dd63030b 100644
> --- a/monitor/misc.c
> +++ b/monitor/misc.c
> @@ -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"
> diff --git a/qapi/machine.json b/qapi/machine.json
> index abb2f48808..9f0c8c8374 100644
> --- a/qapi/machine.json
> +++ b/qapi/machine.json
> @@ -1664,3 +1664,21 @@
>        '*size': 'size',
>        '*max-size': 'size',
>        '*slots': 'uint64' } }
> +
> +##
> +# @dumpdtb:
> +#
> +# Save the FDT in dtb format.
> +#
> +# @filename: name of the FDT file to be created

"name of the binary FDT ..."?

> +#
> +# Since: 7.2
> +#
> +# Example:
> +#   {"execute": "dumpdtb"}
> +#    "arguments": { "filename": "fdt.dtb" } }
> +#
> +##
> +{ 'command': 'dumpdtb',
> +  'data': { 'filename': 'str' },
> +  'if': 'CONFIG_FDT' }
> diff --git a/softmmu/device_tree.c b/softmmu/device_tree.c
> index 6ca3fad285..7031dcf89d 100644
> --- a/softmmu/device_tree.c
> +++ b/softmmu/device_tree.c
> @@ -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;

fdt_totalsize() returns an uint32_t. Maybe use "unsigned" if you
don't want to use uint32_t?

> +
> +    if (!current_machine->fdt) {
> +        error_setg(errp, "This machine doesn't have a FDT");
> +        return;
> +    }
> +
> +    size = fdt_totalsize(current_machine->fdt);

        assert(size > 0); ?

> +
> +    if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
> +        error_setg(errp, "Error saving FDT to file %s: %s",
> +                   filename, err->message);
> +    }

Eventually:

        info_report("Dumped %u bytes of FDT to %s\n", size, filename);

To have a feedback in HMP.

> +}
> +
> +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: Philippe Mathieu-Daudé <f4bug@amsat.org>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 04/14] hw/ppc: set machine->fdt in ppce500_load_device_tree()
  2022-09-08 19:40 ` [PATCH v7 04/14] hw/ppc: set machine->fdt in ppce500_load_device_tree() Daniel Henrique Barboza
@ 2022-09-22 10:51   ` Philippe Mathieu-Daudé via
  0 siblings, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 10:51 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel
  Cc: qemu-ppc, clg, Peter Maydell, David Gibson

+David/Peter

On 8/9/22 21:40, Daniel Henrique Barboza wrote:
> This will enable support for 'dumpdtb' QMP/HMP command for the e500
> machine.
> 
> Cc: Cédric Le Goater <clg@kaod.org>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>   hw/ppc/e500.c | 13 ++++++++++++-
>   1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/ppc/e500.c b/hw/ppc/e500.c
> index 32495d0123..ea5f947824 100644
> --- a/hw/ppc/e500.c
> +++ b/hw/ppc/e500.c
> @@ -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,

Why are we doing that? Is it really necessary? This seems to be only 
required at cold power-on.

> so free machine->fdt
> +     * to avoid leaking the old FDT.
> +     */
> +    g_free(machine->fdt);
> +    machine->fdt = fdt;
>   
>   out:
>       g_free(pci_map);



^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 06/14] hw/ppc: set machine->fdt in sam460ex_load_device_tree()
  2022-09-08 19:40 ` [PATCH v7 06/14] hw/ppc: set machine->fdt in sam460ex_load_device_tree() Daniel Henrique Barboza
@ 2022-09-22 10:53   ` Philippe Mathieu-Daudé via
  0 siblings, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 10:53 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel; +Cc: qemu-ppc, clg, BALATON Zoltan

On 8/9/22 21:40, Daniel Henrique Barboza wrote:
> 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.
> 
> Cc: BALATON Zoltan <balaton@eik.bme.hu>
> Reviewed-by: BALATON Zoltan <balaton@eik.bme.hu>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>   hw/ppc/sam460ex.c | 21 +++++++++++----------
>   1 file changed, 11 insertions(+), 10 deletions(-)
> 

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 07/14] hw/ppc: set machine->fdt in xilinx_load_device_tree()
  2022-09-08 19:40 ` [PATCH v7 07/14] hw/ppc: set machine->fdt in xilinx_load_device_tree() Daniel Henrique Barboza
  2022-09-08 19:57   ` BALATON Zoltan
@ 2022-09-22 10:54   ` Philippe Mathieu-Daudé via
  1 sibling, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 10:54 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel; +Cc: qemu-ppc, clg, Edgar E . Iglesias

On 8/9/22 21:40, Daniel Henrique Barboza wrote:
> This will enable support for 'dumpdtb' QMP/HMP command for the
> virtex_ml507 machine.
> 
> Setting machine->fdt requires a MachineState pointer to be used inside
> xilinx_load_device_tree(). Let's change the function to receive this
> pointer from the caller. kernel_cmdline' can be retrieved directly from
> the 'machine' pointer. 'ramsize' wasn't being used so can be removed.
> 
> Cc: Edgar E. Iglesias <edgar.iglesias@gmail.com>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>   hw/ppc/virtex_ml507.c | 25 ++++++++++++++-----------
>   1 file changed, 14 insertions(+), 11 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 08/14] hw/ppc: set machine->fdt in pegasos2_machine_reset()
  2022-09-08 19:58   ` BALATON Zoltan
@ 2022-09-22 10:54     ` Philippe Mathieu-Daudé via
  0 siblings, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 10:54 UTC (permalink / raw)
  To: BALATON Zoltan, Daniel Henrique Barboza; +Cc: qemu-devel, qemu-ppc, clg

On 8/9/22 21:58, BALATON Zoltan wrote:
> On Thu, 8 Sep 2022, Daniel Henrique Barboza wrote:
>> We'll introduce a QMP/HMP command that requires machine->fdt to be set
>> properly.
>>
>> Cc: BALATON Zoltan <balaton@eik.bme.hu>
>> Cc: qemu-ppc@nongnu.org
>> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> 
> Reviewed-by: BALATON Zoltan <balaton@eik.bme.hu>
> 
>> ---
>> hw/ppc/pegasos2.c | 4 ++++
>> 1 file changed, 4 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 03/14] hw/nios2: set machine->fdt in nios2_load_dtb()
  2022-09-08 19:40 ` [PATCH v7 03/14] hw/nios2: set machine->fdt in nios2_load_dtb() Daniel Henrique Barboza
@ 2022-09-22 10:54   ` Philippe Mathieu-Daudé via
  0 siblings, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 10:54 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel
  Cc: qemu-ppc, clg, Chris Wulff, Marek Vasut

On 8/9/22 21:40, Daniel Henrique Barboza wrote:
> This will enable support for 'dumpdtb' QMP/HMP command for all nios2
> machines that uses nios2_load_dtb().
> 
> Cc: Chris Wulff <crwulff@gmail.com>
> Cc: Marek Vasut <marex@denx.de>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>   hw/nios2/boot.c      | 8 +++++++-
>   hw/nios2/meson.build | 2 +-
>   2 files changed, 8 insertions(+), 2 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 12/14] hw/riscv: set machine->fdt in spike_board_init()
  2022-09-08 19:40 ` [PATCH v7 12/14] hw/riscv: set machine->fdt in spike_board_init() Daniel Henrique Barboza
@ 2022-09-22 10:55   ` Philippe Mathieu-Daudé via
  0 siblings, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 10:55 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel
  Cc: qemu-ppc, clg, Palmer Dabbelt, Alistair Francis, Bin Meng

On 8/9/22 21:40, Daniel Henrique Barboza wrote:
> This will enable support for the 'dumpdtb' QMP/HMP command for the spike
> machine.
> 
> Cc: Palmer Dabbelt <palmer@dabbelt.com>
> Cc: Alistair Francis <alistair.francis@wdc.com>
> Cc: Bin Meng <bin.meng@windriver.com>
> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>   hw/riscv/spike.c | 6 ++++++
>   1 file changed, 6 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>



^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 11/14] hw/riscv: set machine->fdt in sifive_u_machine_init()
  2022-09-08 19:40 ` [PATCH v7 11/14] hw/riscv: set machine->fdt in sifive_u_machine_init() Daniel Henrique Barboza
@ 2022-09-22 10:56   ` Philippe Mathieu-Daudé via
  0 siblings, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 10:56 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel
  Cc: qemu-ppc, clg, Alistair Francis, Bin Meng, Palmer Dabbelt

On 8/9/22 21:40, Daniel Henrique Barboza wrote:
> This will enable support for 'dumpdtb' QMP/HMP command for the sifive_u
> machine.
> 
> Cc: Alistair Francis <Alistair.Francis@wdc.com>
> Cc: Bin Meng <bin.meng@windriver.com>
> Cc: Palmer Dabbelt <palmer@dabbelt.com>
> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>   hw/riscv/sifive_u.c | 3 +++
>   1 file changed, 3 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>



^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 10/14] hw/ppc: set machine->fdt in spapr machine
  2022-09-08 19:40 ` [PATCH v7 10/14] hw/ppc: set machine->fdt in spapr machine Daniel Henrique Barboza
@ 2022-09-22 10:56   ` Philippe Mathieu-Daudé via
  0 siblings, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 10:56 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel; +Cc: qemu-ppc, clg, David Gibson

On 8/9/22 21:40, Daniel Henrique Barboza wrote:
> 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.
> 
> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>   hw/ppc/spapr.c       | 3 +++
>   hw/ppc/spapr_hcall.c | 8 ++++++++
>   2 files changed, 11 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>



^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 14/14] qmp/hmp, device_tree.c: introduce dumpdtb
  2022-09-22 10:47   ` Philippe Mathieu-Daudé via
@ 2022-09-22 11:05     ` Philippe Mathieu-Daudé via
  2022-09-22 12:29     ` Markus Armbruster
  1 sibling, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 11:05 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel
  Cc: qemu-ppc, clg, Dr . David Alan Gilbert, Markus Armbruster,
	Alistair Francis, David Gibson

On 22/9/22 12:47, Philippe Mathieu-Daudé wrote:
> On 8/9/22 21:40, Daniel Henrique Barboza 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
> 
> Typo "parameter".
> 
>> 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 sentence is odd.
> 
>> 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 <dgilbert@redhat.com>
>> Cc: Markus Armbruster <armbru@redhat.com>
>> Cc: Alistair Francis <alistair.francis@wdc.com>
>> Cc: David Gibson <david@gibson.dropbear.id.au>
>> Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
>> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
>> ---
>>   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(+)

>> +void qmp_dumpdtb(const char *filename, Error **errp)
>> +{
>> +    g_autoptr(GError) err = NULL;
>> +    int size;
> 
> fdt_totalsize() returns an uint32_t. Maybe use "unsigned" if you
> don't want to use uint32_t?
> 
>> +
>> +    if (!current_machine->fdt) {
>> +        error_setg(errp, "This machine doesn't have a FDT");
>> +        return;
>> +    }
>> +
>> +    size = fdt_totalsize(current_machine->fdt);
> 
>         assert(size > 0); ?
> 
>> +
>> +    if (!g_file_set_contents(filename, current_machine->fdt, size, 
>> &err)) {
>> +        error_setg(errp, "Error saving FDT to file %s: %s",
>> +                   filename, err->message);
>> +    }
> 
> Eventually:
> 
>         info_report("Dumped %u bytes of FDT to %s\n", size, filename);

Or refactor qemu_fdt_dumpdtb() and call it.

> To have a feedback in HMP.
> 
>> +}
>> +
>> +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: Philippe Mathieu-Daudé <f4bug@amsat.org>



^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb'
  2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
                   ` (14 preceding siblings ...)
  2022-09-22  9:40 ` [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
@ 2022-09-22 11:08 ` Philippe Mathieu-Daudé via
  15 siblings, 0 replies; 32+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-22 11:08 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel; +Cc: qemu-ppc, clg

Hi Daniel,

On 8/9/22 21:40, Daniel Henrique Barboza wrote:
> Hi,
> 
> This new version implements all change requests from the v6.
> 
> - patch 5:
>    - change bamboo_load_device_tree() to use a MachineState pointer
> - patch 7:
>    - change xilinx_load_device_tree() to use a MachineState pointer
> - patch 14:
>    - placed SRST/ERST below the { }'s
>    - removed the '/tmp' reference in the command example
>    - removed all 'Requires libfdt' references
>    - changed qmp_dumpdtb() missing FDT error message to "This machine
>      doesn't have a FDT"
> - v6 link: https://lists.gnu.org/archive/html/qemu-devel/2022-09/msg00534.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


- What about the MIPS Boston machine?

- We need to free ms->fdt in machine_finalize().


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 14/14] qmp/hmp, device_tree.c: introduce dumpdtb
  2022-09-22 10:47   ` Philippe Mathieu-Daudé via
  2022-09-22 11:05     ` Philippe Mathieu-Daudé via
@ 2022-09-22 12:29     ` Markus Armbruster
  2022-09-24  9:48       ` Daniel Henrique Barboza
  1 sibling, 1 reply; 32+ messages in thread
From: Markus Armbruster @ 2022-09-22 12:29 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Daniel Henrique Barboza, qemu-devel, qemu-ppc, clg,
	Dr . David Alan Gilbert, Alistair Francis, David Gibson

Philippe Mathieu-Daudé <f4bug@amsat.org> writes:

> On 8/9/22 21:40, Daniel Henrique Barboza 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
>
> Typo "parameter".
>
>> 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 sentence is odd.

Seconded.

>> 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 <dgilbert@redhat.com>
>> Cc: Markus Armbruster <armbru@redhat.com>
>> Cc: Alistair Francis <alistair.francis@wdc.com>
>> Cc: David Gibson <david@gibson.dropbear.id.au>
>> Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
>> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
>> ---
>>   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(+)
>> diff --git a/hmp-commands.hx b/hmp-commands.hx
>> index 182e639d14..753669a2eb 100644
>> --- a/hmp-commands.hx
>> +++ b/hmp-commands.hx
>> @@ -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)
>> +    {
>> +        .name       = "dumpdtb",
>> +        .args_type  = "filename:F",
>> +        .params     = "filename",
>> +        .help       = "save the FDT in the 'filename' file to be decoded using dtc",

Here, you document the format as "to be decoded using dtc".  In the QAPI
schema below, you document it as "dtb format" and "FDT file".  Pick one
and stick to it, please.

"the 'filename' file" feels a bit awkward.  Suggest something "dump the
FDT in dtb format to 'filename'", similar to your phrasing in the QAPI
schema.


>> +        .cmd        = hmp_dumpdtb,
>> +    },
>> +
>> +SRST
>> +``dumpdtb`` *filename*
>> +  Save the FDT in the 'filename' file to be decoded using dtc.

*filename*, not 'filename'.

>> +ERST
>> +#endif
>> diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h
>> index ef060a9759..e7c5441f56 100644
>> --- a/include/sysemu/device_tree.h
>> +++ b/include/sysemu/device_tree.h
>> @@ -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:
>> diff --git a/monitor/misc.c b/monitor/misc.c
>> index 3d2312ba8d..e7dd63030b 100644
>> --- a/monitor/misc.c
>> +++ b/monitor/misc.c
>> @@ -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"
>> diff --git a/qapi/machine.json b/qapi/machine.json
>> index abb2f48808..9f0c8c8374 100644
>> --- a/qapi/machine.json
>> +++ b/qapi/machine.json
>> @@ -1664,3 +1664,21 @@
>>        '*size': 'size',
>>        '*max-size': 'size',
>>        '*slots': 'uint64' } }
>> +
>> +##
>> +# @dumpdtb:
>> +#
>> +# Save the FDT in dtb format.
>> +#
>> +# @filename: name of the FDT file to be created
>
> "name of the binary FDT ..."?
>
>> +#
>> +# Since: 7.2
>> +#
>> +# Example:
>> +#   {"execute": "dumpdtb"}
>> +#    "arguments": { "filename": "fdt.dtb" } }
>> +#
>> +##
>> +{ 'command': 'dumpdtb',
>> +  'data': { 'filename': 'str' },
>> +  'if': 'CONFIG_FDT' }
>> diff --git a/softmmu/device_tree.c b/softmmu/device_tree.c
>> index 6ca3fad285..7031dcf89d 100644
>> --- a/softmmu/device_tree.c
>> +++ b/softmmu/device_tree.c
>> @@ -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;
>
> fdt_totalsize() returns an uint32_t. Maybe use "unsigned" if you
> don't want to use uint32_t?

Best to avoid unnecessary conversions between signed and unsigned.

The value is passed to g_file_set_contents() below, which takes a
gssize.  uint32_t should be narrower than gssize on anything capable of
running QEMU.  So let's use that.

>> +
>> +    if (!current_machine->fdt) {
>> +        error_setg(errp, "This machine doesn't have a FDT");
>> +        return;
>> +    }
>> +
>> +    size = fdt_totalsize(current_machine->fdt);
>
>        assert(size > 0); ?
>
>> +
>> +    if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
>> +        error_setg(errp, "Error saving FDT to file %s: %s",
>> +                   filename, err->message);
>> +    }
>
> Eventually:
>
>        info_report("Dumped %u bytes of FDT to %s\n", size, filename);
>
> To have a feedback in HMP.

If feedback is desired, it needs to be done in hmp_dumpdtb().
info_report() here would make the QMP command spam stderr.

>> +}
>> +
>> +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: Philippe Mathieu-Daudé <f4bug@amsat.org>

With the commit message, the documentation, and the integer conversions
tidied up:
Reviewed-by: Markus Armbruster <armbru@redhat.com>



^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v7 14/14] qmp/hmp, device_tree.c: introduce dumpdtb
  2022-09-22 12:29     ` Markus Armbruster
@ 2022-09-24  9:48       ` Daniel Henrique Barboza
  0 siblings, 0 replies; 32+ messages in thread
From: Daniel Henrique Barboza @ 2022-09-24  9:48 UTC (permalink / raw)
  To: Markus Armbruster, Philippe Mathieu-Daudé
  Cc: qemu-devel, qemu-ppc, clg, Dr . David Alan Gilbert,
	Alistair Francis, David Gibson



On 9/22/22 09:29, Markus Armbruster wrote:
> Philippe Mathieu-Daudé <f4bug@amsat.org> writes:
> 
>> On 8/9/22 21:40, Daniel Henrique Barboza 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
>>
>> Typo "parameter".
>>
>>> 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 sentence is odd.
> 
> Seconded.

I removed it and changed the previous paragraph as follows:

The 'dumpdtb' command receives a 'filename' parameter and, if the FDT is available
via current_machine->fdt, save it in dtb format to 'filename'. In short, this is a
'-machine dumpdtb' that can be fired on demand via QMP/HMP.


> 
>>> 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 <dgilbert@redhat.com>
>>> Cc: Markus Armbruster <armbru@redhat.com>
>>> Cc: Alistair Francis <alistair.francis@wdc.com>
>>> Cc: David Gibson <david@gibson.dropbear.id.au>
>>> Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
>>> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
>>> ---
>>>    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(+)
>>> diff --git a/hmp-commands.hx b/hmp-commands.hx
>>> index 182e639d14..753669a2eb 100644
>>> --- a/hmp-commands.hx
>>> +++ b/hmp-commands.hx
>>> @@ -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)
>>> +    {
>>> +        .name       = "dumpdtb",
>>> +        .args_type  = "filename:F",
>>> +        .params     = "filename",
>>> +        .help       = "save the FDT in the 'filename' file to be decoded using dtc",
> 
> Here, you document the format as "to be decoded using dtc".  In the QAPI
> schema below, you document it as "dtb format" and "FDT file".  Pick one
> and stick to it, please.
> 
> "the 'filename' file" feels a bit awkward.  Suggest something "dump the
> FDT in dtb format to 'filename'", similar to your phrasing in the QAPI
> schema.

Changed to:

         .help       = "dump the FDT in dtb format to 'filename'",


> 
> 
>>> +        .cmd        = hmp_dumpdtb,
>>> +    },
>>> +
>>> +SRST
>>> +``dumpdtb`` *filename*
>>> +  Save the FDT in the 'filename' file to be decoded using dtc.
> 
> *filename*, not 'filename'.

Changed the sentence to:


   Dump the FDT in dtb format to *filename*.


> 
>>> +ERST
>>> +#endif
>>> diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h
>>> index ef060a9759..e7c5441f56 100644
>>> --- a/include/sysemu/device_tree.h
>>> +++ b/include/sysemu/device_tree.h
>>> @@ -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:
>>> diff --git a/monitor/misc.c b/monitor/misc.c
>>> index 3d2312ba8d..e7dd63030b 100644
>>> --- a/monitor/misc.c
>>> +++ b/monitor/misc.c
>>> @@ -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"
>>> diff --git a/qapi/machine.json b/qapi/machine.json
>>> index abb2f48808..9f0c8c8374 100644
>>> --- a/qapi/machine.json
>>> +++ b/qapi/machine.json
>>> @@ -1664,3 +1664,21 @@
>>>         '*size': 'size',
>>>         '*max-size': 'size',
>>>         '*slots': 'uint64' } }
>>> +
>>> +##
>>> +# @dumpdtb:
>>> +#
>>> +# Save the FDT in dtb format.
>>> +#
>>> +# @filename: name of the FDT file to be created
>>
>> "name of the binary FDT ..."?

Changed to:


# @filename: name of the dtb file to be created



>>
>>> +#
>>> +# Since: 7.2
>>> +#
>>> +# Example:
>>> +#   {"execute": "dumpdtb"}
>>> +#    "arguments": { "filename": "fdt.dtb" } }
>>> +#
>>> +##
>>> +{ 'command': 'dumpdtb',
>>> +  'data': { 'filename': 'str' },
>>> +  'if': 'CONFIG_FDT' }
>>> diff --git a/softmmu/device_tree.c b/softmmu/device_tree.c
>>> index 6ca3fad285..7031dcf89d 100644
>>> --- a/softmmu/device_tree.c
>>> +++ b/softmmu/device_tree.c
>>> @@ -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;
>>
>> fdt_totalsize() returns an uint32_t. Maybe use "unsigned" if you
>> don't want to use uint32_t?
> 
> Best to avoid unnecessary conversions between signed and unsigned.
> 
> The value is passed to g_file_set_contents() below, which takes a
> gssize.  uint32_t should be narrower than gssize on anything capable of
> running QEMU.  So let's use that.


Changed size to uint32_t.


> 
>>> +
>>> +    if (!current_machine->fdt) {
>>> +        error_setg(errp, "This machine doesn't have a FDT");
>>> +        return;
>>> +    }
>>> +
>>> +    size = fdt_totalsize(current_machine->fdt);
>>
>>         assert(size > 0); ?

Given that this is classified as a debug command I believe it's ok to g_assert()
if size == 0 here. Changed.


>>
>>> +
>>> +    if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
>>> +        error_setg(errp, "Error saving FDT to file %s: %s",
>>> +                   filename, err->message);
>>> +    }
>>
>> Eventually:
>>
>>         info_report("Dumped %u bytes of FDT to %s\n", size, filename);
>>
>> To have a feedback in HMP.
> 
> If feedback is desired, it needs to be done in hmp_dumpdtb().
> info_report() here would make the QMP command spam stderr.

Added the following in hmp_dumpdtb():

     if (hmp_handle_error(mon, local_err)) {
         return;
     }

     info_report("dtb dumped to %s",filename);

> 
>>> +}
>>> +
>>> +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: Philippe Mathieu-Daudé <f4bug@amsat.org>
> 
> With the commit message, the documentation, and the integer conversions
> tidied up:
> Reviewed-by: Markus Armbruster <armbru@redhat.com>

Thank you both for the review. I've added both R-bs after the changes mentioned.



Daniel

> 


^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2022-09-24  9:50 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-08 19:40 [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
2022-09-08 19:40 ` [PATCH v7 01/14] hw/arm: do not free machine->fdt in arm_load_dtb() Daniel Henrique Barboza
2022-09-08 19:40 ` [PATCH v7 02/14] hw/microblaze: set machine->fdt in microblaze_load_dtb() Daniel Henrique Barboza
2022-09-08 19:40 ` [PATCH v7 03/14] hw/nios2: set machine->fdt in nios2_load_dtb() Daniel Henrique Barboza
2022-09-22 10:54   ` Philippe Mathieu-Daudé via
2022-09-08 19:40 ` [PATCH v7 04/14] hw/ppc: set machine->fdt in ppce500_load_device_tree() Daniel Henrique Barboza
2022-09-22 10:51   ` Philippe Mathieu-Daudé via
2022-09-08 19:40 ` [PATCH v7 05/14] hw/ppc: set machine->fdt in bamboo_load_device_tree() Daniel Henrique Barboza
2022-09-08 19:55   ` BALATON Zoltan
2022-09-08 19:40 ` [PATCH v7 06/14] hw/ppc: set machine->fdt in sam460ex_load_device_tree() Daniel Henrique Barboza
2022-09-22 10:53   ` Philippe Mathieu-Daudé via
2022-09-08 19:40 ` [PATCH v7 07/14] hw/ppc: set machine->fdt in xilinx_load_device_tree() Daniel Henrique Barboza
2022-09-08 19:57   ` BALATON Zoltan
2022-09-22 10:54   ` Philippe Mathieu-Daudé via
2022-09-08 19:40 ` [PATCH v7 08/14] hw/ppc: set machine->fdt in pegasos2_machine_reset() Daniel Henrique Barboza
2022-09-08 19:58   ` BALATON Zoltan
2022-09-22 10:54     ` Philippe Mathieu-Daudé via
2022-09-08 19:40 ` [PATCH v7 09/14] hw/ppc: set machine->fdt in pnv_reset() Daniel Henrique Barboza
2022-09-08 19:40 ` [PATCH v7 10/14] hw/ppc: set machine->fdt in spapr machine Daniel Henrique Barboza
2022-09-22 10:56   ` Philippe Mathieu-Daudé via
2022-09-08 19:40 ` [PATCH v7 11/14] hw/riscv: set machine->fdt in sifive_u_machine_init() Daniel Henrique Barboza
2022-09-22 10:56   ` Philippe Mathieu-Daudé via
2022-09-08 19:40 ` [PATCH v7 12/14] hw/riscv: set machine->fdt in spike_board_init() Daniel Henrique Barboza
2022-09-22 10:55   ` Philippe Mathieu-Daudé via
2022-09-08 19:40 ` [PATCH v7 13/14] hw/xtensa: set machine->fdt in xtfpga_init() Daniel Henrique Barboza
2022-09-08 19:40 ` [PATCH v7 14/14] qmp/hmp, device_tree.c: introduce dumpdtb Daniel Henrique Barboza
2022-09-22 10:47   ` Philippe Mathieu-Daudé via
2022-09-22 11:05     ` Philippe Mathieu-Daudé via
2022-09-22 12:29     ` Markus Armbruster
2022-09-24  9:48       ` Daniel Henrique Barboza
2022-09-22  9:40 ` [PATCH v7 00/14] QMP/HMP: introduce 'dumpdtb' Daniel Henrique Barboza
2022-09-22 11:08 ` Philippe Mathieu-Daudé via

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.