acpi: ipmi: use AcpiDevAmlIf interface to build IPMI device descriptors
[qemu.git] / softmmu / vl.c
blob4c1e94b00eaaa7c8d1220464f2df848895e44f24
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 "sysemu/watchdog.h"
57 #include "hw/firmware/smbios.h"
58 #include "hw/acpi/acpi.h"
59 #include "hw/xen/xen.h"
60 #include "hw/loader.h"
61 #include "monitor/qdev.h"
62 #include "net/net.h"
63 #include "net/slirp.h"
64 #include "monitor/monitor.h"
65 #include "ui/console.h"
66 #include "ui/input.h"
67 #include "sysemu/sysemu.h"
68 #include "sysemu/numa.h"
69 #include "sysemu/hostmem.h"
70 #include "exec/gdbstub.h"
71 #include "qemu/timer.h"
72 #include "chardev/char.h"
73 #include "qemu/bitmap.h"
74 #include "qemu/log.h"
75 #include "sysemu/blockdev.h"
76 #include "hw/block/block.h"
77 #include "hw/i386/x86.h"
78 #include "hw/i386/pc.h"
79 #include "migration/misc.h"
80 #include "migration/snapshot.h"
81 #include "sysemu/tpm.h"
82 #include "sysemu/dma.h"
83 #include "hw/audio/soundhw.h"
84 #include "audio/audio.h"
85 #include "sysemu/cpus.h"
86 #include "sysemu/cpu-timers.h"
87 #include "migration/colo.h"
88 #include "migration/postcopy-ram.h"
89 #include "sysemu/kvm.h"
90 #include "sysemu/hax.h"
91 #include "qapi/qobject-input-visitor.h"
92 #include "qemu/option.h"
93 #include "qemu/config-file.h"
94 #include "qemu/qemu-options.h"
95 #include "qemu/main-loop.h"
96 #include "hw/cxl/cxl.h"
97 #ifdef CONFIG_VIRTFS
98 #include "fsdev/qemu-fsdev.h"
99 #endif
100 #include "sysemu/qtest.h"
102 #include "disas/disas.h"
104 #include "trace.h"
105 #include "trace/control.h"
106 #include "qemu/plugin.h"
107 #include "qemu/queue.h"
108 #include "sysemu/arch_init.h"
109 #include "exec/confidential-guest-support.h"
111 #include "ui/qemu-spice.h"
112 #include "qapi/string-input-visitor.h"
113 #include "qapi/opts-visitor.h"
114 #include "qapi/clone-visitor.h"
115 #include "qom/object_interfaces.h"
116 #include "semihosting/semihost.h"
117 #include "crypto/init.h"
118 #include "sysemu/replay.h"
119 #include "qapi/qapi-events-run-state.h"
120 #include "qapi/qapi-types-audio.h"
121 #include "qapi/qapi-visit-audio.h"
122 #include "qapi/qapi-visit-block-core.h"
123 #include "qapi/qapi-visit-compat.h"
124 #include "qapi/qapi-visit-machine.h"
125 #include "qapi/qapi-visit-ui.h"
126 #include "qapi/qapi-commands-block-core.h"
127 #include "qapi/qapi-commands-migration.h"
128 #include "qapi/qapi-commands-misc.h"
129 #include "qapi/qapi-visit-qom.h"
130 #include "qapi/qapi-commands-ui.h"
131 #include "qapi/qmp/qdict.h"
132 #include "block/qdict.h"
133 #include "qapi/qmp/qerror.h"
134 #include "sysemu/iothread.h"
135 #include "qemu/guest-random.h"
136 #include "qemu/keyval.h"
138 #include "config-host.h"
140 #define MAX_VIRTIO_CONSOLES 1
142 typedef struct BlockdevOptionsQueueEntry {
143 BlockdevOptions *bdo;
144 Location loc;
145 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
146 } BlockdevOptionsQueueEntry;
148 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
150 typedef struct CXLFMWOptionQueueEntry {
151 CXLFixedMemoryWindowOptions *opts;
152 Location loc;
153 QSIMPLEQ_ENTRY(CXLFMWOptionQueueEntry) entry;
154 } CXLFMWOptionQueueEntry;
156 typedef struct ObjectOption {
157 ObjectOptions *opts;
158 QTAILQ_ENTRY(ObjectOption) next;
159 } ObjectOption;
161 typedef struct DeviceOption {
162 QDict *opts;
163 Location loc;
164 QTAILQ_ENTRY(DeviceOption) next;
165 } DeviceOption;
167 static const char *cpu_option;
168 static const char *mem_path;
169 static const char *incoming;
170 static const char *loadvm;
171 static const char *accelerators;
172 static bool have_custom_ram_size;
173 static const char *ram_memdev_id;
174 static QDict *machine_opts_dict;
175 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
176 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
177 static int display_remote;
178 static int snapshot;
179 static bool preconfig_requested;
180 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
181 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
182 static QSIMPLEQ_HEAD(, CXLFMWOptionQueueEntry) CXLFMW_opts =
183 QSIMPLEQ_HEAD_INITIALIZER(CXLFMW_opts);
184 static bool nographic = false;
185 static int mem_prealloc; /* force preallocation of physical target memory */
186 static const char *vga_model = NULL;
187 static DisplayOptions dpy;
188 static int num_serial_hds;
189 static Chardev **serial_hds;
190 static const char *log_mask;
191 static const char *log_file;
192 static bool list_data_dirs;
193 static const char *watchdog;
194 static const char *qtest_chrdev;
195 static const char *qtest_log;
197 static int has_defaults = 1;
198 static int default_serial = 1;
199 static int default_parallel = 1;
200 static int default_monitor = 1;
201 static int default_floppy = 1;
202 static int default_cdrom = 1;
203 static int default_sdcard = 1;
204 static int default_vga = 1;
205 static int default_net = 1;
207 static struct {
208 const char *driver;
209 int *flag;
210 } default_list[] = {
211 { .driver = "isa-serial", .flag = &default_serial },
212 { .driver = "isa-parallel", .flag = &default_parallel },
213 { .driver = "isa-fdc", .flag = &default_floppy },
214 { .driver = "floppy", .flag = &default_floppy },
215 { .driver = "ide-cd", .flag = &default_cdrom },
216 { .driver = "ide-hd", .flag = &default_cdrom },
217 { .driver = "scsi-cd", .flag = &default_cdrom },
218 { .driver = "scsi-hd", .flag = &default_cdrom },
219 { .driver = "VGA", .flag = &default_vga },
220 { .driver = "isa-vga", .flag = &default_vga },
221 { .driver = "cirrus-vga", .flag = &default_vga },
222 { .driver = "isa-cirrus-vga", .flag = &default_vga },
223 { .driver = "vmware-svga", .flag = &default_vga },
224 { .driver = "qxl-vga", .flag = &default_vga },
225 { .driver = "virtio-vga", .flag = &default_vga },
226 { .driver = "ati-vga", .flag = &default_vga },
227 { .driver = "vhost-user-vga", .flag = &default_vga },
228 { .driver = "virtio-vga-gl", .flag = &default_vga },
231 static QemuOptsList qemu_rtc_opts = {
232 .name = "rtc",
233 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
234 .merge_lists = true,
235 .desc = {
237 .name = "base",
238 .type = QEMU_OPT_STRING,
240 .name = "clock",
241 .type = QEMU_OPT_STRING,
243 .name = "driftfix",
244 .type = QEMU_OPT_STRING,
246 { /* end of list */ }
250 static QemuOptsList qemu_option_rom_opts = {
251 .name = "option-rom",
252 .implied_opt_name = "romfile",
253 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
254 .desc = {
256 .name = "bootindex",
257 .type = QEMU_OPT_NUMBER,
258 }, {
259 .name = "romfile",
260 .type = QEMU_OPT_STRING,
262 { /* end of list */ }
266 static QemuOptsList qemu_accel_opts = {
267 .name = "accel",
268 .implied_opt_name = "accel",
269 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
270 .desc = {
272 * no elements => accept any
273 * sanity checking will happen later
274 * when setting accelerator properties
280 static QemuOptsList qemu_boot_opts = {
281 .name = "boot-opts",
282 .implied_opt_name = "order",
283 .merge_lists = true,
284 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
285 .desc = {
287 .name = "order",
288 .type = QEMU_OPT_STRING,
289 }, {
290 .name = "once",
291 .type = QEMU_OPT_STRING,
292 }, {
293 .name = "menu",
294 .type = QEMU_OPT_BOOL,
295 }, {
296 .name = "splash",
297 .type = QEMU_OPT_STRING,
298 }, {
299 .name = "splash-time",
300 .type = QEMU_OPT_NUMBER,
301 }, {
302 .name = "reboot-timeout",
303 .type = QEMU_OPT_NUMBER,
304 }, {
305 .name = "strict",
306 .type = QEMU_OPT_BOOL,
308 { /*End of list */ }
312 static QemuOptsList qemu_add_fd_opts = {
313 .name = "add-fd",
314 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
315 .desc = {
317 .name = "fd",
318 .type = QEMU_OPT_NUMBER,
319 .help = "file descriptor of which a duplicate is added to fd set",
321 .name = "set",
322 .type = QEMU_OPT_NUMBER,
323 .help = "ID of the fd set to add fd to",
325 .name = "opaque",
326 .type = QEMU_OPT_STRING,
327 .help = "free-form string used to describe fd",
329 { /* end of list */ }
333 static QemuOptsList qemu_object_opts = {
334 .name = "object",
335 .implied_opt_name = "qom-type",
336 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
337 .desc = {
342 static QemuOptsList qemu_tpmdev_opts = {
343 .name = "tpmdev",
344 .implied_opt_name = "type",
345 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
346 .desc = {
347 /* options are defined in the TPM backends */
348 { /* end of list */ }
352 static QemuOptsList qemu_overcommit_opts = {
353 .name = "overcommit",
354 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
355 .desc = {
357 .name = "mem-lock",
358 .type = QEMU_OPT_BOOL,
361 .name = "cpu-pm",
362 .type = QEMU_OPT_BOOL,
364 { /* end of list */ }
368 static QemuOptsList qemu_msg_opts = {
369 .name = "msg",
370 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
371 .desc = {
373 .name = "timestamp",
374 .type = QEMU_OPT_BOOL,
377 .name = "guest-name",
378 .type = QEMU_OPT_BOOL,
379 .help = "Prepends guest name for error messages but only if "
380 "-name guest is set otherwise option is ignored\n",
382 { /* end of list */ }
386 static QemuOptsList qemu_name_opts = {
387 .name = "name",
388 .implied_opt_name = "guest",
389 .merge_lists = true,
390 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
391 .desc = {
393 .name = "guest",
394 .type = QEMU_OPT_STRING,
395 .help = "Sets the name of the guest.\n"
396 "This name will be displayed in the SDL window caption.\n"
397 "The name will also be used for the VNC server",
398 }, {
399 .name = "process",
400 .type = QEMU_OPT_STRING,
401 .help = "Sets the name of the QEMU process, as shown in top etc",
402 }, {
403 .name = "debug-threads",
404 .type = QEMU_OPT_BOOL,
405 .help = "When enabled, name the individual threads; defaults off.\n"
406 "NOTE: The thread names are for debugging and not a\n"
407 "stable API.",
409 { /* End of list */ }
413 static QemuOptsList qemu_mem_opts = {
414 .name = "memory",
415 .implied_opt_name = "size",
416 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
417 .merge_lists = true,
418 .desc = {
420 .name = "size",
421 .type = QEMU_OPT_SIZE,
424 .name = "slots",
425 .type = QEMU_OPT_NUMBER,
428 .name = "maxmem",
429 .type = QEMU_OPT_SIZE,
431 { /* end of list */ }
435 static QemuOptsList qemu_icount_opts = {
436 .name = "icount",
437 .implied_opt_name = "shift",
438 .merge_lists = true,
439 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
440 .desc = {
442 .name = "shift",
443 .type = QEMU_OPT_STRING,
444 }, {
445 .name = "align",
446 .type = QEMU_OPT_BOOL,
447 }, {
448 .name = "sleep",
449 .type = QEMU_OPT_BOOL,
450 }, {
451 .name = "rr",
452 .type = QEMU_OPT_STRING,
453 }, {
454 .name = "rrfile",
455 .type = QEMU_OPT_STRING,
456 }, {
457 .name = "rrsnapshot",
458 .type = QEMU_OPT_STRING,
460 { /* end of list */ }
464 static QemuOptsList qemu_fw_cfg_opts = {
465 .name = "fw_cfg",
466 .implied_opt_name = "name",
467 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
468 .desc = {
470 .name = "name",
471 .type = QEMU_OPT_STRING,
472 .help = "Sets the fw_cfg name of the blob to be inserted",
473 }, {
474 .name = "file",
475 .type = QEMU_OPT_STRING,
476 .help = "Sets the name of the file from which "
477 "the fw_cfg blob will be loaded",
478 }, {
479 .name = "string",
480 .type = QEMU_OPT_STRING,
481 .help = "Sets content of the blob to be inserted from a string",
482 }, {
483 .name = "gen_id",
484 .type = QEMU_OPT_STRING,
485 .help = "Sets id of the object generating the fw_cfg blob "
486 "to be inserted",
488 { /* end of list */ }
492 static QemuOptsList qemu_action_opts = {
493 .name = "action",
494 .merge_lists = true,
495 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
496 .desc = {
498 .name = "shutdown",
499 .type = QEMU_OPT_STRING,
501 .name = "reboot",
502 .type = QEMU_OPT_STRING,
504 .name = "panic",
505 .type = QEMU_OPT_STRING,
507 .name = "watchdog",
508 .type = QEMU_OPT_STRING,
510 { /* end of list */ }
514 const char *qemu_get_vm_name(void)
516 return qemu_name;
519 static void default_driver_disable(const char *driver)
521 int i;
523 if (!driver) {
524 return;
527 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
528 if (strcmp(default_list[i].driver, driver) != 0)
529 continue;
530 *(default_list[i].flag) = 0;
534 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
536 const char *driver = qemu_opt_get(opts, "driver");
538 default_driver_disable(driver);
539 return 0;
542 static void default_driver_check_json(void)
544 DeviceOption *opt;
546 QTAILQ_FOREACH(opt, &device_opts, next) {
547 const char *driver = qdict_get_try_str(opt->opts, "driver");
548 default_driver_disable(driver);
552 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
554 const char *proc_name;
556 if (qemu_opt_get(opts, "debug-threads")) {
557 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
559 qemu_name = qemu_opt_get(opts, "guest");
561 proc_name = qemu_opt_get(opts, "process");
562 if (proc_name) {
563 os_set_proc_name(proc_name);
566 return 0;
569 bool defaults_enabled(void)
571 return has_defaults;
574 #ifndef _WIN32
575 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
577 int fd, dupfd, flags;
578 int64_t fdset_id;
579 const char *fd_opaque = NULL;
580 AddfdInfo *fdinfo;
582 fd = qemu_opt_get_number(opts, "fd", -1);
583 fdset_id = qemu_opt_get_number(opts, "set", -1);
584 fd_opaque = qemu_opt_get(opts, "opaque");
586 if (fd < 0) {
587 error_setg(errp, "fd option is required and must be non-negative");
588 return -1;
591 if (fd <= STDERR_FILENO) {
592 error_setg(errp, "fd cannot be a standard I/O stream");
593 return -1;
597 * All fds inherited across exec() necessarily have FD_CLOEXEC
598 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
600 flags = fcntl(fd, F_GETFD);
601 if (flags == -1 || (flags & FD_CLOEXEC)) {
602 error_setg(errp, "fd is not valid or already in use");
603 return -1;
606 if (fdset_id < 0) {
607 error_setg(errp, "set option is required and must be non-negative");
608 return -1;
611 #ifdef F_DUPFD_CLOEXEC
612 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
613 #else
614 dupfd = dup(fd);
615 if (dupfd != -1) {
616 qemu_set_cloexec(dupfd);
618 #endif
619 if (dupfd == -1) {
620 error_setg(errp, "error duplicating fd: %s", strerror(errno));
621 return -1;
624 /* add the duplicate fd, and optionally the opaque string, to the fd set */
625 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
626 &error_abort);
627 g_free(fdinfo);
629 return 0;
632 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
634 int fd;
636 fd = qemu_opt_get_number(opts, "fd", -1);
637 close(fd);
639 return 0;
641 #endif
643 /***********************************************************/
644 /* QEMU Block devices */
646 #define HD_OPTS "media=disk"
647 #define CDROM_OPTS "media=cdrom"
648 #define FD_OPTS ""
649 #define PFLASH_OPTS ""
650 #define MTD_OPTS ""
651 #define SD_OPTS ""
653 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
655 BlockInterfaceType *block_default_type = opaque;
657 return drive_new(opts, *block_default_type, errp) == NULL;
660 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
662 if (qemu_opt_get(opts, "snapshot") == NULL) {
663 qemu_opt_set(opts, "snapshot", "on", &error_abort);
665 return 0;
668 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
669 int index, const char *optstr)
671 QemuOpts *opts;
672 DriveInfo *dinfo;
674 if (!enable || drive_get_by_index(type, index)) {
675 return;
678 opts = drive_add(type, index, NULL, optstr);
679 if (snapshot) {
680 drive_enable_snapshot(NULL, opts, NULL);
683 dinfo = drive_new(opts, type, &error_abort);
684 dinfo->is_default = true;
688 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
689 MachineClass *machine_class, int snapshot)
692 * If the currently selected machine wishes to override the
693 * units-per-bus property of its default HBA interface type, do so
694 * now.
696 if (machine_class->units_per_default_bus) {
697 override_max_devs(machine_class->block_default_type,
698 machine_class->units_per_default_bus);
701 /* open the virtual block devices */
702 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
703 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
705 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
706 loc_push_restore(&bdo->loc);
707 qmp_blockdev_add(bdo->bdo, &error_fatal);
708 loc_pop(&bdo->loc);
709 qapi_free_BlockdevOptions(bdo->bdo);
710 g_free(bdo);
712 if (snapshot) {
713 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
714 NULL, NULL);
716 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
717 &machine_class->block_default_type, &error_fatal)) {
718 /* We printed help */
719 exit(0);
722 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
723 CDROM_OPTS);
724 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
725 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
729 static QemuOptsList qemu_smp_opts = {
730 .name = "smp-opts",
731 .implied_opt_name = "cpus",
732 .merge_lists = true,
733 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
734 .desc = {
736 .name = "cpus",
737 .type = QEMU_OPT_NUMBER,
738 }, {
739 .name = "sockets",
740 .type = QEMU_OPT_NUMBER,
741 }, {
742 .name = "dies",
743 .type = QEMU_OPT_NUMBER,
744 }, {
745 .name = "clusters",
746 .type = QEMU_OPT_NUMBER,
747 }, {
748 .name = "cores",
749 .type = QEMU_OPT_NUMBER,
750 }, {
751 .name = "threads",
752 .type = QEMU_OPT_NUMBER,
753 }, {
754 .name = "maxcpus",
755 .type = QEMU_OPT_NUMBER,
757 { /*End of list */ }
761 static void realtime_init(void)
763 if (enable_mlock) {
764 if (os_mlock() < 0) {
765 error_report("locking memory failed");
766 exit(1);
772 static void configure_msg(QemuOpts *opts)
774 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
775 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
779 /***********************************************************/
780 /* USB devices */
782 static int usb_device_add(const char *devname)
784 USBDevice *dev = NULL;
786 if (!machine_usb(current_machine)) {
787 return -1;
790 dev = usbdevice_create(devname);
791 if (!dev)
792 return -1;
794 return 0;
797 static int usb_parse(const char *cmdline)
799 int r;
800 r = usb_device_add(cmdline);
801 if (r < 0) {
802 error_report("could not add USB device '%s'", cmdline);
804 return r;
807 /***********************************************************/
808 /* machine registration */
810 static MachineClass *find_machine(const char *name, GSList *machines)
812 GSList *el;
814 for (el = machines; el; el = el->next) {
815 MachineClass *mc = el->data;
817 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
818 return mc;
822 return NULL;
825 static MachineClass *find_default_machine(GSList *machines)
827 GSList *el;
828 MachineClass *default_machineclass = NULL;
830 for (el = machines; el; el = el->next) {
831 MachineClass *mc = el->data;
833 if (mc->is_default) {
834 assert(default_machineclass == NULL && "Multiple default machines");
835 default_machineclass = mc;
839 return default_machineclass;
842 static void version(void)
844 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
845 QEMU_COPYRIGHT "\n");
848 static void help(int exitcode)
850 version();
851 printf("usage: %s [options] [disk_image]\n\n"
852 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
853 g_get_prgname());
855 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
856 if ((arch_mask) & arch_type) \
857 fputs(opt_help, stdout);
859 #define ARCHHEADING(text, arch_mask) \
860 if ((arch_mask) & arch_type) \
861 puts(stringify(text));
863 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
865 #include "qemu-options.def"
867 printf("\nDuring emulation, the following keys are useful:\n"
868 "ctrl-alt-f toggle full screen\n"
869 "ctrl-alt-n switch to virtual console 'n'\n"
870 "ctrl-alt toggle mouse and keyboard grab\n"
871 "\n"
872 "When using -nographic, press 'ctrl-a h' to get some help.\n"
873 "\n"
874 QEMU_HELP_BOTTOM "\n");
876 exit(exitcode);
879 #define HAS_ARG 0x0001
881 typedef struct QEMUOption {
882 const char *name;
883 int flags;
884 int index;
885 uint32_t arch_mask;
886 } QEMUOption;
888 static const QEMUOption qemu_options[] = {
889 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
891 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
892 { option, opt_arg, opt_enum, arch_mask },
893 #define DEFHEADING(text)
894 #define ARCHHEADING(text, arch_mask)
896 #include "qemu-options.def"
897 { NULL },
900 typedef struct VGAInterfaceInfo {
901 const char *opt_name; /* option name */
902 const char *name; /* human-readable name */
903 /* Class names indicating that support is available.
904 * If no class is specified, the interface is always available */
905 const char *class_names[2];
906 } VGAInterfaceInfo;
908 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
909 [VGA_NONE] = {
910 .opt_name = "none",
911 .name = "no graphic card",
913 [VGA_STD] = {
914 .opt_name = "std",
915 .name = "standard VGA",
916 .class_names = { "VGA", "isa-vga" },
918 [VGA_CIRRUS] = {
919 .opt_name = "cirrus",
920 .name = "Cirrus VGA",
921 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
923 [VGA_VMWARE] = {
924 .opt_name = "vmware",
925 .name = "VMWare SVGA",
926 .class_names = { "vmware-svga" },
928 [VGA_VIRTIO] = {
929 .opt_name = "virtio",
930 .name = "Virtio VGA",
931 .class_names = { "virtio-vga" },
933 [VGA_QXL] = {
934 .opt_name = "qxl",
935 .name = "QXL VGA",
936 .class_names = { "qxl-vga" },
938 [VGA_TCX] = {
939 .opt_name = "tcx",
940 .name = "TCX framebuffer",
941 .class_names = { "sun-tcx" },
943 [VGA_CG3] = {
944 .opt_name = "cg3",
945 .name = "CG3 framebuffer",
946 .class_names = { "cgthree" },
948 #ifdef CONFIG_XEN_BACKEND
949 [VGA_XENFB] = {
950 .opt_name = "xenfb",
951 .name = "Xen paravirtualized framebuffer",
953 #endif
956 static bool vga_interface_available(VGAInterfaceType t)
958 const VGAInterfaceInfo *ti = &vga_interfaces[t];
960 assert(t < VGA_TYPE_MAX);
961 return !ti->class_names[0] ||
962 module_object_class_by_name(ti->class_names[0]) ||
963 module_object_class_by_name(ti->class_names[1]);
966 static const char *
967 get_default_vga_model(const MachineClass *machine_class)
969 if (machine_class->default_display) {
970 return machine_class->default_display;
971 } else if (vga_interface_available(VGA_CIRRUS)) {
972 return "cirrus";
973 } else if (vga_interface_available(VGA_STD)) {
974 return "std";
977 return NULL;
980 static void select_vgahw(const MachineClass *machine_class, const char *p)
982 const char *opts;
983 int t;
985 if (g_str_equal(p, "help")) {
986 const char *def = get_default_vga_model(machine_class);
988 for (t = 0; t < VGA_TYPE_MAX; t++) {
989 const VGAInterfaceInfo *ti = &vga_interfaces[t];
991 if (vga_interface_available(t) && ti->opt_name) {
992 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
993 g_str_equal(ti->opt_name, def) ? " (default)" : "");
996 exit(0);
999 assert(vga_interface_type == VGA_NONE);
1000 for (t = 0; t < VGA_TYPE_MAX; t++) {
1001 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1002 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1003 if (!vga_interface_available(t)) {
1004 error_report("%s not available", ti->name);
1005 exit(1);
1007 vga_interface_type = t;
1008 break;
1011 if (t == VGA_TYPE_MAX) {
1012 invalid_vga:
1013 error_report("unknown vga type: %s", p);
1014 exit(1);
1016 while (*opts) {
1017 const char *nextopt;
1019 if (strstart(opts, ",retrace=", &nextopt)) {
1020 opts = nextopt;
1021 if (strstart(opts, "dumb", &nextopt))
1022 vga_retrace_method = VGA_RETRACE_DUMB;
1023 else if (strstart(opts, "precise", &nextopt))
1024 vga_retrace_method = VGA_RETRACE_PRECISE;
1025 else goto invalid_vga;
1026 } else goto invalid_vga;
1027 opts = nextopt;
1031 static void parse_display_qapi(const char *optarg)
1033 DisplayOptions *opts;
1034 Visitor *v;
1036 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1038 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1039 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1041 qapi_free_DisplayOptions(opts);
1042 visit_free(v);
1045 DisplayOptions *qmp_query_display_options(Error **errp)
1047 return QAPI_CLONE(DisplayOptions, &dpy);
1050 static void parse_display(const char *p)
1052 const char *opts;
1054 if (is_help_option(p)) {
1055 qemu_display_help();
1056 exit(0);
1059 if (strstart(p, "vnc", &opts)) {
1061 * vnc isn't a (local) DisplayType but a protocol for remote
1062 * display access.
1064 if (*opts == '=') {
1065 vnc_parse(opts + 1);
1066 } else {
1067 error_report("VNC requires a display argument vnc=<display>");
1068 exit(1);
1070 } else {
1071 parse_display_qapi(p);
1075 static void parse_cxl_fixed_memory_window(const char *optarg)
1077 CXLFMWOptionQueueEntry *cfmws_entry;
1078 Visitor *v;
1080 v = qobject_input_visitor_new_str(optarg, "cxl-fixed-memory-window",
1081 &error_fatal);
1082 cfmws_entry = g_new(CXLFMWOptionQueueEntry, 1);
1083 visit_type_CXLFixedMemoryWindowOptions(v, NULL, &cfmws_entry->opts,
1084 &error_fatal);
1085 if (!cfmws_entry->opts) {
1086 exit(1);
1088 visit_free(v);
1089 loc_save(&cfmws_entry->loc);
1090 QSIMPLEQ_INSERT_TAIL(&CXLFMW_opts, cfmws_entry, entry);
1093 static inline bool nonempty_str(const char *str)
1095 return str && *str;
1098 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1100 gchar *buf;
1101 size_t size;
1102 const char *name, *file, *str, *gen_id;
1103 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1105 if (fw_cfg == NULL) {
1106 error_setg(errp, "fw_cfg device not available");
1107 return -1;
1109 name = qemu_opt_get(opts, "name");
1110 file = qemu_opt_get(opts, "file");
1111 str = qemu_opt_get(opts, "string");
1112 gen_id = qemu_opt_get(opts, "gen_id");
1114 /* we need the name, and exactly one of: file, content string, gen_id */
1115 if (!nonempty_str(name) ||
1116 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1117 error_setg(errp, "name, plus exactly one of file,"
1118 " string and gen_id, are needed");
1119 return -1;
1121 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1122 error_setg(errp, "name too long (max. %d char)",
1123 FW_CFG_MAX_FILE_PATH - 1);
1124 return -1;
1126 if (nonempty_str(gen_id)) {
1128 * In this particular case where the content is populated
1129 * internally, the "etc/" namespace protection is relaxed,
1130 * so do not emit a warning.
1132 } else if (strncmp(name, "opt/", 4) != 0) {
1133 warn_report("externally provided fw_cfg item names "
1134 "should be prefixed with \"opt/\"");
1136 if (nonempty_str(str)) {
1137 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1138 buf = g_memdup(str, size);
1139 } else if (nonempty_str(gen_id)) {
1140 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1141 return -1;
1143 return 0;
1144 } else {
1145 GError *err = NULL;
1146 if (!g_file_get_contents(file, &buf, &size, &err)) {
1147 error_setg(errp, "can't load %s: %s", file, err->message);
1148 g_error_free(err);
1149 return -1;
1152 /* For legacy, keep user files in a specific global order. */
1153 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1154 fw_cfg_add_file(fw_cfg, name, buf, size);
1155 fw_cfg_reset_order_override(fw_cfg);
1156 return 0;
1159 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1161 return qdev_device_help(opts);
1164 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1166 DeviceState *dev;
1168 dev = qdev_device_add(opts, errp);
1169 if (!dev && *errp) {
1170 error_report_err(*errp);
1171 return -1;
1172 } else if (dev) {
1173 object_unref(OBJECT(dev));
1175 return 0;
1178 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1180 Error *local_err = NULL;
1182 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1183 if (local_err) {
1184 error_propagate(errp, local_err);
1185 return -1;
1187 exit(0);
1189 return 0;
1192 #ifdef CONFIG_VIRTFS
1193 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1195 return qemu_fsdev_add(opts, errp);
1197 #endif
1199 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1201 return monitor_init_opts(opts, errp);
1204 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1206 static int monitor_device_index = 0;
1207 QemuOpts *opts;
1208 const char *p;
1209 char label[32];
1211 if (strstart(optarg, "chardev:", &p)) {
1212 snprintf(label, sizeof(label), "%s", p);
1213 } else {
1214 snprintf(label, sizeof(label), "compat_monitor%d",
1215 monitor_device_index);
1216 opts = qemu_chr_parse_compat(label, optarg, true);
1217 if (!opts) {
1218 error_report("parse error: %s", optarg);
1219 exit(1);
1223 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1224 qemu_opt_set(opts, "mode", mode, &error_abort);
1225 qemu_opt_set(opts, "chardev", label, &error_abort);
1226 if (!strcmp(mode, "control")) {
1227 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1228 } else {
1229 assert(pretty == false);
1231 monitor_device_index++;
1234 struct device_config {
1235 enum {
1236 DEV_USB, /* -usbdevice */
1237 DEV_SERIAL, /* -serial */
1238 DEV_PARALLEL, /* -parallel */
1239 DEV_DEBUGCON, /* -debugcon */
1240 DEV_GDB, /* -gdb, -s */
1241 DEV_SCLP, /* s390 sclp */
1242 } type;
1243 const char *cmdline;
1244 Location loc;
1245 QTAILQ_ENTRY(device_config) next;
1248 static QTAILQ_HEAD(, device_config) device_configs =
1249 QTAILQ_HEAD_INITIALIZER(device_configs);
1251 static void add_device_config(int type, const char *cmdline)
1253 struct device_config *conf;
1255 conf = g_malloc0(sizeof(*conf));
1256 conf->type = type;
1257 conf->cmdline = cmdline;
1258 loc_save(&conf->loc);
1259 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1262 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1264 struct device_config *conf;
1265 int rc;
1267 QTAILQ_FOREACH(conf, &device_configs, next) {
1268 if (conf->type != type)
1269 continue;
1270 loc_push_restore(&conf->loc);
1271 rc = func(conf->cmdline);
1272 loc_pop(&conf->loc);
1273 if (rc) {
1274 return rc;
1277 return 0;
1280 static void qemu_disable_default_devices(void)
1282 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1284 default_driver_check_json();
1285 qemu_opts_foreach(qemu_find_opts("device"),
1286 default_driver_check, NULL, NULL);
1287 qemu_opts_foreach(qemu_find_opts("global"),
1288 default_driver_check, NULL, NULL);
1290 if (!vga_model && !default_vga) {
1291 vga_interface_type = VGA_DEVICE;
1292 vga_interface_created = true;
1294 if (!has_defaults || machine_class->no_serial) {
1295 default_serial = 0;
1297 if (!has_defaults || machine_class->no_parallel) {
1298 default_parallel = 0;
1300 if (!has_defaults || machine_class->no_floppy) {
1301 default_floppy = 0;
1303 if (!has_defaults || machine_class->no_cdrom) {
1304 default_cdrom = 0;
1306 if (!has_defaults || machine_class->no_sdcard) {
1307 default_sdcard = 0;
1309 if (!has_defaults) {
1310 default_monitor = 0;
1311 default_net = 0;
1312 default_vga = 0;
1316 static void qemu_create_default_devices(void)
1318 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1320 if (is_daemonized()) {
1321 /* According to documentation and historically, -nographic redirects
1322 * serial port, parallel port and monitor to stdio, which does not work
1323 * with -daemonize. We can redirect these to null instead, but since
1324 * -nographic is legacy, let's just error out.
1325 * We disallow -nographic only if all other ports are not redirected
1326 * explicitly, to not break existing legacy setups which uses
1327 * -nographic _and_ redirects all ports explicitly - this is valid
1328 * usage, -nographic is just a no-op in this case.
1330 if (nographic
1331 && (default_parallel || default_serial || default_monitor)) {
1332 error_report("-nographic cannot be used with -daemonize");
1333 exit(1);
1337 if (nographic) {
1338 if (default_parallel)
1339 add_device_config(DEV_PARALLEL, "null");
1340 if (default_serial && default_monitor) {
1341 add_device_config(DEV_SERIAL, "mon:stdio");
1342 } else {
1343 if (default_serial)
1344 add_device_config(DEV_SERIAL, "stdio");
1345 if (default_monitor)
1346 monitor_parse("stdio", "readline", false);
1348 } else {
1349 if (default_serial)
1350 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1351 if (default_parallel)
1352 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1353 if (default_monitor)
1354 monitor_parse("vc:80Cx24C", "readline", false);
1357 if (default_net) {
1358 QemuOptsList *net = qemu_find_opts("net");
1359 qemu_opts_parse(net, "nic", true, &error_abort);
1360 #ifdef CONFIG_SLIRP
1361 qemu_opts_parse(net, "user", true, &error_abort);
1362 #endif
1365 #if defined(CONFIG_VNC)
1366 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1367 display_remote++;
1369 #endif
1370 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1371 if (!qemu_display_find_default(&dpy)) {
1372 dpy.type = DISPLAY_TYPE_NONE;
1373 #if defined(CONFIG_VNC)
1374 vnc_parse("localhost:0,to=99,id=default");
1375 #endif
1378 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1379 dpy.type = DISPLAY_TYPE_NONE;
1382 /* If no default VGA is requested, the default is "none". */
1383 if (default_vga) {
1384 vga_model = get_default_vga_model(machine_class);
1386 if (vga_model) {
1387 select_vgahw(machine_class, vga_model);
1391 static int serial_parse(const char *devname)
1393 int index = num_serial_hds;
1394 char label[32];
1396 if (strcmp(devname, "none") == 0)
1397 return 0;
1398 snprintf(label, sizeof(label), "serial%d", index);
1399 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1401 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1402 if (!serial_hds[index]) {
1403 error_report("could not connect serial device"
1404 " to character backend '%s'", devname);
1405 return -1;
1407 num_serial_hds++;
1408 return 0;
1411 Chardev *serial_hd(int i)
1413 assert(i >= 0);
1414 if (i < num_serial_hds) {
1415 return serial_hds[i];
1417 return NULL;
1420 static int parallel_parse(const char *devname)
1422 static int index = 0;
1423 char label[32];
1425 if (strcmp(devname, "none") == 0)
1426 return 0;
1427 if (index == MAX_PARALLEL_PORTS) {
1428 error_report("too many parallel ports");
1429 exit(1);
1431 snprintf(label, sizeof(label), "parallel%d", index);
1432 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1433 if (!parallel_hds[index]) {
1434 error_report("could not connect parallel device"
1435 " to character backend '%s'", devname);
1436 return -1;
1438 index++;
1439 return 0;
1442 static int debugcon_parse(const char *devname)
1444 QemuOpts *opts;
1446 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1447 error_report("invalid character backend '%s'", devname);
1448 exit(1);
1450 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1451 if (!opts) {
1452 error_report("already have a debugcon device");
1453 exit(1);
1455 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1456 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1457 return 0;
1460 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1462 const MachineClass *mc1 = a, *mc2 = b;
1463 int res;
1465 if (mc1->family == NULL) {
1466 if (mc2->family == NULL) {
1467 /* Compare standalone machine types against each other; they sort
1468 * in increasing order.
1470 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1471 object_class_get_name(OBJECT_CLASS(mc2)));
1474 /* Standalone machine types sort after families. */
1475 return 1;
1478 if (mc2->family == NULL) {
1479 /* Families sort before standalone machine types. */
1480 return -1;
1483 /* Families sort between each other alphabetically increasingly. */
1484 res = strcmp(mc1->family, mc2->family);
1485 if (res != 0) {
1486 return res;
1489 /* Within the same family, machine types sort in decreasing order. */
1490 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1491 object_class_get_name(OBJECT_CLASS(mc1)));
1494 static void machine_help_func(const QDict *qdict)
1496 GSList *machines, *el;
1497 const char *type = qdict_get_try_str(qdict, "type");
1499 machines = object_class_get_list(TYPE_MACHINE, false);
1500 if (type) {
1501 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1502 if (machine_class) {
1503 type_print_class_properties(object_class_get_name(machine_class));
1504 return;
1508 printf("Supported machines are:\n");
1509 machines = g_slist_sort(machines, machine_class_cmp);
1510 for (el = machines; el; el = el->next) {
1511 MachineClass *mc = el->data;
1512 if (mc->alias) {
1513 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1515 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1516 mc->is_default ? " (default)" : "",
1517 mc->deprecation_reason ? " (deprecated)" : "");
1521 static void
1522 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1524 QDict *opts;
1526 opts = qdict_new();
1527 /* Preserve the caller's reference to prop. */
1528 qobject_ref(prop);
1529 qdict_put(opts, propname, prop);
1530 keyval_merge(machine_opts_dict, opts, errp);
1531 qobject_unref(opts);
1534 static void
1535 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1536 const char *arg)
1538 QDict *prop = NULL;
1539 bool help = false;
1541 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
1542 if (help) {
1543 qemu_opts_print_help(opts_list, true);
1544 exit(0);
1546 machine_merge_property(propname, prop, &error_fatal);
1547 qobject_unref(prop);
1550 static const char *pid_file;
1551 static Notifier qemu_unlink_pidfile_notifier;
1553 static void qemu_unlink_pidfile(Notifier *n, void *data)
1555 if (pid_file) {
1556 unlink(pid_file);
1560 static const QEMUOption *lookup_opt(int argc, char **argv,
1561 const char **poptarg, int *poptind)
1563 const QEMUOption *popt;
1564 int optind = *poptind;
1565 char *r = argv[optind];
1566 const char *optarg;
1568 loc_set_cmdline(argv, optind, 1);
1569 optind++;
1570 /* Treat --foo the same as -foo. */
1571 if (r[1] == '-')
1572 r++;
1573 popt = qemu_options;
1574 for(;;) {
1575 if (!popt->name) {
1576 error_report("invalid option");
1577 exit(1);
1579 if (!strcmp(popt->name, r + 1))
1580 break;
1581 popt++;
1583 if (popt->flags & HAS_ARG) {
1584 if (optind >= argc) {
1585 error_report("requires an argument");
1586 exit(1);
1588 optarg = argv[optind++];
1589 loc_set_cmdline(argv, optind - 2, 2);
1590 } else {
1591 optarg = NULL;
1594 *poptarg = optarg;
1595 *poptind = optind;
1597 return popt;
1600 static MachineClass *select_machine(QDict *qdict, Error **errp)
1602 const char *optarg = qdict_get_try_str(qdict, "type");
1603 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1604 MachineClass *machine_class;
1605 Error *local_err = NULL;
1607 if (optarg) {
1608 machine_class = find_machine(optarg, machines);
1609 qdict_del(qdict, "type");
1610 if (!machine_class) {
1611 error_setg(&local_err, "unsupported machine type");
1613 } else {
1614 machine_class = find_default_machine(machines);
1615 if (!machine_class) {
1616 error_setg(&local_err, "No machine specified, and there is no default");
1620 g_slist_free(machines);
1621 if (local_err) {
1622 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1623 error_propagate(errp, local_err);
1625 return machine_class;
1628 static int object_parse_property_opt(Object *obj,
1629 const char *name, const char *value,
1630 const char *skip, Error **errp)
1632 if (g_str_equal(name, skip)) {
1633 return 0;
1636 if (!object_property_parse(obj, name, value, errp)) {
1637 return -1;
1640 return 0;
1643 /* *Non*recursively replace underscores with dashes in QDict keys. */
1644 static void keyval_dashify(QDict *qdict, Error **errp)
1646 const QDictEntry *ent, *next;
1647 char *p;
1649 for (ent = qdict_first(qdict); ent; ent = next) {
1650 g_autofree char *new_key = NULL;
1652 next = qdict_next(qdict, ent);
1653 if (!strchr(ent->key, '_')) {
1654 continue;
1656 new_key = g_strdup(ent->key);
1657 for (p = new_key; *p; p++) {
1658 if (*p == '_') {
1659 *p = '-';
1662 if (qdict_haskey(qdict, new_key)) {
1663 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1664 return;
1666 qobject_ref(ent->value);
1667 qdict_put_obj(qdict, new_key, ent->value);
1668 qdict_del(qdict, ent->key);
1672 static void qemu_apply_legacy_machine_options(QDict *qdict)
1674 const char *value;
1675 QObject *prop;
1677 keyval_dashify(qdict, &error_fatal);
1679 /* Legacy options do not correspond to MachineState properties. */
1680 value = qdict_get_try_str(qdict, "accel");
1681 if (value) {
1682 accelerators = g_strdup(value);
1683 qdict_del(qdict, "accel");
1686 value = qdict_get_try_str(qdict, "igd-passthru");
1687 if (value) {
1688 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1689 false);
1690 qdict_del(qdict, "igd-passthru");
1693 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1694 if (value) {
1695 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1696 false);
1697 qdict_del(qdict, "kvm-shadow-mem");
1700 value = qdict_get_try_str(qdict, "kernel-irqchip");
1701 if (value) {
1702 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1703 false);
1704 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1705 false);
1706 qdict_del(qdict, "kernel-irqchip");
1709 value = qdict_get_try_str(qdict, "memory-backend");
1710 if (value) {
1711 if (mem_path) {
1712 error_report("'-mem-path' can't be used together with"
1713 "'-machine memory-backend'");
1714 exit(EXIT_FAILURE);
1717 /* Resolved later. */
1718 ram_memdev_id = g_strdup(value);
1719 qdict_del(qdict, "memory-backend");
1722 prop = qdict_get(qdict, "memory");
1723 if (prop) {
1724 have_custom_ram_size =
1725 qobject_type(prop) == QTYPE_QDICT &&
1726 qdict_haskey(qobject_to(QDict, prop), "size");
1730 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1732 ObjectOption *opt, *next;
1734 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1735 const char *type = ObjectType_str(opt->opts->qom_type);
1736 if (type_opt_predicate(type)) {
1737 user_creatable_add_qapi(opt->opts, &error_fatal);
1738 qapi_free_ObjectOptions(opt->opts);
1739 QTAILQ_REMOVE(&object_opts, opt, next);
1740 g_free(opt);
1745 static void object_option_add_visitor(Visitor *v)
1747 ObjectOption *opt = g_new0(ObjectOption, 1);
1748 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1749 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1752 static void object_option_parse(const char *optarg)
1754 QemuOpts *opts;
1755 const char *type;
1756 Visitor *v;
1758 if (optarg[0] == '{') {
1759 QObject *obj = qobject_from_json(optarg, &error_fatal);
1761 v = qobject_input_visitor_new(obj);
1762 qobject_unref(obj);
1763 } else {
1764 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1765 optarg, true);
1766 if (!opts) {
1767 exit(1);
1770 type = qemu_opt_get(opts, "qom-type");
1771 if (!type) {
1772 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1774 if (user_creatable_print_help(type, opts)) {
1775 exit(0);
1778 v = opts_visitor_new(opts);
1781 object_option_add_visitor(v);
1782 visit_free(v);
1786 * Initial object creation happens before all other
1787 * QEMU data types are created. The majority of objects
1788 * can be created at this point. The rng-egd object
1789 * cannot be created here, as it depends on the chardev
1790 * already existing.
1792 static bool object_create_early(const char *type)
1795 * Objects should not be made "delayed" without a reason. If you
1796 * add one, state the reason in a comment!
1799 /* Reason: property "chardev" */
1800 if (g_str_equal(type, "rng-egd") ||
1801 g_str_equal(type, "qtest")) {
1802 return false;
1805 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1806 /* Reason: cryptodev-vhost-user property "chardev" */
1807 if (g_str_equal(type, "cryptodev-vhost-user")) {
1808 return false;
1810 #endif
1812 /* Reason: vhost-user-blk-server property "node-name" */
1813 if (g_str_equal(type, "vhost-user-blk-server")) {
1814 return false;
1817 * Reason: filter-* property "netdev" etc.
1819 if (g_str_equal(type, "filter-buffer") ||
1820 g_str_equal(type, "filter-dump") ||
1821 g_str_equal(type, "filter-mirror") ||
1822 g_str_equal(type, "filter-redirector") ||
1823 g_str_equal(type, "colo-compare") ||
1824 g_str_equal(type, "filter-rewriter") ||
1825 g_str_equal(type, "filter-replay")) {
1826 return false;
1830 * Allocation of large amounts of memory may delay
1831 * chardev initialization for too long, and trigger timeouts
1832 * on software that waits for a monitor socket to be created
1833 * (e.g. libvirt).
1835 if (g_str_has_prefix(type, "memory-backend-")) {
1836 return false;
1839 return true;
1842 static void qemu_apply_machine_options(QDict *qdict)
1844 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1846 if (semihosting_enabled() && !semihosting_get_argc()) {
1847 /* fall back to the -kernel/-append */
1848 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1851 if (current_machine->smp.cpus > 1) {
1852 Error *blocker = NULL;
1853 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1854 replay_add_blocker(blocker);
1858 static void qemu_create_early_backends(void)
1860 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1861 #if defined(CONFIG_SDL)
1862 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1863 #else
1864 const bool use_sdl = false;
1865 #endif
1866 #if defined(CONFIG_GTK)
1867 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1868 #else
1869 const bool use_gtk = false;
1870 #endif
1872 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1873 error_report("window-close is only valid for GTK and SDL, "
1874 "ignoring option");
1877 qemu_display_early_init(&dpy);
1878 qemu_console_early_init();
1880 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1881 #if defined(CONFIG_OPENGL)
1882 error_report("OpenGL is not supported by the display");
1883 #else
1884 error_report("OpenGL support is disabled");
1885 #endif
1886 exit(1);
1889 object_option_foreach_add(object_create_early);
1891 /* spice needs the timers to be initialized by this point */
1892 /* spice must initialize before audio as it changes the default audiodev */
1893 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1894 qemu_spice.init();
1896 qemu_opts_foreach(qemu_find_opts("chardev"),
1897 chardev_init_func, NULL, &error_fatal);
1899 #ifdef CONFIG_VIRTFS
1900 qemu_opts_foreach(qemu_find_opts("fsdev"),
1901 fsdev_init_func, NULL, &error_fatal);
1902 #endif
1905 * Note: we need to create audio and block backends before
1906 * setting machine properties, so they can be referred to.
1908 configure_blockdev(&bdo_queue, machine_class, snapshot);
1909 audio_init_audiodevs();
1914 * The remainder of object creation happens after the
1915 * creation of chardev, fsdev, net clients and device data types.
1917 static bool object_create_late(const char *type)
1919 return !object_create_early(type);
1922 static void qemu_create_late_backends(void)
1924 if (qtest_chrdev) {
1925 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1928 net_init_clients(&error_fatal);
1930 object_option_foreach_add(object_create_late);
1932 if (tpm_init() < 0) {
1933 exit(1);
1936 qemu_opts_foreach(qemu_find_opts("mon"),
1937 mon_init_func, NULL, &error_fatal);
1939 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1940 exit(1);
1941 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1942 exit(1);
1943 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1944 exit(1);
1946 /* now chardevs have been created we may have semihosting to connect */
1947 qemu_semihosting_connect_chardevs();
1948 qemu_semihosting_console_init();
1951 static void cxl_set_opts(void)
1953 while (!QSIMPLEQ_EMPTY(&CXLFMW_opts)) {
1954 CXLFMWOptionQueueEntry *cfmws_entry = QSIMPLEQ_FIRST(&CXLFMW_opts);
1956 loc_restore(&cfmws_entry->loc);
1957 QSIMPLEQ_REMOVE_HEAD(&CXLFMW_opts, entry);
1958 cxl_fixed_memory_window_config(current_machine, cfmws_entry->opts,
1959 &error_fatal);
1960 qapi_free_CXLFixedMemoryWindowOptions(cfmws_entry->opts);
1961 g_free(cfmws_entry);
1965 static void qemu_resolve_machine_memdev(void)
1967 if (ram_memdev_id) {
1968 Object *backend;
1969 ram_addr_t backend_size;
1971 backend = object_resolve_path_type(ram_memdev_id,
1972 TYPE_MEMORY_BACKEND, NULL);
1973 if (!backend) {
1974 error_report("Memory backend '%s' not found", ram_memdev_id);
1975 exit(EXIT_FAILURE);
1977 if (!have_custom_ram_size) {
1978 backend_size = object_property_get_uint(backend, "size", &error_abort);
1979 current_machine->ram_size = backend_size;
1981 object_property_set_link(OBJECT(current_machine),
1982 "memory-backend", backend, &error_fatal);
1986 static void parse_memory_options(const char *arg)
1988 QemuOpts *opts;
1989 QDict *dict, *prop;
1990 const char *mem_str;
1992 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"), arg, true);
1993 if (!opts) {
1994 exit(EXIT_FAILURE);
1997 prop = qdict_new();
1999 if (qemu_opt_get_size(opts, "size", 0) != 0) {
2000 mem_str = qemu_opt_get(opts, "size");
2001 if (!*mem_str) {
2002 error_report("missing 'size' option value");
2003 exit(EXIT_FAILURE);
2006 /* Fix up legacy suffix-less format */
2007 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2008 g_autofree char *mib_str = g_strdup_printf("%sM", mem_str);
2009 qdict_put_str(prop, "size", mib_str);
2010 } else {
2011 qdict_put_str(prop, "size", mem_str);
2015 if (qemu_opt_get(opts, "maxmem")) {
2016 qdict_put_str(prop, "max-size", qemu_opt_get(opts, "maxmem"));
2018 if (qemu_opt_get(opts, "slots")) {
2019 qdict_put_str(prop, "slots", qemu_opt_get(opts, "slots"));
2022 dict = qdict_new();
2023 qdict_put(dict, "memory", prop);
2024 keyval_merge(machine_opts_dict, dict, &error_fatal);
2025 qobject_unref(dict);
2028 static void qemu_create_machine(QDict *qdict)
2030 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2031 object_set_machine_compat_props(machine_class->compat_props);
2033 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2034 object_property_add_child(object_get_root(), "machine",
2035 OBJECT(current_machine));
2036 object_property_add_child(container_get(OBJECT(current_machine),
2037 "/unattached"),
2038 "sysbus", OBJECT(sysbus_get_default()));
2040 if (machine_class->minimum_page_bits) {
2041 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2042 /* This would be a board error: specifying a minimum smaller than
2043 * a target's compile-time fixed setting.
2045 g_assert_not_reached();
2049 cpu_exec_init_all();
2050 page_size_init();
2052 if (machine_class->hw_version) {
2053 qemu_set_hw_version(machine_class->hw_version);
2057 * Get the default machine options from the machine if it is not already
2058 * specified either by the configuration file or by the command line.
2060 if (machine_class->default_machine_opts) {
2061 QDict *default_opts =
2062 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2063 &error_abort);
2064 qemu_apply_legacy_machine_options(default_opts);
2065 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2066 false, &error_abort);
2067 qobject_unref(default_opts);
2071 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2073 GlobalProperty *g;
2075 g = g_malloc0(sizeof(*g));
2076 g->driver = qemu_opt_get(opts, "driver");
2077 g->property = qemu_opt_get(opts, "property");
2078 g->value = qemu_opt_get(opts, "value");
2079 qdev_prop_register_global(g);
2080 return 0;
2084 * Return whether configuration group @group is stored in QemuOpts, or
2085 * recorded as one or more QDicts by qemu_record_config_group.
2087 static bool is_qemuopts_group(const char *group)
2089 if (g_str_equal(group, "object") ||
2090 g_str_equal(group, "machine") ||
2091 g_str_equal(group, "smp-opts") ||
2092 g_str_equal(group, "boot-opts") ||
2093 g_str_equal(group, "memory")) {
2094 return false;
2096 return true;
2099 static void qemu_record_config_group(const char *group, QDict *dict,
2100 bool from_json, Error **errp)
2102 if (g_str_equal(group, "object")) {
2103 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2104 object_option_add_visitor(v);
2105 visit_free(v);
2106 } else if (g_str_equal(group, "machine")) {
2108 * Cannot merge string-valued and type-safe dictionaries, so JSON
2109 * is not accepted yet for -M.
2111 assert(!from_json);
2112 keyval_merge(machine_opts_dict, dict, errp);
2113 } else if (g_str_equal(group, "smp-opts")) {
2114 machine_merge_property("smp", dict, &error_fatal);
2115 } else if (g_str_equal(group, "boot-opts")) {
2116 machine_merge_property("boot", dict, &error_fatal);
2117 } else if (g_str_equal(group, "memory")) {
2118 machine_merge_property("memory", dict, &error_fatal);
2119 } else {
2120 abort();
2125 * Parse non-QemuOpts config file groups, pass the rest to
2126 * qemu_config_do_parse.
2128 static void qemu_parse_config_group(const char *group, QDict *qdict,
2129 void *opaque, Error **errp)
2131 QObject *crumpled;
2132 if (is_qemuopts_group(group)) {
2133 qemu_config_do_parse(group, qdict, opaque, errp);
2134 return;
2137 crumpled = qdict_crumple(qdict, errp);
2138 if (!crumpled) {
2139 return;
2141 switch (qobject_type(crumpled)) {
2142 case QTYPE_QDICT:
2143 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2144 break;
2145 case QTYPE_QLIST:
2146 error_setg(errp, "Lists cannot be at top level of a configuration section");
2147 break;
2148 default:
2149 g_assert_not_reached();
2151 qobject_unref(crumpled);
2154 static void qemu_read_default_config_file(Error **errp)
2156 ERRP_GUARD();
2157 int ret;
2158 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2160 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2161 if (ret < 0) {
2162 if (ret == -ENOENT) {
2163 error_free(*errp);
2164 *errp = NULL;
2169 static void qemu_set_option(const char *str, Error **errp)
2171 char group[64], id[64], arg[64];
2172 QemuOptsList *list;
2173 QemuOpts *opts;
2174 int rc, offset;
2176 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2177 if (rc < 3 || str[offset] != '=') {
2178 error_setg(errp, "can't parse: \"%s\"", str);
2179 return;
2182 if (!is_qemuopts_group(group)) {
2183 error_setg(errp, "-set is not supported with %s", group);
2184 } else {
2185 list = qemu_find_opts_err(group, errp);
2186 if (list) {
2187 opts = qemu_opts_find(list, id);
2188 if (!opts) {
2189 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2190 return;
2192 qemu_opt_set(opts, arg, str + offset + 1, errp);
2197 static void user_register_global_props(void)
2199 qemu_opts_foreach(qemu_find_opts("global"),
2200 global_init_func, NULL, NULL);
2203 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2205 icount_configure(opts, errp);
2206 return 0;
2209 static int accelerator_set_property(void *opaque,
2210 const char *name, const char *value,
2211 Error **errp)
2213 return object_parse_property_opt(opaque, name, value, "accel", errp);
2216 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2218 bool *p_init_failed = opaque;
2219 const char *acc = qemu_opt_get(opts, "accel");
2220 AccelClass *ac = accel_find(acc);
2221 AccelState *accel;
2222 int ret;
2223 bool qtest_with_kvm;
2225 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2227 if (!ac) {
2228 *p_init_failed = true;
2229 if (!qtest_with_kvm) {
2230 error_report("invalid accelerator %s", acc);
2232 return 0;
2234 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2235 object_apply_compat_props(OBJECT(accel));
2236 qemu_opt_foreach(opts, accelerator_set_property,
2237 accel,
2238 &error_fatal);
2240 ret = accel_init_machine(accel, current_machine);
2241 if (ret < 0) {
2242 *p_init_failed = true;
2243 if (!qtest_with_kvm || ret != -ENOENT) {
2244 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2246 return 0;
2249 return 1;
2252 static void configure_accelerators(const char *progname)
2254 bool init_failed = false;
2256 qemu_opts_foreach(qemu_find_opts("icount"),
2257 do_configure_icount, NULL, &error_fatal);
2259 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2260 char **accel_list, **tmp;
2262 if (accelerators == NULL) {
2263 /* Select the default accelerator */
2264 bool have_tcg = accel_find("tcg");
2265 bool have_kvm = accel_find("kvm");
2267 if (have_tcg && have_kvm) {
2268 if (g_str_has_suffix(progname, "kvm")) {
2269 /* If the program name ends with "kvm", we prefer KVM */
2270 accelerators = "kvm:tcg";
2271 } else {
2272 accelerators = "tcg:kvm";
2274 } else if (have_kvm) {
2275 accelerators = "kvm";
2276 } else if (have_tcg) {
2277 accelerators = "tcg";
2278 } else {
2279 error_report("No accelerator selected and"
2280 " no default accelerator available");
2281 exit(1);
2284 accel_list = g_strsplit(accelerators, ":", 0);
2286 for (tmp = accel_list; *tmp; tmp++) {
2288 * Filter invalid accelerators here, to prevent obscenities
2289 * such as "-machine accel=tcg,,thread=single".
2291 if (accel_find(*tmp)) {
2292 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2293 } else {
2294 init_failed = true;
2295 error_report("invalid accelerator %s", *tmp);
2298 g_strfreev(accel_list);
2299 } else {
2300 if (accelerators != NULL) {
2301 error_report("The -accel and \"-machine accel=\" options are incompatible");
2302 exit(1);
2306 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2307 do_configure_accelerator, &init_failed, &error_fatal)) {
2308 if (!init_failed) {
2309 error_report("no accelerator found");
2311 exit(1);
2314 if (init_failed && !qtest_chrdev) {
2315 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2316 error_report("falling back to %s", ac->name);
2319 if (icount_enabled() && !tcg_enabled()) {
2320 error_report("-icount is not allowed with hardware virtualization");
2321 exit(1);
2325 static void qemu_validate_options(const QDict *machine_opts)
2327 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2328 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2329 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2331 if (kernel_filename == NULL) {
2332 if (kernel_cmdline != NULL) {
2333 error_report("-append only allowed with -kernel option");
2334 exit(1);
2337 if (initrd_filename != NULL) {
2338 error_report("-initrd only allowed with -kernel option");
2339 exit(1);
2343 if (loadvm && preconfig_requested) {
2344 error_report("'preconfig' and 'loadvm' options are "
2345 "mutually exclusive");
2346 exit(EXIT_FAILURE);
2348 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2349 error_report("'preconfig' supports '-incoming defer' only");
2350 exit(EXIT_FAILURE);
2353 #ifdef CONFIG_CURSES
2354 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2355 error_report("curses display cannot be used with -daemonize");
2356 exit(1);
2358 #endif
2361 static void qemu_process_sugar_options(void)
2363 if (mem_prealloc) {
2364 QObject *smp = qdict_get(machine_opts_dict, "smp");
2365 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2366 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2367 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2368 const char *val = qstring_get_str(qobject_to(QString, cpus));
2369 object_register_sugar_prop("memory-backend", "prealloc-threads",
2370 val, false);
2373 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2376 if (watchdog) {
2377 int i = select_watchdog(watchdog);
2378 if (i > 0)
2379 exit (i == 1 ? 1 : 0);
2383 /* -action processing */
2386 * Process all the -action parameters parsed from cmdline.
2388 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2390 Error *local_err = NULL;
2391 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2392 QObject *ret = NULL;
2393 qmp_marshal_set_action(qdict, &ret, &local_err);
2394 qobject_unref(ret);
2395 qobject_unref(qdict);
2396 if (local_err) {
2397 error_propagate(errp, local_err);
2398 return 1;
2400 return 0;
2403 static void qemu_process_early_options(void)
2405 #ifdef CONFIG_SECCOMP
2406 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2407 if (olist) {
2408 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2410 #endif
2412 qemu_opts_foreach(qemu_find_opts("name"),
2413 parse_name, NULL, &error_fatal);
2415 if (qemu_opts_foreach(qemu_find_opts("action"),
2416 process_runstate_actions, NULL, &error_fatal)) {
2417 exit(1);
2420 #ifndef _WIN32
2421 qemu_opts_foreach(qemu_find_opts("add-fd"),
2422 parse_add_fd, NULL, &error_fatal);
2424 qemu_opts_foreach(qemu_find_opts("add-fd"),
2425 cleanup_add_fd, NULL, &error_fatal);
2426 #endif
2428 /* Open the logfile at this point and set the log mask if necessary. */
2430 int mask = 0;
2431 if (log_mask) {
2432 mask = qemu_str_to_log_mask(log_mask);
2433 if (!mask) {
2434 qemu_print_log_usage(stdout);
2435 exit(1);
2438 qemu_set_log_filename_flags(log_file, mask, &error_fatal);
2441 qemu_add_default_firmwarepath();
2444 static void qemu_process_help_options(void)
2447 * Check for -cpu help and -device help before we call select_machine(),
2448 * which will return an error if the architecture has no default machine
2449 * type and the user did not specify one, so that the user doesn't need
2450 * to say '-cpu help -machine something'.
2452 if (cpu_option && is_help_option(cpu_option)) {
2453 list_cpus(cpu_option);
2454 exit(0);
2457 if (qemu_opts_foreach(qemu_find_opts("device"),
2458 device_help_func, NULL, NULL)) {
2459 exit(0);
2462 /* -L help lists the data directories and exits. */
2463 if (list_data_dirs) {
2464 qemu_list_data_dirs();
2465 exit(0);
2469 static void qemu_maybe_daemonize(const char *pid_file)
2471 Error *err = NULL;
2473 os_daemonize();
2474 rcu_disable_atfork();
2476 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2477 error_reportf_err(err, "cannot create PID file: ");
2478 exit(1);
2481 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2482 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2485 static void qemu_init_displays(void)
2487 DisplayState *ds;
2489 /* init local displays */
2490 ds = init_displaystate();
2491 qemu_display_init(ds, &dpy);
2493 /* must be after terminal init, SDL library changes signal handlers */
2494 os_setup_signal_handling();
2496 /* init remote displays */
2497 #ifdef CONFIG_VNC
2498 qemu_opts_foreach(qemu_find_opts("vnc"),
2499 vnc_init_func, NULL, &error_fatal);
2500 #endif
2502 if (using_spice) {
2503 qemu_spice.display_init();
2507 static void qemu_init_board(void)
2509 /* process plugin before CPUs are created, but once -smp has been parsed */
2510 qemu_plugin_load_list(&plugin_list, &error_fatal);
2512 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2513 machine_run_board_init(current_machine, mem_path, &error_fatal);
2515 drive_check_orphaned();
2517 realtime_init();
2519 if (hax_enabled()) {
2520 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2521 hax_sync_vcpus();
2525 static void qemu_create_cli_devices(void)
2527 DeviceOption *opt;
2529 soundhw_init();
2531 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2532 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2534 /* init USB devices */
2535 if (machine_usb(current_machine)) {
2536 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2537 exit(1);
2540 /* init generic devices */
2541 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2542 qemu_opts_foreach(qemu_find_opts("device"),
2543 device_init_func, NULL, &error_fatal);
2544 QTAILQ_FOREACH(opt, &device_opts, next) {
2545 DeviceState *dev;
2546 loc_push_restore(&opt->loc);
2548 * TODO Eventually we should call qmp_device_add() here to make sure it
2549 * behaves the same, but QMP still has to accept incorrectly typed
2550 * options until libvirt is fixed and we want to be strict on the CLI
2551 * from the start, so call qdev_device_add_from_qdict() directly for
2552 * now.
2554 dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2555 object_unref(OBJECT(dev));
2556 loc_pop(&opt->loc);
2558 rom_reset_order_override();
2561 static void qemu_machine_creation_done(void)
2563 MachineState *machine = MACHINE(qdev_get_machine());
2565 /* Did we create any drives that we failed to create a device for? */
2566 drive_check_orphaned();
2568 /* Don't warn about the default network setup that you get if
2569 * no command line -net or -netdev options are specified. There
2570 * are two cases that we would otherwise complain about:
2571 * (1) board doesn't support a NIC but the implicit "-net nic"
2572 * requested one
2573 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2574 * sets up a nic that isn't connected to anything.
2576 if (!default_net && (!qtest_enabled() || has_defaults)) {
2577 net_check_clients();
2580 qdev_prop_check_globals();
2582 qdev_machine_creation_done();
2584 if (machine->cgs) {
2586 * Verify that Confidential Guest Support has actually been initialized
2588 assert(machine->cgs->ready);
2591 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2592 exit(1);
2594 if (!vga_interface_created && !default_vga &&
2595 vga_interface_type != VGA_NONE) {
2596 warn_report("A -vga option was passed but this machine "
2597 "type does not use that option; "
2598 "No VGA device has been created");
2602 void qmp_x_exit_preconfig(Error **errp)
2604 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2605 error_setg(errp, "The command is permitted only before machine initialization");
2606 return;
2609 qemu_init_board();
2610 qemu_create_cli_devices();
2611 cxl_fixed_memory_window_link_targets(errp);
2612 qemu_machine_creation_done();
2614 if (loadvm) {
2615 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
2617 if (replay_mode != REPLAY_MODE_NONE) {
2618 replay_vmstate_init();
2621 if (incoming) {
2622 Error *local_err = NULL;
2623 if (strcmp(incoming, "defer") != 0) {
2624 qmp_migrate_incoming(incoming, &local_err);
2625 if (local_err) {
2626 error_reportf_err(local_err, "-incoming %s: ", incoming);
2627 exit(1);
2630 } else if (autostart) {
2631 qmp_cont(NULL);
2635 void qemu_init(int argc, char **argv, char **envp)
2637 QemuOpts *opts;
2638 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2639 QemuOptsList *olist;
2640 int optind;
2641 const char *optarg;
2642 MachineClass *machine_class;
2643 bool userconfig = true;
2644 FILE *vmstate_dump_file = NULL;
2646 qemu_add_opts(&qemu_drive_opts);
2647 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2648 qemu_add_drive_opts(&qemu_common_drive_opts);
2649 qemu_add_drive_opts(&qemu_drive_opts);
2650 qemu_add_drive_opts(&bdrv_runtime_opts);
2651 qemu_add_opts(&qemu_chardev_opts);
2652 qemu_add_opts(&qemu_device_opts);
2653 qemu_add_opts(&qemu_netdev_opts);
2654 qemu_add_opts(&qemu_nic_opts);
2655 qemu_add_opts(&qemu_net_opts);
2656 qemu_add_opts(&qemu_rtc_opts);
2657 qemu_add_opts(&qemu_global_opts);
2658 qemu_add_opts(&qemu_mon_opts);
2659 qemu_add_opts(&qemu_trace_opts);
2660 qemu_plugin_add_opts();
2661 qemu_add_opts(&qemu_option_rom_opts);
2662 qemu_add_opts(&qemu_accel_opts);
2663 qemu_add_opts(&qemu_mem_opts);
2664 qemu_add_opts(&qemu_smp_opts);
2665 qemu_add_opts(&qemu_boot_opts);
2666 qemu_add_opts(&qemu_add_fd_opts);
2667 qemu_add_opts(&qemu_object_opts);
2668 qemu_add_opts(&qemu_tpmdev_opts);
2669 qemu_add_opts(&qemu_overcommit_opts);
2670 qemu_add_opts(&qemu_msg_opts);
2671 qemu_add_opts(&qemu_name_opts);
2672 qemu_add_opts(&qemu_numa_opts);
2673 qemu_add_opts(&qemu_icount_opts);
2674 qemu_add_opts(&qemu_semihosting_config_opts);
2675 qemu_add_opts(&qemu_fw_cfg_opts);
2676 qemu_add_opts(&qemu_action_opts);
2677 module_call_init(MODULE_INIT_OPTS);
2679 error_init(argv[0]);
2680 qemu_init_exec_dir(argv[0]);
2682 qemu_init_arch_modules();
2684 qemu_init_subsystems();
2686 /* first pass of option parsing */
2687 optind = 1;
2688 while (optind < argc) {
2689 if (argv[optind][0] != '-') {
2690 /* disk image */
2691 optind++;
2692 } else {
2693 const QEMUOption *popt;
2695 popt = lookup_opt(argc, argv, &optarg, &optind);
2696 switch (popt->index) {
2697 case QEMU_OPTION_nouserconfig:
2698 userconfig = false;
2699 break;
2704 machine_opts_dict = qdict_new();
2705 if (userconfig) {
2706 qemu_read_default_config_file(&error_fatal);
2709 /* second pass of option parsing */
2710 optind = 1;
2711 for(;;) {
2712 if (optind >= argc)
2713 break;
2714 if (argv[optind][0] != '-') {
2715 loc_set_cmdline(argv, optind, 1);
2716 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2717 } else {
2718 const QEMUOption *popt;
2720 popt = lookup_opt(argc, argv, &optarg, &optind);
2721 if (!(popt->arch_mask & arch_type)) {
2722 error_report("Option not supported for this target");
2723 exit(1);
2725 switch(popt->index) {
2726 case QEMU_OPTION_cpu:
2727 /* hw initialization will check this */
2728 cpu_option = optarg;
2729 break;
2730 case QEMU_OPTION_hda:
2731 case QEMU_OPTION_hdb:
2732 case QEMU_OPTION_hdc:
2733 case QEMU_OPTION_hdd:
2734 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2735 HD_OPTS);
2736 break;
2737 case QEMU_OPTION_blockdev:
2739 Visitor *v;
2740 BlockdevOptionsQueueEntry *bdo;
2742 v = qobject_input_visitor_new_str(optarg, "driver",
2743 &error_fatal);
2745 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2746 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2747 &error_fatal);
2748 visit_free(v);
2749 loc_save(&bdo->loc);
2750 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2751 break;
2753 case QEMU_OPTION_drive:
2754 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
2755 optarg, false);
2756 if (opts == NULL) {
2757 exit(1);
2759 break;
2760 case QEMU_OPTION_set:
2761 qemu_set_option(optarg, &error_fatal);
2762 break;
2763 case QEMU_OPTION_global:
2764 if (qemu_global_option(optarg) != 0)
2765 exit(1);
2766 break;
2767 case QEMU_OPTION_mtdblock:
2768 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2769 break;
2770 case QEMU_OPTION_sd:
2771 drive_add(IF_SD, -1, optarg, SD_OPTS);
2772 break;
2773 case QEMU_OPTION_pflash:
2774 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2775 break;
2776 case QEMU_OPTION_snapshot:
2778 Error *blocker = NULL;
2779 snapshot = 1;
2780 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2781 "-snapshot");
2782 replay_add_blocker(blocker);
2784 break;
2785 case QEMU_OPTION_numa:
2786 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2787 optarg, true);
2788 if (!opts) {
2789 exit(1);
2791 break;
2792 case QEMU_OPTION_cxl_fixed_memory_window:
2793 parse_cxl_fixed_memory_window(optarg);
2794 break;
2795 case QEMU_OPTION_display:
2796 parse_display(optarg);
2797 break;
2798 case QEMU_OPTION_nographic:
2799 qdict_put_str(machine_opts_dict, "graphics", "off");
2800 nographic = true;
2801 dpy.type = DISPLAY_TYPE_NONE;
2802 break;
2803 case QEMU_OPTION_portrait:
2804 graphic_rotate = 90;
2805 break;
2806 case QEMU_OPTION_rotate:
2807 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2808 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2809 graphic_rotate != 180 && graphic_rotate != 270) {
2810 error_report("only 90, 180, 270 deg rotation is available");
2811 exit(1);
2813 break;
2814 case QEMU_OPTION_kernel:
2815 qdict_put_str(machine_opts_dict, "kernel", optarg);
2816 break;
2817 case QEMU_OPTION_initrd:
2818 qdict_put_str(machine_opts_dict, "initrd", optarg);
2819 break;
2820 case QEMU_OPTION_append:
2821 qdict_put_str(machine_opts_dict, "append", optarg);
2822 break;
2823 case QEMU_OPTION_dtb:
2824 qdict_put_str(machine_opts_dict, "dtb", optarg);
2825 break;
2826 case QEMU_OPTION_cdrom:
2827 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2828 break;
2829 case QEMU_OPTION_boot:
2830 machine_parse_property_opt(qemu_find_opts("boot-opts"), "boot", optarg);
2831 break;
2832 case QEMU_OPTION_fda:
2833 case QEMU_OPTION_fdb:
2834 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2835 optarg, FD_OPTS);
2836 break;
2837 case QEMU_OPTION_no_fd_bootchk:
2838 fd_bootchk = 0;
2839 break;
2840 case QEMU_OPTION_netdev:
2841 default_net = 0;
2842 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2843 exit(1);
2845 break;
2846 case QEMU_OPTION_nic:
2847 default_net = 0;
2848 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2849 exit(1);
2851 break;
2852 case QEMU_OPTION_net:
2853 default_net = 0;
2854 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2855 exit(1);
2857 break;
2858 #ifdef CONFIG_LIBISCSI
2859 case QEMU_OPTION_iscsi:
2860 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2861 optarg, false);
2862 if (!opts) {
2863 exit(1);
2865 break;
2866 #endif
2867 case QEMU_OPTION_audio_help:
2868 audio_legacy_help();
2869 exit (0);
2870 break;
2871 case QEMU_OPTION_audiodev:
2872 audio_parse_option(optarg);
2873 break;
2874 case QEMU_OPTION_audio: {
2875 QDict *dict = keyval_parse(optarg, "driver", NULL, &error_fatal);
2876 char *model;
2877 Audiodev *dev = NULL;
2878 Visitor *v;
2880 if (!qdict_haskey(dict, "id")) {
2881 qdict_put_str(dict, "id", "audiodev0");
2883 if (!qdict_haskey(dict, "model")) {
2884 error_setg(&error_fatal, "Parameter 'model' is missing");
2886 model = g_strdup(qdict_get_str(dict, "model"));
2887 qdict_del(dict, "model");
2888 if (is_help_option(model)) {
2889 show_valid_soundhw();
2890 exit(0);
2892 v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2893 qobject_unref(dict);
2894 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
2895 visit_free(v);
2896 audio_define(dev);
2897 select_soundhw(model, dev->id);
2898 g_free(model);
2899 break;
2901 case QEMU_OPTION_h:
2902 help(0);
2903 break;
2904 case QEMU_OPTION_version:
2905 version();
2906 exit(0);
2907 break;
2908 case QEMU_OPTION_m:
2909 parse_memory_options(optarg);
2910 break;
2911 #ifdef CONFIG_TPM
2912 case QEMU_OPTION_tpmdev:
2913 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2914 exit(1);
2916 break;
2917 #endif
2918 case QEMU_OPTION_mempath:
2919 mem_path = optarg;
2920 break;
2921 case QEMU_OPTION_mem_prealloc:
2922 mem_prealloc = 1;
2923 break;
2924 case QEMU_OPTION_d:
2925 log_mask = optarg;
2926 break;
2927 case QEMU_OPTION_D:
2928 log_file = optarg;
2929 break;
2930 case QEMU_OPTION_DFILTER:
2931 qemu_set_dfilter_ranges(optarg, &error_fatal);
2932 break;
2933 case QEMU_OPTION_seed:
2934 qemu_guest_random_seed_main(optarg, &error_fatal);
2935 break;
2936 case QEMU_OPTION_s:
2937 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2938 break;
2939 case QEMU_OPTION_gdb:
2940 add_device_config(DEV_GDB, optarg);
2941 break;
2942 case QEMU_OPTION_L:
2943 if (is_help_option(optarg)) {
2944 list_data_dirs = true;
2945 } else {
2946 qemu_add_data_dir(g_strdup(optarg));
2948 break;
2949 case QEMU_OPTION_bios:
2950 qdict_put_str(machine_opts_dict, "firmware", optarg);
2951 break;
2952 case QEMU_OPTION_singlestep:
2953 singlestep = 1;
2954 break;
2955 case QEMU_OPTION_S:
2956 autostart = 0;
2957 break;
2958 case QEMU_OPTION_k:
2959 keyboard_layout = optarg;
2960 break;
2961 case QEMU_OPTION_vga:
2962 vga_model = optarg;
2963 default_vga = 0;
2964 break;
2965 case QEMU_OPTION_g:
2967 const char *p;
2968 int w, h, depth;
2969 p = optarg;
2970 w = strtol(p, (char **)&p, 10);
2971 if (w <= 0) {
2972 graphic_error:
2973 error_report("invalid resolution or depth");
2974 exit(1);
2976 if (*p != 'x')
2977 goto graphic_error;
2978 p++;
2979 h = strtol(p, (char **)&p, 10);
2980 if (h <= 0)
2981 goto graphic_error;
2982 if (*p == 'x') {
2983 p++;
2984 depth = strtol(p, (char **)&p, 10);
2985 if (depth != 1 && depth != 2 && depth != 4 &&
2986 depth != 8 && depth != 15 && depth != 16 &&
2987 depth != 24 && depth != 32)
2988 goto graphic_error;
2989 } else if (*p == '\0') {
2990 depth = graphic_depth;
2991 } else {
2992 goto graphic_error;
2995 graphic_width = w;
2996 graphic_height = h;
2997 graphic_depth = depth;
2999 break;
3000 case QEMU_OPTION_echr:
3002 char *r;
3003 term_escape_char = strtol(optarg, &r, 0);
3004 if (r == optarg)
3005 printf("Bad argument to echr\n");
3006 break;
3008 case QEMU_OPTION_monitor:
3009 default_monitor = 0;
3010 if (strncmp(optarg, "none", 4)) {
3011 monitor_parse(optarg, "readline", false);
3013 break;
3014 case QEMU_OPTION_qmp:
3015 monitor_parse(optarg, "control", false);
3016 default_monitor = 0;
3017 break;
3018 case QEMU_OPTION_qmp_pretty:
3019 monitor_parse(optarg, "control", true);
3020 default_monitor = 0;
3021 break;
3022 case QEMU_OPTION_mon:
3023 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3024 true);
3025 if (!opts) {
3026 exit(1);
3028 default_monitor = 0;
3029 break;
3030 case QEMU_OPTION_chardev:
3031 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3032 optarg, true);
3033 if (!opts) {
3034 exit(1);
3036 break;
3037 case QEMU_OPTION_fsdev:
3038 olist = qemu_find_opts("fsdev");
3039 if (!olist) {
3040 error_report("fsdev support is disabled");
3041 exit(1);
3043 opts = qemu_opts_parse_noisily(olist, optarg, true);
3044 if (!opts) {
3045 exit(1);
3047 break;
3048 case QEMU_OPTION_virtfs: {
3049 QemuOpts *fsdev;
3050 QemuOpts *device;
3051 const char *writeout, *sock_fd, *socket, *path, *security_model,
3052 *multidevs;
3054 olist = qemu_find_opts("virtfs");
3055 if (!olist) {
3056 error_report("virtfs support is disabled");
3057 exit(1);
3059 opts = qemu_opts_parse_noisily(olist, optarg, true);
3060 if (!opts) {
3061 exit(1);
3064 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3065 qemu_opt_get(opts, "mount_tag") == NULL) {
3066 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3067 exit(1);
3069 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3070 qemu_opts_id(opts) ?:
3071 qemu_opt_get(opts, "mount_tag"),
3072 1, NULL);
3073 if (!fsdev) {
3074 error_report("duplicate or invalid fsdev id: %s",
3075 qemu_opt_get(opts, "mount_tag"));
3076 exit(1);
3079 writeout = qemu_opt_get(opts, "writeout");
3080 if (writeout) {
3081 #ifdef CONFIG_SYNC_FILE_RANGE
3082 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3083 #else
3084 error_report("writeout=immediate not supported "
3085 "on this platform");
3086 exit(1);
3087 #endif
3089 qemu_opt_set(fsdev, "fsdriver",
3090 qemu_opt_get(opts, "fsdriver"), &error_abort);
3091 path = qemu_opt_get(opts, "path");
3092 if (path) {
3093 qemu_opt_set(fsdev, "path", path, &error_abort);
3095 security_model = qemu_opt_get(opts, "security_model");
3096 if (security_model) {
3097 qemu_opt_set(fsdev, "security_model", security_model,
3098 &error_abort);
3100 socket = qemu_opt_get(opts, "socket");
3101 if (socket) {
3102 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3104 sock_fd = qemu_opt_get(opts, "sock_fd");
3105 if (sock_fd) {
3106 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3109 qemu_opt_set_bool(fsdev, "readonly",
3110 qemu_opt_get_bool(opts, "readonly", 0),
3111 &error_abort);
3112 multidevs = qemu_opt_get(opts, "multidevs");
3113 if (multidevs) {
3114 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3116 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3117 &error_abort);
3118 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3119 qemu_opt_set(device, "fsdev",
3120 qemu_opts_id(fsdev), &error_abort);
3121 qemu_opt_set(device, "mount_tag",
3122 qemu_opt_get(opts, "mount_tag"), &error_abort);
3123 break;
3125 case QEMU_OPTION_serial:
3126 add_device_config(DEV_SERIAL, optarg);
3127 default_serial = 0;
3128 if (strncmp(optarg, "mon:", 4) == 0) {
3129 default_monitor = 0;
3131 break;
3132 case QEMU_OPTION_watchdog:
3133 if (watchdog) {
3134 error_report("only one watchdog option may be given");
3135 exit(1);
3137 warn_report("-watchdog is deprecated; use -device instead.");
3138 watchdog = optarg;
3139 break;
3140 case QEMU_OPTION_action:
3141 olist = qemu_find_opts("action");
3142 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3143 exit(1);
3145 break;
3146 case QEMU_OPTION_watchdog_action: {
3147 QemuOpts *opts;
3148 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3149 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
3150 break;
3152 case QEMU_OPTION_parallel:
3153 add_device_config(DEV_PARALLEL, optarg);
3154 default_parallel = 0;
3155 if (strncmp(optarg, "mon:", 4) == 0) {
3156 default_monitor = 0;
3158 break;
3159 case QEMU_OPTION_debugcon:
3160 add_device_config(DEV_DEBUGCON, optarg);
3161 break;
3162 case QEMU_OPTION_loadvm:
3163 loadvm = optarg;
3164 break;
3165 case QEMU_OPTION_full_screen:
3166 dpy.has_full_screen = true;
3167 dpy.full_screen = true;
3168 break;
3169 case QEMU_OPTION_pidfile:
3170 pid_file = optarg;
3171 break;
3172 case QEMU_OPTION_win2k_hack:
3173 win2k_install_hack = 1;
3174 break;
3175 case QEMU_OPTION_acpitable:
3176 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3177 optarg, true);
3178 if (!opts) {
3179 exit(1);
3181 acpi_table_add(opts, &error_fatal);
3182 break;
3183 case QEMU_OPTION_smbios:
3184 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3185 optarg, false);
3186 if (!opts) {
3187 exit(1);
3189 smbios_entry_add(opts, &error_fatal);
3190 break;
3191 case QEMU_OPTION_fwcfg:
3192 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3193 optarg, true);
3194 if (opts == NULL) {
3195 exit(1);
3197 break;
3198 case QEMU_OPTION_preconfig:
3199 preconfig_requested = true;
3200 break;
3201 case QEMU_OPTION_enable_kvm:
3202 qdict_put_str(machine_opts_dict, "accel", "kvm");
3203 break;
3204 case QEMU_OPTION_M:
3205 case QEMU_OPTION_machine:
3207 bool help;
3209 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3210 if (help) {
3211 machine_help_func(machine_opts_dict);
3212 exit(EXIT_SUCCESS);
3214 break;
3216 case QEMU_OPTION_accel:
3217 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3218 optarg, true);
3219 optarg = qemu_opt_get(accel_opts, "accel");
3220 if (!optarg || is_help_option(optarg)) {
3221 printf("Accelerators supported in QEMU binary:\n");
3222 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3223 false);
3224 for (el = accel_list; el; el = el->next) {
3225 gchar *typename = g_strdup(object_class_get_name(
3226 OBJECT_CLASS(el->data)));
3227 /* omit qtest which is used for tests only */
3228 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3229 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3230 gchar **optname = g_strsplit(typename,
3231 ACCEL_CLASS_SUFFIX, 0);
3232 printf("%s\n", optname[0]);
3233 g_strfreev(optname);
3235 g_free(typename);
3237 g_slist_free(accel_list);
3238 exit(0);
3240 break;
3241 case QEMU_OPTION_usb:
3242 qdict_put_str(machine_opts_dict, "usb", "on");
3243 break;
3244 case QEMU_OPTION_usbdevice:
3245 qdict_put_str(machine_opts_dict, "usb", "on");
3246 add_device_config(DEV_USB, optarg);
3247 break;
3248 case QEMU_OPTION_device:
3249 if (optarg[0] == '{') {
3250 QObject *obj = qobject_from_json(optarg, &error_fatal);
3251 DeviceOption *opt = g_new0(DeviceOption, 1);
3252 opt->opts = qobject_to(QDict, obj);
3253 loc_save(&opt->loc);
3254 assert(opt->opts != NULL);
3255 QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3256 } else {
3257 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3258 optarg, true)) {
3259 exit(1);
3262 break;
3263 case QEMU_OPTION_smp:
3264 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3265 "smp", optarg);
3266 break;
3267 case QEMU_OPTION_vnc:
3268 vnc_parse(optarg);
3269 break;
3270 case QEMU_OPTION_no_acpi:
3271 qdict_put_str(machine_opts_dict, "acpi", "off");
3272 break;
3273 case QEMU_OPTION_no_hpet:
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 qemu_create_machine(machine_opts_dict);
3544 suspend_mux_open();
3546 qemu_disable_default_devices();
3547 qemu_create_default_devices();
3548 qemu_create_early_backends();
3550 qemu_apply_legacy_machine_options(machine_opts_dict);
3551 qemu_apply_machine_options(machine_opts_dict);
3552 qobject_unref(machine_opts_dict);
3553 phase_advance(PHASE_MACHINE_CREATED);
3556 * Note: uses machine properties such as kernel-irqchip, must run
3557 * after qemu_apply_machine_options.
3559 configure_accelerators(argv[0]);
3560 phase_advance(PHASE_ACCEL_CREATED);
3563 * Beware, QOM objects created before this point miss global and
3564 * compat properties.
3566 * Global properties get set up by qdev_prop_register_global(),
3567 * called from user_register_global_props(), and certain option
3568 * desugaring. Also in CPU feature desugaring (buried in
3569 * parse_cpu_option()), which happens below this point, but may
3570 * only target the CPU type, which can only be created after
3571 * parse_cpu_option() returned the type.
3573 * Machine compat properties: object_set_machine_compat_props().
3574 * Accelerator compat props: object_set_accelerator_compat_props(),
3575 * called from do_configure_accelerator().
3578 machine_class = MACHINE_GET_CLASS(current_machine);
3579 if (!qtest_enabled() && machine_class->deprecation_reason) {
3580 warn_report("Machine type '%s' is deprecated: %s",
3581 machine_class->name, machine_class->deprecation_reason);
3585 * Note: creates a QOM object, must run only after global and
3586 * compat properties have been set up.
3588 migration_object_init();
3590 qemu_create_late_backends();
3592 /* parse features once if machine provides default cpu_type */
3593 current_machine->cpu_type = machine_class->default_cpu_type;
3594 if (cpu_option) {
3595 current_machine->cpu_type = parse_cpu_option(cpu_option);
3597 /* NB: for machine none cpu_type could STILL be NULL here! */
3599 qemu_resolve_machine_memdev();
3600 parse_numa_opts(current_machine);
3601 cxl_set_opts();
3603 if (vmstate_dump_file) {
3604 /* dump and exit */
3605 module_load_qom_all();
3606 dump_vmstate_json_to_file(vmstate_dump_file);
3607 exit(0);
3610 if (!preconfig_requested) {
3611 qmp_x_exit_preconfig(&error_fatal);
3613 qemu_init_displays();
3614 accel_setup_post(current_machine);
3615 os_setup_post();
3616 resume_mux_open();