python/machine: use subprocess.run instead of subprocess.Popen
[qemu/ar7.git] / softmmu / vl.c
blob6054f6f0b951011d7d9f64d2d56d2bcffcbb87f1
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/qerror.h"
125 #include "sysemu/iothread.h"
126 #include "qemu/guest-random.h"
128 #define MAX_VIRTIO_CONSOLES 1
130 typedef struct BlockdevOptionsQueueEntry {
131 BlockdevOptions *bdo;
132 Location loc;
133 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
134 } BlockdevOptionsQueueEntry;
136 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
138 typedef struct ObjectOption {
139 ObjectOptions *opts;
140 QTAILQ_ENTRY(ObjectOption) next;
141 } ObjectOption;
143 static const char *cpu_option;
144 static const char *mem_path;
145 static const char *incoming;
146 static const char *loadvm;
147 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
148 static ram_addr_t maxram_size;
149 static uint64_t ram_slots;
150 static int display_remote;
151 static int snapshot;
152 static bool preconfig_requested;
153 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
154 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
155 static bool nographic = false;
156 static int mem_prealloc; /* force preallocation of physical target memory */
157 static ram_addr_t ram_size;
158 static const char *vga_model = NULL;
159 static DisplayOptions dpy;
160 static int num_serial_hds;
161 static Chardev **serial_hds;
162 static const char *log_mask;
163 static const char *log_file;
164 static bool list_data_dirs;
165 static const char *watchdog;
166 static const char *qtest_chrdev;
167 static const char *qtest_log;
169 static int has_defaults = 1;
170 static int default_serial = 1;
171 static int default_parallel = 1;
172 static int default_monitor = 1;
173 static int default_floppy = 1;
174 static int default_cdrom = 1;
175 static int default_sdcard = 1;
176 static int default_vga = 1;
177 static int default_net = 1;
179 static struct {
180 const char *driver;
181 int *flag;
182 } default_list[] = {
183 { .driver = "isa-serial", .flag = &default_serial },
184 { .driver = "isa-parallel", .flag = &default_parallel },
185 { .driver = "isa-fdc", .flag = &default_floppy },
186 { .driver = "floppy", .flag = &default_floppy },
187 { .driver = "ide-cd", .flag = &default_cdrom },
188 { .driver = "ide-hd", .flag = &default_cdrom },
189 { .driver = "scsi-cd", .flag = &default_cdrom },
190 { .driver = "scsi-hd", .flag = &default_cdrom },
191 { .driver = "VGA", .flag = &default_vga },
192 { .driver = "isa-vga", .flag = &default_vga },
193 { .driver = "cirrus-vga", .flag = &default_vga },
194 { .driver = "isa-cirrus-vga", .flag = &default_vga },
195 { .driver = "vmware-svga", .flag = &default_vga },
196 { .driver = "qxl-vga", .flag = &default_vga },
197 { .driver = "virtio-vga", .flag = &default_vga },
198 { .driver = "ati-vga", .flag = &default_vga },
199 { .driver = "vhost-user-vga", .flag = &default_vga },
202 static QemuOptsList qemu_rtc_opts = {
203 .name = "rtc",
204 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
205 .merge_lists = true,
206 .desc = {
208 .name = "base",
209 .type = QEMU_OPT_STRING,
211 .name = "clock",
212 .type = QEMU_OPT_STRING,
214 .name = "driftfix",
215 .type = QEMU_OPT_STRING,
217 { /* end of list */ }
221 static QemuOptsList qemu_option_rom_opts = {
222 .name = "option-rom",
223 .implied_opt_name = "romfile",
224 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
225 .desc = {
227 .name = "bootindex",
228 .type = QEMU_OPT_NUMBER,
229 }, {
230 .name = "romfile",
231 .type = QEMU_OPT_STRING,
233 { /* end of list */ }
237 static QemuOptsList qemu_machine_opts = {
238 .name = "machine",
239 .implied_opt_name = "type",
240 .merge_lists = true,
241 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
242 .desc = {
244 * no elements => accept any
245 * sanity checking will happen later
246 * when setting machine properties
252 static QemuOptsList qemu_accel_opts = {
253 .name = "accel",
254 .implied_opt_name = "accel",
255 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
256 .desc = {
258 * no elements => accept any
259 * sanity checking will happen later
260 * when setting accelerator properties
266 static QemuOptsList qemu_boot_opts = {
267 .name = "boot-opts",
268 .implied_opt_name = "order",
269 .merge_lists = true,
270 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
271 .desc = {
273 .name = "order",
274 .type = QEMU_OPT_STRING,
275 }, {
276 .name = "once",
277 .type = QEMU_OPT_STRING,
278 }, {
279 .name = "menu",
280 .type = QEMU_OPT_BOOL,
281 }, {
282 .name = "splash",
283 .type = QEMU_OPT_STRING,
284 }, {
285 .name = "splash-time",
286 .type = QEMU_OPT_NUMBER,
287 }, {
288 .name = "reboot-timeout",
289 .type = QEMU_OPT_NUMBER,
290 }, {
291 .name = "strict",
292 .type = QEMU_OPT_BOOL,
294 { /*End of list */ }
298 static QemuOptsList qemu_add_fd_opts = {
299 .name = "add-fd",
300 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
301 .desc = {
303 .name = "fd",
304 .type = QEMU_OPT_NUMBER,
305 .help = "file descriptor of which a duplicate is added to fd set",
307 .name = "set",
308 .type = QEMU_OPT_NUMBER,
309 .help = "ID of the fd set to add fd to",
311 .name = "opaque",
312 .type = QEMU_OPT_STRING,
313 .help = "free-form string used to describe fd",
315 { /* end of list */ }
319 static QemuOptsList qemu_object_opts = {
320 .name = "object",
321 .implied_opt_name = "qom-type",
322 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
323 .desc = {
328 static QemuOptsList qemu_tpmdev_opts = {
329 .name = "tpmdev",
330 .implied_opt_name = "type",
331 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
332 .desc = {
333 /* options are defined in the TPM backends */
334 { /* end of list */ }
338 static QemuOptsList qemu_overcommit_opts = {
339 .name = "overcommit",
340 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
341 .desc = {
343 .name = "mem-lock",
344 .type = QEMU_OPT_BOOL,
347 .name = "cpu-pm",
348 .type = QEMU_OPT_BOOL,
350 { /* end of list */ }
354 static QemuOptsList qemu_msg_opts = {
355 .name = "msg",
356 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
357 .desc = {
359 .name = "timestamp",
360 .type = QEMU_OPT_BOOL,
363 .name = "guest-name",
364 .type = QEMU_OPT_BOOL,
365 .help = "Prepends guest name for error messages but only if "
366 "-name guest is set otherwise option is ignored\n",
368 { /* end of list */ }
372 static QemuOptsList qemu_name_opts = {
373 .name = "name",
374 .implied_opt_name = "guest",
375 .merge_lists = true,
376 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
377 .desc = {
379 .name = "guest",
380 .type = QEMU_OPT_STRING,
381 .help = "Sets the name of the guest.\n"
382 "This name will be displayed in the SDL window caption.\n"
383 "The name will also be used for the VNC server",
384 }, {
385 .name = "process",
386 .type = QEMU_OPT_STRING,
387 .help = "Sets the name of the QEMU process, as shown in top etc",
388 }, {
389 .name = "debug-threads",
390 .type = QEMU_OPT_BOOL,
391 .help = "When enabled, name the individual threads; defaults off.\n"
392 "NOTE: The thread names are for debugging and not a\n"
393 "stable API.",
395 { /* End of list */ }
399 static QemuOptsList qemu_mem_opts = {
400 .name = "memory",
401 .implied_opt_name = "size",
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
403 .merge_lists = true,
404 .desc = {
406 .name = "size",
407 .type = QEMU_OPT_SIZE,
410 .name = "slots",
411 .type = QEMU_OPT_NUMBER,
414 .name = "maxmem",
415 .type = QEMU_OPT_SIZE,
417 { /* end of list */ }
421 static QemuOptsList qemu_icount_opts = {
422 .name = "icount",
423 .implied_opt_name = "shift",
424 .merge_lists = true,
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
426 .desc = {
428 .name = "shift",
429 .type = QEMU_OPT_STRING,
430 }, {
431 .name = "align",
432 .type = QEMU_OPT_BOOL,
433 }, {
434 .name = "sleep",
435 .type = QEMU_OPT_BOOL,
436 }, {
437 .name = "rr",
438 .type = QEMU_OPT_STRING,
439 }, {
440 .name = "rrfile",
441 .type = QEMU_OPT_STRING,
442 }, {
443 .name = "rrsnapshot",
444 .type = QEMU_OPT_STRING,
446 { /* end of list */ }
450 static QemuOptsList qemu_fw_cfg_opts = {
451 .name = "fw_cfg",
452 .implied_opt_name = "name",
453 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
454 .desc = {
456 .name = "name",
457 .type = QEMU_OPT_STRING,
458 .help = "Sets the fw_cfg name of the blob to be inserted",
459 }, {
460 .name = "file",
461 .type = QEMU_OPT_STRING,
462 .help = "Sets the name of the file from which "
463 "the fw_cfg blob will be loaded",
464 }, {
465 .name = "string",
466 .type = QEMU_OPT_STRING,
467 .help = "Sets content of the blob to be inserted from a string",
468 }, {
469 .name = "gen_id",
470 .type = QEMU_OPT_STRING,
471 .help = "Sets id of the object generating the fw_cfg blob "
472 "to be inserted",
474 { /* end of list */ }
478 static QemuOptsList qemu_action_opts = {
479 .name = "action",
480 .merge_lists = true,
481 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
482 .desc = {
484 .name = "shutdown",
485 .type = QEMU_OPT_STRING,
487 .name = "reboot",
488 .type = QEMU_OPT_STRING,
490 .name = "panic",
491 .type = QEMU_OPT_STRING,
493 .name = "watchdog",
494 .type = QEMU_OPT_STRING,
496 { /* end of list */ }
501 * Get machine options
503 * Returns: machine options (never null).
505 static QemuOpts *qemu_get_machine_opts(void)
507 return qemu_find_opts_singleton("machine");
510 const char *qemu_get_vm_name(void)
512 return qemu_name;
515 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
517 const char *driver = qemu_opt_get(opts, "driver");
518 int i;
520 if (!driver)
521 return 0;
522 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
523 if (strcmp(default_list[i].driver, driver) != 0)
524 continue;
525 *(default_list[i].flag) = 0;
527 return 0;
530 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
532 const char *proc_name;
534 if (qemu_opt_get(opts, "debug-threads")) {
535 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
537 qemu_name = qemu_opt_get(opts, "guest");
539 proc_name = qemu_opt_get(opts, "process");
540 if (proc_name) {
541 os_set_proc_name(proc_name);
544 return 0;
547 bool defaults_enabled(void)
549 return has_defaults;
552 #ifndef _WIN32
553 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
555 int fd, dupfd, flags;
556 int64_t fdset_id;
557 const char *fd_opaque = NULL;
558 AddfdInfo *fdinfo;
560 fd = qemu_opt_get_number(opts, "fd", -1);
561 fdset_id = qemu_opt_get_number(opts, "set", -1);
562 fd_opaque = qemu_opt_get(opts, "opaque");
564 if (fd < 0) {
565 error_setg(errp, "fd option is required and must be non-negative");
566 return -1;
569 if (fd <= STDERR_FILENO) {
570 error_setg(errp, "fd cannot be a standard I/O stream");
571 return -1;
575 * All fds inherited across exec() necessarily have FD_CLOEXEC
576 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
578 flags = fcntl(fd, F_GETFD);
579 if (flags == -1 || (flags & FD_CLOEXEC)) {
580 error_setg(errp, "fd is not valid or already in use");
581 return -1;
584 if (fdset_id < 0) {
585 error_setg(errp, "set option is required and must be non-negative");
586 return -1;
589 #ifdef F_DUPFD_CLOEXEC
590 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
591 #else
592 dupfd = dup(fd);
593 if (dupfd != -1) {
594 qemu_set_cloexec(dupfd);
596 #endif
597 if (dupfd == -1) {
598 error_setg(errp, "error duplicating fd: %s", strerror(errno));
599 return -1;
602 /* add the duplicate fd, and optionally the opaque string, to the fd set */
603 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
604 &error_abort);
605 g_free(fdinfo);
607 return 0;
610 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
612 int fd;
614 fd = qemu_opt_get_number(opts, "fd", -1);
615 close(fd);
617 return 0;
619 #endif
621 /***********************************************************/
622 /* QEMU Block devices */
624 #define HD_OPTS "media=disk"
625 #define CDROM_OPTS "media=cdrom"
626 #define FD_OPTS ""
627 #define PFLASH_OPTS ""
628 #define MTD_OPTS ""
629 #define SD_OPTS ""
631 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
633 BlockInterfaceType *block_default_type = opaque;
635 return drive_new(opts, *block_default_type, errp) == NULL;
638 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
640 if (qemu_opt_get(opts, "snapshot") == NULL) {
641 qemu_opt_set(opts, "snapshot", "on", &error_abort);
643 return 0;
646 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
647 int index, const char *optstr)
649 QemuOpts *opts;
650 DriveInfo *dinfo;
652 if (!enable || drive_get_by_index(type, index)) {
653 return;
656 opts = drive_add(type, index, NULL, optstr);
657 if (snapshot) {
658 drive_enable_snapshot(NULL, opts, NULL);
661 dinfo = drive_new(opts, type, &error_abort);
662 dinfo->is_default = true;
666 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
667 MachineClass *machine_class, int snapshot)
670 * If the currently selected machine wishes to override the
671 * units-per-bus property of its default HBA interface type, do so
672 * now.
674 if (machine_class->units_per_default_bus) {
675 override_max_devs(machine_class->block_default_type,
676 machine_class->units_per_default_bus);
679 /* open the virtual block devices */
680 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
681 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
683 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
684 loc_push_restore(&bdo->loc);
685 qmp_blockdev_add(bdo->bdo, &error_fatal);
686 loc_pop(&bdo->loc);
687 qapi_free_BlockdevOptions(bdo->bdo);
688 g_free(bdo);
690 if (snapshot) {
691 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
692 NULL, NULL);
694 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
695 &machine_class->block_default_type, &error_fatal)) {
696 /* We printed help */
697 exit(0);
700 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
701 CDROM_OPTS);
702 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
703 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
707 static QemuOptsList qemu_smp_opts = {
708 .name = "smp-opts",
709 .implied_opt_name = "cpus",
710 .merge_lists = true,
711 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
712 .desc = {
714 .name = "cpus",
715 .type = QEMU_OPT_NUMBER,
716 }, {
717 .name = "sockets",
718 .type = QEMU_OPT_NUMBER,
719 }, {
720 .name = "dies",
721 .type = QEMU_OPT_NUMBER,
722 }, {
723 .name = "cores",
724 .type = QEMU_OPT_NUMBER,
725 }, {
726 .name = "threads",
727 .type = QEMU_OPT_NUMBER,
728 }, {
729 .name = "maxcpus",
730 .type = QEMU_OPT_NUMBER,
732 { /*End of list */ }
736 static void realtime_init(void)
738 if (enable_mlock) {
739 if (os_mlock() < 0) {
740 error_report("locking memory failed");
741 exit(1);
747 static void configure_msg(QemuOpts *opts)
749 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
750 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
754 /***********************************************************/
755 /* USB devices */
757 static int usb_device_add(const char *devname)
759 USBDevice *dev = NULL;
761 if (!machine_usb(current_machine)) {
762 return -1;
765 dev = usbdevice_create(devname);
766 if (!dev)
767 return -1;
769 return 0;
772 static int usb_parse(const char *cmdline)
774 int r;
775 r = usb_device_add(cmdline);
776 if (r < 0) {
777 error_report("could not add USB device '%s'", cmdline);
779 return r;
782 /***********************************************************/
783 /* machine registration */
785 static MachineClass *find_machine(const char *name, GSList *machines)
787 GSList *el;
789 for (el = machines; el; el = el->next) {
790 MachineClass *mc = el->data;
792 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
793 return mc;
797 return NULL;
800 static MachineClass *find_default_machine(GSList *machines)
802 GSList *el;
803 MachineClass *default_machineclass = NULL;
805 for (el = machines; el; el = el->next) {
806 MachineClass *mc = el->data;
808 if (mc->is_default) {
809 assert(default_machineclass == NULL && "Multiple default machines");
810 default_machineclass = mc;
814 return default_machineclass;
817 static int machine_help_func(QemuOpts *opts, MachineState *machine)
819 ObjectProperty *prop;
820 ObjectPropertyIterator iter;
822 if (!qemu_opt_has_help_opt(opts)) {
823 return 0;
826 object_property_iter_init(&iter, OBJECT(machine));
827 while ((prop = object_property_iter_next(&iter))) {
828 if (!prop->set) {
829 continue;
832 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
833 prop->name, prop->type);
834 if (prop->description) {
835 printf(" (%s)\n", prop->description);
836 } else {
837 printf("\n");
841 return 1;
844 static void version(void)
846 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
847 QEMU_COPYRIGHT "\n");
850 static void help(int exitcode)
852 version();
853 printf("usage: %s [options] [disk_image]\n\n"
854 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
855 error_get_progname());
857 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
858 if ((arch_mask) & arch_type) \
859 fputs(opt_help, stdout);
861 #define ARCHHEADING(text, arch_mask) \
862 if ((arch_mask) & arch_type) \
863 puts(stringify(text));
865 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
867 #include "qemu-options.def"
869 printf("\nDuring emulation, the following keys are useful:\n"
870 "ctrl-alt-f toggle full screen\n"
871 "ctrl-alt-n switch to virtual console 'n'\n"
872 "ctrl-alt toggle mouse and keyboard grab\n"
873 "\n"
874 "When using -nographic, press 'ctrl-a h' to get some help.\n"
875 "\n"
876 QEMU_HELP_BOTTOM "\n");
878 exit(exitcode);
881 #define HAS_ARG 0x0001
883 typedef struct QEMUOption {
884 const char *name;
885 int flags;
886 int index;
887 uint32_t arch_mask;
888 } QEMUOption;
890 static const QEMUOption qemu_options[] = {
891 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
893 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
894 { option, opt_arg, opt_enum, arch_mask },
895 #define DEFHEADING(text)
896 #define ARCHHEADING(text, arch_mask)
898 #include "qemu-options.def"
899 { NULL },
902 typedef struct VGAInterfaceInfo {
903 const char *opt_name; /* option name */
904 const char *name; /* human-readable name */
905 /* Class names indicating that support is available.
906 * If no class is specified, the interface is always available */
907 const char *class_names[2];
908 } VGAInterfaceInfo;
910 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
911 [VGA_NONE] = {
912 .opt_name = "none",
913 .name = "no graphic card",
915 [VGA_STD] = {
916 .opt_name = "std",
917 .name = "standard VGA",
918 .class_names = { "VGA", "isa-vga" },
920 [VGA_CIRRUS] = {
921 .opt_name = "cirrus",
922 .name = "Cirrus VGA",
923 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
925 [VGA_VMWARE] = {
926 .opt_name = "vmware",
927 .name = "VMWare SVGA",
928 .class_names = { "vmware-svga" },
930 [VGA_VIRTIO] = {
931 .opt_name = "virtio",
932 .name = "Virtio VGA",
933 .class_names = { "virtio-vga" },
935 [VGA_QXL] = {
936 .opt_name = "qxl",
937 .name = "QXL VGA",
938 .class_names = { "qxl-vga" },
940 [VGA_TCX] = {
941 .opt_name = "tcx",
942 .name = "TCX framebuffer",
943 .class_names = { "sun-tcx" },
945 [VGA_CG3] = {
946 .opt_name = "cg3",
947 .name = "CG3 framebuffer",
948 .class_names = { "cgthree" },
950 [VGA_XENFB] = {
951 .opt_name = "xenfb",
952 .name = "Xen paravirtualized framebuffer",
956 static bool vga_interface_available(VGAInterfaceType t)
958 const VGAInterfaceInfo *ti = &vga_interfaces[t];
960 assert(t < VGA_TYPE_MAX);
961 return !ti->class_names[0] ||
962 module_object_class_by_name(ti->class_names[0]) ||
963 module_object_class_by_name(ti->class_names[1]);
966 static const char *
967 get_default_vga_model(const MachineClass *machine_class)
969 if (machine_class->default_display) {
970 return machine_class->default_display;
971 } else if (vga_interface_available(VGA_CIRRUS)) {
972 return "cirrus";
973 } else if (vga_interface_available(VGA_STD)) {
974 return "std";
977 return NULL;
980 static void select_vgahw(const MachineClass *machine_class, const char *p)
982 const char *opts;
983 int t;
985 if (g_str_equal(p, "help")) {
986 const char *def = get_default_vga_model(machine_class);
988 for (t = 0; t < VGA_TYPE_MAX; t++) {
989 const VGAInterfaceInfo *ti = &vga_interfaces[t];
991 if (vga_interface_available(t) && ti->opt_name) {
992 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
993 g_str_equal(ti->opt_name, def) ? " (default)" : "");
996 exit(0);
999 assert(vga_interface_type == VGA_NONE);
1000 for (t = 0; t < VGA_TYPE_MAX; t++) {
1001 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1002 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1003 if (!vga_interface_available(t)) {
1004 error_report("%s not available", ti->name);
1005 exit(1);
1007 vga_interface_type = t;
1008 break;
1011 if (t == VGA_TYPE_MAX) {
1012 invalid_vga:
1013 error_report("unknown vga type: %s", p);
1014 exit(1);
1016 while (*opts) {
1017 const char *nextopt;
1019 if (strstart(opts, ",retrace=", &nextopt)) {
1020 opts = nextopt;
1021 if (strstart(opts, "dumb", &nextopt))
1022 vga_retrace_method = VGA_RETRACE_DUMB;
1023 else if (strstart(opts, "precise", &nextopt))
1024 vga_retrace_method = VGA_RETRACE_PRECISE;
1025 else goto invalid_vga;
1026 } else goto invalid_vga;
1027 opts = nextopt;
1031 static void parse_display_qapi(const char *optarg)
1033 DisplayOptions *opts;
1034 Visitor *v;
1036 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1038 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1039 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1041 qapi_free_DisplayOptions(opts);
1042 visit_free(v);
1045 DisplayOptions *qmp_query_display_options(Error **errp)
1047 return QAPI_CLONE(DisplayOptions, &dpy);
1050 static void parse_display(const char *p)
1052 const char *opts;
1054 if (is_help_option(p)) {
1055 qemu_display_help();
1056 exit(0);
1059 if (strstart(p, "sdl", &opts)) {
1061 * sdl DisplayType needs hand-crafted parser instead of
1062 * parse_display_qapi() due to some options not in
1063 * DisplayOptions, specifically:
1064 * - frame
1065 * Already deprecated.
1066 * - ctrl_grab + alt_grab
1067 * Not clear yet what happens to them long-term. Should
1068 * replaced by something better or deprecated and dropped.
1070 dpy.type = DISPLAY_TYPE_SDL;
1071 while (*opts) {
1072 const char *nextopt;
1074 if (strstart(opts, ",alt_grab=", &nextopt)) {
1075 opts = nextopt;
1076 if (strstart(opts, "on", &nextopt)) {
1077 alt_grab = 1;
1078 } else if (strstart(opts, "off", &nextopt)) {
1079 alt_grab = 0;
1080 } else {
1081 goto invalid_sdl_args;
1083 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1084 opts = nextopt;
1085 if (strstart(opts, "on", &nextopt)) {
1086 ctrl_grab = 1;
1087 } else if (strstart(opts, "off", &nextopt)) {
1088 ctrl_grab = 0;
1089 } else {
1090 goto invalid_sdl_args;
1092 } else if (strstart(opts, ",window_close=", &nextopt)) {
1093 opts = nextopt;
1094 dpy.has_window_close = true;
1095 if (strstart(opts, "on", &nextopt)) {
1096 dpy.window_close = true;
1097 } else if (strstart(opts, "off", &nextopt)) {
1098 dpy.window_close = false;
1099 } else {
1100 goto invalid_sdl_args;
1102 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1103 opts = nextopt;
1104 dpy.has_show_cursor = true;
1105 if (strstart(opts, "on", &nextopt)) {
1106 dpy.show_cursor = true;
1107 } else if (strstart(opts, "off", &nextopt)) {
1108 dpy.show_cursor = false;
1109 } else {
1110 goto invalid_sdl_args;
1112 } else if (strstart(opts, ",gl=", &nextopt)) {
1113 opts = nextopt;
1114 dpy.has_gl = true;
1115 if (strstart(opts, "on", &nextopt)) {
1116 dpy.gl = DISPLAYGL_MODE_ON;
1117 } else if (strstart(opts, "core", &nextopt)) {
1118 dpy.gl = DISPLAYGL_MODE_CORE;
1119 } else if (strstart(opts, "es", &nextopt)) {
1120 dpy.gl = DISPLAYGL_MODE_ES;
1121 } else if (strstart(opts, "off", &nextopt)) {
1122 dpy.gl = DISPLAYGL_MODE_OFF;
1123 } else {
1124 goto invalid_sdl_args;
1126 } else {
1127 invalid_sdl_args:
1128 error_report("invalid SDL option string");
1129 exit(1);
1131 opts = nextopt;
1133 } else if (strstart(p, "vnc", &opts)) {
1135 * vnc isn't a (local) DisplayType but a protocol for remote
1136 * display access.
1138 if (*opts == '=') {
1139 vnc_parse(opts + 1);
1140 } else {
1141 error_report("VNC requires a display argument vnc=<display>");
1142 exit(1);
1144 } else {
1145 parse_display_qapi(p);
1149 static inline bool nonempty_str(const char *str)
1151 return str && *str;
1154 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1156 gchar *buf;
1157 size_t size;
1158 const char *name, *file, *str, *gen_id;
1159 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1161 if (fw_cfg == NULL) {
1162 error_setg(errp, "fw_cfg device not available");
1163 return -1;
1165 name = qemu_opt_get(opts, "name");
1166 file = qemu_opt_get(opts, "file");
1167 str = qemu_opt_get(opts, "string");
1168 gen_id = qemu_opt_get(opts, "gen_id");
1170 /* we need the name, and exactly one of: file, content string, gen_id */
1171 if (!nonempty_str(name) ||
1172 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1173 error_setg(errp, "name, plus exactly one of file,"
1174 " string and gen_id, are needed");
1175 return -1;
1177 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1178 error_setg(errp, "name too long (max. %d char)",
1179 FW_CFG_MAX_FILE_PATH - 1);
1180 return -1;
1182 if (nonempty_str(gen_id)) {
1184 * In this particular case where the content is populated
1185 * internally, the "etc/" namespace protection is relaxed,
1186 * so do not emit a warning.
1188 } else if (strncmp(name, "opt/", 4) != 0) {
1189 warn_report("externally provided fw_cfg item names "
1190 "should be prefixed with \"opt/\"");
1192 if (nonempty_str(str)) {
1193 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1194 buf = g_memdup(str, size);
1195 } else if (nonempty_str(gen_id)) {
1196 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1197 return -1;
1199 return 0;
1200 } else {
1201 GError *err = NULL;
1202 if (!g_file_get_contents(file, &buf, &size, &err)) {
1203 error_setg(errp, "can't load %s: %s", file, err->message);
1204 g_error_free(err);
1205 return -1;
1208 /* For legacy, keep user files in a specific global order. */
1209 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1210 fw_cfg_add_file(fw_cfg, name, buf, size);
1211 fw_cfg_reset_order_override(fw_cfg);
1212 return 0;
1215 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1217 return qdev_device_help(opts);
1220 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1222 DeviceState *dev;
1224 dev = qdev_device_add(opts, errp);
1225 if (!dev && *errp) {
1226 error_report_err(*errp);
1227 return -1;
1228 } else if (dev) {
1229 object_unref(OBJECT(dev));
1231 return 0;
1234 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1236 Error *local_err = NULL;
1238 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1239 if (local_err) {
1240 error_propagate(errp, local_err);
1241 return -1;
1243 exit(0);
1245 return 0;
1248 #ifdef CONFIG_VIRTFS
1249 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1251 return qemu_fsdev_add(opts, errp);
1253 #endif
1255 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1257 return monitor_init_opts(opts, errp);
1260 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1262 static int monitor_device_index = 0;
1263 QemuOpts *opts;
1264 const char *p;
1265 char label[32];
1267 if (strstart(optarg, "chardev:", &p)) {
1268 snprintf(label, sizeof(label), "%s", p);
1269 } else {
1270 snprintf(label, sizeof(label), "compat_monitor%d",
1271 monitor_device_index);
1272 opts = qemu_chr_parse_compat(label, optarg, true);
1273 if (!opts) {
1274 error_report("parse error: %s", optarg);
1275 exit(1);
1279 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1280 qemu_opt_set(opts, "mode", mode, &error_abort);
1281 qemu_opt_set(opts, "chardev", label, &error_abort);
1282 if (!strcmp(mode, "control")) {
1283 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1284 } else {
1285 assert(pretty == false);
1287 monitor_device_index++;
1290 struct device_config {
1291 enum {
1292 DEV_USB, /* -usbdevice */
1293 DEV_SERIAL, /* -serial */
1294 DEV_PARALLEL, /* -parallel */
1295 DEV_DEBUGCON, /* -debugcon */
1296 DEV_GDB, /* -gdb, -s */
1297 DEV_SCLP, /* s390 sclp */
1298 } type;
1299 const char *cmdline;
1300 Location loc;
1301 QTAILQ_ENTRY(device_config) next;
1304 static QTAILQ_HEAD(, device_config) device_configs =
1305 QTAILQ_HEAD_INITIALIZER(device_configs);
1307 static void add_device_config(int type, const char *cmdline)
1309 struct device_config *conf;
1311 conf = g_malloc0(sizeof(*conf));
1312 conf->type = type;
1313 conf->cmdline = cmdline;
1314 loc_save(&conf->loc);
1315 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1318 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1320 struct device_config *conf;
1321 int rc;
1323 QTAILQ_FOREACH(conf, &device_configs, next) {
1324 if (conf->type != type)
1325 continue;
1326 loc_push_restore(&conf->loc);
1327 rc = func(conf->cmdline);
1328 loc_pop(&conf->loc);
1329 if (rc) {
1330 return rc;
1333 return 0;
1336 static void qemu_disable_default_devices(void)
1338 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1340 qemu_opts_foreach(qemu_find_opts("device"),
1341 default_driver_check, NULL, NULL);
1342 qemu_opts_foreach(qemu_find_opts("global"),
1343 default_driver_check, NULL, NULL);
1345 if (!vga_model && !default_vga) {
1346 vga_interface_type = VGA_DEVICE;
1348 if (!has_defaults || machine_class->no_serial) {
1349 default_serial = 0;
1351 if (!has_defaults || machine_class->no_parallel) {
1352 default_parallel = 0;
1354 if (!has_defaults || machine_class->no_floppy) {
1355 default_floppy = 0;
1357 if (!has_defaults || machine_class->no_cdrom) {
1358 default_cdrom = 0;
1360 if (!has_defaults || machine_class->no_sdcard) {
1361 default_sdcard = 0;
1363 if (!has_defaults) {
1364 default_monitor = 0;
1365 default_net = 0;
1366 default_vga = 0;
1370 static void qemu_create_default_devices(void)
1372 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1374 if (is_daemonized()) {
1375 /* According to documentation and historically, -nographic redirects
1376 * serial port, parallel port and monitor to stdio, which does not work
1377 * with -daemonize. We can redirect these to null instead, but since
1378 * -nographic is legacy, let's just error out.
1379 * We disallow -nographic only if all other ports are not redirected
1380 * explicitly, to not break existing legacy setups which uses
1381 * -nographic _and_ redirects all ports explicitly - this is valid
1382 * usage, -nographic is just a no-op in this case.
1384 if (nographic
1385 && (default_parallel || default_serial || default_monitor)) {
1386 error_report("-nographic cannot be used with -daemonize");
1387 exit(1);
1391 if (nographic) {
1392 if (default_parallel)
1393 add_device_config(DEV_PARALLEL, "null");
1394 if (default_serial && default_monitor) {
1395 add_device_config(DEV_SERIAL, "mon:stdio");
1396 } else {
1397 if (default_serial)
1398 add_device_config(DEV_SERIAL, "stdio");
1399 if (default_monitor)
1400 monitor_parse("stdio", "readline", false);
1402 } else {
1403 if (default_serial)
1404 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1405 if (default_parallel)
1406 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1407 if (default_monitor)
1408 monitor_parse("vc:80Cx24C", "readline", false);
1411 if (default_net) {
1412 QemuOptsList *net = qemu_find_opts("net");
1413 qemu_opts_parse(net, "nic", true, &error_abort);
1414 #ifdef CONFIG_SLIRP
1415 qemu_opts_parse(net, "user", true, &error_abort);
1416 #endif
1419 #if defined(CONFIG_VNC)
1420 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1421 display_remote++;
1423 #endif
1424 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1425 if (!qemu_display_find_default(&dpy)) {
1426 dpy.type = DISPLAY_TYPE_NONE;
1427 #if defined(CONFIG_VNC)
1428 vnc_parse("localhost:0,to=99,id=default");
1429 #endif
1432 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1433 dpy.type = DISPLAY_TYPE_NONE;
1436 /* If no default VGA is requested, the default is "none". */
1437 if (default_vga) {
1438 vga_model = get_default_vga_model(machine_class);
1440 if (vga_model) {
1441 select_vgahw(machine_class, vga_model);
1445 static int serial_parse(const char *devname)
1447 int index = num_serial_hds;
1448 char label[32];
1450 if (strcmp(devname, "none") == 0)
1451 return 0;
1452 snprintf(label, sizeof(label), "serial%d", index);
1453 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1455 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1456 if (!serial_hds[index]) {
1457 error_report("could not connect serial device"
1458 " to character backend '%s'", devname);
1459 return -1;
1461 num_serial_hds++;
1462 return 0;
1465 Chardev *serial_hd(int i)
1467 assert(i >= 0);
1468 if (i < num_serial_hds) {
1469 return serial_hds[i];
1471 return NULL;
1474 static int parallel_parse(const char *devname)
1476 static int index = 0;
1477 char label[32];
1479 if (strcmp(devname, "none") == 0)
1480 return 0;
1481 if (index == MAX_PARALLEL_PORTS) {
1482 error_report("too many parallel ports");
1483 exit(1);
1485 snprintf(label, sizeof(label), "parallel%d", index);
1486 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1487 if (!parallel_hds[index]) {
1488 error_report("could not connect parallel device"
1489 " to character backend '%s'", devname);
1490 return -1;
1492 index++;
1493 return 0;
1496 static int debugcon_parse(const char *devname)
1498 QemuOpts *opts;
1500 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1501 error_report("invalid character backend '%s'", devname);
1502 exit(1);
1504 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1505 if (!opts) {
1506 error_report("already have a debugcon device");
1507 exit(1);
1509 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1510 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1511 return 0;
1514 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1516 const MachineClass *mc1 = a, *mc2 = b;
1517 int res;
1519 if (mc1->family == NULL) {
1520 if (mc2->family == NULL) {
1521 /* Compare standalone machine types against each other; they sort
1522 * in increasing order.
1524 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1525 object_class_get_name(OBJECT_CLASS(mc2)));
1528 /* Standalone machine types sort after families. */
1529 return 1;
1532 if (mc2->family == NULL) {
1533 /* Families sort before standalone machine types. */
1534 return -1;
1537 /* Families sort between each other alphabetically increasingly. */
1538 res = strcmp(mc1->family, mc2->family);
1539 if (res != 0) {
1540 return res;
1543 /* Within the same family, machine types sort in decreasing order. */
1544 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1545 object_class_get_name(OBJECT_CLASS(mc1)));
1548 static MachineClass *machine_parse(const char *name, GSList *machines)
1550 MachineClass *mc;
1551 GSList *el;
1553 if (is_help_option(name)) {
1554 printf("Supported machines are:\n");
1555 machines = g_slist_sort(machines, machine_class_cmp);
1556 for (el = machines; el; el = el->next) {
1557 MachineClass *mc = el->data;
1558 if (mc->alias) {
1559 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1561 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1562 mc->is_default ? " (default)" : "",
1563 mc->deprecation_reason ? " (deprecated)" : "");
1565 exit(0);
1568 mc = find_machine(name, machines);
1569 if (!mc) {
1570 error_report("unsupported machine type");
1571 error_printf("Use -machine help to list supported machines\n");
1572 exit(1);
1574 return mc;
1577 static const char *pid_file;
1578 static Notifier qemu_unlink_pidfile_notifier;
1580 static void qemu_unlink_pidfile(Notifier *n, void *data)
1582 if (pid_file) {
1583 unlink(pid_file);
1587 static const QEMUOption *lookup_opt(int argc, char **argv,
1588 const char **poptarg, int *poptind)
1590 const QEMUOption *popt;
1591 int optind = *poptind;
1592 char *r = argv[optind];
1593 const char *optarg;
1595 loc_set_cmdline(argv, optind, 1);
1596 optind++;
1597 /* Treat --foo the same as -foo. */
1598 if (r[1] == '-')
1599 r++;
1600 popt = qemu_options;
1601 for(;;) {
1602 if (!popt->name) {
1603 error_report("invalid option");
1604 exit(1);
1606 if (!strcmp(popt->name, r + 1))
1607 break;
1608 popt++;
1610 if (popt->flags & HAS_ARG) {
1611 if (optind >= argc) {
1612 error_report("requires an argument");
1613 exit(1);
1615 optarg = argv[optind++];
1616 loc_set_cmdline(argv, optind - 2, 2);
1617 } else {
1618 optarg = NULL;
1621 *poptarg = optarg;
1622 *poptind = optind;
1624 return popt;
1627 static MachineClass *select_machine(void)
1629 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1630 MachineClass *machine_class = find_default_machine(machines);
1631 const char *optarg;
1632 QemuOpts *opts;
1633 Location loc;
1635 loc_push_none(&loc);
1637 opts = qemu_get_machine_opts();
1638 qemu_opts_loc_restore(opts);
1640 optarg = qemu_opt_get(opts, "type");
1641 if (optarg) {
1642 machine_class = machine_parse(optarg, machines);
1645 if (!machine_class) {
1646 error_report("No machine specified, and there is no default");
1647 error_printf("Use -machine help to list supported machines\n");
1648 exit(1);
1651 loc_pop(&loc);
1652 g_slist_free(machines);
1653 return machine_class;
1656 static int object_parse_property_opt(Object *obj,
1657 const char *name, const char *value,
1658 const char *skip, Error **errp)
1660 if (g_str_equal(name, skip)) {
1661 return 0;
1664 if (!object_property_parse(obj, name, value, errp)) {
1665 return -1;
1668 return 0;
1671 static int machine_set_property(void *opaque,
1672 const char *name, const char *value,
1673 Error **errp)
1675 g_autofree char *qom_name = g_strdup(name);
1676 char *p;
1678 for (p = qom_name; *p; p++) {
1679 if (*p == '_') {
1680 *p = '-';
1684 /* Legacy options do not correspond to MachineState properties. */
1685 if (g_str_equal(qom_name, "accel")) {
1686 return 0;
1688 if (g_str_equal(qom_name, "igd-passthru")) {
1689 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value,
1690 false);
1691 return 0;
1693 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
1694 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1695 false);
1696 return 0;
1698 if (g_str_equal(qom_name, "kernel-irqchip")) {
1699 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1700 false);
1701 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value,
1702 false);
1703 return 0;
1706 return object_parse_property_opt(opaque, name, value, "type", errp);
1709 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1711 ObjectOption *opt, *next;
1713 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1714 const char *type = ObjectType_str(opt->opts->qom_type);
1715 if (type_opt_predicate(type)) {
1716 user_creatable_add_qapi(opt->opts, &error_fatal);
1717 qapi_free_ObjectOptions(opt->opts);
1718 QTAILQ_REMOVE(&object_opts, opt, next);
1719 g_free(opt);
1724 static void object_option_parse(const char *optarg)
1726 ObjectOption *opt;
1727 QemuOpts *opts;
1728 const char *type;
1729 Visitor *v;
1731 if (optarg[0] == '{') {
1732 QObject *obj = qobject_from_json(optarg, &error_fatal);
1734 v = qobject_input_visitor_new(obj);
1735 qobject_unref(obj);
1736 } else {
1737 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1738 optarg, true);
1739 if (!opts) {
1740 exit(1);
1743 type = qemu_opt_get(opts, "qom-type");
1744 if (!type) {
1745 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1747 if (user_creatable_print_help(type, opts)) {
1748 exit(0);
1751 v = opts_visitor_new(opts);
1754 opt = g_new0(ObjectOption, 1);
1755 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1756 visit_free(v);
1758 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1762 * Initial object creation happens before all other
1763 * QEMU data types are created. The majority of objects
1764 * can be created at this point. The rng-egd object
1765 * cannot be created here, as it depends on the chardev
1766 * already existing.
1768 static bool object_create_early(const char *type)
1771 * Objects should not be made "delayed" without a reason. If you
1772 * add one, state the reason in a comment!
1775 /* Reason: property "chardev" */
1776 if (g_str_equal(type, "rng-egd") ||
1777 g_str_equal(type, "qtest")) {
1778 return false;
1781 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1782 /* Reason: cryptodev-vhost-user property "chardev" */
1783 if (g_str_equal(type, "cryptodev-vhost-user")) {
1784 return false;
1786 #endif
1788 /* Reason: vhost-user-blk-server property "node-name" */
1789 if (g_str_equal(type, "vhost-user-blk-server")) {
1790 return false;
1793 * Reason: filter-* property "netdev" etc.
1795 if (g_str_equal(type, "filter-buffer") ||
1796 g_str_equal(type, "filter-dump") ||
1797 g_str_equal(type, "filter-mirror") ||
1798 g_str_equal(type, "filter-redirector") ||
1799 g_str_equal(type, "colo-compare") ||
1800 g_str_equal(type, "filter-rewriter") ||
1801 g_str_equal(type, "filter-replay")) {
1802 return false;
1806 * Allocation of large amounts of memory may delay
1807 * chardev initialization for too long, and trigger timeouts
1808 * on software that waits for a monitor socket to be created
1809 * (e.g. libvirt).
1811 if (g_str_has_prefix(type, "memory-backend-")) {
1812 return false;
1815 return true;
1818 static void qemu_apply_machine_options(void)
1820 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1821 QemuOpts *machine_opts = qemu_get_machine_opts();
1822 const char *boot_order = NULL;
1823 const char *boot_once = NULL;
1824 QemuOpts *opts;
1826 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
1827 &error_fatal);
1828 current_machine->ram_size = ram_size;
1829 current_machine->maxram_size = maxram_size;
1830 current_machine->ram_slots = ram_slots;
1832 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1833 if (opts) {
1834 boot_order = qemu_opt_get(opts, "order");
1835 if (boot_order) {
1836 validate_bootdevices(boot_order, &error_fatal);
1839 boot_once = qemu_opt_get(opts, "once");
1840 if (boot_once) {
1841 validate_bootdevices(boot_once, &error_fatal);
1844 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1845 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1848 if (!boot_order) {
1849 boot_order = machine_class->default_boot_order;
1852 current_machine->boot_order = boot_order;
1853 current_machine->boot_once = boot_once;
1855 if (semihosting_enabled() && !semihosting_get_argc()) {
1856 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
1857 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append") ?: "";
1858 /* fall back to the -kernel/-append */
1859 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
1863 static void qemu_create_early_backends(void)
1865 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1867 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
1868 error_report("-alt-grab and -ctrl-grab are only valid "
1869 "for SDL, ignoring option");
1871 if (dpy.has_window_close &&
1872 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
1873 error_report("-no-quit is only valid for GTK and SDL, "
1874 "ignoring option");
1877 qemu_display_early_init(&dpy);
1878 qemu_console_early_init();
1880 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1881 #if defined(CONFIG_OPENGL)
1882 error_report("OpenGL is not supported by the display");
1883 #else
1884 error_report("OpenGL support is disabled");
1885 #endif
1886 exit(1);
1889 object_option_foreach_add(object_create_early);
1891 /* spice needs the timers to be initialized by this point */
1892 /* spice must initialize before audio as it changes the default auiodev */
1893 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1894 qemu_spice.init();
1896 qemu_opts_foreach(qemu_find_opts("chardev"),
1897 chardev_init_func, NULL, &error_fatal);
1899 #ifdef CONFIG_VIRTFS
1900 qemu_opts_foreach(qemu_find_opts("fsdev"),
1901 fsdev_init_func, NULL, &error_fatal);
1902 #endif
1905 * Note: we need to create audio and block backends before
1906 * machine_set_property(), so machine properties can refer to
1907 * them.
1909 configure_blockdev(&bdo_queue, machine_class, snapshot);
1910 audio_init_audiodevs();
1915 * The remainder of object creation happens after the
1916 * creation of chardev, fsdev, net clients and device data types.
1918 static bool object_create_late(const char *type)
1920 return !object_create_early(type);
1923 static void qemu_create_late_backends(void)
1925 if (qtest_chrdev) {
1926 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1929 net_init_clients(&error_fatal);
1931 object_option_foreach_add(object_create_late);
1933 if (tpm_init() < 0) {
1934 exit(1);
1937 qemu_opts_foreach(qemu_find_opts("mon"),
1938 mon_init_func, NULL, &error_fatal);
1940 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1941 exit(1);
1942 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1943 exit(1);
1944 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1945 exit(1);
1947 /* now chardevs have been created we may have semihosting to connect */
1948 qemu_semihosting_connect_chardevs();
1949 qemu_semihosting_console_init();
1952 static bool have_custom_ram_size(void)
1954 QemuOpts *opts = qemu_find_opts_singleton("memory");
1955 return !!qemu_opt_get_size(opts, "size", 0);
1958 static void qemu_resolve_machine_memdev(void)
1960 if (current_machine->ram_memdev_id) {
1961 Object *backend;
1962 ram_addr_t backend_size;
1964 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1965 TYPE_MEMORY_BACKEND, NULL);
1966 if (!backend) {
1967 error_report("Memory backend '%s' not found",
1968 current_machine->ram_memdev_id);
1969 exit(EXIT_FAILURE);
1971 backend_size = object_property_get_uint(backend, "size", &error_abort);
1972 if (have_custom_ram_size() && backend_size != ram_size) {
1973 error_report("Size specified by -m option must match size of "
1974 "explicitly specified 'memory-backend' property");
1975 exit(EXIT_FAILURE);
1977 if (mem_path) {
1978 error_report("'-mem-path' can't be used together with"
1979 "'-machine memory-backend'");
1980 exit(EXIT_FAILURE);
1982 ram_size = backend_size;
1985 if (!xen_enabled()) {
1986 /* On 32-bit hosts, QEMU is limited by virtual address space */
1987 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1988 error_report("at most 2047 MB RAM can be simulated");
1989 exit(1);
1994 static void set_memory_options(MachineClass *mc)
1996 uint64_t sz;
1997 const char *mem_str;
1998 const ram_addr_t default_ram_size = mc->default_ram_size;
1999 QemuOpts *opts = qemu_find_opts_singleton("memory");
2000 Location loc;
2002 loc_push_none(&loc);
2003 qemu_opts_loc_restore(opts);
2005 sz = 0;
2006 mem_str = qemu_opt_get(opts, "size");
2007 if (mem_str) {
2008 if (!*mem_str) {
2009 error_report("missing 'size' option value");
2010 exit(EXIT_FAILURE);
2013 sz = qemu_opt_get_size(opts, "size", ram_size);
2015 /* Fix up legacy suffix-less format */
2016 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2017 uint64_t overflow_check = sz;
2019 sz *= MiB;
2020 if (sz / MiB != overflow_check) {
2021 error_report("too large 'size' option value");
2022 exit(EXIT_FAILURE);
2027 /* backward compatibility behaviour for case "-m 0" */
2028 if (sz == 0) {
2029 sz = default_ram_size;
2032 sz = QEMU_ALIGN_UP(sz, 8192);
2033 if (mc->fixup_ram_size) {
2034 sz = mc->fixup_ram_size(sz);
2036 ram_size = sz;
2037 if (ram_size != sz) {
2038 error_report("ram size too large");
2039 exit(EXIT_FAILURE);
2042 maxram_size = ram_size;
2044 if (qemu_opt_get(opts, "maxmem")) {
2045 uint64_t slots;
2047 sz = qemu_opt_get_size(opts, "maxmem", 0);
2048 slots = qemu_opt_get_number(opts, "slots", 0);
2049 if (sz < ram_size) {
2050 error_report("invalid value of -m option maxmem: "
2051 "maximum memory size (0x%" PRIx64 ") must be at least "
2052 "the initial memory size (0x" RAM_ADDR_FMT ")",
2053 sz, ram_size);
2054 exit(EXIT_FAILURE);
2055 } else if (slots && sz == ram_size) {
2056 error_report("invalid value of -m option maxmem: "
2057 "memory slots were specified but maximum memory size "
2058 "(0x%" PRIx64 ") is equal to the initial memory size "
2059 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2060 exit(EXIT_FAILURE);
2063 maxram_size = sz;
2064 ram_slots = slots;
2065 } else if (qemu_opt_get(opts, "slots")) {
2066 error_report("invalid -m option value: missing 'maxmem' option");
2067 exit(EXIT_FAILURE);
2070 loc_pop(&loc);
2073 static void qemu_create_machine(MachineClass *machine_class)
2075 object_set_machine_compat_props(machine_class->compat_props);
2077 set_memory_options(machine_class);
2079 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2080 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
2081 exit(0);
2083 object_property_add_child(object_get_root(), "machine",
2084 OBJECT(current_machine));
2085 object_property_add_child(container_get(OBJECT(current_machine),
2086 "/unattached"),
2087 "sysbus", OBJECT(sysbus_get_default()));
2089 if (machine_class->minimum_page_bits) {
2090 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2091 /* This would be a board error: specifying a minimum smaller than
2092 * a target's compile-time fixed setting.
2094 g_assert_not_reached();
2098 cpu_exec_init_all();
2099 page_size_init();
2101 if (machine_class->hw_version) {
2102 qemu_set_hw_version(machine_class->hw_version);
2105 machine_smp_parse(current_machine,
2106 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
2109 * Get the default machine options from the machine if it is not already
2110 * specified either by the configuration file or by the command line.
2112 if (machine_class->default_machine_opts) {
2113 qemu_opts_set_defaults(qemu_find_opts("machine"),
2114 machine_class->default_machine_opts, 0);
2118 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2120 GlobalProperty *g;
2122 g = g_malloc0(sizeof(*g));
2123 g->driver = qemu_opt_get(opts, "driver");
2124 g->property = qemu_opt_get(opts, "property");
2125 g->value = qemu_opt_get(opts, "value");
2126 qdev_prop_register_global(g);
2127 return 0;
2130 static void qemu_read_default_config_file(Error **errp)
2132 ERRP_GUARD();
2133 int ret;
2134 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2136 ret = qemu_read_config_file(file, errp);
2137 if (ret < 0) {
2138 if (ret == -ENOENT) {
2139 error_free(*errp);
2140 *errp = NULL;
2145 static int qemu_set_option(const char *str)
2147 Error *local_err = NULL;
2148 char group[64], id[64], arg[64];
2149 QemuOptsList *list;
2150 QemuOpts *opts;
2151 int rc, offset;
2153 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2154 if (rc < 3 || str[offset] != '=') {
2155 error_report("can't parse: \"%s\"", str);
2156 return -1;
2159 list = qemu_find_opts(group);
2160 if (list == NULL) {
2161 return -1;
2164 opts = qemu_opts_find(list, id);
2165 if (!opts) {
2166 error_report("there is no %s \"%s\" defined",
2167 list->name, id);
2168 return -1;
2171 if (!qemu_opt_set(opts, arg, str + offset + 1, &local_err)) {
2172 error_report_err(local_err);
2173 return -1;
2175 return 0;
2178 static void user_register_global_props(void)
2180 qemu_opts_foreach(qemu_find_opts("global"),
2181 global_init_func, NULL, NULL);
2184 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2186 icount_configure(opts, errp);
2187 return 0;
2190 static int accelerator_set_property(void *opaque,
2191 const char *name, const char *value,
2192 Error **errp)
2194 return object_parse_property_opt(opaque, name, value, "accel", errp);
2197 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2199 bool *p_init_failed = opaque;
2200 const char *acc = qemu_opt_get(opts, "accel");
2201 AccelClass *ac = accel_find(acc);
2202 AccelState *accel;
2203 int ret;
2204 bool qtest_with_kvm;
2206 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2208 if (!ac) {
2209 *p_init_failed = true;
2210 if (!qtest_with_kvm) {
2211 error_report("invalid accelerator %s", acc);
2213 return 0;
2215 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2216 object_apply_compat_props(OBJECT(accel));
2217 qemu_opt_foreach(opts, accelerator_set_property,
2218 accel,
2219 &error_fatal);
2221 ret = accel_init_machine(accel, current_machine);
2222 if (ret < 0) {
2223 *p_init_failed = true;
2224 if (!qtest_with_kvm || ret != -ENOENT) {
2225 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2227 return 0;
2230 return 1;
2233 static void configure_accelerators(const char *progname)
2235 const char *accelerators;
2236 bool init_failed = false;
2238 qemu_opts_foreach(qemu_find_opts("icount"),
2239 do_configure_icount, NULL, &error_fatal);
2241 accelerators = qemu_opt_get(qemu_get_machine_opts(), "accel");
2242 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2243 char **accel_list, **tmp;
2245 if (accelerators == NULL) {
2246 /* Select the default accelerator */
2247 bool have_tcg = accel_find("tcg");
2248 bool have_kvm = accel_find("kvm");
2250 if (have_tcg && have_kvm) {
2251 if (g_str_has_suffix(progname, "kvm")) {
2252 /* If the program name ends with "kvm", we prefer KVM */
2253 accelerators = "kvm:tcg";
2254 } else {
2255 accelerators = "tcg:kvm";
2257 } else if (have_kvm) {
2258 accelerators = "kvm";
2259 } else if (have_tcg) {
2260 accelerators = "tcg";
2261 } else {
2262 error_report("No accelerator selected and"
2263 " no default accelerator available");
2264 exit(1);
2267 accel_list = g_strsplit(accelerators, ":", 0);
2269 for (tmp = accel_list; *tmp; tmp++) {
2271 * Filter invalid accelerators here, to prevent obscenities
2272 * such as "-machine accel=tcg,,thread=single".
2274 if (accel_find(*tmp)) {
2275 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2276 } else {
2277 init_failed = true;
2278 error_report("invalid accelerator %s", *tmp);
2281 g_strfreev(accel_list);
2282 } else {
2283 if (accelerators != NULL) {
2284 error_report("The -accel and \"-machine accel=\" options are incompatible");
2285 exit(1);
2289 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2290 do_configure_accelerator, &init_failed, &error_fatal)) {
2291 if (!init_failed) {
2292 error_report("no accelerator found");
2294 exit(1);
2297 if (init_failed && !qtest_chrdev) {
2298 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2299 error_report("falling back to %s", ac->name);
2302 if (icount_enabled() && !tcg_enabled()) {
2303 error_report("-icount is not allowed with hardware virtualization");
2304 exit(1);
2308 static void create_default_memdev(MachineState *ms, const char *path)
2310 Object *obj;
2311 MachineClass *mc = MACHINE_GET_CLASS(ms);
2313 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2314 if (path) {
2315 object_property_set_str(obj, "mem-path", path, &error_fatal);
2317 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2318 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2319 obj);
2320 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2321 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2322 false, &error_fatal);
2323 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2324 object_unref(obj);
2325 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2326 &error_fatal);
2329 static void qemu_validate_options(void)
2331 QemuOpts *machine_opts = qemu_get_machine_opts();
2332 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2333 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2334 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2336 if (kernel_filename == NULL) {
2337 if (kernel_cmdline != NULL) {
2338 error_report("-append only allowed with -kernel option");
2339 exit(1);
2342 if (initrd_filename != NULL) {
2343 error_report("-initrd only allowed with -kernel option");
2344 exit(1);
2348 if (loadvm && preconfig_requested) {
2349 error_report("'preconfig' and 'loadvm' options are "
2350 "mutually exclusive");
2351 exit(EXIT_FAILURE);
2353 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2354 error_report("'preconfig' supports '-incoming defer' only");
2355 exit(EXIT_FAILURE);
2358 #ifdef CONFIG_CURSES
2359 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2360 error_report("curses display cannot be used with -daemonize");
2361 exit(1);
2363 #endif
2366 static void qemu_process_sugar_options(void)
2368 if (mem_prealloc) {
2369 char *val;
2371 val = g_strdup_printf("%d",
2372 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2373 object_register_sugar_prop("memory-backend", "prealloc-threads", val,
2374 false);
2375 g_free(val);
2376 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2379 if (watchdog) {
2380 int i = select_watchdog(watchdog);
2381 if (i > 0)
2382 exit (i == 1 ? 1 : 0);
2386 /* -action processing */
2389 * Process all the -action parameters parsed from cmdline.
2391 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2393 Error *local_err = NULL;
2394 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2395 QObject *ret = NULL;
2396 qmp_marshal_set_action(qdict, &ret, &local_err);
2397 qobject_unref(ret);
2398 qobject_unref(qdict);
2399 if (local_err) {
2400 error_propagate(errp, local_err);
2401 return 1;
2403 return 0;
2406 static void qemu_process_early_options(void)
2408 #ifdef CONFIG_SECCOMP
2409 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2410 if (olist) {
2411 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2413 #endif
2415 qemu_opts_foreach(qemu_find_opts("name"),
2416 parse_name, NULL, &error_fatal);
2418 if (qemu_opts_foreach(qemu_find_opts("action"),
2419 process_runstate_actions, NULL, &error_fatal)) {
2420 exit(1);
2423 #ifndef _WIN32
2424 qemu_opts_foreach(qemu_find_opts("add-fd"),
2425 parse_add_fd, NULL, &error_fatal);
2427 qemu_opts_foreach(qemu_find_opts("add-fd"),
2428 cleanup_add_fd, NULL, &error_fatal);
2429 #endif
2431 /* Open the logfile at this point and set the log mask if necessary. */
2432 if (log_file) {
2433 qemu_set_log_filename(log_file, &error_fatal);
2435 if (log_mask) {
2436 int mask;
2437 mask = qemu_str_to_log_mask(log_mask);
2438 if (!mask) {
2439 qemu_print_log_usage(stdout);
2440 exit(1);
2442 qemu_set_log(mask);
2443 } else {
2444 qemu_set_log(0);
2447 qemu_add_default_firmwarepath();
2450 static void qemu_process_help_options(void)
2453 * Check for -cpu help and -device help before we call select_machine(),
2454 * which will return an error if the architecture has no default machine
2455 * type and the user did not specify one, so that the user doesn't need
2456 * to say '-cpu help -machine something'.
2458 if (cpu_option && is_help_option(cpu_option)) {
2459 list_cpus(cpu_option);
2460 exit(0);
2463 if (qemu_opts_foreach(qemu_find_opts("device"),
2464 device_help_func, NULL, NULL)) {
2465 exit(0);
2468 /* -L help lists the data directories and exits. */
2469 if (list_data_dirs) {
2470 qemu_list_data_dirs();
2471 exit(0);
2475 static void qemu_maybe_daemonize(const char *pid_file)
2477 Error *err;
2479 os_daemonize();
2480 rcu_disable_atfork();
2482 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2483 error_reportf_err(err, "cannot create PID file: ");
2484 exit(1);
2487 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2488 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2491 static void qemu_init_displays(void)
2493 DisplayState *ds;
2495 /* init local displays */
2496 ds = init_displaystate();
2497 qemu_display_init(ds, &dpy);
2499 /* must be after terminal init, SDL library changes signal handlers */
2500 os_setup_signal_handling();
2502 /* init remote displays */
2503 #ifdef CONFIG_VNC
2504 qemu_opts_foreach(qemu_find_opts("vnc"),
2505 vnc_init_func, NULL, &error_fatal);
2506 #endif
2508 if (using_spice) {
2509 qemu_spice.display_init();
2513 static void qemu_init_board(void)
2515 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2517 if (machine_class->default_ram_id && current_machine->ram_size &&
2518 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2519 create_default_memdev(current_machine, mem_path);
2522 /* process plugin before CPUs are created, but once -smp has been parsed */
2523 qemu_plugin_load_list(&plugin_list, &error_fatal);
2525 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2526 machine_run_board_init(current_machine);
2528 drive_check_orphaned();
2530 realtime_init();
2532 if (hax_enabled()) {
2533 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2534 hax_sync_vcpus();
2538 static void qemu_create_cli_devices(void)
2540 soundhw_init();
2542 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2543 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2545 /* init USB devices */
2546 if (machine_usb(current_machine)) {
2547 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2548 exit(1);
2551 /* init generic devices */
2552 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2553 qemu_opts_foreach(qemu_find_opts("device"),
2554 device_init_func, NULL, &error_fatal);
2555 rom_reset_order_override();
2558 static void qemu_machine_creation_done(void)
2560 MachineState *machine = MACHINE(qdev_get_machine());
2562 /* Did we create any drives that we failed to create a device for? */
2563 drive_check_orphaned();
2565 /* Don't warn about the default network setup that you get if
2566 * no command line -net or -netdev options are specified. There
2567 * are two cases that we would otherwise complain about:
2568 * (1) board doesn't support a NIC but the implicit "-net nic"
2569 * requested one
2570 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2571 * sets up a nic that isn't connected to anything.
2573 if (!default_net && (!qtest_enabled() || has_defaults)) {
2574 net_check_clients();
2577 qdev_prop_check_globals();
2579 qdev_machine_creation_done();
2581 if (machine->cgs) {
2583 * Verify that Confidential Guest Support has actually been initialized
2585 assert(machine->cgs->ready);
2588 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2589 exit(1);
2593 void qmp_x_exit_preconfig(Error **errp)
2595 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2596 error_setg(errp, "The command is permitted only before machine initialization");
2597 return;
2600 qemu_init_board();
2601 qemu_create_cli_devices();
2602 qemu_machine_creation_done();
2604 if (loadvm) {
2605 Error *local_err = NULL;
2606 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2607 error_report_err(local_err);
2608 autostart = 0;
2609 exit(1);
2612 if (replay_mode != REPLAY_MODE_NONE) {
2613 replay_vmstate_init();
2616 if (incoming) {
2617 Error *local_err = NULL;
2618 if (strcmp(incoming, "defer") != 0) {
2619 qmp_migrate_incoming(incoming, &local_err);
2620 if (local_err) {
2621 error_reportf_err(local_err, "-incoming %s: ", incoming);
2622 exit(1);
2625 } else if (autostart) {
2626 qmp_cont(NULL);
2630 #ifdef CONFIG_MODULES
2631 void qemu_load_module_for_opts(const char *group)
2633 static bool spice_tried;
2634 if (g_str_equal(group, "spice") && !spice_tried) {
2635 ui_module_load_one("spice-core");
2636 spice_tried = true;
2639 static bool iscsi_tried;
2640 if (g_str_equal(group, "iscsi") && !iscsi_tried) {
2641 block_module_load_one("iscsi");
2642 iscsi_tried = true;
2645 #endif
2647 void qemu_init(int argc, char **argv, char **envp)
2649 QemuOpts *opts;
2650 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2651 QemuOptsList *olist;
2652 int optind;
2653 const char *optarg;
2654 MachineClass *machine_class;
2655 bool userconfig = true;
2656 FILE *vmstate_dump_file = NULL;
2658 qemu_add_opts(&qemu_drive_opts);
2659 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2660 qemu_add_drive_opts(&qemu_common_drive_opts);
2661 qemu_add_drive_opts(&qemu_drive_opts);
2662 qemu_add_drive_opts(&bdrv_runtime_opts);
2663 qemu_add_opts(&qemu_chardev_opts);
2664 qemu_add_opts(&qemu_device_opts);
2665 qemu_add_opts(&qemu_netdev_opts);
2666 qemu_add_opts(&qemu_nic_opts);
2667 qemu_add_opts(&qemu_net_opts);
2668 qemu_add_opts(&qemu_rtc_opts);
2669 qemu_add_opts(&qemu_global_opts);
2670 qemu_add_opts(&qemu_mon_opts);
2671 qemu_add_opts(&qemu_trace_opts);
2672 qemu_plugin_add_opts();
2673 qemu_add_opts(&qemu_option_rom_opts);
2674 qemu_add_opts(&qemu_machine_opts);
2675 qemu_add_opts(&qemu_accel_opts);
2676 qemu_add_opts(&qemu_mem_opts);
2677 qemu_add_opts(&qemu_smp_opts);
2678 qemu_add_opts(&qemu_boot_opts);
2679 qemu_add_opts(&qemu_add_fd_opts);
2680 qemu_add_opts(&qemu_object_opts);
2681 qemu_add_opts(&qemu_tpmdev_opts);
2682 qemu_add_opts(&qemu_overcommit_opts);
2683 qemu_add_opts(&qemu_msg_opts);
2684 qemu_add_opts(&qemu_name_opts);
2685 qemu_add_opts(&qemu_numa_opts);
2686 qemu_add_opts(&qemu_icount_opts);
2687 qemu_add_opts(&qemu_semihosting_config_opts);
2688 qemu_add_opts(&qemu_fw_cfg_opts);
2689 qemu_add_opts(&qemu_action_opts);
2690 module_call_init(MODULE_INIT_OPTS);
2692 error_init(argv[0]);
2693 qemu_init_exec_dir(argv[0]);
2695 qemu_init_subsystems();
2697 /* first pass of option parsing */
2698 optind = 1;
2699 while (optind < argc) {
2700 if (argv[optind][0] != '-') {
2701 /* disk image */
2702 optind++;
2703 } else {
2704 const QEMUOption *popt;
2706 popt = lookup_opt(argc, argv, &optarg, &optind);
2707 switch (popt->index) {
2708 case QEMU_OPTION_nouserconfig:
2709 userconfig = false;
2710 break;
2715 if (userconfig) {
2716 qemu_read_default_config_file(&error_fatal);
2719 /* second pass of option parsing */
2720 optind = 1;
2721 for(;;) {
2722 if (optind >= argc)
2723 break;
2724 if (argv[optind][0] != '-') {
2725 loc_set_cmdline(argv, optind, 1);
2726 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2727 } else {
2728 const QEMUOption *popt;
2730 popt = lookup_opt(argc, argv, &optarg, &optind);
2731 if (!(popt->arch_mask & arch_type)) {
2732 error_report("Option not supported for this target");
2733 exit(1);
2735 switch(popt->index) {
2736 case QEMU_OPTION_cpu:
2737 /* hw initialization will check this */
2738 cpu_option = optarg;
2739 break;
2740 case QEMU_OPTION_hda:
2741 case QEMU_OPTION_hdb:
2742 case QEMU_OPTION_hdc:
2743 case QEMU_OPTION_hdd:
2744 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2745 HD_OPTS);
2746 break;
2747 case QEMU_OPTION_blockdev:
2749 Visitor *v;
2750 BlockdevOptionsQueueEntry *bdo;
2752 v = qobject_input_visitor_new_str(optarg, "driver",
2753 &error_fatal);
2755 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2756 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2757 &error_fatal);
2758 visit_free(v);
2759 loc_save(&bdo->loc);
2760 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2761 break;
2763 case QEMU_OPTION_drive:
2764 if (drive_def(optarg) == NULL) {
2765 exit(1);
2767 break;
2768 case QEMU_OPTION_set:
2769 if (qemu_set_option(optarg) != 0)
2770 exit(1);
2771 break;
2772 case QEMU_OPTION_global:
2773 if (qemu_global_option(optarg) != 0)
2774 exit(1);
2775 break;
2776 case QEMU_OPTION_mtdblock:
2777 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2778 break;
2779 case QEMU_OPTION_sd:
2780 drive_add(IF_SD, -1, optarg, SD_OPTS);
2781 break;
2782 case QEMU_OPTION_pflash:
2783 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2784 break;
2785 case QEMU_OPTION_snapshot:
2787 Error *blocker = NULL;
2788 snapshot = 1;
2789 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2790 "-snapshot");
2791 replay_add_blocker(blocker);
2793 break;
2794 case QEMU_OPTION_numa:
2795 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2796 optarg, true);
2797 if (!opts) {
2798 exit(1);
2800 break;
2801 case QEMU_OPTION_display:
2802 parse_display(optarg);
2803 break;
2804 case QEMU_OPTION_nographic:
2805 olist = qemu_find_opts("machine");
2806 qemu_opts_parse_noisily(olist, "graphics=off", false);
2807 nographic = true;
2808 dpy.type = DISPLAY_TYPE_NONE;
2809 break;
2810 case QEMU_OPTION_curses:
2811 #ifdef CONFIG_CURSES
2812 dpy.type = DISPLAY_TYPE_CURSES;
2813 #else
2814 error_report("curses or iconv support is disabled");
2815 exit(1);
2816 #endif
2817 break;
2818 case QEMU_OPTION_portrait:
2819 graphic_rotate = 90;
2820 break;
2821 case QEMU_OPTION_rotate:
2822 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2823 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2824 graphic_rotate != 180 && graphic_rotate != 270) {
2825 error_report("only 90, 180, 270 deg rotation is available");
2826 exit(1);
2828 break;
2829 case QEMU_OPTION_kernel:
2830 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
2831 break;
2832 case QEMU_OPTION_initrd:
2833 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
2834 break;
2835 case QEMU_OPTION_append:
2836 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
2837 break;
2838 case QEMU_OPTION_dtb:
2839 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
2840 break;
2841 case QEMU_OPTION_cdrom:
2842 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2843 break;
2844 case QEMU_OPTION_boot:
2845 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2846 optarg, true);
2847 if (!opts) {
2848 exit(1);
2850 break;
2851 case QEMU_OPTION_fda:
2852 case QEMU_OPTION_fdb:
2853 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2854 optarg, FD_OPTS);
2855 break;
2856 case QEMU_OPTION_no_fd_bootchk:
2857 fd_bootchk = 0;
2858 break;
2859 case QEMU_OPTION_netdev:
2860 default_net = 0;
2861 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2862 exit(1);
2864 break;
2865 case QEMU_OPTION_nic:
2866 default_net = 0;
2867 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2868 exit(1);
2870 break;
2871 case QEMU_OPTION_net:
2872 default_net = 0;
2873 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2874 exit(1);
2876 break;
2877 #ifdef CONFIG_LIBISCSI
2878 case QEMU_OPTION_iscsi:
2879 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2880 optarg, false);
2881 if (!opts) {
2882 exit(1);
2884 break;
2885 #endif
2886 case QEMU_OPTION_audio_help:
2887 audio_legacy_help();
2888 exit (0);
2889 break;
2890 case QEMU_OPTION_audiodev:
2891 audio_parse_option(optarg);
2892 break;
2893 case QEMU_OPTION_soundhw:
2894 select_soundhw (optarg);
2895 break;
2896 case QEMU_OPTION_h:
2897 help(0);
2898 break;
2899 case QEMU_OPTION_version:
2900 version();
2901 exit(0);
2902 break;
2903 case QEMU_OPTION_m:
2904 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2905 optarg, true);
2906 if (!opts) {
2907 exit(EXIT_FAILURE);
2909 break;
2910 #ifdef CONFIG_TPM
2911 case QEMU_OPTION_tpmdev:
2912 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2913 exit(1);
2915 break;
2916 #endif
2917 case QEMU_OPTION_mempath:
2918 mem_path = optarg;
2919 break;
2920 case QEMU_OPTION_mem_prealloc:
2921 mem_prealloc = 1;
2922 break;
2923 case QEMU_OPTION_d:
2924 log_mask = optarg;
2925 break;
2926 case QEMU_OPTION_D:
2927 log_file = optarg;
2928 break;
2929 case QEMU_OPTION_DFILTER:
2930 qemu_set_dfilter_ranges(optarg, &error_fatal);
2931 break;
2932 case QEMU_OPTION_seed:
2933 qemu_guest_random_seed_main(optarg, &error_fatal);
2934 break;
2935 case QEMU_OPTION_s:
2936 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2937 break;
2938 case QEMU_OPTION_gdb:
2939 add_device_config(DEV_GDB, optarg);
2940 break;
2941 case QEMU_OPTION_L:
2942 if (is_help_option(optarg)) {
2943 list_data_dirs = true;
2944 } else {
2945 qemu_add_data_dir(g_strdup(optarg));
2947 break;
2948 case QEMU_OPTION_bios:
2949 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
2950 break;
2951 case QEMU_OPTION_singlestep:
2952 singlestep = 1;
2953 break;
2954 case QEMU_OPTION_S:
2955 autostart = 0;
2956 break;
2957 case QEMU_OPTION_k:
2958 keyboard_layout = optarg;
2959 break;
2960 case QEMU_OPTION_vga:
2961 vga_model = optarg;
2962 default_vga = 0;
2963 break;
2964 case QEMU_OPTION_g:
2966 const char *p;
2967 int w, h, depth;
2968 p = optarg;
2969 w = strtol(p, (char **)&p, 10);
2970 if (w <= 0) {
2971 graphic_error:
2972 error_report("invalid resolution or depth");
2973 exit(1);
2975 if (*p != 'x')
2976 goto graphic_error;
2977 p++;
2978 h = strtol(p, (char **)&p, 10);
2979 if (h <= 0)
2980 goto graphic_error;
2981 if (*p == 'x') {
2982 p++;
2983 depth = strtol(p, (char **)&p, 10);
2984 if (depth != 1 && depth != 2 && depth != 4 &&
2985 depth != 8 && depth != 15 && depth != 16 &&
2986 depth != 24 && depth != 32)
2987 goto graphic_error;
2988 } else if (*p == '\0') {
2989 depth = graphic_depth;
2990 } else {
2991 goto graphic_error;
2994 graphic_width = w;
2995 graphic_height = h;
2996 graphic_depth = depth;
2998 break;
2999 case QEMU_OPTION_echr:
3001 char *r;
3002 term_escape_char = strtol(optarg, &r, 0);
3003 if (r == optarg)
3004 printf("Bad argument to echr\n");
3005 break;
3007 case QEMU_OPTION_monitor:
3008 default_monitor = 0;
3009 if (strncmp(optarg, "none", 4)) {
3010 monitor_parse(optarg, "readline", false);
3012 break;
3013 case QEMU_OPTION_qmp:
3014 monitor_parse(optarg, "control", false);
3015 default_monitor = 0;
3016 break;
3017 case QEMU_OPTION_qmp_pretty:
3018 monitor_parse(optarg, "control", true);
3019 default_monitor = 0;
3020 break;
3021 case QEMU_OPTION_mon:
3022 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3023 true);
3024 if (!opts) {
3025 exit(1);
3027 default_monitor = 0;
3028 break;
3029 case QEMU_OPTION_chardev:
3030 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3031 optarg, true);
3032 if (!opts) {
3033 exit(1);
3035 break;
3036 case QEMU_OPTION_fsdev:
3037 olist = qemu_find_opts("fsdev");
3038 if (!olist) {
3039 error_report("fsdev support is disabled");
3040 exit(1);
3042 opts = qemu_opts_parse_noisily(olist, optarg, true);
3043 if (!opts) {
3044 exit(1);
3046 break;
3047 case QEMU_OPTION_virtfs: {
3048 QemuOpts *fsdev;
3049 QemuOpts *device;
3050 const char *writeout, *sock_fd, *socket, *path, *security_model,
3051 *multidevs;
3053 olist = qemu_find_opts("virtfs");
3054 if (!olist) {
3055 error_report("virtfs support is disabled");
3056 exit(1);
3058 opts = qemu_opts_parse_noisily(olist, optarg, true);
3059 if (!opts) {
3060 exit(1);
3063 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3064 qemu_opt_get(opts, "mount_tag") == NULL) {
3065 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3066 exit(1);
3068 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3069 qemu_opts_id(opts) ?:
3070 qemu_opt_get(opts, "mount_tag"),
3071 1, NULL);
3072 if (!fsdev) {
3073 error_report("duplicate or invalid fsdev id: %s",
3074 qemu_opt_get(opts, "mount_tag"));
3075 exit(1);
3078 writeout = qemu_opt_get(opts, "writeout");
3079 if (writeout) {
3080 #ifdef CONFIG_SYNC_FILE_RANGE
3081 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3082 #else
3083 error_report("writeout=immediate not supported "
3084 "on this platform");
3085 exit(1);
3086 #endif
3088 qemu_opt_set(fsdev, "fsdriver",
3089 qemu_opt_get(opts, "fsdriver"), &error_abort);
3090 path = qemu_opt_get(opts, "path");
3091 if (path) {
3092 qemu_opt_set(fsdev, "path", path, &error_abort);
3094 security_model = qemu_opt_get(opts, "security_model");
3095 if (security_model) {
3096 qemu_opt_set(fsdev, "security_model", security_model,
3097 &error_abort);
3099 socket = qemu_opt_get(opts, "socket");
3100 if (socket) {
3101 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3103 sock_fd = qemu_opt_get(opts, "sock_fd");
3104 if (sock_fd) {
3105 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3108 qemu_opt_set_bool(fsdev, "readonly",
3109 qemu_opt_get_bool(opts, "readonly", 0),
3110 &error_abort);
3111 multidevs = qemu_opt_get(opts, "multidevs");
3112 if (multidevs) {
3113 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3115 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3116 &error_abort);
3117 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3118 qemu_opt_set(device, "fsdev",
3119 qemu_opts_id(fsdev), &error_abort);
3120 qemu_opt_set(device, "mount_tag",
3121 qemu_opt_get(opts, "mount_tag"), &error_abort);
3122 break;
3124 case QEMU_OPTION_serial:
3125 add_device_config(DEV_SERIAL, optarg);
3126 default_serial = 0;
3127 if (strncmp(optarg, "mon:", 4) == 0) {
3128 default_monitor = 0;
3130 break;
3131 case QEMU_OPTION_watchdog:
3132 if (watchdog) {
3133 error_report("only one watchdog option may be given");
3134 exit(1);
3136 watchdog = optarg;
3137 break;
3138 case QEMU_OPTION_action:
3139 olist = qemu_find_opts("action");
3140 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3141 exit(1);
3143 break;
3144 case QEMU_OPTION_watchdog_action:
3145 if (select_watchdog_action(optarg) == -1) {
3146 error_report("unknown -watchdog-action parameter");
3147 exit(1);
3149 break;
3150 case QEMU_OPTION_parallel:
3151 add_device_config(DEV_PARALLEL, optarg);
3152 default_parallel = 0;
3153 if (strncmp(optarg, "mon:", 4) == 0) {
3154 default_monitor = 0;
3156 break;
3157 case QEMU_OPTION_debugcon:
3158 add_device_config(DEV_DEBUGCON, optarg);
3159 break;
3160 case QEMU_OPTION_loadvm:
3161 loadvm = optarg;
3162 break;
3163 case QEMU_OPTION_full_screen:
3164 dpy.has_full_screen = true;
3165 dpy.full_screen = true;
3166 break;
3167 case QEMU_OPTION_alt_grab:
3168 alt_grab = 1;
3169 break;
3170 case QEMU_OPTION_ctrl_grab:
3171 ctrl_grab = 1;
3172 break;
3173 case QEMU_OPTION_no_quit:
3174 dpy.has_window_close = true;
3175 dpy.window_close = false;
3176 break;
3177 case QEMU_OPTION_sdl:
3178 #ifdef CONFIG_SDL
3179 dpy.type = DISPLAY_TYPE_SDL;
3180 break;
3181 #else
3182 error_report("SDL support is disabled");
3183 exit(1);
3184 #endif
3185 case QEMU_OPTION_pidfile:
3186 pid_file = optarg;
3187 break;
3188 case QEMU_OPTION_win2k_hack:
3189 win2k_install_hack = 1;
3190 break;
3191 case QEMU_OPTION_acpitable:
3192 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3193 optarg, true);
3194 if (!opts) {
3195 exit(1);
3197 acpi_table_add(opts, &error_fatal);
3198 break;
3199 case QEMU_OPTION_smbios:
3200 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3201 optarg, false);
3202 if (!opts) {
3203 exit(1);
3205 smbios_entry_add(opts, &error_fatal);
3206 break;
3207 case QEMU_OPTION_fwcfg:
3208 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3209 optarg, true);
3210 if (opts == NULL) {
3211 exit(1);
3213 break;
3214 case QEMU_OPTION_preconfig:
3215 preconfig_requested = true;
3216 break;
3217 case QEMU_OPTION_enable_kvm:
3218 olist = qemu_find_opts("machine");
3219 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3220 break;
3221 case QEMU_OPTION_M:
3222 case QEMU_OPTION_machine:
3223 olist = qemu_find_opts("machine");
3224 opts = qemu_opts_parse_noisily(olist, optarg, true);
3225 if (!opts) {
3226 exit(1);
3228 break;
3229 case QEMU_OPTION_accel:
3230 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3231 optarg, true);
3232 optarg = qemu_opt_get(accel_opts, "accel");
3233 if (!optarg || is_help_option(optarg)) {
3234 printf("Accelerators supported in QEMU binary:\n");
3235 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3236 false);
3237 for (el = accel_list; el; el = el->next) {
3238 gchar *typename = g_strdup(object_class_get_name(
3239 OBJECT_CLASS(el->data)));
3240 /* omit qtest which is used for tests only */
3241 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3242 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3243 gchar **optname = g_strsplit(typename,
3244 ACCEL_CLASS_SUFFIX, 0);
3245 printf("%s\n", optname[0]);
3246 g_strfreev(optname);
3248 g_free(typename);
3250 g_slist_free(accel_list);
3251 exit(0);
3253 break;
3254 case QEMU_OPTION_usb:
3255 olist = qemu_find_opts("machine");
3256 qemu_opts_parse_noisily(olist, "usb=on", false);
3257 break;
3258 case QEMU_OPTION_usbdevice:
3259 olist = qemu_find_opts("machine");
3260 qemu_opts_parse_noisily(olist, "usb=on", false);
3261 add_device_config(DEV_USB, optarg);
3262 break;
3263 case QEMU_OPTION_device:
3264 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3265 optarg, true)) {
3266 exit(1);
3268 break;
3269 case QEMU_OPTION_smp:
3270 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3271 optarg, true)) {
3272 exit(1);
3274 break;
3275 case QEMU_OPTION_vnc:
3276 vnc_parse(optarg);
3277 break;
3278 case QEMU_OPTION_no_acpi:
3279 olist = qemu_find_opts("machine");
3280 qemu_opts_parse_noisily(olist, "acpi=off", false);
3281 break;
3282 case QEMU_OPTION_no_hpet:
3283 olist = qemu_find_opts("machine");
3284 qemu_opts_parse_noisily(olist, "hpet=off", false);
3285 break;
3286 case QEMU_OPTION_no_reboot:
3287 olist = qemu_find_opts("action");
3288 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3289 break;
3290 case QEMU_OPTION_no_shutdown:
3291 olist = qemu_find_opts("action");
3292 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3293 break;
3294 case QEMU_OPTION_uuid:
3295 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3296 error_report("failed to parse UUID string: wrong format");
3297 exit(1);
3299 qemu_uuid_set = true;
3300 break;
3301 case QEMU_OPTION_option_rom:
3302 if (nb_option_roms >= MAX_OPTION_ROMS) {
3303 error_report("too many option ROMs");
3304 exit(1);
3306 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3307 optarg, true);
3308 if (!opts) {
3309 exit(1);
3311 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3312 option_rom[nb_option_roms].bootindex =
3313 qemu_opt_get_number(opts, "bootindex", -1);
3314 if (!option_rom[nb_option_roms].name) {
3315 error_report("Option ROM file is not specified");
3316 exit(1);
3318 nb_option_roms++;
3319 break;
3320 case QEMU_OPTION_semihosting:
3321 qemu_semihosting_enable();
3322 break;
3323 case QEMU_OPTION_semihosting_config:
3324 if (qemu_semihosting_config_options(optarg) != 0) {
3325 exit(1);
3327 break;
3328 case QEMU_OPTION_name:
3329 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3330 optarg, true);
3331 if (!opts) {
3332 exit(1);
3334 /* Capture guest name if -msg guest-name is used later */
3335 error_guest_name = qemu_opt_get(opts, "guest");
3336 break;
3337 case QEMU_OPTION_prom_env:
3338 if (nb_prom_envs >= MAX_PROM_ENVS) {
3339 error_report("too many prom variables");
3340 exit(1);
3342 prom_envs[nb_prom_envs] = optarg;
3343 nb_prom_envs++;
3344 break;
3345 case QEMU_OPTION_old_param:
3346 old_param = 1;
3347 break;
3348 case QEMU_OPTION_rtc:
3349 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3350 false);
3351 if (!opts) {
3352 exit(1);
3354 break;
3355 case QEMU_OPTION_icount:
3356 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3357 optarg, true);
3358 if (!icount_opts) {
3359 exit(1);
3361 break;
3362 case QEMU_OPTION_incoming:
3363 if (!incoming) {
3364 runstate_set(RUN_STATE_INMIGRATE);
3366 incoming = optarg;
3367 break;
3368 case QEMU_OPTION_only_migratable:
3369 only_migratable = 1;
3370 break;
3371 case QEMU_OPTION_nodefaults:
3372 has_defaults = 0;
3373 break;
3374 case QEMU_OPTION_xen_domid:
3375 if (!(xen_available())) {
3376 error_report("Option not supported for this target");
3377 exit(1);
3379 xen_domid = atoi(optarg);
3380 break;
3381 case QEMU_OPTION_xen_attach:
3382 if (!(xen_available())) {
3383 error_report("Option not supported for this target");
3384 exit(1);
3386 xen_mode = XEN_ATTACH;
3387 break;
3388 case QEMU_OPTION_xen_domid_restrict:
3389 if (!(xen_available())) {
3390 error_report("Option not supported for this target");
3391 exit(1);
3393 xen_domid_restrict = true;
3394 break;
3395 case QEMU_OPTION_trace:
3396 trace_opt_parse(optarg);
3397 break;
3398 case QEMU_OPTION_plugin:
3399 qemu_plugin_opt_parse(optarg, &plugin_list);
3400 break;
3401 case QEMU_OPTION_readconfig:
3402 qemu_read_config_file(optarg, &error_fatal);
3403 break;
3404 case QEMU_OPTION_spice:
3405 olist = qemu_find_opts_err("spice", NULL);
3406 if (!olist) {
3407 error_report("spice support is disabled");
3408 exit(1);
3410 opts = qemu_opts_parse_noisily(olist, optarg, false);
3411 if (!opts) {
3412 exit(1);
3414 display_remote++;
3415 break;
3416 case QEMU_OPTION_writeconfig:
3418 FILE *fp;
3419 warn_report("-writeconfig is deprecated and will go away without a replacement");
3420 if (strcmp(optarg, "-") == 0) {
3421 fp = stdout;
3422 } else {
3423 fp = fopen(optarg, "w");
3424 if (fp == NULL) {
3425 error_report("open %s: %s", optarg,
3426 strerror(errno));
3427 exit(1);
3430 qemu_config_write(fp);
3431 if (fp != stdout) {
3432 fclose(fp);
3434 break;
3436 case QEMU_OPTION_qtest:
3437 qtest_chrdev = optarg;
3438 break;
3439 case QEMU_OPTION_qtest_log:
3440 qtest_log = optarg;
3441 break;
3442 case QEMU_OPTION_sandbox:
3443 olist = qemu_find_opts("sandbox");
3444 if (!olist) {
3445 #ifndef CONFIG_SECCOMP
3446 error_report("-sandbox support is not enabled "
3447 "in this QEMU binary");
3448 #endif
3449 exit(1);
3452 opts = qemu_opts_parse_noisily(olist, optarg, true);
3453 if (!opts) {
3454 exit(1);
3456 break;
3457 case QEMU_OPTION_add_fd:
3458 #ifndef _WIN32
3459 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3460 optarg, false);
3461 if (!opts) {
3462 exit(1);
3464 #else
3465 error_report("File descriptor passing is disabled on this "
3466 "platform");
3467 exit(1);
3468 #endif
3469 break;
3470 case QEMU_OPTION_object:
3471 object_option_parse(optarg);
3472 break;
3473 case QEMU_OPTION_overcommit:
3474 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3475 optarg, false);
3476 if (!opts) {
3477 exit(1);
3479 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3480 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3481 break;
3482 case QEMU_OPTION_compat:
3484 CompatPolicy *opts;
3485 Visitor *v;
3487 v = qobject_input_visitor_new_str(optarg, NULL,
3488 &error_fatal);
3490 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3491 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3493 qapi_free_CompatPolicy(opts);
3494 visit_free(v);
3495 break;
3497 case QEMU_OPTION_msg:
3498 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3499 false);
3500 if (!opts) {
3501 exit(1);
3503 configure_msg(opts);
3504 break;
3505 case QEMU_OPTION_dump_vmstate:
3506 if (vmstate_dump_file) {
3507 error_report("only one '-dump-vmstate' "
3508 "option may be given");
3509 exit(1);
3511 vmstate_dump_file = fopen(optarg, "w");
3512 if (vmstate_dump_file == NULL) {
3513 error_report("open %s: %s", optarg, strerror(errno));
3514 exit(1);
3516 break;
3517 case QEMU_OPTION_enable_sync_profile:
3518 qsp_enable();
3519 break;
3520 case QEMU_OPTION_nouserconfig:
3521 /* Nothing to be parsed here. Especially, do not error out below. */
3522 break;
3523 default:
3524 if (os_parse_cmd_args(popt->index, optarg)) {
3525 error_report("Option not supported in this build");
3526 exit(1);
3532 * Clear error location left behind by the loop.
3533 * Best done right after the loop. Do not insert code here!
3535 loc_set_none();
3537 qemu_validate_options();
3538 qemu_process_sugar_options();
3541 * These options affect everything else and should be processed
3542 * before daemonizing.
3544 qemu_process_early_options();
3546 qemu_process_help_options();
3547 qemu_maybe_daemonize(pid_file);
3550 * The trace backend must be initialized after daemonizing.
3551 * trace_init_backends() will call st_init(), which will create the
3552 * trace thread in the parent, and also register st_flush_trace_buffer()
3553 * in atexit(). This function will force the parent to wait for the
3554 * writeout thread to finish, which will not occur, and the parent
3555 * process will be left in the host.
3557 if (!trace_init_backends()) {
3558 exit(1);
3560 trace_init_file();
3562 qemu_init_main_loop(&error_fatal);
3563 cpu_timers_init();
3565 user_register_global_props();
3566 replay_configure(icount_opts);
3568 configure_rtc(qemu_find_opts_singleton("rtc"));
3570 qemu_create_machine(select_machine());
3572 suspend_mux_open();
3574 qemu_disable_default_devices();
3575 qemu_create_default_devices();
3576 qemu_create_early_backends();
3578 qemu_apply_machine_options();
3579 phase_advance(PHASE_MACHINE_CREATED);
3582 * Note: uses machine properties such as kernel-irqchip, must run
3583 * after machine_set_property().
3585 configure_accelerators(argv[0]);
3586 phase_advance(PHASE_ACCEL_CREATED);
3589 * Beware, QOM objects created before this point miss global and
3590 * compat properties.
3592 * Global properties get set up by qdev_prop_register_global(),
3593 * called from user_register_global_props(), and certain option
3594 * desugaring. Also in CPU feature desugaring (buried in
3595 * parse_cpu_option()), which happens below this point, but may
3596 * only target the CPU type, which can only be created after
3597 * parse_cpu_option() returned the type.
3599 * Machine compat properties: object_set_machine_compat_props().
3600 * Accelerator compat props: object_set_accelerator_compat_props(),
3601 * called from do_configure_accelerator().
3604 machine_class = MACHINE_GET_CLASS(current_machine);
3605 if (!qtest_enabled() && machine_class->deprecation_reason) {
3606 error_report("Machine type '%s' is deprecated: %s",
3607 machine_class->name, machine_class->deprecation_reason);
3611 * Note: creates a QOM object, must run only after global and
3612 * compat properties have been set up.
3614 migration_object_init();
3616 qemu_create_late_backends();
3618 /* parse features once if machine provides default cpu_type */
3619 current_machine->cpu_type = machine_class->default_cpu_type;
3620 if (cpu_option) {
3621 current_machine->cpu_type = parse_cpu_option(cpu_option);
3623 /* NB: for machine none cpu_type could STILL be NULL here! */
3625 qemu_resolve_machine_memdev();
3626 parse_numa_opts(current_machine);
3628 if (vmstate_dump_file) {
3629 /* dump and exit */
3630 dump_vmstate_json_to_file(vmstate_dump_file);
3631 exit(0);
3634 if (!preconfig_requested) {
3635 qmp_x_exit_preconfig(&error_fatal);
3637 qemu_init_displays();
3638 accel_setup_post(current_machine);
3639 os_setup_post();
3640 resume_mux_open();