]> git.proxmox.com Git - mirror_qemu.git/blame - arch_init.c
Add MemTxAttrs to the IOTLB
[mirror_qemu.git] / arch_init.c
CommitLineData
ad96090a
BS
1/*
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>
b2e0a138 26#include <stdlib.h>
ad96090a 27#ifndef _WIN32
1c47cb16 28#include <sys/types.h>
ad96090a
BS
29#include <sys/mman.h>
30#endif
31#include "config.h"
83c9089e 32#include "monitor/monitor.h"
9c17d615 33#include "sysemu/sysemu.h"
1de7afc9
PB
34#include "qemu/bitops.h"
35#include "qemu/bitmap.h"
9c17d615 36#include "sysemu/arch_init.h"
ad96090a 37#include "audio/audio.h"
0d09e41a 38#include "hw/i386/pc.h"
a2cb15b0 39#include "hw/pci/pci.h"
0d09e41a 40#include "hw/audio/audio.h"
9c17d615 41#include "sysemu/kvm.h"
caf71f86 42#include "migration/migration.h"
0d09e41a 43#include "hw/i386/smbios.h"
022c62cb 44#include "exec/address-spaces.h"
0d09e41a 45#include "hw/audio/pcspk.h"
caf71f86 46#include "migration/page_cache.h"
1de7afc9 47#include "qemu/config-file.h"
d97326ee 48#include "qemu/error-report.h"
99afc91d 49#include "qmp-commands.h"
3c12193d 50#include "trace.h"
0d6d3c87 51#include "exec/cpu-all.h"
12291ec1 52#include "exec/ram_addr.h"
0445259b 53#include "hw/acpi/acpi.h"
aa8dc044 54#include "qemu/host-utils.h"
0dc3f44a 55#include "qemu/rcu_queue.h"
ad96090a 56
3a697f69
OW
57#ifdef DEBUG_ARCH_INIT
58#define DPRINTF(fmt, ...) \
59 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
60#else
61#define DPRINTF(fmt, ...) \
62 do { } while (0)
63#endif
64
ad96090a
BS
65#ifdef TARGET_SPARC
66int graphic_width = 1024;
67int graphic_height = 768;
68int graphic_depth = 8;
69#else
70int graphic_width = 800;
71int graphic_height = 600;
f1ff0e89 72int graphic_depth = 32;
ad96090a
BS
73#endif
74
ad96090a
BS
75
76#if defined(TARGET_ALPHA)
77#define QEMU_ARCH QEMU_ARCH_ALPHA
78#elif defined(TARGET_ARM)
79#define QEMU_ARCH QEMU_ARCH_ARM
80#elif defined(TARGET_CRIS)
81#define QEMU_ARCH QEMU_ARCH_CRIS
82#elif defined(TARGET_I386)
83#define QEMU_ARCH QEMU_ARCH_I386
84#elif defined(TARGET_M68K)
85#define QEMU_ARCH QEMU_ARCH_M68K
81ea0e13
MW
86#elif defined(TARGET_LM32)
87#define QEMU_ARCH QEMU_ARCH_LM32
ad96090a
BS
88#elif defined(TARGET_MICROBLAZE)
89#define QEMU_ARCH QEMU_ARCH_MICROBLAZE
90#elif defined(TARGET_MIPS)
91#define QEMU_ARCH QEMU_ARCH_MIPS
d15a9c23
AG
92#elif defined(TARGET_MOXIE)
93#define QEMU_ARCH QEMU_ARCH_MOXIE
e67db06e
JL
94#elif defined(TARGET_OPENRISC)
95#define QEMU_ARCH QEMU_ARCH_OPENRISC
ad96090a
BS
96#elif defined(TARGET_PPC)
97#define QEMU_ARCH QEMU_ARCH_PPC
98#elif defined(TARGET_S390X)
99#define QEMU_ARCH QEMU_ARCH_S390X
100#elif defined(TARGET_SH4)
101#define QEMU_ARCH QEMU_ARCH_SH4
102#elif defined(TARGET_SPARC)
103#define QEMU_ARCH QEMU_ARCH_SPARC
2328826b
MF
104#elif defined(TARGET_XTENSA)
105#define QEMU_ARCH QEMU_ARCH_XTENSA
4f23a1e6
GX
106#elif defined(TARGET_UNICORE32)
107#define QEMU_ARCH QEMU_ARCH_UNICORE32
48e06fe0
BK
108#elif defined(TARGET_TRICORE)
109#define QEMU_ARCH QEMU_ARCH_TRICORE
ad96090a
BS
110#endif
111
112const uint32_t arch_type = QEMU_ARCH;
7ca1dfad
CV
113static bool mig_throttle_on;
114static int dirty_rate_high_cnt;
115static void check_guest_throttling(void);
ad96090a 116
71411d35
C
117static uint64_t bitmap_sync_count;
118
ad96090a
BS
119/***********************************************************/
120/* ram save/restore */
121
d20878d2
YT
122#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
123#define RAM_SAVE_FLAG_COMPRESS 0x02
124#define RAM_SAVE_FLAG_MEM_SIZE 0x04
125#define RAM_SAVE_FLAG_PAGE 0x08
126#define RAM_SAVE_FLAG_EOS 0x10
127#define RAM_SAVE_FLAG_CONTINUE 0x20
17ad9b35 128#define RAM_SAVE_FLAG_XBZRLE 0x40
0033b8b4 129/* 0x80 is reserved in migration.h start with 0x100 next */
ad96090a 130
756557de
EH
131static struct defconfig_file {
132 const char *filename;
f29a5614
EH
133 /* Indicates it is an user config file (disabled by -no-user-config) */
134 bool userconfig;
756557de 135} default_config_files[] = {
f29a5614 136 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
2e59915d 137 { CONFIG_QEMU_CONFDIR "/target-" TARGET_NAME ".conf", true },
756557de
EH
138 { NULL }, /* end of list */
139};
140
6d3cb1f9 141static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE];
756557de 142
f29a5614 143int qemu_read_default_config_files(bool userconfig)
b5a8fe5e
EH
144{
145 int ret;
756557de 146 struct defconfig_file *f;
b5a8fe5e 147
756557de 148 for (f = default_config_files; f->filename; f++) {
f29a5614
EH
149 if (!userconfig && f->userconfig) {
150 continue;
151 }
756557de
EH
152 ret = qemu_read_config_file(f->filename);
153 if (ret < 0 && ret != -ENOENT) {
154 return ret;
155 }
b5a8fe5e 156 }
4d8b3c63 157
b5a8fe5e
EH
158 return 0;
159}
160
dc3c26a4 161static inline bool is_zero_range(uint8_t *p, uint64_t size)
ad96090a 162{
dc3c26a4 163 return buffer_find_nonzero_offset(p, size) == size;
ad96090a
BS
164}
165
17ad9b35
OW
166/* struct contains XBZRLE cache and a static page
167 used by the compression */
168static struct {
169 /* buffer used for XBZRLE encoding */
170 uint8_t *encoded_buf;
171 /* buffer for storing page content */
172 uint8_t *current_buf;
fd8cec93 173 /* Cache for XBZRLE, Protected by lock. */
17ad9b35 174 PageCache *cache;
fd8cec93 175 QemuMutex lock;
d97326ee
DDAG
176} XBZRLE;
177
905f26f2
GA
178/* buffer used for XBZRLE decoding */
179static uint8_t *xbzrle_decoded_buf;
9e1ba4cc 180
fd8cec93
GA
181static void XBZRLE_cache_lock(void)
182{
183 if (migrate_use_xbzrle())
184 qemu_mutex_lock(&XBZRLE.lock);
185}
186
187static void XBZRLE_cache_unlock(void)
188{
189 if (migrate_use_xbzrle())
190 qemu_mutex_unlock(&XBZRLE.lock);
191}
192
d97326ee
DDAG
193/*
194 * called from qmp_migrate_set_cache_size in main thread, possibly while
195 * a migration is in progress.
196 * A running migration maybe using the cache and might finish during this
197 * call, hence changes to the cache are protected by XBZRLE.lock().
198 */
9e1ba4cc
OW
199int64_t xbzrle_cache_resize(int64_t new_size)
200{
d97326ee
DDAG
201 PageCache *new_cache;
202 int64_t ret;
fd8cec93 203
c91e681a
OW
204 if (new_size < TARGET_PAGE_SIZE) {
205 return -1;
206 }
207
d97326ee
DDAG
208 XBZRLE_cache_lock();
209
9e1ba4cc 210 if (XBZRLE.cache != NULL) {
fd8cec93 211 if (pow2floor(new_size) == migrate_xbzrle_cache_size()) {
d97326ee 212 goto out_new_size;
fd8cec93
GA
213 }
214 new_cache = cache_init(new_size / TARGET_PAGE_SIZE,
215 TARGET_PAGE_SIZE);
216 if (!new_cache) {
d97326ee
DDAG
217 error_report("Error creating cache");
218 ret = -1;
219 goto out;
fd8cec93 220 }
fd8cec93 221
d97326ee
DDAG
222 cache_fini(XBZRLE.cache);
223 XBZRLE.cache = new_cache;
9e1ba4cc 224 }
fd8cec93 225
d97326ee
DDAG
226out_new_size:
227 ret = pow2floor(new_size);
228out:
229 XBZRLE_cache_unlock();
230 return ret;
9e1ba4cc
OW
231}
232
004d4c10
OW
233/* accounting for migration statistics */
234typedef struct AccountingInfo {
235 uint64_t dup_pages;
f1c72795 236 uint64_t skipped_pages;
004d4c10
OW
237 uint64_t norm_pages;
238 uint64_t iterations;
f36d55af
OW
239 uint64_t xbzrle_bytes;
240 uint64_t xbzrle_pages;
241 uint64_t xbzrle_cache_miss;
8bc39233 242 double xbzrle_cache_miss_rate;
f36d55af 243 uint64_t xbzrle_overflows;
004d4c10
OW
244} AccountingInfo;
245
246static AccountingInfo acct_info;
247
248static void acct_clear(void)
249{
250 memset(&acct_info, 0, sizeof(acct_info));
251}
252
253uint64_t dup_mig_bytes_transferred(void)
254{
255 return acct_info.dup_pages * TARGET_PAGE_SIZE;
256}
257
258uint64_t dup_mig_pages_transferred(void)
259{
260 return acct_info.dup_pages;
261}
262
f1c72795
PL
263uint64_t skipped_mig_bytes_transferred(void)
264{
265 return acct_info.skipped_pages * TARGET_PAGE_SIZE;
266}
267
268uint64_t skipped_mig_pages_transferred(void)
269{
270 return acct_info.skipped_pages;
271}
272
004d4c10
OW
273uint64_t norm_mig_bytes_transferred(void)
274{
275 return acct_info.norm_pages * TARGET_PAGE_SIZE;
276}
277
278uint64_t norm_mig_pages_transferred(void)
279{
280 return acct_info.norm_pages;
281}
282
f36d55af
OW
283uint64_t xbzrle_mig_bytes_transferred(void)
284{
285 return acct_info.xbzrle_bytes;
286}
287
288uint64_t xbzrle_mig_pages_transferred(void)
289{
290 return acct_info.xbzrle_pages;
291}
292
293uint64_t xbzrle_mig_pages_cache_miss(void)
294{
295 return acct_info.xbzrle_cache_miss;
296}
297
8bc39233
C
298double xbzrle_mig_cache_miss_rate(void)
299{
300 return acct_info.xbzrle_cache_miss_rate;
301}
302
f36d55af
OW
303uint64_t xbzrle_mig_pages_overflow(void)
304{
305 return acct_info.xbzrle_overflows;
306}
307
73bab2fc
JQ
308/* This is the last block that we have visited serching for dirty pages
309 */
310static RAMBlock *last_seen_block;
311/* This is the last block from where we have sent data */
312static RAMBlock *last_sent_block;
313static ram_addr_t last_offset;
314static unsigned long *migration_bitmap;
315static uint64_t migration_dirty_pages;
316static uint32_t last_version;
317static bool ram_bulk_stage;
318
f6f14c58
JQ
319/**
320 * save_page_header: Write page header to wire
321 *
322 * If this is the 1st block, it also writes the block identification
323 *
324 * Returns: Number of bytes written
325 *
326 * @f: QEMUFile where to send the data
327 * @block: block that contains the page we want to send
328 * @offset: offset inside the block for the page
329 * in the lower bits, it contains flags
330 */
331static size_t save_page_header(QEMUFile *f, RAMBlock *block, ram_addr_t offset)
0c51f43d 332{
3f7d7b09
JQ
333 size_t size;
334
f6f14c58 335 qemu_put_be64(f, offset);
3f7d7b09 336 size = 8;
0c51f43d 337
43edc0ed 338 if (!(offset & RAM_SAVE_FLAG_CONTINUE)) {
3f7d7b09
JQ
339 qemu_put_byte(f, strlen(block->idstr));
340 qemu_put_buffer(f, (uint8_t *)block->idstr,
341 strlen(block->idstr));
342 size += 1 + strlen(block->idstr);
343 }
344 return size;
0c51f43d
OW
345}
346
6d3cb1f9
DDAG
347/* Update the xbzrle cache to reflect a page that's been sent as all 0.
348 * The important thing is that a stale (not-yet-0'd) page be replaced
349 * by the new data.
350 * As a bonus, if the page wasn't in the cache it gets added so that
351 * when a small write is made into the 0'd page it gets XBZRLE sent
352 */
353static void xbzrle_cache_zero_page(ram_addr_t current_addr)
354{
355 if (ram_bulk_stage || !migrate_use_xbzrle()) {
356 return;
357 }
358
359 /* We don't care if this fails to allocate a new cache page
360 * as long as it updated an old one */
27af7d6e
C
361 cache_insert(XBZRLE.cache, current_addr, ZERO_TARGET_PAGE,
362 bitmap_sync_count);
6d3cb1f9
DDAG
363}
364
17ad9b35
OW
365#define ENCODING_FLAG_XBZRLE 0x1
366
f4be0f75
JQ
367/**
368 * save_xbzrle_page: compress and send current page
369 *
370 * Returns: 1 means that we wrote the page
371 * 0 means that page is identical to the one already sent
372 * -1 means that xbzrle would be longer than normal
373 *
374 * @f: QEMUFile where to send the data
375 * @current_data:
376 * @current_addr:
377 * @block: block that contains the page we want to send
378 * @offset: offset inside the block for the page
379 * @last_stage: if we are at the completion stage
380 * @bytes_transferred: increase it with the number of transferred bytes
381 */
1534ee93 382static int save_xbzrle_page(QEMUFile *f, uint8_t **current_data,
17ad9b35 383 ram_addr_t current_addr, RAMBlock *block,
73bab2fc 384 ram_addr_t offset, bool last_stage,
f4be0f75 385 uint64_t *bytes_transferred)
17ad9b35 386{
f4be0f75 387 int encoded_len = 0, bytes_xbzrle;
17ad9b35
OW
388 uint8_t *prev_cached_page;
389
27af7d6e 390 if (!cache_is_cached(XBZRLE.cache, current_addr, bitmap_sync_count)) {
1534ee93 391 acct_info.xbzrle_cache_miss++;
dd051c72 392 if (!last_stage) {
27af7d6e
C
393 if (cache_insert(XBZRLE.cache, current_addr, *current_data,
394 bitmap_sync_count) == -1) {
89db9987 395 return -1;
1534ee93
C
396 } else {
397 /* update *current_data when the page has been
398 inserted into cache */
399 *current_data = get_cached_data(XBZRLE.cache, current_addr);
89db9987 400 }
dd051c72 401 }
17ad9b35
OW
402 return -1;
403 }
404
405 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
406
407 /* save current buffer into memory */
1534ee93 408 memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE);
17ad9b35
OW
409
410 /* XBZRLE encoding (if there is no overflow) */
411 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
412 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
413 TARGET_PAGE_SIZE);
414 if (encoded_len == 0) {
415 DPRINTF("Skipping unmodified page\n");
416 return 0;
417 } else if (encoded_len == -1) {
418 DPRINTF("Overflow\n");
f36d55af 419 acct_info.xbzrle_overflows++;
17ad9b35 420 /* update data in the cache */
1534ee93
C
421 if (!last_stage) {
422 memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE);
423 *current_data = prev_cached_page;
424 }
17ad9b35
OW
425 return -1;
426 }
427
428 /* we need to update the data in the cache, in order to get the same data */
dd051c72
JQ
429 if (!last_stage) {
430 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
431 }
17ad9b35
OW
432
433 /* Send XBZRLE based compressed page */
f6f14c58 434 bytes_xbzrle = save_page_header(f, block, offset | RAM_SAVE_FLAG_XBZRLE);
17ad9b35
OW
435 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
436 qemu_put_be16(f, encoded_len);
437 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
f4be0f75 438 bytes_xbzrle += encoded_len + 1 + 2;
f36d55af 439 acct_info.xbzrle_pages++;
f4be0f75
JQ
440 acct_info.xbzrle_bytes += bytes_xbzrle;
441 *bytes_transferred += bytes_xbzrle;
17ad9b35 442
f4be0f75 443 return 1;
17ad9b35
OW
444}
445
4c8ae0f6
JQ
446static inline
447ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
448 ram_addr_t start)
69268cde 449{
4c8ae0f6
JQ
450 unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS;
451 unsigned long nr = base + (start >> TARGET_PAGE_BITS);
0851c9f7
MT
452 uint64_t mr_size = TARGET_PAGE_ALIGN(memory_region_size(mr));
453 unsigned long size = base + (mr_size >> TARGET_PAGE_BITS);
c6bf8e0e 454
70c8652b
PL
455 unsigned long next;
456
457 if (ram_bulk_stage && nr > base) {
458 next = nr + 1;
459 } else {
460 next = find_next_bit(migration_bitmap, size, nr);
461 }
69268cde 462
4c8ae0f6
JQ
463 if (next < size) {
464 clear_bit(next, migration_bitmap);
c6bf8e0e 465 migration_dirty_pages--;
69268cde 466 }
4c8ae0f6 467 return (next - base) << TARGET_PAGE_BITS;
69268cde
JQ
468}
469
791fa2a2 470static inline bool migration_bitmap_set_dirty(ram_addr_t addr)
e44d26c8 471{
c6bf8e0e 472 bool ret;
791fa2a2 473 int nr = addr >> TARGET_PAGE_BITS;
e44d26c8 474
c6bf8e0e
JQ
475 ret = test_and_set_bit(nr, migration_bitmap);
476
477 if (!ret) {
478 migration_dirty_pages++;
e44d26c8 479 }
c6bf8e0e 480 return ret;
e44d26c8
JQ
481}
482
791fa2a2
JQ
483static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length)
484{
485 ram_addr_t addr;
aa8dc044
JQ
486 unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
487
488 /* start address is aligned at the start of a word? */
489 if (((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) {
490 int k;
491 int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS);
492 unsigned long *src = ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION];
493
494 for (k = page; k < page + nr; k++) {
495 if (src[k]) {
496 unsigned long new_dirty;
497 new_dirty = ~migration_bitmap[k];
498 migration_bitmap[k] |= src[k];
499 new_dirty &= src[k];
500 migration_dirty_pages += ctpopl(new_dirty);
501 src[k] = 0;
502 }
503 }
504 } else {
505 for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
506 if (cpu_physical_memory_get_dirty(start + addr,
507 TARGET_PAGE_SIZE,
508 DIRTY_MEMORY_MIGRATION)) {
509 cpu_physical_memory_reset_dirty(start + addr,
510 TARGET_PAGE_SIZE,
511 DIRTY_MEMORY_MIGRATION);
512 migration_bitmap_set_dirty(start + addr);
513 }
791fa2a2
JQ
514 }
515 }
516}
517
518
6c1b663c
C
519/* Fix me: there are too many global variables used in migration process. */
520static int64_t start_time;
521static int64_t bytes_xfer_prev;
522static int64_t num_dirty_pages_period;
523
524static void migration_bitmap_sync_init(void)
525{
526 start_time = 0;
527 bytes_xfer_prev = 0;
528 num_dirty_pages_period = 0;
529}
32c835ba 530
ae3a7047 531/* Called with iothread lock held, to protect ram_list.dirty_memory[] */
dd2df737
JQ
532static void migration_bitmap_sync(void)
533{
c6bf8e0e 534 RAMBlock *block;
c6bf8e0e 535 uint64_t num_dirty_pages_init = migration_dirty_pages;
8d017193 536 MigrationState *s = migrate_get_current();
8d017193 537 int64_t end_time;
7ca1dfad 538 int64_t bytes_xfer_now;
8bc39233
C
539 static uint64_t xbzrle_cache_miss_prev;
540 static uint64_t iterations_prev;
7ca1dfad 541
71411d35
C
542 bitmap_sync_count++;
543
7ca1dfad
CV
544 if (!bytes_xfer_prev) {
545 bytes_xfer_prev = ram_bytes_transferred();
546 }
8d017193
JQ
547
548 if (!start_time) {
bc72ad67 549 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
8d017193 550 }
3c12193d
JQ
551
552 trace_migration_bitmap_sync_start();
1d671369 553 address_space_sync_dirty_bitmap(&address_space_memory);
c6bf8e0e 554
0dc3f44a
MD
555 rcu_read_lock();
556 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
9b8424d5 557 migration_bitmap_sync_range(block->mr->ram_addr, block->used_length);
c6bf8e0e 558 }
0dc3f44a
MD
559 rcu_read_unlock();
560
c6bf8e0e 561 trace_migration_bitmap_sync_end(migration_dirty_pages
3c12193d 562 - num_dirty_pages_init);
8d017193 563 num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
bc72ad67 564 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
8d017193
JQ
565
566 /* more than 1 second = 1000 millisecons */
567 if (end_time > start_time + 1000) {
7ca1dfad
CV
568 if (migrate_auto_converge()) {
569 /* The following detection logic can be refined later. For now:
570 Check to see if the dirtied bytes is 50% more than the approx.
571 amount of bytes that just got transferred since the last time we
572 were in this routine. If that happens >N times (for now N==4)
573 we turn on the throttle down logic */
574 bytes_xfer_now = ram_bytes_transferred();
575 if (s->dirty_pages_rate &&
576 (num_dirty_pages_period * TARGET_PAGE_SIZE >
577 (bytes_xfer_now - bytes_xfer_prev)/2) &&
578 (dirty_rate_high_cnt++ > 4)) {
579 trace_migration_throttle();
580 mig_throttle_on = true;
581 dirty_rate_high_cnt = 0;
582 }
583 bytes_xfer_prev = bytes_xfer_now;
584 } else {
585 mig_throttle_on = false;
586 }
8bc39233
C
587 if (migrate_use_xbzrle()) {
588 if (iterations_prev != 0) {
589 acct_info.xbzrle_cache_miss_rate =
590 (double)(acct_info.xbzrle_cache_miss -
591 xbzrle_cache_miss_prev) /
592 (acct_info.iterations - iterations_prev);
593 }
594 iterations_prev = acct_info.iterations;
595 xbzrle_cache_miss_prev = acct_info.xbzrle_cache_miss;
596 }
8d017193
JQ
597 s->dirty_pages_rate = num_dirty_pages_period * 1000
598 / (end_time - start_time);
90f8ae72 599 s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE;
8d017193
JQ
600 start_time = end_time;
601 num_dirty_pages_period = 0;
58570ed8 602 s->dirty_sync_count = bitmap_sync_count;
8d017193 603 }
dd2df737
JQ
604}
605
87cf878b 606/**
14bcfdc7
DDAG
607 * ram_save_page: Send the given page to the stream
608 *
87cf878b
JQ
609 * Returns: Number of pages written.
610 *
611 * @f: QEMUFile where to send the data
612 * @block: block that contains the page we want to send
613 * @offset: offset inside the block for the page
614 * @last_stage: if we are at the completion stage
615 * @bytes_transferred: increase it with the number of transferred bytes
14bcfdc7
DDAG
616 */
617static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset,
87cf878b 618 bool last_stage, uint64_t *bytes_transferred)
14bcfdc7 619{
87cf878b 620 int pages = -1;
6e1dea46 621 uint64_t bytes_xmit;
14bcfdc7
DDAG
622 ram_addr_t current_addr;
623 MemoryRegion *mr = block->mr;
624 uint8_t *p;
625 int ret;
626 bool send_async = true;
627
14bcfdc7
DDAG
628 p = memory_region_get_ram_ptr(mr) + offset;
629
630 /* In doubt sent page as normal */
6e1dea46 631 bytes_xmit = 0;
14bcfdc7 632 ret = ram_control_save_page(f, block->offset,
6e1dea46
JQ
633 offset, TARGET_PAGE_SIZE, &bytes_xmit);
634 if (bytes_xmit) {
87cf878b
JQ
635 *bytes_transferred += bytes_xmit;
636 pages = 1;
6e1dea46 637 }
14bcfdc7
DDAG
638
639 XBZRLE_cache_lock();
640
641 current_addr = block->offset + offset;
43edc0ed
JQ
642
643 if (block == last_sent_block) {
644 offset |= RAM_SAVE_FLAG_CONTINUE;
645 }
14bcfdc7
DDAG
646 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
647 if (ret != RAM_SAVE_CONTROL_DELAYED) {
6e1dea46 648 if (bytes_xmit > 0) {
14bcfdc7 649 acct_info.norm_pages++;
6e1dea46 650 } else if (bytes_xmit == 0) {
14bcfdc7
DDAG
651 acct_info.dup_pages++;
652 }
653 }
654 } else if (is_zero_range(p, TARGET_PAGE_SIZE)) {
655 acct_info.dup_pages++;
f6f14c58
JQ
656 *bytes_transferred += save_page_header(f, block,
657 offset | RAM_SAVE_FLAG_COMPRESS);
14bcfdc7 658 qemu_put_byte(f, 0);
87cf878b
JQ
659 *bytes_transferred += 1;
660 pages = 1;
14bcfdc7
DDAG
661 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
662 * page would be stale
663 */
664 xbzrle_cache_zero_page(current_addr);
665 } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
f4be0f75 666 pages = save_xbzrle_page(f, &p, current_addr, block,
73bab2fc 667 offset, last_stage, bytes_transferred);
14bcfdc7
DDAG
668 if (!last_stage) {
669 /* Can't send this cached data async, since the cache page
670 * might get updated before it gets to the wire
671 */
672 send_async = false;
673 }
674 }
675
676 /* XBZRLE overflow or normal page */
87cf878b 677 if (pages == -1) {
f6f14c58
JQ
678 *bytes_transferred += save_page_header(f, block,
679 offset | RAM_SAVE_FLAG_PAGE);
14bcfdc7
DDAG
680 if (send_async) {
681 qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
682 } else {
683 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
684 }
87cf878b
JQ
685 *bytes_transferred += TARGET_PAGE_SIZE;
686 pages = 1;
14bcfdc7
DDAG
687 acct_info.norm_pages++;
688 }
689
690 XBZRLE_cache_unlock();
691
87cf878b 692 return pages;
14bcfdc7
DDAG
693}
694
0fcd8d31
JQ
695/**
696 * ram_find_and_save_block: Finds a dirty page and sends it to f
6c779f22 697 *
0dc3f44a
MD
698 * Called within an RCU critical section.
699 *
0fcd8d31 700 * Returns: The number of pages written
b823ceaa 701 * 0 means no dirty pages
0fcd8d31
JQ
702 *
703 * @f: QEMUFile where to send the data
704 * @last_stage: if we are at the completion stage
705 * @bytes_transferred: increase it with the number of transferred bytes
6c779f22
OW
706 */
707
0fcd8d31
JQ
708static int ram_find_and_save_block(QEMUFile *f, bool last_stage,
709 uint64_t *bytes_transferred)
ad96090a 710{
b23a9a5c 711 RAMBlock *block = last_seen_block;
e44359c3 712 ram_addr_t offset = last_offset;
4c8ae0f6 713 bool complete_round = false;
87cf878b 714 int pages = 0;
71c510e2 715 MemoryRegion *mr;
ad96090a 716
e44359c3 717 if (!block)
0dc3f44a 718 block = QLIST_FIRST_RCU(&ram_list.blocks);
e44359c3 719
4c8ae0f6 720 while (true) {
71c510e2 721 mr = block->mr;
4c8ae0f6
JQ
722 offset = migration_bitmap_find_and_reset_dirty(mr, offset);
723 if (complete_round && block == last_seen_block &&
724 offset >= last_offset) {
725 break;
726 }
9b8424d5 727 if (offset >= block->used_length) {
4c8ae0f6 728 offset = 0;
0dc3f44a 729 block = QLIST_NEXT_RCU(block, next);
4c8ae0f6 730 if (!block) {
0dc3f44a 731 block = QLIST_FIRST_RCU(&ram_list.blocks);
4c8ae0f6 732 complete_round = true;
78d07ae7 733 ram_bulk_stage = false;
4c8ae0f6
JQ
734 }
735 } else {
87cf878b
JQ
736 pages = ram_save_page(f, block, offset, last_stage,
737 bytes_transferred);
17ad9b35 738
17ad9b35 739 /* if page is unmodified, continue to the next */
87cf878b 740 if (pages > 0) {
43edc0ed 741 last_sent_block = block;
17ad9b35
OW
742 break;
743 }
ad96090a 744 }
4c8ae0f6 745 }
ae3a7047 746
b23a9a5c 747 last_seen_block = block;
e44359c3 748 last_offset = offset;
0fcd8d31 749
87cf878b 750 return pages;
ad96090a
BS
751}
752
753static uint64_t bytes_transferred;
754
2b0ce079
MH
755void acct_update_position(QEMUFile *f, size_t size, bool zero)
756{
757 uint64_t pages = size / TARGET_PAGE_SIZE;
758 if (zero) {
759 acct_info.dup_pages += pages;
760 } else {
761 acct_info.norm_pages += pages;
762 bytes_transferred += size;
763 qemu_update_position(f, size);
764 }
765}
766
ad96090a
BS
767static ram_addr_t ram_save_remaining(void)
768{
c6bf8e0e 769 return migration_dirty_pages;
ad96090a
BS
770}
771
772uint64_t ram_bytes_remaining(void)
773{
774 return ram_save_remaining() * TARGET_PAGE_SIZE;
775}
776
777uint64_t ram_bytes_transferred(void)
778{
779 return bytes_transferred;
780}
781
782uint64_t ram_bytes_total(void)
783{
d17b5288
AW
784 RAMBlock *block;
785 uint64_t total = 0;
786
0dc3f44a
MD
787 rcu_read_lock();
788 QLIST_FOREACH_RCU(block, &ram_list.blocks, next)
9b8424d5 789 total += block->used_length;
0dc3f44a 790 rcu_read_unlock();
d17b5288 791 return total;
ad96090a
BS
792}
793
905f26f2
GA
794void free_xbzrle_decoded_buf(void)
795{
796 g_free(xbzrle_decoded_buf);
797 xbzrle_decoded_buf = NULL;
798}
799
8e21cd32
OW
800static void migration_end(void)
801{
244eaa75
PB
802 if (migration_bitmap) {
803 memory_global_dirty_log_stop();
804 g_free(migration_bitmap);
805 migration_bitmap = NULL;
806 }
17ad9b35 807
fd8cec93 808 XBZRLE_cache_lock();
244eaa75 809 if (XBZRLE.cache) {
17ad9b35 810 cache_fini(XBZRLE.cache);
17ad9b35
OW
811 g_free(XBZRLE.encoded_buf);
812 g_free(XBZRLE.current_buf);
17ad9b35 813 XBZRLE.cache = NULL;
f6c6483b
OW
814 XBZRLE.encoded_buf = NULL;
815 XBZRLE.current_buf = NULL;
17ad9b35 816 }
fd8cec93 817 XBZRLE_cache_unlock();
8e21cd32
OW
818}
819
9b5bfab0
JQ
820static void ram_migration_cancel(void *opaque)
821{
822 migration_end();
823}
824
5a170775
JQ
825static void reset_ram_globals(void)
826{
b23a9a5c 827 last_seen_block = NULL;
5f718a15 828 last_sent_block = NULL;
5a170775 829 last_offset = 0;
f798b07f 830 last_version = ram_list.version;
78d07ae7 831 ram_bulk_stage = true;
5a170775
JQ
832}
833
4508bd9e
JQ
834#define MAX_WAIT 50 /* ms, half buffered_file limit */
835
0dc3f44a
MD
836
837/* Each of ram_save_setup, ram_save_iterate and ram_save_complete has
838 * long-running RCU critical section. When rcu-reclaims in the code
839 * start to become numerous it will be necessary to reduce the
840 * granularity of these critical sections.
841 */
842
d1315aac 843static int ram_save_setup(QEMUFile *f, void *opaque)
ad96090a 844{
d1315aac 845 RAMBlock *block;
e30d1d8c 846 int64_t ram_bitmap_pages; /* Size of bitmap in pages, including gaps */
c6bf8e0e 847
7ca1dfad
CV
848 mig_throttle_on = false;
849 dirty_rate_high_cnt = 0;
71411d35 850 bitmap_sync_count = 0;
6c1b663c 851 migration_bitmap_sync_init();
ad96090a 852
17ad9b35 853 if (migrate_use_xbzrle()) {
d97326ee 854 XBZRLE_cache_lock();
17ad9b35
OW
855 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
856 TARGET_PAGE_SIZE,
857 TARGET_PAGE_SIZE);
858 if (!XBZRLE.cache) {
d97326ee
DDAG
859 XBZRLE_cache_unlock();
860 error_report("Error creating cache");
17ad9b35
OW
861 return -1;
862 }
d97326ee 863 XBZRLE_cache_unlock();
a17b2fd3
OW
864
865 /* We prefer not to abort if there is no memory */
866 XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
867 if (!XBZRLE.encoded_buf) {
d97326ee 868 error_report("Error allocating encoded_buf");
a17b2fd3
OW
869 return -1;
870 }
871
872 XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
873 if (!XBZRLE.current_buf) {
d97326ee 874 error_report("Error allocating current_buf");
a17b2fd3
OW
875 g_free(XBZRLE.encoded_buf);
876 XBZRLE.encoded_buf = NULL;
877 return -1;
878 }
879
004d4c10 880 acct_clear();
17ad9b35
OW
881 }
882
ae3a7047 883 /* iothread lock needed for ram_list.dirty_memory[] */
9b095037
PB
884 qemu_mutex_lock_iothread();
885 qemu_mutex_lock_ramlist();
0dc3f44a 886 rcu_read_lock();
9b095037
PB
887 bytes_transferred = 0;
888 reset_ram_globals();
889
e30d1d8c
DDAG
890 ram_bitmap_pages = last_ram_offset() >> TARGET_PAGE_BITS;
891 migration_bitmap = bitmap_new(ram_bitmap_pages);
892 bitmap_set(migration_bitmap, 0, ram_bitmap_pages);
893
894 /*
895 * Count the total number of pages used by ram blocks not including any
896 * gaps due to alignment or unplugs.
897 */
f54a235f 898 migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS;
e30d1d8c 899
d1315aac 900 memory_global_dirty_log_start();
c6bf8e0e 901 migration_bitmap_sync();
0dc3f44a 902 qemu_mutex_unlock_ramlist();
9b095037 903 qemu_mutex_unlock_iothread();
ad96090a 904
d1315aac 905 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
97ab12d4 906
0dc3f44a 907 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
d1315aac
JQ
908 qemu_put_byte(f, strlen(block->idstr));
909 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
9b8424d5 910 qemu_put_be64(f, block->used_length);
ad96090a
BS
911 }
912
0dc3f44a 913 rcu_read_unlock();
0033b8b4
MH
914
915 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
916 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
917
d1315aac
JQ
918 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
919
920 return 0;
921}
922
16310a3c 923static int ram_save_iterate(QEMUFile *f, void *opaque)
d1315aac 924{
d1315aac
JQ
925 int ret;
926 int i;
e4ed1541 927 int64_t t0;
0fcd8d31 928 int pages_sent = 0;
d1315aac 929
0dc3f44a 930 rcu_read_lock();
f798b07f
UD
931 if (ram_list.version != last_version) {
932 reset_ram_globals();
933 }
934
0dc3f44a
MD
935 /* Read version before ram_list.blocks */
936 smp_rmb();
937
0033b8b4
MH
938 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
939
bc72ad67 940 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
4508bd9e 941 i = 0;
2975725f 942 while ((ret = qemu_file_rate_limit(f)) == 0) {
0fcd8d31 943 int pages;
ad96090a 944
0fcd8d31
JQ
945 pages = ram_find_and_save_block(f, false, &bytes_transferred);
946 /* no more pages to sent */
947 if (pages == 0) {
ad96090a
BS
948 break;
949 }
0fcd8d31 950 pages_sent += pages;
004d4c10 951 acct_info.iterations++;
7ca1dfad 952 check_guest_throttling();
4508bd9e
JQ
953 /* we want to check in the 1st loop, just in case it was the 1st time
954 and we had to sync the dirty bitmap.
955 qemu_get_clock_ns() is a bit expensive, so we only check each some
956 iterations
957 */
958 if ((i & 63) == 0) {
bc72ad67 959 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
4508bd9e 960 if (t1 > MAX_WAIT) {
ef37a699 961 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
4508bd9e
JQ
962 t1, i);
963 break;
964 }
965 }
966 i++;
ad96090a 967 }
0dc3f44a 968 rcu_read_unlock();
fb3409de 969
0033b8b4
MH
970 /*
971 * Must occur before EOS (or any QEMUFile operation)
972 * because of RDMA protocol.
973 */
974 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
975
6cd0beda
LL
976 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
977 bytes_transferred += 8;
978
979 ret = qemu_file_get_error(f);
2975725f
JQ
980 if (ret < 0) {
981 return ret;
982 }
983
0fcd8d31 984 return pages_sent;
16310a3c
JQ
985}
986
ae3a7047 987/* Called with iothread lock */
16310a3c
JQ
988static int ram_save_complete(QEMUFile *f, void *opaque)
989{
0dc3f44a
MD
990 rcu_read_lock();
991
9c339485 992 migration_bitmap_sync();
b2a8658e 993
0033b8b4
MH
994 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
995
ad96090a 996 /* try transferring iterative blocks of memory */
3a697f69 997
16310a3c 998 /* flush all remaining blocks regardless of rate limiting */
6c779f22 999 while (true) {
0fcd8d31 1000 int pages;
3fc250b4 1001
0fcd8d31 1002 pages = ram_find_and_save_block(f, true, &bytes_transferred);
6c779f22 1003 /* no more blocks to sent */
0fcd8d31 1004 if (pages == 0) {
6c779f22 1005 break;
ad96090a 1006 }
ad96090a 1007 }
0033b8b4
MH
1008
1009 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
244eaa75 1010 migration_end();
ad96090a 1011
0dc3f44a 1012 rcu_read_unlock();
ad96090a
BS
1013 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1014
5b3c9638 1015 return 0;
ad96090a
BS
1016}
1017
e4ed1541
JQ
1018static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
1019{
1020 uint64_t remaining_size;
1021
1022 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
1023
1024 if (remaining_size < max_size) {
32c835ba 1025 qemu_mutex_lock_iothread();
0dc3f44a 1026 rcu_read_lock();
e4ed1541 1027 migration_bitmap_sync();
0dc3f44a 1028 rcu_read_unlock();
32c835ba 1029 qemu_mutex_unlock_iothread();
e4ed1541
JQ
1030 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
1031 }
1032 return remaining_size;
1033}
1034
17ad9b35
OW
1035static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
1036{
17ad9b35
OW
1037 unsigned int xh_len;
1038 int xh_flags;
1039
905f26f2
GA
1040 if (!xbzrle_decoded_buf) {
1041 xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE);
17ad9b35
OW
1042 }
1043
1044 /* extract RLE header */
1045 xh_flags = qemu_get_byte(f);
1046 xh_len = qemu_get_be16(f);
1047
1048 if (xh_flags != ENCODING_FLAG_XBZRLE) {
0971f1be 1049 error_report("Failed to load XBZRLE page - wrong compression!");
17ad9b35
OW
1050 return -1;
1051 }
1052
1053 if (xh_len > TARGET_PAGE_SIZE) {
0971f1be 1054 error_report("Failed to load XBZRLE page - len overflow!");
17ad9b35
OW
1055 return -1;
1056 }
1057 /* load data and decode */
905f26f2 1058 qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
17ad9b35
OW
1059
1060 /* decode RLE */
fb626663
CG
1061 if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
1062 TARGET_PAGE_SIZE) == -1) {
0971f1be 1063 error_report("Failed to load XBZRLE page - decode error!");
fb626663 1064 return -1;
17ad9b35
OW
1065 }
1066
fb626663 1067 return 0;
17ad9b35
OW
1068}
1069
0dc3f44a
MD
1070/* Must be called from within a rcu critical section.
1071 * Returns a pointer from within the RCU-protected ram_list.
1072 */
a55bbe31
AW
1073static inline void *host_from_stream_offset(QEMUFile *f,
1074 ram_addr_t offset,
1075 int flags)
1076{
1077 static RAMBlock *block = NULL;
1078 char id[256];
1079 uint8_t len;
1080
1081 if (flags & RAM_SAVE_FLAG_CONTINUE) {
9b8424d5 1082 if (!block || block->max_length <= offset) {
0971f1be 1083 error_report("Ack, bad migration stream!");
a55bbe31
AW
1084 return NULL;
1085 }
1086
dc94a7ed 1087 return memory_region_get_ram_ptr(block->mr) + offset;
a55bbe31
AW
1088 }
1089
1090 len = qemu_get_byte(f);
1091 qemu_get_buffer(f, (uint8_t *)id, len);
1092 id[len] = 0;
1093
0dc3f44a 1094 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
9b8424d5
MT
1095 if (!strncmp(id, block->idstr, sizeof(id)) &&
1096 block->max_length > offset) {
dc94a7ed 1097 return memory_region_get_ram_ptr(block->mr) + offset;
0be839a2 1098 }
a55bbe31
AW
1099 }
1100
0971f1be 1101 error_report("Can't find block %s!", id);
a55bbe31
AW
1102 return NULL;
1103}
1104
44c3b58c
MH
1105/*
1106 * If a page (or a whole RDMA chunk) has been
1107 * determined to be zero, then zap it.
1108 */
1109void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
1110{
d613a56f 1111 if (ch != 0 || !is_zero_range(host, size)) {
44c3b58c 1112 memset(host, ch, size);
44c3b58c
MH
1113 }
1114}
1115
7908c78d 1116static int ram_load(QEMUFile *f, void *opaque, int version_id)
ad96090a 1117{
5b0e9dd4 1118 int flags = 0, ret = 0;
3a697f69
OW
1119 static uint64_t seq_iter;
1120
1121 seq_iter++;
ad96090a 1122
21a246a4 1123 if (version_id != 4) {
4798fe55 1124 ret = -EINVAL;
ad96090a
BS
1125 }
1126
0dc3f44a
MD
1127 /* This RCU critical section can be very long running.
1128 * When RCU reclaims in the code start to become numerous,
1129 * it will be necessary to reduce the granularity of this
1130 * critical section.
1131 */
1132 rcu_read_lock();
5b0e9dd4
PL
1133 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) {
1134 ram_addr_t addr, total_ram_bytes;
1135 void *host;
1136 uint8_t ch;
ad96090a 1137
5b0e9dd4 1138 addr = qemu_get_be64(f);
ad96090a
BS
1139 flags = addr & ~TARGET_PAGE_MASK;
1140 addr &= TARGET_PAGE_MASK;
1141
5b0e9dd4
PL
1142 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
1143 case RAM_SAVE_FLAG_MEM_SIZE:
21a246a4 1144 /* Synchronize RAM block list */
5b0e9dd4
PL
1145 total_ram_bytes = addr;
1146 while (!ret && total_ram_bytes) {
21a246a4
C
1147 RAMBlock *block;
1148 uint8_t len;
5b0e9dd4
PL
1149 char id[256];
1150 ram_addr_t length;
21a246a4
C
1151
1152 len = qemu_get_byte(f);
1153 qemu_get_buffer(f, (uint8_t *)id, len);
1154 id[len] = 0;
1155 length = qemu_get_be64(f);
1156
0dc3f44a 1157 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
21a246a4 1158 if (!strncmp(id, block->idstr, sizeof(id))) {
b0cc3f83
MT
1159 if (length != block->used_length) {
1160 Error *local_err = NULL;
1161
1162 ret = qemu_ram_resize(block->offset, length, &local_err);
1163 if (local_err) {
565f65d2 1164 error_report_err(local_err);
b0cc3f83 1165 }
97ab12d4 1166 }
21a246a4 1167 break;
97ab12d4 1168 }
21a246a4 1169 }
97ab12d4 1170
21a246a4 1171 if (!block) {
0971f1be
LT
1172 error_report("Unknown ramblock \"%s\", cannot "
1173 "accept migration", id);
21a246a4 1174 ret = -EINVAL;
db80face 1175 }
21a246a4
C
1176
1177 total_ram_bytes -= length;
ad96090a 1178 }
5b0e9dd4
PL
1179 break;
1180 case RAM_SAVE_FLAG_COMPRESS:
f09f2189 1181 host = host_from_stream_offset(f, addr, flags);
492fb99c 1182 if (!host) {
db80face 1183 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
4798fe55 1184 ret = -EINVAL;
db80face 1185 break;
492fb99c 1186 }
97ab12d4 1187 ch = qemu_get_byte(f);
44c3b58c 1188 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
5b0e9dd4
PL
1189 break;
1190 case RAM_SAVE_FLAG_PAGE:
f09f2189 1191 host = host_from_stream_offset(f, addr, flags);
0ff1f9f5 1192 if (!host) {
db80face 1193 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
4798fe55 1194 ret = -EINVAL;
db80face 1195 break;
0ff1f9f5 1196 }
97ab12d4 1197 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
5b0e9dd4
PL
1198 break;
1199 case RAM_SAVE_FLAG_XBZRLE:
1200 host = host_from_stream_offset(f, addr, flags);
17ad9b35 1201 if (!host) {
db80face 1202 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
4798fe55 1203 ret = -EINVAL;
db80face 1204 break;
17ad9b35 1205 }
17ad9b35 1206 if (load_xbzrle(f, addr, host) < 0) {
db80face
PL
1207 error_report("Failed to decompress XBZRLE page at "
1208 RAM_ADDR_FMT, addr);
17ad9b35 1209 ret = -EINVAL;
db80face 1210 break;
17ad9b35 1211 }
db80face 1212 break;
5b0e9dd4
PL
1213 case RAM_SAVE_FLAG_EOS:
1214 /* normal exit */
db80face 1215 break;
5b0e9dd4
PL
1216 default:
1217 if (flags & RAM_SAVE_FLAG_HOOK) {
1218 ram_control_load_hook(f, flags);
1219 } else {
1220 error_report("Unknown combination of migration flags: %#x",
1221 flags);
1222 ret = -EINVAL;
1223 }
1224 }
1225 if (!ret) {
1226 ret = qemu_file_get_error(f);
ad96090a 1227 }
db80face 1228 }
ad96090a 1229
0dc3f44a 1230 rcu_read_unlock();
ef37a699
IM
1231 DPRINTF("Completed load of VM with exit code %d seq iteration "
1232 "%" PRIu64 "\n", ret, seq_iter);
3a697f69 1233 return ret;
ad96090a
BS
1234}
1235
0d6ab3ab 1236static SaveVMHandlers savevm_ram_handlers = {
d1315aac 1237 .save_live_setup = ram_save_setup,
16310a3c
JQ
1238 .save_live_iterate = ram_save_iterate,
1239 .save_live_complete = ram_save_complete,
e4ed1541 1240 .save_live_pending = ram_save_pending,
7908c78d 1241 .load_state = ram_load,
9b5bfab0 1242 .cancel = ram_migration_cancel,
7908c78d
JQ
1243};
1244
0d6ab3ab
DDAG
1245void ram_mig_init(void)
1246{
d97326ee 1247 qemu_mutex_init(&XBZRLE.lock);
0d6ab3ab
DDAG
1248 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
1249}
1250
0dfa5ef9
IY
1251struct soundhw {
1252 const char *name;
1253 const char *descr;
1254 int enabled;
1255 int isa;
1256 union {
4a0f031d 1257 int (*init_isa) (ISABus *bus);
0dfa5ef9
IY
1258 int (*init_pci) (PCIBus *bus);
1259 } init;
1260};
1261
36cd6f6f
PB
1262static struct soundhw soundhw[9];
1263static int soundhw_count;
ad96090a 1264
36cd6f6f
PB
1265void isa_register_soundhw(const char *name, const char *descr,
1266 int (*init_isa)(ISABus *bus))
1267{
1268 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1269 soundhw[soundhw_count].name = name;
1270 soundhw[soundhw_count].descr = descr;
1271 soundhw[soundhw_count].isa = 1;
1272 soundhw[soundhw_count].init.init_isa = init_isa;
1273 soundhw_count++;
1274}
ad96090a 1275
36cd6f6f
PB
1276void pci_register_soundhw(const char *name, const char *descr,
1277 int (*init_pci)(PCIBus *bus))
1278{
1279 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1280 soundhw[soundhw_count].name = name;
1281 soundhw[soundhw_count].descr = descr;
1282 soundhw[soundhw_count].isa = 0;
1283 soundhw[soundhw_count].init.init_pci = init_pci;
1284 soundhw_count++;
1285}
ad96090a
BS
1286
1287void select_soundhw(const char *optarg)
1288{
1289 struct soundhw *c;
1290
c8057f95 1291 if (is_help_option(optarg)) {
ad96090a
BS
1292 show_valid_cards:
1293
36cd6f6f
PB
1294 if (soundhw_count) {
1295 printf("Valid sound card names (comma separated):\n");
1296 for (c = soundhw; c->name; ++c) {
1297 printf ("%-11s %s\n", c->name, c->descr);
1298 }
1299 printf("\n-soundhw all will enable all of the above\n");
1300 } else {
1301 printf("Machine has no user-selectable audio hardware "
1302 "(it may or may not have always-present audio hardware).\n");
ad96090a 1303 }
c8057f95 1304 exit(!is_help_option(optarg));
ad96090a
BS
1305 }
1306 else {
1307 size_t l;
1308 const char *p;
1309 char *e;
1310 int bad_card = 0;
1311
1312 if (!strcmp(optarg, "all")) {
1313 for (c = soundhw; c->name; ++c) {
1314 c->enabled = 1;
1315 }
1316 return;
1317 }
1318
1319 p = optarg;
1320 while (*p) {
1321 e = strchr(p, ',');
1322 l = !e ? strlen(p) : (size_t) (e - p);
1323
1324 for (c = soundhw; c->name; ++c) {
1325 if (!strncmp(c->name, p, l) && !c->name[l]) {
1326 c->enabled = 1;
1327 break;
1328 }
1329 }
1330
1331 if (!c->name) {
1332 if (l > 80) {
0971f1be 1333 error_report("Unknown sound card name (too big to show)");
ad96090a
BS
1334 }
1335 else {
0971f1be
LT
1336 error_report("Unknown sound card name `%.*s'",
1337 (int) l, p);
ad96090a
BS
1338 }
1339 bad_card = 1;
1340 }
1341 p += l + (e != NULL);
1342 }
1343
1344 if (bad_card) {
1345 goto show_valid_cards;
1346 }
1347 }
1348}
0dfa5ef9 1349
f81222bc 1350void audio_init(void)
0dfa5ef9
IY
1351{
1352 struct soundhw *c;
f81222bc
PB
1353 ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
1354 PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
0dfa5ef9
IY
1355
1356 for (c = soundhw; c->name; ++c) {
1357 if (c->enabled) {
1358 if (c->isa) {
f81222bc 1359 if (!isa_bus) {
0971f1be 1360 error_report("ISA bus not available for %s", c->name);
f81222bc 1361 exit(1);
0dfa5ef9 1362 }
f81222bc 1363 c->init.init_isa(isa_bus);
0dfa5ef9 1364 } else {
f81222bc 1365 if (!pci_bus) {
0971f1be 1366 error_report("PCI bus not available for %s", c->name);
f81222bc 1367 exit(1);
0dfa5ef9 1368 }
f81222bc 1369 c->init.init_pci(pci_bus);
0dfa5ef9
IY
1370 }
1371 }
1372 }
1373}
ad96090a
BS
1374
1375int qemu_uuid_parse(const char *str, uint8_t *uuid)
1376{
1377 int ret;
1378
1379 if (strlen(str) != 36) {
1380 return -1;
1381 }
1382
1383 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1384 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1385 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1386 &uuid[15]);
1387
1388 if (ret != 16) {
1389 return -1;
1390 }
ad96090a
BS
1391 return 0;
1392}
1393
0c764a9d 1394void do_acpitable_option(const QemuOpts *opts)
ad96090a
BS
1395{
1396#ifdef TARGET_I386
23084327
LE
1397 Error *err = NULL;
1398
1399 acpi_table_add(opts, &err);
1400 if (err) {
4a44d85e
SA
1401 error_report("Wrong acpi table provided: %s",
1402 error_get_pretty(err));
23084327 1403 error_free(err);
ad96090a
BS
1404 exit(1);
1405 }
1406#endif
1407}
1408
4f953d2f 1409void do_smbios_option(QemuOpts *opts)
ad96090a
BS
1410{
1411#ifdef TARGET_I386
4f953d2f 1412 smbios_entry_add(opts);
ad96090a
BS
1413#endif
1414}
1415
1416void cpudef_init(void)
1417{
1418#if defined(cpudef_setup)
1419 cpudef_setup(); /* parse cpu definitions in target config file */
1420#endif
1421}
1422
ad96090a
BS
1423int kvm_available(void)
1424{
1425#ifdef CONFIG_KVM
1426 return 1;
1427#else
1428 return 0;
1429#endif
1430}
1431
1432int xen_available(void)
1433{
1434#ifdef CONFIG_XEN
1435 return 1;
1436#else
1437 return 0;
1438#endif
1439}
99afc91d
DB
1440
1441
1442TargetInfo *qmp_query_target(Error **errp)
1443{
1444 TargetInfo *info = g_malloc0(sizeof(*info));
1445
c02a9552 1446 info->arch = g_strdup(TARGET_NAME);
99afc91d
DB
1447
1448 return info;
1449}
7ca1dfad
CV
1450
1451/* Stub function that's gets run on the vcpu when its brought out of the
1452 VM to run inside qemu via async_run_on_cpu()*/
1453static void mig_sleep_cpu(void *opq)
1454{
1455 qemu_mutex_unlock_iothread();
1456 g_usleep(30*1000);
1457 qemu_mutex_lock_iothread();
1458}
1459
1460/* To reduce the dirty rate explicitly disallow the VCPUs from spending
1461 much time in the VM. The migration thread will try to catchup.
1462 Workload will experience a performance drop.
1463*/
7ca1dfad
CV
1464static void mig_throttle_guest_down(void)
1465{
38fcbd3f
AF
1466 CPUState *cpu;
1467
7ca1dfad 1468 qemu_mutex_lock_iothread();
38fcbd3f
AF
1469 CPU_FOREACH(cpu) {
1470 async_run_on_cpu(cpu, mig_sleep_cpu, NULL);
1471 }
7ca1dfad
CV
1472 qemu_mutex_unlock_iothread();
1473}
1474
1475static void check_guest_throttling(void)
1476{
1477 static int64_t t0;
1478 int64_t t1;
1479
1480 if (!mig_throttle_on) {
1481 return;
1482 }
1483
1484 if (!t0) {
bc72ad67 1485 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
7ca1dfad
CV
1486 return;
1487 }
1488
bc72ad67 1489 t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
7ca1dfad
CV
1490
1491 /* If it has been more than 40 ms since the last time the guest
1492 * was throttled then do it again.
1493 */
1494 if (40 < (t1-t0)/1000000) {
1495 mig_throttle_guest_down();
1496 t0 = t1;
1497 }
1498}