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
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "hw/boards.h"
31 #include "hw/qdev-properties.h"
32 #include "qapi/compat-policy.h"
33 #include "qapi/error.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qjson.h"
36 #include "qemu-version.h"
37 #include "qemu/cutils.h"
38 #include "qemu/help_option.h"
39 #include "qemu/uuid.h"
40 #include "sysemu/reset.h"
41 #include "sysemu/runstate.h"
42 #include "sysemu/runstate-action.h"
43 #include "sysemu/seccomp.h"
44 #include "sysemu/tcg.h"
45 #include "sysemu/xen.h"
47 #include "qemu/error-report.h"
48 #include "qemu/sockets.h"
49 #include "qemu/accel.h"
51 #include "hw/isa/isa.h"
52 #include "hw/scsi/scsi.h"
53 #include "hw/display/vga.h"
54 #include "sysemu/watchdog.h"
55 #include "hw/firmware/smbios.h"
56 #include "hw/acpi/acpi.h"
57 #include "hw/xen/xen.h"
58 #include "hw/loader.h"
59 #include "monitor/qdev.h"
61 #include "net/slirp.h"
62 #include "monitor/monitor.h"
63 #include "ui/console.h"
65 #include "sysemu/sysemu.h"
66 #include "sysemu/numa.h"
67 #include "sysemu/hostmem.h"
68 #include "exec/gdbstub.h"
69 #include "qemu/timer.h"
70 #include "chardev/char.h"
71 #include "qemu/bitmap.h"
73 #include "sysemu/blockdev.h"
74 #include "hw/block/block.h"
75 #include "hw/i386/x86.h"
76 #include "hw/i386/pc.h"
77 #include "migration/misc.h"
78 #include "migration/snapshot.h"
79 #include "sysemu/tpm.h"
80 #include "sysemu/dma.h"
81 #include "hw/audio/soundhw.h"
82 #include "audio/audio.h"
83 #include "sysemu/cpus.h"
84 #include "sysemu/cpu-timers.h"
85 #include "migration/colo.h"
86 #include "migration/postcopy-ram.h"
87 #include "sysemu/kvm.h"
88 #include "sysemu/hax.h"
89 #include "qapi/qobject-input-visitor.h"
90 #include "qemu/option.h"
91 #include "qemu/config-file.h"
92 #include "qemu-options.h"
93 #include "qemu/main-loop.h"
95 #include "fsdev/qemu-fsdev.h"
97 #include "sysemu/qtest.h"
99 #include "disas/disas.h"
102 #include "trace/control.h"
103 #include "qemu/plugin.h"
104 #include "qemu/queue.h"
105 #include "sysemu/arch_init.h"
106 #include "exec/confidential-guest-support.h"
108 #include "ui/qemu-spice.h"
109 #include "qapi/string-input-visitor.h"
110 #include "qapi/opts-visitor.h"
111 #include "qapi/clone-visitor.h"
112 #include "qom/object_interfaces.h"
113 #include "semihosting/semihost.h"
114 #include "crypto/init.h"
115 #include "sysemu/replay.h"
116 #include "qapi/qapi-events-run-state.h"
117 #include "qapi/qapi-visit-block-core.h"
118 #include "qapi/qapi-visit-compat.h"
119 #include "qapi/qapi-visit-ui.h"
120 #include "qapi/qapi-commands-block-core.h"
121 #include "qapi/qapi-commands-migration.h"
122 #include "qapi/qapi-commands-misc.h"
123 #include "qapi/qapi-visit-qom.h"
124 #include "qapi/qapi-commands-ui.h"
125 #include "qapi/qmp/qerror.h"
126 #include "sysemu/iothread.h"
127 #include "qemu/guest-random.h"
129 #define MAX_VIRTIO_CONSOLES 1
131 typedef struct BlockdevOptionsQueueEntry {
132 BlockdevOptions *bdo;
134 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
135 } BlockdevOptionsQueueEntry;
137 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
139 typedef struct ObjectOption {
141 QTAILQ_ENTRY(ObjectOption) next;
144 static const char *cpu_option;
145 static const char *mem_path;
146 static const char *incoming;
147 static const char *loadvm;
148 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
149 static ram_addr_t maxram_size;
150 static uint64_t ram_slots;
151 static int display_remote;
153 static bool preconfig_requested;
154 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
155 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
156 static bool nographic = false;
157 static int mem_prealloc; /* force preallocation of physical target memory */
158 static ram_addr_t ram_size;
159 static const char *vga_model = NULL;
160 static DisplayOptions dpy;
161 static int num_serial_hds;
162 static Chardev **serial_hds;
163 static const char *log_mask;
164 static const char *log_file;
165 static bool list_data_dirs;
166 static const char *watchdog;
167 static const char *qtest_chrdev;
168 static const char *qtest_log;
170 static int has_defaults = 1;
171 static int default_serial = 1;
172 static int default_parallel = 1;
173 static int default_monitor = 1;
174 static int default_floppy = 1;
175 static int default_cdrom = 1;
176 static int default_sdcard = 1;
177 static int default_vga = 1;
178 static int default_net = 1;
184 { .driver = "isa-serial", .flag = &default_serial },
185 { .driver = "isa-parallel", .flag = &default_parallel },
186 { .driver = "isa-fdc", .flag = &default_floppy },
187 { .driver = "floppy", .flag = &default_floppy },
188 { .driver = "ide-cd", .flag = &default_cdrom },
189 { .driver = "ide-hd", .flag = &default_cdrom },
190 { .driver = "scsi-cd", .flag = &default_cdrom },
191 { .driver = "scsi-hd", .flag = &default_cdrom },
192 { .driver = "VGA", .flag = &default_vga },
193 { .driver = "isa-vga", .flag = &default_vga },
194 { .driver = "cirrus-vga", .flag = &default_vga },
195 { .driver = "isa-cirrus-vga", .flag = &default_vga },
196 { .driver = "vmware-svga", .flag = &default_vga },
197 { .driver = "qxl-vga", .flag = &default_vga },
198 { .driver = "virtio-vga", .flag = &default_vga },
199 { .driver = "ati-vga", .flag = &default_vga },
200 { .driver = "vhost-user-vga", .flag = &default_vga },
203 static QemuOptsList qemu_rtc_opts = {
205 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
210 .type = QEMU_OPT_STRING,
213 .type = QEMU_OPT_STRING,
216 .type = QEMU_OPT_STRING,
218 { /* end of list */ }
222 static QemuOptsList qemu_option_rom_opts = {
223 .name = "option-rom",
224 .implied_opt_name = "romfile",
225 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
229 .type = QEMU_OPT_NUMBER,
232 .type = QEMU_OPT_STRING,
234 { /* end of list */ }
238 static QemuOptsList qemu_machine_opts = {
240 .implied_opt_name = "type",
242 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
245 * no elements => accept any
246 * sanity checking will happen later
247 * when setting machine properties
253 static QemuOptsList qemu_accel_opts = {
255 .implied_opt_name = "accel",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
259 * no elements => accept any
260 * sanity checking will happen later
261 * when setting accelerator properties
267 static QemuOptsList qemu_boot_opts = {
269 .implied_opt_name = "order",
271 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
275 .type = QEMU_OPT_STRING,
278 .type = QEMU_OPT_STRING,
281 .type = QEMU_OPT_BOOL,
284 .type = QEMU_OPT_STRING,
286 .name = "splash-time",
287 .type = QEMU_OPT_NUMBER,
289 .name = "reboot-timeout",
290 .type = QEMU_OPT_NUMBER,
293 .type = QEMU_OPT_BOOL,
299 static QemuOptsList qemu_add_fd_opts = {
301 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
305 .type = QEMU_OPT_NUMBER,
306 .help = "file descriptor of which a duplicate is added to fd set",
309 .type = QEMU_OPT_NUMBER,
310 .help = "ID of the fd set to add fd to",
313 .type = QEMU_OPT_STRING,
314 .help = "free-form string used to describe fd",
316 { /* end of list */ }
320 static QemuOptsList qemu_object_opts = {
322 .implied_opt_name = "qom-type",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
329 static QemuOptsList qemu_tpmdev_opts = {
331 .implied_opt_name = "type",
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
334 /* options are defined in the TPM backends */
335 { /* end of list */ }
339 static QemuOptsList qemu_overcommit_opts = {
340 .name = "overcommit",
341 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
345 .type = QEMU_OPT_BOOL,
349 .type = QEMU_OPT_BOOL,
351 { /* end of list */ }
355 static QemuOptsList qemu_msg_opts = {
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
361 .type = QEMU_OPT_BOOL,
364 .name = "guest-name",
365 .type = QEMU_OPT_BOOL,
366 .help = "Prepends guest name for error messages but only if "
367 "-name guest is set otherwise option is ignored\n",
369 { /* end of list */ }
373 static QemuOptsList qemu_name_opts = {
375 .implied_opt_name = "guest",
377 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
381 .type = QEMU_OPT_STRING,
382 .help = "Sets the name of the guest.\n"
383 "This name will be displayed in the SDL window caption.\n"
384 "The name will also be used for the VNC server",
387 .type = QEMU_OPT_STRING,
388 .help = "Sets the name of the QEMU process, as shown in top etc",
390 .name = "debug-threads",
391 .type = QEMU_OPT_BOOL,
392 .help = "When enabled, name the individual threads; defaults off.\n"
393 "NOTE: The thread names are for debugging and not a\n"
396 { /* End of list */ }
400 static QemuOptsList qemu_mem_opts = {
402 .implied_opt_name = "size",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
408 .type = QEMU_OPT_SIZE,
412 .type = QEMU_OPT_NUMBER,
416 .type = QEMU_OPT_SIZE,
418 { /* end of list */ }
422 static QemuOptsList qemu_icount_opts = {
424 .implied_opt_name = "shift",
426 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
430 .type = QEMU_OPT_STRING,
433 .type = QEMU_OPT_BOOL,
436 .type = QEMU_OPT_BOOL,
439 .type = QEMU_OPT_STRING,
442 .type = QEMU_OPT_STRING,
444 .name = "rrsnapshot",
445 .type = QEMU_OPT_STRING,
447 { /* end of list */ }
451 static QemuOptsList qemu_fw_cfg_opts = {
453 .implied_opt_name = "name",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
458 .type = QEMU_OPT_STRING,
459 .help = "Sets the fw_cfg name of the blob to be inserted",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets the name of the file from which "
464 "the fw_cfg blob will be loaded",
467 .type = QEMU_OPT_STRING,
468 .help = "Sets content of the blob to be inserted from a string",
471 .type = QEMU_OPT_STRING,
472 .help = "Sets id of the object generating the fw_cfg blob "
475 { /* end of list */ }
479 static QemuOptsList qemu_action_opts = {
482 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
486 .type = QEMU_OPT_STRING,
489 .type = QEMU_OPT_STRING,
492 .type = QEMU_OPT_STRING,
495 .type = QEMU_OPT_STRING,
497 { /* end of list */ }
502 * Get machine options
504 * Returns: machine options (never null).
506 static QemuOpts *qemu_get_machine_opts(void)
508 return qemu_find_opts_singleton("machine");
511 const char *qemu_get_vm_name(void)
516 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
518 const char *driver = qemu_opt_get(opts, "driver");
523 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
524 if (strcmp(default_list[i].driver, driver) != 0)
526 *(default_list[i].flag) = 0;
531 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
533 const char *proc_name;
535 if (qemu_opt_get(opts, "debug-threads")) {
536 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
538 qemu_name = qemu_opt_get(opts, "guest");
540 proc_name = qemu_opt_get(opts, "process");
542 os_set_proc_name(proc_name);
548 bool defaults_enabled(void)
554 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
556 int fd, dupfd, flags;
558 const char *fd_opaque = NULL;
561 fd = qemu_opt_get_number(opts, "fd", -1);
562 fdset_id = qemu_opt_get_number(opts, "set", -1);
563 fd_opaque = qemu_opt_get(opts, "opaque");
566 error_setg(errp, "fd option is required and must be non-negative");
570 if (fd <= STDERR_FILENO) {
571 error_setg(errp, "fd cannot be a standard I/O stream");
576 * All fds inherited across exec() necessarily have FD_CLOEXEC
577 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
579 flags = fcntl(fd, F_GETFD);
580 if (flags == -1 || (flags & FD_CLOEXEC)) {
581 error_setg(errp, "fd is not valid or already in use");
586 error_setg(errp, "set option is required and must be non-negative");
590 #ifdef F_DUPFD_CLOEXEC
591 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
595 qemu_set_cloexec(dupfd);
599 error_setg(errp, "error duplicating fd: %s", strerror(errno));
603 /* add the duplicate fd, and optionally the opaque string, to the fd set */
604 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
611 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
615 fd = qemu_opt_get_number(opts, "fd", -1);
622 /***********************************************************/
623 /* QEMU Block devices */
625 #define HD_OPTS "media=disk"
626 #define CDROM_OPTS "media=cdrom"
628 #define PFLASH_OPTS ""
632 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
634 BlockInterfaceType *block_default_type = opaque;
636 return drive_new(opts, *block_default_type, errp) == NULL;
639 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
641 if (qemu_opt_get(opts, "snapshot") == NULL) {
642 qemu_opt_set(opts, "snapshot", "on", &error_abort);
647 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
648 int index, const char *optstr)
653 if (!enable || drive_get_by_index(type, index)) {
657 opts = drive_add(type, index, NULL, optstr);
659 drive_enable_snapshot(NULL, opts, NULL);
662 dinfo = drive_new(opts, type, &error_abort);
663 dinfo->is_default = true;
667 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
668 MachineClass *machine_class, int snapshot)
671 * If the currently selected machine wishes to override the
672 * units-per-bus property of its default HBA interface type, do so
675 if (machine_class->units_per_default_bus) {
676 override_max_devs(machine_class->block_default_type,
677 machine_class->units_per_default_bus);
680 /* open the virtual block devices */
681 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
682 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
684 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
685 loc_push_restore(&bdo->loc);
686 qmp_blockdev_add(bdo->bdo, &error_fatal);
688 qapi_free_BlockdevOptions(bdo->bdo);
692 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
695 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
696 &machine_class->block_default_type, &error_fatal)) {
697 /* We printed help */
701 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
703 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
704 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
708 static QemuOptsList qemu_smp_opts = {
710 .implied_opt_name = "cpus",
712 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
716 .type = QEMU_OPT_NUMBER,
719 .type = QEMU_OPT_NUMBER,
722 .type = QEMU_OPT_NUMBER,
725 .type = QEMU_OPT_NUMBER,
728 .type = QEMU_OPT_NUMBER,
731 .type = QEMU_OPT_NUMBER,
737 static void realtime_init(void)
740 if (os_mlock() < 0) {
741 error_report("locking memory failed");
748 static void configure_msg(QemuOpts *opts)
750 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
751 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
755 /***********************************************************/
758 static int usb_device_add(const char *devname)
760 USBDevice *dev = NULL;
762 if (!machine_usb(current_machine)) {
766 dev = usbdevice_create(devname);
773 static int usb_parse(const char *cmdline)
776 r = usb_device_add(cmdline);
778 error_report("could not add USB device '%s'", cmdline);
783 /***********************************************************/
784 /* machine registration */
786 static MachineClass *find_machine(const char *name, GSList *machines)
790 for (el = machines; el; el = el->next) {
791 MachineClass *mc = el->data;
793 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
801 static MachineClass *find_default_machine(GSList *machines)
804 MachineClass *default_machineclass = NULL;
806 for (el = machines; el; el = el->next) {
807 MachineClass *mc = el->data;
809 if (mc->is_default) {
810 assert(default_machineclass == NULL && "Multiple default machines");
811 default_machineclass = mc;
815 return default_machineclass;
818 static int machine_help_func(QemuOpts *opts, MachineState *machine)
820 ObjectProperty *prop;
821 ObjectPropertyIterator iter;
823 if (!qemu_opt_has_help_opt(opts)) {
827 object_property_iter_init(&iter, OBJECT(machine));
828 while ((prop = object_property_iter_next(&iter))) {
833 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
834 prop->name, prop->type);
835 if (prop->description) {
836 printf(" (%s)\n", prop->description);
845 static void version(void)
847 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
848 QEMU_COPYRIGHT "\n");
851 static void help(int exitcode)
854 printf("usage: %s [options] [disk_image]\n\n"
855 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
856 error_get_progname());
858 #define QEMU_OPTIONS_GENERATE_HELP
859 #include "qemu-options-wrapper.h"
861 printf("\nDuring emulation, the following keys are useful:\n"
862 "ctrl-alt-f toggle full screen\n"
863 "ctrl-alt-n switch to virtual console 'n'\n"
864 "ctrl-alt toggle mouse and keyboard grab\n"
866 "When using -nographic, press 'ctrl-a h' to get some help.\n"
868 QEMU_HELP_BOTTOM "\n");
873 #define HAS_ARG 0x0001
875 typedef struct QEMUOption {
882 static const QEMUOption qemu_options[] = {
883 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
884 #define QEMU_OPTIONS_GENERATE_OPTIONS
885 #include "qemu-options-wrapper.h"
889 typedef struct VGAInterfaceInfo {
890 const char *opt_name; /* option name */
891 const char *name; /* human-readable name */
892 /* Class names indicating that support is available.
893 * If no class is specified, the interface is always available */
894 const char *class_names[2];
897 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
900 .name = "no graphic card",
904 .name = "standard VGA",
905 .class_names = { "VGA", "isa-vga" },
908 .opt_name = "cirrus",
909 .name = "Cirrus VGA",
910 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
913 .opt_name = "vmware",
914 .name = "VMWare SVGA",
915 .class_names = { "vmware-svga" },
918 .opt_name = "virtio",
919 .name = "Virtio VGA",
920 .class_names = { "virtio-vga" },
925 .class_names = { "qxl-vga" },
929 .name = "TCX framebuffer",
930 .class_names = { "sun-tcx" },
934 .name = "CG3 framebuffer",
935 .class_names = { "cgthree" },
939 .name = "Xen paravirtualized framebuffer",
943 static bool vga_interface_available(VGAInterfaceType t)
945 const VGAInterfaceInfo *ti = &vga_interfaces[t];
947 assert(t < VGA_TYPE_MAX);
948 return !ti->class_names[0] ||
949 module_object_class_by_name(ti->class_names[0]) ||
950 module_object_class_by_name(ti->class_names[1]);
954 get_default_vga_model(const MachineClass *machine_class)
956 if (machine_class->default_display) {
957 return machine_class->default_display;
958 } else if (vga_interface_available(VGA_CIRRUS)) {
960 } else if (vga_interface_available(VGA_STD)) {
967 static void select_vgahw(const MachineClass *machine_class, const char *p)
972 if (g_str_equal(p, "help")) {
973 const char *def = get_default_vga_model(machine_class);
975 for (t = 0; t < VGA_TYPE_MAX; t++) {
976 const VGAInterfaceInfo *ti = &vga_interfaces[t];
978 if (vga_interface_available(t) && ti->opt_name) {
979 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
980 g_str_equal(ti->opt_name, def) ? " (default)" : "");
986 assert(vga_interface_type == VGA_NONE);
987 for (t = 0; t < VGA_TYPE_MAX; t++) {
988 const VGAInterfaceInfo *ti = &vga_interfaces[t];
989 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
990 if (!vga_interface_available(t)) {
991 error_report("%s not available", ti->name);
994 vga_interface_type = t;
998 if (t == VGA_TYPE_MAX) {
1000 error_report("unknown vga type: %s", p);
1004 const char *nextopt;
1006 if (strstart(opts, ",retrace=", &nextopt)) {
1008 if (strstart(opts, "dumb", &nextopt))
1009 vga_retrace_method = VGA_RETRACE_DUMB;
1010 else if (strstart(opts, "precise", &nextopt))
1011 vga_retrace_method = VGA_RETRACE_PRECISE;
1012 else goto invalid_vga;
1013 } else goto invalid_vga;
1018 static void parse_display_qapi(const char *optarg)
1020 DisplayOptions *opts;
1023 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1025 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1026 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1028 qapi_free_DisplayOptions(opts);
1032 DisplayOptions *qmp_query_display_options(Error **errp)
1034 return QAPI_CLONE(DisplayOptions, &dpy);
1037 static void parse_display(const char *p)
1041 if (is_help_option(p)) {
1042 qemu_display_help();
1046 if (strstart(p, "sdl", &opts)) {
1048 * sdl DisplayType needs hand-crafted parser instead of
1049 * parse_display_qapi() due to some options not in
1050 * DisplayOptions, specifically:
1052 * Already deprecated.
1053 * - ctrl_grab + alt_grab
1054 * Not clear yet what happens to them long-term. Should
1055 * replaced by something better or deprecated and dropped.
1057 dpy.type = DISPLAY_TYPE_SDL;
1059 const char *nextopt;
1061 if (strstart(opts, ",alt_grab=", &nextopt)) {
1063 if (strstart(opts, "on", &nextopt)) {
1065 } else if (strstart(opts, "off", &nextopt)) {
1068 goto invalid_sdl_args;
1070 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1072 if (strstart(opts, "on", &nextopt)) {
1074 } else if (strstart(opts, "off", &nextopt)) {
1077 goto invalid_sdl_args;
1079 } else if (strstart(opts, ",window_close=", &nextopt)) {
1081 dpy.has_window_close = true;
1082 if (strstart(opts, "on", &nextopt)) {
1083 dpy.window_close = true;
1084 } else if (strstart(opts, "off", &nextopt)) {
1085 dpy.window_close = false;
1087 goto invalid_sdl_args;
1089 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1091 dpy.has_show_cursor = true;
1092 if (strstart(opts, "on", &nextopt)) {
1093 dpy.show_cursor = true;
1094 } else if (strstart(opts, "off", &nextopt)) {
1095 dpy.show_cursor = false;
1097 goto invalid_sdl_args;
1099 } else if (strstart(opts, ",gl=", &nextopt)) {
1102 if (strstart(opts, "on", &nextopt)) {
1103 dpy.gl = DISPLAYGL_MODE_ON;
1104 } else if (strstart(opts, "core", &nextopt)) {
1105 dpy.gl = DISPLAYGL_MODE_CORE;
1106 } else if (strstart(opts, "es", &nextopt)) {
1107 dpy.gl = DISPLAYGL_MODE_ES;
1108 } else if (strstart(opts, "off", &nextopt)) {
1109 dpy.gl = DISPLAYGL_MODE_OFF;
1111 goto invalid_sdl_args;
1115 error_report("invalid SDL option string");
1120 } else if (strstart(p, "vnc", &opts)) {
1122 * vnc isn't a (local) DisplayType but a protocol for remote
1126 vnc_parse(opts + 1);
1128 error_report("VNC requires a display argument vnc=<display>");
1132 parse_display_qapi(p);
1136 static inline bool nonempty_str(const char *str)
1141 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1145 const char *name, *file, *str, *gen_id;
1146 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1148 if (fw_cfg == NULL) {
1149 error_setg(errp, "fw_cfg device not available");
1152 name = qemu_opt_get(opts, "name");
1153 file = qemu_opt_get(opts, "file");
1154 str = qemu_opt_get(opts, "string");
1155 gen_id = qemu_opt_get(opts, "gen_id");
1157 /* we need the name, and exactly one of: file, content string, gen_id */
1158 if (!nonempty_str(name) ||
1159 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1160 error_setg(errp, "name, plus exactly one of file,"
1161 " string and gen_id, are needed");
1164 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1165 error_setg(errp, "name too long (max. %d char)",
1166 FW_CFG_MAX_FILE_PATH - 1);
1169 if (nonempty_str(gen_id)) {
1171 * In this particular case where the content is populated
1172 * internally, the "etc/" namespace protection is relaxed,
1173 * so do not emit a warning.
1175 } else if (strncmp(name, "opt/", 4) != 0) {
1176 warn_report("externally provided fw_cfg item names "
1177 "should be prefixed with \"opt/\"");
1179 if (nonempty_str(str)) {
1180 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1181 buf = g_memdup(str, size);
1182 } else if (nonempty_str(gen_id)) {
1183 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1189 if (!g_file_get_contents(file, &buf, &size, &err)) {
1190 error_setg(errp, "can't load %s: %s", file, err->message);
1195 /* For legacy, keep user files in a specific global order. */
1196 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1197 fw_cfg_add_file(fw_cfg, name, buf, size);
1198 fw_cfg_reset_order_override(fw_cfg);
1202 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1204 return qdev_device_help(opts);
1207 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1211 dev = qdev_device_add(opts, errp);
1212 if (!dev && *errp) {
1213 error_report_err(*errp);
1216 object_unref(OBJECT(dev));
1221 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1223 Error *local_err = NULL;
1225 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1227 error_propagate(errp, local_err);
1235 #ifdef CONFIG_VIRTFS
1236 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1238 return qemu_fsdev_add(opts, errp);
1242 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1244 return monitor_init_opts(opts, errp);
1247 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1249 static int monitor_device_index = 0;
1254 if (strstart(optarg, "chardev:", &p)) {
1255 snprintf(label, sizeof(label), "%s", p);
1257 snprintf(label, sizeof(label), "compat_monitor%d",
1258 monitor_device_index);
1259 opts = qemu_chr_parse_compat(label, optarg, true);
1261 error_report("parse error: %s", optarg);
1266 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1267 qemu_opt_set(opts, "mode", mode, &error_abort);
1268 qemu_opt_set(opts, "chardev", label, &error_abort);
1269 if (!strcmp(mode, "control")) {
1270 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1272 assert(pretty == false);
1274 monitor_device_index++;
1277 struct device_config {
1279 DEV_USB, /* -usbdevice */
1280 DEV_SERIAL, /* -serial */
1281 DEV_PARALLEL, /* -parallel */
1282 DEV_DEBUGCON, /* -debugcon */
1283 DEV_GDB, /* -gdb, -s */
1284 DEV_SCLP, /* s390 sclp */
1286 const char *cmdline;
1288 QTAILQ_ENTRY(device_config) next;
1291 static QTAILQ_HEAD(, device_config) device_configs =
1292 QTAILQ_HEAD_INITIALIZER(device_configs);
1294 static void add_device_config(int type, const char *cmdline)
1296 struct device_config *conf;
1298 conf = g_malloc0(sizeof(*conf));
1300 conf->cmdline = cmdline;
1301 loc_save(&conf->loc);
1302 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1305 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1307 struct device_config *conf;
1310 QTAILQ_FOREACH(conf, &device_configs, next) {
1311 if (conf->type != type)
1313 loc_push_restore(&conf->loc);
1314 rc = func(conf->cmdline);
1315 loc_pop(&conf->loc);
1323 static void qemu_disable_default_devices(void)
1325 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1327 qemu_opts_foreach(qemu_find_opts("device"),
1328 default_driver_check, NULL, NULL);
1329 qemu_opts_foreach(qemu_find_opts("global"),
1330 default_driver_check, NULL, NULL);
1332 if (!vga_model && !default_vga) {
1333 vga_interface_type = VGA_DEVICE;
1335 if (!has_defaults || machine_class->no_serial) {
1338 if (!has_defaults || machine_class->no_parallel) {
1339 default_parallel = 0;
1341 if (!has_defaults || machine_class->no_floppy) {
1344 if (!has_defaults || machine_class->no_cdrom) {
1347 if (!has_defaults || machine_class->no_sdcard) {
1350 if (!has_defaults) {
1351 default_monitor = 0;
1357 static void qemu_create_default_devices(void)
1359 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1361 if (is_daemonized()) {
1362 /* According to documentation and historically, -nographic redirects
1363 * serial port, parallel port and monitor to stdio, which does not work
1364 * with -daemonize. We can redirect these to null instead, but since
1365 * -nographic is legacy, let's just error out.
1366 * We disallow -nographic only if all other ports are not redirected
1367 * explicitly, to not break existing legacy setups which uses
1368 * -nographic _and_ redirects all ports explicitly - this is valid
1369 * usage, -nographic is just a no-op in this case.
1372 && (default_parallel || default_serial || default_monitor)) {
1373 error_report("-nographic cannot be used with -daemonize");
1379 if (default_parallel)
1380 add_device_config(DEV_PARALLEL, "null");
1381 if (default_serial && default_monitor) {
1382 add_device_config(DEV_SERIAL, "mon:stdio");
1385 add_device_config(DEV_SERIAL, "stdio");
1386 if (default_monitor)
1387 monitor_parse("stdio", "readline", false);
1391 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1392 if (default_parallel)
1393 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1394 if (default_monitor)
1395 monitor_parse("vc:80Cx24C", "readline", false);
1399 QemuOptsList *net = qemu_find_opts("net");
1400 qemu_opts_parse(net, "nic", true, &error_abort);
1402 qemu_opts_parse(net, "user", true, &error_abort);
1406 #if defined(CONFIG_VNC)
1407 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1411 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1412 if (!qemu_display_find_default(&dpy)) {
1413 dpy.type = DISPLAY_TYPE_NONE;
1414 #if defined(CONFIG_VNC)
1415 vnc_parse("localhost:0,to=99,id=default");
1419 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1420 dpy.type = DISPLAY_TYPE_NONE;
1423 /* If no default VGA is requested, the default is "none". */
1425 vga_model = get_default_vga_model(machine_class);
1428 select_vgahw(machine_class, vga_model);
1432 static int serial_parse(const char *devname)
1434 int index = num_serial_hds;
1437 if (strcmp(devname, "none") == 0)
1439 snprintf(label, sizeof(label), "serial%d", index);
1440 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1442 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1443 if (!serial_hds[index]) {
1444 error_report("could not connect serial device"
1445 " to character backend '%s'", devname);
1452 Chardev *serial_hd(int i)
1455 if (i < num_serial_hds) {
1456 return serial_hds[i];
1461 static int parallel_parse(const char *devname)
1463 static int index = 0;
1466 if (strcmp(devname, "none") == 0)
1468 if (index == MAX_PARALLEL_PORTS) {
1469 error_report("too many parallel ports");
1472 snprintf(label, sizeof(label), "parallel%d", index);
1473 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1474 if (!parallel_hds[index]) {
1475 error_report("could not connect parallel device"
1476 " to character backend '%s'", devname);
1483 static int debugcon_parse(const char *devname)
1487 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1488 error_report("invalid character backend '%s'", devname);
1491 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1493 error_report("already have a debugcon device");
1496 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1497 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1501 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1503 const MachineClass *mc1 = a, *mc2 = b;
1506 if (mc1->family == NULL) {
1507 if (mc2->family == NULL) {
1508 /* Compare standalone machine types against each other; they sort
1509 * in increasing order.
1511 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1512 object_class_get_name(OBJECT_CLASS(mc2)));
1515 /* Standalone machine types sort after families. */
1519 if (mc2->family == NULL) {
1520 /* Families sort before standalone machine types. */
1524 /* Families sort between each other alphabetically increasingly. */
1525 res = strcmp(mc1->family, mc2->family);
1530 /* Within the same family, machine types sort in decreasing order. */
1531 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1532 object_class_get_name(OBJECT_CLASS(mc1)));
1535 static MachineClass *machine_parse(const char *name, GSList *machines)
1540 if (is_help_option(name)) {
1541 printf("Supported machines are:\n");
1542 machines = g_slist_sort(machines, machine_class_cmp);
1543 for (el = machines; el; el = el->next) {
1544 MachineClass *mc = el->data;
1546 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1548 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1549 mc->is_default ? " (default)" : "",
1550 mc->deprecation_reason ? " (deprecated)" : "");
1555 mc = find_machine(name, machines);
1557 error_report("unsupported machine type");
1558 error_printf("Use -machine help to list supported machines\n");
1564 static const char *pid_file;
1565 static Notifier qemu_unlink_pidfile_notifier;
1567 static void qemu_unlink_pidfile(Notifier *n, void *data)
1574 static const QEMUOption *lookup_opt(int argc, char **argv,
1575 const char **poptarg, int *poptind)
1577 const QEMUOption *popt;
1578 int optind = *poptind;
1579 char *r = argv[optind];
1582 loc_set_cmdline(argv, optind, 1);
1584 /* Treat --foo the same as -foo. */
1587 popt = qemu_options;
1590 error_report("invalid option");
1593 if (!strcmp(popt->name, r + 1))
1597 if (popt->flags & HAS_ARG) {
1598 if (optind >= argc) {
1599 error_report("requires an argument");
1602 optarg = argv[optind++];
1603 loc_set_cmdline(argv, optind - 2, 2);
1614 static MachineClass *select_machine(void)
1616 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1617 MachineClass *machine_class = find_default_machine(machines);
1622 loc_push_none(&loc);
1624 opts = qemu_get_machine_opts();
1625 qemu_opts_loc_restore(opts);
1627 optarg = qemu_opt_get(opts, "type");
1629 machine_class = machine_parse(optarg, machines);
1632 if (!machine_class) {
1633 error_report("No machine specified, and there is no default");
1634 error_printf("Use -machine help to list supported machines\n");
1639 g_slist_free(machines);
1640 return machine_class;
1643 static int object_parse_property_opt(Object *obj,
1644 const char *name, const char *value,
1645 const char *skip, Error **errp)
1647 if (g_str_equal(name, skip)) {
1651 if (!object_property_parse(obj, name, value, errp)) {
1658 static int machine_set_property(void *opaque,
1659 const char *name, const char *value,
1662 g_autofree char *qom_name = g_strdup(name);
1665 for (p = qom_name; *p; p++) {
1671 /* Legacy options do not correspond to MachineState properties. */
1672 if (g_str_equal(qom_name, "accel")) {
1675 if (g_str_equal(qom_name, "igd-passthru")) {
1676 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value,
1680 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
1681 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1685 if (g_str_equal(qom_name, "kernel-irqchip")) {
1686 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1688 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value,
1693 return object_parse_property_opt(opaque, name, value, "type", errp);
1696 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1698 ObjectOption *opt, *next;
1700 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1701 const char *type = ObjectType_str(opt->opts->qom_type);
1702 if (type_opt_predicate(type)) {
1703 user_creatable_add_qapi(opt->opts, &error_fatal);
1704 qapi_free_ObjectOptions(opt->opts);
1705 QTAILQ_REMOVE(&object_opts, opt, next);
1711 static void object_option_parse(const char *optarg)
1718 if (optarg[0] == '{') {
1719 QObject *obj = qobject_from_json(optarg, &error_fatal);
1721 v = qobject_input_visitor_new(obj);
1724 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1730 type = qemu_opt_get(opts, "qom-type");
1732 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1734 if (user_creatable_print_help(type, opts)) {
1738 v = opts_visitor_new(opts);
1741 opt = g_new0(ObjectOption, 1);
1742 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1745 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1749 * Initial object creation happens before all other
1750 * QEMU data types are created. The majority of objects
1751 * can be created at this point. The rng-egd object
1752 * cannot be created here, as it depends on the chardev
1755 static bool object_create_early(const char *type)
1758 * Objects should not be made "delayed" without a reason. If you
1759 * add one, state the reason in a comment!
1762 /* Reason: rng-egd property "chardev" */
1763 if (g_str_equal(type, "rng-egd")) {
1767 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1768 /* Reason: cryptodev-vhost-user property "chardev" */
1769 if (g_str_equal(type, "cryptodev-vhost-user")) {
1774 /* Reason: vhost-user-blk-server property "node-name" */
1775 if (g_str_equal(type, "vhost-user-blk-server")) {
1779 * Reason: filter-* property "netdev" etc.
1781 if (g_str_equal(type, "filter-buffer") ||
1782 g_str_equal(type, "filter-dump") ||
1783 g_str_equal(type, "filter-mirror") ||
1784 g_str_equal(type, "filter-redirector") ||
1785 g_str_equal(type, "colo-compare") ||
1786 g_str_equal(type, "filter-rewriter") ||
1787 g_str_equal(type, "filter-replay")) {
1792 * Allocation of large amounts of memory may delay
1793 * chardev initialization for too long, and trigger timeouts
1794 * on software that waits for a monitor socket to be created
1797 if (g_str_has_prefix(type, "memory-backend-")) {
1804 static void qemu_apply_machine_options(void)
1806 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1807 QemuOpts *machine_opts = qemu_get_machine_opts();
1808 const char *boot_order = NULL;
1809 const char *boot_once = NULL;
1812 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
1814 current_machine->ram_size = ram_size;
1815 current_machine->maxram_size = maxram_size;
1816 current_machine->ram_slots = ram_slots;
1818 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1820 boot_order = qemu_opt_get(opts, "order");
1822 validate_bootdevices(boot_order, &error_fatal);
1825 boot_once = qemu_opt_get(opts, "once");
1827 validate_bootdevices(boot_once, &error_fatal);
1830 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1831 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1835 boot_order = machine_class->default_boot_order;
1838 current_machine->boot_order = boot_order;
1839 current_machine->boot_once = boot_once;
1841 if (semihosting_enabled() && !semihosting_get_argc()) {
1842 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
1843 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append") ?: "";
1844 /* fall back to the -kernel/-append */
1845 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
1849 static void qemu_create_early_backends(void)
1851 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1853 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
1854 error_report("-alt-grab and -ctrl-grab are only valid "
1855 "for SDL, ignoring option");
1857 if (dpy.has_window_close &&
1858 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
1859 error_report("-no-quit is only valid for GTK and SDL, "
1863 qemu_display_early_init(&dpy);
1864 qemu_console_early_init();
1866 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1867 #if defined(CONFIG_OPENGL)
1868 error_report("OpenGL is not supported by the display");
1870 error_report("OpenGL support is disabled");
1875 object_option_foreach_add(object_create_early);
1877 /* spice needs the timers to be initialized by this point */
1878 /* spice must initialize before audio as it changes the default auiodev */
1879 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1882 qemu_opts_foreach(qemu_find_opts("chardev"),
1883 chardev_init_func, NULL, &error_fatal);
1885 #ifdef CONFIG_VIRTFS
1886 qemu_opts_foreach(qemu_find_opts("fsdev"),
1887 fsdev_init_func, NULL, &error_fatal);
1891 * Note: we need to create audio and block backends before
1892 * machine_set_property(), so machine properties can refer to
1895 configure_blockdev(&bdo_queue, machine_class, snapshot);
1896 audio_init_audiodevs();
1901 * The remainder of object creation happens after the
1902 * creation of chardev, fsdev, net clients and device data types.
1904 static bool object_create_late(const char *type)
1906 return !object_create_early(type);
1909 static void qemu_create_late_backends(void)
1912 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1915 net_init_clients(&error_fatal);
1917 object_option_foreach_add(object_create_late);
1919 if (tpm_init() < 0) {
1923 qemu_opts_foreach(qemu_find_opts("mon"),
1924 mon_init_func, NULL, &error_fatal);
1926 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1928 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1930 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1933 /* now chardevs have been created we may have semihosting to connect */
1934 qemu_semihosting_connect_chardevs();
1935 qemu_semihosting_console_init();
1938 static bool have_custom_ram_size(void)
1940 QemuOpts *opts = qemu_find_opts_singleton("memory");
1941 return !!qemu_opt_get_size(opts, "size", 0);
1944 static void qemu_resolve_machine_memdev(void)
1946 if (current_machine->ram_memdev_id) {
1948 ram_addr_t backend_size;
1950 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1951 TYPE_MEMORY_BACKEND, NULL);
1953 error_report("Memory backend '%s' not found",
1954 current_machine->ram_memdev_id);
1957 backend_size = object_property_get_uint(backend, "size", &error_abort);
1958 if (have_custom_ram_size() && backend_size != ram_size) {
1959 error_report("Size specified by -m option must match size of "
1960 "explicitly specified 'memory-backend' property");
1964 error_report("'-mem-path' can't be used together with"
1965 "'-machine memory-backend'");
1968 ram_size = backend_size;
1971 if (!xen_enabled()) {
1972 /* On 32-bit hosts, QEMU is limited by virtual address space */
1973 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1974 error_report("at most 2047 MB RAM can be simulated");
1980 static void set_memory_options(MachineClass *mc)
1983 const char *mem_str;
1984 const ram_addr_t default_ram_size = mc->default_ram_size;
1985 QemuOpts *opts = qemu_find_opts_singleton("memory");
1988 loc_push_none(&loc);
1989 qemu_opts_loc_restore(opts);
1992 mem_str = qemu_opt_get(opts, "size");
1995 error_report("missing 'size' option value");
1999 sz = qemu_opt_get_size(opts, "size", ram_size);
2001 /* Fix up legacy suffix-less format */
2002 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2003 uint64_t overflow_check = sz;
2006 if (sz / MiB != overflow_check) {
2007 error_report("too large 'size' option value");
2013 /* backward compatibility behaviour for case "-m 0" */
2015 sz = default_ram_size;
2018 sz = QEMU_ALIGN_UP(sz, 8192);
2019 if (mc->fixup_ram_size) {
2020 sz = mc->fixup_ram_size(sz);
2023 if (ram_size != sz) {
2024 error_report("ram size too large");
2028 /* store value for the future use */
2029 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2030 maxram_size = ram_size;
2032 if (qemu_opt_get(opts, "maxmem")) {
2035 sz = qemu_opt_get_size(opts, "maxmem", 0);
2036 slots = qemu_opt_get_number(opts, "slots", 0);
2037 if (sz < ram_size) {
2038 error_report("invalid value of -m option maxmem: "
2039 "maximum memory size (0x%" PRIx64 ") must be at least "
2040 "the initial memory size (0x" RAM_ADDR_FMT ")",
2043 } else if (slots && sz == ram_size) {
2044 error_report("invalid value of -m option maxmem: "
2045 "memory slots were specified but maximum memory size "
2046 "(0x%" PRIx64 ") is equal to the initial memory size "
2047 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2053 } else if (qemu_opt_get(opts, "slots")) {
2054 error_report("invalid -m option value: missing 'maxmem' option");
2061 static void qemu_create_machine(MachineClass *machine_class)
2063 object_set_machine_compat_props(machine_class->compat_props);
2065 set_memory_options(machine_class);
2067 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2068 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
2071 object_property_add_child(object_get_root(), "machine",
2072 OBJECT(current_machine));
2073 object_property_add_child(container_get(OBJECT(current_machine),
2075 "sysbus", OBJECT(sysbus_get_default()));
2077 if (machine_class->minimum_page_bits) {
2078 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2079 /* This would be a board error: specifying a minimum smaller than
2080 * a target's compile-time fixed setting.
2082 g_assert_not_reached();
2086 cpu_exec_init_all();
2089 if (machine_class->hw_version) {
2090 qemu_set_hw_version(machine_class->hw_version);
2093 machine_smp_parse(current_machine,
2094 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
2097 * Get the default machine options from the machine if it is not already
2098 * specified either by the configuration file or by the command line.
2100 if (machine_class->default_machine_opts) {
2101 qemu_opts_set_defaults(qemu_find_opts("machine"),
2102 machine_class->default_machine_opts, 0);
2106 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2110 g = g_malloc0(sizeof(*g));
2111 g->driver = qemu_opt_get(opts, "driver");
2112 g->property = qemu_opt_get(opts, "property");
2113 g->value = qemu_opt_get(opts, "value");
2114 qdev_prop_register_global(g);
2118 static void qemu_read_default_config_file(Error **errp)
2122 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2124 ret = qemu_read_config_file(file, errp);
2126 if (ret == -ENOENT) {
2133 static int qemu_set_option(const char *str)
2135 Error *local_err = NULL;
2136 char group[64], id[64], arg[64];
2141 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2142 if (rc < 3 || str[offset] != '=') {
2143 error_report("can't parse: \"%s\"", str);
2147 list = qemu_find_opts(group);
2152 opts = qemu_opts_find(list, id);
2154 error_report("there is no %s \"%s\" defined",
2159 if (!qemu_opt_set(opts, arg, str + offset + 1, &local_err)) {
2160 error_report_err(local_err);
2166 static void user_register_global_props(void)
2168 qemu_opts_foreach(qemu_find_opts("global"),
2169 global_init_func, NULL, NULL);
2172 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2174 icount_configure(opts, errp);
2178 static int accelerator_set_property(void *opaque,
2179 const char *name, const char *value,
2182 return object_parse_property_opt(opaque, name, value, "accel", errp);
2185 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2187 bool *p_init_failed = opaque;
2188 const char *acc = qemu_opt_get(opts, "accel");
2189 AccelClass *ac = accel_find(acc);
2192 bool qtest_with_kvm;
2194 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2197 *p_init_failed = true;
2198 if (!qtest_with_kvm) {
2199 error_report("invalid accelerator %s", acc);
2203 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2204 object_apply_compat_props(OBJECT(accel));
2205 qemu_opt_foreach(opts, accelerator_set_property,
2209 ret = accel_init_machine(accel, current_machine);
2211 *p_init_failed = true;
2212 if (!qtest_with_kvm || ret != -ENOENT) {
2213 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2221 static void configure_accelerators(const char *progname)
2223 const char *accelerators;
2224 bool init_failed = false;
2226 qemu_opts_foreach(qemu_find_opts("icount"),
2227 do_configure_icount, NULL, &error_fatal);
2229 accelerators = qemu_opt_get(qemu_get_machine_opts(), "accel");
2230 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2231 char **accel_list, **tmp;
2233 if (accelerators == NULL) {
2234 /* Select the default accelerator */
2235 bool have_tcg = accel_find("tcg");
2236 bool have_kvm = accel_find("kvm");
2238 if (have_tcg && have_kvm) {
2239 if (g_str_has_suffix(progname, "kvm")) {
2240 /* If the program name ends with "kvm", we prefer KVM */
2241 accelerators = "kvm:tcg";
2243 accelerators = "tcg:kvm";
2245 } else if (have_kvm) {
2246 accelerators = "kvm";
2247 } else if (have_tcg) {
2248 accelerators = "tcg";
2250 error_report("No accelerator selected and"
2251 " no default accelerator available");
2255 accel_list = g_strsplit(accelerators, ":", 0);
2257 for (tmp = accel_list; *tmp; tmp++) {
2259 * Filter invalid accelerators here, to prevent obscenities
2260 * such as "-machine accel=tcg,,thread=single".
2262 if (accel_find(*tmp)) {
2263 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2266 error_report("invalid accelerator %s", *tmp);
2269 g_strfreev(accel_list);
2271 if (accelerators != NULL) {
2272 error_report("The -accel and \"-machine accel=\" options are incompatible");
2277 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2278 do_configure_accelerator, &init_failed, &error_fatal)) {
2280 error_report("no accelerator found");
2285 if (init_failed && !qtest_chrdev) {
2286 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2287 error_report("falling back to %s", ac->name);
2290 if (icount_enabled() && !tcg_enabled()) {
2291 error_report("-icount is not allowed with hardware virtualization");
2296 static void create_default_memdev(MachineState *ms, const char *path)
2299 MachineClass *mc = MACHINE_GET_CLASS(ms);
2301 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2303 object_property_set_str(obj, "mem-path", path, &error_fatal);
2305 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2306 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2308 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2309 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2310 false, &error_fatal);
2311 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2313 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2317 static void qemu_validate_options(void)
2319 QemuOpts *machine_opts = qemu_get_machine_opts();
2320 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2321 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2322 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2324 if (kernel_filename == NULL) {
2325 if (kernel_cmdline != NULL) {
2326 error_report("-append only allowed with -kernel option");
2330 if (initrd_filename != NULL) {
2331 error_report("-initrd only allowed with -kernel option");
2336 if (loadvm && preconfig_requested) {
2337 error_report("'preconfig' and 'loadvm' options are "
2338 "mutually exclusive");
2341 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2342 error_report("'preconfig' supports '-incoming defer' only");
2346 #ifdef CONFIG_CURSES
2347 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2348 error_report("curses display cannot be used with -daemonize");
2354 static void qemu_process_sugar_options(void)
2359 val = g_strdup_printf("%d",
2360 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2361 object_register_sugar_prop("memory-backend", "prealloc-threads", val,
2364 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2368 int i = select_watchdog(watchdog);
2370 exit (i == 1 ? 1 : 0);
2374 /* -action processing */
2377 * Process all the -action parameters parsed from cmdline.
2379 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2381 Error *local_err = NULL;
2382 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2383 QObject *ret = NULL;
2384 qmp_marshal_set_action(qdict, &ret, &local_err);
2386 qobject_unref(qdict);
2388 error_propagate(errp, local_err);
2394 static void qemu_process_early_options(void)
2396 #ifdef CONFIG_SECCOMP
2397 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2399 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2403 qemu_opts_foreach(qemu_find_opts("name"),
2404 parse_name, NULL, &error_fatal);
2406 if (qemu_opts_foreach(qemu_find_opts("action"),
2407 process_runstate_actions, NULL, &error_fatal)) {
2412 qemu_opts_foreach(qemu_find_opts("add-fd"),
2413 parse_add_fd, NULL, &error_fatal);
2415 qemu_opts_foreach(qemu_find_opts("add-fd"),
2416 cleanup_add_fd, NULL, &error_fatal);
2419 /* Open the logfile at this point and set the log mask if necessary. */
2421 qemu_set_log_filename(log_file, &error_fatal);
2425 mask = qemu_str_to_log_mask(log_mask);
2427 qemu_print_log_usage(stdout);
2435 qemu_add_default_firmwarepath();
2438 static void qemu_process_help_options(void)
2441 * Check for -cpu help and -device help before we call select_machine(),
2442 * which will return an error if the architecture has no default machine
2443 * type and the user did not specify one, so that the user doesn't need
2444 * to say '-cpu help -machine something'.
2446 if (cpu_option && is_help_option(cpu_option)) {
2447 list_cpus(cpu_option);
2451 if (qemu_opts_foreach(qemu_find_opts("device"),
2452 device_help_func, NULL, NULL)) {
2456 /* -L help lists the data directories and exits. */
2457 if (list_data_dirs) {
2458 qemu_list_data_dirs();
2463 static void qemu_maybe_daemonize(const char *pid_file)
2468 rcu_disable_atfork();
2470 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2471 error_reportf_err(err, "cannot create PID file: ");
2475 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2476 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2479 static void qemu_init_displays(void)
2483 /* init local displays */
2484 ds = init_displaystate();
2485 qemu_display_init(ds, &dpy);
2487 /* must be after terminal init, SDL library changes signal handlers */
2488 os_setup_signal_handling();
2490 /* init remote displays */
2492 qemu_opts_foreach(qemu_find_opts("vnc"),
2493 vnc_init_func, NULL, &error_fatal);
2497 qemu_spice.display_init();
2501 static void qemu_init_board(void)
2503 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2505 if (machine_class->default_ram_id && current_machine->ram_size &&
2506 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2507 create_default_memdev(current_machine, mem_path);
2510 /* process plugin before CPUs are created, but once -smp has been parsed */
2511 qemu_plugin_load_list(&plugin_list, &error_fatal);
2513 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2514 machine_run_board_init(current_machine);
2516 drive_check_orphaned();
2520 if (hax_enabled()) {
2521 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2526 static void qemu_create_cli_devices(void)
2530 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2531 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2533 /* init USB devices */
2534 if (machine_usb(current_machine)) {
2535 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2539 /* init generic devices */
2540 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2541 qemu_opts_foreach(qemu_find_opts("device"),
2542 device_init_func, NULL, &error_fatal);
2543 rom_reset_order_override();
2546 static void qemu_machine_creation_done(void)
2548 MachineState *machine = MACHINE(qdev_get_machine());
2550 /* Did we create any drives that we failed to create a device for? */
2551 drive_check_orphaned();
2553 /* Don't warn about the default network setup that you get if
2554 * no command line -net or -netdev options are specified. There
2555 * are two cases that we would otherwise complain about:
2556 * (1) board doesn't support a NIC but the implicit "-net nic"
2558 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2559 * sets up a nic that isn't connected to anything.
2561 if (!default_net && (!qtest_enabled() || has_defaults)) {
2562 net_check_clients();
2565 qdev_prop_check_globals();
2567 qdev_machine_creation_done();
2571 * Verify that Confidential Guest Support has actually been initialized
2573 assert(machine->cgs->ready);
2576 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2581 void qmp_x_exit_preconfig(Error **errp)
2583 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2584 error_setg(errp, "The command is permitted only before machine initialization");
2589 qemu_create_cli_devices();
2590 qemu_machine_creation_done();
2593 Error *local_err = NULL;
2594 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2595 error_report_err(local_err);
2600 if (replay_mode != REPLAY_MODE_NONE) {
2601 replay_vmstate_init();
2605 Error *local_err = NULL;
2606 if (strcmp(incoming, "defer") != 0) {
2607 qmp_migrate_incoming(incoming, &local_err);
2609 error_reportf_err(local_err, "-incoming %s: ", incoming);
2613 } else if (autostart) {
2618 void qemu_init(int argc, char **argv, char **envp)
2621 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2622 QemuOptsList *olist;
2625 MachineClass *machine_class;
2626 bool userconfig = true;
2627 FILE *vmstate_dump_file = NULL;
2629 qemu_add_opts(&qemu_drive_opts);
2630 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2631 qemu_add_drive_opts(&qemu_common_drive_opts);
2632 qemu_add_drive_opts(&qemu_drive_opts);
2633 qemu_add_drive_opts(&bdrv_runtime_opts);
2634 qemu_add_opts(&qemu_chardev_opts);
2635 qemu_add_opts(&qemu_device_opts);
2636 qemu_add_opts(&qemu_netdev_opts);
2637 qemu_add_opts(&qemu_nic_opts);
2638 qemu_add_opts(&qemu_net_opts);
2639 qemu_add_opts(&qemu_rtc_opts);
2640 qemu_add_opts(&qemu_global_opts);
2641 qemu_add_opts(&qemu_mon_opts);
2642 qemu_add_opts(&qemu_trace_opts);
2643 qemu_plugin_add_opts();
2644 qemu_add_opts(&qemu_option_rom_opts);
2645 qemu_add_opts(&qemu_machine_opts);
2646 qemu_add_opts(&qemu_accel_opts);
2647 qemu_add_opts(&qemu_mem_opts);
2648 qemu_add_opts(&qemu_smp_opts);
2649 qemu_add_opts(&qemu_boot_opts);
2650 qemu_add_opts(&qemu_add_fd_opts);
2651 qemu_add_opts(&qemu_object_opts);
2652 qemu_add_opts(&qemu_tpmdev_opts);
2653 qemu_add_opts(&qemu_overcommit_opts);
2654 qemu_add_opts(&qemu_msg_opts);
2655 qemu_add_opts(&qemu_name_opts);
2656 qemu_add_opts(&qemu_numa_opts);
2657 qemu_add_opts(&qemu_icount_opts);
2658 qemu_add_opts(&qemu_semihosting_config_opts);
2659 qemu_add_opts(&qemu_fw_cfg_opts);
2660 qemu_add_opts(&qemu_action_opts);
2661 module_call_init(MODULE_INIT_OPTS);
2663 error_init(argv[0]);
2664 qemu_init_exec_dir(argv[0]);
2666 qemu_init_subsystems();
2668 /* first pass of option parsing */
2670 while (optind < argc) {
2671 if (argv[optind][0] != '-') {
2675 const QEMUOption *popt;
2677 popt = lookup_opt(argc, argv, &optarg, &optind);
2678 switch (popt->index) {
2679 case QEMU_OPTION_nouserconfig:
2687 qemu_read_default_config_file(&error_fatal);
2690 /* second pass of option parsing */
2695 if (argv[optind][0] != '-') {
2696 loc_set_cmdline(argv, optind, 1);
2697 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2699 const QEMUOption *popt;
2701 popt = lookup_opt(argc, argv, &optarg, &optind);
2702 if (!(popt->arch_mask & arch_type)) {
2703 error_report("Option not supported for this target");
2706 switch(popt->index) {
2707 case QEMU_OPTION_cpu:
2708 /* hw initialization will check this */
2709 cpu_option = optarg;
2711 case QEMU_OPTION_hda:
2712 case QEMU_OPTION_hdb:
2713 case QEMU_OPTION_hdc:
2714 case QEMU_OPTION_hdd:
2715 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2718 case QEMU_OPTION_blockdev:
2721 BlockdevOptionsQueueEntry *bdo;
2723 v = qobject_input_visitor_new_str(optarg, "driver",
2726 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2727 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2730 loc_save(&bdo->loc);
2731 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2734 case QEMU_OPTION_drive:
2735 if (drive_def(optarg) == NULL) {
2739 case QEMU_OPTION_set:
2740 if (qemu_set_option(optarg) != 0)
2743 case QEMU_OPTION_global:
2744 if (qemu_global_option(optarg) != 0)
2747 case QEMU_OPTION_mtdblock:
2748 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2750 case QEMU_OPTION_sd:
2751 drive_add(IF_SD, -1, optarg, SD_OPTS);
2753 case QEMU_OPTION_pflash:
2754 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2756 case QEMU_OPTION_snapshot:
2758 Error *blocker = NULL;
2760 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2762 replay_add_blocker(blocker);
2765 case QEMU_OPTION_numa:
2766 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2772 case QEMU_OPTION_display:
2773 parse_display(optarg);
2775 case QEMU_OPTION_nographic:
2776 olist = qemu_find_opts("machine");
2777 qemu_opts_parse_noisily(olist, "graphics=off", false);
2779 dpy.type = DISPLAY_TYPE_NONE;
2781 case QEMU_OPTION_curses:
2782 #ifdef CONFIG_CURSES
2783 dpy.type = DISPLAY_TYPE_CURSES;
2785 error_report("curses or iconv support is disabled");
2789 case QEMU_OPTION_portrait:
2790 graphic_rotate = 90;
2792 case QEMU_OPTION_rotate:
2793 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2794 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2795 graphic_rotate != 180 && graphic_rotate != 270) {
2796 error_report("only 90, 180, 270 deg rotation is available");
2800 case QEMU_OPTION_kernel:
2801 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
2803 case QEMU_OPTION_initrd:
2804 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
2806 case QEMU_OPTION_append:
2807 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
2809 case QEMU_OPTION_dtb:
2810 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
2812 case QEMU_OPTION_cdrom:
2813 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2815 case QEMU_OPTION_boot:
2816 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2822 case QEMU_OPTION_fda:
2823 case QEMU_OPTION_fdb:
2824 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2827 case QEMU_OPTION_no_fd_bootchk:
2830 case QEMU_OPTION_netdev:
2832 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2836 case QEMU_OPTION_nic:
2838 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2842 case QEMU_OPTION_net:
2844 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2848 #ifdef CONFIG_LIBISCSI
2849 case QEMU_OPTION_iscsi:
2850 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2857 case QEMU_OPTION_audio_help:
2858 audio_legacy_help();
2861 case QEMU_OPTION_audiodev:
2862 audio_parse_option(optarg);
2864 case QEMU_OPTION_soundhw:
2865 select_soundhw (optarg);
2870 case QEMU_OPTION_version:
2875 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2882 case QEMU_OPTION_tpmdev:
2883 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2888 case QEMU_OPTION_mempath:
2891 case QEMU_OPTION_mem_prealloc:
2900 case QEMU_OPTION_DFILTER:
2901 qemu_set_dfilter_ranges(optarg, &error_fatal);
2903 case QEMU_OPTION_seed:
2904 qemu_guest_random_seed_main(optarg, &error_fatal);
2907 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2909 case QEMU_OPTION_gdb:
2910 add_device_config(DEV_GDB, optarg);
2913 if (is_help_option(optarg)) {
2914 list_data_dirs = true;
2916 qemu_add_data_dir(g_strdup(optarg));
2919 case QEMU_OPTION_bios:
2920 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
2922 case QEMU_OPTION_singlestep:
2929 keyboard_layout = optarg;
2931 case QEMU_OPTION_vga:
2940 w = strtol(p, (char **)&p, 10);
2943 error_report("invalid resolution or depth");
2949 h = strtol(p, (char **)&p, 10);
2954 depth = strtol(p, (char **)&p, 10);
2955 if (depth != 1 && depth != 2 && depth != 4 &&
2956 depth != 8 && depth != 15 && depth != 16 &&
2957 depth != 24 && depth != 32)
2959 } else if (*p == '\0') {
2960 depth = graphic_depth;
2967 graphic_depth = depth;
2970 case QEMU_OPTION_echr:
2973 term_escape_char = strtol(optarg, &r, 0);
2975 printf("Bad argument to echr\n");
2978 case QEMU_OPTION_monitor:
2979 default_monitor = 0;
2980 if (strncmp(optarg, "none", 4)) {
2981 monitor_parse(optarg, "readline", false);
2984 case QEMU_OPTION_qmp:
2985 monitor_parse(optarg, "control", false);
2986 default_monitor = 0;
2988 case QEMU_OPTION_qmp_pretty:
2989 monitor_parse(optarg, "control", true);
2990 default_monitor = 0;
2992 case QEMU_OPTION_mon:
2993 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
2998 default_monitor = 0;
3000 case QEMU_OPTION_chardev:
3001 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3007 case QEMU_OPTION_fsdev:
3008 olist = qemu_find_opts("fsdev");
3010 error_report("fsdev support is disabled");
3013 opts = qemu_opts_parse_noisily(olist, optarg, true);
3018 case QEMU_OPTION_virtfs: {
3021 const char *writeout, *sock_fd, *socket, *path, *security_model,
3024 olist = qemu_find_opts("virtfs");
3026 error_report("virtfs support is disabled");
3029 opts = qemu_opts_parse_noisily(olist, optarg, true);
3034 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3035 qemu_opt_get(opts, "mount_tag") == NULL) {
3036 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3039 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3040 qemu_opts_id(opts) ?:
3041 qemu_opt_get(opts, "mount_tag"),
3044 error_report("duplicate or invalid fsdev id: %s",
3045 qemu_opt_get(opts, "mount_tag"));
3049 writeout = qemu_opt_get(opts, "writeout");
3051 #ifdef CONFIG_SYNC_FILE_RANGE
3052 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3054 error_report("writeout=immediate not supported "
3055 "on this platform");
3059 qemu_opt_set(fsdev, "fsdriver",
3060 qemu_opt_get(opts, "fsdriver"), &error_abort);
3061 path = qemu_opt_get(opts, "path");
3063 qemu_opt_set(fsdev, "path", path, &error_abort);
3065 security_model = qemu_opt_get(opts, "security_model");
3066 if (security_model) {
3067 qemu_opt_set(fsdev, "security_model", security_model,
3070 socket = qemu_opt_get(opts, "socket");
3072 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3074 sock_fd = qemu_opt_get(opts, "sock_fd");
3076 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3079 qemu_opt_set_bool(fsdev, "readonly",
3080 qemu_opt_get_bool(opts, "readonly", 0),
3082 multidevs = qemu_opt_get(opts, "multidevs");
3084 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3086 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3088 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3089 qemu_opt_set(device, "fsdev",
3090 qemu_opts_id(fsdev), &error_abort);
3091 qemu_opt_set(device, "mount_tag",
3092 qemu_opt_get(opts, "mount_tag"), &error_abort);
3095 case QEMU_OPTION_serial:
3096 add_device_config(DEV_SERIAL, optarg);
3098 if (strncmp(optarg, "mon:", 4) == 0) {
3099 default_monitor = 0;
3102 case QEMU_OPTION_watchdog:
3104 error_report("only one watchdog option may be given");
3109 case QEMU_OPTION_action:
3110 olist = qemu_find_opts("action");
3111 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3115 case QEMU_OPTION_watchdog_action:
3116 if (select_watchdog_action(optarg) == -1) {
3117 error_report("unknown -watchdog-action parameter");
3121 case QEMU_OPTION_parallel:
3122 add_device_config(DEV_PARALLEL, optarg);
3123 default_parallel = 0;
3124 if (strncmp(optarg, "mon:", 4) == 0) {
3125 default_monitor = 0;
3128 case QEMU_OPTION_debugcon:
3129 add_device_config(DEV_DEBUGCON, optarg);
3131 case QEMU_OPTION_loadvm:
3134 case QEMU_OPTION_full_screen:
3135 dpy.has_full_screen = true;
3136 dpy.full_screen = true;
3138 case QEMU_OPTION_alt_grab:
3141 case QEMU_OPTION_ctrl_grab:
3144 case QEMU_OPTION_no_quit:
3145 dpy.has_window_close = true;
3146 dpy.window_close = false;
3148 case QEMU_OPTION_sdl:
3150 dpy.type = DISPLAY_TYPE_SDL;
3153 error_report("SDL support is disabled");
3156 case QEMU_OPTION_pidfile:
3159 case QEMU_OPTION_win2k_hack:
3160 win2k_install_hack = 1;
3162 case QEMU_OPTION_acpitable:
3163 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3168 acpi_table_add(opts, &error_fatal);
3170 case QEMU_OPTION_smbios:
3171 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3176 smbios_entry_add(opts, &error_fatal);
3178 case QEMU_OPTION_fwcfg:
3179 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3185 case QEMU_OPTION_preconfig:
3186 preconfig_requested = true;
3188 case QEMU_OPTION_enable_kvm:
3189 olist = qemu_find_opts("machine");
3190 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3193 case QEMU_OPTION_machine:
3194 olist = qemu_find_opts("machine");
3195 opts = qemu_opts_parse_noisily(olist, optarg, true);
3200 case QEMU_OPTION_accel:
3201 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3203 optarg = qemu_opt_get(accel_opts, "accel");
3204 if (!optarg || is_help_option(optarg)) {
3205 printf("Accelerators supported in QEMU binary:\n");
3206 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3208 for (el = accel_list; el; el = el->next) {
3209 gchar *typename = g_strdup(object_class_get_name(
3210 OBJECT_CLASS(el->data)));
3211 /* omit qtest which is used for tests only */
3212 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3213 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3214 gchar **optname = g_strsplit(typename,
3215 ACCEL_CLASS_SUFFIX, 0);
3216 printf("%s\n", optname[0]);
3217 g_strfreev(optname);
3221 g_slist_free(accel_list);
3225 case QEMU_OPTION_usb:
3226 olist = qemu_find_opts("machine");
3227 qemu_opts_parse_noisily(olist, "usb=on", false);
3229 case QEMU_OPTION_usbdevice:
3230 olist = qemu_find_opts("machine");
3231 qemu_opts_parse_noisily(olist, "usb=on", false);
3232 add_device_config(DEV_USB, optarg);
3234 case QEMU_OPTION_device:
3235 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3240 case QEMU_OPTION_smp:
3241 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3246 case QEMU_OPTION_vnc:
3249 case QEMU_OPTION_no_acpi:
3250 olist = qemu_find_opts("machine");
3251 qemu_opts_parse_noisily(olist, "acpi=off", false);
3253 case QEMU_OPTION_no_hpet:
3254 olist = qemu_find_opts("machine");
3255 qemu_opts_parse_noisily(olist, "hpet=off", false);
3257 case QEMU_OPTION_no_reboot:
3258 olist = qemu_find_opts("action");
3259 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3261 case QEMU_OPTION_no_shutdown:
3262 olist = qemu_find_opts("action");
3263 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3265 case QEMU_OPTION_uuid:
3266 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3267 error_report("failed to parse UUID string: wrong format");
3270 qemu_uuid_set = true;
3272 case QEMU_OPTION_option_rom:
3273 if (nb_option_roms >= MAX_OPTION_ROMS) {
3274 error_report("too many option ROMs");
3277 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3282 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3283 option_rom[nb_option_roms].bootindex =
3284 qemu_opt_get_number(opts, "bootindex", -1);
3285 if (!option_rom[nb_option_roms].name) {
3286 error_report("Option ROM file is not specified");
3291 case QEMU_OPTION_semihosting:
3292 qemu_semihosting_enable();
3294 case QEMU_OPTION_semihosting_config:
3295 if (qemu_semihosting_config_options(optarg) != 0) {
3299 case QEMU_OPTION_name:
3300 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3305 /* Capture guest name if -msg guest-name is used later */
3306 error_guest_name = qemu_opt_get(opts, "guest");
3308 case QEMU_OPTION_prom_env:
3309 if (nb_prom_envs >= MAX_PROM_ENVS) {
3310 error_report("too many prom variables");
3313 prom_envs[nb_prom_envs] = optarg;
3316 case QEMU_OPTION_old_param:
3319 case QEMU_OPTION_rtc:
3320 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3326 case QEMU_OPTION_icount:
3327 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3333 case QEMU_OPTION_incoming:
3335 runstate_set(RUN_STATE_INMIGRATE);
3339 case QEMU_OPTION_only_migratable:
3340 only_migratable = 1;
3342 case QEMU_OPTION_nodefaults:
3345 case QEMU_OPTION_xen_domid:
3346 if (!(xen_available())) {
3347 error_report("Option not supported for this target");
3350 xen_domid = atoi(optarg);
3352 case QEMU_OPTION_xen_attach:
3353 if (!(xen_available())) {
3354 error_report("Option not supported for this target");
3357 xen_mode = XEN_ATTACH;
3359 case QEMU_OPTION_xen_domid_restrict:
3360 if (!(xen_available())) {
3361 error_report("Option not supported for this target");
3364 xen_domid_restrict = true;
3366 case QEMU_OPTION_trace:
3367 trace_opt_parse(optarg);
3369 case QEMU_OPTION_plugin:
3370 qemu_plugin_opt_parse(optarg, &plugin_list);
3372 case QEMU_OPTION_readconfig:
3373 qemu_read_config_file(optarg, &error_fatal);
3375 case QEMU_OPTION_spice:
3376 olist = qemu_find_opts_err("spice", NULL);
3378 ui_module_load_one("spice-core");
3379 olist = qemu_find_opts("spice");
3382 error_report("spice support is disabled");
3385 opts = qemu_opts_parse_noisily(olist, optarg, false);
3391 case QEMU_OPTION_writeconfig:
3394 warn_report("-writeconfig is deprecated and will go away without a replacement");
3395 if (strcmp(optarg, "-") == 0) {
3398 fp = fopen(optarg, "w");
3400 error_report("open %s: %s", optarg,
3405 qemu_config_write(fp);
3411 case QEMU_OPTION_qtest:
3412 qtest_chrdev = optarg;
3414 case QEMU_OPTION_qtest_log:
3417 case QEMU_OPTION_sandbox:
3418 olist = qemu_find_opts("sandbox");
3420 #ifndef CONFIG_SECCOMP
3421 error_report("-sandbox support is not enabled "
3422 "in this QEMU binary");
3427 opts = qemu_opts_parse_noisily(olist, optarg, true);
3432 case QEMU_OPTION_add_fd:
3434 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3440 error_report("File descriptor passing is disabled on this "
3445 case QEMU_OPTION_object:
3446 object_option_parse(optarg);
3448 case QEMU_OPTION_overcommit:
3449 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3454 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3455 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3457 case QEMU_OPTION_compat:
3462 v = qobject_input_visitor_new_str(optarg, NULL,
3465 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3466 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3468 qapi_free_CompatPolicy(opts);
3472 case QEMU_OPTION_msg:
3473 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3478 configure_msg(opts);
3480 case QEMU_OPTION_dump_vmstate:
3481 if (vmstate_dump_file) {
3482 error_report("only one '-dump-vmstate' "
3483 "option may be given");
3486 vmstate_dump_file = fopen(optarg, "w");
3487 if (vmstate_dump_file == NULL) {
3488 error_report("open %s: %s", optarg, strerror(errno));
3492 case QEMU_OPTION_enable_sync_profile:
3495 case QEMU_OPTION_nouserconfig:
3496 /* Nothing to be parsed here. Especially, do not error out below. */
3499 if (os_parse_cmd_args(popt->index, optarg)) {
3500 error_report("Option not supported in this build");
3507 * Clear error location left behind by the loop.
3508 * Best done right after the loop. Do not insert code here!
3512 qemu_validate_options();
3513 qemu_process_sugar_options();
3516 * These options affect everything else and should be processed
3517 * before daemonizing.
3519 qemu_process_early_options();
3521 qemu_process_help_options();
3522 qemu_maybe_daemonize(pid_file);
3525 * The trace backend must be initialized after daemonizing.
3526 * trace_init_backends() will call st_init(), which will create the
3527 * trace thread in the parent, and also register st_flush_trace_buffer()
3528 * in atexit(). This function will force the parent to wait for the
3529 * writeout thread to finish, which will not occur, and the parent
3530 * process will be left in the host.
3532 if (!trace_init_backends()) {
3537 qemu_init_main_loop(&error_fatal);
3540 user_register_global_props();
3541 replay_configure(icount_opts);
3543 configure_rtc(qemu_find_opts_singleton("rtc"));
3545 qemu_create_machine(select_machine());
3549 qemu_disable_default_devices();
3550 qemu_create_default_devices();
3551 qemu_create_early_backends();
3553 qemu_apply_machine_options();
3554 phase_advance(PHASE_MACHINE_CREATED);
3557 * Note: uses machine properties such as kernel-irqchip, must run
3558 * after machine_set_property().
3560 configure_accelerators(argv[0]);
3561 phase_advance(PHASE_ACCEL_CREATED);
3564 * Beware, QOM objects created before this point miss global and
3565 * compat properties.
3567 * Global properties get set up by qdev_prop_register_global(),
3568 * called from user_register_global_props(), and certain option
3569 * desugaring. Also in CPU feature desugaring (buried in
3570 * parse_cpu_option()), which happens below this point, but may
3571 * only target the CPU type, which can only be created after
3572 * parse_cpu_option() returned the type.
3574 * Machine compat properties: object_set_machine_compat_props().
3575 * Accelerator compat props: object_set_accelerator_compat_props(),
3576 * called from do_configure_accelerator().
3579 machine_class = MACHINE_GET_CLASS(current_machine);
3580 if (!qtest_enabled() && machine_class->deprecation_reason) {
3581 error_report("Machine type '%s' is deprecated: %s",
3582 machine_class->name, machine_class->deprecation_reason);
3586 * Note: creates a QOM object, must run only after global and
3587 * compat properties have been set up.
3589 migration_object_init();
3591 qemu_create_late_backends();
3593 /* parse features once if machine provides default cpu_type */
3594 current_machine->cpu_type = machine_class->default_cpu_type;
3596 current_machine->cpu_type = parse_cpu_option(cpu_option);
3598 /* NB: for machine none cpu_type could STILL be NULL here! */
3599 accel_init_interfaces(ACCEL_GET_CLASS(current_machine->accelerator));
3601 qemu_resolve_machine_memdev();
3602 parse_numa_opts(current_machine);
3604 if (vmstate_dump_file) {
3606 dump_vmstate_json_to_file(vmstate_dump_file);
3610 if (!preconfig_requested) {
3611 qmp_x_exit_preconfig(&error_fatal);
3613 qemu_init_displays();
3614 accel_setup_post(current_machine);