1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
3 #include "migration/migration.h"
4 #include "migration/qemu-file.h"
5 #include "migration/vmstate.h"
6 #include "qemu/bitops.h"
7 #include "qemu/error-report.h"
8 #include "qemu/queue.h"
10 #include "migration/qjson.h"
12 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
13 void *opaque, QJSON *vmdesc);
14 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
17 static int vmstate_n_elems(void *opaque, VMStateField *field)
21 if (field->flags & VMS_ARRAY) {
23 } else if (field->flags & VMS_VARRAY_INT32) {
24 n_elems = *(int32_t *)(opaque+field->num_offset);
25 } else if (field->flags & VMS_VARRAY_UINT32) {
26 n_elems = *(uint32_t *)(opaque+field->num_offset);
27 } else if (field->flags & VMS_VARRAY_UINT16) {
28 n_elems = *(uint16_t *)(opaque+field->num_offset);
29 } else if (field->flags & VMS_VARRAY_UINT8) {
30 n_elems = *(uint8_t *)(opaque+field->num_offset);
33 if (field->flags & VMS_MULTIPLY_ELEMENTS) {
34 n_elems *= field->num;
37 trace_vmstate_n_elems(field->name, n_elems);
41 static int vmstate_size(void *opaque, VMStateField *field)
43 int size = field->size;
45 if (field->flags & VMS_VBUFFER) {
46 size = *(int32_t *)(opaque+field->size_offset);
47 if (field->flags & VMS_MULTIPLY) {
55 static void *vmstate_base_addr(void *opaque, VMStateField *field, bool alloc)
57 void *base_addr = opaque + field->offset;
59 if (field->flags & VMS_POINTER) {
60 if (alloc && (field->flags & VMS_ALLOC)) {
62 if (field->flags & VMS_VBUFFER) {
63 size = vmstate_size(opaque, field);
65 int n_elems = vmstate_n_elems(opaque, field);
67 size = n_elems * field->size;
71 *((void **)base_addr + field->start) = g_malloc(size);
74 base_addr = *(void **)base_addr + field->start;
80 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
81 void *opaque, int version_id)
83 VMStateField *field = vmsd->fields;
86 trace_vmstate_load_state(vmsd->name, version_id);
87 if (version_id > vmsd->version_id) {
88 error_report("%s: incoming version_id %d is too new "
89 "for local version_id %d",
90 vmsd->name, version_id, vmsd->version_id);
91 trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
94 if (version_id < vmsd->minimum_version_id) {
95 if (vmsd->load_state_old &&
96 version_id >= vmsd->minimum_version_id_old) {
97 ret = vmsd->load_state_old(f, opaque, version_id);
98 trace_vmstate_load_state_end(vmsd->name, "old path", ret);
101 error_report("%s: incoming version_id %d is too old "
102 "for local minimum version_id %d",
103 vmsd->name, version_id, vmsd->minimum_version_id);
104 trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
107 if (vmsd->pre_load) {
108 int ret = vmsd->pre_load(opaque);
113 while (field->name) {
114 trace_vmstate_load_state_field(vmsd->name, field->name);
115 if ((field->field_exists &&
116 field->field_exists(opaque, version_id)) ||
117 (!field->field_exists &&
118 field->version_id <= version_id)) {
119 void *base_addr = vmstate_base_addr(opaque, field, true);
120 int i, n_elems = vmstate_n_elems(opaque, field);
121 int size = vmstate_size(opaque, field);
123 for (i = 0; i < n_elems; i++) {
124 void *addr = base_addr + size * i;
126 if (field->flags & VMS_ARRAY_OF_POINTER) {
127 addr = *(void **)addr;
129 if (field->flags & VMS_STRUCT) {
130 ret = vmstate_load_state(f, field->vmsd, addr,
131 field->vmsd->version_id);
133 ret = field->info->get(f, addr, size, field);
136 ret = qemu_file_get_error(f);
139 qemu_file_set_error(f, ret);
140 error_report("Failed to load %s:%s", vmsd->name,
142 trace_vmstate_load_field_error(field->name, ret);
146 } else if (field->flags & VMS_MUST_EXIST) {
147 error_report("Input validation failed: %s/%s",
148 vmsd->name, field->name);
153 ret = vmstate_subsection_load(f, vmsd, opaque);
157 if (vmsd->post_load) {
158 ret = vmsd->post_load(opaque, version_id);
160 trace_vmstate_load_state_end(vmsd->name, "end", ret);
164 static int vmfield_name_num(VMStateField *start, VMStateField *search)
169 for (field = start; field->name; field++) {
170 if (!strcmp(field->name, search->name)) {
171 if (field == search) {
181 static bool vmfield_name_is_unique(VMStateField *start, VMStateField *search)
186 for (field = start; field->name; field++) {
187 if (!strcmp(field->name, search->name)) {
189 /* name found more than once, so it's not unique */
199 static const char *vmfield_get_type_name(VMStateField *field)
201 const char *type = "unknown";
203 if (field->flags & VMS_STRUCT) {
205 } else if (field->info->name) {
206 type = field->info->name;
212 static bool vmsd_can_compress(VMStateField *field)
214 if (field->field_exists) {
215 /* Dynamically existing fields mess up compression */
219 if (field->flags & VMS_STRUCT) {
220 VMStateField *sfield = field->vmsd->fields;
221 while (sfield->name) {
222 if (!vmsd_can_compress(sfield)) {
223 /* Child elements can't compress, so can't we */
229 if (field->vmsd->subsections) {
230 /* Subsections may come and go, better don't compress */
238 static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc,
239 VMStateField *field, int i, int max)
241 char *name, *old_name;
242 bool is_array = max > 1;
243 bool can_compress = vmsd_can_compress(field);
249 name = g_strdup(field->name);
251 /* Field name is not unique, need to make it unique */
252 if (!vmfield_name_is_unique(vmsd->fields, field)) {
253 int num = vmfield_name_num(vmsd->fields, field);
255 name = g_strdup_printf("%s[%d]", name, num);
259 json_start_object(vmdesc, NULL);
260 json_prop_str(vmdesc, "name", name);
263 json_prop_int(vmdesc, "array_len", max);
265 json_prop_int(vmdesc, "index", i);
268 json_prop_str(vmdesc, "type", vmfield_get_type_name(field));
270 if (field->flags & VMS_STRUCT) {
271 json_start_object(vmdesc, "struct");
277 static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc,
278 VMStateField *field, size_t size, int i)
284 if (field->flags & VMS_STRUCT) {
285 /* We printed a struct in between, close its child object */
286 json_end_object(vmdesc);
289 json_prop_int(vmdesc, "size", size);
290 json_end_object(vmdesc);
294 bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque)
296 if (vmsd->needed && !vmsd->needed(opaque)) {
297 /* optional section not needed */
304 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
305 void *opaque, QJSON *vmdesc)
307 VMStateField *field = vmsd->fields;
309 trace_vmstate_save_state_top(vmsd->name);
311 if (vmsd->pre_save) {
312 vmsd->pre_save(opaque);
316 json_prop_str(vmdesc, "vmsd_name", vmsd->name);
317 json_prop_int(vmdesc, "version", vmsd->version_id);
318 json_start_array(vmdesc, "fields");
321 while (field->name) {
322 if (!field->field_exists ||
323 field->field_exists(opaque, vmsd->version_id)) {
324 void *base_addr = vmstate_base_addr(opaque, field, false);
325 int i, n_elems = vmstate_n_elems(opaque, field);
326 int size = vmstate_size(opaque, field);
327 int64_t old_offset, written_bytes;
328 QJSON *vmdesc_loop = vmdesc;
330 trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
331 for (i = 0; i < n_elems; i++) {
332 void *addr = base_addr + size * i;
334 vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
335 old_offset = qemu_ftell_fast(f);
337 if (field->flags & VMS_ARRAY_OF_POINTER) {
338 addr = *(void **)addr;
340 if (field->flags & VMS_STRUCT) {
341 vmstate_save_state(f, field->vmsd, addr, vmdesc_loop);
343 field->info->put(f, addr, size, field, vmdesc_loop);
346 written_bytes = qemu_ftell_fast(f) - old_offset;
347 vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
349 /* Compressed arrays only care about the first element */
350 if (vmdesc_loop && vmsd_can_compress(field)) {
355 if (field->flags & VMS_MUST_EXIST) {
356 error_report("Output state validation failed: %s/%s",
357 vmsd->name, field->name);
358 assert(!(field->flags & VMS_MUST_EXIST));
365 json_end_array(vmdesc);
368 vmstate_subsection_save(f, vmsd, opaque, vmdesc);
371 static const VMStateDescription *
372 vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
374 while (sub && *sub && (*sub)->needed) {
375 if (strcmp(idstr, (*sub)->name) == 0) {
383 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
386 trace_vmstate_subsection_load(vmsd->name);
388 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
389 char idstr[256], *idstr_ret;
391 uint8_t version_id, len, size;
392 const VMStateDescription *sub_vmsd;
394 len = qemu_peek_byte(f, 1);
395 if (len < strlen(vmsd->name) + 1) {
396 /* subsection name has be be "section_name/a" */
397 trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
400 size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
402 trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
405 memcpy(idstr, idstr_ret, size);
408 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
409 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
410 /* it doesn't have a valid subsection name */
413 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
414 if (sub_vmsd == NULL) {
415 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
418 qemu_file_skip(f, 1); /* subsection */
419 qemu_file_skip(f, 1); /* len */
420 qemu_file_skip(f, len); /* idstr */
421 version_id = qemu_get_be32(f);
423 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
425 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
430 trace_vmstate_subsection_load_good(vmsd->name);
434 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
435 void *opaque, QJSON *vmdesc)
437 const VMStateDescription **sub = vmsd->subsections;
438 bool subsection_found = false;
440 trace_vmstate_subsection_save_top(vmsd->name);
441 while (sub && *sub && (*sub)->needed) {
442 if ((*sub)->needed(opaque)) {
443 const VMStateDescription *vmsdsub = *sub;
446 trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
448 /* Only create subsection array when we have any */
449 if (!subsection_found) {
450 json_start_array(vmdesc, "subsections");
451 subsection_found = true;
454 json_start_object(vmdesc, NULL);
457 qemu_put_byte(f, QEMU_VM_SUBSECTION);
458 len = strlen(vmsdsub->name);
459 qemu_put_byte(f, len);
460 qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
461 qemu_put_be32(f, vmsdsub->version_id);
462 vmstate_save_state(f, vmsdsub, opaque, vmdesc);
465 json_end_object(vmdesc);
471 if (vmdesc && subsection_found) {
472 json_end_array(vmdesc);
478 static int get_bool(QEMUFile *f, void *pv, size_t size, VMStateField *field)
481 *v = qemu_get_byte(f);
485 static int put_bool(QEMUFile *f, void *pv, size_t size, VMStateField *field,
489 qemu_put_byte(f, *v);
493 const VMStateInfo vmstate_info_bool = {
501 static int get_int8(QEMUFile *f, void *pv, size_t size, VMStateField *field)
508 static int put_int8(QEMUFile *f, void *pv, size_t size, VMStateField *field,
516 const VMStateInfo vmstate_info_int8 = {
524 static int get_int16(QEMUFile *f, void *pv, size_t size, VMStateField *field)
527 qemu_get_sbe16s(f, v);
531 static int put_int16(QEMUFile *f, void *pv, size_t size, VMStateField *field,
535 qemu_put_sbe16s(f, v);
539 const VMStateInfo vmstate_info_int16 = {
547 static int get_int32(QEMUFile *f, void *pv, size_t size, VMStateField *field)
550 qemu_get_sbe32s(f, v);
554 static int put_int32(QEMUFile *f, void *pv, size_t size, VMStateField *field,
558 qemu_put_sbe32s(f, v);
562 const VMStateInfo vmstate_info_int32 = {
568 /* 32 bit int. See that the received value is the same than the one
571 static int get_int32_equal(QEMUFile *f, void *pv, size_t size,
576 qemu_get_sbe32s(f, &v2);
581 error_report("%" PRIx32 " != %" PRIx32, *v, v2);
585 const VMStateInfo vmstate_info_int32_equal = {
586 .name = "int32 equal",
587 .get = get_int32_equal,
591 /* 32 bit int. Check that the received value is non-negative
592 * and less than or equal to the one in the field.
595 static int get_int32_le(QEMUFile *f, void *pv, size_t size, VMStateField *field)
599 qemu_get_sbe32s(f, &loaded);
601 if (loaded >= 0 && loaded <= *cur) {
605 error_report("Invalid value %" PRId32
606 " expecting positive value <= %" PRId32,
611 const VMStateInfo vmstate_info_int32_le = {
619 static int get_int64(QEMUFile *f, void *pv, size_t size, VMStateField *field)
622 qemu_get_sbe64s(f, v);
626 static int put_int64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
630 qemu_put_sbe64s(f, v);
634 const VMStateInfo vmstate_info_int64 = {
640 /* 8 bit unsigned int */
642 static int get_uint8(QEMUFile *f, void *pv, size_t size, VMStateField *field)
649 static int put_uint8(QEMUFile *f, void *pv, size_t size, VMStateField *field,
657 const VMStateInfo vmstate_info_uint8 = {
663 /* 16 bit unsigned int */
665 static int get_uint16(QEMUFile *f, void *pv, size_t size, VMStateField *field)
668 qemu_get_be16s(f, v);
672 static int put_uint16(QEMUFile *f, void *pv, size_t size, VMStateField *field,
676 qemu_put_be16s(f, v);
680 const VMStateInfo vmstate_info_uint16 = {
686 /* 32 bit unsigned int */
688 static int get_uint32(QEMUFile *f, void *pv, size_t size, VMStateField *field)
691 qemu_get_be32s(f, v);
695 static int put_uint32(QEMUFile *f, void *pv, size_t size, VMStateField *field,
699 qemu_put_be32s(f, v);
703 const VMStateInfo vmstate_info_uint32 = {
709 /* 32 bit uint. See that the received value is the same than the one
712 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size,
717 qemu_get_be32s(f, &v2);
722 error_report("%" PRIx32 " != %" PRIx32, *v, v2);
726 const VMStateInfo vmstate_info_uint32_equal = {
727 .name = "uint32 equal",
728 .get = get_uint32_equal,
732 /* 64 bit unsigned int */
734 static int get_uint64(QEMUFile *f, void *pv, size_t size, VMStateField *field)
737 qemu_get_be64s(f, v);
741 static int put_uint64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
745 qemu_put_be64s(f, v);
749 const VMStateInfo vmstate_info_uint64 = {
755 /* 64 bit unsigned int. See that the received value is the same than the one
758 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size,
763 qemu_get_be64s(f, &v2);
768 error_report("%" PRIx64 " != %" PRIx64, *v, v2);
772 const VMStateInfo vmstate_info_uint64_equal = {
773 .name = "int64 equal",
774 .get = get_uint64_equal,
778 /* 8 bit int. See that the received value is the same than the one
781 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size,
791 error_report("%x != %x", *v, v2);
795 const VMStateInfo vmstate_info_uint8_equal = {
796 .name = "uint8 equal",
797 .get = get_uint8_equal,
801 /* 16 bit unsigned int int. See that the received value is the same than the one
804 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size,
809 qemu_get_be16s(f, &v2);
814 error_report("%x != %x", *v, v2);
818 const VMStateInfo vmstate_info_uint16_equal = {
819 .name = "uint16 equal",
820 .get = get_uint16_equal,
826 static int get_float64(QEMUFile *f, void *pv, size_t size,
831 *v = make_float64(qemu_get_be64(f));
835 static int put_float64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
840 qemu_put_be64(f, float64_val(*v));
844 const VMStateInfo vmstate_info_float64 = {
850 /* CPU_DoubleU type */
852 static int get_cpudouble(QEMUFile *f, void *pv, size_t size,
856 qemu_get_be32s(f, &v->l.upper);
857 qemu_get_be32s(f, &v->l.lower);
861 static int put_cpudouble(QEMUFile *f, void *pv, size_t size,
862 VMStateField *field, QJSON *vmdesc)
865 qemu_put_be32s(f, &v->l.upper);
866 qemu_put_be32s(f, &v->l.lower);
870 const VMStateInfo vmstate_info_cpudouble = {
871 .name = "CPU_Double_U",
872 .get = get_cpudouble,
873 .put = put_cpudouble,
876 /* uint8_t buffers */
878 static int get_buffer(QEMUFile *f, void *pv, size_t size,
882 qemu_get_buffer(f, v, size);
886 static int put_buffer(QEMUFile *f, void *pv, size_t size, VMStateField *field,
890 qemu_put_buffer(f, v, size);
894 const VMStateInfo vmstate_info_buffer = {
900 /* unused buffers: space that was used for some fields that are
901 not useful anymore */
903 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size,
910 block_len = MIN(sizeof(buf), size);
912 qemu_get_buffer(f, buf, block_len);
917 static int put_unused_buffer(QEMUFile *f, void *pv, size_t size,
918 VMStateField *field, QJSON *vmdesc)
920 static const uint8_t buf[1024];
924 block_len = MIN(sizeof(buf), size);
926 qemu_put_buffer(f, buf, block_len);
932 const VMStateInfo vmstate_info_unused_buffer = {
933 .name = "unused_buffer",
934 .get = get_unused_buffer,
935 .put = put_unused_buffer,
938 /* bitmaps (as defined by bitmap.h). Note that size here is the size
939 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
940 * bit words with the bits in big endian order. The in-memory format
941 * is an array of 'unsigned long', which may be either 32 or 64 bits.
943 /* This is the number of 64 bit words sent over the wire */
944 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
945 static int get_bitmap(QEMUFile *f, void *pv, size_t size, VMStateField *field)
947 unsigned long *bmp = pv;
949 for (i = 0; i < BITS_TO_U64S(size); i++) {
950 uint64_t w = qemu_get_be64(f);
952 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
953 bmp[idx++] = w >> 32;
959 static int put_bitmap(QEMUFile *f, void *pv, size_t size, VMStateField *field,
962 unsigned long *bmp = pv;
964 for (i = 0; i < BITS_TO_U64S(size); i++) {
965 uint64_t w = bmp[idx++];
966 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
967 w |= ((uint64_t)bmp[idx++]) << 32;
975 const VMStateInfo vmstate_info_bitmap = {
982 * meta data about the QTAILQ is encoded in a VMStateField structure
984 static int get_qtailq(QEMUFile *f, void *pv, size_t unused_size,
988 const VMStateDescription *vmsd = field->vmsd;
989 /* size of a QTAILQ element */
990 size_t size = field->size;
991 /* offset of the QTAILQ entry in a QTAILQ element */
992 size_t entry_offset = field->start;
993 int version_id = field->version_id;
996 trace_get_qtailq(vmsd->name, version_id);
997 if (version_id > vmsd->version_id) {
998 error_report("%s %s", vmsd->name, "too new");
999 trace_get_qtailq_end(vmsd->name, "too new", -EINVAL);
1003 if (version_id < vmsd->minimum_version_id) {
1004 error_report("%s %s", vmsd->name, "too old");
1005 trace_get_qtailq_end(vmsd->name, "too old", -EINVAL);
1009 while (qemu_get_byte(f)) {
1010 elm = g_malloc(size);
1011 ret = vmstate_load_state(f, vmsd, elm, version_id);
1015 QTAILQ_RAW_INSERT_TAIL(pv, elm, entry_offset);
1018 trace_get_qtailq_end(vmsd->name, "end", ret);
1022 /* put for QTAILQ */
1023 static int put_qtailq(QEMUFile *f, void *pv, size_t unused_size,
1024 VMStateField *field, QJSON *vmdesc)
1026 const VMStateDescription *vmsd = field->vmsd;
1027 /* offset of the QTAILQ entry in a QTAILQ element*/
1028 size_t entry_offset = field->start;
1031 trace_put_qtailq(vmsd->name, vmsd->version_id);
1033 QTAILQ_RAW_FOREACH(elm, pv, entry_offset) {
1034 qemu_put_byte(f, true);
1035 vmstate_save_state(f, vmsd, elm, vmdesc);
1037 qemu_put_byte(f, false);
1039 trace_put_qtailq_end(vmsd->name, "end");
1043 const VMStateInfo vmstate_info_qtailq = {