blob: 8cff9c608ff44ba96bab3f5d5ec7d9acaedda6f5 [file] [log] [blame]
Blue Swirlad960902010-03-29 19:23:52 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include <stdint.h>
25#include <stdarg.h>
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +020026#include <stdlib.h>
Blue Swirlad960902010-03-29 19:23:52 +000027#ifndef _WIN32
Blue Swirl1c47cb12010-03-30 19:27:34 +000028#include <sys/types.h>
Blue Swirlad960902010-03-29 19:23:52 +000029#include <sys/mman.h>
30#endif
31#include "config.h"
32#include "monitor.h"
33#include "sysemu.h"
34#include "arch_init.h"
35#include "audio/audio.h"
36#include "hw/pc.h"
37#include "hw/pci.h"
38#include "hw/audiodev.h"
39#include "kvm.h"
40#include "migration.h"
41#include "net.h"
42#include "gdbstub.h"
43#include "hw/smbios.h"
Avi Kivity86e775c2011-12-15 16:24:49 +020044#include "exec-memory.h"
Jan Kiszka302fe512012-02-17 11:24:34 +010045#include "hw/pcspk.h"
Orit Wasserman17ad9b32012-08-06 21:42:53 +030046#include "qemu/page_cache.h"
Blue Swirlad960902010-03-29 19:23:52 +000047
Orit Wasserman3a697f62012-06-19 18:43:15 +030048#ifdef DEBUG_ARCH_INIT
49#define DPRINTF(fmt, ...) \
50 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
51#else
52#define DPRINTF(fmt, ...) \
53 do { } while (0)
54#endif
55
Blue Swirlad960902010-03-29 19:23:52 +000056#ifdef TARGET_SPARC
57int graphic_width = 1024;
58int graphic_height = 768;
59int graphic_depth = 8;
60#else
61int graphic_width = 800;
62int graphic_height = 600;
63int graphic_depth = 15;
64#endif
65
Blue Swirlad960902010-03-29 19:23:52 +000066
67#if defined(TARGET_ALPHA)
68#define QEMU_ARCH QEMU_ARCH_ALPHA
69#elif defined(TARGET_ARM)
70#define QEMU_ARCH QEMU_ARCH_ARM
71#elif defined(TARGET_CRIS)
72#define QEMU_ARCH QEMU_ARCH_CRIS
73#elif defined(TARGET_I386)
74#define QEMU_ARCH QEMU_ARCH_I386
75#elif defined(TARGET_M68K)
76#define QEMU_ARCH QEMU_ARCH_M68K
Michael Walle81ea0e12011-02-17 23:45:02 +010077#elif defined(TARGET_LM32)
78#define QEMU_ARCH QEMU_ARCH_LM32
Blue Swirlad960902010-03-29 19:23:52 +000079#elif defined(TARGET_MICROBLAZE)
80#define QEMU_ARCH QEMU_ARCH_MICROBLAZE
81#elif defined(TARGET_MIPS)
82#define QEMU_ARCH QEMU_ARCH_MIPS
Jia Liue67db062012-07-20 15:50:39 +080083#elif defined(TARGET_OPENRISC)
84#define QEMU_ARCH QEMU_ARCH_OPENRISC
Blue Swirlad960902010-03-29 19:23:52 +000085#elif defined(TARGET_PPC)
86#define QEMU_ARCH QEMU_ARCH_PPC
87#elif defined(TARGET_S390X)
88#define QEMU_ARCH QEMU_ARCH_S390X
89#elif defined(TARGET_SH4)
90#define QEMU_ARCH QEMU_ARCH_SH4
91#elif defined(TARGET_SPARC)
92#define QEMU_ARCH QEMU_ARCH_SPARC
Max Filippov23288262011-09-06 03:55:25 +040093#elif defined(TARGET_XTENSA)
94#define QEMU_ARCH QEMU_ARCH_XTENSA
Blue Swirlad960902010-03-29 19:23:52 +000095#endif
96
97const uint32_t arch_type = QEMU_ARCH;
98
99/***********************************************************/
100/* ram save/restore */
101
Yoshiaki Tamurad20878d2010-08-18 13:30:12 +0900102#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
103#define RAM_SAVE_FLAG_COMPRESS 0x02
104#define RAM_SAVE_FLAG_MEM_SIZE 0x04
105#define RAM_SAVE_FLAG_PAGE 0x08
106#define RAM_SAVE_FLAG_EOS 0x10
107#define RAM_SAVE_FLAG_CONTINUE 0x20
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300108#define RAM_SAVE_FLAG_XBZRLE 0x40
Blue Swirlad960902010-03-29 19:23:52 +0000109
Paolo Bonzini86003612011-12-23 16:17:26 +0100110#ifdef __ALTIVEC__
111#include <altivec.h>
112#define VECTYPE vector unsigned char
113#define SPLAT(p) vec_splat(vec_ld(0, p), 0)
114#define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
Andreas Färberf283edc2012-05-27 16:21:02 +0200115/* altivec.h may redefine the bool macro as vector type.
116 * Reset it to POSIX semantics. */
117#undef bool
118#define bool _Bool
Paolo Bonzini86003612011-12-23 16:17:26 +0100119#elif defined __SSE2__
120#include <emmintrin.h>
121#define VECTYPE __m128i
122#define SPLAT(p) _mm_set1_epi8(*(p))
123#define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
124#else
125#define VECTYPE unsigned long
126#define SPLAT(p) (*(p) * (~0UL / 255))
127#define ALL_EQ(v1, v2) ((v1) == (v2))
128#endif
129
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300130
Eduardo Habkost756557d2012-05-02 13:07:27 -0300131static struct defconfig_file {
132 const char *filename;
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300133 /* Indicates it is an user config file (disabled by -no-user-config) */
134 bool userconfig;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300135} default_config_files[] = {
Eduardo Habkoste2d87bf2012-05-02 13:07:30 -0300136 { CONFIG_QEMU_DATADIR "/cpus-" TARGET_ARCH ".conf", false },
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300137 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
138 { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true },
Eduardo Habkost756557d2012-05-02 13:07:27 -0300139 { NULL }, /* end of list */
140};
141
142
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300143int qemu_read_default_config_files(bool userconfig)
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300144{
145 int ret;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300146 struct defconfig_file *f;
147
148 for (f = default_config_files; f->filename; f++) {
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300149 if (!userconfig && f->userconfig) {
150 continue;
151 }
Eduardo Habkost756557d2012-05-02 13:07:27 -0300152 ret = qemu_read_config_file(f->filename);
153 if (ret < 0 && ret != -ENOENT) {
154 return ret;
155 }
156 }
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300157
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300158 return 0;
159}
160
Paolo Bonzini86003612011-12-23 16:17:26 +0100161static int is_dup_page(uint8_t *page)
Blue Swirlad960902010-03-29 19:23:52 +0000162{
Paolo Bonzini86003612011-12-23 16:17:26 +0100163 VECTYPE *p = (VECTYPE *)page;
164 VECTYPE val = SPLAT(page);
Blue Swirlad960902010-03-29 19:23:52 +0000165 int i;
166
Paolo Bonzini86003612011-12-23 16:17:26 +0100167 for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
168 if (!ALL_EQ(val, p[i])) {
Blue Swirlad960902010-03-29 19:23:52 +0000169 return 0;
170 }
171 }
172
173 return 1;
174}
175
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300176/* struct contains XBZRLE cache and a static page
177 used by the compression */
178static struct {
179 /* buffer used for XBZRLE encoding */
180 uint8_t *encoded_buf;
181 /* buffer for storing page content */
182 uint8_t *current_buf;
183 /* buffer used for XBZRLE decoding */
184 uint8_t *decoded_buf;
185 /* Cache for XBZRLE */
186 PageCache *cache;
187} XBZRLE = {
188 .encoded_buf = NULL,
189 .current_buf = NULL,
190 .decoded_buf = NULL,
191 .cache = NULL,
192};
193
Orit Wasserman0c51f432012-06-19 18:43:14 +0300194static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
195 int cont, int flag)
196{
197 qemu_put_be64(f, offset | cont | flag);
198 if (!cont) {
199 qemu_put_byte(f, strlen(block->idstr));
200 qemu_put_buffer(f, (uint8_t *)block->idstr,
201 strlen(block->idstr));
202 }
203
204}
205
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300206#define ENCODING_FLAG_XBZRLE 0x1
207
208static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
209 ram_addr_t current_addr, RAMBlock *block,
210 ram_addr_t offset, int cont)
211{
212 int encoded_len = 0, bytes_sent = -1;
213 uint8_t *prev_cached_page;
214
215 if (!cache_is_cached(XBZRLE.cache, current_addr)) {
216 cache_insert(XBZRLE.cache, current_addr,
217 g_memdup(current_data, TARGET_PAGE_SIZE));
218 return -1;
219 }
220
221 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
222
223 /* save current buffer into memory */
224 memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
225
226 /* XBZRLE encoding (if there is no overflow) */
227 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
228 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
229 TARGET_PAGE_SIZE);
230 if (encoded_len == 0) {
231 DPRINTF("Skipping unmodified page\n");
232 return 0;
233 } else if (encoded_len == -1) {
234 DPRINTF("Overflow\n");
235 /* update data in the cache */
236 memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
237 return -1;
238 }
239
240 /* we need to update the data in the cache, in order to get the same data */
241 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
242
243 /* Send XBZRLE based compressed page */
244 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
245 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
246 qemu_put_be16(f, encoded_len);
247 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
248 bytes_sent = encoded_len + 1 + 2;
249
250 return bytes_sent;
251}
252
Alex Williamson760e77e2010-08-19 10:18:42 -0300253static RAMBlock *last_block;
254static ram_addr_t last_offset;
255
Orit Wasserman6c779f22012-07-10 12:37:13 +0300256/*
257 * ram_save_block: Writes a page of memory to the stream f
258 *
259 * Returns: 0: if the page hasn't changed
260 * -1: if there are no more dirty pages
261 * n: the amount of bytes written in other case
262 */
263
Blue Swirlad960902010-03-29 19:23:52 +0000264static int ram_save_block(QEMUFile *f)
265{
Alex Williamsone44359c2010-06-25 11:09:57 -0600266 RAMBlock *block = last_block;
267 ram_addr_t offset = last_offset;
Orit Wasserman6c779f22012-07-10 12:37:13 +0300268 int bytes_sent = -1;
Avi Kivity71c510e2011-12-21 13:11:22 +0200269 MemoryRegion *mr;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300270 ram_addr_t current_addr;
Blue Swirlad960902010-03-29 19:23:52 +0000271
Alex Williamsone44359c2010-06-25 11:09:57 -0600272 if (!block)
273 block = QLIST_FIRST(&ram_list.blocks);
274
Alex Williamsone44359c2010-06-25 11:09:57 -0600275 do {
Avi Kivity71c510e2011-12-21 13:11:22 +0200276 mr = block->mr;
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000277 if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
278 DIRTY_MEMORY_MIGRATION)) {
Blue Swirlad960902010-03-29 19:23:52 +0000279 uint8_t *p;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600280 int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000281
Avi Kivity71c510e2011-12-21 13:11:22 +0200282 memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
283 DIRTY_MEMORY_MIGRATION);
Blue Swirlad960902010-03-29 19:23:52 +0000284
Avi Kivity71c510e2011-12-21 13:11:22 +0200285 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000286
Paolo Bonzini86003612011-12-23 16:17:26 +0100287 if (is_dup_page(p)) {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300288 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
Blue Swirlad960902010-03-29 19:23:52 +0000289 qemu_put_byte(f, *p);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200290 bytes_sent = 1;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300291 } else if (migrate_use_xbzrle()) {
292 current_addr = block->offset + offset;
293 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
294 offset, cont);
295 p = get_cached_data(XBZRLE.cache, current_addr);
296 }
297
298 /* either we didn't send yet (we may have had XBZRLE overflow) */
299 if (bytes_sent == -1) {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300300 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Blue Swirlad960902010-03-29 19:23:52 +0000301 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200302 bytes_sent = TARGET_PAGE_SIZE;
Blue Swirlad960902010-03-29 19:23:52 +0000303 }
304
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300305 /* if page is unmodified, continue to the next */
306 if (bytes_sent != 0) {
307 break;
308 }
Blue Swirlad960902010-03-29 19:23:52 +0000309 }
Alex Williamsone44359c2010-06-25 11:09:57 -0600310
311 offset += TARGET_PAGE_SIZE;
312 if (offset >= block->length) {
313 offset = 0;
314 block = QLIST_NEXT(block, next);
315 if (!block)
316 block = QLIST_FIRST(&ram_list.blocks);
317 }
Avi Kivity71c510e2011-12-21 13:11:22 +0200318 } while (block != last_block || offset != last_offset);
Alex Williamsone44359c2010-06-25 11:09:57 -0600319
320 last_block = block;
321 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000322
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200323 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000324}
325
326static uint64_t bytes_transferred;
327
328static ram_addr_t ram_save_remaining(void)
329{
Juan Quintela45f33f02012-06-22 15:21:07 +0200330 return ram_list.dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000331}
332
333uint64_t ram_bytes_remaining(void)
334{
335 return ram_save_remaining() * TARGET_PAGE_SIZE;
336}
337
338uint64_t ram_bytes_transferred(void)
339{
340 return bytes_transferred;
341}
342
343uint64_t ram_bytes_total(void)
344{
Alex Williamsond17b5282010-06-25 11:08:38 -0600345 RAMBlock *block;
346 uint64_t total = 0;
347
348 QLIST_FOREACH(block, &ram_list.blocks, next)
349 total += block->length;
350
351 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000352}
353
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200354static int block_compar(const void *a, const void *b)
355{
356 RAMBlock * const *ablock = a;
357 RAMBlock * const *bblock = b;
Avi Kivity8fec98b2011-12-21 13:22:16 +0200358
359 return strcmp((*ablock)->idstr, (*bblock)->idstr);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200360}
361
362static void sort_ram_list(void)
363{
364 RAMBlock *block, *nblock, **blocks;
365 int n;
366 n = 0;
367 QLIST_FOREACH(block, &ram_list.blocks, next) {
368 ++n;
369 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500370 blocks = g_malloc(n * sizeof *blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200371 n = 0;
372 QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
373 blocks[n++] = block;
374 QLIST_REMOVE(block, next);
375 }
376 qsort(blocks, n, sizeof *blocks, block_compar);
377 while (--n >= 0) {
378 QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
379 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500380 g_free(blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200381}
382
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300383static void migration_end(void)
384{
385 memory_global_dirty_log_stop();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300386
387 if (migrate_use_xbzrle()) {
388 cache_fini(XBZRLE.cache);
389 g_free(XBZRLE.cache);
390 g_free(XBZRLE.encoded_buf);
391 g_free(XBZRLE.current_buf);
392 g_free(XBZRLE.decoded_buf);
393 XBZRLE.cache = NULL;
394 }
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300395}
396
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200397static void ram_migration_cancel(void *opaque)
398{
399 migration_end();
400}
401
Juan Quintela4508bd92012-05-22 16:27:59 +0200402#define MAX_WAIT 50 /* ms, half buffered_file limit */
403
Juan Quintelad1315aa2012-06-28 15:11:57 +0200404static int ram_save_setup(QEMUFile *f, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000405{
406 ram_addr_t addr;
Juan Quintelad1315aa2012-06-28 15:11:57 +0200407 RAMBlock *block;
Blue Swirlad960902010-03-29 19:23:52 +0000408
Juan Quintelad1315aa2012-06-28 15:11:57 +0200409 bytes_transferred = 0;
410 last_block = NULL;
411 last_offset = 0;
412 sort_ram_list();
Blue Swirlad960902010-03-29 19:23:52 +0000413
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300414 if (migrate_use_xbzrle()) {
415 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
416 TARGET_PAGE_SIZE,
417 TARGET_PAGE_SIZE);
418 if (!XBZRLE.cache) {
419 DPRINTF("Error creating cache\n");
420 return -1;
421 }
422 XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
423 XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
424 }
425
Juan Quintelad1315aa2012-06-28 15:11:57 +0200426 /* Make sure all dirty bits are set */
427 QLIST_FOREACH(block, &ram_list.blocks, next) {
428 for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
429 if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE,
430 DIRTY_MEMORY_MIGRATION)) {
431 memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000432 }
433 }
Blue Swirlad960902010-03-29 19:23:52 +0000434 }
435
Juan Quintelad1315aa2012-06-28 15:11:57 +0200436 memory_global_dirty_log_start();
437
438 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
439
440 QLIST_FOREACH(block, &ram_list.blocks, next) {
441 qemu_put_byte(f, strlen(block->idstr));
442 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
443 qemu_put_be64(f, block->length);
444 }
445
Juan Quintelad1315aa2012-06-28 15:11:57 +0200446 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
447
448 return 0;
449}
450
Juan Quintela16310a32012-06-28 15:31:37 +0200451static int ram_save_iterate(QEMUFile *f, void *opaque)
Juan Quintelad1315aa2012-06-28 15:11:57 +0200452{
Blue Swirlad960902010-03-29 19:23:52 +0000453 uint64_t bytes_transferred_last;
454 double bwidth = 0;
455 int ret;
456 int i;
Juan Quintela16310a32012-06-28 15:31:37 +0200457 uint64_t expected_time;
Blue Swirlad960902010-03-29 19:23:52 +0000458
459 bytes_transferred_last = bytes_transferred;
460 bwidth = qemu_get_clock_ns(rt_clock);
461
Juan Quintela4508bd92012-05-22 16:27:59 +0200462 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200463 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200464 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000465
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200466 bytes_sent = ram_save_block(f);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300467 /* no more blocks to sent */
468 if (bytes_sent < 0) {
Blue Swirlad960902010-03-29 19:23:52 +0000469 break;
470 }
Orit Wasserman6c779f22012-07-10 12:37:13 +0300471 bytes_transferred += bytes_sent;
Juan Quintela4508bd92012-05-22 16:27:59 +0200472 /* we want to check in the 1st loop, just in case it was the 1st time
473 and we had to sync the dirty bitmap.
474 qemu_get_clock_ns() is a bit expensive, so we only check each some
475 iterations
476 */
477 if ((i & 63) == 0) {
478 uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000;
479 if (t1 > MAX_WAIT) {
480 DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n",
481 t1, i);
482 break;
483 }
484 }
485 i++;
Blue Swirlad960902010-03-29 19:23:52 +0000486 }
487
Juan Quintela29757252011-10-19 15:22:18 +0200488 if (ret < 0) {
489 return ret;
490 }
491
Blue Swirlad960902010-03-29 19:23:52 +0000492 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
493 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
494
495 /* if we haven't transferred anything this round, force expected_time to a
496 * a very high value, but without crashing */
497 if (bwidth == 0) {
498 bwidth = 0.000001;
499 }
500
Blue Swirlad960902010-03-29 19:23:52 +0000501 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
502
Juan Quintela16310a32012-06-28 15:31:37 +0200503 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
Blue Swirlad960902010-03-29 19:23:52 +0000504
Juan Quintela16310a32012-06-28 15:31:37 +0200505 DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n",
506 expected_time, migrate_max_downtime());
507
Juan Quintela00d94f32012-06-28 19:51:17 +0200508 if (expected_time <= migrate_max_downtime()) {
509 memory_global_sync_dirty_bitmap(get_system_memory());
510 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300511
Juan Quintela5b3c9632012-05-22 00:44:24 +0200512 return expected_time <= migrate_max_downtime();
513 }
514 return 0;
Blue Swirlad960902010-03-29 19:23:52 +0000515}
516
Juan Quintela16310a32012-06-28 15:31:37 +0200517static int ram_save_complete(QEMUFile *f, void *opaque)
518{
Juan Quintela16310a32012-06-28 15:31:37 +0200519 memory_global_sync_dirty_bitmap(get_system_memory());
520
Juan Quintela16310a32012-06-28 15:31:37 +0200521 /* try transferring iterative blocks of memory */
522
523 /* flush all remaining blocks regardless of rate limiting */
Orit Wasserman6c779f22012-07-10 12:37:13 +0300524 while (true) {
525 int bytes_sent;
526
527 bytes_sent = ram_save_block(f);
528 /* no more blocks to sent */
529 if (bytes_sent < 0) {
530 break;
531 }
Juan Quintela16310a32012-06-28 15:31:37 +0200532 bytes_transferred += bytes_sent;
533 }
534 memory_global_dirty_log_stop();
535
Blue Swirlad960902010-03-29 19:23:52 +0000536 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
537
Alex Williamsona55bbe32010-06-25 11:10:05 -0600538 return 0;
539}
540
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300541static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
542{
543 int ret, rc = 0;
544 unsigned int xh_len;
545 int xh_flags;
546
547 if (!XBZRLE.decoded_buf) {
548 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
549 }
550
551 /* extract RLE header */
552 xh_flags = qemu_get_byte(f);
553 xh_len = qemu_get_be16(f);
554
555 if (xh_flags != ENCODING_FLAG_XBZRLE) {
556 fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
557 return -1;
558 }
559
560 if (xh_len > TARGET_PAGE_SIZE) {
561 fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
562 return -1;
563 }
564 /* load data and decode */
565 qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
566
567 /* decode RLE */
568 ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
569 TARGET_PAGE_SIZE);
570 if (ret == -1) {
571 fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
572 rc = -1;
573 } else if (ret > TARGET_PAGE_SIZE) {
574 fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
575 ret, TARGET_PAGE_SIZE);
576 abort();
577 }
578
579 return rc;
580}
581
Alex Williamsona55bbe32010-06-25 11:10:05 -0600582static inline void *host_from_stream_offset(QEMUFile *f,
583 ram_addr_t offset,
584 int flags)
585{
586 static RAMBlock *block = NULL;
587 char id[256];
588 uint8_t len;
589
590 if (flags & RAM_SAVE_FLAG_CONTINUE) {
591 if (!block) {
592 fprintf(stderr, "Ack, bad migration stream!\n");
593 return NULL;
594 }
595
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200596 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600597 }
598
599 len = qemu_get_byte(f);
600 qemu_get_buffer(f, (uint8_t *)id, len);
601 id[len] = 0;
602
603 QLIST_FOREACH(block, &ram_list.blocks, next) {
604 if (!strncmp(id, block->idstr, sizeof(id)))
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200605 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600606 }
607
608 fprintf(stderr, "Can't find block %s!\n", id);
609 return NULL;
610}
611
Juan Quintela7908c782012-06-26 18:46:10 +0200612static int ram_load(QEMUFile *f, void *opaque, int version_id)
Blue Swirlad960902010-03-29 19:23:52 +0000613{
614 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300615 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200616 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300617 static uint64_t seq_iter;
618
619 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000620
Avi Kivityf09f2182011-12-21 13:37:56 +0200621 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000622 return -EINVAL;
623 }
624
625 do {
626 addr = qemu_get_be64(f);
627
628 flags = addr & ~TARGET_PAGE_MASK;
629 addr &= TARGET_PAGE_MASK;
630
631 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200632 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600633 /* Synchronize RAM block list */
634 char id[256];
635 ram_addr_t length;
636 ram_addr_t total_ram_bytes = addr;
637
638 while (total_ram_bytes) {
639 RAMBlock *block;
640 uint8_t len;
641
642 len = qemu_get_byte(f);
643 qemu_get_buffer(f, (uint8_t *)id, len);
644 id[len] = 0;
645 length = qemu_get_be64(f);
646
647 QLIST_FOREACH(block, &ram_list.blocks, next) {
648 if (!strncmp(id, block->idstr, sizeof(id))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300649 if (block->length != length) {
650 ret = -EINVAL;
651 goto done;
652 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600653 break;
654 }
655 }
656
657 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600658 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
659 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300660 ret = -EINVAL;
661 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600662 }
663
664 total_ram_bytes -= length;
665 }
Blue Swirlad960902010-03-29 19:23:52 +0000666 }
667 }
668
669 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600670 void *host;
671 uint8_t ch;
672
Avi Kivityf09f2182011-12-21 13:37:56 +0200673 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +0200674 if (!host) {
675 return -EINVAL;
676 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600677
Alex Williamson97ab12d2010-06-25 11:09:50 -0600678 ch = qemu_get_byte(f);
679 memset(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000680#ifndef _WIN32
681 if (ch == 0 &&
682 (!kvm_enabled() || kvm_has_sync_mmu())) {
Andreas Färbere78815a2010-09-25 11:26:05 +0000683 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
Blue Swirlad960902010-03-29 19:23:52 +0000684 }
685#endif
686 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600687 void *host;
688
Avi Kivityf09f2182011-12-21 13:37:56 +0200689 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +0300690 if (!host) {
691 return -EINVAL;
692 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600693
Alex Williamson97ab12d2010-06-25 11:09:50 -0600694 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300695 } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
696 if (!migrate_use_xbzrle()) {
697 return -EINVAL;
698 }
699 void *host = host_from_stream_offset(f, addr, flags);
700 if (!host) {
701 return -EINVAL;
702 }
703
704 if (load_xbzrle(f, addr, host) < 0) {
705 ret = -EINVAL;
706 goto done;
707 }
Blue Swirlad960902010-03-29 19:23:52 +0000708 }
Juan Quintela42802d42011-10-05 01:14:46 +0200709 error = qemu_file_get_error(f);
710 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300711 ret = error;
712 goto done;
Blue Swirlad960902010-03-29 19:23:52 +0000713 }
714 } while (!(flags & RAM_SAVE_FLAG_EOS));
715
Orit Wasserman3a697f62012-06-19 18:43:15 +0300716done:
717 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64 "\n",
718 ret, seq_iter);
719 return ret;
Blue Swirlad960902010-03-29 19:23:52 +0000720}
721
Juan Quintela7908c782012-06-26 18:46:10 +0200722SaveVMHandlers savevm_ram_handlers = {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200723 .save_live_setup = ram_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +0200724 .save_live_iterate = ram_save_iterate,
725 .save_live_complete = ram_save_complete,
Juan Quintela7908c782012-06-26 18:46:10 +0200726 .load_state = ram_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200727 .cancel = ram_migration_cancel,
Juan Quintela7908c782012-06-26 18:46:10 +0200728};
729
Blue Swirlad960902010-03-29 19:23:52 +0000730#ifdef HAS_AUDIO
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900731struct soundhw {
732 const char *name;
733 const char *descr;
734 int enabled;
735 int isa;
736 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100737 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900738 int (*init_pci) (PCIBus *bus);
739 } init;
740};
741
742static struct soundhw soundhw[] = {
Blue Swirlad960902010-03-29 19:23:52 +0000743#ifdef HAS_AUDIO_CHOICE
Hervé Poussineauda128722012-04-14 22:51:33 +0200744#ifdef CONFIG_PCSPK
Blue Swirlad960902010-03-29 19:23:52 +0000745 {
746 "pcspk",
747 "PC speaker",
748 0,
749 1,
750 { .init_isa = pcspk_audio_init }
751 },
752#endif
753
754#ifdef CONFIG_SB16
755 {
756 "sb16",
757 "Creative Sound Blaster 16",
758 0,
759 1,
760 { .init_isa = SB16_init }
761 },
762#endif
763
764#ifdef CONFIG_CS4231A
765 {
766 "cs4231a",
767 "CS4231A",
768 0,
769 1,
770 { .init_isa = cs4231a_init }
771 },
772#endif
773
774#ifdef CONFIG_ADLIB
775 {
776 "adlib",
777#ifdef HAS_YMF262
778 "Yamaha YMF262 (OPL3)",
779#else
780 "Yamaha YM3812 (OPL2)",
781#endif
782 0,
783 1,
784 { .init_isa = Adlib_init }
785 },
786#endif
787
788#ifdef CONFIG_GUS
789 {
790 "gus",
791 "Gravis Ultrasound GF1",
792 0,
793 1,
794 { .init_isa = GUS_init }
795 },
796#endif
797
798#ifdef CONFIG_AC97
799 {
800 "ac97",
801 "Intel 82801AA AC97 Audio",
802 0,
803 0,
804 { .init_pci = ac97_init }
805 },
806#endif
807
808#ifdef CONFIG_ES1370
809 {
810 "es1370",
811 "ENSONIQ AudioPCI ES1370",
812 0,
813 0,
814 { .init_pci = es1370_init }
815 },
816#endif
817
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100818#ifdef CONFIG_HDA
819 {
820 "hda",
821 "Intel HD Audio",
822 0,
823 0,
824 { .init_pci = intel_hda_and_codec_init }
825 },
826#endif
827
Blue Swirlad960902010-03-29 19:23:52 +0000828#endif /* HAS_AUDIO_CHOICE */
829
830 { NULL, NULL, 0, 0, { NULL } }
831};
832
833void select_soundhw(const char *optarg)
834{
835 struct soundhw *c;
836
Peter Maydellc8057f92012-08-02 13:45:54 +0100837 if (is_help_option(optarg)) {
Blue Swirlad960902010-03-29 19:23:52 +0000838 show_valid_cards:
839
840 printf("Valid sound card names (comma separated):\n");
841 for (c = soundhw; c->name; ++c) {
842 printf ("%-11s %s\n", c->name, c->descr);
843 }
844 printf("\n-soundhw all will enable all of the above\n");
Peter Maydellc8057f92012-08-02 13:45:54 +0100845 exit(!is_help_option(optarg));
Blue Swirlad960902010-03-29 19:23:52 +0000846 }
847 else {
848 size_t l;
849 const char *p;
850 char *e;
851 int bad_card = 0;
852
853 if (!strcmp(optarg, "all")) {
854 for (c = soundhw; c->name; ++c) {
855 c->enabled = 1;
856 }
857 return;
858 }
859
860 p = optarg;
861 while (*p) {
862 e = strchr(p, ',');
863 l = !e ? strlen(p) : (size_t) (e - p);
864
865 for (c = soundhw; c->name; ++c) {
866 if (!strncmp(c->name, p, l) && !c->name[l]) {
867 c->enabled = 1;
868 break;
869 }
870 }
871
872 if (!c->name) {
873 if (l > 80) {
874 fprintf(stderr,
875 "Unknown sound card name (too big to show)\n");
876 }
877 else {
878 fprintf(stderr, "Unknown sound card name `%.*s'\n",
879 (int) l, p);
880 }
881 bad_card = 1;
882 }
883 p += l + (e != NULL);
884 }
885
886 if (bad_card) {
887 goto show_valid_cards;
888 }
889 }
890}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900891
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100892void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900893{
894 struct soundhw *c;
895
896 for (c = soundhw; c->name; ++c) {
897 if (c->enabled) {
898 if (c->isa) {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100899 if (isa_bus) {
900 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900901 }
902 } else {
903 if (pci_bus) {
904 c->init.init_pci(pci_bus);
905 }
906 }
907 }
908 }
909}
Blue Swirlad960902010-03-29 19:23:52 +0000910#else
911void select_soundhw(const char *optarg)
912{
913}
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100914void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900915{
916}
Blue Swirlad960902010-03-29 19:23:52 +0000917#endif
918
919int qemu_uuid_parse(const char *str, uint8_t *uuid)
920{
921 int ret;
922
923 if (strlen(str) != 36) {
924 return -1;
925 }
926
927 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
928 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
929 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
930 &uuid[15]);
931
932 if (ret != 16) {
933 return -1;
934 }
935#ifdef TARGET_I386
936 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
937#endif
938 return 0;
939}
940
941void do_acpitable_option(const char *optarg)
942{
943#ifdef TARGET_I386
944 if (acpi_table_add(optarg) < 0) {
945 fprintf(stderr, "Wrong acpi table provided\n");
946 exit(1);
947 }
948#endif
949}
950
951void do_smbios_option(const char *optarg)
952{
953#ifdef TARGET_I386
954 if (smbios_entry_add(optarg) < 0) {
955 fprintf(stderr, "Wrong smbios provided\n");
956 exit(1);
957 }
958#endif
959}
960
961void cpudef_init(void)
962{
963#if defined(cpudef_setup)
964 cpudef_setup(); /* parse cpu definitions in target config file */
965#endif
966}
967
968int audio_available(void)
969{
970#ifdef HAS_AUDIO
971 return 1;
972#else
973 return 0;
974#endif
975}
976
Anthony PERARD303d4e82010-09-21 20:05:31 +0100977int tcg_available(void)
978{
979 return 1;
980}
981
Blue Swirlad960902010-03-29 19:23:52 +0000982int kvm_available(void)
983{
984#ifdef CONFIG_KVM
985 return 1;
986#else
987 return 0;
988#endif
989}
990
991int xen_available(void)
992{
993#ifdef CONFIG_XEN
994 return 1;
995#else
996 return 0;
997#endif
998}