blob: 2cd3d004603b6935ed0b6b56057b767423bcddab [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 Quintela791fa2a2013-11-05 16:47:20 +0100363static inline bool migration_bitmap_set_dirty(ram_addr_t addr)
Juan Quintelae44d26c2012-07-20 10:16:08 +0200364{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200365 bool ret;
Juan Quintela791fa2a2013-11-05 16:47:20 +0100366 int nr = addr >> TARGET_PAGE_BITS;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200367
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200368 ret = test_and_set_bit(nr, migration_bitmap);
369
370 if (!ret) {
371 migration_dirty_pages++;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200372 }
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200373 return ret;
Juan Quintelae44d26c2012-07-20 10:16:08 +0200374}
375
Juan Quintela791fa2a2013-11-05 16:47:20 +0100376static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length)
377{
378 ram_addr_t addr;
379
380 for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
381 if (cpu_physical_memory_get_dirty(start + addr,
382 TARGET_PAGE_SIZE,
383 DIRTY_MEMORY_MIGRATION)) {
384 cpu_physical_memory_reset_dirty(start + addr,
385 TARGET_PAGE_SIZE,
386 DIRTY_MEMORY_MIGRATION);
387 migration_bitmap_set_dirty(start + addr);
388 }
389 }
390}
391
392
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100393/* Needs iothread lock! */
394
Juan Quinteladd2df732012-07-20 10:52:51 +0200395static void migration_bitmap_sync(void)
396{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200397 RAMBlock *block;
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200398 uint64_t num_dirty_pages_init = migration_dirty_pages;
Juan Quintela8d017192012-08-13 12:31:25 +0200399 MigrationState *s = migrate_get_current();
400 static int64_t start_time;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600401 static int64_t bytes_xfer_prev;
Juan Quintela8d017192012-08-13 12:31:25 +0200402 static int64_t num_dirty_pages_period;
403 int64_t end_time;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600404 int64_t bytes_xfer_now;
405
406 if (!bytes_xfer_prev) {
407 bytes_xfer_prev = ram_bytes_transferred();
408 }
Juan Quintela8d017192012-08-13 12:31:25 +0200409
410 if (!start_time) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100411 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela8d017192012-08-13 12:31:25 +0200412 }
Juan Quintela3c121932012-09-04 13:08:57 +0200413
414 trace_migration_bitmap_sync_start();
Paolo Bonzini1d671362013-04-24 10:46:55 +0200415 address_space_sync_dirty_bitmap(&address_space_memory);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200416
Paolo Bonzinia3161032012-11-14 15:54:48 +0100417 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Juan Quintela791fa2a2013-11-05 16:47:20 +0100418 migration_bitmap_sync_range(block->mr->ram_addr, block->length);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200419 }
420 trace_migration_bitmap_sync_end(migration_dirty_pages
Juan Quintela3c121932012-09-04 13:08:57 +0200421 - num_dirty_pages_init);
Juan Quintela8d017192012-08-13 12:31:25 +0200422 num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
Alex Blighbc72ad62013-08-21 16:03:08 +0100423 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela8d017192012-08-13 12:31:25 +0200424
425 /* more than 1 second = 1000 millisecons */
426 if (end_time > start_time + 1000) {
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600427 if (migrate_auto_converge()) {
428 /* The following detection logic can be refined later. For now:
429 Check to see if the dirtied bytes is 50% more than the approx.
430 amount of bytes that just got transferred since the last time we
431 were in this routine. If that happens >N times (for now N==4)
432 we turn on the throttle down logic */
433 bytes_xfer_now = ram_bytes_transferred();
434 if (s->dirty_pages_rate &&
435 (num_dirty_pages_period * TARGET_PAGE_SIZE >
436 (bytes_xfer_now - bytes_xfer_prev)/2) &&
437 (dirty_rate_high_cnt++ > 4)) {
438 trace_migration_throttle();
439 mig_throttle_on = true;
440 dirty_rate_high_cnt = 0;
441 }
442 bytes_xfer_prev = bytes_xfer_now;
443 } else {
444 mig_throttle_on = false;
445 }
Juan Quintela8d017192012-08-13 12:31:25 +0200446 s->dirty_pages_rate = num_dirty_pages_period * 1000
447 / (end_time - start_time);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100448 s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE;
Juan Quintela8d017192012-08-13 12:31:25 +0200449 start_time = end_time;
450 num_dirty_pages_period = 0;
451 }
Juan Quinteladd2df732012-07-20 10:52:51 +0200452}
453
Orit Wasserman6c779f22012-07-10 12:37:13 +0300454/*
455 * ram_save_block: Writes a page of memory to the stream f
456 *
Juan Quintelab823cea2012-12-10 13:27:50 +0100457 * Returns: The number of bytes written.
458 * 0 means no dirty pages
Orit Wasserman6c779f22012-07-10 12:37:13 +0300459 */
460
Juan Quinteladd051c72012-08-06 21:42:58 +0300461static int ram_save_block(QEMUFile *f, bool last_stage)
Blue Swirlad960902010-03-29 19:23:52 +0000462{
Juan Quintelab23a9a52012-10-17 20:08:04 +0200463 RAMBlock *block = last_seen_block;
Alex Williamsone44359c2010-06-25 11:09:57 -0600464 ram_addr_t offset = last_offset;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200465 bool complete_round = false;
Juan Quintelab823cea2012-12-10 13:27:50 +0100466 int bytes_sent = 0;
Avi Kivity71c510e2011-12-21 13:11:22 +0200467 MemoryRegion *mr;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300468 ram_addr_t current_addr;
Blue Swirlad960902010-03-29 19:23:52 +0000469
Alex Williamsone44359c2010-06-25 11:09:57 -0600470 if (!block)
Paolo Bonzinia3161032012-11-14 15:54:48 +0100471 block = QTAILQ_FIRST(&ram_list.blocks);
Alex Williamsone44359c2010-06-25 11:09:57 -0600472
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200473 while (true) {
Avi Kivity71c510e2011-12-21 13:11:22 +0200474 mr = block->mr;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200475 offset = migration_bitmap_find_and_reset_dirty(mr, offset);
476 if (complete_round && block == last_seen_block &&
477 offset >= last_offset) {
478 break;
479 }
480 if (offset >= block->length) {
481 offset = 0;
482 block = QTAILQ_NEXT(block, next);
483 if (!block) {
484 block = QTAILQ_FIRST(&ram_list.blocks);
485 complete_round = true;
Peter Lieven78d07ae2013-03-26 10:58:36 +0100486 ram_bulk_stage = false;
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200487 }
488 } else {
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400489 int ret;
Blue Swirlad960902010-03-29 19:23:52 +0000490 uint8_t *p;
Juan Quintela5f718a12012-10-17 20:10:55 +0200491 int cont = (block == last_sent_block) ?
Juan Quintelab23a9a52012-10-17 20:08:04 +0200492 RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000493
Avi Kivity71c510e2011-12-21 13:11:22 +0200494 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000495
Juan Quintelab823cea2012-12-10 13:27:50 +0100496 /* In doubt sent page as normal */
497 bytes_sent = -1;
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400498 ret = ram_control_save_page(f, block->offset,
499 offset, TARGET_PAGE_SIZE, &bytes_sent);
500
501 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
502 if (ret != RAM_SAVE_CONTROL_DELAYED) {
503 if (bytes_sent > 0) {
504 acct_info.norm_pages++;
505 } else if (bytes_sent == 0) {
506 acct_info.dup_pages++;
507 }
508 }
Isaku Yamahatadc3c26a2013-09-21 01:23:36 +0900509 } else if (is_zero_range(p, TARGET_PAGE_SIZE)) {
Orit Wasserman004d4c12012-08-06 21:42:56 +0300510 acct_info.dup_pages++;
Peter Lieven9ef051e2013-06-10 12:14:19 +0200511 bytes_sent = save_block_hdr(f, block, offset, cont,
512 RAM_SAVE_FLAG_COMPRESS);
513 qemu_put_byte(f, 0);
514 bytes_sent++;
Peter Lieven5cc11c42013-03-26 10:58:39 +0100515 } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300516 current_addr = block->offset + offset;
517 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
Juan Quinteladd051c72012-08-06 21:42:58 +0300518 offset, cont, last_stage);
519 if (!last_stage) {
520 p = get_cached_data(XBZRLE.cache, current_addr);
521 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300522 }
523
Juan Quintelab823cea2012-12-10 13:27:50 +0100524 /* XBZRLE overflow or normal page */
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300525 if (bytes_sent == -1) {
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200526 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Orit Wasserman500f0062013-03-22 16:48:03 +0200527 qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
Juan Quintela3f7d7b02012-10-18 13:56:35 +0200528 bytes_sent += TARGET_PAGE_SIZE;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300529 acct_info.norm_pages++;
Blue Swirlad960902010-03-29 19:23:52 +0000530 }
531
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300532 /* if page is unmodified, continue to the next */
Juan Quintelab823cea2012-12-10 13:27:50 +0100533 if (bytes_sent > 0) {
Juan Quintela5f718a12012-10-17 20:10:55 +0200534 last_sent_block = block;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300535 break;
536 }
Blue Swirlad960902010-03-29 19:23:52 +0000537 }
Juan Quintela4c8ae0f2012-10-18 00:00:59 +0200538 }
Juan Quintelab23a9a52012-10-17 20:08:04 +0200539 last_seen_block = block;
Alex Williamsone44359c2010-06-25 11:09:57 -0600540 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000541
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200542 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000543}
544
545static uint64_t bytes_transferred;
546
Michael R. Hines2b0ce072013-06-25 21:35:28 -0400547void acct_update_position(QEMUFile *f, size_t size, bool zero)
548{
549 uint64_t pages = size / TARGET_PAGE_SIZE;
550 if (zero) {
551 acct_info.dup_pages += pages;
552 } else {
553 acct_info.norm_pages += pages;
554 bytes_transferred += size;
555 qemu_update_position(f, size);
556 }
557}
558
Blue Swirlad960902010-03-29 19:23:52 +0000559static ram_addr_t ram_save_remaining(void)
560{
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200561 return migration_dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000562}
563
564uint64_t ram_bytes_remaining(void)
565{
566 return ram_save_remaining() * TARGET_PAGE_SIZE;
567}
568
569uint64_t ram_bytes_transferred(void)
570{
571 return bytes_transferred;
572}
573
574uint64_t ram_bytes_total(void)
575{
Alex Williamsond17b5282010-06-25 11:08:38 -0600576 RAMBlock *block;
577 uint64_t total = 0;
578
Paolo Bonzinia3161032012-11-14 15:54:48 +0100579 QTAILQ_FOREACH(block, &ram_list.blocks, next)
Alex Williamsond17b5282010-06-25 11:08:38 -0600580 total += block->length;
581
582 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000583}
584
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300585static void migration_end(void)
586{
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100587 if (migration_bitmap) {
588 memory_global_dirty_log_stop();
589 g_free(migration_bitmap);
590 migration_bitmap = NULL;
591 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300592
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100593 if (XBZRLE.cache) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300594 cache_fini(XBZRLE.cache);
595 g_free(XBZRLE.cache);
596 g_free(XBZRLE.encoded_buf);
597 g_free(XBZRLE.current_buf);
598 g_free(XBZRLE.decoded_buf);
599 XBZRLE.cache = NULL;
600 }
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300601}
602
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200603static void ram_migration_cancel(void *opaque)
604{
605 migration_end();
606}
607
Juan Quintela5a170772012-07-17 17:02:24 +0200608static void reset_ram_globals(void)
609{
Juan Quintelab23a9a52012-10-17 20:08:04 +0200610 last_seen_block = NULL;
Juan Quintela5f718a12012-10-17 20:10:55 +0200611 last_sent_block = NULL;
Juan Quintela5a170772012-07-17 17:02:24 +0200612 last_offset = 0;
Umesh Deshpandef798b072011-08-18 11:41:17 -0700613 last_version = ram_list.version;
Peter Lieven78d07ae2013-03-26 10:58:36 +0100614 ram_bulk_stage = true;
Juan Quintela5a170772012-07-17 17:02:24 +0200615}
616
Juan Quintela4508bd92012-05-22 16:27:59 +0200617#define MAX_WAIT 50 /* ms, half buffered_file limit */
618
Juan Quintelad1315aa2012-06-28 15:11:57 +0200619static int ram_save_setup(QEMUFile *f, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000620{
Juan Quintelad1315aa2012-06-28 15:11:57 +0200621 RAMBlock *block;
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200622 int64_t ram_pages = last_ram_offset() >> TARGET_PAGE_BITS;
623
624 migration_bitmap = bitmap_new(ram_pages);
David Gibson7ec81e52012-12-04 11:38:38 +1100625 bitmap_set(migration_bitmap, 0, ram_pages);
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200626 migration_dirty_pages = ram_pages;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600627 mig_throttle_on = false;
628 dirty_rate_high_cnt = 0;
Blue Swirlad960902010-03-29 19:23:52 +0000629
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300630 if (migrate_use_xbzrle()) {
631 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
632 TARGET_PAGE_SIZE,
633 TARGET_PAGE_SIZE);
634 if (!XBZRLE.cache) {
635 DPRINTF("Error creating cache\n");
636 return -1;
637 }
638 XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
639 XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300640 acct_clear();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300641 }
642
Paolo Bonzini9b095032013-02-22 17:36:28 +0100643 qemu_mutex_lock_iothread();
644 qemu_mutex_lock_ramlist();
645 bytes_transferred = 0;
646 reset_ram_globals();
647
Juan Quintelad1315aa2012-06-28 15:11:57 +0200648 memory_global_dirty_log_start();
Juan Quintelac6bf8e02012-07-20 12:33:00 +0200649 migration_bitmap_sync();
Paolo Bonzini9b095032013-02-22 17:36:28 +0100650 qemu_mutex_unlock_iothread();
Juan Quintelad1315aa2012-06-28 15:11:57 +0200651
652 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
653
Paolo Bonzinia3161032012-11-14 15:54:48 +0100654 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200655 qemu_put_byte(f, strlen(block->idstr));
656 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
657 qemu_put_be64(f, block->length);
658 }
659
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700660 qemu_mutex_unlock_ramlist();
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400661
662 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
663 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
664
Juan Quintelad1315aa2012-06-28 15:11:57 +0200665 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
666
667 return 0;
668}
669
Juan Quintela16310a32012-06-28 15:31:37 +0200670static int ram_save_iterate(QEMUFile *f, void *opaque)
Juan Quintelad1315aa2012-06-28 15:11:57 +0200671{
Blue Swirlad960902010-03-29 19:23:52 +0000672 int ret;
673 int i;
Juan Quintelae4ed1542012-09-21 11:18:18 +0200674 int64_t t0;
Juan Quintelab823cea2012-12-10 13:27:50 +0100675 int total_sent = 0;
Blue Swirlad960902010-03-29 19:23:52 +0000676
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700677 qemu_mutex_lock_ramlist();
678
Umesh Deshpandef798b072011-08-18 11:41:17 -0700679 if (ram_list.version != last_version) {
680 reset_ram_globals();
681 }
682
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400683 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
684
Alex Blighbc72ad62013-08-21 16:03:08 +0100685 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Juan Quintela4508bd92012-05-22 16:27:59 +0200686 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200687 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200688 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000689
Juan Quinteladd051c72012-08-06 21:42:58 +0300690 bytes_sent = ram_save_block(f, false);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300691 /* no more blocks to sent */
Juan Quintelab823cea2012-12-10 13:27:50 +0100692 if (bytes_sent == 0) {
Blue Swirlad960902010-03-29 19:23:52 +0000693 break;
694 }
Juan Quintelab823cea2012-12-10 13:27:50 +0100695 total_sent += bytes_sent;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300696 acct_info.iterations++;
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -0600697 check_guest_throttling();
Juan Quintela4508bd92012-05-22 16:27:59 +0200698 /* we want to check in the 1st loop, just in case it was the 1st time
699 and we had to sync the dirty bitmap.
700 qemu_get_clock_ns() is a bit expensive, so we only check each some
701 iterations
702 */
703 if ((i & 63) == 0) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100704 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
Juan Quintela4508bd92012-05-22 16:27:59 +0200705 if (t1 > MAX_WAIT) {
Igor Mitsyankoef37a692012-09-05 13:04:56 +0400706 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
Juan Quintela4508bd92012-05-22 16:27:59 +0200707 t1, i);
708 break;
709 }
710 }
711 i++;
Blue Swirlad960902010-03-29 19:23:52 +0000712 }
713
Paolo Bonzinifb3409d2012-12-20 11:25:45 +0100714 qemu_mutex_unlock_ramlist();
715
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400716 /*
717 * Must occur before EOS (or any QEMUFile operation)
718 * because of RDMA protocol.
719 */
720 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
721
Lei Li6cd0bed2013-09-04 17:02:36 +0800722 bytes_transferred += total_sent;
723
724 /*
725 * Do not count these 8 bytes into total_sent, so that we can
726 * return 0 if no page had been dirtied.
727 */
728 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
729 bytes_transferred += 8;
730
731 ret = qemu_file_get_error(f);
Juan Quintela29757252011-10-19 15:22:18 +0200732 if (ret < 0) {
733 return ret;
734 }
735
Juan Quintelab823cea2012-12-10 13:27:50 +0100736 return total_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000737}
738
Juan Quintela16310a32012-06-28 15:31:37 +0200739static int ram_save_complete(QEMUFile *f, void *opaque)
740{
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700741 qemu_mutex_lock_ramlist();
Paolo Bonzini9c339482012-12-20 11:26:04 +0100742 migration_bitmap_sync();
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700743
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400744 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
745
Juan Quintela16310a32012-06-28 15:31:37 +0200746 /* try transferring iterative blocks of memory */
747
748 /* flush all remaining blocks regardless of rate limiting */
Orit Wasserman6c779f22012-07-10 12:37:13 +0300749 while (true) {
750 int bytes_sent;
751
Juan Quinteladd051c72012-08-06 21:42:58 +0300752 bytes_sent = ram_save_block(f, true);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300753 /* no more blocks to sent */
Juan Quintelab823cea2012-12-10 13:27:50 +0100754 if (bytes_sent == 0) {
Orit Wasserman6c779f22012-07-10 12:37:13 +0300755 break;
756 }
Juan Quintela16310a32012-06-28 15:31:37 +0200757 bytes_transferred += bytes_sent;
758 }
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400759
760 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
Paolo Bonzini244eaa72012-12-12 12:54:43 +0100761 migration_end();
Juan Quintela16310a32012-06-28 15:31:37 +0200762
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700763 qemu_mutex_unlock_ramlist();
Blue Swirlad960902010-03-29 19:23:52 +0000764 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
765
Alex Williamsona55bbe32010-06-25 11:10:05 -0600766 return 0;
767}
768
Juan Quintelae4ed1542012-09-21 11:18:18 +0200769static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
770{
771 uint64_t remaining_size;
772
773 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
774
775 if (remaining_size < max_size) {
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100776 qemu_mutex_lock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200777 migration_bitmap_sync();
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100778 qemu_mutex_unlock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200779 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
780 }
781 return remaining_size;
782}
783
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300784static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
785{
786 int ret, rc = 0;
787 unsigned int xh_len;
788 int xh_flags;
789
790 if (!XBZRLE.decoded_buf) {
791 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
792 }
793
794 /* extract RLE header */
795 xh_flags = qemu_get_byte(f);
796 xh_len = qemu_get_be16(f);
797
798 if (xh_flags != ENCODING_FLAG_XBZRLE) {
799 fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
800 return -1;
801 }
802
803 if (xh_len > TARGET_PAGE_SIZE) {
804 fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
805 return -1;
806 }
807 /* load data and decode */
808 qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
809
810 /* decode RLE */
811 ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
812 TARGET_PAGE_SIZE);
813 if (ret == -1) {
814 fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
815 rc = -1;
816 } else if (ret > TARGET_PAGE_SIZE) {
817 fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
818 ret, TARGET_PAGE_SIZE);
819 abort();
820 }
821
822 return rc;
823}
824
Alex Williamsona55bbe32010-06-25 11:10:05 -0600825static inline void *host_from_stream_offset(QEMUFile *f,
826 ram_addr_t offset,
827 int flags)
828{
829 static RAMBlock *block = NULL;
830 char id[256];
831 uint8_t len;
832
833 if (flags & RAM_SAVE_FLAG_CONTINUE) {
834 if (!block) {
835 fprintf(stderr, "Ack, bad migration stream!\n");
836 return NULL;
837 }
838
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200839 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600840 }
841
842 len = qemu_get_byte(f);
843 qemu_get_buffer(f, (uint8_t *)id, len);
844 id[len] = 0;
845
Paolo Bonzinia3161032012-11-14 15:54:48 +0100846 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamsona55bbe32010-06-25 11:10:05 -0600847 if (!strncmp(id, block->idstr, sizeof(id)))
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200848 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600849 }
850
851 fprintf(stderr, "Can't find block %s!\n", id);
852 return NULL;
853}
854
Michael R. Hines44c3b582013-07-22 10:01:53 -0400855/*
856 * If a page (or a whole RDMA chunk) has been
857 * determined to be zero, then zap it.
858 */
859void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
860{
Isaku Yamahatad613a562013-09-21 01:23:37 +0900861 if (ch != 0 || !is_zero_range(host, size)) {
Michael R. Hines44c3b582013-07-22 10:01:53 -0400862 memset(host, ch, size);
Michael R. Hines44c3b582013-07-22 10:01:53 -0400863 }
864}
865
Juan Quintela7908c782012-06-26 18:46:10 +0200866static int ram_load(QEMUFile *f, void *opaque, int version_id)
Blue Swirlad960902010-03-29 19:23:52 +0000867{
868 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300869 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200870 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300871 static uint64_t seq_iter;
872
873 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000874
Avi Kivityf09f2182011-12-21 13:37:56 +0200875 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000876 return -EINVAL;
877 }
878
879 do {
880 addr = qemu_get_be64(f);
881
882 flags = addr & ~TARGET_PAGE_MASK;
883 addr &= TARGET_PAGE_MASK;
884
885 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200886 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600887 /* Synchronize RAM block list */
888 char id[256];
889 ram_addr_t length;
890 ram_addr_t total_ram_bytes = addr;
891
892 while (total_ram_bytes) {
893 RAMBlock *block;
894 uint8_t len;
895
896 len = qemu_get_byte(f);
897 qemu_get_buffer(f, (uint8_t *)id, len);
898 id[len] = 0;
899 length = qemu_get_be64(f);
900
Paolo Bonzinia3161032012-11-14 15:54:48 +0100901 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600902 if (!strncmp(id, block->idstr, sizeof(id))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300903 if (block->length != length) {
Stefan Weil6bedfe92013-06-27 21:00:06 +0200904 fprintf(stderr,
905 "Length mismatch: %s: " RAM_ADDR_FMT
906 " in != " RAM_ADDR_FMT "\n", id, length,
Alon Levy87d2f822013-05-12 14:16:28 +0300907 block->length);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300908 ret = -EINVAL;
909 goto done;
910 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600911 break;
912 }
913 }
914
915 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600916 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
917 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300918 ret = -EINVAL;
919 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600920 }
921
922 total_ram_bytes -= length;
923 }
Blue Swirlad960902010-03-29 19:23:52 +0000924 }
925 }
926
927 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600928 void *host;
929 uint8_t ch;
930
Avi Kivityf09f2182011-12-21 13:37:56 +0200931 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +0200932 if (!host) {
933 return -EINVAL;
934 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600935
Alex Williamson97ab12d2010-06-25 11:09:50 -0600936 ch = qemu_get_byte(f);
Michael R. Hines44c3b582013-07-22 10:01:53 -0400937 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000938 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600939 void *host;
940
Avi Kivityf09f2182011-12-21 13:37:56 +0200941 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +0300942 if (!host) {
943 return -EINVAL;
944 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600945
Alex Williamson97ab12d2010-06-25 11:09:50 -0600946 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300947 } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300948 void *host = host_from_stream_offset(f, addr, flags);
949 if (!host) {
950 return -EINVAL;
951 }
952
953 if (load_xbzrle(f, addr, host) < 0) {
954 ret = -EINVAL;
955 goto done;
956 }
Michael R. Hines0033b8b2013-07-22 10:01:55 -0400957 } else if (flags & RAM_SAVE_FLAG_HOOK) {
958 ram_control_load_hook(f, flags);
Blue Swirlad960902010-03-29 19:23:52 +0000959 }
Juan Quintela42802d42011-10-05 01:14:46 +0200960 error = qemu_file_get_error(f);
961 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300962 ret = error;
963 goto done;
Blue Swirlad960902010-03-29 19:23:52 +0000964 }
965 } while (!(flags & RAM_SAVE_FLAG_EOS));
966
Orit Wasserman3a697f62012-06-19 18:43:15 +0300967done:
Igor Mitsyankoef37a692012-09-05 13:04:56 +0400968 DPRINTF("Completed load of VM with exit code %d seq iteration "
969 "%" PRIu64 "\n", ret, seq_iter);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300970 return ret;
Blue Swirlad960902010-03-29 19:23:52 +0000971}
972
Juan Quintela7908c782012-06-26 18:46:10 +0200973SaveVMHandlers savevm_ram_handlers = {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200974 .save_live_setup = ram_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +0200975 .save_live_iterate = ram_save_iterate,
976 .save_live_complete = ram_save_complete,
Juan Quintelae4ed1542012-09-21 11:18:18 +0200977 .save_live_pending = ram_save_pending,
Juan Quintela7908c782012-06-26 18:46:10 +0200978 .load_state = ram_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200979 .cancel = ram_migration_cancel,
Juan Quintela7908c782012-06-26 18:46:10 +0200980};
981
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900982struct soundhw {
983 const char *name;
984 const char *descr;
985 int enabled;
986 int isa;
987 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100988 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900989 int (*init_pci) (PCIBus *bus);
990 } init;
991};
992
Paolo Bonzini36cd6f62013-04-18 18:43:58 +0200993static struct soundhw soundhw[9];
994static int soundhw_count;
Blue Swirlad960902010-03-29 19:23:52 +0000995
Paolo Bonzini36cd6f62013-04-18 18:43:58 +0200996void isa_register_soundhw(const char *name, const char *descr,
997 int (*init_isa)(ISABus *bus))
998{
999 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1000 soundhw[soundhw_count].name = name;
1001 soundhw[soundhw_count].descr = descr;
1002 soundhw[soundhw_count].isa = 1;
1003 soundhw[soundhw_count].init.init_isa = init_isa;
1004 soundhw_count++;
1005}
Blue Swirlad960902010-03-29 19:23:52 +00001006
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001007void pci_register_soundhw(const char *name, const char *descr,
1008 int (*init_pci)(PCIBus *bus))
1009{
1010 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1011 soundhw[soundhw_count].name = name;
1012 soundhw[soundhw_count].descr = descr;
1013 soundhw[soundhw_count].isa = 0;
1014 soundhw[soundhw_count].init.init_pci = init_pci;
1015 soundhw_count++;
1016}
Blue Swirlad960902010-03-29 19:23:52 +00001017
1018void select_soundhw(const char *optarg)
1019{
1020 struct soundhw *c;
1021
Peter Maydellc8057f92012-08-02 13:45:54 +01001022 if (is_help_option(optarg)) {
Blue Swirlad960902010-03-29 19:23:52 +00001023 show_valid_cards:
1024
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001025 if (soundhw_count) {
1026 printf("Valid sound card names (comma separated):\n");
1027 for (c = soundhw; c->name; ++c) {
1028 printf ("%-11s %s\n", c->name, c->descr);
1029 }
1030 printf("\n-soundhw all will enable all of the above\n");
1031 } else {
1032 printf("Machine has no user-selectable audio hardware "
1033 "(it may or may not have always-present audio hardware).\n");
Blue Swirlad960902010-03-29 19:23:52 +00001034 }
Peter Maydellc8057f92012-08-02 13:45:54 +01001035 exit(!is_help_option(optarg));
Blue Swirlad960902010-03-29 19:23:52 +00001036 }
1037 else {
1038 size_t l;
1039 const char *p;
1040 char *e;
1041 int bad_card = 0;
1042
1043 if (!strcmp(optarg, "all")) {
1044 for (c = soundhw; c->name; ++c) {
1045 c->enabled = 1;
1046 }
1047 return;
1048 }
1049
1050 p = optarg;
1051 while (*p) {
1052 e = strchr(p, ',');
1053 l = !e ? strlen(p) : (size_t) (e - p);
1054
1055 for (c = soundhw; c->name; ++c) {
1056 if (!strncmp(c->name, p, l) && !c->name[l]) {
1057 c->enabled = 1;
1058 break;
1059 }
1060 }
1061
1062 if (!c->name) {
1063 if (l > 80) {
1064 fprintf(stderr,
1065 "Unknown sound card name (too big to show)\n");
1066 }
1067 else {
1068 fprintf(stderr, "Unknown sound card name `%.*s'\n",
1069 (int) l, p);
1070 }
1071 bad_card = 1;
1072 }
1073 p += l + (e != NULL);
1074 }
1075
1076 if (bad_card) {
1077 goto show_valid_cards;
1078 }
1079 }
1080}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001081
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001082void audio_init(void)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001083{
1084 struct soundhw *c;
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001085 ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
1086 PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001087
1088 for (c = soundhw; c->name; ++c) {
1089 if (c->enabled) {
1090 if (c->isa) {
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001091 if (!isa_bus) {
1092 fprintf(stderr, "ISA 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_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001096 } else {
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001097 if (!pci_bus) {
1098 fprintf(stderr, "PCI bus not available for %s\n", c->name);
1099 exit(1);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001100 }
Paolo Bonzinif81222b2013-04-18 18:44:03 +02001101 c->init.init_pci(pci_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001102 }
1103 }
1104 }
1105}
Blue Swirlad960902010-03-29 19:23:52 +00001106
1107int qemu_uuid_parse(const char *str, uint8_t *uuid)
1108{
1109 int ret;
1110
1111 if (strlen(str) != 36) {
1112 return -1;
1113 }
1114
1115 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1116 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1117 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1118 &uuid[15]);
1119
1120 if (ret != 16) {
1121 return -1;
1122 }
Blue Swirlad960902010-03-29 19:23:52 +00001123 return 0;
1124}
1125
Laszlo Ersek0c764a92013-03-21 00:23:17 +01001126void do_acpitable_option(const QemuOpts *opts)
Blue Swirlad960902010-03-29 19:23:52 +00001127{
1128#ifdef TARGET_I386
Laszlo Ersek23084322013-03-21 00:23:19 +01001129 Error *err = NULL;
1130
1131 acpi_table_add(opts, &err);
1132 if (err) {
Seiji Aguchi4a44d852013-08-05 15:40:44 -04001133 error_report("Wrong acpi table provided: %s",
1134 error_get_pretty(err));
Laszlo Ersek23084322013-03-21 00:23:19 +01001135 error_free(err);
Blue Swirlad960902010-03-29 19:23:52 +00001136 exit(1);
1137 }
1138#endif
1139}
1140
Markus Armbruster4f953d22013-08-16 15:18:29 +02001141void do_smbios_option(QemuOpts *opts)
Blue Swirlad960902010-03-29 19:23:52 +00001142{
1143#ifdef TARGET_I386
Markus Armbruster4f953d22013-08-16 15:18:29 +02001144 smbios_entry_add(opts);
Blue Swirlad960902010-03-29 19:23:52 +00001145#endif
1146}
1147
1148void cpudef_init(void)
1149{
1150#if defined(cpudef_setup)
1151 cpudef_setup(); /* parse cpu definitions in target config file */
1152#endif
1153}
1154
Anthony PERARD303d4e82010-09-21 20:05:31 +01001155int tcg_available(void)
1156{
1157 return 1;
1158}
1159
Blue Swirlad960902010-03-29 19:23:52 +00001160int kvm_available(void)
1161{
1162#ifdef CONFIG_KVM
1163 return 1;
1164#else
1165 return 0;
1166#endif
1167}
1168
1169int xen_available(void)
1170{
1171#ifdef CONFIG_XEN
1172 return 1;
1173#else
1174 return 0;
1175#endif
1176}
Daniel P. Berrange99afc912012-08-20 15:31:38 +01001177
1178
1179TargetInfo *qmp_query_target(Error **errp)
1180{
1181 TargetInfo *info = g_malloc0(sizeof(*info));
1182
Paolo Bonzinic02a9552013-06-04 14:45:28 +02001183 info->arch = g_strdup(TARGET_NAME);
Daniel P. Berrange99afc912012-08-20 15:31:38 +01001184
1185 return info;
1186}
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001187
1188/* Stub function that's gets run on the vcpu when its brought out of the
1189 VM to run inside qemu via async_run_on_cpu()*/
1190static void mig_sleep_cpu(void *opq)
1191{
1192 qemu_mutex_unlock_iothread();
1193 g_usleep(30*1000);
1194 qemu_mutex_lock_iothread();
1195}
1196
1197/* To reduce the dirty rate explicitly disallow the VCPUs from spending
1198 much time in the VM. The migration thread will try to catchup.
1199 Workload will experience a performance drop.
1200*/
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001201static void mig_throttle_guest_down(void)
1202{
Andreas Färber38fcbd32013-07-07 19:50:23 +02001203 CPUState *cpu;
1204
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001205 qemu_mutex_lock_iothread();
Andreas Färber38fcbd32013-07-07 19:50:23 +02001206 CPU_FOREACH(cpu) {
1207 async_run_on_cpu(cpu, mig_sleep_cpu, NULL);
1208 }
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001209 qemu_mutex_unlock_iothread();
1210}
1211
1212static void check_guest_throttling(void)
1213{
1214 static int64_t t0;
1215 int64_t t1;
1216
1217 if (!mig_throttle_on) {
1218 return;
1219 }
1220
1221 if (!t0) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001222 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001223 return;
1224 }
1225
Alex Blighbc72ad62013-08-21 16:03:08 +01001226 t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Chegu Vinod7ca1dfa2013-06-24 03:47:39 -06001227
1228 /* If it has been more than 40 ms since the last time the guest
1229 * was throttled then do it again.
1230 */
1231 if (40 < (t1-t0)/1000000) {
1232 mig_throttle_guest_down();
1233 t0 = t1;
1234 }
1235}