]>
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" |
88c16567 | 31 | #include "hw/xen/xen.h" |
1422e32d | 32 | #include "net/net.h" |
6666c96a | 33 | #include "migration.h" |
5e22479a | 34 | #include "migration/snapshot.h" |
f8d806c9 | 35 | #include "migration/misc.h" |
f2a8f0a6 | 36 | #include "migration/register.h" |
84a899de | 37 | #include "migration/global_state.h" |
7b1e1a22 | 38 | #include "ram.h" |
40014d81 | 39 | #include "qemu-file-channel.h" |
08a0aee1 | 40 | #include "qemu-file.h" |
20a519a0 | 41 | #include "savevm.h" |
be07b0ac | 42 | #include "postcopy-ram.h" |
cc7a8ea7 | 43 | #include "qapi/qmp/qerror.h" |
d49b6836 | 44 | #include "qemu/error-report.h" |
9c17d615 | 45 | #include "sysemu/cpus.h" |
022c62cb | 46 | #include "exec/memory.h" |
51180423 | 47 | #include "exec/target_page.h" |
a7ae8355 | 48 | #include "qmp-commands.h" |
517a13c9 | 49 | #include "trace.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 | 645 | void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque) |
41bd13af | 646 | { |
8718e999 | 647 | SaveStateEntry *se, *new_se; |
7685ee6a AW |
648 | char id[256] = ""; |
649 | ||
09e5ab63 AL |
650 | if (dev) { |
651 | char *path = qdev_get_dev_path(dev); | |
7685ee6a AW |
652 | if (path) { |
653 | pstrcpy(id, sizeof(id), path); | |
654 | pstrcat(id, sizeof(id), "/"); | |
7267c094 | 655 | g_free(path); |
7685ee6a AW |
656 | } |
657 | } | |
658 | pstrcat(id, sizeof(id), idstr); | |
41bd13af | 659 | |
0163a2e0 | 660 | QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { |
7685ee6a | 661 | if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { |
0163a2e0 | 662 | QTAILQ_REMOVE(&savevm_state.handlers, se, entry); |
ef1e1e07 | 663 | g_free(se->compat); |
7267c094 | 664 | g_free(se); |
41bd13af | 665 | } |
41bd13af AL |
666 | } |
667 | } | |
668 | ||
0be71e32 | 669 | int vmstate_register_with_alias_id(DeviceState *dev, int instance_id, |
4d2ffa08 JK |
670 | const VMStateDescription *vmsd, |
671 | void *opaque, int alias_id, | |
bc5c4f21 DDAG |
672 | int required_for_version, |
673 | Error **errp) | |
9ed7d6ae | 674 | { |
8718e999 | 675 | SaveStateEntry *se; |
9ed7d6ae | 676 | |
4d2ffa08 JK |
677 | /* If this triggers, alias support can be dropped for the vmsd. */ |
678 | assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); | |
679 | ||
97f3ad35 | 680 | se = g_new0(SaveStateEntry, 1); |
9ed7d6ae | 681 | se->version_id = vmsd->version_id; |
0163a2e0 | 682 | se->section_id = savevm_state.global_section_id++; |
9ed7d6ae JQ |
683 | se->opaque = opaque; |
684 | se->vmsd = vmsd; | |
4d2ffa08 | 685 | se->alias_id = alias_id; |
9ed7d6ae | 686 | |
09e5ab63 AL |
687 | if (dev) { |
688 | char *id = qdev_get_dev_path(dev); | |
7685ee6a | 689 | if (id) { |
581f08ba DDAG |
690 | if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >= |
691 | sizeof(se->idstr)) { | |
692 | error_setg(errp, "Path too long for VMState (%s)", id); | |
693 | g_free(id); | |
694 | g_free(se); | |
695 | ||
696 | return -1; | |
697 | } | |
128e4e10 | 698 | g_free(id); |
7685ee6a | 699 | |
97f3ad35 | 700 | se->compat = g_new0(CompatEntry, 1); |
7685ee6a AW |
701 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); |
702 | se->compat->instance_id = instance_id == -1 ? | |
703 | calculate_compat_instance_id(vmsd->name) : instance_id; | |
704 | instance_id = -1; | |
705 | } | |
706 | } | |
707 | pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); | |
708 | ||
8718e999 | 709 | if (instance_id == -1) { |
7685ee6a | 710 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
711 | } else { |
712 | se->instance_id = instance_id; | |
9ed7d6ae | 713 | } |
7685ee6a | 714 | assert(!se->compat || se->instance_id == 0); |
f37bc036 | 715 | savevm_state_handler_insert(se); |
9ed7d6ae JQ |
716 | return 0; |
717 | } | |
718 | ||
0be71e32 AW |
719 | void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd, |
720 | void *opaque) | |
9ed7d6ae | 721 | { |
1eb7538b JQ |
722 | SaveStateEntry *se, *new_se; |
723 | ||
0163a2e0 | 724 | QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { |
1eb7538b | 725 | if (se->vmsd == vmsd && se->opaque == opaque) { |
0163a2e0 | 726 | QTAILQ_REMOVE(&savevm_state.handlers, se, entry); |
ef1e1e07 | 727 | g_free(se->compat); |
7267c094 | 728 | g_free(se); |
1eb7538b JQ |
729 | } |
730 | } | |
9ed7d6ae JQ |
731 | } |
732 | ||
3a011c26 | 733 | static int vmstate_load(QEMUFile *f, SaveStateEntry *se) |
4082be4d | 734 | { |
9013dca5 | 735 | trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); |
9ed7d6ae | 736 | if (!se->vmsd) { /* Old style */ |
3a011c26 | 737 | return se->ops->load_state(f, se->opaque, se->load_version_id); |
9ed7d6ae | 738 | } |
3a011c26 | 739 | return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id); |
4082be4d JQ |
740 | } |
741 | ||
8118f095 AG |
742 | static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) |
743 | { | |
744 | int64_t old_offset, size; | |
745 | ||
746 | old_offset = qemu_ftell_fast(f); | |
747 | se->ops->save_state(f, se->opaque); | |
748 | size = qemu_ftell_fast(f) - old_offset; | |
749 | ||
750 | if (vmdesc) { | |
751 | json_prop_int(vmdesc, "size", size); | |
752 | json_start_array(vmdesc, "fields"); | |
753 | json_start_object(vmdesc, NULL); | |
754 | json_prop_str(vmdesc, "name", "data"); | |
755 | json_prop_int(vmdesc, "size", size); | |
756 | json_prop_str(vmdesc, "type", "buffer"); | |
757 | json_end_object(vmdesc); | |
758 | json_end_array(vmdesc); | |
759 | } | |
760 | } | |
761 | ||
762 | static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) | |
4082be4d | 763 | { |
9013dca5 | 764 | trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); |
8118f095 AG |
765 | if (!se->vmsd) { |
766 | vmstate_save_old_style(f, se, vmdesc); | |
dc912121 | 767 | return; |
9ed7d6ae | 768 | } |
8118f095 | 769 | vmstate_save_state(f, se->vmsd, se->opaque, vmdesc); |
4082be4d JQ |
770 | } |
771 | ||
37fb569c DDAG |
772 | void savevm_skip_section_footers(void) |
773 | { | |
774 | skip_section_footers = true; | |
775 | } | |
776 | ||
ce39bfc9 DDAG |
777 | /* |
778 | * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL) | |
779 | */ | |
780 | static void save_section_header(QEMUFile *f, SaveStateEntry *se, | |
781 | uint8_t section_type) | |
782 | { | |
783 | qemu_put_byte(f, section_type); | |
784 | qemu_put_be32(f, se->section_id); | |
785 | ||
786 | if (section_type == QEMU_VM_SECTION_FULL || | |
787 | section_type == QEMU_VM_SECTION_START) { | |
788 | /* ID string */ | |
789 | size_t len = strlen(se->idstr); | |
790 | qemu_put_byte(f, len); | |
791 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); | |
792 | ||
793 | qemu_put_be32(f, se->instance_id); | |
794 | qemu_put_be32(f, se->version_id); | |
795 | } | |
796 | } | |
797 | ||
f68945d4 DDAG |
798 | /* |
799 | * Write a footer onto device sections that catches cases misformatted device | |
800 | * sections. | |
801 | */ | |
802 | static void save_section_footer(QEMUFile *f, SaveStateEntry *se) | |
803 | { | |
804 | if (!skip_section_footers) { | |
805 | qemu_put_byte(f, QEMU_VM_SECTION_FOOTER); | |
806 | qemu_put_be32(f, se->section_id); | |
807 | } | |
808 | } | |
809 | ||
c76ca188 DDAG |
810 | /** |
811 | * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the | |
812 | * command and associated data. | |
813 | * | |
814 | * @f: File to send command on | |
815 | * @command: Command type to send | |
816 | * @len: Length of associated data | |
817 | * @data: Data associated with command. | |
818 | */ | |
20a519a0 JQ |
819 | static void qemu_savevm_command_send(QEMUFile *f, |
820 | enum qemu_vm_cmd command, | |
821 | uint16_t len, | |
822 | uint8_t *data) | |
c76ca188 DDAG |
823 | { |
824 | trace_savevm_command_send(command, len); | |
825 | qemu_put_byte(f, QEMU_VM_COMMAND); | |
826 | qemu_put_be16(f, (uint16_t)command); | |
827 | qemu_put_be16(f, len); | |
828 | qemu_put_buffer(f, data, len); | |
829 | qemu_fflush(f); | |
830 | } | |
831 | ||
2e37701e DDAG |
832 | void qemu_savevm_send_ping(QEMUFile *f, uint32_t value) |
833 | { | |
834 | uint32_t buf; | |
835 | ||
836 | trace_savevm_send_ping(value); | |
837 | buf = cpu_to_be32(value); | |
838 | qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf); | |
839 | } | |
840 | ||
841 | void qemu_savevm_send_open_return_path(QEMUFile *f) | |
842 | { | |
843 | trace_savevm_send_open_return_path(); | |
844 | qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL); | |
845 | } | |
846 | ||
11cf1d98 DDAG |
847 | /* We have a buffer of data to send; we don't want that all to be loaded |
848 | * by the command itself, so the command contains just the length of the | |
849 | * extra buffer that we then send straight after it. | |
850 | * TODO: Must be a better way to organise that | |
851 | * | |
852 | * Returns: | |
853 | * 0 on success | |
854 | * -ve on error | |
855 | */ | |
61b67d47 | 856 | int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len) |
11cf1d98 | 857 | { |
11cf1d98 DDAG |
858 | uint32_t tmp; |
859 | ||
860 | if (len > MAX_VM_CMD_PACKAGED_SIZE) { | |
861 | error_report("%s: Unreasonably large packaged state: %zu", | |
862 | __func__, len); | |
863 | return -1; | |
864 | } | |
865 | ||
866 | tmp = cpu_to_be32(len); | |
867 | ||
868 | trace_qemu_savevm_send_packaged(); | |
869 | qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp); | |
870 | ||
61b67d47 | 871 | qemu_put_buffer(f, buf, len); |
11cf1d98 DDAG |
872 | |
873 | return 0; | |
874 | } | |
875 | ||
093e3c42 DDAG |
876 | /* Send prior to any postcopy transfer */ |
877 | void qemu_savevm_send_postcopy_advise(QEMUFile *f) | |
878 | { | |
879 | uint64_t tmp[2]; | |
e8ca1db2 | 880 | tmp[0] = cpu_to_be64(ram_pagesize_summary()); |
20afaed9 | 881 | tmp[1] = cpu_to_be64(qemu_target_page_size()); |
093e3c42 DDAG |
882 | |
883 | trace_qemu_savevm_send_postcopy_advise(); | |
884 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 16, (uint8_t *)tmp); | |
885 | } | |
886 | ||
887 | /* Sent prior to starting the destination running in postcopy, discard pages | |
888 | * that have already been sent but redirtied on the source. | |
889 | * CMD_POSTCOPY_RAM_DISCARD consist of: | |
890 | * byte version (0) | |
891 | * byte Length of name field (not including 0) | |
892 | * n x byte RAM block name | |
893 | * byte 0 terminator (just for safety) | |
894 | * n x Byte ranges within the named RAMBlock | |
895 | * be64 Start of the range | |
896 | * be64 Length | |
897 | * | |
898 | * name: RAMBlock name that these entries are part of | |
899 | * len: Number of page entries | |
900 | * start_list: 'len' addresses | |
901 | * length_list: 'len' addresses | |
902 | * | |
903 | */ | |
904 | void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name, | |
905 | uint16_t len, | |
906 | uint64_t *start_list, | |
907 | uint64_t *length_list) | |
908 | { | |
909 | uint8_t *buf; | |
910 | uint16_t tmplen; | |
911 | uint16_t t; | |
912 | size_t name_len = strlen(name); | |
913 | ||
914 | trace_qemu_savevm_send_postcopy_ram_discard(name, len); | |
915 | assert(name_len < 256); | |
916 | buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len); | |
917 | buf[0] = postcopy_ram_discard_version; | |
918 | buf[1] = name_len; | |
919 | memcpy(buf + 2, name, name_len); | |
920 | tmplen = 2 + name_len; | |
921 | buf[tmplen++] = '\0'; | |
922 | ||
923 | for (t = 0; t < len; t++) { | |
4d885131 | 924 | stq_be_p(buf + tmplen, start_list[t]); |
093e3c42 | 925 | tmplen += 8; |
4d885131 | 926 | stq_be_p(buf + tmplen, length_list[t]); |
093e3c42 DDAG |
927 | tmplen += 8; |
928 | } | |
929 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf); | |
930 | g_free(buf); | |
931 | } | |
932 | ||
933 | /* Get the destination into a state where it can receive postcopy data. */ | |
934 | void qemu_savevm_send_postcopy_listen(QEMUFile *f) | |
935 | { | |
936 | trace_savevm_send_postcopy_listen(); | |
937 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL); | |
938 | } | |
939 | ||
940 | /* Kick the destination into running */ | |
941 | void qemu_savevm_send_postcopy_run(QEMUFile *f) | |
942 | { | |
943 | trace_savevm_send_postcopy_run(); | |
944 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL); | |
945 | } | |
946 | ||
e1c37d0e | 947 | bool qemu_savevm_state_blocked(Error **errp) |
dc912121 AW |
948 | { |
949 | SaveStateEntry *se; | |
950 | ||
0163a2e0 | 951 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
7d854c47 | 952 | if (se->vmsd && se->vmsd->unmigratable) { |
f231b88d CR |
953 | error_setg(errp, "State blocked by non-migratable device '%s'", |
954 | se->idstr); | |
dc912121 AW |
955 | return true; |
956 | } | |
957 | } | |
958 | return false; | |
959 | } | |
960 | ||
902c053d GK |
961 | static bool enforce_config_section(void) |
962 | { | |
963 | MachineState *machine = MACHINE(qdev_get_machine()); | |
964 | return machine->enforce_config_section; | |
965 | } | |
966 | ||
f796baa1 DDAG |
967 | void qemu_savevm_state_header(QEMUFile *f) |
968 | { | |
969 | trace_savevm_state_header(); | |
970 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
971 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
172dfd4f | 972 | |
902c053d | 973 | if (!savevm_state.skip_configuration || enforce_config_section()) { |
172dfd4f DDAG |
974 | qemu_put_byte(f, QEMU_VM_CONFIGURATION); |
975 | vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0); | |
976 | } | |
977 | ||
f796baa1 DDAG |
978 | } |
979 | ||
a0762d9e | 980 | void qemu_savevm_state_begin(QEMUFile *f) |
a672b469 AL |
981 | { |
982 | SaveStateEntry *se; | |
39346385 | 983 | int ret; |
a672b469 | 984 | |
9013dca5 | 985 | trace_savevm_state_begin(); |
0163a2e0 | 986 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
d1315aac | 987 | if (!se->ops || !se->ops->save_live_setup) { |
a672b469 | 988 | continue; |
22ea40f4 | 989 | } |
6bd68781 JQ |
990 | if (se->ops && se->ops->is_active) { |
991 | if (!se->ops->is_active(se->opaque)) { | |
992 | continue; | |
993 | } | |
994 | } | |
ce39bfc9 | 995 | save_section_header(f, se, QEMU_VM_SECTION_START); |
a672b469 | 996 | |
d1315aac | 997 | ret = se->ops->save_live_setup(f, se->opaque); |
f68945d4 | 998 | save_section_footer(f, se); |
2975725f | 999 | if (ret < 0) { |
47c8c17a PB |
1000 | qemu_file_set_error(f, ret); |
1001 | break; | |
2975725f | 1002 | } |
a672b469 | 1003 | } |
a672b469 AL |
1004 | } |
1005 | ||
39346385 | 1006 | /* |
07f35073 | 1007 | * this function has three return values: |
39346385 JQ |
1008 | * negative: there was one error, and we have -errno. |
1009 | * 0 : We haven't finished, caller have to go again | |
1010 | * 1 : We have finished, we can go to complete phase | |
1011 | */ | |
35ecd943 | 1012 | int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy) |
a672b469 AL |
1013 | { |
1014 | SaveStateEntry *se; | |
1015 | int ret = 1; | |
1016 | ||
9013dca5 | 1017 | trace_savevm_state_iterate(); |
0163a2e0 | 1018 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
16310a3c | 1019 | if (!se->ops || !se->ops->save_live_iterate) { |
a672b469 | 1020 | continue; |
22ea40f4 | 1021 | } |
6bd68781 JQ |
1022 | if (se->ops && se->ops->is_active) { |
1023 | if (!se->ops->is_active(se->opaque)) { | |
1024 | continue; | |
1025 | } | |
1026 | } | |
35ecd943 DDAG |
1027 | /* |
1028 | * In the postcopy phase, any device that doesn't know how to | |
1029 | * do postcopy should have saved it's state in the _complete | |
1030 | * call that's already run, it might get confused if we call | |
1031 | * iterate afterwards. | |
1032 | */ | |
1033 | if (postcopy && !se->ops->save_live_complete_postcopy) { | |
1034 | continue; | |
1035 | } | |
aac844ed JQ |
1036 | if (qemu_file_rate_limit(f)) { |
1037 | return 0; | |
1038 | } | |
464400f6 | 1039 | trace_savevm_section_start(se->idstr, se->section_id); |
ce39bfc9 DDAG |
1040 | |
1041 | save_section_header(f, se, QEMU_VM_SECTION_PART); | |
a672b469 | 1042 | |
16310a3c | 1043 | ret = se->ops->save_live_iterate(f, se->opaque); |
a5df2a02 | 1044 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
f68945d4 | 1045 | save_section_footer(f, se); |
517a13c9 | 1046 | |
47c8c17a PB |
1047 | if (ret < 0) { |
1048 | qemu_file_set_error(f, ret); | |
1049 | } | |
2975725f | 1050 | if (ret <= 0) { |
90697be8 JK |
1051 | /* Do not proceed to the next vmstate before this one reported |
1052 | completion of the current stage. This serializes the migration | |
1053 | and reduces the probability that a faster changing state is | |
1054 | synchronized over and over again. */ | |
1055 | break; | |
1056 | } | |
a672b469 | 1057 | } |
39346385 | 1058 | return ret; |
a672b469 AL |
1059 | } |
1060 | ||
9850c604 AG |
1061 | static bool should_send_vmdesc(void) |
1062 | { | |
1063 | MachineState *machine = MACHINE(qdev_get_machine()); | |
5727309d | 1064 | bool in_postcopy = migration_in_postcopy(); |
8421b205 | 1065 | return !machine->suppress_vmdesc && !in_postcopy; |
9850c604 AG |
1066 | } |
1067 | ||
763c906b DDAG |
1068 | /* |
1069 | * Calls the save_live_complete_postcopy methods | |
1070 | * causing the last few pages to be sent immediately and doing any associated | |
1071 | * cleanup. | |
1072 | * Note postcopy also calls qemu_savevm_state_complete_precopy to complete | |
1073 | * all the other devices, but that happens at the point we switch to postcopy. | |
1074 | */ | |
1075 | void qemu_savevm_state_complete_postcopy(QEMUFile *f) | |
1076 | { | |
1077 | SaveStateEntry *se; | |
1078 | int ret; | |
1079 | ||
1080 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
1081 | if (!se->ops || !se->ops->save_live_complete_postcopy) { | |
1082 | continue; | |
1083 | } | |
1084 | if (se->ops && se->ops->is_active) { | |
1085 | if (!se->ops->is_active(se->opaque)) { | |
1086 | continue; | |
1087 | } | |
1088 | } | |
1089 | trace_savevm_section_start(se->idstr, se->section_id); | |
1090 | /* Section type */ | |
1091 | qemu_put_byte(f, QEMU_VM_SECTION_END); | |
1092 | qemu_put_be32(f, se->section_id); | |
1093 | ||
1094 | ret = se->ops->save_live_complete_postcopy(f, se->opaque); | |
1095 | trace_savevm_section_end(se->idstr, se->section_id, ret); | |
1096 | save_section_footer(f, se); | |
1097 | if (ret < 0) { | |
1098 | qemu_file_set_error(f, ret); | |
1099 | return; | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | qemu_put_byte(f, QEMU_VM_EOF); | |
1104 | qemu_fflush(f); | |
1105 | } | |
1106 | ||
1c0d249d | 1107 | void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only) |
a672b469 | 1108 | { |
8118f095 AG |
1109 | QJSON *vmdesc; |
1110 | int vmdesc_len; | |
a672b469 | 1111 | SaveStateEntry *se; |
2975725f | 1112 | int ret; |
5727309d | 1113 | bool in_postcopy = migration_in_postcopy(); |
a672b469 | 1114 | |
a3e06c3d | 1115 | trace_savevm_state_complete_precopy(); |
9013dca5 | 1116 | |
ea375f9a JK |
1117 | cpu_synchronize_all_states(); |
1118 | ||
0163a2e0 | 1119 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
763c906b DDAG |
1120 | if (!se->ops || |
1121 | (in_postcopy && se->ops->save_live_complete_postcopy) || | |
1c0d249d | 1122 | (in_postcopy && !iterable_only) || |
763c906b | 1123 | !se->ops->save_live_complete_precopy) { |
a672b469 | 1124 | continue; |
22ea40f4 | 1125 | } |
1c0d249d | 1126 | |
6bd68781 JQ |
1127 | if (se->ops && se->ops->is_active) { |
1128 | if (!se->ops->is_active(se->opaque)) { | |
1129 | continue; | |
1130 | } | |
1131 | } | |
464400f6 | 1132 | trace_savevm_section_start(se->idstr, se->section_id); |
ce39bfc9 DDAG |
1133 | |
1134 | save_section_header(f, se, QEMU_VM_SECTION_END); | |
a672b469 | 1135 | |
a3e06c3d | 1136 | ret = se->ops->save_live_complete_precopy(f, se->opaque); |
a5df2a02 | 1137 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
f68945d4 | 1138 | save_section_footer(f, se); |
2975725f | 1139 | if (ret < 0) { |
47c8c17a PB |
1140 | qemu_file_set_error(f, ret); |
1141 | return; | |
2975725f | 1142 | } |
a672b469 AL |
1143 | } |
1144 | ||
1c0d249d DDAG |
1145 | if (iterable_only) { |
1146 | return; | |
1147 | } | |
1148 | ||
8118f095 | 1149 | vmdesc = qjson_new(); |
46d702b1 | 1150 | json_prop_int(vmdesc, "page_size", qemu_target_page_size()); |
8118f095 | 1151 | json_start_array(vmdesc, "devices"); |
0163a2e0 | 1152 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
a672b469 | 1153 | |
22ea40f4 | 1154 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
5cecf414 | 1155 | continue; |
22ea40f4 | 1156 | } |
df896152 JQ |
1157 | if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) { |
1158 | trace_savevm_section_skip(se->idstr, se->section_id); | |
1159 | continue; | |
1160 | } | |
1161 | ||
464400f6 | 1162 | trace_savevm_section_start(se->idstr, se->section_id); |
8118f095 AG |
1163 | |
1164 | json_start_object(vmdesc, NULL); | |
1165 | json_prop_str(vmdesc, "name", se->idstr); | |
1166 | json_prop_int(vmdesc, "instance_id", se->instance_id); | |
1167 | ||
ce39bfc9 | 1168 | save_section_header(f, se, QEMU_VM_SECTION_FULL); |
8118f095 | 1169 | vmstate_save(f, se, vmdesc); |
a5df2a02 | 1170 | trace_savevm_section_end(se->idstr, se->section_id, 0); |
f68945d4 | 1171 | save_section_footer(f, se); |
bdf46d64 WY |
1172 | |
1173 | json_end_object(vmdesc); | |
a672b469 AL |
1174 | } |
1175 | ||
763c906b DDAG |
1176 | if (!in_postcopy) { |
1177 | /* Postcopy stream will still be going */ | |
1178 | qemu_put_byte(f, QEMU_VM_EOF); | |
1179 | } | |
8118f095 AG |
1180 | |
1181 | json_end_array(vmdesc); | |
1182 | qjson_finish(vmdesc); | |
1183 | vmdesc_len = strlen(qjson_get_str(vmdesc)); | |
1184 | ||
9850c604 AG |
1185 | if (should_send_vmdesc()) { |
1186 | qemu_put_byte(f, QEMU_VM_VMDESCRIPTION); | |
1187 | qemu_put_be32(f, vmdesc_len); | |
1188 | qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len); | |
1189 | } | |
b72fe9e6 | 1190 | qjson_destroy(vmdesc); |
8118f095 | 1191 | |
edaae611 | 1192 | qemu_fflush(f); |
a672b469 AL |
1193 | } |
1194 | ||
c31b098f DDAG |
1195 | /* Give an estimate of the amount left to be transferred, |
1196 | * the result is split into the amount for units that can and | |
1197 | * for units that can't do postcopy. | |
1198 | */ | |
faec066a | 1199 | void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size, |
c31b098f DDAG |
1200 | uint64_t *res_non_postcopiable, |
1201 | uint64_t *res_postcopiable) | |
e4ed1541 JQ |
1202 | { |
1203 | SaveStateEntry *se; | |
c31b098f DDAG |
1204 | |
1205 | *res_non_postcopiable = 0; | |
1206 | *res_postcopiable = 0; | |
1207 | ||
e4ed1541 | 1208 | |
0163a2e0 | 1209 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
e4ed1541 JQ |
1210 | if (!se->ops || !se->ops->save_live_pending) { |
1211 | continue; | |
1212 | } | |
1213 | if (se->ops && se->ops->is_active) { | |
1214 | if (!se->ops->is_active(se->opaque)) { | |
1215 | continue; | |
1216 | } | |
1217 | } | |
faec066a | 1218 | se->ops->save_live_pending(f, se->opaque, threshold_size, |
c31b098f | 1219 | res_non_postcopiable, res_postcopiable); |
e4ed1541 | 1220 | } |
e4ed1541 JQ |
1221 | } |
1222 | ||
ea7415fa | 1223 | void qemu_savevm_state_cleanup(void) |
4ec7fcc7 JK |
1224 | { |
1225 | SaveStateEntry *se; | |
1226 | ||
ea7415fa | 1227 | trace_savevm_state_cleanup(); |
0163a2e0 | 1228 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
d1a8548c LL |
1229 | if (se->ops && se->ops->cleanup) { |
1230 | se->ops->cleanup(se->opaque); | |
4ec7fcc7 JK |
1231 | } |
1232 | } | |
1233 | } | |
1234 | ||
5d80448c | 1235 | static int qemu_savevm_state(QEMUFile *f, Error **errp) |
a672b469 | 1236 | { |
a672b469 | 1237 | int ret; |
a0762d9e | 1238 | MigrationState *ms = migrate_init(); |
6dcf6668 | 1239 | MigrationStatus status; |
89a02a9f | 1240 | ms->to_dst_file = f; |
a672b469 | 1241 | |
24f3902b | 1242 | if (migration_is_blocked(errp)) { |
6dcf6668 DL |
1243 | ret = -EINVAL; |
1244 | goto done; | |
dc912121 AW |
1245 | } |
1246 | ||
ce7c817c JQ |
1247 | if (migrate_use_block()) { |
1248 | error_setg(errp, "Block migration and snapshots are incompatible"); | |
1249 | ret = -EINVAL; | |
1250 | goto done; | |
1251 | } | |
1252 | ||
9b095037 | 1253 | qemu_mutex_unlock_iothread(); |
f796baa1 | 1254 | qemu_savevm_state_header(f); |
a0762d9e | 1255 | qemu_savevm_state_begin(f); |
9b095037 PB |
1256 | qemu_mutex_lock_iothread(); |
1257 | ||
47c8c17a | 1258 | while (qemu_file_get_error(f) == 0) { |
35ecd943 | 1259 | if (qemu_savevm_state_iterate(f, false) > 0) { |
47c8c17a PB |
1260 | break; |
1261 | } | |
1262 | } | |
a672b469 | 1263 | |
47c8c17a | 1264 | ret = qemu_file_get_error(f); |
39346385 | 1265 | if (ret == 0) { |
1c0d249d | 1266 | qemu_savevm_state_complete_precopy(f, false); |
624b9cc2 | 1267 | ret = qemu_file_get_error(f); |
39346385 | 1268 | } |
15b3b8ea | 1269 | qemu_savevm_state_cleanup(); |
04943eba | 1270 | if (ret != 0) { |
5d80448c | 1271 | error_setg_errno(errp, -ret, "Error while writing VM state"); |
04943eba | 1272 | } |
6dcf6668 DL |
1273 | |
1274 | done: | |
1275 | if (ret != 0) { | |
1276 | status = MIGRATION_STATUS_FAILED; | |
1277 | } else { | |
1278 | status = MIGRATION_STATUS_COMPLETED; | |
1279 | } | |
1280 | migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status); | |
f9c8caa0 VSO |
1281 | |
1282 | /* f is outer parameter, it should not stay in global migration state after | |
1283 | * this function finished */ | |
1284 | ms->to_dst_file = NULL; | |
1285 | ||
a672b469 AL |
1286 | return ret; |
1287 | } | |
1288 | ||
a7ae8355 SS |
1289 | static int qemu_save_device_state(QEMUFile *f) |
1290 | { | |
1291 | SaveStateEntry *se; | |
1292 | ||
1293 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
1294 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
1295 | ||
1296 | cpu_synchronize_all_states(); | |
1297 | ||
0163a2e0 | 1298 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
a7ae8355 SS |
1299 | if (se->is_ram) { |
1300 | continue; | |
1301 | } | |
22ea40f4 | 1302 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
a7ae8355 SS |
1303 | continue; |
1304 | } | |
df896152 JQ |
1305 | if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) { |
1306 | continue; | |
1307 | } | |
a7ae8355 | 1308 | |
ce39bfc9 | 1309 | save_section_header(f, se, QEMU_VM_SECTION_FULL); |
a7ae8355 | 1310 | |
8118f095 | 1311 | vmstate_save(f, se, NULL); |
f68945d4 DDAG |
1312 | |
1313 | save_section_footer(f, se); | |
a7ae8355 SS |
1314 | } |
1315 | ||
1316 | qemu_put_byte(f, QEMU_VM_EOF); | |
1317 | ||
1318 | return qemu_file_get_error(f); | |
1319 | } | |
1320 | ||
a672b469 AL |
1321 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
1322 | { | |
1323 | SaveStateEntry *se; | |
1324 | ||
0163a2e0 | 1325 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
a672b469 | 1326 | if (!strcmp(se->idstr, idstr) && |
4d2ffa08 JK |
1327 | (instance_id == se->instance_id || |
1328 | instance_id == se->alias_id)) | |
a672b469 | 1329 | return se; |
7685ee6a AW |
1330 | /* Migrating from an older version? */ |
1331 | if (strstr(se->idstr, idstr) && se->compat) { | |
1332 | if (!strcmp(se->compat->idstr, idstr) && | |
1333 | (instance_id == se->compat->instance_id || | |
1334 | instance_id == se->alias_id)) | |
1335 | return se; | |
1336 | } | |
a672b469 AL |
1337 | } |
1338 | return NULL; | |
1339 | } | |
1340 | ||
7b89bf27 DDAG |
1341 | enum LoadVMExitCodes { |
1342 | /* Allow a command to quit all layers of nested loadvm loops */ | |
1343 | LOADVM_QUIT = 1, | |
1344 | }; | |
1345 | ||
1346 | static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis); | |
093e3c42 DDAG |
1347 | |
1348 | /* ------ incoming postcopy messages ------ */ | |
1349 | /* 'advise' arrives before any transfers just to tell us that a postcopy | |
1350 | * *might* happen - it might be skipped if precopy transferred everything | |
1351 | * quickly. | |
1352 | */ | |
1353 | static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis) | |
1354 | { | |
1355 | PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE); | |
e8ca1db2 | 1356 | uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps; |
093e3c42 DDAG |
1357 | |
1358 | trace_loadvm_postcopy_handle_advise(); | |
1359 | if (ps != POSTCOPY_INCOMING_NONE) { | |
1360 | error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps); | |
1361 | return -1; | |
1362 | } | |
1363 | ||
eb59db53 | 1364 | if (!postcopy_ram_supported_by_host()) { |
328d4d85 | 1365 | postcopy_state_set(POSTCOPY_INCOMING_NONE); |
eb59db53 DDAG |
1366 | return -1; |
1367 | } | |
1368 | ||
e8ca1db2 DDAG |
1369 | remote_pagesize_summary = qemu_get_be64(mis->from_src_file); |
1370 | local_pagesize_summary = ram_pagesize_summary(); | |
1371 | ||
1372 | if (remote_pagesize_summary != local_pagesize_summary) { | |
093e3c42 | 1373 | /* |
e8ca1db2 DDAG |
1374 | * This detects two potential causes of mismatch: |
1375 | * a) A mismatch in host page sizes | |
1376 | * Some combinations of mismatch are probably possible but it gets | |
1377 | * a bit more complicated. In particular we need to place whole | |
1378 | * host pages on the dest at once, and we need to ensure that we | |
1379 | * handle dirtying to make sure we never end up sending part of | |
1380 | * a hostpage on it's own. | |
1381 | * b) The use of different huge page sizes on source/destination | |
1382 | * a more fine grain test is performed during RAM block migration | |
1383 | * but this test here causes a nice early clear failure, and | |
1384 | * also fails when passed to an older qemu that doesn't | |
1385 | * do huge pages. | |
093e3c42 | 1386 | */ |
e8ca1db2 DDAG |
1387 | error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64 |
1388 | " d=%" PRIx64 ")", | |
1389 | remote_pagesize_summary, local_pagesize_summary); | |
093e3c42 DDAG |
1390 | return -1; |
1391 | } | |
1392 | ||
1393 | remote_tps = qemu_get_be64(mis->from_src_file); | |
20afaed9 | 1394 | if (remote_tps != qemu_target_page_size()) { |
093e3c42 DDAG |
1395 | /* |
1396 | * Again, some differences could be dealt with, but for now keep it | |
1397 | * simple. | |
1398 | */ | |
20afaed9 JQ |
1399 | error_report("Postcopy needs matching target page sizes (s=%d d=%zd)", |
1400 | (int)remote_tps, qemu_target_page_size()); | |
093e3c42 DDAG |
1401 | return -1; |
1402 | } | |
1403 | ||
1caddf8a DDAG |
1404 | if (ram_postcopy_incoming_init(mis)) { |
1405 | return -1; | |
1406 | } | |
1407 | ||
1408 | postcopy_state_set(POSTCOPY_INCOMING_ADVISE); | |
1409 | ||
093e3c42 DDAG |
1410 | return 0; |
1411 | } | |
1412 | ||
1413 | /* After postcopy we will be told to throw some pages away since they're | |
1414 | * dirty and will have to be demand fetched. Must happen before CPU is | |
1415 | * started. | |
1416 | * There can be 0..many of these messages, each encoding multiple pages. | |
1417 | */ | |
1418 | static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis, | |
1419 | uint16_t len) | |
1420 | { | |
1421 | int tmp; | |
1422 | char ramid[256]; | |
1423 | PostcopyState ps = postcopy_state_get(); | |
1424 | ||
1425 | trace_loadvm_postcopy_ram_handle_discard(); | |
1426 | ||
1427 | switch (ps) { | |
1428 | case POSTCOPY_INCOMING_ADVISE: | |
1429 | /* 1st discard */ | |
f9527107 | 1430 | tmp = postcopy_ram_prepare_discard(mis); |
093e3c42 DDAG |
1431 | if (tmp) { |
1432 | return tmp; | |
1433 | } | |
1434 | break; | |
1435 | ||
1436 | case POSTCOPY_INCOMING_DISCARD: | |
1437 | /* Expected state */ | |
1438 | break; | |
1439 | ||
1440 | default: | |
1441 | error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)", | |
1442 | ps); | |
1443 | return -1; | |
1444 | } | |
1445 | /* We're expecting a | |
1446 | * Version (0) | |
1447 | * a RAM ID string (length byte, name, 0 term) | |
1448 | * then at least 1 16 byte chunk | |
1449 | */ | |
1450 | if (len < (1 + 1 + 1 + 1 + 2 * 8)) { | |
1451 | error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len); | |
1452 | return -1; | |
1453 | } | |
1454 | ||
1455 | tmp = qemu_get_byte(mis->from_src_file); | |
1456 | if (tmp != postcopy_ram_discard_version) { | |
1457 | error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp); | |
1458 | return -1; | |
1459 | } | |
1460 | ||
1461 | if (!qemu_get_counted_string(mis->from_src_file, ramid)) { | |
1462 | error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID"); | |
1463 | return -1; | |
1464 | } | |
1465 | tmp = qemu_get_byte(mis->from_src_file); | |
1466 | if (tmp != 0) { | |
1467 | error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp); | |
1468 | return -1; | |
1469 | } | |
1470 | ||
1471 | len -= 3 + strlen(ramid); | |
1472 | if (len % 16) { | |
1473 | error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len); | |
1474 | return -1; | |
1475 | } | |
1476 | trace_loadvm_postcopy_ram_handle_discard_header(ramid, len); | |
1477 | while (len) { | |
093e3c42 DDAG |
1478 | uint64_t start_addr, block_length; |
1479 | start_addr = qemu_get_be64(mis->from_src_file); | |
1480 | block_length = qemu_get_be64(mis->from_src_file); | |
1481 | ||
1482 | len -= 16; | |
aaa2064c | 1483 | int ret = ram_discard_range(ramid, start_addr, block_length); |
093e3c42 DDAG |
1484 | if (ret) { |
1485 | return ret; | |
1486 | } | |
093e3c42 DDAG |
1487 | } |
1488 | trace_loadvm_postcopy_ram_handle_discard_end(); | |
1489 | ||
1490 | return 0; | |
1491 | } | |
1492 | ||
c76201ab DDAG |
1493 | /* |
1494 | * Triggered by a postcopy_listen command; this thread takes over reading | |
1495 | * the input stream, leaving the main thread free to carry on loading the rest | |
1496 | * of the device state (from RAM). | |
1497 | * (TODO:This could do with being in a postcopy file - but there again it's | |
1498 | * just another input loop, not that postcopy specific) | |
1499 | */ | |
1500 | static void *postcopy_ram_listen_thread(void *opaque) | |
1501 | { | |
1502 | QEMUFile *f = opaque; | |
1503 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
1504 | int load_res; | |
1505 | ||
6ba996bb DDAG |
1506 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, |
1507 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
c76201ab DDAG |
1508 | qemu_sem_post(&mis->listen_thread_sem); |
1509 | trace_postcopy_ram_listen_thread_start(); | |
1510 | ||
1511 | /* | |
1512 | * Because we're a thread and not a coroutine we can't yield | |
1513 | * in qemu_file, and thus we must be blocking now. | |
1514 | */ | |
1515 | qemu_file_set_blocking(f, true); | |
1516 | load_res = qemu_loadvm_state_main(f, mis); | |
1517 | /* And non-blocking again so we don't block in any cleanup */ | |
1518 | qemu_file_set_blocking(f, false); | |
1519 | ||
1520 | trace_postcopy_ram_listen_thread_exit(); | |
1521 | if (load_res < 0) { | |
1522 | error_report("%s: loadvm failed: %d", __func__, load_res); | |
1523 | qemu_file_set_error(f, load_res); | |
6ba996bb DDAG |
1524 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
1525 | MIGRATION_STATUS_FAILED); | |
c76201ab DDAG |
1526 | } else { |
1527 | /* | |
1528 | * This looks good, but it's possible that the device loading in the | |
1529 | * main thread hasn't finished yet, and so we might not be in 'RUN' | |
1530 | * state yet; wait for the end of the main thread. | |
1531 | */ | |
1532 | qemu_event_wait(&mis->main_thread_load_event); | |
1533 | } | |
1534 | postcopy_ram_incoming_cleanup(mis); | |
c76201ab DDAG |
1535 | |
1536 | if (load_res < 0) { | |
1537 | /* | |
1538 | * If something went wrong then we have a bad state so exit; | |
1539 | * depending how far we got it might be possible at this point | |
1540 | * to leave the guest running and fire MCEs for pages that never | |
1541 | * arrived as a desperate recovery step. | |
1542 | */ | |
1543 | exit(EXIT_FAILURE); | |
1544 | } | |
1545 | ||
6ba996bb DDAG |
1546 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
1547 | MIGRATION_STATUS_COMPLETED); | |
1548 | /* | |
1549 | * If everything has worked fine, then the main thread has waited | |
1550 | * for us to start, and we're the last use of the mis. | |
1551 | * (If something broke then qemu will have to exit anyway since it's | |
1552 | * got a bad migration state). | |
1553 | */ | |
1554 | migration_incoming_state_destroy(); | |
1555 | ||
1556 | ||
c76201ab DDAG |
1557 | return NULL; |
1558 | } | |
1559 | ||
093e3c42 DDAG |
1560 | /* After this message we must be able to immediately receive postcopy data */ |
1561 | static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis) | |
1562 | { | |
1563 | PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING); | |
1564 | trace_loadvm_postcopy_handle_listen(); | |
1565 | if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) { | |
1566 | error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps); | |
1567 | return -1; | |
1568 | } | |
f9527107 DDAG |
1569 | if (ps == POSTCOPY_INCOMING_ADVISE) { |
1570 | /* | |
1571 | * A rare case, we entered listen without having to do any discards, | |
1572 | * so do the setup that's normally done at the time of the 1st discard. | |
1573 | */ | |
1574 | postcopy_ram_prepare_discard(mis); | |
1575 | } | |
093e3c42 | 1576 | |
f0a227ad DDAG |
1577 | /* |
1578 | * Sensitise RAM - can now generate requests for blocks that don't exist | |
1579 | * However, at this point the CPU shouldn't be running, and the IO | |
1580 | * shouldn't be doing anything yet so don't actually expect requests | |
1581 | */ | |
1582 | if (postcopy_ram_enable_notify(mis)) { | |
1583 | return -1; | |
1584 | } | |
1585 | ||
c76201ab DDAG |
1586 | if (mis->have_listen_thread) { |
1587 | error_report("CMD_POSTCOPY_RAM_LISTEN already has a listen thread"); | |
1588 | return -1; | |
1589 | } | |
1590 | ||
1591 | mis->have_listen_thread = true; | |
1592 | /* Start up the listening thread and wait for it to signal ready */ | |
1593 | qemu_sem_init(&mis->listen_thread_sem, 0); | |
1594 | qemu_thread_create(&mis->listen_thread, "postcopy/listen", | |
1595 | postcopy_ram_listen_thread, mis->from_src_file, | |
a587a3fe | 1596 | QEMU_THREAD_DETACHED); |
c76201ab DDAG |
1597 | qemu_sem_wait(&mis->listen_thread_sem); |
1598 | qemu_sem_destroy(&mis->listen_thread_sem); | |
1599 | ||
093e3c42 DDAG |
1600 | return 0; |
1601 | } | |
1602 | ||
86469922 DL |
1603 | |
1604 | typedef struct { | |
1605 | QEMUBH *bh; | |
1606 | } HandleRunBhData; | |
1607 | ||
ea6a55bc | 1608 | static void loadvm_postcopy_handle_run_bh(void *opaque) |
093e3c42 | 1609 | { |
27c6825b | 1610 | Error *local_err = NULL; |
86469922 | 1611 | HandleRunBhData *data = opaque; |
093e3c42 | 1612 | |
27c6825b DDAG |
1613 | /* TODO we should move all of this lot into postcopy_ram.c or a shared code |
1614 | * in migration.c | |
1615 | */ | |
1616 | cpu_synchronize_all_post_init(); | |
1617 | ||
1618 | qemu_announce_self(); | |
1619 | ||
ace21a58 KW |
1620 | /* Make sure all file formats flush their mutable metadata. |
1621 | * If we get an error here, just don't restart the VM yet. */ | |
27c6825b | 1622 | bdrv_invalidate_cache_all(&local_err); |
0042fd36 | 1623 | if (local_err) { |
ace21a58 | 1624 | error_report_err(local_err); |
0042fd36 KW |
1625 | local_err = NULL; |
1626 | autostart = false; | |
1627 | } | |
1628 | ||
27c6825b DDAG |
1629 | trace_loadvm_postcopy_handle_run_cpu_sync(); |
1630 | cpu_synchronize_all_post_init(); | |
1631 | ||
1632 | trace_loadvm_postcopy_handle_run_vmstart(); | |
1633 | ||
093e3c42 DDAG |
1634 | if (autostart) { |
1635 | /* Hold onto your hats, starting the CPU */ | |
1636 | vm_start(); | |
1637 | } else { | |
1638 | /* leave it paused and let management decide when to start the CPU */ | |
1639 | runstate_set(RUN_STATE_PAUSED); | |
1640 | } | |
1641 | ||
86469922 DL |
1642 | qemu_bh_delete(data->bh); |
1643 | g_free(data); | |
ea6a55bc DL |
1644 | } |
1645 | ||
1646 | /* After all discards we can start running and asking for pages */ | |
1647 | static int loadvm_postcopy_handle_run(MigrationIncomingState *mis) | |
1648 | { | |
1649 | PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING); | |
86469922 | 1650 | HandleRunBhData *data; |
ea6a55bc DL |
1651 | |
1652 | trace_loadvm_postcopy_handle_run(); | |
1653 | if (ps != POSTCOPY_INCOMING_LISTENING) { | |
1654 | error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps); | |
1655 | return -1; | |
1656 | } | |
1657 | ||
86469922 DL |
1658 | data = g_new(HandleRunBhData, 1); |
1659 | data->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, data); | |
1660 | qemu_bh_schedule(data->bh); | |
ea6a55bc | 1661 | |
27c6825b DDAG |
1662 | /* We need to finish reading the stream from the package |
1663 | * and also stop reading anything more from the stream that loaded the | |
1664 | * package (since it's now being read by the listener thread). | |
1665 | * LOADVM_QUIT will quit all the layers of nested loadvm loops. | |
1666 | */ | |
1667 | return LOADVM_QUIT; | |
093e3c42 DDAG |
1668 | } |
1669 | ||
c76ca188 | 1670 | /** |
11cf1d98 DDAG |
1671 | * Immediately following this command is a blob of data containing an embedded |
1672 | * chunk of migration stream; read it and load it. | |
1673 | * | |
1674 | * @mis: Incoming state | |
1675 | * @length: Length of packaged data to read | |
c76ca188 | 1676 | * |
11cf1d98 DDAG |
1677 | * Returns: Negative values on error |
1678 | * | |
1679 | */ | |
1680 | static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis) | |
1681 | { | |
1682 | int ret; | |
61b67d47 DB |
1683 | size_t length; |
1684 | QIOChannelBuffer *bioc; | |
11cf1d98 DDAG |
1685 | |
1686 | length = qemu_get_be32(mis->from_src_file); | |
1687 | trace_loadvm_handle_cmd_packaged(length); | |
1688 | ||
1689 | if (length > MAX_VM_CMD_PACKAGED_SIZE) { | |
61b67d47 | 1690 | error_report("Unreasonably large packaged state: %zu", length); |
11cf1d98 DDAG |
1691 | return -1; |
1692 | } | |
61b67d47 DB |
1693 | |
1694 | bioc = qio_channel_buffer_new(length); | |
6f01f136 | 1695 | qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer"); |
61b67d47 DB |
1696 | ret = qemu_get_buffer(mis->from_src_file, |
1697 | bioc->data, | |
1698 | length); | |
11cf1d98 | 1699 | if (ret != length) { |
61b67d47 DB |
1700 | object_unref(OBJECT(bioc)); |
1701 | error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu", | |
9af9e0fe | 1702 | ret, length); |
11cf1d98 DDAG |
1703 | return (ret < 0) ? ret : -EAGAIN; |
1704 | } | |
61b67d47 | 1705 | bioc->usage += length; |
11cf1d98 DDAG |
1706 | trace_loadvm_handle_cmd_packaged_received(ret); |
1707 | ||
61b67d47 | 1708 | QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc)); |
11cf1d98 DDAG |
1709 | |
1710 | ret = qemu_loadvm_state_main(packf, mis); | |
1711 | trace_loadvm_handle_cmd_packaged_main(ret); | |
1712 | qemu_fclose(packf); | |
61b67d47 | 1713 | object_unref(OBJECT(bioc)); |
11cf1d98 DDAG |
1714 | |
1715 | return ret; | |
1716 | } | |
1717 | ||
1718 | /* | |
1719 | * Process an incoming 'QEMU_VM_COMMAND' | |
1720 | * 0 just a normal return | |
1721 | * LOADVM_QUIT All good, but exit the loop | |
1722 | * <0 Error | |
c76ca188 DDAG |
1723 | */ |
1724 | static int loadvm_process_command(QEMUFile *f) | |
1725 | { | |
2e37701e | 1726 | MigrationIncomingState *mis = migration_incoming_get_current(); |
c76ca188 DDAG |
1727 | uint16_t cmd; |
1728 | uint16_t len; | |
2e37701e | 1729 | uint32_t tmp32; |
c76ca188 DDAG |
1730 | |
1731 | cmd = qemu_get_be16(f); | |
1732 | len = qemu_get_be16(f); | |
1733 | ||
1734 | trace_loadvm_process_command(cmd, len); | |
1735 | if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) { | |
1736 | error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len); | |
1737 | return -EINVAL; | |
1738 | } | |
1739 | ||
1740 | if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) { | |
1741 | error_report("%s received with bad length - expecting %zu, got %d", | |
1742 | mig_cmd_args[cmd].name, | |
1743 | (size_t)mig_cmd_args[cmd].len, len); | |
1744 | return -ERANGE; | |
1745 | } | |
1746 | ||
1747 | switch (cmd) { | |
2e37701e DDAG |
1748 | case MIG_CMD_OPEN_RETURN_PATH: |
1749 | if (mis->to_src_file) { | |
1750 | error_report("CMD_OPEN_RETURN_PATH called when RP already open"); | |
1751 | /* Not really a problem, so don't give up */ | |
1752 | return 0; | |
1753 | } | |
1754 | mis->to_src_file = qemu_file_get_return_path(f); | |
1755 | if (!mis->to_src_file) { | |
1756 | error_report("CMD_OPEN_RETURN_PATH failed"); | |
1757 | return -1; | |
1758 | } | |
1759 | break; | |
1760 | ||
1761 | case MIG_CMD_PING: | |
1762 | tmp32 = qemu_get_be32(f); | |
1763 | trace_loadvm_process_command_ping(tmp32); | |
1764 | if (!mis->to_src_file) { | |
1765 | error_report("CMD_PING (0x%x) received with no return path", | |
1766 | tmp32); | |
1767 | return -1; | |
1768 | } | |
6decec93 | 1769 | migrate_send_rp_pong(mis, tmp32); |
2e37701e | 1770 | break; |
093e3c42 | 1771 | |
11cf1d98 DDAG |
1772 | case MIG_CMD_PACKAGED: |
1773 | return loadvm_handle_cmd_packaged(mis); | |
1774 | ||
093e3c42 DDAG |
1775 | case MIG_CMD_POSTCOPY_ADVISE: |
1776 | return loadvm_postcopy_handle_advise(mis); | |
1777 | ||
1778 | case MIG_CMD_POSTCOPY_LISTEN: | |
1779 | return loadvm_postcopy_handle_listen(mis); | |
1780 | ||
1781 | case MIG_CMD_POSTCOPY_RUN: | |
1782 | return loadvm_postcopy_handle_run(mis); | |
1783 | ||
1784 | case MIG_CMD_POSTCOPY_RAM_DISCARD: | |
1785 | return loadvm_postcopy_ram_handle_discard(mis, len); | |
c76ca188 DDAG |
1786 | } |
1787 | ||
1788 | return 0; | |
1789 | } | |
1790 | ||
59f39a47 DDAG |
1791 | /* |
1792 | * Read a footer off the wire and check that it matches the expected section | |
1793 | * | |
1794 | * Returns: true if the footer was good | |
1795 | * false if there is a problem (and calls error_report to say why) | |
1796 | */ | |
0f42f657 | 1797 | static bool check_section_footer(QEMUFile *f, SaveStateEntry *se) |
59f39a47 DDAG |
1798 | { |
1799 | uint8_t read_mark; | |
1800 | uint32_t read_section_id; | |
1801 | ||
1802 | if (skip_section_footers) { | |
1803 | /* No footer to check */ | |
1804 | return true; | |
1805 | } | |
1806 | ||
1807 | read_mark = qemu_get_byte(f); | |
1808 | ||
1809 | if (read_mark != QEMU_VM_SECTION_FOOTER) { | |
0f42f657 | 1810 | error_report("Missing section footer for %s", se->idstr); |
59f39a47 DDAG |
1811 | return false; |
1812 | } | |
1813 | ||
1814 | read_section_id = qemu_get_be32(f); | |
0f42f657 | 1815 | if (read_section_id != se->load_section_id) { |
59f39a47 DDAG |
1816 | error_report("Mismatched section id in footer for %s -" |
1817 | " read 0x%x expected 0x%x", | |
0f42f657 | 1818 | se->idstr, read_section_id, se->load_section_id); |
59f39a47 DDAG |
1819 | return false; |
1820 | } | |
1821 | ||
1822 | /* All good */ | |
1823 | return true; | |
1824 | } | |
1825 | ||
fb3520a8 HZ |
1826 | static int |
1827 | qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis) | |
1828 | { | |
1829 | uint32_t instance_id, version_id, section_id; | |
1830 | SaveStateEntry *se; | |
fb3520a8 HZ |
1831 | char idstr[256]; |
1832 | int ret; | |
1833 | ||
1834 | /* Read section start */ | |
1835 | section_id = qemu_get_be32(f); | |
1836 | if (!qemu_get_counted_string(f, idstr)) { | |
1837 | error_report("Unable to read ID string for section %u", | |
1838 | section_id); | |
1839 | return -EINVAL; | |
1840 | } | |
1841 | instance_id = qemu_get_be32(f); | |
1842 | version_id = qemu_get_be32(f); | |
1843 | ||
1844 | trace_qemu_loadvm_state_section_startfull(section_id, idstr, | |
1845 | instance_id, version_id); | |
1846 | /* Find savevm section */ | |
1847 | se = find_se(idstr, instance_id); | |
1848 | if (se == NULL) { | |
1849 | error_report("Unknown savevm section or instance '%s' %d", | |
1850 | idstr, instance_id); | |
1851 | return -EINVAL; | |
1852 | } | |
1853 | ||
1854 | /* Validate version */ | |
1855 | if (version_id > se->version_id) { | |
1856 | error_report("savevm: unsupported version %d for '%s' v%d", | |
1857 | version_id, idstr, se->version_id); | |
1858 | return -EINVAL; | |
1859 | } | |
0f42f657 JQ |
1860 | se->load_version_id = version_id; |
1861 | se->load_section_id = section_id; | |
fb3520a8 | 1862 | |
88c16567 WC |
1863 | /* Validate if it is a device's state */ |
1864 | if (xen_enabled() && se->is_ram) { | |
1865 | error_report("loadvm: %s RAM loading not allowed on Xen", idstr); | |
1866 | return -EINVAL; | |
1867 | } | |
1868 | ||
3a011c26 | 1869 | ret = vmstate_load(f, se); |
fb3520a8 HZ |
1870 | if (ret < 0) { |
1871 | error_report("error while loading state for instance 0x%x of" | |
1872 | " device '%s'", instance_id, idstr); | |
1873 | return ret; | |
1874 | } | |
0f42f657 | 1875 | if (!check_section_footer(f, se)) { |
fb3520a8 HZ |
1876 | return -EINVAL; |
1877 | } | |
1878 | ||
1879 | return 0; | |
1880 | } | |
1881 | ||
1882 | static int | |
1883 | qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis) | |
1884 | { | |
1885 | uint32_t section_id; | |
0f42f657 | 1886 | SaveStateEntry *se; |
fb3520a8 HZ |
1887 | int ret; |
1888 | ||
1889 | section_id = qemu_get_be32(f); | |
1890 | ||
1891 | trace_qemu_loadvm_state_section_partend(section_id); | |
0f42f657 JQ |
1892 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
1893 | if (se->load_section_id == section_id) { | |
fb3520a8 HZ |
1894 | break; |
1895 | } | |
1896 | } | |
0f42f657 | 1897 | if (se == NULL) { |
fb3520a8 HZ |
1898 | error_report("Unknown savevm section %d", section_id); |
1899 | return -EINVAL; | |
1900 | } | |
1901 | ||
3a011c26 | 1902 | ret = vmstate_load(f, se); |
fb3520a8 HZ |
1903 | if (ret < 0) { |
1904 | error_report("error while loading state section id %d(%s)", | |
0f42f657 | 1905 | section_id, se->idstr); |
fb3520a8 HZ |
1906 | return ret; |
1907 | } | |
0f42f657 | 1908 | if (!check_section_footer(f, se)) { |
fb3520a8 HZ |
1909 | return -EINVAL; |
1910 | } | |
1911 | ||
1912 | return 0; | |
1913 | } | |
1914 | ||
7b89bf27 | 1915 | static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis) |
1a8f46f8 | 1916 | { |
a672b469 | 1917 | uint8_t section_type; |
ccb783c3 | 1918 | int ret = 0; |
61964c23 | 1919 | |
a672b469 | 1920 | while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) { |
ccb783c3 | 1921 | ret = 0; |
a5df2a02 | 1922 | trace_qemu_loadvm_state_section(section_type); |
a672b469 AL |
1923 | switch (section_type) { |
1924 | case QEMU_VM_SECTION_START: | |
1925 | case QEMU_VM_SECTION_FULL: | |
fb3520a8 | 1926 | ret = qemu_loadvm_section_start_full(f, mis); |
b5a22e4a | 1927 | if (ret < 0) { |
ccb783c3 | 1928 | goto out; |
b5a22e4a | 1929 | } |
a672b469 AL |
1930 | break; |
1931 | case QEMU_VM_SECTION_PART: | |
1932 | case QEMU_VM_SECTION_END: | |
fb3520a8 | 1933 | ret = qemu_loadvm_section_part_end(f, mis); |
b5a22e4a | 1934 | if (ret < 0) { |
ccb783c3 | 1935 | goto out; |
b5a22e4a | 1936 | } |
a672b469 | 1937 | break; |
c76ca188 DDAG |
1938 | case QEMU_VM_COMMAND: |
1939 | ret = loadvm_process_command(f); | |
7b89bf27 DDAG |
1940 | trace_qemu_loadvm_state_section_command(ret); |
1941 | if ((ret < 0) || (ret & LOADVM_QUIT)) { | |
ccb783c3 | 1942 | goto out; |
c76ca188 DDAG |
1943 | } |
1944 | break; | |
a672b469 | 1945 | default: |
6a64b644 | 1946 | error_report("Unknown savevm section type %d", section_type); |
ccb783c3 DDAG |
1947 | ret = -EINVAL; |
1948 | goto out; | |
a672b469 AL |
1949 | } |
1950 | } | |
1951 | ||
ccb783c3 DDAG |
1952 | out: |
1953 | if (ret < 0) { | |
1954 | qemu_file_set_error(f, ret); | |
1955 | } | |
1956 | return ret; | |
7b89bf27 DDAG |
1957 | } |
1958 | ||
1959 | int qemu_loadvm_state(QEMUFile *f) | |
1960 | { | |
1961 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
1962 | Error *local_err = NULL; | |
1963 | unsigned int v; | |
1964 | int ret; | |
1965 | ||
1966 | if (qemu_savevm_state_blocked(&local_err)) { | |
1967 | error_report_err(local_err); | |
1968 | return -EINVAL; | |
1969 | } | |
1970 | ||
1971 | v = qemu_get_be32(f); | |
1972 | if (v != QEMU_VM_FILE_MAGIC) { | |
1973 | error_report("Not a migration stream"); | |
1974 | return -EINVAL; | |
1975 | } | |
1976 | ||
1977 | v = qemu_get_be32(f); | |
1978 | if (v == QEMU_VM_FILE_VERSION_COMPAT) { | |
1979 | error_report("SaveVM v2 format is obsolete and don't work anymore"); | |
1980 | return -ENOTSUP; | |
1981 | } | |
1982 | if (v != QEMU_VM_FILE_VERSION) { | |
1983 | error_report("Unsupported migration stream version"); | |
1984 | return -ENOTSUP; | |
1985 | } | |
1986 | ||
902c053d | 1987 | if (!savevm_state.skip_configuration || enforce_config_section()) { |
7b89bf27 DDAG |
1988 | if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) { |
1989 | error_report("Configuration section missing"); | |
1990 | return -EINVAL; | |
1991 | } | |
1992 | ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0); | |
1993 | ||
1994 | if (ret) { | |
1995 | return ret; | |
1996 | } | |
1997 | } | |
1998 | ||
75e972da DG |
1999 | cpu_synchronize_all_pre_loadvm(); |
2000 | ||
7b89bf27 DDAG |
2001 | ret = qemu_loadvm_state_main(f, mis); |
2002 | qemu_event_set(&mis->main_thread_load_event); | |
2003 | ||
2004 | trace_qemu_loadvm_state_post_main(ret); | |
2005 | ||
c76201ab DDAG |
2006 | if (mis->have_listen_thread) { |
2007 | /* Listen thread still going, can't clean up yet */ | |
2008 | return ret; | |
2009 | } | |
2010 | ||
7b89bf27 DDAG |
2011 | if (ret == 0) { |
2012 | ret = qemu_file_get_error(f); | |
2013 | } | |
1925cebc AG |
2014 | |
2015 | /* | |
2016 | * Try to read in the VMDESC section as well, so that dumping tools that | |
2017 | * intercept our migration stream have the chance to see it. | |
2018 | */ | |
1aca9a5f DDAG |
2019 | |
2020 | /* We've got to be careful; if we don't read the data and just shut the fd | |
2021 | * then the sender can error if we close while it's still sending. | |
2022 | * We also mustn't read data that isn't there; some transports (RDMA) | |
2023 | * will stall waiting for that data when the source has already closed. | |
2024 | */ | |
7b89bf27 | 2025 | if (ret == 0 && should_send_vmdesc()) { |
1aca9a5f DDAG |
2026 | uint8_t *buf; |
2027 | uint32_t size; | |
7b89bf27 | 2028 | uint8_t section_type = qemu_get_byte(f); |
1aca9a5f DDAG |
2029 | |
2030 | if (section_type != QEMU_VM_VMDESCRIPTION) { | |
2031 | error_report("Expected vmdescription section, but got %d", | |
2032 | section_type); | |
2033 | /* | |
2034 | * It doesn't seem worth failing at this point since | |
2035 | * we apparently have an otherwise valid VM state | |
2036 | */ | |
2037 | } else { | |
2038 | buf = g_malloc(0x1000); | |
2039 | size = qemu_get_be32(f); | |
2040 | ||
2041 | while (size > 0) { | |
2042 | uint32_t read_chunk = MIN(size, 0x1000); | |
2043 | qemu_get_buffer(f, buf, read_chunk); | |
2044 | size -= read_chunk; | |
2045 | } | |
2046 | g_free(buf); | |
1925cebc | 2047 | } |
1925cebc AG |
2048 | } |
2049 | ||
ea375f9a JK |
2050 | cpu_synchronize_all_post_init(); |
2051 | ||
a672b469 AL |
2052 | return ret; |
2053 | } | |
2054 | ||
5e22479a | 2055 | int save_snapshot(const char *name, Error **errp) |
a672b469 AL |
2056 | { |
2057 | BlockDriverState *bs, *bs1; | |
2058 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; | |
ac8c19ba | 2059 | int ret = -1; |
a672b469 AL |
2060 | QEMUFile *f; |
2061 | int saved_vm_running; | |
c2c9a466 | 2062 | uint64_t vm_state_size; |
68b891ec | 2063 | qemu_timeval tv; |
7d631a11 | 2064 | struct tm tm; |
79b3c12a | 2065 | AioContext *aio_context; |
a672b469 | 2066 | |
e9ff957a | 2067 | if (!bdrv_all_can_snapshot(&bs)) { |
927d6638 JQ |
2068 | error_setg(errp, "Device '%s' is writable but does not support " |
2069 | "snapshots", bdrv_get_device_name(bs)); | |
ac8c19ba | 2070 | return ret; |
feeee5ac MDCF |
2071 | } |
2072 | ||
0b461605 | 2073 | /* Delete old snapshots of the same name */ |
ac8c19ba | 2074 | if (name) { |
927d6638 | 2075 | ret = bdrv_all_delete_snapshot(name, &bs1, errp); |
ac8c19ba | 2076 | if (ret < 0) { |
927d6638 JQ |
2077 | error_prepend(errp, "Error while deleting snapshot on device " |
2078 | "'%s': ", bdrv_get_device_name(bs1)); | |
ac8c19ba PD |
2079 | return ret; |
2080 | } | |
0b461605 DL |
2081 | } |
2082 | ||
7cb14481 DL |
2083 | bs = bdrv_all_find_vmstate_bs(); |
2084 | if (bs == NULL) { | |
927d6638 | 2085 | error_setg(errp, "No block device can accept snapshots"); |
ac8c19ba | 2086 | return ret; |
a672b469 | 2087 | } |
79b3c12a | 2088 | aio_context = bdrv_get_aio_context(bs); |
a672b469 | 2089 | |
1354869c | 2090 | saved_vm_running = runstate_is_running(); |
560d027b JQ |
2091 | |
2092 | ret = global_state_store(); | |
2093 | if (ret) { | |
927d6638 | 2094 | error_setg(errp, "Error saving global state"); |
ac8c19ba | 2095 | return ret; |
560d027b | 2096 | } |
0461d5a6 | 2097 | vm_stop(RUN_STATE_SAVE_VM); |
a672b469 | 2098 | |
79b3c12a DL |
2099 | aio_context_acquire(aio_context); |
2100 | ||
cb499fb2 | 2101 | memset(sn, 0, sizeof(*sn)); |
a672b469 AL |
2102 | |
2103 | /* fill auxiliary fields */ | |
68b891ec | 2104 | qemu_gettimeofday(&tv); |
a672b469 AL |
2105 | sn->date_sec = tv.tv_sec; |
2106 | sn->date_nsec = tv.tv_usec * 1000; | |
bc72ad67 | 2107 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
a672b469 | 2108 | |
7d631a11 MDCF |
2109 | if (name) { |
2110 | ret = bdrv_snapshot_find(bs, old_sn, name); | |
2111 | if (ret >= 0) { | |
2112 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); | |
2113 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); | |
2114 | } else { | |
2115 | pstrcpy(sn->name, sizeof(sn->name), name); | |
2116 | } | |
2117 | } else { | |
d7d9b528 BS |
2118 | /* cast below needed for OpenBSD where tv_sec is still 'long' */ |
2119 | localtime_r((const time_t *)&tv.tv_sec, &tm); | |
7d631a11 | 2120 | strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm); |
7d631a11 MDCF |
2121 | } |
2122 | ||
a672b469 | 2123 | /* save the VM state */ |
45566e9c | 2124 | f = qemu_fopen_bdrv(bs, 1); |
a672b469 | 2125 | if (!f) { |
927d6638 | 2126 | error_setg(errp, "Could not open VM state file"); |
a672b469 AL |
2127 | goto the_end; |
2128 | } | |
927d6638 | 2129 | ret = qemu_savevm_state(f, errp); |
2d22b18f | 2130 | vm_state_size = qemu_ftell(f); |
a672b469 AL |
2131 | qemu_fclose(f); |
2132 | if (ret < 0) { | |
a672b469 AL |
2133 | goto the_end; |
2134 | } | |
2135 | ||
a9085f9b DL |
2136 | ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, &bs); |
2137 | if (ret < 0) { | |
927d6638 JQ |
2138 | error_setg(errp, "Error while creating snapshot on '%s'", |
2139 | bdrv_get_device_name(bs)); | |
ac8c19ba | 2140 | goto the_end; |
a672b469 AL |
2141 | } |
2142 | ||
ac8c19ba PD |
2143 | ret = 0; |
2144 | ||
a672b469 | 2145 | the_end: |
79b3c12a | 2146 | aio_context_release(aio_context); |
38ff78d3 | 2147 | if (saved_vm_running) { |
a672b469 | 2148 | vm_start(); |
38ff78d3 | 2149 | } |
ac8c19ba PD |
2150 | return ret; |
2151 | } | |
2152 | ||
a7ae8355 SS |
2153 | void qmp_xen_save_devices_state(const char *filename, Error **errp) |
2154 | { | |
2155 | QEMUFile *f; | |
8925839f | 2156 | QIOChannelFile *ioc; |
a7ae8355 SS |
2157 | int saved_vm_running; |
2158 | int ret; | |
2159 | ||
2160 | saved_vm_running = runstate_is_running(); | |
2161 | vm_stop(RUN_STATE_SAVE_VM); | |
c69adea4 | 2162 | global_state_store_running(); |
a7ae8355 | 2163 | |
8925839f DB |
2164 | ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp); |
2165 | if (!ioc) { | |
a7ae8355 SS |
2166 | goto the_end; |
2167 | } | |
6f01f136 | 2168 | qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state"); |
8925839f | 2169 | f = qemu_fopen_channel_output(QIO_CHANNEL(ioc)); |
a7ae8355 SS |
2170 | ret = qemu_save_device_state(f); |
2171 | qemu_fclose(f); | |
2172 | if (ret < 0) { | |
c6bd8c70 | 2173 | error_setg(errp, QERR_IO_ERROR); |
a7ae8355 SS |
2174 | } |
2175 | ||
2176 | the_end: | |
38ff78d3 | 2177 | if (saved_vm_running) { |
a7ae8355 | 2178 | vm_start(); |
38ff78d3 | 2179 | } |
a7ae8355 SS |
2180 | } |
2181 | ||
88c16567 WC |
2182 | void qmp_xen_load_devices_state(const char *filename, Error **errp) |
2183 | { | |
2184 | QEMUFile *f; | |
2185 | QIOChannelFile *ioc; | |
2186 | int ret; | |
2187 | ||
2188 | /* Guest must be paused before loading the device state; the RAM state | |
2189 | * will already have been loaded by xc | |
2190 | */ | |
2191 | if (runstate_is_running()) { | |
2192 | error_setg(errp, "Cannot update device state while vm is running"); | |
2193 | return; | |
2194 | } | |
2195 | vm_stop(RUN_STATE_RESTORE_VM); | |
2196 | ||
2197 | ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp); | |
2198 | if (!ioc) { | |
2199 | return; | |
2200 | } | |
6f01f136 | 2201 | qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state"); |
88c16567 WC |
2202 | f = qemu_fopen_channel_input(QIO_CHANNEL(ioc)); |
2203 | ||
88c16567 WC |
2204 | ret = qemu_loadvm_state(f); |
2205 | qemu_fclose(f); | |
2206 | if (ret < 0) { | |
2207 | error_setg(errp, QERR_IO_ERROR); | |
2208 | } | |
2209 | migration_incoming_state_destroy(); | |
2210 | } | |
2211 | ||
5e22479a | 2212 | int load_snapshot(const char *name, Error **errp) |
a672b469 | 2213 | { |
f0aa7a8b | 2214 | BlockDriverState *bs, *bs_vm_state; |
2d22b18f | 2215 | QEMUSnapshotInfo sn; |
a672b469 | 2216 | QEMUFile *f; |
751c6a17 | 2217 | int ret; |
79b3c12a | 2218 | AioContext *aio_context; |
b4b076da | 2219 | MigrationIncomingState *mis = migration_incoming_get_current(); |
a672b469 | 2220 | |
849f96e2 | 2221 | if (!bdrv_all_can_snapshot(&bs)) { |
927d6638 JQ |
2222 | error_setg(errp, |
2223 | "Device '%s' is writable but does not support snapshots", | |
2224 | bdrv_get_device_name(bs)); | |
849f96e2 DL |
2225 | return -ENOTSUP; |
2226 | } | |
723ccda1 DL |
2227 | ret = bdrv_all_find_snapshot(name, &bs); |
2228 | if (ret < 0) { | |
927d6638 JQ |
2229 | error_setg(errp, |
2230 | "Device '%s' does not have the requested snapshot '%s'", | |
2231 | bdrv_get_device_name(bs), name); | |
723ccda1 DL |
2232 | return ret; |
2233 | } | |
849f96e2 | 2234 | |
7cb14481 | 2235 | bs_vm_state = bdrv_all_find_vmstate_bs(); |
f0aa7a8b | 2236 | if (!bs_vm_state) { |
927d6638 | 2237 | error_setg(errp, "No block device supports snapshots"); |
f0aa7a8b MDCF |
2238 | return -ENOTSUP; |
2239 | } | |
79b3c12a | 2240 | aio_context = bdrv_get_aio_context(bs_vm_state); |
f0aa7a8b MDCF |
2241 | |
2242 | /* Don't even try to load empty VM states */ | |
79b3c12a | 2243 | aio_context_acquire(aio_context); |
f0aa7a8b | 2244 | ret = bdrv_snapshot_find(bs_vm_state, &sn, name); |
79b3c12a | 2245 | aio_context_release(aio_context); |
f0aa7a8b MDCF |
2246 | if (ret < 0) { |
2247 | return ret; | |
2248 | } else if (sn.vm_state_size == 0) { | |
927d6638 JQ |
2249 | error_setg(errp, "This is a disk-only snapshot. Revert to it " |
2250 | " offline using qemu-img"); | |
f0aa7a8b MDCF |
2251 | return -EINVAL; |
2252 | } | |
2253 | ||
a672b469 | 2254 | /* Flush all IO requests so they don't interfere with the new state. */ |
922453bc | 2255 | bdrv_drain_all(); |
a672b469 | 2256 | |
4c1cdbaa DL |
2257 | ret = bdrv_all_goto_snapshot(name, &bs); |
2258 | if (ret < 0) { | |
927d6638 | 2259 | error_setg(errp, "Error %d while activating snapshot '%s' on '%s'", |
4c1cdbaa DL |
2260 | ret, name, bdrv_get_device_name(bs)); |
2261 | return ret; | |
a672b469 AL |
2262 | } |
2263 | ||
a672b469 | 2264 | /* restore the VM state */ |
f0aa7a8b | 2265 | f = qemu_fopen_bdrv(bs_vm_state, 0); |
a672b469 | 2266 | if (!f) { |
927d6638 | 2267 | error_setg(errp, "Could not open VM state file"); |
05f2401e | 2268 | return -EINVAL; |
a672b469 | 2269 | } |
f0aa7a8b | 2270 | |
aedbe192 | 2271 | qemu_system_reset(SHUTDOWN_CAUSE_NONE); |
b4b076da | 2272 | mis->from_src_file = f; |
f0aa7a8b | 2273 | |
79b3c12a DL |
2274 | aio_context_acquire(aio_context); |
2275 | ret = qemu_loadvm_state(f); | |
79b3c12a DL |
2276 | aio_context_release(aio_context); |
2277 | ||
bca7856a | 2278 | migration_incoming_state_destroy(); |
a672b469 | 2279 | if (ret < 0) { |
927d6638 | 2280 | error_setg(errp, "Error %d while loading VM state", ret); |
05f2401e | 2281 | return ret; |
a672b469 | 2282 | } |
f0aa7a8b | 2283 | |
05f2401e | 2284 | return 0; |
7b630349 JQ |
2285 | } |
2286 | ||
c5705a77 AK |
2287 | void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev) |
2288 | { | |
fa53a0e5 | 2289 | qemu_ram_set_idstr(mr->ram_block, |
c5705a77 AK |
2290 | memory_region_name(mr), dev); |
2291 | } | |
2292 | ||
2293 | void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev) | |
2294 | { | |
fa53a0e5 | 2295 | qemu_ram_unset_idstr(mr->ram_block); |
c5705a77 AK |
2296 | } |
2297 | ||
2298 | void vmstate_register_ram_global(MemoryRegion *mr) | |
2299 | { | |
2300 | vmstate_register_ram(mr, NULL); | |
2301 | } | |
1bfe5f05 JQ |
2302 | |
2303 | bool vmstate_check_only_migratable(const VMStateDescription *vmsd) | |
2304 | { | |
2305 | /* check needed if --only-migratable is specified */ | |
2306 | if (!only_migratable) { | |
2307 | return true; | |
2308 | } | |
2309 | ||
2310 | return !(vmsd && vmsd->unmigratable); | |
2311 | } |