migration: move ram stuff to migration/ram
[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 #include <zlib.h>
28 #ifndef _WIN32
29 #include <sys/types.h>
30 #include <sys/mman.h>
31 #endif
32 #include "config.h"
33 #include "monitor/monitor.h"
34 #include "sysemu/sysemu.h"
35 #include "qemu/bitops.h"
36 #include "qemu/bitmap.h"
37 #include "sysemu/arch_init.h"
38 #include "audio/audio.h"
39 #include "hw/i386/pc.h"
40 #include "hw/pci/pci.h"
41 #include "hw/audio/audio.h"
42 #include "sysemu/kvm.h"
43 #include "migration/migration.h"
44 #include "hw/i386/smbios.h"
45 #include "exec/address-spaces.h"
46 #include "hw/audio/pcspk.h"
47 #include "migration/page_cache.h"
48 #include "qemu/config-file.h"
49 #include "qemu/error-report.h"
50 #include "qmp-commands.h"
51 #include "trace.h"
52 #include "exec/cpu-all.h"
53 #include "exec/ram_addr.h"
54 #include "hw/acpi/acpi.h"
55 #include "qemu/host-utils.h"
56 #include "qemu/rcu_queue.h"
57
58 #ifdef TARGET_SPARC
59 int graphic_width = 1024;
60 int graphic_height = 768;
61 int graphic_depth = 8;
62 #else
63 int graphic_width = 800;
64 int graphic_height = 600;
65 int graphic_depth = 32;
66 #endif
67
68
69 #if defined(TARGET_ALPHA)
70 #define QEMU_ARCH QEMU_ARCH_ALPHA
71 #elif defined(TARGET_ARM)
72 #define QEMU_ARCH QEMU_ARCH_ARM
73 #elif defined(TARGET_CRIS)
74 #define QEMU_ARCH QEMU_ARCH_CRIS
75 #elif defined(TARGET_I386)
76 #define QEMU_ARCH QEMU_ARCH_I386
77 #elif defined(TARGET_M68K)
78 #define QEMU_ARCH QEMU_ARCH_M68K
79 #elif defined(TARGET_LM32)
80 #define QEMU_ARCH QEMU_ARCH_LM32
81 #elif defined(TARGET_MICROBLAZE)
82 #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
83 #elif defined(TARGET_MIPS)
84 #define QEMU_ARCH QEMU_ARCH_MIPS
85 #elif defined(TARGET_MOXIE)
86 #define QEMU_ARCH QEMU_ARCH_MOXIE
87 #elif defined(TARGET_OPENRISC)
88 #define QEMU_ARCH QEMU_ARCH_OPENRISC
89 #elif defined(TARGET_PPC)
90 #define QEMU_ARCH QEMU_ARCH_PPC
91 #elif defined(TARGET_S390X)
92 #define QEMU_ARCH QEMU_ARCH_S390X
93 #elif defined(TARGET_SH4)
94 #define QEMU_ARCH QEMU_ARCH_SH4
95 #elif defined(TARGET_SPARC)
96 #define QEMU_ARCH QEMU_ARCH_SPARC
97 #elif defined(TARGET_XTENSA)
98 #define QEMU_ARCH QEMU_ARCH_XTENSA
99 #elif defined(TARGET_UNICORE32)
100 #define QEMU_ARCH QEMU_ARCH_UNICORE32
101 #elif defined(TARGET_TRICORE)
102 #define QEMU_ARCH QEMU_ARCH_TRICORE
103 #endif
104
105 const uint32_t arch_type = QEMU_ARCH;
106
107 static struct defconfig_file {
108     const char *filename;
109     /* Indicates it is an user config file (disabled by -no-user-config) */
110     bool userconfig;
111 } default_config_files[] = {
112     { CONFIG_QEMU_CONFDIR "/qemu.conf",                   true },
113     { NULL }, /* end of list */
114 };
115
116 int qemu_read_default_config_files(bool userconfig)
117 {
118     int ret;
119     struct defconfig_file *f;
120
121     for (f = default_config_files; f->filename; f++) {
122         if (!userconfig && f->userconfig) {
123             continue;
124         }
125         ret = qemu_read_config_file(f->filename);
126         if (ret < 0 && ret != -ENOENT) {
127             return ret;
128         }
129     }
130
131     return 0;
132 }
133
134 struct soundhw {
135     const char *name;
136     const char *descr;
137     int enabled;
138     int isa;
139     union {
140         int (*init_isa) (ISABus *bus);
141         int (*init_pci) (PCIBus *bus);
142     } init;
143 };
144
145 static struct soundhw soundhw[9];
146 static int soundhw_count;
147
148 void isa_register_soundhw(const char *name, const char *descr,
149                           int (*init_isa)(ISABus *bus))
150 {
151     assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
152     soundhw[soundhw_count].name = name;
153     soundhw[soundhw_count].descr = descr;
154     soundhw[soundhw_count].isa = 1;
155     soundhw[soundhw_count].init.init_isa = init_isa;
156     soundhw_count++;
157 }
158
159 void pci_register_soundhw(const char *name, const char *descr,
160                           int (*init_pci)(PCIBus *bus))
161 {
162     assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
163     soundhw[soundhw_count].name = name;
164     soundhw[soundhw_count].descr = descr;
165     soundhw[soundhw_count].isa = 0;
166     soundhw[soundhw_count].init.init_pci = init_pci;
167     soundhw_count++;
168 }
169
170 void select_soundhw(const char *optarg)
171 {
172     struct soundhw *c;
173
174     if (is_help_option(optarg)) {
175     show_valid_cards:
176
177         if (soundhw_count) {
178              printf("Valid sound card names (comma separated):\n");
179              for (c = soundhw; c->name; ++c) {
180                  printf ("%-11s %s\n", c->name, c->descr);
181              }
182              printf("\n-soundhw all will enable all of the above\n");
183         } else {
184              printf("Machine has no user-selectable audio hardware "
185                     "(it may or may not have always-present audio hardware).\n");
186         }
187         exit(!is_help_option(optarg));
188     }
189     else {
190         size_t l;
191         const char *p;
192         char *e;
193         int bad_card = 0;
194
195         if (!strcmp(optarg, "all")) {
196             for (c = soundhw; c->name; ++c) {
197                 c->enabled = 1;
198             }
199             return;
200         }
201
202         p = optarg;
203         while (*p) {
204             e = strchr(p, ',');
205             l = !e ? strlen(p) : (size_t) (e - p);
206
207             for (c = soundhw; c->name; ++c) {
208                 if (!strncmp(c->name, p, l) && !c->name[l]) {
209                     c->enabled = 1;
210                     break;
211                 }
212             }
213
214             if (!c->name) {
215                 if (l > 80) {
216                     error_report("Unknown sound card name (too big to show)");
217                 }
218                 else {
219                     error_report("Unknown sound card name `%.*s'",
220                                  (int) l, p);
221                 }
222                 bad_card = 1;
223             }
224             p += l + (e != NULL);
225         }
226
227         if (bad_card) {
228             goto show_valid_cards;
229         }
230     }
231 }
232
233 void audio_init(void)
234 {
235     struct soundhw *c;
236     ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
237     PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
238
239     for (c = soundhw; c->name; ++c) {
240         if (c->enabled) {
241             if (c->isa) {
242                 if (!isa_bus) {
243                     error_report("ISA bus not available for %s", c->name);
244                     exit(1);
245                 }
246                 c->init.init_isa(isa_bus);
247             } else {
248                 if (!pci_bus) {
249                     error_report("PCI bus not available for %s", c->name);
250                     exit(1);
251                 }
252                 c->init.init_pci(pci_bus);
253             }
254         }
255     }
256 }
257
258 int qemu_uuid_parse(const char *str, uint8_t *uuid)
259 {
260     int ret;
261
262     if (strlen(str) != 36) {
263         return -1;
264     }
265
266     ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
267                  &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
268                  &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
269                  &uuid[15]);
270
271     if (ret != 16) {
272         return -1;
273     }
274     return 0;
275 }
276
277 void do_acpitable_option(const QemuOpts *opts)
278 {
279 #ifdef TARGET_I386
280     Error *err = NULL;
281
282     acpi_table_add(opts, &err);
283     if (err) {
284         error_report("Wrong acpi table provided: %s",
285                      error_get_pretty(err));
286         error_free(err);
287         exit(1);
288     }
289 #endif
290 }
291
292 void do_smbios_option(QemuOpts *opts)
293 {
294 #ifdef TARGET_I386
295     smbios_entry_add(opts);
296 #endif
297 }
298
299 void cpudef_init(void)
300 {
301 #if defined(cpudef_setup)
302     cpudef_setup(); /* parse cpu definitions in target config file */
303 #endif
304 }
305
306 int kvm_available(void)
307 {
308 #ifdef CONFIG_KVM
309     return 1;
310 #else
311     return 0;
312 #endif
313 }
314
315 int xen_available(void)
316 {
317 #ifdef CONFIG_XEN
318     return 1;
319 #else
320     return 0;
321 #endif
322 }
323
324
325 TargetInfo *qmp_query_target(Error **errp)
326 {
327     TargetInfo *info = g_malloc0(sizeof(*info));
328
329     info->arch = g_strdup(TARGET_NAME);
330
331     return info;
332 }