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 trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
91 if (version_id < vmsd->minimum_version_id) {
92 if (vmsd->load_state_old &&
93 version_id >= vmsd->minimum_version_id_old) {
94 ret = vmsd->load_state_old(f, opaque, version_id);
95 trace_vmstate_load_state_end(vmsd->name, "old path", ret);
98 trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
101 if (vmsd->pre_load) {
102 int ret = vmsd->pre_load(opaque);
107 while (field->name) {
108 trace_vmstate_load_state_field(vmsd->name, field->name);
109 if ((field->field_exists &&
110 field->field_exists(opaque, version_id)) ||
111 (!field->field_exists &&
112 field->version_id <= version_id)) {
113 void *base_addr = vmstate_base_addr(opaque, field, true);
114 int i, n_elems = vmstate_n_elems(opaque, field);
115 int size = vmstate_size(opaque, field);
117 for (i = 0; i < n_elems; i++) {
118 void *addr = base_addr + size * i;
120 if (field->flags & VMS_ARRAY_OF_POINTER) {
121 addr = *(void **)addr;
123 if (field->flags & VMS_STRUCT) {
124 ret = vmstate_load_state(f, field->vmsd, addr,
125 field->vmsd->version_id);
127 ret = field->info->get(f, addr, size, field);
130 ret = qemu_file_get_error(f);
133 qemu_file_set_error(f, ret);
134 error_report("Failed to load %s:%s", vmsd->name,
136 trace_vmstate_load_field_error(field->name, ret);
140 } else if (field->flags & VMS_MUST_EXIST) {
141 error_report("Input validation failed: %s/%s",
142 vmsd->name, field->name);
147 ret = vmstate_subsection_load(f, vmsd, opaque);
151 if (vmsd->post_load) {
152 ret = vmsd->post_load(opaque, version_id);
154 trace_vmstate_load_state_end(vmsd->name, "end", ret);
158 static int vmfield_name_num(VMStateField *start, VMStateField *search)
163 for (field = start; field->name; field++) {
164 if (!strcmp(field->name, search->name)) {
165 if (field == search) {
175 static bool vmfield_name_is_unique(VMStateField *start, VMStateField *search)
180 for (field = start; field->name; field++) {
181 if (!strcmp(field->name, search->name)) {
183 /* name found more than once, so it's not unique */
193 static const char *vmfield_get_type_name(VMStateField *field)
195 const char *type = "unknown";
197 if (field->flags & VMS_STRUCT) {
199 } else if (field->info->name) {
200 type = field->info->name;
206 static bool vmsd_can_compress(VMStateField *field)
208 if (field->field_exists) {
209 /* Dynamically existing fields mess up compression */
213 if (field->flags & VMS_STRUCT) {
214 VMStateField *sfield = field->vmsd->fields;
215 while (sfield->name) {
216 if (!vmsd_can_compress(sfield)) {
217 /* Child elements can't compress, so can't we */
223 if (field->vmsd->subsections) {
224 /* Subsections may come and go, better don't compress */
232 static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc,
233 VMStateField *field, int i, int max)
235 char *name, *old_name;
236 bool is_array = max > 1;
237 bool can_compress = vmsd_can_compress(field);
243 name = g_strdup(field->name);
245 /* Field name is not unique, need to make it unique */
246 if (!vmfield_name_is_unique(vmsd->fields, field)) {
247 int num = vmfield_name_num(vmsd->fields, field);
249 name = g_strdup_printf("%s[%d]", name, num);
253 json_start_object(vmdesc, NULL);
254 json_prop_str(vmdesc, "name", name);
257 json_prop_int(vmdesc, "array_len", max);
259 json_prop_int(vmdesc, "index", i);
262 json_prop_str(vmdesc, "type", vmfield_get_type_name(field));
264 if (field->flags & VMS_STRUCT) {
265 json_start_object(vmdesc, "struct");
271 static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc,
272 VMStateField *field, size_t size, int i)
278 if (field->flags & VMS_STRUCT) {
279 /* We printed a struct in between, close its child object */
280 json_end_object(vmdesc);
283 json_prop_int(vmdesc, "size", size);
284 json_end_object(vmdesc);
288 bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque)
290 if (vmsd->needed && !vmsd->needed(opaque)) {
291 /* optional section not needed */
298 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
299 void *opaque, QJSON *vmdesc)
301 VMStateField *field = vmsd->fields;
303 if (vmsd->pre_save) {
304 vmsd->pre_save(opaque);
308 json_prop_str(vmdesc, "vmsd_name", vmsd->name);
309 json_prop_int(vmdesc, "version", vmsd->version_id);
310 json_start_array(vmdesc, "fields");
313 while (field->name) {
314 if (!field->field_exists ||
315 field->field_exists(opaque, vmsd->version_id)) {
316 void *base_addr = vmstate_base_addr(opaque, field, false);
317 int i, n_elems = vmstate_n_elems(opaque, field);
318 int size = vmstate_size(opaque, field);
319 int64_t old_offset, written_bytes;
320 QJSON *vmdesc_loop = vmdesc;
322 for (i = 0; i < n_elems; i++) {
323 void *addr = base_addr + size * i;
325 vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
326 old_offset = qemu_ftell_fast(f);
328 if (field->flags & VMS_ARRAY_OF_POINTER) {
329 addr = *(void **)addr;
331 if (field->flags & VMS_STRUCT) {
332 vmstate_save_state(f, field->vmsd, addr, vmdesc_loop);
334 field->info->put(f, addr, size, field, vmdesc_loop);
337 written_bytes = qemu_ftell_fast(f) - old_offset;
338 vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
340 /* Compressed arrays only care about the first element */
341 if (vmdesc_loop && vmsd_can_compress(field)) {
346 if (field->flags & VMS_MUST_EXIST) {
347 error_report("Output state validation failed: %s/%s",
348 vmsd->name, field->name);
349 assert(!(field->flags & VMS_MUST_EXIST));
356 json_end_array(vmdesc);
359 vmstate_subsection_save(f, vmsd, opaque, vmdesc);
362 static const VMStateDescription *
363 vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
365 while (sub && *sub && (*sub)->needed) {
366 if (strcmp(idstr, (*sub)->name) == 0) {
374 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
377 trace_vmstate_subsection_load(vmsd->name);
379 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
380 char idstr[256], *idstr_ret;
382 uint8_t version_id, len, size;
383 const VMStateDescription *sub_vmsd;
385 len = qemu_peek_byte(f, 1);
386 if (len < strlen(vmsd->name) + 1) {
387 /* subsection name has be be "section_name/a" */
388 trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
391 size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
393 trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
396 memcpy(idstr, idstr_ret, size);
399 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
400 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
401 /* it doesn't have a valid subsection name */
404 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
405 if (sub_vmsd == NULL) {
406 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
409 qemu_file_skip(f, 1); /* subsection */
410 qemu_file_skip(f, 1); /* len */
411 qemu_file_skip(f, len); /* idstr */
412 version_id = qemu_get_be32(f);
414 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
416 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
421 trace_vmstate_subsection_load_good(vmsd->name);
425 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
426 void *opaque, QJSON *vmdesc)
428 const VMStateDescription **sub = vmsd->subsections;
429 bool subsection_found = false;
431 while (sub && *sub && (*sub)->needed) {
432 if ((*sub)->needed(opaque)) {
433 const VMStateDescription *vmsd = *sub;
437 /* Only create subsection array when we have any */
438 if (!subsection_found) {
439 json_start_array(vmdesc, "subsections");
440 subsection_found = true;
443 json_start_object(vmdesc, NULL);
446 qemu_put_byte(f, QEMU_VM_SUBSECTION);
447 len = strlen(vmsd->name);
448 qemu_put_byte(f, len);
449 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
450 qemu_put_be32(f, vmsd->version_id);
451 vmstate_save_state(f, vmsd, opaque, vmdesc);
454 json_end_object(vmdesc);
460 if (vmdesc && subsection_found) {
461 json_end_array(vmdesc);
467 static int get_bool(QEMUFile *f, void *pv, size_t size, VMStateField *field)
470 *v = qemu_get_byte(f);
474 static int put_bool(QEMUFile *f, void *pv, size_t size, VMStateField *field,
478 qemu_put_byte(f, *v);
482 const VMStateInfo vmstate_info_bool = {
490 static int get_int8(QEMUFile *f, void *pv, size_t size, VMStateField *field)
497 static int put_int8(QEMUFile *f, void *pv, size_t size, VMStateField *field,
505 const VMStateInfo vmstate_info_int8 = {
513 static int get_int16(QEMUFile *f, void *pv, size_t size, VMStateField *field)
516 qemu_get_sbe16s(f, v);
520 static int put_int16(QEMUFile *f, void *pv, size_t size, VMStateField *field,
524 qemu_put_sbe16s(f, v);
528 const VMStateInfo vmstate_info_int16 = {
536 static int get_int32(QEMUFile *f, void *pv, size_t size, VMStateField *field)
539 qemu_get_sbe32s(f, v);
543 static int put_int32(QEMUFile *f, void *pv, size_t size, VMStateField *field,
547 qemu_put_sbe32s(f, v);
551 const VMStateInfo vmstate_info_int32 = {
557 /* 32 bit int. See that the received value is the same than the one
560 static int get_int32_equal(QEMUFile *f, void *pv, size_t size,
565 qemu_get_sbe32s(f, &v2);
570 error_report("%" PRIx32 " != %" PRIx32, *v, v2);
574 const VMStateInfo vmstate_info_int32_equal = {
575 .name = "int32 equal",
576 .get = get_int32_equal,
580 /* 32 bit int. Check that the received value is non-negative
581 * and less than or equal to the one in the field.
584 static int get_int32_le(QEMUFile *f, void *pv, size_t size, VMStateField *field)
588 qemu_get_sbe32s(f, &loaded);
590 if (loaded >= 0 && loaded <= *cur) {
594 error_report("Invalid value %" PRId32
595 " expecting positive value <= %" PRId32,
600 const VMStateInfo vmstate_info_int32_le = {
608 static int get_int64(QEMUFile *f, void *pv, size_t size, VMStateField *field)
611 qemu_get_sbe64s(f, v);
615 static int put_int64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
619 qemu_put_sbe64s(f, v);
623 const VMStateInfo vmstate_info_int64 = {
629 /* 8 bit unsigned int */
631 static int get_uint8(QEMUFile *f, void *pv, size_t size, VMStateField *field)
638 static int put_uint8(QEMUFile *f, void *pv, size_t size, VMStateField *field,
646 const VMStateInfo vmstate_info_uint8 = {
652 /* 16 bit unsigned int */
654 static int get_uint16(QEMUFile *f, void *pv, size_t size, VMStateField *field)
657 qemu_get_be16s(f, v);
661 static int put_uint16(QEMUFile *f, void *pv, size_t size, VMStateField *field,
665 qemu_put_be16s(f, v);
669 const VMStateInfo vmstate_info_uint16 = {
675 /* 32 bit unsigned int */
677 static int get_uint32(QEMUFile *f, void *pv, size_t size, VMStateField *field)
680 qemu_get_be32s(f, v);
684 static int put_uint32(QEMUFile *f, void *pv, size_t size, VMStateField *field,
688 qemu_put_be32s(f, v);
692 const VMStateInfo vmstate_info_uint32 = {
698 /* 32 bit uint. See that the received value is the same than the one
701 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size,
706 qemu_get_be32s(f, &v2);
711 error_report("%" PRIx32 " != %" PRIx32, *v, v2);
715 const VMStateInfo vmstate_info_uint32_equal = {
716 .name = "uint32 equal",
717 .get = get_uint32_equal,
721 /* 64 bit unsigned int */
723 static int get_uint64(QEMUFile *f, void *pv, size_t size, VMStateField *field)
726 qemu_get_be64s(f, v);
730 static int put_uint64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
734 qemu_put_be64s(f, v);
738 const VMStateInfo vmstate_info_uint64 = {
744 /* 64 bit unsigned int. See that the received value is the same than the one
747 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size,
752 qemu_get_be64s(f, &v2);
757 error_report("%" PRIx64 " != %" PRIx64, *v, v2);
761 const VMStateInfo vmstate_info_uint64_equal = {
762 .name = "int64 equal",
763 .get = get_uint64_equal,
767 /* 8 bit int. See that the received value is the same than the one
770 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size,
780 error_report("%x != %x", *v, v2);
784 const VMStateInfo vmstate_info_uint8_equal = {
785 .name = "uint8 equal",
786 .get = get_uint8_equal,
790 /* 16 bit unsigned int int. See that the received value is the same than the one
793 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size,
798 qemu_get_be16s(f, &v2);
803 error_report("%x != %x", *v, v2);
807 const VMStateInfo vmstate_info_uint16_equal = {
808 .name = "uint16 equal",
809 .get = get_uint16_equal,
815 static int get_float64(QEMUFile *f, void *pv, size_t size,
820 *v = make_float64(qemu_get_be64(f));
824 static int put_float64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
829 qemu_put_be64(f, float64_val(*v));
833 const VMStateInfo vmstate_info_float64 = {
839 /* CPU_DoubleU type */
841 static int get_cpudouble(QEMUFile *f, void *pv, size_t size,
845 qemu_get_be32s(f, &v->l.upper);
846 qemu_get_be32s(f, &v->l.lower);
850 static int put_cpudouble(QEMUFile *f, void *pv, size_t size,
851 VMStateField *field, QJSON *vmdesc)
854 qemu_put_be32s(f, &v->l.upper);
855 qemu_put_be32s(f, &v->l.lower);
859 const VMStateInfo vmstate_info_cpudouble = {
860 .name = "CPU_Double_U",
861 .get = get_cpudouble,
862 .put = put_cpudouble,
865 /* uint8_t buffers */
867 static int get_buffer(QEMUFile *f, void *pv, size_t size,
871 qemu_get_buffer(f, v, size);
875 static int put_buffer(QEMUFile *f, void *pv, size_t size, VMStateField *field,
879 qemu_put_buffer(f, v, size);
883 const VMStateInfo vmstate_info_buffer = {
889 /* unused buffers: space that was used for some fields that are
890 not useful anymore */
892 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size,
899 block_len = MIN(sizeof(buf), size);
901 qemu_get_buffer(f, buf, block_len);
906 static int put_unused_buffer(QEMUFile *f, void *pv, size_t size,
907 VMStateField *field, QJSON *vmdesc)
909 static const uint8_t buf[1024];
913 block_len = MIN(sizeof(buf), size);
915 qemu_put_buffer(f, buf, block_len);
921 const VMStateInfo vmstate_info_unused_buffer = {
922 .name = "unused_buffer",
923 .get = get_unused_buffer,
924 .put = put_unused_buffer,
927 /* bitmaps (as defined by bitmap.h). Note that size here is the size
928 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
929 * bit words with the bits in big endian order. The in-memory format
930 * is an array of 'unsigned long', which may be either 32 or 64 bits.
932 /* This is the number of 64 bit words sent over the wire */
933 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
934 static int get_bitmap(QEMUFile *f, void *pv, size_t size, VMStateField *field)
936 unsigned long *bmp = pv;
938 for (i = 0; i < BITS_TO_U64S(size); i++) {
939 uint64_t w = qemu_get_be64(f);
941 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
942 bmp[idx++] = w >> 32;
948 static int put_bitmap(QEMUFile *f, void *pv, size_t size, VMStateField *field,
951 unsigned long *bmp = pv;
953 for (i = 0; i < BITS_TO_U64S(size); i++) {
954 uint64_t w = bmp[idx++];
955 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
956 w |= ((uint64_t)bmp[idx++]) << 32;
964 const VMStateInfo vmstate_info_bitmap = {
971 * meta data about the QTAILQ is encoded in a VMStateField structure
973 static int get_qtailq(QEMUFile *f, void *pv, size_t unused_size,
977 const VMStateDescription *vmsd = field->vmsd;
978 /* size of a QTAILQ element */
979 size_t size = field->size;
980 /* offset of the QTAILQ entry in a QTAILQ element */
981 size_t entry_offset = field->start;
982 int version_id = field->version_id;
985 trace_get_qtailq(vmsd->name, version_id);
986 if (version_id > vmsd->version_id) {
987 error_report("%s %s", vmsd->name, "too new");
988 trace_get_qtailq_end(vmsd->name, "too new", -EINVAL);
992 if (version_id < vmsd->minimum_version_id) {
993 error_report("%s %s", vmsd->name, "too old");
994 trace_get_qtailq_end(vmsd->name, "too old", -EINVAL);
998 while (qemu_get_byte(f)) {
999 elm = g_malloc(size);
1000 ret = vmstate_load_state(f, vmsd, elm, version_id);
1004 QTAILQ_RAW_INSERT_TAIL(pv, elm, entry_offset);
1007 trace_get_qtailq_end(vmsd->name, "end", ret);
1011 /* put for QTAILQ */
1012 static int put_qtailq(QEMUFile *f, void *pv, size_t unused_size,
1013 VMStateField *field, QJSON *vmdesc)
1015 const VMStateDescription *vmsd = field->vmsd;
1016 /* offset of the QTAILQ entry in a QTAILQ element*/
1017 size_t entry_offset = field->start;
1020 trace_put_qtailq(vmsd->name, vmsd->version_id);
1022 QTAILQ_RAW_FOREACH(elm, pv, entry_offset) {
1023 qemu_put_byte(f, true);
1024 vmstate_save_state(f, vmsd, elm, vmdesc);
1026 qemu_put_byte(f, false);
1028 trace_put_qtailq_end(vmsd->name, "end");
1032 const VMStateInfo vmstate_info_qtailq = {