xref: /openbmc/qemu/migration/vmstate.c (revision 82565fb6b36cd37a37ff0b3380cbd58f76449957)
1 /*
2  * VMState interpreter
3  *
4  * Copyright (c) 2009-2017 Red Hat Inc
5  *
6  * Authors:
7  *  Juan Quintela <quintela@redhat.com>
8  *
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.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "migration.h"
15 #include "migration/vmstate.h"
16 #include "savevm.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/json-writer.h"
19 #include "qemu-file.h"
20 #include "qemu/bitops.h"
21 #include "qemu/error-report.h"
22 #include "trace.h"
23 
24 static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
25                                    void *opaque, JSONWriter *vmdesc,
26                                    Error **errp);
27 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
28                                    void *opaque);
29 
30 /* Whether this field should exist for either save or load the VM? */
31 static bool
vmstate_field_exists(const VMStateDescription * vmsd,const VMStateField * field,void * opaque,int version_id)32 vmstate_field_exists(const VMStateDescription *vmsd, const VMStateField *field,
33                      void *opaque, int version_id)
34 {
35     bool result;
36 
37     if (field->field_exists) {
38         /* If there's the function checker, that's the solo truth */
39         result = field->field_exists(opaque, version_id);
40         trace_vmstate_field_exists(vmsd->name, field->name, field->version_id,
41                                    version_id, result);
42     } else {
43         /*
44          * Otherwise, we only save/load if field version is same or older.
45          * For example, when loading from an old binary with old version,
46          * we ignore new fields with newer version_ids.
47          */
48         result = field->version_id <= version_id;
49     }
50 
51     return result;
52 }
53 
54 /*
55  * Create a fake nullptr field when there's a NULL pointer detected in the
56  * array of a VMS_ARRAY_OF_POINTER VMSD field.  It's needed because we
57  * can't dereference the NULL pointer.
58  */
59 static const VMStateField *
vmsd_create_fake_nullptr_field(const VMStateField * field)60 vmsd_create_fake_nullptr_field(const VMStateField *field)
61 {
62     VMStateField *fake = g_new0(VMStateField, 1);
63 
64     /* It can only happen on an array of pointers! */
65     assert(field->flags & VMS_ARRAY_OF_POINTER);
66 
67     /* Some of fake's properties should match the original's */
68     fake->name = field->name;
69     fake->version_id = field->version_id;
70 
71     /* Do not need "field_exists" check as it always exists (which is null) */
72     fake->field_exists = NULL;
73 
74     /* See vmstate_info_nullptr - use 1 byte to represent nullptr */
75     fake->size = 1;
76     fake->info = &vmstate_info_nullptr;
77     fake->flags = VMS_SINGLE;
78 
79     /* All the rest fields shouldn't matter.. */
80 
81     return (const VMStateField *)fake;
82 }
83 
vmstate_n_elems(void * opaque,const VMStateField * field)84 static int vmstate_n_elems(void *opaque, const VMStateField *field)
85 {
86     int n_elems = 1;
87 
88     if (field->flags & VMS_ARRAY) {
89         n_elems = field->num;
90     } else if (field->flags & VMS_VARRAY_INT32) {
91         n_elems = *(int32_t *)(opaque + field->num_offset);
92     } else if (field->flags & VMS_VARRAY_UINT32) {
93         n_elems = *(uint32_t *)(opaque + field->num_offset);
94     } else if (field->flags & VMS_VARRAY_UINT16) {
95         n_elems = *(uint16_t *)(opaque + field->num_offset);
96     } else if (field->flags & VMS_VARRAY_UINT8) {
97         n_elems = *(uint8_t *)(opaque + field->num_offset);
98     }
99 
100     if (field->flags & VMS_MULTIPLY_ELEMENTS) {
101         n_elems *= field->num;
102     }
103 
104     trace_vmstate_n_elems(field->name, n_elems);
105     return n_elems;
106 }
107 
vmstate_size(void * opaque,const VMStateField * field)108 static int vmstate_size(void *opaque, const VMStateField *field)
109 {
110     int size = field->size;
111 
112     if (field->flags & VMS_VBUFFER) {
113         size = *(int32_t *)(opaque + field->size_offset);
114         if (field->flags & VMS_MULTIPLY) {
115             size *= field->size;
116         }
117     }
118 
119     return size;
120 }
121 
vmstate_handle_alloc(void * ptr,const VMStateField * field,void * opaque)122 static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
123                                  void *opaque)
124 {
125     if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
126         gsize size = vmstate_size(opaque, field);
127         size *= vmstate_n_elems(opaque, field);
128         if (size) {
129             *(void **)ptr = g_malloc(size);
130         }
131     }
132 }
133 
vmstate_load_state(QEMUFile * f,const VMStateDescription * vmsd,void * opaque,int version_id)134 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
135                        void *opaque, int version_id)
136 {
137     const VMStateField *field = vmsd->fields;
138     int ret = 0;
139 
140     trace_vmstate_load_state(vmsd->name, version_id);
141     if (version_id > vmsd->version_id) {
142         error_report("%s: incoming version_id %d is too new "
143                      "for local version_id %d",
144                      vmsd->name, version_id, vmsd->version_id);
145         trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
146         return -EINVAL;
147     }
148     if  (version_id < vmsd->minimum_version_id) {
149         error_report("%s: incoming version_id %d is too old "
150                      "for local minimum version_id  %d",
151                      vmsd->name, version_id, vmsd->minimum_version_id);
152         trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
153         return -EINVAL;
154     }
155     if (vmsd->pre_load) {
156         ret = vmsd->pre_load(opaque);
157         if (ret) {
158             return ret;
159         }
160     }
161     while (field->name) {
162         bool exists = vmstate_field_exists(vmsd, field, opaque, version_id);
163         trace_vmstate_load_state_field(vmsd->name, field->name, exists);
164         if (exists) {
165             void *first_elem = opaque + field->offset;
166             int i, n_elems = vmstate_n_elems(opaque, field);
167             int size = vmstate_size(opaque, field);
168 
169             vmstate_handle_alloc(first_elem, field, opaque);
170             if (field->flags & VMS_POINTER) {
171                 first_elem = *(void **)first_elem;
172                 assert(first_elem || !n_elems || !size);
173             }
174             for (i = 0; i < n_elems; i++) {
175                 void *curr_elem = first_elem + size * i;
176                 const VMStateField *inner_field;
177 
178                 if (field->flags & VMS_ARRAY_OF_POINTER) {
179                     curr_elem = *(void **)curr_elem;
180                 }
181 
182                 if (!curr_elem && size) {
183                     /*
184                      * If null pointer found (which should only happen in
185                      * an array of pointers), use null placeholder and do
186                      * not follow.
187                      */
188                     inner_field = vmsd_create_fake_nullptr_field(field);
189                 } else {
190                     inner_field = field;
191                 }
192 
193                 if (inner_field->flags & VMS_STRUCT) {
194                     ret = vmstate_load_state(f, inner_field->vmsd, curr_elem,
195                                              inner_field->vmsd->version_id);
196                 } else if (inner_field->flags & VMS_VSTRUCT) {
197                     ret = vmstate_load_state(f, inner_field->vmsd, curr_elem,
198                                              inner_field->struct_version_id);
199                 } else {
200                     ret = inner_field->info->get(f, curr_elem, size,
201                                                  inner_field);
202                 }
203 
204                 /* If we used a fake temp field.. free it now */
205                 if (inner_field != field) {
206                     g_clear_pointer((gpointer *)&inner_field, g_free);
207                 }
208 
209                 if (ret >= 0) {
210                     ret = qemu_file_get_error(f);
211                 }
212                 if (ret < 0) {
213                     qemu_file_set_error(f, ret);
214                     error_report("Failed to load %s:%s", vmsd->name,
215                                  field->name);
216                     trace_vmstate_load_field_error(field->name, ret);
217                     return ret;
218                 }
219             }
220         } else if (field->flags & VMS_MUST_EXIST) {
221             error_report("Input validation failed: %s/%s",
222                          vmsd->name, field->name);
223             return -1;
224         }
225         field++;
226     }
227     assert(field->flags == VMS_END);
228     ret = vmstate_subsection_load(f, vmsd, opaque);
229     if (ret != 0) {
230         qemu_file_set_error(f, ret);
231         return ret;
232     }
233     if (vmsd->post_load) {
234         ret = vmsd->post_load(opaque, version_id);
235     }
236     trace_vmstate_load_state_end(vmsd->name, "end", ret);
237     return ret;
238 }
239 
vmfield_name_num(const VMStateField * start,const VMStateField * search)240 static int vmfield_name_num(const VMStateField *start,
241                             const VMStateField *search)
242 {
243     const VMStateField *field;
244     int found = 0;
245 
246     for (field = start; field->name; field++) {
247         if (!strcmp(field->name, search->name)) {
248             if (field == search) {
249                 return found;
250             }
251             found++;
252         }
253     }
254 
255     return -1;
256 }
257 
vmfield_name_is_unique(const VMStateField * start,const VMStateField * search)258 static bool vmfield_name_is_unique(const VMStateField *start,
259                                    const VMStateField *search)
260 {
261     const VMStateField *field;
262     int found = 0;
263 
264     for (field = start; field->name; field++) {
265         if (!strcmp(field->name, search->name)) {
266             found++;
267             /* name found more than once, so it's not unique */
268             if (found > 1) {
269                 return false;
270             }
271         }
272     }
273 
274     return true;
275 }
276 
vmfield_get_type_name(const VMStateField * field)277 static const char *vmfield_get_type_name(const VMStateField *field)
278 {
279     const char *type = "unknown";
280 
281     if (field->flags & VMS_STRUCT) {
282         type = "struct";
283     } else if (field->flags & VMS_VSTRUCT) {
284         type = "vstruct";
285     } else if (field->info->name) {
286         type = field->info->name;
287     }
288 
289     return type;
290 }
291 
vmsd_can_compress(const VMStateField * field)292 static bool vmsd_can_compress(const VMStateField *field)
293 {
294     if (field->field_exists) {
295         /* Dynamically existing fields mess up compression */
296         return false;
297     }
298 
299     if (field->flags & VMS_STRUCT) {
300         const VMStateField *sfield = field->vmsd->fields;
301         while (sfield->name) {
302             if (!vmsd_can_compress(sfield)) {
303                 /* Child elements can't compress, so can't we */
304                 return false;
305             }
306             sfield++;
307         }
308 
309         if (field->vmsd->subsections) {
310             /* Subsections may come and go, better don't compress */
311             return false;
312         }
313     }
314 
315     return true;
316 }
317 
vmsd_desc_field_start(const VMStateDescription * vmsd,JSONWriter * vmdesc,const VMStateField * field,int i,int max)318 static void vmsd_desc_field_start(const VMStateDescription *vmsd,
319                                   JSONWriter *vmdesc,
320                                   const VMStateField *field, int i, int max)
321 {
322     char *name, *old_name;
323     bool is_array = max > 1;
324     bool can_compress = vmsd_can_compress(field);
325 
326     if (!vmdesc) {
327         return;
328     }
329 
330     name = g_strdup(field->name);
331 
332     /* Field name is not unique, need to make it unique */
333     if (!vmfield_name_is_unique(vmsd->fields, field)) {
334         int num = vmfield_name_num(vmsd->fields, field);
335         old_name = name;
336         name = g_strdup_printf("%s[%d]", name, num);
337         g_free(old_name);
338     }
339 
340     json_writer_start_object(vmdesc, NULL);
341     json_writer_str(vmdesc, "name", name);
342     if (is_array) {
343         if (can_compress) {
344             json_writer_int64(vmdesc, "array_len", max);
345         } else {
346             json_writer_int64(vmdesc, "index", i);
347         }
348     }
349     json_writer_str(vmdesc, "type", vmfield_get_type_name(field));
350 
351     if (field->flags & VMS_STRUCT) {
352         json_writer_start_object(vmdesc, "struct");
353     }
354 
355     g_free(name);
356 }
357 
vmsd_desc_field_end(const VMStateDescription * vmsd,JSONWriter * vmdesc,const VMStateField * field,size_t size)358 static void vmsd_desc_field_end(const VMStateDescription *vmsd,
359                                 JSONWriter *vmdesc,
360                                 const VMStateField *field, size_t size)
361 {
362     if (!vmdesc) {
363         return;
364     }
365 
366     if (field->flags & VMS_STRUCT) {
367         /* We printed a struct in between, close its child object */
368         json_writer_end_object(vmdesc);
369     }
370 
371     json_writer_int64(vmdesc, "size", size);
372     json_writer_end_object(vmdesc);
373 }
374 
375 
vmstate_section_needed(const VMStateDescription * vmsd,void * opaque)376 bool vmstate_section_needed(const VMStateDescription *vmsd, void *opaque)
377 {
378     if (vmsd->needed && !vmsd->needed(opaque)) {
379         /* optional section not needed */
380         return false;
381     }
382     return true;
383 }
384 
385 
vmstate_save_state(QEMUFile * f,const VMStateDescription * vmsd,void * opaque,JSONWriter * vmdesc_id)386 int vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
387                        void *opaque, JSONWriter *vmdesc_id)
388 {
389     return vmstate_save_state_v(f, vmsd, opaque, vmdesc_id, vmsd->version_id, NULL);
390 }
391 
vmstate_save_state_with_err(QEMUFile * f,const VMStateDescription * vmsd,void * opaque,JSONWriter * vmdesc_id,Error ** errp)392 int vmstate_save_state_with_err(QEMUFile *f, const VMStateDescription *vmsd,
393                        void *opaque, JSONWriter *vmdesc_id, Error **errp)
394 {
395     return vmstate_save_state_v(f, vmsd, opaque, vmdesc_id, vmsd->version_id, errp);
396 }
397 
vmstate_save_state_v(QEMUFile * f,const VMStateDescription * vmsd,void * opaque,JSONWriter * vmdesc,int version_id,Error ** errp)398 int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
399                          void *opaque, JSONWriter *vmdesc, int version_id, Error **errp)
400 {
401     int ret = 0;
402     const VMStateField *field = vmsd->fields;
403 
404     trace_vmstate_save_state_top(vmsd->name);
405 
406     if (vmsd->pre_save) {
407         ret = vmsd->pre_save(opaque);
408         trace_vmstate_save_state_pre_save_res(vmsd->name, ret);
409         if (ret) {
410             error_setg(errp, "pre-save failed: %s", vmsd->name);
411             return ret;
412         }
413     }
414 
415     if (vmdesc) {
416         json_writer_str(vmdesc, "vmsd_name", vmsd->name);
417         json_writer_int64(vmdesc, "version", version_id);
418         json_writer_start_array(vmdesc, "fields");
419     }
420 
421     while (field->name) {
422         if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
423             void *first_elem = opaque + field->offset;
424             int i, n_elems = vmstate_n_elems(opaque, field);
425             int size = vmstate_size(opaque, field);
426             uint64_t old_offset, written_bytes;
427             JSONWriter *vmdesc_loop = vmdesc;
428             bool is_prev_null = false;
429 
430             trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
431             if (field->flags & VMS_POINTER) {
432                 first_elem = *(void **)first_elem;
433                 assert(first_elem || !n_elems || !size);
434             }
435 
436             for (i = 0; i < n_elems; i++) {
437                 void *curr_elem = first_elem + size * i;
438                 const VMStateField *inner_field;
439                 bool is_null;
440                 int max_elems = n_elems - i;
441 
442                 old_offset = qemu_file_transferred(f);
443                 if (field->flags & VMS_ARRAY_OF_POINTER) {
444                     assert(curr_elem);
445                     curr_elem = *(void **)curr_elem;
446                 }
447 
448                 if (!curr_elem && size) {
449                     /*
450                      * If null pointer found (which should only happen in
451                      * an array of pointers), use null placeholder and do
452                      * not follow.
453                      */
454                     inner_field = vmsd_create_fake_nullptr_field(field);
455                     is_null = true;
456                 } else {
457                     inner_field = field;
458                     is_null = false;
459                 }
460 
461                 /*
462                  * Due to the fake nullptr handling above, if there's mixed
463                  * null/non-null data, it doesn't make sense to emit a
464                  * compressed array representation spanning the entire array
465                  * because the field types will be different (e.g. struct
466                  * vs. nullptr). Search ahead for the next null/non-null element
467                  * and start a new compressed array if found.
468                  */
469                 if (field->flags & VMS_ARRAY_OF_POINTER &&
470                     is_null != is_prev_null) {
471 
472                     is_prev_null = is_null;
473                     vmdesc_loop = vmdesc;
474 
475                     for (int j = i + 1; j < n_elems; j++) {
476                         void *elem = *(void **)(first_elem + size * j);
477                         bool elem_is_null = !elem && size;
478 
479                         if (is_null != elem_is_null) {
480                             max_elems = j - i;
481                             break;
482                         }
483                     }
484                 }
485 
486                 vmsd_desc_field_start(vmsd, vmdesc_loop, inner_field,
487                                       i, max_elems);
488 
489                 if (inner_field->flags & VMS_STRUCT) {
490                     ret = vmstate_save_state(f, inner_field->vmsd,
491                                              curr_elem, vmdesc_loop);
492                 } else if (inner_field->flags & VMS_VSTRUCT) {
493                     ret = vmstate_save_state_v(f, inner_field->vmsd,
494                                                curr_elem, vmdesc_loop,
495                                                inner_field->struct_version_id,
496                                                errp);
497                 } else {
498                     ret = inner_field->info->put(f, curr_elem, size,
499                                                  inner_field, vmdesc_loop);
500                 }
501 
502                 written_bytes = qemu_file_transferred(f) - old_offset;
503                 vmsd_desc_field_end(vmsd, vmdesc_loop, inner_field,
504                                     written_bytes);
505 
506                 /* If we used a fake temp field.. free it now */
507                 if (inner_field != field) {
508                     g_clear_pointer((gpointer *)&inner_field, g_free);
509                 }
510 
511                 if (ret) {
512                     error_setg(errp, "Save of field %s/%s failed",
513                                 vmsd->name, field->name);
514                     if (vmsd->post_save) {
515                         vmsd->post_save(opaque);
516                     }
517                     return ret;
518                 }
519 
520                 /* Compressed arrays only care about the first element */
521                 if (vmdesc_loop && vmsd_can_compress(field)) {
522                     vmdesc_loop = NULL;
523                 }
524             }
525         } else {
526             if (field->flags & VMS_MUST_EXIST) {
527                 error_report("Output state validation failed: %s/%s",
528                         vmsd->name, field->name);
529                 assert(!(field->flags & VMS_MUST_EXIST));
530             }
531         }
532         field++;
533     }
534     assert(field->flags == VMS_END);
535 
536     if (vmdesc) {
537         json_writer_end_array(vmdesc);
538     }
539 
540     ret = vmstate_subsection_save(f, vmsd, opaque, vmdesc, errp);
541 
542     if (vmsd->post_save) {
543         int ps_ret = vmsd->post_save(opaque);
544         if (!ret && ps_ret) {
545             ret = ps_ret;
546             error_setg(errp, "post-save failed: %s", vmsd->name);
547         }
548     }
549     return ret;
550 }
551 
552 static const VMStateDescription *
vmstate_get_subsection(const VMStateDescription * const * sub,const char * idstr)553 vmstate_get_subsection(const VMStateDescription * const *sub,
554                        const char *idstr)
555 {
556     if (sub) {
557         for (const VMStateDescription *s = *sub; s ; s = *++sub) {
558             if (strcmp(idstr, s->name) == 0) {
559                 return s;
560             }
561         }
562     }
563     return NULL;
564 }
565 
vmstate_subsection_load(QEMUFile * f,const VMStateDescription * vmsd,void * opaque)566 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
567                                    void *opaque)
568 {
569     trace_vmstate_subsection_load(vmsd->name);
570 
571     while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
572         char idstr[256], *idstr_ret;
573         int ret;
574         uint8_t version_id, len, size;
575         const VMStateDescription *sub_vmsd;
576 
577         len = qemu_peek_byte(f, 1);
578         if (len < strlen(vmsd->name) + 1) {
579             /* subsection name has to be "section_name/a" */
580             trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
581             return 0;
582         }
583         size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
584         if (size != len) {
585             trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
586             return 0;
587         }
588         memcpy(idstr, idstr_ret, size);
589         idstr[size] = 0;
590 
591         if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
592             trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
593             /* it doesn't have a valid subsection name */
594             return 0;
595         }
596         sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
597         if (sub_vmsd == NULL) {
598             trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
599             return -ENOENT;
600         }
601         qemu_file_skip(f, 1); /* subsection */
602         qemu_file_skip(f, 1); /* len */
603         qemu_file_skip(f, len); /* idstr */
604         version_id = qemu_get_be32(f);
605 
606         ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
607         if (ret) {
608             trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
609             return ret;
610         }
611     }
612 
613     trace_vmstate_subsection_load_good(vmsd->name);
614     return 0;
615 }
616 
vmstate_subsection_save(QEMUFile * f,const VMStateDescription * vmsd,void * opaque,JSONWriter * vmdesc,Error ** errp)617 static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
618                                    void *opaque, JSONWriter *vmdesc,
619                                    Error **errp)
620 {
621     const VMStateDescription * const *sub = vmsd->subsections;
622     bool vmdesc_has_subsections = false;
623     int ret = 0;
624 
625     trace_vmstate_subsection_save_top(vmsd->name);
626     while (sub && *sub) {
627         if (vmstate_section_needed(*sub, opaque)) {
628             const VMStateDescription *vmsdsub = *sub;
629             uint8_t len;
630 
631             trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
632             if (vmdesc) {
633                 /* Only create subsection array when we have any */
634                 if (!vmdesc_has_subsections) {
635                     json_writer_start_array(vmdesc, "subsections");
636                     vmdesc_has_subsections = true;
637                 }
638 
639                 json_writer_start_object(vmdesc, NULL);
640             }
641 
642             qemu_put_byte(f, QEMU_VM_SUBSECTION);
643             len = strlen(vmsdsub->name);
644             qemu_put_byte(f, len);
645             qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
646             qemu_put_be32(f, vmsdsub->version_id);
647             ret = vmstate_save_state_with_err(f, vmsdsub, opaque, vmdesc, errp);
648             if (ret) {
649                 return ret;
650             }
651 
652             if (vmdesc) {
653                 json_writer_end_object(vmdesc);
654             }
655         }
656         sub++;
657     }
658 
659     if (vmdesc_has_subsections) {
660         json_writer_end_array(vmdesc);
661     }
662 
663     return ret;
664 }
665