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 "qemu/error-report.h"
49 #include "qmp-commands.h"
51 #include "exec/cpu-all.h"
52 #include "exec/ram_addr.h"
53 #include "hw/acpi/acpi.h"
54 #include "qemu/host-utils.h"
55 #include "qemu/rcu_queue.h"
57 #ifdef DEBUG_ARCH_INIT
58 #define DPRINTF(fmt, ...) \
59 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
61 #define DPRINTF(fmt, ...) \
66 int graphic_width
= 1024;
67 int graphic_height
= 768;
68 int graphic_depth
= 8;
70 int graphic_width
= 800;
71 int graphic_height
= 600;
72 int graphic_depth
= 32;
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
86 #elif defined(TARGET_LM32)
87 #define QEMU_ARCH QEMU_ARCH_LM32
88 #elif defined(TARGET_MICROBLAZE)
89 #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
90 #elif defined(TARGET_MIPS)
91 #define QEMU_ARCH QEMU_ARCH_MIPS
92 #elif defined(TARGET_MOXIE)
93 #define QEMU_ARCH QEMU_ARCH_MOXIE
94 #elif defined(TARGET_OPENRISC)
95 #define QEMU_ARCH QEMU_ARCH_OPENRISC
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
104 #elif defined(TARGET_XTENSA)
105 #define QEMU_ARCH QEMU_ARCH_XTENSA
106 #elif defined(TARGET_UNICORE32)
107 #define QEMU_ARCH QEMU_ARCH_UNICORE32
108 #elif defined(TARGET_TRICORE)
109 #define QEMU_ARCH QEMU_ARCH_TRICORE
112 const uint32_t arch_type
= QEMU_ARCH
;
113 static bool mig_throttle_on
;
114 static int dirty_rate_high_cnt
;
115 static void check_guest_throttling(void);
117 static uint64_t bitmap_sync_count
;
119 /***********************************************************/
120 /* ram save/restore */
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
128 #define RAM_SAVE_FLAG_XBZRLE 0x40
129 /* 0x80 is reserved in migration.h start with 0x100 next */
131 static struct defconfig_file
{
132 const char *filename
;
133 /* Indicates it is an user config file (disabled by -no-user-config) */
135 } default_config_files
[] = {
136 { CONFIG_QEMU_CONFDIR
"/qemu.conf", true },
137 { CONFIG_QEMU_CONFDIR
"/target-" TARGET_NAME
".conf", true },
138 { NULL
}, /* end of list */
141 static const uint8_t ZERO_TARGET_PAGE
[TARGET_PAGE_SIZE
];
143 int qemu_read_default_config_files(bool userconfig
)
146 struct defconfig_file
*f
;
148 for (f
= default_config_files
; f
->filename
; f
++) {
149 if (!userconfig
&& f
->userconfig
) {
152 ret
= qemu_read_config_file(f
->filename
);
153 if (ret
< 0 && ret
!= -ENOENT
) {
161 static inline bool is_zero_range(uint8_t *p
, uint64_t size
)
163 return buffer_find_nonzero_offset(p
, size
) == size
;
166 /* struct contains XBZRLE cache and a static page
167 used by the compression */
169 /* buffer used for XBZRLE encoding */
170 uint8_t *encoded_buf
;
171 /* buffer for storing page content */
172 uint8_t *current_buf
;
173 /* Cache for XBZRLE, Protected by lock. */
178 /* buffer used for XBZRLE decoding */
179 static uint8_t *xbzrle_decoded_buf
;
181 static void XBZRLE_cache_lock(void)
183 if (migrate_use_xbzrle())
184 qemu_mutex_lock(&XBZRLE
.lock
);
187 static void XBZRLE_cache_unlock(void)
189 if (migrate_use_xbzrle())
190 qemu_mutex_unlock(&XBZRLE
.lock
);
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().
199 int64_t xbzrle_cache_resize(int64_t new_size
)
201 PageCache
*new_cache
;
204 if (new_size
< TARGET_PAGE_SIZE
) {
210 if (XBZRLE
.cache
!= NULL
) {
211 if (pow2floor(new_size
) == migrate_xbzrle_cache_size()) {
214 new_cache
= cache_init(new_size
/ TARGET_PAGE_SIZE
,
217 error_report("Error creating cache");
222 cache_fini(XBZRLE
.cache
);
223 XBZRLE
.cache
= new_cache
;
227 ret
= pow2floor(new_size
);
229 XBZRLE_cache_unlock();
233 /* accounting for migration statistics */
234 typedef struct AccountingInfo
{
236 uint64_t skipped_pages
;
239 uint64_t xbzrle_bytes
;
240 uint64_t xbzrle_pages
;
241 uint64_t xbzrle_cache_miss
;
242 double xbzrle_cache_miss_rate
;
243 uint64_t xbzrle_overflows
;
246 static AccountingInfo acct_info
;
248 static void acct_clear(void)
250 memset(&acct_info
, 0, sizeof(acct_info
));
253 uint64_t dup_mig_bytes_transferred(void)
255 return acct_info
.dup_pages
* TARGET_PAGE_SIZE
;
258 uint64_t dup_mig_pages_transferred(void)
260 return acct_info
.dup_pages
;
263 uint64_t skipped_mig_bytes_transferred(void)
265 return acct_info
.skipped_pages
* TARGET_PAGE_SIZE
;
268 uint64_t skipped_mig_pages_transferred(void)
270 return acct_info
.skipped_pages
;
273 uint64_t norm_mig_bytes_transferred(void)
275 return acct_info
.norm_pages
* TARGET_PAGE_SIZE
;
278 uint64_t norm_mig_pages_transferred(void)
280 return acct_info
.norm_pages
;
283 uint64_t xbzrle_mig_bytes_transferred(void)
285 return acct_info
.xbzrle_bytes
;
288 uint64_t xbzrle_mig_pages_transferred(void)
290 return acct_info
.xbzrle_pages
;
293 uint64_t xbzrle_mig_pages_cache_miss(void)
295 return acct_info
.xbzrle_cache_miss
;
298 double xbzrle_mig_cache_miss_rate(void)
300 return acct_info
.xbzrle_cache_miss_rate
;
303 uint64_t xbzrle_mig_pages_overflow(void)
305 return acct_info
.xbzrle_overflows
;
308 static size_t save_block_hdr(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
,
313 qemu_put_be64(f
, offset
| cont
| flag
);
317 qemu_put_byte(f
, strlen(block
->idstr
));
318 qemu_put_buffer(f
, (uint8_t *)block
->idstr
,
319 strlen(block
->idstr
));
320 size
+= 1 + strlen(block
->idstr
);
325 /* This is the last block that we have visited serching for dirty pages
327 static RAMBlock
*last_seen_block
;
328 /* This is the last block from where we have sent data */
329 static RAMBlock
*last_sent_block
;
330 static ram_addr_t last_offset
;
331 static unsigned long *migration_bitmap
;
332 static uint64_t migration_dirty_pages
;
333 static uint32_t last_version
;
334 static bool ram_bulk_stage
;
336 /* Update the xbzrle cache to reflect a page that's been sent as all 0.
337 * The important thing is that a stale (not-yet-0'd) page be replaced
339 * As a bonus, if the page wasn't in the cache it gets added so that
340 * when a small write is made into the 0'd page it gets XBZRLE sent
342 static void xbzrle_cache_zero_page(ram_addr_t current_addr
)
344 if (ram_bulk_stage
|| !migrate_use_xbzrle()) {
348 /* We don't care if this fails to allocate a new cache page
349 * as long as it updated an old one */
350 cache_insert(XBZRLE
.cache
, current_addr
, ZERO_TARGET_PAGE
,
354 #define ENCODING_FLAG_XBZRLE 0x1
356 static int save_xbzrle_page(QEMUFile
*f
, uint8_t **current_data
,
357 ram_addr_t current_addr
, RAMBlock
*block
,
358 ram_addr_t offset
, int cont
, bool last_stage
)
360 int encoded_len
= 0, bytes_sent
= -1;
361 uint8_t *prev_cached_page
;
363 if (!cache_is_cached(XBZRLE
.cache
, current_addr
, bitmap_sync_count
)) {
364 acct_info
.xbzrle_cache_miss
++;
366 if (cache_insert(XBZRLE
.cache
, current_addr
, *current_data
,
367 bitmap_sync_count
) == -1) {
370 /* update *current_data when the page has been
371 inserted into cache */
372 *current_data
= get_cached_data(XBZRLE
.cache
, current_addr
);
378 prev_cached_page
= get_cached_data(XBZRLE
.cache
, current_addr
);
380 /* save current buffer into memory */
381 memcpy(XBZRLE
.current_buf
, *current_data
, TARGET_PAGE_SIZE
);
383 /* XBZRLE encoding (if there is no overflow) */
384 encoded_len
= xbzrle_encode_buffer(prev_cached_page
, XBZRLE
.current_buf
,
385 TARGET_PAGE_SIZE
, XBZRLE
.encoded_buf
,
387 if (encoded_len
== 0) {
388 DPRINTF("Skipping unmodified page\n");
390 } else if (encoded_len
== -1) {
391 DPRINTF("Overflow\n");
392 acct_info
.xbzrle_overflows
++;
393 /* update data in the cache */
395 memcpy(prev_cached_page
, *current_data
, TARGET_PAGE_SIZE
);
396 *current_data
= prev_cached_page
;
401 /* we need to update the data in the cache, in order to get the same data */
403 memcpy(prev_cached_page
, XBZRLE
.current_buf
, TARGET_PAGE_SIZE
);
406 /* Send XBZRLE based compressed page */
407 bytes_sent
= save_block_hdr(f
, block
, offset
, cont
, RAM_SAVE_FLAG_XBZRLE
);
408 qemu_put_byte(f
, ENCODING_FLAG_XBZRLE
);
409 qemu_put_be16(f
, encoded_len
);
410 qemu_put_buffer(f
, XBZRLE
.encoded_buf
, encoded_len
);
411 bytes_sent
+= encoded_len
+ 1 + 2;
412 acct_info
.xbzrle_pages
++;
413 acct_info
.xbzrle_bytes
+= bytes_sent
;
419 ram_addr_t
migration_bitmap_find_and_reset_dirty(MemoryRegion
*mr
,
422 unsigned long base
= mr
->ram_addr
>> TARGET_PAGE_BITS
;
423 unsigned long nr
= base
+ (start
>> TARGET_PAGE_BITS
);
424 uint64_t mr_size
= TARGET_PAGE_ALIGN(memory_region_size(mr
));
425 unsigned long size
= base
+ (mr_size
>> TARGET_PAGE_BITS
);
429 if (ram_bulk_stage
&& nr
> base
) {
432 next
= find_next_bit(migration_bitmap
, size
, nr
);
436 clear_bit(next
, migration_bitmap
);
437 migration_dirty_pages
--;
439 return (next
- base
) << TARGET_PAGE_BITS
;
442 static inline bool migration_bitmap_set_dirty(ram_addr_t addr
)
445 int nr
= addr
>> TARGET_PAGE_BITS
;
447 ret
= test_and_set_bit(nr
, migration_bitmap
);
450 migration_dirty_pages
++;
455 static void migration_bitmap_sync_range(ram_addr_t start
, ram_addr_t length
)
458 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
460 /* start address is aligned at the start of a word? */
461 if (((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) {
463 int nr
= BITS_TO_LONGS(length
>> TARGET_PAGE_BITS
);
464 unsigned long *src
= ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
];
466 for (k
= page
; k
< page
+ nr
; k
++) {
468 unsigned long new_dirty
;
469 new_dirty
= ~migration_bitmap
[k
];
470 migration_bitmap
[k
] |= src
[k
];
472 migration_dirty_pages
+= ctpopl(new_dirty
);
477 for (addr
= 0; addr
< length
; addr
+= TARGET_PAGE_SIZE
) {
478 if (cpu_physical_memory_get_dirty(start
+ addr
,
480 DIRTY_MEMORY_MIGRATION
)) {
481 cpu_physical_memory_reset_dirty(start
+ addr
,
483 DIRTY_MEMORY_MIGRATION
);
484 migration_bitmap_set_dirty(start
+ addr
);
491 /* Fix me: there are too many global variables used in migration process. */
492 static int64_t start_time
;
493 static int64_t bytes_xfer_prev
;
494 static int64_t num_dirty_pages_period
;
496 static void migration_bitmap_sync_init(void)
500 num_dirty_pages_period
= 0;
503 /* Called with iothread lock held, to protect ram_list.dirty_memory[] */
504 static void migration_bitmap_sync(void)
507 uint64_t num_dirty_pages_init
= migration_dirty_pages
;
508 MigrationState
*s
= migrate_get_current();
510 int64_t bytes_xfer_now
;
511 static uint64_t xbzrle_cache_miss_prev
;
512 static uint64_t iterations_prev
;
516 if (!bytes_xfer_prev
) {
517 bytes_xfer_prev
= ram_bytes_transferred();
521 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
524 trace_migration_bitmap_sync_start();
525 address_space_sync_dirty_bitmap(&address_space_memory
);
528 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
529 migration_bitmap_sync_range(block
->mr
->ram_addr
, block
->used_length
);
533 trace_migration_bitmap_sync_end(migration_dirty_pages
534 - num_dirty_pages_init
);
535 num_dirty_pages_period
+= migration_dirty_pages
- num_dirty_pages_init
;
536 end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
538 /* more than 1 second = 1000 millisecons */
539 if (end_time
> start_time
+ 1000) {
540 if (migrate_auto_converge()) {
541 /* The following detection logic can be refined later. For now:
542 Check to see if the dirtied bytes is 50% more than the approx.
543 amount of bytes that just got transferred since the last time we
544 were in this routine. If that happens >N times (for now N==4)
545 we turn on the throttle down logic */
546 bytes_xfer_now
= ram_bytes_transferred();
547 if (s
->dirty_pages_rate
&&
548 (num_dirty_pages_period
* TARGET_PAGE_SIZE
>
549 (bytes_xfer_now
- bytes_xfer_prev
)/2) &&
550 (dirty_rate_high_cnt
++ > 4)) {
551 trace_migration_throttle();
552 mig_throttle_on
= true;
553 dirty_rate_high_cnt
= 0;
555 bytes_xfer_prev
= bytes_xfer_now
;
557 mig_throttle_on
= false;
559 if (migrate_use_xbzrle()) {
560 if (iterations_prev
!= 0) {
561 acct_info
.xbzrle_cache_miss_rate
=
562 (double)(acct_info
.xbzrle_cache_miss
-
563 xbzrle_cache_miss_prev
) /
564 (acct_info
.iterations
- iterations_prev
);
566 iterations_prev
= acct_info
.iterations
;
567 xbzrle_cache_miss_prev
= acct_info
.xbzrle_cache_miss
;
569 s
->dirty_pages_rate
= num_dirty_pages_period
* 1000
570 / (end_time
- start_time
);
571 s
->dirty_bytes_rate
= s
->dirty_pages_rate
* TARGET_PAGE_SIZE
;
572 start_time
= end_time
;
573 num_dirty_pages_period
= 0;
574 s
->dirty_sync_count
= bitmap_sync_count
;
579 * ram_save_page: Send the given page to the stream
581 * Returns: Number of bytes written.
583 static int ram_save_page(QEMUFile
*f
, RAMBlock
* block
, ram_addr_t offset
,
588 ram_addr_t current_addr
;
589 MemoryRegion
*mr
= block
->mr
;
592 bool send_async
= true;
594 cont
= (block
== last_sent_block
) ? RAM_SAVE_FLAG_CONTINUE
: 0;
596 p
= memory_region_get_ram_ptr(mr
) + offset
;
598 /* In doubt sent page as normal */
600 ret
= ram_control_save_page(f
, block
->offset
,
601 offset
, TARGET_PAGE_SIZE
, &bytes_sent
);
605 current_addr
= block
->offset
+ offset
;
606 if (ret
!= RAM_SAVE_CONTROL_NOT_SUPP
) {
607 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
608 if (bytes_sent
> 0) {
609 acct_info
.norm_pages
++;
610 } else if (bytes_sent
== 0) {
611 acct_info
.dup_pages
++;
614 } else if (is_zero_range(p
, TARGET_PAGE_SIZE
)) {
615 acct_info
.dup_pages
++;
616 bytes_sent
= save_block_hdr(f
, block
, offset
, cont
,
617 RAM_SAVE_FLAG_COMPRESS
);
620 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
621 * page would be stale
623 xbzrle_cache_zero_page(current_addr
);
624 } else if (!ram_bulk_stage
&& migrate_use_xbzrle()) {
625 bytes_sent
= save_xbzrle_page(f
, &p
, current_addr
, block
,
626 offset
, cont
, last_stage
);
628 /* Can't send this cached data async, since the cache page
629 * might get updated before it gets to the wire
635 /* XBZRLE overflow or normal page */
636 if (bytes_sent
== -1) {
637 bytes_sent
= save_block_hdr(f
, block
, offset
, cont
, RAM_SAVE_FLAG_PAGE
);
639 qemu_put_buffer_async(f
, p
, TARGET_PAGE_SIZE
);
641 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
643 bytes_sent
+= TARGET_PAGE_SIZE
;
644 acct_info
.norm_pages
++;
647 XBZRLE_cache_unlock();
653 * ram_find_and_save_block: Finds a page to send and sends it to f
655 * Called within an RCU critical section.
657 * Returns: The number of bytes written.
658 * 0 means no dirty pages
661 static int ram_find_and_save_block(QEMUFile
*f
, bool last_stage
)
663 RAMBlock
*block
= last_seen_block
;
664 ram_addr_t offset
= last_offset
;
665 bool complete_round
= false;
670 block
= QLIST_FIRST_RCU(&ram_list
.blocks
);
674 offset
= migration_bitmap_find_and_reset_dirty(mr
, offset
);
675 if (complete_round
&& block
== last_seen_block
&&
676 offset
>= last_offset
) {
679 if (offset
>= block
->used_length
) {
681 block
= QLIST_NEXT_RCU(block
, next
);
683 block
= QLIST_FIRST_RCU(&ram_list
.blocks
);
684 complete_round
= true;
685 ram_bulk_stage
= false;
688 bytes_sent
= ram_save_page(f
, block
, offset
, last_stage
);
690 /* if page is unmodified, continue to the next */
691 if (bytes_sent
> 0) {
692 last_sent_block
= block
;
698 last_seen_block
= block
;
699 last_offset
= offset
;
703 static uint64_t bytes_transferred
;
705 void acct_update_position(QEMUFile
*f
, size_t size
, bool zero
)
707 uint64_t pages
= size
/ TARGET_PAGE_SIZE
;
709 acct_info
.dup_pages
+= pages
;
711 acct_info
.norm_pages
+= pages
;
712 bytes_transferred
+= size
;
713 qemu_update_position(f
, size
);
717 static ram_addr_t
ram_save_remaining(void)
719 return migration_dirty_pages
;
722 uint64_t ram_bytes_remaining(void)
724 return ram_save_remaining() * TARGET_PAGE_SIZE
;
727 uint64_t ram_bytes_transferred(void)
729 return bytes_transferred
;
732 uint64_t ram_bytes_total(void)
738 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
)
739 total
+= block
->used_length
;
744 void free_xbzrle_decoded_buf(void)
746 g_free(xbzrle_decoded_buf
);
747 xbzrle_decoded_buf
= NULL
;
750 static void migration_end(void)
752 if (migration_bitmap
) {
753 memory_global_dirty_log_stop();
754 g_free(migration_bitmap
);
755 migration_bitmap
= NULL
;
760 cache_fini(XBZRLE
.cache
);
761 g_free(XBZRLE
.encoded_buf
);
762 g_free(XBZRLE
.current_buf
);
764 XBZRLE
.encoded_buf
= NULL
;
765 XBZRLE
.current_buf
= NULL
;
767 XBZRLE_cache_unlock();
770 static void ram_migration_cancel(void *opaque
)
775 static void reset_ram_globals(void)
777 last_seen_block
= NULL
;
778 last_sent_block
= NULL
;
780 last_version
= ram_list
.version
;
781 ram_bulk_stage
= true;
784 #define MAX_WAIT 50 /* ms, half buffered_file limit */
787 /* Each of ram_save_setup, ram_save_iterate and ram_save_complete has
788 * long-running RCU critical section. When rcu-reclaims in the code
789 * start to become numerous it will be necessary to reduce the
790 * granularity of these critical sections.
793 static int ram_save_setup(QEMUFile
*f
, void *opaque
)
796 int64_t ram_bitmap_pages
; /* Size of bitmap in pages, including gaps */
798 mig_throttle_on
= false;
799 dirty_rate_high_cnt
= 0;
800 bitmap_sync_count
= 0;
801 migration_bitmap_sync_init();
803 if (migrate_use_xbzrle()) {
805 XBZRLE
.cache
= cache_init(migrate_xbzrle_cache_size() /
809 XBZRLE_cache_unlock();
810 error_report("Error creating cache");
813 XBZRLE_cache_unlock();
815 /* We prefer not to abort if there is no memory */
816 XBZRLE
.encoded_buf
= g_try_malloc0(TARGET_PAGE_SIZE
);
817 if (!XBZRLE
.encoded_buf
) {
818 error_report("Error allocating encoded_buf");
822 XBZRLE
.current_buf
= g_try_malloc(TARGET_PAGE_SIZE
);
823 if (!XBZRLE
.current_buf
) {
824 error_report("Error allocating current_buf");
825 g_free(XBZRLE
.encoded_buf
);
826 XBZRLE
.encoded_buf
= NULL
;
833 /* iothread lock needed for ram_list.dirty_memory[] */
834 qemu_mutex_lock_iothread();
835 qemu_mutex_lock_ramlist();
837 bytes_transferred
= 0;
840 ram_bitmap_pages
= last_ram_offset() >> TARGET_PAGE_BITS
;
841 migration_bitmap
= bitmap_new(ram_bitmap_pages
);
842 bitmap_set(migration_bitmap
, 0, ram_bitmap_pages
);
845 * Count the total number of pages used by ram blocks not including any
846 * gaps due to alignment or unplugs.
848 migration_dirty_pages
= 0;
849 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
850 uint64_t block_pages
;
852 block_pages
= block
->used_length
>> TARGET_PAGE_BITS
;
853 migration_dirty_pages
+= block_pages
;
856 memory_global_dirty_log_start();
857 migration_bitmap_sync();
858 qemu_mutex_unlock_ramlist();
859 qemu_mutex_unlock_iothread();
861 qemu_put_be64(f
, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE
);
863 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
864 qemu_put_byte(f
, strlen(block
->idstr
));
865 qemu_put_buffer(f
, (uint8_t *)block
->idstr
, strlen(block
->idstr
));
866 qemu_put_be64(f
, block
->used_length
);
871 ram_control_before_iterate(f
, RAM_CONTROL_SETUP
);
872 ram_control_after_iterate(f
, RAM_CONTROL_SETUP
);
874 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
879 static int ram_save_iterate(QEMUFile
*f
, void *opaque
)
887 if (ram_list
.version
!= last_version
) {
891 /* Read version before ram_list.blocks */
894 ram_control_before_iterate(f
, RAM_CONTROL_ROUND
);
896 t0
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
898 while ((ret
= qemu_file_rate_limit(f
)) == 0) {
901 bytes_sent
= ram_find_and_save_block(f
, false);
902 /* no more blocks to sent */
903 if (bytes_sent
== 0) {
906 total_sent
+= bytes_sent
;
907 acct_info
.iterations
++;
908 check_guest_throttling();
909 /* we want to check in the 1st loop, just in case it was the 1st time
910 and we had to sync the dirty bitmap.
911 qemu_get_clock_ns() is a bit expensive, so we only check each some
915 uint64_t t1
= (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - t0
) / 1000000;
917 DPRINTF("big wait: %" PRIu64
" milliseconds, %d iterations\n",
927 * Must occur before EOS (or any QEMUFile operation)
928 * because of RDMA protocol.
930 ram_control_after_iterate(f
, RAM_CONTROL_ROUND
);
932 bytes_transferred
+= total_sent
;
935 * Do not count these 8 bytes into total_sent, so that we can
936 * return 0 if no page had been dirtied.
938 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
939 bytes_transferred
+= 8;
941 ret
= qemu_file_get_error(f
);
949 /* Called with iothread lock */
950 static int ram_save_complete(QEMUFile
*f
, void *opaque
)
954 migration_bitmap_sync();
956 ram_control_before_iterate(f
, RAM_CONTROL_FINISH
);
958 /* try transferring iterative blocks of memory */
960 /* flush all remaining blocks regardless of rate limiting */
964 bytes_sent
= ram_find_and_save_block(f
, true);
965 /* no more blocks to sent */
966 if (bytes_sent
== 0) {
969 bytes_transferred
+= bytes_sent
;
972 ram_control_after_iterate(f
, RAM_CONTROL_FINISH
);
976 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
981 static uint64_t ram_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
983 uint64_t remaining_size
;
985 remaining_size
= ram_save_remaining() * TARGET_PAGE_SIZE
;
987 if (remaining_size
< max_size
) {
988 qemu_mutex_lock_iothread();
990 migration_bitmap_sync();
992 qemu_mutex_unlock_iothread();
993 remaining_size
= ram_save_remaining() * TARGET_PAGE_SIZE
;
995 return remaining_size
;
998 static int load_xbzrle(QEMUFile
*f
, ram_addr_t addr
, void *host
)
1000 unsigned int xh_len
;
1003 if (!xbzrle_decoded_buf
) {
1004 xbzrle_decoded_buf
= g_malloc(TARGET_PAGE_SIZE
);
1007 /* extract RLE header */
1008 xh_flags
= qemu_get_byte(f
);
1009 xh_len
= qemu_get_be16(f
);
1011 if (xh_flags
!= ENCODING_FLAG_XBZRLE
) {
1012 error_report("Failed to load XBZRLE page - wrong compression!");
1016 if (xh_len
> TARGET_PAGE_SIZE
) {
1017 error_report("Failed to load XBZRLE page - len overflow!");
1020 /* load data and decode */
1021 qemu_get_buffer(f
, xbzrle_decoded_buf
, xh_len
);
1024 if (xbzrle_decode_buffer(xbzrle_decoded_buf
, xh_len
, host
,
1025 TARGET_PAGE_SIZE
) == -1) {
1026 error_report("Failed to load XBZRLE page - decode error!");
1033 /* Must be called from within a rcu critical section.
1034 * Returns a pointer from within the RCU-protected ram_list.
1036 static inline void *host_from_stream_offset(QEMUFile
*f
,
1040 static RAMBlock
*block
= NULL
;
1044 if (flags
& RAM_SAVE_FLAG_CONTINUE
) {
1045 if (!block
|| block
->max_length
<= offset
) {
1046 error_report("Ack, bad migration stream!");
1050 return memory_region_get_ram_ptr(block
->mr
) + offset
;
1053 len
= qemu_get_byte(f
);
1054 qemu_get_buffer(f
, (uint8_t *)id
, len
);
1057 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1058 if (!strncmp(id
, block
->idstr
, sizeof(id
)) &&
1059 block
->max_length
> offset
) {
1060 return memory_region_get_ram_ptr(block
->mr
) + offset
;
1064 error_report("Can't find block %s!", id
);
1069 * If a page (or a whole RDMA chunk) has been
1070 * determined to be zero, then zap it.
1072 void ram_handle_compressed(void *host
, uint8_t ch
, uint64_t size
)
1074 if (ch
!= 0 || !is_zero_range(host
, size
)) {
1075 memset(host
, ch
, size
);
1079 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1081 int flags
= 0, ret
= 0;
1082 static uint64_t seq_iter
;
1086 if (version_id
!= 4) {
1090 /* This RCU critical section can be very long running.
1091 * When RCU reclaims in the code start to become numerous,
1092 * it will be necessary to reduce the granularity of this
1096 while (!ret
&& !(flags
& RAM_SAVE_FLAG_EOS
)) {
1097 ram_addr_t addr
, total_ram_bytes
;
1101 addr
= qemu_get_be64(f
);
1102 flags
= addr
& ~TARGET_PAGE_MASK
;
1103 addr
&= TARGET_PAGE_MASK
;
1105 switch (flags
& ~RAM_SAVE_FLAG_CONTINUE
) {
1106 case RAM_SAVE_FLAG_MEM_SIZE
:
1107 /* Synchronize RAM block list */
1108 total_ram_bytes
= addr
;
1109 while (!ret
&& total_ram_bytes
) {
1115 len
= qemu_get_byte(f
);
1116 qemu_get_buffer(f
, (uint8_t *)id
, len
);
1118 length
= qemu_get_be64(f
);
1120 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1121 if (!strncmp(id
, block
->idstr
, sizeof(id
))) {
1122 if (length
!= block
->used_length
) {
1123 Error
*local_err
= NULL
;
1125 ret
= qemu_ram_resize(block
->offset
, length
, &local_err
);
1127 error_report_err(local_err
);
1135 error_report("Unknown ramblock \"%s\", cannot "
1136 "accept migration", id
);
1140 total_ram_bytes
-= length
;
1143 case RAM_SAVE_FLAG_COMPRESS
:
1144 host
= host_from_stream_offset(f
, addr
, flags
);
1146 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
1150 ch
= qemu_get_byte(f
);
1151 ram_handle_compressed(host
, ch
, TARGET_PAGE_SIZE
);
1153 case RAM_SAVE_FLAG_PAGE
:
1154 host
= host_from_stream_offset(f
, addr
, flags
);
1156 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
1160 qemu_get_buffer(f
, host
, TARGET_PAGE_SIZE
);
1162 case RAM_SAVE_FLAG_XBZRLE
:
1163 host
= host_from_stream_offset(f
, addr
, flags
);
1165 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
1169 if (load_xbzrle(f
, addr
, host
) < 0) {
1170 error_report("Failed to decompress XBZRLE page at "
1171 RAM_ADDR_FMT
, addr
);
1176 case RAM_SAVE_FLAG_EOS
:
1180 if (flags
& RAM_SAVE_FLAG_HOOK
) {
1181 ram_control_load_hook(f
, flags
);
1183 error_report("Unknown combination of migration flags: %#x",
1189 ret
= qemu_file_get_error(f
);
1194 DPRINTF("Completed load of VM with exit code %d seq iteration "
1195 "%" PRIu64
"\n", ret
, seq_iter
);
1199 static SaveVMHandlers savevm_ram_handlers
= {
1200 .save_live_setup
= ram_save_setup
,
1201 .save_live_iterate
= ram_save_iterate
,
1202 .save_live_complete
= ram_save_complete
,
1203 .save_live_pending
= ram_save_pending
,
1204 .load_state
= ram_load
,
1205 .cancel
= ram_migration_cancel
,
1208 void ram_mig_init(void)
1210 qemu_mutex_init(&XBZRLE
.lock
);
1211 register_savevm_live(NULL
, "ram", 0, 4, &savevm_ram_handlers
, NULL
);
1220 int (*init_isa
) (ISABus
*bus
);
1221 int (*init_pci
) (PCIBus
*bus
);
1225 static struct soundhw soundhw
[9];
1226 static int soundhw_count
;
1228 void isa_register_soundhw(const char *name
, const char *descr
,
1229 int (*init_isa
)(ISABus
*bus
))
1231 assert(soundhw_count
< ARRAY_SIZE(soundhw
) - 1);
1232 soundhw
[soundhw_count
].name
= name
;
1233 soundhw
[soundhw_count
].descr
= descr
;
1234 soundhw
[soundhw_count
].isa
= 1;
1235 soundhw
[soundhw_count
].init
.init_isa
= init_isa
;
1239 void pci_register_soundhw(const char *name
, const char *descr
,
1240 int (*init_pci
)(PCIBus
*bus
))
1242 assert(soundhw_count
< ARRAY_SIZE(soundhw
) - 1);
1243 soundhw
[soundhw_count
].name
= name
;
1244 soundhw
[soundhw_count
].descr
= descr
;
1245 soundhw
[soundhw_count
].isa
= 0;
1246 soundhw
[soundhw_count
].init
.init_pci
= init_pci
;
1250 void select_soundhw(const char *optarg
)
1254 if (is_help_option(optarg
)) {
1257 if (soundhw_count
) {
1258 printf("Valid sound card names (comma separated):\n");
1259 for (c
= soundhw
; c
->name
; ++c
) {
1260 printf ("%-11s %s\n", c
->name
, c
->descr
);
1262 printf("\n-soundhw all will enable all of the above\n");
1264 printf("Machine has no user-selectable audio hardware "
1265 "(it may or may not have always-present audio hardware).\n");
1267 exit(!is_help_option(optarg
));
1275 if (!strcmp(optarg
, "all")) {
1276 for (c
= soundhw
; c
->name
; ++c
) {
1285 l
= !e
? strlen(p
) : (size_t) (e
- p
);
1287 for (c
= soundhw
; c
->name
; ++c
) {
1288 if (!strncmp(c
->name
, p
, l
) && !c
->name
[l
]) {
1296 error_report("Unknown sound card name (too big to show)");
1299 error_report("Unknown sound card name `%.*s'",
1304 p
+= l
+ (e
!= NULL
);
1308 goto show_valid_cards
;
1313 void audio_init(void)
1316 ISABus
*isa_bus
= (ISABus
*) object_resolve_path_type("", TYPE_ISA_BUS
, NULL
);
1317 PCIBus
*pci_bus
= (PCIBus
*) object_resolve_path_type("", TYPE_PCI_BUS
, NULL
);
1319 for (c
= soundhw
; c
->name
; ++c
) {
1323 error_report("ISA bus not available for %s", c
->name
);
1326 c
->init
.init_isa(isa_bus
);
1329 error_report("PCI bus not available for %s", c
->name
);
1332 c
->init
.init_pci(pci_bus
);
1338 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
1342 if (strlen(str
) != 36) {
1346 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
1347 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
1348 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14],
1357 void do_acpitable_option(const QemuOpts
*opts
)
1362 acpi_table_add(opts
, &err
);
1364 error_report("Wrong acpi table provided: %s",
1365 error_get_pretty(err
));
1372 void do_smbios_option(QemuOpts
*opts
)
1375 smbios_entry_add(opts
);
1379 void cpudef_init(void)
1381 #if defined(cpudef_setup)
1382 cpudef_setup(); /* parse cpu definitions in target config file */
1386 int kvm_available(void)
1395 int xen_available(void)
1405 TargetInfo
*qmp_query_target(Error
**errp
)
1407 TargetInfo
*info
= g_malloc0(sizeof(*info
));
1409 info
->arch
= g_strdup(TARGET_NAME
);
1414 /* Stub function that's gets run on the vcpu when its brought out of the
1415 VM to run inside qemu via async_run_on_cpu()*/
1416 static void mig_sleep_cpu(void *opq
)
1418 qemu_mutex_unlock_iothread();
1420 qemu_mutex_lock_iothread();
1423 /* To reduce the dirty rate explicitly disallow the VCPUs from spending
1424 much time in the VM. The migration thread will try to catchup.
1425 Workload will experience a performance drop.
1427 static void mig_throttle_guest_down(void)
1431 qemu_mutex_lock_iothread();
1433 async_run_on_cpu(cpu
, mig_sleep_cpu
, NULL
);
1435 qemu_mutex_unlock_iothread();
1438 static void check_guest_throttling(void)
1443 if (!mig_throttle_on
) {
1448 t0
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
1452 t1
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
1454 /* If it has been more than 40 ms since the last time the guest
1455 * was throttled then do it again.
1457 if (40 < (t1
-t0
)/1000000) {
1458 mig_throttle_guest_down();