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"
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
);
31 if (field
->flags
& VMS_MULTIPLY_ELEMENTS
) {
32 n_elems
*= field
->num
;
35 trace_vmstate_n_elems(field
->name
, n_elems
);
39 static int vmstate_size(void *opaque
, VMStateField
*field
)
41 int size
= field
->size
;
43 if (field
->flags
& VMS_VBUFFER
) {
44 size
= *(int32_t *)(opaque
+field
->size_offset
);
45 if (field
->flags
& VMS_MULTIPLY
) {
53 static void *vmstate_base_addr(void *opaque
, VMStateField
*field
, bool alloc
)
55 void *base_addr
= opaque
+ field
->offset
;
57 if (field
->flags
& VMS_POINTER
) {
58 if (alloc
&& (field
->flags
& VMS_ALLOC
)) {
60 if (field
->flags
& VMS_VBUFFER
) {
61 size
= vmstate_size(opaque
, field
);
63 int n_elems
= vmstate_n_elems(opaque
, field
);
65 size
= n_elems
* field
->size
;
69 *((void **)base_addr
+ field
->start
) = g_malloc(size
);
72 base_addr
= *(void **)base_addr
+ field
->start
;
78 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
79 void *opaque
, int version_id
)
81 VMStateField
*field
= vmsd
->fields
;
84 trace_vmstate_load_state(vmsd
->name
, version_id
);
85 if (version_id
> vmsd
->version_id
) {
86 trace_vmstate_load_state_end(vmsd
->name
, "too new", -EINVAL
);
89 if (version_id
< vmsd
->minimum_version_id
) {
90 if (vmsd
->load_state_old
&&
91 version_id
>= vmsd
->minimum_version_id_old
) {
92 ret
= vmsd
->load_state_old(f
, opaque
, version_id
);
93 trace_vmstate_load_state_end(vmsd
->name
, "old path", ret
);
96 trace_vmstate_load_state_end(vmsd
->name
, "too old", -EINVAL
);
100 int ret
= vmsd
->pre_load(opaque
);
105 while (field
->name
) {
106 trace_vmstate_load_state_field(vmsd
->name
, field
->name
);
107 if ((field
->field_exists
&&
108 field
->field_exists(opaque
, version_id
)) ||
109 (!field
->field_exists
&&
110 field
->version_id
<= version_id
)) {
111 void *base_addr
= vmstate_base_addr(opaque
, field
, true);
112 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
113 int size
= vmstate_size(opaque
, field
);
115 for (i
= 0; i
< n_elems
; i
++) {
116 void *addr
= base_addr
+ size
* i
;
118 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
119 addr
= *(void **)addr
;
121 if (field
->flags
& VMS_STRUCT
) {
122 ret
= vmstate_load_state(f
, field
->vmsd
, addr
,
123 field
->vmsd
->version_id
);
125 ret
= field
->info
->get(f
, addr
, size
);
129 ret
= qemu_file_get_error(f
);
132 qemu_file_set_error(f
, ret
);
133 error_report("Failed to load %s:%s", vmsd
->name
,
135 trace_vmstate_load_field_error(field
->name
, ret
);
139 } else if (field
->flags
& VMS_MUST_EXIST
) {
140 error_report("Input validation failed: %s/%s",
141 vmsd
->name
, field
->name
);
146 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
150 if (vmsd
->post_load
) {
151 ret
= vmsd
->post_load(opaque
, version_id
);
153 trace_vmstate_load_state_end(vmsd
->name
, "end", ret
);
157 static int vmfield_name_num(VMStateField
*start
, VMStateField
*search
)
162 for (field
= start
; field
->name
; field
++) {
163 if (!strcmp(field
->name
, search
->name
)) {
164 if (field
== search
) {
174 static bool vmfield_name_is_unique(VMStateField
*start
, VMStateField
*search
)
179 for (field
= start
; field
->name
; field
++) {
180 if (!strcmp(field
->name
, search
->name
)) {
182 /* name found more than once, so it's not unique */
192 static const char *vmfield_get_type_name(VMStateField
*field
)
194 const char *type
= "unknown";
196 if (field
->flags
& VMS_STRUCT
) {
198 } else if (field
->info
->name
) {
199 type
= field
->info
->name
;
205 static bool vmsd_can_compress(VMStateField
*field
)
207 if (field
->field_exists
) {
208 /* Dynamically existing fields mess up compression */
212 if (field
->flags
& VMS_STRUCT
) {
213 VMStateField
*sfield
= field
->vmsd
->fields
;
214 while (sfield
->name
) {
215 if (!vmsd_can_compress(sfield
)) {
216 /* Child elements can't compress, so can't we */
222 if (field
->vmsd
->subsections
) {
223 /* Subsections may come and go, better don't compress */
231 static void vmsd_desc_field_start(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
232 VMStateField
*field
, int i
, int max
)
234 char *name
, *old_name
;
235 bool is_array
= max
> 1;
236 bool can_compress
= vmsd_can_compress(field
);
242 name
= g_strdup(field
->name
);
244 /* Field name is not unique, need to make it unique */
245 if (!vmfield_name_is_unique(vmsd
->fields
, field
)) {
246 int num
= vmfield_name_num(vmsd
->fields
, field
);
248 name
= g_strdup_printf("%s[%d]", name
, num
);
252 json_start_object(vmdesc
, NULL
);
253 json_prop_str(vmdesc
, "name", name
);
256 json_prop_int(vmdesc
, "array_len", max
);
258 json_prop_int(vmdesc
, "index", i
);
261 json_prop_str(vmdesc
, "type", vmfield_get_type_name(field
));
263 if (field
->flags
& VMS_STRUCT
) {
264 json_start_object(vmdesc
, "struct");
270 static void vmsd_desc_field_end(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
271 VMStateField
*field
, size_t size
, int i
)
277 if (field
->flags
& VMS_STRUCT
) {
278 /* We printed a struct in between, close its child object */
279 json_end_object(vmdesc
);
282 json_prop_int(vmdesc
, "size", size
);
283 json_end_object(vmdesc
);
287 bool vmstate_save_needed(const VMStateDescription
*vmsd
, void *opaque
)
289 if (vmsd
->needed
&& !vmsd
->needed(opaque
)) {
290 /* optional section not needed */
297 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
298 void *opaque
, QJSON
*vmdesc
)
300 VMStateField
*field
= vmsd
->fields
;
302 if (vmsd
->pre_save
) {
303 vmsd
->pre_save(opaque
);
307 json_prop_str(vmdesc
, "vmsd_name", vmsd
->name
);
308 json_prop_int(vmdesc
, "version", vmsd
->version_id
);
309 json_start_array(vmdesc
, "fields");
312 while (field
->name
) {
313 if (!field
->field_exists
||
314 field
->field_exists(opaque
, vmsd
->version_id
)) {
315 void *base_addr
= vmstate_base_addr(opaque
, field
, false);
316 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
317 int size
= vmstate_size(opaque
, field
);
318 int64_t old_offset
, written_bytes
;
319 QJSON
*vmdesc_loop
= vmdesc
;
321 for (i
= 0; i
< n_elems
; i
++) {
322 void *addr
= base_addr
+ size
* i
;
324 vmsd_desc_field_start(vmsd
, vmdesc_loop
, field
, i
, n_elems
);
325 old_offset
= qemu_ftell_fast(f
);
327 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
328 addr
= *(void **)addr
;
330 if (field
->flags
& VMS_STRUCT
) {
331 vmstate_save_state(f
, field
->vmsd
, addr
, vmdesc_loop
);
333 field
->info
->put(f
, addr
, size
);
336 written_bytes
= qemu_ftell_fast(f
) - old_offset
;
337 vmsd_desc_field_end(vmsd
, vmdesc_loop
, field
, written_bytes
, i
);
339 /* Compressed arrays only care about the first element */
340 if (vmdesc_loop
&& vmsd_can_compress(field
)) {
345 if (field
->flags
& VMS_MUST_EXIST
) {
346 error_report("Output state validation failed: %s/%s",
347 vmsd
->name
, field
->name
);
348 assert(!(field
->flags
& VMS_MUST_EXIST
));
355 json_end_array(vmdesc
);
358 vmstate_subsection_save(f
, vmsd
, opaque
, vmdesc
);
361 static const VMStateDescription
*
362 vmstate_get_subsection(const VMStateDescription
**sub
, char *idstr
)
364 while (sub
&& *sub
&& (*sub
)->needed
) {
365 if (strcmp(idstr
, (*sub
)->name
) == 0) {
373 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
376 trace_vmstate_subsection_load(vmsd
->name
);
378 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
379 char idstr
[256], *idstr_ret
;
381 uint8_t version_id
, len
, size
;
382 const VMStateDescription
*sub_vmsd
;
384 len
= qemu_peek_byte(f
, 1);
385 if (len
< strlen(vmsd
->name
) + 1) {
386 /* subsection name has be be "section_name/a" */
387 trace_vmstate_subsection_load_bad(vmsd
->name
, "(short)", "");
390 size
= qemu_peek_buffer(f
, (uint8_t **)&idstr_ret
, len
, 2);
392 trace_vmstate_subsection_load_bad(vmsd
->name
, "(peek fail)", "");
395 memcpy(idstr
, idstr_ret
, size
);
398 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
399 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(prefix)");
400 /* it doesn't have a valid subsection name */
403 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
404 if (sub_vmsd
== NULL
) {
405 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(lookup)");
408 qemu_file_skip(f
, 1); /* subsection */
409 qemu_file_skip(f
, 1); /* len */
410 qemu_file_skip(f
, len
); /* idstr */
411 version_id
= qemu_get_be32(f
);
413 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
415 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(child)");
420 trace_vmstate_subsection_load_good(vmsd
->name
);
424 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
425 void *opaque
, QJSON
*vmdesc
)
427 const VMStateDescription
**sub
= vmsd
->subsections
;
428 bool subsection_found
= false;
430 while (sub
&& *sub
&& (*sub
)->needed
) {
431 if ((*sub
)->needed(opaque
)) {
432 const VMStateDescription
*vmsd
= *sub
;
436 /* Only create subsection array when we have any */
437 if (!subsection_found
) {
438 json_start_array(vmdesc
, "subsections");
439 subsection_found
= true;
442 json_start_object(vmdesc
, NULL
);
445 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
446 len
= strlen(vmsd
->name
);
447 qemu_put_byte(f
, len
);
448 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
449 qemu_put_be32(f
, vmsd
->version_id
);
450 vmstate_save_state(f
, vmsd
, opaque
, vmdesc
);
453 json_end_object(vmdesc
);
459 if (vmdesc
&& subsection_found
) {
460 json_end_array(vmdesc
);
466 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
469 *v
= qemu_get_byte(f
);
473 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
476 qemu_put_byte(f
, *v
);
479 const VMStateInfo vmstate_info_bool
= {
487 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
494 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
500 const VMStateInfo vmstate_info_int8
= {
508 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
511 qemu_get_sbe16s(f
, v
);
515 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
518 qemu_put_sbe16s(f
, v
);
521 const VMStateInfo vmstate_info_int16
= {
529 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
532 qemu_get_sbe32s(f
, v
);
536 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
539 qemu_put_sbe32s(f
, v
);
542 const VMStateInfo vmstate_info_int32
= {
548 /* 32 bit int. See that the received value is the same than the one
551 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
555 qemu_get_sbe32s(f
, &v2
);
560 error_report("%" PRIx32
" != %" PRIx32
, *v
, v2
);
564 const VMStateInfo vmstate_info_int32_equal
= {
565 .name
= "int32 equal",
566 .get
= get_int32_equal
,
570 /* 32 bit int. Check that the received value is non-negative
571 * and less than or equal to the one in the field.
574 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
578 qemu_get_sbe32s(f
, &loaded
);
580 if (loaded
>= 0 && loaded
<= *cur
) {
584 error_report("Invalid value %" PRId32
585 " expecting positive value <= %" PRId32
,
590 const VMStateInfo vmstate_info_int32_le
= {
598 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
601 qemu_get_sbe64s(f
, v
);
605 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
608 qemu_put_sbe64s(f
, v
);
611 const VMStateInfo vmstate_info_int64
= {
617 /* 8 bit unsigned int */
619 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
626 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
632 const VMStateInfo vmstate_info_uint8
= {
638 /* 16 bit unsigned int */
640 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
643 qemu_get_be16s(f
, v
);
647 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
650 qemu_put_be16s(f
, v
);
653 const VMStateInfo vmstate_info_uint16
= {
659 /* 32 bit unsigned int */
661 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
664 qemu_get_be32s(f
, v
);
668 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
671 qemu_put_be32s(f
, v
);
674 const VMStateInfo vmstate_info_uint32
= {
680 /* 32 bit uint. See that the received value is the same than the one
683 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
687 qemu_get_be32s(f
, &v2
);
692 error_report("%" PRIx32
" != %" PRIx32
, *v
, v2
);
696 const VMStateInfo vmstate_info_uint32_equal
= {
697 .name
= "uint32 equal",
698 .get
= get_uint32_equal
,
702 /* 64 bit unsigned int */
704 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
707 qemu_get_be64s(f
, v
);
711 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
714 qemu_put_be64s(f
, v
);
717 const VMStateInfo vmstate_info_uint64
= {
723 /* 64 bit unsigned int. See that the received value is the same than the one
726 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
730 qemu_get_be64s(f
, &v2
);
735 error_report("%" PRIx64
" != %" PRIx64
, *v
, v2
);
739 const VMStateInfo vmstate_info_uint64_equal
= {
740 .name
= "int64 equal",
741 .get
= get_uint64_equal
,
745 /* 8 bit int. See that the received value is the same than the one
748 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
757 error_report("%x != %x", *v
, v2
);
761 const VMStateInfo vmstate_info_uint8_equal
= {
762 .name
= "uint8 equal",
763 .get
= get_uint8_equal
,
767 /* 16 bit unsigned int int. See that the received value is the same than the one
770 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
774 qemu_get_be16s(f
, &v2
);
779 error_report("%x != %x", *v
, v2
);
783 const VMStateInfo vmstate_info_uint16_equal
= {
784 .name
= "uint16 equal",
785 .get
= get_uint16_equal
,
791 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
795 *v
= make_float64(qemu_get_be64(f
));
799 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
803 qemu_put_be64(f
, float64_val(*v
));
806 const VMStateInfo vmstate_info_float64
= {
812 /* CPU_DoubleU type */
814 static int get_cpudouble(QEMUFile
*f
, void *pv
, size_t size
)
817 qemu_get_be32s(f
, &v
->l
.upper
);
818 qemu_get_be32s(f
, &v
->l
.lower
);
822 static void put_cpudouble(QEMUFile
*f
, void *pv
, size_t size
)
825 qemu_put_be32s(f
, &v
->l
.upper
);
826 qemu_put_be32s(f
, &v
->l
.lower
);
829 const VMStateInfo vmstate_info_cpudouble
= {
830 .name
= "CPU_Double_U",
831 .get
= get_cpudouble
,
832 .put
= put_cpudouble
,
835 /* uint8_t buffers */
837 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
840 qemu_get_buffer(f
, v
, size
);
844 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
847 qemu_put_buffer(f
, v
, size
);
850 const VMStateInfo vmstate_info_buffer
= {
856 /* unused buffers: space that was used for some fields that are
857 not useful anymore */
859 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
865 block_len
= MIN(sizeof(buf
), size
);
867 qemu_get_buffer(f
, buf
, block_len
);
872 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
874 static const uint8_t buf
[1024];
878 block_len
= MIN(sizeof(buf
), size
);
880 qemu_put_buffer(f
, buf
, block_len
);
884 const VMStateInfo vmstate_info_unused_buffer
= {
885 .name
= "unused_buffer",
886 .get
= get_unused_buffer
,
887 .put
= put_unused_buffer
,
890 /* bitmaps (as defined by bitmap.h). Note that size here is the size
891 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
892 * bit words with the bits in big endian order. The in-memory format
893 * is an array of 'unsigned long', which may be either 32 or 64 bits.
895 /* This is the number of 64 bit words sent over the wire */
896 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
897 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
899 unsigned long *bmp
= pv
;
901 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
902 uint64_t w
= qemu_get_be64(f
);
904 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
905 bmp
[idx
++] = w
>> 32;
911 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
913 unsigned long *bmp
= pv
;
915 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
916 uint64_t w
= bmp
[idx
++];
917 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
918 w
|= ((uint64_t)bmp
[idx
++]) << 32;
924 const VMStateInfo vmstate_info_bitmap
= {