Skip to contentSkip to conversation

generic loader FDT support (for direct Xen boot)

RFC PATCH In review Alex Bennée 5 participants 13 messages Oct 9, 2020 – Oct 14, 2020 RISC-V

lore
Share & tools
lore
#0 lore raw

cover generic loader FDT support (for direct Xen boot)

Hi,

This series adds the ability to append FDT data for blobs loaded by the generic loader. My principle use-case was to be able to directly boot Xen with a kernel image which avoided having to:

- get the kernel image into my system image
- deal with bugs in FDT munging by -bios firmware and/or grub

as such this currently a developer hack that makes *my* life easy and is thus presented as an RFC for discussion. While I've tested it with Xen I'm sure the approach would be applicable to other hypervisors or firmware which expect to consume FDT data pointing at various blobs.

An example command line that launches this is (magic from -kernel):

  ./aarch64-softmmu/qemu-system-aarch64 -cpu cortex-a57 \
    -machine type=virt,virtualization=on -display none \
    -serial mon:stdio \
    -netdev user,id=unet,hostfwd=tcp::2222-:22 \
    -device virtio-net-pci,netdev=unet,id=virt-net,disable-legacy=on \
    -device virtio-scsi-pci,id=virt-scsi,disable-legacy=on \
    -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-buster-arm64 \
    -device scsi-hd,drive=hd,id=virt-scsi-hd \
    -smp 4 -m 4096 \
    -kernel ~/lsrc/xen.git/xen/xen \
    -append "dom0_mem=1G,max:1G loglvl=all guest_loglvl=all" \
    -device loader,addr=0x47000000,file=$HOME/lsrc/linux.git/builds/arm64/arch/arm64/boot/Image,len-fdt-compat=2,fdt-compat[0]='multiboot,,module',fdt-compat[1]='multiboot,,kernel',fdt-bootargs="root=/dev/sda2 ro console=hvc0 earlyprintk=xen"

So a couple of choices I've made doing this:

Promoting *fdt to MachineState

This seemed the simplest approach to making the fdt available to the global state, especially as MachineState already has a *dtb pointer. I've converted the ARM virt machine and a RISCV machine but if this is acceptable I can convert the others.

"Polluting" the generic loader arguments

This was mainly out of convenience as the loader already knows the size of the blob being loaded. However you could certainly argue it makes sense to have a more generic "FDT expander" virtual device that could for example query the QOM model somehow to find the details it needs.

FDT isn't the only way of passing system information up the boot chain. We could reasonably want to do a similar thing with ACPI which is what should be being used on SBSA like devices to communicate with the hypervisor.

Also relying on ,, in the QemuOpt parser is ugly. It might be worth having better quoting support if I press on with this.

So what do people think? Something worth developing?

Alex Bennée (4):
hw/board: promote fdt from ARM VirtMachineState to MachineState hw/riscv: migrate fdt field to generic MachineState
device_tree: add qemu_fdt_setprop_string_array helper
generic_loader: allow the insertion of /chosen/module stanzas

 include/hw/arm/virt.h            |   1 -
 include/hw/boards.h              |   1 +
 include/hw/core/generic-loader.h |   4 +
 include/hw/riscv/virt.h          |   1 -
 include/sysemu/device_tree.h     |  17 ++
 device_tree.c                    |  26 +++
 hw/arm/virt.c                    | 322 ++++++++++++++++---------------
 hw/core/generic-loader.c         |  42 ++++
 hw/riscv/virt.c                  |  18 +-
 9 files changed, 268 insertions(+), 164 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

RFC PATCH 1/4 hw/board: promote fdt from ARM VirtMachineState to MachineState

The use of FDT's is quite common across our various platforms. To allow the generic loader to tweak it we need to make it available in the generic state. This creates the field and migrates the initial user to use the generic field. Other boards will be updated in later patches.

---
 include/hw/arm/virt.h |   1 -
 include/hw/boards.h   |   1 +
 hw/arm/virt.c         | 322 ++++++++++++++++++++++--------------------
 3 files changed, 170 insertions(+), 154 deletions(-)
include/hw/arm/virt.h +0 -1
index aad6d69841..648e5d6791 100644
@@ -152,7 +152,6 @@ struct VirtMachineState {
     char *pciehb_nodename;
     const int *irqmap;
     int smp_cpus;
-    void *fdt;
     int fdt_size;
     uint32_t clock_phandle;
     uint32_t gic_phandle;
include/hw/boards.h +1 -0
index bf53e8a16e..4da71decf2 100644
@@ -257,6 +257,7 @@ struct MachineState {
 
     /*< public >*/
 
+    void *fdt;
     char *dtb;
     char *dumpdtb;
     int phandle_start;
hw/arm/virt.c +169 -153

706 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

RFC PATCH 2/4 hw/riscv: migrate fdt field to generic MachineState

In reply to Alex Bennée

This is a mechanical change to make the fdt available through MachineState.

---
 include/hw/riscv/virt.h |  1 -
 hw/riscv/virt.c         | 18 +++++++++---------
 2 files changed, 9 insertions(+), 10 deletions(-)
include/hw/riscv/virt.h +0 -1
index b4ed9a32eb..6505ae8d23 100644
@@ -41,7 +41,6 @@ struct RISCVVirtState {
     DeviceState *plic[VIRT_SOCKETS_MAX];
     PFlashCFI01 *flash[2];
 
-    void *fdt;
     int fdt_size;
 };
 
hw/riscv/virt.c +9 -9
index 41bd2f38ba..17c0706156 100644
@@ -194,7 +194,7 @@ static void create_fdt(RISCVVirtState *s, const struct MemmapEntry *memmap,
     hwaddr flashsize = virt_memmap[VIRT_FLASH].size / 2;
     hwaddr flashbase = virt_memmap[VIRT_FLASH].base;
 
-    fdt = s->fdt = create_device_tree(&s->fdt_size);
+    fdt = mc->fdt = create_device_tree(&s->fdt_size);
     if (!fdt) {
         error_report("create_device_tree() failed");
         exit(1);
@@ -434,12 +434,12 @@ static void create_fdt(RISCVVirtState *s, const struct MemmapEntry *memmap,
     g_free(name);
 
     name = g_strdup_printf("/soc/flash@%" PRIx64, flashbase);
-    qemu_fdt_add_subnode(s->fdt, name);
-    qemu_fdt_setprop_string(s->fdt, name, "compatible", "cfi-flash");
-    qemu_fdt_setprop_sized_cells(s->fdt, name, "reg",
+    qemu_fdt_add_subnode(mc->fdt, name);
+    qemu_fdt_setprop_string(mc->fdt, name, "compatible", "cfi-flash");
+    qemu_fdt_setprop_sized_cells(mc->fdt, name, "reg",
                                  2, flashbase, 2, flashsize,
                                  2, flashbase + flashsize, 2, flashsize);
-    qemu_fdt_setprop_cell(s->fdt, name, "bank-width", 4);
+    qemu_fdt_setprop_cell(mc->fdt, name, "bank-width", 4);
     g_free(name);
 }
 
@@ -613,9 +613,9 @@ static void virt_machine_init(MachineState *machine)
             hwaddr end = riscv_load_initrd(machine->initrd_filename,
                                            machine->ram_size, kernel_entry,
                                            &start);
-            qemu_fdt_setprop_cell(s->fdt, "/chosen",
+            qemu_fdt_setprop_cell(machine->fdt, "/chosen",
                                   "linux,initrd-start", start);
-            qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end",
+            qemu_fdt_setprop_cell(machine->fdt, "/chosen", "linux,initrd-end",
                                   end);
         }
     } else {
@@ -636,11 +636,11 @@ static void virt_machine_init(MachineState *machine)
 
     /* Compute the fdt load address in dram */
     fdt_load_addr = riscv_load_fdt(memmap[VIRT_DRAM].base,
-                                   machine->ram_size, s->fdt);
+                                   machine->ram_size, machine->fdt);
     /* load the reset vector */
     riscv_setup_rom_reset_vec(start_addr, virt_memmap[VIRT_MROM].base,
                               virt_memmap[VIRT_MROM].size, kernel_entry,
-                              fdt_load_addr, s->fdt);
+                              fdt_load_addr, machine->fdt);
 
     /* SiFive Test MMIO device */
     sifive_test_create(memmap[VIRT_TEST].base);
Signature · 1 line
2.20.1
#3 lore raw

RFC PATCH 3/4 device_tree: add qemu_fdt_setprop_string_array helper

In reply to Alex Bennée

A string array in device tree is simply a series of \0 terminated strings next to each other. As libfdt doesn't support that directly we need to build it ourselves.

---
 include/sysemu/device_tree.h | 17 +++++++++++++++++
 device_tree.c                | 26 ++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
include/sysemu/device_tree.h +17 -0
index 982c89345f..8a2fe55622 100644
@@ -70,6 +70,23 @@ int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
                          const char *property, uint64_t val);
 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
                             const char *property, const char *string);
+
+/**
+ * qemu_fdt_setprop_string_array: set a string array property
+ *
+ * @fdt: pointer to the dt blob
+ * @name: node name
+ * @prop: property array
+ * @array: pointer to an array of string pointers
+ * @len: length of array
+ *
+ * assigns a string array to a property. This function converts and
+ * array of strings to a sequential string with \0 separators before
+ * setting the property.
+ */
+int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
+                                  const char *prop, char **array, int len);
+
 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
                              const char *property,
                              const char *target_node_path);
device_tree.c +26 -0
index b335dae707..a19873316a 100644
@@ -21,6 +21,7 @@
 #include "qemu/error-report.h"
 #include "qemu/option.h"
 #include "qemu/bswap.h"
+#include "qemu/cutils.h"
 #include "sysemu/device_tree.h"
 #include "sysemu/sysemu.h"
 #include "hw/loader.h"
@@ -397,6 +398,31 @@ int qemu_fdt_setprop_string(void *fdt, const char *node_path,
     return r;
 }
 
+/*
+ * libfdt doesn't allow us to add string arrays directly but they are
+ * test a series of null terminated strings with a length. We build
+ * the string up here so we can calculate the final length.
+ */
+int qemu_fdt_setprop_string_array(void *fdt, const char *node_path, const char *prop,
+                                  char **array, int len)
+{
+    int ret, i, total_len = 0;
+    char *str, *p;
+    for (i = 0; i < len; i++) {
+        total_len += strlen(array[i]) + 1;
+    }
+    p = str = g_malloc0(total_len);
+    for (i = 0; i < len; i++) {
+        int len = strlen(array[i]) + 1;
+        pstrcpy(p, len, array[i]);
+        p += len;
+    }
+
+    ret = qemu_fdt_setprop(fdt, node_path, prop, str, total_len);
+    g_free(str);
+    return ret;
+}
+
 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
                              const char *property, int *lenp, Error **errp)
 {
Signature · 1 line
2.20.1
#4 lore raw

RFC PATCH 4/4 generic_loader: allow the insertion of /chosen/module stanzas

In reply to Alex Bennée

The /chosen FDT node is how the firmware indicates information about the kernel to the loader code. In a full boot chain this would come from something like a boot loader. However if we use the generic loader to load blobs into RAM before launching a hypervisor for example we can boot directly:

  $QEMU $ARGS  -kernel ~/xen.git/xen/xen \
    -append "dom0_mem=1G,max:1G loglvl=all guest_loglvl=all" \
    -device loader,addr=0x47000000,\
    file=Image,\
    len-fdt-compat=2,\
    fdt-compat[0]='multiboot,,module',\
    fdt-compat[1]='multiboot,,kernel',\
    fdt-bootargs="root=/dev/mapper/vg0-root ro console=hvc0 earlyprintk=xen"

Note the ,, escapes required for the command line parser.

---
 include/hw/core/generic-loader.h |  4 +++
 hw/core/generic-loader.c         | 42 ++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
include/hw/core/generic-loader.h +4 -0
index 19d87b39c8..98b0452430 100644
@@ -39,6 +39,10 @@ struct GenericLoaderState {
     bool force_raw;
     bool data_be;
     bool set_pc;
+
+    char **fdt_compat;
+    uint32_t fdt_compat_count;
+    char *fdt_bootargs;
 };
 
 #define TYPE_GENERIC_LOADER "loader"
hw/core/generic-loader.c +42 -0

66 lines not on this page · raw message

Signature · 1 line
2.20.1
#5 lore raw
In reply to Alex Bennée
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [RFC PATCH 0/4] generic loader FDT support (for direct Xen boot)

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
8fbccbe generic_loader: allow the insertion of /chosen/module stanzas 2baf07a device_tree: add qemu_fdt_setprop_string_array helper dd6eea3 hw/riscv: migrate fdt field to generic MachineState dd8ee86 hw/board: promote fdt from ARM VirtMachineState to MachineState

=== OUTPUT BEGIN ===
1/4 Checking commit dd8ee86f5cdf (hw/board: promote fdt from ARM VirtMachineState to MachineState) 2/4 Checking commit dd6eea3e6e34 (hw/riscv: migrate fdt field to generic MachineState) 3/4 Checking commit 2baf07a7abfc (device_tree: add qemu_fdt_setprop_string_array helper)
WARNING: line over 80 characters
#35: FILE: device_tree.c:406:
+int qemu_fdt_setprop_string_array(void *fdt, const char *node_path, const char *prop,

total: 0 errors, 1 warnings, 61 lines checked

Patch 3/4 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
4/4 Checking commit 8fbccbec9628 (generic_loader: allow the insertion of /chosen/module stanzas)
ERROR: Don't use '#' flag of printf format ('%#') in format strings, use '0x' prefix instead #51: FILE: hw/core/generic-loader.c:73:
+ g_autofree char *node = g_strdup_printf("/chosen/module@%#08lx", s->addr);

WARNING: line over 80 characters
#71: FILE: hw/core/generic-loader.c:93:
+ if (qemu_fdt_setprop_string(fdt, node, "bootargs", s->fdt_bootargs) < 0) {

total: 1 errors, 1 warnings, 76 lines checked

Patch 4/4 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#6 lore raw
In reply to Alex Bennée
Quoted 7 lines · Alex Bennée

Hi,

This series adds the ability to append FDT data for blobs loaded by the generic loader. My principle use-case was to be able to directly boot Xen with a kernel image which avoided having to:

Quoted 7 lines

- get the kernel image into my system image
- deal with bugs in FDT munging by -bios firmware and/or grub

as such this currently a developer hack that makes *my* life easy and is thus presented as an RFC for discussion. While I've tested it with Xen I'm sure the approach would be applicable to other hypervisors or firmware which expect to consume FDT data pointing at various blobs.

An interesting idea. I think this comes up enough that it's worth thinking about.

Quoted 15 lines

An example command line that launches this is (magic from -kernel):

  ./aarch64-softmmu/qemu-system-aarch64 -cpu cortex-a57 \
    -machine type=virt,virtualization=on -display none \
    -serial mon:stdio \
    -netdev user,id=unet,hostfwd=tcp::2222-:22 \
    -device virtio-net-pci,netdev=unet,id=virt-net,disable-legacy=on \
    -device virtio-scsi-pci,id=virt-scsi,disable-legacy=on \
    -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-buster-arm64 \
    -device scsi-hd,drive=hd,id=virt-scsi-hd \
    -smp 4 -m 4096 \
    -kernel ~/lsrc/xen.git/xen/xen \
    -append "dom0_mem=1G,max:1G loglvl=all guest_loglvl=all" \
    -device loader,addr=0x47000000,file=$HOME/lsrc/linux.git/builds/arm64/arch/arm64/boot/Image,len-fdt-compat=2,fdt-compat[0]='multiboot,,module',fdt-compat[1]='multiboot,,kernel',fdt-bootargs="root=/dev/sda2 ro console=hvc0 earlyprintk=xen"

You are loading the kernel Image file, but changing the device tree that was generated by QEMU and is being loaded in memory. As a user that is really confusing.

Quoted 9 lines

So a couple of choices I've made doing this:

Promoting *fdt to MachineState

This seemed the simplest approach to making the fdt available to the global state, especially as MachineState already has a *dtb pointer. I've converted the ARM virt machine and a RISCV machine but if this is acceptable I can convert the others.

This seems fine to me.

Quoted 8 lines

"Polluting" the generic loader arguments

This was mainly out of convenience as the loader already knows the size of the blob being loaded. However you could certainly argue it makes sense to have a more generic "FDT expander" virtual device that could for example query the QOM model somehow to find the details it needs.

That seems like a better option. Why not have a generic way to modify the device tree with a specific argument? It could either be -device loader,file=fdt,... or -fdt ...

Alistair

Quoted 33 lines

FDT isn't the only way of passing system information up the boot chain. We could reasonably want to do a similar thing with ACPI which is what should be being used on SBSA like devices to communicate with the hypervisor.

Also relying on ,, in the QemuOpt parser is ugly. It might be worth having better quoting support if I press on with this.

So what do people think? Something worth developing?

Alex Bennée (4):
hw/board: promote fdt from ARM VirtMachineState to MachineState hw/riscv: migrate fdt field to generic MachineState
device_tree: add qemu_fdt_setprop_string_array helper
generic_loader: allow the insertion of /chosen/module stanzas

 include/hw/arm/virt.h            |   1 -
 include/hw/boards.h              |   1 +
 include/hw/core/generic-loader.h |   4 +
 include/hw/riscv/virt.h          |   1 -
 include/sysemu/device_tree.h     |  17 ++
 device_tree.c                    |  26 +++
 hw/arm/virt.c                    | 322 ++++++++++++++++---------------
 hw/core/generic-loader.c         |  42 ++++
 hw/riscv/virt.c                  |  18 +-
 9 files changed, 268 insertions(+), 164 deletions(-)

--
2.20.1

#7 lore raw
Quoted 35 lines · Alistair Francis
Quoted 7 lines · Alex Bennée

Hi,

This series adds the ability to append FDT data for blobs loaded by the generic loader. My principle use-case was to be able to directly boot Xen with a kernel image which avoided having to:

Quoted 7 lines

- get the kernel image into my system image
- deal with bugs in FDT munging by -bios firmware and/or grub

as such this currently a developer hack that makes *my* life easy and is thus presented as an RFC for discussion. While I've tested it with Xen I'm sure the approach would be applicable to other hypervisors or firmware which expect to consume FDT data pointing at various blobs.

An interesting idea. I think this comes up enough that it's worth thinking about.

Quoted 15 lines

An example command line that launches this is (magic from -kernel):

  ./aarch64-softmmu/qemu-system-aarch64 -cpu cortex-a57 \
    -machine type=virt,virtualization=on -display none \
    -serial mon:stdio \
    -netdev user,id=unet,hostfwd=tcp::2222-:22 \
    -device virtio-net-pci,netdev=unet,id=virt-net,disable-legacy=on \
    -device virtio-scsi-pci,id=virt-scsi,disable-legacy=on \
    -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-buster-arm64 \
    -device scsi-hd,drive=hd,id=virt-scsi-hd \
    -smp 4 -m 4096 \
    -kernel ~/lsrc/xen.git/xen/xen \
    -append "dom0_mem=1G,max:1G loglvl=all guest_loglvl=all" \
    -device loader,addr=0x47000000,file=$HOME/lsrc/linux.git/builds/arm64/arch/arm64/boot/Image,len-fdt-compat=2,fdt-compat[0]='multiboot,,module',fdt-compat[1]='multiboot,,kernel',fdt-bootargs="root=/dev/sda2 ro console=hvc0 earlyprintk=xen"
Quoted 3 lines

You are loading the kernel Image file, but changing the device tree that was generated by QEMU and is being loaded in memory. As a user that is really confusing.

Well in this case the "kernel" is Xen which helpfully comes with a kernel compatible header that the kernel loaded understand. The blob could be any Dom0 kernel - it just happens to be a Linux kernel in this case.

Quoted 22 lines
Quoted 10 lines

So a couple of choices I've made doing this:

Promoting *fdt to MachineState

This seemed the simplest approach to making the fdt available to the global state, especially as MachineState already has a *dtb pointer. I've converted the ARM virt machine and a RISCV machine but if this is acceptable I can convert the others.

This seems fine to me.

Quoted 8 lines

"Polluting" the generic loader arguments

This was mainly out of convenience as the loader already knows the size of the blob being loaded. However you could certainly argue it makes sense to have a more generic "FDT expander" virtual device that could for example query the QOM model somehow to find the details it needs.

Quoted 3 lines

That seems like a better option. Why not have a generic way to modify the device tree with a specific argument? It could either be -device loader,file=fdt,... or -fdt ...

Well it comes down to how much of a special case this is? Pretty much all FDT (and ACPI for the matter) is basically down to the board level models - and not all of them just the funky configurable ones. For other board models we just expect the user to pass the FDT they got with their kernel blob.

For modern VirtIO systems the only thing you really need to expose is the root PCI bus because the rest of the devices are discover-able there.

So the real question is are there any other -devices that we want to be able to graft FDT entries on or is the generic loader enough of a special case that we keep all the logic in there?

Quoted 36 lines

Alistair

Quoted 33 lines

FDT isn't the only way of passing system information up the boot chain. We could reasonably want to do a similar thing with ACPI which is what should be being used on SBSA like devices to communicate with the hypervisor.

Also relying on ,, in the QemuOpt parser is ugly. It might be worth having better quoting support if I press on with this.

So what do people think? Something worth developing?

Alex Bennée (4):
hw/board: promote fdt from ARM VirtMachineState to MachineState hw/riscv: migrate fdt field to generic MachineState
device_tree: add qemu_fdt_setprop_string_array helper
generic_loader: allow the insertion of /chosen/module stanzas

 include/hw/arm/virt.h            |   1 -
 include/hw/boards.h              |   1 +
 include/hw/core/generic-loader.h |   4 +
 include/hw/riscv/virt.h          |   1 -
 include/sysemu/device_tree.h     |  17 ++
 device_tree.c                    |  26 +++
 hw/arm/virt.c                    | 322 ++++++++++++++++---------------
 hw/core/generic-loader.c         |  42 ++++
 hw/riscv/virt.c                  |  18 +-
 9 files changed, 268 insertions(+), 164 deletions(-)

--
2.20.1

Signature · 1 line
Alex Bennée
#8 lore raw
Quoted 3 lines · Alex Bennée

So the real question is are there any other -devices that we want to be able to graft FDT entries on or is the generic loader enough of a special case that we keep all the logic in there?

To my mind the point of the generic loader is exactly that it is not a special case. It works exactly the same way for every board, for every architecture. It shouldn't have special case "this makes things work for the thing I want to load on these boards that all have FDT and want a particular change to it". We already have a loader that has that kind of "do what I mean" behaviour (--kernel), and I very much do not want the generic loader device to go in that direction. Its whole advantage is that it is not that, but is just a "load the file, nothing else, no magic" operation.

thanks
-- PMM

#9 lore raw
Quoted 5 lines · Peter Maydell
Quoted 3 lines · Alex Bennée

So the real question is are there any other -devices that we want to be able to graft FDT entries on or is the generic loader enough of a special case that we keep all the logic in there?

Quoted 11 lines

To my mind the point of the generic loader is exactly that it is not a special case. It works exactly the same way for every board, for every architecture. It shouldn't have special case "this makes things work for the thing I want to load on these boards that all have FDT and want a particular change to it". We already have a loader that has that kind of "do what I mean" behaviour (--kernel), and I very much do not want the generic loader device to go in that direction. Its whole advantage is that it is not that, but is just a "load the file, nothing else, no magic" operation.

So should we introduce a sub-classed -device guest-loader which behaves like generic loader except that it is also aware of platform data issues?

The other option would be to add the logic to each board that supports dynamic platform data. It would keep the problem bounded but also lead to a fair amount of duplicated boiler-plate.

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Alex Bennée
#10 lore raw
In reply to Alex Bennée
Quoted 79 lines · Alex Bennée
Quoted 35 lines · Alistair Francis
Quoted 7 lines · Alex Bennée

Hi,

This series adds the ability to append FDT data for blobs loaded by the generic loader. My principle use-case was to be able to directly boot Xen with a kernel image which avoided having to:

Quoted 7 lines

- get the kernel image into my system image
- deal with bugs in FDT munging by -bios firmware and/or grub

as such this currently a developer hack that makes *my* life easy and is thus presented as an RFC for discussion. While I've tested it with Xen I'm sure the approach would be applicable to other hypervisors or firmware which expect to consume FDT data pointing at various blobs.

An interesting idea. I think this comes up enough that it's worth thinking about.

Quoted 15 lines

An example command line that launches this is (magic from -kernel):

  ./aarch64-softmmu/qemu-system-aarch64 -cpu cortex-a57 \
    -machine type=virt,virtualization=on -display none \
    -serial mon:stdio \
    -netdev user,id=unet,hostfwd=tcp::2222-:22 \
    -device virtio-net-pci,netdev=unet,id=virt-net,disable-legacy=on \
    -device virtio-scsi-pci,id=virt-scsi,disable-legacy=on \
    -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-buster-arm64 \
    -device scsi-hd,drive=hd,id=virt-scsi-hd \
    -smp 4 -m 4096 \
    -kernel ~/lsrc/xen.git/xen/xen \
    -append "dom0_mem=1G,max:1G loglvl=all guest_loglvl=all" \
    -device loader,addr=0x47000000,file=$HOME/lsrc/linux.git/builds/arm64/arch/arm64/boot/Image,len-fdt-compat=2,fdt-compat[0]='multiboot,,module',fdt-compat[1]='multiboot,,kernel',fdt-bootargs="root=/dev/sda2 ro console=hvc0 earlyprintk=xen"
Quoted 3 lines

You are loading the kernel Image file, but changing the device tree that was generated by QEMU and is being loaded in memory. As a user that is really confusing.

Well in this case the "kernel" is Xen which helpfully comes with a kernel compatible header that the kernel loaded understand. The blob could be any Dom0 kernel - it just happens to be a Linux kernel in this case.

Quoted 22 lines
Quoted 10 lines

So a couple of choices I've made doing this:

Promoting *fdt to MachineState

This seemed the simplest approach to making the fdt available to the global state, especially as MachineState already has a *dtb pointer. I've converted the ARM virt machine and a RISCV machine but if this is acceptable I can convert the others.

This seems fine to me.

Quoted 8 lines

"Polluting" the generic loader arguments

This was mainly out of convenience as the loader already knows the size of the blob being loaded. However you could certainly argue it makes sense to have a more generic "FDT expander" virtual device that could for example query the QOM model somehow to find the details it needs.

Quoted 3 lines

That seems like a better option. Why not have a generic way to modify the device tree with a specific argument? It could either be -device loader,file=fdt,... or -fdt ...

Well it comes down to how much of a special case this is? Pretty much all FDT (and ACPI for the matter) is basically down to the board level models - and not all of them just the funky configurable ones. For other board models we just expect the user to pass the FDT they got with their kernel blob.

Quoted 8 lines

For modern VirtIO systems the only thing you really need to expose is the root PCI bus because the rest of the devices are discover-able there.

So the real question is are there any other -devices that we want to be able to graft FDT entries on or is the generic loader enough of a special case that we keep all the logic in there?

Hi,

Another option is to allow the user to pass along a DTB overlay with the generic loader option (or with a separate option as Alistair suggested). With overlways we wouldn't need all the command-line options to enable construction of dtb fragments, it would be more or less transparent to QEMU. There may be limitations with the overlay flows that I'm not aware of though...

Cheers,
Edgar

#11 lore raw
Quoted 14 lines · Edgar E. Iglesias
Quoted 13 lines · Alex Bennée
Quoted 10 lines · Alistair Francis
Quoted 9 lines · Alex Bennée

Hi,

This series adds the ability to append FDT data for blobs loaded by the generic loader. My principle use-case was to be able to directly boot Xen with a kernel image which avoided having to:

- get the kernel image into my system image
- deal with bugs in FDT munging by -bios firmware and/or grub

<snip>

Quoted 27 lines
Quoted 18 lines
Quoted 11 lines
Quoted 7 lines

"Polluting" the generic loader arguments

This was mainly out of convenience as the loader already knows the size of the blob being loaded. However you could certainly argue it makes sense to have a more generic "FDT expander" virtual device that could for example query the QOM model somehow to find the details it needs.

That seems like a better option. Why not have a generic way to modify the device tree with a specific argument? It could either be -device loader,file=fdt,... or -fdt ...

Well it comes down to how much of a special case this is? Pretty much all FDT (and ACPI for the matter) is basically down to the board level models - and not all of them just the funky configurable ones. For other board models we just expect the user to pass the FDT they got with their kernel blob.

Quoted 8 lines

For modern VirtIO systems the only thing you really need to expose is the root PCI bus because the rest of the devices are discover-able there.

So the real question is are there any other -devices that we want to be able to graft FDT entries on or is the generic loader enough of a special case that we keep all the logic in there?

Quoted 8 lines

Hi,

Another option is to allow the user to pass along a DTB overlay with the generic loader option (or with a separate option as Alistair suggested). With overlways we wouldn't need all the command-line options to enable construction of dtb fragments, it would be more or less transparent to QEMU. There may be limitations with the overlay flows that I'm not aware of though...

So the problem of adding DTB overlays is it's not that much easier than the current options of using -machine dumpdtb and then hand hacking the magic values and rebuilding, for example:

https://medium.com/@denisobrezkov/xen-on-arm-and-qemu-1654f24dea75

Unless we come up with some sort of template support that allows QEMU to insert numbers like address and size while processing the template. But that seems a little too over engineered and likely introduces complexity into the system.

Given the generic-loader is so simple I'm leaning towards another approach of just c&p'ing generic-loader into a new "magic" device (maybe guest-loader) and stripping out the bits we don't need (data, data-len, be etc) and making the options more tuned what we are trying to achieve. For example:

-device guest-loader,kernel=path/to/Image,args="command line",addr=0x47000000,hyp=xen -device guest-loader,initrd=path/to/initrd,addr=0x42000000,hyp=xen

And then we can embed the smarts in the loader to set either DTB or ACPI entries as required and if we need additional magic to support different hypervisors (which hopefully you don't but...) you can modulate the hyp=FOO variable.

There may be an argument for having a -hypervisor as a sugar alias for -kernel (which maps to machine.kernel) but currently I see no practical differences you need to launch it except maybe forcing the virtualisation property to true if it exists - but that seems a little ARM focused.

Signature · 1 line
Alex Bennée
#12 lore raw
Quoted 55 lines · Alex Bennée
Quoted 14 lines · Edgar E. Iglesias
Quoted 13 lines · Alex Bennée
Quoted 10 lines · Alistair Francis
Quoted 9 lines · Alex Bennée

Hi,

This series adds the ability to append FDT data for blobs loaded by the generic loader. My principle use-case was to be able to directly boot Xen with a kernel image which avoided having to:

- get the kernel image into my system image
- deal with bugs in FDT munging by -bios firmware and/or grub

<snip>

Quoted 27 lines
Quoted 18 lines
Quoted 11 lines
Quoted 7 lines

"Polluting" the generic loader arguments

This was mainly out of convenience as the loader already knows the size of the blob being loaded. However you could certainly argue it makes sense to have a more generic "FDT expander" virtual device that could for example query the QOM model somehow to find the details it needs.

That seems like a better option. Why not have a generic way to modify the device tree with a specific argument? It could either be -device loader,file=fdt,... or -fdt ...

Well it comes down to how much of a special case this is? Pretty much all FDT (and ACPI for the matter) is basically down to the board level models - and not all of them just the funky configurable ones. For other board models we just expect the user to pass the FDT they got with their kernel blob.

Quoted 8 lines

For modern VirtIO systems the only thing you really need to expose is the root PCI bus because the rest of the devices are discover-able there.

So the real question is are there any other -devices that we want to be able to graft FDT entries on or is the generic loader enough of a special case that we keep all the logic in there?

Quoted 8 lines

Hi,

Another option is to allow the user to pass along a DTB overlay with the generic loader option (or with a separate option as Alistair suggested). With overlways we wouldn't need all the command-line options to enable construction of dtb fragments, it would be more or less transparent to QEMU. There may be limitations with the overlay flows that I'm not aware of though...

Quoted 3 lines

So the problem of adding DTB overlays is it's not that much easier than the current options of using -machine dumpdtb and then hand hacking the magic values and rebuilding, for example:

I agree with this. If a user is changing a DTB from a command line they probably only want small changes and are unlikely to need the full power of an overlay.

Quoted 7 lines

https://medium.com/@denisobrezkov/xen-on-arm-and-qemu-1654f24dea75

Unless we come up with some sort of template support that allows QEMU to insert numbers like address and size while processing the template. But that seems a little too over engineered and likely introduces complexity into the system.

This though sounds interesting :)

Quoted 9 lines

Given the generic-loader is so simple I'm leaning towards another approach of just c&p'ing generic-loader into a new "magic" device (maybe guest-loader) and stripping out the bits we don't need (data, data-len, be etc) and making the options more tuned what we are trying to achieve. For example:

-device guest-loader,kernel=path/to/Image,args="command line",addr=0x47000000,hyp=xen -device guest-loader,initrd=path/to/initrd,addr=0x42000000,hyp=xen

At first I'm not thrilled of adding a new loader. In saying that, there are lots of times where -kernel and friends doesn't do what I want it to do and I have to fall back to the generic loader and code changes to QEMU so maybe this is a good idea for image loading.

I'm guessing this would be the same for every platform which would be a nice change from -kernel.

Alistair

Quoted 14 lines

And then we can embed the smarts in the loader to set either DTB or ACPI entries as required and if we need additional magic to support different hypervisors (which hopefully you don't but...) you can modulate the hyp=FOO variable.

There may be an argument for having a -hypervisor as a sugar alias for -kernel (which maps to machine.kernel) but currently I see no practical differences you need to launch it except maybe forcing the virtualisation property to true if it exists - but that seems a little ARM focused.

--
Alex Bennée