]> git.proxmox.com Git - mirror_qemu.git/blob - arch_init.c
migration: Add the core code of multi-thread compression
[mirror_qemu.git] / arch_init.c
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 #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"
57
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
65
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
75
76
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
112
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);
117
118 static uint64_t bitmap_sync_count;
119
120 /***********************************************************/
121 /* ram save/restore */
122
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
132
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 */
141 };
142
143 static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE];
144
145 int qemu_read_default_config_files(bool userconfig)
146 {
147 int ret;
148 struct defconfig_file *f;
149
150 for (f = default_config_files; f->filename; f++) {
151 if (!userconfig && f->userconfig) {
152 continue;
153 }
154 ret = qemu_read_config_file(f->filename);
155 if (ret < 0 && ret != -ENOENT) {
156 return ret;
157 }
158 }
159
160 return 0;
161 }
162
163 static inline bool is_zero_range(uint8_t *p, uint64_t size)
164 {
165 return buffer_find_nonzero_offset(p, size) == size;
166 }
167
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;
179
180 /* buffer used for XBZRLE decoding */
181 static uint8_t *xbzrle_decoded_buf;
182
183 static void XBZRLE_cache_lock(void)
184 {
185 if (migrate_use_xbzrle())
186 qemu_mutex_lock(&XBZRLE.lock);
187 }
188
189 static void XBZRLE_cache_unlock(void)
190 {
191 if (migrate_use_xbzrle())
192 qemu_mutex_unlock(&XBZRLE.lock);
193 }
194
195 /*
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().
200 */
201 int64_t xbzrle_cache_resize(int64_t new_size)
202 {
203 PageCache *new_cache;
204 int64_t ret;
205
206 if (new_size < TARGET_PAGE_SIZE) {
207 return -1;
208 }
209
210 XBZRLE_cache_lock();
211
212 if (XBZRLE.cache != NULL) {
213 if (pow2floor(new_size) == migrate_xbzrle_cache_size()) {
214 goto out_new_size;
215 }
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;
222 }
223
224 cache_fini(XBZRLE.cache);
225 XBZRLE.cache = new_cache;
226 }
227
228 out_new_size:
229 ret = pow2floor(new_size);
230 out:
231 XBZRLE_cache_unlock();
232 return ret;
233 }
234
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;
247
248 static AccountingInfo acct_info;
249
250 static void acct_clear(void)
251 {
252 memset(&acct_info, 0, sizeof(acct_info));
253 }
254
255 uint64_t dup_mig_bytes_transferred(void)
256 {
257 return acct_info.dup_pages * TARGET_PAGE_SIZE;
258 }
259
260 uint64_t dup_mig_pages_transferred(void)
261 {
262 return acct_info.dup_pages;
263 }
264
265 uint64_t skipped_mig_bytes_transferred(void)
266 {
267 return acct_info.skipped_pages * TARGET_PAGE_SIZE;
268 }
269
270 uint64_t skipped_mig_pages_transferred(void)
271 {
272 return acct_info.skipped_pages;
273 }
274
275 uint64_t norm_mig_bytes_transferred(void)
276 {
277 return acct_info.norm_pages * TARGET_PAGE_SIZE;
278 }
279
280 uint64_t norm_mig_pages_transferred(void)
281 {
282 return acct_info.norm_pages;
283 }
284
285 uint64_t xbzrle_mig_bytes_transferred(void)
286 {
287 return acct_info.xbzrle_bytes;
288 }
289
290 uint64_t xbzrle_mig_pages_transferred(void)
291 {
292 return acct_info.xbzrle_pages;
293 }
294
295 uint64_t xbzrle_mig_pages_cache_miss(void)
296 {
297 return acct_info.xbzrle_cache_miss;
298 }
299
300 double xbzrle_mig_cache_miss_rate(void)
301 {
302 return acct_info.xbzrle_cache_miss_rate;
303 }
304
305 uint64_t xbzrle_mig_pages_overflow(void)
306 {
307 return acct_info.xbzrle_overflows;
308 }
309
310 /* This is the last block that we have visited serching for dirty pages
311 */
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;
320
321 struct CompressParam {
322 bool start;
323 bool done;
324 QEMUFile *file;
325 QemuMutex mutex;
326 QemuCond cond;
327 RAMBlock *block;
328 ram_addr_t offset;
329 };
330 typedef struct CompressParam CompressParam;
331
332 struct DecompressParam {
333 bool start;
334 QemuMutex mutex;
335 QemuCond cond;
336 void *des;
337 uint8 *compbuf;
338 int len;
339 };
340 typedef struct DecompressParam DecompressParam;
341
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.
347 */
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 = { };
352 static bool quit_comp_thread;
353 static bool quit_decomp_thread;
354 static DecompressParam *decomp_param;
355 static QemuThread *decompress_threads;
356 static uint8_t *compressed_data_buf;
357
358 static int do_compress_ram_page(CompressParam *param);
359
360 static void *do_data_compress(void *opaque)
361 {
362 CompressParam *param = opaque;
363
364 while (!quit_comp_thread) {
365 qemu_mutex_lock(&param->mutex);
366 /* Re-check the quit_comp_thread in case of
367 * terminate_compression_threads is called just before
368 * qemu_mutex_lock(&param->mutex) and after
369 * while(!quit_comp_thread), re-check it here can make
370 * sure the compression thread terminate as expected.
371 */
372 while (!param->start && !quit_comp_thread) {
373 qemu_cond_wait(&param->cond, &param->mutex);
374 }
375 if (!quit_comp_thread) {
376 do_compress_ram_page(param);
377 }
378 param->start = false;
379 qemu_mutex_unlock(&param->mutex);
380
381 qemu_mutex_lock(comp_done_lock);
382 param->done = true;
383 qemu_cond_signal(comp_done_cond);
384 qemu_mutex_unlock(comp_done_lock);
385 }
386
387 return NULL;
388 }
389
390 static inline void terminate_compression_threads(void)
391 {
392 int idx, thread_count;
393
394 thread_count = migrate_compress_threads();
395 quit_comp_thread = true;
396 for (idx = 0; idx < thread_count; idx++) {
397 qemu_mutex_lock(&comp_param[idx].mutex);
398 qemu_cond_signal(&comp_param[idx].cond);
399 qemu_mutex_unlock(&comp_param[idx].mutex);
400 }
401 }
402
403 void migrate_compress_threads_join(void)
404 {
405 int i, thread_count;
406
407 if (!migrate_use_compression()) {
408 return;
409 }
410 terminate_compression_threads();
411 thread_count = migrate_compress_threads();
412 for (i = 0; i < thread_count; i++) {
413 qemu_thread_join(compress_threads + i);
414 qemu_fclose(comp_param[i].file);
415 qemu_mutex_destroy(&comp_param[i].mutex);
416 qemu_cond_destroy(&comp_param[i].cond);
417 }
418 qemu_mutex_destroy(comp_done_lock);
419 qemu_cond_destroy(comp_done_cond);
420 g_free(compress_threads);
421 g_free(comp_param);
422 g_free(comp_done_cond);
423 g_free(comp_done_lock);
424 compress_threads = NULL;
425 comp_param = NULL;
426 comp_done_cond = NULL;
427 comp_done_lock = NULL;
428 }
429
430 void migrate_compress_threads_create(void)
431 {
432 int i, thread_count;
433
434 if (!migrate_use_compression()) {
435 return;
436 }
437 quit_comp_thread = false;
438 thread_count = migrate_compress_threads();
439 compress_threads = g_new0(QemuThread, thread_count);
440 comp_param = g_new0(CompressParam, thread_count);
441 comp_done_cond = g_new0(QemuCond, 1);
442 comp_done_lock = g_new0(QemuMutex, 1);
443 qemu_cond_init(comp_done_cond);
444 qemu_mutex_init(comp_done_lock);
445 for (i = 0; i < thread_count; i++) {
446 /* com_param[i].file is just used as a dummy buffer to save data, set
447 * it's ops to empty.
448 */
449 comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops);
450 comp_param[i].done = true;
451 qemu_mutex_init(&comp_param[i].mutex);
452 qemu_cond_init(&comp_param[i].cond);
453 qemu_thread_create(compress_threads + i, "compress",
454 do_data_compress, comp_param + i,
455 QEMU_THREAD_JOINABLE);
456 }
457 }
458
459 /**
460 * save_page_header: Write page header to wire
461 *
462 * If this is the 1st block, it also writes the block identification
463 *
464 * Returns: Number of bytes written
465 *
466 * @f: QEMUFile where to send the data
467 * @block: block that contains the page we want to send
468 * @offset: offset inside the block for the page
469 * in the lower bits, it contains flags
470 */
471 static size_t save_page_header(QEMUFile *f, RAMBlock *block, ram_addr_t offset)
472 {
473 size_t size;
474
475 qemu_put_be64(f, offset);
476 size = 8;
477
478 if (!(offset & RAM_SAVE_FLAG_CONTINUE)) {
479 qemu_put_byte(f, strlen(block->idstr));
480 qemu_put_buffer(f, (uint8_t *)block->idstr,
481 strlen(block->idstr));
482 size += 1 + strlen(block->idstr);
483 }
484 return size;
485 }
486
487 /* Update the xbzrle cache to reflect a page that's been sent as all 0.
488 * The important thing is that a stale (not-yet-0'd) page be replaced
489 * by the new data.
490 * As a bonus, if the page wasn't in the cache it gets added so that
491 * when a small write is made into the 0'd page it gets XBZRLE sent
492 */
493 static void xbzrle_cache_zero_page(ram_addr_t current_addr)
494 {
495 if (ram_bulk_stage || !migrate_use_xbzrle()) {
496 return;
497 }
498
499 /* We don't care if this fails to allocate a new cache page
500 * as long as it updated an old one */
501 cache_insert(XBZRLE.cache, current_addr, ZERO_TARGET_PAGE,
502 bitmap_sync_count);
503 }
504
505 #define ENCODING_FLAG_XBZRLE 0x1
506
507 /**
508 * save_xbzrle_page: compress and send current page
509 *
510 * Returns: 1 means that we wrote the page
511 * 0 means that page is identical to the one already sent
512 * -1 means that xbzrle would be longer than normal
513 *
514 * @f: QEMUFile where to send the data
515 * @current_data:
516 * @current_addr:
517 * @block: block that contains the page we want to send
518 * @offset: offset inside the block for the page
519 * @last_stage: if we are at the completion stage
520 * @bytes_transferred: increase it with the number of transferred bytes
521 */
522 static int save_xbzrle_page(QEMUFile *f, uint8_t **current_data,
523 ram_addr_t current_addr, RAMBlock *block,
524 ram_addr_t offset, bool last_stage,
525 uint64_t *bytes_transferred)
526 {
527 int encoded_len = 0, bytes_xbzrle;
528 uint8_t *prev_cached_page;
529
530 if (!cache_is_cached(XBZRLE.cache, current_addr, bitmap_sync_count)) {
531 acct_info.xbzrle_cache_miss++;
532 if (!last_stage) {
533 if (cache_insert(XBZRLE.cache, current_addr, *current_data,
534 bitmap_sync_count) == -1) {
535 return -1;
536 } else {
537 /* update *current_data when the page has been
538 inserted into cache */
539 *current_data = get_cached_data(XBZRLE.cache, current_addr);
540 }
541 }
542 return -1;
543 }
544
545 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
546
547 /* save current buffer into memory */
548 memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE);
549
550 /* XBZRLE encoding (if there is no overflow) */
551 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
552 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
553 TARGET_PAGE_SIZE);
554 if (encoded_len == 0) {
555 DPRINTF("Skipping unmodified page\n");
556 return 0;
557 } else if (encoded_len == -1) {
558 DPRINTF("Overflow\n");
559 acct_info.xbzrle_overflows++;
560 /* update data in the cache */
561 if (!last_stage) {
562 memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE);
563 *current_data = prev_cached_page;
564 }
565 return -1;
566 }
567
568 /* we need to update the data in the cache, in order to get the same data */
569 if (!last_stage) {
570 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
571 }
572
573 /* Send XBZRLE based compressed page */
574 bytes_xbzrle = save_page_header(f, block, offset | RAM_SAVE_FLAG_XBZRLE);
575 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
576 qemu_put_be16(f, encoded_len);
577 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
578 bytes_xbzrle += encoded_len + 1 + 2;
579 acct_info.xbzrle_pages++;
580 acct_info.xbzrle_bytes += bytes_xbzrle;
581 *bytes_transferred += bytes_xbzrle;
582
583 return 1;
584 }
585
586 static inline
587 ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
588 ram_addr_t start)
589 {
590 unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS;
591 unsigned long nr = base + (start >> TARGET_PAGE_BITS);
592 uint64_t mr_size = TARGET_PAGE_ALIGN(memory_region_size(mr));
593 unsigned long size = base + (mr_size >> TARGET_PAGE_BITS);
594
595 unsigned long next;
596
597 if (ram_bulk_stage && nr > base) {
598 next = nr + 1;
599 } else {
600 next = find_next_bit(migration_bitmap, size, nr);
601 }
602
603 if (next < size) {
604 clear_bit(next, migration_bitmap);
605 migration_dirty_pages--;
606 }
607 return (next - base) << TARGET_PAGE_BITS;
608 }
609
610 static inline bool migration_bitmap_set_dirty(ram_addr_t addr)
611 {
612 bool ret;
613 int nr = addr >> TARGET_PAGE_BITS;
614
615 ret = test_and_set_bit(nr, migration_bitmap);
616
617 if (!ret) {
618 migration_dirty_pages++;
619 }
620 return ret;
621 }
622
623 static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length)
624 {
625 ram_addr_t addr;
626 unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
627
628 /* start address is aligned at the start of a word? */
629 if (((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) {
630 int k;
631 int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS);
632 unsigned long *src = ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION];
633
634 for (k = page; k < page + nr; k++) {
635 if (src[k]) {
636 unsigned long new_dirty;
637 new_dirty = ~migration_bitmap[k];
638 migration_bitmap[k] |= src[k];
639 new_dirty &= src[k];
640 migration_dirty_pages += ctpopl(new_dirty);
641 src[k] = 0;
642 }
643 }
644 } else {
645 for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
646 if (cpu_physical_memory_get_dirty(start + addr,
647 TARGET_PAGE_SIZE,
648 DIRTY_MEMORY_MIGRATION)) {
649 cpu_physical_memory_reset_dirty(start + addr,
650 TARGET_PAGE_SIZE,
651 DIRTY_MEMORY_MIGRATION);
652 migration_bitmap_set_dirty(start + addr);
653 }
654 }
655 }
656 }
657
658
659 /* Fix me: there are too many global variables used in migration process. */
660 static int64_t start_time;
661 static int64_t bytes_xfer_prev;
662 static int64_t num_dirty_pages_period;
663
664 static void migration_bitmap_sync_init(void)
665 {
666 start_time = 0;
667 bytes_xfer_prev = 0;
668 num_dirty_pages_period = 0;
669 }
670
671 /* Called with iothread lock held, to protect ram_list.dirty_memory[] */
672 static void migration_bitmap_sync(void)
673 {
674 RAMBlock *block;
675 uint64_t num_dirty_pages_init = migration_dirty_pages;
676 MigrationState *s = migrate_get_current();
677 int64_t end_time;
678 int64_t bytes_xfer_now;
679 static uint64_t xbzrle_cache_miss_prev;
680 static uint64_t iterations_prev;
681
682 bitmap_sync_count++;
683
684 if (!bytes_xfer_prev) {
685 bytes_xfer_prev = ram_bytes_transferred();
686 }
687
688 if (!start_time) {
689 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
690 }
691
692 trace_migration_bitmap_sync_start();
693 address_space_sync_dirty_bitmap(&address_space_memory);
694
695 rcu_read_lock();
696 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
697 migration_bitmap_sync_range(block->mr->ram_addr, block->used_length);
698 }
699 rcu_read_unlock();
700
701 trace_migration_bitmap_sync_end(migration_dirty_pages
702 - num_dirty_pages_init);
703 num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
704 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
705
706 /* more than 1 second = 1000 millisecons */
707 if (end_time > start_time + 1000) {
708 if (migrate_auto_converge()) {
709 /* The following detection logic can be refined later. For now:
710 Check to see if the dirtied bytes is 50% more than the approx.
711 amount of bytes that just got transferred since the last time we
712 were in this routine. If that happens >N times (for now N==4)
713 we turn on the throttle down logic */
714 bytes_xfer_now = ram_bytes_transferred();
715 if (s->dirty_pages_rate &&
716 (num_dirty_pages_period * TARGET_PAGE_SIZE >
717 (bytes_xfer_now - bytes_xfer_prev)/2) &&
718 (dirty_rate_high_cnt++ > 4)) {
719 trace_migration_throttle();
720 mig_throttle_on = true;
721 dirty_rate_high_cnt = 0;
722 }
723 bytes_xfer_prev = bytes_xfer_now;
724 } else {
725 mig_throttle_on = false;
726 }
727 if (migrate_use_xbzrle()) {
728 if (iterations_prev != 0) {
729 acct_info.xbzrle_cache_miss_rate =
730 (double)(acct_info.xbzrle_cache_miss -
731 xbzrle_cache_miss_prev) /
732 (acct_info.iterations - iterations_prev);
733 }
734 iterations_prev = acct_info.iterations;
735 xbzrle_cache_miss_prev = acct_info.xbzrle_cache_miss;
736 }
737 s->dirty_pages_rate = num_dirty_pages_period * 1000
738 / (end_time - start_time);
739 s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE;
740 start_time = end_time;
741 num_dirty_pages_period = 0;
742 s->dirty_sync_count = bitmap_sync_count;
743 }
744 }
745
746 /**
747 * save_zero_page: Send the zero page to the stream
748 *
749 * Returns: Number of pages written.
750 *
751 * @f: QEMUFile where to send the data
752 * @block: block that contains the page we want to send
753 * @offset: offset inside the block for the page
754 * @p: pointer to the page
755 * @bytes_transferred: increase it with the number of transferred bytes
756 */
757 static int save_zero_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
758 uint8_t *p, uint64_t *bytes_transferred)
759 {
760 int pages = -1;
761
762 if (is_zero_range(p, TARGET_PAGE_SIZE)) {
763 acct_info.dup_pages++;
764 *bytes_transferred += save_page_header(f, block,
765 offset | RAM_SAVE_FLAG_COMPRESS);
766 qemu_put_byte(f, 0);
767 *bytes_transferred += 1;
768 pages = 1;
769 }
770
771 return pages;
772 }
773
774 /**
775 * ram_save_page: Send the given page to the stream
776 *
777 * Returns: Number of pages written.
778 *
779 * @f: QEMUFile where to send the data
780 * @block: block that contains the page we want to send
781 * @offset: offset inside the block for the page
782 * @last_stage: if we are at the completion stage
783 * @bytes_transferred: increase it with the number of transferred bytes
784 */
785 static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset,
786 bool last_stage, uint64_t *bytes_transferred)
787 {
788 int pages = -1;
789 uint64_t bytes_xmit;
790 ram_addr_t current_addr;
791 MemoryRegion *mr = block->mr;
792 uint8_t *p;
793 int ret;
794 bool send_async = true;
795
796 p = memory_region_get_ram_ptr(mr) + offset;
797
798 /* In doubt sent page as normal */
799 bytes_xmit = 0;
800 ret = ram_control_save_page(f, block->offset,
801 offset, TARGET_PAGE_SIZE, &bytes_xmit);
802 if (bytes_xmit) {
803 *bytes_transferred += bytes_xmit;
804 pages = 1;
805 }
806
807 XBZRLE_cache_lock();
808
809 current_addr = block->offset + offset;
810
811 if (block == last_sent_block) {
812 offset |= RAM_SAVE_FLAG_CONTINUE;
813 }
814 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
815 if (ret != RAM_SAVE_CONTROL_DELAYED) {
816 if (bytes_xmit > 0) {
817 acct_info.norm_pages++;
818 } else if (bytes_xmit == 0) {
819 acct_info.dup_pages++;
820 }
821 }
822 } else {
823 pages = save_zero_page(f, block, offset, p, bytes_transferred);
824 if (pages > 0) {
825 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
826 * page would be stale
827 */
828 xbzrle_cache_zero_page(current_addr);
829 } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
830 pages = save_xbzrle_page(f, &p, current_addr, block,
831 offset, last_stage, bytes_transferred);
832 if (!last_stage) {
833 /* Can't send this cached data async, since the cache page
834 * might get updated before it gets to the wire
835 */
836 send_async = false;
837 }
838 }
839 }
840
841 /* XBZRLE overflow or normal page */
842 if (pages == -1) {
843 *bytes_transferred += save_page_header(f, block,
844 offset | RAM_SAVE_FLAG_PAGE);
845 if (send_async) {
846 qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
847 } else {
848 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
849 }
850 *bytes_transferred += TARGET_PAGE_SIZE;
851 pages = 1;
852 acct_info.norm_pages++;
853 }
854
855 XBZRLE_cache_unlock();
856
857 return pages;
858 }
859
860 static int do_compress_ram_page(CompressParam *param)
861 {
862 int bytes_sent, blen;
863 uint8_t *p;
864 RAMBlock *block = param->block;
865 ram_addr_t offset = param->offset;
866
867 p = memory_region_get_ram_ptr(block->mr) + (offset & TARGET_PAGE_MASK);
868
869 bytes_sent = save_page_header(param->file, block, offset |
870 RAM_SAVE_FLAG_COMPRESS_PAGE);
871 blen = qemu_put_compression_data(param->file, p, TARGET_PAGE_SIZE,
872 migrate_compress_level());
873 bytes_sent += blen;
874
875 return bytes_sent;
876 }
877
878 static inline void start_compression(CompressParam *param)
879 {
880 param->done = false;
881 qemu_mutex_lock(&param->mutex);
882 param->start = true;
883 qemu_cond_signal(&param->cond);
884 qemu_mutex_unlock(&param->mutex);
885 }
886
887
888 static uint64_t bytes_transferred;
889
890 static void flush_compressed_data(QEMUFile *f)
891 {
892 int idx, len, thread_count;
893
894 if (!migrate_use_compression()) {
895 return;
896 }
897 thread_count = migrate_compress_threads();
898 for (idx = 0; idx < thread_count; idx++) {
899 if (!comp_param[idx].done) {
900 qemu_mutex_lock(comp_done_lock);
901 while (!comp_param[idx].done && !quit_comp_thread) {
902 qemu_cond_wait(comp_done_cond, comp_done_lock);
903 }
904 qemu_mutex_unlock(comp_done_lock);
905 }
906 if (!quit_comp_thread) {
907 len = qemu_put_qemu_file(f, comp_param[idx].file);
908 bytes_transferred += len;
909 }
910 }
911 }
912
913 static inline void set_compress_params(CompressParam *param, RAMBlock *block,
914 ram_addr_t offset)
915 {
916 param->block = block;
917 param->offset = offset;
918 }
919
920 static int compress_page_with_multi_thread(QEMUFile *f, RAMBlock *block,
921 ram_addr_t offset,
922 uint64_t *bytes_transferred)
923 {
924 int idx, thread_count, bytes_xmit = -1, pages = -1;
925
926 thread_count = migrate_compress_threads();
927 qemu_mutex_lock(comp_done_lock);
928 while (true) {
929 for (idx = 0; idx < thread_count; idx++) {
930 if (comp_param[idx].done) {
931 bytes_xmit = qemu_put_qemu_file(f, comp_param[idx].file);
932 set_compress_params(&comp_param[idx], block, offset);
933 start_compression(&comp_param[idx]);
934 pages = 1;
935 acct_info.norm_pages++;
936 *bytes_transferred += bytes_xmit;
937 break;
938 }
939 }
940 if (pages > 0) {
941 break;
942 } else {
943 qemu_cond_wait(comp_done_cond, comp_done_lock);
944 }
945 }
946 qemu_mutex_unlock(comp_done_lock);
947
948 return pages;
949 }
950
951 /**
952 * ram_save_compressed_page: compress the given page and send it to the stream
953 *
954 * Returns: Number of pages written.
955 *
956 * @f: QEMUFile where to send the data
957 * @block: block that contains the page we want to send
958 * @offset: offset inside the block for the page
959 * @last_stage: if we are at the completion stage
960 * @bytes_transferred: increase it with the number of transferred bytes
961 */
962 static int ram_save_compressed_page(QEMUFile *f, RAMBlock *block,
963 ram_addr_t offset, bool last_stage,
964 uint64_t *bytes_transferred)
965 {
966 int pages = -1;
967 uint64_t bytes_xmit;
968 MemoryRegion *mr = block->mr;
969 uint8_t *p;
970 int ret;
971
972 p = memory_region_get_ram_ptr(mr) + offset;
973
974 bytes_xmit = 0;
975 ret = ram_control_save_page(f, block->offset,
976 offset, TARGET_PAGE_SIZE, &bytes_xmit);
977 if (bytes_xmit) {
978 *bytes_transferred += bytes_xmit;
979 pages = 1;
980 }
981 if (block == last_sent_block) {
982 offset |= RAM_SAVE_FLAG_CONTINUE;
983 }
984 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
985 if (ret != RAM_SAVE_CONTROL_DELAYED) {
986 if (bytes_xmit > 0) {
987 acct_info.norm_pages++;
988 } else if (bytes_xmit == 0) {
989 acct_info.dup_pages++;
990 }
991 }
992 } else {
993 /* When starting the process of a new block, the first page of
994 * the block should be sent out before other pages in the same
995 * block, and all the pages in last block should have been sent
996 * out, keeping this order is important, because the 'cont' flag
997 * is used to avoid resending the block name.
998 */
999 if (block != last_sent_block) {
1000 flush_compressed_data(f);
1001 pages = save_zero_page(f, block, offset, p, bytes_transferred);
1002 if (pages == -1) {
1003 set_compress_params(&comp_param[0], block, offset);
1004 /* Use the qemu thread to compress the data to make sure the
1005 * first page is sent out before other pages
1006 */
1007 bytes_xmit = do_compress_ram_page(&comp_param[0]);
1008 acct_info.norm_pages++;
1009 qemu_put_qemu_file(f, comp_param[0].file);
1010 *bytes_transferred += bytes_xmit;
1011 pages = 1;
1012 }
1013 } else {
1014 pages = save_zero_page(f, block, offset, p, bytes_transferred);
1015 if (pages == -1) {
1016 pages = compress_page_with_multi_thread(f, block, offset,
1017 bytes_transferred);
1018 }
1019 }
1020 }
1021
1022 return pages;
1023 }
1024
1025 /**
1026 * ram_find_and_save_block: Finds a dirty page and sends it to f
1027 *
1028 * Called within an RCU critical section.
1029 *
1030 * Returns: The number of pages written
1031 * 0 means no dirty pages
1032 *
1033 * @f: QEMUFile where to send the data
1034 * @last_stage: if we are at the completion stage
1035 * @bytes_transferred: increase it with the number of transferred bytes
1036 */
1037
1038 static int ram_find_and_save_block(QEMUFile *f, bool last_stage,
1039 uint64_t *bytes_transferred)
1040 {
1041 RAMBlock *block = last_seen_block;
1042 ram_addr_t offset = last_offset;
1043 bool complete_round = false;
1044 int pages = 0;
1045 MemoryRegion *mr;
1046
1047 if (!block)
1048 block = QLIST_FIRST_RCU(&ram_list.blocks);
1049
1050 while (true) {
1051 mr = block->mr;
1052 offset = migration_bitmap_find_and_reset_dirty(mr, offset);
1053 if (complete_round && block == last_seen_block &&
1054 offset >= last_offset) {
1055 break;
1056 }
1057 if (offset >= block->used_length) {
1058 offset = 0;
1059 block = QLIST_NEXT_RCU(block, next);
1060 if (!block) {
1061 block = QLIST_FIRST_RCU(&ram_list.blocks);
1062 complete_round = true;
1063 ram_bulk_stage = false;
1064 }
1065 } else {
1066 if (migrate_use_compression()) {
1067 pages = ram_save_compressed_page(f, block, offset, last_stage,
1068 bytes_transferred);
1069 } else {
1070 pages = ram_save_page(f, block, offset, last_stage,
1071 bytes_transferred);
1072 }
1073
1074 /* if page is unmodified, continue to the next */
1075 if (pages > 0) {
1076 last_sent_block = block;
1077 break;
1078 }
1079 }
1080 }
1081
1082 last_seen_block = block;
1083 last_offset = offset;
1084
1085 return pages;
1086 }
1087
1088 void acct_update_position(QEMUFile *f, size_t size, bool zero)
1089 {
1090 uint64_t pages = size / TARGET_PAGE_SIZE;
1091 if (zero) {
1092 acct_info.dup_pages += pages;
1093 } else {
1094 acct_info.norm_pages += pages;
1095 bytes_transferred += size;
1096 qemu_update_position(f, size);
1097 }
1098 }
1099
1100 static ram_addr_t ram_save_remaining(void)
1101 {
1102 return migration_dirty_pages;
1103 }
1104
1105 uint64_t ram_bytes_remaining(void)
1106 {
1107 return ram_save_remaining() * TARGET_PAGE_SIZE;
1108 }
1109
1110 uint64_t ram_bytes_transferred(void)
1111 {
1112 return bytes_transferred;
1113 }
1114
1115 uint64_t ram_bytes_total(void)
1116 {
1117 RAMBlock *block;
1118 uint64_t total = 0;
1119
1120 rcu_read_lock();
1121 QLIST_FOREACH_RCU(block, &ram_list.blocks, next)
1122 total += block->used_length;
1123 rcu_read_unlock();
1124 return total;
1125 }
1126
1127 void free_xbzrle_decoded_buf(void)
1128 {
1129 g_free(xbzrle_decoded_buf);
1130 xbzrle_decoded_buf = NULL;
1131 }
1132
1133 static void migration_end(void)
1134 {
1135 if (migration_bitmap) {
1136 memory_global_dirty_log_stop();
1137 g_free(migration_bitmap);
1138 migration_bitmap = NULL;
1139 }
1140
1141 XBZRLE_cache_lock();
1142 if (XBZRLE.cache) {
1143 cache_fini(XBZRLE.cache);
1144 g_free(XBZRLE.encoded_buf);
1145 g_free(XBZRLE.current_buf);
1146 XBZRLE.cache = NULL;
1147 XBZRLE.encoded_buf = NULL;
1148 XBZRLE.current_buf = NULL;
1149 }
1150 XBZRLE_cache_unlock();
1151 }
1152
1153 static void ram_migration_cancel(void *opaque)
1154 {
1155 migration_end();
1156 }
1157
1158 static void reset_ram_globals(void)
1159 {
1160 last_seen_block = NULL;
1161 last_sent_block = NULL;
1162 last_offset = 0;
1163 last_version = ram_list.version;
1164 ram_bulk_stage = true;
1165 }
1166
1167 #define MAX_WAIT 50 /* ms, half buffered_file limit */
1168
1169
1170 /* Each of ram_save_setup, ram_save_iterate and ram_save_complete has
1171 * long-running RCU critical section. When rcu-reclaims in the code
1172 * start to become numerous it will be necessary to reduce the
1173 * granularity of these critical sections.
1174 */
1175
1176 static int ram_save_setup(QEMUFile *f, void *opaque)
1177 {
1178 RAMBlock *block;
1179 int64_t ram_bitmap_pages; /* Size of bitmap in pages, including gaps */
1180
1181 mig_throttle_on = false;
1182 dirty_rate_high_cnt = 0;
1183 bitmap_sync_count = 0;
1184 migration_bitmap_sync_init();
1185
1186 if (migrate_use_xbzrle()) {
1187 XBZRLE_cache_lock();
1188 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
1189 TARGET_PAGE_SIZE,
1190 TARGET_PAGE_SIZE);
1191 if (!XBZRLE.cache) {
1192 XBZRLE_cache_unlock();
1193 error_report("Error creating cache");
1194 return -1;
1195 }
1196 XBZRLE_cache_unlock();
1197
1198 /* We prefer not to abort if there is no memory */
1199 XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
1200 if (!XBZRLE.encoded_buf) {
1201 error_report("Error allocating encoded_buf");
1202 return -1;
1203 }
1204
1205 XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
1206 if (!XBZRLE.current_buf) {
1207 error_report("Error allocating current_buf");
1208 g_free(XBZRLE.encoded_buf);
1209 XBZRLE.encoded_buf = NULL;
1210 return -1;
1211 }
1212
1213 acct_clear();
1214 }
1215
1216 /* iothread lock needed for ram_list.dirty_memory[] */
1217 qemu_mutex_lock_iothread();
1218 qemu_mutex_lock_ramlist();
1219 rcu_read_lock();
1220 bytes_transferred = 0;
1221 reset_ram_globals();
1222
1223 ram_bitmap_pages = last_ram_offset() >> TARGET_PAGE_BITS;
1224 migration_bitmap = bitmap_new(ram_bitmap_pages);
1225 bitmap_set(migration_bitmap, 0, ram_bitmap_pages);
1226
1227 /*
1228 * Count the total number of pages used by ram blocks not including any
1229 * gaps due to alignment or unplugs.
1230 */
1231 migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS;
1232
1233 memory_global_dirty_log_start();
1234 migration_bitmap_sync();
1235 qemu_mutex_unlock_ramlist();
1236 qemu_mutex_unlock_iothread();
1237
1238 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
1239
1240 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1241 qemu_put_byte(f, strlen(block->idstr));
1242 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
1243 qemu_put_be64(f, block->used_length);
1244 }
1245
1246 rcu_read_unlock();
1247
1248 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
1249 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
1250
1251 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1252
1253 return 0;
1254 }
1255
1256 static int ram_save_iterate(QEMUFile *f, void *opaque)
1257 {
1258 int ret;
1259 int i;
1260 int64_t t0;
1261 int pages_sent = 0;
1262
1263 rcu_read_lock();
1264 if (ram_list.version != last_version) {
1265 reset_ram_globals();
1266 }
1267
1268 /* Read version before ram_list.blocks */
1269 smp_rmb();
1270
1271 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
1272
1273 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1274 i = 0;
1275 while ((ret = qemu_file_rate_limit(f)) == 0) {
1276 int pages;
1277
1278 pages = ram_find_and_save_block(f, false, &bytes_transferred);
1279 /* no more pages to sent */
1280 if (pages == 0) {
1281 break;
1282 }
1283 pages_sent += pages;
1284 acct_info.iterations++;
1285 check_guest_throttling();
1286 /* we want to check in the 1st loop, just in case it was the 1st time
1287 and we had to sync the dirty bitmap.
1288 qemu_get_clock_ns() is a bit expensive, so we only check each some
1289 iterations
1290 */
1291 if ((i & 63) == 0) {
1292 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
1293 if (t1 > MAX_WAIT) {
1294 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
1295 t1, i);
1296 break;
1297 }
1298 }
1299 i++;
1300 }
1301 flush_compressed_data(f);
1302 rcu_read_unlock();
1303
1304 /*
1305 * Must occur before EOS (or any QEMUFile operation)
1306 * because of RDMA protocol.
1307 */
1308 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
1309
1310 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1311 bytes_transferred += 8;
1312
1313 ret = qemu_file_get_error(f);
1314 if (ret < 0) {
1315 return ret;
1316 }
1317
1318 return pages_sent;
1319 }
1320
1321 /* Called with iothread lock */
1322 static int ram_save_complete(QEMUFile *f, void *opaque)
1323 {
1324 rcu_read_lock();
1325
1326 migration_bitmap_sync();
1327
1328 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
1329
1330 /* try transferring iterative blocks of memory */
1331
1332 /* flush all remaining blocks regardless of rate limiting */
1333 while (true) {
1334 int pages;
1335
1336 pages = ram_find_and_save_block(f, true, &bytes_transferred);
1337 /* no more blocks to sent */
1338 if (pages == 0) {
1339 break;
1340 }
1341 }
1342
1343 flush_compressed_data(f);
1344 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
1345 migration_end();
1346
1347 rcu_read_unlock();
1348 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1349
1350 return 0;
1351 }
1352
1353 static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
1354 {
1355 uint64_t remaining_size;
1356
1357 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
1358
1359 if (remaining_size < max_size) {
1360 qemu_mutex_lock_iothread();
1361 rcu_read_lock();
1362 migration_bitmap_sync();
1363 rcu_read_unlock();
1364 qemu_mutex_unlock_iothread();
1365 remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
1366 }
1367 return remaining_size;
1368 }
1369
1370 static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
1371 {
1372 unsigned int xh_len;
1373 int xh_flags;
1374
1375 if (!xbzrle_decoded_buf) {
1376 xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE);
1377 }
1378
1379 /* extract RLE header */
1380 xh_flags = qemu_get_byte(f);
1381 xh_len = qemu_get_be16(f);
1382
1383 if (xh_flags != ENCODING_FLAG_XBZRLE) {
1384 error_report("Failed to load XBZRLE page - wrong compression!");
1385 return -1;
1386 }
1387
1388 if (xh_len > TARGET_PAGE_SIZE) {
1389 error_report("Failed to load XBZRLE page - len overflow!");
1390 return -1;
1391 }
1392 /* load data and decode */
1393 qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
1394
1395 /* decode RLE */
1396 if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
1397 TARGET_PAGE_SIZE) == -1) {
1398 error_report("Failed to load XBZRLE page - decode error!");
1399 return -1;
1400 }
1401
1402 return 0;
1403 }
1404
1405 /* Must be called from within a rcu critical section.
1406 * Returns a pointer from within the RCU-protected ram_list.
1407 */
1408 static inline void *host_from_stream_offset(QEMUFile *f,
1409 ram_addr_t offset,
1410 int flags)
1411 {
1412 static RAMBlock *block = NULL;
1413 char id[256];
1414 uint8_t len;
1415
1416 if (flags & RAM_SAVE_FLAG_CONTINUE) {
1417 if (!block || block->max_length <= offset) {
1418 error_report("Ack, bad migration stream!");
1419 return NULL;
1420 }
1421
1422 return memory_region_get_ram_ptr(block->mr) + offset;
1423 }
1424
1425 len = qemu_get_byte(f);
1426 qemu_get_buffer(f, (uint8_t *)id, len);
1427 id[len] = 0;
1428
1429 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1430 if (!strncmp(id, block->idstr, sizeof(id)) &&
1431 block->max_length > offset) {
1432 return memory_region_get_ram_ptr(block->mr) + offset;
1433 }
1434 }
1435
1436 error_report("Can't find block %s!", id);
1437 return NULL;
1438 }
1439
1440 /*
1441 * If a page (or a whole RDMA chunk) has been
1442 * determined to be zero, then zap it.
1443 */
1444 void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
1445 {
1446 if (ch != 0 || !is_zero_range(host, size)) {
1447 memset(host, ch, size);
1448 }
1449 }
1450
1451 static void *do_data_decompress(void *opaque)
1452 {
1453 while (!quit_decomp_thread) {
1454 /* To be done */
1455 }
1456
1457 return NULL;
1458 }
1459
1460 void migrate_decompress_threads_create(void)
1461 {
1462 int i, thread_count;
1463
1464 thread_count = migrate_decompress_threads();
1465 decompress_threads = g_new0(QemuThread, thread_count);
1466 decomp_param = g_new0(DecompressParam, thread_count);
1467 compressed_data_buf = g_malloc0(compressBound(TARGET_PAGE_SIZE));
1468 quit_decomp_thread = false;
1469 for (i = 0; i < thread_count; i++) {
1470 qemu_mutex_init(&decomp_param[i].mutex);
1471 qemu_cond_init(&decomp_param[i].cond);
1472 decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE));
1473 qemu_thread_create(decompress_threads + i, "decompress",
1474 do_data_decompress, decomp_param + i,
1475 QEMU_THREAD_JOINABLE);
1476 }
1477 }
1478
1479 void migrate_decompress_threads_join(void)
1480 {
1481 int i, thread_count;
1482
1483 quit_decomp_thread = true;
1484 thread_count = migrate_decompress_threads();
1485 for (i = 0; i < thread_count; i++) {
1486 qemu_thread_join(decompress_threads + i);
1487 qemu_mutex_destroy(&decomp_param[i].mutex);
1488 qemu_cond_destroy(&decomp_param[i].cond);
1489 g_free(decomp_param[i].compbuf);
1490 }
1491 g_free(decompress_threads);
1492 g_free(decomp_param);
1493 g_free(compressed_data_buf);
1494 decompress_threads = NULL;
1495 decomp_param = NULL;
1496 compressed_data_buf = NULL;
1497 }
1498
1499 static void decompress_data_with_multi_threads(uint8_t *compbuf,
1500 void *host, int len)
1501 {
1502 /* To be done */
1503 }
1504
1505 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1506 {
1507 int flags = 0, ret = 0;
1508 static uint64_t seq_iter;
1509 int len = 0;
1510
1511 seq_iter++;
1512
1513 if (version_id != 4) {
1514 ret = -EINVAL;
1515 }
1516
1517 /* This RCU critical section can be very long running.
1518 * When RCU reclaims in the code start to become numerous,
1519 * it will be necessary to reduce the granularity of this
1520 * critical section.
1521 */
1522 rcu_read_lock();
1523 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) {
1524 ram_addr_t addr, total_ram_bytes;
1525 void *host;
1526 uint8_t ch;
1527
1528 addr = qemu_get_be64(f);
1529 flags = addr & ~TARGET_PAGE_MASK;
1530 addr &= TARGET_PAGE_MASK;
1531
1532 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
1533 case RAM_SAVE_FLAG_MEM_SIZE:
1534 /* Synchronize RAM block list */
1535 total_ram_bytes = addr;
1536 while (!ret && total_ram_bytes) {
1537 RAMBlock *block;
1538 uint8_t len;
1539 char id[256];
1540 ram_addr_t length;
1541
1542 len = qemu_get_byte(f);
1543 qemu_get_buffer(f, (uint8_t *)id, len);
1544 id[len] = 0;
1545 length = qemu_get_be64(f);
1546
1547 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1548 if (!strncmp(id, block->idstr, sizeof(id))) {
1549 if (length != block->used_length) {
1550 Error *local_err = NULL;
1551
1552 ret = qemu_ram_resize(block->offset, length, &local_err);
1553 if (local_err) {
1554 error_report_err(local_err);
1555 }
1556 }
1557 break;
1558 }
1559 }
1560
1561 if (!block) {
1562 error_report("Unknown ramblock \"%s\", cannot "
1563 "accept migration", id);
1564 ret = -EINVAL;
1565 }
1566
1567 total_ram_bytes -= length;
1568 }
1569 break;
1570 case RAM_SAVE_FLAG_COMPRESS:
1571 host = host_from_stream_offset(f, addr, flags);
1572 if (!host) {
1573 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
1574 ret = -EINVAL;
1575 break;
1576 }
1577 ch = qemu_get_byte(f);
1578 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
1579 break;
1580 case RAM_SAVE_FLAG_PAGE:
1581 host = host_from_stream_offset(f, addr, flags);
1582 if (!host) {
1583 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
1584 ret = -EINVAL;
1585 break;
1586 }
1587 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
1588 break;
1589 case RAM_SAVE_FLAG_COMPRESS_PAGE:
1590 host = host_from_stream_offset(f, addr, flags);
1591 if (!host) {
1592 error_report("Invalid RAM offset " RAM_ADDR_FMT, addr);
1593 ret = -EINVAL;
1594 break;
1595 }
1596
1597 len = qemu_get_be32(f);
1598 if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) {
1599 error_report("Invalid compressed data length: %d", len);
1600 ret = -EINVAL;
1601 break;
1602 }
1603 qemu_get_buffer(f, compressed_data_buf, len);
1604 decompress_data_with_multi_threads(compressed_data_buf, host, len);
1605 break;
1606 case RAM_SAVE_FLAG_XBZRLE:
1607 host = host_from_stream_offset(f, addr, flags);
1608 if (!host) {
1609 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
1610 ret = -EINVAL;
1611 break;
1612 }
1613 if (load_xbzrle(f, addr, host) < 0) {
1614 error_report("Failed to decompress XBZRLE page at "
1615 RAM_ADDR_FMT, addr);
1616 ret = -EINVAL;
1617 break;
1618 }
1619 break;
1620 case RAM_SAVE_FLAG_EOS:
1621 /* normal exit */
1622 break;
1623 default:
1624 if (flags & RAM_SAVE_FLAG_HOOK) {
1625 ram_control_load_hook(f, flags);
1626 } else {
1627 error_report("Unknown combination of migration flags: %#x",
1628 flags);
1629 ret = -EINVAL;
1630 }
1631 }
1632 if (!ret) {
1633 ret = qemu_file_get_error(f);
1634 }
1635 }
1636
1637 rcu_read_unlock();
1638 DPRINTF("Completed load of VM with exit code %d seq iteration "
1639 "%" PRIu64 "\n", ret, seq_iter);
1640 return ret;
1641 }
1642
1643 static SaveVMHandlers savevm_ram_handlers = {
1644 .save_live_setup = ram_save_setup,
1645 .save_live_iterate = ram_save_iterate,
1646 .save_live_complete = ram_save_complete,
1647 .save_live_pending = ram_save_pending,
1648 .load_state = ram_load,
1649 .cancel = ram_migration_cancel,
1650 };
1651
1652 void ram_mig_init(void)
1653 {
1654 qemu_mutex_init(&XBZRLE.lock);
1655 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
1656 }
1657
1658 struct soundhw {
1659 const char *name;
1660 const char *descr;
1661 int enabled;
1662 int isa;
1663 union {
1664 int (*init_isa) (ISABus *bus);
1665 int (*init_pci) (PCIBus *bus);
1666 } init;
1667 };
1668
1669 static struct soundhw soundhw[9];
1670 static int soundhw_count;
1671
1672 void isa_register_soundhw(const char *name, const char *descr,
1673 int (*init_isa)(ISABus *bus))
1674 {
1675 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1676 soundhw[soundhw_count].name = name;
1677 soundhw[soundhw_count].descr = descr;
1678 soundhw[soundhw_count].isa = 1;
1679 soundhw[soundhw_count].init.init_isa = init_isa;
1680 soundhw_count++;
1681 }
1682
1683 void pci_register_soundhw(const char *name, const char *descr,
1684 int (*init_pci)(PCIBus *bus))
1685 {
1686 assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
1687 soundhw[soundhw_count].name = name;
1688 soundhw[soundhw_count].descr = descr;
1689 soundhw[soundhw_count].isa = 0;
1690 soundhw[soundhw_count].init.init_pci = init_pci;
1691 soundhw_count++;
1692 }
1693
1694 void select_soundhw(const char *optarg)
1695 {
1696 struct soundhw *c;
1697
1698 if (is_help_option(optarg)) {
1699 show_valid_cards:
1700
1701 if (soundhw_count) {
1702 printf("Valid sound card names (comma separated):\n");
1703 for (c = soundhw; c->name; ++c) {
1704 printf ("%-11s %s\n", c->name, c->descr);
1705 }
1706 printf("\n-soundhw all will enable all of the above\n");
1707 } else {
1708 printf("Machine has no user-selectable audio hardware "
1709 "(it may or may not have always-present audio hardware).\n");
1710 }
1711 exit(!is_help_option(optarg));
1712 }
1713 else {
1714 size_t l;
1715 const char *p;
1716 char *e;
1717 int bad_card = 0;
1718
1719 if (!strcmp(optarg, "all")) {
1720 for (c = soundhw; c->name; ++c) {
1721 c->enabled = 1;
1722 }
1723 return;
1724 }
1725
1726 p = optarg;
1727 while (*p) {
1728 e = strchr(p, ',');
1729 l = !e ? strlen(p) : (size_t) (e - p);
1730
1731 for (c = soundhw; c->name; ++c) {
1732 if (!strncmp(c->name, p, l) && !c->name[l]) {
1733 c->enabled = 1;
1734 break;
1735 }
1736 }
1737
1738 if (!c->name) {
1739 if (l > 80) {
1740 error_report("Unknown sound card name (too big to show)");
1741 }
1742 else {
1743 error_report("Unknown sound card name `%.*s'",
1744 (int) l, p);
1745 }
1746 bad_card = 1;
1747 }
1748 p += l + (e != NULL);
1749 }
1750
1751 if (bad_card) {
1752 goto show_valid_cards;
1753 }
1754 }
1755 }
1756
1757 void audio_init(void)
1758 {
1759 struct soundhw *c;
1760 ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
1761 PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
1762
1763 for (c = soundhw; c->name; ++c) {
1764 if (c->enabled) {
1765 if (c->isa) {
1766 if (!isa_bus) {
1767 error_report("ISA bus not available for %s", c->name);
1768 exit(1);
1769 }
1770 c->init.init_isa(isa_bus);
1771 } else {
1772 if (!pci_bus) {
1773 error_report("PCI bus not available for %s", c->name);
1774 exit(1);
1775 }
1776 c->init.init_pci(pci_bus);
1777 }
1778 }
1779 }
1780 }
1781
1782 int qemu_uuid_parse(const char *str, uint8_t *uuid)
1783 {
1784 int ret;
1785
1786 if (strlen(str) != 36) {
1787 return -1;
1788 }
1789
1790 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1791 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1792 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1793 &uuid[15]);
1794
1795 if (ret != 16) {
1796 return -1;
1797 }
1798 return 0;
1799 }
1800
1801 void do_acpitable_option(const QemuOpts *opts)
1802 {
1803 #ifdef TARGET_I386
1804 Error *err = NULL;
1805
1806 acpi_table_add(opts, &err);
1807 if (err) {
1808 error_report("Wrong acpi table provided: %s",
1809 error_get_pretty(err));
1810 error_free(err);
1811 exit(1);
1812 }
1813 #endif
1814 }
1815
1816 void do_smbios_option(QemuOpts *opts)
1817 {
1818 #ifdef TARGET_I386
1819 smbios_entry_add(opts);
1820 #endif
1821 }
1822
1823 void cpudef_init(void)
1824 {
1825 #if defined(cpudef_setup)
1826 cpudef_setup(); /* parse cpu definitions in target config file */
1827 #endif
1828 }
1829
1830 int kvm_available(void)
1831 {
1832 #ifdef CONFIG_KVM
1833 return 1;
1834 #else
1835 return 0;
1836 #endif
1837 }
1838
1839 int xen_available(void)
1840 {
1841 #ifdef CONFIG_XEN
1842 return 1;
1843 #else
1844 return 0;
1845 #endif
1846 }
1847
1848
1849 TargetInfo *qmp_query_target(Error **errp)
1850 {
1851 TargetInfo *info = g_malloc0(sizeof(*info));
1852
1853 info->arch = g_strdup(TARGET_NAME);
1854
1855 return info;
1856 }
1857
1858 /* Stub function that's gets run on the vcpu when its brought out of the
1859 VM to run inside qemu via async_run_on_cpu()*/
1860 static void mig_sleep_cpu(void *opq)
1861 {
1862 qemu_mutex_unlock_iothread();
1863 g_usleep(30*1000);
1864 qemu_mutex_lock_iothread();
1865 }
1866
1867 /* To reduce the dirty rate explicitly disallow the VCPUs from spending
1868 much time in the VM. The migration thread will try to catchup.
1869 Workload will experience a performance drop.
1870 */
1871 static void mig_throttle_guest_down(void)
1872 {
1873 CPUState *cpu;
1874
1875 qemu_mutex_lock_iothread();
1876 CPU_FOREACH(cpu) {
1877 async_run_on_cpu(cpu, mig_sleep_cpu, NULL);
1878 }
1879 qemu_mutex_unlock_iothread();
1880 }
1881
1882 static void check_guest_throttling(void)
1883 {
1884 static int64_t t0;
1885 int64_t t1;
1886
1887 if (!mig_throttle_on) {
1888 return;
1889 }
1890
1891 if (!t0) {
1892 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1893 return;
1894 }
1895
1896 t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1897
1898 /* If it has been more than 40 ms since the last time the guest
1899 * was throttled then do it again.
1900 */
1901 if (40 < (t1-t0)/1000000) {
1902 mig_throttle_guest_down();
1903 t0 = t1;
1904 }
1905 }