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