aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <signal.h> |
| 27 | #include <time.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <zlib.h> |
| 31 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 32 | /* Needed early for CONFIG_BSD etc. */ |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 33 | #include "config-host.h" |
| 34 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 35 | #ifndef _WIN32 |
| 36 | #include <sys/times.h> |
| 37 | #include <sys/wait.h> |
| 38 | #include <termios.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/ioctl.h> |
| 41 | #include <sys/resource.h> |
| 42 | #include <sys/socket.h> |
| 43 | #include <netinet/in.h> |
| 44 | #include <net/if.h> |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 45 | #include <arpa/inet.h> |
| 46 | #include <dirent.h> |
| 47 | #include <netdb.h> |
| 48 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 49 | #ifdef CONFIG_BSD |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 50 | #include <sys/stat.h> |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 51 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 52 | #include <libutil.h> |
| 53 | #else |
| 54 | #include <util.h> |
| 55 | #endif |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 56 | #ifdef __linux__ |
| 57 | #include <pty.h> |
| 58 | #include <malloc.h> |
| 59 | #include <linux/rtc.h> |
| 60 | #endif |
| 61 | #endif |
| 62 | #endif |
| 63 | |
| 64 | #ifdef _WIN32 |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 65 | #include <windows.h> |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 66 | #include <malloc.h> |
| 67 | #include <sys/timeb.h> |
| 68 | #include <mmsystem.h> |
| 69 | #define getopt_long_only getopt_long |
| 70 | #define memalign(align, size) malloc(size) |
| 71 | #endif |
| 72 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 73 | #include "qemu-common.h" |
| 74 | #include "hw/hw.h" |
| 75 | #include "net.h" |
| 76 | #include "monitor.h" |
| 77 | #include "sysemu.h" |
| 78 | #include "qemu-timer.h" |
| 79 | #include "qemu-char.h" |
| 80 | #include "block.h" |
| 81 | #include "audio/audio.h" |
| 82 | #include "migration.h" |
| 83 | #include "qemu_socket.h" |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 84 | #include "qemu-queue.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 85 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 86 | /* point to the block driver where the snapshots are managed */ |
| 87 | static BlockDriverState *bs_snapshots; |
| 88 | |
| 89 | #define SELF_ANNOUNCE_ROUNDS 5 |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 90 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 91 | #ifndef ETH_P_RARP |
Stefan Berger | f8778a7 | 2010-04-24 08:54:07 -0400 | [diff] [blame] | 92 | #define ETH_P_RARP 0x8035 |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 93 | #endif |
| 94 | #define ARP_HTYPE_ETH 0x0001 |
| 95 | #define ARP_PTYPE_IP 0x0800 |
| 96 | #define ARP_OP_REQUEST_REV 0x3 |
| 97 | |
| 98 | static int announce_self_create(uint8_t *buf, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 99 | uint8_t *mac_addr) |
| 100 | { |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 101 | /* 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 */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 105 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 106 | /* 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 */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 116 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 117 | /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */ |
| 118 | memset(buf + 42, 0x00, 18); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 119 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 120 | return 60; /* len (FCS will be added by hardware) */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Mark McLoughlin | f401ca2 | 2009-11-25 18:49:32 +0000 | [diff] [blame] | 123 | static 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 Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 134 | static void qemu_announce_self_once(void *opaque) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 135 | { |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 136 | static int count = SELF_ANNOUNCE_ROUNDS; |
| 137 | QEMUTimer *timer = *(QEMUTimer **)opaque; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 138 | |
Mark McLoughlin | f401ca2 | 2009-11-25 18:49:32 +0000 | [diff] [blame] | 139 | qemu_foreach_nic(qemu_announce_self_iter, NULL); |
| 140 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 141 | if (--count) { |
| 142 | /* delay 50ms, 150ms, 250ms, ... */ |
| 143 | qemu_mod_timer(timer, qemu_get_clock(rt_clock) + |
| 144 | 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100); |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 145 | } else { |
| 146 | qemu_del_timer(timer); |
| 147 | qemu_free_timer(timer); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void qemu_announce_self(void) |
| 152 | { |
| 153 | static QEMUTimer *timer; |
| 154 | timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer); |
| 155 | qemu_announce_self_once(&timer); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /***********************************************************/ |
| 159 | /* savevm/loadvm support */ |
| 160 | |
| 161 | #define IO_BUF_SIZE 32768 |
| 162 | |
| 163 | struct QEMUFile { |
| 164 | QEMUFilePutBufferFunc *put_buffer; |
| 165 | QEMUFileGetBufferFunc *get_buffer; |
| 166 | QEMUFileCloseFunc *close; |
| 167 | QEMUFileRateLimit *rate_limit; |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 168 | QEMUFileSetRateLimit *set_rate_limit; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 169 | QEMUFileGetRateLimit *get_rate_limit; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 170 | 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 | |
| 179 | int has_error; |
| 180 | }; |
| 181 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 182 | typedef struct QEMUFileStdio |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 183 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 184 | FILE *stdio_file; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 185 | QEMUFile *file; |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 186 | } QEMUFileStdio; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 187 | |
| 188 | typedef struct QEMUFileSocket |
| 189 | { |
| 190 | int fd; |
| 191 | QEMUFile *file; |
| 192 | } QEMUFileSocket; |
| 193 | |
| 194 | static 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 Swirl | c5b76b3 | 2009-06-13 08:44:31 +0000 | [diff] [blame] | 200 | len = recv(s->fd, (void *)buf, size, 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 201 | } while (len == -1 && socket_error() == EINTR); |
| 202 | |
| 203 | if (len == -1) |
| 204 | len = -socket_error(); |
| 205 | |
| 206 | return len; |
| 207 | } |
| 208 | |
| 209 | static int socket_close(void *opaque) |
| 210 | { |
| 211 | QEMUFileSocket *s = opaque; |
| 212 | qemu_free(s); |
| 213 | return 0; |
| 214 | } |
| 215 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 216 | static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 217 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 218 | QEMUFileStdio *s = opaque; |
| 219 | return fwrite(buf, 1, size, s->stdio_file); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 222 | static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 223 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 224 | QEMUFileStdio *s = opaque; |
| 225 | FILE *fp = s->stdio_file; |
Uri Lublin | 8a67ec4 | 2009-06-08 19:27:21 +0300 | [diff] [blame] | 226 | 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; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 235 | static int stdio_pclose(void *opaque) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 236 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 237 | QEMUFileStdio *s = opaque; |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 238 | int ret; |
| 239 | ret = pclose(s->stdio_file); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 240 | qemu_free(s); |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 241 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 244 | static int stdio_fclose(void *opaque) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 245 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 246 | QEMUFileStdio *s = opaque; |
| 247 | fclose(s->stdio_file); |
| 248 | qemu_free(s); |
| 249 | return 0; |
| 250 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 251 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 252 | QEMUFile *qemu_popen(FILE *stdio_file, const char *mode) |
| 253 | { |
| 254 | QEMUFileStdio *s; |
| 255 | |
| 256 | if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 257 | fprintf(stderr, "qemu_popen: Argument validity check failed\n"); |
| 258 | return NULL; |
| 259 | } |
| 260 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 261 | s = qemu_mallocz(sizeof(QEMUFileStdio)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 262 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 263 | s->stdio_file = stdio_file; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 264 | |
| 265 | if(mode[0] == 'r') { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 266 | s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, |
| 267 | NULL, NULL, NULL); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 268 | } else { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 269 | s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, |
| 270 | NULL, NULL, NULL); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 271 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 272 | return s->file; |
| 273 | } |
| 274 | |
| 275 | QEMUFile *qemu_popen_cmd(const char *command, const char *mode) |
| 276 | { |
| 277 | FILE *popen_file; |
| 278 | |
| 279 | popen_file = popen(command, mode); |
| 280 | if(popen_file == NULL) { |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | return qemu_popen(popen_file, mode); |
| 285 | } |
| 286 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 287 | int qemu_stdio_fd(QEMUFile *f) |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 288 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 289 | QEMUFileStdio *p; |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 290 | int fd; |
| 291 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 292 | p = (QEMUFileStdio *)f->opaque; |
| 293 | fd = fileno(p->stdio_file); |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 294 | |
| 295 | return fd; |
| 296 | } |
| 297 | |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 298 | QEMUFile *qemu_fdopen(int fd, const char *mode) |
| 299 | { |
| 300 | QEMUFileStdio *s; |
| 301 | |
| 302 | if (mode == NULL || |
| 303 | (mode[0] != 'r' && mode[0] != 'w') || |
| 304 | mode[1] != 'b' || mode[2] != 0) { |
| 305 | fprintf(stderr, "qemu_fdopen: Argument validity check failed\n"); |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | s = qemu_mallocz(sizeof(QEMUFileStdio)); |
| 310 | s->stdio_file = fdopen(fd, mode); |
| 311 | if (!s->stdio_file) |
| 312 | goto fail; |
| 313 | |
| 314 | if(mode[0] == 'r') { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 315 | s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, |
| 316 | NULL, NULL, NULL); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 317 | } else { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 318 | s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, |
| 319 | NULL, NULL, NULL); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 320 | } |
| 321 | return s->file; |
| 322 | |
| 323 | fail: |
| 324 | qemu_free(s); |
| 325 | return NULL; |
| 326 | } |
| 327 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 328 | QEMUFile *qemu_fopen_socket(int fd) |
| 329 | { |
| 330 | QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket)); |
| 331 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 332 | s->fd = fd; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 333 | s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, |
| 334 | NULL, NULL, NULL); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 335 | return s->file; |
| 336 | } |
| 337 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 338 | static int file_put_buffer(void *opaque, const uint8_t *buf, |
| 339 | int64_t pos, int size) |
| 340 | { |
| 341 | QEMUFileStdio *s = opaque; |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 342 | fseek(s->stdio_file, pos, SEEK_SET); |
Kirill A. Shutemov | 5fdb3aa | 2009-12-25 18:19:19 +0000 | [diff] [blame] | 343 | return fwrite(buf, 1, size, s->stdio_file); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
| 347 | { |
| 348 | QEMUFileStdio *s = opaque; |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 349 | fseek(s->stdio_file, pos, SEEK_SET); |
| 350 | return fread(buf, 1, size, s->stdio_file); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
| 354 | { |
| 355 | QEMUFileStdio *s; |
| 356 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 357 | if (mode == NULL || |
| 358 | (mode[0] != 'r' && mode[0] != 'w') || |
| 359 | mode[1] != 'b' || mode[2] != 0) { |
Blue Swirl | 090414a | 2010-03-13 11:36:09 +0000 | [diff] [blame] | 360 | fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 361 | return NULL; |
| 362 | } |
| 363 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 364 | s = qemu_mallocz(sizeof(QEMUFileStdio)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 365 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 366 | s->stdio_file = fopen(filename, mode); |
| 367 | if (!s->stdio_file) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 368 | goto fail; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 369 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 370 | if(mode[0] == 'w') { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 371 | s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, |
| 372 | NULL, NULL, NULL); |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 373 | } else { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 374 | s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, |
| 375 | NULL, NULL, NULL); |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 376 | } |
| 377 | return s->file; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 378 | fail: |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 379 | qemu_free(s); |
| 380 | return NULL; |
| 381 | } |
| 382 | |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 383 | static int block_put_buffer(void *opaque, const uint8_t *buf, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 384 | int64_t pos, int size) |
| 385 | { |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 386 | bdrv_save_vmstate(opaque, buf, pos, size); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 387 | return size; |
| 388 | } |
| 389 | |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 390 | static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 391 | { |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 392 | return bdrv_load_vmstate(opaque, buf, pos, size); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static int bdrv_fclose(void *opaque) |
| 396 | { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 400 | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 401 | { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 402 | if (is_writable) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 403 | return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose, |
| 404 | NULL, NULL, NULL); |
| 405 | return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer, |
| 409 | QEMUFileGetBufferFunc *get_buffer, |
| 410 | QEMUFileCloseFunc *close, |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 411 | QEMUFileRateLimit *rate_limit, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 412 | QEMUFileSetRateLimit *set_rate_limit, |
| 413 | QEMUFileGetRateLimit *get_rate_limit) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 414 | { |
| 415 | QEMUFile *f; |
| 416 | |
| 417 | f = qemu_mallocz(sizeof(QEMUFile)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 418 | |
| 419 | f->opaque = opaque; |
| 420 | f->put_buffer = put_buffer; |
| 421 | f->get_buffer = get_buffer; |
| 422 | f->close = close; |
| 423 | f->rate_limit = rate_limit; |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 424 | f->set_rate_limit = set_rate_limit; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 425 | f->get_rate_limit = get_rate_limit; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 426 | f->is_write = 0; |
| 427 | |
| 428 | return f; |
| 429 | } |
| 430 | |
| 431 | int qemu_file_has_error(QEMUFile *f) |
| 432 | { |
| 433 | return f->has_error; |
| 434 | } |
| 435 | |
aliguori | 4dabe24 | 2009-04-05 19:30:51 +0000 | [diff] [blame] | 436 | void qemu_file_set_error(QEMUFile *f) |
| 437 | { |
| 438 | f->has_error = 1; |
| 439 | } |
| 440 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 441 | void qemu_fflush(QEMUFile *f) |
| 442 | { |
| 443 | if (!f->put_buffer) |
| 444 | return; |
| 445 | |
| 446 | if (f->is_write && f->buf_index > 0) { |
| 447 | int len; |
| 448 | |
| 449 | len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index); |
| 450 | if (len > 0) |
| 451 | f->buf_offset += f->buf_index; |
| 452 | else |
| 453 | f->has_error = 1; |
| 454 | f->buf_index = 0; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | static void qemu_fill_buffer(QEMUFile *f) |
| 459 | { |
| 460 | int len; |
| 461 | |
| 462 | if (!f->get_buffer) |
| 463 | return; |
| 464 | |
| 465 | if (f->is_write) |
| 466 | abort(); |
| 467 | |
| 468 | len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE); |
| 469 | if (len > 0) { |
| 470 | f->buf_index = 0; |
| 471 | f->buf_size = len; |
| 472 | f->buf_offset += len; |
| 473 | } else if (len != -EAGAIN) |
| 474 | f->has_error = 1; |
| 475 | } |
| 476 | |
| 477 | int qemu_fclose(QEMUFile *f) |
| 478 | { |
| 479 | int ret = 0; |
| 480 | qemu_fflush(f); |
| 481 | if (f->close) |
| 482 | ret = f->close(f->opaque); |
| 483 | qemu_free(f); |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | void qemu_file_put_notify(QEMUFile *f) |
| 488 | { |
| 489 | f->put_buffer(f->opaque, NULL, 0, 0); |
| 490 | } |
| 491 | |
| 492 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
| 493 | { |
| 494 | int l; |
| 495 | |
| 496 | if (!f->has_error && f->is_write == 0 && f->buf_index > 0) { |
| 497 | fprintf(stderr, |
| 498 | "Attempted to write to buffer while read buffer is not empty\n"); |
| 499 | abort(); |
| 500 | } |
| 501 | |
| 502 | while (!f->has_error && size > 0) { |
| 503 | l = IO_BUF_SIZE - f->buf_index; |
| 504 | if (l > size) |
| 505 | l = size; |
| 506 | memcpy(f->buf + f->buf_index, buf, l); |
| 507 | f->is_write = 1; |
| 508 | f->buf_index += l; |
| 509 | buf += l; |
| 510 | size -= l; |
| 511 | if (f->buf_index >= IO_BUF_SIZE) |
| 512 | qemu_fflush(f); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | void qemu_put_byte(QEMUFile *f, int v) |
| 517 | { |
| 518 | if (!f->has_error && f->is_write == 0 && f->buf_index > 0) { |
| 519 | fprintf(stderr, |
| 520 | "Attempted to write to buffer while read buffer is not empty\n"); |
| 521 | abort(); |
| 522 | } |
| 523 | |
| 524 | f->buf[f->buf_index++] = v; |
| 525 | f->is_write = 1; |
| 526 | if (f->buf_index >= IO_BUF_SIZE) |
| 527 | qemu_fflush(f); |
| 528 | } |
| 529 | |
| 530 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
| 531 | { |
| 532 | int size, l; |
| 533 | |
| 534 | if (f->is_write) |
| 535 | abort(); |
| 536 | |
| 537 | size = size1; |
| 538 | while (size > 0) { |
| 539 | l = f->buf_size - f->buf_index; |
| 540 | if (l == 0) { |
| 541 | qemu_fill_buffer(f); |
| 542 | l = f->buf_size - f->buf_index; |
| 543 | if (l == 0) |
| 544 | break; |
| 545 | } |
| 546 | if (l > size) |
| 547 | l = size; |
| 548 | memcpy(buf, f->buf + f->buf_index, l); |
| 549 | f->buf_index += l; |
| 550 | buf += l; |
| 551 | size -= l; |
| 552 | } |
| 553 | return size1 - size; |
| 554 | } |
| 555 | |
| 556 | int qemu_get_byte(QEMUFile *f) |
| 557 | { |
| 558 | if (f->is_write) |
| 559 | abort(); |
| 560 | |
| 561 | if (f->buf_index >= f->buf_size) { |
| 562 | qemu_fill_buffer(f); |
| 563 | if (f->buf_index >= f->buf_size) |
| 564 | return 0; |
| 565 | } |
| 566 | return f->buf[f->buf_index++]; |
| 567 | } |
| 568 | |
| 569 | int64_t qemu_ftell(QEMUFile *f) |
| 570 | { |
| 571 | return f->buf_offset - f->buf_size + f->buf_index; |
| 572 | } |
| 573 | |
| 574 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) |
| 575 | { |
| 576 | if (whence == SEEK_SET) { |
| 577 | /* nothing to do */ |
| 578 | } else if (whence == SEEK_CUR) { |
| 579 | pos += qemu_ftell(f); |
| 580 | } else { |
| 581 | /* SEEK_END not supported */ |
| 582 | return -1; |
| 583 | } |
| 584 | if (f->put_buffer) { |
| 585 | qemu_fflush(f); |
| 586 | f->buf_offset = pos; |
| 587 | } else { |
| 588 | f->buf_offset = pos; |
| 589 | f->buf_index = 0; |
| 590 | f->buf_size = 0; |
| 591 | } |
| 592 | return pos; |
| 593 | } |
| 594 | |
| 595 | int qemu_file_rate_limit(QEMUFile *f) |
| 596 | { |
| 597 | if (f->rate_limit) |
| 598 | return f->rate_limit(f->opaque); |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 603 | size_t qemu_file_get_rate_limit(QEMUFile *f) |
| 604 | { |
| 605 | if (f->get_rate_limit) |
| 606 | return f->get_rate_limit(f->opaque); |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 611 | size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate) |
| 612 | { |
Glauber Costa | 0bb05ea | 2009-07-14 18:26:51 -0400 | [diff] [blame] | 613 | /* any failed or completed migration keeps its state to allow probing of |
| 614 | * migration data, but has no associated file anymore */ |
| 615 | if (f && f->set_rate_limit) |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 616 | return f->set_rate_limit(f->opaque, new_rate); |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 621 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
| 622 | { |
| 623 | qemu_put_byte(f, v >> 8); |
| 624 | qemu_put_byte(f, v); |
| 625 | } |
| 626 | |
| 627 | void qemu_put_be32(QEMUFile *f, unsigned int v) |
| 628 | { |
| 629 | qemu_put_byte(f, v >> 24); |
| 630 | qemu_put_byte(f, v >> 16); |
| 631 | qemu_put_byte(f, v >> 8); |
| 632 | qemu_put_byte(f, v); |
| 633 | } |
| 634 | |
| 635 | void qemu_put_be64(QEMUFile *f, uint64_t v) |
| 636 | { |
| 637 | qemu_put_be32(f, v >> 32); |
| 638 | qemu_put_be32(f, v); |
| 639 | } |
| 640 | |
| 641 | unsigned int qemu_get_be16(QEMUFile *f) |
| 642 | { |
| 643 | unsigned int v; |
| 644 | v = qemu_get_byte(f) << 8; |
| 645 | v |= qemu_get_byte(f); |
| 646 | return v; |
| 647 | } |
| 648 | |
| 649 | unsigned int qemu_get_be32(QEMUFile *f) |
| 650 | { |
| 651 | unsigned int v; |
| 652 | v = qemu_get_byte(f) << 24; |
| 653 | v |= qemu_get_byte(f) << 16; |
| 654 | v |= qemu_get_byte(f) << 8; |
| 655 | v |= qemu_get_byte(f); |
| 656 | return v; |
| 657 | } |
| 658 | |
| 659 | uint64_t qemu_get_be64(QEMUFile *f) |
| 660 | { |
| 661 | uint64_t v; |
| 662 | v = (uint64_t)qemu_get_be32(f) << 32; |
| 663 | v |= qemu_get_be32(f); |
| 664 | return v; |
| 665 | } |
| 666 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 667 | /* 8 bit int */ |
| 668 | |
| 669 | static int get_int8(QEMUFile *f, void *pv, size_t size) |
| 670 | { |
| 671 | int8_t *v = pv; |
| 672 | qemu_get_s8s(f, v); |
| 673 | return 0; |
| 674 | } |
| 675 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 676 | static void put_int8(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 677 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 678 | int8_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 679 | qemu_put_s8s(f, v); |
| 680 | } |
| 681 | |
| 682 | const VMStateInfo vmstate_info_int8 = { |
| 683 | .name = "int8", |
| 684 | .get = get_int8, |
| 685 | .put = put_int8, |
| 686 | }; |
| 687 | |
| 688 | /* 16 bit int */ |
| 689 | |
| 690 | static int get_int16(QEMUFile *f, void *pv, size_t size) |
| 691 | { |
| 692 | int16_t *v = pv; |
| 693 | qemu_get_sbe16s(f, v); |
| 694 | return 0; |
| 695 | } |
| 696 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 697 | static void put_int16(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 698 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 699 | int16_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 700 | qemu_put_sbe16s(f, v); |
| 701 | } |
| 702 | |
| 703 | const VMStateInfo vmstate_info_int16 = { |
| 704 | .name = "int16", |
| 705 | .get = get_int16, |
| 706 | .put = put_int16, |
| 707 | }; |
| 708 | |
| 709 | /* 32 bit int */ |
| 710 | |
| 711 | static int get_int32(QEMUFile *f, void *pv, size_t size) |
| 712 | { |
| 713 | int32_t *v = pv; |
| 714 | qemu_get_sbe32s(f, v); |
| 715 | return 0; |
| 716 | } |
| 717 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 718 | static void put_int32(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 719 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 720 | int32_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 721 | qemu_put_sbe32s(f, v); |
| 722 | } |
| 723 | |
| 724 | const VMStateInfo vmstate_info_int32 = { |
| 725 | .name = "int32", |
| 726 | .get = get_int32, |
| 727 | .put = put_int32, |
| 728 | }; |
| 729 | |
Juan Quintela | 8250166 | 2009-08-20 19:42:32 +0200 | [diff] [blame] | 730 | /* 32 bit int. See that the received value is the same than the one |
| 731 | in the field */ |
| 732 | |
| 733 | static int get_int32_equal(QEMUFile *f, void *pv, size_t size) |
| 734 | { |
| 735 | int32_t *v = pv; |
| 736 | int32_t v2; |
| 737 | qemu_get_sbe32s(f, &v2); |
| 738 | |
| 739 | if (*v == v2) |
| 740 | return 0; |
| 741 | return -EINVAL; |
| 742 | } |
| 743 | |
| 744 | const VMStateInfo vmstate_info_int32_equal = { |
| 745 | .name = "int32 equal", |
| 746 | .get = get_int32_equal, |
| 747 | .put = put_int32, |
| 748 | }; |
| 749 | |
Juan Quintela | 0a031e0 | 2009-08-20 19:42:37 +0200 | [diff] [blame] | 750 | /* 32 bit int. See that the received value is the less or the same |
| 751 | than the one in the field */ |
| 752 | |
| 753 | static int get_int32_le(QEMUFile *f, void *pv, size_t size) |
| 754 | { |
| 755 | int32_t *old = pv; |
| 756 | int32_t new; |
| 757 | qemu_get_sbe32s(f, &new); |
| 758 | |
| 759 | if (*old <= new) |
| 760 | return 0; |
| 761 | return -EINVAL; |
| 762 | } |
| 763 | |
| 764 | const VMStateInfo vmstate_info_int32_le = { |
| 765 | .name = "int32 equal", |
| 766 | .get = get_int32_le, |
| 767 | .put = put_int32, |
| 768 | }; |
| 769 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 770 | /* 64 bit int */ |
| 771 | |
| 772 | static int get_int64(QEMUFile *f, void *pv, size_t size) |
| 773 | { |
| 774 | int64_t *v = pv; |
| 775 | qemu_get_sbe64s(f, v); |
| 776 | return 0; |
| 777 | } |
| 778 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 779 | static void put_int64(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 780 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 781 | int64_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 782 | qemu_put_sbe64s(f, v); |
| 783 | } |
| 784 | |
| 785 | const VMStateInfo vmstate_info_int64 = { |
| 786 | .name = "int64", |
| 787 | .get = get_int64, |
| 788 | .put = put_int64, |
| 789 | }; |
| 790 | |
| 791 | /* 8 bit unsigned int */ |
| 792 | |
| 793 | static int get_uint8(QEMUFile *f, void *pv, size_t size) |
| 794 | { |
| 795 | uint8_t *v = pv; |
| 796 | qemu_get_8s(f, v); |
| 797 | return 0; |
| 798 | } |
| 799 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 800 | static void put_uint8(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 801 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 802 | uint8_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 803 | qemu_put_8s(f, v); |
| 804 | } |
| 805 | |
| 806 | const VMStateInfo vmstate_info_uint8 = { |
| 807 | .name = "uint8", |
| 808 | .get = get_uint8, |
| 809 | .put = put_uint8, |
| 810 | }; |
| 811 | |
| 812 | /* 16 bit unsigned int */ |
| 813 | |
| 814 | static int get_uint16(QEMUFile *f, void *pv, size_t size) |
| 815 | { |
| 816 | uint16_t *v = pv; |
| 817 | qemu_get_be16s(f, v); |
| 818 | return 0; |
| 819 | } |
| 820 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 821 | static void put_uint16(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 822 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 823 | uint16_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 824 | qemu_put_be16s(f, v); |
| 825 | } |
| 826 | |
| 827 | const VMStateInfo vmstate_info_uint16 = { |
| 828 | .name = "uint16", |
| 829 | .get = get_uint16, |
| 830 | .put = put_uint16, |
| 831 | }; |
| 832 | |
| 833 | /* 32 bit unsigned int */ |
| 834 | |
| 835 | static int get_uint32(QEMUFile *f, void *pv, size_t size) |
| 836 | { |
| 837 | uint32_t *v = pv; |
| 838 | qemu_get_be32s(f, v); |
| 839 | return 0; |
| 840 | } |
| 841 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 842 | static void put_uint32(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 843 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 844 | uint32_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 845 | qemu_put_be32s(f, v); |
| 846 | } |
| 847 | |
| 848 | const VMStateInfo vmstate_info_uint32 = { |
| 849 | .name = "uint32", |
| 850 | .get = get_uint32, |
| 851 | .put = put_uint32, |
| 852 | }; |
| 853 | |
| 854 | /* 64 bit unsigned int */ |
| 855 | |
| 856 | static int get_uint64(QEMUFile *f, void *pv, size_t size) |
| 857 | { |
| 858 | uint64_t *v = pv; |
| 859 | qemu_get_be64s(f, v); |
| 860 | return 0; |
| 861 | } |
| 862 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 863 | static void put_uint64(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 864 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 865 | uint64_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 866 | qemu_put_be64s(f, v); |
| 867 | } |
| 868 | |
| 869 | const VMStateInfo vmstate_info_uint64 = { |
| 870 | .name = "uint64", |
| 871 | .get = get_uint64, |
| 872 | .put = put_uint64, |
| 873 | }; |
| 874 | |
Juan Quintela | 80cd83e | 2009-09-10 03:04:36 +0200 | [diff] [blame] | 875 | /* 8 bit int. See that the received value is the same than the one |
| 876 | in the field */ |
| 877 | |
| 878 | static int get_uint8_equal(QEMUFile *f, void *pv, size_t size) |
| 879 | { |
| 880 | uint8_t *v = pv; |
| 881 | uint8_t v2; |
| 882 | qemu_get_8s(f, &v2); |
| 883 | |
| 884 | if (*v == v2) |
| 885 | return 0; |
| 886 | return -EINVAL; |
| 887 | } |
| 888 | |
| 889 | const VMStateInfo vmstate_info_uint8_equal = { |
Juan Quintela | aa1cce6 | 2009-10-15 19:16:06 +0200 | [diff] [blame] | 890 | .name = "uint8 equal", |
Juan Quintela | 80cd83e | 2009-09-10 03:04:36 +0200 | [diff] [blame] | 891 | .get = get_uint8_equal, |
| 892 | .put = put_uint8, |
| 893 | }; |
| 894 | |
Juan Quintela | dc3b83a | 2009-10-15 23:16:13 +0200 | [diff] [blame] | 895 | /* 16 bit unsigned int int. See that the received value is the same than the one |
| 896 | in the field */ |
| 897 | |
| 898 | static int get_uint16_equal(QEMUFile *f, void *pv, size_t size) |
| 899 | { |
| 900 | uint16_t *v = pv; |
| 901 | uint16_t v2; |
| 902 | qemu_get_be16s(f, &v2); |
| 903 | |
| 904 | if (*v == v2) |
| 905 | return 0; |
| 906 | return -EINVAL; |
| 907 | } |
| 908 | |
| 909 | const VMStateInfo vmstate_info_uint16_equal = { |
| 910 | .name = "uint16 equal", |
| 911 | .get = get_uint16_equal, |
| 912 | .put = put_uint16, |
| 913 | }; |
| 914 | |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 915 | /* timers */ |
| 916 | |
| 917 | static int get_timer(QEMUFile *f, void *pv, size_t size) |
| 918 | { |
| 919 | QEMUTimer *v = pv; |
| 920 | qemu_get_timer(f, v); |
| 921 | return 0; |
| 922 | } |
| 923 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 924 | static void put_timer(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 925 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 926 | QEMUTimer *v = pv; |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 927 | qemu_put_timer(f, v); |
| 928 | } |
| 929 | |
| 930 | const VMStateInfo vmstate_info_timer = { |
| 931 | .name = "timer", |
| 932 | .get = get_timer, |
| 933 | .put = put_timer, |
| 934 | }; |
| 935 | |
Juan Quintela | 6f67c50 | 2009-08-20 19:42:35 +0200 | [diff] [blame] | 936 | /* uint8_t buffers */ |
| 937 | |
| 938 | static int get_buffer(QEMUFile *f, void *pv, size_t size) |
| 939 | { |
| 940 | uint8_t *v = pv; |
| 941 | qemu_get_buffer(f, v, size); |
| 942 | return 0; |
| 943 | } |
| 944 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 945 | static void put_buffer(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 6f67c50 | 2009-08-20 19:42:35 +0200 | [diff] [blame] | 946 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 947 | uint8_t *v = pv; |
Juan Quintela | 6f67c50 | 2009-08-20 19:42:35 +0200 | [diff] [blame] | 948 | qemu_put_buffer(f, v, size); |
| 949 | } |
| 950 | |
| 951 | const VMStateInfo vmstate_info_buffer = { |
| 952 | .name = "buffer", |
| 953 | .get = get_buffer, |
| 954 | .put = put_buffer, |
| 955 | }; |
| 956 | |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 957 | /* unused buffers: space that was used for some fields that are |
| 958 | not usefull anymore */ |
| 959 | |
| 960 | static int get_unused_buffer(QEMUFile *f, void *pv, size_t size) |
| 961 | { |
Jan Kiszka | 21174c3 | 2009-12-02 12:36:35 +0100 | [diff] [blame] | 962 | uint8_t buf[1024]; |
| 963 | int block_len; |
| 964 | |
| 965 | while (size > 0) { |
| 966 | block_len = MIN(sizeof(buf), size); |
| 967 | size -= block_len; |
| 968 | qemu_get_buffer(f, buf, block_len); |
| 969 | } |
| 970 | return 0; |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | static void put_unused_buffer(QEMUFile *f, void *pv, size_t size) |
| 974 | { |
Jan Kiszka | 21174c3 | 2009-12-02 12:36:35 +0100 | [diff] [blame] | 975 | static const uint8_t buf[1024]; |
| 976 | int block_len; |
| 977 | |
| 978 | while (size > 0) { |
| 979 | block_len = MIN(sizeof(buf), size); |
| 980 | size -= block_len; |
| 981 | qemu_put_buffer(f, buf, block_len); |
| 982 | } |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | const VMStateInfo vmstate_info_unused_buffer = { |
| 986 | .name = "unused_buffer", |
| 987 | .get = get_unused_buffer, |
| 988 | .put = put_unused_buffer, |
| 989 | }; |
| 990 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 991 | typedef struct SaveStateEntry { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 992 | QTAILQ_ENTRY(SaveStateEntry) entry; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 993 | char idstr[256]; |
| 994 | int instance_id; |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 995 | int alias_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 996 | int version_id; |
| 997 | int section_id; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 998 | SaveSetParamsHandler *set_params; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 999 | SaveLiveStateHandler *save_live_state; |
| 1000 | SaveStateHandler *save_state; |
| 1001 | LoadStateHandler *load_state; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1002 | const VMStateDescription *vmsd; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1003 | void *opaque; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1004 | } SaveStateEntry; |
| 1005 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1006 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1007 | static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers = |
| 1008 | QTAILQ_HEAD_INITIALIZER(savevm_handlers); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1009 | static int global_section_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1010 | |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1011 | static int calculate_new_instance_id(const char *idstr) |
| 1012 | { |
| 1013 | SaveStateEntry *se; |
| 1014 | int instance_id = 0; |
| 1015 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1016 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1017 | if (strcmp(idstr, se->idstr) == 0 |
| 1018 | && instance_id <= se->instance_id) { |
| 1019 | instance_id = se->instance_id + 1; |
| 1020 | } |
| 1021 | } |
| 1022 | return instance_id; |
| 1023 | } |
| 1024 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1025 | /* TODO: Individual devices generally have very little idea about the rest |
| 1026 | of the system, so instance_id should be removed/replaced. |
| 1027 | Meanwhile pass -1 as instance_id if you do not already have a clearly |
| 1028 | distinguishing id for all instances of your device class. */ |
| 1029 | int register_savevm_live(const char *idstr, |
| 1030 | int instance_id, |
| 1031 | int version_id, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1032 | SaveSetParamsHandler *set_params, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1033 | SaveLiveStateHandler *save_live_state, |
| 1034 | SaveStateHandler *save_state, |
| 1035 | LoadStateHandler *load_state, |
| 1036 | void *opaque) |
| 1037 | { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1038 | SaveStateEntry *se; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1039 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1040 | se = qemu_mallocz(sizeof(SaveStateEntry)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1041 | pstrcpy(se->idstr, sizeof(se->idstr), idstr); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1042 | se->version_id = version_id; |
| 1043 | se->section_id = global_section_id++; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1044 | se->set_params = set_params; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1045 | se->save_live_state = save_live_state; |
| 1046 | se->save_state = save_state; |
| 1047 | se->load_state = load_state; |
| 1048 | se->opaque = opaque; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1049 | se->vmsd = NULL; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1050 | |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1051 | if (instance_id == -1) { |
| 1052 | se->instance_id = calculate_new_instance_id(idstr); |
| 1053 | } else { |
| 1054 | se->instance_id = instance_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1055 | } |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1056 | /* add at the end of list */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1057 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | int register_savevm(const char *idstr, |
| 1062 | int instance_id, |
| 1063 | int version_id, |
| 1064 | SaveStateHandler *save_state, |
| 1065 | LoadStateHandler *load_state, |
| 1066 | void *opaque) |
| 1067 | { |
| 1068 | return register_savevm_live(idstr, instance_id, version_id, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1069 | NULL, NULL, save_state, load_state, opaque); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1072 | void unregister_savevm(const char *idstr, void *opaque) |
| 1073 | { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1074 | SaveStateEntry *se, *new_se; |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1075 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1076 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1077 | if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1078 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1079 | qemu_free(se); |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1080 | } |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1084 | int vmstate_register_with_alias_id(int instance_id, |
| 1085 | const VMStateDescription *vmsd, |
| 1086 | void *opaque, int alias_id, |
| 1087 | int required_for_version) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1088 | { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1089 | SaveStateEntry *se; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1090 | |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1091 | /* If this triggers, alias support can be dropped for the vmsd. */ |
| 1092 | assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); |
| 1093 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1094 | se = qemu_mallocz(sizeof(SaveStateEntry)); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1095 | pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1096 | se->version_id = vmsd->version_id; |
| 1097 | se->section_id = global_section_id++; |
| 1098 | se->save_live_state = NULL; |
| 1099 | se->save_state = NULL; |
| 1100 | se->load_state = NULL; |
| 1101 | se->opaque = opaque; |
| 1102 | se->vmsd = vmsd; |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1103 | se->alias_id = alias_id; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1104 | |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1105 | if (instance_id == -1) { |
| 1106 | se->instance_id = calculate_new_instance_id(vmsd->name); |
| 1107 | } else { |
| 1108 | se->instance_id = instance_id; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1109 | } |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1110 | /* add at the end of list */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1111 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
| 1114 | |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1115 | int vmstate_register(int instance_id, const VMStateDescription *vmsd, |
| 1116 | void *opaque) |
| 1117 | { |
| 1118 | return vmstate_register_with_alias_id(instance_id, vmsd, opaque, -1, 0); |
| 1119 | } |
| 1120 | |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1121 | void vmstate_unregister(const VMStateDescription *vmsd, void *opaque) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1122 | { |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1123 | SaveStateEntry *se, *new_se; |
| 1124 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1125 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1126 | if (se->vmsd == vmsd && se->opaque == opaque) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1127 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1128 | qemu_free(se); |
| 1129 | } |
| 1130 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd, |
| 1134 | void *opaque, int version_id) |
| 1135 | { |
| 1136 | VMStateField *field = vmsd->fields; |
| 1137 | |
| 1138 | if (version_id > vmsd->version_id) { |
| 1139 | return -EINVAL; |
| 1140 | } |
| 1141 | if (version_id < vmsd->minimum_version_id_old) { |
| 1142 | return -EINVAL; |
| 1143 | } |
| 1144 | if (version_id < vmsd->minimum_version_id) { |
| 1145 | return vmsd->load_state_old(f, opaque, version_id); |
| 1146 | } |
Juan Quintela | fd4d52d | 2009-09-10 03:04:31 +0200 | [diff] [blame] | 1147 | if (vmsd->pre_load) { |
| 1148 | int ret = vmsd->pre_load(opaque); |
| 1149 | if (ret) |
| 1150 | return ret; |
| 1151 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1152 | while(field->name) { |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1153 | if ((field->field_exists && |
| 1154 | field->field_exists(opaque, version_id)) || |
| 1155 | (!field->field_exists && |
| 1156 | field->version_id <= version_id)) { |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1157 | void *base_addr = opaque + field->offset; |
| 1158 | int ret, i, n_elems = 1; |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1159 | int size = field->size; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1160 | |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1161 | if (field->flags & VMS_VBUFFER) { |
| 1162 | size = *(int32_t *)(opaque+field->size_offset); |
Juan Quintela | 33599e2 | 2009-12-02 12:36:43 +0100 | [diff] [blame] | 1163 | if (field->flags & VMS_MULTIPLY) { |
| 1164 | size *= field->size; |
| 1165 | } |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1166 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1167 | if (field->flags & VMS_ARRAY) { |
| 1168 | n_elems = field->num; |
Juan Quintela | d669828 | 2009-10-16 11:27:17 +0200 | [diff] [blame] | 1169 | } else if (field->flags & VMS_VARRAY_INT32) { |
| 1170 | n_elems = *(int32_t *)(opaque+field->num_offset); |
Juan Quintela | bdb4941 | 2009-10-16 11:35:18 +0200 | [diff] [blame] | 1171 | } else if (field->flags & VMS_VARRAY_UINT16) { |
| 1172 | n_elems = *(uint16_t *)(opaque+field->num_offset); |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1173 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1174 | if (field->flags & VMS_POINTER) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1175 | base_addr = *(void **)base_addr + field->start; |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1176 | } |
| 1177 | for (i = 0; i < n_elems; i++) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1178 | void *addr = base_addr + size * i; |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1179 | |
Juan Quintela | 19df438 | 2009-09-29 22:48:41 +0200 | [diff] [blame] | 1180 | if (field->flags & VMS_ARRAY_OF_POINTER) { |
| 1181 | addr = *(void **)addr; |
| 1182 | } |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1183 | if (field->flags & VMS_STRUCT) { |
Juan Quintela | fa3aad2 | 2009-08-28 15:28:25 +0200 | [diff] [blame] | 1184 | ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id); |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1185 | } else { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1186 | ret = field->info->get(f, addr, size); |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1187 | |
| 1188 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1189 | if (ret < 0) { |
| 1190 | return ret; |
| 1191 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | field++; |
| 1195 | } |
Juan Quintela | 752ff2f | 2009-09-10 03:04:30 +0200 | [diff] [blame] | 1196 | if (vmsd->post_load) { |
Juan Quintela | e59fb37 | 2009-09-29 22:48:21 +0200 | [diff] [blame] | 1197 | return vmsd->post_load(opaque, version_id); |
Juan Quintela | 752ff2f | 2009-09-10 03:04:30 +0200 | [diff] [blame] | 1198 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1203 | void *opaque) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1204 | { |
| 1205 | VMStateField *field = vmsd->fields; |
| 1206 | |
Juan Quintela | 8fb0791 | 2009-09-10 03:04:32 +0200 | [diff] [blame] | 1207 | if (vmsd->pre_save) { |
| 1208 | vmsd->pre_save(opaque); |
| 1209 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1210 | while(field->name) { |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1211 | if (!field->field_exists || |
| 1212 | field->field_exists(opaque, vmsd->version_id)) { |
| 1213 | void *base_addr = opaque + field->offset; |
| 1214 | int i, n_elems = 1; |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1215 | int size = field->size; |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1216 | |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1217 | if (field->flags & VMS_VBUFFER) { |
| 1218 | size = *(int32_t *)(opaque+field->size_offset); |
Juan Quintela | 33599e2 | 2009-12-02 12:36:43 +0100 | [diff] [blame] | 1219 | if (field->flags & VMS_MULTIPLY) { |
| 1220 | size *= field->size; |
| 1221 | } |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1222 | } |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1223 | if (field->flags & VMS_ARRAY) { |
| 1224 | n_elems = field->num; |
Juan Quintela | d669828 | 2009-10-16 11:27:17 +0200 | [diff] [blame] | 1225 | } else if (field->flags & VMS_VARRAY_INT32) { |
| 1226 | n_elems = *(int32_t *)(opaque+field->num_offset); |
Juan Quintela | bdb4941 | 2009-10-16 11:35:18 +0200 | [diff] [blame] | 1227 | } else if (field->flags & VMS_VARRAY_UINT16) { |
| 1228 | n_elems = *(uint16_t *)(opaque+field->num_offset); |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1229 | } |
| 1230 | if (field->flags & VMS_POINTER) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1231 | base_addr = *(void **)base_addr + field->start; |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1232 | } |
| 1233 | for (i = 0; i < n_elems; i++) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1234 | void *addr = base_addr + size * i; |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1235 | |
Juan Quintela | 8595387 | 2009-12-02 12:36:37 +0100 | [diff] [blame] | 1236 | if (field->flags & VMS_ARRAY_OF_POINTER) { |
| 1237 | addr = *(void **)addr; |
| 1238 | } |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1239 | if (field->flags & VMS_STRUCT) { |
| 1240 | vmstate_save_state(f, field->vmsd, addr); |
| 1241 | } else { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1242 | field->info->put(f, addr, size); |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1243 | } |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1244 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1245 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1246 | field++; |
| 1247 | } |
| 1248 | } |
| 1249 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1250 | static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id) |
| 1251 | { |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1252 | if (!se->vmsd) { /* Old style */ |
| 1253 | return se->load_state(f, se->opaque, version_id); |
| 1254 | } |
| 1255 | return vmstate_load_state(f, se->vmsd, se->opaque, version_id); |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | static void vmstate_save(QEMUFile *f, SaveStateEntry *se) |
| 1259 | { |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1260 | if (!se->vmsd) { /* Old style */ |
| 1261 | se->save_state(f, se->opaque); |
| 1262 | return; |
| 1263 | } |
| 1264 | vmstate_save_state(f,se->vmsd, se->opaque); |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1265 | } |
| 1266 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1267 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
| 1268 | #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002 |
| 1269 | #define QEMU_VM_FILE_VERSION 0x00000003 |
| 1270 | |
| 1271 | #define QEMU_VM_EOF 0x00 |
| 1272 | #define QEMU_VM_SECTION_START 0x01 |
| 1273 | #define QEMU_VM_SECTION_PART 0x02 |
| 1274 | #define QEMU_VM_SECTION_END 0x03 |
| 1275 | #define QEMU_VM_SECTION_FULL 0x04 |
| 1276 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1277 | int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable, |
| 1278 | int shared) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1279 | { |
| 1280 | SaveStateEntry *se; |
| 1281 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1282 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 1283 | if(se->set_params == NULL) { |
| 1284 | continue; |
| 1285 | } |
| 1286 | se->set_params(blk_enable, shared, se->opaque); |
| 1287 | } |
| 1288 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1289 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 1290 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
| 1291 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1292 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1293 | int len; |
| 1294 | |
| 1295 | if (se->save_live_state == NULL) |
| 1296 | continue; |
| 1297 | |
| 1298 | /* Section type */ |
| 1299 | qemu_put_byte(f, QEMU_VM_SECTION_START); |
| 1300 | qemu_put_be32(f, se->section_id); |
| 1301 | |
| 1302 | /* ID string */ |
| 1303 | len = strlen(se->idstr); |
| 1304 | qemu_put_byte(f, len); |
| 1305 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
| 1306 | |
| 1307 | qemu_put_be32(f, se->instance_id); |
| 1308 | qemu_put_be32(f, se->version_id); |
| 1309 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1310 | se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1313 | if (qemu_file_has_error(f)) { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1314 | qemu_savevm_state_cancel(mon, f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1315 | return -EIO; |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1316 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1317 | |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1321 | int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1322 | { |
| 1323 | SaveStateEntry *se; |
| 1324 | int ret = 1; |
| 1325 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1326 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1327 | if (se->save_live_state == NULL) |
| 1328 | continue; |
| 1329 | |
| 1330 | /* Section type */ |
| 1331 | qemu_put_byte(f, QEMU_VM_SECTION_PART); |
| 1332 | qemu_put_be32(f, se->section_id); |
| 1333 | |
Jan Kiszka | 90697be | 2009-12-01 15:19:55 +0100 | [diff] [blame] | 1334 | ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque); |
| 1335 | if (!ret) { |
| 1336 | /* Do not proceed to the next vmstate before this one reported |
| 1337 | completion of the current stage. This serializes the migration |
| 1338 | and reduces the probability that a faster changing state is |
| 1339 | synchronized over and over again. */ |
| 1340 | break; |
| 1341 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | if (ret) |
| 1345 | return 1; |
| 1346 | |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1347 | if (qemu_file_has_error(f)) { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1348 | qemu_savevm_state_cancel(mon, f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1349 | return -EIO; |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1350 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1351 | |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1355 | int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1356 | { |
| 1357 | SaveStateEntry *se; |
| 1358 | |
Jan Kiszka | ea375f9 | 2010-03-01 19:10:30 +0100 | [diff] [blame] | 1359 | cpu_synchronize_all_states(); |
| 1360 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1361 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1362 | if (se->save_live_state == NULL) |
| 1363 | continue; |
| 1364 | |
| 1365 | /* Section type */ |
| 1366 | qemu_put_byte(f, QEMU_VM_SECTION_END); |
| 1367 | qemu_put_be32(f, se->section_id); |
| 1368 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1369 | se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1372 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1373 | int len; |
| 1374 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1375 | if (se->save_state == NULL && se->vmsd == NULL) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1376 | continue; |
| 1377 | |
| 1378 | /* Section type */ |
| 1379 | qemu_put_byte(f, QEMU_VM_SECTION_FULL); |
| 1380 | qemu_put_be32(f, se->section_id); |
| 1381 | |
| 1382 | /* ID string */ |
| 1383 | len = strlen(se->idstr); |
| 1384 | qemu_put_byte(f, len); |
| 1385 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
| 1386 | |
| 1387 | qemu_put_be32(f, se->instance_id); |
| 1388 | qemu_put_be32(f, se->version_id); |
| 1389 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1390 | vmstate_save(f, se); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | qemu_put_byte(f, QEMU_VM_EOF); |
| 1394 | |
| 1395 | if (qemu_file_has_error(f)) |
| 1396 | return -EIO; |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1401 | void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f) |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1402 | { |
| 1403 | SaveStateEntry *se; |
| 1404 | |
| 1405 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 1406 | if (se->save_live_state) { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1407 | se->save_live_state(mon, f, -1, se->opaque); |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1412 | static int qemu_savevm_state(Monitor *mon, QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1413 | { |
| 1414 | int saved_vm_running; |
| 1415 | int ret; |
| 1416 | |
| 1417 | saved_vm_running = vm_running; |
| 1418 | vm_stop(0); |
| 1419 | |
| 1420 | bdrv_flush_all(); |
| 1421 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1422 | ret = qemu_savevm_state_begin(mon, f, 0, 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1423 | if (ret < 0) |
| 1424 | goto out; |
| 1425 | |
| 1426 | do { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1427 | ret = qemu_savevm_state_iterate(mon, f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1428 | if (ret < 0) |
| 1429 | goto out; |
| 1430 | } while (ret == 0); |
| 1431 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1432 | ret = qemu_savevm_state_complete(mon, f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1433 | |
| 1434 | out: |
| 1435 | if (qemu_file_has_error(f)) |
| 1436 | ret = -EIO; |
| 1437 | |
| 1438 | if (!ret && saved_vm_running) |
| 1439 | vm_start(); |
| 1440 | |
| 1441 | return ret; |
| 1442 | } |
| 1443 | |
| 1444 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
| 1445 | { |
| 1446 | SaveStateEntry *se; |
| 1447 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1448 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1449 | if (!strcmp(se->idstr, idstr) && |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1450 | (instance_id == se->instance_id || |
| 1451 | instance_id == se->alias_id)) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1452 | return se; |
| 1453 | } |
| 1454 | return NULL; |
| 1455 | } |
| 1456 | |
| 1457 | typedef struct LoadStateEntry { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1458 | QLIST_ENTRY(LoadStateEntry) entry; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1459 | SaveStateEntry *se; |
| 1460 | int section_id; |
| 1461 | int version_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1462 | } LoadStateEntry; |
| 1463 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1464 | int qemu_loadvm_state(QEMUFile *f) |
| 1465 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1466 | QLIST_HEAD(, LoadStateEntry) loadvm_handlers = |
| 1467 | QLIST_HEAD_INITIALIZER(loadvm_handlers); |
Juan Quintela | f4dbb8d | 2009-09-01 02:12:33 +0200 | [diff] [blame] | 1468 | LoadStateEntry *le, *new_le; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1469 | uint8_t section_type; |
| 1470 | unsigned int v; |
| 1471 | int ret; |
| 1472 | |
| 1473 | v = qemu_get_be32(f); |
| 1474 | if (v != QEMU_VM_FILE_MAGIC) |
| 1475 | return -EINVAL; |
| 1476 | |
| 1477 | v = qemu_get_be32(f); |
Juan Quintela | bbfe140 | 2009-09-10 03:04:24 +0200 | [diff] [blame] | 1478 | if (v == QEMU_VM_FILE_VERSION_COMPAT) { |
| 1479 | fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n"); |
| 1480 | return -ENOTSUP; |
| 1481 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1482 | if (v != QEMU_VM_FILE_VERSION) |
| 1483 | return -ENOTSUP; |
| 1484 | |
| 1485 | while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) { |
| 1486 | uint32_t instance_id, version_id, section_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1487 | SaveStateEntry *se; |
| 1488 | char idstr[257]; |
| 1489 | int len; |
| 1490 | |
| 1491 | switch (section_type) { |
| 1492 | case QEMU_VM_SECTION_START: |
| 1493 | case QEMU_VM_SECTION_FULL: |
| 1494 | /* Read section start */ |
| 1495 | section_id = qemu_get_be32(f); |
| 1496 | len = qemu_get_byte(f); |
| 1497 | qemu_get_buffer(f, (uint8_t *)idstr, len); |
| 1498 | idstr[len] = 0; |
| 1499 | instance_id = qemu_get_be32(f); |
| 1500 | version_id = qemu_get_be32(f); |
| 1501 | |
| 1502 | /* Find savevm section */ |
| 1503 | se = find_se(idstr, instance_id); |
| 1504 | if (se == NULL) { |
| 1505 | fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id); |
| 1506 | ret = -EINVAL; |
| 1507 | goto out; |
| 1508 | } |
| 1509 | |
| 1510 | /* Validate version */ |
| 1511 | if (version_id > se->version_id) { |
| 1512 | fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n", |
| 1513 | version_id, idstr, se->version_id); |
| 1514 | ret = -EINVAL; |
| 1515 | goto out; |
| 1516 | } |
| 1517 | |
| 1518 | /* Add entry */ |
| 1519 | le = qemu_mallocz(sizeof(*le)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1520 | |
| 1521 | le->se = se; |
| 1522 | le->section_id = section_id; |
| 1523 | le->version_id = version_id; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1524 | QLIST_INSERT_HEAD(&loadvm_handlers, le, entry); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1525 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1526 | ret = vmstate_load(f, le->se, le->version_id); |
Juan Quintela | b5a22e4 | 2009-08-20 19:42:23 +0200 | [diff] [blame] | 1527 | if (ret < 0) { |
| 1528 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", |
| 1529 | instance_id, idstr); |
| 1530 | goto out; |
| 1531 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1532 | break; |
| 1533 | case QEMU_VM_SECTION_PART: |
| 1534 | case QEMU_VM_SECTION_END: |
| 1535 | section_id = qemu_get_be32(f); |
| 1536 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1537 | QLIST_FOREACH(le, &loadvm_handlers, entry) { |
Juan Quintela | f4dbb8d | 2009-09-01 02:12:33 +0200 | [diff] [blame] | 1538 | if (le->section_id == section_id) { |
| 1539 | break; |
| 1540 | } |
| 1541 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1542 | if (le == NULL) { |
| 1543 | fprintf(stderr, "Unknown savevm section %d\n", section_id); |
| 1544 | ret = -EINVAL; |
| 1545 | goto out; |
| 1546 | } |
| 1547 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1548 | ret = vmstate_load(f, le->se, le->version_id); |
Juan Quintela | b5a22e4 | 2009-08-20 19:42:23 +0200 | [diff] [blame] | 1549 | if (ret < 0) { |
| 1550 | fprintf(stderr, "qemu: warning: error while loading state section id %d\n", |
| 1551 | section_id); |
| 1552 | goto out; |
| 1553 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1554 | break; |
| 1555 | default: |
| 1556 | fprintf(stderr, "Unknown savevm section type %d\n", section_type); |
| 1557 | ret = -EINVAL; |
| 1558 | goto out; |
| 1559 | } |
| 1560 | } |
| 1561 | |
Jan Kiszka | ea375f9 | 2010-03-01 19:10:30 +0100 | [diff] [blame] | 1562 | cpu_synchronize_all_post_init(); |
| 1563 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1564 | ret = 0; |
| 1565 | |
| 1566 | out: |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1567 | QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) { |
| 1568 | QLIST_REMOVE(le, entry); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1569 | qemu_free(le); |
| 1570 | } |
| 1571 | |
| 1572 | if (qemu_file_has_error(f)) |
| 1573 | ret = -EIO; |
| 1574 | |
| 1575 | return ret; |
| 1576 | } |
| 1577 | |
| 1578 | /* device can contain snapshots */ |
| 1579 | static int bdrv_can_snapshot(BlockDriverState *bs) |
| 1580 | { |
| 1581 | return (bs && |
| 1582 | !bdrv_is_removable(bs) && |
| 1583 | !bdrv_is_read_only(bs)); |
| 1584 | } |
| 1585 | |
| 1586 | /* device must be snapshots in order to have a reliable snapshot */ |
| 1587 | static int bdrv_has_snapshot(BlockDriverState *bs) |
| 1588 | { |
| 1589 | return (bs && |
| 1590 | !bdrv_is_removable(bs) && |
| 1591 | !bdrv_is_read_only(bs)); |
| 1592 | } |
| 1593 | |
| 1594 | static BlockDriverState *get_bs_snapshots(void) |
| 1595 | { |
| 1596 | BlockDriverState *bs; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1597 | DriveInfo *dinfo; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1598 | |
| 1599 | if (bs_snapshots) |
| 1600 | return bs_snapshots; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1601 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1602 | bs = dinfo->bdrv; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1603 | if (bdrv_can_snapshot(bs)) |
| 1604 | goto ok; |
| 1605 | } |
| 1606 | return NULL; |
| 1607 | ok: |
| 1608 | bs_snapshots = bs; |
| 1609 | return bs; |
| 1610 | } |
| 1611 | |
| 1612 | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
| 1613 | const char *name) |
| 1614 | { |
| 1615 | QEMUSnapshotInfo *sn_tab, *sn; |
| 1616 | int nb_sns, i, ret; |
| 1617 | |
| 1618 | ret = -ENOENT; |
| 1619 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 1620 | if (nb_sns < 0) |
| 1621 | return ret; |
| 1622 | for(i = 0; i < nb_sns; i++) { |
| 1623 | sn = &sn_tab[i]; |
| 1624 | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { |
| 1625 | *sn_info = *sn; |
| 1626 | ret = 0; |
| 1627 | break; |
| 1628 | } |
| 1629 | } |
| 1630 | qemu_free(sn_tab); |
| 1631 | return ret; |
| 1632 | } |
| 1633 | |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1634 | /* |
| 1635 | * Deletes snapshots of a given name in all opened images. |
| 1636 | */ |
| 1637 | static int del_existing_snapshots(Monitor *mon, const char *name) |
| 1638 | { |
| 1639 | BlockDriverState *bs; |
| 1640 | DriveInfo *dinfo; |
| 1641 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
| 1642 | int ret; |
| 1643 | |
| 1644 | QTAILQ_FOREACH(dinfo, &drives, next) { |
| 1645 | bs = dinfo->bdrv; |
| 1646 | if (bdrv_can_snapshot(bs) && |
| 1647 | bdrv_snapshot_find(bs, snapshot, name) >= 0) |
| 1648 | { |
| 1649 | ret = bdrv_snapshot_delete(bs, name); |
| 1650 | if (ret < 0) { |
| 1651 | monitor_printf(mon, |
| 1652 | "Error while deleting snapshot on '%s'\n", |
| 1653 | bdrv_get_device_name(bs)); |
| 1654 | return -1; |
| 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1662 | void do_savevm(Monitor *mon, const QDict *qdict) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1663 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1664 | DriveInfo *dinfo; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1665 | BlockDriverState *bs, *bs1; |
| 1666 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1667 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1668 | QEMUFile *f; |
| 1669 | int saved_vm_running; |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 1670 | uint32_t vm_state_size; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1671 | #ifdef _WIN32 |
| 1672 | struct _timeb tb; |
| 1673 | #else |
| 1674 | struct timeval tv; |
| 1675 | #endif |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1676 | const char *name = qdict_get_try_str(qdict, "name"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1677 | |
| 1678 | bs = get_bs_snapshots(); |
| 1679 | if (!bs) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1680 | monitor_printf(mon, "No block device can accept snapshots\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1681 | return; |
| 1682 | } |
| 1683 | |
| 1684 | /* ??? Should this occur after vm_stop? */ |
| 1685 | qemu_aio_flush(); |
| 1686 | |
| 1687 | saved_vm_running = vm_running; |
| 1688 | vm_stop(0); |
| 1689 | |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1690 | memset(sn, 0, sizeof(*sn)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1691 | if (name) { |
| 1692 | ret = bdrv_snapshot_find(bs, old_sn, name); |
| 1693 | if (ret >= 0) { |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1694 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); |
| 1695 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); |
| 1696 | } else { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1697 | pstrcpy(sn->name, sizeof(sn->name), name); |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1698 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | /* fill auxiliary fields */ |
| 1702 | #ifdef _WIN32 |
| 1703 | _ftime(&tb); |
| 1704 | sn->date_sec = tb.time; |
| 1705 | sn->date_nsec = tb.millitm * 1000000; |
| 1706 | #else |
| 1707 | gettimeofday(&tv, NULL); |
| 1708 | sn->date_sec = tv.tv_sec; |
| 1709 | sn->date_nsec = tv.tv_usec * 1000; |
| 1710 | #endif |
| 1711 | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
| 1712 | |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1713 | /* Delete old snapshots of the same name */ |
Marcelo Tosatti | f139a41 | 2010-01-20 14:26:34 -0200 | [diff] [blame] | 1714 | if (name && del_existing_snapshots(mon, name) < 0) { |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1715 | goto the_end; |
| 1716 | } |
| 1717 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1718 | /* save the VM state */ |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1719 | f = qemu_fopen_bdrv(bs, 1); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1720 | if (!f) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1721 | monitor_printf(mon, "Could not open VM state file\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1722 | goto the_end; |
| 1723 | } |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1724 | ret = qemu_savevm_state(mon, f); |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 1725 | vm_state_size = qemu_ftell(f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1726 | qemu_fclose(f); |
| 1727 | if (ret < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1728 | monitor_printf(mon, "Error %d while writing VM\n", ret); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1729 | goto the_end; |
| 1730 | } |
| 1731 | |
| 1732 | /* create the snapshots */ |
| 1733 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1734 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1735 | bs1 = dinfo->bdrv; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1736 | if (bdrv_has_snapshot(bs1)) { |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 1737 | /* Write VM state size only to the image that contains the state */ |
| 1738 | sn->vm_state_size = (bs == bs1 ? vm_state_size : 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1739 | ret = bdrv_snapshot_create(bs1, sn); |
| 1740 | if (ret < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1741 | monitor_printf(mon, "Error while creating snapshot on '%s'\n", |
| 1742 | bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | the_end: |
| 1748 | if (saved_vm_running) |
| 1749 | vm_start(); |
| 1750 | } |
| 1751 | |
Markus Armbruster | 03cd465 | 2010-02-17 16:24:10 +0100 | [diff] [blame] | 1752 | int load_vmstate(const char *name) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1753 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1754 | DriveInfo *dinfo; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1755 | BlockDriverState *bs, *bs1; |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 1756 | QEMUSnapshotInfo sn; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1757 | QEMUFile *f; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1758 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1759 | |
| 1760 | bs = get_bs_snapshots(); |
| 1761 | if (!bs) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 1762 | error_report("No block device supports snapshots"); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 1763 | return -EINVAL; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | /* Flush all IO requests so they don't interfere with the new state. */ |
| 1767 | qemu_aio_flush(); |
| 1768 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1769 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1770 | bs1 = dinfo->bdrv; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1771 | if (bdrv_has_snapshot(bs1)) { |
| 1772 | ret = bdrv_snapshot_goto(bs1, name); |
| 1773 | if (ret < 0) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1774 | switch(ret) { |
| 1775 | case -ENOTSUP: |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 1776 | error_report("%sSnapshots not supported on device '%s'", |
| 1777 | bs != bs1 ? "Warning: " : "", |
| 1778 | bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1779 | break; |
| 1780 | case -ENOENT: |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 1781 | error_report("%sCould not find snapshot '%s' on device '%s'", |
| 1782 | bs != bs1 ? "Warning: " : "", |
| 1783 | name, bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1784 | break; |
| 1785 | default: |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 1786 | error_report("%sError %d while activating snapshot on '%s'", |
| 1787 | bs != bs1 ? "Warning: " : "", |
| 1788 | ret, bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1789 | break; |
| 1790 | } |
| 1791 | /* fatal on snapshot block device */ |
| 1792 | if (bs == bs1) |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 1793 | return 0; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | } |
| 1797 | |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 1798 | /* Don't even try to load empty VM states */ |
| 1799 | ret = bdrv_snapshot_find(bs, &sn, name); |
| 1800 | if ((ret >= 0) && (sn.vm_state_size == 0)) |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 1801 | return -EINVAL; |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 1802 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1803 | /* restore the VM state */ |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1804 | f = qemu_fopen_bdrv(bs, 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1805 | if (!f) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 1806 | error_report("Could not open VM state file"); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 1807 | return -EINVAL; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1808 | } |
| 1809 | ret = qemu_loadvm_state(f); |
| 1810 | qemu_fclose(f); |
| 1811 | if (ret < 0) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 1812 | error_report("Error %d while loading VM state", ret); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 1813 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1814 | } |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 1815 | return 0; |
Juan Quintela | 7b63034 | 2009-08-20 19:42:20 +0200 | [diff] [blame] | 1816 | } |
| 1817 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1818 | void do_delvm(Monitor *mon, const QDict *qdict) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1819 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1820 | DriveInfo *dinfo; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1821 | BlockDriverState *bs, *bs1; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1822 | int ret; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1823 | const char *name = qdict_get_str(qdict, "name"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1824 | |
| 1825 | bs = get_bs_snapshots(); |
| 1826 | if (!bs) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1827 | monitor_printf(mon, "No block device supports snapshots\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1828 | return; |
| 1829 | } |
| 1830 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1831 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1832 | bs1 = dinfo->bdrv; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1833 | if (bdrv_has_snapshot(bs1)) { |
| 1834 | ret = bdrv_snapshot_delete(bs1, name); |
| 1835 | if (ret < 0) { |
| 1836 | if (ret == -ENOTSUP) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1837 | monitor_printf(mon, |
| 1838 | "Snapshots not supported on device '%s'\n", |
| 1839 | bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1840 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1841 | monitor_printf(mon, "Error %d while deleting snapshot on " |
| 1842 | "'%s'\n", ret, bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | } |
| 1847 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1848 | void do_info_snapshots(Monitor *mon) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1849 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1850 | DriveInfo *dinfo; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1851 | BlockDriverState *bs, *bs1; |
| 1852 | QEMUSnapshotInfo *sn_tab, *sn; |
| 1853 | int nb_sns, i; |
| 1854 | char buf[256]; |
| 1855 | |
| 1856 | bs = get_bs_snapshots(); |
| 1857 | if (!bs) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1858 | monitor_printf(mon, "No available block device supports snapshots\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1859 | return; |
| 1860 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1861 | monitor_printf(mon, "Snapshot devices:"); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1862 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1863 | bs1 = dinfo->bdrv; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1864 | if (bdrv_has_snapshot(bs1)) { |
| 1865 | if (bs == bs1) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1866 | monitor_printf(mon, " %s", bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1867 | } |
| 1868 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1869 | monitor_printf(mon, "\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1870 | |
| 1871 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 1872 | if (nb_sns < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1873 | monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1874 | return; |
| 1875 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1876 | monitor_printf(mon, "Snapshot list (from %s):\n", |
| 1877 | bdrv_get_device_name(bs)); |
| 1878 | monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1879 | for(i = 0; i < nb_sns; i++) { |
| 1880 | sn = &sn_tab[i]; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1881 | monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1882 | } |
| 1883 | qemu_free(sn_tab); |
| 1884 | } |