4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
28 #include <sys/types.h>
32 #include "monitor/monitor.h"
33 #include "sysemu/sysemu.h"
34 #include "qemu/bitops.h"
35 #include "qemu/bitmap.h"
36 #include "sysemu/arch_init.h"
37 #include "audio/audio.h"
38 #include "hw/i386/pc.h"
39 #include "hw/pci/pci.h"
40 #include "hw/audio/audio.h"
41 #include "sysemu/kvm.h"
42 #include "migration/migration.h"
43 #include "hw/i386/smbios.h"
44 #include "exec/address-spaces.h"
45 #include "hw/audio/pcspk.h"
46 #include "migration/page_cache.h"
47 #include "qemu/config-file.h"
48 #include "qmp-commands.h"
50 #include "exec/cpu-all.h"
51 #include "exec/ram_addr.h"
52 #include "hw/acpi/acpi.h"
54 #ifdef DEBUG_ARCH_INIT
55 #define DPRINTF(fmt, ...) \
56 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
58 #define DPRINTF(fmt, ...) \
63 int graphic_width = 1024;
64 int graphic_height = 768;
65 int graphic_depth = 8;
67 int graphic_width = 800;
68 int graphic_height = 600;
69 int graphic_depth = 32;
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
83 #elif defined(TARGET_LM32)
84 #define QEMU_ARCH QEMU_ARCH_LM32
85 #elif defined(TARGET_MICROBLAZE)
86 #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
87 #elif defined(TARGET_MIPS)
88 #define QEMU_ARCH QEMU_ARCH_MIPS
89 #elif defined(TARGET_MOXIE)
90 #define QEMU_ARCH QEMU_ARCH_MOXIE
91 #elif defined(TARGET_OPENRISC)
92 #define QEMU_ARCH QEMU_ARCH_OPENRISC
93 #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
101 #elif defined(TARGET_XTENSA)
102 #define QEMU_ARCH QEMU_ARCH_XTENSA
103 #elif defined(TARGET_UNICORE32)
104 #define QEMU_ARCH QEMU_ARCH_UNICORE32
107 const uint32_t arch_type = QEMU_ARCH;
108 static bool mig_throttle_on;
109 static int dirty_rate_high_cnt;
110 static void check_guest_throttling(void);
112 /***********************************************************/
113 /* ram save/restore */
115 #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
121 #define RAM_SAVE_FLAG_XBZRLE 0x40
122 /* 0x80 is reserved in migration.h start with 0x100 next */
125 static struct defconfig_file {
126 const char *filename;
127 /* Indicates it is an user config file (disabled by -no-user-config) */
129 } default_config_files[] = {
130 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
131 { CONFIG_QEMU_CONFDIR "/target-" TARGET_NAME ".conf", true },
132 { NULL }, /* end of list */
136 int qemu_read_default_config_files(bool userconfig)
139 struct defconfig_file *f;
141 for (f = default_config_files; f->filename; f++) {
142 if (!userconfig && f->userconfig) {
145 ret = qemu_read_config_file(f->filename);
146 if (ret < 0 && ret != -ENOENT) {
154 static inline bool is_zero_range(uint8_t *p, uint64_t size)
156 return buffer_find_nonzero_offset(p, size) == size;
159 /* struct contains XBZRLE cache and a static page
160 used by the compression */
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 */
178 int64_t xbzrle_cache_resize(int64_t new_size)
180 if (XBZRLE.cache != NULL) {
181 return cache_resize(XBZRLE.cache, new_size / TARGET_PAGE_SIZE) *
184 return pow2floor(new_size);
187 /* accounting for migration statistics */
188 typedef struct AccountingInfo {
190 uint64_t skipped_pages;
193 uint64_t xbzrle_bytes;
194 uint64_t xbzrle_pages;
195 uint64_t xbzrle_cache_miss;
196 uint64_t xbzrle_overflows;
199 static AccountingInfo acct_info;
201 static void acct_clear(void)
203 memset(&acct_info, 0, sizeof(acct_info));
206 uint64_t dup_mig_bytes_transferred(void)
208 return acct_info.dup_pages * TARGET_PAGE_SIZE;
211 uint64_t dup_mig_pages_transferred(void)
213 return acct_info.dup_pages;
216 uint64_t skipped_mig_bytes_transferred(void)
218 return acct_info.skipped_pages * TARGET_PAGE_SIZE;
221 uint64_t skipped_mig_pages_transferred(void)
223 return acct_info.skipped_pages;
226 uint64_t norm_mig_bytes_transferred(void)
228 return acct_info.norm_pages * TARGET_PAGE_SIZE;
231 uint64_t norm_mig_pages_transferred(void)
233 return acct_info.norm_pages;
236 uint64_t xbzrle_mig_bytes_transferred(void)
238 return acct_info.xbzrle_bytes;
241 uint64_t xbzrle_mig_pages_transferred(void)
243 return acct_info.xbzrle_pages;
246 uint64_t xbzrle_mig_pages_cache_miss(void)
248 return acct_info.xbzrle_cache_miss;
251 uint64_t xbzrle_mig_pages_overflow(void)
253 return acct_info.xbzrle_overflows;
256 static size_t save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
261 qemu_put_be64(f, offset | cont | flag);
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);
273 #define ENCODING_FLAG_XBZRLE 0x1
275 static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
276 ram_addr_t current_addr, RAMBlock *block,
277 ram_addr_t offset, int cont, bool last_stage)
279 int encoded_len = 0, bytes_sent = -1;
280 uint8_t *prev_cached_page;
282 if (!cache_is_cached(XBZRLE.cache, current_addr)) {
284 cache_insert(XBZRLE.cache, current_addr, current_data);
286 acct_info.xbzrle_cache_miss++;
290 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
292 /* save current buffer into memory */
293 memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
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,
299 if (encoded_len == 0) {
300 DPRINTF("Skipping unmodified page\n");
302 } else if (encoded_len == -1) {
303 DPRINTF("Overflow\n");
304 acct_info.xbzrle_overflows++;
305 /* update data in the cache */
306 memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
310 /* we need to update the data in the cache, in order to get the same data */
312 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
315 /* Send XBZRLE based compressed page */
316 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
317 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
318 qemu_put_be16(f, encoded_len);
319 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
320 bytes_sent += encoded_len + 1 + 2;
321 acct_info.xbzrle_pages++;
322 acct_info.xbzrle_bytes += bytes_sent;
328 /* This is the last block that we have visited serching for dirty pages
330 static RAMBlock *last_seen_block;
331 /* This is the last block from where we have sent data */
332 static RAMBlock *last_sent_block;
333 static ram_addr_t last_offset;
334 static unsigned long *migration_bitmap;
335 static uint64_t migration_dirty_pages;
336 static uint32_t last_version;
337 static bool ram_bulk_stage;
340 ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
343 unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS;
344 unsigned long nr = base + (start >> TARGET_PAGE_BITS);
345 uint64_t mr_size = TARGET_PAGE_ALIGN(memory_region_size(mr));
346 unsigned long size = base + (mr_size >> TARGET_PAGE_BITS);
350 if (ram_bulk_stage && nr > base) {
353 next = find_next_bit(migration_bitmap, size, nr);
357 clear_bit(next, migration_bitmap);
358 migration_dirty_pages--;
360 return (next - base) << TARGET_PAGE_BITS;
363 static inline bool migration_bitmap_set_dirty(ram_addr_t addr)
366 int nr = addr >> TARGET_PAGE_BITS;
368 ret = test_and_set_bit(nr, migration_bitmap);
371 migration_dirty_pages++;
376 static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length)
380 for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
381 if (cpu_physical_memory_get_dirty(start + addr,
383 DIRTY_MEMORY_MIGRATION)) {
384 cpu_physical_memory_reset_dirty(start + addr,
386 DIRTY_MEMORY_MIGRATION);
387 migration_bitmap_set_dirty(start + addr);
393 /* Needs iothread lock! */
395 static void migration_bitmap_sync(void)
398 uint64_t num_dirty_pages_init = migration_dirty_pages;
399 MigrationState *s = migrate_get_current();
400 static int64_t start_time;
401 static int64_t bytes_xfer_prev;
402 static int64_t num_dirty_pages_period;
404 int64_t bytes_xfer_now;
406 if (!bytes_xfer_prev) {
407 bytes_xfer_prev = ram_bytes_transferred();
411 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
414 trace_migration_bitmap_sync_start();
415 address_space_sync_dirty_bitmap(&address_space_memory);
417 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
418 migration_bitmap_sync_range(block->mr->ram_addr, block->length);
420 trace_migration_bitmap_sync_end(migration_dirty_pages
421 - num_dirty_pages_init);
422 num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
423 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
425 /* more than 1 second = 1000 millisecons */
426 if (end_time > start_time + 1000) {
427 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;
442 bytes_xfer_prev = bytes_xfer_now;
444 mig_throttle_on = false;
446 s->dirty_pages_rate = num_dirty_pages_period * 1000
447 / (end_time - start_time);
448 s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE;
449 start_time = end_time;
450 num_dirty_pages_period = 0;
455 * ram_save_block: Writes a page of memory to the stream f
457 * Returns: The number of bytes written.
458 * 0 means no dirty pages
461 static int ram_save_block(QEMUFile *f, bool last_stage)
463 RAMBlock *block = last_seen_block;
464 ram_addr_t offset = last_offset;
465 bool complete_round = false;
468 ram_addr_t current_addr;
471 block = QTAILQ_FIRST(&ram_list.blocks);
475 offset = migration_bitmap_find_and_reset_dirty(mr, offset);
476 if (complete_round && block == last_seen_block &&
477 offset >= last_offset) {
480 if (offset >= block->length) {
482 block = QTAILQ_NEXT(block, next);
484 block = QTAILQ_FIRST(&ram_list.blocks);
485 complete_round = true;
486 ram_bulk_stage = false;
491 int cont = (block == last_sent_block) ?
492 RAM_SAVE_FLAG_CONTINUE : 0;
494 p = memory_region_get_ram_ptr(mr) + offset;
496 /* In doubt sent page as normal */
498 ret = ram_control_save_page(f, block->offset,
499 offset, TARGET_PAGE_SIZE, &bytes_sent);
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++;
509 } else if (is_zero_range(p, TARGET_PAGE_SIZE)) {
510 acct_info.dup_pages++;
511 bytes_sent = save_block_hdr(f, block, offset, cont,
512 RAM_SAVE_FLAG_COMPRESS);
515 } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
516 current_addr = block->offset + offset;
517 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
518 offset, cont, last_stage);
520 p = get_cached_data(XBZRLE.cache, current_addr);
524 /* XBZRLE overflow or normal page */
525 if (bytes_sent == -1) {
526 bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
527 qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
528 bytes_sent += TARGET_PAGE_SIZE;
529 acct_info.norm_pages++;
532 /* if page is unmodified, continue to the next */
533 if (bytes_sent > 0) {
534 last_sent_block = block;
539 last_seen_block = block;
540 last_offset = offset;
545 static uint64_t bytes_transferred;
547 void acct_update_position(QEMUFile *f, size_t size, bool zero)
549 uint64_t pages = size / TARGET_PAGE_SIZE;
551 acct_info.dup_pages += pages;
553 acct_info.norm_pages += pages;
554 bytes_transferred += size;
555 qemu_update_position(f, size);
559 static ram_addr_t ram_save_remaining(void)
561 return migration_dirty_pages;
564 uint64_t ram_bytes_remaining(void)
566 return ram_save_remaining() * TARGET_PAGE_SIZE;
569 uint64_t ram_bytes_transferred(void)
571 return bytes_transferred;
574 uint64_t ram_bytes_total(void)
579 QTAILQ_FOREACH(block, &ram_list.blocks, next)
580 total += block->length;
585 static void migration_end(void)
587 if (migration_bitmap) {
588 memory_global_dirty_log_stop();
589 g_free(migration_bitmap);
590 migration_bitmap = NULL;
594 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);
603 static void ram_migration_cancel(void *opaque)
608 static void reset_ram_globals(void)
610 last_seen_block = NULL;
611 last_sent_block = NULL;
613 last_version = ram_list.version;
614 ram_bulk_stage = true;
617 #define MAX_WAIT 50 /* ms, half buffered_file limit */
619 static int ram_save_setup(QEMUFile *f, void *opaque)
622 int64_t ram_pages = last_ram_offset() >> TARGET_PAGE_BITS;
624 migration_bitmap = bitmap_new(ram_pages);
625 bitmap_set(migration_bitmap, 0, ram_pages);
626 migration_dirty_pages = ram_pages;
627 mig_throttle_on = false;
628 dirty_rate_high_cnt = 0;
630 if (migrate_use_xbzrle()) {
631 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
635 DPRINTF("Error creating cache\n");
638 XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
639 XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
643 qemu_mutex_lock_iothread();
644 qemu_mutex_lock_ramlist();
645 bytes_transferred = 0;
648 memory_global_dirty_log_start();
649 migration_bitmap_sync();
650 qemu_mutex_unlock_iothread();
652 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
654 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
655 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);
660 qemu_mutex_unlock_ramlist();
662 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
663 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
665 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
670 static int ram_save_iterate(QEMUFile *f, void *opaque)
677 qemu_mutex_lock_ramlist();
679 if (ram_list.version != last_version) {
683 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
685 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
687 while ((ret = qemu_file_rate_limit(f)) == 0) {
690 bytes_sent = ram_save_block(f, false);
691 /* no more blocks to sent */
692 if (bytes_sent == 0) {
695 total_sent += bytes_sent;
696 acct_info.iterations++;
697 check_guest_throttling();
698 /* 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
704 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
706 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
714 qemu_mutex_unlock_ramlist();
717 * Must occur before EOS (or any QEMUFile operation)
718 * because of RDMA protocol.
720 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
722 bytes_transferred += total_sent;
725 * Do not count these 8 bytes into total_sent, so that we can
726 * return 0 if no page had been dirtied.
728 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
729 bytes_transferred += 8;
731 ret = qemu_file_get_error(f);
739 static int ram_save_complete(QEMUFile *f, void *opaque)
741 qemu_mutex_lock_ramlist();
742 migration_bitmap_sync();
744 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
746 /* try transferring iterative blocks of memory */
748 /* flush all remaining blocks regardless of rate limiting */
752 bytes_sent = ram_save_block(f, true);
753 /* no more blocks to sent */
754 if (bytes_sent == 0) {
757 bytes_transferred += bytes_sent;
760 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
763 qemu_mutex_unlock_ramlist();
764 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
769 static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
771 uint64_t remaining_size;
773 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
775 if (remaining_size < max_size) {
776 qemu_mutex_lock_iothread();
777 migration_bitmap_sync();
778 qemu_mutex_unlock_iothread();
779 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
781 return remaining_size;
784 static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
790 if (!XBZRLE.decoded_buf) {
791 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
794 /* extract RLE header */
795 xh_flags = qemu_get_byte(f);
796 xh_len = qemu_get_be16(f);
798 if (xh_flags != ENCODING_FLAG_XBZRLE) {
799 fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
803 if (xh_len > TARGET_PAGE_SIZE) {
804 fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
807 /* load data and decode */
808 qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
811 ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
814 fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
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);
825 static inline void *host_from_stream_offset(QEMUFile *f,
829 static RAMBlock *block = NULL;
833 if (flags & RAM_SAVE_FLAG_CONTINUE) {
835 fprintf(stderr, "Ack, bad migration stream!\n");
839 return memory_region_get_ram_ptr(block->mr) + offset;
842 len = qemu_get_byte(f);
843 qemu_get_buffer(f, (uint8_t *)id, len);
846 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
847 if (!strncmp(id, block->idstr, sizeof(id)))
848 return memory_region_get_ram_ptr(block->mr) + offset;
851 fprintf(stderr, "Can't find block %s!\n", id);
856 * If a page (or a whole RDMA chunk) has been
857 * determined to be zero, then zap it.
859 void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
861 if (ch != 0 || !is_zero_range(host, size)) {
862 memset(host, ch, size);
866 static int ram_load(QEMUFile *f, void *opaque, int version_id)
871 static uint64_t seq_iter;
875 if (version_id < 4 || version_id > 4) {
880 addr = qemu_get_be64(f);
882 flags = addr & ~TARGET_PAGE_MASK;
883 addr &= TARGET_PAGE_MASK;
885 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
886 if (version_id == 4) {
887 /* Synchronize RAM block list */
890 ram_addr_t total_ram_bytes = addr;
892 while (total_ram_bytes) {
896 len = qemu_get_byte(f);
897 qemu_get_buffer(f, (uint8_t *)id, len);
899 length = qemu_get_be64(f);
901 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
902 if (!strncmp(id, block->idstr, sizeof(id))) {
903 if (block->length != length) {
905 "Length mismatch: %s: " RAM_ADDR_FMT
906 " in != " RAM_ADDR_FMT "\n", id, length,
916 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
917 "accept migration\n", id);
922 total_ram_bytes -= length;
927 if (flags & RAM_SAVE_FLAG_COMPRESS) {
931 host = host_from_stream_offset(f, addr, flags);
936 ch = qemu_get_byte(f);
937 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
938 } else if (flags & RAM_SAVE_FLAG_PAGE) {
941 host = host_from_stream_offset(f, addr, flags);
946 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
947 } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
948 void *host = host_from_stream_offset(f, addr, flags);
953 if (load_xbzrle(f, addr, host) < 0) {
957 } else if (flags & RAM_SAVE_FLAG_HOOK) {
958 ram_control_load_hook(f, flags);
960 error = qemu_file_get_error(f);
965 } while (!(flags & RAM_SAVE_FLAG_EOS));
968 DPRINTF("Completed load of VM with exit code %d seq iteration "
969 "%" PRIu64 "\n", ret, seq_iter);
973 SaveVMHandlers savevm_ram_handlers = {
974 .save_live_setup = ram_save_setup,
975 .save_live_iterate = ram_save_iterate,
976 .save_live_complete = ram_save_complete,
977 .save_live_pending = ram_save_pending,
978 .load_state = ram_load,
979 .cancel = ram_migration_cancel,
988 int (*init_isa) (ISABus *bus);
989 int (*init_pci) (PCIBus *bus);
993 static struct soundhw soundhw[9];
994 static int soundhw_count;
996 void isa_register_soundhw(const char *name, const char *descr,
997 int (*init_isa)(ISABus *bus))
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;
1007 void pci_register_soundhw(const char *name, const char *descr,
1008 int (*init_pci)(PCIBus *bus))
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;
1018 void select_soundhw(const char *optarg)
1022 if (is_help_option(optarg)) {
1025 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);
1030 printf("\n-soundhw all will enable all of the above\n");
1032 printf("Machine has no user-selectable audio hardware "
1033 "(it may or may not have always-present audio hardware).\n");
1035 exit(!is_help_option(optarg));
1043 if (!strcmp(optarg, "all")) {
1044 for (c = soundhw; c->name; ++c) {
1053 l = !e ? strlen(p) : (size_t) (e - p);
1055 for (c = soundhw; c->name; ++c) {
1056 if (!strncmp(c->name, p, l) && !c->name[l]) {
1065 "Unknown sound card name (too big to show)\n");
1068 fprintf(stderr, "Unknown sound card name `%.*s'\n",
1073 p += l + (e != NULL);
1077 goto show_valid_cards;
1082 void audio_init(void)
1085 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);
1088 for (c = soundhw; c->name; ++c) {
1092 fprintf(stderr, "ISA bus not available for %s\n", c->name);
1095 c->init.init_isa(isa_bus);
1098 fprintf(stderr, "PCI bus not available for %s\n", c->name);
1101 c->init.init_pci(pci_bus);
1107 int qemu_uuid_parse(const char *str, uint8_t *uuid)
1111 if (strlen(str) != 36) {
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],
1126 void do_acpitable_option(const QemuOpts *opts)
1131 acpi_table_add(opts, &err);
1133 error_report("Wrong acpi table provided: %s",
1134 error_get_pretty(err));
1141 void do_smbios_option(QemuOpts *opts)
1144 smbios_entry_add(opts);
1148 void cpudef_init(void)
1150 #if defined(cpudef_setup)
1151 cpudef_setup(); /* parse cpu definitions in target config file */
1155 int tcg_available(void)
1160 int kvm_available(void)
1169 int xen_available(void)
1179 TargetInfo *qmp_query_target(Error **errp)
1181 TargetInfo *info = g_malloc0(sizeof(*info));
1183 info->arch = g_strdup(TARGET_NAME);
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()*/
1190 static void mig_sleep_cpu(void *opq)
1192 qemu_mutex_unlock_iothread();
1194 qemu_mutex_lock_iothread();
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.
1201 static void mig_throttle_guest_down(void)
1205 qemu_mutex_lock_iothread();
1207 async_run_on_cpu(cpu, mig_sleep_cpu, NULL);
1209 qemu_mutex_unlock_iothread();
1212 static void check_guest_throttling(void)
1217 if (!mig_throttle_on) {
1222 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1226 t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1228 /* If it has been more than 40 ms since the last time the guest
1229 * was throttled then do it again.
1231 if (40 < (t1-t0)/1000000) {
1232 mig_throttle_guest_down();