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