virtio-blk: Rename complete_request_early to complete_request_vring
[qemu/ar7.git] / vmstate.c
blobc9965205dfd608ea4b8eaddd93671fcc4ead584c
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"
6 #include "trace.h"
8 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
9 void *opaque);
10 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
11 void *opaque);
13 static int vmstate_n_elems(void *opaque, VMStateField *field)
15 int n_elems = 1;
17 if (field->flags & VMS_ARRAY) {
18 n_elems = field->num;
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);
29 return n_elems;
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) {
39 size *= field->size;
43 return size;
46 static void *vmstate_base_addr(void *opaque, VMStateField *field)
48 void *base_addr = opaque + field->offset;
50 if (field->flags & VMS_POINTER) {
51 base_addr = *(void **)base_addr + field->start;
54 return base_addr;
57 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
58 void *opaque, int version_id)
60 VMStateField *field = vmsd->fields;
61 int ret;
63 if (version_id > vmsd->version_id) {
64 return -EINVAL;
66 if (version_id < vmsd->minimum_version_id) {
67 if (vmsd->load_state_old &&
68 version_id >= vmsd->minimum_version_id_old) {
69 return vmsd->load_state_old(f, opaque, version_id);
71 return -EINVAL;
73 if (vmsd->pre_load) {
74 int ret = vmsd->pre_load(opaque);
75 if (ret) {
76 return ret;
79 while (field->name) {
80 if ((field->field_exists &&
81 field->field_exists(opaque, version_id)) ||
82 (!field->field_exists &&
83 field->version_id <= version_id)) {
84 void *base_addr = vmstate_base_addr(opaque, field);
85 int i, n_elems = vmstate_n_elems(opaque, field);
86 int size = vmstate_size(opaque, field);
88 for (i = 0; i < n_elems; i++) {
89 void *addr = base_addr + size * i;
91 if (field->flags & VMS_ARRAY_OF_POINTER) {
92 addr = *(void **)addr;
94 if (field->flags & VMS_STRUCT) {
95 ret = vmstate_load_state(f, field->vmsd, addr,
96 field->vmsd->version_id);
97 } else {
98 ret = field->info->get(f, addr, size);
101 if (ret >= 0) {
102 ret = qemu_file_get_error(f);
104 if (ret < 0) {
105 qemu_file_set_error(f, ret);
106 trace_vmstate_load_field_error(field->name, ret);
107 return ret;
110 } else if (field->flags & VMS_MUST_EXIST) {
111 fprintf(stderr, "Input validation failed: %s/%s\n",
112 vmsd->name, field->name);
113 return -1;
115 field++;
117 ret = vmstate_subsection_load(f, vmsd, opaque);
118 if (ret != 0) {
119 return ret;
121 if (vmsd->post_load) {
122 return vmsd->post_load(opaque, version_id);
124 return 0;
127 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
128 void *opaque)
130 VMStateField *field = vmsd->fields;
132 if (vmsd->pre_save) {
133 vmsd->pre_save(opaque);
135 while (field->name) {
136 if (!field->field_exists ||
137 field->field_exists(opaque, vmsd->version_id)) {
138 void *base_addr = vmstate_base_addr(opaque, field);
139 int i, n_elems = vmstate_n_elems(opaque, field);
140 int size = vmstate_size(opaque, field);
142 for (i = 0; i < n_elems; i++) {
143 void *addr = base_addr + size * i;
145 if (field->flags & VMS_ARRAY_OF_POINTER) {
146 addr = *(void **)addr;
148 if (field->flags & VMS_STRUCT) {
149 vmstate_save_state(f, field->vmsd, addr);
150 } else {
151 field->info->put(f, addr, size);
154 } else {
155 if (field->flags & VMS_MUST_EXIST) {
156 fprintf(stderr, "Output state validation failed: %s/%s\n",
157 vmsd->name, field->name);
158 assert(!(field->flags & VMS_MUST_EXIST));
161 field++;
163 vmstate_subsection_save(f, vmsd, opaque);
166 static const VMStateDescription *
167 vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
169 while (sub && sub->needed) {
170 if (strcmp(idstr, sub->vmsd->name) == 0) {
171 return sub->vmsd;
173 sub++;
175 return NULL;
178 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
179 void *opaque)
181 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
182 char idstr[256];
183 int ret;
184 uint8_t version_id, len, size;
185 const VMStateDescription *sub_vmsd;
187 len = qemu_peek_byte(f, 1);
188 if (len < strlen(vmsd->name) + 1) {
189 /* subsection name has be be "section_name/a" */
190 return 0;
192 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
193 if (size != len) {
194 return 0;
196 idstr[size] = 0;
198 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
199 /* it don't have a valid subsection name */
200 return 0;
202 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
203 if (sub_vmsd == NULL) {
204 return -ENOENT;
206 qemu_file_skip(f, 1); /* subsection */
207 qemu_file_skip(f, 1); /* len */
208 qemu_file_skip(f, len); /* idstr */
209 version_id = qemu_get_be32(f);
211 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
212 if (ret) {
213 return ret;
216 return 0;
219 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
220 void *opaque)
222 const VMStateSubsection *sub = vmsd->subsections;
224 while (sub && sub->needed) {
225 if (sub->needed(opaque)) {
226 const VMStateDescription *vmsd = sub->vmsd;
227 uint8_t len;
229 qemu_put_byte(f, QEMU_VM_SUBSECTION);
230 len = strlen(vmsd->name);
231 qemu_put_byte(f, len);
232 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
233 qemu_put_be32(f, vmsd->version_id);
234 vmstate_save_state(f, vmsd, opaque);
236 sub++;
240 /* bool */
242 static int get_bool(QEMUFile *f, void *pv, size_t size)
244 bool *v = pv;
245 *v = qemu_get_byte(f);
246 return 0;
249 static void put_bool(QEMUFile *f, void *pv, size_t size)
251 bool *v = pv;
252 qemu_put_byte(f, *v);
255 const VMStateInfo vmstate_info_bool = {
256 .name = "bool",
257 .get = get_bool,
258 .put = put_bool,
261 /* 8 bit int */
263 static int get_int8(QEMUFile *f, void *pv, size_t size)
265 int8_t *v = pv;
266 qemu_get_s8s(f, v);
267 return 0;
270 static void put_int8(QEMUFile *f, void *pv, size_t size)
272 int8_t *v = pv;
273 qemu_put_s8s(f, v);
276 const VMStateInfo vmstate_info_int8 = {
277 .name = "int8",
278 .get = get_int8,
279 .put = put_int8,
282 /* 16 bit int */
284 static int get_int16(QEMUFile *f, void *pv, size_t size)
286 int16_t *v = pv;
287 qemu_get_sbe16s(f, v);
288 return 0;
291 static void put_int16(QEMUFile *f, void *pv, size_t size)
293 int16_t *v = pv;
294 qemu_put_sbe16s(f, v);
297 const VMStateInfo vmstate_info_int16 = {
298 .name = "int16",
299 .get = get_int16,
300 .put = put_int16,
303 /* 32 bit int */
305 static int get_int32(QEMUFile *f, void *pv, size_t size)
307 int32_t *v = pv;
308 qemu_get_sbe32s(f, v);
309 return 0;
312 static void put_int32(QEMUFile *f, void *pv, size_t size)
314 int32_t *v = pv;
315 qemu_put_sbe32s(f, v);
318 const VMStateInfo vmstate_info_int32 = {
319 .name = "int32",
320 .get = get_int32,
321 .put = put_int32,
324 /* 32 bit int. See that the received value is the same than the one
325 in the field */
327 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
329 int32_t *v = pv;
330 int32_t v2;
331 qemu_get_sbe32s(f, &v2);
333 if (*v == v2) {
334 return 0;
336 return -EINVAL;
339 const VMStateInfo vmstate_info_int32_equal = {
340 .name = "int32 equal",
341 .get = get_int32_equal,
342 .put = put_int32,
345 /* 32 bit int. Check that the received value is non-negative
346 * and less than or equal to the one in the field.
349 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
351 int32_t *cur = pv;
352 int32_t loaded;
353 qemu_get_sbe32s(f, &loaded);
355 if (loaded >= 0 && loaded <= *cur) {
356 *cur = loaded;
357 return 0;
359 return -EINVAL;
362 const VMStateInfo vmstate_info_int32_le = {
363 .name = "int32 le",
364 .get = get_int32_le,
365 .put = put_int32,
368 /* 64 bit int */
370 static int get_int64(QEMUFile *f, void *pv, size_t size)
372 int64_t *v = pv;
373 qemu_get_sbe64s(f, v);
374 return 0;
377 static void put_int64(QEMUFile *f, void *pv, size_t size)
379 int64_t *v = pv;
380 qemu_put_sbe64s(f, v);
383 const VMStateInfo vmstate_info_int64 = {
384 .name = "int64",
385 .get = get_int64,
386 .put = put_int64,
389 /* 8 bit unsigned int */
391 static int get_uint8(QEMUFile *f, void *pv, size_t size)
393 uint8_t *v = pv;
394 qemu_get_8s(f, v);
395 return 0;
398 static void put_uint8(QEMUFile *f, void *pv, size_t size)
400 uint8_t *v = pv;
401 qemu_put_8s(f, v);
404 const VMStateInfo vmstate_info_uint8 = {
405 .name = "uint8",
406 .get = get_uint8,
407 .put = put_uint8,
410 /* 16 bit unsigned int */
412 static int get_uint16(QEMUFile *f, void *pv, size_t size)
414 uint16_t *v = pv;
415 qemu_get_be16s(f, v);
416 return 0;
419 static void put_uint16(QEMUFile *f, void *pv, size_t size)
421 uint16_t *v = pv;
422 qemu_put_be16s(f, v);
425 const VMStateInfo vmstate_info_uint16 = {
426 .name = "uint16",
427 .get = get_uint16,
428 .put = put_uint16,
431 /* 32 bit unsigned int */
433 static int get_uint32(QEMUFile *f, void *pv, size_t size)
435 uint32_t *v = pv;
436 qemu_get_be32s(f, v);
437 return 0;
440 static void put_uint32(QEMUFile *f, void *pv, size_t size)
442 uint32_t *v = pv;
443 qemu_put_be32s(f, v);
446 const VMStateInfo vmstate_info_uint32 = {
447 .name = "uint32",
448 .get = get_uint32,
449 .put = put_uint32,
452 /* 32 bit uint. See that the received value is the same than the one
453 in the field */
455 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
457 uint32_t *v = pv;
458 uint32_t v2;
459 qemu_get_be32s(f, &v2);
461 if (*v == v2) {
462 return 0;
464 return -EINVAL;
467 const VMStateInfo vmstate_info_uint32_equal = {
468 .name = "uint32 equal",
469 .get = get_uint32_equal,
470 .put = put_uint32,
473 /* 64 bit unsigned int */
475 static int get_uint64(QEMUFile *f, void *pv, size_t size)
477 uint64_t *v = pv;
478 qemu_get_be64s(f, v);
479 return 0;
482 static void put_uint64(QEMUFile *f, void *pv, size_t size)
484 uint64_t *v = pv;
485 qemu_put_be64s(f, v);
488 const VMStateInfo vmstate_info_uint64 = {
489 .name = "uint64",
490 .get = get_uint64,
491 .put = put_uint64,
494 /* 64 bit unsigned int. See that the received value is the same than the one
495 in the field */
497 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
499 uint64_t *v = pv;
500 uint64_t v2;
501 qemu_get_be64s(f, &v2);
503 if (*v == v2) {
504 return 0;
506 return -EINVAL;
509 const VMStateInfo vmstate_info_uint64_equal = {
510 .name = "int64 equal",
511 .get = get_uint64_equal,
512 .put = put_uint64,
515 /* 8 bit int. See that the received value is the same than the one
516 in the field */
518 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
520 uint8_t *v = pv;
521 uint8_t v2;
522 qemu_get_8s(f, &v2);
524 if (*v == v2) {
525 return 0;
527 return -EINVAL;
530 const VMStateInfo vmstate_info_uint8_equal = {
531 .name = "uint8 equal",
532 .get = get_uint8_equal,
533 .put = put_uint8,
536 /* 16 bit unsigned int int. See that the received value is the same than the one
537 in the field */
539 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
541 uint16_t *v = pv;
542 uint16_t v2;
543 qemu_get_be16s(f, &v2);
545 if (*v == v2) {
546 return 0;
548 return -EINVAL;
551 const VMStateInfo vmstate_info_uint16_equal = {
552 .name = "uint16 equal",
553 .get = get_uint16_equal,
554 .put = put_uint16,
557 /* floating point */
559 static int get_float64(QEMUFile *f, void *pv, size_t size)
561 float64 *v = pv;
563 *v = make_float64(qemu_get_be64(f));
564 return 0;
567 static void put_float64(QEMUFile *f, void *pv, size_t size)
569 uint64_t *v = pv;
571 qemu_put_be64(f, float64_val(*v));
574 const VMStateInfo vmstate_info_float64 = {
575 .name = "float64",
576 .get = get_float64,
577 .put = put_float64,
580 /* uint8_t buffers */
582 static int get_buffer(QEMUFile *f, void *pv, size_t size)
584 uint8_t *v = pv;
585 qemu_get_buffer(f, v, size);
586 return 0;
589 static void put_buffer(QEMUFile *f, void *pv, size_t size)
591 uint8_t *v = pv;
592 qemu_put_buffer(f, v, size);
595 const VMStateInfo vmstate_info_buffer = {
596 .name = "buffer",
597 .get = get_buffer,
598 .put = put_buffer,
601 /* unused buffers: space that was used for some fields that are
602 not useful anymore */
604 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
606 uint8_t buf[1024];
607 int block_len;
609 while (size > 0) {
610 block_len = MIN(sizeof(buf), size);
611 size -= block_len;
612 qemu_get_buffer(f, buf, block_len);
614 return 0;
617 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
619 static const uint8_t buf[1024];
620 int block_len;
622 while (size > 0) {
623 block_len = MIN(sizeof(buf), size);
624 size -= block_len;
625 qemu_put_buffer(f, buf, block_len);
629 const VMStateInfo vmstate_info_unused_buffer = {
630 .name = "unused_buffer",
631 .get = get_unused_buffer,
632 .put = put_unused_buffer,
635 /* bitmaps (as defined by bitmap.h). Note that size here is the size
636 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
637 * bit words with the bits in big endian order. The in-memory format
638 * is an array of 'unsigned long', which may be either 32 or 64 bits.
640 /* This is the number of 64 bit words sent over the wire */
641 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
642 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
644 unsigned long *bmp = pv;
645 int i, idx = 0;
646 for (i = 0; i < BITS_TO_U64S(size); i++) {
647 uint64_t w = qemu_get_be64(f);
648 bmp[idx++] = w;
649 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
650 bmp[idx++] = w >> 32;
653 return 0;
656 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
658 unsigned long *bmp = pv;
659 int i, idx = 0;
660 for (i = 0; i < BITS_TO_U64S(size); i++) {
661 uint64_t w = bmp[idx++];
662 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
663 w |= ((uint64_t)bmp[idx++]) << 32;
665 qemu_put_be64(f, w);
669 const VMStateInfo vmstate_info_bitmap = {
670 .name = "bitmap",
671 .get = get_bitmap,
672 .put = put_bitmap,