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