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_PPC)
83 #define QEMU_ARCH QEMU_ARCH_PPC
84 #elif defined(TARGET_S390X)
85 #define QEMU_ARCH QEMU_ARCH_S390X
86 #elif defined(TARGET_SH4)
87 #define QEMU_ARCH QEMU_ARCH_SH4
88 #elif defined(TARGET_SPARC)
89 #define QEMU_ARCH QEMU_ARCH_SPARC
90 #elif defined(TARGET_XTENSA)
91 #define QEMU_ARCH QEMU_ARCH_XTENSA
94 const uint32_t arch_type
= QEMU_ARCH
;
96 /***********************************************************/
97 /* ram save/restore */
99 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
100 #define RAM_SAVE_FLAG_COMPRESS 0x02
101 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
102 #define RAM_SAVE_FLAG_PAGE 0x08
103 #define RAM_SAVE_FLAG_EOS 0x10
104 #define RAM_SAVE_FLAG_CONTINUE 0x20
108 #define VECTYPE vector unsigned char
109 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
110 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
111 /* altivec.h may redefine the bool macro as vector type.
112 * Reset it to POSIX semantics. */
115 #elif defined __SSE2__
116 #include <emmintrin.h>
117 #define VECTYPE __m128i
118 #define SPLAT(p) _mm_set1_epi8(*(p))
119 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
121 #define VECTYPE unsigned long
122 #define SPLAT(p) (*(p) * (~0UL / 255))
123 #define ALL_EQ(v1, v2) ((v1) == (v2))
127 static struct defconfig_file
{
128 const char *filename
;
129 /* Indicates it is an user config file (disabled by -no-user-config) */
131 } default_config_files
[] = {
132 { CONFIG_QEMU_DATADIR
"/cpus-" TARGET_ARCH
".conf", false },
133 { CONFIG_QEMU_CONFDIR
"/qemu.conf", true },
134 { CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf", true },
135 { NULL
}, /* end of list */
139 int qemu_read_default_config_files(bool userconfig
)
142 struct defconfig_file
*f
;
144 for (f
= default_config_files
; f
->filename
; f
++) {
145 if (!userconfig
&& f
->userconfig
) {
148 ret
= qemu_read_config_file(f
->filename
);
149 if (ret
< 0 && ret
!= -ENOENT
) {
157 static int is_dup_page(uint8_t *page
)
159 VECTYPE
*p
= (VECTYPE
*)page
;
160 VECTYPE val
= SPLAT(page
);
163 for (i
= 0; i
< TARGET_PAGE_SIZE
/ sizeof(VECTYPE
); i
++) {
164 if (!ALL_EQ(val
, p
[i
])) {
172 static void save_block_hdr(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
,
175 qemu_put_be64(f
, offset
| cont
| flag
);
177 qemu_put_byte(f
, strlen(block
->idstr
));
178 qemu_put_buffer(f
, (uint8_t *)block
->idstr
,
179 strlen(block
->idstr
));
184 static RAMBlock
*last_block
;
185 static ram_addr_t last_offset
;
187 static int ram_save_block(QEMUFile
*f
)
189 RAMBlock
*block
= last_block
;
190 ram_addr_t offset
= last_offset
;
195 block
= QLIST_FIRST(&ram_list
.blocks
);
199 if (memory_region_get_dirty(mr
, offset
, TARGET_PAGE_SIZE
,
200 DIRTY_MEMORY_MIGRATION
)) {
202 int cont
= (block
== last_block
) ? RAM_SAVE_FLAG_CONTINUE
: 0;
204 memory_region_reset_dirty(mr
, offset
, TARGET_PAGE_SIZE
,
205 DIRTY_MEMORY_MIGRATION
);
207 p
= memory_region_get_ram_ptr(mr
) + offset
;
209 if (is_dup_page(p
)) {
210 save_block_hdr(f
, block
, offset
, cont
, RAM_SAVE_FLAG_COMPRESS
);
211 qemu_put_byte(f
, *p
);
214 save_block_hdr(f
, block
, offset
, cont
, RAM_SAVE_FLAG_PAGE
);
215 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
216 bytes_sent
= TARGET_PAGE_SIZE
;
222 offset
+= TARGET_PAGE_SIZE
;
223 if (offset
>= block
->length
) {
225 block
= QLIST_NEXT(block
, next
);
227 block
= QLIST_FIRST(&ram_list
.blocks
);
229 } while (block
!= last_block
|| offset
!= last_offset
);
232 last_offset
= offset
;
237 static uint64_t bytes_transferred
;
239 static ram_addr_t
ram_save_remaining(void)
241 return ram_list
.dirty_pages
;
244 uint64_t ram_bytes_remaining(void)
246 return ram_save_remaining() * TARGET_PAGE_SIZE
;
249 uint64_t ram_bytes_transferred(void)
251 return bytes_transferred
;
254 uint64_t ram_bytes_total(void)
259 QLIST_FOREACH(block
, &ram_list
.blocks
, next
)
260 total
+= block
->length
;
265 static int block_compar(const void *a
, const void *b
)
267 RAMBlock
* const *ablock
= a
;
268 RAMBlock
* const *bblock
= b
;
270 return strcmp((*ablock
)->idstr
, (*bblock
)->idstr
);
273 static void sort_ram_list(void)
275 RAMBlock
*block
, *nblock
, **blocks
;
278 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
281 blocks
= g_malloc(n
* sizeof *blocks
);
283 QLIST_FOREACH_SAFE(block
, &ram_list
.blocks
, next
, nblock
) {
285 QLIST_REMOVE(block
, next
);
287 qsort(blocks
, n
, sizeof *blocks
, block_compar
);
289 QLIST_INSERT_HEAD(&ram_list
.blocks
, blocks
[n
], next
);
294 static void migration_end(void)
296 memory_global_dirty_log_stop();
299 static void ram_migration_cancel(void *opaque
)
304 #define MAX_WAIT 50 /* ms, half buffered_file limit */
306 static int ram_save_setup(QEMUFile
*f
, void *opaque
)
314 memory_global_sync_dirty_bitmap(get_system_memory());
316 bytes_transferred
= 0;
321 /* Make sure all dirty bits are set */
322 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
323 for (addr
= 0; addr
< block
->length
; addr
+= TARGET_PAGE_SIZE
) {
324 if (!memory_region_get_dirty(block
->mr
, addr
, TARGET_PAGE_SIZE
,
325 DIRTY_MEMORY_MIGRATION
)) {
326 memory_region_set_dirty(block
->mr
, addr
, TARGET_PAGE_SIZE
);
331 memory_global_dirty_log_start();
333 qemu_put_be64(f
, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE
);
335 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
336 qemu_put_byte(f
, strlen(block
->idstr
));
337 qemu_put_buffer(f
, (uint8_t *)block
->idstr
, strlen(block
->idstr
));
338 qemu_put_be64(f
, block
->length
);
341 bwidth
= qemu_get_clock_ns(rt_clock
);
344 while ((ret
= qemu_file_rate_limit(f
)) == 0) {
347 bytes_sent
= ram_save_block(f
);
348 bytes_transferred
+= bytes_sent
;
349 if (bytes_sent
== 0) { /* no more blocks */
352 /* we want to check in the 1st loop, just in case it was the 1st time
353 and we had to sync the dirty bitmap.
354 qemu_get_clock_ns() is a bit expensive, so we only check each some
358 uint64_t t1
= (qemu_get_clock_ns(rt_clock
) - bwidth
) / 1000000;
360 DPRINTF("big wait: " PRIu64
" milliseconds, %d iterations\n",
372 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
377 static int ram_save_iterate(QEMUFile
*f
, void *opaque
)
379 uint64_t bytes_transferred_last
;
383 uint64_t expected_time
;
385 memory_global_sync_dirty_bitmap(get_system_memory());
387 bytes_transferred_last
= bytes_transferred
;
388 bwidth
= qemu_get_clock_ns(rt_clock
);
391 while ((ret
= qemu_file_rate_limit(f
)) == 0) {
394 bytes_sent
= ram_save_block(f
);
395 bytes_transferred
+= bytes_sent
;
396 if (bytes_sent
== 0) { /* no more blocks */
399 /* we want to check in the 1st loop, just in case it was the 1st time
400 and we had to sync the dirty bitmap.
401 qemu_get_clock_ns() is a bit expensive, so we only check each some
405 uint64_t t1
= (qemu_get_clock_ns(rt_clock
) - bwidth
) / 1000000;
407 DPRINTF("big wait: " PRIu64
" milliseconds, %d iterations\n",
419 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
420 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
422 /* if we haven't transferred anything this round, force expected_time to a
423 * a very high value, but without crashing */
428 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
430 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
432 DPRINTF("ram_save_live: expected(" PRIu64
") <= max(" PRIu64
")?\n",
433 expected_time
, migrate_max_downtime());
435 return expected_time
<= migrate_max_downtime();
438 static int ram_save_complete(QEMUFile
*f
, void *opaque
)
445 memory_global_sync_dirty_bitmap(get_system_memory());
447 bwidth
= qemu_get_clock_ns(rt_clock
);
450 while ((ret
= qemu_file_rate_limit(f
)) == 0) {
451 bytes_sent
= ram_save_block(f
);
452 bytes_transferred
+= bytes_sent
;
453 if (bytes_sent
== 0) { /* no more blocks */
456 /* we want to check in the 1st loop, just in case it was the 1st time
457 and we had to sync the dirty bitmap.
458 qemu_get_clock_ns() is a bit expensive, so we only check each some
462 uint64_t t1
= (qemu_get_clock_ns(rt_clock
) - bwidth
) / 1000000;
464 DPRINTF("big wait: " PRIu64
" milliseconds, %d iterations\n",
476 /* try transferring iterative blocks of memory */
478 /* flush all remaining blocks regardless of rate limiting */
479 while ((bytes_sent
= ram_save_block(f
)) != 0) {
480 bytes_transferred
+= bytes_sent
;
482 memory_global_dirty_log_stop();
484 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
489 static inline void *host_from_stream_offset(QEMUFile
*f
,
493 static RAMBlock
*block
= NULL
;
497 if (flags
& RAM_SAVE_FLAG_CONTINUE
) {
499 fprintf(stderr
, "Ack, bad migration stream!\n");
503 return memory_region_get_ram_ptr(block
->mr
) + offset
;
506 len
= qemu_get_byte(f
);
507 qemu_get_buffer(f
, (uint8_t *)id
, len
);
510 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
511 if (!strncmp(id
, block
->idstr
, sizeof(id
)))
512 return memory_region_get_ram_ptr(block
->mr
) + offset
;
515 fprintf(stderr
, "Can't find block %s!\n", id
);
519 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
524 static uint64_t seq_iter
;
528 if (version_id
< 4 || version_id
> 4) {
533 addr
= qemu_get_be64(f
);
535 flags
= addr
& ~TARGET_PAGE_MASK
;
536 addr
&= TARGET_PAGE_MASK
;
538 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
539 if (version_id
== 4) {
540 /* Synchronize RAM block list */
543 ram_addr_t total_ram_bytes
= addr
;
545 while (total_ram_bytes
) {
549 len
= qemu_get_byte(f
);
550 qemu_get_buffer(f
, (uint8_t *)id
, len
);
552 length
= qemu_get_be64(f
);
554 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
555 if (!strncmp(id
, block
->idstr
, sizeof(id
))) {
556 if (block
->length
!= length
) {
565 fprintf(stderr
, "Unknown ramblock \"%s\", cannot "
566 "accept migration\n", id
);
571 total_ram_bytes
-= length
;
576 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
580 host
= host_from_stream_offset(f
, addr
, flags
);
585 ch
= qemu_get_byte(f
);
586 memset(host
, ch
, TARGET_PAGE_SIZE
);
589 (!kvm_enabled() || kvm_has_sync_mmu())) {
590 qemu_madvise(host
, TARGET_PAGE_SIZE
, QEMU_MADV_DONTNEED
);
593 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
596 host
= host_from_stream_offset(f
, addr
, flags
);
601 qemu_get_buffer(f
, host
, TARGET_PAGE_SIZE
);
603 error
= qemu_file_get_error(f
);
608 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
611 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64
"\n",
616 SaveVMHandlers savevm_ram_handlers
= {
617 .save_live_setup
= ram_save_setup
,
618 .save_live_iterate
= ram_save_iterate
,
619 .save_live_complete
= ram_save_complete
,
620 .load_state
= ram_load
,
621 .cancel
= ram_migration_cancel
,
631 int (*init_isa
) (ISABus
*bus
);
632 int (*init_pci
) (PCIBus
*bus
);
636 static struct soundhw soundhw
[] = {
637 #ifdef HAS_AUDIO_CHOICE
644 { .init_isa
= pcspk_audio_init
}
651 "Creative Sound Blaster 16",
654 { .init_isa
= SB16_init
}
658 #ifdef CONFIG_CS4231A
664 { .init_isa
= cs4231a_init
}
672 "Yamaha YMF262 (OPL3)",
674 "Yamaha YM3812 (OPL2)",
678 { .init_isa
= Adlib_init
}
685 "Gravis Ultrasound GF1",
688 { .init_isa
= GUS_init
}
695 "Intel 82801AA AC97 Audio",
698 { .init_pci
= ac97_init
}
705 "ENSONIQ AudioPCI ES1370",
708 { .init_pci
= es1370_init
}
718 { .init_pci
= intel_hda_and_codec_init
}
722 #endif /* HAS_AUDIO_CHOICE */
724 { NULL
, NULL
, 0, 0, { NULL
} }
727 void select_soundhw(const char *optarg
)
731 if (*optarg
== '?') {
734 printf("Valid sound card names (comma separated):\n");
735 for (c
= soundhw
; c
->name
; ++c
) {
736 printf ("%-11s %s\n", c
->name
, c
->descr
);
738 printf("\n-soundhw all will enable all of the above\n");
739 exit(*optarg
!= '?');
747 if (!strcmp(optarg
, "all")) {
748 for (c
= soundhw
; c
->name
; ++c
) {
757 l
= !e
? strlen(p
) : (size_t) (e
- p
);
759 for (c
= soundhw
; c
->name
; ++c
) {
760 if (!strncmp(c
->name
, p
, l
) && !c
->name
[l
]) {
769 "Unknown sound card name (too big to show)\n");
772 fprintf(stderr
, "Unknown sound card name `%.*s'\n",
777 p
+= l
+ (e
!= NULL
);
781 goto show_valid_cards
;
786 void audio_init(ISABus
*isa_bus
, PCIBus
*pci_bus
)
790 for (c
= soundhw
; c
->name
; ++c
) {
794 c
->init
.init_isa(isa_bus
);
798 c
->init
.init_pci(pci_bus
);
805 void select_soundhw(const char *optarg
)
808 void audio_init(ISABus
*isa_bus
, PCIBus
*pci_bus
)
813 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
817 if (strlen(str
) != 36) {
821 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
822 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
823 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14],
830 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
835 void do_acpitable_option(const char *optarg
)
838 if (acpi_table_add(optarg
) < 0) {
839 fprintf(stderr
, "Wrong acpi table provided\n");
845 void do_smbios_option(const char *optarg
)
848 if (smbios_entry_add(optarg
) < 0) {
849 fprintf(stderr
, "Wrong smbios provided\n");
855 void cpudef_init(void)
857 #if defined(cpudef_setup)
858 cpudef_setup(); /* parse cpu definitions in target config file */
862 int audio_available(void)
871 int tcg_available(void)
876 int kvm_available(void)
885 int xen_available(void)