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