]> git.proxmox.com Git - mirror_qemu.git/blame - vmstate.c
vl: redo -object parsing
[mirror_qemu.git] / vmstate.c
CommitLineData
b6fcfa59
EH
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"
9013dca5 6#include "trace.h"
b6fcfa59
EH
7
8static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
9 void *opaque);
10static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
11 void *opaque);
12
35fc1f71
MT
13static int vmstate_n_elems(void *opaque, VMStateField *field)
14{
15 int n_elems = 1;
16
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);
27 }
28
29 return n_elems;
30}
31
32static int vmstate_size(void *opaque, VMStateField *field)
33{
34 int size = field->size;
35
36 if (field->flags & VMS_VBUFFER) {
37 size = *(int32_t *)(opaque+field->size_offset);
38 if (field->flags & VMS_MULTIPLY) {
39 size *= field->size;
40 }
41 }
42
43 return size;
44}
45
46static void *vmstate_base_addr(void *opaque, VMStateField *field)
47{
48 void *base_addr = opaque + field->offset;
49
50 if (field->flags & VMS_POINTER) {
51 base_addr = *(void **)base_addr + field->start;
52 }
53
54 return base_addr;
55}
56
b6fcfa59
EH
57int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
58 void *opaque, int version_id)
59{
60 VMStateField *field = vmsd->fields;
61 int ret;
62
63 if (version_id > vmsd->version_id) {
64 return -EINVAL;
65 }
b6fcfa59 66 if (version_id < vmsd->minimum_version_id) {
767adce2
PM
67 if (vmsd->load_state_old &&
68 version_id >= vmsd->minimum_version_id_old) {
69 return vmsd->load_state_old(f, opaque, version_id);
70 }
71 return -EINVAL;
b6fcfa59
EH
72 }
73 if (vmsd->pre_load) {
74 int ret = vmsd->pre_load(opaque);
75 if (ret) {
76 return ret;
77 }
78 }
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)) {
35fc1f71
MT
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);
87
b6fcfa59
EH
88 for (i = 0; i < n_elems; i++) {
89 void *addr = base_addr + size * i;
90
91 if (field->flags & VMS_ARRAY_OF_POINTER) {
92 addr = *(void **)addr;
93 }
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);
99
100 }
101 if (ret < 0) {
9013dca5 102 trace_vmstate_load_field_error(field->name, ret);
b6fcfa59
EH
103 return ret;
104 }
105 }
5bf81c8d
MT
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;
b6fcfa59
EH
110 }
111 field++;
112 }
113 ret = vmstate_subsection_load(f, vmsd, opaque);
114 if (ret != 0) {
115 return ret;
116 }
117 if (vmsd->post_load) {
118 return vmsd->post_load(opaque, version_id);
119 }
120 return 0;
121}
122
123void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
124 void *opaque)
125{
126 VMStateField *field = vmsd->fields;
127
128 if (vmsd->pre_save) {
129 vmsd->pre_save(opaque);
130 }
131 while (field->name) {
132 if (!field->field_exists ||
133 field->field_exists(opaque, vmsd->version_id)) {
35fc1f71
MT
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);
137
b6fcfa59
EH
138 for (i = 0; i < n_elems; i++) {
139 void *addr = base_addr + size * i;
140
141 if (field->flags & VMS_ARRAY_OF_POINTER) {
142 addr = *(void **)addr;
143 }
144 if (field->flags & VMS_STRUCT) {
145 vmstate_save_state(f, field->vmsd, addr);
146 } else {
147 field->info->put(f, addr, size);
148 }
149 }
5bf81c8d
MT
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));
155 }
b6fcfa59
EH
156 }
157 field++;
158 }
159 vmstate_subsection_save(f, vmsd, opaque);
160}
161
162static const VMStateDescription *
163 vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
164{
165 while (sub && sub->needed) {
166 if (strcmp(idstr, sub->vmsd->name) == 0) {
167 return sub->vmsd;
168 }
169 sub++;
170 }
171 return NULL;
172}
173
174static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
175 void *opaque)
176{
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;
182
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;
187 }
188 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
189 if (size != len) {
190 return 0;
191 }
192 idstr[size] = 0;
193
194 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
195 /* it don't have a valid subsection name */
196 return 0;
197 }
198 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
199 if (sub_vmsd == NULL) {
200 return -ENOENT;
201 }
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);
206
207 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
208 if (ret) {
209 return ret;
210 }
211 }
212 return 0;
213}
214
215static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
216 void *opaque)
217{
218 const VMStateSubsection *sub = vmsd->subsections;
219
220 while (sub && sub->needed) {
221 if (sub->needed(opaque)) {
222 const VMStateDescription *vmsd = sub->vmsd;
223 uint8_t len;
224
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);
231 }
232 sub++;
233 }
234}
235
236/* bool */
237
238static int get_bool(QEMUFile *f, void *pv, size_t size)
239{
240 bool *v = pv;
241 *v = qemu_get_byte(f);
242 return 0;
243}
244
245static void put_bool(QEMUFile *f, void *pv, size_t size)
246{
247 bool *v = pv;
248 qemu_put_byte(f, *v);
249}
250
251const VMStateInfo vmstate_info_bool = {
252 .name = "bool",
253 .get = get_bool,
254 .put = put_bool,
255};
256
257/* 8 bit int */
258
259static int get_int8(QEMUFile *f, void *pv, size_t size)
260{
261 int8_t *v = pv;
262 qemu_get_s8s(f, v);
263 return 0;
264}
265
266static void put_int8(QEMUFile *f, void *pv, size_t size)
267{
268 int8_t *v = pv;
269 qemu_put_s8s(f, v);
270}
271
272const VMStateInfo vmstate_info_int8 = {
273 .name = "int8",
274 .get = get_int8,
275 .put = put_int8,
276};
277
278/* 16 bit int */
279
280static int get_int16(QEMUFile *f, void *pv, size_t size)
281{
282 int16_t *v = pv;
283 qemu_get_sbe16s(f, v);
284 return 0;
285}
286
287static void put_int16(QEMUFile *f, void *pv, size_t size)
288{
289 int16_t *v = pv;
290 qemu_put_sbe16s(f, v);
291}
292
293const VMStateInfo vmstate_info_int16 = {
294 .name = "int16",
295 .get = get_int16,
296 .put = put_int16,
297};
298
299/* 32 bit int */
300
301static int get_int32(QEMUFile *f, void *pv, size_t size)
302{
303 int32_t *v = pv;
304 qemu_get_sbe32s(f, v);
305 return 0;
306}
307
308static void put_int32(QEMUFile *f, void *pv, size_t size)
309{
310 int32_t *v = pv;
311 qemu_put_sbe32s(f, v);
312}
313
314const VMStateInfo vmstate_info_int32 = {
315 .name = "int32",
316 .get = get_int32,
317 .put = put_int32,
318};
319
320/* 32 bit int. See that the received value is the same than the one
321 in the field */
322
323static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
324{
325 int32_t *v = pv;
326 int32_t v2;
327 qemu_get_sbe32s(f, &v2);
328
329 if (*v == v2) {
330 return 0;
331 }
332 return -EINVAL;
333}
334
335const VMStateInfo vmstate_info_int32_equal = {
336 .name = "int32 equal",
337 .get = get_int32_equal,
338 .put = put_int32,
339};
340
d2ef4b61
MT
341/* 32 bit int. Check that the received value is non-negative
342 * and less than or equal to the one in the field.
343 */
b6fcfa59
EH
344
345static int get_int32_le(QEMUFile *f, void *pv, size_t size)
346{
24a370ef
DDAG
347 int32_t *cur = pv;
348 int32_t loaded;
349 qemu_get_sbe32s(f, &loaded);
b6fcfa59 350
d2ef4b61 351 if (loaded >= 0 && loaded <= *cur) {
24a370ef 352 *cur = loaded;
b6fcfa59
EH
353 return 0;
354 }
355 return -EINVAL;
356}
357
358const VMStateInfo vmstate_info_int32_le = {
24a370ef 359 .name = "int32 le",
b6fcfa59
EH
360 .get = get_int32_le,
361 .put = put_int32,
362};
363
364/* 64 bit int */
365
366static int get_int64(QEMUFile *f, void *pv, size_t size)
367{
368 int64_t *v = pv;
369 qemu_get_sbe64s(f, v);
370 return 0;
371}
372
373static void put_int64(QEMUFile *f, void *pv, size_t size)
374{
375 int64_t *v = pv;
376 qemu_put_sbe64s(f, v);
377}
378
379const VMStateInfo vmstate_info_int64 = {
380 .name = "int64",
381 .get = get_int64,
382 .put = put_int64,
383};
384
385/* 8 bit unsigned int */
386
387static int get_uint8(QEMUFile *f, void *pv, size_t size)
388{
389 uint8_t *v = pv;
390 qemu_get_8s(f, v);
391 return 0;
392}
393
394static void put_uint8(QEMUFile *f, void *pv, size_t size)
395{
396 uint8_t *v = pv;
397 qemu_put_8s(f, v);
398}
399
400const VMStateInfo vmstate_info_uint8 = {
401 .name = "uint8",
402 .get = get_uint8,
403 .put = put_uint8,
404};
405
406/* 16 bit unsigned int */
407
408static int get_uint16(QEMUFile *f, void *pv, size_t size)
409{
410 uint16_t *v = pv;
411 qemu_get_be16s(f, v);
412 return 0;
413}
414
415static void put_uint16(QEMUFile *f, void *pv, size_t size)
416{
417 uint16_t *v = pv;
418 qemu_put_be16s(f, v);
419}
420
421const VMStateInfo vmstate_info_uint16 = {
422 .name = "uint16",
423 .get = get_uint16,
424 .put = put_uint16,
425};
426
427/* 32 bit unsigned int */
428
429static int get_uint32(QEMUFile *f, void *pv, size_t size)
430{
431 uint32_t *v = pv;
432 qemu_get_be32s(f, v);
433 return 0;
434}
435
436static void put_uint32(QEMUFile *f, void *pv, size_t size)
437{
438 uint32_t *v = pv;
439 qemu_put_be32s(f, v);
440}
441
442const VMStateInfo vmstate_info_uint32 = {
443 .name = "uint32",
444 .get = get_uint32,
445 .put = put_uint32,
446};
447
448/* 32 bit uint. See that the received value is the same than the one
449 in the field */
450
451static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
452{
453 uint32_t *v = pv;
454 uint32_t v2;
455 qemu_get_be32s(f, &v2);
456
457 if (*v == v2) {
458 return 0;
459 }
460 return -EINVAL;
461}
462
463const VMStateInfo vmstate_info_uint32_equal = {
464 .name = "uint32 equal",
465 .get = get_uint32_equal,
466 .put = put_uint32,
467};
468
469/* 64 bit unsigned int */
470
471static int get_uint64(QEMUFile *f, void *pv, size_t size)
472{
473 uint64_t *v = pv;
474 qemu_get_be64s(f, v);
475 return 0;
476}
477
478static void put_uint64(QEMUFile *f, void *pv, size_t size)
479{
480 uint64_t *v = pv;
481 qemu_put_be64s(f, v);
482}
483
484const VMStateInfo vmstate_info_uint64 = {
485 .name = "uint64",
486 .get = get_uint64,
487 .put = put_uint64,
488};
489
490/* 64 bit unsigned int. See that the received value is the same than the one
491 in the field */
492
493static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
494{
495 uint64_t *v = pv;
496 uint64_t v2;
497 qemu_get_be64s(f, &v2);
498
499 if (*v == v2) {
500 return 0;
501 }
502 return -EINVAL;
503}
504
505const VMStateInfo vmstate_info_uint64_equal = {
506 .name = "int64 equal",
507 .get = get_uint64_equal,
508 .put = put_uint64,
509};
510
511/* 8 bit int. See that the received value is the same than the one
512 in the field */
513
514static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
515{
516 uint8_t *v = pv;
517 uint8_t v2;
518 qemu_get_8s(f, &v2);
519
520 if (*v == v2) {
521 return 0;
522 }
523 return -EINVAL;
524}
525
526const VMStateInfo vmstate_info_uint8_equal = {
527 .name = "uint8 equal",
528 .get = get_uint8_equal,
529 .put = put_uint8,
530};
531
532/* 16 bit unsigned int int. See that the received value is the same than the one
533 in the field */
534
535static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
536{
537 uint16_t *v = pv;
538 uint16_t v2;
539 qemu_get_be16s(f, &v2);
540
541 if (*v == v2) {
542 return 0;
543 }
544 return -EINVAL;
545}
546
547const VMStateInfo vmstate_info_uint16_equal = {
548 .name = "uint16 equal",
549 .get = get_uint16_equal,
550 .put = put_uint16,
551};
552
553/* floating point */
554
555static int get_float64(QEMUFile *f, void *pv, size_t size)
556{
557 float64 *v = pv;
558
559 *v = make_float64(qemu_get_be64(f));
560 return 0;
561}
562
563static void put_float64(QEMUFile *f, void *pv, size_t size)
564{
565 uint64_t *v = pv;
566
567 qemu_put_be64(f, float64_val(*v));
568}
569
570const VMStateInfo vmstate_info_float64 = {
571 .name = "float64",
572 .get = get_float64,
573 .put = put_float64,
574};
575
576/* uint8_t buffers */
577
578static int get_buffer(QEMUFile *f, void *pv, size_t size)
579{
580 uint8_t *v = pv;
581 qemu_get_buffer(f, v, size);
582 return 0;
583}
584
585static void put_buffer(QEMUFile *f, void *pv, size_t size)
586{
587 uint8_t *v = pv;
588 qemu_put_buffer(f, v, size);
589}
590
591const VMStateInfo vmstate_info_buffer = {
592 .name = "buffer",
593 .get = get_buffer,
594 .put = put_buffer,
595};
596
597/* unused buffers: space that was used for some fields that are
598 not useful anymore */
599
600static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
601{
602 uint8_t buf[1024];
603 int block_len;
604
605 while (size > 0) {
606 block_len = MIN(sizeof(buf), size);
607 size -= block_len;
608 qemu_get_buffer(f, buf, block_len);
609 }
610 return 0;
611}
612
613static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
614{
615 static const uint8_t buf[1024];
616 int block_len;
617
618 while (size > 0) {
619 block_len = MIN(sizeof(buf), size);
620 size -= block_len;
621 qemu_put_buffer(f, buf, block_len);
622 }
623}
624
625const VMStateInfo vmstate_info_unused_buffer = {
626 .name = "unused_buffer",
627 .get = get_unused_buffer,
628 .put = put_unused_buffer,
629};
630
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.
635 */
636/* This is the number of 64 bit words sent over the wire */
637#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
638static int get_bitmap(QEMUFile *f, void *pv, size_t size)
639{
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;
647 }
648 }
649 return 0;
650}
651
652static void put_bitmap(QEMUFile *f, void *pv, size_t size)
653{
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;
660 }
661 qemu_put_be64(f, w);
662 }
663}
664
665const VMStateInfo vmstate_info_bitmap = {
666 .name = "bitmap",
667 .get = get_bitmap,
668 .put = put_bitmap,
669};