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
;
38 static int vmstate_size(void *opaque
, VMStateField
*field
)
40 int size
= field
->size
;
42 if (field
->flags
& VMS_VBUFFER
) {
43 size
= *(int32_t *)(opaque
+field
->size_offset
);
44 if (field
->flags
& VMS_MULTIPLY
) {
52 static void *vmstate_base_addr(void *opaque
, VMStateField
*field
, bool alloc
)
54 void *base_addr
= opaque
+ field
->offset
;
56 if (field
->flags
& VMS_POINTER
) {
57 if (alloc
&& (field
->flags
& VMS_ALLOC
)) {
59 if (field
->flags
& VMS_VBUFFER
) {
60 size
= vmstate_size(opaque
, field
);
62 int n_elems
= vmstate_n_elems(opaque
, field
);
64 size
= n_elems
* field
->size
;
68 *((void **)base_addr
+ field
->start
) = g_malloc(size
);
71 base_addr
= *(void **)base_addr
+ field
->start
;
77 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
78 void *opaque
, int version_id
)
80 VMStateField
*field
= vmsd
->fields
;
83 trace_vmstate_load_state(vmsd
->name
, version_id
);
84 if (version_id
> vmsd
->version_id
) {
85 trace_vmstate_load_state_end(vmsd
->name
, "too new", -EINVAL
);
88 if (version_id
< vmsd
->minimum_version_id
) {
89 if (vmsd
->load_state_old
&&
90 version_id
>= vmsd
->minimum_version_id_old
) {
91 ret
= vmsd
->load_state_old(f
, opaque
, version_id
);
92 trace_vmstate_load_state_end(vmsd
->name
, "old path", ret
);
95 trace_vmstate_load_state_end(vmsd
->name
, "too old", -EINVAL
);
99 int ret
= vmsd
->pre_load(opaque
);
104 while (field
->name
) {
105 trace_vmstate_load_state_field(vmsd
->name
, field
->name
);
106 if ((field
->field_exists
&&
107 field
->field_exists(opaque
, version_id
)) ||
108 (!field
->field_exists
&&
109 field
->version_id
<= version_id
)) {
110 void *base_addr
= vmstate_base_addr(opaque
, field
, true);
111 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
112 int size
= vmstate_size(opaque
, field
);
114 for (i
= 0; i
< n_elems
; i
++) {
115 void *addr
= base_addr
+ size
* i
;
117 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
118 addr
= *(void **)addr
;
120 if (field
->flags
& VMS_STRUCT
) {
121 ret
= vmstate_load_state(f
, field
->vmsd
, addr
,
122 field
->vmsd
->version_id
);
124 ret
= field
->info
->get(f
, addr
, size
);
128 ret
= qemu_file_get_error(f
);
131 qemu_file_set_error(f
, ret
);
132 trace_vmstate_load_field_error(field
->name
, ret
);
136 } else if (field
->flags
& VMS_MUST_EXIST
) {
137 error_report("Input validation failed: %s/%s",
138 vmsd
->name
, field
->name
);
143 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
147 if (vmsd
->post_load
) {
148 ret
= vmsd
->post_load(opaque
, version_id
);
150 trace_vmstate_load_state_end(vmsd
->name
, "end", ret
);
154 static int vmfield_name_num(VMStateField
*start
, VMStateField
*search
)
159 for (field
= start
; field
->name
; field
++) {
160 if (!strcmp(field
->name
, search
->name
)) {
161 if (field
== search
) {
171 static bool vmfield_name_is_unique(VMStateField
*start
, VMStateField
*search
)
176 for (field
= start
; field
->name
; field
++) {
177 if (!strcmp(field
->name
, search
->name
)) {
179 /* name found more than once, so it's not unique */
189 static const char *vmfield_get_type_name(VMStateField
*field
)
191 const char *type
= "unknown";
193 if (field
->flags
& VMS_STRUCT
) {
195 } else if (field
->info
->name
) {
196 type
= field
->info
->name
;
202 static bool vmsd_can_compress(VMStateField
*field
)
204 if (field
->field_exists
) {
205 /* Dynamically existing fields mess up compression */
209 if (field
->flags
& VMS_STRUCT
) {
210 VMStateField
*sfield
= field
->vmsd
->fields
;
211 while (sfield
->name
) {
212 if (!vmsd_can_compress(sfield
)) {
213 /* Child elements can't compress, so can't we */
219 if (field
->vmsd
->subsections
) {
220 /* Subsections may come and go, better don't compress */
228 static void vmsd_desc_field_start(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
229 VMStateField
*field
, int i
, int max
)
231 char *name
, *old_name
;
232 bool is_array
= max
> 1;
233 bool can_compress
= vmsd_can_compress(field
);
239 name
= g_strdup(field
->name
);
241 /* Field name is not unique, need to make it unique */
242 if (!vmfield_name_is_unique(vmsd
->fields
, field
)) {
243 int num
= vmfield_name_num(vmsd
->fields
, field
);
245 name
= g_strdup_printf("%s[%d]", name
, num
);
249 json_start_object(vmdesc
, NULL
);
250 json_prop_str(vmdesc
, "name", name
);
253 json_prop_int(vmdesc
, "array_len", max
);
255 json_prop_int(vmdesc
, "index", i
);
258 json_prop_str(vmdesc
, "type", vmfield_get_type_name(field
));
260 if (field
->flags
& VMS_STRUCT
) {
261 json_start_object(vmdesc
, "struct");
267 static void vmsd_desc_field_end(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
268 VMStateField
*field
, size_t size
, int i
)
274 if (field
->flags
& VMS_STRUCT
) {
275 /* We printed a struct in between, close its child object */
276 json_end_object(vmdesc
);
279 json_prop_int(vmdesc
, "size", size
);
280 json_end_object(vmdesc
);
284 bool vmstate_save_needed(const VMStateDescription
*vmsd
, void *opaque
)
286 if (vmsd
->needed
&& !vmsd
->needed(opaque
)) {
287 /* optional section not needed */
294 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
295 void *opaque
, QJSON
*vmdesc
)
297 VMStateField
*field
= vmsd
->fields
;
299 if (vmsd
->pre_save
) {
300 vmsd
->pre_save(opaque
);
304 json_prop_str(vmdesc
, "vmsd_name", vmsd
->name
);
305 json_prop_int(vmdesc
, "version", vmsd
->version_id
);
306 json_start_array(vmdesc
, "fields");
309 while (field
->name
) {
310 if (!field
->field_exists
||
311 field
->field_exists(opaque
, vmsd
->version_id
)) {
312 void *base_addr
= vmstate_base_addr(opaque
, field
, false);
313 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
314 int size
= vmstate_size(opaque
, field
);
315 int64_t old_offset
, written_bytes
;
316 QJSON
*vmdesc_loop
= vmdesc
;
318 for (i
= 0; i
< n_elems
; i
++) {
319 void *addr
= base_addr
+ size
* i
;
321 vmsd_desc_field_start(vmsd
, vmdesc_loop
, field
, i
, n_elems
);
322 old_offset
= qemu_ftell_fast(f
);
324 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
325 addr
= *(void **)addr
;
327 if (field
->flags
& VMS_STRUCT
) {
328 vmstate_save_state(f
, field
->vmsd
, addr
, vmdesc_loop
);
330 field
->info
->put(f
, addr
, size
);
333 written_bytes
= qemu_ftell_fast(f
) - old_offset
;
334 vmsd_desc_field_end(vmsd
, vmdesc_loop
, field
, written_bytes
, i
);
336 /* Compressed arrays only care about the first element */
337 if (vmdesc_loop
&& vmsd_can_compress(field
)) {
342 if (field
->flags
& VMS_MUST_EXIST
) {
343 error_report("Output state validation failed: %s/%s",
344 vmsd
->name
, field
->name
);
345 assert(!(field
->flags
& VMS_MUST_EXIST
));
352 json_end_array(vmdesc
);
355 vmstate_subsection_save(f
, vmsd
, opaque
, vmdesc
);
358 static const VMStateDescription
*
359 vmstate_get_subsection(const VMStateDescription
**sub
, char *idstr
)
361 while (sub
&& *sub
&& (*sub
)->needed
) {
362 if (strcmp(idstr
, (*sub
)->name
) == 0) {
370 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
373 trace_vmstate_subsection_load(vmsd
->name
);
375 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
376 char idstr
[256], *idstr_ret
;
378 uint8_t version_id
, len
, size
;
379 const VMStateDescription
*sub_vmsd
;
381 len
= qemu_peek_byte(f
, 1);
382 if (len
< strlen(vmsd
->name
) + 1) {
383 /* subsection name has be be "section_name/a" */
384 trace_vmstate_subsection_load_bad(vmsd
->name
, "(short)");
387 size
= qemu_peek_buffer(f
, (uint8_t **)&idstr_ret
, len
, 2);
389 trace_vmstate_subsection_load_bad(vmsd
->name
, "(peek fail)");
392 memcpy(idstr
, idstr_ret
, size
);
395 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
396 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
);
397 /* it don't have a valid subsection name */
400 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
401 if (sub_vmsd
== NULL
) {
402 trace_vmstate_subsection_load_bad(vmsd
->name
, "(lookup)");
405 qemu_file_skip(f
, 1); /* subsection */
406 qemu_file_skip(f
, 1); /* len */
407 qemu_file_skip(f
, len
); /* idstr */
408 version_id
= qemu_get_be32(f
);
410 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
412 trace_vmstate_subsection_load_bad(vmsd
->name
, "(child)");
417 trace_vmstate_subsection_load_good(vmsd
->name
);
421 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
422 void *opaque
, QJSON
*vmdesc
)
424 const VMStateDescription
**sub
= vmsd
->subsections
;
425 bool subsection_found
= false;
427 while (sub
&& *sub
&& (*sub
)->needed
) {
428 if ((*sub
)->needed(opaque
)) {
429 const VMStateDescription
*vmsd
= *sub
;
433 /* Only create subsection array when we have any */
434 if (!subsection_found
) {
435 json_start_array(vmdesc
, "subsections");
436 subsection_found
= true;
439 json_start_object(vmdesc
, NULL
);
442 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
443 len
= strlen(vmsd
->name
);
444 qemu_put_byte(f
, len
);
445 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
446 qemu_put_be32(f
, vmsd
->version_id
);
447 vmstate_save_state(f
, vmsd
, opaque
, vmdesc
);
450 json_end_object(vmdesc
);
456 if (vmdesc
&& subsection_found
) {
457 json_end_array(vmdesc
);
463 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
466 *v
= qemu_get_byte(f
);
470 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
473 qemu_put_byte(f
, *v
);
476 const VMStateInfo vmstate_info_bool
= {
484 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
491 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
497 const VMStateInfo vmstate_info_int8
= {
505 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
508 qemu_get_sbe16s(f
, v
);
512 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
515 qemu_put_sbe16s(f
, v
);
518 const VMStateInfo vmstate_info_int16
= {
526 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
529 qemu_get_sbe32s(f
, v
);
533 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
536 qemu_put_sbe32s(f
, v
);
539 const VMStateInfo vmstate_info_int32
= {
545 /* 32 bit int. See that the received value is the same than the one
548 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
552 qemu_get_sbe32s(f
, &v2
);
560 const VMStateInfo vmstate_info_int32_equal
= {
561 .name
= "int32 equal",
562 .get
= get_int32_equal
,
566 /* 32 bit int. Check that the received value is non-negative
567 * and less than or equal to the one in the field.
570 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
574 qemu_get_sbe32s(f
, &loaded
);
576 if (loaded
>= 0 && loaded
<= *cur
) {
583 const VMStateInfo vmstate_info_int32_le
= {
591 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
594 qemu_get_sbe64s(f
, v
);
598 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
601 qemu_put_sbe64s(f
, v
);
604 const VMStateInfo vmstate_info_int64
= {
610 /* 8 bit unsigned int */
612 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
619 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
625 const VMStateInfo vmstate_info_uint8
= {
631 /* 16 bit unsigned int */
633 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
636 qemu_get_be16s(f
, v
);
640 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
643 qemu_put_be16s(f
, v
);
646 const VMStateInfo vmstate_info_uint16
= {
652 /* 32 bit unsigned int */
654 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
657 qemu_get_be32s(f
, v
);
661 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
664 qemu_put_be32s(f
, v
);
667 const VMStateInfo vmstate_info_uint32
= {
673 /* 32 bit uint. See that the received value is the same than the one
676 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
680 qemu_get_be32s(f
, &v2
);
688 const VMStateInfo vmstate_info_uint32_equal
= {
689 .name
= "uint32 equal",
690 .get
= get_uint32_equal
,
694 /* 64 bit unsigned int */
696 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
699 qemu_get_be64s(f
, v
);
703 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
706 qemu_put_be64s(f
, v
);
709 const VMStateInfo vmstate_info_uint64
= {
715 /* 64 bit unsigned int. See that the received value is the same than the one
718 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
722 qemu_get_be64s(f
, &v2
);
730 const VMStateInfo vmstate_info_uint64_equal
= {
731 .name
= "int64 equal",
732 .get
= get_uint64_equal
,
736 /* 8 bit int. See that the received value is the same than the one
739 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
751 const VMStateInfo vmstate_info_uint8_equal
= {
752 .name
= "uint8 equal",
753 .get
= get_uint8_equal
,
757 /* 16 bit unsigned int int. See that the received value is the same than the one
760 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
764 qemu_get_be16s(f
, &v2
);
772 const VMStateInfo vmstate_info_uint16_equal
= {
773 .name
= "uint16 equal",
774 .get
= get_uint16_equal
,
780 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
784 *v
= make_float64(qemu_get_be64(f
));
788 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
792 qemu_put_be64(f
, float64_val(*v
));
795 const VMStateInfo vmstate_info_float64
= {
801 /* CPU_DoubleU type */
803 static int get_cpudouble(QEMUFile
*f
, void *pv
, size_t size
)
806 qemu_get_be32s(f
, &v
->l
.upper
);
807 qemu_get_be32s(f
, &v
->l
.lower
);
811 static void put_cpudouble(QEMUFile
*f
, void *pv
, size_t size
)
814 qemu_put_be32s(f
, &v
->l
.upper
);
815 qemu_put_be32s(f
, &v
->l
.lower
);
818 const VMStateInfo vmstate_info_cpudouble
= {
819 .name
= "CPU_Double_U",
820 .get
= get_cpudouble
,
821 .put
= put_cpudouble
,
824 /* uint8_t buffers */
826 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
829 qemu_get_buffer(f
, v
, size
);
833 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
836 qemu_put_buffer(f
, v
, size
);
839 const VMStateInfo vmstate_info_buffer
= {
845 /* unused buffers: space that was used for some fields that are
846 not useful anymore */
848 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
854 block_len
= MIN(sizeof(buf
), size
);
856 qemu_get_buffer(f
, buf
, block_len
);
861 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
863 static const uint8_t buf
[1024];
867 block_len
= MIN(sizeof(buf
), size
);
869 qemu_put_buffer(f
, buf
, block_len
);
873 const VMStateInfo vmstate_info_unused_buffer
= {
874 .name
= "unused_buffer",
875 .get
= get_unused_buffer
,
876 .put
= put_unused_buffer
,
879 /* bitmaps (as defined by bitmap.h). Note that size here is the size
880 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
881 * bit words with the bits in big endian order. The in-memory format
882 * is an array of 'unsigned long', which may be either 32 or 64 bits.
884 /* This is the number of 64 bit words sent over the wire */
885 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
886 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
888 unsigned long *bmp
= pv
;
890 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
891 uint64_t w
= qemu_get_be64(f
);
893 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
894 bmp
[idx
++] = w
>> 32;
900 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
902 unsigned long *bmp
= pv
;
904 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
905 uint64_t w
= bmp
[idx
++];
906 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
907 w
|= ((uint64_t)bmp
[idx
++]) << 32;
913 const VMStateInfo vmstate_info_bitmap
= {