blob: 0e8c8b5fc101b04d0679a183e2db0c8eb83d3a73 [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"
Blue Swirlad960902010-03-29 19:23:52 +000053
Orit Wasserman3a697f62012-06-19 18:43:15 +030054#ifdef DEBUG_ARCH_INIT
55#define DPRINTF(fmt, ...) \
56 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
57#else
58#define DPRINTF(fmt, ...) \
59 do { } while (0)
60#endif
61
Blue Swirlad960902010-03-29 19:23:52 +000062#ifdef TARGET_SPARC
63int graphic_width = 1024;
64int graphic_height = 768;
65int graphic_depth = 8;
66#else
67int graphic_width = 800;
68int graphic_height = 600;
Alexander Graff1ff0e82013-06-20 14:06:27 +020069int graphic_depth = 32;
Blue Swirlad960902010-03-29 19:23:52 +000070#endif
71
Blue Swirlad960902010-03-29 19:23:52 +000072
73#if defined(TARGET_ALPHA)
74#define QEMU_ARCH QEMU_ARCH_ALPHA
75#elif defined(TARGET_ARM)
76#define QEMU_ARCH QEMU_ARCH_ARM
77#elif defined(TARGET_CRIS)
78#define QEMU_ARCH QEMU_ARCH_CRIS
79#elif defined(TARGET_I386)
80#define QEMU_ARCH QEMU_ARCH_I386
81#elif defined(TARGET_M68K)
82#define QEMU_ARCH QEMU_ARCH_M68K
Michael Walle81ea0e12011-02-17 23:45:02 +010083#elif defined(TARGET_LM32)
84#define QEMU_ARCH QEMU_ARCH_LM32
Blue Swirlad960902010-03-29 19:23:52 +000085#elif defined(TARGET_MICROBLAZE)
86#define QEMU_ARCH QEMU_ARCH_MICROBLAZE
87#elif defined(TARGET_MIPS)
88#define QEMU_ARCH QEMU_ARCH_MIPS
Anthony Greend15a9c22013-03-18 15:49:25 -040089#elif defined(TARGET_MOXIE)
90#define QEMU_ARCH QEMU_ARCH_MOXIE
Jia Liue67db062012-07-20 15:50:39 +080091#elif defined(TARGET_OPENRISC)
92#define QEMU_ARCH QEMU_ARCH_OPENRISC
Blue Swirlad960902010-03-29 19:23:52 +000093#elif defined(TARGET_PPC)
94#define QEMU_ARCH QEMU_ARCH_PPC
95#elif defined(TARGET_S390X)
96#define QEMU_ARCH QEMU_ARCH_S390X
97#elif defined(TARGET_SH4)
98#define QEMU_ARCH QEMU_ARCH_SH4
99#elif defined(TARGET_SPARC)
100#define QEMU_ARCH QEMU_ARCH_SPARC
Max Filippov23288262011-09-06 03:55:25 +0400101#elif defined(TARGET_XTENSA)
102#define QEMU_ARCH QEMU_ARCH_XTENSA
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800103#elif defined(TARGET_UNICORE32)
104#define QEMU_ARCH QEMU_ARCH_UNICORE32
Blue Swirlad960902010-03-29 19:23:52 +0000105#endif
106
107const uint32_t arch_type = QEMU_ARCH;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600108static bool mig_throttle_on;
109static int dirty_rate_high_cnt;
110static void check_guest_throttling(void);
Blue Swirlad960902010-03-29 19:23:52 +0000111
112/***********************************************************/
113/* ram save/restore */
114
Yoshiaki Tamurad20878d2010-08-18 13:30:12 +0900115#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
116#define RAM_SAVE_FLAG_COMPRESS 0x02
117#define RAM_SAVE_FLAG_MEM_SIZE 0x04
118#define RAM_SAVE_FLAG_PAGE 0x08
119#define RAM_SAVE_FLAG_EOS 0x10
120#define RAM_SAVE_FLAG_CONTINUE 0x20
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300121#define RAM_SAVE_FLAG_XBZRLE 0x40
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400122/* 0x80 is reserved in migration.h start with 0x100 next */
Blue Swirlad960902010-03-29 19:23:52 +0000123
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300124
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
135
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300136int qemu_read_default_config_files(bool userconfig)
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300137{
138 int ret;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300139 struct defconfig_file *f;
140
141 for (f = default_config_files; f->filename; f++) {
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300142 if (!userconfig && f->userconfig) {
143 continue;
144 }
Eduardo Habkost756557d2012-05-02 13:07:27 -0300145 ret = qemu_read_config_file(f->filename);
146 if (ret < 0 && ret != -ENOENT) {
147 return ret;
148 }
149 }
Laszlo Ersek4d8b3c62013-03-21 00:23:13 +0100150
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300151 return 0;
152}
153
Isaku Yamahatadc3c26a2013-09-21 01:23:36 +0900154static inline bool is_zero_range(uint8_t *p, uint64_t size)
Blue Swirlad960902010-03-29 19:23:52 +0000155{
Isaku Yamahatadc3c26a2013-09-21 01:23:36 +0900156 return buffer_find_nonzero_offset(p, size) == size;
Blue Swirlad960902010-03-29 19:23:52 +0000157}
158
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300159/* struct contains XBZRLE cache and a static page
160 used by the compression */
161static struct {
162 /* buffer used for XBZRLE encoding */
163 uint8_t *encoded_buf;
164 /* buffer for storing page content */
165 uint8_t *current_buf;
166 /* buffer used for XBZRLE decoding */
167 uint8_t *decoded_buf;
168 /* Cache for XBZRLE */
169 PageCache *cache;
170} XBZRLE = {
171 .encoded_buf = NULL,
172 .current_buf = NULL,
173 .decoded_buf = NULL,
174 .cache = NULL,
175};
176
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300177
178int64_t xbzrle_cache_resize(int64_t new_size)
179{
180 if (XBZRLE.cache != NULL) {
181 return cache_resize(XBZRLE.cache, new_size / TARGET_PAGE_SIZE) *
182 TARGET_PAGE_SIZE;
183 }
184 return pow2floor(new_size);
185}
186
Orit Wasserman004d4c12012-08-06 21:42:56 +0300187/* accounting for migration statistics */
188typedef struct AccountingInfo {
189 uint64_t dup_pages;
Peter Lievenf1c72792013-03-26 10:58:37 +0100190 uint64_t skipped_pages;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300191 uint64_t norm_pages;
192 uint64_t iterations;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300193 uint64_t xbzrle_bytes;
194 uint64_t xbzrle_pages;
195 uint64_t xbzrle_cache_miss;
196 uint64_t xbzrle_overflows;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300197} AccountingInfo;
198
199static AccountingInfo acct_info;
200
201static void acct_clear(void)
202{
203 memset(&acct_info, 0, sizeof(acct_info));
204}
205
206uint64_t dup_mig_bytes_transferred(void)
207{
208 return acct_info.dup_pages * TARGET_PAGE_SIZE;
209}
210
211uint64_t dup_mig_pages_transferred(void)
212{
213 return acct_info.dup_pages;
214}
215
Peter Lievenf1c72792013-03-26 10:58:37 +0100216uint64_t skipped_mig_bytes_transferred(void)
217{
218 return acct_info.skipped_pages * TARGET_PAGE_SIZE;
219}
220
221uint64_t skipped_mig_pages_transferred(void)
222{
223 return acct_info.skipped_pages;
224}
225
Orit Wasserman004d4c12012-08-06 21:42:56 +0300226uint64_t norm_mig_bytes_transferred(void)
227{
228 return acct_info.norm_pages * TARGET_PAGE_SIZE;
229}
230
231uint64_t norm_mig_pages_transferred(void)
232{
233 return acct_info.norm_pages;
234}
235
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300236uint64_t xbzrle_mig_bytes_transferred(void)
237{
238 return acct_info.xbzrle_bytes;
239}
240
241uint64_t xbzrle_mig_pages_transferred(void)
242{
243 return acct_info.xbzrle_pages;
244}
245
246uint64_t xbzrle_mig_pages_cache_miss(void)
247{
248 return acct_info.xbzrle_cache_miss;
249}
250
251uint64_t xbzrle_mig_pages_overflow(void)
252{
253 return acct_info.xbzrle_overflows;
254}
255
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200256static size_t save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
257 int cont, int flag)
Orit Wasserman0c51f432012-06-19 18:43:14 +0300258{
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200259 size_t size;
Orit Wasserman0c51f432012-06-19 18:43:14 +0300260
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200261 qemu_put_be64(f, offset | cont | flag);
262 size = 8;
263
264 if (!cont) {
265 qemu_put_byte(f, strlen(block->idstr));
266 qemu_put_buffer(f, (uint8_t *)block->idstr,
267 strlen(block->idstr));
268 size += 1 + strlen(block->idstr);
269 }
270 return size;
Orit Wasserman0c51f432012-06-19 18:43:14 +0300271}
272
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300273#define ENCODING_FLAG_XBZRLE 0x1
274
275static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
276 ram_addr_t current_addr, RAMBlock *block,
Juan Quinteladd051c72012-08-06 21:42:58 +0300277 ram_addr_t offset, int cont, bool last_stage)
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300278{
279 int encoded_len = 0, bytes_sent = -1;
280 uint8_t *prev_cached_page;
281
282 if (!cache_is_cached(XBZRLE.cache, current_addr)) {
Juan Quinteladd051c72012-08-06 21:42:58 +0300283 if (!last_stage) {
Peter Lievenee0b44a2013-02-25 19:12:04 +0200284 cache_insert(XBZRLE.cache, current_addr, current_data);
Juan Quinteladd051c72012-08-06 21:42:58 +0300285 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300286 acct_info.xbzrle_cache_miss++;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300287 return -1;
288 }
289
290 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
291
292 /* save current buffer into memory */
293 memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
294
295 /* XBZRLE encoding (if there is no overflow) */
296 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
297 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
298 TARGET_PAGE_SIZE);
299 if (encoded_len == 0) {
300 DPRINTF("Skipping unmodified page\n");
301 return 0;
302 } else if (encoded_len == -1) {
303 DPRINTF("Overflow\n");
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300304 acct_info.xbzrle_overflows++;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300305 /* update data in the cache */
306 memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
307 return -1;
308 }
309
310 /* we need to update the data in the cache, in order to get the same data */
Juan Quinteladd051c72012-08-06 21:42:58 +0300311 if (!last_stage) {
312 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
313 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300314
315 /* Send XBZRLE based compressed page */
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200316 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300317 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
318 qemu_put_be16(f, encoded_len);
319 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200320 bytes_sent += encoded_len + 1 + 2;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300321 acct_info.xbzrle_pages++;
322 acct_info.xbzrle_bytes += bytes_sent;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300323
324 return bytes_sent;
325}
326
Juan Quintelab23a9a52012-10-17 20:08:04 +0200327
328/* This is the last block that we have visited serching for dirty pages
329 */
330static RAMBlock *last_seen_block;
Juan Quintela5f718a12012-10-17 20:10:55 +0200331/* This is the last block from where we have sent data */
332static RAMBlock *last_sent_block;
Alex Williamson760e77e2010-08-19 10:18:42 -0300333static ram_addr_t last_offset;
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200334static unsigned long *migration_bitmap;
335static uint64_t migration_dirty_pages;
Umesh Deshpandef798b072011-08-18 11:41:17 -0700336static uint32_t last_version;
Peter Lieven78d07ae2013-03-26 10:58:36 +0100337static bool ram_bulk_stage;
Alex Williamson760e77e2010-08-19 10:18:42 -0300338
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200339static inline
340ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
341 ram_addr_t start)
Juan Quintela69268cd2012-07-20 10:36:12 +0200342{
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200343 unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS;
344 unsigned long nr = base + (start >> TARGET_PAGE_BITS);
Michael S. Tsirkin0851c9f2013-08-19 17:26:52 +0300345 uint64_t mr_size = TARGET_PAGE_ALIGN(memory_region_size(mr));
346 unsigned long size = base + (mr_size >> TARGET_PAGE_BITS);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200347
Peter Lieven70c86522013-03-26 10:58:38 +0100348 unsigned long next;
349
350 if (ram_bulk_stage && nr > base) {
351 next = nr + 1;
352 } else {
353 next = find_next_bit(migration_bitmap, size, nr);
354 }
Juan Quintela69268cd2012-07-20 10:36:12 +0200355
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200356 if (next < size) {
357 clear_bit(next, migration_bitmap);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200358 migration_dirty_pages--;
Juan Quintela69268cd2012-07-20 10:36:12 +0200359 }
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200360 return (next - base) << TARGET_PAGE_BITS;
Juan Quintela69268cd2012-07-20 10:36:12 +0200361}
362
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200363static inline bool migration_bitmap_set_dirty(MemoryRegion *mr,
364 ram_addr_t offset)
Juan Quintelae44d26c2012-07-20 10:16:08 +0200365{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200366 bool ret;
367 int nr = (mr->ram_addr + offset) >> TARGET_PAGE_BITS;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200368
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200369 ret = test_and_set_bit(nr, migration_bitmap);
370
371 if (!ret) {
372 migration_dirty_pages++;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200373 }
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200374 return ret;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200375}
376
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100377/* Needs iothread lock! */
378
Juan Quinteladd2df732012-07-20 10:52:51 +0200379static void migration_bitmap_sync(void)
380{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200381 RAMBlock *block;
382 ram_addr_t addr;
383 uint64_t num_dirty_pages_init = migration_dirty_pages;
Juan Quintela8d017192012-08-13 12:31:25 +0200384 MigrationState *s = migrate_get_current();
385 static int64_t start_time;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600386 static int64_t bytes_xfer_prev;
Juan Quintela8d017192012-08-13 12:31:25 +0200387 static int64_t num_dirty_pages_period;
388 int64_t end_time;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600389 int64_t bytes_xfer_now;
390
391 if (!bytes_xfer_prev) {
392 bytes_xfer_prev = ram_bytes_transferred();
393 }
Juan Quintela8d017192012-08-13 12:31:25 +0200394
395 if (!start_time) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100396 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela8d017192012-08-13 12:31:25 +0200397 }
Juan Quintela3c121932012-09-04 13:08:57 +0200398
399 trace_migration_bitmap_sync_start();
Paolo Bonzini1d671362013-04-24 10:46:55 +0200400 address_space_sync_dirty_bitmap(&address_space_memory);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200401
Paolo Bonzinia3161032012-11-14 15:54:48 +0100402 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200403 for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
Juan Quintela12291ec2013-10-14 17:14:47 +0200404 if (cpu_physical_memory_get_dirty(block->mr->ram_addr + addr,
405 TARGET_PAGE_SIZE,
406 DIRTY_MEMORY_MIGRATION)) {
407 cpu_physical_memory_reset_dirty(block->mr->ram_addr + addr,
408 TARGET_PAGE_SIZE,
409 DIRTY_MEMORY_MIGRATION);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200410 migration_bitmap_set_dirty(block->mr, addr);
411 }
412 }
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200413 }
414 trace_migration_bitmap_sync_end(migration_dirty_pages
Juan Quintela3c121932012-09-04 13:08:57 +0200415 - num_dirty_pages_init);
Juan Quintela8d017192012-08-13 12:31:25 +0200416 num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
Alex Blighbc72ad62013-08-21 16:03:08 +0100417 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela8d017192012-08-13 12:31:25 +0200418
419 /* more than 1 second = 1000 millisecons */
420 if (end_time > start_time + 1000) {
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600421 if (migrate_auto_converge()) {
422 /* The following detection logic can be refined later. For now:
423 Check to see if the dirtied bytes is 50% more than the approx.
424 amount of bytes that just got transferred since the last time we
425 were in this routine. If that happens >N times (for now N==4)
426 we turn on the throttle down logic */
427 bytes_xfer_now = ram_bytes_transferred();
428 if (s->dirty_pages_rate &&
429 (num_dirty_pages_period * TARGET_PAGE_SIZE >
430 (bytes_xfer_now - bytes_xfer_prev)/2) &&
431 (dirty_rate_high_cnt++ > 4)) {
432 trace_migration_throttle();
433 mig_throttle_on = true;
434 dirty_rate_high_cnt = 0;
435 }
436 bytes_xfer_prev = bytes_xfer_now;
437 } else {
438 mig_throttle_on = false;
439 }
Juan Quintela8d017192012-08-13 12:31:25 +0200440 s->dirty_pages_rate = num_dirty_pages_period * 1000
441 / (end_time - start_time);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100442 s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE;
Juan Quintela8d017192012-08-13 12:31:25 +0200443 start_time = end_time;
444 num_dirty_pages_period = 0;
445 }
Juan Quinteladd2df732012-07-20 10:52:51 +0200446}
447
Orit Wasserman6c779f22012-07-10 12:37:13 +0300448/*
449 * ram_save_block: Writes a page of memory to the stream f
450 *
Juan Quintelab823cea2012-12-10 13:27:50 +0100451 * Returns: The number of bytes written.
452 * 0 means no dirty pages
Orit Wasserman6c779f22012-07-10 12:37:13 +0300453 */
454
Juan Quinteladd051c72012-08-06 21:42:58 +0300455static int ram_save_block(QEMUFile *f, bool last_stage)
Blue Swirlad960902010-03-29 19:23:52 +0000456{
Juan Quintelab23a9a52012-10-17 20:08:04 +0200457 RAMBlock *block = last_seen_block;
Alex Williamsone44359c2010-06-25 11:09:57 -0600458 ram_addr_t offset = last_offset;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200459 bool complete_round = false;
Juan Quintelab823cea2012-12-10 13:27:50 +0100460 int bytes_sent = 0;
Avi Kivity71c510e2011-12-21 13:11:22 +0200461 MemoryRegion *mr;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300462 ram_addr_t current_addr;
Blue Swirlad960902010-03-29 19:23:52 +0000463
Alex Williamsone44359c2010-06-25 11:09:57 -0600464 if (!block)
Paolo Bonzinia3161032012-11-14 15:54:48 +0100465 block = QTAILQ_FIRST(&ram_list.blocks);
Alex Williamsone44359c2010-06-25 11:09:57 -0600466
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200467 while (true) {
Avi Kivity71c510e2011-12-21 13:11:22 +0200468 mr = block->mr;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200469 offset = migration_bitmap_find_and_reset_dirty(mr, offset);
470 if (complete_round && block == last_seen_block &&
471 offset >= last_offset) {
472 break;
473 }
474 if (offset >= block->length) {
475 offset = 0;
476 block = QTAILQ_NEXT(block, next);
477 if (!block) {
478 block = QTAILQ_FIRST(&ram_list.blocks);
479 complete_round = true;
Peter Lieven78d07ae2013-03-26 10:58:36 +0100480 ram_bulk_stage = false;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200481 }
482 } else {
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400483 int ret;
Blue Swirlad960902010-03-29 19:23:52 +0000484 uint8_t *p;
Juan Quintela5f718a12012-10-17 20:10:55 +0200485 int cont = (block == last_sent_block) ?
Juan Quintelab23a9a52012-10-17 20:08:04 +0200486 RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000487
Avi Kivity71c510e2011-12-21 13:11:22 +0200488 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000489
Juan Quintelab823cea2012-12-10 13:27:50 +0100490 /* In doubt sent page as normal */
491 bytes_sent = -1;
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400492 ret = ram_control_save_page(f, block->offset,
493 offset, TARGET_PAGE_SIZE, &bytes_sent);
494
495 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
496 if (ret != RAM_SAVE_CONTROL_DELAYED) {
497 if (bytes_sent > 0) {
498 acct_info.norm_pages++;
499 } else if (bytes_sent == 0) {
500 acct_info.dup_pages++;
501 }
502 }
Isaku Yamahatadc3c26a2013-09-21 01:23:36 +0900503 } else if (is_zero_range(p, TARGET_PAGE_SIZE)) {
Orit Wasserman004d4c12012-08-06 21:42:56 +0300504 acct_info.dup_pages++;
Peter Lieven9ef051e2013-06-10 12:14:19 +0200505 bytes_sent = save_block_hdr(f, block, offset, cont,
506 RAM_SAVE_FLAG_COMPRESS);
507 qemu_put_byte(f, 0);
508 bytes_sent++;
Peter Lieven5cc11c42013-03-26 10:58:39 +0100509 } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300510 current_addr = block->offset + offset;
511 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
Juan Quinteladd051c72012-08-06 21:42:58 +0300512 offset, cont, last_stage);
513 if (!last_stage) {
514 p = get_cached_data(XBZRLE.cache, current_addr);
515 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300516 }
517
Juan Quintelab823cea2012-12-10 13:27:50 +0100518 /* XBZRLE overflow or normal page */
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300519 if (bytes_sent == -1) {
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200520 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Orit Wasserman500f0062013-03-22 16:48:03 +0200521 qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200522 bytes_sent += TARGET_PAGE_SIZE;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300523 acct_info.norm_pages++;
Blue Swirlad960902010-03-29 19:23:52 +0000524 }
525
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300526 /* if page is unmodified, continue to the next */
Juan Quintelab823cea2012-12-10 13:27:50 +0100527 if (bytes_sent > 0) {
Juan Quintela5f718a12012-10-17 20:10:55 +0200528 last_sent_block = block;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300529 break;
530 }
Blue Swirlad960902010-03-29 19:23:52 +0000531 }
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200532 }
Juan Quintelab23a9a52012-10-17 20:08:04 +0200533 last_seen_block = block;
Alex Williamsone44359c2010-06-25 11:09:57 -0600534 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000535
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200536 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000537}
538
539static uint64_t bytes_transferred;
540
Michael R. Hines2b0ce072013-06-25 21:35:28 -0400541void acct_update_position(QEMUFile *f, size_t size, bool zero)
542{
543 uint64_t pages = size / TARGET_PAGE_SIZE;
544 if (zero) {
545 acct_info.dup_pages += pages;
546 } else {
547 acct_info.norm_pages += pages;
548 bytes_transferred += size;
549 qemu_update_position(f, size);
550 }
551}
552
Blue Swirlad960902010-03-29 19:23:52 +0000553static ram_addr_t ram_save_remaining(void)
554{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200555 return migration_dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000556}
557
558uint64_t ram_bytes_remaining(void)
559{
560 return ram_save_remaining() * TARGET_PAGE_SIZE;
561}
562
563uint64_t ram_bytes_transferred(void)
564{
565 return bytes_transferred;
566}
567
568uint64_t ram_bytes_total(void)
569{
Alex Williamsond17b5282010-06-25 11:08:38 -0600570 RAMBlock *block;
571 uint64_t total = 0;
572
Paolo Bonzinia3161032012-11-14 15:54:48 +0100573 QTAILQ_FOREACH(block, &ram_list.blocks, next)
Alex Williamsond17b5282010-06-25 11:08:38 -0600574 total += block->length;
575
576 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000577}
578
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300579static void migration_end(void)
580{
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100581 if (migration_bitmap) {
582 memory_global_dirty_log_stop();
583 g_free(migration_bitmap);
584 migration_bitmap = NULL;
585 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300586
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100587 if (XBZRLE.cache) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300588 cache_fini(XBZRLE.cache);
589 g_free(XBZRLE.cache);
590 g_free(XBZRLE.encoded_buf);
591 g_free(XBZRLE.current_buf);
592 g_free(XBZRLE.decoded_buf);
593 XBZRLE.cache = NULL;
594 }
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300595}
596
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200597static void ram_migration_cancel(void *opaque)
598{
599 migration_end();
600}
601
Juan Quintela5a170772012-07-17 17:02:24 +0200602static void reset_ram_globals(void)
603{
Juan Quintelab23a9a52012-10-17 20:08:04 +0200604 last_seen_block = NULL;
Juan Quintela5f718a12012-10-17 20:10:55 +0200605 last_sent_block = NULL;
Juan Quintela5a170772012-07-17 17:02:24 +0200606 last_offset = 0;
Umesh Deshpandef798b072011-08-18 11:41:17 -0700607 last_version = ram_list.version;
Peter Lieven78d07ae2013-03-26 10:58:36 +0100608 ram_bulk_stage = true;
Juan Quintela5a170772012-07-17 17:02:24 +0200609}
610
Juan Quintela4508bd92012-05-22 16:27:59 +0200611#define MAX_WAIT 50 /* ms, half buffered_file limit */
612
Juan Quintelad1315aa2012-06-28 15:11:57 +0200613static int ram_save_setup(QEMUFile *f, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000614{
Juan Quintelad1315aa2012-06-28 15:11:57 +0200615 RAMBlock *block;
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200616 int64_t ram_pages = last_ram_offset() >> TARGET_PAGE_BITS;
617
618 migration_bitmap = bitmap_new(ram_pages);
David Gibson7ec81e52012-12-04 11:38:38 +1100619 bitmap_set(migration_bitmap, 0, ram_pages);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200620 migration_dirty_pages = ram_pages;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600621 mig_throttle_on = false;
622 dirty_rate_high_cnt = 0;
Blue Swirlad960902010-03-29 19:23:52 +0000623
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300624 if (migrate_use_xbzrle()) {
625 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
626 TARGET_PAGE_SIZE,
627 TARGET_PAGE_SIZE);
628 if (!XBZRLE.cache) {
629 DPRINTF("Error creating cache\n");
630 return -1;
631 }
632 XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
633 XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300634 acct_clear();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300635 }
636
Paolo Bonzini9b095032013-02-22 17:36:28 +0100637 qemu_mutex_lock_iothread();
638 qemu_mutex_lock_ramlist();
639 bytes_transferred = 0;
640 reset_ram_globals();
641
Juan Quintelad1315aa2012-06-28 15:11:57 +0200642 memory_global_dirty_log_start();
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200643 migration_bitmap_sync();
Paolo Bonzini9b095032013-02-22 17:36:28 +0100644 qemu_mutex_unlock_iothread();
Juan Quintelad1315aa2012-06-28 15:11:57 +0200645
646 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
647
Paolo Bonzinia3161032012-11-14 15:54:48 +0100648 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200649 qemu_put_byte(f, strlen(block->idstr));
650 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
651 qemu_put_be64(f, block->length);
652 }
653
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700654 qemu_mutex_unlock_ramlist();
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400655
656 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
657 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
658
Juan Quintelad1315aa2012-06-28 15:11:57 +0200659 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
660
661 return 0;
662}
663
Juan Quintela16310a32012-06-28 15:31:37 +0200664static int ram_save_iterate(QEMUFile *f, void *opaque)
Juan Quintelad1315aa2012-06-28 15:11:57 +0200665{
Blue Swirlad960902010-03-29 19:23:52 +0000666 int ret;
667 int i;
Juan Quintelae4ed1542012-09-21 11:18:18 +0200668 int64_t t0;
Juan Quintelab823cea2012-12-10 13:27:50 +0100669 int total_sent = 0;
Blue Swirlad960902010-03-29 19:23:52 +0000670
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700671 qemu_mutex_lock_ramlist();
672
Umesh Deshpandef798b072011-08-18 11:41:17 -0700673 if (ram_list.version != last_version) {
674 reset_ram_globals();
675 }
676
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400677 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
678
Alex Blighbc72ad62013-08-21 16:03:08 +0100679 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Juan Quintela4508bd92012-05-22 16:27:59 +0200680 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200681 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200682 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000683
Juan Quinteladd051c72012-08-06 21:42:58 +0300684 bytes_sent = ram_save_block(f, false);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300685 /* no more blocks to sent */
Juan Quintelab823cea2012-12-10 13:27:50 +0100686 if (bytes_sent == 0) {
Blue Swirlad960902010-03-29 19:23:52 +0000687 break;
688 }
Juan Quintelab823cea2012-12-10 13:27:50 +0100689 total_sent += bytes_sent;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300690 acct_info.iterations++;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600691 check_guest_throttling();
Juan Quintela4508bd92012-05-22 16:27:59 +0200692 /* we want to check in the 1st loop, just in case it was the 1st time
693 and we had to sync the dirty bitmap.
694 qemu_get_clock_ns() is a bit expensive, so we only check each some
695 iterations
696 */
697 if ((i & 63) == 0) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100698 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
Juan Quintela4508bd92012-05-22 16:27:59 +0200699 if (t1 > MAX_WAIT) {
Igor Mitsyankoef37a692012-09-05 13:04:56 +0400700 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
Juan Quintela4508bd92012-05-22 16:27:59 +0200701 t1, i);
702 break;
703 }
704 }
705 i++;
Blue Swirlad960902010-03-29 19:23:52 +0000706 }
707
Paolo Bonzinifb3409d2012-12-20 11:25:45 +0100708 qemu_mutex_unlock_ramlist();
709
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400710 /*
711 * Must occur before EOS (or any QEMUFile operation)
712 * because of RDMA protocol.
713 */
714 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
715
Lei Li6cd0bed2013-09-04 17:02:36 +0800716 bytes_transferred += total_sent;
717
718 /*
719 * Do not count these 8 bytes into total_sent, so that we can
720 * return 0 if no page had been dirtied.
721 */
722 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
723 bytes_transferred += 8;
724
725 ret = qemu_file_get_error(f);
Juan Quintela29757252011-10-19 15:22:18 +0200726 if (ret < 0) {
727 return ret;
728 }
729
Juan Quintelab823cea2012-12-10 13:27:50 +0100730 return total_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000731}
732
Juan Quintela16310a32012-06-28 15:31:37 +0200733static int ram_save_complete(QEMUFile *f, void *opaque)
734{
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700735 qemu_mutex_lock_ramlist();
Paolo Bonzini9c339482012-12-20 11:26:04 +0100736 migration_bitmap_sync();
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700737
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400738 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
739
Juan Quintela16310a32012-06-28 15:31:37 +0200740 /* try transferring iterative blocks of memory */
741
742 /* flush all remaining blocks regardless of rate limiting */
Orit Wasserman6c779f22012-07-10 12:37:13 +0300743 while (true) {
744 int bytes_sent;
745
Juan Quinteladd051c72012-08-06 21:42:58 +0300746 bytes_sent = ram_save_block(f, true);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300747 /* no more blocks to sent */
Juan Quintelab823cea2012-12-10 13:27:50 +0100748 if (bytes_sent == 0) {
Orit Wasserman6c779f22012-07-10 12:37:13 +0300749 break;
750 }
Juan Quintela16310a32012-06-28 15:31:37 +0200751 bytes_transferred += bytes_sent;
752 }
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400753
754 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100755 migration_end();
Juan Quintela16310a32012-06-28 15:31:37 +0200756
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700757 qemu_mutex_unlock_ramlist();
Blue Swirlad960902010-03-29 19:23:52 +0000758 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
759
Alex Williamsona55bbe32010-06-25 11:10:05 -0600760 return 0;
761}
762
Juan Quintelae4ed1542012-09-21 11:18:18 +0200763static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
764{
765 uint64_t remaining_size;
766
767 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
768
769 if (remaining_size < max_size) {
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100770 qemu_mutex_lock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200771 migration_bitmap_sync();
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100772 qemu_mutex_unlock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200773 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
774 }
775 return remaining_size;
776}
777
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300778static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
779{
780 int ret, rc = 0;
781 unsigned int xh_len;
782 int xh_flags;
783
784 if (!XBZRLE.decoded_buf) {
785 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
786 }
787
788 /* extract RLE header */
789 xh_flags = qemu_get_byte(f);
790 xh_len = qemu_get_be16(f);
791
792 if (xh_flags != ENCODING_FLAG_XBZRLE) {
793 fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
794 return -1;
795 }
796
797 if (xh_len > TARGET_PAGE_SIZE) {
798 fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
799 return -1;
800 }
801 /* load data and decode */
802 qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
803
804 /* decode RLE */
805 ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
806 TARGET_PAGE_SIZE);
807 if (ret == -1) {
808 fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
809 rc = -1;
810 } else if (ret > TARGET_PAGE_SIZE) {
811 fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
812 ret, TARGET_PAGE_SIZE);
813 abort();
814 }
815
816 return rc;
817}
818
Alex Williamsona55bbe32010-06-25 11:10:05 -0600819static inline void *host_from_stream_offset(QEMUFile *f,
820 ram_addr_t offset,
821 int flags)
822{
823 static RAMBlock *block = NULL;
824 char id[256];
825 uint8_t len;
826
827 if (flags & RAM_SAVE_FLAG_CONTINUE) {
828 if (!block) {
829 fprintf(stderr, "Ack, bad migration stream!\n");
830 return NULL;
831 }
832
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200833 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600834 }
835
836 len = qemu_get_byte(f);
837 qemu_get_buffer(f, (uint8_t *)id, len);
838 id[len] = 0;
839
Paolo Bonzinia3161032012-11-14 15:54:48 +0100840 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamsona55bbe32010-06-25 11:10:05 -0600841 if (!strncmp(id, block->idstr, sizeof(id)))
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200842 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600843 }
844
845 fprintf(stderr, "Can't find block %s!\n", id);
846 return NULL;
847}
848
Michael R. Hines44c3b582013-07-22 10:01:53 -0400849/*
850 * If a page (or a whole RDMA chunk) has been
851 * determined to be zero, then zap it.
852 */
853void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
854{
Isaku Yamahatad613a562013-09-21 01:23:37 +0900855 if (ch != 0 || !is_zero_range(host, size)) {
Michael R. Hines44c3b582013-07-22 10:01:53 -0400856 memset(host, ch, size);
Michael R. Hines44c3b582013-07-22 10:01:53 -0400857 }
858}
859
Juan Quintela7908c782012-06-26 18:46:10 +0200860static int ram_load(QEMUFile *f, void *opaque, int version_id)
Blue Swirlad960902010-03-29 19:23:52 +0000861{
862 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300863 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200864 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300865 static uint64_t seq_iter;
866
867 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000868
Avi Kivityf09f2182011-12-21 13:37:56 +0200869 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000870 return -EINVAL;
871 }
872
873 do {
874 addr = qemu_get_be64(f);
875
876 flags = addr & ~TARGET_PAGE_MASK;
877 addr &= TARGET_PAGE_MASK;
878
879 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200880 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600881 /* Synchronize RAM block list */
882 char id[256];
883 ram_addr_t length;
884 ram_addr_t total_ram_bytes = addr;
885
886 while (total_ram_bytes) {
887 RAMBlock *block;
888 uint8_t len;
889
890 len = qemu_get_byte(f);
891 qemu_get_buffer(f, (uint8_t *)id, len);
892 id[len] = 0;
893 length = qemu_get_be64(f);
894
Paolo Bonzinia3161032012-11-14 15:54:48 +0100895 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600896 if (!strncmp(id, block->idstr, sizeof(id))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300897 if (block->length != length) {
Stefan Weil6bedfe92013-06-27 21:00:06 +0200898 fprintf(stderr,
899 "Length mismatch: %s: " RAM_ADDR_FMT
900 " in != " RAM_ADDR_FMT "\n", id, length,
Alon Levy87d2f822013-05-12 14:16:28 +0300901 block->length);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300902 ret = -EINVAL;
903 goto done;
904 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600905 break;
906 }
907 }
908
909 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600910 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
911 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300912 ret = -EINVAL;
913 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600914 }
915
916 total_ram_bytes -= length;
917 }
Blue Swirlad960902010-03-29 19:23:52 +0000918 }
919 }
920
921 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600922 void *host;
923 uint8_t ch;
924
Avi Kivityf09f2182011-12-21 13:37:56 +0200925 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +0200926 if (!host) {
927 return -EINVAL;
928 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600929
Alex Williamson97ab12d2010-06-25 11:09:50 -0600930 ch = qemu_get_byte(f);
Michael R. Hines44c3b582013-07-22 10:01:53 -0400931 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000932 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600933 void *host;
934
Avi Kivityf09f2182011-12-21 13:37:56 +0200935 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +0300936 if (!host) {
937 return -EINVAL;
938 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600939
Alex Williamson97ab12d2010-06-25 11:09:50 -0600940 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300941 } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300942 void *host = host_from_stream_offset(f, addr, flags);
943 if (!host) {
944 return -EINVAL;
945 }
946
947 if (load_xbzrle(f, addr, host) < 0) {
948 ret = -EINVAL;
949 goto done;
950 }
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400951 } else if (flags & RAM_SAVE_FLAG_HOOK) {
952 ram_control_load_hook(f, flags);
Blue Swirlad960902010-03-29 19:23:52 +0000953 }
Juan Quintela42802d42011-10-05 01:14:46 +0200954 error = qemu_file_get_error(f);
955 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300956 ret = error;
957 goto done;
Blue Swirlad960902010-03-29 19:23:52 +0000958 }
959 } while (!(flags & RAM_SAVE_FLAG_EOS));
960
Orit Wasserman3a697f62012-06-19 18:43:15 +0300961done:
Igor Mitsyankoef37a692012-09-05 13:04:56 +0400962 DPRINTF("Completed load of VM with exit code %d seq iteration "
963 "%" PRIu64 "\n", ret, seq_iter);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300964 return ret;
Blue Swirlad960902010-03-29 19:23:52 +0000965}
966
Juan Quintela7908c782012-06-26 18:46:10 +0200967SaveVMHandlers savevm_ram_handlers = {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200968 .save_live_setup = ram_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +0200969 .save_live_iterate = ram_save_iterate,
970 .save_live_complete = ram_save_complete,
Juan Quintelae4ed1542012-09-21 11:18:18 +0200971 .save_live_pending = ram_save_pending,
Juan Quintela7908c782012-06-26 18:46:10 +0200972 .load_state = ram_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200973 .cancel = ram_migration_cancel,
Juan Quintela7908c782012-06-26 18:46:10 +0200974};
975
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900976struct soundhw {
977 const char *name;
978 const char *descr;
979 int enabled;
980 int isa;
981 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100982 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900983 int (*init_pci) (PCIBus *bus);
984 } init;
985};
986
Paolo Bonzini36cd6f62013-04-18 18:43:58 +0200987static struct soundhw soundhw[9];
988static int soundhw_count;
Blue Swirlad960902010-03-29 19:23:52 +0000989
Paolo Bonzini36cd6f62013-04-18 18:43:58 +0200990void isa_register_soundhw(const char *name, const char *descr,
991 int (*init_isa)(ISABus *bus))
992{
993 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
994 soundhw[soundhw_count].name = name;
995 soundhw[soundhw_count].descr = descr;
996 soundhw[soundhw_count].isa = 1;
997 soundhw[soundhw_count].init.init_isa = init_isa;
998 soundhw_count++;
999}
Blue Swirlad960902010-03-29 19:23:52 +00001000
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001001void pci_register_soundhw(const char *name, const char *descr,
1002 int (*init_pci)(PCIBus *bus))
1003{
1004 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1005 soundhw[soundhw_count].name = name;
1006 soundhw[soundhw_count].descr = descr;
1007 soundhw[soundhw_count].isa = 0;
1008 soundhw[soundhw_count].init.init_pci = init_pci;
1009 soundhw_count++;
1010}
Blue Swirlad960902010-03-29 19:23:52 +00001011
1012void select_soundhw(const char *optarg)
1013{
1014 struct soundhw *c;
1015
Peter Maydellc8057f92012-08-02 13:45:54 +01001016 if (is_help_option(optarg)) {
Blue Swirlad960902010-03-29 19:23:52 +00001017 show_valid_cards:
1018
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001019 if (soundhw_count) {
1020 printf("Valid sound card names (comma separated):\n");
1021 for (c = soundhw; c->name; ++c) {
1022 printf ("%-11s %s\n", c->name, c->descr);
1023 }
1024 printf("\n-soundhw all will enable all of the above\n");
1025 } else {
1026 printf("Machine has no user-selectable audio hardware "
1027 "(it may or may not have always-present audio hardware).\n");
Blue Swirlad960902010-03-29 19:23:52 +00001028 }
Peter Maydellc8057f92012-08-02 13:45:54 +01001029 exit(!is_help_option(optarg));
Blue Swirlad960902010-03-29 19:23:52 +00001030 }
1031 else {
1032 size_t l;
1033 const char *p;
1034 char *e;
1035 int bad_card = 0;
1036
1037 if (!strcmp(optarg, "all")) {
1038 for (c = soundhw; c->name; ++c) {
1039 c->enabled = 1;
1040 }
1041 return;
1042 }
1043
1044 p = optarg;
1045 while (*p) {
1046 e = strchr(p, ',');
1047 l = !e ? strlen(p) : (size_t) (e - p);
1048
1049 for (c = soundhw; c->name; ++c) {
1050 if (!strncmp(c->name, p, l) && !c->name[l]) {
1051 c->enabled = 1;
1052 break;
1053 }
1054 }
1055
1056 if (!c->name) {
1057 if (l > 80) {
1058 fprintf(stderr,
1059 "Unknown sound card name (too big to show)\n");
1060 }
1061 else {
1062 fprintf(stderr, "Unknown sound card name `%.*s'\n",
1063 (int) l, p);
1064 }
1065 bad_card = 1;
1066 }
1067 p += l + (e != NULL);
1068 }
1069
1070 if (bad_card) {
1071 goto show_valid_cards;
1072 }
1073 }
1074}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001075
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001076void audio_init(void)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001077{
1078 struct soundhw *c;
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001079 ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
1080 PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001081
1082 for (c = soundhw; c->name; ++c) {
1083 if (c->enabled) {
1084 if (c->isa) {
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001085 if (!isa_bus) {
1086 fprintf(stderr, "ISA bus not available for %s\n", c->name);
1087 exit(1);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001088 }
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001089 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001090 } else {
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001091 if (!pci_bus) {
1092 fprintf(stderr, "PCI bus not available for %s\n", c->name);
1093 exit(1);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001094 }
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001095 c->init.init_pci(pci_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001096 }
1097 }
1098 }
1099}
Blue Swirlad960902010-03-29 19:23:52 +00001100
1101int qemu_uuid_parse(const char *str, uint8_t *uuid)
1102{
1103 int ret;
1104
1105 if (strlen(str) != 36) {
1106 return -1;
1107 }
1108
1109 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1110 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1111 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1112 &uuid[15]);
1113
1114 if (ret != 16) {
1115 return -1;
1116 }
Blue Swirlad960902010-03-29 19:23:52 +00001117 return 0;
1118}
1119
Laszlo Ersek0c764a92013-03-21 00:23:17 +01001120void do_acpitable_option(const QemuOpts *opts)
Blue Swirlad960902010-03-29 19:23:52 +00001121{
1122#ifdef TARGET_I386
Laszlo Ersek23084322013-03-21 00:23:19 +01001123 Error *err = NULL;
1124
1125 acpi_table_add(opts, &err);
1126 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04001127 error_report("Wrong acpi table provided: %s",
1128 error_get_pretty(err));
Laszlo Ersek23084322013-03-21 00:23:19 +01001129 error_free(err);
Blue Swirlad960902010-03-29 19:23:52 +00001130 exit(1);
1131 }
1132#endif
1133}
1134
Markus Armbruster4f953d22013-08-16 15:18:29 +02001135void do_smbios_option(QemuOpts *opts)
Blue Swirlad960902010-03-29 19:23:52 +00001136{
1137#ifdef TARGET_I386
Markus Armbruster4f953d22013-08-16 15:18:29 +02001138 smbios_entry_add(opts);
Blue Swirlad960902010-03-29 19:23:52 +00001139#endif
1140}
1141
1142void cpudef_init(void)
1143{
1144#if defined(cpudef_setup)
1145 cpudef_setup(); /* parse cpu definitions in target config file */
1146#endif
1147}
1148
Anthony PERARD303d4e82010-09-21 20:05:31 +01001149int tcg_available(void)
1150{
1151 return 1;
1152}
1153
Blue Swirlad960902010-03-29 19:23:52 +00001154int kvm_available(void)
1155{
1156#ifdef CONFIG_KVM
1157 return 1;
1158#else
1159 return 0;
1160#endif
1161}
1162
1163int xen_available(void)
1164{
1165#ifdef CONFIG_XEN
1166 return 1;
1167#else
1168 return 0;
1169#endif
1170}
Daniel P. Berrange99afc912012-08-20 15:31:38 +01001171
1172
1173TargetInfo *qmp_query_target(Error **errp)
1174{
1175 TargetInfo *info = g_malloc0(sizeof(*info));
1176
Paolo Bonzinic02a9552013-06-04 14:45:28 +02001177 info->arch = g_strdup(TARGET_NAME);
Daniel P. Berrange99afc912012-08-20 15:31:38 +01001178
1179 return info;
1180}
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001181
1182/* Stub function that's gets run on the vcpu when its brought out of the
1183 VM to run inside qemu via async_run_on_cpu()*/
1184static void mig_sleep_cpu(void *opq)
1185{
1186 qemu_mutex_unlock_iothread();
1187 g_usleep(30*1000);
1188 qemu_mutex_lock_iothread();
1189}
1190
1191/* To reduce the dirty rate explicitly disallow the VCPUs from spending
1192 much time in the VM. The migration thread will try to catchup.
1193 Workload will experience a performance drop.
1194*/
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001195static void mig_throttle_guest_down(void)
1196{
Andreas Färber38fcbd32013-07-07 19:50:23 +02001197 CPUState *cpu;
1198
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001199 qemu_mutex_lock_iothread();
Andreas Färber38fcbd32013-07-07 19:50:23 +02001200 CPU_FOREACH(cpu) {
1201 async_run_on_cpu(cpu, mig_sleep_cpu, NULL);
1202 }
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001203 qemu_mutex_unlock_iothread();
1204}
1205
1206static void check_guest_throttling(void)
1207{
1208 static int64_t t0;
1209 int64_t t1;
1210
1211 if (!mig_throttle_on) {
1212 return;
1213 }
1214
1215 if (!t0) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001216 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001217 return;
1218 }
1219
Alex Blighbc72ad62013-08-21 16:03:08 +01001220 t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001221
1222 /* If it has been more than 40 ms since the last time the guest
1223 * was throttled then do it again.
1224 */
1225 if (40 < (t1-t0)/1000000) {
1226 mig_throttle_guest_down();
1227 t0 = t1;
1228 }
1229}