8fced6
From 04f7fe2423a4de8d2fea7068b3fb316e15e76eaa Mon Sep 17 00:00:00 2001
8fced6
From: Greg Kurz <gkurz@redhat.com>
8fced6
Date: Tue, 19 Jan 2021 15:09:49 -0500
8fced6
Subject: [PATCH 1/9] spapr: Improve handling of fdt buffer size
8fced6
8fced6
RH-Author: Greg Kurz <gkurz@redhat.com>
8fced6
Message-id: <20210119150954.1017058-2-gkurz@redhat.com>
8fced6
Patchwork-id: 100682
8fced6
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 1/6] spapr: Improve handling of fdt buffer size
8fced6
Bugzilla: 1901837
8fced6
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
8fced6
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
8fced6
RH-Acked-by: David Gibson <dgibson@redhat.com>
8fced6
8fced6
From: David Gibson <david@gibson.dropbear.id.au>
8fced6
8fced6
Previously, spapr_build_fdt() constructed the device tree in a fixed
8fced6
buffer of size FDT_MAX_SIZE.  This is a bit inflexible, but more
8fced6
importantly it's awkward for the case where we use it during CAS.  In
8fced6
that case the guest firmware supplies a buffer and we have to
8fced6
awkwardly check that what we generated fits into it afterwards, after
8fced6
doing a lot of size checks during spapr_build_fdt().
8fced6
8fced6
Simplify this by having spapr_build_fdt() take a 'space' parameter.
8fced6
For the CAS case, we pass in the buffer size provided by SLOF, for the
8fced6
machine init case, we continue to pass FDT_MAX_SIZE.
8fced6
8fced6
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
8fced6
Reviewed-by: Cedric Le Goater <clg@fr.ibm.com>
8fced6
Reviewed-by: Greg Kurz <groug@kaod.org>
8fced6
(cherry picked from commit 97b32a6afa78ae68fb16344b9a144b6f433f42a2)
8fced6
Signed-off-by: Greg Kurz <gkurz@redhat.com>
8fced6
Signed-off-by: Jon Maloy <jmaloy.redhat.com>
8fced6
---
8fced6
 hw/ppc/spapr.c | 33 +++++++++++----------------------
8fced6
 1 file changed, 11 insertions(+), 22 deletions(-)
8fced6
8fced6
diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
8fced6
index c74079702d..92f63ad035 100644
8fced6
--- a/hw/ppc/spapr.c
8fced6
+++ b/hw/ppc/spapr.c
8fced6
@@ -918,7 +918,8 @@ static bool spapr_hotplugged_dev_before_cas(void)
8fced6
     return false;
8fced6
 }
8fced6
 
8fced6
-static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset);
8fced6
+static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset,
8fced6
+                             size_t space);
8fced6
 
8fced6
 int spapr_h_cas_compose_response(SpaprMachineState *spapr,
8fced6
                                  target_ulong addr, target_ulong size,
8fced6
@@ -931,24 +932,17 @@ int spapr_h_cas_compose_response(SpaprMachineState *spapr,
8fced6
         return 1;
8fced6
     }
8fced6
 
8fced6
-    if (size < sizeof(hdr) || size > FW_MAX_SIZE) {
8fced6
-        error_report("SLOF provided an unexpected CAS buffer size "
8fced6
-                     TARGET_FMT_lu " (min: %zu, max: %u)",
8fced6
-                     size, sizeof(hdr), FW_MAX_SIZE);
8fced6
+    if (size < sizeof(hdr)) {
8fced6
+        error_report("SLOF provided insufficient CAS buffer "
8fced6
+                     TARGET_FMT_lu " (min: %zu)", size, sizeof(hdr));
8fced6
         exit(EXIT_FAILURE);
8fced6
     }
8fced6
 
8fced6
     size -= sizeof(hdr);
8fced6
 
8fced6
-    fdt = spapr_build_fdt(spapr, false);
8fced6
+    fdt = spapr_build_fdt(spapr, false, size);
8fced6
     _FDT((fdt_pack(fdt)));
8fced6
 
8fced6
-    if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
8fced6
-        g_free(fdt);
8fced6
-        trace_spapr_cas_failed(size);
8fced6
-        return -1;
8fced6
-    }
8fced6
-
8fced6
     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
8fced6
     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
8fced6
     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
8fced6
@@ -1198,7 +1192,8 @@ static void spapr_dt_hypervisor(SpaprMachineState *spapr, void *fdt)
8fced6
     }
8fced6
 }
8fced6
 
8fced6
-static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset)
8fced6
+static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset,
8fced6
+                             size_t space)
8fced6
 {
8fced6
     MachineState *machine = MACHINE(spapr);
8fced6
     MachineClass *mc = MACHINE_GET_CLASS(machine);
8fced6
@@ -1208,8 +1203,8 @@ static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset)
8fced6
     SpaprPhbState *phb;
8fced6
     char *buf;
8fced6
 
8fced6
-    fdt = g_malloc0(FDT_MAX_SIZE);
8fced6
-    _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
8fced6
+    fdt = g_malloc0(space);
8fced6
+    _FDT((fdt_create_empty_tree(fdt, space)));
8fced6
 
8fced6
     /* Root node */
8fced6
     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
8fced6
@@ -1724,19 +1719,13 @@ static void spapr_machine_reset(MachineState *machine)
8fced6
      */
8fced6
     fdt_addr = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FDT_MAX_SIZE;
8fced6
 
8fced6
-    fdt = spapr_build_fdt(spapr, true);
8fced6
+    fdt = spapr_build_fdt(spapr, true, FDT_MAX_SIZE);
8fced6
 
8fced6
     rc = fdt_pack(fdt);
8fced6
 
8fced6
     /* Should only fail if we've built a corrupted tree */
8fced6
     assert(rc == 0);
8fced6
 
8fced6
-    if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
8fced6
-        error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
8fced6
-                     fdt_totalsize(fdt), FDT_MAX_SIZE);
8fced6
-        exit(1);
8fced6
-    }
8fced6
-
8fced6
     /* Load the fdt */
8fced6
     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
8fced6
     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
8fced6
-- 
8fced6
2.18.2
8fced6