2017-04-20 19:41:20 +08: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-27 02:16:54 +08:00
|
|
|
#include "qemu/osdep.h"
|
2017-04-25 02:07:27 +08:00
|
|
|
#include "migration.h"
|
2013-11-28 22:01:15 +08:00
|
|
|
#include "migration/vmstate.h"
|
2018-05-04 03:50:51 +08:00
|
|
|
#include "savevm.h"
|
2017-04-21 00:52:18 +08:00
|
|
|
#include "qemu-file.h"
|
2013-11-28 22:01:15 +08:00
|
|
|
#include "qemu/bitops.h"
|
2015-01-21 18:14:47 +08:00
|
|
|
#include "qemu/error-report.h"
|
2014-03-11 07:42:29 +08:00
|
|
|
#include "trace.h"
|
2017-04-20 19:10:28 +08:00
|
|
|
#include "qjson.h"
|
2013-11-28 22:01:15 +08:00
|
|
|
|
2017-09-25 19:29:15 +08:00
|
|
|
static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque, QJSON *vmdesc);
|
2013-11-28 22:01:15 +08:00
|
|
|
static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque);
|
|
|
|
|
2018-11-14 21:29:30 +08:00
|
|
|
static int vmstate_n_elems(void *opaque, const VMStateField *field)
|
2014-04-04 00:50:26 +08:00
|
|
|
{
|
|
|
|
int n_elems = 1;
|
|
|
|
|
|
|
|
if (field->flags & VMS_ARRAY) {
|
|
|
|
n_elems = field->num;
|
|
|
|
} else if (field->flags & VMS_VARRAY_INT32) {
|
|
|
|
n_elems = *(int32_t *)(opaque+field->num_offset);
|
|
|
|
} else if (field->flags & VMS_VARRAY_UINT32) {
|
|
|
|
n_elems = *(uint32_t *)(opaque+field->num_offset);
|
|
|
|
} else if (field->flags & VMS_VARRAY_UINT16) {
|
|
|
|
n_elems = *(uint16_t *)(opaque+field->num_offset);
|
|
|
|
} else if (field->flags & VMS_VARRAY_UINT8) {
|
|
|
|
n_elems = *(uint8_t *)(opaque+field->num_offset);
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:40:22 +08:00
|
|
|
if (field->flags & VMS_MULTIPLY_ELEMENTS) {
|
|
|
|
n_elems *= field->num;
|
|
|
|
}
|
|
|
|
|
2016-06-14 17:36:26 +08:00
|
|
|
trace_vmstate_n_elems(field->name, n_elems);
|
2014-04-04 00:50:26 +08:00
|
|
|
return n_elems;
|
|
|
|
}
|
|
|
|
|
2018-11-14 21:29:30 +08:00
|
|
|
static int vmstate_size(void *opaque, const VMStateField *field)
|
2014-04-04 00:50:26 +08:00
|
|
|
{
|
|
|
|
int size = field->size;
|
|
|
|
|
|
|
|
if (field->flags & VMS_VBUFFER) {
|
|
|
|
size = *(int32_t *)(opaque+field->size_offset);
|
|
|
|
if (field->flags & VMS_MULTIPLY) {
|
|
|
|
size *= field->size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-11-14 21:29:30 +08:00
|
|
|
static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
|
|
|
|
void *opaque)
|
2014-04-04 00:50:26 +08:00
|
|
|
{
|
2017-02-23 00:01:16 +08: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 17:34:19 +08:00
|
|
|
}
|
2014-04-04 00:50:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-28 22:01:15 +08:00
|
|
|
int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque, int version_id)
|
|
|
|
{
|
2018-11-14 21:29:30 +08:00
|
|
|
const VMStateField *field = vmsd->fields;
|
2015-01-21 18:14:48 +08:00
|
|
|
int ret = 0;
|
2013-11-28 22:01:15 +08:00
|
|
|
|
2015-01-21 18:14:48 +08:00
|
|
|
trace_vmstate_load_state(vmsd->name, version_id);
|
2013-11-28 22:01:15 +08:00
|
|
|
if (version_id > vmsd->version_id) {
|
2017-01-20 03:00:53 +08: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 18:14:48 +08:00
|
|
|
trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
|
2013-11-28 22:01:15 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (version_id < vmsd->minimum_version_id) {
|
2014-04-04 00:52:28 +08:00
|
|
|
if (vmsd->load_state_old &&
|
|
|
|
version_id >= vmsd->minimum_version_id_old) {
|
2015-01-21 18:14:48 +08:00
|
|
|
ret = vmsd->load_state_old(f, opaque, version_id);
|
|
|
|
trace_vmstate_load_state_end(vmsd->name, "old path", ret);
|
|
|
|
return ret;
|
2014-04-04 00:52:28 +08:00
|
|
|
}
|
2017-01-20 03:00:53 +08: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 18:14:48 +08:00
|
|
|
trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
|
2014-04-04 00:52:28 +08:00
|
|
|
return -EINVAL;
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
|
|
|
if (vmsd->pre_load) {
|
|
|
|
int ret = vmsd->pre_load(opaque);
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (field->name) {
|
2015-01-21 18:14:48 +08:00
|
|
|
trace_vmstate_load_state_field(vmsd->name, field->name);
|
2013-11-28 22:01:15 +08:00
|
|
|
if ((field->field_exists &&
|
|
|
|
field->field_exists(opaque, version_id)) ||
|
|
|
|
(!field->field_exists &&
|
|
|
|
field->version_id <= version_id)) {
|
2017-02-23 00:01:16 +08:00
|
|
|
void *first_elem = opaque + field->offset;
|
2014-04-04 00:50:26 +08:00
|
|
|
int i, n_elems = vmstate_n_elems(opaque, field);
|
|
|
|
int size = vmstate_size(opaque, field);
|
|
|
|
|
2017-02-23 00:01:16 +08:00
|
|
|
vmstate_handle_alloc(first_elem, field, opaque);
|
|
|
|
if (field->flags & VMS_POINTER) {
|
|
|
|
first_elem = *(void **)first_elem;
|
2017-03-10 12:44:02 +08:00
|
|
|
assert(first_elem || !n_elems || !size);
|
2017-02-23 00:01:16 +08:00
|
|
|
}
|
2013-11-28 22:01:15 +08:00
|
|
|
for (i = 0; i < n_elems; i++) {
|
2017-02-23 00:01:15 +08:00
|
|
|
void *curr_elem = first_elem + size * i;
|
2013-11-28 22:01:15 +08:00
|
|
|
|
|
|
|
if (field->flags & VMS_ARRAY_OF_POINTER) {
|
2017-02-23 00:01:15 +08:00
|
|
|
curr_elem = *(void **)curr_elem;
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
2017-03-10 12:44:02 +08:00
|
|
|
if (!curr_elem && size) {
|
2017-02-23 00:01:17 +08: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-23 00:01:15 +08:00
|
|
|
ret = vmstate_load_state(f, field->vmsd, curr_elem,
|
2013-11-28 22:01:15 +08:00
|
|
|
field->vmsd->version_id);
|
2018-04-25 23:27:30 +08:00
|
|
|
} else if (field->flags & VMS_VSTRUCT) {
|
|
|
|
ret = vmstate_load_state(f, field->vmsd, curr_elem,
|
|
|
|
field->struct_version_id);
|
2013-11-28 22:01:15 +08:00
|
|
|
} else {
|
2017-02-23 00:01:15 +08:00
|
|
|
ret = field->info->get(f, curr_elem, size, field);
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
2014-04-06 02:16:22 +08:00
|
|
|
if (ret >= 0) {
|
|
|
|
ret = qemu_file_get_error(f);
|
|
|
|
}
|
2013-11-28 22:01:15 +08:00
|
|
|
if (ret < 0) {
|
2014-04-06 02:16:22 +08:00
|
|
|
qemu_file_set_error(f, ret);
|
2016-09-28 02:56:04 +08:00
|
|
|
error_report("Failed to load %s:%s", vmsd->name,
|
|
|
|
field->name);
|
2014-03-11 07:42:29 +08:00
|
|
|
trace_vmstate_load_field_error(field->name, ret);
|
2013-11-28 22:01:15 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2014-04-04 00:50:31 +08:00
|
|
|
} else if (field->flags & VMS_MUST_EXIST) {
|
2015-01-21 18:14:47 +08:00
|
|
|
error_report("Input validation failed: %s/%s",
|
|
|
|
vmsd->name, field->name);
|
2014-04-04 00:50:31 +08:00
|
|
|
return -1;
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
|
|
|
field++;
|
|
|
|
}
|
|
|
|
ret = vmstate_subsection_load(f, vmsd, opaque);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (vmsd->post_load) {
|
2015-01-21 18:14:48 +08:00
|
|
|
ret = vmsd->post_load(opaque, version_id);
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
2015-01-21 18:14:48 +08:00
|
|
|
trace_vmstate_load_state_end(vmsd->name, "end", ret);
|
|
|
|
return ret;
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
|
|
|
|
2018-11-14 21:29:30 +08:00
|
|
|
static int vmfield_name_num(const VMStateField *start,
|
|
|
|
const VMStateField *search)
|
2015-01-22 22:01:39 +08:00
|
|
|
{
|
2018-11-14 21:29:30 +08:00
|
|
|
const VMStateField *field;
|
2015-01-22 22:01:39 +08: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 21:29:30 +08:00
|
|
|
static bool vmfield_name_is_unique(const VMStateField *start,
|
|
|
|
const VMStateField *search)
|
2015-01-22 22:01:39 +08:00
|
|
|
{
|
2018-11-14 21:29:30 +08:00
|
|
|
const VMStateField *field;
|
2015-01-22 22:01:39 +08: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 21:29:30 +08:00
|
|
|
static const char *vmfield_get_type_name(const VMStateField *field)
|
2015-01-22 22:01:39 +08:00
|
|
|
{
|
|
|
|
const char *type = "unknown";
|
|
|
|
|
|
|
|
if (field->flags & VMS_STRUCT) {
|
|
|
|
type = "struct";
|
2018-04-25 23:27:30 +08:00
|
|
|
} else if (field->flags & VMS_VSTRUCT) {
|
|
|
|
type = "vstruct";
|
2015-01-22 22:01:39 +08:00
|
|
|
} else if (field->info->name) {
|
|
|
|
type = field->info->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2018-11-14 21:29:30 +08:00
|
|
|
static bool vmsd_can_compress(const VMStateField *field)
|
2015-01-22 22:01:39 +08:00
|
|
|
{
|
|
|
|
if (field->field_exists) {
|
|
|
|
/* Dynamically existing fields mess up compression */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field->flags & VMS_STRUCT) {
|
2018-11-14 21:29:30 +08:00
|
|
|
const VMStateField *sfield = field->vmsd->fields;
|
2015-01-22 22:01:39 +08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc,
|
2018-11-14 21:29:30 +08:00
|
|
|
const VMStateField *field, int i, int max)
|
2015-01-22 22:01:39 +08: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_start_object(vmdesc, NULL);
|
|
|
|
json_prop_str(vmdesc, "name", name);
|
|
|
|
if (is_array) {
|
|
|
|
if (can_compress) {
|
|
|
|
json_prop_int(vmdesc, "array_len", max);
|
|
|
|
} else {
|
|
|
|
json_prop_int(vmdesc, "index", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
json_prop_str(vmdesc, "type", vmfield_get_type_name(field));
|
|
|
|
|
|
|
|
if (field->flags & VMS_STRUCT) {
|
|
|
|
json_start_object(vmdesc, "struct");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc,
|
2018-11-14 21:29:30 +08:00
|
|
|
const VMStateField *field, size_t size, int i)
|
2015-01-22 22:01:39 +08:00
|
|
|
{
|
|
|
|
if (!vmdesc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field->flags & VMS_STRUCT) {
|
|
|
|
/* We printed a struct in between, close its child object */
|
|
|
|
json_end_object(vmdesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_prop_int(vmdesc, "size", size);
|
|
|
|
json_end_object(vmdesc);
|
|
|
|
}
|
|
|
|
|
2014-10-15 15:39:14 +08:00
|
|
|
|
|
|
|
bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque)
|
|
|
|
{
|
|
|
|
if (vmsd->needed && !vmsd->needed(opaque)) {
|
|
|
|
/* optional section not needed */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-25 19:29:13 +08:00
|
|
|
int vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
|
2018-04-25 23:27:30 +08:00
|
|
|
void *opaque, QJSON *vmdesc_id)
|
|
|
|
{
|
|
|
|
return vmstate_save_state_v(f, vmsd, opaque, vmdesc_id, vmsd->version_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque, QJSON *vmdesc, int version_id)
|
2013-11-28 22:01:15 +08:00
|
|
|
{
|
2017-09-25 19:29:13 +08:00
|
|
|
int ret = 0;
|
2018-11-14 21:29:30 +08:00
|
|
|
const VMStateField *field = vmsd->fields;
|
2013-11-28 22:01:15 +08:00
|
|
|
|
2016-12-12 20:58:38 +08:00
|
|
|
trace_vmstate_save_state_top(vmsd->name);
|
|
|
|
|
2013-11-28 22:01:15 +08:00
|
|
|
if (vmsd->pre_save) {
|
2017-09-25 19:29:13 +08:00
|
|
|
ret = vmsd->pre_save(opaque);
|
|
|
|
trace_vmstate_save_state_pre_save_res(vmsd->name, ret);
|
|
|
|
if (ret) {
|
|
|
|
error_report("pre-save failed: %s", vmsd->name);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
2015-01-22 22:01:39 +08:00
|
|
|
|
|
|
|
if (vmdesc) {
|
|
|
|
json_prop_str(vmdesc, "vmsd_name", vmsd->name);
|
2018-04-25 23:27:30 +08:00
|
|
|
json_prop_int(vmdesc, "version", version_id);
|
2015-01-22 22:01:39 +08:00
|
|
|
json_start_array(vmdesc, "fields");
|
|
|
|
}
|
|
|
|
|
2013-11-28 22:01:15 +08:00
|
|
|
while (field->name) {
|
2018-04-25 23:27:30 +08:00
|
|
|
if ((field->field_exists &&
|
|
|
|
field->field_exists(opaque, version_id)) ||
|
|
|
|
(!field->field_exists &&
|
|
|
|
field->version_id <= version_id)) {
|
2017-02-23 00:01:16 +08:00
|
|
|
void *first_elem = opaque + field->offset;
|
2014-04-04 00:50:26 +08:00
|
|
|
int i, n_elems = vmstate_n_elems(opaque, field);
|
|
|
|
int size = vmstate_size(opaque, field);
|
2015-01-22 22:01:39 +08:00
|
|
|
int64_t old_offset, written_bytes;
|
|
|
|
QJSON *vmdesc_loop = vmdesc;
|
2014-04-04 00:50:26 +08:00
|
|
|
|
2016-12-12 20:58:38 +08:00
|
|
|
trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
|
2017-02-23 00:01:16 +08:00
|
|
|
if (field->flags & VMS_POINTER) {
|
|
|
|
first_elem = *(void **)first_elem;
|
2017-03-10 12:44:02 +08:00
|
|
|
assert(first_elem || !n_elems || !size);
|
2017-02-23 00:01:16 +08:00
|
|
|
}
|
2013-11-28 22:01:15 +08:00
|
|
|
for (i = 0; i < n_elems; i++) {
|
2017-02-23 00:01:15 +08:00
|
|
|
void *curr_elem = first_elem + size * i;
|
2017-09-25 19:29:14 +08:00
|
|
|
ret = 0;
|
2013-11-28 22:01:15 +08:00
|
|
|
|
2015-01-22 22:01:39 +08:00
|
|
|
vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
|
|
|
|
old_offset = qemu_ftell_fast(f);
|
2013-11-28 22:01:15 +08:00
|
|
|
if (field->flags & VMS_ARRAY_OF_POINTER) {
|
2017-02-23 00:01:15 +08:00
|
|
|
assert(curr_elem);
|
|
|
|
curr_elem = *(void **)curr_elem;
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
2017-03-10 12:44:02 +08:00
|
|
|
if (!curr_elem && size) {
|
2017-02-23 00:01:17 +08:00
|
|
|
/* if null pointer write placeholder and do not follow */
|
|
|
|
assert(field->flags & VMS_ARRAY_OF_POINTER);
|
2017-09-25 19:29:14 +08:00
|
|
|
ret = vmstate_info_nullptr.put(f, curr_elem, size, NULL,
|
|
|
|
NULL);
|
2017-02-23 00:01:17 +08:00
|
|
|
} else if (field->flags & VMS_STRUCT) {
|
2017-09-25 19:29:14 +08:00
|
|
|
ret = vmstate_save_state(f, field->vmsd, curr_elem,
|
|
|
|
vmdesc_loop);
|
2018-04-25 23:27:30 +08:00
|
|
|
} else if (field->flags & VMS_VSTRUCT) {
|
|
|
|
ret = vmstate_save_state_v(f, field->vmsd, curr_elem,
|
|
|
|
vmdesc_loop,
|
|
|
|
field->struct_version_id);
|
2013-11-28 22:01:15 +08:00
|
|
|
} else {
|
2017-09-25 19:29:14 +08:00
|
|
|
ret = field->info->put(f, curr_elem, size, field,
|
|
|
|
vmdesc_loop);
|
|
|
|
}
|
|
|
|
if (ret) {
|
|
|
|
error_report("Save of field %s/%s failed",
|
|
|
|
vmsd->name, field->name);
|
2019-01-21 18:23:13 +08:00
|
|
|
if (vmsd->post_save) {
|
|
|
|
vmsd->post_save(opaque);
|
|
|
|
}
|
2017-09-25 19:29:14 +08:00
|
|
|
return ret;
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
2015-01-22 22:01:39 +08:00
|
|
|
|
|
|
|
written_bytes = qemu_ftell_fast(f) - old_offset;
|
|
|
|
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 22:01:15 +08:00
|
|
|
}
|
2014-04-04 00:50:31 +08:00
|
|
|
} else {
|
|
|
|
if (field->flags & VMS_MUST_EXIST) {
|
2015-01-21 18:14:47 +08:00
|
|
|
error_report("Output state validation failed: %s/%s",
|
2014-04-04 00:50:31 +08:00
|
|
|
vmsd->name, field->name);
|
|
|
|
assert(!(field->flags & VMS_MUST_EXIST));
|
|
|
|
}
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
|
|
|
field++;
|
|
|
|
}
|
2015-01-22 22:01:39 +08:00
|
|
|
|
|
|
|
if (vmdesc) {
|
|
|
|
json_end_array(vmdesc);
|
|
|
|
}
|
|
|
|
|
2019-01-21 18:23:13 +08: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 22:01:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription *
|
2014-09-23 20:09:54 +08:00
|
|
|
vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
|
2013-11-28 22:01:15 +08:00
|
|
|
{
|
2018-08-07 21:03:55 +08:00
|
|
|
while (sub && *sub) {
|
2014-09-23 20:09:54 +08:00
|
|
|
if (strcmp(idstr, (*sub)->name) == 0) {
|
|
|
|
return *sub;
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
|
|
|
sub++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque)
|
|
|
|
{
|
2015-01-21 18:14:48 +08:00
|
|
|
trace_vmstate_subsection_load(vmsd->name);
|
|
|
|
|
2013-11-28 22:01:15 +08:00
|
|
|
while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
|
2015-05-21 20:24:15 +08:00
|
|
|
char idstr[256], *idstr_ret;
|
2013-11-28 22:01:15 +08: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 17:36:26 +08:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
|
2013-11-28 22:01:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-21 20:24:15 +08:00
|
|
|
size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
|
2013-11-28 22:01:15 +08:00
|
|
|
if (size != len) {
|
2016-06-14 17:36:26 +08:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
|
2013-11-28 22:01:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-21 20:24:15 +08:00
|
|
|
memcpy(idstr, idstr_ret, size);
|
2013-11-28 22:01:15 +08:00
|
|
|
idstr[size] = 0;
|
|
|
|
|
|
|
|
if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
|
2016-06-14 17:36:26 +08:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
|
|
|
|
/* it doesn't have a valid subsection name */
|
2013-11-28 22:01:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
|
|
|
|
if (sub_vmsd == NULL) {
|
2016-06-14 17:36:26 +08:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
|
2013-11-28 22:01:15 +08: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 17:36:26 +08:00
|
|
|
trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
|
2013-11-28 22:01:15 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2015-01-21 18:14:48 +08:00
|
|
|
|
|
|
|
trace_vmstate_subsection_load_good(vmsd->name);
|
2013-11-28 22:01:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-25 19:29:15 +08:00
|
|
|
static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
|
|
|
|
void *opaque, QJSON *vmdesc)
|
2013-11-28 22:01:15 +08:00
|
|
|
{
|
2014-09-23 20:09:54 +08:00
|
|
|
const VMStateDescription **sub = vmsd->subsections;
|
2019-04-03 09:10:16 +08:00
|
|
|
bool vmdesc_has_subsections = false;
|
2017-09-25 19:29:15 +08:00
|
|
|
int ret = 0;
|
2013-11-28 22:01:15 +08:00
|
|
|
|
2016-12-12 20:58:38 +08:00
|
|
|
trace_vmstate_subsection_save_top(vmsd->name);
|
2018-08-07 21:03:55 +08:00
|
|
|
while (sub && *sub) {
|
|
|
|
if (vmstate_save_needed(*sub, opaque)) {
|
2016-12-12 20:58:38 +08:00
|
|
|
const VMStateDescription *vmsdsub = *sub;
|
2013-11-28 22:01:15 +08:00
|
|
|
uint8_t len;
|
|
|
|
|
2016-12-12 20:58:38 +08:00
|
|
|
trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
|
2015-01-22 22:01:39 +08:00
|
|
|
if (vmdesc) {
|
|
|
|
/* Only create subsection array when we have any */
|
2019-04-03 09:10:16 +08:00
|
|
|
if (!vmdesc_has_subsections) {
|
2015-01-22 22:01:39 +08:00
|
|
|
json_start_array(vmdesc, "subsections");
|
2019-04-03 09:10:16 +08:00
|
|
|
vmdesc_has_subsections = true;
|
2015-01-22 22:01:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
json_start_object(vmdesc, NULL);
|
|
|
|
}
|
|
|
|
|
2013-11-28 22:01:15 +08:00
|
|
|
qemu_put_byte(f, QEMU_VM_SUBSECTION);
|
2016-12-12 20:58:38 +08:00
|
|
|
len = strlen(vmsdsub->name);
|
2013-11-28 22:01:15 +08:00
|
|
|
qemu_put_byte(f, len);
|
2016-12-12 20:58:38 +08:00
|
|
|
qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
|
|
|
|
qemu_put_be32(f, vmsdsub->version_id);
|
2017-09-25 19:29:15 +08:00
|
|
|
ret = vmstate_save_state(f, vmsdsub, opaque, vmdesc);
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-22 22:01:39 +08:00
|
|
|
|
|
|
|
if (vmdesc) {
|
|
|
|
json_end_object(vmdesc);
|
|
|
|
}
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|
|
|
|
sub++;
|
|
|
|
}
|
2015-01-22 22:01:39 +08:00
|
|
|
|
2019-04-03 09:10:16 +08:00
|
|
|
if (vmdesc_has_subsections) {
|
2015-01-22 22:01:39 +08:00
|
|
|
json_end_array(vmdesc);
|
|
|
|
}
|
2017-09-25 19:29:15 +08:00
|
|
|
|
|
|
|
return ret;
|
2013-11-28 22:01:15 +08:00
|
|
|
}
|