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