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