]>
Commit | Line | Data |
---|---|---|
a672b469 AL |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
a672b469 | 24 | |
d40cdb10 | 25 | #include "config-host.h" |
511d2b14 BS |
26 | #include "qemu-common.h" |
27 | #include "hw/hw.h" | |
7685ee6a | 28 | #include "hw/qdev.h" |
1422e32d | 29 | #include "net/net.h" |
83c9089e | 30 | #include "monitor/monitor.h" |
9c17d615 | 31 | #include "sysemu/sysemu.h" |
1de7afc9 | 32 | #include "qemu/timer.h" |
511d2b14 | 33 | #include "audio/audio.h" |
caf71f86 | 34 | #include "migration/migration.h" |
1de7afc9 PB |
35 | #include "qemu/sockets.h" |
36 | #include "qemu/queue.h" | |
9c17d615 | 37 | #include "sysemu/cpus.h" |
022c62cb | 38 | #include "exec/memory.h" |
a7ae8355 | 39 | #include "qmp-commands.h" |
517a13c9 | 40 | #include "trace.h" |
1de7afc9 | 41 | #include "qemu/bitops.h" |
511d2b14 | 42 | |
a672b469 | 43 | #define SELF_ANNOUNCE_ROUNDS 5 |
a672b469 | 44 | |
18995b98 | 45 | #ifndef ETH_P_RARP |
f8778a77 | 46 | #define ETH_P_RARP 0x8035 |
18995b98 N |
47 | #endif |
48 | #define ARP_HTYPE_ETH 0x0001 | |
49 | #define ARP_PTYPE_IP 0x0800 | |
50 | #define ARP_OP_REQUEST_REV 0x3 | |
51 | ||
52 | static int announce_self_create(uint8_t *buf, | |
a672b469 AL |
53 | uint8_t *mac_addr) |
54 | { | |
18995b98 N |
55 | /* Ethernet header. */ |
56 | memset(buf, 0xff, 6); /* destination MAC addr */ | |
57 | memcpy(buf + 6, mac_addr, 6); /* source MAC addr */ | |
58 | *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */ | |
59 | ||
60 | /* RARP header. */ | |
61 | *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */ | |
62 | *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */ | |
63 | *(buf + 18) = 6; /* hardware addr length (ethernet) */ | |
64 | *(buf + 19) = 4; /* protocol addr length (IPv4) */ | |
65 | *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */ | |
66 | memcpy(buf + 22, mac_addr, 6); /* source hw addr */ | |
67 | memset(buf + 28, 0x00, 4); /* source protocol addr */ | |
68 | memcpy(buf + 32, mac_addr, 6); /* target hw addr */ | |
69 | memset(buf + 38, 0x00, 4); /* target protocol addr */ | |
70 | ||
71 | /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */ | |
72 | memset(buf + 42, 0x00, 18); | |
73 | ||
74 | return 60; /* len (FCS will be added by hardware) */ | |
a672b469 AL |
75 | } |
76 | ||
f401ca22 | 77 | static void qemu_announce_self_iter(NICState *nic, void *opaque) |
a672b469 | 78 | { |
18995b98 | 79 | uint8_t buf[60]; |
f401ca22 MM |
80 | int len; |
81 | ||
82 | len = announce_self_create(buf, nic->conf->macaddr.a); | |
83 | ||
b356f76d | 84 | qemu_send_packet_raw(qemu_get_queue(nic), buf, len); |
f401ca22 MM |
85 | } |
86 | ||
87 | ||
88 | static void qemu_announce_self_once(void *opaque) | |
89 | { | |
ed8b330b GN |
90 | static int count = SELF_ANNOUNCE_ROUNDS; |
91 | QEMUTimer *timer = *(QEMUTimer **)opaque; | |
a672b469 | 92 | |
f401ca22 MM |
93 | qemu_foreach_nic(qemu_announce_self_iter, NULL); |
94 | ||
18995b98 N |
95 | if (--count) { |
96 | /* delay 50ms, 150ms, 250ms, ... */ | |
7bd427d8 | 97 | qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) + |
18995b98 | 98 | 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100); |
ed8b330b GN |
99 | } else { |
100 | qemu_del_timer(timer); | |
101 | qemu_free_timer(timer); | |
102 | } | |
103 | } | |
104 | ||
105 | void qemu_announce_self(void) | |
106 | { | |
107 | static QEMUTimer *timer; | |
7bd427d8 | 108 | timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer); |
ed8b330b | 109 | qemu_announce_self_once(&timer); |
a672b469 AL |
110 | } |
111 | ||
112 | /***********************************************************/ | |
113 | /* savevm/loadvm support */ | |
114 | ||
115 | #define IO_BUF_SIZE 32768 | |
116 | ||
117 | struct QEMUFile { | |
9229bf3c | 118 | const QEMUFileOps *ops; |
a672b469 AL |
119 | void *opaque; |
120 | int is_write; | |
121 | ||
1964a397 PB |
122 | int64_t bytes_xfer; |
123 | int64_t xfer_limit; | |
124 | ||
3f2d38fa PB |
125 | int64_t pos; /* start of buffer when writing, end of buffer |
126 | when reading */ | |
a672b469 AL |
127 | int buf_index; |
128 | int buf_size; /* 0 when writing */ | |
129 | uint8_t buf[IO_BUF_SIZE]; | |
130 | ||
3961b4dd | 131 | int last_error; |
a672b469 AL |
132 | }; |
133 | ||
7f79dd28 | 134 | typedef struct QEMUFileStdio |
a672b469 | 135 | { |
7f79dd28 | 136 | FILE *stdio_file; |
a672b469 | 137 | QEMUFile *file; |
7f79dd28 | 138 | } QEMUFileStdio; |
a672b469 AL |
139 | |
140 | typedef struct QEMUFileSocket | |
141 | { | |
142 | int fd; | |
143 | QEMUFile *file; | |
144 | } QEMUFileSocket; | |
145 | ||
d7cd3694 SH |
146 | typedef struct { |
147 | Coroutine *co; | |
148 | int fd; | |
149 | } FDYieldUntilData; | |
150 | ||
151 | static void fd_coroutine_enter(void *opaque) | |
152 | { | |
153 | FDYieldUntilData *data = opaque; | |
154 | qemu_set_fd_handler(data->fd, NULL, NULL, NULL); | |
155 | qemu_coroutine_enter(data->co, NULL); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Yield until a file descriptor becomes readable | |
160 | * | |
161 | * Note that this function clobbers the handlers for the file descriptor. | |
162 | */ | |
163 | static void coroutine_fn yield_until_fd_readable(int fd) | |
164 | { | |
165 | FDYieldUntilData data; | |
166 | ||
167 | assert(qemu_in_coroutine()); | |
168 | data.co = qemu_coroutine_self(); | |
169 | data.fd = fd; | |
170 | qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data); | |
171 | qemu_coroutine_yield(); | |
172 | } | |
173 | ||
70eb6330 PB |
174 | static int socket_get_fd(void *opaque) |
175 | { | |
176 | QEMUFileSocket *s = opaque; | |
177 | ||
178 | return s->fd; | |
179 | } | |
180 | ||
a672b469 AL |
181 | static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
182 | { | |
183 | QEMUFileSocket *s = opaque; | |
184 | ssize_t len; | |
185 | ||
595ab641 | 186 | for (;;) { |
00aa0040 | 187 | len = qemu_recv(s->fd, buf, size, 0); |
595ab641 PB |
188 | if (len != -1) { |
189 | break; | |
190 | } | |
191 | if (socket_error() == EAGAIN) { | |
d7cd3694 | 192 | yield_until_fd_readable(s->fd); |
595ab641 PB |
193 | } else if (socket_error() != EINTR) { |
194 | break; | |
195 | } | |
196 | } | |
a672b469 | 197 | |
595ab641 | 198 | if (len == -1) { |
a672b469 | 199 | len = -socket_error(); |
595ab641 | 200 | } |
a672b469 AL |
201 | return len; |
202 | } | |
203 | ||
0cc3f3cc PB |
204 | static int socket_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) |
205 | { | |
206 | QEMUFileSocket *s = opaque; | |
207 | ssize_t len; | |
208 | ||
209 | len = qemu_send_full(s->fd, buf, size, 0); | |
210 | if (len < size) { | |
211 | len = -socket_error(); | |
212 | } | |
213 | return len; | |
214 | } | |
215 | ||
a672b469 AL |
216 | static int socket_close(void *opaque) |
217 | { | |
218 | QEMUFileSocket *s = opaque; | |
ab52a824 | 219 | closesocket(s->fd); |
7267c094 | 220 | g_free(s); |
a672b469 AL |
221 | return 0; |
222 | } | |
223 | ||
70eb6330 PB |
224 | static int stdio_get_fd(void *opaque) |
225 | { | |
226 | QEMUFileStdio *s = opaque; | |
227 | ||
228 | return fileno(s->stdio_file); | |
229 | } | |
230 | ||
7f79dd28 | 231 | static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) |
a672b469 | 232 | { |
7f79dd28 PB |
233 | QEMUFileStdio *s = opaque; |
234 | return fwrite(buf, 1, size, s->stdio_file); | |
a672b469 AL |
235 | } |
236 | ||
7f79dd28 | 237 | static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
a672b469 | 238 | { |
7f79dd28 PB |
239 | QEMUFileStdio *s = opaque; |
240 | FILE *fp = s->stdio_file; | |
8a67ec4d UL |
241 | int bytes; |
242 | ||
595ab641 | 243 | for (;;) { |
8a67ec4d UL |
244 | clearerr(fp); |
245 | bytes = fread(buf, 1, size, fp); | |
595ab641 PB |
246 | if (bytes != 0 || !ferror(fp)) { |
247 | break; | |
248 | } | |
249 | if (errno == EAGAIN) { | |
d7cd3694 | 250 | yield_until_fd_readable(fileno(fp)); |
595ab641 PB |
251 | } else if (errno != EINTR) { |
252 | break; | |
253 | } | |
254 | } | |
8a67ec4d | 255 | return bytes; |
a672b469 AL |
256 | } |
257 | ||
7f79dd28 PB |
258 | static int stdio_pclose(void *opaque) |
259 | { | |
260 | QEMUFileStdio *s = opaque; | |
41ef56e6 AL |
261 | int ret; |
262 | ret = pclose(s->stdio_file); | |
26f1af0a EH |
263 | if (ret == -1) { |
264 | ret = -errno; | |
13c7b2da PB |
265 | } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) { |
266 | /* close succeeded, but non-zero exit code: */ | |
267 | ret = -EIO; /* fake errno value */ | |
26f1af0a | 268 | } |
7267c094 | 269 | g_free(s); |
41ef56e6 | 270 | return ret; |
7f79dd28 PB |
271 | } |
272 | ||
273 | static int stdio_fclose(void *opaque) | |
a672b469 | 274 | { |
7f79dd28 | 275 | QEMUFileStdio *s = opaque; |
0e286705 | 276 | int ret = 0; |
ce39ee31 PB |
277 | |
278 | if (s->file->ops->put_buffer) { | |
279 | int fd = fileno(s->stdio_file); | |
280 | struct stat st; | |
281 | ||
282 | ret = fstat(fd, &st); | |
283 | if (ret == 0 && S_ISREG(st.st_mode)) { | |
284 | /* | |
285 | * If the file handle is a regular file make sure the | |
286 | * data is flushed to disk before signaling success. | |
287 | */ | |
288 | ret = fsync(fd); | |
289 | if (ret != 0) { | |
290 | ret = -errno; | |
291 | return ret; | |
292 | } | |
293 | } | |
294 | } | |
0e286705 EH |
295 | if (fclose(s->stdio_file) == EOF) { |
296 | ret = -errno; | |
297 | } | |
7267c094 | 298 | g_free(s); |
0e286705 | 299 | return ret; |
a672b469 AL |
300 | } |
301 | ||
9229bf3c | 302 | static const QEMUFileOps stdio_pipe_read_ops = { |
70eb6330 | 303 | .get_fd = stdio_get_fd, |
9229bf3c PB |
304 | .get_buffer = stdio_get_buffer, |
305 | .close = stdio_pclose | |
306 | }; | |
307 | ||
308 | static const QEMUFileOps stdio_pipe_write_ops = { | |
70eb6330 | 309 | .get_fd = stdio_get_fd, |
9229bf3c PB |
310 | .put_buffer = stdio_put_buffer, |
311 | .close = stdio_pclose | |
312 | }; | |
313 | ||
817b9ed5 | 314 | QEMUFile *qemu_popen_cmd(const char *command, const char *mode) |
a672b469 | 315 | { |
817b9ed5 | 316 | FILE *stdio_file; |
7f79dd28 | 317 | QEMUFileStdio *s; |
a672b469 | 318 | |
817b9ed5 PB |
319 | stdio_file = popen(command, mode); |
320 | if (stdio_file == NULL) { | |
321 | return NULL; | |
322 | } | |
323 | ||
324 | if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) { | |
a672b469 AL |
325 | fprintf(stderr, "qemu_popen: Argument validity check failed\n"); |
326 | return NULL; | |
327 | } | |
328 | ||
7267c094 | 329 | s = g_malloc0(sizeof(QEMUFileStdio)); |
a672b469 | 330 | |
7f79dd28 | 331 | s->stdio_file = stdio_file; |
a672b469 AL |
332 | |
333 | if(mode[0] == 'r') { | |
9229bf3c | 334 | s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops); |
a672b469 | 335 | } else { |
9229bf3c | 336 | s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops); |
a672b469 | 337 | } |
a672b469 AL |
338 | return s->file; |
339 | } | |
340 | ||
9229bf3c | 341 | static const QEMUFileOps stdio_file_read_ops = { |
70eb6330 | 342 | .get_fd = stdio_get_fd, |
9229bf3c PB |
343 | .get_buffer = stdio_get_buffer, |
344 | .close = stdio_fclose | |
345 | }; | |
346 | ||
347 | static const QEMUFileOps stdio_file_write_ops = { | |
70eb6330 | 348 | .get_fd = stdio_get_fd, |
9229bf3c PB |
349 | .put_buffer = stdio_put_buffer, |
350 | .close = stdio_fclose | |
351 | }; | |
352 | ||
5ac1fad3 PB |
353 | QEMUFile *qemu_fdopen(int fd, const char *mode) |
354 | { | |
355 | QEMUFileStdio *s; | |
356 | ||
357 | if (mode == NULL || | |
358 | (mode[0] != 'r' && mode[0] != 'w') || | |
359 | mode[1] != 'b' || mode[2] != 0) { | |
360 | fprintf(stderr, "qemu_fdopen: Argument validity check failed\n"); | |
361 | return NULL; | |
362 | } | |
363 | ||
7267c094 | 364 | s = g_malloc0(sizeof(QEMUFileStdio)); |
5ac1fad3 PB |
365 | s->stdio_file = fdopen(fd, mode); |
366 | if (!s->stdio_file) | |
367 | goto fail; | |
368 | ||
369 | if(mode[0] == 'r') { | |
9229bf3c | 370 | s->file = qemu_fopen_ops(s, &stdio_file_read_ops); |
5ac1fad3 | 371 | } else { |
9229bf3c | 372 | s->file = qemu_fopen_ops(s, &stdio_file_write_ops); |
5ac1fad3 PB |
373 | } |
374 | return s->file; | |
375 | ||
376 | fail: | |
7267c094 | 377 | g_free(s); |
5ac1fad3 PB |
378 | return NULL; |
379 | } | |
380 | ||
9229bf3c | 381 | static const QEMUFileOps socket_read_ops = { |
70eb6330 | 382 | .get_fd = socket_get_fd, |
9229bf3c PB |
383 | .get_buffer = socket_get_buffer, |
384 | .close = socket_close | |
385 | }; | |
386 | ||
0cc3f3cc PB |
387 | static const QEMUFileOps socket_write_ops = { |
388 | .get_fd = socket_get_fd, | |
389 | .put_buffer = socket_put_buffer, | |
390 | .close = socket_close | |
391 | }; | |
392 | ||
393 | QEMUFile *qemu_fopen_socket(int fd, const char *mode) | |
a672b469 | 394 | { |
7267c094 | 395 | QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket)); |
a672b469 | 396 | |
0cc3f3cc PB |
397 | if (mode == NULL || |
398 | (mode[0] != 'r' && mode[0] != 'w') || | |
399 | mode[1] != 'b' || mode[2] != 0) { | |
400 | fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); | |
401 | return NULL; | |
402 | } | |
403 | ||
a672b469 | 404 | s->fd = fd; |
0cc3f3cc | 405 | if (mode[0] == 'w') { |
f8bbc128 | 406 | socket_set_block(s->fd); |
0cc3f3cc PB |
407 | s->file = qemu_fopen_ops(s, &socket_write_ops); |
408 | } else { | |
409 | s->file = qemu_fopen_ops(s, &socket_read_ops); | |
410 | } | |
a672b469 AL |
411 | return s->file; |
412 | } | |
413 | ||
a672b469 AL |
414 | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
415 | { | |
416 | QEMUFileStdio *s; | |
417 | ||
7f79dd28 PB |
418 | if (mode == NULL || |
419 | (mode[0] != 'r' && mode[0] != 'w') || | |
420 | mode[1] != 'b' || mode[2] != 0) { | |
090414a3 | 421 | fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); |
7f79dd28 PB |
422 | return NULL; |
423 | } | |
424 | ||
7267c094 | 425 | s = g_malloc0(sizeof(QEMUFileStdio)); |
a672b469 | 426 | |
7f79dd28 PB |
427 | s->stdio_file = fopen(filename, mode); |
428 | if (!s->stdio_file) | |
a672b469 | 429 | goto fail; |
c163b5ca | 430 | |
7f79dd28 | 431 | if(mode[0] == 'w') { |
9229bf3c | 432 | s->file = qemu_fopen_ops(s, &stdio_file_write_ops); |
7f79dd28 | 433 | } else { |
9229bf3c | 434 | s->file = qemu_fopen_ops(s, &stdio_file_read_ops); |
7f79dd28 PB |
435 | } |
436 | return s->file; | |
a672b469 | 437 | fail: |
7267c094 | 438 | g_free(s); |
a672b469 AL |
439 | return NULL; |
440 | } | |
441 | ||
178e08a5 | 442 | static int block_put_buffer(void *opaque, const uint8_t *buf, |
a672b469 AL |
443 | int64_t pos, int size) |
444 | { | |
45566e9c | 445 | bdrv_save_vmstate(opaque, buf, pos, size); |
a672b469 AL |
446 | return size; |
447 | } | |
448 | ||
178e08a5 | 449 | static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
a672b469 | 450 | { |
45566e9c | 451 | return bdrv_load_vmstate(opaque, buf, pos, size); |
a672b469 AL |
452 | } |
453 | ||
454 | static int bdrv_fclose(void *opaque) | |
455 | { | |
ad492c92 | 456 | return bdrv_flush(opaque); |
a672b469 AL |
457 | } |
458 | ||
9229bf3c PB |
459 | static const QEMUFileOps bdrv_read_ops = { |
460 | .get_buffer = block_get_buffer, | |
461 | .close = bdrv_fclose | |
462 | }; | |
463 | ||
464 | static const QEMUFileOps bdrv_write_ops = { | |
465 | .put_buffer = block_put_buffer, | |
466 | .close = bdrv_fclose | |
467 | }; | |
468 | ||
45566e9c | 469 | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) |
a672b469 | 470 | { |
a672b469 | 471 | if (is_writable) |
9229bf3c PB |
472 | return qemu_fopen_ops(bs, &bdrv_write_ops); |
473 | return qemu_fopen_ops(bs, &bdrv_read_ops); | |
a672b469 AL |
474 | } |
475 | ||
9229bf3c | 476 | QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops) |
a672b469 AL |
477 | { |
478 | QEMUFile *f; | |
479 | ||
7267c094 | 480 | f = g_malloc0(sizeof(QEMUFile)); |
a672b469 AL |
481 | |
482 | f->opaque = opaque; | |
9229bf3c | 483 | f->ops = ops; |
a672b469 | 484 | f->is_write = 0; |
a672b469 AL |
485 | return f; |
486 | } | |
487 | ||
624b9cc2 | 488 | int qemu_file_get_error(QEMUFile *f) |
a672b469 | 489 | { |
3961b4dd | 490 | return f->last_error; |
a672b469 AL |
491 | } |
492 | ||
05f28b83 | 493 | static void qemu_file_set_error(QEMUFile *f, int ret) |
4dabe248 | 494 | { |
afe41931 JQ |
495 | if (f->last_error == 0) { |
496 | f->last_error = ret; | |
497 | } | |
4dabe248 AL |
498 | } |
499 | ||
d82ca915 EH |
500 | /** Flushes QEMUFile buffer |
501 | * | |
d82ca915 | 502 | */ |
05f28b83 | 503 | static void qemu_fflush(QEMUFile *f) |
a672b469 | 504 | { |
7311bea3 JQ |
505 | int ret = 0; |
506 | ||
93bf2104 PB |
507 | if (!f->ops->put_buffer) { |
508 | return; | |
509 | } | |
a672b469 | 510 | if (f->is_write && f->buf_index > 0) { |
3f2d38fa | 511 | ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index); |
7311bea3 | 512 | if (ret >= 0) { |
3f2d38fa | 513 | f->pos += f->buf_index; |
7311bea3 | 514 | } |
a672b469 AL |
515 | f->buf_index = 0; |
516 | } | |
93bf2104 PB |
517 | if (ret < 0) { |
518 | qemu_file_set_error(f, ret); | |
519 | } | |
a672b469 AL |
520 | } |
521 | ||
522 | static void qemu_fill_buffer(QEMUFile *f) | |
523 | { | |
524 | int len; | |
0046c45b | 525 | int pending; |
a672b469 | 526 | |
9229bf3c | 527 | if (!f->ops->get_buffer) |
a672b469 AL |
528 | return; |
529 | ||
530 | if (f->is_write) | |
531 | abort(); | |
532 | ||
0046c45b JQ |
533 | pending = f->buf_size - f->buf_index; |
534 | if (pending > 0) { | |
535 | memmove(f->buf, f->buf + f->buf_index, pending); | |
536 | } | |
537 | f->buf_index = 0; | |
538 | f->buf_size = pending; | |
539 | ||
3f2d38fa | 540 | len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos, |
0046c45b | 541 | IO_BUF_SIZE - pending); |
a672b469 | 542 | if (len > 0) { |
0046c45b | 543 | f->buf_size += len; |
3f2d38fa | 544 | f->pos += len; |
fa39a30f | 545 | } else if (len == 0) { |
02c4a051 | 546 | qemu_file_set_error(f, -EIO); |
a672b469 | 547 | } else if (len != -EAGAIN) |
c29110d5 | 548 | qemu_file_set_error(f, len); |
a672b469 AL |
549 | } |
550 | ||
70eb6330 PB |
551 | int qemu_get_fd(QEMUFile *f) |
552 | { | |
553 | if (f->ops->get_fd) { | |
554 | return f->ops->get_fd(f->opaque); | |
555 | } | |
556 | return -1; | |
557 | } | |
558 | ||
d82ca915 EH |
559 | /** Closes the file |
560 | * | |
561 | * Returns negative error value if any error happened on previous operations or | |
562 | * while closing the file. Returns 0 or positive number on success. | |
563 | * | |
564 | * The meaning of return value on success depends on the specific backend | |
565 | * being used. | |
566 | */ | |
567 | int qemu_fclose(QEMUFile *f) | |
568 | { | |
29eee86f | 569 | int ret; |
93bf2104 PB |
570 | qemu_fflush(f); |
571 | ret = qemu_file_get_error(f); | |
7311bea3 | 572 | |
9229bf3c PB |
573 | if (f->ops->close) { |
574 | int ret2 = f->ops->close(f->opaque); | |
29eee86f JQ |
575 | if (ret >= 0) { |
576 | ret = ret2; | |
577 | } | |
7311bea3 | 578 | } |
d82ca915 EH |
579 | /* If any error was spotted before closing, we should report it |
580 | * instead of the close() return value. | |
581 | */ | |
582 | if (f->last_error) { | |
583 | ret = f->last_error; | |
584 | } | |
7267c094 | 585 | g_free(f); |
a672b469 AL |
586 | return ret; |
587 | } | |
588 | ||
a672b469 AL |
589 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
590 | { | |
591 | int l; | |
592 | ||
c10682cb JQ |
593 | if (f->last_error) { |
594 | return; | |
595 | } | |
596 | ||
597 | if (f->is_write == 0 && f->buf_index > 0) { | |
a672b469 AL |
598 | fprintf(stderr, |
599 | "Attempted to write to buffer while read buffer is not empty\n"); | |
600 | abort(); | |
601 | } | |
602 | ||
c10682cb | 603 | while (size > 0) { |
a672b469 AL |
604 | l = IO_BUF_SIZE - f->buf_index; |
605 | if (l > size) | |
606 | l = size; | |
607 | memcpy(f->buf + f->buf_index, buf, l); | |
608 | f->is_write = 1; | |
609 | f->buf_index += l; | |
1964a397 | 610 | f->bytes_xfer += l; |
a672b469 AL |
611 | buf += l; |
612 | size -= l; | |
7311bea3 | 613 | if (f->buf_index >= IO_BUF_SIZE) { |
93bf2104 PB |
614 | qemu_fflush(f); |
615 | if (qemu_file_get_error(f)) { | |
c10682cb JQ |
616 | break; |
617 | } | |
7311bea3 | 618 | } |
a672b469 AL |
619 | } |
620 | } | |
621 | ||
622 | void qemu_put_byte(QEMUFile *f, int v) | |
623 | { | |
c10682cb JQ |
624 | if (f->last_error) { |
625 | return; | |
626 | } | |
627 | ||
628 | if (f->is_write == 0 && f->buf_index > 0) { | |
a672b469 AL |
629 | fprintf(stderr, |
630 | "Attempted to write to buffer while read buffer is not empty\n"); | |
631 | abort(); | |
632 | } | |
633 | ||
634 | f->buf[f->buf_index++] = v; | |
635 | f->is_write = 1; | |
7311bea3 | 636 | if (f->buf_index >= IO_BUF_SIZE) { |
93bf2104 | 637 | qemu_fflush(f); |
7311bea3 | 638 | } |
a672b469 AL |
639 | } |
640 | ||
c6380724 | 641 | static void qemu_file_skip(QEMUFile *f, int size) |
a672b469 | 642 | { |
c6380724 JQ |
643 | if (f->buf_index + size <= f->buf_size) { |
644 | f->buf_index += size; | |
645 | } | |
646 | } | |
647 | ||
648 | static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset) | |
a672b469 | 649 | { |
c6380724 JQ |
650 | int pending; |
651 | int index; | |
a672b469 | 652 | |
b9ce1454 | 653 | if (f->is_write) { |
a672b469 | 654 | abort(); |
b9ce1454 | 655 | } |
a672b469 | 656 | |
c6380724 JQ |
657 | index = f->buf_index + offset; |
658 | pending = f->buf_size - index; | |
659 | if (pending < size) { | |
660 | qemu_fill_buffer(f); | |
661 | index = f->buf_index + offset; | |
662 | pending = f->buf_size - index; | |
663 | } | |
664 | ||
665 | if (pending <= 0) { | |
666 | return 0; | |
667 | } | |
668 | if (size > pending) { | |
669 | size = pending; | |
670 | } | |
671 | ||
672 | memcpy(buf, f->buf + index, size); | |
673 | return size; | |
674 | } | |
675 | ||
676 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) | |
677 | { | |
678 | int pending = size; | |
679 | int done = 0; | |
680 | ||
681 | while (pending > 0) { | |
682 | int res; | |
683 | ||
684 | res = qemu_peek_buffer(f, buf, pending, 0); | |
685 | if (res == 0) { | |
686 | return done; | |
a672b469 | 687 | } |
c6380724 JQ |
688 | qemu_file_skip(f, res); |
689 | buf += res; | |
690 | pending -= res; | |
691 | done += res; | |
a672b469 | 692 | } |
c6380724 | 693 | return done; |
a672b469 AL |
694 | } |
695 | ||
c6380724 | 696 | static int qemu_peek_byte(QEMUFile *f, int offset) |
811814bd | 697 | { |
c6380724 JQ |
698 | int index = f->buf_index + offset; |
699 | ||
b9ce1454 | 700 | if (f->is_write) { |
811814bd | 701 | abort(); |
b9ce1454 | 702 | } |
811814bd | 703 | |
c6380724 | 704 | if (index >= f->buf_size) { |
811814bd | 705 | qemu_fill_buffer(f); |
c6380724 JQ |
706 | index = f->buf_index + offset; |
707 | if (index >= f->buf_size) { | |
811814bd | 708 | return 0; |
b9ce1454 | 709 | } |
811814bd | 710 | } |
c6380724 | 711 | return f->buf[index]; |
811814bd JQ |
712 | } |
713 | ||
a672b469 AL |
714 | int qemu_get_byte(QEMUFile *f) |
715 | { | |
65f3bb3d | 716 | int result; |
a672b469 | 717 | |
c6380724 JQ |
718 | result = qemu_peek_byte(f, 0); |
719 | qemu_file_skip(f, 1); | |
65f3bb3d | 720 | return result; |
a672b469 AL |
721 | } |
722 | ||
ad55ab42 | 723 | int64_t qemu_ftell(QEMUFile *f) |
a672b469 | 724 | { |
3f2d38fa PB |
725 | qemu_fflush(f); |
726 | return f->pos; | |
a672b469 AL |
727 | } |
728 | ||
a672b469 AL |
729 | int qemu_file_rate_limit(QEMUFile *f) |
730 | { | |
1964a397 PB |
731 | if (qemu_file_get_error(f)) { |
732 | return 1; | |
733 | } | |
734 | if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) { | |
735 | return 1; | |
736 | } | |
a672b469 AL |
737 | return 0; |
738 | } | |
739 | ||
3d002df3 | 740 | int64_t qemu_file_get_rate_limit(QEMUFile *f) |
c163b5ca | 741 | { |
1964a397 | 742 | return f->xfer_limit; |
c163b5ca | 743 | } |
744 | ||
1964a397 | 745 | void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit) |
19629537 | 746 | { |
1964a397 PB |
747 | f->xfer_limit = limit; |
748 | } | |
19629537 | 749 | |
1964a397 PB |
750 | void qemu_file_reset_rate_limit(QEMUFile *f) |
751 | { | |
752 | f->bytes_xfer = 0; | |
19629537 GC |
753 | } |
754 | ||
a672b469 AL |
755 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
756 | { | |
757 | qemu_put_byte(f, v >> 8); | |
758 | qemu_put_byte(f, v); | |
759 | } | |
760 | ||
761 | void qemu_put_be32(QEMUFile *f, unsigned int v) | |
762 | { | |
763 | qemu_put_byte(f, v >> 24); | |
764 | qemu_put_byte(f, v >> 16); | |
765 | qemu_put_byte(f, v >> 8); | |
766 | qemu_put_byte(f, v); | |
767 | } | |
768 | ||
769 | void qemu_put_be64(QEMUFile *f, uint64_t v) | |
770 | { | |
771 | qemu_put_be32(f, v >> 32); | |
772 | qemu_put_be32(f, v); | |
773 | } | |
774 | ||
775 | unsigned int qemu_get_be16(QEMUFile *f) | |
776 | { | |
777 | unsigned int v; | |
778 | v = qemu_get_byte(f) << 8; | |
779 | v |= qemu_get_byte(f); | |
780 | return v; | |
781 | } | |
782 | ||
783 | unsigned int qemu_get_be32(QEMUFile *f) | |
784 | { | |
785 | unsigned int v; | |
786 | v = qemu_get_byte(f) << 24; | |
787 | v |= qemu_get_byte(f) << 16; | |
788 | v |= qemu_get_byte(f) << 8; | |
789 | v |= qemu_get_byte(f); | |
790 | return v; | |
791 | } | |
792 | ||
793 | uint64_t qemu_get_be64(QEMUFile *f) | |
794 | { | |
795 | uint64_t v; | |
796 | v = (uint64_t)qemu_get_be32(f) << 32; | |
797 | v |= qemu_get_be32(f); | |
798 | return v; | |
799 | } | |
800 | ||
2ff68d07 PB |
801 | |
802 | /* timer */ | |
803 | ||
804 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) | |
805 | { | |
806 | uint64_t expire_time; | |
807 | ||
808 | expire_time = qemu_timer_expire_time_ns(ts); | |
809 | qemu_put_be64(f, expire_time); | |
810 | } | |
811 | ||
812 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) | |
813 | { | |
814 | uint64_t expire_time; | |
815 | ||
816 | expire_time = qemu_get_be64(f); | |
817 | if (expire_time != -1) { | |
818 | qemu_mod_timer_ns(ts, expire_time); | |
819 | } else { | |
820 | qemu_del_timer(ts); | |
821 | } | |
822 | } | |
823 | ||
824 | ||
cdae5cfb GH |
825 | /* bool */ |
826 | ||
827 | static int get_bool(QEMUFile *f, void *pv, size_t size) | |
828 | { | |
829 | bool *v = pv; | |
830 | *v = qemu_get_byte(f); | |
831 | return 0; | |
832 | } | |
833 | ||
834 | static void put_bool(QEMUFile *f, void *pv, size_t size) | |
835 | { | |
836 | bool *v = pv; | |
837 | qemu_put_byte(f, *v); | |
838 | } | |
839 | ||
840 | const VMStateInfo vmstate_info_bool = { | |
841 | .name = "bool", | |
842 | .get = get_bool, | |
843 | .put = put_bool, | |
844 | }; | |
845 | ||
9ed7d6ae JQ |
846 | /* 8 bit int */ |
847 | ||
848 | static int get_int8(QEMUFile *f, void *pv, size_t size) | |
849 | { | |
850 | int8_t *v = pv; | |
851 | qemu_get_s8s(f, v); | |
852 | return 0; | |
853 | } | |
854 | ||
84e2e3eb | 855 | static void put_int8(QEMUFile *f, void *pv, size_t size) |
9ed7d6ae | 856 | { |
84e2e3eb | 857 | int8_t *v = pv; |
9ed7d6ae JQ |
858 | qemu_put_s8s(f, v); |
859 | } | |
860 | ||
861 | const VMStateInfo vmstate_info_int8 = { | |
862 | .name = "int8", | |
863 | .get = get_int8, | |
864 | .put = put_int8, | |
865 | }; | |
866 | ||
867 | /* 16 bit int */ | |
868 | ||
869 | static int get_int16(QEMUFile *f, void *pv, size_t size) | |
870 | { | |
871 | int16_t *v = pv; | |
872 | qemu_get_sbe16s(f, v); | |
873 | return 0; | |
874 | } | |
875 | ||
84e2e3eb | 876 | static void put_int16(QEMUFile *f, void *pv, size_t size) |
9ed7d6ae | 877 | { |
84e2e3eb | 878 | int16_t *v = pv; |
9ed7d6ae JQ |
879 | qemu_put_sbe16s(f, v); |
880 | } | |
881 | ||
882 | const VMStateInfo vmstate_info_int16 = { | |
883 | .name = "int16", | |
884 | .get = get_int16, | |
885 | .put = put_int16, | |
886 | }; | |
887 | ||
888 | /* 32 bit int */ | |
889 | ||
890 | static int get_int32(QEMUFile *f, void *pv, size_t size) | |
891 | { | |
892 | int32_t *v = pv; | |
893 | qemu_get_sbe32s(f, v); | |
894 | return 0; | |
895 | } | |
896 | ||
84e2e3eb | 897 | static void put_int32(QEMUFile *f, void *pv, size_t size) |
9ed7d6ae | 898 | { |
84e2e3eb | 899 | int32_t *v = pv; |
9ed7d6ae JQ |
900 | qemu_put_sbe32s(f, v); |
901 | } | |
902 | ||
903 | const VMStateInfo vmstate_info_int32 = { | |
904 | .name = "int32", | |
905 | .get = get_int32, | |
906 | .put = put_int32, | |
907 | }; | |
908 | ||
82501660 JQ |
909 | /* 32 bit int. See that the received value is the same than the one |
910 | in the field */ | |
911 | ||
912 | static int get_int32_equal(QEMUFile *f, void *pv, size_t size) | |
913 | { | |
914 | int32_t *v = pv; | |
915 | int32_t v2; | |
916 | qemu_get_sbe32s(f, &v2); | |
917 | ||
918 | if (*v == v2) | |
919 | return 0; | |
920 | return -EINVAL; | |
921 | } | |
922 | ||
923 | const VMStateInfo vmstate_info_int32_equal = { | |
924 | .name = "int32 equal", | |
925 | .get = get_int32_equal, | |
926 | .put = put_int32, | |
927 | }; | |
928 | ||
0a031e0a JQ |
929 | /* 32 bit int. See that the received value is the less or the same |
930 | than the one in the field */ | |
931 | ||
932 | static int get_int32_le(QEMUFile *f, void *pv, size_t size) | |
933 | { | |
934 | int32_t *old = pv; | |
935 | int32_t new; | |
936 | qemu_get_sbe32s(f, &new); | |
937 | ||
938 | if (*old <= new) | |
939 | return 0; | |
940 | return -EINVAL; | |
941 | } | |
942 | ||
943 | const VMStateInfo vmstate_info_int32_le = { | |
944 | .name = "int32 equal", | |
945 | .get = get_int32_le, | |
946 | .put = put_int32, | |
947 | }; | |
948 | ||
9ed7d6ae JQ |
949 | /* 64 bit int */ |
950 | ||
951 | static int get_int64(QEMUFile *f, void *pv, size_t size) | |
952 | { | |
953 | int64_t *v = pv; | |
954 | qemu_get_sbe64s(f, v); | |
955 | return 0; | |
956 | } | |
957 | ||
84e2e3eb | 958 | static void put_int64(QEMUFile *f, void *pv, size_t size) |
9ed7d6ae | 959 | { |
84e2e3eb | 960 | int64_t *v = pv; |
9ed7d6ae JQ |
961 | qemu_put_sbe64s(f, v); |
962 | } | |
963 | ||
964 | const VMStateInfo vmstate_info_int64 = { | |
965 | .name = "int64", | |
966 | .get = get_int64, | |
967 | .put = put_int64, | |
968 | }; | |
969 | ||
970 | /* 8 bit unsigned int */ | |
971 | ||
972 | static int get_uint8(QEMUFile *f, void *pv, size_t size) | |
973 | { | |
974 | uint8_t *v = pv; | |
975 | qemu_get_8s(f, v); | |
976 | return 0; | |
977 | } | |
978 | ||
84e2e3eb | 979 | static void put_uint8(QEMUFile *f, void *pv, size_t size) |
9ed7d6ae | 980 | { |
84e2e3eb | 981 | uint8_t *v = pv; |
9ed7d6ae JQ |
982 | qemu_put_8s(f, v); |
983 | } | |
984 | ||
985 | const VMStateInfo vmstate_info_uint8 = { | |
986 | .name = "uint8", | |
987 | .get = get_uint8, | |
988 | .put = put_uint8, | |
989 | }; | |
990 | ||
991 | /* 16 bit unsigned int */ | |
992 | ||
993 | static int get_uint16(QEMUFile *f, void *pv, size_t size) | |
994 | { | |
995 | uint16_t *v = pv; | |
996 | qemu_get_be16s(f, v); | |
997 | return 0; | |
998 | } | |
999 | ||
84e2e3eb | 1000 | static void put_uint16(QEMUFile *f, void *pv, size_t size) |
9ed7d6ae | 1001 | { |
84e2e3eb | 1002 | uint16_t *v = pv; |
9ed7d6ae JQ |
1003 | qemu_put_be16s(f, v); |
1004 | } | |
1005 | ||
1006 | const VMStateInfo vmstate_info_uint16 = { | |
1007 | .name = "uint16", | |
1008 | .get = get_uint16, | |
1009 | .put = put_uint16, | |
1010 | }; | |
1011 | ||
1012 | /* 32 bit unsigned int */ | |
1013 | ||
1014 | static int get_uint32(QEMUFile *f, void *pv, size_t size) | |
1015 | { | |
1016 | uint32_t *v = pv; | |
1017 | qemu_get_be32s(f, v); | |
1018 | return 0; | |
1019 | } | |
1020 | ||
84e2e3eb | 1021 | static void put_uint32(QEMUFile *f, void *pv, size_t size) |
9ed7d6ae | 1022 | { |
84e2e3eb | 1023 | uint32_t *v = pv; |
9ed7d6ae JQ |
1024 | qemu_put_be32s(f, v); |
1025 | } | |
1026 | ||
1027 | const VMStateInfo vmstate_info_uint32 = { | |
1028 | .name = "uint32", | |
1029 | .get = get_uint32, | |
1030 | .put = put_uint32, | |
1031 | }; | |
1032 | ||
9122a8fe JQ |
1033 | /* 32 bit uint. See that the received value is the same than the one |
1034 | in the field */ | |
1035 | ||
1036 | static int get_uint32_equal(QEMUFile *f, void *pv, size_t size) | |
1037 | { | |
1038 | uint32_t *v = pv; | |
1039 | uint32_t v2; | |
1040 | qemu_get_be32s(f, &v2); | |
1041 | ||
1042 | if (*v == v2) { | |
1043 | return 0; | |
1044 | } | |
1045 | return -EINVAL; | |
1046 | } | |
1047 | ||
1048 | const VMStateInfo vmstate_info_uint32_equal = { | |
1049 | .name = "uint32 equal", | |
1050 | .get = get_uint32_equal, | |
1051 | .put = put_uint32, | |
1052 | }; | |
1053 | ||
9ed7d6ae JQ |
1054 | /* 64 bit unsigned int */ |
1055 | ||
1056 | static int get_uint64(QEMUFile *f, void *pv, size_t size) | |
1057 | { | |
1058 | uint64_t *v = pv; | |
1059 | qemu_get_be64s(f, v); | |
1060 | return 0; | |
1061 | } | |
1062 | ||
84e2e3eb | 1063 | static void put_uint64(QEMUFile *f, void *pv, size_t size) |
9ed7d6ae | 1064 | { |
84e2e3eb | 1065 | uint64_t *v = pv; |
9ed7d6ae JQ |
1066 | qemu_put_be64s(f, v); |
1067 | } | |
1068 | ||
1069 | const VMStateInfo vmstate_info_uint64 = { | |
1070 | .name = "uint64", | |
1071 | .get = get_uint64, | |
1072 | .put = put_uint64, | |
1073 | }; | |
1074 | ||
e344b8a1 DG |
1075 | /* 64 bit unsigned int. See that the received value is the same than the one |
1076 | in the field */ | |
1077 | ||
1078 | static int get_uint64_equal(QEMUFile *f, void *pv, size_t size) | |
1079 | { | |
1080 | uint64_t *v = pv; | |
1081 | uint64_t v2; | |
1082 | qemu_get_be64s(f, &v2); | |
1083 | ||
1084 | if (*v == v2) { | |
1085 | return 0; | |
1086 | } | |
1087 | return -EINVAL; | |
1088 | } | |
1089 | ||
1090 | const VMStateInfo vmstate_info_uint64_equal = { | |
1091 | .name = "int64 equal", | |
1092 | .get = get_uint64_equal, | |
1093 | .put = put_uint64, | |
1094 | }; | |
1095 | ||
80cd83e7 JQ |
1096 | /* 8 bit int. See that the received value is the same than the one |
1097 | in the field */ | |
1098 | ||
1099 | static int get_uint8_equal(QEMUFile *f, void *pv, size_t size) | |
1100 | { | |
1101 | uint8_t *v = pv; | |
1102 | uint8_t v2; | |
1103 | qemu_get_8s(f, &v2); | |
1104 | ||
1105 | if (*v == v2) | |
1106 | return 0; | |
1107 | return -EINVAL; | |
1108 | } | |
1109 | ||
1110 | const VMStateInfo vmstate_info_uint8_equal = { | |
aa1cce69 | 1111 | .name = "uint8 equal", |
80cd83e7 JQ |
1112 | .get = get_uint8_equal, |
1113 | .put = put_uint8, | |
1114 | }; | |
1115 | ||
dc3b83a0 JQ |
1116 | /* 16 bit unsigned int int. See that the received value is the same than the one |
1117 | in the field */ | |
1118 | ||
1119 | static int get_uint16_equal(QEMUFile *f, void *pv, size_t size) | |
1120 | { | |
1121 | uint16_t *v = pv; | |
1122 | uint16_t v2; | |
1123 | qemu_get_be16s(f, &v2); | |
1124 | ||
1125 | if (*v == v2) | |
1126 | return 0; | |
1127 | return -EINVAL; | |
1128 | } | |
1129 | ||
1130 | const VMStateInfo vmstate_info_uint16_equal = { | |
1131 | .name = "uint16 equal", | |
1132 | .get = get_uint16_equal, | |
1133 | .put = put_uint16, | |
1134 | }; | |
1135 | ||
dde0463b JQ |
1136 | /* timers */ |
1137 | ||
1138 | static int get_timer(QEMUFile *f, void *pv, size_t size) | |
1139 | { | |
1140 | QEMUTimer *v = pv; | |
1141 | qemu_get_timer(f, v); | |
1142 | return 0; | |
1143 | } | |
1144 | ||
84e2e3eb | 1145 | static void put_timer(QEMUFile *f, void *pv, size_t size) |
dde0463b | 1146 | { |
84e2e3eb | 1147 | QEMUTimer *v = pv; |
dde0463b JQ |
1148 | qemu_put_timer(f, v); |
1149 | } | |
1150 | ||
1151 | const VMStateInfo vmstate_info_timer = { | |
1152 | .name = "timer", | |
1153 | .get = get_timer, | |
1154 | .put = put_timer, | |
1155 | }; | |
1156 | ||
6f67c50f JQ |
1157 | /* uint8_t buffers */ |
1158 | ||
1159 | static int get_buffer(QEMUFile *f, void *pv, size_t size) | |
1160 | { | |
1161 | uint8_t *v = pv; | |
1162 | qemu_get_buffer(f, v, size); | |
1163 | return 0; | |
1164 | } | |
1165 | ||
84e2e3eb | 1166 | static void put_buffer(QEMUFile *f, void *pv, size_t size) |
6f67c50f | 1167 | { |
84e2e3eb | 1168 | uint8_t *v = pv; |
6f67c50f JQ |
1169 | qemu_put_buffer(f, v, size); |
1170 | } | |
1171 | ||
1172 | const VMStateInfo vmstate_info_buffer = { | |
1173 | .name = "buffer", | |
1174 | .get = get_buffer, | |
1175 | .put = put_buffer, | |
1176 | }; | |
1177 | ||
76507c75 | 1178 | /* unused buffers: space that was used for some fields that are |
61cc8701 | 1179 | not useful anymore */ |
76507c75 JQ |
1180 | |
1181 | static int get_unused_buffer(QEMUFile *f, void *pv, size_t size) | |
1182 | { | |
21174c34 JK |
1183 | uint8_t buf[1024]; |
1184 | int block_len; | |
1185 | ||
1186 | while (size > 0) { | |
1187 | block_len = MIN(sizeof(buf), size); | |
1188 | size -= block_len; | |
1189 | qemu_get_buffer(f, buf, block_len); | |
1190 | } | |
1191 | return 0; | |
76507c75 JQ |
1192 | } |
1193 | ||
1194 | static void put_unused_buffer(QEMUFile *f, void *pv, size_t size) | |
1195 | { | |
21174c34 JK |
1196 | static const uint8_t buf[1024]; |
1197 | int block_len; | |
1198 | ||
1199 | while (size > 0) { | |
1200 | block_len = MIN(sizeof(buf), size); | |
1201 | size -= block_len; | |
1202 | qemu_put_buffer(f, buf, block_len); | |
1203 | } | |
76507c75 JQ |
1204 | } |
1205 | ||
1206 | const VMStateInfo vmstate_info_unused_buffer = { | |
1207 | .name = "unused_buffer", | |
1208 | .get = get_unused_buffer, | |
1209 | .put = put_unused_buffer, | |
1210 | }; | |
1211 | ||
08e99e29 PM |
1212 | /* bitmaps (as defined by bitmap.h). Note that size here is the size |
1213 | * of the bitmap in bits. The on-the-wire format of a bitmap is 64 | |
1214 | * bit words with the bits in big endian order. The in-memory format | |
1215 | * is an array of 'unsigned long', which may be either 32 or 64 bits. | |
1216 | */ | |
1217 | /* This is the number of 64 bit words sent over the wire */ | |
1218 | #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64) | |
1219 | static int get_bitmap(QEMUFile *f, void *pv, size_t size) | |
1220 | { | |
1221 | unsigned long *bmp = pv; | |
1222 | int i, idx = 0; | |
1223 | for (i = 0; i < BITS_TO_U64S(size); i++) { | |
1224 | uint64_t w = qemu_get_be64(f); | |
1225 | bmp[idx++] = w; | |
1226 | if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) { | |
1227 | bmp[idx++] = w >> 32; | |
1228 | } | |
1229 | } | |
1230 | return 0; | |
1231 | } | |
1232 | ||
1233 | static void put_bitmap(QEMUFile *f, void *pv, size_t size) | |
1234 | { | |
1235 | unsigned long *bmp = pv; | |
1236 | int i, idx = 0; | |
1237 | for (i = 0; i < BITS_TO_U64S(size); i++) { | |
1238 | uint64_t w = bmp[idx++]; | |
1239 | if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) { | |
1240 | w |= ((uint64_t)bmp[idx++]) << 32; | |
1241 | } | |
1242 | qemu_put_be64(f, w); | |
1243 | } | |
1244 | } | |
1245 | ||
1246 | const VMStateInfo vmstate_info_bitmap = { | |
1247 | .name = "bitmap", | |
1248 | .get = get_bitmap, | |
1249 | .put = put_bitmap, | |
1250 | }; | |
1251 | ||
7685ee6a AW |
1252 | typedef struct CompatEntry { |
1253 | char idstr[256]; | |
1254 | int instance_id; | |
1255 | } CompatEntry; | |
1256 | ||
a672b469 | 1257 | typedef struct SaveStateEntry { |
72cf2d4f | 1258 | QTAILQ_ENTRY(SaveStateEntry) entry; |
a672b469 AL |
1259 | char idstr[256]; |
1260 | int instance_id; | |
4d2ffa08 | 1261 | int alias_id; |
a672b469 AL |
1262 | int version_id; |
1263 | int section_id; | |
22ea40f4 | 1264 | SaveVMHandlers *ops; |
9ed7d6ae | 1265 | const VMStateDescription *vmsd; |
a672b469 | 1266 | void *opaque; |
7685ee6a | 1267 | CompatEntry *compat; |
24312968 | 1268 | int no_migrate; |
a7ae8355 | 1269 | int is_ram; |
a672b469 AL |
1270 | } SaveStateEntry; |
1271 | ||
c163b5ca | 1272 | |
72cf2d4f BS |
1273 | static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers = |
1274 | QTAILQ_HEAD_INITIALIZER(savevm_handlers); | |
9ed7d6ae | 1275 | static int global_section_id; |
a672b469 | 1276 | |
8718e999 JQ |
1277 | static int calculate_new_instance_id(const char *idstr) |
1278 | { | |
1279 | SaveStateEntry *se; | |
1280 | int instance_id = 0; | |
1281 | ||
72cf2d4f | 1282 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
8718e999 JQ |
1283 | if (strcmp(idstr, se->idstr) == 0 |
1284 | && instance_id <= se->instance_id) { | |
1285 | instance_id = se->instance_id + 1; | |
1286 | } | |
1287 | } | |
1288 | return instance_id; | |
1289 | } | |
1290 | ||
7685ee6a AW |
1291 | static int calculate_compat_instance_id(const char *idstr) |
1292 | { | |
1293 | SaveStateEntry *se; | |
1294 | int instance_id = 0; | |
1295 | ||
1296 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { | |
1297 | if (!se->compat) | |
1298 | continue; | |
1299 | ||
1300 | if (strcmp(idstr, se->compat->idstr) == 0 | |
1301 | && instance_id <= se->compat->instance_id) { | |
1302 | instance_id = se->compat->instance_id + 1; | |
1303 | } | |
1304 | } | |
1305 | return instance_id; | |
1306 | } | |
1307 | ||
a672b469 AL |
1308 | /* TODO: Individual devices generally have very little idea about the rest |
1309 | of the system, so instance_id should be removed/replaced. | |
1310 | Meanwhile pass -1 as instance_id if you do not already have a clearly | |
1311 | distinguishing id for all instances of your device class. */ | |
0be71e32 AW |
1312 | int register_savevm_live(DeviceState *dev, |
1313 | const char *idstr, | |
a672b469 AL |
1314 | int instance_id, |
1315 | int version_id, | |
7908c78d | 1316 | SaveVMHandlers *ops, |
a672b469 AL |
1317 | void *opaque) |
1318 | { | |
8718e999 | 1319 | SaveStateEntry *se; |
a672b469 | 1320 | |
7267c094 | 1321 | se = g_malloc0(sizeof(SaveStateEntry)); |
a672b469 AL |
1322 | se->version_id = version_id; |
1323 | se->section_id = global_section_id++; | |
7908c78d | 1324 | se->ops = ops; |
a672b469 | 1325 | se->opaque = opaque; |
9ed7d6ae | 1326 | se->vmsd = NULL; |
24312968 | 1327 | se->no_migrate = 0; |
a7ae8355 | 1328 | /* if this is a live_savem then set is_ram */ |
16310a3c | 1329 | if (ops->save_live_setup != NULL) { |
a7ae8355 SS |
1330 | se->is_ram = 1; |
1331 | } | |
a672b469 | 1332 | |
09e5ab63 AL |
1333 | if (dev) { |
1334 | char *id = qdev_get_dev_path(dev); | |
7685ee6a AW |
1335 | if (id) { |
1336 | pstrcpy(se->idstr, sizeof(se->idstr), id); | |
1337 | pstrcat(se->idstr, sizeof(se->idstr), "/"); | |
7267c094 | 1338 | g_free(id); |
7685ee6a | 1339 | |
7267c094 | 1340 | se->compat = g_malloc0(sizeof(CompatEntry)); |
7685ee6a AW |
1341 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr); |
1342 | se->compat->instance_id = instance_id == -1 ? | |
1343 | calculate_compat_instance_id(idstr) : instance_id; | |
1344 | instance_id = -1; | |
1345 | } | |
1346 | } | |
1347 | pstrcat(se->idstr, sizeof(se->idstr), idstr); | |
1348 | ||
8718e999 | 1349 | if (instance_id == -1) { |
7685ee6a | 1350 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
1351 | } else { |
1352 | se->instance_id = instance_id; | |
a672b469 | 1353 | } |
7685ee6a | 1354 | assert(!se->compat || se->instance_id == 0); |
8718e999 | 1355 | /* add at the end of list */ |
72cf2d4f | 1356 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
a672b469 AL |
1357 | return 0; |
1358 | } | |
1359 | ||
0be71e32 AW |
1360 | int register_savevm(DeviceState *dev, |
1361 | const char *idstr, | |
a672b469 AL |
1362 | int instance_id, |
1363 | int version_id, | |
1364 | SaveStateHandler *save_state, | |
1365 | LoadStateHandler *load_state, | |
1366 | void *opaque) | |
1367 | { | |
7908c78d JQ |
1368 | SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers)); |
1369 | ops->save_state = save_state; | |
1370 | ops->load_state = load_state; | |
0be71e32 | 1371 | return register_savevm_live(dev, idstr, instance_id, version_id, |
7908c78d | 1372 | ops, opaque); |
a672b469 AL |
1373 | } |
1374 | ||
0be71e32 | 1375 | void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque) |
41bd13af | 1376 | { |
8718e999 | 1377 | SaveStateEntry *se, *new_se; |
7685ee6a AW |
1378 | char id[256] = ""; |
1379 | ||
09e5ab63 AL |
1380 | if (dev) { |
1381 | char *path = qdev_get_dev_path(dev); | |
7685ee6a AW |
1382 | if (path) { |
1383 | pstrcpy(id, sizeof(id), path); | |
1384 | pstrcat(id, sizeof(id), "/"); | |
7267c094 | 1385 | g_free(path); |
7685ee6a AW |
1386 | } |
1387 | } | |
1388 | pstrcat(id, sizeof(id), idstr); | |
41bd13af | 1389 | |
72cf2d4f | 1390 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
7685ee6a | 1391 | if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { |
72cf2d4f | 1392 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
69e58af9 | 1393 | if (se->compat) { |
7267c094 | 1394 | g_free(se->compat); |
69e58af9 | 1395 | } |
22ea40f4 | 1396 | g_free(se->ops); |
7267c094 | 1397 | g_free(se); |
41bd13af | 1398 | } |
41bd13af AL |
1399 | } |
1400 | } | |
1401 | ||
0be71e32 | 1402 | int vmstate_register_with_alias_id(DeviceState *dev, int instance_id, |
4d2ffa08 JK |
1403 | const VMStateDescription *vmsd, |
1404 | void *opaque, int alias_id, | |
1405 | int required_for_version) | |
9ed7d6ae | 1406 | { |
8718e999 | 1407 | SaveStateEntry *se; |
9ed7d6ae | 1408 | |
4d2ffa08 JK |
1409 | /* If this triggers, alias support can be dropped for the vmsd. */ |
1410 | assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); | |
1411 | ||
7267c094 | 1412 | se = g_malloc0(sizeof(SaveStateEntry)); |
9ed7d6ae JQ |
1413 | se->version_id = vmsd->version_id; |
1414 | se->section_id = global_section_id++; | |
9ed7d6ae JQ |
1415 | se->opaque = opaque; |
1416 | se->vmsd = vmsd; | |
4d2ffa08 | 1417 | se->alias_id = alias_id; |
2837c8ea | 1418 | se->no_migrate = vmsd->unmigratable; |
9ed7d6ae | 1419 | |
09e5ab63 AL |
1420 | if (dev) { |
1421 | char *id = qdev_get_dev_path(dev); | |
7685ee6a AW |
1422 | if (id) { |
1423 | pstrcpy(se->idstr, sizeof(se->idstr), id); | |
1424 | pstrcat(se->idstr, sizeof(se->idstr), "/"); | |
7267c094 | 1425 | g_free(id); |
7685ee6a | 1426 | |
7267c094 | 1427 | se->compat = g_malloc0(sizeof(CompatEntry)); |
7685ee6a AW |
1428 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); |
1429 | se->compat->instance_id = instance_id == -1 ? | |
1430 | calculate_compat_instance_id(vmsd->name) : instance_id; | |
1431 | instance_id = -1; | |
1432 | } | |
1433 | } | |
1434 | pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); | |
1435 | ||
8718e999 | 1436 | if (instance_id == -1) { |
7685ee6a | 1437 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
1438 | } else { |
1439 | se->instance_id = instance_id; | |
9ed7d6ae | 1440 | } |
7685ee6a | 1441 | assert(!se->compat || se->instance_id == 0); |
8718e999 | 1442 | /* add at the end of list */ |
72cf2d4f | 1443 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
9ed7d6ae JQ |
1444 | return 0; |
1445 | } | |
1446 | ||
0be71e32 AW |
1447 | void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd, |
1448 | void *opaque) | |
9ed7d6ae | 1449 | { |
1eb7538b JQ |
1450 | SaveStateEntry *se, *new_se; |
1451 | ||
72cf2d4f | 1452 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
1eb7538b | 1453 | if (se->vmsd == vmsd && se->opaque == opaque) { |
72cf2d4f | 1454 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
69e58af9 | 1455 | if (se->compat) { |
7267c094 | 1456 | g_free(se->compat); |
69e58af9 | 1457 | } |
7267c094 | 1458 | g_free(se); |
1eb7538b JQ |
1459 | } |
1460 | } | |
9ed7d6ae JQ |
1461 | } |
1462 | ||
811814bd JQ |
1463 | static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, |
1464 | void *opaque); | |
1465 | static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd, | |
1466 | void *opaque); | |
1467 | ||
9ed7d6ae JQ |
1468 | int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd, |
1469 | void *opaque, int version_id) | |
1470 | { | |
1471 | VMStateField *field = vmsd->fields; | |
811814bd | 1472 | int ret; |
9ed7d6ae JQ |
1473 | |
1474 | if (version_id > vmsd->version_id) { | |
1475 | return -EINVAL; | |
1476 | } | |
1477 | if (version_id < vmsd->minimum_version_id_old) { | |
1478 | return -EINVAL; | |
1479 | } | |
1480 | if (version_id < vmsd->minimum_version_id) { | |
1481 | return vmsd->load_state_old(f, opaque, version_id); | |
1482 | } | |
fd4d52de JQ |
1483 | if (vmsd->pre_load) { |
1484 | int ret = vmsd->pre_load(opaque); | |
1485 | if (ret) | |
1486 | return ret; | |
1487 | } | |
9ed7d6ae | 1488 | while(field->name) { |
f11f6a5f JQ |
1489 | if ((field->field_exists && |
1490 | field->field_exists(opaque, version_id)) || | |
1491 | (!field->field_exists && | |
1492 | field->version_id <= version_id)) { | |
f752a6aa | 1493 | void *base_addr = opaque + field->offset; |
811814bd | 1494 | int i, n_elems = 1; |
e61a1e0a | 1495 | int size = field->size; |
9ed7d6ae | 1496 | |
e61a1e0a JQ |
1497 | if (field->flags & VMS_VBUFFER) { |
1498 | size = *(int32_t *)(opaque+field->size_offset); | |
33599e2a JQ |
1499 | if (field->flags & VMS_MULTIPLY) { |
1500 | size *= field->size; | |
1501 | } | |
e61a1e0a | 1502 | } |
f752a6aa JQ |
1503 | if (field->flags & VMS_ARRAY) { |
1504 | n_elems = field->num; | |
d6698281 JQ |
1505 | } else if (field->flags & VMS_VARRAY_INT32) { |
1506 | n_elems = *(int32_t *)(opaque+field->num_offset); | |
a624b086 JQ |
1507 | } else if (field->flags & VMS_VARRAY_UINT32) { |
1508 | n_elems = *(uint32_t *)(opaque+field->num_offset); | |
bdb4941d JQ |
1509 | } else if (field->flags & VMS_VARRAY_UINT16) { |
1510 | n_elems = *(uint16_t *)(opaque+field->num_offset); | |
82fa39b7 JQ |
1511 | } else if (field->flags & VMS_VARRAY_UINT8) { |
1512 | n_elems = *(uint8_t *)(opaque+field->num_offset); | |
f752a6aa | 1513 | } |
dde0463b | 1514 | if (field->flags & VMS_POINTER) { |
e61a1e0a | 1515 | base_addr = *(void **)base_addr + field->start; |
dde0463b | 1516 | } |
f752a6aa | 1517 | for (i = 0; i < n_elems; i++) { |
e61a1e0a | 1518 | void *addr = base_addr + size * i; |
ec245e21 | 1519 | |
19df438b JQ |
1520 | if (field->flags & VMS_ARRAY_OF_POINTER) { |
1521 | addr = *(void **)addr; | |
1522 | } | |
ec245e21 | 1523 | if (field->flags & VMS_STRUCT) { |
fa3aad24 | 1524 | ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id); |
ec245e21 | 1525 | } else { |
e61a1e0a | 1526 | ret = field->info->get(f, addr, size); |
ec245e21 JQ |
1527 | |
1528 | } | |
f752a6aa JQ |
1529 | if (ret < 0) { |
1530 | return ret; | |
1531 | } | |
9ed7d6ae JQ |
1532 | } |
1533 | } | |
1534 | field++; | |
1535 | } | |
811814bd JQ |
1536 | ret = vmstate_subsection_load(f, vmsd, opaque); |
1537 | if (ret != 0) { | |
1538 | return ret; | |
1539 | } | |
752ff2fa | 1540 | if (vmsd->post_load) { |
e59fb374 | 1541 | return vmsd->post_load(opaque, version_id); |
752ff2fa | 1542 | } |
9ed7d6ae JQ |
1543 | return 0; |
1544 | } | |
1545 | ||
1546 | void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, | |
84e2e3eb | 1547 | void *opaque) |
9ed7d6ae JQ |
1548 | { |
1549 | VMStateField *field = vmsd->fields; | |
1550 | ||
8fb0791d JQ |
1551 | if (vmsd->pre_save) { |
1552 | vmsd->pre_save(opaque); | |
1553 | } | |
9ed7d6ae | 1554 | while(field->name) { |
f11f6a5f JQ |
1555 | if (!field->field_exists || |
1556 | field->field_exists(opaque, vmsd->version_id)) { | |
1557 | void *base_addr = opaque + field->offset; | |
1558 | int i, n_elems = 1; | |
e61a1e0a | 1559 | int size = field->size; |
dde0463b | 1560 | |
e61a1e0a JQ |
1561 | if (field->flags & VMS_VBUFFER) { |
1562 | size = *(int32_t *)(opaque+field->size_offset); | |
33599e2a JQ |
1563 | if (field->flags & VMS_MULTIPLY) { |
1564 | size *= field->size; | |
1565 | } | |
e61a1e0a | 1566 | } |
f11f6a5f JQ |
1567 | if (field->flags & VMS_ARRAY) { |
1568 | n_elems = field->num; | |
d6698281 JQ |
1569 | } else if (field->flags & VMS_VARRAY_INT32) { |
1570 | n_elems = *(int32_t *)(opaque+field->num_offset); | |
1329d189 AK |
1571 | } else if (field->flags & VMS_VARRAY_UINT32) { |
1572 | n_elems = *(uint32_t *)(opaque+field->num_offset); | |
bdb4941d JQ |
1573 | } else if (field->flags & VMS_VARRAY_UINT16) { |
1574 | n_elems = *(uint16_t *)(opaque+field->num_offset); | |
b784421c JQ |
1575 | } else if (field->flags & VMS_VARRAY_UINT8) { |
1576 | n_elems = *(uint8_t *)(opaque+field->num_offset); | |
f11f6a5f JQ |
1577 | } |
1578 | if (field->flags & VMS_POINTER) { | |
e61a1e0a | 1579 | base_addr = *(void **)base_addr + field->start; |
f11f6a5f JQ |
1580 | } |
1581 | for (i = 0; i < n_elems; i++) { | |
e61a1e0a | 1582 | void *addr = base_addr + size * i; |
ec245e21 | 1583 | |
8595387e JQ |
1584 | if (field->flags & VMS_ARRAY_OF_POINTER) { |
1585 | addr = *(void **)addr; | |
1586 | } | |
f11f6a5f JQ |
1587 | if (field->flags & VMS_STRUCT) { |
1588 | vmstate_save_state(f, field->vmsd, addr); | |
1589 | } else { | |
e61a1e0a | 1590 | field->info->put(f, addr, size); |
f11f6a5f | 1591 | } |
ec245e21 | 1592 | } |
dde0463b | 1593 | } |
9ed7d6ae JQ |
1594 | field++; |
1595 | } | |
811814bd | 1596 | vmstate_subsection_save(f, vmsd, opaque); |
9ed7d6ae JQ |
1597 | } |
1598 | ||
4082be4d JQ |
1599 | static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id) |
1600 | { | |
9ed7d6ae | 1601 | if (!se->vmsd) { /* Old style */ |
22ea40f4 | 1602 | return se->ops->load_state(f, se->opaque, version_id); |
9ed7d6ae JQ |
1603 | } |
1604 | return vmstate_load_state(f, se->vmsd, se->opaque, version_id); | |
4082be4d JQ |
1605 | } |
1606 | ||
dc912121 | 1607 | static void vmstate_save(QEMUFile *f, SaveStateEntry *se) |
4082be4d | 1608 | { |
9ed7d6ae | 1609 | if (!se->vmsd) { /* Old style */ |
22ea40f4 | 1610 | se->ops->save_state(f, se->opaque); |
dc912121 | 1611 | return; |
9ed7d6ae JQ |
1612 | } |
1613 | vmstate_save_state(f,se->vmsd, se->opaque); | |
4082be4d JQ |
1614 | } |
1615 | ||
a672b469 AL |
1616 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
1617 | #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002 | |
1618 | #define QEMU_VM_FILE_VERSION 0x00000003 | |
1619 | ||
1620 | #define QEMU_VM_EOF 0x00 | |
1621 | #define QEMU_VM_SECTION_START 0x01 | |
1622 | #define QEMU_VM_SECTION_PART 0x02 | |
1623 | #define QEMU_VM_SECTION_END 0x03 | |
1624 | #define QEMU_VM_SECTION_FULL 0x04 | |
811814bd | 1625 | #define QEMU_VM_SUBSECTION 0x05 |
a672b469 | 1626 | |
e1c37d0e | 1627 | bool qemu_savevm_state_blocked(Error **errp) |
dc912121 AW |
1628 | { |
1629 | SaveStateEntry *se; | |
1630 | ||
1631 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { | |
1632 | if (se->no_migrate) { | |
e1c37d0e | 1633 | error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr); |
dc912121 AW |
1634 | return true; |
1635 | } | |
1636 | } | |
1637 | return false; | |
1638 | } | |
1639 | ||
47c8c17a PB |
1640 | void qemu_savevm_state_begin(QEMUFile *f, |
1641 | const MigrationParams *params) | |
a672b469 AL |
1642 | { |
1643 | SaveStateEntry *se; | |
39346385 | 1644 | int ret; |
a672b469 | 1645 | |
c163b5ca | 1646 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
22ea40f4 | 1647 | if (!se->ops || !se->ops->set_params) { |
c163b5ca | 1648 | continue; |
6607ae23 | 1649 | } |
22ea40f4 | 1650 | se->ops->set_params(params, se->opaque); |
c163b5ca | 1651 | } |
1652 | ||
a672b469 AL |
1653 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
1654 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
1655 | ||
72cf2d4f | 1656 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
a672b469 AL |
1657 | int len; |
1658 | ||
d1315aac | 1659 | if (!se->ops || !se->ops->save_live_setup) { |
a672b469 | 1660 | continue; |
22ea40f4 | 1661 | } |
6bd68781 JQ |
1662 | if (se->ops && se->ops->is_active) { |
1663 | if (!se->ops->is_active(se->opaque)) { | |
1664 | continue; | |
1665 | } | |
1666 | } | |
a672b469 AL |
1667 | /* Section type */ |
1668 | qemu_put_byte(f, QEMU_VM_SECTION_START); | |
1669 | qemu_put_be32(f, se->section_id); | |
1670 | ||
1671 | /* ID string */ | |
1672 | len = strlen(se->idstr); | |
1673 | qemu_put_byte(f, len); | |
1674 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); | |
1675 | ||
1676 | qemu_put_be32(f, se->instance_id); | |
1677 | qemu_put_be32(f, se->version_id); | |
1678 | ||
d1315aac | 1679 | ret = se->ops->save_live_setup(f, se->opaque); |
2975725f | 1680 | if (ret < 0) { |
47c8c17a PB |
1681 | qemu_file_set_error(f, ret); |
1682 | break; | |
2975725f | 1683 | } |
a672b469 | 1684 | } |
a672b469 AL |
1685 | } |
1686 | ||
39346385 | 1687 | /* |
07f35073 | 1688 | * this function has three return values: |
39346385 JQ |
1689 | * negative: there was one error, and we have -errno. |
1690 | * 0 : We haven't finished, caller have to go again | |
1691 | * 1 : We have finished, we can go to complete phase | |
1692 | */ | |
539de124 | 1693 | int qemu_savevm_state_iterate(QEMUFile *f) |
a672b469 AL |
1694 | { |
1695 | SaveStateEntry *se; | |
1696 | int ret = 1; | |
1697 | ||
72cf2d4f | 1698 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
16310a3c | 1699 | if (!se->ops || !se->ops->save_live_iterate) { |
a672b469 | 1700 | continue; |
22ea40f4 | 1701 | } |
6bd68781 JQ |
1702 | if (se->ops && se->ops->is_active) { |
1703 | if (!se->ops->is_active(se->opaque)) { | |
1704 | continue; | |
1705 | } | |
1706 | } | |
aac844ed JQ |
1707 | if (qemu_file_rate_limit(f)) { |
1708 | return 0; | |
1709 | } | |
517a13c9 | 1710 | trace_savevm_section_start(); |
a672b469 AL |
1711 | /* Section type */ |
1712 | qemu_put_byte(f, QEMU_VM_SECTION_PART); | |
1713 | qemu_put_be32(f, se->section_id); | |
1714 | ||
16310a3c | 1715 | ret = se->ops->save_live_iterate(f, se->opaque); |
517a13c9 JQ |
1716 | trace_savevm_section_end(se->section_id); |
1717 | ||
47c8c17a PB |
1718 | if (ret < 0) { |
1719 | qemu_file_set_error(f, ret); | |
1720 | } | |
2975725f | 1721 | if (ret <= 0) { |
90697be8 JK |
1722 | /* Do not proceed to the next vmstate before this one reported |
1723 | completion of the current stage. This serializes the migration | |
1724 | and reduces the probability that a faster changing state is | |
1725 | synchronized over and over again. */ | |
1726 | break; | |
1727 | } | |
a672b469 | 1728 | } |
39346385 | 1729 | return ret; |
a672b469 AL |
1730 | } |
1731 | ||
47c8c17a | 1732 | void qemu_savevm_state_complete(QEMUFile *f) |
a672b469 AL |
1733 | { |
1734 | SaveStateEntry *se; | |
2975725f | 1735 | int ret; |
a672b469 | 1736 | |
ea375f9a JK |
1737 | cpu_synchronize_all_states(); |
1738 | ||
72cf2d4f | 1739 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
16310a3c | 1740 | if (!se->ops || !se->ops->save_live_complete) { |
a672b469 | 1741 | continue; |
22ea40f4 | 1742 | } |
6bd68781 JQ |
1743 | if (se->ops && se->ops->is_active) { |
1744 | if (!se->ops->is_active(se->opaque)) { | |
1745 | continue; | |
1746 | } | |
1747 | } | |
517a13c9 | 1748 | trace_savevm_section_start(); |
a672b469 AL |
1749 | /* Section type */ |
1750 | qemu_put_byte(f, QEMU_VM_SECTION_END); | |
1751 | qemu_put_be32(f, se->section_id); | |
1752 | ||
16310a3c | 1753 | ret = se->ops->save_live_complete(f, se->opaque); |
517a13c9 | 1754 | trace_savevm_section_end(se->section_id); |
2975725f | 1755 | if (ret < 0) { |
47c8c17a PB |
1756 | qemu_file_set_error(f, ret); |
1757 | return; | |
2975725f | 1758 | } |
a672b469 AL |
1759 | } |
1760 | ||
72cf2d4f | 1761 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
a672b469 AL |
1762 | int len; |
1763 | ||
22ea40f4 | 1764 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
a672b469 | 1765 | continue; |
22ea40f4 | 1766 | } |
517a13c9 | 1767 | trace_savevm_section_start(); |
a672b469 AL |
1768 | /* Section type */ |
1769 | qemu_put_byte(f, QEMU_VM_SECTION_FULL); | |
1770 | qemu_put_be32(f, se->section_id); | |
1771 | ||
1772 | /* ID string */ | |
1773 | len = strlen(se->idstr); | |
1774 | qemu_put_byte(f, len); | |
1775 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); | |
1776 | ||
1777 | qemu_put_be32(f, se->instance_id); | |
1778 | qemu_put_be32(f, se->version_id); | |
1779 | ||
dc912121 | 1780 | vmstate_save(f, se); |
517a13c9 | 1781 | trace_savevm_section_end(se->section_id); |
a672b469 AL |
1782 | } |
1783 | ||
1784 | qemu_put_byte(f, QEMU_VM_EOF); | |
edaae611 | 1785 | qemu_fflush(f); |
a672b469 AL |
1786 | } |
1787 | ||
e4ed1541 JQ |
1788 | uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size) |
1789 | { | |
1790 | SaveStateEntry *se; | |
1791 | uint64_t ret = 0; | |
1792 | ||
1793 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { | |
1794 | if (!se->ops || !se->ops->save_live_pending) { | |
1795 | continue; | |
1796 | } | |
1797 | if (se->ops && se->ops->is_active) { | |
1798 | if (!se->ops->is_active(se->opaque)) { | |
1799 | continue; | |
1800 | } | |
1801 | } | |
1802 | ret += se->ops->save_live_pending(f, se->opaque, max_size); | |
1803 | } | |
1804 | return ret; | |
1805 | } | |
1806 | ||
6522773f | 1807 | void qemu_savevm_state_cancel(void) |
4ec7fcc7 JK |
1808 | { |
1809 | SaveStateEntry *se; | |
1810 | ||
1811 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { | |
9b5bfab0 JQ |
1812 | if (se->ops && se->ops->cancel) { |
1813 | se->ops->cancel(se->opaque); | |
4ec7fcc7 JK |
1814 | } |
1815 | } | |
1816 | } | |
1817 | ||
e1c37d0e | 1818 | static int qemu_savevm_state(QEMUFile *f) |
a672b469 | 1819 | { |
a672b469 | 1820 | int ret; |
6607ae23 IY |
1821 | MigrationParams params = { |
1822 | .blk = 0, | |
1823 | .shared = 0 | |
1824 | }; | |
a672b469 | 1825 | |
e1c37d0e | 1826 | if (qemu_savevm_state_blocked(NULL)) { |
04943eba | 1827 | return -EINVAL; |
dc912121 AW |
1828 | } |
1829 | ||
9b095037 | 1830 | qemu_mutex_unlock_iothread(); |
47c8c17a | 1831 | qemu_savevm_state_begin(f, ¶ms); |
9b095037 PB |
1832 | qemu_mutex_lock_iothread(); |
1833 | ||
47c8c17a PB |
1834 | while (qemu_file_get_error(f) == 0) { |
1835 | if (qemu_savevm_state_iterate(f) > 0) { | |
1836 | break; | |
1837 | } | |
1838 | } | |
a672b469 | 1839 | |
47c8c17a | 1840 | ret = qemu_file_get_error(f); |
39346385 | 1841 | if (ret == 0) { |
47c8c17a | 1842 | qemu_savevm_state_complete(f); |
624b9cc2 | 1843 | ret = qemu_file_get_error(f); |
39346385 | 1844 | } |
04943eba PB |
1845 | if (ret != 0) { |
1846 | qemu_savevm_state_cancel(); | |
1847 | } | |
a672b469 AL |
1848 | return ret; |
1849 | } | |
1850 | ||
a7ae8355 SS |
1851 | static int qemu_save_device_state(QEMUFile *f) |
1852 | { | |
1853 | SaveStateEntry *se; | |
1854 | ||
1855 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
1856 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
1857 | ||
1858 | cpu_synchronize_all_states(); | |
1859 | ||
1860 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { | |
1861 | int len; | |
1862 | ||
1863 | if (se->is_ram) { | |
1864 | continue; | |
1865 | } | |
22ea40f4 | 1866 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
a7ae8355 SS |
1867 | continue; |
1868 | } | |
1869 | ||
1870 | /* Section type */ | |
1871 | qemu_put_byte(f, QEMU_VM_SECTION_FULL); | |
1872 | qemu_put_be32(f, se->section_id); | |
1873 | ||
1874 | /* ID string */ | |
1875 | len = strlen(se->idstr); | |
1876 | qemu_put_byte(f, len); | |
1877 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); | |
1878 | ||
1879 | qemu_put_be32(f, se->instance_id); | |
1880 | qemu_put_be32(f, se->version_id); | |
1881 | ||
1882 | vmstate_save(f, se); | |
1883 | } | |
1884 | ||
1885 | qemu_put_byte(f, QEMU_VM_EOF); | |
1886 | ||
1887 | return qemu_file_get_error(f); | |
1888 | } | |
1889 | ||
a672b469 AL |
1890 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
1891 | { | |
1892 | SaveStateEntry *se; | |
1893 | ||
72cf2d4f | 1894 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
a672b469 | 1895 | if (!strcmp(se->idstr, idstr) && |
4d2ffa08 JK |
1896 | (instance_id == se->instance_id || |
1897 | instance_id == se->alias_id)) | |
a672b469 | 1898 | return se; |
7685ee6a AW |
1899 | /* Migrating from an older version? */ |
1900 | if (strstr(se->idstr, idstr) && se->compat) { | |
1901 | if (!strcmp(se->compat->idstr, idstr) && | |
1902 | (instance_id == se->compat->instance_id || | |
1903 | instance_id == se->alias_id)) | |
1904 | return se; | |
1905 | } | |
a672b469 AL |
1906 | } |
1907 | return NULL; | |
1908 | } | |
1909 | ||
811814bd JQ |
1910 | static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr) |
1911 | { | |
1912 | while(sub && sub->needed) { | |
1913 | if (strcmp(idstr, sub->vmsd->name) == 0) { | |
1914 | return sub->vmsd; | |
1915 | } | |
1916 | sub++; | |
1917 | } | |
1918 | return NULL; | |
1919 | } | |
1920 | ||
1921 | static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd, | |
1922 | void *opaque) | |
1923 | { | |
c6380724 | 1924 | while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) { |
811814bd JQ |
1925 | char idstr[256]; |
1926 | int ret; | |
c6380724 | 1927 | uint8_t version_id, len, size; |
811814bd JQ |
1928 | const VMStateDescription *sub_vmsd; |
1929 | ||
c6380724 JQ |
1930 | len = qemu_peek_byte(f, 1); |
1931 | if (len < strlen(vmsd->name) + 1) { | |
1932 | /* subsection name has be be "section_name/a" */ | |
1933 | return 0; | |
1934 | } | |
1935 | size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2); | |
1936 | if (size != len) { | |
1937 | return 0; | |
1938 | } | |
1939 | idstr[size] = 0; | |
811814bd | 1940 | |
c6380724 JQ |
1941 | if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) { |
1942 | /* it don't have a valid subsection name */ | |
1943 | return 0; | |
1944 | } | |
3da9eebd | 1945 | sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr); |
811814bd JQ |
1946 | if (sub_vmsd == NULL) { |
1947 | return -ENOENT; | |
1948 | } | |
c6380724 JQ |
1949 | qemu_file_skip(f, 1); /* subsection */ |
1950 | qemu_file_skip(f, 1); /* len */ | |
1951 | qemu_file_skip(f, len); /* idstr */ | |
1952 | version_id = qemu_get_be32(f); | |
1953 | ||
811814bd JQ |
1954 | ret = vmstate_load_state(f, sub_vmsd, opaque, version_id); |
1955 | if (ret) { | |
1956 | return ret; | |
1957 | } | |
1958 | } | |
1959 | return 0; | |
1960 | } | |
1961 | ||
1962 | static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, | |
1963 | void *opaque) | |
1964 | { | |
1965 | const VMStateSubsection *sub = vmsd->subsections; | |
1966 | ||
1967 | while (sub && sub->needed) { | |
1968 | if (sub->needed(opaque)) { | |
1969 | const VMStateDescription *vmsd = sub->vmsd; | |
1970 | uint8_t len; | |
1971 | ||
1972 | qemu_put_byte(f, QEMU_VM_SUBSECTION); | |
1973 | len = strlen(vmsd->name); | |
1974 | qemu_put_byte(f, len); | |
1975 | qemu_put_buffer(f, (uint8_t *)vmsd->name, len); | |
1976 | qemu_put_be32(f, vmsd->version_id); | |
1977 | vmstate_save_state(f, vmsd, opaque); | |
1978 | } | |
1979 | sub++; | |
1980 | } | |
1981 | } | |
1982 | ||
a672b469 | 1983 | typedef struct LoadStateEntry { |
72cf2d4f | 1984 | QLIST_ENTRY(LoadStateEntry) entry; |
a672b469 AL |
1985 | SaveStateEntry *se; |
1986 | int section_id; | |
1987 | int version_id; | |
a672b469 AL |
1988 | } LoadStateEntry; |
1989 | ||
a672b469 AL |
1990 | int qemu_loadvm_state(QEMUFile *f) |
1991 | { | |
72cf2d4f BS |
1992 | QLIST_HEAD(, LoadStateEntry) loadvm_handlers = |
1993 | QLIST_HEAD_INITIALIZER(loadvm_handlers); | |
f4dbb8dd | 1994 | LoadStateEntry *le, *new_le; |
a672b469 AL |
1995 | uint8_t section_type; |
1996 | unsigned int v; | |
1997 | int ret; | |
1998 | ||
e1c37d0e | 1999 | if (qemu_savevm_state_blocked(NULL)) { |
dc912121 AW |
2000 | return -EINVAL; |
2001 | } | |
2002 | ||
a672b469 AL |
2003 | v = qemu_get_be32(f); |
2004 | if (v != QEMU_VM_FILE_MAGIC) | |
2005 | return -EINVAL; | |
2006 | ||
2007 | v = qemu_get_be32(f); | |
bbfe1408 JQ |
2008 | if (v == QEMU_VM_FILE_VERSION_COMPAT) { |
2009 | fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n"); | |
2010 | return -ENOTSUP; | |
2011 | } | |
a672b469 AL |
2012 | if (v != QEMU_VM_FILE_VERSION) |
2013 | return -ENOTSUP; | |
2014 | ||
2015 | while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) { | |
2016 | uint32_t instance_id, version_id, section_id; | |
a672b469 AL |
2017 | SaveStateEntry *se; |
2018 | char idstr[257]; | |
2019 | int len; | |
2020 | ||
2021 | switch (section_type) { | |
2022 | case QEMU_VM_SECTION_START: | |
2023 | case QEMU_VM_SECTION_FULL: | |
2024 | /* Read section start */ | |
2025 | section_id = qemu_get_be32(f); | |
2026 | len = qemu_get_byte(f); | |
2027 | qemu_get_buffer(f, (uint8_t *)idstr, len); | |
2028 | idstr[len] = 0; | |
2029 | instance_id = qemu_get_be32(f); | |
2030 | version_id = qemu_get_be32(f); | |
2031 | ||
2032 | /* Find savevm section */ | |
2033 | se = find_se(idstr, instance_id); | |
2034 | if (se == NULL) { | |
2035 | fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id); | |
2036 | ret = -EINVAL; | |
2037 | goto out; | |
2038 | } | |
2039 | ||
2040 | /* Validate version */ | |
2041 | if (version_id > se->version_id) { | |
2042 | fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n", | |
2043 | version_id, idstr, se->version_id); | |
2044 | ret = -EINVAL; | |
2045 | goto out; | |
2046 | } | |
2047 | ||
2048 | /* Add entry */ | |
7267c094 | 2049 | le = g_malloc0(sizeof(*le)); |
a672b469 AL |
2050 | |
2051 | le->se = se; | |
2052 | le->section_id = section_id; | |
2053 | le->version_id = version_id; | |
72cf2d4f | 2054 | QLIST_INSERT_HEAD(&loadvm_handlers, le, entry); |
a672b469 | 2055 | |
4082be4d | 2056 | ret = vmstate_load(f, le->se, le->version_id); |
b5a22e4a JQ |
2057 | if (ret < 0) { |
2058 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", | |
2059 | instance_id, idstr); | |
2060 | goto out; | |
2061 | } | |
a672b469 AL |
2062 | break; |
2063 | case QEMU_VM_SECTION_PART: | |
2064 | case QEMU_VM_SECTION_END: | |
2065 | section_id = qemu_get_be32(f); | |
2066 | ||
72cf2d4f | 2067 | QLIST_FOREACH(le, &loadvm_handlers, entry) { |
f4dbb8dd JQ |
2068 | if (le->section_id == section_id) { |
2069 | break; | |
2070 | } | |
2071 | } | |
a672b469 AL |
2072 | if (le == NULL) { |
2073 | fprintf(stderr, "Unknown savevm section %d\n", section_id); | |
2074 | ret = -EINVAL; | |
2075 | goto out; | |
2076 | } | |
2077 | ||
4082be4d | 2078 | ret = vmstate_load(f, le->se, le->version_id); |
b5a22e4a JQ |
2079 | if (ret < 0) { |
2080 | fprintf(stderr, "qemu: warning: error while loading state section id %d\n", | |
2081 | section_id); | |
2082 | goto out; | |
2083 | } | |
a672b469 AL |
2084 | break; |
2085 | default: | |
2086 | fprintf(stderr, "Unknown savevm section type %d\n", section_type); | |
2087 | ret = -EINVAL; | |
2088 | goto out; | |
2089 | } | |
2090 | } | |
2091 | ||
ea375f9a JK |
2092 | cpu_synchronize_all_post_init(); |
2093 | ||
a672b469 AL |
2094 | ret = 0; |
2095 | ||
2096 | out: | |
72cf2d4f BS |
2097 | QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) { |
2098 | QLIST_REMOVE(le, entry); | |
7267c094 | 2099 | g_free(le); |
a672b469 AL |
2100 | } |
2101 | ||
42802d47 JQ |
2102 | if (ret == 0) { |
2103 | ret = qemu_file_get_error(f); | |
624b9cc2 | 2104 | } |
a672b469 AL |
2105 | |
2106 | return ret; | |
2107 | } | |
2108 | ||
a672b469 AL |
2109 | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
2110 | const char *name) | |
2111 | { | |
2112 | QEMUSnapshotInfo *sn_tab, *sn; | |
2113 | int nb_sns, i, ret; | |
2114 | ||
2115 | ret = -ENOENT; | |
2116 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
2117 | if (nb_sns < 0) | |
2118 | return ret; | |
2119 | for(i = 0; i < nb_sns; i++) { | |
2120 | sn = &sn_tab[i]; | |
2121 | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { | |
2122 | *sn_info = *sn; | |
2123 | ret = 0; | |
2124 | break; | |
2125 | } | |
2126 | } | |
7267c094 | 2127 | g_free(sn_tab); |
a672b469 AL |
2128 | return ret; |
2129 | } | |
2130 | ||
cb499fb2 KW |
2131 | /* |
2132 | * Deletes snapshots of a given name in all opened images. | |
2133 | */ | |
2134 | static int del_existing_snapshots(Monitor *mon, const char *name) | |
2135 | { | |
2136 | BlockDriverState *bs; | |
cb499fb2 KW |
2137 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
2138 | int ret; | |
2139 | ||
dbc13590 MA |
2140 | bs = NULL; |
2141 | while ((bs = bdrv_next(bs))) { | |
cb499fb2 KW |
2142 | if (bdrv_can_snapshot(bs) && |
2143 | bdrv_snapshot_find(bs, snapshot, name) >= 0) | |
2144 | { | |
2145 | ret = bdrv_snapshot_delete(bs, name); | |
2146 | if (ret < 0) { | |
2147 | monitor_printf(mon, | |
2148 | "Error while deleting snapshot on '%s'\n", | |
2149 | bdrv_get_device_name(bs)); | |
2150 | return -1; | |
2151 | } | |
2152 | } | |
2153 | } | |
2154 | ||
2155 | return 0; | |
2156 | } | |
2157 | ||
d54908a5 | 2158 | void do_savevm(Monitor *mon, const QDict *qdict) |
a672b469 AL |
2159 | { |
2160 | BlockDriverState *bs, *bs1; | |
2161 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; | |
cb499fb2 | 2162 | int ret; |
a672b469 AL |
2163 | QEMUFile *f; |
2164 | int saved_vm_running; | |
c2c9a466 | 2165 | uint64_t vm_state_size; |
68b891ec | 2166 | qemu_timeval tv; |
7d631a11 | 2167 | struct tm tm; |
d54908a5 | 2168 | const char *name = qdict_get_try_str(qdict, "name"); |
a672b469 | 2169 | |
feeee5ac | 2170 | /* Verify if there is a device that doesn't support snapshots and is writable */ |
dbc13590 MA |
2171 | bs = NULL; |
2172 | while ((bs = bdrv_next(bs))) { | |
feeee5ac | 2173 | |
07b70bfb | 2174 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
feeee5ac MDCF |
2175 | continue; |
2176 | } | |
2177 | ||
2178 | if (!bdrv_can_snapshot(bs)) { | |
2179 | monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n", | |
2180 | bdrv_get_device_name(bs)); | |
2181 | return; | |
2182 | } | |
2183 | } | |
2184 | ||
f9092b10 | 2185 | bs = bdrv_snapshots(); |
a672b469 | 2186 | if (!bs) { |
376253ec | 2187 | monitor_printf(mon, "No block device can accept snapshots\n"); |
a672b469 AL |
2188 | return; |
2189 | } | |
a672b469 | 2190 | |
1354869c | 2191 | saved_vm_running = runstate_is_running(); |
0461d5a6 | 2192 | vm_stop(RUN_STATE_SAVE_VM); |
a672b469 | 2193 | |
cb499fb2 | 2194 | memset(sn, 0, sizeof(*sn)); |
a672b469 AL |
2195 | |
2196 | /* fill auxiliary fields */ | |
68b891ec | 2197 | qemu_gettimeofday(&tv); |
a672b469 AL |
2198 | sn->date_sec = tv.tv_sec; |
2199 | sn->date_nsec = tv.tv_usec * 1000; | |
74475455 | 2200 | sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock); |
a672b469 | 2201 | |
7d631a11 MDCF |
2202 | if (name) { |
2203 | ret = bdrv_snapshot_find(bs, old_sn, name); | |
2204 | if (ret >= 0) { | |
2205 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); | |
2206 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); | |
2207 | } else { | |
2208 | pstrcpy(sn->name, sizeof(sn->name), name); | |
2209 | } | |
2210 | } else { | |
d7d9b528 BS |
2211 | /* cast below needed for OpenBSD where tv_sec is still 'long' */ |
2212 | localtime_r((const time_t *)&tv.tv_sec, &tm); | |
7d631a11 | 2213 | strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm); |
7d631a11 MDCF |
2214 | } |
2215 | ||
cb499fb2 | 2216 | /* Delete old snapshots of the same name */ |
f139a412 | 2217 | if (name && del_existing_snapshots(mon, name) < 0) { |
cb499fb2 KW |
2218 | goto the_end; |
2219 | } | |
2220 | ||
a672b469 | 2221 | /* save the VM state */ |
45566e9c | 2222 | f = qemu_fopen_bdrv(bs, 1); |
a672b469 | 2223 | if (!f) { |
376253ec | 2224 | monitor_printf(mon, "Could not open VM state file\n"); |
a672b469 AL |
2225 | goto the_end; |
2226 | } | |
e1c37d0e | 2227 | ret = qemu_savevm_state(f); |
2d22b18f | 2228 | vm_state_size = qemu_ftell(f); |
a672b469 AL |
2229 | qemu_fclose(f); |
2230 | if (ret < 0) { | |
376253ec | 2231 | monitor_printf(mon, "Error %d while writing VM\n", ret); |
a672b469 AL |
2232 | goto the_end; |
2233 | } | |
2234 | ||
2235 | /* create the snapshots */ | |
2236 | ||
dbc13590 MA |
2237 | bs1 = NULL; |
2238 | while ((bs1 = bdrv_next(bs1))) { | |
feeee5ac | 2239 | if (bdrv_can_snapshot(bs1)) { |
2d22b18f AL |
2240 | /* Write VM state size only to the image that contains the state */ |
2241 | sn->vm_state_size = (bs == bs1 ? vm_state_size : 0); | |
a672b469 AL |
2242 | ret = bdrv_snapshot_create(bs1, sn); |
2243 | if (ret < 0) { | |
376253ec AL |
2244 | monitor_printf(mon, "Error while creating snapshot on '%s'\n", |
2245 | bdrv_get_device_name(bs1)); | |
a672b469 AL |
2246 | } |
2247 | } | |
2248 | } | |
2249 | ||
2250 | the_end: | |
2251 | if (saved_vm_running) | |
2252 | vm_start(); | |
2253 | } | |
2254 | ||
a7ae8355 SS |
2255 | void qmp_xen_save_devices_state(const char *filename, Error **errp) |
2256 | { | |
2257 | QEMUFile *f; | |
2258 | int saved_vm_running; | |
2259 | int ret; | |
2260 | ||
2261 | saved_vm_running = runstate_is_running(); | |
2262 | vm_stop(RUN_STATE_SAVE_VM); | |
2263 | ||
2264 | f = qemu_fopen(filename, "wb"); | |
2265 | if (!f) { | |
2266 | error_set(errp, QERR_OPEN_FILE_FAILED, filename); | |
2267 | goto the_end; | |
2268 | } | |
2269 | ret = qemu_save_device_state(f); | |
2270 | qemu_fclose(f); | |
2271 | if (ret < 0) { | |
2272 | error_set(errp, QERR_IO_ERROR); | |
2273 | } | |
2274 | ||
2275 | the_end: | |
2276 | if (saved_vm_running) | |
2277 | vm_start(); | |
a7ae8355 SS |
2278 | } |
2279 | ||
03cd4655 | 2280 | int load_vmstate(const char *name) |
a672b469 | 2281 | { |
f0aa7a8b | 2282 | BlockDriverState *bs, *bs_vm_state; |
2d22b18f | 2283 | QEMUSnapshotInfo sn; |
a672b469 | 2284 | QEMUFile *f; |
751c6a17 | 2285 | int ret; |
a672b469 | 2286 | |
f0aa7a8b MDCF |
2287 | bs_vm_state = bdrv_snapshots(); |
2288 | if (!bs_vm_state) { | |
2289 | error_report("No block device supports snapshots"); | |
2290 | return -ENOTSUP; | |
2291 | } | |
2292 | ||
2293 | /* Don't even try to load empty VM states */ | |
2294 | ret = bdrv_snapshot_find(bs_vm_state, &sn, name); | |
2295 | if (ret < 0) { | |
2296 | return ret; | |
2297 | } else if (sn.vm_state_size == 0) { | |
e11480db KW |
2298 | error_report("This is a disk-only snapshot. Revert to it offline " |
2299 | "using qemu-img."); | |
f0aa7a8b MDCF |
2300 | return -EINVAL; |
2301 | } | |
2302 | ||
2303 | /* Verify if there is any device that doesn't support snapshots and is | |
2304 | writable and check if the requested snapshot is available too. */ | |
dbc13590 MA |
2305 | bs = NULL; |
2306 | while ((bs = bdrv_next(bs))) { | |
feeee5ac | 2307 | |
07b70bfb | 2308 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
feeee5ac MDCF |
2309 | continue; |
2310 | } | |
2311 | ||
2312 | if (!bdrv_can_snapshot(bs)) { | |
2313 | error_report("Device '%s' is writable but does not support snapshots.", | |
2314 | bdrv_get_device_name(bs)); | |
2315 | return -ENOTSUP; | |
2316 | } | |
feeee5ac | 2317 | |
f0aa7a8b MDCF |
2318 | ret = bdrv_snapshot_find(bs, &sn, name); |
2319 | if (ret < 0) { | |
2320 | error_report("Device '%s' does not have the requested snapshot '%s'", | |
2321 | bdrv_get_device_name(bs), name); | |
2322 | return ret; | |
2323 | } | |
a672b469 AL |
2324 | } |
2325 | ||
2326 | /* Flush all IO requests so they don't interfere with the new state. */ | |
922453bc | 2327 | bdrv_drain_all(); |
a672b469 | 2328 | |
f0aa7a8b MDCF |
2329 | bs = NULL; |
2330 | while ((bs = bdrv_next(bs))) { | |
2331 | if (bdrv_can_snapshot(bs)) { | |
2332 | ret = bdrv_snapshot_goto(bs, name); | |
a672b469 | 2333 | if (ret < 0) { |
f0aa7a8b MDCF |
2334 | error_report("Error %d while activating snapshot '%s' on '%s'", |
2335 | ret, name, bdrv_get_device_name(bs)); | |
2336 | return ret; | |
a672b469 AL |
2337 | } |
2338 | } | |
2339 | } | |
2340 | ||
a672b469 | 2341 | /* restore the VM state */ |
f0aa7a8b | 2342 | f = qemu_fopen_bdrv(bs_vm_state, 0); |
a672b469 | 2343 | if (!f) { |
1ecda02b | 2344 | error_report("Could not open VM state file"); |
05f2401e | 2345 | return -EINVAL; |
a672b469 | 2346 | } |
f0aa7a8b | 2347 | |
5a8a49d7 | 2348 | qemu_system_reset(VMRESET_SILENT); |
a672b469 | 2349 | ret = qemu_loadvm_state(f); |
f0aa7a8b | 2350 | |
a672b469 AL |
2351 | qemu_fclose(f); |
2352 | if (ret < 0) { | |
1ecda02b | 2353 | error_report("Error %d while loading VM state", ret); |
05f2401e | 2354 | return ret; |
a672b469 | 2355 | } |
f0aa7a8b | 2356 | |
05f2401e | 2357 | return 0; |
7b630349 JQ |
2358 | } |
2359 | ||
d54908a5 | 2360 | void do_delvm(Monitor *mon, const QDict *qdict) |
a672b469 AL |
2361 | { |
2362 | BlockDriverState *bs, *bs1; | |
751c6a17 | 2363 | int ret; |
d54908a5 | 2364 | const char *name = qdict_get_str(qdict, "name"); |
a672b469 | 2365 | |
f9092b10 | 2366 | bs = bdrv_snapshots(); |
a672b469 | 2367 | if (!bs) { |
376253ec | 2368 | monitor_printf(mon, "No block device supports snapshots\n"); |
a672b469 AL |
2369 | return; |
2370 | } | |
2371 | ||
dbc13590 MA |
2372 | bs1 = NULL; |
2373 | while ((bs1 = bdrv_next(bs1))) { | |
feeee5ac | 2374 | if (bdrv_can_snapshot(bs1)) { |
a672b469 AL |
2375 | ret = bdrv_snapshot_delete(bs1, name); |
2376 | if (ret < 0) { | |
2377 | if (ret == -ENOTSUP) | |
376253ec AL |
2378 | monitor_printf(mon, |
2379 | "Snapshots not supported on device '%s'\n", | |
2380 | bdrv_get_device_name(bs1)); | |
a672b469 | 2381 | else |
376253ec AL |
2382 | monitor_printf(mon, "Error %d while deleting snapshot on " |
2383 | "'%s'\n", ret, bdrv_get_device_name(bs1)); | |
a672b469 AL |
2384 | } |
2385 | } | |
2386 | } | |
2387 | } | |
2388 | ||
84f2d0ea | 2389 | void do_info_snapshots(Monitor *mon, const QDict *qdict) |
a672b469 AL |
2390 | { |
2391 | BlockDriverState *bs, *bs1; | |
f9209915 MDCF |
2392 | QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s; |
2393 | int nb_sns, i, ret, available; | |
2394 | int total; | |
2395 | int *available_snapshots; | |
a672b469 AL |
2396 | char buf[256]; |
2397 | ||
f9092b10 | 2398 | bs = bdrv_snapshots(); |
a672b469 | 2399 | if (!bs) { |
376253ec | 2400 | monitor_printf(mon, "No available block device supports snapshots\n"); |
a672b469 AL |
2401 | return; |
2402 | } | |
a672b469 AL |
2403 | |
2404 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
2405 | if (nb_sns < 0) { | |
376253ec | 2406 | monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns); |
a672b469 AL |
2407 | return; |
2408 | } | |
f9209915 MDCF |
2409 | |
2410 | if (nb_sns == 0) { | |
2411 | monitor_printf(mon, "There is no snapshot available.\n"); | |
2412 | return; | |
2413 | } | |
2414 | ||
7267c094 | 2415 | available_snapshots = g_malloc0(sizeof(int) * nb_sns); |
f9209915 MDCF |
2416 | total = 0; |
2417 | for (i = 0; i < nb_sns; i++) { | |
a672b469 | 2418 | sn = &sn_tab[i]; |
f9209915 MDCF |
2419 | available = 1; |
2420 | bs1 = NULL; | |
2421 | ||
2422 | while ((bs1 = bdrv_next(bs1))) { | |
2423 | if (bdrv_can_snapshot(bs1) && bs1 != bs) { | |
2424 | ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str); | |
2425 | if (ret < 0) { | |
2426 | available = 0; | |
2427 | break; | |
2428 | } | |
2429 | } | |
2430 | } | |
2431 | ||
2432 | if (available) { | |
2433 | available_snapshots[total] = i; | |
2434 | total++; | |
2435 | } | |
a672b469 | 2436 | } |
f9209915 MDCF |
2437 | |
2438 | if (total > 0) { | |
2439 | monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); | |
2440 | for (i = 0; i < total; i++) { | |
2441 | sn = &sn_tab[available_snapshots[i]]; | |
2442 | monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); | |
2443 | } | |
2444 | } else { | |
2445 | monitor_printf(mon, "There is no suitable snapshot available\n"); | |
2446 | } | |
2447 | ||
7267c094 AL |
2448 | g_free(sn_tab); |
2449 | g_free(available_snapshots); | |
f9209915 | 2450 | |
a672b469 | 2451 | } |
c5705a77 AK |
2452 | |
2453 | void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev) | |
2454 | { | |
1ddde087 | 2455 | qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK, |
c5705a77 AK |
2456 | memory_region_name(mr), dev); |
2457 | } | |
2458 | ||
2459 | void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev) | |
2460 | { | |
2461 | /* Nothing do to while the implementation is in RAMBlock */ | |
2462 | } | |
2463 | ||
2464 | void vmstate_register_ram_global(MemoryRegion *mr) | |
2465 | { | |
2466 | vmstate_register_ram(mr, NULL); | |
2467 | } |