xref: /openbmc/qemu/migration/savevm.c (revision d528227d4c30869dce6fa81124d12b39bcb987cf)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2009-2015 Red Hat Inc
6  *
7  * Authors:
8  *  Juan Quintela <quintela@redhat.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  */
28 
29 #include "qemu/osdep.h"
30 #include "hw/boards.h"
31 #include "hw/xen/xen.h"
32 #include "net/net.h"
33 #include "migration.h"
34 #include "migration/snapshot.h"
35 #include "migration/misc.h"
36 #include "migration/register.h"
37 #include "migration/global_state.h"
38 #include "ram.h"
39 #include "qemu-file-channel.h"
40 #include "qemu-file.h"
41 #include "savevm.h"
42 #include "postcopy-ram.h"
43 #include "qapi/qmp/qerror.h"
44 #include "qemu/error-report.h"
45 #include "sysemu/cpus.h"
46 #include "exec/memory.h"
47 #include "exec/target_page.h"
48 #include "qmp-commands.h"
49 #include "trace.h"
50 #include "qemu/iov.h"
51 #include "block/snapshot.h"
52 #include "qemu/cutils.h"
53 #include "io/channel-buffer.h"
54 #include "io/channel-file.h"
55 
56 #ifndef ETH_P_RARP
57 #define ETH_P_RARP 0x8035
58 #endif
59 #define ARP_HTYPE_ETH 0x0001
60 #define ARP_PTYPE_IP 0x0800
61 #define ARP_OP_REQUEST_REV 0x3
62 
63 const unsigned int postcopy_ram_discard_version = 0;
64 
65 static bool skip_section_footers;
66 
67 /* Subcommands for QEMU_VM_COMMAND */
68 enum qemu_vm_cmd {
69     MIG_CMD_INVALID = 0,   /* Must be 0 */
70     MIG_CMD_OPEN_RETURN_PATH,  /* Tell the dest to open the Return path */
71     MIG_CMD_PING,              /* Request a PONG on the RP */
72 
73     MIG_CMD_POSTCOPY_ADVISE,       /* Prior to any page transfers, just
74                                       warn we might want to do PC */
75     MIG_CMD_POSTCOPY_LISTEN,       /* Start listening for incoming
76                                       pages as it's running. */
77     MIG_CMD_POSTCOPY_RUN,          /* Start execution */
78 
79     MIG_CMD_POSTCOPY_RAM_DISCARD,  /* A list of pages to discard that
80                                       were previously sent during
81                                       precopy but are dirty. */
82     MIG_CMD_PACKAGED,          /* Send a wrapped stream within this stream */
83     MIG_CMD_MAX
84 };
85 
86 #define MAX_VM_CMD_PACKAGED_SIZE (1ul << 24)
87 static struct mig_cmd_args {
88     ssize_t     len; /* -1 = variable */
89     const char *name;
90 } mig_cmd_args[] = {
91     [MIG_CMD_INVALID]          = { .len = -1, .name = "INVALID" },
92     [MIG_CMD_OPEN_RETURN_PATH] = { .len =  0, .name = "OPEN_RETURN_PATH" },
93     [MIG_CMD_PING]             = { .len = sizeof(uint32_t), .name = "PING" },
94     [MIG_CMD_POSTCOPY_ADVISE]  = { .len = 16, .name = "POSTCOPY_ADVISE" },
95     [MIG_CMD_POSTCOPY_LISTEN]  = { .len =  0, .name = "POSTCOPY_LISTEN" },
96     [MIG_CMD_POSTCOPY_RUN]     = { .len =  0, .name = "POSTCOPY_RUN" },
97     [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
98                                    .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
99     [MIG_CMD_PACKAGED]         = { .len =  4, .name = "PACKAGED" },
100     [MIG_CMD_MAX]              = { .len = -1, .name = "MAX" },
101 };
102 
103 static int announce_self_create(uint8_t *buf,
104                                 uint8_t *mac_addr)
105 {
106     /* Ethernet header. */
107     memset(buf, 0xff, 6);         /* destination MAC addr */
108     memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
109     *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
110 
111     /* RARP header. */
112     *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
113     *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
114     *(buf + 18) = 6; /* hardware addr length (ethernet) */
115     *(buf + 19) = 4; /* protocol addr length (IPv4) */
116     *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
117     memcpy(buf + 22, mac_addr, 6); /* source hw addr */
118     memset(buf + 28, 0x00, 4);     /* source protocol addr */
119     memcpy(buf + 32, mac_addr, 6); /* target hw addr */
120     memset(buf + 38, 0x00, 4);     /* target protocol addr */
121 
122     /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
123     memset(buf + 42, 0x00, 18);
124 
125     return 60; /* len (FCS will be added by hardware) */
126 }
127 
128 static void qemu_announce_self_iter(NICState *nic, void *opaque)
129 {
130     uint8_t buf[60];
131     int len;
132 
133     trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
134     len = announce_self_create(buf, nic->conf->macaddr.a);
135 
136     qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
137 }
138 
139 
140 static void qemu_announce_self_once(void *opaque)
141 {
142     static int count = SELF_ANNOUNCE_ROUNDS;
143     QEMUTimer *timer = *(QEMUTimer **)opaque;
144 
145     qemu_foreach_nic(qemu_announce_self_iter, NULL);
146 
147     if (--count) {
148         /* delay 50ms, 150ms, 250ms, ... */
149         timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
150                   self_announce_delay(count));
151     } else {
152             timer_del(timer);
153             timer_free(timer);
154     }
155 }
156 
157 void qemu_announce_self(void)
158 {
159     static QEMUTimer *timer;
160     timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
161     qemu_announce_self_once(&timer);
162 }
163 
164 /***********************************************************/
165 /* savevm/loadvm support */
166 
167 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
168                                    int64_t pos)
169 {
170     int ret;
171     QEMUIOVector qiov;
172 
173     qemu_iovec_init_external(&qiov, iov, iovcnt);
174     ret = bdrv_writev_vmstate(opaque, &qiov, pos);
175     if (ret < 0) {
176         return ret;
177     }
178 
179     return qiov.size;
180 }
181 
182 static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
183                                 size_t size)
184 {
185     return bdrv_load_vmstate(opaque, buf, pos, size);
186 }
187 
188 static int bdrv_fclose(void *opaque)
189 {
190     return bdrv_flush(opaque);
191 }
192 
193 static const QEMUFileOps bdrv_read_ops = {
194     .get_buffer = block_get_buffer,
195     .close =      bdrv_fclose
196 };
197 
198 static const QEMUFileOps bdrv_write_ops = {
199     .writev_buffer  = block_writev_buffer,
200     .close          = bdrv_fclose
201 };
202 
203 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
204 {
205     if (is_writable) {
206         return qemu_fopen_ops(bs, &bdrv_write_ops);
207     }
208     return qemu_fopen_ops(bs, &bdrv_read_ops);
209 }
210 
211 
212 /* QEMUFile timer support.
213  * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
214  */
215 
216 void timer_put(QEMUFile *f, QEMUTimer *ts)
217 {
218     uint64_t expire_time;
219 
220     expire_time = timer_expire_time_ns(ts);
221     qemu_put_be64(f, expire_time);
222 }
223 
224 void timer_get(QEMUFile *f, QEMUTimer *ts)
225 {
226     uint64_t expire_time;
227 
228     expire_time = qemu_get_be64(f);
229     if (expire_time != -1) {
230         timer_mod_ns(ts, expire_time);
231     } else {
232         timer_del(ts);
233     }
234 }
235 
236 
237 /* VMState timer support.
238  * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
239  */
240 
241 static int get_timer(QEMUFile *f, void *pv, size_t size, VMStateField *field)
242 {
243     QEMUTimer *v = pv;
244     timer_get(f, v);
245     return 0;
246 }
247 
248 static int put_timer(QEMUFile *f, void *pv, size_t size, VMStateField *field,
249                      QJSON *vmdesc)
250 {
251     QEMUTimer *v = pv;
252     timer_put(f, v);
253 
254     return 0;
255 }
256 
257 const VMStateInfo vmstate_info_timer = {
258     .name = "timer",
259     .get  = get_timer,
260     .put  = put_timer,
261 };
262 
263 
264 typedef struct CompatEntry {
265     char idstr[256];
266     int instance_id;
267 } CompatEntry;
268 
269 typedef struct SaveStateEntry {
270     QTAILQ_ENTRY(SaveStateEntry) entry;
271     char idstr[256];
272     int instance_id;
273     int alias_id;
274     int version_id;
275     /* version id read from the stream */
276     int load_version_id;
277     int section_id;
278     /* section id read from the stream */
279     int load_section_id;
280     SaveVMHandlers *ops;
281     const VMStateDescription *vmsd;
282     void *opaque;
283     CompatEntry *compat;
284     int is_ram;
285 } SaveStateEntry;
286 
287 typedef struct SaveState {
288     QTAILQ_HEAD(, SaveStateEntry) handlers;
289     int global_section_id;
290     bool skip_configuration;
291     uint32_t len;
292     const char *name;
293     uint32_t target_page_bits;
294 } SaveState;
295 
296 static SaveState savevm_state = {
297     .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
298     .global_section_id = 0,
299     .skip_configuration = false,
300 };
301 
302 void savevm_skip_configuration(void)
303 {
304     savevm_state.skip_configuration = true;
305 }
306 
307 
308 static void configuration_pre_save(void *opaque)
309 {
310     SaveState *state = opaque;
311     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
312 
313     state->len = strlen(current_name);
314     state->name = current_name;
315     state->target_page_bits = qemu_target_page_bits();
316 }
317 
318 static int configuration_pre_load(void *opaque)
319 {
320     SaveState *state = opaque;
321 
322     /* If there is no target-page-bits subsection it means the source
323      * predates the variable-target-page-bits support and is using the
324      * minimum possible value for this CPU.
325      */
326     state->target_page_bits = qemu_target_page_bits_min();
327     return 0;
328 }
329 
330 static int configuration_post_load(void *opaque, int version_id)
331 {
332     SaveState *state = opaque;
333     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
334 
335     if (strncmp(state->name, current_name, state->len) != 0) {
336         error_report("Machine type received is '%.*s' and local is '%s'",
337                      (int) state->len, state->name, current_name);
338         return -EINVAL;
339     }
340 
341     if (state->target_page_bits != qemu_target_page_bits()) {
342         error_report("Received TARGET_PAGE_BITS is %d but local is %d",
343                      state->target_page_bits, qemu_target_page_bits());
344         return -EINVAL;
345     }
346 
347     return 0;
348 }
349 
350 /* The target-page-bits subsection is present only if the
351  * target page size is not the same as the default (ie the
352  * minimum page size for a variable-page-size guest CPU).
353  * If it is present then it contains the actual target page
354  * bits for the machine, and migration will fail if the
355  * two ends don't agree about it.
356  */
357 static bool vmstate_target_page_bits_needed(void *opaque)
358 {
359     return qemu_target_page_bits()
360         > qemu_target_page_bits_min();
361 }
362 
363 static const VMStateDescription vmstate_target_page_bits = {
364     .name = "configuration/target-page-bits",
365     .version_id = 1,
366     .minimum_version_id = 1,
367     .needed = vmstate_target_page_bits_needed,
368     .fields = (VMStateField[]) {
369         VMSTATE_UINT32(target_page_bits, SaveState),
370         VMSTATE_END_OF_LIST()
371     }
372 };
373 
374 static const VMStateDescription vmstate_configuration = {
375     .name = "configuration",
376     .version_id = 1,
377     .pre_load = configuration_pre_load,
378     .post_load = configuration_post_load,
379     .pre_save = configuration_pre_save,
380     .fields = (VMStateField[]) {
381         VMSTATE_UINT32(len, SaveState),
382         VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
383         VMSTATE_END_OF_LIST()
384     },
385     .subsections = (const VMStateDescription*[]) {
386         &vmstate_target_page_bits,
387         NULL
388     }
389 };
390 
391 static void dump_vmstate_vmsd(FILE *out_file,
392                               const VMStateDescription *vmsd, int indent,
393                               bool is_subsection);
394 
395 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
396                               int indent)
397 {
398     fprintf(out_file, "%*s{\n", indent, "");
399     indent += 2;
400     fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
401     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
402             field->version_id);
403     fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
404             field->field_exists ? "true" : "false");
405     fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
406     if (field->vmsd != NULL) {
407         fprintf(out_file, ",\n");
408         dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
409     }
410     fprintf(out_file, "\n%*s}", indent - 2, "");
411 }
412 
413 static void dump_vmstate_vmss(FILE *out_file,
414                               const VMStateDescription **subsection,
415                               int indent)
416 {
417     if (*subsection != NULL) {
418         dump_vmstate_vmsd(out_file, *subsection, indent, true);
419     }
420 }
421 
422 static void dump_vmstate_vmsd(FILE *out_file,
423                               const VMStateDescription *vmsd, int indent,
424                               bool is_subsection)
425 {
426     if (is_subsection) {
427         fprintf(out_file, "%*s{\n", indent, "");
428     } else {
429         fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
430     }
431     indent += 2;
432     fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
433     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
434             vmsd->version_id);
435     fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
436             vmsd->minimum_version_id);
437     if (vmsd->fields != NULL) {
438         const VMStateField *field = vmsd->fields;
439         bool first;
440 
441         fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
442         first = true;
443         while (field->name != NULL) {
444             if (field->flags & VMS_MUST_EXIST) {
445                 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
446                 field++;
447                 continue;
448             }
449             if (!first) {
450                 fprintf(out_file, ",\n");
451             }
452             dump_vmstate_vmsf(out_file, field, indent + 2);
453             field++;
454             first = false;
455         }
456         fprintf(out_file, "\n%*s]", indent, "");
457     }
458     if (vmsd->subsections != NULL) {
459         const VMStateDescription **subsection = vmsd->subsections;
460         bool first;
461 
462         fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
463         first = true;
464         while (*subsection != NULL) {
465             if (!first) {
466                 fprintf(out_file, ",\n");
467             }
468             dump_vmstate_vmss(out_file, subsection, indent + 2);
469             subsection++;
470             first = false;
471         }
472         fprintf(out_file, "\n%*s]", indent, "");
473     }
474     fprintf(out_file, "\n%*s}", indent - 2, "");
475 }
476 
477 static void dump_machine_type(FILE *out_file)
478 {
479     MachineClass *mc;
480 
481     mc = MACHINE_GET_CLASS(current_machine);
482 
483     fprintf(out_file, "  \"vmschkmachine\": {\n");
484     fprintf(out_file, "    \"Name\": \"%s\"\n", mc->name);
485     fprintf(out_file, "  },\n");
486 }
487 
488 void dump_vmstate_json_to_file(FILE *out_file)
489 {
490     GSList *list, *elt;
491     bool first;
492 
493     fprintf(out_file, "{\n");
494     dump_machine_type(out_file);
495 
496     first = true;
497     list = object_class_get_list(TYPE_DEVICE, true);
498     for (elt = list; elt; elt = elt->next) {
499         DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
500                                              TYPE_DEVICE);
501         const char *name;
502         int indent = 2;
503 
504         if (!dc->vmsd) {
505             continue;
506         }
507 
508         if (!first) {
509             fprintf(out_file, ",\n");
510         }
511         name = object_class_get_name(OBJECT_CLASS(dc));
512         fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
513         indent += 2;
514         fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
515         fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
516                 dc->vmsd->version_id);
517         fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
518                 dc->vmsd->minimum_version_id);
519 
520         dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
521 
522         fprintf(out_file, "\n%*s}", indent - 2, "");
523         first = false;
524     }
525     fprintf(out_file, "\n}\n");
526     fclose(out_file);
527 }
528 
529 static int calculate_new_instance_id(const char *idstr)
530 {
531     SaveStateEntry *se;
532     int instance_id = 0;
533 
534     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
535         if (strcmp(idstr, se->idstr) == 0
536             && instance_id <= se->instance_id) {
537             instance_id = se->instance_id + 1;
538         }
539     }
540     return instance_id;
541 }
542 
543 static int calculate_compat_instance_id(const char *idstr)
544 {
545     SaveStateEntry *se;
546     int instance_id = 0;
547 
548     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
549         if (!se->compat) {
550             continue;
551         }
552 
553         if (strcmp(idstr, se->compat->idstr) == 0
554             && instance_id <= se->compat->instance_id) {
555             instance_id = se->compat->instance_id + 1;
556         }
557     }
558     return instance_id;
559 }
560 
561 static inline MigrationPriority save_state_priority(SaveStateEntry *se)
562 {
563     if (se->vmsd) {
564         return se->vmsd->priority;
565     }
566     return MIG_PRI_DEFAULT;
567 }
568 
569 static void savevm_state_handler_insert(SaveStateEntry *nse)
570 {
571     MigrationPriority priority = save_state_priority(nse);
572     SaveStateEntry *se;
573 
574     assert(priority <= MIG_PRI_MAX);
575 
576     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
577         if (save_state_priority(se) < priority) {
578             break;
579         }
580     }
581 
582     if (se) {
583         QTAILQ_INSERT_BEFORE(se, nse, entry);
584     } else {
585         QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
586     }
587 }
588 
589 /* TODO: Individual devices generally have very little idea about the rest
590    of the system, so instance_id should be removed/replaced.
591    Meanwhile pass -1 as instance_id if you do not already have a clearly
592    distinguishing id for all instances of your device class. */
593 int register_savevm_live(DeviceState *dev,
594                          const char *idstr,
595                          int instance_id,
596                          int version_id,
597                          SaveVMHandlers *ops,
598                          void *opaque)
599 {
600     SaveStateEntry *se;
601 
602     se = g_new0(SaveStateEntry, 1);
603     se->version_id = version_id;
604     se->section_id = savevm_state.global_section_id++;
605     se->ops = ops;
606     se->opaque = opaque;
607     se->vmsd = NULL;
608     /* if this is a live_savem then set is_ram */
609     if (ops->save_live_setup != NULL) {
610         se->is_ram = 1;
611     }
612 
613     if (dev) {
614         char *id = qdev_get_dev_path(dev);
615         if (id) {
616             if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
617                 sizeof(se->idstr)) {
618                 error_report("Path too long for VMState (%s)", id);
619                 g_free(id);
620                 g_free(se);
621 
622                 return -1;
623             }
624             g_free(id);
625 
626             se->compat = g_new0(CompatEntry, 1);
627             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
628             se->compat->instance_id = instance_id == -1 ?
629                          calculate_compat_instance_id(idstr) : instance_id;
630             instance_id = -1;
631         }
632     }
633     pstrcat(se->idstr, sizeof(se->idstr), idstr);
634 
635     if (instance_id == -1) {
636         se->instance_id = calculate_new_instance_id(se->idstr);
637     } else {
638         se->instance_id = instance_id;
639     }
640     assert(!se->compat || se->instance_id == 0);
641     savevm_state_handler_insert(se);
642     return 0;
643 }
644 
645 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
646 {
647     SaveStateEntry *se, *new_se;
648     char id[256] = "";
649 
650     if (dev) {
651         char *path = qdev_get_dev_path(dev);
652         if (path) {
653             pstrcpy(id, sizeof(id), path);
654             pstrcat(id, sizeof(id), "/");
655             g_free(path);
656         }
657     }
658     pstrcat(id, sizeof(id), idstr);
659 
660     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
661         if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
662             QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
663             g_free(se->compat);
664             g_free(se);
665         }
666     }
667 }
668 
669 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
670                                    const VMStateDescription *vmsd,
671                                    void *opaque, int alias_id,
672                                    int required_for_version,
673                                    Error **errp)
674 {
675     SaveStateEntry *se;
676 
677     /* If this triggers, alias support can be dropped for the vmsd. */
678     assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
679 
680     se = g_new0(SaveStateEntry, 1);
681     se->version_id = vmsd->version_id;
682     se->section_id = savevm_state.global_section_id++;
683     se->opaque = opaque;
684     se->vmsd = vmsd;
685     se->alias_id = alias_id;
686 
687     if (dev) {
688         char *id = qdev_get_dev_path(dev);
689         if (id) {
690             if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
691                 sizeof(se->idstr)) {
692                 error_setg(errp, "Path too long for VMState (%s)", id);
693                 g_free(id);
694                 g_free(se);
695 
696                 return -1;
697             }
698             g_free(id);
699 
700             se->compat = g_new0(CompatEntry, 1);
701             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
702             se->compat->instance_id = instance_id == -1 ?
703                          calculate_compat_instance_id(vmsd->name) : instance_id;
704             instance_id = -1;
705         }
706     }
707     pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
708 
709     if (instance_id == -1) {
710         se->instance_id = calculate_new_instance_id(se->idstr);
711     } else {
712         se->instance_id = instance_id;
713     }
714     assert(!se->compat || se->instance_id == 0);
715     savevm_state_handler_insert(se);
716     return 0;
717 }
718 
719 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
720                         void *opaque)
721 {
722     SaveStateEntry *se, *new_se;
723 
724     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
725         if (se->vmsd == vmsd && se->opaque == opaque) {
726             QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
727             g_free(se->compat);
728             g_free(se);
729         }
730     }
731 }
732 
733 static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
734 {
735     trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
736     if (!se->vmsd) {         /* Old style */
737         return se->ops->load_state(f, se->opaque, se->load_version_id);
738     }
739     return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id);
740 }
741 
742 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
743 {
744     int64_t old_offset, size;
745 
746     old_offset = qemu_ftell_fast(f);
747     se->ops->save_state(f, se->opaque);
748     size = qemu_ftell_fast(f) - old_offset;
749 
750     if (vmdesc) {
751         json_prop_int(vmdesc, "size", size);
752         json_start_array(vmdesc, "fields");
753         json_start_object(vmdesc, NULL);
754         json_prop_str(vmdesc, "name", "data");
755         json_prop_int(vmdesc, "size", size);
756         json_prop_str(vmdesc, "type", "buffer");
757         json_end_object(vmdesc);
758         json_end_array(vmdesc);
759     }
760 }
761 
762 static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
763 {
764     trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
765     if (!se->vmsd) {
766         vmstate_save_old_style(f, se, vmdesc);
767         return;
768     }
769     vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
770 }
771 
772 void savevm_skip_section_footers(void)
773 {
774     skip_section_footers = true;
775 }
776 
777 /*
778  * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
779  */
780 static void save_section_header(QEMUFile *f, SaveStateEntry *se,
781                                 uint8_t section_type)
782 {
783     qemu_put_byte(f, section_type);
784     qemu_put_be32(f, se->section_id);
785 
786     if (section_type == QEMU_VM_SECTION_FULL ||
787         section_type == QEMU_VM_SECTION_START) {
788         /* ID string */
789         size_t len = strlen(se->idstr);
790         qemu_put_byte(f, len);
791         qemu_put_buffer(f, (uint8_t *)se->idstr, len);
792 
793         qemu_put_be32(f, se->instance_id);
794         qemu_put_be32(f, se->version_id);
795     }
796 }
797 
798 /*
799  * Write a footer onto device sections that catches cases misformatted device
800  * sections.
801  */
802 static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
803 {
804     if (!skip_section_footers) {
805         qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
806         qemu_put_be32(f, se->section_id);
807     }
808 }
809 
810 /**
811  * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
812  *                           command and associated data.
813  *
814  * @f: File to send command on
815  * @command: Command type to send
816  * @len: Length of associated data
817  * @data: Data associated with command.
818  */
819 static void qemu_savevm_command_send(QEMUFile *f,
820                                      enum qemu_vm_cmd command,
821                                      uint16_t len,
822                                      uint8_t *data)
823 {
824     trace_savevm_command_send(command, len);
825     qemu_put_byte(f, QEMU_VM_COMMAND);
826     qemu_put_be16(f, (uint16_t)command);
827     qemu_put_be16(f, len);
828     qemu_put_buffer(f, data, len);
829     qemu_fflush(f);
830 }
831 
832 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
833 {
834     uint32_t buf;
835 
836     trace_savevm_send_ping(value);
837     buf = cpu_to_be32(value);
838     qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
839 }
840 
841 void qemu_savevm_send_open_return_path(QEMUFile *f)
842 {
843     trace_savevm_send_open_return_path();
844     qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
845 }
846 
847 /* We have a buffer of data to send; we don't want that all to be loaded
848  * by the command itself, so the command contains just the length of the
849  * extra buffer that we then send straight after it.
850  * TODO: Must be a better way to organise that
851  *
852  * Returns:
853  *    0 on success
854  *    -ve on error
855  */
856 int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
857 {
858     uint32_t tmp;
859 
860     if (len > MAX_VM_CMD_PACKAGED_SIZE) {
861         error_report("%s: Unreasonably large packaged state: %zu",
862                      __func__, len);
863         return -1;
864     }
865 
866     tmp = cpu_to_be32(len);
867 
868     trace_qemu_savevm_send_packaged();
869     qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
870 
871     qemu_put_buffer(f, buf, len);
872 
873     return 0;
874 }
875 
876 /* Send prior to any postcopy transfer */
877 void qemu_savevm_send_postcopy_advise(QEMUFile *f)
878 {
879     uint64_t tmp[2];
880     tmp[0] = cpu_to_be64(ram_pagesize_summary());
881     tmp[1] = cpu_to_be64(qemu_target_page_size());
882 
883     trace_qemu_savevm_send_postcopy_advise();
884     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 16, (uint8_t *)tmp);
885 }
886 
887 /* Sent prior to starting the destination running in postcopy, discard pages
888  * that have already been sent but redirtied on the source.
889  * CMD_POSTCOPY_RAM_DISCARD consist of:
890  *      byte   version (0)
891  *      byte   Length of name field (not including 0)
892  *  n x byte   RAM block name
893  *      byte   0 terminator (just for safety)
894  *  n x        Byte ranges within the named RAMBlock
895  *      be64   Start of the range
896  *      be64   Length
897  *
898  *  name:  RAMBlock name that these entries are part of
899  *  len: Number of page entries
900  *  start_list: 'len' addresses
901  *  length_list: 'len' addresses
902  *
903  */
904 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
905                                            uint16_t len,
906                                            uint64_t *start_list,
907                                            uint64_t *length_list)
908 {
909     uint8_t *buf;
910     uint16_t tmplen;
911     uint16_t t;
912     size_t name_len = strlen(name);
913 
914     trace_qemu_savevm_send_postcopy_ram_discard(name, len);
915     assert(name_len < 256);
916     buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
917     buf[0] = postcopy_ram_discard_version;
918     buf[1] = name_len;
919     memcpy(buf + 2, name, name_len);
920     tmplen = 2 + name_len;
921     buf[tmplen++] = '\0';
922 
923     for (t = 0; t < len; t++) {
924         stq_be_p(buf + tmplen, start_list[t]);
925         tmplen += 8;
926         stq_be_p(buf + tmplen, length_list[t]);
927         tmplen += 8;
928     }
929     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
930     g_free(buf);
931 }
932 
933 /* Get the destination into a state where it can receive postcopy data. */
934 void qemu_savevm_send_postcopy_listen(QEMUFile *f)
935 {
936     trace_savevm_send_postcopy_listen();
937     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
938 }
939 
940 /* Kick the destination into running */
941 void qemu_savevm_send_postcopy_run(QEMUFile *f)
942 {
943     trace_savevm_send_postcopy_run();
944     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
945 }
946 
947 bool qemu_savevm_state_blocked(Error **errp)
948 {
949     SaveStateEntry *se;
950 
951     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
952         if (se->vmsd && se->vmsd->unmigratable) {
953             error_setg(errp, "State blocked by non-migratable device '%s'",
954                        se->idstr);
955             return true;
956         }
957     }
958     return false;
959 }
960 
961 static bool enforce_config_section(void)
962 {
963     MachineState *machine = MACHINE(qdev_get_machine());
964     return machine->enforce_config_section;
965 }
966 
967 void qemu_savevm_state_header(QEMUFile *f)
968 {
969     trace_savevm_state_header();
970     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
971     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
972 
973     if (!savevm_state.skip_configuration || enforce_config_section()) {
974         qemu_put_byte(f, QEMU_VM_CONFIGURATION);
975         vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
976     }
977 
978 }
979 
980 void qemu_savevm_state_begin(QEMUFile *f)
981 {
982     SaveStateEntry *se;
983     int ret;
984 
985     trace_savevm_state_begin();
986     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
987         if (!se->ops || !se->ops->save_live_setup) {
988             continue;
989         }
990         if (se->ops && se->ops->is_active) {
991             if (!se->ops->is_active(se->opaque)) {
992                 continue;
993             }
994         }
995         save_section_header(f, se, QEMU_VM_SECTION_START);
996 
997         ret = se->ops->save_live_setup(f, se->opaque);
998         save_section_footer(f, se);
999         if (ret < 0) {
1000             qemu_file_set_error(f, ret);
1001             break;
1002         }
1003     }
1004 }
1005 
1006 /*
1007  * this function has three return values:
1008  *   negative: there was one error, and we have -errno.
1009  *   0 : We haven't finished, caller have to go again
1010  *   1 : We have finished, we can go to complete phase
1011  */
1012 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
1013 {
1014     SaveStateEntry *se;
1015     int ret = 1;
1016 
1017     trace_savevm_state_iterate();
1018     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1019         if (!se->ops || !se->ops->save_live_iterate) {
1020             continue;
1021         }
1022         if (se->ops && se->ops->is_active) {
1023             if (!se->ops->is_active(se->opaque)) {
1024                 continue;
1025             }
1026         }
1027         /*
1028          * In the postcopy phase, any device that doesn't know how to
1029          * do postcopy should have saved it's state in the _complete
1030          * call that's already run, it might get confused if we call
1031          * iterate afterwards.
1032          */
1033         if (postcopy && !se->ops->save_live_complete_postcopy) {
1034             continue;
1035         }
1036         if (qemu_file_rate_limit(f)) {
1037             return 0;
1038         }
1039         trace_savevm_section_start(se->idstr, se->section_id);
1040 
1041         save_section_header(f, se, QEMU_VM_SECTION_PART);
1042 
1043         ret = se->ops->save_live_iterate(f, se->opaque);
1044         trace_savevm_section_end(se->idstr, se->section_id, ret);
1045         save_section_footer(f, se);
1046 
1047         if (ret < 0) {
1048             qemu_file_set_error(f, ret);
1049         }
1050         if (ret <= 0) {
1051             /* Do not proceed to the next vmstate before this one reported
1052                completion of the current stage. This serializes the migration
1053                and reduces the probability that a faster changing state is
1054                synchronized over and over again. */
1055             break;
1056         }
1057     }
1058     return ret;
1059 }
1060 
1061 static bool should_send_vmdesc(void)
1062 {
1063     MachineState *machine = MACHINE(qdev_get_machine());
1064     bool in_postcopy = migration_in_postcopy();
1065     return !machine->suppress_vmdesc && !in_postcopy;
1066 }
1067 
1068 /*
1069  * Calls the save_live_complete_postcopy methods
1070  * causing the last few pages to be sent immediately and doing any associated
1071  * cleanup.
1072  * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1073  * all the other devices, but that happens at the point we switch to postcopy.
1074  */
1075 void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1076 {
1077     SaveStateEntry *se;
1078     int ret;
1079 
1080     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1081         if (!se->ops || !se->ops->save_live_complete_postcopy) {
1082             continue;
1083         }
1084         if (se->ops && se->ops->is_active) {
1085             if (!se->ops->is_active(se->opaque)) {
1086                 continue;
1087             }
1088         }
1089         trace_savevm_section_start(se->idstr, se->section_id);
1090         /* Section type */
1091         qemu_put_byte(f, QEMU_VM_SECTION_END);
1092         qemu_put_be32(f, se->section_id);
1093 
1094         ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1095         trace_savevm_section_end(se->idstr, se->section_id, ret);
1096         save_section_footer(f, se);
1097         if (ret < 0) {
1098             qemu_file_set_error(f, ret);
1099             return;
1100         }
1101     }
1102 
1103     qemu_put_byte(f, QEMU_VM_EOF);
1104     qemu_fflush(f);
1105 }
1106 
1107 void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
1108 {
1109     QJSON *vmdesc;
1110     int vmdesc_len;
1111     SaveStateEntry *se;
1112     int ret;
1113     bool in_postcopy = migration_in_postcopy();
1114 
1115     trace_savevm_state_complete_precopy();
1116 
1117     cpu_synchronize_all_states();
1118 
1119     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1120         if (!se->ops ||
1121             (in_postcopy && se->ops->save_live_complete_postcopy) ||
1122             (in_postcopy && !iterable_only) ||
1123             !se->ops->save_live_complete_precopy) {
1124             continue;
1125         }
1126 
1127         if (se->ops && se->ops->is_active) {
1128             if (!se->ops->is_active(se->opaque)) {
1129                 continue;
1130             }
1131         }
1132         trace_savevm_section_start(se->idstr, se->section_id);
1133 
1134         save_section_header(f, se, QEMU_VM_SECTION_END);
1135 
1136         ret = se->ops->save_live_complete_precopy(f, se->opaque);
1137         trace_savevm_section_end(se->idstr, se->section_id, ret);
1138         save_section_footer(f, se);
1139         if (ret < 0) {
1140             qemu_file_set_error(f, ret);
1141             return;
1142         }
1143     }
1144 
1145     if (iterable_only) {
1146         return;
1147     }
1148 
1149     vmdesc = qjson_new();
1150     json_prop_int(vmdesc, "page_size", qemu_target_page_size());
1151     json_start_array(vmdesc, "devices");
1152     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1153 
1154         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1155             continue;
1156         }
1157         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1158             trace_savevm_section_skip(se->idstr, se->section_id);
1159             continue;
1160         }
1161 
1162         trace_savevm_section_start(se->idstr, se->section_id);
1163 
1164         json_start_object(vmdesc, NULL);
1165         json_prop_str(vmdesc, "name", se->idstr);
1166         json_prop_int(vmdesc, "instance_id", se->instance_id);
1167 
1168         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1169         vmstate_save(f, se, vmdesc);
1170         trace_savevm_section_end(se->idstr, se->section_id, 0);
1171         save_section_footer(f, se);
1172 
1173         json_end_object(vmdesc);
1174     }
1175 
1176     if (!in_postcopy) {
1177         /* Postcopy stream will still be going */
1178         qemu_put_byte(f, QEMU_VM_EOF);
1179     }
1180 
1181     json_end_array(vmdesc);
1182     qjson_finish(vmdesc);
1183     vmdesc_len = strlen(qjson_get_str(vmdesc));
1184 
1185     if (should_send_vmdesc()) {
1186         qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1187         qemu_put_be32(f, vmdesc_len);
1188         qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
1189     }
1190     qjson_destroy(vmdesc);
1191 
1192     qemu_fflush(f);
1193 }
1194 
1195 /* Give an estimate of the amount left to be transferred,
1196  * the result is split into the amount for units that can and
1197  * for units that can't do postcopy.
1198  */
1199 void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,
1200                                uint64_t *res_non_postcopiable,
1201                                uint64_t *res_postcopiable)
1202 {
1203     SaveStateEntry *se;
1204 
1205     *res_non_postcopiable = 0;
1206     *res_postcopiable = 0;
1207 
1208 
1209     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1210         if (!se->ops || !se->ops->save_live_pending) {
1211             continue;
1212         }
1213         if (se->ops && se->ops->is_active) {
1214             if (!se->ops->is_active(se->opaque)) {
1215                 continue;
1216             }
1217         }
1218         se->ops->save_live_pending(f, se->opaque, threshold_size,
1219                                    res_non_postcopiable, res_postcopiable);
1220     }
1221 }
1222 
1223 void qemu_savevm_state_cleanup(void)
1224 {
1225     SaveStateEntry *se;
1226 
1227     trace_savevm_state_cleanup();
1228     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1229         if (se->ops && se->ops->cleanup) {
1230             se->ops->cleanup(se->opaque);
1231         }
1232     }
1233 }
1234 
1235 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1236 {
1237     int ret;
1238     MigrationState *ms = migrate_init();
1239     MigrationStatus status;
1240     ms->to_dst_file = f;
1241 
1242     if (migration_is_blocked(errp)) {
1243         ret = -EINVAL;
1244         goto done;
1245     }
1246 
1247     if (migrate_use_block()) {
1248         error_setg(errp, "Block migration and snapshots are incompatible");
1249         ret = -EINVAL;
1250         goto done;
1251     }
1252 
1253     qemu_mutex_unlock_iothread();
1254     qemu_savevm_state_header(f);
1255     qemu_savevm_state_begin(f);
1256     qemu_mutex_lock_iothread();
1257 
1258     while (qemu_file_get_error(f) == 0) {
1259         if (qemu_savevm_state_iterate(f, false) > 0) {
1260             break;
1261         }
1262     }
1263 
1264     ret = qemu_file_get_error(f);
1265     if (ret == 0) {
1266         qemu_savevm_state_complete_precopy(f, false);
1267         ret = qemu_file_get_error(f);
1268     }
1269     qemu_savevm_state_cleanup();
1270     if (ret != 0) {
1271         error_setg_errno(errp, -ret, "Error while writing VM state");
1272     }
1273 
1274 done:
1275     if (ret != 0) {
1276         status = MIGRATION_STATUS_FAILED;
1277     } else {
1278         status = MIGRATION_STATUS_COMPLETED;
1279     }
1280     migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
1281 
1282     /* f is outer parameter, it should not stay in global migration state after
1283      * this function finished */
1284     ms->to_dst_file = NULL;
1285 
1286     return ret;
1287 }
1288 
1289 static int qemu_save_device_state(QEMUFile *f)
1290 {
1291     SaveStateEntry *se;
1292 
1293     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1294     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1295 
1296     cpu_synchronize_all_states();
1297 
1298     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1299         if (se->is_ram) {
1300             continue;
1301         }
1302         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1303             continue;
1304         }
1305         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1306             continue;
1307         }
1308 
1309         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1310 
1311         vmstate_save(f, se, NULL);
1312 
1313         save_section_footer(f, se);
1314     }
1315 
1316     qemu_put_byte(f, QEMU_VM_EOF);
1317 
1318     return qemu_file_get_error(f);
1319 }
1320 
1321 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1322 {
1323     SaveStateEntry *se;
1324 
1325     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1326         if (!strcmp(se->idstr, idstr) &&
1327             (instance_id == se->instance_id ||
1328              instance_id == se->alias_id))
1329             return se;
1330         /* Migrating from an older version? */
1331         if (strstr(se->idstr, idstr) && se->compat) {
1332             if (!strcmp(se->compat->idstr, idstr) &&
1333                 (instance_id == se->compat->instance_id ||
1334                  instance_id == se->alias_id))
1335                 return se;
1336         }
1337     }
1338     return NULL;
1339 }
1340 
1341 enum LoadVMExitCodes {
1342     /* Allow a command to quit all layers of nested loadvm loops */
1343     LOADVM_QUIT     =  1,
1344 };
1345 
1346 static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis);
1347 
1348 /* ------ incoming postcopy messages ------ */
1349 /* 'advise' arrives before any transfers just to tell us that a postcopy
1350  * *might* happen - it might be skipped if precopy transferred everything
1351  * quickly.
1352  */
1353 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis)
1354 {
1355     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1356     uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
1357 
1358     trace_loadvm_postcopy_handle_advise();
1359     if (ps != POSTCOPY_INCOMING_NONE) {
1360         error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1361         return -1;
1362     }
1363 
1364     if (!postcopy_ram_supported_by_host()) {
1365         postcopy_state_set(POSTCOPY_INCOMING_NONE);
1366         return -1;
1367     }
1368 
1369     remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1370     local_pagesize_summary = ram_pagesize_summary();
1371 
1372     if (remote_pagesize_summary != local_pagesize_summary)  {
1373         /*
1374          * This detects two potential causes of mismatch:
1375          *   a) A mismatch in host page sizes
1376          *      Some combinations of mismatch are probably possible but it gets
1377          *      a bit more complicated.  In particular we need to place whole
1378          *      host pages on the dest at once, and we need to ensure that we
1379          *      handle dirtying to make sure we never end up sending part of
1380          *      a hostpage on it's own.
1381          *   b) The use of different huge page sizes on source/destination
1382          *      a more fine grain test is performed during RAM block migration
1383          *      but this test here causes a nice early clear failure, and
1384          *      also fails when passed to an older qemu that doesn't
1385          *      do huge pages.
1386          */
1387         error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1388                                                              " d=%" PRIx64 ")",
1389                      remote_pagesize_summary, local_pagesize_summary);
1390         return -1;
1391     }
1392 
1393     remote_tps = qemu_get_be64(mis->from_src_file);
1394     if (remote_tps != qemu_target_page_size()) {
1395         /*
1396          * Again, some differences could be dealt with, but for now keep it
1397          * simple.
1398          */
1399         error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1400                      (int)remote_tps, qemu_target_page_size());
1401         return -1;
1402     }
1403 
1404     if (ram_postcopy_incoming_init(mis)) {
1405         return -1;
1406     }
1407 
1408     postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1409 
1410     return 0;
1411 }
1412 
1413 /* After postcopy we will be told to throw some pages away since they're
1414  * dirty and will have to be demand fetched.  Must happen before CPU is
1415  * started.
1416  * There can be 0..many of these messages, each encoding multiple pages.
1417  */
1418 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1419                                               uint16_t len)
1420 {
1421     int tmp;
1422     char ramid[256];
1423     PostcopyState ps = postcopy_state_get();
1424 
1425     trace_loadvm_postcopy_ram_handle_discard();
1426 
1427     switch (ps) {
1428     case POSTCOPY_INCOMING_ADVISE:
1429         /* 1st discard */
1430         tmp = postcopy_ram_prepare_discard(mis);
1431         if (tmp) {
1432             return tmp;
1433         }
1434         break;
1435 
1436     case POSTCOPY_INCOMING_DISCARD:
1437         /* Expected state */
1438         break;
1439 
1440     default:
1441         error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1442                      ps);
1443         return -1;
1444     }
1445     /* We're expecting a
1446      *    Version (0)
1447      *    a RAM ID string (length byte, name, 0 term)
1448      *    then at least 1 16 byte chunk
1449     */
1450     if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1451         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1452         return -1;
1453     }
1454 
1455     tmp = qemu_get_byte(mis->from_src_file);
1456     if (tmp != postcopy_ram_discard_version) {
1457         error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1458         return -1;
1459     }
1460 
1461     if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1462         error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1463         return -1;
1464     }
1465     tmp = qemu_get_byte(mis->from_src_file);
1466     if (tmp != 0) {
1467         error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1468         return -1;
1469     }
1470 
1471     len -= 3 + strlen(ramid);
1472     if (len % 16) {
1473         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1474         return -1;
1475     }
1476     trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1477     while (len) {
1478         uint64_t start_addr, block_length;
1479         start_addr = qemu_get_be64(mis->from_src_file);
1480         block_length = qemu_get_be64(mis->from_src_file);
1481 
1482         len -= 16;
1483         int ret = ram_discard_range(ramid, start_addr, block_length);
1484         if (ret) {
1485             return ret;
1486         }
1487     }
1488     trace_loadvm_postcopy_ram_handle_discard_end();
1489 
1490     return 0;
1491 }
1492 
1493 /*
1494  * Triggered by a postcopy_listen command; this thread takes over reading
1495  * the input stream, leaving the main thread free to carry on loading the rest
1496  * of the device state (from RAM).
1497  * (TODO:This could do with being in a postcopy file - but there again it's
1498  * just another input loop, not that postcopy specific)
1499  */
1500 static void *postcopy_ram_listen_thread(void *opaque)
1501 {
1502     QEMUFile *f = opaque;
1503     MigrationIncomingState *mis = migration_incoming_get_current();
1504     int load_res;
1505 
1506     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1507                                    MIGRATION_STATUS_POSTCOPY_ACTIVE);
1508     qemu_sem_post(&mis->listen_thread_sem);
1509     trace_postcopy_ram_listen_thread_start();
1510 
1511     /*
1512      * Because we're a thread and not a coroutine we can't yield
1513      * in qemu_file, and thus we must be blocking now.
1514      */
1515     qemu_file_set_blocking(f, true);
1516     load_res = qemu_loadvm_state_main(f, mis);
1517     /* And non-blocking again so we don't block in any cleanup */
1518     qemu_file_set_blocking(f, false);
1519 
1520     trace_postcopy_ram_listen_thread_exit();
1521     if (load_res < 0) {
1522         error_report("%s: loadvm failed: %d", __func__, load_res);
1523         qemu_file_set_error(f, load_res);
1524         migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1525                                        MIGRATION_STATUS_FAILED);
1526     } else {
1527         /*
1528          * This looks good, but it's possible that the device loading in the
1529          * main thread hasn't finished yet, and so we might not be in 'RUN'
1530          * state yet; wait for the end of the main thread.
1531          */
1532         qemu_event_wait(&mis->main_thread_load_event);
1533     }
1534     postcopy_ram_incoming_cleanup(mis);
1535 
1536     if (load_res < 0) {
1537         /*
1538          * If something went wrong then we have a bad state so exit;
1539          * depending how far we got it might be possible at this point
1540          * to leave the guest running and fire MCEs for pages that never
1541          * arrived as a desperate recovery step.
1542          */
1543         exit(EXIT_FAILURE);
1544     }
1545 
1546     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1547                                    MIGRATION_STATUS_COMPLETED);
1548     /*
1549      * If everything has worked fine, then the main thread has waited
1550      * for us to start, and we're the last use of the mis.
1551      * (If something broke then qemu will have to exit anyway since it's
1552      * got a bad migration state).
1553      */
1554     migration_incoming_state_destroy();
1555 
1556 
1557     return NULL;
1558 }
1559 
1560 /* After this message we must be able to immediately receive postcopy data */
1561 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
1562 {
1563     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
1564     trace_loadvm_postcopy_handle_listen();
1565     if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
1566         error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
1567         return -1;
1568     }
1569     if (ps == POSTCOPY_INCOMING_ADVISE) {
1570         /*
1571          * A rare case, we entered listen without having to do any discards,
1572          * so do the setup that's normally done at the time of the 1st discard.
1573          */
1574         postcopy_ram_prepare_discard(mis);
1575     }
1576 
1577     /*
1578      * Sensitise RAM - can now generate requests for blocks that don't exist
1579      * However, at this point the CPU shouldn't be running, and the IO
1580      * shouldn't be doing anything yet so don't actually expect requests
1581      */
1582     if (postcopy_ram_enable_notify(mis)) {
1583         return -1;
1584     }
1585 
1586     if (mis->have_listen_thread) {
1587         error_report("CMD_POSTCOPY_RAM_LISTEN already has a listen thread");
1588         return -1;
1589     }
1590 
1591     mis->have_listen_thread = true;
1592     /* Start up the listening thread and wait for it to signal ready */
1593     qemu_sem_init(&mis->listen_thread_sem, 0);
1594     qemu_thread_create(&mis->listen_thread, "postcopy/listen",
1595                        postcopy_ram_listen_thread, mis->from_src_file,
1596                        QEMU_THREAD_DETACHED);
1597     qemu_sem_wait(&mis->listen_thread_sem);
1598     qemu_sem_destroy(&mis->listen_thread_sem);
1599 
1600     return 0;
1601 }
1602 
1603 
1604 typedef struct {
1605     QEMUBH *bh;
1606 } HandleRunBhData;
1607 
1608 static void loadvm_postcopy_handle_run_bh(void *opaque)
1609 {
1610     Error *local_err = NULL;
1611     HandleRunBhData *data = opaque;
1612 
1613     /* TODO we should move all of this lot into postcopy_ram.c or a shared code
1614      * in migration.c
1615      */
1616     cpu_synchronize_all_post_init();
1617 
1618     qemu_announce_self();
1619 
1620     /* Make sure all file formats flush their mutable metadata.
1621      * If we get an error here, just don't restart the VM yet. */
1622     bdrv_invalidate_cache_all(&local_err);
1623     if (local_err) {
1624         error_report_err(local_err);
1625         local_err = NULL;
1626         autostart = false;
1627     }
1628 
1629     trace_loadvm_postcopy_handle_run_cpu_sync();
1630     cpu_synchronize_all_post_init();
1631 
1632     trace_loadvm_postcopy_handle_run_vmstart();
1633 
1634     if (autostart) {
1635         /* Hold onto your hats, starting the CPU */
1636         vm_start();
1637     } else {
1638         /* leave it paused and let management decide when to start the CPU */
1639         runstate_set(RUN_STATE_PAUSED);
1640     }
1641 
1642     qemu_bh_delete(data->bh);
1643     g_free(data);
1644 }
1645 
1646 /* After all discards we can start running and asking for pages */
1647 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
1648 {
1649     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
1650     HandleRunBhData *data;
1651 
1652     trace_loadvm_postcopy_handle_run();
1653     if (ps != POSTCOPY_INCOMING_LISTENING) {
1654         error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
1655         return -1;
1656     }
1657 
1658     data = g_new(HandleRunBhData, 1);
1659     data->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, data);
1660     qemu_bh_schedule(data->bh);
1661 
1662     /* We need to finish reading the stream from the package
1663      * and also stop reading anything more from the stream that loaded the
1664      * package (since it's now being read by the listener thread).
1665      * LOADVM_QUIT will quit all the layers of nested loadvm loops.
1666      */
1667     return LOADVM_QUIT;
1668 }
1669 
1670 /**
1671  * Immediately following this command is a blob of data containing an embedded
1672  * chunk of migration stream; read it and load it.
1673  *
1674  * @mis: Incoming state
1675  * @length: Length of packaged data to read
1676  *
1677  * Returns: Negative values on error
1678  *
1679  */
1680 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
1681 {
1682     int ret;
1683     size_t length;
1684     QIOChannelBuffer *bioc;
1685 
1686     length = qemu_get_be32(mis->from_src_file);
1687     trace_loadvm_handle_cmd_packaged(length);
1688 
1689     if (length > MAX_VM_CMD_PACKAGED_SIZE) {
1690         error_report("Unreasonably large packaged state: %zu", length);
1691         return -1;
1692     }
1693 
1694     bioc = qio_channel_buffer_new(length);
1695     qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
1696     ret = qemu_get_buffer(mis->from_src_file,
1697                           bioc->data,
1698                           length);
1699     if (ret != length) {
1700         object_unref(OBJECT(bioc));
1701         error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
1702                      ret, length);
1703         return (ret < 0) ? ret : -EAGAIN;
1704     }
1705     bioc->usage += length;
1706     trace_loadvm_handle_cmd_packaged_received(ret);
1707 
1708     QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
1709 
1710     ret = qemu_loadvm_state_main(packf, mis);
1711     trace_loadvm_handle_cmd_packaged_main(ret);
1712     qemu_fclose(packf);
1713     object_unref(OBJECT(bioc));
1714 
1715     return ret;
1716 }
1717 
1718 /*
1719  * Process an incoming 'QEMU_VM_COMMAND'
1720  * 0           just a normal return
1721  * LOADVM_QUIT All good, but exit the loop
1722  * <0          Error
1723  */
1724 static int loadvm_process_command(QEMUFile *f)
1725 {
1726     MigrationIncomingState *mis = migration_incoming_get_current();
1727     uint16_t cmd;
1728     uint16_t len;
1729     uint32_t tmp32;
1730 
1731     cmd = qemu_get_be16(f);
1732     len = qemu_get_be16(f);
1733 
1734     trace_loadvm_process_command(cmd, len);
1735     if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
1736         error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
1737         return -EINVAL;
1738     }
1739 
1740     if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
1741         error_report("%s received with bad length - expecting %zu, got %d",
1742                      mig_cmd_args[cmd].name,
1743                      (size_t)mig_cmd_args[cmd].len, len);
1744         return -ERANGE;
1745     }
1746 
1747     switch (cmd) {
1748     case MIG_CMD_OPEN_RETURN_PATH:
1749         if (mis->to_src_file) {
1750             error_report("CMD_OPEN_RETURN_PATH called when RP already open");
1751             /* Not really a problem, so don't give up */
1752             return 0;
1753         }
1754         mis->to_src_file = qemu_file_get_return_path(f);
1755         if (!mis->to_src_file) {
1756             error_report("CMD_OPEN_RETURN_PATH failed");
1757             return -1;
1758         }
1759         break;
1760 
1761     case MIG_CMD_PING:
1762         tmp32 = qemu_get_be32(f);
1763         trace_loadvm_process_command_ping(tmp32);
1764         if (!mis->to_src_file) {
1765             error_report("CMD_PING (0x%x) received with no return path",
1766                          tmp32);
1767             return -1;
1768         }
1769         migrate_send_rp_pong(mis, tmp32);
1770         break;
1771 
1772     case MIG_CMD_PACKAGED:
1773         return loadvm_handle_cmd_packaged(mis);
1774 
1775     case MIG_CMD_POSTCOPY_ADVISE:
1776         return loadvm_postcopy_handle_advise(mis);
1777 
1778     case MIG_CMD_POSTCOPY_LISTEN:
1779         return loadvm_postcopy_handle_listen(mis);
1780 
1781     case MIG_CMD_POSTCOPY_RUN:
1782         return loadvm_postcopy_handle_run(mis);
1783 
1784     case MIG_CMD_POSTCOPY_RAM_DISCARD:
1785         return loadvm_postcopy_ram_handle_discard(mis, len);
1786     }
1787 
1788     return 0;
1789 }
1790 
1791 /*
1792  * Read a footer off the wire and check that it matches the expected section
1793  *
1794  * Returns: true if the footer was good
1795  *          false if there is a problem (and calls error_report to say why)
1796  */
1797 static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
1798 {
1799     uint8_t read_mark;
1800     uint32_t read_section_id;
1801 
1802     if (skip_section_footers) {
1803         /* No footer to check */
1804         return true;
1805     }
1806 
1807     read_mark = qemu_get_byte(f);
1808 
1809     if (read_mark != QEMU_VM_SECTION_FOOTER) {
1810         error_report("Missing section footer for %s", se->idstr);
1811         return false;
1812     }
1813 
1814     read_section_id = qemu_get_be32(f);
1815     if (read_section_id != se->load_section_id) {
1816         error_report("Mismatched section id in footer for %s -"
1817                      " read 0x%x expected 0x%x",
1818                      se->idstr, read_section_id, se->load_section_id);
1819         return false;
1820     }
1821 
1822     /* All good */
1823     return true;
1824 }
1825 
1826 static int
1827 qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis)
1828 {
1829     uint32_t instance_id, version_id, section_id;
1830     SaveStateEntry *se;
1831     char idstr[256];
1832     int ret;
1833 
1834     /* Read section start */
1835     section_id = qemu_get_be32(f);
1836     if (!qemu_get_counted_string(f, idstr)) {
1837         error_report("Unable to read ID string for section %u",
1838                      section_id);
1839         return -EINVAL;
1840     }
1841     instance_id = qemu_get_be32(f);
1842     version_id = qemu_get_be32(f);
1843 
1844     trace_qemu_loadvm_state_section_startfull(section_id, idstr,
1845             instance_id, version_id);
1846     /* Find savevm section */
1847     se = find_se(idstr, instance_id);
1848     if (se == NULL) {
1849         error_report("Unknown savevm section or instance '%s' %d",
1850                      idstr, instance_id);
1851         return -EINVAL;
1852     }
1853 
1854     /* Validate version */
1855     if (version_id > se->version_id) {
1856         error_report("savevm: unsupported version %d for '%s' v%d",
1857                      version_id, idstr, se->version_id);
1858         return -EINVAL;
1859     }
1860     se->load_version_id = version_id;
1861     se->load_section_id = section_id;
1862 
1863     /* Validate if it is a device's state */
1864     if (xen_enabled() && se->is_ram) {
1865         error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
1866         return -EINVAL;
1867     }
1868 
1869     ret = vmstate_load(f, se);
1870     if (ret < 0) {
1871         error_report("error while loading state for instance 0x%x of"
1872                      " device '%s'", instance_id, idstr);
1873         return ret;
1874     }
1875     if (!check_section_footer(f, se)) {
1876         return -EINVAL;
1877     }
1878 
1879     return 0;
1880 }
1881 
1882 static int
1883 qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis)
1884 {
1885     uint32_t section_id;
1886     SaveStateEntry *se;
1887     int ret;
1888 
1889     section_id = qemu_get_be32(f);
1890 
1891     trace_qemu_loadvm_state_section_partend(section_id);
1892     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1893         if (se->load_section_id == section_id) {
1894             break;
1895         }
1896     }
1897     if (se == NULL) {
1898         error_report("Unknown savevm section %d", section_id);
1899         return -EINVAL;
1900     }
1901 
1902     ret = vmstate_load(f, se);
1903     if (ret < 0) {
1904         error_report("error while loading state section id %d(%s)",
1905                      section_id, se->idstr);
1906         return ret;
1907     }
1908     if (!check_section_footer(f, se)) {
1909         return -EINVAL;
1910     }
1911 
1912     return 0;
1913 }
1914 
1915 static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
1916 {
1917     uint8_t section_type;
1918     int ret = 0;
1919 
1920     while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1921         ret = 0;
1922         trace_qemu_loadvm_state_section(section_type);
1923         switch (section_type) {
1924         case QEMU_VM_SECTION_START:
1925         case QEMU_VM_SECTION_FULL:
1926             ret = qemu_loadvm_section_start_full(f, mis);
1927             if (ret < 0) {
1928                 goto out;
1929             }
1930             break;
1931         case QEMU_VM_SECTION_PART:
1932         case QEMU_VM_SECTION_END:
1933             ret = qemu_loadvm_section_part_end(f, mis);
1934             if (ret < 0) {
1935                 goto out;
1936             }
1937             break;
1938         case QEMU_VM_COMMAND:
1939             ret = loadvm_process_command(f);
1940             trace_qemu_loadvm_state_section_command(ret);
1941             if ((ret < 0) || (ret & LOADVM_QUIT)) {
1942                 goto out;
1943             }
1944             break;
1945         default:
1946             error_report("Unknown savevm section type %d", section_type);
1947             ret = -EINVAL;
1948             goto out;
1949         }
1950     }
1951 
1952 out:
1953     if (ret < 0) {
1954         qemu_file_set_error(f, ret);
1955     }
1956     return ret;
1957 }
1958 
1959 int qemu_loadvm_state(QEMUFile *f)
1960 {
1961     MigrationIncomingState *mis = migration_incoming_get_current();
1962     Error *local_err = NULL;
1963     unsigned int v;
1964     int ret;
1965 
1966     if (qemu_savevm_state_blocked(&local_err)) {
1967         error_report_err(local_err);
1968         return -EINVAL;
1969     }
1970 
1971     v = qemu_get_be32(f);
1972     if (v != QEMU_VM_FILE_MAGIC) {
1973         error_report("Not a migration stream");
1974         return -EINVAL;
1975     }
1976 
1977     v = qemu_get_be32(f);
1978     if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1979         error_report("SaveVM v2 format is obsolete and don't work anymore");
1980         return -ENOTSUP;
1981     }
1982     if (v != QEMU_VM_FILE_VERSION) {
1983         error_report("Unsupported migration stream version");
1984         return -ENOTSUP;
1985     }
1986 
1987     if (!savevm_state.skip_configuration || enforce_config_section()) {
1988         if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
1989             error_report("Configuration section missing");
1990             return -EINVAL;
1991         }
1992         ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
1993 
1994         if (ret) {
1995             return ret;
1996         }
1997     }
1998 
1999     cpu_synchronize_all_pre_loadvm();
2000 
2001     ret = qemu_loadvm_state_main(f, mis);
2002     qemu_event_set(&mis->main_thread_load_event);
2003 
2004     trace_qemu_loadvm_state_post_main(ret);
2005 
2006     if (mis->have_listen_thread) {
2007         /* Listen thread still going, can't clean up yet */
2008         return ret;
2009     }
2010 
2011     if (ret == 0) {
2012         ret = qemu_file_get_error(f);
2013     }
2014 
2015     /*
2016      * Try to read in the VMDESC section as well, so that dumping tools that
2017      * intercept our migration stream have the chance to see it.
2018      */
2019 
2020     /* We've got to be careful; if we don't read the data and just shut the fd
2021      * then the sender can error if we close while it's still sending.
2022      * We also mustn't read data that isn't there; some transports (RDMA)
2023      * will stall waiting for that data when the source has already closed.
2024      */
2025     if (ret == 0 && should_send_vmdesc()) {
2026         uint8_t *buf;
2027         uint32_t size;
2028         uint8_t  section_type = qemu_get_byte(f);
2029 
2030         if (section_type != QEMU_VM_VMDESCRIPTION) {
2031             error_report("Expected vmdescription section, but got %d",
2032                          section_type);
2033             /*
2034              * It doesn't seem worth failing at this point since
2035              * we apparently have an otherwise valid VM state
2036              */
2037         } else {
2038             buf = g_malloc(0x1000);
2039             size = qemu_get_be32(f);
2040 
2041             while (size > 0) {
2042                 uint32_t read_chunk = MIN(size, 0x1000);
2043                 qemu_get_buffer(f, buf, read_chunk);
2044                 size -= read_chunk;
2045             }
2046             g_free(buf);
2047         }
2048     }
2049 
2050     cpu_synchronize_all_post_init();
2051 
2052     return ret;
2053 }
2054 
2055 int save_snapshot(const char *name, Error **errp)
2056 {
2057     BlockDriverState *bs, *bs1;
2058     QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2059     int ret = -1;
2060     QEMUFile *f;
2061     int saved_vm_running;
2062     uint64_t vm_state_size;
2063     qemu_timeval tv;
2064     struct tm tm;
2065     AioContext *aio_context;
2066 
2067     if (!bdrv_all_can_snapshot(&bs)) {
2068         error_setg(errp, "Device '%s' is writable but does not support "
2069                    "snapshots", bdrv_get_device_name(bs));
2070         return ret;
2071     }
2072 
2073     /* Delete old snapshots of the same name */
2074     if (name) {
2075         ret = bdrv_all_delete_snapshot(name, &bs1, errp);
2076         if (ret < 0) {
2077             error_prepend(errp, "Error while deleting snapshot on device "
2078                           "'%s': ", bdrv_get_device_name(bs1));
2079             return ret;
2080         }
2081     }
2082 
2083     bs = bdrv_all_find_vmstate_bs();
2084     if (bs == NULL) {
2085         error_setg(errp, "No block device can accept snapshots");
2086         return ret;
2087     }
2088     aio_context = bdrv_get_aio_context(bs);
2089 
2090     saved_vm_running = runstate_is_running();
2091 
2092     ret = global_state_store();
2093     if (ret) {
2094         error_setg(errp, "Error saving global state");
2095         return ret;
2096     }
2097     vm_stop(RUN_STATE_SAVE_VM);
2098 
2099     aio_context_acquire(aio_context);
2100 
2101     memset(sn, 0, sizeof(*sn));
2102 
2103     /* fill auxiliary fields */
2104     qemu_gettimeofday(&tv);
2105     sn->date_sec = tv.tv_sec;
2106     sn->date_nsec = tv.tv_usec * 1000;
2107     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2108 
2109     if (name) {
2110         ret = bdrv_snapshot_find(bs, old_sn, name);
2111         if (ret >= 0) {
2112             pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2113             pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2114         } else {
2115             pstrcpy(sn->name, sizeof(sn->name), name);
2116         }
2117     } else {
2118         /* cast below needed for OpenBSD where tv_sec is still 'long' */
2119         localtime_r((const time_t *)&tv.tv_sec, &tm);
2120         strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2121     }
2122 
2123     /* save the VM state */
2124     f = qemu_fopen_bdrv(bs, 1);
2125     if (!f) {
2126         error_setg(errp, "Could not open VM state file");
2127         goto the_end;
2128     }
2129     ret = qemu_savevm_state(f, errp);
2130     vm_state_size = qemu_ftell(f);
2131     qemu_fclose(f);
2132     if (ret < 0) {
2133         goto the_end;
2134     }
2135 
2136     ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, &bs);
2137     if (ret < 0) {
2138         error_setg(errp, "Error while creating snapshot on '%s'",
2139                    bdrv_get_device_name(bs));
2140         goto the_end;
2141     }
2142 
2143     ret = 0;
2144 
2145  the_end:
2146     aio_context_release(aio_context);
2147     if (saved_vm_running) {
2148         vm_start();
2149     }
2150     return ret;
2151 }
2152 
2153 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2154 {
2155     QEMUFile *f;
2156     QIOChannelFile *ioc;
2157     int saved_vm_running;
2158     int ret;
2159 
2160     saved_vm_running = runstate_is_running();
2161     vm_stop(RUN_STATE_SAVE_VM);
2162     global_state_store_running();
2163 
2164     ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp);
2165     if (!ioc) {
2166         goto the_end;
2167     }
2168     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
2169     f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
2170     ret = qemu_save_device_state(f);
2171     qemu_fclose(f);
2172     if (ret < 0) {
2173         error_setg(errp, QERR_IO_ERROR);
2174     }
2175 
2176  the_end:
2177     if (saved_vm_running) {
2178         vm_start();
2179     }
2180 }
2181 
2182 void qmp_xen_load_devices_state(const char *filename, Error **errp)
2183 {
2184     QEMUFile *f;
2185     QIOChannelFile *ioc;
2186     int ret;
2187 
2188     /* Guest must be paused before loading the device state; the RAM state
2189      * will already have been loaded by xc
2190      */
2191     if (runstate_is_running()) {
2192         error_setg(errp, "Cannot update device state while vm is running");
2193         return;
2194     }
2195     vm_stop(RUN_STATE_RESTORE_VM);
2196 
2197     ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
2198     if (!ioc) {
2199         return;
2200     }
2201     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
2202     f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
2203 
2204     ret = qemu_loadvm_state(f);
2205     qemu_fclose(f);
2206     if (ret < 0) {
2207         error_setg(errp, QERR_IO_ERROR);
2208     }
2209     migration_incoming_state_destroy();
2210 }
2211 
2212 int load_snapshot(const char *name, Error **errp)
2213 {
2214     BlockDriverState *bs, *bs_vm_state;
2215     QEMUSnapshotInfo sn;
2216     QEMUFile *f;
2217     int ret;
2218     AioContext *aio_context;
2219     MigrationIncomingState *mis = migration_incoming_get_current();
2220 
2221     if (!bdrv_all_can_snapshot(&bs)) {
2222         error_setg(errp,
2223                    "Device '%s' is writable but does not support snapshots",
2224                    bdrv_get_device_name(bs));
2225         return -ENOTSUP;
2226     }
2227     ret = bdrv_all_find_snapshot(name, &bs);
2228     if (ret < 0) {
2229         error_setg(errp,
2230                    "Device '%s' does not have the requested snapshot '%s'",
2231                    bdrv_get_device_name(bs), name);
2232         return ret;
2233     }
2234 
2235     bs_vm_state = bdrv_all_find_vmstate_bs();
2236     if (!bs_vm_state) {
2237         error_setg(errp, "No block device supports snapshots");
2238         return -ENOTSUP;
2239     }
2240     aio_context = bdrv_get_aio_context(bs_vm_state);
2241 
2242     /* Don't even try to load empty VM states */
2243     aio_context_acquire(aio_context);
2244     ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2245     aio_context_release(aio_context);
2246     if (ret < 0) {
2247         return ret;
2248     } else if (sn.vm_state_size == 0) {
2249         error_setg(errp, "This is a disk-only snapshot. Revert to it "
2250                    " offline using qemu-img");
2251         return -EINVAL;
2252     }
2253 
2254     /* Flush all IO requests so they don't interfere with the new state.  */
2255     bdrv_drain_all();
2256 
2257     ret = bdrv_all_goto_snapshot(name, &bs);
2258     if (ret < 0) {
2259         error_setg(errp, "Error %d while activating snapshot '%s' on '%s'",
2260                      ret, name, bdrv_get_device_name(bs));
2261         return ret;
2262     }
2263 
2264     /* restore the VM state */
2265     f = qemu_fopen_bdrv(bs_vm_state, 0);
2266     if (!f) {
2267         error_setg(errp, "Could not open VM state file");
2268         return -EINVAL;
2269     }
2270 
2271     qemu_system_reset(SHUTDOWN_CAUSE_NONE);
2272     mis->from_src_file = f;
2273 
2274     aio_context_acquire(aio_context);
2275     ret = qemu_loadvm_state(f);
2276     aio_context_release(aio_context);
2277 
2278     migration_incoming_state_destroy();
2279     if (ret < 0) {
2280         error_setg(errp, "Error %d while loading VM state", ret);
2281         return ret;
2282     }
2283 
2284     return 0;
2285 }
2286 
2287 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2288 {
2289     qemu_ram_set_idstr(mr->ram_block,
2290                        memory_region_name(mr), dev);
2291 }
2292 
2293 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2294 {
2295     qemu_ram_unset_idstr(mr->ram_block);
2296 }
2297 
2298 void vmstate_register_ram_global(MemoryRegion *mr)
2299 {
2300     vmstate_register_ram(mr, NULL);
2301 }
2302 
2303 bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
2304 {
2305     /* check needed if --only-migratable is specified */
2306     if (!only_migratable) {
2307         return true;
2308     }
2309 
2310     return !(vmsd && vmsd->unmigratable);
2311 }
2312