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
94 #elif defined(TARGET_UNICORE32)
95 #define QEMU_ARCH QEMU_ARCH_UNICORE32
98 const uint32_t arch_type
= QEMU_ARCH
;
100 /***********************************************************/
101 /* ram save/restore */
103 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
104 #define RAM_SAVE_FLAG_COMPRESS 0x02
105 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
106 #define RAM_SAVE_FLAG_PAGE 0x08
107 #define RAM_SAVE_FLAG_EOS 0x10
108 #define RAM_SAVE_FLAG_CONTINUE 0x20
112 #define VECTYPE vector unsigned char
113 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
114 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
115 /* altivec.h may redefine the bool macro as vector type.
116 * Reset it to POSIX semantics. */
119 #elif defined __SSE2__
120 #include <emmintrin.h>
121 #define VECTYPE __m128i
122 #define SPLAT(p) _mm_set1_epi8(*(p))
123 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
125 #define VECTYPE unsigned long
126 #define SPLAT(p) (*(p) * (~0UL / 255))
127 #define ALL_EQ(v1, v2) ((v1) == (v2))
131 static struct defconfig_file
{
132 const char *filename
;
133 /* Indicates it is an user config file (disabled by -no-user-config) */
135 } default_config_files
[] = {
136 { CONFIG_QEMU_DATADIR
"/cpus-" TARGET_ARCH
".conf", false },
137 { CONFIG_QEMU_CONFDIR
"/qemu.conf", true },
138 { CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf", true },
139 { NULL
}, /* end of list */
143 int qemu_read_default_config_files(bool userconfig
)
146 struct defconfig_file
*f
;
148 for (f
= default_config_files
; f
->filename
; f
++) {
149 if (!userconfig
&& f
->userconfig
) {
152 ret
= qemu_read_config_file(f
->filename
);
153 if (ret
< 0 && ret
!= -ENOENT
) {
161 static int is_dup_page(uint8_t *page
)
163 VECTYPE
*p
= (VECTYPE
*)page
;
164 VECTYPE val
= SPLAT(page
);
167 for (i
= 0; i
< TARGET_PAGE_SIZE
/ sizeof(VECTYPE
); i
++) {
168 if (!ALL_EQ(val
, p
[i
])) {
176 static void save_block_hdr(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
,
179 qemu_put_be64(f
, offset
| cont
| flag
);
181 qemu_put_byte(f
, strlen(block
->idstr
));
182 qemu_put_buffer(f
, (uint8_t *)block
->idstr
,
183 strlen(block
->idstr
));
188 static RAMBlock
*last_block
;
189 static ram_addr_t last_offset
;
192 * ram_save_block: Writes a page of memory to the stream f
194 * Returns: 0: if the page hasn't changed
195 * -1: if there are no more dirty pages
196 * n: the amount of bytes written in other case
199 static int ram_save_block(QEMUFile
*f
)
201 RAMBlock
*block
= last_block
;
202 ram_addr_t offset
= last_offset
;
207 block
= QLIST_FIRST(&ram_list
.blocks
);
211 if (memory_region_get_dirty(mr
, offset
, TARGET_PAGE_SIZE
,
212 DIRTY_MEMORY_MIGRATION
)) {
214 int cont
= (block
== last_block
) ? RAM_SAVE_FLAG_CONTINUE
: 0;
216 memory_region_reset_dirty(mr
, offset
, TARGET_PAGE_SIZE
,
217 DIRTY_MEMORY_MIGRATION
);
219 p
= memory_region_get_ram_ptr(mr
) + offset
;
221 if (is_dup_page(p
)) {
222 save_block_hdr(f
, block
, offset
, cont
, RAM_SAVE_FLAG_COMPRESS
);
223 qemu_put_byte(f
, *p
);
226 save_block_hdr(f
, block
, offset
, cont
, RAM_SAVE_FLAG_PAGE
);
227 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
228 bytes_sent
= TARGET_PAGE_SIZE
;
234 offset
+= TARGET_PAGE_SIZE
;
235 if (offset
>= block
->length
) {
237 block
= QLIST_NEXT(block
, next
);
239 block
= QLIST_FIRST(&ram_list
.blocks
);
241 } while (block
!= last_block
|| offset
!= last_offset
);
244 last_offset
= offset
;
249 static uint64_t bytes_transferred
;
251 static ram_addr_t
ram_save_remaining(void)
253 return ram_list
.dirty_pages
;
256 uint64_t ram_bytes_remaining(void)
258 return ram_save_remaining() * TARGET_PAGE_SIZE
;
261 uint64_t ram_bytes_transferred(void)
263 return bytes_transferred
;
266 uint64_t ram_bytes_total(void)
271 QLIST_FOREACH(block
, &ram_list
.blocks
, next
)
272 total
+= block
->length
;
277 static int block_compar(const void *a
, const void *b
)
279 RAMBlock
* const *ablock
= a
;
280 RAMBlock
* const *bblock
= b
;
282 return strcmp((*ablock
)->idstr
, (*bblock
)->idstr
);
285 static void sort_ram_list(void)
287 RAMBlock
*block
, *nblock
, **blocks
;
290 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
293 blocks
= g_malloc(n
* sizeof *blocks
);
295 QLIST_FOREACH_SAFE(block
, &ram_list
.blocks
, next
, nblock
) {
297 QLIST_REMOVE(block
, next
);
299 qsort(blocks
, n
, sizeof *blocks
, block_compar
);
301 QLIST_INSERT_HEAD(&ram_list
.blocks
, blocks
[n
], next
);
306 static void migration_end(void)
308 memory_global_dirty_log_stop();
311 static void ram_migration_cancel(void *opaque
)
316 #define MAX_WAIT 50 /* ms, half buffered_file limit */
318 static int ram_save_setup(QEMUFile
*f
, void *opaque
)
323 bytes_transferred
= 0;
328 /* Make sure all dirty bits are set */
329 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
330 for (addr
= 0; addr
< block
->length
; addr
+= TARGET_PAGE_SIZE
) {
331 if (!memory_region_get_dirty(block
->mr
, addr
, TARGET_PAGE_SIZE
,
332 DIRTY_MEMORY_MIGRATION
)) {
333 memory_region_set_dirty(block
->mr
, addr
, TARGET_PAGE_SIZE
);
338 memory_global_dirty_log_start();
340 qemu_put_be64(f
, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE
);
342 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
343 qemu_put_byte(f
, strlen(block
->idstr
));
344 qemu_put_buffer(f
, (uint8_t *)block
->idstr
, strlen(block
->idstr
));
345 qemu_put_be64(f
, block
->length
);
348 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
353 static int ram_save_iterate(QEMUFile
*f
, void *opaque
)
355 uint64_t bytes_transferred_last
;
359 uint64_t expected_time
;
361 bytes_transferred_last
= bytes_transferred
;
362 bwidth
= qemu_get_clock_ns(rt_clock
);
365 while ((ret
= qemu_file_rate_limit(f
)) == 0) {
368 bytes_sent
= ram_save_block(f
);
369 /* no more blocks to sent */
370 if (bytes_sent
< 0) {
373 bytes_transferred
+= bytes_sent
;
374 /* we want to check in the 1st loop, just in case it was the 1st time
375 and we had to sync the dirty bitmap.
376 qemu_get_clock_ns() is a bit expensive, so we only check each some
380 uint64_t t1
= (qemu_get_clock_ns(rt_clock
) - bwidth
) / 1000000;
382 DPRINTF("big wait: " PRIu64
" milliseconds, %d iterations\n",
394 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
395 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
397 /* if we haven't transferred anything this round, force expected_time to a
398 * a very high value, but without crashing */
403 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
405 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
407 DPRINTF("ram_save_live: expected(" PRIu64
") <= max(" PRIu64
")?\n",
408 expected_time
, migrate_max_downtime());
410 if (expected_time
<= migrate_max_downtime()) {
411 memory_global_sync_dirty_bitmap(get_system_memory());
412 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
414 return expected_time
<= migrate_max_downtime();
419 static int ram_save_complete(QEMUFile
*f
, void *opaque
)
421 memory_global_sync_dirty_bitmap(get_system_memory());
423 /* try transferring iterative blocks of memory */
425 /* flush all remaining blocks regardless of rate limiting */
429 bytes_sent
= ram_save_block(f
);
430 /* no more blocks to sent */
431 if (bytes_sent
< 0) {
434 bytes_transferred
+= bytes_sent
;
436 memory_global_dirty_log_stop();
438 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
443 static inline void *host_from_stream_offset(QEMUFile
*f
,
447 static RAMBlock
*block
= NULL
;
451 if (flags
& RAM_SAVE_FLAG_CONTINUE
) {
453 fprintf(stderr
, "Ack, bad migration stream!\n");
457 return memory_region_get_ram_ptr(block
->mr
) + offset
;
460 len
= qemu_get_byte(f
);
461 qemu_get_buffer(f
, (uint8_t *)id
, len
);
464 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
465 if (!strncmp(id
, block
->idstr
, sizeof(id
)))
466 return memory_region_get_ram_ptr(block
->mr
) + offset
;
469 fprintf(stderr
, "Can't find block %s!\n", id
);
473 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
478 static uint64_t seq_iter
;
482 if (version_id
< 4 || version_id
> 4) {
487 addr
= qemu_get_be64(f
);
489 flags
= addr
& ~TARGET_PAGE_MASK
;
490 addr
&= TARGET_PAGE_MASK
;
492 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
493 if (version_id
== 4) {
494 /* Synchronize RAM block list */
497 ram_addr_t total_ram_bytes
= addr
;
499 while (total_ram_bytes
) {
503 len
= qemu_get_byte(f
);
504 qemu_get_buffer(f
, (uint8_t *)id
, len
);
506 length
= qemu_get_be64(f
);
508 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
509 if (!strncmp(id
, block
->idstr
, sizeof(id
))) {
510 if (block
->length
!= length
) {
519 fprintf(stderr
, "Unknown ramblock \"%s\", cannot "
520 "accept migration\n", id
);
525 total_ram_bytes
-= length
;
530 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
534 host
= host_from_stream_offset(f
, addr
, flags
);
539 ch
= qemu_get_byte(f
);
540 memset(host
, ch
, TARGET_PAGE_SIZE
);
543 (!kvm_enabled() || kvm_has_sync_mmu())) {
544 qemu_madvise(host
, TARGET_PAGE_SIZE
, QEMU_MADV_DONTNEED
);
547 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
550 host
= host_from_stream_offset(f
, addr
, flags
);
555 qemu_get_buffer(f
, host
, TARGET_PAGE_SIZE
);
557 error
= qemu_file_get_error(f
);
562 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
565 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64
"\n",
570 SaveVMHandlers savevm_ram_handlers
= {
571 .save_live_setup
= ram_save_setup
,
572 .save_live_iterate
= ram_save_iterate
,
573 .save_live_complete
= ram_save_complete
,
574 .load_state
= ram_load
,
575 .cancel
= ram_migration_cancel
,
585 int (*init_isa
) (ISABus
*bus
);
586 int (*init_pci
) (PCIBus
*bus
);
590 static struct soundhw soundhw
[] = {
591 #ifdef HAS_AUDIO_CHOICE
598 { .init_isa
= pcspk_audio_init
}
605 "Creative Sound Blaster 16",
608 { .init_isa
= SB16_init
}
612 #ifdef CONFIG_CS4231A
618 { .init_isa
= cs4231a_init
}
626 "Yamaha YMF262 (OPL3)",
628 "Yamaha YM3812 (OPL2)",
632 { .init_isa
= Adlib_init
}
639 "Gravis Ultrasound GF1",
642 { .init_isa
= GUS_init
}
649 "Intel 82801AA AC97 Audio",
652 { .init_pci
= ac97_init
}
659 "ENSONIQ AudioPCI ES1370",
662 { .init_pci
= es1370_init
}
672 { .init_pci
= intel_hda_and_codec_init
}
676 #endif /* HAS_AUDIO_CHOICE */
678 { NULL
, NULL
, 0, 0, { NULL
} }
681 void select_soundhw(const char *optarg
)
685 if (is_help_option(optarg
)) {
688 printf("Valid sound card names (comma separated):\n");
689 for (c
= soundhw
; c
->name
; ++c
) {
690 printf ("%-11s %s\n", c
->name
, c
->descr
);
692 printf("\n-soundhw all will enable all of the above\n");
693 exit(!is_help_option(optarg
));
701 if (!strcmp(optarg
, "all")) {
702 for (c
= soundhw
; c
->name
; ++c
) {
711 l
= !e
? strlen(p
) : (size_t) (e
- p
);
713 for (c
= soundhw
; c
->name
; ++c
) {
714 if (!strncmp(c
->name
, p
, l
) && !c
->name
[l
]) {
723 "Unknown sound card name (too big to show)\n");
726 fprintf(stderr
, "Unknown sound card name `%.*s'\n",
731 p
+= l
+ (e
!= NULL
);
735 goto show_valid_cards
;
740 void audio_init(ISABus
*isa_bus
, PCIBus
*pci_bus
)
744 for (c
= soundhw
; c
->name
; ++c
) {
748 c
->init
.init_isa(isa_bus
);
752 c
->init
.init_pci(pci_bus
);
759 void select_soundhw(const char *optarg
)
762 void audio_init(ISABus
*isa_bus
, PCIBus
*pci_bus
)
767 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
771 if (strlen(str
) != 36) {
775 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
776 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
777 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14],
784 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
789 void do_acpitable_option(const char *optarg
)
792 if (acpi_table_add(optarg
) < 0) {
793 fprintf(stderr
, "Wrong acpi table provided\n");
799 void do_smbios_option(const char *optarg
)
802 if (smbios_entry_add(optarg
) < 0) {
803 fprintf(stderr
, "Wrong smbios provided\n");
809 void cpudef_init(void)
811 #if defined(cpudef_setup)
812 cpudef_setup(); /* parse cpu definitions in target config file */
816 int audio_available(void)
825 int tcg_available(void)
830 int kvm_available(void)
839 int xen_available(void)