Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2017-02-22' into staging
[qemu/ar7.git] / migration / vmstate.c
blobb4d8ae982ac5662a6995aa0a0aa90377c11ed3f5
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"
9 #include "trace.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,
15 void *opaque);
17 static int vmstate_n_elems(void *opaque, VMStateField *field)
19 int n_elems = 1;
21 if (field->flags & VMS_ARRAY) {
22 n_elems = field->num;
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);
38 return 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) {
48 size *= field->size;
52 return size;
55 static void *vmstate_base_addr(void *opaque, VMStateField *field, bool alloc)
57 void *base_addr = opaque + field->offset;
59 if (field->flags & VMS_POINTER) {
60 if (alloc && (field->flags & VMS_ALLOC)) {
61 gsize size = 0;
62 if (field->flags & VMS_VBUFFER) {
63 size = vmstate_size(opaque, field);
64 } else {
65 int n_elems = vmstate_n_elems(opaque, field);
66 if (n_elems) {
67 size = n_elems * field->size;
70 if (size) {
71 *(void **)base_addr = g_malloc(size);
74 base_addr = *(void **)base_addr;
77 return base_addr;
80 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
81 void *opaque, int version_id)
83 VMStateField *field = vmsd->fields;
84 int ret = 0;
86 trace_vmstate_load_state(vmsd->name, version_id);
87 if (version_id > vmsd->version_id) {
88 error_report("%s: incoming version_id %d is too new "
89 "for local version_id %d",
90 vmsd->name, version_id, vmsd->version_id);
91 trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
92 return -EINVAL;
94 if (version_id < vmsd->minimum_version_id) {
95 if (vmsd->load_state_old &&
96 version_id >= vmsd->minimum_version_id_old) {
97 ret = vmsd->load_state_old(f, opaque, version_id);
98 trace_vmstate_load_state_end(vmsd->name, "old path", ret);
99 return ret;
101 error_report("%s: incoming version_id %d is too old "
102 "for local minimum version_id %d",
103 vmsd->name, version_id, vmsd->minimum_version_id);
104 trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
105 return -EINVAL;
107 if (vmsd->pre_load) {
108 int ret = vmsd->pre_load(opaque);
109 if (ret) {
110 return ret;
113 while (field->name) {
114 trace_vmstate_load_state_field(vmsd->name, field->name);
115 if ((field->field_exists &&
116 field->field_exists(opaque, version_id)) ||
117 (!field->field_exists &&
118 field->version_id <= version_id)) {
119 void *base_addr = vmstate_base_addr(opaque, field, true);
120 int i, n_elems = vmstate_n_elems(opaque, field);
121 int size = vmstate_size(opaque, field);
123 for (i = 0; i < n_elems; i++) {
124 void *addr = base_addr + size * i;
126 if (field->flags & VMS_ARRAY_OF_POINTER) {
127 addr = *(void **)addr;
129 if (field->flags & VMS_STRUCT) {
130 ret = vmstate_load_state(f, field->vmsd, addr,
131 field->vmsd->version_id);
132 } else {
133 ret = field->info->get(f, addr, size, field);
135 if (ret >= 0) {
136 ret = qemu_file_get_error(f);
138 if (ret < 0) {
139 qemu_file_set_error(f, ret);
140 error_report("Failed to load %s:%s", vmsd->name,
141 field->name);
142 trace_vmstate_load_field_error(field->name, ret);
143 return ret;
146 } else if (field->flags & VMS_MUST_EXIST) {
147 error_report("Input validation failed: %s/%s",
148 vmsd->name, field->name);
149 return -1;
151 field++;
153 ret = vmstate_subsection_load(f, vmsd, opaque);
154 if (ret != 0) {
155 return ret;
157 if (vmsd->post_load) {
158 ret = vmsd->post_load(opaque, version_id);
160 trace_vmstate_load_state_end(vmsd->name, "end", ret);
161 return ret;
164 static int vmfield_name_num(VMStateField *start, VMStateField *search)
166 VMStateField *field;
167 int found = 0;
169 for (field = start; field->name; field++) {
170 if (!strcmp(field->name, search->name)) {
171 if (field == search) {
172 return found;
174 found++;
178 return -1;
181 static bool vmfield_name_is_unique(VMStateField *start, VMStateField *search)
183 VMStateField *field;
184 int found = 0;
186 for (field = start; field->name; field++) {
187 if (!strcmp(field->name, search->name)) {
188 found++;
189 /* name found more than once, so it's not unique */
190 if (found > 1) {
191 return false;
196 return true;
199 static const char *vmfield_get_type_name(VMStateField *field)
201 const char *type = "unknown";
203 if (field->flags & VMS_STRUCT) {
204 type = "struct";
205 } else if (field->info->name) {
206 type = field->info->name;
209 return type;
212 static bool vmsd_can_compress(VMStateField *field)
214 if (field->field_exists) {
215 /* Dynamically existing fields mess up compression */
216 return false;
219 if (field->flags & VMS_STRUCT) {
220 VMStateField *sfield = field->vmsd->fields;
221 while (sfield->name) {
222 if (!vmsd_can_compress(sfield)) {
223 /* Child elements can't compress, so can't we */
224 return false;
226 sfield++;
229 if (field->vmsd->subsections) {
230 /* Subsections may come and go, better don't compress */
231 return false;
235 return true;
238 static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc,
239 VMStateField *field, int i, int max)
241 char *name, *old_name;
242 bool is_array = max > 1;
243 bool can_compress = vmsd_can_compress(field);
245 if (!vmdesc) {
246 return;
249 name = g_strdup(field->name);
251 /* Field name is not unique, need to make it unique */
252 if (!vmfield_name_is_unique(vmsd->fields, field)) {
253 int num = vmfield_name_num(vmsd->fields, field);
254 old_name = name;
255 name = g_strdup_printf("%s[%d]", name, num);
256 g_free(old_name);
259 json_start_object(vmdesc, NULL);
260 json_prop_str(vmdesc, "name", name);
261 if (is_array) {
262 if (can_compress) {
263 json_prop_int(vmdesc, "array_len", max);
264 } else {
265 json_prop_int(vmdesc, "index", i);
268 json_prop_str(vmdesc, "type", vmfield_get_type_name(field));
270 if (field->flags & VMS_STRUCT) {
271 json_start_object(vmdesc, "struct");
274 g_free(name);
277 static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc,
278 VMStateField *field, size_t size, int i)
280 if (!vmdesc) {
281 return;
284 if (field->flags & VMS_STRUCT) {
285 /* We printed a struct in between, close its child object */
286 json_end_object(vmdesc);
289 json_prop_int(vmdesc, "size", size);
290 json_end_object(vmdesc);
294 bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque)
296 if (vmsd->needed && !vmsd->needed(opaque)) {
297 /* optional section not needed */
298 return false;
300 return true;
304 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
305 void *opaque, QJSON *vmdesc)
307 VMStateField *field = vmsd->fields;
309 trace_vmstate_save_state_top(vmsd->name);
311 if (vmsd->pre_save) {
312 vmsd->pre_save(opaque);
315 if (vmdesc) {
316 json_prop_str(vmdesc, "vmsd_name", vmsd->name);
317 json_prop_int(vmdesc, "version", vmsd->version_id);
318 json_start_array(vmdesc, "fields");
321 while (field->name) {
322 if (!field->field_exists ||
323 field->field_exists(opaque, vmsd->version_id)) {
324 void *base_addr = vmstate_base_addr(opaque, field, false);
325 int i, n_elems = vmstate_n_elems(opaque, field);
326 int size = vmstate_size(opaque, field);
327 int64_t old_offset, written_bytes;
328 QJSON *vmdesc_loop = vmdesc;
330 trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
331 for (i = 0; i < n_elems; i++) {
332 void *addr = base_addr + size * i;
334 vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
335 old_offset = qemu_ftell_fast(f);
337 if (field->flags & VMS_ARRAY_OF_POINTER) {
338 addr = *(void **)addr;
340 if (field->flags & VMS_STRUCT) {
341 vmstate_save_state(f, field->vmsd, addr, vmdesc_loop);
342 } else {
343 field->info->put(f, addr, size, field, vmdesc_loop);
346 written_bytes = qemu_ftell_fast(f) - old_offset;
347 vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
349 /* Compressed arrays only care about the first element */
350 if (vmdesc_loop && vmsd_can_compress(field)) {
351 vmdesc_loop = NULL;
354 } else {
355 if (field->flags & VMS_MUST_EXIST) {
356 error_report("Output state validation failed: %s/%s",
357 vmsd->name, field->name);
358 assert(!(field->flags & VMS_MUST_EXIST));
361 field++;
364 if (vmdesc) {
365 json_end_array(vmdesc);
368 vmstate_subsection_save(f, vmsd, opaque, vmdesc);
371 static const VMStateDescription *
372 vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
374 while (sub && *sub && (*sub)->needed) {
375 if (strcmp(idstr, (*sub)->name) == 0) {
376 return *sub;
378 sub++;
380 return NULL;
383 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
384 void *opaque)
386 trace_vmstate_subsection_load(vmsd->name);
388 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
389 char idstr[256], *idstr_ret;
390 int ret;
391 uint8_t version_id, len, size;
392 const VMStateDescription *sub_vmsd;
394 len = qemu_peek_byte(f, 1);
395 if (len < strlen(vmsd->name) + 1) {
396 /* subsection name has be be "section_name/a" */
397 trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
398 return 0;
400 size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
401 if (size != len) {
402 trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
403 return 0;
405 memcpy(idstr, idstr_ret, size);
406 idstr[size] = 0;
408 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
409 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
410 /* it doesn't have a valid subsection name */
411 return 0;
413 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
414 if (sub_vmsd == NULL) {
415 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
416 return -ENOENT;
418 qemu_file_skip(f, 1); /* subsection */
419 qemu_file_skip(f, 1); /* len */
420 qemu_file_skip(f, len); /* idstr */
421 version_id = qemu_get_be32(f);
423 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
424 if (ret) {
425 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
426 return ret;
430 trace_vmstate_subsection_load_good(vmsd->name);
431 return 0;
434 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
435 void *opaque, QJSON *vmdesc)
437 const VMStateDescription **sub = vmsd->subsections;
438 bool subsection_found = false;
440 trace_vmstate_subsection_save_top(vmsd->name);
441 while (sub && *sub && (*sub)->needed) {
442 if ((*sub)->needed(opaque)) {
443 const VMStateDescription *vmsdsub = *sub;
444 uint8_t len;
446 trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
447 if (vmdesc) {
448 /* Only create subsection array when we have any */
449 if (!subsection_found) {
450 json_start_array(vmdesc, "subsections");
451 subsection_found = true;
454 json_start_object(vmdesc, NULL);
457 qemu_put_byte(f, QEMU_VM_SUBSECTION);
458 len = strlen(vmsdsub->name);
459 qemu_put_byte(f, len);
460 qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
461 qemu_put_be32(f, vmsdsub->version_id);
462 vmstate_save_state(f, vmsdsub, opaque, vmdesc);
464 if (vmdesc) {
465 json_end_object(vmdesc);
468 sub++;
471 if (vmdesc && subsection_found) {
472 json_end_array(vmdesc);
476 /* bool */
478 static int get_bool(QEMUFile *f, void *pv, size_t size, VMStateField *field)
480 bool *v = pv;
481 *v = qemu_get_byte(f);
482 return 0;
485 static int put_bool(QEMUFile *f, void *pv, size_t size, VMStateField *field,
486 QJSON *vmdesc)
488 bool *v = pv;
489 qemu_put_byte(f, *v);
490 return 0;
493 const VMStateInfo vmstate_info_bool = {
494 .name = "bool",
495 .get = get_bool,
496 .put = put_bool,
499 /* 8 bit int */
501 static int get_int8(QEMUFile *f, void *pv, size_t size, VMStateField *field)
503 int8_t *v = pv;
504 qemu_get_s8s(f, v);
505 return 0;
508 static int put_int8(QEMUFile *f, void *pv, size_t size, VMStateField *field,
509 QJSON *vmdesc)
511 int8_t *v = pv;
512 qemu_put_s8s(f, v);
513 return 0;
516 const VMStateInfo vmstate_info_int8 = {
517 .name = "int8",
518 .get = get_int8,
519 .put = put_int8,
522 /* 16 bit int */
524 static int get_int16(QEMUFile *f, void *pv, size_t size, VMStateField *field)
526 int16_t *v = pv;
527 qemu_get_sbe16s(f, v);
528 return 0;
531 static int put_int16(QEMUFile *f, void *pv, size_t size, VMStateField *field,
532 QJSON *vmdesc)
534 int16_t *v = pv;
535 qemu_put_sbe16s(f, v);
536 return 0;
539 const VMStateInfo vmstate_info_int16 = {
540 .name = "int16",
541 .get = get_int16,
542 .put = put_int16,
545 /* 32 bit int */
547 static int get_int32(QEMUFile *f, void *pv, size_t size, VMStateField *field)
549 int32_t *v = pv;
550 qemu_get_sbe32s(f, v);
551 return 0;
554 static int put_int32(QEMUFile *f, void *pv, size_t size, VMStateField *field,
555 QJSON *vmdesc)
557 int32_t *v = pv;
558 qemu_put_sbe32s(f, v);
559 return 0;
562 const VMStateInfo vmstate_info_int32 = {
563 .name = "int32",
564 .get = get_int32,
565 .put = put_int32,
568 /* 32 bit int. See that the received value is the same than the one
569 in the field */
571 static int get_int32_equal(QEMUFile *f, void *pv, size_t size,
572 VMStateField *field)
574 int32_t *v = pv;
575 int32_t v2;
576 qemu_get_sbe32s(f, &v2);
578 if (*v == v2) {
579 return 0;
581 error_report("%" PRIx32 " != %" PRIx32, *v, v2);
582 return -EINVAL;
585 const VMStateInfo vmstate_info_int32_equal = {
586 .name = "int32 equal",
587 .get = get_int32_equal,
588 .put = put_int32,
591 /* 32 bit int. Check that the received value is non-negative
592 * and less than or equal to the one in the field.
595 static int get_int32_le(QEMUFile *f, void *pv, size_t size, VMStateField *field)
597 int32_t *cur = pv;
598 int32_t loaded;
599 qemu_get_sbe32s(f, &loaded);
601 if (loaded >= 0 && loaded <= *cur) {
602 *cur = loaded;
603 return 0;
605 error_report("Invalid value %" PRId32
606 " expecting positive value <= %" PRId32,
607 loaded, *cur);
608 return -EINVAL;
611 const VMStateInfo vmstate_info_int32_le = {
612 .name = "int32 le",
613 .get = get_int32_le,
614 .put = put_int32,
617 /* 64 bit int */
619 static int get_int64(QEMUFile *f, void *pv, size_t size, VMStateField *field)
621 int64_t *v = pv;
622 qemu_get_sbe64s(f, v);
623 return 0;
626 static int put_int64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
627 QJSON *vmdesc)
629 int64_t *v = pv;
630 qemu_put_sbe64s(f, v);
631 return 0;
634 const VMStateInfo vmstate_info_int64 = {
635 .name = "int64",
636 .get = get_int64,
637 .put = put_int64,
640 /* 8 bit unsigned int */
642 static int get_uint8(QEMUFile *f, void *pv, size_t size, VMStateField *field)
644 uint8_t *v = pv;
645 qemu_get_8s(f, v);
646 return 0;
649 static int put_uint8(QEMUFile *f, void *pv, size_t size, VMStateField *field,
650 QJSON *vmdesc)
652 uint8_t *v = pv;
653 qemu_put_8s(f, v);
654 return 0;
657 const VMStateInfo vmstate_info_uint8 = {
658 .name = "uint8",
659 .get = get_uint8,
660 .put = put_uint8,
663 /* 16 bit unsigned int */
665 static int get_uint16(QEMUFile *f, void *pv, size_t size, VMStateField *field)
667 uint16_t *v = pv;
668 qemu_get_be16s(f, v);
669 return 0;
672 static int put_uint16(QEMUFile *f, void *pv, size_t size, VMStateField *field,
673 QJSON *vmdesc)
675 uint16_t *v = pv;
676 qemu_put_be16s(f, v);
677 return 0;
680 const VMStateInfo vmstate_info_uint16 = {
681 .name = "uint16",
682 .get = get_uint16,
683 .put = put_uint16,
686 /* 32 bit unsigned int */
688 static int get_uint32(QEMUFile *f, void *pv, size_t size, VMStateField *field)
690 uint32_t *v = pv;
691 qemu_get_be32s(f, v);
692 return 0;
695 static int put_uint32(QEMUFile *f, void *pv, size_t size, VMStateField *field,
696 QJSON *vmdesc)
698 uint32_t *v = pv;
699 qemu_put_be32s(f, v);
700 return 0;
703 const VMStateInfo vmstate_info_uint32 = {
704 .name = "uint32",
705 .get = get_uint32,
706 .put = put_uint32,
709 /* 32 bit uint. See that the received value is the same than the one
710 in the field */
712 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size,
713 VMStateField *field)
715 uint32_t *v = pv;
716 uint32_t v2;
717 qemu_get_be32s(f, &v2);
719 if (*v == v2) {
720 return 0;
722 error_report("%" PRIx32 " != %" PRIx32, *v, v2);
723 return -EINVAL;
726 const VMStateInfo vmstate_info_uint32_equal = {
727 .name = "uint32 equal",
728 .get = get_uint32_equal,
729 .put = put_uint32,
732 /* 64 bit unsigned int */
734 static int get_uint64(QEMUFile *f, void *pv, size_t size, VMStateField *field)
736 uint64_t *v = pv;
737 qemu_get_be64s(f, v);
738 return 0;
741 static int put_uint64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
742 QJSON *vmdesc)
744 uint64_t *v = pv;
745 qemu_put_be64s(f, v);
746 return 0;
749 const VMStateInfo vmstate_info_uint64 = {
750 .name = "uint64",
751 .get = get_uint64,
752 .put = put_uint64,
755 /* 64 bit unsigned int. See that the received value is the same than the one
756 in the field */
758 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size,
759 VMStateField *field)
761 uint64_t *v = pv;
762 uint64_t v2;
763 qemu_get_be64s(f, &v2);
765 if (*v == v2) {
766 return 0;
768 error_report("%" PRIx64 " != %" PRIx64, *v, v2);
769 return -EINVAL;
772 const VMStateInfo vmstate_info_uint64_equal = {
773 .name = "int64 equal",
774 .get = get_uint64_equal,
775 .put = put_uint64,
778 /* 8 bit int. See that the received value is the same than the one
779 in the field */
781 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size,
782 VMStateField *field)
784 uint8_t *v = pv;
785 uint8_t v2;
786 qemu_get_8s(f, &v2);
788 if (*v == v2) {
789 return 0;
791 error_report("%x != %x", *v, v2);
792 return -EINVAL;
795 const VMStateInfo vmstate_info_uint8_equal = {
796 .name = "uint8 equal",
797 .get = get_uint8_equal,
798 .put = put_uint8,
801 /* 16 bit unsigned int int. See that the received value is the same than the one
802 in the field */
804 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size,
805 VMStateField *field)
807 uint16_t *v = pv;
808 uint16_t v2;
809 qemu_get_be16s(f, &v2);
811 if (*v == v2) {
812 return 0;
814 error_report("%x != %x", *v, v2);
815 return -EINVAL;
818 const VMStateInfo vmstate_info_uint16_equal = {
819 .name = "uint16 equal",
820 .get = get_uint16_equal,
821 .put = put_uint16,
824 /* floating point */
826 static int get_float64(QEMUFile *f, void *pv, size_t size,
827 VMStateField *field)
829 float64 *v = pv;
831 *v = make_float64(qemu_get_be64(f));
832 return 0;
835 static int put_float64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
836 QJSON *vmdesc)
838 uint64_t *v = pv;
840 qemu_put_be64(f, float64_val(*v));
841 return 0;
844 const VMStateInfo vmstate_info_float64 = {
845 .name = "float64",
846 .get = get_float64,
847 .put = put_float64,
850 /* CPU_DoubleU type */
852 static int get_cpudouble(QEMUFile *f, void *pv, size_t size,
853 VMStateField *field)
855 CPU_DoubleU *v = pv;
856 qemu_get_be32s(f, &v->l.upper);
857 qemu_get_be32s(f, &v->l.lower);
858 return 0;
861 static int put_cpudouble(QEMUFile *f, void *pv, size_t size,
862 VMStateField *field, QJSON *vmdesc)
864 CPU_DoubleU *v = pv;
865 qemu_put_be32s(f, &v->l.upper);
866 qemu_put_be32s(f, &v->l.lower);
867 return 0;
870 const VMStateInfo vmstate_info_cpudouble = {
871 .name = "CPU_Double_U",
872 .get = get_cpudouble,
873 .put = put_cpudouble,
876 /* uint8_t buffers */
878 static int get_buffer(QEMUFile *f, void *pv, size_t size,
879 VMStateField *field)
881 uint8_t *v = pv;
882 qemu_get_buffer(f, v, size);
883 return 0;
886 static int put_buffer(QEMUFile *f, void *pv, size_t size, VMStateField *field,
887 QJSON *vmdesc)
889 uint8_t *v = pv;
890 qemu_put_buffer(f, v, size);
891 return 0;
894 const VMStateInfo vmstate_info_buffer = {
895 .name = "buffer",
896 .get = get_buffer,
897 .put = put_buffer,
900 /* unused buffers: space that was used for some fields that are
901 not useful anymore */
903 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size,
904 VMStateField *field)
906 uint8_t buf[1024];
907 int block_len;
909 while (size > 0) {
910 block_len = MIN(sizeof(buf), size);
911 size -= block_len;
912 qemu_get_buffer(f, buf, block_len);
914 return 0;
917 static int put_unused_buffer(QEMUFile *f, void *pv, size_t size,
918 VMStateField *field, QJSON *vmdesc)
920 static const uint8_t buf[1024];
921 int block_len;
923 while (size > 0) {
924 block_len = MIN(sizeof(buf), size);
925 size -= block_len;
926 qemu_put_buffer(f, buf, block_len);
929 return 0;
932 const VMStateInfo vmstate_info_unused_buffer = {
933 .name = "unused_buffer",
934 .get = get_unused_buffer,
935 .put = put_unused_buffer,
938 /* vmstate_info_tmp, see VMSTATE_WITH_TMP, the idea is that we allocate
939 * a temporary buffer and the pre_load/pre_save methods in the child vmsd
940 * copy stuff from the parent into the child and do calculations to fill
941 * in fields that don't really exist in the parent but need to be in the
942 * stream.
944 static int get_tmp(QEMUFile *f, void *pv, size_t size, VMStateField *field)
946 int ret;
947 const VMStateDescription *vmsd = field->vmsd;
948 int version_id = field->version_id;
949 void *tmp = g_malloc(size);
951 /* Writes the parent field which is at the start of the tmp */
952 *(void **)tmp = pv;
953 ret = vmstate_load_state(f, vmsd, tmp, version_id);
954 g_free(tmp);
955 return ret;
958 static int put_tmp(QEMUFile *f, void *pv, size_t size, VMStateField *field,
959 QJSON *vmdesc)
961 const VMStateDescription *vmsd = field->vmsd;
962 void *tmp = g_malloc(size);
964 /* Writes the parent field which is at the start of the tmp */
965 *(void **)tmp = pv;
966 vmstate_save_state(f, vmsd, tmp, vmdesc);
967 g_free(tmp);
969 return 0;
972 const VMStateInfo vmstate_info_tmp = {
973 .name = "tmp",
974 .get = get_tmp,
975 .put = put_tmp,
978 /* bitmaps (as defined by bitmap.h). Note that size here is the size
979 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
980 * bit words with the bits in big endian order. The in-memory format
981 * is an array of 'unsigned long', which may be either 32 or 64 bits.
983 /* This is the number of 64 bit words sent over the wire */
984 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
985 static int get_bitmap(QEMUFile *f, void *pv, size_t size, VMStateField *field)
987 unsigned long *bmp = pv;
988 int i, idx = 0;
989 for (i = 0; i < BITS_TO_U64S(size); i++) {
990 uint64_t w = qemu_get_be64(f);
991 bmp[idx++] = w;
992 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
993 bmp[idx++] = w >> 32;
996 return 0;
999 static int put_bitmap(QEMUFile *f, void *pv, size_t size, VMStateField *field,
1000 QJSON *vmdesc)
1002 unsigned long *bmp = pv;
1003 int i, idx = 0;
1004 for (i = 0; i < BITS_TO_U64S(size); i++) {
1005 uint64_t w = bmp[idx++];
1006 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1007 w |= ((uint64_t)bmp[idx++]) << 32;
1009 qemu_put_be64(f, w);
1012 return 0;
1015 const VMStateInfo vmstate_info_bitmap = {
1016 .name = "bitmap",
1017 .get = get_bitmap,
1018 .put = put_bitmap,
1021 /* get for QTAILQ
1022 * meta data about the QTAILQ is encoded in a VMStateField structure
1024 static int get_qtailq(QEMUFile *f, void *pv, size_t unused_size,
1025 VMStateField *field)
1027 int ret = 0;
1028 const VMStateDescription *vmsd = field->vmsd;
1029 /* size of a QTAILQ element */
1030 size_t size = field->size;
1031 /* offset of the QTAILQ entry in a QTAILQ element */
1032 size_t entry_offset = field->start;
1033 int version_id = field->version_id;
1034 void *elm;
1036 trace_get_qtailq(vmsd->name, version_id);
1037 if (version_id > vmsd->version_id) {
1038 error_report("%s %s", vmsd->name, "too new");
1039 trace_get_qtailq_end(vmsd->name, "too new", -EINVAL);
1041 return -EINVAL;
1043 if (version_id < vmsd->minimum_version_id) {
1044 error_report("%s %s", vmsd->name, "too old");
1045 trace_get_qtailq_end(vmsd->name, "too old", -EINVAL);
1046 return -EINVAL;
1049 while (qemu_get_byte(f)) {
1050 elm = g_malloc(size);
1051 ret = vmstate_load_state(f, vmsd, elm, version_id);
1052 if (ret) {
1053 return ret;
1055 QTAILQ_RAW_INSERT_TAIL(pv, elm, entry_offset);
1058 trace_get_qtailq_end(vmsd->name, "end", ret);
1059 return ret;
1062 /* put for QTAILQ */
1063 static int put_qtailq(QEMUFile *f, void *pv, size_t unused_size,
1064 VMStateField *field, QJSON *vmdesc)
1066 const VMStateDescription *vmsd = field->vmsd;
1067 /* offset of the QTAILQ entry in a QTAILQ element*/
1068 size_t entry_offset = field->start;
1069 void *elm;
1071 trace_put_qtailq(vmsd->name, vmsd->version_id);
1073 QTAILQ_RAW_FOREACH(elm, pv, entry_offset) {
1074 qemu_put_byte(f, true);
1075 vmstate_save_state(f, vmsd, elm, vmdesc);
1077 qemu_put_byte(f, false);
1079 trace_put_qtailq_end(vmsd->name, "end");
1081 return 0;
1083 const VMStateInfo vmstate_info_qtailq = {
1084 .name = "qtailq",
1085 .get = get_qtailq,
1086 .put = put_qtailq,