Initialize
[sdk/emulator/qemu.git] / arch_init.c
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
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
45 #ifdef TARGET_SPARC
46 int graphic_width = 1024;
47 int graphic_height = 768;
48 int graphic_depth = 8;
49 #else
50 int graphic_width = 800;
51 int graphic_height = 600;
52 int graphic_depth = 15;
53 #endif
54
55 const char arch_config_name[] = CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf";
56
57 #if defined(TARGET_ALPHA)
58 #define QEMU_ARCH QEMU_ARCH_ALPHA
59 #elif defined(TARGET_ARM)
60 #define QEMU_ARCH QEMU_ARCH_ARM
61 #elif defined(TARGET_CRIS)
62 #define QEMU_ARCH QEMU_ARCH_CRIS
63 #elif defined(TARGET_I386)
64 #define QEMU_ARCH QEMU_ARCH_I386
65 #elif defined(TARGET_M68K)
66 #define QEMU_ARCH QEMU_ARCH_M68K
67 #elif defined(TARGET_MICROBLAZE)
68 #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
69 #elif defined(TARGET_MIPS)
70 #define QEMU_ARCH QEMU_ARCH_MIPS
71 #elif defined(TARGET_PPC)
72 #define QEMU_ARCH QEMU_ARCH_PPC
73 #elif defined(TARGET_S390X)
74 #define QEMU_ARCH QEMU_ARCH_S390X
75 #elif defined(TARGET_SH4)
76 #define QEMU_ARCH QEMU_ARCH_SH4
77 #elif defined(TARGET_SPARC)
78 #define QEMU_ARCH QEMU_ARCH_SPARC
79 #endif
80
81 const uint32_t arch_type = QEMU_ARCH;
82
83 /***********************************************************/
84 /* ram save/restore */
85
86 #define RAM_SAVE_FLAG_FULL     0x01 /* Obsolete, not used anymore */
87 #define RAM_SAVE_FLAG_COMPRESS 0x02
88 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
89 #define RAM_SAVE_FLAG_PAGE     0x08
90 #define RAM_SAVE_FLAG_EOS      0x10
91 #define RAM_SAVE_FLAG_CONTINUE 0x20
92
93 static int is_dup_page(uint8_t *page, uint8_t ch)
94 {
95     uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
96     uint32_t *array = (uint32_t *)page;
97     int i;
98
99     for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
100         if (array[i] != val) {
101             return 0;
102         }
103     }
104
105     return 1;
106 }
107
108 static RAMBlock *last_block;
109 static ram_addr_t last_offset;
110
111 static int ram_save_block(QEMUFile *f)
112 {
113     RAMBlock *block = last_block;
114     ram_addr_t offset = last_offset;
115     ram_addr_t current_addr;
116     int bytes_sent = 0;
117
118     if (!block)
119         block = QLIST_FIRST(&ram_list.blocks);
120
121     current_addr = block->offset + offset;
122
123     do {
124         if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
125             uint8_t *p;
126             int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
127
128             cpu_physical_memory_reset_dirty(current_addr,
129                                             current_addr + TARGET_PAGE_SIZE,
130                                             MIGRATION_DIRTY_FLAG);
131
132             p = block->host + offset;
133
134             if (is_dup_page(p, *p)) {
135                 qemu_put_be64(f, offset | cont | RAM_SAVE_FLAG_COMPRESS);
136                 if (!cont) {
137                     qemu_put_byte(f, strlen(block->idstr));
138                     qemu_put_buffer(f, (uint8_t *)block->idstr,
139                                     strlen(block->idstr));
140                 }
141                 qemu_put_byte(f, *p);
142                 bytes_sent = 1;
143             } else {
144                 qemu_put_be64(f, offset | cont | RAM_SAVE_FLAG_PAGE);
145                 if (!cont) {
146                     qemu_put_byte(f, strlen(block->idstr));
147                     qemu_put_buffer(f, (uint8_t *)block->idstr,
148                                     strlen(block->idstr));
149                 }
150                 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
151                 bytes_sent = TARGET_PAGE_SIZE;
152             }
153
154             break;
155         }
156
157         offset += TARGET_PAGE_SIZE;
158         if (offset >= block->length) {
159             offset = 0;
160             block = QLIST_NEXT(block, next);
161             if (!block)
162                 block = QLIST_FIRST(&ram_list.blocks);
163         }
164
165         current_addr = block->offset + offset;
166
167     } while (current_addr != last_block->offset + last_offset);
168
169     last_block = block;
170     last_offset = offset;
171
172     return bytes_sent;
173 }
174
175 static uint64_t bytes_transferred;
176
177 static ram_addr_t ram_save_remaining(void)
178 {
179     RAMBlock *block;
180     ram_addr_t count = 0;
181
182     QLIST_FOREACH(block, &ram_list.blocks, next) {
183         ram_addr_t addr;
184         for (addr = block->offset; addr < block->offset + block->length;
185              addr += TARGET_PAGE_SIZE) {
186             if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) {
187                 count++;
188             }
189         }
190     }
191
192     return count;
193 }
194
195 uint64_t ram_bytes_remaining(void)
196 {
197     return ram_save_remaining() * TARGET_PAGE_SIZE;
198 }
199
200 uint64_t ram_bytes_transferred(void)
201 {
202     return bytes_transferred;
203 }
204
205 uint64_t ram_bytes_total(void)
206 {
207     RAMBlock *block;
208     uint64_t total = 0;
209
210     QLIST_FOREACH(block, &ram_list.blocks, next)
211         total += block->length;
212
213     return total;
214 }
215
216 static int block_compar(const void *a, const void *b)
217 {
218     RAMBlock * const *ablock = a;
219     RAMBlock * const *bblock = b;
220     if ((*ablock)->offset < (*bblock)->offset) {
221         return -1;
222     } else if ((*ablock)->offset > (*bblock)->offset) {
223         return 1;
224     }
225     return 0;
226 }
227
228 static void sort_ram_list(void)
229 {
230     RAMBlock *block, *nblock, **blocks;
231     int n;
232     n = 0;
233     QLIST_FOREACH(block, &ram_list.blocks, next) {
234         ++n;
235     }
236     blocks = qemu_malloc(n * sizeof *blocks);
237     n = 0;
238     QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
239         blocks[n++] = block;
240         QLIST_REMOVE(block, next);
241     }
242     qsort(blocks, n, sizeof *blocks, block_compar);
243     while (--n >= 0) {
244         QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
245     }
246     qemu_free(blocks);
247 }
248
249 int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
250 {
251     ram_addr_t addr;
252     uint64_t bytes_transferred_last;
253     double bwidth = 0;
254     uint64_t expected_time = 0;
255
256     if (stage < 0) {
257         cpu_physical_memory_set_dirty_tracking(0);
258         return 0;
259     }
260
261     if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
262         qemu_file_set_error(f);
263         return 0;
264     }
265
266     if (stage == 1) {
267         RAMBlock *block;
268         bytes_transferred = 0;
269         last_block = NULL;
270         last_offset = 0;
271         sort_ram_list();
272
273         /* Make sure all dirty bits are set */
274         QLIST_FOREACH(block, &ram_list.blocks, next) {
275             for (addr = block->offset; addr < block->offset + block->length;
276                  addr += TARGET_PAGE_SIZE) {
277                 if (!cpu_physical_memory_get_dirty(addr,
278                                                    MIGRATION_DIRTY_FLAG)) {
279                     cpu_physical_memory_set_dirty(addr);
280                 }
281             }
282         }
283
284         /* Enable dirty memory tracking */
285         cpu_physical_memory_set_dirty_tracking(1);
286
287         qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
288
289         QLIST_FOREACH(block, &ram_list.blocks, next) {
290             qemu_put_byte(f, strlen(block->idstr));
291             qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
292             qemu_put_be64(f, block->length);
293         }
294     }
295
296     bytes_transferred_last = bytes_transferred;
297     bwidth = qemu_get_clock_ns(rt_clock);
298
299     while (!qemu_file_rate_limit(f)) {
300         int bytes_sent;
301
302         bytes_sent = ram_save_block(f);
303         bytes_transferred += bytes_sent;
304                 monitor_printf(mon, "Completed=%f\n",(float)bytes_transferred/(float)ram_bytes_total());
305         if (bytes_sent == 0) { /* no more blocks */
306             break;
307         }
308     }
309
310     bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
311     bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
312
313     /* if we haven't transferred anything this round, force expected_time to a
314      * a very high value, but without crashing */
315     if (bwidth == 0) {
316         bwidth = 0.000001;
317     }
318
319     /* try transferring iterative blocks of memory */
320     if (stage == 3) {
321         int bytes_sent;
322
323         /* flush all remaining blocks regardless of rate limiting */
324         while ((bytes_sent = ram_save_block(f)) != 0) {
325             bytes_transferred += bytes_sent;
326         }
327         cpu_physical_memory_set_dirty_tracking(0);
328     }
329
330     qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
331
332     expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
333
334     return (stage == 2) && (expected_time <= migrate_max_downtime());
335 }
336
337 static inline void *host_from_stream_offset(QEMUFile *f,
338                                             ram_addr_t offset,
339                                             int flags)
340 {
341     static RAMBlock *block = NULL;
342     char id[256];
343     uint8_t len;
344
345     if (flags & RAM_SAVE_FLAG_CONTINUE) {
346         if (!block) {
347             fprintf(stderr, "Ack, bad migration stream!\n");
348             return NULL;
349         }
350
351         return block->host + offset;
352     }
353
354     len = qemu_get_byte(f);
355     qemu_get_buffer(f, (uint8_t *)id, len);
356     id[len] = 0;
357
358     QLIST_FOREACH(block, &ram_list.blocks, next) {
359         if (!strncmp(id, block->idstr, sizeof(id)))
360             return block->host + offset;
361     }
362
363     fprintf(stderr, "Can't find block %s!\n", id);
364     return NULL;
365 }
366
367 int ram_load(QEMUFile *f, void *opaque, int version_id)
368 {
369     ram_addr_t addr;
370     int flags;
371
372     if (version_id < 3 || version_id > 4) {
373         return -EINVAL;
374     }
375
376     do {
377         addr = qemu_get_be64(f);
378
379         flags = addr & ~TARGET_PAGE_MASK;
380         addr &= TARGET_PAGE_MASK;
381
382         if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
383             if (version_id == 3) {
384                 if (addr != ram_bytes_total()) {
385                     return -EINVAL;
386                 }
387             } else {
388                 /* Synchronize RAM block list */
389                 char id[256];
390                 ram_addr_t length;
391                 ram_addr_t total_ram_bytes = addr;
392
393                 while (total_ram_bytes) {
394                     RAMBlock *block;
395                     uint8_t len;
396
397                     len = qemu_get_byte(f);
398                     qemu_get_buffer(f, (uint8_t *)id, len);
399                     id[len] = 0;
400                     length = qemu_get_be64(f);
401
402                     QLIST_FOREACH(block, &ram_list.blocks, next) {
403                         if (!strncmp(id, block->idstr, sizeof(id))) {
404                             if (block->length != length)
405                                 return -EINVAL;
406                             break;
407                         }
408                     }
409
410                     if (!block) {
411                         fprintf(stderr, "Unknown ramblock \"%s\", cannot "
412                                 "accept migration\n", id);
413                         return -EINVAL;
414                     }
415
416                     total_ram_bytes -= length;
417                 }
418             }
419         }
420
421         if (flags & RAM_SAVE_FLAG_COMPRESS) {
422             void *host;
423             uint8_t ch;
424
425             if (version_id == 3)
426                 host = qemu_get_ram_ptr(addr);
427             else
428                 host = host_from_stream_offset(f, addr, flags);
429             if (!host) {
430                 return -EINVAL;
431             }
432
433             ch = qemu_get_byte(f);
434             memset(host, ch, TARGET_PAGE_SIZE);
435 #ifndef _WIN32
436             if (ch == 0 &&
437                 (!kvm_enabled() || kvm_has_sync_mmu())) {
438                 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
439             }
440 #endif
441         } else if (flags & RAM_SAVE_FLAG_PAGE) {
442             void *host;
443
444             if (version_id == 3)
445                 host = qemu_get_ram_ptr(addr);
446             else
447                 host = host_from_stream_offset(f, addr, flags);
448
449             qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
450         }
451         if (qemu_file_has_error(f)) {
452             return -EIO;
453         }
454     } while (!(flags & RAM_SAVE_FLAG_EOS));
455
456     return 0;
457 }
458
459 void qemu_service_io(void)
460 {
461     qemu_notify_event();
462 }
463
464 #ifdef HAS_AUDIO
465 struct soundhw {
466     const char *name;
467     const char *descr;
468     int enabled;
469     int isa;
470     union {
471         int (*init_isa) (qemu_irq *pic);
472         int (*init_pci) (PCIBus *bus);
473     } init;
474 };
475
476 static struct soundhw soundhw[] = {
477 #ifdef HAS_AUDIO_CHOICE
478 #if defined(TARGET_I386) || defined(TARGET_MIPS)
479     {
480         "pcspk",
481         "PC speaker",
482         0,
483         1,
484         { .init_isa = pcspk_audio_init }
485     },
486 #endif
487
488 #ifdef CONFIG_SB16
489     {
490         "sb16",
491         "Creative Sound Blaster 16",
492         0,
493         1,
494         { .init_isa = SB16_init }
495     },
496 #endif
497
498 #ifdef CONFIG_CS4231A
499     {
500         "cs4231a",
501         "CS4231A",
502         0,
503         1,
504         { .init_isa = cs4231a_init }
505     },
506 #endif
507
508 #ifdef CONFIG_ADLIB
509     {
510         "adlib",
511 #ifdef HAS_YMF262
512         "Yamaha YMF262 (OPL3)",
513 #else
514         "Yamaha YM3812 (OPL2)",
515 #endif
516         0,
517         1,
518         { .init_isa = Adlib_init }
519     },
520 #endif
521
522 #ifdef CONFIG_GUS
523     {
524         "gus",
525         "Gravis Ultrasound GF1",
526         0,
527         1,
528         { .init_isa = GUS_init }
529     },
530 #endif
531
532 #ifdef CONFIG_AC97
533     {
534         "ac97",
535         "Intel 82801AA AC97 Audio",
536         0,
537         0,
538         { .init_pci = ac97_init }
539     },
540 #endif
541
542 #ifdef CONFIG_ES1370
543     {
544         "es1370",
545         "ENSONIQ AudioPCI ES1370",
546         0,
547         0,
548         { .init_pci = es1370_init }
549     },
550 #endif
551
552 #ifdef CONFIG_HDA
553     {
554         "hda",
555         "Intel HD Audio",
556         0,
557         0,
558         { .init_pci = intel_hda_and_codec_init }
559     },
560 #endif
561
562 #endif /* HAS_AUDIO_CHOICE */
563
564     { NULL, NULL, 0, 0, { NULL } }
565 };
566
567 void select_soundhw(const char *optarg)
568 {
569     struct soundhw *c;
570
571     if (*optarg == '?') {
572     show_valid_cards:
573
574         printf("Valid sound card names (comma separated):\n");
575         for (c = soundhw; c->name; ++c) {
576             printf ("%-11s %s\n", c->name, c->descr);
577         }
578         printf("\n-soundhw all will enable all of the above\n");
579         exit(*optarg != '?');
580     }
581     else {
582         size_t l;
583         const char *p;
584         char *e;
585         int bad_card = 0;
586
587         if (!strcmp(optarg, "all")) {
588             for (c = soundhw; c->name; ++c) {
589                 c->enabled = 1;
590             }
591             return;
592         }
593
594         p = optarg;
595         while (*p) {
596             e = strchr(p, ',');
597             l = !e ? strlen(p) : (size_t) (e - p);
598
599             for (c = soundhw; c->name; ++c) {
600                 if (!strncmp(c->name, p, l) && !c->name[l]) {
601                     c->enabled = 1;
602                     break;
603                 }
604             }
605
606             if (!c->name) {
607                 if (l > 80) {
608                     fprintf(stderr,
609                             "Unknown sound card name (too big to show)\n");
610                 }
611                 else {
612                     fprintf(stderr, "Unknown sound card name `%.*s'\n",
613                             (int) l, p);
614                 }
615                 bad_card = 1;
616             }
617             p += l + (e != NULL);
618         }
619
620         if (bad_card) {
621             goto show_valid_cards;
622         }
623     }
624 }
625
626 void audio_init(qemu_irq *isa_pic, PCIBus *pci_bus)
627 {
628     struct soundhw *c;
629
630     for (c = soundhw; c->name; ++c) {
631         if (c->enabled) {
632             if (c->isa) {
633                 if (isa_pic) {
634                     c->init.init_isa(isa_pic);
635                 }
636             } else {
637                 if (pci_bus) {
638                     c->init.init_pci(pci_bus);
639                 }
640             }
641         }
642     }
643 }
644 #else
645 void select_soundhw(const char *optarg)
646 {
647 }
648 void audio_init(qemu_irq *isa_pic, PCIBus *pci_bus)
649 {
650 }
651 #endif
652
653 int qemu_uuid_parse(const char *str, uint8_t *uuid)
654 {
655     int ret;
656
657     if (strlen(str) != 36) {
658         return -1;
659     }
660
661     ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
662                  &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
663                  &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
664                  &uuid[15]);
665
666     if (ret != 16) {
667         return -1;
668     }
669 #ifdef TARGET_I386
670     smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
671 #endif
672     return 0;
673 }
674
675 void do_acpitable_option(const char *optarg)
676 {
677 #ifdef TARGET_I386
678     if (acpi_table_add(optarg) < 0) {
679         fprintf(stderr, "Wrong acpi table provided\n");
680         exit(1);
681     }
682 #endif
683 }
684
685 void do_smbios_option(const char *optarg)
686 {
687 #ifdef TARGET_I386
688     if (smbios_entry_add(optarg) < 0) {
689         fprintf(stderr, "Wrong smbios provided\n");
690         exit(1);
691     }
692 #endif
693 }
694
695 void cpudef_init(void)
696 {
697 #if defined(cpudef_setup)
698     cpudef_setup(); /* parse cpu definitions in target config file */
699 #endif
700 }
701
702 int audio_available(void)
703 {
704 #ifdef HAS_AUDIO
705     return 1;
706 #else
707     return 0;
708 #endif
709 }
710
711 int kvm_available(void)
712 {
713 #ifdef CONFIG_KVM
714     return 1;
715 #else
716     return 0;
717 #endif
718 }
719
720 int xen_available(void)
721 {
722 #ifdef CONFIG_XEN
723     return 1;
724 #else
725     return 0;
726 #endif
727 }