blob: 7fbfe91ad50f1a1242510cbc4ad2f002e856d9ec [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 Wasserman0c51f432012-06-19 18:43:14 +0300204static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
205 int cont, int flag)
206{
207 qemu_put_be64(f, offset | cont | flag);
208 if (!cont) {
209 qemu_put_byte(f, strlen(block->idstr));
210 qemu_put_buffer(f, (uint8_t *)block->idstr,
211 strlen(block->idstr));
212 }
213
214}
215
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300216#define ENCODING_FLAG_XBZRLE 0x1
217
218static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
219 ram_addr_t current_addr, RAMBlock *block,
220 ram_addr_t offset, int cont)
221{
222 int encoded_len = 0, bytes_sent = -1;
223 uint8_t *prev_cached_page;
224
225 if (!cache_is_cached(XBZRLE.cache, current_addr)) {
226 cache_insert(XBZRLE.cache, current_addr,
227 g_memdup(current_data, TARGET_PAGE_SIZE));
228 return -1;
229 }
230
231 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
232
233 /* save current buffer into memory */
234 memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
235
236 /* XBZRLE encoding (if there is no overflow) */
237 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
238 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
239 TARGET_PAGE_SIZE);
240 if (encoded_len == 0) {
241 DPRINTF("Skipping unmodified page\n");
242 return 0;
243 } else if (encoded_len == -1) {
244 DPRINTF("Overflow\n");
245 /* update data in the cache */
246 memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
247 return -1;
248 }
249
250 /* we need to update the data in the cache, in order to get the same data */
251 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
252
253 /* Send XBZRLE based compressed page */
254 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
255 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
256 qemu_put_be16(f, encoded_len);
257 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
258 bytes_sent = encoded_len + 1 + 2;
259
260 return bytes_sent;
261}
262
Alex Williamson760e77e2010-08-19 10:18:42 -0300263static RAMBlock *last_block;
264static ram_addr_t last_offset;
265
Orit Wasserman6c779f22012-07-10 12:37:13 +0300266/*
267 * ram_save_block: Writes a page of memory to the stream f
268 *
269 * Returns: 0: if the page hasn't changed
270 * -1: if there are no more dirty pages
271 * n: the amount of bytes written in other case
272 */
273
Blue Swirlad960902010-03-29 19:23:52 +0000274static int ram_save_block(QEMUFile *f)
275{
Alex Williamsone44359c2010-06-25 11:09:57 -0600276 RAMBlock *block = last_block;
277 ram_addr_t offset = last_offset;
Orit Wasserman6c779f22012-07-10 12:37:13 +0300278 int bytes_sent = -1;
Avi Kivity71c510e2011-12-21 13:11:22 +0200279 MemoryRegion *mr;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300280 ram_addr_t current_addr;
Blue Swirlad960902010-03-29 19:23:52 +0000281
Alex Williamsone44359c2010-06-25 11:09:57 -0600282 if (!block)
283 block = QLIST_FIRST(&ram_list.blocks);
284
Alex Williamsone44359c2010-06-25 11:09:57 -0600285 do {
Avi Kivity71c510e2011-12-21 13:11:22 +0200286 mr = block->mr;
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000287 if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
288 DIRTY_MEMORY_MIGRATION)) {
Blue Swirlad960902010-03-29 19:23:52 +0000289 uint8_t *p;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600290 int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000291
Avi Kivity71c510e2011-12-21 13:11:22 +0200292 memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
293 DIRTY_MEMORY_MIGRATION);
Blue Swirlad960902010-03-29 19:23:52 +0000294
Avi Kivity71c510e2011-12-21 13:11:22 +0200295 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000296
Paolo Bonzini86003612011-12-23 16:17:26 +0100297 if (is_dup_page(p)) {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300298 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
Blue Swirlad960902010-03-29 19:23:52 +0000299 qemu_put_byte(f, *p);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200300 bytes_sent = 1;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300301 } else if (migrate_use_xbzrle()) {
302 current_addr = block->offset + offset;
303 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
304 offset, cont);
305 p = get_cached_data(XBZRLE.cache, current_addr);
306 }
307
308 /* either we didn't send yet (we may have had XBZRLE overflow) */
309 if (bytes_sent == -1) {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300310 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Blue Swirlad960902010-03-29 19:23:52 +0000311 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200312 bytes_sent = TARGET_PAGE_SIZE;
Blue Swirlad960902010-03-29 19:23:52 +0000313 }
314
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300315 /* if page is unmodified, continue to the next */
316 if (bytes_sent != 0) {
317 break;
318 }
Blue Swirlad960902010-03-29 19:23:52 +0000319 }
Alex Williamsone44359c2010-06-25 11:09:57 -0600320
321 offset += TARGET_PAGE_SIZE;
322 if (offset >= block->length) {
323 offset = 0;
324 block = QLIST_NEXT(block, next);
325 if (!block)
326 block = QLIST_FIRST(&ram_list.blocks);
327 }
Avi Kivity71c510e2011-12-21 13:11:22 +0200328 } while (block != last_block || offset != last_offset);
Alex Williamsone44359c2010-06-25 11:09:57 -0600329
330 last_block = block;
331 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000332
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200333 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000334}
335
336static uint64_t bytes_transferred;
337
338static ram_addr_t ram_save_remaining(void)
339{
Juan Quintela45f33f02012-06-22 15:21:07 +0200340 return ram_list.dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000341}
342
343uint64_t ram_bytes_remaining(void)
344{
345 return ram_save_remaining() * TARGET_PAGE_SIZE;
346}
347
348uint64_t ram_bytes_transferred(void)
349{
350 return bytes_transferred;
351}
352
353uint64_t ram_bytes_total(void)
354{
Alex Williamsond17b5282010-06-25 11:08:38 -0600355 RAMBlock *block;
356 uint64_t total = 0;
357
358 QLIST_FOREACH(block, &ram_list.blocks, next)
359 total += block->length;
360
361 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000362}
363
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200364static int block_compar(const void *a, const void *b)
365{
366 RAMBlock * const *ablock = a;
367 RAMBlock * const *bblock = b;
Avi Kivity8fec98b2011-12-21 13:22:16 +0200368
369 return strcmp((*ablock)->idstr, (*bblock)->idstr);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200370}
371
372static void sort_ram_list(void)
373{
374 RAMBlock *block, *nblock, **blocks;
375 int n;
376 n = 0;
377 QLIST_FOREACH(block, &ram_list.blocks, next) {
378 ++n;
379 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500380 blocks = g_malloc(n * sizeof *blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200381 n = 0;
382 QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
383 blocks[n++] = block;
384 QLIST_REMOVE(block, next);
385 }
386 qsort(blocks, n, sizeof *blocks, block_compar);
387 while (--n >= 0) {
388 QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
389 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500390 g_free(blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200391}
392
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300393static void migration_end(void)
394{
395 memory_global_dirty_log_stop();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300396
397 if (migrate_use_xbzrle()) {
398 cache_fini(XBZRLE.cache);
399 g_free(XBZRLE.cache);
400 g_free(XBZRLE.encoded_buf);
401 g_free(XBZRLE.current_buf);
402 g_free(XBZRLE.decoded_buf);
403 XBZRLE.cache = NULL;
404 }
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300405}
406
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200407static void ram_migration_cancel(void *opaque)
408{
409 migration_end();
410}
411
Juan Quintela4508bd92012-05-22 16:27:59 +0200412#define MAX_WAIT 50 /* ms, half buffered_file limit */
413
Juan Quintelad1315aa2012-06-28 15:11:57 +0200414static int ram_save_setup(QEMUFile *f, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000415{
416 ram_addr_t addr;
Juan Quintelad1315aa2012-06-28 15:11:57 +0200417 RAMBlock *block;
Blue Swirlad960902010-03-29 19:23:52 +0000418
Juan Quintelad1315aa2012-06-28 15:11:57 +0200419 bytes_transferred = 0;
420 last_block = NULL;
421 last_offset = 0;
422 sort_ram_list();
Blue Swirlad960902010-03-29 19:23:52 +0000423
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300424 if (migrate_use_xbzrle()) {
425 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
426 TARGET_PAGE_SIZE,
427 TARGET_PAGE_SIZE);
428 if (!XBZRLE.cache) {
429 DPRINTF("Error creating cache\n");
430 return -1;
431 }
432 XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
433 XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
434 }
435
Juan Quintelad1315aa2012-06-28 15:11:57 +0200436 /* Make sure all dirty bits are set */
437 QLIST_FOREACH(block, &ram_list.blocks, next) {
438 for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
439 if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE,
440 DIRTY_MEMORY_MIGRATION)) {
441 memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000442 }
443 }
Blue Swirlad960902010-03-29 19:23:52 +0000444 }
445
Juan Quintelad1315aa2012-06-28 15:11:57 +0200446 memory_global_dirty_log_start();
447
448 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
449
450 QLIST_FOREACH(block, &ram_list.blocks, next) {
451 qemu_put_byte(f, strlen(block->idstr));
452 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
453 qemu_put_be64(f, block->length);
454 }
455
Juan Quintelad1315aa2012-06-28 15:11:57 +0200456 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
457
458 return 0;
459}
460
Juan Quintela16310a32012-06-28 15:31:37 +0200461static int ram_save_iterate(QEMUFile *f, void *opaque)
Juan Quintelad1315aa2012-06-28 15:11:57 +0200462{
Blue Swirlad960902010-03-29 19:23:52 +0000463 uint64_t bytes_transferred_last;
464 double bwidth = 0;
465 int ret;
466 int i;
Juan Quintela16310a32012-06-28 15:31:37 +0200467 uint64_t expected_time;
Blue Swirlad960902010-03-29 19:23:52 +0000468
469 bytes_transferred_last = bytes_transferred;
470 bwidth = qemu_get_clock_ns(rt_clock);
471
Juan Quintela4508bd92012-05-22 16:27:59 +0200472 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200473 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200474 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000475
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200476 bytes_sent = ram_save_block(f);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300477 /* no more blocks to sent */
478 if (bytes_sent < 0) {
Blue Swirlad960902010-03-29 19:23:52 +0000479 break;
480 }
Orit Wasserman6c779f22012-07-10 12:37:13 +0300481 bytes_transferred += bytes_sent;
Juan Quintela4508bd92012-05-22 16:27:59 +0200482 /* we want to check in the 1st loop, just in case it was the 1st time
483 and we had to sync the dirty bitmap.
484 qemu_get_clock_ns() is a bit expensive, so we only check each some
485 iterations
486 */
487 if ((i & 63) == 0) {
488 uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000;
489 if (t1 > MAX_WAIT) {
490 DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n",
491 t1, i);
492 break;
493 }
494 }
495 i++;
Blue Swirlad960902010-03-29 19:23:52 +0000496 }
497
Juan Quintela29757252011-10-19 15:22:18 +0200498 if (ret < 0) {
499 return ret;
500 }
501
Blue Swirlad960902010-03-29 19:23:52 +0000502 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
503 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
504
505 /* if we haven't transferred anything this round, force expected_time to a
506 * a very high value, but without crashing */
507 if (bwidth == 0) {
508 bwidth = 0.000001;
509 }
510
Blue Swirlad960902010-03-29 19:23:52 +0000511 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
512
Juan Quintela16310a32012-06-28 15:31:37 +0200513 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
Blue Swirlad960902010-03-29 19:23:52 +0000514
Juan Quintela16310a32012-06-28 15:31:37 +0200515 DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n",
516 expected_time, migrate_max_downtime());
517
Juan Quintela00d94f32012-06-28 19:51:17 +0200518 if (expected_time <= migrate_max_downtime()) {
519 memory_global_sync_dirty_bitmap(get_system_memory());
520 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300521
Juan Quintela5b3c9632012-05-22 00:44:24 +0200522 return expected_time <= migrate_max_downtime();
523 }
524 return 0;
Blue Swirlad960902010-03-29 19:23:52 +0000525}
526
Juan Quintela16310a32012-06-28 15:31:37 +0200527static int ram_save_complete(QEMUFile *f, void *opaque)
528{
Juan Quintela16310a32012-06-28 15:31:37 +0200529 memory_global_sync_dirty_bitmap(get_system_memory());
530
Juan Quintela16310a32012-06-28 15:31:37 +0200531 /* try transferring iterative blocks of memory */
532
533 /* flush all remaining blocks regardless of rate limiting */
Orit Wasserman6c779f22012-07-10 12:37:13 +0300534 while (true) {
535 int bytes_sent;
536
537 bytes_sent = ram_save_block(f);
538 /* no more blocks to sent */
539 if (bytes_sent < 0) {
540 break;
541 }
Juan Quintela16310a32012-06-28 15:31:37 +0200542 bytes_transferred += bytes_sent;
543 }
544 memory_global_dirty_log_stop();
545
Blue Swirlad960902010-03-29 19:23:52 +0000546 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
547
Alex Williamsona55bbe32010-06-25 11:10:05 -0600548 return 0;
549}
550
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300551static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
552{
553 int ret, rc = 0;
554 unsigned int xh_len;
555 int xh_flags;
556
557 if (!XBZRLE.decoded_buf) {
558 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
559 }
560
561 /* extract RLE header */
562 xh_flags = qemu_get_byte(f);
563 xh_len = qemu_get_be16(f);
564
565 if (xh_flags != ENCODING_FLAG_XBZRLE) {
566 fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
567 return -1;
568 }
569
570 if (xh_len > TARGET_PAGE_SIZE) {
571 fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
572 return -1;
573 }
574 /* load data and decode */
575 qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
576
577 /* decode RLE */
578 ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
579 TARGET_PAGE_SIZE);
580 if (ret == -1) {
581 fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
582 rc = -1;
583 } else if (ret > TARGET_PAGE_SIZE) {
584 fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
585 ret, TARGET_PAGE_SIZE);
586 abort();
587 }
588
589 return rc;
590}
591
Alex Williamsona55bbe32010-06-25 11:10:05 -0600592static inline void *host_from_stream_offset(QEMUFile *f,
593 ram_addr_t offset,
594 int flags)
595{
596 static RAMBlock *block = NULL;
597 char id[256];
598 uint8_t len;
599
600 if (flags & RAM_SAVE_FLAG_CONTINUE) {
601 if (!block) {
602 fprintf(stderr, "Ack, bad migration stream!\n");
603 return NULL;
604 }
605
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200606 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600607 }
608
609 len = qemu_get_byte(f);
610 qemu_get_buffer(f, (uint8_t *)id, len);
611 id[len] = 0;
612
613 QLIST_FOREACH(block, &ram_list.blocks, next) {
614 if (!strncmp(id, block->idstr, sizeof(id)))
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200615 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600616 }
617
618 fprintf(stderr, "Can't find block %s!\n", id);
619 return NULL;
620}
621
Juan Quintela7908c782012-06-26 18:46:10 +0200622static int ram_load(QEMUFile *f, void *opaque, int version_id)
Blue Swirlad960902010-03-29 19:23:52 +0000623{
624 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300625 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200626 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300627 static uint64_t seq_iter;
628
629 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000630
Avi Kivityf09f2182011-12-21 13:37:56 +0200631 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000632 return -EINVAL;
633 }
634
635 do {
636 addr = qemu_get_be64(f);
637
638 flags = addr & ~TARGET_PAGE_MASK;
639 addr &= TARGET_PAGE_MASK;
640
641 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200642 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600643 /* Synchronize RAM block list */
644 char id[256];
645 ram_addr_t length;
646 ram_addr_t total_ram_bytes = addr;
647
648 while (total_ram_bytes) {
649 RAMBlock *block;
650 uint8_t len;
651
652 len = qemu_get_byte(f);
653 qemu_get_buffer(f, (uint8_t *)id, len);
654 id[len] = 0;
655 length = qemu_get_be64(f);
656
657 QLIST_FOREACH(block, &ram_list.blocks, next) {
658 if (!strncmp(id, block->idstr, sizeof(id))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300659 if (block->length != length) {
660 ret = -EINVAL;
661 goto done;
662 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600663 break;
664 }
665 }
666
667 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600668 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
669 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300670 ret = -EINVAL;
671 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600672 }
673
674 total_ram_bytes -= length;
675 }
Blue Swirlad960902010-03-29 19:23:52 +0000676 }
677 }
678
679 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600680 void *host;
681 uint8_t ch;
682
Avi Kivityf09f2182011-12-21 13:37:56 +0200683 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +0200684 if (!host) {
685 return -EINVAL;
686 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600687
Alex Williamson97ab12d2010-06-25 11:09:50 -0600688 ch = qemu_get_byte(f);
689 memset(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000690#ifndef _WIN32
691 if (ch == 0 &&
692 (!kvm_enabled() || kvm_has_sync_mmu())) {
Andreas Färbere78815a2010-09-25 11:26:05 +0000693 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
Blue Swirlad960902010-03-29 19:23:52 +0000694 }
695#endif
696 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600697 void *host;
698
Avi Kivityf09f2182011-12-21 13:37:56 +0200699 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +0300700 if (!host) {
701 return -EINVAL;
702 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600703
Alex Williamson97ab12d2010-06-25 11:09:50 -0600704 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300705 } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
706 if (!migrate_use_xbzrle()) {
707 return -EINVAL;
708 }
709 void *host = host_from_stream_offset(f, addr, flags);
710 if (!host) {
711 return -EINVAL;
712 }
713
714 if (load_xbzrle(f, addr, host) < 0) {
715 ret = -EINVAL;
716 goto done;
717 }
Blue Swirlad960902010-03-29 19:23:52 +0000718 }
Juan Quintela42802d42011-10-05 01:14:46 +0200719 error = qemu_file_get_error(f);
720 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300721 ret = error;
722 goto done;
Blue Swirlad960902010-03-29 19:23:52 +0000723 }
724 } while (!(flags & RAM_SAVE_FLAG_EOS));
725
Orit Wasserman3a697f62012-06-19 18:43:15 +0300726done:
727 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64 "\n",
728 ret, seq_iter);
729 return ret;
Blue Swirlad960902010-03-29 19:23:52 +0000730}
731
Juan Quintela7908c782012-06-26 18:46:10 +0200732SaveVMHandlers savevm_ram_handlers = {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200733 .save_live_setup = ram_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +0200734 .save_live_iterate = ram_save_iterate,
735 .save_live_complete = ram_save_complete,
Juan Quintela7908c782012-06-26 18:46:10 +0200736 .load_state = ram_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200737 .cancel = ram_migration_cancel,
Juan Quintela7908c782012-06-26 18:46:10 +0200738};
739
Blue Swirlad960902010-03-29 19:23:52 +0000740#ifdef HAS_AUDIO
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900741struct soundhw {
742 const char *name;
743 const char *descr;
744 int enabled;
745 int isa;
746 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100747 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900748 int (*init_pci) (PCIBus *bus);
749 } init;
750};
751
752static struct soundhw soundhw[] = {
Blue Swirlad960902010-03-29 19:23:52 +0000753#ifdef HAS_AUDIO_CHOICE
Hervé Poussineauda128722012-04-14 22:51:33 +0200754#ifdef CONFIG_PCSPK
Blue Swirlad960902010-03-29 19:23:52 +0000755 {
756 "pcspk",
757 "PC speaker",
758 0,
759 1,
760 { .init_isa = pcspk_audio_init }
761 },
762#endif
763
764#ifdef CONFIG_SB16
765 {
766 "sb16",
767 "Creative Sound Blaster 16",
768 0,
769 1,
770 { .init_isa = SB16_init }
771 },
772#endif
773
774#ifdef CONFIG_CS4231A
775 {
776 "cs4231a",
777 "CS4231A",
778 0,
779 1,
780 { .init_isa = cs4231a_init }
781 },
782#endif
783
784#ifdef CONFIG_ADLIB
785 {
786 "adlib",
787#ifdef HAS_YMF262
788 "Yamaha YMF262 (OPL3)",
789#else
790 "Yamaha YM3812 (OPL2)",
791#endif
792 0,
793 1,
794 { .init_isa = Adlib_init }
795 },
796#endif
797
798#ifdef CONFIG_GUS
799 {
800 "gus",
801 "Gravis Ultrasound GF1",
802 0,
803 1,
804 { .init_isa = GUS_init }
805 },
806#endif
807
808#ifdef CONFIG_AC97
809 {
810 "ac97",
811 "Intel 82801AA AC97 Audio",
812 0,
813 0,
814 { .init_pci = ac97_init }
815 },
816#endif
817
818#ifdef CONFIG_ES1370
819 {
820 "es1370",
821 "ENSONIQ AudioPCI ES1370",
822 0,
823 0,
824 { .init_pci = es1370_init }
825 },
826#endif
827
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100828#ifdef CONFIG_HDA
829 {
830 "hda",
831 "Intel HD Audio",
832 0,
833 0,
834 { .init_pci = intel_hda_and_codec_init }
835 },
836#endif
837
Blue Swirlad960902010-03-29 19:23:52 +0000838#endif /* HAS_AUDIO_CHOICE */
839
840 { NULL, NULL, 0, 0, { NULL } }
841};
842
843void select_soundhw(const char *optarg)
844{
845 struct soundhw *c;
846
Peter Maydellc8057f92012-08-02 13:45:54 +0100847 if (is_help_option(optarg)) {
Blue Swirlad960902010-03-29 19:23:52 +0000848 show_valid_cards:
849
850 printf("Valid sound card names (comma separated):\n");
851 for (c = soundhw; c->name; ++c) {
852 printf ("%-11s %s\n", c->name, c->descr);
853 }
854 printf("\n-soundhw all will enable all of the above\n");
Peter Maydellc8057f92012-08-02 13:45:54 +0100855 exit(!is_help_option(optarg));
Blue Swirlad960902010-03-29 19:23:52 +0000856 }
857 else {
858 size_t l;
859 const char *p;
860 char *e;
861 int bad_card = 0;
862
863 if (!strcmp(optarg, "all")) {
864 for (c = soundhw; c->name; ++c) {
865 c->enabled = 1;
866 }
867 return;
868 }
869
870 p = optarg;
871 while (*p) {
872 e = strchr(p, ',');
873 l = !e ? strlen(p) : (size_t) (e - p);
874
875 for (c = soundhw; c->name; ++c) {
876 if (!strncmp(c->name, p, l) && !c->name[l]) {
877 c->enabled = 1;
878 break;
879 }
880 }
881
882 if (!c->name) {
883 if (l > 80) {
884 fprintf(stderr,
885 "Unknown sound card name (too big to show)\n");
886 }
887 else {
888 fprintf(stderr, "Unknown sound card name `%.*s'\n",
889 (int) l, p);
890 }
891 bad_card = 1;
892 }
893 p += l + (e != NULL);
894 }
895
896 if (bad_card) {
897 goto show_valid_cards;
898 }
899 }
900}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900901
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100902void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900903{
904 struct soundhw *c;
905
906 for (c = soundhw; c->name; ++c) {
907 if (c->enabled) {
908 if (c->isa) {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100909 if (isa_bus) {
910 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900911 }
912 } else {
913 if (pci_bus) {
914 c->init.init_pci(pci_bus);
915 }
916 }
917 }
918 }
919}
Blue Swirlad960902010-03-29 19:23:52 +0000920#else
921void select_soundhw(const char *optarg)
922{
923}
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100924void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900925{
926}
Blue Swirlad960902010-03-29 19:23:52 +0000927#endif
928
929int qemu_uuid_parse(const char *str, uint8_t *uuid)
930{
931 int ret;
932
933 if (strlen(str) != 36) {
934 return -1;
935 }
936
937 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
938 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
939 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
940 &uuid[15]);
941
942 if (ret != 16) {
943 return -1;
944 }
945#ifdef TARGET_I386
946 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
947#endif
948 return 0;
949}
950
951void do_acpitable_option(const char *optarg)
952{
953#ifdef TARGET_I386
954 if (acpi_table_add(optarg) < 0) {
955 fprintf(stderr, "Wrong acpi table provided\n");
956 exit(1);
957 }
958#endif
959}
960
961void do_smbios_option(const char *optarg)
962{
963#ifdef TARGET_I386
964 if (smbios_entry_add(optarg) < 0) {
965 fprintf(stderr, "Wrong smbios provided\n");
966 exit(1);
967 }
968#endif
969}
970
971void cpudef_init(void)
972{
973#if defined(cpudef_setup)
974 cpudef_setup(); /* parse cpu definitions in target config file */
975#endif
976}
977
978int audio_available(void)
979{
980#ifdef HAS_AUDIO
981 return 1;
982#else
983 return 0;
984#endif
985}
986
Anthony PERARD303d4e82010-09-21 20:05:31 +0100987int tcg_available(void)
988{
989 return 1;
990}
991
Blue Swirlad960902010-03-29 19:23:52 +0000992int kvm_available(void)
993{
994#ifdef CONFIG_KVM
995 return 1;
996#else
997 return 0;
998#endif
999}
1000
1001int xen_available(void)
1002{
1003#ifdef CONFIG_XEN
1004 return 1;
1005#else
1006 return 0;
1007#endif
1008}