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
) {
67 if (vmsd
->load_state_old
&&
68 version_id
>= vmsd
->minimum_version_id_old
) {
69 return vmsd
->load_state_old(f
, opaque
, version_id
);
74 int ret
= vmsd
->pre_load(opaque
);
80 if ((field
->field_exists
&&
81 field
->field_exists(opaque
, version_id
)) ||
82 (!field
->field_exists
&&
83 field
->version_id
<= version_id
)) {
84 void *base_addr
= vmstate_base_addr(opaque
, field
);
85 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
86 int size
= vmstate_size(opaque
, field
);
88 for (i
= 0; i
< n_elems
; i
++) {
89 void *addr
= base_addr
+ size
* i
;
91 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
92 addr
= *(void **)addr
;
94 if (field
->flags
& VMS_STRUCT
) {
95 ret
= vmstate_load_state(f
, field
->vmsd
, addr
,
96 field
->vmsd
->version_id
);
98 ret
= field
->info
->get(f
, addr
, size
);
102 ret
= qemu_file_get_error(f
);
105 qemu_file_set_error(f
, ret
);
106 trace_vmstate_load_field_error(field
->name
, ret
);
110 } else if (field
->flags
& VMS_MUST_EXIST
) {
111 fprintf(stderr
, "Input validation failed: %s/%s\n",
112 vmsd
->name
, field
->name
);
117 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
121 if (vmsd
->post_load
) {
122 return vmsd
->post_load(opaque
, version_id
);
127 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
130 VMStateField
*field
= vmsd
->fields
;
132 if (vmsd
->pre_save
) {
133 vmsd
->pre_save(opaque
);
135 while (field
->name
) {
136 if (!field
->field_exists
||
137 field
->field_exists(opaque
, vmsd
->version_id
)) {
138 void *base_addr
= vmstate_base_addr(opaque
, field
);
139 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
140 int size
= vmstate_size(opaque
, field
);
142 for (i
= 0; i
< n_elems
; i
++) {
143 void *addr
= base_addr
+ size
* i
;
145 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
146 addr
= *(void **)addr
;
148 if (field
->flags
& VMS_STRUCT
) {
149 vmstate_save_state(f
, field
->vmsd
, addr
);
151 field
->info
->put(f
, addr
, size
);
155 if (field
->flags
& VMS_MUST_EXIST
) {
156 fprintf(stderr
, "Output state validation failed: %s/%s\n",
157 vmsd
->name
, field
->name
);
158 assert(!(field
->flags
& VMS_MUST_EXIST
));
163 vmstate_subsection_save(f
, vmsd
, opaque
);
166 static const VMStateDescription
*
167 vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
169 while (sub
&& sub
->needed
) {
170 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
178 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
181 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
184 uint8_t version_id
, len
, size
;
185 const VMStateDescription
*sub_vmsd
;
187 len
= qemu_peek_byte(f
, 1);
188 if (len
< strlen(vmsd
->name
) + 1) {
189 /* subsection name has be be "section_name/a" */
192 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
198 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
199 /* it don't have a valid subsection name */
202 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
203 if (sub_vmsd
== NULL
) {
206 qemu_file_skip(f
, 1); /* subsection */
207 qemu_file_skip(f
, 1); /* len */
208 qemu_file_skip(f
, len
); /* idstr */
209 version_id
= qemu_get_be32(f
);
211 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
219 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
222 const VMStateSubsection
*sub
= vmsd
->subsections
;
224 while (sub
&& sub
->needed
) {
225 if (sub
->needed(opaque
)) {
226 const VMStateDescription
*vmsd
= sub
->vmsd
;
229 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
230 len
= strlen(vmsd
->name
);
231 qemu_put_byte(f
, len
);
232 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
233 qemu_put_be32(f
, vmsd
->version_id
);
234 vmstate_save_state(f
, vmsd
, opaque
);
242 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
245 *v
= qemu_get_byte(f
);
249 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
252 qemu_put_byte(f
, *v
);
255 const VMStateInfo vmstate_info_bool
= {
263 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
270 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
276 const VMStateInfo vmstate_info_int8
= {
284 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
287 qemu_get_sbe16s(f
, v
);
291 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
294 qemu_put_sbe16s(f
, v
);
297 const VMStateInfo vmstate_info_int16
= {
305 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
308 qemu_get_sbe32s(f
, v
);
312 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
315 qemu_put_sbe32s(f
, v
);
318 const VMStateInfo vmstate_info_int32
= {
324 /* 32 bit int. See that the received value is the same than the one
327 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
331 qemu_get_sbe32s(f
, &v2
);
339 const VMStateInfo vmstate_info_int32_equal
= {
340 .name
= "int32 equal",
341 .get
= get_int32_equal
,
345 /* 32 bit int. Check that the received value is non-negative
346 * and less than or equal to the one in the field.
349 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
353 qemu_get_sbe32s(f
, &loaded
);
355 if (loaded
>= 0 && loaded
<= *cur
) {
362 const VMStateInfo vmstate_info_int32_le
= {
370 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
373 qemu_get_sbe64s(f
, v
);
377 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
380 qemu_put_sbe64s(f
, v
);
383 const VMStateInfo vmstate_info_int64
= {
389 /* 8 bit unsigned int */
391 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
398 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
404 const VMStateInfo vmstate_info_uint8
= {
410 /* 16 bit unsigned int */
412 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
415 qemu_get_be16s(f
, v
);
419 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
422 qemu_put_be16s(f
, v
);
425 const VMStateInfo vmstate_info_uint16
= {
431 /* 32 bit unsigned int */
433 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
436 qemu_get_be32s(f
, v
);
440 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
443 qemu_put_be32s(f
, v
);
446 const VMStateInfo vmstate_info_uint32
= {
452 /* 32 bit uint. See that the received value is the same than the one
455 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
459 qemu_get_be32s(f
, &v2
);
467 const VMStateInfo vmstate_info_uint32_equal
= {
468 .name
= "uint32 equal",
469 .get
= get_uint32_equal
,
473 /* 64 bit unsigned int */
475 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
478 qemu_get_be64s(f
, v
);
482 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
485 qemu_put_be64s(f
, v
);
488 const VMStateInfo vmstate_info_uint64
= {
494 /* 64 bit unsigned int. See that the received value is the same than the one
497 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
501 qemu_get_be64s(f
, &v2
);
509 const VMStateInfo vmstate_info_uint64_equal
= {
510 .name
= "int64 equal",
511 .get
= get_uint64_equal
,
515 /* 8 bit int. See that the received value is the same than the one
518 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
530 const VMStateInfo vmstate_info_uint8_equal
= {
531 .name
= "uint8 equal",
532 .get
= get_uint8_equal
,
536 /* 16 bit unsigned int int. See that the received value is the same than the one
539 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
543 qemu_get_be16s(f
, &v2
);
551 const VMStateInfo vmstate_info_uint16_equal
= {
552 .name
= "uint16 equal",
553 .get
= get_uint16_equal
,
559 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
563 *v
= make_float64(qemu_get_be64(f
));
567 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
571 qemu_put_be64(f
, float64_val(*v
));
574 const VMStateInfo vmstate_info_float64
= {
580 /* uint8_t buffers */
582 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
585 qemu_get_buffer(f
, v
, size
);
589 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
592 qemu_put_buffer(f
, v
, size
);
595 const VMStateInfo vmstate_info_buffer
= {
601 /* unused buffers: space that was used for some fields that are
602 not useful anymore */
604 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
610 block_len
= MIN(sizeof(buf
), size
);
612 qemu_get_buffer(f
, buf
, block_len
);
617 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
619 static const uint8_t buf
[1024];
623 block_len
= MIN(sizeof(buf
), size
);
625 qemu_put_buffer(f
, buf
, block_len
);
629 const VMStateInfo vmstate_info_unused_buffer
= {
630 .name
= "unused_buffer",
631 .get
= get_unused_buffer
,
632 .put
= put_unused_buffer
,
635 /* bitmaps (as defined by bitmap.h). Note that size here is the size
636 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
637 * bit words with the bits in big endian order. The in-memory format
638 * is an array of 'unsigned long', which may be either 32 or 64 bits.
640 /* This is the number of 64 bit words sent over the wire */
641 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
642 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
644 unsigned long *bmp
= pv
;
646 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
647 uint64_t w
= qemu_get_be64(f
);
649 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
650 bmp
[idx
++] = w
>> 32;
656 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
658 unsigned long *bmp
= pv
;
660 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
661 uint64_t w
= bmp
[idx
++];
662 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
663 w
|= ((uint64_t)bmp
[idx
++]) << 32;
669 const VMStateInfo vmstate_info_bitmap
= {