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 | |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 25 | #include "config-host.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 26 | #include "qemu-common.h" |
| 27 | #include "hw/hw.h" |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 28 | #include "hw/qdev.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 29 | #include "net/net.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 30 | #include "monitor/monitor.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 31 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 32 | #include "qemu/timer.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 33 | #include "audio/audio.h" |
Paolo Bonzini | caf71f8 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 34 | #include "migration/migration.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 35 | #include "qemu/sockets.h" |
| 36 | #include "qemu/queue.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 37 | #include "sysemu/cpus.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 38 | #include "exec/memory.h" |
Stefano Stabellini | a7ae835 | 2012-01-25 12:24:51 +0000 | [diff] [blame] | 39 | #include "qmp-commands.h" |
Juan Quintela | 517a13c | 2012-05-21 23:46:44 +0200 | [diff] [blame] | 40 | #include "trace.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 41 | #include "qemu/bitops.h" |
Orit Wasserman | 28085f7 | 2013-03-22 16:47:58 +0200 | [diff] [blame] | 42 | #include "qemu/iov.h" |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 43 | #include "block/snapshot.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 44 | #include "block/qapi.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 45 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 46 | #define SELF_ANNOUNCE_ROUNDS 5 |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 47 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 48 | #ifndef ETH_P_RARP |
Stefan Berger | f8778a7 | 2010-04-24 08:54:07 -0400 | [diff] [blame] | 49 | #define ETH_P_RARP 0x8035 |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 50 | #endif |
| 51 | #define ARP_HTYPE_ETH 0x0001 |
| 52 | #define ARP_PTYPE_IP 0x0800 |
| 53 | #define ARP_OP_REQUEST_REV 0x3 |
| 54 | |
| 55 | static int announce_self_create(uint8_t *buf, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 56 | uint8_t *mac_addr) |
| 57 | { |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 58 | /* Ethernet header. */ |
| 59 | memset(buf, 0xff, 6); /* destination MAC addr */ |
| 60 | memcpy(buf + 6, mac_addr, 6); /* source MAC addr */ |
| 61 | *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 62 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 63 | /* RARP header. */ |
| 64 | *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */ |
| 65 | *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */ |
| 66 | *(buf + 18) = 6; /* hardware addr length (ethernet) */ |
| 67 | *(buf + 19) = 4; /* protocol addr length (IPv4) */ |
| 68 | *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */ |
| 69 | memcpy(buf + 22, mac_addr, 6); /* source hw addr */ |
| 70 | memset(buf + 28, 0x00, 4); /* source protocol addr */ |
| 71 | memcpy(buf + 32, mac_addr, 6); /* target hw addr */ |
| 72 | memset(buf + 38, 0x00, 4); /* target protocol addr */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 73 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 74 | /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */ |
| 75 | memset(buf + 42, 0x00, 18); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 76 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 77 | return 60; /* len (FCS will be added by hardware) */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Mark McLoughlin | f401ca2 | 2009-11-25 18:49:32 +0000 | [diff] [blame] | 80 | static void qemu_announce_self_iter(NICState *nic, void *opaque) |
| 81 | { |
| 82 | uint8_t buf[60]; |
| 83 | int len; |
| 84 | |
| 85 | len = announce_self_create(buf, nic->conf->macaddr.a); |
| 86 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 87 | qemu_send_packet_raw(qemu_get_queue(nic), buf, len); |
Mark McLoughlin | f401ca2 | 2009-11-25 18:49:32 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 91 | static void qemu_announce_self_once(void *opaque) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 92 | { |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 93 | static int count = SELF_ANNOUNCE_ROUNDS; |
| 94 | QEMUTimer *timer = *(QEMUTimer **)opaque; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 95 | |
Mark McLoughlin | f401ca2 | 2009-11-25 18:49:32 +0000 | [diff] [blame] | 96 | qemu_foreach_nic(qemu_announce_self_iter, NULL); |
| 97 | |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 98 | if (--count) { |
| 99 | /* delay 50ms, 150ms, 250ms, ... */ |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 100 | qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) + |
Nolan | 18995b9 | 2009-10-15 16:53:55 -0700 | [diff] [blame] | 101 | 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100); |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 102 | } else { |
| 103 | qemu_del_timer(timer); |
| 104 | qemu_free_timer(timer); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void qemu_announce_self(void) |
| 109 | { |
| 110 | static QEMUTimer *timer; |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 111 | timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer); |
Gleb Natapov | ed8b330 | 2009-05-21 17:17:44 +0300 | [diff] [blame] | 112 | qemu_announce_self_once(&timer); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /***********************************************************/ |
| 116 | /* savevm/loadvm support */ |
| 117 | |
| 118 | #define IO_BUF_SIZE 32768 |
Orit Wasserman | b3ea2bd | 2013-03-22 16:48:00 +0200 | [diff] [blame] | 119 | #define MAX_IOV_SIZE MIN(IOV_MAX, 64) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 120 | |
| 121 | struct QEMUFile { |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 122 | const QEMUFileOps *ops; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 123 | void *opaque; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 124 | |
Paolo Bonzini | 1964a39 | 2013-02-22 17:36:45 +0100 | [diff] [blame] | 125 | int64_t bytes_xfer; |
| 126 | int64_t xfer_limit; |
| 127 | |
Paolo Bonzini | 3f2d38f | 2013-02-22 17:36:40 +0100 | [diff] [blame] | 128 | int64_t pos; /* start of buffer when writing, end of buffer |
| 129 | when reading */ |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 130 | int buf_index; |
| 131 | int buf_size; /* 0 when writing */ |
| 132 | uint8_t buf[IO_BUF_SIZE]; |
| 133 | |
Orit Wasserman | b3ea2bd | 2013-03-22 16:48:00 +0200 | [diff] [blame] | 134 | struct iovec iov[MAX_IOV_SIZE]; |
| 135 | unsigned int iovcnt; |
| 136 | |
Juan Quintela | 3961b4d | 2011-10-05 01:05:21 +0200 | [diff] [blame] | 137 | int last_error; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 140 | typedef struct QEMUFileStdio |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 141 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 142 | FILE *stdio_file; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 143 | QEMUFile *file; |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 144 | } QEMUFileStdio; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 145 | |
| 146 | typedef struct QEMUFileSocket |
| 147 | { |
| 148 | int fd; |
| 149 | QEMUFile *file; |
| 150 | } QEMUFileSocket; |
| 151 | |
Kevin Wolf | 05fcc84 | 2013-04-05 21:27:54 +0200 | [diff] [blame] | 152 | static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt, |
| 153 | int64_t pos) |
Orit Wasserman | 28085f7 | 2013-03-22 16:47:58 +0200 | [diff] [blame] | 154 | { |
| 155 | QEMUFileSocket *s = opaque; |
| 156 | ssize_t len; |
| 157 | ssize_t size = iov_size(iov, iovcnt); |
| 158 | |
| 159 | len = iov_send(s->fd, iov, iovcnt, 0, size); |
| 160 | if (len < size) { |
| 161 | len = -socket_error(); |
| 162 | } |
| 163 | return len; |
| 164 | } |
| 165 | |
Paolo Bonzini | 70eb633 | 2012-08-08 10:20:18 +0200 | [diff] [blame] | 166 | static int socket_get_fd(void *opaque) |
| 167 | { |
| 168 | QEMUFileSocket *s = opaque; |
| 169 | |
| 170 | return s->fd; |
| 171 | } |
| 172 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 173 | static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
| 174 | { |
| 175 | QEMUFileSocket *s = opaque; |
| 176 | ssize_t len; |
| 177 | |
Paolo Bonzini | 595ab64 | 2012-08-07 11:07:59 +0200 | [diff] [blame] | 178 | for (;;) { |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 179 | len = qemu_recv(s->fd, buf, size, 0); |
Paolo Bonzini | 595ab64 | 2012-08-07 11:07:59 +0200 | [diff] [blame] | 180 | if (len != -1) { |
| 181 | break; |
| 182 | } |
| 183 | if (socket_error() == EAGAIN) { |
Stefan Hajnoczi | d7cd369 | 2013-02-11 17:01:45 +0100 | [diff] [blame] | 184 | yield_until_fd_readable(s->fd); |
Paolo Bonzini | 595ab64 | 2012-08-07 11:07:59 +0200 | [diff] [blame] | 185 | } else if (socket_error() != EINTR) { |
| 186 | break; |
| 187 | } |
| 188 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 189 | |
Paolo Bonzini | 595ab64 | 2012-08-07 11:07:59 +0200 | [diff] [blame] | 190 | if (len == -1) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 191 | len = -socket_error(); |
Paolo Bonzini | 595ab64 | 2012-08-07 11:07:59 +0200 | [diff] [blame] | 192 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 193 | return len; |
| 194 | } |
| 195 | |
| 196 | static int socket_close(void *opaque) |
| 197 | { |
| 198 | QEMUFileSocket *s = opaque; |
Paolo Bonzini | ab52a82 | 2012-08-07 10:50:26 +0200 | [diff] [blame] | 199 | closesocket(s->fd); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 200 | g_free(s); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Paolo Bonzini | 70eb633 | 2012-08-08 10:20:18 +0200 | [diff] [blame] | 204 | static int stdio_get_fd(void *opaque) |
| 205 | { |
| 206 | QEMUFileStdio *s = opaque; |
| 207 | |
| 208 | return fileno(s->stdio_file); |
| 209 | } |
| 210 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 211 | 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] | 212 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 213 | QEMUFileStdio *s = opaque; |
| 214 | return fwrite(buf, 1, size, s->stdio_file); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 217 | 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] | 218 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 219 | QEMUFileStdio *s = opaque; |
| 220 | FILE *fp = s->stdio_file; |
Uri Lublin | 8a67ec4 | 2009-06-08 19:27:21 +0300 | [diff] [blame] | 221 | int bytes; |
| 222 | |
Paolo Bonzini | 595ab64 | 2012-08-07 11:07:59 +0200 | [diff] [blame] | 223 | for (;;) { |
Uri Lublin | 8a67ec4 | 2009-06-08 19:27:21 +0300 | [diff] [blame] | 224 | clearerr(fp); |
| 225 | bytes = fread(buf, 1, size, fp); |
Paolo Bonzini | 595ab64 | 2012-08-07 11:07:59 +0200 | [diff] [blame] | 226 | if (bytes != 0 || !ferror(fp)) { |
| 227 | break; |
| 228 | } |
| 229 | if (errno == EAGAIN) { |
Stefan Hajnoczi | d7cd369 | 2013-02-11 17:01:45 +0100 | [diff] [blame] | 230 | yield_until_fd_readable(fileno(fp)); |
Paolo Bonzini | 595ab64 | 2012-08-07 11:07:59 +0200 | [diff] [blame] | 231 | } else if (errno != EINTR) { |
| 232 | break; |
| 233 | } |
| 234 | } |
Uri Lublin | 8a67ec4 | 2009-06-08 19:27:21 +0300 | [diff] [blame] | 235 | return bytes; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 238 | static int stdio_pclose(void *opaque) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 239 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 240 | QEMUFileStdio *s = opaque; |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 241 | int ret; |
| 242 | ret = pclose(s->stdio_file); |
Eduardo Habkost | 26f1af0 | 2011-11-10 10:41:44 -0200 | [diff] [blame] | 243 | if (ret == -1) { |
| 244 | ret = -errno; |
Paolo Bonzini | 13c7b2d | 2013-02-22 17:36:38 +0100 | [diff] [blame] | 245 | } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) { |
| 246 | /* close succeeded, but non-zero exit code: */ |
| 247 | ret = -EIO; /* fake errno value */ |
Eduardo Habkost | 26f1af0 | 2011-11-10 10:41:44 -0200 | [diff] [blame] | 248 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 249 | g_free(s); |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 250 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 253 | static int stdio_fclose(void *opaque) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 254 | { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 255 | QEMUFileStdio *s = opaque; |
Eduardo Habkost | 0e28670 | 2011-11-10 10:41:45 -0200 | [diff] [blame] | 256 | int ret = 0; |
Paolo Bonzini | ce39ee3 | 2013-02-22 17:36:37 +0100 | [diff] [blame] | 257 | |
Orit Wasserman | cb88aa8 | 2013-03-22 16:48:01 +0200 | [diff] [blame] | 258 | if (s->file->ops->put_buffer || s->file->ops->writev_buffer) { |
Paolo Bonzini | ce39ee3 | 2013-02-22 17:36:37 +0100 | [diff] [blame] | 259 | int fd = fileno(s->stdio_file); |
| 260 | struct stat st; |
| 261 | |
| 262 | ret = fstat(fd, &st); |
| 263 | if (ret == 0 && S_ISREG(st.st_mode)) { |
| 264 | /* |
| 265 | * If the file handle is a regular file make sure the |
| 266 | * data is flushed to disk before signaling success. |
| 267 | */ |
| 268 | ret = fsync(fd); |
| 269 | if (ret != 0) { |
| 270 | ret = -errno; |
| 271 | return ret; |
| 272 | } |
| 273 | } |
| 274 | } |
Eduardo Habkost | 0e28670 | 2011-11-10 10:41:45 -0200 | [diff] [blame] | 275 | if (fclose(s->stdio_file) == EOF) { |
| 276 | ret = -errno; |
| 277 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 278 | g_free(s); |
Eduardo Habkost | 0e28670 | 2011-11-10 10:41:45 -0200 | [diff] [blame] | 279 | return ret; |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 280 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 281 | |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 282 | static const QEMUFileOps stdio_pipe_read_ops = { |
Paolo Bonzini | 70eb633 | 2012-08-08 10:20:18 +0200 | [diff] [blame] | 283 | .get_fd = stdio_get_fd, |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 284 | .get_buffer = stdio_get_buffer, |
| 285 | .close = stdio_pclose |
| 286 | }; |
| 287 | |
| 288 | static const QEMUFileOps stdio_pipe_write_ops = { |
Paolo Bonzini | 70eb633 | 2012-08-08 10:20:18 +0200 | [diff] [blame] | 289 | .get_fd = stdio_get_fd, |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 290 | .put_buffer = stdio_put_buffer, |
| 291 | .close = stdio_pclose |
| 292 | }; |
| 293 | |
Paolo Bonzini | 817b9ed | 2013-02-22 17:36:36 +0100 | [diff] [blame] | 294 | QEMUFile *qemu_popen_cmd(const char *command, const char *mode) |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 295 | { |
Paolo Bonzini | 817b9ed | 2013-02-22 17:36:36 +0100 | [diff] [blame] | 296 | FILE *stdio_file; |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 297 | QEMUFileStdio *s; |
| 298 | |
Paolo Bonzini | a4cc73d | 2013-05-31 14:00:27 +0200 | [diff] [blame] | 299 | if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) { |
| 300 | fprintf(stderr, "qemu_popen: Argument validity check failed\n"); |
Paolo Bonzini | 817b9ed | 2013-02-22 17:36:36 +0100 | [diff] [blame] | 301 | return NULL; |
| 302 | } |
| 303 | |
Paolo Bonzini | a4cc73d | 2013-05-31 14:00:27 +0200 | [diff] [blame] | 304 | stdio_file = popen(command, mode); |
| 305 | if (stdio_file == NULL) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 306 | return NULL; |
| 307 | } |
| 308 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 309 | s = g_malloc0(sizeof(QEMUFileStdio)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 310 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 311 | s->stdio_file = stdio_file; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 312 | |
| 313 | if(mode[0] == 'r') { |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 314 | s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 315 | } else { |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 316 | s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 317 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 318 | return s->file; |
| 319 | } |
| 320 | |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 321 | static const QEMUFileOps stdio_file_read_ops = { |
Paolo Bonzini | 70eb633 | 2012-08-08 10:20:18 +0200 | [diff] [blame] | 322 | .get_fd = stdio_get_fd, |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 323 | .get_buffer = stdio_get_buffer, |
| 324 | .close = stdio_fclose |
| 325 | }; |
| 326 | |
| 327 | static const QEMUFileOps stdio_file_write_ops = { |
Paolo Bonzini | 70eb633 | 2012-08-08 10:20:18 +0200 | [diff] [blame] | 328 | .get_fd = stdio_get_fd, |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 329 | .put_buffer = stdio_put_buffer, |
| 330 | .close = stdio_fclose |
| 331 | }; |
| 332 | |
Paolo Bonzini | e9d8fbf | 2013-03-27 17:36:32 +0100 | [diff] [blame] | 333 | static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt, |
| 334 | int64_t pos) |
| 335 | { |
| 336 | QEMUFileSocket *s = opaque; |
| 337 | ssize_t len, offset; |
| 338 | ssize_t size = iov_size(iov, iovcnt); |
| 339 | ssize_t total = 0; |
| 340 | |
| 341 | assert(iovcnt > 0); |
| 342 | offset = 0; |
| 343 | while (size > 0) { |
| 344 | /* Find the next start position; skip all full-sized vector elements */ |
| 345 | while (offset >= iov[0].iov_len) { |
| 346 | offset -= iov[0].iov_len; |
| 347 | iov++, iovcnt--; |
| 348 | } |
| 349 | |
| 350 | /* skip `offset' bytes from the (now) first element, undo it on exit */ |
| 351 | assert(iovcnt > 0); |
| 352 | iov[0].iov_base += offset; |
| 353 | iov[0].iov_len -= offset; |
| 354 | |
| 355 | do { |
| 356 | len = writev(s->fd, iov, iovcnt); |
| 357 | } while (len == -1 && errno == EINTR); |
| 358 | if (len == -1) { |
| 359 | return -errno; |
| 360 | } |
| 361 | |
| 362 | /* Undo the changes above */ |
| 363 | iov[0].iov_base -= offset; |
| 364 | iov[0].iov_len += offset; |
| 365 | |
| 366 | /* Prepare for the next iteration */ |
| 367 | offset += len; |
| 368 | total += len; |
| 369 | size -= len; |
| 370 | } |
| 371 | |
| 372 | return total; |
| 373 | } |
| 374 | |
| 375 | static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
| 376 | { |
| 377 | QEMUFileSocket *s = opaque; |
| 378 | ssize_t len; |
| 379 | |
| 380 | for (;;) { |
| 381 | len = read(s->fd, buf, size); |
| 382 | if (len != -1) { |
| 383 | break; |
| 384 | } |
| 385 | if (errno == EAGAIN) { |
| 386 | yield_until_fd_readable(s->fd); |
| 387 | } else if (errno != EINTR) { |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if (len == -1) { |
| 393 | len = -errno; |
| 394 | } |
| 395 | return len; |
| 396 | } |
| 397 | |
| 398 | static int unix_close(void *opaque) |
| 399 | { |
| 400 | QEMUFileSocket *s = opaque; |
| 401 | close(s->fd); |
| 402 | g_free(s); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static const QEMUFileOps unix_read_ops = { |
| 407 | .get_fd = socket_get_fd, |
| 408 | .get_buffer = unix_get_buffer, |
| 409 | .close = unix_close |
| 410 | }; |
| 411 | |
| 412 | static const QEMUFileOps unix_write_ops = { |
| 413 | .get_fd = socket_get_fd, |
| 414 | .writev_buffer = unix_writev_buffer, |
| 415 | .close = unix_close |
| 416 | }; |
| 417 | |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 418 | QEMUFile *qemu_fdopen(int fd, const char *mode) |
| 419 | { |
Paolo Bonzini | e9d8fbf | 2013-03-27 17:36:32 +0100 | [diff] [blame] | 420 | QEMUFileSocket *s; |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 421 | |
| 422 | if (mode == NULL || |
| 423 | (mode[0] != 'r' && mode[0] != 'w') || |
| 424 | mode[1] != 'b' || mode[2] != 0) { |
| 425 | fprintf(stderr, "qemu_fdopen: Argument validity check failed\n"); |
| 426 | return NULL; |
| 427 | } |
| 428 | |
Paolo Bonzini | e9d8fbf | 2013-03-27 17:36:32 +0100 | [diff] [blame] | 429 | s = g_malloc0(sizeof(QEMUFileSocket)); |
| 430 | s->fd = fd; |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 431 | |
| 432 | if(mode[0] == 'r') { |
Paolo Bonzini | e9d8fbf | 2013-03-27 17:36:32 +0100 | [diff] [blame] | 433 | s->file = qemu_fopen_ops(s, &unix_read_ops); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 434 | } else { |
Paolo Bonzini | e9d8fbf | 2013-03-27 17:36:32 +0100 | [diff] [blame] | 435 | s->file = qemu_fopen_ops(s, &unix_write_ops); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 436 | } |
| 437 | return s->file; |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 438 | } |
| 439 | |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 440 | static const QEMUFileOps socket_read_ops = { |
Paolo Bonzini | 70eb633 | 2012-08-08 10:20:18 +0200 | [diff] [blame] | 441 | .get_fd = socket_get_fd, |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 442 | .get_buffer = socket_get_buffer, |
| 443 | .close = socket_close |
| 444 | }; |
| 445 | |
Paolo Bonzini | 0cc3f3c | 2013-02-22 17:36:39 +0100 | [diff] [blame] | 446 | static const QEMUFileOps socket_write_ops = { |
| 447 | .get_fd = socket_get_fd, |
Orit Wasserman | 28085f7 | 2013-03-22 16:47:58 +0200 | [diff] [blame] | 448 | .writev_buffer = socket_writev_buffer, |
Paolo Bonzini | 0cc3f3c | 2013-02-22 17:36:39 +0100 | [diff] [blame] | 449 | .close = socket_close |
| 450 | }; |
| 451 | |
Michael R. Hines | bc1256f | 2013-06-25 21:35:31 -0400 | [diff] [blame] | 452 | bool qemu_file_mode_is_not_valid(const char *mode) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 453 | { |
Paolo Bonzini | 0cc3f3c | 2013-02-22 17:36:39 +0100 | [diff] [blame] | 454 | if (mode == NULL || |
| 455 | (mode[0] != 'r' && mode[0] != 'w') || |
| 456 | mode[1] != 'b' || mode[2] != 0) { |
| 457 | fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); |
Michael R. Hines | bc1256f | 2013-06-25 21:35:31 -0400 | [diff] [blame] | 458 | return true; |
| 459 | } |
| 460 | |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | QEMUFile *qemu_fopen_socket(int fd, const char *mode) |
| 465 | { |
| 466 | QEMUFileSocket *s; |
| 467 | |
| 468 | if (qemu_file_mode_is_not_valid(mode)) { |
Paolo Bonzini | 0cc3f3c | 2013-02-22 17:36:39 +0100 | [diff] [blame] | 469 | return NULL; |
| 470 | } |
| 471 | |
Stefan Weil | 4f08005 | 2013-06-16 13:33:05 +0200 | [diff] [blame] | 472 | s = g_malloc0(sizeof(QEMUFileSocket)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 473 | s->fd = fd; |
Paolo Bonzini | 0cc3f3c | 2013-02-22 17:36:39 +0100 | [diff] [blame] | 474 | if (mode[0] == 'w') { |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 475 | qemu_set_block(s->fd); |
Paolo Bonzini | 0cc3f3c | 2013-02-22 17:36:39 +0100 | [diff] [blame] | 476 | s->file = qemu_fopen_ops(s, &socket_write_ops); |
| 477 | } else { |
| 478 | s->file = qemu_fopen_ops(s, &socket_read_ops); |
| 479 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 480 | return s->file; |
| 481 | } |
| 482 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 483 | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
| 484 | { |
| 485 | QEMUFileStdio *s; |
| 486 | |
Michael R. Hines | bc1256f | 2013-06-25 21:35:31 -0400 | [diff] [blame] | 487 | if (qemu_file_mode_is_not_valid(mode)) { |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 488 | return NULL; |
| 489 | } |
| 490 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 491 | s = g_malloc0(sizeof(QEMUFileStdio)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 492 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 493 | s->stdio_file = fopen(filename, mode); |
| 494 | if (!s->stdio_file) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 495 | goto fail; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 496 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 497 | if(mode[0] == 'w') { |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 498 | s->file = qemu_fopen_ops(s, &stdio_file_write_ops); |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 499 | } else { |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 500 | s->file = qemu_fopen_ops(s, &stdio_file_read_ops); |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 501 | } |
| 502 | return s->file; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 503 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 504 | g_free(s); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 505 | return NULL; |
| 506 | } |
| 507 | |
Kevin Wolf | 05fcc84 | 2013-04-05 21:27:54 +0200 | [diff] [blame] | 508 | static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt, |
| 509 | int64_t pos) |
| 510 | { |
| 511 | int ret; |
| 512 | QEMUIOVector qiov; |
| 513 | |
| 514 | qemu_iovec_init_external(&qiov, iov, iovcnt); |
| 515 | ret = bdrv_writev_vmstate(opaque, &qiov, pos); |
| 516 | if (ret < 0) { |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | return qiov.size; |
| 521 | } |
| 522 | |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 523 | static int block_put_buffer(void *opaque, const uint8_t *buf, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 524 | int64_t pos, int size) |
| 525 | { |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 526 | bdrv_save_vmstate(opaque, buf, pos, size); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 527 | return size; |
| 528 | } |
| 529 | |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 530 | 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] | 531 | { |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 532 | return bdrv_load_vmstate(opaque, buf, pos, size); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static int bdrv_fclose(void *opaque) |
| 536 | { |
Paolo Bonzini | ad492c9 | 2012-06-06 00:04:50 +0200 | [diff] [blame] | 537 | return bdrv_flush(opaque); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 540 | static const QEMUFileOps bdrv_read_ops = { |
| 541 | .get_buffer = block_get_buffer, |
| 542 | .close = bdrv_fclose |
| 543 | }; |
| 544 | |
| 545 | static const QEMUFileOps bdrv_write_ops = { |
Kevin Wolf | 05fcc84 | 2013-04-05 21:27:54 +0200 | [diff] [blame] | 546 | .put_buffer = block_put_buffer, |
| 547 | .writev_buffer = block_writev_buffer, |
| 548 | .close = bdrv_fclose |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 549 | }; |
| 550 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 551 | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 552 | { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 553 | if (is_writable) |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 554 | return qemu_fopen_ops(bs, &bdrv_write_ops); |
| 555 | return qemu_fopen_ops(bs, &bdrv_read_ops); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 558 | QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 559 | { |
| 560 | QEMUFile *f; |
| 561 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 562 | f = g_malloc0(sizeof(QEMUFile)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 563 | |
| 564 | f->opaque = opaque; |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 565 | f->ops = ops; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 566 | return f; |
| 567 | } |
| 568 | |
Juan Quintela | 624b9cc | 2011-10-05 01:02:52 +0200 | [diff] [blame] | 569 | int qemu_file_get_error(QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 570 | { |
Juan Quintela | 3961b4d | 2011-10-05 01:05:21 +0200 | [diff] [blame] | 571 | return f->last_error; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Paolo Bonzini | 05f28b8 | 2013-02-22 17:36:31 +0100 | [diff] [blame] | 574 | static void qemu_file_set_error(QEMUFile *f, int ret) |
aliguori | 4dabe24 | 2009-04-05 19:30:51 +0000 | [diff] [blame] | 575 | { |
Juan Quintela | afe4193 | 2013-01-14 13:36:28 +0100 | [diff] [blame] | 576 | if (f->last_error == 0) { |
| 577 | f->last_error = ret; |
| 578 | } |
aliguori | 4dabe24 | 2009-04-05 19:30:51 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Paolo Bonzini | d9658c4 | 2013-04-08 13:29:56 +0200 | [diff] [blame] | 581 | static inline bool qemu_file_is_writable(QEMUFile *f) |
| 582 | { |
| 583 | return f->ops->writev_buffer || f->ops->put_buffer; |
| 584 | } |
| 585 | |
Orit Wasserman | cb88aa8 | 2013-03-22 16:48:01 +0200 | [diff] [blame] | 586 | /** |
| 587 | * Flushes QEMUFile buffer |
Eduardo Habkost | d82ca91 | 2011-11-10 10:41:43 -0200 | [diff] [blame] | 588 | * |
Orit Wasserman | cb88aa8 | 2013-03-22 16:48:01 +0200 | [diff] [blame] | 589 | * If there is writev_buffer QEMUFileOps it uses it otherwise uses |
| 590 | * put_buffer ops. |
Eduardo Habkost | d82ca91 | 2011-11-10 10:41:43 -0200 | [diff] [blame] | 591 | */ |
Michael R. Hines | be903b2 | 2013-06-25 21:35:32 -0400 | [diff] [blame] | 592 | void qemu_fflush(QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 593 | { |
Orit Wasserman | cb88aa8 | 2013-03-22 16:48:01 +0200 | [diff] [blame] | 594 | ssize_t ret = 0; |
Juan Quintela | 7311bea | 2012-08-29 19:08:59 +0200 | [diff] [blame] | 595 | |
Paolo Bonzini | d9658c4 | 2013-04-08 13:29:56 +0200 | [diff] [blame] | 596 | if (!qemu_file_is_writable(f)) { |
Paolo Bonzini | 93bf210 | 2013-02-22 17:36:12 +0100 | [diff] [blame] | 597 | return; |
| 598 | } |
Orit Wasserman | cb88aa8 | 2013-03-22 16:48:01 +0200 | [diff] [blame] | 599 | |
Paolo Bonzini | d9658c4 | 2013-04-08 13:29:56 +0200 | [diff] [blame] | 600 | if (f->ops->writev_buffer) { |
| 601 | if (f->iovcnt > 0) { |
Kevin Wolf | 05fcc84 | 2013-04-05 21:27:54 +0200 | [diff] [blame] | 602 | ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos); |
Juan Quintela | 7311bea | 2012-08-29 19:08:59 +0200 | [diff] [blame] | 603 | } |
Paolo Bonzini | d9658c4 | 2013-04-08 13:29:56 +0200 | [diff] [blame] | 604 | } else { |
| 605 | if (f->buf_index > 0) { |
| 606 | ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index); |
Paolo Bonzini | 7ce51f1 | 2013-04-08 13:29:55 +0200 | [diff] [blame] | 607 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 608 | } |
Paolo Bonzini | d9658c4 | 2013-04-08 13:29:56 +0200 | [diff] [blame] | 609 | if (ret >= 0) { |
| 610 | f->pos += ret; |
| 611 | } |
| 612 | f->buf_index = 0; |
| 613 | f->iovcnt = 0; |
Paolo Bonzini | 93bf210 | 2013-02-22 17:36:12 +0100 | [diff] [blame] | 614 | if (ret < 0) { |
| 615 | qemu_file_set_error(f, ret); |
| 616 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Michael R. Hines | 43487c6 | 2013-06-25 21:35:35 -0400 | [diff] [blame] | 619 | void ram_control_before_iterate(QEMUFile *f, uint64_t flags) |
| 620 | { |
| 621 | int ret = 0; |
| 622 | |
| 623 | if (f->ops->before_ram_iterate) { |
| 624 | ret = f->ops->before_ram_iterate(f, f->opaque, flags); |
| 625 | if (ret < 0) { |
| 626 | qemu_file_set_error(f, ret); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | void ram_control_after_iterate(QEMUFile *f, uint64_t flags) |
| 632 | { |
| 633 | int ret = 0; |
| 634 | |
| 635 | if (f->ops->after_ram_iterate) { |
| 636 | ret = f->ops->after_ram_iterate(f, f->opaque, flags); |
| 637 | if (ret < 0) { |
| 638 | qemu_file_set_error(f, ret); |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | void ram_control_load_hook(QEMUFile *f, uint64_t flags) |
| 644 | { |
| 645 | int ret = 0; |
| 646 | |
| 647 | if (f->ops->hook_ram_load) { |
| 648 | ret = f->ops->hook_ram_load(f, f->opaque, flags); |
| 649 | if (ret < 0) { |
| 650 | qemu_file_set_error(f, ret); |
| 651 | } |
| 652 | } else { |
| 653 | qemu_file_set_error(f, ret); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, |
| 658 | ram_addr_t offset, size_t size, int *bytes_sent) |
| 659 | { |
| 660 | if (f->ops->save_page) { |
| 661 | int ret = f->ops->save_page(f, f->opaque, block_offset, |
| 662 | offset, size, bytes_sent); |
| 663 | |
| 664 | if (ret != RAM_SAVE_CONTROL_DELAYED) { |
| 665 | if (*bytes_sent > 0) { |
| 666 | qemu_update_position(f, *bytes_sent); |
| 667 | } else if (ret < 0) { |
| 668 | qemu_file_set_error(f, ret); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return ret; |
| 673 | } |
| 674 | |
| 675 | return RAM_SAVE_CONTROL_NOT_SUPP; |
| 676 | } |
| 677 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 678 | static void qemu_fill_buffer(QEMUFile *f) |
| 679 | { |
| 680 | int len; |
Juan Quintela | 0046c45 | 2011-09-30 19:28:45 +0200 | [diff] [blame] | 681 | int pending; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 682 | |
Paolo Bonzini | d9658c4 | 2013-04-08 13:29:56 +0200 | [diff] [blame] | 683 | assert(!qemu_file_is_writable(f)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 684 | |
Juan Quintela | 0046c45 | 2011-09-30 19:28:45 +0200 | [diff] [blame] | 685 | pending = f->buf_size - f->buf_index; |
| 686 | if (pending > 0) { |
| 687 | memmove(f->buf, f->buf + f->buf_index, pending); |
| 688 | } |
| 689 | f->buf_index = 0; |
| 690 | f->buf_size = pending; |
| 691 | |
Paolo Bonzini | 3f2d38f | 2013-02-22 17:36:40 +0100 | [diff] [blame] | 692 | len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos, |
Juan Quintela | 0046c45 | 2011-09-30 19:28:45 +0200 | [diff] [blame] | 693 | IO_BUF_SIZE - pending); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 694 | if (len > 0) { |
Juan Quintela | 0046c45 | 2011-09-30 19:28:45 +0200 | [diff] [blame] | 695 | f->buf_size += len; |
Paolo Bonzini | 3f2d38f | 2013-02-22 17:36:40 +0100 | [diff] [blame] | 696 | f->pos += len; |
Juan Quintela | fa39a30 | 2011-10-25 19:18:58 +0200 | [diff] [blame] | 697 | } else if (len == 0) { |
Juan Quintela | 02c4a05 | 2012-08-29 19:36:26 +0200 | [diff] [blame] | 698 | qemu_file_set_error(f, -EIO); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 699 | } else if (len != -EAGAIN) |
Eduardo Habkost | c29110d | 2011-11-10 10:41:39 -0200 | [diff] [blame] | 700 | qemu_file_set_error(f, len); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Paolo Bonzini | 70eb633 | 2012-08-08 10:20:18 +0200 | [diff] [blame] | 703 | int qemu_get_fd(QEMUFile *f) |
| 704 | { |
| 705 | if (f->ops->get_fd) { |
| 706 | return f->ops->get_fd(f->opaque); |
| 707 | } |
| 708 | return -1; |
| 709 | } |
| 710 | |
Michael R. Hines | 2b0ce07 | 2013-06-25 21:35:28 -0400 | [diff] [blame] | 711 | void qemu_update_position(QEMUFile *f, size_t size) |
| 712 | { |
| 713 | f->pos += size; |
| 714 | } |
| 715 | |
Eduardo Habkost | d82ca91 | 2011-11-10 10:41:43 -0200 | [diff] [blame] | 716 | /** Closes the file |
| 717 | * |
| 718 | * Returns negative error value if any error happened on previous operations or |
| 719 | * while closing the file. Returns 0 or positive number on success. |
| 720 | * |
| 721 | * The meaning of return value on success depends on the specific backend |
| 722 | * being used. |
| 723 | */ |
| 724 | int qemu_fclose(QEMUFile *f) |
| 725 | { |
Juan Quintela | 29eee86 | 2012-08-29 19:14:54 +0200 | [diff] [blame] | 726 | int ret; |
Paolo Bonzini | 93bf210 | 2013-02-22 17:36:12 +0100 | [diff] [blame] | 727 | qemu_fflush(f); |
| 728 | ret = qemu_file_get_error(f); |
Juan Quintela | 7311bea | 2012-08-29 19:08:59 +0200 | [diff] [blame] | 729 | |
Paolo Bonzini | 9229bf3 | 2012-08-08 10:15:15 +0200 | [diff] [blame] | 730 | if (f->ops->close) { |
| 731 | int ret2 = f->ops->close(f->opaque); |
Juan Quintela | 29eee86 | 2012-08-29 19:14:54 +0200 | [diff] [blame] | 732 | if (ret >= 0) { |
| 733 | ret = ret2; |
| 734 | } |
Juan Quintela | 7311bea | 2012-08-29 19:08:59 +0200 | [diff] [blame] | 735 | } |
Eduardo Habkost | d82ca91 | 2011-11-10 10:41:43 -0200 | [diff] [blame] | 736 | /* If any error was spotted before closing, we should report it |
| 737 | * instead of the close() return value. |
| 738 | */ |
| 739 | if (f->last_error) { |
| 740 | ret = f->last_error; |
| 741 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 742 | g_free(f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 743 | return ret; |
| 744 | } |
| 745 | |
Orit Wasserman | b3ea2bd | 2013-03-22 16:48:00 +0200 | [diff] [blame] | 746 | static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size) |
| 747 | { |
| 748 | /* check for adjacent buffer and coalesce them */ |
| 749 | if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base + |
| 750 | f->iov[f->iovcnt - 1].iov_len) { |
| 751 | f->iov[f->iovcnt - 1].iov_len += size; |
| 752 | } else { |
| 753 | f->iov[f->iovcnt].iov_base = (uint8_t *)buf; |
| 754 | f->iov[f->iovcnt++].iov_len = size; |
| 755 | } |
Paolo Bonzini | af74db7 | 2013-04-08 13:29:54 +0200 | [diff] [blame] | 756 | |
Paolo Bonzini | 4d11724 | 2013-04-08 13:29:57 +0200 | [diff] [blame] | 757 | if (f->iovcnt >= MAX_IOV_SIZE) { |
Paolo Bonzini | af74db7 | 2013-04-08 13:29:54 +0200 | [diff] [blame] | 758 | qemu_fflush(f); |
| 759 | } |
Orit Wasserman | b3ea2bd | 2013-03-22 16:48:00 +0200 | [diff] [blame] | 760 | } |
| 761 | |
Orit Wasserman | 6181ec2 | 2013-03-22 16:48:02 +0200 | [diff] [blame] | 762 | void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size) |
| 763 | { |
Paolo Bonzini | 7ce51f1 | 2013-04-08 13:29:55 +0200 | [diff] [blame] | 764 | if (!f->ops->writev_buffer) { |
| 765 | qemu_put_buffer(f, buf, size); |
| 766 | return; |
| 767 | } |
| 768 | |
Orit Wasserman | 6181ec2 | 2013-03-22 16:48:02 +0200 | [diff] [blame] | 769 | if (f->last_error) { |
| 770 | return; |
| 771 | } |
| 772 | |
Orit Wasserman | 6181ec2 | 2013-03-22 16:48:02 +0200 | [diff] [blame] | 773 | f->bytes_xfer += size; |
Paolo Bonzini | af74db7 | 2013-04-08 13:29:54 +0200 | [diff] [blame] | 774 | add_to_iovec(f, buf, size); |
Orit Wasserman | 6181ec2 | 2013-03-22 16:48:02 +0200 | [diff] [blame] | 775 | } |
| 776 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 777 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
| 778 | { |
| 779 | int l; |
| 780 | |
Juan Quintela | c10682c | 2012-08-29 19:43:39 +0200 | [diff] [blame] | 781 | if (f->last_error) { |
| 782 | return; |
| 783 | } |
| 784 | |
Juan Quintela | c10682c | 2012-08-29 19:43:39 +0200 | [diff] [blame] | 785 | while (size > 0) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 786 | l = IO_BUF_SIZE - f->buf_index; |
| 787 | if (l > size) |
| 788 | l = size; |
| 789 | memcpy(f->buf + f->buf_index, buf, l); |
Paolo Bonzini | 7ce51f1 | 2013-04-08 13:29:55 +0200 | [diff] [blame] | 790 | f->bytes_xfer += size; |
| 791 | if (f->ops->writev_buffer) { |
| 792 | add_to_iovec(f, f->buf + f->buf_index, l); |
Paolo Bonzini | 4d11724 | 2013-04-08 13:29:57 +0200 | [diff] [blame] | 793 | } |
| 794 | f->buf_index += l; |
| 795 | if (f->buf_index == IO_BUF_SIZE) { |
| 796 | qemu_fflush(f); |
Paolo Bonzini | 7ce51f1 | 2013-04-08 13:29:55 +0200 | [diff] [blame] | 797 | } |
Orit Wasserman | 6181ec2 | 2013-03-22 16:48:02 +0200 | [diff] [blame] | 798 | if (qemu_file_get_error(f)) { |
| 799 | break; |
| 800 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 801 | buf += l; |
| 802 | size -= l; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
| 806 | void qemu_put_byte(QEMUFile *f, int v) |
| 807 | { |
Juan Quintela | c10682c | 2012-08-29 19:43:39 +0200 | [diff] [blame] | 808 | if (f->last_error) { |
| 809 | return; |
| 810 | } |
| 811 | |
Paolo Bonzini | af74db7 | 2013-04-08 13:29:54 +0200 | [diff] [blame] | 812 | f->buf[f->buf_index] = v; |
Orit Wasserman | 7d8a30b | 2013-03-22 16:47:59 +0200 | [diff] [blame] | 813 | f->bytes_xfer++; |
Paolo Bonzini | 7ce51f1 | 2013-04-08 13:29:55 +0200 | [diff] [blame] | 814 | if (f->ops->writev_buffer) { |
| 815 | add_to_iovec(f, f->buf + f->buf_index, 1); |
Paolo Bonzini | 4d11724 | 2013-04-08 13:29:57 +0200 | [diff] [blame] | 816 | } |
| 817 | f->buf_index++; |
| 818 | if (f->buf_index == IO_BUF_SIZE) { |
| 819 | qemu_fflush(f); |
Paolo Bonzini | 7ce51f1 | 2013-04-08 13:29:55 +0200 | [diff] [blame] | 820 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 823 | static void qemu_file_skip(QEMUFile *f, int size) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 824 | { |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 825 | if (f->buf_index + size <= f->buf_size) { |
| 826 | f->buf_index += size; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 827 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 830 | 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] | 831 | { |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 832 | int pending; |
| 833 | int index; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 834 | |
Paolo Bonzini | d9658c4 | 2013-04-08 13:29:56 +0200 | [diff] [blame] | 835 | assert(!qemu_file_is_writable(f)); |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 836 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 837 | index = f->buf_index + offset; |
| 838 | pending = f->buf_size - index; |
| 839 | if (pending < size) { |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 840 | qemu_fill_buffer(f); |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 841 | index = f->buf_index + offset; |
| 842 | pending = f->buf_size - index; |
| 843 | } |
| 844 | |
| 845 | if (pending <= 0) { |
| 846 | return 0; |
| 847 | } |
| 848 | if (size > pending) { |
| 849 | size = pending; |
| 850 | } |
| 851 | |
| 852 | memcpy(buf, f->buf + index, size); |
| 853 | return size; |
| 854 | } |
| 855 | |
| 856 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) |
| 857 | { |
| 858 | int pending = size; |
| 859 | int done = 0; |
| 860 | |
| 861 | while (pending > 0) { |
| 862 | int res; |
| 863 | |
| 864 | res = qemu_peek_buffer(f, buf, pending, 0); |
| 865 | if (res == 0) { |
| 866 | return done; |
| 867 | } |
| 868 | qemu_file_skip(f, res); |
| 869 | buf += res; |
| 870 | pending -= res; |
| 871 | done += res; |
| 872 | } |
| 873 | return done; |
| 874 | } |
| 875 | |
| 876 | static int qemu_peek_byte(QEMUFile *f, int offset) |
| 877 | { |
| 878 | int index = f->buf_index + offset; |
| 879 | |
Paolo Bonzini | d9658c4 | 2013-04-08 13:29:56 +0200 | [diff] [blame] | 880 | assert(!qemu_file_is_writable(f)); |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 881 | |
| 882 | if (index >= f->buf_size) { |
| 883 | qemu_fill_buffer(f); |
| 884 | index = f->buf_index + offset; |
| 885 | if (index >= f->buf_size) { |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 886 | return 0; |
Juan Quintela | b9ce145 | 2011-10-04 13:55:32 +0200 | [diff] [blame] | 887 | } |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 888 | } |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 889 | return f->buf[index]; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 890 | } |
| 891 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 892 | int qemu_get_byte(QEMUFile *f) |
| 893 | { |
Juan Quintela | 65f3bb3 | 2011-10-06 14:29:32 +0200 | [diff] [blame] | 894 | int result; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 895 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 896 | result = qemu_peek_byte(f, 0); |
| 897 | qemu_file_skip(f, 1); |
Juan Quintela | 65f3bb3 | 2011-10-06 14:29:32 +0200 | [diff] [blame] | 898 | return result; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Stefan Hajnoczi | ad55ab4 | 2013-02-12 10:37:14 +0100 | [diff] [blame] | 901 | int64_t qemu_ftell(QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 902 | { |
Paolo Bonzini | 3f2d38f | 2013-02-22 17:36:40 +0100 | [diff] [blame] | 903 | qemu_fflush(f); |
| 904 | return f->pos; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 905 | } |
| 906 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 907 | int qemu_file_rate_limit(QEMUFile *f) |
| 908 | { |
Paolo Bonzini | 1964a39 | 2013-02-22 17:36:45 +0100 | [diff] [blame] | 909 | if (qemu_file_get_error(f)) { |
| 910 | return 1; |
| 911 | } |
| 912 | if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) { |
| 913 | return 1; |
| 914 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | |
Michael S. Tsirkin | 3d002df | 2010-11-23 19:05:54 +0200 | [diff] [blame] | 918 | int64_t qemu_file_get_rate_limit(QEMUFile *f) |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 919 | { |
Paolo Bonzini | 1964a39 | 2013-02-22 17:36:45 +0100 | [diff] [blame] | 920 | return f->xfer_limit; |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 921 | } |
| 922 | |
Paolo Bonzini | 1964a39 | 2013-02-22 17:36:45 +0100 | [diff] [blame] | 923 | void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit) |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 924 | { |
Paolo Bonzini | 1964a39 | 2013-02-22 17:36:45 +0100 | [diff] [blame] | 925 | f->xfer_limit = limit; |
| 926 | } |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 927 | |
Paolo Bonzini | 1964a39 | 2013-02-22 17:36:45 +0100 | [diff] [blame] | 928 | void qemu_file_reset_rate_limit(QEMUFile *f) |
| 929 | { |
| 930 | f->bytes_xfer = 0; |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 931 | } |
| 932 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 933 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
| 934 | { |
| 935 | qemu_put_byte(f, v >> 8); |
| 936 | qemu_put_byte(f, v); |
| 937 | } |
| 938 | |
| 939 | void qemu_put_be32(QEMUFile *f, unsigned int v) |
| 940 | { |
| 941 | qemu_put_byte(f, v >> 24); |
| 942 | qemu_put_byte(f, v >> 16); |
| 943 | qemu_put_byte(f, v >> 8); |
| 944 | qemu_put_byte(f, v); |
| 945 | } |
| 946 | |
| 947 | void qemu_put_be64(QEMUFile *f, uint64_t v) |
| 948 | { |
| 949 | qemu_put_be32(f, v >> 32); |
| 950 | qemu_put_be32(f, v); |
| 951 | } |
| 952 | |
| 953 | unsigned int qemu_get_be16(QEMUFile *f) |
| 954 | { |
| 955 | unsigned int v; |
| 956 | v = qemu_get_byte(f) << 8; |
| 957 | v |= qemu_get_byte(f); |
| 958 | return v; |
| 959 | } |
| 960 | |
| 961 | unsigned int qemu_get_be32(QEMUFile *f) |
| 962 | { |
| 963 | unsigned int v; |
| 964 | v = qemu_get_byte(f) << 24; |
| 965 | v |= qemu_get_byte(f) << 16; |
| 966 | v |= qemu_get_byte(f) << 8; |
| 967 | v |= qemu_get_byte(f); |
| 968 | return v; |
| 969 | } |
| 970 | |
| 971 | uint64_t qemu_get_be64(QEMUFile *f) |
| 972 | { |
| 973 | uint64_t v; |
| 974 | v = (uint64_t)qemu_get_be32(f) << 32; |
| 975 | v |= qemu_get_be32(f); |
| 976 | return v; |
| 977 | } |
| 978 | |
Paolo Bonzini | 2ff68d0 | 2011-09-12 16:21:44 +0200 | [diff] [blame] | 979 | |
| 980 | /* timer */ |
| 981 | |
| 982 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 983 | { |
| 984 | uint64_t expire_time; |
| 985 | |
| 986 | expire_time = qemu_timer_expire_time_ns(ts); |
| 987 | qemu_put_be64(f, expire_time); |
| 988 | } |
| 989 | |
| 990 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 991 | { |
| 992 | uint64_t expire_time; |
| 993 | |
| 994 | expire_time = qemu_get_be64(f); |
| 995 | if (expire_time != -1) { |
| 996 | qemu_mod_timer_ns(ts, expire_time); |
| 997 | } else { |
| 998 | qemu_del_timer(ts); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | |
Gerd Hoffmann | cdae5cf | 2010-11-01 15:51:54 +0100 | [diff] [blame] | 1003 | /* bool */ |
| 1004 | |
| 1005 | static int get_bool(QEMUFile *f, void *pv, size_t size) |
| 1006 | { |
| 1007 | bool *v = pv; |
| 1008 | *v = qemu_get_byte(f); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static void put_bool(QEMUFile *f, void *pv, size_t size) |
| 1013 | { |
| 1014 | bool *v = pv; |
| 1015 | qemu_put_byte(f, *v); |
| 1016 | } |
| 1017 | |
| 1018 | const VMStateInfo vmstate_info_bool = { |
| 1019 | .name = "bool", |
| 1020 | .get = get_bool, |
| 1021 | .put = put_bool, |
| 1022 | }; |
| 1023 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1024 | /* 8 bit int */ |
| 1025 | |
| 1026 | static int get_int8(QEMUFile *f, void *pv, size_t size) |
| 1027 | { |
| 1028 | int8_t *v = pv; |
| 1029 | qemu_get_s8s(f, v); |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1033 | static void put_int8(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1034 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1035 | int8_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1036 | qemu_put_s8s(f, v); |
| 1037 | } |
| 1038 | |
| 1039 | const VMStateInfo vmstate_info_int8 = { |
| 1040 | .name = "int8", |
| 1041 | .get = get_int8, |
| 1042 | .put = put_int8, |
| 1043 | }; |
| 1044 | |
| 1045 | /* 16 bit int */ |
| 1046 | |
| 1047 | static int get_int16(QEMUFile *f, void *pv, size_t size) |
| 1048 | { |
| 1049 | int16_t *v = pv; |
| 1050 | qemu_get_sbe16s(f, v); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1054 | static void put_int16(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1055 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1056 | int16_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1057 | qemu_put_sbe16s(f, v); |
| 1058 | } |
| 1059 | |
| 1060 | const VMStateInfo vmstate_info_int16 = { |
| 1061 | .name = "int16", |
| 1062 | .get = get_int16, |
| 1063 | .put = put_int16, |
| 1064 | }; |
| 1065 | |
| 1066 | /* 32 bit int */ |
| 1067 | |
| 1068 | static int get_int32(QEMUFile *f, void *pv, size_t size) |
| 1069 | { |
| 1070 | int32_t *v = pv; |
| 1071 | qemu_get_sbe32s(f, v); |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1075 | static void put_int32(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1076 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1077 | int32_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1078 | qemu_put_sbe32s(f, v); |
| 1079 | } |
| 1080 | |
| 1081 | const VMStateInfo vmstate_info_int32 = { |
| 1082 | .name = "int32", |
| 1083 | .get = get_int32, |
| 1084 | .put = put_int32, |
| 1085 | }; |
| 1086 | |
Juan Quintela | 8250166 | 2009-08-20 19:42:32 +0200 | [diff] [blame] | 1087 | /* 32 bit int. See that the received value is the same than the one |
| 1088 | in the field */ |
| 1089 | |
| 1090 | static int get_int32_equal(QEMUFile *f, void *pv, size_t size) |
| 1091 | { |
| 1092 | int32_t *v = pv; |
| 1093 | int32_t v2; |
| 1094 | qemu_get_sbe32s(f, &v2); |
| 1095 | |
| 1096 | if (*v == v2) |
| 1097 | return 0; |
| 1098 | return -EINVAL; |
| 1099 | } |
| 1100 | |
| 1101 | const VMStateInfo vmstate_info_int32_equal = { |
| 1102 | .name = "int32 equal", |
| 1103 | .get = get_int32_equal, |
| 1104 | .put = put_int32, |
| 1105 | }; |
| 1106 | |
Juan Quintela | 0a031e0 | 2009-08-20 19:42:37 +0200 | [diff] [blame] | 1107 | /* 32 bit int. See that the received value is the less or the same |
| 1108 | than the one in the field */ |
| 1109 | |
| 1110 | static int get_int32_le(QEMUFile *f, void *pv, size_t size) |
| 1111 | { |
| 1112 | int32_t *old = pv; |
| 1113 | int32_t new; |
| 1114 | qemu_get_sbe32s(f, &new); |
| 1115 | |
| 1116 | if (*old <= new) |
| 1117 | return 0; |
| 1118 | return -EINVAL; |
| 1119 | } |
| 1120 | |
| 1121 | const VMStateInfo vmstate_info_int32_le = { |
| 1122 | .name = "int32 equal", |
| 1123 | .get = get_int32_le, |
| 1124 | .put = put_int32, |
| 1125 | }; |
| 1126 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1127 | /* 64 bit int */ |
| 1128 | |
| 1129 | static int get_int64(QEMUFile *f, void *pv, size_t size) |
| 1130 | { |
| 1131 | int64_t *v = pv; |
| 1132 | qemu_get_sbe64s(f, v); |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1136 | static void put_int64(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1137 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1138 | int64_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1139 | qemu_put_sbe64s(f, v); |
| 1140 | } |
| 1141 | |
| 1142 | const VMStateInfo vmstate_info_int64 = { |
| 1143 | .name = "int64", |
| 1144 | .get = get_int64, |
| 1145 | .put = put_int64, |
| 1146 | }; |
| 1147 | |
| 1148 | /* 8 bit unsigned int */ |
| 1149 | |
| 1150 | static int get_uint8(QEMUFile *f, void *pv, size_t size) |
| 1151 | { |
| 1152 | uint8_t *v = pv; |
| 1153 | qemu_get_8s(f, v); |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1157 | static void put_uint8(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1158 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1159 | uint8_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1160 | qemu_put_8s(f, v); |
| 1161 | } |
| 1162 | |
| 1163 | const VMStateInfo vmstate_info_uint8 = { |
| 1164 | .name = "uint8", |
| 1165 | .get = get_uint8, |
| 1166 | .put = put_uint8, |
| 1167 | }; |
| 1168 | |
| 1169 | /* 16 bit unsigned int */ |
| 1170 | |
| 1171 | static int get_uint16(QEMUFile *f, void *pv, size_t size) |
| 1172 | { |
| 1173 | uint16_t *v = pv; |
| 1174 | qemu_get_be16s(f, v); |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1178 | static void put_uint16(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1179 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1180 | uint16_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1181 | qemu_put_be16s(f, v); |
| 1182 | } |
| 1183 | |
| 1184 | const VMStateInfo vmstate_info_uint16 = { |
| 1185 | .name = "uint16", |
| 1186 | .get = get_uint16, |
| 1187 | .put = put_uint16, |
| 1188 | }; |
| 1189 | |
| 1190 | /* 32 bit unsigned int */ |
| 1191 | |
| 1192 | static int get_uint32(QEMUFile *f, void *pv, size_t size) |
| 1193 | { |
| 1194 | uint32_t *v = pv; |
| 1195 | qemu_get_be32s(f, v); |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1199 | static void put_uint32(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1200 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1201 | uint32_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1202 | qemu_put_be32s(f, v); |
| 1203 | } |
| 1204 | |
| 1205 | const VMStateInfo vmstate_info_uint32 = { |
| 1206 | .name = "uint32", |
| 1207 | .get = get_uint32, |
| 1208 | .put = put_uint32, |
| 1209 | }; |
| 1210 | |
Juan Quintela | 9122a8f | 2011-03-10 12:33:48 +0100 | [diff] [blame] | 1211 | /* 32 bit uint. See that the received value is the same than the one |
| 1212 | in the field */ |
| 1213 | |
| 1214 | static int get_uint32_equal(QEMUFile *f, void *pv, size_t size) |
| 1215 | { |
| 1216 | uint32_t *v = pv; |
| 1217 | uint32_t v2; |
| 1218 | qemu_get_be32s(f, &v2); |
| 1219 | |
| 1220 | if (*v == v2) { |
| 1221 | return 0; |
| 1222 | } |
| 1223 | return -EINVAL; |
| 1224 | } |
| 1225 | |
| 1226 | const VMStateInfo vmstate_info_uint32_equal = { |
| 1227 | .name = "uint32 equal", |
| 1228 | .get = get_uint32_equal, |
| 1229 | .put = put_uint32, |
| 1230 | }; |
| 1231 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1232 | /* 64 bit unsigned int */ |
| 1233 | |
| 1234 | static int get_uint64(QEMUFile *f, void *pv, size_t size) |
| 1235 | { |
| 1236 | uint64_t *v = pv; |
| 1237 | qemu_get_be64s(f, v); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1241 | static void put_uint64(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1242 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1243 | uint64_t *v = pv; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1244 | qemu_put_be64s(f, v); |
| 1245 | } |
| 1246 | |
| 1247 | const VMStateInfo vmstate_info_uint64 = { |
| 1248 | .name = "uint64", |
| 1249 | .get = get_uint64, |
| 1250 | .put = put_uint64, |
| 1251 | }; |
| 1252 | |
David Gibson | e344b8a | 2013-03-12 14:06:00 +1100 | [diff] [blame] | 1253 | /* 64 bit unsigned int. See that the received value is the same than the one |
| 1254 | in the field */ |
| 1255 | |
| 1256 | static int get_uint64_equal(QEMUFile *f, void *pv, size_t size) |
| 1257 | { |
| 1258 | uint64_t *v = pv; |
| 1259 | uint64_t v2; |
| 1260 | qemu_get_be64s(f, &v2); |
| 1261 | |
| 1262 | if (*v == v2) { |
| 1263 | return 0; |
| 1264 | } |
| 1265 | return -EINVAL; |
| 1266 | } |
| 1267 | |
| 1268 | const VMStateInfo vmstate_info_uint64_equal = { |
| 1269 | .name = "int64 equal", |
| 1270 | .get = get_uint64_equal, |
| 1271 | .put = put_uint64, |
| 1272 | }; |
| 1273 | |
Juan Quintela | 80cd83e | 2009-09-10 03:04:36 +0200 | [diff] [blame] | 1274 | /* 8 bit int. See that the received value is the same than the one |
| 1275 | in the field */ |
| 1276 | |
| 1277 | static int get_uint8_equal(QEMUFile *f, void *pv, size_t size) |
| 1278 | { |
| 1279 | uint8_t *v = pv; |
| 1280 | uint8_t v2; |
| 1281 | qemu_get_8s(f, &v2); |
| 1282 | |
| 1283 | if (*v == v2) |
| 1284 | return 0; |
| 1285 | return -EINVAL; |
| 1286 | } |
| 1287 | |
| 1288 | const VMStateInfo vmstate_info_uint8_equal = { |
Juan Quintela | aa1cce6 | 2009-10-15 19:16:06 +0200 | [diff] [blame] | 1289 | .name = "uint8 equal", |
Juan Quintela | 80cd83e | 2009-09-10 03:04:36 +0200 | [diff] [blame] | 1290 | .get = get_uint8_equal, |
| 1291 | .put = put_uint8, |
| 1292 | }; |
| 1293 | |
Juan Quintela | dc3b83a | 2009-10-15 23:16:13 +0200 | [diff] [blame] | 1294 | /* 16 bit unsigned int int. See that the received value is the same than the one |
| 1295 | in the field */ |
| 1296 | |
| 1297 | static int get_uint16_equal(QEMUFile *f, void *pv, size_t size) |
| 1298 | { |
| 1299 | uint16_t *v = pv; |
| 1300 | uint16_t v2; |
| 1301 | qemu_get_be16s(f, &v2); |
| 1302 | |
| 1303 | if (*v == v2) |
| 1304 | return 0; |
| 1305 | return -EINVAL; |
| 1306 | } |
| 1307 | |
| 1308 | const VMStateInfo vmstate_info_uint16_equal = { |
| 1309 | .name = "uint16 equal", |
| 1310 | .get = get_uint16_equal, |
| 1311 | .put = put_uint16, |
| 1312 | }; |
| 1313 | |
David Gibson | 213945e | 2013-03-12 14:06:02 +1100 | [diff] [blame] | 1314 | /* floating point */ |
| 1315 | |
| 1316 | static int get_float64(QEMUFile *f, void *pv, size_t size) |
| 1317 | { |
| 1318 | float64 *v = pv; |
| 1319 | |
| 1320 | *v = make_float64(qemu_get_be64(f)); |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | static void put_float64(QEMUFile *f, void *pv, size_t size) |
| 1325 | { |
| 1326 | uint64_t *v = pv; |
| 1327 | |
| 1328 | qemu_put_be64(f, float64_val(*v)); |
| 1329 | } |
| 1330 | |
| 1331 | const VMStateInfo vmstate_info_float64 = { |
| 1332 | .name = "float64", |
| 1333 | .get = get_float64, |
| 1334 | .put = put_float64, |
| 1335 | }; |
| 1336 | |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1337 | /* timers */ |
| 1338 | |
| 1339 | static int get_timer(QEMUFile *f, void *pv, size_t size) |
| 1340 | { |
| 1341 | QEMUTimer *v = pv; |
| 1342 | qemu_get_timer(f, v); |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1346 | static void put_timer(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1347 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1348 | QEMUTimer *v = pv; |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1349 | qemu_put_timer(f, v); |
| 1350 | } |
| 1351 | |
| 1352 | const VMStateInfo vmstate_info_timer = { |
| 1353 | .name = "timer", |
| 1354 | .get = get_timer, |
| 1355 | .put = put_timer, |
| 1356 | }; |
| 1357 | |
Juan Quintela | 6f67c50 | 2009-08-20 19:42:35 +0200 | [diff] [blame] | 1358 | /* uint8_t buffers */ |
| 1359 | |
| 1360 | static int get_buffer(QEMUFile *f, void *pv, size_t size) |
| 1361 | { |
| 1362 | uint8_t *v = pv; |
| 1363 | qemu_get_buffer(f, v, size); |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1367 | static void put_buffer(QEMUFile *f, void *pv, size_t size) |
Juan Quintela | 6f67c50 | 2009-08-20 19:42:35 +0200 | [diff] [blame] | 1368 | { |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1369 | uint8_t *v = pv; |
Juan Quintela | 6f67c50 | 2009-08-20 19:42:35 +0200 | [diff] [blame] | 1370 | qemu_put_buffer(f, v, size); |
| 1371 | } |
| 1372 | |
| 1373 | const VMStateInfo vmstate_info_buffer = { |
| 1374 | .name = "buffer", |
| 1375 | .get = get_buffer, |
| 1376 | .put = put_buffer, |
| 1377 | }; |
| 1378 | |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 1379 | /* unused buffers: space that was used for some fields that are |
Stefan Weil | 61cc870 | 2011-04-13 22:45:22 +0200 | [diff] [blame] | 1380 | not useful anymore */ |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 1381 | |
| 1382 | static int get_unused_buffer(QEMUFile *f, void *pv, size_t size) |
| 1383 | { |
Jan Kiszka | 21174c3 | 2009-12-02 12:36:35 +0100 | [diff] [blame] | 1384 | uint8_t buf[1024]; |
| 1385 | int block_len; |
| 1386 | |
| 1387 | while (size > 0) { |
| 1388 | block_len = MIN(sizeof(buf), size); |
| 1389 | size -= block_len; |
| 1390 | qemu_get_buffer(f, buf, block_len); |
| 1391 | } |
| 1392 | return 0; |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | static void put_unused_buffer(QEMUFile *f, void *pv, size_t size) |
| 1396 | { |
Jan Kiszka | 21174c3 | 2009-12-02 12:36:35 +0100 | [diff] [blame] | 1397 | static const uint8_t buf[1024]; |
| 1398 | int block_len; |
| 1399 | |
| 1400 | while (size > 0) { |
| 1401 | block_len = MIN(sizeof(buf), size); |
| 1402 | size -= block_len; |
| 1403 | qemu_put_buffer(f, buf, block_len); |
| 1404 | } |
Juan Quintela | 76507c7 | 2009-10-19 15:46:28 +0200 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | const VMStateInfo vmstate_info_unused_buffer = { |
| 1408 | .name = "unused_buffer", |
| 1409 | .get = get_unused_buffer, |
| 1410 | .put = put_unused_buffer, |
| 1411 | }; |
| 1412 | |
Peter Maydell | 08e99e2 | 2012-10-30 07:45:12 +0000 | [diff] [blame] | 1413 | /* bitmaps (as defined by bitmap.h). Note that size here is the size |
| 1414 | * of the bitmap in bits. The on-the-wire format of a bitmap is 64 |
| 1415 | * bit words with the bits in big endian order. The in-memory format |
| 1416 | * is an array of 'unsigned long', which may be either 32 or 64 bits. |
| 1417 | */ |
| 1418 | /* This is the number of 64 bit words sent over the wire */ |
| 1419 | #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64) |
| 1420 | static int get_bitmap(QEMUFile *f, void *pv, size_t size) |
| 1421 | { |
| 1422 | unsigned long *bmp = pv; |
| 1423 | int i, idx = 0; |
| 1424 | for (i = 0; i < BITS_TO_U64S(size); i++) { |
| 1425 | uint64_t w = qemu_get_be64(f); |
| 1426 | bmp[idx++] = w; |
| 1427 | if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) { |
| 1428 | bmp[idx++] = w >> 32; |
| 1429 | } |
| 1430 | } |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | static void put_bitmap(QEMUFile *f, void *pv, size_t size) |
| 1435 | { |
| 1436 | unsigned long *bmp = pv; |
| 1437 | int i, idx = 0; |
| 1438 | for (i = 0; i < BITS_TO_U64S(size); i++) { |
| 1439 | uint64_t w = bmp[idx++]; |
| 1440 | if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) { |
| 1441 | w |= ((uint64_t)bmp[idx++]) << 32; |
| 1442 | } |
| 1443 | qemu_put_be64(f, w); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | const VMStateInfo vmstate_info_bitmap = { |
| 1448 | .name = "bitmap", |
| 1449 | .get = get_bitmap, |
| 1450 | .put = put_bitmap, |
| 1451 | }; |
| 1452 | |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1453 | typedef struct CompatEntry { |
| 1454 | char idstr[256]; |
| 1455 | int instance_id; |
| 1456 | } CompatEntry; |
| 1457 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1458 | typedef struct SaveStateEntry { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1459 | QTAILQ_ENTRY(SaveStateEntry) entry; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1460 | char idstr[256]; |
| 1461 | int instance_id; |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1462 | int alias_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1463 | int version_id; |
| 1464 | int section_id; |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1465 | SaveVMHandlers *ops; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1466 | const VMStateDescription *vmsd; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1467 | void *opaque; |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1468 | CompatEntry *compat; |
Cam Macdonell | 2431296 | 2010-07-26 18:11:00 -0600 | [diff] [blame] | 1469 | int no_migrate; |
Stefano Stabellini | a7ae835 | 2012-01-25 12:24:51 +0000 | [diff] [blame] | 1470 | int is_ram; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1471 | } SaveStateEntry; |
| 1472 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1473 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1474 | static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers = |
| 1475 | QTAILQ_HEAD_INITIALIZER(savevm_handlers); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1476 | static int global_section_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1477 | |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1478 | static int calculate_new_instance_id(const char *idstr) |
| 1479 | { |
| 1480 | SaveStateEntry *se; |
| 1481 | int instance_id = 0; |
| 1482 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1483 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1484 | if (strcmp(idstr, se->idstr) == 0 |
| 1485 | && instance_id <= se->instance_id) { |
| 1486 | instance_id = se->instance_id + 1; |
| 1487 | } |
| 1488 | } |
| 1489 | return instance_id; |
| 1490 | } |
| 1491 | |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1492 | static int calculate_compat_instance_id(const char *idstr) |
| 1493 | { |
| 1494 | SaveStateEntry *se; |
| 1495 | int instance_id = 0; |
| 1496 | |
| 1497 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 1498 | if (!se->compat) |
| 1499 | continue; |
| 1500 | |
| 1501 | if (strcmp(idstr, se->compat->idstr) == 0 |
| 1502 | && instance_id <= se->compat->instance_id) { |
| 1503 | instance_id = se->compat->instance_id + 1; |
| 1504 | } |
| 1505 | } |
| 1506 | return instance_id; |
| 1507 | } |
| 1508 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1509 | /* TODO: Individual devices generally have very little idea about the rest |
| 1510 | of the system, so instance_id should be removed/replaced. |
| 1511 | Meanwhile pass -1 as instance_id if you do not already have a clearly |
| 1512 | distinguishing id for all instances of your device class. */ |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1513 | int register_savevm_live(DeviceState *dev, |
| 1514 | const char *idstr, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1515 | int instance_id, |
| 1516 | int version_id, |
Juan Quintela | 7908c78 | 2012-06-26 18:46:10 +0200 | [diff] [blame] | 1517 | SaveVMHandlers *ops, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1518 | void *opaque) |
| 1519 | { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1520 | SaveStateEntry *se; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1521 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1522 | se = g_malloc0(sizeof(SaveStateEntry)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1523 | se->version_id = version_id; |
| 1524 | se->section_id = global_section_id++; |
Juan Quintela | 7908c78 | 2012-06-26 18:46:10 +0200 | [diff] [blame] | 1525 | se->ops = ops; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1526 | se->opaque = opaque; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1527 | se->vmsd = NULL; |
Cam Macdonell | 2431296 | 2010-07-26 18:11:00 -0600 | [diff] [blame] | 1528 | se->no_migrate = 0; |
Stefano Stabellini | a7ae835 | 2012-01-25 12:24:51 +0000 | [diff] [blame] | 1529 | /* if this is a live_savem then set is_ram */ |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 1530 | if (ops->save_live_setup != NULL) { |
Stefano Stabellini | a7ae835 | 2012-01-25 12:24:51 +0000 | [diff] [blame] | 1531 | se->is_ram = 1; |
| 1532 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1533 | |
Anthony Liguori | 09e5ab6 | 2012-02-03 12:28:43 -0600 | [diff] [blame] | 1534 | if (dev) { |
| 1535 | char *id = qdev_get_dev_path(dev); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1536 | if (id) { |
| 1537 | pstrcpy(se->idstr, sizeof(se->idstr), id); |
| 1538 | pstrcat(se->idstr, sizeof(se->idstr), "/"); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1539 | g_free(id); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1540 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1541 | se->compat = g_malloc0(sizeof(CompatEntry)); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1542 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr); |
| 1543 | se->compat->instance_id = instance_id == -1 ? |
| 1544 | calculate_compat_instance_id(idstr) : instance_id; |
| 1545 | instance_id = -1; |
| 1546 | } |
| 1547 | } |
| 1548 | pstrcat(se->idstr, sizeof(se->idstr), idstr); |
| 1549 | |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1550 | if (instance_id == -1) { |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1551 | se->instance_id = calculate_new_instance_id(se->idstr); |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1552 | } else { |
| 1553 | se->instance_id = instance_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1554 | } |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1555 | assert(!se->compat || se->instance_id == 0); |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1556 | /* add at the end of list */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1557 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1558 | return 0; |
| 1559 | } |
| 1560 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1561 | int register_savevm(DeviceState *dev, |
| 1562 | const char *idstr, |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1563 | int instance_id, |
| 1564 | int version_id, |
| 1565 | SaveStateHandler *save_state, |
| 1566 | LoadStateHandler *load_state, |
| 1567 | void *opaque) |
| 1568 | { |
Juan Quintela | 7908c78 | 2012-06-26 18:46:10 +0200 | [diff] [blame] | 1569 | SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers)); |
| 1570 | ops->save_state = save_state; |
| 1571 | ops->load_state = load_state; |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1572 | return register_savevm_live(dev, idstr, instance_id, version_id, |
Juan Quintela | 7908c78 | 2012-06-26 18:46:10 +0200 | [diff] [blame] | 1573 | ops, opaque); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1576 | void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque) |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1577 | { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1578 | SaveStateEntry *se, *new_se; |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1579 | char id[256] = ""; |
| 1580 | |
Anthony Liguori | 09e5ab6 | 2012-02-03 12:28:43 -0600 | [diff] [blame] | 1581 | if (dev) { |
| 1582 | char *path = qdev_get_dev_path(dev); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1583 | if (path) { |
| 1584 | pstrcpy(id, sizeof(id), path); |
| 1585 | pstrcat(id, sizeof(id), "/"); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1586 | g_free(path); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | pstrcat(id, sizeof(id), idstr); |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1590 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1591 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1592 | if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1593 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
Alex Williamson | 69e58af | 2010-07-21 08:35:31 -0600 | [diff] [blame] | 1594 | if (se->compat) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1595 | g_free(se->compat); |
Alex Williamson | 69e58af | 2010-07-21 08:35:31 -0600 | [diff] [blame] | 1596 | } |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1597 | g_free(se->ops); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1598 | g_free(se); |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1599 | } |
aliguori | 41bd13a | 2009-04-17 17:10:59 +0000 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1603 | int vmstate_register_with_alias_id(DeviceState *dev, int instance_id, |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1604 | const VMStateDescription *vmsd, |
| 1605 | void *opaque, int alias_id, |
| 1606 | int required_for_version) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1607 | { |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1608 | SaveStateEntry *se; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1609 | |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1610 | /* If this triggers, alias support can be dropped for the vmsd. */ |
| 1611 | assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); |
| 1612 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1613 | se = g_malloc0(sizeof(SaveStateEntry)); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1614 | se->version_id = vmsd->version_id; |
| 1615 | se->section_id = global_section_id++; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1616 | se->opaque = opaque; |
| 1617 | se->vmsd = vmsd; |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 1618 | se->alias_id = alias_id; |
Gerd Hoffmann | 2837c8e | 2011-07-08 10:44:35 +0200 | [diff] [blame] | 1619 | se->no_migrate = vmsd->unmigratable; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1620 | |
Anthony Liguori | 09e5ab6 | 2012-02-03 12:28:43 -0600 | [diff] [blame] | 1621 | if (dev) { |
| 1622 | char *id = qdev_get_dev_path(dev); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1623 | if (id) { |
| 1624 | pstrcpy(se->idstr, sizeof(se->idstr), id); |
| 1625 | pstrcat(se->idstr, sizeof(se->idstr), "/"); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1626 | g_free(id); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1627 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1628 | se->compat = g_malloc0(sizeof(CompatEntry)); |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1629 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); |
| 1630 | se->compat->instance_id = instance_id == -1 ? |
| 1631 | calculate_compat_instance_id(vmsd->name) : instance_id; |
| 1632 | instance_id = -1; |
| 1633 | } |
| 1634 | } |
| 1635 | pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); |
| 1636 | |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1637 | if (instance_id == -1) { |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1638 | se->instance_id = calculate_new_instance_id(se->idstr); |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1639 | } else { |
| 1640 | se->instance_id = instance_id; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1641 | } |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 1642 | assert(!se->compat || se->instance_id == 0); |
Juan Quintela | 8718e99 | 2009-09-01 02:12:31 +0200 | [diff] [blame] | 1643 | /* add at the end of list */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1644 | QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1645 | return 0; |
| 1646 | } |
| 1647 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 1648 | void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd, |
| 1649 | void *opaque) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1650 | { |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1651 | SaveStateEntry *se, *new_se; |
| 1652 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1653 | QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) { |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1654 | if (se->vmsd == vmsd && se->opaque == opaque) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1655 | QTAILQ_REMOVE(&savevm_handlers, se, entry); |
Alex Williamson | 69e58af | 2010-07-21 08:35:31 -0600 | [diff] [blame] | 1656 | if (se->compat) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1657 | g_free(se->compat); |
Alex Williamson | 69e58af | 2010-07-21 08:35:31 -0600 | [diff] [blame] | 1658 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1659 | g_free(se); |
Juan Quintela | 1eb7538 | 2009-09-10 03:04:29 +0200 | [diff] [blame] | 1660 | } |
| 1661 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1662 | } |
| 1663 | |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1664 | static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, |
| 1665 | void *opaque); |
| 1666 | static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd, |
| 1667 | void *opaque); |
| 1668 | |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1669 | int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd, |
| 1670 | void *opaque, int version_id) |
| 1671 | { |
| 1672 | VMStateField *field = vmsd->fields; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1673 | int ret; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1674 | |
| 1675 | if (version_id > vmsd->version_id) { |
| 1676 | return -EINVAL; |
| 1677 | } |
| 1678 | if (version_id < vmsd->minimum_version_id_old) { |
| 1679 | return -EINVAL; |
| 1680 | } |
| 1681 | if (version_id < vmsd->minimum_version_id) { |
| 1682 | return vmsd->load_state_old(f, opaque, version_id); |
| 1683 | } |
Juan Quintela | fd4d52d | 2009-09-10 03:04:31 +0200 | [diff] [blame] | 1684 | if (vmsd->pre_load) { |
| 1685 | int ret = vmsd->pre_load(opaque); |
| 1686 | if (ret) |
| 1687 | return ret; |
| 1688 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1689 | while(field->name) { |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1690 | if ((field->field_exists && |
| 1691 | field->field_exists(opaque, version_id)) || |
| 1692 | (!field->field_exists && |
| 1693 | field->version_id <= version_id)) { |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1694 | void *base_addr = opaque + field->offset; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1695 | int i, n_elems = 1; |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1696 | int size = field->size; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1697 | |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1698 | if (field->flags & VMS_VBUFFER) { |
| 1699 | size = *(int32_t *)(opaque+field->size_offset); |
Juan Quintela | 33599e2 | 2009-12-02 12:36:43 +0100 | [diff] [blame] | 1700 | if (field->flags & VMS_MULTIPLY) { |
| 1701 | size *= field->size; |
| 1702 | } |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1703 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1704 | if (field->flags & VMS_ARRAY) { |
| 1705 | n_elems = field->num; |
Juan Quintela | d669828 | 2009-10-16 11:27:17 +0200 | [diff] [blame] | 1706 | } else if (field->flags & VMS_VARRAY_INT32) { |
| 1707 | n_elems = *(int32_t *)(opaque+field->num_offset); |
Juan Quintela | a624b08 | 2011-03-10 12:33:50 +0100 | [diff] [blame] | 1708 | } else if (field->flags & VMS_VARRAY_UINT32) { |
| 1709 | n_elems = *(uint32_t *)(opaque+field->num_offset); |
Juan Quintela | bdb4941 | 2009-10-16 11:35:18 +0200 | [diff] [blame] | 1710 | } else if (field->flags & VMS_VARRAY_UINT16) { |
| 1711 | n_elems = *(uint16_t *)(opaque+field->num_offset); |
Juan Quintela | 82fa39b | 2011-03-10 12:33:49 +0100 | [diff] [blame] | 1712 | } else if (field->flags & VMS_VARRAY_UINT8) { |
| 1713 | n_elems = *(uint8_t *)(opaque+field->num_offset); |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1714 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1715 | if (field->flags & VMS_POINTER) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1716 | base_addr = *(void **)base_addr + field->start; |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1717 | } |
| 1718 | for (i = 0; i < n_elems; i++) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1719 | void *addr = base_addr + size * i; |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1720 | |
Juan Quintela | 19df438 | 2009-09-29 22:48:41 +0200 | [diff] [blame] | 1721 | if (field->flags & VMS_ARRAY_OF_POINTER) { |
| 1722 | addr = *(void **)addr; |
| 1723 | } |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1724 | if (field->flags & VMS_STRUCT) { |
Juan Quintela | fa3aad2 | 2009-08-28 15:28:25 +0200 | [diff] [blame] | 1725 | ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id); |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1726 | } else { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1727 | ret = field->info->get(f, addr, size); |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1728 | |
| 1729 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1730 | if (ret < 0) { |
| 1731 | return ret; |
| 1732 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1733 | } |
| 1734 | } |
| 1735 | field++; |
| 1736 | } |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1737 | ret = vmstate_subsection_load(f, vmsd, opaque); |
| 1738 | if (ret != 0) { |
| 1739 | return ret; |
| 1740 | } |
Juan Quintela | 752ff2f | 2009-09-10 03:04:30 +0200 | [diff] [blame] | 1741 | if (vmsd->post_load) { |
Juan Quintela | e59fb37 | 2009-09-29 22:48:21 +0200 | [diff] [blame] | 1742 | return vmsd->post_load(opaque, version_id); |
Juan Quintela | 752ff2f | 2009-09-10 03:04:30 +0200 | [diff] [blame] | 1743 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, |
Juan Quintela | 84e2e3e | 2009-09-29 22:48:20 +0200 | [diff] [blame] | 1748 | void *opaque) |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1749 | { |
| 1750 | VMStateField *field = vmsd->fields; |
| 1751 | |
Juan Quintela | 8fb0791 | 2009-09-10 03:04:32 +0200 | [diff] [blame] | 1752 | if (vmsd->pre_save) { |
| 1753 | vmsd->pre_save(opaque); |
| 1754 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1755 | while(field->name) { |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1756 | if (!field->field_exists || |
| 1757 | field->field_exists(opaque, vmsd->version_id)) { |
| 1758 | void *base_addr = opaque + field->offset; |
| 1759 | int i, n_elems = 1; |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1760 | int size = field->size; |
Juan Quintela | dde0463 | 2009-08-20 19:42:26 +0200 | [diff] [blame] | 1761 | |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1762 | if (field->flags & VMS_VBUFFER) { |
| 1763 | size = *(int32_t *)(opaque+field->size_offset); |
Juan Quintela | 33599e2 | 2009-12-02 12:36:43 +0100 | [diff] [blame] | 1764 | if (field->flags & VMS_MULTIPLY) { |
| 1765 | size *= field->size; |
| 1766 | } |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1767 | } |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1768 | if (field->flags & VMS_ARRAY) { |
| 1769 | n_elems = field->num; |
Juan Quintela | d669828 | 2009-10-16 11:27:17 +0200 | [diff] [blame] | 1770 | } else if (field->flags & VMS_VARRAY_INT32) { |
| 1771 | n_elems = *(int32_t *)(opaque+field->num_offset); |
Amos Kong | 1329d18 | 2012-03-13 14:05:36 +0800 | [diff] [blame] | 1772 | } else if (field->flags & VMS_VARRAY_UINT32) { |
| 1773 | n_elems = *(uint32_t *)(opaque+field->num_offset); |
Juan Quintela | bdb4941 | 2009-10-16 11:35:18 +0200 | [diff] [blame] | 1774 | } else if (field->flags & VMS_VARRAY_UINT16) { |
| 1775 | n_elems = *(uint16_t *)(opaque+field->num_offset); |
Juan Quintela | b784421 | 2011-03-15 15:53:25 +0100 | [diff] [blame] | 1776 | } else if (field->flags & VMS_VARRAY_UINT8) { |
| 1777 | n_elems = *(uint8_t *)(opaque+field->num_offset); |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1778 | } |
| 1779 | if (field->flags & VMS_POINTER) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1780 | base_addr = *(void **)base_addr + field->start; |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1781 | } |
| 1782 | for (i = 0; i < n_elems; i++) { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1783 | void *addr = base_addr + size * i; |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1784 | |
Juan Quintela | 8595387 | 2009-12-02 12:36:37 +0100 | [diff] [blame] | 1785 | if (field->flags & VMS_ARRAY_OF_POINTER) { |
| 1786 | addr = *(void **)addr; |
| 1787 | } |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1788 | if (field->flags & VMS_STRUCT) { |
| 1789 | vmstate_save_state(f, field->vmsd, addr); |
| 1790 | } else { |
Juan Quintela | e61a1e0 | 2009-12-02 12:36:38 +0100 | [diff] [blame] | 1791 | field->info->put(f, addr, size); |
Juan Quintela | f11f6a5 | 2009-09-29 22:49:07 +0200 | [diff] [blame] | 1792 | } |
Juan Quintela | ec245e2 | 2009-08-20 19:42:29 +0200 | [diff] [blame] | 1793 | } |
Juan Quintela | f752a6a | 2009-08-20 19:42:27 +0200 | [diff] [blame] | 1794 | } |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1795 | field++; |
| 1796 | } |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1797 | vmstate_subsection_save(f, vmsd, opaque); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1798 | } |
| 1799 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1800 | static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id) |
| 1801 | { |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1802 | if (!se->vmsd) { /* Old style */ |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1803 | return se->ops->load_state(f, se->opaque, version_id); |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1804 | } |
| 1805 | return vmstate_load_state(f, se->vmsd, se->opaque, version_id); |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1806 | } |
| 1807 | |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1808 | static void vmstate_save(QEMUFile *f, SaveStateEntry *se) |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1809 | { |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1810 | if (!se->vmsd) { /* Old style */ |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1811 | se->ops->save_state(f, se->opaque); |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1812 | return; |
Juan Quintela | 9ed7d6a | 2009-08-20 19:42:25 +0200 | [diff] [blame] | 1813 | } |
| 1814 | vmstate_save_state(f,se->vmsd, se->opaque); |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 1815 | } |
| 1816 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1817 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
| 1818 | #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002 |
| 1819 | #define QEMU_VM_FILE_VERSION 0x00000003 |
| 1820 | |
| 1821 | #define QEMU_VM_EOF 0x00 |
| 1822 | #define QEMU_VM_SECTION_START 0x01 |
| 1823 | #define QEMU_VM_SECTION_PART 0x02 |
| 1824 | #define QEMU_VM_SECTION_END 0x03 |
| 1825 | #define QEMU_VM_SECTION_FULL 0x04 |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 1826 | #define QEMU_VM_SUBSECTION 0x05 |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1827 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 1828 | bool qemu_savevm_state_blocked(Error **errp) |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1829 | { |
| 1830 | SaveStateEntry *se; |
| 1831 | |
| 1832 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 1833 | if (se->no_migrate) { |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 1834 | error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr); |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1835 | return true; |
| 1836 | } |
| 1837 | } |
| 1838 | return false; |
| 1839 | } |
| 1840 | |
Paolo Bonzini | 47c8c17 | 2013-02-22 17:36:13 +0100 | [diff] [blame] | 1841 | void qemu_savevm_state_begin(QEMUFile *f, |
| 1842 | const MigrationParams *params) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1843 | { |
| 1844 | SaveStateEntry *se; |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1845 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1846 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1847 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1848 | if (!se->ops || !se->ops->set_params) { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1849 | continue; |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 1850 | } |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1851 | se->ops->set_params(params, se->opaque); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1854 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 1855 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
| 1856 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1857 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1858 | int len; |
| 1859 | |
Juan Quintela | d1315aa | 2012-06-28 15:11:57 +0200 | [diff] [blame] | 1860 | if (!se->ops || !se->ops->save_live_setup) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1861 | continue; |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1862 | } |
Juan Quintela | 6bd6878 | 2012-06-27 10:59:15 +0200 | [diff] [blame] | 1863 | if (se->ops && se->ops->is_active) { |
| 1864 | if (!se->ops->is_active(se->opaque)) { |
| 1865 | continue; |
| 1866 | } |
| 1867 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1868 | /* Section type */ |
| 1869 | qemu_put_byte(f, QEMU_VM_SECTION_START); |
| 1870 | qemu_put_be32(f, se->section_id); |
| 1871 | |
| 1872 | /* ID string */ |
| 1873 | len = strlen(se->idstr); |
| 1874 | qemu_put_byte(f, len); |
| 1875 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
| 1876 | |
| 1877 | qemu_put_be32(f, se->instance_id); |
| 1878 | qemu_put_be32(f, se->version_id); |
| 1879 | |
Juan Quintela | d1315aa | 2012-06-28 15:11:57 +0200 | [diff] [blame] | 1880 | ret = se->ops->save_live_setup(f, se->opaque); |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1881 | if (ret < 0) { |
Paolo Bonzini | 47c8c17 | 2013-02-22 17:36:13 +0100 | [diff] [blame] | 1882 | qemu_file_set_error(f, ret); |
| 1883 | break; |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1884 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1885 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1888 | /* |
Dong Xu Wang | 07f3507 | 2011-11-22 18:06:26 +0800 | [diff] [blame] | 1889 | * this function has three return values: |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1890 | * negative: there was one error, and we have -errno. |
| 1891 | * 0 : We haven't finished, caller have to go again |
| 1892 | * 1 : We have finished, we can go to complete phase |
| 1893 | */ |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 1894 | int qemu_savevm_state_iterate(QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1895 | { |
| 1896 | SaveStateEntry *se; |
| 1897 | int ret = 1; |
| 1898 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1899 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 1900 | if (!se->ops || !se->ops->save_live_iterate) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1901 | continue; |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1902 | } |
Juan Quintela | 6bd6878 | 2012-06-27 10:59:15 +0200 | [diff] [blame] | 1903 | if (se->ops && se->ops->is_active) { |
| 1904 | if (!se->ops->is_active(se->opaque)) { |
| 1905 | continue; |
| 1906 | } |
| 1907 | } |
Juan Quintela | aac844e | 2012-05-22 00:38:26 +0200 | [diff] [blame] | 1908 | if (qemu_file_rate_limit(f)) { |
| 1909 | return 0; |
| 1910 | } |
Juan Quintela | 517a13c | 2012-05-21 23:46:44 +0200 | [diff] [blame] | 1911 | trace_savevm_section_start(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1912 | /* Section type */ |
| 1913 | qemu_put_byte(f, QEMU_VM_SECTION_PART); |
| 1914 | qemu_put_be32(f, se->section_id); |
| 1915 | |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 1916 | ret = se->ops->save_live_iterate(f, se->opaque); |
Juan Quintela | 517a13c | 2012-05-21 23:46:44 +0200 | [diff] [blame] | 1917 | trace_savevm_section_end(se->section_id); |
| 1918 | |
Paolo Bonzini | 47c8c17 | 2013-02-22 17:36:13 +0100 | [diff] [blame] | 1919 | if (ret < 0) { |
| 1920 | qemu_file_set_error(f, ret); |
| 1921 | } |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1922 | if (ret <= 0) { |
Jan Kiszka | 90697be | 2009-12-01 15:19:55 +0100 | [diff] [blame] | 1923 | /* Do not proceed to the next vmstate before this one reported |
| 1924 | completion of the current stage. This serializes the migration |
| 1925 | and reduces the probability that a faster changing state is |
| 1926 | synchronized over and over again. */ |
| 1927 | break; |
| 1928 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1929 | } |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 1930 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Paolo Bonzini | 47c8c17 | 2013-02-22 17:36:13 +0100 | [diff] [blame] | 1933 | void qemu_savevm_state_complete(QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1934 | { |
| 1935 | SaveStateEntry *se; |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1936 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1937 | |
Jan Kiszka | ea375f9 | 2010-03-01 19:10:30 +0100 | [diff] [blame] | 1938 | cpu_synchronize_all_states(); |
| 1939 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1940 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 1941 | if (!se->ops || !se->ops->save_live_complete) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1942 | continue; |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1943 | } |
Juan Quintela | 6bd6878 | 2012-06-27 10:59:15 +0200 | [diff] [blame] | 1944 | if (se->ops && se->ops->is_active) { |
| 1945 | if (!se->ops->is_active(se->opaque)) { |
| 1946 | continue; |
| 1947 | } |
| 1948 | } |
Juan Quintela | 517a13c | 2012-05-21 23:46:44 +0200 | [diff] [blame] | 1949 | trace_savevm_section_start(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1950 | /* Section type */ |
| 1951 | qemu_put_byte(f, QEMU_VM_SECTION_END); |
| 1952 | qemu_put_be32(f, se->section_id); |
| 1953 | |
Juan Quintela | 16310a3 | 2012-06-28 15:31:37 +0200 | [diff] [blame] | 1954 | ret = se->ops->save_live_complete(f, se->opaque); |
Juan Quintela | 517a13c | 2012-05-21 23:46:44 +0200 | [diff] [blame] | 1955 | trace_savevm_section_end(se->section_id); |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1956 | if (ret < 0) { |
Paolo Bonzini | 47c8c17 | 2013-02-22 17:36:13 +0100 | [diff] [blame] | 1957 | qemu_file_set_error(f, ret); |
| 1958 | return; |
Juan Quintela | 2975725 | 2011-10-19 15:22:18 +0200 | [diff] [blame] | 1959 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1962 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1963 | int len; |
| 1964 | |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1965 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1966 | continue; |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 1967 | } |
Juan Quintela | 517a13c | 2012-05-21 23:46:44 +0200 | [diff] [blame] | 1968 | trace_savevm_section_start(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1969 | /* Section type */ |
| 1970 | qemu_put_byte(f, QEMU_VM_SECTION_FULL); |
| 1971 | qemu_put_be32(f, se->section_id); |
| 1972 | |
| 1973 | /* ID string */ |
| 1974 | len = strlen(se->idstr); |
| 1975 | qemu_put_byte(f, len); |
| 1976 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
| 1977 | |
| 1978 | qemu_put_be32(f, se->instance_id); |
| 1979 | qemu_put_be32(f, se->version_id); |
| 1980 | |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 1981 | vmstate_save(f, se); |
Juan Quintela | 517a13c | 2012-05-21 23:46:44 +0200 | [diff] [blame] | 1982 | trace_savevm_section_end(se->section_id); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | qemu_put_byte(f, QEMU_VM_EOF); |
Paolo Bonzini | edaae61 | 2013-02-22 17:36:29 +0100 | [diff] [blame] | 1986 | qemu_fflush(f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Juan Quintela | e4ed154 | 2012-09-21 11:18:18 +0200 | [diff] [blame] | 1989 | uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size) |
| 1990 | { |
| 1991 | SaveStateEntry *se; |
| 1992 | uint64_t ret = 0; |
| 1993 | |
| 1994 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 1995 | if (!se->ops || !se->ops->save_live_pending) { |
| 1996 | continue; |
| 1997 | } |
| 1998 | if (se->ops && se->ops->is_active) { |
| 1999 | if (!se->ops->is_active(se->opaque)) { |
| 2000 | continue; |
| 2001 | } |
| 2002 | } |
| 2003 | ret += se->ops->save_live_pending(f, se->opaque, max_size); |
| 2004 | } |
| 2005 | return ret; |
| 2006 | } |
| 2007 | |
Juan Quintela | 6522773 | 2013-01-14 14:14:42 +0100 | [diff] [blame] | 2008 | void qemu_savevm_state_cancel(void) |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 2009 | { |
| 2010 | SaveStateEntry *se; |
| 2011 | |
| 2012 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
Juan Quintela | 9b5bfab | 2012-06-26 19:26:41 +0200 | [diff] [blame] | 2013 | if (se->ops && se->ops->cancel) { |
| 2014 | se->ops->cancel(se->opaque); |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 2019 | static int qemu_savevm_state(QEMUFile *f) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2020 | { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2021 | int ret; |
Isaku Yamahata | 6607ae2 | 2012-06-19 18:43:09 +0300 | [diff] [blame] | 2022 | MigrationParams params = { |
| 2023 | .blk = 0, |
| 2024 | .shared = 0 |
| 2025 | }; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2026 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 2027 | if (qemu_savevm_state_blocked(NULL)) { |
Paolo Bonzini | 04943eb | 2013-02-22 17:36:10 +0100 | [diff] [blame] | 2028 | return -EINVAL; |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
Paolo Bonzini | 9b09503 | 2013-02-22 17:36:28 +0100 | [diff] [blame] | 2031 | qemu_mutex_unlock_iothread(); |
Paolo Bonzini | 47c8c17 | 2013-02-22 17:36:13 +0100 | [diff] [blame] | 2032 | qemu_savevm_state_begin(f, ¶ms); |
Paolo Bonzini | 9b09503 | 2013-02-22 17:36:28 +0100 | [diff] [blame] | 2033 | qemu_mutex_lock_iothread(); |
| 2034 | |
Paolo Bonzini | 47c8c17 | 2013-02-22 17:36:13 +0100 | [diff] [blame] | 2035 | while (qemu_file_get_error(f) == 0) { |
| 2036 | if (qemu_savevm_state_iterate(f) > 0) { |
| 2037 | break; |
| 2038 | } |
| 2039 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2040 | |
Paolo Bonzini | 47c8c17 | 2013-02-22 17:36:13 +0100 | [diff] [blame] | 2041 | ret = qemu_file_get_error(f); |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 2042 | if (ret == 0) { |
Paolo Bonzini | 47c8c17 | 2013-02-22 17:36:13 +0100 | [diff] [blame] | 2043 | qemu_savevm_state_complete(f); |
Juan Quintela | 624b9cc | 2011-10-05 01:02:52 +0200 | [diff] [blame] | 2044 | ret = qemu_file_get_error(f); |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 2045 | } |
Paolo Bonzini | 04943eb | 2013-02-22 17:36:10 +0100 | [diff] [blame] | 2046 | if (ret != 0) { |
| 2047 | qemu_savevm_state_cancel(); |
| 2048 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2049 | return ret; |
| 2050 | } |
| 2051 | |
Stefano Stabellini | a7ae835 | 2012-01-25 12:24:51 +0000 | [diff] [blame] | 2052 | static int qemu_save_device_state(QEMUFile *f) |
| 2053 | { |
| 2054 | SaveStateEntry *se; |
| 2055 | |
| 2056 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 2057 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
| 2058 | |
| 2059 | cpu_synchronize_all_states(); |
| 2060 | |
| 2061 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
| 2062 | int len; |
| 2063 | |
| 2064 | if (se->is_ram) { |
| 2065 | continue; |
| 2066 | } |
Juan Quintela | 22ea40f | 2012-06-26 17:19:10 +0200 | [diff] [blame] | 2067 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
Stefano Stabellini | a7ae835 | 2012-01-25 12:24:51 +0000 | [diff] [blame] | 2068 | continue; |
| 2069 | } |
| 2070 | |
| 2071 | /* Section type */ |
| 2072 | qemu_put_byte(f, QEMU_VM_SECTION_FULL); |
| 2073 | qemu_put_be32(f, se->section_id); |
| 2074 | |
| 2075 | /* ID string */ |
| 2076 | len = strlen(se->idstr); |
| 2077 | qemu_put_byte(f, len); |
| 2078 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
| 2079 | |
| 2080 | qemu_put_be32(f, se->instance_id); |
| 2081 | qemu_put_be32(f, se->version_id); |
| 2082 | |
| 2083 | vmstate_save(f, se); |
| 2084 | } |
| 2085 | |
| 2086 | qemu_put_byte(f, QEMU_VM_EOF); |
| 2087 | |
| 2088 | return qemu_file_get_error(f); |
| 2089 | } |
| 2090 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2091 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
| 2092 | { |
| 2093 | SaveStateEntry *se; |
| 2094 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2095 | QTAILQ_FOREACH(se, &savevm_handlers, entry) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2096 | if (!strcmp(se->idstr, idstr) && |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 2097 | (instance_id == se->instance_id || |
| 2098 | instance_id == se->alias_id)) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2099 | return se; |
Alex Williamson | 7685ee6 | 2010-06-25 11:09:14 -0600 | [diff] [blame] | 2100 | /* Migrating from an older version? */ |
| 2101 | if (strstr(se->idstr, idstr) && se->compat) { |
| 2102 | if (!strcmp(se->compat->idstr, idstr) && |
| 2103 | (instance_id == se->compat->instance_id || |
| 2104 | instance_id == se->alias_id)) |
| 2105 | return se; |
| 2106 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2107 | } |
| 2108 | return NULL; |
| 2109 | } |
| 2110 | |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 2111 | static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr) |
| 2112 | { |
| 2113 | while(sub && sub->needed) { |
| 2114 | if (strcmp(idstr, sub->vmsd->name) == 0) { |
| 2115 | return sub->vmsd; |
| 2116 | } |
| 2117 | sub++; |
| 2118 | } |
| 2119 | return NULL; |
| 2120 | } |
| 2121 | |
| 2122 | static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd, |
| 2123 | void *opaque) |
| 2124 | { |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 2125 | while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) { |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 2126 | char idstr[256]; |
| 2127 | int ret; |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 2128 | uint8_t version_id, len, size; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 2129 | const VMStateDescription *sub_vmsd; |
| 2130 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 2131 | len = qemu_peek_byte(f, 1); |
| 2132 | if (len < strlen(vmsd->name) + 1) { |
| 2133 | /* subsection name has be be "section_name/a" */ |
| 2134 | return 0; |
| 2135 | } |
| 2136 | size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2); |
| 2137 | if (size != len) { |
| 2138 | return 0; |
| 2139 | } |
| 2140 | idstr[size] = 0; |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 2141 | |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 2142 | if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) { |
| 2143 | /* it don't have a valid subsection name */ |
| 2144 | return 0; |
| 2145 | } |
Juan Quintela | 3da9eeb | 2011-09-30 19:46:43 +0200 | [diff] [blame] | 2146 | sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr); |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 2147 | if (sub_vmsd == NULL) { |
| 2148 | return -ENOENT; |
| 2149 | } |
Juan Quintela | c638072 | 2011-10-04 15:28:31 +0200 | [diff] [blame] | 2150 | qemu_file_skip(f, 1); /* subsection */ |
| 2151 | qemu_file_skip(f, 1); /* len */ |
| 2152 | qemu_file_skip(f, len); /* idstr */ |
| 2153 | version_id = qemu_get_be32(f); |
| 2154 | |
Juan Quintela | 811814b | 2010-07-26 21:38:43 +0200 | [diff] [blame] | 2155 | ret = vmstate_load_state(f, sub_vmsd, opaque, version_id); |
| 2156 | if (ret) { |
| 2157 | return ret; |
| 2158 | } |
| 2159 | } |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, |
| 2164 | void *opaque) |
| 2165 | { |
| 2166 | const VMStateSubsection *sub = vmsd->subsections; |
| 2167 | |
| 2168 | while (sub && sub->needed) { |
| 2169 | if (sub->needed(opaque)) { |
| 2170 | const VMStateDescription *vmsd = sub->vmsd; |
| 2171 | uint8_t len; |
| 2172 | |
| 2173 | qemu_put_byte(f, QEMU_VM_SUBSECTION); |
| 2174 | len = strlen(vmsd->name); |
| 2175 | qemu_put_byte(f, len); |
| 2176 | qemu_put_buffer(f, (uint8_t *)vmsd->name, len); |
| 2177 | qemu_put_be32(f, vmsd->version_id); |
| 2178 | vmstate_save_state(f, vmsd, opaque); |
| 2179 | } |
| 2180 | sub++; |
| 2181 | } |
| 2182 | } |
| 2183 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2184 | typedef struct LoadStateEntry { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2185 | QLIST_ENTRY(LoadStateEntry) entry; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2186 | SaveStateEntry *se; |
| 2187 | int section_id; |
| 2188 | int version_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2189 | } LoadStateEntry; |
| 2190 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2191 | int qemu_loadvm_state(QEMUFile *f) |
| 2192 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2193 | QLIST_HEAD(, LoadStateEntry) loadvm_handlers = |
| 2194 | QLIST_HEAD_INITIALIZER(loadvm_handlers); |
Juan Quintela | f4dbb8d | 2009-09-01 02:12:33 +0200 | [diff] [blame] | 2195 | LoadStateEntry *le, *new_le; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2196 | uint8_t section_type; |
| 2197 | unsigned int v; |
| 2198 | int ret; |
| 2199 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 2200 | if (qemu_savevm_state_blocked(NULL)) { |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 2201 | return -EINVAL; |
| 2202 | } |
| 2203 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2204 | v = qemu_get_be32(f); |
| 2205 | if (v != QEMU_VM_FILE_MAGIC) |
| 2206 | return -EINVAL; |
| 2207 | |
| 2208 | v = qemu_get_be32(f); |
Juan Quintela | bbfe140 | 2009-09-10 03:04:24 +0200 | [diff] [blame] | 2209 | if (v == QEMU_VM_FILE_VERSION_COMPAT) { |
| 2210 | fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n"); |
| 2211 | return -ENOTSUP; |
| 2212 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2213 | if (v != QEMU_VM_FILE_VERSION) |
| 2214 | return -ENOTSUP; |
| 2215 | |
| 2216 | while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) { |
| 2217 | uint32_t instance_id, version_id, section_id; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2218 | SaveStateEntry *se; |
| 2219 | char idstr[257]; |
| 2220 | int len; |
| 2221 | |
| 2222 | switch (section_type) { |
| 2223 | case QEMU_VM_SECTION_START: |
| 2224 | case QEMU_VM_SECTION_FULL: |
| 2225 | /* Read section start */ |
| 2226 | section_id = qemu_get_be32(f); |
| 2227 | len = qemu_get_byte(f); |
| 2228 | qemu_get_buffer(f, (uint8_t *)idstr, len); |
| 2229 | idstr[len] = 0; |
| 2230 | instance_id = qemu_get_be32(f); |
| 2231 | version_id = qemu_get_be32(f); |
| 2232 | |
| 2233 | /* Find savevm section */ |
| 2234 | se = find_se(idstr, instance_id); |
| 2235 | if (se == NULL) { |
| 2236 | fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id); |
| 2237 | ret = -EINVAL; |
| 2238 | goto out; |
| 2239 | } |
| 2240 | |
| 2241 | /* Validate version */ |
| 2242 | if (version_id > se->version_id) { |
| 2243 | fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n", |
| 2244 | version_id, idstr, se->version_id); |
| 2245 | ret = -EINVAL; |
| 2246 | goto out; |
| 2247 | } |
| 2248 | |
| 2249 | /* Add entry */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2250 | le = g_malloc0(sizeof(*le)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2251 | |
| 2252 | le->se = se; |
| 2253 | le->section_id = section_id; |
| 2254 | le->version_id = version_id; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2255 | QLIST_INSERT_HEAD(&loadvm_handlers, le, entry); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2256 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 2257 | ret = vmstate_load(f, le->se, le->version_id); |
Juan Quintela | b5a22e4 | 2009-08-20 19:42:23 +0200 | [diff] [blame] | 2258 | if (ret < 0) { |
| 2259 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", |
| 2260 | instance_id, idstr); |
| 2261 | goto out; |
| 2262 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2263 | break; |
| 2264 | case QEMU_VM_SECTION_PART: |
| 2265 | case QEMU_VM_SECTION_END: |
| 2266 | section_id = qemu_get_be32(f); |
| 2267 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2268 | QLIST_FOREACH(le, &loadvm_handlers, entry) { |
Juan Quintela | f4dbb8d | 2009-09-01 02:12:33 +0200 | [diff] [blame] | 2269 | if (le->section_id == section_id) { |
| 2270 | break; |
| 2271 | } |
| 2272 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2273 | if (le == NULL) { |
| 2274 | fprintf(stderr, "Unknown savevm section %d\n", section_id); |
| 2275 | ret = -EINVAL; |
| 2276 | goto out; |
| 2277 | } |
| 2278 | |
Juan Quintela | 4082be4 | 2009-08-20 19:42:24 +0200 | [diff] [blame] | 2279 | ret = vmstate_load(f, le->se, le->version_id); |
Juan Quintela | b5a22e4 | 2009-08-20 19:42:23 +0200 | [diff] [blame] | 2280 | if (ret < 0) { |
| 2281 | fprintf(stderr, "qemu: warning: error while loading state section id %d\n", |
| 2282 | section_id); |
| 2283 | goto out; |
| 2284 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2285 | break; |
| 2286 | default: |
| 2287 | fprintf(stderr, "Unknown savevm section type %d\n", section_type); |
| 2288 | ret = -EINVAL; |
| 2289 | goto out; |
| 2290 | } |
| 2291 | } |
| 2292 | |
Jan Kiszka | ea375f9 | 2010-03-01 19:10:30 +0100 | [diff] [blame] | 2293 | cpu_synchronize_all_post_init(); |
| 2294 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2295 | ret = 0; |
| 2296 | |
| 2297 | out: |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2298 | QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) { |
| 2299 | QLIST_REMOVE(le, entry); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2300 | g_free(le); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Juan Quintela | 42802d4 | 2011-10-05 01:14:46 +0200 | [diff] [blame] | 2303 | if (ret == 0) { |
| 2304 | ret = qemu_file_get_error(f); |
Juan Quintela | 624b9cc | 2011-10-05 01:02:52 +0200 | [diff] [blame] | 2305 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2306 | |
| 2307 | return ret; |
| 2308 | } |
| 2309 | |
Stefan Hajnoczi | 29d7827 | 2013-05-25 11:09:42 +0800 | [diff] [blame] | 2310 | static BlockDriverState *find_vmstate_bs(void) |
| 2311 | { |
| 2312 | BlockDriverState *bs = NULL; |
| 2313 | while ((bs = bdrv_next(bs))) { |
| 2314 | if (bdrv_can_snapshot(bs)) { |
| 2315 | return bs; |
| 2316 | } |
| 2317 | } |
| 2318 | return NULL; |
| 2319 | } |
| 2320 | |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2321 | /* |
| 2322 | * Deletes snapshots of a given name in all opened images. |
| 2323 | */ |
| 2324 | static int del_existing_snapshots(Monitor *mon, const char *name) |
| 2325 | { |
| 2326 | BlockDriverState *bs; |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2327 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
| 2328 | int ret; |
| 2329 | |
Markus Armbruster | dbc1359 | 2010-06-02 18:55:21 +0200 | [diff] [blame] | 2330 | bs = NULL; |
| 2331 | while ((bs = bdrv_next(bs))) { |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2332 | if (bdrv_can_snapshot(bs) && |
| 2333 | bdrv_snapshot_find(bs, snapshot, name) >= 0) |
| 2334 | { |
| 2335 | ret = bdrv_snapshot_delete(bs, name); |
| 2336 | if (ret < 0) { |
| 2337 | monitor_printf(mon, |
| 2338 | "Error while deleting snapshot on '%s'\n", |
| 2339 | bdrv_get_device_name(bs)); |
| 2340 | return -1; |
| 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | return 0; |
| 2346 | } |
| 2347 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2348 | void do_savevm(Monitor *mon, const QDict *qdict) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2349 | { |
| 2350 | BlockDriverState *bs, *bs1; |
| 2351 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2352 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2353 | QEMUFile *f; |
| 2354 | int saved_vm_running; |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 2355 | uint64_t vm_state_size; |
Stefan Weil | 68b891e | 2013-01-07 22:20:27 +0100 | [diff] [blame] | 2356 | qemu_timeval tv; |
Miguel Di Ciurcio Filho | 7d631a1 | 2010-08-04 14:55:49 -0300 | [diff] [blame] | 2357 | struct tm tm; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2358 | const char *name = qdict_get_try_str(qdict, "name"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2359 | |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2360 | /* 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] | 2361 | bs = NULL; |
| 2362 | while ((bs = bdrv_next(bs))) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2363 | |
Markus Armbruster | 07b70bf | 2011-08-03 15:08:11 +0200 | [diff] [blame] | 2364 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2365 | continue; |
| 2366 | } |
| 2367 | |
| 2368 | if (!bdrv_can_snapshot(bs)) { |
| 2369 | monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n", |
| 2370 | bdrv_get_device_name(bs)); |
| 2371 | return; |
| 2372 | } |
| 2373 | } |
| 2374 | |
Stefan Hajnoczi | 29d7827 | 2013-05-25 11:09:42 +0800 | [diff] [blame] | 2375 | bs = find_vmstate_bs(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2376 | if (!bs) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2377 | monitor_printf(mon, "No block device can accept snapshots\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2378 | return; |
| 2379 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2380 | |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 2381 | saved_vm_running = runstate_is_running(); |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2382 | vm_stop(RUN_STATE_SAVE_VM); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2383 | |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2384 | memset(sn, 0, sizeof(*sn)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2385 | |
| 2386 | /* fill auxiliary fields */ |
Stefan Weil | 68b891e | 2013-01-07 22:20:27 +0100 | [diff] [blame] | 2387 | qemu_gettimeofday(&tv); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2388 | sn->date_sec = tv.tv_sec; |
| 2389 | sn->date_nsec = tv.tv_usec * 1000; |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 2390 | sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2391 | |
Miguel Di Ciurcio Filho | 7d631a1 | 2010-08-04 14:55:49 -0300 | [diff] [blame] | 2392 | if (name) { |
| 2393 | ret = bdrv_snapshot_find(bs, old_sn, name); |
| 2394 | if (ret >= 0) { |
| 2395 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); |
| 2396 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); |
| 2397 | } else { |
| 2398 | pstrcpy(sn->name, sizeof(sn->name), name); |
| 2399 | } |
| 2400 | } else { |
Blue Swirl | d7d9b52 | 2010-09-09 19:13:04 +0000 | [diff] [blame] | 2401 | /* cast below needed for OpenBSD where tv_sec is still 'long' */ |
| 2402 | localtime_r((const time_t *)&tv.tv_sec, &tm); |
Miguel Di Ciurcio Filho | 7d631a1 | 2010-08-04 14:55:49 -0300 | [diff] [blame] | 2403 | strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm); |
Miguel Di Ciurcio Filho | 7d631a1 | 2010-08-04 14:55:49 -0300 | [diff] [blame] | 2404 | } |
| 2405 | |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2406 | /* Delete old snapshots of the same name */ |
Marcelo Tosatti | f139a41 | 2010-01-20 14:26:34 -0200 | [diff] [blame] | 2407 | if (name && del_existing_snapshots(mon, name) < 0) { |
Kevin Wolf | cb499fb | 2009-11-03 17:34:37 +0100 | [diff] [blame] | 2408 | goto the_end; |
| 2409 | } |
| 2410 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2411 | /* save the VM state */ |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 2412 | f = qemu_fopen_bdrv(bs, 1); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2413 | if (!f) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2414 | monitor_printf(mon, "Could not open VM state file\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2415 | goto the_end; |
| 2416 | } |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 2417 | ret = qemu_savevm_state(f); |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 2418 | vm_state_size = qemu_ftell(f); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2419 | qemu_fclose(f); |
| 2420 | if (ret < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2421 | monitor_printf(mon, "Error %d while writing VM\n", ret); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2422 | goto the_end; |
| 2423 | } |
| 2424 | |
| 2425 | /* create the snapshots */ |
| 2426 | |
Markus Armbruster | dbc1359 | 2010-06-02 18:55:21 +0200 | [diff] [blame] | 2427 | bs1 = NULL; |
| 2428 | while ((bs1 = bdrv_next(bs1))) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2429 | if (bdrv_can_snapshot(bs1)) { |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 2430 | /* Write VM state size only to the image that contains the state */ |
| 2431 | sn->vm_state_size = (bs == bs1 ? vm_state_size : 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2432 | ret = bdrv_snapshot_create(bs1, sn); |
| 2433 | if (ret < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2434 | monitor_printf(mon, "Error while creating snapshot on '%s'\n", |
| 2435 | bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | the_end: |
| 2441 | if (saved_vm_running) |
| 2442 | vm_start(); |
| 2443 | } |
| 2444 | |
Stefano Stabellini | a7ae835 | 2012-01-25 12:24:51 +0000 | [diff] [blame] | 2445 | void qmp_xen_save_devices_state(const char *filename, Error **errp) |
| 2446 | { |
| 2447 | QEMUFile *f; |
| 2448 | int saved_vm_running; |
| 2449 | int ret; |
| 2450 | |
| 2451 | saved_vm_running = runstate_is_running(); |
| 2452 | vm_stop(RUN_STATE_SAVE_VM); |
| 2453 | |
| 2454 | f = qemu_fopen(filename, "wb"); |
| 2455 | if (!f) { |
Luiz Capitulino | 1befce9 | 2013-06-07 14:36:58 -0400 | [diff] [blame] | 2456 | error_setg_file_open(errp, errno, filename); |
Stefano Stabellini | a7ae835 | 2012-01-25 12:24:51 +0000 | [diff] [blame] | 2457 | goto the_end; |
| 2458 | } |
| 2459 | ret = qemu_save_device_state(f); |
| 2460 | qemu_fclose(f); |
| 2461 | if (ret < 0) { |
| 2462 | error_set(errp, QERR_IO_ERROR); |
| 2463 | } |
| 2464 | |
| 2465 | the_end: |
| 2466 | if (saved_vm_running) |
| 2467 | vm_start(); |
Stefano Stabellini | a7ae835 | 2012-01-25 12:24:51 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Markus Armbruster | 03cd465 | 2010-02-17 16:24:10 +0100 | [diff] [blame] | 2470 | int load_vmstate(const char *name) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2471 | { |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2472 | BlockDriverState *bs, *bs_vm_state; |
aliguori | 2d22b18 | 2008-12-11 21:06:49 +0000 | [diff] [blame] | 2473 | QEMUSnapshotInfo sn; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2474 | QEMUFile *f; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2475 | int ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2476 | |
Stefan Hajnoczi | 29d7827 | 2013-05-25 11:09:42 +0800 | [diff] [blame] | 2477 | bs_vm_state = find_vmstate_bs(); |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2478 | if (!bs_vm_state) { |
| 2479 | error_report("No block device supports snapshots"); |
| 2480 | return -ENOTSUP; |
| 2481 | } |
| 2482 | |
| 2483 | /* Don't even try to load empty VM states */ |
| 2484 | ret = bdrv_snapshot_find(bs_vm_state, &sn, name); |
| 2485 | if (ret < 0) { |
| 2486 | return ret; |
| 2487 | } else if (sn.vm_state_size == 0) { |
Kevin Wolf | e11480d | 2011-03-01 10:48:12 +0100 | [diff] [blame] | 2488 | error_report("This is a disk-only snapshot. Revert to it offline " |
| 2489 | "using qemu-img."); |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2490 | return -EINVAL; |
| 2491 | } |
| 2492 | |
| 2493 | /* Verify if there is any device that doesn't support snapshots and is |
| 2494 | writable and check if the requested snapshot is available too. */ |
Markus Armbruster | dbc1359 | 2010-06-02 18:55:21 +0200 | [diff] [blame] | 2495 | bs = NULL; |
| 2496 | while ((bs = bdrv_next(bs))) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2497 | |
Markus Armbruster | 07b70bf | 2011-08-03 15:08:11 +0200 | [diff] [blame] | 2498 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2499 | continue; |
| 2500 | } |
| 2501 | |
| 2502 | if (!bdrv_can_snapshot(bs)) { |
| 2503 | error_report("Device '%s' is writable but does not support snapshots.", |
| 2504 | bdrv_get_device_name(bs)); |
| 2505 | return -ENOTSUP; |
| 2506 | } |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2507 | |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2508 | ret = bdrv_snapshot_find(bs, &sn, name); |
| 2509 | if (ret < 0) { |
| 2510 | error_report("Device '%s' does not have the requested snapshot '%s'", |
| 2511 | bdrv_get_device_name(bs), name); |
| 2512 | return ret; |
| 2513 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | /* 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] | 2517 | bdrv_drain_all(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2518 | |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2519 | bs = NULL; |
| 2520 | while ((bs = bdrv_next(bs))) { |
| 2521 | if (bdrv_can_snapshot(bs)) { |
| 2522 | ret = bdrv_snapshot_goto(bs, name); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2523 | if (ret < 0) { |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2524 | error_report("Error %d while activating snapshot '%s' on '%s'", |
| 2525 | ret, name, bdrv_get_device_name(bs)); |
| 2526 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2527 | } |
| 2528 | } |
| 2529 | } |
| 2530 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2531 | /* restore the VM state */ |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2532 | f = qemu_fopen_bdrv(bs_vm_state, 0); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2533 | if (!f) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 2534 | error_report("Could not open VM state file"); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 2535 | return -EINVAL; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2536 | } |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2537 | |
Jan Kiszka | 5a8a49d | 2011-06-14 18:29:45 +0200 | [diff] [blame] | 2538 | qemu_system_reset(VMRESET_SILENT); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2539 | ret = qemu_loadvm_state(f); |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2540 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2541 | qemu_fclose(f); |
| 2542 | if (ret < 0) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 2543 | error_report("Error %d while loading VM state", ret); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 2544 | return ret; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2545 | } |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 2546 | |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 2547 | return 0; |
Juan Quintela | 7b63034 | 2009-08-20 19:42:20 +0200 | [diff] [blame] | 2548 | } |
| 2549 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2550 | void do_delvm(Monitor *mon, const QDict *qdict) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2551 | { |
| 2552 | BlockDriverState *bs, *bs1; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2553 | int ret; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2554 | const char *name = qdict_get_str(qdict, "name"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2555 | |
Stefan Hajnoczi | 29d7827 | 2013-05-25 11:09:42 +0800 | [diff] [blame] | 2556 | bs = find_vmstate_bs(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2557 | if (!bs) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2558 | monitor_printf(mon, "No block device supports snapshots\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2559 | return; |
| 2560 | } |
| 2561 | |
Markus Armbruster | dbc1359 | 2010-06-02 18:55:21 +0200 | [diff] [blame] | 2562 | bs1 = NULL; |
| 2563 | while ((bs1 = bdrv_next(bs1))) { |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2564 | if (bdrv_can_snapshot(bs1)) { |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2565 | ret = bdrv_snapshot_delete(bs1, name); |
| 2566 | if (ret < 0) { |
| 2567 | if (ret == -ENOTSUP) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2568 | monitor_printf(mon, |
| 2569 | "Snapshots not supported on device '%s'\n", |
| 2570 | bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2571 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2572 | monitor_printf(mon, "Error %d while deleting snapshot on " |
| 2573 | "'%s'\n", ret, bdrv_get_device_name(bs1)); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2574 | } |
| 2575 | } |
| 2576 | } |
| 2577 | } |
| 2578 | |
Wenchao Xia | 84f2d0e | 2013-01-14 14:06:25 +0800 | [diff] [blame] | 2579 | void do_info_snapshots(Monitor *mon, const QDict *qdict) |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2580 | { |
| 2581 | BlockDriverState *bs, *bs1; |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2582 | QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s; |
| 2583 | int nb_sns, i, ret, available; |
| 2584 | int total; |
| 2585 | int *available_snapshots; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2586 | |
Stefan Hajnoczi | 29d7827 | 2013-05-25 11:09:42 +0800 | [diff] [blame] | 2587 | bs = find_vmstate_bs(); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2588 | if (!bs) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2589 | monitor_printf(mon, "No available block device supports snapshots\n"); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2590 | return; |
| 2591 | } |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2592 | |
| 2593 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2594 | if (nb_sns < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2595 | monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns); |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2596 | return; |
| 2597 | } |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2598 | |
| 2599 | if (nb_sns == 0) { |
| 2600 | monitor_printf(mon, "There is no snapshot available.\n"); |
| 2601 | return; |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2602 | } |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2603 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2604 | available_snapshots = g_malloc0(sizeof(int) * nb_sns); |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2605 | total = 0; |
| 2606 | for (i = 0; i < nb_sns; i++) { |
| 2607 | sn = &sn_tab[i]; |
| 2608 | available = 1; |
| 2609 | bs1 = NULL; |
| 2610 | |
| 2611 | while ((bs1 = bdrv_next(bs1))) { |
| 2612 | if (bdrv_can_snapshot(bs1) && bs1 != bs) { |
| 2613 | ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str); |
| 2614 | if (ret < 0) { |
| 2615 | available = 0; |
| 2616 | break; |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | if (available) { |
| 2622 | available_snapshots[total] = i; |
| 2623 | total++; |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | if (total > 0) { |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2628 | bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL); |
| 2629 | monitor_printf(mon, "\n"); |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2630 | for (i = 0; i < total; i++) { |
| 2631 | sn = &sn_tab[available_snapshots[i]]; |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2632 | bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn); |
| 2633 | monitor_printf(mon, "\n"); |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2634 | } |
| 2635 | } else { |
| 2636 | monitor_printf(mon, "There is no suitable snapshot available\n"); |
| 2637 | } |
| 2638 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2639 | g_free(sn_tab); |
| 2640 | g_free(available_snapshots); |
Miguel Di Ciurcio Filho | f920991 | 2010-08-04 14:55:48 -0300 | [diff] [blame] | 2641 | |
aliguori | a672b46 | 2008-11-11 21:33:36 +0000 | [diff] [blame] | 2642 | } |
Avi Kivity | c5705a7 | 2011-12-20 15:59:12 +0200 | [diff] [blame] | 2643 | |
| 2644 | void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev) |
| 2645 | { |
Avi Kivity | 1ddde08 | 2012-01-08 13:18:19 +0200 | [diff] [blame] | 2646 | qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK, |
Avi Kivity | c5705a7 | 2011-12-20 15:59:12 +0200 | [diff] [blame] | 2647 | memory_region_name(mr), dev); |
| 2648 | } |
| 2649 | |
| 2650 | void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev) |
| 2651 | { |
| 2652 | /* Nothing do to while the implementation is in RAMBlock */ |
| 2653 | } |
| 2654 | |
| 2655 | void vmstate_register_ram_global(MemoryRegion *mr) |
| 2656 | { |
| 2657 | vmstate_register_ram(mr, NULL); |
| 2658 | } |