4 * Copyright (c) 2009-2017 Red Hat Inc
7 * Juan Quintela <quintela@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
15 #include "migration.h"
16 #include "migration/vmstate.h"
18 #include "qemu-file.h"
19 #include "qemu/bitops.h"
20 #include "qemu/error-report.h"
24 static int vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
25 void *opaque
, QJSON
*vmdesc
);
26 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
29 static int vmstate_n_elems(void *opaque
, const VMStateField
*field
)
33 if (field
->flags
& VMS_ARRAY
) {
35 } else if (field
->flags
& VMS_VARRAY_INT32
) {
36 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
37 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
38 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
39 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
40 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
41 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
42 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
45 if (field
->flags
& VMS_MULTIPLY_ELEMENTS
) {
46 n_elems
*= field
->num
;
49 trace_vmstate_n_elems(field
->name
, n_elems
);
53 static int vmstate_size(void *opaque
, const VMStateField
*field
)
55 int size
= field
->size
;
57 if (field
->flags
& VMS_VBUFFER
) {
58 size
= *(int32_t *)(opaque
+field
->size_offset
);
59 if (field
->flags
& VMS_MULTIPLY
) {
67 static void vmstate_handle_alloc(void *ptr
, const VMStateField
*field
,
70 if (field
->flags
& VMS_POINTER
&& field
->flags
& VMS_ALLOC
) {
71 gsize size
= vmstate_size(opaque
, field
);
72 size
*= vmstate_n_elems(opaque
, field
);
74 *(void **)ptr
= g_malloc(size
);
79 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
80 void *opaque
, int version_id
)
82 const VMStateField
*field
= vmsd
->fields
;
85 trace_vmstate_load_state(vmsd
->name
, version_id
);
86 if (version_id
> vmsd
->version_id
) {
87 error_report("%s: incoming version_id %d is too new "
88 "for local version_id %d",
89 vmsd
->name
, version_id
, vmsd
->version_id
);
90 trace_vmstate_load_state_end(vmsd
->name
, "too new", -EINVAL
);
93 if (version_id
< vmsd
->minimum_version_id
) {
94 if (vmsd
->load_state_old
&&
95 version_id
>= vmsd
->minimum_version_id_old
) {
96 ret
= vmsd
->load_state_old(f
, opaque
, version_id
);
97 trace_vmstate_load_state_end(vmsd
->name
, "old path", ret
);
100 error_report("%s: incoming version_id %d is too old "
101 "for local minimum version_id %d",
102 vmsd
->name
, version_id
, vmsd
->minimum_version_id
);
103 trace_vmstate_load_state_end(vmsd
->name
, "too old", -EINVAL
);
106 if (vmsd
->pre_load
) {
107 int ret
= vmsd
->pre_load(opaque
);
112 while (field
->name
) {
113 trace_vmstate_load_state_field(vmsd
->name
, field
->name
);
114 if ((field
->field_exists
&&
115 field
->field_exists(opaque
, version_id
)) ||
116 (!field
->field_exists
&&
117 field
->version_id
<= version_id
)) {
118 void *first_elem
= opaque
+ field
->offset
;
119 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
120 int size
= vmstate_size(opaque
, field
);
122 vmstate_handle_alloc(first_elem
, field
, opaque
);
123 if (field
->flags
& VMS_POINTER
) {
124 first_elem
= *(void **)first_elem
;
125 assert(first_elem
|| !n_elems
|| !size
);
127 for (i
= 0; i
< n_elems
; i
++) {
128 void *curr_elem
= first_elem
+ size
* i
;
130 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
131 curr_elem
= *(void **)curr_elem
;
133 if (!curr_elem
&& size
) {
134 /* if null pointer check placeholder and do not follow */
135 assert(field
->flags
& VMS_ARRAY_OF_POINTER
);
136 ret
= vmstate_info_nullptr
.get(f
, curr_elem
, size
, NULL
);
137 } else if (field
->flags
& VMS_STRUCT
) {
138 ret
= vmstate_load_state(f
, field
->vmsd
, curr_elem
,
139 field
->vmsd
->version_id
);
140 } else if (field
->flags
& VMS_VSTRUCT
) {
141 ret
= vmstate_load_state(f
, field
->vmsd
, curr_elem
,
142 field
->struct_version_id
);
144 ret
= field
->info
->get(f
, curr_elem
, size
, field
);
147 ret
= qemu_file_get_error(f
);
150 qemu_file_set_error(f
, ret
);
151 error_report("Failed to load %s:%s", vmsd
->name
,
153 trace_vmstate_load_field_error(field
->name
, ret
);
157 } else if (field
->flags
& VMS_MUST_EXIST
) {
158 error_report("Input validation failed: %s/%s",
159 vmsd
->name
, field
->name
);
164 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
168 if (vmsd
->post_load
) {
169 ret
= vmsd
->post_load(opaque
, version_id
);
171 trace_vmstate_load_state_end(vmsd
->name
, "end", ret
);
175 static int vmfield_name_num(const VMStateField
*start
,
176 const VMStateField
*search
)
178 const VMStateField
*field
;
181 for (field
= start
; field
->name
; field
++) {
182 if (!strcmp(field
->name
, search
->name
)) {
183 if (field
== search
) {
193 static bool vmfield_name_is_unique(const VMStateField
*start
,
194 const VMStateField
*search
)
196 const VMStateField
*field
;
199 for (field
= start
; field
->name
; field
++) {
200 if (!strcmp(field
->name
, search
->name
)) {
202 /* name found more than once, so it's not unique */
212 static const char *vmfield_get_type_name(const VMStateField
*field
)
214 const char *type
= "unknown";
216 if (field
->flags
& VMS_STRUCT
) {
218 } else if (field
->flags
& VMS_VSTRUCT
) {
220 } else if (field
->info
->name
) {
221 type
= field
->info
->name
;
227 static bool vmsd_can_compress(const VMStateField
*field
)
229 if (field
->field_exists
) {
230 /* Dynamically existing fields mess up compression */
234 if (field
->flags
& VMS_STRUCT
) {
235 const VMStateField
*sfield
= field
->vmsd
->fields
;
236 while (sfield
->name
) {
237 if (!vmsd_can_compress(sfield
)) {
238 /* Child elements can't compress, so can't we */
244 if (field
->vmsd
->subsections
) {
245 /* Subsections may come and go, better don't compress */
253 static void vmsd_desc_field_start(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
254 const VMStateField
*field
, int i
, int max
)
256 char *name
, *old_name
;
257 bool is_array
= max
> 1;
258 bool can_compress
= vmsd_can_compress(field
);
264 name
= g_strdup(field
->name
);
266 /* Field name is not unique, need to make it unique */
267 if (!vmfield_name_is_unique(vmsd
->fields
, field
)) {
268 int num
= vmfield_name_num(vmsd
->fields
, field
);
270 name
= g_strdup_printf("%s[%d]", name
, num
);
274 json_start_object(vmdesc
, NULL
);
275 json_prop_str(vmdesc
, "name", name
);
278 json_prop_int(vmdesc
, "array_len", max
);
280 json_prop_int(vmdesc
, "index", i
);
283 json_prop_str(vmdesc
, "type", vmfield_get_type_name(field
));
285 if (field
->flags
& VMS_STRUCT
) {
286 json_start_object(vmdesc
, "struct");
292 static void vmsd_desc_field_end(const VMStateDescription
*vmsd
, QJSON
*vmdesc
,
293 const VMStateField
*field
, size_t size
, int i
)
299 if (field
->flags
& VMS_STRUCT
) {
300 /* We printed a struct in between, close its child object */
301 json_end_object(vmdesc
);
304 json_prop_int(vmdesc
, "size", size
);
305 json_end_object(vmdesc
);
309 bool vmstate_save_needed(const VMStateDescription
*vmsd
, void *opaque
)
311 if (vmsd
->needed
&& !vmsd
->needed(opaque
)) {
312 /* optional section not needed */
319 int vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
320 void *opaque
, QJSON
*vmdesc_id
)
322 return vmstate_save_state_v(f
, vmsd
, opaque
, vmdesc_id
, vmsd
->version_id
);
325 int vmstate_save_state_v(QEMUFile
*f
, const VMStateDescription
*vmsd
,
326 void *opaque
, QJSON
*vmdesc
, int version_id
)
329 const VMStateField
*field
= vmsd
->fields
;
331 trace_vmstate_save_state_top(vmsd
->name
);
333 if (vmsd
->pre_save
) {
334 ret
= vmsd
->pre_save(opaque
);
335 trace_vmstate_save_state_pre_save_res(vmsd
->name
, ret
);
337 error_report("pre-save failed: %s", vmsd
->name
);
343 json_prop_str(vmdesc
, "vmsd_name", vmsd
->name
);
344 json_prop_int(vmdesc
, "version", version_id
);
345 json_start_array(vmdesc
, "fields");
348 while (field
->name
) {
349 if ((field
->field_exists
&&
350 field
->field_exists(opaque
, version_id
)) ||
351 (!field
->field_exists
&&
352 field
->version_id
<= version_id
)) {
353 void *first_elem
= opaque
+ field
->offset
;
354 int i
, n_elems
= vmstate_n_elems(opaque
, field
);
355 int size
= vmstate_size(opaque
, field
);
356 int64_t old_offset
, written_bytes
;
357 QJSON
*vmdesc_loop
= vmdesc
;
359 trace_vmstate_save_state_loop(vmsd
->name
, field
->name
, n_elems
);
360 if (field
->flags
& VMS_POINTER
) {
361 first_elem
= *(void **)first_elem
;
362 assert(first_elem
|| !n_elems
|| !size
);
364 for (i
= 0; i
< n_elems
; i
++) {
365 void *curr_elem
= first_elem
+ size
* i
;
368 vmsd_desc_field_start(vmsd
, vmdesc_loop
, field
, i
, n_elems
);
369 old_offset
= qemu_ftell_fast(f
);
370 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
372 curr_elem
= *(void **)curr_elem
;
374 if (!curr_elem
&& size
) {
375 /* if null pointer write placeholder and do not follow */
376 assert(field
->flags
& VMS_ARRAY_OF_POINTER
);
377 ret
= vmstate_info_nullptr
.put(f
, curr_elem
, size
, NULL
,
379 } else if (field
->flags
& VMS_STRUCT
) {
380 ret
= vmstate_save_state(f
, field
->vmsd
, curr_elem
,
382 } else if (field
->flags
& VMS_VSTRUCT
) {
383 ret
= vmstate_save_state_v(f
, field
->vmsd
, curr_elem
,
385 field
->struct_version_id
);
387 ret
= field
->info
->put(f
, curr_elem
, size
, field
,
391 error_report("Save of field %s/%s failed",
392 vmsd
->name
, field
->name
);
396 written_bytes
= qemu_ftell_fast(f
) - old_offset
;
397 vmsd_desc_field_end(vmsd
, vmdesc_loop
, field
, written_bytes
, i
);
399 /* Compressed arrays only care about the first element */
400 if (vmdesc_loop
&& vmsd_can_compress(field
)) {
405 if (field
->flags
& VMS_MUST_EXIST
) {
406 error_report("Output state validation failed: %s/%s",
407 vmsd
->name
, field
->name
);
408 assert(!(field
->flags
& VMS_MUST_EXIST
));
415 json_end_array(vmdesc
);
418 return vmstate_subsection_save(f
, vmsd
, opaque
, vmdesc
);
421 static const VMStateDescription
*
422 vmstate_get_subsection(const VMStateDescription
**sub
, char *idstr
)
424 while (sub
&& *sub
) {
425 if (strcmp(idstr
, (*sub
)->name
) == 0) {
433 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
436 trace_vmstate_subsection_load(vmsd
->name
);
438 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
439 char idstr
[256], *idstr_ret
;
441 uint8_t version_id
, len
, size
;
442 const VMStateDescription
*sub_vmsd
;
444 len
= qemu_peek_byte(f
, 1);
445 if (len
< strlen(vmsd
->name
) + 1) {
446 /* subsection name has be be "section_name/a" */
447 trace_vmstate_subsection_load_bad(vmsd
->name
, "(short)", "");
450 size
= qemu_peek_buffer(f
, (uint8_t **)&idstr_ret
, len
, 2);
452 trace_vmstate_subsection_load_bad(vmsd
->name
, "(peek fail)", "");
455 memcpy(idstr
, idstr_ret
, size
);
458 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
459 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(prefix)");
460 /* it doesn't have a valid subsection name */
463 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
464 if (sub_vmsd
== NULL
) {
465 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(lookup)");
468 qemu_file_skip(f
, 1); /* subsection */
469 qemu_file_skip(f
, 1); /* len */
470 qemu_file_skip(f
, len
); /* idstr */
471 version_id
= qemu_get_be32(f
);
473 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
475 trace_vmstate_subsection_load_bad(vmsd
->name
, idstr
, "(child)");
480 trace_vmstate_subsection_load_good(vmsd
->name
);
484 static int vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
485 void *opaque
, QJSON
*vmdesc
)
487 const VMStateDescription
**sub
= vmsd
->subsections
;
488 bool subsection_found
= false;
491 trace_vmstate_subsection_save_top(vmsd
->name
);
492 while (sub
&& *sub
) {
493 if (vmstate_save_needed(*sub
, opaque
)) {
494 const VMStateDescription
*vmsdsub
= *sub
;
497 trace_vmstate_subsection_save_loop(vmsd
->name
, vmsdsub
->name
);
499 /* Only create subsection array when we have any */
500 if (!subsection_found
) {
501 json_start_array(vmdesc
, "subsections");
502 subsection_found
= true;
505 json_start_object(vmdesc
, NULL
);
508 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
509 len
= strlen(vmsdsub
->name
);
510 qemu_put_byte(f
, len
);
511 qemu_put_buffer(f
, (uint8_t *)vmsdsub
->name
, len
);
512 qemu_put_be32(f
, vmsdsub
->version_id
);
513 ret
= vmstate_save_state(f
, vmsdsub
, opaque
, vmdesc
);
519 json_end_object(vmdesc
);
525 if (vmdesc
&& subsection_found
) {
526 json_end_array(vmdesc
);