]>
Commit | Line | Data |
---|---|---|
a672b469 AL |
1 | /* |
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 | */ | |
a672b469 | 24 | |
d40cdb10 | 25 | #include "config-host.h" |
511d2b14 | 26 | #include "qemu-common.h" |
abfd9ce3 | 27 | #include "hw/boards.h" |
511d2b14 | 28 | #include "hw/hw.h" |
7685ee6a | 29 | #include "hw/qdev.h" |
1422e32d | 30 | #include "net/net.h" |
83c9089e | 31 | #include "monitor/monitor.h" |
9c17d615 | 32 | #include "sysemu/sysemu.h" |
1de7afc9 | 33 | #include "qemu/timer.h" |
511d2b14 | 34 | #include "audio/audio.h" |
caf71f86 | 35 | #include "migration/migration.h" |
1de7afc9 PB |
36 | #include "qemu/sockets.h" |
37 | #include "qemu/queue.h" | |
9c17d615 | 38 | #include "sysemu/cpus.h" |
022c62cb | 39 | #include "exec/memory.h" |
a7ae8355 | 40 | #include "qmp-commands.h" |
517a13c9 | 41 | #include "trace.h" |
28085f7b | 42 | #include "qemu/iov.h" |
de08c606 | 43 | #include "block/snapshot.h" |
f364ec65 | 44 | #include "block/qapi.h" |
511d2b14 | 45 | |
a672b469 | 46 | |
18995b98 | 47 | #ifndef ETH_P_RARP |
f8778a77 | 48 | #define ETH_P_RARP 0x8035 |
18995b98 N |
49 | #endif |
50 | #define ARP_HTYPE_ETH 0x0001 | |
51 | #define ARP_PTYPE_IP 0x0800 | |
52 | #define ARP_OP_REQUEST_REV 0x3 | |
53 | ||
54 | static int announce_self_create(uint8_t *buf, | |
5cecf414 | 55 | uint8_t *mac_addr) |
a672b469 | 56 | { |
18995b98 N |
57 | /* 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 */ | |
61 | ||
62 | /* 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 */ | |
72 | ||
73 | /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */ | |
74 | memset(buf + 42, 0x00, 18); | |
75 | ||
76 | return 60; /* len (FCS will be added by hardware) */ | |
a672b469 AL |
77 | } |
78 | ||
f401ca22 | 79 | static void qemu_announce_self_iter(NICState *nic, void *opaque) |
a672b469 | 80 | { |
18995b98 | 81 | uint8_t buf[60]; |
f401ca22 MM |
82 | int len; |
83 | ||
9013dca5 | 84 | trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr)); |
f401ca22 MM |
85 | len = announce_self_create(buf, nic->conf->macaddr.a); |
86 | ||
b356f76d | 87 | qemu_send_packet_raw(qemu_get_queue(nic), buf, len); |
f401ca22 MM |
88 | } |
89 | ||
90 | ||
91 | static void qemu_announce_self_once(void *opaque) | |
92 | { | |
ed8b330b GN |
93 | static int count = SELF_ANNOUNCE_ROUNDS; |
94 | QEMUTimer *timer = *(QEMUTimer **)opaque; | |
a672b469 | 95 | |
f401ca22 MM |
96 | qemu_foreach_nic(qemu_announce_self_iter, NULL); |
97 | ||
18995b98 N |
98 | if (--count) { |
99 | /* delay 50ms, 150ms, 250ms, ... */ | |
bc72ad67 | 100 | timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + |
508e1180 | 101 | self_announce_delay(count)); |
ed8b330b | 102 | } else { |
5cecf414 EH |
103 | timer_del(timer); |
104 | timer_free(timer); | |
ed8b330b GN |
105 | } |
106 | } | |
107 | ||
108 | void qemu_announce_self(void) | |
109 | { | |
5cecf414 EH |
110 | static QEMUTimer *timer; |
111 | timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer); | |
112 | qemu_announce_self_once(&timer); | |
a672b469 AL |
113 | } |
114 | ||
115 | /***********************************************************/ | |
116 | /* savevm/loadvm support */ | |
117 | ||
05fcc848 KW |
118 | static 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 | ||
178e08a5 | 133 | static int block_put_buffer(void *opaque, const uint8_t *buf, |
a672b469 AL |
134 | int64_t pos, int size) |
135 | { | |
45566e9c | 136 | bdrv_save_vmstate(opaque, buf, pos, size); |
a672b469 AL |
137 | return size; |
138 | } | |
139 | ||
178e08a5 | 140 | static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
a672b469 | 141 | { |
45566e9c | 142 | return bdrv_load_vmstate(opaque, buf, pos, size); |
a672b469 AL |
143 | } |
144 | ||
145 | static int bdrv_fclose(void *opaque) | |
146 | { | |
ad492c92 | 147 | return bdrv_flush(opaque); |
a672b469 AL |
148 | } |
149 | ||
9229bf3c PB |
150 | static const QEMUFileOps bdrv_read_ops = { |
151 | .get_buffer = block_get_buffer, | |
152 | .close = bdrv_fclose | |
153 | }; | |
154 | ||
155 | static const QEMUFileOps bdrv_write_ops = { | |
05fcc848 KW |
156 | .put_buffer = block_put_buffer, |
157 | .writev_buffer = block_writev_buffer, | |
158 | .close = bdrv_fclose | |
9229bf3c PB |
159 | }; |
160 | ||
45566e9c | 161 | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) |
a672b469 | 162 | { |
38ff78d3 | 163 | if (is_writable) { |
9229bf3c | 164 | return qemu_fopen_ops(bs, &bdrv_write_ops); |
38ff78d3 | 165 | } |
9229bf3c | 166 | return qemu_fopen_ops(bs, &bdrv_read_ops); |
a672b469 AL |
167 | } |
168 | ||
2ff68d07 | 169 | |
bb1a6d8c EH |
170 | /* QEMUFile timer support. |
171 | * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c | |
172 | */ | |
2ff68d07 | 173 | |
40daca54 | 174 | void timer_put(QEMUFile *f, QEMUTimer *ts) |
2ff68d07 PB |
175 | { |
176 | uint64_t expire_time; | |
177 | ||
e93379b0 | 178 | expire_time = timer_expire_time_ns(ts); |
2ff68d07 PB |
179 | qemu_put_be64(f, expire_time); |
180 | } | |
181 | ||
40daca54 | 182 | void timer_get(QEMUFile *f, QEMUTimer *ts) |
2ff68d07 PB |
183 | { |
184 | uint64_t expire_time; | |
185 | ||
186 | expire_time = qemu_get_be64(f); | |
187 | if (expire_time != -1) { | |
bc72ad67 | 188 | timer_mod_ns(ts, expire_time); |
2ff68d07 | 189 | } else { |
bc72ad67 | 190 | timer_del(ts); |
2ff68d07 PB |
191 | } |
192 | } | |
193 | ||
194 | ||
bb1a6d8c EH |
195 | /* VMState timer support. |
196 | * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c | |
197 | */ | |
dde0463b JQ |
198 | |
199 | static int get_timer(QEMUFile *f, void *pv, size_t size) | |
200 | { | |
201 | QEMUTimer *v = pv; | |
40daca54 | 202 | timer_get(f, v); |
dde0463b JQ |
203 | return 0; |
204 | } | |
205 | ||
84e2e3eb | 206 | static void put_timer(QEMUFile *f, void *pv, size_t size) |
dde0463b | 207 | { |
84e2e3eb | 208 | QEMUTimer *v = pv; |
40daca54 | 209 | timer_put(f, v); |
dde0463b JQ |
210 | } |
211 | ||
212 | const VMStateInfo vmstate_info_timer = { | |
213 | .name = "timer", | |
214 | .get = get_timer, | |
215 | .put = put_timer, | |
216 | }; | |
217 | ||
08e99e29 | 218 | |
7685ee6a AW |
219 | typedef struct CompatEntry { |
220 | char idstr[256]; | |
221 | int instance_id; | |
222 | } CompatEntry; | |
223 | ||
a672b469 | 224 | typedef struct SaveStateEntry { |
72cf2d4f | 225 | QTAILQ_ENTRY(SaveStateEntry) entry; |
a672b469 AL |
226 | char idstr[256]; |
227 | int instance_id; | |
4d2ffa08 | 228 | int alias_id; |
a672b469 AL |
229 | int version_id; |
230 | int section_id; | |
22ea40f4 | 231 | SaveVMHandlers *ops; |
9ed7d6ae | 232 | const VMStateDescription *vmsd; |
a672b469 | 233 | void *opaque; |
7685ee6a | 234 | CompatEntry *compat; |
a7ae8355 | 235 | int is_ram; |
a672b469 AL |
236 | } SaveStateEntry; |
237 | ||
c163b5ca | 238 | |
72cf2d4f BS |
239 | static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers = |
240 | QTAILQ_HEAD_INITIALIZER(savevm_handlers); | |
9ed7d6ae | 241 | static int global_section_id; |
a672b469 | 242 | |
abfd9ce3 AS |
243 | static void dump_vmstate_vmsd(FILE *out_file, |
244 | const VMStateDescription *vmsd, int indent, | |
245 | bool is_subsection); | |
246 | ||
247 | static 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 | ||
265 | static 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 | ||
274 | static 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 | ||
329 | static 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 | ||
340 | void 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 | ||
8718e999 JQ |
381 | static int calculate_new_instance_id(const char *idstr) |
382 | { | |
383 | SaveStateEntry *se; | |
384 | int instance_id = 0; | |
385 | ||
72cf2d4f | 386 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
8718e999 JQ |
387 | 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 | ||
7685ee6a AW |
395 | static 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) { | |
38ff78d3 | 401 | if (!se->compat) { |
7685ee6a | 402 | continue; |
38ff78d3 | 403 | } |
7685ee6a AW |
404 | |
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 | ||
a672b469 AL |
413 | /* 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. */ | |
0be71e32 AW |
417 | int register_savevm_live(DeviceState *dev, |
418 | const char *idstr, | |
a672b469 AL |
419 | int instance_id, |
420 | int version_id, | |
7908c78d | 421 | SaveVMHandlers *ops, |
a672b469 AL |
422 | void *opaque) |
423 | { | |
8718e999 | 424 | SaveStateEntry *se; |
a672b469 | 425 | |
7267c094 | 426 | se = g_malloc0(sizeof(SaveStateEntry)); |
a672b469 AL |
427 | se->version_id = version_id; |
428 | se->section_id = global_section_id++; | |
7908c78d | 429 | se->ops = ops; |
a672b469 | 430 | se->opaque = opaque; |
9ed7d6ae | 431 | se->vmsd = NULL; |
a7ae8355 | 432 | /* if this is a live_savem then set is_ram */ |
16310a3c | 433 | if (ops->save_live_setup != NULL) { |
a7ae8355 SS |
434 | se->is_ram = 1; |
435 | } | |
a672b469 | 436 | |
09e5ab63 AL |
437 | if (dev) { |
438 | char *id = qdev_get_dev_path(dev); | |
7685ee6a AW |
439 | if (id) { |
440 | pstrcpy(se->idstr, sizeof(se->idstr), id); | |
441 | pstrcat(se->idstr, sizeof(se->idstr), "/"); | |
7267c094 | 442 | g_free(id); |
7685ee6a | 443 | |
7267c094 | 444 | se->compat = g_malloc0(sizeof(CompatEntry)); |
7685ee6a AW |
445 | 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 | ||
8718e999 | 453 | if (instance_id == -1) { |
7685ee6a | 454 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
455 | } else { |
456 | se->instance_id = instance_id; | |
a672b469 | 457 | } |
7685ee6a | 458 | assert(!se->compat || se->instance_id == 0); |
8718e999 | 459 | /* add at the end of list */ |
72cf2d4f | 460 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
a672b469 AL |
461 | return 0; |
462 | } | |
463 | ||
0be71e32 AW |
464 | int register_savevm(DeviceState *dev, |
465 | const char *idstr, | |
a672b469 AL |
466 | int instance_id, |
467 | int version_id, | |
468 | SaveStateHandler *save_state, | |
469 | LoadStateHandler *load_state, | |
470 | void *opaque) | |
471 | { | |
7908c78d JQ |
472 | SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers)); |
473 | ops->save_state = save_state; | |
474 | ops->load_state = load_state; | |
0be71e32 | 475 | return register_savevm_live(dev, idstr, instance_id, version_id, |
7908c78d | 476 | ops, opaque); |
a672b469 AL |
477 | } |
478 | ||
0be71e32 | 479 | void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque) |
41bd13af | 480 | { |
8718e999 | 481 | SaveStateEntry *se, *new_se; |
7685ee6a AW |
482 | char id[256] = ""; |
483 | ||
09e5ab63 AL |
484 | if (dev) { |
485 | char *path = qdev_get_dev_path(dev); | |
7685ee6a AW |
486 | if (path) { |
487 | pstrcpy(id, sizeof(id), path); | |
488 | pstrcat(id, sizeof(id), "/"); | |
7267c094 | 489 | g_free(path); |
7685ee6a AW |
490 | } |
491 | } | |
492 | pstrcat(id, sizeof(id), idstr); | |
41bd13af | 493 | |
72cf2d4f | 494 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
7685ee6a | 495 | if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { |
72cf2d4f | 496 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
69e58af9 | 497 | if (se->compat) { |
7267c094 | 498 | g_free(se->compat); |
69e58af9 | 499 | } |
22ea40f4 | 500 | g_free(se->ops); |
7267c094 | 501 | g_free(se); |
41bd13af | 502 | } |
41bd13af AL |
503 | } |
504 | } | |
505 | ||
0be71e32 | 506 | int vmstate_register_with_alias_id(DeviceState *dev, int instance_id, |
4d2ffa08 JK |
507 | const VMStateDescription *vmsd, |
508 | void *opaque, int alias_id, | |
509 | int required_for_version) | |
9ed7d6ae | 510 | { |
8718e999 | 511 | SaveStateEntry *se; |
9ed7d6ae | 512 | |
4d2ffa08 JK |
513 | /* If this triggers, alias support can be dropped for the vmsd. */ |
514 | assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); | |
515 | ||
7267c094 | 516 | se = g_malloc0(sizeof(SaveStateEntry)); |
9ed7d6ae JQ |
517 | se->version_id = vmsd->version_id; |
518 | se->section_id = global_section_id++; | |
9ed7d6ae JQ |
519 | se->opaque = opaque; |
520 | se->vmsd = vmsd; | |
4d2ffa08 | 521 | se->alias_id = alias_id; |
9ed7d6ae | 522 | |
09e5ab63 AL |
523 | if (dev) { |
524 | char *id = qdev_get_dev_path(dev); | |
7685ee6a AW |
525 | if (id) { |
526 | pstrcpy(se->idstr, sizeof(se->idstr), id); | |
527 | pstrcat(se->idstr, sizeof(se->idstr), "/"); | |
7267c094 | 528 | g_free(id); |
7685ee6a | 529 | |
7267c094 | 530 | se->compat = g_malloc0(sizeof(CompatEntry)); |
7685ee6a AW |
531 | 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 | ||
8718e999 | 539 | if (instance_id == -1) { |
7685ee6a | 540 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
541 | } else { |
542 | se->instance_id = instance_id; | |
9ed7d6ae | 543 | } |
7685ee6a | 544 | assert(!se->compat || se->instance_id == 0); |
8718e999 | 545 | /* add at the end of list */ |
72cf2d4f | 546 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
9ed7d6ae JQ |
547 | return 0; |
548 | } | |
549 | ||
0be71e32 AW |
550 | void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd, |
551 | void *opaque) | |
9ed7d6ae | 552 | { |
1eb7538b JQ |
553 | SaveStateEntry *se, *new_se; |
554 | ||
72cf2d4f | 555 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
1eb7538b | 556 | if (se->vmsd == vmsd && se->opaque == opaque) { |
72cf2d4f | 557 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
69e58af9 | 558 | if (se->compat) { |
7267c094 | 559 | g_free(se->compat); |
69e58af9 | 560 | } |
7267c094 | 561 | g_free(se); |
1eb7538b JQ |
562 | } |
563 | } | |
9ed7d6ae JQ |
564 | } |
565 | ||
4082be4d JQ |
566 | static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id) |
567 | { | |
9013dca5 | 568 | trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); |
9ed7d6ae | 569 | if (!se->vmsd) { /* Old style */ |
22ea40f4 | 570 | return se->ops->load_state(f, se->opaque, version_id); |
9ed7d6ae JQ |
571 | } |
572 | return vmstate_load_state(f, se->vmsd, se->opaque, version_id); | |
4082be4d JQ |
573 | } |
574 | ||
8118f095 AG |
575 | static 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 | ||
595 | static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) | |
4082be4d | 596 | { |
9013dca5 | 597 | trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); |
8118f095 AG |
598 | if (!se->vmsd) { |
599 | vmstate_save_old_style(f, se, vmdesc); | |
dc912121 | 600 | return; |
9ed7d6ae | 601 | } |
8118f095 | 602 | vmstate_save_state(f, se->vmsd, se->opaque, vmdesc); |
4082be4d JQ |
603 | } |
604 | ||
e1c37d0e | 605 | bool qemu_savevm_state_blocked(Error **errp) |
dc912121 AW |
606 | { |
607 | SaveStateEntry *se; | |
608 | ||
609 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { | |
7d854c47 | 610 | if (se->vmsd && se->vmsd->unmigratable) { |
f231b88d CR |
611 | error_setg(errp, "State blocked by non-migratable device '%s'", |
612 | se->idstr); | |
dc912121 AW |
613 | return true; |
614 | } | |
615 | } | |
616 | return false; | |
617 | } | |
618 | ||
47c8c17a PB |
619 | void qemu_savevm_state_begin(QEMUFile *f, |
620 | const MigrationParams *params) | |
a672b469 AL |
621 | { |
622 | SaveStateEntry *se; | |
39346385 | 623 | int ret; |
a672b469 | 624 | |
9013dca5 | 625 | trace_savevm_state_begin(); |
c163b5ca | 626 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
22ea40f4 | 627 | if (!se->ops || !se->ops->set_params) { |
c163b5ca | 628 | continue; |
6607ae23 | 629 | } |
22ea40f4 | 630 | se->ops->set_params(params, se->opaque); |
c163b5ca | 631 | } |
38ff78d3 | 632 | |
a672b469 AL |
633 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
634 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
635 | ||
72cf2d4f | 636 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
a672b469 AL |
637 | int len; |
638 | ||
d1315aac | 639 | if (!se->ops || !se->ops->save_live_setup) { |
a672b469 | 640 | continue; |
22ea40f4 | 641 | } |
6bd68781 JQ |
642 | if (se->ops && se->ops->is_active) { |
643 | if (!se->ops->is_active(se->opaque)) { | |
644 | continue; | |
645 | } | |
646 | } | |
a672b469 AL |
647 | /* 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 | ||
d1315aac | 659 | ret = se->ops->save_live_setup(f, se->opaque); |
2975725f | 660 | if (ret < 0) { |
47c8c17a PB |
661 | qemu_file_set_error(f, ret); |
662 | break; | |
2975725f | 663 | } |
a672b469 | 664 | } |
a672b469 AL |
665 | } |
666 | ||
39346385 | 667 | /* |
07f35073 | 668 | * this function has three return values: |
39346385 JQ |
669 | * 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 | */ | |
539de124 | 673 | int qemu_savevm_state_iterate(QEMUFile *f) |
a672b469 AL |
674 | { |
675 | SaveStateEntry *se; | |
676 | int ret = 1; | |
677 | ||
9013dca5 | 678 | trace_savevm_state_iterate(); |
72cf2d4f | 679 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
16310a3c | 680 | if (!se->ops || !se->ops->save_live_iterate) { |
a672b469 | 681 | continue; |
22ea40f4 | 682 | } |
6bd68781 JQ |
683 | if (se->ops && se->ops->is_active) { |
684 | if (!se->ops->is_active(se->opaque)) { | |
685 | continue; | |
686 | } | |
687 | } | |
aac844ed JQ |
688 | if (qemu_file_rate_limit(f)) { |
689 | return 0; | |
690 | } | |
464400f6 | 691 | trace_savevm_section_start(se->idstr, se->section_id); |
a672b469 AL |
692 | /* Section type */ |
693 | qemu_put_byte(f, QEMU_VM_SECTION_PART); | |
694 | qemu_put_be32(f, se->section_id); | |
695 | ||
16310a3c | 696 | ret = se->ops->save_live_iterate(f, se->opaque); |
a5df2a02 | 697 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
517a13c9 | 698 | |
47c8c17a PB |
699 | if (ret < 0) { |
700 | qemu_file_set_error(f, ret); | |
701 | } | |
2975725f | 702 | if (ret <= 0) { |
90697be8 JK |
703 | /* 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 | } | |
a672b469 | 709 | } |
39346385 | 710 | return ret; |
a672b469 AL |
711 | } |
712 | ||
47c8c17a | 713 | void qemu_savevm_state_complete(QEMUFile *f) |
a672b469 | 714 | { |
8118f095 AG |
715 | QJSON *vmdesc; |
716 | int vmdesc_len; | |
a672b469 | 717 | SaveStateEntry *se; |
2975725f | 718 | int ret; |
a672b469 | 719 | |
9013dca5 AK |
720 | trace_savevm_state_complete(); |
721 | ||
ea375f9a JK |
722 | cpu_synchronize_all_states(); |
723 | ||
72cf2d4f | 724 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
16310a3c | 725 | if (!se->ops || !se->ops->save_live_complete) { |
a672b469 | 726 | continue; |
22ea40f4 | 727 | } |
6bd68781 JQ |
728 | if (se->ops && se->ops->is_active) { |
729 | if (!se->ops->is_active(se->opaque)) { | |
730 | continue; | |
731 | } | |
732 | } | |
464400f6 | 733 | trace_savevm_section_start(se->idstr, se->section_id); |
a672b469 AL |
734 | /* Section type */ |
735 | qemu_put_byte(f, QEMU_VM_SECTION_END); | |
736 | qemu_put_be32(f, se->section_id); | |
737 | ||
16310a3c | 738 | ret = se->ops->save_live_complete(f, se->opaque); |
a5df2a02 | 739 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
2975725f | 740 | if (ret < 0) { |
47c8c17a PB |
741 | qemu_file_set_error(f, ret); |
742 | return; | |
2975725f | 743 | } |
a672b469 AL |
744 | } |
745 | ||
8118f095 AG |
746 | vmdesc = qjson_new(); |
747 | json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE); | |
748 | json_start_array(vmdesc, "devices"); | |
72cf2d4f | 749 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
a672b469 AL |
750 | int len; |
751 | ||
22ea40f4 | 752 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
5cecf414 | 753 | continue; |
22ea40f4 | 754 | } |
464400f6 | 755 | trace_savevm_section_start(se->idstr, se->section_id); |
8118f095 AG |
756 | |
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 | ||
a672b469 AL |
761 | /* 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 | ||
8118f095 AG |
773 | vmstate_save(f, se, vmdesc); |
774 | ||
775 | json_end_object(vmdesc); | |
a5df2a02 | 776 | trace_savevm_section_end(se->idstr, se->section_id, 0); |
a672b469 AL |
777 | } |
778 | ||
779 | qemu_put_byte(f, QEMU_VM_EOF); | |
8118f095 AG |
780 | |
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 | ||
edaae611 | 790 | qemu_fflush(f); |
a672b469 AL |
791 | } |
792 | ||
e4ed1541 JQ |
793 | uint64_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 | ||
6522773f | 812 | void qemu_savevm_state_cancel(void) |
4ec7fcc7 JK |
813 | { |
814 | SaveStateEntry *se; | |
815 | ||
9013dca5 | 816 | trace_savevm_state_cancel(); |
4ec7fcc7 | 817 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
9b5bfab0 JQ |
818 | if (se->ops && se->ops->cancel) { |
819 | se->ops->cancel(se->opaque); | |
4ec7fcc7 JK |
820 | } |
821 | } | |
822 | } | |
823 | ||
5d80448c | 824 | static int qemu_savevm_state(QEMUFile *f, Error **errp) |
a672b469 | 825 | { |
a672b469 | 826 | int ret; |
6607ae23 IY |
827 | MigrationParams params = { |
828 | .blk = 0, | |
829 | .shared = 0 | |
830 | }; | |
a672b469 | 831 | |
5d80448c | 832 | if (qemu_savevm_state_blocked(errp)) { |
04943eba | 833 | return -EINVAL; |
dc912121 AW |
834 | } |
835 | ||
9b095037 | 836 | qemu_mutex_unlock_iothread(); |
47c8c17a | 837 | qemu_savevm_state_begin(f, ¶ms); |
9b095037 PB |
838 | qemu_mutex_lock_iothread(); |
839 | ||
47c8c17a PB |
840 | while (qemu_file_get_error(f) == 0) { |
841 | if (qemu_savevm_state_iterate(f) > 0) { | |
842 | break; | |
843 | } | |
844 | } | |
a672b469 | 845 | |
47c8c17a | 846 | ret = qemu_file_get_error(f); |
39346385 | 847 | if (ret == 0) { |
47c8c17a | 848 | qemu_savevm_state_complete(f); |
624b9cc2 | 849 | ret = qemu_file_get_error(f); |
39346385 | 850 | } |
04943eba PB |
851 | if (ret != 0) { |
852 | qemu_savevm_state_cancel(); | |
5d80448c | 853 | error_setg_errno(errp, -ret, "Error while writing VM state"); |
04943eba | 854 | } |
a672b469 AL |
855 | return ret; |
856 | } | |
857 | ||
a7ae8355 SS |
858 | static int qemu_save_device_state(QEMUFile *f) |
859 | { | |
860 | SaveStateEntry *se; | |
861 | ||
862 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
863 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
864 | ||
865 | cpu_synchronize_all_states(); | |
866 | ||
867 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { | |
868 | int len; | |
869 | ||
870 | if (se->is_ram) { | |
871 | continue; | |
872 | } | |
22ea40f4 | 873 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
a7ae8355 SS |
874 | continue; |
875 | } | |
876 | ||
877 | /* Section type */ | |
878 | qemu_put_byte(f, QEMU_VM_SECTION_FULL); | |
879 | qemu_put_be32(f, se->section_id); | |
880 | ||
881 | /* ID string */ | |
882 | len = strlen(se->idstr); | |
883 | qemu_put_byte(f, len); | |
884 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); | |
885 | ||
886 | qemu_put_be32(f, se->instance_id); | |
887 | qemu_put_be32(f, se->version_id); | |
888 | ||
8118f095 | 889 | vmstate_save(f, se, NULL); |
a7ae8355 SS |
890 | } |
891 | ||
892 | qemu_put_byte(f, QEMU_VM_EOF); | |
893 | ||
894 | return qemu_file_get_error(f); | |
895 | } | |
896 | ||
a672b469 AL |
897 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
898 | { | |
899 | SaveStateEntry *se; | |
900 | ||
72cf2d4f | 901 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
a672b469 | 902 | if (!strcmp(se->idstr, idstr) && |
4d2ffa08 JK |
903 | (instance_id == se->instance_id || |
904 | instance_id == se->alias_id)) | |
a672b469 | 905 | return se; |
7685ee6a AW |
906 | /* Migrating from an older version? */ |
907 | if (strstr(se->idstr, idstr) && se->compat) { | |
908 | if (!strcmp(se->compat->idstr, idstr) && | |
909 | (instance_id == se->compat->instance_id || | |
910 | instance_id == se->alias_id)) | |
911 | return se; | |
912 | } | |
a672b469 AL |
913 | } |
914 | return NULL; | |
915 | } | |
916 | ||
917 | typedef struct LoadStateEntry { | |
72cf2d4f | 918 | QLIST_ENTRY(LoadStateEntry) entry; |
a672b469 AL |
919 | SaveStateEntry *se; |
920 | int section_id; | |
921 | int version_id; | |
a672b469 AL |
922 | } LoadStateEntry; |
923 | ||
a672b469 AL |
924 | int qemu_loadvm_state(QEMUFile *f) |
925 | { | |
72cf2d4f BS |
926 | QLIST_HEAD(, LoadStateEntry) loadvm_handlers = |
927 | QLIST_HEAD_INITIALIZER(loadvm_handlers); | |
f4dbb8dd | 928 | LoadStateEntry *le, *new_le; |
0457d073 | 929 | Error *local_err = NULL; |
a672b469 AL |
930 | uint8_t section_type; |
931 | unsigned int v; | |
932 | int ret; | |
933 | ||
0457d073 DDAG |
934 | if (qemu_savevm_state_blocked(&local_err)) { |
935 | error_report("%s", error_get_pretty(local_err)); | |
936 | error_free(local_err); | |
dc912121 AW |
937 | return -EINVAL; |
938 | } | |
939 | ||
a672b469 | 940 | v = qemu_get_be32(f); |
38ff78d3 | 941 | if (v != QEMU_VM_FILE_MAGIC) { |
0457d073 | 942 | error_report("Not a migration stream"); |
a672b469 | 943 | return -EINVAL; |
38ff78d3 | 944 | } |
a672b469 AL |
945 | |
946 | v = qemu_get_be32(f); | |
bbfe1408 | 947 | if (v == QEMU_VM_FILE_VERSION_COMPAT) { |
6a64b644 | 948 | error_report("SaveVM v2 format is obsolete and don't work anymore"); |
bbfe1408 JQ |
949 | return -ENOTSUP; |
950 | } | |
38ff78d3 | 951 | if (v != QEMU_VM_FILE_VERSION) { |
0457d073 | 952 | error_report("Unsupported migration stream version"); |
a672b469 | 953 | return -ENOTSUP; |
38ff78d3 | 954 | } |
a672b469 AL |
955 | |
956 | while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) { | |
957 | uint32_t instance_id, version_id, section_id; | |
a672b469 AL |
958 | SaveStateEntry *se; |
959 | char idstr[257]; | |
960 | int len; | |
961 | ||
a5df2a02 | 962 | trace_qemu_loadvm_state_section(section_type); |
a672b469 AL |
963 | switch (section_type) { |
964 | case QEMU_VM_SECTION_START: | |
965 | case QEMU_VM_SECTION_FULL: | |
966 | /* Read section start */ | |
967 | section_id = qemu_get_be32(f); | |
968 | len = qemu_get_byte(f); | |
969 | qemu_get_buffer(f, (uint8_t *)idstr, len); | |
970 | idstr[len] = 0; | |
971 | instance_id = qemu_get_be32(f); | |
972 | version_id = qemu_get_be32(f); | |
973 | ||
a5df2a02 DDAG |
974 | trace_qemu_loadvm_state_section_startfull(section_id, idstr, |
975 | instance_id, version_id); | |
a672b469 AL |
976 | /* Find savevm section */ |
977 | se = find_se(idstr, instance_id); | |
978 | if (se == NULL) { | |
6a64b644 DDAG |
979 | error_report("Unknown savevm section or instance '%s' %d", |
980 | idstr, instance_id); | |
a672b469 AL |
981 | ret = -EINVAL; |
982 | goto out; | |
983 | } | |
984 | ||
985 | /* Validate version */ | |
986 | if (version_id > se->version_id) { | |
6a64b644 DDAG |
987 | error_report("savevm: unsupported version %d for '%s' v%d", |
988 | version_id, idstr, se->version_id); | |
a672b469 AL |
989 | ret = -EINVAL; |
990 | goto out; | |
991 | } | |
992 | ||
993 | /* Add entry */ | |
7267c094 | 994 | le = g_malloc0(sizeof(*le)); |
a672b469 AL |
995 | |
996 | le->se = se; | |
997 | le->section_id = section_id; | |
998 | le->version_id = version_id; | |
72cf2d4f | 999 | QLIST_INSERT_HEAD(&loadvm_handlers, le, entry); |
a672b469 | 1000 | |
4082be4d | 1001 | ret = vmstate_load(f, le->se, le->version_id); |
b5a22e4a | 1002 | if (ret < 0) { |
6a64b644 DDAG |
1003 | error_report("error while loading state for instance 0x%x of" |
1004 | " device '%s'", instance_id, idstr); | |
b5a22e4a JQ |
1005 | goto out; |
1006 | } | |
a672b469 AL |
1007 | break; |
1008 | case QEMU_VM_SECTION_PART: | |
1009 | case QEMU_VM_SECTION_END: | |
1010 | section_id = qemu_get_be32(f); | |
1011 | ||
a5df2a02 | 1012 | trace_qemu_loadvm_state_section_partend(section_id); |
72cf2d4f | 1013 | QLIST_FOREACH(le, &loadvm_handlers, entry) { |
f4dbb8dd JQ |
1014 | if (le->section_id == section_id) { |
1015 | break; | |
1016 | } | |
1017 | } | |
a672b469 | 1018 | if (le == NULL) { |
6a64b644 | 1019 | error_report("Unknown savevm section %d", section_id); |
a672b469 AL |
1020 | ret = -EINVAL; |
1021 | goto out; | |
1022 | } | |
1023 | ||
4082be4d | 1024 | ret = vmstate_load(f, le->se, le->version_id); |
b5a22e4a | 1025 | if (ret < 0) { |
6a64b644 DDAG |
1026 | error_report("error while loading state section id %d(%s)", |
1027 | section_id, le->se->idstr); | |
b5a22e4a JQ |
1028 | goto out; |
1029 | } | |
a672b469 AL |
1030 | break; |
1031 | default: | |
6a64b644 | 1032 | error_report("Unknown savevm section type %d", section_type); |
a672b469 AL |
1033 | ret = -EINVAL; |
1034 | goto out; | |
1035 | } | |
1036 | } | |
1037 | ||
ea375f9a JK |
1038 | cpu_synchronize_all_post_init(); |
1039 | ||
a672b469 AL |
1040 | ret = 0; |
1041 | ||
1042 | out: | |
72cf2d4f BS |
1043 | QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) { |
1044 | QLIST_REMOVE(le, entry); | |
7267c094 | 1045 | g_free(le); |
a672b469 AL |
1046 | } |
1047 | ||
42802d47 JQ |
1048 | if (ret == 0) { |
1049 | ret = qemu_file_get_error(f); | |
624b9cc2 | 1050 | } |
a672b469 AL |
1051 | |
1052 | return ret; | |
1053 | } | |
1054 | ||
29d78271 SH |
1055 | static BlockDriverState *find_vmstate_bs(void) |
1056 | { | |
1057 | BlockDriverState *bs = NULL; | |
1058 | while ((bs = bdrv_next(bs))) { | |
1059 | if (bdrv_can_snapshot(bs)) { | |
1060 | return bs; | |
1061 | } | |
1062 | } | |
1063 | return NULL; | |
1064 | } | |
1065 | ||
cb499fb2 KW |
1066 | /* |
1067 | * Deletes snapshots of a given name in all opened images. | |
1068 | */ | |
1069 | static int del_existing_snapshots(Monitor *mon, const char *name) | |
1070 | { | |
1071 | BlockDriverState *bs; | |
cb499fb2 | 1072 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
a89d89d3 | 1073 | Error *err = NULL; |
cb499fb2 | 1074 | |
dbc13590 MA |
1075 | bs = NULL; |
1076 | while ((bs = bdrv_next(bs))) { | |
cb499fb2 | 1077 | if (bdrv_can_snapshot(bs) && |
38ff78d3 | 1078 | bdrv_snapshot_find(bs, snapshot, name) >= 0) { |
a89d89d3 | 1079 | bdrv_snapshot_delete_by_id_or_name(bs, name, &err); |
84d18f06 | 1080 | if (err) { |
cb499fb2 | 1081 | monitor_printf(mon, |
a89d89d3 WX |
1082 | "Error while deleting snapshot on device '%s':" |
1083 | " %s\n", | |
1084 | bdrv_get_device_name(bs), | |
1085 | error_get_pretty(err)); | |
1086 | error_free(err); | |
cb499fb2 KW |
1087 | return -1; |
1088 | } | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | return 0; | |
1093 | } | |
1094 | ||
3e5a50d6 | 1095 | void hmp_savevm(Monitor *mon, const QDict *qdict) |
a672b469 AL |
1096 | { |
1097 | BlockDriverState *bs, *bs1; | |
1098 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; | |
cb499fb2 | 1099 | int ret; |
a672b469 AL |
1100 | QEMUFile *f; |
1101 | int saved_vm_running; | |
c2c9a466 | 1102 | uint64_t vm_state_size; |
68b891ec | 1103 | qemu_timeval tv; |
7d631a11 | 1104 | struct tm tm; |
d54908a5 | 1105 | const char *name = qdict_get_try_str(qdict, "name"); |
5d80448c | 1106 | Error *local_err = NULL; |
a672b469 | 1107 | |
feeee5ac | 1108 | /* Verify if there is a device that doesn't support snapshots and is writable */ |
dbc13590 MA |
1109 | bs = NULL; |
1110 | while ((bs = bdrv_next(bs))) { | |
feeee5ac | 1111 | |
07b70bfb | 1112 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
feeee5ac MDCF |
1113 | continue; |
1114 | } | |
1115 | ||
1116 | if (!bdrv_can_snapshot(bs)) { | |
1117 | monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n", | |
1118 | bdrv_get_device_name(bs)); | |
1119 | return; | |
1120 | } | |
1121 | } | |
1122 | ||
29d78271 | 1123 | bs = find_vmstate_bs(); |
a672b469 | 1124 | if (!bs) { |
376253ec | 1125 | monitor_printf(mon, "No block device can accept snapshots\n"); |
a672b469 AL |
1126 | return; |
1127 | } | |
a672b469 | 1128 | |
1354869c | 1129 | saved_vm_running = runstate_is_running(); |
0461d5a6 | 1130 | vm_stop(RUN_STATE_SAVE_VM); |
a672b469 | 1131 | |
cb499fb2 | 1132 | memset(sn, 0, sizeof(*sn)); |
a672b469 AL |
1133 | |
1134 | /* fill auxiliary fields */ | |
68b891ec | 1135 | qemu_gettimeofday(&tv); |
a672b469 AL |
1136 | sn->date_sec = tv.tv_sec; |
1137 | sn->date_nsec = tv.tv_usec * 1000; | |
bc72ad67 | 1138 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
a672b469 | 1139 | |
7d631a11 MDCF |
1140 | if (name) { |
1141 | ret = bdrv_snapshot_find(bs, old_sn, name); | |
1142 | if (ret >= 0) { | |
1143 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); | |
1144 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); | |
1145 | } else { | |
1146 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1147 | } | |
1148 | } else { | |
d7d9b528 BS |
1149 | /* cast below needed for OpenBSD where tv_sec is still 'long' */ |
1150 | localtime_r((const time_t *)&tv.tv_sec, &tm); | |
7d631a11 | 1151 | strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm); |
7d631a11 MDCF |
1152 | } |
1153 | ||
cb499fb2 | 1154 | /* Delete old snapshots of the same name */ |
f139a412 | 1155 | if (name && del_existing_snapshots(mon, name) < 0) { |
cb499fb2 KW |
1156 | goto the_end; |
1157 | } | |
1158 | ||
a672b469 | 1159 | /* save the VM state */ |
45566e9c | 1160 | f = qemu_fopen_bdrv(bs, 1); |
a672b469 | 1161 | if (!f) { |
376253ec | 1162 | monitor_printf(mon, "Could not open VM state file\n"); |
a672b469 AL |
1163 | goto the_end; |
1164 | } | |
5d80448c | 1165 | ret = qemu_savevm_state(f, &local_err); |
2d22b18f | 1166 | vm_state_size = qemu_ftell(f); |
a672b469 AL |
1167 | qemu_fclose(f); |
1168 | if (ret < 0) { | |
5d80448c KW |
1169 | monitor_printf(mon, "%s\n", error_get_pretty(local_err)); |
1170 | error_free(local_err); | |
a672b469 AL |
1171 | goto the_end; |
1172 | } | |
1173 | ||
1174 | /* create the snapshots */ | |
1175 | ||
dbc13590 MA |
1176 | bs1 = NULL; |
1177 | while ((bs1 = bdrv_next(bs1))) { | |
feeee5ac | 1178 | if (bdrv_can_snapshot(bs1)) { |
2d22b18f AL |
1179 | /* Write VM state size only to the image that contains the state */ |
1180 | sn->vm_state_size = (bs == bs1 ? vm_state_size : 0); | |
a672b469 AL |
1181 | ret = bdrv_snapshot_create(bs1, sn); |
1182 | if (ret < 0) { | |
376253ec AL |
1183 | monitor_printf(mon, "Error while creating snapshot on '%s'\n", |
1184 | bdrv_get_device_name(bs1)); | |
a672b469 AL |
1185 | } |
1186 | } | |
1187 | } | |
1188 | ||
1189 | the_end: | |
38ff78d3 | 1190 | if (saved_vm_running) { |
a672b469 | 1191 | vm_start(); |
38ff78d3 | 1192 | } |
a672b469 AL |
1193 | } |
1194 | ||
a7ae8355 SS |
1195 | void qmp_xen_save_devices_state(const char *filename, Error **errp) |
1196 | { | |
1197 | QEMUFile *f; | |
1198 | int saved_vm_running; | |
1199 | int ret; | |
1200 | ||
1201 | saved_vm_running = runstate_is_running(); | |
1202 | vm_stop(RUN_STATE_SAVE_VM); | |
1203 | ||
1204 | f = qemu_fopen(filename, "wb"); | |
1205 | if (!f) { | |
1befce96 | 1206 | error_setg_file_open(errp, errno, filename); |
a7ae8355 SS |
1207 | goto the_end; |
1208 | } | |
1209 | ret = qemu_save_device_state(f); | |
1210 | qemu_fclose(f); | |
1211 | if (ret < 0) { | |
1212 | error_set(errp, QERR_IO_ERROR); | |
1213 | } | |
1214 | ||
1215 | the_end: | |
38ff78d3 | 1216 | if (saved_vm_running) { |
a7ae8355 | 1217 | vm_start(); |
38ff78d3 | 1218 | } |
a7ae8355 SS |
1219 | } |
1220 | ||
03cd4655 | 1221 | int load_vmstate(const char *name) |
a672b469 | 1222 | { |
f0aa7a8b | 1223 | BlockDriverState *bs, *bs_vm_state; |
2d22b18f | 1224 | QEMUSnapshotInfo sn; |
a672b469 | 1225 | QEMUFile *f; |
751c6a17 | 1226 | int ret; |
a672b469 | 1227 | |
29d78271 | 1228 | bs_vm_state = find_vmstate_bs(); |
f0aa7a8b MDCF |
1229 | if (!bs_vm_state) { |
1230 | error_report("No block device supports snapshots"); | |
1231 | return -ENOTSUP; | |
1232 | } | |
1233 | ||
1234 | /* Don't even try to load empty VM states */ | |
1235 | ret = bdrv_snapshot_find(bs_vm_state, &sn, name); | |
1236 | if (ret < 0) { | |
1237 | return ret; | |
1238 | } else if (sn.vm_state_size == 0) { | |
e11480db KW |
1239 | error_report("This is a disk-only snapshot. Revert to it offline " |
1240 | "using qemu-img."); | |
f0aa7a8b MDCF |
1241 | return -EINVAL; |
1242 | } | |
1243 | ||
1244 | /* Verify if there is any device that doesn't support snapshots and is | |
1245 | writable and check if the requested snapshot is available too. */ | |
dbc13590 MA |
1246 | bs = NULL; |
1247 | while ((bs = bdrv_next(bs))) { | |
feeee5ac | 1248 | |
07b70bfb | 1249 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
feeee5ac MDCF |
1250 | continue; |
1251 | } | |
1252 | ||
1253 | if (!bdrv_can_snapshot(bs)) { | |
1254 | error_report("Device '%s' is writable but does not support snapshots.", | |
1255 | bdrv_get_device_name(bs)); | |
1256 | return -ENOTSUP; | |
1257 | } | |
feeee5ac | 1258 | |
f0aa7a8b MDCF |
1259 | ret = bdrv_snapshot_find(bs, &sn, name); |
1260 | if (ret < 0) { | |
1261 | error_report("Device '%s' does not have the requested snapshot '%s'", | |
1262 | bdrv_get_device_name(bs), name); | |
1263 | return ret; | |
1264 | } | |
a672b469 AL |
1265 | } |
1266 | ||
1267 | /* Flush all IO requests so they don't interfere with the new state. */ | |
922453bc | 1268 | bdrv_drain_all(); |
a672b469 | 1269 | |
f0aa7a8b MDCF |
1270 | bs = NULL; |
1271 | while ((bs = bdrv_next(bs))) { | |
1272 | if (bdrv_can_snapshot(bs)) { | |
1273 | ret = bdrv_snapshot_goto(bs, name); | |
a672b469 | 1274 | if (ret < 0) { |
f0aa7a8b MDCF |
1275 | error_report("Error %d while activating snapshot '%s' on '%s'", |
1276 | ret, name, bdrv_get_device_name(bs)); | |
1277 | return ret; | |
a672b469 AL |
1278 | } |
1279 | } | |
1280 | } | |
1281 | ||
a672b469 | 1282 | /* restore the VM state */ |
f0aa7a8b | 1283 | f = qemu_fopen_bdrv(bs_vm_state, 0); |
a672b469 | 1284 | if (!f) { |
1ecda02b | 1285 | error_report("Could not open VM state file"); |
05f2401e | 1286 | return -EINVAL; |
a672b469 | 1287 | } |
f0aa7a8b | 1288 | |
5a8a49d7 | 1289 | qemu_system_reset(VMRESET_SILENT); |
a672b469 | 1290 | ret = qemu_loadvm_state(f); |
f0aa7a8b | 1291 | |
a672b469 AL |
1292 | qemu_fclose(f); |
1293 | if (ret < 0) { | |
1ecda02b | 1294 | error_report("Error %d while loading VM state", ret); |
05f2401e | 1295 | return ret; |
a672b469 | 1296 | } |
f0aa7a8b | 1297 | |
05f2401e | 1298 | return 0; |
7b630349 JQ |
1299 | } |
1300 | ||
3e5a50d6 | 1301 | void hmp_delvm(Monitor *mon, const QDict *qdict) |
a672b469 | 1302 | { |
af957387 | 1303 | BlockDriverState *bs; |
ba2b2288 | 1304 | Error *err; |
d54908a5 | 1305 | const char *name = qdict_get_str(qdict, "name"); |
a672b469 | 1306 | |
af957387 | 1307 | if (!find_vmstate_bs()) { |
376253ec | 1308 | monitor_printf(mon, "No block device supports snapshots\n"); |
a672b469 AL |
1309 | return; |
1310 | } | |
1311 | ||
af957387 ZH |
1312 | bs = NULL; |
1313 | while ((bs = bdrv_next(bs))) { | |
1314 | if (bdrv_can_snapshot(bs)) { | |
ba2b2288 | 1315 | err = NULL; |
a89d89d3 | 1316 | bdrv_snapshot_delete_by_id_or_name(bs, name, &err); |
84d18f06 | 1317 | if (err) { |
a89d89d3 WX |
1318 | monitor_printf(mon, |
1319 | "Error while deleting snapshot on device '%s':" | |
1320 | " %s\n", | |
1321 | bdrv_get_device_name(bs), | |
1322 | error_get_pretty(err)); | |
1323 | error_free(err); | |
a672b469 AL |
1324 | } |
1325 | } | |
1326 | } | |
1327 | } | |
1328 | ||
1ce6be24 | 1329 | void hmp_info_snapshots(Monitor *mon, const QDict *qdict) |
a672b469 AL |
1330 | { |
1331 | BlockDriverState *bs, *bs1; | |
f9209915 MDCF |
1332 | QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s; |
1333 | int nb_sns, i, ret, available; | |
1334 | int total; | |
1335 | int *available_snapshots; | |
a672b469 | 1336 | |
29d78271 | 1337 | bs = find_vmstate_bs(); |
a672b469 | 1338 | if (!bs) { |
376253ec | 1339 | monitor_printf(mon, "No available block device supports snapshots\n"); |
a672b469 AL |
1340 | return; |
1341 | } | |
a672b469 AL |
1342 | |
1343 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
1344 | if (nb_sns < 0) { | |
376253ec | 1345 | monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns); |
a672b469 AL |
1346 | return; |
1347 | } | |
f9209915 MDCF |
1348 | |
1349 | if (nb_sns == 0) { | |
1350 | monitor_printf(mon, "There is no snapshot available.\n"); | |
1351 | return; | |
1352 | } | |
1353 | ||
7267c094 | 1354 | available_snapshots = g_malloc0(sizeof(int) * nb_sns); |
f9209915 MDCF |
1355 | total = 0; |
1356 | for (i = 0; i < nb_sns; i++) { | |
a672b469 | 1357 | sn = &sn_tab[i]; |
f9209915 MDCF |
1358 | available = 1; |
1359 | bs1 = NULL; | |
1360 | ||
1361 | while ((bs1 = bdrv_next(bs1))) { | |
1362 | if (bdrv_can_snapshot(bs1) && bs1 != bs) { | |
1363 | ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str); | |
1364 | if (ret < 0) { | |
1365 | available = 0; | |
1366 | break; | |
1367 | } | |
1368 | } | |
1369 | } | |
1370 | ||
1371 | if (available) { | |
1372 | available_snapshots[total] = i; | |
1373 | total++; | |
1374 | } | |
a672b469 | 1375 | } |
f9209915 MDCF |
1376 | |
1377 | if (total > 0) { | |
5b917044 WX |
1378 | bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL); |
1379 | monitor_printf(mon, "\n"); | |
f9209915 MDCF |
1380 | for (i = 0; i < total; i++) { |
1381 | sn = &sn_tab[available_snapshots[i]]; | |
5b917044 WX |
1382 | bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn); |
1383 | monitor_printf(mon, "\n"); | |
f9209915 MDCF |
1384 | } |
1385 | } else { | |
1386 | monitor_printf(mon, "There is no suitable snapshot available\n"); | |
1387 | } | |
1388 | ||
7267c094 AL |
1389 | g_free(sn_tab); |
1390 | g_free(available_snapshots); | |
f9209915 | 1391 | |
a672b469 | 1392 | } |
c5705a77 AK |
1393 | |
1394 | void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev) | |
1395 | { | |
1ddde087 | 1396 | qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK, |
c5705a77 AK |
1397 | memory_region_name(mr), dev); |
1398 | } | |
1399 | ||
1400 | void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev) | |
1401 | { | |
b0e56e0b | 1402 | qemu_ram_unset_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK); |
c5705a77 AK |
1403 | } |
1404 | ||
1405 | void vmstate_register_ram_global(MemoryRegion *mr) | |
1406 | { | |
1407 | vmstate_register_ram(mr, NULL); | |
1408 | } |