savevm: Factorize ram globals reset in its own function
[qemu/ar7.git] / arch_init.c
blob6b9f949db3a0f24164f85548ec85e8dc86af8a9d
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <stdint.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #ifndef _WIN32
28 #include <sys/types.h>
29 #include <sys/mman.h>
30 #endif
31 #include "config.h"
32 #include "monitor.h"
33 #include "sysemu.h"
34 #include "arch_init.h"
35 #include "audio/audio.h"
36 #include "hw/pc.h"
37 #include "hw/pci.h"
38 #include "hw/audiodev.h"
39 #include "kvm.h"
40 #include "migration.h"
41 #include "net.h"
42 #include "gdbstub.h"
43 #include "hw/smbios.h"
44 #include "exec-memory.h"
45 #include "hw/pcspk.h"
46 #include "qemu/page_cache.h"
47 #include "qmp-commands.h"
49 #ifdef DEBUG_ARCH_INIT
50 #define DPRINTF(fmt, ...) \
51 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
52 #else
53 #define DPRINTF(fmt, ...) \
54 do { } while (0)
55 #endif
57 #ifdef TARGET_SPARC
58 int graphic_width = 1024;
59 int graphic_height = 768;
60 int graphic_depth = 8;
61 #else
62 int graphic_width = 800;
63 int graphic_height = 600;
64 int graphic_depth = 15;
65 #endif
68 #if defined(TARGET_ALPHA)
69 #define QEMU_ARCH QEMU_ARCH_ALPHA
70 #elif defined(TARGET_ARM)
71 #define QEMU_ARCH QEMU_ARCH_ARM
72 #elif defined(TARGET_CRIS)
73 #define QEMU_ARCH QEMU_ARCH_CRIS
74 #elif defined(TARGET_I386)
75 #define QEMU_ARCH QEMU_ARCH_I386
76 #elif defined(TARGET_M68K)
77 #define QEMU_ARCH QEMU_ARCH_M68K
78 #elif defined(TARGET_LM32)
79 #define QEMU_ARCH QEMU_ARCH_LM32
80 #elif defined(TARGET_MICROBLAZE)
81 #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
82 #elif defined(TARGET_MIPS)
83 #define QEMU_ARCH QEMU_ARCH_MIPS
84 #elif defined(TARGET_OPENRISC)
85 #define QEMU_ARCH QEMU_ARCH_OPENRISC
86 #elif defined(TARGET_PPC)
87 #define QEMU_ARCH QEMU_ARCH_PPC
88 #elif defined(TARGET_S390X)
89 #define QEMU_ARCH QEMU_ARCH_S390X
90 #elif defined(TARGET_SH4)
91 #define QEMU_ARCH QEMU_ARCH_SH4
92 #elif defined(TARGET_SPARC)
93 #define QEMU_ARCH QEMU_ARCH_SPARC
94 #elif defined(TARGET_XTENSA)
95 #define QEMU_ARCH QEMU_ARCH_XTENSA
96 #elif defined(TARGET_UNICORE32)
97 #define QEMU_ARCH QEMU_ARCH_UNICORE32
98 #endif
100 const uint32_t arch_type = QEMU_ARCH;
102 /***********************************************************/
103 /* ram save/restore */
105 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
106 #define RAM_SAVE_FLAG_COMPRESS 0x02
107 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
108 #define RAM_SAVE_FLAG_PAGE 0x08
109 #define RAM_SAVE_FLAG_EOS 0x10
110 #define RAM_SAVE_FLAG_CONTINUE 0x20
111 #define RAM_SAVE_FLAG_XBZRLE 0x40
113 #ifdef __ALTIVEC__
114 #include <altivec.h>
115 #define VECTYPE vector unsigned char
116 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
117 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
118 /* altivec.h may redefine the bool macro as vector type.
119 * Reset it to POSIX semantics. */
120 #undef bool
121 #define bool _Bool
122 #elif defined __SSE2__
123 #include <emmintrin.h>
124 #define VECTYPE __m128i
125 #define SPLAT(p) _mm_set1_epi8(*(p))
126 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
127 #else
128 #define VECTYPE unsigned long
129 #define SPLAT(p) (*(p) * (~0UL / 255))
130 #define ALL_EQ(v1, v2) ((v1) == (v2))
131 #endif
134 static struct defconfig_file {
135 const char *filename;
136 /* Indicates it is an user config file (disabled by -no-user-config) */
137 bool userconfig;
138 } default_config_files[] = {
139 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
140 { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true },
141 { NULL }, /* end of list */
145 int qemu_read_default_config_files(bool userconfig)
147 int ret;
148 struct defconfig_file *f;
150 for (f = default_config_files; f->filename; f++) {
151 if (!userconfig && f->userconfig) {
152 continue;
154 ret = qemu_read_config_file(f->filename);
155 if (ret < 0 && ret != -ENOENT) {
156 return ret;
160 return 0;
163 static int is_dup_page(uint8_t *page)
165 VECTYPE *p = (VECTYPE *)page;
166 VECTYPE val = SPLAT(page);
167 int i;
169 for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
170 if (!ALL_EQ(val, p[i])) {
171 return 0;
175 return 1;
178 /* struct contains XBZRLE cache and a static page
179 used by the compression */
180 static struct {
181 /* buffer used for XBZRLE encoding */
182 uint8_t *encoded_buf;
183 /* buffer for storing page content */
184 uint8_t *current_buf;
185 /* buffer used for XBZRLE decoding */
186 uint8_t *decoded_buf;
187 /* Cache for XBZRLE */
188 PageCache *cache;
189 } XBZRLE = {
190 .encoded_buf = NULL,
191 .current_buf = NULL,
192 .decoded_buf = NULL,
193 .cache = NULL,
197 int64_t xbzrle_cache_resize(int64_t new_size)
199 if (XBZRLE.cache != NULL) {
200 return cache_resize(XBZRLE.cache, new_size / TARGET_PAGE_SIZE) *
201 TARGET_PAGE_SIZE;
203 return pow2floor(new_size);
206 /* accounting for migration statistics */
207 typedef struct AccountingInfo {
208 uint64_t dup_pages;
209 uint64_t norm_pages;
210 uint64_t iterations;
211 uint64_t xbzrle_bytes;
212 uint64_t xbzrle_pages;
213 uint64_t xbzrle_cache_miss;
214 uint64_t xbzrle_overflows;
215 } AccountingInfo;
217 static AccountingInfo acct_info;
219 static void acct_clear(void)
221 memset(&acct_info, 0, sizeof(acct_info));
224 uint64_t dup_mig_bytes_transferred(void)
226 return acct_info.dup_pages * TARGET_PAGE_SIZE;
229 uint64_t dup_mig_pages_transferred(void)
231 return acct_info.dup_pages;
234 uint64_t norm_mig_bytes_transferred(void)
236 return acct_info.norm_pages * TARGET_PAGE_SIZE;
239 uint64_t norm_mig_pages_transferred(void)
241 return acct_info.norm_pages;
244 uint64_t xbzrle_mig_bytes_transferred(void)
246 return acct_info.xbzrle_bytes;
249 uint64_t xbzrle_mig_pages_transferred(void)
251 return acct_info.xbzrle_pages;
254 uint64_t xbzrle_mig_pages_cache_miss(void)
256 return acct_info.xbzrle_cache_miss;
259 uint64_t xbzrle_mig_pages_overflow(void)
261 return acct_info.xbzrle_overflows;
264 static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
265 int cont, int flag)
267 qemu_put_be64(f, offset | cont | flag);
268 if (!cont) {
269 qemu_put_byte(f, strlen(block->idstr));
270 qemu_put_buffer(f, (uint8_t *)block->idstr,
271 strlen(block->idstr));
276 #define ENCODING_FLAG_XBZRLE 0x1
278 static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
279 ram_addr_t current_addr, RAMBlock *block,
280 ram_addr_t offset, int cont, bool last_stage)
282 int encoded_len = 0, bytes_sent = -1;
283 uint8_t *prev_cached_page;
285 if (!cache_is_cached(XBZRLE.cache, current_addr)) {
286 if (!last_stage) {
287 cache_insert(XBZRLE.cache, current_addr,
288 g_memdup(current_data, TARGET_PAGE_SIZE));
290 acct_info.xbzrle_cache_miss++;
291 return -1;
294 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
296 /* save current buffer into memory */
297 memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
299 /* XBZRLE encoding (if there is no overflow) */
300 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
301 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
302 TARGET_PAGE_SIZE);
303 if (encoded_len == 0) {
304 DPRINTF("Skipping unmodified page\n");
305 return 0;
306 } else if (encoded_len == -1) {
307 DPRINTF("Overflow\n");
308 acct_info.xbzrle_overflows++;
309 /* update data in the cache */
310 memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
311 return -1;
314 /* we need to update the data in the cache, in order to get the same data */
315 if (!last_stage) {
316 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
319 /* Send XBZRLE based compressed page */
320 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
321 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
322 qemu_put_be16(f, encoded_len);
323 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
324 bytes_sent = encoded_len + 1 + 2;
325 acct_info.xbzrle_pages++;
326 acct_info.xbzrle_bytes += bytes_sent;
328 return bytes_sent;
331 static RAMBlock *last_block;
332 static ram_addr_t last_offset;
335 * ram_save_block: Writes a page of memory to the stream f
337 * Returns: 0: if the page hasn't changed
338 * -1: if there are no more dirty pages
339 * n: the amount of bytes written in other case
342 static int ram_save_block(QEMUFile *f, bool last_stage)
344 RAMBlock *block = last_block;
345 ram_addr_t offset = last_offset;
346 int bytes_sent = -1;
347 MemoryRegion *mr;
348 ram_addr_t current_addr;
350 if (!block)
351 block = QLIST_FIRST(&ram_list.blocks);
353 do {
354 mr = block->mr;
355 if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
356 DIRTY_MEMORY_MIGRATION)) {
357 uint8_t *p;
358 int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
360 memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
361 DIRTY_MEMORY_MIGRATION);
363 p = memory_region_get_ram_ptr(mr) + offset;
365 if (is_dup_page(p)) {
366 acct_info.dup_pages++;
367 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
368 qemu_put_byte(f, *p);
369 bytes_sent = 1;
370 } else if (migrate_use_xbzrle()) {
371 current_addr = block->offset + offset;
372 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
373 offset, cont, last_stage);
374 if (!last_stage) {
375 p = get_cached_data(XBZRLE.cache, current_addr);
379 /* either we didn't send yet (we may have had XBZRLE overflow) */
380 if (bytes_sent == -1) {
381 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
382 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
383 bytes_sent = TARGET_PAGE_SIZE;
384 acct_info.norm_pages++;
387 /* if page is unmodified, continue to the next */
388 if (bytes_sent != 0) {
389 break;
393 offset += TARGET_PAGE_SIZE;
394 if (offset >= block->length) {
395 offset = 0;
396 block = QLIST_NEXT(block, next);
397 if (!block)
398 block = QLIST_FIRST(&ram_list.blocks);
400 } while (block != last_block || offset != last_offset);
402 last_block = block;
403 last_offset = offset;
405 return bytes_sent;
408 static uint64_t bytes_transferred;
410 static ram_addr_t ram_save_remaining(void)
412 return ram_list.dirty_pages;
415 uint64_t ram_bytes_remaining(void)
417 return ram_save_remaining() * TARGET_PAGE_SIZE;
420 uint64_t ram_bytes_transferred(void)
422 return bytes_transferred;
425 uint64_t ram_bytes_total(void)
427 RAMBlock *block;
428 uint64_t total = 0;
430 QLIST_FOREACH(block, &ram_list.blocks, next)
431 total += block->length;
433 return total;
436 static int block_compar(const void *a, const void *b)
438 RAMBlock * const *ablock = a;
439 RAMBlock * const *bblock = b;
441 return strcmp((*ablock)->idstr, (*bblock)->idstr);
444 static void sort_ram_list(void)
446 RAMBlock *block, *nblock, **blocks;
447 int n;
448 n = 0;
449 QLIST_FOREACH(block, &ram_list.blocks, next) {
450 ++n;
452 blocks = g_malloc(n * sizeof *blocks);
453 n = 0;
454 QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
455 blocks[n++] = block;
456 QLIST_REMOVE(block, next);
458 qsort(blocks, n, sizeof *blocks, block_compar);
459 while (--n >= 0) {
460 QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
462 g_free(blocks);
465 static void migration_end(void)
467 memory_global_dirty_log_stop();
469 if (migrate_use_xbzrle()) {
470 cache_fini(XBZRLE.cache);
471 g_free(XBZRLE.cache);
472 g_free(XBZRLE.encoded_buf);
473 g_free(XBZRLE.current_buf);
474 g_free(XBZRLE.decoded_buf);
475 XBZRLE.cache = NULL;
479 static void ram_migration_cancel(void *opaque)
481 migration_end();
485 static void reset_ram_globals(void)
487 last_block = NULL;
488 last_offset = 0;
489 sort_ram_list();
492 #define MAX_WAIT 50 /* ms, half buffered_file limit */
494 static int ram_save_setup(QEMUFile *f, void *opaque)
496 ram_addr_t addr;
497 RAMBlock *block;
499 bytes_transferred = 0;
500 reset_ram_globals();
502 if (migrate_use_xbzrle()) {
503 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
504 TARGET_PAGE_SIZE,
505 TARGET_PAGE_SIZE);
506 if (!XBZRLE.cache) {
507 DPRINTF("Error creating cache\n");
508 return -1;
510 XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
511 XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
512 acct_clear();
515 /* Make sure all dirty bits are set */
516 QLIST_FOREACH(block, &ram_list.blocks, next) {
517 for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
518 if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE,
519 DIRTY_MEMORY_MIGRATION)) {
520 memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE);
525 memory_global_dirty_log_start();
526 memory_global_sync_dirty_bitmap(get_system_memory());
528 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
530 QLIST_FOREACH(block, &ram_list.blocks, next) {
531 qemu_put_byte(f, strlen(block->idstr));
532 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
533 qemu_put_be64(f, block->length);
536 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
538 return 0;
541 static int ram_save_iterate(QEMUFile *f, void *opaque)
543 uint64_t bytes_transferred_last;
544 double bwidth = 0;
545 int ret;
546 int i;
547 uint64_t expected_downtime;
548 MigrationState *s = migrate_get_current();
550 bytes_transferred_last = bytes_transferred;
551 bwidth = qemu_get_clock_ns(rt_clock);
553 i = 0;
554 while ((ret = qemu_file_rate_limit(f)) == 0) {
555 int bytes_sent;
557 bytes_sent = ram_save_block(f, false);
558 /* no more blocks to sent */
559 if (bytes_sent < 0) {
560 break;
562 bytes_transferred += bytes_sent;
563 acct_info.iterations++;
564 /* we want to check in the 1st loop, just in case it was the 1st time
565 and we had to sync the dirty bitmap.
566 qemu_get_clock_ns() is a bit expensive, so we only check each some
567 iterations
569 if ((i & 63) == 0) {
570 uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000;
571 if (t1 > MAX_WAIT) {
572 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
573 t1, i);
574 break;
577 i++;
580 if (ret < 0) {
581 return ret;
584 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
585 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
587 /* if we haven't transferred anything this round, force
588 * expected_downtime to a very high value, but without
589 * crashing */
590 if (bwidth == 0) {
591 bwidth = 0.000001;
594 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
596 expected_downtime = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
597 DPRINTF("ram_save_live: expected(%" PRIu64 ") <= max(" PRIu64 ")?\n",
598 expected_downtime, migrate_max_downtime());
600 if (expected_downtime <= migrate_max_downtime()) {
601 memory_global_sync_dirty_bitmap(get_system_memory());
602 expected_downtime = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
603 s->expected_downtime = expected_downtime / 1000000; /* ns -> ms */
605 return expected_downtime <= migrate_max_downtime();
607 return 0;
610 static int ram_save_complete(QEMUFile *f, void *opaque)
612 memory_global_sync_dirty_bitmap(get_system_memory());
614 /* try transferring iterative blocks of memory */
616 /* flush all remaining blocks regardless of rate limiting */
617 while (true) {
618 int bytes_sent;
620 bytes_sent = ram_save_block(f, true);
621 /* no more blocks to sent */
622 if (bytes_sent < 0) {
623 break;
625 bytes_transferred += bytes_sent;
627 memory_global_dirty_log_stop();
629 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
631 return 0;
634 static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
636 int ret, rc = 0;
637 unsigned int xh_len;
638 int xh_flags;
640 if (!XBZRLE.decoded_buf) {
641 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
644 /* extract RLE header */
645 xh_flags = qemu_get_byte(f);
646 xh_len = qemu_get_be16(f);
648 if (xh_flags != ENCODING_FLAG_XBZRLE) {
649 fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
650 return -1;
653 if (xh_len > TARGET_PAGE_SIZE) {
654 fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
655 return -1;
657 /* load data and decode */
658 qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
660 /* decode RLE */
661 ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
662 TARGET_PAGE_SIZE);
663 if (ret == -1) {
664 fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
665 rc = -1;
666 } else if (ret > TARGET_PAGE_SIZE) {
667 fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
668 ret, TARGET_PAGE_SIZE);
669 abort();
672 return rc;
675 static inline void *host_from_stream_offset(QEMUFile *f,
676 ram_addr_t offset,
677 int flags)
679 static RAMBlock *block = NULL;
680 char id[256];
681 uint8_t len;
683 if (flags & RAM_SAVE_FLAG_CONTINUE) {
684 if (!block) {
685 fprintf(stderr, "Ack, bad migration stream!\n");
686 return NULL;
689 return memory_region_get_ram_ptr(block->mr) + offset;
692 len = qemu_get_byte(f);
693 qemu_get_buffer(f, (uint8_t *)id, len);
694 id[len] = 0;
696 QLIST_FOREACH(block, &ram_list.blocks, next) {
697 if (!strncmp(id, block->idstr, sizeof(id)))
698 return memory_region_get_ram_ptr(block->mr) + offset;
701 fprintf(stderr, "Can't find block %s!\n", id);
702 return NULL;
705 static int ram_load(QEMUFile *f, void *opaque, int version_id)
707 ram_addr_t addr;
708 int flags, ret = 0;
709 int error;
710 static uint64_t seq_iter;
712 seq_iter++;
714 if (version_id < 4 || version_id > 4) {
715 return -EINVAL;
718 do {
719 addr = qemu_get_be64(f);
721 flags = addr & ~TARGET_PAGE_MASK;
722 addr &= TARGET_PAGE_MASK;
724 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
725 if (version_id == 4) {
726 /* Synchronize RAM block list */
727 char id[256];
728 ram_addr_t length;
729 ram_addr_t total_ram_bytes = addr;
731 while (total_ram_bytes) {
732 RAMBlock *block;
733 uint8_t len;
735 len = qemu_get_byte(f);
736 qemu_get_buffer(f, (uint8_t *)id, len);
737 id[len] = 0;
738 length = qemu_get_be64(f);
740 QLIST_FOREACH(block, &ram_list.blocks, next) {
741 if (!strncmp(id, block->idstr, sizeof(id))) {
742 if (block->length != length) {
743 ret = -EINVAL;
744 goto done;
746 break;
750 if (!block) {
751 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
752 "accept migration\n", id);
753 ret = -EINVAL;
754 goto done;
757 total_ram_bytes -= length;
762 if (flags & RAM_SAVE_FLAG_COMPRESS) {
763 void *host;
764 uint8_t ch;
766 host = host_from_stream_offset(f, addr, flags);
767 if (!host) {
768 return -EINVAL;
771 ch = qemu_get_byte(f);
772 memset(host, ch, TARGET_PAGE_SIZE);
773 #ifndef _WIN32
774 if (ch == 0 &&
775 (!kvm_enabled() || kvm_has_sync_mmu())) {
776 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
778 #endif
779 } else if (flags & RAM_SAVE_FLAG_PAGE) {
780 void *host;
782 host = host_from_stream_offset(f, addr, flags);
783 if (!host) {
784 return -EINVAL;
787 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
788 } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
789 if (!migrate_use_xbzrle()) {
790 return -EINVAL;
792 void *host = host_from_stream_offset(f, addr, flags);
793 if (!host) {
794 return -EINVAL;
797 if (load_xbzrle(f, addr, host) < 0) {
798 ret = -EINVAL;
799 goto done;
802 error = qemu_file_get_error(f);
803 if (error) {
804 ret = error;
805 goto done;
807 } while (!(flags & RAM_SAVE_FLAG_EOS));
809 done:
810 DPRINTF("Completed load of VM with exit code %d seq iteration "
811 "%" PRIu64 "\n", ret, seq_iter);
812 return ret;
815 SaveVMHandlers savevm_ram_handlers = {
816 .save_live_setup = ram_save_setup,
817 .save_live_iterate = ram_save_iterate,
818 .save_live_complete = ram_save_complete,
819 .load_state = ram_load,
820 .cancel = ram_migration_cancel,
823 #ifdef HAS_AUDIO
824 struct soundhw {
825 const char *name;
826 const char *descr;
827 int enabled;
828 int isa;
829 union {
830 int (*init_isa) (ISABus *bus);
831 int (*init_pci) (PCIBus *bus);
832 } init;
835 static struct soundhw soundhw[] = {
836 #ifdef HAS_AUDIO_CHOICE
837 #ifdef CONFIG_PCSPK
839 "pcspk",
840 "PC speaker",
843 { .init_isa = pcspk_audio_init }
845 #endif
847 #ifdef CONFIG_SB16
849 "sb16",
850 "Creative Sound Blaster 16",
853 { .init_isa = SB16_init }
855 #endif
857 #ifdef CONFIG_CS4231A
859 "cs4231a",
860 "CS4231A",
863 { .init_isa = cs4231a_init }
865 #endif
867 #ifdef CONFIG_ADLIB
869 "adlib",
870 #ifdef HAS_YMF262
871 "Yamaha YMF262 (OPL3)",
872 #else
873 "Yamaha YM3812 (OPL2)",
874 #endif
877 { .init_isa = Adlib_init }
879 #endif
881 #ifdef CONFIG_GUS
883 "gus",
884 "Gravis Ultrasound GF1",
887 { .init_isa = GUS_init }
889 #endif
891 #ifdef CONFIG_AC97
893 "ac97",
894 "Intel 82801AA AC97 Audio",
897 { .init_pci = ac97_init }
899 #endif
901 #ifdef CONFIG_ES1370
903 "es1370",
904 "ENSONIQ AudioPCI ES1370",
907 { .init_pci = es1370_init }
909 #endif
911 #ifdef CONFIG_HDA
913 "hda",
914 "Intel HD Audio",
917 { .init_pci = intel_hda_and_codec_init }
919 #endif
921 #endif /* HAS_AUDIO_CHOICE */
923 { NULL, NULL, 0, 0, { NULL } }
926 void select_soundhw(const char *optarg)
928 struct soundhw *c;
930 if (is_help_option(optarg)) {
931 show_valid_cards:
933 #ifdef HAS_AUDIO_CHOICE
934 printf("Valid sound card names (comma separated):\n");
935 for (c = soundhw; c->name; ++c) {
936 printf ("%-11s %s\n", c->name, c->descr);
938 printf("\n-soundhw all will enable all of the above\n");
939 #else
940 printf("Machine has no user-selectable audio hardware "
941 "(it may or may not have always-present audio hardware).\n");
942 #endif
943 exit(!is_help_option(optarg));
945 else {
946 size_t l;
947 const char *p;
948 char *e;
949 int bad_card = 0;
951 if (!strcmp(optarg, "all")) {
952 for (c = soundhw; c->name; ++c) {
953 c->enabled = 1;
955 return;
958 p = optarg;
959 while (*p) {
960 e = strchr(p, ',');
961 l = !e ? strlen(p) : (size_t) (e - p);
963 for (c = soundhw; c->name; ++c) {
964 if (!strncmp(c->name, p, l) && !c->name[l]) {
965 c->enabled = 1;
966 break;
970 if (!c->name) {
971 if (l > 80) {
972 fprintf(stderr,
973 "Unknown sound card name (too big to show)\n");
975 else {
976 fprintf(stderr, "Unknown sound card name `%.*s'\n",
977 (int) l, p);
979 bad_card = 1;
981 p += l + (e != NULL);
984 if (bad_card) {
985 goto show_valid_cards;
990 void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
992 struct soundhw *c;
994 for (c = soundhw; c->name; ++c) {
995 if (c->enabled) {
996 if (c->isa) {
997 if (isa_bus) {
998 c->init.init_isa(isa_bus);
1000 } else {
1001 if (pci_bus) {
1002 c->init.init_pci(pci_bus);
1008 #else
1009 void select_soundhw(const char *optarg)
1012 void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
1015 #endif
1017 int qemu_uuid_parse(const char *str, uint8_t *uuid)
1019 int ret;
1021 if (strlen(str) != 36) {
1022 return -1;
1025 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1026 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1027 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1028 &uuid[15]);
1030 if (ret != 16) {
1031 return -1;
1033 #ifdef TARGET_I386
1034 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
1035 #endif
1036 return 0;
1039 void do_acpitable_option(const char *optarg)
1041 #ifdef TARGET_I386
1042 if (acpi_table_add(optarg) < 0) {
1043 fprintf(stderr, "Wrong acpi table provided\n");
1044 exit(1);
1046 #endif
1049 void do_smbios_option(const char *optarg)
1051 #ifdef TARGET_I386
1052 if (smbios_entry_add(optarg) < 0) {
1053 fprintf(stderr, "Wrong smbios provided\n");
1054 exit(1);
1056 #endif
1059 void cpudef_init(void)
1061 #if defined(cpudef_setup)
1062 cpudef_setup(); /* parse cpu definitions in target config file */
1063 #endif
1066 int audio_available(void)
1068 #ifdef HAS_AUDIO
1069 return 1;
1070 #else
1071 return 0;
1072 #endif
1075 int tcg_available(void)
1077 return 1;
1080 int kvm_available(void)
1082 #ifdef CONFIG_KVM
1083 return 1;
1084 #else
1085 return 0;
1086 #endif
1089 int xen_available(void)
1091 #ifdef CONFIG_XEN
1092 return 1;
1093 #else
1094 return 0;
1095 #endif
1099 TargetInfo *qmp_query_target(Error **errp)
1101 TargetInfo *info = g_malloc0(sizeof(*info));
1103 info->arch = TARGET_TYPE;
1105 return info;