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> |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 26 | #include <time.h> |
| 27 | #include <errno.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <zlib.h> |
| 30 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 31 | /* Needed early for CONFIG_BSD etc. */ |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 32 | #include "config-host.h" |
| 33 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 34 | #ifndef _WIN32 |
| 35 | #include <sys/times.h> |
| 36 | #include <sys/wait.h> |
| 37 | #include <termios.h> |
| 38 | #include <sys/mman.h> |
| 39 | #include <sys/ioctl.h> |
| 40 | #include <sys/resource.h> |
| 41 | #include <sys/socket.h> |
| 42 | #include <netinet/in.h> |
| 43 | #include <net/if.h> |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 44 | #include <arpa/inet.h> |
| 45 | #include <dirent.h> |
| 46 | #include <netdb.h> |
| 47 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_BSD |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 49 | #include <sys/stat.h> |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 50 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 51 | #include <libutil.h> |
| 52 | #else |
| 53 | #include <util.h> |
| 54 | #endif |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 55 | #ifdef __linux__ |
| 56 | #include <pty.h> |
| 57 | #include <malloc.h> |
| 58 | #include <linux/rtc.h> |
| 59 | #endif |
| 60 | #endif |
| 61 | #endif |
| 62 | |
| 63 | #ifdef _WIN32 |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 64 | #include <windows.h> |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 65 | #include <malloc.h> |
| 66 | #include <sys/timeb.h> |
| 67 | #include <mmsystem.h> |
| 68 | #define getopt_long_only getopt_long |
| 69 | #define memalign(align, size) malloc(size) |
| 70 | #endif |
| 71 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 72 | #include "qemu-common.h" |
| 73 | #include "hw/hw.h" |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 74 | #include "hw/qdev.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 75 | #include "net.h" |
| 76 | #include "monitor.h" |
| 77 | #include "sysemu.h" |
| 78 | #include "qemu-timer.h" |
| 79 | #include "qemu-char.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 80 | #include "audio/audio.h" |
| 81 | #include "migration.h" |
| 82 | #include "qemu_socket.h" |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 83 | #include "qemu-queue.h" |
Paolo Bonzini | 2ff68d0 | 2011-09-12 16:21:44 +0200 | [diff] [blame] | 84 | #include "qemu-timer.h" |
Blue Swirl | 17a4663 | 2011-03-27 16:05:08 +0000 | [diff] [blame] | 85 | #include "cpus.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 86 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 87 | #define SELF_ANNOUNCE_ROUNDS 5 |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 88 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 89 | #ifndef ETH_P_RARP |
Stefan Berger | f8778a7 | 2010-04-24 08:54:07 -0400 | [diff] [blame] | 90 | #define ETH_P_RARP 0x8035 |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 91 | #endif |
| 92 | #define ARP_HTYPE_ETH 0x0001 |
| 93 | #define ARP_PTYPE_IP 0x0800 |
| 94 | #define ARP_OP_REQUEST_REV 0x3 |
| 95 | |
| 96 | static int announce_self_create(uint8_t *buf, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 97 | uint8_t *mac_addr) |
| 98 | { |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 99 | /* Ethernet header. */ |
| 100 | memset(buf, 0xff, 6); /* destination MAC addr */ |
| 101 | memcpy(buf + 6, mac_addr, 6); /* source MAC addr */ |
| 102 | *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 103 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 104 | /* RARP header. */ |
| 105 | *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */ |
| 106 | *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */ |
| 107 | *(buf + 18) = 6; /* hardware addr length (ethernet) */ |
| 108 | *(buf + 19) = 4; /* protocol addr length (IPv4) */ |
| 109 | *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */ |
| 110 | memcpy(buf + 22, mac_addr, 6); /* source hw addr */ |
| 111 | memset(buf + 28, 0x00, 4); /* source protocol addr */ |
| 112 | memcpy(buf + 32, mac_addr, 6); /* target hw addr */ |
| 113 | memset(buf + 38, 0x00, 4); /* target protocol addr */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 114 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 115 | /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */ |
| 116 | memset(buf + 42, 0x00, 18); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 117 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 118 | return 60; /* len (FCS will be added by hardware) */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Mark McLoughlin | f401ca2 | 2009-11-25 18:49:32 +0000 | [diff] [blame] | 121 | static void qemu_announce_self_iter(NICState *nic, void *opaque) |
| 122 | { |
| 123 | uint8_t buf[60]; |
| 124 | int len; |
| 125 | |
| 126 | len = announce_self_create(buf, nic->conf->macaddr.a); |
| 127 | |
| 128 | qemu_send_packet_raw(&nic->nc, buf, len); |
| 129 | } |
| 130 | |
| 131 | |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 132 | static void qemu_announce_self_once(void *opaque) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 133 | { |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 134 | static int count = SELF_ANNOUNCE_ROUNDS; |
| 135 | QEMUTimer *timer = *(QEMUTimer **)opaque; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 136 | |
Mark McLoughlin | f401ca2 | 2009-11-25 18:49:32 +0000 | [diff] [blame] | 137 | qemu_foreach_nic(qemu_announce_self_iter, NULL); |
| 138 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 139 | if (--count) { |
| 140 | /* delay 50ms, 150ms, 250ms, ... */ |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 141 | qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) + |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 142 | 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100); |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 143 | } else { |
| 144 | qemu_del_timer(timer); |
| 145 | qemu_free_timer(timer); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void qemu_announce_self(void) |
| 150 | { |
| 151 | static QEMUTimer *timer; |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 152 | timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer); |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 153 | qemu_announce_self_once(&timer); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /***********************************************************/ |
| 157 | /* savevm/loadvm support */ |
| 158 | |
| 159 | #define IO_BUF_SIZE 32768 |
| 160 | |
| 161 | struct QEMUFile { |
| 162 | QEMUFilePutBufferFunc *put_buffer; |
| 163 | QEMUFileGetBufferFunc *get_buffer; |
| 164 | QEMUFileCloseFunc *close; |
| 165 | QEMUFileRateLimit *rate_limit; |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 166 | QEMUFileSetRateLimit *set_rate_limit; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 167 | QEMUFileGetRateLimit *get_rate_limit; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 168 | void *opaque; |
| 169 | int is_write; |
| 170 | |
| 171 | int64_t buf_offset; /* start of buffer when writing, end of buffer |
| 172 | when reading */ |
| 173 | int buf_index; |
| 174 | int buf_size; /* 0 when writing */ |
| 175 | uint8_t buf[IO_BUF_SIZE]; |
| 176 | |
Juan Quintela | 3961b4d | 2011-10-05 01:05:21 +0200 | [diff] [blame] | 177 | int last_error; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 180 | typedef struct QEMUFileStdio |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 181 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 182 | FILE *stdio_file; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 183 | QEMUFile *file; |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 184 | } QEMUFileStdio; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 185 | |
| 186 | typedef struct QEMUFileSocket |
| 187 | { |
| 188 | int fd; |
| 189 | QEMUFile *file; |
| 190 | } QEMUFileSocket; |
| 191 | |
| 192 | static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
| 193 | { |
| 194 | QEMUFileSocket *s = opaque; |
| 195 | ssize_t len; |
| 196 | |
| 197 | do { |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 198 | len = qemu_recv(s->fd, buf, size, 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 199 | } while (len == -1 && socket_error() == EINTR); |
| 200 | |
| 201 | if (len == -1) |
| 202 | len = -socket_error(); |
| 203 | |
| 204 | return len; |
| 205 | } |
| 206 | |
| 207 | static int socket_close(void *opaque) |
| 208 | { |
| 209 | QEMUFileSocket *s = opaque; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 210 | g_free(s); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 214 | 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] | 215 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 216 | QEMUFileStdio *s = opaque; |
| 217 | return fwrite(buf, 1, size, s->stdio_file); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 220 | 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] | 221 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 222 | QEMUFileStdio *s = opaque; |
| 223 | FILE *fp = s->stdio_file; |
Uri Lublin | 8a67ec4 | 2009-06-08 19:27:21 +0300 | [diff] [blame] | 224 | int bytes; |
| 225 | |
| 226 | do { |
| 227 | clearerr(fp); |
| 228 | bytes = fread(buf, 1, size, fp); |
| 229 | } while ((bytes == 0) && ferror(fp) && (errno == EINTR)); |
| 230 | return bytes; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 233 | static int stdio_pclose(void *opaque) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 234 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 235 | QEMUFileStdio *s = opaque; |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 236 | int ret; |
| 237 | ret = pclose(s->stdio_file); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 238 | g_free(s); |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 239 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 242 | static int stdio_fclose(void *opaque) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 243 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 244 | QEMUFileStdio *s = opaque; |
| 245 | fclose(s->stdio_file); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 246 | g_free(s); |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 247 | return 0; |
| 248 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 249 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 250 | QEMUFile *qemu_popen(FILE *stdio_file, const char *mode) |
| 251 | { |
| 252 | QEMUFileStdio *s; |
| 253 | |
| 254 | 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] | 255 | fprintf(stderr, "qemu_popen: Argument validity check failed\n"); |
| 256 | return NULL; |
| 257 | } |
| 258 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 259 | s = g_malloc0(sizeof(QEMUFileStdio)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 260 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 261 | s->stdio_file = stdio_file; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 262 | |
| 263 | if(mode[0] == 'r') { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 264 | s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, |
| 265 | NULL, NULL, NULL); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 266 | } else { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 267 | s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, |
| 268 | NULL, NULL, NULL); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 269 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 270 | return s->file; |
| 271 | } |
| 272 | |
| 273 | QEMUFile *qemu_popen_cmd(const char *command, const char *mode) |
| 274 | { |
| 275 | FILE *popen_file; |
| 276 | |
| 277 | popen_file = popen(command, mode); |
| 278 | if(popen_file == NULL) { |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | return qemu_popen(popen_file, mode); |
| 283 | } |
| 284 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 285 | int qemu_stdio_fd(QEMUFile *f) |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 286 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 287 | QEMUFileStdio *p; |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 288 | int fd; |
| 289 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 290 | p = (QEMUFileStdio *)f->opaque; |
| 291 | fd = fileno(p->stdio_file); |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 292 | |
| 293 | return fd; |
| 294 | } |
| 295 | |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 296 | QEMUFile *qemu_fdopen(int fd, const char *mode) |
| 297 | { |
| 298 | QEMUFileStdio *s; |
| 299 | |
| 300 | if (mode == NULL || |
| 301 | (mode[0] != 'r' && mode[0] != 'w') || |
| 302 | mode[1] != 'b' || mode[2] != 0) { |
| 303 | fprintf(stderr, "qemu_fdopen: Argument validity check failed\n"); |
| 304 | return NULL; |
| 305 | } |
| 306 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 307 | s = g_malloc0(sizeof(QEMUFileStdio)); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 308 | s->stdio_file = fdopen(fd, mode); |
| 309 | if (!s->stdio_file) |
| 310 | goto fail; |
| 311 | |
| 312 | if(mode[0] == 'r') { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 313 | s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, |
| 314 | NULL, NULL, NULL); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 315 | } else { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 316 | s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, |
| 317 | NULL, NULL, NULL); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 318 | } |
| 319 | return s->file; |
| 320 | |
| 321 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 322 | g_free(s); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 323 | return NULL; |
| 324 | } |
| 325 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 326 | QEMUFile *qemu_fopen_socket(int fd) |
| 327 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 328 | QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 329 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 330 | s->fd = fd; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 331 | s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, |
| 332 | NULL, NULL, NULL); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 333 | return s->file; |
| 334 | } |
| 335 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 336 | static int file_put_buffer(void *opaque, const uint8_t *buf, |
| 337 | int64_t pos, int size) |
| 338 | { |
| 339 | QEMUFileStdio *s = opaque; |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 340 | fseek(s->stdio_file, pos, SEEK_SET); |
Kirill A. Shutemov | 5fdb3aa | 2009-12-25 18:19:19 +0000 | [diff] [blame] | 341 | return fwrite(buf, 1, size, s->stdio_file); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
| 345 | { |
| 346 | QEMUFileStdio *s = opaque; |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 347 | fseek(s->stdio_file, pos, SEEK_SET); |
| 348 | return fread(buf, 1, size, s->stdio_file); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
| 352 | { |
| 353 | QEMUFileStdio *s; |
| 354 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 355 | if (mode == NULL || |
| 356 | (mode[0] != 'r' && mode[0] != 'w') || |
| 357 | mode[1] != 'b' || mode[2] != 0) { |
Blue Swirl | 090414a | 2010-03-13 11:36:09 +0000 | [diff] [blame] | 358 | fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| 361 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 362 | s = g_malloc0(sizeof(QEMUFileStdio)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 363 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 364 | s->stdio_file = fopen(filename, mode); |
| 365 | if (!s->stdio_file) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 366 | goto fail; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 367 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 368 | if(mode[0] == 'w') { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 369 | s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, |
| 370 | NULL, NULL, NULL); |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 371 | } else { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 372 | s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, |
| 373 | NULL, NULL, NULL); |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 374 | } |
| 375 | return s->file; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 376 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 377 | g_free(s); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 378 | return NULL; |
| 379 | } |
| 380 | |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 381 | static int block_put_buffer(void *opaque, const uint8_t *buf, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 382 | int64_t pos, int size) |
| 383 | { |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 384 | bdrv_save_vmstate(opaque, buf, pos, size); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 385 | return size; |
| 386 | } |
| 387 | |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 388 | 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] | 389 | { |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 390 | return bdrv_load_vmstate(opaque, buf, pos, size); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static int bdrv_fclose(void *opaque) |
| 394 | { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 398 | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 399 | { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 400 | if (is_writable) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 401 | return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose, |
| 402 | NULL, NULL, NULL); |
| 403 | 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] | 404 | } |
| 405 | |
| 406 | QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer, |
| 407 | QEMUFileGetBufferFunc *get_buffer, |
| 408 | QEMUFileCloseFunc *close, |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 409 | QEMUFileRateLimit *rate_limit, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 410 | QEMUFileSetRateLimit *set_rate_limit, |
| 411 | QEMUFileGetRateLimit *get_rate_limit) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 412 | { |
| 413 | QEMUFile *f; |
| 414 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 415 | f = g_malloc0(sizeof(QEMUFile)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 416 | |
| 417 | f->opaque = opaque; |
| 418 | f->put_buffer = put_buffer; |
| 419 | f->get_buffer = get_buffer; |
| 420 | f->close = close; |
| 421 | f->rate_limit = rate_limit; |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 422 | f->set_rate_limit = set_rate_limit; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 423 | f->get_rate_limit = get_rate_limit; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 424 | f->is_write = 0; |
| 425 | |
| 426 | return f; |
| 427 | } |
| 428 | |
Juan Quintela | 624b9cc | 2011-10-05 01:02:52 +0200 | [diff] [blame] | 429 | int qemu_file_get_error(QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 430 | { |
Juan Quintela | 3961b4d | 2011-10-05 01:05:21 +0200 | [diff] [blame] | 431 | return f->last_error; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Juan Quintela | dcd1d22 | 2011-09-21 23:01:54 +0200 | [diff] [blame] | 434 | void qemu_file_set_error(QEMUFile *f, int ret) |
aliguori | 4dabe24 | 2009-04-05 19:30:51 +0000 | [diff] [blame] | 435 | { |
Juan Quintela | 3961b4d | 2011-10-05 01:05:21 +0200 | [diff] [blame] | 436 | f->last_error = ret; |
aliguori | 4dabe24 | 2009-04-05 19:30:51 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Eduardo Habkost | d82ca91 | 2011-11-10 10:41:43 -0200 | [diff] [blame^] | 439 | /** Sets last_error conditionally |
| 440 | * |
| 441 | * Sets last_error only if ret is negative _and_ no error |
| 442 | * was set before. |
| 443 | */ |
| 444 | static void qemu_file_set_if_error(QEMUFile *f, int ret) |
| 445 | { |
| 446 | if (ret < 0 && !f->last_error) { |
| 447 | qemu_file_set_error(f, ret); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /** Flushes QEMUFile buffer |
| 452 | * |
| 453 | * In case of error, last_error is set. |
| 454 | */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 455 | void qemu_fflush(QEMUFile *f) |
| 456 | { |
| 457 | if (!f->put_buffer) |
| 458 | return; |
| 459 | |
| 460 | if (f->is_write && f->buf_index > 0) { |
| 461 | int len; |
| 462 | |
| 463 | len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index); |
| 464 | if (len > 0) |
| 465 | f->buf_offset += f->buf_index; |
| 466 | else |
Eduardo Habkost | c29110d | 2011-11-10 10:41:39 -0200 | [diff] [blame] | 467 | qemu_file_set_error(f, -EINVAL); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 468 | f->buf_index = 0; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | static void qemu_fill_buffer(QEMUFile *f) |
| 473 | { |
| 474 | int len; |
Juan Quintela | 0046c45 | 2011-09-30 19:28:45 +0200 | [diff] [blame] | 475 | int pending; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 476 | |
| 477 | if (!f->get_buffer) |
| 478 | return; |
| 479 | |
| 480 | if (f->is_write) |
| 481 | abort(); |
| 482 | |
Juan Quintela | 0046c45 | 2011-09-30 19:28:45 +0200 | [diff] [blame] | 483 | pending = f->buf_size - f->buf_index; |
| 484 | if (pending > 0) { |
| 485 | memmove(f->buf, f->buf + f->buf_index, pending); |
| 486 | } |
| 487 | f->buf_index = 0; |
| 488 | f->buf_size = pending; |
| 489 | |
| 490 | len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset, |
| 491 | IO_BUF_SIZE - pending); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 492 | if (len > 0) { |
Juan Quintela | 0046c45 | 2011-09-30 19:28:45 +0200 | [diff] [blame] | 493 | f->buf_size += len; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 494 | f->buf_offset += len; |
Juan Quintela | fa39a30 | 2011-10-25 19:18:58 +0200 | [diff] [blame] | 495 | } else if (len == 0) { |
| 496 | f->last_error = -EIO; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 497 | } else if (len != -EAGAIN) |
Eduardo Habkost | c29110d | 2011-11-10 10:41:39 -0200 | [diff] [blame] | 498 | qemu_file_set_error(f, len); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Eduardo Habkost | d82ca91 | 2011-11-10 10:41:43 -0200 | [diff] [blame^] | 501 | /** Calls close function and set last_error if needed |
| 502 | * |
| 503 | * Internal function. qemu_fflush() must be called before this. |
| 504 | * |
| 505 | * Returns f->close() return value, or 0 if close function is not set. |
| 506 | */ |
| 507 | static int qemu_close(QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 508 | { |
| 509 | int ret = 0; |
Eduardo Habkost | d82ca91 | 2011-11-10 10:41:43 -0200 | [diff] [blame^] | 510 | if (f->close) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 511 | ret = f->close(f->opaque); |
Eduardo Habkost | d82ca91 | 2011-11-10 10:41:43 -0200 | [diff] [blame^] | 512 | qemu_file_set_if_error(f, ret); |
| 513 | } |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | /** Closes the file |
| 518 | * |
| 519 | * Returns negative error value if any error happened on previous operations or |
| 520 | * while closing the file. Returns 0 or positive number on success. |
| 521 | * |
| 522 | * The meaning of return value on success depends on the specific backend |
| 523 | * being used. |
| 524 | */ |
| 525 | int qemu_fclose(QEMUFile *f) |
| 526 | { |
| 527 | int ret; |
| 528 | qemu_fflush(f); |
| 529 | ret = qemu_close(f); |
| 530 | /* If any error was spotted before closing, we should report it |
| 531 | * instead of the close() return value. |
| 532 | */ |
| 533 | if (f->last_error) { |
| 534 | ret = f->last_error; |
| 535 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 536 | g_free(f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | void qemu_file_put_notify(QEMUFile *f) |
| 541 | { |
| 542 | f->put_buffer(f->opaque, NULL, 0, 0); |
| 543 | } |
| 544 | |
| 545 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
| 546 | { |
| 547 | int l; |
| 548 | |
Juan Quintela | 3961b4d | 2011-10-05 01:05:21 +0200 | [diff] [blame] | 549 | if (!f->last_error && f->is_write == 0 && f->buf_index > 0) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 550 | fprintf(stderr, |
| 551 | "Attempted to write to buffer while read buffer is not empty\n"); |
| 552 | abort(); |
| 553 | } |
| 554 | |
Juan Quintela | 3961b4d | 2011-10-05 01:05:21 +0200 | [diff] [blame] | 555 | while (!f->last_error && size > 0) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 556 | l = IO_BUF_SIZE - f->buf_index; |
| 557 | if (l > size) |
| 558 | l = size; |
| 559 | memcpy(f->buf + f->buf_index, buf, l); |
| 560 | f->is_write = 1; |
| 561 | f->buf_index += l; |
| 562 | buf += l; |
| 563 | size -= l; |
| 564 | if (f->buf_index >= IO_BUF_SIZE) |
| 565 | qemu_fflush(f); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void qemu_put_byte(QEMUFile *f, int v) |
| 570 | { |
Juan Quintela | 3961b4d | 2011-10-05 01:05:21 +0200 | [diff] [blame] | 571 | if (!f->last_error && f->is_write == 0 && f->buf_index > 0) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 572 | fprintf(stderr, |
| 573 | "Attempted to write to buffer while read buffer is not empty\n"); |
| 574 | abort(); |
| 575 | } |
| 576 | |
| 577 | f->buf[f->buf_index++] = v; |
| 578 | f->is_write = 1; |
| 579 | if (f->buf_index >= IO_BUF_SIZE) |
| 580 | qemu_fflush(f); |
| 581 | } |
| 582 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 583 | static void qemu_file_skip(QEMUFile *f, int size) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 584 | { |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 585 | if (f->buf_index + size <= f->buf_size) { |
| 586 | f->buf_index += size; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 587 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 590 | static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset) |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 591 | { |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 592 | int pending; |
| 593 | int index; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 594 | |
Juan Quintela | b9ce145 | 2011-10-04 13:55:32 +0200 | [diff] [blame] | 595 | if (f->is_write) { |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 596 | abort(); |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 597 | } |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 598 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 599 | index = f->buf_index + offset; |
| 600 | pending = f->buf_size - index; |
| 601 | if (pending < size) { |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 602 | qemu_fill_buffer(f); |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 603 | index = f->buf_index + offset; |
| 604 | pending = f->buf_size - index; |
| 605 | } |
| 606 | |
| 607 | if (pending <= 0) { |
| 608 | return 0; |
| 609 | } |
| 610 | if (size > pending) { |
| 611 | size = pending; |
| 612 | } |
| 613 | |
| 614 | memcpy(buf, f->buf + index, size); |
| 615 | return size; |
| 616 | } |
| 617 | |
| 618 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) |
| 619 | { |
| 620 | int pending = size; |
| 621 | int done = 0; |
| 622 | |
| 623 | while (pending > 0) { |
| 624 | int res; |
| 625 | |
| 626 | res = qemu_peek_buffer(f, buf, pending, 0); |
| 627 | if (res == 0) { |
| 628 | return done; |
| 629 | } |
| 630 | qemu_file_skip(f, res); |
| 631 | buf += res; |
| 632 | pending -= res; |
| 633 | done += res; |
| 634 | } |
| 635 | return done; |
| 636 | } |
| 637 | |
| 638 | static int qemu_peek_byte(QEMUFile *f, int offset) |
| 639 | { |
| 640 | int index = f->buf_index + offset; |
| 641 | |
| 642 | if (f->is_write) { |
| 643 | abort(); |
| 644 | } |
| 645 | |
| 646 | if (index >= f->buf_size) { |
| 647 | qemu_fill_buffer(f); |
| 648 | index = f->buf_index + offset; |
| 649 | if (index >= f->buf_size) { |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 650 | return 0; |
Juan Quintela | b9ce145 | 2011-10-04 13:55:32 +0200 | [diff] [blame] | 651 | } |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 652 | } |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 653 | return f->buf[index]; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 654 | } |
| 655 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 656 | int qemu_get_byte(QEMUFile *f) |
| 657 | { |
Juan Quintela | 65f3bb3 | 2011-10-06 14:29:32 +0200 | [diff] [blame] | 658 | int result; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 659 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 660 | result = qemu_peek_byte(f, 0); |
| 661 | qemu_file_skip(f, 1); |
Juan Quintela | 65f3bb3 | 2011-10-06 14:29:32 +0200 | [diff] [blame] | 662 | return result; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | int64_t qemu_ftell(QEMUFile *f) |
| 666 | { |
| 667 | return f->buf_offset - f->buf_size + f->buf_index; |
| 668 | } |
| 669 | |
| 670 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) |
| 671 | { |
| 672 | if (whence == SEEK_SET) { |
| 673 | /* nothing to do */ |
| 674 | } else if (whence == SEEK_CUR) { |
| 675 | pos += qemu_ftell(f); |
| 676 | } else { |
| 677 | /* SEEK_END not supported */ |
| 678 | return -1; |
| 679 | } |
| 680 | if (f->put_buffer) { |
| 681 | qemu_fflush(f); |
| 682 | f->buf_offset = pos; |
| 683 | } else { |
| 684 | f->buf_offset = pos; |
| 685 | f->buf_index = 0; |
| 686 | f->buf_size = 0; |
| 687 | } |
| 688 | return pos; |
| 689 | } |
| 690 | |
| 691 | int qemu_file_rate_limit(QEMUFile *f) |
| 692 | { |
| 693 | if (f->rate_limit) |
| 694 | return f->rate_limit(f->opaque); |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
Michael S. Tsirkin | 3d002df | 2010-11-23 19:05:54 +0200 | [diff] [blame] | 699 | int64_t qemu_file_get_rate_limit(QEMUFile *f) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 700 | { |
| 701 | if (f->get_rate_limit) |
| 702 | return f->get_rate_limit(f->opaque); |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
Michael S. Tsirkin | 3d002df | 2010-11-23 19:05:54 +0200 | [diff] [blame] | 707 | int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate) |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 708 | { |
Glauber Costa | 0bb05ea | 2009-07-14 18:26:51 -0400 | [diff] [blame] | 709 | /* any failed or completed migration keeps its state to allow probing of |
| 710 | * migration data, but has no associated file anymore */ |
| 711 | if (f && f->set_rate_limit) |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 712 | return f->set_rate_limit(f->opaque, new_rate); |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 717 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
| 718 | { |
| 719 | qemu_put_byte(f, v >> 8); |
| 720 | qemu_put_byte(f, v); |
| 721 | } |
| 722 | |
| 723 | void qemu_put_be32(QEMUFile *f, unsigned int v) |
| 724 | { |
| 725 | qemu_put_byte(f, v >> 24); |
| 726 | qemu_put_byte(f, v >> 16); |
| 727 | qemu_put_byte(f, v >> 8); |
| 728 | qemu_put_byte(f, v); |
| 729 | } |
| 730 | |
| 731 | void qemu_put_be64(QEMUFile *f, uint64_t v) |
| 732 | { |
| 733 | qemu_put_be32(f, v >> 32); |
| 734 | qemu_put_be32(f, v); |
| 735 | } |
| 736 | |
| 737 | unsigned int qemu_get_be16(QEMUFile *f) |
| 738 | { |
| 739 | unsigned int v; |
| 740 | v = qemu_get_byte(f) << 8; |
| 741 | v |= qemu_get_byte(f); |
| 742 | return v; |
| 743 | } |
| 744 | |
| 745 | unsigned int qemu_get_be32(QEMUFile *f) |
| 746 | { |
| 747 | unsigned int v; |
| 748 | v = qemu_get_byte(f) << 24; |
| 749 | v |= qemu_get_byte(f) << 16; |
| 750 | v |= qemu_get_byte(f) << 8; |
| 751 | v |= qemu_get_byte(f); |
| 752 | return v; |
| 753 | } |
| 754 | |
| 755 | uint64_t qemu_get_be64(QEMUFile *f) |
| 756 | { |
| 757 | uint64_t v; |
| 758 | v = (uint64_t)qemu_get_be32(f) << 32; |
| 759 | v |= qemu_get_be32(f); |
| 760 | return v; |
| 761 | } |
| 762 | |
Paolo Bonzini | 2ff68d0 | 2011-09-12 16:21:44 +0200 | [diff] [blame] | 763 | |
| 764 | /* timer */ |
| 765 | |
| 766 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 767 | { |
| 768 | uint64_t expire_time; |
| 769 | |
| 770 | expire_time = qemu_timer_expire_time_ns(ts); |
| 771 | qemu_put_be64(f, expire_time); |
| 772 | } |
| 773 | |
| 774 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 775 | { |
| 776 | uint64_t expire_time; |
| 777 | |
| 778 | expire_time = qemu_get_be64(f); |
| 779 | if (expire_time != -1) { |
| 780 | qemu_mod_timer_ns(ts, expire_time); |
| 781 | } else { |
| 782 | qemu_del_timer(ts); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | |
Gerd Hoffmann | cdae5cf | 2010-11-01 15:51:54 +0100 | [diff] [blame] | 787 | /* bool */ |
| 788 | |
| 789 | static int get_bool(QEMUFile *f, void *pv, size_t size) |
| 790 | { |
| 791 | bool *v = pv; |
| 792 | *v = qemu_get_byte(f); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static void put_bool(QEMUFile *f, void *pv, size_t size) |
| 797 | { |
| 798 | bool *v = pv; |
| 799 | qemu_put_byte(f, *v); |
| 800 | } |
| 801 | |
| 802 | const VMStateInfo vmstate_info_bool = { |
| 803 | .name = "bool", |
| 804 | .get = get_bool, |
| 805 | .put = put_bool, |
| 806 | }; |
| 807 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 808 | /* 8 bit int */ |
| 809 | |
| 810 | static int get_int8(QEMUFile *f, void *pv, size_t size) |
| 811 | { |
| 812 | int8_t *v = pv; |
| 813 | qemu_get_s8s(f, v); |
| 814 | return 0; |
| 815 | } |
| 816 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 817 | static void put_int8(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 818 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 819 | int8_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 820 | qemu_put_s8s(f, v); |
| 821 | } |
| 822 | |
| 823 | const VMStateInfo vmstate_info_int8 = { |
| 824 | .name = "int8", |
| 825 | .get = get_int8, |
| 826 | .put = put_int8, |
| 827 | }; |
| 828 | |
| 829 | /* 16 bit int */ |
| 830 | |
| 831 | static int get_int16(QEMUFile *f, void *pv, size_t size) |
| 832 | { |
| 833 | int16_t *v = pv; |
| 834 | qemu_get_sbe16s(f, v); |
| 835 | return 0; |
| 836 | } |
| 837 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 838 | static void put_int16(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 839 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 840 | int16_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 841 | qemu_put_sbe16s(f, v); |
| 842 | } |
| 843 | |
| 844 | const VMStateInfo vmstate_info_int16 = { |
| 845 | .name = "int16", |
| 846 | .get = get_int16, |
| 847 | .put = put_int16, |
| 848 | }; |
| 849 | |
| 850 | /* 32 bit int */ |
| 851 | |
| 852 | static int get_int32(QEMUFile *f, void *pv, size_t size) |
| 853 | { |
| 854 | int32_t *v = pv; |
| 855 | qemu_get_sbe32s(f, v); |
| 856 | return 0; |
| 857 | } |
| 858 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 859 | static void put_int32(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 860 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 861 | int32_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 862 | qemu_put_sbe32s(f, v); |
| 863 | } |
| 864 | |
| 865 | const VMStateInfo vmstate_info_int32 = { |
| 866 | .name = "int32", |
| 867 | .get = get_int32, |
| 868 | .put = put_int32, |
| 869 | }; |
| 870 | |
Juan Quintela | 8250166 | 2009-08-20 19:42:32 +0200 | [diff] [blame] | 871 | /* 32 bit int. See that the received value is the same than the one |
| 872 | in the field */ |
| 873 | |
| 874 | static int get_int32_equal(QEMUFile *f, void *pv, size_t size) |
| 875 | { |
| 876 | int32_t *v = pv; |
| 877 | int32_t v2; |
| 878 | qemu_get_sbe32s(f, &v2); |
| 879 | |
| 880 | if (*v == v2) |
| 881 | return 0; |
| 882 | return -EINVAL; |
| 883 | } |
| 884 | |
| 885 | const VMStateInfo vmstate_info_int32_equal = { |
| 886 | .name = "int32 equal", |
| 887 | .get = get_int32_equal, |
| 888 | .put = put_int32, |
| 889 | }; |
| 890 | |
Juan Quintela | 0a031e0 | 2009-08-20 19:42:37 +0200 | [diff] [blame] | 891 | /* 32 bit int. See that the received value is the less or the same |
| 892 | than the one in the field */ |
| 893 | |
| 894 | static int get_int32_le(QEMUFile *f, void *pv, size_t size) |
| 895 | { |
| 896 | int32_t *old = pv; |
| 897 | int32_t new; |
| 898 | qemu_get_sbe32s(f, &new); |
| 899 | |
| 900 | if (*old <= new) |
| 901 | return 0; |
| 902 | return -EINVAL; |
| 903 | } |
| 904 | |
| 905 | const VMStateInfo vmstate_info_int32_le = { |
| 906 | .name = "int32 equal", |
| 907 | .get = get_int32_le, |
| 908 | .put = put_int32, |
| 909 | }; |
| 910 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 911 | /* 64 bit int */ |
| 912 | |
| 913 | static int get_int64(QEMUFile *f, void *pv, size_t size) |
| 914 | { |
| 915 | int64_t *v = pv; |
| 916 | qemu_get_sbe64s(f, v); |
| 917 | return 0; |
| 918 | } |
| 919 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 920 | static void put_int64(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 921 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 922 | int64_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 923 | qemu_put_sbe64s(f, v); |
| 924 | } |
| 925 | |
| 926 | const VMStateInfo vmstate_info_int64 = { |
| 927 | .name = "int64", |
| 928 | .get = get_int64, |
| 929 | .put = put_int64, |
| 930 | }; |
| 931 | |
| 932 | /* 8 bit unsigned int */ |
| 933 | |
| 934 | static int get_uint8(QEMUFile *f, void *pv, size_t size) |
| 935 | { |
| 936 | uint8_t *v = pv; |
| 937 | qemu_get_8s(f, v); |
| 938 | return 0; |
| 939 | } |
| 940 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 941 | static void put_uint8(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 942 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 943 | uint8_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 944 | qemu_put_8s(f, v); |
| 945 | } |
| 946 | |
| 947 | const VMStateInfo vmstate_info_uint8 = { |
| 948 | .name = "uint8", |
| 949 | .get = get_uint8, |
| 950 | .put = put_uint8, |
| 951 | }; |
| 952 | |
| 953 | /* 16 bit unsigned int */ |
| 954 | |
| 955 | static int get_uint16(QEMUFile *f, void *pv, size_t size) |
| 956 | { |
| 957 | uint16_t *v = pv; |
| 958 | qemu_get_be16s(f, v); |
| 959 | return 0; |
| 960 | } |
| 961 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 962 | static void put_uint16(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 963 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 964 | uint16_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 965 | qemu_put_be16s(f, v); |
| 966 | } |
| 967 | |
| 968 | const VMStateInfo vmstate_info_uint16 = { |
| 969 | .name = "uint16", |
| 970 | .get = get_uint16, |
| 971 | .put = put_uint16, |
| 972 | }; |
| 973 | |
| 974 | /* 32 bit unsigned int */ |
| 975 | |
| 976 | static int get_uint32(QEMUFile *f, void *pv, size_t size) |
| 977 | { |
| 978 | uint32_t *v = pv; |
| 979 | qemu_get_be32s(f, v); |
| 980 | return 0; |
| 981 | } |
| 982 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 983 | static void put_uint32(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 984 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 985 | uint32_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 986 | qemu_put_be32s(f, v); |
| 987 | } |
| 988 | |
| 989 | const VMStateInfo vmstate_info_uint32 = { |
| 990 | .name = "uint32", |
| 991 | .get = get_uint32, |
| 992 | .put = put_uint32, |
| 993 | }; |
| 994 | |
Juan Quintela | 9122a8f | 2011-03-10 12:33:48 +0100 | [diff] [blame] | 995 | /* 32 bit uint. See that the received value is the same than the one |
| 996 | in the field */ |
| 997 | |
| 998 | static int get_uint32_equal(QEMUFile *f, void *pv, size_t size) |
| 999 | { |
| 1000 | uint32_t *v = pv; |
| 1001 | uint32_t v2; |
| 1002 | qemu_get_be32s(f, &v2); |
| 1003 | |
| 1004 | if (*v == v2) { |
| 1005 | return 0; |
| 1006 | } |
| 1007 | return -EINVAL; |
| 1008 | } |
| 1009 | |
| 1010 | const VMStateInfo vmstate_info_uint32_equal = { |
| 1011 | .name = "uint32 equal", |
| 1012 | .get = get_uint32_equal, |
| 1013 | .put = put_uint32, |
| 1014 | }; |
| 1015 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1016 | /* 64 bit unsigned int */ |
| 1017 | |
| 1018 | static int get_uint64(QEMUFile *f, void *pv, size_t size) |
| 1019 | { |
| 1020 | uint64_t *v = pv; |
| 1021 | qemu_get_be64s(f, v); |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1025 | static void put_uint64(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1026 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1027 | uint64_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1028 | qemu_put_be64s(f, v); |
| 1029 | } |
| 1030 | |
| 1031 | const VMStateInfo vmstate_info_uint64 = { |
| 1032 | .name = "uint64", |
| 1033 | .get = get_uint64, |
| 1034 | .put = put_uint64, |
| 1035 | }; |
| 1036 | |
Juan Quintela | 80cd83e | 2009-09-10 03:04:36 +0200 | [diff] [blame] | 1037 | /* 8 bit int. See that the received value is the same than the one |
| 1038 | in the field */ |
| 1039 | |
| 1040 | static int get_uint8_equal(QEMUFile *f, void *pv, size_t size) |
| 1041 | { |
| 1042 | uint8_t *v = pv; |
| 1043 | uint8_t v2; |
| 1044 | qemu_get_8s(f, &v2); |
| 1045 | |
| 1046 | if (*v == v2) |
| 1047 | return 0; |
| 1048 | return -EINVAL; |
| 1049 | } |
| 1050 | |
| 1051 | const VMStateInfo vmstate_info_uint8_equal = { |
Juan Quintela | aa1cce6 | 2009-10-15 19:16:06 +0200 | [diff] [blame] | 1052 | .name = "uint8 equal", |
Juan Quintela | 80cd83e | 2009-09-10 03:04:36 +0200 | [diff] [blame] | 1053 | .get = get_uint8_equal, |
| 1054 | .put = put_uint8, |
| 1055 | }; |
| 1056 | |
Juan Quintela | dc3b83a | 2009-10-15 23:16:13 +0200 | [diff] [blame] | 1057 | /* 16 bit unsigned int int. See that the received value is the same than the one |
| 1058 | in the field */ |
| 1059 | |
| 1060 | static int get_uint16_equal(QEMUFile *f, void *pv, size_t size) |
| 1061 | { |
| 1062 | uint16_t *v = pv; |
| 1063 | uint16_t v2; |
| 1064 | qemu_get_be16s(f, &v2); |
| 1065 | |
| 1066 | if (*v == v2) |
| 1067 | return 0; |
| 1068 | return -EINVAL; |
| 1069 | } |
| 1070 | |
| 1071 | const VMStateInfo vmstate_info_uint16_equal = { |
| 1072 | .name = "uint16 equal", |
| 1073 | .get = get_uint16_equal, |
| 1074 | .put = put_uint16, |
| 1075 | }; |
| 1076 | |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1077 | /* timers */ |
| 1078 | |
| 1079 | static int get_timer(QEMUFile *f, void *pv, size_t size) |
| 1080 | { |
| 1081 | QEMUTimer *v = pv; |
| 1082 | qemu_get_timer(f, v); |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1086 | static void put_timer(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1087 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1088 | QEMUTimer *v = pv; |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1089 | qemu_put_timer(f, v); |
| 1090 | } |
| 1091 | |
| 1092 | const VMStateInfo vmstate_info_timer = { |
| 1093 | .name = "timer", |
| 1094 | .get = get_timer, |
| 1095 | .put = put_timer, |
| 1096 | }; |
| 1097 | |
Juan Quintela | 6f67c50 | 2009-08-20 19:42:35 +0200 | [diff] [blame] | 1098 | /* uint8_t buffers */ |
| 1099 | |
| 1100 | static int get_buffer(QEMUFile *f, void *pv, size_t size) |
| 1101 | { |
| 1102 | uint8_t *v = pv; |
| 1103 | qemu_get_buffer(f, v, size); |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1107 | static void put_buffer(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 6f67c50 | 2009-08-20 19:42:35 +0200 | [diff] [blame] | 1108 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1109 | uint8_t *v = pv; |
Juan Quintela | 6f67c50 | 2009-08-20 19:42:35 +0200 | [diff] [blame] | 1110 | qemu_put_buffer(f, v, size); |
| 1111 | } |
| 1112 | |
| 1113 | const VMStateInfo vmstate_info_buffer = { |
| 1114 | .name = "buffer", |
| 1115 | .get = get_buffer, |
| 1116 | .put = put_buffer, |
| 1117 | }; |
| 1118 | |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 1119 | /* unused buffers: space that was used for some fields that are |
Stefan Weil | 61cc870 | 2011-04-13 22:45:22 +0200 | [diff] [blame] | 1120 | not useful anymore */ |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 1121 | |
| 1122 | static int get_unused_buffer(QEMUFile *f, void *pv, size_t size) |
| 1123 | { |
Jan Kiszka | 21174c3 | 2009-12-02 12:36:35 +0100 | [diff] [blame] | 1124 | uint8_t buf[1024]; |
| 1125 | int block_len; |
| 1126 | |
| 1127 | while (size > 0) { |
| 1128 | block_len = MIN(sizeof(buf), size); |
| 1129 | size -= block_len; |
| 1130 | qemu_get_buffer(f, buf, block_len); |
| 1131 | } |
| 1132 | return 0; |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static void put_unused_buffer(QEMUFile *f, void *pv, size_t size) |
| 1136 | { |
Jan Kiszka | 21174c3 | 2009-12-02 12:36:35 +0100 | [diff] [blame] | 1137 | static const uint8_t buf[1024]; |
| 1138 | int block_len; |
| 1139 | |
| 1140 | while (size > 0) { |
| 1141 | block_len = MIN(sizeof(buf), size); |
| 1142 | size -= block_len; |
| 1143 | qemu_put_buffer(f, buf, block_len); |
| 1144 | } |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | const VMStateInfo vmstate_info_unused_buffer = { |
| 1148 | .name = "unused_buffer", |
| 1149 | .get = get_unused_buffer, |
| 1150 | .put = put_unused_buffer, |
| 1151 | }; |
| 1152 | |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1153 | typedef struct CompatEntry { |
| 1154 | char idstr[256]; |
| 1155 | int instance_id; |
| 1156 | } CompatEntry; |
| 1157 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1158 | typedef struct SaveStateEntry { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1159 | QTAILQ_ENTRY(SaveStateEntry) entry; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1160 | char idstr[256]; |
| 1161 | int instance_id; |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1162 | int alias_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1163 | int version_id; |
| 1164 | int section_id; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1165 | SaveSetParamsHandler *set_params; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1166 | SaveLiveStateHandler *save_live_state; |
| 1167 | SaveStateHandler *save_state; |
| 1168 | LoadStateHandler *load_state; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1169 | const VMStateDescription *vmsd; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1170 | void *opaque; |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1171 | CompatEntry *compat; |
Cam Macdonell | 2431296 | 2010-07-26 18:11:00 -0600 | [diff] [blame] | 1172 | int no_migrate; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1173 | } SaveStateEntry; |
| 1174 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1175 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1176 | static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers = |
| 1177 | QTAILQ_HEAD_INITIALIZER(savevm_handlers); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1178 | static int global_section_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1179 | |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1180 | static int calculate_new_instance_id(const char *idstr) |
| 1181 | { |
| 1182 | SaveStateEntry *se; |
| 1183 | int instance_id = 0; |
| 1184 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1185 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1186 | if (strcmp(idstr, se->idstr) == 0 |
| 1187 | && instance_id <= se->instance_id) { |
| 1188 | instance_id = se->instance_id + 1; |
| 1189 | } |
| 1190 | } |
| 1191 | return instance_id; |
| 1192 | } |
| 1193 | |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1194 | static int calculate_compat_instance_id(const char *idstr) |
| 1195 | { |
| 1196 | SaveStateEntry *se; |
| 1197 | int instance_id = 0; |
| 1198 | |
| 1199 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 1200 | if (!se->compat) |
| 1201 | continue; |
| 1202 | |
| 1203 | if (strcmp(idstr, se->compat->idstr) == 0 |
| 1204 | && instance_id <= se->compat->instance_id) { |
| 1205 | instance_id = se->compat->instance_id + 1; |
| 1206 | } |
| 1207 | } |
| 1208 | return instance_id; |
| 1209 | } |
| 1210 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1211 | /* TODO: Individual devices generally have very little idea about the rest |
| 1212 | of the system, so instance_id should be removed/replaced. |
| 1213 | Meanwhile pass -1 as instance_id if you do not already have a clearly |
| 1214 | distinguishing id for all instances of your device class. */ |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1215 | int register_savevm_live(DeviceState *dev, |
| 1216 | const char *idstr, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1217 | int instance_id, |
| 1218 | int version_id, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1219 | SaveSetParamsHandler *set_params, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1220 | SaveLiveStateHandler *save_live_state, |
| 1221 | SaveStateHandler *save_state, |
| 1222 | LoadStateHandler *load_state, |
| 1223 | void *opaque) |
| 1224 | { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1225 | SaveStateEntry *se; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1226 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1227 | se = g_malloc0(sizeof(SaveStateEntry)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1228 | se->version_id = version_id; |
| 1229 | se->section_id = global_section_id++; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1230 | se->set_params = set_params; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1231 | se->save_live_state = save_live_state; |
| 1232 | se->save_state = save_state; |
| 1233 | se->load_state = load_state; |
| 1234 | se->opaque = opaque; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1235 | se->vmsd = NULL; |
Cam Macdonell | 2431296 | 2010-07-26 18:11:00 -0600 | [diff] [blame] | 1236 | se->no_migrate = 0; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1237 | |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1238 | if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) { |
| 1239 | char *id = dev->parent_bus->info->get_dev_path(dev); |
| 1240 | if (id) { |
| 1241 | pstrcpy(se->idstr, sizeof(se->idstr), id); |
| 1242 | pstrcat(se->idstr, sizeof(se->idstr), "/"); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1243 | g_free(id); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1244 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1245 | se->compat = g_malloc0(sizeof(CompatEntry)); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1246 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr); |
| 1247 | se->compat->instance_id = instance_id == -1 ? |
| 1248 | calculate_compat_instance_id(idstr) : instance_id; |
| 1249 | instance_id = -1; |
| 1250 | } |
| 1251 | } |
| 1252 | pstrcat(se->idstr, sizeof(se->idstr), idstr); |
| 1253 | |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1254 | if (instance_id == -1) { |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1255 | se->instance_id = calculate_new_instance_id(se->idstr); |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1256 | } else { |
| 1257 | se->instance_id = instance_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1258 | } |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1259 | assert(!se->compat || se->instance_id == 0); |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1260 | /* add at the end of list */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1261 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1262 | return 0; |
| 1263 | } |
| 1264 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1265 | int register_savevm(DeviceState *dev, |
| 1266 | const char *idstr, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1267 | int instance_id, |
| 1268 | int version_id, |
| 1269 | SaveStateHandler *save_state, |
| 1270 | LoadStateHandler *load_state, |
| 1271 | void *opaque) |
| 1272 | { |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1273 | return register_savevm_live(dev, idstr, instance_id, version_id, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1274 | NULL, NULL, save_state, load_state, opaque); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1277 | void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque) |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1278 | { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1279 | SaveStateEntry *se, *new_se; |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1280 | char id[256] = ""; |
| 1281 | |
| 1282 | if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) { |
| 1283 | char *path = dev->parent_bus->info->get_dev_path(dev); |
| 1284 | if (path) { |
| 1285 | pstrcpy(id, sizeof(id), path); |
| 1286 | pstrcat(id, sizeof(id), "/"); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1287 | g_free(path); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | pstrcat(id, sizeof(id), idstr); |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1291 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1292 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1293 | if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1294 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
Alex Williamson | 69e58af | 2010-07-21 08:35:31 -0600 | [diff] [blame] | 1295 | if (se->compat) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1296 | g_free(se->compat); |
Alex Williamson | 69e58af | 2010-07-21 08:35:31 -0600 | [diff] [blame] | 1297 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1298 | g_free(se); |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1299 | } |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1303 | int vmstate_register_with_alias_id(DeviceState *dev, int instance_id, |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1304 | const VMStateDescription *vmsd, |
| 1305 | void *opaque, int alias_id, |
| 1306 | int required_for_version) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1307 | { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1308 | SaveStateEntry *se; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1309 | |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1310 | /* If this triggers, alias support can be dropped for the vmsd. */ |
| 1311 | assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); |
| 1312 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1313 | se = g_malloc0(sizeof(SaveStateEntry)); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1314 | se->version_id = vmsd->version_id; |
| 1315 | se->section_id = global_section_id++; |
| 1316 | se->save_live_state = NULL; |
| 1317 | se->save_state = NULL; |
| 1318 | se->load_state = NULL; |
| 1319 | se->opaque = opaque; |
| 1320 | se->vmsd = vmsd; |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1321 | se->alias_id = alias_id; |
Gerd Hoffmann | 2837c8e | 2011-07-08 10:44:35 +0200 | [diff] [blame] | 1322 | se->no_migrate = vmsd->unmigratable; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1323 | |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1324 | if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) { |
| 1325 | char *id = dev->parent_bus->info->get_dev_path(dev); |
| 1326 | if (id) { |
| 1327 | pstrcpy(se->idstr, sizeof(se->idstr), id); |
| 1328 | pstrcat(se->idstr, sizeof(se->idstr), "/"); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1329 | g_free(id); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1330 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1331 | se->compat = g_malloc0(sizeof(CompatEntry)); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1332 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); |
| 1333 | se->compat->instance_id = instance_id == -1 ? |
| 1334 | calculate_compat_instance_id(vmsd->name) : instance_id; |
| 1335 | instance_id = -1; |
| 1336 | } |
| 1337 | } |
| 1338 | pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); |
| 1339 | |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1340 | if (instance_id == -1) { |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1341 | se->instance_id = calculate_new_instance_id(se->idstr); |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1342 | } else { |
| 1343 | se->instance_id = instance_id; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1344 | } |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1345 | assert(!se->compat || se->instance_id == 0); |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1346 | /* add at the end of list */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1347 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1348 | return 0; |
| 1349 | } |
| 1350 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1351 | int vmstate_register(DeviceState *dev, int instance_id, |
| 1352 | const VMStateDescription *vmsd, void *opaque) |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1353 | { |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1354 | return vmstate_register_with_alias_id(dev, instance_id, vmsd, |
| 1355 | opaque, -1, 0); |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1356 | } |
| 1357 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1358 | void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd, |
| 1359 | void *opaque) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1360 | { |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1361 | SaveStateEntry *se, *new_se; |
| 1362 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1363 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1364 | if (se->vmsd == vmsd && se->opaque == opaque) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1365 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
Alex Williamson | 69e58af | 2010-07-21 08:35:31 -0600 | [diff] [blame] | 1366 | if (se->compat) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1367 | g_free(se->compat); |
Alex Williamson | 69e58af | 2010-07-21 08:35:31 -0600 | [diff] [blame] | 1368 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1369 | g_free(se); |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1370 | } |
| 1371 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1374 | static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, |
| 1375 | void *opaque); |
| 1376 | static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd, |
| 1377 | void *opaque); |
| 1378 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1379 | int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd, |
| 1380 | void *opaque, int version_id) |
| 1381 | { |
| 1382 | VMStateField *field = vmsd->fields; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1383 | int ret; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1384 | |
| 1385 | if (version_id > vmsd->version_id) { |
| 1386 | return -EINVAL; |
| 1387 | } |
| 1388 | if (version_id < vmsd->minimum_version_id_old) { |
| 1389 | return -EINVAL; |
| 1390 | } |
| 1391 | if (version_id < vmsd->minimum_version_id) { |
| 1392 | return vmsd->load_state_old(f, opaque, version_id); |
| 1393 | } |
Juan Quintela | fd4d52d | 2009-09-10 03:04:31 +0200 | [diff] [blame] | 1394 | if (vmsd->pre_load) { |
| 1395 | int ret = vmsd->pre_load(opaque); |
| 1396 | if (ret) |
| 1397 | return ret; |
| 1398 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1399 | while(field->name) { |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1400 | if ((field->field_exists && |
| 1401 | field->field_exists(opaque, version_id)) || |
| 1402 | (!field->field_exists && |
| 1403 | field->version_id <= version_id)) { |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1404 | void *base_addr = opaque + field->offset; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1405 | int i, n_elems = 1; |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1406 | int size = field->size; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1407 | |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1408 | if (field->flags & VMS_VBUFFER) { |
| 1409 | size = *(int32_t *)(opaque+field->size_offset); |
Juan Quintela | 33599e2 | 2009-12-02 12:36:43 +0100 | [diff] [blame] | 1410 | if (field->flags & VMS_MULTIPLY) { |
| 1411 | size *= field->size; |
| 1412 | } |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1413 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1414 | if (field->flags & VMS_ARRAY) { |
| 1415 | n_elems = field->num; |
Juan Quintela | d669828 | 2009-10-16 11:27:17 +0200 | [diff] [blame] | 1416 | } else if (field->flags & VMS_VARRAY_INT32) { |
| 1417 | n_elems = *(int32_t *)(opaque+field->num_offset); |
Juan Quintela | a624b08 | 2011-03-10 12:33:50 +0100 | [diff] [blame] | 1418 | } else if (field->flags & VMS_VARRAY_UINT32) { |
| 1419 | n_elems = *(uint32_t *)(opaque+field->num_offset); |
Juan Quintela | bdb4941 | 2009-10-16 11:35:18 +0200 | [diff] [blame] | 1420 | } else if (field->flags & VMS_VARRAY_UINT16) { |
| 1421 | n_elems = *(uint16_t *)(opaque+field->num_offset); |
Juan Quintela | 82fa39b | 2011-03-10 12:33:49 +0100 | [diff] [blame] | 1422 | } else if (field->flags & VMS_VARRAY_UINT8) { |
| 1423 | n_elems = *(uint8_t *)(opaque+field->num_offset); |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1424 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1425 | if (field->flags & VMS_POINTER) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1426 | base_addr = *(void **)base_addr + field->start; |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1427 | } |
| 1428 | for (i = 0; i < n_elems; i++) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1429 | void *addr = base_addr + size * i; |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1430 | |
Juan Quintela | 19df438 | 2009-09-29 22:48:41 +0200 | [diff] [blame] | 1431 | if (field->flags & VMS_ARRAY_OF_POINTER) { |
| 1432 | addr = *(void **)addr; |
| 1433 | } |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1434 | if (field->flags & VMS_STRUCT) { |
Juan Quintela | fa3aad2 | 2009-08-28 15:28:25 +0200 | [diff] [blame] | 1435 | ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id); |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1436 | } else { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1437 | ret = field->info->get(f, addr, size); |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1438 | |
| 1439 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1440 | if (ret < 0) { |
| 1441 | return ret; |
| 1442 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | field++; |
| 1446 | } |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1447 | ret = vmstate_subsection_load(f, vmsd, opaque); |
| 1448 | if (ret != 0) { |
| 1449 | return ret; |
| 1450 | } |
Juan Quintela | 752ff2f | 2009-09-10 03:04:30 +0200 | [diff] [blame] | 1451 | if (vmsd->post_load) { |
Juan Quintela | e59fb37 | 2009-09-29 22:48:21 +0200 | [diff] [blame] | 1452 | return vmsd->post_load(opaque, version_id); |
Juan Quintela | 752ff2f | 2009-09-10 03:04:30 +0200 | [diff] [blame] | 1453 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1458 | void *opaque) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1459 | { |
| 1460 | VMStateField *field = vmsd->fields; |
| 1461 | |
Juan Quintela | 8fb0791 | 2009-09-10 03:04:32 +0200 | [diff] [blame] | 1462 | if (vmsd->pre_save) { |
| 1463 | vmsd->pre_save(opaque); |
| 1464 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1465 | while(field->name) { |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1466 | if (!field->field_exists || |
| 1467 | field->field_exists(opaque, vmsd->version_id)) { |
| 1468 | void *base_addr = opaque + field->offset; |
| 1469 | int i, n_elems = 1; |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1470 | int size = field->size; |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1471 | |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1472 | if (field->flags & VMS_VBUFFER) { |
| 1473 | size = *(int32_t *)(opaque+field->size_offset); |
Juan Quintela | 33599e2 | 2009-12-02 12:36:43 +0100 | [diff] [blame] | 1474 | if (field->flags & VMS_MULTIPLY) { |
| 1475 | size *= field->size; |
| 1476 | } |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1477 | } |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1478 | if (field->flags & VMS_ARRAY) { |
| 1479 | n_elems = field->num; |
Juan Quintela | d669828 | 2009-10-16 11:27:17 +0200 | [diff] [blame] | 1480 | } else if (field->flags & VMS_VARRAY_INT32) { |
| 1481 | n_elems = *(int32_t *)(opaque+field->num_offset); |
Juan Quintela | bdb4941 | 2009-10-16 11:35:18 +0200 | [diff] [blame] | 1482 | } else if (field->flags & VMS_VARRAY_UINT16) { |
| 1483 | n_elems = *(uint16_t *)(opaque+field->num_offset); |
Juan Quintela | b784421 | 2011-03-15 15:53:25 +0100 | [diff] [blame] | 1484 | } else if (field->flags & VMS_VARRAY_UINT8) { |
| 1485 | n_elems = *(uint8_t *)(opaque+field->num_offset); |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1486 | } |
| 1487 | if (field->flags & VMS_POINTER) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1488 | base_addr = *(void **)base_addr + field->start; |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1489 | } |
| 1490 | for (i = 0; i < n_elems; i++) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1491 | void *addr = base_addr + size * i; |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1492 | |
Juan Quintela | 8595387 | 2009-12-02 12:36:37 +0100 | [diff] [blame] | 1493 | if (field->flags & VMS_ARRAY_OF_POINTER) { |
| 1494 | addr = *(void **)addr; |
| 1495 | } |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1496 | if (field->flags & VMS_STRUCT) { |
| 1497 | vmstate_save_state(f, field->vmsd, addr); |
| 1498 | } else { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1499 | field->info->put(f, addr, size); |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1500 | } |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1501 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1502 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1503 | field++; |
| 1504 | } |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1505 | vmstate_subsection_save(f, vmsd, opaque); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1506 | } |
| 1507 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1508 | static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id) |
| 1509 | { |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1510 | if (!se->vmsd) { /* Old style */ |
| 1511 | return se->load_state(f, se->opaque, version_id); |
| 1512 | } |
| 1513 | return vmstate_load_state(f, se->vmsd, se->opaque, version_id); |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1514 | } |
| 1515 | |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1516 | static void vmstate_save(QEMUFile *f, SaveStateEntry *se) |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1517 | { |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1518 | if (!se->vmsd) { /* Old style */ |
| 1519 | se->save_state(f, se->opaque); |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1520 | return; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1521 | } |
| 1522 | vmstate_save_state(f,se->vmsd, se->opaque); |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1523 | } |
| 1524 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1525 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
| 1526 | #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002 |
| 1527 | #define QEMU_VM_FILE_VERSION 0x00000003 |
| 1528 | |
| 1529 | #define QEMU_VM_EOF 0x00 |
| 1530 | #define QEMU_VM_SECTION_START 0x01 |
| 1531 | #define QEMU_VM_SECTION_PART 0x02 |
| 1532 | #define QEMU_VM_SECTION_END 0x03 |
| 1533 | #define QEMU_VM_SECTION_FULL 0x04 |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1534 | #define QEMU_VM_SUBSECTION 0x05 |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1535 | |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1536 | bool qemu_savevm_state_blocked(Monitor *mon) |
| 1537 | { |
| 1538 | SaveStateEntry *se; |
| 1539 | |
| 1540 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 1541 | if (se->no_migrate) { |
| 1542 | monitor_printf(mon, "state blocked by non-migratable device '%s'\n", |
| 1543 | se->idstr); |
| 1544 | return true; |
| 1545 | } |
| 1546 | } |
| 1547 | return false; |
| 1548 | } |
| 1549 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1550 | int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable, |
| 1551 | int shared) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1552 | { |
| 1553 | SaveStateEntry *se; |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1554 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1555 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1556 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 1557 | if(se->set_params == NULL) { |
| 1558 | continue; |
| 1559 | } |
| 1560 | se->set_params(blk_enable, shared, se->opaque); |
| 1561 | } |
| 1562 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1563 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 1564 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
| 1565 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1566 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1567 | int len; |
| 1568 | |
| 1569 | if (se->save_live_state == NULL) |
| 1570 | continue; |
| 1571 | |
| 1572 | /* Section type */ |
| 1573 | qemu_put_byte(f, QEMU_VM_SECTION_START); |
| 1574 | qemu_put_be32(f, se->section_id); |
| 1575 | |
| 1576 | /* ID string */ |
| 1577 | len = strlen(se->idstr); |
| 1578 | qemu_put_byte(f, len); |
| 1579 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
| 1580 | |
| 1581 | qemu_put_be32(f, se->instance_id); |
| 1582 | qemu_put_be32(f, se->version_id); |
| 1583 | |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1584 | ret = se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque); |
| 1585 | if (ret < 0) { |
| 1586 | qemu_savevm_state_cancel(mon, f); |
| 1587 | return ret; |
| 1588 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1589 | } |
Juan Quintela | 624b9cc | 2011-10-05 01:02:52 +0200 | [diff] [blame] | 1590 | ret = qemu_file_get_error(f); |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1591 | if (ret != 0) { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1592 | qemu_savevm_state_cancel(mon, f); |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1593 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1594 | |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1595 | return ret; |
| 1596 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1599 | /* |
Dong Xu Wang | 07f3507 | 2011-11-22 18:06:26 +0800 | [diff] [blame] | 1600 | * this function has three return values: |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1601 | * negative: there was one error, and we have -errno. |
| 1602 | * 0 : We haven't finished, caller have to go again |
| 1603 | * 1 : We have finished, we can go to complete phase |
| 1604 | */ |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1605 | int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1606 | { |
| 1607 | SaveStateEntry *se; |
| 1608 | int ret = 1; |
| 1609 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1610 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1611 | if (se->save_live_state == NULL) |
| 1612 | continue; |
| 1613 | |
| 1614 | /* Section type */ |
| 1615 | qemu_put_byte(f, QEMU_VM_SECTION_PART); |
| 1616 | qemu_put_be32(f, se->section_id); |
| 1617 | |
Jan Kiszka | 90697be | 2009-12-01 15:19:55 +0100 | [diff] [blame] | 1618 | ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque); |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1619 | if (ret <= 0) { |
Jan Kiszka | 90697be | 2009-12-01 15:19:55 +0100 | [diff] [blame] | 1620 | /* Do not proceed to the next vmstate before this one reported |
| 1621 | completion of the current stage. This serializes the migration |
| 1622 | and reduces the probability that a faster changing state is |
| 1623 | synchronized over and over again. */ |
| 1624 | break; |
| 1625 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1626 | } |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1627 | if (ret != 0) { |
| 1628 | return ret; |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1629 | } |
Juan Quintela | 624b9cc | 2011-10-05 01:02:52 +0200 | [diff] [blame] | 1630 | ret = qemu_file_get_error(f); |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1631 | if (ret != 0) { |
| 1632 | qemu_savevm_state_cancel(mon, f); |
| 1633 | } |
| 1634 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1637 | int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1638 | { |
| 1639 | SaveStateEntry *se; |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1640 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1641 | |
Jan Kiszka | ea375f9 | 2010-03-01 19:10:30 +0100 | [diff] [blame] | 1642 | cpu_synchronize_all_states(); |
| 1643 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1644 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1645 | if (se->save_live_state == NULL) |
| 1646 | continue; |
| 1647 | |
| 1648 | /* Section type */ |
| 1649 | qemu_put_byte(f, QEMU_VM_SECTION_END); |
| 1650 | qemu_put_be32(f, se->section_id); |
| 1651 | |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1652 | ret = se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque); |
| 1653 | if (ret < 0) { |
| 1654 | return ret; |
| 1655 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1658 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1659 | int len; |
| 1660 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1661 | if (se->save_state == NULL && se->vmsd == NULL) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1662 | continue; |
| 1663 | |
| 1664 | /* Section type */ |
| 1665 | qemu_put_byte(f, QEMU_VM_SECTION_FULL); |
| 1666 | qemu_put_be32(f, se->section_id); |
| 1667 | |
| 1668 | /* ID string */ |
| 1669 | len = strlen(se->idstr); |
| 1670 | qemu_put_byte(f, len); |
| 1671 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
| 1672 | |
| 1673 | qemu_put_be32(f, se->instance_id); |
| 1674 | qemu_put_be32(f, se->version_id); |
| 1675 | |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1676 | vmstate_save(f, se); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | qemu_put_byte(f, QEMU_VM_EOF); |
| 1680 | |
Juan Quintela | 624b9cc | 2011-10-05 01:02:52 +0200 | [diff] [blame] | 1681 | return qemu_file_get_error(f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1684 | void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f) |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1685 | { |
| 1686 | SaveStateEntry *se; |
| 1687 | |
| 1688 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 1689 | if (se->save_live_state) { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1690 | se->save_live_state(mon, f, -1, se->opaque); |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1695 | static int qemu_savevm_state(Monitor *mon, QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1696 | { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1697 | int ret; |
| 1698 | |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1699 | if (qemu_savevm_state_blocked(mon)) { |
| 1700 | ret = -EINVAL; |
| 1701 | goto out; |
| 1702 | } |
| 1703 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1704 | ret = qemu_savevm_state_begin(mon, f, 0, 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1705 | if (ret < 0) |
| 1706 | goto out; |
| 1707 | |
| 1708 | do { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1709 | ret = qemu_savevm_state_iterate(mon, f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1710 | if (ret < 0) |
| 1711 | goto out; |
| 1712 | } while (ret == 0); |
| 1713 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 1714 | ret = qemu_savevm_state_complete(mon, f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1715 | |
| 1716 | out: |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1717 | if (ret == 0) { |
Juan Quintela | 624b9cc | 2011-10-05 01:02:52 +0200 | [diff] [blame] | 1718 | ret = qemu_file_get_error(f); |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1719 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1720 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1721 | return ret; |
| 1722 | } |
| 1723 | |
| 1724 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
| 1725 | { |
| 1726 | SaveStateEntry *se; |
| 1727 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1728 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1729 | if (!strcmp(se->idstr, idstr) && |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1730 | (instance_id == se->instance_id || |
| 1731 | instance_id == se->alias_id)) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1732 | return se; |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1733 | /* Migrating from an older version? */ |
| 1734 | if (strstr(se->idstr, idstr) && se->compat) { |
| 1735 | if (!strcmp(se->compat->idstr, idstr) && |
| 1736 | (instance_id == se->compat->instance_id || |
| 1737 | instance_id == se->alias_id)) |
| 1738 | return se; |
| 1739 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1740 | } |
| 1741 | return NULL; |
| 1742 | } |
| 1743 | |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1744 | static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr) |
| 1745 | { |
| 1746 | while(sub && sub->needed) { |
| 1747 | if (strcmp(idstr, sub->vmsd->name) == 0) { |
| 1748 | return sub->vmsd; |
| 1749 | } |
| 1750 | sub++; |
| 1751 | } |
| 1752 | return NULL; |
| 1753 | } |
| 1754 | |
| 1755 | static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd, |
| 1756 | void *opaque) |
| 1757 | { |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 1758 | while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) { |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1759 | char idstr[256]; |
| 1760 | int ret; |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 1761 | uint8_t version_id, len, size; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1762 | const VMStateDescription *sub_vmsd; |
| 1763 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 1764 | len = qemu_peek_byte(f, 1); |
| 1765 | if (len < strlen(vmsd->name) + 1) { |
| 1766 | /* subsection name has be be "section_name/a" */ |
| 1767 | return 0; |
| 1768 | } |
| 1769 | size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2); |
| 1770 | if (size != len) { |
| 1771 | return 0; |
| 1772 | } |
| 1773 | idstr[size] = 0; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1774 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 1775 | if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) { |
| 1776 | /* it don't have a valid subsection name */ |
| 1777 | return 0; |
| 1778 | } |
Juan Quintela | 3da9eeb | 2011-09-30 19:46:43 +0200 | [diff] [blame] | 1779 | sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr); |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1780 | if (sub_vmsd == NULL) { |
| 1781 | return -ENOENT; |
| 1782 | } |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 1783 | qemu_file_skip(f, 1); /* subsection */ |
| 1784 | qemu_file_skip(f, 1); /* len */ |
| 1785 | qemu_file_skip(f, len); /* idstr */ |
| 1786 | version_id = qemu_get_be32(f); |
| 1787 | |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1788 | ret = vmstate_load_state(f, sub_vmsd, opaque, version_id); |
| 1789 | if (ret) { |
| 1790 | return ret; |
| 1791 | } |
| 1792 | } |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
| 1796 | static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, |
| 1797 | void *opaque) |
| 1798 | { |
| 1799 | const VMStateSubsection *sub = vmsd->subsections; |
| 1800 | |
| 1801 | while (sub && sub->needed) { |
| 1802 | if (sub->needed(opaque)) { |
| 1803 | const VMStateDescription *vmsd = sub->vmsd; |
| 1804 | uint8_t len; |
| 1805 | |
| 1806 | qemu_put_byte(f, QEMU_VM_SUBSECTION); |
| 1807 | len = strlen(vmsd->name); |
| 1808 | qemu_put_byte(f, len); |
| 1809 | qemu_put_buffer(f, (uint8_t *)vmsd->name, len); |
| 1810 | qemu_put_be32(f, vmsd->version_id); |
| 1811 | vmstate_save_state(f, vmsd, opaque); |
| 1812 | } |
| 1813 | sub++; |
| 1814 | } |
| 1815 | } |
| 1816 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1817 | typedef struct LoadStateEntry { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1818 | QLIST_ENTRY(LoadStateEntry) entry; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1819 | SaveStateEntry *se; |
| 1820 | int section_id; |
| 1821 | int version_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1822 | } LoadStateEntry; |
| 1823 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1824 | int qemu_loadvm_state(QEMUFile *f) |
| 1825 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1826 | QLIST_HEAD(, LoadStateEntry) loadvm_handlers = |
| 1827 | QLIST_HEAD_INITIALIZER(loadvm_handlers); |
Juan Quintela | f4dbb8d | 2009-09-01 02:12:33 +0200 | [diff] [blame] | 1828 | LoadStateEntry *le, *new_le; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1829 | uint8_t section_type; |
| 1830 | unsigned int v; |
| 1831 | int ret; |
| 1832 | |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1833 | if (qemu_savevm_state_blocked(default_mon)) { |
| 1834 | return -EINVAL; |
| 1835 | } |
| 1836 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1837 | v = qemu_get_be32(f); |
| 1838 | if (v != QEMU_VM_FILE_MAGIC) |
| 1839 | return -EINVAL; |
| 1840 | |
| 1841 | v = qemu_get_be32(f); |
Juan Quintela | bbfe140 | 2009-09-10 03:04:24 +0200 | [diff] [blame] | 1842 | if (v == QEMU_VM_FILE_VERSION_COMPAT) { |
| 1843 | fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n"); |
| 1844 | return -ENOTSUP; |
| 1845 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1846 | if (v != QEMU_VM_FILE_VERSION) |
| 1847 | return -ENOTSUP; |
| 1848 | |
| 1849 | while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) { |
| 1850 | uint32_t instance_id, version_id, section_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1851 | SaveStateEntry *se; |
| 1852 | char idstr[257]; |
| 1853 | int len; |
| 1854 | |
| 1855 | switch (section_type) { |
| 1856 | case QEMU_VM_SECTION_START: |
| 1857 | case QEMU_VM_SECTION_FULL: |
| 1858 | /* Read section start */ |
| 1859 | section_id = qemu_get_be32(f); |
| 1860 | len = qemu_get_byte(f); |
| 1861 | qemu_get_buffer(f, (uint8_t *)idstr, len); |
| 1862 | idstr[len] = 0; |
| 1863 | instance_id = qemu_get_be32(f); |
| 1864 | version_id = qemu_get_be32(f); |
| 1865 | |
| 1866 | /* Find savevm section */ |
| 1867 | se = find_se(idstr, instance_id); |
| 1868 | if (se == NULL) { |
| 1869 | fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id); |
| 1870 | ret = -EINVAL; |
| 1871 | goto out; |
| 1872 | } |
| 1873 | |
| 1874 | /* Validate version */ |
| 1875 | if (version_id > se->version_id) { |
| 1876 | fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n", |
| 1877 | version_id, idstr, se->version_id); |
| 1878 | ret = -EINVAL; |
| 1879 | goto out; |
| 1880 | } |
| 1881 | |
| 1882 | /* Add entry */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1883 | le = g_malloc0(sizeof(*le)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1884 | |
| 1885 | le->se = se; |
| 1886 | le->section_id = section_id; |
| 1887 | le->version_id = version_id; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1888 | QLIST_INSERT_HEAD(&loadvm_handlers, le, entry); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1889 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1890 | ret = vmstate_load(f, le->se, le->version_id); |
Juan Quintela | b5a22e4 | 2009-08-20 19:42:23 +0200 | [diff] [blame] | 1891 | if (ret < 0) { |
| 1892 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", |
| 1893 | instance_id, idstr); |
| 1894 | goto out; |
| 1895 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1896 | break; |
| 1897 | case QEMU_VM_SECTION_PART: |
| 1898 | case QEMU_VM_SECTION_END: |
| 1899 | section_id = qemu_get_be32(f); |
| 1900 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1901 | QLIST_FOREACH(le, &loadvm_handlers, entry) { |
Juan Quintela | f4dbb8d | 2009-09-01 02:12:33 +0200 | [diff] [blame] | 1902 | if (le->section_id == section_id) { |
| 1903 | break; |
| 1904 | } |
| 1905 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1906 | if (le == NULL) { |
| 1907 | fprintf(stderr, "Unknown savevm section %d\n", section_id); |
| 1908 | ret = -EINVAL; |
| 1909 | goto out; |
| 1910 | } |
| 1911 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1912 | ret = vmstate_load(f, le->se, le->version_id); |
Juan Quintela | b5a22e4 | 2009-08-20 19:42:23 +0200 | [diff] [blame] | 1913 | if (ret < 0) { |
| 1914 | fprintf(stderr, "qemu: warning: error while loading state section id %d\n", |
| 1915 | section_id); |
| 1916 | goto out; |
| 1917 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1918 | break; |
| 1919 | default: |
| 1920 | fprintf(stderr, "Unknown savevm section type %d\n", section_type); |
| 1921 | ret = -EINVAL; |
| 1922 | goto out; |
| 1923 | } |
| 1924 | } |
| 1925 | |
Jan Kiszka | ea375f9 | 2010-03-01 19:10:30 +0100 | [diff] [blame] | 1926 | cpu_synchronize_all_post_init(); |
| 1927 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1928 | ret = 0; |
| 1929 | |
| 1930 | out: |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1931 | QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) { |
| 1932 | QLIST_REMOVE(le, entry); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1933 | g_free(le); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Juan Quintela | 42802d4 | 2011-10-05 01:14:46 +0200 | [diff] [blame] | 1936 | if (ret == 0) { |
| 1937 | ret = qemu_file_get_error(f); |
Juan Quintela | 624b9cc | 2011-10-05 01:02:52 +0200 | [diff] [blame] | 1938 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1939 | |
| 1940 | return ret; |
| 1941 | } |
| 1942 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1943 | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
| 1944 | const char *name) |
| 1945 | { |
| 1946 | QEMUSnapshotInfo *sn_tab, *sn; |
| 1947 | int nb_sns, i, ret; |
| 1948 | |
| 1949 | ret = -ENOENT; |
| 1950 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 1951 | if (nb_sns < 0) |
| 1952 | return ret; |
| 1953 | for(i = 0; i < nb_sns; i++) { |
| 1954 | sn = &sn_tab[i]; |
| 1955 | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { |
| 1956 | *sn_info = *sn; |
| 1957 | ret = 0; |
| 1958 | break; |
| 1959 | } |
| 1960 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1961 | g_free(sn_tab); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1962 | return ret; |
| 1963 | } |
| 1964 | |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1965 | /* |
| 1966 | * Deletes snapshots of a given name in all opened images. |
| 1967 | */ |
| 1968 | static int del_existing_snapshots(Monitor *mon, const char *name) |
| 1969 | { |
| 1970 | BlockDriverState *bs; |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1971 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
| 1972 | int ret; |
| 1973 | |
Markus Armbruster | dbc1359 | 2010-06-02 18:55:21 +0200 | [diff] [blame] | 1974 | bs = NULL; |
| 1975 | while ((bs = bdrv_next(bs))) { |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1976 | if (bdrv_can_snapshot(bs) && |
| 1977 | bdrv_snapshot_find(bs, snapshot, name) >= 0) |
| 1978 | { |
| 1979 | ret = bdrv_snapshot_delete(bs, name); |
| 1980 | if (ret < 0) { |
| 1981 | monitor_printf(mon, |
| 1982 | "Error while deleting snapshot on '%s'\n", |
| 1983 | bdrv_get_device_name(bs)); |
| 1984 | return -1; |
| 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | return 0; |
| 1990 | } |
| 1991 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1992 | void do_savevm(Monitor *mon, const QDict *qdict) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1993 | { |
| 1994 | BlockDriverState *bs, *bs1; |
| 1995 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 1996 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1997 | QEMUFile *f; |
| 1998 | int saved_vm_running; |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 1999 | uint32_t vm_state_size; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2000 | #ifdef _WIN32 |
| 2001 | struct _timeb tb; |
Miguel Di Ciurcio Filho | 7d631a1 | 2010-08-04 14:55:49 -0300 | [diff] [blame] | 2002 | struct tm *ptm; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2003 | #else |
| 2004 | struct timeval tv; |
Miguel Di Ciurcio Filho | 7d631a1 | 2010-08-04 14:55:49 -0300 | [diff] [blame] | 2005 | struct tm tm; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2006 | #endif |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2007 | const char *name = qdict_get_try_str(qdict, "name"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2008 | |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2009 | /* Verify if there is a device that doesn't support snapshots and is writable */ |
Markus Armbruster | dbc1359 | 2010-06-02 18:55:21 +0200 | [diff] [blame] | 2010 | bs = NULL; |
| 2011 | while ((bs = bdrv_next(bs))) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2012 | |
Markus Armbruster | 07b70bf | 2011-08-03 15:08:11 +0200 | [diff] [blame] | 2013 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2014 | continue; |
| 2015 | } |
| 2016 | |
| 2017 | if (!bdrv_can_snapshot(bs)) { |
| 2018 | monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n", |
| 2019 | bdrv_get_device_name(bs)); |
| 2020 | return; |
| 2021 | } |
| 2022 | } |
| 2023 | |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 2024 | bs = bdrv_snapshots(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2025 | if (!bs) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2026 | monitor_printf(mon, "No block device can accept snapshots\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2027 | return; |
| 2028 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2029 | |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 2030 | saved_vm_running = runstate_is_running(); |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2031 | vm_stop(RUN_STATE_SAVE_VM); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2032 | |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2033 | memset(sn, 0, sizeof(*sn)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2034 | |
| 2035 | /* fill auxiliary fields */ |
| 2036 | #ifdef _WIN32 |
| 2037 | _ftime(&tb); |
| 2038 | sn->date_sec = tb.time; |
| 2039 | sn->date_nsec = tb.millitm * 1000000; |
| 2040 | #else |
| 2041 | gettimeofday(&tv, NULL); |
| 2042 | sn->date_sec = tv.tv_sec; |
| 2043 | sn->date_nsec = tv.tv_usec * 1000; |
| 2044 | #endif |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 2045 | sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2046 | |
Miguel Di Ciurcio Filho | 7d631a1 | 2010-08-04 14:55:49 -0300 | [diff] [blame] | 2047 | if (name) { |
| 2048 | ret = bdrv_snapshot_find(bs, old_sn, name); |
| 2049 | if (ret >= 0) { |
| 2050 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); |
| 2051 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); |
| 2052 | } else { |
| 2053 | pstrcpy(sn->name, sizeof(sn->name), name); |
| 2054 | } |
| 2055 | } else { |
| 2056 | #ifdef _WIN32 |
| 2057 | ptm = localtime(&tb.time); |
| 2058 | strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm); |
| 2059 | #else |
Blue Swirl | d7d9b52 | 2010-09-09 19:13:04 +0000 | [diff] [blame] | 2060 | /* cast below needed for OpenBSD where tv_sec is still 'long' */ |
| 2061 | localtime_r((const time_t *)&tv.tv_sec, &tm); |
Miguel Di Ciurcio Filho | 7d631a1 | 2010-08-04 14:55:49 -0300 | [diff] [blame] | 2062 | strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm); |
| 2063 | #endif |
| 2064 | } |
| 2065 | |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2066 | /* Delete old snapshots of the same name */ |
Marcelo Tosatti | f139a41 | 2010-01-20 14:26:34 -0200 | [diff] [blame] | 2067 | if (name && del_existing_snapshots(mon, name) < 0) { |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2068 | goto the_end; |
| 2069 | } |
| 2070 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2071 | /* save the VM state */ |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 2072 | f = qemu_fopen_bdrv(bs, 1); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2073 | if (!f) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2074 | monitor_printf(mon, "Could not open VM state file\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2075 | goto the_end; |
| 2076 | } |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 2077 | ret = qemu_savevm_state(mon, f); |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 2078 | vm_state_size = qemu_ftell(f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2079 | qemu_fclose(f); |
| 2080 | if (ret < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2081 | monitor_printf(mon, "Error %d while writing VM\n", ret); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2082 | goto the_end; |
| 2083 | } |
| 2084 | |
| 2085 | /* create the snapshots */ |
| 2086 | |
Markus Armbruster | dbc1359 | 2010-06-02 18:55:21 +0200 | [diff] [blame] | 2087 | bs1 = NULL; |
| 2088 | while ((bs1 = bdrv_next(bs1))) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2089 | if (bdrv_can_snapshot(bs1)) { |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 2090 | /* Write VM state size only to the image that contains the state */ |
| 2091 | sn->vm_state_size = (bs == bs1 ? vm_state_size : 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2092 | ret = bdrv_snapshot_create(bs1, sn); |
| 2093 | if (ret < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2094 | monitor_printf(mon, "Error while creating snapshot on '%s'\n", |
| 2095 | bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | the_end: |
| 2101 | if (saved_vm_running) |
| 2102 | vm_start(); |
| 2103 | } |
| 2104 | |
Markus Armbruster | 03cd465 | 2010-02-17 16:24:10 +0100 | [diff] [blame] | 2105 | int load_vmstate(const char *name) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2106 | { |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2107 | BlockDriverState *bs, *bs_vm_state; |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 2108 | QEMUSnapshotInfo sn; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2109 | QEMUFile *f; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2110 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2111 | |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2112 | bs_vm_state = bdrv_snapshots(); |
| 2113 | if (!bs_vm_state) { |
| 2114 | error_report("No block device supports snapshots"); |
| 2115 | return -ENOTSUP; |
| 2116 | } |
| 2117 | |
| 2118 | /* Don't even try to load empty VM states */ |
| 2119 | ret = bdrv_snapshot_find(bs_vm_state, &sn, name); |
| 2120 | if (ret < 0) { |
| 2121 | return ret; |
| 2122 | } else if (sn.vm_state_size == 0) { |
Kevin Wolf | e11480d | 2011-03-01 10:48:12 +0100 | [diff] [blame] | 2123 | error_report("This is a disk-only snapshot. Revert to it offline " |
| 2124 | "using qemu-img."); |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2125 | return -EINVAL; |
| 2126 | } |
| 2127 | |
| 2128 | /* Verify if there is any device that doesn't support snapshots and is |
| 2129 | writable and check if the requested snapshot is available too. */ |
Markus Armbruster | dbc1359 | 2010-06-02 18:55:21 +0200 | [diff] [blame] | 2130 | bs = NULL; |
| 2131 | while ((bs = bdrv_next(bs))) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2132 | |
Markus Armbruster | 07b70bf | 2011-08-03 15:08:11 +0200 | [diff] [blame] | 2133 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2134 | continue; |
| 2135 | } |
| 2136 | |
| 2137 | if (!bdrv_can_snapshot(bs)) { |
| 2138 | error_report("Device '%s' is writable but does not support snapshots.", |
| 2139 | bdrv_get_device_name(bs)); |
| 2140 | return -ENOTSUP; |
| 2141 | } |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2142 | |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2143 | ret = bdrv_snapshot_find(bs, &sn, name); |
| 2144 | if (ret < 0) { |
| 2145 | error_report("Device '%s' does not have the requested snapshot '%s'", |
| 2146 | bdrv_get_device_name(bs), name); |
| 2147 | return ret; |
| 2148 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | /* Flush all IO requests so they don't interfere with the new state. */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 2152 | bdrv_drain_all(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2153 | |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2154 | bs = NULL; |
| 2155 | while ((bs = bdrv_next(bs))) { |
| 2156 | if (bdrv_can_snapshot(bs)) { |
| 2157 | ret = bdrv_snapshot_goto(bs, name); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2158 | if (ret < 0) { |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2159 | error_report("Error %d while activating snapshot '%s' on '%s'", |
| 2160 | ret, name, bdrv_get_device_name(bs)); |
| 2161 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2162 | } |
| 2163 | } |
| 2164 | } |
| 2165 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2166 | /* restore the VM state */ |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2167 | f = qemu_fopen_bdrv(bs_vm_state, 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2168 | if (!f) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 2169 | error_report("Could not open VM state file"); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 2170 | return -EINVAL; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2171 | } |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2172 | |
Jan Kiszka | 5a8a49d | 2011-06-14 18:29:45 +0200 | [diff] [blame] | 2173 | qemu_system_reset(VMRESET_SILENT); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2174 | ret = qemu_loadvm_state(f); |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2175 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2176 | qemu_fclose(f); |
| 2177 | if (ret < 0) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 2178 | error_report("Error %d while loading VM state", ret); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 2179 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2180 | } |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2181 | |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 2182 | return 0; |
Juan Quintela | 7b63034 | 2009-08-20 19:42:20 +0200 | [diff] [blame] | 2183 | } |
| 2184 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2185 | void do_delvm(Monitor *mon, const QDict *qdict) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2186 | { |
| 2187 | BlockDriverState *bs, *bs1; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2188 | int ret; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2189 | const char *name = qdict_get_str(qdict, "name"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2190 | |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 2191 | bs = bdrv_snapshots(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2192 | if (!bs) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2193 | monitor_printf(mon, "No block device supports snapshots\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2194 | return; |
| 2195 | } |
| 2196 | |
Markus Armbruster | dbc1359 | 2010-06-02 18:55:21 +0200 | [diff] [blame] | 2197 | bs1 = NULL; |
| 2198 | while ((bs1 = bdrv_next(bs1))) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2199 | if (bdrv_can_snapshot(bs1)) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2200 | ret = bdrv_snapshot_delete(bs1, name); |
| 2201 | if (ret < 0) { |
| 2202 | if (ret == -ENOTSUP) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2203 | monitor_printf(mon, |
| 2204 | "Snapshots not supported on device '%s'\n", |
| 2205 | bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2206 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2207 | monitor_printf(mon, "Error %d while deleting snapshot on " |
| 2208 | "'%s'\n", ret, bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2209 | } |
| 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2214 | void do_info_snapshots(Monitor *mon) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2215 | { |
| 2216 | BlockDriverState *bs, *bs1; |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2217 | QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s; |
| 2218 | int nb_sns, i, ret, available; |
| 2219 | int total; |
| 2220 | int *available_snapshots; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2221 | char buf[256]; |
| 2222 | |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 2223 | bs = bdrv_snapshots(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2224 | if (!bs) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2225 | monitor_printf(mon, "No available block device supports snapshots\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2226 | return; |
| 2227 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2228 | |
| 2229 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2230 | if (nb_sns < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2231 | monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2232 | return; |
| 2233 | } |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2234 | |
| 2235 | if (nb_sns == 0) { |
| 2236 | monitor_printf(mon, "There is no snapshot available.\n"); |
| 2237 | return; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2238 | } |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2239 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2240 | available_snapshots = g_malloc0(sizeof(int) * nb_sns); |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2241 | total = 0; |
| 2242 | for (i = 0; i < nb_sns; i++) { |
| 2243 | sn = &sn_tab[i]; |
| 2244 | available = 1; |
| 2245 | bs1 = NULL; |
| 2246 | |
| 2247 | while ((bs1 = bdrv_next(bs1))) { |
| 2248 | if (bdrv_can_snapshot(bs1) && bs1 != bs) { |
| 2249 | ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str); |
| 2250 | if (ret < 0) { |
| 2251 | available = 0; |
| 2252 | break; |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | if (available) { |
| 2258 | available_snapshots[total] = i; |
| 2259 | total++; |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | if (total > 0) { |
| 2264 | monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 2265 | for (i = 0; i < total; i++) { |
| 2266 | sn = &sn_tab[available_snapshots[i]]; |
| 2267 | monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
| 2268 | } |
| 2269 | } else { |
| 2270 | monitor_printf(mon, "There is no suitable snapshot available\n"); |
| 2271 | } |
| 2272 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2273 | g_free(sn_tab); |
| 2274 | g_free(available_snapshots); |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2275 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2276 | } |