vmstate: reduce code duplication
[qemu/ar7.git] / vmstate.c
blobdd6f8343310d40ed8e217418132b0b50c38aff10
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_old) {
67 return -EINVAL;
69 if (version_id < vmsd->minimum_version_id) {
70 return vmsd->load_state_old(f, opaque, version_id);
72 if (vmsd->pre_load) {
73 int ret = vmsd->pre_load(opaque);
74 if (ret) {
75 return ret;
78 while (field->name) {
79 if ((field->field_exists &&
80 field->field_exists(opaque, version_id)) ||
81 (!field->field_exists &&
82 field->version_id <= version_id)) {
83 void *base_addr = vmstate_base_addr(opaque, field);
84 int i, n_elems = vmstate_n_elems(opaque, field);
85 int size = vmstate_size(opaque, field);
87 for (i = 0; i < n_elems; i++) {
88 void *addr = base_addr + size * i;
90 if (field->flags & VMS_ARRAY_OF_POINTER) {
91 addr = *(void **)addr;
93 if (field->flags & VMS_STRUCT) {
94 ret = vmstate_load_state(f, field->vmsd, addr,
95 field->vmsd->version_id);
96 } else {
97 ret = field->info->get(f, addr, size);
100 if (ret < 0) {
101 trace_vmstate_load_field_error(field->name, ret);
102 return ret;
106 field++;
108 ret = vmstate_subsection_load(f, vmsd, opaque);
109 if (ret != 0) {
110 return ret;
112 if (vmsd->post_load) {
113 return vmsd->post_load(opaque, version_id);
115 return 0;
118 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
119 void *opaque)
121 VMStateField *field = vmsd->fields;
123 if (vmsd->pre_save) {
124 vmsd->pre_save(opaque);
126 while (field->name) {
127 if (!field->field_exists ||
128 field->field_exists(opaque, vmsd->version_id)) {
129 void *base_addr = vmstate_base_addr(opaque, field);
130 int i, n_elems = vmstate_n_elems(opaque, field);
131 int size = vmstate_size(opaque, field);
133 for (i = 0; i < n_elems; i++) {
134 void *addr = base_addr + size * i;
136 if (field->flags & VMS_ARRAY_OF_POINTER) {
137 addr = *(void **)addr;
139 if (field->flags & VMS_STRUCT) {
140 vmstate_save_state(f, field->vmsd, addr);
141 } else {
142 field->info->put(f, addr, size);
146 field++;
148 vmstate_subsection_save(f, vmsd, opaque);
151 static const VMStateDescription *
152 vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
154 while (sub && sub->needed) {
155 if (strcmp(idstr, sub->vmsd->name) == 0) {
156 return sub->vmsd;
158 sub++;
160 return NULL;
163 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
164 void *opaque)
166 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
167 char idstr[256];
168 int ret;
169 uint8_t version_id, len, size;
170 const VMStateDescription *sub_vmsd;
172 len = qemu_peek_byte(f, 1);
173 if (len < strlen(vmsd->name) + 1) {
174 /* subsection name has be be "section_name/a" */
175 return 0;
177 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
178 if (size != len) {
179 return 0;
181 idstr[size] = 0;
183 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
184 /* it don't have a valid subsection name */
185 return 0;
187 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
188 if (sub_vmsd == NULL) {
189 return -ENOENT;
191 qemu_file_skip(f, 1); /* subsection */
192 qemu_file_skip(f, 1); /* len */
193 qemu_file_skip(f, len); /* idstr */
194 version_id = qemu_get_be32(f);
196 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
197 if (ret) {
198 return ret;
201 return 0;
204 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
205 void *opaque)
207 const VMStateSubsection *sub = vmsd->subsections;
209 while (sub && sub->needed) {
210 if (sub->needed(opaque)) {
211 const VMStateDescription *vmsd = sub->vmsd;
212 uint8_t len;
214 qemu_put_byte(f, QEMU_VM_SUBSECTION);
215 len = strlen(vmsd->name);
216 qemu_put_byte(f, len);
217 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
218 qemu_put_be32(f, vmsd->version_id);
219 vmstate_save_state(f, vmsd, opaque);
221 sub++;
225 /* bool */
227 static int get_bool(QEMUFile *f, void *pv, size_t size)
229 bool *v = pv;
230 *v = qemu_get_byte(f);
231 return 0;
234 static void put_bool(QEMUFile *f, void *pv, size_t size)
236 bool *v = pv;
237 qemu_put_byte(f, *v);
240 const VMStateInfo vmstate_info_bool = {
241 .name = "bool",
242 .get = get_bool,
243 .put = put_bool,
246 /* 8 bit int */
248 static int get_int8(QEMUFile *f, void *pv, size_t size)
250 int8_t *v = pv;
251 qemu_get_s8s(f, v);
252 return 0;
255 static void put_int8(QEMUFile *f, void *pv, size_t size)
257 int8_t *v = pv;
258 qemu_put_s8s(f, v);
261 const VMStateInfo vmstate_info_int8 = {
262 .name = "int8",
263 .get = get_int8,
264 .put = put_int8,
267 /* 16 bit int */
269 static int get_int16(QEMUFile *f, void *pv, size_t size)
271 int16_t *v = pv;
272 qemu_get_sbe16s(f, v);
273 return 0;
276 static void put_int16(QEMUFile *f, void *pv, size_t size)
278 int16_t *v = pv;
279 qemu_put_sbe16s(f, v);
282 const VMStateInfo vmstate_info_int16 = {
283 .name = "int16",
284 .get = get_int16,
285 .put = put_int16,
288 /* 32 bit int */
290 static int get_int32(QEMUFile *f, void *pv, size_t size)
292 int32_t *v = pv;
293 qemu_get_sbe32s(f, v);
294 return 0;
297 static void put_int32(QEMUFile *f, void *pv, size_t size)
299 int32_t *v = pv;
300 qemu_put_sbe32s(f, v);
303 const VMStateInfo vmstate_info_int32 = {
304 .name = "int32",
305 .get = get_int32,
306 .put = put_int32,
309 /* 32 bit int. See that the received value is the same than the one
310 in the field */
312 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
314 int32_t *v = pv;
315 int32_t v2;
316 qemu_get_sbe32s(f, &v2);
318 if (*v == v2) {
319 return 0;
321 return -EINVAL;
324 const VMStateInfo vmstate_info_int32_equal = {
325 .name = "int32 equal",
326 .get = get_int32_equal,
327 .put = put_int32,
330 /* 32 bit int. Check that the received value is less than or equal to
331 the one in the field */
333 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
335 int32_t *cur = pv;
336 int32_t loaded;
337 qemu_get_sbe32s(f, &loaded);
339 if (loaded <= *cur) {
340 *cur = loaded;
341 return 0;
343 return -EINVAL;
346 const VMStateInfo vmstate_info_int32_le = {
347 .name = "int32 le",
348 .get = get_int32_le,
349 .put = put_int32,
352 /* 64 bit int */
354 static int get_int64(QEMUFile *f, void *pv, size_t size)
356 int64_t *v = pv;
357 qemu_get_sbe64s(f, v);
358 return 0;
361 static void put_int64(QEMUFile *f, void *pv, size_t size)
363 int64_t *v = pv;
364 qemu_put_sbe64s(f, v);
367 const VMStateInfo vmstate_info_int64 = {
368 .name = "int64",
369 .get = get_int64,
370 .put = put_int64,
373 /* 8 bit unsigned int */
375 static int get_uint8(QEMUFile *f, void *pv, size_t size)
377 uint8_t *v = pv;
378 qemu_get_8s(f, v);
379 return 0;
382 static void put_uint8(QEMUFile *f, void *pv, size_t size)
384 uint8_t *v = pv;
385 qemu_put_8s(f, v);
388 const VMStateInfo vmstate_info_uint8 = {
389 .name = "uint8",
390 .get = get_uint8,
391 .put = put_uint8,
394 /* 16 bit unsigned int */
396 static int get_uint16(QEMUFile *f, void *pv, size_t size)
398 uint16_t *v = pv;
399 qemu_get_be16s(f, v);
400 return 0;
403 static void put_uint16(QEMUFile *f, void *pv, size_t size)
405 uint16_t *v = pv;
406 qemu_put_be16s(f, v);
409 const VMStateInfo vmstate_info_uint16 = {
410 .name = "uint16",
411 .get = get_uint16,
412 .put = put_uint16,
415 /* 32 bit unsigned int */
417 static int get_uint32(QEMUFile *f, void *pv, size_t size)
419 uint32_t *v = pv;
420 qemu_get_be32s(f, v);
421 return 0;
424 static void put_uint32(QEMUFile *f, void *pv, size_t size)
426 uint32_t *v = pv;
427 qemu_put_be32s(f, v);
430 const VMStateInfo vmstate_info_uint32 = {
431 .name = "uint32",
432 .get = get_uint32,
433 .put = put_uint32,
436 /* 32 bit uint. See that the received value is the same than the one
437 in the field */
439 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
441 uint32_t *v = pv;
442 uint32_t v2;
443 qemu_get_be32s(f, &v2);
445 if (*v == v2) {
446 return 0;
448 return -EINVAL;
451 const VMStateInfo vmstate_info_uint32_equal = {
452 .name = "uint32 equal",
453 .get = get_uint32_equal,
454 .put = put_uint32,
457 /* 64 bit unsigned int */
459 static int get_uint64(QEMUFile *f, void *pv, size_t size)
461 uint64_t *v = pv;
462 qemu_get_be64s(f, v);
463 return 0;
466 static void put_uint64(QEMUFile *f, void *pv, size_t size)
468 uint64_t *v = pv;
469 qemu_put_be64s(f, v);
472 const VMStateInfo vmstate_info_uint64 = {
473 .name = "uint64",
474 .get = get_uint64,
475 .put = put_uint64,
478 /* 64 bit unsigned int. See that the received value is the same than the one
479 in the field */
481 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
483 uint64_t *v = pv;
484 uint64_t v2;
485 qemu_get_be64s(f, &v2);
487 if (*v == v2) {
488 return 0;
490 return -EINVAL;
493 const VMStateInfo vmstate_info_uint64_equal = {
494 .name = "int64 equal",
495 .get = get_uint64_equal,
496 .put = put_uint64,
499 /* 8 bit int. See that the received value is the same than the one
500 in the field */
502 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
504 uint8_t *v = pv;
505 uint8_t v2;
506 qemu_get_8s(f, &v2);
508 if (*v == v2) {
509 return 0;
511 return -EINVAL;
514 const VMStateInfo vmstate_info_uint8_equal = {
515 .name = "uint8 equal",
516 .get = get_uint8_equal,
517 .put = put_uint8,
520 /* 16 bit unsigned int int. See that the received value is the same than the one
521 in the field */
523 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
525 uint16_t *v = pv;
526 uint16_t v2;
527 qemu_get_be16s(f, &v2);
529 if (*v == v2) {
530 return 0;
532 return -EINVAL;
535 const VMStateInfo vmstate_info_uint16_equal = {
536 .name = "uint16 equal",
537 .get = get_uint16_equal,
538 .put = put_uint16,
541 /* floating point */
543 static int get_float64(QEMUFile *f, void *pv, size_t size)
545 float64 *v = pv;
547 *v = make_float64(qemu_get_be64(f));
548 return 0;
551 static void put_float64(QEMUFile *f, void *pv, size_t size)
553 uint64_t *v = pv;
555 qemu_put_be64(f, float64_val(*v));
558 const VMStateInfo vmstate_info_float64 = {
559 .name = "float64",
560 .get = get_float64,
561 .put = put_float64,
564 /* uint8_t buffers */
566 static int get_buffer(QEMUFile *f, void *pv, size_t size)
568 uint8_t *v = pv;
569 qemu_get_buffer(f, v, size);
570 return 0;
573 static void put_buffer(QEMUFile *f, void *pv, size_t size)
575 uint8_t *v = pv;
576 qemu_put_buffer(f, v, size);
579 const VMStateInfo vmstate_info_buffer = {
580 .name = "buffer",
581 .get = get_buffer,
582 .put = put_buffer,
585 /* unused buffers: space that was used for some fields that are
586 not useful anymore */
588 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
590 uint8_t buf[1024];
591 int block_len;
593 while (size > 0) {
594 block_len = MIN(sizeof(buf), size);
595 size -= block_len;
596 qemu_get_buffer(f, buf, block_len);
598 return 0;
601 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
603 static const uint8_t buf[1024];
604 int block_len;
606 while (size > 0) {
607 block_len = MIN(sizeof(buf), size);
608 size -= block_len;
609 qemu_put_buffer(f, buf, block_len);
613 const VMStateInfo vmstate_info_unused_buffer = {
614 .name = "unused_buffer",
615 .get = get_unused_buffer,
616 .put = put_unused_buffer,
619 /* bitmaps (as defined by bitmap.h). Note that size here is the size
620 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
621 * bit words with the bits in big endian order. The in-memory format
622 * is an array of 'unsigned long', which may be either 32 or 64 bits.
624 /* This is the number of 64 bit words sent over the wire */
625 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
626 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
628 unsigned long *bmp = pv;
629 int i, idx = 0;
630 for (i = 0; i < BITS_TO_U64S(size); i++) {
631 uint64_t w = qemu_get_be64(f);
632 bmp[idx++] = w;
633 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
634 bmp[idx++] = w >> 32;
637 return 0;
640 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
642 unsigned long *bmp = pv;
643 int i, idx = 0;
644 for (i = 0; i < BITS_TO_U64S(size); i++) {
645 uint64_t w = bmp[idx++];
646 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
647 w |= ((uint64_t)bmp[idx++]) << 32;
649 qemu_put_be64(f, w);
653 const VMStateInfo vmstate_info_bitmap = {
654 .name = "bitmap",
655 .get = get_bitmap,
656 .put = put_bitmap,