blob: fe1727922cbaf120a7ff8c6985d3315cd9636f98 [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"
Paolo Bonzini83c90892012-12-17 18:19:49 +010032#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010034#include "qemu/bitops.h"
35#include "qemu/bitmap.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/arch_init.h"
Blue Swirlad960902010-03-29 19:23:52 +000037#include "audio/audio.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010038#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020039#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010040#include "hw/audio/audio.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010041#include "sysemu/kvm.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010042#include "migration/migration.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010043#include "hw/i386/smbios.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010044#include "exec/address-spaces.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010045#include "hw/audio/pcspk.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010046#include "migration/page_cache.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/config-file.h"
Daniel P. Berrange99afc912012-08-20 15:31:38 +010048#include "qmp-commands.h"
Juan Quintela3c121932012-09-04 13:08:57 +020049#include "trace.h"
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +010050#include "exec/cpu-all.h"
Juan Quintela12291ec2013-10-14 17:14:47 +020051#include "exec/ram_addr.h"
Michael S. Tsirkin04452592013-04-15 09:19:22 +030052#include "hw/acpi/acpi.h"
Juan Quintelaaa8dc042013-11-06 11:33:05 +010053#include "qemu/host-utils.h"
Blue Swirlad960902010-03-29 19:23:52 +000054
Orit Wasserman3a697f62012-06-19 18:43:15 +030055#ifdef DEBUG_ARCH_INIT
56#define DPRINTF(fmt, ...) \
57 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
58#else
59#define DPRINTF(fmt, ...) \
60 do { } while (0)
61#endif
62
Blue Swirlad960902010-03-29 19:23:52 +000063#ifdef TARGET_SPARC
64int graphic_width = 1024;
65int graphic_height = 768;
66int graphic_depth = 8;
67#else
68int graphic_width = 800;
69int graphic_height = 600;
Alexander Graff1ff0e82013-06-20 14:06:27 +020070int graphic_depth = 32;
Blue Swirlad960902010-03-29 19:23:52 +000071#endif
72
Blue Swirlad960902010-03-29 19:23:52 +000073
74#if defined(TARGET_ALPHA)
75#define QEMU_ARCH QEMU_ARCH_ALPHA
76#elif defined(TARGET_ARM)
77#define QEMU_ARCH QEMU_ARCH_ARM
78#elif defined(TARGET_CRIS)
79#define QEMU_ARCH QEMU_ARCH_CRIS
80#elif defined(TARGET_I386)
81#define QEMU_ARCH QEMU_ARCH_I386
82#elif defined(TARGET_M68K)
83#define QEMU_ARCH QEMU_ARCH_M68K
Michael Walle81ea0e12011-02-17 23:45:02 +010084#elif defined(TARGET_LM32)
85#define QEMU_ARCH QEMU_ARCH_LM32
Blue Swirlad960902010-03-29 19:23:52 +000086#elif defined(TARGET_MICROBLAZE)
87#define QEMU_ARCH QEMU_ARCH_MICROBLAZE
88#elif defined(TARGET_MIPS)
89#define QEMU_ARCH QEMU_ARCH_MIPS
Anthony Greend15a9c22013-03-18 15:49:25 -040090#elif defined(TARGET_MOXIE)
91#define QEMU_ARCH QEMU_ARCH_MOXIE
Jia Liue67db062012-07-20 15:50:39 +080092#elif defined(TARGET_OPENRISC)
93#define QEMU_ARCH QEMU_ARCH_OPENRISC
Blue Swirlad960902010-03-29 19:23:52 +000094#elif defined(TARGET_PPC)
95#define QEMU_ARCH QEMU_ARCH_PPC
96#elif defined(TARGET_S390X)
97#define QEMU_ARCH QEMU_ARCH_S390X
98#elif defined(TARGET_SH4)
99#define QEMU_ARCH QEMU_ARCH_SH4
100#elif defined(TARGET_SPARC)
101#define QEMU_ARCH QEMU_ARCH_SPARC
Max Filippov23288262011-09-06 03:55:25 +0400102#elif defined(TARGET_XTENSA)
103#define QEMU_ARCH QEMU_ARCH_XTENSA
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800104#elif defined(TARGET_UNICORE32)
105#define QEMU_ARCH QEMU_ARCH_UNICORE32
Blue Swirlad960902010-03-29 19:23:52 +0000106#endif
107
108const uint32_t arch_type = QEMU_ARCH;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600109static bool mig_throttle_on;
110static int dirty_rate_high_cnt;
111static void check_guest_throttling(void);
Blue Swirlad960902010-03-29 19:23:52 +0000112
113/***********************************************************/
114/* ram save/restore */
115
Yoshiaki Tamurad20878d2010-08-18 13:30:12 +0900116#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
117#define RAM_SAVE_FLAG_COMPRESS 0x02
118#define RAM_SAVE_FLAG_MEM_SIZE 0x04
119#define RAM_SAVE_FLAG_PAGE 0x08
120#define RAM_SAVE_FLAG_EOS 0x10
121#define RAM_SAVE_FLAG_CONTINUE 0x20
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300122#define RAM_SAVE_FLAG_XBZRLE 0x40
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400123/* 0x80 is reserved in migration.h start with 0x100 next */
Blue Swirlad960902010-03-29 19:23:52 +0000124
Eduardo Habkost756557d2012-05-02 13:07:27 -0300125static struct defconfig_file {
126 const char *filename;
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300127 /* Indicates it is an user config file (disabled by -no-user-config) */
128 bool userconfig;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300129} default_config_files[] = {
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300130 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
Paolo Bonzini2e599152013-06-04 14:45:27 +0200131 { CONFIG_QEMU_CONFDIR "/target-" TARGET_NAME ".conf", true },
Eduardo Habkost756557d2012-05-02 13:07:27 -0300132 { NULL }, /* end of list */
133};
134
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000135static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE];
Eduardo Habkost756557d2012-05-02 13:07:27 -0300136
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300137int qemu_read_default_config_files(bool userconfig)
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300138{
139 int ret;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300140 struct defconfig_file *f;
141
142 for (f = default_config_files; f->filename; f++) {
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300143 if (!userconfig && f->userconfig) {
144 continue;
145 }
Eduardo Habkost756557d2012-05-02 13:07:27 -0300146 ret = qemu_read_config_file(f->filename);
147 if (ret < 0 && ret != -ENOENT) {
148 return ret;
149 }
150 }
Laszlo Ersek4d8b3c62013-03-21 00:23:13 +0100151
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300152 return 0;
153}
154
Isaku Yamahatadc3c26a2013-09-21 01:23:36 +0900155static inline bool is_zero_range(uint8_t *p, uint64_t size)
Blue Swirlad960902010-03-29 19:23:52 +0000156{
Isaku Yamahatadc3c26a2013-09-21 01:23:36 +0900157 return buffer_find_nonzero_offset(p, size) == size;
Blue Swirlad960902010-03-29 19:23:52 +0000158}
159
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300160/* struct contains XBZRLE cache and a static page
161 used by the compression */
162static struct {
163 /* buffer used for XBZRLE encoding */
164 uint8_t *encoded_buf;
165 /* buffer for storing page content */
166 uint8_t *current_buf;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300167 /* Cache for XBZRLE */
168 PageCache *cache;
169} XBZRLE = {
170 .encoded_buf = NULL,
171 .current_buf = NULL,
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300172 .cache = NULL,
173};
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200174/* buffer used for XBZRLE decoding */
175static uint8_t *xbzrle_decoded_buf;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300176
177int64_t xbzrle_cache_resize(int64_t new_size)
178{
Orit Wassermanc91e6812014-01-30 20:08:34 +0200179 if (new_size < TARGET_PAGE_SIZE) {
180 return -1;
181 }
182
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300183 if (XBZRLE.cache != NULL) {
184 return cache_resize(XBZRLE.cache, new_size / TARGET_PAGE_SIZE) *
185 TARGET_PAGE_SIZE;
186 }
187 return pow2floor(new_size);
188}
189
Orit Wasserman004d4c12012-08-06 21:42:56 +0300190/* accounting for migration statistics */
191typedef struct AccountingInfo {
192 uint64_t dup_pages;
Peter Lievenf1c72792013-03-26 10:58:37 +0100193 uint64_t skipped_pages;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300194 uint64_t norm_pages;
195 uint64_t iterations;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300196 uint64_t xbzrle_bytes;
197 uint64_t xbzrle_pages;
198 uint64_t xbzrle_cache_miss;
199 uint64_t xbzrle_overflows;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300200} AccountingInfo;
201
202static AccountingInfo acct_info;
203
204static void acct_clear(void)
205{
206 memset(&acct_info, 0, sizeof(acct_info));
207}
208
209uint64_t dup_mig_bytes_transferred(void)
210{
211 return acct_info.dup_pages * TARGET_PAGE_SIZE;
212}
213
214uint64_t dup_mig_pages_transferred(void)
215{
216 return acct_info.dup_pages;
217}
218
Peter Lievenf1c72792013-03-26 10:58:37 +0100219uint64_t skipped_mig_bytes_transferred(void)
220{
221 return acct_info.skipped_pages * TARGET_PAGE_SIZE;
222}
223
224uint64_t skipped_mig_pages_transferred(void)
225{
226 return acct_info.skipped_pages;
227}
228
Orit Wasserman004d4c12012-08-06 21:42:56 +0300229uint64_t norm_mig_bytes_transferred(void)
230{
231 return acct_info.norm_pages * TARGET_PAGE_SIZE;
232}
233
234uint64_t norm_mig_pages_transferred(void)
235{
236 return acct_info.norm_pages;
237}
238
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300239uint64_t xbzrle_mig_bytes_transferred(void)
240{
241 return acct_info.xbzrle_bytes;
242}
243
244uint64_t xbzrle_mig_pages_transferred(void)
245{
246 return acct_info.xbzrle_pages;
247}
248
249uint64_t xbzrle_mig_pages_cache_miss(void)
250{
251 return acct_info.xbzrle_cache_miss;
252}
253
254uint64_t xbzrle_mig_pages_overflow(void)
255{
256 return acct_info.xbzrle_overflows;
257}
258
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200259static size_t save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
260 int cont, int flag)
Orit Wasserman0c51f432012-06-19 18:43:14 +0300261{
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200262 size_t size;
Orit Wasserman0c51f432012-06-19 18:43:14 +0300263
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200264 qemu_put_be64(f, offset | cont | flag);
265 size = 8;
266
267 if (!cont) {
268 qemu_put_byte(f, strlen(block->idstr));
269 qemu_put_buffer(f, (uint8_t *)block->idstr,
270 strlen(block->idstr));
271 size += 1 + strlen(block->idstr);
272 }
273 return size;
Orit Wasserman0c51f432012-06-19 18:43:14 +0300274}
275
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000276/* This is the last block that we have visited serching for dirty pages
277 */
278static RAMBlock *last_seen_block;
279/* This is the last block from where we have sent data */
280static RAMBlock *last_sent_block;
281static ram_addr_t last_offset;
282static unsigned long *migration_bitmap;
283static uint64_t migration_dirty_pages;
284static uint32_t last_version;
285static bool ram_bulk_stage;
286
287/* Update the xbzrle cache to reflect a page that's been sent as all 0.
288 * The important thing is that a stale (not-yet-0'd) page be replaced
289 * by the new data.
290 * As a bonus, if the page wasn't in the cache it gets added so that
291 * when a small write is made into the 0'd page it gets XBZRLE sent
292 */
293static void xbzrle_cache_zero_page(ram_addr_t current_addr)
294{
295 if (ram_bulk_stage || !migrate_use_xbzrle()) {
296 return;
297 }
298
299 /* We don't care if this fails to allocate a new cache page
300 * as long as it updated an old one */
301 cache_insert(XBZRLE.cache, current_addr, ZERO_TARGET_PAGE);
302}
303
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300304#define ENCODING_FLAG_XBZRLE 0x1
305
306static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
307 ram_addr_t current_addr, RAMBlock *block,
Juan Quinteladd051c72012-08-06 21:42:58 +0300308 ram_addr_t offset, int cont, bool last_stage)
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300309{
310 int encoded_len = 0, bytes_sent = -1;
311 uint8_t *prev_cached_page;
312
313 if (!cache_is_cached(XBZRLE.cache, current_addr)) {
Juan Quinteladd051c72012-08-06 21:42:58 +0300314 if (!last_stage) {
Orit Wasserman89db9982014-01-30 20:08:38 +0200315 if (cache_insert(XBZRLE.cache, current_addr, current_data) == -1) {
316 return -1;
317 }
Juan Quinteladd051c72012-08-06 21:42:58 +0300318 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300319 acct_info.xbzrle_cache_miss++;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300320 return -1;
321 }
322
323 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
324
325 /* save current buffer into memory */
326 memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
327
328 /* XBZRLE encoding (if there is no overflow) */
329 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
330 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
331 TARGET_PAGE_SIZE);
332 if (encoded_len == 0) {
333 DPRINTF("Skipping unmodified page\n");
334 return 0;
335 } else if (encoded_len == -1) {
336 DPRINTF("Overflow\n");
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300337 acct_info.xbzrle_overflows++;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300338 /* update data in the cache */
339 memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
340 return -1;
341 }
342
343 /* we need to update the data in the cache, in order to get the same data */
Juan Quinteladd051c72012-08-06 21:42:58 +0300344 if (!last_stage) {
345 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
346 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300347
348 /* Send XBZRLE based compressed page */
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200349 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300350 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
351 qemu_put_be16(f, encoded_len);
352 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200353 bytes_sent += encoded_len + 1 + 2;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300354 acct_info.xbzrle_pages++;
355 acct_info.xbzrle_bytes += bytes_sent;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300356
357 return bytes_sent;
358}
359
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200360static inline
361ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
362 ram_addr_t start)
Juan Quintela69268cd2012-07-20 10:36:12 +0200363{
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200364 unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS;
365 unsigned long nr = base + (start >> TARGET_PAGE_BITS);
Michael S. Tsirkin0851c9f2013-08-19 17:26:52 +0300366 uint64_t mr_size = TARGET_PAGE_ALIGN(memory_region_size(mr));
367 unsigned long size = base + (mr_size >> TARGET_PAGE_BITS);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200368
Peter Lieven70c86522013-03-26 10:58:38 +0100369 unsigned long next;
370
371 if (ram_bulk_stage && nr > base) {
372 next = nr + 1;
373 } else {
374 next = find_next_bit(migration_bitmap, size, nr);
375 }
Juan Quintela69268cd2012-07-20 10:36:12 +0200376
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200377 if (next < size) {
378 clear_bit(next, migration_bitmap);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200379 migration_dirty_pages--;
Juan Quintela69268cd2012-07-20 10:36:12 +0200380 }
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200381 return (next - base) << TARGET_PAGE_BITS;
Juan Quintela69268cd2012-07-20 10:36:12 +0200382}
383
Juan Quintela791fa2a2013-11-05 16:47:20 +0100384static inline bool migration_bitmap_set_dirty(ram_addr_t addr)
Juan Quintelae44d26c2012-07-20 10:16:08 +0200385{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200386 bool ret;
Juan Quintela791fa2a2013-11-05 16:47:20 +0100387 int nr = addr >> TARGET_PAGE_BITS;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200388
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200389 ret = test_and_set_bit(nr, migration_bitmap);
390
391 if (!ret) {
392 migration_dirty_pages++;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200393 }
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200394 return ret;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200395}
396
Juan Quintela791fa2a2013-11-05 16:47:20 +0100397static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length)
398{
399 ram_addr_t addr;
Juan Quintelaaa8dc042013-11-06 11:33:05 +0100400 unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
Juan Quintela791fa2a2013-11-05 16:47:20 +0100401
Juan Quintelaaa8dc042013-11-06 11:33:05 +0100402 /* start address is aligned at the start of a word? */
403 if (((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) {
404 int k;
405 int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS);
406 unsigned long *src = ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION];
407
408 for (k = page; k < page + nr; k++) {
409 if (src[k]) {
410 unsigned long new_dirty;
411 new_dirty = ~migration_bitmap[k];
412 migration_bitmap[k] |= src[k];
413 new_dirty &= src[k];
414 migration_dirty_pages += ctpopl(new_dirty);
415 src[k] = 0;
416 }
417 }
418 } else {
419 for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
420 if (cpu_physical_memory_get_dirty(start + addr,
421 TARGET_PAGE_SIZE,
422 DIRTY_MEMORY_MIGRATION)) {
423 cpu_physical_memory_reset_dirty(start + addr,
424 TARGET_PAGE_SIZE,
425 DIRTY_MEMORY_MIGRATION);
426 migration_bitmap_set_dirty(start + addr);
427 }
Juan Quintela791fa2a2013-11-05 16:47:20 +0100428 }
429 }
430}
431
432
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100433/* Needs iothread lock! */
434
Juan Quinteladd2df732012-07-20 10:52:51 +0200435static void migration_bitmap_sync(void)
436{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200437 RAMBlock *block;
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200438 uint64_t num_dirty_pages_init = migration_dirty_pages;
Juan Quintela8d017192012-08-13 12:31:25 +0200439 MigrationState *s = migrate_get_current();
440 static int64_t start_time;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600441 static int64_t bytes_xfer_prev;
Juan Quintela8d017192012-08-13 12:31:25 +0200442 static int64_t num_dirty_pages_period;
443 int64_t end_time;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600444 int64_t bytes_xfer_now;
445
446 if (!bytes_xfer_prev) {
447 bytes_xfer_prev = ram_bytes_transferred();
448 }
Juan Quintela8d017192012-08-13 12:31:25 +0200449
450 if (!start_time) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100451 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela8d017192012-08-13 12:31:25 +0200452 }
Juan Quintela3c121932012-09-04 13:08:57 +0200453
454 trace_migration_bitmap_sync_start();
Paolo Bonzini1d671362013-04-24 10:46:55 +0200455 address_space_sync_dirty_bitmap(&address_space_memory);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200456
Paolo Bonzinia3161032012-11-14 15:54:48 +0100457 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Juan Quintela791fa2a2013-11-05 16:47:20 +0100458 migration_bitmap_sync_range(block->mr->ram_addr, block->length);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200459 }
460 trace_migration_bitmap_sync_end(migration_dirty_pages
Juan Quintela3c121932012-09-04 13:08:57 +0200461 - num_dirty_pages_init);
Juan Quintela8d017192012-08-13 12:31:25 +0200462 num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
Alex Blighbc72ad62013-08-21 16:03:08 +0100463 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela8d017192012-08-13 12:31:25 +0200464
465 /* more than 1 second = 1000 millisecons */
466 if (end_time > start_time + 1000) {
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600467 if (migrate_auto_converge()) {
468 /* The following detection logic can be refined later. For now:
469 Check to see if the dirtied bytes is 50% more than the approx.
470 amount of bytes that just got transferred since the last time we
471 were in this routine. If that happens >N times (for now N==4)
472 we turn on the throttle down logic */
473 bytes_xfer_now = ram_bytes_transferred();
474 if (s->dirty_pages_rate &&
475 (num_dirty_pages_period * TARGET_PAGE_SIZE >
476 (bytes_xfer_now - bytes_xfer_prev)/2) &&
477 (dirty_rate_high_cnt++ > 4)) {
478 trace_migration_throttle();
479 mig_throttle_on = true;
480 dirty_rate_high_cnt = 0;
481 }
482 bytes_xfer_prev = bytes_xfer_now;
483 } else {
484 mig_throttle_on = false;
485 }
Juan Quintela8d017192012-08-13 12:31:25 +0200486 s->dirty_pages_rate = num_dirty_pages_period * 1000
487 / (end_time - start_time);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100488 s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE;
Juan Quintela8d017192012-08-13 12:31:25 +0200489 start_time = end_time;
490 num_dirty_pages_period = 0;
491 }
Juan Quinteladd2df732012-07-20 10:52:51 +0200492}
493
Orit Wasserman6c779f22012-07-10 12:37:13 +0300494/*
495 * ram_save_block: Writes a page of memory to the stream f
496 *
Juan Quintelab823cea2012-12-10 13:27:50 +0100497 * Returns: The number of bytes written.
498 * 0 means no dirty pages
Orit Wasserman6c779f22012-07-10 12:37:13 +0300499 */
500
Juan Quinteladd051c72012-08-06 21:42:58 +0300501static int ram_save_block(QEMUFile *f, bool last_stage)
Blue Swirlad960902010-03-29 19:23:52 +0000502{
Juan Quintelab23a9a52012-10-17 20:08:04 +0200503 RAMBlock *block = last_seen_block;
Alex Williamsone44359c2010-06-25 11:09:57 -0600504 ram_addr_t offset = last_offset;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200505 bool complete_round = false;
Juan Quintelab823cea2012-12-10 13:27:50 +0100506 int bytes_sent = 0;
Avi Kivity71c510e2011-12-21 13:11:22 +0200507 MemoryRegion *mr;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300508 ram_addr_t current_addr;
Blue Swirlad960902010-03-29 19:23:52 +0000509
Alex Williamsone44359c2010-06-25 11:09:57 -0600510 if (!block)
Paolo Bonzinia3161032012-11-14 15:54:48 +0100511 block = QTAILQ_FIRST(&ram_list.blocks);
Alex Williamsone44359c2010-06-25 11:09:57 -0600512
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200513 while (true) {
Avi Kivity71c510e2011-12-21 13:11:22 +0200514 mr = block->mr;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200515 offset = migration_bitmap_find_and_reset_dirty(mr, offset);
516 if (complete_round && block == last_seen_block &&
517 offset >= last_offset) {
518 break;
519 }
520 if (offset >= block->length) {
521 offset = 0;
522 block = QTAILQ_NEXT(block, next);
523 if (!block) {
524 block = QTAILQ_FIRST(&ram_list.blocks);
525 complete_round = true;
Peter Lieven78d07ae2013-03-26 10:58:36 +0100526 ram_bulk_stage = false;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200527 }
528 } else {
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400529 int ret;
Blue Swirlad960902010-03-29 19:23:52 +0000530 uint8_t *p;
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000531 bool send_async = true;
Juan Quintela5f718a12012-10-17 20:10:55 +0200532 int cont = (block == last_sent_block) ?
Juan Quintelab23a9a52012-10-17 20:08:04 +0200533 RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000534
Avi Kivity71c510e2011-12-21 13:11:22 +0200535 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000536
Juan Quintelab823cea2012-12-10 13:27:50 +0100537 /* In doubt sent page as normal */
538 bytes_sent = -1;
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400539 ret = ram_control_save_page(f, block->offset,
540 offset, TARGET_PAGE_SIZE, &bytes_sent);
541
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000542 current_addr = block->offset + offset;
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400543 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
544 if (ret != RAM_SAVE_CONTROL_DELAYED) {
545 if (bytes_sent > 0) {
546 acct_info.norm_pages++;
547 } else if (bytes_sent == 0) {
548 acct_info.dup_pages++;
549 }
550 }
Isaku Yamahatadc3c26a2013-09-21 01:23:36 +0900551 } else if (is_zero_range(p, TARGET_PAGE_SIZE)) {
Orit Wasserman004d4c12012-08-06 21:42:56 +0300552 acct_info.dup_pages++;
Peter Lieven9ef051e2013-06-10 12:14:19 +0200553 bytes_sent = save_block_hdr(f, block, offset, cont,
554 RAM_SAVE_FLAG_COMPRESS);
555 qemu_put_byte(f, 0);
556 bytes_sent++;
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000557 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
558 * page would be stale
559 */
560 xbzrle_cache_zero_page(current_addr);
Peter Lieven5cc11c42013-03-26 10:58:39 +0100561 } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300562 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
Juan Quinteladd051c72012-08-06 21:42:58 +0300563 offset, cont, last_stage);
564 if (!last_stage) {
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000565 /* We must send exactly what's in the xbzrle cache
566 * even if the page wasn't xbzrle compressed, so that
567 * it's right next time.
568 */
Juan Quinteladd051c72012-08-06 21:42:58 +0300569 p = get_cached_data(XBZRLE.cache, current_addr);
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000570
571 /* Can't send this cached data async, since the cache page
572 * might get updated before it gets to the wire
573 */
574 send_async = false;
Juan Quinteladd051c72012-08-06 21:42:58 +0300575 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300576 }
577
Juan Quintelab823cea2012-12-10 13:27:50 +0100578 /* XBZRLE overflow or normal page */
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300579 if (bytes_sent == -1) {
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200580 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000581 if (send_async) {
582 qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
583 } else {
584 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
585 }
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200586 bytes_sent += TARGET_PAGE_SIZE;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300587 acct_info.norm_pages++;
Blue Swirlad960902010-03-29 19:23:52 +0000588 }
589
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300590 /* if page is unmodified, continue to the next */
Juan Quintelab823cea2012-12-10 13:27:50 +0100591 if (bytes_sent > 0) {
Juan Quintela5f718a12012-10-17 20:10:55 +0200592 last_sent_block = block;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300593 break;
594 }
Blue Swirlad960902010-03-29 19:23:52 +0000595 }
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200596 }
Juan Quintelab23a9a52012-10-17 20:08:04 +0200597 last_seen_block = block;
Alex Williamsone44359c2010-06-25 11:09:57 -0600598 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000599
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200600 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000601}
602
603static uint64_t bytes_transferred;
604
Michael R. Hines2b0ce072013-06-25 21:35:28 -0400605void acct_update_position(QEMUFile *f, size_t size, bool zero)
606{
607 uint64_t pages = size / TARGET_PAGE_SIZE;
608 if (zero) {
609 acct_info.dup_pages += pages;
610 } else {
611 acct_info.norm_pages += pages;
612 bytes_transferred += size;
613 qemu_update_position(f, size);
614 }
615}
616
Blue Swirlad960902010-03-29 19:23:52 +0000617static ram_addr_t ram_save_remaining(void)
618{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200619 return migration_dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000620}
621
622uint64_t ram_bytes_remaining(void)
623{
624 return ram_save_remaining() * TARGET_PAGE_SIZE;
625}
626
627uint64_t ram_bytes_transferred(void)
628{
629 return bytes_transferred;
630}
631
632uint64_t ram_bytes_total(void)
633{
Alex Williamsond17b5282010-06-25 11:08:38 -0600634 RAMBlock *block;
635 uint64_t total = 0;
636
Paolo Bonzinia3161032012-11-14 15:54:48 +0100637 QTAILQ_FOREACH(block, &ram_list.blocks, next)
Alex Williamsond17b5282010-06-25 11:08:38 -0600638 total += block->length;
639
640 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000641}
642
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200643void free_xbzrle_decoded_buf(void)
644{
645 g_free(xbzrle_decoded_buf);
646 xbzrle_decoded_buf = NULL;
647}
648
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300649static void migration_end(void)
650{
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100651 if (migration_bitmap) {
652 memory_global_dirty_log_stop();
653 g_free(migration_bitmap);
654 migration_bitmap = NULL;
655 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300656
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100657 if (XBZRLE.cache) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300658 cache_fini(XBZRLE.cache);
659 g_free(XBZRLE.cache);
660 g_free(XBZRLE.encoded_buf);
661 g_free(XBZRLE.current_buf);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300662 XBZRLE.cache = NULL;
Orit Wassermanf6c64832014-01-30 20:08:33 +0200663 XBZRLE.encoded_buf = NULL;
664 XBZRLE.current_buf = NULL;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300665 }
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300666}
667
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200668static void ram_migration_cancel(void *opaque)
669{
670 migration_end();
671}
672
Juan Quintela5a170772012-07-17 17:02:24 +0200673static void reset_ram_globals(void)
674{
Juan Quintelab23a9a52012-10-17 20:08:04 +0200675 last_seen_block = NULL;
Juan Quintela5f718a12012-10-17 20:10:55 +0200676 last_sent_block = NULL;
Juan Quintela5a170772012-07-17 17:02:24 +0200677 last_offset = 0;
Umesh Deshpandef798b072011-08-18 11:41:17 -0700678 last_version = ram_list.version;
Peter Lieven78d07ae2013-03-26 10:58:36 +0100679 ram_bulk_stage = true;
Juan Quintela5a170772012-07-17 17:02:24 +0200680}
681
Juan Quintela4508bd92012-05-22 16:27:59 +0200682#define MAX_WAIT 50 /* ms, half buffered_file limit */
683
Juan Quintelad1315aa2012-06-28 15:11:57 +0200684static int ram_save_setup(QEMUFile *f, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000685{
Juan Quintelad1315aa2012-06-28 15:11:57 +0200686 RAMBlock *block;
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200687 int64_t ram_pages = last_ram_offset() >> TARGET_PAGE_BITS;
688
689 migration_bitmap = bitmap_new(ram_pages);
David Gibson7ec81e52012-12-04 11:38:38 +1100690 bitmap_set(migration_bitmap, 0, ram_pages);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200691 migration_dirty_pages = ram_pages;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600692 mig_throttle_on = false;
693 dirty_rate_high_cnt = 0;
Blue Swirlad960902010-03-29 19:23:52 +0000694
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300695 if (migrate_use_xbzrle()) {
696 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
697 TARGET_PAGE_SIZE,
698 TARGET_PAGE_SIZE);
699 if (!XBZRLE.cache) {
700 DPRINTF("Error creating cache\n");
701 return -1;
702 }
Orit Wassermana17b2fd2014-01-30 20:08:37 +0200703
704 /* We prefer not to abort if there is no memory */
705 XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
706 if (!XBZRLE.encoded_buf) {
707 DPRINTF("Error allocating encoded_buf\n");
708 return -1;
709 }
710
711 XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
712 if (!XBZRLE.current_buf) {
713 DPRINTF("Error allocating current_buf\n");
714 g_free(XBZRLE.encoded_buf);
715 XBZRLE.encoded_buf = NULL;
716 return -1;
717 }
718
Orit Wasserman004d4c12012-08-06 21:42:56 +0300719 acct_clear();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300720 }
721
Paolo Bonzini9b095032013-02-22 17:36:28 +0100722 qemu_mutex_lock_iothread();
723 qemu_mutex_lock_ramlist();
724 bytes_transferred = 0;
725 reset_ram_globals();
726
Juan Quintelad1315aa2012-06-28 15:11:57 +0200727 memory_global_dirty_log_start();
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200728 migration_bitmap_sync();
Paolo Bonzini9b095032013-02-22 17:36:28 +0100729 qemu_mutex_unlock_iothread();
Juan Quintelad1315aa2012-06-28 15:11:57 +0200730
731 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
732
Paolo Bonzinia3161032012-11-14 15:54:48 +0100733 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200734 qemu_put_byte(f, strlen(block->idstr));
735 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
736 qemu_put_be64(f, block->length);
737 }
738
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700739 qemu_mutex_unlock_ramlist();
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400740
741 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
742 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
743
Juan Quintelad1315aa2012-06-28 15:11:57 +0200744 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
745
746 return 0;
747}
748
Juan Quintela16310a32012-06-28 15:31:37 +0200749static int ram_save_iterate(QEMUFile *f, void *opaque)
Juan Quintelad1315aa2012-06-28 15:11:57 +0200750{
Blue Swirlad960902010-03-29 19:23:52 +0000751 int ret;
752 int i;
Juan Quintelae4ed1542012-09-21 11:18:18 +0200753 int64_t t0;
Juan Quintelab823cea2012-12-10 13:27:50 +0100754 int total_sent = 0;
Blue Swirlad960902010-03-29 19:23:52 +0000755
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700756 qemu_mutex_lock_ramlist();
757
Umesh Deshpandef798b072011-08-18 11:41:17 -0700758 if (ram_list.version != last_version) {
759 reset_ram_globals();
760 }
761
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400762 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
763
Alex Blighbc72ad62013-08-21 16:03:08 +0100764 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Juan Quintela4508bd92012-05-22 16:27:59 +0200765 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200766 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200767 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000768
Juan Quinteladd051c72012-08-06 21:42:58 +0300769 bytes_sent = ram_save_block(f, false);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300770 /* no more blocks to sent */
Juan Quintelab823cea2012-12-10 13:27:50 +0100771 if (bytes_sent == 0) {
Blue Swirlad960902010-03-29 19:23:52 +0000772 break;
773 }
Juan Quintelab823cea2012-12-10 13:27:50 +0100774 total_sent += bytes_sent;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300775 acct_info.iterations++;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600776 check_guest_throttling();
Juan Quintela4508bd92012-05-22 16:27:59 +0200777 /* we want to check in the 1st loop, just in case it was the 1st time
778 and we had to sync the dirty bitmap.
779 qemu_get_clock_ns() is a bit expensive, so we only check each some
780 iterations
781 */
782 if ((i & 63) == 0) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100783 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
Juan Quintela4508bd92012-05-22 16:27:59 +0200784 if (t1 > MAX_WAIT) {
Igor Mitsyankoef37a692012-09-05 13:04:56 +0400785 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
Juan Quintela4508bd92012-05-22 16:27:59 +0200786 t1, i);
787 break;
788 }
789 }
790 i++;
Blue Swirlad960902010-03-29 19:23:52 +0000791 }
792
Paolo Bonzinifb3409d2012-12-20 11:25:45 +0100793 qemu_mutex_unlock_ramlist();
794
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400795 /*
796 * Must occur before EOS (or any QEMUFile operation)
797 * because of RDMA protocol.
798 */
799 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
800
Lei Li6cd0bed2013-09-04 17:02:36 +0800801 bytes_transferred += total_sent;
802
803 /*
804 * Do not count these 8 bytes into total_sent, so that we can
805 * return 0 if no page had been dirtied.
806 */
807 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
808 bytes_transferred += 8;
809
810 ret = qemu_file_get_error(f);
Juan Quintela29757252011-10-19 15:22:18 +0200811 if (ret < 0) {
812 return ret;
813 }
814
Juan Quintelab823cea2012-12-10 13:27:50 +0100815 return total_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000816}
817
Juan Quintela16310a32012-06-28 15:31:37 +0200818static int ram_save_complete(QEMUFile *f, void *opaque)
819{
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700820 qemu_mutex_lock_ramlist();
Paolo Bonzini9c339482012-12-20 11:26:04 +0100821 migration_bitmap_sync();
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700822
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400823 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
824
Juan Quintela16310a32012-06-28 15:31:37 +0200825 /* try transferring iterative blocks of memory */
826
827 /* flush all remaining blocks regardless of rate limiting */
Orit Wasserman6c779f22012-07-10 12:37:13 +0300828 while (true) {
829 int bytes_sent;
830
Juan Quinteladd051c72012-08-06 21:42:58 +0300831 bytes_sent = ram_save_block(f, true);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300832 /* no more blocks to sent */
Juan Quintelab823cea2012-12-10 13:27:50 +0100833 if (bytes_sent == 0) {
Orit Wasserman6c779f22012-07-10 12:37:13 +0300834 break;
835 }
Juan Quintela16310a32012-06-28 15:31:37 +0200836 bytes_transferred += bytes_sent;
837 }
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400838
839 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100840 migration_end();
Juan Quintela16310a32012-06-28 15:31:37 +0200841
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700842 qemu_mutex_unlock_ramlist();
Blue Swirlad960902010-03-29 19:23:52 +0000843 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
844
Alex Williamsona55bbe32010-06-25 11:10:05 -0600845 return 0;
846}
847
Juan Quintelae4ed1542012-09-21 11:18:18 +0200848static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
849{
850 uint64_t remaining_size;
851
852 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
853
854 if (remaining_size < max_size) {
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100855 qemu_mutex_lock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200856 migration_bitmap_sync();
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100857 qemu_mutex_unlock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200858 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
859 }
860 return remaining_size;
861}
862
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300863static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
864{
865 int ret, rc = 0;
866 unsigned int xh_len;
867 int xh_flags;
868
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200869 if (!xbzrle_decoded_buf) {
870 xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300871 }
872
873 /* extract RLE header */
874 xh_flags = qemu_get_byte(f);
875 xh_len = qemu_get_be16(f);
876
877 if (xh_flags != ENCODING_FLAG_XBZRLE) {
878 fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
879 return -1;
880 }
881
882 if (xh_len > TARGET_PAGE_SIZE) {
883 fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
884 return -1;
885 }
886 /* load data and decode */
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200887 qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300888
889 /* decode RLE */
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200890 ret = xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300891 TARGET_PAGE_SIZE);
892 if (ret == -1) {
893 fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
894 rc = -1;
895 } else if (ret > TARGET_PAGE_SIZE) {
896 fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
897 ret, TARGET_PAGE_SIZE);
898 abort();
899 }
900
901 return rc;
902}
903
Alex Williamsona55bbe32010-06-25 11:10:05 -0600904static inline void *host_from_stream_offset(QEMUFile *f,
905 ram_addr_t offset,
906 int flags)
907{
908 static RAMBlock *block = NULL;
909 char id[256];
910 uint8_t len;
911
912 if (flags & RAM_SAVE_FLAG_CONTINUE) {
913 if (!block) {
914 fprintf(stderr, "Ack, bad migration stream!\n");
915 return NULL;
916 }
917
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200918 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600919 }
920
921 len = qemu_get_byte(f);
922 qemu_get_buffer(f, (uint8_t *)id, len);
923 id[len] = 0;
924
Paolo Bonzinia3161032012-11-14 15:54:48 +0100925 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamsona55bbe32010-06-25 11:10:05 -0600926 if (!strncmp(id, block->idstr, sizeof(id)))
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200927 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600928 }
929
930 fprintf(stderr, "Can't find block %s!\n", id);
931 return NULL;
932}
933
Michael R. Hines44c3b582013-07-22 10:01:53 -0400934/*
935 * If a page (or a whole RDMA chunk) has been
936 * determined to be zero, then zap it.
937 */
938void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
939{
Isaku Yamahatad613a562013-09-21 01:23:37 +0900940 if (ch != 0 || !is_zero_range(host, size)) {
Michael R. Hines44c3b582013-07-22 10:01:53 -0400941 memset(host, ch, size);
Michael R. Hines44c3b582013-07-22 10:01:53 -0400942 }
943}
944
Juan Quintela7908c782012-06-26 18:46:10 +0200945static int ram_load(QEMUFile *f, void *opaque, int version_id)
Blue Swirlad960902010-03-29 19:23:52 +0000946{
947 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300948 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200949 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300950 static uint64_t seq_iter;
951
952 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000953
Avi Kivityf09f2182011-12-21 13:37:56 +0200954 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000955 return -EINVAL;
956 }
957
958 do {
959 addr = qemu_get_be64(f);
960
961 flags = addr & ~TARGET_PAGE_MASK;
962 addr &= TARGET_PAGE_MASK;
963
964 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200965 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600966 /* Synchronize RAM block list */
967 char id[256];
968 ram_addr_t length;
969 ram_addr_t total_ram_bytes = addr;
970
971 while (total_ram_bytes) {
972 RAMBlock *block;
973 uint8_t len;
974
975 len = qemu_get_byte(f);
976 qemu_get_buffer(f, (uint8_t *)id, len);
977 id[len] = 0;
978 length = qemu_get_be64(f);
979
Paolo Bonzinia3161032012-11-14 15:54:48 +0100980 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600981 if (!strncmp(id, block->idstr, sizeof(id))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300982 if (block->length != length) {
Stefan Weil6bedfe92013-06-27 21:00:06 +0200983 fprintf(stderr,
984 "Length mismatch: %s: " RAM_ADDR_FMT
985 " in != " RAM_ADDR_FMT "\n", id, length,
Alon Levy87d2f822013-05-12 14:16:28 +0300986 block->length);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300987 ret = -EINVAL;
988 goto done;
989 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600990 break;
991 }
992 }
993
994 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600995 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
996 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300997 ret = -EINVAL;
998 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600999 }
1000
1001 total_ram_bytes -= length;
1002 }
Blue Swirlad960902010-03-29 19:23:52 +00001003 }
1004 }
1005
1006 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -06001007 void *host;
1008 uint8_t ch;
1009
Avi Kivityf09f2182011-12-21 13:37:56 +02001010 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +02001011 if (!host) {
1012 return -EINVAL;
1013 }
Alex Williamson97ab12d2010-06-25 11:09:50 -06001014
Alex Williamson97ab12d2010-06-25 11:09:50 -06001015 ch = qemu_get_byte(f);
Michael R. Hines44c3b582013-07-22 10:01:53 -04001016 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +00001017 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -06001018 void *host;
1019
Avi Kivityf09f2182011-12-21 13:37:56 +02001020 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +03001021 if (!host) {
1022 return -EINVAL;
1023 }
Alex Williamson97ab12d2010-06-25 11:09:50 -06001024
Alex Williamson97ab12d2010-06-25 11:09:50 -06001025 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001026 } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001027 void *host = host_from_stream_offset(f, addr, flags);
1028 if (!host) {
1029 return -EINVAL;
1030 }
1031
1032 if (load_xbzrle(f, addr, host) < 0) {
1033 ret = -EINVAL;
1034 goto done;
1035 }
Michael R. Hines0033b8b2013-07-22 10:01:55 -04001036 } else if (flags & RAM_SAVE_FLAG_HOOK) {
1037 ram_control_load_hook(f, flags);
Blue Swirlad960902010-03-29 19:23:52 +00001038 }
Juan Quintela42802d42011-10-05 01:14:46 +02001039 error = qemu_file_get_error(f);
1040 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +03001041 ret = error;
1042 goto done;
Blue Swirlad960902010-03-29 19:23:52 +00001043 }
1044 } while (!(flags & RAM_SAVE_FLAG_EOS));
1045
Orit Wasserman3a697f62012-06-19 18:43:15 +03001046done:
Igor Mitsyankoef37a692012-09-05 13:04:56 +04001047 DPRINTF("Completed load of VM with exit code %d seq iteration "
1048 "%" PRIu64 "\n", ret, seq_iter);
Orit Wasserman3a697f62012-06-19 18:43:15 +03001049 return ret;
Blue Swirlad960902010-03-29 19:23:52 +00001050}
1051
Juan Quintela7908c782012-06-26 18:46:10 +02001052SaveVMHandlers savevm_ram_handlers = {
Juan Quintelad1315aa2012-06-28 15:11:57 +02001053 .save_live_setup = ram_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +02001054 .save_live_iterate = ram_save_iterate,
1055 .save_live_complete = ram_save_complete,
Juan Quintelae4ed1542012-09-21 11:18:18 +02001056 .save_live_pending = ram_save_pending,
Juan Quintela7908c782012-06-26 18:46:10 +02001057 .load_state = ram_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001058 .cancel = ram_migration_cancel,
Juan Quintela7908c782012-06-26 18:46:10 +02001059};
1060
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001061struct soundhw {
1062 const char *name;
1063 const char *descr;
1064 int enabled;
1065 int isa;
1066 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +01001067 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001068 int (*init_pci) (PCIBus *bus);
1069 } init;
1070};
1071
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001072static struct soundhw soundhw[9];
1073static int soundhw_count;
Blue Swirlad960902010-03-29 19:23:52 +00001074
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001075void isa_register_soundhw(const char *name, const char *descr,
1076 int (*init_isa)(ISABus *bus))
1077{
1078 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1079 soundhw[soundhw_count].name = name;
1080 soundhw[soundhw_count].descr = descr;
1081 soundhw[soundhw_count].isa = 1;
1082 soundhw[soundhw_count].init.init_isa = init_isa;
1083 soundhw_count++;
1084}
Blue Swirlad960902010-03-29 19:23:52 +00001085
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001086void pci_register_soundhw(const char *name, const char *descr,
1087 int (*init_pci)(PCIBus *bus))
1088{
1089 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1090 soundhw[soundhw_count].name = name;
1091 soundhw[soundhw_count].descr = descr;
1092 soundhw[soundhw_count].isa = 0;
1093 soundhw[soundhw_count].init.init_pci = init_pci;
1094 soundhw_count++;
1095}
Blue Swirlad960902010-03-29 19:23:52 +00001096
1097void select_soundhw(const char *optarg)
1098{
1099 struct soundhw *c;
1100
Peter Maydellc8057f92012-08-02 13:45:54 +01001101 if (is_help_option(optarg)) {
Blue Swirlad960902010-03-29 19:23:52 +00001102 show_valid_cards:
1103
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001104 if (soundhw_count) {
1105 printf("Valid sound card names (comma separated):\n");
1106 for (c = soundhw; c->name; ++c) {
1107 printf ("%-11s %s\n", c->name, c->descr);
1108 }
1109 printf("\n-soundhw all will enable all of the above\n");
1110 } else {
1111 printf("Machine has no user-selectable audio hardware "
1112 "(it may or may not have always-present audio hardware).\n");
Blue Swirlad960902010-03-29 19:23:52 +00001113 }
Peter Maydellc8057f92012-08-02 13:45:54 +01001114 exit(!is_help_option(optarg));
Blue Swirlad960902010-03-29 19:23:52 +00001115 }
1116 else {
1117 size_t l;
1118 const char *p;
1119 char *e;
1120 int bad_card = 0;
1121
1122 if (!strcmp(optarg, "all")) {
1123 for (c = soundhw; c->name; ++c) {
1124 c->enabled = 1;
1125 }
1126 return;
1127 }
1128
1129 p = optarg;
1130 while (*p) {
1131 e = strchr(p, ',');
1132 l = !e ? strlen(p) : (size_t) (e - p);
1133
1134 for (c = soundhw; c->name; ++c) {
1135 if (!strncmp(c->name, p, l) && !c->name[l]) {
1136 c->enabled = 1;
1137 break;
1138 }
1139 }
1140
1141 if (!c->name) {
1142 if (l > 80) {
1143 fprintf(stderr,
1144 "Unknown sound card name (too big to show)\n");
1145 }
1146 else {
1147 fprintf(stderr, "Unknown sound card name `%.*s'\n",
1148 (int) l, p);
1149 }
1150 bad_card = 1;
1151 }
1152 p += l + (e != NULL);
1153 }
1154
1155 if (bad_card) {
1156 goto show_valid_cards;
1157 }
1158 }
1159}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001160
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001161void audio_init(void)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001162{
1163 struct soundhw *c;
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001164 ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
1165 PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001166
1167 for (c = soundhw; c->name; ++c) {
1168 if (c->enabled) {
1169 if (c->isa) {
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001170 if (!isa_bus) {
1171 fprintf(stderr, "ISA bus not available for %s\n", c->name);
1172 exit(1);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001173 }
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001174 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001175 } else {
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001176 if (!pci_bus) {
1177 fprintf(stderr, "PCI bus not available for %s\n", c->name);
1178 exit(1);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001179 }
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001180 c->init.init_pci(pci_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001181 }
1182 }
1183 }
1184}
Blue Swirlad960902010-03-29 19:23:52 +00001185
1186int qemu_uuid_parse(const char *str, uint8_t *uuid)
1187{
1188 int ret;
1189
1190 if (strlen(str) != 36) {
1191 return -1;
1192 }
1193
1194 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1195 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1196 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1197 &uuid[15]);
1198
1199 if (ret != 16) {
1200 return -1;
1201 }
Blue Swirlad960902010-03-29 19:23:52 +00001202 return 0;
1203}
1204
Laszlo Ersek0c764a92013-03-21 00:23:17 +01001205void do_acpitable_option(const QemuOpts *opts)
Blue Swirlad960902010-03-29 19:23:52 +00001206{
1207#ifdef TARGET_I386
Laszlo Ersek23084322013-03-21 00:23:19 +01001208 Error *err = NULL;
1209
1210 acpi_table_add(opts, &err);
1211 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04001212 error_report("Wrong acpi table provided: %s",
1213 error_get_pretty(err));
Laszlo Ersek23084322013-03-21 00:23:19 +01001214 error_free(err);
Blue Swirlad960902010-03-29 19:23:52 +00001215 exit(1);
1216 }
1217#endif
1218}
1219
Markus Armbruster4f953d22013-08-16 15:18:29 +02001220void do_smbios_option(QemuOpts *opts)
Blue Swirlad960902010-03-29 19:23:52 +00001221{
1222#ifdef TARGET_I386
Markus Armbruster4f953d22013-08-16 15:18:29 +02001223 smbios_entry_add(opts);
Blue Swirlad960902010-03-29 19:23:52 +00001224#endif
1225}
1226
1227void cpudef_init(void)
1228{
1229#if defined(cpudef_setup)
1230 cpudef_setup(); /* parse cpu definitions in target config file */
1231#endif
1232}
1233
Anthony PERARD303d4e82010-09-21 20:05:31 +01001234int tcg_available(void)
1235{
1236 return 1;
1237}
1238
Blue Swirlad960902010-03-29 19:23:52 +00001239int kvm_available(void)
1240{
1241#ifdef CONFIG_KVM
1242 return 1;
1243#else
1244 return 0;
1245#endif
1246}
1247
1248int xen_available(void)
1249{
1250#ifdef CONFIG_XEN
1251 return 1;
1252#else
1253 return 0;
1254#endif
1255}
Daniel P. Berrange99afc912012-08-20 15:31:38 +01001256
1257
1258TargetInfo *qmp_query_target(Error **errp)
1259{
1260 TargetInfo *info = g_malloc0(sizeof(*info));
1261
Paolo Bonzinic02a9552013-06-04 14:45:28 +02001262 info->arch = g_strdup(TARGET_NAME);
Daniel P. Berrange99afc912012-08-20 15:31:38 +01001263
1264 return info;
1265}
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001266
1267/* Stub function that's gets run on the vcpu when its brought out of the
1268 VM to run inside qemu via async_run_on_cpu()*/
1269static void mig_sleep_cpu(void *opq)
1270{
1271 qemu_mutex_unlock_iothread();
1272 g_usleep(30*1000);
1273 qemu_mutex_lock_iothread();
1274}
1275
1276/* To reduce the dirty rate explicitly disallow the VCPUs from spending
1277 much time in the VM. The migration thread will try to catchup.
1278 Workload will experience a performance drop.
1279*/
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001280static void mig_throttle_guest_down(void)
1281{
Andreas Färber38fcbd32013-07-07 19:50:23 +02001282 CPUState *cpu;
1283
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001284 qemu_mutex_lock_iothread();
Andreas Färber38fcbd32013-07-07 19:50:23 +02001285 CPU_FOREACH(cpu) {
1286 async_run_on_cpu(cpu, mig_sleep_cpu, NULL);
1287 }
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001288 qemu_mutex_unlock_iothread();
1289}
1290
1291static void check_guest_throttling(void)
1292{
1293 static int64_t t0;
1294 int64_t t1;
1295
1296 if (!mig_throttle_on) {
1297 return;
1298 }
1299
1300 if (!t0) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001301 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001302 return;
1303 }
1304
Alex Blighbc72ad62013-08-21 16:03:08 +01001305 t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001306
1307 /* If it has been more than 40 ms since the last time the guest
1308 * was throttled then do it again.
1309 */
1310 if (40 < (t1-t0)/1000000) {
1311 mig_throttle_guest_down();
1312 t0 = t1;
1313 }
1314}