1 #include "qemu-common.h"
2 #include "migration/migration.h"
3 #include "migration/qemu-file.h"
4 #include "migration/vmstate.h"
5 #include "qemu/bitops.h"
6 #include "qemu/error-report.h"
10 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
11 void *opaque, QJSON *vmdesc);
12 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
15 static int vmstate_n_elems(void *opaque, VMStateField *field)
19 if (field->flags & VMS_ARRAY) {
21 } else if (field->flags & VMS_VARRAY_INT32) {
22 n_elems = *(int32_t *)(opaque+field->num_offset);
23 } else if (field->flags & VMS_VARRAY_UINT32) {
24 n_elems = *(uint32_t *)(opaque+field->num_offset);
25 } else if (field->flags & VMS_VARRAY_UINT16) {
26 n_elems = *(uint16_t *)(opaque+field->num_offset);
27 } else if (field->flags & VMS_VARRAY_UINT8) {
28 n_elems = *(uint8_t *)(opaque+field->num_offset);
34 static int vmstate_size(void *opaque, VMStateField *field)
36 int size = field->size;
38 if (field->flags & VMS_VBUFFER) {
39 size = *(int32_t *)(opaque+field->size_offset);
40 if (field->flags & VMS_MULTIPLY) {
48 static void *vmstate_base_addr(void *opaque, VMStateField *field, bool alloc)
50 void *base_addr = opaque + field->offset;
52 if (field->flags & VMS_POINTER) {
53 if (alloc && (field->flags & VMS_ALLOC)) {
55 if (field->flags & VMS_VBUFFER) {
56 size = vmstate_size(opaque, field);
58 int n_elems = vmstate_n_elems(opaque, field);
60 size = n_elems * field->size;
64 *((void **)base_addr + field->start) = g_malloc(size);
67 base_addr = *(void **)base_addr + field->start;
73 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
74 void *opaque, int version_id)
76 VMStateField *field = vmsd->fields;
79 trace_vmstate_load_state(vmsd->name, version_id);
80 if (version_id > vmsd->version_id) {
81 trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
84 if (version_id < vmsd->minimum_version_id) {
85 if (vmsd->load_state_old &&
86 version_id >= vmsd->minimum_version_id_old) {
87 ret = vmsd->load_state_old(f, opaque, version_id);
88 trace_vmstate_load_state_end(vmsd->name, "old path", ret);
91 trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
95 int ret = vmsd->pre_load(opaque);
100 while (field->name) {
101 trace_vmstate_load_state_field(vmsd->name, field->name);
102 if ((field->field_exists &&
103 field->field_exists(opaque, version_id)) ||
104 (!field->field_exists &&
105 field->version_id <= version_id)) {
106 void *base_addr = vmstate_base_addr(opaque, field, true);
107 int i, n_elems = vmstate_n_elems(opaque, field);
108 int size = vmstate_size(opaque, field);
110 for (i = 0; i < n_elems; i++) {
111 void *addr = base_addr + size * i;
113 if (field->flags & VMS_ARRAY_OF_POINTER) {
114 addr = *(void **)addr;
116 if (field->flags & VMS_STRUCT) {
117 ret = vmstate_load_state(f, field->vmsd, addr,
118 field->vmsd->version_id);
120 ret = field->info->get(f, addr, size);
124 ret = qemu_file_get_error(f);
127 qemu_file_set_error(f, ret);
128 trace_vmstate_load_field_error(field->name, ret);
132 } else if (field->flags & VMS_MUST_EXIST) {
133 error_report("Input validation failed: %s/%s",
134 vmsd->name, field->name);
139 ret = vmstate_subsection_load(f, vmsd, opaque);
143 if (vmsd->post_load) {
144 ret = vmsd->post_load(opaque, version_id);
146 trace_vmstate_load_state_end(vmsd->name, "end", ret);
150 static int vmfield_name_num(VMStateField *start, VMStateField *search)
155 for (field = start; field->name; field++) {
156 if (!strcmp(field->name, search->name)) {
157 if (field == search) {
167 static bool vmfield_name_is_unique(VMStateField *start, VMStateField *search)
172 for (field = start; field->name; field++) {
173 if (!strcmp(field->name, search->name)) {
175 /* name found more than once, so it's not unique */
185 static const char *vmfield_get_type_name(VMStateField *field)
187 const char *type = "unknown";
189 if (field->flags & VMS_STRUCT) {
191 } else if (field->info->name) {
192 type = field->info->name;
198 static bool vmsd_can_compress(VMStateField *field)
200 if (field->field_exists) {
201 /* Dynamically existing fields mess up compression */
205 if (field->flags & VMS_STRUCT) {
206 VMStateField *sfield = field->vmsd->fields;
207 while (sfield->name) {
208 if (!vmsd_can_compress(sfield)) {
209 /* Child elements can't compress, so can't we */
215 if (field->vmsd->subsections) {
216 /* Subsections may come and go, better don't compress */
224 static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc,
225 VMStateField *field, int i, int max)
227 char *name, *old_name;
228 bool is_array = max > 1;
229 bool can_compress = vmsd_can_compress(field);
235 name = g_strdup(field->name);
237 /* Field name is not unique, need to make it unique */
238 if (!vmfield_name_is_unique(vmsd->fields, field)) {
239 int num = vmfield_name_num(vmsd->fields, field);
241 name = g_strdup_printf("%s[%d]", name, num);
245 json_start_object(vmdesc, NULL);
246 json_prop_str(vmdesc, "name", name);
249 json_prop_int(vmdesc, "array_len", max);
251 json_prop_int(vmdesc, "index", i);
254 json_prop_str(vmdesc, "type", vmfield_get_type_name(field));
256 if (field->flags & VMS_STRUCT) {
257 json_start_object(vmdesc, "struct");
263 static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc,
264 VMStateField *field, size_t size, int i)
270 if (field->flags & VMS_STRUCT) {
271 /* We printed a struct in between, close its child object */
272 json_end_object(vmdesc);
275 json_prop_int(vmdesc, "size", size);
276 json_end_object(vmdesc);
280 bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque)
282 if (vmsd->needed && !vmsd->needed(opaque)) {
283 /* optional section not needed */
290 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
291 void *opaque, QJSON *vmdesc)
293 VMStateField *field = vmsd->fields;
295 if (vmsd->pre_save) {
296 vmsd->pre_save(opaque);
300 json_prop_str(vmdesc, "vmsd_name", vmsd->name);
301 json_prop_int(vmdesc, "version", vmsd->version_id);
302 json_start_array(vmdesc, "fields");
305 while (field->name) {
306 if (!field->field_exists ||
307 field->field_exists(opaque, vmsd->version_id)) {
308 void *base_addr = vmstate_base_addr(opaque, field, false);
309 int i, n_elems = vmstate_n_elems(opaque, field);
310 int size = vmstate_size(opaque, field);
311 int64_t old_offset, written_bytes;
312 QJSON *vmdesc_loop = vmdesc;
314 for (i = 0; i < n_elems; i++) {
315 void *addr = base_addr + size * i;
317 vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
318 old_offset = qemu_ftell_fast(f);
320 if (field->flags & VMS_ARRAY_OF_POINTER) {
321 addr = *(void **)addr;
323 if (field->flags & VMS_STRUCT) {
324 vmstate_save_state(f, field->vmsd, addr, vmdesc_loop);
326 field->info->put(f, addr, size);
329 written_bytes = qemu_ftell_fast(f) - old_offset;
330 vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
332 /* Compressed arrays only care about the first element */
333 if (vmdesc_loop && vmsd_can_compress(field)) {
338 if (field->flags & VMS_MUST_EXIST) {
339 error_report("Output state validation failed: %s/%s",
340 vmsd->name, field->name);
341 assert(!(field->flags & VMS_MUST_EXIST));
348 json_end_array(vmdesc);
351 vmstate_subsection_save(f, vmsd, opaque, vmdesc);
354 static const VMStateDescription *
355 vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
357 while (sub && *sub && (*sub)->needed) {
358 if (strcmp(idstr, (*sub)->name) == 0) {
366 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
369 trace_vmstate_subsection_load(vmsd->name);
371 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
372 char idstr[256], *idstr_ret;
374 uint8_t version_id, len, size;
375 const VMStateDescription *sub_vmsd;
377 len = qemu_peek_byte(f, 1);
378 if (len < strlen(vmsd->name) + 1) {
379 /* subsection name has be be "section_name/a" */
380 trace_vmstate_subsection_load_bad(vmsd->name, "(short)");
383 size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
385 trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)");
388 memcpy(idstr, idstr_ret, size);
391 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
392 trace_vmstate_subsection_load_bad(vmsd->name, idstr);
393 /* it don't have a valid subsection name */
396 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
397 if (sub_vmsd == NULL) {
398 trace_vmstate_subsection_load_bad(vmsd->name, "(lookup)");
401 qemu_file_skip(f, 1); /* subsection */
402 qemu_file_skip(f, 1); /* len */
403 qemu_file_skip(f, len); /* idstr */
404 version_id = qemu_get_be32(f);
406 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
408 trace_vmstate_subsection_load_bad(vmsd->name, "(child)");
413 trace_vmstate_subsection_load_good(vmsd->name);
417 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
418 void *opaque, QJSON *vmdesc)
420 const VMStateDescription **sub = vmsd->subsections;
421 bool subsection_found = false;
423 while (sub && *sub && (*sub)->needed) {
424 if ((*sub)->needed(opaque)) {
425 const VMStateDescription *vmsd = *sub;
429 /* Only create subsection array when we have any */
430 if (!subsection_found) {
431 json_start_array(vmdesc, "subsections");
432 subsection_found = true;
435 json_start_object(vmdesc, NULL);
438 qemu_put_byte(f, QEMU_VM_SUBSECTION);
439 len = strlen(vmsd->name);
440 qemu_put_byte(f, len);
441 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
442 qemu_put_be32(f, vmsd->version_id);
443 vmstate_save_state(f, vmsd, opaque, vmdesc);
446 json_end_object(vmdesc);
452 if (vmdesc && subsection_found) {
453 json_end_array(vmdesc);
459 static int get_bool(QEMUFile *f, void *pv, size_t size)
462 *v = qemu_get_byte(f);
466 static void put_bool(QEMUFile *f, void *pv, size_t size)
469 qemu_put_byte(f, *v);
472 const VMStateInfo vmstate_info_bool = {
480 static int get_int8(QEMUFile *f, void *pv, size_t size)
487 static void put_int8(QEMUFile *f, void *pv, size_t size)
493 const VMStateInfo vmstate_info_int8 = {
501 static int get_int16(QEMUFile *f, void *pv, size_t size)
504 qemu_get_sbe16s(f, v);
508 static void put_int16(QEMUFile *f, void *pv, size_t size)
511 qemu_put_sbe16s(f, v);
514 const VMStateInfo vmstate_info_int16 = {
522 static int get_int32(QEMUFile *f, void *pv, size_t size)
525 qemu_get_sbe32s(f, v);
529 static void put_int32(QEMUFile *f, void *pv, size_t size)
532 qemu_put_sbe32s(f, v);
535 const VMStateInfo vmstate_info_int32 = {
541 /* 32 bit int. See that the received value is the same than the one
544 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
548 qemu_get_sbe32s(f, &v2);
556 const VMStateInfo vmstate_info_int32_equal = {
557 .name = "int32 equal",
558 .get = get_int32_equal,
562 /* 32 bit int. Check that the received value is non-negative
563 * and less than or equal to the one in the field.
566 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
570 qemu_get_sbe32s(f, &loaded);
572 if (loaded >= 0 && loaded <= *cur) {
579 const VMStateInfo vmstate_info_int32_le = {
587 static int get_int64(QEMUFile *f, void *pv, size_t size)
590 qemu_get_sbe64s(f, v);
594 static void put_int64(QEMUFile *f, void *pv, size_t size)
597 qemu_put_sbe64s(f, v);
600 const VMStateInfo vmstate_info_int64 = {
606 /* 8 bit unsigned int */
608 static int get_uint8(QEMUFile *f, void *pv, size_t size)
615 static void put_uint8(QEMUFile *f, void *pv, size_t size)
621 const VMStateInfo vmstate_info_uint8 = {
627 /* 16 bit unsigned int */
629 static int get_uint16(QEMUFile *f, void *pv, size_t size)
632 qemu_get_be16s(f, v);
636 static void put_uint16(QEMUFile *f, void *pv, size_t size)
639 qemu_put_be16s(f, v);
642 const VMStateInfo vmstate_info_uint16 = {
648 /* 32 bit unsigned int */
650 static int get_uint32(QEMUFile *f, void *pv, size_t size)
653 qemu_get_be32s(f, v);
657 static void put_uint32(QEMUFile *f, void *pv, size_t size)
660 qemu_put_be32s(f, v);
663 const VMStateInfo vmstate_info_uint32 = {
669 /* 32 bit uint. See that the received value is the same than the one
672 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
676 qemu_get_be32s(f, &v2);
684 const VMStateInfo vmstate_info_uint32_equal = {
685 .name = "uint32 equal",
686 .get = get_uint32_equal,
690 /* 64 bit unsigned int */
692 static int get_uint64(QEMUFile *f, void *pv, size_t size)
695 qemu_get_be64s(f, v);
699 static void put_uint64(QEMUFile *f, void *pv, size_t size)
702 qemu_put_be64s(f, v);
705 const VMStateInfo vmstate_info_uint64 = {
711 /* 64 bit unsigned int. See that the received value is the same than the one
714 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
718 qemu_get_be64s(f, &v2);
726 const VMStateInfo vmstate_info_uint64_equal = {
727 .name = "int64 equal",
728 .get = get_uint64_equal,
732 /* 8 bit int. See that the received value is the same than the one
735 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
747 const VMStateInfo vmstate_info_uint8_equal = {
748 .name = "uint8 equal",
749 .get = get_uint8_equal,
753 /* 16 bit unsigned int int. See that the received value is the same than the one
756 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
760 qemu_get_be16s(f, &v2);
768 const VMStateInfo vmstate_info_uint16_equal = {
769 .name = "uint16 equal",
770 .get = get_uint16_equal,
776 static int get_float64(QEMUFile *f, void *pv, size_t size)
780 *v = make_float64(qemu_get_be64(f));
784 static void put_float64(QEMUFile *f, void *pv, size_t size)
788 qemu_put_be64(f, float64_val(*v));
791 const VMStateInfo vmstate_info_float64 = {
797 /* uint8_t buffers */
799 static int get_buffer(QEMUFile *f, void *pv, size_t size)
802 qemu_get_buffer(f, v, size);
806 static void put_buffer(QEMUFile *f, void *pv, size_t size)
809 qemu_put_buffer(f, v, size);
812 const VMStateInfo vmstate_info_buffer = {
818 /* unused buffers: space that was used for some fields that are
819 not useful anymore */
821 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
827 block_len = MIN(sizeof(buf), size);
829 qemu_get_buffer(f, buf, block_len);
834 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
836 static const uint8_t buf[1024];
840 block_len = MIN(sizeof(buf), size);
842 qemu_put_buffer(f, buf, block_len);
846 const VMStateInfo vmstate_info_unused_buffer = {
847 .name = "unused_buffer",
848 .get = get_unused_buffer,
849 .put = put_unused_buffer,
852 /* bitmaps (as defined by bitmap.h). Note that size here is the size
853 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
854 * bit words with the bits in big endian order. The in-memory format
855 * is an array of 'unsigned long', which may be either 32 or 64 bits.
857 /* This is the number of 64 bit words sent over the wire */
858 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
859 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
861 unsigned long *bmp = pv;
863 for (i = 0; i < BITS_TO_U64S(size); i++) {
864 uint64_t w = qemu_get_be64(f);
866 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
867 bmp[idx++] = w >> 32;
873 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
875 unsigned long *bmp = pv;
877 for (i = 0; i < BITS_TO_U64S(size); i++) {
878 uint64_t w = bmp[idx++];
879 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
880 w |= ((uint64_t)bmp[idx++]) << 32;
886 const VMStateInfo vmstate_info_bitmap = {