Merge tag 'qemu-macppc-20230206' of https://github.com/mcayland/qemu into staging
[qemu/rayw.git] / softmmu / vl.c
blob9177d95d4ecac4b1bc49f871939df18848a60b99
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu/help-texts.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "exec/page-vary.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/qstring.h"
36 #include "qapi/qmp/qjson.h"
37 #include "qemu-version.h"
38 #include "qemu/cutils.h"
39 #include "qemu/help_option.h"
40 #include "qemu/hw-version.h"
41 #include "qemu/uuid.h"
42 #include "sysemu/reset.h"
43 #include "sysemu/runstate.h"
44 #include "sysemu/runstate-action.h"
45 #include "sysemu/seccomp.h"
46 #include "sysemu/tcg.h"
47 #include "sysemu/xen.h"
49 #include "qemu/error-report.h"
50 #include "qemu/sockets.h"
51 #include "qemu/accel.h"
52 #include "hw/usb.h"
53 #include "hw/isa/isa.h"
54 #include "hw/scsi/scsi.h"
55 #include "hw/display/vga.h"
56 #include "hw/firmware/smbios.h"
57 #include "hw/acpi/acpi.h"
58 #include "hw/xen/xen.h"
59 #include "hw/loader.h"
60 #include "monitor/qdev.h"
61 #include "net/net.h"
62 #include "net/slirp.h"
63 #include "monitor/monitor.h"
64 #include "ui/console.h"
65 #include "ui/input.h"
66 #include "sysemu/sysemu.h"
67 #include "sysemu/numa.h"
68 #include "sysemu/hostmem.h"
69 #include "exec/gdbstub.h"
70 #include "qemu/timer.h"
71 #include "chardev/char.h"
72 #include "qemu/bitmap.h"
73 #include "qemu/log.h"
74 #include "sysemu/blockdev.h"
75 #include "hw/block/block.h"
76 #include "hw/i386/x86.h"
77 #include "hw/i386/pc.h"
78 #include "migration/misc.h"
79 #include "migration/snapshot.h"
80 #include "sysemu/tpm.h"
81 #include "sysemu/dma.h"
82 #include "hw/audio/soundhw.h"
83 #include "audio/audio.h"
84 #include "sysemu/cpus.h"
85 #include "sysemu/cpu-timers.h"
86 #include "migration/colo.h"
87 #include "migration/postcopy-ram.h"
88 #include "sysemu/kvm.h"
89 #include "sysemu/hax.h"
90 #include "qapi/qobject-input-visitor.h"
91 #include "qemu/option.h"
92 #include "qemu/config-file.h"
93 #include "qemu/qemu-options.h"
94 #include "qemu/main-loop.h"
95 #ifdef CONFIG_VIRTFS
96 #include "fsdev/qemu-fsdev.h"
97 #endif
98 #include "sysemu/qtest.h"
99 #ifdef CONFIG_TCG
100 #include "accel/tcg/perf.h"
101 #endif
103 #include "disas/disas.h"
105 #include "trace.h"
106 #include "trace/control.h"
107 #include "qemu/plugin.h"
108 #include "qemu/queue.h"
109 #include "sysemu/arch_init.h"
110 #include "exec/confidential-guest-support.h"
112 #include "ui/qemu-spice.h"
113 #include "qapi/string-input-visitor.h"
114 #include "qapi/opts-visitor.h"
115 #include "qapi/clone-visitor.h"
116 #include "qom/object_interfaces.h"
117 #include "semihosting/semihost.h"
118 #include "crypto/init.h"
119 #include "sysemu/replay.h"
120 #include "qapi/qapi-events-run-state.h"
121 #include "qapi/qapi-types-audio.h"
122 #include "qapi/qapi-visit-audio.h"
123 #include "qapi/qapi-visit-block-core.h"
124 #include "qapi/qapi-visit-compat.h"
125 #include "qapi/qapi-visit-machine.h"
126 #include "qapi/qapi-visit-ui.h"
127 #include "qapi/qapi-commands-block-core.h"
128 #include "qapi/qapi-commands-migration.h"
129 #include "qapi/qapi-commands-misc.h"
130 #include "qapi/qapi-visit-qom.h"
131 #include "qapi/qapi-commands-ui.h"
132 #include "qapi/qmp/qdict.h"
133 #include "block/qdict.h"
134 #include "qapi/qmp/qerror.h"
135 #include "sysemu/iothread.h"
136 #include "qemu/guest-random.h"
137 #include "qemu/keyval.h"
139 #include "config-host.h"
141 #define MAX_VIRTIO_CONSOLES 1
143 typedef struct BlockdevOptionsQueueEntry {
144 BlockdevOptions *bdo;
145 Location loc;
146 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
147 } BlockdevOptionsQueueEntry;
149 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
151 typedef struct ObjectOption {
152 ObjectOptions *opts;
153 QTAILQ_ENTRY(ObjectOption) next;
154 } ObjectOption;
156 typedef struct DeviceOption {
157 QDict *opts;
158 Location loc;
159 QTAILQ_ENTRY(DeviceOption) next;
160 } DeviceOption;
162 static const char *cpu_option;
163 static const char *mem_path;
164 static const char *incoming;
165 static const char *loadvm;
166 static const char *accelerators;
167 static bool have_custom_ram_size;
168 static const char *ram_memdev_id;
169 static QDict *machine_opts_dict;
170 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
171 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
172 static int display_remote;
173 static int snapshot;
174 static bool preconfig_requested;
175 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
176 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
177 static bool nographic = false;
178 static int mem_prealloc; /* force preallocation of physical target memory */
179 static const char *vga_model = NULL;
180 static DisplayOptions dpy;
181 static int num_serial_hds;
182 static Chardev **serial_hds;
183 static const char *log_mask;
184 static const char *log_file;
185 static bool list_data_dirs;
186 static const char *qtest_chrdev;
187 static const char *qtest_log;
189 static int has_defaults = 1;
190 static int default_serial = 1;
191 static int default_parallel = 1;
192 static int default_monitor = 1;
193 static int default_floppy = 1;
194 static int default_cdrom = 1;
195 static int default_sdcard = 1;
196 static int default_vga = 1;
197 static int default_net = 1;
199 static struct {
200 const char *driver;
201 int *flag;
202 } default_list[] = {
203 { .driver = "isa-serial", .flag = &default_serial },
204 { .driver = "isa-parallel", .flag = &default_parallel },
205 { .driver = "isa-fdc", .flag = &default_floppy },
206 { .driver = "floppy", .flag = &default_floppy },
207 { .driver = "ide-cd", .flag = &default_cdrom },
208 { .driver = "ide-hd", .flag = &default_cdrom },
209 { .driver = "scsi-cd", .flag = &default_cdrom },
210 { .driver = "scsi-hd", .flag = &default_cdrom },
211 { .driver = "VGA", .flag = &default_vga },
212 { .driver = "isa-vga", .flag = &default_vga },
213 { .driver = "cirrus-vga", .flag = &default_vga },
214 { .driver = "isa-cirrus-vga", .flag = &default_vga },
215 { .driver = "vmware-svga", .flag = &default_vga },
216 { .driver = "qxl-vga", .flag = &default_vga },
217 { .driver = "virtio-vga", .flag = &default_vga },
218 { .driver = "ati-vga", .flag = &default_vga },
219 { .driver = "vhost-user-vga", .flag = &default_vga },
220 { .driver = "virtio-vga-gl", .flag = &default_vga },
223 static QemuOptsList qemu_rtc_opts = {
224 .name = "rtc",
225 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
226 .merge_lists = true,
227 .desc = {
229 .name = "base",
230 .type = QEMU_OPT_STRING,
232 .name = "clock",
233 .type = QEMU_OPT_STRING,
235 .name = "driftfix",
236 .type = QEMU_OPT_STRING,
238 { /* end of list */ }
242 static QemuOptsList qemu_option_rom_opts = {
243 .name = "option-rom",
244 .implied_opt_name = "romfile",
245 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
246 .desc = {
248 .name = "bootindex",
249 .type = QEMU_OPT_NUMBER,
250 }, {
251 .name = "romfile",
252 .type = QEMU_OPT_STRING,
254 { /* end of list */ }
258 static QemuOptsList qemu_accel_opts = {
259 .name = "accel",
260 .implied_opt_name = "accel",
261 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
262 .desc = {
264 * no elements => accept any
265 * sanity checking will happen later
266 * when setting accelerator properties
272 static QemuOptsList qemu_boot_opts = {
273 .name = "boot-opts",
274 .implied_opt_name = "order",
275 .merge_lists = true,
276 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
277 .desc = {
279 .name = "order",
280 .type = QEMU_OPT_STRING,
281 }, {
282 .name = "once",
283 .type = QEMU_OPT_STRING,
284 }, {
285 .name = "menu",
286 .type = QEMU_OPT_BOOL,
287 }, {
288 .name = "splash",
289 .type = QEMU_OPT_STRING,
290 }, {
291 .name = "splash-time",
292 .type = QEMU_OPT_NUMBER,
293 }, {
294 .name = "reboot-timeout",
295 .type = QEMU_OPT_NUMBER,
296 }, {
297 .name = "strict",
298 .type = QEMU_OPT_BOOL,
300 { /*End of list */ }
304 static QemuOptsList qemu_add_fd_opts = {
305 .name = "add-fd",
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
307 .desc = {
309 .name = "fd",
310 .type = QEMU_OPT_NUMBER,
311 .help = "file descriptor of which a duplicate is added to fd set",
313 .name = "set",
314 .type = QEMU_OPT_NUMBER,
315 .help = "ID of the fd set to add fd to",
317 .name = "opaque",
318 .type = QEMU_OPT_STRING,
319 .help = "free-form string used to describe fd",
321 { /* end of list */ }
325 static QemuOptsList qemu_object_opts = {
326 .name = "object",
327 .implied_opt_name = "qom-type",
328 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
329 .desc = {
334 static QemuOptsList qemu_tpmdev_opts = {
335 .name = "tpmdev",
336 .implied_opt_name = "type",
337 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
338 .desc = {
339 /* options are defined in the TPM backends */
340 { /* end of list */ }
344 static QemuOptsList qemu_overcommit_opts = {
345 .name = "overcommit",
346 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
347 .desc = {
349 .name = "mem-lock",
350 .type = QEMU_OPT_BOOL,
353 .name = "cpu-pm",
354 .type = QEMU_OPT_BOOL,
356 { /* end of list */ }
360 static QemuOptsList qemu_msg_opts = {
361 .name = "msg",
362 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
363 .desc = {
365 .name = "timestamp",
366 .type = QEMU_OPT_BOOL,
369 .name = "guest-name",
370 .type = QEMU_OPT_BOOL,
371 .help = "Prepends guest name for error messages but only if "
372 "-name guest is set otherwise option is ignored\n",
374 { /* end of list */ }
378 static QemuOptsList qemu_name_opts = {
379 .name = "name",
380 .implied_opt_name = "guest",
381 .merge_lists = true,
382 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
383 .desc = {
385 .name = "guest",
386 .type = QEMU_OPT_STRING,
387 .help = "Sets the name of the guest.\n"
388 "This name will be displayed in the SDL window caption.\n"
389 "The name will also be used for the VNC server",
390 }, {
391 .name = "process",
392 .type = QEMU_OPT_STRING,
393 .help = "Sets the name of the QEMU process, as shown in top etc",
394 }, {
395 .name = "debug-threads",
396 .type = QEMU_OPT_BOOL,
397 .help = "When enabled, name the individual threads; defaults off.\n"
398 "NOTE: The thread names are for debugging and not a\n"
399 "stable API.",
401 { /* End of list */ }
405 static QemuOptsList qemu_mem_opts = {
406 .name = "memory",
407 .implied_opt_name = "size",
408 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
409 .merge_lists = true,
410 .desc = {
412 .name = "size",
413 .type = QEMU_OPT_SIZE,
416 .name = "slots",
417 .type = QEMU_OPT_NUMBER,
420 .name = "maxmem",
421 .type = QEMU_OPT_SIZE,
423 { /* end of list */ }
427 static QemuOptsList qemu_icount_opts = {
428 .name = "icount",
429 .implied_opt_name = "shift",
430 .merge_lists = true,
431 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
432 .desc = {
434 .name = "shift",
435 .type = QEMU_OPT_STRING,
436 }, {
437 .name = "align",
438 .type = QEMU_OPT_BOOL,
439 }, {
440 .name = "sleep",
441 .type = QEMU_OPT_BOOL,
442 }, {
443 .name = "rr",
444 .type = QEMU_OPT_STRING,
445 }, {
446 .name = "rrfile",
447 .type = QEMU_OPT_STRING,
448 }, {
449 .name = "rrsnapshot",
450 .type = QEMU_OPT_STRING,
452 { /* end of list */ }
456 static QemuOptsList qemu_fw_cfg_opts = {
457 .name = "fw_cfg",
458 .implied_opt_name = "name",
459 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
460 .desc = {
462 .name = "name",
463 .type = QEMU_OPT_STRING,
464 .help = "Sets the fw_cfg name of the blob to be inserted",
465 }, {
466 .name = "file",
467 .type = QEMU_OPT_STRING,
468 .help = "Sets the name of the file from which "
469 "the fw_cfg blob will be loaded",
470 }, {
471 .name = "string",
472 .type = QEMU_OPT_STRING,
473 .help = "Sets content of the blob to be inserted from a string",
474 }, {
475 .name = "gen_id",
476 .type = QEMU_OPT_STRING,
477 .help = "Sets id of the object generating the fw_cfg blob "
478 "to be inserted",
480 { /* end of list */ }
484 static QemuOptsList qemu_action_opts = {
485 .name = "action",
486 .merge_lists = true,
487 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
488 .desc = {
490 .name = "shutdown",
491 .type = QEMU_OPT_STRING,
493 .name = "reboot",
494 .type = QEMU_OPT_STRING,
496 .name = "panic",
497 .type = QEMU_OPT_STRING,
499 .name = "watchdog",
500 .type = QEMU_OPT_STRING,
502 { /* end of list */ }
506 const char *qemu_get_vm_name(void)
508 return qemu_name;
511 static void default_driver_disable(const char *driver)
513 int i;
515 if (!driver) {
516 return;
519 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
520 if (strcmp(default_list[i].driver, driver) != 0)
521 continue;
522 *(default_list[i].flag) = 0;
526 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
528 const char *driver = qemu_opt_get(opts, "driver");
530 default_driver_disable(driver);
531 return 0;
534 static void default_driver_check_json(void)
536 DeviceOption *opt;
538 QTAILQ_FOREACH(opt, &device_opts, next) {
539 const char *driver = qdict_get_try_str(opt->opts, "driver");
540 default_driver_disable(driver);
544 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
546 const char *proc_name;
548 if (qemu_opt_get(opts, "debug-threads")) {
549 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
551 qemu_name = qemu_opt_get(opts, "guest");
553 proc_name = qemu_opt_get(opts, "process");
554 if (proc_name) {
555 os_set_proc_name(proc_name);
558 return 0;
561 bool defaults_enabled(void)
563 return has_defaults;
566 #ifndef _WIN32
567 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
569 int fd, dupfd, flags;
570 int64_t fdset_id;
571 const char *fd_opaque = NULL;
572 AddfdInfo *fdinfo;
574 fd = qemu_opt_get_number(opts, "fd", -1);
575 fdset_id = qemu_opt_get_number(opts, "set", -1);
576 fd_opaque = qemu_opt_get(opts, "opaque");
578 if (fd < 0) {
579 error_setg(errp, "fd option is required and must be non-negative");
580 return -1;
583 if (fd <= STDERR_FILENO) {
584 error_setg(errp, "fd cannot be a standard I/O stream");
585 return -1;
589 * All fds inherited across exec() necessarily have FD_CLOEXEC
590 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
592 flags = fcntl(fd, F_GETFD);
593 if (flags == -1 || (flags & FD_CLOEXEC)) {
594 error_setg(errp, "fd is not valid or already in use");
595 return -1;
598 if (fdset_id < 0) {
599 error_setg(errp, "set option is required and must be non-negative");
600 return -1;
603 #ifdef F_DUPFD_CLOEXEC
604 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
605 #else
606 dupfd = dup(fd);
607 if (dupfd != -1) {
608 qemu_set_cloexec(dupfd);
610 #endif
611 if (dupfd == -1) {
612 error_setg(errp, "error duplicating fd: %s", strerror(errno));
613 return -1;
616 /* add the duplicate fd, and optionally the opaque string, to the fd set */
617 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque,
618 &error_abort);
619 g_free(fdinfo);
621 return 0;
624 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
626 int fd;
628 fd = qemu_opt_get_number(opts, "fd", -1);
629 close(fd);
631 return 0;
633 #endif
635 /***********************************************************/
636 /* QEMU Block devices */
638 #define HD_OPTS "media=disk"
639 #define CDROM_OPTS "media=cdrom"
640 #define FD_OPTS ""
641 #define PFLASH_OPTS ""
642 #define MTD_OPTS ""
643 #define SD_OPTS ""
645 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
647 BlockInterfaceType *block_default_type = opaque;
649 return drive_new(opts, *block_default_type, errp) == NULL;
652 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
654 if (qemu_opt_get(opts, "snapshot") == NULL) {
655 qemu_opt_set(opts, "snapshot", "on", &error_abort);
657 return 0;
660 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
661 int index, const char *optstr)
663 QemuOpts *opts;
664 DriveInfo *dinfo;
666 if (!enable || drive_get_by_index(type, index)) {
667 return;
670 opts = drive_add(type, index, NULL, optstr);
671 if (snapshot) {
672 drive_enable_snapshot(NULL, opts, NULL);
675 dinfo = drive_new(opts, type, &error_abort);
676 dinfo->is_default = true;
680 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
681 MachineClass *machine_class, int snapshot)
684 * If the currently selected machine wishes to override the
685 * units-per-bus property of its default HBA interface type, do so
686 * now.
688 if (machine_class->units_per_default_bus) {
689 override_max_devs(machine_class->block_default_type,
690 machine_class->units_per_default_bus);
693 /* open the virtual block devices */
694 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
695 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
697 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
698 loc_push_restore(&bdo->loc);
699 qmp_blockdev_add(bdo->bdo, &error_fatal);
700 loc_pop(&bdo->loc);
701 qapi_free_BlockdevOptions(bdo->bdo);
702 g_free(bdo);
704 if (snapshot) {
705 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
706 NULL, NULL);
708 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
709 &machine_class->block_default_type, &error_fatal)) {
710 /* We printed help */
711 exit(0);
714 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
715 CDROM_OPTS);
716 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
717 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
721 static QemuOptsList qemu_smp_opts = {
722 .name = "smp-opts",
723 .implied_opt_name = "cpus",
724 .merge_lists = true,
725 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
726 .desc = {
728 .name = "cpus",
729 .type = QEMU_OPT_NUMBER,
730 }, {
731 .name = "sockets",
732 .type = QEMU_OPT_NUMBER,
733 }, {
734 .name = "dies",
735 .type = QEMU_OPT_NUMBER,
736 }, {
737 .name = "clusters",
738 .type = QEMU_OPT_NUMBER,
739 }, {
740 .name = "cores",
741 .type = QEMU_OPT_NUMBER,
742 }, {
743 .name = "threads",
744 .type = QEMU_OPT_NUMBER,
745 }, {
746 .name = "maxcpus",
747 .type = QEMU_OPT_NUMBER,
749 { /*End of list */ }
753 static void realtime_init(void)
755 if (enable_mlock) {
756 if (os_mlock() < 0) {
757 error_report("locking memory failed");
758 exit(1);
764 static void configure_msg(QemuOpts *opts)
766 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
767 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
771 /***********************************************************/
772 /* USB devices */
774 static int usb_device_add(const char *devname)
776 USBDevice *dev = NULL;
778 if (!machine_usb(current_machine)) {
779 return -1;
782 dev = usbdevice_create(devname);
783 if (!dev)
784 return -1;
786 return 0;
789 static int usb_parse(const char *cmdline)
791 int r;
792 r = usb_device_add(cmdline);
793 if (r < 0) {
794 error_report("could not add USB device '%s'", cmdline);
796 return r;
799 /***********************************************************/
800 /* machine registration */
802 static MachineClass *find_machine(const char *name, GSList *machines)
804 GSList *el;
806 for (el = machines; el; el = el->next) {
807 MachineClass *mc = el->data;
809 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
810 return mc;
814 return NULL;
817 static MachineClass *find_default_machine(GSList *machines)
819 GSList *el;
820 MachineClass *default_machineclass = NULL;
822 for (el = machines; el; el = el->next) {
823 MachineClass *mc = el->data;
825 if (mc->is_default) {
826 assert(default_machineclass == NULL && "Multiple default machines");
827 default_machineclass = mc;
831 return default_machineclass;
834 static void version(void)
836 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
837 QEMU_COPYRIGHT "\n");
840 static void help(int exitcode)
842 version();
843 printf("usage: %s [options] [disk_image]\n\n"
844 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
845 g_get_prgname());
847 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
848 if ((arch_mask) & arch_type) \
849 fputs(opt_help, stdout);
851 #define ARCHHEADING(text, arch_mask) \
852 if ((arch_mask) & arch_type) \
853 puts(stringify(text));
855 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
857 #include "qemu-options.def"
859 printf("\nDuring emulation, the following keys are useful:\n"
860 "ctrl-alt-f toggle full screen\n"
861 "ctrl-alt-n switch to virtual console 'n'\n"
862 "ctrl-alt toggle mouse and keyboard grab\n"
863 "\n"
864 "When using -nographic, press 'ctrl-a h' to get some help.\n"
865 "\n"
866 QEMU_HELP_BOTTOM "\n");
868 exit(exitcode);
871 #define HAS_ARG 0x0001
873 typedef struct QEMUOption {
874 const char *name;
875 int flags;
876 int index;
877 uint32_t arch_mask;
878 } QEMUOption;
880 static const QEMUOption qemu_options[] = {
881 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
883 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
884 { option, opt_arg, opt_enum, arch_mask },
885 #define DEFHEADING(text)
886 #define ARCHHEADING(text, arch_mask)
888 #include "qemu-options.def"
889 { NULL },
892 typedef struct VGAInterfaceInfo {
893 const char *opt_name; /* option name */
894 const char *name; /* human-readable name */
895 /* Class names indicating that support is available.
896 * If no class is specified, the interface is always available */
897 const char *class_names[2];
898 } VGAInterfaceInfo;
900 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
901 [VGA_NONE] = {
902 .opt_name = "none",
903 .name = "no graphic card",
905 [VGA_STD] = {
906 .opt_name = "std",
907 .name = "standard VGA",
908 .class_names = { "VGA", "isa-vga" },
910 [VGA_CIRRUS] = {
911 .opt_name = "cirrus",
912 .name = "Cirrus VGA",
913 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
915 [VGA_VMWARE] = {
916 .opt_name = "vmware",
917 .name = "VMWare SVGA",
918 .class_names = { "vmware-svga" },
920 [VGA_VIRTIO] = {
921 .opt_name = "virtio",
922 .name = "Virtio VGA",
923 .class_names = { "virtio-vga" },
925 [VGA_QXL] = {
926 .opt_name = "qxl",
927 .name = "QXL VGA",
928 .class_names = { "qxl-vga" },
930 [VGA_TCX] = {
931 .opt_name = "tcx",
932 .name = "TCX framebuffer",
933 .class_names = { "sun-tcx" },
935 [VGA_CG3] = {
936 .opt_name = "cg3",
937 .name = "CG3 framebuffer",
938 .class_names = { "cgthree" },
940 #ifdef CONFIG_XEN_BACKEND
941 [VGA_XENFB] = {
942 .opt_name = "xenfb",
943 .name = "Xen paravirtualized framebuffer",
945 #endif
948 static bool vga_interface_available(VGAInterfaceType t)
950 const VGAInterfaceInfo *ti = &vga_interfaces[t];
952 assert(t < VGA_TYPE_MAX);
953 return !ti->class_names[0] ||
954 module_object_class_by_name(ti->class_names[0]) ||
955 module_object_class_by_name(ti->class_names[1]);
958 static const char *
959 get_default_vga_model(const MachineClass *machine_class)
961 if (machine_class->default_display) {
962 return machine_class->default_display;
963 } else if (vga_interface_available(VGA_CIRRUS)) {
964 return "cirrus";
965 } else if (vga_interface_available(VGA_STD)) {
966 return "std";
969 return NULL;
972 static void select_vgahw(const MachineClass *machine_class, const char *p)
974 const char *opts;
975 int t;
977 if (g_str_equal(p, "help")) {
978 const char *def = get_default_vga_model(machine_class);
980 for (t = 0; t < VGA_TYPE_MAX; t++) {
981 const VGAInterfaceInfo *ti = &vga_interfaces[t];
983 if (vga_interface_available(t) && ti->opt_name) {
984 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
985 (def && g_str_equal(ti->opt_name, def)) ?
986 " (default)" : "");
989 exit(0);
992 assert(vga_interface_type == VGA_NONE);
993 for (t = 0; t < VGA_TYPE_MAX; t++) {
994 const VGAInterfaceInfo *ti = &vga_interfaces[t];
995 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
996 if (!vga_interface_available(t)) {
997 error_report("%s not available", ti->name);
998 exit(1);
1000 vga_interface_type = t;
1001 break;
1004 if (t == VGA_TYPE_MAX) {
1005 invalid_vga:
1006 error_report("unknown vga type: %s", p);
1007 exit(1);
1009 while (*opts) {
1010 const char *nextopt;
1012 if (strstart(opts, ",retrace=", &nextopt)) {
1013 opts = nextopt;
1014 if (strstart(opts, "dumb", &nextopt))
1015 vga_retrace_method = VGA_RETRACE_DUMB;
1016 else if (strstart(opts, "precise", &nextopt))
1017 vga_retrace_method = VGA_RETRACE_PRECISE;
1018 else goto invalid_vga;
1019 } else goto invalid_vga;
1020 opts = nextopt;
1024 static void parse_display_qapi(const char *optarg)
1026 DisplayOptions *opts;
1027 Visitor *v;
1029 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1031 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1032 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1034 qapi_free_DisplayOptions(opts);
1035 visit_free(v);
1038 DisplayOptions *qmp_query_display_options(Error **errp)
1040 return QAPI_CLONE(DisplayOptions, &dpy);
1043 static void parse_display(const char *p)
1045 const char *opts;
1047 if (is_help_option(p)) {
1048 qemu_display_help();
1049 exit(0);
1052 if (strstart(p, "vnc", &opts)) {
1054 * vnc isn't a (local) DisplayType but a protocol for remote
1055 * display access.
1057 if (*opts == '=') {
1058 vnc_parse(opts + 1);
1059 } else {
1060 error_report("VNC requires a display argument vnc=<display>");
1061 exit(1);
1063 } else {
1064 parse_display_qapi(p);
1068 static inline bool nonempty_str(const char *str)
1070 return str && *str;
1073 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1075 gchar *buf;
1076 size_t size;
1077 const char *name, *file, *str, *gen_id;
1078 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1080 if (fw_cfg == NULL) {
1081 error_setg(errp, "fw_cfg device not available");
1082 return -1;
1084 name = qemu_opt_get(opts, "name");
1085 file = qemu_opt_get(opts, "file");
1086 str = qemu_opt_get(opts, "string");
1087 gen_id = qemu_opt_get(opts, "gen_id");
1089 /* we need the name, and exactly one of: file, content string, gen_id */
1090 if (!nonempty_str(name) ||
1091 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1092 error_setg(errp, "name, plus exactly one of file,"
1093 " string and gen_id, are needed");
1094 return -1;
1096 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1097 error_setg(errp, "name too long (max. %d char)",
1098 FW_CFG_MAX_FILE_PATH - 1);
1099 return -1;
1101 if (nonempty_str(gen_id)) {
1103 * In this particular case where the content is populated
1104 * internally, the "etc/" namespace protection is relaxed,
1105 * so do not emit a warning.
1107 } else if (strncmp(name, "opt/", 4) != 0) {
1108 warn_report("externally provided fw_cfg item names "
1109 "should be prefixed with \"opt/\"");
1111 if (nonempty_str(str)) {
1112 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1113 buf = g_memdup(str, size);
1114 } else if (nonempty_str(gen_id)) {
1115 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1116 return -1;
1118 return 0;
1119 } else {
1120 GError *err = NULL;
1121 if (!g_file_get_contents(file, &buf, &size, &err)) {
1122 error_setg(errp, "can't load %s: %s", file, err->message);
1123 g_error_free(err);
1124 return -1;
1127 /* For legacy, keep user files in a specific global order. */
1128 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1129 fw_cfg_add_file(fw_cfg, name, buf, size);
1130 fw_cfg_reset_order_override(fw_cfg);
1131 return 0;
1134 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1136 return qdev_device_help(opts);
1139 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1141 DeviceState *dev;
1143 dev = qdev_device_add(opts, errp);
1144 if (!dev && *errp) {
1145 error_report_err(*errp);
1146 return -1;
1147 } else if (dev) {
1148 object_unref(OBJECT(dev));
1150 return 0;
1153 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1155 Error *local_err = NULL;
1157 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1158 if (local_err) {
1159 error_propagate(errp, local_err);
1160 return -1;
1162 exit(0);
1164 return 0;
1167 #ifdef CONFIG_VIRTFS
1168 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1170 return qemu_fsdev_add(opts, errp);
1172 #endif
1174 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1176 return monitor_init_opts(opts, errp);
1179 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1181 static int monitor_device_index = 0;
1182 QemuOpts *opts;
1183 const char *p;
1184 char label[32];
1186 if (strstart(optarg, "chardev:", &p)) {
1187 snprintf(label, sizeof(label), "%s", p);
1188 } else {
1189 snprintf(label, sizeof(label), "compat_monitor%d",
1190 monitor_device_index);
1191 opts = qemu_chr_parse_compat(label, optarg, true);
1192 if (!opts) {
1193 error_report("parse error: %s", optarg);
1194 exit(1);
1198 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1199 qemu_opt_set(opts, "mode", mode, &error_abort);
1200 qemu_opt_set(opts, "chardev", label, &error_abort);
1201 if (!strcmp(mode, "control")) {
1202 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1203 } else {
1204 assert(pretty == false);
1206 monitor_device_index++;
1209 struct device_config {
1210 enum {
1211 DEV_USB, /* -usbdevice */
1212 DEV_SERIAL, /* -serial */
1213 DEV_PARALLEL, /* -parallel */
1214 DEV_DEBUGCON, /* -debugcon */
1215 DEV_GDB, /* -gdb, -s */
1216 DEV_SCLP, /* s390 sclp */
1217 } type;
1218 const char *cmdline;
1219 Location loc;
1220 QTAILQ_ENTRY(device_config) next;
1223 static QTAILQ_HEAD(, device_config) device_configs =
1224 QTAILQ_HEAD_INITIALIZER(device_configs);
1226 static void add_device_config(int type, const char *cmdline)
1228 struct device_config *conf;
1230 conf = g_malloc0(sizeof(*conf));
1231 conf->type = type;
1232 conf->cmdline = cmdline;
1233 loc_save(&conf->loc);
1234 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1237 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1239 struct device_config *conf;
1240 int rc;
1242 QTAILQ_FOREACH(conf, &device_configs, next) {
1243 if (conf->type != type)
1244 continue;
1245 loc_push_restore(&conf->loc);
1246 rc = func(conf->cmdline);
1247 loc_pop(&conf->loc);
1248 if (rc) {
1249 return rc;
1252 return 0;
1255 static void qemu_disable_default_devices(void)
1257 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1259 default_driver_check_json();
1260 qemu_opts_foreach(qemu_find_opts("device"),
1261 default_driver_check, NULL, NULL);
1262 qemu_opts_foreach(qemu_find_opts("global"),
1263 default_driver_check, NULL, NULL);
1265 if (!vga_model && !default_vga) {
1266 vga_interface_type = VGA_DEVICE;
1267 vga_interface_created = true;
1269 if (!has_defaults || machine_class->no_serial) {
1270 default_serial = 0;
1272 if (!has_defaults || machine_class->no_parallel) {
1273 default_parallel = 0;
1275 if (!has_defaults || machine_class->no_floppy) {
1276 default_floppy = 0;
1278 if (!has_defaults || machine_class->no_cdrom) {
1279 default_cdrom = 0;
1281 if (!has_defaults || machine_class->no_sdcard) {
1282 default_sdcard = 0;
1284 if (!has_defaults) {
1285 default_monitor = 0;
1286 default_net = 0;
1287 default_vga = 0;
1291 static void qemu_create_default_devices(void)
1293 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1295 if (is_daemonized()) {
1296 /* According to documentation and historically, -nographic redirects
1297 * serial port, parallel port and monitor to stdio, which does not work
1298 * with -daemonize. We can redirect these to null instead, but since
1299 * -nographic is legacy, let's just error out.
1300 * We disallow -nographic only if all other ports are not redirected
1301 * explicitly, to not break existing legacy setups which uses
1302 * -nographic _and_ redirects all ports explicitly - this is valid
1303 * usage, -nographic is just a no-op in this case.
1305 if (nographic
1306 && (default_parallel || default_serial || default_monitor)) {
1307 error_report("-nographic cannot be used with -daemonize");
1308 exit(1);
1312 if (nographic) {
1313 if (default_parallel)
1314 add_device_config(DEV_PARALLEL, "null");
1315 if (default_serial && default_monitor) {
1316 add_device_config(DEV_SERIAL, "mon:stdio");
1317 } else {
1318 if (default_serial)
1319 add_device_config(DEV_SERIAL, "stdio");
1320 if (default_monitor)
1321 monitor_parse("stdio", "readline", false);
1323 } else {
1324 if (default_serial)
1325 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1326 if (default_parallel)
1327 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1328 if (default_monitor)
1329 monitor_parse("vc:80Cx24C", "readline", false);
1332 if (default_net) {
1333 QemuOptsList *net = qemu_find_opts("net");
1334 qemu_opts_parse(net, "nic", true, &error_abort);
1335 #ifdef CONFIG_SLIRP
1336 qemu_opts_parse(net, "user", true, &error_abort);
1337 #endif
1340 #if defined(CONFIG_VNC)
1341 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1342 display_remote++;
1344 #endif
1345 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1346 if (!qemu_display_find_default(&dpy)) {
1347 dpy.type = DISPLAY_TYPE_NONE;
1348 #if defined(CONFIG_VNC)
1349 vnc_parse("localhost:0,to=99,id=default");
1350 #endif
1353 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1354 dpy.type = DISPLAY_TYPE_NONE;
1357 /* If no default VGA is requested, the default is "none". */
1358 if (default_vga) {
1359 vga_model = get_default_vga_model(machine_class);
1361 if (vga_model) {
1362 select_vgahw(machine_class, vga_model);
1366 static int serial_parse(const char *devname)
1368 int index = num_serial_hds;
1369 char label[32];
1371 if (strcmp(devname, "none") == 0)
1372 return 0;
1373 snprintf(label, sizeof(label), "serial%d", index);
1374 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1376 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1377 if (!serial_hds[index]) {
1378 error_report("could not connect serial device"
1379 " to character backend '%s'", devname);
1380 return -1;
1382 num_serial_hds++;
1383 return 0;
1386 Chardev *serial_hd(int i)
1388 assert(i >= 0);
1389 if (i < num_serial_hds) {
1390 return serial_hds[i];
1392 return NULL;
1395 static int parallel_parse(const char *devname)
1397 static int index = 0;
1398 char label[32];
1400 if (strcmp(devname, "none") == 0)
1401 return 0;
1402 if (index == MAX_PARALLEL_PORTS) {
1403 error_report("too many parallel ports");
1404 exit(1);
1406 snprintf(label, sizeof(label), "parallel%d", index);
1407 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1408 if (!parallel_hds[index]) {
1409 error_report("could not connect parallel device"
1410 " to character backend '%s'", devname);
1411 return -1;
1413 index++;
1414 return 0;
1417 static int debugcon_parse(const char *devname)
1419 QemuOpts *opts;
1421 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1422 error_report("invalid character backend '%s'", devname);
1423 exit(1);
1425 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1426 if (!opts) {
1427 error_report("already have a debugcon device");
1428 exit(1);
1430 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1431 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1432 return 0;
1435 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1437 const MachineClass *mc1 = a, *mc2 = b;
1438 int res;
1440 if (mc1->family == NULL) {
1441 if (mc2->family == NULL) {
1442 /* Compare standalone machine types against each other; they sort
1443 * in increasing order.
1445 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1446 object_class_get_name(OBJECT_CLASS(mc2)));
1449 /* Standalone machine types sort after families. */
1450 return 1;
1453 if (mc2->family == NULL) {
1454 /* Families sort before standalone machine types. */
1455 return -1;
1458 /* Families sort between each other alphabetically increasingly. */
1459 res = strcmp(mc1->family, mc2->family);
1460 if (res != 0) {
1461 return res;
1464 /* Within the same family, machine types sort in decreasing order. */
1465 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1466 object_class_get_name(OBJECT_CLASS(mc1)));
1469 static void machine_help_func(const QDict *qdict)
1471 GSList *machines, *el;
1472 const char *type = qdict_get_try_str(qdict, "type");
1474 machines = object_class_get_list(TYPE_MACHINE, false);
1475 if (type) {
1476 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1477 if (machine_class) {
1478 type_print_class_properties(object_class_get_name(machine_class));
1479 return;
1483 printf("Supported machines are:\n");
1484 machines = g_slist_sort(machines, machine_class_cmp);
1485 for (el = machines; el; el = el->next) {
1486 MachineClass *mc = el->data;
1487 if (mc->alias) {
1488 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1490 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1491 mc->is_default ? " (default)" : "",
1492 mc->deprecation_reason ? " (deprecated)" : "");
1496 static void
1497 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1499 QDict *opts;
1501 opts = qdict_new();
1502 /* Preserve the caller's reference to prop. */
1503 qobject_ref(prop);
1504 qdict_put(opts, propname, prop);
1505 keyval_merge(machine_opts_dict, opts, errp);
1506 qobject_unref(opts);
1509 static void
1510 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1511 const char *arg)
1513 QDict *prop = NULL;
1514 bool help = false;
1516 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
1517 if (help) {
1518 qemu_opts_print_help(opts_list, true);
1519 exit(0);
1521 machine_merge_property(propname, prop, &error_fatal);
1522 qobject_unref(prop);
1525 static const char *pid_file;
1526 struct UnlinkPidfileNotifier {
1527 Notifier notifier;
1528 char *pid_file_realpath;
1530 static struct UnlinkPidfileNotifier qemu_unlink_pidfile_notifier;
1532 static void qemu_unlink_pidfile(Notifier *n, void *data)
1534 struct UnlinkPidfileNotifier *upn;
1536 upn = DO_UPCAST(struct UnlinkPidfileNotifier, notifier, n);
1537 unlink(upn->pid_file_realpath);
1540 static const QEMUOption *lookup_opt(int argc, char **argv,
1541 const char **poptarg, int *poptind)
1543 const QEMUOption *popt;
1544 int optind = *poptind;
1545 char *r = argv[optind];
1546 const char *optarg;
1548 loc_set_cmdline(argv, optind, 1);
1549 optind++;
1550 /* Treat --foo the same as -foo. */
1551 if (r[1] == '-')
1552 r++;
1553 popt = qemu_options;
1554 for(;;) {
1555 if (!popt->name) {
1556 error_report("invalid option");
1557 exit(1);
1559 if (!strcmp(popt->name, r + 1))
1560 break;
1561 popt++;
1563 if (popt->flags & HAS_ARG) {
1564 if (optind >= argc) {
1565 error_report("requires an argument");
1566 exit(1);
1568 optarg = argv[optind++];
1569 loc_set_cmdline(argv, optind - 2, 2);
1570 } else {
1571 optarg = NULL;
1574 *poptarg = optarg;
1575 *poptind = optind;
1577 return popt;
1580 static MachineClass *select_machine(QDict *qdict, Error **errp)
1582 const char *optarg = qdict_get_try_str(qdict, "type");
1583 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1584 MachineClass *machine_class;
1585 Error *local_err = NULL;
1587 if (optarg) {
1588 machine_class = find_machine(optarg, machines);
1589 qdict_del(qdict, "type");
1590 if (!machine_class) {
1591 error_setg(&local_err, "unsupported machine type");
1593 } else {
1594 machine_class = find_default_machine(machines);
1595 if (!machine_class) {
1596 error_setg(&local_err, "No machine specified, and there is no default");
1600 g_slist_free(machines);
1601 if (local_err) {
1602 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1603 error_propagate(errp, local_err);
1605 return machine_class;
1608 static int object_parse_property_opt(Object *obj,
1609 const char *name, const char *value,
1610 const char *skip, Error **errp)
1612 if (g_str_equal(name, skip)) {
1613 return 0;
1616 if (!object_property_parse(obj, name, value, errp)) {
1617 return -1;
1620 return 0;
1623 /* *Non*recursively replace underscores with dashes in QDict keys. */
1624 static void keyval_dashify(QDict *qdict, Error **errp)
1626 const QDictEntry *ent, *next;
1627 char *p;
1629 for (ent = qdict_first(qdict); ent; ent = next) {
1630 g_autofree char *new_key = NULL;
1632 next = qdict_next(qdict, ent);
1633 if (!strchr(ent->key, '_')) {
1634 continue;
1636 new_key = g_strdup(ent->key);
1637 for (p = new_key; *p; p++) {
1638 if (*p == '_') {
1639 *p = '-';
1642 if (qdict_haskey(qdict, new_key)) {
1643 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1644 return;
1646 qobject_ref(ent->value);
1647 qdict_put_obj(qdict, new_key, ent->value);
1648 qdict_del(qdict, ent->key);
1652 static void qemu_apply_legacy_machine_options(QDict *qdict)
1654 const char *value;
1655 QObject *prop;
1657 keyval_dashify(qdict, &error_fatal);
1659 /* Legacy options do not correspond to MachineState properties. */
1660 value = qdict_get_try_str(qdict, "accel");
1661 if (value) {
1662 accelerators = g_strdup(value);
1663 qdict_del(qdict, "accel");
1666 value = qdict_get_try_str(qdict, "igd-passthru");
1667 if (value) {
1668 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1669 false);
1670 qdict_del(qdict, "igd-passthru");
1673 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1674 if (value) {
1675 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1676 false);
1677 qdict_del(qdict, "kvm-shadow-mem");
1680 value = qdict_get_try_str(qdict, "kernel-irqchip");
1681 if (value) {
1682 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1683 false);
1684 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1685 false);
1686 qdict_del(qdict, "kernel-irqchip");
1689 value = qdict_get_try_str(qdict, "memory-backend");
1690 if (value) {
1691 if (mem_path) {
1692 error_report("'-mem-path' can't be used together with"
1693 "'-machine memory-backend'");
1694 exit(EXIT_FAILURE);
1697 /* Resolved later. */
1698 ram_memdev_id = g_strdup(value);
1699 qdict_del(qdict, "memory-backend");
1702 prop = qdict_get(qdict, "memory");
1703 if (prop) {
1704 have_custom_ram_size =
1705 qobject_type(prop) == QTYPE_QDICT &&
1706 qdict_haskey(qobject_to(QDict, prop), "size");
1710 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1712 ObjectOption *opt, *next;
1714 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1715 const char *type = ObjectType_str(opt->opts->qom_type);
1716 if (type_opt_predicate(type)) {
1717 user_creatable_add_qapi(opt->opts, &error_fatal);
1718 qapi_free_ObjectOptions(opt->opts);
1719 QTAILQ_REMOVE(&object_opts, opt, next);
1720 g_free(opt);
1725 static void object_option_add_visitor(Visitor *v)
1727 ObjectOption *opt = g_new0(ObjectOption, 1);
1728 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1729 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1732 static void object_option_parse(const char *optarg)
1734 QemuOpts *opts;
1735 const char *type;
1736 Visitor *v;
1738 if (optarg[0] == '{') {
1739 QObject *obj = qobject_from_json(optarg, &error_fatal);
1741 v = qobject_input_visitor_new(obj);
1742 qobject_unref(obj);
1743 } else {
1744 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1745 optarg, true);
1746 if (!opts) {
1747 exit(1);
1750 type = qemu_opt_get(opts, "qom-type");
1751 if (!type) {
1752 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1754 if (user_creatable_print_help(type, opts)) {
1755 exit(0);
1758 v = opts_visitor_new(opts);
1761 object_option_add_visitor(v);
1762 visit_free(v);
1766 * Very early object creation, before the sandbox options have been activated.
1768 static bool object_create_pre_sandbox(const char *type)
1771 * Objects should in general not get initialized "too early" without
1772 * a reason. If you add one, state the reason in a comment!
1776 * Reason: -sandbox on,resourcecontrol=deny disallows setting CPU
1777 * affinity of threads.
1779 if (g_str_equal(type, "thread-context")) {
1780 return true;
1783 return false;
1787 * Initial object creation happens before all other
1788 * QEMU data types are created. The majority of objects
1789 * can be created at this point. The rng-egd object
1790 * cannot be created here, as it depends on the chardev
1791 * already existing.
1793 static bool object_create_early(const char *type)
1796 * Objects should not be made "delayed" without a reason. If you
1797 * add one, state the reason in a comment!
1800 /* Reason: already created. */
1801 if (object_create_pre_sandbox(type)) {
1802 return false;
1805 /* Reason: property "chardev" */
1806 if (g_str_equal(type, "rng-egd") ||
1807 g_str_equal(type, "qtest")) {
1808 return false;
1811 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1812 /* Reason: cryptodev-vhost-user property "chardev" */
1813 if (g_str_equal(type, "cryptodev-vhost-user")) {
1814 return false;
1816 #endif
1818 /* Reason: vhost-user-blk-server property "node-name" */
1819 if (g_str_equal(type, "vhost-user-blk-server")) {
1820 return false;
1823 * Reason: filter-* property "netdev" etc.
1825 if (g_str_equal(type, "filter-buffer") ||
1826 g_str_equal(type, "filter-dump") ||
1827 g_str_equal(type, "filter-mirror") ||
1828 g_str_equal(type, "filter-redirector") ||
1829 g_str_equal(type, "colo-compare") ||
1830 g_str_equal(type, "filter-rewriter") ||
1831 g_str_equal(type, "filter-replay")) {
1832 return false;
1836 * Allocation of large amounts of memory may delay
1837 * chardev initialization for too long, and trigger timeouts
1838 * on software that waits for a monitor socket to be created
1839 * (e.g. libvirt).
1841 if (g_str_has_prefix(type, "memory-backend-")) {
1842 return false;
1845 return true;
1848 static void qemu_apply_machine_options(QDict *qdict)
1850 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1852 if (semihosting_enabled(false) && !semihosting_get_argc()) {
1853 /* fall back to the -kernel/-append */
1854 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1857 if (current_machine->smp.cpus > 1) {
1858 Error *blocker = NULL;
1859 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1860 replay_add_blocker(blocker);
1864 static void qemu_create_early_backends(void)
1866 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1867 #if defined(CONFIG_SDL)
1868 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1869 #else
1870 const bool use_sdl = false;
1871 #endif
1872 #if defined(CONFIG_GTK)
1873 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1874 #else
1875 const bool use_gtk = false;
1876 #endif
1878 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1879 error_report("window-close is only valid for GTK and SDL, "
1880 "ignoring option");
1883 qemu_display_early_init(&dpy);
1884 qemu_console_early_init();
1886 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1887 #if defined(CONFIG_OPENGL)
1888 error_report("OpenGL is not supported by the display");
1889 #else
1890 error_report("OpenGL support is disabled");
1891 #endif
1892 exit(1);
1895 object_option_foreach_add(object_create_early);
1897 /* spice needs the timers to be initialized by this point */
1898 /* spice must initialize before audio as it changes the default audiodev */
1899 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1900 qemu_spice.init();
1902 qemu_opts_foreach(qemu_find_opts("chardev"),
1903 chardev_init_func, NULL, &error_fatal);
1905 #ifdef CONFIG_VIRTFS
1906 qemu_opts_foreach(qemu_find_opts("fsdev"),
1907 fsdev_init_func, NULL, &error_fatal);
1908 #endif
1911 * Note: we need to create audio and block backends before
1912 * setting machine properties, so they can be referred to.
1914 configure_blockdev(&bdo_queue, machine_class, snapshot);
1915 if (!audio_init_audiodevs()) {
1916 exit(1);
1922 * The remainder of object creation happens after the
1923 * creation of chardev, fsdev, net clients and device data types.
1925 static bool object_create_late(const char *type)
1927 return !object_create_early(type) && !object_create_pre_sandbox(type);
1930 static void qemu_create_late_backends(void)
1932 if (qtest_chrdev) {
1933 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1936 net_init_clients();
1938 object_option_foreach_add(object_create_late);
1940 if (tpm_init() < 0) {
1941 exit(1);
1944 qemu_opts_foreach(qemu_find_opts("mon"),
1945 mon_init_func, NULL, &error_fatal);
1947 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1948 exit(1);
1949 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1950 exit(1);
1951 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1952 exit(1);
1954 /* now chardevs have been created we may have semihosting to connect */
1955 qemu_semihosting_chardev_init();
1958 static void qemu_resolve_machine_memdev(void)
1960 if (ram_memdev_id) {
1961 Object *backend;
1962 ram_addr_t backend_size;
1964 backend = object_resolve_path_type(ram_memdev_id,
1965 TYPE_MEMORY_BACKEND, NULL);
1966 if (!backend) {
1967 error_report("Memory backend '%s' not found", ram_memdev_id);
1968 exit(EXIT_FAILURE);
1970 if (!have_custom_ram_size) {
1971 backend_size = object_property_get_uint(backend, "size", &error_abort);
1972 current_machine->ram_size = backend_size;
1974 object_property_set_link(OBJECT(current_machine),
1975 "memory-backend", backend, &error_fatal);
1979 static void parse_memory_options(void)
1981 QemuOpts *opts = qemu_find_opts_singleton("memory");
1982 QDict *dict, *prop;
1983 const char *mem_str;
1984 Location loc;
1986 loc_push_none(&loc);
1987 qemu_opts_loc_restore(opts);
1989 prop = qdict_new();
1991 if (qemu_opt_get_size(opts, "size", 0) != 0) {
1992 /* Fix up legacy suffix-less format */
1993 mem_str = qemu_opt_get(opts, "size");
1994 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
1995 g_autofree char *mib_str = g_strdup_printf("%sM", mem_str);
1996 qdict_put_str(prop, "size", mib_str);
1997 } else {
1998 qdict_put_str(prop, "size", mem_str);
2002 if (qemu_opt_get(opts, "maxmem")) {
2003 qdict_put_str(prop, "max-size", qemu_opt_get(opts, "maxmem"));
2005 if (qemu_opt_get(opts, "slots")) {
2006 qdict_put_str(prop, "slots", qemu_opt_get(opts, "slots"));
2009 dict = qdict_new();
2010 qdict_put(dict, "memory", prop);
2011 keyval_merge(machine_opts_dict, dict, &error_fatal);
2012 qobject_unref(dict);
2013 loc_pop(&loc);
2016 static void qemu_create_machine(QDict *qdict)
2018 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2019 object_set_machine_compat_props(machine_class->compat_props);
2021 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2022 object_property_add_child(object_get_root(), "machine",
2023 OBJECT(current_machine));
2024 object_property_add_child(container_get(OBJECT(current_machine),
2025 "/unattached"),
2026 "sysbus", OBJECT(sysbus_get_default()));
2028 if (machine_class->minimum_page_bits) {
2029 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2030 /* This would be a board error: specifying a minimum smaller than
2031 * a target's compile-time fixed setting.
2033 g_assert_not_reached();
2037 cpu_exec_init_all();
2038 page_size_init();
2040 if (machine_class->hw_version) {
2041 qemu_set_hw_version(machine_class->hw_version);
2045 * Get the default machine options from the machine if it is not already
2046 * specified either by the configuration file or by the command line.
2048 if (machine_class->default_machine_opts) {
2049 QDict *default_opts =
2050 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2051 &error_abort);
2052 qemu_apply_legacy_machine_options(default_opts);
2053 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2054 false, &error_abort);
2055 qobject_unref(default_opts);
2059 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2061 GlobalProperty *g;
2063 g = g_malloc0(sizeof(*g));
2064 g->driver = qemu_opt_get(opts, "driver");
2065 g->property = qemu_opt_get(opts, "property");
2066 g->value = qemu_opt_get(opts, "value");
2067 qdev_prop_register_global(g);
2068 return 0;
2072 * Return whether configuration group @group is stored in QemuOpts, or
2073 * recorded as one or more QDicts by qemu_record_config_group.
2075 static bool is_qemuopts_group(const char *group)
2077 if (g_str_equal(group, "object") ||
2078 g_str_equal(group, "machine") ||
2079 g_str_equal(group, "smp-opts") ||
2080 g_str_equal(group, "boot-opts")) {
2081 return false;
2083 return true;
2086 static void qemu_record_config_group(const char *group, QDict *dict,
2087 bool from_json, Error **errp)
2089 if (g_str_equal(group, "object")) {
2090 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2091 object_option_add_visitor(v);
2092 visit_free(v);
2093 } else if (g_str_equal(group, "machine")) {
2095 * Cannot merge string-valued and type-safe dictionaries, so JSON
2096 * is not accepted yet for -M.
2098 assert(!from_json);
2099 keyval_merge(machine_opts_dict, dict, errp);
2100 } else if (g_str_equal(group, "smp-opts")) {
2101 machine_merge_property("smp", dict, &error_fatal);
2102 } else if (g_str_equal(group, "boot-opts")) {
2103 machine_merge_property("boot", dict, &error_fatal);
2104 } else {
2105 abort();
2110 * Parse non-QemuOpts config file groups, pass the rest to
2111 * qemu_config_do_parse.
2113 static void qemu_parse_config_group(const char *group, QDict *qdict,
2114 void *opaque, Error **errp)
2116 QObject *crumpled;
2117 if (is_qemuopts_group(group)) {
2118 qemu_config_do_parse(group, qdict, opaque, errp);
2119 return;
2122 crumpled = qdict_crumple(qdict, errp);
2123 if (!crumpled) {
2124 return;
2126 switch (qobject_type(crumpled)) {
2127 case QTYPE_QDICT:
2128 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2129 break;
2130 case QTYPE_QLIST:
2131 error_setg(errp, "Lists cannot be at top level of a configuration section");
2132 break;
2133 default:
2134 g_assert_not_reached();
2136 qobject_unref(crumpled);
2139 static void qemu_read_default_config_file(Error **errp)
2141 ERRP_GUARD();
2142 int ret;
2143 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2145 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2146 if (ret < 0) {
2147 if (ret == -ENOENT) {
2148 error_free(*errp);
2149 *errp = NULL;
2154 static void qemu_set_option(const char *str, Error **errp)
2156 char group[64], id[64], arg[64];
2157 QemuOptsList *list;
2158 QemuOpts *opts;
2159 int rc, offset;
2161 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2162 if (rc < 3 || str[offset] != '=') {
2163 error_setg(errp, "can't parse: \"%s\"", str);
2164 return;
2167 if (!is_qemuopts_group(group)) {
2168 error_setg(errp, "-set is not supported with %s", group);
2169 } else {
2170 list = qemu_find_opts_err(group, errp);
2171 if (list) {
2172 opts = qemu_opts_find(list, id);
2173 if (!opts) {
2174 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2175 return;
2177 qemu_opt_set(opts, arg, str + offset + 1, errp);
2182 static void user_register_global_props(void)
2184 qemu_opts_foreach(qemu_find_opts("global"),
2185 global_init_func, NULL, NULL);
2188 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2190 icount_configure(opts, errp);
2191 return 0;
2194 static int accelerator_set_property(void *opaque,
2195 const char *name, const char *value,
2196 Error **errp)
2198 return object_parse_property_opt(opaque, name, value, "accel", errp);
2201 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2203 bool *p_init_failed = opaque;
2204 const char *acc = qemu_opt_get(opts, "accel");
2205 AccelClass *ac = accel_find(acc);
2206 AccelState *accel;
2207 int ret;
2208 bool qtest_with_kvm;
2210 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2212 if (!ac) {
2213 *p_init_failed = true;
2214 if (!qtest_with_kvm) {
2215 error_report("invalid accelerator %s", acc);
2217 return 0;
2219 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2220 object_apply_compat_props(OBJECT(accel));
2221 qemu_opt_foreach(opts, accelerator_set_property,
2222 accel,
2223 &error_fatal);
2225 ret = accel_init_machine(accel, current_machine);
2226 if (ret < 0) {
2227 *p_init_failed = true;
2228 if (!qtest_with_kvm || ret != -ENOENT) {
2229 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2231 return 0;
2234 return 1;
2237 static void configure_accelerators(const char *progname)
2239 bool init_failed = false;
2241 qemu_opts_foreach(qemu_find_opts("icount"),
2242 do_configure_icount, NULL, &error_fatal);
2244 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2245 char **accel_list, **tmp;
2247 if (accelerators == NULL) {
2248 /* Select the default accelerator */
2249 bool have_tcg = accel_find("tcg");
2250 bool have_kvm = accel_find("kvm");
2252 if (have_tcg && have_kvm) {
2253 if (g_str_has_suffix(progname, "kvm")) {
2254 /* If the program name ends with "kvm", we prefer KVM */
2255 accelerators = "kvm:tcg";
2256 } else {
2257 accelerators = "tcg:kvm";
2259 } else if (have_kvm) {
2260 accelerators = "kvm";
2261 } else if (have_tcg) {
2262 accelerators = "tcg";
2263 } else {
2264 error_report("No accelerator selected and"
2265 " no default accelerator available");
2266 exit(1);
2269 accel_list = g_strsplit(accelerators, ":", 0);
2271 for (tmp = accel_list; *tmp; tmp++) {
2273 * Filter invalid accelerators here, to prevent obscenities
2274 * such as "-machine accel=tcg,,thread=single".
2276 if (accel_find(*tmp)) {
2277 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2278 } else {
2279 init_failed = true;
2280 error_report("invalid accelerator %s", *tmp);
2283 g_strfreev(accel_list);
2284 } else {
2285 if (accelerators != NULL) {
2286 error_report("The -accel and \"-machine accel=\" options are incompatible");
2287 exit(1);
2291 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2292 do_configure_accelerator, &init_failed, &error_fatal)) {
2293 if (!init_failed) {
2294 error_report("no accelerator found");
2296 exit(1);
2299 if (init_failed && !qtest_chrdev) {
2300 error_report("falling back to %s", current_accel_name());
2303 if (icount_enabled() && !tcg_enabled()) {
2304 error_report("-icount is not allowed with hardware virtualization");
2305 exit(1);
2309 static void qemu_validate_options(const QDict *machine_opts)
2311 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2312 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2313 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2315 if (kernel_filename == NULL) {
2316 if (kernel_cmdline != NULL) {
2317 error_report("-append only allowed with -kernel option");
2318 exit(1);
2321 if (initrd_filename != NULL) {
2322 error_report("-initrd only allowed with -kernel option");
2323 exit(1);
2327 if (loadvm && preconfig_requested) {
2328 error_report("'preconfig' and 'loadvm' options are "
2329 "mutually exclusive");
2330 exit(EXIT_FAILURE);
2332 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2333 error_report("'preconfig' supports '-incoming defer' only");
2334 exit(EXIT_FAILURE);
2337 #ifdef CONFIG_CURSES
2338 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2339 error_report("curses display cannot be used with -daemonize");
2340 exit(1);
2342 #endif
2345 static void qemu_process_sugar_options(void)
2347 if (mem_prealloc) {
2348 QObject *smp = qdict_get(machine_opts_dict, "smp");
2349 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2350 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2351 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2352 const char *val = qstring_get_str(qobject_to(QString, cpus));
2353 object_register_sugar_prop("memory-backend", "prealloc-threads",
2354 val, false);
2357 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2361 /* -action processing */
2364 * Process all the -action parameters parsed from cmdline.
2366 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2368 Error *local_err = NULL;
2369 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2370 QObject *ret = NULL;
2371 qmp_marshal_set_action(qdict, &ret, &local_err);
2372 qobject_unref(ret);
2373 qobject_unref(qdict);
2374 if (local_err) {
2375 error_propagate(errp, local_err);
2376 return 1;
2378 return 0;
2381 static void qemu_process_early_options(void)
2383 qemu_opts_foreach(qemu_find_opts("name"),
2384 parse_name, NULL, &error_fatal);
2386 object_option_foreach_add(object_create_pre_sandbox);
2388 #ifdef CONFIG_SECCOMP
2389 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2390 if (olist) {
2391 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2393 #endif
2395 if (qemu_opts_foreach(qemu_find_opts("action"),
2396 process_runstate_actions, NULL, &error_fatal)) {
2397 exit(1);
2400 #ifndef _WIN32
2401 qemu_opts_foreach(qemu_find_opts("add-fd"),
2402 parse_add_fd, NULL, &error_fatal);
2404 qemu_opts_foreach(qemu_find_opts("add-fd"),
2405 cleanup_add_fd, NULL, &error_fatal);
2406 #endif
2408 /* Open the logfile at this point and set the log mask if necessary. */
2410 int mask = 0;
2411 if (log_mask) {
2412 mask = qemu_str_to_log_mask(log_mask);
2413 if (!mask) {
2414 qemu_print_log_usage(stdout);
2415 exit(1);
2418 qemu_set_log_filename_flags(log_file, mask, &error_fatal);
2421 qemu_add_default_firmwarepath();
2424 static void qemu_process_help_options(void)
2427 * Check for -cpu help and -device help before we call select_machine(),
2428 * which will return an error if the architecture has no default machine
2429 * type and the user did not specify one, so that the user doesn't need
2430 * to say '-cpu help -machine something'.
2432 if (cpu_option && is_help_option(cpu_option)) {
2433 list_cpus(cpu_option);
2434 exit(0);
2437 if (qemu_opts_foreach(qemu_find_opts("device"),
2438 device_help_func, NULL, NULL)) {
2439 exit(0);
2442 /* -L help lists the data directories and exits. */
2443 if (list_data_dirs) {
2444 qemu_list_data_dirs();
2445 exit(0);
2449 static void qemu_maybe_daemonize(const char *pid_file)
2451 Error *err = NULL;
2453 os_daemonize();
2454 rcu_disable_atfork();
2456 if (pid_file) {
2457 char *pid_file_realpath = NULL;
2459 if (!qemu_write_pidfile(pid_file, &err)) {
2460 error_reportf_err(err, "cannot create PID file: ");
2461 exit(1);
2464 pid_file_realpath = g_malloc0(PATH_MAX);
2465 if (!realpath(pid_file, pid_file_realpath)) {
2466 error_report("cannot resolve PID file path: %s: %s",
2467 pid_file, strerror(errno));
2468 unlink(pid_file);
2469 exit(1);
2472 qemu_unlink_pidfile_notifier = (struct UnlinkPidfileNotifier) {
2473 .notifier = {
2474 .notify = qemu_unlink_pidfile,
2476 .pid_file_realpath = pid_file_realpath,
2478 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier.notifier);
2482 static void qemu_init_displays(void)
2484 DisplayState *ds;
2486 /* init local displays */
2487 ds = init_displaystate();
2488 qemu_display_init(ds, &dpy);
2490 /* must be after terminal init, SDL library changes signal handlers */
2491 os_setup_signal_handling();
2493 /* init remote displays */
2494 #ifdef CONFIG_VNC
2495 qemu_opts_foreach(qemu_find_opts("vnc"),
2496 vnc_init_func, NULL, &error_fatal);
2497 #endif
2499 if (using_spice) {
2500 qemu_spice.display_init();
2504 static void qemu_init_board(void)
2506 /* process plugin before CPUs are created, but once -smp has been parsed */
2507 qemu_plugin_load_list(&plugin_list, &error_fatal);
2509 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2510 machine_run_board_init(current_machine, mem_path, &error_fatal);
2512 drive_check_orphaned();
2514 realtime_init();
2516 if (hax_enabled()) {
2517 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2518 hax_sync_vcpus();
2522 static void qemu_create_cli_devices(void)
2524 DeviceOption *opt;
2526 soundhw_init();
2528 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2529 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2531 /* init USB devices */
2532 if (machine_usb(current_machine)) {
2533 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2534 exit(1);
2537 /* init generic devices */
2538 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2539 qemu_opts_foreach(qemu_find_opts("device"),
2540 device_init_func, NULL, &error_fatal);
2541 QTAILQ_FOREACH(opt, &device_opts, next) {
2542 DeviceState *dev;
2543 loc_push_restore(&opt->loc);
2545 * TODO Eventually we should call qmp_device_add() here to make sure it
2546 * behaves the same, but QMP still has to accept incorrectly typed
2547 * options until libvirt is fixed and we want to be strict on the CLI
2548 * from the start, so call qdev_device_add_from_qdict() directly for
2549 * now.
2551 dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2552 object_unref(OBJECT(dev));
2553 loc_pop(&opt->loc);
2555 rom_reset_order_override();
2558 static void qemu_machine_creation_done(void)
2560 MachineState *machine = MACHINE(qdev_get_machine());
2562 /* Did we create any drives that we failed to create a device for? */
2563 drive_check_orphaned();
2565 /* Don't warn about the default network setup that you get if
2566 * no command line -net or -netdev options are specified. There
2567 * are two cases that we would otherwise complain about:
2568 * (1) board doesn't support a NIC but the implicit "-net nic"
2569 * requested one
2570 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2571 * sets up a nic that isn't connected to anything.
2573 if (!default_net && (!qtest_enabled() || has_defaults)) {
2574 net_check_clients();
2577 qdev_prop_check_globals();
2579 qdev_machine_creation_done();
2581 if (machine->cgs) {
2583 * Verify that Confidential Guest Support has actually been initialized
2585 assert(machine->cgs->ready);
2588 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2589 exit(1);
2591 if (!vga_interface_created && !default_vga &&
2592 vga_interface_type != VGA_NONE) {
2593 warn_report("A -vga option was passed but this machine "
2594 "type does not use that option; "
2595 "No VGA device has been created");
2599 void qmp_x_exit_preconfig(Error **errp)
2601 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2602 error_setg(errp, "The command is permitted only before machine initialization");
2603 return;
2606 qemu_init_board();
2607 qemu_create_cli_devices();
2608 qemu_machine_creation_done();
2610 if (loadvm) {
2611 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
2613 if (replay_mode != REPLAY_MODE_NONE) {
2614 replay_vmstate_init();
2617 if (incoming) {
2618 Error *local_err = NULL;
2619 if (strcmp(incoming, "defer") != 0) {
2620 qmp_migrate_incoming(incoming, &local_err);
2621 if (local_err) {
2622 error_reportf_err(local_err, "-incoming %s: ", incoming);
2623 exit(1);
2626 } else if (autostart) {
2627 qmp_cont(NULL);
2631 void qemu_init(int argc, char **argv)
2633 QemuOpts *opts;
2634 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2635 QemuOptsList *olist;
2636 int optind;
2637 const char *optarg;
2638 MachineClass *machine_class;
2639 bool userconfig = true;
2640 FILE *vmstate_dump_file = NULL;
2642 qemu_add_opts(&qemu_drive_opts);
2643 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2644 qemu_add_drive_opts(&qemu_common_drive_opts);
2645 qemu_add_drive_opts(&qemu_drive_opts);
2646 qemu_add_drive_opts(&bdrv_runtime_opts);
2647 qemu_add_opts(&qemu_chardev_opts);
2648 qemu_add_opts(&qemu_device_opts);
2649 qemu_add_opts(&qemu_netdev_opts);
2650 qemu_add_opts(&qemu_nic_opts);
2651 qemu_add_opts(&qemu_net_opts);
2652 qemu_add_opts(&qemu_rtc_opts);
2653 qemu_add_opts(&qemu_global_opts);
2654 qemu_add_opts(&qemu_mon_opts);
2655 qemu_add_opts(&qemu_trace_opts);
2656 qemu_plugin_add_opts();
2657 qemu_add_opts(&qemu_option_rom_opts);
2658 qemu_add_opts(&qemu_accel_opts);
2659 qemu_add_opts(&qemu_mem_opts);
2660 qemu_add_opts(&qemu_smp_opts);
2661 qemu_add_opts(&qemu_boot_opts);
2662 qemu_add_opts(&qemu_add_fd_opts);
2663 qemu_add_opts(&qemu_object_opts);
2664 qemu_add_opts(&qemu_tpmdev_opts);
2665 qemu_add_opts(&qemu_overcommit_opts);
2666 qemu_add_opts(&qemu_msg_opts);
2667 qemu_add_opts(&qemu_name_opts);
2668 qemu_add_opts(&qemu_numa_opts);
2669 qemu_add_opts(&qemu_icount_opts);
2670 qemu_add_opts(&qemu_semihosting_config_opts);
2671 qemu_add_opts(&qemu_fw_cfg_opts);
2672 qemu_add_opts(&qemu_action_opts);
2673 module_call_init(MODULE_INIT_OPTS);
2675 error_init(argv[0]);
2676 qemu_init_exec_dir(argv[0]);
2678 qemu_init_arch_modules();
2680 qemu_init_subsystems();
2682 /* first pass of option parsing */
2683 optind = 1;
2684 while (optind < argc) {
2685 if (argv[optind][0] != '-') {
2686 /* disk image */
2687 optind++;
2688 } else {
2689 const QEMUOption *popt;
2691 popt = lookup_opt(argc, argv, &optarg, &optind);
2692 switch (popt->index) {
2693 case QEMU_OPTION_nouserconfig:
2694 userconfig = false;
2695 break;
2700 machine_opts_dict = qdict_new();
2701 if (userconfig) {
2702 qemu_read_default_config_file(&error_fatal);
2705 /* second pass of option parsing */
2706 optind = 1;
2707 for(;;) {
2708 if (optind >= argc)
2709 break;
2710 if (argv[optind][0] != '-') {
2711 loc_set_cmdline(argv, optind, 1);
2712 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2713 } else {
2714 const QEMUOption *popt;
2716 popt = lookup_opt(argc, argv, &optarg, &optind);
2717 if (!(popt->arch_mask & arch_type)) {
2718 error_report("Option not supported for this target");
2719 exit(1);
2721 switch(popt->index) {
2722 case QEMU_OPTION_cpu:
2723 /* hw initialization will check this */
2724 cpu_option = optarg;
2725 break;
2726 case QEMU_OPTION_hda:
2727 case QEMU_OPTION_hdb:
2728 case QEMU_OPTION_hdc:
2729 case QEMU_OPTION_hdd:
2730 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2731 HD_OPTS);
2732 break;
2733 case QEMU_OPTION_blockdev:
2735 Visitor *v;
2736 BlockdevOptionsQueueEntry *bdo;
2738 v = qobject_input_visitor_new_str(optarg, "driver",
2739 &error_fatal);
2741 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2742 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2743 &error_fatal);
2744 visit_free(v);
2745 loc_save(&bdo->loc);
2746 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2747 break;
2749 case QEMU_OPTION_drive:
2750 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
2751 optarg, false);
2752 if (opts == NULL) {
2753 exit(1);
2755 break;
2756 case QEMU_OPTION_set:
2757 qemu_set_option(optarg, &error_fatal);
2758 break;
2759 case QEMU_OPTION_global:
2760 if (qemu_global_option(optarg) != 0)
2761 exit(1);
2762 break;
2763 case QEMU_OPTION_mtdblock:
2764 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2765 break;
2766 case QEMU_OPTION_sd:
2767 drive_add(IF_SD, -1, optarg, SD_OPTS);
2768 break;
2769 case QEMU_OPTION_pflash:
2770 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2771 break;
2772 case QEMU_OPTION_snapshot:
2774 Error *blocker = NULL;
2775 snapshot = 1;
2776 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2777 "-snapshot");
2778 replay_add_blocker(blocker);
2780 break;
2781 case QEMU_OPTION_numa:
2782 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2783 optarg, true);
2784 if (!opts) {
2785 exit(1);
2787 break;
2788 case QEMU_OPTION_display:
2789 parse_display(optarg);
2790 break;
2791 case QEMU_OPTION_nographic:
2792 qdict_put_str(machine_opts_dict, "graphics", "off");
2793 nographic = true;
2794 dpy.type = DISPLAY_TYPE_NONE;
2795 break;
2796 case QEMU_OPTION_portrait:
2797 graphic_rotate = 90;
2798 break;
2799 case QEMU_OPTION_rotate:
2800 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2801 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2802 graphic_rotate != 180 && graphic_rotate != 270) {
2803 error_report("only 90, 180, 270 deg rotation is available");
2804 exit(1);
2806 break;
2807 case QEMU_OPTION_kernel:
2808 qdict_put_str(machine_opts_dict, "kernel", optarg);
2809 break;
2810 case QEMU_OPTION_initrd:
2811 qdict_put_str(machine_opts_dict, "initrd", optarg);
2812 break;
2813 case QEMU_OPTION_append:
2814 qdict_put_str(machine_opts_dict, "append", optarg);
2815 break;
2816 case QEMU_OPTION_dtb:
2817 qdict_put_str(machine_opts_dict, "dtb", optarg);
2818 break;
2819 case QEMU_OPTION_cdrom:
2820 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2821 break;
2822 case QEMU_OPTION_boot:
2823 machine_parse_property_opt(qemu_find_opts("boot-opts"), "boot", optarg);
2824 break;
2825 case QEMU_OPTION_fda:
2826 case QEMU_OPTION_fdb:
2827 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2828 optarg, FD_OPTS);
2829 break;
2830 case QEMU_OPTION_no_fd_bootchk:
2831 fd_bootchk = 0;
2832 break;
2833 case QEMU_OPTION_netdev:
2834 default_net = 0;
2835 if (netdev_is_modern(optarg)) {
2836 netdev_parse_modern(optarg);
2837 } else {
2838 net_client_parse(qemu_find_opts("netdev"), optarg);
2840 break;
2841 case QEMU_OPTION_nic:
2842 default_net = 0;
2843 net_client_parse(qemu_find_opts("nic"), optarg);
2844 break;
2845 case QEMU_OPTION_net:
2846 default_net = 0;
2847 net_client_parse(qemu_find_opts("net"), optarg);
2848 break;
2849 #ifdef CONFIG_LIBISCSI
2850 case QEMU_OPTION_iscsi:
2851 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2852 optarg, false);
2853 if (!opts) {
2854 exit(1);
2856 break;
2857 #endif
2858 case QEMU_OPTION_audio_help:
2859 audio_legacy_help();
2860 exit (0);
2861 break;
2862 case QEMU_OPTION_audiodev:
2863 audio_parse_option(optarg);
2864 break;
2865 case QEMU_OPTION_audio: {
2866 bool help;
2867 char *model;
2868 Audiodev *dev = NULL;
2869 Visitor *v;
2870 QDict *dict = keyval_parse(optarg, "driver", &help, &error_fatal);
2871 if (help || (qdict_haskey(dict, "driver") &&
2872 is_help_option(qdict_get_str(dict, "driver")))) {
2873 audio_help();
2874 exit(EXIT_SUCCESS);
2876 if (!qdict_haskey(dict, "id")) {
2877 qdict_put_str(dict, "id", "audiodev0");
2879 if (!qdict_haskey(dict, "model")) {
2880 error_setg(&error_fatal, "Parameter 'model' is missing");
2882 model = g_strdup(qdict_get_str(dict, "model"));
2883 qdict_del(dict, "model");
2884 if (is_help_option(model)) {
2885 show_valid_soundhw();
2886 exit(0);
2888 v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2889 qobject_unref(dict);
2890 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
2891 visit_free(v);
2892 audio_define(dev);
2893 select_soundhw(model, dev->id);
2894 g_free(model);
2895 break;
2897 case QEMU_OPTION_h:
2898 help(0);
2899 break;
2900 case QEMU_OPTION_version:
2901 version();
2902 exit(0);
2903 break;
2904 case QEMU_OPTION_m:
2905 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"), optarg, true);
2906 if (opts == NULL) {
2907 exit(1);
2909 break;
2910 #ifdef CONFIG_TPM
2911 case QEMU_OPTION_tpmdev:
2912 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2913 exit(1);
2915 break;
2916 #endif
2917 case QEMU_OPTION_mempath:
2918 mem_path = optarg;
2919 break;
2920 case QEMU_OPTION_mem_prealloc:
2921 mem_prealloc = 1;
2922 break;
2923 case QEMU_OPTION_d:
2924 log_mask = optarg;
2925 break;
2926 case QEMU_OPTION_D:
2927 log_file = optarg;
2928 break;
2929 case QEMU_OPTION_DFILTER:
2930 qemu_set_dfilter_ranges(optarg, &error_fatal);
2931 break;
2932 #if defined(CONFIG_TCG) && defined(CONFIG_LINUX)
2933 case QEMU_OPTION_perfmap:
2934 perf_enable_perfmap();
2935 break;
2936 case QEMU_OPTION_jitdump:
2937 perf_enable_jitdump();
2938 break;
2939 #endif
2940 case QEMU_OPTION_seed:
2941 qemu_guest_random_seed_main(optarg, &error_fatal);
2942 break;
2943 case QEMU_OPTION_s:
2944 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2945 break;
2946 case QEMU_OPTION_gdb:
2947 add_device_config(DEV_GDB, optarg);
2948 break;
2949 case QEMU_OPTION_L:
2950 if (is_help_option(optarg)) {
2951 list_data_dirs = true;
2952 } else {
2953 qemu_add_data_dir(g_strdup(optarg));
2955 break;
2956 case QEMU_OPTION_bios:
2957 qdict_put_str(machine_opts_dict, "firmware", optarg);
2958 break;
2959 case QEMU_OPTION_singlestep:
2960 singlestep = 1;
2961 break;
2962 case QEMU_OPTION_S:
2963 autostart = 0;
2964 break;
2965 case QEMU_OPTION_k:
2966 keyboard_layout = optarg;
2967 break;
2968 case QEMU_OPTION_vga:
2969 vga_model = optarg;
2970 default_vga = 0;
2971 break;
2972 case QEMU_OPTION_g:
2974 const char *p;
2975 int w, h, depth;
2976 p = optarg;
2977 w = strtol(p, (char **)&p, 10);
2978 if (w <= 0) {
2979 graphic_error:
2980 error_report("invalid resolution or depth");
2981 exit(1);
2983 if (*p != 'x')
2984 goto graphic_error;
2985 p++;
2986 h = strtol(p, (char **)&p, 10);
2987 if (h <= 0)
2988 goto graphic_error;
2989 if (*p == 'x') {
2990 p++;
2991 depth = strtol(p, (char **)&p, 10);
2992 if (depth != 1 && depth != 2 && depth != 4 &&
2993 depth != 8 && depth != 15 && depth != 16 &&
2994 depth != 24 && depth != 32)
2995 goto graphic_error;
2996 } else if (*p == '\0') {
2997 depth = graphic_depth;
2998 } else {
2999 goto graphic_error;
3002 graphic_width = w;
3003 graphic_height = h;
3004 graphic_depth = depth;
3006 break;
3007 case QEMU_OPTION_echr:
3009 char *r;
3010 term_escape_char = strtol(optarg, &r, 0);
3011 if (r == optarg)
3012 printf("Bad argument to echr\n");
3013 break;
3015 case QEMU_OPTION_monitor:
3016 default_monitor = 0;
3017 if (strncmp(optarg, "none", 4)) {
3018 monitor_parse(optarg, "readline", false);
3020 break;
3021 case QEMU_OPTION_qmp:
3022 monitor_parse(optarg, "control", false);
3023 default_monitor = 0;
3024 break;
3025 case QEMU_OPTION_qmp_pretty:
3026 monitor_parse(optarg, "control", true);
3027 default_monitor = 0;
3028 break;
3029 case QEMU_OPTION_mon:
3030 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3031 true);
3032 if (!opts) {
3033 exit(1);
3035 default_monitor = 0;
3036 break;
3037 case QEMU_OPTION_chardev:
3038 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3039 optarg, true);
3040 if (!opts) {
3041 exit(1);
3043 break;
3044 case QEMU_OPTION_fsdev:
3045 olist = qemu_find_opts("fsdev");
3046 if (!olist) {
3047 error_report("fsdev support is disabled");
3048 exit(1);
3050 opts = qemu_opts_parse_noisily(olist, optarg, true);
3051 if (!opts) {
3052 exit(1);
3054 break;
3055 case QEMU_OPTION_virtfs: {
3056 QemuOpts *fsdev;
3057 QemuOpts *device;
3058 const char *writeout, *sock_fd, *socket, *path, *security_model,
3059 *multidevs;
3061 olist = qemu_find_opts("virtfs");
3062 if (!olist) {
3063 error_report("virtfs support is disabled");
3064 exit(1);
3066 opts = qemu_opts_parse_noisily(olist, optarg, true);
3067 if (!opts) {
3068 exit(1);
3071 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3072 qemu_opt_get(opts, "mount_tag") == NULL) {
3073 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3074 exit(1);
3076 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3077 qemu_opts_id(opts) ?:
3078 qemu_opt_get(opts, "mount_tag"),
3079 1, NULL);
3080 if (!fsdev) {
3081 error_report("duplicate or invalid fsdev id: %s",
3082 qemu_opt_get(opts, "mount_tag"));
3083 exit(1);
3086 writeout = qemu_opt_get(opts, "writeout");
3087 if (writeout) {
3088 #ifdef CONFIG_SYNC_FILE_RANGE
3089 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3090 #else
3091 error_report("writeout=immediate not supported "
3092 "on this platform");
3093 exit(1);
3094 #endif
3096 qemu_opt_set(fsdev, "fsdriver",
3097 qemu_opt_get(opts, "fsdriver"), &error_abort);
3098 path = qemu_opt_get(opts, "path");
3099 if (path) {
3100 qemu_opt_set(fsdev, "path", path, &error_abort);
3102 security_model = qemu_opt_get(opts, "security_model");
3103 if (security_model) {
3104 qemu_opt_set(fsdev, "security_model", security_model,
3105 &error_abort);
3107 socket = qemu_opt_get(opts, "socket");
3108 if (socket) {
3109 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3111 sock_fd = qemu_opt_get(opts, "sock_fd");
3112 if (sock_fd) {
3113 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3116 qemu_opt_set_bool(fsdev, "readonly",
3117 qemu_opt_get_bool(opts, "readonly", 0),
3118 &error_abort);
3119 multidevs = qemu_opt_get(opts, "multidevs");
3120 if (multidevs) {
3121 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3123 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3124 &error_abort);
3125 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3126 qemu_opt_set(device, "fsdev",
3127 qemu_opts_id(fsdev), &error_abort);
3128 qemu_opt_set(device, "mount_tag",
3129 qemu_opt_get(opts, "mount_tag"), &error_abort);
3130 break;
3132 case QEMU_OPTION_serial:
3133 add_device_config(DEV_SERIAL, optarg);
3134 default_serial = 0;
3135 if (strncmp(optarg, "mon:", 4) == 0) {
3136 default_monitor = 0;
3138 break;
3139 case QEMU_OPTION_action:
3140 olist = qemu_find_opts("action");
3141 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3142 exit(1);
3144 break;
3145 case QEMU_OPTION_watchdog_action: {
3146 QemuOpts *opts;
3147 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3148 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
3149 break;
3151 case QEMU_OPTION_parallel:
3152 add_device_config(DEV_PARALLEL, optarg);
3153 default_parallel = 0;
3154 if (strncmp(optarg, "mon:", 4) == 0) {
3155 default_monitor = 0;
3157 break;
3158 case QEMU_OPTION_debugcon:
3159 add_device_config(DEV_DEBUGCON, optarg);
3160 break;
3161 case QEMU_OPTION_loadvm:
3162 loadvm = optarg;
3163 break;
3164 case QEMU_OPTION_full_screen:
3165 dpy.has_full_screen = true;
3166 dpy.full_screen = true;
3167 break;
3168 case QEMU_OPTION_pidfile:
3169 pid_file = optarg;
3170 break;
3171 case QEMU_OPTION_win2k_hack:
3172 win2k_install_hack = 1;
3173 break;
3174 case QEMU_OPTION_acpitable:
3175 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3176 optarg, true);
3177 if (!opts) {
3178 exit(1);
3180 acpi_table_add(opts, &error_fatal);
3181 break;
3182 case QEMU_OPTION_smbios:
3183 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3184 optarg, false);
3185 if (!opts) {
3186 exit(1);
3188 smbios_entry_add(opts, &error_fatal);
3189 break;
3190 case QEMU_OPTION_fwcfg:
3191 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3192 optarg, true);
3193 if (opts == NULL) {
3194 exit(1);
3196 break;
3197 case QEMU_OPTION_preconfig:
3198 preconfig_requested = true;
3199 break;
3200 case QEMU_OPTION_enable_kvm:
3201 qdict_put_str(machine_opts_dict, "accel", "kvm");
3202 break;
3203 case QEMU_OPTION_M:
3204 case QEMU_OPTION_machine:
3206 bool help;
3208 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3209 if (help) {
3210 machine_help_func(machine_opts_dict);
3211 exit(EXIT_SUCCESS);
3213 break;
3215 case QEMU_OPTION_accel:
3216 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3217 optarg, true);
3218 optarg = qemu_opt_get(accel_opts, "accel");
3219 if (!optarg || is_help_option(optarg)) {
3220 printf("Accelerators supported in QEMU binary:\n");
3221 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3222 false);
3223 for (el = accel_list; el; el = el->next) {
3224 gchar *typename = g_strdup(object_class_get_name(
3225 OBJECT_CLASS(el->data)));
3226 /* omit qtest which is used for tests only */
3227 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3228 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3229 gchar **optname = g_strsplit(typename,
3230 ACCEL_CLASS_SUFFIX, 0);
3231 printf("%s\n", optname[0]);
3232 g_strfreev(optname);
3234 g_free(typename);
3236 g_slist_free(accel_list);
3237 exit(0);
3239 break;
3240 case QEMU_OPTION_usb:
3241 qdict_put_str(machine_opts_dict, "usb", "on");
3242 break;
3243 case QEMU_OPTION_usbdevice:
3244 qdict_put_str(machine_opts_dict, "usb", "on");
3245 add_device_config(DEV_USB, optarg);
3246 break;
3247 case QEMU_OPTION_device:
3248 if (optarg[0] == '{') {
3249 QObject *obj = qobject_from_json(optarg, &error_fatal);
3250 DeviceOption *opt = g_new0(DeviceOption, 1);
3251 opt->opts = qobject_to(QDict, obj);
3252 loc_save(&opt->loc);
3253 assert(opt->opts != NULL);
3254 QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3255 } else {
3256 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3257 optarg, true)) {
3258 exit(1);
3261 break;
3262 case QEMU_OPTION_smp:
3263 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3264 "smp", optarg);
3265 break;
3266 case QEMU_OPTION_vnc:
3267 vnc_parse(optarg);
3268 break;
3269 case QEMU_OPTION_no_acpi:
3270 qdict_put_str(machine_opts_dict, "acpi", "off");
3271 break;
3272 case QEMU_OPTION_no_hpet:
3273 warn_report("-no-hpet is deprecated, use '-machine hpet=off' instead");
3274 qdict_put_str(machine_opts_dict, "hpet", "off");
3275 break;
3276 case QEMU_OPTION_no_reboot:
3277 olist = qemu_find_opts("action");
3278 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3279 break;
3280 case QEMU_OPTION_no_shutdown:
3281 olist = qemu_find_opts("action");
3282 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3283 break;
3284 case QEMU_OPTION_uuid:
3285 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3286 error_report("failed to parse UUID string: wrong format");
3287 exit(1);
3289 qemu_uuid_set = true;
3290 break;
3291 case QEMU_OPTION_option_rom:
3292 if (nb_option_roms >= MAX_OPTION_ROMS) {
3293 error_report("too many option ROMs");
3294 exit(1);
3296 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3297 optarg, true);
3298 if (!opts) {
3299 exit(1);
3301 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3302 option_rom[nb_option_roms].bootindex =
3303 qemu_opt_get_number(opts, "bootindex", -1);
3304 if (!option_rom[nb_option_roms].name) {
3305 error_report("Option ROM file is not specified");
3306 exit(1);
3308 nb_option_roms++;
3309 break;
3310 case QEMU_OPTION_semihosting:
3311 qemu_semihosting_enable();
3312 break;
3313 case QEMU_OPTION_semihosting_config:
3314 if (qemu_semihosting_config_options(optarg) != 0) {
3315 exit(1);
3317 break;
3318 case QEMU_OPTION_name:
3319 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3320 optarg, true);
3321 if (!opts) {
3322 exit(1);
3324 /* Capture guest name if -msg guest-name is used later */
3325 error_guest_name = qemu_opt_get(opts, "guest");
3326 break;
3327 case QEMU_OPTION_prom_env:
3328 if (nb_prom_envs >= MAX_PROM_ENVS) {
3329 error_report("too many prom variables");
3330 exit(1);
3332 prom_envs[nb_prom_envs] = optarg;
3333 nb_prom_envs++;
3334 break;
3335 case QEMU_OPTION_old_param:
3336 old_param = 1;
3337 break;
3338 case QEMU_OPTION_rtc:
3339 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3340 false);
3341 if (!opts) {
3342 exit(1);
3344 break;
3345 case QEMU_OPTION_icount:
3346 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3347 optarg, true);
3348 if (!icount_opts) {
3349 exit(1);
3351 break;
3352 case QEMU_OPTION_incoming:
3353 if (!incoming) {
3354 runstate_set(RUN_STATE_INMIGRATE);
3356 incoming = optarg;
3357 break;
3358 case QEMU_OPTION_only_migratable:
3359 only_migratable = 1;
3360 break;
3361 case QEMU_OPTION_nodefaults:
3362 has_defaults = 0;
3363 break;
3364 case QEMU_OPTION_xen_domid:
3365 if (!(accel_find("xen"))) {
3366 error_report("Option not supported for this target");
3367 exit(1);
3369 xen_domid = atoi(optarg);
3370 break;
3371 case QEMU_OPTION_xen_attach:
3372 if (!(accel_find("xen"))) {
3373 error_report("Option not supported for this target");
3374 exit(1);
3376 xen_mode = XEN_ATTACH;
3377 break;
3378 case QEMU_OPTION_xen_domid_restrict:
3379 if (!(accel_find("xen"))) {
3380 error_report("Option not supported for this target");
3381 exit(1);
3383 xen_domid_restrict = true;
3384 break;
3385 case QEMU_OPTION_trace:
3386 trace_opt_parse(optarg);
3387 break;
3388 case QEMU_OPTION_plugin:
3389 qemu_plugin_opt_parse(optarg, &plugin_list);
3390 break;
3391 case QEMU_OPTION_readconfig:
3392 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3393 break;
3394 #ifdef CONFIG_SPICE
3395 case QEMU_OPTION_spice:
3396 olist = qemu_find_opts_err("spice", NULL);
3397 if (!olist) {
3398 error_report("spice support is disabled");
3399 exit(1);
3401 opts = qemu_opts_parse_noisily(olist, optarg, false);
3402 if (!opts) {
3403 exit(1);
3405 display_remote++;
3406 break;
3407 #endif
3408 case QEMU_OPTION_qtest:
3409 qtest_chrdev = optarg;
3410 break;
3411 case QEMU_OPTION_qtest_log:
3412 qtest_log = optarg;
3413 break;
3414 case QEMU_OPTION_sandbox:
3415 olist = qemu_find_opts("sandbox");
3416 if (!olist) {
3417 #ifndef CONFIG_SECCOMP
3418 error_report("-sandbox support is not enabled "
3419 "in this QEMU binary");
3420 #endif
3421 exit(1);
3424 opts = qemu_opts_parse_noisily(olist, optarg, true);
3425 if (!opts) {
3426 exit(1);
3428 break;
3429 case QEMU_OPTION_add_fd:
3430 #ifndef _WIN32
3431 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3432 optarg, false);
3433 if (!opts) {
3434 exit(1);
3436 #else
3437 error_report("File descriptor passing is disabled on this "
3438 "platform");
3439 exit(1);
3440 #endif
3441 break;
3442 case QEMU_OPTION_object:
3443 object_option_parse(optarg);
3444 break;
3445 case QEMU_OPTION_overcommit:
3446 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3447 optarg, false);
3448 if (!opts) {
3449 exit(1);
3451 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3452 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3453 break;
3454 case QEMU_OPTION_compat:
3456 CompatPolicy *opts;
3457 Visitor *v;
3459 v = qobject_input_visitor_new_str(optarg, NULL,
3460 &error_fatal);
3462 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3463 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3465 qapi_free_CompatPolicy(opts);
3466 visit_free(v);
3467 break;
3469 case QEMU_OPTION_msg:
3470 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3471 false);
3472 if (!opts) {
3473 exit(1);
3475 configure_msg(opts);
3476 break;
3477 case QEMU_OPTION_dump_vmstate:
3478 if (vmstate_dump_file) {
3479 error_report("only one '-dump-vmstate' "
3480 "option may be given");
3481 exit(1);
3483 vmstate_dump_file = fopen(optarg, "w");
3484 if (vmstate_dump_file == NULL) {
3485 error_report("open %s: %s", optarg, strerror(errno));
3486 exit(1);
3488 break;
3489 case QEMU_OPTION_enable_sync_profile:
3490 qsp_enable();
3491 break;
3492 case QEMU_OPTION_nouserconfig:
3493 /* Nothing to be parsed here. Especially, do not error out below. */
3494 break;
3495 default:
3496 if (os_parse_cmd_args(popt->index, optarg)) {
3497 error_report("Option not supported in this build");
3498 exit(1);
3504 * Clear error location left behind by the loop.
3505 * Best done right after the loop. Do not insert code here!
3507 loc_set_none();
3509 qemu_validate_options(machine_opts_dict);
3510 qemu_process_sugar_options();
3513 * These options affect everything else and should be processed
3514 * before daemonizing.
3516 qemu_process_early_options();
3518 qemu_process_help_options();
3519 qemu_maybe_daemonize(pid_file);
3522 * The trace backend must be initialized after daemonizing.
3523 * trace_init_backends() will call st_init(), which will create the
3524 * trace thread in the parent, and also register st_flush_trace_buffer()
3525 * in atexit(). This function will force the parent to wait for the
3526 * writeout thread to finish, which will not occur, and the parent
3527 * process will be left in the host.
3529 if (!trace_init_backends()) {
3530 exit(1);
3532 trace_init_file();
3534 qemu_init_main_loop(&error_fatal);
3535 cpu_timers_init();
3537 user_register_global_props();
3538 replay_configure(icount_opts);
3540 configure_rtc(qemu_find_opts_singleton("rtc"));
3542 /* Transfer QemuOpts options into machine options */
3543 parse_memory_options();
3545 qemu_create_machine(machine_opts_dict);
3547 suspend_mux_open();
3549 qemu_disable_default_devices();
3550 qemu_create_default_devices();
3551 qemu_create_early_backends();
3553 qemu_apply_legacy_machine_options(machine_opts_dict);
3554 qemu_apply_machine_options(machine_opts_dict);
3555 qobject_unref(machine_opts_dict);
3556 phase_advance(PHASE_MACHINE_CREATED);
3559 * Note: uses machine properties such as kernel-irqchip, must run
3560 * after qemu_apply_machine_options.
3562 configure_accelerators(argv[0]);
3563 phase_advance(PHASE_ACCEL_CREATED);
3566 * Beware, QOM objects created before this point miss global and
3567 * compat properties.
3569 * Global properties get set up by qdev_prop_register_global(),
3570 * called from user_register_global_props(), and certain option
3571 * desugaring. Also in CPU feature desugaring (buried in
3572 * parse_cpu_option()), which happens below this point, but may
3573 * only target the CPU type, which can only be created after
3574 * parse_cpu_option() returned the type.
3576 * Machine compat properties: object_set_machine_compat_props().
3577 * Accelerator compat props: object_set_accelerator_compat_props(),
3578 * called from do_configure_accelerator().
3581 machine_class = MACHINE_GET_CLASS(current_machine);
3582 if (!qtest_enabled() && machine_class->deprecation_reason) {
3583 warn_report("Machine type '%s' is deprecated: %s",
3584 machine_class->name, machine_class->deprecation_reason);
3588 * Note: creates a QOM object, must run only after global and
3589 * compat properties have been set up.
3591 migration_object_init();
3593 qemu_create_late_backends();
3595 /* parse features once if machine provides default cpu_type */
3596 current_machine->cpu_type = machine_class->default_cpu_type;
3597 if (cpu_option) {
3598 current_machine->cpu_type = parse_cpu_option(cpu_option);
3600 /* NB: for machine none cpu_type could STILL be NULL here! */
3602 qemu_resolve_machine_memdev();
3603 parse_numa_opts(current_machine);
3605 if (vmstate_dump_file) {
3606 /* dump and exit */
3607 module_load_qom_all();
3608 dump_vmstate_json_to_file(vmstate_dump_file);
3609 exit(0);
3612 if (!preconfig_requested) {
3613 qmp_x_exit_preconfig(&error_fatal);
3615 qemu_init_displays();
3616 accel_setup_post(current_machine);
3617 os_setup_post();
3618 resume_mux_open();