Skip to contentSkip to conversation

igvm: add device tree parameter support

PATCH Superseded Luigi Leonardi 2 participants 4 messages Jun 11 – Jun 15

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 igvm: add device tree parameter support

Coconut SVSM, with the upcoming device tree support [1], will use the IGVM device tree parameter to discover virtio-mmio and ISA serial devices instead of relying on the fw_cfg interface, which is QEMU-specific.

The device tree is packed before copying into the IGVM parameter area to reduce its size, since IGVM files can define tighter memory constraints for parameter areas. Packing is done in the generic IGVM backend rather than in per-architecture device tree setup code, so that each architecture does not need to handle it individually.

[1] https://github.com/coconut-svsm/svsm/pull/1006

---
This device tree will be consumed by Coconut SVSM with the upcoming device tree support [1]. This will allow SVSM to run on a normal CPU (not AMD SEV-SNP) and execute all its tests using upstream QEMU.

Eventually edk2 will also be able to consume DT via IGVM, but the work has not started yet.

[1] https://github.com/coconut-svsm/svsm/pull/1006

To: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Gerd Hoffmann <[email protected]>
Cc: Stefano Garzarella <[email protected]>
Cc: Ani Sinha <[email protected]>
Cc: Zhao Liu <[email protected]>
Cc: Oliver Steffen <[email protected]>
---
Changes in v3:
- Device tree is packed in igvm core code. [Gerd]
- Dropped microvm patch.
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]

Changes in v2:
- First patch introduced a shadowed variable, fixed.
- When parameter area is not found return -1.
- Moved qigvm_directive_device_tree to backends/igvm.c
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
---

Error handling on qigvm_find_param_entry will change once [2] is merged. I am already using the new approach here.

[2] https://lore.kernel.org/qemu-devel/[email protected]

---
 backends/igvm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
backends/igvm.c +47 -0
index c347d0c17e..452715b105 100644
@@ -25,6 +25,7 @@
 
 #include <igvm/igvm.h>
 #include <igvm/igvm_defs.h>
+#include <libfdt.h>
 
 
 /*
@@ -100,6 +101,8 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,
 static int qigvm_initialization_guest_policy(QIgvm *ctx,
                                        const uint8_t *header_data,
                                        Error **errp);
+static int qigvm_directive_device_tree(QIgvm *ctx, const uint8_t *header_data,
+                                       Error **errp);
 
 struct QIGVMHandler {
     uint32_t type;
@@ -130,6 +133,8 @@ static struct QIGVMHandler handlers[] = {
       qigvm_initialization_guest_policy },
     { IGVM_VHT_MADT, IGVM_HEADER_SECTION_DIRECTIVE,
       qigvm_directive_madt },
+    { IGVM_VHT_DEVICE_TREE, IGVM_HEADER_SECTION_DIRECTIVE,
+      qigvm_directive_device_tree },
 };
 
 static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
@@ -752,6 +757,48 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,
     return 0;
 }
 
+static int qigvm_directive_device_tree(QIgvm *ctx, const uint8_t *header_data,
+                                       Error **errp)
+{
+    const IGVM_VHS_PARAMETER *param = (const IGVM_VHS_PARAMETER *)header_data;
+    g_autofree void *fdt_packed = NULL;
+    QIgvmParameterData *param_entry;
+    uint32_t fdt_size;
+
+    param_entry = qigvm_find_param_entry(ctx, param->parameter_area_index);
+    if (param_entry == NULL) {
+        error_setg(errp, "IGVM: parameter area index %u not found",
+                   param->parameter_area_index);
+        return -1;
+    }
+
+    if (ctx->machine_state->fdt == NULL) {
+        error_setg(errp, "IGVM: device tree not available");
+        return -1;
+    }
+
+    fdt_size = fdt_totalsize(ctx->machine_state->fdt);
+    fdt_packed = g_malloc(fdt_size);
+    memcpy(fdt_packed, ctx->machine_state->fdt, fdt_size);
+
+    if (fdt_pack(fdt_packed)) {
+        error_setg(errp, "IGVM: failed to pack device tree");
+        return -1;
+    }
+
+    fdt_size = fdt_totalsize(fdt_packed);
+    if (fdt_size > param_entry->size) {
+        error_setg(errp,
+                   "IGVM: device tree size exceeds parameter area"
+                   " defined in IGVM file");
+        return -1;
+    }
+
+    memcpy(param_entry->data, fdt_packed, fdt_size);
+
+    return 0;
+}
+
 static int qigvm_initialization_guest_policy(QIgvm *ctx,
                                        const uint8_t *header_data, Error **errp)
 {

---
base-commit: cc329c491768b2d91eb0b0984f3baa0bf805776d
change-id: 20260608-microvm_device_tree-0263c3b1be86

Best regards,

Signature · 1 line
Luigi Leonardi <[email protected]>
#1 lore raw
Quoted 59 lines · Luigi Leonardi

Coconut SVSM, with the upcoming device tree support [1], will use the IGVM device tree parameter to discover virtio-mmio and ISA serial devices instead of relying on the fw_cfg interface, which is QEMU-specific.

The device tree is packed before copying into the IGVM parameter area to reduce its size, since IGVM files can define tighter memory constraints for parameter areas. Packing is done in the generic IGVM backend rather than in per-architecture device tree setup code, so that each architecture does not need to handle it individually.

[1] https://github.com/coconut-svsm/svsm/pull/1006

---
This device tree will be consumed by Coconut SVSM with the upcoming device tree support [1]. This will allow SVSM to run on a normal CPU (not AMD SEV-SNP) and execute all its tests using upstream QEMU.

Eventually edk2 will also be able to consume DT via IGVM, but the work has not started yet.

[1] https://github.com/coconut-svsm/svsm/pull/1006

To: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Gerd Hoffmann <[email protected]>
Cc: Stefano Garzarella <[email protected]>
Cc: Ani Sinha <[email protected]>
Cc: Zhao Liu <[email protected]>
Cc: Oliver Steffen <[email protected]>
---
Changes in v3:
- Device tree is packed in igvm core code. [Gerd]
- Dropped microvm patch.
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]

Changes in v2:
- First patch introduced a shadowed variable, fixed.
- When parameter area is not found return -1.
- Moved qigvm_directive_device_tree to backends/igvm.c
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
---

Error handling on qigvm_find_param_entry will change once [2] is merged. I am already using the new approach here.

[2] https://lore.kernel.org/qemu-devel/[email protected]
---
backends/igvm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 47 insertions(+)
backends/igvm.c +0 -0
index c347d0c17e..452715b105 100644

backends/igvm.c line 28

Quoted 4 lines
backends/igvm.c +1 -0
@@ -25,6 +25,7 @@
 
 #include <igvm/igvm.h>
 #include <igvm/igvm_defs.h>
+#include <libfdt.h>

So now we depend on libfdt. Should we add this dependency to the build system? Or should we disable IGVM_VHT_DEVICE_TREE support here if libfdt is not available?

I just tried:

$ ./configure ...  --disable-fdt --enable-igvm
$ make
[110/110] Linking target qemu-system-x86_64
FAILED: [code=1] qemu-system-x86_64
cc -m64 @qemu-system-x86_64.rsp
/usr/bin/ld.bfd: libsystem.a.p/backends_igvm.c.o: in function `qigvm_directive_device_tree':
/home/stefano/repos/qemu-review/build/../backends/igvm.c:784:(.text+0x109b): undefined reference to `fdt_pack'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Quoted 38 lines
backends/igvm.c +17 -0
 /*
@@ -100,6 +101,8 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,
 static int qigvm_initialization_guest_policy(QIgvm *ctx,
                                       const uint8_t *header_data,
                                       Error **errp);
+static int qigvm_directive_device_tree(QIgvm *ctx, const uint8_t *header_data,
+                                       Error **errp);
 
 struct QIGVMHandler {
    uint32_t type;
@@ -130,6 +133,8 @@ static struct QIGVMHandler handlers[] = {
      qigvm_initialization_guest_policy },
    { IGVM_VHT_MADT, IGVM_HEADER_SECTION_DIRECTIVE,
      qigvm_directive_madt },
+    { IGVM_VHT_DEVICE_TREE, IGVM_HEADER_SECTION_DIRECTIVE,
+      qigvm_directive_device_tree },
 };
 
 static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
@@ -752,6 +757,48 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,
    return 0;
 }
 
+static int qigvm_directive_device_tree(QIgvm *ctx, const uint8_t *header_data,
+                                       Error **errp)
+{
+    const IGVM_VHS_PARAMETER *param = (const IGVM_VHS_PARAMETER *)header_data;
+    g_autofree void *fdt_packed = NULL;
+    QIgvmParameterData *param_entry;
+    uint32_t fdt_size;
+
+    param_entry = qigvm_find_param_entry(ctx, param->parameter_area_index);
+    if (param_entry == NULL) {
+        error_setg(errp, "IGVM: parameter area index %u not found",
+                   param->parameter_area_index);
+        return -1;

backends/igvm.c static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data, · line 782

Quoted 10 lines
backends/igvm.c +10 -0
+    }
+
+    if (ctx->machine_state->fdt == NULL) {
+        error_setg(errp, "IGVM: device tree not available");
+        return -1;
+    }
+
+    fdt_size = fdt_totalsize(ctx->machine_state->fdt);
+    fdt_packed = g_malloc(fdt_size);
+    memcpy(fdt_packed, ctx->machine_state->fdt, fdt_size);

What about using g_memdup2() ?

Stefano

Quoted 31 lines
backends/igvm.c +19 -1
+
+    if (fdt_pack(fdt_packed)) {
+        error_setg(errp, "IGVM: failed to pack device tree");
+        return -1;
+    }
+
+    fdt_size = fdt_totalsize(fdt_packed);
+    if (fdt_size > param_entry->size) {
+        error_setg(errp,
+                   "IGVM: device tree size exceeds parameter area"
+                   " defined in IGVM file");
+        return -1;
+    }
+
+    memcpy(param_entry->data, fdt_packed, fdt_size);
+
+    return 0;
+}
+
 static int qigvm_initialization_guest_policy(QIgvm *ctx,
                                       const uint8_t *header_data, Error **errp)
 {
 
---

base-commit: cc329c491768b2d91eb0b0984f3baa0bf805776d
change-id: 20260608-microvm_device_tree-0263c3b1be86

Best regards,

Signature · 1 line
Luigi Leonardi <[email protected]>
#2 lore raw
Quoted 61 lines · Stefano Garzarella
Quoted 60 lines · Luigi Leonardi

Coconut SVSM, with the upcoming device tree support [1], will use the IGVM device tree parameter to discover virtio-mmio and ISA serial devices instead of relying on the fw_cfg interface, which is QEMU-specific.

The device tree is packed before copying into the IGVM parameter area to reduce its size, since IGVM files can define tighter memory constraints for parameter areas. Packing is done in the generic IGVM backend rather than in per-architecture device tree setup code, so that each architecture does not need to handle it individually.

[1] https://github.com/coconut-svsm/svsm/pull/1006

---
This device tree will be consumed by Coconut SVSM with the upcoming device tree support [1]. This will allow SVSM to run on a normal CPU (not AMD SEV-SNP) and execute all its tests using upstream QEMU.

Eventually edk2 will also be able to consume DT via IGVM, but the work has not started yet.

[1] https://github.com/coconut-svsm/svsm/pull/1006

To: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Gerd Hoffmann <[email protected]>
Cc: Stefano Garzarella <[email protected]>
Cc: Ani Sinha <[email protected]>
Cc: Zhao Liu <[email protected]>
Cc: Oliver Steffen <[email protected]>
---
Changes in v3:
- Device tree is packed in igvm core code. [Gerd]
- Dropped microvm patch.
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]

Changes in v2:
- First patch introduced a shadowed variable, fixed.
- When parameter area is not found return -1.
- Moved qigvm_directive_device_tree to backends/igvm.c
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
---

Error handling on qigvm_find_param_entry will change once [2] is merged. I am already using the new approach here.

[2] https://lore.kernel.org/qemu-devel/[email protected]
---
backends/igvm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 47 insertions(+)
backends/igvm.c +0 -0
index c347d0c17e..452715b105 100644
@@ -25,6 +25,7 @@
 
Quoted 5 lines

backends/igvm.c line 28

Quoted 3 lines
backends/igvm.c +1 -0
 #include <igvm/igvm.h>
 #include <igvm/igvm_defs.h>
+#include <libfdt.h>

So now we depend on libfdt. Should we add this dependency to the build system? Or should we disable IGVM_VHT_DEVICE_TREE support here if libfdt is not available?

Good question: IIUC, running --disable-fdt disables all the things that depend on on it, like microvm.

So, disabling FDT doesn't make too much sense to provide IGVM_VHT_DEVICE_TREE when fdt is disabled. WDYT?

Quoted 55 lines

I just tried:

$ ./configure ...  --disable-fdt --enable-igvm
$ make
[110/110] Linking target qemu-system-x86_64
FAILED: [code=1] qemu-system-x86_64
cc -m64 @qemu-system-x86_64.rsp
/usr/bin/ld.bfd: libsystem.a.p/backends_igvm.c.o: in function `qigvm_directive_device_tree':
/home/stefano/repos/qemu-review/build/../backends/igvm.c:784:(.text+0x109b): undefined reference to `fdt_pack'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Quoted 42 lines
backends/igvm.c +21 -0
 /*
@@ -100,6 +101,8 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,
 static int qigvm_initialization_guest_policy(QIgvm *ctx,
                                      const uint8_t *header_data,
                                      Error **errp);
+static int qigvm_directive_device_tree(QIgvm *ctx, const uint8_t *header_data,
+                                       Error **errp);
 
 struct QIGVMHandler {
   uint32_t type;
@@ -130,6 +133,8 @@ static struct QIGVMHandler handlers[] = {
     qigvm_initialization_guest_policy },
   { IGVM_VHT_MADT, IGVM_HEADER_SECTION_DIRECTIVE,
     qigvm_directive_madt },
+    { IGVM_VHT_DEVICE_TREE, IGVM_HEADER_SECTION_DIRECTIVE,
+      qigvm_directive_device_tree },
 };
 
 static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
@@ -752,6 +757,48 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,
   return 0;
 }
 
+static int qigvm_directive_device_tree(QIgvm *ctx, const uint8_t *header_data,
+                                       Error **errp)
+{
+    const IGVM_VHS_PARAMETER *param = (const IGVM_VHS_PARAMETER *)header_data;
+    g_autofree void *fdt_packed = NULL;
+    QIgvmParameterData *param_entry;
+    uint32_t fdt_size;
+
+    param_entry = qigvm_find_param_entry(ctx, param->parameter_area_index);
+    if (param_entry == NULL) {
+        error_setg(errp, "IGVM: parameter area index %u not found",
+                   param->parameter_area_index);
+        return -1;
+    }
+
+    if (ctx->machine_state->fdt == NULL) {
+        error_setg(errp, "IGVM: device tree not available");
Quoted 8 lines

backends/igvm.c static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data, · line 782

Quoted 6 lines
backends/igvm.c +6 -0
+        return -1;
+    }
+
+    fdt_size = fdt_totalsize(ctx->machine_state->fdt);
+    fdt_packed = g_malloc(fdt_size);
+    memcpy(fdt_packed, ctx->machine_state->fdt, fdt_size);

What about using g_memdup2() ?

Yep, good idea.

Thanks,
Luigi

#3 lore raw
Quoted 62 lines · Luigi Leonardi
Quoted 61 lines · Stefano Garzarella
Quoted 60 lines · Luigi Leonardi

Coconut SVSM, with the upcoming device tree support [1], will use the IGVM device tree parameter to discover virtio-mmio and ISA serial devices instead of relying on the fw_cfg interface, which is QEMU-specific.

The device tree is packed before copying into the IGVM parameter area to reduce its size, since IGVM files can define tighter memory constraints for parameter areas. Packing is done in the generic IGVM backend rather than in per-architecture device tree setup code, so that each architecture does not need to handle it individually.

[1] https://github.com/coconut-svsm/svsm/pull/1006

---
This device tree will be consumed by Coconut SVSM with the upcoming device tree support [1]. This will allow SVSM to run on a normal CPU (not AMD SEV-SNP) and execute all its tests using upstream QEMU.

Eventually edk2 will also be able to consume DT via IGVM, but the work has not started yet.

[1] https://github.com/coconut-svsm/svsm/pull/1006

To: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Gerd Hoffmann <[email protected]>
Cc: Stefano Garzarella <[email protected]>
Cc: Ani Sinha <[email protected]>
Cc: Zhao Liu <[email protected]>
Cc: Oliver Steffen <[email protected]>
---
Changes in v3:
- Device tree is packed in igvm core code. [Gerd]
- Dropped microvm patch.
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]

Changes in v2:
- First patch introduced a shadowed variable, fixed.
- When parameter area is not found return -1.
- Moved qigvm_directive_device_tree to backends/igvm.c
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
---

Error handling on qigvm_find_param_entry will change once [2] is merged. I am already using the new approach here.

[2] https://lore.kernel.org/qemu-devel/[email protected]
---
backends/igvm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 47 insertions(+)
backends/igvm.c +0 -0
index c347d0c17e..452715b105 100644
@@ -25,6 +25,7 @@
 
Quoted 8 lines
Quoted 5 lines

backends/igvm.c line 28

Quoted 3 lines
backends/igvm.c +1 -0
 #include <igvm/igvm.h>
 #include <igvm/igvm_defs.h>
+#include <libfdt.h>

So now we depend on libfdt. Should we add this dependency to the build system? Or should we disable IGVM_VHT_DEVICE_TREE support here if libfdt is not available?

Good question: IIUC, running --disable-fdt disables all the things that depend on on it, like microvm.

That was just a test; I just wanted to point out that if libfdt isn't there, there's a problem here.

Quoted 3 lines

So, disabling FDT doesn't make too much sense to provide IGVM_VHT_DEVICE_TREE when fdt is disabled. WDYT?

Yes, I agree. I would avoid direct dependency, so we can enable IGVM even if libfdt isn't present (as was the case before this patch) and, of course, support the device tree only when it is present.

Thanks,
Stefano

Quoted 70 lines
Quoted 56 lines

I just tried:

$ ./configure ...  --disable-fdt --enable-igvm
$ make
[110/110] Linking target qemu-system-x86_64
FAILED: [code=1] qemu-system-x86_64
cc -m64 @qemu-system-x86_64.rsp
/usr/bin/ld.bfd: libsystem.a.p/backends_igvm.c.o: in function `qigvm_directive_device_tree':
/home/stefano/repos/qemu-review/build/../backends/igvm.c:784:(.text+0x109b): undefined reference to `fdt_pack'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Quoted 42 lines
backends/igvm.c +21 -0
 /*
@@ -100,6 +101,8 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,
 static int qigvm_initialization_guest_policy(QIgvm *ctx,
                                     const uint8_t *header_data,
                                     Error **errp);
+static int qigvm_directive_device_tree(QIgvm *ctx, const uint8_t *header_data,
+                                       Error **errp);
 
 struct QIGVMHandler {
  uint32_t type;
@@ -130,6 +133,8 @@ static struct QIGVMHandler handlers[] = {
    qigvm_initialization_guest_policy },
  { IGVM_VHT_MADT, IGVM_HEADER_SECTION_DIRECTIVE,
    qigvm_directive_madt },
+    { IGVM_VHT_DEVICE_TREE, IGVM_HEADER_SECTION_DIRECTIVE,
+      qigvm_directive_device_tree },
 };
 
 static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
@@ -752,6 +757,48 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,
  return 0;
 }
 
+static int qigvm_directive_device_tree(QIgvm *ctx, const uint8_t *header_data,
+                                       Error **errp)
+{
+    const IGVM_VHS_PARAMETER *param = (const IGVM_VHS_PARAMETER *)header_data;
+    g_autofree void *fdt_packed = NULL;
+    QIgvmParameterData *param_entry;
+    uint32_t fdt_size;
+
+    param_entry = qigvm_find_param_entry(ctx, param->parameter_area_index);
+    if (param_entry == NULL) {
+        error_setg(errp, "IGVM: parameter area index %u not found",
+                   param->parameter_area_index);
+        return -1;
+    }
+
+    if (ctx->machine_state->fdt == NULL) {
+        error_setg(errp, "IGVM: device tree not available");
Quoted 8 lines

backends/igvm.c static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data, · line 782

Quoted 6 lines
backends/igvm.c +6 -0
+        return -1;
+    }
+
+    fdt_size = fdt_totalsize(ctx->machine_state->fdt);
+    fdt_packed = g_malloc(fdt_size);
+    memcpy(fdt_packed, ctx->machine_state->fdt, fdt_size);

What about using g_memdup2() ?

Yep, good idea.

Thanks,
Luigi