qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Daniel Henrique Barboza <danielhb413@gmail.com>
Cc: qemu-devel@nongnu.org, alistair.francis@wdc.com,
	"Dr . David Alan Gilbert" <dgilbert@redhat.com>
Subject: Re: [PATCH for-7.2 v2 15/20] qmp/hmp, device_tree.c: introduce 'info fdt' command
Date: Thu, 18 Aug 2022 12:46:28 +1000	[thread overview]
Message-ID: <Yv2oBI7p1qK7pGx7@yekko> (raw)
In-Reply-To: <2e4509e2-db98-70ff-fb12-9d753127bd64@gmail.com>

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

On Mon, Aug 15, 2022 at 07:48:14PM -0300, Daniel Henrique Barboza wrote:
> 
> 
> On 8/8/22 01:21, David Gibson wrote:
> > On Fri, Aug 05, 2022 at 06:39:43AM -0300, Daniel Henrique Barboza wrote:
> > > Reading the FDT requires that the user saves the fdt_blob and then use
> > > 'dtc' to read the contents. Saving the file and using 'dtc' is a strong
> > > use case when we need to compare two FDTs, but it's a lot of steps if
> > > you want to do quick check on a certain node or property.
> > > 
> > > 'info fdt' retrieves FDT nodes (and properties, later on) and print it
> > > to the user. This can be used to check the FDT on running machines
> > > without having to save the blob and use 'dtc'.
> > > 
> > > The implementation is based on the premise that the machine thas a FDT
> > > created using libfdt and pointed by 'machine->fdt'. As long as this
> > > pre-requisite is met the machine should be able to support it.
> > > 
> > > For now we're going to add the required QMP/HMP boilerplate and the
> > > capability of printing the name of the properties of a given node. Next
> > > patches will extend 'info fdt' to be able to print nodes recursively,
> > > and then individual properties.
> > > 
> > > 'info fdt' is not something that we expect to be used aside from debugging,
> > > so we're implementing it in QMP as 'x-query-fdt'.
> > > 
> > > This is an example of 'info fdt' fetching the '/chosen' node of the
> > > pSeries machine:
> > > 
> > > (qemu) info fdt /chosen
> > > chosen {
> > >      ibm,architecture-vec-5;
> > >      rng-seed;
> > >      ibm,arch-vec-5-platform-support;
> > >      linux,pci-probe-only;
> > >      stdout-path;
> > >      linux,stdout-path;
> > >      qemu,graphic-depth;
> > >      qemu,graphic-height;
> > >      qemu,graphic-width;
> > > }
> > > 
> > > And the same node for the aarch64 'virt' machine:
> > > 
> > > (qemu) info fdt /chosen
> > > chosen {
> > >      stdout-path;
> > >      rng-seed;
> > >      kaslr-seed;
> > > }
> > 
> > So... it's listing the names of the properties, but not the contents?
> > That seems kind of odd.
> > 
> > > 
> > > Cc: Dr. David Alan Gilbert <dgilbert@redhat.com>
> > > Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> > > ---
> > >   hmp-commands-info.hx         | 13 ++++++++++
> > >   include/monitor/hmp.h        |  1 +
> > >   include/sysemu/device_tree.h |  4 +++
> > >   monitor/hmp-cmds.c           | 13 ++++++++++
> > >   monitor/qmp-cmds.c           | 12 +++++++++
> > >   qapi/machine.json            | 19 +++++++++++++++
> > >   softmmu/device_tree.c        | 47 ++++++++++++++++++++++++++++++++++++
> > >   7 files changed, 109 insertions(+)
> > > 
> > > diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
> > > index 188d9ece3b..743b48865d 100644
> > > --- a/hmp-commands-info.hx
> > > +++ b/hmp-commands-info.hx
> > > @@ -921,3 +921,16 @@ SRST
> > >     ``stats``
> > >       Show runtime-collected statistics
> > >   ERST
> > > +
> > > +    {
> > > +        .name       = "fdt",
> > > +        .args_type  = "nodepath:s",
> > > +        .params     = "nodepath",
> > > +        .help       = "show firmware device tree node given its path",
> > > +        .cmd        = hmp_info_fdt,
> > > +    },
> > > +
> > > +SRST
> > > +  ``info fdt``
> > > +    Show a firmware device tree node given its path. Requires libfdt.
> > > +ERST
> > > diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
> > > index d7f324da59..c0883dd1e3 100644
> > > --- a/include/monitor/hmp.h
> > > +++ b/include/monitor/hmp.h
> > > @@ -135,6 +135,7 @@ void hmp_set_vcpu_dirty_limit(Monitor *mon, const QDict *qdict);
> > >   void hmp_cancel_vcpu_dirty_limit(Monitor *mon, const QDict *qdict);
> > >   void hmp_info_vcpu_dirty_limit(Monitor *mon, const QDict *qdict);
> > >   void hmp_dumpdtb(Monitor *mon, const QDict *qdict);
> > > +void hmp_info_fdt(Monitor *mon, const QDict *qdict);
> > >   void hmp_human_readable_text_helper(Monitor *mon,
> > >                                       HumanReadableText *(*qmp_handler)(Error **));
> > >   void hmp_info_stats(Monitor *mon, const QDict *qdict);
> > > diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h
> > > index bf7684e4ed..057d13e397 100644
> > > --- a/include/sysemu/device_tree.h
> > > +++ b/include/sysemu/device_tree.h
> > > @@ -14,6 +14,8 @@
> > >   #ifndef DEVICE_TREE_H
> > >   #define DEVICE_TREE_H
> > > +#include "qapi/qapi-types-common.h"
> > > +
> > >   void *create_device_tree(int *sizep);
> > >   void *load_device_tree(const char *filename_path, int *sizep);
> > >   #ifdef CONFIG_LINUX
> > > @@ -137,6 +139,8 @@ int qemu_fdt_add_path(void *fdt, const char *path);
> > >   void qemu_fdt_dumpdtb(void *fdt, int size);
> > >   void qemu_fdt_qmp_dumpdtb(const char *filename, Error **errp);
> > > +HumanReadableText *qemu_fdt_qmp_query_fdt(const char *nodepath,
> > > +                                          Error **errp);
> > >   /**
> > >    * qemu_fdt_setprop_sized_cells_from_array:
> > > diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
> > > index d23ec85f9d..accde90380 100644
> > > --- a/monitor/hmp-cmds.c
> > > +++ b/monitor/hmp-cmds.c
> > > @@ -2484,3 +2484,16 @@ void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
> > >           error_report_err(local_err);
> > >       }
> > >   }
> > > +
> > > +void hmp_info_fdt(Monitor *mon, const QDict *qdict)
> > > +{
> > > +    const char *nodepath = qdict_get_str(qdict, "nodepath");
> > > +    Error *err = NULL;
> > > +    g_autoptr(HumanReadableText) info = qmp_x_query_fdt(nodepath, &err);
> > > +
> > > +    if (hmp_handle_error(mon, err)) {
> > > +        return;
> > > +    }
> > > +
> > > +    monitor_printf(mon, "%s", info->human_readable_text);
> > > +}
> > > diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c
> > > index 8415aca08c..db2c6aa7da 100644
> > > --- a/monitor/qmp-cmds.c
> > > +++ b/monitor/qmp-cmds.c
> > > @@ -603,9 +603,21 @@ void qmp_dumpdtb(const char *filename, Error **errp)
> > >   {
> > >       return qemu_fdt_qmp_dumpdtb(filename, errp);
> > >   }
> > > +
> > > +HumanReadableText *qmp_x_query_fdt(const char *nodepath, Error **errp)
> > > +{
> > > +    return qemu_fdt_qmp_query_fdt(nodepath, errp);
> > > +}
> > >   #else
> > >   void qmp_dumpdtb(const char *filename, Error **errp)
> > >   {
> > >       error_setg(errp, "dumpdtb requires libfdt");
> > >   }
> > > +
> > > +HumanReadableText *qmp_x_query_fdt(const char *nodepath, Error **errp)
> > > +{
> > > +    error_setg(errp, "this command requires libfdt");
> > > +
> > > +    return NULL;
> > > +}
> > >   #endif
> > > diff --git a/qapi/machine.json b/qapi/machine.json
> > > index aeb013f3dd..96cff541ca 100644
> > > --- a/qapi/machine.json
> > > +++ b/qapi/machine.json
> > > @@ -1681,3 +1681,22 @@
> > >   ##
> > >   { 'command': 'dumpdtb',
> > >     'data': { 'filename': 'str' } }
> > > +
> > > +##
> > > +# @x-query-fdt:
> > > +#
> > > +# Query for FDT element (node or property). Requires 'libfdt'.
> > > +#
> > > +# @nodepath: the path of the FDT node to be retrieved
> > > +#
> > > +# Features:
> > > +# @unstable: This command is meant for debugging.
> > > +#
> > > +# Returns: FDT node
> > > +#
> > > +# Since: 7.2
> > > +##
> > > +{ 'command': 'x-query-fdt',
> > > +  'data': { 'nodepath': 'str' },
> > > +  'returns': 'HumanReadableText',
> > > +  'features': [ 'unstable' ]  }
> > 
> > 
> > A QMP command that returns human readable text, rather than something
> > JSON structured seems... odd.
> > 
> > Admittedly, *how* you'd JSON structure the results gets a bit tricky.
> > Listing nodes or property names would be easy enough, but getting the
> > property contents is hairy, since JSON strings are supposed to be
> > Unicode, but DT properties can be arbitrary bytestrings.
> > 
> > > diff --git a/softmmu/device_tree.c b/softmmu/device_tree.c
> > > index cd487ddd4d..3fb07b537f 100644
> > > --- a/softmmu/device_tree.c
> > > +++ b/softmmu/device_tree.c
> > > @@ -18,6 +18,7 @@
> > >   #endif
> > >   #include "qapi/error.h"
> > > +#include "qapi/type-helpers.h"
> > >   #include "qemu/error-report.h"
> > >   #include "qemu/option.h"
> > >   #include "qemu/bswap.h"
> > > @@ -661,3 +662,49 @@ void qemu_fdt_qmp_dumpdtb(const char *filename, Error **errp)
> > >       error_setg(errp, "Error when saving machine FDT to file %s", filename);
> > >   }
> > > +
> > > +static void fdt_format_node(GString *buf, int node, int depth)
> > > +{
> > > +    const struct fdt_property *prop = NULL;
> > > +    const char *propname = NULL;
> > > +    void *fdt = current_machine->fdt;
> > > +    int padding = depth * 4;
> > > +    int property = 0;
> > > +    int prop_size;
> > > +
> > > +    g_string_append_printf(buf, "%*s%s {\n", padding, "",
> > > +                           fdt_get_name(fdt, node, NULL));
> > > +
> > > +    padding += 4;
> > > +
> > > +    fdt_for_each_property_offset(property, fdt, node) {
> > > +        prop = fdt_get_property_by_offset(fdt, property, &prop_size);
> > > +        propname = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
> > > +
> > > +        g_string_append_printf(buf, "%*s%s;\n", padding, "", propname);
> > > +    }
> > > +
> > > +    padding -= 4;
> > > +    g_string_append_printf(buf, "%*s}\n", padding, "");
> > 
> > So, this lists it in dts format... kind of.  Because you don't include
> > the property values, it makes it look like all properties are binary
> > (either absent or present-but-empty).  I think that's misleading.  If
> > you're only going to list properties, I think you'd be better off
> > using different formatting (and maybe a more specific command name as
> > well).
> 
> As you've already noticed in the next patch, I decided to split between QMP/HMP
> introduction and the dts formatting to avoid clogging everything in a single patch.
> In the end the whole fdt tree can be printed with all the properties types.
> 
> As for using HumanReadableText, I tried to imagine a structured output for
> 'info fdt'. It would be something like:
> 
> - struct Property: an union of the possible types (none, string, uint32 array,
> byte array)

I don't think it's wise to try to interpret the property any further
than a raw bytestring in a machine oriented interface.  You can't
really know the type of a property without knowing all the DT bindings
that apply to it, what we do for formatting in this patch series, and
in dtc is a heuristic only.  That's useful for debuging output aimed
at humans, but not a good idea for a structured interface.

Note that even for the human interface it might be a better compromise
to just always emit properties essentially as a hexdump.  I wouldn't
suggest approximating dts format in this case though, to make the
distinction clearer.

> - struct Node: an array of properties and subnodes
> 
> And then 'info fdt <node>' would return a struct node and  'info fdt <node> <prop>'
> would return a struct Property

Well, for a "native" QMP interface, I'd suggest splitting up the
operations more.  Say "fdt-ls" (takes a path, returns array of subnode
names), "fdt-properties" (takes a path, returns array of property names), and getprop
(takes a path and a property name, returns bytestring).  I guess a
"dump" operation of some sort to get the whole thing without a million
commands might be a good idea too.

> Adding this stable ABI sounded like too much extra work for a command that would be
> used mostly for debug/development purposes. Every other command that outputs
> internal guest state (info roms, info rdma, info irq, info numa ...) are happy with
> returning HumanReadableFormat and being considered debug only. I decided to do the
> same thing.

Ok, I hadn't realized there was precedent for this approach.  If
that's the case I'm less bothered by this.

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2022-08-18  6:55 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-05  9:39 [PATCH for-7.2 v2 00/20] QMP/HMP: add 'dumpdtb' and 'info fdt' commands Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 01/20] hw/arm: do not free machine->fdt in arm_load_dtb() Daniel Henrique Barboza
2022-08-08  3:23   ` David Gibson
2022-08-08 23:00     ` Daniel Henrique Barboza
2022-08-12 22:03     ` Daniel Henrique Barboza
2022-08-15  2:36       ` David Gibson
2022-08-05  9:39 ` [PATCH for-7.2 v2 02/20] hw/microblaze: set machine->fdt in microblaze_load_dtb() Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 03/20] hw/nios2: set machine->fdt in nios2_load_dtb() Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 04/20] hw/ppc: set machine->fdt in ppce500_load_device_tree() Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 05/20] hw/ppc: set machine->fdt in bamboo_load_device_tree() Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 06/20] hw/ppc: set machine->fdt in sam460ex_load_device_tree() Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 07/20] hw/ppc: set machine->fdt in xilinx_load_device_tree() Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 08/20] hw/ppc: set machine->fdt in pegasos2_machine_reset() Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 09/20] hw/ppc: set machine->fdt in pnv_reset() Daniel Henrique Barboza
2022-08-05 11:03   ` Frederic Barrat
2022-08-05 12:31     ` Daniel Henrique Barboza
2022-08-08  3:25       ` David Gibson
2022-08-08  6:47   ` Cédric Le Goater
2022-08-08  7:13     ` Cédric Le Goater
2022-08-10 19:30       ` Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 10/20] hw/ppc: set machine->fdt in spapr machine Daniel Henrique Barboza
2022-08-08  3:26   ` David Gibson
2022-08-12 22:23     ` Daniel Henrique Barboza
2022-08-15  2:37       ` David Gibson
2022-08-19  2:11   ` Alexey Kardashevskiy
2022-08-19  2:33     ` David Gibson
2022-08-19  9:42     ` Daniel Henrique Barboza
2022-08-22  3:05       ` David Gibson
2022-08-22  3:29         ` Alexey Kardashevskiy
2022-08-22 10:30           ` Daniel Henrique Barboza
2022-08-23  8:58             ` Alexey Kardashevskiy
2022-08-23 18:09               ` Daniel Henrique Barboza
2022-09-01  1:57             ` David Gibson
2022-08-05  9:39 ` [PATCH for-7.2 v2 11/20] hw/riscv: set machine->fdt in sifive_u_machine_init() Daniel Henrique Barboza
2022-08-07 22:46   ` Alistair Francis
2022-08-05  9:39 ` [PATCH for-7.2 v2 12/20] hw/riscv: set machine->fdt in spike_board_init() Daniel Henrique Barboza
2022-08-07 22:46   ` Alistair Francis
2022-08-05  9:39 ` [PATCH for-7.2 v2 13/20] hw/xtensa: set machine->fdt in xtfpga_init() Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 14/20] qmp/hmp, device_tree.c: introduce dumpdtb Daniel Henrique Barboza
2022-08-07 23:02   ` Alistair Francis
2022-08-08  3:30   ` David Gibson
2022-08-15 17:36     ` Daniel Henrique Barboza
2022-08-15 18:31       ` Dr. David Alan Gilbert
2022-08-15 19:20         ` Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 15/20] qmp/hmp, device_tree.c: introduce 'info fdt' command Daniel Henrique Barboza
2022-08-08  4:21   ` David Gibson
2022-08-15 22:48     ` Daniel Henrique Barboza
2022-08-18  2:46       ` David Gibson [this message]
2022-08-05  9:39 ` [PATCH for-7.2 v2 16/20] device_tree.c: support string props in fdt_format_node() Daniel Henrique Barboza
2022-08-08  4:36   ` David Gibson
2022-08-10 19:40     ` Daniel Henrique Barboza
2022-08-11  4:09       ` David Gibson
2022-08-05  9:39 ` [PATCH for-7.2 v2 17/20] device_tree.c: support remaining FDT prop types Daniel Henrique Barboza
2022-08-08  4:40   ` David Gibson
2022-08-05  9:39 ` [PATCH for-7.2 v2 18/20] device_node.c: enable 'info fdt' to print subnodes Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 19/20] device_tree.c: add fdt_format_property() helper Daniel Henrique Barboza
2022-08-05  9:39 ` [PATCH for-7.2 v2 20/20] hmp, device_tree.c: add 'info fdt <property>' support Daniel Henrique Barboza
2022-08-15 18:38   ` Dr. David Alan Gilbert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Yv2oBI7p1qK7pGx7@yekko \
    --to=david@gibson.dropbear.id.au \
    --cc=alistair.francis@wdc.com \
    --cc=danielhb413@gmail.com \
    --cc=dgilbert@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).