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