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