2017-04-20 13:41:20 +02:00
|
|
|
/*
|
|
|
|
* VMState interpreter
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2017 Red Hat Inc
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Juan Quintela <quintela@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:16:54 +01:00
|
|
|
#include "qemu/osdep.h"
|
2017-04-24 20:07:27 +02:00
|
|
|
#include "migration.h"
|
2013-11-28 15:01:15 +01:00
|
|
|
#include "migration/vmstate.h"
|
2018-05-03 21:50:51 +02:00
|
|
|
#include "savevm.h"
|
2023-10-03 08:55:38 +02:00
|
|
|
#include "qapi/error.h"
|
2020-12-11 18:11:48 +01:00
|
|
|
#include "qapi/qmp/json-writer.h"
|
2017-04-20 18:52:18 +02:00
|
|
|
#include "qemu-file.h"
|
2013-11-28 15:01:15 +01:00
|
|
|
#include "qemu/bitops.h"
|
2015-01-21 11:14:47 +01:00
|
|
|
#include "qemu/error-report.h"
|
2014-03-11 00:42:29 +01:00
|
|
|
#include "trace.h"
|
2013-11-28 15:01:15 +01:00
|
|
|
|
2017-09-25 13:29:15 +02:00
|
|
|
static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
|
2020-12-11 18:11:48 +01:00
|
|
|
void *opaque, JSONWriter *vmdesc);
|
2013-11-28 15:01:15 +01:00
|
|
|
static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque);
|
|
|
|
|
2023-09-06 22:47:22 +02:00
|
|
|
/* Whether this field should exist for either save or load the VM? */
|
|
|
|
static bool
|
|
|
|
vmstate_field_exists(const VMStateDescription *vmsd, const VMStateField *field,
|
|
|
|
void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
if (field->field_exists) {
|
|
|
|
/* If there's the function checker, that's the solo truth */
|
|
|
|
result = field->field_exists(opaque, version_id);
|
|
|
|
trace_vmstate_field_exists(vmsd->name, field->name, field->version_id,
|
|
|
|
version_id, result);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Otherwise, we only save/load if field version is same or older.
|
|
|
|
* For example, when loading from an old binary with old version,
|
|
|
|
* we ignore new fields with newer version_ids.
|
|
|
|
*/
|
|
|
|
result = field->version_id <= version_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-14 14:29:30 +01:00
|
|
|
static int vmstate_n_elems(void *opaque, const VMStateField *field)
|
2014-04-03 18:50:26 +02:00
|
|
|
{
|
|
|
|
int n_elems = 1;
|
|
|
|
|
|
|
|
if (field->flags & VMS_ARRAY) {
|
|
|
|
n_elems = field->num;
|
|
|
|
} else if (field->flags & VMS_VARRAY_INT32) {
|
2020-10-20 05:10:43 +02:00
|
|
|
n_elems = *(int32_t *)(opaque + field->num_offset);
|
2014-04-03 18:50:26 +02:00
|
|
|
} else if (field->flags & VMS_VARRAY_UINT32) {
|
2020-10-20 05:10:43 +02:00
|
|
|
n_elems = *(uint32_t *)(opaque + field->num_offset);
|
2014-04-03 18:50:26 +02:00
|
|
|
} else if (field->flags & VMS_VARRAY_UINT16) {
|
2020-10-20 05:10:43 +02:00
|
|
|
n_elems = *(uint16_t *)(opaque + field->num_offset);
|
2014-04-03 18:50:26 +02:00
|
|
|
} else if (field->flags & VMS_VARRAY_UINT8) {
|
2020-10-20 05:10:43 +02:00
|
|
|
n_elems = *(uint8_t *)(opaque + field->num_offset);
|
2014-04-03 18:50:26 +02:00
|
|
|
}
|
|
|
|
|
2016-01-11 13:40:22 +01:00
|
|
|
if (field->flags & VMS_MULTIPLY_ELEMENTS) {
|
|
|
|
n_elems *= field->num;
|
|
|
|
}
|
|
|
|
|
2016-06-14 11:36:26 +02:00
|
|
|
trace_vmstate_n_elems(field->name, n_elems);
|
2014-04-03 18:50:26 +02:00
|
|
|
return n_elems;
|
|
|
|
}
|
|
|
|
|
2018-11-14 14:29:30 +01:00
|
|
|
static int vmstate_size(void *opaque, const VMStateField *field)
|
2014-04-03 18:50:26 +02:00
|
|
|
{
|
|
|
|
int size = field->size;
|
|
|
|
|
|
|
|
if (field->flags & VMS_VBUFFER) {
|
2020-10-20 05:10:43 +02:00
|
|
|
size = *(int32_t *)(opaque + field->size_offset);
|
2014-04-03 18:50:26 +02:00
|
|
|
if (field->flags & VMS_MULTIPLY) {
|
|
|
|
size *= field->size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-11-14 14:29:30 +01:00
|
|
|
static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
|
|
|
|
void *opaque)
|
2014-04-03 18:50:26 +02:00
|
|
|
{
|
2017-02-22 17:01:16 +01:00
|
|
|
if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
|
|
|
|
gsize size = vmstate_size(opaque, field);
|
|
|
|
size *= vmstate_n_elems(opaque, field);
|
|
|
|
if (size) {
|
|
|
|
*(void **)ptr = g_malloc(size);
|
2014-05-30 11:34:19 +02:00
|
|
|
}
|
2014-04-03 18:50:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-28 15:01:15 +01:00
|
|
|
int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque, int version_id)
|
|
|
|
{
|
2018-11-14 14:29:30 +01:00
|
|
|
const VMStateField *field = vmsd->fields;
|
2015-01-21 11:14:48 +01:00
|
|
|
int ret = 0;
|
2013-11-28 15:01:15 +01:00
|
|
|
|
2015-01-21 11:14:48 +01:00
|
|
|
trace_vmstate_load_state(vmsd->name, version_id);
|
2013-11-28 15:01:15 +01:00
|
|
|
if (version_id > vmsd->version_id) {
|
2017-01-19 20:00:53 +01:00
|
|
|
error_report("%s: incoming version_id %d is too new "
|
|
|
|
"for local version_id %d",
|
|
|
|
vmsd->name, version_id, vmsd->version_id);
|
2015-01-21 11:14:48 +01:00
|
|
|
trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
|
2013-11-28 15:01:15 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (version_id < vmsd->minimum_version_id) {
|
2017-01-19 20:00:53 +01:00
|
|
|
error_report("%s: incoming version_id %d is too old "
|
|
|
|
"for local minimum version_id %d",
|
|
|
|
vmsd->name, version_id, vmsd->minimum_version_id);
|
2015-01-21 11:14:48 +01:00
|
|
|
trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
|
2014-04-03 18:52:28 +02:00
|
|
|
return -EINVAL;
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
|
|
|
if (vmsd->pre_load) {
|
2023-09-21 14:13:07 +02:00
|
|
|
ret = vmsd->pre_load(opaque);
|
2013-11-28 15:01:15 +01:00
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (field->name) {
|
2015-01-21 11:14:48 +01:00
|
|
|
trace_vmstate_load_state_field(vmsd->name, field->name);
|
2023-09-06 22:47:22 +02:00
|
|
|
if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
|
2017-02-22 17:01:16 +01:00
|
|
|
void *first_elem = opaque + field->offset;
|
2014-04-03 18:50:26 +02:00
|
|
|
int i, n_elems = vmstate_n_elems(opaque, field);
|
|
|
|
int size = vmstate_size(opaque, field);
|
|
|
|
|
2017-02-22 17:01:16 +01:00
|
|
|
vmstate_handle_alloc(first_elem, field, opaque);
|
|
|
|
if (field->flags & VMS_POINTER) {
|
|
|
|
first_elem = *(void **)first_elem;
|
2017-03-10 05:44:02 +01:00
|
|
|
assert(first_elem || !n_elems || !size);
|
2017-02-22 17:01:16 +01:00
|
|
|
}
|
2013-11-28 15:01:15 +01:00
|
|
|
for (i = 0; i < n_elems; i++) {
|
2017-02-22 17:01:15 +01:00
|
|
|
void *curr_elem = first_elem + size * i;
|
2013-11-28 15:01:15 +01:00
|
|
|
|
|
|
|
if (field->flags & VMS_ARRAY_OF_POINTER) {
|
2017-02-22 17:01:15 +01:00
|
|
|
curr_elem = *(void **)curr_elem;
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
2017-03-10 05:44:02 +01:00
|
|
|
if (!curr_elem && size) {
|
2017-02-22 17:01:17 +01:00
|
|
|
/* if null pointer check placeholder and do not follow */
|
|
|
|
assert(field->flags & VMS_ARRAY_OF_POINTER);
|
|
|
|
ret = vmstate_info_nullptr.get(f, curr_elem, size, NULL);
|
|
|
|
} else if (field->flags & VMS_STRUCT) {
|
2017-02-22 17:01:15 +01:00
|
|
|
ret = vmstate_load_state(f, field->vmsd, curr_elem,
|
2013-11-28 15:01:15 +01:00
|
|
|
field->vmsd->version_id);
|
2018-04-25 17:27:30 +02:00
|
|
|
} else if (field->flags & VMS_VSTRUCT) {
|
|
|
|
ret = vmstate_load_state(f, field->vmsd, curr_elem,
|
|
|
|
field->struct_version_id);
|
2013-11-28 15:01:15 +01:00
|
|
|
} else {
|
2017-02-22 17:01:15 +01:00
|
|
|
ret = field->info->get(f, curr_elem, size, field);
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
2014-04-05 20:16:22 +02:00
|
|
|
if (ret >= 0) {
|
|
|
|
ret = qemu_file_get_error(f);
|
|
|
|
}
|
2013-11-28 15:01:15 +01:00
|
|
|
if (ret < 0) {
|
2014-04-05 20:16:22 +02:00
|
|
|
qemu_file_set_error(f, ret);
|
2016-09-27 20:56:04 +02:00
|
|
|
error_report("Failed to load %s:%s", vmsd->name,
|
|
|
|
field->name);
|
2014-03-11 00:42:29 +01:00
|
|
|
trace_vmstate_load_field_error(field->name, ret);
|
2013-11-28 15:01:15 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 18:50:31 +02:00
|
|
|
} else if (field->flags & VMS_MUST_EXIST) {
|
2015-01-21 11:14:47 +01:00
|
|
|
error_report("Input validation failed: %s/%s",
|
|
|
|
vmsd->name, field->name);
|
2014-04-03 18:50:31 +02:00
|
|
|
return -1;
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
|
|
|
field++;
|
|
|
|
}
|
2022-01-13 20:44:51 +01:00
|
|
|
assert(field->flags == VMS_END);
|
2013-11-28 15:01:15 +01:00
|
|
|
ret = vmstate_subsection_load(f, vmsd, opaque);
|
|
|
|
if (ret != 0) {
|
2023-10-24 10:40:41 +02:00
|
|
|
qemu_file_set_error(f, ret);
|
2013-11-28 15:01:15 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (vmsd->post_load) {
|
2015-01-21 11:14:48 +01:00
|
|
|
ret = vmsd->post_load(opaque, version_id);
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
2015-01-21 11:14:48 +01:00
|
|
|
trace_vmstate_load_state_end(vmsd->name, "end", ret);
|
|
|
|
return ret;
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 14:29:30 +01:00
|
|
|
static int vmfield_name_num(const VMStateField *start,
|
|
|
|
const VMStateField *search)
|
2015-01-22 15:01:39 +01:00
|
|
|
{
|
2018-11-14 14:29:30 +01:00
|
|
|
const VMStateField *field;
|
2015-01-22 15:01:39 +01:00
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
for (field = start; field->name; field++) {
|
|
|
|
if (!strcmp(field->name, search->name)) {
|
|
|
|
if (field == search) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
found++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-14 14:29:30 +01:00
|
|
|
static bool vmfield_name_is_unique(const VMStateField *start,
|
|
|
|
const VMStateField *search)
|
2015-01-22 15:01:39 +01:00
|
|
|
{
|
2018-11-14 14:29:30 +01:00
|
|
|
const VMStateField *field;
|
2015-01-22 15:01:39 +01:00
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
for (field = start; field->name; field++) {
|
|
|
|
if (!strcmp(field->name, search->name)) {
|
|
|
|
found++;
|
|
|
|
/* name found more than once, so it's not unique */
|
|
|
|
if (found > 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-14 14:29:30 +01:00
|
|
|
static const char *vmfield_get_type_name(const VMStateField *field)
|
2015-01-22 15:01:39 +01:00
|
|
|
{
|
|
|
|
const char *type = "unknown";
|
|
|
|
|
|
|
|
if (field->flags & VMS_STRUCT) {
|
|
|
|
type = "struct";
|
2018-04-25 17:27:30 +02:00
|
|
|
} else if (field->flags & VMS_VSTRUCT) {
|
|
|
|
type = "vstruct";
|
2015-01-22 15:01:39 +01:00
|
|
|
} else if (field->info->name) {
|
|
|
|
type = field->info->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2018-11-14 14:29:30 +01:00
|
|
|
static bool vmsd_can_compress(const VMStateField *field)
|
2015-01-22 15:01:39 +01:00
|
|
|
{
|
|
|
|
if (field->field_exists) {
|
|
|
|
/* Dynamically existing fields mess up compression */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field->flags & VMS_STRUCT) {
|
2018-11-14 14:29:30 +01:00
|
|
|
const VMStateField *sfield = field->vmsd->fields;
|
2015-01-22 15:01:39 +01:00
|
|
|
while (sfield->name) {
|
|
|
|
if (!vmsd_can_compress(sfield)) {
|
|
|
|
/* Child elements can't compress, so can't we */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sfield++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field->vmsd->subsections) {
|
|
|
|
/* Subsections may come and go, better don't compress */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-11 18:11:48 +01:00
|
|
|
static void vmsd_desc_field_start(const VMStateDescription *vmsd,
|
|
|
|
JSONWriter *vmdesc,
|
2018-11-14 14:29:30 +01:00
|
|
|
const VMStateField *field, int i, int max)
|
2015-01-22 15:01:39 +01:00
|
|
|
{
|
|
|
|
char *name, *old_name;
|
|
|
|
bool is_array = max > 1;
|
|
|
|
bool can_compress = vmsd_can_compress(field);
|
|
|
|
|
|
|
|
if (!vmdesc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = g_strdup(field->name);
|
|
|
|
|
|
|
|
/* Field name is not unique, need to make it unique */
|
|
|
|
if (!vmfield_name_is_unique(vmsd->fields, field)) {
|
|
|
|
int num = vmfield_name_num(vmsd->fields, field);
|
|
|
|
old_name = name;
|
|
|
|
name = g_strdup_printf("%s[%d]", name, num);
|
|
|
|
g_free(old_name);
|
|
|
|
}
|
|
|
|
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_start_object(vmdesc, NULL);
|
|
|
|
json_writer_str(vmdesc, "name", name);
|
2015-01-22 15:01:39 +01:00
|
|
|
if (is_array) {
|
|
|
|
if (can_compress) {
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_int64(vmdesc, "array_len", max);
|
2015-01-22 15:01:39 +01:00
|
|
|
} else {
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_int64(vmdesc, "index", i);
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_str(vmdesc, "type", vmfield_get_type_name(field));
|
2015-01-22 15:01:39 +01:00
|
|
|
|
|
|
|
if (field->flags & VMS_STRUCT) {
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_start_object(vmdesc, "struct");
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free(name);
|
|
|
|
}
|
|
|
|
|
2020-12-11 18:11:48 +01:00
|
|
|
static void vmsd_desc_field_end(const VMStateDescription *vmsd,
|
|
|
|
JSONWriter *vmdesc,
|
2018-11-14 14:29:30 +01:00
|
|
|
const VMStateField *field, size_t size, int i)
|
2015-01-22 15:01:39 +01:00
|
|
|
{
|
|
|
|
if (!vmdesc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field->flags & VMS_STRUCT) {
|
|
|
|
/* We printed a struct in between, close its child object */
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_end_object(vmdesc);
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_int64(vmdesc, "size", size);
|
|
|
|
json_writer_end_object(vmdesc);
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
|
|
|
|
2014-10-15 09:39:14 +02:00
|
|
|
|
2023-10-24 10:40:38 +02:00
|
|
|
bool vmstate_section_needed(const VMStateDescription *vmsd, void *opaque)
|
2014-10-15 09:39:14 +02:00
|
|
|
{
|
|
|
|
if (vmsd->needed && !vmsd->needed(opaque)) {
|
|
|
|
/* optional section not needed */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-25 13:29:13 +02:00
|
|
|
int vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
|
2020-12-11 18:11:48 +01:00
|
|
|
void *opaque, JSONWriter *vmdesc_id)
|
2018-04-25 17:27:30 +02:00
|
|
|
{
|
2023-10-03 08:55:37 +02:00
|
|
|
return vmstate_save_state_v(f, vmsd, opaque, vmdesc_id, vmsd->version_id, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int vmstate_save_state_with_err(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque, JSONWriter *vmdesc_id, Error **errp)
|
|
|
|
{
|
|
|
|
return vmstate_save_state_v(f, vmsd, opaque, vmdesc_id, vmsd->version_id, errp);
|
2018-04-25 17:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
|
2023-10-03 08:55:37 +02:00
|
|
|
void *opaque, JSONWriter *vmdesc, int version_id, Error **errp)
|
2013-11-28 15:01:15 +01:00
|
|
|
{
|
2017-09-25 13:29:13 +02:00
|
|
|
int ret = 0;
|
2018-11-14 14:29:30 +01:00
|
|
|
const VMStateField *field = vmsd->fields;
|
2013-11-28 15:01:15 +01:00
|
|
|
|
2016-12-12 13:58:38 +01:00
|
|
|
trace_vmstate_save_state_top(vmsd->name);
|
|
|
|
|
2013-11-28 15:01:15 +01:00
|
|
|
if (vmsd->pre_save) {
|
2017-09-25 13:29:13 +02:00
|
|
|
ret = vmsd->pre_save(opaque);
|
|
|
|
trace_vmstate_save_state_pre_save_res(vmsd->name, ret);
|
|
|
|
if (ret) {
|
2023-10-03 08:55:38 +02:00
|
|
|
error_setg(errp, "pre-save failed: %s", vmsd->name);
|
2017-09-25 13:29:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
2015-01-22 15:01:39 +01:00
|
|
|
|
|
|
|
if (vmdesc) {
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_str(vmdesc, "vmsd_name", vmsd->name);
|
|
|
|
json_writer_int64(vmdesc, "version", version_id);
|
|
|
|
json_writer_start_array(vmdesc, "fields");
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
|
|
|
|
2013-11-28 15:01:15 +01:00
|
|
|
while (field->name) {
|
2023-09-06 22:47:22 +02:00
|
|
|
if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
|
2017-02-22 17:01:16 +01:00
|
|
|
void *first_elem = opaque + field->offset;
|
2014-04-03 18:50:26 +02:00
|
|
|
int i, n_elems = vmstate_n_elems(opaque, field);
|
|
|
|
int size = vmstate_size(opaque, field);
|
2023-05-04 13:38:39 +02:00
|
|
|
uint64_t old_offset, written_bytes;
|
2020-12-11 18:11:48 +01:00
|
|
|
JSONWriter *vmdesc_loop = vmdesc;
|
2014-04-03 18:50:26 +02:00
|
|
|
|
2016-12-12 13:58:38 +01:00
|
|
|
trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
|
2017-02-22 17:01:16 +01:00
|
|
|
if (field->flags & VMS_POINTER) {
|
|
|
|
first_elem = *(void **)first_elem;
|
2017-03-10 05:44:02 +01:00
|
|
|
assert(first_elem || !n_elems || !size);
|
2017-02-22 17:01:16 +01:00
|
|
|
}
|
2013-11-28 15:01:15 +01:00
|
|
|
for (i = 0; i < n_elems; i++) {
|
2017-02-22 17:01:15 +01:00
|
|
|
void *curr_elem = first_elem + size * i;
|
2013-11-28 15:01:15 +01:00
|
|
|
|
2015-01-22 15:01:39 +01:00
|
|
|
vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
|
2023-10-25 11:11:11 +02:00
|
|
|
old_offset = qemu_file_transferred(f);
|
2013-11-28 15:01:15 +01:00
|
|
|
if (field->flags & VMS_ARRAY_OF_POINTER) {
|
2017-02-22 17:01:15 +01:00
|
|
|
assert(curr_elem);
|
|
|
|
curr_elem = *(void **)curr_elem;
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
2017-03-10 05:44:02 +01:00
|
|
|
if (!curr_elem && size) {
|
2017-02-22 17:01:17 +01:00
|
|
|
/* if null pointer write placeholder and do not follow */
|
|
|
|
assert(field->flags & VMS_ARRAY_OF_POINTER);
|
2017-09-25 13:29:14 +02:00
|
|
|
ret = vmstate_info_nullptr.put(f, curr_elem, size, NULL,
|
|
|
|
NULL);
|
2017-02-22 17:01:17 +01:00
|
|
|
} else if (field->flags & VMS_STRUCT) {
|
2017-09-25 13:29:14 +02:00
|
|
|
ret = vmstate_save_state(f, field->vmsd, curr_elem,
|
|
|
|
vmdesc_loop);
|
2018-04-25 17:27:30 +02:00
|
|
|
} else if (field->flags & VMS_VSTRUCT) {
|
|
|
|
ret = vmstate_save_state_v(f, field->vmsd, curr_elem,
|
|
|
|
vmdesc_loop,
|
2023-10-03 08:55:37 +02:00
|
|
|
field->struct_version_id, errp);
|
2013-11-28 15:01:15 +01:00
|
|
|
} else {
|
2017-09-25 13:29:14 +02:00
|
|
|
ret = field->info->put(f, curr_elem, size, field,
|
|
|
|
vmdesc_loop);
|
|
|
|
}
|
|
|
|
if (ret) {
|
2023-10-03 08:55:38 +02:00
|
|
|
error_setg(errp, "Save of field %s/%s failed",
|
|
|
|
vmsd->name, field->name);
|
2019-01-21 11:23:13 +01:00
|
|
|
if (vmsd->post_save) {
|
|
|
|
vmsd->post_save(opaque);
|
|
|
|
}
|
2017-09-25 13:29:14 +02:00
|
|
|
return ret;
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
2015-01-22 15:01:39 +01:00
|
|
|
|
2023-10-25 11:11:11 +02:00
|
|
|
written_bytes = qemu_file_transferred(f) - old_offset;
|
2015-01-22 15:01:39 +01:00
|
|
|
vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
|
|
|
|
|
|
|
|
/* Compressed arrays only care about the first element */
|
|
|
|
if (vmdesc_loop && vmsd_can_compress(field)) {
|
|
|
|
vmdesc_loop = NULL;
|
|
|
|
}
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
2014-04-03 18:50:31 +02:00
|
|
|
} else {
|
|
|
|
if (field->flags & VMS_MUST_EXIST) {
|
2015-01-21 11:14:47 +01:00
|
|
|
error_report("Output state validation failed: %s/%s",
|
2014-04-03 18:50:31 +02:00
|
|
|
vmsd->name, field->name);
|
|
|
|
assert(!(field->flags & VMS_MUST_EXIST));
|
|
|
|
}
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
|
|
|
field++;
|
|
|
|
}
|
2022-01-13 20:44:51 +01:00
|
|
|
assert(field->flags == VMS_END);
|
2015-01-22 15:01:39 +01:00
|
|
|
|
|
|
|
if (vmdesc) {
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_end_array(vmdesc);
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-21 11:23:13 +01:00
|
|
|
ret = vmstate_subsection_save(f, vmsd, opaque, vmdesc);
|
|
|
|
|
|
|
|
if (vmsd->post_save) {
|
|
|
|
int ps_ret = vmsd->post_save(opaque);
|
|
|
|
if (!ret) {
|
|
|
|
ret = ps_ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription *
|
2014-09-23 14:09:54 +02:00
|
|
|
vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
|
2013-11-28 15:01:15 +01:00
|
|
|
{
|
2018-08-07 15:03:55 +02:00
|
|
|
while (sub && *sub) {
|
2014-09-23 14:09:54 +02:00
|
|
|
if (strcmp(idstr, (*sub)->name) == 0) {
|
|
|
|
return *sub;
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
|
|
|
sub++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque)
|
|
|
|
{
|
2015-01-21 11:14:48 +01:00
|
|
|
trace_vmstate_subsection_load(vmsd->name);
|
|
|
|
|
2013-11-28 15:01:15 +01:00
|
|
|
while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
|
2015-05-21 14:24:15 +02:00
|
|
|
char idstr[256], *idstr_ret;
|
2013-11-28 15:01:15 +01:00
|
|
|
int ret;
|
|
|
|
uint8_t version_id, len, size;
|
|
|
|
const VMStateDescription *sub_vmsd;
|
|
|
|
|
|
|
|
len = qemu_peek_byte(f, 1);
|
|
|
|
if (len < strlen(vmsd->name) + 1) {
|
|
|
|
/* subsection name has be be "section_name/a" */
|
2016-06-14 11:36:26 +02:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
|
2013-11-28 15:01:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-21 14:24:15 +02:00
|
|
|
size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
|
2013-11-28 15:01:15 +01:00
|
|
|
if (size != len) {
|
2016-06-14 11:36:26 +02:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
|
2013-11-28 15:01:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-21 14:24:15 +02:00
|
|
|
memcpy(idstr, idstr_ret, size);
|
2013-11-28 15:01:15 +01:00
|
|
|
idstr[size] = 0;
|
|
|
|
|
|
|
|
if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
|
2016-06-14 11:36:26 +02:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
|
|
|
|
/* it doesn't have a valid subsection name */
|
2013-11-28 15:01:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
|
|
|
|
if (sub_vmsd == NULL) {
|
2016-06-14 11:36:26 +02:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
|
2013-11-28 15:01:15 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
qemu_file_skip(f, 1); /* subsection */
|
|
|
|
qemu_file_skip(f, 1); /* len */
|
|
|
|
qemu_file_skip(f, len); /* idstr */
|
|
|
|
version_id = qemu_get_be32(f);
|
|
|
|
|
|
|
|
ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
|
|
|
|
if (ret) {
|
2016-06-14 11:36:26 +02:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
|
2013-11-28 15:01:15 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2015-01-21 11:14:48 +01:00
|
|
|
|
|
|
|
trace_vmstate_subsection_load_good(vmsd->name);
|
2013-11-28 15:01:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-25 13:29:15 +02:00
|
|
|
static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
|
2020-12-11 18:11:48 +01:00
|
|
|
void *opaque, JSONWriter *vmdesc)
|
2013-11-28 15:01:15 +01:00
|
|
|
{
|
2014-09-23 14:09:54 +02:00
|
|
|
const VMStateDescription **sub = vmsd->subsections;
|
2019-04-03 03:10:16 +02:00
|
|
|
bool vmdesc_has_subsections = false;
|
2017-09-25 13:29:15 +02:00
|
|
|
int ret = 0;
|
2013-11-28 15:01:15 +01:00
|
|
|
|
2016-12-12 13:58:38 +01:00
|
|
|
trace_vmstate_subsection_save_top(vmsd->name);
|
2018-08-07 15:03:55 +02:00
|
|
|
while (sub && *sub) {
|
2023-10-24 10:40:38 +02:00
|
|
|
if (vmstate_section_needed(*sub, opaque)) {
|
2016-12-12 13:58:38 +01:00
|
|
|
const VMStateDescription *vmsdsub = *sub;
|
2013-11-28 15:01:15 +01:00
|
|
|
uint8_t len;
|
|
|
|
|
2016-12-12 13:58:38 +01:00
|
|
|
trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
|
2015-01-22 15:01:39 +01:00
|
|
|
if (vmdesc) {
|
|
|
|
/* Only create subsection array when we have any */
|
2019-04-03 03:10:16 +02:00
|
|
|
if (!vmdesc_has_subsections) {
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_start_array(vmdesc, "subsections");
|
2019-04-03 03:10:16 +02:00
|
|
|
vmdesc_has_subsections = true;
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_start_object(vmdesc, NULL);
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
|
|
|
|
2013-11-28 15:01:15 +01:00
|
|
|
qemu_put_byte(f, QEMU_VM_SUBSECTION);
|
2016-12-12 13:58:38 +01:00
|
|
|
len = strlen(vmsdsub->name);
|
2013-11-28 15:01:15 +01:00
|
|
|
qemu_put_byte(f, len);
|
2016-12-12 13:58:38 +01:00
|
|
|
qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
|
|
|
|
qemu_put_be32(f, vmsdsub->version_id);
|
2017-09-25 13:29:15 +02:00
|
|
|
ret = vmstate_save_state(f, vmsdsub, opaque, vmdesc);
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-22 15:01:39 +01:00
|
|
|
|
|
|
|
if (vmdesc) {
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_end_object(vmdesc);
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|
|
|
|
sub++;
|
|
|
|
}
|
2015-01-22 15:01:39 +01:00
|
|
|
|
2019-04-03 03:10:16 +02:00
|
|
|
if (vmdesc_has_subsections) {
|
2020-12-11 18:11:48 +01:00
|
|
|
json_writer_end_array(vmdesc);
|
2015-01-22 15:01:39 +01:00
|
|
|
}
|
2017-09-25 13:29:15 +02:00
|
|
|
|
|
|
|
return ret;
|
2013-11-28 15:01:15 +01:00
|
|
|
}
|