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
29 #include <sys/types.h>
33 #include "monitor/monitor.h"
34 #include "sysemu/sysemu.h"
35 #include "qemu/bitops.h"
36 #include "qemu/bitmap.h"
37 #include "sysemu/arch_init.h"
38 #include "audio/audio.h"
39 #include "hw/i386/pc.h"
40 #include "hw/pci/pci.h"
41 #include "hw/audio/audio.h"
42 #include "sysemu/kvm.h"
43 #include "migration/migration.h"
44 #include "hw/i386/smbios.h"
45 #include "exec/address-spaces.h"
46 #include "hw/audio/pcspk.h"
47 #include "migration/page_cache.h"
48 #include "qemu/config-file.h"
49 #include "qemu/error-report.h"
50 #include "qmp-commands.h"
52 #include "exec/cpu-all.h"
53 #include "exec/ram_addr.h"
54 #include "hw/acpi/acpi.h"
55 #include "qemu/host-utils.h"
56 #include "qemu/rcu_queue.h"
58 #ifdef DEBUG_ARCH_INIT
59 #define DPRINTF(fmt, ...) \
60 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
62 #define DPRINTF(fmt, ...) \
67 int graphic_width
= 1024;
68 int graphic_height
= 768;
69 int graphic_depth
= 8;
71 int graphic_width
= 800;
72 int graphic_height
= 600;
73 int graphic_depth
= 32;
77 #if defined(TARGET_ALPHA)
78 #define QEMU_ARCH QEMU_ARCH_ALPHA
79 #elif defined(TARGET_ARM)
80 #define QEMU_ARCH QEMU_ARCH_ARM
81 #elif defined(TARGET_CRIS)
82 #define QEMU_ARCH QEMU_ARCH_CRIS
83 #elif defined(TARGET_I386)
84 #define QEMU_ARCH QEMU_ARCH_I386
85 #elif defined(TARGET_M68K)
86 #define QEMU_ARCH QEMU_ARCH_M68K
87 #elif defined(TARGET_LM32)
88 #define QEMU_ARCH QEMU_ARCH_LM32
89 #elif defined(TARGET_MICROBLAZE)
90 #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
91 #elif defined(TARGET_MIPS)
92 #define QEMU_ARCH QEMU_ARCH_MIPS
93 #elif defined(TARGET_MOXIE)
94 #define QEMU_ARCH QEMU_ARCH_MOXIE
95 #elif defined(TARGET_OPENRISC)
96 #define QEMU_ARCH QEMU_ARCH_OPENRISC
97 #elif defined(TARGET_PPC)
98 #define QEMU_ARCH QEMU_ARCH_PPC
99 #elif defined(TARGET_S390X)
100 #define QEMU_ARCH QEMU_ARCH_S390X
101 #elif defined(TARGET_SH4)
102 #define QEMU_ARCH QEMU_ARCH_SH4
103 #elif defined(TARGET_SPARC)
104 #define QEMU_ARCH QEMU_ARCH_SPARC
105 #elif defined(TARGET_XTENSA)
106 #define QEMU_ARCH QEMU_ARCH_XTENSA
107 #elif defined(TARGET_UNICORE32)
108 #define QEMU_ARCH QEMU_ARCH_UNICORE32
109 #elif defined(TARGET_TRICORE)
110 #define QEMU_ARCH QEMU_ARCH_TRICORE
113 const uint32_t arch_type
= QEMU_ARCH
;
114 static bool mig_throttle_on
;
115 static int dirty_rate_high_cnt
;
116 static void check_guest_throttling(void);
118 static uint64_t bitmap_sync_count
;
120 /***********************************************************/
121 /* ram save/restore */
123 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
124 #define RAM_SAVE_FLAG_COMPRESS 0x02
125 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
126 #define RAM_SAVE_FLAG_PAGE 0x08
127 #define RAM_SAVE_FLAG_EOS 0x10
128 #define RAM_SAVE_FLAG_CONTINUE 0x20
129 #define RAM_SAVE_FLAG_XBZRLE 0x40
130 /* 0x80 is reserved in migration.h start with 0x100 next */
131 #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100
133 static struct defconfig_file
{
134 const char *filename
;
135 /* Indicates it is an user config file (disabled by -no-user-config) */
137 } default_config_files
[] = {
138 { CONFIG_QEMU_CONFDIR
"/qemu.conf", true },
139 { CONFIG_QEMU_CONFDIR
"/target-" TARGET_NAME
".conf", true },
140 { NULL
}, /* end of list */
143 static const uint8_t ZERO_TARGET_PAGE
[TARGET_PAGE_SIZE
];
145 int qemu_read_default_config_files(bool userconfig
)
148 struct defconfig_file
*f
;
150 for (f
= default_config_files
; f
->filename
; f
++) {
151 if (!userconfig
&& f
->userconfig
) {
154 ret
= qemu_read_config_file(f
->filename
);
155 if (ret
< 0 && ret
!= -ENOENT
) {
163 static inline bool is_zero_range(uint8_t *p
, uint64_t size
)
165 return buffer_find_nonzero_offset(p
, size
) == size
;
168 /* struct contains XBZRLE cache and a static page
169 used by the compression */
171 /* buffer used for XBZRLE encoding */
172 uint8_t *encoded_buf
;
173 /* buffer for storing page content */
174 uint8_t *current_buf
;
175 /* Cache for XBZRLE, Protected by lock. */
180 /* buffer used for XBZRLE decoding */
181 static uint8_t *xbzrle_decoded_buf
;
183 static void XBZRLE_cache_lock(void)
185 if (migrate_use_xbzrle())
186 qemu_mutex_lock(&XBZRLE
.lock
);
189 static void XBZRLE_cache_unlock(void)
191 if (migrate_use_xbzrle())
192 qemu_mutex_unlock(&XBZRLE
.lock
);
196 * called from qmp_migrate_set_cache_size in main thread, possibly while
197 * a migration is in progress.
198 * A running migration maybe using the cache and might finish during this
199 * call, hence changes to the cache are protected by XBZRLE.lock().
201 int64_t xbzrle_cache_resize(int64_t new_size
)
203 PageCache
*new_cache
;
206 if (new_size
< TARGET_PAGE_SIZE
) {
212 if (XBZRLE
.cache
!= NULL
) {
213 if (pow2floor(new_size
) == migrate_xbzrle_cache_size()) {
216 new_cache
= cache_init(new_size
/ TARGET_PAGE_SIZE
,
219 error_report("Error creating cache");
224 cache_fini(XBZRLE
.cache
);
225 XBZRLE
.cache
= new_cache
;
229 ret
= pow2floor(new_size
);
231 XBZRLE_cache_unlock();
235 /* accounting for migration statistics */
236 typedef struct AccountingInfo
{
238 uint64_t skipped_pages
;
241 uint64_t xbzrle_bytes
;
242 uint64_t xbzrle_pages
;
243 uint64_t xbzrle_cache_miss
;
244 double xbzrle_cache_miss_rate
;
245 uint64_t xbzrle_overflows
;
248 static AccountingInfo acct_info
;
250 static void acct_clear(void)
252 memset(&acct_info
, 0, sizeof(acct_info
));
255 uint64_t dup_mig_bytes_transferred(void)
257 return acct_info
.dup_pages
* TARGET_PAGE_SIZE
;
260 uint64_t dup_mig_pages_transferred(void)
262 return acct_info
.dup_pages
;
265 uint64_t skipped_mig_bytes_transferred(void)
267 return acct_info
.skipped_pages
* TARGET_PAGE_SIZE
;
270 uint64_t skipped_mig_pages_transferred(void)
272 return acct_info
.skipped_pages
;
275 uint64_t norm_mig_bytes_transferred(void)
277 return acct_info
.norm_pages
* TARGET_PAGE_SIZE
;
280 uint64_t norm_mig_pages_transferred(void)
282 return acct_info
.norm_pages
;
285 uint64_t xbzrle_mig_bytes_transferred(void)
287 return acct_info
.xbzrle_bytes
;
290 uint64_t xbzrle_mig_pages_transferred(void)
292 return acct_info
.xbzrle_pages
;
295 uint64_t xbzrle_mig_pages_cache_miss(void)
297 return acct_info
.xbzrle_cache_miss
;
300 double xbzrle_mig_cache_miss_rate(void)
302 return acct_info
.xbzrle_cache_miss_rate
;
305 uint64_t xbzrle_mig_pages_overflow(void)
307 return acct_info
.xbzrle_overflows
;
310 /* This is the last block that we have visited serching for dirty pages
312 static RAMBlock
*last_seen_block
;
313 /* This is the last block from where we have sent data */
314 static RAMBlock
*last_sent_block
;
315 static ram_addr_t last_offset
;
316 static unsigned long *migration_bitmap
;
317 static uint64_t migration_dirty_pages
;
318 static uint32_t last_version
;
319 static bool ram_bulk_stage
;
321 struct CompressParam
{
330 typedef struct CompressParam CompressParam
;
332 struct DecompressParam
{
340 typedef struct DecompressParam DecompressParam
;
342 static CompressParam
*comp_param
;
343 static QemuThread
*compress_threads
;
344 /* comp_done_cond is used to wake up the migration thread when
345 * one of the compression threads has finished the compression.
346 * comp_done_lock is used to co-work with comp_done_cond.
348 static QemuMutex
*comp_done_lock
;
349 static QemuCond
*comp_done_cond
;
350 /* The empty QEMUFileOps will be used by file in CompressParam */
351 static const QEMUFileOps empty_ops
= { };
353 static bool compression_switch
;
354 static bool quit_comp_thread
;
355 static bool quit_decomp_thread
;
356 static DecompressParam
*decomp_param
;
357 static QemuThread
*decompress_threads
;
358 static uint8_t *compressed_data_buf
;
360 static int do_compress_ram_page(CompressParam
*param
);
362 static void *do_data_compress(void *opaque
)
364 CompressParam
*param
= opaque
;
366 while (!quit_comp_thread
) {
367 qemu_mutex_lock(¶m
->mutex
);
368 /* Re-check the quit_comp_thread in case of
369 * terminate_compression_threads is called just before
370 * qemu_mutex_lock(¶m->mutex) and after
371 * while(!quit_comp_thread), re-check it here can make
372 * sure the compression thread terminate as expected.
374 while (!param
->start
&& !quit_comp_thread
) {
375 qemu_cond_wait(¶m
->cond
, ¶m
->mutex
);
377 if (!quit_comp_thread
) {
378 do_compress_ram_page(param
);
380 param
->start
= false;
381 qemu_mutex_unlock(¶m
->mutex
);
383 qemu_mutex_lock(comp_done_lock
);
385 qemu_cond_signal(comp_done_cond
);
386 qemu_mutex_unlock(comp_done_lock
);
392 static inline void terminate_compression_threads(void)
394 int idx
, thread_count
;
396 thread_count
= migrate_compress_threads();
397 quit_comp_thread
= true;
398 for (idx
= 0; idx
< thread_count
; idx
++) {
399 qemu_mutex_lock(&comp_param
[idx
].mutex
);
400 qemu_cond_signal(&comp_param
[idx
].cond
);
401 qemu_mutex_unlock(&comp_param
[idx
].mutex
);
405 void migrate_compress_threads_join(void)
409 if (!migrate_use_compression()) {
412 terminate_compression_threads();
413 thread_count
= migrate_compress_threads();
414 for (i
= 0; i
< thread_count
; i
++) {
415 qemu_thread_join(compress_threads
+ i
);
416 qemu_fclose(comp_param
[i
].file
);
417 qemu_mutex_destroy(&comp_param
[i
].mutex
);
418 qemu_cond_destroy(&comp_param
[i
].cond
);
420 qemu_mutex_destroy(comp_done_lock
);
421 qemu_cond_destroy(comp_done_cond
);
422 g_free(compress_threads
);
424 g_free(comp_done_cond
);
425 g_free(comp_done_lock
);
426 compress_threads
= NULL
;
428 comp_done_cond
= NULL
;
429 comp_done_lock
= NULL
;
432 void migrate_compress_threads_create(void)
436 if (!migrate_use_compression()) {
439 quit_comp_thread
= false;
440 compression_switch
= true;
441 thread_count
= migrate_compress_threads();
442 compress_threads
= g_new0(QemuThread
, thread_count
);
443 comp_param
= g_new0(CompressParam
, thread_count
);
444 comp_done_cond
= g_new0(QemuCond
, 1);
445 comp_done_lock
= g_new0(QemuMutex
, 1);
446 qemu_cond_init(comp_done_cond
);
447 qemu_mutex_init(comp_done_lock
);
448 for (i
= 0; i
< thread_count
; i
++) {
449 /* com_param[i].file is just used as a dummy buffer to save data, set
452 comp_param
[i
].file
= qemu_fopen_ops(NULL
, &empty_ops
);
453 comp_param
[i
].done
= true;
454 qemu_mutex_init(&comp_param
[i
].mutex
);
455 qemu_cond_init(&comp_param
[i
].cond
);
456 qemu_thread_create(compress_threads
+ i
, "compress",
457 do_data_compress
, comp_param
+ i
,
458 QEMU_THREAD_JOINABLE
);
463 * save_page_header: Write page header to wire
465 * If this is the 1st block, it also writes the block identification
467 * Returns: Number of bytes written
469 * @f: QEMUFile where to send the data
470 * @block: block that contains the page we want to send
471 * @offset: offset inside the block for the page
472 * in the lower bits, it contains flags
474 static size_t save_page_header(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
)
478 qemu_put_be64(f
, offset
);
481 if (!(offset
& RAM_SAVE_FLAG_CONTINUE
)) {
482 qemu_put_byte(f
, strlen(block
->idstr
));
483 qemu_put_buffer(f
, (uint8_t *)block
->idstr
,
484 strlen(block
->idstr
));
485 size
+= 1 + strlen(block
->idstr
);
490 /* Update the xbzrle cache to reflect a page that's been sent as all 0.
491 * The important thing is that a stale (not-yet-0'd) page be replaced
493 * As a bonus, if the page wasn't in the cache it gets added so that
494 * when a small write is made into the 0'd page it gets XBZRLE sent
496 static void xbzrle_cache_zero_page(ram_addr_t current_addr
)
498 if (ram_bulk_stage
|| !migrate_use_xbzrle()) {
502 /* We don't care if this fails to allocate a new cache page
503 * as long as it updated an old one */
504 cache_insert(XBZRLE
.cache
, current_addr
, ZERO_TARGET_PAGE
,
508 #define ENCODING_FLAG_XBZRLE 0x1
511 * save_xbzrle_page: compress and send current page
513 * Returns: 1 means that we wrote the page
514 * 0 means that page is identical to the one already sent
515 * -1 means that xbzrle would be longer than normal
517 * @f: QEMUFile where to send the data
520 * @block: block that contains the page we want to send
521 * @offset: offset inside the block for the page
522 * @last_stage: if we are at the completion stage
523 * @bytes_transferred: increase it with the number of transferred bytes
525 static int save_xbzrle_page(QEMUFile
*f
, uint8_t **current_data
,
526 ram_addr_t current_addr
, RAMBlock
*block
,
527 ram_addr_t offset
, bool last_stage
,
528 uint64_t *bytes_transferred
)
530 int encoded_len
= 0, bytes_xbzrle
;
531 uint8_t *prev_cached_page
;
533 if (!cache_is_cached(XBZRLE
.cache
, current_addr
, bitmap_sync_count
)) {
534 acct_info
.xbzrle_cache_miss
++;
536 if (cache_insert(XBZRLE
.cache
, current_addr
, *current_data
,
537 bitmap_sync_count
) == -1) {
540 /* update *current_data when the page has been
541 inserted into cache */
542 *current_data
= get_cached_data(XBZRLE
.cache
, current_addr
);
548 prev_cached_page
= get_cached_data(XBZRLE
.cache
, current_addr
);
550 /* save current buffer into memory */
551 memcpy(XBZRLE
.current_buf
, *current_data
, TARGET_PAGE_SIZE
);
553 /* XBZRLE encoding (if there is no overflow) */
554 encoded_len
= xbzrle_encode_buffer(prev_cached_page
, XBZRLE
.current_buf
,
555 TARGET_PAGE_SIZE
, XBZRLE
.encoded_buf
,
557 if (encoded_len
== 0) {
558 DPRINTF("Skipping unmodified page\n");
560 } else if (encoded_len
== -1) {
561 DPRINTF("Overflow\n");
562 acct_info
.xbzrle_overflows
++;
563 /* update data in the cache */
565 memcpy(prev_cached_page
, *current_data
, TARGET_PAGE_SIZE
);
566 *current_data
= prev_cached_page
;
571 /* we need to update the data in the cache, in order to get the same data */
573 memcpy(prev_cached_page
, XBZRLE
.current_buf
, TARGET_PAGE_SIZE
);
576 /* Send XBZRLE based compressed page */
577 bytes_xbzrle
= save_page_header(f
, block
, offset
| RAM_SAVE_FLAG_XBZRLE
);
578 qemu_put_byte(f
, ENCODING_FLAG_XBZRLE
);
579 qemu_put_be16(f
, encoded_len
);
580 qemu_put_buffer(f
, XBZRLE
.encoded_buf
, encoded_len
);
581 bytes_xbzrle
+= encoded_len
+ 1 + 2;
582 acct_info
.xbzrle_pages
++;
583 acct_info
.xbzrle_bytes
+= bytes_xbzrle
;
584 *bytes_transferred
+= bytes_xbzrle
;
590 ram_addr_t
migration_bitmap_find_and_reset_dirty(MemoryRegion
*mr
,
593 unsigned long base
= mr
->ram_addr
>> TARGET_PAGE_BITS
;
594 unsigned long nr
= base
+ (start
>> TARGET_PAGE_BITS
);
595 uint64_t mr_size
= TARGET_PAGE_ALIGN(memory_region_size(mr
));
596 unsigned long size
= base
+ (mr_size
>> TARGET_PAGE_BITS
);
600 if (ram_bulk_stage
&& nr
> base
) {
603 next
= find_next_bit(migration_bitmap
, size
, nr
);
607 clear_bit(next
, migration_bitmap
);
608 migration_dirty_pages
--;
610 return (next
- base
) << TARGET_PAGE_BITS
;
613 static inline bool migration_bitmap_set_dirty(ram_addr_t addr
)
616 int nr
= addr
>> TARGET_PAGE_BITS
;
618 ret
= test_and_set_bit(nr
, migration_bitmap
);
621 migration_dirty_pages
++;
626 static void migration_bitmap_sync_range(ram_addr_t start
, ram_addr_t length
)
629 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
631 /* start address is aligned at the start of a word? */
632 if (((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) {
634 int nr
= BITS_TO_LONGS(length
>> TARGET_PAGE_BITS
);
635 unsigned long *src
= ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
];
637 for (k
= page
; k
< page
+ nr
; k
++) {
639 unsigned long new_dirty
;
640 new_dirty
= ~migration_bitmap
[k
];
641 migration_bitmap
[k
] |= src
[k
];
643 migration_dirty_pages
+= ctpopl(new_dirty
);
648 for (addr
= 0; addr
< length
; addr
+= TARGET_PAGE_SIZE
) {
649 if (cpu_physical_memory_get_dirty(start
+ addr
,
651 DIRTY_MEMORY_MIGRATION
)) {
652 cpu_physical_memory_reset_dirty(start
+ addr
,
654 DIRTY_MEMORY_MIGRATION
);
655 migration_bitmap_set_dirty(start
+ addr
);
662 /* Fix me: there are too many global variables used in migration process. */
663 static int64_t start_time
;
664 static int64_t bytes_xfer_prev
;
665 static int64_t num_dirty_pages_period
;
667 static void migration_bitmap_sync_init(void)
671 num_dirty_pages_period
= 0;
674 /* Called with iothread lock held, to protect ram_list.dirty_memory[] */
675 static void migration_bitmap_sync(void)
678 uint64_t num_dirty_pages_init
= migration_dirty_pages
;
679 MigrationState
*s
= migrate_get_current();
681 int64_t bytes_xfer_now
;
682 static uint64_t xbzrle_cache_miss_prev
;
683 static uint64_t iterations_prev
;
687 if (!bytes_xfer_prev
) {
688 bytes_xfer_prev
= ram_bytes_transferred();
692 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
695 trace_migration_bitmap_sync_start();
696 address_space_sync_dirty_bitmap(&address_space_memory
);
699 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
700 migration_bitmap_sync_range(block
->mr
->ram_addr
, block
->used_length
);
704 trace_migration_bitmap_sync_end(migration_dirty_pages
705 - num_dirty_pages_init
);
706 num_dirty_pages_period
+= migration_dirty_pages
- num_dirty_pages_init
;
707 end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
709 /* more than 1 second = 1000 millisecons */
710 if (end_time
> start_time
+ 1000) {
711 if (migrate_auto_converge()) {
712 /* The following detection logic can be refined later. For now:
713 Check to see if the dirtied bytes is 50% more than the approx.
714 amount of bytes that just got transferred since the last time we
715 were in this routine. If that happens >N times (for now N==4)
716 we turn on the throttle down logic */
717 bytes_xfer_now
= ram_bytes_transferred();
718 if (s
->dirty_pages_rate
&&
719 (num_dirty_pages_period
* TARGET_PAGE_SIZE
>
720 (bytes_xfer_now
- bytes_xfer_prev
)/2) &&
721 (dirty_rate_high_cnt
++ > 4)) {
722 trace_migration_throttle();
723 mig_throttle_on
= true;
724 dirty_rate_high_cnt
= 0;
726 bytes_xfer_prev
= bytes_xfer_now
;
728 mig_throttle_on
= false;
730 if (migrate_use_xbzrle()) {
731 if (iterations_prev
!= 0) {
732 acct_info
.xbzrle_cache_miss_rate
=
733 (double)(acct_info
.xbzrle_cache_miss
-
734 xbzrle_cache_miss_prev
) /
735 (acct_info
.iterations
- iterations_prev
);
737 iterations_prev
= acct_info
.iterations
;
738 xbzrle_cache_miss_prev
= acct_info
.xbzrle_cache_miss
;
740 s
->dirty_pages_rate
= num_dirty_pages_period
* 1000
741 / (end_time
- start_time
);
742 s
->dirty_bytes_rate
= s
->dirty_pages_rate
* TARGET_PAGE_SIZE
;
743 start_time
= end_time
;
744 num_dirty_pages_period
= 0;
745 s
->dirty_sync_count
= bitmap_sync_count
;
750 * save_zero_page: Send the zero page to the stream
752 * Returns: Number of pages written.
754 * @f: QEMUFile where to send the data
755 * @block: block that contains the page we want to send
756 * @offset: offset inside the block for the page
757 * @p: pointer to the page
758 * @bytes_transferred: increase it with the number of transferred bytes
760 static int save_zero_page(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
,
761 uint8_t *p
, uint64_t *bytes_transferred
)
765 if (is_zero_range(p
, TARGET_PAGE_SIZE
)) {
766 acct_info
.dup_pages
++;
767 *bytes_transferred
+= save_page_header(f
, block
,
768 offset
| RAM_SAVE_FLAG_COMPRESS
);
770 *bytes_transferred
+= 1;
778 * ram_save_page: Send the given page to the stream
780 * Returns: Number of pages written.
782 * @f: QEMUFile where to send the data
783 * @block: block that contains the page we want to send
784 * @offset: offset inside the block for the page
785 * @last_stage: if we are at the completion stage
786 * @bytes_transferred: increase it with the number of transferred bytes
788 static int ram_save_page(QEMUFile
*f
, RAMBlock
* block
, ram_addr_t offset
,
789 bool last_stage
, uint64_t *bytes_transferred
)
793 ram_addr_t current_addr
;
794 MemoryRegion
*mr
= block
->mr
;
797 bool send_async
= true;
799 p
= memory_region_get_ram_ptr(mr
) + offset
;
801 /* In doubt sent page as normal */
803 ret
= ram_control_save_page(f
, block
->offset
,
804 offset
, TARGET_PAGE_SIZE
, &bytes_xmit
);
806 *bytes_transferred
+= bytes_xmit
;
812 current_addr
= block
->offset
+ offset
;
814 if (block
== last_sent_block
) {
815 offset
|= RAM_SAVE_FLAG_CONTINUE
;
817 if (ret
!= RAM_SAVE_CONTROL_NOT_SUPP
) {
818 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
819 if (bytes_xmit
> 0) {
820 acct_info
.norm_pages
++;
821 } else if (bytes_xmit
== 0) {
822 acct_info
.dup_pages
++;
826 pages
= save_zero_page(f
, block
, offset
, p
, bytes_transferred
);
828 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
829 * page would be stale
831 xbzrle_cache_zero_page(current_addr
);
832 } else if (!ram_bulk_stage
&& migrate_use_xbzrle()) {
833 pages
= save_xbzrle_page(f
, &p
, current_addr
, block
,
834 offset
, last_stage
, bytes_transferred
);
836 /* Can't send this cached data async, since the cache page
837 * might get updated before it gets to the wire
844 /* XBZRLE overflow or normal page */
846 *bytes_transferred
+= save_page_header(f
, block
,
847 offset
| RAM_SAVE_FLAG_PAGE
);
849 qemu_put_buffer_async(f
, p
, TARGET_PAGE_SIZE
);
851 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
853 *bytes_transferred
+= TARGET_PAGE_SIZE
;
855 acct_info
.norm_pages
++;
858 XBZRLE_cache_unlock();
863 static int do_compress_ram_page(CompressParam
*param
)
865 int bytes_sent
, blen
;
867 RAMBlock
*block
= param
->block
;
868 ram_addr_t offset
= param
->offset
;
870 p
= memory_region_get_ram_ptr(block
->mr
) + (offset
& TARGET_PAGE_MASK
);
872 bytes_sent
= save_page_header(param
->file
, block
, offset
|
873 RAM_SAVE_FLAG_COMPRESS_PAGE
);
874 blen
= qemu_put_compression_data(param
->file
, p
, TARGET_PAGE_SIZE
,
875 migrate_compress_level());
881 static inline void start_compression(CompressParam
*param
)
884 qemu_mutex_lock(¶m
->mutex
);
886 qemu_cond_signal(¶m
->cond
);
887 qemu_mutex_unlock(¶m
->mutex
);
890 static inline void start_decompression(DecompressParam
*param
)
892 qemu_mutex_lock(¶m
->mutex
);
894 qemu_cond_signal(¶m
->cond
);
895 qemu_mutex_unlock(¶m
->mutex
);
898 static uint64_t bytes_transferred
;
900 static void flush_compressed_data(QEMUFile
*f
)
902 int idx
, len
, thread_count
;
904 if (!migrate_use_compression()) {
907 thread_count
= migrate_compress_threads();
908 for (idx
= 0; idx
< thread_count
; idx
++) {
909 if (!comp_param
[idx
].done
) {
910 qemu_mutex_lock(comp_done_lock
);
911 while (!comp_param
[idx
].done
&& !quit_comp_thread
) {
912 qemu_cond_wait(comp_done_cond
, comp_done_lock
);
914 qemu_mutex_unlock(comp_done_lock
);
916 if (!quit_comp_thread
) {
917 len
= qemu_put_qemu_file(f
, comp_param
[idx
].file
);
918 bytes_transferred
+= len
;
923 static inline void set_compress_params(CompressParam
*param
, RAMBlock
*block
,
926 param
->block
= block
;
927 param
->offset
= offset
;
930 static int compress_page_with_multi_thread(QEMUFile
*f
, RAMBlock
*block
,
932 uint64_t *bytes_transferred
)
934 int idx
, thread_count
, bytes_xmit
= -1, pages
= -1;
936 thread_count
= migrate_compress_threads();
937 qemu_mutex_lock(comp_done_lock
);
939 for (idx
= 0; idx
< thread_count
; idx
++) {
940 if (comp_param
[idx
].done
) {
941 bytes_xmit
= qemu_put_qemu_file(f
, comp_param
[idx
].file
);
942 set_compress_params(&comp_param
[idx
], block
, offset
);
943 start_compression(&comp_param
[idx
]);
945 acct_info
.norm_pages
++;
946 *bytes_transferred
+= bytes_xmit
;
953 qemu_cond_wait(comp_done_cond
, comp_done_lock
);
956 qemu_mutex_unlock(comp_done_lock
);
962 * ram_save_compressed_page: compress the given page and send it to the stream
964 * Returns: Number of pages written.
966 * @f: QEMUFile where to send the data
967 * @block: block that contains the page we want to send
968 * @offset: offset inside the block for the page
969 * @last_stage: if we are at the completion stage
970 * @bytes_transferred: increase it with the number of transferred bytes
972 static int ram_save_compressed_page(QEMUFile
*f
, RAMBlock
*block
,
973 ram_addr_t offset
, bool last_stage
,
974 uint64_t *bytes_transferred
)
978 MemoryRegion
*mr
= block
->mr
;
982 p
= memory_region_get_ram_ptr(mr
) + offset
;
985 ret
= ram_control_save_page(f
, block
->offset
,
986 offset
, TARGET_PAGE_SIZE
, &bytes_xmit
);
988 *bytes_transferred
+= bytes_xmit
;
991 if (block
== last_sent_block
) {
992 offset
|= RAM_SAVE_FLAG_CONTINUE
;
994 if (ret
!= RAM_SAVE_CONTROL_NOT_SUPP
) {
995 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
996 if (bytes_xmit
> 0) {
997 acct_info
.norm_pages
++;
998 } else if (bytes_xmit
== 0) {
999 acct_info
.dup_pages
++;
1003 /* When starting the process of a new block, the first page of
1004 * the block should be sent out before other pages in the same
1005 * block, and all the pages in last block should have been sent
1006 * out, keeping this order is important, because the 'cont' flag
1007 * is used to avoid resending the block name.
1009 if (block
!= last_sent_block
) {
1010 flush_compressed_data(f
);
1011 pages
= save_zero_page(f
, block
, offset
, p
, bytes_transferred
);
1013 set_compress_params(&comp_param
[0], block
, offset
);
1014 /* Use the qemu thread to compress the data to make sure the
1015 * first page is sent out before other pages
1017 bytes_xmit
= do_compress_ram_page(&comp_param
[0]);
1018 acct_info
.norm_pages
++;
1019 qemu_put_qemu_file(f
, comp_param
[0].file
);
1020 *bytes_transferred
+= bytes_xmit
;
1024 pages
= save_zero_page(f
, block
, offset
, p
, bytes_transferred
);
1026 pages
= compress_page_with_multi_thread(f
, block
, offset
,
1036 * ram_find_and_save_block: Finds a dirty page and sends it to f
1038 * Called within an RCU critical section.
1040 * Returns: The number of pages written
1041 * 0 means no dirty pages
1043 * @f: QEMUFile where to send the data
1044 * @last_stage: if we are at the completion stage
1045 * @bytes_transferred: increase it with the number of transferred bytes
1048 static int ram_find_and_save_block(QEMUFile
*f
, bool last_stage
,
1049 uint64_t *bytes_transferred
)
1051 RAMBlock
*block
= last_seen_block
;
1052 ram_addr_t offset
= last_offset
;
1053 bool complete_round
= false;
1058 block
= QLIST_FIRST_RCU(&ram_list
.blocks
);
1062 offset
= migration_bitmap_find_and_reset_dirty(mr
, offset
);
1063 if (complete_round
&& block
== last_seen_block
&&
1064 offset
>= last_offset
) {
1067 if (offset
>= block
->used_length
) {
1069 block
= QLIST_NEXT_RCU(block
, next
);
1071 block
= QLIST_FIRST_RCU(&ram_list
.blocks
);
1072 complete_round
= true;
1073 ram_bulk_stage
= false;
1074 if (migrate_use_xbzrle()) {
1075 /* If xbzrle is on, stop using the data compression at this
1076 * point. In theory, xbzrle can do better than compression.
1078 flush_compressed_data(f
);
1079 compression_switch
= false;
1083 if (compression_switch
&& migrate_use_compression()) {
1084 pages
= ram_save_compressed_page(f
, block
, offset
, last_stage
,
1087 pages
= ram_save_page(f
, block
, offset
, last_stage
,
1091 /* if page is unmodified, continue to the next */
1093 last_sent_block
= block
;
1099 last_seen_block
= block
;
1100 last_offset
= offset
;
1105 void acct_update_position(QEMUFile
*f
, size_t size
, bool zero
)
1107 uint64_t pages
= size
/ TARGET_PAGE_SIZE
;
1109 acct_info
.dup_pages
+= pages
;
1111 acct_info
.norm_pages
+= pages
;
1112 bytes_transferred
+= size
;
1113 qemu_update_position(f
, size
);
1117 static ram_addr_t
ram_save_remaining(void)
1119 return migration_dirty_pages
;
1122 uint64_t ram_bytes_remaining(void)
1124 return ram_save_remaining() * TARGET_PAGE_SIZE
;
1127 uint64_t ram_bytes_transferred(void)
1129 return bytes_transferred
;
1132 uint64_t ram_bytes_total(void)
1138 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
)
1139 total
+= block
->used_length
;
1144 void free_xbzrle_decoded_buf(void)
1146 g_free(xbzrle_decoded_buf
);
1147 xbzrle_decoded_buf
= NULL
;
1150 static void migration_end(void)
1152 if (migration_bitmap
) {
1153 memory_global_dirty_log_stop();
1154 g_free(migration_bitmap
);
1155 migration_bitmap
= NULL
;
1158 XBZRLE_cache_lock();
1160 cache_fini(XBZRLE
.cache
);
1161 g_free(XBZRLE
.encoded_buf
);
1162 g_free(XBZRLE
.current_buf
);
1163 XBZRLE
.cache
= NULL
;
1164 XBZRLE
.encoded_buf
= NULL
;
1165 XBZRLE
.current_buf
= NULL
;
1167 XBZRLE_cache_unlock();
1170 static void ram_migration_cancel(void *opaque
)
1175 static void reset_ram_globals(void)
1177 last_seen_block
= NULL
;
1178 last_sent_block
= NULL
;
1180 last_version
= ram_list
.version
;
1181 ram_bulk_stage
= true;
1184 #define MAX_WAIT 50 /* ms, half buffered_file limit */
1187 /* Each of ram_save_setup, ram_save_iterate and ram_save_complete has
1188 * long-running RCU critical section. When rcu-reclaims in the code
1189 * start to become numerous it will be necessary to reduce the
1190 * granularity of these critical sections.
1193 static int ram_save_setup(QEMUFile
*f
, void *opaque
)
1196 int64_t ram_bitmap_pages
; /* Size of bitmap in pages, including gaps */
1198 mig_throttle_on
= false;
1199 dirty_rate_high_cnt
= 0;
1200 bitmap_sync_count
= 0;
1201 migration_bitmap_sync_init();
1203 if (migrate_use_xbzrle()) {
1204 XBZRLE_cache_lock();
1205 XBZRLE
.cache
= cache_init(migrate_xbzrle_cache_size() /
1208 if (!XBZRLE
.cache
) {
1209 XBZRLE_cache_unlock();
1210 error_report("Error creating cache");
1213 XBZRLE_cache_unlock();
1215 /* We prefer not to abort if there is no memory */
1216 XBZRLE
.encoded_buf
= g_try_malloc0(TARGET_PAGE_SIZE
);
1217 if (!XBZRLE
.encoded_buf
) {
1218 error_report("Error allocating encoded_buf");
1222 XBZRLE
.current_buf
= g_try_malloc(TARGET_PAGE_SIZE
);
1223 if (!XBZRLE
.current_buf
) {
1224 error_report("Error allocating current_buf");
1225 g_free(XBZRLE
.encoded_buf
);
1226 XBZRLE
.encoded_buf
= NULL
;
1233 /* iothread lock needed for ram_list.dirty_memory[] */
1234 qemu_mutex_lock_iothread();
1235 qemu_mutex_lock_ramlist();
1237 bytes_transferred
= 0;
1238 reset_ram_globals();
1240 ram_bitmap_pages
= last_ram_offset() >> TARGET_PAGE_BITS
;
1241 migration_bitmap
= bitmap_new(ram_bitmap_pages
);
1242 bitmap_set(migration_bitmap
, 0, ram_bitmap_pages
);
1245 * Count the total number of pages used by ram blocks not including any
1246 * gaps due to alignment or unplugs.
1248 migration_dirty_pages
= ram_bytes_total() >> TARGET_PAGE_BITS
;
1250 memory_global_dirty_log_start();
1251 migration_bitmap_sync();
1252 qemu_mutex_unlock_ramlist();
1253 qemu_mutex_unlock_iothread();
1255 qemu_put_be64(f
, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE
);
1257 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1258 qemu_put_byte(f
, strlen(block
->idstr
));
1259 qemu_put_buffer(f
, (uint8_t *)block
->idstr
, strlen(block
->idstr
));
1260 qemu_put_be64(f
, block
->used_length
);
1265 ram_control_before_iterate(f
, RAM_CONTROL_SETUP
);
1266 ram_control_after_iterate(f
, RAM_CONTROL_SETUP
);
1268 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1273 static int ram_save_iterate(QEMUFile
*f
, void *opaque
)
1281 if (ram_list
.version
!= last_version
) {
1282 reset_ram_globals();
1285 /* Read version before ram_list.blocks */
1288 ram_control_before_iterate(f
, RAM_CONTROL_ROUND
);
1290 t0
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
1292 while ((ret
= qemu_file_rate_limit(f
)) == 0) {
1295 pages
= ram_find_and_save_block(f
, false, &bytes_transferred
);
1296 /* no more pages to sent */
1300 pages_sent
+= pages
;
1301 acct_info
.iterations
++;
1302 check_guest_throttling();
1303 /* we want to check in the 1st loop, just in case it was the 1st time
1304 and we had to sync the dirty bitmap.
1305 qemu_get_clock_ns() is a bit expensive, so we only check each some
1308 if ((i
& 63) == 0) {
1309 uint64_t t1
= (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - t0
) / 1000000;
1310 if (t1
> MAX_WAIT
) {
1311 DPRINTF("big wait: %" PRIu64
" milliseconds, %d iterations\n",
1318 flush_compressed_data(f
);
1322 * Must occur before EOS (or any QEMUFile operation)
1323 * because of RDMA protocol.
1325 ram_control_after_iterate(f
, RAM_CONTROL_ROUND
);
1327 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1328 bytes_transferred
+= 8;
1330 ret
= qemu_file_get_error(f
);
1338 /* Called with iothread lock */
1339 static int ram_save_complete(QEMUFile
*f
, void *opaque
)
1343 migration_bitmap_sync();
1345 ram_control_before_iterate(f
, RAM_CONTROL_FINISH
);
1347 /* try transferring iterative blocks of memory */
1349 /* flush all remaining blocks regardless of rate limiting */
1353 pages
= ram_find_and_save_block(f
, true, &bytes_transferred
);
1354 /* no more blocks to sent */
1360 flush_compressed_data(f
);
1361 ram_control_after_iterate(f
, RAM_CONTROL_FINISH
);
1365 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1370 static uint64_t ram_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
)
1372 uint64_t remaining_size
;
1374 remaining_size
= ram_save_remaining() * TARGET_PAGE_SIZE
;
1376 if (remaining_size
< max_size
) {
1377 qemu_mutex_lock_iothread();
1379 migration_bitmap_sync();
1381 qemu_mutex_unlock_iothread();
1382 remaining_size
= ram_save_remaining() * TARGET_PAGE_SIZE
;
1384 return remaining_size
;
1387 static int load_xbzrle(QEMUFile
*f
, ram_addr_t addr
, void *host
)
1389 unsigned int xh_len
;
1392 if (!xbzrle_decoded_buf
) {
1393 xbzrle_decoded_buf
= g_malloc(TARGET_PAGE_SIZE
);
1396 /* extract RLE header */
1397 xh_flags
= qemu_get_byte(f
);
1398 xh_len
= qemu_get_be16(f
);
1400 if (xh_flags
!= ENCODING_FLAG_XBZRLE
) {
1401 error_report("Failed to load XBZRLE page - wrong compression!");
1405 if (xh_len
> TARGET_PAGE_SIZE
) {
1406 error_report("Failed to load XBZRLE page - len overflow!");
1409 /* load data and decode */
1410 qemu_get_buffer(f
, xbzrle_decoded_buf
, xh_len
);
1413 if (xbzrle_decode_buffer(xbzrle_decoded_buf
, xh_len
, host
,
1414 TARGET_PAGE_SIZE
) == -1) {
1415 error_report("Failed to load XBZRLE page - decode error!");
1422 /* Must be called from within a rcu critical section.
1423 * Returns a pointer from within the RCU-protected ram_list.
1425 static inline void *host_from_stream_offset(QEMUFile
*f
,
1429 static RAMBlock
*block
= NULL
;
1433 if (flags
& RAM_SAVE_FLAG_CONTINUE
) {
1434 if (!block
|| block
->max_length
<= offset
) {
1435 error_report("Ack, bad migration stream!");
1439 return memory_region_get_ram_ptr(block
->mr
) + offset
;
1442 len
= qemu_get_byte(f
);
1443 qemu_get_buffer(f
, (uint8_t *)id
, len
);
1446 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1447 if (!strncmp(id
, block
->idstr
, sizeof(id
)) &&
1448 block
->max_length
> offset
) {
1449 return memory_region_get_ram_ptr(block
->mr
) + offset
;
1453 error_report("Can't find block %s!", id
);
1458 * If a page (or a whole RDMA chunk) has been
1459 * determined to be zero, then zap it.
1461 void ram_handle_compressed(void *host
, uint8_t ch
, uint64_t size
)
1463 if (ch
!= 0 || !is_zero_range(host
, size
)) {
1464 memset(host
, ch
, size
);
1468 static void *do_data_decompress(void *opaque
)
1470 DecompressParam
*param
= opaque
;
1471 unsigned long pagesize
;
1473 while (!quit_decomp_thread
) {
1474 qemu_mutex_lock(¶m
->mutex
);
1475 while (!param
->start
&& !quit_decomp_thread
) {
1476 qemu_cond_wait(¶m
->cond
, ¶m
->mutex
);
1477 pagesize
= TARGET_PAGE_SIZE
;
1478 if (!quit_decomp_thread
) {
1479 /* uncompress() will return failed in some case, especially
1480 * when the page is dirted when doing the compression, it's
1481 * not a problem because the dirty page will be retransferred
1482 * and uncompress() won't break the data in other pages.
1484 uncompress((Bytef
*)param
->des
, &pagesize
,
1485 (const Bytef
*)param
->compbuf
, param
->len
);
1487 param
->start
= false;
1489 qemu_mutex_unlock(¶m
->mutex
);
1495 void migrate_decompress_threads_create(void)
1497 int i
, thread_count
;
1499 thread_count
= migrate_decompress_threads();
1500 decompress_threads
= g_new0(QemuThread
, thread_count
);
1501 decomp_param
= g_new0(DecompressParam
, thread_count
);
1502 compressed_data_buf
= g_malloc0(compressBound(TARGET_PAGE_SIZE
));
1503 quit_decomp_thread
= false;
1504 for (i
= 0; i
< thread_count
; i
++) {
1505 qemu_mutex_init(&decomp_param
[i
].mutex
);
1506 qemu_cond_init(&decomp_param
[i
].cond
);
1507 decomp_param
[i
].compbuf
= g_malloc0(compressBound(TARGET_PAGE_SIZE
));
1508 qemu_thread_create(decompress_threads
+ i
, "decompress",
1509 do_data_decompress
, decomp_param
+ i
,
1510 QEMU_THREAD_JOINABLE
);
1514 void migrate_decompress_threads_join(void)
1516 int i
, thread_count
;
1518 quit_decomp_thread
= true;
1519 thread_count
= migrate_decompress_threads();
1520 for (i
= 0; i
< thread_count
; i
++) {
1521 qemu_mutex_lock(&decomp_param
[i
].mutex
);
1522 qemu_cond_signal(&decomp_param
[i
].cond
);
1523 qemu_mutex_unlock(&decomp_param
[i
].mutex
);
1525 for (i
= 0; i
< thread_count
; i
++) {
1526 qemu_thread_join(decompress_threads
+ i
);
1527 qemu_mutex_destroy(&decomp_param
[i
].mutex
);
1528 qemu_cond_destroy(&decomp_param
[i
].cond
);
1529 g_free(decomp_param
[i
].compbuf
);
1531 g_free(decompress_threads
);
1532 g_free(decomp_param
);
1533 g_free(compressed_data_buf
);
1534 decompress_threads
= NULL
;
1535 decomp_param
= NULL
;
1536 compressed_data_buf
= NULL
;
1539 static void decompress_data_with_multi_threads(uint8_t *compbuf
,
1540 void *host
, int len
)
1542 int idx
, thread_count
;
1544 thread_count
= migrate_decompress_threads();
1546 for (idx
= 0; idx
< thread_count
; idx
++) {
1547 if (!decomp_param
[idx
].start
) {
1548 memcpy(decomp_param
[idx
].compbuf
, compbuf
, len
);
1549 decomp_param
[idx
].des
= host
;
1550 decomp_param
[idx
].len
= len
;
1551 start_decompression(&decomp_param
[idx
]);
1555 if (idx
< thread_count
) {
1561 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1563 int flags
= 0, ret
= 0;
1564 static uint64_t seq_iter
;
1569 if (version_id
!= 4) {
1573 /* This RCU critical section can be very long running.
1574 * When RCU reclaims in the code start to become numerous,
1575 * it will be necessary to reduce the granularity of this
1579 while (!ret
&& !(flags
& RAM_SAVE_FLAG_EOS
)) {
1580 ram_addr_t addr
, total_ram_bytes
;
1584 addr
= qemu_get_be64(f
);
1585 flags
= addr
& ~TARGET_PAGE_MASK
;
1586 addr
&= TARGET_PAGE_MASK
;
1588 switch (flags
& ~RAM_SAVE_FLAG_CONTINUE
) {
1589 case RAM_SAVE_FLAG_MEM_SIZE
:
1590 /* Synchronize RAM block list */
1591 total_ram_bytes
= addr
;
1592 while (!ret
&& total_ram_bytes
) {
1598 len
= qemu_get_byte(f
);
1599 qemu_get_buffer(f
, (uint8_t *)id
, len
);
1601 length
= qemu_get_be64(f
);
1603 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1604 if (!strncmp(id
, block
->idstr
, sizeof(id
))) {
1605 if (length
!= block
->used_length
) {
1606 Error
*local_err
= NULL
;
1608 ret
= qemu_ram_resize(block
->offset
, length
, &local_err
);
1610 error_report_err(local_err
);
1618 error_report("Unknown ramblock \"%s\", cannot "
1619 "accept migration", id
);
1623 total_ram_bytes
-= length
;
1626 case RAM_SAVE_FLAG_COMPRESS
:
1627 host
= host_from_stream_offset(f
, addr
, flags
);
1629 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
1633 ch
= qemu_get_byte(f
);
1634 ram_handle_compressed(host
, ch
, TARGET_PAGE_SIZE
);
1636 case RAM_SAVE_FLAG_PAGE
:
1637 host
= host_from_stream_offset(f
, addr
, flags
);
1639 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
1643 qemu_get_buffer(f
, host
, TARGET_PAGE_SIZE
);
1645 case RAM_SAVE_FLAG_COMPRESS_PAGE
:
1646 host
= host_from_stream_offset(f
, addr
, flags
);
1648 error_report("Invalid RAM offset " RAM_ADDR_FMT
, addr
);
1653 len
= qemu_get_be32(f
);
1654 if (len
< 0 || len
> compressBound(TARGET_PAGE_SIZE
)) {
1655 error_report("Invalid compressed data length: %d", len
);
1659 qemu_get_buffer(f
, compressed_data_buf
, len
);
1660 decompress_data_with_multi_threads(compressed_data_buf
, host
, len
);
1662 case RAM_SAVE_FLAG_XBZRLE
:
1663 host
= host_from_stream_offset(f
, addr
, flags
);
1665 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
1669 if (load_xbzrle(f
, addr
, host
) < 0) {
1670 error_report("Failed to decompress XBZRLE page at "
1671 RAM_ADDR_FMT
, addr
);
1676 case RAM_SAVE_FLAG_EOS
:
1680 if (flags
& RAM_SAVE_FLAG_HOOK
) {
1681 ram_control_load_hook(f
, flags
);
1683 error_report("Unknown combination of migration flags: %#x",
1689 ret
= qemu_file_get_error(f
);
1694 DPRINTF("Completed load of VM with exit code %d seq iteration "
1695 "%" PRIu64
"\n", ret
, seq_iter
);
1699 static SaveVMHandlers savevm_ram_handlers
= {
1700 .save_live_setup
= ram_save_setup
,
1701 .save_live_iterate
= ram_save_iterate
,
1702 .save_live_complete
= ram_save_complete
,
1703 .save_live_pending
= ram_save_pending
,
1704 .load_state
= ram_load
,
1705 .cancel
= ram_migration_cancel
,
1708 void ram_mig_init(void)
1710 qemu_mutex_init(&XBZRLE
.lock
);
1711 register_savevm_live(NULL
, "ram", 0, 4, &savevm_ram_handlers
, NULL
);
1720 int (*init_isa
) (ISABus
*bus
);
1721 int (*init_pci
) (PCIBus
*bus
);
1725 static struct soundhw soundhw
[9];
1726 static int soundhw_count
;
1728 void isa_register_soundhw(const char *name
, const char *descr
,
1729 int (*init_isa
)(ISABus
*bus
))
1731 assert(soundhw_count
< ARRAY_SIZE(soundhw
) - 1);
1732 soundhw
[soundhw_count
].name
= name
;
1733 soundhw
[soundhw_count
].descr
= descr
;
1734 soundhw
[soundhw_count
].isa
= 1;
1735 soundhw
[soundhw_count
].init
.init_isa
= init_isa
;
1739 void pci_register_soundhw(const char *name
, const char *descr
,
1740 int (*init_pci
)(PCIBus
*bus
))
1742 assert(soundhw_count
< ARRAY_SIZE(soundhw
) - 1);
1743 soundhw
[soundhw_count
].name
= name
;
1744 soundhw
[soundhw_count
].descr
= descr
;
1745 soundhw
[soundhw_count
].isa
= 0;
1746 soundhw
[soundhw_count
].init
.init_pci
= init_pci
;
1750 void select_soundhw(const char *optarg
)
1754 if (is_help_option(optarg
)) {
1757 if (soundhw_count
) {
1758 printf("Valid sound card names (comma separated):\n");
1759 for (c
= soundhw
; c
->name
; ++c
) {
1760 printf ("%-11s %s\n", c
->name
, c
->descr
);
1762 printf("\n-soundhw all will enable all of the above\n");
1764 printf("Machine has no user-selectable audio hardware "
1765 "(it may or may not have always-present audio hardware).\n");
1767 exit(!is_help_option(optarg
));
1775 if (!strcmp(optarg
, "all")) {
1776 for (c
= soundhw
; c
->name
; ++c
) {
1785 l
= !e
? strlen(p
) : (size_t) (e
- p
);
1787 for (c
= soundhw
; c
->name
; ++c
) {
1788 if (!strncmp(c
->name
, p
, l
) && !c
->name
[l
]) {
1796 error_report("Unknown sound card name (too big to show)");
1799 error_report("Unknown sound card name `%.*s'",
1804 p
+= l
+ (e
!= NULL
);
1808 goto show_valid_cards
;
1813 void audio_init(void)
1816 ISABus
*isa_bus
= (ISABus
*) object_resolve_path_type("", TYPE_ISA_BUS
, NULL
);
1817 PCIBus
*pci_bus
= (PCIBus
*) object_resolve_path_type("", TYPE_PCI_BUS
, NULL
);
1819 for (c
= soundhw
; c
->name
; ++c
) {
1823 error_report("ISA bus not available for %s", c
->name
);
1826 c
->init
.init_isa(isa_bus
);
1829 error_report("PCI bus not available for %s", c
->name
);
1832 c
->init
.init_pci(pci_bus
);
1838 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
1842 if (strlen(str
) != 36) {
1846 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
1847 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
1848 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14],
1857 void do_acpitable_option(const QemuOpts
*opts
)
1862 acpi_table_add(opts
, &err
);
1864 error_report("Wrong acpi table provided: %s",
1865 error_get_pretty(err
));
1872 void do_smbios_option(QemuOpts
*opts
)
1875 smbios_entry_add(opts
);
1879 void cpudef_init(void)
1881 #if defined(cpudef_setup)
1882 cpudef_setup(); /* parse cpu definitions in target config file */
1886 int kvm_available(void)
1895 int xen_available(void)
1905 TargetInfo
*qmp_query_target(Error
**errp
)
1907 TargetInfo
*info
= g_malloc0(sizeof(*info
));
1909 info
->arch
= g_strdup(TARGET_NAME
);
1914 /* Stub function that's gets run on the vcpu when its brought out of the
1915 VM to run inside qemu via async_run_on_cpu()*/
1916 static void mig_sleep_cpu(void *opq
)
1918 qemu_mutex_unlock_iothread();
1920 qemu_mutex_lock_iothread();
1923 /* To reduce the dirty rate explicitly disallow the VCPUs from spending
1924 much time in the VM. The migration thread will try to catchup.
1925 Workload will experience a performance drop.
1927 static void mig_throttle_guest_down(void)
1931 qemu_mutex_lock_iothread();
1933 async_run_on_cpu(cpu
, mig_sleep_cpu
, NULL
);
1935 qemu_mutex_unlock_iothread();
1938 static void check_guest_throttling(void)
1943 if (!mig_throttle_on
) {
1948 t0
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
1952 t1
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
1954 /* If it has been more than 40 ms since the last time the guest
1955 * was throttled then do it again.
1957 if (40 < (t1
-t0
)/1000000) {
1958 mig_throttle_guest_down();