blob: f153c25419c17f4f2162c8b157e6b5c7bb40d2d8 [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"
Paolo Bonzini2ff68d02011-09-12 16:21:44 +020084#include "qemu-timer.h"
Blue Swirl17a46632011-03-27 16:05:08 +000085#include "cpus.h"
blueswir1511d2b12009-03-07 15:32:56 +000086
aliguoria672b462008-11-11 21:33:36 +000087#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000088
Nolan18995b92009-10-15 16:53:55 -070089#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040090#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070091#endif
92#define ARP_HTYPE_ETH 0x0001
93#define ARP_PTYPE_IP 0x0800
94#define ARP_OP_REQUEST_REV 0x3
95
96static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000097 uint8_t *mac_addr)
98{
Nolan18995b92009-10-15 16:53:55 -070099 /* Ethernet header. */
100 memset(buf, 0xff, 6); /* destination MAC addr */
101 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
102 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +0000103
Nolan18995b92009-10-15 16:53:55 -0700104 /* RARP header. */
105 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
106 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
107 *(buf + 18) = 6; /* hardware addr length (ethernet) */
108 *(buf + 19) = 4; /* protocol addr length (IPv4) */
109 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
110 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
111 memset(buf + 28, 0x00, 4); /* source protocol addr */
112 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
113 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +0000114
Nolan18995b92009-10-15 16:53:55 -0700115 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
116 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +0000117
Nolan18995b92009-10-15 16:53:55 -0700118 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +0000119}
120
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000121static void qemu_announce_self_iter(NICState *nic, void *opaque)
122{
123 uint8_t buf[60];
124 int len;
125
126 len = announce_self_create(buf, nic->conf->macaddr.a);
127
128 qemu_send_packet_raw(&nic->nc, buf, len);
129}
130
131
Gleb Natapoved8b3302009-05-21 17:17:44 +0300132static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000133{
Gleb Natapoved8b3302009-05-21 17:17:44 +0300134 static int count = SELF_ANNOUNCE_ROUNDS;
135 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +0000136
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000137 qemu_foreach_nic(qemu_announce_self_iter, NULL);
138
Nolan18995b92009-10-15 16:53:55 -0700139 if (--count) {
140 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100141 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -0700142 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300143 } else {
144 qemu_del_timer(timer);
145 qemu_free_timer(timer);
146 }
147}
148
149void qemu_announce_self(void)
150{
151 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100152 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300153 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000154}
155
156/***********************************************************/
157/* savevm/loadvm support */
158
159#define IO_BUF_SIZE 32768
160
161struct QEMUFile {
162 QEMUFilePutBufferFunc *put_buffer;
163 QEMUFileGetBufferFunc *get_buffer;
164 QEMUFileCloseFunc *close;
165 QEMUFileRateLimit *rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400166 QEMUFileSetRateLimit *set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200167 QEMUFileGetRateLimit *get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000168 void *opaque;
169 int is_write;
170
171 int64_t buf_offset; /* start of buffer when writing, end of buffer
172 when reading */
173 int buf_index;
174 int buf_size; /* 0 when writing */
175 uint8_t buf[IO_BUF_SIZE];
176
Juan Quintela3961b4d2011-10-05 01:05:21 +0200177 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000178};
179
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200180typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000181{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200182 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000183 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200184} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000185
186typedef struct QEMUFileSocket
187{
188 int fd;
189 QEMUFile *file;
190} QEMUFileSocket;
191
192static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
193{
194 QEMUFileSocket *s = opaque;
195 ssize_t len;
196
197 do {
Blue Swirl00aa0042011-07-23 20:04:29 +0000198 len = qemu_recv(s->fd, buf, size, 0);
aliguoria672b462008-11-11 21:33:36 +0000199 } while (len == -1 && socket_error() == EINTR);
200
201 if (len == -1)
202 len = -socket_error();
203
204 return len;
205}
206
207static int socket_close(void *opaque)
208{
209 QEMUFileSocket *s = opaque;
Anthony Liguori7267c092011-08-20 22:09:37 -0500210 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000211 return 0;
212}
213
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200214static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000215{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200216 QEMUFileStdio *s = opaque;
217 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000218}
219
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200220static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000221{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200222 QEMUFileStdio *s = opaque;
223 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300224 int bytes;
225
226 do {
227 clearerr(fp);
228 bytes = fread(buf, 1, size, fp);
229 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
230 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000231}
232
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200233static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000234{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200235 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500236 int ret;
237 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200238 if (ret == -1) {
239 ret = -errno;
240 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500241 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500242 return ret;
aliguoria672b462008-11-11 21:33:36 +0000243}
244
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200245static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000246{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200247 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200248 int ret = 0;
249 if (fclose(s->stdio_file) == EOF) {
250 ret = -errno;
251 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500252 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200253 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200254}
aliguoria672b462008-11-11 21:33:36 +0000255
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200256QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
257{
258 QEMUFileStdio *s;
259
260 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000261 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
262 return NULL;
263 }
264
Anthony Liguori7267c092011-08-20 22:09:37 -0500265 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000266
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200267 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000268
269 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200270 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
271 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000272 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200273 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
274 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000275 }
aliguoria672b462008-11-11 21:33:36 +0000276 return s->file;
277}
278
279QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
280{
281 FILE *popen_file;
282
283 popen_file = popen(command, mode);
284 if(popen_file == NULL) {
285 return NULL;
286 }
287
288 return qemu_popen(popen_file, mode);
289}
290
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200291int qemu_stdio_fd(QEMUFile *f)
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200292{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200293 QEMUFileStdio *p;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200294 int fd;
295
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200296 p = (QEMUFileStdio *)f->opaque;
297 fd = fileno(p->stdio_file);
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200298
299 return fd;
300}
301
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200302QEMUFile *qemu_fdopen(int fd, const char *mode)
303{
304 QEMUFileStdio *s;
305
306 if (mode == NULL ||
307 (mode[0] != 'r' && mode[0] != 'w') ||
308 mode[1] != 'b' || mode[2] != 0) {
309 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
310 return NULL;
311 }
312
Anthony Liguori7267c092011-08-20 22:09:37 -0500313 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200314 s->stdio_file = fdopen(fd, mode);
315 if (!s->stdio_file)
316 goto fail;
317
318 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200319 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
320 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200321 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200322 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
323 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200324 }
325 return s->file;
326
327fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500328 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200329 return NULL;
330}
331
aliguoria672b462008-11-11 21:33:36 +0000332QEMUFile *qemu_fopen_socket(int fd)
333{
Anthony Liguori7267c092011-08-20 22:09:37 -0500334 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000335
aliguoria672b462008-11-11 21:33:36 +0000336 s->fd = fd;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200337 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
338 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000339 return s->file;
340}
341
aliguoria672b462008-11-11 21:33:36 +0000342static int file_put_buffer(void *opaque, const uint8_t *buf,
343 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);
Kirill A. Shutemov5fdb3aa2009-12-25 18:19:19 +0000347 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000348}
349
350static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
351{
352 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200353 fseek(s->stdio_file, pos, SEEK_SET);
354 return fread(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000355}
356
357QEMUFile *qemu_fopen(const char *filename, const char *mode)
358{
359 QEMUFileStdio *s;
360
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200361 if (mode == NULL ||
362 (mode[0] != 'r' && mode[0] != 'w') ||
363 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000364 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200365 return NULL;
366 }
367
Anthony Liguori7267c092011-08-20 22:09:37 -0500368 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000369
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200370 s->stdio_file = fopen(filename, mode);
371 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000372 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200373
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200374 if(mode[0] == 'w') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200375 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
376 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200377 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200378 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
379 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200380 }
381 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000382fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500383 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000384 return NULL;
385}
386
aliguori178e08a2009-04-05 19:10:55 +0000387static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000388 int64_t pos, int size)
389{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200390 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000391 return size;
392}
393
aliguori178e08a2009-04-05 19:10:55 +0000394static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000395{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200396 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000397}
398
399static int bdrv_fclose(void *opaque)
400{
aliguoria672b462008-11-11 21:33:36 +0000401 return 0;
402}
403
Christoph Hellwig45566e92009-07-10 23:11:57 +0200404static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000405{
aliguoria672b462008-11-11 21:33:36 +0000406 if (is_writable)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200407 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
408 NULL, NULL, NULL);
409 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000410}
411
412QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
413 QEMUFileGetBufferFunc *get_buffer,
414 QEMUFileCloseFunc *close,
Glauber Costa19629532009-05-20 18:26:57 -0400415 QEMUFileRateLimit *rate_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200416 QEMUFileSetRateLimit *set_rate_limit,
417 QEMUFileGetRateLimit *get_rate_limit)
aliguoria672b462008-11-11 21:33:36 +0000418{
419 QEMUFile *f;
420
Anthony Liguori7267c092011-08-20 22:09:37 -0500421 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000422
423 f->opaque = opaque;
424 f->put_buffer = put_buffer;
425 f->get_buffer = get_buffer;
426 f->close = close;
427 f->rate_limit = rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400428 f->set_rate_limit = set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200429 f->get_rate_limit = get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000430 f->is_write = 0;
431
432 return f;
433}
434
Juan Quintela624b9cc2011-10-05 01:02:52 +0200435int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000436{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200437 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000438}
439
Juan Quinteladcd1d222011-09-21 23:01:54 +0200440void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000441{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200442 f->last_error = ret;
aliguori4dabe242009-04-05 19:30:51 +0000443}
444
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200445/** Sets last_error conditionally
446 *
447 * Sets last_error only if ret is negative _and_ no error
448 * was set before.
449 */
450static void qemu_file_set_if_error(QEMUFile *f, int ret)
451{
452 if (ret < 0 && !f->last_error) {
453 qemu_file_set_error(f, ret);
454 }
455}
456
457/** Flushes QEMUFile buffer
458 *
459 * In case of error, last_error is set.
460 */
aliguoria672b462008-11-11 21:33:36 +0000461void qemu_fflush(QEMUFile *f)
462{
463 if (!f->put_buffer)
464 return;
465
466 if (f->is_write && f->buf_index > 0) {
467 int len;
468
469 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
470 if (len > 0)
471 f->buf_offset += f->buf_index;
472 else
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200473 qemu_file_set_error(f, -EINVAL);
aliguoria672b462008-11-11 21:33:36 +0000474 f->buf_index = 0;
475 }
476}
477
478static void qemu_fill_buffer(QEMUFile *f)
479{
480 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200481 int pending;
aliguoria672b462008-11-11 21:33:36 +0000482
483 if (!f->get_buffer)
484 return;
485
486 if (f->is_write)
487 abort();
488
Juan Quintela0046c452011-09-30 19:28:45 +0200489 pending = f->buf_size - f->buf_index;
490 if (pending > 0) {
491 memmove(f->buf, f->buf + f->buf_index, pending);
492 }
493 f->buf_index = 0;
494 f->buf_size = pending;
495
496 len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
497 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000498 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200499 f->buf_size += len;
aliguoria672b462008-11-11 21:33:36 +0000500 f->buf_offset += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200501 } else if (len == 0) {
502 f->last_error = -EIO;
aliguoria672b462008-11-11 21:33:36 +0000503 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200504 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000505}
506
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200507/** Calls close function and set last_error if needed
508 *
509 * Internal function. qemu_fflush() must be called before this.
510 *
511 * Returns f->close() return value, or 0 if close function is not set.
512 */
513static int qemu_close(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000514{
515 int ret = 0;
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200516 if (f->close) {
aliguoria672b462008-11-11 21:33:36 +0000517 ret = f->close(f->opaque);
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200518 qemu_file_set_if_error(f, ret);
519 }
520 return ret;
521}
522
523/** Closes the file
524 *
525 * Returns negative error value if any error happened on previous operations or
526 * while closing the file. Returns 0 or positive number on success.
527 *
528 * The meaning of return value on success depends on the specific backend
529 * being used.
530 */
531int qemu_fclose(QEMUFile *f)
532{
533 int ret;
534 qemu_fflush(f);
535 ret = qemu_close(f);
536 /* If any error was spotted before closing, we should report it
537 * instead of the close() return value.
538 */
539 if (f->last_error) {
540 ret = f->last_error;
541 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500542 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000543 return ret;
544}
545
546void qemu_file_put_notify(QEMUFile *f)
547{
548 f->put_buffer(f->opaque, NULL, 0, 0);
549}
550
551void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
552{
553 int l;
554
Juan Quintela3961b4d2011-10-05 01:05:21 +0200555 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000556 fprintf(stderr,
557 "Attempted to write to buffer while read buffer is not empty\n");
558 abort();
559 }
560
Juan Quintela3961b4d2011-10-05 01:05:21 +0200561 while (!f->last_error && size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000562 l = IO_BUF_SIZE - f->buf_index;
563 if (l > size)
564 l = size;
565 memcpy(f->buf + f->buf_index, buf, l);
566 f->is_write = 1;
567 f->buf_index += l;
568 buf += l;
569 size -= l;
570 if (f->buf_index >= IO_BUF_SIZE)
571 qemu_fflush(f);
572 }
573}
574
575void qemu_put_byte(QEMUFile *f, int v)
576{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200577 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000578 fprintf(stderr,
579 "Attempted to write to buffer while read buffer is not empty\n");
580 abort();
581 }
582
583 f->buf[f->buf_index++] = v;
584 f->is_write = 1;
585 if (f->buf_index >= IO_BUF_SIZE)
586 qemu_fflush(f);
587}
588
Juan Quintelac6380722011-10-04 15:28:31 +0200589static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000590{
Juan Quintelac6380722011-10-04 15:28:31 +0200591 if (f->buf_index + size <= f->buf_size) {
592 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000593 }
aliguoria672b462008-11-11 21:33:36 +0000594}
595
Juan Quintelac6380722011-10-04 15:28:31 +0200596static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200597{
Juan Quintelac6380722011-10-04 15:28:31 +0200598 int pending;
599 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200600
Juan Quintelab9ce1452011-10-04 13:55:32 +0200601 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200602 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200603 }
Juan Quintela811814b2010-07-26 21:38:43 +0200604
Juan Quintelac6380722011-10-04 15:28:31 +0200605 index = f->buf_index + offset;
606 pending = f->buf_size - index;
607 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200608 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200609 index = f->buf_index + offset;
610 pending = f->buf_size - index;
611 }
612
613 if (pending <= 0) {
614 return 0;
615 }
616 if (size > pending) {
617 size = pending;
618 }
619
620 memcpy(buf, f->buf + index, size);
621 return size;
622}
623
624int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
625{
626 int pending = size;
627 int done = 0;
628
629 while (pending > 0) {
630 int res;
631
632 res = qemu_peek_buffer(f, buf, pending, 0);
633 if (res == 0) {
634 return done;
635 }
636 qemu_file_skip(f, res);
637 buf += res;
638 pending -= res;
639 done += res;
640 }
641 return done;
642}
643
644static int qemu_peek_byte(QEMUFile *f, int offset)
645{
646 int index = f->buf_index + offset;
647
648 if (f->is_write) {
649 abort();
650 }
651
652 if (index >= f->buf_size) {
653 qemu_fill_buffer(f);
654 index = f->buf_index + offset;
655 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200656 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200657 }
Juan Quintela811814b2010-07-26 21:38:43 +0200658 }
Juan Quintelac6380722011-10-04 15:28:31 +0200659 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200660}
661
aliguoria672b462008-11-11 21:33:36 +0000662int qemu_get_byte(QEMUFile *f)
663{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200664 int result;
aliguoria672b462008-11-11 21:33:36 +0000665
Juan Quintelac6380722011-10-04 15:28:31 +0200666 result = qemu_peek_byte(f, 0);
667 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200668 return result;
aliguoria672b462008-11-11 21:33:36 +0000669}
670
671int64_t qemu_ftell(QEMUFile *f)
672{
673 return f->buf_offset - f->buf_size + f->buf_index;
674}
675
676int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
677{
678 if (whence == SEEK_SET) {
679 /* nothing to do */
680 } else if (whence == SEEK_CUR) {
681 pos += qemu_ftell(f);
682 } else {
683 /* SEEK_END not supported */
684 return -1;
685 }
686 if (f->put_buffer) {
687 qemu_fflush(f);
688 f->buf_offset = pos;
689 } else {
690 f->buf_offset = pos;
691 f->buf_index = 0;
692 f->buf_size = 0;
693 }
694 return pos;
695}
696
697int qemu_file_rate_limit(QEMUFile *f)
698{
699 if (f->rate_limit)
700 return f->rate_limit(f->opaque);
701
702 return 0;
703}
704
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200705int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200706{
707 if (f->get_rate_limit)
708 return f->get_rate_limit(f->opaque);
709
710 return 0;
711}
712
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200713int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
Glauber Costa19629532009-05-20 18:26:57 -0400714{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400715 /* any failed or completed migration keeps its state to allow probing of
716 * migration data, but has no associated file anymore */
717 if (f && f->set_rate_limit)
Glauber Costa19629532009-05-20 18:26:57 -0400718 return f->set_rate_limit(f->opaque, new_rate);
719
720 return 0;
721}
722
aliguoria672b462008-11-11 21:33:36 +0000723void qemu_put_be16(QEMUFile *f, unsigned int v)
724{
725 qemu_put_byte(f, v >> 8);
726 qemu_put_byte(f, v);
727}
728
729void qemu_put_be32(QEMUFile *f, unsigned int v)
730{
731 qemu_put_byte(f, v >> 24);
732 qemu_put_byte(f, v >> 16);
733 qemu_put_byte(f, v >> 8);
734 qemu_put_byte(f, v);
735}
736
737void qemu_put_be64(QEMUFile *f, uint64_t v)
738{
739 qemu_put_be32(f, v >> 32);
740 qemu_put_be32(f, v);
741}
742
743unsigned int qemu_get_be16(QEMUFile *f)
744{
745 unsigned int v;
746 v = qemu_get_byte(f) << 8;
747 v |= qemu_get_byte(f);
748 return v;
749}
750
751unsigned int qemu_get_be32(QEMUFile *f)
752{
753 unsigned int v;
754 v = qemu_get_byte(f) << 24;
755 v |= qemu_get_byte(f) << 16;
756 v |= qemu_get_byte(f) << 8;
757 v |= qemu_get_byte(f);
758 return v;
759}
760
761uint64_t qemu_get_be64(QEMUFile *f)
762{
763 uint64_t v;
764 v = (uint64_t)qemu_get_be32(f) << 32;
765 v |= qemu_get_be32(f);
766 return v;
767}
768
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200769
770/* timer */
771
772void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
773{
774 uint64_t expire_time;
775
776 expire_time = qemu_timer_expire_time_ns(ts);
777 qemu_put_be64(f, expire_time);
778}
779
780void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
781{
782 uint64_t expire_time;
783
784 expire_time = qemu_get_be64(f);
785 if (expire_time != -1) {
786 qemu_mod_timer_ns(ts, expire_time);
787 } else {
788 qemu_del_timer(ts);
789 }
790}
791
792
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100793/* bool */
794
795static int get_bool(QEMUFile *f, void *pv, size_t size)
796{
797 bool *v = pv;
798 *v = qemu_get_byte(f);
799 return 0;
800}
801
802static void put_bool(QEMUFile *f, void *pv, size_t size)
803{
804 bool *v = pv;
805 qemu_put_byte(f, *v);
806}
807
808const VMStateInfo vmstate_info_bool = {
809 .name = "bool",
810 .get = get_bool,
811 .put = put_bool,
812};
813
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200814/* 8 bit int */
815
816static int get_int8(QEMUFile *f, void *pv, size_t size)
817{
818 int8_t *v = pv;
819 qemu_get_s8s(f, v);
820 return 0;
821}
822
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200823static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200824{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200825 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200826 qemu_put_s8s(f, v);
827}
828
829const VMStateInfo vmstate_info_int8 = {
830 .name = "int8",
831 .get = get_int8,
832 .put = put_int8,
833};
834
835/* 16 bit int */
836
837static int get_int16(QEMUFile *f, void *pv, size_t size)
838{
839 int16_t *v = pv;
840 qemu_get_sbe16s(f, v);
841 return 0;
842}
843
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200844static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200845{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200846 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200847 qemu_put_sbe16s(f, v);
848}
849
850const VMStateInfo vmstate_info_int16 = {
851 .name = "int16",
852 .get = get_int16,
853 .put = put_int16,
854};
855
856/* 32 bit int */
857
858static int get_int32(QEMUFile *f, void *pv, size_t size)
859{
860 int32_t *v = pv;
861 qemu_get_sbe32s(f, v);
862 return 0;
863}
864
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200865static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200866{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200867 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200868 qemu_put_sbe32s(f, v);
869}
870
871const VMStateInfo vmstate_info_int32 = {
872 .name = "int32",
873 .get = get_int32,
874 .put = put_int32,
875};
876
Juan Quintela82501662009-08-20 19:42:32 +0200877/* 32 bit int. See that the received value is the same than the one
878 in the field */
879
880static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
881{
882 int32_t *v = pv;
883 int32_t v2;
884 qemu_get_sbe32s(f, &v2);
885
886 if (*v == v2)
887 return 0;
888 return -EINVAL;
889}
890
891const VMStateInfo vmstate_info_int32_equal = {
892 .name = "int32 equal",
893 .get = get_int32_equal,
894 .put = put_int32,
895};
896
Juan Quintela0a031e02009-08-20 19:42:37 +0200897/* 32 bit int. See that the received value is the less or the same
898 than the one in the field */
899
900static int get_int32_le(QEMUFile *f, void *pv, size_t size)
901{
902 int32_t *old = pv;
903 int32_t new;
904 qemu_get_sbe32s(f, &new);
905
906 if (*old <= new)
907 return 0;
908 return -EINVAL;
909}
910
911const VMStateInfo vmstate_info_int32_le = {
912 .name = "int32 equal",
913 .get = get_int32_le,
914 .put = put_int32,
915};
916
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200917/* 64 bit int */
918
919static int get_int64(QEMUFile *f, void *pv, size_t size)
920{
921 int64_t *v = pv;
922 qemu_get_sbe64s(f, v);
923 return 0;
924}
925
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200926static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200927{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200928 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200929 qemu_put_sbe64s(f, v);
930}
931
932const VMStateInfo vmstate_info_int64 = {
933 .name = "int64",
934 .get = get_int64,
935 .put = put_int64,
936};
937
938/* 8 bit unsigned int */
939
940static int get_uint8(QEMUFile *f, void *pv, size_t size)
941{
942 uint8_t *v = pv;
943 qemu_get_8s(f, v);
944 return 0;
945}
946
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200947static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200948{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200949 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200950 qemu_put_8s(f, v);
951}
952
953const VMStateInfo vmstate_info_uint8 = {
954 .name = "uint8",
955 .get = get_uint8,
956 .put = put_uint8,
957};
958
959/* 16 bit unsigned int */
960
961static int get_uint16(QEMUFile *f, void *pv, size_t size)
962{
963 uint16_t *v = pv;
964 qemu_get_be16s(f, v);
965 return 0;
966}
967
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200968static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200969{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200970 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200971 qemu_put_be16s(f, v);
972}
973
974const VMStateInfo vmstate_info_uint16 = {
975 .name = "uint16",
976 .get = get_uint16,
977 .put = put_uint16,
978};
979
980/* 32 bit unsigned int */
981
982static int get_uint32(QEMUFile *f, void *pv, size_t size)
983{
984 uint32_t *v = pv;
985 qemu_get_be32s(f, v);
986 return 0;
987}
988
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200989static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200990{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200991 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200992 qemu_put_be32s(f, v);
993}
994
995const VMStateInfo vmstate_info_uint32 = {
996 .name = "uint32",
997 .get = get_uint32,
998 .put = put_uint32,
999};
1000
Juan Quintela9122a8f2011-03-10 12:33:48 +01001001/* 32 bit uint. See that the received value is the same than the one
1002 in the field */
1003
1004static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1005{
1006 uint32_t *v = pv;
1007 uint32_t v2;
1008 qemu_get_be32s(f, &v2);
1009
1010 if (*v == v2) {
1011 return 0;
1012 }
1013 return -EINVAL;
1014}
1015
1016const VMStateInfo vmstate_info_uint32_equal = {
1017 .name = "uint32 equal",
1018 .get = get_uint32_equal,
1019 .put = put_uint32,
1020};
1021
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001022/* 64 bit unsigned int */
1023
1024static int get_uint64(QEMUFile *f, void *pv, size_t size)
1025{
1026 uint64_t *v = pv;
1027 qemu_get_be64s(f, v);
1028 return 0;
1029}
1030
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001031static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001032{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001033 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001034 qemu_put_be64s(f, v);
1035}
1036
1037const VMStateInfo vmstate_info_uint64 = {
1038 .name = "uint64",
1039 .get = get_uint64,
1040 .put = put_uint64,
1041};
1042
Juan Quintela80cd83e2009-09-10 03:04:36 +02001043/* 8 bit int. See that the received value is the same than the one
1044 in the field */
1045
1046static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1047{
1048 uint8_t *v = pv;
1049 uint8_t v2;
1050 qemu_get_8s(f, &v2);
1051
1052 if (*v == v2)
1053 return 0;
1054 return -EINVAL;
1055}
1056
1057const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001058 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001059 .get = get_uint8_equal,
1060 .put = put_uint8,
1061};
1062
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001063/* 16 bit unsigned int int. See that the received value is the same than the one
1064 in the field */
1065
1066static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1067{
1068 uint16_t *v = pv;
1069 uint16_t v2;
1070 qemu_get_be16s(f, &v2);
1071
1072 if (*v == v2)
1073 return 0;
1074 return -EINVAL;
1075}
1076
1077const VMStateInfo vmstate_info_uint16_equal = {
1078 .name = "uint16 equal",
1079 .get = get_uint16_equal,
1080 .put = put_uint16,
1081};
1082
Juan Quinteladde04632009-08-20 19:42:26 +02001083/* timers */
1084
1085static int get_timer(QEMUFile *f, void *pv, size_t size)
1086{
1087 QEMUTimer *v = pv;
1088 qemu_get_timer(f, v);
1089 return 0;
1090}
1091
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001092static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001093{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001094 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001095 qemu_put_timer(f, v);
1096}
1097
1098const VMStateInfo vmstate_info_timer = {
1099 .name = "timer",
1100 .get = get_timer,
1101 .put = put_timer,
1102};
1103
Juan Quintela6f67c502009-08-20 19:42:35 +02001104/* uint8_t buffers */
1105
1106static int get_buffer(QEMUFile *f, void *pv, size_t size)
1107{
1108 uint8_t *v = pv;
1109 qemu_get_buffer(f, v, size);
1110 return 0;
1111}
1112
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001113static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001114{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001115 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001116 qemu_put_buffer(f, v, size);
1117}
1118
1119const VMStateInfo vmstate_info_buffer = {
1120 .name = "buffer",
1121 .get = get_buffer,
1122 .put = put_buffer,
1123};
1124
Juan Quintela76507c72009-10-19 15:46:28 +02001125/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001126 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001127
1128static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1129{
Jan Kiszka21174c32009-12-02 12:36:35 +01001130 uint8_t buf[1024];
1131 int block_len;
1132
1133 while (size > 0) {
1134 block_len = MIN(sizeof(buf), size);
1135 size -= block_len;
1136 qemu_get_buffer(f, buf, block_len);
1137 }
1138 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001139}
1140
1141static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1142{
Jan Kiszka21174c32009-12-02 12:36:35 +01001143 static const uint8_t buf[1024];
1144 int block_len;
1145
1146 while (size > 0) {
1147 block_len = MIN(sizeof(buf), size);
1148 size -= block_len;
1149 qemu_put_buffer(f, buf, block_len);
1150 }
Juan Quintela76507c72009-10-19 15:46:28 +02001151}
1152
1153const VMStateInfo vmstate_info_unused_buffer = {
1154 .name = "unused_buffer",
1155 .get = get_unused_buffer,
1156 .put = put_unused_buffer,
1157};
1158
Alex Williamson7685ee62010-06-25 11:09:14 -06001159typedef struct CompatEntry {
1160 char idstr[256];
1161 int instance_id;
1162} CompatEntry;
1163
aliguoria672b462008-11-11 21:33:36 +00001164typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001165 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001166 char idstr[256];
1167 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001168 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001169 int version_id;
1170 int section_id;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001171 SaveSetParamsHandler *set_params;
aliguoria672b462008-11-11 21:33:36 +00001172 SaveLiveStateHandler *save_live_state;
1173 SaveStateHandler *save_state;
1174 LoadStateHandler *load_state;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001175 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001176 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001177 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001178 int no_migrate;
aliguoria672b462008-11-11 21:33:36 +00001179} SaveStateEntry;
1180
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001181
Blue Swirl72cf2d42009-09-12 07:36:22 +00001182static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1183 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001184static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001185
Juan Quintela8718e992009-09-01 02:12:31 +02001186static int calculate_new_instance_id(const char *idstr)
1187{
1188 SaveStateEntry *se;
1189 int instance_id = 0;
1190
Blue Swirl72cf2d42009-09-12 07:36:22 +00001191 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001192 if (strcmp(idstr, se->idstr) == 0
1193 && instance_id <= se->instance_id) {
1194 instance_id = se->instance_id + 1;
1195 }
1196 }
1197 return instance_id;
1198}
1199
Alex Williamson7685ee62010-06-25 11:09:14 -06001200static int calculate_compat_instance_id(const char *idstr)
1201{
1202 SaveStateEntry *se;
1203 int instance_id = 0;
1204
1205 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1206 if (!se->compat)
1207 continue;
1208
1209 if (strcmp(idstr, se->compat->idstr) == 0
1210 && instance_id <= se->compat->instance_id) {
1211 instance_id = se->compat->instance_id + 1;
1212 }
1213 }
1214 return instance_id;
1215}
1216
aliguoria672b462008-11-11 21:33:36 +00001217/* TODO: Individual devices generally have very little idea about the rest
1218 of the system, so instance_id should be removed/replaced.
1219 Meanwhile pass -1 as instance_id if you do not already have a clearly
1220 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001221int register_savevm_live(DeviceState *dev,
1222 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001223 int instance_id,
1224 int version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001225 SaveSetParamsHandler *set_params,
aliguoria672b462008-11-11 21:33:36 +00001226 SaveLiveStateHandler *save_live_state,
1227 SaveStateHandler *save_state,
1228 LoadStateHandler *load_state,
1229 void *opaque)
1230{
Juan Quintela8718e992009-09-01 02:12:31 +02001231 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001232
Anthony Liguori7267c092011-08-20 22:09:37 -05001233 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001234 se->version_id = version_id;
1235 se->section_id = global_section_id++;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001236 se->set_params = set_params;
aliguoria672b462008-11-11 21:33:36 +00001237 se->save_live_state = save_live_state;
1238 se->save_state = save_state;
1239 se->load_state = load_state;
1240 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001241 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001242 se->no_migrate = 0;
aliguoria672b462008-11-11 21:33:36 +00001243
Alex Williamson7685ee62010-06-25 11:09:14 -06001244 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1245 char *id = dev->parent_bus->info->get_dev_path(dev);
1246 if (id) {
1247 pstrcpy(se->idstr, sizeof(se->idstr), id);
1248 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001249 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001250
Anthony Liguori7267c092011-08-20 22:09:37 -05001251 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001252 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1253 se->compat->instance_id = instance_id == -1 ?
1254 calculate_compat_instance_id(idstr) : instance_id;
1255 instance_id = -1;
1256 }
1257 }
1258 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1259
Juan Quintela8718e992009-09-01 02:12:31 +02001260 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001261 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001262 } else {
1263 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001264 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001265 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001266 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001267 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001268 return 0;
1269}
1270
Alex Williamson0be71e32010-06-25 11:09:07 -06001271int register_savevm(DeviceState *dev,
1272 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001273 int instance_id,
1274 int version_id,
1275 SaveStateHandler *save_state,
1276 LoadStateHandler *load_state,
1277 void *opaque)
1278{
Alex Williamson0be71e32010-06-25 11:09:07 -06001279 return register_savevm_live(dev, idstr, instance_id, version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001280 NULL, NULL, save_state, load_state, opaque);
aliguoria672b462008-11-11 21:33:36 +00001281}
1282
Alex Williamson0be71e32010-06-25 11:09:07 -06001283void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001284{
Juan Quintela8718e992009-09-01 02:12:31 +02001285 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001286 char id[256] = "";
1287
1288 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1289 char *path = dev->parent_bus->info->get_dev_path(dev);
1290 if (path) {
1291 pstrcpy(id, sizeof(id), path);
1292 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001293 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001294 }
1295 }
1296 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001297
Blue Swirl72cf2d42009-09-12 07:36:22 +00001298 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001299 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001300 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001301 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001302 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001303 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001304 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001305 }
aliguori41bd13a2009-04-17 17:10:59 +00001306 }
1307}
1308
Alex Williamson0be71e32010-06-25 11:09:07 -06001309int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001310 const VMStateDescription *vmsd,
1311 void *opaque, int alias_id,
1312 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001313{
Juan Quintela8718e992009-09-01 02:12:31 +02001314 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001315
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001316 /* If this triggers, alias support can be dropped for the vmsd. */
1317 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1318
Anthony Liguori7267c092011-08-20 22:09:37 -05001319 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001320 se->version_id = vmsd->version_id;
1321 se->section_id = global_section_id++;
1322 se->save_live_state = NULL;
1323 se->save_state = NULL;
1324 se->load_state = NULL;
1325 se->opaque = opaque;
1326 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001327 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001328 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001329
Alex Williamson7685ee62010-06-25 11:09:14 -06001330 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1331 char *id = dev->parent_bus->info->get_dev_path(dev);
1332 if (id) {
1333 pstrcpy(se->idstr, sizeof(se->idstr), id);
1334 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001335 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001336
Anthony Liguori7267c092011-08-20 22:09:37 -05001337 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001338 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1339 se->compat->instance_id = instance_id == -1 ?
1340 calculate_compat_instance_id(vmsd->name) : instance_id;
1341 instance_id = -1;
1342 }
1343 }
1344 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1345
Juan Quintela8718e992009-09-01 02:12:31 +02001346 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001347 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001348 } else {
1349 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001350 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001351 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001352 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001353 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001354 return 0;
1355}
1356
Alex Williamson0be71e32010-06-25 11:09:07 -06001357int vmstate_register(DeviceState *dev, int instance_id,
1358 const VMStateDescription *vmsd, void *opaque)
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001359{
Alex Williamson0be71e32010-06-25 11:09:07 -06001360 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1361 opaque, -1, 0);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001362}
1363
Alex Williamson0be71e32010-06-25 11:09:07 -06001364void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1365 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001366{
Juan Quintela1eb75382009-09-10 03:04:29 +02001367 SaveStateEntry *se, *new_se;
1368
Blue Swirl72cf2d42009-09-12 07:36:22 +00001369 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001370 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001371 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001372 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001373 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001374 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001375 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001376 }
1377 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001378}
1379
Juan Quintela811814b2010-07-26 21:38:43 +02001380static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1381 void *opaque);
1382static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1383 void *opaque);
1384
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001385int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1386 void *opaque, int version_id)
1387{
1388 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001389 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001390
1391 if (version_id > vmsd->version_id) {
1392 return -EINVAL;
1393 }
1394 if (version_id < vmsd->minimum_version_id_old) {
1395 return -EINVAL;
1396 }
1397 if (version_id < vmsd->minimum_version_id) {
1398 return vmsd->load_state_old(f, opaque, version_id);
1399 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001400 if (vmsd->pre_load) {
1401 int ret = vmsd->pre_load(opaque);
1402 if (ret)
1403 return ret;
1404 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001405 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001406 if ((field->field_exists &&
1407 field->field_exists(opaque, version_id)) ||
1408 (!field->field_exists &&
1409 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001410 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001411 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001412 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001413
Juan Quintelae61a1e02009-12-02 12:36:38 +01001414 if (field->flags & VMS_VBUFFER) {
1415 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001416 if (field->flags & VMS_MULTIPLY) {
1417 size *= field->size;
1418 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001419 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001420 if (field->flags & VMS_ARRAY) {
1421 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001422 } else if (field->flags & VMS_VARRAY_INT32) {
1423 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001424 } else if (field->flags & VMS_VARRAY_UINT32) {
1425 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001426 } else if (field->flags & VMS_VARRAY_UINT16) {
1427 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001428 } else if (field->flags & VMS_VARRAY_UINT8) {
1429 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001430 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001431 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001432 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001433 }
1434 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001435 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001436
Juan Quintela19df4382009-09-29 22:48:41 +02001437 if (field->flags & VMS_ARRAY_OF_POINTER) {
1438 addr = *(void **)addr;
1439 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001440 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001441 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001442 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001443 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001444
1445 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001446 if (ret < 0) {
1447 return ret;
1448 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001449 }
1450 }
1451 field++;
1452 }
Juan Quintela811814b2010-07-26 21:38:43 +02001453 ret = vmstate_subsection_load(f, vmsd, opaque);
1454 if (ret != 0) {
1455 return ret;
1456 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001457 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001458 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001459 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001460 return 0;
1461}
1462
1463void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001464 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001465{
1466 VMStateField *field = vmsd->fields;
1467
Juan Quintela8fb07912009-09-10 03:04:32 +02001468 if (vmsd->pre_save) {
1469 vmsd->pre_save(opaque);
1470 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001471 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001472 if (!field->field_exists ||
1473 field->field_exists(opaque, vmsd->version_id)) {
1474 void *base_addr = opaque + field->offset;
1475 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001476 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001477
Juan Quintelae61a1e02009-12-02 12:36:38 +01001478 if (field->flags & VMS_VBUFFER) {
1479 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001480 if (field->flags & VMS_MULTIPLY) {
1481 size *= field->size;
1482 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001483 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001484 if (field->flags & VMS_ARRAY) {
1485 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001486 } else if (field->flags & VMS_VARRAY_INT32) {
1487 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001488 } else if (field->flags & VMS_VARRAY_UINT16) {
1489 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001490 } else if (field->flags & VMS_VARRAY_UINT8) {
1491 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001492 }
1493 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001494 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001495 }
1496 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001497 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001498
Juan Quintela85953872009-12-02 12:36:37 +01001499 if (field->flags & VMS_ARRAY_OF_POINTER) {
1500 addr = *(void **)addr;
1501 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001502 if (field->flags & VMS_STRUCT) {
1503 vmstate_save_state(f, field->vmsd, addr);
1504 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001505 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001506 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001507 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001508 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001509 field++;
1510 }
Juan Quintela811814b2010-07-26 21:38:43 +02001511 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001512}
1513
Juan Quintela4082be42009-08-20 19:42:24 +02001514static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1515{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001516 if (!se->vmsd) { /* Old style */
1517 return se->load_state(f, se->opaque, version_id);
1518 }
1519 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001520}
1521
Alex Williamsondc912122011-01-11 14:39:43 -07001522static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001523{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001524 if (!se->vmsd) { /* Old style */
1525 se->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001526 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001527 }
1528 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001529}
1530
aliguoria672b462008-11-11 21:33:36 +00001531#define QEMU_VM_FILE_MAGIC 0x5145564d
1532#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1533#define QEMU_VM_FILE_VERSION 0x00000003
1534
1535#define QEMU_VM_EOF 0x00
1536#define QEMU_VM_SECTION_START 0x01
1537#define QEMU_VM_SECTION_PART 0x02
1538#define QEMU_VM_SECTION_END 0x03
1539#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001540#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001541
Alex Williamsondc912122011-01-11 14:39:43 -07001542bool qemu_savevm_state_blocked(Monitor *mon)
1543{
1544 SaveStateEntry *se;
1545
1546 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1547 if (se->no_migrate) {
1548 monitor_printf(mon, "state blocked by non-migratable device '%s'\n",
1549 se->idstr);
1550 return true;
1551 }
1552 }
1553 return false;
1554}
1555
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001556int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
1557 int shared)
aliguoria672b462008-11-11 21:33:36 +00001558{
1559 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001560 int ret;
aliguoria672b462008-11-11 21:33:36 +00001561
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001562 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1563 if(se->set_params == NULL) {
1564 continue;
1565 }
1566 se->set_params(blk_enable, shared, se->opaque);
1567 }
1568
aliguoria672b462008-11-11 21:33:36 +00001569 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1570 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1571
Blue Swirl72cf2d42009-09-12 07:36:22 +00001572 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001573 int len;
1574
1575 if (se->save_live_state == NULL)
1576 continue;
1577
1578 /* Section type */
1579 qemu_put_byte(f, QEMU_VM_SECTION_START);
1580 qemu_put_be32(f, se->section_id);
1581
1582 /* ID string */
1583 len = strlen(se->idstr);
1584 qemu_put_byte(f, len);
1585 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1586
1587 qemu_put_be32(f, se->instance_id);
1588 qemu_put_be32(f, se->version_id);
1589
Juan Quintela29757252011-10-19 15:22:18 +02001590 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque);
1591 if (ret < 0) {
1592 qemu_savevm_state_cancel(mon, f);
1593 return ret;
1594 }
aliguoria672b462008-11-11 21:33:36 +00001595 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001596 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001597 if (ret != 0) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001598 qemu_savevm_state_cancel(mon, f);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001599 }
aliguoria672b462008-11-11 21:33:36 +00001600
Juan Quintela39346382011-09-22 11:02:14 +02001601 return ret;
1602
aliguoria672b462008-11-11 21:33:36 +00001603}
1604
Juan Quintela39346382011-09-22 11:02:14 +02001605/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001606 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001607 * negative: there was one error, and we have -errno.
1608 * 0 : We haven't finished, caller have to go again
1609 * 1 : We have finished, we can go to complete phase
1610 */
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001611int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001612{
1613 SaveStateEntry *se;
1614 int ret = 1;
1615
Blue Swirl72cf2d42009-09-12 07:36:22 +00001616 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001617 if (se->save_live_state == NULL)
1618 continue;
1619
1620 /* Section type */
1621 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1622 qemu_put_be32(f, se->section_id);
1623
Jan Kiszka90697be2009-12-01 15:19:55 +01001624 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001625 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001626 /* Do not proceed to the next vmstate before this one reported
1627 completion of the current stage. This serializes the migration
1628 and reduces the probability that a faster changing state is
1629 synchronized over and over again. */
1630 break;
1631 }
aliguoria672b462008-11-11 21:33:36 +00001632 }
Juan Quintela39346382011-09-22 11:02:14 +02001633 if (ret != 0) {
1634 return ret;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001635 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001636 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001637 if (ret != 0) {
1638 qemu_savevm_state_cancel(mon, f);
1639 }
1640 return ret;
aliguoria672b462008-11-11 21:33:36 +00001641}
1642
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001643int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001644{
1645 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001646 int ret;
aliguoria672b462008-11-11 21:33:36 +00001647
Jan Kiszkaea375f92010-03-01 19:10:30 +01001648 cpu_synchronize_all_states();
1649
Blue Swirl72cf2d42009-09-12 07:36:22 +00001650 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001651 if (se->save_live_state == NULL)
1652 continue;
1653
1654 /* Section type */
1655 qemu_put_byte(f, QEMU_VM_SECTION_END);
1656 qemu_put_be32(f, se->section_id);
1657
Juan Quintela29757252011-10-19 15:22:18 +02001658 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque);
1659 if (ret < 0) {
1660 return ret;
1661 }
aliguoria672b462008-11-11 21:33:36 +00001662 }
1663
Blue Swirl72cf2d42009-09-12 07:36:22 +00001664 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001665 int len;
1666
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001667 if (se->save_state == NULL && se->vmsd == NULL)
aliguoria672b462008-11-11 21:33:36 +00001668 continue;
1669
1670 /* Section type */
1671 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1672 qemu_put_be32(f, se->section_id);
1673
1674 /* ID string */
1675 len = strlen(se->idstr);
1676 qemu_put_byte(f, len);
1677 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1678
1679 qemu_put_be32(f, se->instance_id);
1680 qemu_put_be32(f, se->version_id);
1681
Alex Williamsondc912122011-01-11 14:39:43 -07001682 vmstate_save(f, se);
aliguoria672b462008-11-11 21:33:36 +00001683 }
1684
1685 qemu_put_byte(f, QEMU_VM_EOF);
1686
Juan Quintela624b9cc2011-10-05 01:02:52 +02001687 return qemu_file_get_error(f);
aliguoria672b462008-11-11 21:33:36 +00001688}
1689
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001690void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001691{
1692 SaveStateEntry *se;
1693
1694 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1695 if (se->save_live_state) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001696 se->save_live_state(mon, f, -1, se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001697 }
1698 }
1699}
1700
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001701static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001702{
aliguoria672b462008-11-11 21:33:36 +00001703 int ret;
1704
Alex Williamsondc912122011-01-11 14:39:43 -07001705 if (qemu_savevm_state_blocked(mon)) {
1706 ret = -EINVAL;
1707 goto out;
1708 }
1709
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001710 ret = qemu_savevm_state_begin(mon, f, 0, 0);
aliguoria672b462008-11-11 21:33:36 +00001711 if (ret < 0)
1712 goto out;
1713
1714 do {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001715 ret = qemu_savevm_state_iterate(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001716 if (ret < 0)
1717 goto out;
1718 } while (ret == 0);
1719
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001720 ret = qemu_savevm_state_complete(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001721
1722out:
Juan Quintela39346382011-09-22 11:02:14 +02001723 if (ret == 0) {
Juan Quintela624b9cc2011-10-05 01:02:52 +02001724 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001725 }
aliguoria672b462008-11-11 21:33:36 +00001726
aliguoria672b462008-11-11 21:33:36 +00001727 return ret;
1728}
1729
1730static SaveStateEntry *find_se(const char *idstr, int instance_id)
1731{
1732 SaveStateEntry *se;
1733
Blue Swirl72cf2d42009-09-12 07:36:22 +00001734 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001735 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001736 (instance_id == se->instance_id ||
1737 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001738 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001739 /* Migrating from an older version? */
1740 if (strstr(se->idstr, idstr) && se->compat) {
1741 if (!strcmp(se->compat->idstr, idstr) &&
1742 (instance_id == se->compat->instance_id ||
1743 instance_id == se->alias_id))
1744 return se;
1745 }
aliguoria672b462008-11-11 21:33:36 +00001746 }
1747 return NULL;
1748}
1749
Juan Quintela811814b2010-07-26 21:38:43 +02001750static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1751{
1752 while(sub && sub->needed) {
1753 if (strcmp(idstr, sub->vmsd->name) == 0) {
1754 return sub->vmsd;
1755 }
1756 sub++;
1757 }
1758 return NULL;
1759}
1760
1761static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1762 void *opaque)
1763{
Juan Quintelac6380722011-10-04 15:28:31 +02001764 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001765 char idstr[256];
1766 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001767 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001768 const VMStateDescription *sub_vmsd;
1769
Juan Quintelac6380722011-10-04 15:28:31 +02001770 len = qemu_peek_byte(f, 1);
1771 if (len < strlen(vmsd->name) + 1) {
1772 /* subsection name has be be "section_name/a" */
1773 return 0;
1774 }
1775 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1776 if (size != len) {
1777 return 0;
1778 }
1779 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02001780
Juan Quintelac6380722011-10-04 15:28:31 +02001781 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1782 /* it don't have a valid subsection name */
1783 return 0;
1784 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02001785 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02001786 if (sub_vmsd == NULL) {
1787 return -ENOENT;
1788 }
Juan Quintelac6380722011-10-04 15:28:31 +02001789 qemu_file_skip(f, 1); /* subsection */
1790 qemu_file_skip(f, 1); /* len */
1791 qemu_file_skip(f, len); /* idstr */
1792 version_id = qemu_get_be32(f);
1793
Juan Quintela811814b2010-07-26 21:38:43 +02001794 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1795 if (ret) {
1796 return ret;
1797 }
1798 }
1799 return 0;
1800}
1801
1802static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1803 void *opaque)
1804{
1805 const VMStateSubsection *sub = vmsd->subsections;
1806
1807 while (sub && sub->needed) {
1808 if (sub->needed(opaque)) {
1809 const VMStateDescription *vmsd = sub->vmsd;
1810 uint8_t len;
1811
1812 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1813 len = strlen(vmsd->name);
1814 qemu_put_byte(f, len);
1815 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1816 qemu_put_be32(f, vmsd->version_id);
1817 vmstate_save_state(f, vmsd, opaque);
1818 }
1819 sub++;
1820 }
1821}
1822
aliguoria672b462008-11-11 21:33:36 +00001823typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001824 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001825 SaveStateEntry *se;
1826 int section_id;
1827 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001828} LoadStateEntry;
1829
aliguoria672b462008-11-11 21:33:36 +00001830int qemu_loadvm_state(QEMUFile *f)
1831{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001832 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1833 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001834 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001835 uint8_t section_type;
1836 unsigned int v;
1837 int ret;
1838
Alex Williamsondc912122011-01-11 14:39:43 -07001839 if (qemu_savevm_state_blocked(default_mon)) {
1840 return -EINVAL;
1841 }
1842
aliguoria672b462008-11-11 21:33:36 +00001843 v = qemu_get_be32(f);
1844 if (v != QEMU_VM_FILE_MAGIC)
1845 return -EINVAL;
1846
1847 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001848 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1849 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1850 return -ENOTSUP;
1851 }
aliguoria672b462008-11-11 21:33:36 +00001852 if (v != QEMU_VM_FILE_VERSION)
1853 return -ENOTSUP;
1854
1855 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1856 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001857 SaveStateEntry *se;
1858 char idstr[257];
1859 int len;
1860
1861 switch (section_type) {
1862 case QEMU_VM_SECTION_START:
1863 case QEMU_VM_SECTION_FULL:
1864 /* Read section start */
1865 section_id = qemu_get_be32(f);
1866 len = qemu_get_byte(f);
1867 qemu_get_buffer(f, (uint8_t *)idstr, len);
1868 idstr[len] = 0;
1869 instance_id = qemu_get_be32(f);
1870 version_id = qemu_get_be32(f);
1871
1872 /* Find savevm section */
1873 se = find_se(idstr, instance_id);
1874 if (se == NULL) {
1875 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1876 ret = -EINVAL;
1877 goto out;
1878 }
1879
1880 /* Validate version */
1881 if (version_id > se->version_id) {
1882 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1883 version_id, idstr, se->version_id);
1884 ret = -EINVAL;
1885 goto out;
1886 }
1887
1888 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05001889 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00001890
1891 le->se = se;
1892 le->section_id = section_id;
1893 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001894 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00001895
Juan Quintela4082be42009-08-20 19:42:24 +02001896 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001897 if (ret < 0) {
1898 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1899 instance_id, idstr);
1900 goto out;
1901 }
aliguoria672b462008-11-11 21:33:36 +00001902 break;
1903 case QEMU_VM_SECTION_PART:
1904 case QEMU_VM_SECTION_END:
1905 section_id = qemu_get_be32(f);
1906
Blue Swirl72cf2d42009-09-12 07:36:22 +00001907 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001908 if (le->section_id == section_id) {
1909 break;
1910 }
1911 }
aliguoria672b462008-11-11 21:33:36 +00001912 if (le == NULL) {
1913 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1914 ret = -EINVAL;
1915 goto out;
1916 }
1917
Juan Quintela4082be42009-08-20 19:42:24 +02001918 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001919 if (ret < 0) {
1920 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1921 section_id);
1922 goto out;
1923 }
aliguoria672b462008-11-11 21:33:36 +00001924 break;
1925 default:
1926 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1927 ret = -EINVAL;
1928 goto out;
1929 }
1930 }
1931
Jan Kiszkaea375f92010-03-01 19:10:30 +01001932 cpu_synchronize_all_post_init();
1933
aliguoria672b462008-11-11 21:33:36 +00001934 ret = 0;
1935
1936out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00001937 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1938 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001939 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00001940 }
1941
Juan Quintela42802d42011-10-05 01:14:46 +02001942 if (ret == 0) {
1943 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001944 }
aliguoria672b462008-11-11 21:33:36 +00001945
1946 return ret;
1947}
1948
aliguoria672b462008-11-11 21:33:36 +00001949static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1950 const char *name)
1951{
1952 QEMUSnapshotInfo *sn_tab, *sn;
1953 int nb_sns, i, ret;
1954
1955 ret = -ENOENT;
1956 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1957 if (nb_sns < 0)
1958 return ret;
1959 for(i = 0; i < nb_sns; i++) {
1960 sn = &sn_tab[i];
1961 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1962 *sn_info = *sn;
1963 ret = 0;
1964 break;
1965 }
1966 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001967 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00001968 return ret;
1969}
1970
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001971/*
1972 * Deletes snapshots of a given name in all opened images.
1973 */
1974static int del_existing_snapshots(Monitor *mon, const char *name)
1975{
1976 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001977 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1978 int ret;
1979
Markus Armbrusterdbc13592010-06-02 18:55:21 +02001980 bs = NULL;
1981 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001982 if (bdrv_can_snapshot(bs) &&
1983 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1984 {
1985 ret = bdrv_snapshot_delete(bs, name);
1986 if (ret < 0) {
1987 monitor_printf(mon,
1988 "Error while deleting snapshot on '%s'\n",
1989 bdrv_get_device_name(bs));
1990 return -1;
1991 }
1992 }
1993 }
1994
1995 return 0;
1996}
1997
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001998void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001999{
2000 BlockDriverState *bs, *bs1;
2001 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002002 int ret;
aliguoria672b462008-11-11 21:33:36 +00002003 QEMUFile *f;
2004 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002005 uint64_t vm_state_size;
aliguoria672b462008-11-11 21:33:36 +00002006#ifdef _WIN32
2007 struct _timeb tb;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002008 struct tm *ptm;
aliguoria672b462008-11-11 21:33:36 +00002009#else
2010 struct timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002011 struct tm tm;
aliguoria672b462008-11-11 21:33:36 +00002012#endif
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002013 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002014
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002015 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002016 bs = NULL;
2017 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002018
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002019 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002020 continue;
2021 }
2022
2023 if (!bdrv_can_snapshot(bs)) {
2024 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2025 bdrv_get_device_name(bs));
2026 return;
2027 }
2028 }
2029
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002030 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002031 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002032 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002033 return;
2034 }
aliguoria672b462008-11-11 21:33:36 +00002035
Luiz Capitulino13548692011-07-29 15:36:43 -03002036 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002037 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002038
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002039 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002040
2041 /* fill auxiliary fields */
2042#ifdef _WIN32
2043 _ftime(&tb);
2044 sn->date_sec = tb.time;
2045 sn->date_nsec = tb.millitm * 1000000;
2046#else
2047 gettimeofday(&tv, NULL);
2048 sn->date_sec = tv.tv_sec;
2049 sn->date_nsec = tv.tv_usec * 1000;
2050#endif
Paolo Bonzini74475452011-03-11 16:47:48 +01002051 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002052
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002053 if (name) {
2054 ret = bdrv_snapshot_find(bs, old_sn, name);
2055 if (ret >= 0) {
2056 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2057 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2058 } else {
2059 pstrcpy(sn->name, sizeof(sn->name), name);
2060 }
2061 } else {
2062#ifdef _WIN32
2063 ptm = localtime(&tb.time);
2064 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2065#else
Blue Swirld7d9b522010-09-09 19:13:04 +00002066 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2067 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002068 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2069#endif
2070 }
2071
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002072 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002073 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002074 goto the_end;
2075 }
2076
aliguoria672b462008-11-11 21:33:36 +00002077 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002078 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002079 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002080 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002081 goto the_end;
2082 }
Jan Kiszkaf327aa02009-11-30 18:21:21 +01002083 ret = qemu_savevm_state(mon, f);
aliguori2d22b182008-12-11 21:06:49 +00002084 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002085 qemu_fclose(f);
2086 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002087 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002088 goto the_end;
2089 }
2090
2091 /* create the snapshots */
2092
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002093 bs1 = NULL;
2094 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002095 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002096 /* Write VM state size only to the image that contains the state */
2097 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002098 ret = bdrv_snapshot_create(bs1, sn);
2099 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002100 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2101 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002102 }
2103 }
2104 }
2105
2106 the_end:
2107 if (saved_vm_running)
2108 vm_start();
2109}
2110
Markus Armbruster03cd4652010-02-17 16:24:10 +01002111int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002112{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002113 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002114 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002115 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002116 int ret;
aliguoria672b462008-11-11 21:33:36 +00002117
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002118 bs_vm_state = bdrv_snapshots();
2119 if (!bs_vm_state) {
2120 error_report("No block device supports snapshots");
2121 return -ENOTSUP;
2122 }
2123
2124 /* Don't even try to load empty VM states */
2125 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2126 if (ret < 0) {
2127 return ret;
2128 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002129 error_report("This is a disk-only snapshot. Revert to it offline "
2130 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002131 return -EINVAL;
2132 }
2133
2134 /* Verify if there is any device that doesn't support snapshots and is
2135 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002136 bs = NULL;
2137 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002138
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002139 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002140 continue;
2141 }
2142
2143 if (!bdrv_can_snapshot(bs)) {
2144 error_report("Device '%s' is writable but does not support snapshots.",
2145 bdrv_get_device_name(bs));
2146 return -ENOTSUP;
2147 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002148
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002149 ret = bdrv_snapshot_find(bs, &sn, name);
2150 if (ret < 0) {
2151 error_report("Device '%s' does not have the requested snapshot '%s'",
2152 bdrv_get_device_name(bs), name);
2153 return ret;
2154 }
aliguoria672b462008-11-11 21:33:36 +00002155 }
2156
2157 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002158 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002159
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002160 bs = NULL;
2161 while ((bs = bdrv_next(bs))) {
2162 if (bdrv_can_snapshot(bs)) {
2163 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002164 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002165 error_report("Error %d while activating snapshot '%s' on '%s'",
2166 ret, name, bdrv_get_device_name(bs));
2167 return ret;
aliguoria672b462008-11-11 21:33:36 +00002168 }
2169 }
2170 }
2171
aliguoria672b462008-11-11 21:33:36 +00002172 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002173 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002174 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002175 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002176 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002177 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002178
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002179 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002180 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002181
aliguoria672b462008-11-11 21:33:36 +00002182 qemu_fclose(f);
2183 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002184 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002185 return ret;
aliguoria672b462008-11-11 21:33:36 +00002186 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002187
Juan Quintela05f24012009-08-20 19:42:22 +02002188 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002189}
2190
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002191void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002192{
2193 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002194 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002195 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002196
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002197 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002198 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002199 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002200 return;
2201 }
2202
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002203 bs1 = NULL;
2204 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002205 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002206 ret = bdrv_snapshot_delete(bs1, name);
2207 if (ret < 0) {
2208 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002209 monitor_printf(mon,
2210 "Snapshots not supported on device '%s'\n",
2211 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002212 else
aliguori376253e2009-03-05 23:01:23 +00002213 monitor_printf(mon, "Error %d while deleting snapshot on "
2214 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002215 }
2216 }
2217 }
2218}
2219
aliguori376253e2009-03-05 23:01:23 +00002220void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00002221{
2222 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002223 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2224 int nb_sns, i, ret, available;
2225 int total;
2226 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002227 char buf[256];
2228
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002229 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002230 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002231 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002232 return;
2233 }
aliguoria672b462008-11-11 21:33:36 +00002234
2235 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2236 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002237 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002238 return;
2239 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002240
2241 if (nb_sns == 0) {
2242 monitor_printf(mon, "There is no snapshot available.\n");
2243 return;
aliguoria672b462008-11-11 21:33:36 +00002244 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002245
Anthony Liguori7267c092011-08-20 22:09:37 -05002246 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002247 total = 0;
2248 for (i = 0; i < nb_sns; i++) {
2249 sn = &sn_tab[i];
2250 available = 1;
2251 bs1 = NULL;
2252
2253 while ((bs1 = bdrv_next(bs1))) {
2254 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2255 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2256 if (ret < 0) {
2257 available = 0;
2258 break;
2259 }
2260 }
2261 }
2262
2263 if (available) {
2264 available_snapshots[total] = i;
2265 total++;
2266 }
2267 }
2268
2269 if (total > 0) {
2270 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2271 for (i = 0; i < total; i++) {
2272 sn = &sn_tab[available_snapshots[i]];
2273 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2274 }
2275 } else {
2276 monitor_printf(mon, "There is no suitable snapshot available\n");
2277 }
2278
Anthony Liguori7267c092011-08-20 22:09:37 -05002279 g_free(sn_tab);
2280 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002281
aliguoria672b462008-11-11 21:33:36 +00002282}