blob: 691b5e2e91648f6c2b955e8e95e51f4e0d48e07c [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"
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +000048#include "qemu/error-report.h"
Daniel P. Berrange99afc912012-08-20 15:31:38 +010049#include "qmp-commands.h"
Juan Quintela3c121932012-09-04 13:08:57 +020050#include "trace.h"
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +010051#include "exec/cpu-all.h"
Juan Quintela12291ec2013-10-14 17:14:47 +020052#include "exec/ram_addr.h"
Michael S. Tsirkin04452592013-04-15 09:19:22 +030053#include "hw/acpi/acpi.h"
Juan Quintelaaa8dc042013-11-06 11:33:05 +010054#include "qemu/host-utils.h"
Mike Day0dc3f442013-09-05 14:41:35 -040055#include "qemu/rcu_queue.h"
Blue Swirlad960902010-03-29 19:23:52 +000056
Orit Wasserman3a697f62012-06-19 18:43:15 +030057#ifdef DEBUG_ARCH_INIT
58#define DPRINTF(fmt, ...) \
59 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
60#else
61#define DPRINTF(fmt, ...) \
62 do { } while (0)
63#endif
64
Blue Swirlad960902010-03-29 19:23:52 +000065#ifdef TARGET_SPARC
66int graphic_width = 1024;
67int graphic_height = 768;
68int graphic_depth = 8;
69#else
70int graphic_width = 800;
71int graphic_height = 600;
Alexander Graff1ff0e82013-06-20 14:06:27 +020072int graphic_depth = 32;
Blue Swirlad960902010-03-29 19:23:52 +000073#endif
74
Blue Swirlad960902010-03-29 19:23:52 +000075
76#if defined(TARGET_ALPHA)
77#define QEMU_ARCH QEMU_ARCH_ALPHA
78#elif defined(TARGET_ARM)
79#define QEMU_ARCH QEMU_ARCH_ARM
80#elif defined(TARGET_CRIS)
81#define QEMU_ARCH QEMU_ARCH_CRIS
82#elif defined(TARGET_I386)
83#define QEMU_ARCH QEMU_ARCH_I386
84#elif defined(TARGET_M68K)
85#define QEMU_ARCH QEMU_ARCH_M68K
Michael Walle81ea0e12011-02-17 23:45:02 +010086#elif defined(TARGET_LM32)
87#define QEMU_ARCH QEMU_ARCH_LM32
Blue Swirlad960902010-03-29 19:23:52 +000088#elif defined(TARGET_MICROBLAZE)
89#define QEMU_ARCH QEMU_ARCH_MICROBLAZE
90#elif defined(TARGET_MIPS)
91#define QEMU_ARCH QEMU_ARCH_MIPS
Anthony Greend15a9c22013-03-18 15:49:25 -040092#elif defined(TARGET_MOXIE)
93#define QEMU_ARCH QEMU_ARCH_MOXIE
Jia Liue67db062012-07-20 15:50:39 +080094#elif defined(TARGET_OPENRISC)
95#define QEMU_ARCH QEMU_ARCH_OPENRISC
Blue Swirlad960902010-03-29 19:23:52 +000096#elif defined(TARGET_PPC)
97#define QEMU_ARCH QEMU_ARCH_PPC
98#elif defined(TARGET_S390X)
99#define QEMU_ARCH QEMU_ARCH_S390X
100#elif defined(TARGET_SH4)
101#define QEMU_ARCH QEMU_ARCH_SH4
102#elif defined(TARGET_SPARC)
103#define QEMU_ARCH QEMU_ARCH_SPARC
Max Filippov23288262011-09-06 03:55:25 +0400104#elif defined(TARGET_XTENSA)
105#define QEMU_ARCH QEMU_ARCH_XTENSA
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800106#elif defined(TARGET_UNICORE32)
107#define QEMU_ARCH QEMU_ARCH_UNICORE32
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +0100108#elif defined(TARGET_TRICORE)
109#define QEMU_ARCH QEMU_ARCH_TRICORE
Blue Swirlad960902010-03-29 19:23:52 +0000110#endif
111
112const uint32_t arch_type = QEMU_ARCH;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600113static bool mig_throttle_on;
114static int dirty_rate_high_cnt;
115static void check_guest_throttling(void);
Blue Swirlad960902010-03-29 19:23:52 +0000116
ChenLiang71411d32014-04-04 17:57:54 +0800117static uint64_t bitmap_sync_count;
118
Blue Swirlad960902010-03-29 19:23:52 +0000119/***********************************************************/
120/* ram save/restore */
121
Yoshiaki Tamurad20878d2010-08-18 13:30:12 +0900122#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
123#define RAM_SAVE_FLAG_COMPRESS 0x02
124#define RAM_SAVE_FLAG_MEM_SIZE 0x04
125#define RAM_SAVE_FLAG_PAGE 0x08
126#define RAM_SAVE_FLAG_EOS 0x10
127#define RAM_SAVE_FLAG_CONTINUE 0x20
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300128#define RAM_SAVE_FLAG_XBZRLE 0x40
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400129/* 0x80 is reserved in migration.h start with 0x100 next */
Blue Swirlad960902010-03-29 19:23:52 +0000130
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 Habkostf29a5612012-05-02 13:07:29 -0300136 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
Paolo Bonzini2e599152013-06-04 14:45:27 +0200137 { CONFIG_QEMU_CONFDIR "/target-" TARGET_NAME ".conf", true },
Eduardo Habkost756557d2012-05-02 13:07:27 -0300138 { NULL }, /* end of list */
139};
140
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000141static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE];
Eduardo Habkost756557d2012-05-02 13:07:27 -0300142
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 }
Laszlo Ersek4d8b3c62013-03-21 00:23:13 +0100157
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300158 return 0;
159}
160
Isaku Yamahatadc3c26a2013-09-21 01:23:36 +0900161static inline bool is_zero_range(uint8_t *p, uint64_t size)
Blue Swirlad960902010-03-29 19:23:52 +0000162{
Isaku Yamahatadc3c26a2013-09-21 01:23:36 +0900163 return buffer_find_nonzero_offset(p, size) == size;
Blue Swirlad960902010-03-29 19:23:52 +0000164}
165
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300166/* struct contains XBZRLE cache and a static page
167 used by the compression */
168static struct {
169 /* buffer used for XBZRLE encoding */
170 uint8_t *encoded_buf;
171 /* buffer for storing page content */
172 uint8_t *current_buf;
Gongleifd8cec92014-03-04 21:29:21 +0800173 /* Cache for XBZRLE, Protected by lock. */
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300174 PageCache *cache;
Gongleifd8cec92014-03-04 21:29:21 +0800175 QemuMutex lock;
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000176} XBZRLE;
177
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200178/* buffer used for XBZRLE decoding */
179static uint8_t *xbzrle_decoded_buf;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300180
Gongleifd8cec92014-03-04 21:29:21 +0800181static void XBZRLE_cache_lock(void)
182{
183 if (migrate_use_xbzrle())
184 qemu_mutex_lock(&XBZRLE.lock);
185}
186
187static void XBZRLE_cache_unlock(void)
188{
189 if (migrate_use_xbzrle())
190 qemu_mutex_unlock(&XBZRLE.lock);
191}
192
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000193/*
194 * called from qmp_migrate_set_cache_size in main thread, possibly while
195 * a migration is in progress.
196 * A running migration maybe using the cache and might finish during this
197 * call, hence changes to the cache are protected by XBZRLE.lock().
198 */
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300199int64_t xbzrle_cache_resize(int64_t new_size)
200{
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000201 PageCache *new_cache;
202 int64_t ret;
Gongleifd8cec92014-03-04 21:29:21 +0800203
Orit Wassermanc91e6812014-01-30 20:08:34 +0200204 if (new_size < TARGET_PAGE_SIZE) {
205 return -1;
206 }
207
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000208 XBZRLE_cache_lock();
209
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300210 if (XBZRLE.cache != NULL) {
Gongleifd8cec92014-03-04 21:29:21 +0800211 if (pow2floor(new_size) == migrate_xbzrle_cache_size()) {
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000212 goto out_new_size;
Gongleifd8cec92014-03-04 21:29:21 +0800213 }
214 new_cache = cache_init(new_size / TARGET_PAGE_SIZE,
215 TARGET_PAGE_SIZE);
216 if (!new_cache) {
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000217 error_report("Error creating cache");
218 ret = -1;
219 goto out;
Gongleifd8cec92014-03-04 21:29:21 +0800220 }
221
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000222 cache_fini(XBZRLE.cache);
223 XBZRLE.cache = new_cache;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300224 }
Gongleifd8cec92014-03-04 21:29:21 +0800225
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000226out_new_size:
227 ret = pow2floor(new_size);
228out:
229 XBZRLE_cache_unlock();
230 return ret;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300231}
232
Orit Wasserman004d4c12012-08-06 21:42:56 +0300233/* accounting for migration statistics */
234typedef struct AccountingInfo {
235 uint64_t dup_pages;
Peter Lievenf1c72792013-03-26 10:58:37 +0100236 uint64_t skipped_pages;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300237 uint64_t norm_pages;
238 uint64_t iterations;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300239 uint64_t xbzrle_bytes;
240 uint64_t xbzrle_pages;
241 uint64_t xbzrle_cache_miss;
ChenLiang8bc39232014-04-04 17:57:56 +0800242 double xbzrle_cache_miss_rate;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300243 uint64_t xbzrle_overflows;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300244} AccountingInfo;
245
246static AccountingInfo acct_info;
247
248static void acct_clear(void)
249{
250 memset(&acct_info, 0, sizeof(acct_info));
251}
252
253uint64_t dup_mig_bytes_transferred(void)
254{
255 return acct_info.dup_pages * TARGET_PAGE_SIZE;
256}
257
258uint64_t dup_mig_pages_transferred(void)
259{
260 return acct_info.dup_pages;
261}
262
Peter Lievenf1c72792013-03-26 10:58:37 +0100263uint64_t skipped_mig_bytes_transferred(void)
264{
265 return acct_info.skipped_pages * TARGET_PAGE_SIZE;
266}
267
268uint64_t skipped_mig_pages_transferred(void)
269{
270 return acct_info.skipped_pages;
271}
272
Orit Wasserman004d4c12012-08-06 21:42:56 +0300273uint64_t norm_mig_bytes_transferred(void)
274{
275 return acct_info.norm_pages * TARGET_PAGE_SIZE;
276}
277
278uint64_t norm_mig_pages_transferred(void)
279{
280 return acct_info.norm_pages;
281}
282
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300283uint64_t xbzrle_mig_bytes_transferred(void)
284{
285 return acct_info.xbzrle_bytes;
286}
287
288uint64_t xbzrle_mig_pages_transferred(void)
289{
290 return acct_info.xbzrle_pages;
291}
292
293uint64_t xbzrle_mig_pages_cache_miss(void)
294{
295 return acct_info.xbzrle_cache_miss;
296}
297
ChenLiang8bc39232014-04-04 17:57:56 +0800298double xbzrle_mig_cache_miss_rate(void)
299{
300 return acct_info.xbzrle_cache_miss_rate;
301}
302
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300303uint64_t xbzrle_mig_pages_overflow(void)
304{
305 return acct_info.xbzrle_overflows;
306}
307
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200308static size_t save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
309 int cont, int flag)
Orit Wasserman0c51f432012-06-19 18:43:14 +0300310{
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200311 size_t size;
Orit Wasserman0c51f432012-06-19 18:43:14 +0300312
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200313 qemu_put_be64(f, offset | cont | flag);
314 size = 8;
315
316 if (!cont) {
317 qemu_put_byte(f, strlen(block->idstr));
318 qemu_put_buffer(f, (uint8_t *)block->idstr,
319 strlen(block->idstr));
320 size += 1 + strlen(block->idstr);
321 }
322 return size;
Orit Wasserman0c51f432012-06-19 18:43:14 +0300323}
324
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000325/* This is the last block that we have visited serching for dirty pages
326 */
327static RAMBlock *last_seen_block;
328/* This is the last block from where we have sent data */
329static RAMBlock *last_sent_block;
330static ram_addr_t last_offset;
331static unsigned long *migration_bitmap;
332static uint64_t migration_dirty_pages;
333static uint32_t last_version;
334static bool ram_bulk_stage;
335
336/* Update the xbzrle cache to reflect a page that's been sent as all 0.
337 * The important thing is that a stale (not-yet-0'd) page be replaced
338 * by the new data.
339 * As a bonus, if the page wasn't in the cache it gets added so that
340 * when a small write is made into the 0'd page it gets XBZRLE sent
341 */
342static void xbzrle_cache_zero_page(ram_addr_t current_addr)
343{
344 if (ram_bulk_stage || !migrate_use_xbzrle()) {
345 return;
346 }
347
348 /* We don't care if this fails to allocate a new cache page
349 * as long as it updated an old one */
ChenLiang27af7d62014-11-24 19:55:47 +0800350 cache_insert(XBZRLE.cache, current_addr, ZERO_TARGET_PAGE,
351 bitmap_sync_count);
Dr. David Alan Gilbert6d3cb1f2014-02-13 19:44:45 +0000352}
353
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300354#define ENCODING_FLAG_XBZRLE 0x1
355
ChenLiang1534ee92014-04-04 17:57:53 +0800356static int save_xbzrle_page(QEMUFile *f, uint8_t **current_data,
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300357 ram_addr_t current_addr, RAMBlock *block,
Juan Quinteladd051c72012-08-06 21:42:58 +0300358 ram_addr_t offset, int cont, bool last_stage)
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300359{
360 int encoded_len = 0, bytes_sent = -1;
361 uint8_t *prev_cached_page;
362
ChenLiang27af7d62014-11-24 19:55:47 +0800363 if (!cache_is_cached(XBZRLE.cache, current_addr, bitmap_sync_count)) {
ChenLiang1534ee92014-04-04 17:57:53 +0800364 acct_info.xbzrle_cache_miss++;
Juan Quinteladd051c72012-08-06 21:42:58 +0300365 if (!last_stage) {
ChenLiang27af7d62014-11-24 19:55:47 +0800366 if (cache_insert(XBZRLE.cache, current_addr, *current_data,
367 bitmap_sync_count) == -1) {
Orit Wasserman89db9982014-01-30 20:08:38 +0200368 return -1;
ChenLiang1534ee92014-04-04 17:57:53 +0800369 } else {
370 /* update *current_data when the page has been
371 inserted into cache */
372 *current_data = get_cached_data(XBZRLE.cache, current_addr);
Orit Wasserman89db9982014-01-30 20:08:38 +0200373 }
Juan Quinteladd051c72012-08-06 21:42:58 +0300374 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300375 return -1;
376 }
377
378 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
379
380 /* save current buffer into memory */
ChenLiang1534ee92014-04-04 17:57:53 +0800381 memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300382
383 /* XBZRLE encoding (if there is no overflow) */
384 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
385 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
386 TARGET_PAGE_SIZE);
387 if (encoded_len == 0) {
388 DPRINTF("Skipping unmodified page\n");
389 return 0;
390 } else if (encoded_len == -1) {
391 DPRINTF("Overflow\n");
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300392 acct_info.xbzrle_overflows++;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300393 /* update data in the cache */
ChenLiang1534ee92014-04-04 17:57:53 +0800394 if (!last_stage) {
395 memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE);
396 *current_data = prev_cached_page;
397 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300398 return -1;
399 }
400
401 /* we need to update the data in the cache, in order to get the same data */
Juan Quinteladd051c72012-08-06 21:42:58 +0300402 if (!last_stage) {
403 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
404 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300405
406 /* Send XBZRLE based compressed page */
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200407 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300408 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
409 qemu_put_be16(f, encoded_len);
410 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200411 bytes_sent += encoded_len + 1 + 2;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300412 acct_info.xbzrle_pages++;
413 acct_info.xbzrle_bytes += bytes_sent;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300414
415 return bytes_sent;
416}
417
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200418static inline
419ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
420 ram_addr_t start)
Juan Quintela69268cd2012-07-20 10:36:12 +0200421{
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200422 unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS;
423 unsigned long nr = base + (start >> TARGET_PAGE_BITS);
Michael S. Tsirkin0851c9f2013-08-19 17:26:52 +0300424 uint64_t mr_size = TARGET_PAGE_ALIGN(memory_region_size(mr));
425 unsigned long size = base + (mr_size >> TARGET_PAGE_BITS);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200426
Peter Lieven70c86522013-03-26 10:58:38 +0100427 unsigned long next;
428
429 if (ram_bulk_stage && nr > base) {
430 next = nr + 1;
431 } else {
432 next = find_next_bit(migration_bitmap, size, nr);
433 }
Juan Quintela69268cd2012-07-20 10:36:12 +0200434
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200435 if (next < size) {
436 clear_bit(next, migration_bitmap);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200437 migration_dirty_pages--;
Juan Quintela69268cd2012-07-20 10:36:12 +0200438 }
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200439 return (next - base) << TARGET_PAGE_BITS;
Juan Quintela69268cd2012-07-20 10:36:12 +0200440}
441
Juan Quintela791fa2a2013-11-05 16:47:20 +0100442static inline bool migration_bitmap_set_dirty(ram_addr_t addr)
Juan Quintelae44d26c2012-07-20 10:16:08 +0200443{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200444 bool ret;
Juan Quintela791fa2a2013-11-05 16:47:20 +0100445 int nr = addr >> TARGET_PAGE_BITS;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200446
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200447 ret = test_and_set_bit(nr, migration_bitmap);
448
449 if (!ret) {
450 migration_dirty_pages++;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200451 }
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200452 return ret;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200453}
454
Juan Quintela791fa2a2013-11-05 16:47:20 +0100455static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length)
456{
457 ram_addr_t addr;
Juan Quintelaaa8dc042013-11-06 11:33:05 +0100458 unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
Juan Quintela791fa2a2013-11-05 16:47:20 +0100459
Juan Quintelaaa8dc042013-11-06 11:33:05 +0100460 /* start address is aligned at the start of a word? */
461 if (((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) {
462 int k;
463 int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS);
464 unsigned long *src = ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION];
465
466 for (k = page; k < page + nr; k++) {
467 if (src[k]) {
468 unsigned long new_dirty;
469 new_dirty = ~migration_bitmap[k];
470 migration_bitmap[k] |= src[k];
471 new_dirty &= src[k];
472 migration_dirty_pages += ctpopl(new_dirty);
473 src[k] = 0;
474 }
475 }
476 } else {
477 for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
478 if (cpu_physical_memory_get_dirty(start + addr,
479 TARGET_PAGE_SIZE,
480 DIRTY_MEMORY_MIGRATION)) {
481 cpu_physical_memory_reset_dirty(start + addr,
482 TARGET_PAGE_SIZE,
483 DIRTY_MEMORY_MIGRATION);
484 migration_bitmap_set_dirty(start + addr);
485 }
Juan Quintela791fa2a2013-11-05 16:47:20 +0100486 }
487 }
488}
489
490
ChenLiang6c1b6632014-03-20 20:15:03 +0800491/* Fix me: there are too many global variables used in migration process. */
492static int64_t start_time;
493static int64_t bytes_xfer_prev;
494static int64_t num_dirty_pages_period;
495
496static void migration_bitmap_sync_init(void)
497{
498 start_time = 0;
499 bytes_xfer_prev = 0;
500 num_dirty_pages_period = 0;
501}
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100502
Mike Dayae3a7042013-09-05 14:41:35 -0400503/* Called with iothread lock held, to protect ram_list.dirty_memory[] */
Juan Quinteladd2df732012-07-20 10:52:51 +0200504static void migration_bitmap_sync(void)
505{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200506 RAMBlock *block;
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200507 uint64_t num_dirty_pages_init = migration_dirty_pages;
Juan Quintela8d017192012-08-13 12:31:25 +0200508 MigrationState *s = migrate_get_current();
Juan Quintela8d017192012-08-13 12:31:25 +0200509 int64_t end_time;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600510 int64_t bytes_xfer_now;
ChenLiang8bc39232014-04-04 17:57:56 +0800511 static uint64_t xbzrle_cache_miss_prev;
512 static uint64_t iterations_prev;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600513
ChenLiang71411d32014-04-04 17:57:54 +0800514 bitmap_sync_count++;
515
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600516 if (!bytes_xfer_prev) {
517 bytes_xfer_prev = ram_bytes_transferred();
518 }
Juan Quintela8d017192012-08-13 12:31:25 +0200519
520 if (!start_time) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100521 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela8d017192012-08-13 12:31:25 +0200522 }
Juan Quintela3c121932012-09-04 13:08:57 +0200523
524 trace_migration_bitmap_sync_start();
Paolo Bonzini1d671362013-04-24 10:46:55 +0200525 address_space_sync_dirty_bitmap(&address_space_memory);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200526
Mike Day0dc3f442013-09-05 14:41:35 -0400527 rcu_read_lock();
528 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
Michael S. Tsirkin9b8424d2014-12-15 22:55:32 +0200529 migration_bitmap_sync_range(block->mr->ram_addr, block->used_length);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200530 }
Mike Day0dc3f442013-09-05 14:41:35 -0400531 rcu_read_unlock();
532
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200533 trace_migration_bitmap_sync_end(migration_dirty_pages
Juan Quintela3c121932012-09-04 13:08:57 +0200534 - num_dirty_pages_init);
Juan Quintela8d017192012-08-13 12:31:25 +0200535 num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
Alex Blighbc72ad62013-08-21 16:03:08 +0100536 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela8d017192012-08-13 12:31:25 +0200537
538 /* more than 1 second = 1000 millisecons */
539 if (end_time > start_time + 1000) {
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600540 if (migrate_auto_converge()) {
541 /* The following detection logic can be refined later. For now:
542 Check to see if the dirtied bytes is 50% more than the approx.
543 amount of bytes that just got transferred since the last time we
544 were in this routine. If that happens >N times (for now N==4)
545 we turn on the throttle down logic */
546 bytes_xfer_now = ram_bytes_transferred();
547 if (s->dirty_pages_rate &&
548 (num_dirty_pages_period * TARGET_PAGE_SIZE >
549 (bytes_xfer_now - bytes_xfer_prev)/2) &&
550 (dirty_rate_high_cnt++ > 4)) {
551 trace_migration_throttle();
552 mig_throttle_on = true;
553 dirty_rate_high_cnt = 0;
554 }
555 bytes_xfer_prev = bytes_xfer_now;
556 } else {
557 mig_throttle_on = false;
558 }
ChenLiang8bc39232014-04-04 17:57:56 +0800559 if (migrate_use_xbzrle()) {
560 if (iterations_prev != 0) {
561 acct_info.xbzrle_cache_miss_rate =
562 (double)(acct_info.xbzrle_cache_miss -
563 xbzrle_cache_miss_prev) /
564 (acct_info.iterations - iterations_prev);
565 }
566 iterations_prev = acct_info.iterations;
567 xbzrle_cache_miss_prev = acct_info.xbzrle_cache_miss;
568 }
Juan Quintela8d017192012-08-13 12:31:25 +0200569 s->dirty_pages_rate = num_dirty_pages_period * 1000
570 / (end_time - start_time);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100571 s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE;
Juan Quintela8d017192012-08-13 12:31:25 +0200572 start_time = end_time;
573 num_dirty_pages_period = 0;
ChenLiang58570ed2014-04-04 17:57:55 +0800574 s->dirty_sync_count = bitmap_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200575 }
Juan Quinteladd2df732012-07-20 10:52:51 +0200576}
577
Orit Wasserman6c779f22012-07-10 12:37:13 +0300578/*
Dr. David Alan Gilbert14bcfdc2014-05-09 11:54:55 +0100579 * ram_save_page: Send the given page to the stream
580 *
581 * Returns: Number of bytes written.
582 */
583static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset,
584 bool last_stage)
585{
586 int bytes_sent;
587 int cont;
588 ram_addr_t current_addr;
589 MemoryRegion *mr = block->mr;
590 uint8_t *p;
591 int ret;
592 bool send_async = true;
593
594 cont = (block == last_sent_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
595
596 p = memory_region_get_ram_ptr(mr) + offset;
597
598 /* In doubt sent page as normal */
599 bytes_sent = -1;
600 ret = ram_control_save_page(f, block->offset,
601 offset, TARGET_PAGE_SIZE, &bytes_sent);
602
603 XBZRLE_cache_lock();
604
605 current_addr = block->offset + offset;
606 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
607 if (ret != RAM_SAVE_CONTROL_DELAYED) {
608 if (bytes_sent > 0) {
609 acct_info.norm_pages++;
610 } else if (bytes_sent == 0) {
611 acct_info.dup_pages++;
612 }
613 }
614 } else if (is_zero_range(p, TARGET_PAGE_SIZE)) {
615 acct_info.dup_pages++;
616 bytes_sent = save_block_hdr(f, block, offset, cont,
617 RAM_SAVE_FLAG_COMPRESS);
618 qemu_put_byte(f, 0);
619 bytes_sent++;
620 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
621 * page would be stale
622 */
623 xbzrle_cache_zero_page(current_addr);
624 } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
625 bytes_sent = save_xbzrle_page(f, &p, current_addr, block,
626 offset, cont, last_stage);
627 if (!last_stage) {
628 /* Can't send this cached data async, since the cache page
629 * might get updated before it gets to the wire
630 */
631 send_async = false;
632 }
633 }
634
635 /* XBZRLE overflow or normal page */
636 if (bytes_sent == -1) {
637 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
638 if (send_async) {
639 qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
640 } else {
641 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
642 }
643 bytes_sent += TARGET_PAGE_SIZE;
644 acct_info.norm_pages++;
645 }
646
647 XBZRLE_cache_unlock();
648
649 return bytes_sent;
650}
651
652/*
653 * ram_find_and_save_block: Finds a page to send and sends it to f
Orit Wasserman6c779f22012-07-10 12:37:13 +0300654 *
Mike Day0dc3f442013-09-05 14:41:35 -0400655 * Called within an RCU critical section.
656 *
Juan Quintelab823cea2012-12-10 13:27:50 +0100657 * Returns: The number of bytes written.
658 * 0 means no dirty pages
Orit Wasserman6c779f22012-07-10 12:37:13 +0300659 */
660
Dr. David Alan Gilbert14bcfdc2014-05-09 11:54:55 +0100661static int ram_find_and_save_block(QEMUFile *f, bool last_stage)
Blue Swirlad960902010-03-29 19:23:52 +0000662{
Juan Quintelab23a9a52012-10-17 20:08:04 +0200663 RAMBlock *block = last_seen_block;
Alex Williamsone44359c2010-06-25 11:09:57 -0600664 ram_addr_t offset = last_offset;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200665 bool complete_round = false;
Juan Quintelab823cea2012-12-10 13:27:50 +0100666 int bytes_sent = 0;
Avi Kivity71c510e2011-12-21 13:11:22 +0200667 MemoryRegion *mr;
Blue Swirlad960902010-03-29 19:23:52 +0000668
Alex Williamsone44359c2010-06-25 11:09:57 -0600669 if (!block)
Mike Day0dc3f442013-09-05 14:41:35 -0400670 block = QLIST_FIRST_RCU(&ram_list.blocks);
Alex Williamsone44359c2010-06-25 11:09:57 -0600671
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200672 while (true) {
Avi Kivity71c510e2011-12-21 13:11:22 +0200673 mr = block->mr;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200674 offset = migration_bitmap_find_and_reset_dirty(mr, offset);
675 if (complete_round && block == last_seen_block &&
676 offset >= last_offset) {
677 break;
678 }
Michael S. Tsirkin9b8424d2014-12-15 22:55:32 +0200679 if (offset >= block->used_length) {
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200680 offset = 0;
Mike Day0dc3f442013-09-05 14:41:35 -0400681 block = QLIST_NEXT_RCU(block, next);
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200682 if (!block) {
Mike Day0dc3f442013-09-05 14:41:35 -0400683 block = QLIST_FIRST_RCU(&ram_list.blocks);
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200684 complete_round = true;
Peter Lieven78d07ae2013-03-26 10:58:36 +0100685 ram_bulk_stage = false;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200686 }
687 } else {
Dr. David Alan Gilbert14bcfdc2014-05-09 11:54:55 +0100688 bytes_sent = ram_save_page(f, block, offset, last_stage);
Blue Swirlad960902010-03-29 19:23:52 +0000689
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300690 /* if page is unmodified, continue to the next */
Juan Quintelab823cea2012-12-10 13:27:50 +0100691 if (bytes_sent > 0) {
Juan Quintela5f718a12012-10-17 20:10:55 +0200692 last_sent_block = block;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300693 break;
694 }
Blue Swirlad960902010-03-29 19:23:52 +0000695 }
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200696 }
Mike Dayae3a7042013-09-05 14:41:35 -0400697
Juan Quintelab23a9a52012-10-17 20:08:04 +0200698 last_seen_block = block;
Alex Williamsone44359c2010-06-25 11:09:57 -0600699 last_offset = offset;
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200700 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000701}
702
703static uint64_t bytes_transferred;
704
Michael R. Hines2b0ce072013-06-25 21:35:28 -0400705void acct_update_position(QEMUFile *f, size_t size, bool zero)
706{
707 uint64_t pages = size / TARGET_PAGE_SIZE;
708 if (zero) {
709 acct_info.dup_pages += pages;
710 } else {
711 acct_info.norm_pages += pages;
712 bytes_transferred += size;
713 qemu_update_position(f, size);
714 }
715}
716
Blue Swirlad960902010-03-29 19:23:52 +0000717static ram_addr_t ram_save_remaining(void)
718{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200719 return migration_dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000720}
721
722uint64_t ram_bytes_remaining(void)
723{
724 return ram_save_remaining() * TARGET_PAGE_SIZE;
725}
726
727uint64_t ram_bytes_transferred(void)
728{
729 return bytes_transferred;
730}
731
732uint64_t ram_bytes_total(void)
733{
Alex Williamsond17b5282010-06-25 11:08:38 -0600734 RAMBlock *block;
735 uint64_t total = 0;
736
Mike Day0dc3f442013-09-05 14:41:35 -0400737 rcu_read_lock();
738 QLIST_FOREACH_RCU(block, &ram_list.blocks, next)
Michael S. Tsirkin9b8424d2014-12-15 22:55:32 +0200739 total += block->used_length;
Mike Day0dc3f442013-09-05 14:41:35 -0400740 rcu_read_unlock();
Alex Williamsond17b5282010-06-25 11:08:38 -0600741 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000742}
743
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200744void free_xbzrle_decoded_buf(void)
745{
746 g_free(xbzrle_decoded_buf);
747 xbzrle_decoded_buf = NULL;
748}
749
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300750static void migration_end(void)
751{
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100752 if (migration_bitmap) {
753 memory_global_dirty_log_stop();
754 g_free(migration_bitmap);
755 migration_bitmap = NULL;
756 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300757
Gongleifd8cec92014-03-04 21:29:21 +0800758 XBZRLE_cache_lock();
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100759 if (XBZRLE.cache) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300760 cache_fini(XBZRLE.cache);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300761 g_free(XBZRLE.encoded_buf);
762 g_free(XBZRLE.current_buf);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300763 XBZRLE.cache = NULL;
Orit Wassermanf6c64832014-01-30 20:08:33 +0200764 XBZRLE.encoded_buf = NULL;
765 XBZRLE.current_buf = NULL;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300766 }
Gongleifd8cec92014-03-04 21:29:21 +0800767 XBZRLE_cache_unlock();
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300768}
769
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200770static void ram_migration_cancel(void *opaque)
771{
772 migration_end();
773}
774
Juan Quintela5a170772012-07-17 17:02:24 +0200775static void reset_ram_globals(void)
776{
Juan Quintelab23a9a52012-10-17 20:08:04 +0200777 last_seen_block = NULL;
Juan Quintela5f718a12012-10-17 20:10:55 +0200778 last_sent_block = NULL;
Juan Quintela5a170772012-07-17 17:02:24 +0200779 last_offset = 0;
Umesh Deshpandef798b072011-08-18 11:41:17 -0700780 last_version = ram_list.version;
Peter Lieven78d07ae2013-03-26 10:58:36 +0100781 ram_bulk_stage = true;
Juan Quintela5a170772012-07-17 17:02:24 +0200782}
783
Juan Quintela4508bd92012-05-22 16:27:59 +0200784#define MAX_WAIT 50 /* ms, half buffered_file limit */
785
Mike Day0dc3f442013-09-05 14:41:35 -0400786
787/* Each of ram_save_setup, ram_save_iterate and ram_save_complete has
788 * long-running RCU critical section. When rcu-reclaims in the code
789 * start to become numerous it will be necessary to reduce the
790 * granularity of these critical sections.
791 */
792
Juan Quintelad1315aa2012-06-28 15:11:57 +0200793static int ram_save_setup(QEMUFile *f, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000794{
Juan Quintelad1315aa2012-06-28 15:11:57 +0200795 RAMBlock *block;
Dr. David Alan Gilberte30d1d82014-03-27 15:01:48 +0000796 int64_t ram_bitmap_pages; /* Size of bitmap in pages, including gaps */
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200797
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600798 mig_throttle_on = false;
799 dirty_rate_high_cnt = 0;
ChenLiang71411d32014-04-04 17:57:54 +0800800 bitmap_sync_count = 0;
ChenLiang6c1b6632014-03-20 20:15:03 +0800801 migration_bitmap_sync_init();
Blue Swirlad960902010-03-29 19:23:52 +0000802
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300803 if (migrate_use_xbzrle()) {
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000804 XBZRLE_cache_lock();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300805 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
806 TARGET_PAGE_SIZE,
807 TARGET_PAGE_SIZE);
808 if (!XBZRLE.cache) {
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000809 XBZRLE_cache_unlock();
810 error_report("Error creating cache");
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300811 return -1;
812 }
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000813 XBZRLE_cache_unlock();
Orit Wassermana17b2fd2014-01-30 20:08:37 +0200814
815 /* We prefer not to abort if there is no memory */
816 XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
817 if (!XBZRLE.encoded_buf) {
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000818 error_report("Error allocating encoded_buf");
Orit Wassermana17b2fd2014-01-30 20:08:37 +0200819 return -1;
820 }
821
822 XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
823 if (!XBZRLE.current_buf) {
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +0000824 error_report("Error allocating current_buf");
Orit Wassermana17b2fd2014-01-30 20:08:37 +0200825 g_free(XBZRLE.encoded_buf);
826 XBZRLE.encoded_buf = NULL;
827 return -1;
828 }
829
Orit Wasserman004d4c12012-08-06 21:42:56 +0300830 acct_clear();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300831 }
832
Mike Dayae3a7042013-09-05 14:41:35 -0400833 /* iothread lock needed for ram_list.dirty_memory[] */
Paolo Bonzini9b095032013-02-22 17:36:28 +0100834 qemu_mutex_lock_iothread();
835 qemu_mutex_lock_ramlist();
Mike Day0dc3f442013-09-05 14:41:35 -0400836 rcu_read_lock();
Paolo Bonzini9b095032013-02-22 17:36:28 +0100837 bytes_transferred = 0;
838 reset_ram_globals();
839
Dr. David Alan Gilberte30d1d82014-03-27 15:01:48 +0000840 ram_bitmap_pages = last_ram_offset() >> TARGET_PAGE_BITS;
841 migration_bitmap = bitmap_new(ram_bitmap_pages);
842 bitmap_set(migration_bitmap, 0, ram_bitmap_pages);
843
844 /*
845 * Count the total number of pages used by ram blocks not including any
846 * gaps due to alignment or unplugs.
847 */
848 migration_dirty_pages = 0;
Mike Day0dc3f442013-09-05 14:41:35 -0400849 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
Dr. David Alan Gilberte30d1d82014-03-27 15:01:48 +0000850 uint64_t block_pages;
851
Michael S. Tsirkin9b8424d2014-12-15 22:55:32 +0200852 block_pages = block->used_length >> TARGET_PAGE_BITS;
Dr. David Alan Gilberte30d1d82014-03-27 15:01:48 +0000853 migration_dirty_pages += block_pages;
854 }
855
Juan Quintelad1315aa2012-06-28 15:11:57 +0200856 memory_global_dirty_log_start();
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200857 migration_bitmap_sync();
Mike Day0dc3f442013-09-05 14:41:35 -0400858 qemu_mutex_unlock_ramlist();
Paolo Bonzini9b095032013-02-22 17:36:28 +0100859 qemu_mutex_unlock_iothread();
Juan Quintelad1315aa2012-06-28 15:11:57 +0200860
861 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
862
Mike Day0dc3f442013-09-05 14:41:35 -0400863 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200864 qemu_put_byte(f, strlen(block->idstr));
865 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
Michael S. Tsirkin9b8424d2014-12-15 22:55:32 +0200866 qemu_put_be64(f, block->used_length);
Juan Quintelad1315aa2012-06-28 15:11:57 +0200867 }
868
Mike Day0dc3f442013-09-05 14:41:35 -0400869 rcu_read_unlock();
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400870
871 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
872 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
873
Juan Quintelad1315aa2012-06-28 15:11:57 +0200874 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
875
876 return 0;
877}
878
Juan Quintela16310a32012-06-28 15:31:37 +0200879static int ram_save_iterate(QEMUFile *f, void *opaque)
Juan Quintelad1315aa2012-06-28 15:11:57 +0200880{
Blue Swirlad960902010-03-29 19:23:52 +0000881 int ret;
882 int i;
Juan Quintelae4ed1542012-09-21 11:18:18 +0200883 int64_t t0;
Juan Quintelab823cea2012-12-10 13:27:50 +0100884 int total_sent = 0;
Blue Swirlad960902010-03-29 19:23:52 +0000885
Mike Day0dc3f442013-09-05 14:41:35 -0400886 rcu_read_lock();
Umesh Deshpandef798b072011-08-18 11:41:17 -0700887 if (ram_list.version != last_version) {
888 reset_ram_globals();
889 }
890
Mike Day0dc3f442013-09-05 14:41:35 -0400891 /* Read version before ram_list.blocks */
892 smp_rmb();
893
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400894 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
895
Alex Blighbc72ad62013-08-21 16:03:08 +0100896 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Juan Quintela4508bd92012-05-22 16:27:59 +0200897 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200898 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200899 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000900
Dr. David Alan Gilbert14bcfdc2014-05-09 11:54:55 +0100901 bytes_sent = ram_find_and_save_block(f, false);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300902 /* no more blocks to sent */
Juan Quintelab823cea2012-12-10 13:27:50 +0100903 if (bytes_sent == 0) {
Blue Swirlad960902010-03-29 19:23:52 +0000904 break;
905 }
Juan Quintelab823cea2012-12-10 13:27:50 +0100906 total_sent += bytes_sent;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300907 acct_info.iterations++;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600908 check_guest_throttling();
Juan Quintela4508bd92012-05-22 16:27:59 +0200909 /* we want to check in the 1st loop, just in case it was the 1st time
910 and we had to sync the dirty bitmap.
911 qemu_get_clock_ns() is a bit expensive, so we only check each some
912 iterations
913 */
914 if ((i & 63) == 0) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100915 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
Juan Quintela4508bd92012-05-22 16:27:59 +0200916 if (t1 > MAX_WAIT) {
Igor Mitsyankoef37a692012-09-05 13:04:56 +0400917 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
Juan Quintela4508bd92012-05-22 16:27:59 +0200918 t1, i);
919 break;
920 }
921 }
922 i++;
Blue Swirlad960902010-03-29 19:23:52 +0000923 }
Mike Day0dc3f442013-09-05 14:41:35 -0400924 rcu_read_unlock();
Paolo Bonzinifb3409d2012-12-20 11:25:45 +0100925
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400926 /*
927 * Must occur before EOS (or any QEMUFile operation)
928 * because of RDMA protocol.
929 */
930 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
931
Lei Li6cd0bed2013-09-04 17:02:36 +0800932 bytes_transferred += total_sent;
933
934 /*
935 * Do not count these 8 bytes into total_sent, so that we can
936 * return 0 if no page had been dirtied.
937 */
938 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
939 bytes_transferred += 8;
940
941 ret = qemu_file_get_error(f);
Juan Quintela29757252011-10-19 15:22:18 +0200942 if (ret < 0) {
943 return ret;
944 }
945
Juan Quintelab823cea2012-12-10 13:27:50 +0100946 return total_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000947}
948
Mike Dayae3a7042013-09-05 14:41:35 -0400949/* Called with iothread lock */
Juan Quintela16310a32012-06-28 15:31:37 +0200950static int ram_save_complete(QEMUFile *f, void *opaque)
951{
Mike Day0dc3f442013-09-05 14:41:35 -0400952 rcu_read_lock();
953
Paolo Bonzini9c339482012-12-20 11:26:04 +0100954 migration_bitmap_sync();
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700955
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400956 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
957
Juan Quintela16310a32012-06-28 15:31:37 +0200958 /* try transferring iterative blocks of memory */
959
960 /* flush all remaining blocks regardless of rate limiting */
Orit Wasserman6c779f22012-07-10 12:37:13 +0300961 while (true) {
962 int bytes_sent;
963
Dr. David Alan Gilbert14bcfdc2014-05-09 11:54:55 +0100964 bytes_sent = ram_find_and_save_block(f, true);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300965 /* no more blocks to sent */
Juan Quintelab823cea2012-12-10 13:27:50 +0100966 if (bytes_sent == 0) {
Orit Wasserman6c779f22012-07-10 12:37:13 +0300967 break;
968 }
Juan Quintela16310a32012-06-28 15:31:37 +0200969 bytes_transferred += bytes_sent;
970 }
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400971
972 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100973 migration_end();
Juan Quintela16310a32012-06-28 15:31:37 +0200974
Mike Day0dc3f442013-09-05 14:41:35 -0400975 rcu_read_unlock();
Blue Swirlad960902010-03-29 19:23:52 +0000976 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
977
Alex Williamsona55bbe32010-06-25 11:10:05 -0600978 return 0;
979}
980
Juan Quintelae4ed1542012-09-21 11:18:18 +0200981static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
982{
983 uint64_t remaining_size;
984
985 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
986
987 if (remaining_size < max_size) {
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100988 qemu_mutex_lock_iothread();
Mike Day0dc3f442013-09-05 14:41:35 -0400989 rcu_read_lock();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200990 migration_bitmap_sync();
Mike Day0dc3f442013-09-05 14:41:35 -0400991 rcu_read_unlock();
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100992 qemu_mutex_unlock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200993 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
994 }
995 return remaining_size;
996}
997
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300998static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
999{
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001000 unsigned int xh_len;
1001 int xh_flags;
1002
Gonglei (Arei)905f26f2014-01-30 20:08:35 +02001003 if (!xbzrle_decoded_buf) {
1004 xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001005 }
1006
1007 /* extract RLE header */
1008 xh_flags = qemu_get_byte(f);
1009 xh_len = qemu_get_be16(f);
1010
1011 if (xh_flags != ENCODING_FLAG_XBZRLE) {
Le Tan0971f1b2014-05-21 08:10:38 +08001012 error_report("Failed to load XBZRLE page - wrong compression!");
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001013 return -1;
1014 }
1015
1016 if (xh_len > TARGET_PAGE_SIZE) {
Le Tan0971f1b2014-05-21 08:10:38 +08001017 error_report("Failed to load XBZRLE page - len overflow!");
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001018 return -1;
1019 }
1020 /* load data and decode */
Gonglei (Arei)905f26f2014-01-30 20:08:35 +02001021 qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001022
1023 /* decode RLE */
Chen Gangfb626662014-05-10 20:51:24 +08001024 if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
1025 TARGET_PAGE_SIZE) == -1) {
Le Tan0971f1b2014-05-21 08:10:38 +08001026 error_report("Failed to load XBZRLE page - decode error!");
Chen Gangfb626662014-05-10 20:51:24 +08001027 return -1;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001028 }
1029
Chen Gangfb626662014-05-10 20:51:24 +08001030 return 0;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001031}
1032
Mike Day0dc3f442013-09-05 14:41:35 -04001033/* Must be called from within a rcu critical section.
1034 * Returns a pointer from within the RCU-protected ram_list.
1035 */
Alex Williamsona55bbe32010-06-25 11:10:05 -06001036static inline void *host_from_stream_offset(QEMUFile *f,
1037 ram_addr_t offset,
1038 int flags)
1039{
1040 static RAMBlock *block = NULL;
1041 char id[256];
1042 uint8_t len;
1043
1044 if (flags & RAM_SAVE_FLAG_CONTINUE) {
Michael S. Tsirkin9b8424d2014-12-15 22:55:32 +02001045 if (!block || block->max_length <= offset) {
Le Tan0971f1b2014-05-21 08:10:38 +08001046 error_report("Ack, bad migration stream!");
Alex Williamsona55bbe32010-06-25 11:10:05 -06001047 return NULL;
1048 }
1049
Avi Kivitydc94a7e2011-12-21 13:54:33 +02001050 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -06001051 }
1052
1053 len = qemu_get_byte(f);
1054 qemu_get_buffer(f, (uint8_t *)id, len);
1055 id[len] = 0;
1056
Mike Day0dc3f442013-09-05 14:41:35 -04001057 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
Michael S. Tsirkin9b8424d2014-12-15 22:55:32 +02001058 if (!strncmp(id, block->idstr, sizeof(id)) &&
1059 block->max_length > offset) {
Avi Kivitydc94a7e2011-12-21 13:54:33 +02001060 return memory_region_get_ram_ptr(block->mr) + offset;
Michael S. Tsirkin0be839a2014-11-12 11:44:39 +02001061 }
Alex Williamsona55bbe32010-06-25 11:10:05 -06001062 }
1063
Le Tan0971f1b2014-05-21 08:10:38 +08001064 error_report("Can't find block %s!", id);
Alex Williamsona55bbe32010-06-25 11:10:05 -06001065 return NULL;
1066}
1067
Michael R. Hines44c3b582013-07-22 10:01:53 -04001068/*
1069 * If a page (or a whole RDMA chunk) has been
1070 * determined to be zero, then zap it.
1071 */
1072void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
1073{
Isaku Yamahatad613a562013-09-21 01:23:37 +09001074 if (ch != 0 || !is_zero_range(host, size)) {
Michael R. Hines44c3b582013-07-22 10:01:53 -04001075 memset(host, ch, size);
Michael R. Hines44c3b582013-07-22 10:01:53 -04001076 }
1077}
1078
Juan Quintela7908c782012-06-26 18:46:10 +02001079static int ram_load(QEMUFile *f, void *opaque, int version_id)
Blue Swirlad960902010-03-29 19:23:52 +00001080{
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001081 int flags = 0, ret = 0;
Orit Wasserman3a697f62012-06-19 18:43:15 +03001082 static uint64_t seq_iter;
1083
1084 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +00001085
ChenLiang21a246a2014-04-25 17:06:20 +08001086 if (version_id != 4) {
Chen Gang4798fe52014-05-01 21:28:11 +08001087 ret = -EINVAL;
Blue Swirlad960902010-03-29 19:23:52 +00001088 }
1089
Mike Day0dc3f442013-09-05 14:41:35 -04001090 /* This RCU critical section can be very long running.
1091 * When RCU reclaims in the code start to become numerous,
1092 * it will be necessary to reduce the granularity of this
1093 * critical section.
1094 */
1095 rcu_read_lock();
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001096 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) {
1097 ram_addr_t addr, total_ram_bytes;
1098 void *host;
1099 uint8_t ch;
Blue Swirlad960902010-03-29 19:23:52 +00001100
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001101 addr = qemu_get_be64(f);
Blue Swirlad960902010-03-29 19:23:52 +00001102 flags = addr & ~TARGET_PAGE_MASK;
1103 addr &= TARGET_PAGE_MASK;
1104
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001105 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
1106 case RAM_SAVE_FLAG_MEM_SIZE:
ChenLiang21a246a2014-04-25 17:06:20 +08001107 /* Synchronize RAM block list */
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001108 total_ram_bytes = addr;
1109 while (!ret && total_ram_bytes) {
ChenLiang21a246a2014-04-25 17:06:20 +08001110 RAMBlock *block;
1111 uint8_t len;
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001112 char id[256];
1113 ram_addr_t length;
Alex Williamson97ab12d2010-06-25 11:09:50 -06001114
ChenLiang21a246a2014-04-25 17:06:20 +08001115 len = qemu_get_byte(f);
1116 qemu_get_buffer(f, (uint8_t *)id, len);
1117 id[len] = 0;
1118 length = qemu_get_be64(f);
Alex Williamson97ab12d2010-06-25 11:09:50 -06001119
Mike Day0dc3f442013-09-05 14:41:35 -04001120 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
ChenLiang21a246a2014-04-25 17:06:20 +08001121 if (!strncmp(id, block->idstr, sizeof(id))) {
Michael S. Tsirkinb0cc3f82014-11-17 17:55:43 +02001122 if (length != block->used_length) {
1123 Error *local_err = NULL;
1124
1125 ret = qemu_ram_resize(block->offset, length, &local_err);
1126 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001127 error_report_err(local_err);
Michael S. Tsirkinb0cc3f82014-11-17 17:55:43 +02001128 }
Alex Williamson97ab12d2010-06-25 11:09:50 -06001129 }
ChenLiang21a246a2014-04-25 17:06:20 +08001130 break;
Alex Williamson97ab12d2010-06-25 11:09:50 -06001131 }
Alex Williamson97ab12d2010-06-25 11:09:50 -06001132 }
ChenLiang21a246a2014-04-25 17:06:20 +08001133
1134 if (!block) {
Le Tan0971f1b2014-05-21 08:10:38 +08001135 error_report("Unknown ramblock \"%s\", cannot "
1136 "accept migration", id);
ChenLiang21a246a2014-04-25 17:06:20 +08001137 ret = -EINVAL;
Peter Lievendb80fac2014-06-10 11:29:16 +02001138 }
ChenLiang21a246a2014-04-25 17:06:20 +08001139
1140 total_ram_bytes -= length;
Blue Swirlad960902010-03-29 19:23:52 +00001141 }
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001142 break;
1143 case RAM_SAVE_FLAG_COMPRESS:
Avi Kivityf09f2182011-12-21 13:37:56 +02001144 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +02001145 if (!host) {
Peter Lievendb80fac2014-06-10 11:29:16 +02001146 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
Chen Gang4798fe52014-05-01 21:28:11 +08001147 ret = -EINVAL;
Peter Lievendb80fac2014-06-10 11:29:16 +02001148 break;
Michael S. Tsirkin492fb992010-10-17 20:43:40 +02001149 }
Alex Williamson97ab12d2010-06-25 11:09:50 -06001150 ch = qemu_get_byte(f);
Michael R. Hines44c3b582013-07-22 10:01:53 -04001151 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001152 break;
1153 case RAM_SAVE_FLAG_PAGE:
Avi Kivityf09f2182011-12-21 13:37:56 +02001154 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +03001155 if (!host) {
Peter Lievendb80fac2014-06-10 11:29:16 +02001156 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
Chen Gang4798fe52014-05-01 21:28:11 +08001157 ret = -EINVAL;
Peter Lievendb80fac2014-06-10 11:29:16 +02001158 break;
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +03001159 }
Alex Williamson97ab12d2010-06-25 11:09:50 -06001160 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001161 break;
1162 case RAM_SAVE_FLAG_XBZRLE:
1163 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001164 if (!host) {
Peter Lievendb80fac2014-06-10 11:29:16 +02001165 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
Chen Gang4798fe52014-05-01 21:28:11 +08001166 ret = -EINVAL;
Peter Lievendb80fac2014-06-10 11:29:16 +02001167 break;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001168 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001169 if (load_xbzrle(f, addr, host) < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +02001170 error_report("Failed to decompress XBZRLE page at "
1171 RAM_ADDR_FMT, addr);
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001172 ret = -EINVAL;
Peter Lievendb80fac2014-06-10 11:29:16 +02001173 break;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001174 }
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001175 break;
1176 case RAM_SAVE_FLAG_EOS:
Peter Lievendb80fac2014-06-10 11:29:16 +02001177 /* normal exit */
1178 break;
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001179 default:
1180 if (flags & RAM_SAVE_FLAG_HOOK) {
1181 ram_control_load_hook(f, flags);
1182 } else {
1183 error_report("Unknown combination of migration flags: %#x",
1184 flags);
1185 ret = -EINVAL;
1186 }
Blue Swirlad960902010-03-29 19:23:52 +00001187 }
Peter Lieven5b0e9dd2014-06-24 11:32:36 +02001188 if (!ret) {
1189 ret = qemu_file_get_error(f);
1190 }
Peter Lievendb80fac2014-06-10 11:29:16 +02001191 }
Blue Swirlad960902010-03-29 19:23:52 +00001192
Mike Day0dc3f442013-09-05 14:41:35 -04001193 rcu_read_unlock();
Igor Mitsyankoef37a692012-09-05 13:04:56 +04001194 DPRINTF("Completed load of VM with exit code %d seq iteration "
1195 "%" PRIu64 "\n", ret, seq_iter);
Orit Wasserman3a697f62012-06-19 18:43:15 +03001196 return ret;
Blue Swirlad960902010-03-29 19:23:52 +00001197}
1198
Dr. David Alan Gilbert0d6ab3a2014-03-19 18:32:30 +00001199static SaveVMHandlers savevm_ram_handlers = {
Juan Quintelad1315aa2012-06-28 15:11:57 +02001200 .save_live_setup = ram_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +02001201 .save_live_iterate = ram_save_iterate,
1202 .save_live_complete = ram_save_complete,
Juan Quintelae4ed1542012-09-21 11:18:18 +02001203 .save_live_pending = ram_save_pending,
Juan Quintela7908c782012-06-26 18:46:10 +02001204 .load_state = ram_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001205 .cancel = ram_migration_cancel,
Juan Quintela7908c782012-06-26 18:46:10 +02001206};
1207
Dr. David Alan Gilbert0d6ab3a2014-03-19 18:32:30 +00001208void ram_mig_init(void)
1209{
Dr. David Alan Gilbertd97326e2014-03-19 18:32:31 +00001210 qemu_mutex_init(&XBZRLE.lock);
Dr. David Alan Gilbert0d6ab3a2014-03-19 18:32:30 +00001211 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
1212}
1213
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001214struct soundhw {
1215 const char *name;
1216 const char *descr;
1217 int enabled;
1218 int isa;
1219 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +01001220 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001221 int (*init_pci) (PCIBus *bus);
1222 } init;
1223};
1224
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001225static struct soundhw soundhw[9];
1226static int soundhw_count;
Blue Swirlad960902010-03-29 19:23:52 +00001227
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001228void isa_register_soundhw(const char *name, const char *descr,
1229 int (*init_isa)(ISABus *bus))
1230{
1231 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1232 soundhw[soundhw_count].name = name;
1233 soundhw[soundhw_count].descr = descr;
1234 soundhw[soundhw_count].isa = 1;
1235 soundhw[soundhw_count].init.init_isa = init_isa;
1236 soundhw_count++;
1237}
Blue Swirlad960902010-03-29 19:23:52 +00001238
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001239void pci_register_soundhw(const char *name, const char *descr,
1240 int (*init_pci)(PCIBus *bus))
1241{
1242 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1243 soundhw[soundhw_count].name = name;
1244 soundhw[soundhw_count].descr = descr;
1245 soundhw[soundhw_count].isa = 0;
1246 soundhw[soundhw_count].init.init_pci = init_pci;
1247 soundhw_count++;
1248}
Blue Swirlad960902010-03-29 19:23:52 +00001249
1250void select_soundhw(const char *optarg)
1251{
1252 struct soundhw *c;
1253
Peter Maydellc8057f92012-08-02 13:45:54 +01001254 if (is_help_option(optarg)) {
Blue Swirlad960902010-03-29 19:23:52 +00001255 show_valid_cards:
1256
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001257 if (soundhw_count) {
1258 printf("Valid sound card names (comma separated):\n");
1259 for (c = soundhw; c->name; ++c) {
1260 printf ("%-11s %s\n", c->name, c->descr);
1261 }
1262 printf("\n-soundhw all will enable all of the above\n");
1263 } else {
1264 printf("Machine has no user-selectable audio hardware "
1265 "(it may or may not have always-present audio hardware).\n");
Blue Swirlad960902010-03-29 19:23:52 +00001266 }
Peter Maydellc8057f92012-08-02 13:45:54 +01001267 exit(!is_help_option(optarg));
Blue Swirlad960902010-03-29 19:23:52 +00001268 }
1269 else {
1270 size_t l;
1271 const char *p;
1272 char *e;
1273 int bad_card = 0;
1274
1275 if (!strcmp(optarg, "all")) {
1276 for (c = soundhw; c->name; ++c) {
1277 c->enabled = 1;
1278 }
1279 return;
1280 }
1281
1282 p = optarg;
1283 while (*p) {
1284 e = strchr(p, ',');
1285 l = !e ? strlen(p) : (size_t) (e - p);
1286
1287 for (c = soundhw; c->name; ++c) {
1288 if (!strncmp(c->name, p, l) && !c->name[l]) {
1289 c->enabled = 1;
1290 break;
1291 }
1292 }
1293
1294 if (!c->name) {
1295 if (l > 80) {
Le Tan0971f1b2014-05-21 08:10:38 +08001296 error_report("Unknown sound card name (too big to show)");
Blue Swirlad960902010-03-29 19:23:52 +00001297 }
1298 else {
Le Tan0971f1b2014-05-21 08:10:38 +08001299 error_report("Unknown sound card name `%.*s'",
1300 (int) l, p);
Blue Swirlad960902010-03-29 19:23:52 +00001301 }
1302 bad_card = 1;
1303 }
1304 p += l + (e != NULL);
1305 }
1306
1307 if (bad_card) {
1308 goto show_valid_cards;
1309 }
1310 }
1311}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001312
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001313void audio_init(void)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001314{
1315 struct soundhw *c;
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001316 ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
1317 PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001318
1319 for (c = soundhw; c->name; ++c) {
1320 if (c->enabled) {
1321 if (c->isa) {
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001322 if (!isa_bus) {
Le Tan0971f1b2014-05-21 08:10:38 +08001323 error_report("ISA bus not available for %s", c->name);
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001324 exit(1);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001325 }
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001326 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001327 } else {
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001328 if (!pci_bus) {
Le Tan0971f1b2014-05-21 08:10:38 +08001329 error_report("PCI bus not available for %s", c->name);
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001330 exit(1);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001331 }
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001332 c->init.init_pci(pci_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001333 }
1334 }
1335 }
1336}
Blue Swirlad960902010-03-29 19:23:52 +00001337
1338int qemu_uuid_parse(const char *str, uint8_t *uuid)
1339{
1340 int ret;
1341
1342 if (strlen(str) != 36) {
1343 return -1;
1344 }
1345
1346 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1347 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1348 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1349 &uuid[15]);
1350
1351 if (ret != 16) {
1352 return -1;
1353 }
Blue Swirlad960902010-03-29 19:23:52 +00001354 return 0;
1355}
1356
Laszlo Ersek0c764a92013-03-21 00:23:17 +01001357void do_acpitable_option(const QemuOpts *opts)
Blue Swirlad960902010-03-29 19:23:52 +00001358{
1359#ifdef TARGET_I386
Laszlo Ersek23084322013-03-21 00:23:19 +01001360 Error *err = NULL;
1361
1362 acpi_table_add(opts, &err);
1363 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04001364 error_report("Wrong acpi table provided: %s",
1365 error_get_pretty(err));
Laszlo Ersek23084322013-03-21 00:23:19 +01001366 error_free(err);
Blue Swirlad960902010-03-29 19:23:52 +00001367 exit(1);
1368 }
1369#endif
1370}
1371
Markus Armbruster4f953d22013-08-16 15:18:29 +02001372void do_smbios_option(QemuOpts *opts)
Blue Swirlad960902010-03-29 19:23:52 +00001373{
1374#ifdef TARGET_I386
Markus Armbruster4f953d22013-08-16 15:18:29 +02001375 smbios_entry_add(opts);
Blue Swirlad960902010-03-29 19:23:52 +00001376#endif
1377}
1378
1379void cpudef_init(void)
1380{
1381#if defined(cpudef_setup)
1382 cpudef_setup(); /* parse cpu definitions in target config file */
1383#endif
1384}
1385
Blue Swirlad960902010-03-29 19:23:52 +00001386int kvm_available(void)
1387{
1388#ifdef CONFIG_KVM
1389 return 1;
1390#else
1391 return 0;
1392#endif
1393}
1394
1395int xen_available(void)
1396{
1397#ifdef CONFIG_XEN
1398 return 1;
1399#else
1400 return 0;
1401#endif
1402}
Daniel P. Berrange99afc912012-08-20 15:31:38 +01001403
1404
1405TargetInfo *qmp_query_target(Error **errp)
1406{
1407 TargetInfo *info = g_malloc0(sizeof(*info));
1408
Paolo Bonzinic02a9552013-06-04 14:45:28 +02001409 info->arch = g_strdup(TARGET_NAME);
Daniel P. Berrange99afc912012-08-20 15:31:38 +01001410
1411 return info;
1412}
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001413
1414/* Stub function that's gets run on the vcpu when its brought out of the
1415 VM to run inside qemu via async_run_on_cpu()*/
1416static void mig_sleep_cpu(void *opq)
1417{
1418 qemu_mutex_unlock_iothread();
1419 g_usleep(30*1000);
1420 qemu_mutex_lock_iothread();
1421}
1422
1423/* To reduce the dirty rate explicitly disallow the VCPUs from spending
1424 much time in the VM. The migration thread will try to catchup.
1425 Workload will experience a performance drop.
1426*/
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001427static void mig_throttle_guest_down(void)
1428{
Andreas Färber38fcbd32013-07-07 19:50:23 +02001429 CPUState *cpu;
1430
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001431 qemu_mutex_lock_iothread();
Andreas Färber38fcbd32013-07-07 19:50:23 +02001432 CPU_FOREACH(cpu) {
1433 async_run_on_cpu(cpu, mig_sleep_cpu, NULL);
1434 }
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001435 qemu_mutex_unlock_iothread();
1436}
1437
1438static void check_guest_throttling(void)
1439{
1440 static int64_t t0;
1441 int64_t t1;
1442
1443 if (!mig_throttle_on) {
1444 return;
1445 }
1446
1447 if (!t0) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001448 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001449 return;
1450 }
1451
Alex Blighbc72ad62013-08-21 16:03:08 +01001452 t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001453
1454 /* If it has been more than 40 ms since the last time the guest
1455 * was throttled then do it again.
1456 */
1457 if (40 < (t1-t0)/1000000) {
1458 mig_throttle_guest_down();
1459 t0 = t1;
1460 }
1461}