tests/vm: update openbsd to release 6.9
[qemu.git] / softmmu / vl.c
blob4df149610185dccb44cee8c454e5f8f084c7b8bd
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "hw/qdev-properties.h"
31 #include "qapi/compat-policy.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu-version.h"
36 #include "qemu/cutils.h"
37 #include "qemu/help_option.h"
38 #include "qemu/uuid.h"
39 #include "sysemu/reset.h"
40 #include "sysemu/runstate.h"
41 #include "sysemu/runstate-action.h"
42 #include "sysemu/seccomp.h"
43 #include "sysemu/tcg.h"
44 #include "sysemu/xen.h"
46 #include "qemu/error-report.h"
47 #include "qemu/sockets.h"
48 #include "qemu/accel.h"
49 #include "hw/usb.h"
50 #include "hw/isa/isa.h"
51 #include "hw/scsi/scsi.h"
52 #include "hw/display/vga.h"
53 #include "sysemu/watchdog.h"
54 #include "hw/firmware/smbios.h"
55 #include "hw/acpi/acpi.h"
56 #include "hw/xen/xen.h"
57 #include "hw/loader.h"
58 #include "monitor/qdev.h"
59 #include "net/net.h"
60 #include "net/slirp.h"
61 #include "monitor/monitor.h"
62 #include "ui/console.h"
63 #include "ui/input.h"
64 #include "sysemu/sysemu.h"
65 #include "sysemu/numa.h"
66 #include "sysemu/hostmem.h"
67 #include "exec/gdbstub.h"
68 #include "qemu/timer.h"
69 #include "chardev/char.h"
70 #include "qemu/bitmap.h"
71 #include "qemu/log.h"
72 #include "sysemu/blockdev.h"
73 #include "hw/block/block.h"
74 #include "hw/i386/x86.h"
75 #include "hw/i386/pc.h"
76 #include "migration/misc.h"
77 #include "migration/snapshot.h"
78 #include "sysemu/tpm.h"
79 #include "sysemu/dma.h"
80 #include "hw/audio/soundhw.h"
81 #include "audio/audio.h"
82 #include "sysemu/cpus.h"
83 #include "sysemu/cpu-timers.h"
84 #include "migration/colo.h"
85 #include "migration/postcopy-ram.h"
86 #include "sysemu/kvm.h"
87 #include "sysemu/hax.h"
88 #include "qapi/qobject-input-visitor.h"
89 #include "qemu/option.h"
90 #include "qemu/config-file.h"
91 #include "qemu/qemu-options.h"
92 #include "qemu/main-loop.h"
93 #ifdef CONFIG_VIRTFS
94 #include "fsdev/qemu-fsdev.h"
95 #endif
96 #include "sysemu/qtest.h"
98 #include "disas/disas.h"
100 #include "trace.h"
101 #include "trace/control.h"
102 #include "qemu/plugin.h"
103 #include "qemu/queue.h"
104 #include "sysemu/arch_init.h"
105 #include "exec/confidential-guest-support.h"
107 #include "ui/qemu-spice.h"
108 #include "qapi/string-input-visitor.h"
109 #include "qapi/opts-visitor.h"
110 #include "qapi/clone-visitor.h"
111 #include "qom/object_interfaces.h"
112 #include "semihosting/semihost.h"
113 #include "crypto/init.h"
114 #include "sysemu/replay.h"
115 #include "qapi/qapi-events-run-state.h"
116 #include "qapi/qapi-visit-block-core.h"
117 #include "qapi/qapi-visit-compat.h"
118 #include "qapi/qapi-visit-ui.h"
119 #include "qapi/qapi-commands-block-core.h"
120 #include "qapi/qapi-commands-migration.h"
121 #include "qapi/qapi-commands-misc.h"
122 #include "qapi/qapi-visit-qom.h"
123 #include "qapi/qapi-commands-ui.h"
124 #include "qapi/qmp/qdict.h"
125 #include "qapi/qmp/qerror.h"
126 #include "sysemu/iothread.h"
127 #include "qemu/guest-random.h"
129 #include "config-host.h"
131 #define MAX_VIRTIO_CONSOLES 1
133 typedef struct BlockdevOptionsQueueEntry {
134 BlockdevOptions *bdo;
135 Location loc;
136 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
137 } BlockdevOptionsQueueEntry;
139 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
141 typedef struct ObjectOption {
142 ObjectOptions *opts;
143 QTAILQ_ENTRY(ObjectOption) next;
144 } ObjectOption;
146 static const char *cpu_option;
147 static const char *mem_path;
148 static const char *incoming;
149 static const char *loadvm;
150 static const char *accelerators;
151 static QDict *machine_opts_dict;
152 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
153 static ram_addr_t maxram_size;
154 static uint64_t ram_slots;
155 static int display_remote;
156 static int snapshot;
157 static bool preconfig_requested;
158 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
159 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
160 static bool nographic = false;
161 static int mem_prealloc; /* force preallocation of physical target memory */
162 static ram_addr_t ram_size;
163 static const char *vga_model = NULL;
164 static DisplayOptions dpy;
165 static int num_serial_hds;
166 static Chardev **serial_hds;
167 static const char *log_mask;
168 static const char *log_file;
169 static bool list_data_dirs;
170 static const char *watchdog;
171 static const char *qtest_chrdev;
172 static const char *qtest_log;
174 static int has_defaults = 1;
175 static int default_serial = 1;
176 static int default_parallel = 1;
177 static int default_monitor = 1;
178 static int default_floppy = 1;
179 static int default_cdrom = 1;
180 static int default_sdcard = 1;
181 static int default_vga = 1;
182 static int default_net = 1;
184 static struct {
185 const char *driver;
186 int *flag;
187 } default_list[] = {
188 { .driver = "isa-serial", .flag = &default_serial },
189 { .driver = "isa-parallel", .flag = &default_parallel },
190 { .driver = "isa-fdc", .flag = &default_floppy },
191 { .driver = "floppy", .flag = &default_floppy },
192 { .driver = "ide-cd", .flag = &default_cdrom },
193 { .driver = "ide-hd", .flag = &default_cdrom },
194 { .driver = "scsi-cd", .flag = &default_cdrom },
195 { .driver = "scsi-hd", .flag = &default_cdrom },
196 { .driver = "VGA", .flag = &default_vga },
197 { .driver = "isa-vga", .flag = &default_vga },
198 { .driver = "cirrus-vga", .flag = &default_vga },
199 { .driver = "isa-cirrus-vga", .flag = &default_vga },
200 { .driver = "vmware-svga", .flag = &default_vga },
201 { .driver = "qxl-vga", .flag = &default_vga },
202 { .driver = "virtio-vga", .flag = &default_vga },
203 { .driver = "ati-vga", .flag = &default_vga },
204 { .driver = "vhost-user-vga", .flag = &default_vga },
207 static QemuOptsList qemu_rtc_opts = {
208 .name = "rtc",
209 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
210 .merge_lists = true,
211 .desc = {
213 .name = "base",
214 .type = QEMU_OPT_STRING,
216 .name = "clock",
217 .type = QEMU_OPT_STRING,
219 .name = "driftfix",
220 .type = QEMU_OPT_STRING,
222 { /* end of list */ }
226 static QemuOptsList qemu_option_rom_opts = {
227 .name = "option-rom",
228 .implied_opt_name = "romfile",
229 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
230 .desc = {
232 .name = "bootindex",
233 .type = QEMU_OPT_NUMBER,
234 }, {
235 .name = "romfile",
236 .type = QEMU_OPT_STRING,
238 { /* end of list */ }
242 static QemuOptsList qemu_accel_opts = {
243 .name = "accel",
244 .implied_opt_name = "accel",
245 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
246 .desc = {
248 * no elements => accept any
249 * sanity checking will happen later
250 * when setting accelerator properties
256 static QemuOptsList qemu_boot_opts = {
257 .name = "boot-opts",
258 .implied_opt_name = "order",
259 .merge_lists = true,
260 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
261 .desc = {
263 .name = "order",
264 .type = QEMU_OPT_STRING,
265 }, {
266 .name = "once",
267 .type = QEMU_OPT_STRING,
268 }, {
269 .name = "menu",
270 .type = QEMU_OPT_BOOL,
271 }, {
272 .name = "splash",
273 .type = QEMU_OPT_STRING,
274 }, {
275 .name = "splash-time",
276 .type = QEMU_OPT_NUMBER,
277 }, {
278 .name = "reboot-timeout",
279 .type = QEMU_OPT_NUMBER,
280 }, {
281 .name = "strict",
282 .type = QEMU_OPT_BOOL,
284 { /*End of list */ }
288 static QemuOptsList qemu_add_fd_opts = {
289 .name = "add-fd",
290 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
291 .desc = {
293 .name = "fd",
294 .type = QEMU_OPT_NUMBER,
295 .help = "file descriptor of which a duplicate is added to fd set",
297 .name = "set",
298 .type = QEMU_OPT_NUMBER,
299 .help = "ID of the fd set to add fd to",
301 .name = "opaque",
302 .type = QEMU_OPT_STRING,
303 .help = "free-form string used to describe fd",
305 { /* end of list */ }
309 static QemuOptsList qemu_object_opts = {
310 .name = "object",
311 .implied_opt_name = "qom-type",
312 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
313 .desc = {
318 static QemuOptsList qemu_tpmdev_opts = {
319 .name = "tpmdev",
320 .implied_opt_name = "type",
321 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
322 .desc = {
323 /* options are defined in the TPM backends */
324 { /* end of list */ }
328 static QemuOptsList qemu_overcommit_opts = {
329 .name = "overcommit",
330 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
331 .desc = {
333 .name = "mem-lock",
334 .type = QEMU_OPT_BOOL,
337 .name = "cpu-pm",
338 .type = QEMU_OPT_BOOL,
340 { /* end of list */ }
344 static QemuOptsList qemu_msg_opts = {
345 .name = "msg",
346 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
347 .desc = {
349 .name = "timestamp",
350 .type = QEMU_OPT_BOOL,
353 .name = "guest-name",
354 .type = QEMU_OPT_BOOL,
355 .help = "Prepends guest name for error messages but only if "
356 "-name guest is set otherwise option is ignored\n",
358 { /* end of list */ }
362 static QemuOptsList qemu_name_opts = {
363 .name = "name",
364 .implied_opt_name = "guest",
365 .merge_lists = true,
366 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
367 .desc = {
369 .name = "guest",
370 .type = QEMU_OPT_STRING,
371 .help = "Sets the name of the guest.\n"
372 "This name will be displayed in the SDL window caption.\n"
373 "The name will also be used for the VNC server",
374 }, {
375 .name = "process",
376 .type = QEMU_OPT_STRING,
377 .help = "Sets the name of the QEMU process, as shown in top etc",
378 }, {
379 .name = "debug-threads",
380 .type = QEMU_OPT_BOOL,
381 .help = "When enabled, name the individual threads; defaults off.\n"
382 "NOTE: The thread names are for debugging and not a\n"
383 "stable API.",
385 { /* End of list */ }
389 static QemuOptsList qemu_mem_opts = {
390 .name = "memory",
391 .implied_opt_name = "size",
392 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
393 .merge_lists = true,
394 .desc = {
396 .name = "size",
397 .type = QEMU_OPT_SIZE,
400 .name = "slots",
401 .type = QEMU_OPT_NUMBER,
404 .name = "maxmem",
405 .type = QEMU_OPT_SIZE,
407 { /* end of list */ }
411 static QemuOptsList qemu_icount_opts = {
412 .name = "icount",
413 .implied_opt_name = "shift",
414 .merge_lists = true,
415 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
416 .desc = {
418 .name = "shift",
419 .type = QEMU_OPT_STRING,
420 }, {
421 .name = "align",
422 .type = QEMU_OPT_BOOL,
423 }, {
424 .name = "sleep",
425 .type = QEMU_OPT_BOOL,
426 }, {
427 .name = "rr",
428 .type = QEMU_OPT_STRING,
429 }, {
430 .name = "rrfile",
431 .type = QEMU_OPT_STRING,
432 }, {
433 .name = "rrsnapshot",
434 .type = QEMU_OPT_STRING,
436 { /* end of list */ }
440 static QemuOptsList qemu_fw_cfg_opts = {
441 .name = "fw_cfg",
442 .implied_opt_name = "name",
443 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
444 .desc = {
446 .name = "name",
447 .type = QEMU_OPT_STRING,
448 .help = "Sets the fw_cfg name of the blob to be inserted",
449 }, {
450 .name = "file",
451 .type = QEMU_OPT_STRING,
452 .help = "Sets the name of the file from which "
453 "the fw_cfg blob will be loaded",
454 }, {
455 .name = "string",
456 .type = QEMU_OPT_STRING,
457 .help = "Sets content of the blob to be inserted from a string",
458 }, {
459 .name = "gen_id",
460 .type = QEMU_OPT_STRING,
461 .help = "Sets id of the object generating the fw_cfg blob "
462 "to be inserted",
464 { /* end of list */ }
468 static QemuOptsList qemu_action_opts = {
469 .name = "action",
470 .merge_lists = true,
471 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
472 .desc = {
474 .name = "shutdown",
475 .type = QEMU_OPT_STRING,
477 .name = "reboot",
478 .type = QEMU_OPT_STRING,
480 .name = "panic",
481 .type = QEMU_OPT_STRING,
483 .name = "watchdog",
484 .type = QEMU_OPT_STRING,
486 { /* end of list */ }
490 const char *qemu_get_vm_name(void)
492 return qemu_name;
495 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
497 const char *driver = qemu_opt_get(opts, "driver");
498 int i;
500 if (!driver)
501 return 0;
502 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
503 if (strcmp(default_list[i].driver, driver) != 0)
504 continue;
505 *(default_list[i].flag) = 0;
507 return 0;
510 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
512 const char *proc_name;
514 if (qemu_opt_get(opts, "debug-threads")) {
515 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
517 qemu_name = qemu_opt_get(opts, "guest");
519 proc_name = qemu_opt_get(opts, "process");
520 if (proc_name) {
521 os_set_proc_name(proc_name);
524 return 0;
527 bool defaults_enabled(void)
529 return has_defaults;
532 #ifndef _WIN32
533 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
535 int fd, dupfd, flags;
536 int64_t fdset_id;
537 const char *fd_opaque = NULL;
538 AddfdInfo *fdinfo;
540 fd = qemu_opt_get_number(opts, "fd", -1);
541 fdset_id = qemu_opt_get_number(opts, "set", -1);
542 fd_opaque = qemu_opt_get(opts, "opaque");
544 if (fd < 0) {
545 error_setg(errp, "fd option is required and must be non-negative");
546 return -1;
549 if (fd <= STDERR_FILENO) {
550 error_setg(errp, "fd cannot be a standard I/O stream");
551 return -1;
555 * All fds inherited across exec() necessarily have FD_CLOEXEC
556 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
558 flags = fcntl(fd, F_GETFD);
559 if (flags == -1 || (flags & FD_CLOEXEC)) {
560 error_setg(errp, "fd is not valid or already in use");
561 return -1;
564 if (fdset_id < 0) {
565 error_setg(errp, "set option is required and must be non-negative");
566 return -1;
569 #ifdef F_DUPFD_CLOEXEC
570 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
571 #else
572 dupfd = dup(fd);
573 if (dupfd != -1) {
574 qemu_set_cloexec(dupfd);
576 #endif
577 if (dupfd == -1) {
578 error_setg(errp, "error duplicating fd: %s", strerror(errno));
579 return -1;
582 /* add the duplicate fd, and optionally the opaque string, to the fd set */
583 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
584 &error_abort);
585 g_free(fdinfo);
587 return 0;
590 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
592 int fd;
594 fd = qemu_opt_get_number(opts, "fd", -1);
595 close(fd);
597 return 0;
599 #endif
601 /***********************************************************/
602 /* QEMU Block devices */
604 #define HD_OPTS "media=disk"
605 #define CDROM_OPTS "media=cdrom"
606 #define FD_OPTS ""
607 #define PFLASH_OPTS ""
608 #define MTD_OPTS ""
609 #define SD_OPTS ""
611 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
613 BlockInterfaceType *block_default_type = opaque;
615 return drive_new(opts, *block_default_type, errp) == NULL;
618 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
620 if (qemu_opt_get(opts, "snapshot") == NULL) {
621 qemu_opt_set(opts, "snapshot", "on", &error_abort);
623 return 0;
626 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
627 int index, const char *optstr)
629 QemuOpts *opts;
630 DriveInfo *dinfo;
632 if (!enable || drive_get_by_index(type, index)) {
633 return;
636 opts = drive_add(type, index, NULL, optstr);
637 if (snapshot) {
638 drive_enable_snapshot(NULL, opts, NULL);
641 dinfo = drive_new(opts, type, &error_abort);
642 dinfo->is_default = true;
646 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
647 MachineClass *machine_class, int snapshot)
650 * If the currently selected machine wishes to override the
651 * units-per-bus property of its default HBA interface type, do so
652 * now.
654 if (machine_class->units_per_default_bus) {
655 override_max_devs(machine_class->block_default_type,
656 machine_class->units_per_default_bus);
659 /* open the virtual block devices */
660 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
661 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
663 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
664 loc_push_restore(&bdo->loc);
665 qmp_blockdev_add(bdo->bdo, &error_fatal);
666 loc_pop(&bdo->loc);
667 qapi_free_BlockdevOptions(bdo->bdo);
668 g_free(bdo);
670 if (snapshot) {
671 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
672 NULL, NULL);
674 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
675 &machine_class->block_default_type, &error_fatal)) {
676 /* We printed help */
677 exit(0);
680 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
681 CDROM_OPTS);
682 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
683 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
687 static QemuOptsList qemu_smp_opts = {
688 .name = "smp-opts",
689 .implied_opt_name = "cpus",
690 .merge_lists = true,
691 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
692 .desc = {
694 .name = "cpus",
695 .type = QEMU_OPT_NUMBER,
696 }, {
697 .name = "sockets",
698 .type = QEMU_OPT_NUMBER,
699 }, {
700 .name = "dies",
701 .type = QEMU_OPT_NUMBER,
702 }, {
703 .name = "cores",
704 .type = QEMU_OPT_NUMBER,
705 }, {
706 .name = "threads",
707 .type = QEMU_OPT_NUMBER,
708 }, {
709 .name = "maxcpus",
710 .type = QEMU_OPT_NUMBER,
712 { /*End of list */ }
716 static void realtime_init(void)
718 if (enable_mlock) {
719 if (os_mlock() < 0) {
720 error_report("locking memory failed");
721 exit(1);
727 static void configure_msg(QemuOpts *opts)
729 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
730 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
734 /***********************************************************/
735 /* USB devices */
737 static int usb_device_add(const char *devname)
739 USBDevice *dev = NULL;
741 if (!machine_usb(current_machine)) {
742 return -1;
745 dev = usbdevice_create(devname);
746 if (!dev)
747 return -1;
749 return 0;
752 static int usb_parse(const char *cmdline)
754 int r;
755 r = usb_device_add(cmdline);
756 if (r < 0) {
757 error_report("could not add USB device '%s'", cmdline);
759 return r;
762 /***********************************************************/
763 /* machine registration */
765 static MachineClass *find_machine(const char *name, GSList *machines)
767 GSList *el;
769 for (el = machines; el; el = el->next) {
770 MachineClass *mc = el->data;
772 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
773 return mc;
777 return NULL;
780 static MachineClass *find_default_machine(GSList *machines)
782 GSList *el;
783 MachineClass *default_machineclass = NULL;
785 for (el = machines; el; el = el->next) {
786 MachineClass *mc = el->data;
788 if (mc->is_default) {
789 assert(default_machineclass == NULL && "Multiple default machines");
790 default_machineclass = mc;
794 return default_machineclass;
797 static void version(void)
799 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
800 QEMU_COPYRIGHT "\n");
803 static void help(int exitcode)
805 version();
806 printf("usage: %s [options] [disk_image]\n\n"
807 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
808 error_get_progname());
810 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
811 if ((arch_mask) & arch_type) \
812 fputs(opt_help, stdout);
814 #define ARCHHEADING(text, arch_mask) \
815 if ((arch_mask) & arch_type) \
816 puts(stringify(text));
818 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
820 #include "qemu-options.def"
822 printf("\nDuring emulation, the following keys are useful:\n"
823 "ctrl-alt-f toggle full screen\n"
824 "ctrl-alt-n switch to virtual console 'n'\n"
825 "ctrl-alt toggle mouse and keyboard grab\n"
826 "\n"
827 "When using -nographic, press 'ctrl-a h' to get some help.\n"
828 "\n"
829 QEMU_HELP_BOTTOM "\n");
831 exit(exitcode);
834 #define HAS_ARG 0x0001
836 typedef struct QEMUOption {
837 const char *name;
838 int flags;
839 int index;
840 uint32_t arch_mask;
841 } QEMUOption;
843 static const QEMUOption qemu_options[] = {
844 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
846 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
847 { option, opt_arg, opt_enum, arch_mask },
848 #define DEFHEADING(text)
849 #define ARCHHEADING(text, arch_mask)
851 #include "qemu-options.def"
852 { NULL },
855 typedef struct VGAInterfaceInfo {
856 const char *opt_name; /* option name */
857 const char *name; /* human-readable name */
858 /* Class names indicating that support is available.
859 * If no class is specified, the interface is always available */
860 const char *class_names[2];
861 } VGAInterfaceInfo;
863 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
864 [VGA_NONE] = {
865 .opt_name = "none",
866 .name = "no graphic card",
868 [VGA_STD] = {
869 .opt_name = "std",
870 .name = "standard VGA",
871 .class_names = { "VGA", "isa-vga" },
873 [VGA_CIRRUS] = {
874 .opt_name = "cirrus",
875 .name = "Cirrus VGA",
876 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
878 [VGA_VMWARE] = {
879 .opt_name = "vmware",
880 .name = "VMWare SVGA",
881 .class_names = { "vmware-svga" },
883 [VGA_VIRTIO] = {
884 .opt_name = "virtio",
885 .name = "Virtio VGA",
886 .class_names = { "virtio-vga" },
888 [VGA_QXL] = {
889 .opt_name = "qxl",
890 .name = "QXL VGA",
891 .class_names = { "qxl-vga" },
893 [VGA_TCX] = {
894 .opt_name = "tcx",
895 .name = "TCX framebuffer",
896 .class_names = { "sun-tcx" },
898 [VGA_CG3] = {
899 .opt_name = "cg3",
900 .name = "CG3 framebuffer",
901 .class_names = { "cgthree" },
903 [VGA_XENFB] = {
904 .opt_name = "xenfb",
905 .name = "Xen paravirtualized framebuffer",
909 static bool vga_interface_available(VGAInterfaceType t)
911 const VGAInterfaceInfo *ti = &vga_interfaces[t];
913 assert(t < VGA_TYPE_MAX);
914 return !ti->class_names[0] ||
915 module_object_class_by_name(ti->class_names[0]) ||
916 module_object_class_by_name(ti->class_names[1]);
919 static const char *
920 get_default_vga_model(const MachineClass *machine_class)
922 if (machine_class->default_display) {
923 return machine_class->default_display;
924 } else if (vga_interface_available(VGA_CIRRUS)) {
925 return "cirrus";
926 } else if (vga_interface_available(VGA_STD)) {
927 return "std";
930 return NULL;
933 static void select_vgahw(const MachineClass *machine_class, const char *p)
935 const char *opts;
936 int t;
938 if (g_str_equal(p, "help")) {
939 const char *def = get_default_vga_model(machine_class);
941 for (t = 0; t < VGA_TYPE_MAX; t++) {
942 const VGAInterfaceInfo *ti = &vga_interfaces[t];
944 if (vga_interface_available(t) && ti->opt_name) {
945 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
946 g_str_equal(ti->opt_name, def) ? " (default)" : "");
949 exit(0);
952 assert(vga_interface_type == VGA_NONE);
953 for (t = 0; t < VGA_TYPE_MAX; t++) {
954 const VGAInterfaceInfo *ti = &vga_interfaces[t];
955 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
956 if (!vga_interface_available(t)) {
957 error_report("%s not available", ti->name);
958 exit(1);
960 vga_interface_type = t;
961 break;
964 if (t == VGA_TYPE_MAX) {
965 invalid_vga:
966 error_report("unknown vga type: %s", p);
967 exit(1);
969 while (*opts) {
970 const char *nextopt;
972 if (strstart(opts, ",retrace=", &nextopt)) {
973 opts = nextopt;
974 if (strstart(opts, "dumb", &nextopt))
975 vga_retrace_method = VGA_RETRACE_DUMB;
976 else if (strstart(opts, "precise", &nextopt))
977 vga_retrace_method = VGA_RETRACE_PRECISE;
978 else goto invalid_vga;
979 } else goto invalid_vga;
980 opts = nextopt;
984 static void parse_display_qapi(const char *optarg)
986 DisplayOptions *opts;
987 Visitor *v;
989 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
991 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
992 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
994 qapi_free_DisplayOptions(opts);
995 visit_free(v);
998 DisplayOptions *qmp_query_display_options(Error **errp)
1000 return QAPI_CLONE(DisplayOptions, &dpy);
1003 static void parse_display(const char *p)
1005 const char *opts;
1007 if (is_help_option(p)) {
1008 qemu_display_help();
1009 exit(0);
1012 if (strstart(p, "sdl", &opts)) {
1014 * sdl DisplayType needs hand-crafted parser instead of
1015 * parse_display_qapi() due to some options not in
1016 * DisplayOptions, specifically:
1017 * - ctrl_grab + alt_grab
1018 * Not clear yet what happens to them long-term. Should
1019 * replaced by something better or deprecated and dropped.
1021 #if defined(CONFIG_SDL)
1022 dpy.type = DISPLAY_TYPE_SDL;
1023 while (*opts) {
1024 const char *nextopt;
1026 if (strstart(opts, ",alt_grab=", &nextopt)) {
1027 opts = nextopt;
1028 if (strstart(opts, "on", &nextopt)) {
1029 alt_grab = 1;
1030 } else if (strstart(opts, "off", &nextopt)) {
1031 alt_grab = 0;
1032 } else {
1033 goto invalid_sdl_args;
1035 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1036 opts = nextopt;
1037 if (strstart(opts, "on", &nextopt)) {
1038 ctrl_grab = 1;
1039 } else if (strstart(opts, "off", &nextopt)) {
1040 ctrl_grab = 0;
1041 } else {
1042 goto invalid_sdl_args;
1044 } else if (strstart(opts, ",window_close=", &nextopt) ||
1045 strstart(opts, ",window-close=", &nextopt)) {
1046 if (strstart(opts, ",window_close=", NULL)) {
1047 warn_report("window_close with an underscore is deprecated,"
1048 " please use window-close instead.");
1050 opts = nextopt;
1051 dpy.has_window_close = true;
1052 if (strstart(opts, "on", &nextopt)) {
1053 dpy.window_close = true;
1054 } else if (strstart(opts, "off", &nextopt)) {
1055 dpy.window_close = false;
1056 } else {
1057 goto invalid_sdl_args;
1059 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1060 opts = nextopt;
1061 dpy.has_show_cursor = true;
1062 if (strstart(opts, "on", &nextopt)) {
1063 dpy.show_cursor = true;
1064 } else if (strstart(opts, "off", &nextopt)) {
1065 dpy.show_cursor = false;
1066 } else {
1067 goto invalid_sdl_args;
1069 } else if (strstart(opts, ",gl=", &nextopt)) {
1070 opts = nextopt;
1071 dpy.has_gl = true;
1072 if (strstart(opts, "on", &nextopt)) {
1073 dpy.gl = DISPLAYGL_MODE_ON;
1074 } else if (strstart(opts, "core", &nextopt)) {
1075 dpy.gl = DISPLAYGL_MODE_CORE;
1076 } else if (strstart(opts, "es", &nextopt)) {
1077 dpy.gl = DISPLAYGL_MODE_ES;
1078 } else if (strstart(opts, "off", &nextopt)) {
1079 dpy.gl = DISPLAYGL_MODE_OFF;
1080 } else {
1081 goto invalid_sdl_args;
1083 } else {
1084 invalid_sdl_args:
1085 error_report("invalid SDL option string");
1086 exit(1);
1088 opts = nextopt;
1090 #else
1091 error_report("SDL display supported is not available in this binary");
1092 exit(1);
1093 #endif
1094 } else if (strstart(p, "vnc", &opts)) {
1096 * vnc isn't a (local) DisplayType but a protocol for remote
1097 * display access.
1099 if (*opts == '=') {
1100 vnc_parse(opts + 1);
1101 } else {
1102 error_report("VNC requires a display argument vnc=<display>");
1103 exit(1);
1105 } else {
1106 parse_display_qapi(p);
1110 static inline bool nonempty_str(const char *str)
1112 return str && *str;
1115 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1117 gchar *buf;
1118 size_t size;
1119 const char *name, *file, *str, *gen_id;
1120 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1122 if (fw_cfg == NULL) {
1123 error_setg(errp, "fw_cfg device not available");
1124 return -1;
1126 name = qemu_opt_get(opts, "name");
1127 file = qemu_opt_get(opts, "file");
1128 str = qemu_opt_get(opts, "string");
1129 gen_id = qemu_opt_get(opts, "gen_id");
1131 /* we need the name, and exactly one of: file, content string, gen_id */
1132 if (!nonempty_str(name) ||
1133 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1134 error_setg(errp, "name, plus exactly one of file,"
1135 " string and gen_id, are needed");
1136 return -1;
1138 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1139 error_setg(errp, "name too long (max. %d char)",
1140 FW_CFG_MAX_FILE_PATH - 1);
1141 return -1;
1143 if (nonempty_str(gen_id)) {
1145 * In this particular case where the content is populated
1146 * internally, the "etc/" namespace protection is relaxed,
1147 * so do not emit a warning.
1149 } else if (strncmp(name, "opt/", 4) != 0) {
1150 warn_report("externally provided fw_cfg item names "
1151 "should be prefixed with \"opt/\"");
1153 if (nonempty_str(str)) {
1154 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1155 buf = g_memdup(str, size);
1156 } else if (nonempty_str(gen_id)) {
1157 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1158 return -1;
1160 return 0;
1161 } else {
1162 GError *err = NULL;
1163 if (!g_file_get_contents(file, &buf, &size, &err)) {
1164 error_setg(errp, "can't load %s: %s", file, err->message);
1165 g_error_free(err);
1166 return -1;
1169 /* For legacy, keep user files in a specific global order. */
1170 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1171 fw_cfg_add_file(fw_cfg, name, buf, size);
1172 fw_cfg_reset_order_override(fw_cfg);
1173 return 0;
1176 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1178 return qdev_device_help(opts);
1181 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1183 DeviceState *dev;
1185 dev = qdev_device_add(opts, errp);
1186 if (!dev && *errp) {
1187 error_report_err(*errp);
1188 return -1;
1189 } else if (dev) {
1190 object_unref(OBJECT(dev));
1192 return 0;
1195 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1197 Error *local_err = NULL;
1199 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1200 if (local_err) {
1201 error_propagate(errp, local_err);
1202 return -1;
1204 exit(0);
1206 return 0;
1209 #ifdef CONFIG_VIRTFS
1210 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1212 return qemu_fsdev_add(opts, errp);
1214 #endif
1216 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1218 return monitor_init_opts(opts, errp);
1221 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1223 static int monitor_device_index = 0;
1224 QemuOpts *opts;
1225 const char *p;
1226 char label[32];
1228 if (strstart(optarg, "chardev:", &p)) {
1229 snprintf(label, sizeof(label), "%s", p);
1230 } else {
1231 snprintf(label, sizeof(label), "compat_monitor%d",
1232 monitor_device_index);
1233 opts = qemu_chr_parse_compat(label, optarg, true);
1234 if (!opts) {
1235 error_report("parse error: %s", optarg);
1236 exit(1);
1240 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1241 qemu_opt_set(opts, "mode", mode, &error_abort);
1242 qemu_opt_set(opts, "chardev", label, &error_abort);
1243 if (!strcmp(mode, "control")) {
1244 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1245 } else {
1246 assert(pretty == false);
1248 monitor_device_index++;
1251 struct device_config {
1252 enum {
1253 DEV_USB, /* -usbdevice */
1254 DEV_SERIAL, /* -serial */
1255 DEV_PARALLEL, /* -parallel */
1256 DEV_DEBUGCON, /* -debugcon */
1257 DEV_GDB, /* -gdb, -s */
1258 DEV_SCLP, /* s390 sclp */
1259 } type;
1260 const char *cmdline;
1261 Location loc;
1262 QTAILQ_ENTRY(device_config) next;
1265 static QTAILQ_HEAD(, device_config) device_configs =
1266 QTAILQ_HEAD_INITIALIZER(device_configs);
1268 static void add_device_config(int type, const char *cmdline)
1270 struct device_config *conf;
1272 conf = g_malloc0(sizeof(*conf));
1273 conf->type = type;
1274 conf->cmdline = cmdline;
1275 loc_save(&conf->loc);
1276 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1279 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1281 struct device_config *conf;
1282 int rc;
1284 QTAILQ_FOREACH(conf, &device_configs, next) {
1285 if (conf->type != type)
1286 continue;
1287 loc_push_restore(&conf->loc);
1288 rc = func(conf->cmdline);
1289 loc_pop(&conf->loc);
1290 if (rc) {
1291 return rc;
1294 return 0;
1297 static void qemu_disable_default_devices(void)
1299 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1301 qemu_opts_foreach(qemu_find_opts("device"),
1302 default_driver_check, NULL, NULL);
1303 qemu_opts_foreach(qemu_find_opts("global"),
1304 default_driver_check, NULL, NULL);
1306 if (!vga_model && !default_vga) {
1307 vga_interface_type = VGA_DEVICE;
1309 if (!has_defaults || machine_class->no_serial) {
1310 default_serial = 0;
1312 if (!has_defaults || machine_class->no_parallel) {
1313 default_parallel = 0;
1315 if (!has_defaults || machine_class->no_floppy) {
1316 default_floppy = 0;
1318 if (!has_defaults || machine_class->no_cdrom) {
1319 default_cdrom = 0;
1321 if (!has_defaults || machine_class->no_sdcard) {
1322 default_sdcard = 0;
1324 if (!has_defaults) {
1325 default_monitor = 0;
1326 default_net = 0;
1327 default_vga = 0;
1331 static void qemu_create_default_devices(void)
1333 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1335 if (is_daemonized()) {
1336 /* According to documentation and historically, -nographic redirects
1337 * serial port, parallel port and monitor to stdio, which does not work
1338 * with -daemonize. We can redirect these to null instead, but since
1339 * -nographic is legacy, let's just error out.
1340 * We disallow -nographic only if all other ports are not redirected
1341 * explicitly, to not break existing legacy setups which uses
1342 * -nographic _and_ redirects all ports explicitly - this is valid
1343 * usage, -nographic is just a no-op in this case.
1345 if (nographic
1346 && (default_parallel || default_serial || default_monitor)) {
1347 error_report("-nographic cannot be used with -daemonize");
1348 exit(1);
1352 if (nographic) {
1353 if (default_parallel)
1354 add_device_config(DEV_PARALLEL, "null");
1355 if (default_serial && default_monitor) {
1356 add_device_config(DEV_SERIAL, "mon:stdio");
1357 } else {
1358 if (default_serial)
1359 add_device_config(DEV_SERIAL, "stdio");
1360 if (default_monitor)
1361 monitor_parse("stdio", "readline", false);
1363 } else {
1364 if (default_serial)
1365 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1366 if (default_parallel)
1367 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1368 if (default_monitor)
1369 monitor_parse("vc:80Cx24C", "readline", false);
1372 if (default_net) {
1373 QemuOptsList *net = qemu_find_opts("net");
1374 qemu_opts_parse(net, "nic", true, &error_abort);
1375 #ifdef CONFIG_SLIRP
1376 qemu_opts_parse(net, "user", true, &error_abort);
1377 #endif
1380 #if defined(CONFIG_VNC)
1381 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1382 display_remote++;
1384 #endif
1385 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1386 if (!qemu_display_find_default(&dpy)) {
1387 dpy.type = DISPLAY_TYPE_NONE;
1388 #if defined(CONFIG_VNC)
1389 vnc_parse("localhost:0,to=99,id=default");
1390 #endif
1393 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1394 dpy.type = DISPLAY_TYPE_NONE;
1397 /* If no default VGA is requested, the default is "none". */
1398 if (default_vga) {
1399 vga_model = get_default_vga_model(machine_class);
1401 if (vga_model) {
1402 select_vgahw(machine_class, vga_model);
1406 static int serial_parse(const char *devname)
1408 int index = num_serial_hds;
1409 char label[32];
1411 if (strcmp(devname, "none") == 0)
1412 return 0;
1413 snprintf(label, sizeof(label), "serial%d", index);
1414 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1416 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1417 if (!serial_hds[index]) {
1418 error_report("could not connect serial device"
1419 " to character backend '%s'", devname);
1420 return -1;
1422 num_serial_hds++;
1423 return 0;
1426 Chardev *serial_hd(int i)
1428 assert(i >= 0);
1429 if (i < num_serial_hds) {
1430 return serial_hds[i];
1432 return NULL;
1435 static int parallel_parse(const char *devname)
1437 static int index = 0;
1438 char label[32];
1440 if (strcmp(devname, "none") == 0)
1441 return 0;
1442 if (index == MAX_PARALLEL_PORTS) {
1443 error_report("too many parallel ports");
1444 exit(1);
1446 snprintf(label, sizeof(label), "parallel%d", index);
1447 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1448 if (!parallel_hds[index]) {
1449 error_report("could not connect parallel device"
1450 " to character backend '%s'", devname);
1451 return -1;
1453 index++;
1454 return 0;
1457 static int debugcon_parse(const char *devname)
1459 QemuOpts *opts;
1461 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1462 error_report("invalid character backend '%s'", devname);
1463 exit(1);
1465 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1466 if (!opts) {
1467 error_report("already have a debugcon device");
1468 exit(1);
1470 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1471 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1472 return 0;
1475 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1477 const MachineClass *mc1 = a, *mc2 = b;
1478 int res;
1480 if (mc1->family == NULL) {
1481 if (mc2->family == NULL) {
1482 /* Compare standalone machine types against each other; they sort
1483 * in increasing order.
1485 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1486 object_class_get_name(OBJECT_CLASS(mc2)));
1489 /* Standalone machine types sort after families. */
1490 return 1;
1493 if (mc2->family == NULL) {
1494 /* Families sort before standalone machine types. */
1495 return -1;
1498 /* Families sort between each other alphabetically increasingly. */
1499 res = strcmp(mc1->family, mc2->family);
1500 if (res != 0) {
1501 return res;
1504 /* Within the same family, machine types sort in decreasing order. */
1505 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1506 object_class_get_name(OBJECT_CLASS(mc1)));
1509 static void machine_help_func(const QDict *qdict)
1511 GSList *machines, *el;
1512 const char *type = qdict_get_try_str(qdict, "type");
1514 machines = object_class_get_list(TYPE_MACHINE, false);
1515 if (type) {
1516 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1517 if (machine_class) {
1518 type_print_class_properties(object_class_get_name(machine_class));
1519 return;
1523 printf("Supported machines are:\n");
1524 machines = g_slist_sort(machines, machine_class_cmp);
1525 for (el = machines; el; el = el->next) {
1526 MachineClass *mc = el->data;
1527 if (mc->alias) {
1528 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1530 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1531 mc->is_default ? " (default)" : "",
1532 mc->deprecation_reason ? " (deprecated)" : "");
1536 static void
1537 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1538 const char *arg, Error **errp)
1540 QDict *opts, *prop;
1541 bool help = false;
1542 ERRP_GUARD();
1544 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, errp);
1545 if (help) {
1546 qemu_opts_print_help(opts_list, true);
1547 return;
1549 opts = qdict_new();
1550 qdict_put(opts, propname, prop);
1551 keyval_merge(machine_opts_dict, opts, errp);
1552 qobject_unref(opts);
1555 static const char *pid_file;
1556 static Notifier qemu_unlink_pidfile_notifier;
1558 static void qemu_unlink_pidfile(Notifier *n, void *data)
1560 if (pid_file) {
1561 unlink(pid_file);
1565 static const QEMUOption *lookup_opt(int argc, char **argv,
1566 const char **poptarg, int *poptind)
1568 const QEMUOption *popt;
1569 int optind = *poptind;
1570 char *r = argv[optind];
1571 const char *optarg;
1573 loc_set_cmdline(argv, optind, 1);
1574 optind++;
1575 /* Treat --foo the same as -foo. */
1576 if (r[1] == '-')
1577 r++;
1578 popt = qemu_options;
1579 for(;;) {
1580 if (!popt->name) {
1581 error_report("invalid option");
1582 exit(1);
1584 if (!strcmp(popt->name, r + 1))
1585 break;
1586 popt++;
1588 if (popt->flags & HAS_ARG) {
1589 if (optind >= argc) {
1590 error_report("requires an argument");
1591 exit(1);
1593 optarg = argv[optind++];
1594 loc_set_cmdline(argv, optind - 2, 2);
1595 } else {
1596 optarg = NULL;
1599 *poptarg = optarg;
1600 *poptind = optind;
1602 return popt;
1605 static MachineClass *select_machine(QDict *qdict, Error **errp)
1607 const char *optarg = qdict_get_try_str(qdict, "type");
1608 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1609 MachineClass *machine_class;
1610 Error *local_err = NULL;
1612 if (optarg) {
1613 machine_class = find_machine(optarg, machines);
1614 qdict_del(qdict, "type");
1615 if (!machine_class) {
1616 error_setg(&local_err, "unsupported machine type");
1618 } else {
1619 machine_class = find_default_machine(machines);
1620 if (!machine_class) {
1621 error_setg(&local_err, "No machine specified, and there is no default");
1625 g_slist_free(machines);
1626 if (local_err) {
1627 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1628 error_propagate(errp, local_err);
1630 return machine_class;
1633 static int object_parse_property_opt(Object *obj,
1634 const char *name, const char *value,
1635 const char *skip, Error **errp)
1637 if (g_str_equal(name, skip)) {
1638 return 0;
1641 if (!object_property_parse(obj, name, value, errp)) {
1642 return -1;
1645 return 0;
1648 /* *Non*recursively replace underscores with dashes in QDict keys. */
1649 static void keyval_dashify(QDict *qdict, Error **errp)
1651 const QDictEntry *ent, *next;
1652 char *p;
1654 for (ent = qdict_first(qdict); ent; ent = next) {
1655 g_autofree char *new_key = NULL;
1657 next = qdict_next(qdict, ent);
1658 if (!strchr(ent->key, '_')) {
1659 continue;
1661 new_key = g_strdup(ent->key);
1662 for (p = new_key; *p; p++) {
1663 if (*p == '_') {
1664 *p = '-';
1667 if (qdict_haskey(qdict, new_key)) {
1668 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1669 return;
1671 qobject_ref(ent->value);
1672 qdict_put_obj(qdict, new_key, ent->value);
1673 qdict_del(qdict, ent->key);
1677 static void qemu_apply_legacy_machine_options(QDict *qdict)
1679 const char *value;
1681 keyval_dashify(qdict, &error_fatal);
1683 /* Legacy options do not correspond to MachineState properties. */
1684 value = qdict_get_try_str(qdict, "accel");
1685 if (value) {
1686 accelerators = g_strdup(value);
1687 qdict_del(qdict, "accel");
1690 value = qdict_get_try_str(qdict, "igd-passthru");
1691 if (value) {
1692 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1693 false);
1694 qdict_del(qdict, "igd-passthru");
1697 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1698 if (value) {
1699 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1700 false);
1701 qdict_del(qdict, "kvm-shadow-mem");
1704 value = qdict_get_try_str(qdict, "kernel-irqchip");
1705 if (value) {
1706 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1707 false);
1708 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1709 false);
1710 qdict_del(qdict, "kernel-irqchip");
1714 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1716 ObjectOption *opt, *next;
1718 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1719 const char *type = ObjectType_str(opt->opts->qom_type);
1720 if (type_opt_predicate(type)) {
1721 user_creatable_add_qapi(opt->opts, &error_fatal);
1722 qapi_free_ObjectOptions(opt->opts);
1723 QTAILQ_REMOVE(&object_opts, opt, next);
1724 g_free(opt);
1729 static void object_option_add_visitor(Visitor *v)
1731 ObjectOption *opt = g_new0(ObjectOption, 1);
1732 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1733 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1736 static void object_option_parse(const char *optarg)
1738 QemuOpts *opts;
1739 const char *type;
1740 Visitor *v;
1742 if (optarg[0] == '{') {
1743 QObject *obj = qobject_from_json(optarg, &error_fatal);
1745 v = qobject_input_visitor_new(obj);
1746 qobject_unref(obj);
1747 } else {
1748 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1749 optarg, true);
1750 if (!opts) {
1751 exit(1);
1754 type = qemu_opt_get(opts, "qom-type");
1755 if (!type) {
1756 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1758 if (user_creatable_print_help(type, opts)) {
1759 exit(0);
1762 v = opts_visitor_new(opts);
1765 object_option_add_visitor(v);
1766 visit_free(v);
1770 * Initial object creation happens before all other
1771 * QEMU data types are created. The majority of objects
1772 * can be created at this point. The rng-egd object
1773 * cannot be created here, as it depends on the chardev
1774 * already existing.
1776 static bool object_create_early(const char *type)
1779 * Objects should not be made "delayed" without a reason. If you
1780 * add one, state the reason in a comment!
1783 /* Reason: property "chardev" */
1784 if (g_str_equal(type, "rng-egd") ||
1785 g_str_equal(type, "qtest")) {
1786 return false;
1789 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1790 /* Reason: cryptodev-vhost-user property "chardev" */
1791 if (g_str_equal(type, "cryptodev-vhost-user")) {
1792 return false;
1794 #endif
1796 /* Reason: vhost-user-blk-server property "node-name" */
1797 if (g_str_equal(type, "vhost-user-blk-server")) {
1798 return false;
1801 * Reason: filter-* property "netdev" etc.
1803 if (g_str_equal(type, "filter-buffer") ||
1804 g_str_equal(type, "filter-dump") ||
1805 g_str_equal(type, "filter-mirror") ||
1806 g_str_equal(type, "filter-redirector") ||
1807 g_str_equal(type, "colo-compare") ||
1808 g_str_equal(type, "filter-rewriter") ||
1809 g_str_equal(type, "filter-replay")) {
1810 return false;
1814 * Allocation of large amounts of memory may delay
1815 * chardev initialization for too long, and trigger timeouts
1816 * on software that waits for a monitor socket to be created
1817 * (e.g. libvirt).
1819 if (g_str_has_prefix(type, "memory-backend-")) {
1820 return false;
1823 return true;
1826 static void qemu_apply_machine_options(QDict *qdict)
1828 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1829 const char *boot_order = NULL;
1830 const char *boot_once = NULL;
1831 QemuOpts *opts;
1833 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1834 current_machine->ram_size = ram_size;
1835 current_machine->maxram_size = maxram_size;
1836 current_machine->ram_slots = ram_slots;
1838 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1839 if (opts) {
1840 boot_order = qemu_opt_get(opts, "order");
1841 if (boot_order) {
1842 validate_bootdevices(boot_order, &error_fatal);
1845 boot_once = qemu_opt_get(opts, "once");
1846 if (boot_once) {
1847 validate_bootdevices(boot_once, &error_fatal);
1850 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1851 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1854 if (!boot_order) {
1855 boot_order = machine_class->default_boot_order;
1858 current_machine->boot_order = boot_order;
1859 current_machine->boot_once = boot_once;
1861 if (semihosting_enabled() && !semihosting_get_argc()) {
1862 /* fall back to the -kernel/-append */
1863 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1866 if (current_machine->smp.cpus > 1) {
1867 Error *blocker = NULL;
1868 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1869 replay_add_blocker(blocker);
1873 static void qemu_create_early_backends(void)
1875 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1876 #if defined(CONFIG_SDL)
1877 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1878 #else
1879 const bool use_sdl = false;
1880 #endif
1881 #if defined(CONFIG_GTK)
1882 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1883 #else
1884 const bool use_gtk = false;
1885 #endif
1887 if ((alt_grab || ctrl_grab) && !use_sdl) {
1888 error_report("-alt-grab and -ctrl-grab are only valid "
1889 "for SDL, ignoring option");
1891 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1892 error_report("-no-quit is only valid for GTK and SDL, "
1893 "ignoring option");
1896 qemu_display_early_init(&dpy);
1897 qemu_console_early_init();
1899 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1900 #if defined(CONFIG_OPENGL)
1901 error_report("OpenGL is not supported by the display");
1902 #else
1903 error_report("OpenGL support is disabled");
1904 #endif
1905 exit(1);
1908 object_option_foreach_add(object_create_early);
1910 /* spice needs the timers to be initialized by this point */
1911 /* spice must initialize before audio as it changes the default auiodev */
1912 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1913 qemu_spice.init();
1915 qemu_opts_foreach(qemu_find_opts("chardev"),
1916 chardev_init_func, NULL, &error_fatal);
1918 #ifdef CONFIG_VIRTFS
1919 qemu_opts_foreach(qemu_find_opts("fsdev"),
1920 fsdev_init_func, NULL, &error_fatal);
1921 #endif
1924 * Note: we need to create audio and block backends before
1925 * setting machine properties, so they can be referred to.
1927 configure_blockdev(&bdo_queue, machine_class, snapshot);
1928 audio_init_audiodevs();
1933 * The remainder of object creation happens after the
1934 * creation of chardev, fsdev, net clients and device data types.
1936 static bool object_create_late(const char *type)
1938 return !object_create_early(type);
1941 static void qemu_create_late_backends(void)
1943 if (qtest_chrdev) {
1944 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1947 net_init_clients(&error_fatal);
1949 object_option_foreach_add(object_create_late);
1951 if (tpm_init() < 0) {
1952 exit(1);
1955 qemu_opts_foreach(qemu_find_opts("mon"),
1956 mon_init_func, NULL, &error_fatal);
1958 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1959 exit(1);
1960 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1961 exit(1);
1962 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1963 exit(1);
1965 /* now chardevs have been created we may have semihosting to connect */
1966 qemu_semihosting_connect_chardevs();
1967 qemu_semihosting_console_init();
1970 static bool have_custom_ram_size(void)
1972 QemuOpts *opts = qemu_find_opts_singleton("memory");
1973 return !!qemu_opt_get_size(opts, "size", 0);
1976 static void qemu_resolve_machine_memdev(void)
1978 if (current_machine->ram_memdev_id) {
1979 Object *backend;
1980 ram_addr_t backend_size;
1982 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1983 TYPE_MEMORY_BACKEND, NULL);
1984 if (!backend) {
1985 error_report("Memory backend '%s' not found",
1986 current_machine->ram_memdev_id);
1987 exit(EXIT_FAILURE);
1989 backend_size = object_property_get_uint(backend, "size", &error_abort);
1990 if (have_custom_ram_size() && backend_size != ram_size) {
1991 error_report("Size specified by -m option must match size of "
1992 "explicitly specified 'memory-backend' property");
1993 exit(EXIT_FAILURE);
1995 if (mem_path) {
1996 error_report("'-mem-path' can't be used together with"
1997 "'-machine memory-backend'");
1998 exit(EXIT_FAILURE);
2000 ram_size = backend_size;
2003 if (!xen_enabled()) {
2004 /* On 32-bit hosts, QEMU is limited by virtual address space */
2005 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
2006 error_report("at most 2047 MB RAM can be simulated");
2007 exit(1);
2012 static void set_memory_options(MachineClass *mc)
2014 uint64_t sz;
2015 const char *mem_str;
2016 const ram_addr_t default_ram_size = mc->default_ram_size;
2017 QemuOpts *opts = qemu_find_opts_singleton("memory");
2018 Location loc;
2020 loc_push_none(&loc);
2021 qemu_opts_loc_restore(opts);
2023 sz = 0;
2024 mem_str = qemu_opt_get(opts, "size");
2025 if (mem_str) {
2026 if (!*mem_str) {
2027 error_report("missing 'size' option value");
2028 exit(EXIT_FAILURE);
2031 sz = qemu_opt_get_size(opts, "size", ram_size);
2033 /* Fix up legacy suffix-less format */
2034 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2035 uint64_t overflow_check = sz;
2037 sz *= MiB;
2038 if (sz / MiB != overflow_check) {
2039 error_report("too large 'size' option value");
2040 exit(EXIT_FAILURE);
2045 /* backward compatibility behaviour for case "-m 0" */
2046 if (sz == 0) {
2047 sz = default_ram_size;
2050 sz = QEMU_ALIGN_UP(sz, 8192);
2051 if (mc->fixup_ram_size) {
2052 sz = mc->fixup_ram_size(sz);
2054 ram_size = sz;
2055 if (ram_size != sz) {
2056 error_report("ram size too large");
2057 exit(EXIT_FAILURE);
2060 maxram_size = ram_size;
2062 if (qemu_opt_get(opts, "maxmem")) {
2063 uint64_t slots;
2065 sz = qemu_opt_get_size(opts, "maxmem", 0);
2066 slots = qemu_opt_get_number(opts, "slots", 0);
2067 if (sz < ram_size) {
2068 error_report("invalid value of -m option maxmem: "
2069 "maximum memory size (0x%" PRIx64 ") must be at least "
2070 "the initial memory size (0x" RAM_ADDR_FMT ")",
2071 sz, ram_size);
2072 exit(EXIT_FAILURE);
2073 } else if (slots && sz == ram_size) {
2074 error_report("invalid value of -m option maxmem: "
2075 "memory slots were specified but maximum memory size "
2076 "(0x%" PRIx64 ") is equal to the initial memory size "
2077 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2078 exit(EXIT_FAILURE);
2081 maxram_size = sz;
2082 ram_slots = slots;
2083 } else if (qemu_opt_get(opts, "slots")) {
2084 error_report("invalid -m option value: missing 'maxmem' option");
2085 exit(EXIT_FAILURE);
2088 loc_pop(&loc);
2091 static void qemu_create_machine(QDict *qdict)
2093 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2094 object_set_machine_compat_props(machine_class->compat_props);
2096 set_memory_options(machine_class);
2098 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2099 object_property_add_child(object_get_root(), "machine",
2100 OBJECT(current_machine));
2101 object_property_add_child(container_get(OBJECT(current_machine),
2102 "/unattached"),
2103 "sysbus", OBJECT(sysbus_get_default()));
2105 if (machine_class->minimum_page_bits) {
2106 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2107 /* This would be a board error: specifying a minimum smaller than
2108 * a target's compile-time fixed setting.
2110 g_assert_not_reached();
2114 cpu_exec_init_all();
2115 page_size_init();
2117 if (machine_class->hw_version) {
2118 qemu_set_hw_version(machine_class->hw_version);
2122 * Get the default machine options from the machine if it is not already
2123 * specified either by the configuration file or by the command line.
2125 if (machine_class->default_machine_opts) {
2126 QDict *default_opts =
2127 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2128 &error_abort);
2129 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2130 false, &error_abort);
2131 qobject_unref(default_opts);
2135 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2137 GlobalProperty *g;
2139 g = g_malloc0(sizeof(*g));
2140 g->driver = qemu_opt_get(opts, "driver");
2141 g->property = qemu_opt_get(opts, "property");
2142 g->value = qemu_opt_get(opts, "value");
2143 qdev_prop_register_global(g);
2144 return 0;
2148 * Return whether configuration group @group is stored in QemuOpts, or
2149 * recorded as one or more QDicts by qemu_record_config_group.
2151 static bool is_qemuopts_group(const char *group)
2153 if (g_str_equal(group, "object") ||
2154 g_str_equal(group, "machine")) {
2155 return false;
2157 return true;
2160 static void qemu_record_config_group(const char *group, QDict *dict,
2161 bool from_json, Error **errp)
2163 if (g_str_equal(group, "object")) {
2164 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2165 object_option_add_visitor(v);
2166 visit_free(v);
2167 } else if (g_str_equal(group, "machine")) {
2169 * Cannot merge string-valued and type-safe dictionaries, so JSON
2170 * is not accepted yet for -M.
2172 assert(!from_json);
2173 keyval_merge(machine_opts_dict, dict, errp);
2174 } else {
2175 abort();
2180 * Parse non-QemuOpts config file groups, pass the rest to
2181 * qemu_config_do_parse.
2183 static void qemu_parse_config_group(const char *group, QDict *qdict,
2184 void *opaque, Error **errp)
2186 QObject *crumpled;
2187 if (is_qemuopts_group(group)) {
2188 qemu_config_do_parse(group, qdict, opaque, errp);
2189 return;
2192 crumpled = qdict_crumple(qdict, errp);
2193 if (!crumpled) {
2194 return;
2196 switch (qobject_type(crumpled)) {
2197 case QTYPE_QDICT:
2198 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2199 break;
2200 case QTYPE_QLIST:
2201 error_setg(errp, "Lists cannot be at top level of a configuration section");
2202 break;
2203 default:
2204 g_assert_not_reached();
2206 qobject_unref(crumpled);
2209 static void qemu_read_default_config_file(Error **errp)
2211 ERRP_GUARD();
2212 int ret;
2213 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2215 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2216 if (ret < 0) {
2217 if (ret == -ENOENT) {
2218 error_free(*errp);
2219 *errp = NULL;
2224 static void qemu_set_option(const char *str, Error **errp)
2226 char group[64], id[64], arg[64];
2227 QemuOptsList *list;
2228 QemuOpts *opts;
2229 int rc, offset;
2231 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2232 if (rc < 3 || str[offset] != '=') {
2233 error_setg(errp, "can't parse: \"%s\"", str);
2234 return;
2237 if (!is_qemuopts_group(group)) {
2238 error_setg(errp, "-set is not supported with %s", group);
2239 } else {
2240 list = qemu_find_opts_err(group, errp);
2241 if (list) {
2242 opts = qemu_opts_find(list, id);
2243 if (!opts) {
2244 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2245 return;
2247 qemu_opt_set(opts, arg, str + offset + 1, errp);
2252 static void user_register_global_props(void)
2254 qemu_opts_foreach(qemu_find_opts("global"),
2255 global_init_func, NULL, NULL);
2258 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2260 icount_configure(opts, errp);
2261 return 0;
2264 static int accelerator_set_property(void *opaque,
2265 const char *name, const char *value,
2266 Error **errp)
2268 return object_parse_property_opt(opaque, name, value, "accel", errp);
2271 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2273 bool *p_init_failed = opaque;
2274 const char *acc = qemu_opt_get(opts, "accel");
2275 AccelClass *ac = accel_find(acc);
2276 AccelState *accel;
2277 int ret;
2278 bool qtest_with_kvm;
2280 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2282 if (!ac) {
2283 *p_init_failed = true;
2284 if (!qtest_with_kvm) {
2285 error_report("invalid accelerator %s", acc);
2287 return 0;
2289 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2290 object_apply_compat_props(OBJECT(accel));
2291 qemu_opt_foreach(opts, accelerator_set_property,
2292 accel,
2293 &error_fatal);
2295 ret = accel_init_machine(accel, current_machine);
2296 if (ret < 0) {
2297 *p_init_failed = true;
2298 if (!qtest_with_kvm || ret != -ENOENT) {
2299 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2301 return 0;
2304 return 1;
2307 static void configure_accelerators(const char *progname)
2309 bool init_failed = false;
2311 qemu_opts_foreach(qemu_find_opts("icount"),
2312 do_configure_icount, NULL, &error_fatal);
2314 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2315 char **accel_list, **tmp;
2317 if (accelerators == NULL) {
2318 /* Select the default accelerator */
2319 bool have_tcg = accel_find("tcg");
2320 bool have_kvm = accel_find("kvm");
2322 if (have_tcg && have_kvm) {
2323 if (g_str_has_suffix(progname, "kvm")) {
2324 /* If the program name ends with "kvm", we prefer KVM */
2325 accelerators = "kvm:tcg";
2326 } else {
2327 accelerators = "tcg:kvm";
2329 } else if (have_kvm) {
2330 accelerators = "kvm";
2331 } else if (have_tcg) {
2332 accelerators = "tcg";
2333 } else {
2334 error_report("No accelerator selected and"
2335 " no default accelerator available");
2336 exit(1);
2339 accel_list = g_strsplit(accelerators, ":", 0);
2341 for (tmp = accel_list; *tmp; tmp++) {
2343 * Filter invalid accelerators here, to prevent obscenities
2344 * such as "-machine accel=tcg,,thread=single".
2346 if (accel_find(*tmp)) {
2347 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2348 } else {
2349 init_failed = true;
2350 error_report("invalid accelerator %s", *tmp);
2353 g_strfreev(accel_list);
2354 } else {
2355 if (accelerators != NULL) {
2356 error_report("The -accel and \"-machine accel=\" options are incompatible");
2357 exit(1);
2361 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2362 do_configure_accelerator, &init_failed, &error_fatal)) {
2363 if (!init_failed) {
2364 error_report("no accelerator found");
2366 exit(1);
2369 if (init_failed && !qtest_chrdev) {
2370 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2371 error_report("falling back to %s", ac->name);
2374 if (icount_enabled() && !tcg_enabled()) {
2375 error_report("-icount is not allowed with hardware virtualization");
2376 exit(1);
2380 static void create_default_memdev(MachineState *ms, const char *path)
2382 Object *obj;
2383 MachineClass *mc = MACHINE_GET_CLASS(ms);
2385 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2386 if (path) {
2387 object_property_set_str(obj, "mem-path", path, &error_fatal);
2389 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2390 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2391 obj);
2392 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2393 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2394 false, &error_fatal);
2395 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2396 object_unref(obj);
2397 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2398 &error_fatal);
2401 static void qemu_validate_options(const QDict *machine_opts)
2403 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2404 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2405 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2407 if (kernel_filename == NULL) {
2408 if (kernel_cmdline != NULL) {
2409 error_report("-append only allowed with -kernel option");
2410 exit(1);
2413 if (initrd_filename != NULL) {
2414 error_report("-initrd only allowed with -kernel option");
2415 exit(1);
2419 if (loadvm && preconfig_requested) {
2420 error_report("'preconfig' and 'loadvm' options are "
2421 "mutually exclusive");
2422 exit(EXIT_FAILURE);
2424 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2425 error_report("'preconfig' supports '-incoming defer' only");
2426 exit(EXIT_FAILURE);
2429 #ifdef CONFIG_CURSES
2430 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2431 error_report("curses display cannot be used with -daemonize");
2432 exit(1);
2434 #endif
2437 static void qemu_process_sugar_options(void)
2439 if (mem_prealloc) {
2440 char *val;
2442 val = g_strdup_printf("%d",
2443 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2444 object_register_sugar_prop("memory-backend", "prealloc-threads", val,
2445 false);
2446 g_free(val);
2447 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2450 if (watchdog) {
2451 int i = select_watchdog(watchdog);
2452 if (i > 0)
2453 exit (i == 1 ? 1 : 0);
2457 /* -action processing */
2460 * Process all the -action parameters parsed from cmdline.
2462 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2464 Error *local_err = NULL;
2465 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2466 QObject *ret = NULL;
2467 qmp_marshal_set_action(qdict, &ret, &local_err);
2468 qobject_unref(ret);
2469 qobject_unref(qdict);
2470 if (local_err) {
2471 error_propagate(errp, local_err);
2472 return 1;
2474 return 0;
2477 static void qemu_process_early_options(void)
2479 #ifdef CONFIG_SECCOMP
2480 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2481 if (olist) {
2482 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2484 #endif
2486 qemu_opts_foreach(qemu_find_opts("name"),
2487 parse_name, NULL, &error_fatal);
2489 if (qemu_opts_foreach(qemu_find_opts("action"),
2490 process_runstate_actions, NULL, &error_fatal)) {
2491 exit(1);
2494 #ifndef _WIN32
2495 qemu_opts_foreach(qemu_find_opts("add-fd"),
2496 parse_add_fd, NULL, &error_fatal);
2498 qemu_opts_foreach(qemu_find_opts("add-fd"),
2499 cleanup_add_fd, NULL, &error_fatal);
2500 #endif
2502 /* Open the logfile at this point and set the log mask if necessary. */
2503 if (log_file) {
2504 qemu_set_log_filename(log_file, &error_fatal);
2506 if (log_mask) {
2507 int mask;
2508 mask = qemu_str_to_log_mask(log_mask);
2509 if (!mask) {
2510 qemu_print_log_usage(stdout);
2511 exit(1);
2513 qemu_set_log(mask);
2514 } else {
2515 qemu_set_log(0);
2518 qemu_add_default_firmwarepath();
2521 static void qemu_process_help_options(void)
2524 * Check for -cpu help and -device help before we call select_machine(),
2525 * which will return an error if the architecture has no default machine
2526 * type and the user did not specify one, so that the user doesn't need
2527 * to say '-cpu help -machine something'.
2529 if (cpu_option && is_help_option(cpu_option)) {
2530 list_cpus(cpu_option);
2531 exit(0);
2534 if (qemu_opts_foreach(qemu_find_opts("device"),
2535 device_help_func, NULL, NULL)) {
2536 exit(0);
2539 /* -L help lists the data directories and exits. */
2540 if (list_data_dirs) {
2541 qemu_list_data_dirs();
2542 exit(0);
2546 static void qemu_maybe_daemonize(const char *pid_file)
2548 Error *err = NULL;
2550 os_daemonize();
2551 rcu_disable_atfork();
2553 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2554 error_reportf_err(err, "cannot create PID file: ");
2555 exit(1);
2558 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2559 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2562 static void qemu_init_displays(void)
2564 DisplayState *ds;
2566 /* init local displays */
2567 ds = init_displaystate();
2568 qemu_display_init(ds, &dpy);
2570 /* must be after terminal init, SDL library changes signal handlers */
2571 os_setup_signal_handling();
2573 /* init remote displays */
2574 #ifdef CONFIG_VNC
2575 qemu_opts_foreach(qemu_find_opts("vnc"),
2576 vnc_init_func, NULL, &error_fatal);
2577 #endif
2579 if (using_spice) {
2580 qemu_spice.display_init();
2584 static void qemu_init_board(void)
2586 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2588 if (machine_class->default_ram_id && current_machine->ram_size &&
2589 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2590 create_default_memdev(current_machine, mem_path);
2593 /* process plugin before CPUs are created, but once -smp has been parsed */
2594 qemu_plugin_load_list(&plugin_list, &error_fatal);
2596 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2597 machine_run_board_init(current_machine);
2599 drive_check_orphaned();
2601 realtime_init();
2603 if (hax_enabled()) {
2604 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2605 hax_sync_vcpus();
2609 static void qemu_create_cli_devices(void)
2611 soundhw_init();
2613 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2614 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2616 /* init USB devices */
2617 if (machine_usb(current_machine)) {
2618 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2619 exit(1);
2622 /* init generic devices */
2623 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2624 qemu_opts_foreach(qemu_find_opts("device"),
2625 device_init_func, NULL, &error_fatal);
2626 rom_reset_order_override();
2629 static void qemu_machine_creation_done(void)
2631 MachineState *machine = MACHINE(qdev_get_machine());
2633 /* Did we create any drives that we failed to create a device for? */
2634 drive_check_orphaned();
2636 /* Don't warn about the default network setup that you get if
2637 * no command line -net or -netdev options are specified. There
2638 * are two cases that we would otherwise complain about:
2639 * (1) board doesn't support a NIC but the implicit "-net nic"
2640 * requested one
2641 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2642 * sets up a nic that isn't connected to anything.
2644 if (!default_net && (!qtest_enabled() || has_defaults)) {
2645 net_check_clients();
2648 qdev_prop_check_globals();
2650 qdev_machine_creation_done();
2652 if (machine->cgs) {
2654 * Verify that Confidential Guest Support has actually been initialized
2656 assert(machine->cgs->ready);
2659 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2660 exit(1);
2664 void qmp_x_exit_preconfig(Error **errp)
2666 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2667 error_setg(errp, "The command is permitted only before machine initialization");
2668 return;
2671 qemu_init_board();
2672 qemu_create_cli_devices();
2673 qemu_machine_creation_done();
2675 if (loadvm) {
2676 Error *local_err = NULL;
2677 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2678 error_report_err(local_err);
2679 autostart = 0;
2680 exit(1);
2683 if (replay_mode != REPLAY_MODE_NONE) {
2684 replay_vmstate_init();
2687 if (incoming) {
2688 Error *local_err = NULL;
2689 if (strcmp(incoming, "defer") != 0) {
2690 qmp_migrate_incoming(incoming, &local_err);
2691 if (local_err) {
2692 error_reportf_err(local_err, "-incoming %s: ", incoming);
2693 exit(1);
2696 } else if (autostart) {
2697 qmp_cont(NULL);
2701 void qemu_init(int argc, char **argv, char **envp)
2703 QemuOpts *opts;
2704 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2705 QemuOptsList *olist;
2706 int optind;
2707 const char *optarg;
2708 MachineClass *machine_class;
2709 bool userconfig = true;
2710 FILE *vmstate_dump_file = NULL;
2712 qemu_add_opts(&qemu_drive_opts);
2713 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2714 qemu_add_drive_opts(&qemu_common_drive_opts);
2715 qemu_add_drive_opts(&qemu_drive_opts);
2716 qemu_add_drive_opts(&bdrv_runtime_opts);
2717 qemu_add_opts(&qemu_chardev_opts);
2718 qemu_add_opts(&qemu_device_opts);
2719 qemu_add_opts(&qemu_netdev_opts);
2720 qemu_add_opts(&qemu_nic_opts);
2721 qemu_add_opts(&qemu_net_opts);
2722 qemu_add_opts(&qemu_rtc_opts);
2723 qemu_add_opts(&qemu_global_opts);
2724 qemu_add_opts(&qemu_mon_opts);
2725 qemu_add_opts(&qemu_trace_opts);
2726 qemu_plugin_add_opts();
2727 qemu_add_opts(&qemu_option_rom_opts);
2728 qemu_add_opts(&qemu_accel_opts);
2729 qemu_add_opts(&qemu_mem_opts);
2730 qemu_add_opts(&qemu_smp_opts);
2731 qemu_add_opts(&qemu_boot_opts);
2732 qemu_add_opts(&qemu_add_fd_opts);
2733 qemu_add_opts(&qemu_object_opts);
2734 qemu_add_opts(&qemu_tpmdev_opts);
2735 qemu_add_opts(&qemu_overcommit_opts);
2736 qemu_add_opts(&qemu_msg_opts);
2737 qemu_add_opts(&qemu_name_opts);
2738 qemu_add_opts(&qemu_numa_opts);
2739 qemu_add_opts(&qemu_icount_opts);
2740 qemu_add_opts(&qemu_semihosting_config_opts);
2741 qemu_add_opts(&qemu_fw_cfg_opts);
2742 qemu_add_opts(&qemu_action_opts);
2743 module_call_init(MODULE_INIT_OPTS);
2745 error_init(argv[0]);
2746 qemu_init_exec_dir(argv[0]);
2748 #ifdef CONFIG_MODULES
2749 module_init_info(qemu_modinfo);
2750 module_allow_arch(TARGET_NAME);
2751 #endif
2753 qemu_init_subsystems();
2755 /* first pass of option parsing */
2756 optind = 1;
2757 while (optind < argc) {
2758 if (argv[optind][0] != '-') {
2759 /* disk image */
2760 optind++;
2761 } else {
2762 const QEMUOption *popt;
2764 popt = lookup_opt(argc, argv, &optarg, &optind);
2765 switch (popt->index) {
2766 case QEMU_OPTION_nouserconfig:
2767 userconfig = false;
2768 break;
2773 machine_opts_dict = qdict_new();
2774 if (userconfig) {
2775 qemu_read_default_config_file(&error_fatal);
2778 /* second pass of option parsing */
2779 optind = 1;
2780 for(;;) {
2781 if (optind >= argc)
2782 break;
2783 if (argv[optind][0] != '-') {
2784 loc_set_cmdline(argv, optind, 1);
2785 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2786 } else {
2787 const QEMUOption *popt;
2789 popt = lookup_opt(argc, argv, &optarg, &optind);
2790 if (!(popt->arch_mask & arch_type)) {
2791 error_report("Option not supported for this target");
2792 exit(1);
2794 switch(popt->index) {
2795 case QEMU_OPTION_cpu:
2796 /* hw initialization will check this */
2797 cpu_option = optarg;
2798 break;
2799 case QEMU_OPTION_hda:
2800 case QEMU_OPTION_hdb:
2801 case QEMU_OPTION_hdc:
2802 case QEMU_OPTION_hdd:
2803 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2804 HD_OPTS);
2805 break;
2806 case QEMU_OPTION_blockdev:
2808 Visitor *v;
2809 BlockdevOptionsQueueEntry *bdo;
2811 v = qobject_input_visitor_new_str(optarg, "driver",
2812 &error_fatal);
2814 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2815 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2816 &error_fatal);
2817 visit_free(v);
2818 loc_save(&bdo->loc);
2819 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2820 break;
2822 case QEMU_OPTION_drive:
2823 if (drive_def(optarg) == NULL) {
2824 exit(1);
2826 break;
2827 case QEMU_OPTION_set:
2828 qemu_set_option(optarg, &error_fatal);
2829 break;
2830 case QEMU_OPTION_global:
2831 if (qemu_global_option(optarg) != 0)
2832 exit(1);
2833 break;
2834 case QEMU_OPTION_mtdblock:
2835 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2836 break;
2837 case QEMU_OPTION_sd:
2838 drive_add(IF_SD, -1, optarg, SD_OPTS);
2839 break;
2840 case QEMU_OPTION_pflash:
2841 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2842 break;
2843 case QEMU_OPTION_snapshot:
2845 Error *blocker = NULL;
2846 snapshot = 1;
2847 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2848 "-snapshot");
2849 replay_add_blocker(blocker);
2851 break;
2852 case QEMU_OPTION_numa:
2853 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2854 optarg, true);
2855 if (!opts) {
2856 exit(1);
2858 break;
2859 case QEMU_OPTION_display:
2860 parse_display(optarg);
2861 break;
2862 case QEMU_OPTION_nographic:
2863 qdict_put_str(machine_opts_dict, "graphics", "off");
2864 nographic = true;
2865 dpy.type = DISPLAY_TYPE_NONE;
2866 break;
2867 case QEMU_OPTION_curses:
2868 #ifdef CONFIG_CURSES
2869 dpy.type = DISPLAY_TYPE_CURSES;
2870 #else
2871 error_report("curses or iconv support is disabled");
2872 exit(1);
2873 #endif
2874 break;
2875 case QEMU_OPTION_portrait:
2876 graphic_rotate = 90;
2877 break;
2878 case QEMU_OPTION_rotate:
2879 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2880 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2881 graphic_rotate != 180 && graphic_rotate != 270) {
2882 error_report("only 90, 180, 270 deg rotation is available");
2883 exit(1);
2885 break;
2886 case QEMU_OPTION_kernel:
2887 qdict_put_str(machine_opts_dict, "kernel", optarg);
2888 break;
2889 case QEMU_OPTION_initrd:
2890 qdict_put_str(machine_opts_dict, "initrd", optarg);
2891 break;
2892 case QEMU_OPTION_append:
2893 qdict_put_str(machine_opts_dict, "append", optarg);
2894 break;
2895 case QEMU_OPTION_dtb:
2896 qdict_put_str(machine_opts_dict, "dtb", optarg);
2897 break;
2898 case QEMU_OPTION_cdrom:
2899 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2900 break;
2901 case QEMU_OPTION_boot:
2902 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2903 optarg, true);
2904 if (!opts) {
2905 exit(1);
2907 break;
2908 case QEMU_OPTION_fda:
2909 case QEMU_OPTION_fdb:
2910 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2911 optarg, FD_OPTS);
2912 break;
2913 case QEMU_OPTION_no_fd_bootchk:
2914 fd_bootchk = 0;
2915 break;
2916 case QEMU_OPTION_netdev:
2917 default_net = 0;
2918 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2919 exit(1);
2921 break;
2922 case QEMU_OPTION_nic:
2923 default_net = 0;
2924 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2925 exit(1);
2927 break;
2928 case QEMU_OPTION_net:
2929 default_net = 0;
2930 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2931 exit(1);
2933 break;
2934 #ifdef CONFIG_LIBISCSI
2935 case QEMU_OPTION_iscsi:
2936 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2937 optarg, false);
2938 if (!opts) {
2939 exit(1);
2941 break;
2942 #endif
2943 case QEMU_OPTION_audio_help:
2944 audio_legacy_help();
2945 exit (0);
2946 break;
2947 case QEMU_OPTION_audiodev:
2948 audio_parse_option(optarg);
2949 break;
2950 case QEMU_OPTION_soundhw:
2951 select_soundhw (optarg);
2952 break;
2953 case QEMU_OPTION_h:
2954 help(0);
2955 break;
2956 case QEMU_OPTION_version:
2957 version();
2958 exit(0);
2959 break;
2960 case QEMU_OPTION_m:
2961 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2962 optarg, true);
2963 if (!opts) {
2964 exit(EXIT_FAILURE);
2966 break;
2967 #ifdef CONFIG_TPM
2968 case QEMU_OPTION_tpmdev:
2969 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2970 exit(1);
2972 break;
2973 #endif
2974 case QEMU_OPTION_mempath:
2975 mem_path = optarg;
2976 break;
2977 case QEMU_OPTION_mem_prealloc:
2978 mem_prealloc = 1;
2979 break;
2980 case QEMU_OPTION_d:
2981 log_mask = optarg;
2982 break;
2983 case QEMU_OPTION_D:
2984 log_file = optarg;
2985 break;
2986 case QEMU_OPTION_DFILTER:
2987 qemu_set_dfilter_ranges(optarg, &error_fatal);
2988 break;
2989 case QEMU_OPTION_seed:
2990 qemu_guest_random_seed_main(optarg, &error_fatal);
2991 break;
2992 case QEMU_OPTION_s:
2993 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2994 break;
2995 case QEMU_OPTION_gdb:
2996 add_device_config(DEV_GDB, optarg);
2997 break;
2998 case QEMU_OPTION_L:
2999 if (is_help_option(optarg)) {
3000 list_data_dirs = true;
3001 } else {
3002 qemu_add_data_dir(g_strdup(optarg));
3004 break;
3005 case QEMU_OPTION_bios:
3006 qdict_put_str(machine_opts_dict, "firmware", optarg);
3007 break;
3008 case QEMU_OPTION_singlestep:
3009 singlestep = 1;
3010 break;
3011 case QEMU_OPTION_S:
3012 autostart = 0;
3013 break;
3014 case QEMU_OPTION_k:
3015 keyboard_layout = optarg;
3016 break;
3017 case QEMU_OPTION_vga:
3018 vga_model = optarg;
3019 default_vga = 0;
3020 break;
3021 case QEMU_OPTION_g:
3023 const char *p;
3024 int w, h, depth;
3025 p = optarg;
3026 w = strtol(p, (char **)&p, 10);
3027 if (w <= 0) {
3028 graphic_error:
3029 error_report("invalid resolution or depth");
3030 exit(1);
3032 if (*p != 'x')
3033 goto graphic_error;
3034 p++;
3035 h = strtol(p, (char **)&p, 10);
3036 if (h <= 0)
3037 goto graphic_error;
3038 if (*p == 'x') {
3039 p++;
3040 depth = strtol(p, (char **)&p, 10);
3041 if (depth != 1 && depth != 2 && depth != 4 &&
3042 depth != 8 && depth != 15 && depth != 16 &&
3043 depth != 24 && depth != 32)
3044 goto graphic_error;
3045 } else if (*p == '\0') {
3046 depth = graphic_depth;
3047 } else {
3048 goto graphic_error;
3051 graphic_width = w;
3052 graphic_height = h;
3053 graphic_depth = depth;
3055 break;
3056 case QEMU_OPTION_echr:
3058 char *r;
3059 term_escape_char = strtol(optarg, &r, 0);
3060 if (r == optarg)
3061 printf("Bad argument to echr\n");
3062 break;
3064 case QEMU_OPTION_monitor:
3065 default_monitor = 0;
3066 if (strncmp(optarg, "none", 4)) {
3067 monitor_parse(optarg, "readline", false);
3069 break;
3070 case QEMU_OPTION_qmp:
3071 monitor_parse(optarg, "control", false);
3072 default_monitor = 0;
3073 break;
3074 case QEMU_OPTION_qmp_pretty:
3075 monitor_parse(optarg, "control", true);
3076 default_monitor = 0;
3077 break;
3078 case QEMU_OPTION_mon:
3079 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3080 true);
3081 if (!opts) {
3082 exit(1);
3084 default_monitor = 0;
3085 break;
3086 case QEMU_OPTION_chardev:
3087 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3088 optarg, true);
3089 if (!opts) {
3090 exit(1);
3092 break;
3093 case QEMU_OPTION_fsdev:
3094 olist = qemu_find_opts("fsdev");
3095 if (!olist) {
3096 error_report("fsdev support is disabled");
3097 exit(1);
3099 opts = qemu_opts_parse_noisily(olist, optarg, true);
3100 if (!opts) {
3101 exit(1);
3103 break;
3104 case QEMU_OPTION_virtfs: {
3105 QemuOpts *fsdev;
3106 QemuOpts *device;
3107 const char *writeout, *sock_fd, *socket, *path, *security_model,
3108 *multidevs;
3110 olist = qemu_find_opts("virtfs");
3111 if (!olist) {
3112 error_report("virtfs support is disabled");
3113 exit(1);
3115 opts = qemu_opts_parse_noisily(olist, optarg, true);
3116 if (!opts) {
3117 exit(1);
3120 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3121 qemu_opt_get(opts, "mount_tag") == NULL) {
3122 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3123 exit(1);
3125 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3126 qemu_opts_id(opts) ?:
3127 qemu_opt_get(opts, "mount_tag"),
3128 1, NULL);
3129 if (!fsdev) {
3130 error_report("duplicate or invalid fsdev id: %s",
3131 qemu_opt_get(opts, "mount_tag"));
3132 exit(1);
3135 writeout = qemu_opt_get(opts, "writeout");
3136 if (writeout) {
3137 #ifdef CONFIG_SYNC_FILE_RANGE
3138 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3139 #else
3140 error_report("writeout=immediate not supported "
3141 "on this platform");
3142 exit(1);
3143 #endif
3145 qemu_opt_set(fsdev, "fsdriver",
3146 qemu_opt_get(opts, "fsdriver"), &error_abort);
3147 path = qemu_opt_get(opts, "path");
3148 if (path) {
3149 qemu_opt_set(fsdev, "path", path, &error_abort);
3151 security_model = qemu_opt_get(opts, "security_model");
3152 if (security_model) {
3153 qemu_opt_set(fsdev, "security_model", security_model,
3154 &error_abort);
3156 socket = qemu_opt_get(opts, "socket");
3157 if (socket) {
3158 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3160 sock_fd = qemu_opt_get(opts, "sock_fd");
3161 if (sock_fd) {
3162 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3165 qemu_opt_set_bool(fsdev, "readonly",
3166 qemu_opt_get_bool(opts, "readonly", 0),
3167 &error_abort);
3168 multidevs = qemu_opt_get(opts, "multidevs");
3169 if (multidevs) {
3170 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3172 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3173 &error_abort);
3174 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3175 qemu_opt_set(device, "fsdev",
3176 qemu_opts_id(fsdev), &error_abort);
3177 qemu_opt_set(device, "mount_tag",
3178 qemu_opt_get(opts, "mount_tag"), &error_abort);
3179 break;
3181 case QEMU_OPTION_serial:
3182 add_device_config(DEV_SERIAL, optarg);
3183 default_serial = 0;
3184 if (strncmp(optarg, "mon:", 4) == 0) {
3185 default_monitor = 0;
3187 break;
3188 case QEMU_OPTION_watchdog:
3189 if (watchdog) {
3190 error_report("only one watchdog option may be given");
3191 exit(1);
3193 watchdog = optarg;
3194 break;
3195 case QEMU_OPTION_action:
3196 olist = qemu_find_opts("action");
3197 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3198 exit(1);
3200 break;
3201 case QEMU_OPTION_watchdog_action:
3202 if (select_watchdog_action(optarg) == -1) {
3203 error_report("unknown -watchdog-action parameter");
3204 exit(1);
3206 break;
3207 case QEMU_OPTION_parallel:
3208 add_device_config(DEV_PARALLEL, optarg);
3209 default_parallel = 0;
3210 if (strncmp(optarg, "mon:", 4) == 0) {
3211 default_monitor = 0;
3213 break;
3214 case QEMU_OPTION_debugcon:
3215 add_device_config(DEV_DEBUGCON, optarg);
3216 break;
3217 case QEMU_OPTION_loadvm:
3218 loadvm = optarg;
3219 break;
3220 case QEMU_OPTION_full_screen:
3221 dpy.has_full_screen = true;
3222 dpy.full_screen = true;
3223 break;
3224 case QEMU_OPTION_alt_grab:
3225 alt_grab = 1;
3226 break;
3227 case QEMU_OPTION_ctrl_grab:
3228 ctrl_grab = 1;
3229 break;
3230 case QEMU_OPTION_no_quit:
3231 dpy.has_window_close = true;
3232 dpy.window_close = false;
3233 warn_report("-no-quit is deprecated, please use "
3234 "-display ...,window-close=off instead.");
3235 break;
3236 case QEMU_OPTION_sdl:
3237 #ifdef CONFIG_SDL
3238 dpy.type = DISPLAY_TYPE_SDL;
3239 break;
3240 #else
3241 error_report("SDL support is disabled");
3242 exit(1);
3243 #endif
3244 case QEMU_OPTION_pidfile:
3245 pid_file = optarg;
3246 break;
3247 case QEMU_OPTION_win2k_hack:
3248 win2k_install_hack = 1;
3249 break;
3250 case QEMU_OPTION_acpitable:
3251 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3252 optarg, true);
3253 if (!opts) {
3254 exit(1);
3256 acpi_table_add(opts, &error_fatal);
3257 break;
3258 case QEMU_OPTION_smbios:
3259 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3260 optarg, false);
3261 if (!opts) {
3262 exit(1);
3264 smbios_entry_add(opts, &error_fatal);
3265 break;
3266 case QEMU_OPTION_fwcfg:
3267 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3268 optarg, true);
3269 if (opts == NULL) {
3270 exit(1);
3272 break;
3273 case QEMU_OPTION_preconfig:
3274 preconfig_requested = true;
3275 break;
3276 case QEMU_OPTION_enable_kvm:
3277 qdict_put_str(machine_opts_dict, "accel", "kvm");
3278 break;
3279 case QEMU_OPTION_M:
3280 case QEMU_OPTION_machine:
3282 bool help;
3284 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3285 if (help) {
3286 machine_help_func(machine_opts_dict);
3287 exit(EXIT_SUCCESS);
3289 break;
3291 case QEMU_OPTION_accel:
3292 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3293 optarg, true);
3294 optarg = qemu_opt_get(accel_opts, "accel");
3295 if (!optarg || is_help_option(optarg)) {
3296 printf("Accelerators supported in QEMU binary:\n");
3297 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3298 false);
3299 for (el = accel_list; el; el = el->next) {
3300 gchar *typename = g_strdup(object_class_get_name(
3301 OBJECT_CLASS(el->data)));
3302 /* omit qtest which is used for tests only */
3303 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3304 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3305 gchar **optname = g_strsplit(typename,
3306 ACCEL_CLASS_SUFFIX, 0);
3307 printf("%s\n", optname[0]);
3308 g_strfreev(optname);
3310 g_free(typename);
3312 g_slist_free(accel_list);
3313 exit(0);
3315 break;
3316 case QEMU_OPTION_usb:
3317 qdict_put_str(machine_opts_dict, "usb", "on");
3318 break;
3319 case QEMU_OPTION_usbdevice:
3320 qdict_put_str(machine_opts_dict, "usb", "on");
3321 add_device_config(DEV_USB, optarg);
3322 break;
3323 case QEMU_OPTION_device:
3324 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3325 optarg, true)) {
3326 exit(1);
3328 break;
3329 case QEMU_OPTION_smp:
3330 machine_parse_property_opt(qemu_find_opts("smp-opts"), "smp", optarg, &error_fatal);
3331 break;
3332 case QEMU_OPTION_vnc:
3333 vnc_parse(optarg);
3334 break;
3335 case QEMU_OPTION_no_acpi:
3336 qdict_put_str(machine_opts_dict, "acpi", "off");
3337 break;
3338 case QEMU_OPTION_no_hpet:
3339 qdict_put_str(machine_opts_dict, "hpet", "off");
3340 break;
3341 case QEMU_OPTION_no_reboot:
3342 olist = qemu_find_opts("action");
3343 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3344 break;
3345 case QEMU_OPTION_no_shutdown:
3346 olist = qemu_find_opts("action");
3347 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3348 break;
3349 case QEMU_OPTION_uuid:
3350 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3351 error_report("failed to parse UUID string: wrong format");
3352 exit(1);
3354 qemu_uuid_set = true;
3355 break;
3356 case QEMU_OPTION_option_rom:
3357 if (nb_option_roms >= MAX_OPTION_ROMS) {
3358 error_report("too many option ROMs");
3359 exit(1);
3361 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3362 optarg, true);
3363 if (!opts) {
3364 exit(1);
3366 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3367 option_rom[nb_option_roms].bootindex =
3368 qemu_opt_get_number(opts, "bootindex", -1);
3369 if (!option_rom[nb_option_roms].name) {
3370 error_report("Option ROM file is not specified");
3371 exit(1);
3373 nb_option_roms++;
3374 break;
3375 case QEMU_OPTION_semihosting:
3376 qemu_semihosting_enable();
3377 break;
3378 case QEMU_OPTION_semihosting_config:
3379 if (qemu_semihosting_config_options(optarg) != 0) {
3380 exit(1);
3382 break;
3383 case QEMU_OPTION_name:
3384 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3385 optarg, true);
3386 if (!opts) {
3387 exit(1);
3389 /* Capture guest name if -msg guest-name is used later */
3390 error_guest_name = qemu_opt_get(opts, "guest");
3391 break;
3392 case QEMU_OPTION_prom_env:
3393 if (nb_prom_envs >= MAX_PROM_ENVS) {
3394 error_report("too many prom variables");
3395 exit(1);
3397 prom_envs[nb_prom_envs] = optarg;
3398 nb_prom_envs++;
3399 break;
3400 case QEMU_OPTION_old_param:
3401 old_param = 1;
3402 break;
3403 case QEMU_OPTION_rtc:
3404 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3405 false);
3406 if (!opts) {
3407 exit(1);
3409 break;
3410 case QEMU_OPTION_icount:
3411 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3412 optarg, true);
3413 if (!icount_opts) {
3414 exit(1);
3416 break;
3417 case QEMU_OPTION_incoming:
3418 if (!incoming) {
3419 runstate_set(RUN_STATE_INMIGRATE);
3421 incoming = optarg;
3422 break;
3423 case QEMU_OPTION_only_migratable:
3424 only_migratable = 1;
3425 break;
3426 case QEMU_OPTION_nodefaults:
3427 has_defaults = 0;
3428 break;
3429 case QEMU_OPTION_xen_domid:
3430 if (!(xen_available())) {
3431 error_report("Option not supported for this target");
3432 exit(1);
3434 xen_domid = atoi(optarg);
3435 break;
3436 case QEMU_OPTION_xen_attach:
3437 if (!(xen_available())) {
3438 error_report("Option not supported for this target");
3439 exit(1);
3441 xen_mode = XEN_ATTACH;
3442 break;
3443 case QEMU_OPTION_xen_domid_restrict:
3444 if (!(xen_available())) {
3445 error_report("Option not supported for this target");
3446 exit(1);
3448 xen_domid_restrict = true;
3449 break;
3450 case QEMU_OPTION_trace:
3451 trace_opt_parse(optarg);
3452 break;
3453 case QEMU_OPTION_plugin:
3454 qemu_plugin_opt_parse(optarg, &plugin_list);
3455 break;
3456 case QEMU_OPTION_readconfig:
3457 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3458 break;
3459 case QEMU_OPTION_spice:
3460 olist = qemu_find_opts_err("spice", NULL);
3461 if (!olist) {
3462 error_report("spice support is disabled");
3463 exit(1);
3465 opts = qemu_opts_parse_noisily(olist, optarg, false);
3466 if (!opts) {
3467 exit(1);
3469 display_remote++;
3470 break;
3471 case QEMU_OPTION_writeconfig:
3473 FILE *fp;
3474 warn_report("-writeconfig is deprecated and will go away without a replacement");
3475 if (strcmp(optarg, "-") == 0) {
3476 fp = stdout;
3477 } else {
3478 fp = fopen(optarg, "w");
3479 if (fp == NULL) {
3480 error_report("open %s: %s", optarg,
3481 strerror(errno));
3482 exit(1);
3485 qemu_config_write(fp);
3486 if (fp != stdout) {
3487 fclose(fp);
3489 break;
3491 case QEMU_OPTION_qtest:
3492 qtest_chrdev = optarg;
3493 break;
3494 case QEMU_OPTION_qtest_log:
3495 qtest_log = optarg;
3496 break;
3497 case QEMU_OPTION_sandbox:
3498 olist = qemu_find_opts("sandbox");
3499 if (!olist) {
3500 #ifndef CONFIG_SECCOMP
3501 error_report("-sandbox support is not enabled "
3502 "in this QEMU binary");
3503 #endif
3504 exit(1);
3507 opts = qemu_opts_parse_noisily(olist, optarg, true);
3508 if (!opts) {
3509 exit(1);
3511 break;
3512 case QEMU_OPTION_add_fd:
3513 #ifndef _WIN32
3514 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3515 optarg, false);
3516 if (!opts) {
3517 exit(1);
3519 #else
3520 error_report("File descriptor passing is disabled on this "
3521 "platform");
3522 exit(1);
3523 #endif
3524 break;
3525 case QEMU_OPTION_object:
3526 object_option_parse(optarg);
3527 break;
3528 case QEMU_OPTION_overcommit:
3529 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3530 optarg, false);
3531 if (!opts) {
3532 exit(1);
3534 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3535 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3536 break;
3537 case QEMU_OPTION_compat:
3539 CompatPolicy *opts;
3540 Visitor *v;
3542 v = qobject_input_visitor_new_str(optarg, NULL,
3543 &error_fatal);
3545 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3546 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3548 qapi_free_CompatPolicy(opts);
3549 visit_free(v);
3550 break;
3552 case QEMU_OPTION_msg:
3553 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3554 false);
3555 if (!opts) {
3556 exit(1);
3558 configure_msg(opts);
3559 break;
3560 case QEMU_OPTION_dump_vmstate:
3561 if (vmstate_dump_file) {
3562 error_report("only one '-dump-vmstate' "
3563 "option may be given");
3564 exit(1);
3566 vmstate_dump_file = fopen(optarg, "w");
3567 if (vmstate_dump_file == NULL) {
3568 error_report("open %s: %s", optarg, strerror(errno));
3569 exit(1);
3571 break;
3572 case QEMU_OPTION_enable_sync_profile:
3573 qsp_enable();
3574 break;
3575 case QEMU_OPTION_nouserconfig:
3576 /* Nothing to be parsed here. Especially, do not error out below. */
3577 break;
3578 default:
3579 if (os_parse_cmd_args(popt->index, optarg)) {
3580 error_report("Option not supported in this build");
3581 exit(1);
3587 * Clear error location left behind by the loop.
3588 * Best done right after the loop. Do not insert code here!
3590 loc_set_none();
3592 qemu_validate_options(machine_opts_dict);
3593 qemu_process_sugar_options();
3596 * These options affect everything else and should be processed
3597 * before daemonizing.
3599 qemu_process_early_options();
3601 qemu_process_help_options();
3602 qemu_maybe_daemonize(pid_file);
3605 * The trace backend must be initialized after daemonizing.
3606 * trace_init_backends() will call st_init(), which will create the
3607 * trace thread in the parent, and also register st_flush_trace_buffer()
3608 * in atexit(). This function will force the parent to wait for the
3609 * writeout thread to finish, which will not occur, and the parent
3610 * process will be left in the host.
3612 if (!trace_init_backends()) {
3613 exit(1);
3615 trace_init_file();
3617 qemu_init_main_loop(&error_fatal);
3618 cpu_timers_init();
3620 user_register_global_props();
3621 replay_configure(icount_opts);
3623 configure_rtc(qemu_find_opts_singleton("rtc"));
3625 qemu_create_machine(machine_opts_dict);
3627 suspend_mux_open();
3629 qemu_disable_default_devices();
3630 qemu_create_default_devices();
3631 qemu_create_early_backends();
3633 qemu_apply_legacy_machine_options(machine_opts_dict);
3634 qemu_apply_machine_options(machine_opts_dict);
3635 qobject_unref(machine_opts_dict);
3636 phase_advance(PHASE_MACHINE_CREATED);
3639 * Note: uses machine properties such as kernel-irqchip, must run
3640 * after qemu_apply_machine_options.
3642 configure_accelerators(argv[0]);
3643 phase_advance(PHASE_ACCEL_CREATED);
3646 * Beware, QOM objects created before this point miss global and
3647 * compat properties.
3649 * Global properties get set up by qdev_prop_register_global(),
3650 * called from user_register_global_props(), and certain option
3651 * desugaring. Also in CPU feature desugaring (buried in
3652 * parse_cpu_option()), which happens below this point, but may
3653 * only target the CPU type, which can only be created after
3654 * parse_cpu_option() returned the type.
3656 * Machine compat properties: object_set_machine_compat_props().
3657 * Accelerator compat props: object_set_accelerator_compat_props(),
3658 * called from do_configure_accelerator().
3661 machine_class = MACHINE_GET_CLASS(current_machine);
3662 if (!qtest_enabled() && machine_class->deprecation_reason) {
3663 error_report("Machine type '%s' is deprecated: %s",
3664 machine_class->name, machine_class->deprecation_reason);
3668 * Note: creates a QOM object, must run only after global and
3669 * compat properties have been set up.
3671 migration_object_init();
3673 qemu_create_late_backends();
3675 /* parse features once if machine provides default cpu_type */
3676 current_machine->cpu_type = machine_class->default_cpu_type;
3677 if (cpu_option) {
3678 current_machine->cpu_type = parse_cpu_option(cpu_option);
3680 /* NB: for machine none cpu_type could STILL be NULL here! */
3682 qemu_resolve_machine_memdev();
3683 parse_numa_opts(current_machine);
3685 if (vmstate_dump_file) {
3686 /* dump and exit */
3687 dump_vmstate_json_to_file(vmstate_dump_file);
3688 exit(0);
3691 if (!preconfig_requested) {
3692 qmp_x_exit_preconfig(&error_fatal);
3694 qemu_init_displays();
3695 accel_setup_post(current_machine);
3696 os_setup_post();
3697 resume_mux_open();