Skip to contentSkip to conversation

loader: add support for resizeable blobs

PATCH In review Michael S. Tsirkin 3 participants 7 messages Jul 28, 2014 – Jul 29, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 loader: add support for resizeable blobs

Support resizeable blobs: we allocate more memory than currently available in the blob, which can later be filled in.

---
 include/hw/loader.h       | 14 +++++++--
 include/hw/nvram/fw_cfg.h |  2 +-
 hw/core/loader.c          | 15 +++++----
 hw/nvram/fw_cfg.c         | 79 ++++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 96 insertions(+), 14 deletions(-)
include/hw/loader.h +12 -2
index 796cbf9..ecce654 100644
@@ -55,9 +55,19 @@ extern bool rom_file_has_mr;
 int rom_add_file(const char *file, const char *fw_dir,
                  hwaddr addr, int32_t bootindex,
                  bool option_rom);
-void *rom_add_blob(const char *name, const void *blob, size_t len,
+void *rom_add_blob_resizeable(const char *name, const void *blob,
+                              size_t len, size_t max_len,
+                              hwaddr addr, const char *fw_file_name,
+                              FWCfgReadCallback fw_callback,
+                              void *callback_opaque);
+static inline void *rom_add_blob(const char *name, const void *blob, size_t len,
                    hwaddr addr, const char *fw_file_name,
-                   FWCfgReadCallback fw_callback, void *callback_opaque);
+                   FWCfgReadCallback fw_callback, void *callback_opaque)
+{
+    return rom_add_blob_resizeable(name, blob, len, len, addr,
+                                   fw_file_name, fw_callback, callback_opaque);
+}
+
 int rom_add_elf_program(const char *name, void *data, size_t datasize,
                         size_t romsize, hwaddr addr);
 int rom_load_all(void);
include/hw/nvram/fw_cfg.h +1 -1
index 72b1549..da4f5c5 100644
@@ -75,7 +75,7 @@ void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,
                      size_t len);
 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
                               FWCfgReadCallback callback, void *callback_opaque,
-                              void *data, size_t len);
+                              void *data, size_t len, size_t max_len);
 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
                         hwaddr crl_addr, hwaddr data_addr);
 
hw/core/loader.c +9 -6
index 2bf6b8f..ad6ec67 100644
@@ -720,9 +720,11 @@ err:
     return -1;
 }
 
-void *rom_add_blob(const char *name, const void *blob, size_t len,
-                   hwaddr addr, const char *fw_file_name,
-                   FWCfgReadCallback fw_callback, void *callback_opaque)
+void *rom_add_blob_resizeable(const char *name, const void *blob,
+                              size_t len, size_t max_len,
+                              hwaddr addr, const char *fw_file_name,
+                              FWCfgReadCallback fw_callback,
+                              void *callback_opaque)
 {
     Rom *rom;
     void *data = NULL;
@@ -730,9 +732,10 @@ void *rom_add_blob(const char *name, const void *blob, size_t len,
     rom           = g_malloc0(sizeof(*rom));
     rom->name     = g_strdup(name);
     rom->addr     = addr;
-    rom->romsize  = len;
-    rom->datasize = len;
+    rom->romsize  = max_len;
+    rom->datasize = max_len;
     rom->data     = g_malloc0(rom->datasize);
+    assert(len <= rom->datasize);
     memcpy(rom->data, blob, len);
     rom_insert(rom);
     if (fw_file_name && fw_cfg) {
@@ -748,7 +751,7 @@ void *rom_add_blob(const char *name, const void *blob, size_t len,
 
         fw_cfg_add_file_callback(fw_cfg, fw_file_name,
                                  fw_callback, callback_opaque,
-                                 data, rom->romsize);
+                                 data, rom->romsize, rom->datasize);
     }
     return data;
 }
hw/nvram/fw_cfg.c +74 -5

156 lines not on this page · raw message

Signature · 1 line
MST
#2 lore raw

PATCH 3/3 loader: mark MR for resizeable blobs as extendable

This makes migration from older QEMU versions
more robust.

---
 hw/core/loader.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/core/loader.c +7 -0
index ad6ec67..fc00a87 100644
@@ -745,6 +745,13 @@ void *rom_add_blob_resizeable(const char *name, const void *blob,
 
         if (rom_file_has_mr) {
             data = rom_set_mr(rom, OBJECT(fw_cfg), devpath);
+            /* If there's padding at tail, blob is resizeable.
+             * Set flag to allow migration from older QEMU versions
+             * where this region could have been smaller.
+             */
+            if (max_len != len) {
+                memory_region_permit_extendable_migration(rom->mr);
+            }
         } else {
             data = rom->data;
         }
Signature · 1 line
MST
#1 lore raw

PATCH 2/3 migration: load smaller RAMBlock to a bigger one if permitted

In reply to Michael S. Tsirkin

From: Igor Mammedov <[email protected]>

Add API to mark memory region as extend-able on migration, to allow migration code to load smaller RAMBlock into a bigger one on destination QEMU instance.

This will allow to fix broken migration from QEMU 1.7/2.0 to QEMU 2.1 due to ACPI tables size changes across 1.7/2.0/2.1 versions by marking ACPI tables ROM blob as extend-able. So that smaller tables from previous version could be always migrated to a bigger rom blob on new version.

Credits-for-idea: Michael S. Tsirkin <[email protected]>

---
 include/exec/memory.h   | 11 +++++++++++
 include/exec/ram_addr.h |  3 +++
 arch_init.c             | 22 +++++++++++++++++-----
 exec.c                  |  8 ++++++++
 memory.c                |  5 +++++
 5 files changed, 44 insertions(+), 5 deletions(-)
include/exec/memory.h +11 -0
index e2c8e3e..f96ddbb 100644
@@ -894,6 +894,17 @@ bool memory_region_present(MemoryRegion *container, hwaddr addr);
 bool memory_region_is_mapped(MemoryRegion *mr);
 
 /**
+ * memory_region_permit_extendable_migration: marks #MemoryRegion
+ * as extendable on migration, allowing the migration code to load
+ * source memory block of smaller size than destination memory block
+ * at migration time
+ *
+ * @mr: a #MemoryRegion whose #RAMBlock should be marked as
+ *      extendable on migration
+ */
+void memory_region_permit_extendable_migration(MemoryRegion *mr);
+
+/**
  * memory_region_find: translate an address/size relative to a
  * MemoryRegion into a #MemoryRegionSection.
  *
include/exec/ram_addr.h +3 -0
index 6593be1..7a6b782 100644
@@ -34,6 +34,9 @@ void *qemu_get_ram_ptr(ram_addr_t addr);
 void qemu_ram_free(ram_addr_t addr);
 void qemu_ram_free_from_ptr(ram_addr_t addr);
 
+#define RAM_EXTENDABLE_ON_MIGRATE (1U << 31)
+void qemu_ram_set_extendable_on_migration(ram_addr_t addr);
+
 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start,
                                                  ram_addr_t length,
                                                  unsigned client)
arch_init.c +17 -5
index 8ddaf35..2c0c238 100644
@@ -1071,11 +1071,23 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 
                 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
                     if (!strncmp(id, block->idstr, sizeof(id))) {
-                        if (block->length != length) {
-                            error_report("Length mismatch: %s: " RAM_ADDR_FMT
-                                         " in != " RAM_ADDR_FMT, id, length,
-                                         block->length);
-                            ret =  -EINVAL;
+                        if (block->flags & RAM_EXTENDABLE_ON_MIGRATE) {
+                            if (block->length < length) {
+                                error_report("Length too big: %s: " RAM_ADDR_FMT
+                                             " in > " RAM_ADDR_FMT, id, length,
+                                             block->length);
+                                ret =  -EINVAL;
+                            } else {
+                                memset(block->host, 0, block->length);
+                            }
+                        } else {
+                            if (block->length != length) {
+                                error_report("Length mismatch: %s: "
+                                             RAM_ADDR_FMT " in != "
+                                             RAM_ADDR_FMT,
+                                             id, length, block->length);
+                                ret =  -EINVAL;
+                            }
                         }
                         break;
                     }
exec.c +8 -0
index 765bd94..02536f8e 100644
@@ -1214,6 +1214,14 @@ void qemu_ram_unset_idstr(ram_addr_t addr)
     }
 }
 
+void qemu_ram_set_extendable_on_migration(ram_addr_t addr)
+{
+    RAMBlock *block = find_ram_block(addr);
+
+    assert(block != NULL);
+    block->flags |= RAM_EXTENDABLE_ON_MIGRATE;
+}
+
 static int memory_try_enable_merging(void *addr, size_t len)
 {
     if (!qemu_opt_get_bool(qemu_get_machine_opts(), "mem-merge", true)) {
memory.c +5 -0
index 64d7176..744c746 100644
@@ -1791,6 +1791,11 @@ bool memory_region_is_mapped(MemoryRegion *mr)
     return mr->container ? true : false;
 }
 
+void memory_region_permit_extendable_migration(MemoryRegion *mr)
+{
+    qemu_ram_set_extendable_on_migration(mr->ram_addr);
+}
+
 MemoryRegionSection memory_region_find(MemoryRegion *mr,
                                        hwaddr addr, uint64_t size)
 {
Signature · 1 line
MST
#3 lore raw
In reply to Michael S. Tsirkin
Quoted 18 lines · * Michael S. Tsirkin ([email protected]) wrote:

Support resizeable blobs: we allocate more memory than currently available in the blob, which can later be filled in.

---
 include/hw/loader.h       | 14 +++++++--
 include/hw/nvram/fw_cfg.h |  2 +-
 hw/core/loader.c          | 15 +++++----
 hw/nvram/fw_cfg.c         | 79 ++++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 96 insertions(+), 14 deletions(-)
hw/nvram/fw_cfg.c +5 -0
+static bool fw_cfg_len_needed(void *opaque, int version_id)
+{
+    FWCfgState *s = FW_CFG(opaque);
+    int i, j;
+

hw/nvram/fw_cfg.c static bool is_version_1(void *opaque, int version_id) · line 430

Quoted 10 lines
hw/nvram/fw_cfg.c +10 -0
+    for (i = 0; i < ARRAY_SIZE(s->entries); ++i) {
+        for (j = 0; j < ARRAY_SIZE(s->entries[0]); ++j) {
+            if (s->entries[i][j].len != s->entries[i][j].max_len) {
+                return true;
+            }
+        }
+    }
+
+    return false;
+}

This feels a bit delicate; it means that increasing the 'max_len' changes the expected migration stream, which seems odd for a parameter whose job is to make things easier to migrate.

Dave

Quoted 88 lines
hw/nvram/fw_cfg.c +19 -5
+
 static const VMStateDescription vmstate_fw_cfg = {
     .name = "fw_cfg",
     .version_id = 2,
     .minimum_version_id = 1,
+    .post_load = fw_cfg_post_load,
+    .pre_save = fw_cfg_pre_save,
     .fields = (VMStateField[]) {
         VMSTATE_UINT16(cur_entry, FWCfgState),
         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
+        VMSTATE_ARRAY_TEST(len, FWCfgState, FW_CFG_LEN_ENTRIES,
+                           fw_cfg_len_needed,
+                           vmstate_info_uint32, uint32_t),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -388,23 +449,28 @@ static const VMStateDescription vmstate_fw_cfg = {
 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
                                            FWCfgReadCallback callback,
                                            void *callback_opaque,
-                                           void *data, size_t len)
+                                           void *data, size_t len,
+                                           size_t max_len)
 {
     int arch = !!(key & FW_CFG_ARCH_LOCAL);
 
+    assert(len <= max_len);
+
     key &= FW_CFG_ENTRY_MASK;
 
     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
 
     s->entries[arch][key].data = data;
     s->entries[arch][key].len = (uint32_t)len;
+    s->entries[arch][key].reset_len = (uint32_t)len;
+    s->entries[arch][key].max_len = (uint32_t)max_len;
     s->entries[arch][key].read_callback = callback;
     s->entries[arch][key].callback_opaque = callback_opaque;
 }
 
 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
 {
-    fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
+    fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len, len);
 }
 
 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
@@ -454,13 +520,15 @@ void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
 
     s->entries[arch][key].data = data;
     s->entries[arch][key].len = (uint32_t)len;
+    s->entries[arch][key].reset_len = (uint32_t)len;
+    s->entries[arch][key].max_len = (uint32_t)len;
     s->entries[arch][key].callback_opaque = callback_opaque;
     s->entries[arch][key].callback = callback;
 }
 
 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
                               FWCfgReadCallback callback, void *callback_opaque,
-                              void *data, size_t len)
+                              void *data, size_t len, size_t max_len)
 {
     int i, index;
     size_t dsize;
@@ -475,7 +543,8 @@ void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
     assert(index < FW_CFG_FILE_SLOTS);
 
     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
-                                   callback, callback_opaque, data, len);
+                                   callback, callback_opaque, data, len,
+                                   max_len);
 
     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
             filename);
@@ -496,7 +565,7 @@ void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
                      void *data, size_t len)
 {
-    fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
+    fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, len);
 }
 
 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
Signature · 1 line
MST

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#4 lore raw
Quoted 34 lines · Dr. David Alan Gilbert
Quoted 18 lines · * Michael S. Tsirkin ([email protected]) wrote:

Support resizeable blobs: we allocate more memory than currently available in the blob, which can later be filled in.

---
 include/hw/loader.h       | 14 +++++++--
 include/hw/nvram/fw_cfg.h |  2 +-
 hw/core/loader.c          | 15 +++++----
 hw/nvram/fw_cfg.c         | 79 ++++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 96 insertions(+), 14 deletions(-)
hw/nvram/fw_cfg.c +5 -0
+static bool fw_cfg_len_needed(void *opaque, int version_id)
+{
+    FWCfgState *s = FW_CFG(opaque);
+    int i, j;
+

hw/nvram/fw_cfg.c static bool is_version_1(void *opaque, int version_id) · line 430

Quoted 10 lines
hw/nvram/fw_cfg.c +10 -0
+    for (i = 0; i < ARRAY_SIZE(s->entries); ++i) {
+        for (j = 0; j < ARRAY_SIZE(s->entries[0]); ++j) {
+            if (s->entries[i][j].len != s->entries[i][j].max_len) {
+                return true;
+            }
+        }
+    }
+
+    return false;
+}

This feels a bit delicate; it means that increasing the 'max_len' changes the expected migration stream, which seems odd for a parameter whose job is to make things easier to migrate.

Quoted 1 line

Dave

It's an API issue - so you are more comfortable with an explicit flag?

Quoted 90 lines
Quoted 88 lines
hw/nvram/fw_cfg.c +19 -5
+
 static const VMStateDescription vmstate_fw_cfg = {
     .name = "fw_cfg",
     .version_id = 2,
     .minimum_version_id = 1,
+    .post_load = fw_cfg_post_load,
+    .pre_save = fw_cfg_pre_save,
     .fields = (VMStateField[]) {
         VMSTATE_UINT16(cur_entry, FWCfgState),
         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
+        VMSTATE_ARRAY_TEST(len, FWCfgState, FW_CFG_LEN_ENTRIES,
+                           fw_cfg_len_needed,
+                           vmstate_info_uint32, uint32_t),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -388,23 +449,28 @@ static const VMStateDescription vmstate_fw_cfg = {
 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
                                            FWCfgReadCallback callback,
                                            void *callback_opaque,
-                                           void *data, size_t len)
+                                           void *data, size_t len,
+                                           size_t max_len)
 {
     int arch = !!(key & FW_CFG_ARCH_LOCAL);
 
+    assert(len <= max_len);
+
     key &= FW_CFG_ENTRY_MASK;
 
     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
 
     s->entries[arch][key].data = data;
     s->entries[arch][key].len = (uint32_t)len;
+    s->entries[arch][key].reset_len = (uint32_t)len;
+    s->entries[arch][key].max_len = (uint32_t)max_len;
     s->entries[arch][key].read_callback = callback;
     s->entries[arch][key].callback_opaque = callback_opaque;
 }
 
 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
 {
-    fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
+    fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len, len);
 }
 
 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
@@ -454,13 +520,15 @@ void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
 
     s->entries[arch][key].data = data;
     s->entries[arch][key].len = (uint32_t)len;
+    s->entries[arch][key].reset_len = (uint32_t)len;
+    s->entries[arch][key].max_len = (uint32_t)len;
     s->entries[arch][key].callback_opaque = callback_opaque;
     s->entries[arch][key].callback = callback;
 }
 
 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
                               FWCfgReadCallback callback, void *callback_opaque,
-                              void *data, size_t len)
+                              void *data, size_t len, size_t max_len)
 {
     int i, index;
     size_t dsize;
@@ -475,7 +543,8 @@ void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
     assert(index < FW_CFG_FILE_SLOTS);
 
     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
-                                   callback, callback_opaque, data, len);
+                                   callback, callback_opaque, data, len,
+                                   max_len);
 
     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
             filename);
@@ -496,7 +565,7 @@ void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
                      void *data, size_t len)
 {
-    fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
+    fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, len);
 }
 
 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
Signature · 1 line
MST

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#5 lore raw

Il 28/07/2014 21:07, Michael S. Tsirkin ha scritto:

Quoted 35 lines · Dr. David Alan Gilbert
Quoted 34 lines · * Michael S. Tsirkin ([email protected]) wrote:
Quoted 18 lines

Support resizeable blobs: we allocate more memory than currently available in the blob, which can later be filled in.

---
 include/hw/loader.h       | 14 +++++++--
 include/hw/nvram/fw_cfg.h |  2 +-
 hw/core/loader.c          | 15 +++++----
 hw/nvram/fw_cfg.c         | 79 ++++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 96 insertions(+), 14 deletions(-)
hw/nvram/fw_cfg.c +5 -0
+static bool fw_cfg_len_needed(void *opaque, int version_id)
+{
+    FWCfgState *s = FW_CFG(opaque);
+    int i, j;
+

hw/nvram/fw_cfg.c static bool is_version_1(void *opaque, int version_id) · line 430

Quoted 10 lines
hw/nvram/fw_cfg.c +10 -0
+    for (i = 0; i < ARRAY_SIZE(s->entries); ++i) {
+        for (j = 0; j < ARRAY_SIZE(s->entries[0]); ++j) {
+            if (s->entries[i][j].len != s->entries[i][j].max_len) {
+                return true;
+            }
+        }
+    }
+
+    return false;
+}

This feels a bit delicate; it means that increasing the 'max_len' changes the expected migration stream, which seems odd for a parameter whose job is to make things easier to migrate.

Quoted 4 lines
Quoted 1 line

Dave

It's an API issue - so you are more comfortable with an explicit flag?

Yeah, that would help but I think these patches are too much for 2.1.

Paolo

Quoted 90 lines
Quoted 90 lines
Quoted 88 lines
hw/nvram/fw_cfg.c +19 -5
+
 static const VMStateDescription vmstate_fw_cfg = {
     .name = "fw_cfg",
     .version_id = 2,
     .minimum_version_id = 1,
+    .post_load = fw_cfg_post_load,
+    .pre_save = fw_cfg_pre_save,
     .fields = (VMStateField[]) {
         VMSTATE_UINT16(cur_entry, FWCfgState),
         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
+        VMSTATE_ARRAY_TEST(len, FWCfgState, FW_CFG_LEN_ENTRIES,
+                           fw_cfg_len_needed,
+                           vmstate_info_uint32, uint32_t),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -388,23 +449,28 @@ static const VMStateDescription vmstate_fw_cfg = {
 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
                                            FWCfgReadCallback callback,
                                            void *callback_opaque,
-                                           void *data, size_t len)
+                                           void *data, size_t len,
+                                           size_t max_len)
 {
     int arch = !!(key & FW_CFG_ARCH_LOCAL);
 
+    assert(len <= max_len);
+
     key &= FW_CFG_ENTRY_MASK;
 
     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
 
     s->entries[arch][key].data = data;
     s->entries[arch][key].len = (uint32_t)len;
+    s->entries[arch][key].reset_len = (uint32_t)len;
+    s->entries[arch][key].max_len = (uint32_t)max_len;
     s->entries[arch][key].read_callback = callback;
     s->entries[arch][key].callback_opaque = callback_opaque;
 }
 
 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
 {
-    fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
+    fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len, len);
 }
 
 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
@@ -454,13 +520,15 @@ void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
 
     s->entries[arch][key].data = data;
     s->entries[arch][key].len = (uint32_t)len;
+    s->entries[arch][key].reset_len = (uint32_t)len;
+    s->entries[arch][key].max_len = (uint32_t)len;
     s->entries[arch][key].callback_opaque = callback_opaque;
     s->entries[arch][key].callback = callback;
 }
 
 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
                               FWCfgReadCallback callback, void *callback_opaque,
-                              void *data, size_t len)
+                              void *data, size_t len, size_t max_len)
 {
     int i, index;
     size_t dsize;
@@ -475,7 +543,8 @@ void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
     assert(index < FW_CFG_FILE_SLOTS);
 
     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
-                                   callback, callback_opaque, data, len);
+                                   callback, callback_opaque, data, len,
+                                   max_len);
 
     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
             filename);
@@ -496,7 +565,7 @@ void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
                      void *data, size_t len)
 {
-    fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
+    fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, len);
 }
 
 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
Signature · 1 line
MST

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#6 lore raw
In reply to Michael S. Tsirkin
Quoted 35 lines · * Michael S. Tsirkin ([email protected]) wrote:
Quoted 34 lines · Dr. David Alan Gilbert
Quoted 18 lines · * Michael S. Tsirkin ([email protected]) wrote:

Support resizeable blobs: we allocate more memory than currently available in the blob, which can later be filled in.

---
 include/hw/loader.h       | 14 +++++++--
 include/hw/nvram/fw_cfg.h |  2 +-
 hw/core/loader.c          | 15 +++++----
 hw/nvram/fw_cfg.c         | 79 ++++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 96 insertions(+), 14 deletions(-)
hw/nvram/fw_cfg.c +5 -0
+static bool fw_cfg_len_needed(void *opaque, int version_id)
+{
+    FWCfgState *s = FW_CFG(opaque);
+    int i, j;
+

hw/nvram/fw_cfg.c static bool is_version_1(void *opaque, int version_id) · line 430

Quoted 10 lines
hw/nvram/fw_cfg.c +10 -0
+    for (i = 0; i < ARRAY_SIZE(s->entries); ++i) {
+        for (j = 0; j < ARRAY_SIZE(s->entries[0]); ++j) {
+            if (s->entries[i][j].len != s->entries[i][j].max_len) {
+                return true;
+            }
+        }
+    }
+
+    return false;
+}

This feels a bit delicate; it means that increasing the 'max_len' changes the expected migration stream, which seems odd for a parameter whose job is to make things easier to migrate.

Quoted 4 lines
Quoted 1 line

Dave

It's an API issue - so you are more comfortable with an explicit flag?

Yes, if it was something that was just switched on with new machine types I wouldn't worry as much.

Dave

Quoted 84 lines
Quoted 84 lines
Quoted 84 lines
hw/nvram/fw_cfg.c +19 -5
+
 static const VMStateDescription vmstate_fw_cfg = {
     .name = "fw_cfg",
     .version_id = 2,
     .minimum_version_id = 1,
+    .post_load = fw_cfg_post_load,
+    .pre_save = fw_cfg_pre_save,
     .fields = (VMStateField[]) {
         VMSTATE_UINT16(cur_entry, FWCfgState),
         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
+        VMSTATE_ARRAY_TEST(len, FWCfgState, FW_CFG_LEN_ENTRIES,
+                           fw_cfg_len_needed,
+                           vmstate_info_uint32, uint32_t),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -388,23 +449,28 @@ static const VMStateDescription vmstate_fw_cfg = {
 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
                                            FWCfgReadCallback callback,
                                            void *callback_opaque,
-                                           void *data, size_t len)
+                                           void *data, size_t len,
+                                           size_t max_len)
 {
     int arch = !!(key & FW_CFG_ARCH_LOCAL);
 
+    assert(len <= max_len);
+
     key &= FW_CFG_ENTRY_MASK;
 
     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
 
     s->entries[arch][key].data = data;
     s->entries[arch][key].len = (uint32_t)len;
+    s->entries[arch][key].reset_len = (uint32_t)len;
+    s->entries[arch][key].max_len = (uint32_t)max_len;
     s->entries[arch][key].read_callback = callback;
     s->entries[arch][key].callback_opaque = callback_opaque;
 }
 
 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
 {
-    fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
+    fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len, len);
 }
 
 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
@@ -454,13 +520,15 @@ void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
 
     s->entries[arch][key].data = data;
     s->entries[arch][key].len = (uint32_t)len;
+    s->entries[arch][key].reset_len = (uint32_t)len;
+    s->entries[arch][key].max_len = (uint32_t)len;
     s->entries[arch][key].callback_opaque = callback_opaque;
     s->entries[arch][key].callback = callback;
 }
 
 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
                               FWCfgReadCallback callback, void *callback_opaque,
-                              void *data, size_t len)
+                              void *data, size_t len, size_t max_len)
 {
     int i, index;
     size_t dsize;
@@ -475,7 +543,8 @@ void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
     assert(index < FW_CFG_FILE_SLOTS);
 
     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
-                                   callback, callback_opaque, data, len);
+                                   callback, callback_opaque, data, len,
+                                   max_len);
 
     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
             filename);
@@ -496,7 +565,7 @@ void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
                      void *data, size_t len)
 {
-    fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
+    fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, len);
 }
 
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
hw/nvram/fw_cfg.c +0 -0
 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
Signature · 1 line
MST

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK