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)) {
52 int n_elems = vmstate_n_elems(opaque, field);
54 gsize size = n_elems * field->size;
55 *((void **)base_addr + field->start) = g_malloc(size);
58 base_addr = *(void **)base_addr + field->start;
64 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
65 void *opaque, int version_id)
67 VMStateField *field = vmsd->fields;
70 if (version_id > vmsd->version_id) {
73 if (version_id < vmsd->minimum_version_id) {
74 if (vmsd->load_state_old &&
75 version_id >= vmsd->minimum_version_id_old) {
76 return vmsd->load_state_old(f, opaque, version_id);
81 int ret = vmsd->pre_load(opaque);
87 if ((field->field_exists &&
88 field->field_exists(opaque, version_id)) ||
89 (!field->field_exists &&
90 field->version_id <= version_id)) {
91 void *base_addr = vmstate_base_addr(opaque, field, true);
92 int i, n_elems = vmstate_n_elems(opaque, field);
93 int size = vmstate_size(opaque, field);
95 for (i = 0; i < n_elems; i++) {
96 void *addr = base_addr + size * i;
98 if (field->flags & VMS_ARRAY_OF_POINTER) {
99 addr = *(void **)addr;
101 if (field->flags & VMS_STRUCT) {
102 ret = vmstate_load_state(f, field->vmsd, addr,
103 field->vmsd->version_id);
105 ret = field->info->get(f, addr, size);
109 ret = qemu_file_get_error(f);
112 qemu_file_set_error(f, ret);
113 trace_vmstate_load_field_error(field->name, ret);
117 } else if (field->flags & VMS_MUST_EXIST) {
118 fprintf(stderr, "Input validation failed: %s/%s\n",
119 vmsd->name, field->name);
124 ret = vmstate_subsection_load(f, vmsd, opaque);
128 if (vmsd->post_load) {
129 return vmsd->post_load(opaque, version_id);
134 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
137 VMStateField *field = vmsd->fields;
139 if (vmsd->pre_save) {
140 vmsd->pre_save(opaque);
142 while (field->name) {
143 if (!field->field_exists ||
144 field->field_exists(opaque, vmsd->version_id)) {
145 void *base_addr = vmstate_base_addr(opaque, field, false);
146 int i, n_elems = vmstate_n_elems(opaque, field);
147 int size = vmstate_size(opaque, field);
149 for (i = 0; i < n_elems; i++) {
150 void *addr = base_addr + size * i;
152 if (field->flags & VMS_ARRAY_OF_POINTER) {
153 addr = *(void **)addr;
155 if (field->flags & VMS_STRUCT) {
156 vmstate_save_state(f, field->vmsd, addr);
158 field->info->put(f, addr, size);
162 if (field->flags & VMS_MUST_EXIST) {
163 fprintf(stderr, "Output state validation failed: %s/%s\n",
164 vmsd->name, field->name);
165 assert(!(field->flags & VMS_MUST_EXIST));
170 vmstate_subsection_save(f, vmsd, opaque);
173 static const VMStateDescription *
174 vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
176 while (sub && sub->needed) {
177 if (strcmp(idstr, sub->vmsd->name) == 0) {
185 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
188 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
191 uint8_t version_id, len, size;
192 const VMStateDescription *sub_vmsd;
194 len = qemu_peek_byte(f, 1);
195 if (len < strlen(vmsd->name) + 1) {
196 /* subsection name has be be "section_name/a" */
199 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
205 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
206 /* it don't have a valid subsection name */
209 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
210 if (sub_vmsd == NULL) {
213 qemu_file_skip(f, 1); /* subsection */
214 qemu_file_skip(f, 1); /* len */
215 qemu_file_skip(f, len); /* idstr */
216 version_id = qemu_get_be32(f);
218 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
226 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
229 const VMStateSubsection *sub = vmsd->subsections;
231 while (sub && sub->needed) {
232 if (sub->needed(opaque)) {
233 const VMStateDescription *vmsd = sub->vmsd;
236 qemu_put_byte(f, QEMU_VM_SUBSECTION);
237 len = strlen(vmsd->name);
238 qemu_put_byte(f, len);
239 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
240 qemu_put_be32(f, vmsd->version_id);
241 vmstate_save_state(f, vmsd, opaque);
249 static int get_bool(QEMUFile *f, void *pv, size_t size)
252 *v = qemu_get_byte(f);
256 static void put_bool(QEMUFile *f, void *pv, size_t size)
259 qemu_put_byte(f, *v);
262 const VMStateInfo vmstate_info_bool = {
270 static int get_int8(QEMUFile *f, void *pv, size_t size)
277 static void put_int8(QEMUFile *f, void *pv, size_t size)
283 const VMStateInfo vmstate_info_int8 = {
291 static int get_int16(QEMUFile *f, void *pv, size_t size)
294 qemu_get_sbe16s(f, v);
298 static void put_int16(QEMUFile *f, void *pv, size_t size)
301 qemu_put_sbe16s(f, v);
304 const VMStateInfo vmstate_info_int16 = {
312 static int get_int32(QEMUFile *f, void *pv, size_t size)
315 qemu_get_sbe32s(f, v);
319 static void put_int32(QEMUFile *f, void *pv, size_t size)
322 qemu_put_sbe32s(f, v);
325 const VMStateInfo vmstate_info_int32 = {
331 /* 32 bit int. See that the received value is the same than the one
334 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
338 qemu_get_sbe32s(f, &v2);
346 const VMStateInfo vmstate_info_int32_equal = {
347 .name = "int32 equal",
348 .get = get_int32_equal,
352 /* 32 bit int. Check that the received value is non-negative
353 * and less than or equal to the one in the field.
356 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
360 qemu_get_sbe32s(f, &loaded);
362 if (loaded >= 0 && loaded <= *cur) {
369 const VMStateInfo vmstate_info_int32_le = {
377 static int get_int64(QEMUFile *f, void *pv, size_t size)
380 qemu_get_sbe64s(f, v);
384 static void put_int64(QEMUFile *f, void *pv, size_t size)
387 qemu_put_sbe64s(f, v);
390 const VMStateInfo vmstate_info_int64 = {
396 /* 8 bit unsigned int */
398 static int get_uint8(QEMUFile *f, void *pv, size_t size)
405 static void put_uint8(QEMUFile *f, void *pv, size_t size)
411 const VMStateInfo vmstate_info_uint8 = {
417 /* 16 bit unsigned int */
419 static int get_uint16(QEMUFile *f, void *pv, size_t size)
422 qemu_get_be16s(f, v);
426 static void put_uint16(QEMUFile *f, void *pv, size_t size)
429 qemu_put_be16s(f, v);
432 const VMStateInfo vmstate_info_uint16 = {
438 /* 32 bit unsigned int */
440 static int get_uint32(QEMUFile *f, void *pv, size_t size)
443 qemu_get_be32s(f, v);
447 static void put_uint32(QEMUFile *f, void *pv, size_t size)
450 qemu_put_be32s(f, v);
453 const VMStateInfo vmstate_info_uint32 = {
459 /* 32 bit uint. See that the received value is the same than the one
462 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
466 qemu_get_be32s(f, &v2);
474 const VMStateInfo vmstate_info_uint32_equal = {
475 .name = "uint32 equal",
476 .get = get_uint32_equal,
480 /* 64 bit unsigned int */
482 static int get_uint64(QEMUFile *f, void *pv, size_t size)
485 qemu_get_be64s(f, v);
489 static void put_uint64(QEMUFile *f, void *pv, size_t size)
492 qemu_put_be64s(f, v);
495 const VMStateInfo vmstate_info_uint64 = {
501 /* 64 bit unsigned int. See that the received value is the same than the one
504 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
508 qemu_get_be64s(f, &v2);
516 const VMStateInfo vmstate_info_uint64_equal = {
517 .name = "int64 equal",
518 .get = get_uint64_equal,
522 /* 8 bit int. See that the received value is the same than the one
525 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
537 const VMStateInfo vmstate_info_uint8_equal = {
538 .name = "uint8 equal",
539 .get = get_uint8_equal,
543 /* 16 bit unsigned int int. See that the received value is the same than the one
546 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
550 qemu_get_be16s(f, &v2);
558 const VMStateInfo vmstate_info_uint16_equal = {
559 .name = "uint16 equal",
560 .get = get_uint16_equal,
566 static int get_float64(QEMUFile *f, void *pv, size_t size)
570 *v = make_float64(qemu_get_be64(f));
574 static void put_float64(QEMUFile *f, void *pv, size_t size)
578 qemu_put_be64(f, float64_val(*v));
581 const VMStateInfo vmstate_info_float64 = {
587 /* uint8_t buffers */
589 static int get_buffer(QEMUFile *f, void *pv, size_t size)
592 qemu_get_buffer(f, v, size);
596 static void put_buffer(QEMUFile *f, void *pv, size_t size)
599 qemu_put_buffer(f, v, size);
602 const VMStateInfo vmstate_info_buffer = {
608 /* unused buffers: space that was used for some fields that are
609 not useful anymore */
611 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
617 block_len = MIN(sizeof(buf), size);
619 qemu_get_buffer(f, buf, block_len);
624 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
626 static const uint8_t buf[1024];
630 block_len = MIN(sizeof(buf), size);
632 qemu_put_buffer(f, buf, block_len);
636 const VMStateInfo vmstate_info_unused_buffer = {
637 .name = "unused_buffer",
638 .get = get_unused_buffer,
639 .put = put_unused_buffer,
642 /* bitmaps (as defined by bitmap.h). Note that size here is the size
643 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
644 * bit words with the bits in big endian order. The in-memory format
645 * is an array of 'unsigned long', which may be either 32 or 64 bits.
647 /* This is the number of 64 bit words sent over the wire */
648 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
649 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
651 unsigned long *bmp = pv;
653 for (i = 0; i < BITS_TO_U64S(size); i++) {
654 uint64_t w = qemu_get_be64(f);
656 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
657 bmp[idx++] = w >> 32;
663 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
665 unsigned long *bmp = pv;
667 for (i = 0; i < BITS_TO_U64S(size); i++) {
668 uint64_t w = bmp[idx++];
669 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
670 w |= ((uint64_t)bmp[idx++]) << 32;
676 const VMStateInfo vmstate_info_bitmap = {