blob: ed9608c42487354e2f46ae4ff2b51928ef3e0af0 [file] [log] [blame]
aliguoria672b462008-11-11 21:33:36 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
aliguoria672b462008-11-11 21:33:36 +000024
blueswir1d40cdb12009-03-07 16:52:02 +000025#include "config-host.h"
blueswir1511d2b12009-03-07 15:32:56 +000026#include "qemu-common.h"
Amit Shahabfd9ce2014-06-20 18:56:08 +053027#include "hw/boards.h"
blueswir1511d2b12009-03-07 15:32:56 +000028#include "hw/hw.h"
Alex Williamson7685ee62010-06-25 11:09:14 -060029#include "hw/qdev.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020030#include "net/net.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010031#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010032#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/timer.h"
blueswir1511d2b12009-03-07 15:32:56 +000034#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010035#include "migration/migration.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/sockets.h"
37#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/cpus.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010039#include "exec/memory.h"
Stefano Stabellinia7ae8352012-01-25 12:24:51 +000040#include "qmp-commands.h"
Juan Quintela517a13c2012-05-21 23:46:44 +020041#include "trace.h"
Orit Wasserman28085f72013-03-22 16:47:58 +020042#include "qemu/iov.h"
Wenchao Xiade08c602013-05-25 11:09:43 +080043#include "block/snapshot.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080044#include "block/qapi.h"
blueswir1511d2b12009-03-07 15:32:56 +000045
aliguoria672b462008-11-11 21:33:36 +000046
Nolan18995b92009-10-15 16:53:55 -070047#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040048#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070049#endif
50#define ARP_HTYPE_ETH 0x0001
51#define ARP_PTYPE_IP 0x0800
52#define ARP_OP_REQUEST_REV 0x3
53
54static int announce_self_create(uint8_t *buf,
Eduardo Habkost5cecf412013-11-28 12:01:12 -020055 uint8_t *mac_addr)
aliguoria672b462008-11-11 21:33:36 +000056{
Nolan18995b92009-10-15 16:53:55 -070057 /* Ethernet header. */
58 memset(buf, 0xff, 6); /* destination MAC addr */
59 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
60 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +000061
Nolan18995b92009-10-15 16:53:55 -070062 /* RARP header. */
63 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
64 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
65 *(buf + 18) = 6; /* hardware addr length (ethernet) */
66 *(buf + 19) = 4; /* protocol addr length (IPv4) */
67 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
68 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
69 memset(buf + 28, 0x00, 4); /* source protocol addr */
70 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
71 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +000072
Nolan18995b92009-10-15 16:53:55 -070073 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
74 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +000075
Nolan18995b92009-10-15 16:53:55 -070076 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +000077}
78
Mark McLoughlinf401ca22009-11-25 18:49:32 +000079static void qemu_announce_self_iter(NICState *nic, void *opaque)
80{
81 uint8_t buf[60];
82 int len;
83
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +110084 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
Mark McLoughlinf401ca22009-11-25 18:49:32 +000085 len = announce_self_create(buf, nic->conf->macaddr.a);
86
Jason Wangb356f762013-01-30 19:12:22 +080087 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
Mark McLoughlinf401ca22009-11-25 18:49:32 +000088}
89
90
Gleb Natapoved8b3302009-05-21 17:17:44 +030091static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +000092{
Gleb Natapoved8b3302009-05-21 17:17:44 +030093 static int count = SELF_ANNOUNCE_ROUNDS;
94 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +000095
Mark McLoughlinf401ca22009-11-25 18:49:32 +000096 qemu_foreach_nic(qemu_announce_self_iter, NULL);
97
Nolan18995b92009-10-15 16:53:55 -070098 if (--count) {
99 /* delay 50ms, 150ms, 250ms, ... */
Alex Blighbc72ad62013-08-21 16:03:08 +0100100 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
Jason Wang508e1182014-05-20 14:01:43 +0800101 self_announce_delay(count));
Gleb Natapoved8b3302009-05-21 17:17:44 +0300102 } else {
Eduardo Habkost5cecf412013-11-28 12:01:12 -0200103 timer_del(timer);
104 timer_free(timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300105 }
106}
107
108void qemu_announce_self(void)
109{
Eduardo Habkost5cecf412013-11-28 12:01:12 -0200110 static QEMUTimer *timer;
111 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
112 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000113}
114
115/***********************************************************/
116/* savevm/loadvm support */
117
Kevin Wolf05fcc842013-04-05 21:27:54 +0200118static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
119 int64_t pos)
120{
121 int ret;
122 QEMUIOVector qiov;
123
124 qemu_iovec_init_external(&qiov, iov, iovcnt);
125 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
126 if (ret < 0) {
127 return ret;
128 }
129
130 return qiov.size;
131}
132
aliguori178e08a2009-04-05 19:10:55 +0000133static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000134 int64_t pos, int size)
135{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200136 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000137 return size;
138}
139
aliguori178e08a2009-04-05 19:10:55 +0000140static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000141{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200142 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000143}
144
145static int bdrv_fclose(void *opaque)
146{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200147 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000148}
149
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200150static const QEMUFileOps bdrv_read_ops = {
151 .get_buffer = block_get_buffer,
152 .close = bdrv_fclose
153};
154
155static const QEMUFileOps bdrv_write_ops = {
Kevin Wolf05fcc842013-04-05 21:27:54 +0200156 .put_buffer = block_put_buffer,
157 .writev_buffer = block_writev_buffer,
158 .close = bdrv_fclose
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200159};
160
Christoph Hellwig45566e92009-07-10 23:11:57 +0200161static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000162{
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200163 if (is_writable) {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200164 return qemu_fopen_ops(bs, &bdrv_write_ops);
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200165 }
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200166 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000167}
168
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200169
Eduardo Habkostbb1a6d82013-11-29 12:26:02 -0200170/* QEMUFile timer support.
171 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
172 */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200173
Alex Bligh40daca52013-08-21 16:03:02 +0100174void timer_put(QEMUFile *f, QEMUTimer *ts)
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200175{
176 uint64_t expire_time;
177
Alex Blighe93379b2013-08-21 16:02:39 +0100178 expire_time = timer_expire_time_ns(ts);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200179 qemu_put_be64(f, expire_time);
180}
181
Alex Bligh40daca52013-08-21 16:03:02 +0100182void timer_get(QEMUFile *f, QEMUTimer *ts)
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200183{
184 uint64_t expire_time;
185
186 expire_time = qemu_get_be64(f);
187 if (expire_time != -1) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100188 timer_mod_ns(ts, expire_time);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200189 } else {
Alex Blighbc72ad62013-08-21 16:03:08 +0100190 timer_del(ts);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200191 }
192}
193
194
Eduardo Habkostbb1a6d82013-11-29 12:26:02 -0200195/* VMState timer support.
196 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
197 */
Juan Quinteladde04632009-08-20 19:42:26 +0200198
199static int get_timer(QEMUFile *f, void *pv, size_t size)
200{
201 QEMUTimer *v = pv;
Alex Bligh40daca52013-08-21 16:03:02 +0100202 timer_get(f, v);
Juan Quinteladde04632009-08-20 19:42:26 +0200203 return 0;
204}
205
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200206static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +0200207{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200208 QEMUTimer *v = pv;
Alex Bligh40daca52013-08-21 16:03:02 +0100209 timer_put(f, v);
Juan Quinteladde04632009-08-20 19:42:26 +0200210}
211
212const VMStateInfo vmstate_info_timer = {
213 .name = "timer",
214 .get = get_timer,
215 .put = put_timer,
216};
217
Peter Maydell08e99e22012-10-30 07:45:12 +0000218
Alex Williamson7685ee62010-06-25 11:09:14 -0600219typedef struct CompatEntry {
220 char idstr[256];
221 int instance_id;
222} CompatEntry;
223
aliguoria672b462008-11-11 21:33:36 +0000224typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000225 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +0000226 char idstr[256];
227 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200228 int alias_id;
aliguoria672b462008-11-11 21:33:36 +0000229 int version_id;
230 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +0200231 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200232 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +0000233 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -0600234 CompatEntry *compat;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +0000235 int is_ram;
aliguoria672b462008-11-11 21:33:36 +0000236} SaveStateEntry;
237
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200238
Blue Swirl72cf2d42009-09-12 07:36:22 +0000239static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
240 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200241static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +0000242
Amit Shahabfd9ce2014-06-20 18:56:08 +0530243static void dump_vmstate_vmsd(FILE *out_file,
244 const VMStateDescription *vmsd, int indent,
245 bool is_subsection);
246
247static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
248 int indent)
249{
250 fprintf(out_file, "%*s{\n", indent, "");
251 indent += 2;
252 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
253 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
254 field->version_id);
255 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
256 field->field_exists ? "true" : "false");
257 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
258 if (field->vmsd != NULL) {
259 fprintf(out_file, ",\n");
260 dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
261 }
262 fprintf(out_file, "\n%*s}", indent - 2, "");
263}
264
265static void dump_vmstate_vmss(FILE *out_file,
266 const VMStateSubsection *subsection,
267 int indent)
268{
269 if (subsection->vmsd != NULL) {
270 dump_vmstate_vmsd(out_file, subsection->vmsd, indent, true);
271 }
272}
273
274static void dump_vmstate_vmsd(FILE *out_file,
275 const VMStateDescription *vmsd, int indent,
276 bool is_subsection)
277{
278 if (is_subsection) {
279 fprintf(out_file, "%*s{\n", indent, "");
280 } else {
281 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
282 }
283 indent += 2;
284 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
285 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
286 vmsd->version_id);
287 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
288 vmsd->minimum_version_id);
289 if (vmsd->fields != NULL) {
290 const VMStateField *field = vmsd->fields;
291 bool first;
292
293 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
294 first = true;
295 while (field->name != NULL) {
296 if (field->flags & VMS_MUST_EXIST) {
297 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
298 field++;
299 continue;
300 }
301 if (!first) {
302 fprintf(out_file, ",\n");
303 }
304 dump_vmstate_vmsf(out_file, field, indent + 2);
305 field++;
306 first = false;
307 }
308 fprintf(out_file, "\n%*s]", indent, "");
309 }
310 if (vmsd->subsections != NULL) {
311 const VMStateSubsection *subsection = vmsd->subsections;
312 bool first;
313
314 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
315 first = true;
316 while (subsection->vmsd != NULL) {
317 if (!first) {
318 fprintf(out_file, ",\n");
319 }
320 dump_vmstate_vmss(out_file, subsection, indent + 2);
321 subsection++;
322 first = false;
323 }
324 fprintf(out_file, "\n%*s]", indent, "");
325 }
326 fprintf(out_file, "\n%*s}", indent - 2, "");
327}
328
329static void dump_machine_type(FILE *out_file)
330{
331 MachineClass *mc;
332
333 mc = MACHINE_GET_CLASS(current_machine);
334
335 fprintf(out_file, " \"vmschkmachine\": {\n");
336 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
337 fprintf(out_file, " },\n");
338}
339
340void dump_vmstate_json_to_file(FILE *out_file)
341{
342 GSList *list, *elt;
343 bool first;
344
345 fprintf(out_file, "{\n");
346 dump_machine_type(out_file);
347
348 first = true;
349 list = object_class_get_list(TYPE_DEVICE, true);
350 for (elt = list; elt; elt = elt->next) {
351 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
352 TYPE_DEVICE);
353 const char *name;
354 int indent = 2;
355
356 if (!dc->vmsd) {
357 continue;
358 }
359
360 if (!first) {
361 fprintf(out_file, ",\n");
362 }
363 name = object_class_get_name(OBJECT_CLASS(dc));
364 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
365 indent += 2;
366 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
367 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
368 dc->vmsd->version_id);
369 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
370 dc->vmsd->minimum_version_id);
371
372 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
373
374 fprintf(out_file, "\n%*s}", indent - 2, "");
375 first = false;
376 }
377 fprintf(out_file, "\n}\n");
378 fclose(out_file);
379}
380
Juan Quintela8718e992009-09-01 02:12:31 +0200381static int calculate_new_instance_id(const char *idstr)
382{
383 SaveStateEntry *se;
384 int instance_id = 0;
385
Blue Swirl72cf2d42009-09-12 07:36:22 +0000386 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +0200387 if (strcmp(idstr, se->idstr) == 0
388 && instance_id <= se->instance_id) {
389 instance_id = se->instance_id + 1;
390 }
391 }
392 return instance_id;
393}
394
Alex Williamson7685ee62010-06-25 11:09:14 -0600395static int calculate_compat_instance_id(const char *idstr)
396{
397 SaveStateEntry *se;
398 int instance_id = 0;
399
400 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200401 if (!se->compat) {
Alex Williamson7685ee62010-06-25 11:09:14 -0600402 continue;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200403 }
Alex Williamson7685ee62010-06-25 11:09:14 -0600404
405 if (strcmp(idstr, se->compat->idstr) == 0
406 && instance_id <= se->compat->instance_id) {
407 instance_id = se->compat->instance_id + 1;
408 }
409 }
410 return instance_id;
411}
412
aliguoria672b462008-11-11 21:33:36 +0000413/* TODO: Individual devices generally have very little idea about the rest
414 of the system, so instance_id should be removed/replaced.
415 Meanwhile pass -1 as instance_id if you do not already have a clearly
416 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -0600417int register_savevm_live(DeviceState *dev,
418 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +0000419 int instance_id,
420 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +0200421 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +0000422 void *opaque)
423{
Juan Quintela8718e992009-09-01 02:12:31 +0200424 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +0000425
Anthony Liguori7267c092011-08-20 22:09:37 -0500426 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +0000427 se->version_id = version_id;
428 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +0200429 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000430 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200431 se->vmsd = NULL;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +0000432 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +0200433 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +0000434 se->is_ram = 1;
435 }
aliguoria672b462008-11-11 21:33:36 +0000436
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600437 if (dev) {
438 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -0600439 if (id) {
440 pstrcpy(se->idstr, sizeof(se->idstr), id);
441 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -0500442 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -0600443
Anthony Liguori7267c092011-08-20 22:09:37 -0500444 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -0600445 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
446 se->compat->instance_id = instance_id == -1 ?
447 calculate_compat_instance_id(idstr) : instance_id;
448 instance_id = -1;
449 }
450 }
451 pstrcat(se->idstr, sizeof(se->idstr), idstr);
452
Juan Quintela8718e992009-09-01 02:12:31 +0200453 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -0600454 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +0200455 } else {
456 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +0000457 }
Alex Williamson7685ee62010-06-25 11:09:14 -0600458 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +0200459 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000460 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +0000461 return 0;
462}
463
Alex Williamson0be71e32010-06-25 11:09:07 -0600464int register_savevm(DeviceState *dev,
465 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +0000466 int instance_id,
467 int version_id,
468 SaveStateHandler *save_state,
469 LoadStateHandler *load_state,
470 void *opaque)
471{
Juan Quintela7908c782012-06-26 18:46:10 +0200472 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
473 ops->save_state = save_state;
474 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -0600475 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +0200476 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +0000477}
478
Alex Williamson0be71e32010-06-25 11:09:07 -0600479void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +0000480{
Juan Quintela8718e992009-09-01 02:12:31 +0200481 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -0600482 char id[256] = "";
483
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600484 if (dev) {
485 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -0600486 if (path) {
487 pstrcpy(id, sizeof(id), path);
488 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -0500489 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -0600490 }
491 }
492 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +0000493
Blue Swirl72cf2d42009-09-12 07:36:22 +0000494 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -0600495 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000496 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -0600497 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500498 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -0600499 }
Juan Quintela22ea40f2012-06-26 17:19:10 +0200500 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -0500501 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +0000502 }
aliguori41bd13a2009-04-17 17:10:59 +0000503 }
504}
505
Alex Williamson0be71e32010-06-25 11:09:07 -0600506int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200507 const VMStateDescription *vmsd,
508 void *opaque, int alias_id,
509 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200510{
Juan Quintela8718e992009-09-01 02:12:31 +0200511 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200512
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200513 /* If this triggers, alias support can be dropped for the vmsd. */
514 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
515
Anthony Liguori7267c092011-08-20 22:09:37 -0500516 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200517 se->version_id = vmsd->version_id;
518 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200519 se->opaque = opaque;
520 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200521 se->alias_id = alias_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200522
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600523 if (dev) {
524 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -0600525 if (id) {
526 pstrcpy(se->idstr, sizeof(se->idstr), id);
527 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -0500528 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -0600529
Anthony Liguori7267c092011-08-20 22:09:37 -0500530 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -0600531 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
532 se->compat->instance_id = instance_id == -1 ?
533 calculate_compat_instance_id(vmsd->name) : instance_id;
534 instance_id = -1;
535 }
536 }
537 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
538
Juan Quintela8718e992009-09-01 02:12:31 +0200539 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -0600540 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +0200541 } else {
542 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200543 }
Alex Williamson7685ee62010-06-25 11:09:14 -0600544 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +0200545 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000546 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200547 return 0;
548}
549
Alex Williamson0be71e32010-06-25 11:09:07 -0600550void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
551 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200552{
Juan Quintela1eb75382009-09-10 03:04:29 +0200553 SaveStateEntry *se, *new_se;
554
Blue Swirl72cf2d42009-09-12 07:36:22 +0000555 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +0200556 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000557 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -0600558 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500559 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -0600560 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500561 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +0200562 }
563 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200564}
565
Juan Quintela4082be42009-08-20 19:42:24 +0200566static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
567{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100568 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200569 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +0200570 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200571 }
572 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +0200573}
574
Alexander Graf8118f092015-01-22 15:01:39 +0100575static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
576{
577 int64_t old_offset, size;
578
579 old_offset = qemu_ftell_fast(f);
580 se->ops->save_state(f, se->opaque);
581 size = qemu_ftell_fast(f) - old_offset;
582
583 if (vmdesc) {
584 json_prop_int(vmdesc, "size", size);
585 json_start_array(vmdesc, "fields");
586 json_start_object(vmdesc, NULL);
587 json_prop_str(vmdesc, "name", "data");
588 json_prop_int(vmdesc, "size", size);
589 json_prop_str(vmdesc, "type", "buffer");
590 json_end_object(vmdesc);
591 json_end_array(vmdesc);
592 }
593}
594
595static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
Juan Quintela4082be42009-08-20 19:42:24 +0200596{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100597 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
Alexander Graf8118f092015-01-22 15:01:39 +0100598 if (!se->vmsd) {
599 vmstate_save_old_style(f, se, vmdesc);
Alex Williamsondc912122011-01-11 14:39:43 -0700600 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200601 }
Alexander Graf8118f092015-01-22 15:01:39 +0100602 vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
Juan Quintela4082be42009-08-20 19:42:24 +0200603}
604
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200605bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -0700606{
607 SaveStateEntry *se;
608
609 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Marcelo Tosatti7d854c42014-05-14 16:30:08 -0300610 if (se->vmsd && se->vmsd->unmigratable) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400611 error_setg(errp, "State blocked by non-migratable device '%s'",
612 se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -0700613 return true;
614 }
615 }
616 return false;
617}
618
Paolo Bonzini47c8c172013-02-22 17:36:13 +0100619void qemu_savevm_state_begin(QEMUFile *f,
620 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +0000621{
622 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +0200623 int ret;
aliguoria672b462008-11-11 21:33:36 +0000624
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100625 trace_savevm_state_begin();
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200626 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +0200627 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200628 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300629 }
Juan Quintela22ea40f2012-06-26 17:19:10 +0200630 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200631 }
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200632
aliguoria672b462008-11-11 21:33:36 +0000633 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
634 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
635
Blue Swirl72cf2d42009-09-12 07:36:22 +0000636 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +0000637 int len;
638
Juan Quintelad1315aa2012-06-28 15:11:57 +0200639 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +0000640 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +0200641 }
Juan Quintela6bd68782012-06-27 10:59:15 +0200642 if (se->ops && se->ops->is_active) {
643 if (!se->ops->is_active(se->opaque)) {
644 continue;
645 }
646 }
aliguoria672b462008-11-11 21:33:36 +0000647 /* Section type */
648 qemu_put_byte(f, QEMU_VM_SECTION_START);
649 qemu_put_be32(f, se->section_id);
650
651 /* ID string */
652 len = strlen(se->idstr);
653 qemu_put_byte(f, len);
654 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
655
656 qemu_put_be32(f, se->instance_id);
657 qemu_put_be32(f, se->version_id);
658
Juan Quintelad1315aa2012-06-28 15:11:57 +0200659 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +0200660 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +0100661 qemu_file_set_error(f, ret);
662 break;
Juan Quintela29757252011-10-19 15:22:18 +0200663 }
aliguoria672b462008-11-11 21:33:36 +0000664 }
aliguoria672b462008-11-11 21:33:36 +0000665}
666
Juan Quintela39346382011-09-22 11:02:14 +0200667/*
Dong Xu Wang07f35072011-11-22 18:06:26 +0800668 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +0200669 * negative: there was one error, and we have -errno.
670 * 0 : We haven't finished, caller have to go again
671 * 1 : We have finished, we can go to complete phase
672 */
Luiz Capitulino539de122011-12-05 14:06:56 -0200673int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000674{
675 SaveStateEntry *se;
676 int ret = 1;
677
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100678 trace_savevm_state_iterate();
Blue Swirl72cf2d42009-09-12 07:36:22 +0000679 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +0200680 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +0000681 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +0200682 }
Juan Quintela6bd68782012-06-27 10:59:15 +0200683 if (se->ops && se->ops->is_active) {
684 if (!se->ops->is_active(se->opaque)) {
685 continue;
686 }
687 }
Juan Quintelaaac844e2012-05-22 00:38:26 +0200688 if (qemu_file_rate_limit(f)) {
689 return 0;
690 }
Alexey Kardashevskiy464400f2014-03-07 01:33:37 +0530691 trace_savevm_section_start(se->idstr, se->section_id);
aliguoria672b462008-11-11 21:33:36 +0000692 /* Section type */
693 qemu_put_byte(f, QEMU_VM_SECTION_PART);
694 qemu_put_be32(f, se->section_id);
695
Juan Quintela16310a32012-06-28 15:31:37 +0200696 ret = se->ops->save_live_iterate(f, se->opaque);
Dr. David Alan Gilberta5df2a02015-01-21 10:14:48 +0000697 trace_savevm_section_end(se->idstr, se->section_id, ret);
Juan Quintela517a13c2012-05-21 23:46:44 +0200698
Paolo Bonzini47c8c172013-02-22 17:36:13 +0100699 if (ret < 0) {
700 qemu_file_set_error(f, ret);
701 }
Juan Quintela29757252011-10-19 15:22:18 +0200702 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +0100703 /* Do not proceed to the next vmstate before this one reported
704 completion of the current stage. This serializes the migration
705 and reduces the probability that a faster changing state is
706 synchronized over and over again. */
707 break;
708 }
aliguoria672b462008-11-11 21:33:36 +0000709 }
Juan Quintela39346382011-09-22 11:02:14 +0200710 return ret;
aliguoria672b462008-11-11 21:33:36 +0000711}
712
Paolo Bonzini47c8c172013-02-22 17:36:13 +0100713void qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000714{
Alexander Graf8118f092015-01-22 15:01:39 +0100715 QJSON *vmdesc;
716 int vmdesc_len;
aliguoria672b462008-11-11 21:33:36 +0000717 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +0200718 int ret;
aliguoria672b462008-11-11 21:33:36 +0000719
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100720 trace_savevm_state_complete();
721
Jan Kiszkaea375f92010-03-01 19:10:30 +0100722 cpu_synchronize_all_states();
723
Blue Swirl72cf2d42009-09-12 07:36:22 +0000724 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +0200725 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +0000726 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +0200727 }
Juan Quintela6bd68782012-06-27 10:59:15 +0200728 if (se->ops && se->ops->is_active) {
729 if (!se->ops->is_active(se->opaque)) {
730 continue;
731 }
732 }
Alexey Kardashevskiy464400f2014-03-07 01:33:37 +0530733 trace_savevm_section_start(se->idstr, se->section_id);
aliguoria672b462008-11-11 21:33:36 +0000734 /* Section type */
735 qemu_put_byte(f, QEMU_VM_SECTION_END);
736 qemu_put_be32(f, se->section_id);
737
Juan Quintela16310a32012-06-28 15:31:37 +0200738 ret = se->ops->save_live_complete(f, se->opaque);
Dr. David Alan Gilberta5df2a02015-01-21 10:14:48 +0000739 trace_savevm_section_end(se->idstr, se->section_id, ret);
Juan Quintela29757252011-10-19 15:22:18 +0200740 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +0100741 qemu_file_set_error(f, ret);
742 return;
Juan Quintela29757252011-10-19 15:22:18 +0200743 }
aliguoria672b462008-11-11 21:33:36 +0000744 }
745
Alexander Graf8118f092015-01-22 15:01:39 +0100746 vmdesc = qjson_new();
747 json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE);
748 json_start_array(vmdesc, "devices");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000749 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +0000750 int len;
751
Juan Quintela22ea40f2012-06-26 17:19:10 +0200752 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Eduardo Habkost5cecf412013-11-28 12:01:12 -0200753 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +0200754 }
Alexey Kardashevskiy464400f2014-03-07 01:33:37 +0530755 trace_savevm_section_start(se->idstr, se->section_id);
Alexander Graf8118f092015-01-22 15:01:39 +0100756
757 json_start_object(vmdesc, NULL);
758 json_prop_str(vmdesc, "name", se->idstr);
759 json_prop_int(vmdesc, "instance_id", se->instance_id);
760
aliguoria672b462008-11-11 21:33:36 +0000761 /* Section type */
762 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
763 qemu_put_be32(f, se->section_id);
764
765 /* ID string */
766 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
Alexander Graf8118f092015-01-22 15:01:39 +0100773 vmstate_save(f, se, vmdesc);
774
775 json_end_object(vmdesc);
Dr. David Alan Gilberta5df2a02015-01-21 10:14:48 +0000776 trace_savevm_section_end(se->idstr, se->section_id, 0);
aliguoria672b462008-11-11 21:33:36 +0000777 }
778
779 qemu_put_byte(f, QEMU_VM_EOF);
Alexander Graf8118f092015-01-22 15:01:39 +0100780
781 json_end_array(vmdesc);
782 qjson_finish(vmdesc);
783 vmdesc_len = strlen(qjson_get_str(vmdesc));
784
785 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
786 qemu_put_be32(f, vmdesc_len);
787 qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
788 object_unref(OBJECT(vmdesc));
789
Paolo Bonziniedaae612013-02-22 17:36:29 +0100790 qemu_fflush(f);
aliguoria672b462008-11-11 21:33:36 +0000791}
792
Juan Quintelae4ed1542012-09-21 11:18:18 +0200793uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
794{
795 SaveStateEntry *se;
796 uint64_t ret = 0;
797
798 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
799 if (!se->ops || !se->ops->save_live_pending) {
800 continue;
801 }
802 if (se->ops && se->ops->is_active) {
803 if (!se->ops->is_active(se->opaque)) {
804 continue;
805 }
806 }
807 ret += se->ops->save_live_pending(f, se->opaque, max_size);
808 }
809 return ret;
810}
811
Juan Quintela65227732013-01-14 14:14:42 +0100812void qemu_savevm_state_cancel(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100813{
814 SaveStateEntry *se;
815
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100816 trace_savevm_state_cancel();
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100817 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200818 if (se->ops && se->ops->cancel) {
819 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +0100820 }
821 }
822}
823
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200824static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000825{
aliguoria672b462008-11-11 21:33:36 +0000826 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300827 MigrationParams params = {
828 .blk = 0,
829 .shared = 0
830 };
aliguoria672b462008-11-11 21:33:36 +0000831
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200832 if (qemu_savevm_state_blocked(NULL)) {
Paolo Bonzini04943eb2013-02-22 17:36:10 +0100833 return -EINVAL;
Alex Williamsondc912122011-01-11 14:39:43 -0700834 }
835
Paolo Bonzini9b095032013-02-22 17:36:28 +0100836 qemu_mutex_unlock_iothread();
Paolo Bonzini47c8c172013-02-22 17:36:13 +0100837 qemu_savevm_state_begin(f, &params);
Paolo Bonzini9b095032013-02-22 17:36:28 +0100838 qemu_mutex_lock_iothread();
839
Paolo Bonzini47c8c172013-02-22 17:36:13 +0100840 while (qemu_file_get_error(f) == 0) {
841 if (qemu_savevm_state_iterate(f) > 0) {
842 break;
843 }
844 }
aliguoria672b462008-11-11 21:33:36 +0000845
Paolo Bonzini47c8c172013-02-22 17:36:13 +0100846 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +0200847 if (ret == 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +0100848 qemu_savevm_state_complete(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +0200849 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +0200850 }
Paolo Bonzini04943eb2013-02-22 17:36:10 +0100851 if (ret != 0) {
852 qemu_savevm_state_cancel();
853 }
aliguoria672b462008-11-11 21:33:36 +0000854 return ret;
855}
856
Stefano Stabellinia7ae8352012-01-25 12:24:51 +0000857static int qemu_save_device_state(QEMUFile *f)
858{
859 SaveStateEntry *se;
860
861 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
862 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
863
864 cpu_synchronize_all_states();
865
866 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
867 int len;
868
869 if (se->is_ram) {
870 continue;
871 }
Juan Quintela22ea40f2012-06-26 17:19:10 +0200872 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +0000873 continue;
874 }
875
876 /* Section type */
877 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
878 qemu_put_be32(f, se->section_id);
879
880 /* ID string */
881 len = strlen(se->idstr);
882 qemu_put_byte(f, len);
883 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
884
885 qemu_put_be32(f, se->instance_id);
886 qemu_put_be32(f, se->version_id);
887
Alexander Graf8118f092015-01-22 15:01:39 +0100888 vmstate_save(f, se, NULL);
Stefano Stabellinia7ae8352012-01-25 12:24:51 +0000889 }
890
891 qemu_put_byte(f, QEMU_VM_EOF);
892
893 return qemu_file_get_error(f);
894}
895
aliguoria672b462008-11-11 21:33:36 +0000896static SaveStateEntry *find_se(const char *idstr, int instance_id)
897{
898 SaveStateEntry *se;
899
Blue Swirl72cf2d42009-09-12 07:36:22 +0000900 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +0000901 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200902 (instance_id == se->instance_id ||
903 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +0000904 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -0600905 /* Migrating from an older version? */
906 if (strstr(se->idstr, idstr) && se->compat) {
907 if (!strcmp(se->compat->idstr, idstr) &&
908 (instance_id == se->compat->instance_id ||
909 instance_id == se->alias_id))
910 return se;
911 }
aliguoria672b462008-11-11 21:33:36 +0000912 }
913 return NULL;
914}
915
916typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000917 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +0000918 SaveStateEntry *se;
919 int section_id;
920 int version_id;
aliguoria672b462008-11-11 21:33:36 +0000921} LoadStateEntry;
922
aliguoria672b462008-11-11 21:33:36 +0000923int qemu_loadvm_state(QEMUFile *f)
924{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000925 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
926 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +0200927 LoadStateEntry *le, *new_le;
Dr. David Alan Gilbert0457d072015-01-21 10:14:49 +0000928 Error *local_err = NULL;
aliguoria672b462008-11-11 21:33:36 +0000929 uint8_t section_type;
930 unsigned int v;
931 int ret;
932
Dr. David Alan Gilbert0457d072015-01-21 10:14:49 +0000933 if (qemu_savevm_state_blocked(&local_err)) {
934 error_report("%s", error_get_pretty(local_err));
935 error_free(local_err);
Alex Williamsondc912122011-01-11 14:39:43 -0700936 return -EINVAL;
937 }
938
aliguoria672b462008-11-11 21:33:36 +0000939 v = qemu_get_be32(f);
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200940 if (v != QEMU_VM_FILE_MAGIC) {
Dr. David Alan Gilbert0457d072015-01-21 10:14:49 +0000941 error_report("Not a migration stream");
aliguoria672b462008-11-11 21:33:36 +0000942 return -EINVAL;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200943 }
aliguoria672b462008-11-11 21:33:36 +0000944
945 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +0200946 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
Dr. David Alan Gilbert6a64b642015-01-21 10:14:47 +0000947 error_report("SaveVM v2 format is obsolete and don't work anymore");
Juan Quintelabbfe1402009-09-10 03:04:24 +0200948 return -ENOTSUP;
949 }
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200950 if (v != QEMU_VM_FILE_VERSION) {
Dr. David Alan Gilbert0457d072015-01-21 10:14:49 +0000951 error_report("Unsupported migration stream version");
aliguoria672b462008-11-11 21:33:36 +0000952 return -ENOTSUP;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200953 }
aliguoria672b462008-11-11 21:33:36 +0000954
955 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
956 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +0000957 SaveStateEntry *se;
958 char idstr[257];
959 int len;
960
Dr. David Alan Gilberta5df2a02015-01-21 10:14:48 +0000961 trace_qemu_loadvm_state_section(section_type);
aliguoria672b462008-11-11 21:33:36 +0000962 switch (section_type) {
963 case QEMU_VM_SECTION_START:
964 case QEMU_VM_SECTION_FULL:
965 /* Read section start */
966 section_id = qemu_get_be32(f);
967 len = qemu_get_byte(f);
968 qemu_get_buffer(f, (uint8_t *)idstr, len);
969 idstr[len] = 0;
970 instance_id = qemu_get_be32(f);
971 version_id = qemu_get_be32(f);
972
Dr. David Alan Gilberta5df2a02015-01-21 10:14:48 +0000973 trace_qemu_loadvm_state_section_startfull(section_id, idstr,
974 instance_id, version_id);
aliguoria672b462008-11-11 21:33:36 +0000975 /* Find savevm section */
976 se = find_se(idstr, instance_id);
977 if (se == NULL) {
Dr. David Alan Gilbert6a64b642015-01-21 10:14:47 +0000978 error_report("Unknown savevm section or instance '%s' %d",
979 idstr, instance_id);
aliguoria672b462008-11-11 21:33:36 +0000980 ret = -EINVAL;
981 goto out;
982 }
983
984 /* Validate version */
985 if (version_id > se->version_id) {
Dr. David Alan Gilbert6a64b642015-01-21 10:14:47 +0000986 error_report("savevm: unsupported version %d for '%s' v%d",
987 version_id, idstr, se->version_id);
aliguoria672b462008-11-11 21:33:36 +0000988 ret = -EINVAL;
989 goto out;
990 }
991
992 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -0500993 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +0000994
995 le->se = se;
996 le->section_id = section_id;
997 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000998 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +0000999
Juan Quintela4082be42009-08-20 19:42:24 +02001000 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001001 if (ret < 0) {
Dr. David Alan Gilbert6a64b642015-01-21 10:14:47 +00001002 error_report("error while loading state for instance 0x%x of"
1003 " device '%s'", instance_id, idstr);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001004 goto out;
1005 }
aliguoria672b462008-11-11 21:33:36 +00001006 break;
1007 case QEMU_VM_SECTION_PART:
1008 case QEMU_VM_SECTION_END:
1009 section_id = qemu_get_be32(f);
1010
Dr. David Alan Gilberta5df2a02015-01-21 10:14:48 +00001011 trace_qemu_loadvm_state_section_partend(section_id);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001012 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001013 if (le->section_id == section_id) {
1014 break;
1015 }
1016 }
aliguoria672b462008-11-11 21:33:36 +00001017 if (le == NULL) {
Dr. David Alan Gilbert6a64b642015-01-21 10:14:47 +00001018 error_report("Unknown savevm section %d", section_id);
aliguoria672b462008-11-11 21:33:36 +00001019 ret = -EINVAL;
1020 goto out;
1021 }
1022
Juan Quintela4082be42009-08-20 19:42:24 +02001023 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001024 if (ret < 0) {
Dr. David Alan Gilbert6a64b642015-01-21 10:14:47 +00001025 error_report("error while loading state section id %d(%s)",
1026 section_id, le->se->idstr);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001027 goto out;
1028 }
aliguoria672b462008-11-11 21:33:36 +00001029 break;
1030 default:
Dr. David Alan Gilbert6a64b642015-01-21 10:14:47 +00001031 error_report("Unknown savevm section type %d", section_type);
aliguoria672b462008-11-11 21:33:36 +00001032 ret = -EINVAL;
1033 goto out;
1034 }
1035 }
1036
Jan Kiszkaea375f92010-03-01 19:10:30 +01001037 cpu_synchronize_all_post_init();
1038
aliguoria672b462008-11-11 21:33:36 +00001039 ret = 0;
1040
1041out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00001042 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1043 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001044 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00001045 }
1046
Juan Quintela42802d42011-10-05 01:14:46 +02001047 if (ret == 0) {
1048 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001049 }
aliguoria672b462008-11-11 21:33:36 +00001050
1051 return ret;
1052}
1053
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08001054static BlockDriverState *find_vmstate_bs(void)
1055{
1056 BlockDriverState *bs = NULL;
1057 while ((bs = bdrv_next(bs))) {
1058 if (bdrv_can_snapshot(bs)) {
1059 return bs;
1060 }
1061 }
1062 return NULL;
1063}
1064
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001065/*
1066 * Deletes snapshots of a given name in all opened images.
1067 */
1068static int del_existing_snapshots(Monitor *mon, const char *name)
1069{
1070 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001071 QEMUSnapshotInfo sn1, *snapshot = &sn1;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08001072 Error *err = NULL;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001073
Markus Armbrusterdbc13592010-06-02 18:55:21 +02001074 bs = NULL;
1075 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001076 if (bdrv_can_snapshot(bs) &&
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001077 bdrv_snapshot_find(bs, snapshot, name) >= 0) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08001078 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001079 if (err) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001080 monitor_printf(mon,
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08001081 "Error while deleting snapshot on device '%s':"
1082 " %s\n",
1083 bdrv_get_device_name(bs),
1084 error_get_pretty(err));
1085 error_free(err);
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001086 return -1;
1087 }
1088 }
1089 }
1090
1091 return 0;
1092}
1093
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001094void hmp_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001095{
1096 BlockDriverState *bs, *bs1;
1097 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001098 int ret;
aliguoria672b462008-11-11 21:33:36 +00001099 QEMUFile *f;
1100 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01001101 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01001102 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03001103 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001104 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00001105
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001106 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02001107 bs = NULL;
1108 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001109
Markus Armbruster07b70bf2011-08-03 15:08:11 +02001110 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001111 continue;
1112 }
1113
1114 if (!bdrv_can_snapshot(bs)) {
1115 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
1116 bdrv_get_device_name(bs));
1117 return;
1118 }
1119 }
1120
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08001121 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00001122 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001123 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001124 return;
1125 }
aliguoria672b462008-11-11 21:33:36 +00001126
Luiz Capitulino13548692011-07-29 15:36:43 -03001127 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001128 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00001129
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001130 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00001131
1132 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01001133 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00001134 sn->date_sec = tv.tv_sec;
1135 sn->date_nsec = tv.tv_usec * 1000;
Alex Blighbc72ad62013-08-21 16:03:08 +01001136 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
aliguoria672b462008-11-11 21:33:36 +00001137
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03001138 if (name) {
1139 ret = bdrv_snapshot_find(bs, old_sn, name);
1140 if (ret >= 0) {
1141 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1142 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1143 } else {
1144 pstrcpy(sn->name, sizeof(sn->name), name);
1145 }
1146 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00001147 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1148 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03001149 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03001150 }
1151
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001152 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02001153 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001154 goto the_end;
1155 }
1156
aliguoria672b462008-11-11 21:33:36 +00001157 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02001158 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00001159 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001160 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00001161 goto the_end;
1162 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001163 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00001164 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00001165 qemu_fclose(f);
1166 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001167 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00001168 goto the_end;
1169 }
1170
1171 /* create the snapshots */
1172
Markus Armbrusterdbc13592010-06-02 18:55:21 +02001173 bs1 = NULL;
1174 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001175 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00001176 /* Write VM state size only to the image that contains the state */
1177 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00001178 ret = bdrv_snapshot_create(bs1, sn);
1179 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001180 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1181 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001182 }
1183 }
1184 }
1185
1186 the_end:
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001187 if (saved_vm_running) {
aliguoria672b462008-11-11 21:33:36 +00001188 vm_start();
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001189 }
aliguoria672b462008-11-11 21:33:36 +00001190}
1191
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001192void qmp_xen_save_devices_state(const char *filename, Error **errp)
1193{
1194 QEMUFile *f;
1195 int saved_vm_running;
1196 int ret;
1197
1198 saved_vm_running = runstate_is_running();
1199 vm_stop(RUN_STATE_SAVE_VM);
1200
1201 f = qemu_fopen(filename, "wb");
1202 if (!f) {
Luiz Capitulino1befce92013-06-07 14:36:58 -04001203 error_setg_file_open(errp, errno, filename);
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001204 goto the_end;
1205 }
1206 ret = qemu_save_device_state(f);
1207 qemu_fclose(f);
1208 if (ret < 0) {
1209 error_set(errp, QERR_IO_ERROR);
1210 }
1211
1212 the_end:
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001213 if (saved_vm_running) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001214 vm_start();
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001215 }
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001216}
1217
Markus Armbruster03cd4652010-02-17 16:24:10 +01001218int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00001219{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001220 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00001221 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00001222 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001223 int ret;
aliguoria672b462008-11-11 21:33:36 +00001224
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08001225 bs_vm_state = find_vmstate_bs();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001226 if (!bs_vm_state) {
1227 error_report("No block device supports snapshots");
1228 return -ENOTSUP;
1229 }
1230
1231 /* Don't even try to load empty VM states */
1232 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
1233 if (ret < 0) {
1234 return ret;
1235 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01001236 error_report("This is a disk-only snapshot. Revert to it offline "
1237 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001238 return -EINVAL;
1239 }
1240
1241 /* Verify if there is any device that doesn't support snapshots and is
1242 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02001243 bs = NULL;
1244 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001245
Markus Armbruster07b70bf2011-08-03 15:08:11 +02001246 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001247 continue;
1248 }
1249
1250 if (!bdrv_can_snapshot(bs)) {
1251 error_report("Device '%s' is writable but does not support snapshots.",
1252 bdrv_get_device_name(bs));
1253 return -ENOTSUP;
1254 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001255
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001256 ret = bdrv_snapshot_find(bs, &sn, name);
1257 if (ret < 0) {
1258 error_report("Device '%s' does not have the requested snapshot '%s'",
1259 bdrv_get_device_name(bs), name);
1260 return ret;
1261 }
aliguoria672b462008-11-11 21:33:36 +00001262 }
1263
1264 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001265 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00001266
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001267 bs = NULL;
1268 while ((bs = bdrv_next(bs))) {
1269 if (bdrv_can_snapshot(bs)) {
1270 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00001271 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001272 error_report("Error %d while activating snapshot '%s' on '%s'",
1273 ret, name, bdrv_get_device_name(bs));
1274 return ret;
aliguoria672b462008-11-11 21:33:36 +00001275 }
1276 }
1277 }
1278
aliguoria672b462008-11-11 21:33:36 +00001279 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001280 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00001281 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001282 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02001283 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00001284 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001285
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02001286 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00001287 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001288
aliguoria672b462008-11-11 21:33:36 +00001289 qemu_fclose(f);
1290 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001291 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02001292 return ret;
aliguoria672b462008-11-11 21:33:36 +00001293 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001294
Juan Quintela05f24012009-08-20 19:42:22 +02001295 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02001296}
1297
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001298void hmp_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001299{
Zhang Haoyuaf957382014-09-29 16:38:02 +08001300 BlockDriverState *bs;
Chris Spiegelba2b2282014-10-06 09:33:45 -07001301 Error *err;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001302 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00001303
Zhang Haoyuaf957382014-09-29 16:38:02 +08001304 if (!find_vmstate_bs()) {
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001306 return;
1307 }
1308
Zhang Haoyuaf957382014-09-29 16:38:02 +08001309 bs = NULL;
1310 while ((bs = bdrv_next(bs))) {
1311 if (bdrv_can_snapshot(bs)) {
Chris Spiegelba2b2282014-10-06 09:33:45 -07001312 err = NULL;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08001313 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001314 if (err) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08001315 monitor_printf(mon,
1316 "Error while deleting snapshot on device '%s':"
1317 " %s\n",
1318 bdrv_get_device_name(bs),
1319 error_get_pretty(err));
1320 error_free(err);
aliguoria672b462008-11-11 21:33:36 +00001321 }
1322 }
1323 }
1324}
1325
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001326void do_info_snapshots(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001327{
1328 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03001329 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
1330 int nb_sns, i, ret, available;
1331 int total;
1332 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00001333
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08001334 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00001335 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001336 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001337 return;
1338 }
aliguoria672b462008-11-11 21:33:36 +00001339
1340 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1341 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00001342 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00001343 return;
1344 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03001345
1346 if (nb_sns == 0) {
1347 monitor_printf(mon, "There is no snapshot available.\n");
1348 return;
aliguoria672b462008-11-11 21:33:36 +00001349 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03001350
Anthony Liguori7267c092011-08-20 22:09:37 -05001351 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03001352 total = 0;
1353 for (i = 0; i < nb_sns; i++) {
1354 sn = &sn_tab[i];
1355 available = 1;
1356 bs1 = NULL;
1357
1358 while ((bs1 = bdrv_next(bs1))) {
1359 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
1360 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
1361 if (ret < 0) {
1362 available = 0;
1363 break;
1364 }
1365 }
1366 }
1367
1368 if (available) {
1369 available_snapshots[total] = i;
1370 total++;
1371 }
1372 }
1373
1374 if (total > 0) {
Wenchao Xia5b917042013-05-25 11:09:45 +08001375 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1376 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03001377 for (i = 0; i < total; i++) {
1378 sn = &sn_tab[available_snapshots[i]];
Wenchao Xia5b917042013-05-25 11:09:45 +08001379 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1380 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03001381 }
1382 } else {
1383 monitor_printf(mon, "There is no suitable snapshot available\n");
1384 }
1385
Anthony Liguori7267c092011-08-20 22:09:37 -05001386 g_free(sn_tab);
1387 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03001388
aliguoria672b462008-11-11 21:33:36 +00001389}
Avi Kivityc5705a72011-12-20 15:59:12 +02001390
1391void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
1392{
Avi Kivity1ddde082012-01-08 13:18:19 +02001393 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02001394 memory_region_name(mr), dev);
1395}
1396
1397void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
1398{
Hu Taob0e56e02014-04-02 15:13:27 +08001399 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK);
Avi Kivityc5705a72011-12-20 15:59:12 +02001400}
1401
1402void vmstate_register_ram_global(MemoryRegion *mr)
1403{
1404 vmstate_register_ram(mr, NULL);
1405}