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
, bool alloc
)
48 void *base_addr
= opaque
+ field
->offset
;
50 if (field
->flags
& VMS_POINTER
) {
51 if (alloc
&& (field
->flags
& VMS_ALLOC
)) {
53 if (field
->flags
& VMS_VBUFFER
) {
54 size
= vmstate_size(opaque
, field
);
56 int n_elems
= vmstate_n_elems(opaque
, field
);
58 size
= n_elems
* field
->size
;
62 *((void **)base_addr
+ field
->start
) = g_malloc(size
);
65 base_addr
= *(void **)base_addr
+ field
->start
;
71 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
72 void *opaque
, int version_id
)
74 VMStateField
*field
= vmsd
->fields
;
77 if (version_id
> vmsd
->version_id
) {
80 if (version_id
< vmsd
->minimum_version_id
) {
81 if (vmsd
->load_state_old
&&
82 version_id
>= vmsd
->minimum_version_id_old
) {
83 return vmsd
->load_state_old(f
, opaque
, version_id
);
88 int ret
= vmsd
->pre_load(opaque
);
94 if ((field
->field_exists
&&
95 field
->field_exists(opaque
, version_id
)) ||
96 (!field
->field_exists
&&
97 field
->version_id
<= version_id
)) {
98 void *base_addr
= vmstate_base_addr(opaque
, field
, true);
99 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
100 int size
= vmstate_size(opaque
, field
);
102 for (i
= 0; i
< n_elems
; i
++) {
103 void *addr
= base_addr
+ size
* i
;
105 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
106 addr
= *(void **)addr
;
108 if (field
->flags
& VMS_STRUCT
) {
109 ret
= vmstate_load_state(f
, field
->vmsd
, addr
,
110 field
->vmsd
->version_id
);
112 ret
= field
->info
->get(f
, addr
, size
);
116 ret
= qemu_file_get_error(f
);
119 qemu_file_set_error(f
, ret
);
120 trace_vmstate_load_field_error(field
->name
, ret
);
124 } else if (field
->flags
& VMS_MUST_EXIST
) {
125 fprintf(stderr
, "Input validation failed: %s/%s\n",
126 vmsd
->name
, field
->name
);
131 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
135 if (vmsd
->post_load
) {
136 return vmsd
->post_load(opaque
, version_id
);
141 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
144 VMStateField
*field
= vmsd
->fields
;
146 if (vmsd
->pre_save
) {
147 vmsd
->pre_save(opaque
);
149 while (field
->name
) {
150 if (!field
->field_exists
||
151 field
->field_exists(opaque
, vmsd
->version_id
)) {
152 void *base_addr
= vmstate_base_addr(opaque
, field
, false);
153 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
154 int size
= vmstate_size(opaque
, field
);
156 for (i
= 0; i
< n_elems
; i
++) {
157 void *addr
= base_addr
+ size
* i
;
159 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
160 addr
= *(void **)addr
;
162 if (field
->flags
& VMS_STRUCT
) {
163 vmstate_save_state(f
, field
->vmsd
, addr
);
165 field
->info
->put(f
, addr
, size
);
169 if (field
->flags
& VMS_MUST_EXIST
) {
170 fprintf(stderr
, "Output state validation failed: %s/%s\n",
171 vmsd
->name
, field
->name
);
172 assert(!(field
->flags
& VMS_MUST_EXIST
));
177 vmstate_subsection_save(f
, vmsd
, opaque
);
180 static const VMStateDescription
*
181 vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
183 while (sub
&& sub
->needed
) {
184 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
192 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
195 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
198 uint8_t version_id
, len
, size
;
199 const VMStateDescription
*sub_vmsd
;
201 len
= qemu_peek_byte(f
, 1);
202 if (len
< strlen(vmsd
->name
) + 1) {
203 /* subsection name has be be "section_name/a" */
206 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
212 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
213 /* it don't have a valid subsection name */
216 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
217 if (sub_vmsd
== NULL
) {
220 qemu_file_skip(f
, 1); /* subsection */
221 qemu_file_skip(f
, 1); /* len */
222 qemu_file_skip(f
, len
); /* idstr */
223 version_id
= qemu_get_be32(f
);
225 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
233 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
236 const VMStateSubsection
*sub
= vmsd
->subsections
;
238 while (sub
&& sub
->needed
) {
239 if (sub
->needed(opaque
)) {
240 const VMStateDescription
*vmsd
= sub
->vmsd
;
243 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
244 len
= strlen(vmsd
->name
);
245 qemu_put_byte(f
, len
);
246 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
247 qemu_put_be32(f
, vmsd
->version_id
);
248 vmstate_save_state(f
, vmsd
, opaque
);
256 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
259 *v
= qemu_get_byte(f
);
263 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
266 qemu_put_byte(f
, *v
);
269 const VMStateInfo vmstate_info_bool
= {
277 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
284 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
290 const VMStateInfo vmstate_info_int8
= {
298 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
301 qemu_get_sbe16s(f
, v
);
305 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
308 qemu_put_sbe16s(f
, v
);
311 const VMStateInfo vmstate_info_int16
= {
319 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
322 qemu_get_sbe32s(f
, v
);
326 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
329 qemu_put_sbe32s(f
, v
);
332 const VMStateInfo vmstate_info_int32
= {
338 /* 32 bit int. See that the received value is the same than the one
341 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
345 qemu_get_sbe32s(f
, &v2
);
353 const VMStateInfo vmstate_info_int32_equal
= {
354 .name
= "int32 equal",
355 .get
= get_int32_equal
,
359 /* 32 bit int. Check that the received value is non-negative
360 * and less than or equal to the one in the field.
363 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
367 qemu_get_sbe32s(f
, &loaded
);
369 if (loaded
>= 0 && loaded
<= *cur
) {
376 const VMStateInfo vmstate_info_int32_le
= {
384 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
387 qemu_get_sbe64s(f
, v
);
391 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
394 qemu_put_sbe64s(f
, v
);
397 const VMStateInfo vmstate_info_int64
= {
403 /* 8 bit unsigned int */
405 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
412 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
418 const VMStateInfo vmstate_info_uint8
= {
424 /* 16 bit unsigned int */
426 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
429 qemu_get_be16s(f
, v
);
433 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
436 qemu_put_be16s(f
, v
);
439 const VMStateInfo vmstate_info_uint16
= {
445 /* 32 bit unsigned int */
447 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
450 qemu_get_be32s(f
, v
);
454 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
457 qemu_put_be32s(f
, v
);
460 const VMStateInfo vmstate_info_uint32
= {
466 /* 32 bit uint. See that the received value is the same than the one
469 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
473 qemu_get_be32s(f
, &v2
);
481 const VMStateInfo vmstate_info_uint32_equal
= {
482 .name
= "uint32 equal",
483 .get
= get_uint32_equal
,
487 /* 64 bit unsigned int */
489 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
492 qemu_get_be64s(f
, v
);
496 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
499 qemu_put_be64s(f
, v
);
502 const VMStateInfo vmstate_info_uint64
= {
508 /* 64 bit unsigned int. See that the received value is the same than the one
511 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
515 qemu_get_be64s(f
, &v2
);
523 const VMStateInfo vmstate_info_uint64_equal
= {
524 .name
= "int64 equal",
525 .get
= get_uint64_equal
,
529 /* 8 bit int. See that the received value is the same than the one
532 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
544 const VMStateInfo vmstate_info_uint8_equal
= {
545 .name
= "uint8 equal",
546 .get
= get_uint8_equal
,
550 /* 16 bit unsigned int int. See that the received value is the same than the one
553 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
557 qemu_get_be16s(f
, &v2
);
565 const VMStateInfo vmstate_info_uint16_equal
= {
566 .name
= "uint16 equal",
567 .get
= get_uint16_equal
,
573 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
577 *v
= make_float64(qemu_get_be64(f
));
581 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
585 qemu_put_be64(f
, float64_val(*v
));
588 const VMStateInfo vmstate_info_float64
= {
594 /* uint8_t buffers */
596 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
599 qemu_get_buffer(f
, v
, size
);
603 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
606 qemu_put_buffer(f
, v
, size
);
609 const VMStateInfo vmstate_info_buffer
= {
615 /* unused buffers: space that was used for some fields that are
616 not useful anymore */
618 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
624 block_len
= MIN(sizeof(buf
), size
);
626 qemu_get_buffer(f
, buf
, block_len
);
631 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
633 static const uint8_t buf
[1024];
637 block_len
= MIN(sizeof(buf
), size
);
639 qemu_put_buffer(f
, buf
, block_len
);
643 const VMStateInfo vmstate_info_unused_buffer
= {
644 .name
= "unused_buffer",
645 .get
= get_unused_buffer
,
646 .put
= put_unused_buffer
,
649 /* bitmaps (as defined by bitmap.h). Note that size here is the size
650 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
651 * bit words with the bits in big endian order. The in-memory format
652 * is an array of 'unsigned long', which may be either 32 or 64 bits.
654 /* This is the number of 64 bit words sent over the wire */
655 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
656 static int get_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
= qemu_get_be64(f
);
663 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
664 bmp
[idx
++] = w
>> 32;
670 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
672 unsigned long *bmp
= pv
;
674 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
675 uint64_t w
= bmp
[idx
++];
676 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
677 w
|= ((uint64_t)bmp
[idx
++]) << 32;
683 const VMStateInfo vmstate_info_bitmap
= {