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 trace_vmstate_load_field_error(field
->name
, ret
);
137 } else if (field
->flags
& VMS_MUST_EXIST
) {
138 error_report("Input validation failed: %s/%s",
139 vmsd
->name
, field
->name
);
144 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
148 if (vmsd
->post_load
) {
149 ret
= vmsd
->post_load(opaque
, version_id
);
151 trace_vmstate_load_state_end(vmsd
->name
, "end", ret
);
155 static int vmfield_name_num(VMStateField
*start
, VMStateField
*search
)
160 for (field
= start
; field
->name
; field
++) {
161 if (!strcmp(field
->name
, search
->name
)) {
162 if (field
== search
) {
172 static bool vmfield_name_is_unique(VMStateField
*start
, VMStateField
*search
)
177 for (field
= start
; field
->name
; field
++) {
178 if (!strcmp(field
->name
, search
->name
)) {
180 /* name found more than once, so it's not unique */
190 static const char *vmfield_get_type_name(VMStateField
*field
)
192 const char *type
= "unknown";
194 if (field
->flags
& VMS_STRUCT
) {
196 } else if (field
->info
->name
) {
197 type
= field
->info
->name
;
203 static bool vmsd_can_compress(VMStateField
*field
)
205 if (field
->field_exists
) {
206 /* Dynamically existing fields mess up compression */
210 if (field
->flags
& VMS_STRUCT
) {
211 VMStateField
*sfield
= field
->vmsd
->fields
;
212 while (sfield
->name
) {
213 if (!vmsd_can_compress(sfield
)) {
214 /* Child elements can't compress, so can't we */
220 if (field
->vmsd
->subsections
) {
221 /* Subsections may come and go, better don't compress */
229 static void vmsd_desc_field_start(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
230 VMStateField
*field
, int i
, int max
)
232 char *name
, *old_name
;
233 bool is_array
= max
> 1;
234 bool can_compress
= vmsd_can_compress(field
);
240 name
= g_strdup(field
->name
);
242 /* Field name is not unique, need to make it unique */
243 if (!vmfield_name_is_unique(vmsd
->fields
, field
)) {
244 int num
= vmfield_name_num(vmsd
->fields
, field
);
246 name
= g_strdup_printf("%s[%d]", name
, num
);
250 json_start_object(vmdesc
, NULL
);
251 json_prop_str(vmdesc
, "name", name
);
254 json_prop_int(vmdesc
, "array_len", max
);
256 json_prop_int(vmdesc
, "index", i
);
259 json_prop_str(vmdesc
, "type", vmfield_get_type_name(field
));
261 if (field
->flags
& VMS_STRUCT
) {
262 json_start_object(vmdesc
, "struct");
268 static void vmsd_desc_field_end(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
269 VMStateField
*field
, size_t size
, int i
)
275 if (field
->flags
& VMS_STRUCT
) {
276 /* We printed a struct in between, close its child object */
277 json_end_object(vmdesc
);
280 json_prop_int(vmdesc
, "size", size
);
281 json_end_object(vmdesc
);
285 bool vmstate_save_needed(const VMStateDescription
*vmsd
, void *opaque
)
287 if (vmsd
->needed
&& !vmsd
->needed(opaque
)) {
288 /* optional section not needed */
295 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
296 void *opaque
, QJSON
*vmdesc
)
298 VMStateField
*field
= vmsd
->fields
;
300 if (vmsd
->pre_save
) {
301 vmsd
->pre_save(opaque
);
305 json_prop_str(vmdesc
, "vmsd_name", vmsd
->name
);
306 json_prop_int(vmdesc
, "version", vmsd
->version_id
);
307 json_start_array(vmdesc
, "fields");
310 while (field
->name
) {
311 if (!field
->field_exists
||
312 field
->field_exists(opaque
, vmsd
->version_id
)) {
313 void *base_addr
= vmstate_base_addr(opaque
, field
, false);
314 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
315 int size
= vmstate_size(opaque
, field
);
316 int64_t old_offset
, written_bytes
;
317 QJSON
*vmdesc_loop
= vmdesc
;
319 for (i
= 0; i
< n_elems
; i
++) {
320 void *addr
= base_addr
+ size
* i
;
322 vmsd_desc_field_start(vmsd
, vmdesc_loop
, field
, i
, n_elems
);
323 old_offset
= qemu_ftell_fast(f
);
325 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
326 addr
= *(void **)addr
;
328 if (field
->flags
& VMS_STRUCT
) {
329 vmstate_save_state(f
, field
->vmsd
, addr
, vmdesc_loop
);
331 field
->info
->put(f
, addr
, size
);
334 written_bytes
= qemu_ftell_fast(f
) - old_offset
;
335 vmsd_desc_field_end(vmsd
, vmdesc_loop
, field
, written_bytes
, i
);
337 /* Compressed arrays only care about the first element */
338 if (vmdesc_loop
&& vmsd_can_compress(field
)) {
343 if (field
->flags
& VMS_MUST_EXIST
) {
344 error_report("Output state validation failed: %s/%s",
345 vmsd
->name
, field
->name
);
346 assert(!(field
->flags
& VMS_MUST_EXIST
));
353 json_end_array(vmdesc
);
356 vmstate_subsection_save(f
, vmsd
, opaque
, vmdesc
);
359 static const VMStateDescription
*
360 vmstate_get_subsection(const VMStateDescription
**sub
, char *idstr
)
362 while (sub
&& *sub
&& (*sub
)->needed
) {
363 if (strcmp(idstr
, (*sub
)->name
) == 0) {
371 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
374 trace_vmstate_subsection_load(vmsd
->name
);
376 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
377 char idstr
[256], *idstr_ret
;
379 uint8_t version_id
, len
, size
;
380 const VMStateDescription
*sub_vmsd
;
382 len
= qemu_peek_byte(f
, 1);
383 if (len
< strlen(vmsd
->name
) + 1) {
384 /* subsection name has be be "section_name/a" */
385 trace_vmstate_subsection_load_bad(vmsd
->name
, "(short)", "");
388 size
= qemu_peek_buffer(f
, (uint8_t **)&idstr_ret
, len
, 2);
390 trace_vmstate_subsection_load_bad(vmsd
->name
, "(peek fail)", "");
393 memcpy(idstr
, idstr_ret
, size
);
396 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
397 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(prefix)");
398 /* it doesn't have a valid subsection name */
401 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
402 if (sub_vmsd
== NULL
) {
403 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(lookup)");
406 qemu_file_skip(f
, 1); /* subsection */
407 qemu_file_skip(f
, 1); /* len */
408 qemu_file_skip(f
, len
); /* idstr */
409 version_id
= qemu_get_be32(f
);
411 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
413 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(child)");
418 trace_vmstate_subsection_load_good(vmsd
->name
);
422 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
423 void *opaque
, QJSON
*vmdesc
)
425 const VMStateDescription
**sub
= vmsd
->subsections
;
426 bool subsection_found
= false;
428 while (sub
&& *sub
&& (*sub
)->needed
) {
429 if ((*sub
)->needed(opaque
)) {
430 const VMStateDescription
*vmsd
= *sub
;
434 /* Only create subsection array when we have any */
435 if (!subsection_found
) {
436 json_start_array(vmdesc
, "subsections");
437 subsection_found
= true;
440 json_start_object(vmdesc
, NULL
);
443 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
444 len
= strlen(vmsd
->name
);
445 qemu_put_byte(f
, len
);
446 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
447 qemu_put_be32(f
, vmsd
->version_id
);
448 vmstate_save_state(f
, vmsd
, opaque
, vmdesc
);
451 json_end_object(vmdesc
);
457 if (vmdesc
&& subsection_found
) {
458 json_end_array(vmdesc
);
464 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
467 *v
= qemu_get_byte(f
);
471 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
474 qemu_put_byte(f
, *v
);
477 const VMStateInfo vmstate_info_bool
= {
485 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
492 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
498 const VMStateInfo vmstate_info_int8
= {
506 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
509 qemu_get_sbe16s(f
, v
);
513 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
516 qemu_put_sbe16s(f
, v
);
519 const VMStateInfo vmstate_info_int16
= {
527 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
530 qemu_get_sbe32s(f
, v
);
534 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
537 qemu_put_sbe32s(f
, v
);
540 const VMStateInfo vmstate_info_int32
= {
546 /* 32 bit int. See that the received value is the same than the one
549 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
553 qemu_get_sbe32s(f
, &v2
);
561 const VMStateInfo vmstate_info_int32_equal
= {
562 .name
= "int32 equal",
563 .get
= get_int32_equal
,
567 /* 32 bit int. Check that the received value is non-negative
568 * and less than or equal to the one in the field.
571 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
575 qemu_get_sbe32s(f
, &loaded
);
577 if (loaded
>= 0 && loaded
<= *cur
) {
584 const VMStateInfo vmstate_info_int32_le
= {
592 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
595 qemu_get_sbe64s(f
, v
);
599 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
602 qemu_put_sbe64s(f
, v
);
605 const VMStateInfo vmstate_info_int64
= {
611 /* 8 bit unsigned int */
613 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
620 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
626 const VMStateInfo vmstate_info_uint8
= {
632 /* 16 bit unsigned int */
634 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
637 qemu_get_be16s(f
, v
);
641 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
644 qemu_put_be16s(f
, v
);
647 const VMStateInfo vmstate_info_uint16
= {
653 /* 32 bit unsigned int */
655 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
658 qemu_get_be32s(f
, v
);
662 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
665 qemu_put_be32s(f
, v
);
668 const VMStateInfo vmstate_info_uint32
= {
674 /* 32 bit uint. See that the received value is the same than the one
677 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
681 qemu_get_be32s(f
, &v2
);
689 const VMStateInfo vmstate_info_uint32_equal
= {
690 .name
= "uint32 equal",
691 .get
= get_uint32_equal
,
695 /* 64 bit unsigned int */
697 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
700 qemu_get_be64s(f
, v
);
704 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
707 qemu_put_be64s(f
, v
);
710 const VMStateInfo vmstate_info_uint64
= {
716 /* 64 bit unsigned int. See that the received value is the same than the one
719 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
723 qemu_get_be64s(f
, &v2
);
731 const VMStateInfo vmstate_info_uint64_equal
= {
732 .name
= "int64 equal",
733 .get
= get_uint64_equal
,
737 /* 8 bit int. See that the received value is the same than the one
740 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
752 const VMStateInfo vmstate_info_uint8_equal
= {
753 .name
= "uint8 equal",
754 .get
= get_uint8_equal
,
758 /* 16 bit unsigned int int. See that the received value is the same than the one
761 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
765 qemu_get_be16s(f
, &v2
);
773 const VMStateInfo vmstate_info_uint16_equal
= {
774 .name
= "uint16 equal",
775 .get
= get_uint16_equal
,
781 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
785 *v
= make_float64(qemu_get_be64(f
));
789 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
793 qemu_put_be64(f
, float64_val(*v
));
796 const VMStateInfo vmstate_info_float64
= {
802 /* CPU_DoubleU type */
804 static int get_cpudouble(QEMUFile
*f
, void *pv
, size_t size
)
807 qemu_get_be32s(f
, &v
->l
.upper
);
808 qemu_get_be32s(f
, &v
->l
.lower
);
812 static void put_cpudouble(QEMUFile
*f
, void *pv
, size_t size
)
815 qemu_put_be32s(f
, &v
->l
.upper
);
816 qemu_put_be32s(f
, &v
->l
.lower
);
819 const VMStateInfo vmstate_info_cpudouble
= {
820 .name
= "CPU_Double_U",
821 .get
= get_cpudouble
,
822 .put
= put_cpudouble
,
825 /* uint8_t buffers */
827 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
830 qemu_get_buffer(f
, v
, size
);
834 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
837 qemu_put_buffer(f
, v
, size
);
840 const VMStateInfo vmstate_info_buffer
= {
846 /* unused buffers: space that was used for some fields that are
847 not useful anymore */
849 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
855 block_len
= MIN(sizeof(buf
), size
);
857 qemu_get_buffer(f
, buf
, block_len
);
862 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
864 static const uint8_t buf
[1024];
868 block_len
= MIN(sizeof(buf
), size
);
870 qemu_put_buffer(f
, buf
, block_len
);
874 const VMStateInfo vmstate_info_unused_buffer
= {
875 .name
= "unused_buffer",
876 .get
= get_unused_buffer
,
877 .put
= put_unused_buffer
,
880 /* bitmaps (as defined by bitmap.h). Note that size here is the size
881 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
882 * bit words with the bits in big endian order. The in-memory format
883 * is an array of 'unsigned long', which may be either 32 or 64 bits.
885 /* This is the number of 64 bit words sent over the wire */
886 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
887 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
889 unsigned long *bmp
= pv
;
891 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
892 uint64_t w
= qemu_get_be64(f
);
894 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
895 bmp
[idx
++] = w
>> 32;
901 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
903 unsigned long *bmp
= pv
;
905 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
906 uint64_t w
= bmp
[idx
++];
907 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
908 w
|= ((uint64_t)bmp
[idx
++]) << 32;
914 const VMStateInfo vmstate_info_bitmap
= {