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