]> Git Repo - qemu.git/blob - vl.c
-machine vmport=off: Allow disabling of VMWare ioport emulation
[qemu.git] / vl.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 <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30
31 #include "config-host.h"
32
33 #ifdef CONFIG_SECCOMP
34 #include "sysemu/seccomp.h"
35 #endif
36
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
39 #endif
40
41 #ifdef CONFIG_SDL
42 #if defined(__APPLE__) || defined(main)
43 #include <SDL.h>
44 int qemu_main(int argc, char **argv, char **envp);
45 int main(int argc, char **argv)
46 {
47     return qemu_main(argc, argv, NULL);
48 }
49 #undef main
50 #define main qemu_main
51 #endif
52 #endif /* CONFIG_SDL */
53
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
58
59 #include <glib.h>
60
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
65 #include "hw/usb.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
68 #include "hw/bt.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
72 #include "hw/qdev.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
76 #include "net/net.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
99 #ifdef CONFIG_VIRTFS
100 #include "fsdev/qemu-fsdev.h"
101 #endif
102 #include "sysemu/qtest.h"
103
104 #include "disas/disas.h"
105
106
107 #include "slirp/libslirp.h"
108
109 #include "trace.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
114 #include "qemu/osdep.h"
115
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
121
122 #define DEFAULT_RAM_SIZE 128
123
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
126
127 static const char *data_dir[16];
128 static int data_dir_idx;
129 const char *bios_name = NULL;
130 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
131 DisplayType display_type = DT_DEFAULT;
132 static int display_remote;
133 const char* keyboard_layout = NULL;
134 ram_addr_t ram_size;
135 const char *mem_path = NULL;
136 int mem_prealloc = 0; /* force preallocation of physical target memory */
137 bool enable_mlock = false;
138 int nb_nics;
139 NICInfo nd_table[MAX_NICS];
140 int autostart;
141 static int rtc_utc = 1;
142 static int rtc_date_offset = -1; /* -1 means no change */
143 QEMUClockType rtc_clock;
144 int vga_interface_type = VGA_NONE;
145 static int full_screen = 0;
146 static int no_frame = 0;
147 int no_quit = 0;
148 #ifdef CONFIG_GTK
149 static bool grab_on_hover;
150 #endif
151 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
152 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
153 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
154 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
155 int win2k_install_hack = 0;
156 int singlestep = 0;
157 int smp_cpus = 1;
158 int max_cpus = 0;
159 int smp_cores = 1;
160 int smp_threads = 1;
161 #ifdef CONFIG_VNC
162 const char *vnc_display;
163 #endif
164 int acpi_enabled = 1;
165 int no_hpet = 0;
166 int fd_bootchk = 1;
167 static int no_reboot;
168 int no_shutdown = 0;
169 int cursor_hide = 1;
170 int graphic_rotate = 0;
171 const char *watchdog;
172 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
173 int nb_option_roms;
174 int semihosting_enabled = 0;
175 int old_param = 0;
176 const char *qemu_name;
177 int alt_grab = 0;
178 int ctrl_grab = 0;
179 unsigned int nb_prom_envs = 0;
180 const char *prom_envs[MAX_PROM_ENVS];
181 int boot_menu;
182 bool boot_strict;
183 uint8_t *boot_splash_filedata;
184 size_t boot_splash_filedata_size;
185 uint8_t qemu_extra_params_fw[2];
186
187 int icount_align_option;
188
189 int nb_numa_nodes;
190 int max_numa_nodeid;
191 NodeInfo numa_info[MAX_NODES];
192
193 uint8_t qemu_uuid[16];
194 bool qemu_uuid_set;
195
196 static QEMUBootSetHandler *boot_set_handler;
197 static void *boot_set_opaque;
198
199 static NotifierList exit_notifiers =
200     NOTIFIER_LIST_INITIALIZER(exit_notifiers);
201
202 static NotifierList machine_init_done_notifiers =
203     NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
204
205 bool xen_allowed;
206 uint32_t xen_domid;
207 enum xen_mode xen_mode = XEN_EMULATE;
208
209 static int has_defaults = 1;
210 static int default_serial = 1;
211 static int default_parallel = 1;
212 static int default_virtcon = 1;
213 static int default_sclp = 1;
214 static int default_monitor = 1;
215 static int default_floppy = 1;
216 static int default_cdrom = 1;
217 static int default_sdcard = 1;
218 static int default_vga = 1;
219
220 static struct {
221     const char *driver;
222     int *flag;
223 } default_list[] = {
224     { .driver = "isa-serial",           .flag = &default_serial    },
225     { .driver = "isa-parallel",         .flag = &default_parallel  },
226     { .driver = "isa-fdc",              .flag = &default_floppy    },
227     { .driver = "ide-cd",               .flag = &default_cdrom     },
228     { .driver = "ide-hd",               .flag = &default_cdrom     },
229     { .driver = "ide-drive",            .flag = &default_cdrom     },
230     { .driver = "scsi-cd",              .flag = &default_cdrom     },
231     { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
232     { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
233     { .driver = "virtio-serial",        .flag = &default_virtcon   },
234     { .driver = "VGA",                  .flag = &default_vga       },
235     { .driver = "isa-vga",              .flag = &default_vga       },
236     { .driver = "cirrus-vga",           .flag = &default_vga       },
237     { .driver = "isa-cirrus-vga",       .flag = &default_vga       },
238     { .driver = "vmware-svga",          .flag = &default_vga       },
239     { .driver = "qxl-vga",              .flag = &default_vga       },
240 };
241
242 static QemuOptsList qemu_rtc_opts = {
243     .name = "rtc",
244     .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
245     .desc = {
246         {
247             .name = "base",
248             .type = QEMU_OPT_STRING,
249         },{
250             .name = "clock",
251             .type = QEMU_OPT_STRING,
252         },{
253             .name = "driftfix",
254             .type = QEMU_OPT_STRING,
255         },
256         { /* end of list */ }
257     },
258 };
259
260 static QemuOptsList qemu_sandbox_opts = {
261     .name = "sandbox",
262     .implied_opt_name = "enable",
263     .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
264     .desc = {
265         {
266             .name = "enable",
267             .type = QEMU_OPT_BOOL,
268         },
269         { /* end of list */ }
270     },
271 };
272
273 static QemuOptsList qemu_trace_opts = {
274     .name = "trace",
275     .implied_opt_name = "trace",
276     .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
277     .desc = {
278         {
279             .name = "events",
280             .type = QEMU_OPT_STRING,
281         },{
282             .name = "file",
283             .type = QEMU_OPT_STRING,
284         },
285         { /* end of list */ }
286     },
287 };
288
289 static QemuOptsList qemu_option_rom_opts = {
290     .name = "option-rom",
291     .implied_opt_name = "romfile",
292     .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
293     .desc = {
294         {
295             .name = "bootindex",
296             .type = QEMU_OPT_NUMBER,
297         }, {
298             .name = "romfile",
299             .type = QEMU_OPT_STRING,
300         },
301         { /* end of list */ }
302     },
303 };
304
305 static QemuOptsList qemu_machine_opts = {
306     .name = "machine",
307     .implied_opt_name = "type",
308     .merge_lists = true,
309     .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
310     .desc = {
311         {
312             .name = "type",
313             .type = QEMU_OPT_STRING,
314             .help = "emulated machine"
315         }, {
316             .name = "accel",
317             .type = QEMU_OPT_STRING,
318             .help = "accelerator list",
319         }, {
320             .name = "kernel_irqchip",
321             .type = QEMU_OPT_BOOL,
322             .help = "use KVM in-kernel irqchip",
323         }, {
324             .name = "kvm_shadow_mem",
325             .type = QEMU_OPT_SIZE,
326             .help = "KVM shadow MMU size",
327         }, {
328             .name = "kernel",
329             .type = QEMU_OPT_STRING,
330             .help = "Linux kernel image file",
331         }, {
332             .name = "initrd",
333             .type = QEMU_OPT_STRING,
334             .help = "Linux initial ramdisk file",
335         }, {
336             .name = "append",
337             .type = QEMU_OPT_STRING,
338             .help = "Linux kernel command line",
339         }, {
340             .name = "dtb",
341             .type = QEMU_OPT_STRING,
342             .help = "Linux kernel device tree file",
343         }, {
344             .name = "dumpdtb",
345             .type = QEMU_OPT_STRING,
346             .help = "Dump current dtb to a file and quit",
347         }, {
348             .name = "phandle_start",
349             .type = QEMU_OPT_NUMBER,
350             .help = "The first phandle ID we may generate dynamically",
351         }, {
352             .name = "dt_compatible",
353             .type = QEMU_OPT_STRING,
354             .help = "Overrides the \"compatible\" property of the dt root node",
355         }, {
356             .name = "dump-guest-core",
357             .type = QEMU_OPT_BOOL,
358             .help = "Include guest memory in  a core dump",
359         }, {
360             .name = "mem-merge",
361             .type = QEMU_OPT_BOOL,
362             .help = "enable/disable memory merge support",
363         },{
364             .name = "usb",
365             .type = QEMU_OPT_BOOL,
366             .help = "Set on/off to enable/disable usb",
367         },{
368             .name = "firmware",
369             .type = QEMU_OPT_STRING,
370             .help = "firmware image",
371         },{
372             .name = "kvm-type",
373             .type = QEMU_OPT_STRING,
374             .help = "Specifies the KVM virtualization mode (HV, PR)",
375         },{
376             .name = PC_MACHINE_MAX_RAM_BELOW_4G,
377             .type = QEMU_OPT_SIZE,
378             .help = "maximum ram below the 4G boundary (32bit boundary)",
379         }, {
380             .name = PC_MACHINE_VMPORT,
381             .type = QEMU_OPT_BOOL,
382             .help = "Enable vmport (pc & q35)",
383         },{
384             .name = "iommu",
385             .type = QEMU_OPT_BOOL,
386             .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
387         },
388         { /* End of list */ }
389     },
390 };
391
392 static QemuOptsList qemu_boot_opts = {
393     .name = "boot-opts",
394     .implied_opt_name = "order",
395     .merge_lists = true,
396     .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
397     .desc = {
398         {
399             .name = "order",
400             .type = QEMU_OPT_STRING,
401         }, {
402             .name = "once",
403             .type = QEMU_OPT_STRING,
404         }, {
405             .name = "menu",
406             .type = QEMU_OPT_BOOL,
407         }, {
408             .name = "splash",
409             .type = QEMU_OPT_STRING,
410         }, {
411             .name = "splash-time",
412             .type = QEMU_OPT_STRING,
413         }, {
414             .name = "reboot-timeout",
415             .type = QEMU_OPT_STRING,
416         }, {
417             .name = "strict",
418             .type = QEMU_OPT_BOOL,
419         },
420         { /*End of list */ }
421     },
422 };
423
424 static QemuOptsList qemu_add_fd_opts = {
425     .name = "add-fd",
426     .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
427     .desc = {
428         {
429             .name = "fd",
430             .type = QEMU_OPT_NUMBER,
431             .help = "file descriptor of which a duplicate is added to fd set",
432         },{
433             .name = "set",
434             .type = QEMU_OPT_NUMBER,
435             .help = "ID of the fd set to add fd to",
436         },{
437             .name = "opaque",
438             .type = QEMU_OPT_STRING,
439             .help = "free-form string used to describe fd",
440         },
441         { /* end of list */ }
442     },
443 };
444
445 static QemuOptsList qemu_object_opts = {
446     .name = "object",
447     .implied_opt_name = "qom-type",
448     .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
449     .desc = {
450         { }
451     },
452 };
453
454 static QemuOptsList qemu_tpmdev_opts = {
455     .name = "tpmdev",
456     .implied_opt_name = "type",
457     .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
458     .desc = {
459         /* options are defined in the TPM backends */
460         { /* end of list */ }
461     },
462 };
463
464 static QemuOptsList qemu_realtime_opts = {
465     .name = "realtime",
466     .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
467     .desc = {
468         {
469             .name = "mlock",
470             .type = QEMU_OPT_BOOL,
471         },
472         { /* end of list */ }
473     },
474 };
475
476 static QemuOptsList qemu_msg_opts = {
477     .name = "msg",
478     .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
479     .desc = {
480         {
481             .name = "timestamp",
482             .type = QEMU_OPT_BOOL,
483         },
484         { /* end of list */ }
485     },
486 };
487
488 static QemuOptsList qemu_name_opts = {
489     .name = "name",
490     .implied_opt_name = "guest",
491     .merge_lists = true,
492     .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
493     .desc = {
494         {
495             .name = "guest",
496             .type = QEMU_OPT_STRING,
497             .help = "Sets the name of the guest.\n"
498                     "This name will be displayed in the SDL window caption.\n"
499                     "The name will also be used for the VNC server",
500         }, {
501             .name = "process",
502             .type = QEMU_OPT_STRING,
503             .help = "Sets the name of the QEMU process, as shown in top etc",
504         }, {
505             .name = "debug-threads",
506             .type = QEMU_OPT_BOOL,
507             .help = "When enabled, name the individual threads; defaults off.\n"
508                     "NOTE: The thread names are for debugging and not a\n"
509                     "stable API.",
510         },
511         { /* End of list */ }
512     },
513 };
514
515 static QemuOptsList qemu_mem_opts = {
516     .name = "memory",
517     .implied_opt_name = "size",
518     .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
519     .merge_lists = true,
520     .desc = {
521         {
522             .name = "size",
523             .type = QEMU_OPT_SIZE,
524         },
525         {
526             .name = "slots",
527             .type = QEMU_OPT_NUMBER,
528         },
529         {
530             .name = "maxmem",
531             .type = QEMU_OPT_SIZE,
532         },
533         { /* end of list */ }
534     },
535 };
536
537 static QemuOptsList qemu_icount_opts = {
538     .name = "icount",
539     .implied_opt_name = "shift",
540     .merge_lists = true,
541     .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
542     .desc = {
543         {
544             .name = "shift",
545             .type = QEMU_OPT_STRING,
546         }, {
547             .name = "align",
548             .type = QEMU_OPT_BOOL,
549         },
550         { /* end of list */ }
551     },
552 };
553
554 /**
555  * Get machine options
556  *
557  * Returns: machine options (never null).
558  */
559 QemuOpts *qemu_get_machine_opts(void)
560 {
561     return qemu_find_opts_singleton("machine");
562 }
563
564 const char *qemu_get_vm_name(void)
565 {
566     return qemu_name;
567 }
568
569 static void res_free(void)
570 {
571     if (boot_splash_filedata != NULL) {
572         g_free(boot_splash_filedata);
573         boot_splash_filedata = NULL;
574     }
575 }
576
577 static int default_driver_check(QemuOpts *opts, void *opaque)
578 {
579     const char *driver = qemu_opt_get(opts, "driver");
580     int i;
581
582     if (!driver)
583         return 0;
584     for (i = 0; i < ARRAY_SIZE(default_list); i++) {
585         if (strcmp(default_list[i].driver, driver) != 0)
586             continue;
587         *(default_list[i].flag) = 0;
588     }
589     return 0;
590 }
591
592 /***********************************************************/
593 /* QEMU state */
594
595 static RunState current_run_state = RUN_STATE_PRELAUNCH;
596
597 /* We use RUN_STATE_MAX but any invalid value will do */
598 static RunState vmstop_requested = RUN_STATE_MAX;
599 static QemuMutex vmstop_lock;
600
601 typedef struct {
602     RunState from;
603     RunState to;
604 } RunStateTransition;
605
606 static const RunStateTransition runstate_transitions_def[] = {
607     /*     from      ->     to      */
608     { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
609     { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
610
611     { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
612     { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
613
614     { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
615     { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
616
617     { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
618     { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
619
620     { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
621     { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
622
623     { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
624     { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
625
626     { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
627     { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
628     { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
629
630     { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
631     { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
632
633     { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
634
635     { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
636     { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
637     { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
638     { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
639     { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
640     { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
641     { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
642     { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
643     { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
644     { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
645
646     { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
647
648     { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
649     { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
650
651     { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
652     { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
653     { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
654     { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
655
656     { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
657     { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
658
659     { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
660     { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
661
662     { RUN_STATE_MAX, RUN_STATE_MAX },
663 };
664
665 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
666
667 bool runstate_check(RunState state)
668 {
669     return current_run_state == state;
670 }
671
672 static void runstate_init(void)
673 {
674     const RunStateTransition *p;
675
676     memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
677     for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
678         runstate_valid_transitions[p->from][p->to] = true;
679     }
680
681     qemu_mutex_init(&vmstop_lock);
682 }
683
684 /* This function will abort() on invalid state transitions */
685 void runstate_set(RunState new_state)
686 {
687     assert(new_state < RUN_STATE_MAX);
688
689     if (!runstate_valid_transitions[current_run_state][new_state]) {
690         fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
691                 RunState_lookup[current_run_state],
692                 RunState_lookup[new_state]);
693         abort();
694     }
695     trace_runstate_set(new_state);
696     current_run_state = new_state;
697 }
698
699 int runstate_is_running(void)
700 {
701     return runstate_check(RUN_STATE_RUNNING);
702 }
703
704 bool runstate_needs_reset(void)
705 {
706     return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
707         runstate_check(RUN_STATE_SHUTDOWN);
708 }
709
710 StatusInfo *qmp_query_status(Error **errp)
711 {
712     StatusInfo *info = g_malloc0(sizeof(*info));
713
714     info->running = runstate_is_running();
715     info->singlestep = singlestep;
716     info->status = current_run_state;
717
718     return info;
719 }
720
721 static bool qemu_vmstop_requested(RunState *r)
722 {
723     qemu_mutex_lock(&vmstop_lock);
724     *r = vmstop_requested;
725     vmstop_requested = RUN_STATE_MAX;
726     qemu_mutex_unlock(&vmstop_lock);
727     return *r < RUN_STATE_MAX;
728 }
729
730 void qemu_system_vmstop_request_prepare(void)
731 {
732     qemu_mutex_lock(&vmstop_lock);
733 }
734
735 void qemu_system_vmstop_request(RunState state)
736 {
737     vmstop_requested = state;
738     qemu_mutex_unlock(&vmstop_lock);
739     qemu_notify_event();
740 }
741
742 void vm_start(void)
743 {
744     RunState requested;
745
746     qemu_vmstop_requested(&requested);
747     if (runstate_is_running() && requested == RUN_STATE_MAX) {
748         return;
749     }
750
751     /* Ensure that a STOP/RESUME pair of events is emitted if a
752      * vmstop request was pending.  The BLOCK_IO_ERROR event, for
753      * example, according to documentation is always followed by
754      * the STOP event.
755      */
756     if (runstate_is_running()) {
757         qapi_event_send_stop(&error_abort);
758     } else {
759         cpu_enable_ticks();
760         runstate_set(RUN_STATE_RUNNING);
761         vm_state_notify(1, RUN_STATE_RUNNING);
762         resume_all_vcpus();
763     }
764
765     qapi_event_send_resume(&error_abort);
766 }
767
768
769 /***********************************************************/
770 /* real time host monotonic timer */
771
772 /***********************************************************/
773 /* host time/date access */
774 void qemu_get_timedate(struct tm *tm, int offset)
775 {
776     time_t ti;
777
778     time(&ti);
779     ti += offset;
780     if (rtc_date_offset == -1) {
781         if (rtc_utc)
782             gmtime_r(&ti, tm);
783         else
784             localtime_r(&ti, tm);
785     } else {
786         ti -= rtc_date_offset;
787         gmtime_r(&ti, tm);
788     }
789 }
790
791 int qemu_timedate_diff(struct tm *tm)
792 {
793     time_t seconds;
794
795     if (rtc_date_offset == -1)
796         if (rtc_utc)
797             seconds = mktimegm(tm);
798         else {
799             struct tm tmp = *tm;
800             tmp.tm_isdst = -1; /* use timezone to figure it out */
801             seconds = mktime(&tmp);
802         }
803     else
804         seconds = mktimegm(tm) + rtc_date_offset;
805
806     return seconds - time(NULL);
807 }
808
809 static void configure_rtc_date_offset(const char *startdate, int legacy)
810 {
811     time_t rtc_start_date;
812     struct tm tm;
813
814     if (!strcmp(startdate, "now") && legacy) {
815         rtc_date_offset = -1;
816     } else {
817         if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
818                    &tm.tm_year,
819                    &tm.tm_mon,
820                    &tm.tm_mday,
821                    &tm.tm_hour,
822                    &tm.tm_min,
823                    &tm.tm_sec) == 6) {
824             /* OK */
825         } else if (sscanf(startdate, "%d-%d-%d",
826                           &tm.tm_year,
827                           &tm.tm_mon,
828                           &tm.tm_mday) == 3) {
829             tm.tm_hour = 0;
830             tm.tm_min = 0;
831             tm.tm_sec = 0;
832         } else {
833             goto date_fail;
834         }
835         tm.tm_year -= 1900;
836         tm.tm_mon--;
837         rtc_start_date = mktimegm(&tm);
838         if (rtc_start_date == -1) {
839         date_fail:
840             fprintf(stderr, "Invalid date format. Valid formats are:\n"
841                             "'2006-06-17T16:01:21' or '2006-06-17'\n");
842             exit(1);
843         }
844         rtc_date_offset = time(NULL) - rtc_start_date;
845     }
846 }
847
848 static void configure_rtc(QemuOpts *opts)
849 {
850     const char *value;
851
852     value = qemu_opt_get(opts, "base");
853     if (value) {
854         if (!strcmp(value, "utc")) {
855             rtc_utc = 1;
856         } else if (!strcmp(value, "localtime")) {
857             rtc_utc = 0;
858         } else {
859             configure_rtc_date_offset(value, 0);
860         }
861     }
862     value = qemu_opt_get(opts, "clock");
863     if (value) {
864         if (!strcmp(value, "host")) {
865             rtc_clock = QEMU_CLOCK_HOST;
866         } else if (!strcmp(value, "rt")) {
867             rtc_clock = QEMU_CLOCK_REALTIME;
868         } else if (!strcmp(value, "vm")) {
869             rtc_clock = QEMU_CLOCK_VIRTUAL;
870         } else {
871             fprintf(stderr, "qemu: invalid option value '%s'\n", value);
872             exit(1);
873         }
874     }
875     value = qemu_opt_get(opts, "driftfix");
876     if (value) {
877         if (!strcmp(value, "slew")) {
878             static GlobalProperty slew_lost_ticks[] = {
879                 {
880                     .driver   = "mc146818rtc",
881                     .property = "lost_tick_policy",
882                     .value    = "slew",
883                 },
884                 { /* end of list */ }
885             };
886
887             qdev_prop_register_global_list(slew_lost_ticks);
888         } else if (!strcmp(value, "none")) {
889             /* discard is default */
890         } else {
891             fprintf(stderr, "qemu: invalid option value '%s'\n", value);
892             exit(1);
893         }
894     }
895 }
896
897 /***********************************************************/
898 /* Bluetooth support */
899 static int nb_hcis;
900 static int cur_hci;
901 static struct HCIInfo *hci_table[MAX_NICS];
902
903 struct HCIInfo *qemu_next_hci(void)
904 {
905     if (cur_hci == nb_hcis)
906         return &null_hci;
907
908     return hci_table[cur_hci++];
909 }
910
911 static int bt_hci_parse(const char *str)
912 {
913     struct HCIInfo *hci;
914     bdaddr_t bdaddr;
915
916     if (nb_hcis >= MAX_NICS) {
917         fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
918         return -1;
919     }
920
921     hci = hci_init(str);
922     if (!hci)
923         return -1;
924
925     bdaddr.b[0] = 0x52;
926     bdaddr.b[1] = 0x54;
927     bdaddr.b[2] = 0x00;
928     bdaddr.b[3] = 0x12;
929     bdaddr.b[4] = 0x34;
930     bdaddr.b[5] = 0x56 + nb_hcis;
931     hci->bdaddr_set(hci, bdaddr.b);
932
933     hci_table[nb_hcis++] = hci;
934
935     return 0;
936 }
937
938 static void bt_vhci_add(int vlan_id)
939 {
940     struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
941
942     if (!vlan->slave)
943         fprintf(stderr, "qemu: warning: adding a VHCI to "
944                         "an empty scatternet %i\n", vlan_id);
945
946     bt_vhci_init(bt_new_hci(vlan));
947 }
948
949 static struct bt_device_s *bt_device_add(const char *opt)
950 {
951     struct bt_scatternet_s *vlan;
952     int vlan_id = 0;
953     char *endp = strstr(opt, ",vlan=");
954     int len = (endp ? endp - opt : strlen(opt)) + 1;
955     char devname[10];
956
957     pstrcpy(devname, MIN(sizeof(devname), len), opt);
958
959     if (endp) {
960         vlan_id = strtol(endp + 6, &endp, 0);
961         if (*endp) {
962             fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
963             return 0;
964         }
965     }
966
967     vlan = qemu_find_bt_vlan(vlan_id);
968
969     if (!vlan->slave)
970         fprintf(stderr, "qemu: warning: adding a slave device to "
971                         "an empty scatternet %i\n", vlan_id);
972
973     if (!strcmp(devname, "keyboard"))
974         return bt_keyboard_init(vlan);
975
976     fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
977     return 0;
978 }
979
980 static int bt_parse(const char *opt)
981 {
982     const char *endp, *p;
983     int vlan;
984
985     if (strstart(opt, "hci", &endp)) {
986         if (!*endp || *endp == ',') {
987             if (*endp)
988                 if (!strstart(endp, ",vlan=", 0))
989                     opt = endp + 1;
990
991             return bt_hci_parse(opt);
992        }
993     } else if (strstart(opt, "vhci", &endp)) {
994         if (!*endp || *endp == ',') {
995             if (*endp) {
996                 if (strstart(endp, ",vlan=", &p)) {
997                     vlan = strtol(p, (char **) &endp, 0);
998                     if (*endp) {
999                         fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1000                         return 1;
1001                     }
1002                 } else {
1003                     fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1004                     return 1;
1005                 }
1006             } else
1007                 vlan = 0;
1008
1009             bt_vhci_add(vlan);
1010             return 0;
1011         }
1012     } else if (strstart(opt, "device:", &endp))
1013         return !bt_device_add(endp);
1014
1015     fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1016     return 1;
1017 }
1018
1019 static int parse_sandbox(QemuOpts *opts, void *opaque)
1020 {
1021     /* FIXME: change this to true for 1.3 */
1022     if (qemu_opt_get_bool(opts, "enable", false)) {
1023 #ifdef CONFIG_SECCOMP
1024         if (seccomp_start() < 0) {
1025             qerror_report(ERROR_CLASS_GENERIC_ERROR,
1026                           "failed to install seccomp syscall filter in the kernel");
1027             return -1;
1028         }
1029 #else
1030         qerror_report(ERROR_CLASS_GENERIC_ERROR,
1031                       "sandboxing request but seccomp is not compiled into this build");
1032         return -1;
1033 #endif
1034     }
1035
1036     return 0;
1037 }
1038
1039 static int parse_name(QemuOpts *opts, void *opaque)
1040 {
1041     const char *proc_name;
1042
1043     if (qemu_opt_get(opts, "debug-threads")) {
1044         qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1045     }
1046     qemu_name = qemu_opt_get(opts, "guest");
1047
1048     proc_name = qemu_opt_get(opts, "process");
1049     if (proc_name) {
1050         os_set_proc_name(proc_name);
1051     }
1052
1053     return 0;
1054 }
1055
1056 bool usb_enabled(bool default_usb)
1057 {
1058     return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1059                              has_defaults && default_usb);
1060 }
1061
1062 #ifndef _WIN32
1063 static int parse_add_fd(QemuOpts *opts, void *opaque)
1064 {
1065     int fd, dupfd, flags;
1066     int64_t fdset_id;
1067     const char *fd_opaque = NULL;
1068
1069     fd = qemu_opt_get_number(opts, "fd", -1);
1070     fdset_id = qemu_opt_get_number(opts, "set", -1);
1071     fd_opaque = qemu_opt_get(opts, "opaque");
1072
1073     if (fd < 0) {
1074         qerror_report(ERROR_CLASS_GENERIC_ERROR,
1075                       "fd option is required and must be non-negative");
1076         return -1;
1077     }
1078
1079     if (fd <= STDERR_FILENO) {
1080         qerror_report(ERROR_CLASS_GENERIC_ERROR,
1081                       "fd cannot be a standard I/O stream");
1082         return -1;
1083     }
1084
1085     /*
1086      * All fds inherited across exec() necessarily have FD_CLOEXEC
1087      * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1088      */
1089     flags = fcntl(fd, F_GETFD);
1090     if (flags == -1 || (flags & FD_CLOEXEC)) {
1091         qerror_report(ERROR_CLASS_GENERIC_ERROR,
1092                       "fd is not valid or already in use");
1093         return -1;
1094     }
1095
1096     if (fdset_id < 0) {
1097         qerror_report(ERROR_CLASS_GENERIC_ERROR,
1098                       "set option is required and must be non-negative");
1099         return -1;
1100     }
1101
1102 #ifdef F_DUPFD_CLOEXEC
1103     dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1104 #else
1105     dupfd = dup(fd);
1106     if (dupfd != -1) {
1107         qemu_set_cloexec(dupfd);
1108     }
1109 #endif
1110     if (dupfd == -1) {
1111         qerror_report(ERROR_CLASS_GENERIC_ERROR,
1112                       "Error duplicating fd: %s", strerror(errno));
1113         return -1;
1114     }
1115
1116     /* add the duplicate fd, and optionally the opaque string, to the fd set */
1117     monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1118                          fd_opaque, NULL);
1119
1120     return 0;
1121 }
1122
1123 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1124 {
1125     int fd;
1126
1127     fd = qemu_opt_get_number(opts, "fd", -1);
1128     close(fd);
1129
1130     return 0;
1131 }
1132 #endif
1133
1134 /***********************************************************/
1135 /* QEMU Block devices */
1136
1137 #define HD_OPTS "media=disk"
1138 #define CDROM_OPTS "media=cdrom"
1139 #define FD_OPTS ""
1140 #define PFLASH_OPTS ""
1141 #define MTD_OPTS ""
1142 #define SD_OPTS ""
1143
1144 static int drive_init_func(QemuOpts *opts, void *opaque)
1145 {
1146     BlockInterfaceType *block_default_type = opaque;
1147
1148     return drive_new(opts, *block_default_type) == NULL;
1149 }
1150
1151 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1152 {
1153     if (qemu_opt_get(opts, "snapshot") == NULL) {
1154         qemu_opt_set(opts, "snapshot", "on");
1155     }
1156     return 0;
1157 }
1158
1159 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1160                           int index, const char *optstr)
1161 {
1162     QemuOpts *opts;
1163     DriveInfo *dinfo;
1164
1165     if (!enable || drive_get_by_index(type, index)) {
1166         return;
1167     }
1168
1169     opts = drive_add(type, index, NULL, optstr);
1170     if (snapshot) {
1171         drive_enable_snapshot(opts, NULL);
1172     }
1173
1174     dinfo = drive_new(opts, type);
1175     if (!dinfo) {
1176         exit(1);
1177     }
1178     dinfo->is_default = true;
1179
1180 }
1181
1182 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1183 {
1184     boot_set_handler = func;
1185     boot_set_opaque = opaque;
1186 }
1187
1188 int qemu_boot_set(const char *boot_order)
1189 {
1190     if (!boot_set_handler) {
1191         return -EINVAL;
1192     }
1193     return boot_set_handler(boot_set_opaque, boot_order);
1194 }
1195
1196 static void validate_bootdevices(const char *devices)
1197 {
1198     /* We just do some generic consistency checks */
1199     const char *p;
1200     int bitmap = 0;
1201
1202     for (p = devices; *p != '\0'; p++) {
1203         /* Allowed boot devices are:
1204          * a-b: floppy disk drives
1205          * c-f: IDE disk drives
1206          * g-m: machine implementation dependent drives
1207          * n-p: network devices
1208          * It's up to each machine implementation to check if the given boot
1209          * devices match the actual hardware implementation and firmware
1210          * features.
1211          */
1212         if (*p < 'a' || *p > 'p') {
1213             fprintf(stderr, "Invalid boot device '%c'\n", *p);
1214             exit(1);
1215         }
1216         if (bitmap & (1 << (*p - 'a'))) {
1217             fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1218             exit(1);
1219         }
1220         bitmap |= 1 << (*p - 'a');
1221     }
1222 }
1223
1224 static void restore_boot_order(void *opaque)
1225 {
1226     char *normal_boot_order = opaque;
1227     static int first = 1;
1228
1229     /* Restore boot order and remove ourselves after the first boot */
1230     if (first) {
1231         first = 0;
1232         return;
1233     }
1234
1235     qemu_boot_set(normal_boot_order);
1236
1237     qemu_unregister_reset(restore_boot_order, normal_boot_order);
1238     g_free(normal_boot_order);
1239 }
1240
1241 static QemuOptsList qemu_smp_opts = {
1242     .name = "smp-opts",
1243     .implied_opt_name = "cpus",
1244     .merge_lists = true,
1245     .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1246     .desc = {
1247         {
1248             .name = "cpus",
1249             .type = QEMU_OPT_NUMBER,
1250         }, {
1251             .name = "sockets",
1252             .type = QEMU_OPT_NUMBER,
1253         }, {
1254             .name = "cores",
1255             .type = QEMU_OPT_NUMBER,
1256         }, {
1257             .name = "threads",
1258             .type = QEMU_OPT_NUMBER,
1259         }, {
1260             .name = "maxcpus",
1261             .type = QEMU_OPT_NUMBER,
1262         },
1263         { /*End of list */ }
1264     },
1265 };
1266
1267 static void smp_parse(QemuOpts *opts)
1268 {
1269     if (opts) {
1270
1271         unsigned cpus    = qemu_opt_get_number(opts, "cpus", 0);
1272         unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1273         unsigned cores   = qemu_opt_get_number(opts, "cores", 0);
1274         unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1275
1276         /* compute missing values, prefer sockets over cores over threads */
1277         if (cpus == 0 || sockets == 0) {
1278             sockets = sockets > 0 ? sockets : 1;
1279             cores = cores > 0 ? cores : 1;
1280             threads = threads > 0 ? threads : 1;
1281             if (cpus == 0) {
1282                 cpus = cores * threads * sockets;
1283             }
1284         } else {
1285             if (cores == 0) {
1286                 threads = threads > 0 ? threads : 1;
1287                 cores = cpus / (sockets * threads);
1288             } else {
1289                 threads = cpus / (cores * sockets);
1290             }
1291         }
1292
1293         max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1294
1295         smp_cpus = cpus;
1296         smp_cores = cores > 0 ? cores : 1;
1297         smp_threads = threads > 0 ? threads : 1;
1298
1299     }
1300
1301     if (max_cpus == 0) {
1302         max_cpus = smp_cpus;
1303     }
1304
1305     if (max_cpus > MAX_CPUMASK_BITS) {
1306         fprintf(stderr, "Unsupported number of maxcpus\n");
1307         exit(1);
1308     }
1309     if (max_cpus < smp_cpus) {
1310         fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1311         exit(1);
1312     }
1313
1314 }
1315
1316 static void realtime_init(void)
1317 {
1318     if (enable_mlock) {
1319         if (os_mlock() < 0) {
1320             fprintf(stderr, "qemu: locking memory failed\n");
1321             exit(1);
1322         }
1323     }
1324 }
1325
1326
1327 static void configure_msg(QemuOpts *opts)
1328 {
1329     enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1330 }
1331
1332 /***********************************************************/
1333 /* USB devices */
1334
1335 static int usb_device_add(const char *devname)
1336 {
1337     USBDevice *dev = NULL;
1338 #ifndef CONFIG_LINUX
1339     const char *p;
1340 #endif
1341
1342     if (!usb_enabled(false)) {
1343         return -1;
1344     }
1345
1346     /* drivers with .usbdevice_name entry in USBDeviceInfo */
1347     dev = usbdevice_create(devname);
1348     if (dev)
1349         goto done;
1350
1351     /* the other ones */
1352 #ifndef CONFIG_LINUX
1353     /* only the linux version is qdev-ified, usb-bsd still needs this */
1354     if (strstart(devname, "host:", &p)) {
1355         dev = usb_host_device_open(usb_bus_find(-1), p);
1356     }
1357 #endif
1358     if (!dev)
1359         return -1;
1360
1361 done:
1362     return 0;
1363 }
1364
1365 static int usb_device_del(const char *devname)
1366 {
1367     int bus_num, addr;
1368     const char *p;
1369
1370     if (strstart(devname, "host:", &p)) {
1371         return -1;
1372     }
1373
1374     if (!usb_enabled(false)) {
1375         return -1;
1376     }
1377
1378     p = strchr(devname, '.');
1379     if (!p)
1380         return -1;
1381     bus_num = strtoul(devname, NULL, 0);
1382     addr = strtoul(p + 1, NULL, 0);
1383
1384     return usb_device_delete_addr(bus_num, addr);
1385 }
1386
1387 static int usb_parse(const char *cmdline)
1388 {
1389     int r;
1390     r = usb_device_add(cmdline);
1391     if (r < 0) {
1392         fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1393     }
1394     return r;
1395 }
1396
1397 void do_usb_add(Monitor *mon, const QDict *qdict)
1398 {
1399     const char *devname = qdict_get_str(qdict, "devname");
1400     if (usb_device_add(devname) < 0) {
1401         error_report("could not add USB device '%s'", devname);
1402     }
1403 }
1404
1405 void do_usb_del(Monitor *mon, const QDict *qdict)
1406 {
1407     const char *devname = qdict_get_str(qdict, "devname");
1408     if (usb_device_del(devname) < 0) {
1409         error_report("could not delete USB device '%s'", devname);
1410     }
1411 }
1412
1413 /***********************************************************/
1414 /* machine registration */
1415
1416 MachineState *current_machine;
1417
1418 static void machine_class_init(ObjectClass *oc, void *data)
1419 {
1420     MachineClass *mc = MACHINE_CLASS(oc);
1421     QEMUMachine *qm = data;
1422
1423     mc->family = qm->family;
1424     mc->name = qm->name;
1425     mc->alias = qm->alias;
1426     mc->desc = qm->desc;
1427     mc->init = qm->init;
1428     mc->reset = qm->reset;
1429     mc->hot_add_cpu = qm->hot_add_cpu;
1430     mc->kvm_type = qm->kvm_type;
1431     mc->block_default_type = qm->block_default_type;
1432     mc->units_per_default_bus = qm->units_per_default_bus;
1433     mc->max_cpus = qm->max_cpus;
1434     mc->no_serial = qm->no_serial;
1435     mc->no_parallel = qm->no_parallel;
1436     mc->use_virtcon = qm->use_virtcon;
1437     mc->use_sclp = qm->use_sclp;
1438     mc->no_floppy = qm->no_floppy;
1439     mc->no_cdrom = qm->no_cdrom;
1440     mc->no_sdcard = qm->no_sdcard;
1441     mc->is_default = qm->is_default;
1442     mc->default_machine_opts = qm->default_machine_opts;
1443     mc->default_boot_order = qm->default_boot_order;
1444     mc->compat_props = qm->compat_props;
1445     mc->hw_version = qm->hw_version;
1446 }
1447
1448 int qemu_register_machine(QEMUMachine *m)
1449 {
1450     char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1451     TypeInfo ti = {
1452         .name       = name,
1453         .parent     = TYPE_MACHINE,
1454         .class_init = machine_class_init,
1455         .class_data = (void *)m,
1456     };
1457
1458     type_register(&ti);
1459     g_free(name);
1460
1461     return 0;
1462 }
1463
1464 static MachineClass *find_machine(const char *name)
1465 {
1466     GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1467     MachineClass *mc = NULL;
1468
1469     for (el = machines; el; el = el->next) {
1470         MachineClass *temp = el->data;
1471
1472         if (!strcmp(temp->name, name)) {
1473             mc = temp;
1474             break;
1475         }
1476         if (temp->alias &&
1477             !strcmp(temp->alias, name)) {
1478             mc = temp;
1479             break;
1480         }
1481     }
1482
1483     g_slist_free(machines);
1484     return mc;
1485 }
1486
1487 MachineClass *find_default_machine(void)
1488 {
1489     GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1490     MachineClass *mc = NULL;
1491
1492     for (el = machines; el; el = el->next) {
1493         MachineClass *temp = el->data;
1494
1495         if (temp->is_default) {
1496             mc = temp;
1497             break;
1498         }
1499     }
1500
1501     g_slist_free(machines);
1502     return mc;
1503 }
1504
1505 MachineInfoList *qmp_query_machines(Error **errp)
1506 {
1507     GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1508     MachineInfoList *mach_list = NULL;
1509
1510     for (el = machines; el; el = el->next) {
1511         MachineClass *mc = el->data;
1512         MachineInfoList *entry;
1513         MachineInfo *info;
1514
1515         info = g_malloc0(sizeof(*info));
1516         if (mc->is_default) {
1517             info->has_is_default = true;
1518             info->is_default = true;
1519         }
1520
1521         if (mc->alias) {
1522             info->has_alias = true;
1523             info->alias = g_strdup(mc->alias);
1524         }
1525
1526         info->name = g_strdup(mc->name);
1527         info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1528
1529         entry = g_malloc0(sizeof(*entry));
1530         entry->value = info;
1531         entry->next = mach_list;
1532         mach_list = entry;
1533     }
1534
1535     g_slist_free(machines);
1536     return mach_list;
1537 }
1538
1539 /***********************************************************/
1540 /* main execution loop */
1541
1542 struct vm_change_state_entry {
1543     VMChangeStateHandler *cb;
1544     void *opaque;
1545     QLIST_ENTRY (vm_change_state_entry) entries;
1546 };
1547
1548 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1549
1550 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1551                                                      void *opaque)
1552 {
1553     VMChangeStateEntry *e;
1554
1555     e = g_malloc0(sizeof (*e));
1556
1557     e->cb = cb;
1558     e->opaque = opaque;
1559     QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1560     return e;
1561 }
1562
1563 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1564 {
1565     QLIST_REMOVE (e, entries);
1566     g_free (e);
1567 }
1568
1569 void vm_state_notify(int running, RunState state)
1570 {
1571     VMChangeStateEntry *e, *next;
1572
1573     trace_vm_state_notify(running, state);
1574
1575     QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1576         e->cb(e->opaque, running, state);
1577     }
1578 }
1579
1580 /* reset/shutdown handler */
1581
1582 typedef struct QEMUResetEntry {
1583     QTAILQ_ENTRY(QEMUResetEntry) entry;
1584     QEMUResetHandler *func;
1585     void *opaque;
1586 } QEMUResetEntry;
1587
1588 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1589     QTAILQ_HEAD_INITIALIZER(reset_handlers);
1590 static int reset_requested;
1591 static int shutdown_requested, shutdown_signal = -1;
1592 static pid_t shutdown_pid;
1593 static int powerdown_requested;
1594 static int debug_requested;
1595 static int suspend_requested;
1596 static WakeupReason wakeup_reason;
1597 static NotifierList powerdown_notifiers =
1598     NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1599 static NotifierList suspend_notifiers =
1600     NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1601 static NotifierList wakeup_notifiers =
1602     NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1603 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1604
1605 int qemu_shutdown_requested_get(void)
1606 {
1607     return shutdown_requested;
1608 }
1609
1610 int qemu_reset_requested_get(void)
1611 {
1612     return reset_requested;
1613 }
1614
1615 static int qemu_shutdown_requested(void)
1616 {
1617     return atomic_xchg(&shutdown_requested, 0);
1618 }
1619
1620 static void qemu_kill_report(void)
1621 {
1622     if (!qtest_driver() && shutdown_signal != -1) {
1623         fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1624         if (shutdown_pid == 0) {
1625             /* This happens for eg ^C at the terminal, so it's worth
1626              * avoiding printing an odd message in that case.
1627              */
1628             fputc('\n', stderr);
1629         } else {
1630             fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1631         }
1632         shutdown_signal = -1;
1633     }
1634 }
1635
1636 static int qemu_reset_requested(void)
1637 {
1638     int r = reset_requested;
1639     reset_requested = 0;
1640     return r;
1641 }
1642
1643 static int qemu_suspend_requested(void)
1644 {
1645     int r = suspend_requested;
1646     suspend_requested = 0;
1647     return r;
1648 }
1649
1650 static WakeupReason qemu_wakeup_requested(void)
1651 {
1652     return wakeup_reason;
1653 }
1654
1655 static int qemu_powerdown_requested(void)
1656 {
1657     int r = powerdown_requested;
1658     powerdown_requested = 0;
1659     return r;
1660 }
1661
1662 static int qemu_debug_requested(void)
1663 {
1664     int r = debug_requested;
1665     debug_requested = 0;
1666     return r;
1667 }
1668
1669 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1670 {
1671     QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1672
1673     re->func = func;
1674     re->opaque = opaque;
1675     QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1676 }
1677
1678 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1679 {
1680     QEMUResetEntry *re;
1681
1682     QTAILQ_FOREACH(re, &reset_handlers, entry) {
1683         if (re->func == func && re->opaque == opaque) {
1684             QTAILQ_REMOVE(&reset_handlers, re, entry);
1685             g_free(re);
1686             return;
1687         }
1688     }
1689 }
1690
1691 void qemu_devices_reset(void)
1692 {
1693     QEMUResetEntry *re, *nre;
1694
1695     /* reset all devices */
1696     QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1697         re->func(re->opaque);
1698     }
1699 }
1700
1701 void qemu_system_reset(bool report)
1702 {
1703     MachineClass *mc;
1704
1705     mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1706
1707     if (mc && mc->reset) {
1708         mc->reset();
1709     } else {
1710         qemu_devices_reset();
1711     }
1712     if (report) {
1713         qapi_event_send_reset(&error_abort);
1714     }
1715     cpu_synchronize_all_post_reset();
1716 }
1717
1718 void qemu_system_reset_request(void)
1719 {
1720     if (no_reboot) {
1721         shutdown_requested = 1;
1722     } else {
1723         reset_requested = 1;
1724     }
1725     cpu_stop_current();
1726     qemu_notify_event();
1727 }
1728
1729 static void qemu_system_suspend(void)
1730 {
1731     pause_all_vcpus();
1732     notifier_list_notify(&suspend_notifiers, NULL);
1733     runstate_set(RUN_STATE_SUSPENDED);
1734     qapi_event_send_suspend(&error_abort);
1735 }
1736
1737 void qemu_system_suspend_request(void)
1738 {
1739     if (runstate_check(RUN_STATE_SUSPENDED)) {
1740         return;
1741     }
1742     suspend_requested = 1;
1743     cpu_stop_current();
1744     qemu_notify_event();
1745 }
1746
1747 void qemu_register_suspend_notifier(Notifier *notifier)
1748 {
1749     notifier_list_add(&suspend_notifiers, notifier);
1750 }
1751
1752 void qemu_system_wakeup_request(WakeupReason reason)
1753 {
1754     trace_system_wakeup_request(reason);
1755
1756     if (!runstate_check(RUN_STATE_SUSPENDED)) {
1757         return;
1758     }
1759     if (!(wakeup_reason_mask & (1 << reason))) {
1760         return;
1761     }
1762     runstate_set(RUN_STATE_RUNNING);
1763     wakeup_reason = reason;
1764     qemu_notify_event();
1765 }
1766
1767 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1768 {
1769     if (enabled) {
1770         wakeup_reason_mask |= (1 << reason);
1771     } else {
1772         wakeup_reason_mask &= ~(1 << reason);
1773     }
1774 }
1775
1776 void qemu_register_wakeup_notifier(Notifier *notifier)
1777 {
1778     notifier_list_add(&wakeup_notifiers, notifier);
1779 }
1780
1781 void qemu_system_killed(int signal, pid_t pid)
1782 {
1783     shutdown_signal = signal;
1784     shutdown_pid = pid;
1785     no_shutdown = 0;
1786     qemu_system_shutdown_request();
1787 }
1788
1789 void qemu_system_shutdown_request(void)
1790 {
1791     trace_qemu_system_shutdown_request();
1792     shutdown_requested = 1;
1793     qemu_notify_event();
1794 }
1795
1796 static void qemu_system_powerdown(void)
1797 {
1798     qapi_event_send_powerdown(&error_abort);
1799     notifier_list_notify(&powerdown_notifiers, NULL);
1800 }
1801
1802 void qemu_system_powerdown_request(void)
1803 {
1804     trace_qemu_system_powerdown_request();
1805     powerdown_requested = 1;
1806     qemu_notify_event();
1807 }
1808
1809 void qemu_register_powerdown_notifier(Notifier *notifier)
1810 {
1811     notifier_list_add(&powerdown_notifiers, notifier);
1812 }
1813
1814 void qemu_system_debug_request(void)
1815 {
1816     debug_requested = 1;
1817     qemu_notify_event();
1818 }
1819
1820 static bool main_loop_should_exit(void)
1821 {
1822     RunState r;
1823     if (qemu_debug_requested()) {
1824         vm_stop(RUN_STATE_DEBUG);
1825     }
1826     if (qemu_suspend_requested()) {
1827         qemu_system_suspend();
1828     }
1829     if (qemu_shutdown_requested()) {
1830         qemu_kill_report();
1831         qapi_event_send_shutdown(&error_abort);
1832         if (no_shutdown) {
1833             vm_stop(RUN_STATE_SHUTDOWN);
1834         } else {
1835             return true;
1836         }
1837     }
1838     if (qemu_reset_requested()) {
1839         pause_all_vcpus();
1840         cpu_synchronize_all_states();
1841         qemu_system_reset(VMRESET_REPORT);
1842         resume_all_vcpus();
1843         if (runstate_needs_reset()) {
1844             runstate_set(RUN_STATE_PAUSED);
1845         }
1846     }
1847     if (qemu_wakeup_requested()) {
1848         pause_all_vcpus();
1849         cpu_synchronize_all_states();
1850         qemu_system_reset(VMRESET_SILENT);
1851         notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1852         wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1853         resume_all_vcpus();
1854         qapi_event_send_wakeup(&error_abort);
1855     }
1856     if (qemu_powerdown_requested()) {
1857         qemu_system_powerdown();
1858     }
1859     if (qemu_vmstop_requested(&r)) {
1860         vm_stop(r);
1861     }
1862     return false;
1863 }
1864
1865 static void main_loop(void)
1866 {
1867     bool nonblocking;
1868     int last_io = 0;
1869 #ifdef CONFIG_PROFILER
1870     int64_t ti;
1871 #endif
1872     do {
1873         nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1874 #ifdef CONFIG_PROFILER
1875         ti = profile_getclock();
1876 #endif
1877         last_io = main_loop_wait(nonblocking);
1878 #ifdef CONFIG_PROFILER
1879         dev_time += profile_getclock() - ti;
1880 #endif
1881     } while (!main_loop_should_exit());
1882 }
1883
1884 static void version(void)
1885 {
1886     printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1887 }
1888
1889 static void help(int exitcode)
1890 {
1891     version();
1892     printf("usage: %s [options] [disk_image]\n\n"
1893            "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1894             error_get_progname());
1895
1896 #define QEMU_OPTIONS_GENERATE_HELP
1897 #include "qemu-options-wrapper.h"
1898
1899     printf("\nDuring emulation, the following keys are useful:\n"
1900            "ctrl-alt-f      toggle full screen\n"
1901            "ctrl-alt-n      switch to virtual console 'n'\n"
1902            "ctrl-alt        toggle mouse and keyboard grab\n"
1903            "\n"
1904            "When using -nographic, press 'ctrl-a h' to get some help.\n");
1905
1906     exit(exitcode);
1907 }
1908
1909 #define HAS_ARG 0x0001
1910
1911 typedef struct QEMUOption {
1912     const char *name;
1913     int flags;
1914     int index;
1915     uint32_t arch_mask;
1916 } QEMUOption;
1917
1918 static const QEMUOption qemu_options[] = {
1919     { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1920 #define QEMU_OPTIONS_GENERATE_OPTIONS
1921 #include "qemu-options-wrapper.h"
1922     { NULL },
1923 };
1924
1925 static bool vga_available(void)
1926 {
1927     return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1928 }
1929
1930 static bool cirrus_vga_available(void)
1931 {
1932     return object_class_by_name("cirrus-vga")
1933            || object_class_by_name("isa-cirrus-vga");
1934 }
1935
1936 static bool vmware_vga_available(void)
1937 {
1938     return object_class_by_name("vmware-svga");
1939 }
1940
1941 static bool qxl_vga_available(void)
1942 {
1943     return object_class_by_name("qxl-vga");
1944 }
1945
1946 static bool tcx_vga_available(void)
1947 {
1948     return object_class_by_name("SUNW,tcx");
1949 }
1950
1951 static bool cg3_vga_available(void)
1952 {
1953     return object_class_by_name("cgthree");
1954 }
1955
1956 static void select_vgahw (const char *p)
1957 {
1958     const char *opts;
1959
1960     assert(vga_interface_type == VGA_NONE);
1961     if (strstart(p, "std", &opts)) {
1962         if (vga_available()) {
1963             vga_interface_type = VGA_STD;
1964         } else {
1965             fprintf(stderr, "Error: standard VGA not available\n");
1966             exit(0);
1967         }
1968     } else if (strstart(p, "cirrus", &opts)) {
1969         if (cirrus_vga_available()) {
1970             vga_interface_type = VGA_CIRRUS;
1971         } else {
1972             fprintf(stderr, "Error: Cirrus VGA not available\n");
1973             exit(0);
1974         }
1975     } else if (strstart(p, "vmware", &opts)) {
1976         if (vmware_vga_available()) {
1977             vga_interface_type = VGA_VMWARE;
1978         } else {
1979             fprintf(stderr, "Error: VMWare SVGA not available\n");
1980             exit(0);
1981         }
1982     } else if (strstart(p, "xenfb", &opts)) {
1983         vga_interface_type = VGA_XENFB;
1984     } else if (strstart(p, "qxl", &opts)) {
1985         if (qxl_vga_available()) {
1986             vga_interface_type = VGA_QXL;
1987         } else {
1988             fprintf(stderr, "Error: QXL VGA not available\n");
1989             exit(0);
1990         }
1991     } else if (strstart(p, "tcx", &opts)) {
1992         if (tcx_vga_available()) {
1993             vga_interface_type = VGA_TCX;
1994         } else {
1995             fprintf(stderr, "Error: TCX framebuffer not available\n");
1996             exit(0);
1997         }
1998     } else if (strstart(p, "cg3", &opts)) {
1999         if (cg3_vga_available()) {
2000             vga_interface_type = VGA_CG3;
2001         } else {
2002             fprintf(stderr, "Error: CG3 framebuffer not available\n");
2003             exit(0);
2004         }
2005     } else if (!strstart(p, "none", &opts)) {
2006     invalid_vga:
2007         fprintf(stderr, "Unknown vga type: %s\n", p);
2008         exit(1);
2009     }
2010     while (*opts) {
2011         const char *nextopt;
2012
2013         if (strstart(opts, ",retrace=", &nextopt)) {
2014             opts = nextopt;
2015             if (strstart(opts, "dumb", &nextopt))
2016                 vga_retrace_method = VGA_RETRACE_DUMB;
2017             else if (strstart(opts, "precise", &nextopt))
2018                 vga_retrace_method = VGA_RETRACE_PRECISE;
2019             else goto invalid_vga;
2020         } else goto invalid_vga;
2021         opts = nextopt;
2022     }
2023 }
2024
2025 static DisplayType select_display(const char *p)
2026 {
2027     const char *opts;
2028     DisplayType display = DT_DEFAULT;
2029
2030     if (strstart(p, "sdl", &opts)) {
2031 #ifdef CONFIG_SDL
2032         display = DT_SDL;
2033         while (*opts) {
2034             const char *nextopt;
2035
2036             if (strstart(opts, ",frame=", &nextopt)) {
2037                 opts = nextopt;
2038                 if (strstart(opts, "on", &nextopt)) {
2039                     no_frame = 0;
2040                 } else if (strstart(opts, "off", &nextopt)) {
2041                     no_frame = 1;
2042                 } else {
2043                     goto invalid_sdl_args;
2044                 }
2045             } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2046                 opts = nextopt;
2047                 if (strstart(opts, "on", &nextopt)) {
2048                     alt_grab = 1;
2049                 } else if (strstart(opts, "off", &nextopt)) {
2050                     alt_grab = 0;
2051                 } else {
2052                     goto invalid_sdl_args;
2053                 }
2054             } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2055                 opts = nextopt;
2056                 if (strstart(opts, "on", &nextopt)) {
2057                     ctrl_grab = 1;
2058                 } else if (strstart(opts, "off", &nextopt)) {
2059                     ctrl_grab = 0;
2060                 } else {
2061                     goto invalid_sdl_args;
2062                 }
2063             } else if (strstart(opts, ",window_close=", &nextopt)) {
2064                 opts = nextopt;
2065                 if (strstart(opts, "on", &nextopt)) {
2066                     no_quit = 0;
2067                 } else if (strstart(opts, "off", &nextopt)) {
2068                     no_quit = 1;
2069                 } else {
2070                     goto invalid_sdl_args;
2071                 }
2072             } else {
2073             invalid_sdl_args:
2074                 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2075                 exit(1);
2076             }
2077             opts = nextopt;
2078         }
2079 #else
2080         fprintf(stderr, "SDL support is disabled\n");
2081         exit(1);
2082 #endif
2083     } else if (strstart(p, "vnc", &opts)) {
2084 #ifdef CONFIG_VNC
2085         display_remote++;
2086
2087         if (*opts) {
2088             const char *nextopt;
2089
2090             if (strstart(opts, "=", &nextopt)) {
2091                 vnc_display = nextopt;
2092             }
2093         }
2094         if (!vnc_display) {
2095             fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2096             exit(1);
2097         }
2098 #else
2099         fprintf(stderr, "VNC support is disabled\n");
2100         exit(1);
2101 #endif
2102     } else if (strstart(p, "curses", &opts)) {
2103 #ifdef CONFIG_CURSES
2104         display = DT_CURSES;
2105 #else
2106         fprintf(stderr, "Curses support is disabled\n");
2107         exit(1);
2108 #endif
2109     } else if (strstart(p, "gtk", &opts)) {
2110 #ifdef CONFIG_GTK
2111         display = DT_GTK;
2112         while (*opts) {
2113             const char *nextopt;
2114
2115             if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2116                 opts = nextopt;
2117                 if (strstart(opts, "on", &nextopt)) {
2118                     grab_on_hover = true;
2119                 } else if (strstart(opts, "off", &nextopt)) {
2120                     grab_on_hover = false;
2121                 } else {
2122                     goto invalid_gtk_args;
2123                 }
2124             } else {
2125             invalid_gtk_args:
2126                 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2127                 exit(1);
2128             }
2129             opts = nextopt;
2130         }
2131 #else
2132         fprintf(stderr, "GTK support is disabled\n");
2133         exit(1);
2134 #endif
2135     } else if (strstart(p, "none", &opts)) {
2136         display = DT_NONE;
2137     } else {
2138         fprintf(stderr, "Unknown display type: %s\n", p);
2139         exit(1);
2140     }
2141
2142     return display;
2143 }
2144
2145 static int balloon_parse(const char *arg)
2146 {
2147     QemuOpts *opts;
2148
2149     if (strcmp(arg, "none") == 0) {
2150         return 0;
2151     }
2152
2153     if (!strncmp(arg, "virtio", 6)) {
2154         if (arg[6] == ',') {
2155             /* have params -> parse them */
2156             opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2157             if (!opts)
2158                 return  -1;
2159         } else {
2160             /* create empty opts */
2161             opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2162                                     &error_abort);
2163         }
2164         qemu_opt_set(opts, "driver", "virtio-balloon");
2165         return 0;
2166     }
2167
2168     return -1;
2169 }
2170
2171 char *qemu_find_file(int type, const char *name)
2172 {
2173     int i;
2174     const char *subdir;
2175     char *buf;
2176
2177     /* Try the name as a straight path first */
2178     if (access(name, R_OK) == 0) {
2179         trace_load_file(name, name);
2180         return g_strdup(name);
2181     }
2182
2183     switch (type) {
2184     case QEMU_FILE_TYPE_BIOS:
2185         subdir = "";
2186         break;
2187     case QEMU_FILE_TYPE_KEYMAP:
2188         subdir = "keymaps/";
2189         break;
2190     default:
2191         abort();
2192     }
2193
2194     for (i = 0; i < data_dir_idx; i++) {
2195         buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2196         if (access(buf, R_OK) == 0) {
2197             trace_load_file(name, buf);
2198             return buf;
2199         }
2200         g_free(buf);
2201     }
2202     return NULL;
2203 }
2204
2205 static int device_help_func(QemuOpts *opts, void *opaque)
2206 {
2207     return qdev_device_help(opts);
2208 }
2209
2210 static int device_init_func(QemuOpts *opts, void *opaque)
2211 {
2212     DeviceState *dev;
2213
2214     dev = qdev_device_add(opts);
2215     if (!dev)
2216         return -1;
2217     object_unref(OBJECT(dev));
2218     return 0;
2219 }
2220
2221 static int chardev_init_func(QemuOpts *opts, void *opaque)
2222 {
2223     Error *local_err = NULL;
2224
2225     qemu_chr_new_from_opts(opts, NULL, &local_err);
2226     if (local_err) {
2227         error_report("%s", error_get_pretty(local_err));
2228         error_free(local_err);
2229         return -1;
2230     }
2231     return 0;
2232 }
2233
2234 #ifdef CONFIG_VIRTFS
2235 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2236 {
2237     int ret;
2238     ret = qemu_fsdev_add(opts);
2239
2240     return ret;
2241 }
2242 #endif
2243
2244 static int mon_init_func(QemuOpts *opts, void *opaque)
2245 {
2246     CharDriverState *chr;
2247     const char *chardev;
2248     const char *mode;
2249     int flags;
2250
2251     mode = qemu_opt_get(opts, "mode");
2252     if (mode == NULL) {
2253         mode = "readline";
2254     }
2255     if (strcmp(mode, "readline") == 0) {
2256         flags = MONITOR_USE_READLINE;
2257     } else if (strcmp(mode, "control") == 0) {
2258         flags = MONITOR_USE_CONTROL;
2259     } else {
2260         fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2261         exit(1);
2262     }
2263
2264     if (qemu_opt_get_bool(opts, "pretty", 0))
2265         flags |= MONITOR_USE_PRETTY;
2266
2267     if (qemu_opt_get_bool(opts, "default", 0))
2268         flags |= MONITOR_IS_DEFAULT;
2269
2270     chardev = qemu_opt_get(opts, "chardev");
2271     chr = qemu_chr_find(chardev);
2272     if (chr == NULL) {
2273         fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2274         exit(1);
2275     }
2276
2277     qemu_chr_fe_claim_no_fail(chr);
2278     monitor_init(chr, flags);
2279     return 0;
2280 }
2281
2282 static void monitor_parse(const char *optarg, const char *mode)
2283 {
2284     static int monitor_device_index = 0;
2285     QemuOpts *opts;
2286     const char *p;
2287     char label[32];
2288     int def = 0;
2289
2290     if (strstart(optarg, "chardev:", &p)) {
2291         snprintf(label, sizeof(label), "%s", p);
2292     } else {
2293         snprintf(label, sizeof(label), "compat_monitor%d",
2294                  monitor_device_index);
2295         if (monitor_device_index == 0) {
2296             def = 1;
2297         }
2298         opts = qemu_chr_parse_compat(label, optarg);
2299         if (!opts) {
2300             fprintf(stderr, "parse error: %s\n", optarg);
2301             exit(1);
2302         }
2303     }
2304
2305     opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2306     if (!opts) {
2307         fprintf(stderr, "duplicate chardev: %s\n", label);
2308         exit(1);
2309     }
2310     qemu_opt_set(opts, "mode", mode);
2311     qemu_opt_set(opts, "chardev", label);
2312     if (def)
2313         qemu_opt_set(opts, "default", "on");
2314     monitor_device_index++;
2315 }
2316
2317 struct device_config {
2318     enum {
2319         DEV_USB,       /* -usbdevice     */
2320         DEV_BT,        /* -bt            */
2321         DEV_SERIAL,    /* -serial        */
2322         DEV_PARALLEL,  /* -parallel      */
2323         DEV_VIRTCON,   /* -virtioconsole */
2324         DEV_DEBUGCON,  /* -debugcon */
2325         DEV_GDB,       /* -gdb, -s */
2326         DEV_SCLP,      /* s390 sclp */
2327     } type;
2328     const char *cmdline;
2329     Location loc;
2330     QTAILQ_ENTRY(device_config) next;
2331 };
2332
2333 static QTAILQ_HEAD(, device_config) device_configs =
2334     QTAILQ_HEAD_INITIALIZER(device_configs);
2335
2336 static void add_device_config(int type, const char *cmdline)
2337 {
2338     struct device_config *conf;
2339
2340     conf = g_malloc0(sizeof(*conf));
2341     conf->type = type;
2342     conf->cmdline = cmdline;
2343     loc_save(&conf->loc);
2344     QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2345 }
2346
2347 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2348 {
2349     struct device_config *conf;
2350     int rc;
2351
2352     QTAILQ_FOREACH(conf, &device_configs, next) {
2353         if (conf->type != type)
2354             continue;
2355         loc_push_restore(&conf->loc);
2356         rc = func(conf->cmdline);
2357         loc_pop(&conf->loc);
2358         if (rc) {
2359             return rc;
2360         }
2361     }
2362     return 0;
2363 }
2364
2365 static int serial_parse(const char *devname)
2366 {
2367     static int index = 0;
2368     char label[32];
2369
2370     if (strcmp(devname, "none") == 0)
2371         return 0;
2372     if (index == MAX_SERIAL_PORTS) {
2373         fprintf(stderr, "qemu: too many serial ports\n");
2374         exit(1);
2375     }
2376     snprintf(label, sizeof(label), "serial%d", index);
2377     serial_hds[index] = qemu_chr_new(label, devname, NULL);
2378     if (!serial_hds[index]) {
2379         fprintf(stderr, "qemu: could not connect serial device"
2380                 " to character backend '%s'\n", devname);
2381         return -1;
2382     }
2383     index++;
2384     return 0;
2385 }
2386
2387 static int parallel_parse(const char *devname)
2388 {
2389     static int index = 0;
2390     char label[32];
2391
2392     if (strcmp(devname, "none") == 0)
2393         return 0;
2394     if (index == MAX_PARALLEL_PORTS) {
2395         fprintf(stderr, "qemu: too many parallel ports\n");
2396         exit(1);
2397     }
2398     snprintf(label, sizeof(label), "parallel%d", index);
2399     parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2400     if (!parallel_hds[index]) {
2401         fprintf(stderr, "qemu: could not connect parallel device"
2402                 " to character backend '%s'\n", devname);
2403         return -1;
2404     }
2405     index++;
2406     return 0;
2407 }
2408
2409 static int virtcon_parse(const char *devname)
2410 {
2411     QemuOptsList *device = qemu_find_opts("device");
2412     static int index = 0;
2413     char label[32];
2414     QemuOpts *bus_opts, *dev_opts;
2415
2416     if (strcmp(devname, "none") == 0)
2417         return 0;
2418     if (index == MAX_VIRTIO_CONSOLES) {
2419         fprintf(stderr, "qemu: too many virtio consoles\n");
2420         exit(1);
2421     }
2422
2423     bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2424     if (arch_type == QEMU_ARCH_S390X) {
2425         qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2426     } else {
2427         qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2428     }
2429
2430     dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2431     qemu_opt_set(dev_opts, "driver", "virtconsole");
2432
2433     snprintf(label, sizeof(label), "virtcon%d", index);
2434     virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2435     if (!virtcon_hds[index]) {
2436         fprintf(stderr, "qemu: could not connect virtio console"
2437                 " to character backend '%s'\n", devname);
2438         return -1;
2439     }
2440     qemu_opt_set(dev_opts, "chardev", label);
2441
2442     index++;
2443     return 0;
2444 }
2445
2446 static int sclp_parse(const char *devname)
2447 {
2448     QemuOptsList *device = qemu_find_opts("device");
2449     static int index = 0;
2450     char label[32];
2451     QemuOpts *dev_opts;
2452
2453     if (strcmp(devname, "none") == 0) {
2454         return 0;
2455     }
2456     if (index == MAX_SCLP_CONSOLES) {
2457         fprintf(stderr, "qemu: too many sclp consoles\n");
2458         exit(1);
2459     }
2460
2461     assert(arch_type == QEMU_ARCH_S390X);
2462
2463     dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2464     qemu_opt_set(dev_opts, "driver", "sclpconsole");
2465
2466     snprintf(label, sizeof(label), "sclpcon%d", index);
2467     sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2468     if (!sclp_hds[index]) {
2469         fprintf(stderr, "qemu: could not connect sclp console"
2470                 " to character backend '%s'\n", devname);
2471         return -1;
2472     }
2473     qemu_opt_set(dev_opts, "chardev", label);
2474
2475     index++;
2476     return 0;
2477 }
2478
2479 static int debugcon_parse(const char *devname)
2480 {
2481     QemuOpts *opts;
2482
2483     if (!qemu_chr_new("debugcon", devname, NULL)) {
2484         exit(1);
2485     }
2486     opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2487     if (!opts) {
2488         fprintf(stderr, "qemu: already have a debugcon device\n");
2489         exit(1);
2490     }
2491     qemu_opt_set(opts, "driver", "isa-debugcon");
2492     qemu_opt_set(opts, "chardev", "debugcon");
2493     return 0;
2494 }
2495
2496 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2497 {
2498     const MachineClass *mc1 = a, *mc2 = b;
2499     int res;
2500
2501     if (mc1->family == NULL) {
2502         if (mc2->family == NULL) {
2503             /* Compare standalone machine types against each other; they sort
2504              * in increasing order.
2505              */
2506             return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2507                           object_class_get_name(OBJECT_CLASS(mc2)));
2508         }
2509
2510         /* Standalone machine types sort after families. */
2511         return 1;
2512     }
2513
2514     if (mc2->family == NULL) {
2515         /* Families sort before standalone machine types. */
2516         return -1;
2517     }
2518
2519     /* Families sort between each other alphabetically increasingly. */
2520     res = strcmp(mc1->family, mc2->family);
2521     if (res != 0) {
2522         return res;
2523     }
2524
2525     /* Within the same family, machine types sort in decreasing order. */
2526     return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2527                   object_class_get_name(OBJECT_CLASS(mc1)));
2528 }
2529
2530  static MachineClass *machine_parse(const char *name)
2531 {
2532     MachineClass *mc = NULL;
2533     GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2534
2535     if (name) {
2536         mc = find_machine(name);
2537     }
2538     if (mc) {
2539         return mc;
2540     }
2541     if (name && !is_help_option(name)) {
2542         error_report("Unsupported machine type");
2543         error_printf("Use -machine help to list supported machines!\n");
2544     } else {
2545         printf("Supported machines are:\n");
2546         machines = g_slist_sort(machines, machine_class_cmp);
2547         for (el = machines; el; el = el->next) {
2548             MachineClass *mc = el->data;
2549             if (mc->alias) {
2550                 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2551             }
2552             printf("%-20s %s%s\n", mc->name, mc->desc,
2553                    mc->is_default ? " (default)" : "");
2554         }
2555     }
2556
2557     g_slist_free(machines);
2558     exit(!name || !is_help_option(name));
2559 }
2560
2561 void qemu_add_exit_notifier(Notifier *notify)
2562 {
2563     notifier_list_add(&exit_notifiers, notify);
2564 }
2565
2566 void qemu_remove_exit_notifier(Notifier *notify)
2567 {
2568     notifier_remove(notify);
2569 }
2570
2571 static void qemu_run_exit_notifiers(void)
2572 {
2573     notifier_list_notify(&exit_notifiers, NULL);
2574 }
2575
2576 void qemu_add_machine_init_done_notifier(Notifier *notify)
2577 {
2578     notifier_list_add(&machine_init_done_notifiers, notify);
2579 }
2580
2581 static void qemu_run_machine_init_done_notifiers(void)
2582 {
2583     notifier_list_notify(&machine_init_done_notifiers, NULL);
2584 }
2585
2586 static const QEMUOption *lookup_opt(int argc, char **argv,
2587                                     const char **poptarg, int *poptind)
2588 {
2589     const QEMUOption *popt;
2590     int optind = *poptind;
2591     char *r = argv[optind];
2592     const char *optarg;
2593
2594     loc_set_cmdline(argv, optind, 1);
2595     optind++;
2596     /* Treat --foo the same as -foo.  */
2597     if (r[1] == '-')
2598         r++;
2599     popt = qemu_options;
2600     for(;;) {
2601         if (!popt->name) {
2602             error_report("invalid option");
2603             exit(1);
2604         }
2605         if (!strcmp(popt->name, r + 1))
2606             break;
2607         popt++;
2608     }
2609     if (popt->flags & HAS_ARG) {
2610         if (optind >= argc) {
2611             error_report("requires an argument");
2612             exit(1);
2613         }
2614         optarg = argv[optind++];
2615         loc_set_cmdline(argv, optind - 2, 2);
2616     } else {
2617         optarg = NULL;
2618     }
2619
2620     *poptarg = optarg;
2621     *poptind = optind;
2622
2623     return popt;
2624 }
2625
2626 static gpointer malloc_and_trace(gsize n_bytes)
2627 {
2628     void *ptr = malloc(n_bytes);
2629     trace_g_malloc(n_bytes, ptr);
2630     return ptr;
2631 }
2632
2633 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2634 {
2635     void *ptr = realloc(mem, n_bytes);
2636     trace_g_realloc(mem, n_bytes, ptr);
2637     return ptr;
2638 }
2639
2640 static void free_and_trace(gpointer mem)
2641 {
2642     trace_g_free(mem);
2643     free(mem);
2644 }
2645
2646 static int machine_set_property(const char *name, const char *value,
2647                                 void *opaque)
2648 {
2649     Object *obj = OBJECT(opaque);
2650     StringInputVisitor *siv;
2651     Error *local_err = NULL;
2652     char *c, *qom_name;
2653
2654     if (strcmp(name, "type") == 0) {
2655         return 0;
2656     }
2657
2658     qom_name = g_strdup(name);
2659     c = qom_name;
2660     while (*c++) {
2661         if (*c == '_') {
2662             *c = '-';
2663         }
2664     }
2665
2666     siv = string_input_visitor_new(value);
2667     object_property_set(obj, string_input_get_visitor(siv), qom_name, &local_err);
2668     string_input_visitor_cleanup(siv);
2669     g_free(qom_name);
2670
2671     if (local_err) {
2672         qerror_report_err(local_err);
2673         error_free(local_err);
2674         return -1;
2675     }
2676
2677     return 0;
2678 }
2679
2680 static int object_create(QemuOpts *opts, void *opaque)
2681 {
2682     Error *err = NULL;
2683     char *type = NULL;
2684     char *id = NULL;
2685     void *dummy = NULL;
2686     OptsVisitor *ov;
2687     QDict *pdict;
2688
2689     ov = opts_visitor_new(opts);
2690     pdict = qemu_opts_to_qdict(opts, NULL);
2691
2692     visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2693     if (err) {
2694         goto out;
2695     }
2696
2697     qdict_del(pdict, "qom-type");
2698     visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2699     if (err) {
2700         goto out;
2701     }
2702
2703     qdict_del(pdict, "id");
2704     visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2705     if (err) {
2706         goto out;
2707     }
2708
2709     object_add(type, id, pdict, opts_get_visitor(ov), &err);
2710     if (err) {
2711         goto out;
2712     }
2713     visit_end_struct(opts_get_visitor(ov), &err);
2714     if (err) {
2715         qmp_object_del(id, NULL);
2716     }
2717
2718 out:
2719     opts_visitor_cleanup(ov);
2720
2721     QDECREF(pdict);
2722     g_free(id);
2723     g_free(type);
2724     g_free(dummy);
2725     if (err) {
2726         qerror_report_err(err);
2727         error_free(err);
2728         return -1;
2729     }
2730     return 0;
2731 }
2732
2733 int main(int argc, char **argv, char **envp)
2734 {
2735     int i;
2736     int snapshot, linux_boot;
2737     const char *initrd_filename;
2738     const char *kernel_filename, *kernel_cmdline;
2739     const char *boot_order;
2740     DisplayState *ds;
2741     int cyls, heads, secs, translation;
2742     QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2743     QemuOptsList *olist;
2744     int optind;
2745     const char *optarg;
2746     const char *loadvm = NULL;
2747     MachineClass *machine_class;
2748     const char *cpu_model;
2749     const char *vga_model = NULL;
2750     const char *qtest_chrdev = NULL;
2751     const char *qtest_log = NULL;
2752     const char *pid_file = NULL;
2753     const char *incoming = NULL;
2754 #ifdef CONFIG_VNC
2755     int show_vnc_port = 0;
2756 #endif
2757     bool defconfig = true;
2758     bool userconfig = true;
2759     const char *log_mask = NULL;
2760     const char *log_file = NULL;
2761     GMemVTable mem_trace = {
2762         .malloc = malloc_and_trace,
2763         .realloc = realloc_and_trace,
2764         .free = free_and_trace,
2765     };
2766     const char *trace_events = NULL;
2767     const char *trace_file = NULL;
2768     const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2769                                         1024 * 1024;
2770     ram_addr_t maxram_size = default_ram_size;
2771     uint64_t ram_slots = 0;
2772     FILE *vmstate_dump_file = NULL;
2773     Error *main_loop_err = NULL;
2774
2775     atexit(qemu_run_exit_notifiers);
2776     error_set_progname(argv[0]);
2777     qemu_init_exec_dir(argv[0]);
2778
2779     g_mem_set_vtable(&mem_trace);
2780
2781     module_call_init(MODULE_INIT_QOM);
2782
2783     qemu_add_opts(&qemu_drive_opts);
2784     qemu_add_drive_opts(&qemu_legacy_drive_opts);
2785     qemu_add_drive_opts(&qemu_common_drive_opts);
2786     qemu_add_drive_opts(&qemu_drive_opts);
2787     qemu_add_opts(&qemu_chardev_opts);
2788     qemu_add_opts(&qemu_device_opts);
2789     qemu_add_opts(&qemu_netdev_opts);
2790     qemu_add_opts(&qemu_net_opts);
2791     qemu_add_opts(&qemu_rtc_opts);
2792     qemu_add_opts(&qemu_global_opts);
2793     qemu_add_opts(&qemu_mon_opts);
2794     qemu_add_opts(&qemu_trace_opts);
2795     qemu_add_opts(&qemu_option_rom_opts);
2796     qemu_add_opts(&qemu_machine_opts);
2797     qemu_add_opts(&qemu_mem_opts);
2798     qemu_add_opts(&qemu_smp_opts);
2799     qemu_add_opts(&qemu_boot_opts);
2800     qemu_add_opts(&qemu_sandbox_opts);
2801     qemu_add_opts(&qemu_add_fd_opts);
2802     qemu_add_opts(&qemu_object_opts);
2803     qemu_add_opts(&qemu_tpmdev_opts);
2804     qemu_add_opts(&qemu_realtime_opts);
2805     qemu_add_opts(&qemu_msg_opts);
2806     qemu_add_opts(&qemu_name_opts);
2807     qemu_add_opts(&qemu_numa_opts);
2808     qemu_add_opts(&qemu_icount_opts);
2809
2810     runstate_init();
2811
2812     rtc_clock = QEMU_CLOCK_HOST;
2813
2814     QLIST_INIT (&vm_change_state_head);
2815     os_setup_early_signal_handling();
2816
2817     module_call_init(MODULE_INIT_MACHINE);
2818     machine_class = find_default_machine();
2819     cpu_model = NULL;
2820     ram_size = default_ram_size;
2821     snapshot = 0;
2822     cyls = heads = secs = 0;
2823     translation = BIOS_ATA_TRANSLATION_AUTO;
2824
2825     for (i = 0; i < MAX_NODES; i++) {
2826         numa_info[i].node_mem = 0;
2827         numa_info[i].present = false;
2828         bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2829     }
2830
2831     nb_numa_nodes = 0;
2832     max_numa_nodeid = 0;
2833     nb_nics = 0;
2834
2835     bdrv_init_with_whitelist();
2836
2837     autostart = 1;
2838
2839     /* first pass of option parsing */
2840     optind = 1;
2841     while (optind < argc) {
2842         if (argv[optind][0] != '-') {
2843             /* disk image */
2844             optind++;
2845         } else {
2846             const QEMUOption *popt;
2847
2848             popt = lookup_opt(argc, argv, &optarg, &optind);
2849             switch (popt->index) {
2850             case QEMU_OPTION_nodefconfig:
2851                 defconfig = false;
2852                 break;
2853             case QEMU_OPTION_nouserconfig:
2854                 userconfig = false;
2855                 break;
2856             }
2857         }
2858     }
2859
2860     if (defconfig) {
2861         int ret;
2862         ret = qemu_read_default_config_files(userconfig);
2863         if (ret < 0) {
2864             exit(1);
2865         }
2866     }
2867
2868     /* second pass of option parsing */
2869     optind = 1;
2870     for(;;) {
2871         if (optind >= argc)
2872             break;
2873         if (argv[optind][0] != '-') {
2874             hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2875         } else {
2876             const QEMUOption *popt;
2877
2878             popt = lookup_opt(argc, argv, &optarg, &optind);
2879             if (!(popt->arch_mask & arch_type)) {
2880                 printf("Option %s not supported for this target\n", popt->name);
2881                 exit(1);
2882             }
2883             switch(popt->index) {
2884             case QEMU_OPTION_M:
2885                 machine_class = machine_parse(optarg);
2886                 break;
2887             case QEMU_OPTION_no_kvm_irqchip: {
2888                 olist = qemu_find_opts("machine");
2889                 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2890                 break;
2891             }
2892             case QEMU_OPTION_cpu:
2893                 /* hw initialization will check this */
2894                 cpu_model = optarg;
2895                 break;
2896             case QEMU_OPTION_hda:
2897                 {
2898                     char buf[256];
2899                     if (cyls == 0)
2900                         snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2901                     else
2902                         snprintf(buf, sizeof(buf),
2903                                  "%s,cyls=%d,heads=%d,secs=%d%s",
2904                                  HD_OPTS , cyls, heads, secs,
2905                                  translation == BIOS_ATA_TRANSLATION_LBA ?
2906                                  ",trans=lba" :
2907                                  translation == BIOS_ATA_TRANSLATION_NONE ?
2908                                  ",trans=none" : "");
2909                     drive_add(IF_DEFAULT, 0, optarg, buf);
2910                     break;
2911                 }
2912             case QEMU_OPTION_hdb:
2913             case QEMU_OPTION_hdc:
2914             case QEMU_OPTION_hdd:
2915                 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2916                           HD_OPTS);
2917                 break;
2918             case QEMU_OPTION_drive:
2919                 if (drive_def(optarg) == NULL) {
2920                     exit(1);
2921                 }
2922                 break;
2923             case QEMU_OPTION_set:
2924                 if (qemu_set_option(optarg) != 0)
2925                     exit(1);
2926                 break;
2927             case QEMU_OPTION_global:
2928                 if (qemu_global_option(optarg) != 0)
2929                     exit(1);
2930                 break;
2931             case QEMU_OPTION_mtdblock:
2932                 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2933                 break;
2934             case QEMU_OPTION_sd:
2935                 drive_add(IF_SD, -1, optarg, SD_OPTS);
2936                 break;
2937             case QEMU_OPTION_pflash:
2938                 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2939                 break;
2940             case QEMU_OPTION_snapshot:
2941                 snapshot = 1;
2942                 break;
2943             case QEMU_OPTION_hdachs:
2944                 {
2945                     const char *p;
2946                     p = optarg;
2947                     cyls = strtol(p, (char **)&p, 0);
2948                     if (cyls < 1 || cyls > 16383)
2949                         goto chs_fail;
2950                     if (*p != ',')
2951                         goto chs_fail;
2952                     p++;
2953                     heads = strtol(p, (char **)&p, 0);
2954                     if (heads < 1 || heads > 16)
2955                         goto chs_fail;
2956                     if (*p != ',')
2957                         goto chs_fail;
2958                     p++;
2959                     secs = strtol(p, (char **)&p, 0);
2960                     if (secs < 1 || secs > 63)
2961                         goto chs_fail;
2962                     if (*p == ',') {
2963                         p++;
2964                         if (!strcmp(p, "large")) {
2965                             translation = BIOS_ATA_TRANSLATION_LARGE;
2966                         } else if (!strcmp(p, "rechs")) {
2967                             translation = BIOS_ATA_TRANSLATION_RECHS;
2968                         } else if (!strcmp(p, "none")) {
2969                             translation = BIOS_ATA_TRANSLATION_NONE;
2970                         } else if (!strcmp(p, "lba")) {
2971                             translation = BIOS_ATA_TRANSLATION_LBA;
2972                         } else if (!strcmp(p, "auto")) {
2973                             translation = BIOS_ATA_TRANSLATION_AUTO;
2974                         } else {
2975                             goto chs_fail;
2976                         }
2977                     } else if (*p != '\0') {
2978                     chs_fail:
2979                         fprintf(stderr, "qemu: invalid physical CHS format\n");
2980                         exit(1);
2981                     }
2982                     if (hda_opts != NULL) {
2983                         char num[16];
2984                         snprintf(num, sizeof(num), "%d", cyls);
2985                         qemu_opt_set(hda_opts, "cyls", num);
2986                         snprintf(num, sizeof(num), "%d", heads);
2987                         qemu_opt_set(hda_opts, "heads", num);
2988                         snprintf(num, sizeof(num), "%d", secs);
2989                         qemu_opt_set(hda_opts, "secs", num);
2990                         if (translation == BIOS_ATA_TRANSLATION_LARGE) {
2991                             qemu_opt_set(hda_opts, "trans", "large");
2992                         } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
2993                             qemu_opt_set(hda_opts, "trans", "rechs");
2994                         } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
2995                             qemu_opt_set(hda_opts, "trans", "lba");
2996                         } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
2997                             qemu_opt_set(hda_opts, "trans", "none");
2998                         }
2999                     }
3000                 }
3001                 break;
3002             case QEMU_OPTION_numa:
3003                 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3004                 if (!opts) {
3005                     exit(1);
3006                 }
3007                 break;
3008             case QEMU_OPTION_display:
3009                 display_type = select_display(optarg);
3010                 break;
3011             case QEMU_OPTION_nographic:
3012                 display_type = DT_NOGRAPHIC;
3013                 break;
3014             case QEMU_OPTION_curses:
3015 #ifdef CONFIG_CURSES
3016                 display_type = DT_CURSES;
3017 #else
3018                 fprintf(stderr, "Curses support is disabled\n");
3019                 exit(1);
3020 #endif
3021                 break;
3022             case QEMU_OPTION_portrait:
3023                 graphic_rotate = 90;
3024                 break;
3025             case QEMU_OPTION_rotate:
3026                 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3027                 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3028                     graphic_rotate != 180 && graphic_rotate != 270) {
3029                     fprintf(stderr,
3030                         "qemu: only 90, 180, 270 deg rotation is available\n");
3031                     exit(1);
3032                 }
3033                 break;
3034             case QEMU_OPTION_kernel:
3035                 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3036                 break;
3037             case QEMU_OPTION_initrd:
3038                 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3039                 break;
3040             case QEMU_OPTION_append:
3041                 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3042                 break;
3043             case QEMU_OPTION_dtb:
3044                 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3045                 break;
3046             case QEMU_OPTION_cdrom:
3047                 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3048                 break;
3049             case QEMU_OPTION_boot:
3050                 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3051                 if (!opts) {
3052                     exit(1);
3053                 }
3054                 break;
3055             case QEMU_OPTION_fda:
3056             case QEMU_OPTION_fdb:
3057                 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3058                           optarg, FD_OPTS);
3059                 break;
3060             case QEMU_OPTION_no_fd_bootchk:
3061                 fd_bootchk = 0;
3062                 break;
3063             case QEMU_OPTION_netdev:
3064                 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3065                     exit(1);
3066                 }
3067                 break;
3068             case QEMU_OPTION_net:
3069                 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3070                     exit(1);
3071                 }
3072                 break;
3073 #ifdef CONFIG_LIBISCSI
3074             case QEMU_OPTION_iscsi:
3075                 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3076                 if (!opts) {
3077                     exit(1);
3078                 }
3079                 break;
3080 #endif
3081 #ifdef CONFIG_SLIRP
3082             case QEMU_OPTION_tftp:
3083                 legacy_tftp_prefix = optarg;
3084                 break;
3085             case QEMU_OPTION_bootp:
3086                 legacy_bootp_filename = optarg;
3087                 break;
3088             case QEMU_OPTION_redir:
3089                 if (net_slirp_redir(optarg) < 0)
3090                     exit(1);
3091                 break;
3092 #endif
3093             case QEMU_OPTION_bt:
3094                 add_device_config(DEV_BT, optarg);
3095                 break;
3096             case QEMU_OPTION_audio_help:
3097                 AUD_help ();
3098                 exit (0);
3099                 break;
3100             case QEMU_OPTION_soundhw:
3101                 select_soundhw (optarg);
3102                 break;
3103             case QEMU_OPTION_h:
3104                 help(0);
3105                 break;
3106             case QEMU_OPTION_version:
3107                 version();
3108                 exit(0);
3109                 break;
3110             case QEMU_OPTION_m: {
3111                 uint64_t sz;
3112                 const char *mem_str;
3113                 const char *maxmem_str, *slots_str;
3114
3115                 opts = qemu_opts_parse(qemu_find_opts("memory"),
3116                                        optarg, 1);
3117                 if (!opts) {
3118                     exit(EXIT_FAILURE);
3119                 }
3120
3121                 mem_str = qemu_opt_get(opts, "size");
3122                 if (!mem_str) {
3123                     error_report("invalid -m option, missing 'size' option");
3124                     exit(EXIT_FAILURE);
3125                 }
3126                 if (!*mem_str) {
3127                     error_report("missing 'size' option value");
3128                     exit(EXIT_FAILURE);
3129                 }
3130
3131                 sz = qemu_opt_get_size(opts, "size", ram_size);
3132
3133                 /* Fix up legacy suffix-less format */
3134                 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3135                     uint64_t overflow_check = sz;
3136
3137                     sz <<= 20;
3138                     if ((sz >> 20) != overflow_check) {
3139                         error_report("too large 'size' option value");
3140                         exit(EXIT_FAILURE);
3141                     }
3142                 }
3143
3144                 /* backward compatibility behaviour for case "-m 0" */
3145                 if (sz == 0) {
3146                     sz = default_ram_size;
3147                 }
3148
3149                 sz = QEMU_ALIGN_UP(sz, 8192);
3150                 ram_size = sz;
3151                 if (ram_size != sz) {
3152                     error_report("ram size too large");
3153                     exit(EXIT_FAILURE);
3154                 }
3155                 maxram_size = ram_size;
3156
3157                 maxmem_str = qemu_opt_get(opts, "maxmem");
3158                 slots_str = qemu_opt_get(opts, "slots");
3159                 if (maxmem_str && slots_str) {
3160                     uint64_t slots;
3161
3162                     sz = qemu_opt_get_size(opts, "maxmem", 0);
3163                     if (sz < ram_size) {
3164                         error_report("invalid -m option value: maxmem "
3165                                 "(0x%" PRIx64 ") <= initial memory (0x"
3166                                 RAM_ADDR_FMT ")", sz, ram_size);
3167                         exit(EXIT_FAILURE);
3168                     }
3169
3170                     slots = qemu_opt_get_number(opts, "slots", 0);
3171                     if ((sz > ram_size) && !slots) {
3172                         error_report("invalid -m option value: maxmem "
3173                                 "(0x%" PRIx64 ") more than initial memory (0x"
3174                                 RAM_ADDR_FMT ") but no hotplug slots where "
3175                                 "specified", sz, ram_size);
3176                         exit(EXIT_FAILURE);
3177                     }
3178
3179                     if ((sz <= ram_size) && slots) {
3180                         error_report("invalid -m option value:  %"
3181                                 PRIu64 " hotplug slots where specified but "
3182                                 "maxmem (0x%" PRIx64 ") <= initial memory (0x"
3183                                 RAM_ADDR_FMT ")", slots, sz, ram_size);
3184                         exit(EXIT_FAILURE);
3185                     }
3186                     maxram_size = sz;
3187                     ram_slots = slots;
3188                 } else if ((!maxmem_str && slots_str) ||
3189                            (maxmem_str && !slots_str)) {
3190                     error_report("invalid -m option value: missing "
3191                             "'%s' option", slots_str ? "maxmem" : "slots");
3192                     exit(EXIT_FAILURE);
3193                 }
3194                 break;
3195             }
3196 #ifdef CONFIG_TPM
3197             case QEMU_OPTION_tpmdev:
3198                 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3199                     exit(1);
3200                 }
3201                 break;
3202 #endif
3203             case QEMU_OPTION_mempath:
3204                 mem_path = optarg;
3205                 break;
3206             case QEMU_OPTION_mem_prealloc:
3207                 mem_prealloc = 1;
3208                 break;
3209             case QEMU_OPTION_d:
3210                 log_mask = optarg;
3211                 break;
3212             case QEMU_OPTION_D:
3213                 log_file = optarg;
3214                 break;
3215             case QEMU_OPTION_s:
3216                 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3217                 break;
3218             case QEMU_OPTION_gdb:
3219                 add_device_config(DEV_GDB, optarg);
3220                 break;
3221             case QEMU_OPTION_L:
3222                 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3223                     data_dir[data_dir_idx++] = optarg;
3224                 }
3225                 break;
3226             case QEMU_OPTION_bios:
3227                 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3228                 break;
3229             case QEMU_OPTION_singlestep:
3230                 singlestep = 1;
3231                 break;
3232             case QEMU_OPTION_S:
3233                 autostart = 0;
3234                 break;
3235             case QEMU_OPTION_k:
3236                 keyboard_layout = optarg;
3237                 break;
3238             case QEMU_OPTION_localtime:
3239                 rtc_utc = 0;
3240                 break;
3241             case QEMU_OPTION_vga:
3242                 vga_model = optarg;
3243                 default_vga = 0;
3244                 break;
3245             case QEMU_OPTION_g:
3246                 {
3247                     const char *p;
3248                     int w, h, depth;
3249                     p = optarg;
3250                     w = strtol(p, (char **)&p, 10);
3251                     if (w <= 0) {
3252                     graphic_error:
3253                         fprintf(stderr, "qemu: invalid resolution or depth\n");
3254                         exit(1);
3255                     }
3256                     if (*p != 'x')
3257                         goto graphic_error;
3258                     p++;
3259                     h = strtol(p, (char **)&p, 10);
3260                     if (h <= 0)
3261                         goto graphic_error;
3262                     if (*p == 'x') {
3263                         p++;
3264                         depth = strtol(p, (char **)&p, 10);
3265                         if (depth != 8 && depth != 15 && depth != 16 &&
3266                             depth != 24 && depth != 32)
3267                             goto graphic_error;
3268                     } else if (*p == '\0') {
3269                         depth = graphic_depth;
3270                     } else {
3271                         goto graphic_error;
3272                     }
3273
3274                     graphic_width = w;
3275                     graphic_height = h;
3276                     graphic_depth = depth;
3277                 }
3278                 break;
3279             case QEMU_OPTION_echr:
3280                 {
3281                     char *r;
3282                     term_escape_char = strtol(optarg, &r, 0);
3283                     if (r == optarg)
3284                         printf("Bad argument to echr\n");
3285                     break;
3286                 }
3287             case QEMU_OPTION_monitor:
3288                 default_monitor = 0;
3289                 if (strncmp(optarg, "none", 4)) {
3290                     monitor_parse(optarg, "readline");
3291                 }
3292                 break;
3293             case QEMU_OPTION_qmp:
3294                 monitor_parse(optarg, "control");
3295                 default_monitor = 0;
3296                 break;
3297             case QEMU_OPTION_mon:
3298                 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3299                 if (!opts) {
3300                     exit(1);
3301                 }
3302                 default_monitor = 0;
3303                 break;
3304             case QEMU_OPTION_chardev:
3305                 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3306                 if (!opts) {
3307                     exit(1);
3308                 }
3309                 break;
3310             case QEMU_OPTION_fsdev:
3311                 olist = qemu_find_opts("fsdev");
3312                 if (!olist) {
3313                     fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3314                     exit(1);
3315                 }
3316                 opts = qemu_opts_parse(olist, optarg, 1);
3317                 if (!opts) {
3318                     exit(1);
3319                 }
3320                 break;
3321             case QEMU_OPTION_virtfs: {
3322                 QemuOpts *fsdev;
3323                 QemuOpts *device;
3324                 const char *writeout, *sock_fd, *socket;
3325
3326                 olist = qemu_find_opts("virtfs");
3327                 if (!olist) {
3328                     fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3329                     exit(1);
3330                 }
3331                 opts = qemu_opts_parse(olist, optarg, 1);
3332                 if (!opts) {
3333                     exit(1);
3334                 }
3335
3336                 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3337                     qemu_opt_get(opts, "mount_tag") == NULL) {
3338                     fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3339                     exit(1);
3340                 }
3341                 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3342                                          qemu_opt_get(opts, "mount_tag"),
3343                                          1, NULL);
3344                 if (!fsdev) {
3345                     fprintf(stderr, "duplicate fsdev id: %s\n",
3346                             qemu_opt_get(opts, "mount_tag"));
3347                     exit(1);
3348                 }
3349
3350                 writeout = qemu_opt_get(opts, "writeout");
3351                 if (writeout) {
3352 #ifdef CONFIG_SYNC_FILE_RANGE
3353                     qemu_opt_set(fsdev, "writeout", writeout);
3354 #else
3355                     fprintf(stderr, "writeout=immediate not supported on "
3356                             "this platform\n");
3357                     exit(1);
3358 #endif
3359                 }
3360                 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3361                 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3362                 qemu_opt_set(fsdev, "security_model",
3363                              qemu_opt_get(opts, "security_model"));
3364                 socket = qemu_opt_get(opts, "socket");
3365                 if (socket) {
3366                     qemu_opt_set(fsdev, "socket", socket);
3367                 }
3368                 sock_fd = qemu_opt_get(opts, "sock_fd");
3369                 if (sock_fd) {
3370                     qemu_opt_set(fsdev, "sock_fd", sock_fd);
3371                 }
3372
3373                 qemu_opt_set_bool(fsdev, "readonly",
3374                                 qemu_opt_get_bool(opts, "readonly", 0));
3375                 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3376                                           &error_abort);
3377                 qemu_opt_set(device, "driver", "virtio-9p-pci");
3378                 qemu_opt_set(device, "fsdev",
3379                              qemu_opt_get(opts, "mount_tag"));
3380                 qemu_opt_set(device, "mount_tag",
3381                              qemu_opt_get(opts, "mount_tag"));
3382                 break;
3383             }
3384             case QEMU_OPTION_virtfs_synth: {
3385                 QemuOpts *fsdev;
3386                 QemuOpts *device;
3387
3388                 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3389                                          1, NULL);
3390                 if (!fsdev) {
3391                     fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3392                     exit(1);
3393                 }
3394                 qemu_opt_set(fsdev, "fsdriver", "synth");
3395
3396                 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3397                                           &error_abort);
3398                 qemu_opt_set(device, "driver", "virtio-9p-pci");
3399                 qemu_opt_set(device, "fsdev", "v_synth");
3400                 qemu_opt_set(device, "mount_tag", "v_synth");
3401                 break;
3402             }
3403             case QEMU_OPTION_serial:
3404                 add_device_config(DEV_SERIAL, optarg);
3405                 default_serial = 0;
3406                 if (strncmp(optarg, "mon:", 4) == 0) {
3407                     default_monitor = 0;
3408                 }
3409                 break;
3410             case QEMU_OPTION_watchdog:
3411                 if (watchdog) {
3412                     fprintf(stderr,
3413                             "qemu: only one watchdog option may be given\n");
3414                     return 1;
3415                 }
3416                 watchdog = optarg;
3417                 break;
3418             case QEMU_OPTION_watchdog_action:
3419                 if (select_watchdog_action(optarg) == -1) {
3420                     fprintf(stderr, "Unknown -watchdog-action parameter\n");
3421                     exit(1);
3422                 }
3423                 break;
3424             case QEMU_OPTION_virtiocon:
3425                 add_device_config(DEV_VIRTCON, optarg);
3426                 default_virtcon = 0;
3427                 if (strncmp(optarg, "mon:", 4) == 0) {
3428                     default_monitor = 0;
3429                 }
3430                 break;
3431             case QEMU_OPTION_parallel:
3432                 add_device_config(DEV_PARALLEL, optarg);
3433                 default_parallel = 0;
3434                 if (strncmp(optarg, "mon:", 4) == 0) {
3435                     default_monitor = 0;
3436                 }
3437                 break;
3438             case QEMU_OPTION_debugcon:
3439                 add_device_config(DEV_DEBUGCON, optarg);
3440                 break;
3441             case QEMU_OPTION_loadvm:
3442                 loadvm = optarg;
3443                 break;
3444             case QEMU_OPTION_full_screen:
3445                 full_screen = 1;
3446                 break;
3447             case QEMU_OPTION_no_frame:
3448                 no_frame = 1;
3449                 break;
3450             case QEMU_OPTION_alt_grab:
3451                 alt_grab = 1;
3452                 break;
3453             case QEMU_OPTION_ctrl_grab:
3454                 ctrl_grab = 1;
3455                 break;
3456             case QEMU_OPTION_no_quit:
3457                 no_quit = 1;
3458                 break;
3459             case QEMU_OPTION_sdl:
3460 #ifdef CONFIG_SDL
3461                 display_type = DT_SDL;
3462                 break;
3463 #else
3464                 fprintf(stderr, "SDL support is disabled\n");
3465                 exit(1);
3466 #endif
3467             case QEMU_OPTION_pidfile:
3468                 pid_file = optarg;
3469                 break;
3470             case QEMU_OPTION_win2k_hack:
3471                 win2k_install_hack = 1;
3472                 break;
3473             case QEMU_OPTION_rtc_td_hack: {
3474                 static GlobalProperty slew_lost_ticks[] = {
3475                     {
3476                         .driver   = "mc146818rtc",
3477                         .property = "lost_tick_policy",
3478                         .value    = "slew",
3479                     },
3480                     { /* end of list */ }
3481                 };
3482
3483                 qdev_prop_register_global_list(slew_lost_ticks);
3484                 break;
3485             }
3486             case QEMU_OPTION_acpitable:
3487                 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3488                 if (!opts) {
3489                     exit(1);
3490                 }
3491                 do_acpitable_option(opts);
3492                 break;
3493             case QEMU_OPTION_smbios:
3494                 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3495                 if (!opts) {
3496                     exit(1);
3497                 }
3498                 do_smbios_option(opts);
3499                 break;
3500             case QEMU_OPTION_enable_kvm:
3501                 olist = qemu_find_opts("machine");
3502                 qemu_opts_parse(olist, "accel=kvm", 0);
3503                 break;
3504             case QEMU_OPTION_machine:
3505                 olist = qemu_find_opts("machine");
3506                 opts = qemu_opts_parse(olist, optarg, 1);
3507                 if (!opts) {
3508                     exit(1);
3509                 }
3510                 optarg = qemu_opt_get(opts, "type");
3511                 if (optarg) {
3512                     machine_class = machine_parse(optarg);
3513                 }
3514                 break;
3515              case QEMU_OPTION_no_kvm:
3516                 olist = qemu_find_opts("machine");
3517                 qemu_opts_parse(olist, "accel=tcg", 0);
3518                 break;
3519             case QEMU_OPTION_no_kvm_pit: {
3520                 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3521                                 "separately.\n");
3522                 break;
3523             }
3524             case QEMU_OPTION_no_kvm_pit_reinjection: {
3525                 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3526                     {
3527                         .driver   = "kvm-pit",
3528                         .property = "lost_tick_policy",
3529                         .value    = "discard",
3530                     },
3531                     { /* end of list */ }
3532                 };
3533
3534                 fprintf(stderr, "Warning: option deprecated, use "
3535                         "lost_tick_policy property of kvm-pit instead.\n");
3536                 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3537                 break;
3538             }
3539             case QEMU_OPTION_usb:
3540                 olist = qemu_find_opts("machine");
3541                 qemu_opts_parse(olist, "usb=on", 0);
3542                 break;
3543             case QEMU_OPTION_usbdevice:
3544                 olist = qemu_find_opts("machine");
3545                 qemu_opts_parse(olist, "usb=on", 0);
3546                 add_device_config(DEV_USB, optarg);
3547                 break;
3548             case QEMU_OPTION_device:
3549                 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3550                     exit(1);
3551                 }
3552                 break;
3553             case QEMU_OPTION_smp:
3554                 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3555                     exit(1);
3556                 }
3557                 break;
3558             case QEMU_OPTION_vnc:
3559 #ifdef CONFIG_VNC
3560                 display_remote++;
3561                 vnc_display = optarg;
3562 #else
3563                 fprintf(stderr, "VNC support is disabled\n");
3564                 exit(1);
3565 #endif
3566                 break;
3567             case QEMU_OPTION_no_acpi:
3568                 acpi_enabled = 0;
3569                 break;
3570             case QEMU_OPTION_no_hpet:
3571                 no_hpet = 1;
3572                 break;
3573             case QEMU_OPTION_balloon:
3574                 if (balloon_parse(optarg) < 0) {
3575                     fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3576                     exit(1);
3577                 }
3578                 break;
3579             case QEMU_OPTION_no_reboot:
3580                 no_reboot = 1;
3581                 break;
3582             case QEMU_OPTION_no_shutdown:
3583                 no_shutdown = 1;
3584                 break;
3585             case QEMU_OPTION_show_cursor:
3586                 cursor_hide = 0;
3587                 break;
3588             case QEMU_OPTION_uuid:
3589                 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3590                     fprintf(stderr, "Fail to parse UUID string."
3591                             " Wrong format.\n");
3592                     exit(1);
3593                 }
3594                 qemu_uuid_set = true;
3595                 break;
3596             case QEMU_OPTION_option_rom:
3597                 if (nb_option_roms >= MAX_OPTION_ROMS) {
3598                     fprintf(stderr, "Too many option ROMs\n");
3599                     exit(1);
3600                 }
3601                 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3602                 if (!opts) {
3603                     exit(1);
3604                 }
3605                 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3606                 option_rom[nb_option_roms].bootindex =
3607                     qemu_opt_get_number(opts, "bootindex", -1);
3608                 if (!option_rom[nb_option_roms].name) {
3609                     fprintf(stderr, "Option ROM file is not specified\n");
3610                     exit(1);
3611                 }
3612                 nb_option_roms++;
3613                 break;
3614             case QEMU_OPTION_semihosting:
3615                 semihosting_enabled = 1;
3616                 break;
3617             case QEMU_OPTION_tdf:
3618                 fprintf(stderr, "Warning: user space PIT time drift fix "
3619                                 "is no longer supported.\n");
3620                 break;
3621             case QEMU_OPTION_name:
3622                 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3623                 if (!opts) {
3624                     exit(1);
3625                 }
3626                 break;
3627             case QEMU_OPTION_prom_env:
3628                 if (nb_prom_envs >= MAX_PROM_ENVS) {
3629                     fprintf(stderr, "Too many prom variables\n");
3630                     exit(1);
3631                 }
3632                 prom_envs[nb_prom_envs] = optarg;
3633                 nb_prom_envs++;
3634                 break;
3635             case QEMU_OPTION_old_param:
3636                 old_param = 1;
3637                 break;
3638             case QEMU_OPTION_clock:
3639                 /* Clock options no longer exist.  Keep this option for
3640                  * backward compatibility.
3641                  */
3642                 break;
3643             case QEMU_OPTION_startdate:
3644                 configure_rtc_date_offset(optarg, 1);
3645                 break;
3646             case QEMU_OPTION_rtc:
3647                 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3648                 if (!opts) {
3649                     exit(1);
3650                 }
3651                 configure_rtc(opts);
3652                 break;
3653             case QEMU_OPTION_tb_size:
3654                 tcg_tb_size = strtol(optarg, NULL, 0);
3655                 if (tcg_tb_size < 0) {
3656                     tcg_tb_size = 0;
3657                 }
3658                 break;
3659             case QEMU_OPTION_icount:
3660                 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3661                                               optarg, 1);
3662                 if (!icount_opts) {
3663                     exit(1);
3664                 }
3665                 break;
3666             case QEMU_OPTION_incoming:
3667                 incoming = optarg;
3668                 runstate_set(RUN_STATE_INMIGRATE);
3669                 break;
3670             case QEMU_OPTION_nodefaults:
3671                 has_defaults = 0;
3672                 break;
3673             case QEMU_OPTION_xen_domid:
3674                 if (!(xen_available())) {
3675                     printf("Option %s not supported for this target\n", popt->name);
3676                     exit(1);
3677                 }
3678                 xen_domid = atoi(optarg);
3679                 break;
3680             case QEMU_OPTION_xen_create:
3681                 if (!(xen_available())) {
3682                     printf("Option %s not supported for this target\n", popt->name);
3683                     exit(1);
3684                 }
3685                 xen_mode = XEN_CREATE;
3686                 break;
3687             case QEMU_OPTION_xen_attach:
3688                 if (!(xen_available())) {
3689                     printf("Option %s not supported for this target\n", popt->name);
3690                     exit(1);
3691                 }
3692                 xen_mode = XEN_ATTACH;
3693                 break;
3694             case QEMU_OPTION_trace:
3695             {
3696                 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3697                 if (!opts) {
3698                     exit(1);
3699                 }
3700                 trace_events = qemu_opt_get(opts, "events");
3701                 trace_file = qemu_opt_get(opts, "file");
3702                 break;
3703             }
3704             case QEMU_OPTION_readconfig:
3705                 {
3706                     int ret = qemu_read_config_file(optarg);
3707                     if (ret < 0) {
3708                         fprintf(stderr, "read config %s: %s\n", optarg,
3709                             strerror(-ret));
3710                         exit(1);
3711                     }
3712                     break;
3713                 }
3714             case QEMU_OPTION_spice:
3715                 olist = qemu_find_opts("spice");
3716                 if (!olist) {
3717                     fprintf(stderr, "spice is not supported by this qemu build.\n");
3718                     exit(1);
3719                 }
3720                 opts = qemu_opts_parse(olist, optarg, 0);
3721                 if (!opts) {
3722                     exit(1);
3723                 }
3724                 display_remote++;
3725                 break;
3726             case QEMU_OPTION_writeconfig:
3727                 {
3728                     FILE *fp;
3729                     if (strcmp(optarg, "-") == 0) {
3730                         fp = stdout;
3731                     } else {
3732                         fp = fopen(optarg, "w");
3733                         if (fp == NULL) {
3734                             fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3735                             exit(1);
3736                         }
3737                     }
3738                     qemu_config_write(fp);
3739                     if (fp != stdout) {
3740                         fclose(fp);
3741                     }
3742                     break;
3743                 }
3744             case QEMU_OPTION_qtest:
3745                 qtest_chrdev = optarg;
3746                 break;
3747             case QEMU_OPTION_qtest_log:
3748                 qtest_log = optarg;
3749                 break;
3750             case QEMU_OPTION_sandbox:
3751                 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3752                 if (!opts) {
3753                     exit(1);
3754                 }
3755                 break;
3756             case QEMU_OPTION_add_fd:
3757 #ifndef _WIN32
3758                 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3759                 if (!opts) {
3760                     exit(1);
3761                 }
3762 #else
3763                 error_report("File descriptor passing is disabled on this "
3764                              "platform");
3765                 exit(1);
3766 #endif
3767                 break;
3768             case QEMU_OPTION_object:
3769                 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3770                 if (!opts) {
3771                     exit(1);
3772                 }
3773                 break;
3774             case QEMU_OPTION_realtime:
3775                 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3776                 if (!opts) {
3777                     exit(1);
3778                 }
3779                 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3780                 break;
3781             case QEMU_OPTION_msg:
3782                 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3783                 if (!opts) {
3784                     exit(1);
3785                 }
3786                 configure_msg(opts);
3787                 break;
3788             case QEMU_OPTION_dump_vmstate:
3789                 vmstate_dump_file = fopen(optarg, "w");
3790                 if (vmstate_dump_file == NULL) {
3791                     fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3792                     exit(1);
3793                 }
3794                 break;
3795             default:
3796                 os_parse_cmd_args(popt->index, optarg);
3797             }
3798         }
3799     }
3800     loc_set_none();
3801
3802     os_daemonize();
3803
3804     if (qemu_init_main_loop(&main_loop_err)) {
3805         error_report("%s", error_get_pretty(main_loop_err));
3806         exit(1);
3807     }
3808
3809     if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3810         exit(1);
3811     }
3812
3813     if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3814         exit(1);
3815     }
3816
3817 #ifndef _WIN32
3818     if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3819         exit(1);
3820     }
3821
3822     if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3823         exit(1);
3824     }
3825 #endif
3826
3827     if (machine_class == NULL) {
3828         fprintf(stderr, "No machine specified, and there is no default.\n"
3829                 "Use -machine help to list supported machines!\n");
3830         exit(1);
3831     }
3832
3833     current_machine = MACHINE(object_new(object_class_get_name(
3834                           OBJECT_CLASS(machine_class))));
3835     object_property_add_child(object_get_root(), "machine",
3836                               OBJECT(current_machine), &error_abort);
3837     cpu_exec_init_all();
3838
3839     if (machine_class->hw_version) {
3840         qemu_set_version(machine_class->hw_version);
3841     }
3842
3843     /* Init CPU def lists, based on config
3844      * - Must be called after all the qemu_read_config_file() calls
3845      * - Must be called before list_cpus()
3846      * - Must be called before machine->init()
3847      */
3848     cpudef_init();
3849
3850     if (cpu_model && is_help_option(cpu_model)) {
3851         list_cpus(stdout, &fprintf, cpu_model);
3852         exit(0);
3853     }
3854
3855     /* Open the logfile at this point, if necessary. We can't open the logfile
3856      * when encountering either of the logging options (-d or -D) because the
3857      * other one may be encountered later on the command line, changing the
3858      * location or level of logging.
3859      */
3860     if (log_mask) {
3861         int mask;
3862         if (log_file) {
3863             qemu_set_log_filename(log_file);
3864         }
3865
3866         mask = qemu_str_to_log_mask(log_mask);
3867         if (!mask) {
3868             qemu_print_log_usage(stdout);
3869             exit(1);
3870         }
3871         qemu_set_log(mask);
3872     }
3873
3874     if (!is_daemonized()) {
3875         if (!trace_init_backends(trace_events, trace_file)) {
3876             exit(1);
3877         }
3878     }
3879
3880     /* If no data_dir is specified then try to find it relative to the
3881        executable path.  */
3882     if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3883         data_dir[data_dir_idx] = os_find_datadir();
3884         if (data_dir[data_dir_idx] != NULL) {
3885             data_dir_idx++;
3886         }
3887     }
3888     /* If all else fails use the install path specified when building. */
3889     if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3890         data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3891     }
3892
3893     smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3894
3895     machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3896     if (smp_cpus > machine_class->max_cpus) {
3897         fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3898                 "supported by machine `%s' (%d)\n", smp_cpus,
3899                 machine_class->name, machine_class->max_cpus);
3900         exit(1);
3901     }
3902
3903     /*
3904      * Get the default machine options from the machine if it is not already
3905      * specified either by the configuration file or by the command line.
3906      */
3907     if (machine_class->default_machine_opts) {
3908         qemu_opts_set_defaults(qemu_find_opts("machine"),
3909                                machine_class->default_machine_opts, 0);
3910     }
3911
3912     qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3913     qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3914
3915     if (!vga_model && !default_vga) {
3916         vga_interface_type = VGA_DEVICE;
3917     }
3918     if (!has_defaults || machine_class->no_serial) {
3919         default_serial = 0;
3920     }
3921     if (!has_defaults || machine_class->no_parallel) {
3922         default_parallel = 0;
3923     }
3924     if (!has_defaults || !machine_class->use_virtcon) {
3925         default_virtcon = 0;
3926     }
3927     if (!has_defaults || !machine_class->use_sclp) {
3928         default_sclp = 0;
3929     }
3930     if (!has_defaults || machine_class->no_floppy) {
3931         default_floppy = 0;
3932     }
3933     if (!has_defaults || machine_class->no_cdrom) {
3934         default_cdrom = 0;
3935     }
3936     if (!has_defaults || machine_class->no_sdcard) {
3937         default_sdcard = 0;
3938     }
3939     if (!has_defaults) {
3940         default_monitor = 0;
3941         default_net = 0;
3942         default_vga = 0;
3943     }
3944
3945     if (is_daemonized()) {
3946         /* According to documentation and historically, -nographic redirects
3947          * serial port, parallel port and monitor to stdio, which does not work
3948          * with -daemonize.  We can redirect these to null instead, but since
3949          * -nographic is legacy, let's just error out.
3950          * We disallow -nographic only if all other ports are not redirected
3951          * explicitly, to not break existing legacy setups which uses
3952          * -nographic _and_ redirects all ports explicitly - this is valid
3953          * usage, -nographic is just a no-op in this case.
3954          */
3955         if (display_type == DT_NOGRAPHIC
3956             && (default_parallel || default_serial
3957                 || default_monitor || default_virtcon)) {
3958             fprintf(stderr, "-nographic can not be used with -daemonize\n");
3959             exit(1);
3960         }
3961 #ifdef CONFIG_CURSES
3962         if (display_type == DT_CURSES) {
3963             fprintf(stderr, "curses display can not be used with -daemonize\n");
3964             exit(1);
3965         }
3966 #endif
3967     }
3968
3969     if (display_type == DT_NOGRAPHIC) {
3970         if (default_parallel)
3971             add_device_config(DEV_PARALLEL, "null");
3972         if (default_serial && default_monitor) {
3973             add_device_config(DEV_SERIAL, "mon:stdio");
3974         } else if (default_virtcon && default_monitor) {
3975             add_device_config(DEV_VIRTCON, "mon:stdio");
3976         } else if (default_sclp && default_monitor) {
3977             add_device_config(DEV_SCLP, "mon:stdio");
3978         } else {
3979             if (default_serial)
3980                 add_device_config(DEV_SERIAL, "stdio");
3981             if (default_virtcon)
3982                 add_device_config(DEV_VIRTCON, "stdio");
3983             if (default_sclp) {
3984                 add_device_config(DEV_SCLP, "stdio");
3985             }
3986             if (default_monitor)
3987                 monitor_parse("stdio", "readline");
3988         }
3989     } else {
3990         if (default_serial)
3991             add_device_config(DEV_SERIAL, "vc:80Cx24C");
3992         if (default_parallel)
3993             add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3994         if (default_monitor)
3995             monitor_parse("vc:80Cx24C", "readline");
3996         if (default_virtcon)
3997             add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3998         if (default_sclp) {
3999             add_device_config(DEV_SCLP, "vc:80Cx24C");
4000         }
4001     }
4002
4003     if (display_type == DT_DEFAULT && !display_remote) {
4004 #if defined(CONFIG_GTK)
4005         display_type = DT_GTK;
4006 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4007         display_type = DT_SDL;
4008 #elif defined(CONFIG_VNC)
4009         vnc_display = "localhost:0,to=99";
4010         show_vnc_port = 1;
4011 #else
4012         display_type = DT_NONE;
4013 #endif
4014     }
4015
4016     if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4017         fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4018                         "for SDL, ignoring option\n");
4019     }
4020     if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4021         fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4022                         "ignoring option\n");
4023     }
4024
4025 #if defined(CONFIG_GTK)
4026     if (display_type == DT_GTK) {
4027         early_gtk_display_init();
4028     }
4029 #endif
4030
4031     socket_init();
4032
4033     if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4034         exit(1);
4035 #ifdef CONFIG_VIRTFS
4036     if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4037         exit(1);
4038     }
4039 #endif
4040
4041     if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4042         os_pidfile_error();
4043         exit(1);
4044     }
4045
4046     /* store value for the future use */
4047     qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4048
4049     if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4050         != 0) {
4051         exit(0);
4052     }
4053
4054     if (qemu_opts_foreach(qemu_find_opts("object"),
4055                           object_create, NULL, 0) != 0) {
4056         exit(1);
4057     }
4058
4059     machine_opts = qemu_get_machine_opts();
4060     if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4061                          1) < 0) {
4062         object_unref(OBJECT(current_machine));
4063         exit(1);
4064     }
4065
4066     configure_accelerator(current_machine);
4067
4068     if (qtest_chrdev) {
4069         Error *local_err = NULL;
4070         qtest_init(qtest_chrdev, qtest_log, &local_err);
4071         if (local_err) {
4072             error_report("%s", error_get_pretty(local_err));
4073             error_free(local_err);
4074             exit(1);
4075         }
4076     }
4077
4078     machine_opts = qemu_get_machine_opts();
4079     kernel_filename = qemu_opt_get(machine_opts, "kernel");
4080     initrd_filename = qemu_opt_get(machine_opts, "initrd");
4081     kernel_cmdline = qemu_opt_get(machine_opts, "append");
4082     bios_name = qemu_opt_get(machine_opts, "firmware");
4083
4084     boot_order = machine_class->default_boot_order;
4085     opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4086     if (opts) {
4087         char *normal_boot_order;
4088         const char *order, *once;
4089
4090         order = qemu_opt_get(opts, "order");
4091         if (order) {
4092             validate_bootdevices(order);
4093             boot_order = order;
4094         }
4095
4096         once = qemu_opt_get(opts, "once");
4097         if (once) {
4098             validate_bootdevices(once);
4099             normal_boot_order = g_strdup(boot_order);
4100             boot_order = once;
4101             qemu_register_reset(restore_boot_order, normal_boot_order);
4102         }
4103
4104         boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4105         boot_strict = qemu_opt_get_bool(opts, "strict", false);
4106     }
4107
4108     if (!kernel_cmdline) {
4109         kernel_cmdline = "";
4110         current_machine->kernel_cmdline = (char *)kernel_cmdline;
4111     }
4112
4113     linux_boot = (kernel_filename != NULL);
4114
4115     if (!linux_boot && *kernel_cmdline != '\0') {
4116         fprintf(stderr, "-append only allowed with -kernel option\n");
4117         exit(1);
4118     }
4119
4120     if (!linux_boot && initrd_filename != NULL) {
4121         fprintf(stderr, "-initrd only allowed with -kernel option\n");
4122         exit(1);
4123     }
4124
4125     if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4126         fprintf(stderr, "-dtb only allowed with -kernel option\n");
4127         exit(1);
4128     }
4129
4130     os_set_line_buffering();
4131
4132     qemu_init_cpu_loop();
4133     qemu_mutex_lock_iothread();
4134
4135 #ifdef CONFIG_SPICE
4136     /* spice needs the timers to be initialized by this point */
4137     qemu_spice_init();
4138 #endif
4139
4140     cpu_ticks_init();
4141     if (icount_opts) {
4142         if (kvm_enabled() || xen_enabled()) {
4143             fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4144             exit(1);
4145         }
4146         configure_icount(icount_opts, &error_abort);
4147         qemu_opts_del(icount_opts);
4148     }
4149
4150     /* clean up network at qemu process termination */
4151     atexit(&net_cleanup);
4152
4153     if (net_init_clients() < 0) {
4154         exit(1);
4155     }
4156
4157 #ifdef CONFIG_TPM
4158     if (tpm_init() < 0) {
4159         exit(1);
4160     }
4161 #endif
4162
4163     /* init the bluetooth world */
4164     if (foreach_device_config(DEV_BT, bt_parse))
4165         exit(1);
4166
4167     if (!xen_enabled()) {
4168         /* On 32-bit hosts, QEMU is limited by virtual address space */
4169         if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4170             fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4171             exit(1);
4172         }
4173     }
4174
4175     blk_mig_init();
4176     ram_mig_init();
4177
4178     /* If the currently selected machine wishes to override the units-per-bus
4179      * property of its default HBA interface type, do so now. */
4180     if (machine_class->units_per_default_bus) {
4181         override_max_devs(machine_class->block_default_type,
4182                           machine_class->units_per_default_bus);
4183     }
4184
4185     /* open the virtual block devices */
4186     if (snapshot)
4187         qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4188     if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4189                           &machine_class->block_default_type, 1) != 0) {
4190         exit(1);
4191     }
4192
4193     default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4194                   CDROM_OPTS);
4195     default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4196     default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4197
4198     if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4199                           NULL, 1) != 0) {
4200         exit(1);
4201     }
4202
4203     set_numa_nodes();
4204
4205     if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4206         exit(1);
4207     }
4208
4209     if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4210         exit(1);
4211     if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4212         exit(1);
4213     if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4214         exit(1);
4215     if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4216         exit(1);
4217     }
4218     if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4219         exit(1);
4220
4221     /* If no default VGA is requested, the default is "none".  */
4222     if (default_vga) {
4223         if (cirrus_vga_available()) {
4224             vga_model = "cirrus";
4225         } else if (vga_available()) {
4226             vga_model = "std";
4227         }
4228     }
4229     if (vga_model) {
4230         select_vgahw(vga_model);
4231     }
4232
4233     if (watchdog) {
4234         i = select_watchdog(watchdog);
4235         if (i > 0)
4236             exit (i == 1 ? 1 : 0);
4237     }
4238
4239     if (machine_class->compat_props) {
4240         qdev_prop_register_global_list(machine_class->compat_props);
4241     }
4242     qemu_add_globals();
4243
4244     qdev_machine_init();
4245
4246     current_machine->ram_size = ram_size;
4247     current_machine->maxram_size = maxram_size;
4248     current_machine->ram_slots = ram_slots;
4249     current_machine->boot_order = boot_order;
4250     current_machine->cpu_model = cpu_model;
4251
4252     machine_class->init(current_machine);
4253
4254     realtime_init();
4255
4256     audio_init();
4257
4258     cpu_synchronize_all_post_init();
4259
4260     set_numa_modes();
4261
4262     /* init USB devices */
4263     if (usb_enabled(false)) {
4264         if (foreach_device_config(DEV_USB, usb_parse) < 0)
4265             exit(1);
4266     }
4267
4268     /* init generic devices */
4269     if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4270         exit(1);
4271
4272     /* Did we create any drives that we failed to create a device for? */
4273     drive_check_orphaned();
4274
4275     net_check_clients();
4276
4277     ds = init_displaystate();
4278
4279     /* init local displays */
4280     switch (display_type) {
4281     case DT_NOGRAPHIC:
4282         (void)ds;       /* avoid warning if no display is configured */
4283         break;
4284 #if defined(CONFIG_CURSES)
4285     case DT_CURSES:
4286         curses_display_init(ds, full_screen);
4287         break;
4288 #endif
4289 #if defined(CONFIG_SDL)
4290     case DT_SDL:
4291         sdl_display_init(ds, full_screen, no_frame);
4292         break;
4293 #elif defined(CONFIG_COCOA)
4294     case DT_SDL:
4295         cocoa_display_init(ds, full_screen);
4296         break;
4297 #endif
4298 #if defined(CONFIG_GTK)
4299     case DT_GTK:
4300         gtk_display_init(ds, full_screen, grab_on_hover);
4301         break;
4302 #endif
4303     default:
4304         break;
4305     }
4306
4307     /* must be after terminal init, SDL library changes signal handlers */
4308     os_setup_signal_handling();
4309
4310 #ifdef CONFIG_VNC
4311     /* init remote displays */
4312     if (vnc_display) {
4313         Error *local_err = NULL;
4314         vnc_display_init(ds);
4315         vnc_display_open(ds, vnc_display, &local_err);
4316         if (local_err != NULL) {
4317             error_report("Failed to start VNC server on `%s': %s",
4318                          vnc_display, error_get_pretty(local_err));
4319             error_free(local_err);
4320             exit(1);
4321         }
4322
4323         if (show_vnc_port) {
4324             printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4325         }
4326     }
4327 #endif
4328 #ifdef CONFIG_SPICE
4329     if (using_spice) {
4330         qemu_spice_display_init();
4331     }
4332 #endif
4333
4334     if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4335         exit(1);
4336     }
4337
4338     qdev_machine_creation_done();
4339
4340     if (rom_load_all() != 0) {
4341         fprintf(stderr, "rom loading failed\n");
4342         exit(1);
4343     }
4344
4345     /* TODO: once all bus devices are qdevified, this should be done
4346      * when bus is created by qdev.c */
4347     qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4348     qemu_run_machine_init_done_notifiers();
4349
4350     /* Done notifiers can load ROMs */
4351     rom_load_done();
4352
4353     qemu_system_reset(VMRESET_SILENT);
4354     if (loadvm) {
4355         if (load_vmstate(loadvm) < 0) {
4356             autostart = 0;
4357         }
4358     }
4359
4360     qdev_prop_check_globals();
4361     if (vmstate_dump_file) {
4362         /* dump and exit */
4363         dump_vmstate_json_to_file(vmstate_dump_file);
4364         return 0;
4365     }
4366
4367     if (incoming) {
4368         Error *local_err = NULL;
4369         qemu_start_incoming_migration(incoming, &local_err);
4370         if (local_err) {
4371             error_report("-incoming %s: %s", incoming,
4372                          error_get_pretty(local_err));
4373             error_free(local_err);
4374             exit(1);
4375         }
4376     } else if (autostart) {
4377         vm_start();
4378     }
4379
4380     os_setup_post();
4381
4382     if (is_daemonized()) {
4383         if (!trace_init_backends(trace_events, trace_file)) {
4384             exit(1);
4385         }
4386     }
4387
4388     main_loop();
4389     bdrv_close_all();
4390     pause_all_vcpus();
4391     res_free();
4392 #ifdef CONFIG_TPM
4393     tpm_cleanup();
4394 #endif
4395
4396     return 0;
4397 }
This page took 0.25799 seconds and 4 git commands to generate.