]> git.proxmox.com Git - mirror_qemu.git/blame - migration/vmstate.c
migration: report an error giving the failed field
[mirror_qemu.git] / migration / vmstate.c
CommitLineData
1393a485 1#include "qemu/osdep.h"
b6fcfa59
EH
2#include "qemu-common.h"
3#include "migration/migration.h"
4#include "migration/qemu-file.h"
5#include "migration/vmstate.h"
6#include "qemu/bitops.h"
6a64b644 7#include "qemu/error-report.h"
9013dca5 8#include "trace.h"
b6fcfa59
EH
9
10static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
8118f095 11 void *opaque, QJSON *vmdesc);
b6fcfa59
EH
12static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
13 void *opaque);
14
35fc1f71
MT
15static int vmstate_n_elems(void *opaque, VMStateField *field)
16{
17 int n_elems = 1;
18
19 if (field->flags & VMS_ARRAY) {
20 n_elems = field->num;
21 } else if (field->flags & VMS_VARRAY_INT32) {
22 n_elems = *(int32_t *)(opaque+field->num_offset);
23 } else if (field->flags & VMS_VARRAY_UINT32) {
24 n_elems = *(uint32_t *)(opaque+field->num_offset);
25 } else if (field->flags & VMS_VARRAY_UINT16) {
26 n_elems = *(uint16_t *)(opaque+field->num_offset);
27 } else if (field->flags & VMS_VARRAY_UINT8) {
28 n_elems = *(uint8_t *)(opaque+field->num_offset);
29 }
30
b47d3af7
JQ
31 if (field->flags & VMS_MULTIPLY_ELEMENTS) {
32 n_elems *= field->num;
33 }
34
023ad1a6 35 trace_vmstate_n_elems(field->name, n_elems);
35fc1f71
MT
36 return n_elems;
37}
38
39static int vmstate_size(void *opaque, VMStateField *field)
40{
41 int size = field->size;
42
43 if (field->flags & VMS_VBUFFER) {
44 size = *(int32_t *)(opaque+field->size_offset);
45 if (field->flags & VMS_MULTIPLY) {
46 size *= field->size;
47 }
48 }
49
50 return size;
51}
52
f32935ea 53static void *vmstate_base_addr(void *opaque, VMStateField *field, bool alloc)
35fc1f71
MT
54{
55 void *base_addr = opaque + field->offset;
56
57 if (field->flags & VMS_POINTER) {
f32935ea 58 if (alloc && (field->flags & VMS_ALLOC)) {
94ed706d
AK
59 gsize size = 0;
60 if (field->flags & VMS_VBUFFER) {
61 size = vmstate_size(opaque, field);
62 } else {
63 int n_elems = vmstate_n_elems(opaque, field);
64 if (n_elems) {
65 size = n_elems * field->size;
66 }
67 }
68 if (size) {
f32935ea
AK
69 *((void **)base_addr + field->start) = g_malloc(size);
70 }
71 }
35fc1f71
MT
72 base_addr = *(void **)base_addr + field->start;
73 }
74
75 return base_addr;
76}
77
b6fcfa59
EH
78int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
79 void *opaque, int version_id)
80{
81 VMStateField *field = vmsd->fields;
a5df2a02 82 int ret = 0;
b6fcfa59 83
a5df2a02 84 trace_vmstate_load_state(vmsd->name, version_id);
b6fcfa59 85 if (version_id > vmsd->version_id) {
a5df2a02 86 trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
b6fcfa59
EH
87 return -EINVAL;
88 }
b6fcfa59 89 if (version_id < vmsd->minimum_version_id) {
767adce2
PM
90 if (vmsd->load_state_old &&
91 version_id >= vmsd->minimum_version_id_old) {
a5df2a02
DDAG
92 ret = vmsd->load_state_old(f, opaque, version_id);
93 trace_vmstate_load_state_end(vmsd->name, "old path", ret);
94 return ret;
767adce2 95 }
a5df2a02 96 trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
767adce2 97 return -EINVAL;
b6fcfa59
EH
98 }
99 if (vmsd->pre_load) {
100 int ret = vmsd->pre_load(opaque);
101 if (ret) {
102 return ret;
103 }
104 }
105 while (field->name) {
a5df2a02 106 trace_vmstate_load_state_field(vmsd->name, field->name);
b6fcfa59
EH
107 if ((field->field_exists &&
108 field->field_exists(opaque, version_id)) ||
109 (!field->field_exists &&
110 field->version_id <= version_id)) {
f32935ea 111 void *base_addr = vmstate_base_addr(opaque, field, true);
35fc1f71
MT
112 int i, n_elems = vmstate_n_elems(opaque, field);
113 int size = vmstate_size(opaque, field);
114
b6fcfa59
EH
115 for (i = 0; i < n_elems; i++) {
116 void *addr = base_addr + size * i;
117
118 if (field->flags & VMS_ARRAY_OF_POINTER) {
119 addr = *(void **)addr;
120 }
121 if (field->flags & VMS_STRUCT) {
122 ret = vmstate_load_state(f, field->vmsd, addr,
123 field->vmsd->version_id);
124 } else {
125 ret = field->info->get(f, addr, size);
126
127 }
13cde508
JQ
128 if (ret >= 0) {
129 ret = qemu_file_get_error(f);
130 }
b6fcfa59 131 if (ret < 0) {
13cde508 132 qemu_file_set_error(f, ret);
a1771070
DDAG
133 error_report("Failed to load %s:%s", vmsd->name,
134 field->name);
9013dca5 135 trace_vmstate_load_field_error(field->name, ret);
b6fcfa59
EH
136 return ret;
137 }
138 }
5bf81c8d 139 } else if (field->flags & VMS_MUST_EXIST) {
6a64b644
DDAG
140 error_report("Input validation failed: %s/%s",
141 vmsd->name, field->name);
5bf81c8d 142 return -1;
b6fcfa59
EH
143 }
144 field++;
145 }
146 ret = vmstate_subsection_load(f, vmsd, opaque);
147 if (ret != 0) {
148 return ret;
149 }
150 if (vmsd->post_load) {
a5df2a02 151 ret = vmsd->post_load(opaque, version_id);
b6fcfa59 152 }
a5df2a02
DDAG
153 trace_vmstate_load_state_end(vmsd->name, "end", ret);
154 return ret;
b6fcfa59
EH
155}
156
8118f095
AG
157static int vmfield_name_num(VMStateField *start, VMStateField *search)
158{
159 VMStateField *field;
160 int found = 0;
161
162 for (field = start; field->name; field++) {
163 if (!strcmp(field->name, search->name)) {
164 if (field == search) {
165 return found;
166 }
167 found++;
168 }
169 }
170
171 return -1;
172}
173
174static bool vmfield_name_is_unique(VMStateField *start, VMStateField *search)
175{
176 VMStateField *field;
177 int found = 0;
178
179 for (field = start; field->name; field++) {
180 if (!strcmp(field->name, search->name)) {
181 found++;
182 /* name found more than once, so it's not unique */
183 if (found > 1) {
184 return false;
185 }
186 }
187 }
188
189 return true;
190}
191
192static const char *vmfield_get_type_name(VMStateField *field)
193{
194 const char *type = "unknown";
195
196 if (field->flags & VMS_STRUCT) {
197 type = "struct";
198 } else if (field->info->name) {
199 type = field->info->name;
200 }
201
202 return type;
203}
204
205static bool vmsd_can_compress(VMStateField *field)
206{
207 if (field->field_exists) {
208 /* Dynamically existing fields mess up compression */
209 return false;
210 }
211
212 if (field->flags & VMS_STRUCT) {
213 VMStateField *sfield = field->vmsd->fields;
214 while (sfield->name) {
215 if (!vmsd_can_compress(sfield)) {
216 /* Child elements can't compress, so can't we */
217 return false;
218 }
219 sfield++;
220 }
221
222 if (field->vmsd->subsections) {
223 /* Subsections may come and go, better don't compress */
224 return false;
225 }
226 }
227
228 return true;
229}
230
231static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc,
232 VMStateField *field, int i, int max)
233{
234 char *name, *old_name;
235 bool is_array = max > 1;
236 bool can_compress = vmsd_can_compress(field);
237
238 if (!vmdesc) {
239 return;
240 }
241
242 name = g_strdup(field->name);
243
244 /* Field name is not unique, need to make it unique */
245 if (!vmfield_name_is_unique(vmsd->fields, field)) {
246 int num = vmfield_name_num(vmsd->fields, field);
247 old_name = name;
248 name = g_strdup_printf("%s[%d]", name, num);
249 g_free(old_name);
250 }
251
252 json_start_object(vmdesc, NULL);
253 json_prop_str(vmdesc, "name", name);
254 if (is_array) {
255 if (can_compress) {
256 json_prop_int(vmdesc, "array_len", max);
257 } else {
258 json_prop_int(vmdesc, "index", i);
259 }
260 }
261 json_prop_str(vmdesc, "type", vmfield_get_type_name(field));
262
263 if (field->flags & VMS_STRUCT) {
264 json_start_object(vmdesc, "struct");
265 }
266
267 g_free(name);
268}
269
270static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc,
271 VMStateField *field, size_t size, int i)
272{
273 if (!vmdesc) {
274 return;
275 }
276
277 if (field->flags & VMS_STRUCT) {
278 /* We printed a struct in between, close its child object */
279 json_end_object(vmdesc);
280 }
281
282 json_prop_int(vmdesc, "size", size);
283 json_end_object(vmdesc);
284}
285
df896152
JQ
286
287bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque)
288{
289 if (vmsd->needed && !vmsd->needed(opaque)) {
290 /* optional section not needed */
291 return false;
292 }
293 return true;
294}
295
296
b6fcfa59 297void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
8118f095 298 void *opaque, QJSON *vmdesc)
b6fcfa59
EH
299{
300 VMStateField *field = vmsd->fields;
301
302 if (vmsd->pre_save) {
303 vmsd->pre_save(opaque);
304 }
8118f095
AG
305
306 if (vmdesc) {
307 json_prop_str(vmdesc, "vmsd_name", vmsd->name);
308 json_prop_int(vmdesc, "version", vmsd->version_id);
309 json_start_array(vmdesc, "fields");
310 }
311
b6fcfa59
EH
312 while (field->name) {
313 if (!field->field_exists ||
314 field->field_exists(opaque, vmsd->version_id)) {
f32935ea 315 void *base_addr = vmstate_base_addr(opaque, field, false);
35fc1f71
MT
316 int i, n_elems = vmstate_n_elems(opaque, field);
317 int size = vmstate_size(opaque, field);
8118f095
AG
318 int64_t old_offset, written_bytes;
319 QJSON *vmdesc_loop = vmdesc;
35fc1f71 320
b6fcfa59
EH
321 for (i = 0; i < n_elems; i++) {
322 void *addr = base_addr + size * i;
323
8118f095
AG
324 vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
325 old_offset = qemu_ftell_fast(f);
326
b6fcfa59
EH
327 if (field->flags & VMS_ARRAY_OF_POINTER) {
328 addr = *(void **)addr;
329 }
330 if (field->flags & VMS_STRUCT) {
8118f095 331 vmstate_save_state(f, field->vmsd, addr, vmdesc_loop);
b6fcfa59
EH
332 } else {
333 field->info->put(f, addr, size);
334 }
8118f095
AG
335
336 written_bytes = qemu_ftell_fast(f) - old_offset;
337 vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
338
339 /* Compressed arrays only care about the first element */
340 if (vmdesc_loop && vmsd_can_compress(field)) {
341 vmdesc_loop = NULL;
342 }
b6fcfa59 343 }
5bf81c8d
MT
344 } else {
345 if (field->flags & VMS_MUST_EXIST) {
6a64b644 346 error_report("Output state validation failed: %s/%s",
5bf81c8d
MT
347 vmsd->name, field->name);
348 assert(!(field->flags & VMS_MUST_EXIST));
349 }
b6fcfa59
EH
350 }
351 field++;
352 }
8118f095
AG
353
354 if (vmdesc) {
355 json_end_array(vmdesc);
356 }
357
358 vmstate_subsection_save(f, vmsd, opaque, vmdesc);
b6fcfa59
EH
359}
360
361static const VMStateDescription *
5cd8cada 362vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
b6fcfa59 363{
5cd8cada
JQ
364 while (sub && *sub && (*sub)->needed) {
365 if (strcmp(idstr, (*sub)->name) == 0) {
366 return *sub;
b6fcfa59
EH
367 }
368 sub++;
369 }
370 return NULL;
371}
372
373static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
374 void *opaque)
375{
a5df2a02
DDAG
376 trace_vmstate_subsection_load(vmsd->name);
377
b6fcfa59 378 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
7c1e52ba 379 char idstr[256], *idstr_ret;
b6fcfa59
EH
380 int ret;
381 uint8_t version_id, len, size;
382 const VMStateDescription *sub_vmsd;
383
384 len = qemu_peek_byte(f, 1);
385 if (len < strlen(vmsd->name) + 1) {
386 /* subsection name has be be "section_name/a" */
023ad1a6 387 trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
b6fcfa59
EH
388 return 0;
389 }
7c1e52ba 390 size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
b6fcfa59 391 if (size != len) {
023ad1a6 392 trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
b6fcfa59
EH
393 return 0;
394 }
7c1e52ba 395 memcpy(idstr, idstr_ret, size);
b6fcfa59
EH
396 idstr[size] = 0;
397
398 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
023ad1a6
DDAG
399 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
400 /* it doesn't have a valid subsection name */
b6fcfa59
EH
401 return 0;
402 }
403 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
404 if (sub_vmsd == NULL) {
023ad1a6 405 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
b6fcfa59
EH
406 return -ENOENT;
407 }
408 qemu_file_skip(f, 1); /* subsection */
409 qemu_file_skip(f, 1); /* len */
410 qemu_file_skip(f, len); /* idstr */
411 version_id = qemu_get_be32(f);
412
413 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
414 if (ret) {
023ad1a6 415 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
b6fcfa59
EH
416 return ret;
417 }
418 }
a5df2a02
DDAG
419
420 trace_vmstate_subsection_load_good(vmsd->name);
b6fcfa59
EH
421 return 0;
422}
423
424static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
8118f095 425 void *opaque, QJSON *vmdesc)
b6fcfa59 426{
5cd8cada 427 const VMStateDescription **sub = vmsd->subsections;
8118f095 428 bool subsection_found = false;
b6fcfa59 429
5cd8cada
JQ
430 while (sub && *sub && (*sub)->needed) {
431 if ((*sub)->needed(opaque)) {
432 const VMStateDescription *vmsd = *sub;
b6fcfa59
EH
433 uint8_t len;
434
8118f095
AG
435 if (vmdesc) {
436 /* Only create subsection array when we have any */
437 if (!subsection_found) {
438 json_start_array(vmdesc, "subsections");
439 subsection_found = true;
440 }
441
442 json_start_object(vmdesc, NULL);
443 }
444
b6fcfa59
EH
445 qemu_put_byte(f, QEMU_VM_SUBSECTION);
446 len = strlen(vmsd->name);
447 qemu_put_byte(f, len);
448 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
449 qemu_put_be32(f, vmsd->version_id);
8118f095
AG
450 vmstate_save_state(f, vmsd, opaque, vmdesc);
451
452 if (vmdesc) {
453 json_end_object(vmdesc);
454 }
b6fcfa59
EH
455 }
456 sub++;
457 }
8118f095
AG
458
459 if (vmdesc && subsection_found) {
460 json_end_array(vmdesc);
461 }
b6fcfa59
EH
462}
463
464/* bool */
465
466static int get_bool(QEMUFile *f, void *pv, size_t size)
467{
468 bool *v = pv;
469 *v = qemu_get_byte(f);
470 return 0;
471}
472
473static void put_bool(QEMUFile *f, void *pv, size_t size)
474{
475 bool *v = pv;
476 qemu_put_byte(f, *v);
477}
478
479const VMStateInfo vmstate_info_bool = {
480 .name = "bool",
481 .get = get_bool,
482 .put = put_bool,
483};
484
485/* 8 bit int */
486
487static int get_int8(QEMUFile *f, void *pv, size_t size)
488{
489 int8_t *v = pv;
490 qemu_get_s8s(f, v);
491 return 0;
492}
493
494static void put_int8(QEMUFile *f, void *pv, size_t size)
495{
496 int8_t *v = pv;
497 qemu_put_s8s(f, v);
498}
499
500const VMStateInfo vmstate_info_int8 = {
501 .name = "int8",
502 .get = get_int8,
503 .put = put_int8,
504};
505
506/* 16 bit int */
507
508static int get_int16(QEMUFile *f, void *pv, size_t size)
509{
510 int16_t *v = pv;
511 qemu_get_sbe16s(f, v);
512 return 0;
513}
514
515static void put_int16(QEMUFile *f, void *pv, size_t size)
516{
517 int16_t *v = pv;
518 qemu_put_sbe16s(f, v);
519}
520
521const VMStateInfo vmstate_info_int16 = {
522 .name = "int16",
523 .get = get_int16,
524 .put = put_int16,
525};
526
527/* 32 bit int */
528
529static int get_int32(QEMUFile *f, void *pv, size_t size)
530{
531 int32_t *v = pv;
532 qemu_get_sbe32s(f, v);
533 return 0;
534}
535
536static void put_int32(QEMUFile *f, void *pv, size_t size)
537{
538 int32_t *v = pv;
539 qemu_put_sbe32s(f, v);
540}
541
542const VMStateInfo vmstate_info_int32 = {
543 .name = "int32",
544 .get = get_int32,
545 .put = put_int32,
546};
547
548/* 32 bit int. See that the received value is the same than the one
549 in the field */
550
551static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
552{
553 int32_t *v = pv;
554 int32_t v2;
555 qemu_get_sbe32s(f, &v2);
556
557 if (*v == v2) {
558 return 0;
559 }
560 return -EINVAL;
561}
562
563const VMStateInfo vmstate_info_int32_equal = {
564 .name = "int32 equal",
565 .get = get_int32_equal,
566 .put = put_int32,
567};
568
d2ef4b61
MT
569/* 32 bit int. Check that the received value is non-negative
570 * and less than or equal to the one in the field.
571 */
b6fcfa59
EH
572
573static int get_int32_le(QEMUFile *f, void *pv, size_t size)
574{
24a370ef
DDAG
575 int32_t *cur = pv;
576 int32_t loaded;
577 qemu_get_sbe32s(f, &loaded);
b6fcfa59 578
d2ef4b61 579 if (loaded >= 0 && loaded <= *cur) {
24a370ef 580 *cur = loaded;
b6fcfa59
EH
581 return 0;
582 }
583 return -EINVAL;
584}
585
586const VMStateInfo vmstate_info_int32_le = {
24a370ef 587 .name = "int32 le",
b6fcfa59
EH
588 .get = get_int32_le,
589 .put = put_int32,
590};
591
592/* 64 bit int */
593
594static int get_int64(QEMUFile *f, void *pv, size_t size)
595{
596 int64_t *v = pv;
597 qemu_get_sbe64s(f, v);
598 return 0;
599}
600
601static void put_int64(QEMUFile *f, void *pv, size_t size)
602{
603 int64_t *v = pv;
604 qemu_put_sbe64s(f, v);
605}
606
607const VMStateInfo vmstate_info_int64 = {
608 .name = "int64",
609 .get = get_int64,
610 .put = put_int64,
611};
612
613/* 8 bit unsigned int */
614
615static int get_uint8(QEMUFile *f, void *pv, size_t size)
616{
617 uint8_t *v = pv;
618 qemu_get_8s(f, v);
619 return 0;
620}
621
622static void put_uint8(QEMUFile *f, void *pv, size_t size)
623{
624 uint8_t *v = pv;
625 qemu_put_8s(f, v);
626}
627
628const VMStateInfo vmstate_info_uint8 = {
629 .name = "uint8",
630 .get = get_uint8,
631 .put = put_uint8,
632};
633
634/* 16 bit unsigned int */
635
636static int get_uint16(QEMUFile *f, void *pv, size_t size)
637{
638 uint16_t *v = pv;
639 qemu_get_be16s(f, v);
640 return 0;
641}
642
643static void put_uint16(QEMUFile *f, void *pv, size_t size)
644{
645 uint16_t *v = pv;
646 qemu_put_be16s(f, v);
647}
648
649const VMStateInfo vmstate_info_uint16 = {
650 .name = "uint16",
651 .get = get_uint16,
652 .put = put_uint16,
653};
654
655/* 32 bit unsigned int */
656
657static int get_uint32(QEMUFile *f, void *pv, size_t size)
658{
659 uint32_t *v = pv;
660 qemu_get_be32s(f, v);
661 return 0;
662}
663
664static void put_uint32(QEMUFile *f, void *pv, size_t size)
665{
666 uint32_t *v = pv;
667 qemu_put_be32s(f, v);
668}
669
670const VMStateInfo vmstate_info_uint32 = {
671 .name = "uint32",
672 .get = get_uint32,
673 .put = put_uint32,
674};
675
676/* 32 bit uint. See that the received value is the same than the one
677 in the field */
678
679static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
680{
681 uint32_t *v = pv;
682 uint32_t v2;
683 qemu_get_be32s(f, &v2);
684
685 if (*v == v2) {
686 return 0;
687 }
688 return -EINVAL;
689}
690
691const VMStateInfo vmstate_info_uint32_equal = {
692 .name = "uint32 equal",
693 .get = get_uint32_equal,
694 .put = put_uint32,
695};
696
697/* 64 bit unsigned int */
698
699static int get_uint64(QEMUFile *f, void *pv, size_t size)
700{
701 uint64_t *v = pv;
702 qemu_get_be64s(f, v);
703 return 0;
704}
705
706static void put_uint64(QEMUFile *f, void *pv, size_t size)
707{
708 uint64_t *v = pv;
709 qemu_put_be64s(f, v);
710}
711
712const VMStateInfo vmstate_info_uint64 = {
713 .name = "uint64",
714 .get = get_uint64,
715 .put = put_uint64,
716};
717
718/* 64 bit unsigned int. See that the received value is the same than the one
719 in the field */
720
721static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
722{
723 uint64_t *v = pv;
724 uint64_t v2;
725 qemu_get_be64s(f, &v2);
726
727 if (*v == v2) {
728 return 0;
729 }
730 return -EINVAL;
731}
732
733const VMStateInfo vmstate_info_uint64_equal = {
734 .name = "int64 equal",
735 .get = get_uint64_equal,
736 .put = put_uint64,
737};
738
739/* 8 bit int. See that the received value is the same than the one
740 in the field */
741
742static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
743{
744 uint8_t *v = pv;
745 uint8_t v2;
746 qemu_get_8s(f, &v2);
747
748 if (*v == v2) {
749 return 0;
750 }
751 return -EINVAL;
752}
753
754const VMStateInfo vmstate_info_uint8_equal = {
755 .name = "uint8 equal",
756 .get = get_uint8_equal,
757 .put = put_uint8,
758};
759
760/* 16 bit unsigned int int. See that the received value is the same than the one
761 in the field */
762
763static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
764{
765 uint16_t *v = pv;
766 uint16_t v2;
767 qemu_get_be16s(f, &v2);
768
769 if (*v == v2) {
770 return 0;
771 }
772 return -EINVAL;
773}
774
775const VMStateInfo vmstate_info_uint16_equal = {
776 .name = "uint16 equal",
777 .get = get_uint16_equal,
778 .put = put_uint16,
779};
780
781/* floating point */
782
783static int get_float64(QEMUFile *f, void *pv, size_t size)
784{
785 float64 *v = pv;
786
787 *v = make_float64(qemu_get_be64(f));
788 return 0;
789}
790
791static void put_float64(QEMUFile *f, void *pv, size_t size)
792{
793 uint64_t *v = pv;
794
795 qemu_put_be64(f, float64_val(*v));
796}
797
798const VMStateInfo vmstate_info_float64 = {
799 .name = "float64",
800 .get = get_float64,
801 .put = put_float64,
802};
803
55174749
JQ
804/* CPU_DoubleU type */
805
806static int get_cpudouble(QEMUFile *f, void *pv, size_t size)
807{
808 CPU_DoubleU *v = pv;
809 qemu_get_be32s(f, &v->l.upper);
810 qemu_get_be32s(f, &v->l.lower);
811 return 0;
812}
813
814static void put_cpudouble(QEMUFile *f, void *pv, size_t size)
815{
816 CPU_DoubleU *v = pv;
817 qemu_put_be32s(f, &v->l.upper);
818 qemu_put_be32s(f, &v->l.lower);
819}
820
821const VMStateInfo vmstate_info_cpudouble = {
822 .name = "CPU_Double_U",
823 .get = get_cpudouble,
824 .put = put_cpudouble,
825};
826
b6fcfa59
EH
827/* uint8_t buffers */
828
829static int get_buffer(QEMUFile *f, void *pv, size_t size)
830{
831 uint8_t *v = pv;
832 qemu_get_buffer(f, v, size);
833 return 0;
834}
835
836static void put_buffer(QEMUFile *f, void *pv, size_t size)
837{
838 uint8_t *v = pv;
839 qemu_put_buffer(f, v, size);
840}
841
842const VMStateInfo vmstate_info_buffer = {
843 .name = "buffer",
844 .get = get_buffer,
845 .put = put_buffer,
846};
847
848/* unused buffers: space that was used for some fields that are
849 not useful anymore */
850
851static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
852{
853 uint8_t buf[1024];
854 int block_len;
855
856 while (size > 0) {
857 block_len = MIN(sizeof(buf), size);
858 size -= block_len;
859 qemu_get_buffer(f, buf, block_len);
860 }
861 return 0;
862}
863
864static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
865{
866 static const uint8_t buf[1024];
867 int block_len;
868
869 while (size > 0) {
870 block_len = MIN(sizeof(buf), size);
871 size -= block_len;
872 qemu_put_buffer(f, buf, block_len);
873 }
874}
875
876const VMStateInfo vmstate_info_unused_buffer = {
877 .name = "unused_buffer",
878 .get = get_unused_buffer,
879 .put = put_unused_buffer,
880};
881
882/* bitmaps (as defined by bitmap.h). Note that size here is the size
883 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
884 * bit words with the bits in big endian order. The in-memory format
885 * is an array of 'unsigned long', which may be either 32 or 64 bits.
886 */
887/* This is the number of 64 bit words sent over the wire */
888#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
889static int get_bitmap(QEMUFile *f, void *pv, size_t size)
890{
891 unsigned long *bmp = pv;
892 int i, idx = 0;
893 for (i = 0; i < BITS_TO_U64S(size); i++) {
894 uint64_t w = qemu_get_be64(f);
895 bmp[idx++] = w;
896 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
897 bmp[idx++] = w >> 32;
898 }
899 }
900 return 0;
901}
902
903static void put_bitmap(QEMUFile *f, void *pv, size_t size)
904{
905 unsigned long *bmp = pv;
906 int i, idx = 0;
907 for (i = 0; i < BITS_TO_U64S(size); i++) {
908 uint64_t w = bmp[idx++];
909 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
910 w |= ((uint64_t)bmp[idx++]) << 32;
911 }
912 qemu_put_be64(f, w);
913 }
914}
915
916const VMStateInfo vmstate_info_bitmap = {
917 .name = "bitmap",
918 .get = get_bitmap,
919 .put = put_bitmap,
920};