s390x/tcg: Implement 32/128 bit for VECTOR FP MULTIPLY AND (ADD|SUBTRACT)
[qemu/kevin.git] / softmmu / vl.c
blobfeb4d201f30f0b8d7d3a1b93d99282e081626140
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 #define MAX_VIRTIO_CONSOLES 1
131 typedef struct BlockdevOptionsQueueEntry {
132 BlockdevOptions *bdo;
133 Location loc;
134 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
135 } BlockdevOptionsQueueEntry;
137 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
139 typedef struct ObjectOption {
140 ObjectOptions *opts;
141 QTAILQ_ENTRY(ObjectOption) next;
142 } ObjectOption;
144 static const char *cpu_option;
145 static const char *mem_path;
146 static const char *incoming;
147 static const char *loadvm;
148 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
149 static ram_addr_t maxram_size;
150 static uint64_t ram_slots;
151 static int display_remote;
152 static int snapshot;
153 static bool preconfig_requested;
154 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
155 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
156 static bool nographic = false;
157 static int mem_prealloc; /* force preallocation of physical target memory */
158 static ram_addr_t ram_size;
159 static const char *vga_model = NULL;
160 static DisplayOptions dpy;
161 static int num_serial_hds;
162 static Chardev **serial_hds;
163 static const char *log_mask;
164 static const char *log_file;
165 static bool list_data_dirs;
166 static const char *watchdog;
167 static const char *qtest_chrdev;
168 static const char *qtest_log;
170 static int has_defaults = 1;
171 static int default_serial = 1;
172 static int default_parallel = 1;
173 static int default_monitor = 1;
174 static int default_floppy = 1;
175 static int default_cdrom = 1;
176 static int default_sdcard = 1;
177 static int default_vga = 1;
178 static int default_net = 1;
180 static struct {
181 const char *driver;
182 int *flag;
183 } default_list[] = {
184 { .driver = "isa-serial", .flag = &default_serial },
185 { .driver = "isa-parallel", .flag = &default_parallel },
186 { .driver = "isa-fdc", .flag = &default_floppy },
187 { .driver = "floppy", .flag = &default_floppy },
188 { .driver = "ide-cd", .flag = &default_cdrom },
189 { .driver = "ide-hd", .flag = &default_cdrom },
190 { .driver = "scsi-cd", .flag = &default_cdrom },
191 { .driver = "scsi-hd", .flag = &default_cdrom },
192 { .driver = "VGA", .flag = &default_vga },
193 { .driver = "isa-vga", .flag = &default_vga },
194 { .driver = "cirrus-vga", .flag = &default_vga },
195 { .driver = "isa-cirrus-vga", .flag = &default_vga },
196 { .driver = "vmware-svga", .flag = &default_vga },
197 { .driver = "qxl-vga", .flag = &default_vga },
198 { .driver = "virtio-vga", .flag = &default_vga },
199 { .driver = "ati-vga", .flag = &default_vga },
200 { .driver = "vhost-user-vga", .flag = &default_vga },
203 static QemuOptsList qemu_rtc_opts = {
204 .name = "rtc",
205 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
206 .merge_lists = true,
207 .desc = {
209 .name = "base",
210 .type = QEMU_OPT_STRING,
212 .name = "clock",
213 .type = QEMU_OPT_STRING,
215 .name = "driftfix",
216 .type = QEMU_OPT_STRING,
218 { /* end of list */ }
222 static QemuOptsList qemu_option_rom_opts = {
223 .name = "option-rom",
224 .implied_opt_name = "romfile",
225 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
226 .desc = {
228 .name = "bootindex",
229 .type = QEMU_OPT_NUMBER,
230 }, {
231 .name = "romfile",
232 .type = QEMU_OPT_STRING,
234 { /* end of list */ }
238 static QemuOptsList qemu_machine_opts = {
239 .name = "machine",
240 .implied_opt_name = "type",
241 .merge_lists = true,
242 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
243 .desc = {
245 * no elements => accept any
246 * sanity checking will happen later
247 * when setting machine properties
253 static QemuOptsList qemu_accel_opts = {
254 .name = "accel",
255 .implied_opt_name = "accel",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
257 .desc = {
259 * no elements => accept any
260 * sanity checking will happen later
261 * when setting accelerator properties
267 static QemuOptsList qemu_boot_opts = {
268 .name = "boot-opts",
269 .implied_opt_name = "order",
270 .merge_lists = true,
271 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
272 .desc = {
274 .name = "order",
275 .type = QEMU_OPT_STRING,
276 }, {
277 .name = "once",
278 .type = QEMU_OPT_STRING,
279 }, {
280 .name = "menu",
281 .type = QEMU_OPT_BOOL,
282 }, {
283 .name = "splash",
284 .type = QEMU_OPT_STRING,
285 }, {
286 .name = "splash-time",
287 .type = QEMU_OPT_NUMBER,
288 }, {
289 .name = "reboot-timeout",
290 .type = QEMU_OPT_NUMBER,
291 }, {
292 .name = "strict",
293 .type = QEMU_OPT_BOOL,
295 { /*End of list */ }
299 static QemuOptsList qemu_add_fd_opts = {
300 .name = "add-fd",
301 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
302 .desc = {
304 .name = "fd",
305 .type = QEMU_OPT_NUMBER,
306 .help = "file descriptor of which a duplicate is added to fd set",
308 .name = "set",
309 .type = QEMU_OPT_NUMBER,
310 .help = "ID of the fd set to add fd to",
312 .name = "opaque",
313 .type = QEMU_OPT_STRING,
314 .help = "free-form string used to describe fd",
316 { /* end of list */ }
320 static QemuOptsList qemu_object_opts = {
321 .name = "object",
322 .implied_opt_name = "qom-type",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
324 .desc = {
329 static QemuOptsList qemu_tpmdev_opts = {
330 .name = "tpmdev",
331 .implied_opt_name = "type",
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
333 .desc = {
334 /* options are defined in the TPM backends */
335 { /* end of list */ }
339 static QemuOptsList qemu_overcommit_opts = {
340 .name = "overcommit",
341 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
342 .desc = {
344 .name = "mem-lock",
345 .type = QEMU_OPT_BOOL,
348 .name = "cpu-pm",
349 .type = QEMU_OPT_BOOL,
351 { /* end of list */ }
355 static QemuOptsList qemu_msg_opts = {
356 .name = "msg",
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
358 .desc = {
360 .name = "timestamp",
361 .type = QEMU_OPT_BOOL,
364 .name = "guest-name",
365 .type = QEMU_OPT_BOOL,
366 .help = "Prepends guest name for error messages but only if "
367 "-name guest is set otherwise option is ignored\n",
369 { /* end of list */ }
373 static QemuOptsList qemu_name_opts = {
374 .name = "name",
375 .implied_opt_name = "guest",
376 .merge_lists = true,
377 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
378 .desc = {
380 .name = "guest",
381 .type = QEMU_OPT_STRING,
382 .help = "Sets the name of the guest.\n"
383 "This name will be displayed in the SDL window caption.\n"
384 "The name will also be used for the VNC server",
385 }, {
386 .name = "process",
387 .type = QEMU_OPT_STRING,
388 .help = "Sets the name of the QEMU process, as shown in top etc",
389 }, {
390 .name = "debug-threads",
391 .type = QEMU_OPT_BOOL,
392 .help = "When enabled, name the individual threads; defaults off.\n"
393 "NOTE: The thread names are for debugging and not a\n"
394 "stable API.",
396 { /* End of list */ }
400 static QemuOptsList qemu_mem_opts = {
401 .name = "memory",
402 .implied_opt_name = "size",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
404 .merge_lists = true,
405 .desc = {
407 .name = "size",
408 .type = QEMU_OPT_SIZE,
411 .name = "slots",
412 .type = QEMU_OPT_NUMBER,
415 .name = "maxmem",
416 .type = QEMU_OPT_SIZE,
418 { /* end of list */ }
422 static QemuOptsList qemu_icount_opts = {
423 .name = "icount",
424 .implied_opt_name = "shift",
425 .merge_lists = true,
426 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
427 .desc = {
429 .name = "shift",
430 .type = QEMU_OPT_STRING,
431 }, {
432 .name = "align",
433 .type = QEMU_OPT_BOOL,
434 }, {
435 .name = "sleep",
436 .type = QEMU_OPT_BOOL,
437 }, {
438 .name = "rr",
439 .type = QEMU_OPT_STRING,
440 }, {
441 .name = "rrfile",
442 .type = QEMU_OPT_STRING,
443 }, {
444 .name = "rrsnapshot",
445 .type = QEMU_OPT_STRING,
447 { /* end of list */ }
451 static QemuOptsList qemu_fw_cfg_opts = {
452 .name = "fw_cfg",
453 .implied_opt_name = "name",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
455 .desc = {
457 .name = "name",
458 .type = QEMU_OPT_STRING,
459 .help = "Sets the fw_cfg name of the blob to be inserted",
460 }, {
461 .name = "file",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets the name of the file from which "
464 "the fw_cfg blob will be loaded",
465 }, {
466 .name = "string",
467 .type = QEMU_OPT_STRING,
468 .help = "Sets content of the blob to be inserted from a string",
469 }, {
470 .name = "gen_id",
471 .type = QEMU_OPT_STRING,
472 .help = "Sets id of the object generating the fw_cfg blob "
473 "to be inserted",
475 { /* end of list */ }
479 static QemuOptsList qemu_action_opts = {
480 .name = "action",
481 .merge_lists = true,
482 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
483 .desc = {
485 .name = "shutdown",
486 .type = QEMU_OPT_STRING,
488 .name = "reboot",
489 .type = QEMU_OPT_STRING,
491 .name = "panic",
492 .type = QEMU_OPT_STRING,
494 .name = "watchdog",
495 .type = QEMU_OPT_STRING,
497 { /* end of list */ }
502 * Get machine options
504 * Returns: machine options (never null).
506 static QemuOpts *qemu_get_machine_opts(void)
508 return qemu_find_opts_singleton("machine");
511 const char *qemu_get_vm_name(void)
513 return qemu_name;
516 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
518 const char *driver = qemu_opt_get(opts, "driver");
519 int i;
521 if (!driver)
522 return 0;
523 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
524 if (strcmp(default_list[i].driver, driver) != 0)
525 continue;
526 *(default_list[i].flag) = 0;
528 return 0;
531 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
533 const char *proc_name;
535 if (qemu_opt_get(opts, "debug-threads")) {
536 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
538 qemu_name = qemu_opt_get(opts, "guest");
540 proc_name = qemu_opt_get(opts, "process");
541 if (proc_name) {
542 os_set_proc_name(proc_name);
545 return 0;
548 bool defaults_enabled(void)
550 return has_defaults;
553 #ifndef _WIN32
554 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
556 int fd, dupfd, flags;
557 int64_t fdset_id;
558 const char *fd_opaque = NULL;
559 AddfdInfo *fdinfo;
561 fd = qemu_opt_get_number(opts, "fd", -1);
562 fdset_id = qemu_opt_get_number(opts, "set", -1);
563 fd_opaque = qemu_opt_get(opts, "opaque");
565 if (fd < 0) {
566 error_setg(errp, "fd option is required and must be non-negative");
567 return -1;
570 if (fd <= STDERR_FILENO) {
571 error_setg(errp, "fd cannot be a standard I/O stream");
572 return -1;
576 * All fds inherited across exec() necessarily have FD_CLOEXEC
577 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
579 flags = fcntl(fd, F_GETFD);
580 if (flags == -1 || (flags & FD_CLOEXEC)) {
581 error_setg(errp, "fd is not valid or already in use");
582 return -1;
585 if (fdset_id < 0) {
586 error_setg(errp, "set option is required and must be non-negative");
587 return -1;
590 #ifdef F_DUPFD_CLOEXEC
591 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
592 #else
593 dupfd = dup(fd);
594 if (dupfd != -1) {
595 qemu_set_cloexec(dupfd);
597 #endif
598 if (dupfd == -1) {
599 error_setg(errp, "error duplicating fd: %s", strerror(errno));
600 return -1;
603 /* add the duplicate fd, and optionally the opaque string, to the fd set */
604 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
605 &error_abort);
606 g_free(fdinfo);
608 return 0;
611 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
613 int fd;
615 fd = qemu_opt_get_number(opts, "fd", -1);
616 close(fd);
618 return 0;
620 #endif
622 /***********************************************************/
623 /* QEMU Block devices */
625 #define HD_OPTS "media=disk"
626 #define CDROM_OPTS "media=cdrom"
627 #define FD_OPTS ""
628 #define PFLASH_OPTS ""
629 #define MTD_OPTS ""
630 #define SD_OPTS ""
632 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
634 BlockInterfaceType *block_default_type = opaque;
636 return drive_new(opts, *block_default_type, errp) == NULL;
639 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
641 if (qemu_opt_get(opts, "snapshot") == NULL) {
642 qemu_opt_set(opts, "snapshot", "on", &error_abort);
644 return 0;
647 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
648 int index, const char *optstr)
650 QemuOpts *opts;
651 DriveInfo *dinfo;
653 if (!enable || drive_get_by_index(type, index)) {
654 return;
657 opts = drive_add(type, index, NULL, optstr);
658 if (snapshot) {
659 drive_enable_snapshot(NULL, opts, NULL);
662 dinfo = drive_new(opts, type, &error_abort);
663 dinfo->is_default = true;
667 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
668 MachineClass *machine_class, int snapshot)
671 * If the currently selected machine wishes to override the
672 * units-per-bus property of its default HBA interface type, do so
673 * now.
675 if (machine_class->units_per_default_bus) {
676 override_max_devs(machine_class->block_default_type,
677 machine_class->units_per_default_bus);
680 /* open the virtual block devices */
681 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
682 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
684 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
685 loc_push_restore(&bdo->loc);
686 qmp_blockdev_add(bdo->bdo, &error_fatal);
687 loc_pop(&bdo->loc);
688 qapi_free_BlockdevOptions(bdo->bdo);
689 g_free(bdo);
691 if (snapshot) {
692 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
693 NULL, NULL);
695 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
696 &machine_class->block_default_type, &error_fatal)) {
697 /* We printed help */
698 exit(0);
701 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
702 CDROM_OPTS);
703 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
704 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
708 static QemuOptsList qemu_smp_opts = {
709 .name = "smp-opts",
710 .implied_opt_name = "cpus",
711 .merge_lists = true,
712 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
713 .desc = {
715 .name = "cpus",
716 .type = QEMU_OPT_NUMBER,
717 }, {
718 .name = "sockets",
719 .type = QEMU_OPT_NUMBER,
720 }, {
721 .name = "dies",
722 .type = QEMU_OPT_NUMBER,
723 }, {
724 .name = "cores",
725 .type = QEMU_OPT_NUMBER,
726 }, {
727 .name = "threads",
728 .type = QEMU_OPT_NUMBER,
729 }, {
730 .name = "maxcpus",
731 .type = QEMU_OPT_NUMBER,
733 { /*End of list */ }
737 static void realtime_init(void)
739 if (enable_mlock) {
740 if (os_mlock() < 0) {
741 error_report("locking memory failed");
742 exit(1);
748 static void configure_msg(QemuOpts *opts)
750 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
751 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
755 /***********************************************************/
756 /* USB devices */
758 static int usb_device_add(const char *devname)
760 USBDevice *dev = NULL;
762 if (!machine_usb(current_machine)) {
763 return -1;
766 dev = usbdevice_create(devname);
767 if (!dev)
768 return -1;
770 return 0;
773 static int usb_parse(const char *cmdline)
775 int r;
776 r = usb_device_add(cmdline);
777 if (r < 0) {
778 error_report("could not add USB device '%s'", cmdline);
780 return r;
783 /***********************************************************/
784 /* machine registration */
786 static MachineClass *find_machine(const char *name, GSList *machines)
788 GSList *el;
790 for (el = machines; el; el = el->next) {
791 MachineClass *mc = el->data;
793 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
794 return mc;
798 return NULL;
801 static MachineClass *find_default_machine(GSList *machines)
803 GSList *el;
804 MachineClass *default_machineclass = NULL;
806 for (el = machines; el; el = el->next) {
807 MachineClass *mc = el->data;
809 if (mc->is_default) {
810 assert(default_machineclass == NULL && "Multiple default machines");
811 default_machineclass = mc;
815 return default_machineclass;
818 static int machine_help_func(QemuOpts *opts, MachineState *machine)
820 ObjectProperty *prop;
821 ObjectPropertyIterator iter;
823 if (!qemu_opt_has_help_opt(opts)) {
824 return 0;
827 object_property_iter_init(&iter, OBJECT(machine));
828 while ((prop = object_property_iter_next(&iter))) {
829 if (!prop->set) {
830 continue;
833 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
834 prop->name, prop->type);
835 if (prop->description) {
836 printf(" (%s)\n", prop->description);
837 } else {
838 printf("\n");
842 return 1;
845 static void version(void)
847 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
848 QEMU_COPYRIGHT "\n");
851 static void help(int exitcode)
853 version();
854 printf("usage: %s [options] [disk_image]\n\n"
855 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
856 error_get_progname());
858 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
859 if ((arch_mask) & arch_type) \
860 fputs(opt_help, stdout);
862 #define ARCHHEADING(text, arch_mask) \
863 if ((arch_mask) & arch_type) \
864 puts(stringify(text));
866 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
868 #include "qemu-options.def"
870 printf("\nDuring emulation, the following keys are useful:\n"
871 "ctrl-alt-f toggle full screen\n"
872 "ctrl-alt-n switch to virtual console 'n'\n"
873 "ctrl-alt toggle mouse and keyboard grab\n"
874 "\n"
875 "When using -nographic, press 'ctrl-a h' to get some help.\n"
876 "\n"
877 QEMU_HELP_BOTTOM "\n");
879 exit(exitcode);
882 #define HAS_ARG 0x0001
884 typedef struct QEMUOption {
885 const char *name;
886 int flags;
887 int index;
888 uint32_t arch_mask;
889 } QEMUOption;
891 static const QEMUOption qemu_options[] = {
892 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
894 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
895 { option, opt_arg, opt_enum, arch_mask },
896 #define DEFHEADING(text)
897 #define ARCHHEADING(text, arch_mask)
899 #include "qemu-options.def"
900 { NULL },
903 typedef struct VGAInterfaceInfo {
904 const char *opt_name; /* option name */
905 const char *name; /* human-readable name */
906 /* Class names indicating that support is available.
907 * If no class is specified, the interface is always available */
908 const char *class_names[2];
909 } VGAInterfaceInfo;
911 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
912 [VGA_NONE] = {
913 .opt_name = "none",
914 .name = "no graphic card",
916 [VGA_STD] = {
917 .opt_name = "std",
918 .name = "standard VGA",
919 .class_names = { "VGA", "isa-vga" },
921 [VGA_CIRRUS] = {
922 .opt_name = "cirrus",
923 .name = "Cirrus VGA",
924 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
926 [VGA_VMWARE] = {
927 .opt_name = "vmware",
928 .name = "VMWare SVGA",
929 .class_names = { "vmware-svga" },
931 [VGA_VIRTIO] = {
932 .opt_name = "virtio",
933 .name = "Virtio VGA",
934 .class_names = { "virtio-vga" },
936 [VGA_QXL] = {
937 .opt_name = "qxl",
938 .name = "QXL VGA",
939 .class_names = { "qxl-vga" },
941 [VGA_TCX] = {
942 .opt_name = "tcx",
943 .name = "TCX framebuffer",
944 .class_names = { "sun-tcx" },
946 [VGA_CG3] = {
947 .opt_name = "cg3",
948 .name = "CG3 framebuffer",
949 .class_names = { "cgthree" },
951 [VGA_XENFB] = {
952 .opt_name = "xenfb",
953 .name = "Xen paravirtualized framebuffer",
957 static bool vga_interface_available(VGAInterfaceType t)
959 const VGAInterfaceInfo *ti = &vga_interfaces[t];
961 assert(t < VGA_TYPE_MAX);
962 return !ti->class_names[0] ||
963 module_object_class_by_name(ti->class_names[0]) ||
964 module_object_class_by_name(ti->class_names[1]);
967 static const char *
968 get_default_vga_model(const MachineClass *machine_class)
970 if (machine_class->default_display) {
971 return machine_class->default_display;
972 } else if (vga_interface_available(VGA_CIRRUS)) {
973 return "cirrus";
974 } else if (vga_interface_available(VGA_STD)) {
975 return "std";
978 return NULL;
981 static void select_vgahw(const MachineClass *machine_class, const char *p)
983 const char *opts;
984 int t;
986 if (g_str_equal(p, "help")) {
987 const char *def = get_default_vga_model(machine_class);
989 for (t = 0; t < VGA_TYPE_MAX; t++) {
990 const VGAInterfaceInfo *ti = &vga_interfaces[t];
992 if (vga_interface_available(t) && ti->opt_name) {
993 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
994 g_str_equal(ti->opt_name, def) ? " (default)" : "");
997 exit(0);
1000 assert(vga_interface_type == VGA_NONE);
1001 for (t = 0; t < VGA_TYPE_MAX; t++) {
1002 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1003 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1004 if (!vga_interface_available(t)) {
1005 error_report("%s not available", ti->name);
1006 exit(1);
1008 vga_interface_type = t;
1009 break;
1012 if (t == VGA_TYPE_MAX) {
1013 invalid_vga:
1014 error_report("unknown vga type: %s", p);
1015 exit(1);
1017 while (*opts) {
1018 const char *nextopt;
1020 if (strstart(opts, ",retrace=", &nextopt)) {
1021 opts = nextopt;
1022 if (strstart(opts, "dumb", &nextopt))
1023 vga_retrace_method = VGA_RETRACE_DUMB;
1024 else if (strstart(opts, "precise", &nextopt))
1025 vga_retrace_method = VGA_RETRACE_PRECISE;
1026 else goto invalid_vga;
1027 } else goto invalid_vga;
1028 opts = nextopt;
1032 static void parse_display_qapi(const char *optarg)
1034 DisplayOptions *opts;
1035 Visitor *v;
1037 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1039 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1040 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1042 qapi_free_DisplayOptions(opts);
1043 visit_free(v);
1046 DisplayOptions *qmp_query_display_options(Error **errp)
1048 return QAPI_CLONE(DisplayOptions, &dpy);
1051 static void parse_display(const char *p)
1053 const char *opts;
1055 if (is_help_option(p)) {
1056 qemu_display_help();
1057 exit(0);
1060 if (strstart(p, "sdl", &opts)) {
1062 * sdl DisplayType needs hand-crafted parser instead of
1063 * parse_display_qapi() due to some options not in
1064 * DisplayOptions, specifically:
1065 * - frame
1066 * Already deprecated.
1067 * - ctrl_grab + alt_grab
1068 * Not clear yet what happens to them long-term. Should
1069 * replaced by something better or deprecated and dropped.
1071 dpy.type = DISPLAY_TYPE_SDL;
1072 while (*opts) {
1073 const char *nextopt;
1075 if (strstart(opts, ",alt_grab=", &nextopt)) {
1076 opts = nextopt;
1077 if (strstart(opts, "on", &nextopt)) {
1078 alt_grab = 1;
1079 } else if (strstart(opts, "off", &nextopt)) {
1080 alt_grab = 0;
1081 } else {
1082 goto invalid_sdl_args;
1084 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1085 opts = nextopt;
1086 if (strstart(opts, "on", &nextopt)) {
1087 ctrl_grab = 1;
1088 } else if (strstart(opts, "off", &nextopt)) {
1089 ctrl_grab = 0;
1090 } else {
1091 goto invalid_sdl_args;
1093 } else if (strstart(opts, ",window_close=", &nextopt)) {
1094 opts = nextopt;
1095 dpy.has_window_close = true;
1096 if (strstart(opts, "on", &nextopt)) {
1097 dpy.window_close = true;
1098 } else if (strstart(opts, "off", &nextopt)) {
1099 dpy.window_close = false;
1100 } else {
1101 goto invalid_sdl_args;
1103 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1104 opts = nextopt;
1105 dpy.has_show_cursor = true;
1106 if (strstart(opts, "on", &nextopt)) {
1107 dpy.show_cursor = true;
1108 } else if (strstart(opts, "off", &nextopt)) {
1109 dpy.show_cursor = false;
1110 } else {
1111 goto invalid_sdl_args;
1113 } else if (strstart(opts, ",gl=", &nextopt)) {
1114 opts = nextopt;
1115 dpy.has_gl = true;
1116 if (strstart(opts, "on", &nextopt)) {
1117 dpy.gl = DISPLAYGL_MODE_ON;
1118 } else if (strstart(opts, "core", &nextopt)) {
1119 dpy.gl = DISPLAYGL_MODE_CORE;
1120 } else if (strstart(opts, "es", &nextopt)) {
1121 dpy.gl = DISPLAYGL_MODE_ES;
1122 } else if (strstart(opts, "off", &nextopt)) {
1123 dpy.gl = DISPLAYGL_MODE_OFF;
1124 } else {
1125 goto invalid_sdl_args;
1127 } else {
1128 invalid_sdl_args:
1129 error_report("invalid SDL option string");
1130 exit(1);
1132 opts = nextopt;
1134 } else if (strstart(p, "vnc", &opts)) {
1136 * vnc isn't a (local) DisplayType but a protocol for remote
1137 * display access.
1139 if (*opts == '=') {
1140 vnc_parse(opts + 1);
1141 } else {
1142 error_report("VNC requires a display argument vnc=<display>");
1143 exit(1);
1145 } else {
1146 parse_display_qapi(p);
1150 static inline bool nonempty_str(const char *str)
1152 return str && *str;
1155 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1157 gchar *buf;
1158 size_t size;
1159 const char *name, *file, *str, *gen_id;
1160 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1162 if (fw_cfg == NULL) {
1163 error_setg(errp, "fw_cfg device not available");
1164 return -1;
1166 name = qemu_opt_get(opts, "name");
1167 file = qemu_opt_get(opts, "file");
1168 str = qemu_opt_get(opts, "string");
1169 gen_id = qemu_opt_get(opts, "gen_id");
1171 /* we need the name, and exactly one of: file, content string, gen_id */
1172 if (!nonempty_str(name) ||
1173 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1174 error_setg(errp, "name, plus exactly one of file,"
1175 " string and gen_id, are needed");
1176 return -1;
1178 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1179 error_setg(errp, "name too long (max. %d char)",
1180 FW_CFG_MAX_FILE_PATH - 1);
1181 return -1;
1183 if (nonempty_str(gen_id)) {
1185 * In this particular case where the content is populated
1186 * internally, the "etc/" namespace protection is relaxed,
1187 * so do not emit a warning.
1189 } else if (strncmp(name, "opt/", 4) != 0) {
1190 warn_report("externally provided fw_cfg item names "
1191 "should be prefixed with \"opt/\"");
1193 if (nonempty_str(str)) {
1194 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1195 buf = g_memdup(str, size);
1196 } else if (nonempty_str(gen_id)) {
1197 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1198 return -1;
1200 return 0;
1201 } else {
1202 GError *err = NULL;
1203 if (!g_file_get_contents(file, &buf, &size, &err)) {
1204 error_setg(errp, "can't load %s: %s", file, err->message);
1205 g_error_free(err);
1206 return -1;
1209 /* For legacy, keep user files in a specific global order. */
1210 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1211 fw_cfg_add_file(fw_cfg, name, buf, size);
1212 fw_cfg_reset_order_override(fw_cfg);
1213 return 0;
1216 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1218 return qdev_device_help(opts);
1221 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1223 DeviceState *dev;
1225 dev = qdev_device_add(opts, errp);
1226 if (!dev && *errp) {
1227 error_report_err(*errp);
1228 return -1;
1229 } else if (dev) {
1230 object_unref(OBJECT(dev));
1232 return 0;
1235 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1237 Error *local_err = NULL;
1239 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1240 if (local_err) {
1241 error_propagate(errp, local_err);
1242 return -1;
1244 exit(0);
1246 return 0;
1249 #ifdef CONFIG_VIRTFS
1250 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1252 return qemu_fsdev_add(opts, errp);
1254 #endif
1256 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1258 return monitor_init_opts(opts, errp);
1261 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1263 static int monitor_device_index = 0;
1264 QemuOpts *opts;
1265 const char *p;
1266 char label[32];
1268 if (strstart(optarg, "chardev:", &p)) {
1269 snprintf(label, sizeof(label), "%s", p);
1270 } else {
1271 snprintf(label, sizeof(label), "compat_monitor%d",
1272 monitor_device_index);
1273 opts = qemu_chr_parse_compat(label, optarg, true);
1274 if (!opts) {
1275 error_report("parse error: %s", optarg);
1276 exit(1);
1280 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1281 qemu_opt_set(opts, "mode", mode, &error_abort);
1282 qemu_opt_set(opts, "chardev", label, &error_abort);
1283 if (!strcmp(mode, "control")) {
1284 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1285 } else {
1286 assert(pretty == false);
1288 monitor_device_index++;
1291 struct device_config {
1292 enum {
1293 DEV_USB, /* -usbdevice */
1294 DEV_SERIAL, /* -serial */
1295 DEV_PARALLEL, /* -parallel */
1296 DEV_DEBUGCON, /* -debugcon */
1297 DEV_GDB, /* -gdb, -s */
1298 DEV_SCLP, /* s390 sclp */
1299 } type;
1300 const char *cmdline;
1301 Location loc;
1302 QTAILQ_ENTRY(device_config) next;
1305 static QTAILQ_HEAD(, device_config) device_configs =
1306 QTAILQ_HEAD_INITIALIZER(device_configs);
1308 static void add_device_config(int type, const char *cmdline)
1310 struct device_config *conf;
1312 conf = g_malloc0(sizeof(*conf));
1313 conf->type = type;
1314 conf->cmdline = cmdline;
1315 loc_save(&conf->loc);
1316 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1319 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1321 struct device_config *conf;
1322 int rc;
1324 QTAILQ_FOREACH(conf, &device_configs, next) {
1325 if (conf->type != type)
1326 continue;
1327 loc_push_restore(&conf->loc);
1328 rc = func(conf->cmdline);
1329 loc_pop(&conf->loc);
1330 if (rc) {
1331 return rc;
1334 return 0;
1337 static void qemu_disable_default_devices(void)
1339 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1341 qemu_opts_foreach(qemu_find_opts("device"),
1342 default_driver_check, NULL, NULL);
1343 qemu_opts_foreach(qemu_find_opts("global"),
1344 default_driver_check, NULL, NULL);
1346 if (!vga_model && !default_vga) {
1347 vga_interface_type = VGA_DEVICE;
1349 if (!has_defaults || machine_class->no_serial) {
1350 default_serial = 0;
1352 if (!has_defaults || machine_class->no_parallel) {
1353 default_parallel = 0;
1355 if (!has_defaults || machine_class->no_floppy) {
1356 default_floppy = 0;
1358 if (!has_defaults || machine_class->no_cdrom) {
1359 default_cdrom = 0;
1361 if (!has_defaults || machine_class->no_sdcard) {
1362 default_sdcard = 0;
1364 if (!has_defaults) {
1365 default_monitor = 0;
1366 default_net = 0;
1367 default_vga = 0;
1371 static void qemu_create_default_devices(void)
1373 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1375 if (is_daemonized()) {
1376 /* According to documentation and historically, -nographic redirects
1377 * serial port, parallel port and monitor to stdio, which does not work
1378 * with -daemonize. We can redirect these to null instead, but since
1379 * -nographic is legacy, let's just error out.
1380 * We disallow -nographic only if all other ports are not redirected
1381 * explicitly, to not break existing legacy setups which uses
1382 * -nographic _and_ redirects all ports explicitly - this is valid
1383 * usage, -nographic is just a no-op in this case.
1385 if (nographic
1386 && (default_parallel || default_serial || default_monitor)) {
1387 error_report("-nographic cannot be used with -daemonize");
1388 exit(1);
1392 if (nographic) {
1393 if (default_parallel)
1394 add_device_config(DEV_PARALLEL, "null");
1395 if (default_serial && default_monitor) {
1396 add_device_config(DEV_SERIAL, "mon:stdio");
1397 } else {
1398 if (default_serial)
1399 add_device_config(DEV_SERIAL, "stdio");
1400 if (default_monitor)
1401 monitor_parse("stdio", "readline", false);
1403 } else {
1404 if (default_serial)
1405 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1406 if (default_parallel)
1407 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1408 if (default_monitor)
1409 monitor_parse("vc:80Cx24C", "readline", false);
1412 if (default_net) {
1413 QemuOptsList *net = qemu_find_opts("net");
1414 qemu_opts_parse(net, "nic", true, &error_abort);
1415 #ifdef CONFIG_SLIRP
1416 qemu_opts_parse(net, "user", true, &error_abort);
1417 #endif
1420 #if defined(CONFIG_VNC)
1421 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1422 display_remote++;
1424 #endif
1425 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1426 if (!qemu_display_find_default(&dpy)) {
1427 dpy.type = DISPLAY_TYPE_NONE;
1428 #if defined(CONFIG_VNC)
1429 vnc_parse("localhost:0,to=99,id=default");
1430 #endif
1433 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1434 dpy.type = DISPLAY_TYPE_NONE;
1437 /* If no default VGA is requested, the default is "none". */
1438 if (default_vga) {
1439 vga_model = get_default_vga_model(machine_class);
1441 if (vga_model) {
1442 select_vgahw(machine_class, vga_model);
1446 static int serial_parse(const char *devname)
1448 int index = num_serial_hds;
1449 char label[32];
1451 if (strcmp(devname, "none") == 0)
1452 return 0;
1453 snprintf(label, sizeof(label), "serial%d", index);
1454 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1456 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1457 if (!serial_hds[index]) {
1458 error_report("could not connect serial device"
1459 " to character backend '%s'", devname);
1460 return -1;
1462 num_serial_hds++;
1463 return 0;
1466 Chardev *serial_hd(int i)
1468 assert(i >= 0);
1469 if (i < num_serial_hds) {
1470 return serial_hds[i];
1472 return NULL;
1475 static int parallel_parse(const char *devname)
1477 static int index = 0;
1478 char label[32];
1480 if (strcmp(devname, "none") == 0)
1481 return 0;
1482 if (index == MAX_PARALLEL_PORTS) {
1483 error_report("too many parallel ports");
1484 exit(1);
1486 snprintf(label, sizeof(label), "parallel%d", index);
1487 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1488 if (!parallel_hds[index]) {
1489 error_report("could not connect parallel device"
1490 " to character backend '%s'", devname);
1491 return -1;
1493 index++;
1494 return 0;
1497 static int debugcon_parse(const char *devname)
1499 QemuOpts *opts;
1501 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1502 error_report("invalid character backend '%s'", devname);
1503 exit(1);
1505 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1506 if (!opts) {
1507 error_report("already have a debugcon device");
1508 exit(1);
1510 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1511 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1512 return 0;
1515 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1517 const MachineClass *mc1 = a, *mc2 = b;
1518 int res;
1520 if (mc1->family == NULL) {
1521 if (mc2->family == NULL) {
1522 /* Compare standalone machine types against each other; they sort
1523 * in increasing order.
1525 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1526 object_class_get_name(OBJECT_CLASS(mc2)));
1529 /* Standalone machine types sort after families. */
1530 return 1;
1533 if (mc2->family == NULL) {
1534 /* Families sort before standalone machine types. */
1535 return -1;
1538 /* Families sort between each other alphabetically increasingly. */
1539 res = strcmp(mc1->family, mc2->family);
1540 if (res != 0) {
1541 return res;
1544 /* Within the same family, machine types sort in decreasing order. */
1545 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1546 object_class_get_name(OBJECT_CLASS(mc1)));
1549 static MachineClass *machine_parse(const char *name, GSList *machines)
1551 MachineClass *mc;
1552 GSList *el;
1554 if (is_help_option(name)) {
1555 printf("Supported machines are:\n");
1556 machines = g_slist_sort(machines, machine_class_cmp);
1557 for (el = machines; el; el = el->next) {
1558 MachineClass *mc = el->data;
1559 if (mc->alias) {
1560 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1562 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1563 mc->is_default ? " (default)" : "",
1564 mc->deprecation_reason ? " (deprecated)" : "");
1566 exit(0);
1569 mc = find_machine(name, machines);
1570 if (!mc) {
1571 error_report("unsupported machine type");
1572 error_printf("Use -machine help to list supported machines\n");
1573 exit(1);
1575 return mc;
1578 static const char *pid_file;
1579 static Notifier qemu_unlink_pidfile_notifier;
1581 static void qemu_unlink_pidfile(Notifier *n, void *data)
1583 if (pid_file) {
1584 unlink(pid_file);
1588 static const QEMUOption *lookup_opt(int argc, char **argv,
1589 const char **poptarg, int *poptind)
1591 const QEMUOption *popt;
1592 int optind = *poptind;
1593 char *r = argv[optind];
1594 const char *optarg;
1596 loc_set_cmdline(argv, optind, 1);
1597 optind++;
1598 /* Treat --foo the same as -foo. */
1599 if (r[1] == '-')
1600 r++;
1601 popt = qemu_options;
1602 for(;;) {
1603 if (!popt->name) {
1604 error_report("invalid option");
1605 exit(1);
1607 if (!strcmp(popt->name, r + 1))
1608 break;
1609 popt++;
1611 if (popt->flags & HAS_ARG) {
1612 if (optind >= argc) {
1613 error_report("requires an argument");
1614 exit(1);
1616 optarg = argv[optind++];
1617 loc_set_cmdline(argv, optind - 2, 2);
1618 } else {
1619 optarg = NULL;
1622 *poptarg = optarg;
1623 *poptind = optind;
1625 return popt;
1628 static MachineClass *select_machine(void)
1630 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1631 MachineClass *machine_class = find_default_machine(machines);
1632 const char *optarg;
1633 QemuOpts *opts;
1634 Location loc;
1636 loc_push_none(&loc);
1638 opts = qemu_get_machine_opts();
1639 qemu_opts_loc_restore(opts);
1641 optarg = qemu_opt_get(opts, "type");
1642 if (optarg) {
1643 machine_class = machine_parse(optarg, machines);
1646 if (!machine_class) {
1647 error_report("No machine specified, and there is no default");
1648 error_printf("Use -machine help to list supported machines\n");
1649 exit(1);
1652 loc_pop(&loc);
1653 g_slist_free(machines);
1654 return machine_class;
1657 static int object_parse_property_opt(Object *obj,
1658 const char *name, const char *value,
1659 const char *skip, Error **errp)
1661 if (g_str_equal(name, skip)) {
1662 return 0;
1665 if (!object_property_parse(obj, name, value, errp)) {
1666 return -1;
1669 return 0;
1672 static int machine_set_property(void *opaque,
1673 const char *name, const char *value,
1674 Error **errp)
1676 g_autofree char *qom_name = g_strdup(name);
1677 char *p;
1679 for (p = qom_name; *p; p++) {
1680 if (*p == '_') {
1681 *p = '-';
1685 /* Legacy options do not correspond to MachineState properties. */
1686 if (g_str_equal(qom_name, "accel")) {
1687 return 0;
1689 if (g_str_equal(qom_name, "igd-passthru")) {
1690 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value,
1691 false);
1692 return 0;
1694 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
1695 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1696 false);
1697 return 0;
1699 if (g_str_equal(qom_name, "kernel-irqchip")) {
1700 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value,
1701 false);
1702 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value,
1703 false);
1704 return 0;
1707 return object_parse_property_opt(opaque, name, value, "type", errp);
1710 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1712 ObjectOption *opt, *next;
1714 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1715 const char *type = ObjectType_str(opt->opts->qom_type);
1716 if (type_opt_predicate(type)) {
1717 user_creatable_add_qapi(opt->opts, &error_fatal);
1718 qapi_free_ObjectOptions(opt->opts);
1719 QTAILQ_REMOVE(&object_opts, opt, next);
1720 g_free(opt);
1725 static void object_option_add_visitor(Visitor *v)
1727 ObjectOption *opt = g_new0(ObjectOption, 1);
1728 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1729 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1732 static void object_option_parse(const char *optarg)
1734 QemuOpts *opts;
1735 const char *type;
1736 Visitor *v;
1738 if (optarg[0] == '{') {
1739 QObject *obj = qobject_from_json(optarg, &error_fatal);
1741 v = qobject_input_visitor_new(obj);
1742 qobject_unref(obj);
1743 } else {
1744 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1745 optarg, true);
1746 if (!opts) {
1747 exit(1);
1750 type = qemu_opt_get(opts, "qom-type");
1751 if (!type) {
1752 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1754 if (user_creatable_print_help(type, opts)) {
1755 exit(0);
1758 v = opts_visitor_new(opts);
1761 object_option_add_visitor(v);
1762 visit_free(v);
1766 * Initial object creation happens before all other
1767 * QEMU data types are created. The majority of objects
1768 * can be created at this point. The rng-egd object
1769 * cannot be created here, as it depends on the chardev
1770 * already existing.
1772 static bool object_create_early(const char *type)
1775 * Objects should not be made "delayed" without a reason. If you
1776 * add one, state the reason in a comment!
1779 /* Reason: property "chardev" */
1780 if (g_str_equal(type, "rng-egd") ||
1781 g_str_equal(type, "qtest")) {
1782 return false;
1785 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1786 /* Reason: cryptodev-vhost-user property "chardev" */
1787 if (g_str_equal(type, "cryptodev-vhost-user")) {
1788 return false;
1790 #endif
1792 /* Reason: vhost-user-blk-server property "node-name" */
1793 if (g_str_equal(type, "vhost-user-blk-server")) {
1794 return false;
1797 * Reason: filter-* property "netdev" etc.
1799 if (g_str_equal(type, "filter-buffer") ||
1800 g_str_equal(type, "filter-dump") ||
1801 g_str_equal(type, "filter-mirror") ||
1802 g_str_equal(type, "filter-redirector") ||
1803 g_str_equal(type, "colo-compare") ||
1804 g_str_equal(type, "filter-rewriter") ||
1805 g_str_equal(type, "filter-replay")) {
1806 return false;
1810 * Allocation of large amounts of memory may delay
1811 * chardev initialization for too long, and trigger timeouts
1812 * on software that waits for a monitor socket to be created
1813 * (e.g. libvirt).
1815 if (g_str_has_prefix(type, "memory-backend-")) {
1816 return false;
1819 return true;
1822 static void qemu_apply_machine_options(void)
1824 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1825 QemuOpts *machine_opts = qemu_get_machine_opts();
1826 const char *boot_order = NULL;
1827 const char *boot_once = NULL;
1828 QemuOpts *opts;
1830 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
1831 &error_fatal);
1832 current_machine->ram_size = ram_size;
1833 current_machine->maxram_size = maxram_size;
1834 current_machine->ram_slots = ram_slots;
1836 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1837 if (opts) {
1838 boot_order = qemu_opt_get(opts, "order");
1839 if (boot_order) {
1840 validate_bootdevices(boot_order, &error_fatal);
1843 boot_once = qemu_opt_get(opts, "once");
1844 if (boot_once) {
1845 validate_bootdevices(boot_once, &error_fatal);
1848 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1849 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1852 if (!boot_order) {
1853 boot_order = machine_class->default_boot_order;
1856 current_machine->boot_order = boot_order;
1857 current_machine->boot_once = boot_once;
1859 if (semihosting_enabled() && !semihosting_get_argc()) {
1860 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
1861 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append") ?: "";
1862 /* fall back to the -kernel/-append */
1863 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
1867 static void qemu_create_early_backends(void)
1869 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1871 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
1872 error_report("-alt-grab and -ctrl-grab are only valid "
1873 "for SDL, ignoring option");
1875 if (dpy.has_window_close &&
1876 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
1877 error_report("-no-quit is only valid for GTK and SDL, "
1878 "ignoring option");
1881 qemu_display_early_init(&dpy);
1882 qemu_console_early_init();
1884 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1885 #if defined(CONFIG_OPENGL)
1886 error_report("OpenGL is not supported by the display");
1887 #else
1888 error_report("OpenGL support is disabled");
1889 #endif
1890 exit(1);
1893 object_option_foreach_add(object_create_early);
1895 /* spice needs the timers to be initialized by this point */
1896 /* spice must initialize before audio as it changes the default auiodev */
1897 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1898 qemu_spice.init();
1900 qemu_opts_foreach(qemu_find_opts("chardev"),
1901 chardev_init_func, NULL, &error_fatal);
1903 #ifdef CONFIG_VIRTFS
1904 qemu_opts_foreach(qemu_find_opts("fsdev"),
1905 fsdev_init_func, NULL, &error_fatal);
1906 #endif
1909 * Note: we need to create audio and block backends before
1910 * machine_set_property(), so machine properties can refer to
1911 * them.
1913 configure_blockdev(&bdo_queue, machine_class, snapshot);
1914 audio_init_audiodevs();
1919 * The remainder of object creation happens after the
1920 * creation of chardev, fsdev, net clients and device data types.
1922 static bool object_create_late(const char *type)
1924 return !object_create_early(type);
1927 static void qemu_create_late_backends(void)
1929 if (qtest_chrdev) {
1930 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1933 net_init_clients(&error_fatal);
1935 object_option_foreach_add(object_create_late);
1937 if (tpm_init() < 0) {
1938 exit(1);
1941 qemu_opts_foreach(qemu_find_opts("mon"),
1942 mon_init_func, NULL, &error_fatal);
1944 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1945 exit(1);
1946 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1947 exit(1);
1948 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1949 exit(1);
1951 /* now chardevs have been created we may have semihosting to connect */
1952 qemu_semihosting_connect_chardevs();
1953 qemu_semihosting_console_init();
1956 static bool have_custom_ram_size(void)
1958 QemuOpts *opts = qemu_find_opts_singleton("memory");
1959 return !!qemu_opt_get_size(opts, "size", 0);
1962 static void qemu_resolve_machine_memdev(void)
1964 if (current_machine->ram_memdev_id) {
1965 Object *backend;
1966 ram_addr_t backend_size;
1968 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1969 TYPE_MEMORY_BACKEND, NULL);
1970 if (!backend) {
1971 error_report("Memory backend '%s' not found",
1972 current_machine->ram_memdev_id);
1973 exit(EXIT_FAILURE);
1975 backend_size = object_property_get_uint(backend, "size", &error_abort);
1976 if (have_custom_ram_size() && backend_size != ram_size) {
1977 error_report("Size specified by -m option must match size of "
1978 "explicitly specified 'memory-backend' property");
1979 exit(EXIT_FAILURE);
1981 if (mem_path) {
1982 error_report("'-mem-path' can't be used together with"
1983 "'-machine memory-backend'");
1984 exit(EXIT_FAILURE);
1986 ram_size = backend_size;
1989 if (!xen_enabled()) {
1990 /* On 32-bit hosts, QEMU is limited by virtual address space */
1991 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1992 error_report("at most 2047 MB RAM can be simulated");
1993 exit(1);
1998 static void set_memory_options(MachineClass *mc)
2000 uint64_t sz;
2001 const char *mem_str;
2002 const ram_addr_t default_ram_size = mc->default_ram_size;
2003 QemuOpts *opts = qemu_find_opts_singleton("memory");
2004 Location loc;
2006 loc_push_none(&loc);
2007 qemu_opts_loc_restore(opts);
2009 sz = 0;
2010 mem_str = qemu_opt_get(opts, "size");
2011 if (mem_str) {
2012 if (!*mem_str) {
2013 error_report("missing 'size' option value");
2014 exit(EXIT_FAILURE);
2017 sz = qemu_opt_get_size(opts, "size", ram_size);
2019 /* Fix up legacy suffix-less format */
2020 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2021 uint64_t overflow_check = sz;
2023 sz *= MiB;
2024 if (sz / MiB != overflow_check) {
2025 error_report("too large 'size' option value");
2026 exit(EXIT_FAILURE);
2031 /* backward compatibility behaviour for case "-m 0" */
2032 if (sz == 0) {
2033 sz = default_ram_size;
2036 sz = QEMU_ALIGN_UP(sz, 8192);
2037 if (mc->fixup_ram_size) {
2038 sz = mc->fixup_ram_size(sz);
2040 ram_size = sz;
2041 if (ram_size != sz) {
2042 error_report("ram size too large");
2043 exit(EXIT_FAILURE);
2046 maxram_size = ram_size;
2048 if (qemu_opt_get(opts, "maxmem")) {
2049 uint64_t slots;
2051 sz = qemu_opt_get_size(opts, "maxmem", 0);
2052 slots = qemu_opt_get_number(opts, "slots", 0);
2053 if (sz < ram_size) {
2054 error_report("invalid value of -m option maxmem: "
2055 "maximum memory size (0x%" PRIx64 ") must be at least "
2056 "the initial memory size (0x" RAM_ADDR_FMT ")",
2057 sz, ram_size);
2058 exit(EXIT_FAILURE);
2059 } else if (slots && sz == ram_size) {
2060 error_report("invalid value of -m option maxmem: "
2061 "memory slots were specified but maximum memory size "
2062 "(0x%" PRIx64 ") is equal to the initial memory size "
2063 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2064 exit(EXIT_FAILURE);
2067 maxram_size = sz;
2068 ram_slots = slots;
2069 } else if (qemu_opt_get(opts, "slots")) {
2070 error_report("invalid -m option value: missing 'maxmem' option");
2071 exit(EXIT_FAILURE);
2074 loc_pop(&loc);
2077 static void qemu_create_machine(MachineClass *machine_class)
2079 object_set_machine_compat_props(machine_class->compat_props);
2081 set_memory_options(machine_class);
2083 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2084 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
2085 exit(0);
2087 object_property_add_child(object_get_root(), "machine",
2088 OBJECT(current_machine));
2089 object_property_add_child(container_get(OBJECT(current_machine),
2090 "/unattached"),
2091 "sysbus", OBJECT(sysbus_get_default()));
2093 if (machine_class->minimum_page_bits) {
2094 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2095 /* This would be a board error: specifying a minimum smaller than
2096 * a target's compile-time fixed setting.
2098 g_assert_not_reached();
2102 cpu_exec_init_all();
2103 page_size_init();
2105 if (machine_class->hw_version) {
2106 qemu_set_hw_version(machine_class->hw_version);
2109 machine_smp_parse(current_machine,
2110 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
2113 * Get the default machine options from the machine if it is not already
2114 * specified either by the configuration file or by the command line.
2116 if (machine_class->default_machine_opts) {
2117 qemu_opts_set_defaults(qemu_find_opts("machine"),
2118 machine_class->default_machine_opts, 0);
2122 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2124 GlobalProperty *g;
2126 g = g_malloc0(sizeof(*g));
2127 g->driver = qemu_opt_get(opts, "driver");
2128 g->property = qemu_opt_get(opts, "property");
2129 g->value = qemu_opt_get(opts, "value");
2130 qdev_prop_register_global(g);
2131 return 0;
2135 * Return whether configuration group @group is stored in QemuOpts, or
2136 * recorded as one or more QDicts by qemu_record_config_group.
2138 static bool is_qemuopts_group(const char *group)
2140 if (g_str_equal(group, "object")) {
2141 return false;
2143 return true;
2146 static void qemu_record_config_group(const char *group, QDict *dict,
2147 bool from_json, Error **errp)
2149 if (g_str_equal(group, "object")) {
2150 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2151 object_option_add_visitor(v);
2152 visit_free(v);
2153 } else {
2154 abort();
2159 * Parse non-QemuOpts config file groups, pass the rest to
2160 * qemu_config_do_parse.
2162 static void qemu_parse_config_group(const char *group, QDict *qdict,
2163 void *opaque, Error **errp)
2165 QObject *crumpled;
2166 if (is_qemuopts_group(group)) {
2167 qemu_config_do_parse(group, qdict, opaque, errp);
2168 return;
2171 crumpled = qdict_crumple(qdict, errp);
2172 if (!crumpled) {
2173 return;
2175 if (qobject_type(crumpled) != QTYPE_QDICT) {
2176 assert(qobject_type(crumpled) == QTYPE_QLIST);
2177 error_setg(errp, "Lists cannot be at top level of a configuration section");
2178 return;
2180 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2183 static void qemu_read_default_config_file(Error **errp)
2185 ERRP_GUARD();
2186 int ret;
2187 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2189 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2190 if (ret < 0) {
2191 if (ret == -ENOENT) {
2192 error_free(*errp);
2193 *errp = NULL;
2198 static void qemu_set_option(const char *str, Error **errp)
2200 char group[64], id[64], arg[64];
2201 QemuOptsList *list;
2202 QemuOpts *opts;
2203 int rc, offset;
2205 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2206 if (rc < 3 || str[offset] != '=') {
2207 error_setg(errp, "can't parse: \"%s\"", str);
2208 return;
2211 if (!is_qemuopts_group(group)) {
2212 error_setg(errp, "-set is not supported with %s", group);
2213 } else {
2214 list = qemu_find_opts_err(group, errp);
2215 if (list) {
2216 opts = qemu_opts_find(list, id);
2217 if (!opts) {
2218 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2219 return;
2221 qemu_opt_set(opts, arg, str + offset + 1, errp);
2226 static void user_register_global_props(void)
2228 qemu_opts_foreach(qemu_find_opts("global"),
2229 global_init_func, NULL, NULL);
2232 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2234 icount_configure(opts, errp);
2235 return 0;
2238 static int accelerator_set_property(void *opaque,
2239 const char *name, const char *value,
2240 Error **errp)
2242 return object_parse_property_opt(opaque, name, value, "accel", errp);
2245 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2247 bool *p_init_failed = opaque;
2248 const char *acc = qemu_opt_get(opts, "accel");
2249 AccelClass *ac = accel_find(acc);
2250 AccelState *accel;
2251 int ret;
2252 bool qtest_with_kvm;
2254 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2256 if (!ac) {
2257 *p_init_failed = true;
2258 if (!qtest_with_kvm) {
2259 error_report("invalid accelerator %s", acc);
2261 return 0;
2263 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2264 object_apply_compat_props(OBJECT(accel));
2265 qemu_opt_foreach(opts, accelerator_set_property,
2266 accel,
2267 &error_fatal);
2269 ret = accel_init_machine(accel, current_machine);
2270 if (ret < 0) {
2271 *p_init_failed = true;
2272 if (!qtest_with_kvm || ret != -ENOENT) {
2273 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2275 return 0;
2278 return 1;
2281 static void configure_accelerators(const char *progname)
2283 const char *accelerators;
2284 bool init_failed = false;
2286 qemu_opts_foreach(qemu_find_opts("icount"),
2287 do_configure_icount, NULL, &error_fatal);
2289 accelerators = qemu_opt_get(qemu_get_machine_opts(), "accel");
2290 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2291 char **accel_list, **tmp;
2293 if (accelerators == NULL) {
2294 /* Select the default accelerator */
2295 bool have_tcg = accel_find("tcg");
2296 bool have_kvm = accel_find("kvm");
2298 if (have_tcg && have_kvm) {
2299 if (g_str_has_suffix(progname, "kvm")) {
2300 /* If the program name ends with "kvm", we prefer KVM */
2301 accelerators = "kvm:tcg";
2302 } else {
2303 accelerators = "tcg:kvm";
2305 } else if (have_kvm) {
2306 accelerators = "kvm";
2307 } else if (have_tcg) {
2308 accelerators = "tcg";
2309 } else {
2310 error_report("No accelerator selected and"
2311 " no default accelerator available");
2312 exit(1);
2315 accel_list = g_strsplit(accelerators, ":", 0);
2317 for (tmp = accel_list; *tmp; tmp++) {
2319 * Filter invalid accelerators here, to prevent obscenities
2320 * such as "-machine accel=tcg,,thread=single".
2322 if (accel_find(*tmp)) {
2323 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2324 } else {
2325 init_failed = true;
2326 error_report("invalid accelerator %s", *tmp);
2329 g_strfreev(accel_list);
2330 } else {
2331 if (accelerators != NULL) {
2332 error_report("The -accel and \"-machine accel=\" options are incompatible");
2333 exit(1);
2337 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2338 do_configure_accelerator, &init_failed, &error_fatal)) {
2339 if (!init_failed) {
2340 error_report("no accelerator found");
2342 exit(1);
2345 if (init_failed && !qtest_chrdev) {
2346 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2347 error_report("falling back to %s", ac->name);
2350 if (icount_enabled() && !tcg_enabled()) {
2351 error_report("-icount is not allowed with hardware virtualization");
2352 exit(1);
2356 static void create_default_memdev(MachineState *ms, const char *path)
2358 Object *obj;
2359 MachineClass *mc = MACHINE_GET_CLASS(ms);
2361 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2362 if (path) {
2363 object_property_set_str(obj, "mem-path", path, &error_fatal);
2365 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2366 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2367 obj);
2368 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2369 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2370 false, &error_fatal);
2371 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2372 object_unref(obj);
2373 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2374 &error_fatal);
2377 static void qemu_validate_options(void)
2379 QemuOpts *machine_opts = qemu_get_machine_opts();
2380 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2381 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2382 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2384 if (kernel_filename == NULL) {
2385 if (kernel_cmdline != NULL) {
2386 error_report("-append only allowed with -kernel option");
2387 exit(1);
2390 if (initrd_filename != NULL) {
2391 error_report("-initrd only allowed with -kernel option");
2392 exit(1);
2396 if (loadvm && preconfig_requested) {
2397 error_report("'preconfig' and 'loadvm' options are "
2398 "mutually exclusive");
2399 exit(EXIT_FAILURE);
2401 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2402 error_report("'preconfig' supports '-incoming defer' only");
2403 exit(EXIT_FAILURE);
2406 #ifdef CONFIG_CURSES
2407 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2408 error_report("curses display cannot be used with -daemonize");
2409 exit(1);
2411 #endif
2414 static void qemu_process_sugar_options(void)
2416 if (mem_prealloc) {
2417 char *val;
2419 val = g_strdup_printf("%d",
2420 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2421 object_register_sugar_prop("memory-backend", "prealloc-threads", val,
2422 false);
2423 g_free(val);
2424 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2427 if (watchdog) {
2428 int i = select_watchdog(watchdog);
2429 if (i > 0)
2430 exit (i == 1 ? 1 : 0);
2434 /* -action processing */
2437 * Process all the -action parameters parsed from cmdline.
2439 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2441 Error *local_err = NULL;
2442 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2443 QObject *ret = NULL;
2444 qmp_marshal_set_action(qdict, &ret, &local_err);
2445 qobject_unref(ret);
2446 qobject_unref(qdict);
2447 if (local_err) {
2448 error_propagate(errp, local_err);
2449 return 1;
2451 return 0;
2454 static void qemu_process_early_options(void)
2456 #ifdef CONFIG_SECCOMP
2457 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2458 if (olist) {
2459 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2461 #endif
2463 qemu_opts_foreach(qemu_find_opts("name"),
2464 parse_name, NULL, &error_fatal);
2466 if (qemu_opts_foreach(qemu_find_opts("action"),
2467 process_runstate_actions, NULL, &error_fatal)) {
2468 exit(1);
2471 #ifndef _WIN32
2472 qemu_opts_foreach(qemu_find_opts("add-fd"),
2473 parse_add_fd, NULL, &error_fatal);
2475 qemu_opts_foreach(qemu_find_opts("add-fd"),
2476 cleanup_add_fd, NULL, &error_fatal);
2477 #endif
2479 /* Open the logfile at this point and set the log mask if necessary. */
2480 if (log_file) {
2481 qemu_set_log_filename(log_file, &error_fatal);
2483 if (log_mask) {
2484 int mask;
2485 mask = qemu_str_to_log_mask(log_mask);
2486 if (!mask) {
2487 qemu_print_log_usage(stdout);
2488 exit(1);
2490 qemu_set_log(mask);
2491 } else {
2492 qemu_set_log(0);
2495 qemu_add_default_firmwarepath();
2498 static void qemu_process_help_options(void)
2501 * Check for -cpu help and -device help before we call select_machine(),
2502 * which will return an error if the architecture has no default machine
2503 * type and the user did not specify one, so that the user doesn't need
2504 * to say '-cpu help -machine something'.
2506 if (cpu_option && is_help_option(cpu_option)) {
2507 list_cpus(cpu_option);
2508 exit(0);
2511 if (qemu_opts_foreach(qemu_find_opts("device"),
2512 device_help_func, NULL, NULL)) {
2513 exit(0);
2516 /* -L help lists the data directories and exits. */
2517 if (list_data_dirs) {
2518 qemu_list_data_dirs();
2519 exit(0);
2523 static void qemu_maybe_daemonize(const char *pid_file)
2525 Error *err = NULL;
2527 os_daemonize();
2528 rcu_disable_atfork();
2530 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2531 error_reportf_err(err, "cannot create PID file: ");
2532 exit(1);
2535 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2536 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2539 static void qemu_init_displays(void)
2541 DisplayState *ds;
2543 /* init local displays */
2544 ds = init_displaystate();
2545 qemu_display_init(ds, &dpy);
2547 /* must be after terminal init, SDL library changes signal handlers */
2548 os_setup_signal_handling();
2550 /* init remote displays */
2551 #ifdef CONFIG_VNC
2552 qemu_opts_foreach(qemu_find_opts("vnc"),
2553 vnc_init_func, NULL, &error_fatal);
2554 #endif
2556 if (using_spice) {
2557 qemu_spice.display_init();
2561 static void qemu_init_board(void)
2563 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2565 if (machine_class->default_ram_id && current_machine->ram_size &&
2566 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2567 create_default_memdev(current_machine, mem_path);
2570 /* process plugin before CPUs are created, but once -smp has been parsed */
2571 qemu_plugin_load_list(&plugin_list, &error_fatal);
2573 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2574 machine_run_board_init(current_machine);
2576 drive_check_orphaned();
2578 realtime_init();
2580 if (hax_enabled()) {
2581 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2582 hax_sync_vcpus();
2586 static void qemu_create_cli_devices(void)
2588 soundhw_init();
2590 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2591 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2593 /* init USB devices */
2594 if (machine_usb(current_machine)) {
2595 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2596 exit(1);
2599 /* init generic devices */
2600 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2601 qemu_opts_foreach(qemu_find_opts("device"),
2602 device_init_func, NULL, &error_fatal);
2603 rom_reset_order_override();
2606 static void qemu_machine_creation_done(void)
2608 MachineState *machine = MACHINE(qdev_get_machine());
2610 /* Did we create any drives that we failed to create a device for? */
2611 drive_check_orphaned();
2613 /* Don't warn about the default network setup that you get if
2614 * no command line -net or -netdev options are specified. There
2615 * are two cases that we would otherwise complain about:
2616 * (1) board doesn't support a NIC but the implicit "-net nic"
2617 * requested one
2618 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2619 * sets up a nic that isn't connected to anything.
2621 if (!default_net && (!qtest_enabled() || has_defaults)) {
2622 net_check_clients();
2625 qdev_prop_check_globals();
2627 qdev_machine_creation_done();
2629 if (machine->cgs) {
2631 * Verify that Confidential Guest Support has actually been initialized
2633 assert(machine->cgs->ready);
2636 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2637 exit(1);
2641 void qmp_x_exit_preconfig(Error **errp)
2643 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2644 error_setg(errp, "The command is permitted only before machine initialization");
2645 return;
2648 qemu_init_board();
2649 qemu_create_cli_devices();
2650 qemu_machine_creation_done();
2652 if (loadvm) {
2653 Error *local_err = NULL;
2654 if (!load_snapshot(loadvm, NULL, false, NULL, &local_err)) {
2655 error_report_err(local_err);
2656 autostart = 0;
2657 exit(1);
2660 if (replay_mode != REPLAY_MODE_NONE) {
2661 replay_vmstate_init();
2664 if (incoming) {
2665 Error *local_err = NULL;
2666 if (strcmp(incoming, "defer") != 0) {
2667 qmp_migrate_incoming(incoming, &local_err);
2668 if (local_err) {
2669 error_reportf_err(local_err, "-incoming %s: ", incoming);
2670 exit(1);
2673 } else if (autostart) {
2674 qmp_cont(NULL);
2678 #ifdef CONFIG_MODULES
2679 void qemu_load_module_for_opts(const char *group)
2681 static bool spice_tried;
2682 if (g_str_equal(group, "spice") && !spice_tried) {
2683 ui_module_load_one("spice-core");
2684 spice_tried = true;
2687 static bool iscsi_tried;
2688 if (g_str_equal(group, "iscsi") && !iscsi_tried) {
2689 block_module_load_one("iscsi");
2690 iscsi_tried = true;
2693 #endif
2695 void qemu_init(int argc, char **argv, char **envp)
2697 QemuOpts *opts;
2698 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2699 QemuOptsList *olist;
2700 int optind;
2701 const char *optarg;
2702 MachineClass *machine_class;
2703 bool userconfig = true;
2704 FILE *vmstate_dump_file = NULL;
2706 qemu_add_opts(&qemu_drive_opts);
2707 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2708 qemu_add_drive_opts(&qemu_common_drive_opts);
2709 qemu_add_drive_opts(&qemu_drive_opts);
2710 qemu_add_drive_opts(&bdrv_runtime_opts);
2711 qemu_add_opts(&qemu_chardev_opts);
2712 qemu_add_opts(&qemu_device_opts);
2713 qemu_add_opts(&qemu_netdev_opts);
2714 qemu_add_opts(&qemu_nic_opts);
2715 qemu_add_opts(&qemu_net_opts);
2716 qemu_add_opts(&qemu_rtc_opts);
2717 qemu_add_opts(&qemu_global_opts);
2718 qemu_add_opts(&qemu_mon_opts);
2719 qemu_add_opts(&qemu_trace_opts);
2720 qemu_plugin_add_opts();
2721 qemu_add_opts(&qemu_option_rom_opts);
2722 qemu_add_opts(&qemu_machine_opts);
2723 qemu_add_opts(&qemu_accel_opts);
2724 qemu_add_opts(&qemu_mem_opts);
2725 qemu_add_opts(&qemu_smp_opts);
2726 qemu_add_opts(&qemu_boot_opts);
2727 qemu_add_opts(&qemu_add_fd_opts);
2728 qemu_add_opts(&qemu_object_opts);
2729 qemu_add_opts(&qemu_tpmdev_opts);
2730 qemu_add_opts(&qemu_overcommit_opts);
2731 qemu_add_opts(&qemu_msg_opts);
2732 qemu_add_opts(&qemu_name_opts);
2733 qemu_add_opts(&qemu_numa_opts);
2734 qemu_add_opts(&qemu_icount_opts);
2735 qemu_add_opts(&qemu_semihosting_config_opts);
2736 qemu_add_opts(&qemu_fw_cfg_opts);
2737 qemu_add_opts(&qemu_action_opts);
2738 module_call_init(MODULE_INIT_OPTS);
2740 error_init(argv[0]);
2741 qemu_init_exec_dir(argv[0]);
2743 qemu_init_subsystems();
2745 /* first pass of option parsing */
2746 optind = 1;
2747 while (optind < argc) {
2748 if (argv[optind][0] != '-') {
2749 /* disk image */
2750 optind++;
2751 } else {
2752 const QEMUOption *popt;
2754 popt = lookup_opt(argc, argv, &optarg, &optind);
2755 switch (popt->index) {
2756 case QEMU_OPTION_nouserconfig:
2757 userconfig = false;
2758 break;
2763 if (userconfig) {
2764 qemu_read_default_config_file(&error_fatal);
2767 /* second pass of option parsing */
2768 optind = 1;
2769 for(;;) {
2770 if (optind >= argc)
2771 break;
2772 if (argv[optind][0] != '-') {
2773 loc_set_cmdline(argv, optind, 1);
2774 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2775 } else {
2776 const QEMUOption *popt;
2778 popt = lookup_opt(argc, argv, &optarg, &optind);
2779 if (!(popt->arch_mask & arch_type)) {
2780 error_report("Option not supported for this target");
2781 exit(1);
2783 switch(popt->index) {
2784 case QEMU_OPTION_cpu:
2785 /* hw initialization will check this */
2786 cpu_option = optarg;
2787 break;
2788 case QEMU_OPTION_hda:
2789 case QEMU_OPTION_hdb:
2790 case QEMU_OPTION_hdc:
2791 case QEMU_OPTION_hdd:
2792 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2793 HD_OPTS);
2794 break;
2795 case QEMU_OPTION_blockdev:
2797 Visitor *v;
2798 BlockdevOptionsQueueEntry *bdo;
2800 v = qobject_input_visitor_new_str(optarg, "driver",
2801 &error_fatal);
2803 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2804 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2805 &error_fatal);
2806 visit_free(v);
2807 loc_save(&bdo->loc);
2808 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2809 break;
2811 case QEMU_OPTION_drive:
2812 if (drive_def(optarg) == NULL) {
2813 exit(1);
2815 break;
2816 case QEMU_OPTION_set:
2817 qemu_set_option(optarg, &error_fatal);
2818 break;
2819 case QEMU_OPTION_global:
2820 if (qemu_global_option(optarg) != 0)
2821 exit(1);
2822 break;
2823 case QEMU_OPTION_mtdblock:
2824 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2825 break;
2826 case QEMU_OPTION_sd:
2827 drive_add(IF_SD, -1, optarg, SD_OPTS);
2828 break;
2829 case QEMU_OPTION_pflash:
2830 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2831 break;
2832 case QEMU_OPTION_snapshot:
2834 Error *blocker = NULL;
2835 snapshot = 1;
2836 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2837 "-snapshot");
2838 replay_add_blocker(blocker);
2840 break;
2841 case QEMU_OPTION_numa:
2842 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2843 optarg, true);
2844 if (!opts) {
2845 exit(1);
2847 break;
2848 case QEMU_OPTION_display:
2849 parse_display(optarg);
2850 break;
2851 case QEMU_OPTION_nographic:
2852 olist = qemu_find_opts("machine");
2853 qemu_opts_parse_noisily(olist, "graphics=off", false);
2854 nographic = true;
2855 dpy.type = DISPLAY_TYPE_NONE;
2856 break;
2857 case QEMU_OPTION_curses:
2858 #ifdef CONFIG_CURSES
2859 dpy.type = DISPLAY_TYPE_CURSES;
2860 #else
2861 error_report("curses or iconv support is disabled");
2862 exit(1);
2863 #endif
2864 break;
2865 case QEMU_OPTION_portrait:
2866 graphic_rotate = 90;
2867 break;
2868 case QEMU_OPTION_rotate:
2869 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2870 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2871 graphic_rotate != 180 && graphic_rotate != 270) {
2872 error_report("only 90, 180, 270 deg rotation is available");
2873 exit(1);
2875 break;
2876 case QEMU_OPTION_kernel:
2877 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
2878 break;
2879 case QEMU_OPTION_initrd:
2880 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
2881 break;
2882 case QEMU_OPTION_append:
2883 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
2884 break;
2885 case QEMU_OPTION_dtb:
2886 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
2887 break;
2888 case QEMU_OPTION_cdrom:
2889 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2890 break;
2891 case QEMU_OPTION_boot:
2892 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2893 optarg, true);
2894 if (!opts) {
2895 exit(1);
2897 break;
2898 case QEMU_OPTION_fda:
2899 case QEMU_OPTION_fdb:
2900 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2901 optarg, FD_OPTS);
2902 break;
2903 case QEMU_OPTION_no_fd_bootchk:
2904 fd_bootchk = 0;
2905 break;
2906 case QEMU_OPTION_netdev:
2907 default_net = 0;
2908 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2909 exit(1);
2911 break;
2912 case QEMU_OPTION_nic:
2913 default_net = 0;
2914 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2915 exit(1);
2917 break;
2918 case QEMU_OPTION_net:
2919 default_net = 0;
2920 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2921 exit(1);
2923 break;
2924 #ifdef CONFIG_LIBISCSI
2925 case QEMU_OPTION_iscsi:
2926 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2927 optarg, false);
2928 if (!opts) {
2929 exit(1);
2931 break;
2932 #endif
2933 case QEMU_OPTION_audio_help:
2934 audio_legacy_help();
2935 exit (0);
2936 break;
2937 case QEMU_OPTION_audiodev:
2938 audio_parse_option(optarg);
2939 break;
2940 case QEMU_OPTION_soundhw:
2941 select_soundhw (optarg);
2942 break;
2943 case QEMU_OPTION_h:
2944 help(0);
2945 break;
2946 case QEMU_OPTION_version:
2947 version();
2948 exit(0);
2949 break;
2950 case QEMU_OPTION_m:
2951 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2952 optarg, true);
2953 if (!opts) {
2954 exit(EXIT_FAILURE);
2956 break;
2957 #ifdef CONFIG_TPM
2958 case QEMU_OPTION_tpmdev:
2959 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2960 exit(1);
2962 break;
2963 #endif
2964 case QEMU_OPTION_mempath:
2965 mem_path = optarg;
2966 break;
2967 case QEMU_OPTION_mem_prealloc:
2968 mem_prealloc = 1;
2969 break;
2970 case QEMU_OPTION_d:
2971 log_mask = optarg;
2972 break;
2973 case QEMU_OPTION_D:
2974 log_file = optarg;
2975 break;
2976 case QEMU_OPTION_DFILTER:
2977 qemu_set_dfilter_ranges(optarg, &error_fatal);
2978 break;
2979 case QEMU_OPTION_seed:
2980 qemu_guest_random_seed_main(optarg, &error_fatal);
2981 break;
2982 case QEMU_OPTION_s:
2983 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2984 break;
2985 case QEMU_OPTION_gdb:
2986 add_device_config(DEV_GDB, optarg);
2987 break;
2988 case QEMU_OPTION_L:
2989 if (is_help_option(optarg)) {
2990 list_data_dirs = true;
2991 } else {
2992 qemu_add_data_dir(g_strdup(optarg));
2994 break;
2995 case QEMU_OPTION_bios:
2996 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
2997 break;
2998 case QEMU_OPTION_singlestep:
2999 singlestep = 1;
3000 break;
3001 case QEMU_OPTION_S:
3002 autostart = 0;
3003 break;
3004 case QEMU_OPTION_k:
3005 keyboard_layout = optarg;
3006 break;
3007 case QEMU_OPTION_vga:
3008 vga_model = optarg;
3009 default_vga = 0;
3010 break;
3011 case QEMU_OPTION_g:
3013 const char *p;
3014 int w, h, depth;
3015 p = optarg;
3016 w = strtol(p, (char **)&p, 10);
3017 if (w <= 0) {
3018 graphic_error:
3019 error_report("invalid resolution or depth");
3020 exit(1);
3022 if (*p != 'x')
3023 goto graphic_error;
3024 p++;
3025 h = strtol(p, (char **)&p, 10);
3026 if (h <= 0)
3027 goto graphic_error;
3028 if (*p == 'x') {
3029 p++;
3030 depth = strtol(p, (char **)&p, 10);
3031 if (depth != 1 && depth != 2 && depth != 4 &&
3032 depth != 8 && depth != 15 && depth != 16 &&
3033 depth != 24 && depth != 32)
3034 goto graphic_error;
3035 } else if (*p == '\0') {
3036 depth = graphic_depth;
3037 } else {
3038 goto graphic_error;
3041 graphic_width = w;
3042 graphic_height = h;
3043 graphic_depth = depth;
3045 break;
3046 case QEMU_OPTION_echr:
3048 char *r;
3049 term_escape_char = strtol(optarg, &r, 0);
3050 if (r == optarg)
3051 printf("Bad argument to echr\n");
3052 break;
3054 case QEMU_OPTION_monitor:
3055 default_monitor = 0;
3056 if (strncmp(optarg, "none", 4)) {
3057 monitor_parse(optarg, "readline", false);
3059 break;
3060 case QEMU_OPTION_qmp:
3061 monitor_parse(optarg, "control", false);
3062 default_monitor = 0;
3063 break;
3064 case QEMU_OPTION_qmp_pretty:
3065 monitor_parse(optarg, "control", true);
3066 default_monitor = 0;
3067 break;
3068 case QEMU_OPTION_mon:
3069 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3070 true);
3071 if (!opts) {
3072 exit(1);
3074 default_monitor = 0;
3075 break;
3076 case QEMU_OPTION_chardev:
3077 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3078 optarg, true);
3079 if (!opts) {
3080 exit(1);
3082 break;
3083 case QEMU_OPTION_fsdev:
3084 olist = qemu_find_opts("fsdev");
3085 if (!olist) {
3086 error_report("fsdev support is disabled");
3087 exit(1);
3089 opts = qemu_opts_parse_noisily(olist, optarg, true);
3090 if (!opts) {
3091 exit(1);
3093 break;
3094 case QEMU_OPTION_virtfs: {
3095 QemuOpts *fsdev;
3096 QemuOpts *device;
3097 const char *writeout, *sock_fd, *socket, *path, *security_model,
3098 *multidevs;
3100 olist = qemu_find_opts("virtfs");
3101 if (!olist) {
3102 error_report("virtfs support is disabled");
3103 exit(1);
3105 opts = qemu_opts_parse_noisily(olist, optarg, true);
3106 if (!opts) {
3107 exit(1);
3110 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3111 qemu_opt_get(opts, "mount_tag") == NULL) {
3112 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3113 exit(1);
3115 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3116 qemu_opts_id(opts) ?:
3117 qemu_opt_get(opts, "mount_tag"),
3118 1, NULL);
3119 if (!fsdev) {
3120 error_report("duplicate or invalid fsdev id: %s",
3121 qemu_opt_get(opts, "mount_tag"));
3122 exit(1);
3125 writeout = qemu_opt_get(opts, "writeout");
3126 if (writeout) {
3127 #ifdef CONFIG_SYNC_FILE_RANGE
3128 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3129 #else
3130 error_report("writeout=immediate not supported "
3131 "on this platform");
3132 exit(1);
3133 #endif
3135 qemu_opt_set(fsdev, "fsdriver",
3136 qemu_opt_get(opts, "fsdriver"), &error_abort);
3137 path = qemu_opt_get(opts, "path");
3138 if (path) {
3139 qemu_opt_set(fsdev, "path", path, &error_abort);
3141 security_model = qemu_opt_get(opts, "security_model");
3142 if (security_model) {
3143 qemu_opt_set(fsdev, "security_model", security_model,
3144 &error_abort);
3146 socket = qemu_opt_get(opts, "socket");
3147 if (socket) {
3148 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3150 sock_fd = qemu_opt_get(opts, "sock_fd");
3151 if (sock_fd) {
3152 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3155 qemu_opt_set_bool(fsdev, "readonly",
3156 qemu_opt_get_bool(opts, "readonly", 0),
3157 &error_abort);
3158 multidevs = qemu_opt_get(opts, "multidevs");
3159 if (multidevs) {
3160 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3162 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3163 &error_abort);
3164 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3165 qemu_opt_set(device, "fsdev",
3166 qemu_opts_id(fsdev), &error_abort);
3167 qemu_opt_set(device, "mount_tag",
3168 qemu_opt_get(opts, "mount_tag"), &error_abort);
3169 break;
3171 case QEMU_OPTION_serial:
3172 add_device_config(DEV_SERIAL, optarg);
3173 default_serial = 0;
3174 if (strncmp(optarg, "mon:", 4) == 0) {
3175 default_monitor = 0;
3177 break;
3178 case QEMU_OPTION_watchdog:
3179 if (watchdog) {
3180 error_report("only one watchdog option may be given");
3181 exit(1);
3183 watchdog = optarg;
3184 break;
3185 case QEMU_OPTION_action:
3186 olist = qemu_find_opts("action");
3187 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3188 exit(1);
3190 break;
3191 case QEMU_OPTION_watchdog_action:
3192 if (select_watchdog_action(optarg) == -1) {
3193 error_report("unknown -watchdog-action parameter");
3194 exit(1);
3196 break;
3197 case QEMU_OPTION_parallel:
3198 add_device_config(DEV_PARALLEL, optarg);
3199 default_parallel = 0;
3200 if (strncmp(optarg, "mon:", 4) == 0) {
3201 default_monitor = 0;
3203 break;
3204 case QEMU_OPTION_debugcon:
3205 add_device_config(DEV_DEBUGCON, optarg);
3206 break;
3207 case QEMU_OPTION_loadvm:
3208 loadvm = optarg;
3209 break;
3210 case QEMU_OPTION_full_screen:
3211 dpy.has_full_screen = true;
3212 dpy.full_screen = true;
3213 break;
3214 case QEMU_OPTION_alt_grab:
3215 alt_grab = 1;
3216 break;
3217 case QEMU_OPTION_ctrl_grab:
3218 ctrl_grab = 1;
3219 break;
3220 case QEMU_OPTION_no_quit:
3221 dpy.has_window_close = true;
3222 dpy.window_close = false;
3223 break;
3224 case QEMU_OPTION_sdl:
3225 #ifdef CONFIG_SDL
3226 dpy.type = DISPLAY_TYPE_SDL;
3227 break;
3228 #else
3229 error_report("SDL support is disabled");
3230 exit(1);
3231 #endif
3232 case QEMU_OPTION_pidfile:
3233 pid_file = optarg;
3234 break;
3235 case QEMU_OPTION_win2k_hack:
3236 win2k_install_hack = 1;
3237 break;
3238 case QEMU_OPTION_acpitable:
3239 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3240 optarg, true);
3241 if (!opts) {
3242 exit(1);
3244 acpi_table_add(opts, &error_fatal);
3245 break;
3246 case QEMU_OPTION_smbios:
3247 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3248 optarg, false);
3249 if (!opts) {
3250 exit(1);
3252 smbios_entry_add(opts, &error_fatal);
3253 break;
3254 case QEMU_OPTION_fwcfg:
3255 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3256 optarg, true);
3257 if (opts == NULL) {
3258 exit(1);
3260 break;
3261 case QEMU_OPTION_preconfig:
3262 preconfig_requested = true;
3263 break;
3264 case QEMU_OPTION_enable_kvm:
3265 olist = qemu_find_opts("machine");
3266 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3267 break;
3268 case QEMU_OPTION_M:
3269 case QEMU_OPTION_machine:
3270 olist = qemu_find_opts("machine");
3271 opts = qemu_opts_parse_noisily(olist, optarg, true);
3272 if (!opts) {
3273 exit(1);
3275 break;
3276 case QEMU_OPTION_accel:
3277 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3278 optarg, true);
3279 optarg = qemu_opt_get(accel_opts, "accel");
3280 if (!optarg || is_help_option(optarg)) {
3281 printf("Accelerators supported in QEMU binary:\n");
3282 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3283 false);
3284 for (el = accel_list; el; el = el->next) {
3285 gchar *typename = g_strdup(object_class_get_name(
3286 OBJECT_CLASS(el->data)));
3287 /* omit qtest which is used for tests only */
3288 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3289 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3290 gchar **optname = g_strsplit(typename,
3291 ACCEL_CLASS_SUFFIX, 0);
3292 printf("%s\n", optname[0]);
3293 g_strfreev(optname);
3295 g_free(typename);
3297 g_slist_free(accel_list);
3298 exit(0);
3300 break;
3301 case QEMU_OPTION_usb:
3302 olist = qemu_find_opts("machine");
3303 qemu_opts_parse_noisily(olist, "usb=on", false);
3304 break;
3305 case QEMU_OPTION_usbdevice:
3306 olist = qemu_find_opts("machine");
3307 qemu_opts_parse_noisily(olist, "usb=on", false);
3308 add_device_config(DEV_USB, optarg);
3309 break;
3310 case QEMU_OPTION_device:
3311 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3312 optarg, true)) {
3313 exit(1);
3315 break;
3316 case QEMU_OPTION_smp:
3317 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3318 optarg, true)) {
3319 exit(1);
3321 break;
3322 case QEMU_OPTION_vnc:
3323 vnc_parse(optarg);
3324 break;
3325 case QEMU_OPTION_no_acpi:
3326 olist = qemu_find_opts("machine");
3327 qemu_opts_parse_noisily(olist, "acpi=off", false);
3328 break;
3329 case QEMU_OPTION_no_hpet:
3330 olist = qemu_find_opts("machine");
3331 qemu_opts_parse_noisily(olist, "hpet=off", false);
3332 break;
3333 case QEMU_OPTION_no_reboot:
3334 olist = qemu_find_opts("action");
3335 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3336 break;
3337 case QEMU_OPTION_no_shutdown:
3338 olist = qemu_find_opts("action");
3339 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3340 break;
3341 case QEMU_OPTION_uuid:
3342 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3343 error_report("failed to parse UUID string: wrong format");
3344 exit(1);
3346 qemu_uuid_set = true;
3347 break;
3348 case QEMU_OPTION_option_rom:
3349 if (nb_option_roms >= MAX_OPTION_ROMS) {
3350 error_report("too many option ROMs");
3351 exit(1);
3353 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3354 optarg, true);
3355 if (!opts) {
3356 exit(1);
3358 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3359 option_rom[nb_option_roms].bootindex =
3360 qemu_opt_get_number(opts, "bootindex", -1);
3361 if (!option_rom[nb_option_roms].name) {
3362 error_report("Option ROM file is not specified");
3363 exit(1);
3365 nb_option_roms++;
3366 break;
3367 case QEMU_OPTION_semihosting:
3368 qemu_semihosting_enable();
3369 break;
3370 case QEMU_OPTION_semihosting_config:
3371 if (qemu_semihosting_config_options(optarg) != 0) {
3372 exit(1);
3374 break;
3375 case QEMU_OPTION_name:
3376 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3377 optarg, true);
3378 if (!opts) {
3379 exit(1);
3381 /* Capture guest name if -msg guest-name is used later */
3382 error_guest_name = qemu_opt_get(opts, "guest");
3383 break;
3384 case QEMU_OPTION_prom_env:
3385 if (nb_prom_envs >= MAX_PROM_ENVS) {
3386 error_report("too many prom variables");
3387 exit(1);
3389 prom_envs[nb_prom_envs] = optarg;
3390 nb_prom_envs++;
3391 break;
3392 case QEMU_OPTION_old_param:
3393 old_param = 1;
3394 break;
3395 case QEMU_OPTION_rtc:
3396 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3397 false);
3398 if (!opts) {
3399 exit(1);
3401 break;
3402 case QEMU_OPTION_icount:
3403 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3404 optarg, true);
3405 if (!icount_opts) {
3406 exit(1);
3408 break;
3409 case QEMU_OPTION_incoming:
3410 if (!incoming) {
3411 runstate_set(RUN_STATE_INMIGRATE);
3413 incoming = optarg;
3414 break;
3415 case QEMU_OPTION_only_migratable:
3416 only_migratable = 1;
3417 break;
3418 case QEMU_OPTION_nodefaults:
3419 has_defaults = 0;
3420 break;
3421 case QEMU_OPTION_xen_domid:
3422 if (!(xen_available())) {
3423 error_report("Option not supported for this target");
3424 exit(1);
3426 xen_domid = atoi(optarg);
3427 break;
3428 case QEMU_OPTION_xen_attach:
3429 if (!(xen_available())) {
3430 error_report("Option not supported for this target");
3431 exit(1);
3433 xen_mode = XEN_ATTACH;
3434 break;
3435 case QEMU_OPTION_xen_domid_restrict:
3436 if (!(xen_available())) {
3437 error_report("Option not supported for this target");
3438 exit(1);
3440 xen_domid_restrict = true;
3441 break;
3442 case QEMU_OPTION_trace:
3443 trace_opt_parse(optarg);
3444 break;
3445 case QEMU_OPTION_plugin:
3446 qemu_plugin_opt_parse(optarg, &plugin_list);
3447 break;
3448 case QEMU_OPTION_readconfig:
3449 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3450 break;
3451 case QEMU_OPTION_spice:
3452 olist = qemu_find_opts_err("spice", NULL);
3453 if (!olist) {
3454 error_report("spice support is disabled");
3455 exit(1);
3457 opts = qemu_opts_parse_noisily(olist, optarg, false);
3458 if (!opts) {
3459 exit(1);
3461 display_remote++;
3462 break;
3463 case QEMU_OPTION_writeconfig:
3465 FILE *fp;
3466 warn_report("-writeconfig is deprecated and will go away without a replacement");
3467 if (strcmp(optarg, "-") == 0) {
3468 fp = stdout;
3469 } else {
3470 fp = fopen(optarg, "w");
3471 if (fp == NULL) {
3472 error_report("open %s: %s", optarg,
3473 strerror(errno));
3474 exit(1);
3477 qemu_config_write(fp);
3478 if (fp != stdout) {
3479 fclose(fp);
3481 break;
3483 case QEMU_OPTION_qtest:
3484 qtest_chrdev = optarg;
3485 break;
3486 case QEMU_OPTION_qtest_log:
3487 qtest_log = optarg;
3488 break;
3489 case QEMU_OPTION_sandbox:
3490 olist = qemu_find_opts("sandbox");
3491 if (!olist) {
3492 #ifndef CONFIG_SECCOMP
3493 error_report("-sandbox support is not enabled "
3494 "in this QEMU binary");
3495 #endif
3496 exit(1);
3499 opts = qemu_opts_parse_noisily(olist, optarg, true);
3500 if (!opts) {
3501 exit(1);
3503 break;
3504 case QEMU_OPTION_add_fd:
3505 #ifndef _WIN32
3506 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3507 optarg, false);
3508 if (!opts) {
3509 exit(1);
3511 #else
3512 error_report("File descriptor passing is disabled on this "
3513 "platform");
3514 exit(1);
3515 #endif
3516 break;
3517 case QEMU_OPTION_object:
3518 object_option_parse(optarg);
3519 break;
3520 case QEMU_OPTION_overcommit:
3521 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3522 optarg, false);
3523 if (!opts) {
3524 exit(1);
3526 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3527 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3528 break;
3529 case QEMU_OPTION_compat:
3531 CompatPolicy *opts;
3532 Visitor *v;
3534 v = qobject_input_visitor_new_str(optarg, NULL,
3535 &error_fatal);
3537 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3538 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3540 qapi_free_CompatPolicy(opts);
3541 visit_free(v);
3542 break;
3544 case QEMU_OPTION_msg:
3545 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3546 false);
3547 if (!opts) {
3548 exit(1);
3550 configure_msg(opts);
3551 break;
3552 case QEMU_OPTION_dump_vmstate:
3553 if (vmstate_dump_file) {
3554 error_report("only one '-dump-vmstate' "
3555 "option may be given");
3556 exit(1);
3558 vmstate_dump_file = fopen(optarg, "w");
3559 if (vmstate_dump_file == NULL) {
3560 error_report("open %s: %s", optarg, strerror(errno));
3561 exit(1);
3563 break;
3564 case QEMU_OPTION_enable_sync_profile:
3565 qsp_enable();
3566 break;
3567 case QEMU_OPTION_nouserconfig:
3568 /* Nothing to be parsed here. Especially, do not error out below. */
3569 break;
3570 default:
3571 if (os_parse_cmd_args(popt->index, optarg)) {
3572 error_report("Option not supported in this build");
3573 exit(1);
3579 * Clear error location left behind by the loop.
3580 * Best done right after the loop. Do not insert code here!
3582 loc_set_none();
3584 qemu_validate_options();
3585 qemu_process_sugar_options();
3588 * These options affect everything else and should be processed
3589 * before daemonizing.
3591 qemu_process_early_options();
3593 qemu_process_help_options();
3594 qemu_maybe_daemonize(pid_file);
3597 * The trace backend must be initialized after daemonizing.
3598 * trace_init_backends() will call st_init(), which will create the
3599 * trace thread in the parent, and also register st_flush_trace_buffer()
3600 * in atexit(). This function will force the parent to wait for the
3601 * writeout thread to finish, which will not occur, and the parent
3602 * process will be left in the host.
3604 if (!trace_init_backends()) {
3605 exit(1);
3607 trace_init_file();
3609 qemu_init_main_loop(&error_fatal);
3610 cpu_timers_init();
3612 user_register_global_props();
3613 replay_configure(icount_opts);
3615 configure_rtc(qemu_find_opts_singleton("rtc"));
3617 qemu_create_machine(select_machine());
3619 suspend_mux_open();
3621 qemu_disable_default_devices();
3622 qemu_create_default_devices();
3623 qemu_create_early_backends();
3625 qemu_apply_machine_options();
3626 phase_advance(PHASE_MACHINE_CREATED);
3629 * Note: uses machine properties such as kernel-irqchip, must run
3630 * after machine_set_property().
3632 configure_accelerators(argv[0]);
3633 phase_advance(PHASE_ACCEL_CREATED);
3636 * Beware, QOM objects created before this point miss global and
3637 * compat properties.
3639 * Global properties get set up by qdev_prop_register_global(),
3640 * called from user_register_global_props(), and certain option
3641 * desugaring. Also in CPU feature desugaring (buried in
3642 * parse_cpu_option()), which happens below this point, but may
3643 * only target the CPU type, which can only be created after
3644 * parse_cpu_option() returned the type.
3646 * Machine compat properties: object_set_machine_compat_props().
3647 * Accelerator compat props: object_set_accelerator_compat_props(),
3648 * called from do_configure_accelerator().
3651 machine_class = MACHINE_GET_CLASS(current_machine);
3652 if (!qtest_enabled() && machine_class->deprecation_reason) {
3653 error_report("Machine type '%s' is deprecated: %s",
3654 machine_class->name, machine_class->deprecation_reason);
3658 * Note: creates a QOM object, must run only after global and
3659 * compat properties have been set up.
3661 migration_object_init();
3663 qemu_create_late_backends();
3665 /* parse features once if machine provides default cpu_type */
3666 current_machine->cpu_type = machine_class->default_cpu_type;
3667 if (cpu_option) {
3668 current_machine->cpu_type = parse_cpu_option(cpu_option);
3670 /* NB: for machine none cpu_type could STILL be NULL here! */
3672 qemu_resolve_machine_memdev();
3673 parse_numa_opts(current_machine);
3675 if (vmstate_dump_file) {
3676 /* dump and exit */
3677 dump_vmstate_json_to_file(vmstate_dump_file);
3678 exit(0);
3681 if (!preconfig_requested) {
3682 qmp_x_exit_preconfig(&error_fatal);
3684 qemu_init_displays();
3685 accel_setup_post(current_machine);
3686 os_setup_post();
3687 resume_mux_open();