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