arm: Move some ARM devices into libhw
[qemu/agraf.git] / arch_init.c
blob7b65c4888b7151dfb6a7f8e0cd226df3f86037be
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"
47 #ifdef DEBUG_ARCH_INIT
48 #define DPRINTF(fmt, ...) \
49 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
50 #else
51 #define DPRINTF(fmt, ...) \
52 do { } while (0)
53 #endif
55 #ifdef TARGET_SPARC
56 int graphic_width = 1024;
57 int graphic_height = 768;
58 int graphic_depth = 8;
59 #else
60 int graphic_width = 800;
61 int graphic_height = 600;
62 int graphic_depth = 15;
63 #endif
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
96 #endif
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
110 #ifdef __ALTIVEC__
111 #include <altivec.h>
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. */
117 #undef bool
118 #define bool _Bool
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)
124 #else
125 #define VECTYPE unsigned long
126 #define SPLAT(p) (*(p) * (~0UL / 255))
127 #define ALL_EQ(v1, v2) ((v1) == (v2))
128 #endif
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_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)
145 int ret;
146 struct defconfig_file *f;
148 for (f = default_config_files; f->filename; f++) {
149 if (!userconfig && f->userconfig) {
150 continue;
152 ret = qemu_read_config_file(f->filename);
153 if (ret < 0 && ret != -ENOENT) {
154 return ret;
158 return 0;
161 static int is_dup_page(uint8_t *page)
163 VECTYPE *p = (VECTYPE *)page;
164 VECTYPE val = SPLAT(page);
165 int i;
167 for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
168 if (!ALL_EQ(val, p[i])) {
169 return 0;
173 return 1;
176 static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
177 int cont, int flag)
179 qemu_put_be64(f, offset | cont | flag);
180 if (!cont) {
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;
203 int bytes_sent = -1;
204 MemoryRegion *mr;
206 if (!block)
207 block = QLIST_FIRST(&ram_list.blocks);
209 do {
210 mr = block->mr;
211 if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
212 DIRTY_MEMORY_MIGRATION)) {
213 uint8_t *p;
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);
224 bytes_sent = 1;
225 } else {
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;
231 break;
234 offset += TARGET_PAGE_SIZE;
235 if (offset >= block->length) {
236 offset = 0;
237 block = QLIST_NEXT(block, next);
238 if (!block)
239 block = QLIST_FIRST(&ram_list.blocks);
241 } while (block != last_block || offset != last_offset);
243 last_block = block;
244 last_offset = offset;
246 return bytes_sent;
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)
268 RAMBlock *block;
269 uint64_t total = 0;
271 QLIST_FOREACH(block, &ram_list.blocks, next)
272 total += block->length;
274 return total;
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;
288 int n;
289 n = 0;
290 QLIST_FOREACH(block, &ram_list.blocks, next) {
291 ++n;
293 blocks = g_malloc(n * sizeof *blocks);
294 n = 0;
295 QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
296 blocks[n++] = block;
297 QLIST_REMOVE(block, next);
299 qsort(blocks, n, sizeof *blocks, block_compar);
300 while (--n >= 0) {
301 QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
303 g_free(blocks);
306 static void migration_end(void)
308 memory_global_dirty_log_stop();
311 static void ram_migration_cancel(void *opaque)
313 migration_end();
316 #define MAX_WAIT 50 /* ms, half buffered_file limit */
318 static int ram_save_setup(QEMUFile *f, void *opaque)
320 ram_addr_t addr;
321 RAMBlock *block;
323 bytes_transferred = 0;
324 last_block = NULL;
325 last_offset = 0;
326 sort_ram_list();
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);
350 return 0;
353 static int ram_save_iterate(QEMUFile *f, void *opaque)
355 uint64_t bytes_transferred_last;
356 double bwidth = 0;
357 int ret;
358 int i;
359 uint64_t expected_time;
361 bytes_transferred_last = bytes_transferred;
362 bwidth = qemu_get_clock_ns(rt_clock);
364 i = 0;
365 while ((ret = qemu_file_rate_limit(f)) == 0) {
366 int bytes_sent;
368 bytes_sent = ram_save_block(f);
369 /* no more blocks to sent */
370 if (bytes_sent < 0) {
371 break;
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
377 iterations
379 if ((i & 63) == 0) {
380 uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000;
381 if (t1 > MAX_WAIT) {
382 DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n",
383 t1, i);
384 break;
387 i++;
390 if (ret < 0) {
391 return ret;
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 */
399 if (bwidth == 0) {
400 bwidth = 0.000001;
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();
416 return 0;
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 */
426 while (true) {
427 int bytes_sent;
429 bytes_sent = ram_save_block(f);
430 /* no more blocks to sent */
431 if (bytes_sent < 0) {
432 break;
434 bytes_transferred += bytes_sent;
436 memory_global_dirty_log_stop();
438 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
440 return 0;
443 static inline void *host_from_stream_offset(QEMUFile *f,
444 ram_addr_t offset,
445 int flags)
447 static RAMBlock *block = NULL;
448 char id[256];
449 uint8_t len;
451 if (flags & RAM_SAVE_FLAG_CONTINUE) {
452 if (!block) {
453 fprintf(stderr, "Ack, bad migration stream!\n");
454 return NULL;
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);
462 id[len] = 0;
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);
470 return NULL;
473 static int ram_load(QEMUFile *f, void *opaque, int version_id)
475 ram_addr_t addr;
476 int flags, ret = 0;
477 int error;
478 static uint64_t seq_iter;
480 seq_iter++;
482 if (version_id < 4 || version_id > 4) {
483 return -EINVAL;
486 do {
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 */
495 char id[256];
496 ram_addr_t length;
497 ram_addr_t total_ram_bytes = addr;
499 while (total_ram_bytes) {
500 RAMBlock *block;
501 uint8_t len;
503 len = qemu_get_byte(f);
504 qemu_get_buffer(f, (uint8_t *)id, len);
505 id[len] = 0;
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) {
511 ret = -EINVAL;
512 goto done;
514 break;
518 if (!block) {
519 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
520 "accept migration\n", id);
521 ret = -EINVAL;
522 goto done;
525 total_ram_bytes -= length;
530 if (flags & RAM_SAVE_FLAG_COMPRESS) {
531 void *host;
532 uint8_t ch;
534 host = host_from_stream_offset(f, addr, flags);
535 if (!host) {
536 return -EINVAL;
539 ch = qemu_get_byte(f);
540 memset(host, ch, TARGET_PAGE_SIZE);
541 #ifndef _WIN32
542 if (ch == 0 &&
543 (!kvm_enabled() || kvm_has_sync_mmu())) {
544 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
546 #endif
547 } else if (flags & RAM_SAVE_FLAG_PAGE) {
548 void *host;
550 host = host_from_stream_offset(f, addr, flags);
551 if (!host) {
552 return -EINVAL;
555 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
557 error = qemu_file_get_error(f);
558 if (error) {
559 ret = error;
560 goto done;
562 } while (!(flags & RAM_SAVE_FLAG_EOS));
564 done:
565 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64 "\n",
566 ret, seq_iter);
567 return ret;
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,
578 #ifdef HAS_AUDIO
579 struct soundhw {
580 const char *name;
581 const char *descr;
582 int enabled;
583 int isa;
584 union {
585 int (*init_isa) (ISABus *bus);
586 int (*init_pci) (PCIBus *bus);
587 } init;
590 static struct soundhw soundhw[] = {
591 #ifdef HAS_AUDIO_CHOICE
592 #ifdef CONFIG_PCSPK
594 "pcspk",
595 "PC speaker",
598 { .init_isa = pcspk_audio_init }
600 #endif
602 #ifdef CONFIG_SB16
604 "sb16",
605 "Creative Sound Blaster 16",
608 { .init_isa = SB16_init }
610 #endif
612 #ifdef CONFIG_CS4231A
614 "cs4231a",
615 "CS4231A",
618 { .init_isa = cs4231a_init }
620 #endif
622 #ifdef CONFIG_ADLIB
624 "adlib",
625 #ifdef HAS_YMF262
626 "Yamaha YMF262 (OPL3)",
627 #else
628 "Yamaha YM3812 (OPL2)",
629 #endif
632 { .init_isa = Adlib_init }
634 #endif
636 #ifdef CONFIG_GUS
638 "gus",
639 "Gravis Ultrasound GF1",
642 { .init_isa = GUS_init }
644 #endif
646 #ifdef CONFIG_AC97
648 "ac97",
649 "Intel 82801AA AC97 Audio",
652 { .init_pci = ac97_init }
654 #endif
656 #ifdef CONFIG_ES1370
658 "es1370",
659 "ENSONIQ AudioPCI ES1370",
662 { .init_pci = es1370_init }
664 #endif
666 #ifdef CONFIG_HDA
668 "hda",
669 "Intel HD Audio",
672 { .init_pci = intel_hda_and_codec_init }
674 #endif
676 #endif /* HAS_AUDIO_CHOICE */
678 { NULL, NULL, 0, 0, { NULL } }
681 void select_soundhw(const char *optarg)
683 struct soundhw *c;
685 if (is_help_option(optarg)) {
686 show_valid_cards:
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));
695 else {
696 size_t l;
697 const char *p;
698 char *e;
699 int bad_card = 0;
701 if (!strcmp(optarg, "all")) {
702 for (c = soundhw; c->name; ++c) {
703 c->enabled = 1;
705 return;
708 p = optarg;
709 while (*p) {
710 e = strchr(p, ',');
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]) {
715 c->enabled = 1;
716 break;
720 if (!c->name) {
721 if (l > 80) {
722 fprintf(stderr,
723 "Unknown sound card name (too big to show)\n");
725 else {
726 fprintf(stderr, "Unknown sound card name `%.*s'\n",
727 (int) l, p);
729 bad_card = 1;
731 p += l + (e != NULL);
734 if (bad_card) {
735 goto show_valid_cards;
740 void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
742 struct soundhw *c;
744 for (c = soundhw; c->name; ++c) {
745 if (c->enabled) {
746 if (c->isa) {
747 if (isa_bus) {
748 c->init.init_isa(isa_bus);
750 } else {
751 if (pci_bus) {
752 c->init.init_pci(pci_bus);
758 #else
759 void select_soundhw(const char *optarg)
762 void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
765 #endif
767 int qemu_uuid_parse(const char *str, uint8_t *uuid)
769 int ret;
771 if (strlen(str) != 36) {
772 return -1;
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],
778 &uuid[15]);
780 if (ret != 16) {
781 return -1;
783 #ifdef TARGET_I386
784 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
785 #endif
786 return 0;
789 void do_acpitable_option(const char *optarg)
791 #ifdef TARGET_I386
792 if (acpi_table_add(optarg) < 0) {
793 fprintf(stderr, "Wrong acpi table provided\n");
794 exit(1);
796 #endif
799 void do_smbios_option(const char *optarg)
801 #ifdef TARGET_I386
802 if (smbios_entry_add(optarg) < 0) {
803 fprintf(stderr, "Wrong smbios provided\n");
804 exit(1);
806 #endif
809 void cpudef_init(void)
811 #if defined(cpudef_setup)
812 cpudef_setup(); /* parse cpu definitions in target config file */
813 #endif
816 int audio_available(void)
818 #ifdef HAS_AUDIO
819 return 1;
820 #else
821 return 0;
822 #endif
825 int tcg_available(void)
827 return 1;
830 int kvm_available(void)
832 #ifdef CONFIG_KVM
833 return 1;
834 #else
835 return 0;
836 #endif
839 int xen_available(void)
841 #ifdef CONFIG_XEN
842 return 1;
843 #else
844 return 0;
845 #endif