blob: ee692ae4e53c3c11e2ad862c97cf82f70843dbfd [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 Wasserman9e1ba4c2012-08-06 21:42:54 +0300194
195int64_t xbzrle_cache_resize(int64_t new_size)
196{
197 if (XBZRLE.cache != NULL) {
198 return cache_resize(XBZRLE.cache, new_size / TARGET_PAGE_SIZE) *
199 TARGET_PAGE_SIZE;
200 }
201 return pow2floor(new_size);
202}
203
Orit Wasserman004d4c12012-08-06 21:42:56 +0300204/* accounting for migration statistics */
205typedef struct AccountingInfo {
206 uint64_t dup_pages;
207 uint64_t norm_pages;
208 uint64_t iterations;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300209 uint64_t xbzrle_bytes;
210 uint64_t xbzrle_pages;
211 uint64_t xbzrle_cache_miss;
212 uint64_t xbzrle_overflows;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300213} AccountingInfo;
214
215static AccountingInfo acct_info;
216
217static void acct_clear(void)
218{
219 memset(&acct_info, 0, sizeof(acct_info));
220}
221
222uint64_t dup_mig_bytes_transferred(void)
223{
224 return acct_info.dup_pages * TARGET_PAGE_SIZE;
225}
226
227uint64_t dup_mig_pages_transferred(void)
228{
229 return acct_info.dup_pages;
230}
231
232uint64_t norm_mig_bytes_transferred(void)
233{
234 return acct_info.norm_pages * TARGET_PAGE_SIZE;
235}
236
237uint64_t norm_mig_pages_transferred(void)
238{
239 return acct_info.norm_pages;
240}
241
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300242uint64_t xbzrle_mig_bytes_transferred(void)
243{
244 return acct_info.xbzrle_bytes;
245}
246
247uint64_t xbzrle_mig_pages_transferred(void)
248{
249 return acct_info.xbzrle_pages;
250}
251
252uint64_t xbzrle_mig_pages_cache_miss(void)
253{
254 return acct_info.xbzrle_cache_miss;
255}
256
257uint64_t xbzrle_mig_pages_overflow(void)
258{
259 return acct_info.xbzrle_overflows;
260}
261
Orit Wasserman0c51f432012-06-19 18:43:14 +0300262static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
263 int cont, int flag)
264{
265 qemu_put_be64(f, offset | cont | flag);
266 if (!cont) {
267 qemu_put_byte(f, strlen(block->idstr));
268 qemu_put_buffer(f, (uint8_t *)block->idstr,
269 strlen(block->idstr));
270 }
271
272}
273
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300274#define ENCODING_FLAG_XBZRLE 0x1
275
276static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
277 ram_addr_t current_addr, RAMBlock *block,
278 ram_addr_t offset, int cont)
279{
280 int encoded_len = 0, bytes_sent = -1;
281 uint8_t *prev_cached_page;
282
283 if (!cache_is_cached(XBZRLE.cache, current_addr)) {
284 cache_insert(XBZRLE.cache, current_addr,
285 g_memdup(current_data, TARGET_PAGE_SIZE));
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300286 acct_info.xbzrle_cache_miss++;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300287 return -1;
288 }
289
290 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
291
292 /* save current buffer into memory */
293 memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
294
295 /* XBZRLE encoding (if there is no overflow) */
296 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
297 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
298 TARGET_PAGE_SIZE);
299 if (encoded_len == 0) {
300 DPRINTF("Skipping unmodified page\n");
301 return 0;
302 } else if (encoded_len == -1) {
303 DPRINTF("Overflow\n");
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300304 acct_info.xbzrle_overflows++;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300305 /* update data in the cache */
306 memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
307 return -1;
308 }
309
310 /* we need to update the data in the cache, in order to get the same data */
311 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
312
313 /* Send XBZRLE based compressed page */
314 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
315 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
316 qemu_put_be16(f, encoded_len);
317 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
318 bytes_sent = encoded_len + 1 + 2;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300319 acct_info.xbzrle_pages++;
320 acct_info.xbzrle_bytes += bytes_sent;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300321
322 return bytes_sent;
323}
324
Alex Williamson760e77e2010-08-19 10:18:42 -0300325static RAMBlock *last_block;
326static ram_addr_t last_offset;
327
Orit Wasserman6c779f22012-07-10 12:37:13 +0300328/*
329 * ram_save_block: Writes a page of memory to the stream f
330 *
331 * Returns: 0: if the page hasn't changed
332 * -1: if there are no more dirty pages
333 * n: the amount of bytes written in other case
334 */
335
Blue Swirlad960902010-03-29 19:23:52 +0000336static int ram_save_block(QEMUFile *f)
337{
Alex Williamsone44359c2010-06-25 11:09:57 -0600338 RAMBlock *block = last_block;
339 ram_addr_t offset = last_offset;
Orit Wasserman6c779f22012-07-10 12:37:13 +0300340 int bytes_sent = -1;
Avi Kivity71c510e2011-12-21 13:11:22 +0200341 MemoryRegion *mr;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300342 ram_addr_t current_addr;
Blue Swirlad960902010-03-29 19:23:52 +0000343
Alex Williamsone44359c2010-06-25 11:09:57 -0600344 if (!block)
345 block = QLIST_FIRST(&ram_list.blocks);
346
Alex Williamsone44359c2010-06-25 11:09:57 -0600347 do {
Avi Kivity71c510e2011-12-21 13:11:22 +0200348 mr = block->mr;
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000349 if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
350 DIRTY_MEMORY_MIGRATION)) {
Blue Swirlad960902010-03-29 19:23:52 +0000351 uint8_t *p;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600352 int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000353
Avi Kivity71c510e2011-12-21 13:11:22 +0200354 memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
355 DIRTY_MEMORY_MIGRATION);
Blue Swirlad960902010-03-29 19:23:52 +0000356
Avi Kivity71c510e2011-12-21 13:11:22 +0200357 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000358
Paolo Bonzini86003612011-12-23 16:17:26 +0100359 if (is_dup_page(p)) {
Orit Wasserman004d4c12012-08-06 21:42:56 +0300360 acct_info.dup_pages++;
Orit Wasserman0c51f432012-06-19 18:43:14 +0300361 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
Blue Swirlad960902010-03-29 19:23:52 +0000362 qemu_put_byte(f, *p);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200363 bytes_sent = 1;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300364 } else if (migrate_use_xbzrle()) {
365 current_addr = block->offset + offset;
366 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
367 offset, cont);
368 p = get_cached_data(XBZRLE.cache, current_addr);
369 }
370
371 /* either we didn't send yet (we may have had XBZRLE overflow) */
372 if (bytes_sent == -1) {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300373 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Blue Swirlad960902010-03-29 19:23:52 +0000374 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200375 bytes_sent = TARGET_PAGE_SIZE;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300376 acct_info.norm_pages++;
Blue Swirlad960902010-03-29 19:23:52 +0000377 }
378
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300379 /* if page is unmodified, continue to the next */
380 if (bytes_sent != 0) {
381 break;
382 }
Blue Swirlad960902010-03-29 19:23:52 +0000383 }
Alex Williamsone44359c2010-06-25 11:09:57 -0600384
385 offset += TARGET_PAGE_SIZE;
386 if (offset >= block->length) {
387 offset = 0;
388 block = QLIST_NEXT(block, next);
389 if (!block)
390 block = QLIST_FIRST(&ram_list.blocks);
391 }
Avi Kivity71c510e2011-12-21 13:11:22 +0200392 } while (block != last_block || offset != last_offset);
Alex Williamsone44359c2010-06-25 11:09:57 -0600393
394 last_block = block;
395 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000396
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200397 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000398}
399
400static uint64_t bytes_transferred;
401
402static ram_addr_t ram_save_remaining(void)
403{
Juan Quintela45f33f02012-06-22 15:21:07 +0200404 return ram_list.dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000405}
406
407uint64_t ram_bytes_remaining(void)
408{
409 return ram_save_remaining() * TARGET_PAGE_SIZE;
410}
411
412uint64_t ram_bytes_transferred(void)
413{
414 return bytes_transferred;
415}
416
417uint64_t ram_bytes_total(void)
418{
Alex Williamsond17b5282010-06-25 11:08:38 -0600419 RAMBlock *block;
420 uint64_t total = 0;
421
422 QLIST_FOREACH(block, &ram_list.blocks, next)
423 total += block->length;
424
425 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000426}
427
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200428static int block_compar(const void *a, const void *b)
429{
430 RAMBlock * const *ablock = a;
431 RAMBlock * const *bblock = b;
Avi Kivity8fec98b2011-12-21 13:22:16 +0200432
433 return strcmp((*ablock)->idstr, (*bblock)->idstr);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200434}
435
436static void sort_ram_list(void)
437{
438 RAMBlock *block, *nblock, **blocks;
439 int n;
440 n = 0;
441 QLIST_FOREACH(block, &ram_list.blocks, next) {
442 ++n;
443 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500444 blocks = g_malloc(n * sizeof *blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200445 n = 0;
446 QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
447 blocks[n++] = block;
448 QLIST_REMOVE(block, next);
449 }
450 qsort(blocks, n, sizeof *blocks, block_compar);
451 while (--n >= 0) {
452 QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
453 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500454 g_free(blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200455}
456
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300457static void migration_end(void)
458{
459 memory_global_dirty_log_stop();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300460
461 if (migrate_use_xbzrle()) {
462 cache_fini(XBZRLE.cache);
463 g_free(XBZRLE.cache);
464 g_free(XBZRLE.encoded_buf);
465 g_free(XBZRLE.current_buf);
466 g_free(XBZRLE.decoded_buf);
467 XBZRLE.cache = NULL;
468 }
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300469}
470
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200471static void ram_migration_cancel(void *opaque)
472{
473 migration_end();
474}
475
Juan Quintela4508bd92012-05-22 16:27:59 +0200476#define MAX_WAIT 50 /* ms, half buffered_file limit */
477
Juan Quintelad1315aa2012-06-28 15:11:57 +0200478static int ram_save_setup(QEMUFile *f, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000479{
480 ram_addr_t addr;
Juan Quintelad1315aa2012-06-28 15:11:57 +0200481 RAMBlock *block;
Blue Swirlad960902010-03-29 19:23:52 +0000482
Juan Quintelad1315aa2012-06-28 15:11:57 +0200483 bytes_transferred = 0;
484 last_block = NULL;
485 last_offset = 0;
486 sort_ram_list();
Blue Swirlad960902010-03-29 19:23:52 +0000487
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300488 if (migrate_use_xbzrle()) {
489 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
490 TARGET_PAGE_SIZE,
491 TARGET_PAGE_SIZE);
492 if (!XBZRLE.cache) {
493 DPRINTF("Error creating cache\n");
494 return -1;
495 }
496 XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
497 XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300498 acct_clear();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300499 }
500
Juan Quintelad1315aa2012-06-28 15:11:57 +0200501 /* Make sure all dirty bits are set */
502 QLIST_FOREACH(block, &ram_list.blocks, next) {
503 for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
504 if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE,
505 DIRTY_MEMORY_MIGRATION)) {
506 memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000507 }
508 }
Blue Swirlad960902010-03-29 19:23:52 +0000509 }
510
Juan Quintelad1315aa2012-06-28 15:11:57 +0200511 memory_global_dirty_log_start();
512
513 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
514
515 QLIST_FOREACH(block, &ram_list.blocks, next) {
516 qemu_put_byte(f, strlen(block->idstr));
517 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
518 qemu_put_be64(f, block->length);
519 }
520
Juan Quintelad1315aa2012-06-28 15:11:57 +0200521 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
522
523 return 0;
524}
525
Juan Quintela16310a32012-06-28 15:31:37 +0200526static int ram_save_iterate(QEMUFile *f, void *opaque)
Juan Quintelad1315aa2012-06-28 15:11:57 +0200527{
Blue Swirlad960902010-03-29 19:23:52 +0000528 uint64_t bytes_transferred_last;
529 double bwidth = 0;
530 int ret;
531 int i;
Juan Quintela16310a32012-06-28 15:31:37 +0200532 uint64_t expected_time;
Blue Swirlad960902010-03-29 19:23:52 +0000533
534 bytes_transferred_last = bytes_transferred;
535 bwidth = qemu_get_clock_ns(rt_clock);
536
Juan Quintela4508bd92012-05-22 16:27:59 +0200537 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200538 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200539 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000540
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200541 bytes_sent = ram_save_block(f);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300542 /* no more blocks to sent */
543 if (bytes_sent < 0) {
Blue Swirlad960902010-03-29 19:23:52 +0000544 break;
545 }
Orit Wasserman6c779f22012-07-10 12:37:13 +0300546 bytes_transferred += bytes_sent;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300547 acct_info.iterations++;
Juan Quintela4508bd92012-05-22 16:27:59 +0200548 /* we want to check in the 1st loop, just in case it was the 1st time
549 and we had to sync the dirty bitmap.
550 qemu_get_clock_ns() is a bit expensive, so we only check each some
551 iterations
552 */
553 if ((i & 63) == 0) {
554 uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000;
555 if (t1 > MAX_WAIT) {
556 DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n",
557 t1, i);
558 break;
559 }
560 }
561 i++;
Blue Swirlad960902010-03-29 19:23:52 +0000562 }
563
Juan Quintela29757252011-10-19 15:22:18 +0200564 if (ret < 0) {
565 return ret;
566 }
567
Blue Swirlad960902010-03-29 19:23:52 +0000568 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
569 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
570
571 /* if we haven't transferred anything this round, force expected_time to a
572 * a very high value, but without crashing */
573 if (bwidth == 0) {
574 bwidth = 0.000001;
575 }
576
Blue Swirlad960902010-03-29 19:23:52 +0000577 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
578
Juan Quintela16310a32012-06-28 15:31:37 +0200579 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
Blue Swirlad960902010-03-29 19:23:52 +0000580
Juan Quintela16310a32012-06-28 15:31:37 +0200581 DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n",
582 expected_time, migrate_max_downtime());
583
Juan Quintela00d94f32012-06-28 19:51:17 +0200584 if (expected_time <= migrate_max_downtime()) {
585 memory_global_sync_dirty_bitmap(get_system_memory());
586 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300587
Juan Quintela5b3c9632012-05-22 00:44:24 +0200588 return expected_time <= migrate_max_downtime();
589 }
590 return 0;
Blue Swirlad960902010-03-29 19:23:52 +0000591}
592
Juan Quintela16310a32012-06-28 15:31:37 +0200593static int ram_save_complete(QEMUFile *f, void *opaque)
594{
Juan Quintela16310a32012-06-28 15:31:37 +0200595 memory_global_sync_dirty_bitmap(get_system_memory());
596
Juan Quintela16310a32012-06-28 15:31:37 +0200597 /* try transferring iterative blocks of memory */
598
599 /* flush all remaining blocks regardless of rate limiting */
Orit Wasserman6c779f22012-07-10 12:37:13 +0300600 while (true) {
601 int bytes_sent;
602
603 bytes_sent = ram_save_block(f);
604 /* no more blocks to sent */
605 if (bytes_sent < 0) {
606 break;
607 }
Juan Quintela16310a32012-06-28 15:31:37 +0200608 bytes_transferred += bytes_sent;
609 }
610 memory_global_dirty_log_stop();
611
Blue Swirlad960902010-03-29 19:23:52 +0000612 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
613
Alex Williamsona55bbe32010-06-25 11:10:05 -0600614 return 0;
615}
616
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300617static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
618{
619 int ret, rc = 0;
620 unsigned int xh_len;
621 int xh_flags;
622
623 if (!XBZRLE.decoded_buf) {
624 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
625 }
626
627 /* extract RLE header */
628 xh_flags = qemu_get_byte(f);
629 xh_len = qemu_get_be16(f);
630
631 if (xh_flags != ENCODING_FLAG_XBZRLE) {
632 fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
633 return -1;
634 }
635
636 if (xh_len > TARGET_PAGE_SIZE) {
637 fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
638 return -1;
639 }
640 /* load data and decode */
641 qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
642
643 /* decode RLE */
644 ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
645 TARGET_PAGE_SIZE);
646 if (ret == -1) {
647 fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
648 rc = -1;
649 } else if (ret > TARGET_PAGE_SIZE) {
650 fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
651 ret, TARGET_PAGE_SIZE);
652 abort();
653 }
654
655 return rc;
656}
657
Alex Williamsona55bbe32010-06-25 11:10:05 -0600658static inline void *host_from_stream_offset(QEMUFile *f,
659 ram_addr_t offset,
660 int flags)
661{
662 static RAMBlock *block = NULL;
663 char id[256];
664 uint8_t len;
665
666 if (flags & RAM_SAVE_FLAG_CONTINUE) {
667 if (!block) {
668 fprintf(stderr, "Ack, bad migration stream!\n");
669 return NULL;
670 }
671
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200672 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600673 }
674
675 len = qemu_get_byte(f);
676 qemu_get_buffer(f, (uint8_t *)id, len);
677 id[len] = 0;
678
679 QLIST_FOREACH(block, &ram_list.blocks, next) {
680 if (!strncmp(id, block->idstr, sizeof(id)))
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200681 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600682 }
683
684 fprintf(stderr, "Can't find block %s!\n", id);
685 return NULL;
686}
687
Juan Quintela7908c782012-06-26 18:46:10 +0200688static int ram_load(QEMUFile *f, void *opaque, int version_id)
Blue Swirlad960902010-03-29 19:23:52 +0000689{
690 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300691 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200692 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300693 static uint64_t seq_iter;
694
695 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000696
Avi Kivityf09f2182011-12-21 13:37:56 +0200697 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000698 return -EINVAL;
699 }
700
701 do {
702 addr = qemu_get_be64(f);
703
704 flags = addr & ~TARGET_PAGE_MASK;
705 addr &= TARGET_PAGE_MASK;
706
707 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200708 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600709 /* Synchronize RAM block list */
710 char id[256];
711 ram_addr_t length;
712 ram_addr_t total_ram_bytes = addr;
713
714 while (total_ram_bytes) {
715 RAMBlock *block;
716 uint8_t len;
717
718 len = qemu_get_byte(f);
719 qemu_get_buffer(f, (uint8_t *)id, len);
720 id[len] = 0;
721 length = qemu_get_be64(f);
722
723 QLIST_FOREACH(block, &ram_list.blocks, next) {
724 if (!strncmp(id, block->idstr, sizeof(id))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300725 if (block->length != length) {
726 ret = -EINVAL;
727 goto done;
728 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600729 break;
730 }
731 }
732
733 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600734 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
735 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300736 ret = -EINVAL;
737 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600738 }
739
740 total_ram_bytes -= length;
741 }
Blue Swirlad960902010-03-29 19:23:52 +0000742 }
743 }
744
745 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600746 void *host;
747 uint8_t ch;
748
Avi Kivityf09f2182011-12-21 13:37:56 +0200749 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +0200750 if (!host) {
751 return -EINVAL;
752 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600753
Alex Williamson97ab12d2010-06-25 11:09:50 -0600754 ch = qemu_get_byte(f);
755 memset(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000756#ifndef _WIN32
757 if (ch == 0 &&
758 (!kvm_enabled() || kvm_has_sync_mmu())) {
Andreas Färbere78815a2010-09-25 11:26:05 +0000759 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
Blue Swirlad960902010-03-29 19:23:52 +0000760 }
761#endif
762 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600763 void *host;
764
Avi Kivityf09f2182011-12-21 13:37:56 +0200765 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +0300766 if (!host) {
767 return -EINVAL;
768 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600769
Alex Williamson97ab12d2010-06-25 11:09:50 -0600770 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300771 } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
772 if (!migrate_use_xbzrle()) {
773 return -EINVAL;
774 }
775 void *host = host_from_stream_offset(f, addr, flags);
776 if (!host) {
777 return -EINVAL;
778 }
779
780 if (load_xbzrle(f, addr, host) < 0) {
781 ret = -EINVAL;
782 goto done;
783 }
Blue Swirlad960902010-03-29 19:23:52 +0000784 }
Juan Quintela42802d42011-10-05 01:14:46 +0200785 error = qemu_file_get_error(f);
786 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300787 ret = error;
788 goto done;
Blue Swirlad960902010-03-29 19:23:52 +0000789 }
790 } while (!(flags & RAM_SAVE_FLAG_EOS));
791
Orit Wasserman3a697f62012-06-19 18:43:15 +0300792done:
793 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64 "\n",
794 ret, seq_iter);
795 return ret;
Blue Swirlad960902010-03-29 19:23:52 +0000796}
797
Juan Quintela7908c782012-06-26 18:46:10 +0200798SaveVMHandlers savevm_ram_handlers = {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200799 .save_live_setup = ram_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +0200800 .save_live_iterate = ram_save_iterate,
801 .save_live_complete = ram_save_complete,
Juan Quintela7908c782012-06-26 18:46:10 +0200802 .load_state = ram_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200803 .cancel = ram_migration_cancel,
Juan Quintela7908c782012-06-26 18:46:10 +0200804};
805
Blue Swirlad960902010-03-29 19:23:52 +0000806#ifdef HAS_AUDIO
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900807struct soundhw {
808 const char *name;
809 const char *descr;
810 int enabled;
811 int isa;
812 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100813 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900814 int (*init_pci) (PCIBus *bus);
815 } init;
816};
817
818static struct soundhw soundhw[] = {
Blue Swirlad960902010-03-29 19:23:52 +0000819#ifdef HAS_AUDIO_CHOICE
Hervé Poussineauda128722012-04-14 22:51:33 +0200820#ifdef CONFIG_PCSPK
Blue Swirlad960902010-03-29 19:23:52 +0000821 {
822 "pcspk",
823 "PC speaker",
824 0,
825 1,
826 { .init_isa = pcspk_audio_init }
827 },
828#endif
829
830#ifdef CONFIG_SB16
831 {
832 "sb16",
833 "Creative Sound Blaster 16",
834 0,
835 1,
836 { .init_isa = SB16_init }
837 },
838#endif
839
840#ifdef CONFIG_CS4231A
841 {
842 "cs4231a",
843 "CS4231A",
844 0,
845 1,
846 { .init_isa = cs4231a_init }
847 },
848#endif
849
850#ifdef CONFIG_ADLIB
851 {
852 "adlib",
853#ifdef HAS_YMF262
854 "Yamaha YMF262 (OPL3)",
855#else
856 "Yamaha YM3812 (OPL2)",
857#endif
858 0,
859 1,
860 { .init_isa = Adlib_init }
861 },
862#endif
863
864#ifdef CONFIG_GUS
865 {
866 "gus",
867 "Gravis Ultrasound GF1",
868 0,
869 1,
870 { .init_isa = GUS_init }
871 },
872#endif
873
874#ifdef CONFIG_AC97
875 {
876 "ac97",
877 "Intel 82801AA AC97 Audio",
878 0,
879 0,
880 { .init_pci = ac97_init }
881 },
882#endif
883
884#ifdef CONFIG_ES1370
885 {
886 "es1370",
887 "ENSONIQ AudioPCI ES1370",
888 0,
889 0,
890 { .init_pci = es1370_init }
891 },
892#endif
893
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100894#ifdef CONFIG_HDA
895 {
896 "hda",
897 "Intel HD Audio",
898 0,
899 0,
900 { .init_pci = intel_hda_and_codec_init }
901 },
902#endif
903
Blue Swirlad960902010-03-29 19:23:52 +0000904#endif /* HAS_AUDIO_CHOICE */
905
906 { NULL, NULL, 0, 0, { NULL } }
907};
908
909void select_soundhw(const char *optarg)
910{
911 struct soundhw *c;
912
Peter Maydellc8057f92012-08-02 13:45:54 +0100913 if (is_help_option(optarg)) {
Blue Swirlad960902010-03-29 19:23:52 +0000914 show_valid_cards:
915
916 printf("Valid sound card names (comma separated):\n");
917 for (c = soundhw; c->name; ++c) {
918 printf ("%-11s %s\n", c->name, c->descr);
919 }
920 printf("\n-soundhw all will enable all of the above\n");
Peter Maydellc8057f92012-08-02 13:45:54 +0100921 exit(!is_help_option(optarg));
Blue Swirlad960902010-03-29 19:23:52 +0000922 }
923 else {
924 size_t l;
925 const char *p;
926 char *e;
927 int bad_card = 0;
928
929 if (!strcmp(optarg, "all")) {
930 for (c = soundhw; c->name; ++c) {
931 c->enabled = 1;
932 }
933 return;
934 }
935
936 p = optarg;
937 while (*p) {
938 e = strchr(p, ',');
939 l = !e ? strlen(p) : (size_t) (e - p);
940
941 for (c = soundhw; c->name; ++c) {
942 if (!strncmp(c->name, p, l) && !c->name[l]) {
943 c->enabled = 1;
944 break;
945 }
946 }
947
948 if (!c->name) {
949 if (l > 80) {
950 fprintf(stderr,
951 "Unknown sound card name (too big to show)\n");
952 }
953 else {
954 fprintf(stderr, "Unknown sound card name `%.*s'\n",
955 (int) l, p);
956 }
957 bad_card = 1;
958 }
959 p += l + (e != NULL);
960 }
961
962 if (bad_card) {
963 goto show_valid_cards;
964 }
965 }
966}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900967
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100968void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900969{
970 struct soundhw *c;
971
972 for (c = soundhw; c->name; ++c) {
973 if (c->enabled) {
974 if (c->isa) {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100975 if (isa_bus) {
976 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900977 }
978 } else {
979 if (pci_bus) {
980 c->init.init_pci(pci_bus);
981 }
982 }
983 }
984 }
985}
Blue Swirlad960902010-03-29 19:23:52 +0000986#else
987void select_soundhw(const char *optarg)
988{
989}
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100990void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900991{
992}
Blue Swirlad960902010-03-29 19:23:52 +0000993#endif
994
995int qemu_uuid_parse(const char *str, uint8_t *uuid)
996{
997 int ret;
998
999 if (strlen(str) != 36) {
1000 return -1;
1001 }
1002
1003 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1004 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1005 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1006 &uuid[15]);
1007
1008 if (ret != 16) {
1009 return -1;
1010 }
1011#ifdef TARGET_I386
1012 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
1013#endif
1014 return 0;
1015}
1016
1017void do_acpitable_option(const char *optarg)
1018{
1019#ifdef TARGET_I386
1020 if (acpi_table_add(optarg) < 0) {
1021 fprintf(stderr, "Wrong acpi table provided\n");
1022 exit(1);
1023 }
1024#endif
1025}
1026
1027void do_smbios_option(const char *optarg)
1028{
1029#ifdef TARGET_I386
1030 if (smbios_entry_add(optarg) < 0) {
1031 fprintf(stderr, "Wrong smbios provided\n");
1032 exit(1);
1033 }
1034#endif
1035}
1036
1037void cpudef_init(void)
1038{
1039#if defined(cpudef_setup)
1040 cpudef_setup(); /* parse cpu definitions in target config file */
1041#endif
1042}
1043
1044int audio_available(void)
1045{
1046#ifdef HAS_AUDIO
1047 return 1;
1048#else
1049 return 0;
1050#endif
1051}
1052
Anthony PERARD303d4e82010-09-21 20:05:31 +01001053int tcg_available(void)
1054{
1055 return 1;
1056}
1057
Blue Swirlad960902010-03-29 19:23:52 +00001058int kvm_available(void)
1059{
1060#ifdef CONFIG_KVM
1061 return 1;
1062#else
1063 return 0;
1064#endif
1065}
1066
1067int xen_available(void)
1068{
1069#ifdef CONFIG_XEN
1070 return 1;
1071#else
1072 return 0;
1073#endif
1074}