blob: 70f5c4f9bbd8001f984fdc6082fe213f73c1dc48 [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"
Avi Kivityc5705a72011-12-20 15:59:12 +020086#include "memory.h"
blueswir1511d2b12009-03-07 15:32:56 +000087
aliguoria672b462008-11-11 21:33:36 +000088#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000089
Nolan18995b92009-10-15 16:53:55 -070090#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040091#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070092#endif
93#define ARP_HTYPE_ETH 0x0001
94#define ARP_PTYPE_IP 0x0800
95#define ARP_OP_REQUEST_REV 0x3
96
97static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000098 uint8_t *mac_addr)
99{
Nolan18995b92009-10-15 16:53:55 -0700100 /* Ethernet header. */
101 memset(buf, 0xff, 6); /* destination MAC addr */
102 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
103 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +0000104
Nolan18995b92009-10-15 16:53:55 -0700105 /* RARP header. */
106 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
107 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
108 *(buf + 18) = 6; /* hardware addr length (ethernet) */
109 *(buf + 19) = 4; /* protocol addr length (IPv4) */
110 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
111 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
112 memset(buf + 28, 0x00, 4); /* source protocol addr */
113 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
114 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +0000115
Nolan18995b92009-10-15 16:53:55 -0700116 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
117 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +0000118
Nolan18995b92009-10-15 16:53:55 -0700119 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +0000120}
121
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000122static void qemu_announce_self_iter(NICState *nic, void *opaque)
123{
124 uint8_t buf[60];
125 int len;
126
127 len = announce_self_create(buf, nic->conf->macaddr.a);
128
129 qemu_send_packet_raw(&nic->nc, buf, len);
130}
131
132
Gleb Natapoved8b3302009-05-21 17:17:44 +0300133static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000134{
Gleb Natapoved8b3302009-05-21 17:17:44 +0300135 static int count = SELF_ANNOUNCE_ROUNDS;
136 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +0000137
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000138 qemu_foreach_nic(qemu_announce_self_iter, NULL);
139
Nolan18995b92009-10-15 16:53:55 -0700140 if (--count) {
141 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100142 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -0700143 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300144 } else {
145 qemu_del_timer(timer);
146 qemu_free_timer(timer);
147 }
148}
149
150void qemu_announce_self(void)
151{
152 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100153 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300154 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000155}
156
157/***********************************************************/
158/* savevm/loadvm support */
159
160#define IO_BUF_SIZE 32768
161
162struct QEMUFile {
163 QEMUFilePutBufferFunc *put_buffer;
164 QEMUFileGetBufferFunc *get_buffer;
165 QEMUFileCloseFunc *close;
166 QEMUFileRateLimit *rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400167 QEMUFileSetRateLimit *set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200168 QEMUFileGetRateLimit *get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000169 void *opaque;
170 int is_write;
171
172 int64_t buf_offset; /* start of buffer when writing, end of buffer
173 when reading */
174 int buf_index;
175 int buf_size; /* 0 when writing */
176 uint8_t buf[IO_BUF_SIZE];
177
Juan Quintela3961b4d2011-10-05 01:05:21 +0200178 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000179};
180
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200181typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000182{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200183 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000184 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200185} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000186
187typedef struct QEMUFileSocket
188{
189 int fd;
190 QEMUFile *file;
191} QEMUFileSocket;
192
193static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
194{
195 QEMUFileSocket *s = opaque;
196 ssize_t len;
197
198 do {
Blue Swirl00aa0042011-07-23 20:04:29 +0000199 len = qemu_recv(s->fd, buf, size, 0);
aliguoria672b462008-11-11 21:33:36 +0000200 } while (len == -1 && socket_error() == EINTR);
201
202 if (len == -1)
203 len = -socket_error();
204
205 return len;
206}
207
208static int socket_close(void *opaque)
209{
210 QEMUFileSocket *s = opaque;
Anthony Liguori7267c092011-08-20 22:09:37 -0500211 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000212 return 0;
213}
214
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200215static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000216{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200217 QEMUFileStdio *s = opaque;
218 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000219}
220
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200221static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000222{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200223 QEMUFileStdio *s = opaque;
224 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300225 int bytes;
226
227 do {
228 clearerr(fp);
229 bytes = fread(buf, 1, size, fp);
230 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
231 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000232}
233
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200234static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000235{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200236 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500237 int ret;
238 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200239 if (ret == -1) {
240 ret = -errno;
241 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500242 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500243 return ret;
aliguoria672b462008-11-11 21:33:36 +0000244}
245
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200246static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000247{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200248 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200249 int ret = 0;
250 if (fclose(s->stdio_file) == EOF) {
251 ret = -errno;
252 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500253 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200254 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200255}
aliguoria672b462008-11-11 21:33:36 +0000256
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200257QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
258{
259 QEMUFileStdio *s;
260
261 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000262 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
263 return NULL;
264 }
265
Anthony Liguori7267c092011-08-20 22:09:37 -0500266 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000267
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200268 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000269
270 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200271 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
272 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000273 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200274 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
275 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000276 }
aliguoria672b462008-11-11 21:33:36 +0000277 return s->file;
278}
279
280QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
281{
282 FILE *popen_file;
283
284 popen_file = popen(command, mode);
285 if(popen_file == NULL) {
286 return NULL;
287 }
288
289 return qemu_popen(popen_file, mode);
290}
291
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200292int qemu_stdio_fd(QEMUFile *f)
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200293{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200294 QEMUFileStdio *p;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200295 int fd;
296
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200297 p = (QEMUFileStdio *)f->opaque;
298 fd = fileno(p->stdio_file);
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200299
300 return fd;
301}
302
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200303QEMUFile *qemu_fdopen(int fd, const char *mode)
304{
305 QEMUFileStdio *s;
306
307 if (mode == NULL ||
308 (mode[0] != 'r' && mode[0] != 'w') ||
309 mode[1] != 'b' || mode[2] != 0) {
310 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
311 return NULL;
312 }
313
Anthony Liguori7267c092011-08-20 22:09:37 -0500314 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200315 s->stdio_file = fdopen(fd, mode);
316 if (!s->stdio_file)
317 goto fail;
318
319 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200320 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
321 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200322 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200323 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
324 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200325 }
326 return s->file;
327
328fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500329 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200330 return NULL;
331}
332
aliguoria672b462008-11-11 21:33:36 +0000333QEMUFile *qemu_fopen_socket(int fd)
334{
Anthony Liguori7267c092011-08-20 22:09:37 -0500335 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000336
aliguoria672b462008-11-11 21:33:36 +0000337 s->fd = fd;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200338 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
339 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000340 return s->file;
341}
342
aliguoria672b462008-11-11 21:33:36 +0000343static int file_put_buffer(void *opaque, const uint8_t *buf,
344 int64_t pos, int size)
345{
346 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200347 fseek(s->stdio_file, pos, SEEK_SET);
Kirill A. Shutemov5fdb3aa2009-12-25 18:19:19 +0000348 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000349}
350
351static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
352{
353 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200354 fseek(s->stdio_file, pos, SEEK_SET);
355 return fread(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000356}
357
358QEMUFile *qemu_fopen(const char *filename, const char *mode)
359{
360 QEMUFileStdio *s;
361
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200362 if (mode == NULL ||
363 (mode[0] != 'r' && mode[0] != 'w') ||
364 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000365 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200366 return NULL;
367 }
368
Anthony Liguori7267c092011-08-20 22:09:37 -0500369 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000370
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200371 s->stdio_file = fopen(filename, mode);
372 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000373 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200374
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200375 if(mode[0] == 'w') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200376 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
377 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200378 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200379 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
380 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200381 }
382 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000383fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500384 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000385 return NULL;
386}
387
aliguori178e08a2009-04-05 19:10:55 +0000388static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000389 int64_t pos, int size)
390{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200391 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000392 return size;
393}
394
aliguori178e08a2009-04-05 19:10:55 +0000395static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000396{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200397 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000398}
399
400static int bdrv_fclose(void *opaque)
401{
aliguoria672b462008-11-11 21:33:36 +0000402 return 0;
403}
404
Christoph Hellwig45566e92009-07-10 23:11:57 +0200405static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000406{
aliguoria672b462008-11-11 21:33:36 +0000407 if (is_writable)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200408 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
409 NULL, NULL, NULL);
410 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000411}
412
413QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
414 QEMUFileGetBufferFunc *get_buffer,
415 QEMUFileCloseFunc *close,
Glauber Costa19629532009-05-20 18:26:57 -0400416 QEMUFileRateLimit *rate_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200417 QEMUFileSetRateLimit *set_rate_limit,
418 QEMUFileGetRateLimit *get_rate_limit)
aliguoria672b462008-11-11 21:33:36 +0000419{
420 QEMUFile *f;
421
Anthony Liguori7267c092011-08-20 22:09:37 -0500422 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000423
424 f->opaque = opaque;
425 f->put_buffer = put_buffer;
426 f->get_buffer = get_buffer;
427 f->close = close;
428 f->rate_limit = rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400429 f->set_rate_limit = set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200430 f->get_rate_limit = get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000431 f->is_write = 0;
432
433 return f;
434}
435
Juan Quintela624b9cc2011-10-05 01:02:52 +0200436int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000437{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200438 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000439}
440
Juan Quinteladcd1d222011-09-21 23:01:54 +0200441void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000442{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200443 f->last_error = ret;
aliguori4dabe242009-04-05 19:30:51 +0000444}
445
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200446/** Sets last_error conditionally
447 *
448 * Sets last_error only if ret is negative _and_ no error
449 * was set before.
450 */
451static void qemu_file_set_if_error(QEMUFile *f, int ret)
452{
453 if (ret < 0 && !f->last_error) {
454 qemu_file_set_error(f, ret);
455 }
456}
457
458/** Flushes QEMUFile buffer
459 *
460 * In case of error, last_error is set.
461 */
aliguoria672b462008-11-11 21:33:36 +0000462void qemu_fflush(QEMUFile *f)
463{
464 if (!f->put_buffer)
465 return;
466
467 if (f->is_write && f->buf_index > 0) {
468 int len;
469
470 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
471 if (len > 0)
472 f->buf_offset += f->buf_index;
473 else
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200474 qemu_file_set_error(f, -EINVAL);
aliguoria672b462008-11-11 21:33:36 +0000475 f->buf_index = 0;
476 }
477}
478
479static void qemu_fill_buffer(QEMUFile *f)
480{
481 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200482 int pending;
aliguoria672b462008-11-11 21:33:36 +0000483
484 if (!f->get_buffer)
485 return;
486
487 if (f->is_write)
488 abort();
489
Juan Quintela0046c452011-09-30 19:28:45 +0200490 pending = f->buf_size - f->buf_index;
491 if (pending > 0) {
492 memmove(f->buf, f->buf + f->buf_index, pending);
493 }
494 f->buf_index = 0;
495 f->buf_size = pending;
496
497 len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
498 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000499 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200500 f->buf_size += len;
aliguoria672b462008-11-11 21:33:36 +0000501 f->buf_offset += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200502 } else if (len == 0) {
503 f->last_error = -EIO;
aliguoria672b462008-11-11 21:33:36 +0000504 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200505 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000506}
507
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200508/** Calls close function and set last_error if needed
509 *
510 * Internal function. qemu_fflush() must be called before this.
511 *
512 * Returns f->close() return value, or 0 if close function is not set.
513 */
514static int qemu_close(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000515{
516 int ret = 0;
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200517 if (f->close) {
aliguoria672b462008-11-11 21:33:36 +0000518 ret = f->close(f->opaque);
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200519 qemu_file_set_if_error(f, ret);
520 }
521 return ret;
522}
523
524/** Closes the file
525 *
526 * Returns negative error value if any error happened on previous operations or
527 * while closing the file. Returns 0 or positive number on success.
528 *
529 * The meaning of return value on success depends on the specific backend
530 * being used.
531 */
532int qemu_fclose(QEMUFile *f)
533{
534 int ret;
535 qemu_fflush(f);
536 ret = qemu_close(f);
537 /* If any error was spotted before closing, we should report it
538 * instead of the close() return value.
539 */
540 if (f->last_error) {
541 ret = f->last_error;
542 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500543 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000544 return ret;
545}
546
547void qemu_file_put_notify(QEMUFile *f)
548{
549 f->put_buffer(f->opaque, NULL, 0, 0);
550}
551
552void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
553{
554 int l;
555
Juan Quintela3961b4d2011-10-05 01:05:21 +0200556 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000557 fprintf(stderr,
558 "Attempted to write to buffer while read buffer is not empty\n");
559 abort();
560 }
561
Juan Quintela3961b4d2011-10-05 01:05:21 +0200562 while (!f->last_error && size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000563 l = IO_BUF_SIZE - f->buf_index;
564 if (l > size)
565 l = size;
566 memcpy(f->buf + f->buf_index, buf, l);
567 f->is_write = 1;
568 f->buf_index += l;
569 buf += l;
570 size -= l;
571 if (f->buf_index >= IO_BUF_SIZE)
572 qemu_fflush(f);
573 }
574}
575
576void qemu_put_byte(QEMUFile *f, int v)
577{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200578 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000579 fprintf(stderr,
580 "Attempted to write to buffer while read buffer is not empty\n");
581 abort();
582 }
583
584 f->buf[f->buf_index++] = v;
585 f->is_write = 1;
586 if (f->buf_index >= IO_BUF_SIZE)
587 qemu_fflush(f);
588}
589
Juan Quintelac6380722011-10-04 15:28:31 +0200590static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000591{
Juan Quintelac6380722011-10-04 15:28:31 +0200592 if (f->buf_index + size <= f->buf_size) {
593 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000594 }
aliguoria672b462008-11-11 21:33:36 +0000595}
596
Juan Quintelac6380722011-10-04 15:28:31 +0200597static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200598{
Juan Quintelac6380722011-10-04 15:28:31 +0200599 int pending;
600 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200601
Juan Quintelab9ce1452011-10-04 13:55:32 +0200602 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200603 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200604 }
Juan Quintela811814b2010-07-26 21:38:43 +0200605
Juan Quintelac6380722011-10-04 15:28:31 +0200606 index = f->buf_index + offset;
607 pending = f->buf_size - index;
608 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200609 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200610 index = f->buf_index + offset;
611 pending = f->buf_size - index;
612 }
613
614 if (pending <= 0) {
615 return 0;
616 }
617 if (size > pending) {
618 size = pending;
619 }
620
621 memcpy(buf, f->buf + index, size);
622 return size;
623}
624
625int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
626{
627 int pending = size;
628 int done = 0;
629
630 while (pending > 0) {
631 int res;
632
633 res = qemu_peek_buffer(f, buf, pending, 0);
634 if (res == 0) {
635 return done;
636 }
637 qemu_file_skip(f, res);
638 buf += res;
639 pending -= res;
640 done += res;
641 }
642 return done;
643}
644
645static int qemu_peek_byte(QEMUFile *f, int offset)
646{
647 int index = f->buf_index + offset;
648
649 if (f->is_write) {
650 abort();
651 }
652
653 if (index >= f->buf_size) {
654 qemu_fill_buffer(f);
655 index = f->buf_index + offset;
656 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200657 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200658 }
Juan Quintela811814b2010-07-26 21:38:43 +0200659 }
Juan Quintelac6380722011-10-04 15:28:31 +0200660 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200661}
662
aliguoria672b462008-11-11 21:33:36 +0000663int qemu_get_byte(QEMUFile *f)
664{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200665 int result;
aliguoria672b462008-11-11 21:33:36 +0000666
Juan Quintelac6380722011-10-04 15:28:31 +0200667 result = qemu_peek_byte(f, 0);
668 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200669 return result;
aliguoria672b462008-11-11 21:33:36 +0000670}
671
672int64_t qemu_ftell(QEMUFile *f)
673{
674 return f->buf_offset - f->buf_size + f->buf_index;
675}
676
677int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
678{
679 if (whence == SEEK_SET) {
680 /* nothing to do */
681 } else if (whence == SEEK_CUR) {
682 pos += qemu_ftell(f);
683 } else {
684 /* SEEK_END not supported */
685 return -1;
686 }
687 if (f->put_buffer) {
688 qemu_fflush(f);
689 f->buf_offset = pos;
690 } else {
691 f->buf_offset = pos;
692 f->buf_index = 0;
693 f->buf_size = 0;
694 }
695 return pos;
696}
697
698int qemu_file_rate_limit(QEMUFile *f)
699{
700 if (f->rate_limit)
701 return f->rate_limit(f->opaque);
702
703 return 0;
704}
705
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200706int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200707{
708 if (f->get_rate_limit)
709 return f->get_rate_limit(f->opaque);
710
711 return 0;
712}
713
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200714int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
Glauber Costa19629532009-05-20 18:26:57 -0400715{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400716 /* any failed or completed migration keeps its state to allow probing of
717 * migration data, but has no associated file anymore */
718 if (f && f->set_rate_limit)
Glauber Costa19629532009-05-20 18:26:57 -0400719 return f->set_rate_limit(f->opaque, new_rate);
720
721 return 0;
722}
723
aliguoria672b462008-11-11 21:33:36 +0000724void qemu_put_be16(QEMUFile *f, unsigned int v)
725{
726 qemu_put_byte(f, v >> 8);
727 qemu_put_byte(f, v);
728}
729
730void qemu_put_be32(QEMUFile *f, unsigned int v)
731{
732 qemu_put_byte(f, v >> 24);
733 qemu_put_byte(f, v >> 16);
734 qemu_put_byte(f, v >> 8);
735 qemu_put_byte(f, v);
736}
737
738void qemu_put_be64(QEMUFile *f, uint64_t v)
739{
740 qemu_put_be32(f, v >> 32);
741 qemu_put_be32(f, v);
742}
743
744unsigned int qemu_get_be16(QEMUFile *f)
745{
746 unsigned int v;
747 v = qemu_get_byte(f) << 8;
748 v |= qemu_get_byte(f);
749 return v;
750}
751
752unsigned int qemu_get_be32(QEMUFile *f)
753{
754 unsigned int v;
755 v = qemu_get_byte(f) << 24;
756 v |= qemu_get_byte(f) << 16;
757 v |= qemu_get_byte(f) << 8;
758 v |= qemu_get_byte(f);
759 return v;
760}
761
762uint64_t qemu_get_be64(QEMUFile *f)
763{
764 uint64_t v;
765 v = (uint64_t)qemu_get_be32(f) << 32;
766 v |= qemu_get_be32(f);
767 return v;
768}
769
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200770
771/* timer */
772
773void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
774{
775 uint64_t expire_time;
776
777 expire_time = qemu_timer_expire_time_ns(ts);
778 qemu_put_be64(f, expire_time);
779}
780
781void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
782{
783 uint64_t expire_time;
784
785 expire_time = qemu_get_be64(f);
786 if (expire_time != -1) {
787 qemu_mod_timer_ns(ts, expire_time);
788 } else {
789 qemu_del_timer(ts);
790 }
791}
792
793
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100794/* bool */
795
796static int get_bool(QEMUFile *f, void *pv, size_t size)
797{
798 bool *v = pv;
799 *v = qemu_get_byte(f);
800 return 0;
801}
802
803static void put_bool(QEMUFile *f, void *pv, size_t size)
804{
805 bool *v = pv;
806 qemu_put_byte(f, *v);
807}
808
809const VMStateInfo vmstate_info_bool = {
810 .name = "bool",
811 .get = get_bool,
812 .put = put_bool,
813};
814
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200815/* 8 bit int */
816
817static int get_int8(QEMUFile *f, void *pv, size_t size)
818{
819 int8_t *v = pv;
820 qemu_get_s8s(f, v);
821 return 0;
822}
823
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200824static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200825{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200826 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200827 qemu_put_s8s(f, v);
828}
829
830const VMStateInfo vmstate_info_int8 = {
831 .name = "int8",
832 .get = get_int8,
833 .put = put_int8,
834};
835
836/* 16 bit int */
837
838static int get_int16(QEMUFile *f, void *pv, size_t size)
839{
840 int16_t *v = pv;
841 qemu_get_sbe16s(f, v);
842 return 0;
843}
844
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200845static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200846{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200847 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200848 qemu_put_sbe16s(f, v);
849}
850
851const VMStateInfo vmstate_info_int16 = {
852 .name = "int16",
853 .get = get_int16,
854 .put = put_int16,
855};
856
857/* 32 bit int */
858
859static int get_int32(QEMUFile *f, void *pv, size_t size)
860{
861 int32_t *v = pv;
862 qemu_get_sbe32s(f, v);
863 return 0;
864}
865
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200866static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200867{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200868 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200869 qemu_put_sbe32s(f, v);
870}
871
872const VMStateInfo vmstate_info_int32 = {
873 .name = "int32",
874 .get = get_int32,
875 .put = put_int32,
876};
877
Juan Quintela82501662009-08-20 19:42:32 +0200878/* 32 bit int. See that the received value is the same than the one
879 in the field */
880
881static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
882{
883 int32_t *v = pv;
884 int32_t v2;
885 qemu_get_sbe32s(f, &v2);
886
887 if (*v == v2)
888 return 0;
889 return -EINVAL;
890}
891
892const VMStateInfo vmstate_info_int32_equal = {
893 .name = "int32 equal",
894 .get = get_int32_equal,
895 .put = put_int32,
896};
897
Juan Quintela0a031e02009-08-20 19:42:37 +0200898/* 32 bit int. See that the received value is the less or the same
899 than the one in the field */
900
901static int get_int32_le(QEMUFile *f, void *pv, size_t size)
902{
903 int32_t *old = pv;
904 int32_t new;
905 qemu_get_sbe32s(f, &new);
906
907 if (*old <= new)
908 return 0;
909 return -EINVAL;
910}
911
912const VMStateInfo vmstate_info_int32_le = {
913 .name = "int32 equal",
914 .get = get_int32_le,
915 .put = put_int32,
916};
917
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200918/* 64 bit int */
919
920static int get_int64(QEMUFile *f, void *pv, size_t size)
921{
922 int64_t *v = pv;
923 qemu_get_sbe64s(f, v);
924 return 0;
925}
926
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200927static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200928{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200929 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200930 qemu_put_sbe64s(f, v);
931}
932
933const VMStateInfo vmstate_info_int64 = {
934 .name = "int64",
935 .get = get_int64,
936 .put = put_int64,
937};
938
939/* 8 bit unsigned int */
940
941static int get_uint8(QEMUFile *f, void *pv, size_t size)
942{
943 uint8_t *v = pv;
944 qemu_get_8s(f, v);
945 return 0;
946}
947
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200948static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200949{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200950 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200951 qemu_put_8s(f, v);
952}
953
954const VMStateInfo vmstate_info_uint8 = {
955 .name = "uint8",
956 .get = get_uint8,
957 .put = put_uint8,
958};
959
960/* 16 bit unsigned int */
961
962static int get_uint16(QEMUFile *f, void *pv, size_t size)
963{
964 uint16_t *v = pv;
965 qemu_get_be16s(f, v);
966 return 0;
967}
968
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200969static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200970{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200971 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200972 qemu_put_be16s(f, v);
973}
974
975const VMStateInfo vmstate_info_uint16 = {
976 .name = "uint16",
977 .get = get_uint16,
978 .put = put_uint16,
979};
980
981/* 32 bit unsigned int */
982
983static int get_uint32(QEMUFile *f, void *pv, size_t size)
984{
985 uint32_t *v = pv;
986 qemu_get_be32s(f, v);
987 return 0;
988}
989
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200990static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200991{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200992 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200993 qemu_put_be32s(f, v);
994}
995
996const VMStateInfo vmstate_info_uint32 = {
997 .name = "uint32",
998 .get = get_uint32,
999 .put = put_uint32,
1000};
1001
Juan Quintela9122a8f2011-03-10 12:33:48 +01001002/* 32 bit uint. See that the received value is the same than the one
1003 in the field */
1004
1005static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1006{
1007 uint32_t *v = pv;
1008 uint32_t v2;
1009 qemu_get_be32s(f, &v2);
1010
1011 if (*v == v2) {
1012 return 0;
1013 }
1014 return -EINVAL;
1015}
1016
1017const VMStateInfo vmstate_info_uint32_equal = {
1018 .name = "uint32 equal",
1019 .get = get_uint32_equal,
1020 .put = put_uint32,
1021};
1022
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001023/* 64 bit unsigned int */
1024
1025static int get_uint64(QEMUFile *f, void *pv, size_t size)
1026{
1027 uint64_t *v = pv;
1028 qemu_get_be64s(f, v);
1029 return 0;
1030}
1031
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001032static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001033{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001034 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001035 qemu_put_be64s(f, v);
1036}
1037
1038const VMStateInfo vmstate_info_uint64 = {
1039 .name = "uint64",
1040 .get = get_uint64,
1041 .put = put_uint64,
1042};
1043
Juan Quintela80cd83e2009-09-10 03:04:36 +02001044/* 8 bit int. See that the received value is the same than the one
1045 in the field */
1046
1047static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1048{
1049 uint8_t *v = pv;
1050 uint8_t v2;
1051 qemu_get_8s(f, &v2);
1052
1053 if (*v == v2)
1054 return 0;
1055 return -EINVAL;
1056}
1057
1058const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001059 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001060 .get = get_uint8_equal,
1061 .put = put_uint8,
1062};
1063
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001064/* 16 bit unsigned int int. See that the received value is the same than the one
1065 in the field */
1066
1067static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1068{
1069 uint16_t *v = pv;
1070 uint16_t v2;
1071 qemu_get_be16s(f, &v2);
1072
1073 if (*v == v2)
1074 return 0;
1075 return -EINVAL;
1076}
1077
1078const VMStateInfo vmstate_info_uint16_equal = {
1079 .name = "uint16 equal",
1080 .get = get_uint16_equal,
1081 .put = put_uint16,
1082};
1083
Juan Quinteladde04632009-08-20 19:42:26 +02001084/* timers */
1085
1086static int get_timer(QEMUFile *f, void *pv, size_t size)
1087{
1088 QEMUTimer *v = pv;
1089 qemu_get_timer(f, v);
1090 return 0;
1091}
1092
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001093static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001094{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001095 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001096 qemu_put_timer(f, v);
1097}
1098
1099const VMStateInfo vmstate_info_timer = {
1100 .name = "timer",
1101 .get = get_timer,
1102 .put = put_timer,
1103};
1104
Juan Quintela6f67c502009-08-20 19:42:35 +02001105/* uint8_t buffers */
1106
1107static int get_buffer(QEMUFile *f, void *pv, size_t size)
1108{
1109 uint8_t *v = pv;
1110 qemu_get_buffer(f, v, size);
1111 return 0;
1112}
1113
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001114static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001115{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001116 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001117 qemu_put_buffer(f, v, size);
1118}
1119
1120const VMStateInfo vmstate_info_buffer = {
1121 .name = "buffer",
1122 .get = get_buffer,
1123 .put = put_buffer,
1124};
1125
Juan Quintela76507c72009-10-19 15:46:28 +02001126/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001127 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001128
1129static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1130{
Jan Kiszka21174c32009-12-02 12:36:35 +01001131 uint8_t buf[1024];
1132 int block_len;
1133
1134 while (size > 0) {
1135 block_len = MIN(sizeof(buf), size);
1136 size -= block_len;
1137 qemu_get_buffer(f, buf, block_len);
1138 }
1139 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001140}
1141
1142static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1143{
Jan Kiszka21174c32009-12-02 12:36:35 +01001144 static const uint8_t buf[1024];
1145 int block_len;
1146
1147 while (size > 0) {
1148 block_len = MIN(sizeof(buf), size);
1149 size -= block_len;
1150 qemu_put_buffer(f, buf, block_len);
1151 }
Juan Quintela76507c72009-10-19 15:46:28 +02001152}
1153
1154const VMStateInfo vmstate_info_unused_buffer = {
1155 .name = "unused_buffer",
1156 .get = get_unused_buffer,
1157 .put = put_unused_buffer,
1158};
1159
Alex Williamson7685ee62010-06-25 11:09:14 -06001160typedef struct CompatEntry {
1161 char idstr[256];
1162 int instance_id;
1163} CompatEntry;
1164
aliguoria672b462008-11-11 21:33:36 +00001165typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001166 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001167 char idstr[256];
1168 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001169 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001170 int version_id;
1171 int section_id;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001172 SaveSetParamsHandler *set_params;
aliguoria672b462008-11-11 21:33:36 +00001173 SaveLiveStateHandler *save_live_state;
1174 SaveStateHandler *save_state;
1175 LoadStateHandler *load_state;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001176 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001177 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001178 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001179 int no_migrate;
aliguoria672b462008-11-11 21:33:36 +00001180} SaveStateEntry;
1181
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001182
Blue Swirl72cf2d42009-09-12 07:36:22 +00001183static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1184 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001185static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001186
Juan Quintela8718e992009-09-01 02:12:31 +02001187static int calculate_new_instance_id(const char *idstr)
1188{
1189 SaveStateEntry *se;
1190 int instance_id = 0;
1191
Blue Swirl72cf2d42009-09-12 07:36:22 +00001192 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001193 if (strcmp(idstr, se->idstr) == 0
1194 && instance_id <= se->instance_id) {
1195 instance_id = se->instance_id + 1;
1196 }
1197 }
1198 return instance_id;
1199}
1200
Alex Williamson7685ee62010-06-25 11:09:14 -06001201static int calculate_compat_instance_id(const char *idstr)
1202{
1203 SaveStateEntry *se;
1204 int instance_id = 0;
1205
1206 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1207 if (!se->compat)
1208 continue;
1209
1210 if (strcmp(idstr, se->compat->idstr) == 0
1211 && instance_id <= se->compat->instance_id) {
1212 instance_id = se->compat->instance_id + 1;
1213 }
1214 }
1215 return instance_id;
1216}
1217
aliguoria672b462008-11-11 21:33:36 +00001218/* TODO: Individual devices generally have very little idea about the rest
1219 of the system, so instance_id should be removed/replaced.
1220 Meanwhile pass -1 as instance_id if you do not already have a clearly
1221 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001222int register_savevm_live(DeviceState *dev,
1223 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001224 int instance_id,
1225 int version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001226 SaveSetParamsHandler *set_params,
aliguoria672b462008-11-11 21:33:36 +00001227 SaveLiveStateHandler *save_live_state,
1228 SaveStateHandler *save_state,
1229 LoadStateHandler *load_state,
1230 void *opaque)
1231{
Juan Quintela8718e992009-09-01 02:12:31 +02001232 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001233
Anthony Liguori7267c092011-08-20 22:09:37 -05001234 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001235 se->version_id = version_id;
1236 se->section_id = global_section_id++;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001237 se->set_params = set_params;
aliguoria672b462008-11-11 21:33:36 +00001238 se->save_live_state = save_live_state;
1239 se->save_state = save_state;
1240 se->load_state = load_state;
1241 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001242 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001243 se->no_migrate = 0;
aliguoria672b462008-11-11 21:33:36 +00001244
Alex Williamson7685ee62010-06-25 11:09:14 -06001245 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1246 char *id = dev->parent_bus->info->get_dev_path(dev);
1247 if (id) {
1248 pstrcpy(se->idstr, sizeof(se->idstr), id);
1249 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001250 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001251
Anthony Liguori7267c092011-08-20 22:09:37 -05001252 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001253 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1254 se->compat->instance_id = instance_id == -1 ?
1255 calculate_compat_instance_id(idstr) : instance_id;
1256 instance_id = -1;
1257 }
1258 }
1259 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1260
Juan Quintela8718e992009-09-01 02:12:31 +02001261 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001262 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001263 } else {
1264 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001265 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001266 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001267 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001268 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001269 return 0;
1270}
1271
Alex Williamson0be71e32010-06-25 11:09:07 -06001272int register_savevm(DeviceState *dev,
1273 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001274 int instance_id,
1275 int version_id,
1276 SaveStateHandler *save_state,
1277 LoadStateHandler *load_state,
1278 void *opaque)
1279{
Alex Williamson0be71e32010-06-25 11:09:07 -06001280 return register_savevm_live(dev, idstr, instance_id, version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001281 NULL, NULL, save_state, load_state, opaque);
aliguoria672b462008-11-11 21:33:36 +00001282}
1283
Alex Williamson0be71e32010-06-25 11:09:07 -06001284void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001285{
Juan Quintela8718e992009-09-01 02:12:31 +02001286 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001287 char id[256] = "";
1288
1289 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1290 char *path = dev->parent_bus->info->get_dev_path(dev);
1291 if (path) {
1292 pstrcpy(id, sizeof(id), path);
1293 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001294 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001295 }
1296 }
1297 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001298
Blue Swirl72cf2d42009-09-12 07:36:22 +00001299 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001300 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001301 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001302 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001303 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001304 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001305 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001306 }
aliguori41bd13a2009-04-17 17:10:59 +00001307 }
1308}
1309
Alex Williamson0be71e32010-06-25 11:09:07 -06001310int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001311 const VMStateDescription *vmsd,
1312 void *opaque, int alias_id,
1313 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001314{
Juan Quintela8718e992009-09-01 02:12:31 +02001315 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001316
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001317 /* If this triggers, alias support can be dropped for the vmsd. */
1318 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1319
Anthony Liguori7267c092011-08-20 22:09:37 -05001320 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001321 se->version_id = vmsd->version_id;
1322 se->section_id = global_section_id++;
1323 se->save_live_state = NULL;
1324 se->save_state = NULL;
1325 se->load_state = NULL;
1326 se->opaque = opaque;
1327 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001328 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001329 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001330
Alex Williamson7685ee62010-06-25 11:09:14 -06001331 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1332 char *id = dev->parent_bus->info->get_dev_path(dev);
1333 if (id) {
1334 pstrcpy(se->idstr, sizeof(se->idstr), id);
1335 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001336 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001337
Anthony Liguori7267c092011-08-20 22:09:37 -05001338 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001339 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1340 se->compat->instance_id = instance_id == -1 ?
1341 calculate_compat_instance_id(vmsd->name) : instance_id;
1342 instance_id = -1;
1343 }
1344 }
1345 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1346
Juan Quintela8718e992009-09-01 02:12:31 +02001347 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001348 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001349 } else {
1350 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001351 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001352 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001353 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001354 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001355 return 0;
1356}
1357
Alex Williamson0be71e32010-06-25 11:09:07 -06001358int vmstate_register(DeviceState *dev, int instance_id,
1359 const VMStateDescription *vmsd, void *opaque)
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001360{
Alex Williamson0be71e32010-06-25 11:09:07 -06001361 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1362 opaque, -1, 0);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001363}
1364
Alex Williamson0be71e32010-06-25 11:09:07 -06001365void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1366 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001367{
Juan Quintela1eb75382009-09-10 03:04:29 +02001368 SaveStateEntry *se, *new_se;
1369
Blue Swirl72cf2d42009-09-12 07:36:22 +00001370 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001371 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001372 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001373 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001374 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001375 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001376 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001377 }
1378 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001379}
1380
Juan Quintela811814b2010-07-26 21:38:43 +02001381static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1382 void *opaque);
1383static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1384 void *opaque);
1385
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001386int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1387 void *opaque, int version_id)
1388{
1389 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001390 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001391
1392 if (version_id > vmsd->version_id) {
1393 return -EINVAL;
1394 }
1395 if (version_id < vmsd->minimum_version_id_old) {
1396 return -EINVAL;
1397 }
1398 if (version_id < vmsd->minimum_version_id) {
1399 return vmsd->load_state_old(f, opaque, version_id);
1400 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001401 if (vmsd->pre_load) {
1402 int ret = vmsd->pre_load(opaque);
1403 if (ret)
1404 return ret;
1405 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001406 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001407 if ((field->field_exists &&
1408 field->field_exists(opaque, version_id)) ||
1409 (!field->field_exists &&
1410 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001411 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001412 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001413 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001414
Juan Quintelae61a1e02009-12-02 12:36:38 +01001415 if (field->flags & VMS_VBUFFER) {
1416 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001417 if (field->flags & VMS_MULTIPLY) {
1418 size *= field->size;
1419 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001420 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001421 if (field->flags & VMS_ARRAY) {
1422 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001423 } else if (field->flags & VMS_VARRAY_INT32) {
1424 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001425 } else if (field->flags & VMS_VARRAY_UINT32) {
1426 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001427 } else if (field->flags & VMS_VARRAY_UINT16) {
1428 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001429 } else if (field->flags & VMS_VARRAY_UINT8) {
1430 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001431 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001432 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001433 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001434 }
1435 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001436 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001437
Juan Quintela19df4382009-09-29 22:48:41 +02001438 if (field->flags & VMS_ARRAY_OF_POINTER) {
1439 addr = *(void **)addr;
1440 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001441 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001442 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001443 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001444 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001445
1446 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001447 if (ret < 0) {
1448 return ret;
1449 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001450 }
1451 }
1452 field++;
1453 }
Juan Quintela811814b2010-07-26 21:38:43 +02001454 ret = vmstate_subsection_load(f, vmsd, opaque);
1455 if (ret != 0) {
1456 return ret;
1457 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001458 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001459 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001460 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001461 return 0;
1462}
1463
1464void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001465 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001466{
1467 VMStateField *field = vmsd->fields;
1468
Juan Quintela8fb07912009-09-10 03:04:32 +02001469 if (vmsd->pre_save) {
1470 vmsd->pre_save(opaque);
1471 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001472 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001473 if (!field->field_exists ||
1474 field->field_exists(opaque, vmsd->version_id)) {
1475 void *base_addr = opaque + field->offset;
1476 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001477 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001478
Juan Quintelae61a1e02009-12-02 12:36:38 +01001479 if (field->flags & VMS_VBUFFER) {
1480 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001481 if (field->flags & VMS_MULTIPLY) {
1482 size *= field->size;
1483 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001484 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001485 if (field->flags & VMS_ARRAY) {
1486 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001487 } else if (field->flags & VMS_VARRAY_INT32) {
1488 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001489 } else if (field->flags & VMS_VARRAY_UINT16) {
1490 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001491 } else if (field->flags & VMS_VARRAY_UINT8) {
1492 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001493 }
1494 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001495 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001496 }
1497 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001498 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001499
Juan Quintela85953872009-12-02 12:36:37 +01001500 if (field->flags & VMS_ARRAY_OF_POINTER) {
1501 addr = *(void **)addr;
1502 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001503 if (field->flags & VMS_STRUCT) {
1504 vmstate_save_state(f, field->vmsd, addr);
1505 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001506 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001507 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001508 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001509 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001510 field++;
1511 }
Juan Quintela811814b2010-07-26 21:38:43 +02001512 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001513}
1514
Juan Quintela4082be42009-08-20 19:42:24 +02001515static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1516{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001517 if (!se->vmsd) { /* Old style */
1518 return se->load_state(f, se->opaque, version_id);
1519 }
1520 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001521}
1522
Alex Williamsondc912122011-01-11 14:39:43 -07001523static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001524{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001525 if (!se->vmsd) { /* Old style */
1526 se->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001527 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001528 }
1529 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001530}
1531
aliguoria672b462008-11-11 21:33:36 +00001532#define QEMU_VM_FILE_MAGIC 0x5145564d
1533#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1534#define QEMU_VM_FILE_VERSION 0x00000003
1535
1536#define QEMU_VM_EOF 0x00
1537#define QEMU_VM_SECTION_START 0x01
1538#define QEMU_VM_SECTION_PART 0x02
1539#define QEMU_VM_SECTION_END 0x03
1540#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001541#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001542
Alex Williamsondc912122011-01-11 14:39:43 -07001543bool qemu_savevm_state_blocked(Monitor *mon)
1544{
1545 SaveStateEntry *se;
1546
1547 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1548 if (se->no_migrate) {
1549 monitor_printf(mon, "state blocked by non-migratable device '%s'\n",
1550 se->idstr);
1551 return true;
1552 }
1553 }
1554 return false;
1555}
1556
Luiz Capitulino539de122011-12-05 14:06:56 -02001557int qemu_savevm_state_begin(QEMUFile *f, int blk_enable, 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
Luiz Capitulino539de122011-12-05 14:06:56 -02001590 ret = se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001591 if (ret < 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001592 qemu_savevm_state_cancel(f);
Juan Quintela29757252011-10-19 15:22:18 +02001593 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) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001598 qemu_savevm_state_cancel(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 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001611int qemu_savevm_state_iterate(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
Luiz Capitulino539de122011-12-05 14:06:56 -02001624 ret = se->save_live_state(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) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001638 qemu_savevm_state_cancel(f);
Juan Quintela39346382011-09-22 11:02:14 +02001639 }
1640 return ret;
aliguoria672b462008-11-11 21:33:36 +00001641}
1642
Luiz Capitulino539de122011-12-05 14:06:56 -02001643int qemu_savevm_state_complete(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
Luiz Capitulino539de122011-12-05 14:06:56 -02001658 ret = se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001659 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
Luiz Capitulino539de122011-12-05 14:06:56 -02001690void qemu_savevm_state_cancel(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) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001696 se->save_live_state(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
Luiz Capitulino539de122011-12-05 14:06:56 -02001710 ret = qemu_savevm_state_begin(f, 0, 0);
aliguoria672b462008-11-11 21:33:36 +00001711 if (ret < 0)
1712 goto out;
1713
1714 do {
Luiz Capitulino539de122011-12-05 14:06:56 -02001715 ret = qemu_savevm_state_iterate(f);
aliguoria672b462008-11-11 21:33:36 +00001716 if (ret < 0)
1717 goto out;
1718 } while (ret == 0);
1719
Luiz Capitulino539de122011-12-05 14:06:56 -02001720 ret = qemu_savevm_state_complete(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}
Avi Kivityc5705a72011-12-20 15:59:12 +02002283
2284void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2285{
Avi Kivity1ddde082012-01-08 13:18:19 +02002286 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002287 memory_region_name(mr), dev);
2288}
2289
2290void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2291{
2292 /* Nothing do to while the implementation is in RAMBlock */
2293}
2294
2295void vmstate_register_ram_global(MemoryRegion *mr)
2296{
2297 vmstate_register_ram(mr, NULL);
2298}