1 #include "qemu-common.h"
2 #include "migration/migration.h"
3 #include "migration/qemu-file.h"
4 #include "migration/vmstate.h"
5 #include "qemu/bitops.h"
8 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
10 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
13 static int vmstate_n_elems(void *opaque
, VMStateField
*field
)
17 if (field
->flags
& VMS_ARRAY
) {
19 } else if (field
->flags
& VMS_VARRAY_INT32
) {
20 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
21 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
22 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
23 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
24 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
25 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
26 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
32 static int vmstate_size(void *opaque
, VMStateField
*field
)
34 int size
= field
->size
;
36 if (field
->flags
& VMS_VBUFFER
) {
37 size
= *(int32_t *)(opaque
+field
->size_offset
);
38 if (field
->flags
& VMS_MULTIPLY
) {
46 static void *vmstate_base_addr(void *opaque
, VMStateField
*field
)
48 void *base_addr
= opaque
+ field
->offset
;
50 if (field
->flags
& VMS_POINTER
) {
51 base_addr
= *(void **)base_addr
+ field
->start
;
57 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
58 void *opaque
, int version_id
)
60 VMStateField
*field
= vmsd
->fields
;
63 if (version_id
> vmsd
->version_id
) {
66 if (version_id
< vmsd
->minimum_version_id_old
) {
69 if (version_id
< vmsd
->minimum_version_id
) {
70 return vmsd
->load_state_old(f
, opaque
, version_id
);
73 int ret
= vmsd
->pre_load(opaque
);
79 if ((field
->field_exists
&&
80 field
->field_exists(opaque
, version_id
)) ||
81 (!field
->field_exists
&&
82 field
->version_id
<= version_id
)) {
83 void *base_addr
= vmstate_base_addr(opaque
, field
);
84 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
85 int size
= vmstate_size(opaque
, field
);
87 for (i
= 0; i
< n_elems
; i
++) {
88 void *addr
= base_addr
+ size
* i
;
90 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
91 addr
= *(void **)addr
;
93 if (field
->flags
& VMS_STRUCT
) {
94 ret
= vmstate_load_state(f
, field
->vmsd
, addr
,
95 field
->vmsd
->version_id
);
97 ret
= field
->info
->get(f
, addr
, size
);
101 trace_vmstate_load_field_error(field
->name
, ret
);
105 } else if (field
->flags
& VMS_MUST_EXIST
) {
106 fprintf(stderr
, "Input validation failed: %s/%s\n",
107 vmsd
->name
, field
->name
);
112 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
116 if (vmsd
->post_load
) {
117 return vmsd
->post_load(opaque
, version_id
);
122 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
125 VMStateField
*field
= vmsd
->fields
;
127 if (vmsd
->pre_save
) {
128 vmsd
->pre_save(opaque
);
130 while (field
->name
) {
131 if (!field
->field_exists
||
132 field
->field_exists(opaque
, vmsd
->version_id
)) {
133 void *base_addr
= vmstate_base_addr(opaque
, field
);
134 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
135 int size
= vmstate_size(opaque
, field
);
137 for (i
= 0; i
< n_elems
; i
++) {
138 void *addr
= base_addr
+ size
* i
;
140 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
141 addr
= *(void **)addr
;
143 if (field
->flags
& VMS_STRUCT
) {
144 vmstate_save_state(f
, field
->vmsd
, addr
);
146 field
->info
->put(f
, addr
, size
);
150 if (field
->flags
& VMS_MUST_EXIST
) {
151 fprintf(stderr
, "Output state validation failed: %s/%s\n",
152 vmsd
->name
, field
->name
);
153 assert(!(field
->flags
& VMS_MUST_EXIST
));
158 vmstate_subsection_save(f
, vmsd
, opaque
);
161 static const VMStateDescription
*
162 vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
164 while (sub
&& sub
->needed
) {
165 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
173 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
176 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
179 uint8_t version_id
, len
, size
;
180 const VMStateDescription
*sub_vmsd
;
182 len
= qemu_peek_byte(f
, 1);
183 if (len
< strlen(vmsd
->name
) + 1) {
184 /* subsection name has be be "section_name/a" */
187 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
193 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
194 /* it don't have a valid subsection name */
197 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
198 if (sub_vmsd
== NULL
) {
201 qemu_file_skip(f
, 1); /* subsection */
202 qemu_file_skip(f
, 1); /* len */
203 qemu_file_skip(f
, len
); /* idstr */
204 version_id
= qemu_get_be32(f
);
206 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
214 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
217 const VMStateSubsection
*sub
= vmsd
->subsections
;
219 while (sub
&& sub
->needed
) {
220 if (sub
->needed(opaque
)) {
221 const VMStateDescription
*vmsd
= sub
->vmsd
;
224 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
225 len
= strlen(vmsd
->name
);
226 qemu_put_byte(f
, len
);
227 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
228 qemu_put_be32(f
, vmsd
->version_id
);
229 vmstate_save_state(f
, vmsd
, opaque
);
237 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
240 *v
= qemu_get_byte(f
);
244 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
247 qemu_put_byte(f
, *v
);
250 const VMStateInfo vmstate_info_bool
= {
258 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
265 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
271 const VMStateInfo vmstate_info_int8
= {
279 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
282 qemu_get_sbe16s(f
, v
);
286 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
289 qemu_put_sbe16s(f
, v
);
292 const VMStateInfo vmstate_info_int16
= {
300 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
303 qemu_get_sbe32s(f
, v
);
307 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
310 qemu_put_sbe32s(f
, v
);
313 const VMStateInfo vmstate_info_int32
= {
319 /* 32 bit int. See that the received value is the same than the one
322 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
326 qemu_get_sbe32s(f
, &v2
);
334 const VMStateInfo vmstate_info_int32_equal
= {
335 .name
= "int32 equal",
336 .get
= get_int32_equal
,
340 /* 32 bit int. Check that the received value is less than or equal to
341 the one in the field */
343 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
347 qemu_get_sbe32s(f
, &loaded
);
349 if (loaded
<= *cur
) {
356 const VMStateInfo vmstate_info_int32_le
= {
364 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
367 qemu_get_sbe64s(f
, v
);
371 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
374 qemu_put_sbe64s(f
, v
);
377 const VMStateInfo vmstate_info_int64
= {
383 /* 8 bit unsigned int */
385 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
392 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
398 const VMStateInfo vmstate_info_uint8
= {
404 /* 16 bit unsigned int */
406 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
409 qemu_get_be16s(f
, v
);
413 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
416 qemu_put_be16s(f
, v
);
419 const VMStateInfo vmstate_info_uint16
= {
425 /* 32 bit unsigned int */
427 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
430 qemu_get_be32s(f
, v
);
434 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
437 qemu_put_be32s(f
, v
);
440 const VMStateInfo vmstate_info_uint32
= {
446 /* 32 bit uint. See that the received value is the same than the one
449 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
453 qemu_get_be32s(f
, &v2
);
461 const VMStateInfo vmstate_info_uint32_equal
= {
462 .name
= "uint32 equal",
463 .get
= get_uint32_equal
,
467 /* 64 bit unsigned int */
469 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
472 qemu_get_be64s(f
, v
);
476 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
479 qemu_put_be64s(f
, v
);
482 const VMStateInfo vmstate_info_uint64
= {
488 /* 64 bit unsigned int. See that the received value is the same than the one
491 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
495 qemu_get_be64s(f
, &v2
);
503 const VMStateInfo vmstate_info_uint64_equal
= {
504 .name
= "int64 equal",
505 .get
= get_uint64_equal
,
509 /* 8 bit int. See that the received value is the same than the one
512 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
524 const VMStateInfo vmstate_info_uint8_equal
= {
525 .name
= "uint8 equal",
526 .get
= get_uint8_equal
,
530 /* 16 bit unsigned int int. See that the received value is the same than the one
533 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
537 qemu_get_be16s(f
, &v2
);
545 const VMStateInfo vmstate_info_uint16_equal
= {
546 .name
= "uint16 equal",
547 .get
= get_uint16_equal
,
553 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
557 *v
= make_float64(qemu_get_be64(f
));
561 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
565 qemu_put_be64(f
, float64_val(*v
));
568 const VMStateInfo vmstate_info_float64
= {
574 /* uint8_t buffers */
576 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
579 qemu_get_buffer(f
, v
, size
);
583 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
586 qemu_put_buffer(f
, v
, size
);
589 const VMStateInfo vmstate_info_buffer
= {
595 /* unused buffers: space that was used for some fields that are
596 not useful anymore */
598 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
604 block_len
= MIN(sizeof(buf
), size
);
606 qemu_get_buffer(f
, buf
, block_len
);
611 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
613 static const uint8_t buf
[1024];
617 block_len
= MIN(sizeof(buf
), size
);
619 qemu_put_buffer(f
, buf
, block_len
);
623 const VMStateInfo vmstate_info_unused_buffer
= {
624 .name
= "unused_buffer",
625 .get
= get_unused_buffer
,
626 .put
= put_unused_buffer
,
629 /* bitmaps (as defined by bitmap.h). Note that size here is the size
630 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
631 * bit words with the bits in big endian order. The in-memory format
632 * is an array of 'unsigned long', which may be either 32 or 64 bits.
634 /* This is the number of 64 bit words sent over the wire */
635 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
636 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
638 unsigned long *bmp
= pv
;
640 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
641 uint64_t w
= qemu_get_be64(f
);
643 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
644 bmp
[idx
++] = w
>> 32;
650 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
652 unsigned long *bmp
= pv
;
654 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
655 uint64_t w
= bmp
[idx
++];
656 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
657 w
|= ((uint64_t)bmp
[idx
++]) << 32;
663 const VMStateInfo vmstate_info_bitmap
= {