migration: Add the framework of multi-thread decompression
[qemu/cris-port.git] / arch_init.c
blob874948195a8075c2ee7593fb39ae397ab035dd47
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
24 #include <stdint.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <zlib.h>
28 #ifndef _WIN32
29 #include <sys/types.h>
30 #include <sys/mman.h>
31 #endif
32 #include "config.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"
51 #include "trace.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)
61 #else
62 #define DPRINTF(fmt, ...) \
63 do { } while (0)
64 #endif
66 #ifdef TARGET_SPARC
67 int graphic_width = 1024;
68 int graphic_height = 768;
69 int graphic_depth = 8;
70 #else
71 int graphic_width = 800;
72 int graphic_height = 600;
73 int graphic_depth = 32;
74 #endif
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
111 #endif
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) */
136 bool userconfig;
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)
147 int ret;
148 struct defconfig_file *f;
150 for (f = default_config_files; f->filename; f++) {
151 if (!userconfig && f->userconfig) {
152 continue;
154 ret = qemu_read_config_file(f->filename);
155 if (ret < 0 && ret != -ENOENT) {
156 return ret;
160 return 0;
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 */
170 static struct {
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. */
176 PageCache *cache;
177 QemuMutex lock;
178 } XBZRLE;
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;
204 int64_t ret;
206 if (new_size < TARGET_PAGE_SIZE) {
207 return -1;
210 XBZRLE_cache_lock();
212 if (XBZRLE.cache != NULL) {
213 if (pow2floor(new_size) == migrate_xbzrle_cache_size()) {
214 goto out_new_size;
216 new_cache = cache_init(new_size / TARGET_PAGE_SIZE,
217 TARGET_PAGE_SIZE);
218 if (!new_cache) {
219 error_report("Error creating cache");
220 ret = -1;
221 goto out;
224 cache_fini(XBZRLE.cache);
225 XBZRLE.cache = new_cache;
228 out_new_size:
229 ret = pow2floor(new_size);
230 out:
231 XBZRLE_cache_unlock();
232 return ret;
235 /* accounting for migration statistics */
236 typedef struct AccountingInfo {
237 uint64_t dup_pages;
238 uint64_t skipped_pages;
239 uint64_t norm_pages;
240 uint64_t iterations;
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;
246 } AccountingInfo;
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 {
322 /* To be done */
324 typedef struct CompressParam CompressParam;
326 struct DecompressParam {
327 /* To be done */
329 typedef struct DecompressParam DecompressParam;
331 static CompressParam *comp_param;
332 static QemuThread *compress_threads;
333 static bool quit_comp_thread;
334 static bool quit_decomp_thread;
335 static DecompressParam *decomp_param;
336 static QemuThread *decompress_threads;
337 static uint8_t *compressed_data_buf;
339 static void *do_data_compress(void *opaque)
341 while (!quit_comp_thread) {
343 /* To be done */
347 return NULL;
350 static inline void terminate_compression_threads(void)
352 quit_comp_thread = true;
354 /* To be done */
357 void migrate_compress_threads_join(void)
359 int i, thread_count;
361 if (!migrate_use_compression()) {
362 return;
364 terminate_compression_threads();
365 thread_count = migrate_compress_threads();
366 for (i = 0; i < thread_count; i++) {
367 qemu_thread_join(compress_threads + i);
369 g_free(compress_threads);
370 g_free(comp_param);
371 compress_threads = NULL;
372 comp_param = NULL;
375 void migrate_compress_threads_create(void)
377 int i, thread_count;
379 if (!migrate_use_compression()) {
380 return;
382 quit_comp_thread = false;
383 thread_count = migrate_compress_threads();
384 compress_threads = g_new0(QemuThread, thread_count);
385 comp_param = g_new0(CompressParam, thread_count);
386 for (i = 0; i < thread_count; i++) {
387 qemu_thread_create(compress_threads + i, "compress",
388 do_data_compress, comp_param + i,
389 QEMU_THREAD_JOINABLE);
394 * save_page_header: Write page header to wire
396 * If this is the 1st block, it also writes the block identification
398 * Returns: Number of bytes written
400 * @f: QEMUFile where to send the data
401 * @block: block that contains the page we want to send
402 * @offset: offset inside the block for the page
403 * in the lower bits, it contains flags
405 static size_t save_page_header(QEMUFile *f, RAMBlock *block, ram_addr_t offset)
407 size_t size;
409 qemu_put_be64(f, offset);
410 size = 8;
412 if (!(offset & RAM_SAVE_FLAG_CONTINUE)) {
413 qemu_put_byte(f, strlen(block->idstr));
414 qemu_put_buffer(f, (uint8_t *)block->idstr,
415 strlen(block->idstr));
416 size += 1 + strlen(block->idstr);
418 return size;
421 /* Update the xbzrle cache to reflect a page that's been sent as all 0.
422 * The important thing is that a stale (not-yet-0'd) page be replaced
423 * by the new data.
424 * As a bonus, if the page wasn't in the cache it gets added so that
425 * when a small write is made into the 0'd page it gets XBZRLE sent
427 static void xbzrle_cache_zero_page(ram_addr_t current_addr)
429 if (ram_bulk_stage || !migrate_use_xbzrle()) {
430 return;
433 /* We don't care if this fails to allocate a new cache page
434 * as long as it updated an old one */
435 cache_insert(XBZRLE.cache, current_addr, ZERO_TARGET_PAGE,
436 bitmap_sync_count);
439 #define ENCODING_FLAG_XBZRLE 0x1
442 * save_xbzrle_page: compress and send current page
444 * Returns: 1 means that we wrote the page
445 * 0 means that page is identical to the one already sent
446 * -1 means that xbzrle would be longer than normal
448 * @f: QEMUFile where to send the data
449 * @current_data:
450 * @current_addr:
451 * @block: block that contains the page we want to send
452 * @offset: offset inside the block for the page
453 * @last_stage: if we are at the completion stage
454 * @bytes_transferred: increase it with the number of transferred bytes
456 static int save_xbzrle_page(QEMUFile *f, uint8_t **current_data,
457 ram_addr_t current_addr, RAMBlock *block,
458 ram_addr_t offset, bool last_stage,
459 uint64_t *bytes_transferred)
461 int encoded_len = 0, bytes_xbzrle;
462 uint8_t *prev_cached_page;
464 if (!cache_is_cached(XBZRLE.cache, current_addr, bitmap_sync_count)) {
465 acct_info.xbzrle_cache_miss++;
466 if (!last_stage) {
467 if (cache_insert(XBZRLE.cache, current_addr, *current_data,
468 bitmap_sync_count) == -1) {
469 return -1;
470 } else {
471 /* update *current_data when the page has been
472 inserted into cache */
473 *current_data = get_cached_data(XBZRLE.cache, current_addr);
476 return -1;
479 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
481 /* save current buffer into memory */
482 memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE);
484 /* XBZRLE encoding (if there is no overflow) */
485 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
486 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
487 TARGET_PAGE_SIZE);
488 if (encoded_len == 0) {
489 DPRINTF("Skipping unmodified page\n");
490 return 0;
491 } else if (encoded_len == -1) {
492 DPRINTF("Overflow\n");
493 acct_info.xbzrle_overflows++;
494 /* update data in the cache */
495 if (!last_stage) {
496 memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE);
497 *current_data = prev_cached_page;
499 return -1;
502 /* we need to update the data in the cache, in order to get the same data */
503 if (!last_stage) {
504 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
507 /* Send XBZRLE based compressed page */
508 bytes_xbzrle = save_page_header(f, block, offset | RAM_SAVE_FLAG_XBZRLE);
509 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
510 qemu_put_be16(f, encoded_len);
511 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
512 bytes_xbzrle += encoded_len + 1 + 2;
513 acct_info.xbzrle_pages++;
514 acct_info.xbzrle_bytes += bytes_xbzrle;
515 *bytes_transferred += bytes_xbzrle;
517 return 1;
520 static inline
521 ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
522 ram_addr_t start)
524 unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS;
525 unsigned long nr = base + (start >> TARGET_PAGE_BITS);
526 uint64_t mr_size = TARGET_PAGE_ALIGN(memory_region_size(mr));
527 unsigned long size = base + (mr_size >> TARGET_PAGE_BITS);
529 unsigned long next;
531 if (ram_bulk_stage && nr > base) {
532 next = nr + 1;
533 } else {
534 next = find_next_bit(migration_bitmap, size, nr);
537 if (next < size) {
538 clear_bit(next, migration_bitmap);
539 migration_dirty_pages--;
541 return (next - base) << TARGET_PAGE_BITS;
544 static inline bool migration_bitmap_set_dirty(ram_addr_t addr)
546 bool ret;
547 int nr = addr >> TARGET_PAGE_BITS;
549 ret = test_and_set_bit(nr, migration_bitmap);
551 if (!ret) {
552 migration_dirty_pages++;
554 return ret;
557 static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length)
559 ram_addr_t addr;
560 unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
562 /* start address is aligned at the start of a word? */
563 if (((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) {
564 int k;
565 int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS);
566 unsigned long *src = ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION];
568 for (k = page; k < page + nr; k++) {
569 if (src[k]) {
570 unsigned long new_dirty;
571 new_dirty = ~migration_bitmap[k];
572 migration_bitmap[k] |= src[k];
573 new_dirty &= src[k];
574 migration_dirty_pages += ctpopl(new_dirty);
575 src[k] = 0;
578 } else {
579 for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
580 if (cpu_physical_memory_get_dirty(start + addr,
581 TARGET_PAGE_SIZE,
582 DIRTY_MEMORY_MIGRATION)) {
583 cpu_physical_memory_reset_dirty(start + addr,
584 TARGET_PAGE_SIZE,
585 DIRTY_MEMORY_MIGRATION);
586 migration_bitmap_set_dirty(start + addr);
593 /* Fix me: there are too many global variables used in migration process. */
594 static int64_t start_time;
595 static int64_t bytes_xfer_prev;
596 static int64_t num_dirty_pages_period;
598 static void migration_bitmap_sync_init(void)
600 start_time = 0;
601 bytes_xfer_prev = 0;
602 num_dirty_pages_period = 0;
605 /* Called with iothread lock held, to protect ram_list.dirty_memory[] */
606 static void migration_bitmap_sync(void)
608 RAMBlock *block;
609 uint64_t num_dirty_pages_init = migration_dirty_pages;
610 MigrationState *s = migrate_get_current();
611 int64_t end_time;
612 int64_t bytes_xfer_now;
613 static uint64_t xbzrle_cache_miss_prev;
614 static uint64_t iterations_prev;
616 bitmap_sync_count++;
618 if (!bytes_xfer_prev) {
619 bytes_xfer_prev = ram_bytes_transferred();
622 if (!start_time) {
623 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
626 trace_migration_bitmap_sync_start();
627 address_space_sync_dirty_bitmap(&address_space_memory);
629 rcu_read_lock();
630 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
631 migration_bitmap_sync_range(block->mr->ram_addr, block->used_length);
633 rcu_read_unlock();
635 trace_migration_bitmap_sync_end(migration_dirty_pages
636 - num_dirty_pages_init);
637 num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
638 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
640 /* more than 1 second = 1000 millisecons */
641 if (end_time > start_time + 1000) {
642 if (migrate_auto_converge()) {
643 /* The following detection logic can be refined later. For now:
644 Check to see if the dirtied bytes is 50% more than the approx.
645 amount of bytes that just got transferred since the last time we
646 were in this routine. If that happens >N times (for now N==4)
647 we turn on the throttle down logic */
648 bytes_xfer_now = ram_bytes_transferred();
649 if (s->dirty_pages_rate &&
650 (num_dirty_pages_period * TARGET_PAGE_SIZE >
651 (bytes_xfer_now - bytes_xfer_prev)/2) &&
652 (dirty_rate_high_cnt++ > 4)) {
653 trace_migration_throttle();
654 mig_throttle_on = true;
655 dirty_rate_high_cnt = 0;
657 bytes_xfer_prev = bytes_xfer_now;
658 } else {
659 mig_throttle_on = false;
661 if (migrate_use_xbzrle()) {
662 if (iterations_prev != 0) {
663 acct_info.xbzrle_cache_miss_rate =
664 (double)(acct_info.xbzrle_cache_miss -
665 xbzrle_cache_miss_prev) /
666 (acct_info.iterations - iterations_prev);
668 iterations_prev = acct_info.iterations;
669 xbzrle_cache_miss_prev = acct_info.xbzrle_cache_miss;
671 s->dirty_pages_rate = num_dirty_pages_period * 1000
672 / (end_time - start_time);
673 s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE;
674 start_time = end_time;
675 num_dirty_pages_period = 0;
676 s->dirty_sync_count = bitmap_sync_count;
681 * ram_save_page: Send the given page to the stream
683 * Returns: Number of pages written.
685 * @f: QEMUFile where to send the data
686 * @block: block that contains the page we want to send
687 * @offset: offset inside the block for the page
688 * @last_stage: if we are at the completion stage
689 * @bytes_transferred: increase it with the number of transferred bytes
691 static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset,
692 bool last_stage, uint64_t *bytes_transferred)
694 int pages = -1;
695 uint64_t bytes_xmit;
696 ram_addr_t current_addr;
697 MemoryRegion *mr = block->mr;
698 uint8_t *p;
699 int ret;
700 bool send_async = true;
702 p = memory_region_get_ram_ptr(mr) + offset;
704 /* In doubt sent page as normal */
705 bytes_xmit = 0;
706 ret = ram_control_save_page(f, block->offset,
707 offset, TARGET_PAGE_SIZE, &bytes_xmit);
708 if (bytes_xmit) {
709 *bytes_transferred += bytes_xmit;
710 pages = 1;
713 XBZRLE_cache_lock();
715 current_addr = block->offset + offset;
717 if (block == last_sent_block) {
718 offset |= RAM_SAVE_FLAG_CONTINUE;
720 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
721 if (ret != RAM_SAVE_CONTROL_DELAYED) {
722 if (bytes_xmit > 0) {
723 acct_info.norm_pages++;
724 } else if (bytes_xmit == 0) {
725 acct_info.dup_pages++;
728 } else if (is_zero_range(p, TARGET_PAGE_SIZE)) {
729 acct_info.dup_pages++;
730 *bytes_transferred += save_page_header(f, block,
731 offset | RAM_SAVE_FLAG_COMPRESS);
732 qemu_put_byte(f, 0);
733 *bytes_transferred += 1;
734 pages = 1;
735 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
736 * page would be stale
738 xbzrle_cache_zero_page(current_addr);
739 } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
740 pages = save_xbzrle_page(f, &p, current_addr, block,
741 offset, last_stage, bytes_transferred);
742 if (!last_stage) {
743 /* Can't send this cached data async, since the cache page
744 * might get updated before it gets to the wire
746 send_async = false;
750 /* XBZRLE overflow or normal page */
751 if (pages == -1) {
752 *bytes_transferred += save_page_header(f, block,
753 offset | RAM_SAVE_FLAG_PAGE);
754 if (send_async) {
755 qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
756 } else {
757 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
759 *bytes_transferred += TARGET_PAGE_SIZE;
760 pages = 1;
761 acct_info.norm_pages++;
764 XBZRLE_cache_unlock();
766 return pages;
770 * ram_save_compressed_page: compress the given page and send it to the stream
772 * Returns: Number of pages written.
774 * @f: QEMUFile where to send the data
775 * @block: block that contains the page we want to send
776 * @offset: offset inside the block for the page
777 * @last_stage: if we are at the completion stage
778 * @bytes_transferred: increase it with the number of transferred bytes
780 static int ram_save_compressed_page(QEMUFile *f, RAMBlock *block,
781 ram_addr_t offset, bool last_stage,
782 uint64_t *bytes_transferred)
784 int pages = -1;
786 /* To be done*/
788 return pages;
792 * ram_find_and_save_block: Finds a dirty page and sends it to f
794 * Called within an RCU critical section.
796 * Returns: The number of pages written
797 * 0 means no dirty pages
799 * @f: QEMUFile where to send the data
800 * @last_stage: if we are at the completion stage
801 * @bytes_transferred: increase it with the number of transferred bytes
804 static int ram_find_and_save_block(QEMUFile *f, bool last_stage,
805 uint64_t *bytes_transferred)
807 RAMBlock *block = last_seen_block;
808 ram_addr_t offset = last_offset;
809 bool complete_round = false;
810 int pages = 0;
811 MemoryRegion *mr;
813 if (!block)
814 block = QLIST_FIRST_RCU(&ram_list.blocks);
816 while (true) {
817 mr = block->mr;
818 offset = migration_bitmap_find_and_reset_dirty(mr, offset);
819 if (complete_round && block == last_seen_block &&
820 offset >= last_offset) {
821 break;
823 if (offset >= block->used_length) {
824 offset = 0;
825 block = QLIST_NEXT_RCU(block, next);
826 if (!block) {
827 block = QLIST_FIRST_RCU(&ram_list.blocks);
828 complete_round = true;
829 ram_bulk_stage = false;
831 } else {
832 if (migrate_use_compression()) {
833 pages = ram_save_compressed_page(f, block, offset, last_stage,
834 bytes_transferred);
835 } else {
836 pages = ram_save_page(f, block, offset, last_stage,
837 bytes_transferred);
840 /* if page is unmodified, continue to the next */
841 if (pages > 0) {
842 last_sent_block = block;
843 break;
848 last_seen_block = block;
849 last_offset = offset;
851 return pages;
854 static uint64_t bytes_transferred;
856 void acct_update_position(QEMUFile *f, size_t size, bool zero)
858 uint64_t pages = size / TARGET_PAGE_SIZE;
859 if (zero) {
860 acct_info.dup_pages += pages;
861 } else {
862 acct_info.norm_pages += pages;
863 bytes_transferred += size;
864 qemu_update_position(f, size);
868 static ram_addr_t ram_save_remaining(void)
870 return migration_dirty_pages;
873 uint64_t ram_bytes_remaining(void)
875 return ram_save_remaining() * TARGET_PAGE_SIZE;
878 uint64_t ram_bytes_transferred(void)
880 return bytes_transferred;
883 uint64_t ram_bytes_total(void)
885 RAMBlock *block;
886 uint64_t total = 0;
888 rcu_read_lock();
889 QLIST_FOREACH_RCU(block, &ram_list.blocks, next)
890 total += block->used_length;
891 rcu_read_unlock();
892 return total;
895 void free_xbzrle_decoded_buf(void)
897 g_free(xbzrle_decoded_buf);
898 xbzrle_decoded_buf = NULL;
901 static void migration_end(void)
903 if (migration_bitmap) {
904 memory_global_dirty_log_stop();
905 g_free(migration_bitmap);
906 migration_bitmap = NULL;
909 XBZRLE_cache_lock();
910 if (XBZRLE.cache) {
911 cache_fini(XBZRLE.cache);
912 g_free(XBZRLE.encoded_buf);
913 g_free(XBZRLE.current_buf);
914 XBZRLE.cache = NULL;
915 XBZRLE.encoded_buf = NULL;
916 XBZRLE.current_buf = NULL;
918 XBZRLE_cache_unlock();
921 static void ram_migration_cancel(void *opaque)
923 migration_end();
926 static void reset_ram_globals(void)
928 last_seen_block = NULL;
929 last_sent_block = NULL;
930 last_offset = 0;
931 last_version = ram_list.version;
932 ram_bulk_stage = true;
935 #define MAX_WAIT 50 /* ms, half buffered_file limit */
938 /* Each of ram_save_setup, ram_save_iterate and ram_save_complete has
939 * long-running RCU critical section. When rcu-reclaims in the code
940 * start to become numerous it will be necessary to reduce the
941 * granularity of these critical sections.
944 static int ram_save_setup(QEMUFile *f, void *opaque)
946 RAMBlock *block;
947 int64_t ram_bitmap_pages; /* Size of bitmap in pages, including gaps */
949 mig_throttle_on = false;
950 dirty_rate_high_cnt = 0;
951 bitmap_sync_count = 0;
952 migration_bitmap_sync_init();
954 if (migrate_use_xbzrle()) {
955 XBZRLE_cache_lock();
956 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
957 TARGET_PAGE_SIZE,
958 TARGET_PAGE_SIZE);
959 if (!XBZRLE.cache) {
960 XBZRLE_cache_unlock();
961 error_report("Error creating cache");
962 return -1;
964 XBZRLE_cache_unlock();
966 /* We prefer not to abort if there is no memory */
967 XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
968 if (!XBZRLE.encoded_buf) {
969 error_report("Error allocating encoded_buf");
970 return -1;
973 XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
974 if (!XBZRLE.current_buf) {
975 error_report("Error allocating current_buf");
976 g_free(XBZRLE.encoded_buf);
977 XBZRLE.encoded_buf = NULL;
978 return -1;
981 acct_clear();
984 /* iothread lock needed for ram_list.dirty_memory[] */
985 qemu_mutex_lock_iothread();
986 qemu_mutex_lock_ramlist();
987 rcu_read_lock();
988 bytes_transferred = 0;
989 reset_ram_globals();
991 ram_bitmap_pages = last_ram_offset() >> TARGET_PAGE_BITS;
992 migration_bitmap = bitmap_new(ram_bitmap_pages);
993 bitmap_set(migration_bitmap, 0, ram_bitmap_pages);
996 * Count the total number of pages used by ram blocks not including any
997 * gaps due to alignment or unplugs.
999 migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS;
1001 memory_global_dirty_log_start();
1002 migration_bitmap_sync();
1003 qemu_mutex_unlock_ramlist();
1004 qemu_mutex_unlock_iothread();
1006 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
1008 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1009 qemu_put_byte(f, strlen(block->idstr));
1010 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
1011 qemu_put_be64(f, block->used_length);
1014 rcu_read_unlock();
1016 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
1017 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
1019 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1021 return 0;
1024 static int ram_save_iterate(QEMUFile *f, void *opaque)
1026 int ret;
1027 int i;
1028 int64_t t0;
1029 int pages_sent = 0;
1031 rcu_read_lock();
1032 if (ram_list.version != last_version) {
1033 reset_ram_globals();
1036 /* Read version before ram_list.blocks */
1037 smp_rmb();
1039 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
1041 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1042 i = 0;
1043 while ((ret = qemu_file_rate_limit(f)) == 0) {
1044 int pages;
1046 pages = ram_find_and_save_block(f, false, &bytes_transferred);
1047 /* no more pages to sent */
1048 if (pages == 0) {
1049 break;
1051 pages_sent += pages;
1052 acct_info.iterations++;
1053 check_guest_throttling();
1054 /* we want to check in the 1st loop, just in case it was the 1st time
1055 and we had to sync the dirty bitmap.
1056 qemu_get_clock_ns() is a bit expensive, so we only check each some
1057 iterations
1059 if ((i & 63) == 0) {
1060 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
1061 if (t1 > MAX_WAIT) {
1062 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
1063 t1, i);
1064 break;
1067 i++;
1069 rcu_read_unlock();
1072 * Must occur before EOS (or any QEMUFile operation)
1073 * because of RDMA protocol.
1075 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
1077 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1078 bytes_transferred += 8;
1080 ret = qemu_file_get_error(f);
1081 if (ret < 0) {
1082 return ret;
1085 return pages_sent;
1088 /* Called with iothread lock */
1089 static int ram_save_complete(QEMUFile *f, void *opaque)
1091 rcu_read_lock();
1093 migration_bitmap_sync();
1095 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
1097 /* try transferring iterative blocks of memory */
1099 /* flush all remaining blocks regardless of rate limiting */
1100 while (true) {
1101 int pages;
1103 pages = ram_find_and_save_block(f, true, &bytes_transferred);
1104 /* no more blocks to sent */
1105 if (pages == 0) {
1106 break;
1110 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
1111 migration_end();
1113 rcu_read_unlock();
1114 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1116 return 0;
1119 static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
1121 uint64_t remaining_size;
1123 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
1125 if (remaining_size < max_size) {
1126 qemu_mutex_lock_iothread();
1127 rcu_read_lock();
1128 migration_bitmap_sync();
1129 rcu_read_unlock();
1130 qemu_mutex_unlock_iothread();
1131 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
1133 return remaining_size;
1136 static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
1138 unsigned int xh_len;
1139 int xh_flags;
1141 if (!xbzrle_decoded_buf) {
1142 xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE);
1145 /* extract RLE header */
1146 xh_flags = qemu_get_byte(f);
1147 xh_len = qemu_get_be16(f);
1149 if (xh_flags != ENCODING_FLAG_XBZRLE) {
1150 error_report("Failed to load XBZRLE page - wrong compression!");
1151 return -1;
1154 if (xh_len > TARGET_PAGE_SIZE) {
1155 error_report("Failed to load XBZRLE page - len overflow!");
1156 return -1;
1158 /* load data and decode */
1159 qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
1161 /* decode RLE */
1162 if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
1163 TARGET_PAGE_SIZE) == -1) {
1164 error_report("Failed to load XBZRLE page - decode error!");
1165 return -1;
1168 return 0;
1171 /* Must be called from within a rcu critical section.
1172 * Returns a pointer from within the RCU-protected ram_list.
1174 static inline void *host_from_stream_offset(QEMUFile *f,
1175 ram_addr_t offset,
1176 int flags)
1178 static RAMBlock *block = NULL;
1179 char id[256];
1180 uint8_t len;
1182 if (flags & RAM_SAVE_FLAG_CONTINUE) {
1183 if (!block || block->max_length <= offset) {
1184 error_report("Ack, bad migration stream!");
1185 return NULL;
1188 return memory_region_get_ram_ptr(block->mr) + offset;
1191 len = qemu_get_byte(f);
1192 qemu_get_buffer(f, (uint8_t *)id, len);
1193 id[len] = 0;
1195 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1196 if (!strncmp(id, block->idstr, sizeof(id)) &&
1197 block->max_length > offset) {
1198 return memory_region_get_ram_ptr(block->mr) + offset;
1202 error_report("Can't find block %s!", id);
1203 return NULL;
1207 * If a page (or a whole RDMA chunk) has been
1208 * determined to be zero, then zap it.
1210 void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
1212 if (ch != 0 || !is_zero_range(host, size)) {
1213 memset(host, ch, size);
1217 static void *do_data_decompress(void *opaque)
1219 while (!quit_decomp_thread) {
1220 /* To be done */
1223 return NULL;
1226 void migrate_decompress_threads_create(void)
1228 int i, thread_count;
1230 thread_count = migrate_decompress_threads();
1231 decompress_threads = g_new0(QemuThread, thread_count);
1232 decomp_param = g_new0(DecompressParam, thread_count);
1233 compressed_data_buf = g_malloc0(compressBound(TARGET_PAGE_SIZE));
1234 quit_decomp_thread = false;
1235 for (i = 0; i < thread_count; i++) {
1236 qemu_thread_create(decompress_threads + i, "decompress",
1237 do_data_decompress, decomp_param + i,
1238 QEMU_THREAD_JOINABLE);
1242 void migrate_decompress_threads_join(void)
1244 int i, thread_count;
1246 quit_decomp_thread = true;
1247 thread_count = migrate_decompress_threads();
1248 for (i = 0; i < thread_count; i++) {
1249 qemu_thread_join(decompress_threads + i);
1251 g_free(decompress_threads);
1252 g_free(decomp_param);
1253 g_free(compressed_data_buf);
1254 decompress_threads = NULL;
1255 decomp_param = NULL;
1256 compressed_data_buf = NULL;
1259 static void decompress_data_with_multi_threads(uint8_t *compbuf,
1260 void *host, int len)
1262 /* To be done */
1265 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1267 int flags = 0, ret = 0;
1268 static uint64_t seq_iter;
1269 int len = 0;
1271 seq_iter++;
1273 if (version_id != 4) {
1274 ret = -EINVAL;
1277 /* This RCU critical section can be very long running.
1278 * When RCU reclaims in the code start to become numerous,
1279 * it will be necessary to reduce the granularity of this
1280 * critical section.
1282 rcu_read_lock();
1283 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) {
1284 ram_addr_t addr, total_ram_bytes;
1285 void *host;
1286 uint8_t ch;
1288 addr = qemu_get_be64(f);
1289 flags = addr & ~TARGET_PAGE_MASK;
1290 addr &= TARGET_PAGE_MASK;
1292 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
1293 case RAM_SAVE_FLAG_MEM_SIZE:
1294 /* Synchronize RAM block list */
1295 total_ram_bytes = addr;
1296 while (!ret && total_ram_bytes) {
1297 RAMBlock *block;
1298 uint8_t len;
1299 char id[256];
1300 ram_addr_t length;
1302 len = qemu_get_byte(f);
1303 qemu_get_buffer(f, (uint8_t *)id, len);
1304 id[len] = 0;
1305 length = qemu_get_be64(f);
1307 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1308 if (!strncmp(id, block->idstr, sizeof(id))) {
1309 if (length != block->used_length) {
1310 Error *local_err = NULL;
1312 ret = qemu_ram_resize(block->offset, length, &local_err);
1313 if (local_err) {
1314 error_report_err(local_err);
1317 break;
1321 if (!block) {
1322 error_report("Unknown ramblock \"%s\", cannot "
1323 "accept migration", id);
1324 ret = -EINVAL;
1327 total_ram_bytes -= length;
1329 break;
1330 case RAM_SAVE_FLAG_COMPRESS:
1331 host = host_from_stream_offset(f, addr, flags);
1332 if (!host) {
1333 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
1334 ret = -EINVAL;
1335 break;
1337 ch = qemu_get_byte(f);
1338 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
1339 break;
1340 case RAM_SAVE_FLAG_PAGE:
1341 host = host_from_stream_offset(f, addr, flags);
1342 if (!host) {
1343 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
1344 ret = -EINVAL;
1345 break;
1347 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
1348 break;
1349 case RAM_SAVE_FLAG_COMPRESS_PAGE:
1350 host = host_from_stream_offset(f, addr, flags);
1351 if (!host) {
1352 error_report("Invalid RAM offset " RAM_ADDR_FMT, addr);
1353 ret = -EINVAL;
1354 break;
1357 len = qemu_get_be32(f);
1358 if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) {
1359 error_report("Invalid compressed data length: %d", len);
1360 ret = -EINVAL;
1361 break;
1363 qemu_get_buffer(f, compressed_data_buf, len);
1364 decompress_data_with_multi_threads(compressed_data_buf, host, len);
1365 break;
1366 case RAM_SAVE_FLAG_XBZRLE:
1367 host = host_from_stream_offset(f, addr, flags);
1368 if (!host) {
1369 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
1370 ret = -EINVAL;
1371 break;
1373 if (load_xbzrle(f, addr, host) < 0) {
1374 error_report("Failed to decompress XBZRLE page at "
1375 RAM_ADDR_FMT, addr);
1376 ret = -EINVAL;
1377 break;
1379 break;
1380 case RAM_SAVE_FLAG_EOS:
1381 /* normal exit */
1382 break;
1383 default:
1384 if (flags & RAM_SAVE_FLAG_HOOK) {
1385 ram_control_load_hook(f, flags);
1386 } else {
1387 error_report("Unknown combination of migration flags: %#x",
1388 flags);
1389 ret = -EINVAL;
1392 if (!ret) {
1393 ret = qemu_file_get_error(f);
1397 rcu_read_unlock();
1398 DPRINTF("Completed load of VM with exit code %d seq iteration "
1399 "%" PRIu64 "\n", ret, seq_iter);
1400 return ret;
1403 static SaveVMHandlers savevm_ram_handlers = {
1404 .save_live_setup = ram_save_setup,
1405 .save_live_iterate = ram_save_iterate,
1406 .save_live_complete = ram_save_complete,
1407 .save_live_pending = ram_save_pending,
1408 .load_state = ram_load,
1409 .cancel = ram_migration_cancel,
1412 void ram_mig_init(void)
1414 qemu_mutex_init(&XBZRLE.lock);
1415 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
1418 struct soundhw {
1419 const char *name;
1420 const char *descr;
1421 int enabled;
1422 int isa;
1423 union {
1424 int (*init_isa) (ISABus *bus);
1425 int (*init_pci) (PCIBus *bus);
1426 } init;
1429 static struct soundhw soundhw[9];
1430 static int soundhw_count;
1432 void isa_register_soundhw(const char *name, const char *descr,
1433 int (*init_isa)(ISABus *bus))
1435 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1436 soundhw[soundhw_count].name = name;
1437 soundhw[soundhw_count].descr = descr;
1438 soundhw[soundhw_count].isa = 1;
1439 soundhw[soundhw_count].init.init_isa = init_isa;
1440 soundhw_count++;
1443 void pci_register_soundhw(const char *name, const char *descr,
1444 int (*init_pci)(PCIBus *bus))
1446 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1447 soundhw[soundhw_count].name = name;
1448 soundhw[soundhw_count].descr = descr;
1449 soundhw[soundhw_count].isa = 0;
1450 soundhw[soundhw_count].init.init_pci = init_pci;
1451 soundhw_count++;
1454 void select_soundhw(const char *optarg)
1456 struct soundhw *c;
1458 if (is_help_option(optarg)) {
1459 show_valid_cards:
1461 if (soundhw_count) {
1462 printf("Valid sound card names (comma separated):\n");
1463 for (c = soundhw; c->name; ++c) {
1464 printf ("%-11s %s\n", c->name, c->descr);
1466 printf("\n-soundhw all will enable all of the above\n");
1467 } else {
1468 printf("Machine has no user-selectable audio hardware "
1469 "(it may or may not have always-present audio hardware).\n");
1471 exit(!is_help_option(optarg));
1473 else {
1474 size_t l;
1475 const char *p;
1476 char *e;
1477 int bad_card = 0;
1479 if (!strcmp(optarg, "all")) {
1480 for (c = soundhw; c->name; ++c) {
1481 c->enabled = 1;
1483 return;
1486 p = optarg;
1487 while (*p) {
1488 e = strchr(p, ',');
1489 l = !e ? strlen(p) : (size_t) (e - p);
1491 for (c = soundhw; c->name; ++c) {
1492 if (!strncmp(c->name, p, l) && !c->name[l]) {
1493 c->enabled = 1;
1494 break;
1498 if (!c->name) {
1499 if (l > 80) {
1500 error_report("Unknown sound card name (too big to show)");
1502 else {
1503 error_report("Unknown sound card name `%.*s'",
1504 (int) l, p);
1506 bad_card = 1;
1508 p += l + (e != NULL);
1511 if (bad_card) {
1512 goto show_valid_cards;
1517 void audio_init(void)
1519 struct soundhw *c;
1520 ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
1521 PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
1523 for (c = soundhw; c->name; ++c) {
1524 if (c->enabled) {
1525 if (c->isa) {
1526 if (!isa_bus) {
1527 error_report("ISA bus not available for %s", c->name);
1528 exit(1);
1530 c->init.init_isa(isa_bus);
1531 } else {
1532 if (!pci_bus) {
1533 error_report("PCI bus not available for %s", c->name);
1534 exit(1);
1536 c->init.init_pci(pci_bus);
1542 int qemu_uuid_parse(const char *str, uint8_t *uuid)
1544 int ret;
1546 if (strlen(str) != 36) {
1547 return -1;
1550 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1551 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1552 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1553 &uuid[15]);
1555 if (ret != 16) {
1556 return -1;
1558 return 0;
1561 void do_acpitable_option(const QemuOpts *opts)
1563 #ifdef TARGET_I386
1564 Error *err = NULL;
1566 acpi_table_add(opts, &err);
1567 if (err) {
1568 error_report("Wrong acpi table provided: %s",
1569 error_get_pretty(err));
1570 error_free(err);
1571 exit(1);
1573 #endif
1576 void do_smbios_option(QemuOpts *opts)
1578 #ifdef TARGET_I386
1579 smbios_entry_add(opts);
1580 #endif
1583 void cpudef_init(void)
1585 #if defined(cpudef_setup)
1586 cpudef_setup(); /* parse cpu definitions in target config file */
1587 #endif
1590 int kvm_available(void)
1592 #ifdef CONFIG_KVM
1593 return 1;
1594 #else
1595 return 0;
1596 #endif
1599 int xen_available(void)
1601 #ifdef CONFIG_XEN
1602 return 1;
1603 #else
1604 return 0;
1605 #endif
1609 TargetInfo *qmp_query_target(Error **errp)
1611 TargetInfo *info = g_malloc0(sizeof(*info));
1613 info->arch = g_strdup(TARGET_NAME);
1615 return info;
1618 /* Stub function that's gets run on the vcpu when its brought out of the
1619 VM to run inside qemu via async_run_on_cpu()*/
1620 static void mig_sleep_cpu(void *opq)
1622 qemu_mutex_unlock_iothread();
1623 g_usleep(30*1000);
1624 qemu_mutex_lock_iothread();
1627 /* To reduce the dirty rate explicitly disallow the VCPUs from spending
1628 much time in the VM. The migration thread will try to catchup.
1629 Workload will experience a performance drop.
1631 static void mig_throttle_guest_down(void)
1633 CPUState *cpu;
1635 qemu_mutex_lock_iothread();
1636 CPU_FOREACH(cpu) {
1637 async_run_on_cpu(cpu, mig_sleep_cpu, NULL);
1639 qemu_mutex_unlock_iothread();
1642 static void check_guest_throttling(void)
1644 static int64_t t0;
1645 int64_t t1;
1647 if (!mig_throttle_on) {
1648 return;
1651 if (!t0) {
1652 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1653 return;
1656 t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1658 /* If it has been more than 40 ms since the last time the guest
1659 * was throttled then do it again.
1661 if (40 < (t1-t0)/1000000) {
1662 mig_throttle_guest_down();
1663 t0 = t1;