usb-host: wire up timer for windows
[qemu/kevin.git] / softmmu / vl.c
blobf815acccaae0ec056cd9145ff58cc47ed537551b
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "hw/qdev-properties.h"
31 #include "qapi/compat-policy.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu-version.h"
36 #include "qemu/cutils.h"
37 #include "qemu/help_option.h"
38 #include "qemu/uuid.h"
39 #include "sysemu/reset.h"
40 #include "sysemu/runstate.h"
41 #include "sysemu/runstate-action.h"
42 #include "sysemu/seccomp.h"
43 #include "sysemu/tcg.h"
44 #include "sysemu/xen.h"
46 #include "qemu/error-report.h"
47 #include "qemu/sockets.h"
48 #include "qemu/accel.h"
49 #include "hw/usb.h"
50 #include "hw/isa/isa.h"
51 #include "hw/scsi/scsi.h"
52 #include "hw/display/vga.h"
53 #include "sysemu/watchdog.h"
54 #include "hw/firmware/smbios.h"
55 #include "hw/acpi/acpi.h"
56 #include "hw/xen/xen.h"
57 #include "hw/loader.h"
58 #include "monitor/qdev.h"
59 #include "net/net.h"
60 #include "net/slirp.h"
61 #include "monitor/monitor.h"
62 #include "ui/console.h"
63 #include "ui/input.h"
64 #include "sysemu/sysemu.h"
65 #include "sysemu/numa.h"
66 #include "sysemu/hostmem.h"
67 #include "exec/gdbstub.h"
68 #include "qemu/timer.h"
69 #include "chardev/char.h"
70 #include "qemu/bitmap.h"
71 #include "qemu/log.h"
72 #include "sysemu/blockdev.h"
73 #include "hw/block/block.h"
74 #include "hw/i386/x86.h"
75 #include "hw/i386/pc.h"
76 #include "migration/misc.h"
77 #include "migration/snapshot.h"
78 #include "sysemu/tpm.h"
79 #include "sysemu/dma.h"
80 #include "hw/audio/soundhw.h"
81 #include "audio/audio.h"
82 #include "sysemu/cpus.h"
83 #include "sysemu/cpu-timers.h"
84 #include "migration/colo.h"
85 #include "migration/postcopy-ram.h"
86 #include "sysemu/kvm.h"
87 #include "sysemu/hax.h"
88 #include "qapi/qobject-input-visitor.h"
89 #include "qemu/option.h"
90 #include "qemu/config-file.h"
91 #include "qemu/qemu-options.h"
92 #include "qemu/main-loop.h"
93 #ifdef CONFIG_VIRTFS
94 #include "fsdev/qemu-fsdev.h"
95 #endif
96 #include "sysemu/qtest.h"
98 #include "disas/disas.h"
100 #include "trace.h"
101 #include "trace/control.h"
102 #include "qemu/plugin.h"
103 #include "qemu/queue.h"
104 #include "sysemu/arch_init.h"
105 #include "exec/confidential-guest-support.h"
107 #include "ui/qemu-spice.h"
108 #include "qapi/string-input-visitor.h"
109 #include "qapi/opts-visitor.h"
110 #include "qapi/clone-visitor.h"
111 #include "qom/object_interfaces.h"
112 #include "semihosting/semihost.h"
113 #include "crypto/init.h"
114 #include "sysemu/replay.h"
115 #include "qapi/qapi-events-run-state.h"
116 #include "qapi/qapi-visit-block-core.h"
117 #include "qapi/qapi-visit-compat.h"
118 #include "qapi/qapi-visit-ui.h"
119 #include "qapi/qapi-commands-block-core.h"
120 #include "qapi/qapi-commands-migration.h"
121 #include "qapi/qapi-commands-misc.h"
122 #include "qapi/qapi-visit-qom.h"
123 #include "qapi/qapi-commands-ui.h"
124 #include "qapi/qmp/qdict.h"
125 #include "qapi/qmp/qerror.h"
126 #include "sysemu/iothread.h"
127 #include "qemu/guest-random.h"
129 #include "config-host.h"
131 #define MAX_VIRTIO_CONSOLES 1
133 typedef struct BlockdevOptionsQueueEntry {
134 BlockdevOptions *bdo;
135 Location loc;
136 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
137 } BlockdevOptionsQueueEntry;
139 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
141 typedef struct ObjectOption {
142 ObjectOptions *opts;
143 QTAILQ_ENTRY(ObjectOption) next;
144 } ObjectOption;
146 static const char *cpu_option;
147 static const char *mem_path;
148 static const char *incoming;
149 static const char *loadvm;
150 static const char *accelerators;
151 static QDict *machine_opts_dict;
152 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
153 static ram_addr_t maxram_size;
154 static uint64_t ram_slots;
155 static int display_remote;
156 static int snapshot;
157 static bool preconfig_requested;
158 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
159 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
160 static bool nographic = false;
161 static int mem_prealloc; /* force preallocation of physical target memory */
162 static ram_addr_t ram_size;
163 static const char *vga_model = NULL;
164 static DisplayOptions dpy;
165 static int num_serial_hds;
166 static Chardev **serial_hds;
167 static const char *log_mask;
168 static const char *log_file;
169 static bool list_data_dirs;
170 static const char *watchdog;
171 static const char *qtest_chrdev;
172 static const char *qtest_log;
174 static int has_defaults = 1;
175 static int default_serial = 1;
176 static int default_parallel = 1;
177 static int default_monitor = 1;
178 static int default_floppy = 1;
179 static int default_cdrom = 1;
180 static int default_sdcard = 1;
181 static int default_vga = 1;
182 static int default_net = 1;
184 static struct {
185 const char *driver;
186 int *flag;
187 } default_list[] = {
188 { .driver = "isa-serial", .flag = &default_serial },
189 { .driver = "isa-parallel", .flag = &default_parallel },
190 { .driver = "isa-fdc", .flag = &default_floppy },
191 { .driver = "floppy", .flag = &default_floppy },
192 { .driver = "ide-cd", .flag = &default_cdrom },
193 { .driver = "ide-hd", .flag = &default_cdrom },
194 { .driver = "scsi-cd", .flag = &default_cdrom },
195 { .driver = "scsi-hd", .flag = &default_cdrom },
196 { .driver = "VGA", .flag = &default_vga },
197 { .driver = "isa-vga", .flag = &default_vga },
198 { .driver = "cirrus-vga", .flag = &default_vga },
199 { .driver = "isa-cirrus-vga", .flag = &default_vga },
200 { .driver = "vmware-svga", .flag = &default_vga },
201 { .driver = "qxl-vga", .flag = &default_vga },
202 { .driver = "virtio-vga", .flag = &default_vga },
203 { .driver = "ati-vga", .flag = &default_vga },
204 { .driver = "vhost-user-vga", .flag = &default_vga },
205 { .driver = "virtio-vga-gl", .flag = &default_vga },
208 static QemuOptsList qemu_rtc_opts = {
209 .name = "rtc",
210 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
211 .merge_lists = true,
212 .desc = {
214 .name = "base",
215 .type = QEMU_OPT_STRING,
217 .name = "clock",
218 .type = QEMU_OPT_STRING,
220 .name = "driftfix",
221 .type = QEMU_OPT_STRING,
223 { /* end of list */ }
227 static QemuOptsList qemu_option_rom_opts = {
228 .name = "option-rom",
229 .implied_opt_name = "romfile",
230 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
231 .desc = {
233 .name = "bootindex",
234 .type = QEMU_OPT_NUMBER,
235 }, {
236 .name = "romfile",
237 .type = QEMU_OPT_STRING,
239 { /* end of list */ }
243 static QemuOptsList qemu_accel_opts = {
244 .name = "accel",
245 .implied_opt_name = "accel",
246 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
247 .desc = {
249 * no elements => accept any
250 * sanity checking will happen later
251 * when setting accelerator properties
257 static QemuOptsList qemu_boot_opts = {
258 .name = "boot-opts",
259 .implied_opt_name = "order",
260 .merge_lists = true,
261 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
262 .desc = {
264 .name = "order",
265 .type = QEMU_OPT_STRING,
266 }, {
267 .name = "once",
268 .type = QEMU_OPT_STRING,
269 }, {
270 .name = "menu",
271 .type = QEMU_OPT_BOOL,
272 }, {
273 .name = "splash",
274 .type = QEMU_OPT_STRING,
275 }, {
276 .name = "splash-time",
277 .type = QEMU_OPT_NUMBER,
278 }, {
279 .name = "reboot-timeout",
280 .type = QEMU_OPT_NUMBER,
281 }, {
282 .name = "strict",
283 .type = QEMU_OPT_BOOL,
285 { /*End of list */ }
289 static QemuOptsList qemu_add_fd_opts = {
290 .name = "add-fd",
291 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
292 .desc = {
294 .name = "fd",
295 .type = QEMU_OPT_NUMBER,
296 .help = "file descriptor of which a duplicate is added to fd set",
298 .name = "set",
299 .type = QEMU_OPT_NUMBER,
300 .help = "ID of the fd set to add fd to",
302 .name = "opaque",
303 .type = QEMU_OPT_STRING,
304 .help = "free-form string used to describe fd",
306 { /* end of list */ }
310 static QemuOptsList qemu_object_opts = {
311 .name = "object",
312 .implied_opt_name = "qom-type",
313 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
314 .desc = {
319 static QemuOptsList qemu_tpmdev_opts = {
320 .name = "tpmdev",
321 .implied_opt_name = "type",
322 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
323 .desc = {
324 /* options are defined in the TPM backends */
325 { /* end of list */ }
329 static QemuOptsList qemu_overcommit_opts = {
330 .name = "overcommit",
331 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
332 .desc = {
334 .name = "mem-lock",
335 .type = QEMU_OPT_BOOL,
338 .name = "cpu-pm",
339 .type = QEMU_OPT_BOOL,
341 { /* end of list */ }
345 static QemuOptsList qemu_msg_opts = {
346 .name = "msg",
347 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
348 .desc = {
350 .name = "timestamp",
351 .type = QEMU_OPT_BOOL,
354 .name = "guest-name",
355 .type = QEMU_OPT_BOOL,
356 .help = "Prepends guest name for error messages but only if "
357 "-name guest is set otherwise option is ignored\n",
359 { /* end of list */ }
363 static QemuOptsList qemu_name_opts = {
364 .name = "name",
365 .implied_opt_name = "guest",
366 .merge_lists = true,
367 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
368 .desc = {
370 .name = "guest",
371 .type = QEMU_OPT_STRING,
372 .help = "Sets the name of the guest.\n"
373 "This name will be displayed in the SDL window caption.\n"
374 "The name will also be used for the VNC server",
375 }, {
376 .name = "process",
377 .type = QEMU_OPT_STRING,
378 .help = "Sets the name of the QEMU process, as shown in top etc",
379 }, {
380 .name = "debug-threads",
381 .type = QEMU_OPT_BOOL,
382 .help = "When enabled, name the individual threads; defaults off.\n"
383 "NOTE: The thread names are for debugging and not a\n"
384 "stable API.",
386 { /* End of list */ }
390 static QemuOptsList qemu_mem_opts = {
391 .name = "memory",
392 .implied_opt_name = "size",
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
394 .merge_lists = true,
395 .desc = {
397 .name = "size",
398 .type = QEMU_OPT_SIZE,
401 .name = "slots",
402 .type = QEMU_OPT_NUMBER,
405 .name = "maxmem",
406 .type = QEMU_OPT_SIZE,
408 { /* end of list */ }
412 static QemuOptsList qemu_icount_opts = {
413 .name = "icount",
414 .implied_opt_name = "shift",
415 .merge_lists = true,
416 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
417 .desc = {
419 .name = "shift",
420 .type = QEMU_OPT_STRING,
421 }, {
422 .name = "align",
423 .type = QEMU_OPT_BOOL,
424 }, {
425 .name = "sleep",
426 .type = QEMU_OPT_BOOL,
427 }, {
428 .name = "rr",
429 .type = QEMU_OPT_STRING,
430 }, {
431 .name = "rrfile",
432 .type = QEMU_OPT_STRING,
433 }, {
434 .name = "rrsnapshot",
435 .type = QEMU_OPT_STRING,
437 { /* end of list */ }
441 static QemuOptsList qemu_fw_cfg_opts = {
442 .name = "fw_cfg",
443 .implied_opt_name = "name",
444 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
445 .desc = {
447 .name = "name",
448 .type = QEMU_OPT_STRING,
449 .help = "Sets the fw_cfg name of the blob to be inserted",
450 }, {
451 .name = "file",
452 .type = QEMU_OPT_STRING,
453 .help = "Sets the name of the file from which "
454 "the fw_cfg blob will be loaded",
455 }, {
456 .name = "string",
457 .type = QEMU_OPT_STRING,
458 .help = "Sets content of the blob to be inserted from a string",
459 }, {
460 .name = "gen_id",
461 .type = QEMU_OPT_STRING,
462 .help = "Sets id of the object generating the fw_cfg blob "
463 "to be inserted",
465 { /* end of list */ }
469 static QemuOptsList qemu_action_opts = {
470 .name = "action",
471 .merge_lists = true,
472 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
473 .desc = {
475 .name = "shutdown",
476 .type = QEMU_OPT_STRING,
478 .name = "reboot",
479 .type = QEMU_OPT_STRING,
481 .name = "panic",
482 .type = QEMU_OPT_STRING,
484 .name = "watchdog",
485 .type = QEMU_OPT_STRING,
487 { /* end of list */ }
491 const char *qemu_get_vm_name(void)
493 return qemu_name;
496 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
498 const char *driver = qemu_opt_get(opts, "driver");
499 int i;
501 if (!driver)
502 return 0;
503 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
504 if (strcmp(default_list[i].driver, driver) != 0)
505 continue;
506 *(default_list[i].flag) = 0;
508 return 0;
511 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
513 const char *proc_name;
515 if (qemu_opt_get(opts, "debug-threads")) {
516 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
518 qemu_name = qemu_opt_get(opts, "guest");
520 proc_name = qemu_opt_get(opts, "process");
521 if (proc_name) {
522 os_set_proc_name(proc_name);
525 return 0;
528 bool defaults_enabled(void)
530 return has_defaults;
533 #ifndef _WIN32
534 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
536 int fd, dupfd, flags;
537 int64_t fdset_id;
538 const char *fd_opaque = NULL;
539 AddfdInfo *fdinfo;
541 fd = qemu_opt_get_number(opts, "fd", -1);
542 fdset_id = qemu_opt_get_number(opts, "set", -1);
543 fd_opaque = qemu_opt_get(opts, "opaque");
545 if (fd < 0) {
546 error_setg(errp, "fd option is required and must be non-negative");
547 return -1;
550 if (fd <= STDERR_FILENO) {
551 error_setg(errp, "fd cannot be a standard I/O stream");
552 return -1;
556 * All fds inherited across exec() necessarily have FD_CLOEXEC
557 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
559 flags = fcntl(fd, F_GETFD);
560 if (flags == -1 || (flags & FD_CLOEXEC)) {
561 error_setg(errp, "fd is not valid or already in use");
562 return -1;
565 if (fdset_id < 0) {
566 error_setg(errp, "set option is required and must be non-negative");
567 return -1;
570 #ifdef F_DUPFD_CLOEXEC
571 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
572 #else
573 dupfd = dup(fd);
574 if (dupfd != -1) {
575 qemu_set_cloexec(dupfd);
577 #endif
578 if (dupfd == -1) {
579 error_setg(errp, "error duplicating fd: %s", strerror(errno));
580 return -1;
583 /* add the duplicate fd, and optionally the opaque string, to the fd set */
584 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
585 &error_abort);
586 g_free(fdinfo);
588 return 0;
591 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
593 int fd;
595 fd = qemu_opt_get_number(opts, "fd", -1);
596 close(fd);
598 return 0;
600 #endif
602 /***********************************************************/
603 /* QEMU Block devices */
605 #define HD_OPTS "media=disk"
606 #define CDROM_OPTS "media=cdrom"
607 #define FD_OPTS ""
608 #define PFLASH_OPTS ""
609 #define MTD_OPTS ""
610 #define SD_OPTS ""
612 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
614 BlockInterfaceType *block_default_type = opaque;
616 return drive_new(opts, *block_default_type, errp) == NULL;
619 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
621 if (qemu_opt_get(opts, "snapshot") == NULL) {
622 qemu_opt_set(opts, "snapshot", "on", &error_abort);
624 return 0;
627 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
628 int index, const char *optstr)
630 QemuOpts *opts;
631 DriveInfo *dinfo;
633 if (!enable || drive_get_by_index(type, index)) {
634 return;
637 opts = drive_add(type, index, NULL, optstr);
638 if (snapshot) {
639 drive_enable_snapshot(NULL, opts, NULL);
642 dinfo = drive_new(opts, type, &error_abort);
643 dinfo->is_default = true;
647 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
648 MachineClass *machine_class, int snapshot)
651 * If the currently selected machine wishes to override the
652 * units-per-bus property of its default HBA interface type, do so
653 * now.
655 if (machine_class->units_per_default_bus) {
656 override_max_devs(machine_class->block_default_type,
657 machine_class->units_per_default_bus);
660 /* open the virtual block devices */
661 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
662 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
664 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
665 loc_push_restore(&bdo->loc);
666 qmp_blockdev_add(bdo->bdo, &error_fatal);
667 loc_pop(&bdo->loc);
668 qapi_free_BlockdevOptions(bdo->bdo);
669 g_free(bdo);
671 if (snapshot) {
672 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
673 NULL, NULL);
675 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
676 &machine_class->block_default_type, &error_fatal)) {
677 /* We printed help */
678 exit(0);
681 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
682 CDROM_OPTS);
683 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
684 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
688 static QemuOptsList qemu_smp_opts = {
689 .name = "smp-opts",
690 .implied_opt_name = "cpus",
691 .merge_lists = true,
692 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
693 .desc = {
695 .name = "cpus",
696 .type = QEMU_OPT_NUMBER,
697 }, {
698 .name = "sockets",
699 .type = QEMU_OPT_NUMBER,
700 }, {
701 .name = "dies",
702 .type = QEMU_OPT_NUMBER,
703 }, {
704 .name = "cores",
705 .type = QEMU_OPT_NUMBER,
706 }, {
707 .name = "threads",
708 .type = QEMU_OPT_NUMBER,
709 }, {
710 .name = "maxcpus",
711 .type = QEMU_OPT_NUMBER,
713 { /*End of list */ }
717 static void realtime_init(void)
719 if (enable_mlock) {
720 if (os_mlock() < 0) {
721 error_report("locking memory failed");
722 exit(1);
728 static void configure_msg(QemuOpts *opts)
730 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
731 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
735 /***********************************************************/
736 /* USB devices */
738 static int usb_device_add(const char *devname)
740 USBDevice *dev = NULL;
742 if (!machine_usb(current_machine)) {
743 return -1;
746 dev = usbdevice_create(devname);
747 if (!dev)
748 return -1;
750 return 0;
753 static int usb_parse(const char *cmdline)
755 int r;
756 r = usb_device_add(cmdline);
757 if (r < 0) {
758 error_report("could not add USB device '%s'", cmdline);
760 return r;
763 /***********************************************************/
764 /* machine registration */
766 static MachineClass *find_machine(const char *name, GSList *machines)
768 GSList *el;
770 for (el = machines; el; el = el->next) {
771 MachineClass *mc = el->data;
773 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
774 return mc;
778 return NULL;
781 static MachineClass *find_default_machine(GSList *machines)
783 GSList *el;
784 MachineClass *default_machineclass = NULL;
786 for (el = machines; el; el = el->next) {
787 MachineClass *mc = el->data;
789 if (mc->is_default) {
790 assert(default_machineclass == NULL && "Multiple default machines");
791 default_machineclass = mc;
795 return default_machineclass;
798 static void version(void)
800 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
801 QEMU_COPYRIGHT "\n");
804 static void help(int exitcode)
806 version();
807 printf("usage: %s [options] [disk_image]\n\n"
808 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
809 error_get_progname());
811 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
812 if ((arch_mask) & arch_type) \
813 fputs(opt_help, stdout);
815 #define ARCHHEADING(text, arch_mask) \
816 if ((arch_mask) & arch_type) \
817 puts(stringify(text));
819 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
821 #include "qemu-options.def"
823 printf("\nDuring emulation, the following keys are useful:\n"
824 "ctrl-alt-f toggle full screen\n"
825 "ctrl-alt-n switch to virtual console 'n'\n"
826 "ctrl-alt toggle mouse and keyboard grab\n"
827 "\n"
828 "When using -nographic, press 'ctrl-a h' to get some help.\n"
829 "\n"
830 QEMU_HELP_BOTTOM "\n");
832 exit(exitcode);
835 #define HAS_ARG 0x0001
837 typedef struct QEMUOption {
838 const char *name;
839 int flags;
840 int index;
841 uint32_t arch_mask;
842 } QEMUOption;
844 static const QEMUOption qemu_options[] = {
845 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
847 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
848 { option, opt_arg, opt_enum, arch_mask },
849 #define DEFHEADING(text)
850 #define ARCHHEADING(text, arch_mask)
852 #include "qemu-options.def"
853 { NULL },
856 typedef struct VGAInterfaceInfo {
857 const char *opt_name; /* option name */
858 const char *name; /* human-readable name */
859 /* Class names indicating that support is available.
860 * If no class is specified, the interface is always available */
861 const char *class_names[2];
862 } VGAInterfaceInfo;
864 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
865 [VGA_NONE] = {
866 .opt_name = "none",
867 .name = "no graphic card",
869 [VGA_STD] = {
870 .opt_name = "std",
871 .name = "standard VGA",
872 .class_names = { "VGA", "isa-vga" },
874 [VGA_CIRRUS] = {
875 .opt_name = "cirrus",
876 .name = "Cirrus VGA",
877 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
879 [VGA_VMWARE] = {
880 .opt_name = "vmware",
881 .name = "VMWare SVGA",
882 .class_names = { "vmware-svga" },
884 [VGA_VIRTIO] = {
885 .opt_name = "virtio",
886 .name = "Virtio VGA",
887 .class_names = { "virtio-vga" },
889 [VGA_QXL] = {
890 .opt_name = "qxl",
891 .name = "QXL VGA",
892 .class_names = { "qxl-vga" },
894 [VGA_TCX] = {
895 .opt_name = "tcx",
896 .name = "TCX framebuffer",
897 .class_names = { "sun-tcx" },
899 [VGA_CG3] = {
900 .opt_name = "cg3",
901 .name = "CG3 framebuffer",
902 .class_names = { "cgthree" },
904 [VGA_XENFB] = {
905 .opt_name = "xenfb",
906 .name = "Xen paravirtualized framebuffer",
910 static bool vga_interface_available(VGAInterfaceType t)
912 const VGAInterfaceInfo *ti = &vga_interfaces[t];
914 assert(t < VGA_TYPE_MAX);
915 return !ti->class_names[0] ||
916 module_object_class_by_name(ti->class_names[0]) ||
917 module_object_class_by_name(ti->class_names[1]);
920 static const char *
921 get_default_vga_model(const MachineClass *machine_class)
923 if (machine_class->default_display) {
924 return machine_class->default_display;
925 } else if (vga_interface_available(VGA_CIRRUS)) {
926 return "cirrus";
927 } else if (vga_interface_available(VGA_STD)) {
928 return "std";
931 return NULL;
934 static void select_vgahw(const MachineClass *machine_class, const char *p)
936 const char *opts;
937 int t;
939 if (g_str_equal(p, "help")) {
940 const char *def = get_default_vga_model(machine_class);
942 for (t = 0; t < VGA_TYPE_MAX; t++) {
943 const VGAInterfaceInfo *ti = &vga_interfaces[t];
945 if (vga_interface_available(t) && ti->opt_name) {
946 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
947 g_str_equal(ti->opt_name, def) ? " (default)" : "");
950 exit(0);
953 assert(vga_interface_type == VGA_NONE);
954 for (t = 0; t < VGA_TYPE_MAX; t++) {
955 const VGAInterfaceInfo *ti = &vga_interfaces[t];
956 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
957 if (!vga_interface_available(t)) {
958 error_report("%s not available", ti->name);
959 exit(1);
961 vga_interface_type = t;
962 break;
965 if (t == VGA_TYPE_MAX) {
966 invalid_vga:
967 error_report("unknown vga type: %s", p);
968 exit(1);
970 while (*opts) {
971 const char *nextopt;
973 if (strstart(opts, ",retrace=", &nextopt)) {
974 opts = nextopt;
975 if (strstart(opts, "dumb", &nextopt))
976 vga_retrace_method = VGA_RETRACE_DUMB;
977 else if (strstart(opts, "precise", &nextopt))
978 vga_retrace_method = VGA_RETRACE_PRECISE;
979 else goto invalid_vga;
980 } else goto invalid_vga;
981 opts = nextopt;
985 static void parse_display_qapi(const char *optarg)
987 DisplayOptions *opts;
988 Visitor *v;
990 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
992 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
993 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
995 qapi_free_DisplayOptions(opts);
996 visit_free(v);
999 DisplayOptions *qmp_query_display_options(Error **errp)
1001 return QAPI_CLONE(DisplayOptions, &dpy);
1004 static void parse_display(const char *p)
1006 const char *opts;
1008 if (is_help_option(p)) {
1009 qemu_display_help();
1010 exit(0);
1013 if (strstart(p, "sdl", &opts)) {
1015 * sdl DisplayType needs hand-crafted parser instead of
1016 * parse_display_qapi() due to some options not in
1017 * DisplayOptions, specifically:
1018 * - ctrl_grab + alt_grab
1019 * Not clear yet what happens to them long-term. Should
1020 * replaced by something better or deprecated and dropped.
1022 #if defined(CONFIG_SDL)
1023 dpy.type = DISPLAY_TYPE_SDL;
1024 while (*opts) {
1025 const char *nextopt;
1027 if (strstart(opts, ",alt_grab=", &nextopt)) {
1028 opts = nextopt;
1029 if (strstart(opts, "on", &nextopt)) {
1030 alt_grab = 1;
1031 } else if (strstart(opts, "off", &nextopt)) {
1032 alt_grab = 0;
1033 } else {
1034 goto invalid_sdl_args;
1036 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1037 opts = nextopt;
1038 if (strstart(opts, "on", &nextopt)) {
1039 ctrl_grab = 1;
1040 } else if (strstart(opts, "off", &nextopt)) {
1041 ctrl_grab = 0;
1042 } else {
1043 goto invalid_sdl_args;
1045 } else if (strstart(opts, ",window_close=", &nextopt) ||
1046 strstart(opts, ",window-close=", &nextopt)) {
1047 if (strstart(opts, ",window_close=", NULL)) {
1048 warn_report("window_close with an underscore is deprecated,"
1049 " please use window-close instead.");
1051 opts = nextopt;
1052 dpy.has_window_close = true;
1053 if (strstart(opts, "on", &nextopt)) {
1054 dpy.window_close = true;
1055 } else if (strstart(opts, "off", &nextopt)) {
1056 dpy.window_close = false;
1057 } else {
1058 goto invalid_sdl_args;
1060 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1061 opts = nextopt;
1062 dpy.has_show_cursor = true;
1063 if (strstart(opts, "on", &nextopt)) {
1064 dpy.show_cursor = true;
1065 } else if (strstart(opts, "off", &nextopt)) {
1066 dpy.show_cursor = false;
1067 } else {
1068 goto invalid_sdl_args;
1070 } else if (strstart(opts, ",gl=", &nextopt)) {
1071 opts = nextopt;
1072 dpy.has_gl = true;
1073 if (strstart(opts, "on", &nextopt)) {
1074 dpy.gl = DISPLAYGL_MODE_ON;
1075 } else if (strstart(opts, "core", &nextopt)) {
1076 dpy.gl = DISPLAYGL_MODE_CORE;
1077 } else if (strstart(opts, "es", &nextopt)) {
1078 dpy.gl = DISPLAYGL_MODE_ES;
1079 } else if (strstart(opts, "off", &nextopt)) {
1080 dpy.gl = DISPLAYGL_MODE_OFF;
1081 } else {
1082 goto invalid_sdl_args;
1084 } else {
1085 invalid_sdl_args:
1086 error_report("invalid SDL option string");
1087 exit(1);
1089 opts = nextopt;
1091 #else
1092 error_report("SDL display supported is not available in this binary");
1093 exit(1);
1094 #endif
1095 } else if (strstart(p, "vnc", &opts)) {
1097 * vnc isn't a (local) DisplayType but a protocol for remote
1098 * display access.
1100 if (*opts == '=') {
1101 vnc_parse(opts + 1);
1102 } else {
1103 error_report("VNC requires a display argument vnc=<display>");
1104 exit(1);
1106 } else {
1107 parse_display_qapi(p);
1111 static inline bool nonempty_str(const char *str)
1113 return str && *str;
1116 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1118 gchar *buf;
1119 size_t size;
1120 const char *name, *file, *str, *gen_id;
1121 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1123 if (fw_cfg == NULL) {
1124 error_setg(errp, "fw_cfg device not available");
1125 return -1;
1127 name = qemu_opt_get(opts, "name");
1128 file = qemu_opt_get(opts, "file");
1129 str = qemu_opt_get(opts, "string");
1130 gen_id = qemu_opt_get(opts, "gen_id");
1132 /* we need the name, and exactly one of: file, content string, gen_id */
1133 if (!nonempty_str(name) ||
1134 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1135 error_setg(errp, "name, plus exactly one of file,"
1136 " string and gen_id, are needed");
1137 return -1;
1139 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1140 error_setg(errp, "name too long (max. %d char)",
1141 FW_CFG_MAX_FILE_PATH - 1);
1142 return -1;
1144 if (nonempty_str(gen_id)) {
1146 * In this particular case where the content is populated
1147 * internally, the "etc/" namespace protection is relaxed,
1148 * so do not emit a warning.
1150 } else if (strncmp(name, "opt/", 4) != 0) {
1151 warn_report("externally provided fw_cfg item names "
1152 "should be prefixed with \"opt/\"");
1154 if (nonempty_str(str)) {
1155 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1156 buf = g_memdup(str, size);
1157 } else if (nonempty_str(gen_id)) {
1158 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1159 return -1;
1161 return 0;
1162 } else {
1163 GError *err = NULL;
1164 if (!g_file_get_contents(file, &buf, &size, &err)) {
1165 error_setg(errp, "can't load %s: %s", file, err->message);
1166 g_error_free(err);
1167 return -1;
1170 /* For legacy, keep user files in a specific global order. */
1171 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1172 fw_cfg_add_file(fw_cfg, name, buf, size);
1173 fw_cfg_reset_order_override(fw_cfg);
1174 return 0;
1177 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1179 return qdev_device_help(opts);
1182 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1184 DeviceState *dev;
1186 dev = qdev_device_add(opts, errp);
1187 if (!dev && *errp) {
1188 error_report_err(*errp);
1189 return -1;
1190 } else if (dev) {
1191 object_unref(OBJECT(dev));
1193 return 0;
1196 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1198 Error *local_err = NULL;
1200 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1201 if (local_err) {
1202 error_propagate(errp, local_err);
1203 return -1;
1205 exit(0);
1207 return 0;
1210 #ifdef CONFIG_VIRTFS
1211 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1213 return qemu_fsdev_add(opts, errp);
1215 #endif
1217 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1219 return monitor_init_opts(opts, errp);
1222 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1224 static int monitor_device_index = 0;
1225 QemuOpts *opts;
1226 const char *p;
1227 char label[32];
1229 if (strstart(optarg, "chardev:", &p)) {
1230 snprintf(label, sizeof(label), "%s", p);
1231 } else {
1232 snprintf(label, sizeof(label), "compat_monitor%d",
1233 monitor_device_index);
1234 opts = qemu_chr_parse_compat(label, optarg, true);
1235 if (!opts) {
1236 error_report("parse error: %s", optarg);
1237 exit(1);
1241 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1242 qemu_opt_set(opts, "mode", mode, &error_abort);
1243 qemu_opt_set(opts, "chardev", label, &error_abort);
1244 if (!strcmp(mode, "control")) {
1245 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1246 } else {
1247 assert(pretty == false);
1249 monitor_device_index++;
1252 struct device_config {
1253 enum {
1254 DEV_USB, /* -usbdevice */
1255 DEV_SERIAL, /* -serial */
1256 DEV_PARALLEL, /* -parallel */
1257 DEV_DEBUGCON, /* -debugcon */
1258 DEV_GDB, /* -gdb, -s */
1259 DEV_SCLP, /* s390 sclp */
1260 } type;
1261 const char *cmdline;
1262 Location loc;
1263 QTAILQ_ENTRY(device_config) next;
1266 static QTAILQ_HEAD(, device_config) device_configs =
1267 QTAILQ_HEAD_INITIALIZER(device_configs);
1269 static void add_device_config(int type, const char *cmdline)
1271 struct device_config *conf;
1273 conf = g_malloc0(sizeof(*conf));
1274 conf->type = type;
1275 conf->cmdline = cmdline;
1276 loc_save(&conf->loc);
1277 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1280 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1282 struct device_config *conf;
1283 int rc;
1285 QTAILQ_FOREACH(conf, &device_configs, next) {
1286 if (conf->type != type)
1287 continue;
1288 loc_push_restore(&conf->loc);
1289 rc = func(conf->cmdline);
1290 loc_pop(&conf->loc);
1291 if (rc) {
1292 return rc;
1295 return 0;
1298 static void qemu_disable_default_devices(void)
1300 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1302 qemu_opts_foreach(qemu_find_opts("device"),
1303 default_driver_check, NULL, NULL);
1304 qemu_opts_foreach(qemu_find_opts("global"),
1305 default_driver_check, NULL, NULL);
1307 if (!vga_model && !default_vga) {
1308 vga_interface_type = VGA_DEVICE;
1310 if (!has_defaults || machine_class->no_serial) {
1311 default_serial = 0;
1313 if (!has_defaults || machine_class->no_parallel) {
1314 default_parallel = 0;
1316 if (!has_defaults || machine_class->no_floppy) {
1317 default_floppy = 0;
1319 if (!has_defaults || machine_class->no_cdrom) {
1320 default_cdrom = 0;
1322 if (!has_defaults || machine_class->no_sdcard) {
1323 default_sdcard = 0;
1325 if (!has_defaults) {
1326 default_monitor = 0;
1327 default_net = 0;
1328 default_vga = 0;
1332 static void qemu_create_default_devices(void)
1334 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1336 if (is_daemonized()) {
1337 /* According to documentation and historically, -nographic redirects
1338 * serial port, parallel port and monitor to stdio, which does not work
1339 * with -daemonize. We can redirect these to null instead, but since
1340 * -nographic is legacy, let's just error out.
1341 * We disallow -nographic only if all other ports are not redirected
1342 * explicitly, to not break existing legacy setups which uses
1343 * -nographic _and_ redirects all ports explicitly - this is valid
1344 * usage, -nographic is just a no-op in this case.
1346 if (nographic
1347 && (default_parallel || default_serial || default_monitor)) {
1348 error_report("-nographic cannot be used with -daemonize");
1349 exit(1);
1353 if (nographic) {
1354 if (default_parallel)
1355 add_device_config(DEV_PARALLEL, "null");
1356 if (default_serial && default_monitor) {
1357 add_device_config(DEV_SERIAL, "mon:stdio");
1358 } else {
1359 if (default_serial)
1360 add_device_config(DEV_SERIAL, "stdio");
1361 if (default_monitor)
1362 monitor_parse("stdio", "readline", false);
1364 } else {
1365 if (default_serial)
1366 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1367 if (default_parallel)
1368 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1369 if (default_monitor)
1370 monitor_parse("vc:80Cx24C", "readline", false);
1373 if (default_net) {
1374 QemuOptsList *net = qemu_find_opts("net");
1375 qemu_opts_parse(net, "nic", true, &error_abort);
1376 #ifdef CONFIG_SLIRP
1377 qemu_opts_parse(net, "user", true, &error_abort);
1378 #endif
1381 #if defined(CONFIG_VNC)
1382 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1383 display_remote++;
1385 #endif
1386 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1387 if (!qemu_display_find_default(&dpy)) {
1388 dpy.type = DISPLAY_TYPE_NONE;
1389 #if defined(CONFIG_VNC)
1390 vnc_parse("localhost:0,to=99,id=default");
1391 #endif
1394 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1395 dpy.type = DISPLAY_TYPE_NONE;
1398 /* If no default VGA is requested, the default is "none". */
1399 if (default_vga) {
1400 vga_model = get_default_vga_model(machine_class);
1402 if (vga_model) {
1403 select_vgahw(machine_class, vga_model);
1407 static int serial_parse(const char *devname)
1409 int index = num_serial_hds;
1410 char label[32];
1412 if (strcmp(devname, "none") == 0)
1413 return 0;
1414 snprintf(label, sizeof(label), "serial%d", index);
1415 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1417 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1418 if (!serial_hds[index]) {
1419 error_report("could not connect serial device"
1420 " to character backend '%s'", devname);
1421 return -1;
1423 num_serial_hds++;
1424 return 0;
1427 Chardev *serial_hd(int i)
1429 assert(i >= 0);
1430 if (i < num_serial_hds) {
1431 return serial_hds[i];
1433 return NULL;
1436 static int parallel_parse(const char *devname)
1438 static int index = 0;
1439 char label[32];
1441 if (strcmp(devname, "none") == 0)
1442 return 0;
1443 if (index == MAX_PARALLEL_PORTS) {
1444 error_report("too many parallel ports");
1445 exit(1);
1447 snprintf(label, sizeof(label), "parallel%d", index);
1448 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1449 if (!parallel_hds[index]) {
1450 error_report("could not connect parallel device"
1451 " to character backend '%s'", devname);
1452 return -1;
1454 index++;
1455 return 0;
1458 static int debugcon_parse(const char *devname)
1460 QemuOpts *opts;
1462 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1463 error_report("invalid character backend '%s'", devname);
1464 exit(1);
1466 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1467 if (!opts) {
1468 error_report("already have a debugcon device");
1469 exit(1);
1471 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1472 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1473 return 0;
1476 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1478 const MachineClass *mc1 = a, *mc2 = b;
1479 int res;
1481 if (mc1->family == NULL) {
1482 if (mc2->family == NULL) {
1483 /* Compare standalone machine types against each other; they sort
1484 * in increasing order.
1486 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1487 object_class_get_name(OBJECT_CLASS(mc2)));
1490 /* Standalone machine types sort after families. */
1491 return 1;
1494 if (mc2->family == NULL) {
1495 /* Families sort before standalone machine types. */
1496 return -1;
1499 /* Families sort between each other alphabetically increasingly. */
1500 res = strcmp(mc1->family, mc2->family);
1501 if (res != 0) {
1502 return res;
1505 /* Within the same family, machine types sort in decreasing order. */
1506 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1507 object_class_get_name(OBJECT_CLASS(mc1)));
1510 static void machine_help_func(const QDict *qdict)
1512 GSList *machines, *el;
1513 const char *type = qdict_get_try_str(qdict, "type");
1515 machines = object_class_get_list(TYPE_MACHINE, false);
1516 if (type) {
1517 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1518 if (machine_class) {
1519 type_print_class_properties(object_class_get_name(machine_class));
1520 return;
1524 printf("Supported machines are:\n");
1525 machines = g_slist_sort(machines, machine_class_cmp);
1526 for (el = machines; el; el = el->next) {
1527 MachineClass *mc = el->data;
1528 if (mc->alias) {
1529 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1531 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1532 mc->is_default ? " (default)" : "",
1533 mc->deprecation_reason ? " (deprecated)" : "");
1537 static void
1538 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1539 const char *arg, Error **errp)
1541 QDict *opts, *prop;
1542 bool help = false;
1543 ERRP_GUARD();
1545 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, errp);
1546 if (help) {
1547 qemu_opts_print_help(opts_list, true);
1548 return;
1550 opts = qdict_new();
1551 qdict_put(opts, propname, prop);
1552 keyval_merge(machine_opts_dict, opts, errp);
1553 qobject_unref(opts);
1556 static const char *pid_file;
1557 static Notifier qemu_unlink_pidfile_notifier;
1559 static void qemu_unlink_pidfile(Notifier *n, void *data)
1561 if (pid_file) {
1562 unlink(pid_file);
1566 static const QEMUOption *lookup_opt(int argc, char **argv,
1567 const char **poptarg, int *poptind)
1569 const QEMUOption *popt;
1570 int optind = *poptind;
1571 char *r = argv[optind];
1572 const char *optarg;
1574 loc_set_cmdline(argv, optind, 1);
1575 optind++;
1576 /* Treat --foo the same as -foo. */
1577 if (r[1] == '-')
1578 r++;
1579 popt = qemu_options;
1580 for(;;) {
1581 if (!popt->name) {
1582 error_report("invalid option");
1583 exit(1);
1585 if (!strcmp(popt->name, r + 1))
1586 break;
1587 popt++;
1589 if (popt->flags & HAS_ARG) {
1590 if (optind >= argc) {
1591 error_report("requires an argument");
1592 exit(1);
1594 optarg = argv[optind++];
1595 loc_set_cmdline(argv, optind - 2, 2);
1596 } else {
1597 optarg = NULL;
1600 *poptarg = optarg;
1601 *poptind = optind;
1603 return popt;
1606 static MachineClass *select_machine(QDict *qdict, Error **errp)
1608 const char *optarg = qdict_get_try_str(qdict, "type");
1609 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1610 MachineClass *machine_class;
1611 Error *local_err = NULL;
1613 if (optarg) {
1614 machine_class = find_machine(optarg, machines);
1615 qdict_del(qdict, "type");
1616 if (!machine_class) {
1617 error_setg(&local_err, "unsupported machine type");
1619 } else {
1620 machine_class = find_default_machine(machines);
1621 if (!machine_class) {
1622 error_setg(&local_err, "No machine specified, and there is no default");
1626 g_slist_free(machines);
1627 if (local_err) {
1628 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1629 error_propagate(errp, local_err);
1631 return machine_class;
1634 static int object_parse_property_opt(Object *obj,
1635 const char *name, const char *value,
1636 const char *skip, Error **errp)
1638 if (g_str_equal(name, skip)) {
1639 return 0;
1642 if (!object_property_parse(obj, name, value, errp)) {
1643 return -1;
1646 return 0;
1649 /* *Non*recursively replace underscores with dashes in QDict keys. */
1650 static void keyval_dashify(QDict *qdict, Error **errp)
1652 const QDictEntry *ent, *next;
1653 char *p;
1655 for (ent = qdict_first(qdict); ent; ent = next) {
1656 g_autofree char *new_key = NULL;
1658 next = qdict_next(qdict, ent);
1659 if (!strchr(ent->key, '_')) {
1660 continue;
1662 new_key = g_strdup(ent->key);
1663 for (p = new_key; *p; p++) {
1664 if (*p == '_') {
1665 *p = '-';
1668 if (qdict_haskey(qdict, new_key)) {
1669 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1670 return;
1672 qobject_ref(ent->value);
1673 qdict_put_obj(qdict, new_key, ent->value);
1674 qdict_del(qdict, ent->key);
1678 static void qemu_apply_legacy_machine_options(QDict *qdict)
1680 const char *value;
1682 keyval_dashify(qdict, &error_fatal);
1684 /* Legacy options do not correspond to MachineState properties. */
1685 value = qdict_get_try_str(qdict, "accel");
1686 if (value) {
1687 accelerators = g_strdup(value);
1688 qdict_del(qdict, "accel");
1691 value = qdict_get_try_str(qdict, "igd-passthru");
1692 if (value) {
1693 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1694 false);
1695 qdict_del(qdict, "igd-passthru");
1698 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1699 if (value) {
1700 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1701 false);
1702 qdict_del(qdict, "kvm-shadow-mem");
1705 value = qdict_get_try_str(qdict, "kernel-irqchip");
1706 if (value) {
1707 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1708 false);
1709 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1710 false);
1711 qdict_del(qdict, "kernel-irqchip");
1715 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1717 ObjectOption *opt, *next;
1719 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1720 const char *type = ObjectType_str(opt->opts->qom_type);
1721 if (type_opt_predicate(type)) {
1722 user_creatable_add_qapi(opt->opts, &error_fatal);
1723 qapi_free_ObjectOptions(opt->opts);
1724 QTAILQ_REMOVE(&object_opts, opt, next);
1725 g_free(opt);
1730 static void object_option_add_visitor(Visitor *v)
1732 ObjectOption *opt = g_new0(ObjectOption, 1);
1733 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1734 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1737 static void object_option_parse(const char *optarg)
1739 QemuOpts *opts;
1740 const char *type;
1741 Visitor *v;
1743 if (optarg[0] == '{') {
1744 QObject *obj = qobject_from_json(optarg, &error_fatal);
1746 v = qobject_input_visitor_new(obj);
1747 qobject_unref(obj);
1748 } else {
1749 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1750 optarg, true);
1751 if (!opts) {
1752 exit(1);
1755 type = qemu_opt_get(opts, "qom-type");
1756 if (!type) {
1757 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1759 if (user_creatable_print_help(type, opts)) {
1760 exit(0);
1763 v = opts_visitor_new(opts);
1766 object_option_add_visitor(v);
1767 visit_free(v);
1771 * Initial object creation happens before all other
1772 * QEMU data types are created. The majority of objects
1773 * can be created at this point. The rng-egd object
1774 * cannot be created here, as it depends on the chardev
1775 * already existing.
1777 static bool object_create_early(const char *type)
1780 * Objects should not be made "delayed" without a reason. If you
1781 * add one, state the reason in a comment!
1784 /* Reason: property "chardev" */
1785 if (g_str_equal(type, "rng-egd") ||
1786 g_str_equal(type, "qtest")) {
1787 return false;
1790 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1791 /* Reason: cryptodev-vhost-user property "chardev" */
1792 if (g_str_equal(type, "cryptodev-vhost-user")) {
1793 return false;
1795 #endif
1797 /* Reason: vhost-user-blk-server property "node-name" */
1798 if (g_str_equal(type, "vhost-user-blk-server")) {
1799 return false;
1802 * Reason: filter-* property "netdev" etc.
1804 if (g_str_equal(type, "filter-buffer") ||
1805 g_str_equal(type, "filter-dump") ||
1806 g_str_equal(type, "filter-mirror") ||
1807 g_str_equal(type, "filter-redirector") ||
1808 g_str_equal(type, "colo-compare") ||
1809 g_str_equal(type, "filter-rewriter") ||
1810 g_str_equal(type, "filter-replay")) {
1811 return false;
1815 * Allocation of large amounts of memory may delay
1816 * chardev initialization for too long, and trigger timeouts
1817 * on software that waits for a monitor socket to be created
1818 * (e.g. libvirt).
1820 if (g_str_has_prefix(type, "memory-backend-")) {
1821 return false;
1824 return true;
1827 static void qemu_apply_machine_options(QDict *qdict)
1829 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1830 const char *boot_order = NULL;
1831 const char *boot_once = NULL;
1832 QemuOpts *opts;
1834 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1835 current_machine->ram_size = ram_size;
1836 current_machine->maxram_size = maxram_size;
1837 current_machine->ram_slots = ram_slots;
1839 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1840 if (opts) {
1841 boot_order = qemu_opt_get(opts, "order");
1842 if (boot_order) {
1843 validate_bootdevices(boot_order, &error_fatal);
1846 boot_once = qemu_opt_get(opts, "once");
1847 if (boot_once) {
1848 validate_bootdevices(boot_once, &error_fatal);
1851 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1852 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1855 if (!boot_order) {
1856 boot_order = machine_class->default_boot_order;
1859 current_machine->boot_order = boot_order;
1860 current_machine->boot_once = boot_once;
1862 if (semihosting_enabled() && !semihosting_get_argc()) {
1863 /* fall back to the -kernel/-append */
1864 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1867 if (current_machine->smp.cpus > 1) {
1868 Error *blocker = NULL;
1869 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1870 replay_add_blocker(blocker);
1874 static void qemu_create_early_backends(void)
1876 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1877 #if defined(CONFIG_SDL)
1878 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1879 #else
1880 const bool use_sdl = false;
1881 #endif
1882 #if defined(CONFIG_GTK)
1883 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1884 #else
1885 const bool use_gtk = false;
1886 #endif
1888 if ((alt_grab || ctrl_grab) && !use_sdl) {
1889 error_report("-alt-grab and -ctrl-grab are only valid "
1890 "for SDL, ignoring option");
1892 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1893 error_report("-no-quit is only valid for GTK and SDL, "
1894 "ignoring option");
1897 qemu_display_early_init(&dpy);
1898 qemu_console_early_init();
1900 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1901 #if defined(CONFIG_OPENGL)
1902 error_report("OpenGL is not supported by the display");
1903 #else
1904 error_report("OpenGL support is disabled");
1905 #endif
1906 exit(1);
1909 object_option_foreach_add(object_create_early);
1911 /* spice needs the timers to be initialized by this point */
1912 /* spice must initialize before audio as it changes the default auiodev */
1913 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1914 qemu_spice.init();
1916 qemu_opts_foreach(qemu_find_opts("chardev"),
1917 chardev_init_func, NULL, &error_fatal);
1919 #ifdef CONFIG_VIRTFS
1920 qemu_opts_foreach(qemu_find_opts("fsdev"),
1921 fsdev_init_func, NULL, &error_fatal);
1922 #endif
1925 * Note: we need to create audio and block backends before
1926 * setting machine properties, so they can be referred to.
1928 configure_blockdev(&bdo_queue, machine_class, snapshot);
1929 audio_init_audiodevs();
1934 * The remainder of object creation happens after the
1935 * creation of chardev, fsdev, net clients and device data types.
1937 static bool object_create_late(const char *type)
1939 return !object_create_early(type);
1942 static void qemu_create_late_backends(void)
1944 if (qtest_chrdev) {
1945 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1948 net_init_clients(&error_fatal);
1950 object_option_foreach_add(object_create_late);
1952 if (tpm_init() < 0) {
1953 exit(1);
1956 qemu_opts_foreach(qemu_find_opts("mon"),
1957 mon_init_func, NULL, &error_fatal);
1959 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1960 exit(1);
1961 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1962 exit(1);
1963 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1964 exit(1);
1966 /* now chardevs have been created we may have semihosting to connect */
1967 qemu_semihosting_connect_chardevs();
1968 qemu_semihosting_console_init();
1971 static bool have_custom_ram_size(void)
1973 QemuOpts *opts = qemu_find_opts_singleton("memory");
1974 return !!qemu_opt_get_size(opts, "size", 0);
1977 static void qemu_resolve_machine_memdev(void)
1979 if (current_machine->ram_memdev_id) {
1980 Object *backend;
1981 ram_addr_t backend_size;
1983 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1984 TYPE_MEMORY_BACKEND, NULL);
1985 if (!backend) {
1986 error_report("Memory backend '%s' not found",
1987 current_machine->ram_memdev_id);
1988 exit(EXIT_FAILURE);
1990 backend_size = object_property_get_uint(backend, "size", &error_abort);
1991 if (have_custom_ram_size() && backend_size != ram_size) {
1992 error_report("Size specified by -m option must match size of "
1993 "explicitly specified 'memory-backend' property");
1994 exit(EXIT_FAILURE);
1996 if (mem_path) {
1997 error_report("'-mem-path' can't be used together with"
1998 "'-machine memory-backend'");
1999 exit(EXIT_FAILURE);
2001 ram_size = backend_size;
2004 if (!xen_enabled()) {
2005 /* On 32-bit hosts, QEMU is limited by virtual address space */
2006 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
2007 error_report("at most 2047 MB RAM can be simulated");
2008 exit(1);
2013 static void set_memory_options(MachineClass *mc)
2015 uint64_t sz;
2016 const char *mem_str;
2017 const ram_addr_t default_ram_size = mc->default_ram_size;
2018 QemuOpts *opts = qemu_find_opts_singleton("memory");
2019 Location loc;
2021 loc_push_none(&loc);
2022 qemu_opts_loc_restore(opts);
2024 sz = 0;
2025 mem_str = qemu_opt_get(opts, "size");
2026 if (mem_str) {
2027 if (!*mem_str) {
2028 error_report("missing 'size' option value");
2029 exit(EXIT_FAILURE);
2032 sz = qemu_opt_get_size(opts, "size", ram_size);
2034 /* Fix up legacy suffix-less format */
2035 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2036 uint64_t overflow_check = sz;
2038 sz *= MiB;
2039 if (sz / MiB != overflow_check) {
2040 error_report("too large 'size' option value");
2041 exit(EXIT_FAILURE);
2046 /* backward compatibility behaviour for case "-m 0" */
2047 if (sz == 0) {
2048 sz = default_ram_size;
2051 sz = QEMU_ALIGN_UP(sz, 8192);
2052 if (mc->fixup_ram_size) {
2053 sz = mc->fixup_ram_size(sz);
2055 ram_size = sz;
2056 if (ram_size != sz) {
2057 error_report("ram size too large");
2058 exit(EXIT_FAILURE);
2061 maxram_size = ram_size;
2063 if (qemu_opt_get(opts, "maxmem")) {
2064 uint64_t slots;
2066 sz = qemu_opt_get_size(opts, "maxmem", 0);
2067 slots = qemu_opt_get_number(opts, "slots", 0);
2068 if (sz < ram_size) {
2069 error_report("invalid value of -m option maxmem: "
2070 "maximum memory size (0x%" PRIx64 ") must be at least "
2071 "the initial memory size (0x" RAM_ADDR_FMT ")",
2072 sz, ram_size);
2073 exit(EXIT_FAILURE);
2074 } else if (slots && sz == ram_size) {
2075 error_report("invalid value of -m option maxmem: "
2076 "memory slots were specified but maximum memory size "
2077 "(0x%" PRIx64 ") is equal to the initial memory size "
2078 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2079 exit(EXIT_FAILURE);
2082 maxram_size = sz;
2083 ram_slots = slots;
2084 } else if (qemu_opt_get(opts, "slots")) {
2085 error_report("invalid -m option value: missing 'maxmem' option");
2086 exit(EXIT_FAILURE);
2089 loc_pop(&loc);
2092 static void qemu_create_machine(QDict *qdict)
2094 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2095 object_set_machine_compat_props(machine_class->compat_props);
2097 set_memory_options(machine_class);
2099 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2100 object_property_add_child(object_get_root(), "machine",
2101 OBJECT(current_machine));
2102 object_property_add_child(container_get(OBJECT(current_machine),
2103 "/unattached"),
2104 "sysbus", OBJECT(sysbus_get_default()));
2106 if (machine_class->minimum_page_bits) {
2107 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2108 /* This would be a board error: specifying a minimum smaller than
2109 * a target's compile-time fixed setting.
2111 g_assert_not_reached();
2115 cpu_exec_init_all();
2116 page_size_init();
2118 if (machine_class->hw_version) {
2119 qemu_set_hw_version(machine_class->hw_version);
2123 * Get the default machine options from the machine if it is not already
2124 * specified either by the configuration file or by the command line.
2126 if (machine_class->default_machine_opts) {
2127 QDict *default_opts =
2128 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2129 &error_abort);
2130 qemu_apply_legacy_machine_options(default_opts);
2131 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2132 false, &error_abort);
2133 qobject_unref(default_opts);
2137 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2139 GlobalProperty *g;
2141 g = g_malloc0(sizeof(*g));
2142 g->driver = qemu_opt_get(opts, "driver");
2143 g->property = qemu_opt_get(opts, "property");
2144 g->value = qemu_opt_get(opts, "value");
2145 qdev_prop_register_global(g);
2146 return 0;
2150 * Return whether configuration group @group is stored in QemuOpts, or
2151 * recorded as one or more QDicts by qemu_record_config_group.
2153 static bool is_qemuopts_group(const char *group)
2155 if (g_str_equal(group, "object") ||
2156 g_str_equal(group, "machine")) {
2157 return false;
2159 return true;
2162 static void qemu_record_config_group(const char *group, QDict *dict,
2163 bool from_json, Error **errp)
2165 if (g_str_equal(group, "object")) {
2166 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2167 object_option_add_visitor(v);
2168 visit_free(v);
2169 } else if (g_str_equal(group, "machine")) {
2171 * Cannot merge string-valued and type-safe dictionaries, so JSON
2172 * is not accepted yet for -M.
2174 assert(!from_json);
2175 keyval_merge(machine_opts_dict, dict, errp);
2176 } else {
2177 abort();
2182 * Parse non-QemuOpts config file groups, pass the rest to
2183 * qemu_config_do_parse.
2185 static void qemu_parse_config_group(const char *group, QDict *qdict,
2186 void *opaque, Error **errp)
2188 QObject *crumpled;
2189 if (is_qemuopts_group(group)) {
2190 qemu_config_do_parse(group, qdict, opaque, errp);
2191 return;
2194 crumpled = qdict_crumple(qdict, errp);
2195 if (!crumpled) {
2196 return;
2198 switch (qobject_type(crumpled)) {
2199 case QTYPE_QDICT:
2200 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2201 break;
2202 case QTYPE_QLIST:
2203 error_setg(errp, "Lists cannot be at top level of a configuration section");
2204 break;
2205 default:
2206 g_assert_not_reached();
2208 qobject_unref(crumpled);
2211 static void qemu_read_default_config_file(Error **errp)
2213 ERRP_GUARD();
2214 int ret;
2215 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2217 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2218 if (ret < 0) {
2219 if (ret == -ENOENT) {
2220 error_free(*errp);
2221 *errp = NULL;
2226 static void qemu_set_option(const char *str, Error **errp)
2228 char group[64], id[64], arg[64];
2229 QemuOptsList *list;
2230 QemuOpts *opts;
2231 int rc, offset;
2233 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2234 if (rc < 3 || str[offset] != '=') {
2235 error_setg(errp, "can't parse: \"%s\"", str);
2236 return;
2239 if (!is_qemuopts_group(group)) {
2240 error_setg(errp, "-set is not supported with %s", group);
2241 } else {
2242 list = qemu_find_opts_err(group, errp);
2243 if (list) {
2244 opts = qemu_opts_find(list, id);
2245 if (!opts) {
2246 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2247 return;
2249 qemu_opt_set(opts, arg, str + offset + 1, errp);
2254 static void user_register_global_props(void)
2256 qemu_opts_foreach(qemu_find_opts("global"),
2257 global_init_func, NULL, NULL);
2260 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2262 icount_configure(opts, errp);
2263 return 0;
2266 static int accelerator_set_property(void *opaque,
2267 const char *name, const char *value,
2268 Error **errp)
2270 return object_parse_property_opt(opaque, name, value, "accel", errp);
2273 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2275 bool *p_init_failed = opaque;
2276 const char *acc = qemu_opt_get(opts, "accel");
2277 AccelClass *ac = accel_find(acc);
2278 AccelState *accel;
2279 int ret;
2280 bool qtest_with_kvm;
2282 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2284 if (!ac) {
2285 *p_init_failed = true;
2286 if (!qtest_with_kvm) {
2287 error_report("invalid accelerator %s", acc);
2289 return 0;
2291 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2292 object_apply_compat_props(OBJECT(accel));
2293 qemu_opt_foreach(opts, accelerator_set_property,
2294 accel,
2295 &error_fatal);
2297 ret = accel_init_machine(accel, current_machine);
2298 if (ret < 0) {
2299 *p_init_failed = true;
2300 if (!qtest_with_kvm || ret != -ENOENT) {
2301 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2303 return 0;
2306 return 1;
2309 static void configure_accelerators(const char *progname)
2311 bool init_failed = false;
2313 qemu_opts_foreach(qemu_find_opts("icount"),
2314 do_configure_icount, NULL, &error_fatal);
2316 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2317 char **accel_list, **tmp;
2319 if (accelerators == NULL) {
2320 /* Select the default accelerator */
2321 bool have_tcg = accel_find("tcg");
2322 bool have_kvm = accel_find("kvm");
2324 if (have_tcg && have_kvm) {
2325 if (g_str_has_suffix(progname, "kvm")) {
2326 /* If the program name ends with "kvm", we prefer KVM */
2327 accelerators = "kvm:tcg";
2328 } else {
2329 accelerators = "tcg:kvm";
2331 } else if (have_kvm) {
2332 accelerators = "kvm";
2333 } else if (have_tcg) {
2334 accelerators = "tcg";
2335 } else {
2336 error_report("No accelerator selected and"
2337 " no default accelerator available");
2338 exit(1);
2341 accel_list = g_strsplit(accelerators, ":", 0);
2343 for (tmp = accel_list; *tmp; tmp++) {
2345 * Filter invalid accelerators here, to prevent obscenities
2346 * such as "-machine accel=tcg,,thread=single".
2348 if (accel_find(*tmp)) {
2349 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2350 } else {
2351 init_failed = true;
2352 error_report("invalid accelerator %s", *tmp);
2355 g_strfreev(accel_list);
2356 } else {
2357 if (accelerators != NULL) {
2358 error_report("The -accel and \"-machine accel=\" options are incompatible");
2359 exit(1);
2363 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2364 do_configure_accelerator, &init_failed, &error_fatal)) {
2365 if (!init_failed) {
2366 error_report("no accelerator found");
2368 exit(1);
2371 if (init_failed && !qtest_chrdev) {
2372 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2373 error_report("falling back to %s", ac->name);
2376 if (icount_enabled() && !tcg_enabled()) {
2377 error_report("-icount is not allowed with hardware virtualization");
2378 exit(1);
2382 static void create_default_memdev(MachineState *ms, const char *path)
2384 Object *obj;
2385 MachineClass *mc = MACHINE_GET_CLASS(ms);
2387 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2388 if (path) {
2389 object_property_set_str(obj, "mem-path", path, &error_fatal);
2391 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2392 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2393 obj);
2394 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2395 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2396 false, &error_fatal);
2397 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2398 object_unref(obj);
2399 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2400 &error_fatal);
2403 static void qemu_validate_options(const QDict *machine_opts)
2405 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2406 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2407 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2409 if (kernel_filename == NULL) {
2410 if (kernel_cmdline != NULL) {
2411 error_report("-append only allowed with -kernel option");
2412 exit(1);
2415 if (initrd_filename != NULL) {
2416 error_report("-initrd only allowed with -kernel option");
2417 exit(1);
2421 if (loadvm && preconfig_requested) {
2422 error_report("'preconfig' and 'loadvm' options are "
2423 "mutually exclusive");
2424 exit(EXIT_FAILURE);
2426 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2427 error_report("'preconfig' supports '-incoming defer' only");
2428 exit(EXIT_FAILURE);
2431 #ifdef CONFIG_CURSES
2432 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2433 error_report("curses display cannot be used with -daemonize");
2434 exit(1);
2436 #endif
2439 static void qemu_process_sugar_options(void)
2441 if (mem_prealloc) {
2442 char *val;
2444 val = g_strdup_printf("%d",
2445 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2446 object_register_sugar_prop("memory-backend", "prealloc-threads", val,
2447 false);
2448 g_free(val);
2449 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2452 if (watchdog) {
2453 int i = select_watchdog(watchdog);
2454 if (i > 0)
2455 exit (i == 1 ? 1 : 0);
2459 /* -action processing */
2462 * Process all the -action parameters parsed from cmdline.
2464 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2466 Error *local_err = NULL;
2467 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2468 QObject *ret = NULL;
2469 qmp_marshal_set_action(qdict, &ret, &local_err);
2470 qobject_unref(ret);
2471 qobject_unref(qdict);
2472 if (local_err) {
2473 error_propagate(errp, local_err);
2474 return 1;
2476 return 0;
2479 static void qemu_process_early_options(void)
2481 #ifdef CONFIG_SECCOMP
2482 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2483 if (olist) {
2484 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2486 #endif
2488 qemu_opts_foreach(qemu_find_opts("name"),
2489 parse_name, NULL, &error_fatal);
2491 if (qemu_opts_foreach(qemu_find_opts("action"),
2492 process_runstate_actions, NULL, &error_fatal)) {
2493 exit(1);
2496 #ifndef _WIN32
2497 qemu_opts_foreach(qemu_find_opts("add-fd"),
2498 parse_add_fd, NULL, &error_fatal);
2500 qemu_opts_foreach(qemu_find_opts("add-fd"),
2501 cleanup_add_fd, NULL, &error_fatal);
2502 #endif
2504 /* Open the logfile at this point and set the log mask if necessary. */
2505 if (log_file) {
2506 qemu_set_log_filename(log_file, &error_fatal);
2508 if (log_mask) {
2509 int mask;
2510 mask = qemu_str_to_log_mask(log_mask);
2511 if (!mask) {
2512 qemu_print_log_usage(stdout);
2513 exit(1);
2515 qemu_set_log(mask);
2516 } else {
2517 qemu_set_log(0);
2520 qemu_add_default_firmwarepath();
2523 static void qemu_process_help_options(void)
2526 * Check for -cpu help and -device help before we call select_machine(),
2527 * which will return an error if the architecture has no default machine
2528 * type and the user did not specify one, so that the user doesn't need
2529 * to say '-cpu help -machine something'.
2531 if (cpu_option && is_help_option(cpu_option)) {
2532 list_cpus(cpu_option);
2533 exit(0);
2536 if (qemu_opts_foreach(qemu_find_opts("device"),
2537 device_help_func, NULL, NULL)) {
2538 exit(0);
2541 /* -L help lists the data directories and exits. */
2542 if (list_data_dirs) {
2543 qemu_list_data_dirs();
2544 exit(0);
2548 static void qemu_maybe_daemonize(const char *pid_file)
2550 Error *err = NULL;
2552 os_daemonize();
2553 rcu_disable_atfork();
2555 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2556 error_reportf_err(err, "cannot create PID file: ");
2557 exit(1);
2560 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2561 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2564 static void qemu_init_displays(void)
2566 DisplayState *ds;
2568 /* init local displays */
2569 ds = init_displaystate();
2570 qemu_display_init(ds, &dpy);
2572 /* must be after terminal init, SDL library changes signal handlers */
2573 os_setup_signal_handling();
2575 /* init remote displays */
2576 #ifdef CONFIG_VNC
2577 qemu_opts_foreach(qemu_find_opts("vnc"),
2578 vnc_init_func, NULL, &error_fatal);
2579 #endif
2581 if (using_spice) {
2582 qemu_spice.display_init();
2586 static void qemu_init_board(void)
2588 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2590 if (machine_class->default_ram_id && current_machine->ram_size &&
2591 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2592 create_default_memdev(current_machine, mem_path);
2595 /* process plugin before CPUs are created, but once -smp has been parsed */
2596 qemu_plugin_load_list(&plugin_list, &error_fatal);
2598 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2599 machine_run_board_init(current_machine);
2601 drive_check_orphaned();
2603 realtime_init();
2605 if (hax_enabled()) {
2606 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2607 hax_sync_vcpus();
2611 static void qemu_create_cli_devices(void)
2613 soundhw_init();
2615 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2616 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2618 /* init USB devices */
2619 if (machine_usb(current_machine)) {
2620 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2621 exit(1);
2624 /* init generic devices */
2625 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2626 qemu_opts_foreach(qemu_find_opts("device"),
2627 device_init_func, NULL, &error_fatal);
2628 rom_reset_order_override();
2631 static void qemu_machine_creation_done(void)
2633 MachineState *machine = MACHINE(qdev_get_machine());
2635 /* Did we create any drives that we failed to create a device for? */
2636 drive_check_orphaned();
2638 /* Don't warn about the default network setup that you get if
2639 * no command line -net or -netdev options are specified. There
2640 * are two cases that we would otherwise complain about:
2641 * (1) board doesn't support a NIC but the implicit "-net nic"
2642 * requested one
2643 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2644 * sets up a nic that isn't connected to anything.
2646 if (!default_net && (!qtest_enabled() || has_defaults)) {
2647 net_check_clients();
2650 qdev_prop_check_globals();
2652 qdev_machine_creation_done();
2654 if (machine->cgs) {
2656 * Verify that Confidential Guest Support has actually been initialized
2658 assert(machine->cgs->ready);
2661 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2662 exit(1);
2666 void qmp_x_exit_preconfig(Error **errp)
2668 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2669 error_setg(errp, "The command is permitted only before machine initialization");
2670 return;
2673 qemu_init_board();
2674 qemu_create_cli_devices();
2675 qemu_machine_creation_done();
2677 if (loadvm) {
2678 Error *local_err = NULL;
2679 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2680 error_report_err(local_err);
2681 autostart = 0;
2682 exit(1);
2685 if (replay_mode != REPLAY_MODE_NONE) {
2686 replay_vmstate_init();
2689 if (incoming) {
2690 Error *local_err = NULL;
2691 if (strcmp(incoming, "defer") != 0) {
2692 qmp_migrate_incoming(incoming, &local_err);
2693 if (local_err) {
2694 error_reportf_err(local_err, "-incoming %s: ", incoming);
2695 exit(1);
2698 } else if (autostart) {
2699 qmp_cont(NULL);
2703 void qemu_init(int argc, char **argv, char **envp)
2705 QemuOpts *opts;
2706 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2707 QemuOptsList *olist;
2708 int optind;
2709 const char *optarg;
2710 MachineClass *machine_class;
2711 bool userconfig = true;
2712 FILE *vmstate_dump_file = NULL;
2714 qemu_add_opts(&qemu_drive_opts);
2715 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2716 qemu_add_drive_opts(&qemu_common_drive_opts);
2717 qemu_add_drive_opts(&qemu_drive_opts);
2718 qemu_add_drive_opts(&bdrv_runtime_opts);
2719 qemu_add_opts(&qemu_chardev_opts);
2720 qemu_add_opts(&qemu_device_opts);
2721 qemu_add_opts(&qemu_netdev_opts);
2722 qemu_add_opts(&qemu_nic_opts);
2723 qemu_add_opts(&qemu_net_opts);
2724 qemu_add_opts(&qemu_rtc_opts);
2725 qemu_add_opts(&qemu_global_opts);
2726 qemu_add_opts(&qemu_mon_opts);
2727 qemu_add_opts(&qemu_trace_opts);
2728 qemu_plugin_add_opts();
2729 qemu_add_opts(&qemu_option_rom_opts);
2730 qemu_add_opts(&qemu_accel_opts);
2731 qemu_add_opts(&qemu_mem_opts);
2732 qemu_add_opts(&qemu_smp_opts);
2733 qemu_add_opts(&qemu_boot_opts);
2734 qemu_add_opts(&qemu_add_fd_opts);
2735 qemu_add_opts(&qemu_object_opts);
2736 qemu_add_opts(&qemu_tpmdev_opts);
2737 qemu_add_opts(&qemu_overcommit_opts);
2738 qemu_add_opts(&qemu_msg_opts);
2739 qemu_add_opts(&qemu_name_opts);
2740 qemu_add_opts(&qemu_numa_opts);
2741 qemu_add_opts(&qemu_icount_opts);
2742 qemu_add_opts(&qemu_semihosting_config_opts);
2743 qemu_add_opts(&qemu_fw_cfg_opts);
2744 qemu_add_opts(&qemu_action_opts);
2745 module_call_init(MODULE_INIT_OPTS);
2747 error_init(argv[0]);
2748 qemu_init_exec_dir(argv[0]);
2750 #ifdef CONFIG_MODULES
2751 module_init_info(qemu_modinfo);
2752 module_allow_arch(TARGET_NAME);
2753 #endif
2755 qemu_init_subsystems();
2757 /* first pass of option parsing */
2758 optind = 1;
2759 while (optind < argc) {
2760 if (argv[optind][0] != '-') {
2761 /* disk image */
2762 optind++;
2763 } else {
2764 const QEMUOption *popt;
2766 popt = lookup_opt(argc, argv, &optarg, &optind);
2767 switch (popt->index) {
2768 case QEMU_OPTION_nouserconfig:
2769 userconfig = false;
2770 break;
2775 machine_opts_dict = qdict_new();
2776 if (userconfig) {
2777 qemu_read_default_config_file(&error_fatal);
2780 /* second pass of option parsing */
2781 optind = 1;
2782 for(;;) {
2783 if (optind >= argc)
2784 break;
2785 if (argv[optind][0] != '-') {
2786 loc_set_cmdline(argv, optind, 1);
2787 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2788 } else {
2789 const QEMUOption *popt;
2791 popt = lookup_opt(argc, argv, &optarg, &optind);
2792 if (!(popt->arch_mask & arch_type)) {
2793 error_report("Option not supported for this target");
2794 exit(1);
2796 switch(popt->index) {
2797 case QEMU_OPTION_cpu:
2798 /* hw initialization will check this */
2799 cpu_option = optarg;
2800 break;
2801 case QEMU_OPTION_hda:
2802 case QEMU_OPTION_hdb:
2803 case QEMU_OPTION_hdc:
2804 case QEMU_OPTION_hdd:
2805 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2806 HD_OPTS);
2807 break;
2808 case QEMU_OPTION_blockdev:
2810 Visitor *v;
2811 BlockdevOptionsQueueEntry *bdo;
2813 v = qobject_input_visitor_new_str(optarg, "driver",
2814 &error_fatal);
2816 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2817 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2818 &error_fatal);
2819 visit_free(v);
2820 loc_save(&bdo->loc);
2821 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2822 break;
2824 case QEMU_OPTION_drive:
2825 if (drive_def(optarg) == NULL) {
2826 exit(1);
2828 break;
2829 case QEMU_OPTION_set:
2830 qemu_set_option(optarg, &error_fatal);
2831 break;
2832 case QEMU_OPTION_global:
2833 if (qemu_global_option(optarg) != 0)
2834 exit(1);
2835 break;
2836 case QEMU_OPTION_mtdblock:
2837 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2838 break;
2839 case QEMU_OPTION_sd:
2840 drive_add(IF_SD, -1, optarg, SD_OPTS);
2841 break;
2842 case QEMU_OPTION_pflash:
2843 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2844 break;
2845 case QEMU_OPTION_snapshot:
2847 Error *blocker = NULL;
2848 snapshot = 1;
2849 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2850 "-snapshot");
2851 replay_add_blocker(blocker);
2853 break;
2854 case QEMU_OPTION_numa:
2855 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2856 optarg, true);
2857 if (!opts) {
2858 exit(1);
2860 break;
2861 case QEMU_OPTION_display:
2862 parse_display(optarg);
2863 break;
2864 case QEMU_OPTION_nographic:
2865 qdict_put_str(machine_opts_dict, "graphics", "off");
2866 nographic = true;
2867 dpy.type = DISPLAY_TYPE_NONE;
2868 break;
2869 case QEMU_OPTION_curses:
2870 #ifdef CONFIG_CURSES
2871 dpy.type = DISPLAY_TYPE_CURSES;
2872 #else
2873 error_report("curses or iconv support is disabled");
2874 exit(1);
2875 #endif
2876 break;
2877 case QEMU_OPTION_portrait:
2878 graphic_rotate = 90;
2879 break;
2880 case QEMU_OPTION_rotate:
2881 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2882 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2883 graphic_rotate != 180 && graphic_rotate != 270) {
2884 error_report("only 90, 180, 270 deg rotation is available");
2885 exit(1);
2887 break;
2888 case QEMU_OPTION_kernel:
2889 qdict_put_str(machine_opts_dict, "kernel", optarg);
2890 break;
2891 case QEMU_OPTION_initrd:
2892 qdict_put_str(machine_opts_dict, "initrd", optarg);
2893 break;
2894 case QEMU_OPTION_append:
2895 qdict_put_str(machine_opts_dict, "append", optarg);
2896 break;
2897 case QEMU_OPTION_dtb:
2898 qdict_put_str(machine_opts_dict, "dtb", optarg);
2899 break;
2900 case QEMU_OPTION_cdrom:
2901 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2902 break;
2903 case QEMU_OPTION_boot:
2904 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2905 optarg, true);
2906 if (!opts) {
2907 exit(1);
2909 break;
2910 case QEMU_OPTION_fda:
2911 case QEMU_OPTION_fdb:
2912 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2913 optarg, FD_OPTS);
2914 break;
2915 case QEMU_OPTION_no_fd_bootchk:
2916 fd_bootchk = 0;
2917 break;
2918 case QEMU_OPTION_netdev:
2919 default_net = 0;
2920 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2921 exit(1);
2923 break;
2924 case QEMU_OPTION_nic:
2925 default_net = 0;
2926 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2927 exit(1);
2929 break;
2930 case QEMU_OPTION_net:
2931 default_net = 0;
2932 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2933 exit(1);
2935 break;
2936 #ifdef CONFIG_LIBISCSI
2937 case QEMU_OPTION_iscsi:
2938 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2939 optarg, false);
2940 if (!opts) {
2941 exit(1);
2943 break;
2944 #endif
2945 case QEMU_OPTION_audio_help:
2946 audio_legacy_help();
2947 exit (0);
2948 break;
2949 case QEMU_OPTION_audiodev:
2950 audio_parse_option(optarg);
2951 break;
2952 case QEMU_OPTION_soundhw:
2953 select_soundhw (optarg);
2954 break;
2955 case QEMU_OPTION_h:
2956 help(0);
2957 break;
2958 case QEMU_OPTION_version:
2959 version();
2960 exit(0);
2961 break;
2962 case QEMU_OPTION_m:
2963 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2964 optarg, true);
2965 if (!opts) {
2966 exit(EXIT_FAILURE);
2968 break;
2969 #ifdef CONFIG_TPM
2970 case QEMU_OPTION_tpmdev:
2971 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2972 exit(1);
2974 break;
2975 #endif
2976 case QEMU_OPTION_mempath:
2977 mem_path = optarg;
2978 break;
2979 case QEMU_OPTION_mem_prealloc:
2980 mem_prealloc = 1;
2981 break;
2982 case QEMU_OPTION_d:
2983 log_mask = optarg;
2984 break;
2985 case QEMU_OPTION_D:
2986 log_file = optarg;
2987 break;
2988 case QEMU_OPTION_DFILTER:
2989 qemu_set_dfilter_ranges(optarg, &error_fatal);
2990 break;
2991 case QEMU_OPTION_seed:
2992 qemu_guest_random_seed_main(optarg, &error_fatal);
2993 break;
2994 case QEMU_OPTION_s:
2995 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2996 break;
2997 case QEMU_OPTION_gdb:
2998 add_device_config(DEV_GDB, optarg);
2999 break;
3000 case QEMU_OPTION_L:
3001 if (is_help_option(optarg)) {
3002 list_data_dirs = true;
3003 } else {
3004 qemu_add_data_dir(g_strdup(optarg));
3006 break;
3007 case QEMU_OPTION_bios:
3008 qdict_put_str(machine_opts_dict, "firmware", optarg);
3009 break;
3010 case QEMU_OPTION_singlestep:
3011 singlestep = 1;
3012 break;
3013 case QEMU_OPTION_S:
3014 autostart = 0;
3015 break;
3016 case QEMU_OPTION_k:
3017 keyboard_layout = optarg;
3018 break;
3019 case QEMU_OPTION_vga:
3020 vga_model = optarg;
3021 default_vga = 0;
3022 break;
3023 case QEMU_OPTION_g:
3025 const char *p;
3026 int w, h, depth;
3027 p = optarg;
3028 w = strtol(p, (char **)&p, 10);
3029 if (w <= 0) {
3030 graphic_error:
3031 error_report("invalid resolution or depth");
3032 exit(1);
3034 if (*p != 'x')
3035 goto graphic_error;
3036 p++;
3037 h = strtol(p, (char **)&p, 10);
3038 if (h <= 0)
3039 goto graphic_error;
3040 if (*p == 'x') {
3041 p++;
3042 depth = strtol(p, (char **)&p, 10);
3043 if (depth != 1 && depth != 2 && depth != 4 &&
3044 depth != 8 && depth != 15 && depth != 16 &&
3045 depth != 24 && depth != 32)
3046 goto graphic_error;
3047 } else if (*p == '\0') {
3048 depth = graphic_depth;
3049 } else {
3050 goto graphic_error;
3053 graphic_width = w;
3054 graphic_height = h;
3055 graphic_depth = depth;
3057 break;
3058 case QEMU_OPTION_echr:
3060 char *r;
3061 term_escape_char = strtol(optarg, &r, 0);
3062 if (r == optarg)
3063 printf("Bad argument to echr\n");
3064 break;
3066 case QEMU_OPTION_monitor:
3067 default_monitor = 0;
3068 if (strncmp(optarg, "none", 4)) {
3069 monitor_parse(optarg, "readline", false);
3071 break;
3072 case QEMU_OPTION_qmp:
3073 monitor_parse(optarg, "control", false);
3074 default_monitor = 0;
3075 break;
3076 case QEMU_OPTION_qmp_pretty:
3077 monitor_parse(optarg, "control", true);
3078 default_monitor = 0;
3079 break;
3080 case QEMU_OPTION_mon:
3081 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3082 true);
3083 if (!opts) {
3084 exit(1);
3086 default_monitor = 0;
3087 break;
3088 case QEMU_OPTION_chardev:
3089 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3090 optarg, true);
3091 if (!opts) {
3092 exit(1);
3094 break;
3095 case QEMU_OPTION_fsdev:
3096 olist = qemu_find_opts("fsdev");
3097 if (!olist) {
3098 error_report("fsdev support is disabled");
3099 exit(1);
3101 opts = qemu_opts_parse_noisily(olist, optarg, true);
3102 if (!opts) {
3103 exit(1);
3105 break;
3106 case QEMU_OPTION_virtfs: {
3107 QemuOpts *fsdev;
3108 QemuOpts *device;
3109 const char *writeout, *sock_fd, *socket, *path, *security_model,
3110 *multidevs;
3112 olist = qemu_find_opts("virtfs");
3113 if (!olist) {
3114 error_report("virtfs support is disabled");
3115 exit(1);
3117 opts = qemu_opts_parse_noisily(olist, optarg, true);
3118 if (!opts) {
3119 exit(1);
3122 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3123 qemu_opt_get(opts, "mount_tag") == NULL) {
3124 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3125 exit(1);
3127 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3128 qemu_opts_id(opts) ?:
3129 qemu_opt_get(opts, "mount_tag"),
3130 1, NULL);
3131 if (!fsdev) {
3132 error_report("duplicate or invalid fsdev id: %s",
3133 qemu_opt_get(opts, "mount_tag"));
3134 exit(1);
3137 writeout = qemu_opt_get(opts, "writeout");
3138 if (writeout) {
3139 #ifdef CONFIG_SYNC_FILE_RANGE
3140 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3141 #else
3142 error_report("writeout=immediate not supported "
3143 "on this platform");
3144 exit(1);
3145 #endif
3147 qemu_opt_set(fsdev, "fsdriver",
3148 qemu_opt_get(opts, "fsdriver"), &error_abort);
3149 path = qemu_opt_get(opts, "path");
3150 if (path) {
3151 qemu_opt_set(fsdev, "path", path, &error_abort);
3153 security_model = qemu_opt_get(opts, "security_model");
3154 if (security_model) {
3155 qemu_opt_set(fsdev, "security_model", security_model,
3156 &error_abort);
3158 socket = qemu_opt_get(opts, "socket");
3159 if (socket) {
3160 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3162 sock_fd = qemu_opt_get(opts, "sock_fd");
3163 if (sock_fd) {
3164 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3167 qemu_opt_set_bool(fsdev, "readonly",
3168 qemu_opt_get_bool(opts, "readonly", 0),
3169 &error_abort);
3170 multidevs = qemu_opt_get(opts, "multidevs");
3171 if (multidevs) {
3172 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3174 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3175 &error_abort);
3176 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3177 qemu_opt_set(device, "fsdev",
3178 qemu_opts_id(fsdev), &error_abort);
3179 qemu_opt_set(device, "mount_tag",
3180 qemu_opt_get(opts, "mount_tag"), &error_abort);
3181 break;
3183 case QEMU_OPTION_serial:
3184 add_device_config(DEV_SERIAL, optarg);
3185 default_serial = 0;
3186 if (strncmp(optarg, "mon:", 4) == 0) {
3187 default_monitor = 0;
3189 break;
3190 case QEMU_OPTION_watchdog:
3191 if (watchdog) {
3192 error_report("only one watchdog option may be given");
3193 exit(1);
3195 watchdog = optarg;
3196 break;
3197 case QEMU_OPTION_action:
3198 olist = qemu_find_opts("action");
3199 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3200 exit(1);
3202 break;
3203 case QEMU_OPTION_watchdog_action:
3204 if (select_watchdog_action(optarg) == -1) {
3205 error_report("unknown -watchdog-action parameter");
3206 exit(1);
3208 break;
3209 case QEMU_OPTION_parallel:
3210 add_device_config(DEV_PARALLEL, optarg);
3211 default_parallel = 0;
3212 if (strncmp(optarg, "mon:", 4) == 0) {
3213 default_monitor = 0;
3215 break;
3216 case QEMU_OPTION_debugcon:
3217 add_device_config(DEV_DEBUGCON, optarg);
3218 break;
3219 case QEMU_OPTION_loadvm:
3220 loadvm = optarg;
3221 break;
3222 case QEMU_OPTION_full_screen:
3223 dpy.has_full_screen = true;
3224 dpy.full_screen = true;
3225 break;
3226 case QEMU_OPTION_alt_grab:
3227 alt_grab = 1;
3228 break;
3229 case QEMU_OPTION_ctrl_grab:
3230 ctrl_grab = 1;
3231 break;
3232 case QEMU_OPTION_no_quit:
3233 dpy.has_window_close = true;
3234 dpy.window_close = false;
3235 warn_report("-no-quit is deprecated, please use "
3236 "-display ...,window-close=off instead.");
3237 break;
3238 case QEMU_OPTION_sdl:
3239 #ifdef CONFIG_SDL
3240 dpy.type = DISPLAY_TYPE_SDL;
3241 break;
3242 #else
3243 error_report("SDL support is disabled");
3244 exit(1);
3245 #endif
3246 case QEMU_OPTION_pidfile:
3247 pid_file = optarg;
3248 break;
3249 case QEMU_OPTION_win2k_hack:
3250 win2k_install_hack = 1;
3251 break;
3252 case QEMU_OPTION_acpitable:
3253 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3254 optarg, true);
3255 if (!opts) {
3256 exit(1);
3258 acpi_table_add(opts, &error_fatal);
3259 break;
3260 case QEMU_OPTION_smbios:
3261 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3262 optarg, false);
3263 if (!opts) {
3264 exit(1);
3266 smbios_entry_add(opts, &error_fatal);
3267 break;
3268 case QEMU_OPTION_fwcfg:
3269 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3270 optarg, true);
3271 if (opts == NULL) {
3272 exit(1);
3274 break;
3275 case QEMU_OPTION_preconfig:
3276 preconfig_requested = true;
3277 break;
3278 case QEMU_OPTION_enable_kvm:
3279 qdict_put_str(machine_opts_dict, "accel", "kvm");
3280 break;
3281 case QEMU_OPTION_M:
3282 case QEMU_OPTION_machine:
3284 bool help;
3286 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3287 if (help) {
3288 machine_help_func(machine_opts_dict);
3289 exit(EXIT_SUCCESS);
3291 break;
3293 case QEMU_OPTION_accel:
3294 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3295 optarg, true);
3296 optarg = qemu_opt_get(accel_opts, "accel");
3297 if (!optarg || is_help_option(optarg)) {
3298 printf("Accelerators supported in QEMU binary:\n");
3299 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3300 false);
3301 for (el = accel_list; el; el = el->next) {
3302 gchar *typename = g_strdup(object_class_get_name(
3303 OBJECT_CLASS(el->data)));
3304 /* omit qtest which is used for tests only */
3305 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3306 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3307 gchar **optname = g_strsplit(typename,
3308 ACCEL_CLASS_SUFFIX, 0);
3309 printf("%s\n", optname[0]);
3310 g_strfreev(optname);
3312 g_free(typename);
3314 g_slist_free(accel_list);
3315 exit(0);
3317 break;
3318 case QEMU_OPTION_usb:
3319 qdict_put_str(machine_opts_dict, "usb", "on");
3320 break;
3321 case QEMU_OPTION_usbdevice:
3322 qdict_put_str(machine_opts_dict, "usb", "on");
3323 add_device_config(DEV_USB, optarg);
3324 break;
3325 case QEMU_OPTION_device:
3326 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3327 optarg, true)) {
3328 exit(1);
3330 break;
3331 case QEMU_OPTION_smp:
3332 machine_parse_property_opt(qemu_find_opts("smp-opts"), "smp", optarg, &error_fatal);
3333 break;
3334 case QEMU_OPTION_vnc:
3335 vnc_parse(optarg);
3336 break;
3337 case QEMU_OPTION_no_acpi:
3338 qdict_put_str(machine_opts_dict, "acpi", "off");
3339 break;
3340 case QEMU_OPTION_no_hpet:
3341 qdict_put_str(machine_opts_dict, "hpet", "off");
3342 break;
3343 case QEMU_OPTION_no_reboot:
3344 olist = qemu_find_opts("action");
3345 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3346 break;
3347 case QEMU_OPTION_no_shutdown:
3348 olist = qemu_find_opts("action");
3349 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3350 break;
3351 case QEMU_OPTION_uuid:
3352 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3353 error_report("failed to parse UUID string: wrong format");
3354 exit(1);
3356 qemu_uuid_set = true;
3357 break;
3358 case QEMU_OPTION_option_rom:
3359 if (nb_option_roms >= MAX_OPTION_ROMS) {
3360 error_report("too many option ROMs");
3361 exit(1);
3363 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3364 optarg, true);
3365 if (!opts) {
3366 exit(1);
3368 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3369 option_rom[nb_option_roms].bootindex =
3370 qemu_opt_get_number(opts, "bootindex", -1);
3371 if (!option_rom[nb_option_roms].name) {
3372 error_report("Option ROM file is not specified");
3373 exit(1);
3375 nb_option_roms++;
3376 break;
3377 case QEMU_OPTION_semihosting:
3378 qemu_semihosting_enable();
3379 break;
3380 case QEMU_OPTION_semihosting_config:
3381 if (qemu_semihosting_config_options(optarg) != 0) {
3382 exit(1);
3384 break;
3385 case QEMU_OPTION_name:
3386 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3387 optarg, true);
3388 if (!opts) {
3389 exit(1);
3391 /* Capture guest name if -msg guest-name is used later */
3392 error_guest_name = qemu_opt_get(opts, "guest");
3393 break;
3394 case QEMU_OPTION_prom_env:
3395 if (nb_prom_envs >= MAX_PROM_ENVS) {
3396 error_report("too many prom variables");
3397 exit(1);
3399 prom_envs[nb_prom_envs] = optarg;
3400 nb_prom_envs++;
3401 break;
3402 case QEMU_OPTION_old_param:
3403 old_param = 1;
3404 break;
3405 case QEMU_OPTION_rtc:
3406 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3407 false);
3408 if (!opts) {
3409 exit(1);
3411 break;
3412 case QEMU_OPTION_icount:
3413 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3414 optarg, true);
3415 if (!icount_opts) {
3416 exit(1);
3418 break;
3419 case QEMU_OPTION_incoming:
3420 if (!incoming) {
3421 runstate_set(RUN_STATE_INMIGRATE);
3423 incoming = optarg;
3424 break;
3425 case QEMU_OPTION_only_migratable:
3426 only_migratable = 1;
3427 break;
3428 case QEMU_OPTION_nodefaults:
3429 has_defaults = 0;
3430 break;
3431 case QEMU_OPTION_xen_domid:
3432 if (!(xen_available())) {
3433 error_report("Option not supported for this target");
3434 exit(1);
3436 xen_domid = atoi(optarg);
3437 break;
3438 case QEMU_OPTION_xen_attach:
3439 if (!(xen_available())) {
3440 error_report("Option not supported for this target");
3441 exit(1);
3443 xen_mode = XEN_ATTACH;
3444 break;
3445 case QEMU_OPTION_xen_domid_restrict:
3446 if (!(xen_available())) {
3447 error_report("Option not supported for this target");
3448 exit(1);
3450 xen_domid_restrict = true;
3451 break;
3452 case QEMU_OPTION_trace:
3453 trace_opt_parse(optarg);
3454 break;
3455 case QEMU_OPTION_plugin:
3456 qemu_plugin_opt_parse(optarg, &plugin_list);
3457 break;
3458 case QEMU_OPTION_readconfig:
3459 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3460 break;
3461 case QEMU_OPTION_spice:
3462 olist = qemu_find_opts_err("spice", NULL);
3463 if (!olist) {
3464 error_report("spice support is disabled");
3465 exit(1);
3467 opts = qemu_opts_parse_noisily(olist, optarg, false);
3468 if (!opts) {
3469 exit(1);
3471 display_remote++;
3472 break;
3473 case QEMU_OPTION_writeconfig:
3475 FILE *fp;
3476 warn_report("-writeconfig is deprecated and will go away without a replacement");
3477 if (strcmp(optarg, "-") == 0) {
3478 fp = stdout;
3479 } else {
3480 fp = fopen(optarg, "w");
3481 if (fp == NULL) {
3482 error_report("open %s: %s", optarg,
3483 strerror(errno));
3484 exit(1);
3487 qemu_config_write(fp);
3488 if (fp != stdout) {
3489 fclose(fp);
3491 break;
3493 case QEMU_OPTION_qtest:
3494 qtest_chrdev = optarg;
3495 break;
3496 case QEMU_OPTION_qtest_log:
3497 qtest_log = optarg;
3498 break;
3499 case QEMU_OPTION_sandbox:
3500 olist = qemu_find_opts("sandbox");
3501 if (!olist) {
3502 #ifndef CONFIG_SECCOMP
3503 error_report("-sandbox support is not enabled "
3504 "in this QEMU binary");
3505 #endif
3506 exit(1);
3509 opts = qemu_opts_parse_noisily(olist, optarg, true);
3510 if (!opts) {
3511 exit(1);
3513 break;
3514 case QEMU_OPTION_add_fd:
3515 #ifndef _WIN32
3516 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3517 optarg, false);
3518 if (!opts) {
3519 exit(1);
3521 #else
3522 error_report("File descriptor passing is disabled on this "
3523 "platform");
3524 exit(1);
3525 #endif
3526 break;
3527 case QEMU_OPTION_object:
3528 object_option_parse(optarg);
3529 break;
3530 case QEMU_OPTION_overcommit:
3531 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3532 optarg, false);
3533 if (!opts) {
3534 exit(1);
3536 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3537 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3538 break;
3539 case QEMU_OPTION_compat:
3541 CompatPolicy *opts;
3542 Visitor *v;
3544 v = qobject_input_visitor_new_str(optarg, NULL,
3545 &error_fatal);
3547 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3548 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3550 qapi_free_CompatPolicy(opts);
3551 visit_free(v);
3552 break;
3554 case QEMU_OPTION_msg:
3555 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3556 false);
3557 if (!opts) {
3558 exit(1);
3560 configure_msg(opts);
3561 break;
3562 case QEMU_OPTION_dump_vmstate:
3563 if (vmstate_dump_file) {
3564 error_report("only one '-dump-vmstate' "
3565 "option may be given");
3566 exit(1);
3568 vmstate_dump_file = fopen(optarg, "w");
3569 if (vmstate_dump_file == NULL) {
3570 error_report("open %s: %s", optarg, strerror(errno));
3571 exit(1);
3573 break;
3574 case QEMU_OPTION_enable_sync_profile:
3575 qsp_enable();
3576 break;
3577 case QEMU_OPTION_nouserconfig:
3578 /* Nothing to be parsed here. Especially, do not error out below. */
3579 break;
3580 default:
3581 if (os_parse_cmd_args(popt->index, optarg)) {
3582 error_report("Option not supported in this build");
3583 exit(1);
3589 * Clear error location left behind by the loop.
3590 * Best done right after the loop. Do not insert code here!
3592 loc_set_none();
3594 qemu_validate_options(machine_opts_dict);
3595 qemu_process_sugar_options();
3598 * These options affect everything else and should be processed
3599 * before daemonizing.
3601 qemu_process_early_options();
3603 qemu_process_help_options();
3604 qemu_maybe_daemonize(pid_file);
3607 * The trace backend must be initialized after daemonizing.
3608 * trace_init_backends() will call st_init(), which will create the
3609 * trace thread in the parent, and also register st_flush_trace_buffer()
3610 * in atexit(). This function will force the parent to wait for the
3611 * writeout thread to finish, which will not occur, and the parent
3612 * process will be left in the host.
3614 if (!trace_init_backends()) {
3615 exit(1);
3617 trace_init_file();
3619 qemu_init_main_loop(&error_fatal);
3620 cpu_timers_init();
3622 user_register_global_props();
3623 replay_configure(icount_opts);
3625 configure_rtc(qemu_find_opts_singleton("rtc"));
3627 qemu_create_machine(machine_opts_dict);
3629 suspend_mux_open();
3631 qemu_disable_default_devices();
3632 qemu_create_default_devices();
3633 qemu_create_early_backends();
3635 qemu_apply_legacy_machine_options(machine_opts_dict);
3636 qemu_apply_machine_options(machine_opts_dict);
3637 qobject_unref(machine_opts_dict);
3638 phase_advance(PHASE_MACHINE_CREATED);
3641 * Note: uses machine properties such as kernel-irqchip, must run
3642 * after qemu_apply_machine_options.
3644 configure_accelerators(argv[0]);
3645 phase_advance(PHASE_ACCEL_CREATED);
3648 * Beware, QOM objects created before this point miss global and
3649 * compat properties.
3651 * Global properties get set up by qdev_prop_register_global(),
3652 * called from user_register_global_props(), and certain option
3653 * desugaring. Also in CPU feature desugaring (buried in
3654 * parse_cpu_option()), which happens below this point, but may
3655 * only target the CPU type, which can only be created after
3656 * parse_cpu_option() returned the type.
3658 * Machine compat properties: object_set_machine_compat_props().
3659 * Accelerator compat props: object_set_accelerator_compat_props(),
3660 * called from do_configure_accelerator().
3663 machine_class = MACHINE_GET_CLASS(current_machine);
3664 if (!qtest_enabled() && machine_class->deprecation_reason) {
3665 error_report("Machine type '%s' is deprecated: %s",
3666 machine_class->name, machine_class->deprecation_reason);
3670 * Note: creates a QOM object, must run only after global and
3671 * compat properties have been set up.
3673 migration_object_init();
3675 qemu_create_late_backends();
3677 /* parse features once if machine provides default cpu_type */
3678 current_machine->cpu_type = machine_class->default_cpu_type;
3679 if (cpu_option) {
3680 current_machine->cpu_type = parse_cpu_option(cpu_option);
3682 /* NB: for machine none cpu_type could STILL be NULL here! */
3684 qemu_resolve_machine_memdev();
3685 parse_numa_opts(current_machine);
3687 if (vmstate_dump_file) {
3688 /* dump and exit */
3689 dump_vmstate_json_to_file(vmstate_dump_file);
3690 exit(0);
3693 if (!preconfig_requested) {
3694 qmp_x_exit_preconfig(&error_fatal);
3696 qemu_init_displays();
3697 accel_setup_post(current_machine);
3698 os_setup_post();
3699 resume_mux_open();