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