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