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/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"
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"
59 #include "net/slirp.h"
60 #include "monitor/monitor.h"
61 #include "ui/console.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"
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"
93 #include "fsdev/qemu-fsdev.h"
95 #include "sysemu/qtest.h"
97 #include "disas/disas.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 "hw/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
;
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
;
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;
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
= "ide-drive", .flag
= &default_cdrom
},
181 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
182 { .driver
= "scsi-hd", .flag
= &default_cdrom
},
183 { .driver
= "VGA", .flag
= &default_vga
},
184 { .driver
= "isa-vga", .flag
= &default_vga
},
185 { .driver
= "cirrus-vga", .flag
= &default_vga
},
186 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
187 { .driver
= "vmware-svga", .flag
= &default_vga
},
188 { .driver
= "qxl-vga", .flag
= &default_vga
},
189 { .driver
= "virtio-vga", .flag
= &default_vga
},
190 { .driver
= "ati-vga", .flag
= &default_vga
},
191 { .driver
= "vhost-user-vga", .flag
= &default_vga
},
194 static QemuOptsList qemu_rtc_opts
= {
196 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
201 .type
= QEMU_OPT_STRING
,
204 .type
= QEMU_OPT_STRING
,
207 .type
= QEMU_OPT_STRING
,
209 { /* end of list */ }
213 static QemuOptsList qemu_option_rom_opts
= {
214 .name
= "option-rom",
215 .implied_opt_name
= "romfile",
216 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
220 .type
= QEMU_OPT_NUMBER
,
223 .type
= QEMU_OPT_STRING
,
225 { /* end of list */ }
229 static QemuOptsList qemu_machine_opts
= {
231 .implied_opt_name
= "type",
233 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
236 * no elements => accept any
237 * sanity checking will happen later
238 * when setting machine properties
244 static QemuOptsList qemu_accel_opts
= {
246 .implied_opt_name
= "accel",
247 .head
= QTAILQ_HEAD_INITIALIZER(qemu_accel_opts
.head
),
250 * no elements => accept any
251 * sanity checking will happen later
252 * when setting accelerator properties
258 static QemuOptsList qemu_boot_opts
= {
260 .implied_opt_name
= "order",
262 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
266 .type
= QEMU_OPT_STRING
,
269 .type
= QEMU_OPT_STRING
,
272 .type
= QEMU_OPT_BOOL
,
275 .type
= QEMU_OPT_STRING
,
277 .name
= "splash-time",
278 .type
= QEMU_OPT_NUMBER
,
280 .name
= "reboot-timeout",
281 .type
= QEMU_OPT_NUMBER
,
284 .type
= QEMU_OPT_BOOL
,
290 static QemuOptsList qemu_add_fd_opts
= {
292 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
296 .type
= QEMU_OPT_NUMBER
,
297 .help
= "file descriptor of which a duplicate is added to fd set",
300 .type
= QEMU_OPT_NUMBER
,
301 .help
= "ID of the fd set to add fd to",
304 .type
= QEMU_OPT_STRING
,
305 .help
= "free-form string used to describe fd",
307 { /* end of list */ }
311 static QemuOptsList qemu_object_opts
= {
313 .implied_opt_name
= "qom-type",
314 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
320 static QemuOptsList qemu_tpmdev_opts
= {
322 .implied_opt_name
= "type",
323 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
325 /* options are defined in the TPM backends */
326 { /* end of list */ }
330 static QemuOptsList qemu_overcommit_opts
= {
331 .name
= "overcommit",
332 .head
= QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts
.head
),
336 .type
= QEMU_OPT_BOOL
,
340 .type
= QEMU_OPT_BOOL
,
342 { /* end of list */ }
346 static QemuOptsList qemu_msg_opts
= {
348 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
352 .type
= QEMU_OPT_BOOL
,
355 .name
= "guest-name",
356 .type
= QEMU_OPT_BOOL
,
357 .help
= "Prepends guest name for error messages but only if "
358 "-name guest is set otherwise option is ignored\n",
360 { /* end of list */ }
364 static QemuOptsList qemu_name_opts
= {
366 .implied_opt_name
= "guest",
368 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
372 .type
= QEMU_OPT_STRING
,
373 .help
= "Sets the name of the guest.\n"
374 "This name will be displayed in the SDL window caption.\n"
375 "The name will also be used for the VNC server",
378 .type
= QEMU_OPT_STRING
,
379 .help
= "Sets the name of the QEMU process, as shown in top etc",
381 .name
= "debug-threads",
382 .type
= QEMU_OPT_BOOL
,
383 .help
= "When enabled, name the individual threads; defaults off.\n"
384 "NOTE: The thread names are for debugging and not a\n"
387 { /* End of list */ }
391 static QemuOptsList qemu_mem_opts
= {
393 .implied_opt_name
= "size",
394 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
399 .type
= QEMU_OPT_SIZE
,
403 .type
= QEMU_OPT_NUMBER
,
407 .type
= QEMU_OPT_SIZE
,
409 { /* end of list */ }
413 static QemuOptsList qemu_icount_opts
= {
415 .implied_opt_name
= "shift",
417 .head
= QTAILQ_HEAD_INITIALIZER(qemu_icount_opts
.head
),
421 .type
= QEMU_OPT_STRING
,
424 .type
= QEMU_OPT_BOOL
,
427 .type
= QEMU_OPT_BOOL
,
430 .type
= QEMU_OPT_STRING
,
433 .type
= QEMU_OPT_STRING
,
435 .name
= "rrsnapshot",
436 .type
= QEMU_OPT_STRING
,
438 { /* end of list */ }
442 static QemuOptsList qemu_fw_cfg_opts
= {
444 .implied_opt_name
= "name",
445 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts
.head
),
449 .type
= QEMU_OPT_STRING
,
450 .help
= "Sets the fw_cfg name of the blob to be inserted",
453 .type
= QEMU_OPT_STRING
,
454 .help
= "Sets the name of the file from which "
455 "the fw_cfg blob will be loaded",
458 .type
= QEMU_OPT_STRING
,
459 .help
= "Sets content of the blob to be inserted from a string",
462 .type
= QEMU_OPT_STRING
,
463 .help
= "Sets id of the object generating the fw_cfg blob "
466 { /* end of list */ }
470 static QemuOptsList qemu_action_opts
= {
473 .head
= QTAILQ_HEAD_INITIALIZER(qemu_action_opts
.head
),
477 .type
= QEMU_OPT_STRING
,
480 .type
= QEMU_OPT_STRING
,
483 .type
= QEMU_OPT_STRING
,
486 .type
= QEMU_OPT_STRING
,
488 { /* end of list */ }
493 * Get machine options
495 * Returns: machine options (never null).
497 static QemuOpts
*qemu_get_machine_opts(void)
499 return qemu_find_opts_singleton("machine");
502 const char *qemu_get_vm_name(void)
507 static int default_driver_check(void *opaque
, QemuOpts
*opts
, Error
**errp
)
509 const char *driver
= qemu_opt_get(opts
, "driver");
514 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
515 if (strcmp(default_list
[i
].driver
, driver
) != 0)
517 *(default_list
[i
].flag
) = 0;
522 static int parse_name(void *opaque
, QemuOpts
*opts
, Error
**errp
)
524 const char *proc_name
;
526 if (qemu_opt_get(opts
, "debug-threads")) {
527 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
529 qemu_name
= qemu_opt_get(opts
, "guest");
531 proc_name
= qemu_opt_get(opts
, "process");
533 os_set_proc_name(proc_name
);
539 bool defaults_enabled(void)
545 static int parse_add_fd(void *opaque
, QemuOpts
*opts
, Error
**errp
)
547 int fd
, dupfd
, flags
;
549 const char *fd_opaque
= NULL
;
552 fd
= qemu_opt_get_number(opts
, "fd", -1);
553 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
554 fd_opaque
= qemu_opt_get(opts
, "opaque");
557 error_setg(errp
, "fd option is required and must be non-negative");
561 if (fd
<= STDERR_FILENO
) {
562 error_setg(errp
, "fd cannot be a standard I/O stream");
567 * All fds inherited across exec() necessarily have FD_CLOEXEC
568 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
570 flags
= fcntl(fd
, F_GETFD
);
571 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
572 error_setg(errp
, "fd is not valid or already in use");
577 error_setg(errp
, "set option is required and must be non-negative");
581 #ifdef F_DUPFD_CLOEXEC
582 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
586 qemu_set_cloexec(dupfd
);
590 error_setg(errp
, "error duplicating fd: %s", strerror(errno
));
594 /* add the duplicate fd, and optionally the opaque string, to the fd set */
595 fdinfo
= monitor_fdset_add_fd(dupfd
, true, fdset_id
, !!fd_opaque
, fd_opaque
,
602 static int cleanup_add_fd(void *opaque
, QemuOpts
*opts
, Error
**errp
)
606 fd
= qemu_opt_get_number(opts
, "fd", -1);
613 /***********************************************************/
614 /* QEMU Block devices */
616 #define HD_OPTS "media=disk"
617 #define CDROM_OPTS "media=cdrom"
619 #define PFLASH_OPTS ""
623 static int drive_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
625 BlockInterfaceType
*block_default_type
= opaque
;
627 return drive_new(opts
, *block_default_type
, errp
) == NULL
;
630 static int drive_enable_snapshot(void *opaque
, QemuOpts
*opts
, Error
**errp
)
632 if (qemu_opt_get(opts
, "snapshot") == NULL
) {
633 qemu_opt_set(opts
, "snapshot", "on", &error_abort
);
638 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
639 int index
, const char *optstr
)
644 if (!enable
|| drive_get_by_index(type
, index
)) {
648 opts
= drive_add(type
, index
, NULL
, optstr
);
650 drive_enable_snapshot(NULL
, opts
, NULL
);
653 dinfo
= drive_new(opts
, type
, &error_abort
);
654 dinfo
->is_default
= true;
658 static void configure_blockdev(BlockdevOptionsQueue
*bdo_queue
,
659 MachineClass
*machine_class
, int snapshot
)
662 * If the currently selected machine wishes to override the
663 * units-per-bus property of its default HBA interface type, do so
666 if (machine_class
->units_per_default_bus
) {
667 override_max_devs(machine_class
->block_default_type
,
668 machine_class
->units_per_default_bus
);
671 /* open the virtual block devices */
672 while (!QSIMPLEQ_EMPTY(bdo_queue
)) {
673 BlockdevOptionsQueueEntry
*bdo
= QSIMPLEQ_FIRST(bdo_queue
);
675 QSIMPLEQ_REMOVE_HEAD(bdo_queue
, entry
);
676 loc_push_restore(&bdo
->loc
);
677 qmp_blockdev_add(bdo
->bdo
, &error_fatal
);
679 qapi_free_BlockdevOptions(bdo
->bdo
);
683 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
,
686 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
687 &machine_class
->block_default_type
, &error_fatal
)) {
688 /* We printed help */
692 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
694 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
695 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
699 static QemuOptsList qemu_smp_opts
= {
701 .implied_opt_name
= "cpus",
703 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
707 .type
= QEMU_OPT_NUMBER
,
710 .type
= QEMU_OPT_NUMBER
,
713 .type
= QEMU_OPT_NUMBER
,
716 .type
= QEMU_OPT_NUMBER
,
719 .type
= QEMU_OPT_NUMBER
,
722 .type
= QEMU_OPT_NUMBER
,
728 static void realtime_init(void)
731 if (os_mlock() < 0) {
732 error_report("locking memory failed");
739 static void configure_msg(QemuOpts
*opts
)
741 message_with_timestamp
= qemu_opt_get_bool(opts
, "timestamp", false);
742 error_with_guestname
= qemu_opt_get_bool(opts
, "guest-name", false);
746 /***********************************************************/
749 static int usb_device_add(const char *devname
)
751 USBDevice
*dev
= NULL
;
753 if (!machine_usb(current_machine
)) {
757 dev
= usbdevice_create(devname
);
764 static int usb_parse(const char *cmdline
)
767 r
= usb_device_add(cmdline
);
769 error_report("could not add USB device '%s'", cmdline
);
774 /***********************************************************/
775 /* machine registration */
777 static MachineClass
*find_machine(const char *name
, GSList
*machines
)
781 for (el
= machines
; el
; el
= el
->next
) {
782 MachineClass
*mc
= el
->data
;
784 if (!strcmp(mc
->name
, name
) || !g_strcmp0(mc
->alias
, name
)) {
792 static MachineClass
*find_default_machine(GSList
*machines
)
795 MachineClass
*default_machineclass
= NULL
;
797 for (el
= machines
; el
; el
= el
->next
) {
798 MachineClass
*mc
= el
->data
;
800 if (mc
->is_default
) {
801 assert(default_machineclass
== NULL
&& "Multiple default machines");
802 default_machineclass
= mc
;
806 return default_machineclass
;
809 static int machine_help_func(QemuOpts
*opts
, MachineState
*machine
)
811 ObjectProperty
*prop
;
812 ObjectPropertyIterator iter
;
814 if (!qemu_opt_has_help_opt(opts
)) {
818 object_property_iter_init(&iter
, OBJECT(machine
));
819 while ((prop
= object_property_iter_next(&iter
))) {
824 printf("%s.%s=%s", MACHINE_GET_CLASS(machine
)->name
,
825 prop
->name
, prop
->type
);
826 if (prop
->description
) {
827 printf(" (%s)\n", prop
->description
);
836 static void version(void)
838 printf("QEMU emulator version " QEMU_FULL_VERSION
"\n"
839 QEMU_COPYRIGHT
"\n");
842 static void help(int exitcode
)
845 printf("usage: %s [options] [disk_image]\n\n"
846 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
847 error_get_progname());
849 #define QEMU_OPTIONS_GENERATE_HELP
850 #include "qemu-options-wrapper.h"
852 printf("\nDuring emulation, the following keys are useful:\n"
853 "ctrl-alt-f toggle full screen\n"
854 "ctrl-alt-n switch to virtual console 'n'\n"
855 "ctrl-alt toggle mouse and keyboard grab\n"
857 "When using -nographic, press 'ctrl-a h' to get some help.\n"
859 QEMU_HELP_BOTTOM
"\n");
864 #define HAS_ARG 0x0001
866 typedef struct QEMUOption
{
873 static const QEMUOption qemu_options
[] = {
874 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
875 #define QEMU_OPTIONS_GENERATE_OPTIONS
876 #include "qemu-options-wrapper.h"
880 typedef struct VGAInterfaceInfo
{
881 const char *opt_name
; /* option name */
882 const char *name
; /* human-readable name */
883 /* Class names indicating that support is available.
884 * If no class is specified, the interface is always available */
885 const char *class_names
[2];
888 static const VGAInterfaceInfo vga_interfaces
[VGA_TYPE_MAX
] = {
891 .name
= "no graphic card",
895 .name
= "standard VGA",
896 .class_names
= { "VGA", "isa-vga" },
899 .opt_name
= "cirrus",
900 .name
= "Cirrus VGA",
901 .class_names
= { "cirrus-vga", "isa-cirrus-vga" },
904 .opt_name
= "vmware",
905 .name
= "VMWare SVGA",
906 .class_names
= { "vmware-svga" },
909 .opt_name
= "virtio",
910 .name
= "Virtio VGA",
911 .class_names
= { "virtio-vga" },
916 .class_names
= { "qxl-vga" },
920 .name
= "TCX framebuffer",
921 .class_names
= { "SUNW,tcx" },
925 .name
= "CG3 framebuffer",
926 .class_names
= { "cgthree" },
930 .name
= "Xen paravirtualized framebuffer",
934 static bool vga_interface_available(VGAInterfaceType t
)
936 const VGAInterfaceInfo
*ti
= &vga_interfaces
[t
];
938 assert(t
< VGA_TYPE_MAX
);
939 return !ti
->class_names
[0] ||
940 module_object_class_by_name(ti
->class_names
[0]) ||
941 module_object_class_by_name(ti
->class_names
[1]);
945 get_default_vga_model(const MachineClass
*machine_class
)
947 if (machine_class
->default_display
) {
948 return machine_class
->default_display
;
949 } else if (vga_interface_available(VGA_CIRRUS
)) {
951 } else if (vga_interface_available(VGA_STD
)) {
958 static void select_vgahw(const MachineClass
*machine_class
, const char *p
)
963 if (g_str_equal(p
, "help")) {
964 const char *def
= get_default_vga_model(machine_class
);
966 for (t
= 0; t
< VGA_TYPE_MAX
; t
++) {
967 const VGAInterfaceInfo
*ti
= &vga_interfaces
[t
];
969 if (vga_interface_available(t
) && ti
->opt_name
) {
970 printf("%-20s %s%s\n", ti
->opt_name
, ti
->name
?: "",
971 g_str_equal(ti
->opt_name
, def
) ? " (default)" : "");
977 assert(vga_interface_type
== VGA_NONE
);
978 for (t
= 0; t
< VGA_TYPE_MAX
; t
++) {
979 const VGAInterfaceInfo
*ti
= &vga_interfaces
[t
];
980 if (ti
->opt_name
&& strstart(p
, ti
->opt_name
, &opts
)) {
981 if (!vga_interface_available(t
)) {
982 error_report("%s not available", ti
->name
);
985 vga_interface_type
= t
;
989 if (t
== VGA_TYPE_MAX
) {
991 error_report("unknown vga type: %s", p
);
997 if (strstart(opts
, ",retrace=", &nextopt
)) {
999 if (strstart(opts
, "dumb", &nextopt
))
1000 vga_retrace_method
= VGA_RETRACE_DUMB
;
1001 else if (strstart(opts
, "precise", &nextopt
))
1002 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1003 else goto invalid_vga
;
1004 } else goto invalid_vga
;
1009 static void parse_display_qapi(const char *optarg
)
1011 DisplayOptions
*opts
;
1014 v
= qobject_input_visitor_new_str(optarg
, "type", &error_fatal
);
1016 visit_type_DisplayOptions(v
, NULL
, &opts
, &error_fatal
);
1017 QAPI_CLONE_MEMBERS(DisplayOptions
, &dpy
, opts
);
1019 qapi_free_DisplayOptions(opts
);
1023 DisplayOptions
*qmp_query_display_options(Error
**errp
)
1025 return QAPI_CLONE(DisplayOptions
, &dpy
);
1028 static void parse_display(const char *p
)
1032 if (is_help_option(p
)) {
1033 qemu_display_help();
1037 if (strstart(p
, "sdl", &opts
)) {
1039 * sdl DisplayType needs hand-crafted parser instead of
1040 * parse_display_qapi() due to some options not in
1041 * DisplayOptions, specifically:
1043 * Already deprecated.
1044 * - ctrl_grab + alt_grab
1045 * Not clear yet what happens to them long-term. Should
1046 * replaced by something better or deprecated and dropped.
1048 dpy
.type
= DISPLAY_TYPE_SDL
;
1050 const char *nextopt
;
1052 if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1054 if (strstart(opts
, "on", &nextopt
)) {
1056 } else if (strstart(opts
, "off", &nextopt
)) {
1059 goto invalid_sdl_args
;
1061 } else if (strstart(opts
, ",ctrl_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
, ",window_close=", &nextopt
)) {
1072 dpy
.has_window_close
= true;
1073 if (strstart(opts
, "on", &nextopt
)) {
1074 dpy
.window_close
= true;
1075 } else if (strstart(opts
, "off", &nextopt
)) {
1076 dpy
.window_close
= false;
1078 goto invalid_sdl_args
;
1080 } else if (strstart(opts
, ",show-cursor=", &nextopt
)) {
1082 dpy
.has_show_cursor
= true;
1083 if (strstart(opts
, "on", &nextopt
)) {
1084 dpy
.show_cursor
= true;
1085 } else if (strstart(opts
, "off", &nextopt
)) {
1086 dpy
.show_cursor
= false;
1088 goto invalid_sdl_args
;
1090 } else if (strstart(opts
, ",gl=", &nextopt
)) {
1093 if (strstart(opts
, "on", &nextopt
)) {
1094 dpy
.gl
= DISPLAYGL_MODE_ON
;
1095 } else if (strstart(opts
, "core", &nextopt
)) {
1096 dpy
.gl
= DISPLAYGL_MODE_CORE
;
1097 } else if (strstart(opts
, "es", &nextopt
)) {
1098 dpy
.gl
= DISPLAYGL_MODE_ES
;
1099 } else if (strstart(opts
, "off", &nextopt
)) {
1100 dpy
.gl
= DISPLAYGL_MODE_OFF
;
1102 goto invalid_sdl_args
;
1106 error_report("invalid SDL option string");
1111 } else if (strstart(p
, "vnc", &opts
)) {
1113 * vnc isn't a (local) DisplayType but a protocol for remote
1117 vnc_parse(opts
+ 1);
1119 error_report("VNC requires a display argument vnc=<display>");
1123 parse_display_qapi(p
);
1127 static inline bool nonempty_str(const char *str
)
1132 static int parse_fw_cfg(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1136 const char *name
, *file
, *str
, *gen_id
;
1137 FWCfgState
*fw_cfg
= (FWCfgState
*) opaque
;
1139 if (fw_cfg
== NULL
) {
1140 error_setg(errp
, "fw_cfg device not available");
1143 name
= qemu_opt_get(opts
, "name");
1144 file
= qemu_opt_get(opts
, "file");
1145 str
= qemu_opt_get(opts
, "string");
1146 gen_id
= qemu_opt_get(opts
, "gen_id");
1148 /* we need the name, and exactly one of: file, content string, gen_id */
1149 if (!nonempty_str(name
) ||
1150 nonempty_str(file
) + nonempty_str(str
) + nonempty_str(gen_id
) != 1) {
1151 error_setg(errp
, "name, plus exactly one of file,"
1152 " string and gen_id, are needed");
1155 if (strlen(name
) > FW_CFG_MAX_FILE_PATH
- 1) {
1156 error_setg(errp
, "name too long (max. %d char)",
1157 FW_CFG_MAX_FILE_PATH
- 1);
1160 if (nonempty_str(gen_id
)) {
1162 * In this particular case where the content is populated
1163 * internally, the "etc/" namespace protection is relaxed,
1164 * so do not emit a warning.
1166 } else if (strncmp(name
, "opt/", 4) != 0) {
1167 warn_report("externally provided fw_cfg item names "
1168 "should be prefixed with \"opt/\"");
1170 if (nonempty_str(str
)) {
1171 size
= strlen(str
); /* NUL terminator NOT included in fw_cfg blob */
1172 buf
= g_memdup(str
, size
);
1173 } else if (nonempty_str(gen_id
)) {
1174 if (!fw_cfg_add_from_generator(fw_cfg
, name
, gen_id
, errp
)) {
1180 if (!g_file_get_contents(file
, &buf
, &size
, &err
)) {
1181 error_setg(errp
, "can't load %s: %s", file
, err
->message
);
1186 /* For legacy, keep user files in a specific global order. */
1187 fw_cfg_set_order_override(fw_cfg
, FW_CFG_ORDER_OVERRIDE_USER
);
1188 fw_cfg_add_file(fw_cfg
, name
, buf
, size
);
1189 fw_cfg_reset_order_override(fw_cfg
);
1193 static int device_help_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1195 return qdev_device_help(opts
);
1198 static int device_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1202 dev
= qdev_device_add(opts
, errp
);
1203 if (!dev
&& *errp
) {
1204 error_report_err(*errp
);
1207 object_unref(OBJECT(dev
));
1212 static int chardev_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1214 Error
*local_err
= NULL
;
1216 if (!qemu_chr_new_from_opts(opts
, NULL
, &local_err
)) {
1218 error_propagate(errp
, local_err
);
1226 #ifdef CONFIG_VIRTFS
1227 static int fsdev_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1229 return qemu_fsdev_add(opts
, errp
);
1233 static int mon_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1235 return monitor_init_opts(opts
, errp
);
1238 static void monitor_parse(const char *optarg
, const char *mode
, bool pretty
)
1240 static int monitor_device_index
= 0;
1245 if (strstart(optarg
, "chardev:", &p
)) {
1246 snprintf(label
, sizeof(label
), "%s", p
);
1248 snprintf(label
, sizeof(label
), "compat_monitor%d",
1249 monitor_device_index
);
1250 opts
= qemu_chr_parse_compat(label
, optarg
, true);
1252 error_report("parse error: %s", optarg
);
1257 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, &error_fatal
);
1258 qemu_opt_set(opts
, "mode", mode
, &error_abort
);
1259 qemu_opt_set(opts
, "chardev", label
, &error_abort
);
1260 if (!strcmp(mode
, "control")) {
1261 qemu_opt_set_bool(opts
, "pretty", pretty
, &error_abort
);
1263 assert(pretty
== false);
1265 monitor_device_index
++;
1268 struct device_config
{
1270 DEV_USB
, /* -usbdevice */
1271 DEV_SERIAL
, /* -serial */
1272 DEV_PARALLEL
, /* -parallel */
1273 DEV_DEBUGCON
, /* -debugcon */
1274 DEV_GDB
, /* -gdb, -s */
1275 DEV_SCLP
, /* s390 sclp */
1277 const char *cmdline
;
1279 QTAILQ_ENTRY(device_config
) next
;
1282 static QTAILQ_HEAD(, device_config
) device_configs
=
1283 QTAILQ_HEAD_INITIALIZER(device_configs
);
1285 static void add_device_config(int type
, const char *cmdline
)
1287 struct device_config
*conf
;
1289 conf
= g_malloc0(sizeof(*conf
));
1291 conf
->cmdline
= cmdline
;
1292 loc_save(&conf
->loc
);
1293 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1296 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1298 struct device_config
*conf
;
1301 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1302 if (conf
->type
!= type
)
1304 loc_push_restore(&conf
->loc
);
1305 rc
= func(conf
->cmdline
);
1306 loc_pop(&conf
->loc
);
1314 static void qemu_disable_default_devices(void)
1316 MachineClass
*machine_class
= MACHINE_GET_CLASS(current_machine
);
1318 qemu_opts_foreach(qemu_find_opts("device"),
1319 default_driver_check
, NULL
, NULL
);
1320 qemu_opts_foreach(qemu_find_opts("global"),
1321 default_driver_check
, NULL
, NULL
);
1323 if (!vga_model
&& !default_vga
) {
1324 vga_interface_type
= VGA_DEVICE
;
1326 if (!has_defaults
|| machine_class
->no_serial
) {
1329 if (!has_defaults
|| machine_class
->no_parallel
) {
1330 default_parallel
= 0;
1332 if (!has_defaults
|| machine_class
->no_floppy
) {
1335 if (!has_defaults
|| machine_class
->no_cdrom
) {
1338 if (!has_defaults
|| machine_class
->no_sdcard
) {
1341 if (!has_defaults
) {
1342 default_monitor
= 0;
1348 static void qemu_create_default_devices(void)
1350 MachineClass
*machine_class
= MACHINE_GET_CLASS(current_machine
);
1352 if (is_daemonized()) {
1353 /* According to documentation and historically, -nographic redirects
1354 * serial port, parallel port and monitor to stdio, which does not work
1355 * with -daemonize. We can redirect these to null instead, but since
1356 * -nographic is legacy, let's just error out.
1357 * We disallow -nographic only if all other ports are not redirected
1358 * explicitly, to not break existing legacy setups which uses
1359 * -nographic _and_ redirects all ports explicitly - this is valid
1360 * usage, -nographic is just a no-op in this case.
1363 && (default_parallel
|| default_serial
|| default_monitor
)) {
1364 error_report("-nographic cannot be used with -daemonize");
1370 if (default_parallel
)
1371 add_device_config(DEV_PARALLEL
, "null");
1372 if (default_serial
&& default_monitor
) {
1373 add_device_config(DEV_SERIAL
, "mon:stdio");
1376 add_device_config(DEV_SERIAL
, "stdio");
1377 if (default_monitor
)
1378 monitor_parse("stdio", "readline", false);
1382 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
1383 if (default_parallel
)
1384 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
1385 if (default_monitor
)
1386 monitor_parse("vc:80Cx24C", "readline", false);
1390 QemuOptsList
*net
= qemu_find_opts("net");
1391 qemu_opts_parse(net
, "nic", true, &error_abort
);
1393 qemu_opts_parse(net
, "user", true, &error_abort
);
1397 #if defined(CONFIG_VNC)
1398 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head
))) {
1402 if (dpy
.type
== DISPLAY_TYPE_DEFAULT
&& !display_remote
) {
1403 if (!qemu_display_find_default(&dpy
)) {
1404 dpy
.type
= DISPLAY_TYPE_NONE
;
1405 #if defined(CONFIG_VNC)
1406 vnc_parse("localhost:0,to=99,id=default");
1410 if (dpy
.type
== DISPLAY_TYPE_DEFAULT
) {
1411 dpy
.type
= DISPLAY_TYPE_NONE
;
1414 /* If no default VGA is requested, the default is "none". */
1416 vga_model
= get_default_vga_model(machine_class
);
1419 select_vgahw(machine_class
, vga_model
);
1423 static int serial_parse(const char *devname
)
1425 int index
= num_serial_hds
;
1428 if (strcmp(devname
, "none") == 0)
1430 snprintf(label
, sizeof(label
), "serial%d", index
);
1431 serial_hds
= g_renew(Chardev
*, serial_hds
, index
+ 1);
1433 serial_hds
[index
] = qemu_chr_new_mux_mon(label
, devname
, NULL
);
1434 if (!serial_hds
[index
]) {
1435 error_report("could not connect serial device"
1436 " to character backend '%s'", devname
);
1443 Chardev
*serial_hd(int i
)
1446 if (i
< num_serial_hds
) {
1447 return serial_hds
[i
];
1452 static int parallel_parse(const char *devname
)
1454 static int index
= 0;
1457 if (strcmp(devname
, "none") == 0)
1459 if (index
== MAX_PARALLEL_PORTS
) {
1460 error_report("too many parallel ports");
1463 snprintf(label
, sizeof(label
), "parallel%d", index
);
1464 parallel_hds
[index
] = qemu_chr_new_mux_mon(label
, devname
, NULL
);
1465 if (!parallel_hds
[index
]) {
1466 error_report("could not connect parallel device"
1467 " to character backend '%s'", devname
);
1474 static int debugcon_parse(const char *devname
)
1478 if (!qemu_chr_new_mux_mon("debugcon", devname
, NULL
)) {
1479 error_report("invalid character backend '%s'", devname
);
1482 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
1484 error_report("already have a debugcon device");
1487 qemu_opt_set(opts
, "driver", "isa-debugcon", &error_abort
);
1488 qemu_opt_set(opts
, "chardev", "debugcon", &error_abort
);
1492 static gint
machine_class_cmp(gconstpointer a
, gconstpointer b
)
1494 const MachineClass
*mc1
= a
, *mc2
= b
;
1497 if (mc1
->family
== NULL
) {
1498 if (mc2
->family
== NULL
) {
1499 /* Compare standalone machine types against each other; they sort
1500 * in increasing order.
1502 return strcmp(object_class_get_name(OBJECT_CLASS(mc1
)),
1503 object_class_get_name(OBJECT_CLASS(mc2
)));
1506 /* Standalone machine types sort after families. */
1510 if (mc2
->family
== NULL
) {
1511 /* Families sort before standalone machine types. */
1515 /* Families sort between each other alphabetically increasingly. */
1516 res
= strcmp(mc1
->family
, mc2
->family
);
1521 /* Within the same family, machine types sort in decreasing order. */
1522 return strcmp(object_class_get_name(OBJECT_CLASS(mc2
)),
1523 object_class_get_name(OBJECT_CLASS(mc1
)));
1526 static MachineClass
*machine_parse(const char *name
, GSList
*machines
)
1531 if (is_help_option(name
)) {
1532 printf("Supported machines are:\n");
1533 machines
= g_slist_sort(machines
, machine_class_cmp
);
1534 for (el
= machines
; el
; el
= el
->next
) {
1535 MachineClass
*mc
= el
->data
;
1537 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
1539 printf("%-20s %s%s%s\n", mc
->name
, mc
->desc
,
1540 mc
->is_default
? " (default)" : "",
1541 mc
->deprecation_reason
? " (deprecated)" : "");
1546 mc
= find_machine(name
, machines
);
1548 error_report("unsupported machine type");
1549 error_printf("Use -machine help to list supported machines\n");
1555 static const char *pid_file
;
1556 static Notifier qemu_unlink_pidfile_notifier
;
1558 static void qemu_unlink_pidfile(Notifier
*n
, void *data
)
1565 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
1566 const char **poptarg
, int *poptind
)
1568 const QEMUOption
*popt
;
1569 int optind
= *poptind
;
1570 char *r
= argv
[optind
];
1573 loc_set_cmdline(argv
, optind
, 1);
1575 /* Treat --foo the same as -foo. */
1578 popt
= qemu_options
;
1581 error_report("invalid option");
1584 if (!strcmp(popt
->name
, r
+ 1))
1588 if (popt
->flags
& HAS_ARG
) {
1589 if (optind
>= argc
) {
1590 error_report("requires an argument");
1593 optarg
= argv
[optind
++];
1594 loc_set_cmdline(argv
, optind
- 2, 2);
1605 static MachineClass
*select_machine(void)
1607 GSList
*machines
= object_class_get_list(TYPE_MACHINE
, false);
1608 MachineClass
*machine_class
= find_default_machine(machines
);
1613 loc_push_none(&loc
);
1615 opts
= qemu_get_machine_opts();
1616 qemu_opts_loc_restore(opts
);
1618 optarg
= qemu_opt_get(opts
, "type");
1620 machine_class
= machine_parse(optarg
, machines
);
1623 if (!machine_class
) {
1624 error_report("No machine specified, and there is no default");
1625 error_printf("Use -machine help to list supported machines\n");
1630 g_slist_free(machines
);
1631 return machine_class
;
1634 static int object_parse_property_opt(Object
*obj
,
1635 const char *name
, const char *value
,
1636 const char *skip
, Error
**errp
)
1638 if (g_str_equal(name
, skip
)) {
1642 if (!object_property_parse(obj
, name
, value
, errp
)) {
1649 static int machine_set_property(void *opaque
,
1650 const char *name
, const char *value
,
1653 g_autofree
char *qom_name
= g_strdup(name
);
1656 for (p
= qom_name
; *p
; p
++) {
1662 /* Legacy options do not correspond to MachineState properties. */
1663 if (g_str_equal(qom_name
, "accel")) {
1666 if (g_str_equal(qom_name
, "igd-passthru")) {
1667 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name
, value
,
1671 if (g_str_equal(qom_name
, "kvm-shadow-mem")) {
1672 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name
, value
,
1676 if (g_str_equal(qom_name
, "kernel-irqchip")) {
1677 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name
, value
,
1679 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name
, value
,
1684 return object_parse_property_opt(opaque
, name
, value
, "type", errp
);
1688 * Initial object creation happens before all other
1689 * QEMU data types are created. The majority of objects
1690 * can be created at this point. The rng-egd object
1691 * cannot be created here, as it depends on the chardev
1694 static bool object_create_early(const char *type
, QemuOpts
*opts
)
1696 if (user_creatable_print_help(type
, opts
)) {
1701 * Objects should not be made "delayed" without a reason. If you
1702 * add one, state the reason in a comment!
1705 /* Reason: rng-egd property "chardev" */
1706 if (g_str_equal(type
, "rng-egd")) {
1710 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1711 /* Reason: cryptodev-vhost-user property "chardev" */
1712 if (g_str_equal(type
, "cryptodev-vhost-user")) {
1717 /* Reason: vhost-user-blk-server property "node-name" */
1718 if (g_str_equal(type
, "vhost-user-blk-server")) {
1722 * Reason: filter-* property "netdev" etc.
1724 if (g_str_equal(type
, "filter-buffer") ||
1725 g_str_equal(type
, "filter-dump") ||
1726 g_str_equal(type
, "filter-mirror") ||
1727 g_str_equal(type
, "filter-redirector") ||
1728 g_str_equal(type
, "colo-compare") ||
1729 g_str_equal(type
, "filter-rewriter") ||
1730 g_str_equal(type
, "filter-replay")) {
1735 * Allocation of large amounts of memory may delay
1736 * chardev initialization for too long, and trigger timeouts
1737 * on software that waits for a monitor socket to be created
1740 if (g_str_has_prefix(type
, "memory-backend-")) {
1747 static void qemu_apply_machine_options(void)
1749 MachineClass
*machine_class
= MACHINE_GET_CLASS(current_machine
);
1750 QemuOpts
*machine_opts
= qemu_get_machine_opts();
1751 const char *boot_order
= NULL
;
1752 const char *boot_once
= NULL
;
1755 qemu_opt_foreach(machine_opts
, machine_set_property
, current_machine
,
1757 current_machine
->ram_size
= ram_size
;
1758 current_machine
->maxram_size
= maxram_size
;
1759 current_machine
->ram_slots
= ram_slots
;
1761 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
1763 boot_order
= qemu_opt_get(opts
, "order");
1765 validate_bootdevices(boot_order
, &error_fatal
);
1768 boot_once
= qemu_opt_get(opts
, "once");
1770 validate_bootdevices(boot_once
, &error_fatal
);
1773 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
1774 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
1778 boot_order
= machine_class
->default_boot_order
;
1781 current_machine
->boot_order
= boot_order
;
1782 current_machine
->boot_once
= boot_once
;
1784 if (semihosting_enabled() && !semihosting_get_argc()) {
1785 const char *kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
1786 const char *kernel_cmdline
= qemu_opt_get(machine_opts
, "append") ?: "";
1787 /* fall back to the -kernel/-append */
1788 semihosting_arg_fallback(kernel_filename
, kernel_cmdline
);
1792 static void qemu_create_early_backends(void)
1794 MachineClass
*machine_class
= MACHINE_GET_CLASS(current_machine
);
1796 if ((alt_grab
|| ctrl_grab
) && dpy
.type
!= DISPLAY_TYPE_SDL
) {
1797 error_report("-alt-grab and -ctrl-grab are only valid "
1798 "for SDL, ignoring option");
1800 if (dpy
.has_window_close
&&
1801 (dpy
.type
!= DISPLAY_TYPE_GTK
&& dpy
.type
!= DISPLAY_TYPE_SDL
)) {
1802 error_report("-no-quit is only valid for GTK and SDL, "
1806 qemu_display_early_init(&dpy
);
1807 qemu_console_early_init();
1809 if (dpy
.has_gl
&& dpy
.gl
!= DISPLAYGL_MODE_OFF
&& display_opengl
== 0) {
1810 #if defined(CONFIG_OPENGL)
1811 error_report("OpenGL is not supported by the display");
1813 error_report("OpenGL support is disabled");
1818 qemu_opts_foreach(qemu_find_opts("object"),
1819 user_creatable_add_opts_foreach
,
1820 object_create_early
, &error_fatal
);
1822 /* spice needs the timers to be initialized by this point */
1823 /* spice must initialize before audio as it changes the default auiodev */
1824 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1827 qemu_opts_foreach(qemu_find_opts("chardev"),
1828 chardev_init_func
, NULL
, &error_fatal
);
1830 #ifdef CONFIG_VIRTFS
1831 qemu_opts_foreach(qemu_find_opts("fsdev"),
1832 fsdev_init_func
, NULL
, &error_fatal
);
1836 * Note: we need to create audio and block backends before
1837 * machine_set_property(), so machine properties can refer to
1840 configure_blockdev(&bdo_queue
, machine_class
, snapshot
);
1841 audio_init_audiodevs();
1846 * The remainder of object creation happens after the
1847 * creation of chardev, fsdev, net clients and device data types.
1849 static bool object_create_late(const char *type
, QemuOpts
*opts
)
1851 return !object_create_early(type
, opts
);
1854 static void qemu_create_late_backends(void)
1857 qtest_server_init(qtest_chrdev
, qtest_log
, &error_fatal
);
1860 net_init_clients(&error_fatal
);
1862 qemu_opts_foreach(qemu_find_opts("object"),
1863 user_creatable_add_opts_foreach
,
1864 object_create_late
, &error_fatal
);
1866 if (tpm_init() < 0) {
1870 qemu_opts_foreach(qemu_find_opts("mon"),
1871 mon_init_func
, NULL
, &error_fatal
);
1873 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
1875 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
1877 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
1880 /* now chardevs have been created we may have semihosting to connect */
1881 qemu_semihosting_connect_chardevs();
1882 qemu_semihosting_console_init();
1885 static bool have_custom_ram_size(void)
1887 QemuOpts
*opts
= qemu_find_opts_singleton("memory");
1888 return !!qemu_opt_get_size(opts
, "size", 0);
1891 static void qemu_resolve_machine_memdev(void)
1893 if (current_machine
->ram_memdev_id
) {
1895 ram_addr_t backend_size
;
1897 backend
= object_resolve_path_type(current_machine
->ram_memdev_id
,
1898 TYPE_MEMORY_BACKEND
, NULL
);
1900 error_report("Memory backend '%s' not found",
1901 current_machine
->ram_memdev_id
);
1904 backend_size
= object_property_get_uint(backend
, "size", &error_abort
);
1905 if (have_custom_ram_size() && backend_size
!= ram_size
) {
1906 error_report("Size specified by -m option must match size of "
1907 "explicitly specified 'memory-backend' property");
1911 error_report("'-mem-path' can't be used together with"
1912 "'-machine memory-backend'");
1915 ram_size
= backend_size
;
1918 if (!xen_enabled()) {
1919 /* On 32-bit hosts, QEMU is limited by virtual address space */
1920 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
1921 error_report("at most 2047 MB RAM can be simulated");
1927 static void set_memory_options(MachineClass
*mc
)
1930 const char *mem_str
;
1931 const ram_addr_t default_ram_size
= mc
->default_ram_size
;
1932 QemuOpts
*opts
= qemu_find_opts_singleton("memory");
1935 loc_push_none(&loc
);
1936 qemu_opts_loc_restore(opts
);
1939 mem_str
= qemu_opt_get(opts
, "size");
1942 error_report("missing 'size' option value");
1946 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
1948 /* Fix up legacy suffix-less format */
1949 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
1950 uint64_t overflow_check
= sz
;
1953 if (sz
/ MiB
!= overflow_check
) {
1954 error_report("too large 'size' option value");
1960 /* backward compatibility behaviour for case "-m 0" */
1962 sz
= default_ram_size
;
1965 sz
= QEMU_ALIGN_UP(sz
, 8192);
1966 if (mc
->fixup_ram_size
) {
1967 sz
= mc
->fixup_ram_size(sz
);
1970 if (ram_size
!= sz
) {
1971 error_report("ram size too large");
1975 /* store value for the future use */
1976 qemu_opt_set_number(opts
, "size", ram_size
, &error_abort
);
1977 maxram_size
= ram_size
;
1979 if (qemu_opt_get(opts
, "maxmem")) {
1982 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
1983 slots
= qemu_opt_get_number(opts
, "slots", 0);
1984 if (sz
< ram_size
) {
1985 error_report("invalid value of -m option maxmem: "
1986 "maximum memory size (0x%" PRIx64
") must be at least "
1987 "the initial memory size (0x" RAM_ADDR_FMT
")",
1990 } else if (slots
&& sz
== ram_size
) {
1991 error_report("invalid value of -m option maxmem: "
1992 "memory slots were specified but maximum memory size "
1993 "(0x%" PRIx64
") is equal to the initial memory size "
1994 "(0x" RAM_ADDR_FMT
")", sz
, ram_size
);
2000 } else if (qemu_opt_get(opts
, "slots")) {
2001 error_report("invalid -m option value: missing 'maxmem' option");
2008 static void qemu_create_machine(MachineClass
*machine_class
)
2010 object_set_machine_compat_props(machine_class
->compat_props
);
2012 set_memory_options(machine_class
);
2014 current_machine
= MACHINE(object_new_with_class(OBJECT_CLASS(machine_class
)));
2015 if (machine_help_func(qemu_get_machine_opts(), current_machine
)) {
2018 object_property_add_child(object_get_root(), "machine",
2019 OBJECT(current_machine
));
2020 object_property_add_child(container_get(OBJECT(current_machine
),
2022 "sysbus", OBJECT(sysbus_get_default()));
2024 if (machine_class
->minimum_page_bits
) {
2025 if (!set_preferred_target_page_bits(machine_class
->minimum_page_bits
)) {
2026 /* This would be a board error: specifying a minimum smaller than
2027 * a target's compile-time fixed setting.
2029 g_assert_not_reached();
2033 cpu_exec_init_all();
2036 if (machine_class
->hw_version
) {
2037 qemu_set_hw_version(machine_class
->hw_version
);
2040 machine_smp_parse(current_machine
,
2041 qemu_opts_find(qemu_find_opts("smp-opts"), NULL
), &error_fatal
);
2044 * Get the default machine options from the machine if it is not already
2045 * specified either by the configuration file or by the command line.
2047 if (machine_class
->default_machine_opts
) {
2048 qemu_opts_set_defaults(qemu_find_opts("machine"),
2049 machine_class
->default_machine_opts
, 0);
2053 static int global_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2057 g
= g_malloc0(sizeof(*g
));
2058 g
->driver
= qemu_opt_get(opts
, "driver");
2059 g
->property
= qemu_opt_get(opts
, "property");
2060 g
->value
= qemu_opt_get(opts
, "value");
2061 qdev_prop_register_global(g
);
2065 static int qemu_read_default_config_file(void)
2068 g_autofree
char *file
= get_relocated_path(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2070 ret
= qemu_read_config_file(file
);
2071 if (ret
< 0 && ret
!= -ENOENT
) {
2078 static int qemu_set_option(const char *str
)
2080 Error
*local_err
= NULL
;
2081 char group
[64], id
[64], arg
[64];
2086 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
2087 if (rc
< 3 || str
[offset
] != '=') {
2088 error_report("can't parse: \"%s\"", str
);
2092 list
= qemu_find_opts(group
);
2097 opts
= qemu_opts_find(list
, id
);
2099 error_report("there is no %s \"%s\" defined",
2104 if (!qemu_opt_set(opts
, arg
, str
+ offset
+ 1, &local_err
)) {
2105 error_report_err(local_err
);
2111 static void user_register_global_props(void)
2113 qemu_opts_foreach(qemu_find_opts("global"),
2114 global_init_func
, NULL
, NULL
);
2117 static int do_configure_icount(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2119 icount_configure(opts
, errp
);
2123 static int accelerator_set_property(void *opaque
,
2124 const char *name
, const char *value
,
2127 return object_parse_property_opt(opaque
, name
, value
, "accel", errp
);
2130 static int do_configure_accelerator(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2132 bool *p_init_failed
= opaque
;
2133 const char *acc
= qemu_opt_get(opts
, "accel");
2134 AccelClass
*ac
= accel_find(acc
);
2137 bool qtest_with_kvm
;
2139 qtest_with_kvm
= g_str_equal(acc
, "kvm") && qtest_chrdev
!= NULL
;
2142 *p_init_failed
= true;
2143 if (!qtest_with_kvm
) {
2144 error_report("invalid accelerator %s", acc
);
2148 accel
= ACCEL(object_new_with_class(OBJECT_CLASS(ac
)));
2149 object_apply_compat_props(OBJECT(accel
));
2150 qemu_opt_foreach(opts
, accelerator_set_property
,
2154 ret
= accel_init_machine(accel
, current_machine
);
2156 *p_init_failed
= true;
2157 if (!qtest_with_kvm
|| ret
!= -ENOENT
) {
2158 error_report("failed to initialize %s: %s", acc
, strerror(-ret
));
2166 static void configure_accelerators(const char *progname
)
2168 const char *accelerators
;
2169 bool init_failed
= false;
2171 qemu_opts_foreach(qemu_find_opts("icount"),
2172 do_configure_icount
, NULL
, &error_fatal
);
2174 accelerators
= qemu_opt_get(qemu_get_machine_opts(), "accel");
2175 if (QTAILQ_EMPTY(&qemu_accel_opts
.head
)) {
2176 char **accel_list
, **tmp
;
2178 if (accelerators
== NULL
) {
2179 /* Select the default accelerator */
2180 bool have_tcg
= accel_find("tcg");
2181 bool have_kvm
= accel_find("kvm");
2183 if (have_tcg
&& have_kvm
) {
2184 if (g_str_has_suffix(progname
, "kvm")) {
2185 /* If the program name ends with "kvm", we prefer KVM */
2186 accelerators
= "kvm:tcg";
2188 accelerators
= "tcg:kvm";
2190 } else if (have_kvm
) {
2191 accelerators
= "kvm";
2192 } else if (have_tcg
) {
2193 accelerators
= "tcg";
2195 error_report("No accelerator selected and"
2196 " no default accelerator available");
2200 accel_list
= g_strsplit(accelerators
, ":", 0);
2202 for (tmp
= accel_list
; *tmp
; tmp
++) {
2204 * Filter invalid accelerators here, to prevent obscenities
2205 * such as "-machine accel=tcg,,thread=single".
2207 if (accel_find(*tmp
)) {
2208 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp
, true);
2211 error_report("invalid accelerator %s", *tmp
);
2214 g_strfreev(accel_list
);
2216 if (accelerators
!= NULL
) {
2217 error_report("The -accel and \"-machine accel=\" options are incompatible");
2222 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2223 do_configure_accelerator
, &init_failed
, &error_fatal
)) {
2225 error_report("no accelerator found");
2230 if (init_failed
&& !qtest_chrdev
) {
2231 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
2232 error_report("falling back to %s", ac
->name
);
2235 if (icount_enabled() && !tcg_enabled()) {
2236 error_report("-icount is not allowed with hardware virtualization");
2241 static void create_default_memdev(MachineState
*ms
, const char *path
)
2244 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
2246 obj
= object_new(path
? TYPE_MEMORY_BACKEND_FILE
: TYPE_MEMORY_BACKEND_RAM
);
2248 object_property_set_str(obj
, "mem-path", path
, &error_fatal
);
2250 object_property_set_int(obj
, "size", ms
->ram_size
, &error_fatal
);
2251 object_property_add_child(object_get_objects_root(), mc
->default_ram_id
,
2253 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2254 object_property_set_bool(obj
, "x-use-canonical-path-for-ramblock-id",
2255 false, &error_fatal
);
2256 user_creatable_complete(USER_CREATABLE(obj
), &error_fatal
);
2258 object_property_set_str(OBJECT(ms
), "memory-backend", mc
->default_ram_id
,
2262 static void qemu_validate_options(void)
2264 QemuOpts
*machine_opts
= qemu_get_machine_opts();
2265 const char *kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
2266 const char *initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
2267 const char *kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
2269 if (kernel_filename
== NULL
) {
2270 if (kernel_cmdline
!= NULL
) {
2271 error_report("-append only allowed with -kernel option");
2275 if (initrd_filename
!= NULL
) {
2276 error_report("-initrd only allowed with -kernel option");
2281 if (loadvm
&& preconfig_requested
) {
2282 error_report("'preconfig' and 'loadvm' options are "
2283 "mutually exclusive");
2286 if (incoming
&& preconfig_requested
&& strcmp(incoming
, "defer") != 0) {
2287 error_report("'preconfig' supports '-incoming defer' only");
2291 #ifdef CONFIG_CURSES
2292 if (is_daemonized() && dpy
.type
== DISPLAY_TYPE_CURSES
) {
2293 error_report("curses display cannot be used with -daemonize");
2299 static void qemu_process_sugar_options(void)
2304 val
= g_strdup_printf("%d",
2305 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2306 object_register_sugar_prop("memory-backend", "prealloc-threads", val
,
2309 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2313 int i
= select_watchdog(watchdog
);
2315 exit (i
== 1 ? 1 : 0);
2319 /* -action processing */
2322 * Process all the -action parameters parsed from cmdline.
2324 static int process_runstate_actions(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2326 Error
*local_err
= NULL
;
2327 QDict
*qdict
= qemu_opts_to_qdict(opts
, NULL
);
2328 QObject
*ret
= NULL
;
2329 qmp_marshal_set_action(qdict
, &ret
, &local_err
);
2331 qobject_unref(qdict
);
2333 error_propagate(errp
, local_err
);
2339 static void qemu_process_early_options(void)
2341 #ifdef CONFIG_SECCOMP
2342 QemuOptsList
*olist
= qemu_find_opts_err("sandbox", NULL
);
2344 qemu_opts_foreach(olist
, parse_sandbox
, NULL
, &error_fatal
);
2348 qemu_opts_foreach(qemu_find_opts("name"),
2349 parse_name
, NULL
, &error_fatal
);
2351 if (qemu_opts_foreach(qemu_find_opts("action"),
2352 process_runstate_actions
, NULL
, &error_fatal
)) {
2357 qemu_opts_foreach(qemu_find_opts("add-fd"),
2358 parse_add_fd
, NULL
, &error_fatal
);
2360 qemu_opts_foreach(qemu_find_opts("add-fd"),
2361 cleanup_add_fd
, NULL
, &error_fatal
);
2364 if (!trace_init_backends()) {
2369 /* Open the logfile at this point and set the log mask if necessary. */
2370 qemu_set_log_filename(log_file
, &error_fatal
);
2373 mask
= qemu_str_to_log_mask(log_mask
);
2375 qemu_print_log_usage(stdout
);
2383 qemu_add_default_firmwarepath();
2386 static void qemu_process_help_options(void)
2389 * Check for -cpu help and -device help before we call select_machine(),
2390 * which will return an error if the architecture has no default machine
2391 * type and the user did not specify one, so that the user doesn't need
2392 * to say '-cpu help -machine something'.
2394 if (cpu_option
&& is_help_option(cpu_option
)) {
2395 list_cpus(cpu_option
);
2399 if (qemu_opts_foreach(qemu_find_opts("device"),
2400 device_help_func
, NULL
, NULL
)) {
2404 /* -L help lists the data directories and exits. */
2405 if (list_data_dirs
) {
2406 qemu_list_data_dirs();
2411 static void qemu_maybe_daemonize(const char *pid_file
)
2416 rcu_disable_atfork();
2418 if (pid_file
&& !qemu_write_pidfile(pid_file
, &err
)) {
2419 error_reportf_err(err
, "cannot create PID file: ");
2423 qemu_unlink_pidfile_notifier
.notify
= qemu_unlink_pidfile
;
2424 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier
);
2427 static void qemu_init_displays(void)
2431 /* init local displays */
2432 ds
= init_displaystate();
2433 qemu_display_init(ds
, &dpy
);
2435 /* must be after terminal init, SDL library changes signal handlers */
2436 os_setup_signal_handling();
2438 /* init remote displays */
2440 qemu_opts_foreach(qemu_find_opts("vnc"),
2441 vnc_init_func
, NULL
, &error_fatal
);
2445 qemu_spice
.display_init();
2449 static void qemu_init_board(void)
2451 MachineClass
*machine_class
= MACHINE_GET_CLASS(current_machine
);
2453 if (machine_class
->default_ram_id
&& current_machine
->ram_size
&&
2454 numa_uses_legacy_mem() && !current_machine
->ram_memdev_id
) {
2455 create_default_memdev(current_machine
, mem_path
);
2458 /* process plugin before CPUs are created, but once -smp has been parsed */
2459 qemu_plugin_load_list(&plugin_list
, &error_fatal
);
2461 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2462 machine_run_board_init(current_machine
);
2465 * TODO To drop support for deprecated bogus if=..., move
2466 * drive_check_orphaned() here, replacing this call. Also drop
2467 * its deprecation warning, along with DriveInfo member
2468 * @claimed_by_board.
2470 drive_mark_claimed_by_board();
2474 if (hax_enabled()) {
2475 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2480 static void qemu_create_cli_devices(void)
2484 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2485 parse_fw_cfg
, fw_cfg_find(), &error_fatal
);
2487 /* init USB devices */
2488 if (machine_usb(current_machine
)) {
2489 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
2493 /* init generic devices */
2494 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE
);
2495 qemu_opts_foreach(qemu_find_opts("device"),
2496 device_init_func
, NULL
, &error_fatal
);
2497 rom_reset_order_override();
2500 static void qemu_machine_creation_done(void)
2502 MachineState
*machine
= MACHINE(qdev_get_machine());
2504 /* Did we create any drives that we failed to create a device for? */
2505 drive_check_orphaned();
2507 /* Don't warn about the default network setup that you get if
2508 * no command line -net or -netdev options are specified. There
2509 * are two cases that we would otherwise complain about:
2510 * (1) board doesn't support a NIC but the implicit "-net nic"
2512 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2513 * sets up a nic that isn't connected to anything.
2515 if (!default_net
&& (!qtest_enabled() || has_defaults
)) {
2516 net_check_clients();
2519 qdev_prop_check_globals();
2521 qdev_machine_creation_done();
2525 * Verify that Confidential Guest Support has actually been initialized
2527 assert(machine
->cgs
->ready
);
2530 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
2535 void qmp_x_exit_preconfig(Error
**errp
)
2537 if (phase_check(PHASE_MACHINE_INITIALIZED
)) {
2538 error_setg(errp
, "The command is permitted only before machine initialization");
2543 qemu_create_cli_devices();
2544 qemu_machine_creation_done();
2547 Error
*local_err
= NULL
;
2548 if (!load_snapshot(loadvm
, NULL
, false, NULL
, &local_err
)) {
2549 error_report_err(local_err
);
2554 if (replay_mode
!= REPLAY_MODE_NONE
) {
2555 replay_vmstate_init();
2559 Error
*local_err
= NULL
;
2560 if (strcmp(incoming
, "defer") != 0) {
2561 qmp_migrate_incoming(incoming
, &local_err
);
2563 error_reportf_err(local_err
, "-incoming %s: ", incoming
);
2567 } else if (autostart
) {
2572 void qemu_init(int argc
, char **argv
, char **envp
)
2575 QemuOpts
*icount_opts
= NULL
, *accel_opts
= NULL
;
2576 QemuOptsList
*olist
;
2579 MachineClass
*machine_class
;
2580 bool userconfig
= true;
2581 FILE *vmstate_dump_file
= NULL
;
2583 qemu_add_opts(&qemu_drive_opts
);
2584 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2585 qemu_add_drive_opts(&qemu_common_drive_opts
);
2586 qemu_add_drive_opts(&qemu_drive_opts
);
2587 qemu_add_drive_opts(&bdrv_runtime_opts
);
2588 qemu_add_opts(&qemu_chardev_opts
);
2589 qemu_add_opts(&qemu_device_opts
);
2590 qemu_add_opts(&qemu_netdev_opts
);
2591 qemu_add_opts(&qemu_nic_opts
);
2592 qemu_add_opts(&qemu_net_opts
);
2593 qemu_add_opts(&qemu_rtc_opts
);
2594 qemu_add_opts(&qemu_global_opts
);
2595 qemu_add_opts(&qemu_mon_opts
);
2596 qemu_add_opts(&qemu_trace_opts
);
2597 qemu_plugin_add_opts();
2598 qemu_add_opts(&qemu_option_rom_opts
);
2599 qemu_add_opts(&qemu_machine_opts
);
2600 qemu_add_opts(&qemu_accel_opts
);
2601 qemu_add_opts(&qemu_mem_opts
);
2602 qemu_add_opts(&qemu_smp_opts
);
2603 qemu_add_opts(&qemu_boot_opts
);
2604 qemu_add_opts(&qemu_add_fd_opts
);
2605 qemu_add_opts(&qemu_object_opts
);
2606 qemu_add_opts(&qemu_tpmdev_opts
);
2607 qemu_add_opts(&qemu_overcommit_opts
);
2608 qemu_add_opts(&qemu_msg_opts
);
2609 qemu_add_opts(&qemu_name_opts
);
2610 qemu_add_opts(&qemu_numa_opts
);
2611 qemu_add_opts(&qemu_icount_opts
);
2612 qemu_add_opts(&qemu_semihosting_config_opts
);
2613 qemu_add_opts(&qemu_fw_cfg_opts
);
2614 qemu_add_opts(&qemu_action_opts
);
2615 module_call_init(MODULE_INIT_OPTS
);
2617 error_init(argv
[0]);
2618 qemu_init_exec_dir(argv
[0]);
2620 qemu_init_subsystems();
2622 /* first pass of option parsing */
2624 while (optind
< argc
) {
2625 if (argv
[optind
][0] != '-') {
2629 const QEMUOption
*popt
;
2631 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2632 switch (popt
->index
) {
2633 case QEMU_OPTION_nouserconfig
:
2641 if (qemu_read_default_config_file() < 0) {
2646 /* second pass of option parsing */
2651 if (argv
[optind
][0] != '-') {
2652 loc_set_cmdline(argv
, optind
, 1);
2653 drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2655 const QEMUOption
*popt
;
2657 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2658 if (!(popt
->arch_mask
& arch_type
)) {
2659 error_report("Option not supported for this target");
2662 switch(popt
->index
) {
2663 case QEMU_OPTION_cpu
:
2664 /* hw initialization will check this */
2665 cpu_option
= optarg
;
2667 case QEMU_OPTION_hda
:
2668 case QEMU_OPTION_hdb
:
2669 case QEMU_OPTION_hdc
:
2670 case QEMU_OPTION_hdd
:
2671 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2674 case QEMU_OPTION_blockdev
:
2677 BlockdevOptionsQueueEntry
*bdo
;
2679 v
= qobject_input_visitor_new_str(optarg
, "driver",
2682 bdo
= g_new(BlockdevOptionsQueueEntry
, 1);
2683 visit_type_BlockdevOptions(v
, NULL
, &bdo
->bdo
,
2686 loc_save(&bdo
->loc
);
2687 QSIMPLEQ_INSERT_TAIL(&bdo_queue
, bdo
, entry
);
2690 case QEMU_OPTION_drive
:
2691 if (drive_def(optarg
) == NULL
) {
2695 case QEMU_OPTION_set
:
2696 if (qemu_set_option(optarg
) != 0)
2699 case QEMU_OPTION_global
:
2700 if (qemu_global_option(optarg
) != 0)
2703 case QEMU_OPTION_mtdblock
:
2704 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2706 case QEMU_OPTION_sd
:
2707 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
2709 case QEMU_OPTION_pflash
:
2710 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2712 case QEMU_OPTION_snapshot
:
2714 Error
*blocker
= NULL
;
2716 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
,
2718 replay_add_blocker(blocker
);
2721 case QEMU_OPTION_numa
:
2722 opts
= qemu_opts_parse_noisily(qemu_find_opts("numa"),
2728 case QEMU_OPTION_display
:
2729 parse_display(optarg
);
2731 case QEMU_OPTION_nographic
:
2732 olist
= qemu_find_opts("machine");
2733 qemu_opts_parse_noisily(olist
, "graphics=off", false);
2735 dpy
.type
= DISPLAY_TYPE_NONE
;
2737 case QEMU_OPTION_curses
:
2738 #ifdef CONFIG_CURSES
2739 dpy
.type
= DISPLAY_TYPE_CURSES
;
2741 error_report("curses or iconv support is disabled");
2745 case QEMU_OPTION_portrait
:
2746 graphic_rotate
= 90;
2748 case QEMU_OPTION_rotate
:
2749 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2750 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2751 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2752 error_report("only 90, 180, 270 deg rotation is available");
2756 case QEMU_OPTION_kernel
:
2757 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg
, &error_abort
);
2759 case QEMU_OPTION_initrd
:
2760 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg
, &error_abort
);
2762 case QEMU_OPTION_append
:
2763 qemu_opts_set(qemu_find_opts("machine"), "append", optarg
, &error_abort
);
2765 case QEMU_OPTION_dtb
:
2766 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg
, &error_abort
);
2768 case QEMU_OPTION_cdrom
:
2769 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2771 case QEMU_OPTION_boot
:
2772 opts
= qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2778 case QEMU_OPTION_fda
:
2779 case QEMU_OPTION_fdb
:
2780 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2783 case QEMU_OPTION_no_fd_bootchk
:
2786 case QEMU_OPTION_netdev
:
2788 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2792 case QEMU_OPTION_nic
:
2794 if (net_client_parse(qemu_find_opts("nic"), optarg
) == -1) {
2798 case QEMU_OPTION_net
:
2800 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2804 #ifdef CONFIG_LIBISCSI
2805 case QEMU_OPTION_iscsi
:
2806 opts
= qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2813 case QEMU_OPTION_audio_help
:
2814 audio_legacy_help();
2817 case QEMU_OPTION_audiodev
:
2818 audio_parse_option(optarg
);
2820 case QEMU_OPTION_soundhw
:
2821 select_soundhw (optarg
);
2826 case QEMU_OPTION_version
:
2831 opts
= qemu_opts_parse_noisily(qemu_find_opts("memory"),
2838 case QEMU_OPTION_tpmdev
:
2839 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
2844 case QEMU_OPTION_mempath
:
2847 case QEMU_OPTION_mem_prealloc
:
2856 case QEMU_OPTION_DFILTER
:
2857 qemu_set_dfilter_ranges(optarg
, &error_fatal
);
2859 case QEMU_OPTION_seed
:
2860 qemu_guest_random_seed_main(optarg
, &error_fatal
);
2863 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
2865 case QEMU_OPTION_gdb
:
2866 add_device_config(DEV_GDB
, optarg
);
2869 if (is_help_option(optarg
)) {
2870 list_data_dirs
= true;
2872 qemu_add_data_dir(g_strdup(optarg
));
2875 case QEMU_OPTION_bios
:
2876 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg
, &error_abort
);
2878 case QEMU_OPTION_singlestep
:
2885 keyboard_layout
= optarg
;
2887 case QEMU_OPTION_vga
:
2896 w
= strtol(p
, (char **)&p
, 10);
2899 error_report("invalid resolution or depth");
2905 h
= strtol(p
, (char **)&p
, 10);
2910 depth
= strtol(p
, (char **)&p
, 10);
2911 if (depth
!= 1 && depth
!= 2 && depth
!= 4 &&
2912 depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2913 depth
!= 24 && depth
!= 32)
2915 } else if (*p
== '\0') {
2916 depth
= graphic_depth
;
2923 graphic_depth
= depth
;
2926 case QEMU_OPTION_echr
:
2929 term_escape_char
= strtol(optarg
, &r
, 0);
2931 printf("Bad argument to echr\n");
2934 case QEMU_OPTION_monitor
:
2935 default_monitor
= 0;
2936 if (strncmp(optarg
, "none", 4)) {
2937 monitor_parse(optarg
, "readline", false);
2940 case QEMU_OPTION_qmp
:
2941 monitor_parse(optarg
, "control", false);
2942 default_monitor
= 0;
2944 case QEMU_OPTION_qmp_pretty
:
2945 monitor_parse(optarg
, "control", true);
2946 default_monitor
= 0;
2948 case QEMU_OPTION_mon
:
2949 opts
= qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg
,
2954 default_monitor
= 0;
2956 case QEMU_OPTION_chardev
:
2957 opts
= qemu_opts_parse_noisily(qemu_find_opts("chardev"),
2963 case QEMU_OPTION_fsdev
:
2964 olist
= qemu_find_opts("fsdev");
2966 error_report("fsdev support is disabled");
2969 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
2974 case QEMU_OPTION_virtfs
: {
2977 const char *writeout
, *sock_fd
, *socket
, *path
, *security_model
,
2980 olist
= qemu_find_opts("virtfs");
2982 error_report("virtfs support is disabled");
2985 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
2990 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
2991 qemu_opt_get(opts
, "mount_tag") == NULL
) {
2992 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
2995 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2996 qemu_opts_id(opts
) ?:
2997 qemu_opt_get(opts
, "mount_tag"),
3000 error_report("duplicate or invalid fsdev id: %s",
3001 qemu_opt_get(opts
, "mount_tag"));
3005 writeout
= qemu_opt_get(opts
, "writeout");
3007 #ifdef CONFIG_SYNC_FILE_RANGE
3008 qemu_opt_set(fsdev
, "writeout", writeout
, &error_abort
);
3010 error_report("writeout=immediate not supported "
3011 "on this platform");
3015 qemu_opt_set(fsdev
, "fsdriver",
3016 qemu_opt_get(opts
, "fsdriver"), &error_abort
);
3017 path
= qemu_opt_get(opts
, "path");
3019 qemu_opt_set(fsdev
, "path", path
, &error_abort
);
3021 security_model
= qemu_opt_get(opts
, "security_model");
3022 if (security_model
) {
3023 qemu_opt_set(fsdev
, "security_model", security_model
,
3026 socket
= qemu_opt_get(opts
, "socket");
3028 qemu_opt_set(fsdev
, "socket", socket
, &error_abort
);
3030 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3032 qemu_opt_set(fsdev
, "sock_fd", sock_fd
, &error_abort
);
3035 qemu_opt_set_bool(fsdev
, "readonly",
3036 qemu_opt_get_bool(opts
, "readonly", 0),
3038 multidevs
= qemu_opt_get(opts
, "multidevs");
3040 qemu_opt_set(fsdev
, "multidevs", multidevs
, &error_abort
);
3042 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3044 qemu_opt_set(device
, "driver", "virtio-9p-pci", &error_abort
);
3045 qemu_opt_set(device
, "fsdev",
3046 qemu_opts_id(fsdev
), &error_abort
);
3047 qemu_opt_set(device
, "mount_tag",
3048 qemu_opt_get(opts
, "mount_tag"), &error_abort
);
3051 case QEMU_OPTION_serial
:
3052 add_device_config(DEV_SERIAL
, optarg
);
3054 if (strncmp(optarg
, "mon:", 4) == 0) {
3055 default_monitor
= 0;
3058 case QEMU_OPTION_watchdog
:
3060 error_report("only one watchdog option may be given");
3065 case QEMU_OPTION_action
:
3066 olist
= qemu_find_opts("action");
3067 if (!qemu_opts_parse_noisily(olist
, optarg
, false)) {
3071 case QEMU_OPTION_watchdog_action
:
3072 if (select_watchdog_action(optarg
) == -1) {
3073 error_report("unknown -watchdog-action parameter");
3077 case QEMU_OPTION_parallel
:
3078 add_device_config(DEV_PARALLEL
, optarg
);
3079 default_parallel
= 0;
3080 if (strncmp(optarg
, "mon:", 4) == 0) {
3081 default_monitor
= 0;
3084 case QEMU_OPTION_debugcon
:
3085 add_device_config(DEV_DEBUGCON
, optarg
);
3087 case QEMU_OPTION_loadvm
:
3090 case QEMU_OPTION_full_screen
:
3091 dpy
.has_full_screen
= true;
3092 dpy
.full_screen
= true;
3094 case QEMU_OPTION_alt_grab
:
3097 case QEMU_OPTION_ctrl_grab
:
3100 case QEMU_OPTION_no_quit
:
3101 dpy
.has_window_close
= true;
3102 dpy
.window_close
= false;
3104 case QEMU_OPTION_sdl
:
3106 dpy
.type
= DISPLAY_TYPE_SDL
;
3109 error_report("SDL support is disabled");
3112 case QEMU_OPTION_pidfile
:
3115 case QEMU_OPTION_win2k_hack
:
3116 win2k_install_hack
= 1;
3118 case QEMU_OPTION_acpitable
:
3119 opts
= qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3124 acpi_table_add(opts
, &error_fatal
);
3126 case QEMU_OPTION_smbios
:
3127 opts
= qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3132 smbios_entry_add(opts
, &error_fatal
);
3134 case QEMU_OPTION_fwcfg
:
3135 opts
= qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3141 case QEMU_OPTION_preconfig
:
3142 preconfig_requested
= true;
3144 case QEMU_OPTION_enable_kvm
:
3145 olist
= qemu_find_opts("machine");
3146 qemu_opts_parse_noisily(olist
, "accel=kvm", false);
3149 case QEMU_OPTION_machine
:
3150 olist
= qemu_find_opts("machine");
3151 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
3156 case QEMU_OPTION_accel
:
3157 accel_opts
= qemu_opts_parse_noisily(qemu_find_opts("accel"),
3159 optarg
= qemu_opt_get(accel_opts
, "accel");
3160 if (!optarg
|| is_help_option(optarg
)) {
3161 printf("Accelerators supported in QEMU binary:\n");
3162 GSList
*el
, *accel_list
= object_class_get_list(TYPE_ACCEL
,
3164 for (el
= accel_list
; el
; el
= el
->next
) {
3165 gchar
*typename
= g_strdup(object_class_get_name(
3166 OBJECT_CLASS(el
->data
)));
3167 /* omit qtest which is used for tests only */
3168 if (g_strcmp0(typename
, ACCEL_CLASS_NAME("qtest")) &&
3169 g_str_has_suffix(typename
, ACCEL_CLASS_SUFFIX
)) {
3170 gchar
**optname
= g_strsplit(typename
,
3171 ACCEL_CLASS_SUFFIX
, 0);
3172 printf("%s\n", optname
[0]);
3173 g_strfreev(optname
);
3177 g_slist_free(accel_list
);
3181 case QEMU_OPTION_usb
:
3182 olist
= qemu_find_opts("machine");
3183 qemu_opts_parse_noisily(olist
, "usb=on", false);
3185 case QEMU_OPTION_usbdevice
:
3186 error_report("'-usbdevice' is deprecated, please use "
3187 "'-device usb-...' instead");
3188 olist
= qemu_find_opts("machine");
3189 qemu_opts_parse_noisily(olist
, "usb=on", false);
3190 add_device_config(DEV_USB
, optarg
);
3192 case QEMU_OPTION_device
:
3193 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3198 case QEMU_OPTION_smp
:
3199 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3204 case QEMU_OPTION_vnc
:
3207 case QEMU_OPTION_no_acpi
:
3208 olist
= qemu_find_opts("machine");
3209 qemu_opts_parse_noisily(olist
, "acpi=off", false);
3211 case QEMU_OPTION_no_hpet
:
3212 olist
= qemu_find_opts("machine");
3213 qemu_opts_parse_noisily(olist
, "hpet=off", false);
3215 case QEMU_OPTION_no_reboot
:
3216 olist
= qemu_find_opts("action");
3217 qemu_opts_parse_noisily(olist
, "reboot=shutdown", false);
3219 case QEMU_OPTION_no_shutdown
:
3220 olist
= qemu_find_opts("action");
3221 qemu_opts_parse_noisily(olist
, "shutdown=pause", false);
3223 case QEMU_OPTION_uuid
:
3224 if (qemu_uuid_parse(optarg
, &qemu_uuid
) < 0) {
3225 error_report("failed to parse UUID string: wrong format");
3228 qemu_uuid_set
= true;
3230 case QEMU_OPTION_option_rom
:
3231 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3232 error_report("too many option ROMs");
3235 opts
= qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3240 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3241 option_rom
[nb_option_roms
].bootindex
=
3242 qemu_opt_get_number(opts
, "bootindex", -1);
3243 if (!option_rom
[nb_option_roms
].name
) {
3244 error_report("Option ROM file is not specified");
3249 case QEMU_OPTION_semihosting
:
3250 qemu_semihosting_enable();
3252 case QEMU_OPTION_semihosting_config
:
3253 if (qemu_semihosting_config_options(optarg
) != 0) {
3257 case QEMU_OPTION_name
:
3258 opts
= qemu_opts_parse_noisily(qemu_find_opts("name"),
3263 /* Capture guest name if -msg guest-name is used later */
3264 error_guest_name
= qemu_opt_get(opts
, "guest");
3266 case QEMU_OPTION_prom_env
:
3267 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3268 error_report("too many prom variables");
3271 prom_envs
[nb_prom_envs
] = optarg
;
3274 case QEMU_OPTION_old_param
:
3277 case QEMU_OPTION_rtc
:
3278 opts
= qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg
,
3284 case QEMU_OPTION_icount
:
3285 icount_opts
= qemu_opts_parse_noisily(qemu_find_opts("icount"),
3291 case QEMU_OPTION_incoming
:
3293 runstate_set(RUN_STATE_INMIGRATE
);
3297 case QEMU_OPTION_only_migratable
:
3298 only_migratable
= 1;
3300 case QEMU_OPTION_nodefaults
:
3303 case QEMU_OPTION_xen_domid
:
3304 if (!(xen_available())) {
3305 error_report("Option not supported for this target");
3308 xen_domid
= atoi(optarg
);
3310 case QEMU_OPTION_xen_attach
:
3311 if (!(xen_available())) {
3312 error_report("Option not supported for this target");
3315 xen_mode
= XEN_ATTACH
;
3317 case QEMU_OPTION_xen_domid_restrict
:
3318 if (!(xen_available())) {
3319 error_report("Option not supported for this target");
3322 xen_domid_restrict
= true;
3324 case QEMU_OPTION_trace
:
3325 trace_opt_parse(optarg
);
3327 case QEMU_OPTION_plugin
:
3328 qemu_plugin_opt_parse(optarg
, &plugin_list
);
3330 case QEMU_OPTION_readconfig
:
3332 int ret
= qemu_read_config_file(optarg
);
3334 error_report("read config %s: %s", optarg
,
3340 case QEMU_OPTION_spice
:
3341 olist
= qemu_find_opts_err("spice", NULL
);
3343 ui_module_load_one("spice-core");
3344 olist
= qemu_find_opts("spice");
3347 error_report("spice support is disabled");
3350 opts
= qemu_opts_parse_noisily(olist
, optarg
, false);
3356 case QEMU_OPTION_writeconfig
:
3359 if (strcmp(optarg
, "-") == 0) {
3362 fp
= fopen(optarg
, "w");
3364 error_report("open %s: %s", optarg
,
3369 qemu_config_write(fp
);
3375 case QEMU_OPTION_qtest
:
3376 qtest_chrdev
= optarg
;
3378 case QEMU_OPTION_qtest_log
:
3381 case QEMU_OPTION_sandbox
:
3382 olist
= qemu_find_opts("sandbox");
3384 #ifndef CONFIG_SECCOMP
3385 error_report("-sandbox support is not enabled "
3386 "in this QEMU binary");
3391 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
3396 case QEMU_OPTION_add_fd
:
3398 opts
= qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3404 error_report("File descriptor passing is disabled on this "
3409 case QEMU_OPTION_object
:
3410 opts
= qemu_opts_parse_noisily(qemu_find_opts("object"),
3416 case QEMU_OPTION_overcommit
:
3417 opts
= qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3422 enable_mlock
= qemu_opt_get_bool(opts
, "mem-lock", false);
3423 enable_cpu_pm
= qemu_opt_get_bool(opts
, "cpu-pm", false);
3425 case QEMU_OPTION_msg
:
3426 opts
= qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg
,
3431 configure_msg(opts
);
3433 case QEMU_OPTION_dump_vmstate
:
3434 if (vmstate_dump_file
) {
3435 error_report("only one '-dump-vmstate' "
3436 "option may be given");
3439 vmstate_dump_file
= fopen(optarg
, "w");
3440 if (vmstate_dump_file
== NULL
) {
3441 error_report("open %s: %s", optarg
, strerror(errno
));
3445 case QEMU_OPTION_enable_sync_profile
:
3448 case QEMU_OPTION_nouserconfig
:
3449 /* Nothing to be parsed here. Especially, do not error out below. */
3452 if (os_parse_cmd_args(popt
->index
, optarg
)) {
3453 error_report("Option not supported in this build");
3460 * Clear error location left behind by the loop.
3461 * Best done right after the loop. Do not insert code here!
3465 qemu_validate_options();
3466 qemu_process_sugar_options();
3469 * These options affect everything else and should be processed
3470 * before daemonizing.
3472 qemu_process_early_options();
3474 qemu_process_help_options();
3475 qemu_maybe_daemonize(pid_file
);
3477 qemu_init_main_loop(&error_fatal
);
3480 user_register_global_props();
3481 replay_configure(icount_opts
);
3483 configure_rtc(qemu_find_opts_singleton("rtc"));
3485 qemu_create_machine(select_machine());
3489 qemu_disable_default_devices();
3490 qemu_create_default_devices();
3491 qemu_create_early_backends();
3493 qemu_apply_machine_options();
3494 phase_advance(PHASE_MACHINE_CREATED
);
3497 * Note: uses machine properties such as kernel-irqchip, must run
3498 * after machine_set_property().
3500 configure_accelerators(argv
[0]);
3501 phase_advance(PHASE_ACCEL_CREATED
);
3504 * Beware, QOM objects created before this point miss global and
3505 * compat properties.
3507 * Global properties get set up by qdev_prop_register_global(),
3508 * called from user_register_global_props(), and certain option
3509 * desugaring. Also in CPU feature desugaring (buried in
3510 * parse_cpu_option()), which happens below this point, but may
3511 * only target the CPU type, which can only be created after
3512 * parse_cpu_option() returned the type.
3514 * Machine compat properties: object_set_machine_compat_props().
3515 * Accelerator compat props: object_set_accelerator_compat_props(),
3516 * called from do_configure_accelerator().
3519 machine_class
= MACHINE_GET_CLASS(current_machine
);
3520 if (!qtest_enabled() && machine_class
->deprecation_reason
) {
3521 error_report("Machine type '%s' is deprecated: %s",
3522 machine_class
->name
, machine_class
->deprecation_reason
);
3526 * Note: creates a QOM object, must run only after global and
3527 * compat properties have been set up.
3529 migration_object_init();
3531 qemu_create_late_backends();
3533 /* parse features once if machine provides default cpu_type */
3534 current_machine
->cpu_type
= machine_class
->default_cpu_type
;
3536 current_machine
->cpu_type
= parse_cpu_option(cpu_option
);
3538 /* NB: for machine none cpu_type could STILL be NULL here! */
3539 accel_init_interfaces(ACCEL_GET_CLASS(current_machine
->accelerator
));
3541 qemu_resolve_machine_memdev();
3542 parse_numa_opts(current_machine
);
3544 if (vmstate_dump_file
) {
3546 dump_vmstate_json_to_file(vmstate_dump_file
);
3550 if (!preconfig_requested
) {
3551 qmp_x_exit_preconfig(&error_fatal
);
3553 qemu_init_displays();
3554 accel_setup_post(current_machine
);