blob: faa81457d5f657b711f9d8f5ff0b08a173c1001f [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"
Stefano Stabellinia7ae8352012-01-25 12:24:51 +000087#include "qmp-commands.h"
blueswir1511d2b12009-03-07 15:32:56 +000088
aliguoria672b462008-11-11 21:33:36 +000089#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000090
Nolan18995b92009-10-15 16:53:55 -070091#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040092#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070093#endif
94#define ARP_HTYPE_ETH 0x0001
95#define ARP_PTYPE_IP 0x0800
96#define ARP_OP_REQUEST_REV 0x3
97
98static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000099 uint8_t *mac_addr)
100{
Nolan18995b92009-10-15 16:53:55 -0700101 /* Ethernet header. */
102 memset(buf, 0xff, 6); /* destination MAC addr */
103 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
104 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +0000105
Nolan18995b92009-10-15 16:53:55 -0700106 /* RARP header. */
107 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
108 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
109 *(buf + 18) = 6; /* hardware addr length (ethernet) */
110 *(buf + 19) = 4; /* protocol addr length (IPv4) */
111 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
112 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
113 memset(buf + 28, 0x00, 4); /* source protocol addr */
114 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
115 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +0000116
Nolan18995b92009-10-15 16:53:55 -0700117 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
118 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +0000119
Nolan18995b92009-10-15 16:53:55 -0700120 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +0000121}
122
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000123static void qemu_announce_self_iter(NICState *nic, void *opaque)
124{
125 uint8_t buf[60];
126 int len;
127
128 len = announce_self_create(buf, nic->conf->macaddr.a);
129
130 qemu_send_packet_raw(&nic->nc, buf, len);
131}
132
133
Gleb Natapoved8b3302009-05-21 17:17:44 +0300134static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000135{
Gleb Natapoved8b3302009-05-21 17:17:44 +0300136 static int count = SELF_ANNOUNCE_ROUNDS;
137 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +0000138
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000139 qemu_foreach_nic(qemu_announce_self_iter, NULL);
140
Nolan18995b92009-10-15 16:53:55 -0700141 if (--count) {
142 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100143 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -0700144 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300145 } else {
146 qemu_del_timer(timer);
147 qemu_free_timer(timer);
148 }
149}
150
151void qemu_announce_self(void)
152{
153 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100154 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300155 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000156}
157
158/***********************************************************/
159/* savevm/loadvm support */
160
161#define IO_BUF_SIZE 32768
162
163struct QEMUFile {
164 QEMUFilePutBufferFunc *put_buffer;
165 QEMUFileGetBufferFunc *get_buffer;
166 QEMUFileCloseFunc *close;
167 QEMUFileRateLimit *rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400168 QEMUFileSetRateLimit *set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200169 QEMUFileGetRateLimit *get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000170 void *opaque;
171 int is_write;
172
173 int64_t buf_offset; /* start of buffer when writing, end of buffer
174 when reading */
175 int buf_index;
176 int buf_size; /* 0 when writing */
177 uint8_t buf[IO_BUF_SIZE];
178
Juan Quintela3961b4d2011-10-05 01:05:21 +0200179 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000180};
181
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200182typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000183{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200184 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000185 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200186} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000187
188typedef struct QEMUFileSocket
189{
190 int fd;
191 QEMUFile *file;
192} QEMUFileSocket;
193
194static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
195{
196 QEMUFileSocket *s = opaque;
197 ssize_t len;
198
199 do {
Blue Swirl00aa0042011-07-23 20:04:29 +0000200 len = qemu_recv(s->fd, buf, size, 0);
aliguoria672b462008-11-11 21:33:36 +0000201 } while (len == -1 && socket_error() == EINTR);
202
203 if (len == -1)
204 len = -socket_error();
205
206 return len;
207}
208
209static int socket_close(void *opaque)
210{
211 QEMUFileSocket *s = opaque;
Anthony Liguori7267c092011-08-20 22:09:37 -0500212 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000213 return 0;
214}
215
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200216static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000217{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200218 QEMUFileStdio *s = opaque;
219 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000220}
221
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200222static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000223{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200224 QEMUFileStdio *s = opaque;
225 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300226 int bytes;
227
228 do {
229 clearerr(fp);
230 bytes = fread(buf, 1, size, fp);
231 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
232 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000233}
234
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200235static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000236{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200237 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500238 int ret;
239 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200240 if (ret == -1) {
241 ret = -errno;
242 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500243 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500244 return ret;
aliguoria672b462008-11-11 21:33:36 +0000245}
246
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200247static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000248{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200249 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200250 int ret = 0;
251 if (fclose(s->stdio_file) == EOF) {
252 ret = -errno;
253 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500254 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200255 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200256}
aliguoria672b462008-11-11 21:33:36 +0000257
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200258QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
259{
260 QEMUFileStdio *s;
261
262 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000263 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
264 return NULL;
265 }
266
Anthony Liguori7267c092011-08-20 22:09:37 -0500267 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000268
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200269 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000270
271 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200272 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
273 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000274 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200275 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
276 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000277 }
aliguoria672b462008-11-11 21:33:36 +0000278 return s->file;
279}
280
281QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
282{
283 FILE *popen_file;
284
285 popen_file = popen(command, mode);
286 if(popen_file == NULL) {
287 return NULL;
288 }
289
290 return qemu_popen(popen_file, mode);
291}
292
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200293int qemu_stdio_fd(QEMUFile *f)
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200294{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200295 QEMUFileStdio *p;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200296 int fd;
297
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200298 p = (QEMUFileStdio *)f->opaque;
299 fd = fileno(p->stdio_file);
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200300
301 return fd;
302}
303
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200304QEMUFile *qemu_fdopen(int fd, const char *mode)
305{
306 QEMUFileStdio *s;
307
308 if (mode == NULL ||
309 (mode[0] != 'r' && mode[0] != 'w') ||
310 mode[1] != 'b' || mode[2] != 0) {
311 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
312 return NULL;
313 }
314
Anthony Liguori7267c092011-08-20 22:09:37 -0500315 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200316 s->stdio_file = fdopen(fd, mode);
317 if (!s->stdio_file)
318 goto fail;
319
320 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200321 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
322 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200323 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200324 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
325 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200326 }
327 return s->file;
328
329fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500330 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200331 return NULL;
332}
333
aliguoria672b462008-11-11 21:33:36 +0000334QEMUFile *qemu_fopen_socket(int fd)
335{
Anthony Liguori7267c092011-08-20 22:09:37 -0500336 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000337
aliguoria672b462008-11-11 21:33:36 +0000338 s->fd = fd;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200339 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
340 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000341 return s->file;
342}
343
aliguoria672b462008-11-11 21:33:36 +0000344static int file_put_buffer(void *opaque, const uint8_t *buf,
345 int64_t pos, int size)
346{
347 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200348 fseek(s->stdio_file, pos, SEEK_SET);
Kirill A. Shutemov5fdb3aa2009-12-25 18:19:19 +0000349 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000350}
351
352static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
353{
354 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200355 fseek(s->stdio_file, pos, SEEK_SET);
356 return fread(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000357}
358
359QEMUFile *qemu_fopen(const char *filename, const char *mode)
360{
361 QEMUFileStdio *s;
362
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200363 if (mode == NULL ||
364 (mode[0] != 'r' && mode[0] != 'w') ||
365 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000366 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200367 return NULL;
368 }
369
Anthony Liguori7267c092011-08-20 22:09:37 -0500370 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000371
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200372 s->stdio_file = fopen(filename, mode);
373 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000374 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200375
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200376 if(mode[0] == 'w') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200377 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
378 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200379 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200380 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
381 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200382 }
383 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000384fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500385 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000386 return NULL;
387}
388
aliguori178e08a2009-04-05 19:10:55 +0000389static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000390 int64_t pos, int size)
391{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200392 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000393 return size;
394}
395
aliguori178e08a2009-04-05 19:10:55 +0000396static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000397{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200398 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000399}
400
401static int bdrv_fclose(void *opaque)
402{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200403 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000404}
405
Christoph Hellwig45566e92009-07-10 23:11:57 +0200406static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000407{
aliguoria672b462008-11-11 21:33:36 +0000408 if (is_writable)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200409 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
410 NULL, NULL, NULL);
411 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000412}
413
414QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
415 QEMUFileGetBufferFunc *get_buffer,
416 QEMUFileCloseFunc *close,
Glauber Costa19629532009-05-20 18:26:57 -0400417 QEMUFileRateLimit *rate_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200418 QEMUFileSetRateLimit *set_rate_limit,
419 QEMUFileGetRateLimit *get_rate_limit)
aliguoria672b462008-11-11 21:33:36 +0000420{
421 QEMUFile *f;
422
Anthony Liguori7267c092011-08-20 22:09:37 -0500423 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000424
425 f->opaque = opaque;
426 f->put_buffer = put_buffer;
427 f->get_buffer = get_buffer;
428 f->close = close;
429 f->rate_limit = rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400430 f->set_rate_limit = set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200431 f->get_rate_limit = get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000432 f->is_write = 0;
433
434 return f;
435}
436
Juan Quintela624b9cc2011-10-05 01:02:52 +0200437int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000438{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200439 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000440}
441
Juan Quinteladcd1d222011-09-21 23:01:54 +0200442void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000443{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200444 f->last_error = ret;
aliguori4dabe242009-04-05 19:30:51 +0000445}
446
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200447/** Sets last_error conditionally
448 *
449 * Sets last_error only if ret is negative _and_ no error
450 * was set before.
451 */
452static void qemu_file_set_if_error(QEMUFile *f, int ret)
453{
454 if (ret < 0 && !f->last_error) {
455 qemu_file_set_error(f, ret);
456 }
457}
458
459/** Flushes QEMUFile buffer
460 *
461 * In case of error, last_error is set.
462 */
aliguoria672b462008-11-11 21:33:36 +0000463void qemu_fflush(QEMUFile *f)
464{
465 if (!f->put_buffer)
466 return;
467
468 if (f->is_write && f->buf_index > 0) {
469 int len;
470
471 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
472 if (len > 0)
473 f->buf_offset += f->buf_index;
474 else
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200475 qemu_file_set_error(f, -EINVAL);
aliguoria672b462008-11-11 21:33:36 +0000476 f->buf_index = 0;
477 }
478}
479
480static void qemu_fill_buffer(QEMUFile *f)
481{
482 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200483 int pending;
aliguoria672b462008-11-11 21:33:36 +0000484
485 if (!f->get_buffer)
486 return;
487
488 if (f->is_write)
489 abort();
490
Juan Quintela0046c452011-09-30 19:28:45 +0200491 pending = f->buf_size - f->buf_index;
492 if (pending > 0) {
493 memmove(f->buf, f->buf + f->buf_index, pending);
494 }
495 f->buf_index = 0;
496 f->buf_size = pending;
497
498 len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
499 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000500 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200501 f->buf_size += len;
aliguoria672b462008-11-11 21:33:36 +0000502 f->buf_offset += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200503 } else if (len == 0) {
504 f->last_error = -EIO;
aliguoria672b462008-11-11 21:33:36 +0000505 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200506 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000507}
508
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200509/** Calls close function and set last_error if needed
510 *
511 * Internal function. qemu_fflush() must be called before this.
512 *
513 * Returns f->close() return value, or 0 if close function is not set.
514 */
515static int qemu_close(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000516{
517 int ret = 0;
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200518 if (f->close) {
aliguoria672b462008-11-11 21:33:36 +0000519 ret = f->close(f->opaque);
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200520 qemu_file_set_if_error(f, ret);
521 }
522 return ret;
523}
524
525/** Closes the file
526 *
527 * Returns negative error value if any error happened on previous operations or
528 * while closing the file. Returns 0 or positive number on success.
529 *
530 * The meaning of return value on success depends on the specific backend
531 * being used.
532 */
533int qemu_fclose(QEMUFile *f)
534{
535 int ret;
536 qemu_fflush(f);
537 ret = qemu_close(f);
538 /* If any error was spotted before closing, we should report it
539 * instead of the close() return value.
540 */
541 if (f->last_error) {
542 ret = f->last_error;
543 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500544 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000545 return ret;
546}
547
548void qemu_file_put_notify(QEMUFile *f)
549{
550 f->put_buffer(f->opaque, NULL, 0, 0);
551}
552
553void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
554{
555 int l;
556
Juan Quintela3961b4d2011-10-05 01:05:21 +0200557 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000558 fprintf(stderr,
559 "Attempted to write to buffer while read buffer is not empty\n");
560 abort();
561 }
562
Juan Quintela3961b4d2011-10-05 01:05:21 +0200563 while (!f->last_error && size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000564 l = IO_BUF_SIZE - f->buf_index;
565 if (l > size)
566 l = size;
567 memcpy(f->buf + f->buf_index, buf, l);
568 f->is_write = 1;
569 f->buf_index += l;
570 buf += l;
571 size -= l;
572 if (f->buf_index >= IO_BUF_SIZE)
573 qemu_fflush(f);
574 }
575}
576
577void qemu_put_byte(QEMUFile *f, int v)
578{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200579 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000580 fprintf(stderr,
581 "Attempted to write to buffer while read buffer is not empty\n");
582 abort();
583 }
584
585 f->buf[f->buf_index++] = v;
586 f->is_write = 1;
587 if (f->buf_index >= IO_BUF_SIZE)
588 qemu_fflush(f);
589}
590
Juan Quintelac6380722011-10-04 15:28:31 +0200591static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000592{
Juan Quintelac6380722011-10-04 15:28:31 +0200593 if (f->buf_index + size <= f->buf_size) {
594 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000595 }
aliguoria672b462008-11-11 21:33:36 +0000596}
597
Juan Quintelac6380722011-10-04 15:28:31 +0200598static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200599{
Juan Quintelac6380722011-10-04 15:28:31 +0200600 int pending;
601 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200602
Juan Quintelab9ce1452011-10-04 13:55:32 +0200603 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200604 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200605 }
Juan Quintela811814b2010-07-26 21:38:43 +0200606
Juan Quintelac6380722011-10-04 15:28:31 +0200607 index = f->buf_index + offset;
608 pending = f->buf_size - index;
609 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200610 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200611 index = f->buf_index + offset;
612 pending = f->buf_size - index;
613 }
614
615 if (pending <= 0) {
616 return 0;
617 }
618 if (size > pending) {
619 size = pending;
620 }
621
622 memcpy(buf, f->buf + index, size);
623 return size;
624}
625
626int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
627{
628 int pending = size;
629 int done = 0;
630
631 while (pending > 0) {
632 int res;
633
634 res = qemu_peek_buffer(f, buf, pending, 0);
635 if (res == 0) {
636 return done;
637 }
638 qemu_file_skip(f, res);
639 buf += res;
640 pending -= res;
641 done += res;
642 }
643 return done;
644}
645
646static int qemu_peek_byte(QEMUFile *f, int offset)
647{
648 int index = f->buf_index + offset;
649
650 if (f->is_write) {
651 abort();
652 }
653
654 if (index >= f->buf_size) {
655 qemu_fill_buffer(f);
656 index = f->buf_index + offset;
657 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200658 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200659 }
Juan Quintela811814b2010-07-26 21:38:43 +0200660 }
Juan Quintelac6380722011-10-04 15:28:31 +0200661 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200662}
663
aliguoria672b462008-11-11 21:33:36 +0000664int qemu_get_byte(QEMUFile *f)
665{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200666 int result;
aliguoria672b462008-11-11 21:33:36 +0000667
Juan Quintelac6380722011-10-04 15:28:31 +0200668 result = qemu_peek_byte(f, 0);
669 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200670 return result;
aliguoria672b462008-11-11 21:33:36 +0000671}
672
673int64_t qemu_ftell(QEMUFile *f)
674{
675 return f->buf_offset - f->buf_size + f->buf_index;
676}
677
678int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
679{
680 if (whence == SEEK_SET) {
681 /* nothing to do */
682 } else if (whence == SEEK_CUR) {
683 pos += qemu_ftell(f);
684 } else {
685 /* SEEK_END not supported */
686 return -1;
687 }
688 if (f->put_buffer) {
689 qemu_fflush(f);
690 f->buf_offset = pos;
691 } else {
692 f->buf_offset = pos;
693 f->buf_index = 0;
694 f->buf_size = 0;
695 }
696 return pos;
697}
698
699int qemu_file_rate_limit(QEMUFile *f)
700{
701 if (f->rate_limit)
702 return f->rate_limit(f->opaque);
703
704 return 0;
705}
706
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200707int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200708{
709 if (f->get_rate_limit)
710 return f->get_rate_limit(f->opaque);
711
712 return 0;
713}
714
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200715int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
Glauber Costa19629532009-05-20 18:26:57 -0400716{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400717 /* any failed or completed migration keeps its state to allow probing of
718 * migration data, but has no associated file anymore */
719 if (f && f->set_rate_limit)
Glauber Costa19629532009-05-20 18:26:57 -0400720 return f->set_rate_limit(f->opaque, new_rate);
721
722 return 0;
723}
724
aliguoria672b462008-11-11 21:33:36 +0000725void qemu_put_be16(QEMUFile *f, unsigned int v)
726{
727 qemu_put_byte(f, v >> 8);
728 qemu_put_byte(f, v);
729}
730
731void qemu_put_be32(QEMUFile *f, unsigned int v)
732{
733 qemu_put_byte(f, v >> 24);
734 qemu_put_byte(f, v >> 16);
735 qemu_put_byte(f, v >> 8);
736 qemu_put_byte(f, v);
737}
738
739void qemu_put_be64(QEMUFile *f, uint64_t v)
740{
741 qemu_put_be32(f, v >> 32);
742 qemu_put_be32(f, v);
743}
744
745unsigned int qemu_get_be16(QEMUFile *f)
746{
747 unsigned int v;
748 v = qemu_get_byte(f) << 8;
749 v |= qemu_get_byte(f);
750 return v;
751}
752
753unsigned int qemu_get_be32(QEMUFile *f)
754{
755 unsigned int v;
756 v = qemu_get_byte(f) << 24;
757 v |= qemu_get_byte(f) << 16;
758 v |= qemu_get_byte(f) << 8;
759 v |= qemu_get_byte(f);
760 return v;
761}
762
763uint64_t qemu_get_be64(QEMUFile *f)
764{
765 uint64_t v;
766 v = (uint64_t)qemu_get_be32(f) << 32;
767 v |= qemu_get_be32(f);
768 return v;
769}
770
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200771
772/* timer */
773
774void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
775{
776 uint64_t expire_time;
777
778 expire_time = qemu_timer_expire_time_ns(ts);
779 qemu_put_be64(f, expire_time);
780}
781
782void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
783{
784 uint64_t expire_time;
785
786 expire_time = qemu_get_be64(f);
787 if (expire_time != -1) {
788 qemu_mod_timer_ns(ts, expire_time);
789 } else {
790 qemu_del_timer(ts);
791 }
792}
793
794
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100795/* bool */
796
797static int get_bool(QEMUFile *f, void *pv, size_t size)
798{
799 bool *v = pv;
800 *v = qemu_get_byte(f);
801 return 0;
802}
803
804static void put_bool(QEMUFile *f, void *pv, size_t size)
805{
806 bool *v = pv;
807 qemu_put_byte(f, *v);
808}
809
810const VMStateInfo vmstate_info_bool = {
811 .name = "bool",
812 .get = get_bool,
813 .put = put_bool,
814};
815
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200816/* 8 bit int */
817
818static int get_int8(QEMUFile *f, void *pv, size_t size)
819{
820 int8_t *v = pv;
821 qemu_get_s8s(f, v);
822 return 0;
823}
824
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200825static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200826{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200827 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200828 qemu_put_s8s(f, v);
829}
830
831const VMStateInfo vmstate_info_int8 = {
832 .name = "int8",
833 .get = get_int8,
834 .put = put_int8,
835};
836
837/* 16 bit int */
838
839static int get_int16(QEMUFile *f, void *pv, size_t size)
840{
841 int16_t *v = pv;
842 qemu_get_sbe16s(f, v);
843 return 0;
844}
845
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200846static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200847{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200848 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200849 qemu_put_sbe16s(f, v);
850}
851
852const VMStateInfo vmstate_info_int16 = {
853 .name = "int16",
854 .get = get_int16,
855 .put = put_int16,
856};
857
858/* 32 bit int */
859
860static int get_int32(QEMUFile *f, void *pv, size_t size)
861{
862 int32_t *v = pv;
863 qemu_get_sbe32s(f, v);
864 return 0;
865}
866
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200867static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200868{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200869 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200870 qemu_put_sbe32s(f, v);
871}
872
873const VMStateInfo vmstate_info_int32 = {
874 .name = "int32",
875 .get = get_int32,
876 .put = put_int32,
877};
878
Juan Quintela82501662009-08-20 19:42:32 +0200879/* 32 bit int. See that the received value is the same than the one
880 in the field */
881
882static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
883{
884 int32_t *v = pv;
885 int32_t v2;
886 qemu_get_sbe32s(f, &v2);
887
888 if (*v == v2)
889 return 0;
890 return -EINVAL;
891}
892
893const VMStateInfo vmstate_info_int32_equal = {
894 .name = "int32 equal",
895 .get = get_int32_equal,
896 .put = put_int32,
897};
898
Juan Quintela0a031e02009-08-20 19:42:37 +0200899/* 32 bit int. See that the received value is the less or the same
900 than the one in the field */
901
902static int get_int32_le(QEMUFile *f, void *pv, size_t size)
903{
904 int32_t *old = pv;
905 int32_t new;
906 qemu_get_sbe32s(f, &new);
907
908 if (*old <= new)
909 return 0;
910 return -EINVAL;
911}
912
913const VMStateInfo vmstate_info_int32_le = {
914 .name = "int32 equal",
915 .get = get_int32_le,
916 .put = put_int32,
917};
918
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200919/* 64 bit int */
920
921static int get_int64(QEMUFile *f, void *pv, size_t size)
922{
923 int64_t *v = pv;
924 qemu_get_sbe64s(f, v);
925 return 0;
926}
927
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200928static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200929{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200930 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200931 qemu_put_sbe64s(f, v);
932}
933
934const VMStateInfo vmstate_info_int64 = {
935 .name = "int64",
936 .get = get_int64,
937 .put = put_int64,
938};
939
940/* 8 bit unsigned int */
941
942static int get_uint8(QEMUFile *f, void *pv, size_t size)
943{
944 uint8_t *v = pv;
945 qemu_get_8s(f, v);
946 return 0;
947}
948
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200949static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200950{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200951 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200952 qemu_put_8s(f, v);
953}
954
955const VMStateInfo vmstate_info_uint8 = {
956 .name = "uint8",
957 .get = get_uint8,
958 .put = put_uint8,
959};
960
961/* 16 bit unsigned int */
962
963static int get_uint16(QEMUFile *f, void *pv, size_t size)
964{
965 uint16_t *v = pv;
966 qemu_get_be16s(f, v);
967 return 0;
968}
969
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200970static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200971{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200972 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200973 qemu_put_be16s(f, v);
974}
975
976const VMStateInfo vmstate_info_uint16 = {
977 .name = "uint16",
978 .get = get_uint16,
979 .put = put_uint16,
980};
981
982/* 32 bit unsigned int */
983
984static int get_uint32(QEMUFile *f, void *pv, size_t size)
985{
986 uint32_t *v = pv;
987 qemu_get_be32s(f, v);
988 return 0;
989}
990
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200991static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200992{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200993 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200994 qemu_put_be32s(f, v);
995}
996
997const VMStateInfo vmstate_info_uint32 = {
998 .name = "uint32",
999 .get = get_uint32,
1000 .put = put_uint32,
1001};
1002
Juan Quintela9122a8f2011-03-10 12:33:48 +01001003/* 32 bit uint. See that the received value is the same than the one
1004 in the field */
1005
1006static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1007{
1008 uint32_t *v = pv;
1009 uint32_t v2;
1010 qemu_get_be32s(f, &v2);
1011
1012 if (*v == v2) {
1013 return 0;
1014 }
1015 return -EINVAL;
1016}
1017
1018const VMStateInfo vmstate_info_uint32_equal = {
1019 .name = "uint32 equal",
1020 .get = get_uint32_equal,
1021 .put = put_uint32,
1022};
1023
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001024/* 64 bit unsigned int */
1025
1026static int get_uint64(QEMUFile *f, void *pv, size_t size)
1027{
1028 uint64_t *v = pv;
1029 qemu_get_be64s(f, v);
1030 return 0;
1031}
1032
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001033static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001034{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001035 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001036 qemu_put_be64s(f, v);
1037}
1038
1039const VMStateInfo vmstate_info_uint64 = {
1040 .name = "uint64",
1041 .get = get_uint64,
1042 .put = put_uint64,
1043};
1044
Juan Quintela80cd83e2009-09-10 03:04:36 +02001045/* 8 bit int. See that the received value is the same than the one
1046 in the field */
1047
1048static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1049{
1050 uint8_t *v = pv;
1051 uint8_t v2;
1052 qemu_get_8s(f, &v2);
1053
1054 if (*v == v2)
1055 return 0;
1056 return -EINVAL;
1057}
1058
1059const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001060 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001061 .get = get_uint8_equal,
1062 .put = put_uint8,
1063};
1064
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001065/* 16 bit unsigned int int. See that the received value is the same than the one
1066 in the field */
1067
1068static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1069{
1070 uint16_t *v = pv;
1071 uint16_t v2;
1072 qemu_get_be16s(f, &v2);
1073
1074 if (*v == v2)
1075 return 0;
1076 return -EINVAL;
1077}
1078
1079const VMStateInfo vmstate_info_uint16_equal = {
1080 .name = "uint16 equal",
1081 .get = get_uint16_equal,
1082 .put = put_uint16,
1083};
1084
Juan Quinteladde04632009-08-20 19:42:26 +02001085/* timers */
1086
1087static int get_timer(QEMUFile *f, void *pv, size_t size)
1088{
1089 QEMUTimer *v = pv;
1090 qemu_get_timer(f, v);
1091 return 0;
1092}
1093
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001094static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001095{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001096 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001097 qemu_put_timer(f, v);
1098}
1099
1100const VMStateInfo vmstate_info_timer = {
1101 .name = "timer",
1102 .get = get_timer,
1103 .put = put_timer,
1104};
1105
Juan Quintela6f67c502009-08-20 19:42:35 +02001106/* uint8_t buffers */
1107
1108static int get_buffer(QEMUFile *f, void *pv, size_t size)
1109{
1110 uint8_t *v = pv;
1111 qemu_get_buffer(f, v, size);
1112 return 0;
1113}
1114
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001115static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001116{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001117 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001118 qemu_put_buffer(f, v, size);
1119}
1120
1121const VMStateInfo vmstate_info_buffer = {
1122 .name = "buffer",
1123 .get = get_buffer,
1124 .put = put_buffer,
1125};
1126
Juan Quintela76507c72009-10-19 15:46:28 +02001127/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001128 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001129
1130static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1131{
Jan Kiszka21174c32009-12-02 12:36:35 +01001132 uint8_t buf[1024];
1133 int block_len;
1134
1135 while (size > 0) {
1136 block_len = MIN(sizeof(buf), size);
1137 size -= block_len;
1138 qemu_get_buffer(f, buf, block_len);
1139 }
1140 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001141}
1142
1143static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1144{
Jan Kiszka21174c32009-12-02 12:36:35 +01001145 static const uint8_t buf[1024];
1146 int block_len;
1147
1148 while (size > 0) {
1149 block_len = MIN(sizeof(buf), size);
1150 size -= block_len;
1151 qemu_put_buffer(f, buf, block_len);
1152 }
Juan Quintela76507c72009-10-19 15:46:28 +02001153}
1154
1155const VMStateInfo vmstate_info_unused_buffer = {
1156 .name = "unused_buffer",
1157 .get = get_unused_buffer,
1158 .put = put_unused_buffer,
1159};
1160
Alex Williamson7685ee62010-06-25 11:09:14 -06001161typedef struct CompatEntry {
1162 char idstr[256];
1163 int instance_id;
1164} CompatEntry;
1165
aliguoria672b462008-11-11 21:33:36 +00001166typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001167 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001168 char idstr[256];
1169 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001170 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001171 int version_id;
1172 int section_id;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001173 SaveSetParamsHandler *set_params;
aliguoria672b462008-11-11 21:33:36 +00001174 SaveLiveStateHandler *save_live_state;
1175 SaveStateHandler *save_state;
1176 LoadStateHandler *load_state;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001177 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001178 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001179 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001180 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001181 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001182} SaveStateEntry;
1183
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001184
Blue Swirl72cf2d42009-09-12 07:36:22 +00001185static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1186 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001187static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001188
Juan Quintela8718e992009-09-01 02:12:31 +02001189static int calculate_new_instance_id(const char *idstr)
1190{
1191 SaveStateEntry *se;
1192 int instance_id = 0;
1193
Blue Swirl72cf2d42009-09-12 07:36:22 +00001194 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001195 if (strcmp(idstr, se->idstr) == 0
1196 && instance_id <= se->instance_id) {
1197 instance_id = se->instance_id + 1;
1198 }
1199 }
1200 return instance_id;
1201}
1202
Alex Williamson7685ee62010-06-25 11:09:14 -06001203static int calculate_compat_instance_id(const char *idstr)
1204{
1205 SaveStateEntry *se;
1206 int instance_id = 0;
1207
1208 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1209 if (!se->compat)
1210 continue;
1211
1212 if (strcmp(idstr, se->compat->idstr) == 0
1213 && instance_id <= se->compat->instance_id) {
1214 instance_id = se->compat->instance_id + 1;
1215 }
1216 }
1217 return instance_id;
1218}
1219
aliguoria672b462008-11-11 21:33:36 +00001220/* TODO: Individual devices generally have very little idea about the rest
1221 of the system, so instance_id should be removed/replaced.
1222 Meanwhile pass -1 as instance_id if you do not already have a clearly
1223 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001224int register_savevm_live(DeviceState *dev,
1225 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001226 int instance_id,
1227 int version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001228 SaveSetParamsHandler *set_params,
aliguoria672b462008-11-11 21:33:36 +00001229 SaveLiveStateHandler *save_live_state,
1230 SaveStateHandler *save_state,
1231 LoadStateHandler *load_state,
1232 void *opaque)
1233{
Juan Quintela8718e992009-09-01 02:12:31 +02001234 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001235
Anthony Liguori7267c092011-08-20 22:09:37 -05001236 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001237 se->version_id = version_id;
1238 se->section_id = global_section_id++;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001239 se->set_params = set_params;
aliguoria672b462008-11-11 21:33:36 +00001240 se->save_live_state = save_live_state;
1241 se->save_state = save_state;
1242 se->load_state = load_state;
1243 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001244 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001245 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001246 /* if this is a live_savem then set is_ram */
1247 if (save_live_state != NULL) {
1248 se->is_ram = 1;
1249 }
aliguoria672b462008-11-11 21:33:36 +00001250
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001251 if (dev) {
1252 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001253 if (id) {
1254 pstrcpy(se->idstr, sizeof(se->idstr), id);
1255 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001256 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001257
Anthony Liguori7267c092011-08-20 22:09:37 -05001258 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001259 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1260 se->compat->instance_id = instance_id == -1 ?
1261 calculate_compat_instance_id(idstr) : instance_id;
1262 instance_id = -1;
1263 }
1264 }
1265 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1266
Juan Quintela8718e992009-09-01 02:12:31 +02001267 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001268 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001269 } else {
1270 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001271 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001272 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001273 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001274 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001275 return 0;
1276}
1277
Alex Williamson0be71e32010-06-25 11:09:07 -06001278int register_savevm(DeviceState *dev,
1279 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001280 int instance_id,
1281 int version_id,
1282 SaveStateHandler *save_state,
1283 LoadStateHandler *load_state,
1284 void *opaque)
1285{
Alex Williamson0be71e32010-06-25 11:09:07 -06001286 return register_savevm_live(dev, idstr, instance_id, version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001287 NULL, NULL, save_state, load_state, opaque);
aliguoria672b462008-11-11 21:33:36 +00001288}
1289
Alex Williamson0be71e32010-06-25 11:09:07 -06001290void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001291{
Juan Quintela8718e992009-09-01 02:12:31 +02001292 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001293 char id[256] = "";
1294
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001295 if (dev) {
1296 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001297 if (path) {
1298 pstrcpy(id, sizeof(id), path);
1299 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001300 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001301 }
1302 }
1303 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001304
Blue Swirl72cf2d42009-09-12 07:36:22 +00001305 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001306 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001307 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001308 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001309 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001310 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001311 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001312 }
aliguori41bd13a2009-04-17 17:10:59 +00001313 }
1314}
1315
Alex Williamson0be71e32010-06-25 11:09:07 -06001316int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001317 const VMStateDescription *vmsd,
1318 void *opaque, int alias_id,
1319 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001320{
Juan Quintela8718e992009-09-01 02:12:31 +02001321 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001322
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001323 /* If this triggers, alias support can be dropped for the vmsd. */
1324 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1325
Anthony Liguori7267c092011-08-20 22:09:37 -05001326 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001327 se->version_id = vmsd->version_id;
1328 se->section_id = global_section_id++;
1329 se->save_live_state = NULL;
1330 se->save_state = NULL;
1331 se->load_state = NULL;
1332 se->opaque = opaque;
1333 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001334 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001335 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001336
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001337 if (dev) {
1338 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001339 if (id) {
1340 pstrcpy(se->idstr, sizeof(se->idstr), id);
1341 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001342 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001343
Anthony Liguori7267c092011-08-20 22:09:37 -05001344 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001345 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1346 se->compat->instance_id = instance_id == -1 ?
1347 calculate_compat_instance_id(vmsd->name) : instance_id;
1348 instance_id = -1;
1349 }
1350 }
1351 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1352
Juan Quintela8718e992009-09-01 02:12:31 +02001353 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001354 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001355 } else {
1356 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001357 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001358 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001359 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001360 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001361 return 0;
1362}
1363
Alex Williamson0be71e32010-06-25 11:09:07 -06001364int vmstate_register(DeviceState *dev, int instance_id,
1365 const VMStateDescription *vmsd, void *opaque)
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001366{
Alex Williamson0be71e32010-06-25 11:09:07 -06001367 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1368 opaque, -1, 0);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001369}
1370
Alex Williamson0be71e32010-06-25 11:09:07 -06001371void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1372 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001373{
Juan Quintela1eb75382009-09-10 03:04:29 +02001374 SaveStateEntry *se, *new_se;
1375
Blue Swirl72cf2d42009-09-12 07:36:22 +00001376 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001377 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001378 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001379 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001380 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001381 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001382 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001383 }
1384 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001385}
1386
Juan Quintela811814b2010-07-26 21:38:43 +02001387static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1388 void *opaque);
1389static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1390 void *opaque);
1391
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001392int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1393 void *opaque, int version_id)
1394{
1395 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001396 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001397
1398 if (version_id > vmsd->version_id) {
1399 return -EINVAL;
1400 }
1401 if (version_id < vmsd->minimum_version_id_old) {
1402 return -EINVAL;
1403 }
1404 if (version_id < vmsd->minimum_version_id) {
1405 return vmsd->load_state_old(f, opaque, version_id);
1406 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001407 if (vmsd->pre_load) {
1408 int ret = vmsd->pre_load(opaque);
1409 if (ret)
1410 return ret;
1411 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001412 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001413 if ((field->field_exists &&
1414 field->field_exists(opaque, version_id)) ||
1415 (!field->field_exists &&
1416 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001417 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001418 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001419 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001420
Juan Quintelae61a1e02009-12-02 12:36:38 +01001421 if (field->flags & VMS_VBUFFER) {
1422 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001423 if (field->flags & VMS_MULTIPLY) {
1424 size *= field->size;
1425 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001426 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001427 if (field->flags & VMS_ARRAY) {
1428 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001429 } else if (field->flags & VMS_VARRAY_INT32) {
1430 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001431 } else if (field->flags & VMS_VARRAY_UINT32) {
1432 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001433 } else if (field->flags & VMS_VARRAY_UINT16) {
1434 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001435 } else if (field->flags & VMS_VARRAY_UINT8) {
1436 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001437 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001438 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001439 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001440 }
1441 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001442 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001443
Juan Quintela19df4382009-09-29 22:48:41 +02001444 if (field->flags & VMS_ARRAY_OF_POINTER) {
1445 addr = *(void **)addr;
1446 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001447 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001448 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001449 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001450 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001451
1452 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001453 if (ret < 0) {
1454 return ret;
1455 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001456 }
1457 }
1458 field++;
1459 }
Juan Quintela811814b2010-07-26 21:38:43 +02001460 ret = vmstate_subsection_load(f, vmsd, opaque);
1461 if (ret != 0) {
1462 return ret;
1463 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001464 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001465 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001466 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001467 return 0;
1468}
1469
1470void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001471 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001472{
1473 VMStateField *field = vmsd->fields;
1474
Juan Quintela8fb07912009-09-10 03:04:32 +02001475 if (vmsd->pre_save) {
1476 vmsd->pre_save(opaque);
1477 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001478 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001479 if (!field->field_exists ||
1480 field->field_exists(opaque, vmsd->version_id)) {
1481 void *base_addr = opaque + field->offset;
1482 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001483 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001484
Juan Quintelae61a1e02009-12-02 12:36:38 +01001485 if (field->flags & VMS_VBUFFER) {
1486 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001487 if (field->flags & VMS_MULTIPLY) {
1488 size *= field->size;
1489 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001490 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001491 if (field->flags & VMS_ARRAY) {
1492 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001493 } else if (field->flags & VMS_VARRAY_INT32) {
1494 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001495 } else if (field->flags & VMS_VARRAY_UINT32) {
1496 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001497 } else if (field->flags & VMS_VARRAY_UINT16) {
1498 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001499 } else if (field->flags & VMS_VARRAY_UINT8) {
1500 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001501 }
1502 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001503 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001504 }
1505 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001506 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001507
Juan Quintela85953872009-12-02 12:36:37 +01001508 if (field->flags & VMS_ARRAY_OF_POINTER) {
1509 addr = *(void **)addr;
1510 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001511 if (field->flags & VMS_STRUCT) {
1512 vmstate_save_state(f, field->vmsd, addr);
1513 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001514 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001515 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001516 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001517 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001518 field++;
1519 }
Juan Quintela811814b2010-07-26 21:38:43 +02001520 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001521}
1522
Juan Quintela4082be42009-08-20 19:42:24 +02001523static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1524{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001525 if (!se->vmsd) { /* Old style */
1526 return se->load_state(f, se->opaque, version_id);
1527 }
1528 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001529}
1530
Alex Williamsondc912122011-01-11 14:39:43 -07001531static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001532{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001533 if (!se->vmsd) { /* Old style */
1534 se->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001535 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001536 }
1537 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001538}
1539
aliguoria672b462008-11-11 21:33:36 +00001540#define QEMU_VM_FILE_MAGIC 0x5145564d
1541#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1542#define QEMU_VM_FILE_VERSION 0x00000003
1543
1544#define QEMU_VM_EOF 0x00
1545#define QEMU_VM_SECTION_START 0x01
1546#define QEMU_VM_SECTION_PART 0x02
1547#define QEMU_VM_SECTION_END 0x03
1548#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001549#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001550
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001551bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001552{
1553 SaveStateEntry *se;
1554
1555 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1556 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001557 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001558 return true;
1559 }
1560 }
1561 return false;
1562}
1563
Luiz Capitulino539de122011-12-05 14:06:56 -02001564int qemu_savevm_state_begin(QEMUFile *f, int blk_enable, int shared)
aliguoria672b462008-11-11 21:33:36 +00001565{
1566 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001567 int ret;
aliguoria672b462008-11-11 21:33:36 +00001568
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001569 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1570 if(se->set_params == NULL) {
1571 continue;
1572 }
1573 se->set_params(blk_enable, shared, se->opaque);
1574 }
1575
aliguoria672b462008-11-11 21:33:36 +00001576 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1577 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1578
Blue Swirl72cf2d42009-09-12 07:36:22 +00001579 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001580 int len;
1581
1582 if (se->save_live_state == NULL)
1583 continue;
1584
1585 /* Section type */
1586 qemu_put_byte(f, QEMU_VM_SECTION_START);
1587 qemu_put_be32(f, se->section_id);
1588
1589 /* ID string */
1590 len = strlen(se->idstr);
1591 qemu_put_byte(f, len);
1592 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1593
1594 qemu_put_be32(f, se->instance_id);
1595 qemu_put_be32(f, se->version_id);
1596
Luiz Capitulino539de122011-12-05 14:06:56 -02001597 ret = se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001598 if (ret < 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001599 qemu_savevm_state_cancel(f);
Juan Quintela29757252011-10-19 15:22:18 +02001600 return ret;
1601 }
aliguoria672b462008-11-11 21:33:36 +00001602 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001603 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001604 if (ret != 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001605 qemu_savevm_state_cancel(f);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001606 }
aliguoria672b462008-11-11 21:33:36 +00001607
Juan Quintela39346382011-09-22 11:02:14 +02001608 return ret;
1609
aliguoria672b462008-11-11 21:33:36 +00001610}
1611
Juan Quintela39346382011-09-22 11:02:14 +02001612/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001613 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001614 * negative: there was one error, and we have -errno.
1615 * 0 : We haven't finished, caller have to go again
1616 * 1 : We have finished, we can go to complete phase
1617 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001618int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001619{
1620 SaveStateEntry *se;
1621 int ret = 1;
1622
Blue Swirl72cf2d42009-09-12 07:36:22 +00001623 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001624 if (se->save_live_state == NULL)
1625 continue;
1626
1627 /* Section type */
1628 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1629 qemu_put_be32(f, se->section_id);
1630
Luiz Capitulino539de122011-12-05 14:06:56 -02001631 ret = se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001632 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001633 /* Do not proceed to the next vmstate before this one reported
1634 completion of the current stage. This serializes the migration
1635 and reduces the probability that a faster changing state is
1636 synchronized over and over again. */
1637 break;
1638 }
aliguoria672b462008-11-11 21:33:36 +00001639 }
Juan Quintela39346382011-09-22 11:02:14 +02001640 if (ret != 0) {
1641 return ret;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001642 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001643 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001644 if (ret != 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001645 qemu_savevm_state_cancel(f);
Juan Quintela39346382011-09-22 11:02:14 +02001646 }
1647 return ret;
aliguoria672b462008-11-11 21:33:36 +00001648}
1649
Luiz Capitulino539de122011-12-05 14:06:56 -02001650int qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001651{
1652 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001653 int ret;
aliguoria672b462008-11-11 21:33:36 +00001654
Jan Kiszkaea375f92010-03-01 19:10:30 +01001655 cpu_synchronize_all_states();
1656
Blue Swirl72cf2d42009-09-12 07:36:22 +00001657 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001658 if (se->save_live_state == NULL)
1659 continue;
1660
1661 /* Section type */
1662 qemu_put_byte(f, QEMU_VM_SECTION_END);
1663 qemu_put_be32(f, se->section_id);
1664
Luiz Capitulino539de122011-12-05 14:06:56 -02001665 ret = se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001666 if (ret < 0) {
1667 return ret;
1668 }
aliguoria672b462008-11-11 21:33:36 +00001669 }
1670
Blue Swirl72cf2d42009-09-12 07:36:22 +00001671 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001672 int len;
1673
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001674 if (se->save_state == NULL && se->vmsd == NULL)
aliguoria672b462008-11-11 21:33:36 +00001675 continue;
1676
1677 /* Section type */
1678 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1679 qemu_put_be32(f, se->section_id);
1680
1681 /* ID string */
1682 len = strlen(se->idstr);
1683 qemu_put_byte(f, len);
1684 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1685
1686 qemu_put_be32(f, se->instance_id);
1687 qemu_put_be32(f, se->version_id);
1688
Alex Williamsondc912122011-01-11 14:39:43 -07001689 vmstate_save(f, se);
aliguoria672b462008-11-11 21:33:36 +00001690 }
1691
1692 qemu_put_byte(f, QEMU_VM_EOF);
1693
Juan Quintela624b9cc2011-10-05 01:02:52 +02001694 return qemu_file_get_error(f);
aliguoria672b462008-11-11 21:33:36 +00001695}
1696
Luiz Capitulino539de122011-12-05 14:06:56 -02001697void qemu_savevm_state_cancel(QEMUFile *f)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001698{
1699 SaveStateEntry *se;
1700
1701 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1702 if (se->save_live_state) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001703 se->save_live_state(f, -1, se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001704 }
1705 }
1706}
1707
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001708static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001709{
aliguoria672b462008-11-11 21:33:36 +00001710 int ret;
1711
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001712 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07001713 ret = -EINVAL;
1714 goto out;
1715 }
1716
Luiz Capitulino539de122011-12-05 14:06:56 -02001717 ret = qemu_savevm_state_begin(f, 0, 0);
aliguoria672b462008-11-11 21:33:36 +00001718 if (ret < 0)
1719 goto out;
1720
1721 do {
Luiz Capitulino539de122011-12-05 14:06:56 -02001722 ret = qemu_savevm_state_iterate(f);
aliguoria672b462008-11-11 21:33:36 +00001723 if (ret < 0)
1724 goto out;
1725 } while (ret == 0);
1726
Luiz Capitulino539de122011-12-05 14:06:56 -02001727 ret = qemu_savevm_state_complete(f);
aliguoria672b462008-11-11 21:33:36 +00001728
1729out:
Juan Quintela39346382011-09-22 11:02:14 +02001730 if (ret == 0) {
Juan Quintela624b9cc2011-10-05 01:02:52 +02001731 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001732 }
aliguoria672b462008-11-11 21:33:36 +00001733
aliguoria672b462008-11-11 21:33:36 +00001734 return ret;
1735}
1736
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001737static int qemu_save_device_state(QEMUFile *f)
1738{
1739 SaveStateEntry *se;
1740
1741 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1742 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1743
1744 cpu_synchronize_all_states();
1745
1746 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1747 int len;
1748
1749 if (se->is_ram) {
1750 continue;
1751 }
1752 if (se->save_state == NULL && se->vmsd == NULL) {
1753 continue;
1754 }
1755
1756 /* Section type */
1757 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1758 qemu_put_be32(f, se->section_id);
1759
1760 /* ID string */
1761 len = strlen(se->idstr);
1762 qemu_put_byte(f, len);
1763 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1764
1765 qemu_put_be32(f, se->instance_id);
1766 qemu_put_be32(f, se->version_id);
1767
1768 vmstate_save(f, se);
1769 }
1770
1771 qemu_put_byte(f, QEMU_VM_EOF);
1772
1773 return qemu_file_get_error(f);
1774}
1775
aliguoria672b462008-11-11 21:33:36 +00001776static SaveStateEntry *find_se(const char *idstr, int instance_id)
1777{
1778 SaveStateEntry *se;
1779
Blue Swirl72cf2d42009-09-12 07:36:22 +00001780 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001781 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001782 (instance_id == se->instance_id ||
1783 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001784 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001785 /* Migrating from an older version? */
1786 if (strstr(se->idstr, idstr) && se->compat) {
1787 if (!strcmp(se->compat->idstr, idstr) &&
1788 (instance_id == se->compat->instance_id ||
1789 instance_id == se->alias_id))
1790 return se;
1791 }
aliguoria672b462008-11-11 21:33:36 +00001792 }
1793 return NULL;
1794}
1795
Juan Quintela811814b2010-07-26 21:38:43 +02001796static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1797{
1798 while(sub && sub->needed) {
1799 if (strcmp(idstr, sub->vmsd->name) == 0) {
1800 return sub->vmsd;
1801 }
1802 sub++;
1803 }
1804 return NULL;
1805}
1806
1807static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1808 void *opaque)
1809{
Juan Quintelac6380722011-10-04 15:28:31 +02001810 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001811 char idstr[256];
1812 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001813 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001814 const VMStateDescription *sub_vmsd;
1815
Juan Quintelac6380722011-10-04 15:28:31 +02001816 len = qemu_peek_byte(f, 1);
1817 if (len < strlen(vmsd->name) + 1) {
1818 /* subsection name has be be "section_name/a" */
1819 return 0;
1820 }
1821 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1822 if (size != len) {
1823 return 0;
1824 }
1825 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02001826
Juan Quintelac6380722011-10-04 15:28:31 +02001827 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1828 /* it don't have a valid subsection name */
1829 return 0;
1830 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02001831 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02001832 if (sub_vmsd == NULL) {
1833 return -ENOENT;
1834 }
Juan Quintelac6380722011-10-04 15:28:31 +02001835 qemu_file_skip(f, 1); /* subsection */
1836 qemu_file_skip(f, 1); /* len */
1837 qemu_file_skip(f, len); /* idstr */
1838 version_id = qemu_get_be32(f);
1839
Juan Quintela811814b2010-07-26 21:38:43 +02001840 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1841 if (ret) {
1842 return ret;
1843 }
1844 }
1845 return 0;
1846}
1847
1848static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1849 void *opaque)
1850{
1851 const VMStateSubsection *sub = vmsd->subsections;
1852
1853 while (sub && sub->needed) {
1854 if (sub->needed(opaque)) {
1855 const VMStateDescription *vmsd = sub->vmsd;
1856 uint8_t len;
1857
1858 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1859 len = strlen(vmsd->name);
1860 qemu_put_byte(f, len);
1861 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1862 qemu_put_be32(f, vmsd->version_id);
1863 vmstate_save_state(f, vmsd, opaque);
1864 }
1865 sub++;
1866 }
1867}
1868
aliguoria672b462008-11-11 21:33:36 +00001869typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001870 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001871 SaveStateEntry *se;
1872 int section_id;
1873 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001874} LoadStateEntry;
1875
aliguoria672b462008-11-11 21:33:36 +00001876int qemu_loadvm_state(QEMUFile *f)
1877{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001878 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1879 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001880 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001881 uint8_t section_type;
1882 unsigned int v;
1883 int ret;
1884
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001885 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07001886 return -EINVAL;
1887 }
1888
aliguoria672b462008-11-11 21:33:36 +00001889 v = qemu_get_be32(f);
1890 if (v != QEMU_VM_FILE_MAGIC)
1891 return -EINVAL;
1892
1893 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001894 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1895 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1896 return -ENOTSUP;
1897 }
aliguoria672b462008-11-11 21:33:36 +00001898 if (v != QEMU_VM_FILE_VERSION)
1899 return -ENOTSUP;
1900
1901 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1902 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001903 SaveStateEntry *se;
1904 char idstr[257];
1905 int len;
1906
1907 switch (section_type) {
1908 case QEMU_VM_SECTION_START:
1909 case QEMU_VM_SECTION_FULL:
1910 /* Read section start */
1911 section_id = qemu_get_be32(f);
1912 len = qemu_get_byte(f);
1913 qemu_get_buffer(f, (uint8_t *)idstr, len);
1914 idstr[len] = 0;
1915 instance_id = qemu_get_be32(f);
1916 version_id = qemu_get_be32(f);
1917
1918 /* Find savevm section */
1919 se = find_se(idstr, instance_id);
1920 if (se == NULL) {
1921 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1922 ret = -EINVAL;
1923 goto out;
1924 }
1925
1926 /* Validate version */
1927 if (version_id > se->version_id) {
1928 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1929 version_id, idstr, se->version_id);
1930 ret = -EINVAL;
1931 goto out;
1932 }
1933
1934 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05001935 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00001936
1937 le->se = se;
1938 le->section_id = section_id;
1939 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001940 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00001941
Juan Quintela4082be42009-08-20 19:42:24 +02001942 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001943 if (ret < 0) {
1944 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1945 instance_id, idstr);
1946 goto out;
1947 }
aliguoria672b462008-11-11 21:33:36 +00001948 break;
1949 case QEMU_VM_SECTION_PART:
1950 case QEMU_VM_SECTION_END:
1951 section_id = qemu_get_be32(f);
1952
Blue Swirl72cf2d42009-09-12 07:36:22 +00001953 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001954 if (le->section_id == section_id) {
1955 break;
1956 }
1957 }
aliguoria672b462008-11-11 21:33:36 +00001958 if (le == NULL) {
1959 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1960 ret = -EINVAL;
1961 goto out;
1962 }
1963
Juan Quintela4082be42009-08-20 19:42:24 +02001964 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001965 if (ret < 0) {
1966 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1967 section_id);
1968 goto out;
1969 }
aliguoria672b462008-11-11 21:33:36 +00001970 break;
1971 default:
1972 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1973 ret = -EINVAL;
1974 goto out;
1975 }
1976 }
1977
Jan Kiszkaea375f92010-03-01 19:10:30 +01001978 cpu_synchronize_all_post_init();
1979
aliguoria672b462008-11-11 21:33:36 +00001980 ret = 0;
1981
1982out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00001983 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1984 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001985 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00001986 }
1987
Juan Quintela42802d42011-10-05 01:14:46 +02001988 if (ret == 0) {
1989 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001990 }
aliguoria672b462008-11-11 21:33:36 +00001991
1992 return ret;
1993}
1994
aliguoria672b462008-11-11 21:33:36 +00001995static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1996 const char *name)
1997{
1998 QEMUSnapshotInfo *sn_tab, *sn;
1999 int nb_sns, i, ret;
2000
2001 ret = -ENOENT;
2002 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2003 if (nb_sns < 0)
2004 return ret;
2005 for(i = 0; i < nb_sns; i++) {
2006 sn = &sn_tab[i];
2007 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2008 *sn_info = *sn;
2009 ret = 0;
2010 break;
2011 }
2012 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002013 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00002014 return ret;
2015}
2016
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002017/*
2018 * Deletes snapshots of a given name in all opened images.
2019 */
2020static int del_existing_snapshots(Monitor *mon, const char *name)
2021{
2022 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002023 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2024 int ret;
2025
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002026 bs = NULL;
2027 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002028 if (bdrv_can_snapshot(bs) &&
2029 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2030 {
2031 ret = bdrv_snapshot_delete(bs, name);
2032 if (ret < 0) {
2033 monitor_printf(mon,
2034 "Error while deleting snapshot on '%s'\n",
2035 bdrv_get_device_name(bs));
2036 return -1;
2037 }
2038 }
2039 }
2040
2041 return 0;
2042}
2043
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002044void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002045{
2046 BlockDriverState *bs, *bs1;
2047 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002048 int ret;
aliguoria672b462008-11-11 21:33:36 +00002049 QEMUFile *f;
2050 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002051 uint64_t vm_state_size;
aliguoria672b462008-11-11 21:33:36 +00002052#ifdef _WIN32
2053 struct _timeb tb;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002054 struct tm *ptm;
aliguoria672b462008-11-11 21:33:36 +00002055#else
2056 struct timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002057 struct tm tm;
aliguoria672b462008-11-11 21:33:36 +00002058#endif
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002059 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002060
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002061 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002062 bs = NULL;
2063 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002064
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002065 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002066 continue;
2067 }
2068
2069 if (!bdrv_can_snapshot(bs)) {
2070 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2071 bdrv_get_device_name(bs));
2072 return;
2073 }
2074 }
2075
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002076 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002077 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002078 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002079 return;
2080 }
aliguoria672b462008-11-11 21:33:36 +00002081
Luiz Capitulino13548692011-07-29 15:36:43 -03002082 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002083 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002084
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002085 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002086
2087 /* fill auxiliary fields */
2088#ifdef _WIN32
2089 _ftime(&tb);
2090 sn->date_sec = tb.time;
2091 sn->date_nsec = tb.millitm * 1000000;
2092#else
2093 gettimeofday(&tv, NULL);
2094 sn->date_sec = tv.tv_sec;
2095 sn->date_nsec = tv.tv_usec * 1000;
2096#endif
Paolo Bonzini74475452011-03-11 16:47:48 +01002097 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002098
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002099 if (name) {
2100 ret = bdrv_snapshot_find(bs, old_sn, name);
2101 if (ret >= 0) {
2102 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2103 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2104 } else {
2105 pstrcpy(sn->name, sizeof(sn->name), name);
2106 }
2107 } else {
2108#ifdef _WIN32
Stefan Weil55dd9ff2012-04-12 22:33:12 +02002109 time_t t = tb.time;
2110 ptm = localtime(&t);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002111 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2112#else
Blue Swirld7d9b522010-09-09 19:13:04 +00002113 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2114 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002115 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2116#endif
2117 }
2118
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002119 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002120 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002121 goto the_end;
2122 }
2123
aliguoria672b462008-11-11 21:33:36 +00002124 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002125 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002126 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002127 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002128 goto the_end;
2129 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002130 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002131 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002132 qemu_fclose(f);
2133 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002134 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002135 goto the_end;
2136 }
2137
2138 /* create the snapshots */
2139
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002140 bs1 = NULL;
2141 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002142 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002143 /* Write VM state size only to the image that contains the state */
2144 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002145 ret = bdrv_snapshot_create(bs1, sn);
2146 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002147 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2148 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002149 }
2150 }
2151 }
2152
2153 the_end:
2154 if (saved_vm_running)
2155 vm_start();
2156}
2157
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002158void qmp_xen_save_devices_state(const char *filename, Error **errp)
2159{
2160 QEMUFile *f;
2161 int saved_vm_running;
2162 int ret;
2163
2164 saved_vm_running = runstate_is_running();
2165 vm_stop(RUN_STATE_SAVE_VM);
2166
2167 f = qemu_fopen(filename, "wb");
2168 if (!f) {
2169 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2170 goto the_end;
2171 }
2172 ret = qemu_save_device_state(f);
2173 qemu_fclose(f);
2174 if (ret < 0) {
2175 error_set(errp, QERR_IO_ERROR);
2176 }
2177
2178 the_end:
2179 if (saved_vm_running)
2180 vm_start();
2181 return;
2182}
2183
Markus Armbruster03cd4652010-02-17 16:24:10 +01002184int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002185{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002186 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002187 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002188 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002189 int ret;
aliguoria672b462008-11-11 21:33:36 +00002190
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002191 bs_vm_state = bdrv_snapshots();
2192 if (!bs_vm_state) {
2193 error_report("No block device supports snapshots");
2194 return -ENOTSUP;
2195 }
2196
2197 /* Don't even try to load empty VM states */
2198 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2199 if (ret < 0) {
2200 return ret;
2201 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002202 error_report("This is a disk-only snapshot. Revert to it offline "
2203 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002204 return -EINVAL;
2205 }
2206
2207 /* Verify if there is any device that doesn't support snapshots and is
2208 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002209 bs = NULL;
2210 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002211
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002212 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002213 continue;
2214 }
2215
2216 if (!bdrv_can_snapshot(bs)) {
2217 error_report("Device '%s' is writable but does not support snapshots.",
2218 bdrv_get_device_name(bs));
2219 return -ENOTSUP;
2220 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002221
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002222 ret = bdrv_snapshot_find(bs, &sn, name);
2223 if (ret < 0) {
2224 error_report("Device '%s' does not have the requested snapshot '%s'",
2225 bdrv_get_device_name(bs), name);
2226 return ret;
2227 }
aliguoria672b462008-11-11 21:33:36 +00002228 }
2229
2230 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002231 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002232
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002233 bs = NULL;
2234 while ((bs = bdrv_next(bs))) {
2235 if (bdrv_can_snapshot(bs)) {
2236 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002237 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002238 error_report("Error %d while activating snapshot '%s' on '%s'",
2239 ret, name, bdrv_get_device_name(bs));
2240 return ret;
aliguoria672b462008-11-11 21:33:36 +00002241 }
2242 }
2243 }
2244
aliguoria672b462008-11-11 21:33:36 +00002245 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002246 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002247 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002248 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002249 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002250 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002251
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002252 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002253 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002254
aliguoria672b462008-11-11 21:33:36 +00002255 qemu_fclose(f);
2256 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002257 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002258 return ret;
aliguoria672b462008-11-11 21:33:36 +00002259 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002260
Juan Quintela05f24012009-08-20 19:42:22 +02002261 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002262}
2263
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002264void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002265{
2266 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002267 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002268 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002269
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002270 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002271 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002272 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002273 return;
2274 }
2275
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002276 bs1 = NULL;
2277 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002278 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002279 ret = bdrv_snapshot_delete(bs1, name);
2280 if (ret < 0) {
2281 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002282 monitor_printf(mon,
2283 "Snapshots not supported on device '%s'\n",
2284 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002285 else
aliguori376253e2009-03-05 23:01:23 +00002286 monitor_printf(mon, "Error %d while deleting snapshot on "
2287 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002288 }
2289 }
2290 }
2291}
2292
aliguori376253e2009-03-05 23:01:23 +00002293void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00002294{
2295 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002296 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2297 int nb_sns, i, ret, available;
2298 int total;
2299 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002300 char buf[256];
2301
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002302 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002303 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002304 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002305 return;
2306 }
aliguoria672b462008-11-11 21:33:36 +00002307
2308 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2309 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002310 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002311 return;
2312 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002313
2314 if (nb_sns == 0) {
2315 monitor_printf(mon, "There is no snapshot available.\n");
2316 return;
aliguoria672b462008-11-11 21:33:36 +00002317 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002318
Anthony Liguori7267c092011-08-20 22:09:37 -05002319 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002320 total = 0;
2321 for (i = 0; i < nb_sns; i++) {
2322 sn = &sn_tab[i];
2323 available = 1;
2324 bs1 = NULL;
2325
2326 while ((bs1 = bdrv_next(bs1))) {
2327 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2328 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2329 if (ret < 0) {
2330 available = 0;
2331 break;
2332 }
2333 }
2334 }
2335
2336 if (available) {
2337 available_snapshots[total] = i;
2338 total++;
2339 }
2340 }
2341
2342 if (total > 0) {
2343 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2344 for (i = 0; i < total; i++) {
2345 sn = &sn_tab[available_snapshots[i]];
2346 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2347 }
2348 } else {
2349 monitor_printf(mon, "There is no suitable snapshot available\n");
2350 }
2351
Anthony Liguori7267c092011-08-20 22:09:37 -05002352 g_free(sn_tab);
2353 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002354
aliguoria672b462008-11-11 21:33:36 +00002355}
Avi Kivityc5705a72011-12-20 15:59:12 +02002356
2357void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2358{
Avi Kivity1ddde082012-01-08 13:18:19 +02002359 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002360 memory_region_name(mr), dev);
2361}
2362
2363void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2364{
2365 /* Nothing do to while the implementation is in RAMBlock */
2366}
2367
2368void vmstate_register_ram_global(MemoryRegion *mr)
2369{
2370 vmstate_register_ram(mr, NULL);
2371}