blob: ca9e233c9af972c73a1490e12b27e7520b6077ad [file] [log] [blame]
aliguoria672b462008-11-11 21:33:36 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
aliguoria672b462008-11-11 21:33:36 +000024#include <unistd.h>
25#include <fcntl.h>
aliguoria672b462008-11-11 21:33:36 +000026#include <time.h>
27#include <errno.h>
28#include <sys/time.h>
29#include <zlib.h>
30
Juan Quintela71e72a12009-07-27 16:12:56 +020031/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000032#include "config-host.h"
33
aliguoria672b462008-11-11 21:33:36 +000034#ifndef _WIN32
35#include <sys/times.h>
36#include <sys/wait.h>
37#include <termios.h>
38#include <sys/mman.h>
39#include <sys/ioctl.h>
40#include <sys/resource.h>
41#include <sys/socket.h>
42#include <netinet/in.h>
43#include <net/if.h>
aliguoria672b462008-11-11 21:33:36 +000044#include <arpa/inet.h>
45#include <dirent.h>
46#include <netdb.h>
47#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020048#ifdef CONFIG_BSD
aliguoria672b462008-11-11 21:33:36 +000049#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010050#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
aliguoria672b462008-11-11 21:33:36 +000051#include <libutil.h>
52#else
53#include <util.h>
54#endif
aliguoria672b462008-11-11 21:33:36 +000055#ifdef __linux__
56#include <pty.h>
57#include <malloc.h>
58#include <linux/rtc.h>
59#endif
60#endif
61#endif
62
63#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000064#include <windows.h>
aliguoria672b462008-11-11 21:33:36 +000065#include <malloc.h>
66#include <sys/timeb.h>
67#include <mmsystem.h>
68#define getopt_long_only getopt_long
69#define memalign(align, size) malloc(size)
70#endif
71
blueswir1511d2b12009-03-07 15:32:56 +000072#include "qemu-common.h"
73#include "hw/hw.h"
Alex Williamson7685ee62010-06-25 11:09:14 -060074#include "hw/qdev.h"
blueswir1511d2b12009-03-07 15:32:56 +000075#include "net.h"
76#include "monitor.h"
77#include "sysemu.h"
78#include "qemu-timer.h"
79#include "qemu-char.h"
blueswir1511d2b12009-03-07 15:32:56 +000080#include "audio/audio.h"
81#include "migration.h"
82#include "qemu_socket.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +000083#include "qemu-queue.h"
Blue Swirl17a46632011-03-27 16:05:08 +000084#include "cpus.h"
blueswir1511d2b12009-03-07 15:32:56 +000085
aliguoria672b462008-11-11 21:33:36 +000086#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000087
Nolan18995b92009-10-15 16:53:55 -070088#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040089#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070090#endif
91#define ARP_HTYPE_ETH 0x0001
92#define ARP_PTYPE_IP 0x0800
93#define ARP_OP_REQUEST_REV 0x3
94
95static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000096 uint8_t *mac_addr)
97{
Nolan18995b92009-10-15 16:53:55 -070098 /* Ethernet header. */
99 memset(buf, 0xff, 6); /* destination MAC addr */
100 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
101 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +0000102
Nolan18995b92009-10-15 16:53:55 -0700103 /* RARP header. */
104 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
105 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
106 *(buf + 18) = 6; /* hardware addr length (ethernet) */
107 *(buf + 19) = 4; /* protocol addr length (IPv4) */
108 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
109 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
110 memset(buf + 28, 0x00, 4); /* source protocol addr */
111 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
112 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +0000113
Nolan18995b92009-10-15 16:53:55 -0700114 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
115 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +0000116
Nolan18995b92009-10-15 16:53:55 -0700117 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +0000118}
119
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000120static void qemu_announce_self_iter(NICState *nic, void *opaque)
121{
122 uint8_t buf[60];
123 int len;
124
125 len = announce_self_create(buf, nic->conf->macaddr.a);
126
127 qemu_send_packet_raw(&nic->nc, buf, len);
128}
129
130
Gleb Natapoved8b3302009-05-21 17:17:44 +0300131static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000132{
Gleb Natapoved8b3302009-05-21 17:17:44 +0300133 static int count = SELF_ANNOUNCE_ROUNDS;
134 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +0000135
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000136 qemu_foreach_nic(qemu_announce_self_iter, NULL);
137
Nolan18995b92009-10-15 16:53:55 -0700138 if (--count) {
139 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100140 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -0700141 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300142 } else {
143 qemu_del_timer(timer);
144 qemu_free_timer(timer);
145 }
146}
147
148void qemu_announce_self(void)
149{
150 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100151 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300152 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000153}
154
155/***********************************************************/
156/* savevm/loadvm support */
157
158#define IO_BUF_SIZE 32768
159
160struct QEMUFile {
161 QEMUFilePutBufferFunc *put_buffer;
162 QEMUFileGetBufferFunc *get_buffer;
163 QEMUFileCloseFunc *close;
164 QEMUFileRateLimit *rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400165 QEMUFileSetRateLimit *set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200166 QEMUFileGetRateLimit *get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000167 void *opaque;
168 int is_write;
169
170 int64_t buf_offset; /* start of buffer when writing, end of buffer
171 when reading */
172 int buf_index;
173 int buf_size; /* 0 when writing */
174 uint8_t buf[IO_BUF_SIZE];
175
176 int has_error;
177};
178
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200179typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000180{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200181 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000182 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200183} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000184
185typedef struct QEMUFileSocket
186{
187 int fd;
188 QEMUFile *file;
189} QEMUFileSocket;
190
191static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
192{
193 QEMUFileSocket *s = opaque;
194 ssize_t len;
195
196 do {
Blue Swirl00aa0042011-07-23 20:04:29 +0000197 len = qemu_recv(s->fd, buf, size, 0);
aliguoria672b462008-11-11 21:33:36 +0000198 } while (len == -1 && socket_error() == EINTR);
199
200 if (len == -1)
201 len = -socket_error();
202
203 return len;
204}
205
206static int socket_close(void *opaque)
207{
208 QEMUFileSocket *s = opaque;
Anthony Liguori7267c092011-08-20 22:09:37 -0500209 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000210 return 0;
211}
212
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200213static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000214{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200215 QEMUFileStdio *s = opaque;
216 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000217}
218
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200219static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000220{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200221 QEMUFileStdio *s = opaque;
222 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300223 int bytes;
224
225 do {
226 clearerr(fp);
227 bytes = fread(buf, 1, size, fp);
228 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
229 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000230}
231
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200232static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000233{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200234 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500235 int ret;
236 ret = pclose(s->stdio_file);
Anthony Liguori7267c092011-08-20 22:09:37 -0500237 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500238 return ret;
aliguoria672b462008-11-11 21:33:36 +0000239}
240
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200241static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000242{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200243 QEMUFileStdio *s = opaque;
244 fclose(s->stdio_file);
Anthony Liguori7267c092011-08-20 22:09:37 -0500245 g_free(s);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200246 return 0;
247}
aliguoria672b462008-11-11 21:33:36 +0000248
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200249QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
250{
251 QEMUFileStdio *s;
252
253 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000254 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
255 return NULL;
256 }
257
Anthony Liguori7267c092011-08-20 22:09:37 -0500258 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000259
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200260 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000261
262 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200263 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
264 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000265 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200266 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
267 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000268 }
aliguoria672b462008-11-11 21:33:36 +0000269 return s->file;
270}
271
272QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
273{
274 FILE *popen_file;
275
276 popen_file = popen(command, mode);
277 if(popen_file == NULL) {
278 return NULL;
279 }
280
281 return qemu_popen(popen_file, mode);
282}
283
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200284int qemu_stdio_fd(QEMUFile *f)
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200285{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200286 QEMUFileStdio *p;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200287 int fd;
288
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200289 p = (QEMUFileStdio *)f->opaque;
290 fd = fileno(p->stdio_file);
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200291
292 return fd;
293}
294
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200295QEMUFile *qemu_fdopen(int fd, const char *mode)
296{
297 QEMUFileStdio *s;
298
299 if (mode == NULL ||
300 (mode[0] != 'r' && mode[0] != 'w') ||
301 mode[1] != 'b' || mode[2] != 0) {
302 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
303 return NULL;
304 }
305
Anthony Liguori7267c092011-08-20 22:09:37 -0500306 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200307 s->stdio_file = fdopen(fd, mode);
308 if (!s->stdio_file)
309 goto fail;
310
311 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200312 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
313 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200314 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200315 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
316 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200317 }
318 return s->file;
319
320fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500321 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200322 return NULL;
323}
324
aliguoria672b462008-11-11 21:33:36 +0000325QEMUFile *qemu_fopen_socket(int fd)
326{
Anthony Liguori7267c092011-08-20 22:09:37 -0500327 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000328
aliguoria672b462008-11-11 21:33:36 +0000329 s->fd = fd;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200330 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
331 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000332 return s->file;
333}
334
aliguoria672b462008-11-11 21:33:36 +0000335static int file_put_buffer(void *opaque, const uint8_t *buf,
336 int64_t pos, int size)
337{
338 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200339 fseek(s->stdio_file, pos, SEEK_SET);
Kirill A. Shutemov5fdb3aa2009-12-25 18:19:19 +0000340 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000341}
342
343static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
344{
345 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200346 fseek(s->stdio_file, pos, SEEK_SET);
347 return fread(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000348}
349
350QEMUFile *qemu_fopen(const char *filename, const char *mode)
351{
352 QEMUFileStdio *s;
353
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200354 if (mode == NULL ||
355 (mode[0] != 'r' && mode[0] != 'w') ||
356 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000357 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200358 return NULL;
359 }
360
Anthony Liguori7267c092011-08-20 22:09:37 -0500361 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000362
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200363 s->stdio_file = fopen(filename, mode);
364 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000365 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200366
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200367 if(mode[0] == 'w') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200368 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
369 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200370 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200371 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
372 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200373 }
374 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000375fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500376 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000377 return NULL;
378}
379
aliguori178e08a2009-04-05 19:10:55 +0000380static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000381 int64_t pos, int size)
382{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200383 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000384 return size;
385}
386
aliguori178e08a2009-04-05 19:10:55 +0000387static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000388{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200389 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000390}
391
392static int bdrv_fclose(void *opaque)
393{
aliguoria672b462008-11-11 21:33:36 +0000394 return 0;
395}
396
Christoph Hellwig45566e92009-07-10 23:11:57 +0200397static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000398{
aliguoria672b462008-11-11 21:33:36 +0000399 if (is_writable)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200400 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
401 NULL, NULL, NULL);
402 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000403}
404
405QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
406 QEMUFileGetBufferFunc *get_buffer,
407 QEMUFileCloseFunc *close,
Glauber Costa19629532009-05-20 18:26:57 -0400408 QEMUFileRateLimit *rate_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200409 QEMUFileSetRateLimit *set_rate_limit,
410 QEMUFileGetRateLimit *get_rate_limit)
aliguoria672b462008-11-11 21:33:36 +0000411{
412 QEMUFile *f;
413
Anthony Liguori7267c092011-08-20 22:09:37 -0500414 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000415
416 f->opaque = opaque;
417 f->put_buffer = put_buffer;
418 f->get_buffer = get_buffer;
419 f->close = close;
420 f->rate_limit = rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400421 f->set_rate_limit = set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200422 f->get_rate_limit = get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000423 f->is_write = 0;
424
425 return f;
426}
427
428int qemu_file_has_error(QEMUFile *f)
429{
430 return f->has_error;
431}
432
aliguori4dabe242009-04-05 19:30:51 +0000433void qemu_file_set_error(QEMUFile *f)
434{
435 f->has_error = 1;
436}
437
aliguoria672b462008-11-11 21:33:36 +0000438void qemu_fflush(QEMUFile *f)
439{
440 if (!f->put_buffer)
441 return;
442
443 if (f->is_write && f->buf_index > 0) {
444 int len;
445
446 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
447 if (len > 0)
448 f->buf_offset += f->buf_index;
449 else
450 f->has_error = 1;
451 f->buf_index = 0;
452 }
453}
454
455static void qemu_fill_buffer(QEMUFile *f)
456{
457 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200458 int pending;
aliguoria672b462008-11-11 21:33:36 +0000459
460 if (!f->get_buffer)
461 return;
462
463 if (f->is_write)
464 abort();
465
Juan Quintela0046c452011-09-30 19:28:45 +0200466 pending = f->buf_size - f->buf_index;
467 if (pending > 0) {
468 memmove(f->buf, f->buf + f->buf_index, pending);
469 }
470 f->buf_index = 0;
471 f->buf_size = pending;
472
473 len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
474 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000475 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200476 f->buf_size += len;
aliguoria672b462008-11-11 21:33:36 +0000477 f->buf_offset += len;
478 } else if (len != -EAGAIN)
479 f->has_error = 1;
480}
481
482int qemu_fclose(QEMUFile *f)
483{
484 int ret = 0;
485 qemu_fflush(f);
486 if (f->close)
487 ret = f->close(f->opaque);
Anthony Liguori7267c092011-08-20 22:09:37 -0500488 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000489 return ret;
490}
491
492void qemu_file_put_notify(QEMUFile *f)
493{
494 f->put_buffer(f->opaque, NULL, 0, 0);
495}
496
497void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
498{
499 int l;
500
501 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
502 fprintf(stderr,
503 "Attempted to write to buffer while read buffer is not empty\n");
504 abort();
505 }
506
507 while (!f->has_error && size > 0) {
508 l = IO_BUF_SIZE - f->buf_index;
509 if (l > size)
510 l = size;
511 memcpy(f->buf + f->buf_index, buf, l);
512 f->is_write = 1;
513 f->buf_index += l;
514 buf += l;
515 size -= l;
516 if (f->buf_index >= IO_BUF_SIZE)
517 qemu_fflush(f);
518 }
519}
520
521void qemu_put_byte(QEMUFile *f, int v)
522{
523 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
524 fprintf(stderr,
525 "Attempted to write to buffer while read buffer is not empty\n");
526 abort();
527 }
528
529 f->buf[f->buf_index++] = v;
530 f->is_write = 1;
531 if (f->buf_index >= IO_BUF_SIZE)
532 qemu_fflush(f);
533}
534
Juan Quintelac6380722011-10-04 15:28:31 +0200535static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000536{
Juan Quintelac6380722011-10-04 15:28:31 +0200537 if (f->buf_index + size <= f->buf_size) {
538 f->buf_index += size;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200539 }
aliguoria672b462008-11-11 21:33:36 +0000540}
541
Juan Quintelac6380722011-10-04 15:28:31 +0200542static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200543{
Juan Quintelac6380722011-10-04 15:28:31 +0200544 int pending;
545 int index;
546
Juan Quintelab9ce1452011-10-04 13:55:32 +0200547 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200548 abort();
Juan Quintelab9ce1452011-10-04 13:55:32 +0200549 }
Juan Quintela811814b2010-07-26 21:38:43 +0200550
Juan Quintelac6380722011-10-04 15:28:31 +0200551 index = f->buf_index + offset;
552 pending = f->buf_size - index;
553 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200554 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200555 index = f->buf_index + offset;
556 pending = f->buf_size - index;
557 }
558
559 if (pending <= 0) {
560 return 0;
561 }
562 if (size > pending) {
563 size = pending;
564 }
565
566 memcpy(buf, f->buf + index, size);
567 return size;
568}
569
570int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
571{
572 int pending = size;
573 int done = 0;
574
575 while (pending > 0) {
576 int res;
577
578 res = qemu_peek_buffer(f, buf, pending, 0);
579 if (res == 0) {
580 return done;
581 }
582 qemu_file_skip(f, res);
583 buf += res;
584 pending -= res;
585 done += res;
586 }
587 return done;
588}
589
590static int qemu_peek_byte(QEMUFile *f, int offset)
591{
592 int index = f->buf_index + offset;
593
594 if (f->is_write) {
595 abort();
596 }
597
598 if (index >= f->buf_size) {
599 qemu_fill_buffer(f);
600 index = f->buf_index + offset;
601 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200602 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200603 }
Juan Quintela811814b2010-07-26 21:38:43 +0200604 }
Juan Quintelac6380722011-10-04 15:28:31 +0200605 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200606}
607
aliguoria672b462008-11-11 21:33:36 +0000608int qemu_get_byte(QEMUFile *f)
609{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200610 int result;
aliguoria672b462008-11-11 21:33:36 +0000611
Juan Quintelac6380722011-10-04 15:28:31 +0200612 result = qemu_peek_byte(f, 0);
613 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200614 return result;
aliguoria672b462008-11-11 21:33:36 +0000615}
616
617int64_t qemu_ftell(QEMUFile *f)
618{
619 return f->buf_offset - f->buf_size + f->buf_index;
620}
621
622int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
623{
624 if (whence == SEEK_SET) {
625 /* nothing to do */
626 } else if (whence == SEEK_CUR) {
627 pos += qemu_ftell(f);
628 } else {
629 /* SEEK_END not supported */
630 return -1;
631 }
632 if (f->put_buffer) {
633 qemu_fflush(f);
634 f->buf_offset = pos;
635 } else {
636 f->buf_offset = pos;
637 f->buf_index = 0;
638 f->buf_size = 0;
639 }
640 return pos;
641}
642
643int qemu_file_rate_limit(QEMUFile *f)
644{
645 if (f->rate_limit)
646 return f->rate_limit(f->opaque);
647
648 return 0;
649}
650
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200651int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200652{
653 if (f->get_rate_limit)
654 return f->get_rate_limit(f->opaque);
655
656 return 0;
657}
658
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200659int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
Glauber Costa19629532009-05-20 18:26:57 -0400660{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400661 /* any failed or completed migration keeps its state to allow probing of
662 * migration data, but has no associated file anymore */
663 if (f && f->set_rate_limit)
Glauber Costa19629532009-05-20 18:26:57 -0400664 return f->set_rate_limit(f->opaque, new_rate);
665
666 return 0;
667}
668
aliguoria672b462008-11-11 21:33:36 +0000669void qemu_put_be16(QEMUFile *f, unsigned int v)
670{
671 qemu_put_byte(f, v >> 8);
672 qemu_put_byte(f, v);
673}
674
675void qemu_put_be32(QEMUFile *f, unsigned int v)
676{
677 qemu_put_byte(f, v >> 24);
678 qemu_put_byte(f, v >> 16);
679 qemu_put_byte(f, v >> 8);
680 qemu_put_byte(f, v);
681}
682
683void qemu_put_be64(QEMUFile *f, uint64_t v)
684{
685 qemu_put_be32(f, v >> 32);
686 qemu_put_be32(f, v);
687}
688
689unsigned int qemu_get_be16(QEMUFile *f)
690{
691 unsigned int v;
692 v = qemu_get_byte(f) << 8;
693 v |= qemu_get_byte(f);
694 return v;
695}
696
697unsigned int qemu_get_be32(QEMUFile *f)
698{
699 unsigned int v;
700 v = qemu_get_byte(f) << 24;
701 v |= qemu_get_byte(f) << 16;
702 v |= qemu_get_byte(f) << 8;
703 v |= qemu_get_byte(f);
704 return v;
705}
706
707uint64_t qemu_get_be64(QEMUFile *f)
708{
709 uint64_t v;
710 v = (uint64_t)qemu_get_be32(f) << 32;
711 v |= qemu_get_be32(f);
712 return v;
713}
714
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100715/* bool */
716
717static int get_bool(QEMUFile *f, void *pv, size_t size)
718{
719 bool *v = pv;
720 *v = qemu_get_byte(f);
721 return 0;
722}
723
724static void put_bool(QEMUFile *f, void *pv, size_t size)
725{
726 bool *v = pv;
727 qemu_put_byte(f, *v);
728}
729
730const VMStateInfo vmstate_info_bool = {
731 .name = "bool",
732 .get = get_bool,
733 .put = put_bool,
734};
735
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200736/* 8 bit int */
737
738static int get_int8(QEMUFile *f, void *pv, size_t size)
739{
740 int8_t *v = pv;
741 qemu_get_s8s(f, v);
742 return 0;
743}
744
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200745static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200746{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200747 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200748 qemu_put_s8s(f, v);
749}
750
751const VMStateInfo vmstate_info_int8 = {
752 .name = "int8",
753 .get = get_int8,
754 .put = put_int8,
755};
756
757/* 16 bit int */
758
759static int get_int16(QEMUFile *f, void *pv, size_t size)
760{
761 int16_t *v = pv;
762 qemu_get_sbe16s(f, v);
763 return 0;
764}
765
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200766static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200767{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200768 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200769 qemu_put_sbe16s(f, v);
770}
771
772const VMStateInfo vmstate_info_int16 = {
773 .name = "int16",
774 .get = get_int16,
775 .put = put_int16,
776};
777
778/* 32 bit int */
779
780static int get_int32(QEMUFile *f, void *pv, size_t size)
781{
782 int32_t *v = pv;
783 qemu_get_sbe32s(f, v);
784 return 0;
785}
786
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200787static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200788{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200789 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200790 qemu_put_sbe32s(f, v);
791}
792
793const VMStateInfo vmstate_info_int32 = {
794 .name = "int32",
795 .get = get_int32,
796 .put = put_int32,
797};
798
Juan Quintela82501662009-08-20 19:42:32 +0200799/* 32 bit int. See that the received value is the same than the one
800 in the field */
801
802static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
803{
804 int32_t *v = pv;
805 int32_t v2;
806 qemu_get_sbe32s(f, &v2);
807
808 if (*v == v2)
809 return 0;
810 return -EINVAL;
811}
812
813const VMStateInfo vmstate_info_int32_equal = {
814 .name = "int32 equal",
815 .get = get_int32_equal,
816 .put = put_int32,
817};
818
Juan Quintela0a031e02009-08-20 19:42:37 +0200819/* 32 bit int. See that the received value is the less or the same
820 than the one in the field */
821
822static int get_int32_le(QEMUFile *f, void *pv, size_t size)
823{
824 int32_t *old = pv;
825 int32_t new;
826 qemu_get_sbe32s(f, &new);
827
828 if (*old <= new)
829 return 0;
830 return -EINVAL;
831}
832
833const VMStateInfo vmstate_info_int32_le = {
834 .name = "int32 equal",
835 .get = get_int32_le,
836 .put = put_int32,
837};
838
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200839/* 64 bit int */
840
841static int get_int64(QEMUFile *f, void *pv, size_t size)
842{
843 int64_t *v = pv;
844 qemu_get_sbe64s(f, v);
845 return 0;
846}
847
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200848static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200849{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200850 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200851 qemu_put_sbe64s(f, v);
852}
853
854const VMStateInfo vmstate_info_int64 = {
855 .name = "int64",
856 .get = get_int64,
857 .put = put_int64,
858};
859
860/* 8 bit unsigned int */
861
862static int get_uint8(QEMUFile *f, void *pv, size_t size)
863{
864 uint8_t *v = pv;
865 qemu_get_8s(f, v);
866 return 0;
867}
868
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200869static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200870{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200871 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200872 qemu_put_8s(f, v);
873}
874
875const VMStateInfo vmstate_info_uint8 = {
876 .name = "uint8",
877 .get = get_uint8,
878 .put = put_uint8,
879};
880
881/* 16 bit unsigned int */
882
883static int get_uint16(QEMUFile *f, void *pv, size_t size)
884{
885 uint16_t *v = pv;
886 qemu_get_be16s(f, v);
887 return 0;
888}
889
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200890static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200891{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200892 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200893 qemu_put_be16s(f, v);
894}
895
896const VMStateInfo vmstate_info_uint16 = {
897 .name = "uint16",
898 .get = get_uint16,
899 .put = put_uint16,
900};
901
902/* 32 bit unsigned int */
903
904static int get_uint32(QEMUFile *f, void *pv, size_t size)
905{
906 uint32_t *v = pv;
907 qemu_get_be32s(f, v);
908 return 0;
909}
910
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200911static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200912{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200913 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200914 qemu_put_be32s(f, v);
915}
916
917const VMStateInfo vmstate_info_uint32 = {
918 .name = "uint32",
919 .get = get_uint32,
920 .put = put_uint32,
921};
922
Juan Quintela9122a8f2011-03-10 12:33:48 +0100923/* 32 bit uint. See that the received value is the same than the one
924 in the field */
925
926static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
927{
928 uint32_t *v = pv;
929 uint32_t v2;
930 qemu_get_be32s(f, &v2);
931
932 if (*v == v2) {
933 return 0;
934 }
935 return -EINVAL;
936}
937
938const VMStateInfo vmstate_info_uint32_equal = {
939 .name = "uint32 equal",
940 .get = get_uint32_equal,
941 .put = put_uint32,
942};
943
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200944/* 64 bit unsigned int */
945
946static int get_uint64(QEMUFile *f, void *pv, size_t size)
947{
948 uint64_t *v = pv;
949 qemu_get_be64s(f, v);
950 return 0;
951}
952
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200953static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200954{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200955 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200956 qemu_put_be64s(f, v);
957}
958
959const VMStateInfo vmstate_info_uint64 = {
960 .name = "uint64",
961 .get = get_uint64,
962 .put = put_uint64,
963};
964
Juan Quintela80cd83e2009-09-10 03:04:36 +0200965/* 8 bit int. See that the received value is the same than the one
966 in the field */
967
968static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
969{
970 uint8_t *v = pv;
971 uint8_t v2;
972 qemu_get_8s(f, &v2);
973
974 if (*v == v2)
975 return 0;
976 return -EINVAL;
977}
978
979const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +0200980 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +0200981 .get = get_uint8_equal,
982 .put = put_uint8,
983};
984
Juan Quinteladc3b83a2009-10-15 23:16:13 +0200985/* 16 bit unsigned int int. See that the received value is the same than the one
986 in the field */
987
988static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
989{
990 uint16_t *v = pv;
991 uint16_t v2;
992 qemu_get_be16s(f, &v2);
993
994 if (*v == v2)
995 return 0;
996 return -EINVAL;
997}
998
999const VMStateInfo vmstate_info_uint16_equal = {
1000 .name = "uint16 equal",
1001 .get = get_uint16_equal,
1002 .put = put_uint16,
1003};
1004
Juan Quinteladde04632009-08-20 19:42:26 +02001005/* timers */
1006
1007static int get_timer(QEMUFile *f, void *pv, size_t size)
1008{
1009 QEMUTimer *v = pv;
1010 qemu_get_timer(f, v);
1011 return 0;
1012}
1013
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001014static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001015{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001016 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001017 qemu_put_timer(f, v);
1018}
1019
1020const VMStateInfo vmstate_info_timer = {
1021 .name = "timer",
1022 .get = get_timer,
1023 .put = put_timer,
1024};
1025
Juan Quintela6f67c502009-08-20 19:42:35 +02001026/* uint8_t buffers */
1027
1028static int get_buffer(QEMUFile *f, void *pv, size_t size)
1029{
1030 uint8_t *v = pv;
1031 qemu_get_buffer(f, v, size);
1032 return 0;
1033}
1034
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001035static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001036{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001037 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001038 qemu_put_buffer(f, v, size);
1039}
1040
1041const VMStateInfo vmstate_info_buffer = {
1042 .name = "buffer",
1043 .get = get_buffer,
1044 .put = put_buffer,
1045};
1046
Juan Quintela76507c72009-10-19 15:46:28 +02001047/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001048 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001049
1050static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1051{
Jan Kiszka21174c32009-12-02 12:36:35 +01001052 uint8_t buf[1024];
1053 int block_len;
1054
1055 while (size > 0) {
1056 block_len = MIN(sizeof(buf), size);
1057 size -= block_len;
1058 qemu_get_buffer(f, buf, block_len);
1059 }
1060 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001061}
1062
1063static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1064{
Jan Kiszka21174c32009-12-02 12:36:35 +01001065 static const uint8_t buf[1024];
1066 int block_len;
1067
1068 while (size > 0) {
1069 block_len = MIN(sizeof(buf), size);
1070 size -= block_len;
1071 qemu_put_buffer(f, buf, block_len);
1072 }
Juan Quintela76507c72009-10-19 15:46:28 +02001073}
1074
1075const VMStateInfo vmstate_info_unused_buffer = {
1076 .name = "unused_buffer",
1077 .get = get_unused_buffer,
1078 .put = put_unused_buffer,
1079};
1080
Alex Williamson7685ee62010-06-25 11:09:14 -06001081typedef struct CompatEntry {
1082 char idstr[256];
1083 int instance_id;
1084} CompatEntry;
1085
aliguoria672b462008-11-11 21:33:36 +00001086typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001087 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001088 char idstr[256];
1089 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001090 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001091 int version_id;
1092 int section_id;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001093 SaveSetParamsHandler *set_params;
aliguoria672b462008-11-11 21:33:36 +00001094 SaveLiveStateHandler *save_live_state;
1095 SaveStateHandler *save_state;
1096 LoadStateHandler *load_state;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001097 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001098 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001099 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001100 int no_migrate;
aliguoria672b462008-11-11 21:33:36 +00001101} SaveStateEntry;
1102
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001103
Blue Swirl72cf2d42009-09-12 07:36:22 +00001104static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1105 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001106static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001107
Juan Quintela8718e992009-09-01 02:12:31 +02001108static int calculate_new_instance_id(const char *idstr)
1109{
1110 SaveStateEntry *se;
1111 int instance_id = 0;
1112
Blue Swirl72cf2d42009-09-12 07:36:22 +00001113 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001114 if (strcmp(idstr, se->idstr) == 0
1115 && instance_id <= se->instance_id) {
1116 instance_id = se->instance_id + 1;
1117 }
1118 }
1119 return instance_id;
1120}
1121
Alex Williamson7685ee62010-06-25 11:09:14 -06001122static int calculate_compat_instance_id(const char *idstr)
1123{
1124 SaveStateEntry *se;
1125 int instance_id = 0;
1126
1127 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1128 if (!se->compat)
1129 continue;
1130
1131 if (strcmp(idstr, se->compat->idstr) == 0
1132 && instance_id <= se->compat->instance_id) {
1133 instance_id = se->compat->instance_id + 1;
1134 }
1135 }
1136 return instance_id;
1137}
1138
aliguoria672b462008-11-11 21:33:36 +00001139/* TODO: Individual devices generally have very little idea about the rest
1140 of the system, so instance_id should be removed/replaced.
1141 Meanwhile pass -1 as instance_id if you do not already have a clearly
1142 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001143int register_savevm_live(DeviceState *dev,
1144 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001145 int instance_id,
1146 int version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001147 SaveSetParamsHandler *set_params,
aliguoria672b462008-11-11 21:33:36 +00001148 SaveLiveStateHandler *save_live_state,
1149 SaveStateHandler *save_state,
1150 LoadStateHandler *load_state,
1151 void *opaque)
1152{
Juan Quintela8718e992009-09-01 02:12:31 +02001153 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001154
Anthony Liguori7267c092011-08-20 22:09:37 -05001155 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001156 se->version_id = version_id;
1157 se->section_id = global_section_id++;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001158 se->set_params = set_params;
aliguoria672b462008-11-11 21:33:36 +00001159 se->save_live_state = save_live_state;
1160 se->save_state = save_state;
1161 se->load_state = load_state;
1162 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001163 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001164 se->no_migrate = 0;
aliguoria672b462008-11-11 21:33:36 +00001165
Alex Williamson7685ee62010-06-25 11:09:14 -06001166 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1167 char *id = dev->parent_bus->info->get_dev_path(dev);
1168 if (id) {
1169 pstrcpy(se->idstr, sizeof(se->idstr), id);
1170 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001171 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001172
Anthony Liguori7267c092011-08-20 22:09:37 -05001173 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001174 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1175 se->compat->instance_id = instance_id == -1 ?
1176 calculate_compat_instance_id(idstr) : instance_id;
1177 instance_id = -1;
1178 }
1179 }
1180 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1181
Juan Quintela8718e992009-09-01 02:12:31 +02001182 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001183 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001184 } else {
1185 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001186 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001187 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001188 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001189 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001190 return 0;
1191}
1192
Alex Williamson0be71e32010-06-25 11:09:07 -06001193int register_savevm(DeviceState *dev,
1194 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001195 int instance_id,
1196 int version_id,
1197 SaveStateHandler *save_state,
1198 LoadStateHandler *load_state,
1199 void *opaque)
1200{
Alex Williamson0be71e32010-06-25 11:09:07 -06001201 return register_savevm_live(dev, idstr, instance_id, version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001202 NULL, NULL, save_state, load_state, opaque);
aliguoria672b462008-11-11 21:33:36 +00001203}
1204
Alex Williamson0be71e32010-06-25 11:09:07 -06001205void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001206{
Juan Quintela8718e992009-09-01 02:12:31 +02001207 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001208 char id[256] = "";
1209
1210 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1211 char *path = dev->parent_bus->info->get_dev_path(dev);
1212 if (path) {
1213 pstrcpy(id, sizeof(id), path);
1214 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001215 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001216 }
1217 }
1218 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001219
Blue Swirl72cf2d42009-09-12 07:36:22 +00001220 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001221 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001222 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001223 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001224 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001225 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001226 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001227 }
aliguori41bd13a2009-04-17 17:10:59 +00001228 }
1229}
1230
Cam Macdonell24312962010-07-26 18:11:00 -06001231/* mark a device as not to be migrated, that is the device should be
1232 unplugged before migration */
1233void register_device_unmigratable(DeviceState *dev, const char *idstr,
1234 void *opaque)
1235{
1236 SaveStateEntry *se;
1237 char id[256] = "";
1238
1239 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1240 char *path = dev->parent_bus->info->get_dev_path(dev);
1241 if (path) {
1242 pstrcpy(id, sizeof(id), path);
1243 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001244 g_free(path);
Cam Macdonell24312962010-07-26 18:11:00 -06001245 }
1246 }
1247 pstrcat(id, sizeof(id), idstr);
1248
1249 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1250 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1251 se->no_migrate = 1;
1252 }
1253 }
1254}
1255
Alex Williamson0be71e32010-06-25 11:09:07 -06001256int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001257 const VMStateDescription *vmsd,
1258 void *opaque, int alias_id,
1259 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001260{
Juan Quintela8718e992009-09-01 02:12:31 +02001261 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001262
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001263 /* If this triggers, alias support can be dropped for the vmsd. */
1264 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1265
Anthony Liguori7267c092011-08-20 22:09:37 -05001266 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001267 se->version_id = vmsd->version_id;
1268 se->section_id = global_section_id++;
1269 se->save_live_state = NULL;
1270 se->save_state = NULL;
1271 se->load_state = NULL;
1272 se->opaque = opaque;
1273 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001274 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001275 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001276
Alex Williamson7685ee62010-06-25 11:09:14 -06001277 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1278 char *id = dev->parent_bus->info->get_dev_path(dev);
1279 if (id) {
1280 pstrcpy(se->idstr, sizeof(se->idstr), id);
1281 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001282 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001283
Anthony Liguori7267c092011-08-20 22:09:37 -05001284 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001285 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1286 se->compat->instance_id = instance_id == -1 ?
1287 calculate_compat_instance_id(vmsd->name) : instance_id;
1288 instance_id = -1;
1289 }
1290 }
1291 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1292
Juan Quintela8718e992009-09-01 02:12:31 +02001293 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001294 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001295 } else {
1296 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001297 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001298 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001299 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001300 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001301 return 0;
1302}
1303
Alex Williamson0be71e32010-06-25 11:09:07 -06001304int vmstate_register(DeviceState *dev, int instance_id,
1305 const VMStateDescription *vmsd, void *opaque)
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001306{
Alex Williamson0be71e32010-06-25 11:09:07 -06001307 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1308 opaque, -1, 0);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001309}
1310
Alex Williamson0be71e32010-06-25 11:09:07 -06001311void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1312 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001313{
Juan Quintela1eb75382009-09-10 03:04:29 +02001314 SaveStateEntry *se, *new_se;
1315
Blue Swirl72cf2d42009-09-12 07:36:22 +00001316 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001317 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001318 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001319 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001320 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001321 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001322 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001323 }
1324 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001325}
1326
Juan Quintela811814b2010-07-26 21:38:43 +02001327static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1328 void *opaque);
1329static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1330 void *opaque);
1331
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001332int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1333 void *opaque, int version_id)
1334{
1335 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001336 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001337
1338 if (version_id > vmsd->version_id) {
1339 return -EINVAL;
1340 }
1341 if (version_id < vmsd->minimum_version_id_old) {
1342 return -EINVAL;
1343 }
1344 if (version_id < vmsd->minimum_version_id) {
1345 return vmsd->load_state_old(f, opaque, version_id);
1346 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001347 if (vmsd->pre_load) {
1348 int ret = vmsd->pre_load(opaque);
1349 if (ret)
1350 return ret;
1351 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001352 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001353 if ((field->field_exists &&
1354 field->field_exists(opaque, version_id)) ||
1355 (!field->field_exists &&
1356 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001357 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001358 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001359 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001360
Juan Quintelae61a1e02009-12-02 12:36:38 +01001361 if (field->flags & VMS_VBUFFER) {
1362 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001363 if (field->flags & VMS_MULTIPLY) {
1364 size *= field->size;
1365 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001366 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001367 if (field->flags & VMS_ARRAY) {
1368 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001369 } else if (field->flags & VMS_VARRAY_INT32) {
1370 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001371 } else if (field->flags & VMS_VARRAY_UINT32) {
1372 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001373 } else if (field->flags & VMS_VARRAY_UINT16) {
1374 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001375 } else if (field->flags & VMS_VARRAY_UINT8) {
1376 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001377 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001378 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001379 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001380 }
1381 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001382 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001383
Juan Quintela19df4382009-09-29 22:48:41 +02001384 if (field->flags & VMS_ARRAY_OF_POINTER) {
1385 addr = *(void **)addr;
1386 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001387 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001388 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001389 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001390 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001391
1392 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001393 if (ret < 0) {
1394 return ret;
1395 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001396 }
1397 }
1398 field++;
1399 }
Juan Quintela811814b2010-07-26 21:38:43 +02001400 ret = vmstate_subsection_load(f, vmsd, opaque);
1401 if (ret != 0) {
1402 return ret;
1403 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001404 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001405 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001406 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001407 return 0;
1408}
1409
1410void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001411 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001412{
1413 VMStateField *field = vmsd->fields;
1414
Juan Quintela8fb07912009-09-10 03:04:32 +02001415 if (vmsd->pre_save) {
1416 vmsd->pre_save(opaque);
1417 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001418 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001419 if (!field->field_exists ||
1420 field->field_exists(opaque, vmsd->version_id)) {
1421 void *base_addr = opaque + field->offset;
1422 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001423 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001424
Juan Quintelae61a1e02009-12-02 12:36:38 +01001425 if (field->flags & VMS_VBUFFER) {
1426 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001427 if (field->flags & VMS_MULTIPLY) {
1428 size *= field->size;
1429 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001430 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001431 if (field->flags & VMS_ARRAY) {
1432 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001433 } else if (field->flags & VMS_VARRAY_INT32) {
1434 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001435 } else if (field->flags & VMS_VARRAY_UINT16) {
1436 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001437 } else if (field->flags & VMS_VARRAY_UINT8) {
1438 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001439 }
1440 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001441 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001442 }
1443 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001444 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001445
Juan Quintela85953872009-12-02 12:36:37 +01001446 if (field->flags & VMS_ARRAY_OF_POINTER) {
1447 addr = *(void **)addr;
1448 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001449 if (field->flags & VMS_STRUCT) {
1450 vmstate_save_state(f, field->vmsd, addr);
1451 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001452 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001453 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001454 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001455 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001456 field++;
1457 }
Juan Quintela811814b2010-07-26 21:38:43 +02001458 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001459}
1460
Juan Quintela4082be42009-08-20 19:42:24 +02001461static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1462{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001463 if (!se->vmsd) { /* Old style */
1464 return se->load_state(f, se->opaque, version_id);
1465 }
1466 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001467}
1468
Alex Williamsondc912122011-01-11 14:39:43 -07001469static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001470{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001471 if (!se->vmsd) { /* Old style */
1472 se->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001473 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001474 }
1475 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001476}
1477
aliguoria672b462008-11-11 21:33:36 +00001478#define QEMU_VM_FILE_MAGIC 0x5145564d
1479#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1480#define QEMU_VM_FILE_VERSION 0x00000003
1481
1482#define QEMU_VM_EOF 0x00
1483#define QEMU_VM_SECTION_START 0x01
1484#define QEMU_VM_SECTION_PART 0x02
1485#define QEMU_VM_SECTION_END 0x03
1486#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001487#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001488
Alex Williamsondc912122011-01-11 14:39:43 -07001489bool qemu_savevm_state_blocked(Monitor *mon)
1490{
1491 SaveStateEntry *se;
1492
1493 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1494 if (se->no_migrate) {
1495 monitor_printf(mon, "state blocked by non-migratable device '%s'\n",
1496 se->idstr);
1497 return true;
1498 }
1499 }
1500 return false;
1501}
1502
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001503int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
1504 int shared)
aliguoria672b462008-11-11 21:33:36 +00001505{
1506 SaveStateEntry *se;
1507
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001508 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1509 if(se->set_params == NULL) {
1510 continue;
1511 }
1512 se->set_params(blk_enable, shared, se->opaque);
1513 }
1514
aliguoria672b462008-11-11 21:33:36 +00001515 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1516 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1517
Blue Swirl72cf2d42009-09-12 07:36:22 +00001518 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001519 int len;
1520
1521 if (se->save_live_state == NULL)
1522 continue;
1523
1524 /* Section type */
1525 qemu_put_byte(f, QEMU_VM_SECTION_START);
1526 qemu_put_be32(f, se->section_id);
1527
1528 /* ID string */
1529 len = strlen(se->idstr);
1530 qemu_put_byte(f, len);
1531 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1532
1533 qemu_put_be32(f, se->instance_id);
1534 qemu_put_be32(f, se->version_id);
1535
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001536 se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque);
aliguoria672b462008-11-11 21:33:36 +00001537 }
1538
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001539 if (qemu_file_has_error(f)) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001540 qemu_savevm_state_cancel(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001541 return -EIO;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001542 }
aliguoria672b462008-11-11 21:33:36 +00001543
1544 return 0;
1545}
1546
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001547int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001548{
1549 SaveStateEntry *se;
1550 int ret = 1;
1551
Blue Swirl72cf2d42009-09-12 07:36:22 +00001552 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001553 if (se->save_live_state == NULL)
1554 continue;
1555
1556 /* Section type */
1557 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1558 qemu_put_be32(f, se->section_id);
1559
Jan Kiszka90697be2009-12-01 15:19:55 +01001560 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque);
1561 if (!ret) {
1562 /* Do not proceed to the next vmstate before this one reported
1563 completion of the current stage. This serializes the migration
1564 and reduces the probability that a faster changing state is
1565 synchronized over and over again. */
1566 break;
1567 }
aliguoria672b462008-11-11 21:33:36 +00001568 }
1569
1570 if (ret)
1571 return 1;
1572
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001573 if (qemu_file_has_error(f)) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001574 qemu_savevm_state_cancel(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001575 return -EIO;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001576 }
aliguoria672b462008-11-11 21:33:36 +00001577
1578 return 0;
1579}
1580
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001581int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001582{
1583 SaveStateEntry *se;
1584
Jan Kiszkaea375f92010-03-01 19:10:30 +01001585 cpu_synchronize_all_states();
1586
Blue Swirl72cf2d42009-09-12 07:36:22 +00001587 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001588 if (se->save_live_state == NULL)
1589 continue;
1590
1591 /* Section type */
1592 qemu_put_byte(f, QEMU_VM_SECTION_END);
1593 qemu_put_be32(f, se->section_id);
1594
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001595 se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque);
aliguoria672b462008-11-11 21:33:36 +00001596 }
1597
Blue Swirl72cf2d42009-09-12 07:36:22 +00001598 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001599 int len;
1600
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001601 if (se->save_state == NULL && se->vmsd == NULL)
aliguoria672b462008-11-11 21:33:36 +00001602 continue;
1603
1604 /* Section type */
1605 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1606 qemu_put_be32(f, se->section_id);
1607
1608 /* ID string */
1609 len = strlen(se->idstr);
1610 qemu_put_byte(f, len);
1611 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1612
1613 qemu_put_be32(f, se->instance_id);
1614 qemu_put_be32(f, se->version_id);
1615
Alex Williamsondc912122011-01-11 14:39:43 -07001616 vmstate_save(f, se);
aliguoria672b462008-11-11 21:33:36 +00001617 }
1618
1619 qemu_put_byte(f, QEMU_VM_EOF);
1620
1621 if (qemu_file_has_error(f))
1622 return -EIO;
1623
1624 return 0;
1625}
1626
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001627void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001628{
1629 SaveStateEntry *se;
1630
1631 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1632 if (se->save_live_state) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001633 se->save_live_state(mon, f, -1, se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001634 }
1635 }
1636}
1637
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001638static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001639{
1640 int saved_vm_running;
1641 int ret;
1642
Luiz Capitulino13548692011-07-29 15:36:43 -03001643 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001644 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00001645
Alex Williamsondc912122011-01-11 14:39:43 -07001646 if (qemu_savevm_state_blocked(mon)) {
1647 ret = -EINVAL;
1648 goto out;
1649 }
1650
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001651 ret = qemu_savevm_state_begin(mon, f, 0, 0);
aliguoria672b462008-11-11 21:33:36 +00001652 if (ret < 0)
1653 goto out;
1654
1655 do {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001656 ret = qemu_savevm_state_iterate(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001657 if (ret < 0)
1658 goto out;
1659 } while (ret == 0);
1660
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001661 ret = qemu_savevm_state_complete(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001662
1663out:
1664 if (qemu_file_has_error(f))
1665 ret = -EIO;
1666
1667 if (!ret && saved_vm_running)
1668 vm_start();
1669
1670 return ret;
1671}
1672
1673static SaveStateEntry *find_se(const char *idstr, int instance_id)
1674{
1675 SaveStateEntry *se;
1676
Blue Swirl72cf2d42009-09-12 07:36:22 +00001677 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001678 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001679 (instance_id == se->instance_id ||
1680 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001681 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001682 /* Migrating from an older version? */
1683 if (strstr(se->idstr, idstr) && se->compat) {
1684 if (!strcmp(se->compat->idstr, idstr) &&
1685 (instance_id == se->compat->instance_id ||
1686 instance_id == se->alias_id))
1687 return se;
1688 }
aliguoria672b462008-11-11 21:33:36 +00001689 }
1690 return NULL;
1691}
1692
Juan Quintela811814b2010-07-26 21:38:43 +02001693static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1694{
1695 while(sub && sub->needed) {
1696 if (strcmp(idstr, sub->vmsd->name) == 0) {
1697 return sub->vmsd;
1698 }
1699 sub++;
1700 }
1701 return NULL;
1702}
1703
1704static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1705 void *opaque)
1706{
Juan Quintelac6380722011-10-04 15:28:31 +02001707 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001708 char idstr[256];
1709 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001710 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001711 const VMStateDescription *sub_vmsd;
1712
Juan Quintelac6380722011-10-04 15:28:31 +02001713 len = qemu_peek_byte(f, 1);
1714 if (len < strlen(vmsd->name) + 1) {
1715 /* subsection name has be be "section_name/a" */
1716 return 0;
1717 }
1718 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1719 if (size != len) {
1720 return 0;
1721 }
1722 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02001723
Juan Quintelac6380722011-10-04 15:28:31 +02001724 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1725 /* it don't have a valid subsection name */
1726 return 0;
1727 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02001728 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02001729 if (sub_vmsd == NULL) {
1730 return -ENOENT;
1731 }
Juan Quintelac6380722011-10-04 15:28:31 +02001732 qemu_file_skip(f, 1); /* subsection */
1733 qemu_file_skip(f, 1); /* len */
1734 qemu_file_skip(f, len); /* idstr */
1735 version_id = qemu_get_be32(f);
1736
Juan Quintela811814b2010-07-26 21:38:43 +02001737 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1738 if (ret) {
1739 return ret;
1740 }
1741 }
1742 return 0;
1743}
1744
1745static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1746 void *opaque)
1747{
1748 const VMStateSubsection *sub = vmsd->subsections;
1749
1750 while (sub && sub->needed) {
1751 if (sub->needed(opaque)) {
1752 const VMStateDescription *vmsd = sub->vmsd;
1753 uint8_t len;
1754
1755 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1756 len = strlen(vmsd->name);
1757 qemu_put_byte(f, len);
1758 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1759 qemu_put_be32(f, vmsd->version_id);
1760 vmstate_save_state(f, vmsd, opaque);
1761 }
1762 sub++;
1763 }
1764}
1765
aliguoria672b462008-11-11 21:33:36 +00001766typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001767 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001768 SaveStateEntry *se;
1769 int section_id;
1770 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001771} LoadStateEntry;
1772
aliguoria672b462008-11-11 21:33:36 +00001773int qemu_loadvm_state(QEMUFile *f)
1774{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001775 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1776 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001777 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001778 uint8_t section_type;
1779 unsigned int v;
1780 int ret;
1781
Alex Williamsondc912122011-01-11 14:39:43 -07001782 if (qemu_savevm_state_blocked(default_mon)) {
1783 return -EINVAL;
1784 }
1785
aliguoria672b462008-11-11 21:33:36 +00001786 v = qemu_get_be32(f);
1787 if (v != QEMU_VM_FILE_MAGIC)
1788 return -EINVAL;
1789
1790 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001791 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1792 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1793 return -ENOTSUP;
1794 }
aliguoria672b462008-11-11 21:33:36 +00001795 if (v != QEMU_VM_FILE_VERSION)
1796 return -ENOTSUP;
1797
1798 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1799 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001800 SaveStateEntry *se;
1801 char idstr[257];
1802 int len;
1803
1804 switch (section_type) {
1805 case QEMU_VM_SECTION_START:
1806 case QEMU_VM_SECTION_FULL:
1807 /* Read section start */
1808 section_id = qemu_get_be32(f);
1809 len = qemu_get_byte(f);
1810 qemu_get_buffer(f, (uint8_t *)idstr, len);
1811 idstr[len] = 0;
1812 instance_id = qemu_get_be32(f);
1813 version_id = qemu_get_be32(f);
1814
1815 /* Find savevm section */
1816 se = find_se(idstr, instance_id);
1817 if (se == NULL) {
1818 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1819 ret = -EINVAL;
1820 goto out;
1821 }
1822
1823 /* Validate version */
1824 if (version_id > se->version_id) {
1825 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1826 version_id, idstr, se->version_id);
1827 ret = -EINVAL;
1828 goto out;
1829 }
1830
1831 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05001832 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00001833
1834 le->se = se;
1835 le->section_id = section_id;
1836 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001837 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00001838
Juan Quintela4082be42009-08-20 19:42:24 +02001839 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001840 if (ret < 0) {
1841 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1842 instance_id, idstr);
1843 goto out;
1844 }
aliguoria672b462008-11-11 21:33:36 +00001845 break;
1846 case QEMU_VM_SECTION_PART:
1847 case QEMU_VM_SECTION_END:
1848 section_id = qemu_get_be32(f);
1849
Blue Swirl72cf2d42009-09-12 07:36:22 +00001850 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001851 if (le->section_id == section_id) {
1852 break;
1853 }
1854 }
aliguoria672b462008-11-11 21:33:36 +00001855 if (le == NULL) {
1856 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1857 ret = -EINVAL;
1858 goto out;
1859 }
1860
Juan Quintela4082be42009-08-20 19:42:24 +02001861 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001862 if (ret < 0) {
1863 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1864 section_id);
1865 goto out;
1866 }
aliguoria672b462008-11-11 21:33:36 +00001867 break;
1868 default:
1869 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1870 ret = -EINVAL;
1871 goto out;
1872 }
1873 }
1874
Jan Kiszkaea375f92010-03-01 19:10:30 +01001875 cpu_synchronize_all_post_init();
1876
aliguoria672b462008-11-11 21:33:36 +00001877 ret = 0;
1878
1879out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00001880 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1881 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001882 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00001883 }
1884
1885 if (qemu_file_has_error(f))
1886 ret = -EIO;
1887
1888 return ret;
1889}
1890
aliguoria672b462008-11-11 21:33:36 +00001891static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1892 const char *name)
1893{
1894 QEMUSnapshotInfo *sn_tab, *sn;
1895 int nb_sns, i, ret;
1896
1897 ret = -ENOENT;
1898 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1899 if (nb_sns < 0)
1900 return ret;
1901 for(i = 0; i < nb_sns; i++) {
1902 sn = &sn_tab[i];
1903 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1904 *sn_info = *sn;
1905 ret = 0;
1906 break;
1907 }
1908 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001909 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00001910 return ret;
1911}
1912
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001913/*
1914 * Deletes snapshots of a given name in all opened images.
1915 */
1916static int del_existing_snapshots(Monitor *mon, const char *name)
1917{
1918 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001919 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1920 int ret;
1921
Markus Armbrusterdbc13592010-06-02 18:55:21 +02001922 bs = NULL;
1923 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001924 if (bdrv_can_snapshot(bs) &&
1925 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1926 {
1927 ret = bdrv_snapshot_delete(bs, name);
1928 if (ret < 0) {
1929 monitor_printf(mon,
1930 "Error while deleting snapshot on '%s'\n",
1931 bdrv_get_device_name(bs));
1932 return -1;
1933 }
1934 }
1935 }
1936
1937 return 0;
1938}
1939
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001940void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001941{
1942 BlockDriverState *bs, *bs1;
1943 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001944 int ret;
aliguoria672b462008-11-11 21:33:36 +00001945 QEMUFile *f;
1946 int saved_vm_running;
aliguori2d22b182008-12-11 21:06:49 +00001947 uint32_t vm_state_size;
aliguoria672b462008-11-11 21:33:36 +00001948#ifdef _WIN32
1949 struct _timeb tb;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03001950 struct tm *ptm;
aliguoria672b462008-11-11 21:33:36 +00001951#else
1952 struct timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03001953 struct tm tm;
aliguoria672b462008-11-11 21:33:36 +00001954#endif
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001955 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00001956
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001957 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02001958 bs = NULL;
1959 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001960
Markus Armbruster07b70bf2011-08-03 15:08:11 +02001961 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001962 continue;
1963 }
1964
1965 if (!bdrv_can_snapshot(bs)) {
1966 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
1967 bdrv_get_device_name(bs));
1968 return;
1969 }
1970 }
1971
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001972 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00001973 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001974 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001975 return;
1976 }
aliguoria672b462008-11-11 21:33:36 +00001977
Luiz Capitulino13548692011-07-29 15:36:43 -03001978 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001979 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00001980
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001981 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00001982
1983 /* fill auxiliary fields */
1984#ifdef _WIN32
1985 _ftime(&tb);
1986 sn->date_sec = tb.time;
1987 sn->date_nsec = tb.millitm * 1000000;
1988#else
1989 gettimeofday(&tv, NULL);
1990 sn->date_sec = tv.tv_sec;
1991 sn->date_nsec = tv.tv_usec * 1000;
1992#endif
Paolo Bonzini74475452011-03-11 16:47:48 +01001993 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00001994
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03001995 if (name) {
1996 ret = bdrv_snapshot_find(bs, old_sn, name);
1997 if (ret >= 0) {
1998 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1999 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2000 } else {
2001 pstrcpy(sn->name, sizeof(sn->name), name);
2002 }
2003 } else {
2004#ifdef _WIN32
2005 ptm = localtime(&tb.time);
2006 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2007#else
Blue Swirld7d9b522010-09-09 19:13:04 +00002008 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2009 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002010 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2011#endif
2012 }
2013
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002014 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002015 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002016 goto the_end;
2017 }
2018
aliguoria672b462008-11-11 21:33:36 +00002019 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002020 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002021 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002022 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002023 goto the_end;
2024 }
Jan Kiszkaf327aa02009-11-30 18:21:21 +01002025 ret = qemu_savevm_state(mon, f);
aliguori2d22b182008-12-11 21:06:49 +00002026 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002027 qemu_fclose(f);
2028 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002029 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002030 goto the_end;
2031 }
2032
2033 /* create the snapshots */
2034
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002035 bs1 = NULL;
2036 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002037 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002038 /* Write VM state size only to the image that contains the state */
2039 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002040 ret = bdrv_snapshot_create(bs1, sn);
2041 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002042 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2043 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002044 }
2045 }
2046 }
2047
2048 the_end:
2049 if (saved_vm_running)
2050 vm_start();
2051}
2052
Markus Armbruster03cd4652010-02-17 16:24:10 +01002053int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002054{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002055 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002056 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002057 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002058 int ret;
aliguoria672b462008-11-11 21:33:36 +00002059
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002060 bs_vm_state = bdrv_snapshots();
2061 if (!bs_vm_state) {
2062 error_report("No block device supports snapshots");
2063 return -ENOTSUP;
2064 }
2065
2066 /* Don't even try to load empty VM states */
2067 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2068 if (ret < 0) {
2069 return ret;
2070 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002071 error_report("This is a disk-only snapshot. Revert to it offline "
2072 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002073 return -EINVAL;
2074 }
2075
2076 /* Verify if there is any device that doesn't support snapshots and is
2077 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002078 bs = NULL;
2079 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002080
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002081 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002082 continue;
2083 }
2084
2085 if (!bdrv_can_snapshot(bs)) {
2086 error_report("Device '%s' is writable but does not support snapshots.",
2087 bdrv_get_device_name(bs));
2088 return -ENOTSUP;
2089 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002090
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002091 ret = bdrv_snapshot_find(bs, &sn, name);
2092 if (ret < 0) {
2093 error_report("Device '%s' does not have the requested snapshot '%s'",
2094 bdrv_get_device_name(bs), name);
2095 return ret;
2096 }
aliguoria672b462008-11-11 21:33:36 +00002097 }
2098
2099 /* Flush all IO requests so they don't interfere with the new state. */
2100 qemu_aio_flush();
2101
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002102 bs = NULL;
2103 while ((bs = bdrv_next(bs))) {
2104 if (bdrv_can_snapshot(bs)) {
2105 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002106 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002107 error_report("Error %d while activating snapshot '%s' on '%s'",
2108 ret, name, bdrv_get_device_name(bs));
2109 return ret;
aliguoria672b462008-11-11 21:33:36 +00002110 }
2111 }
2112 }
2113
aliguoria672b462008-11-11 21:33:36 +00002114 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002115 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002116 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002117 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002118 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002119 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002120
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002121 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002122 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002123
aliguoria672b462008-11-11 21:33:36 +00002124 qemu_fclose(f);
2125 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002126 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002127 return ret;
aliguoria672b462008-11-11 21:33:36 +00002128 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002129
Juan Quintela05f24012009-08-20 19:42:22 +02002130 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002131}
2132
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002133void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002134{
2135 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002136 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002137 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002138
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002139 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002140 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002141 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002142 return;
2143 }
2144
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002145 bs1 = NULL;
2146 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002147 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002148 ret = bdrv_snapshot_delete(bs1, name);
2149 if (ret < 0) {
2150 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002151 monitor_printf(mon,
2152 "Snapshots not supported on device '%s'\n",
2153 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002154 else
aliguori376253e2009-03-05 23:01:23 +00002155 monitor_printf(mon, "Error %d while deleting snapshot on "
2156 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002157 }
2158 }
2159 }
2160}
2161
aliguori376253e2009-03-05 23:01:23 +00002162void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00002163{
2164 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002165 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2166 int nb_sns, i, ret, available;
2167 int total;
2168 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002169 char buf[256];
2170
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002171 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002172 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002173 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002174 return;
2175 }
aliguoria672b462008-11-11 21:33:36 +00002176
2177 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2178 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002179 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002180 return;
2181 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002182
2183 if (nb_sns == 0) {
2184 monitor_printf(mon, "There is no snapshot available.\n");
2185 return;
aliguoria672b462008-11-11 21:33:36 +00002186 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002187
Anthony Liguori7267c092011-08-20 22:09:37 -05002188 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002189 total = 0;
2190 for (i = 0; i < nb_sns; i++) {
2191 sn = &sn_tab[i];
2192 available = 1;
2193 bs1 = NULL;
2194
2195 while ((bs1 = bdrv_next(bs1))) {
2196 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2197 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2198 if (ret < 0) {
2199 available = 0;
2200 break;
2201 }
2202 }
2203 }
2204
2205 if (available) {
2206 available_snapshots[total] = i;
2207 total++;
2208 }
2209 }
2210
2211 if (total > 0) {
2212 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2213 for (i = 0; i < total; i++) {
2214 sn = &sn_tab[available_snapshots[i]];
2215 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2216 }
2217 } else {
2218 monitor_printf(mon, "There is no suitable snapshot available\n");
2219 }
2220
Anthony Liguori7267c092011-08-20 22:09:37 -05002221 g_free(sn_tab);
2222 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002223
aliguoria672b462008-11-11 21:33:36 +00002224}