include: move target page bits declaration to page-vary.h
[qemu/ar7.git] / softmmu / vl.c
blob46aba6a039c41cf1bb91be9c9da9a4e9a476c3ae
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 "exec/page-vary.h"
31 #include "hw/qdev-properties.h"
32 #include "qapi/compat-policy.h"
33 #include "qapi/error.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qstring.h"
36 #include "qapi/qmp/qjson.h"
37 #include "qemu-version.h"
38 #include "qemu/cutils.h"
39 #include "qemu/help_option.h"
40 #include "qemu/hw-version.h"
41 #include "qemu/uuid.h"
42 #include "sysemu/reset.h"
43 #include "sysemu/runstate.h"
44 #include "sysemu/runstate-action.h"
45 #include "sysemu/seccomp.h"
46 #include "sysemu/tcg.h"
47 #include "sysemu/xen.h"
49 #include "qemu/error-report.h"
50 #include "qemu/sockets.h"
51 #include "qemu/accel.h"
52 #include "hw/usb.h"
53 #include "hw/isa/isa.h"
54 #include "hw/scsi/scsi.h"
55 #include "hw/display/vga.h"
56 #include "sysemu/watchdog.h"
57 #include "hw/firmware/smbios.h"
58 #include "hw/acpi/acpi.h"
59 #include "hw/xen/xen.h"
60 #include "hw/loader.h"
61 #include "monitor/qdev.h"
62 #include "net/net.h"
63 #include "net/slirp.h"
64 #include "monitor/monitor.h"
65 #include "ui/console.h"
66 #include "ui/input.h"
67 #include "sysemu/sysemu.h"
68 #include "sysemu/numa.h"
69 #include "sysemu/hostmem.h"
70 #include "exec/gdbstub.h"
71 #include "qemu/timer.h"
72 #include "chardev/char.h"
73 #include "qemu/bitmap.h"
74 #include "qemu/log.h"
75 #include "sysemu/blockdev.h"
76 #include "hw/block/block.h"
77 #include "hw/i386/x86.h"
78 #include "hw/i386/pc.h"
79 #include "migration/misc.h"
80 #include "migration/snapshot.h"
81 #include "sysemu/tpm.h"
82 #include "sysemu/dma.h"
83 #include "hw/audio/soundhw.h"
84 #include "audio/audio.h"
85 #include "sysemu/cpus.h"
86 #include "sysemu/cpu-timers.h"
87 #include "migration/colo.h"
88 #include "migration/postcopy-ram.h"
89 #include "sysemu/kvm.h"
90 #include "sysemu/hax.h"
91 #include "qapi/qobject-input-visitor.h"
92 #include "qemu/option.h"
93 #include "qemu/config-file.h"
94 #include "qemu/qemu-options.h"
95 #include "qemu/main-loop.h"
96 #ifdef CONFIG_VIRTFS
97 #include "fsdev/qemu-fsdev.h"
98 #endif
99 #include "sysemu/qtest.h"
101 #include "disas/disas.h"
103 #include "trace.h"
104 #include "trace/control.h"
105 #include "qemu/plugin.h"
106 #include "qemu/queue.h"
107 #include "sysemu/arch_init.h"
108 #include "exec/confidential-guest-support.h"
110 #include "ui/qemu-spice.h"
111 #include "qapi/string-input-visitor.h"
112 #include "qapi/opts-visitor.h"
113 #include "qapi/clone-visitor.h"
114 #include "qom/object_interfaces.h"
115 #include "semihosting/semihost.h"
116 #include "crypto/init.h"
117 #include "sysemu/replay.h"
118 #include "qapi/qapi-events-run-state.h"
119 #include "qapi/qapi-visit-block-core.h"
120 #include "qapi/qapi-visit-compat.h"
121 #include "qapi/qapi-visit-ui.h"
122 #include "qapi/qapi-commands-block-core.h"
123 #include "qapi/qapi-commands-migration.h"
124 #include "qapi/qapi-commands-misc.h"
125 #include "qapi/qapi-visit-qom.h"
126 #include "qapi/qapi-commands-ui.h"
127 #include "qapi/qmp/qdict.h"
128 #include "qapi/qmp/qerror.h"
129 #include "sysemu/iothread.h"
130 #include "qemu/guest-random.h"
132 #include "config-host.h"
134 #define MAX_VIRTIO_CONSOLES 1
136 typedef struct BlockdevOptionsQueueEntry {
137 BlockdevOptions *bdo;
138 Location loc;
139 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
140 } BlockdevOptionsQueueEntry;
142 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
144 typedef struct ObjectOption {
145 ObjectOptions *opts;
146 QTAILQ_ENTRY(ObjectOption) next;
147 } ObjectOption;
149 typedef struct DeviceOption {
150 QDict *opts;
151 Location loc;
152 QTAILQ_ENTRY(DeviceOption) next;
153 } DeviceOption;
155 static const char *cpu_option;
156 static const char *mem_path;
157 static const char *incoming;
158 static const char *loadvm;
159 static const char *accelerators;
160 static QDict *machine_opts_dict;
161 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
162 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
163 static ram_addr_t maxram_size;
164 static uint64_t ram_slots;
165 static int display_remote;
166 static int snapshot;
167 static bool preconfig_requested;
168 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
169 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
170 static bool nographic = false;
171 static int mem_prealloc; /* force preallocation of physical target memory */
172 static ram_addr_t ram_size;
173 static const char *vga_model = NULL;
174 static DisplayOptions dpy;
175 static int num_serial_hds;
176 static Chardev **serial_hds;
177 static const char *log_mask;
178 static const char *log_file;
179 static bool list_data_dirs;
180 static const char *watchdog;
181 static const char *qtest_chrdev;
182 static const char *qtest_log;
184 static int has_defaults = 1;
185 static int default_serial = 1;
186 static int default_parallel = 1;
187 static int default_monitor = 1;
188 static int default_floppy = 1;
189 static int default_cdrom = 1;
190 static int default_sdcard = 1;
191 static int default_vga = 1;
192 static int default_net = 1;
194 static struct {
195 const char *driver;
196 int *flag;
197 } default_list[] = {
198 { .driver = "isa-serial", .flag = &default_serial },
199 { .driver = "isa-parallel", .flag = &default_parallel },
200 { .driver = "isa-fdc", .flag = &default_floppy },
201 { .driver = "floppy", .flag = &default_floppy },
202 { .driver = "ide-cd", .flag = &default_cdrom },
203 { .driver = "ide-hd", .flag = &default_cdrom },
204 { .driver = "scsi-cd", .flag = &default_cdrom },
205 { .driver = "scsi-hd", .flag = &default_cdrom },
206 { .driver = "VGA", .flag = &default_vga },
207 { .driver = "isa-vga", .flag = &default_vga },
208 { .driver = "cirrus-vga", .flag = &default_vga },
209 { .driver = "isa-cirrus-vga", .flag = &default_vga },
210 { .driver = "vmware-svga", .flag = &default_vga },
211 { .driver = "qxl-vga", .flag = &default_vga },
212 { .driver = "virtio-vga", .flag = &default_vga },
213 { .driver = "ati-vga", .flag = &default_vga },
214 { .driver = "vhost-user-vga", .flag = &default_vga },
215 { .driver = "virtio-vga-gl", .flag = &default_vga },
218 static QemuOptsList qemu_rtc_opts = {
219 .name = "rtc",
220 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
221 .merge_lists = true,
222 .desc = {
224 .name = "base",
225 .type = QEMU_OPT_STRING,
227 .name = "clock",
228 .type = QEMU_OPT_STRING,
230 .name = "driftfix",
231 .type = QEMU_OPT_STRING,
233 { /* end of list */ }
237 static QemuOptsList qemu_option_rom_opts = {
238 .name = "option-rom",
239 .implied_opt_name = "romfile",
240 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
241 .desc = {
243 .name = "bootindex",
244 .type = QEMU_OPT_NUMBER,
245 }, {
246 .name = "romfile",
247 .type = QEMU_OPT_STRING,
249 { /* end of list */ }
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 */ }
501 const char *qemu_get_vm_name(void)
503 return qemu_name;
506 static void default_driver_disable(const char *driver)
508 int i;
510 if (!driver) {
511 return;
514 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
515 if (strcmp(default_list[i].driver, driver) != 0)
516 continue;
517 *(default_list[i].flag) = 0;
521 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
523 const char *driver = qemu_opt_get(opts, "driver");
525 default_driver_disable(driver);
526 return 0;
529 static void default_driver_check_json(void)
531 DeviceOption *opt;
533 QTAILQ_FOREACH(opt, &device_opts, next) {
534 const char *driver = qdict_get_try_str(opt->opts, "driver");
535 default_driver_disable(driver);
539 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
541 const char *proc_name;
543 if (qemu_opt_get(opts, "debug-threads")) {
544 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
546 qemu_name = qemu_opt_get(opts, "guest");
548 proc_name = qemu_opt_get(opts, "process");
549 if (proc_name) {
550 os_set_proc_name(proc_name);
553 return 0;
556 bool defaults_enabled(void)
558 return has_defaults;
561 #ifndef _WIN32
562 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
564 int fd, dupfd, flags;
565 int64_t fdset_id;
566 const char *fd_opaque = NULL;
567 AddfdInfo *fdinfo;
569 fd = qemu_opt_get_number(opts, "fd", -1);
570 fdset_id = qemu_opt_get_number(opts, "set", -1);
571 fd_opaque = qemu_opt_get(opts, "opaque");
573 if (fd < 0) {
574 error_setg(errp, "fd option is required and must be non-negative");
575 return -1;
578 if (fd <= STDERR_FILENO) {
579 error_setg(errp, "fd cannot be a standard I/O stream");
580 return -1;
584 * All fds inherited across exec() necessarily have FD_CLOEXEC
585 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
587 flags = fcntl(fd, F_GETFD);
588 if (flags == -1 || (flags & FD_CLOEXEC)) {
589 error_setg(errp, "fd is not valid or already in use");
590 return -1;
593 if (fdset_id < 0) {
594 error_setg(errp, "set option is required and must be non-negative");
595 return -1;
598 #ifdef F_DUPFD_CLOEXEC
599 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
600 #else
601 dupfd = dup(fd);
602 if (dupfd != -1) {
603 qemu_set_cloexec(dupfd);
605 #endif
606 if (dupfd == -1) {
607 error_setg(errp, "error duplicating fd: %s", strerror(errno));
608 return -1;
611 /* add the duplicate fd, and optionally the opaque string, to the fd set */
612 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
613 &error_abort);
614 g_free(fdinfo);
616 return 0;
619 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
621 int fd;
623 fd = qemu_opt_get_number(opts, "fd", -1);
624 close(fd);
626 return 0;
628 #endif
630 /***********************************************************/
631 /* QEMU Block devices */
633 #define HD_OPTS "media=disk"
634 #define CDROM_OPTS "media=cdrom"
635 #define FD_OPTS ""
636 #define PFLASH_OPTS ""
637 #define MTD_OPTS ""
638 #define SD_OPTS ""
640 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
642 BlockInterfaceType *block_default_type = opaque;
644 return drive_new(opts, *block_default_type, errp) == NULL;
647 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
649 if (qemu_opt_get(opts, "snapshot") == NULL) {
650 qemu_opt_set(opts, "snapshot", "on", &error_abort);
652 return 0;
655 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
656 int index, const char *optstr)
658 QemuOpts *opts;
659 DriveInfo *dinfo;
661 if (!enable || drive_get_by_index(type, index)) {
662 return;
665 opts = drive_add(type, index, NULL, optstr);
666 if (snapshot) {
667 drive_enable_snapshot(NULL, opts, NULL);
670 dinfo = drive_new(opts, type, &error_abort);
671 dinfo->is_default = true;
675 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
676 MachineClass *machine_class, int snapshot)
679 * If the currently selected machine wishes to override the
680 * units-per-bus property of its default HBA interface type, do so
681 * now.
683 if (machine_class->units_per_default_bus) {
684 override_max_devs(machine_class->block_default_type,
685 machine_class->units_per_default_bus);
688 /* open the virtual block devices */
689 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
690 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
692 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
693 loc_push_restore(&bdo->loc);
694 qmp_blockdev_add(bdo->bdo, &error_fatal);
695 loc_pop(&bdo->loc);
696 qapi_free_BlockdevOptions(bdo->bdo);
697 g_free(bdo);
699 if (snapshot) {
700 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
701 NULL, NULL);
703 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
704 &machine_class->block_default_type, &error_fatal)) {
705 /* We printed help */
706 exit(0);
709 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
710 CDROM_OPTS);
711 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
712 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
716 static QemuOptsList qemu_smp_opts = {
717 .name = "smp-opts",
718 .implied_opt_name = "cpus",
719 .merge_lists = true,
720 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
721 .desc = {
723 .name = "cpus",
724 .type = QEMU_OPT_NUMBER,
725 }, {
726 .name = "sockets",
727 .type = QEMU_OPT_NUMBER,
728 }, {
729 .name = "dies",
730 .type = QEMU_OPT_NUMBER,
731 }, {
732 .name = "clusters",
733 .type = QEMU_OPT_NUMBER,
734 }, {
735 .name = "cores",
736 .type = QEMU_OPT_NUMBER,
737 }, {
738 .name = "threads",
739 .type = QEMU_OPT_NUMBER,
740 }, {
741 .name = "maxcpus",
742 .type = QEMU_OPT_NUMBER,
744 { /*End of list */ }
748 static void realtime_init(void)
750 if (enable_mlock) {
751 if (os_mlock() < 0) {
752 error_report("locking memory failed");
753 exit(1);
759 static void configure_msg(QemuOpts *opts)
761 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
762 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
766 /***********************************************************/
767 /* USB devices */
769 static int usb_device_add(const char *devname)
771 USBDevice *dev = NULL;
773 if (!machine_usb(current_machine)) {
774 return -1;
777 dev = usbdevice_create(devname);
778 if (!dev)
779 return -1;
781 return 0;
784 static int usb_parse(const char *cmdline)
786 int r;
787 r = usb_device_add(cmdline);
788 if (r < 0) {
789 error_report("could not add USB device '%s'", cmdline);
791 return r;
794 /***********************************************************/
795 /* machine registration */
797 static MachineClass *find_machine(const char *name, GSList *machines)
799 GSList *el;
801 for (el = machines; el; el = el->next) {
802 MachineClass *mc = el->data;
804 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
805 return mc;
809 return NULL;
812 static MachineClass *find_default_machine(GSList *machines)
814 GSList *el;
815 MachineClass *default_machineclass = NULL;
817 for (el = machines; el; el = el->next) {
818 MachineClass *mc = el->data;
820 if (mc->is_default) {
821 assert(default_machineclass == NULL && "Multiple default machines");
822 default_machineclass = mc;
826 return default_machineclass;
829 static void version(void)
831 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
832 QEMU_COPYRIGHT "\n");
835 static void help(int exitcode)
837 version();
838 printf("usage: %s [options] [disk_image]\n\n"
839 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
840 g_get_prgname());
842 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
843 if ((arch_mask) & arch_type) \
844 fputs(opt_help, stdout);
846 #define ARCHHEADING(text, arch_mask) \
847 if ((arch_mask) & arch_type) \
848 puts(stringify(text));
850 #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
852 #include "qemu-options.def"
854 printf("\nDuring emulation, the following keys are useful:\n"
855 "ctrl-alt-f toggle full screen\n"
856 "ctrl-alt-n switch to virtual console 'n'\n"
857 "ctrl-alt toggle mouse and keyboard grab\n"
858 "\n"
859 "When using -nographic, press 'ctrl-a h' to get some help.\n"
860 "\n"
861 QEMU_HELP_BOTTOM "\n");
863 exit(exitcode);
866 #define HAS_ARG 0x0001
868 typedef struct QEMUOption {
869 const char *name;
870 int flags;
871 int index;
872 uint32_t arch_mask;
873 } QEMUOption;
875 static const QEMUOption qemu_options[] = {
876 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
878 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
879 { option, opt_arg, opt_enum, arch_mask },
880 #define DEFHEADING(text)
881 #define ARCHHEADING(text, arch_mask)
883 #include "qemu-options.def"
884 { NULL },
887 typedef struct VGAInterfaceInfo {
888 const char *opt_name; /* option name */
889 const char *name; /* human-readable name */
890 /* Class names indicating that support is available.
891 * If no class is specified, the interface is always available */
892 const char *class_names[2];
893 } VGAInterfaceInfo;
895 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
896 [VGA_NONE] = {
897 .opt_name = "none",
898 .name = "no graphic card",
900 [VGA_STD] = {
901 .opt_name = "std",
902 .name = "standard VGA",
903 .class_names = { "VGA", "isa-vga" },
905 [VGA_CIRRUS] = {
906 .opt_name = "cirrus",
907 .name = "Cirrus VGA",
908 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
910 [VGA_VMWARE] = {
911 .opt_name = "vmware",
912 .name = "VMWare SVGA",
913 .class_names = { "vmware-svga" },
915 [VGA_VIRTIO] = {
916 .opt_name = "virtio",
917 .name = "Virtio VGA",
918 .class_names = { "virtio-vga" },
920 [VGA_QXL] = {
921 .opt_name = "qxl",
922 .name = "QXL VGA",
923 .class_names = { "qxl-vga" },
925 [VGA_TCX] = {
926 .opt_name = "tcx",
927 .name = "TCX framebuffer",
928 .class_names = { "sun-tcx" },
930 [VGA_CG3] = {
931 .opt_name = "cg3",
932 .name = "CG3 framebuffer",
933 .class_names = { "cgthree" },
935 [VGA_XENFB] = {
936 .opt_name = "xenfb",
937 .name = "Xen paravirtualized framebuffer",
941 static bool vga_interface_available(VGAInterfaceType t)
943 const VGAInterfaceInfo *ti = &vga_interfaces[t];
945 assert(t < VGA_TYPE_MAX);
946 return !ti->class_names[0] ||
947 module_object_class_by_name(ti->class_names[0]) ||
948 module_object_class_by_name(ti->class_names[1]);
951 static const char *
952 get_default_vga_model(const MachineClass *machine_class)
954 if (machine_class->default_display) {
955 return machine_class->default_display;
956 } else if (vga_interface_available(VGA_CIRRUS)) {
957 return "cirrus";
958 } else if (vga_interface_available(VGA_STD)) {
959 return "std";
962 return NULL;
965 static void select_vgahw(const MachineClass *machine_class, const char *p)
967 const char *opts;
968 int t;
970 if (g_str_equal(p, "help")) {
971 const char *def = get_default_vga_model(machine_class);
973 for (t = 0; t < VGA_TYPE_MAX; t++) {
974 const VGAInterfaceInfo *ti = &vga_interfaces[t];
976 if (vga_interface_available(t) && ti->opt_name) {
977 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
978 g_str_equal(ti->opt_name, def) ? " (default)" : "");
981 exit(0);
984 assert(vga_interface_type == VGA_NONE);
985 for (t = 0; t < VGA_TYPE_MAX; t++) {
986 const VGAInterfaceInfo *ti = &vga_interfaces[t];
987 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
988 if (!vga_interface_available(t)) {
989 error_report("%s not available", ti->name);
990 exit(1);
992 vga_interface_type = t;
993 break;
996 if (t == VGA_TYPE_MAX) {
997 invalid_vga:
998 error_report("unknown vga type: %s", p);
999 exit(1);
1001 while (*opts) {
1002 const char *nextopt;
1004 if (strstart(opts, ",retrace=", &nextopt)) {
1005 opts = nextopt;
1006 if (strstart(opts, "dumb", &nextopt))
1007 vga_retrace_method = VGA_RETRACE_DUMB;
1008 else if (strstart(opts, "precise", &nextopt))
1009 vga_retrace_method = VGA_RETRACE_PRECISE;
1010 else goto invalid_vga;
1011 } else goto invalid_vga;
1012 opts = nextopt;
1016 static void parse_display_qapi(const char *optarg)
1018 DisplayOptions *opts;
1019 Visitor *v;
1021 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1023 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1024 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1026 qapi_free_DisplayOptions(opts);
1027 visit_free(v);
1030 DisplayOptions *qmp_query_display_options(Error **errp)
1032 return QAPI_CLONE(DisplayOptions, &dpy);
1035 static void parse_display(const char *p)
1037 const char *opts;
1039 if (is_help_option(p)) {
1040 qemu_display_help();
1041 exit(0);
1044 if (strstart(p, "sdl", &opts)) {
1046 * sdl DisplayType needs hand-crafted parser instead of
1047 * parse_display_qapi() due to some options not in
1048 * DisplayOptions, specifically:
1049 * - ctrl_grab + alt_grab
1050 * They can't be moved into the QAPI since they use underscores,
1051 * thus they will get replaced by "grab-mod" in the long term
1053 #if defined(CONFIG_SDL)
1054 dpy.type = DISPLAY_TYPE_SDL;
1055 while (*opts) {
1056 const char *nextopt;
1058 if (strstart(opts, ",grab-mod=", &nextopt)) {
1059 opts = nextopt;
1060 if (strstart(opts, "lshift-lctrl-lalt", &nextopt)) {
1061 alt_grab = 1;
1062 } else if (strstart(opts, "rctrl", &nextopt)) {
1063 ctrl_grab = 1;
1064 } else {
1065 goto invalid_sdl_args;
1067 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1068 opts = nextopt;
1069 if (strstart(opts, "on", &nextopt)) {
1070 alt_grab = 1;
1071 } else if (strstart(opts, "off", &nextopt)) {
1072 alt_grab = 0;
1073 } else {
1074 goto invalid_sdl_args;
1076 warn_report("alt_grab is deprecated, use grab-mod instead.");
1077 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1078 opts = nextopt;
1079 if (strstart(opts, "on", &nextopt)) {
1080 ctrl_grab = 1;
1081 } else if (strstart(opts, "off", &nextopt)) {
1082 ctrl_grab = 0;
1083 } else {
1084 goto invalid_sdl_args;
1086 warn_report("ctrl_grab is deprecated, use grab-mod instead.");
1087 } else if (strstart(opts, ",window_close=", &nextopt) ||
1088 strstart(opts, ",window-close=", &nextopt)) {
1089 if (strstart(opts, ",window_close=", NULL)) {
1090 warn_report("window_close with an underscore is deprecated,"
1091 " please use window-close instead.");
1093 opts = nextopt;
1094 dpy.has_window_close = true;
1095 if (strstart(opts, "on", &nextopt)) {
1096 dpy.window_close = true;
1097 } else if (strstart(opts, "off", &nextopt)) {
1098 dpy.window_close = false;
1099 } else {
1100 goto invalid_sdl_args;
1102 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1103 opts = nextopt;
1104 dpy.has_show_cursor = true;
1105 if (strstart(opts, "on", &nextopt)) {
1106 dpy.show_cursor = true;
1107 } else if (strstart(opts, "off", &nextopt)) {
1108 dpy.show_cursor = false;
1109 } else {
1110 goto invalid_sdl_args;
1112 } else if (strstart(opts, ",gl=", &nextopt)) {
1113 opts = nextopt;
1114 dpy.has_gl = true;
1115 if (strstart(opts, "on", &nextopt)) {
1116 dpy.gl = DISPLAYGL_MODE_ON;
1117 } else if (strstart(opts, "core", &nextopt)) {
1118 dpy.gl = DISPLAYGL_MODE_CORE;
1119 } else if (strstart(opts, "es", &nextopt)) {
1120 dpy.gl = DISPLAYGL_MODE_ES;
1121 } else if (strstart(opts, "off", &nextopt)) {
1122 dpy.gl = DISPLAYGL_MODE_OFF;
1123 } else {
1124 goto invalid_sdl_args;
1126 } else {
1127 invalid_sdl_args:
1128 error_report("invalid SDL option string");
1129 exit(1);
1131 opts = nextopt;
1133 #else
1134 error_report("SDL display supported is not available in this binary");
1135 exit(1);
1136 #endif
1137 } else if (strstart(p, "vnc", &opts)) {
1139 * vnc isn't a (local) DisplayType but a protocol for remote
1140 * display access.
1142 if (*opts == '=') {
1143 vnc_parse(opts + 1);
1144 } else {
1145 error_report("VNC requires a display argument vnc=<display>");
1146 exit(1);
1148 } else {
1149 parse_display_qapi(p);
1153 static inline bool nonempty_str(const char *str)
1155 return str && *str;
1158 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1160 gchar *buf;
1161 size_t size;
1162 const char *name, *file, *str, *gen_id;
1163 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1165 if (fw_cfg == NULL) {
1166 error_setg(errp, "fw_cfg device not available");
1167 return -1;
1169 name = qemu_opt_get(opts, "name");
1170 file = qemu_opt_get(opts, "file");
1171 str = qemu_opt_get(opts, "string");
1172 gen_id = qemu_opt_get(opts, "gen_id");
1174 /* we need the name, and exactly one of: file, content string, gen_id */
1175 if (!nonempty_str(name) ||
1176 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1177 error_setg(errp, "name, plus exactly one of file,"
1178 " string and gen_id, are needed");
1179 return -1;
1181 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1182 error_setg(errp, "name too long (max. %d char)",
1183 FW_CFG_MAX_FILE_PATH - 1);
1184 return -1;
1186 if (nonempty_str(gen_id)) {
1188 * In this particular case where the content is populated
1189 * internally, the "etc/" namespace protection is relaxed,
1190 * so do not emit a warning.
1192 } else if (strncmp(name, "opt/", 4) != 0) {
1193 warn_report("externally provided fw_cfg item names "
1194 "should be prefixed with \"opt/\"");
1196 if (nonempty_str(str)) {
1197 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1198 buf = g_memdup(str, size);
1199 } else if (nonempty_str(gen_id)) {
1200 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1201 return -1;
1203 return 0;
1204 } else {
1205 GError *err = NULL;
1206 if (!g_file_get_contents(file, &buf, &size, &err)) {
1207 error_setg(errp, "can't load %s: %s", file, err->message);
1208 g_error_free(err);
1209 return -1;
1212 /* For legacy, keep user files in a specific global order. */
1213 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1214 fw_cfg_add_file(fw_cfg, name, buf, size);
1215 fw_cfg_reset_order_override(fw_cfg);
1216 return 0;
1219 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1221 return qdev_device_help(opts);
1224 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1226 DeviceState *dev;
1228 dev = qdev_device_add(opts, errp);
1229 if (!dev && *errp) {
1230 error_report_err(*errp);
1231 return -1;
1232 } else if (dev) {
1233 object_unref(OBJECT(dev));
1235 return 0;
1238 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1240 Error *local_err = NULL;
1242 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1243 if (local_err) {
1244 error_propagate(errp, local_err);
1245 return -1;
1247 exit(0);
1249 return 0;
1252 #ifdef CONFIG_VIRTFS
1253 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1255 return qemu_fsdev_add(opts, errp);
1257 #endif
1259 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1261 return monitor_init_opts(opts, errp);
1264 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1266 static int monitor_device_index = 0;
1267 QemuOpts *opts;
1268 const char *p;
1269 char label[32];
1271 if (strstart(optarg, "chardev:", &p)) {
1272 snprintf(label, sizeof(label), "%s", p);
1273 } else {
1274 snprintf(label, sizeof(label), "compat_monitor%d",
1275 monitor_device_index);
1276 opts = qemu_chr_parse_compat(label, optarg, true);
1277 if (!opts) {
1278 error_report("parse error: %s", optarg);
1279 exit(1);
1283 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1284 qemu_opt_set(opts, "mode", mode, &error_abort);
1285 qemu_opt_set(opts, "chardev", label, &error_abort);
1286 if (!strcmp(mode, "control")) {
1287 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1288 } else {
1289 assert(pretty == false);
1291 monitor_device_index++;
1294 struct device_config {
1295 enum {
1296 DEV_USB, /* -usbdevice */
1297 DEV_SERIAL, /* -serial */
1298 DEV_PARALLEL, /* -parallel */
1299 DEV_DEBUGCON, /* -debugcon */
1300 DEV_GDB, /* -gdb, -s */
1301 DEV_SCLP, /* s390 sclp */
1302 } type;
1303 const char *cmdline;
1304 Location loc;
1305 QTAILQ_ENTRY(device_config) next;
1308 static QTAILQ_HEAD(, device_config) device_configs =
1309 QTAILQ_HEAD_INITIALIZER(device_configs);
1311 static void add_device_config(int type, const char *cmdline)
1313 struct device_config *conf;
1315 conf = g_malloc0(sizeof(*conf));
1316 conf->type = type;
1317 conf->cmdline = cmdline;
1318 loc_save(&conf->loc);
1319 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1322 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1324 struct device_config *conf;
1325 int rc;
1327 QTAILQ_FOREACH(conf, &device_configs, next) {
1328 if (conf->type != type)
1329 continue;
1330 loc_push_restore(&conf->loc);
1331 rc = func(conf->cmdline);
1332 loc_pop(&conf->loc);
1333 if (rc) {
1334 return rc;
1337 return 0;
1340 static void qemu_disable_default_devices(void)
1342 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1344 default_driver_check_json();
1345 qemu_opts_foreach(qemu_find_opts("device"),
1346 default_driver_check, NULL, NULL);
1347 qemu_opts_foreach(qemu_find_opts("global"),
1348 default_driver_check, NULL, NULL);
1350 if (!vga_model && !default_vga) {
1351 vga_interface_type = VGA_DEVICE;
1353 if (!has_defaults || machine_class->no_serial) {
1354 default_serial = 0;
1356 if (!has_defaults || machine_class->no_parallel) {
1357 default_parallel = 0;
1359 if (!has_defaults || machine_class->no_floppy) {
1360 default_floppy = 0;
1362 if (!has_defaults || machine_class->no_cdrom) {
1363 default_cdrom = 0;
1365 if (!has_defaults || machine_class->no_sdcard) {
1366 default_sdcard = 0;
1368 if (!has_defaults) {
1369 default_monitor = 0;
1370 default_net = 0;
1371 default_vga = 0;
1375 static void qemu_create_default_devices(void)
1377 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1379 if (is_daemonized()) {
1380 /* According to documentation and historically, -nographic redirects
1381 * serial port, parallel port and monitor to stdio, which does not work
1382 * with -daemonize. We can redirect these to null instead, but since
1383 * -nographic is legacy, let's just error out.
1384 * We disallow -nographic only if all other ports are not redirected
1385 * explicitly, to not break existing legacy setups which uses
1386 * -nographic _and_ redirects all ports explicitly - this is valid
1387 * usage, -nographic is just a no-op in this case.
1389 if (nographic
1390 && (default_parallel || default_serial || default_monitor)) {
1391 error_report("-nographic cannot be used with -daemonize");
1392 exit(1);
1396 if (nographic) {
1397 if (default_parallel)
1398 add_device_config(DEV_PARALLEL, "null");
1399 if (default_serial && default_monitor) {
1400 add_device_config(DEV_SERIAL, "mon:stdio");
1401 } else {
1402 if (default_serial)
1403 add_device_config(DEV_SERIAL, "stdio");
1404 if (default_monitor)
1405 monitor_parse("stdio", "readline", false);
1407 } else {
1408 if (default_serial)
1409 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1410 if (default_parallel)
1411 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1412 if (default_monitor)
1413 monitor_parse("vc:80Cx24C", "readline", false);
1416 if (default_net) {
1417 QemuOptsList *net = qemu_find_opts("net");
1418 qemu_opts_parse(net, "nic", true, &error_abort);
1419 #ifdef CONFIG_SLIRP
1420 qemu_opts_parse(net, "user", true, &error_abort);
1421 #endif
1424 #if defined(CONFIG_VNC)
1425 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1426 display_remote++;
1428 #endif
1429 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1430 if (!qemu_display_find_default(&dpy)) {
1431 dpy.type = DISPLAY_TYPE_NONE;
1432 #if defined(CONFIG_VNC)
1433 vnc_parse("localhost:0,to=99,id=default");
1434 #endif
1437 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1438 dpy.type = DISPLAY_TYPE_NONE;
1441 /* If no default VGA is requested, the default is "none". */
1442 if (default_vga) {
1443 vga_model = get_default_vga_model(machine_class);
1445 if (vga_model) {
1446 select_vgahw(machine_class, vga_model);
1450 static int serial_parse(const char *devname)
1452 int index = num_serial_hds;
1453 char label[32];
1455 if (strcmp(devname, "none") == 0)
1456 return 0;
1457 snprintf(label, sizeof(label), "serial%d", index);
1458 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1460 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1461 if (!serial_hds[index]) {
1462 error_report("could not connect serial device"
1463 " to character backend '%s'", devname);
1464 return -1;
1466 num_serial_hds++;
1467 return 0;
1470 Chardev *serial_hd(int i)
1472 assert(i >= 0);
1473 if (i < num_serial_hds) {
1474 return serial_hds[i];
1476 return NULL;
1479 static int parallel_parse(const char *devname)
1481 static int index = 0;
1482 char label[32];
1484 if (strcmp(devname, "none") == 0)
1485 return 0;
1486 if (index == MAX_PARALLEL_PORTS) {
1487 error_report("too many parallel ports");
1488 exit(1);
1490 snprintf(label, sizeof(label), "parallel%d", index);
1491 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1492 if (!parallel_hds[index]) {
1493 error_report("could not connect parallel device"
1494 " to character backend '%s'", devname);
1495 return -1;
1497 index++;
1498 return 0;
1501 static int debugcon_parse(const char *devname)
1503 QemuOpts *opts;
1505 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1506 error_report("invalid character backend '%s'", devname);
1507 exit(1);
1509 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1510 if (!opts) {
1511 error_report("already have a debugcon device");
1512 exit(1);
1514 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1515 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1516 return 0;
1519 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1521 const MachineClass *mc1 = a, *mc2 = b;
1522 int res;
1524 if (mc1->family == NULL) {
1525 if (mc2->family == NULL) {
1526 /* Compare standalone machine types against each other; they sort
1527 * in increasing order.
1529 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1530 object_class_get_name(OBJECT_CLASS(mc2)));
1533 /* Standalone machine types sort after families. */
1534 return 1;
1537 if (mc2->family == NULL) {
1538 /* Families sort before standalone machine types. */
1539 return -1;
1542 /* Families sort between each other alphabetically increasingly. */
1543 res = strcmp(mc1->family, mc2->family);
1544 if (res != 0) {
1545 return res;
1548 /* Within the same family, machine types sort in decreasing order. */
1549 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1550 object_class_get_name(OBJECT_CLASS(mc1)));
1553 static void machine_help_func(const QDict *qdict)
1555 GSList *machines, *el;
1556 const char *type = qdict_get_try_str(qdict, "type");
1558 machines = object_class_get_list(TYPE_MACHINE, false);
1559 if (type) {
1560 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1561 if (machine_class) {
1562 type_print_class_properties(object_class_get_name(machine_class));
1563 return;
1567 printf("Supported machines are:\n");
1568 machines = g_slist_sort(machines, machine_class_cmp);
1569 for (el = machines; el; el = el->next) {
1570 MachineClass *mc = el->data;
1571 if (mc->alias) {
1572 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1574 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1575 mc->is_default ? " (default)" : "",
1576 mc->deprecation_reason ? " (deprecated)" : "");
1580 static void
1581 machine_merge_property(const char *propname, QDict *prop, Error **errp)
1583 QDict *opts;
1585 opts = qdict_new();
1586 /* Preserve the caller's reference to prop. */
1587 qobject_ref(prop);
1588 qdict_put(opts, propname, prop);
1589 keyval_merge(machine_opts_dict, opts, errp);
1590 qobject_unref(opts);
1593 static void
1594 machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
1595 const char *arg)
1597 QDict *prop = NULL;
1598 bool help = false;
1600 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
1601 if (help) {
1602 qemu_opts_print_help(opts_list, true);
1603 exit(0);
1605 machine_merge_property(propname, prop, &error_fatal);
1606 qobject_unref(prop);
1609 static const char *pid_file;
1610 static Notifier qemu_unlink_pidfile_notifier;
1612 static void qemu_unlink_pidfile(Notifier *n, void *data)
1614 if (pid_file) {
1615 unlink(pid_file);
1619 static const QEMUOption *lookup_opt(int argc, char **argv,
1620 const char **poptarg, int *poptind)
1622 const QEMUOption *popt;
1623 int optind = *poptind;
1624 char *r = argv[optind];
1625 const char *optarg;
1627 loc_set_cmdline(argv, optind, 1);
1628 optind++;
1629 /* Treat --foo the same as -foo. */
1630 if (r[1] == '-')
1631 r++;
1632 popt = qemu_options;
1633 for(;;) {
1634 if (!popt->name) {
1635 error_report("invalid option");
1636 exit(1);
1638 if (!strcmp(popt->name, r + 1))
1639 break;
1640 popt++;
1642 if (popt->flags & HAS_ARG) {
1643 if (optind >= argc) {
1644 error_report("requires an argument");
1645 exit(1);
1647 optarg = argv[optind++];
1648 loc_set_cmdline(argv, optind - 2, 2);
1649 } else {
1650 optarg = NULL;
1653 *poptarg = optarg;
1654 *poptind = optind;
1656 return popt;
1659 static MachineClass *select_machine(QDict *qdict, Error **errp)
1661 const char *optarg = qdict_get_try_str(qdict, "type");
1662 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1663 MachineClass *machine_class;
1664 Error *local_err = NULL;
1666 if (optarg) {
1667 machine_class = find_machine(optarg, machines);
1668 qdict_del(qdict, "type");
1669 if (!machine_class) {
1670 error_setg(&local_err, "unsupported machine type");
1672 } else {
1673 machine_class = find_default_machine(machines);
1674 if (!machine_class) {
1675 error_setg(&local_err, "No machine specified, and there is no default");
1679 g_slist_free(machines);
1680 if (local_err) {
1681 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1682 error_propagate(errp, local_err);
1684 return machine_class;
1687 static int object_parse_property_opt(Object *obj,
1688 const char *name, const char *value,
1689 const char *skip, Error **errp)
1691 if (g_str_equal(name, skip)) {
1692 return 0;
1695 if (!object_property_parse(obj, name, value, errp)) {
1696 return -1;
1699 return 0;
1702 /* *Non*recursively replace underscores with dashes in QDict keys. */
1703 static void keyval_dashify(QDict *qdict, Error **errp)
1705 const QDictEntry *ent, *next;
1706 char *p;
1708 for (ent = qdict_first(qdict); ent; ent = next) {
1709 g_autofree char *new_key = NULL;
1711 next = qdict_next(qdict, ent);
1712 if (!strchr(ent->key, '_')) {
1713 continue;
1715 new_key = g_strdup(ent->key);
1716 for (p = new_key; *p; p++) {
1717 if (*p == '_') {
1718 *p = '-';
1721 if (qdict_haskey(qdict, new_key)) {
1722 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1723 return;
1725 qobject_ref(ent->value);
1726 qdict_put_obj(qdict, new_key, ent->value);
1727 qdict_del(qdict, ent->key);
1731 static void qemu_apply_legacy_machine_options(QDict *qdict)
1733 const char *value;
1735 keyval_dashify(qdict, &error_fatal);
1737 /* Legacy options do not correspond to MachineState properties. */
1738 value = qdict_get_try_str(qdict, "accel");
1739 if (value) {
1740 accelerators = g_strdup(value);
1741 qdict_del(qdict, "accel");
1744 value = qdict_get_try_str(qdict, "igd-passthru");
1745 if (value) {
1746 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
1747 false);
1748 qdict_del(qdict, "igd-passthru");
1751 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1752 if (value) {
1753 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
1754 false);
1755 qdict_del(qdict, "kvm-shadow-mem");
1758 value = qdict_get_try_str(qdict, "kernel-irqchip");
1759 if (value) {
1760 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
1761 false);
1762 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
1763 false);
1764 qdict_del(qdict, "kernel-irqchip");
1768 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1770 ObjectOption *opt, *next;
1772 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1773 const char *type = ObjectType_str(opt->opts->qom_type);
1774 if (type_opt_predicate(type)) {
1775 user_creatable_add_qapi(opt->opts, &error_fatal);
1776 qapi_free_ObjectOptions(opt->opts);
1777 QTAILQ_REMOVE(&object_opts, opt, next);
1778 g_free(opt);
1783 static void object_option_add_visitor(Visitor *v)
1785 ObjectOption *opt = g_new0(ObjectOption, 1);
1786 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1787 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1790 static void object_option_parse(const char *optarg)
1792 QemuOpts *opts;
1793 const char *type;
1794 Visitor *v;
1796 if (optarg[0] == '{') {
1797 QObject *obj = qobject_from_json(optarg, &error_fatal);
1799 v = qobject_input_visitor_new(obj);
1800 qobject_unref(obj);
1801 } else {
1802 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1803 optarg, true);
1804 if (!opts) {
1805 exit(1);
1808 type = qemu_opt_get(opts, "qom-type");
1809 if (!type) {
1810 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1812 if (user_creatable_print_help(type, opts)) {
1813 exit(0);
1816 v = opts_visitor_new(opts);
1819 object_option_add_visitor(v);
1820 visit_free(v);
1824 * Initial object creation happens before all other
1825 * QEMU data types are created. The majority of objects
1826 * can be created at this point. The rng-egd object
1827 * cannot be created here, as it depends on the chardev
1828 * already existing.
1830 static bool object_create_early(const char *type)
1833 * Objects should not be made "delayed" without a reason. If you
1834 * add one, state the reason in a comment!
1837 /* Reason: property "chardev" */
1838 if (g_str_equal(type, "rng-egd") ||
1839 g_str_equal(type, "qtest")) {
1840 return false;
1843 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1844 /* Reason: cryptodev-vhost-user property "chardev" */
1845 if (g_str_equal(type, "cryptodev-vhost-user")) {
1846 return false;
1848 #endif
1850 /* Reason: vhost-user-blk-server property "node-name" */
1851 if (g_str_equal(type, "vhost-user-blk-server")) {
1852 return false;
1855 * Reason: filter-* property "netdev" etc.
1857 if (g_str_equal(type, "filter-buffer") ||
1858 g_str_equal(type, "filter-dump") ||
1859 g_str_equal(type, "filter-mirror") ||
1860 g_str_equal(type, "filter-redirector") ||
1861 g_str_equal(type, "colo-compare") ||
1862 g_str_equal(type, "filter-rewriter") ||
1863 g_str_equal(type, "filter-replay")) {
1864 return false;
1868 * Allocation of large amounts of memory may delay
1869 * chardev initialization for too long, and trigger timeouts
1870 * on software that waits for a monitor socket to be created
1871 * (e.g. libvirt).
1873 if (g_str_has_prefix(type, "memory-backend-")) {
1874 return false;
1877 return true;
1880 static void qemu_apply_machine_options(QDict *qdict)
1882 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1883 const char *boot_order = NULL;
1884 const char *boot_once = NULL;
1885 QemuOpts *opts;
1887 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
1888 current_machine->ram_size = ram_size;
1889 current_machine->maxram_size = maxram_size;
1890 current_machine->ram_slots = ram_slots;
1892 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1893 if (opts) {
1894 boot_order = qemu_opt_get(opts, "order");
1895 if (boot_order) {
1896 validate_bootdevices(boot_order, &error_fatal);
1899 boot_once = qemu_opt_get(opts, "once");
1900 if (boot_once) {
1901 validate_bootdevices(boot_once, &error_fatal);
1904 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1905 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1908 if (!boot_order) {
1909 boot_order = machine_class->default_boot_order;
1912 current_machine->boot_order = boot_order;
1913 current_machine->boot_once = boot_once;
1915 if (semihosting_enabled() && !semihosting_get_argc()) {
1916 /* fall back to the -kernel/-append */
1917 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
1920 if (current_machine->smp.cpus > 1) {
1921 Error *blocker = NULL;
1922 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1923 replay_add_blocker(blocker);
1927 static void qemu_create_early_backends(void)
1929 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1930 #if defined(CONFIG_SDL)
1931 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1932 #else
1933 const bool use_sdl = false;
1934 #endif
1935 #if defined(CONFIG_GTK)
1936 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1937 #else
1938 const bool use_gtk = false;
1939 #endif
1941 if ((alt_grab || ctrl_grab) && !use_sdl) {
1942 error_report("-alt-grab and -ctrl-grab are only valid "
1943 "for SDL, ignoring option");
1945 if (dpy.has_window_close && !use_gtk && !use_sdl) {
1946 error_report("window-close is only valid for GTK and SDL, "
1947 "ignoring option");
1950 qemu_display_early_init(&dpy);
1951 qemu_console_early_init();
1953 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1954 #if defined(CONFIG_OPENGL)
1955 error_report("OpenGL is not supported by the display");
1956 #else
1957 error_report("OpenGL support is disabled");
1958 #endif
1959 exit(1);
1962 object_option_foreach_add(object_create_early);
1964 /* spice needs the timers to be initialized by this point */
1965 /* spice must initialize before audio as it changes the default audiodev */
1966 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1967 qemu_spice.init();
1969 qemu_opts_foreach(qemu_find_opts("chardev"),
1970 chardev_init_func, NULL, &error_fatal);
1972 #ifdef CONFIG_VIRTFS
1973 qemu_opts_foreach(qemu_find_opts("fsdev"),
1974 fsdev_init_func, NULL, &error_fatal);
1975 #endif
1978 * Note: we need to create audio and block backends before
1979 * setting machine properties, so they can be referred to.
1981 configure_blockdev(&bdo_queue, machine_class, snapshot);
1982 audio_init_audiodevs();
1987 * The remainder of object creation happens after the
1988 * creation of chardev, fsdev, net clients and device data types.
1990 static bool object_create_late(const char *type)
1992 return !object_create_early(type);
1995 static void qemu_create_late_backends(void)
1997 if (qtest_chrdev) {
1998 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
2001 net_init_clients(&error_fatal);
2003 object_option_foreach_add(object_create_late);
2005 if (tpm_init() < 0) {
2006 exit(1);
2009 qemu_opts_foreach(qemu_find_opts("mon"),
2010 mon_init_func, NULL, &error_fatal);
2012 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2013 exit(1);
2014 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2015 exit(1);
2016 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2017 exit(1);
2019 /* now chardevs have been created we may have semihosting to connect */
2020 qemu_semihosting_connect_chardevs();
2021 qemu_semihosting_console_init();
2024 static bool have_custom_ram_size(void)
2026 QemuOpts *opts = qemu_find_opts_singleton("memory");
2027 return !!qemu_opt_get_size(opts, "size", 0);
2030 static void qemu_resolve_machine_memdev(void)
2032 if (current_machine->ram_memdev_id) {
2033 Object *backend;
2034 ram_addr_t backend_size;
2036 backend = object_resolve_path_type(current_machine->ram_memdev_id,
2037 TYPE_MEMORY_BACKEND, NULL);
2038 if (!backend) {
2039 error_report("Memory backend '%s' not found",
2040 current_machine->ram_memdev_id);
2041 exit(EXIT_FAILURE);
2043 backend_size = object_property_get_uint(backend, "size", &error_abort);
2044 if (have_custom_ram_size() && backend_size != ram_size) {
2045 error_report("Size specified by -m option must match size of "
2046 "explicitly specified 'memory-backend' property");
2047 exit(EXIT_FAILURE);
2049 if (mem_path) {
2050 error_report("'-mem-path' can't be used together with"
2051 "'-machine memory-backend'");
2052 exit(EXIT_FAILURE);
2054 ram_size = backend_size;
2057 if (!xen_enabled()) {
2058 /* On 32-bit hosts, QEMU is limited by virtual address space */
2059 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
2060 error_report("at most 2047 MB RAM can be simulated");
2061 exit(1);
2066 static void set_memory_options(MachineClass *mc)
2068 uint64_t sz;
2069 const char *mem_str;
2070 const ram_addr_t default_ram_size = mc->default_ram_size;
2071 QemuOpts *opts = qemu_find_opts_singleton("memory");
2072 Location loc;
2074 loc_push_none(&loc);
2075 qemu_opts_loc_restore(opts);
2077 sz = 0;
2078 mem_str = qemu_opt_get(opts, "size");
2079 if (mem_str) {
2080 if (!*mem_str) {
2081 error_report("missing 'size' option value");
2082 exit(EXIT_FAILURE);
2085 sz = qemu_opt_get_size(opts, "size", ram_size);
2087 /* Fix up legacy suffix-less format */
2088 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2089 uint64_t overflow_check = sz;
2091 sz *= MiB;
2092 if (sz / MiB != overflow_check) {
2093 error_report("too large 'size' option value");
2094 exit(EXIT_FAILURE);
2099 /* backward compatibility behaviour for case "-m 0" */
2100 if (sz == 0) {
2101 sz = default_ram_size;
2104 sz = QEMU_ALIGN_UP(sz, 8192);
2105 if (mc->fixup_ram_size) {
2106 sz = mc->fixup_ram_size(sz);
2108 ram_size = sz;
2109 if (ram_size != sz) {
2110 error_report("ram size too large");
2111 exit(EXIT_FAILURE);
2114 maxram_size = ram_size;
2116 if (qemu_opt_get(opts, "maxmem")) {
2117 uint64_t slots;
2119 sz = qemu_opt_get_size(opts, "maxmem", 0);
2120 slots = qemu_opt_get_number(opts, "slots", 0);
2121 if (sz < ram_size) {
2122 error_report("invalid value of -m option maxmem: "
2123 "maximum memory size (0x%" PRIx64 ") must be at least "
2124 "the initial memory size (0x" RAM_ADDR_FMT ")",
2125 sz, ram_size);
2126 exit(EXIT_FAILURE);
2127 } else if (slots && sz == ram_size) {
2128 error_report("invalid value of -m option maxmem: "
2129 "memory slots were specified but maximum memory size "
2130 "(0x%" PRIx64 ") is equal to the initial memory size "
2131 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2132 exit(EXIT_FAILURE);
2135 maxram_size = sz;
2136 ram_slots = slots;
2137 } else if (qemu_opt_get(opts, "slots")) {
2138 error_report("invalid -m option value: missing 'maxmem' option");
2139 exit(EXIT_FAILURE);
2142 loc_pop(&loc);
2145 static void qemu_create_machine(QDict *qdict)
2147 MachineClass *machine_class = select_machine(qdict, &error_fatal);
2148 object_set_machine_compat_props(machine_class->compat_props);
2150 set_memory_options(machine_class);
2152 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
2153 object_property_add_child(object_get_root(), "machine",
2154 OBJECT(current_machine));
2155 object_property_add_child(container_get(OBJECT(current_machine),
2156 "/unattached"),
2157 "sysbus", OBJECT(sysbus_get_default()));
2159 if (machine_class->minimum_page_bits) {
2160 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2161 /* This would be a board error: specifying a minimum smaller than
2162 * a target's compile-time fixed setting.
2164 g_assert_not_reached();
2168 cpu_exec_init_all();
2169 page_size_init();
2171 if (machine_class->hw_version) {
2172 qemu_set_hw_version(machine_class->hw_version);
2176 * Get the default machine options from the machine if it is not already
2177 * specified either by the configuration file or by the command line.
2179 if (machine_class->default_machine_opts) {
2180 QDict *default_opts =
2181 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2182 &error_abort);
2183 qemu_apply_legacy_machine_options(default_opts);
2184 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2185 false, &error_abort);
2186 qobject_unref(default_opts);
2190 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2192 GlobalProperty *g;
2194 g = g_malloc0(sizeof(*g));
2195 g->driver = qemu_opt_get(opts, "driver");
2196 g->property = qemu_opt_get(opts, "property");
2197 g->value = qemu_opt_get(opts, "value");
2198 qdev_prop_register_global(g);
2199 return 0;
2203 * Return whether configuration group @group is stored in QemuOpts, or
2204 * recorded as one or more QDicts by qemu_record_config_group.
2206 static bool is_qemuopts_group(const char *group)
2208 if (g_str_equal(group, "object") ||
2209 g_str_equal(group, "machine") ||
2210 g_str_equal(group, "smp-opts")) {
2211 return false;
2213 return true;
2216 static void qemu_record_config_group(const char *group, QDict *dict,
2217 bool from_json, Error **errp)
2219 if (g_str_equal(group, "object")) {
2220 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2221 object_option_add_visitor(v);
2222 visit_free(v);
2223 } else if (g_str_equal(group, "machine")) {
2225 * Cannot merge string-valued and type-safe dictionaries, so JSON
2226 * is not accepted yet for -M.
2228 assert(!from_json);
2229 keyval_merge(machine_opts_dict, dict, errp);
2230 } else if (g_str_equal(group, "smp-opts")) {
2231 machine_merge_property("smp", dict, &error_fatal);
2232 } else {
2233 abort();
2238 * Parse non-QemuOpts config file groups, pass the rest to
2239 * qemu_config_do_parse.
2241 static void qemu_parse_config_group(const char *group, QDict *qdict,
2242 void *opaque, Error **errp)
2244 QObject *crumpled;
2245 if (is_qemuopts_group(group)) {
2246 qemu_config_do_parse(group, qdict, opaque, errp);
2247 return;
2250 crumpled = qdict_crumple(qdict, errp);
2251 if (!crumpled) {
2252 return;
2254 switch (qobject_type(crumpled)) {
2255 case QTYPE_QDICT:
2256 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2257 break;
2258 case QTYPE_QLIST:
2259 error_setg(errp, "Lists cannot be at top level of a configuration section");
2260 break;
2261 default:
2262 g_assert_not_reached();
2264 qobject_unref(crumpled);
2267 static void qemu_read_default_config_file(Error **errp)
2269 ERRP_GUARD();
2270 int ret;
2271 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2273 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
2274 if (ret < 0) {
2275 if (ret == -ENOENT) {
2276 error_free(*errp);
2277 *errp = NULL;
2282 static void qemu_set_option(const char *str, Error **errp)
2284 char group[64], id[64], arg[64];
2285 QemuOptsList *list;
2286 QemuOpts *opts;
2287 int rc, offset;
2289 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2290 if (rc < 3 || str[offset] != '=') {
2291 error_setg(errp, "can't parse: \"%s\"", str);
2292 return;
2295 if (!is_qemuopts_group(group)) {
2296 error_setg(errp, "-set is not supported with %s", group);
2297 } else {
2298 list = qemu_find_opts_err(group, errp);
2299 if (list) {
2300 opts = qemu_opts_find(list, id);
2301 if (!opts) {
2302 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2303 return;
2305 qemu_opt_set(opts, arg, str + offset + 1, errp);
2310 static void user_register_global_props(void)
2312 qemu_opts_foreach(qemu_find_opts("global"),
2313 global_init_func, NULL, NULL);
2316 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2318 icount_configure(opts, errp);
2319 return 0;
2322 static int accelerator_set_property(void *opaque,
2323 const char *name, const char *value,
2324 Error **errp)
2326 return object_parse_property_opt(opaque, name, value, "accel", errp);
2329 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2331 bool *p_init_failed = opaque;
2332 const char *acc = qemu_opt_get(opts, "accel");
2333 AccelClass *ac = accel_find(acc);
2334 AccelState *accel;
2335 int ret;
2336 bool qtest_with_kvm;
2338 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2340 if (!ac) {
2341 *p_init_failed = true;
2342 if (!qtest_with_kvm) {
2343 error_report("invalid accelerator %s", acc);
2345 return 0;
2347 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2348 object_apply_compat_props(OBJECT(accel));
2349 qemu_opt_foreach(opts, accelerator_set_property,
2350 accel,
2351 &error_fatal);
2353 ret = accel_init_machine(accel, current_machine);
2354 if (ret < 0) {
2355 *p_init_failed = true;
2356 if (!qtest_with_kvm || ret != -ENOENT) {
2357 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2359 return 0;
2362 return 1;
2365 static void configure_accelerators(const char *progname)
2367 bool init_failed = false;
2369 qemu_opts_foreach(qemu_find_opts("icount"),
2370 do_configure_icount, NULL, &error_fatal);
2372 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2373 char **accel_list, **tmp;
2375 if (accelerators == NULL) {
2376 /* Select the default accelerator */
2377 bool have_tcg = accel_find("tcg");
2378 bool have_kvm = accel_find("kvm");
2380 if (have_tcg && have_kvm) {
2381 if (g_str_has_suffix(progname, "kvm")) {
2382 /* If the program name ends with "kvm", we prefer KVM */
2383 accelerators = "kvm:tcg";
2384 } else {
2385 accelerators = "tcg:kvm";
2387 } else if (have_kvm) {
2388 accelerators = "kvm";
2389 } else if (have_tcg) {
2390 accelerators = "tcg";
2391 } else {
2392 error_report("No accelerator selected and"
2393 " no default accelerator available");
2394 exit(1);
2397 accel_list = g_strsplit(accelerators, ":", 0);
2399 for (tmp = accel_list; *tmp; tmp++) {
2401 * Filter invalid accelerators here, to prevent obscenities
2402 * such as "-machine accel=tcg,,thread=single".
2404 if (accel_find(*tmp)) {
2405 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2406 } else {
2407 init_failed = true;
2408 error_report("invalid accelerator %s", *tmp);
2411 g_strfreev(accel_list);
2412 } else {
2413 if (accelerators != NULL) {
2414 error_report("The -accel and \"-machine accel=\" options are incompatible");
2415 exit(1);
2419 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2420 do_configure_accelerator, &init_failed, &error_fatal)) {
2421 if (!init_failed) {
2422 error_report("no accelerator found");
2424 exit(1);
2427 if (init_failed && !qtest_chrdev) {
2428 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2429 error_report("falling back to %s", ac->name);
2432 if (icount_enabled() && !tcg_enabled()) {
2433 error_report("-icount is not allowed with hardware virtualization");
2434 exit(1);
2438 static void create_default_memdev(MachineState *ms, const char *path)
2440 Object *obj;
2441 MachineClass *mc = MACHINE_GET_CLASS(ms);
2443 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2444 if (path) {
2445 object_property_set_str(obj, "mem-path", path, &error_fatal);
2447 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2448 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2449 obj);
2450 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2451 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2452 false, &error_fatal);
2453 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2454 object_unref(obj);
2455 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2456 &error_fatal);
2459 static void qemu_validate_options(const QDict *machine_opts)
2461 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2462 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2463 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
2465 if (kernel_filename == NULL) {
2466 if (kernel_cmdline != NULL) {
2467 error_report("-append only allowed with -kernel option");
2468 exit(1);
2471 if (initrd_filename != NULL) {
2472 error_report("-initrd only allowed with -kernel option");
2473 exit(1);
2477 if (loadvm && preconfig_requested) {
2478 error_report("'preconfig' and 'loadvm' options are "
2479 "mutually exclusive");
2480 exit(EXIT_FAILURE);
2482 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2483 error_report("'preconfig' supports '-incoming defer' only");
2484 exit(EXIT_FAILURE);
2487 #ifdef CONFIG_CURSES
2488 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2489 error_report("curses display cannot be used with -daemonize");
2490 exit(1);
2492 #endif
2495 static void qemu_process_sugar_options(void)
2497 if (mem_prealloc) {
2498 QObject *smp = qdict_get(machine_opts_dict, "smp");
2499 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2500 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2501 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2502 const char *val = qstring_get_str(qobject_to(QString, cpus));
2503 object_register_sugar_prop("memory-backend", "prealloc-threads",
2504 val, false);
2507 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
2510 if (watchdog) {
2511 int i = select_watchdog(watchdog);
2512 if (i > 0)
2513 exit (i == 1 ? 1 : 0);
2517 /* -action processing */
2520 * Process all the -action parameters parsed from cmdline.
2522 static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2524 Error *local_err = NULL;
2525 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2526 QObject *ret = NULL;
2527 qmp_marshal_set_action(qdict, &ret, &local_err);
2528 qobject_unref(ret);
2529 qobject_unref(qdict);
2530 if (local_err) {
2531 error_propagate(errp, local_err);
2532 return 1;
2534 return 0;
2537 static void qemu_process_early_options(void)
2539 #ifdef CONFIG_SECCOMP
2540 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2541 if (olist) {
2542 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2544 #endif
2546 qemu_opts_foreach(qemu_find_opts("name"),
2547 parse_name, NULL, &error_fatal);
2549 if (qemu_opts_foreach(qemu_find_opts("action"),
2550 process_runstate_actions, NULL, &error_fatal)) {
2551 exit(1);
2554 #ifndef _WIN32
2555 qemu_opts_foreach(qemu_find_opts("add-fd"),
2556 parse_add_fd, NULL, &error_fatal);
2558 qemu_opts_foreach(qemu_find_opts("add-fd"),
2559 cleanup_add_fd, NULL, &error_fatal);
2560 #endif
2562 /* Open the logfile at this point and set the log mask if necessary. */
2563 if (log_file) {
2564 qemu_set_log_filename(log_file, &error_fatal);
2566 if (log_mask) {
2567 int mask;
2568 mask = qemu_str_to_log_mask(log_mask);
2569 if (!mask) {
2570 qemu_print_log_usage(stdout);
2571 exit(1);
2573 qemu_set_log(mask);
2574 } else {
2575 qemu_set_log(0);
2578 qemu_add_default_firmwarepath();
2581 static void qemu_process_help_options(void)
2584 * Check for -cpu help and -device help before we call select_machine(),
2585 * which will return an error if the architecture has no default machine
2586 * type and the user did not specify one, so that the user doesn't need
2587 * to say '-cpu help -machine something'.
2589 if (cpu_option && is_help_option(cpu_option)) {
2590 list_cpus(cpu_option);
2591 exit(0);
2594 if (qemu_opts_foreach(qemu_find_opts("device"),
2595 device_help_func, NULL, NULL)) {
2596 exit(0);
2599 /* -L help lists the data directories and exits. */
2600 if (list_data_dirs) {
2601 qemu_list_data_dirs();
2602 exit(0);
2606 static void qemu_maybe_daemonize(const char *pid_file)
2608 Error *err = NULL;
2610 os_daemonize();
2611 rcu_disable_atfork();
2613 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2614 error_reportf_err(err, "cannot create PID file: ");
2615 exit(1);
2618 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2619 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2622 static void qemu_init_displays(void)
2624 DisplayState *ds;
2626 /* init local displays */
2627 ds = init_displaystate();
2628 qemu_display_init(ds, &dpy);
2630 /* must be after terminal init, SDL library changes signal handlers */
2631 os_setup_signal_handling();
2633 /* init remote displays */
2634 #ifdef CONFIG_VNC
2635 qemu_opts_foreach(qemu_find_opts("vnc"),
2636 vnc_init_func, NULL, &error_fatal);
2637 #endif
2639 if (using_spice) {
2640 qemu_spice.display_init();
2644 static void qemu_init_board(void)
2646 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2648 if (machine_class->default_ram_id && current_machine->ram_size &&
2649 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2650 create_default_memdev(current_machine, mem_path);
2653 /* process plugin before CPUs are created, but once -smp has been parsed */
2654 qemu_plugin_load_list(&plugin_list, &error_fatal);
2656 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2657 machine_run_board_init(current_machine);
2659 drive_check_orphaned();
2661 realtime_init();
2663 if (hax_enabled()) {
2664 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2665 hax_sync_vcpus();
2669 static void qemu_create_cli_devices(void)
2671 DeviceOption *opt;
2673 soundhw_init();
2675 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2676 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2678 /* init USB devices */
2679 if (machine_usb(current_machine)) {
2680 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2681 exit(1);
2684 /* init generic devices */
2685 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2686 qemu_opts_foreach(qemu_find_opts("device"),
2687 device_init_func, NULL, &error_fatal);
2688 QTAILQ_FOREACH(opt, &device_opts, next) {
2689 DeviceState *dev;
2690 loc_push_restore(&opt->loc);
2692 * TODO Eventually we should call qmp_device_add() here to make sure it
2693 * behaves the same, but QMP still has to accept incorrectly typed
2694 * options until libvirt is fixed and we want to be strict on the CLI
2695 * from the start, so call qdev_device_add_from_qdict() directly for
2696 * now.
2698 dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2699 object_unref(OBJECT(dev));
2700 loc_pop(&opt->loc);
2702 rom_reset_order_override();
2705 static void qemu_machine_creation_done(void)
2707 MachineState *machine = MACHINE(qdev_get_machine());
2709 /* Did we create any drives that we failed to create a device for? */
2710 drive_check_orphaned();
2712 /* Don't warn about the default network setup that you get if
2713 * no command line -net or -netdev options are specified. There
2714 * are two cases that we would otherwise complain about:
2715 * (1) board doesn't support a NIC but the implicit "-net nic"
2716 * requested one
2717 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2718 * sets up a nic that isn't connected to anything.
2720 if (!default_net && (!qtest_enabled() || has_defaults)) {
2721 net_check_clients();
2724 qdev_prop_check_globals();
2726 qdev_machine_creation_done();
2728 if (machine->cgs) {
2730 * Verify that Confidential Guest Support has actually been initialized
2732 assert(machine->cgs->ready);
2735 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2736 exit(1);
2740 void qmp_x_exit_preconfig(Error **errp)
2742 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2743 error_setg(errp, "The command is permitted only before machine initialization");
2744 return;
2747 qemu_init_board();
2748 qemu_create_cli_devices();
2749 qemu_machine_creation_done();
2751 if (loadvm) {
2752 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
2754 if (replay_mode != REPLAY_MODE_NONE) {
2755 replay_vmstate_init();
2758 if (incoming) {
2759 Error *local_err = NULL;
2760 if (strcmp(incoming, "defer") != 0) {
2761 qmp_migrate_incoming(incoming, &local_err);
2762 if (local_err) {
2763 error_reportf_err(local_err, "-incoming %s: ", incoming);
2764 exit(1);
2767 } else if (autostart) {
2768 qmp_cont(NULL);
2772 void qemu_init(int argc, char **argv, char **envp)
2774 QemuOpts *opts;
2775 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2776 QemuOptsList *olist;
2777 int optind;
2778 const char *optarg;
2779 MachineClass *machine_class;
2780 bool userconfig = true;
2781 FILE *vmstate_dump_file = NULL;
2783 qemu_add_opts(&qemu_drive_opts);
2784 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2785 qemu_add_drive_opts(&qemu_common_drive_opts);
2786 qemu_add_drive_opts(&qemu_drive_opts);
2787 qemu_add_drive_opts(&bdrv_runtime_opts);
2788 qemu_add_opts(&qemu_chardev_opts);
2789 qemu_add_opts(&qemu_device_opts);
2790 qemu_add_opts(&qemu_netdev_opts);
2791 qemu_add_opts(&qemu_nic_opts);
2792 qemu_add_opts(&qemu_net_opts);
2793 qemu_add_opts(&qemu_rtc_opts);
2794 qemu_add_opts(&qemu_global_opts);
2795 qemu_add_opts(&qemu_mon_opts);
2796 qemu_add_opts(&qemu_trace_opts);
2797 qemu_plugin_add_opts();
2798 qemu_add_opts(&qemu_option_rom_opts);
2799 qemu_add_opts(&qemu_accel_opts);
2800 qemu_add_opts(&qemu_mem_opts);
2801 qemu_add_opts(&qemu_smp_opts);
2802 qemu_add_opts(&qemu_boot_opts);
2803 qemu_add_opts(&qemu_add_fd_opts);
2804 qemu_add_opts(&qemu_object_opts);
2805 qemu_add_opts(&qemu_tpmdev_opts);
2806 qemu_add_opts(&qemu_overcommit_opts);
2807 qemu_add_opts(&qemu_msg_opts);
2808 qemu_add_opts(&qemu_name_opts);
2809 qemu_add_opts(&qemu_numa_opts);
2810 qemu_add_opts(&qemu_icount_opts);
2811 qemu_add_opts(&qemu_semihosting_config_opts);
2812 qemu_add_opts(&qemu_fw_cfg_opts);
2813 qemu_add_opts(&qemu_action_opts);
2814 module_call_init(MODULE_INIT_OPTS);
2816 error_init(argv[0]);
2817 qemu_init_exec_dir(argv[0]);
2819 qemu_init_arch_modules();
2821 qemu_init_subsystems();
2823 /* first pass of option parsing */
2824 optind = 1;
2825 while (optind < argc) {
2826 if (argv[optind][0] != '-') {
2827 /* disk image */
2828 optind++;
2829 } else {
2830 const QEMUOption *popt;
2832 popt = lookup_opt(argc, argv, &optarg, &optind);
2833 switch (popt->index) {
2834 case QEMU_OPTION_nouserconfig:
2835 userconfig = false;
2836 break;
2841 machine_opts_dict = qdict_new();
2842 if (userconfig) {
2843 qemu_read_default_config_file(&error_fatal);
2846 /* second pass of option parsing */
2847 optind = 1;
2848 for(;;) {
2849 if (optind >= argc)
2850 break;
2851 if (argv[optind][0] != '-') {
2852 loc_set_cmdline(argv, optind, 1);
2853 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2854 } else {
2855 const QEMUOption *popt;
2857 popt = lookup_opt(argc, argv, &optarg, &optind);
2858 if (!(popt->arch_mask & arch_type)) {
2859 error_report("Option not supported for this target");
2860 exit(1);
2862 switch(popt->index) {
2863 case QEMU_OPTION_cpu:
2864 /* hw initialization will check this */
2865 cpu_option = optarg;
2866 break;
2867 case QEMU_OPTION_hda:
2868 case QEMU_OPTION_hdb:
2869 case QEMU_OPTION_hdc:
2870 case QEMU_OPTION_hdd:
2871 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2872 HD_OPTS);
2873 break;
2874 case QEMU_OPTION_blockdev:
2876 Visitor *v;
2877 BlockdevOptionsQueueEntry *bdo;
2879 v = qobject_input_visitor_new_str(optarg, "driver",
2880 &error_fatal);
2882 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2883 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2884 &error_fatal);
2885 visit_free(v);
2886 loc_save(&bdo->loc);
2887 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2888 break;
2890 case QEMU_OPTION_drive:
2891 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
2892 optarg, false);
2893 if (opts == NULL) {
2894 exit(1);
2896 break;
2897 case QEMU_OPTION_set:
2898 qemu_set_option(optarg, &error_fatal);
2899 break;
2900 case QEMU_OPTION_global:
2901 if (qemu_global_option(optarg) != 0)
2902 exit(1);
2903 break;
2904 case QEMU_OPTION_mtdblock:
2905 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2906 break;
2907 case QEMU_OPTION_sd:
2908 drive_add(IF_SD, -1, optarg, SD_OPTS);
2909 break;
2910 case QEMU_OPTION_pflash:
2911 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2912 break;
2913 case QEMU_OPTION_snapshot:
2915 Error *blocker = NULL;
2916 snapshot = 1;
2917 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2918 "-snapshot");
2919 replay_add_blocker(blocker);
2921 break;
2922 case QEMU_OPTION_numa:
2923 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2924 optarg, true);
2925 if (!opts) {
2926 exit(1);
2928 break;
2929 case QEMU_OPTION_display:
2930 parse_display(optarg);
2931 break;
2932 case QEMU_OPTION_nographic:
2933 qdict_put_str(machine_opts_dict, "graphics", "off");
2934 nographic = true;
2935 dpy.type = DISPLAY_TYPE_NONE;
2936 break;
2937 case QEMU_OPTION_curses:
2938 warn_report("-curses is deprecated, "
2939 "use -display curses instead.");
2940 #ifdef CONFIG_CURSES
2941 dpy.type = DISPLAY_TYPE_CURSES;
2942 #else
2943 error_report("curses or iconv support is disabled");
2944 exit(1);
2945 #endif
2946 break;
2947 case QEMU_OPTION_portrait:
2948 graphic_rotate = 90;
2949 break;
2950 case QEMU_OPTION_rotate:
2951 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2952 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2953 graphic_rotate != 180 && graphic_rotate != 270) {
2954 error_report("only 90, 180, 270 deg rotation is available");
2955 exit(1);
2957 break;
2958 case QEMU_OPTION_kernel:
2959 qdict_put_str(machine_opts_dict, "kernel", optarg);
2960 break;
2961 case QEMU_OPTION_initrd:
2962 qdict_put_str(machine_opts_dict, "initrd", optarg);
2963 break;
2964 case QEMU_OPTION_append:
2965 qdict_put_str(machine_opts_dict, "append", optarg);
2966 break;
2967 case QEMU_OPTION_dtb:
2968 qdict_put_str(machine_opts_dict, "dtb", optarg);
2969 break;
2970 case QEMU_OPTION_cdrom:
2971 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2972 break;
2973 case QEMU_OPTION_boot:
2974 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2975 optarg, true);
2976 if (!opts) {
2977 exit(1);
2979 break;
2980 case QEMU_OPTION_fda:
2981 case QEMU_OPTION_fdb:
2982 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2983 optarg, FD_OPTS);
2984 break;
2985 case QEMU_OPTION_no_fd_bootchk:
2986 fd_bootchk = 0;
2987 break;
2988 case QEMU_OPTION_netdev:
2989 default_net = 0;
2990 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2991 exit(1);
2993 break;
2994 case QEMU_OPTION_nic:
2995 default_net = 0;
2996 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2997 exit(1);
2999 break;
3000 case QEMU_OPTION_net:
3001 default_net = 0;
3002 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3003 exit(1);
3005 break;
3006 #ifdef CONFIG_LIBISCSI
3007 case QEMU_OPTION_iscsi:
3008 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3009 optarg, false);
3010 if (!opts) {
3011 exit(1);
3013 break;
3014 #endif
3015 case QEMU_OPTION_audio_help:
3016 audio_legacy_help();
3017 exit (0);
3018 break;
3019 case QEMU_OPTION_audiodev:
3020 audio_parse_option(optarg);
3021 break;
3022 case QEMU_OPTION_soundhw:
3023 select_soundhw (optarg);
3024 break;
3025 case QEMU_OPTION_h:
3026 help(0);
3027 break;
3028 case QEMU_OPTION_version:
3029 version();
3030 exit(0);
3031 break;
3032 case QEMU_OPTION_m:
3033 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3034 optarg, true);
3035 if (!opts) {
3036 exit(EXIT_FAILURE);
3038 break;
3039 #ifdef CONFIG_TPM
3040 case QEMU_OPTION_tpmdev:
3041 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3042 exit(1);
3044 break;
3045 #endif
3046 case QEMU_OPTION_mempath:
3047 mem_path = optarg;
3048 break;
3049 case QEMU_OPTION_mem_prealloc:
3050 mem_prealloc = 1;
3051 break;
3052 case QEMU_OPTION_d:
3053 log_mask = optarg;
3054 break;
3055 case QEMU_OPTION_D:
3056 log_file = optarg;
3057 break;
3058 case QEMU_OPTION_DFILTER:
3059 qemu_set_dfilter_ranges(optarg, &error_fatal);
3060 break;
3061 case QEMU_OPTION_seed:
3062 qemu_guest_random_seed_main(optarg, &error_fatal);
3063 break;
3064 case QEMU_OPTION_s:
3065 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3066 break;
3067 case QEMU_OPTION_gdb:
3068 add_device_config(DEV_GDB, optarg);
3069 break;
3070 case QEMU_OPTION_L:
3071 if (is_help_option(optarg)) {
3072 list_data_dirs = true;
3073 } else {
3074 qemu_add_data_dir(g_strdup(optarg));
3076 break;
3077 case QEMU_OPTION_bios:
3078 qdict_put_str(machine_opts_dict, "firmware", optarg);
3079 break;
3080 case QEMU_OPTION_singlestep:
3081 singlestep = 1;
3082 break;
3083 case QEMU_OPTION_S:
3084 autostart = 0;
3085 break;
3086 case QEMU_OPTION_k:
3087 keyboard_layout = optarg;
3088 break;
3089 case QEMU_OPTION_vga:
3090 vga_model = optarg;
3091 default_vga = 0;
3092 break;
3093 case QEMU_OPTION_g:
3095 const char *p;
3096 int w, h, depth;
3097 p = optarg;
3098 w = strtol(p, (char **)&p, 10);
3099 if (w <= 0) {
3100 graphic_error:
3101 error_report("invalid resolution or depth");
3102 exit(1);
3104 if (*p != 'x')
3105 goto graphic_error;
3106 p++;
3107 h = strtol(p, (char **)&p, 10);
3108 if (h <= 0)
3109 goto graphic_error;
3110 if (*p == 'x') {
3111 p++;
3112 depth = strtol(p, (char **)&p, 10);
3113 if (depth != 1 && depth != 2 && depth != 4 &&
3114 depth != 8 && depth != 15 && depth != 16 &&
3115 depth != 24 && depth != 32)
3116 goto graphic_error;
3117 } else if (*p == '\0') {
3118 depth = graphic_depth;
3119 } else {
3120 goto graphic_error;
3123 graphic_width = w;
3124 graphic_height = h;
3125 graphic_depth = depth;
3127 break;
3128 case QEMU_OPTION_echr:
3130 char *r;
3131 term_escape_char = strtol(optarg, &r, 0);
3132 if (r == optarg)
3133 printf("Bad argument to echr\n");
3134 break;
3136 case QEMU_OPTION_monitor:
3137 default_monitor = 0;
3138 if (strncmp(optarg, "none", 4)) {
3139 monitor_parse(optarg, "readline", false);
3141 break;
3142 case QEMU_OPTION_qmp:
3143 monitor_parse(optarg, "control", false);
3144 default_monitor = 0;
3145 break;
3146 case QEMU_OPTION_qmp_pretty:
3147 monitor_parse(optarg, "control", true);
3148 default_monitor = 0;
3149 break;
3150 case QEMU_OPTION_mon:
3151 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3152 true);
3153 if (!opts) {
3154 exit(1);
3156 default_monitor = 0;
3157 break;
3158 case QEMU_OPTION_chardev:
3159 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3160 optarg, true);
3161 if (!opts) {
3162 exit(1);
3164 break;
3165 case QEMU_OPTION_fsdev:
3166 olist = qemu_find_opts("fsdev");
3167 if (!olist) {
3168 error_report("fsdev support is disabled");
3169 exit(1);
3171 opts = qemu_opts_parse_noisily(olist, optarg, true);
3172 if (!opts) {
3173 exit(1);
3175 break;
3176 case QEMU_OPTION_virtfs: {
3177 QemuOpts *fsdev;
3178 QemuOpts *device;
3179 const char *writeout, *sock_fd, *socket, *path, *security_model,
3180 *multidevs;
3182 olist = qemu_find_opts("virtfs");
3183 if (!olist) {
3184 error_report("virtfs support is disabled");
3185 exit(1);
3187 opts = qemu_opts_parse_noisily(olist, optarg, true);
3188 if (!opts) {
3189 exit(1);
3192 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3193 qemu_opt_get(opts, "mount_tag") == NULL) {
3194 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3195 exit(1);
3197 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3198 qemu_opts_id(opts) ?:
3199 qemu_opt_get(opts, "mount_tag"),
3200 1, NULL);
3201 if (!fsdev) {
3202 error_report("duplicate or invalid fsdev id: %s",
3203 qemu_opt_get(opts, "mount_tag"));
3204 exit(1);
3207 writeout = qemu_opt_get(opts, "writeout");
3208 if (writeout) {
3209 #ifdef CONFIG_SYNC_FILE_RANGE
3210 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3211 #else
3212 error_report("writeout=immediate not supported "
3213 "on this platform");
3214 exit(1);
3215 #endif
3217 qemu_opt_set(fsdev, "fsdriver",
3218 qemu_opt_get(opts, "fsdriver"), &error_abort);
3219 path = qemu_opt_get(opts, "path");
3220 if (path) {
3221 qemu_opt_set(fsdev, "path", path, &error_abort);
3223 security_model = qemu_opt_get(opts, "security_model");
3224 if (security_model) {
3225 qemu_opt_set(fsdev, "security_model", security_model,
3226 &error_abort);
3228 socket = qemu_opt_get(opts, "socket");
3229 if (socket) {
3230 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3232 sock_fd = qemu_opt_get(opts, "sock_fd");
3233 if (sock_fd) {
3234 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3237 qemu_opt_set_bool(fsdev, "readonly",
3238 qemu_opt_get_bool(opts, "readonly", 0),
3239 &error_abort);
3240 multidevs = qemu_opt_get(opts, "multidevs");
3241 if (multidevs) {
3242 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3244 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3245 &error_abort);
3246 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3247 qemu_opt_set(device, "fsdev",
3248 qemu_opts_id(fsdev), &error_abort);
3249 qemu_opt_set(device, "mount_tag",
3250 qemu_opt_get(opts, "mount_tag"), &error_abort);
3251 break;
3253 case QEMU_OPTION_serial:
3254 add_device_config(DEV_SERIAL, optarg);
3255 default_serial = 0;
3256 if (strncmp(optarg, "mon:", 4) == 0) {
3257 default_monitor = 0;
3259 break;
3260 case QEMU_OPTION_watchdog:
3261 if (watchdog) {
3262 error_report("only one watchdog option may be given");
3263 exit(1);
3265 warn_report("-watchdog is deprecated; use -device instead.");
3266 watchdog = optarg;
3267 break;
3268 case QEMU_OPTION_action:
3269 olist = qemu_find_opts("action");
3270 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3271 exit(1);
3273 break;
3274 case QEMU_OPTION_watchdog_action: {
3275 QemuOpts *opts;
3276 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3277 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
3278 break;
3280 case QEMU_OPTION_parallel:
3281 add_device_config(DEV_PARALLEL, optarg);
3282 default_parallel = 0;
3283 if (strncmp(optarg, "mon:", 4) == 0) {
3284 default_monitor = 0;
3286 break;
3287 case QEMU_OPTION_debugcon:
3288 add_device_config(DEV_DEBUGCON, optarg);
3289 break;
3290 case QEMU_OPTION_loadvm:
3291 loadvm = optarg;
3292 break;
3293 case QEMU_OPTION_full_screen:
3294 dpy.has_full_screen = true;
3295 dpy.full_screen = true;
3296 break;
3297 case QEMU_OPTION_alt_grab:
3298 alt_grab = 1;
3299 warn_report("-alt-grab is deprecated, please use "
3300 "-display sdl,grab-mod=lshift-lctrl-lalt instead.");
3301 break;
3302 case QEMU_OPTION_ctrl_grab:
3303 ctrl_grab = 1;
3304 warn_report("-ctrl-grab is deprecated, please use "
3305 "-display sdl,grab-mod=rctrl instead.");
3306 break;
3307 case QEMU_OPTION_sdl:
3308 warn_report("-sdl is deprecated, use -display sdl instead.");
3309 #ifdef CONFIG_SDL
3310 dpy.type = DISPLAY_TYPE_SDL;
3311 break;
3312 #else
3313 error_report("SDL support is disabled");
3314 exit(1);
3315 #endif
3316 case QEMU_OPTION_pidfile:
3317 pid_file = optarg;
3318 break;
3319 case QEMU_OPTION_win2k_hack:
3320 win2k_install_hack = 1;
3321 break;
3322 case QEMU_OPTION_acpitable:
3323 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3324 optarg, true);
3325 if (!opts) {
3326 exit(1);
3328 acpi_table_add(opts, &error_fatal);
3329 break;
3330 case QEMU_OPTION_smbios:
3331 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3332 optarg, false);
3333 if (!opts) {
3334 exit(1);
3336 smbios_entry_add(opts, &error_fatal);
3337 break;
3338 case QEMU_OPTION_fwcfg:
3339 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3340 optarg, true);
3341 if (opts == NULL) {
3342 exit(1);
3344 break;
3345 case QEMU_OPTION_preconfig:
3346 preconfig_requested = true;
3347 break;
3348 case QEMU_OPTION_enable_kvm:
3349 qdict_put_str(machine_opts_dict, "accel", "kvm");
3350 break;
3351 case QEMU_OPTION_M:
3352 case QEMU_OPTION_machine:
3354 bool help;
3356 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3357 if (help) {
3358 machine_help_func(machine_opts_dict);
3359 exit(EXIT_SUCCESS);
3361 break;
3363 case QEMU_OPTION_accel:
3364 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3365 optarg, true);
3366 optarg = qemu_opt_get(accel_opts, "accel");
3367 if (!optarg || is_help_option(optarg)) {
3368 printf("Accelerators supported in QEMU binary:\n");
3369 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3370 false);
3371 for (el = accel_list; el; el = el->next) {
3372 gchar *typename = g_strdup(object_class_get_name(
3373 OBJECT_CLASS(el->data)));
3374 /* omit qtest which is used for tests only */
3375 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3376 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3377 gchar **optname = g_strsplit(typename,
3378 ACCEL_CLASS_SUFFIX, 0);
3379 printf("%s\n", optname[0]);
3380 g_strfreev(optname);
3382 g_free(typename);
3384 g_slist_free(accel_list);
3385 exit(0);
3387 break;
3388 case QEMU_OPTION_usb:
3389 qdict_put_str(machine_opts_dict, "usb", "on");
3390 break;
3391 case QEMU_OPTION_usbdevice:
3392 qdict_put_str(machine_opts_dict, "usb", "on");
3393 add_device_config(DEV_USB, optarg);
3394 break;
3395 case QEMU_OPTION_device:
3396 if (optarg[0] == '{') {
3397 QObject *obj = qobject_from_json(optarg, &error_fatal);
3398 DeviceOption *opt = g_new0(DeviceOption, 1);
3399 opt->opts = qobject_to(QDict, obj);
3400 loc_save(&opt->loc);
3401 assert(opt->opts != NULL);
3402 QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3403 } else {
3404 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3405 optarg, true)) {
3406 exit(1);
3409 break;
3410 case QEMU_OPTION_smp:
3411 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3412 "smp", optarg);
3413 break;
3414 case QEMU_OPTION_vnc:
3415 vnc_parse(optarg);
3416 break;
3417 case QEMU_OPTION_no_acpi:
3418 qdict_put_str(machine_opts_dict, "acpi", "off");
3419 break;
3420 case QEMU_OPTION_no_hpet:
3421 qdict_put_str(machine_opts_dict, "hpet", "off");
3422 break;
3423 case QEMU_OPTION_no_reboot:
3424 olist = qemu_find_opts("action");
3425 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
3426 break;
3427 case QEMU_OPTION_no_shutdown:
3428 olist = qemu_find_opts("action");
3429 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
3430 break;
3431 case QEMU_OPTION_uuid:
3432 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3433 error_report("failed to parse UUID string: wrong format");
3434 exit(1);
3436 qemu_uuid_set = true;
3437 break;
3438 case QEMU_OPTION_option_rom:
3439 if (nb_option_roms >= MAX_OPTION_ROMS) {
3440 error_report("too many option ROMs");
3441 exit(1);
3443 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3444 optarg, true);
3445 if (!opts) {
3446 exit(1);
3448 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3449 option_rom[nb_option_roms].bootindex =
3450 qemu_opt_get_number(opts, "bootindex", -1);
3451 if (!option_rom[nb_option_roms].name) {
3452 error_report("Option ROM file is not specified");
3453 exit(1);
3455 nb_option_roms++;
3456 break;
3457 case QEMU_OPTION_semihosting:
3458 qemu_semihosting_enable();
3459 break;
3460 case QEMU_OPTION_semihosting_config:
3461 if (qemu_semihosting_config_options(optarg) != 0) {
3462 exit(1);
3464 break;
3465 case QEMU_OPTION_name:
3466 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3467 optarg, true);
3468 if (!opts) {
3469 exit(1);
3471 /* Capture guest name if -msg guest-name is used later */
3472 error_guest_name = qemu_opt_get(opts, "guest");
3473 break;
3474 case QEMU_OPTION_prom_env:
3475 if (nb_prom_envs >= MAX_PROM_ENVS) {
3476 error_report("too many prom variables");
3477 exit(1);
3479 prom_envs[nb_prom_envs] = optarg;
3480 nb_prom_envs++;
3481 break;
3482 case QEMU_OPTION_old_param:
3483 old_param = 1;
3484 break;
3485 case QEMU_OPTION_rtc:
3486 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3487 false);
3488 if (!opts) {
3489 exit(1);
3491 break;
3492 case QEMU_OPTION_icount:
3493 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3494 optarg, true);
3495 if (!icount_opts) {
3496 exit(1);
3498 break;
3499 case QEMU_OPTION_incoming:
3500 if (!incoming) {
3501 runstate_set(RUN_STATE_INMIGRATE);
3503 incoming = optarg;
3504 break;
3505 case QEMU_OPTION_only_migratable:
3506 only_migratable = 1;
3507 break;
3508 case QEMU_OPTION_nodefaults:
3509 has_defaults = 0;
3510 break;
3511 case QEMU_OPTION_xen_domid:
3512 if (!(accel_find("xen"))) {
3513 error_report("Option not supported for this target");
3514 exit(1);
3516 xen_domid = atoi(optarg);
3517 break;
3518 case QEMU_OPTION_xen_attach:
3519 if (!(accel_find("xen"))) {
3520 error_report("Option not supported for this target");
3521 exit(1);
3523 xen_mode = XEN_ATTACH;
3524 break;
3525 case QEMU_OPTION_xen_domid_restrict:
3526 if (!(accel_find("xen"))) {
3527 error_report("Option not supported for this target");
3528 exit(1);
3530 xen_domid_restrict = true;
3531 break;
3532 case QEMU_OPTION_trace:
3533 trace_opt_parse(optarg);
3534 break;
3535 case QEMU_OPTION_plugin:
3536 qemu_plugin_opt_parse(optarg, &plugin_list);
3537 break;
3538 case QEMU_OPTION_readconfig:
3539 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
3540 break;
3541 #ifdef CONFIG_SPICE
3542 case QEMU_OPTION_spice:
3543 olist = qemu_find_opts_err("spice", NULL);
3544 if (!olist) {
3545 error_report("spice support is disabled");
3546 exit(1);
3548 opts = qemu_opts_parse_noisily(olist, optarg, false);
3549 if (!opts) {
3550 exit(1);
3552 display_remote++;
3553 break;
3554 #endif
3555 case QEMU_OPTION_writeconfig:
3557 FILE *fp;
3558 warn_report("-writeconfig is deprecated and will go away without a replacement");
3559 if (strcmp(optarg, "-") == 0) {
3560 fp = stdout;
3561 } else {
3562 fp = fopen(optarg, "w");
3563 if (fp == NULL) {
3564 error_report("open %s: %s", optarg,
3565 strerror(errno));
3566 exit(1);
3569 qemu_config_write(fp);
3570 if (fp != stdout) {
3571 fclose(fp);
3573 break;
3575 case QEMU_OPTION_qtest:
3576 qtest_chrdev = optarg;
3577 break;
3578 case QEMU_OPTION_qtest_log:
3579 qtest_log = optarg;
3580 break;
3581 case QEMU_OPTION_sandbox:
3582 olist = qemu_find_opts("sandbox");
3583 if (!olist) {
3584 #ifndef CONFIG_SECCOMP
3585 error_report("-sandbox support is not enabled "
3586 "in this QEMU binary");
3587 #endif
3588 exit(1);
3591 opts = qemu_opts_parse_noisily(olist, optarg, true);
3592 if (!opts) {
3593 exit(1);
3595 break;
3596 case QEMU_OPTION_add_fd:
3597 #ifndef _WIN32
3598 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3599 optarg, false);
3600 if (!opts) {
3601 exit(1);
3603 #else
3604 error_report("File descriptor passing is disabled on this "
3605 "platform");
3606 exit(1);
3607 #endif
3608 break;
3609 case QEMU_OPTION_object:
3610 object_option_parse(optarg);
3611 break;
3612 case QEMU_OPTION_overcommit:
3613 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3614 optarg, false);
3615 if (!opts) {
3616 exit(1);
3618 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
3619 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3620 break;
3621 case QEMU_OPTION_compat:
3623 CompatPolicy *opts;
3624 Visitor *v;
3626 v = qobject_input_visitor_new_str(optarg, NULL,
3627 &error_fatal);
3629 visit_type_CompatPolicy(v, NULL, &opts, &error_fatal);
3630 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts);
3632 qapi_free_CompatPolicy(opts);
3633 visit_free(v);
3634 break;
3636 case QEMU_OPTION_msg:
3637 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3638 false);
3639 if (!opts) {
3640 exit(1);
3642 configure_msg(opts);
3643 break;
3644 case QEMU_OPTION_dump_vmstate:
3645 if (vmstate_dump_file) {
3646 error_report("only one '-dump-vmstate' "
3647 "option may be given");
3648 exit(1);
3650 vmstate_dump_file = fopen(optarg, "w");
3651 if (vmstate_dump_file == NULL) {
3652 error_report("open %s: %s", optarg, strerror(errno));
3653 exit(1);
3655 break;
3656 case QEMU_OPTION_enable_sync_profile:
3657 qsp_enable();
3658 break;
3659 case QEMU_OPTION_nouserconfig:
3660 /* Nothing to be parsed here. Especially, do not error out below. */
3661 break;
3662 default:
3663 if (os_parse_cmd_args(popt->index, optarg)) {
3664 error_report("Option not supported in this build");
3665 exit(1);
3671 * Clear error location left behind by the loop.
3672 * Best done right after the loop. Do not insert code here!
3674 loc_set_none();
3676 qemu_validate_options(machine_opts_dict);
3677 qemu_process_sugar_options();
3680 * These options affect everything else and should be processed
3681 * before daemonizing.
3683 qemu_process_early_options();
3685 qemu_process_help_options();
3686 qemu_maybe_daemonize(pid_file);
3689 * The trace backend must be initialized after daemonizing.
3690 * trace_init_backends() will call st_init(), which will create the
3691 * trace thread in the parent, and also register st_flush_trace_buffer()
3692 * in atexit(). This function will force the parent to wait for the
3693 * writeout thread to finish, which will not occur, and the parent
3694 * process will be left in the host.
3696 if (!trace_init_backends()) {
3697 exit(1);
3699 trace_init_file();
3701 qemu_init_main_loop(&error_fatal);
3702 cpu_timers_init();
3704 user_register_global_props();
3705 replay_configure(icount_opts);
3707 configure_rtc(qemu_find_opts_singleton("rtc"));
3709 qemu_create_machine(machine_opts_dict);
3711 suspend_mux_open();
3713 qemu_disable_default_devices();
3714 qemu_create_default_devices();
3715 qemu_create_early_backends();
3717 qemu_apply_legacy_machine_options(machine_opts_dict);
3718 qemu_apply_machine_options(machine_opts_dict);
3719 qobject_unref(machine_opts_dict);
3720 phase_advance(PHASE_MACHINE_CREATED);
3723 * Note: uses machine properties such as kernel-irqchip, must run
3724 * after qemu_apply_machine_options.
3726 configure_accelerators(argv[0]);
3727 phase_advance(PHASE_ACCEL_CREATED);
3730 * Beware, QOM objects created before this point miss global and
3731 * compat properties.
3733 * Global properties get set up by qdev_prop_register_global(),
3734 * called from user_register_global_props(), and certain option
3735 * desugaring. Also in CPU feature desugaring (buried in
3736 * parse_cpu_option()), which happens below this point, but may
3737 * only target the CPU type, which can only be created after
3738 * parse_cpu_option() returned the type.
3740 * Machine compat properties: object_set_machine_compat_props().
3741 * Accelerator compat props: object_set_accelerator_compat_props(),
3742 * called from do_configure_accelerator().
3745 machine_class = MACHINE_GET_CLASS(current_machine);
3746 if (!qtest_enabled() && machine_class->deprecation_reason) {
3747 error_report("Machine type '%s' is deprecated: %s",
3748 machine_class->name, machine_class->deprecation_reason);
3752 * Note: creates a QOM object, must run only after global and
3753 * compat properties have been set up.
3755 migration_object_init();
3757 qemu_create_late_backends();
3759 /* parse features once if machine provides default cpu_type */
3760 current_machine->cpu_type = machine_class->default_cpu_type;
3761 if (cpu_option) {
3762 current_machine->cpu_type = parse_cpu_option(cpu_option);
3764 /* NB: for machine none cpu_type could STILL be NULL here! */
3766 qemu_resolve_machine_memdev();
3767 parse_numa_opts(current_machine);
3769 if (vmstate_dump_file) {
3770 /* dump and exit */
3771 module_load_qom_all();
3772 dump_vmstate_json_to_file(vmstate_dump_file);
3773 exit(0);
3776 if (!preconfig_requested) {
3777 qmp_x_exit_preconfig(&error_fatal);
3779 qemu_init_displays();
3780 accel_setup_post(current_machine);
3781 os_setup_post();
3782 resume_mux_open();