libcacard: remove unnecessary EOL from debug prints
[qemu/ar7.git] / vmstate.c
blobb5882fae6719f324607f5e832de6b4de21dd6776
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 trace_vmstate_load_field_error(field->name, ret);
103 return ret;
106 } else if (field->flags & VMS_MUST_EXIST) {
107 fprintf(stderr, "Input validation failed: %s/%s\n",
108 vmsd->name, field->name);
109 return -1;
111 field++;
113 ret = vmstate_subsection_load(f, vmsd, opaque);
114 if (ret != 0) {
115 return ret;
117 if (vmsd->post_load) {
118 return vmsd->post_load(opaque, version_id);
120 return 0;
123 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
124 void *opaque)
126 VMStateField *field = vmsd->fields;
128 if (vmsd->pre_save) {
129 vmsd->pre_save(opaque);
131 while (field->name) {
132 if (!field->field_exists ||
133 field->field_exists(opaque, vmsd->version_id)) {
134 void *base_addr = vmstate_base_addr(opaque, field);
135 int i, n_elems = vmstate_n_elems(opaque, field);
136 int size = vmstate_size(opaque, field);
138 for (i = 0; i < n_elems; i++) {
139 void *addr = base_addr + size * i;
141 if (field->flags & VMS_ARRAY_OF_POINTER) {
142 addr = *(void **)addr;
144 if (field->flags & VMS_STRUCT) {
145 vmstate_save_state(f, field->vmsd, addr);
146 } else {
147 field->info->put(f, addr, size);
150 } else {
151 if (field->flags & VMS_MUST_EXIST) {
152 fprintf(stderr, "Output state validation failed: %s/%s\n",
153 vmsd->name, field->name);
154 assert(!(field->flags & VMS_MUST_EXIST));
157 field++;
159 vmstate_subsection_save(f, vmsd, opaque);
162 static const VMStateDescription *
163 vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
165 while (sub && sub->needed) {
166 if (strcmp(idstr, sub->vmsd->name) == 0) {
167 return sub->vmsd;
169 sub++;
171 return NULL;
174 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
175 void *opaque)
177 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
178 char idstr[256];
179 int ret;
180 uint8_t version_id, len, size;
181 const VMStateDescription *sub_vmsd;
183 len = qemu_peek_byte(f, 1);
184 if (len < strlen(vmsd->name) + 1) {
185 /* subsection name has be be "section_name/a" */
186 return 0;
188 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
189 if (size != len) {
190 return 0;
192 idstr[size] = 0;
194 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
195 /* it don't have a valid subsection name */
196 return 0;
198 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
199 if (sub_vmsd == NULL) {
200 return -ENOENT;
202 qemu_file_skip(f, 1); /* subsection */
203 qemu_file_skip(f, 1); /* len */
204 qemu_file_skip(f, len); /* idstr */
205 version_id = qemu_get_be32(f);
207 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
208 if (ret) {
209 return ret;
212 return 0;
215 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
216 void *opaque)
218 const VMStateSubsection *sub = vmsd->subsections;
220 while (sub && sub->needed) {
221 if (sub->needed(opaque)) {
222 const VMStateDescription *vmsd = sub->vmsd;
223 uint8_t len;
225 qemu_put_byte(f, QEMU_VM_SUBSECTION);
226 len = strlen(vmsd->name);
227 qemu_put_byte(f, len);
228 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
229 qemu_put_be32(f, vmsd->version_id);
230 vmstate_save_state(f, vmsd, opaque);
232 sub++;
236 /* bool */
238 static int get_bool(QEMUFile *f, void *pv, size_t size)
240 bool *v = pv;
241 *v = qemu_get_byte(f);
242 return 0;
245 static void put_bool(QEMUFile *f, void *pv, size_t size)
247 bool *v = pv;
248 qemu_put_byte(f, *v);
251 const VMStateInfo vmstate_info_bool = {
252 .name = "bool",
253 .get = get_bool,
254 .put = put_bool,
257 /* 8 bit int */
259 static int get_int8(QEMUFile *f, void *pv, size_t size)
261 int8_t *v = pv;
262 qemu_get_s8s(f, v);
263 return 0;
266 static void put_int8(QEMUFile *f, void *pv, size_t size)
268 int8_t *v = pv;
269 qemu_put_s8s(f, v);
272 const VMStateInfo vmstate_info_int8 = {
273 .name = "int8",
274 .get = get_int8,
275 .put = put_int8,
278 /* 16 bit int */
280 static int get_int16(QEMUFile *f, void *pv, size_t size)
282 int16_t *v = pv;
283 qemu_get_sbe16s(f, v);
284 return 0;
287 static void put_int16(QEMUFile *f, void *pv, size_t size)
289 int16_t *v = pv;
290 qemu_put_sbe16s(f, v);
293 const VMStateInfo vmstate_info_int16 = {
294 .name = "int16",
295 .get = get_int16,
296 .put = put_int16,
299 /* 32 bit int */
301 static int get_int32(QEMUFile *f, void *pv, size_t size)
303 int32_t *v = pv;
304 qemu_get_sbe32s(f, v);
305 return 0;
308 static void put_int32(QEMUFile *f, void *pv, size_t size)
310 int32_t *v = pv;
311 qemu_put_sbe32s(f, v);
314 const VMStateInfo vmstate_info_int32 = {
315 .name = "int32",
316 .get = get_int32,
317 .put = put_int32,
320 /* 32 bit int. See that the received value is the same than the one
321 in the field */
323 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
325 int32_t *v = pv;
326 int32_t v2;
327 qemu_get_sbe32s(f, &v2);
329 if (*v == v2) {
330 return 0;
332 return -EINVAL;
335 const VMStateInfo vmstate_info_int32_equal = {
336 .name = "int32 equal",
337 .get = get_int32_equal,
338 .put = put_int32,
341 /* 32 bit int. Check that the received value is non-negative
342 * and less than or equal to the one in the field.
345 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
347 int32_t *cur = pv;
348 int32_t loaded;
349 qemu_get_sbe32s(f, &loaded);
351 if (loaded >= 0 && loaded <= *cur) {
352 *cur = loaded;
353 return 0;
355 return -EINVAL;
358 const VMStateInfo vmstate_info_int32_le = {
359 .name = "int32 le",
360 .get = get_int32_le,
361 .put = put_int32,
364 /* 64 bit int */
366 static int get_int64(QEMUFile *f, void *pv, size_t size)
368 int64_t *v = pv;
369 qemu_get_sbe64s(f, v);
370 return 0;
373 static void put_int64(QEMUFile *f, void *pv, size_t size)
375 int64_t *v = pv;
376 qemu_put_sbe64s(f, v);
379 const VMStateInfo vmstate_info_int64 = {
380 .name = "int64",
381 .get = get_int64,
382 .put = put_int64,
385 /* 8 bit unsigned int */
387 static int get_uint8(QEMUFile *f, void *pv, size_t size)
389 uint8_t *v = pv;
390 qemu_get_8s(f, v);
391 return 0;
394 static void put_uint8(QEMUFile *f, void *pv, size_t size)
396 uint8_t *v = pv;
397 qemu_put_8s(f, v);
400 const VMStateInfo vmstate_info_uint8 = {
401 .name = "uint8",
402 .get = get_uint8,
403 .put = put_uint8,
406 /* 16 bit unsigned int */
408 static int get_uint16(QEMUFile *f, void *pv, size_t size)
410 uint16_t *v = pv;
411 qemu_get_be16s(f, v);
412 return 0;
415 static void put_uint16(QEMUFile *f, void *pv, size_t size)
417 uint16_t *v = pv;
418 qemu_put_be16s(f, v);
421 const VMStateInfo vmstate_info_uint16 = {
422 .name = "uint16",
423 .get = get_uint16,
424 .put = put_uint16,
427 /* 32 bit unsigned int */
429 static int get_uint32(QEMUFile *f, void *pv, size_t size)
431 uint32_t *v = pv;
432 qemu_get_be32s(f, v);
433 return 0;
436 static void put_uint32(QEMUFile *f, void *pv, size_t size)
438 uint32_t *v = pv;
439 qemu_put_be32s(f, v);
442 const VMStateInfo vmstate_info_uint32 = {
443 .name = "uint32",
444 .get = get_uint32,
445 .put = put_uint32,
448 /* 32 bit uint. See that the received value is the same than the one
449 in the field */
451 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
453 uint32_t *v = pv;
454 uint32_t v2;
455 qemu_get_be32s(f, &v2);
457 if (*v == v2) {
458 return 0;
460 return -EINVAL;
463 const VMStateInfo vmstate_info_uint32_equal = {
464 .name = "uint32 equal",
465 .get = get_uint32_equal,
466 .put = put_uint32,
469 /* 64 bit unsigned int */
471 static int get_uint64(QEMUFile *f, void *pv, size_t size)
473 uint64_t *v = pv;
474 qemu_get_be64s(f, v);
475 return 0;
478 static void put_uint64(QEMUFile *f, void *pv, size_t size)
480 uint64_t *v = pv;
481 qemu_put_be64s(f, v);
484 const VMStateInfo vmstate_info_uint64 = {
485 .name = "uint64",
486 .get = get_uint64,
487 .put = put_uint64,
490 /* 64 bit unsigned int. See that the received value is the same than the one
491 in the field */
493 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
495 uint64_t *v = pv;
496 uint64_t v2;
497 qemu_get_be64s(f, &v2);
499 if (*v == v2) {
500 return 0;
502 return -EINVAL;
505 const VMStateInfo vmstate_info_uint64_equal = {
506 .name = "int64 equal",
507 .get = get_uint64_equal,
508 .put = put_uint64,
511 /* 8 bit int. See that the received value is the same than the one
512 in the field */
514 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
516 uint8_t *v = pv;
517 uint8_t v2;
518 qemu_get_8s(f, &v2);
520 if (*v == v2) {
521 return 0;
523 return -EINVAL;
526 const VMStateInfo vmstate_info_uint8_equal = {
527 .name = "uint8 equal",
528 .get = get_uint8_equal,
529 .put = put_uint8,
532 /* 16 bit unsigned int int. See that the received value is the same than the one
533 in the field */
535 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
537 uint16_t *v = pv;
538 uint16_t v2;
539 qemu_get_be16s(f, &v2);
541 if (*v == v2) {
542 return 0;
544 return -EINVAL;
547 const VMStateInfo vmstate_info_uint16_equal = {
548 .name = "uint16 equal",
549 .get = get_uint16_equal,
550 .put = put_uint16,
553 /* floating point */
555 static int get_float64(QEMUFile *f, void *pv, size_t size)
557 float64 *v = pv;
559 *v = make_float64(qemu_get_be64(f));
560 return 0;
563 static void put_float64(QEMUFile *f, void *pv, size_t size)
565 uint64_t *v = pv;
567 qemu_put_be64(f, float64_val(*v));
570 const VMStateInfo vmstate_info_float64 = {
571 .name = "float64",
572 .get = get_float64,
573 .put = put_float64,
576 /* uint8_t buffers */
578 static int get_buffer(QEMUFile *f, void *pv, size_t size)
580 uint8_t *v = pv;
581 qemu_get_buffer(f, v, size);
582 return 0;
585 static void put_buffer(QEMUFile *f, void *pv, size_t size)
587 uint8_t *v = pv;
588 qemu_put_buffer(f, v, size);
591 const VMStateInfo vmstate_info_buffer = {
592 .name = "buffer",
593 .get = get_buffer,
594 .put = put_buffer,
597 /* unused buffers: space that was used for some fields that are
598 not useful anymore */
600 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
602 uint8_t buf[1024];
603 int block_len;
605 while (size > 0) {
606 block_len = MIN(sizeof(buf), size);
607 size -= block_len;
608 qemu_get_buffer(f, buf, block_len);
610 return 0;
613 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
615 static const uint8_t buf[1024];
616 int block_len;
618 while (size > 0) {
619 block_len = MIN(sizeof(buf), size);
620 size -= block_len;
621 qemu_put_buffer(f, buf, block_len);
625 const VMStateInfo vmstate_info_unused_buffer = {
626 .name = "unused_buffer",
627 .get = get_unused_buffer,
628 .put = put_unused_buffer,
631 /* bitmaps (as defined by bitmap.h). Note that size here is the size
632 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
633 * bit words with the bits in big endian order. The in-memory format
634 * is an array of 'unsigned long', which may be either 32 or 64 bits.
636 /* This is the number of 64 bit words sent over the wire */
637 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
638 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
640 unsigned long *bmp = pv;
641 int i, idx = 0;
642 for (i = 0; i < BITS_TO_U64S(size); i++) {
643 uint64_t w = qemu_get_be64(f);
644 bmp[idx++] = w;
645 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
646 bmp[idx++] = w >> 32;
649 return 0;
652 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
654 unsigned long *bmp = pv;
655 int i, idx = 0;
656 for (i = 0; i < BITS_TO_U64S(size); i++) {
657 uint64_t w = bmp[idx++];
658 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
659 w |= ((uint64_t)bmp[idx++]) << 32;
661 qemu_put_be64(f, w);
665 const VMStateInfo vmstate_info_bitmap = {
666 .name = "bitmap",
667 .get = get_bitmap,
668 .put = put_bitmap,