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
28 #include <sys/types.h>
34 #include "arch_init.h"
35 #include "audio/audio.h"
38 #include "hw/audiodev.h"
40 #include "migration.h"
43 #include "hw/smbios.h"
44 #include "exec-memory.h"
47 #ifdef DEBUG_ARCH_INIT
48 #define DPRINTF(fmt, ...) \
49 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
51 #define DPRINTF(fmt, ...) \
56 int graphic_width = 1024;
57 int graphic_height = 768;
58 int graphic_depth = 8;
60 int graphic_width = 800;
61 int graphic_height = 600;
62 int graphic_depth = 15;
66 #if defined(TARGET_ALPHA)
67 #define QEMU_ARCH QEMU_ARCH_ALPHA
68 #elif defined(TARGET_ARM)
69 #define QEMU_ARCH QEMU_ARCH_ARM
70 #elif defined(TARGET_CRIS)
71 #define QEMU_ARCH QEMU_ARCH_CRIS
72 #elif defined(TARGET_I386)
73 #define QEMU_ARCH QEMU_ARCH_I386
74 #elif defined(TARGET_M68K)
75 #define QEMU_ARCH QEMU_ARCH_M68K
76 #elif defined(TARGET_LM32)
77 #define QEMU_ARCH QEMU_ARCH_LM32
78 #elif defined(TARGET_MICROBLAZE)
79 #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
80 #elif defined(TARGET_MIPS)
81 #define QEMU_ARCH QEMU_ARCH_MIPS
82 #elif defined(TARGET_OPENRISC)
83 #define QEMU_ARCH QEMU_ARCH_OPENRISC
84 #elif defined(TARGET_PPC)
85 #define QEMU_ARCH QEMU_ARCH_PPC
86 #elif defined(TARGET_S390X)
87 #define QEMU_ARCH QEMU_ARCH_S390X
88 #elif defined(TARGET_SH4)
89 #define QEMU_ARCH QEMU_ARCH_SH4
90 #elif defined(TARGET_SPARC)
91 #define QEMU_ARCH QEMU_ARCH_SPARC
92 #elif defined(TARGET_XTENSA)
93 #define QEMU_ARCH QEMU_ARCH_XTENSA
96 const uint32_t arch_type = QEMU_ARCH;
98 /***********************************************************/
99 /* ram save/restore */
101 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
102 #define RAM_SAVE_FLAG_COMPRESS 0x02
103 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
104 #define RAM_SAVE_FLAG_PAGE 0x08
105 #define RAM_SAVE_FLAG_EOS 0x10
106 #define RAM_SAVE_FLAG_CONTINUE 0x20
110 #define VECTYPE vector unsigned char
111 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
112 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
113 /* altivec.h may redefine the bool macro as vector type.
114 * Reset it to POSIX semantics. */
117 #elif defined __SSE2__
118 #include <emmintrin.h>
119 #define VECTYPE __m128i
120 #define SPLAT(p) _mm_set1_epi8(*(p))
121 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
123 #define VECTYPE unsigned long
124 #define SPLAT(p) (*(p) * (~0UL / 255))
125 #define ALL_EQ(v1, v2) ((v1) == (v2))
129 static struct defconfig_file {
130 const char *filename;
131 /* Indicates it is an user config file (disabled by -no-user-config) */
133 } default_config_files[] = {
134 { CONFIG_QEMU_DATADIR "/cpus-" TARGET_ARCH ".conf", false },
135 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
136 { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true },
137 { NULL }, /* end of list */
141 int qemu_read_default_config_files(bool userconfig)
144 struct defconfig_file *f;
146 for (f = default_config_files; f->filename; f++) {
147 if (!userconfig && f->userconfig) {
150 ret = qemu_read_config_file(f->filename);
151 if (ret < 0 && ret != -ENOENT) {
159 static int is_dup_page(uint8_t *page)
161 VECTYPE *p = (VECTYPE *)page;
162 VECTYPE val = SPLAT(page);
165 for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
166 if (!ALL_EQ(val, p[i])) {
174 static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
177 qemu_put_be64(f, offset | cont | flag);
179 qemu_put_byte(f, strlen(block->idstr));
180 qemu_put_buffer(f, (uint8_t *)block->idstr,
181 strlen(block->idstr));
186 static RAMBlock *last_block;
187 static ram_addr_t last_offset;
189 static int ram_save_block(QEMUFile *f)
191 RAMBlock *block = last_block;
192 ram_addr_t offset = last_offset;
197 block = QLIST_FIRST(&ram_list.blocks);
201 if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
202 DIRTY_MEMORY_MIGRATION)) {
204 int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
206 memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
207 DIRTY_MEMORY_MIGRATION);
209 p = memory_region_get_ram_ptr(mr) + offset;
211 if (is_dup_page(p)) {
212 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
213 qemu_put_byte(f, *p);
216 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
217 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
218 bytes_sent = TARGET_PAGE_SIZE;
224 offset += TARGET_PAGE_SIZE;
225 if (offset >= block->length) {
227 block = QLIST_NEXT(block, next);
229 block = QLIST_FIRST(&ram_list.blocks);
231 } while (block != last_block || offset != last_offset);
234 last_offset = offset;
239 static uint64_t bytes_transferred;
241 static ram_addr_t ram_save_remaining(void)
243 return ram_list.dirty_pages;
246 uint64_t ram_bytes_remaining(void)
248 return ram_save_remaining() * TARGET_PAGE_SIZE;
251 uint64_t ram_bytes_transferred(void)
253 return bytes_transferred;
256 uint64_t ram_bytes_total(void)
261 QLIST_FOREACH(block, &ram_list.blocks, next)
262 total += block->length;
267 static int block_compar(const void *a, const void *b)
269 RAMBlock * const *ablock = a;
270 RAMBlock * const *bblock = b;
272 return strcmp((*ablock)->idstr, (*bblock)->idstr);
275 static void sort_ram_list(void)
277 RAMBlock *block, *nblock, **blocks;
280 QLIST_FOREACH(block, &ram_list.blocks, next) {
283 blocks = g_malloc(n * sizeof *blocks);
285 QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
287 QLIST_REMOVE(block, next);
289 qsort(blocks, n, sizeof *blocks, block_compar);
291 QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
296 static void migration_end(void)
298 memory_global_dirty_log_stop();
301 #define MAX_WAIT 50 /* ms, half buffered_file limit */
303 int ram_save_live(QEMUFile *f, int stage, void *opaque)
306 uint64_t bytes_transferred_last;
316 memory_global_sync_dirty_bitmap(get_system_memory());
320 bytes_transferred = 0;
325 /* Make sure all dirty bits are set */
326 QLIST_FOREACH(block, &ram_list.blocks, next) {
327 for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
328 if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE,
329 DIRTY_MEMORY_MIGRATION)) {
330 memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE);
335 memory_global_dirty_log_start();
337 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
339 QLIST_FOREACH(block, &ram_list.blocks, next) {
340 qemu_put_byte(f, strlen(block->idstr));
341 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
342 qemu_put_be64(f, block->length);
346 bytes_transferred_last = bytes_transferred;
347 bwidth = qemu_get_clock_ns(rt_clock);
350 while ((ret = qemu_file_rate_limit(f)) == 0) {
353 bytes_sent = ram_save_block(f);
354 bytes_transferred += bytes_sent;
355 if (bytes_sent == 0) { /* no more blocks */
358 /* we want to check in the 1st loop, just in case it was the 1st time
359 and we had to sync the dirty bitmap.
360 qemu_get_clock_ns() is a bit expensive, so we only check each some
364 uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000;
366 DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n",
378 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
379 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
381 /* if we haven't transferred anything this round, force expected_time to a
382 * a very high value, but without crashing */
387 /* try transferring iterative blocks of memory */
391 /* flush all remaining blocks regardless of rate limiting */
392 while ((bytes_sent = ram_save_block(f)) != 0) {
393 bytes_transferred += bytes_sent;
395 memory_global_dirty_log_stop();
398 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
401 uint64_t expected_time;
402 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
404 DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n",
405 expected_time, migrate_max_downtime());
407 return expected_time <= migrate_max_downtime();
412 static inline void *host_from_stream_offset(QEMUFile *f,
416 static RAMBlock *block = NULL;
420 if (flags & RAM_SAVE_FLAG_CONTINUE) {
422 fprintf(stderr, "Ack, bad migration stream!\n");
426 return memory_region_get_ram_ptr(block->mr) + offset;
429 len = qemu_get_byte(f);
430 qemu_get_buffer(f, (uint8_t *)id, len);
433 QLIST_FOREACH(block, &ram_list.blocks, next) {
434 if (!strncmp(id, block->idstr, sizeof(id)))
435 return memory_region_get_ram_ptr(block->mr) + offset;
438 fprintf(stderr, "Can't find block %s!\n", id);
442 int ram_load(QEMUFile *f, void *opaque, int version_id)
447 static uint64_t seq_iter;
451 if (version_id < 4 || version_id > 4) {
456 addr = qemu_get_be64(f);
458 flags = addr & ~TARGET_PAGE_MASK;
459 addr &= TARGET_PAGE_MASK;
461 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
462 if (version_id == 4) {
463 /* Synchronize RAM block list */
466 ram_addr_t total_ram_bytes = addr;
468 while (total_ram_bytes) {
472 len = qemu_get_byte(f);
473 qemu_get_buffer(f, (uint8_t *)id, len);
475 length = qemu_get_be64(f);
477 QLIST_FOREACH(block, &ram_list.blocks, next) {
478 if (!strncmp(id, block->idstr, sizeof(id))) {
479 if (block->length != length) {
488 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
489 "accept migration\n", id);
494 total_ram_bytes -= length;
499 if (flags & RAM_SAVE_FLAG_COMPRESS) {
503 host = host_from_stream_offset(f, addr, flags);
508 ch = qemu_get_byte(f);
509 memset(host, ch, TARGET_PAGE_SIZE);
512 (!kvm_enabled() || kvm_has_sync_mmu())) {
513 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
516 } else if (flags & RAM_SAVE_FLAG_PAGE) {
519 host = host_from_stream_offset(f, addr, flags);
524 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
526 error = qemu_file_get_error(f);
531 } while (!(flags & RAM_SAVE_FLAG_EOS));
534 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64 "\n",
546 int (*init_isa) (ISABus *bus);
547 int (*init_pci) (PCIBus *bus);
551 static struct soundhw soundhw[] = {
552 #ifdef HAS_AUDIO_CHOICE
559 { .init_isa = pcspk_audio_init }
566 "Creative Sound Blaster 16",
569 { .init_isa = SB16_init }
573 #ifdef CONFIG_CS4231A
579 { .init_isa = cs4231a_init }
587 "Yamaha YMF262 (OPL3)",
589 "Yamaha YM3812 (OPL2)",
593 { .init_isa = Adlib_init }
600 "Gravis Ultrasound GF1",
603 { .init_isa = GUS_init }
610 "Intel 82801AA AC97 Audio",
613 { .init_pci = ac97_init }
620 "ENSONIQ AudioPCI ES1370",
623 { .init_pci = es1370_init }
633 { .init_pci = intel_hda_and_codec_init }
637 #endif /* HAS_AUDIO_CHOICE */
639 { NULL, NULL, 0, 0, { NULL } }
642 void select_soundhw(const char *optarg)
646 if (*optarg == '?') {
649 printf("Valid sound card names (comma separated):\n");
650 for (c = soundhw; c->name; ++c) {
651 printf ("%-11s %s\n", c->name, c->descr);
653 printf("\n-soundhw all will enable all of the above\n");
654 exit(*optarg != '?');
662 if (!strcmp(optarg, "all")) {
663 for (c = soundhw; c->name; ++c) {
672 l = !e ? strlen(p) : (size_t) (e - p);
674 for (c = soundhw; c->name; ++c) {
675 if (!strncmp(c->name, p, l) && !c->name[l]) {
684 "Unknown sound card name (too big to show)\n");
687 fprintf(stderr, "Unknown sound card name `%.*s'\n",
692 p += l + (e != NULL);
696 goto show_valid_cards;
701 void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
705 for (c = soundhw; c->name; ++c) {
709 c->init.init_isa(isa_bus);
713 c->init.init_pci(pci_bus);
720 void select_soundhw(const char *optarg)
723 void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
728 int qemu_uuid_parse(const char *str, uint8_t *uuid)
732 if (strlen(str) != 36) {
736 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
737 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
738 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
745 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
750 void do_acpitable_option(const char *optarg)
753 if (acpi_table_add(optarg) < 0) {
754 fprintf(stderr, "Wrong acpi table provided\n");
760 void do_smbios_option(const char *optarg)
763 if (smbios_entry_add(optarg) < 0) {
764 fprintf(stderr, "Wrong smbios provided\n");
770 void cpudef_init(void)
772 #if defined(cpudef_setup)
773 cpudef_setup(); /* parse cpu definitions in target config file */
777 int audio_available(void)
786 int tcg_available(void)
791 int kvm_available(void)
800 int xen_available(void)