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"
8 #include "qemu/queue.h"
10 #include "migration/qjson.h"
12 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
13 void *opaque
, QJSON
*vmdesc
);
14 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
17 static int vmstate_n_elems(void *opaque
, VMStateField
*field
)
21 if (field
->flags
& VMS_ARRAY
) {
23 } else if (field
->flags
& VMS_VARRAY_INT32
) {
24 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
25 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
26 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
27 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
28 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
29 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
30 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
33 if (field
->flags
& VMS_MULTIPLY_ELEMENTS
) {
34 n_elems
*= field
->num
;
37 trace_vmstate_n_elems(field
->name
, n_elems
);
41 static int vmstate_size(void *opaque
, VMStateField
*field
)
43 int size
= field
->size
;
45 if (field
->flags
& VMS_VBUFFER
) {
46 size
= *(int32_t *)(opaque
+field
->size_offset
);
47 if (field
->flags
& VMS_MULTIPLY
) {
55 static void vmstate_handle_alloc(void *ptr
, VMStateField
*field
, void *opaque
)
57 if (field
->flags
& VMS_POINTER
&& field
->flags
& VMS_ALLOC
) {
58 gsize size
= vmstate_size(opaque
, field
);
59 size
*= vmstate_n_elems(opaque
, field
);
61 *(void **)ptr
= g_malloc(size
);
66 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
67 void *opaque
, int version_id
)
69 VMStateField
*field
= vmsd
->fields
;
72 trace_vmstate_load_state(vmsd
->name
, version_id
);
73 if (version_id
> vmsd
->version_id
) {
74 error_report("%s: incoming version_id %d is too new "
75 "for local version_id %d",
76 vmsd
->name
, version_id
, vmsd
->version_id
);
77 trace_vmstate_load_state_end(vmsd
->name
, "too new", -EINVAL
);
80 if (version_id
< vmsd
->minimum_version_id
) {
81 if (vmsd
->load_state_old
&&
82 version_id
>= vmsd
->minimum_version_id_old
) {
83 ret
= vmsd
->load_state_old(f
, opaque
, version_id
);
84 trace_vmstate_load_state_end(vmsd
->name
, "old path", ret
);
87 error_report("%s: incoming version_id %d is too old "
88 "for local minimum version_id %d",
89 vmsd
->name
, version_id
, vmsd
->minimum_version_id
);
90 trace_vmstate_load_state_end(vmsd
->name
, "too old", -EINVAL
);
94 int ret
= vmsd
->pre_load(opaque
);
100 trace_vmstate_load_state_field(vmsd
->name
, field
->name
);
101 if ((field
->field_exists
&&
102 field
->field_exists(opaque
, version_id
)) ||
103 (!field
->field_exists
&&
104 field
->version_id
<= version_id
)) {
105 void *first_elem
= opaque
+ field
->offset
;
106 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
107 int size
= vmstate_size(opaque
, field
);
109 vmstate_handle_alloc(first_elem
, field
, opaque
);
110 if (field
->flags
& VMS_POINTER
) {
111 first_elem
= *(void **)first_elem
;
112 assert(first_elem
|| !n_elems
|| !size
);
114 for (i
= 0; i
< n_elems
; i
++) {
115 void *curr_elem
= first_elem
+ size
* i
;
117 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
118 curr_elem
= *(void **)curr_elem
;
120 if (!curr_elem
&& size
) {
121 /* if null pointer check placeholder and do not follow */
122 assert(field
->flags
& VMS_ARRAY_OF_POINTER
);
123 ret
= vmstate_info_nullptr
.get(f
, curr_elem
, size
, NULL
);
124 } else if (field
->flags
& VMS_STRUCT
) {
125 ret
= vmstate_load_state(f
, field
->vmsd
, curr_elem
,
126 field
->vmsd
->version_id
);
128 ret
= field
->info
->get(f
, curr_elem
, size
, field
);
131 ret
= qemu_file_get_error(f
);
134 qemu_file_set_error(f
, ret
);
135 error_report("Failed to load %s:%s", vmsd
->name
,
137 trace_vmstate_load_field_error(field
->name
, ret
);
141 } else if (field
->flags
& VMS_MUST_EXIST
) {
142 error_report("Input validation failed: %s/%s",
143 vmsd
->name
, field
->name
);
148 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
152 if (vmsd
->post_load
) {
153 ret
= vmsd
->post_load(opaque
, version_id
);
155 trace_vmstate_load_state_end(vmsd
->name
, "end", ret
);
159 static int vmfield_name_num(VMStateField
*start
, VMStateField
*search
)
164 for (field
= start
; field
->name
; field
++) {
165 if (!strcmp(field
->name
, search
->name
)) {
166 if (field
== search
) {
176 static bool vmfield_name_is_unique(VMStateField
*start
, VMStateField
*search
)
181 for (field
= start
; field
->name
; field
++) {
182 if (!strcmp(field
->name
, search
->name
)) {
184 /* name found more than once, so it's not unique */
194 static const char *vmfield_get_type_name(VMStateField
*field
)
196 const char *type
= "unknown";
198 if (field
->flags
& VMS_STRUCT
) {
200 } else if (field
->info
->name
) {
201 type
= field
->info
->name
;
207 static bool vmsd_can_compress(VMStateField
*field
)
209 if (field
->field_exists
) {
210 /* Dynamically existing fields mess up compression */
214 if (field
->flags
& VMS_STRUCT
) {
215 VMStateField
*sfield
= field
->vmsd
->fields
;
216 while (sfield
->name
) {
217 if (!vmsd_can_compress(sfield
)) {
218 /* Child elements can't compress, so can't we */
224 if (field
->vmsd
->subsections
) {
225 /* Subsections may come and go, better don't compress */
233 static void vmsd_desc_field_start(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
234 VMStateField
*field
, int i
, int max
)
236 char *name
, *old_name
;
237 bool is_array
= max
> 1;
238 bool can_compress
= vmsd_can_compress(field
);
244 name
= g_strdup(field
->name
);
246 /* Field name is not unique, need to make it unique */
247 if (!vmfield_name_is_unique(vmsd
->fields
, field
)) {
248 int num
= vmfield_name_num(vmsd
->fields
, field
);
250 name
= g_strdup_printf("%s[%d]", name
, num
);
254 json_start_object(vmdesc
, NULL
);
255 json_prop_str(vmdesc
, "name", name
);
258 json_prop_int(vmdesc
, "array_len", max
);
260 json_prop_int(vmdesc
, "index", i
);
263 json_prop_str(vmdesc
, "type", vmfield_get_type_name(field
));
265 if (field
->flags
& VMS_STRUCT
) {
266 json_start_object(vmdesc
, "struct");
272 static void vmsd_desc_field_end(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
273 VMStateField
*field
, size_t size
, int i
)
279 if (field
->flags
& VMS_STRUCT
) {
280 /* We printed a struct in between, close its child object */
281 json_end_object(vmdesc
);
284 json_prop_int(vmdesc
, "size", size
);
285 json_end_object(vmdesc
);
289 bool vmstate_save_needed(const VMStateDescription
*vmsd
, void *opaque
)
291 if (vmsd
->needed
&& !vmsd
->needed(opaque
)) {
292 /* optional section not needed */
299 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
300 void *opaque
, QJSON
*vmdesc
)
302 VMStateField
*field
= vmsd
->fields
;
304 trace_vmstate_save_state_top(vmsd
->name
);
306 if (vmsd
->pre_save
) {
307 vmsd
->pre_save(opaque
);
311 json_prop_str(vmdesc
, "vmsd_name", vmsd
->name
);
312 json_prop_int(vmdesc
, "version", vmsd
->version_id
);
313 json_start_array(vmdesc
, "fields");
316 while (field
->name
) {
317 if (!field
->field_exists
||
318 field
->field_exists(opaque
, vmsd
->version_id
)) {
319 void *first_elem
= opaque
+ field
->offset
;
320 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
321 int size
= vmstate_size(opaque
, field
);
322 int64_t old_offset
, written_bytes
;
323 QJSON
*vmdesc_loop
= vmdesc
;
325 trace_vmstate_save_state_loop(vmsd
->name
, field
->name
, n_elems
);
326 if (field
->flags
& VMS_POINTER
) {
327 first_elem
= *(void **)first_elem
;
328 assert(first_elem
|| !n_elems
|| !size
);
330 for (i
= 0; i
< n_elems
; i
++) {
331 void *curr_elem
= first_elem
+ size
* i
;
333 vmsd_desc_field_start(vmsd
, vmdesc_loop
, field
, i
, n_elems
);
334 old_offset
= qemu_ftell_fast(f
);
335 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
337 curr_elem
= *(void **)curr_elem
;
339 if (!curr_elem
&& size
) {
340 /* if null pointer write placeholder and do not follow */
341 assert(field
->flags
& VMS_ARRAY_OF_POINTER
);
342 vmstate_info_nullptr
.put(f
, curr_elem
, size
, NULL
, NULL
);
343 } else if (field
->flags
& VMS_STRUCT
) {
344 vmstate_save_state(f
, field
->vmsd
, curr_elem
, vmdesc_loop
);
346 field
->info
->put(f
, curr_elem
, size
, field
, vmdesc_loop
);
349 written_bytes
= qemu_ftell_fast(f
) - old_offset
;
350 vmsd_desc_field_end(vmsd
, vmdesc_loop
, field
, written_bytes
, i
);
352 /* Compressed arrays only care about the first element */
353 if (vmdesc_loop
&& vmsd_can_compress(field
)) {
358 if (field
->flags
& VMS_MUST_EXIST
) {
359 error_report("Output state validation failed: %s/%s",
360 vmsd
->name
, field
->name
);
361 assert(!(field
->flags
& VMS_MUST_EXIST
));
368 json_end_array(vmdesc
);
371 vmstate_subsection_save(f
, vmsd
, opaque
, vmdesc
);
374 static const VMStateDescription
*
375 vmstate_get_subsection(const VMStateDescription
**sub
, char *idstr
)
377 while (sub
&& *sub
&& (*sub
)->needed
) {
378 if (strcmp(idstr
, (*sub
)->name
) == 0) {
386 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
389 trace_vmstate_subsection_load(vmsd
->name
);
391 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
392 char idstr
[256], *idstr_ret
;
394 uint8_t version_id
, len
, size
;
395 const VMStateDescription
*sub_vmsd
;
397 len
= qemu_peek_byte(f
, 1);
398 if (len
< strlen(vmsd
->name
) + 1) {
399 /* subsection name has be be "section_name/a" */
400 trace_vmstate_subsection_load_bad(vmsd
->name
, "(short)", "");
403 size
= qemu_peek_buffer(f
, (uint8_t **)&idstr_ret
, len
, 2);
405 trace_vmstate_subsection_load_bad(vmsd
->name
, "(peek fail)", "");
408 memcpy(idstr
, idstr_ret
, size
);
411 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
412 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(prefix)");
413 /* it doesn't have a valid subsection name */
416 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
417 if (sub_vmsd
== NULL
) {
418 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(lookup)");
421 qemu_file_skip(f
, 1); /* subsection */
422 qemu_file_skip(f
, 1); /* len */
423 qemu_file_skip(f
, len
); /* idstr */
424 version_id
= qemu_get_be32(f
);
426 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
428 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(child)");
433 trace_vmstate_subsection_load_good(vmsd
->name
);
437 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
438 void *opaque
, QJSON
*vmdesc
)
440 const VMStateDescription
**sub
= vmsd
->subsections
;
441 bool subsection_found
= false;
443 trace_vmstate_subsection_save_top(vmsd
->name
);
444 while (sub
&& *sub
&& (*sub
)->needed
) {
445 if ((*sub
)->needed(opaque
)) {
446 const VMStateDescription
*vmsdsub
= *sub
;
449 trace_vmstate_subsection_save_loop(vmsd
->name
, vmsdsub
->name
);
451 /* Only create subsection array when we have any */
452 if (!subsection_found
) {
453 json_start_array(vmdesc
, "subsections");
454 subsection_found
= true;
457 json_start_object(vmdesc
, NULL
);
460 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
461 len
= strlen(vmsdsub
->name
);
462 qemu_put_byte(f
, len
);
463 qemu_put_buffer(f
, (uint8_t *)vmsdsub
->name
, len
);
464 qemu_put_be32(f
, vmsdsub
->version_id
);
465 vmstate_save_state(f
, vmsdsub
, opaque
, vmdesc
);
468 json_end_object(vmdesc
);
474 if (vmdesc
&& subsection_found
) {
475 json_end_array(vmdesc
);
481 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
484 *v
= qemu_get_byte(f
);
488 static int put_bool(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
492 qemu_put_byte(f
, *v
);
496 const VMStateInfo vmstate_info_bool
= {
504 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
511 static int put_int8(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
519 const VMStateInfo vmstate_info_int8
= {
527 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
530 qemu_get_sbe16s(f
, v
);
534 static int put_int16(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
538 qemu_put_sbe16s(f
, v
);
542 const VMStateInfo vmstate_info_int16
= {
550 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
553 qemu_get_sbe32s(f
, v
);
557 static int put_int32(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
561 qemu_put_sbe32s(f
, v
);
565 const VMStateInfo vmstate_info_int32
= {
571 /* 32 bit int. See that the received value is the same than the one
574 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
,
579 qemu_get_sbe32s(f
, &v2
);
584 error_report("%" PRIx32
" != %" PRIx32
, *v
, v2
);
588 const VMStateInfo vmstate_info_int32_equal
= {
589 .name
= "int32 equal",
590 .get
= get_int32_equal
,
594 /* 32 bit int. Check that the received value is non-negative
595 * and less than or equal to the one in the field.
598 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
602 qemu_get_sbe32s(f
, &loaded
);
604 if (loaded
>= 0 && loaded
<= *cur
) {
608 error_report("Invalid value %" PRId32
609 " expecting positive value <= %" PRId32
,
614 const VMStateInfo vmstate_info_int32_le
= {
622 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
625 qemu_get_sbe64s(f
, v
);
629 static int put_int64(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
633 qemu_put_sbe64s(f
, v
);
637 const VMStateInfo vmstate_info_int64
= {
643 /* 8 bit unsigned int */
645 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
652 static int put_uint8(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
660 const VMStateInfo vmstate_info_uint8
= {
666 /* 16 bit unsigned int */
668 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
671 qemu_get_be16s(f
, v
);
675 static int put_uint16(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
679 qemu_put_be16s(f
, v
);
683 const VMStateInfo vmstate_info_uint16
= {
689 /* 32 bit unsigned int */
691 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
694 qemu_get_be32s(f
, v
);
698 static int put_uint32(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
702 qemu_put_be32s(f
, v
);
706 const VMStateInfo vmstate_info_uint32
= {
712 /* 32 bit uint. See that the received value is the same than the one
715 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
,
720 qemu_get_be32s(f
, &v2
);
725 error_report("%" PRIx32
" != %" PRIx32
, *v
, v2
);
729 const VMStateInfo vmstate_info_uint32_equal
= {
730 .name
= "uint32 equal",
731 .get
= get_uint32_equal
,
735 /* 64 bit unsigned int */
737 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
740 qemu_get_be64s(f
, v
);
744 static int put_uint64(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
748 qemu_put_be64s(f
, v
);
752 const VMStateInfo vmstate_info_uint64
= {
758 static int get_nullptr(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
761 if (qemu_get_byte(f
) == VMS_NULLPTR_MARKER
) {
764 error_report("vmstate: get_nullptr expected VMS_NULLPTR_MARKER");
768 static int put_nullptr(QEMUFile
*f
, void *pv
, size_t size
,
769 VMStateField
*field
, QJSON
*vmdesc
)
773 qemu_put_byte(f
, VMS_NULLPTR_MARKER
);
776 error_report("vmstate: put_nullptr must be called with pv == NULL");
780 const VMStateInfo vmstate_info_nullptr
= {
786 /* 64 bit unsigned int. See that the received value is the same than the one
789 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
,
794 qemu_get_be64s(f
, &v2
);
799 error_report("%" PRIx64
" != %" PRIx64
, *v
, v2
);
803 const VMStateInfo vmstate_info_uint64_equal
= {
804 .name
= "int64 equal",
805 .get
= get_uint64_equal
,
809 /* 8 bit int. See that the received value is the same than the one
812 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
,
822 error_report("%x != %x", *v
, v2
);
826 const VMStateInfo vmstate_info_uint8_equal
= {
827 .name
= "uint8 equal",
828 .get
= get_uint8_equal
,
832 /* 16 bit unsigned int int. See that the received value is the same than the one
835 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
,
840 qemu_get_be16s(f
, &v2
);
845 error_report("%x != %x", *v
, v2
);
849 const VMStateInfo vmstate_info_uint16_equal
= {
850 .name
= "uint16 equal",
851 .get
= get_uint16_equal
,
857 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
,
862 *v
= make_float64(qemu_get_be64(f
));
866 static int put_float64(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
871 qemu_put_be64(f
, float64_val(*v
));
875 const VMStateInfo vmstate_info_float64
= {
881 /* CPU_DoubleU type */
883 static int get_cpudouble(QEMUFile
*f
, void *pv
, size_t size
,
887 qemu_get_be32s(f
, &v
->l
.upper
);
888 qemu_get_be32s(f
, &v
->l
.lower
);
892 static int put_cpudouble(QEMUFile
*f
, void *pv
, size_t size
,
893 VMStateField
*field
, QJSON
*vmdesc
)
896 qemu_put_be32s(f
, &v
->l
.upper
);
897 qemu_put_be32s(f
, &v
->l
.lower
);
901 const VMStateInfo vmstate_info_cpudouble
= {
902 .name
= "CPU_Double_U",
903 .get
= get_cpudouble
,
904 .put
= put_cpudouble
,
907 /* uint8_t buffers */
909 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
,
913 qemu_get_buffer(f
, v
, size
);
917 static int put_buffer(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
921 qemu_put_buffer(f
, v
, size
);
925 const VMStateInfo vmstate_info_buffer
= {
931 /* unused buffers: space that was used for some fields that are
932 not useful anymore */
934 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
,
941 block_len
= MIN(sizeof(buf
), size
);
943 qemu_get_buffer(f
, buf
, block_len
);
948 static int put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
,
949 VMStateField
*field
, QJSON
*vmdesc
)
951 static const uint8_t buf
[1024];
955 block_len
= MIN(sizeof(buf
), size
);
957 qemu_put_buffer(f
, buf
, block_len
);
963 const VMStateInfo vmstate_info_unused_buffer
= {
964 .name
= "unused_buffer",
965 .get
= get_unused_buffer
,
966 .put
= put_unused_buffer
,
969 /* vmstate_info_tmp, see VMSTATE_WITH_TMP, the idea is that we allocate
970 * a temporary buffer and the pre_load/pre_save methods in the child vmsd
971 * copy stuff from the parent into the child and do calculations to fill
972 * in fields that don't really exist in the parent but need to be in the
975 static int get_tmp(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
978 const VMStateDescription
*vmsd
= field
->vmsd
;
979 int version_id
= field
->version_id
;
980 void *tmp
= g_malloc(size
);
982 /* Writes the parent field which is at the start of the tmp */
984 ret
= vmstate_load_state(f
, vmsd
, tmp
, version_id
);
989 static int put_tmp(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
992 const VMStateDescription
*vmsd
= field
->vmsd
;
993 void *tmp
= g_malloc(size
);
995 /* Writes the parent field which is at the start of the tmp */
997 vmstate_save_state(f
, vmsd
, tmp
, vmdesc
);
1003 const VMStateInfo vmstate_info_tmp
= {
1009 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1010 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1011 * bit words with the bits in big endian order. The in-memory format
1012 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1014 /* This is the number of 64 bit words sent over the wire */
1015 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1016 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
)
1018 unsigned long *bmp
= pv
;
1020 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1021 uint64_t w
= qemu_get_be64(f
);
1023 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1024 bmp
[idx
++] = w
>> 32;
1030 static int put_bitmap(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
1033 unsigned long *bmp
= pv
;
1035 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1036 uint64_t w
= bmp
[idx
++];
1037 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1038 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1040 qemu_put_be64(f
, w
);
1046 const VMStateInfo vmstate_info_bitmap
= {
1053 * meta data about the QTAILQ is encoded in a VMStateField structure
1055 static int get_qtailq(QEMUFile
*f
, void *pv
, size_t unused_size
,
1056 VMStateField
*field
)
1059 const VMStateDescription
*vmsd
= field
->vmsd
;
1060 /* size of a QTAILQ element */
1061 size_t size
= field
->size
;
1062 /* offset of the QTAILQ entry in a QTAILQ element */
1063 size_t entry_offset
= field
->start
;
1064 int version_id
= field
->version_id
;
1067 trace_get_qtailq(vmsd
->name
, version_id
);
1068 if (version_id
> vmsd
->version_id
) {
1069 error_report("%s %s", vmsd
->name
, "too new");
1070 trace_get_qtailq_end(vmsd
->name
, "too new", -EINVAL
);
1074 if (version_id
< vmsd
->minimum_version_id
) {
1075 error_report("%s %s", vmsd
->name
, "too old");
1076 trace_get_qtailq_end(vmsd
->name
, "too old", -EINVAL
);
1080 while (qemu_get_byte(f
)) {
1081 elm
= g_malloc(size
);
1082 ret
= vmstate_load_state(f
, vmsd
, elm
, version_id
);
1086 QTAILQ_RAW_INSERT_TAIL(pv
, elm
, entry_offset
);
1089 trace_get_qtailq_end(vmsd
->name
, "end", ret
);
1093 /* put for QTAILQ */
1094 static int put_qtailq(QEMUFile
*f
, void *pv
, size_t unused_size
,
1095 VMStateField
*field
, QJSON
*vmdesc
)
1097 const VMStateDescription
*vmsd
= field
->vmsd
;
1098 /* offset of the QTAILQ entry in a QTAILQ element*/
1099 size_t entry_offset
= field
->start
;
1102 trace_put_qtailq(vmsd
->name
, vmsd
->version_id
);
1104 QTAILQ_RAW_FOREACH(elm
, pv
, entry_offset
) {
1105 qemu_put_byte(f
, true);
1106 vmstate_save_state(f
, vmsd
, elm
, vmdesc
);
1108 qemu_put_byte(f
, false);
1110 trace_put_qtailq_end(vmsd
->name
, "end");
1114 const VMStateInfo vmstate_info_qtailq
= {