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"
7 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
9 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
12 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
13 void *opaque
, int version_id
)
15 VMStateField
*field
= vmsd
->fields
;
18 if (version_id
> vmsd
->version_id
) {
21 if (version_id
< vmsd
->minimum_version_id_old
) {
24 if (version_id
< vmsd
->minimum_version_id
) {
25 return vmsd
->load_state_old(f
, opaque
, version_id
);
28 int ret
= vmsd
->pre_load(opaque
);
34 if ((field
->field_exists
&&
35 field
->field_exists(opaque
, version_id
)) ||
36 (!field
->field_exists
&&
37 field
->version_id
<= version_id
)) {
38 void *base_addr
= opaque
+ field
->offset
;
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
) {
48 if (field
->flags
& VMS_ARRAY
) {
50 } else if (field
->flags
& VMS_VARRAY_INT32
) {
51 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
52 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
53 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
54 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
55 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
56 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
57 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
59 if (field
->flags
& VMS_POINTER
) {
60 base_addr
= *(void **)base_addr
+ field
->start
;
62 for (i
= 0; i
< n_elems
; i
++) {
63 void *addr
= base_addr
+ size
* i
;
65 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
66 addr
= *(void **)addr
;
68 if (field
->flags
& VMS_STRUCT
) {
69 ret
= vmstate_load_state(f
, field
->vmsd
, addr
,
70 field
->vmsd
->version_id
);
72 ret
= field
->info
->get(f
, addr
, size
);
82 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
86 if (vmsd
->post_load
) {
87 return vmsd
->post_load(opaque
, version_id
);
92 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
95 VMStateField
*field
= vmsd
->fields
;
98 vmsd
->pre_save(opaque
);
100 while (field
->name
) {
101 if (!field
->field_exists
||
102 field
->field_exists(opaque
, vmsd
->version_id
)) {
103 void *base_addr
= opaque
+ field
->offset
;
105 int size
= field
->size
;
107 if (field
->flags
& VMS_VBUFFER
) {
108 size
= *(int32_t *)(opaque
+field
->size_offset
);
109 if (field
->flags
& VMS_MULTIPLY
) {
113 if (field
->flags
& VMS_ARRAY
) {
114 n_elems
= field
->num
;
115 } else if (field
->flags
& VMS_VARRAY_INT32
) {
116 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
117 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
118 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
119 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
120 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
121 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
122 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
124 if (field
->flags
& VMS_POINTER
) {
125 base_addr
= *(void **)base_addr
+ field
->start
;
127 for (i
= 0; i
< n_elems
; i
++) {
128 void *addr
= base_addr
+ size
* i
;
130 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
131 addr
= *(void **)addr
;
133 if (field
->flags
& VMS_STRUCT
) {
134 vmstate_save_state(f
, field
->vmsd
, addr
);
136 field
->info
->put(f
, addr
, size
);
142 vmstate_subsection_save(f
, vmsd
, opaque
);
145 static const VMStateDescription
*
146 vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
148 while (sub
&& sub
->needed
) {
149 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
157 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
160 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
163 uint8_t version_id
, len
, size
;
164 const VMStateDescription
*sub_vmsd
;
166 len
= qemu_peek_byte(f
, 1);
167 if (len
< strlen(vmsd
->name
) + 1) {
168 /* subsection name has be be "section_name/a" */
171 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
177 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
178 /* it don't have a valid subsection name */
181 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
182 if (sub_vmsd
== NULL
) {
185 qemu_file_skip(f
, 1); /* subsection */
186 qemu_file_skip(f
, 1); /* len */
187 qemu_file_skip(f
, len
); /* idstr */
188 version_id
= qemu_get_be32(f
);
190 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
198 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
201 const VMStateSubsection
*sub
= vmsd
->subsections
;
203 while (sub
&& sub
->needed
) {
204 if (sub
->needed(opaque
)) {
205 const VMStateDescription
*vmsd
= sub
->vmsd
;
208 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
209 len
= strlen(vmsd
->name
);
210 qemu_put_byte(f
, len
);
211 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
212 qemu_put_be32(f
, vmsd
->version_id
);
213 vmstate_save_state(f
, vmsd
, opaque
);
221 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
224 *v
= qemu_get_byte(f
);
228 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
231 qemu_put_byte(f
, *v
);
234 const VMStateInfo vmstate_info_bool
= {
242 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
249 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
255 const VMStateInfo vmstate_info_int8
= {
263 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
266 qemu_get_sbe16s(f
, v
);
270 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
273 qemu_put_sbe16s(f
, v
);
276 const VMStateInfo vmstate_info_int16
= {
284 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
287 qemu_get_sbe32s(f
, v
);
291 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
294 qemu_put_sbe32s(f
, v
);
297 const VMStateInfo vmstate_info_int32
= {
303 /* 32 bit int. See that the received value is the same than the one
306 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
310 qemu_get_sbe32s(f
, &v2
);
318 const VMStateInfo vmstate_info_int32_equal
= {
319 .name
= "int32 equal",
320 .get
= get_int32_equal
,
324 /* 32 bit int. See that the received value is the less or the same
325 than the one in the field */
327 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
331 qemu_get_sbe32s(f
, &new);
339 const VMStateInfo vmstate_info_int32_le
= {
340 .name
= "int32 equal",
347 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
350 qemu_get_sbe64s(f
, v
);
354 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
357 qemu_put_sbe64s(f
, v
);
360 const VMStateInfo vmstate_info_int64
= {
366 /* 8 bit unsigned int */
368 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
375 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
381 const VMStateInfo vmstate_info_uint8
= {
387 /* 16 bit unsigned int */
389 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
392 qemu_get_be16s(f
, v
);
396 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
399 qemu_put_be16s(f
, v
);
402 const VMStateInfo vmstate_info_uint16
= {
408 /* 32 bit unsigned int */
410 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
413 qemu_get_be32s(f
, v
);
417 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
420 qemu_put_be32s(f
, v
);
423 const VMStateInfo vmstate_info_uint32
= {
429 /* 32 bit uint. See that the received value is the same than the one
432 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
436 qemu_get_be32s(f
, &v2
);
444 const VMStateInfo vmstate_info_uint32_equal
= {
445 .name
= "uint32 equal",
446 .get
= get_uint32_equal
,
450 /* 64 bit unsigned int */
452 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
455 qemu_get_be64s(f
, v
);
459 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
462 qemu_put_be64s(f
, v
);
465 const VMStateInfo vmstate_info_uint64
= {
471 /* 64 bit unsigned int. See that the received value is the same than the one
474 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
478 qemu_get_be64s(f
, &v2
);
486 const VMStateInfo vmstate_info_uint64_equal
= {
487 .name
= "int64 equal",
488 .get
= get_uint64_equal
,
492 /* 8 bit int. See that the received value is the same than the one
495 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
507 const VMStateInfo vmstate_info_uint8_equal
= {
508 .name
= "uint8 equal",
509 .get
= get_uint8_equal
,
513 /* 16 bit unsigned int int. See that the received value is the same than the one
516 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
520 qemu_get_be16s(f
, &v2
);
528 const VMStateInfo vmstate_info_uint16_equal
= {
529 .name
= "uint16 equal",
530 .get
= get_uint16_equal
,
536 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
540 *v
= make_float64(qemu_get_be64(f
));
544 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
548 qemu_put_be64(f
, float64_val(*v
));
551 const VMStateInfo vmstate_info_float64
= {
557 /* uint8_t buffers */
559 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
562 qemu_get_buffer(f
, v
, size
);
566 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
569 qemu_put_buffer(f
, v
, size
);
572 const VMStateInfo vmstate_info_buffer
= {
578 /* unused buffers: space that was used for some fields that are
579 not useful anymore */
581 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
587 block_len
= MIN(sizeof(buf
), size
);
589 qemu_get_buffer(f
, buf
, block_len
);
594 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
596 static const uint8_t buf
[1024];
600 block_len
= MIN(sizeof(buf
), size
);
602 qemu_put_buffer(f
, buf
, block_len
);
606 const VMStateInfo vmstate_info_unused_buffer
= {
607 .name
= "unused_buffer",
608 .get
= get_unused_buffer
,
609 .put
= put_unused_buffer
,
612 /* bitmaps (as defined by bitmap.h). Note that size here is the size
613 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
614 * bit words with the bits in big endian order. The in-memory format
615 * is an array of 'unsigned long', which may be either 32 or 64 bits.
617 /* This is the number of 64 bit words sent over the wire */
618 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
619 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
621 unsigned long *bmp
= pv
;
623 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
624 uint64_t w
= qemu_get_be64(f
);
626 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
627 bmp
[idx
++] = w
>> 32;
633 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
635 unsigned long *bmp
= pv
;
637 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
638 uint64_t w
= bmp
[idx
++];
639 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
640 w
|= ((uint64_t)bmp
[idx
++]) << 32;
646 const VMStateInfo vmstate_info_bitmap
= {