]> Git Repo - qemu.git/blame - migration/savevm.c
qmp-commands: move 'inject-nmi' doc to schema
[qemu.git] / migration / savevm.c
CommitLineData
a672b469
AL
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
76cc7b58
JQ
5 * Copyright (c) 2009-2015 Red Hat Inc
6 *
7 * Authors:
8 * Juan Quintela <[email protected]>
a672b469
AL
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 */
a672b469 28
1393a485 29#include "qemu/osdep.h"
33c11879 30#include "cpu.h"
abfd9ce3 31#include "hw/boards.h"
511d2b14 32#include "hw/hw.h"
7685ee6a 33#include "hw/qdev.h"
88c16567 34#include "hw/xen/xen.h"
1422e32d 35#include "net/net.h"
83c9089e 36#include "monitor/monitor.h"
9c17d615 37#include "sysemu/sysemu.h"
1de7afc9 38#include "qemu/timer.h"
511d2b14 39#include "audio/audio.h"
caf71f86 40#include "migration/migration.h"
eb59db53 41#include "migration/postcopy-ram.h"
cc7a8ea7 42#include "qapi/qmp/qerror.h"
d49b6836 43#include "qemu/error-report.h"
1de7afc9
PB
44#include "qemu/sockets.h"
45#include "qemu/queue.h"
9c17d615 46#include "sysemu/cpus.h"
022c62cb 47#include "exec/memory.h"
a7ae8355 48#include "qmp-commands.h"
517a13c9 49#include "trace.h"
093e3c42 50#include "qemu/bitops.h"
28085f7b 51#include "qemu/iov.h"
de08c606 52#include "block/snapshot.h"
f364ec65 53#include "block/qapi.h"
f348b6d1 54#include "qemu/cutils.h"
61b67d47 55#include "io/channel-buffer.h"
8925839f 56#include "io/channel-file.h"
a672b469 57
18995b98 58#ifndef ETH_P_RARP
f8778a77 59#define ETH_P_RARP 0x8035
18995b98
N
60#endif
61#define ARP_HTYPE_ETH 0x0001
62#define ARP_PTYPE_IP 0x0800
63#define ARP_OP_REQUEST_REV 0x3
64
093e3c42
DDAG
65const unsigned int postcopy_ram_discard_version = 0;
66
37fb569c
DDAG
67static bool skip_section_footers;
68
c76ca188
DDAG
69static 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" },
2e37701e
DDAG
74 [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" },
75 [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" },
093e3c42
DDAG
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" },
11cf1d98 81 [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" },
c76ca188
DDAG
82 [MIG_CMD_MAX] = { .len = -1, .name = "MAX" },
83};
84
18995b98 85static int announce_self_create(uint8_t *buf,
5cecf414 86 uint8_t *mac_addr)
a672b469 87{
18995b98
N
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) */
a672b469
AL
108}
109
f401ca22 110static void qemu_announce_self_iter(NICState *nic, void *opaque)
a672b469 111{
18995b98 112 uint8_t buf[60];
f401ca22
MM
113 int len;
114
9013dca5 115 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
f401ca22
MM
116 len = announce_self_create(buf, nic->conf->macaddr.a);
117
b356f76d 118 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
f401ca22
MM
119}
120
121
122static void qemu_announce_self_once(void *opaque)
123{
ed8b330b
GN
124 static int count = SELF_ANNOUNCE_ROUNDS;
125 QEMUTimer *timer = *(QEMUTimer **)opaque;
a672b469 126
f401ca22
MM
127 qemu_foreach_nic(qemu_announce_self_iter, NULL);
128
18995b98
N
129 if (--count) {
130 /* delay 50ms, 150ms, 250ms, ... */
bc72ad67 131 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
508e1180 132 self_announce_delay(count));
ed8b330b 133 } else {
5cecf414
EH
134 timer_del(timer);
135 timer_free(timer);
ed8b330b
GN
136 }
137}
138
139void qemu_announce_self(void)
140{
5cecf414
EH
141 static QEMUTimer *timer;
142 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
143 qemu_announce_self_once(&timer);
a672b469
AL
144}
145
146/***********************************************************/
147/* savevm/loadvm support */
148
05fcc848
KW
149static 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
a202a4c0
DDAG
164static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
165 size_t size)
a672b469 166{
45566e9c 167 return bdrv_load_vmstate(opaque, buf, pos, size);
a672b469
AL
168}
169
170static int bdrv_fclose(void *opaque)
171{
ad492c92 172 return bdrv_flush(opaque);
a672b469
AL
173}
174
9229bf3c
PB
175static const QEMUFileOps bdrv_read_ops = {
176 .get_buffer = block_get_buffer,
177 .close = bdrv_fclose
178};
179
180static const QEMUFileOps bdrv_write_ops = {
05fcc848
KW
181 .writev_buffer = block_writev_buffer,
182 .close = bdrv_fclose
9229bf3c
PB
183};
184
45566e9c 185static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
a672b469 186{
38ff78d3 187 if (is_writable) {
9229bf3c 188 return qemu_fopen_ops(bs, &bdrv_write_ops);
38ff78d3 189 }
9229bf3c 190 return qemu_fopen_ops(bs, &bdrv_read_ops);
a672b469
AL
191}
192
2ff68d07 193
bb1a6d8c
EH
194/* QEMUFile timer support.
195 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
196 */
2ff68d07 197
40daca54 198void timer_put(QEMUFile *f, QEMUTimer *ts)
2ff68d07
PB
199{
200 uint64_t expire_time;
201
e93379b0 202 expire_time = timer_expire_time_ns(ts);
2ff68d07
PB
203 qemu_put_be64(f, expire_time);
204}
205
40daca54 206void timer_get(QEMUFile *f, QEMUTimer *ts)
2ff68d07
PB
207{
208 uint64_t expire_time;
209
210 expire_time = qemu_get_be64(f);
211 if (expire_time != -1) {
bc72ad67 212 timer_mod_ns(ts, expire_time);
2ff68d07 213 } else {
bc72ad67 214 timer_del(ts);
2ff68d07
PB
215 }
216}
217
218
bb1a6d8c
EH
219/* VMState timer support.
220 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
221 */
dde0463b
JQ
222
223static int get_timer(QEMUFile *f, void *pv, size_t size)
224{
225 QEMUTimer *v = pv;
40daca54 226 timer_get(f, v);
dde0463b
JQ
227 return 0;
228}
229
84e2e3eb 230static void put_timer(QEMUFile *f, void *pv, size_t size)
dde0463b 231{
84e2e3eb 232 QEMUTimer *v = pv;
40daca54 233 timer_put(f, v);
dde0463b
JQ
234}
235
236const VMStateInfo vmstate_info_timer = {
237 .name = "timer",
238 .get = get_timer,
239 .put = put_timer,
240};
241
08e99e29 242
7685ee6a
AW
243typedef struct CompatEntry {
244 char idstr[256];
245 int instance_id;
246} CompatEntry;
247
a672b469 248typedef struct SaveStateEntry {
72cf2d4f 249 QTAILQ_ENTRY(SaveStateEntry) entry;
a672b469
AL
250 char idstr[256];
251 int instance_id;
4d2ffa08 252 int alias_id;
a672b469
AL
253 int version_id;
254 int section_id;
22ea40f4 255 SaveVMHandlers *ops;
9ed7d6ae 256 const VMStateDescription *vmsd;
a672b469 257 void *opaque;
7685ee6a 258 CompatEntry *compat;
a7ae8355 259 int is_ram;
a672b469
AL
260} SaveStateEntry;
261
0163a2e0
JQ
262typedef struct SaveState {
263 QTAILQ_HEAD(, SaveStateEntry) handlers;
264 int global_section_id;
61964c23
JQ
265 bool skip_configuration;
266 uint32_t len;
267 const char *name;
59811a32 268 uint32_t target_page_bits;
0163a2e0
JQ
269} SaveState;
270
271static SaveState savevm_state = {
272 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
273 .global_section_id = 0,
61964c23
JQ
274 .skip_configuration = false,
275};
276
277void savevm_skip_configuration(void)
278{
279 savevm_state.skip_configuration = true;
280}
281
282
283static 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;
59811a32
PM
290 state->target_page_bits = TARGET_PAGE_BITS;
291}
292
293static 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;
61964c23
JQ
303}
304
305static 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) {
15d61692
GK
311 error_report("Machine type received is '%.*s' and local is '%s'",
312 (int) state->len, state->name, current_name);
61964c23
JQ
313 return -EINVAL;
314 }
59811a32
PM
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
61964c23
JQ
322 return 0;
323}
324
59811a32
PM
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 */
332static bool vmstate_target_page_bits_needed(void *opaque)
333{
334 return TARGET_PAGE_BITS > TARGET_PAGE_BITS_MIN;
335}
336
337static 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
61964c23
JQ
348static const VMStateDescription vmstate_configuration = {
349 .name = "configuration",
350 .version_id = 1,
59811a32 351 .pre_load = configuration_pre_load,
61964c23
JQ
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 },
59811a32
PM
359 .subsections = (const VMStateDescription*[]) {
360 &vmstate_target_page_bits,
361 NULL
362 }
0163a2e0 363};
a672b469 364
abfd9ce3
AS
365static void dump_vmstate_vmsd(FILE *out_file,
366 const VMStateDescription *vmsd, int indent,
367 bool is_subsection);
368
369static 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
387static void dump_vmstate_vmss(FILE *out_file,
5cd8cada 388 const VMStateDescription **subsection,
abfd9ce3
AS
389 int indent)
390{
5cd8cada
JQ
391 if (*subsection != NULL) {
392 dump_vmstate_vmsd(out_file, *subsection, indent, true);
abfd9ce3
AS
393 }
394}
395
396static 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) {
5cd8cada 433 const VMStateDescription **subsection = vmsd->subsections;
abfd9ce3
AS
434 bool first;
435
436 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
437 first = true;
5cd8cada 438 while (*subsection != NULL) {
abfd9ce3
AS
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
451static 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
462void 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
8718e999
JQ
503static int calculate_new_instance_id(const char *idstr)
504{
505 SaveStateEntry *se;
506 int instance_id = 0;
507
0163a2e0 508 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
8718e999
JQ
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
7685ee6a
AW
517static int calculate_compat_instance_id(const char *idstr)
518{
519 SaveStateEntry *se;
520 int instance_id = 0;
521
0163a2e0 522 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
38ff78d3 523 if (!se->compat) {
7685ee6a 524 continue;
38ff78d3 525 }
7685ee6a
AW
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
f37bc036
PX
535static 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
543static 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
a672b469
AL
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. */
0be71e32
AW
567int register_savevm_live(DeviceState *dev,
568 const char *idstr,
a672b469
AL
569 int instance_id,
570 int version_id,
7908c78d 571 SaveVMHandlers *ops,
a672b469
AL
572 void *opaque)
573{
8718e999 574 SaveStateEntry *se;
a672b469 575
97f3ad35 576 se = g_new0(SaveStateEntry, 1);
a672b469 577 se->version_id = version_id;
0163a2e0 578 se->section_id = savevm_state.global_section_id++;
7908c78d 579 se->ops = ops;
a672b469 580 se->opaque = opaque;
9ed7d6ae 581 se->vmsd = NULL;
a7ae8355 582 /* if this is a live_savem then set is_ram */
16310a3c 583 if (ops->save_live_setup != NULL) {
a7ae8355
SS
584 se->is_ram = 1;
585 }
a672b469 586
09e5ab63
AL
587 if (dev) {
588 char *id = qdev_get_dev_path(dev);
7685ee6a
AW
589 if (id) {
590 pstrcpy(se->idstr, sizeof(se->idstr), id);
591 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 592 g_free(id);
7685ee6a 593
97f3ad35 594 se->compat = g_new0(CompatEntry, 1);
7685ee6a
AW
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
8718e999 603 if (instance_id == -1) {
7685ee6a 604 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
605 } else {
606 se->instance_id = instance_id;
a672b469 607 }
7685ee6a 608 assert(!se->compat || se->instance_id == 0);
f37bc036 609 savevm_state_handler_insert(se);
a672b469
AL
610 return 0;
611}
612
0be71e32
AW
613int register_savevm(DeviceState *dev,
614 const char *idstr,
a672b469
AL
615 int instance_id,
616 int version_id,
617 SaveStateHandler *save_state,
618 LoadStateHandler *load_state,
619 void *opaque)
620{
97f3ad35 621 SaveVMHandlers *ops = g_new0(SaveVMHandlers, 1);
7908c78d
JQ
622 ops->save_state = save_state;
623 ops->load_state = load_state;
0be71e32 624 return register_savevm_live(dev, idstr, instance_id, version_id,
7908c78d 625 ops, opaque);
a672b469
AL
626}
627
0be71e32 628void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
41bd13af 629{
8718e999 630 SaveStateEntry *se, *new_se;
7685ee6a
AW
631 char id[256] = "";
632
09e5ab63
AL
633 if (dev) {
634 char *path = qdev_get_dev_path(dev);
7685ee6a
AW
635 if (path) {
636 pstrcpy(id, sizeof(id), path);
637 pstrcat(id, sizeof(id), "/");
7267c094 638 g_free(path);
7685ee6a
AW
639 }
640 }
641 pstrcat(id, sizeof(id), idstr);
41bd13af 642
0163a2e0 643 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
7685ee6a 644 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
0163a2e0 645 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
ef1e1e07 646 g_free(se->compat);
22ea40f4 647 g_free(se->ops);
7267c094 648 g_free(se);
41bd13af 649 }
41bd13af
AL
650 }
651}
652
0be71e32 653int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
4d2ffa08
JK
654 const VMStateDescription *vmsd,
655 void *opaque, int alias_id,
656 int required_for_version)
9ed7d6ae 657{
8718e999 658 SaveStateEntry *se;
9ed7d6ae 659
4d2ffa08
JK
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
97f3ad35 663 se = g_new0(SaveStateEntry, 1);
9ed7d6ae 664 se->version_id = vmsd->version_id;
0163a2e0 665 se->section_id = savevm_state.global_section_id++;
9ed7d6ae
JQ
666 se->opaque = opaque;
667 se->vmsd = vmsd;
4d2ffa08 668 se->alias_id = alias_id;
9ed7d6ae 669
09e5ab63
AL
670 if (dev) {
671 char *id = qdev_get_dev_path(dev);
7685ee6a
AW
672 if (id) {
673 pstrcpy(se->idstr, sizeof(se->idstr), id);
674 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 675 g_free(id);
7685ee6a 676
97f3ad35 677 se->compat = g_new0(CompatEntry, 1);
7685ee6a
AW
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
8718e999 686 if (instance_id == -1) {
7685ee6a 687 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
688 } else {
689 se->instance_id = instance_id;
9ed7d6ae 690 }
7685ee6a 691 assert(!se->compat || se->instance_id == 0);
f37bc036 692 savevm_state_handler_insert(se);
9ed7d6ae
JQ
693 return 0;
694}
695
0be71e32
AW
696void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
697 void *opaque)
9ed7d6ae 698{
1eb7538b
JQ
699 SaveStateEntry *se, *new_se;
700
0163a2e0 701 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
1eb7538b 702 if (se->vmsd == vmsd && se->opaque == opaque) {
0163a2e0 703 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
ef1e1e07 704 g_free(se->compat);
7267c094 705 g_free(se);
1eb7538b
JQ
706 }
707 }
9ed7d6ae
JQ
708}
709
4082be4d
JQ
710static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
711{
9013dca5 712 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
9ed7d6ae 713 if (!se->vmsd) { /* Old style */
22ea40f4 714 return se->ops->load_state(f, se->opaque, version_id);
9ed7d6ae
JQ
715 }
716 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
4082be4d
JQ
717}
718
8118f095
AG
719static 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
739static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
4082be4d 740{
9013dca5 741 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
8118f095
AG
742 if (!se->vmsd) {
743 vmstate_save_old_style(f, se, vmdesc);
dc912121 744 return;
9ed7d6ae 745 }
8118f095 746 vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
4082be4d
JQ
747}
748
37fb569c
DDAG
749void savevm_skip_section_footers(void)
750{
751 skip_section_footers = true;
752}
753
ce39bfc9
DDAG
754/*
755 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
756 */
757static 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
f68945d4
DDAG
775/*
776 * Write a footer onto device sections that catches cases misformatted device
777 * sections.
778 */
779static 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
c76ca188
DDAG
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 */
796void 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
2e37701e
DDAG
809void 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
818void 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
11cf1d98
DDAG
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 */
61b67d47 833int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
11cf1d98 834{
11cf1d98
DDAG
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
61b67d47 848 qemu_put_buffer(f, buf, len);
11cf1d98
DDAG
849
850 return 0;
851}
852
093e3c42
DDAG
853/* Send prior to any postcopy transfer */
854void 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 */
881void 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++) {
4d885131 901 stq_be_p(buf + tmplen, start_list[t]);
093e3c42 902 tmplen += 8;
4d885131 903 stq_be_p(buf + tmplen, length_list[t]);
093e3c42
DDAG
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. */
911void 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 */
918void 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
e1c37d0e 924bool qemu_savevm_state_blocked(Error **errp)
dc912121
AW
925{
926 SaveStateEntry *se;
927
0163a2e0 928 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
7d854c47 929 if (se->vmsd && se->vmsd->unmigratable) {
f231b88d
CR
930 error_setg(errp, "State blocked by non-migratable device '%s'",
931 se->idstr);
dc912121
AW
932 return true;
933 }
934 }
935 return false;
936}
937
902c053d
GK
938static bool enforce_config_section(void)
939{
940 MachineState *machine = MACHINE(qdev_get_machine());
941 return machine->enforce_config_section;
942}
943
f796baa1
DDAG
944void 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);
172dfd4f 949
902c053d 950 if (!savevm_state.skip_configuration || enforce_config_section()) {
172dfd4f
DDAG
951 qemu_put_byte(f, QEMU_VM_CONFIGURATION);
952 vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
953 }
954
f796baa1
DDAG
955}
956
47c8c17a
PB
957void qemu_savevm_state_begin(QEMUFile *f,
958 const MigrationParams *params)
a672b469
AL
959{
960 SaveStateEntry *se;
39346385 961 int ret;
a672b469 962
9013dca5 963 trace_savevm_state_begin();
0163a2e0 964 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
22ea40f4 965 if (!se->ops || !se->ops->set_params) {
c163b5ca 966 continue;
6607ae23 967 }
22ea40f4 968 se->ops->set_params(params, se->opaque);
c163b5ca 969 }
38ff78d3 970
0163a2e0 971 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
d1315aac 972 if (!se->ops || !se->ops->save_live_setup) {
a672b469 973 continue;
22ea40f4 974 }
6bd68781
JQ
975 if (se->ops && se->ops->is_active) {
976 if (!se->ops->is_active(se->opaque)) {
977 continue;
978 }
979 }
ce39bfc9 980 save_section_header(f, se, QEMU_VM_SECTION_START);
a672b469 981
d1315aac 982 ret = se->ops->save_live_setup(f, se->opaque);
f68945d4 983 save_section_footer(f, se);
2975725f 984 if (ret < 0) {
47c8c17a
PB
985 qemu_file_set_error(f, ret);
986 break;
2975725f 987 }
a672b469 988 }
a672b469
AL
989}
990
39346385 991/*
07f35073 992 * this function has three return values:
39346385
JQ
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 */
35ecd943 997int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
a672b469
AL
998{
999 SaveStateEntry *se;
1000 int ret = 1;
1001
9013dca5 1002 trace_savevm_state_iterate();
0163a2e0 1003 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
16310a3c 1004 if (!se->ops || !se->ops->save_live_iterate) {
a672b469 1005 continue;
22ea40f4 1006 }
6bd68781
JQ
1007 if (se->ops && se->ops->is_active) {
1008 if (!se->ops->is_active(se->opaque)) {
1009 continue;
1010 }
1011 }
35ecd943
DDAG
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 }
aac844ed
JQ
1021 if (qemu_file_rate_limit(f)) {
1022 return 0;
1023 }
464400f6 1024 trace_savevm_section_start(se->idstr, se->section_id);
ce39bfc9
DDAG
1025
1026 save_section_header(f, se, QEMU_VM_SECTION_PART);
a672b469 1027
16310a3c 1028 ret = se->ops->save_live_iterate(f, se->opaque);
a5df2a02 1029 trace_savevm_section_end(se->idstr, se->section_id, ret);
f68945d4 1030 save_section_footer(f, se);
517a13c9 1031
47c8c17a
PB
1032 if (ret < 0) {
1033 qemu_file_set_error(f, ret);
1034 }
2975725f 1035 if (ret <= 0) {
90697be8
JK
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 }
a672b469 1042 }
39346385 1043 return ret;
a672b469
AL
1044}
1045
9850c604
AG
1046static bool should_send_vmdesc(void)
1047{
1048 MachineState *machine = MACHINE(qdev_get_machine());
8421b205
DDAG
1049 bool in_postcopy = migration_in_postcopy(migrate_get_current());
1050 return !machine->suppress_vmdesc && !in_postcopy;
9850c604
AG
1051}
1052
763c906b
DDAG
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 */
1060void 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
1c0d249d 1092void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
a672b469 1093{
8118f095
AG
1094 QJSON *vmdesc;
1095 int vmdesc_len;
a672b469 1096 SaveStateEntry *se;
2975725f 1097 int ret;
763c906b 1098 bool in_postcopy = migration_in_postcopy(migrate_get_current());
a672b469 1099
a3e06c3d 1100 trace_savevm_state_complete_precopy();
9013dca5 1101
ea375f9a
JK
1102 cpu_synchronize_all_states();
1103
0163a2e0 1104 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
763c906b
DDAG
1105 if (!se->ops ||
1106 (in_postcopy && se->ops->save_live_complete_postcopy) ||
1c0d249d 1107 (in_postcopy && !iterable_only) ||
763c906b 1108 !se->ops->save_live_complete_precopy) {
a672b469 1109 continue;
22ea40f4 1110 }
1c0d249d 1111
6bd68781
JQ
1112 if (se->ops && se->ops->is_active) {
1113 if (!se->ops->is_active(se->opaque)) {
1114 continue;
1115 }
1116 }
464400f6 1117 trace_savevm_section_start(se->idstr, se->section_id);
ce39bfc9
DDAG
1118
1119 save_section_header(f, se, QEMU_VM_SECTION_END);
a672b469 1120
a3e06c3d 1121 ret = se->ops->save_live_complete_precopy(f, se->opaque);
a5df2a02 1122 trace_savevm_section_end(se->idstr, se->section_id, ret);
f68945d4 1123 save_section_footer(f, se);
2975725f 1124 if (ret < 0) {
47c8c17a
PB
1125 qemu_file_set_error(f, ret);
1126 return;
2975725f 1127 }
a672b469
AL
1128 }
1129
1c0d249d
DDAG
1130 if (iterable_only) {
1131 return;
1132 }
1133
8118f095
AG
1134 vmdesc = qjson_new();
1135 json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE);
1136 json_start_array(vmdesc, "devices");
0163a2e0 1137 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a672b469 1138
22ea40f4 1139 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
5cecf414 1140 continue;
22ea40f4 1141 }
df896152
JQ
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
464400f6 1147 trace_savevm_section_start(se->idstr, se->section_id);
8118f095
AG
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
ce39bfc9 1153 save_section_header(f, se, QEMU_VM_SECTION_FULL);
8118f095 1154 vmstate_save(f, se, vmdesc);
a5df2a02 1155 trace_savevm_section_end(se->idstr, se->section_id, 0);
f68945d4 1156 save_section_footer(f, se);
bdf46d64
WY
1157
1158 json_end_object(vmdesc);
a672b469
AL
1159 }
1160
763c906b
DDAG
1161 if (!in_postcopy) {
1162 /* Postcopy stream will still be going */
1163 qemu_put_byte(f, QEMU_VM_EOF);
1164 }
8118f095
AG
1165
1166 json_end_array(vmdesc);
1167 qjson_finish(vmdesc);
1168 vmdesc_len = strlen(qjson_get_str(vmdesc));
1169
9850c604
AG
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 }
b72fe9e6 1175 qjson_destroy(vmdesc);
8118f095 1176
edaae611 1177 qemu_fflush(f);
a672b469
AL
1178}
1179
c31b098f
DDAG
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 */
1184void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
1185 uint64_t *res_non_postcopiable,
1186 uint64_t *res_postcopiable)
e4ed1541
JQ
1187{
1188 SaveStateEntry *se;
c31b098f
DDAG
1189
1190 *res_non_postcopiable = 0;
1191 *res_postcopiable = 0;
1192
e4ed1541 1193
0163a2e0 1194 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
e4ed1541
JQ
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 }
c31b098f
DDAG
1203 se->ops->save_live_pending(f, se->opaque, max_size,
1204 res_non_postcopiable, res_postcopiable);
e4ed1541 1205 }
e4ed1541
JQ
1206}
1207
ea7415fa 1208void qemu_savevm_state_cleanup(void)
4ec7fcc7
JK
1209{
1210 SaveStateEntry *se;
1211
ea7415fa 1212 trace_savevm_state_cleanup();
0163a2e0 1213 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
d1a8548c
LL
1214 if (se->ops && se->ops->cleanup) {
1215 se->ops->cleanup(se->opaque);
4ec7fcc7
JK
1216 }
1217 }
1218}
1219
5d80448c 1220static int qemu_savevm_state(QEMUFile *f, Error **errp)
a672b469 1221{
a672b469 1222 int ret;
6607ae23
IY
1223 MigrationParams params = {
1224 .blk = 0,
1225 .shared = 0
1226 };
aefeb18b 1227 MigrationState *ms = migrate_init(&params);
6dcf6668 1228 MigrationStatus status;
89a02a9f 1229 ms->to_dst_file = f;
a672b469 1230
24f3902b 1231 if (migration_is_blocked(errp)) {
6dcf6668
DL
1232 ret = -EINVAL;
1233 goto done;
dc912121
AW
1234 }
1235
9b095037 1236 qemu_mutex_unlock_iothread();
f796baa1 1237 qemu_savevm_state_header(f);
47c8c17a 1238 qemu_savevm_state_begin(f, &params);
9b095037
PB
1239 qemu_mutex_lock_iothread();
1240
47c8c17a 1241 while (qemu_file_get_error(f) == 0) {
35ecd943 1242 if (qemu_savevm_state_iterate(f, false) > 0) {
47c8c17a
PB
1243 break;
1244 }
1245 }
a672b469 1246
47c8c17a 1247 ret = qemu_file_get_error(f);
39346385 1248 if (ret == 0) {
1c0d249d 1249 qemu_savevm_state_complete_precopy(f, false);
624b9cc2 1250 ret = qemu_file_get_error(f);
39346385 1251 }
15b3b8ea 1252 qemu_savevm_state_cleanup();
04943eba 1253 if (ret != 0) {
5d80448c 1254 error_setg_errno(errp, -ret, "Error while writing VM state");
04943eba 1255 }
6dcf6668
DL
1256
1257done:
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);
a672b469
AL
1264 return ret;
1265}
1266
a7ae8355
SS
1267static 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
0163a2e0 1276 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a7ae8355
SS
1277 if (se->is_ram) {
1278 continue;
1279 }
22ea40f4 1280 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
a7ae8355
SS
1281 continue;
1282 }
df896152
JQ
1283 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1284 continue;
1285 }
a7ae8355 1286
ce39bfc9 1287 save_section_header(f, se, QEMU_VM_SECTION_FULL);
a7ae8355 1288
8118f095 1289 vmstate_save(f, se, NULL);
f68945d4
DDAG
1290
1291 save_section_footer(f, se);
a7ae8355
SS
1292 }
1293
1294 qemu_put_byte(f, QEMU_VM_EOF);
1295
1296 return qemu_file_get_error(f);
1297}
1298
a672b469
AL
1299static SaveStateEntry *find_se(const char *idstr, int instance_id)
1300{
1301 SaveStateEntry *se;
1302
0163a2e0 1303 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a672b469 1304 if (!strcmp(se->idstr, idstr) &&
4d2ffa08
JK
1305 (instance_id == se->instance_id ||
1306 instance_id == se->alias_id))
a672b469 1307 return se;
7685ee6a
AW
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 }
a672b469
AL
1315 }
1316 return NULL;
1317}
1318
7b89bf27
DDAG
1319enum LoadVMExitCodes {
1320 /* Allow a command to quit all layers of nested loadvm loops */
1321 LOADVM_QUIT = 1,
1322};
1323
1324static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis);
093e3c42
DDAG
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 */
1331static 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
eb59db53
DDAG
1342 if (!postcopy_ram_supported_by_host()) {
1343 return -1;
1344 }
1345
093e3c42
DDAG
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
1caddf8a
DDAG
1371 if (ram_postcopy_incoming_init(mis)) {
1372 return -1;
1373 }
1374
1375 postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1376
093e3c42
DDAG
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 */
1385static 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 */
f9527107 1397 tmp = postcopy_ram_prepare_discard(mis);
093e3c42
DDAG
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) {
093e3c42
DDAG
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 }
093e3c42
DDAG
1455 }
1456 trace_loadvm_postcopy_ram_handle_discard_end();
1457
1458 return 0;
1459}
1460
c76201ab
DDAG
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 */
1468static void *postcopy_ram_listen_thread(void *opaque)
1469{
1470 QEMUFile *f = opaque;
1471 MigrationIncomingState *mis = migration_incoming_get_current();
1472 int load_res;
1473
6ba996bb
DDAG
1474 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1475 MIGRATION_STATUS_POSTCOPY_ACTIVE);
c76201ab
DDAG
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);
6ba996bb
DDAG
1492 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1493 MIGRATION_STATUS_FAILED);
c76201ab
DDAG
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);
c76201ab
DDAG
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
6ba996bb
DDAG
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
c76201ab
DDAG
1525 return NULL;
1526}
1527
093e3c42
DDAG
1528/* After this message we must be able to immediately receive postcopy data */
1529static 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 }
f9527107
DDAG
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 }
093e3c42 1544
f0a227ad
DDAG
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
c76201ab
DDAG
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,
a587a3fe 1564 QEMU_THREAD_DETACHED);
c76201ab
DDAG
1565 qemu_sem_wait(&mis->listen_thread_sem);
1566 qemu_sem_destroy(&mis->listen_thread_sem);
1567
093e3c42
DDAG
1568 return 0;
1569}
1570
86469922
DL
1571
1572typedef struct {
1573 QEMUBH *bh;
1574} HandleRunBhData;
1575
ea6a55bc 1576static void loadvm_postcopy_handle_run_bh(void *opaque)
093e3c42 1577{
27c6825b 1578 Error *local_err = NULL;
86469922 1579 HandleRunBhData *data = opaque;
093e3c42 1580
27c6825b
DDAG
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);
27c6825b
DDAG
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
093e3c42
DDAG
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
86469922
DL
1607 qemu_bh_delete(data->bh);
1608 g_free(data);
ea6a55bc
DL
1609}
1610
1611/* After all discards we can start running and asking for pages */
1612static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
1613{
1614 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
86469922 1615 HandleRunBhData *data;
ea6a55bc
DL
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
86469922
DL
1623 data = g_new(HandleRunBhData, 1);
1624 data->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, data);
1625 qemu_bh_schedule(data->bh);
ea6a55bc 1626
27c6825b
DDAG
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;
093e3c42
DDAG
1633}
1634
c76ca188 1635/**
11cf1d98
DDAG
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
c76ca188 1641 *
11cf1d98
DDAG
1642 * Returns: Negative values on error
1643 *
1644 */
1645static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
1646{
1647 int ret;
61b67d47
DB
1648 size_t length;
1649 QIOChannelBuffer *bioc;
11cf1d98
DDAG
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) {
61b67d47 1655 error_report("Unreasonably large packaged state: %zu", length);
11cf1d98
DDAG
1656 return -1;
1657 }
61b67d47
DB
1658
1659 bioc = qio_channel_buffer_new(length);
6f01f136 1660 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
61b67d47
DB
1661 ret = qemu_get_buffer(mis->from_src_file,
1662 bioc->data,
1663 length);
11cf1d98 1664 if (ret != length) {
61b67d47
DB
1665 object_unref(OBJECT(bioc));
1666 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
9af9e0fe 1667 ret, length);
11cf1d98
DDAG
1668 return (ret < 0) ? ret : -EAGAIN;
1669 }
61b67d47 1670 bioc->usage += length;
11cf1d98
DDAG
1671 trace_loadvm_handle_cmd_packaged_received(ret);
1672
61b67d47 1673 QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
11cf1d98
DDAG
1674
1675 ret = qemu_loadvm_state_main(packf, mis);
1676 trace_loadvm_handle_cmd_packaged_main(ret);
1677 qemu_fclose(packf);
61b67d47 1678 object_unref(OBJECT(bioc));
11cf1d98
DDAG
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
c76ca188
DDAG
1688 */
1689static int loadvm_process_command(QEMUFile *f)
1690{
2e37701e 1691 MigrationIncomingState *mis = migration_incoming_get_current();
c76ca188
DDAG
1692 uint16_t cmd;
1693 uint16_t len;
2e37701e 1694 uint32_t tmp32;
c76ca188
DDAG
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) {
2e37701e
DDAG
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 }
6decec93 1734 migrate_send_rp_pong(mis, tmp32);
2e37701e 1735 break;
093e3c42 1736
11cf1d98
DDAG
1737 case MIG_CMD_PACKAGED:
1738 return loadvm_handle_cmd_packaged(mis);
1739
093e3c42
DDAG
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);
c76ca188
DDAG
1751 }
1752
1753 return 0;
1754}
1755
1a8f46f8 1756struct LoadStateEntry {
72cf2d4f 1757 QLIST_ENTRY(LoadStateEntry) entry;
a672b469
AL
1758 SaveStateEntry *se;
1759 int section_id;
1760 int version_id;
1a8f46f8 1761};
a672b469 1762
59f39a47
DDAG
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 */
1769static 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
1a8f46f8 1798void loadvm_free_handlers(MigrationIncomingState *mis)
a672b469 1799{
f4dbb8dd 1800 LoadStateEntry *le, *new_le;
1a8f46f8
DDAG
1801
1802 QLIST_FOREACH_SAFE(le, &mis->loadvm_handlers, entry, new_le) {
1803 QLIST_REMOVE(le, entry);
1804 g_free(le);
1805 }
1806}
1807
fb3520a8
HZ
1808static int
1809qemu_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
88c16567
WC
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
fb3520a8
HZ
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
1871static int
1872qemu_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
7b89bf27 1904static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
1a8f46f8 1905{
a672b469 1906 uint8_t section_type;
ccb783c3 1907 int ret = 0;
61964c23 1908
a672b469 1909 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
ccb783c3 1910 ret = 0;
a5df2a02 1911 trace_qemu_loadvm_state_section(section_type);
a672b469
AL
1912 switch (section_type) {
1913 case QEMU_VM_SECTION_START:
1914 case QEMU_VM_SECTION_FULL:
fb3520a8 1915 ret = qemu_loadvm_section_start_full(f, mis);
b5a22e4a 1916 if (ret < 0) {
ccb783c3 1917 goto out;
b5a22e4a 1918 }
a672b469
AL
1919 break;
1920 case QEMU_VM_SECTION_PART:
1921 case QEMU_VM_SECTION_END:
fb3520a8 1922 ret = qemu_loadvm_section_part_end(f, mis);
b5a22e4a 1923 if (ret < 0) {
ccb783c3 1924 goto out;
b5a22e4a 1925 }
a672b469 1926 break;
c76ca188
DDAG
1927 case QEMU_VM_COMMAND:
1928 ret = loadvm_process_command(f);
7b89bf27
DDAG
1929 trace_qemu_loadvm_state_section_command(ret);
1930 if ((ret < 0) || (ret & LOADVM_QUIT)) {
ccb783c3 1931 goto out;
c76ca188
DDAG
1932 }
1933 break;
a672b469 1934 default:
6a64b644 1935 error_report("Unknown savevm section type %d", section_type);
ccb783c3
DDAG
1936 ret = -EINVAL;
1937 goto out;
a672b469
AL
1938 }
1939 }
1940
ccb783c3
DDAG
1941out:
1942 if (ret < 0) {
1943 qemu_file_set_error(f, ret);
1944 }
1945 return ret;
7b89bf27
DDAG
1946}
1947
1948int 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
902c053d 1976 if (!savevm_state.skip_configuration || enforce_config_section()) {
7b89bf27
DDAG
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
c76201ab
DDAG
1993 if (mis->have_listen_thread) {
1994 /* Listen thread still going, can't clean up yet */
1995 return ret;
1996 }
1997
7b89bf27
DDAG
1998 if (ret == 0) {
1999 ret = qemu_file_get_error(f);
2000 }
1925cebc
AG
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 */
1aca9a5f
DDAG
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 */
7b89bf27 2012 if (ret == 0 && should_send_vmdesc()) {
1aca9a5f
DDAG
2013 uint8_t *buf;
2014 uint32_t size;
7b89bf27 2015 uint8_t section_type = qemu_get_byte(f);
1aca9a5f
DDAG
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);
1925cebc 2034 }
1925cebc
AG
2035 }
2036
ea375f9a
JK
2037 cpu_synchronize_all_post_init();
2038
a672b469
AL
2039 return ret;
2040}
2041
3e5a50d6 2042void hmp_savevm(Monitor *mon, const QDict *qdict)
a672b469
AL
2043{
2044 BlockDriverState *bs, *bs1;
2045 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
cb499fb2 2046 int ret;
a672b469
AL
2047 QEMUFile *f;
2048 int saved_vm_running;
c2c9a466 2049 uint64_t vm_state_size;
68b891ec 2050 qemu_timeval tv;
7d631a11 2051 struct tm tm;
d54908a5 2052 const char *name = qdict_get_try_str(qdict, "name");
5d80448c 2053 Error *local_err = NULL;
79b3c12a 2054 AioContext *aio_context;
a672b469 2055
e9ff957a
DL
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;
feeee5ac
MDCF
2060 }
2061
0b461605
DL
2062 /* Delete old snapshots of the same name */
2063 if (name && bdrv_all_delete_snapshot(name, &bs1, &local_err) < 0) {
d410fe14
MA
2064 error_reportf_err(local_err,
2065 "Error while deleting snapshot on device '%s': ",
2066 bdrv_get_device_name(bs1));
0b461605
DL
2067 return;
2068 }
2069
7cb14481
DL
2070 bs = bdrv_all_find_vmstate_bs();
2071 if (bs == NULL) {
376253ec 2072 monitor_printf(mon, "No block device can accept snapshots\n");
a672b469
AL
2073 return;
2074 }
79b3c12a 2075 aio_context = bdrv_get_aio_context(bs);
a672b469 2076
1354869c 2077 saved_vm_running = runstate_is_running();
560d027b
JQ
2078
2079 ret = global_state_store();
2080 if (ret) {
2081 monitor_printf(mon, "Error saving global state\n");
2082 return;
2083 }
0461d5a6 2084 vm_stop(RUN_STATE_SAVE_VM);
a672b469 2085
79b3c12a
DL
2086 aio_context_acquire(aio_context);
2087
cb499fb2 2088 memset(sn, 0, sizeof(*sn));
a672b469
AL
2089
2090 /* fill auxiliary fields */
68b891ec 2091 qemu_gettimeofday(&tv);
a672b469
AL
2092 sn->date_sec = tv.tv_sec;
2093 sn->date_nsec = tv.tv_usec * 1000;
bc72ad67 2094 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
a672b469 2095
7d631a11
MDCF
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 {
d7d9b528
BS
2105 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2106 localtime_r((const time_t *)&tv.tv_sec, &tm);
7d631a11 2107 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
7d631a11
MDCF
2108 }
2109
a672b469 2110 /* save the VM state */
45566e9c 2111 f = qemu_fopen_bdrv(bs, 1);
a672b469 2112 if (!f) {
376253ec 2113 monitor_printf(mon, "Could not open VM state file\n");
a672b469
AL
2114 goto the_end;
2115 }
5d80448c 2116 ret = qemu_savevm_state(f, &local_err);
2d22b18f 2117 vm_state_size = qemu_ftell(f);
a672b469
AL
2118 qemu_fclose(f);
2119 if (ret < 0) {
193227f9 2120 error_report_err(local_err);
a672b469
AL
2121 goto the_end;
2122 }
2123
a9085f9b
DL
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));
a672b469
AL
2128 }
2129
2130 the_end:
79b3c12a 2131 aio_context_release(aio_context);
38ff78d3 2132 if (saved_vm_running) {
a672b469 2133 vm_start();
38ff78d3 2134 }
a672b469
AL
2135}
2136
a7ae8355
SS
2137void qmp_xen_save_devices_state(const char *filename, Error **errp)
2138{
2139 QEMUFile *f;
8925839f 2140 QIOChannelFile *ioc;
a7ae8355
SS
2141 int saved_vm_running;
2142 int ret;
2143
2144 saved_vm_running = runstate_is_running();
2145 vm_stop(RUN_STATE_SAVE_VM);
c69adea4 2146 global_state_store_running();
a7ae8355 2147
8925839f
DB
2148 ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp);
2149 if (!ioc) {
a7ae8355
SS
2150 goto the_end;
2151 }
6f01f136 2152 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
8925839f 2153 f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
a7ae8355
SS
2154 ret = qemu_save_device_state(f);
2155 qemu_fclose(f);
2156 if (ret < 0) {
c6bd8c70 2157 error_setg(errp, QERR_IO_ERROR);
a7ae8355
SS
2158 }
2159
2160 the_end:
38ff78d3 2161 if (saved_vm_running) {
a7ae8355 2162 vm_start();
38ff78d3 2163 }
a7ae8355
SS
2164}
2165
88c16567
WC
2166void 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 }
6f01f136 2185 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
88c16567
WC
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
03cd4655 2197int load_vmstate(const char *name)
a672b469 2198{
f0aa7a8b 2199 BlockDriverState *bs, *bs_vm_state;
2d22b18f 2200 QEMUSnapshotInfo sn;
a672b469 2201 QEMUFile *f;
751c6a17 2202 int ret;
79b3c12a 2203 AioContext *aio_context;
a672b469 2204
849f96e2
DL
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 }
723ccda1
DL
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 }
849f96e2 2216
7cb14481 2217 bs_vm_state = bdrv_all_find_vmstate_bs();
f0aa7a8b
MDCF
2218 if (!bs_vm_state) {
2219 error_report("No block device supports snapshots");
2220 return -ENOTSUP;
2221 }
79b3c12a 2222 aio_context = bdrv_get_aio_context(bs_vm_state);
f0aa7a8b
MDCF
2223
2224 /* Don't even try to load empty VM states */
79b3c12a 2225 aio_context_acquire(aio_context);
f0aa7a8b 2226 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
79b3c12a 2227 aio_context_release(aio_context);
f0aa7a8b
MDCF
2228 if (ret < 0) {
2229 return ret;
2230 } else if (sn.vm_state_size == 0) {
e11480db
KW
2231 error_report("This is a disk-only snapshot. Revert to it offline "
2232 "using qemu-img.");
f0aa7a8b
MDCF
2233 return -EINVAL;
2234 }
2235
a672b469 2236 /* Flush all IO requests so they don't interfere with the new state. */
922453bc 2237 bdrv_drain_all();
a672b469 2238
4c1cdbaa
DL
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;
a672b469
AL
2244 }
2245
a672b469 2246 /* restore the VM state */
f0aa7a8b 2247 f = qemu_fopen_bdrv(bs_vm_state, 0);
a672b469 2248 if (!f) {
1ecda02b 2249 error_report("Could not open VM state file");
05f2401e 2250 return -EINVAL;
a672b469 2251 }
f0aa7a8b 2252
5a8a49d7 2253 qemu_system_reset(VMRESET_SILENT);
bca7856a 2254 migration_incoming_state_new(f);
f0aa7a8b 2255
79b3c12a
DL
2256 aio_context_acquire(aio_context);
2257 ret = qemu_loadvm_state(f);
a672b469 2258 qemu_fclose(f);
79b3c12a
DL
2259 aio_context_release(aio_context);
2260
bca7856a 2261 migration_incoming_state_destroy();
a672b469 2262 if (ret < 0) {
1ecda02b 2263 error_report("Error %d while loading VM state", ret);
05f2401e 2264 return ret;
a672b469 2265 }
f0aa7a8b 2266
05f2401e 2267 return 0;
7b630349
JQ
2268}
2269
3e5a50d6 2270void hmp_delvm(Monitor *mon, const QDict *qdict)
a672b469 2271{
af957387 2272 BlockDriverState *bs;
ba2b2288 2273 Error *err;
d54908a5 2274 const char *name = qdict_get_str(qdict, "name");
a672b469 2275
9b00ea37 2276 if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
d410fe14
MA
2277 error_reportf_err(err,
2278 "Error while deleting snapshot on device '%s': ",
2279 bdrv_get_device_name(bs));
a672b469
AL
2280 }
2281}
2282
1ce6be24 2283void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
a672b469
AL
2284{
2285 BlockDriverState *bs, *bs1;
0c204cc8 2286 BdrvNextIterator it1;
723ccda1 2287 QEMUSnapshotInfo *sn_tab, *sn;
0c204cc8 2288 bool no_snapshot = true;
723ccda1 2289 int nb_sns, i;
f9209915 2290 int total;
0c204cc8 2291 int *global_snapshots;
79b3c12a 2292 AioContext *aio_context;
a672b469 2293
0c204cc8
LM
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
7cb14481 2311 bs = bdrv_all_find_vmstate_bs();
a672b469 2312 if (!bs) {
376253ec 2313 monitor_printf(mon, "No available block device supports snapshots\n");
a672b469
AL
2314 return;
2315 }
79b3c12a 2316 aio_context = bdrv_get_aio_context(bs);
a672b469 2317
79b3c12a 2318 aio_context_acquire(aio_context);
a672b469 2319 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
79b3c12a
DL
2320 aio_context_release(aio_context);
2321
a672b469 2322 if (nb_sns < 0) {
376253ec 2323 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
a672b469
AL
2324 return;
2325 }
f9209915 2326
0c204cc8
LM
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) {
f9209915
MDCF
2355 monitor_printf(mon, "There is no snapshot available.\n");
2356 return;
2357 }
2358
0c204cc8 2359 global_snapshots = g_new0(int, nb_sns);
f9209915
MDCF
2360 total = 0;
2361 for (i = 0; i < nb_sns; i++) {
0c204cc8 2362 SnapshotEntry *next_sn;
3a1ee711 2363 if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
0c204cc8 2364 global_snapshots[total] = i;
f9209915 2365 total++;
0c204cc8
LM
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 }
f9209915 2376 }
a672b469 2377 }
f9209915 2378
0c204cc8
LM
2379 monitor_printf(mon, "List of snapshots present on all disks:\n");
2380
f9209915 2381 if (total > 0) {
5b917044
WX
2382 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
2383 monitor_printf(mon, "\n");
f9209915 2384 for (i = 0; i < total; i++) {
0c204cc8 2385 sn = &sn_tab[global_snapshots[i]];
3a1ee711
LM
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), "--");
5b917044
WX
2390 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
2391 monitor_printf(mon, "\n");
f9209915
MDCF
2392 }
2393 } else {
0c204cc8 2394 monitor_printf(mon, "None\n");
f9209915
MDCF
2395 }
2396
0c204cc8
LM
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 }
7267c094 2421 g_free(sn_tab);
0c204cc8 2422 g_free(global_snapshots);
f9209915 2423
a672b469 2424}
c5705a77
AK
2425
2426void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2427{
fa53a0e5 2428 qemu_ram_set_idstr(mr->ram_block,
c5705a77
AK
2429 memory_region_name(mr), dev);
2430}
2431
2432void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2433{
fa53a0e5 2434 qemu_ram_unset_idstr(mr->ram_block);
c5705a77
AK
2435}
2436
2437void vmstate_register_ram_global(MemoryRegion *mr)
2438{
2439 vmstate_register_ram(mr, NULL);
2440}
This page took 1.146354 seconds and 4 git commands to generate.