Merge remote-tracking branch 'remotes/lalrae/tags/mips-20141103' into staging
[qemu.git] / vmstate.c
blob3dde574c0fac10022fb8a44044b72d460435ad1e
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, bool alloc)
48 void *base_addr = opaque + field->offset;
50 if (field->flags & VMS_POINTER) {
51 if (alloc && (field->flags & VMS_ALLOC)) {
52 gsize size = 0;
53 if (field->flags & VMS_VBUFFER) {
54 size = vmstate_size(opaque, field);
55 } else {
56 int n_elems = vmstate_n_elems(opaque, field);
57 if (n_elems) {
58 size = n_elems * field->size;
61 if (size) {
62 *((void **)base_addr + field->start) = g_malloc(size);
65 base_addr = *(void **)base_addr + field->start;
68 return base_addr;
71 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
72 void *opaque, int version_id)
74 VMStateField *field = vmsd->fields;
75 int ret;
77 if (version_id > vmsd->version_id) {
78 return -EINVAL;
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);
85 return -EINVAL;
87 if (vmsd->pre_load) {
88 int ret = vmsd->pre_load(opaque);
89 if (ret) {
90 return ret;
93 while (field->name) {
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);
111 } else {
112 ret = field->info->get(f, addr, size);
115 if (ret >= 0) {
116 ret = qemu_file_get_error(f);
118 if (ret < 0) {
119 qemu_file_set_error(f, ret);
120 trace_vmstate_load_field_error(field->name, ret);
121 return ret;
124 } else if (field->flags & VMS_MUST_EXIST) {
125 fprintf(stderr, "Input validation failed: %s/%s\n",
126 vmsd->name, field->name);
127 return -1;
129 field++;
131 ret = vmstate_subsection_load(f, vmsd, opaque);
132 if (ret != 0) {
133 return ret;
135 if (vmsd->post_load) {
136 return vmsd->post_load(opaque, version_id);
138 return 0;
141 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
142 void *opaque)
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);
164 } else {
165 field->info->put(f, addr, size);
168 } else {
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));
175 field++;
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) {
185 return sub->vmsd;
187 sub++;
189 return NULL;
192 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
193 void *opaque)
195 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
196 char idstr[256];
197 int ret;
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" */
204 return 0;
206 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
207 if (size != len) {
208 return 0;
210 idstr[size] = 0;
212 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
213 /* it don't have a valid subsection name */
214 return 0;
216 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
217 if (sub_vmsd == NULL) {
218 return -ENOENT;
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);
226 if (ret) {
227 return ret;
230 return 0;
233 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
234 void *opaque)
236 const VMStateSubsection *sub = vmsd->subsections;
238 while (sub && sub->needed) {
239 if (sub->needed(opaque)) {
240 const VMStateDescription *vmsd = sub->vmsd;
241 uint8_t len;
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);
250 sub++;
254 /* bool */
256 static int get_bool(QEMUFile *f, void *pv, size_t size)
258 bool *v = pv;
259 *v = qemu_get_byte(f);
260 return 0;
263 static void put_bool(QEMUFile *f, void *pv, size_t size)
265 bool *v = pv;
266 qemu_put_byte(f, *v);
269 const VMStateInfo vmstate_info_bool = {
270 .name = "bool",
271 .get = get_bool,
272 .put = put_bool,
275 /* 8 bit int */
277 static int get_int8(QEMUFile *f, void *pv, size_t size)
279 int8_t *v = pv;
280 qemu_get_s8s(f, v);
281 return 0;
284 static void put_int8(QEMUFile *f, void *pv, size_t size)
286 int8_t *v = pv;
287 qemu_put_s8s(f, v);
290 const VMStateInfo vmstate_info_int8 = {
291 .name = "int8",
292 .get = get_int8,
293 .put = put_int8,
296 /* 16 bit int */
298 static int get_int16(QEMUFile *f, void *pv, size_t size)
300 int16_t *v = pv;
301 qemu_get_sbe16s(f, v);
302 return 0;
305 static void put_int16(QEMUFile *f, void *pv, size_t size)
307 int16_t *v = pv;
308 qemu_put_sbe16s(f, v);
311 const VMStateInfo vmstate_info_int16 = {
312 .name = "int16",
313 .get = get_int16,
314 .put = put_int16,
317 /* 32 bit int */
319 static int get_int32(QEMUFile *f, void *pv, size_t size)
321 int32_t *v = pv;
322 qemu_get_sbe32s(f, v);
323 return 0;
326 static void put_int32(QEMUFile *f, void *pv, size_t size)
328 int32_t *v = pv;
329 qemu_put_sbe32s(f, v);
332 const VMStateInfo vmstate_info_int32 = {
333 .name = "int32",
334 .get = get_int32,
335 .put = put_int32,
338 /* 32 bit int. See that the received value is the same than the one
339 in the field */
341 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
343 int32_t *v = pv;
344 int32_t v2;
345 qemu_get_sbe32s(f, &v2);
347 if (*v == v2) {
348 return 0;
350 return -EINVAL;
353 const VMStateInfo vmstate_info_int32_equal = {
354 .name = "int32 equal",
355 .get = get_int32_equal,
356 .put = put_int32,
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)
365 int32_t *cur = pv;
366 int32_t loaded;
367 qemu_get_sbe32s(f, &loaded);
369 if (loaded >= 0 && loaded <= *cur) {
370 *cur = loaded;
371 return 0;
373 return -EINVAL;
376 const VMStateInfo vmstate_info_int32_le = {
377 .name = "int32 le",
378 .get = get_int32_le,
379 .put = put_int32,
382 /* 64 bit int */
384 static int get_int64(QEMUFile *f, void *pv, size_t size)
386 int64_t *v = pv;
387 qemu_get_sbe64s(f, v);
388 return 0;
391 static void put_int64(QEMUFile *f, void *pv, size_t size)
393 int64_t *v = pv;
394 qemu_put_sbe64s(f, v);
397 const VMStateInfo vmstate_info_int64 = {
398 .name = "int64",
399 .get = get_int64,
400 .put = put_int64,
403 /* 8 bit unsigned int */
405 static int get_uint8(QEMUFile *f, void *pv, size_t size)
407 uint8_t *v = pv;
408 qemu_get_8s(f, v);
409 return 0;
412 static void put_uint8(QEMUFile *f, void *pv, size_t size)
414 uint8_t *v = pv;
415 qemu_put_8s(f, v);
418 const VMStateInfo vmstate_info_uint8 = {
419 .name = "uint8",
420 .get = get_uint8,
421 .put = put_uint8,
424 /* 16 bit unsigned int */
426 static int get_uint16(QEMUFile *f, void *pv, size_t size)
428 uint16_t *v = pv;
429 qemu_get_be16s(f, v);
430 return 0;
433 static void put_uint16(QEMUFile *f, void *pv, size_t size)
435 uint16_t *v = pv;
436 qemu_put_be16s(f, v);
439 const VMStateInfo vmstate_info_uint16 = {
440 .name = "uint16",
441 .get = get_uint16,
442 .put = put_uint16,
445 /* 32 bit unsigned int */
447 static int get_uint32(QEMUFile *f, void *pv, size_t size)
449 uint32_t *v = pv;
450 qemu_get_be32s(f, v);
451 return 0;
454 static void put_uint32(QEMUFile *f, void *pv, size_t size)
456 uint32_t *v = pv;
457 qemu_put_be32s(f, v);
460 const VMStateInfo vmstate_info_uint32 = {
461 .name = "uint32",
462 .get = get_uint32,
463 .put = put_uint32,
466 /* 32 bit uint. See that the received value is the same than the one
467 in the field */
469 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
471 uint32_t *v = pv;
472 uint32_t v2;
473 qemu_get_be32s(f, &v2);
475 if (*v == v2) {
476 return 0;
478 return -EINVAL;
481 const VMStateInfo vmstate_info_uint32_equal = {
482 .name = "uint32 equal",
483 .get = get_uint32_equal,
484 .put = put_uint32,
487 /* 64 bit unsigned int */
489 static int get_uint64(QEMUFile *f, void *pv, size_t size)
491 uint64_t *v = pv;
492 qemu_get_be64s(f, v);
493 return 0;
496 static void put_uint64(QEMUFile *f, void *pv, size_t size)
498 uint64_t *v = pv;
499 qemu_put_be64s(f, v);
502 const VMStateInfo vmstate_info_uint64 = {
503 .name = "uint64",
504 .get = get_uint64,
505 .put = put_uint64,
508 /* 64 bit unsigned int. See that the received value is the same than the one
509 in the field */
511 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
513 uint64_t *v = pv;
514 uint64_t v2;
515 qemu_get_be64s(f, &v2);
517 if (*v == v2) {
518 return 0;
520 return -EINVAL;
523 const VMStateInfo vmstate_info_uint64_equal = {
524 .name = "int64 equal",
525 .get = get_uint64_equal,
526 .put = put_uint64,
529 /* 8 bit int. See that the received value is the same than the one
530 in the field */
532 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
534 uint8_t *v = pv;
535 uint8_t v2;
536 qemu_get_8s(f, &v2);
538 if (*v == v2) {
539 return 0;
541 return -EINVAL;
544 const VMStateInfo vmstate_info_uint8_equal = {
545 .name = "uint8 equal",
546 .get = get_uint8_equal,
547 .put = put_uint8,
550 /* 16 bit unsigned int int. See that the received value is the same than the one
551 in the field */
553 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
555 uint16_t *v = pv;
556 uint16_t v2;
557 qemu_get_be16s(f, &v2);
559 if (*v == v2) {
560 return 0;
562 return -EINVAL;
565 const VMStateInfo vmstate_info_uint16_equal = {
566 .name = "uint16 equal",
567 .get = get_uint16_equal,
568 .put = put_uint16,
571 /* floating point */
573 static int get_float64(QEMUFile *f, void *pv, size_t size)
575 float64 *v = pv;
577 *v = make_float64(qemu_get_be64(f));
578 return 0;
581 static void put_float64(QEMUFile *f, void *pv, size_t size)
583 uint64_t *v = pv;
585 qemu_put_be64(f, float64_val(*v));
588 const VMStateInfo vmstate_info_float64 = {
589 .name = "float64",
590 .get = get_float64,
591 .put = put_float64,
594 /* uint8_t buffers */
596 static int get_buffer(QEMUFile *f, void *pv, size_t size)
598 uint8_t *v = pv;
599 qemu_get_buffer(f, v, size);
600 return 0;
603 static void put_buffer(QEMUFile *f, void *pv, size_t size)
605 uint8_t *v = pv;
606 qemu_put_buffer(f, v, size);
609 const VMStateInfo vmstate_info_buffer = {
610 .name = "buffer",
611 .get = get_buffer,
612 .put = put_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)
620 uint8_t buf[1024];
621 int block_len;
623 while (size > 0) {
624 block_len = MIN(sizeof(buf), size);
625 size -= block_len;
626 qemu_get_buffer(f, buf, block_len);
628 return 0;
631 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
633 static const uint8_t buf[1024];
634 int block_len;
636 while (size > 0) {
637 block_len = MIN(sizeof(buf), size);
638 size -= block_len;
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;
659 int i, idx = 0;
660 for (i = 0; i < BITS_TO_U64S(size); i++) {
661 uint64_t w = qemu_get_be64(f);
662 bmp[idx++] = w;
663 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
664 bmp[idx++] = w >> 32;
667 return 0;
670 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
672 unsigned long *bmp = pv;
673 int i, idx = 0;
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;
679 qemu_put_be64(f, w);
683 const VMStateInfo vmstate_info_bitmap = {
684 .name = "bitmap",
685 .get = get_bitmap,
686 .put = put_bitmap,