]> Git Repo - qemu.git/blob - softmmu/vl.c
Merge remote-tracking branch 'remotes/hdeller/tags/hppa-updates-pull-request' into...
[qemu.git] / softmmu / 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
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "hw/qdev-properties.h"
31 #include "qapi/compat-policy.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qstring.h"
35 #include "qapi/qmp/qjson.h"
36 #include "qemu-version.h"
37 #include "qemu/cutils.h"
38 #include "qemu/help_option.h"
39 #include "qemu/uuid.h"
40 #include "sysemu/reset.h"
41 #include "sysemu/runstate.h"
42 #include "sysemu/runstate-action.h"
43 #include "sysemu/seccomp.h"
44 #include "sysemu/tcg.h"
45 #include "sysemu/xen.h"
46
47 #include "qemu/error-report.h"
48 #include "qemu/sockets.h"
49 #include "qemu/accel.h"
50 #include "hw/usb.h"
51 #include "hw/isa/isa.h"
52 #include "hw/scsi/scsi.h"
53 #include "hw/display/vga.h"
54 #include "sysemu/watchdog.h"
55 #include "hw/firmware/smbios.h"
56 #include "hw/acpi/acpi.h"
57 #include "hw/xen/xen.h"
58 #include "hw/loader.h"
59 #include "monitor/qdev.h"
60 #include "net/net.h"
61 #include "net/slirp.h"
62 #include "monitor/monitor.h"
63 #include "ui/console.h"
64 #include "ui/input.h"
65 #include "sysemu/sysemu.h"
66 #include "sysemu/numa.h"
67 #include "sysemu/hostmem.h"
68 #include "exec/gdbstub.h"
69 #include "qemu/timer.h"
70 #include "chardev/char.h"
71 #include "qemu/bitmap.h"
72 #include "qemu/log.h"
73 #include "sysemu/blockdev.h"
74 #include "hw/block/block.h"
75 #include "hw/i386/x86.h"
76 #include "hw/i386/pc.h"
77 #include "migration/misc.h"
78 #include "migration/snapshot.h"
79 #include "sysemu/tpm.h"
80 #include "sysemu/dma.h"
81 #include "hw/audio/soundhw.h"
82 #include "audio/audio.h"
83 #include "sysemu/cpus.h"
84 #include "sysemu/cpu-timers.h"
85 #include "migration/colo.h"
86 #include "migration/postcopy-ram.h"
87 #include "sysemu/kvm.h"
88 #include "sysemu/hax.h"
89 #include "qapi/qobject-input-visitor.h"
90 #include "qemu/option.h"
91 #include "qemu/config-file.h"
92 #include "qemu/qemu-options.h"
93 #include "qemu/main-loop.h"
94 #ifdef CONFIG_VIRTFS
95 #include "fsdev/qemu-fsdev.h"
96 #endif
97 #include "sysemu/qtest.h"
98
99 #include "disas/disas.h"
100
101 #include "trace.h"
102 #include "trace/control.h"
103 #include "qemu/plugin.h"
104 #include "qemu/queue.h"
105 #include "sysemu/arch_init.h"
106 #include "exec/confidential-guest-support.h"
107
108 #include "ui/qemu-spice.h"
109 #include "qapi/string-input-visitor.h"
110 #include "qapi/opts-visitor.h"
111 #include "qapi/clone-visitor.h"
112 #include "qom/object_interfaces.h"
113 #include "semihosting/semihost.h"
114 #include "crypto/init.h"
115 #include "sysemu/replay.h"
116 #include "qapi/qapi-events-run-state.h"
117 #include "qapi/qapi-visit-block-core.h"
118 #include "qapi/qapi-visit-compat.h"
119 #include "qapi/qapi-visit-ui.h"
120 #include "qapi/qapi-commands-block-core.h"
121 #include "qapi/qapi-commands-migration.h"
122 #include "qapi/qapi-commands-misc.h"
123 #include "qapi/qapi-visit-qom.h"
124 #include "qapi/qapi-commands-ui.h"
125 #include "qapi/qmp/qdict.h"
126 #include "qapi/qmp/qerror.h"
127 #include "sysemu/iothread.h"
128 #include "qemu/guest-random.h"
129
130 #include "config-host.h"
131
132 #define MAX_VIRTIO_CONSOLES 1
133
134 typedef struct BlockdevOptionsQueueEntry {
135     BlockdevOptions *bdo;
136     Location loc;
137     QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
138 } BlockdevOptionsQueueEntry;
139
140 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
141
142 typedef struct ObjectOption {
143     ObjectOptions *opts;
144     QTAILQ_ENTRY(ObjectOption) next;
145 } ObjectOption;
146
147 typedef struct DeviceOption {
148     QDict *opts;
149     Location loc;
150     QTAILQ_ENTRY(DeviceOption) next;
151 } DeviceOption;
152
153 static const char *cpu_option;
154 static const char *mem_path;
155 static const char *incoming;
156 static const char *loadvm;
157 static const char *accelerators;
158 static QDict *machine_opts_dict;
159 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
160 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
161 static ram_addr_t maxram_size;
162 static uint64_t ram_slots;
163 static int display_remote;
164 static int snapshot;
165 static bool preconfig_requested;
166 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
167 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
168 static bool nographic = false;
169 static int mem_prealloc; /* force preallocation of physical target memory */
170 static ram_addr_t ram_size;
171 static const char *vga_model = NULL;
172 static DisplayOptions dpy;
173 static int num_serial_hds;
174 static Chardev **serial_hds;
175 static const char *log_mask;
176 static const char *log_file;
177 static bool list_data_dirs;
178 static const char *watchdog;
179 static const char *qtest_chrdev;
180 static const char *qtest_log;
181
182 static int has_defaults = 1;
183 static int default_serial = 1;
184 static int default_parallel = 1;
185 static int default_monitor = 1;
186 static int default_floppy = 1;
187 static int default_cdrom = 1;
188 static int default_sdcard = 1;
189 static int default_vga = 1;
190 static int default_net = 1;
191
192 static struct {
193     const char *driver;
194     int *flag;
195 } default_list[] = {
196     { .driver = "isa-serial",           .flag = &default_serial    },
197     { .driver = "isa-parallel",         .flag = &default_parallel  },
198     { .driver = "isa-fdc",              .flag = &default_floppy    },
199     { .driver = "floppy",               .flag = &default_floppy    },
200     { .driver = "ide-cd",               .flag = &default_cdrom     },
201     { .driver = "ide-hd",               .flag = &default_cdrom     },
202     { .driver = "scsi-cd",              .flag = &default_cdrom     },
203     { .driver = "scsi-hd",              .flag = &default_cdrom     },
204     { .driver = "VGA",                  .flag = &default_vga       },
205     { .driver = "isa-vga",              .flag = &default_vga       },
206     { .driver = "cirrus-vga",           .flag = &default_vga       },
207     { .driver = "isa-cirrus-vga",       .flag = &default_vga       },
208     { .driver = "vmware-svga",          .flag = &default_vga       },
209     { .driver = "qxl-vga",              .flag = &default_vga       },
210     { .driver = "virtio-vga",           .flag = &default_vga       },
211     { .driver = "ati-vga",              .flag = &default_vga       },
212     { .driver = "vhost-user-vga",       .flag = &default_vga       },
213     { .driver = "virtio-vga-gl",        .flag = &default_vga       },
214 };
215
216 static QemuOptsList qemu_rtc_opts = {
217     .name = "rtc",
218     .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
219     .merge_lists = true,
220     .desc = {
221         {
222             .name = "base",
223             .type = QEMU_OPT_STRING,
224         },{
225             .name = "clock",
226             .type = QEMU_OPT_STRING,
227         },{
228             .name = "driftfix",
229             .type = QEMU_OPT_STRING,
230         },
231         { /* end of list */ }
232     },
233 };
234
235 static QemuOptsList qemu_option_rom_opts = {
236     .name = "option-rom",
237     .implied_opt_name = "romfile",
238     .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
239     .desc = {
240         {
241             .name = "bootindex",
242             .type = QEMU_OPT_NUMBER,
243         }, {
244             .name = "romfile",
245             .type = QEMU_OPT_STRING,
246         },
247         { /* end of list */ }
248     },
249 };
250
251 static QemuOptsList qemu_accel_opts = {
252     .name = "accel",
253     .implied_opt_name = "accel",
254     .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
255     .desc = {
256         /*
257          * no elements => accept any
258          * sanity checking will happen later
259          * when setting accelerator properties
260          */
261         { }
262     },
263 };
264
265 static QemuOptsList qemu_boot_opts = {
266     .name = "boot-opts",
267     .implied_opt_name = "order",
268     .merge_lists = true,
269     .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
270     .desc = {
271         {
272             .name = "order",
273             .type = QEMU_OPT_STRING,
274         }, {
275             .name = "once",
276             .type = QEMU_OPT_STRING,
277         }, {
278             .name = "menu",
279             .type = QEMU_OPT_BOOL,
280         }, {
281             .name = "splash",
282             .type = QEMU_OPT_STRING,
283         }, {
284             .name = "splash-time",
285             .type = QEMU_OPT_NUMBER,
286         }, {
287             .name = "reboot-timeout",
288             .type = QEMU_OPT_NUMBER,
289         }, {
290             .name = "strict",
291             .type = QEMU_OPT_BOOL,
292         },
293         { /*End of list */ }
294     },
295 };
296
297 static QemuOptsList qemu_add_fd_opts = {
298     .name = "add-fd",
299     .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
300     .desc = {
301         {
302             .name = "fd",
303             .type = QEMU_OPT_NUMBER,
304             .help = "file descriptor of which a duplicate is added to fd set",
305         },{
306             .name = "set",
307             .type = QEMU_OPT_NUMBER,
308             .help = "ID of the fd set to add fd to",
309         },{
310             .name = "opaque",
311             .type = QEMU_OPT_STRING,
312             .help = "free-form string used to describe fd",
313         },
314         { /* end of list */ }
315     },
316 };
317
318 static QemuOptsList qemu_object_opts = {
319     .name = "object",
320     .implied_opt_name = "qom-type",
321     .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
322     .desc = {
323         { }
324     },
325 };
326
327 static QemuOptsList qemu_tpmdev_opts = {
328     .name = "tpmdev",
329     .implied_opt_name = "type",
330     .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
331     .desc = {
332         /* options are defined in the TPM backends */
333         { /* end of list */ }
334     },
335 };
336
337 static QemuOptsList qemu_overcommit_opts = {
338     .name = "overcommit",
339     .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
340     .desc = {
341         {
342             .name = "mem-lock",
343             .type = QEMU_OPT_BOOL,
344         },
345         {
346             .name = "cpu-pm",
347             .type = QEMU_OPT_BOOL,
348         },
349         { /* end of list */ }
350     },
351 };
352
353 static QemuOptsList qemu_msg_opts = {
354     .name = "msg",
355     .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
356     .desc = {
357         {
358             .name = "timestamp",
359             .type = QEMU_OPT_BOOL,
360         },
361         {
362             .name = "guest-name",
363             .type = QEMU_OPT_BOOL,
364             .help = "Prepends guest name for error messages but only if "
365                     "-name guest is set otherwise option is ignored\n",
366         },
367         { /* end of list */ }
368     },
369 };
370
371 static QemuOptsList qemu_name_opts = {
372     .name = "name",
373     .implied_opt_name = "guest",
374     .merge_lists = true,
375     .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
376     .desc = {
377         {
378             .name = "guest",
379             .type = QEMU_OPT_STRING,
380             .help = "Sets the name of the guest.\n"
381                     "This name will be displayed in the SDL window caption.\n"
382                     "The name will also be used for the VNC server",
383         }, {
384             .name = "process",
385             .type = QEMU_OPT_STRING,
386             .help = "Sets the name of the QEMU process, as shown in top etc",
387         }, {
388             .name = "debug-threads",
389             .type = QEMU_OPT_BOOL,
390             .help = "When enabled, name the individual threads; defaults off.\n"
391                     "NOTE: The thread names are for debugging and not a\n"
392                     "stable API.",
393         },
394         { /* End of list */ }
395     },
396 };
397
398 static QemuOptsList qemu_mem_opts = {
399     .name = "memory",
400     .implied_opt_name = "size",
401     .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
402     .merge_lists = true,
403     .desc = {
404         {
405             .name = "size",
406             .type = QEMU_OPT_SIZE,
407         },
408         {
409             .name = "slots",
410             .type = QEMU_OPT_NUMBER,
411         },
412         {
413             .name = "maxmem",
414             .type = QEMU_OPT_SIZE,
415         },
416         { /* end of list */ }
417     },
418 };
419
420 static QemuOptsList qemu_icount_opts = {
421     .name = "icount",
422     .implied_opt_name = "shift",
423     .merge_lists = true,
424     .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
425     .desc = {
426         {
427             .name = "shift",
428             .type = QEMU_OPT_STRING,
429         }, {
430             .name = "align",
431             .type = QEMU_OPT_BOOL,
432         }, {
433             .name = "sleep",
434             .type = QEMU_OPT_BOOL,
435         }, {
436             .name = "rr",
437             .type = QEMU_OPT_STRING,
438         }, {
439             .name = "rrfile",
440             .type = QEMU_OPT_STRING,
441         }, {
442             .name = "rrsnapshot",
443             .type = QEMU_OPT_STRING,
444         },
445         { /* end of list */ }
446     },
447 };
448
449 static QemuOptsList qemu_fw_cfg_opts = {
450     .name = "fw_cfg",
451     .implied_opt_name = "name",
452     .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
453     .desc = {
454         {
455             .name = "name",
456             .type = QEMU_OPT_STRING,
457             .help = "Sets the fw_cfg name of the blob to be inserted",
458         }, {
459             .name = "file",
460             .type = QEMU_OPT_STRING,
461             .help = "Sets the name of the file from which "
462                     "the fw_cfg blob will be loaded",
463         }, {
464             .name = "string",
465             .type = QEMU_OPT_STRING,
466             .help = "Sets content of the blob to be inserted from a string",
467         }, {
468             .name = "gen_id",
469             .type = QEMU_OPT_STRING,
470             .help = "Sets id of the object generating the fw_cfg blob "
471                     "to be inserted",
472         },
473         { /* end of list */ }
474     },
475 };
476
477 static QemuOptsList qemu_action_opts = {
478     .name = "action",
479     .merge_lists = true,
480     .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
481     .desc = {
482         {
483             .name = "shutdown",
484             .type = QEMU_OPT_STRING,
485         },{
486             .name = "reboot",
487             .type = QEMU_OPT_STRING,
488         },{
489             .name = "panic",
490             .type = QEMU_OPT_STRING,
491         },{
492             .name = "watchdog",
493             .type = QEMU_OPT_STRING,
494         },
495         { /* end of list */ }
496     },
497 };
498
499 const char *qemu_get_vm_name(void)
500 {
501     return qemu_name;
502 }
503
504 static void default_driver_disable(const char *driver)
505 {
506     int i;
507
508     if (!driver) {
509         return;
510     }
511
512     for (i = 0; i < ARRAY_SIZE(default_list); i++) {
513         if (strcmp(default_list[i].driver, driver) != 0)
514             continue;
515         *(default_list[i].flag) = 0;
516     }
517 }
518
519 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
520 {
521     const char *driver = qemu_opt_get(opts, "driver");
522
523     default_driver_disable(driver);
524     return 0;
525 }
526
527 static void default_driver_check_json(void)
528 {
529     DeviceOption *opt;
530
531     QTAILQ_FOREACH(opt, &device_opts, next) {
532         const char *driver = qdict_get_try_str(opt->opts, "driver");
533         default_driver_disable(driver);
534     }
535 }
536
537 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
538 {
539     const char *proc_name;
540
541     if (qemu_opt_get(opts, "debug-threads")) {
542         qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
543     }
544     qemu_name = qemu_opt_get(opts, "guest");
545
546     proc_name = qemu_opt_get(opts, "process");
547     if (proc_name) {
548         os_set_proc_name(proc_name);
549     }
550
551     return 0;
552 }
553
554 bool defaults_enabled(void)
555 {
556     return has_defaults;
557 }
558
559 #ifndef _WIN32
560 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
561 {
562     int fd, dupfd, flags;
563     int64_t fdset_id;
564     const char *fd_opaque = NULL;
565     AddfdInfo *fdinfo;
566
567     fd = qemu_opt_get_number(opts, "fd", -1);
568     fdset_id = qemu_opt_get_number(opts, "set", -1);
569     fd_opaque = qemu_opt_get(opts, "opaque");
570
571     if (fd < 0) {
572         error_setg(errp, "fd option is required and must be non-negative");
573         return -1;
574     }
575
576     if (fd <= STDERR_FILENO) {
577         error_setg(errp, "fd cannot be a standard I/O stream");
578         return -1;
579     }
580
581     /*
582      * All fds inherited across exec() necessarily have FD_CLOEXEC
583      * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
584      */
585     flags = fcntl(fd, F_GETFD);
586     if (flags == -1 || (flags & FD_CLOEXEC)) {
587         error_setg(errp, "fd is not valid or already in use");
588         return -1;
589     }
590
591     if (fdset_id < 0) {
592         error_setg(errp, "set option is required and must be non-negative");
593         return -1;
594     }
595
596 #ifdef F_DUPFD_CLOEXEC
597     dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
598 #else
599     dupfd = dup(fd);
600     if (dupfd != -1) {
601         qemu_set_cloexec(dupfd);
602     }
603 #endif
604     if (dupfd == -1) {
605         error_setg(errp, "error duplicating fd: %s", strerror(errno));
606         return -1;
607     }
608
609     /* add the duplicate fd, and optionally the opaque string, to the fd set */
610     fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
611                                   &error_abort);
612     g_free(fdinfo);
613
614     return 0;
615 }
616
617 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
618 {
619     int fd;
620
621     fd = qemu_opt_get_number(opts, "fd", -1);
622     close(fd);
623
624     return 0;
625 }
626 #endif
627
628 /***********************************************************/
629 /* QEMU Block devices */
630
631 #define HD_OPTS "media=disk"
632 #define CDROM_OPTS "media=cdrom"
633 #define FD_OPTS ""
634 #define PFLASH_OPTS ""
635 #define MTD_OPTS ""
636 #define SD_OPTS ""
637
638 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
639 {
640     BlockInterfaceType *block_default_type = opaque;
641
642     return drive_new(opts, *block_default_type, errp) == NULL;
643 }
644
645 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
646 {
647     if (qemu_opt_get(opts, "snapshot") == NULL) {
648         qemu_opt_set(opts, "snapshot", "on", &error_abort);
649     }
650     return 0;
651 }
652
653 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
654                           int index, const char *optstr)
655 {
656     QemuOpts *opts;
657     DriveInfo *dinfo;
658
659     if (!enable || drive_get_by_index(type, index)) {
660         return;
661     }
662
663     opts = drive_add(type, index, NULL, optstr);
664     if (snapshot) {
665         drive_enable_snapshot(NULL, opts, NULL);
666     }
667
668     dinfo = drive_new(opts, type, &error_abort);
669     dinfo->is_default = true;
670
671 }
672
673 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
674                                MachineClass *machine_class, int snapshot)
675 {
676     /*
677      * If the currently selected machine wishes to override the
678      * units-per-bus property of its default HBA interface type, do so
679      * now.
680      */
681     if (machine_class->units_per_default_bus) {
682         override_max_devs(machine_class->block_default_type,
683                           machine_class->units_per_default_bus);
684     }
685
686     /* open the virtual block devices */
687     while (!QSIMPLEQ_EMPTY(bdo_queue)) {
688         BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
689
690         QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
691         loc_push_restore(&bdo->loc);
692         qmp_blockdev_add(bdo->bdo, &error_fatal);
693         loc_pop(&bdo->loc);
694         qapi_free_BlockdevOptions(bdo->bdo);
695         g_free(bdo);
696     }
697     if (snapshot) {
698         qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
699                           NULL, NULL);
700     }
701     if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
702                           &machine_class->block_default_type, &error_fatal)) {
703         /* We printed help */
704         exit(0);
705     }
706
707     default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
708                   CDROM_OPTS);
709     default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
710     default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
711
712 }
713
714 static QemuOptsList qemu_smp_opts = {
715     .name = "smp-opts",
716     .implied_opt_name = "cpus",
717     .merge_lists = true,
718     .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
719     .desc = {
720         {
721             .name = "cpus",
722             .type = QEMU_OPT_NUMBER,
723         }, {
724             .name = "sockets",
725             .type = QEMU_OPT_NUMBER,
726         }, {
727             .name = "dies",
728             .type = QEMU_OPT_NUMBER,
729         }, {
730             .name = "clusters",
731             .type = QEMU_OPT_NUMBER,
732         }, {
733             .name = "cores",
734             .type = QEMU_OPT_NUMBER,
735         }, {
736             .name = "threads",
737             .type = QEMU_OPT_NUMBER,
738         }, {
739             .name = "maxcpus",
740             .type = QEMU_OPT_NUMBER,
741         },
742         { /*End of list */ }
743     },
744 };
745
746 static void realtime_init(void)
747 {
748     if (enable_mlock) {
749         if (os_mlock() < 0) {
750             error_report("locking memory failed");
751             exit(1);
752         }
753     }
754 }
755
756
757 static void configure_msg(QemuOpts *opts)
758 {
759     message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
760     error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
761 }
762
763
764 /***********************************************************/
765 /* USB devices */
766
767 static int usb_device_add(const char *devname)
768 {
769     USBDevice *dev = NULL;
770
771     if (!machine_usb(current_machine)) {
772         return -1;
773     }
774
775     dev = usbdevice_create(devname);
776     if (!dev)
777         return -1;
778
779     return 0;
780 }
781
782 static int usb_parse(const char *cmdline)
783 {
784     int r;
785     r = usb_device_add(cmdline);
786     if (r < 0) {
787         error_report("could not add USB device '%s'", cmdline);
788     }
789     return r;
790 }
791
792 /***********************************************************/
793 /* machine registration */
794
795 static MachineClass *find_machine(const char *name, GSList *machines)
796 {
797     GSList *el;
798
799     for (el = machines; el; el = el->next) {
800         MachineClass *mc = el->data;
801
802         if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
803             return mc;
804         }
805     }
806
807     return NULL;
808 }
809
810 static MachineClass *find_default_machine(GSList *machines)
811 {
812     GSList *el;
813     MachineClass *default_machineclass = NULL;
814
815     for (el = machines; el; el = el->next) {
816         MachineClass *mc = el->data;
817
818         if (mc->is_default) {
819             assert(default_machineclass == NULL && "Multiple default machines");
820             default_machineclass = mc;
821         }
822     }
823
824     return default_machineclass;
825 }
826
827 static void version(void)
828 {
829     printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
830            QEMU_COPYRIGHT "\n");
831 }
832
833 static void help(int exitcode)
834 {
835     version();
836     printf("usage: %s [options] [disk_image]\n\n"
837            "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
838             error_get_progname());
839
840 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)    \
841     if ((arch_mask) & arch_type)                               \
842         fputs(opt_help, stdout);
843
844 #define ARCHHEADING(text, arch_mask) \
845     if ((arch_mask) & arch_type)    \
846         puts(stringify(text));
847
848 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
849
850 #include "qemu-options.def"
851
852     printf("\nDuring emulation, the following keys are useful:\n"
853            "ctrl-alt-f      toggle full screen\n"
854            "ctrl-alt-n      switch to virtual console 'n'\n"
855            "ctrl-alt        toggle mouse and keyboard grab\n"
856            "\n"
857            "When using -nographic, press 'ctrl-a h' to get some help.\n"
858            "\n"
859            QEMU_HELP_BOTTOM "\n");
860
861     exit(exitcode);
862 }
863
864 #define HAS_ARG 0x0001
865
866 typedef struct QEMUOption {
867     const char *name;
868     int flags;
869     int index;
870     uint32_t arch_mask;
871 } QEMUOption;
872
873 static const QEMUOption qemu_options[] = {
874     { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
875
876 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
877     { option, opt_arg, opt_enum, arch_mask },
878 #define DEFHEADING(text)
879 #define ARCHHEADING(text, arch_mask)
880
881 #include "qemu-options.def"
882     { NULL },
883 };
884
885 typedef struct VGAInterfaceInfo {
886     const char *opt_name;    /* option name */
887     const char *name;        /* human-readable name */
888     /* Class names indicating that support is available.
889      * If no class is specified, the interface is always available */
890     const char *class_names[2];
891 } VGAInterfaceInfo;
892
893 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
894     [VGA_NONE] = {
895         .opt_name = "none",
896         .name = "no graphic card",
897     },
898     [VGA_STD] = {
899         .opt_name = "std",
900         .name = "standard VGA",
901         .class_names = { "VGA", "isa-vga" },
902     },
903     [VGA_CIRRUS] = {
904         .opt_name = "cirrus",
905         .name = "Cirrus VGA",
906         .class_names = { "cirrus-vga", "isa-cirrus-vga" },
907     },
908     [VGA_VMWARE] = {
909         .opt_name = "vmware",
910         .name = "VMWare SVGA",
911         .class_names = { "vmware-svga" },
912     },
913     [VGA_VIRTIO] = {
914         .opt_name = "virtio",
915         .name = "Virtio VGA",
916         .class_names = { "virtio-vga" },
917     },
918     [VGA_QXL] = {
919         .opt_name = "qxl",
920         .name = "QXL VGA",
921         .class_names = { "qxl-vga" },
922     },
923     [VGA_TCX] = {
924         .opt_name = "tcx",
925         .name = "TCX framebuffer",
926         .class_names = { "sun-tcx" },
927     },
928     [VGA_CG3] = {
929         .opt_name = "cg3",
930         .name = "CG3 framebuffer",
931         .class_names = { "cgthree" },
932     },
933     [VGA_XENFB] = {
934         .opt_name = "xenfb",
935         .name = "Xen paravirtualized framebuffer",
936     },
937 };
938
939 static bool vga_interface_available(VGAInterfaceType t)
940 {
941     const VGAInterfaceInfo *ti = &vga_interfaces[t];
942
943     assert(t < VGA_TYPE_MAX);
944     return !ti->class_names[0] ||
945            module_object_class_by_name(ti->class_names[0]) ||
946            module_object_class_by_name(ti->class_names[1]);
947 }
948
949 static const char *
950 get_default_vga_model(const MachineClass *machine_class)
951 {
952     if (machine_class->default_display) {
953         return machine_class->default_display;
954     } else if (vga_interface_available(VGA_CIRRUS)) {
955         return "cirrus";
956     } else if (vga_interface_available(VGA_STD)) {
957         return "std";
958     }
959
960     return NULL;
961 }
962
963 static void select_vgahw(const MachineClass *machine_class, const char *p)
964 {
965     const char *opts;
966     int t;
967
968     if (g_str_equal(p, "help")) {
969         const char *def = get_default_vga_model(machine_class);
970
971         for (t = 0; t < VGA_TYPE_MAX; t++) {
972             const VGAInterfaceInfo *ti = &vga_interfaces[t];
973
974             if (vga_interface_available(t) && ti->opt_name) {
975                 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
976                        g_str_equal(ti->opt_name, def) ? " (default)" : "");
977             }
978         }
979         exit(0);
980     }
981
982     assert(vga_interface_type == VGA_NONE);
983     for (t = 0; t < VGA_TYPE_MAX; t++) {
984         const VGAInterfaceInfo *ti = &vga_interfaces[t];
985         if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
986             if (!vga_interface_available(t)) {
987                 error_report("%s not available", ti->name);
988                 exit(1);
989             }
990             vga_interface_type = t;
991             break;
992         }
993     }
994     if (t == VGA_TYPE_MAX) {
995     invalid_vga:
996         error_report("unknown vga type: %s", p);
997         exit(1);
998     }
999     while (*opts) {
1000         const char *nextopt;
1001
1002         if (strstart(opts, ",retrace=", &nextopt)) {
1003             opts = nextopt;
1004             if (strstart(opts, "dumb", &nextopt))
1005                 vga_retrace_method = VGA_RETRACE_DUMB;
1006             else if (strstart(opts, "precise", &nextopt))
1007                 vga_retrace_method = VGA_RETRACE_PRECISE;
1008             else goto invalid_vga;
1009         } else goto invalid_vga;
1010         opts = nextopt;
1011     }
1012 }
1013
1014 static void parse_display_qapi(const char *optarg)
1015 {
1016     DisplayOptions *opts;
1017     Visitor *v;
1018
1019     v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1020
1021     visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1022     QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1023
1024     qapi_free_DisplayOptions(opts);
1025     visit_free(v);
1026 }
1027
1028 DisplayOptions *qmp_query_display_options(Error **errp)
1029 {
1030     return QAPI_CLONE(DisplayOptions, &dpy);
1031 }
1032
1033 static void parse_display(const char *p)
1034 {
1035     const char *opts;
1036
1037     if (is_help_option(p)) {
1038         qemu_display_help();
1039         exit(0);
1040     }
1041
1042     if (strstart(p, "sdl", &opts)) {
1043         /*
1044          * sdl DisplayType needs hand-crafted parser instead of
1045          * parse_display_qapi() due to some options not in
1046          * DisplayOptions, specifically:
1047          *   - ctrl_grab + alt_grab
1048          *     They can't be moved into the QAPI since they use underscores,
1049          *     thus they will get replaced by "grab-mod" in the long term
1050          */
1051 #if defined(CONFIG_SDL)
1052         dpy.type = DISPLAY_TYPE_SDL;
1053         while (*opts) {
1054             const char *nextopt;
1055
1056             if (strstart(opts, ",grab-mod=", &nextopt)) {
1057                 opts = nextopt;
1058                 if (strstart(opts, "lshift-lctrl-lalt", &nextopt)) {
1059                     alt_grab = 1;
1060                 } else if (strstart(opts, "rctrl", &nextopt)) {
1061                     ctrl_grab = 1;
1062                 } else {
1063                     goto invalid_sdl_args;
1064                 }
1065             } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1066                 opts = nextopt;
1067                 if (strstart(opts, "on", &nextopt)) {
1068                     alt_grab = 1;
1069                 } else if (strstart(opts, "off", &nextopt)) {
1070                     alt_grab = 0;
1071                 } else {
1072                     goto invalid_sdl_args;
1073                 }
1074                 warn_report("alt_grab is deprecated, use grab-mod instead.");
1075             } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1076                 opts = nextopt;
1077                 if (strstart(opts, "on", &nextopt)) {
1078                     ctrl_grab = 1;
1079                 } else if (strstart(opts, "off", &nextopt)) {
1080                     ctrl_grab = 0;
1081                 } else {
1082                     goto invalid_sdl_args;
1083                 }
1084                 warn_report("ctrl_grab is deprecated, use grab-mod instead.");
1085             } else if (strstart(opts, ",window_close=", &nextopt) ||
1086                        strstart(opts, ",window-close=", &nextopt)) {
1087                 if (strstart(opts, ",window_close=", NULL)) {
1088                     warn_report("window_close with an underscore is deprecated,"
1089                                 " please use window-close instead.");
1090                 }
1091                 opts = nextopt;
1092                 dpy.has_window_close = true;
1093                 if (strstart(opts, "on", &nextopt)) {
1094                     dpy.window_close = true;
1095                 } else if (strstart(opts, "off", &nextopt)) {
1096                     dpy.window_close = false;
1097                 } else {
1098                     goto invalid_sdl_args;
1099                 }
1100             } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1101                 opts = nextopt;
1102                 dpy.has_show_cursor = true;
1103                 if (strstart(opts, "on", &nextopt)) {
1104                     dpy.show_cursor = true;
1105                 } else if (strstart(opts, "off", &nextopt)) {
1106                     dpy.show_cursor = false;
1107                 } else {
1108                     goto invalid_sdl_args;
1109                 }
1110             } else if (strstart(opts, ",gl=", &nextopt)) {
1111                 opts = nextopt;
1112                 dpy.has_gl = true;
1113                 if (strstart(opts, "on", &nextopt)) {
1114                     dpy.gl = DISPLAYGL_MODE_ON;
1115                 } else if (strstart(opts, "core", &nextopt)) {
1116                     dpy.gl = DISPLAYGL_MODE_CORE;
1117                 } else if (strstart(opts, "es", &nextopt)) {
1118                     dpy.gl = DISPLAYGL_MODE_ES;
1119                 } else if (strstart(opts, "off", &nextopt)) {
1120                     dpy.gl = DISPLAYGL_MODE_OFF;
1121                 } else {
1122                     goto invalid_sdl_args;
1123                 }
1124             } else {
1125             invalid_sdl_args:
1126                 error_report("invalid SDL option string");
1127                 exit(1);
1128             }
1129             opts = nextopt;
1130         }
1131 #else
1132         error_report("SDL display supported is not available in this binary");
1133         exit(1);
1134 #endif
1135     } else if (strstart(p, "vnc", &opts)) {
1136         /*
1137          * vnc isn't a (local) DisplayType but a protocol for remote
1138          * display access.
1139          */
1140         if (*opts == '=') {
1141             vnc_parse(opts + 1);
1142         } else {
1143             error_report("VNC requires a display argument vnc=<display>");
1144             exit(1);
1145         }
1146     } else {
1147         parse_display_qapi(p);
1148     }
1149 }
1150
1151 static inline bool nonempty_str(const char *str)
1152 {
1153     return str && *str;
1154 }
1155
1156 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1157 {
1158     gchar *buf;
1159     size_t size;
1160     const char *name, *file, *str, *gen_id;
1161     FWCfgState *fw_cfg = (FWCfgState *) opaque;
1162
1163     if (fw_cfg == NULL) {
1164         error_setg(errp, "fw_cfg device not available");
1165         return -1;
1166     }
1167     name = qemu_opt_get(opts, "name");
1168     file = qemu_opt_get(opts, "file");
1169     str = qemu_opt_get(opts, "string");
1170     gen_id = qemu_opt_get(opts, "gen_id");
1171
1172     /* we need the name, and exactly one of: file, content string, gen_id */
1173     if (!nonempty_str(name) ||
1174         nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1175         error_setg(errp, "name, plus exactly one of file,"
1176                          " string and gen_id, are needed");
1177         return -1;
1178     }
1179     if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1180         error_setg(errp, "name too long (max. %d char)",
1181                    FW_CFG_MAX_FILE_PATH - 1);
1182         return -1;
1183     }
1184     if (nonempty_str(gen_id)) {
1185         /*
1186          * In this particular case where the content is populated
1187          * internally, the "etc/" namespace protection is relaxed,
1188          * so do not emit a warning.
1189          */
1190     } else if (strncmp(name, "opt/", 4) != 0) {
1191         warn_report("externally provided fw_cfg item names "
1192                     "should be prefixed with \"opt/\"");
1193     }
1194     if (nonempty_str(str)) {
1195         size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1196         buf = g_memdup(str, size);
1197     } else if (nonempty_str(gen_id)) {
1198         if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1199             return -1;
1200         }
1201         return 0;
1202     } else {
1203         GError *err = NULL;
1204         if (!g_file_get_contents(file, &buf, &size, &err)) {
1205             error_setg(errp, "can't load %s: %s", file, err->message);
1206             g_error_free(err);
1207             return -1;
1208         }
1209     }
1210     /* For legacy, keep user files in a specific global order. */
1211     fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1212     fw_cfg_add_file(fw_cfg, name, buf, size);
1213     fw_cfg_reset_order_override(fw_cfg);
1214     return 0;
1215 }
1216
1217 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1218 {
1219     return qdev_device_help(opts);
1220 }
1221
1222 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1223 {
1224     DeviceState *dev;
1225
1226     dev = qdev_device_add(opts, errp);
1227     if (!dev && *errp) {
1228         error_report_err(*errp);
1229         return -1;
1230     } else if (dev) {
1231         object_unref(OBJECT(dev));
1232     }
1233     return 0;
1234 }
1235
1236 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1237 {
1238     Error *local_err = NULL;
1239
1240     if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1241         if (local_err) {
1242             error_propagate(errp, local_err);
1243             return -1;
1244         }
1245         exit(0);
1246     }
1247     return 0;
1248 }
1249
1250 #ifdef CONFIG_VIRTFS
1251 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1252 {
1253     return qemu_fsdev_add(opts, errp);
1254 }
1255 #endif
1256
1257 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1258 {
1259     return monitor_init_opts(opts, errp);
1260 }
1261
1262 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1263 {
1264     static int monitor_device_index = 0;
1265     QemuOpts *opts;
1266     const char *p;
1267     char label[32];
1268
1269     if (strstart(optarg, "chardev:", &p)) {
1270         snprintf(label, sizeof(label), "%s", p);
1271     } else {
1272         snprintf(label, sizeof(label), "compat_monitor%d",
1273                  monitor_device_index);
1274         opts = qemu_chr_parse_compat(label, optarg, true);
1275         if (!opts) {
1276             error_report("parse error: %s", optarg);
1277             exit(1);
1278         }
1279     }
1280
1281     opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1282     qemu_opt_set(opts, "mode", mode, &error_abort);
1283     qemu_opt_set(opts, "chardev", label, &error_abort);
1284     if (!strcmp(mode, "control")) {
1285         qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1286     } else {
1287         assert(pretty == false);
1288     }
1289     monitor_device_index++;
1290 }
1291
1292 struct device_config {
1293     enum {
1294         DEV_USB,       /* -usbdevice     */
1295         DEV_SERIAL,    /* -serial        */
1296         DEV_PARALLEL,  /* -parallel      */
1297         DEV_DEBUGCON,  /* -debugcon */
1298         DEV_GDB,       /* -gdb, -s */
1299         DEV_SCLP,      /* s390 sclp */
1300     } type;
1301     const char *cmdline;
1302     Location loc;
1303     QTAILQ_ENTRY(device_config) next;
1304 };
1305
1306 static QTAILQ_HEAD(, device_config) device_configs =
1307     QTAILQ_HEAD_INITIALIZER(device_configs);
1308
1309 static void add_device_config(int type, const char *cmdline)
1310 {
1311     struct device_config *conf;
1312
1313     conf = g_malloc0(sizeof(*conf));
1314     conf->type = type;
1315     conf->cmdline = cmdline;
1316     loc_save(&conf->loc);
1317     QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1318 }
1319
1320 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1321 {
1322     struct device_config *conf;
1323     int rc;
1324
1325     QTAILQ_FOREACH(conf, &device_configs, next) {
1326         if (conf->type != type)
1327             continue;
1328         loc_push_restore(&conf->loc);
1329         rc = func(conf->cmdline);
1330         loc_pop(&conf->loc);
1331         if (rc) {
1332             return rc;
1333         }
1334     }
1335     return 0;
1336 }
1337
1338 static void qemu_disable_default_devices(void)
1339 {
1340     MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1341
1342     default_driver_check_json();
1343     qemu_opts_foreach(qemu_find_opts("device"),
1344                       default_driver_check, NULL, NULL);
1345     qemu_opts_foreach(qemu_find_opts("global"),
1346                       default_driver_check, NULL, NULL);
1347
1348     if (!vga_model && !default_vga) {
1349         vga_interface_type = VGA_DEVICE;
1350     }
1351     if (!has_defaults || machine_class->no_serial) {
1352         default_serial = 0;
1353     }
1354     if (!has_defaults || machine_class->no_parallel) {
1355         default_parallel = 0;
1356     }
1357     if (!has_defaults || machine_class->no_floppy) {
1358         default_floppy = 0;
1359     }
1360     if (!has_defaults || machine_class->no_cdrom) {
1361         default_cdrom = 0;
1362     }
1363     if (!has_defaults || machine_class->no_sdcard) {
1364         default_sdcard = 0;
1365     }
1366     if (!has_defaults) {
1367         default_monitor = 0;
1368         default_net = 0;
1369         default_vga = 0;
1370     }
1371 }
1372
1373 static void qemu_create_default_devices(void)
1374 {
1375     MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1376
1377     if (is_daemonized()) {
1378         /* According to documentation and historically, -nographic redirects
1379          * serial port, parallel port and monitor to stdio, which does not work
1380          * with -daemonize.  We can redirect these to null instead, but since
1381          * -nographic is legacy, let's just error out.
1382          * We disallow -nographic only if all other ports are not redirected
1383          * explicitly, to not break existing legacy setups which uses
1384          * -nographic _and_ redirects all ports explicitly - this is valid
1385          * usage, -nographic is just a no-op in this case.
1386          */
1387         if (nographic
1388             && (default_parallel || default_serial || default_monitor)) {
1389             error_report("-nographic cannot be used with -daemonize");
1390             exit(1);
1391         }
1392     }
1393
1394     if (nographic) {
1395         if (default_parallel)
1396             add_device_config(DEV_PARALLEL, "null");
1397         if (default_serial && default_monitor) {
1398             add_device_config(DEV_SERIAL, "mon:stdio");
1399         } else {
1400             if (default_serial)
1401                 add_device_config(DEV_SERIAL, "stdio");
1402             if (default_monitor)
1403                 monitor_parse("stdio", "readline", false);
1404         }
1405     } else {
1406         if (default_serial)
1407             add_device_config(DEV_SERIAL, "vc:80Cx24C");
1408         if (default_parallel)
1409             add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1410         if (default_monitor)
1411             monitor_parse("vc:80Cx24C", "readline", false);
1412     }
1413
1414     if (default_net) {
1415         QemuOptsList *net = qemu_find_opts("net");
1416         qemu_opts_parse(net, "nic", true, &error_abort);
1417 #ifdef CONFIG_SLIRP
1418         qemu_opts_parse(net, "user", true, &error_abort);
1419 #endif
1420     }
1421
1422 #if defined(CONFIG_VNC)
1423     if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1424         display_remote++;
1425     }
1426 #endif
1427     if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1428         if (!qemu_display_find_default(&dpy)) {
1429             dpy.type = DISPLAY_TYPE_NONE;
1430 #if defined(CONFIG_VNC)
1431             vnc_parse("localhost:0,to=99,id=default");
1432 #endif
1433         }
1434     }
1435     if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1436         dpy.type = DISPLAY_TYPE_NONE;
1437     }
1438
1439     /* If no default VGA is requested, the default is "none".  */
1440     if (default_vga) {
1441         vga_model = get_default_vga_model(machine_class);
1442     }
1443     if (vga_model) {
1444         select_vgahw(machine_class, vga_model);
1445     }
1446 }
1447
1448 static int serial_parse(const char *devname)
1449 {
1450     int index = num_serial_hds;
1451     char label[32];
1452
1453     if (strcmp(devname, "none") == 0)
1454         return 0;
1455     snprintf(label, sizeof(label), "serial%d", index);
1456     serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1457
1458     serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1459     if (!serial_hds[index]) {
1460         error_report("could not connect serial device"
1461                      " to character backend '%s'", devname);
1462         return -1;
1463     }
1464     num_serial_hds++;
1465     return 0;
1466 }
1467
1468 Chardev *serial_hd(int i)
1469 {
1470     assert(i >= 0);
1471     if (i < num_serial_hds) {
1472         return serial_hds[i];
1473     }
1474     return NULL;
1475 }
1476
1477 static int parallel_parse(const char *devname)
1478 {
1479     static int index = 0;
1480     char label[32];
1481
1482     if (strcmp(devname, "none") == 0)
1483         return 0;
1484     if (index == MAX_PARALLEL_PORTS) {
1485         error_report("too many parallel ports");
1486         exit(1);
1487     }
1488     snprintf(label, sizeof(label), "parallel%d", index);
1489     parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1490     if (!parallel_hds[index]) {
1491         error_report("could not connect parallel device"
1492                      " to character backend '%s'", devname);
1493         return -1;
1494     }
1495     index++;
1496     return 0;
1497 }
1498
1499 static int debugcon_parse(const char *devname)
1500 {
1501     QemuOpts *opts;
1502
1503     if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1504         error_report("invalid character backend '%s'", devname);
1505         exit(1);
1506     }
1507     opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1508     if (!opts) {
1509         error_report("already have a debugcon device");
1510         exit(1);
1511     }
1512     qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1513     qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1514     return 0;
1515 }
1516
1517 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1518 {
1519     const MachineClass *mc1 = a, *mc2 = b;
1520     int res;
1521
1522     if (mc1->family == NULL) {
1523         if (mc2->family == NULL) {
1524             /* Compare standalone machine types against each other; they sort
1525              * in increasing order.
1526              */
1527             return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1528                           object_class_get_name(OBJECT_CLASS(mc2)));
1529         }
1530
1531         /* Standalone machine types sort after families. */
1532         return 1;
1533     }
1534
1535     if (mc2->family == NULL) {
1536         /* Families sort before standalone machine types. */
1537         return -1;
1538     }
1539
1540     /* Families sort between each other alphabetically increasingly. */
1541     res = strcmp(mc1->family, mc2->family);
1542     if (res != 0) {
1543         return res;
1544     }
1545
1546     /* Within the same family, machine types sort in decreasing order. */
1547     return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1548                   object_class_get_name(OBJECT_CLASS(mc1)));
1549 }
1550
1551 static void machine_help_func(const QDict *qdict)
1552 {
1553     GSList *machines, *el;
1554     const char *type = qdict_get_try_str(qdict, "type");
1555
1556     machines = object_class_get_list(TYPE_MACHINE, false);
1557     if (type) {
1558         ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1559         if (machine_class) {
1560             type_print_class_properties(object_class_get_name(machine_class));
1561             return;
1562         }
1563     }
1564
1565     printf("Supported machines are:\n");
1566     machines = g_slist_sort(machines, machine_class_cmp);
1567     for (el = machines; el; el = el->next) {
1568         MachineClass *mc = el->data;
1569         if (mc->alias) {
1570             printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1571         }
1572         printf("%-20s %s%s%s\n", mc->name, mc->desc,
1573                mc->is_default ? " (default)" : "",
1574                mc->deprecation_reason ? " (deprecated)" : "");
1575     }
1576 }
1577
1578 static void
1579 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1580 {
1581     QDict *opts;
1582
1583     opts = qdict_new();
1584     /* Preserve the caller's reference to prop.  */
1585     qobject_ref(prop);
1586     qdict_put(opts, propname, prop);
1587     keyval_merge(machine_opts_dict, opts, errp);
1588     qobject_unref(opts);
1589 }
1590
1591 static void
1592 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1593                            const char *arg)
1594 {
1595     QDict *prop = NULL;
1596     bool help = false;
1597
1598     prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
1599     if (help) {
1600         qemu_opts_print_help(opts_list, true);
1601         exit(0);
1602     }
1603     machine_merge_property(propname, prop, &error_fatal);
1604     qobject_unref(prop);
1605 }
1606
1607 static const char *pid_file;
1608 static Notifier qemu_unlink_pidfile_notifier;
1609
1610 static void qemu_unlink_pidfile(Notifier *n, void *data)
1611 {
1612     if (pid_file) {
1613         unlink(pid_file);
1614     }
1615 }
1616
1617 static const QEMUOption *lookup_opt(int argc, char **argv,
1618                                     const char **poptarg, int *poptind)
1619 {
1620     const QEMUOption *popt;
1621     int optind = *poptind;
1622     char *r = argv[optind];
1623     const char *optarg;
1624
1625     loc_set_cmdline(argv, optind, 1);
1626     optind++;
1627     /* Treat --foo the same as -foo.  */
1628     if (r[1] == '-')
1629         r++;
1630     popt = qemu_options;
1631     for(;;) {
1632         if (!popt->name) {
1633             error_report("invalid option");
1634             exit(1);
1635         }
1636         if (!strcmp(popt->name, r + 1))
1637             break;
1638         popt++;
1639     }
1640     if (popt->flags & HAS_ARG) {
1641         if (optind >= argc) {
1642             error_report("requires an argument");
1643             exit(1);
1644         }
1645         optarg = argv[optind++];
1646         loc_set_cmdline(argv, optind - 2, 2);
1647     } else {
1648         optarg = NULL;
1649     }
1650
1651     *poptarg = optarg;
1652     *poptind = optind;
1653
1654     return popt;
1655 }
1656
1657 static MachineClass *select_machine(QDict *qdict, Error **errp)
1658 {
1659     const char *optarg = qdict_get_try_str(qdict, "type");
1660     GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1661     MachineClass *machine_class;
1662     Error *local_err = NULL;
1663
1664     if (optarg) {
1665         machine_class = find_machine(optarg, machines);
1666         qdict_del(qdict, "type");
1667         if (!machine_class) {
1668             error_setg(&local_err, "unsupported machine type");
1669         }
1670     } else {
1671         machine_class = find_default_machine(machines);
1672         if (!machine_class) {
1673             error_setg(&local_err, "No machine specified, and there is no default");
1674         }
1675     }
1676
1677     g_slist_free(machines);
1678     if (local_err) {
1679         error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1680         error_propagate(errp, local_err);
1681     }
1682     return machine_class;
1683 }
1684
1685 static int object_parse_property_opt(Object *obj,
1686                                      const char *name, const char *value,
1687                                      const char *skip, Error **errp)
1688 {
1689     if (g_str_equal(name, skip)) {
1690         return 0;
1691     }
1692
1693     if (!object_property_parse(obj, name, value, errp)) {
1694         return -1;
1695     }
1696
1697     return 0;
1698 }
1699
1700 /* *Non*recursively replace underscores with dashes in QDict keys.  */
1701 static void keyval_dashify(QDict *qdict, Error **errp)
1702 {
1703     const QDictEntry *ent, *next;
1704     char *p;
1705
1706     for (ent = qdict_first(qdict); ent; ent = next) {
1707         g_autofree char *new_key = NULL;
1708
1709         next = qdict_next(qdict, ent);
1710         if (!strchr(ent->key, '_')) {
1711             continue;
1712         }
1713         new_key = g_strdup(ent->key);
1714         for (p = new_key; *p; p++) {
1715             if (*p == '_') {
1716                 *p = '-';
1717             }
1718         }
1719         if (qdict_haskey(qdict, new_key)) {
1720             error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1721             return;
1722         }
1723         qobject_ref(ent->value);
1724         qdict_put_obj(qdict, new_key, ent->value);
1725         qdict_del(qdict, ent->key);
1726     }
1727 }
1728
1729 static void qemu_apply_legacy_machine_options(QDict *qdict)
1730 {
1731     const char *value;
1732
1733     keyval_dashify(qdict, &error_fatal);
1734
1735     /* Legacy options do not correspond to MachineState properties.  */
1736     value = qdict_get_try_str(qdict, "accel");
1737     if (value) {
1738         accelerators = g_strdup(value);
1739         qdict_del(qdict, "accel");
1740     }
1741
1742     value = qdict_get_try_str(qdict, "igd-passthru");
1743     if (value) {
1744         object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1745                                    false);
1746         qdict_del(qdict, "igd-passthru");
1747     }
1748
1749     value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1750     if (value) {
1751         object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1752                                    false);
1753         qdict_del(qdict, "kvm-shadow-mem");
1754     }
1755
1756     value = qdict_get_try_str(qdict, "kernel-irqchip");
1757     if (value) {
1758         object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1759                                    false);
1760         object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1761                                    false);
1762         qdict_del(qdict, "kernel-irqchip");
1763     }
1764 }
1765
1766 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1767 {
1768     ObjectOption *opt, *next;
1769
1770     QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1771         const char *type = ObjectType_str(opt->opts->qom_type);
1772         if (type_opt_predicate(type)) {
1773             user_creatable_add_qapi(opt->opts, &error_fatal);
1774             qapi_free_ObjectOptions(opt->opts);
1775             QTAILQ_REMOVE(&object_opts, opt, next);
1776             g_free(opt);
1777         }
1778     }
1779 }
1780
1781 static void object_option_add_visitor(Visitor *v)
1782 {
1783     ObjectOption *opt = g_new0(ObjectOption, 1);
1784     visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1785     QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1786 }
1787
1788 static void object_option_parse(const char *optarg)
1789 {
1790     QemuOpts *opts;
1791     const char *type;
1792     Visitor *v;
1793
1794     if (optarg[0] == '{') {
1795         QObject *obj = qobject_from_json(optarg, &error_fatal);
1796
1797         v = qobject_input_visitor_new(obj);
1798         qobject_unref(obj);
1799     } else {
1800         opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1801                                        optarg, true);
1802         if (!opts) {
1803             exit(1);
1804         }
1805
1806         type = qemu_opt_get(opts, "qom-type");
1807         if (!type) {
1808             error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1809         }
1810         if (user_creatable_print_help(type, opts)) {
1811             exit(0);
1812         }
1813
1814         v = opts_visitor_new(opts);
1815     }
1816
1817     object_option_add_visitor(v);
1818     visit_free(v);
1819 }
1820
1821 /*
1822  * Initial object creation happens before all other
1823  * QEMU data types are created. The majority of objects
1824  * can be created at this point. The rng-egd object
1825  * cannot be created here, as it depends on the chardev
1826  * already existing.
1827  */
1828 static bool object_create_early(const char *type)
1829 {
1830     /*
1831      * Objects should not be made "delayed" without a reason.  If you
1832      * add one, state the reason in a comment!
1833      */
1834
1835     /* Reason: property "chardev" */
1836     if (g_str_equal(type, "rng-egd") ||
1837         g_str_equal(type, "qtest")) {
1838         return false;
1839     }
1840
1841 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1842     /* Reason: cryptodev-vhost-user property "chardev" */
1843     if (g_str_equal(type, "cryptodev-vhost-user")) {
1844         return false;
1845     }
1846 #endif
1847
1848     /* Reason: vhost-user-blk-server property "node-name" */
1849     if (g_str_equal(type, "vhost-user-blk-server")) {
1850         return false;
1851     }
1852     /*
1853      * Reason: filter-* property "netdev" etc.
1854      */
1855     if (g_str_equal(type, "filter-buffer") ||
1856         g_str_equal(type, "filter-dump") ||
1857         g_str_equal(type, "filter-mirror") ||
1858         g_str_equal(type, "filter-redirector") ||
1859         g_str_equal(type, "colo-compare") ||
1860         g_str_equal(type, "filter-rewriter") ||
1861         g_str_equal(type, "filter-replay")) {
1862         return false;
1863     }
1864
1865     /*
1866      * Allocation of large amounts of memory may delay
1867      * chardev initialization for too long, and trigger timeouts
1868      * on software that waits for a monitor socket to be created
1869      * (e.g. libvirt).
1870      */
1871     if (g_str_has_prefix(type, "memory-backend-")) {
1872         return false;
1873     }
1874
1875     return true;
1876 }
1877
1878 static void qemu_apply_machine_options(QDict *qdict)
1879 {
1880     MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1881     const char *boot_order = NULL;
1882     const char *boot_once = NULL;
1883     QemuOpts *opts;
1884
1885     object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1886     current_machine->ram_size = ram_size;
1887     current_machine->maxram_size = maxram_size;
1888     current_machine->ram_slots = ram_slots;
1889
1890     opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1891     if (opts) {
1892         boot_order = qemu_opt_get(opts, "order");
1893         if (boot_order) {
1894             validate_bootdevices(boot_order, &error_fatal);
1895         }
1896
1897         boot_once = qemu_opt_get(opts, "once");
1898         if (boot_once) {
1899             validate_bootdevices(boot_once, &error_fatal);
1900         }
1901
1902         boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1903         boot_strict = qemu_opt_get_bool(opts, "strict", false);
1904     }
1905
1906     if (!boot_order) {
1907         boot_order = machine_class->default_boot_order;
1908     }
1909
1910     current_machine->boot_order = boot_order;
1911     current_machine->boot_once = boot_once;
1912
1913     if (semihosting_enabled() && !semihosting_get_argc()) {
1914         /* fall back to the -kernel/-append */
1915         semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1916     }
1917
1918     if (current_machine->smp.cpus > 1) {
1919         Error *blocker = NULL;
1920         error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1921         replay_add_blocker(blocker);
1922     }
1923 }
1924
1925 static void qemu_create_early_backends(void)
1926 {
1927     MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1928 #if defined(CONFIG_SDL)
1929     const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1930 #else
1931     const bool use_sdl = false;
1932 #endif
1933 #if defined(CONFIG_GTK)
1934     const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1935 #else
1936     const bool use_gtk = false;
1937 #endif
1938
1939     if ((alt_grab || ctrl_grab) && !use_sdl) {
1940         error_report("-alt-grab and -ctrl-grab are only valid "
1941                      "for SDL, ignoring option");
1942     }
1943     if (dpy.has_window_close && !use_gtk && !use_sdl) {
1944         error_report("window-close is only valid for GTK and SDL, "
1945                      "ignoring option");
1946     }
1947
1948     qemu_display_early_init(&dpy);
1949     qemu_console_early_init();
1950
1951     if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1952 #if defined(CONFIG_OPENGL)
1953         error_report("OpenGL is not supported by the display");
1954 #else
1955         error_report("OpenGL support is disabled");
1956 #endif
1957         exit(1);
1958     }
1959
1960     object_option_foreach_add(object_create_early);
1961
1962     /* spice needs the timers to be initialized by this point */
1963     /* spice must initialize before audio as it changes the default auiodev */
1964     /* spice must initialize before chardevs (for spicevmc and spiceport) */
1965     qemu_spice.init();
1966
1967     qemu_opts_foreach(qemu_find_opts("chardev"),
1968                       chardev_init_func, NULL, &error_fatal);
1969
1970 #ifdef CONFIG_VIRTFS
1971     qemu_opts_foreach(qemu_find_opts("fsdev"),
1972                       fsdev_init_func, NULL, &error_fatal);
1973 #endif
1974
1975     /*
1976      * Note: we need to create audio and block backends before
1977      * setting machine properties, so they can be referred to.
1978      */
1979     configure_blockdev(&bdo_queue, machine_class, snapshot);
1980     audio_init_audiodevs();
1981 }
1982
1983
1984 /*
1985  * The remainder of object creation happens after the
1986  * creation of chardev, fsdev, net clients and device data types.
1987  */
1988 static bool object_create_late(const char *type)
1989 {
1990     return !object_create_early(type);
1991 }
1992
1993 static void qemu_create_late_backends(void)
1994 {
1995     if (qtest_chrdev) {
1996         qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1997     }
1998
1999     net_init_clients(&error_fatal);
2000
2001     object_option_foreach_add(object_create_late);
2002
2003     if (tpm_init() < 0) {
2004         exit(1);
2005     }
2006
2007     qemu_opts_foreach(qemu_find_opts("mon"),
2008                       mon_init_func, NULL, &error_fatal);
2009
2010     if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2011         exit(1);
2012     if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2013         exit(1);
2014     if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2015         exit(1);
2016
2017     /* now chardevs have been created we may have semihosting to connect */
2018     qemu_semihosting_connect_chardevs();
2019     qemu_semihosting_console_init();
2020 }
2021
2022 static bool have_custom_ram_size(void)
2023 {
2024     QemuOpts *opts = qemu_find_opts_singleton("memory");
2025     return !!qemu_opt_get_size(opts, "size", 0);
2026 }
2027
2028 static void qemu_resolve_machine_memdev(void)
2029 {
2030     if (current_machine->ram_memdev_id) {
2031         Object *backend;
2032         ram_addr_t backend_size;
2033
2034         backend = object_resolve_path_type(current_machine->ram_memdev_id,
2035                                            TYPE_MEMORY_BACKEND, NULL);
2036         if (!backend) {
2037             error_report("Memory backend '%s' not found",
2038                          current_machine->ram_memdev_id);
2039             exit(EXIT_FAILURE);
2040         }
2041         backend_size = object_property_get_uint(backend, "size",  &error_abort);
2042         if (have_custom_ram_size() && backend_size != ram_size) {
2043                 error_report("Size specified by -m option must match size of "
2044                              "explicitly specified 'memory-backend' property");
2045                 exit(EXIT_FAILURE);
2046         }
2047         if (mem_path) {
2048             error_report("'-mem-path' can't be used together with"
2049                          "'-machine memory-backend'");
2050             exit(EXIT_FAILURE);
2051         }
2052         ram_size = backend_size;
2053     }
2054
2055     if (!xen_enabled()) {
2056         /* On 32-bit hosts, QEMU is limited by virtual address space */
2057         if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
2058             error_report("at most 2047 MB RAM can be simulated");
2059             exit(1);
2060         }
2061     }
2062 }
2063
2064 static void set_memory_options(MachineClass *mc)
2065 {
2066     uint64_t sz;
2067     const char *mem_str;
2068     const ram_addr_t default_ram_size = mc->default_ram_size;
2069     QemuOpts *opts = qemu_find_opts_singleton("memory");
2070     Location loc;
2071
2072     loc_push_none(&loc);
2073     qemu_opts_loc_restore(opts);
2074
2075     sz = 0;
2076     mem_str = qemu_opt_get(opts, "size");
2077     if (mem_str) {
2078         if (!*mem_str) {
2079             error_report("missing 'size' option value");
2080             exit(EXIT_FAILURE);
2081         }
2082
2083         sz = qemu_opt_get_size(opts, "size", ram_size);
2084
2085         /* Fix up legacy suffix-less format */
2086         if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2087             uint64_t overflow_check = sz;
2088
2089             sz *= MiB;
2090             if (sz / MiB != overflow_check) {
2091                 error_report("too large 'size' option value");
2092                 exit(EXIT_FAILURE);
2093             }
2094         }
2095     }
2096
2097     /* backward compatibility behaviour for case "-m 0" */
2098     if (sz == 0) {
2099         sz = default_ram_size;
2100     }
2101
2102     sz = QEMU_ALIGN_UP(sz, 8192);
2103     if (mc->fixup_ram_size) {
2104         sz = mc->fixup_ram_size(sz);
2105     }
2106     ram_size = sz;
2107     if (ram_size != sz) {
2108         error_report("ram size too large");
2109         exit(EXIT_FAILURE);
2110     }
2111
2112     maxram_size = ram_size;
2113
2114     if (qemu_opt_get(opts, "maxmem")) {
2115         uint64_t slots;
2116
2117         sz = qemu_opt_get_size(opts, "maxmem", 0);
2118         slots = qemu_opt_get_number(opts, "slots", 0);
2119         if (sz < ram_size) {
2120             error_report("invalid value of -m option maxmem: "
2121                          "maximum memory size (0x%" PRIx64 ") must be at least "
2122                          "the initial memory size (0x" RAM_ADDR_FMT ")",
2123                          sz, ram_size);
2124             exit(EXIT_FAILURE);
2125         } else if (slots && sz == ram_size) {
2126             error_report("invalid value of -m option maxmem: "
2127                          "memory slots were specified but maximum memory size "
2128                          "(0x%" PRIx64 ") is equal to the initial memory size "
2129                          "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2130             exit(EXIT_FAILURE);
2131         }
2132
2133         maxram_size = sz;
2134         ram_slots = slots;
2135     } else if (qemu_opt_get(opts, "slots")) {
2136         error_report("invalid -m option value: missing 'maxmem' option");
2137         exit(EXIT_FAILURE);
2138     }
2139
2140     loc_pop(&loc);
2141 }
2142
2143 static void qemu_create_machine(QDict *qdict)
2144 {
2145     MachineClass *machine_class = select_machine(qdict, &error_fatal);
2146     object_set_machine_compat_props(machine_class->compat_props);
2147
2148     set_memory_options(machine_class);
2149
2150     current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2151     object_property_add_child(object_get_root(), "machine",
2152                               OBJECT(current_machine));
2153     object_property_add_child(container_get(OBJECT(current_machine),
2154                                             "/unattached"),
2155                               "sysbus", OBJECT(sysbus_get_default()));
2156
2157     if (machine_class->minimum_page_bits) {
2158         if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2159             /* This would be a board error: specifying a minimum smaller than
2160              * a target's compile-time fixed setting.
2161              */
2162             g_assert_not_reached();
2163         }
2164     }
2165
2166     cpu_exec_init_all();
2167     page_size_init();
2168
2169     if (machine_class->hw_version) {
2170         qemu_set_hw_version(machine_class->hw_version);
2171     }
2172
2173     /*
2174      * Get the default machine options from the machine if it is not already
2175      * specified either by the configuration file or by the command line.
2176      */
2177     if (machine_class->default_machine_opts) {
2178         QDict *default_opts =
2179             keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2180                          &error_abort);
2181         qemu_apply_legacy_machine_options(default_opts);
2182         object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2183                                           false, &error_abort);
2184         qobject_unref(default_opts);
2185     }
2186 }
2187
2188 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2189 {
2190     GlobalProperty *g;
2191
2192     g = g_malloc0(sizeof(*g));
2193     g->driver   = qemu_opt_get(opts, "driver");
2194     g->property = qemu_opt_get(opts, "property");
2195     g->value    = qemu_opt_get(opts, "value");
2196     qdev_prop_register_global(g);
2197     return 0;
2198 }
2199
2200 /*
2201  * Return whether configuration group @group is stored in QemuOpts, or
2202  * recorded as one or more QDicts by qemu_record_config_group.
2203  */
2204 static bool is_qemuopts_group(const char *group)
2205 {
2206     if (g_str_equal(group, "object") ||
2207         g_str_equal(group, "machine") ||
2208         g_str_equal(group, "smp-opts")) {
2209         return false;
2210     }
2211     return true;
2212 }
2213
2214 static void qemu_record_config_group(const char *group, QDict *dict,
2215                                      bool from_json, Error **errp)
2216 {
2217     if (g_str_equal(group, "object")) {
2218         Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2219         object_option_add_visitor(v);
2220         visit_free(v);
2221     } else if (g_str_equal(group, "machine")) {
2222         /*
2223          * Cannot merge string-valued and type-safe dictionaries, so JSON
2224          * is not accepted yet for -M.
2225          */
2226         assert(!from_json);
2227         keyval_merge(machine_opts_dict, dict, errp);
2228     } else if (g_str_equal(group, "smp-opts")) {
2229         machine_merge_property("smp", dict, &error_fatal);
2230     } else {
2231         abort();
2232     }
2233 }
2234
2235 /*
2236  * Parse non-QemuOpts config file groups, pass the rest to
2237  * qemu_config_do_parse.
2238  */
2239 static void qemu_parse_config_group(const char *group, QDict *qdict,
2240                                     void *opaque, Error **errp)
2241 {
2242     QObject *crumpled;
2243     if (is_qemuopts_group(group)) {
2244         qemu_config_do_parse(group, qdict, opaque, errp);
2245         return;
2246     }
2247
2248     crumpled = qdict_crumple(qdict, errp);
2249     if (!crumpled) {
2250         return;
2251     }
2252     switch (qobject_type(crumpled)) {
2253     case QTYPE_QDICT:
2254         qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2255         break;
2256     case QTYPE_QLIST:
2257         error_setg(errp, "Lists cannot be at top level of a configuration section");
2258         break;
2259     default:
2260         g_assert_not_reached();
2261     }
2262     qobject_unref(crumpled);
2263 }
2264
2265 static void qemu_read_default_config_file(Error **errp)
2266 {
2267     ERRP_GUARD();
2268     int ret;
2269     g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2270
2271     ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2272     if (ret < 0) {
2273         if (ret == -ENOENT) {
2274             error_free(*errp);
2275             *errp = NULL;
2276         }
2277     }
2278 }
2279
2280 static void qemu_set_option(const char *str, Error **errp)
2281 {
2282     char group[64], id[64], arg[64];
2283     QemuOptsList *list;
2284     QemuOpts *opts;
2285     int rc, offset;
2286
2287     rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2288     if (rc < 3 || str[offset] != '=') {
2289         error_setg(errp, "can't parse: \"%s\"", str);
2290         return;
2291     }
2292
2293     if (!is_qemuopts_group(group)) {
2294         error_setg(errp, "-set is not supported with %s", group);
2295     } else {
2296         list = qemu_find_opts_err(group, errp);
2297         if (list) {
2298             opts = qemu_opts_find(list, id);
2299             if (!opts) {
2300                 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2301                 return;
2302             }
2303             qemu_opt_set(opts, arg, str + offset + 1, errp);
2304         }
2305     }
2306 }
2307
2308 static void user_register_global_props(void)
2309 {
2310     qemu_opts_foreach(qemu_find_opts("global"),
2311                       global_init_func, NULL, NULL);
2312 }
2313
2314 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2315 {
2316     icount_configure(opts, errp);
2317     return 0;
2318 }
2319
2320 static int accelerator_set_property(void *opaque,
2321                                 const char *name, const char *value,
2322                                 Error **errp)
2323 {
2324     return object_parse_property_opt(opaque, name, value, "accel", errp);
2325 }
2326
2327 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2328 {
2329     bool *p_init_failed = opaque;
2330     const char *acc = qemu_opt_get(opts, "accel");
2331     AccelClass *ac = accel_find(acc);
2332     AccelState *accel;
2333     int ret;
2334     bool qtest_with_kvm;
2335
2336     qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2337
2338     if (!ac) {
2339         *p_init_failed = true;
2340         if (!qtest_with_kvm) {
2341             error_report("invalid accelerator %s", acc);
2342         }
2343         return 0;
2344     }
2345     accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2346     object_apply_compat_props(OBJECT(accel));
2347     qemu_opt_foreach(opts, accelerator_set_property,
2348                      accel,
2349                      &error_fatal);
2350
2351     ret = accel_init_machine(accel, current_machine);
2352     if (ret < 0) {
2353         *p_init_failed = true;
2354         if (!qtest_with_kvm || ret != -ENOENT) {
2355             error_report("failed to initialize %s: %s", acc, strerror(-ret));
2356         }
2357         return 0;
2358     }
2359
2360     return 1;
2361 }
2362
2363 static void configure_accelerators(const char *progname)
2364 {
2365     bool init_failed = false;
2366
2367     qemu_opts_foreach(qemu_find_opts("icount"),
2368                       do_configure_icount, NULL, &error_fatal);
2369
2370     if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2371         char **accel_list, **tmp;
2372
2373         if (accelerators == NULL) {
2374             /* Select the default accelerator */
2375             bool have_tcg = accel_find("tcg");
2376             bool have_kvm = accel_find("kvm");
2377
2378             if (have_tcg && have_kvm) {
2379                 if (g_str_has_suffix(progname, "kvm")) {
2380                     /* If the program name ends with "kvm", we prefer KVM */
2381                     accelerators = "kvm:tcg";
2382                 } else {
2383                     accelerators = "tcg:kvm";
2384                 }
2385             } else if (have_kvm) {
2386                 accelerators = "kvm";
2387             } else if (have_tcg) {
2388                 accelerators = "tcg";
2389             } else {
2390                 error_report("No accelerator selected and"
2391                              " no default accelerator available");
2392                 exit(1);
2393             }
2394         }
2395         accel_list = g_strsplit(accelerators, ":", 0);
2396
2397         for (tmp = accel_list; *tmp; tmp++) {
2398             /*
2399              * Filter invalid accelerators here, to prevent obscenities
2400              * such as "-machine accel=tcg,,thread=single".
2401              */
2402             if (accel_find(*tmp)) {
2403                 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2404             } else {
2405                 init_failed = true;
2406                 error_report("invalid accelerator %s", *tmp);
2407             }
2408         }
2409         g_strfreev(accel_list);
2410     } else {
2411         if (accelerators != NULL) {
2412             error_report("The -accel and \"-machine accel=\" options are incompatible");
2413             exit(1);
2414         }
2415     }
2416
2417     if (!qemu_opts_foreach(qemu_find_opts("accel"),
2418                            do_configure_accelerator, &init_failed, &error_fatal)) {
2419         if (!init_failed) {
2420             error_report("no accelerator found");
2421         }
2422         exit(1);
2423     }
2424
2425     if (init_failed && !qtest_chrdev) {
2426         AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2427         error_report("falling back to %s", ac->name);
2428     }
2429
2430     if (icount_enabled() && !tcg_enabled()) {
2431         error_report("-icount is not allowed with hardware virtualization");
2432         exit(1);
2433     }
2434 }
2435
2436 static void create_default_memdev(MachineState *ms, const char *path)
2437 {
2438     Object *obj;
2439     MachineClass *mc = MACHINE_GET_CLASS(ms);
2440
2441     obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2442     if (path) {
2443         object_property_set_str(obj, "mem-path", path, &error_fatal);
2444     }
2445     object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2446     object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2447                               obj);
2448     /* Ensure backend's memory region name is equal to mc->default_ram_id */
2449     object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2450                              false, &error_fatal);
2451     user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2452     object_unref(obj);
2453     object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2454                             &error_fatal);
2455 }
2456
2457 static void qemu_validate_options(const QDict *machine_opts)
2458 {
2459     const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2460     const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2461     const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2462
2463     if (kernel_filename == NULL) {
2464          if (kernel_cmdline != NULL) {
2465               error_report("-append only allowed with -kernel option");
2466               exit(1);
2467           }
2468
2469           if (initrd_filename != NULL) {
2470               error_report("-initrd only allowed with -kernel option");
2471               exit(1);
2472           }
2473     }
2474
2475     if (loadvm && preconfig_requested) {
2476         error_report("'preconfig' and 'loadvm' options are "
2477                      "mutually exclusive");
2478         exit(EXIT_FAILURE);
2479     }
2480     if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2481         error_report("'preconfig' supports '-incoming defer' only");
2482         exit(EXIT_FAILURE);
2483     }
2484
2485 #ifdef CONFIG_CURSES
2486     if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2487         error_report("curses display cannot be used with -daemonize");
2488         exit(1);
2489     }
2490 #endif
2491 }
2492
2493 static void qemu_process_sugar_options(void)
2494 {
2495     if (mem_prealloc) {
2496         QObject *smp = qdict_get(machine_opts_dict, "smp");
2497         if (smp && qobject_type(smp) == QTYPE_QDICT) {
2498             QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2499             if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2500                 const char *val = qstring_get_str(qobject_to(QString, cpus));
2501                 object_register_sugar_prop("memory-backend", "prealloc-threads",
2502                                            val, false);
2503             }
2504         }
2505         object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2506     }
2507
2508     if (watchdog) {
2509         int i = select_watchdog(watchdog);
2510         if (i > 0)
2511             exit (i == 1 ? 1 : 0);
2512     }
2513 }
2514
2515 /* -action processing */
2516
2517 /*
2518  * Process all the -action parameters parsed from cmdline.
2519  */
2520 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2521 {
2522     Error *local_err = NULL;
2523     QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2524     QObject *ret = NULL;
2525     qmp_marshal_set_action(qdict, &ret, &local_err);
2526     qobject_unref(ret);
2527     qobject_unref(qdict);
2528     if (local_err) {
2529         error_propagate(errp, local_err);
2530         return 1;
2531     }
2532     return 0;
2533 }
2534
2535 static void qemu_process_early_options(void)
2536 {
2537 #ifdef CONFIG_SECCOMP
2538     QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2539     if (olist) {
2540         qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2541     }
2542 #endif
2543
2544     qemu_opts_foreach(qemu_find_opts("name"),
2545                       parse_name, NULL, &error_fatal);
2546
2547     if (qemu_opts_foreach(qemu_find_opts("action"),
2548                           process_runstate_actions, NULL, &error_fatal)) {
2549         exit(1);
2550     }
2551
2552 #ifndef _WIN32
2553     qemu_opts_foreach(qemu_find_opts("add-fd"),
2554                       parse_add_fd, NULL, &error_fatal);
2555
2556     qemu_opts_foreach(qemu_find_opts("add-fd"),
2557                       cleanup_add_fd, NULL, &error_fatal);
2558 #endif
2559
2560     /* Open the logfile at this point and set the log mask if necessary.  */
2561     if (log_file) {
2562         qemu_set_log_filename(log_file, &error_fatal);
2563     }
2564     if (log_mask) {
2565         int mask;
2566         mask = qemu_str_to_log_mask(log_mask);
2567         if (!mask) {
2568             qemu_print_log_usage(stdout);
2569             exit(1);
2570         }
2571         qemu_set_log(mask);
2572     } else {
2573         qemu_set_log(0);
2574     }
2575
2576     qemu_add_default_firmwarepath();
2577 }
2578
2579 static void qemu_process_help_options(void)
2580 {
2581     /*
2582      * Check for -cpu help and -device help before we call select_machine(),
2583      * which will return an error if the architecture has no default machine
2584      * type and the user did not specify one, so that the user doesn't need
2585      * to say '-cpu help -machine something'.
2586      */
2587     if (cpu_option && is_help_option(cpu_option)) {
2588         list_cpus(cpu_option);
2589         exit(0);
2590     }
2591
2592     if (qemu_opts_foreach(qemu_find_opts("device"),
2593                           device_help_func, NULL, NULL)) {
2594         exit(0);
2595     }
2596
2597     /* -L help lists the data directories and exits. */
2598     if (list_data_dirs) {
2599         qemu_list_data_dirs();
2600         exit(0);
2601     }
2602 }
2603
2604 static void qemu_maybe_daemonize(const char *pid_file)
2605 {
2606     Error *err = NULL;
2607
2608     os_daemonize();
2609     rcu_disable_atfork();
2610
2611     if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2612         error_reportf_err(err, "cannot create PID file: ");
2613         exit(1);
2614     }
2615
2616     qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2617     qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2618 }
2619
2620 static void qemu_init_displays(void)
2621 {
2622     DisplayState *ds;
2623
2624     /* init local displays */
2625     ds = init_displaystate();
2626     qemu_display_init(ds, &dpy);
2627
2628     /* must be after terminal init, SDL library changes signal handlers */
2629     os_setup_signal_handling();
2630
2631     /* init remote displays */
2632 #ifdef CONFIG_VNC
2633     qemu_opts_foreach(qemu_find_opts("vnc"),
2634                       vnc_init_func, NULL, &error_fatal);
2635 #endif
2636
2637     if (using_spice) {
2638         qemu_spice.display_init();
2639     }
2640 }
2641
2642 static void qemu_init_board(void)
2643 {
2644     MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2645
2646     if (machine_class->default_ram_id && current_machine->ram_size &&
2647         numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2648         create_default_memdev(current_machine, mem_path);
2649     }
2650
2651     /* process plugin before CPUs are created, but once -smp has been parsed */
2652     qemu_plugin_load_list(&plugin_list, &error_fatal);
2653
2654     /* From here on we enter MACHINE_PHASE_INITIALIZED.  */
2655     machine_run_board_init(current_machine);
2656
2657     drive_check_orphaned();
2658
2659     realtime_init();
2660
2661     if (hax_enabled()) {
2662         /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2663         hax_sync_vcpus();
2664     }
2665 }
2666
2667 static void qemu_create_cli_devices(void)
2668 {
2669     DeviceOption *opt;
2670
2671     soundhw_init();
2672
2673     qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2674                       parse_fw_cfg, fw_cfg_find(), &error_fatal);
2675
2676     /* init USB devices */
2677     if (machine_usb(current_machine)) {
2678         if (foreach_device_config(DEV_USB, usb_parse) < 0)
2679             exit(1);
2680     }
2681
2682     /* init generic devices */
2683     rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2684     qemu_opts_foreach(qemu_find_opts("device"),
2685                       device_init_func, NULL, &error_fatal);
2686     QTAILQ_FOREACH(opt, &device_opts, next) {
2687         DeviceState *dev;
2688         loc_push_restore(&opt->loc);
2689         /*
2690          * TODO Eventually we should call qmp_device_add() here to make sure it
2691          * behaves the same, but QMP still has to accept incorrectly typed
2692          * options until libvirt is fixed and we want to be strict on the CLI
2693          * from the start, so call qdev_device_add_from_qdict() directly for
2694          * now.
2695          */
2696         dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2697         object_unref(OBJECT(dev));
2698         loc_pop(&opt->loc);
2699     }
2700     rom_reset_order_override();
2701 }
2702
2703 static void qemu_machine_creation_done(void)
2704 {
2705     MachineState *machine = MACHINE(qdev_get_machine());
2706
2707     /* Did we create any drives that we failed to create a device for? */
2708     drive_check_orphaned();
2709
2710     /* Don't warn about the default network setup that you get if
2711      * no command line -net or -netdev options are specified. There
2712      * are two cases that we would otherwise complain about:
2713      * (1) board doesn't support a NIC but the implicit "-net nic"
2714      * requested one
2715      * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2716      * sets up a nic that isn't connected to anything.
2717      */
2718     if (!default_net && (!qtest_enabled() || has_defaults)) {
2719         net_check_clients();
2720     }
2721
2722     qdev_prop_check_globals();
2723
2724     qdev_machine_creation_done();
2725
2726     if (machine->cgs) {
2727         /*
2728          * Verify that Confidential Guest Support has actually been initialized
2729          */
2730         assert(machine->cgs->ready);
2731     }
2732
2733     if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2734         exit(1);
2735     }
2736 }
2737
2738 void qmp_x_exit_preconfig(Error **errp)
2739 {
2740     if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2741         error_setg(errp, "The command is permitted only before machine initialization");
2742         return;
2743     }
2744
2745     qemu_init_board();
2746     qemu_create_cli_devices();
2747     qemu_machine_creation_done();
2748
2749     if (loadvm) {
2750         load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
2751     }
2752     if (replay_mode != REPLAY_MODE_NONE) {
2753         replay_vmstate_init();
2754     }
2755
2756     if (incoming) {
2757         Error *local_err = NULL;
2758         if (strcmp(incoming, "defer") != 0) {
2759             qmp_migrate_incoming(incoming, &local_err);
2760             if (local_err) {
2761                 error_reportf_err(local_err, "-incoming %s: ", incoming);
2762                 exit(1);
2763             }
2764         }
2765     } else if (autostart) {
2766         qmp_cont(NULL);
2767     }
2768 }
2769
2770 void qemu_init(int argc, char **argv, char **envp)
2771 {
2772     QemuOpts *opts;
2773     QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2774     QemuOptsList *olist;
2775     int optind;
2776     const char *optarg;
2777     MachineClass *machine_class;
2778     bool userconfig = true;
2779     FILE *vmstate_dump_file = NULL;
2780
2781     qemu_add_opts(&qemu_drive_opts);
2782     qemu_add_drive_opts(&qemu_legacy_drive_opts);
2783     qemu_add_drive_opts(&qemu_common_drive_opts);
2784     qemu_add_drive_opts(&qemu_drive_opts);
2785     qemu_add_drive_opts(&bdrv_runtime_opts);
2786     qemu_add_opts(&qemu_chardev_opts);
2787     qemu_add_opts(&qemu_device_opts);
2788     qemu_add_opts(&qemu_netdev_opts);
2789     qemu_add_opts(&qemu_nic_opts);
2790     qemu_add_opts(&qemu_net_opts);
2791     qemu_add_opts(&qemu_rtc_opts);
2792     qemu_add_opts(&qemu_global_opts);
2793     qemu_add_opts(&qemu_mon_opts);
2794     qemu_add_opts(&qemu_trace_opts);
2795     qemu_plugin_add_opts();
2796     qemu_add_opts(&qemu_option_rom_opts);
2797     qemu_add_opts(&qemu_accel_opts);
2798     qemu_add_opts(&qemu_mem_opts);
2799     qemu_add_opts(&qemu_smp_opts);
2800     qemu_add_opts(&qemu_boot_opts);
2801     qemu_add_opts(&qemu_add_fd_opts);
2802     qemu_add_opts(&qemu_object_opts);
2803     qemu_add_opts(&qemu_tpmdev_opts);
2804     qemu_add_opts(&qemu_overcommit_opts);
2805     qemu_add_opts(&qemu_msg_opts);
2806     qemu_add_opts(&qemu_name_opts);
2807     qemu_add_opts(&qemu_numa_opts);
2808     qemu_add_opts(&qemu_icount_opts);
2809     qemu_add_opts(&qemu_semihosting_config_opts);
2810     qemu_add_opts(&qemu_fw_cfg_opts);
2811     qemu_add_opts(&qemu_action_opts);
2812     module_call_init(MODULE_INIT_OPTS);
2813
2814     error_init(argv[0]);
2815     qemu_init_exec_dir(argv[0]);
2816
2817 #ifdef CONFIG_MODULES
2818     module_init_info(qemu_modinfo);
2819     module_allow_arch(TARGET_NAME);
2820 #endif
2821
2822     qemu_init_subsystems();
2823
2824     /* first pass of option parsing */
2825     optind = 1;
2826     while (optind < argc) {
2827         if (argv[optind][0] != '-') {
2828             /* disk image */
2829             optind++;
2830         } else {
2831             const QEMUOption *popt;
2832
2833             popt = lookup_opt(argc, argv, &optarg, &optind);
2834             switch (popt->index) {
2835             case QEMU_OPTION_nouserconfig:
2836                 userconfig = false;
2837                 break;
2838             }
2839         }
2840     }
2841
2842     machine_opts_dict = qdict_new();
2843     if (userconfig) {
2844         qemu_read_default_config_file(&error_fatal);
2845     }
2846
2847     /* second pass of option parsing */
2848     optind = 1;
2849     for(;;) {
2850         if (optind >= argc)
2851             break;
2852         if (argv[optind][0] != '-') {
2853             loc_set_cmdline(argv, optind, 1);
2854             drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2855         } else {
2856             const QEMUOption *popt;
2857
2858             popt = lookup_opt(argc, argv, &optarg, &optind);
2859             if (!(popt->arch_mask & arch_type)) {
2860                 error_report("Option not supported for this target");
2861                 exit(1);
2862             }
2863             switch(popt->index) {
2864             case QEMU_OPTION_cpu:
2865                 /* hw initialization will check this */
2866                 cpu_option = optarg;
2867                 break;
2868             case QEMU_OPTION_hda:
2869             case QEMU_OPTION_hdb:
2870             case QEMU_OPTION_hdc:
2871             case QEMU_OPTION_hdd:
2872                 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2873                           HD_OPTS);
2874                 break;
2875             case QEMU_OPTION_blockdev:
2876                 {
2877                     Visitor *v;
2878                     BlockdevOptionsQueueEntry *bdo;
2879
2880                     v = qobject_input_visitor_new_str(optarg, "driver",
2881                                                       &error_fatal);
2882
2883                     bdo = g_new(BlockdevOptionsQueueEntry, 1);
2884                     visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2885                                                &error_fatal);
2886                     visit_free(v);
2887                     loc_save(&bdo->loc);
2888                     QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2889                     break;
2890                 }
2891             case QEMU_OPTION_drive:
2892                 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
2893                                                optarg, false);
2894                 if (opts == NULL) {
2895                     exit(1);
2896                 }
2897                 break;
2898             case QEMU_OPTION_set:
2899                 qemu_set_option(optarg, &error_fatal);
2900                 break;
2901             case QEMU_OPTION_global:
2902                 if (qemu_global_option(optarg) != 0)
2903                     exit(1);
2904                 break;
2905             case QEMU_OPTION_mtdblock:
2906                 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2907                 break;
2908             case QEMU_OPTION_sd:
2909                 drive_add(IF_SD, -1, optarg, SD_OPTS);
2910                 break;
2911             case QEMU_OPTION_pflash:
2912                 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2913                 break;
2914             case QEMU_OPTION_snapshot:
2915                 {
2916                     Error *blocker = NULL;
2917                     snapshot = 1;
2918                     error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2919                                "-snapshot");
2920                     replay_add_blocker(blocker);
2921                 }
2922                 break;
2923             case QEMU_OPTION_numa:
2924                 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2925                                                optarg, true);
2926                 if (!opts) {
2927                     exit(1);
2928                 }
2929                 break;
2930             case QEMU_OPTION_display:
2931                 parse_display(optarg);
2932                 break;
2933             case QEMU_OPTION_nographic:
2934                 qdict_put_str(machine_opts_dict, "graphics", "off");
2935                 nographic = true;
2936                 dpy.type = DISPLAY_TYPE_NONE;
2937                 break;
2938             case QEMU_OPTION_curses:
2939                 warn_report("-curses is deprecated, "
2940                             "use -display curses instead.");
2941 #ifdef CONFIG_CURSES
2942                 dpy.type = DISPLAY_TYPE_CURSES;
2943 #else
2944                 error_report("curses or iconv support is disabled");
2945                 exit(1);
2946 #endif
2947                 break;
2948             case QEMU_OPTION_portrait:
2949                 graphic_rotate = 90;
2950                 break;
2951             case QEMU_OPTION_rotate:
2952                 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2953                 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2954                     graphic_rotate != 180 && graphic_rotate != 270) {
2955                     error_report("only 90, 180, 270 deg rotation is available");
2956                     exit(1);
2957                 }
2958                 break;
2959             case QEMU_OPTION_kernel:
2960                 qdict_put_str(machine_opts_dict, "kernel", optarg);
2961                 break;
2962             case QEMU_OPTION_initrd:
2963                 qdict_put_str(machine_opts_dict, "initrd", optarg);
2964                 break;
2965             case QEMU_OPTION_append:
2966                 qdict_put_str(machine_opts_dict, "append", optarg);
2967                 break;
2968             case QEMU_OPTION_dtb:
2969                 qdict_put_str(machine_opts_dict, "dtb", optarg);
2970                 break;
2971             case QEMU_OPTION_cdrom:
2972                 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2973                 break;
2974             case QEMU_OPTION_boot:
2975                 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2976                                                optarg, true);
2977                 if (!opts) {
2978                     exit(1);
2979                 }
2980                 break;
2981             case QEMU_OPTION_fda:
2982             case QEMU_OPTION_fdb:
2983                 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2984                           optarg, FD_OPTS);
2985                 break;
2986             case QEMU_OPTION_no_fd_bootchk:
2987                 fd_bootchk = 0;
2988                 break;
2989             case QEMU_OPTION_netdev:
2990                 default_net = 0;
2991                 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2992                     exit(1);
2993                 }
2994                 break;
2995             case QEMU_OPTION_nic:
2996                 default_net = 0;
2997                 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2998                     exit(1);
2999                 }
3000                 break;
3001             case QEMU_OPTION_net:
3002                 default_net = 0;
3003                 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3004                     exit(1);
3005                 }
3006                 break;
3007 #ifdef CONFIG_LIBISCSI
3008             case QEMU_OPTION_iscsi:
3009                 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3010                                                optarg, false);
3011                 if (!opts) {
3012                     exit(1);
3013                 }
3014                 break;
3015 #endif
3016             case QEMU_OPTION_audio_help:
3017                 audio_legacy_help();
3018                 exit (0);
3019                 break;
3020             case QEMU_OPTION_audiodev:
3021                 audio_parse_option(optarg);
3022                 break;
3023             case QEMU_OPTION_soundhw:
3024                 select_soundhw (optarg);
3025                 break;
3026             case QEMU_OPTION_h:
3027                 help(0);
3028                 break;
3029             case QEMU_OPTION_version:
3030                 version();
3031                 exit(0);
3032                 break;
3033             case QEMU_OPTION_m:
3034                 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3035                                                optarg, true);
3036                 if (!opts) {
3037                     exit(EXIT_FAILURE);
3038                 }
3039                 break;
3040 #ifdef CONFIG_TPM
3041             case QEMU_OPTION_tpmdev:
3042                 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3043                     exit(1);
3044                 }
3045                 break;
3046 #endif
3047             case QEMU_OPTION_mempath:
3048                 mem_path = optarg;
3049                 break;
3050             case QEMU_OPTION_mem_prealloc:
3051                 mem_prealloc = 1;
3052                 break;
3053             case QEMU_OPTION_d:
3054                 log_mask = optarg;
3055                 break;
3056             case QEMU_OPTION_D:
3057                 log_file = optarg;
3058                 break;
3059             case QEMU_OPTION_DFILTER:
3060                 qemu_set_dfilter_ranges(optarg, &error_fatal);
3061                 break;
3062             case QEMU_OPTION_seed:
3063                 qemu_guest_random_seed_main(optarg, &error_fatal);
3064                 break;
3065             case QEMU_OPTION_s:
3066                 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3067                 break;
3068             case QEMU_OPTION_gdb:
3069                 add_device_config(DEV_GDB, optarg);
3070                 break;
3071             case QEMU_OPTION_L:
3072                 if (is_help_option(optarg)) {
3073                     list_data_dirs = true;
3074                 } else {
3075                     qemu_add_data_dir(g_strdup(optarg));
3076                 }
3077                 break;
3078             case QEMU_OPTION_bios:
3079                 qdict_put_str(machine_opts_dict, "firmware", optarg);
3080                 break;
3081             case QEMU_OPTION_singlestep:
3082                 singlestep = 1;
3083                 break;
3084             case QEMU_OPTION_S:
3085                 autostart = 0;
3086                 break;
3087             case QEMU_OPTION_k:
3088                 keyboard_layout = optarg;
3089                 break;
3090             case QEMU_OPTION_vga:
3091                 vga_model = optarg;
3092                 default_vga = 0;
3093                 break;
3094             case QEMU_OPTION_g:
3095                 {
3096                     const char *p;
3097                     int w, h, depth;
3098                     p = optarg;
3099                     w = strtol(p, (char **)&p, 10);
3100                     if (w <= 0) {
3101                     graphic_error:
3102                         error_report("invalid resolution or depth");
3103                         exit(1);
3104                     }
3105                     if (*p != 'x')
3106                         goto graphic_error;
3107                     p++;
3108                     h = strtol(p, (char **)&p, 10);
3109                     if (h <= 0)
3110                         goto graphic_error;
3111                     if (*p == 'x') {
3112                         p++;
3113                         depth = strtol(p, (char **)&p, 10);
3114                         if (depth != 1 && depth != 2 && depth != 4 &&
3115                             depth != 8 && depth != 15 && depth != 16 &&
3116                             depth != 24 && depth != 32)
3117                             goto graphic_error;
3118                     } else if (*p == '\0') {
3119                         depth = graphic_depth;
3120                     } else {
3121                         goto graphic_error;
3122                     }
3123
3124                     graphic_width = w;
3125                     graphic_height = h;
3126                     graphic_depth = depth;
3127                 }
3128                 break;
3129             case QEMU_OPTION_echr:
3130                 {
3131                     char *r;
3132                     term_escape_char = strtol(optarg, &r, 0);
3133                     if (r == optarg)
3134                         printf("Bad argument to echr\n");
3135                     break;
3136                 }
3137             case QEMU_OPTION_monitor:
3138                 default_monitor = 0;
3139                 if (strncmp(optarg, "none", 4)) {
3140                     monitor_parse(optarg, "readline", false);
3141                 }
3142                 break;
3143             case QEMU_OPTION_qmp:
3144                 monitor_parse(optarg, "control", false);
3145                 default_monitor = 0;
3146                 break;
3147             case QEMU_OPTION_qmp_pretty:
3148                 monitor_parse(optarg, "control", true);
3149                 default_monitor = 0;
3150                 break;
3151             case QEMU_OPTION_mon:
3152                 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3153                                                true);
3154                 if (!opts) {
3155                     exit(1);
3156                 }
3157                 default_monitor = 0;
3158                 break;
3159             case QEMU_OPTION_chardev:
3160                 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3161                                                optarg, true);
3162                 if (!opts) {
3163                     exit(1);
3164                 }
3165                 break;
3166             case QEMU_OPTION_fsdev:
3167                 olist = qemu_find_opts("fsdev");
3168                 if (!olist) {
3169                     error_report("fsdev support is disabled");
3170                     exit(1);
3171                 }
3172                 opts = qemu_opts_parse_noisily(olist, optarg, true);
3173                 if (!opts) {
3174                     exit(1);
3175                 }
3176                 break;
3177             case QEMU_OPTION_virtfs: {
3178                 QemuOpts *fsdev;
3179                 QemuOpts *device;
3180                 const char *writeout, *sock_fd, *socket, *path, *security_model,
3181                            *multidevs;
3182
3183                 olist = qemu_find_opts("virtfs");
3184                 if (!olist) {
3185                     error_report("virtfs support is disabled");
3186                     exit(1);
3187                 }
3188                 opts = qemu_opts_parse_noisily(olist, optarg, true);
3189                 if (!opts) {
3190                     exit(1);
3191                 }
3192
3193                 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3194                     qemu_opt_get(opts, "mount_tag") == NULL) {
3195                     error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3196                     exit(1);
3197                 }
3198                 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3199                                          qemu_opts_id(opts) ?:
3200                                          qemu_opt_get(opts, "mount_tag"),
3201                                          1, NULL);
3202                 if (!fsdev) {
3203                     error_report("duplicate or invalid fsdev id: %s",
3204                                  qemu_opt_get(opts, "mount_tag"));
3205                     exit(1);
3206                 }
3207
3208                 writeout = qemu_opt_get(opts, "writeout");
3209                 if (writeout) {
3210 #ifdef CONFIG_SYNC_FILE_RANGE
3211                     qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3212 #else
3213                     error_report("writeout=immediate not supported "
3214                                  "on this platform");
3215                     exit(1);
3216 #endif
3217                 }
3218                 qemu_opt_set(fsdev, "fsdriver",
3219                              qemu_opt_get(opts, "fsdriver"), &error_abort);
3220                 path = qemu_opt_get(opts, "path");
3221                 if (path) {
3222                     qemu_opt_set(fsdev, "path", path, &error_abort);
3223                 }
3224                 security_model = qemu_opt_get(opts, "security_model");
3225                 if (security_model) {
3226                     qemu_opt_set(fsdev, "security_model", security_model,
3227                                  &error_abort);
3228                 }
3229                 socket = qemu_opt_get(opts, "socket");
3230                 if (socket) {
3231                     qemu_opt_set(fsdev, "socket", socket, &error_abort);
3232                 }
3233                 sock_fd = qemu_opt_get(opts, "sock_fd");
3234                 if (sock_fd) {
3235                     qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3236                 }
3237
3238                 qemu_opt_set_bool(fsdev, "readonly",
3239                                   qemu_opt_get_bool(opts, "readonly", 0),
3240                                   &error_abort);
3241                 multidevs = qemu_opt_get(opts, "multidevs");
3242                 if (multidevs) {
3243                     qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3244                 }
3245                 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3246                                           &error_abort);
3247                 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3248                 qemu_opt_set(device, "fsdev",
3249                              qemu_opts_id(fsdev), &error_abort);
3250                 qemu_opt_set(device, "mount_tag",
3251                              qemu_opt_get(opts, "mount_tag"), &error_abort);
3252                 break;
3253             }
3254             case QEMU_OPTION_serial:
3255                 add_device_config(DEV_SERIAL, optarg);
3256                 default_serial = 0;
3257                 if (strncmp(optarg, "mon:", 4) == 0) {
3258                     default_monitor = 0;
3259                 }
3260                 break;
3261             case QEMU_OPTION_watchdog:
3262                 if (watchdog) {
3263                     error_report("only one watchdog option may be given");
3264                     exit(1);
3265                 }
3266                 warn_report("-watchdog is deprecated; use -device instead.");
3267                 watchdog = optarg;
3268                 break;
3269             case QEMU_OPTION_action:
3270                 olist = qemu_find_opts("action");
3271                 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3272                      exit(1);
3273                 }
3274                 break;
3275             case QEMU_OPTION_watchdog_action: {
3276                 QemuOpts *opts;
3277                 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3278                 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
3279                 break;
3280             }
3281             case QEMU_OPTION_parallel:
3282                 add_device_config(DEV_PARALLEL, optarg);
3283                 default_parallel = 0;
3284                 if (strncmp(optarg, "mon:", 4) == 0) {
3285                     default_monitor = 0;
3286                 }
3287                 break;
3288             case QEMU_OPTION_debugcon:
3289                 add_device_config(DEV_DEBUGCON, optarg);
3290                 break;
3291             case QEMU_OPTION_loadvm:
3292                 loadvm = optarg;
3293                 break;
3294             case QEMU_OPTION_full_screen:
3295                 dpy.has_full_screen = true;
3296                 dpy.full_screen = true;
3297                 break;
3298             case QEMU_OPTION_alt_grab:
3299                 alt_grab = 1;
3300                 warn_report("-alt-grab is deprecated, please use "
3301                             "-display sdl,grab-mod=lshift-lctrl-lalt instead.");
3302                 break;
3303             case QEMU_OPTION_ctrl_grab:
3304                 ctrl_grab = 1;
3305                 warn_report("-ctrl-grab is deprecated, please use "
3306                             "-display sdl,grab-mod=rctrl instead.");
3307                 break;
3308             case QEMU_OPTION_sdl:
3309                 warn_report("-sdl is deprecated, use -display sdl instead.");
3310 #ifdef CONFIG_SDL
3311                 dpy.type = DISPLAY_TYPE_SDL;
3312                 break;
3313 #else
3314                 error_report("SDL support is disabled");
3315                 exit(1);
3316 #endif
3317             case QEMU_OPTION_pidfile:
3318                 pid_file = optarg;
3319                 break;
3320             case QEMU_OPTION_win2k_hack:
3321                 win2k_install_hack = 1;
3322                 break;
3323             case QEMU_OPTION_acpitable:
3324                 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3325                                                optarg, true);
3326                 if (!opts) {
3327                     exit(1);
3328                 }
3329                 acpi_table_add(opts, &error_fatal);
3330                 break;
3331             case QEMU_OPTION_smbios:
3332                 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3333                                                optarg, false);
3334                 if (!opts) {
3335                     exit(1);
3336                 }
3337                 smbios_entry_add(opts, &error_fatal);
3338                 break;
3339             case QEMU_OPTION_fwcfg:
3340                 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3341                                                optarg, true);
3342                 if (opts == NULL) {
3343                     exit(1);
3344                 }
3345                 break;
3346             case QEMU_OPTION_preconfig:
3347                 preconfig_requested = true;
3348                 break;
3349             case QEMU_OPTION_enable_kvm:
3350                 qdict_put_str(machine_opts_dict, "accel", "kvm");
3351                 break;
3352             case QEMU_OPTION_M:
3353             case QEMU_OPTION_machine:
3354                 {
3355                     bool help;
3356
3357                     keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3358                     if (help) {
3359                         machine_help_func(machine_opts_dict);
3360                         exit(EXIT_SUCCESS);
3361                     }
3362                     break;
3363                 }
3364             case QEMU_OPTION_accel:
3365                 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3366                                                      optarg, true);
3367                 optarg = qemu_opt_get(accel_opts, "accel");
3368                 if (!optarg || is_help_option(optarg)) {
3369                     printf("Accelerators supported in QEMU binary:\n");
3370                     GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3371                                                                     false);
3372                     for (el = accel_list; el; el = el->next) {
3373                         gchar *typename = g_strdup(object_class_get_name(
3374                                                    OBJECT_CLASS(el->data)));
3375                         /* omit qtest which is used for tests only */
3376                         if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3377                             g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3378                             gchar **optname = g_strsplit(typename,
3379                                                          ACCEL_CLASS_SUFFIX, 0);
3380                             printf("%s\n", optname[0]);
3381                             g_strfreev(optname);
3382                         }
3383                         g_free(typename);
3384                     }
3385                     g_slist_free(accel_list);
3386                     exit(0);
3387                 }
3388                 break;
3389             case QEMU_OPTION_usb:
3390                 qdict_put_str(machine_opts_dict, "usb", "on");
3391                 break;
3392             case QEMU_OPTION_usbdevice:
3393                 qdict_put_str(machine_opts_dict, "usb", "on");
3394                 add_device_config(DEV_USB, optarg);
3395                 break;
3396             case QEMU_OPTION_device:
3397                 if (optarg[0] == '{') {
3398                     QObject *obj = qobject_from_json(optarg, &error_fatal);
3399                     DeviceOption *opt = g_new0(DeviceOption, 1);
3400                     opt->opts = qobject_to(QDict, obj);
3401                     loc_save(&opt->loc);
3402                     assert(opt->opts != NULL);
3403                     QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3404                 } else {
3405                     if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3406                                                  optarg, true)) {
3407                         exit(1);
3408                     }
3409                 }
3410                 break;
3411             case QEMU_OPTION_smp:
3412                 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3413                                            "smp", optarg);
3414                 break;
3415             case QEMU_OPTION_vnc:
3416                 vnc_parse(optarg);
3417                 break;
3418             case QEMU_OPTION_no_acpi:
3419                 qdict_put_str(machine_opts_dict, "acpi", "off");
3420                 break;
3421             case QEMU_OPTION_no_hpet:
3422                 qdict_put_str(machine_opts_dict, "hpet", "off");
3423                 break;
3424             case QEMU_OPTION_no_reboot:
3425                 olist = qemu_find_opts("action");
3426                 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3427                 break;
3428             case QEMU_OPTION_no_shutdown:
3429                 olist = qemu_find_opts("action");
3430                 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3431                 break;
3432             case QEMU_OPTION_uuid:
3433                 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3434                     error_report("failed to parse UUID string: wrong format");
3435                     exit(1);
3436                 }
3437                 qemu_uuid_set = true;
3438                 break;
3439             case QEMU_OPTION_option_rom:
3440                 if (nb_option_roms >= MAX_OPTION_ROMS) {
3441                     error_report("too many option ROMs");
3442                     exit(1);
3443                 }
3444                 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3445                                                optarg, true);
3446                 if (!opts) {
3447                     exit(1);
3448                 }
3449                 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3450                 option_rom[nb_option_roms].bootindex =
3451                     qemu_opt_get_number(opts, "bootindex", -1);
3452                 if (!option_rom[nb_option_roms].name) {
3453                     error_report("Option ROM file is not specified");
3454                     exit(1);
3455                 }
3456                 nb_option_roms++;
3457                 break;
3458             case QEMU_OPTION_semihosting:
3459                 qemu_semihosting_enable();
3460                 break;
3461             case QEMU_OPTION_semihosting_config:
3462                 if (qemu_semihosting_config_options(optarg) != 0) {
3463                     exit(1);
3464                 }
3465                 break;
3466             case QEMU_OPTION_name:
3467                 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3468                                                optarg, true);
3469                 if (!opts) {
3470                     exit(1);
3471                 }
3472                 /* Capture guest name if -msg guest-name is used later */
3473                 error_guest_name = qemu_opt_get(opts, "guest");
3474                 break;
3475             case QEMU_OPTION_prom_env:
3476                 if (nb_prom_envs >= MAX_PROM_ENVS) {
3477                     error_report("too many prom variables");
3478                     exit(1);
3479                 }
3480                 prom_envs[nb_prom_envs] = optarg;
3481                 nb_prom_envs++;
3482                 break;
3483             case QEMU_OPTION_old_param:
3484                 old_param = 1;
3485                 break;
3486             case QEMU_OPTION_rtc:
3487                 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3488                                                false);
3489                 if (!opts) {
3490                     exit(1);
3491                 }
3492                 break;
3493             case QEMU_OPTION_icount:
3494                 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3495                                                       optarg, true);
3496                 if (!icount_opts) {
3497                     exit(1);
3498                 }
3499                 break;
3500             case QEMU_OPTION_incoming:
3501                 if (!incoming) {
3502                     runstate_set(RUN_STATE_INMIGRATE);
3503                 }
3504                 incoming = optarg;
3505                 break;
3506             case QEMU_OPTION_only_migratable:
3507                 only_migratable = 1;
3508                 break;
3509             case QEMU_OPTION_nodefaults:
3510                 has_defaults = 0;
3511                 break;
3512             case QEMU_OPTION_xen_domid:
3513                 if (!(accel_find("xen"))) {
3514                     error_report("Option not supported for this target");
3515                     exit(1);
3516                 }
3517                 xen_domid = atoi(optarg);
3518                 break;
3519             case QEMU_OPTION_xen_attach:
3520                 if (!(accel_find("xen"))) {
3521                     error_report("Option not supported for this target");
3522                     exit(1);
3523                 }
3524                 xen_mode = XEN_ATTACH;
3525                 break;
3526             case QEMU_OPTION_xen_domid_restrict:
3527                 if (!(accel_find("xen"))) {
3528                     error_report("Option not supported for this target");
3529                     exit(1);
3530                 }
3531                 xen_domid_restrict = true;
3532                 break;
3533             case QEMU_OPTION_trace:
3534                 trace_opt_parse(optarg);
3535                 break;
3536             case QEMU_OPTION_plugin:
3537                 qemu_plugin_opt_parse(optarg, &plugin_list);
3538                 break;
3539             case QEMU_OPTION_readconfig:
3540                 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3541                 break;
3542             case QEMU_OPTION_spice:
3543                 olist = qemu_find_opts_err("spice", NULL);
3544                 if (!olist) {
3545                     error_report("spice support is disabled");
3546                     exit(1);
3547                 }
3548                 opts = qemu_opts_parse_noisily(olist, optarg, false);
3549                 if (!opts) {
3550                     exit(1);
3551                 }
3552                 display_remote++;
3553                 break;
3554             case QEMU_OPTION_writeconfig:
3555                 {
3556                     FILE *fp;
3557                     warn_report("-writeconfig is deprecated and will go away without a replacement");
3558                     if (strcmp(optarg, "-") == 0) {
3559                         fp = stdout;
3560                     } else {
3561                         fp = fopen(optarg, "w");
3562                         if (fp == NULL) {
3563                             error_report("open %s: %s", optarg,
3564                                          strerror(errno));
3565                             exit(1);
3566                         }
3567                     }
3568                     qemu_config_write(fp);
3569                     if (fp != stdout) {
3570                         fclose(fp);
3571                     }
3572                     break;
3573                 }
3574             case QEMU_OPTION_qtest:
3575                 qtest_chrdev = optarg;
3576                 break;
3577             case QEMU_OPTION_qtest_log:
3578                 qtest_log = optarg;
3579                 break;
3580             case QEMU_OPTION_sandbox:
3581                 olist = qemu_find_opts("sandbox");
3582                 if (!olist) {
3583 #ifndef CONFIG_SECCOMP
3584                     error_report("-sandbox support is not enabled "
3585                                  "in this QEMU binary");
3586 #endif
3587                     exit(1);
3588                 }
3589
3590                 opts = qemu_opts_parse_noisily(olist, optarg, true);
3591                 if (!opts) {
3592                     exit(1);
3593                 }
3594                 break;
3595             case QEMU_OPTION_add_fd:
3596 #ifndef _WIN32
3597                 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3598                                                optarg, false);
3599                 if (!opts) {
3600                     exit(1);
3601                 }
3602 #else
3603                 error_report("File descriptor passing is disabled on this "
3604                              "platform");
3605                 exit(1);
3606 #endif
3607                 break;
3608             case QEMU_OPTION_object:
3609                 object_option_parse(optarg);
3610                 break;
3611             case QEMU_OPTION_overcommit:
3612                 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3613                                                optarg, false);
3614                 if (!opts) {
3615                     exit(1);
3616                 }
3617                 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3618                 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3619                 break;
3620             case QEMU_OPTION_compat:
3621                 {
3622                     CompatPolicy *opts;
3623                     Visitor *v;
3624
3625                     v = qobject_input_visitor_new_str(optarg, NULL,
3626                                                       &error_fatal);
3627
3628                     visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3629                     QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3630
3631                     qapi_free_CompatPolicy(opts);
3632                     visit_free(v);
3633                     break;
3634                 }
3635             case QEMU_OPTION_msg:
3636                 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3637                                                false);
3638                 if (!opts) {
3639                     exit(1);
3640                 }
3641                 configure_msg(opts);
3642                 break;
3643             case QEMU_OPTION_dump_vmstate:
3644                 if (vmstate_dump_file) {
3645                     error_report("only one '-dump-vmstate' "
3646                                  "option may be given");
3647                     exit(1);
3648                 }
3649                 vmstate_dump_file = fopen(optarg, "w");
3650                 if (vmstate_dump_file == NULL) {
3651                     error_report("open %s: %s", optarg, strerror(errno));
3652                     exit(1);
3653                 }
3654                 break;
3655             case QEMU_OPTION_enable_sync_profile:
3656                 qsp_enable();
3657                 break;
3658             case QEMU_OPTION_nouserconfig:
3659                 /* Nothing to be parsed here. Especially, do not error out below. */
3660                 break;
3661             default:
3662                 if (os_parse_cmd_args(popt->index, optarg)) {
3663                     error_report("Option not supported in this build");
3664                     exit(1);
3665                 }
3666             }
3667         }
3668     }
3669     /*
3670      * Clear error location left behind by the loop.
3671      * Best done right after the loop.  Do not insert code here!
3672      */
3673     loc_set_none();
3674
3675     qemu_validate_options(machine_opts_dict);
3676     qemu_process_sugar_options();
3677
3678     /*
3679      * These options affect everything else and should be processed
3680      * before daemonizing.
3681      */
3682     qemu_process_early_options();
3683
3684     qemu_process_help_options();
3685     qemu_maybe_daemonize(pid_file);
3686
3687     /*
3688      * The trace backend must be initialized after daemonizing.
3689      * trace_init_backends() will call st_init(), which will create the
3690      * trace thread in the parent, and also register st_flush_trace_buffer()
3691      * in atexit(). This function will force the parent to wait for the
3692      * writeout thread to finish, which will not occur, and the parent
3693      * process will be left in the host.
3694      */
3695     if (!trace_init_backends()) {
3696         exit(1);
3697     }
3698     trace_init_file();
3699
3700     qemu_init_main_loop(&error_fatal);
3701     cpu_timers_init();
3702
3703     user_register_global_props();
3704     replay_configure(icount_opts);
3705
3706     configure_rtc(qemu_find_opts_singleton("rtc"));
3707
3708     qemu_create_machine(machine_opts_dict);
3709
3710     suspend_mux_open();
3711
3712     qemu_disable_default_devices();
3713     qemu_create_default_devices();
3714     qemu_create_early_backends();
3715
3716     qemu_apply_legacy_machine_options(machine_opts_dict);
3717     qemu_apply_machine_options(machine_opts_dict);
3718     qobject_unref(machine_opts_dict);
3719     phase_advance(PHASE_MACHINE_CREATED);
3720
3721     /*
3722      * Note: uses machine properties such as kernel-irqchip, must run
3723      * after qemu_apply_machine_options.
3724      */
3725     configure_accelerators(argv[0]);
3726     phase_advance(PHASE_ACCEL_CREATED);
3727
3728     /*
3729      * Beware, QOM objects created before this point miss global and
3730      * compat properties.
3731      *
3732      * Global properties get set up by qdev_prop_register_global(),
3733      * called from user_register_global_props(), and certain option
3734      * desugaring.  Also in CPU feature desugaring (buried in
3735      * parse_cpu_option()), which happens below this point, but may
3736      * only target the CPU type, which can only be created after
3737      * parse_cpu_option() returned the type.
3738      *
3739      * Machine compat properties: object_set_machine_compat_props().
3740      * Accelerator compat props: object_set_accelerator_compat_props(),
3741      * called from do_configure_accelerator().
3742      */
3743
3744     machine_class = MACHINE_GET_CLASS(current_machine);
3745     if (!qtest_enabled() && machine_class->deprecation_reason) {
3746         error_report("Machine type '%s' is deprecated: %s",
3747                      machine_class->name, machine_class->deprecation_reason);
3748     }
3749
3750     /*
3751      * Note: creates a QOM object, must run only after global and
3752      * compat properties have been set up.
3753      */
3754     migration_object_init();
3755
3756     qemu_create_late_backends();
3757
3758     /* parse features once if machine provides default cpu_type */
3759     current_machine->cpu_type = machine_class->default_cpu_type;
3760     if (cpu_option) {
3761         current_machine->cpu_type = parse_cpu_option(cpu_option);
3762     }
3763     /* NB: for machine none cpu_type could STILL be NULL here! */
3764
3765     qemu_resolve_machine_memdev();
3766     parse_numa_opts(current_machine);
3767
3768     if (vmstate_dump_file) {
3769         /* dump and exit */
3770         module_load_qom_all();
3771         dump_vmstate_json_to_file(vmstate_dump_file);
3772         exit(0);
3773     }
3774
3775     if (!preconfig_requested) {
3776         qmp_x_exit_preconfig(&error_fatal);
3777     }
3778     qemu_init_displays();
3779     accel_setup_post(current_machine);
3780     os_setup_post();
3781     resume_mux_open();
3782 }
This page took 0.238059 seconds and 4 git commands to generate.