qmp: generalize watchdog-set-action to -no-reboot/-no-shutdown
[qemu/ar7.git] / softmmu / vl.c
blob04d94da84375889a254e12039789c63885f4ec15
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/datadir.h"
28 #include "qemu/units.h"
29 #include "exec/cpu-common.h"
30 #include "hw/boards.h"
31 #include "hw/qdev-properties.h"
32 #include "qapi/error.h"
33 #include "qemu-version.h"
34 #include "qemu/cutils.h"
35 #include "qemu/help_option.h"
36 #include "qemu/uuid.h"
37 #include "sysemu/reset.h"
38 #include "sysemu/runstate.h"
39 #include "sysemu/runstate-action.h"
40 #include "sysemu/seccomp.h"
41 #include "sysemu/tcg.h"
42 #include "sysemu/xen.h"
44 #include "qemu/error-report.h"
45 #include "qemu/sockets.h"
46 #include "sysemu/accel.h"
47 #include "hw/usb.h"
48 #include "hw/isa/isa.h"
49 #include "hw/scsi/scsi.h"
50 #include "hw/display/vga.h"
51 #include "sysemu/watchdog.h"
52 #include "hw/firmware/smbios.h"
53 #include "hw/acpi/acpi.h"
54 #include "hw/xen/xen.h"
55 #include "hw/loader.h"
56 #include "monitor/qdev.h"
57 #include "net/net.h"
58 #include "net/slirp.h"
59 #include "monitor/monitor.h"
60 #include "ui/console.h"
61 #include "ui/input.h"
62 #include "sysemu/sysemu.h"
63 #include "sysemu/numa.h"
64 #include "sysemu/hostmem.h"
65 #include "exec/gdbstub.h"
66 #include "qemu/timer.h"
67 #include "chardev/char.h"
68 #include "qemu/bitmap.h"
69 #include "qemu/log.h"
70 #include "sysemu/blockdev.h"
71 #include "hw/block/block.h"
72 #include "hw/i386/x86.h"
73 #include "hw/i386/pc.h"
74 #include "migration/misc.h"
75 #include "migration/snapshot.h"
76 #include "sysemu/tpm.h"
77 #include "sysemu/dma.h"
78 #include "hw/audio/soundhw.h"
79 #include "audio/audio.h"
80 #include "sysemu/cpus.h"
81 #include "sysemu/cpu-timers.h"
82 #include "migration/colo.h"
83 #include "migration/postcopy-ram.h"
84 #include "sysemu/kvm.h"
85 #include "sysemu/hax.h"
86 #include "qapi/qobject-input-visitor.h"
87 #include "qemu/option.h"
88 #include "qemu/config-file.h"
89 #include "qemu-options.h"
90 #include "qemu/main-loop.h"
91 #ifdef CONFIG_VIRTFS
92 #include "fsdev/qemu-fsdev.h"
93 #endif
94 #include "sysemu/qtest.h"
96 #include "disas/disas.h"
98 #include "trace.h"
99 #include "trace/control.h"
100 #include "qemu/plugin.h"
101 #include "qemu/queue.h"
102 #include "sysemu/arch_init.h"
104 #include "ui/qemu-spice.h"
105 #include "qapi/string-input-visitor.h"
106 #include "qapi/opts-visitor.h"
107 #include "qapi/clone-visitor.h"
108 #include "qom/object_interfaces.h"
109 #include "hw/semihosting/semihost.h"
110 #include "crypto/init.h"
111 #include "sysemu/replay.h"
112 #include "qapi/qapi-events-run-state.h"
113 #include "qapi/qapi-visit-block-core.h"
114 #include "qapi/qapi-visit-ui.h"
115 #include "qapi/qapi-commands-block-core.h"
116 #include "qapi/qapi-commands-migration.h"
117 #include "qapi/qapi-commands-misc.h"
118 #include "qapi/qapi-commands-ui.h"
119 #include "qapi/qmp/qerror.h"
120 #include "sysemu/iothread.h"
121 #include "qemu/guest-random.h"
123 #define MAX_VIRTIO_CONSOLES 1
125 typedef struct BlockdevOptionsQueueEntry {
126 BlockdevOptions *bdo;
127 Location loc;
128 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
129 } BlockdevOptionsQueueEntry;
131 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
133 static const char *cpu_option;
134 static const char *mem_path;
135 static const char *incoming;
136 static const char *loadvm;
137 static ram_addr_t maxram_size;
138 static uint64_t ram_slots;
139 static int display_remote;
140 static int snapshot;
141 static bool preconfig_requested;
142 static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
143 static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
144 static bool nographic = false;
145 static int mem_prealloc; /* force preallocation of physical target memory */
146 static ram_addr_t ram_size;
147 static const char *vga_model = NULL;
148 static DisplayOptions dpy;
149 static int num_serial_hds;
150 static Chardev **serial_hds;
151 static const char *log_mask;
152 static const char *log_file;
153 static bool list_data_dirs;
154 static const char *watchdog;
155 static const char *qtest_chrdev;
156 static const char *qtest_log;
158 static int has_defaults = 1;
159 static int default_serial = 1;
160 static int default_parallel = 1;
161 static int default_monitor = 1;
162 static int default_floppy = 1;
163 static int default_cdrom = 1;
164 static int default_sdcard = 1;
165 static int default_vga = 1;
166 static int default_net = 1;
168 static struct {
169 const char *driver;
170 int *flag;
171 } default_list[] = {
172 { .driver = "isa-serial", .flag = &default_serial },
173 { .driver = "isa-parallel", .flag = &default_parallel },
174 { .driver = "isa-fdc", .flag = &default_floppy },
175 { .driver = "floppy", .flag = &default_floppy },
176 { .driver = "ide-cd", .flag = &default_cdrom },
177 { .driver = "ide-hd", .flag = &default_cdrom },
178 { .driver = "ide-drive", .flag = &default_cdrom },
179 { .driver = "scsi-cd", .flag = &default_cdrom },
180 { .driver = "scsi-hd", .flag = &default_cdrom },
181 { .driver = "VGA", .flag = &default_vga },
182 { .driver = "isa-vga", .flag = &default_vga },
183 { .driver = "cirrus-vga", .flag = &default_vga },
184 { .driver = "isa-cirrus-vga", .flag = &default_vga },
185 { .driver = "vmware-svga", .flag = &default_vga },
186 { .driver = "qxl-vga", .flag = &default_vga },
187 { .driver = "virtio-vga", .flag = &default_vga },
188 { .driver = "ati-vga", .flag = &default_vga },
189 { .driver = "vhost-user-vga", .flag = &default_vga },
192 static QemuOptsList qemu_rtc_opts = {
193 .name = "rtc",
194 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
195 .merge_lists = true,
196 .desc = {
198 .name = "base",
199 .type = QEMU_OPT_STRING,
201 .name = "clock",
202 .type = QEMU_OPT_STRING,
204 .name = "driftfix",
205 .type = QEMU_OPT_STRING,
207 { /* end of list */ }
211 static QemuOptsList qemu_option_rom_opts = {
212 .name = "option-rom",
213 .implied_opt_name = "romfile",
214 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
215 .desc = {
217 .name = "bootindex",
218 .type = QEMU_OPT_NUMBER,
219 }, {
220 .name = "romfile",
221 .type = QEMU_OPT_STRING,
223 { /* end of list */ }
227 static QemuOptsList qemu_machine_opts = {
228 .name = "machine",
229 .implied_opt_name = "type",
230 .merge_lists = true,
231 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
232 .desc = {
234 * no elements => accept any
235 * sanity checking will happen later
236 * when setting machine properties
242 static QemuOptsList qemu_accel_opts = {
243 .name = "accel",
244 .implied_opt_name = "accel",
245 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
246 .desc = {
248 * no elements => accept any
249 * sanity checking will happen later
250 * when setting accelerator properties
256 static QemuOptsList qemu_boot_opts = {
257 .name = "boot-opts",
258 .implied_opt_name = "order",
259 .merge_lists = true,
260 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
261 .desc = {
263 .name = "order",
264 .type = QEMU_OPT_STRING,
265 }, {
266 .name = "once",
267 .type = QEMU_OPT_STRING,
268 }, {
269 .name = "menu",
270 .type = QEMU_OPT_BOOL,
271 }, {
272 .name = "splash",
273 .type = QEMU_OPT_STRING,
274 }, {
275 .name = "splash-time",
276 .type = QEMU_OPT_NUMBER,
277 }, {
278 .name = "reboot-timeout",
279 .type = QEMU_OPT_NUMBER,
280 }, {
281 .name = "strict",
282 .type = QEMU_OPT_BOOL,
284 { /*End of list */ }
288 static QemuOptsList qemu_add_fd_opts = {
289 .name = "add-fd",
290 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
291 .desc = {
293 .name = "fd",
294 .type = QEMU_OPT_NUMBER,
295 .help = "file descriptor of which a duplicate is added to fd set",
297 .name = "set",
298 .type = QEMU_OPT_NUMBER,
299 .help = "ID of the fd set to add fd to",
301 .name = "opaque",
302 .type = QEMU_OPT_STRING,
303 .help = "free-form string used to describe fd",
305 { /* end of list */ }
309 static QemuOptsList qemu_object_opts = {
310 .name = "object",
311 .implied_opt_name = "qom-type",
312 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
313 .desc = {
318 static QemuOptsList qemu_tpmdev_opts = {
319 .name = "tpmdev",
320 .implied_opt_name = "type",
321 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
322 .desc = {
323 /* options are defined in the TPM backends */
324 { /* end of list */ }
328 static QemuOptsList qemu_realtime_opts = {
329 .name = "realtime",
330 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
331 .desc = {
333 .name = "mlock",
334 .type = QEMU_OPT_BOOL,
336 { /* end of list */ }
340 static QemuOptsList qemu_overcommit_opts = {
341 .name = "overcommit",
342 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
343 .desc = {
345 .name = "mem-lock",
346 .type = QEMU_OPT_BOOL,
349 .name = "cpu-pm",
350 .type = QEMU_OPT_BOOL,
352 { /* end of list */ }
356 static QemuOptsList qemu_msg_opts = {
357 .name = "msg",
358 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
359 .desc = {
361 .name = "timestamp",
362 .type = QEMU_OPT_BOOL,
365 .name = "guest-name",
366 .type = QEMU_OPT_BOOL,
367 .help = "Prepends guest name for error messages but only if "
368 "-name guest is set otherwise option is ignored\n",
370 { /* end of list */ }
374 static QemuOptsList qemu_name_opts = {
375 .name = "name",
376 .implied_opt_name = "guest",
377 .merge_lists = true,
378 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
379 .desc = {
381 .name = "guest",
382 .type = QEMU_OPT_STRING,
383 .help = "Sets the name of the guest.\n"
384 "This name will be displayed in the SDL window caption.\n"
385 "The name will also be used for the VNC server",
386 }, {
387 .name = "process",
388 .type = QEMU_OPT_STRING,
389 .help = "Sets the name of the QEMU process, as shown in top etc",
390 }, {
391 .name = "debug-threads",
392 .type = QEMU_OPT_BOOL,
393 .help = "When enabled, name the individual threads; defaults off.\n"
394 "NOTE: The thread names are for debugging and not a\n"
395 "stable API.",
397 { /* End of list */ }
401 static QemuOptsList qemu_mem_opts = {
402 .name = "memory",
403 .implied_opt_name = "size",
404 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
405 .merge_lists = true,
406 .desc = {
408 .name = "size",
409 .type = QEMU_OPT_SIZE,
412 .name = "slots",
413 .type = QEMU_OPT_NUMBER,
416 .name = "maxmem",
417 .type = QEMU_OPT_SIZE,
419 { /* end of list */ }
423 static QemuOptsList qemu_icount_opts = {
424 .name = "icount",
425 .implied_opt_name = "shift",
426 .merge_lists = true,
427 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
428 .desc = {
430 .name = "shift",
431 .type = QEMU_OPT_STRING,
432 }, {
433 .name = "align",
434 .type = QEMU_OPT_BOOL,
435 }, {
436 .name = "sleep",
437 .type = QEMU_OPT_BOOL,
438 }, {
439 .name = "rr",
440 .type = QEMU_OPT_STRING,
441 }, {
442 .name = "rrfile",
443 .type = QEMU_OPT_STRING,
444 }, {
445 .name = "rrsnapshot",
446 .type = QEMU_OPT_STRING,
448 { /* end of list */ }
452 static QemuOptsList qemu_fw_cfg_opts = {
453 .name = "fw_cfg",
454 .implied_opt_name = "name",
455 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
456 .desc = {
458 .name = "name",
459 .type = QEMU_OPT_STRING,
460 .help = "Sets the fw_cfg name of the blob to be inserted",
461 }, {
462 .name = "file",
463 .type = QEMU_OPT_STRING,
464 .help = "Sets the name of the file from which "
465 "the fw_cfg blob will be loaded",
466 }, {
467 .name = "string",
468 .type = QEMU_OPT_STRING,
469 .help = "Sets content of the blob to be inserted from a string",
470 }, {
471 .name = "gen_id",
472 .type = QEMU_OPT_STRING,
473 .help = "Sets id of the object generating the fw_cfg blob "
474 "to be inserted",
476 { /* end of list */ }
481 * Get machine options
483 * Returns: machine options (never null).
485 static QemuOpts *qemu_get_machine_opts(void)
487 return qemu_find_opts_singleton("machine");
490 const char *qemu_get_vm_name(void)
492 return qemu_name;
495 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
497 const char *driver = qemu_opt_get(opts, "driver");
498 int i;
500 if (!driver)
501 return 0;
502 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
503 if (strcmp(default_list[i].driver, driver) != 0)
504 continue;
505 *(default_list[i].flag) = 0;
507 return 0;
510 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
512 const char *proc_name;
514 if (qemu_opt_get(opts, "debug-threads")) {
515 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
517 qemu_name = qemu_opt_get(opts, "guest");
519 proc_name = qemu_opt_get(opts, "process");
520 if (proc_name) {
521 os_set_proc_name(proc_name);
524 return 0;
527 bool defaults_enabled(void)
529 return has_defaults;
532 #ifndef _WIN32
533 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
535 int fd, dupfd, flags;
536 int64_t fdset_id;
537 const char *fd_opaque = NULL;
538 AddfdInfo *fdinfo;
540 fd = qemu_opt_get_number(opts, "fd", -1);
541 fdset_id = qemu_opt_get_number(opts, "set", -1);
542 fd_opaque = qemu_opt_get(opts, "opaque");
544 if (fd < 0) {
545 error_setg(errp, "fd option is required and must be non-negative");
546 return -1;
549 if (fd <= STDERR_FILENO) {
550 error_setg(errp, "fd cannot be a standard I/O stream");
551 return -1;
555 * All fds inherited across exec() necessarily have FD_CLOEXEC
556 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
558 flags = fcntl(fd, F_GETFD);
559 if (flags == -1 || (flags & FD_CLOEXEC)) {
560 error_setg(errp, "fd is not valid or already in use");
561 return -1;
564 if (fdset_id < 0) {
565 error_setg(errp, "set option is required and must be non-negative");
566 return -1;
569 #ifdef F_DUPFD_CLOEXEC
570 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
571 #else
572 dupfd = dup(fd);
573 if (dupfd != -1) {
574 qemu_set_cloexec(dupfd);
576 #endif
577 if (dupfd == -1) {
578 error_setg(errp, "error duplicating fd: %s", strerror(errno));
579 return -1;
582 /* add the duplicate fd, and optionally the opaque string, to the fd set */
583 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
584 &error_abort);
585 g_free(fdinfo);
587 return 0;
590 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
592 int fd;
594 fd = qemu_opt_get_number(opts, "fd", -1);
595 close(fd);
597 return 0;
599 #endif
601 /***********************************************************/
602 /* QEMU Block devices */
604 #define HD_OPTS "media=disk"
605 #define CDROM_OPTS "media=cdrom"
606 #define FD_OPTS ""
607 #define PFLASH_OPTS ""
608 #define MTD_OPTS ""
609 #define SD_OPTS ""
611 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
613 BlockInterfaceType *block_default_type = opaque;
615 return drive_new(opts, *block_default_type, errp) == NULL;
618 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
620 if (qemu_opt_get(opts, "snapshot") == NULL) {
621 qemu_opt_set(opts, "snapshot", "on", &error_abort);
623 return 0;
626 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
627 int index, const char *optstr)
629 QemuOpts *opts;
630 DriveInfo *dinfo;
632 if (!enable || drive_get_by_index(type, index)) {
633 return;
636 opts = drive_add(type, index, NULL, optstr);
637 if (snapshot) {
638 drive_enable_snapshot(NULL, opts, NULL);
641 dinfo = drive_new(opts, type, &error_abort);
642 dinfo->is_default = true;
646 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
647 MachineClass *machine_class, int snapshot)
650 * If the currently selected machine wishes to override the
651 * units-per-bus property of its default HBA interface type, do so
652 * now.
654 if (machine_class->units_per_default_bus) {
655 override_max_devs(machine_class->block_default_type,
656 machine_class->units_per_default_bus);
659 /* open the virtual block devices */
660 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
661 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
663 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
664 loc_push_restore(&bdo->loc);
665 qmp_blockdev_add(bdo->bdo, &error_fatal);
666 loc_pop(&bdo->loc);
667 qapi_free_BlockdevOptions(bdo->bdo);
668 g_free(bdo);
670 if (snapshot) {
671 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
672 NULL, NULL);
674 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
675 &machine_class->block_default_type, &error_fatal)) {
676 /* We printed help */
677 exit(0);
680 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
681 CDROM_OPTS);
682 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
683 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
687 static QemuOptsList qemu_smp_opts = {
688 .name = "smp-opts",
689 .implied_opt_name = "cpus",
690 .merge_lists = true,
691 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
692 .desc = {
694 .name = "cpus",
695 .type = QEMU_OPT_NUMBER,
696 }, {
697 .name = "sockets",
698 .type = QEMU_OPT_NUMBER,
699 }, {
700 .name = "dies",
701 .type = QEMU_OPT_NUMBER,
702 }, {
703 .name = "cores",
704 .type = QEMU_OPT_NUMBER,
705 }, {
706 .name = "threads",
707 .type = QEMU_OPT_NUMBER,
708 }, {
709 .name = "maxcpus",
710 .type = QEMU_OPT_NUMBER,
712 { /*End of list */ }
716 static void realtime_init(void)
718 if (enable_mlock) {
719 if (os_mlock() < 0) {
720 error_report("locking memory failed");
721 exit(1);
727 static void configure_msg(QemuOpts *opts)
729 error_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
730 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
734 /***********************************************************/
735 /* USB devices */
737 static int usb_device_add(const char *devname)
739 USBDevice *dev = NULL;
741 if (!machine_usb(current_machine)) {
742 return -1;
745 dev = usbdevice_create(devname);
746 if (!dev)
747 return -1;
749 return 0;
752 static int usb_parse(const char *cmdline)
754 int r;
755 r = usb_device_add(cmdline);
756 if (r < 0) {
757 error_report("could not add USB device '%s'", cmdline);
759 return r;
762 /***********************************************************/
763 /* machine registration */
765 static MachineClass *find_machine(const char *name, GSList *machines)
767 GSList *el;
769 for (el = machines; el; el = el->next) {
770 MachineClass *mc = el->data;
772 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
773 return mc;
777 return NULL;
780 static MachineClass *find_default_machine(GSList *machines)
782 GSList *el;
783 MachineClass *default_machineclass = NULL;
785 for (el = machines; el; el = el->next) {
786 MachineClass *mc = el->data;
788 if (mc->is_default) {
789 assert(default_machineclass == NULL && "Multiple default machines");
790 default_machineclass = mc;
794 return default_machineclass;
797 static int machine_help_func(QemuOpts *opts, MachineState *machine)
799 ObjectProperty *prop;
800 ObjectPropertyIterator iter;
802 if (!qemu_opt_has_help_opt(opts)) {
803 return 0;
806 object_property_iter_init(&iter, OBJECT(machine));
807 while ((prop = object_property_iter_next(&iter))) {
808 if (!prop->set) {
809 continue;
812 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
813 prop->name, prop->type);
814 if (prop->description) {
815 printf(" (%s)\n", prop->description);
816 } else {
817 printf("\n");
821 return 1;
824 static void version(void)
826 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
827 QEMU_COPYRIGHT "\n");
830 static void help(int exitcode)
832 version();
833 printf("usage: %s [options] [disk_image]\n\n"
834 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
835 error_get_progname());
837 #define QEMU_OPTIONS_GENERATE_HELP
838 #include "qemu-options-wrapper.h"
840 printf("\nDuring emulation, the following keys are useful:\n"
841 "ctrl-alt-f toggle full screen\n"
842 "ctrl-alt-n switch to virtual console 'n'\n"
843 "ctrl-alt toggle mouse and keyboard grab\n"
844 "\n"
845 "When using -nographic, press 'ctrl-a h' to get some help.\n"
846 "\n"
847 QEMU_HELP_BOTTOM "\n");
849 exit(exitcode);
852 #define HAS_ARG 0x0001
854 typedef struct QEMUOption {
855 const char *name;
856 int flags;
857 int index;
858 uint32_t arch_mask;
859 } QEMUOption;
861 static const QEMUOption qemu_options[] = {
862 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
863 #define QEMU_OPTIONS_GENERATE_OPTIONS
864 #include "qemu-options-wrapper.h"
865 { NULL },
868 typedef struct VGAInterfaceInfo {
869 const char *opt_name; /* option name */
870 const char *name; /* human-readable name */
871 /* Class names indicating that support is available.
872 * If no class is specified, the interface is always available */
873 const char *class_names[2];
874 } VGAInterfaceInfo;
876 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
877 [VGA_NONE] = {
878 .opt_name = "none",
879 .name = "no graphic card",
881 [VGA_STD] = {
882 .opt_name = "std",
883 .name = "standard VGA",
884 .class_names = { "VGA", "isa-vga" },
886 [VGA_CIRRUS] = {
887 .opt_name = "cirrus",
888 .name = "Cirrus VGA",
889 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
891 [VGA_VMWARE] = {
892 .opt_name = "vmware",
893 .name = "VMWare SVGA",
894 .class_names = { "vmware-svga" },
896 [VGA_VIRTIO] = {
897 .opt_name = "virtio",
898 .name = "Virtio VGA",
899 .class_names = { "virtio-vga" },
901 [VGA_QXL] = {
902 .opt_name = "qxl",
903 .name = "QXL VGA",
904 .class_names = { "qxl-vga" },
906 [VGA_TCX] = {
907 .opt_name = "tcx",
908 .name = "TCX framebuffer",
909 .class_names = { "SUNW,tcx" },
911 [VGA_CG3] = {
912 .opt_name = "cg3",
913 .name = "CG3 framebuffer",
914 .class_names = { "cgthree" },
916 [VGA_XENFB] = {
917 .opt_name = "xenfb",
918 .name = "Xen paravirtualized framebuffer",
922 static bool vga_interface_available(VGAInterfaceType t)
924 const VGAInterfaceInfo *ti = &vga_interfaces[t];
926 assert(t < VGA_TYPE_MAX);
927 return !ti->class_names[0] ||
928 module_object_class_by_name(ti->class_names[0]) ||
929 module_object_class_by_name(ti->class_names[1]);
932 static const char *
933 get_default_vga_model(const MachineClass *machine_class)
935 if (machine_class->default_display) {
936 return machine_class->default_display;
937 } else if (vga_interface_available(VGA_CIRRUS)) {
938 return "cirrus";
939 } else if (vga_interface_available(VGA_STD)) {
940 return "std";
943 return NULL;
946 static void select_vgahw(const MachineClass *machine_class, const char *p)
948 const char *opts;
949 int t;
951 if (g_str_equal(p, "help")) {
952 const char *def = get_default_vga_model(machine_class);
954 for (t = 0; t < VGA_TYPE_MAX; t++) {
955 const VGAInterfaceInfo *ti = &vga_interfaces[t];
957 if (vga_interface_available(t) && ti->opt_name) {
958 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
959 g_str_equal(ti->opt_name, def) ? " (default)" : "");
962 exit(0);
965 assert(vga_interface_type == VGA_NONE);
966 for (t = 0; t < VGA_TYPE_MAX; t++) {
967 const VGAInterfaceInfo *ti = &vga_interfaces[t];
968 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
969 if (!vga_interface_available(t)) {
970 error_report("%s not available", ti->name);
971 exit(1);
973 vga_interface_type = t;
974 break;
977 if (t == VGA_TYPE_MAX) {
978 invalid_vga:
979 error_report("unknown vga type: %s", p);
980 exit(1);
982 while (*opts) {
983 const char *nextopt;
985 if (strstart(opts, ",retrace=", &nextopt)) {
986 opts = nextopt;
987 if (strstart(opts, "dumb", &nextopt))
988 vga_retrace_method = VGA_RETRACE_DUMB;
989 else if (strstart(opts, "precise", &nextopt))
990 vga_retrace_method = VGA_RETRACE_PRECISE;
991 else goto invalid_vga;
992 } else goto invalid_vga;
993 opts = nextopt;
997 static void parse_display_qapi(const char *optarg)
999 DisplayOptions *opts;
1000 Visitor *v;
1002 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
1004 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1005 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1007 qapi_free_DisplayOptions(opts);
1008 visit_free(v);
1011 DisplayOptions *qmp_query_display_options(Error **errp)
1013 return QAPI_CLONE(DisplayOptions, &dpy);
1016 static void parse_display(const char *p)
1018 const char *opts;
1020 if (is_help_option(p)) {
1021 qemu_display_help();
1022 exit(0);
1025 if (strstart(p, "sdl", &opts)) {
1027 * sdl DisplayType needs hand-crafted parser instead of
1028 * parse_display_qapi() due to some options not in
1029 * DisplayOptions, specifically:
1030 * - frame
1031 * Already deprecated.
1032 * - ctrl_grab + alt_grab
1033 * Not clear yet what happens to them long-term. Should
1034 * replaced by something better or deprecated and dropped.
1036 dpy.type = DISPLAY_TYPE_SDL;
1037 while (*opts) {
1038 const char *nextopt;
1040 if (strstart(opts, ",alt_grab=", &nextopt)) {
1041 opts = nextopt;
1042 if (strstart(opts, "on", &nextopt)) {
1043 alt_grab = 1;
1044 } else if (strstart(opts, "off", &nextopt)) {
1045 alt_grab = 0;
1046 } else {
1047 goto invalid_sdl_args;
1049 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1050 opts = nextopt;
1051 if (strstart(opts, "on", &nextopt)) {
1052 ctrl_grab = 1;
1053 } else if (strstart(opts, "off", &nextopt)) {
1054 ctrl_grab = 0;
1055 } else {
1056 goto invalid_sdl_args;
1058 } else if (strstart(opts, ",window_close=", &nextopt)) {
1059 opts = nextopt;
1060 dpy.has_window_close = true;
1061 if (strstart(opts, "on", &nextopt)) {
1062 dpy.window_close = true;
1063 } else if (strstart(opts, "off", &nextopt)) {
1064 dpy.window_close = false;
1065 } else {
1066 goto invalid_sdl_args;
1068 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1069 opts = nextopt;
1070 dpy.has_show_cursor = true;
1071 if (strstart(opts, "on", &nextopt)) {
1072 dpy.show_cursor = true;
1073 } else if (strstart(opts, "off", &nextopt)) {
1074 dpy.show_cursor = false;
1075 } else {
1076 goto invalid_sdl_args;
1078 } else if (strstart(opts, ",gl=", &nextopt)) {
1079 opts = nextopt;
1080 dpy.has_gl = true;
1081 if (strstart(opts, "on", &nextopt)) {
1082 dpy.gl = DISPLAYGL_MODE_ON;
1083 } else if (strstart(opts, "core", &nextopt)) {
1084 dpy.gl = DISPLAYGL_MODE_CORE;
1085 } else if (strstart(opts, "es", &nextopt)) {
1086 dpy.gl = DISPLAYGL_MODE_ES;
1087 } else if (strstart(opts, "off", &nextopt)) {
1088 dpy.gl = DISPLAYGL_MODE_OFF;
1089 } else {
1090 goto invalid_sdl_args;
1092 } else {
1093 invalid_sdl_args:
1094 error_report("invalid SDL option string");
1095 exit(1);
1097 opts = nextopt;
1099 } else if (strstart(p, "vnc", &opts)) {
1101 * vnc isn't a (local) DisplayType but a protocol for remote
1102 * display access.
1104 if (*opts == '=') {
1105 vnc_parse(opts + 1, &error_fatal);
1106 } else {
1107 error_report("VNC requires a display argument vnc=<display>");
1108 exit(1);
1110 } else {
1111 parse_display_qapi(p);
1115 static inline bool nonempty_str(const char *str)
1117 return str && *str;
1120 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1122 gchar *buf;
1123 size_t size;
1124 const char *name, *file, *str, *gen_id;
1125 FWCfgState *fw_cfg = (FWCfgState *) opaque;
1127 if (fw_cfg == NULL) {
1128 error_setg(errp, "fw_cfg device not available");
1129 return -1;
1131 name = qemu_opt_get(opts, "name");
1132 file = qemu_opt_get(opts, "file");
1133 str = qemu_opt_get(opts, "string");
1134 gen_id = qemu_opt_get(opts, "gen_id");
1136 /* we need the name, and exactly one of: file, content string, gen_id */
1137 if (!nonempty_str(name) ||
1138 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1139 error_setg(errp, "name, plus exactly one of file,"
1140 " string and gen_id, are needed");
1141 return -1;
1143 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
1144 error_setg(errp, "name too long (max. %d char)",
1145 FW_CFG_MAX_FILE_PATH - 1);
1146 return -1;
1148 if (nonempty_str(gen_id)) {
1150 * In this particular case where the content is populated
1151 * internally, the "etc/" namespace protection is relaxed,
1152 * so do not emit a warning.
1154 } else if (strncmp(name, "opt/", 4) != 0) {
1155 warn_report("externally provided fw_cfg item names "
1156 "should be prefixed with \"opt/\"");
1158 if (nonempty_str(str)) {
1159 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1160 buf = g_memdup(str, size);
1161 } else if (nonempty_str(gen_id)) {
1162 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
1163 return -1;
1165 return 0;
1166 } else {
1167 GError *err = NULL;
1168 if (!g_file_get_contents(file, &buf, &size, &err)) {
1169 error_setg(errp, "can't load %s: %s", file, err->message);
1170 g_error_free(err);
1171 return -1;
1174 /* For legacy, keep user files in a specific global order. */
1175 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1176 fw_cfg_add_file(fw_cfg, name, buf, size);
1177 fw_cfg_reset_order_override(fw_cfg);
1178 return 0;
1181 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
1183 return qdev_device_help(opts);
1186 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
1188 DeviceState *dev;
1190 dev = qdev_device_add(opts, errp);
1191 if (!dev && *errp) {
1192 error_report_err(*errp);
1193 return -1;
1194 } else if (dev) {
1195 object_unref(OBJECT(dev));
1197 return 0;
1200 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1202 Error *local_err = NULL;
1204 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
1205 if (local_err) {
1206 error_propagate(errp, local_err);
1207 return -1;
1209 exit(0);
1211 return 0;
1214 #ifdef CONFIG_VIRTFS
1215 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1217 return qemu_fsdev_add(opts, errp);
1219 #endif
1221 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
1223 return monitor_init_opts(opts, errp);
1226 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
1228 static int monitor_device_index = 0;
1229 QemuOpts *opts;
1230 const char *p;
1231 char label[32];
1233 if (strstart(optarg, "chardev:", &p)) {
1234 snprintf(label, sizeof(label), "%s", p);
1235 } else {
1236 snprintf(label, sizeof(label), "compat_monitor%d",
1237 monitor_device_index);
1238 opts = qemu_chr_parse_compat(label, optarg, true);
1239 if (!opts) {
1240 error_report("parse error: %s", optarg);
1241 exit(1);
1245 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
1246 qemu_opt_set(opts, "mode", mode, &error_abort);
1247 qemu_opt_set(opts, "chardev", label, &error_abort);
1248 if (!strcmp(mode, "control")) {
1249 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1250 } else {
1251 assert(pretty == false);
1253 monitor_device_index++;
1256 struct device_config {
1257 enum {
1258 DEV_USB, /* -usbdevice */
1259 DEV_SERIAL, /* -serial */
1260 DEV_PARALLEL, /* -parallel */
1261 DEV_DEBUGCON, /* -debugcon */
1262 DEV_GDB, /* -gdb, -s */
1263 DEV_SCLP, /* s390 sclp */
1264 } type;
1265 const char *cmdline;
1266 Location loc;
1267 QTAILQ_ENTRY(device_config) next;
1270 static QTAILQ_HEAD(, device_config) device_configs =
1271 QTAILQ_HEAD_INITIALIZER(device_configs);
1273 static void add_device_config(int type, const char *cmdline)
1275 struct device_config *conf;
1277 conf = g_malloc0(sizeof(*conf));
1278 conf->type = type;
1279 conf->cmdline = cmdline;
1280 loc_save(&conf->loc);
1281 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1284 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1286 struct device_config *conf;
1287 int rc;
1289 QTAILQ_FOREACH(conf, &device_configs, next) {
1290 if (conf->type != type)
1291 continue;
1292 loc_push_restore(&conf->loc);
1293 rc = func(conf->cmdline);
1294 loc_pop(&conf->loc);
1295 if (rc) {
1296 return rc;
1299 return 0;
1302 static void qemu_disable_default_devices(void)
1304 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1306 qemu_opts_foreach(qemu_find_opts("device"),
1307 default_driver_check, NULL, NULL);
1308 qemu_opts_foreach(qemu_find_opts("global"),
1309 default_driver_check, NULL, NULL);
1311 if (!vga_model && !default_vga) {
1312 vga_interface_type = VGA_DEVICE;
1314 if (!has_defaults || machine_class->no_serial) {
1315 default_serial = 0;
1317 if (!has_defaults || machine_class->no_parallel) {
1318 default_parallel = 0;
1320 if (!has_defaults || machine_class->no_floppy) {
1321 default_floppy = 0;
1323 if (!has_defaults || machine_class->no_cdrom) {
1324 default_cdrom = 0;
1326 if (!has_defaults || machine_class->no_sdcard) {
1327 default_sdcard = 0;
1329 if (!has_defaults) {
1330 default_monitor = 0;
1331 default_net = 0;
1332 default_vga = 0;
1336 static void qemu_create_default_devices(void)
1338 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1340 if (is_daemonized()) {
1341 /* According to documentation and historically, -nographic redirects
1342 * serial port, parallel port and monitor to stdio, which does not work
1343 * with -daemonize. We can redirect these to null instead, but since
1344 * -nographic is legacy, let's just error out.
1345 * We disallow -nographic only if all other ports are not redirected
1346 * explicitly, to not break existing legacy setups which uses
1347 * -nographic _and_ redirects all ports explicitly - this is valid
1348 * usage, -nographic is just a no-op in this case.
1350 if (nographic
1351 && (default_parallel || default_serial || default_monitor)) {
1352 error_report("-nographic cannot be used with -daemonize");
1353 exit(1);
1357 if (nographic) {
1358 if (default_parallel)
1359 add_device_config(DEV_PARALLEL, "null");
1360 if (default_serial && default_monitor) {
1361 add_device_config(DEV_SERIAL, "mon:stdio");
1362 } else {
1363 if (default_serial)
1364 add_device_config(DEV_SERIAL, "stdio");
1365 if (default_monitor)
1366 monitor_parse("stdio", "readline", false);
1368 } else {
1369 if (default_serial)
1370 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1371 if (default_parallel)
1372 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1373 if (default_monitor)
1374 monitor_parse("vc:80Cx24C", "readline", false);
1377 if (default_net) {
1378 QemuOptsList *net = qemu_find_opts("net");
1379 qemu_opts_parse(net, "nic", true, &error_abort);
1380 #ifdef CONFIG_SLIRP
1381 qemu_opts_parse(net, "user", true, &error_abort);
1382 #endif
1385 #if defined(CONFIG_VNC)
1386 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1387 display_remote++;
1389 #endif
1390 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1391 if (!qemu_display_find_default(&dpy)) {
1392 dpy.type = DISPLAY_TYPE_NONE;
1393 #if defined(CONFIG_VNC)
1394 vnc_parse("localhost:0,to=99,id=default", &error_abort);
1395 #endif
1398 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1399 dpy.type = DISPLAY_TYPE_NONE;
1402 /* If no default VGA is requested, the default is "none". */
1403 if (default_vga) {
1404 vga_model = get_default_vga_model(machine_class);
1406 if (vga_model) {
1407 select_vgahw(machine_class, vga_model);
1411 static int serial_parse(const char *devname)
1413 int index = num_serial_hds;
1414 char label[32];
1416 if (strcmp(devname, "none") == 0)
1417 return 0;
1418 snprintf(label, sizeof(label), "serial%d", index);
1419 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1421 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1422 if (!serial_hds[index]) {
1423 error_report("could not connect serial device"
1424 " to character backend '%s'", devname);
1425 return -1;
1427 num_serial_hds++;
1428 return 0;
1431 Chardev *serial_hd(int i)
1433 assert(i >= 0);
1434 if (i < num_serial_hds) {
1435 return serial_hds[i];
1437 return NULL;
1440 static int parallel_parse(const char *devname)
1442 static int index = 0;
1443 char label[32];
1445 if (strcmp(devname, "none") == 0)
1446 return 0;
1447 if (index == MAX_PARALLEL_PORTS) {
1448 error_report("too many parallel ports");
1449 exit(1);
1451 snprintf(label, sizeof(label), "parallel%d", index);
1452 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
1453 if (!parallel_hds[index]) {
1454 error_report("could not connect parallel device"
1455 " to character backend '%s'", devname);
1456 return -1;
1458 index++;
1459 return 0;
1462 static int debugcon_parse(const char *devname)
1464 QemuOpts *opts;
1466 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
1467 error_report("invalid character backend '%s'", devname);
1468 exit(1);
1470 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
1471 if (!opts) {
1472 error_report("already have a debugcon device");
1473 exit(1);
1475 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1476 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
1477 return 0;
1480 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1482 const MachineClass *mc1 = a, *mc2 = b;
1483 int res;
1485 if (mc1->family == NULL) {
1486 if (mc2->family == NULL) {
1487 /* Compare standalone machine types against each other; they sort
1488 * in increasing order.
1490 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1491 object_class_get_name(OBJECT_CLASS(mc2)));
1494 /* Standalone machine types sort after families. */
1495 return 1;
1498 if (mc2->family == NULL) {
1499 /* Families sort before standalone machine types. */
1500 return -1;
1503 /* Families sort between each other alphabetically increasingly. */
1504 res = strcmp(mc1->family, mc2->family);
1505 if (res != 0) {
1506 return res;
1509 /* Within the same family, machine types sort in decreasing order. */
1510 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1511 object_class_get_name(OBJECT_CLASS(mc1)));
1514 static MachineClass *machine_parse(const char *name, GSList *machines)
1516 MachineClass *mc;
1517 GSList *el;
1519 if (is_help_option(name)) {
1520 printf("Supported machines are:\n");
1521 machines = g_slist_sort(machines, machine_class_cmp);
1522 for (el = machines; el; el = el->next) {
1523 MachineClass *mc = el->data;
1524 if (mc->alias) {
1525 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1527 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1528 mc->is_default ? " (default)" : "",
1529 mc->deprecation_reason ? " (deprecated)" : "");
1531 exit(0);
1534 mc = find_machine(name, machines);
1535 if (!mc) {
1536 error_report("unsupported machine type");
1537 error_printf("Use -machine help to list supported machines\n");
1538 exit(1);
1540 return mc;
1543 static const char *pid_file;
1544 static Notifier qemu_unlink_pidfile_notifier;
1546 static void qemu_unlink_pidfile(Notifier *n, void *data)
1548 if (pid_file) {
1549 unlink(pid_file);
1553 static const QEMUOption *lookup_opt(int argc, char **argv,
1554 const char **poptarg, int *poptind)
1556 const QEMUOption *popt;
1557 int optind = *poptind;
1558 char *r = argv[optind];
1559 const char *optarg;
1561 loc_set_cmdline(argv, optind, 1);
1562 optind++;
1563 /* Treat --foo the same as -foo. */
1564 if (r[1] == '-')
1565 r++;
1566 popt = qemu_options;
1567 for(;;) {
1568 if (!popt->name) {
1569 error_report("invalid option");
1570 exit(1);
1572 if (!strcmp(popt->name, r + 1))
1573 break;
1574 popt++;
1576 if (popt->flags & HAS_ARG) {
1577 if (optind >= argc) {
1578 error_report("requires an argument");
1579 exit(1);
1581 optarg = argv[optind++];
1582 loc_set_cmdline(argv, optind - 2, 2);
1583 } else {
1584 optarg = NULL;
1587 *poptarg = optarg;
1588 *poptind = optind;
1590 return popt;
1593 static MachineClass *select_machine(void)
1595 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
1596 MachineClass *machine_class = find_default_machine(machines);
1597 const char *optarg;
1598 QemuOpts *opts;
1599 Location loc;
1601 loc_push_none(&loc);
1603 opts = qemu_get_machine_opts();
1604 qemu_opts_loc_restore(opts);
1606 optarg = qemu_opt_get(opts, "type");
1607 if (optarg) {
1608 machine_class = machine_parse(optarg, machines);
1611 if (!machine_class) {
1612 error_report("No machine specified, and there is no default");
1613 error_printf("Use -machine help to list supported machines\n");
1614 exit(1);
1617 loc_pop(&loc);
1618 g_slist_free(machines);
1619 return machine_class;
1622 static int object_parse_property_opt(Object *obj,
1623 const char *name, const char *value,
1624 const char *skip, Error **errp)
1626 if (g_str_equal(name, skip)) {
1627 return 0;
1630 if (!object_property_parse(obj, name, value, errp)) {
1631 return -1;
1634 return 0;
1637 static int machine_set_property(void *opaque,
1638 const char *name, const char *value,
1639 Error **errp)
1641 g_autofree char *qom_name = g_strdup(name);
1642 char *p;
1644 for (p = qom_name; *p; p++) {
1645 if (*p == '_') {
1646 *p = '-';
1650 /* Legacy options do not correspond to MachineState properties. */
1651 if (g_str_equal(qom_name, "accel")) {
1652 return 0;
1654 if (g_str_equal(qom_name, "igd-passthru")) {
1655 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value);
1656 return 0;
1658 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
1659 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value);
1660 return 0;
1662 if (g_str_equal(qom_name, "kernel-irqchip")) {
1663 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value);
1664 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value);
1665 return 0;
1668 return object_parse_property_opt(opaque, name, value, "type", errp);
1672 * Initial object creation happens before all other
1673 * QEMU data types are created. The majority of objects
1674 * can be created at this point. The rng-egd object
1675 * cannot be created here, as it depends on the chardev
1676 * already existing.
1678 static bool object_create_early(const char *type, QemuOpts *opts)
1680 if (user_creatable_print_help(type, opts)) {
1681 exit(0);
1685 * Objects should not be made "delayed" without a reason. If you
1686 * add one, state the reason in a comment!
1689 /* Reason: rng-egd property "chardev" */
1690 if (g_str_equal(type, "rng-egd")) {
1691 return false;
1694 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1695 /* Reason: cryptodev-vhost-user property "chardev" */
1696 if (g_str_equal(type, "cryptodev-vhost-user")) {
1697 return false;
1699 #endif
1701 /* Reason: vhost-user-blk-server property "node-name" */
1702 if (g_str_equal(type, "vhost-user-blk-server")) {
1703 return false;
1706 * Reason: filter-* property "netdev" etc.
1708 if (g_str_equal(type, "filter-buffer") ||
1709 g_str_equal(type, "filter-dump") ||
1710 g_str_equal(type, "filter-mirror") ||
1711 g_str_equal(type, "filter-redirector") ||
1712 g_str_equal(type, "colo-compare") ||
1713 g_str_equal(type, "filter-rewriter") ||
1714 g_str_equal(type, "filter-replay")) {
1715 return false;
1718 /* Allocation of large amounts of memory may delay
1719 * chardev initialization for too long, and trigger timeouts
1720 * on software that waits for a monitor socket to be created
1721 * (e.g. libvirt).
1723 if (g_str_has_prefix(type, "memory-backend-")) {
1724 return false;
1727 return true;
1730 static void qemu_apply_machine_options(void)
1732 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1733 QemuOpts *machine_opts = qemu_get_machine_opts();
1734 const char *boot_order = NULL;
1735 const char *boot_once = NULL;
1736 QemuOpts *opts;
1738 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
1739 &error_fatal);
1740 current_machine->ram_size = ram_size;
1741 current_machine->maxram_size = maxram_size;
1742 current_machine->ram_slots = ram_slots;
1744 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
1745 if (opts) {
1746 boot_order = qemu_opt_get(opts, "order");
1747 if (boot_order) {
1748 validate_bootdevices(boot_order, &error_fatal);
1751 boot_once = qemu_opt_get(opts, "once");
1752 if (boot_once) {
1753 validate_bootdevices(boot_once, &error_fatal);
1756 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
1757 boot_strict = qemu_opt_get_bool(opts, "strict", false);
1760 if (!boot_order) {
1761 boot_order = machine_class->default_boot_order;
1764 current_machine->boot_order = boot_order;
1765 current_machine->boot_once = boot_once;
1767 if (semihosting_enabled() && !semihosting_get_argc()) {
1768 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
1769 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append") ?: "";
1770 /* fall back to the -kernel/-append */
1771 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
1775 static void qemu_create_early_backends(void)
1777 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1779 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
1780 error_report("-alt-grab and -ctrl-grab are only valid "
1781 "for SDL, ignoring option");
1783 if (dpy.has_window_close &&
1784 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
1785 error_report("-no-quit is only valid for GTK and SDL, "
1786 "ignoring option");
1789 qemu_display_early_init(&dpy);
1790 qemu_console_early_init();
1792 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1793 #if defined(CONFIG_OPENGL)
1794 error_report("OpenGL is not supported by the display");
1795 #else
1796 error_report("OpenGL support is disabled");
1797 #endif
1798 exit(1);
1801 qemu_opts_foreach(qemu_find_opts("object"),
1802 user_creatable_add_opts_foreach,
1803 object_create_early, &error_fatal);
1805 /* spice needs the timers to be initialized by this point */
1806 /* spice must initialize before audio as it changes the default auiodev */
1807 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1808 qemu_spice.init();
1810 qemu_opts_foreach(qemu_find_opts("chardev"),
1811 chardev_init_func, NULL, &error_fatal);
1813 #ifdef CONFIG_VIRTFS
1814 qemu_opts_foreach(qemu_find_opts("fsdev"),
1815 fsdev_init_func, NULL, &error_fatal);
1816 #endif
1819 * Note: we need to create audio and block backends before
1820 * machine_set_property(), so machine properties can refer to
1821 * them.
1823 configure_blockdev(&bdo_queue, machine_class, snapshot);
1824 audio_init_audiodevs();
1829 * The remainder of object creation happens after the
1830 * creation of chardev, fsdev, net clients and device data types.
1832 static bool object_create_late(const char *type, QemuOpts *opts)
1834 return !object_create_early(type, opts);
1837 static void qemu_create_late_backends(void)
1839 if (qtest_chrdev) {
1840 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1843 net_init_clients(&error_fatal);
1845 qemu_opts_foreach(qemu_find_opts("object"),
1846 user_creatable_add_opts_foreach,
1847 object_create_late, &error_fatal);
1849 if (tpm_init() < 0) {
1850 exit(1);
1853 qemu_opts_foreach(qemu_find_opts("mon"),
1854 mon_init_func, NULL, &error_fatal);
1856 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1857 exit(1);
1858 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1859 exit(1);
1860 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
1861 exit(1);
1863 /* now chardevs have been created we may have semihosting to connect */
1864 qemu_semihosting_connect_chardevs();
1865 qemu_semihosting_console_init();
1868 static bool have_custom_ram_size(void)
1870 QemuOpts *opts = qemu_find_opts_singleton("memory");
1871 return !!qemu_opt_get_size(opts, "size", 0);
1874 static void qemu_resolve_machine_memdev(void)
1876 if (current_machine->ram_memdev_id) {
1877 Object *backend;
1878 ram_addr_t backend_size;
1880 backend = object_resolve_path_type(current_machine->ram_memdev_id,
1881 TYPE_MEMORY_BACKEND, NULL);
1882 if (!backend) {
1883 error_report("Memory backend '%s' not found",
1884 current_machine->ram_memdev_id);
1885 exit(EXIT_FAILURE);
1887 backend_size = object_property_get_uint(backend, "size", &error_abort);
1888 if (have_custom_ram_size() && backend_size != ram_size) {
1889 error_report("Size specified by -m option must match size of "
1890 "explicitly specified 'memory-backend' property");
1891 exit(EXIT_FAILURE);
1893 if (mem_path) {
1894 error_report("'-mem-path' can't be used together with"
1895 "'-machine memory-backend'");
1896 exit(EXIT_FAILURE);
1898 ram_size = backend_size;
1901 if (!xen_enabled()) {
1902 /* On 32-bit hosts, QEMU is limited by virtual address space */
1903 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1904 error_report("at most 2047 MB RAM can be simulated");
1905 exit(1);
1910 static void set_memory_options(MachineClass *mc)
1912 uint64_t sz;
1913 const char *mem_str;
1914 const ram_addr_t default_ram_size = mc->default_ram_size;
1915 QemuOpts *opts = qemu_find_opts_singleton("memory");
1916 Location loc;
1918 loc_push_none(&loc);
1919 qemu_opts_loc_restore(opts);
1921 sz = 0;
1922 mem_str = qemu_opt_get(opts, "size");
1923 if (mem_str) {
1924 if (!*mem_str) {
1925 error_report("missing 'size' option value");
1926 exit(EXIT_FAILURE);
1929 sz = qemu_opt_get_size(opts, "size", ram_size);
1931 /* Fix up legacy suffix-less format */
1932 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
1933 uint64_t overflow_check = sz;
1935 sz *= MiB;
1936 if (sz / MiB != overflow_check) {
1937 error_report("too large 'size' option value");
1938 exit(EXIT_FAILURE);
1943 /* backward compatibility behaviour for case "-m 0" */
1944 if (sz == 0) {
1945 sz = default_ram_size;
1948 sz = QEMU_ALIGN_UP(sz, 8192);
1949 if (mc->fixup_ram_size) {
1950 sz = mc->fixup_ram_size(sz);
1952 ram_size = sz;
1953 if (ram_size != sz) {
1954 error_report("ram size too large");
1955 exit(EXIT_FAILURE);
1958 /* store value for the future use */
1959 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
1960 maxram_size = ram_size;
1962 if (qemu_opt_get(opts, "maxmem")) {
1963 uint64_t slots;
1965 sz = qemu_opt_get_size(opts, "maxmem", 0);
1966 slots = qemu_opt_get_number(opts, "slots", 0);
1967 if (sz < ram_size) {
1968 error_report("invalid value of -m option maxmem: "
1969 "maximum memory size (0x%" PRIx64 ") must be at least "
1970 "the initial memory size (0x" RAM_ADDR_FMT ")",
1971 sz, ram_size);
1972 exit(EXIT_FAILURE);
1973 } else if (slots && sz == ram_size) {
1974 error_report("invalid value of -m option maxmem: "
1975 "memory slots were specified but maximum memory size "
1976 "(0x%" PRIx64 ") is equal to the initial memory size "
1977 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
1978 exit(EXIT_FAILURE);
1981 maxram_size = sz;
1982 ram_slots = slots;
1983 } else if (qemu_opt_get(opts, "slots")) {
1984 error_report("invalid -m option value: missing 'maxmem' option");
1985 exit(EXIT_FAILURE);
1988 loc_pop(&loc);
1991 static void qemu_create_machine(MachineClass *machine_class)
1993 object_set_machine_compat_props(machine_class->compat_props);
1995 set_memory_options(machine_class);
1997 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
1998 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
1999 exit(0);
2001 object_property_add_child(object_get_root(), "machine",
2002 OBJECT(current_machine));
2003 object_property_add_child(container_get(OBJECT(current_machine),
2004 "/unattached"),
2005 "sysbus", OBJECT(sysbus_get_default()));
2007 if (machine_class->minimum_page_bits) {
2008 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2009 /* This would be a board error: specifying a minimum smaller than
2010 * a target's compile-time fixed setting.
2012 g_assert_not_reached();
2016 cpu_exec_init_all();
2017 page_size_init();
2019 if (machine_class->hw_version) {
2020 qemu_set_hw_version(machine_class->hw_version);
2023 machine_smp_parse(current_machine,
2024 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
2027 * Get the default machine options from the machine if it is not already
2028 * specified either by the configuration file or by the command line.
2030 if (machine_class->default_machine_opts) {
2031 qemu_opts_set_defaults(qemu_find_opts("machine"),
2032 machine_class->default_machine_opts, 0);
2036 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2038 GlobalProperty *g;
2040 g = g_malloc0(sizeof(*g));
2041 g->driver = qemu_opt_get(opts, "driver");
2042 g->property = qemu_opt_get(opts, "property");
2043 g->value = qemu_opt_get(opts, "value");
2044 qdev_prop_register_global(g);
2045 return 0;
2048 static int qemu_read_default_config_file(void)
2050 int ret;
2051 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
2053 ret = qemu_read_config_file(file);
2054 if (ret < 0 && ret != -ENOENT) {
2055 return ret;
2058 return 0;
2061 static int qemu_set_option(const char *str)
2063 Error *local_err = NULL;
2064 char group[64], id[64], arg[64];
2065 QemuOptsList *list;
2066 QemuOpts *opts;
2067 int rc, offset;
2069 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2070 if (rc < 3 || str[offset] != '=') {
2071 error_report("can't parse: \"%s\"", str);
2072 return -1;
2075 list = qemu_find_opts(group);
2076 if (list == NULL) {
2077 return -1;
2080 opts = qemu_opts_find(list, id);
2081 if (!opts) {
2082 error_report("there is no %s \"%s\" defined",
2083 list->name, id);
2084 return -1;
2087 if (!qemu_opt_set(opts, arg, str + offset + 1, &local_err)) {
2088 error_report_err(local_err);
2089 return -1;
2091 return 0;
2094 static void user_register_global_props(void)
2096 qemu_opts_foreach(qemu_find_opts("global"),
2097 global_init_func, NULL, NULL);
2100 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2102 icount_configure(opts, errp);
2103 return 0;
2106 static int accelerator_set_property(void *opaque,
2107 const char *name, const char *value,
2108 Error **errp)
2110 return object_parse_property_opt(opaque, name, value, "accel", errp);
2113 static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2115 bool *p_init_failed = opaque;
2116 const char *acc = qemu_opt_get(opts, "accel");
2117 AccelClass *ac = accel_find(acc);
2118 AccelState *accel;
2119 int ret;
2120 bool qtest_with_kvm;
2122 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
2124 if (!ac) {
2125 *p_init_failed = true;
2126 if (!qtest_with_kvm) {
2127 error_report("invalid accelerator %s", acc);
2129 return 0;
2131 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
2132 object_apply_compat_props(OBJECT(accel));
2133 qemu_opt_foreach(opts, accelerator_set_property,
2134 accel,
2135 &error_fatal);
2137 ret = accel_init_machine(accel, current_machine);
2138 if (ret < 0) {
2139 *p_init_failed = true;
2140 if (!qtest_with_kvm || ret != -ENOENT) {
2141 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2143 return 0;
2146 return 1;
2149 static void configure_accelerators(const char *progname)
2151 const char *accel;
2152 bool init_failed = false;
2154 qemu_opts_foreach(qemu_find_opts("icount"),
2155 do_configure_icount, NULL, &error_fatal);
2157 accel = qemu_opt_get(qemu_get_machine_opts(), "accel");
2158 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
2159 char **accel_list, **tmp;
2161 if (accel == NULL) {
2162 /* Select the default accelerator */
2163 bool have_tcg = accel_find("tcg");
2164 bool have_kvm = accel_find("kvm");
2166 if (have_tcg && have_kvm) {
2167 if (g_str_has_suffix(progname, "kvm")) {
2168 /* If the program name ends with "kvm", we prefer KVM */
2169 accel = "kvm:tcg";
2170 } else {
2171 accel = "tcg:kvm";
2173 } else if (have_kvm) {
2174 accel = "kvm";
2175 } else if (have_tcg) {
2176 accel = "tcg";
2177 } else {
2178 error_report("No accelerator selected and"
2179 " no default accelerator available");
2180 exit(1);
2183 accel_list = g_strsplit(accel, ":", 0);
2185 for (tmp = accel_list; *tmp; tmp++) {
2187 * Filter invalid accelerators here, to prevent obscenities
2188 * such as "-machine accel=tcg,,thread=single".
2190 if (accel_find(*tmp)) {
2191 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
2192 } else {
2193 init_failed = true;
2194 error_report("invalid accelerator %s", *tmp);
2197 g_strfreev(accel_list);
2198 } else {
2199 if (accel != NULL) {
2200 error_report("The -accel and \"-machine accel=\" options are incompatible");
2201 exit(1);
2205 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2206 do_configure_accelerator, &init_failed, &error_fatal)) {
2207 if (!init_failed) {
2208 error_report("no accelerator found");
2210 exit(1);
2213 if (init_failed && !qtest_chrdev) {
2214 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
2215 error_report("falling back to %s", ac->name);
2218 if (icount_enabled() && !tcg_enabled()) {
2219 error_report("-icount is not allowed with hardware virtualization");
2220 exit(1);
2224 static void create_default_memdev(MachineState *ms, const char *path)
2226 Object *obj;
2227 MachineClass *mc = MACHINE_GET_CLASS(ms);
2229 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2230 if (path) {
2231 object_property_set_str(obj, "mem-path", path, &error_fatal);
2233 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
2234 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
2235 obj);
2236 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2237 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2238 false, &error_fatal);
2239 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2240 object_unref(obj);
2241 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
2242 &error_fatal);
2245 static void qemu_validate_options(void)
2247 QemuOpts *machine_opts = qemu_get_machine_opts();
2248 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2249 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2250 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2252 if (kernel_filename == NULL) {
2253 if (kernel_cmdline != NULL) {
2254 error_report("-append only allowed with -kernel option");
2255 exit(1);
2258 if (initrd_filename != NULL) {
2259 error_report("-initrd only allowed with -kernel option");
2260 exit(1);
2264 if (loadvm && preconfig_requested) {
2265 error_report("'preconfig' and 'loadvm' options are "
2266 "mutually exclusive");
2267 exit(EXIT_FAILURE);
2269 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2270 error_report("'preconfig' supports '-incoming defer' only");
2271 exit(EXIT_FAILURE);
2274 #ifdef CONFIG_CURSES
2275 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2276 error_report("curses display cannot be used with -daemonize");
2277 exit(1);
2279 #endif
2282 static void qemu_process_sugar_options(void)
2284 if (mem_prealloc) {
2285 char *val;
2287 val = g_strdup_printf("%d",
2288 (uint32_t) qemu_opt_get_number(qemu_find_opts_singleton("smp-opts"), "cpus", 1));
2289 object_register_sugar_prop("memory-backend", "prealloc-threads", val);
2290 g_free(val);
2291 object_register_sugar_prop("memory-backend", "prealloc", "on");
2294 if (watchdog) {
2295 int i = select_watchdog(watchdog);
2296 if (i > 0)
2297 exit (i == 1 ? 1 : 0);
2301 static void qemu_process_early_options(void)
2303 #ifdef CONFIG_SECCOMP
2304 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2305 if (olist) {
2306 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2308 #endif
2310 qemu_opts_foreach(qemu_find_opts("name"),
2311 parse_name, NULL, &error_fatal);
2313 #ifndef _WIN32
2314 qemu_opts_foreach(qemu_find_opts("add-fd"),
2315 parse_add_fd, NULL, &error_fatal);
2317 qemu_opts_foreach(qemu_find_opts("add-fd"),
2318 cleanup_add_fd, NULL, &error_fatal);
2319 #endif
2321 if (!trace_init_backends()) {
2322 exit(1);
2324 trace_init_file();
2326 /* Open the logfile at this point and set the log mask if necessary. */
2327 qemu_set_log_filename(log_file, &error_fatal);
2328 if (log_mask) {
2329 int mask;
2330 mask = qemu_str_to_log_mask(log_mask);
2331 if (!mask) {
2332 qemu_print_log_usage(stdout);
2333 exit(1);
2335 qemu_set_log(mask);
2336 } else {
2337 qemu_set_log(0);
2340 qemu_add_default_firmwarepath();
2343 static void qemu_process_help_options(void)
2346 * Check for -cpu help and -device help before we call select_machine(),
2347 * which will return an error if the architecture has no default machine
2348 * type and the user did not specify one, so that the user doesn't need
2349 * to say '-cpu help -machine something'.
2351 if (cpu_option && is_help_option(cpu_option)) {
2352 list_cpus(cpu_option);
2353 exit(0);
2356 if (qemu_opts_foreach(qemu_find_opts("device"),
2357 device_help_func, NULL, NULL)) {
2358 exit(0);
2361 /* -L help lists the data directories and exits. */
2362 if (list_data_dirs) {
2363 qemu_list_data_dirs();
2364 exit(0);
2368 static void qemu_maybe_daemonize(const char *pid_file)
2370 Error *err;
2372 os_daemonize();
2373 rcu_disable_atfork();
2375 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
2376 error_reportf_err(err, "cannot create PID file: ");
2377 exit(1);
2380 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
2381 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
2384 static void qemu_init_displays(void)
2386 DisplayState *ds;
2388 /* init local displays */
2389 ds = init_displaystate();
2390 qemu_display_init(ds, &dpy);
2392 /* must be after terminal init, SDL library changes signal handlers */
2393 os_setup_signal_handling();
2395 /* init remote displays */
2396 #ifdef CONFIG_VNC
2397 qemu_opts_foreach(qemu_find_opts("vnc"),
2398 vnc_init_func, NULL, &error_fatal);
2399 #endif
2401 if (using_spice) {
2402 qemu_spice.display_init();
2406 static void qemu_init_board(void)
2408 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
2410 if (machine_class->default_ram_id && current_machine->ram_size &&
2411 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
2412 create_default_memdev(current_machine, mem_path);
2415 /* process plugin before CPUs are created, but once -smp has been parsed */
2416 qemu_plugin_load_list(&plugin_list, &error_fatal);
2418 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
2419 machine_run_board_init(current_machine);
2422 * TODO To drop support for deprecated bogus if=..., move
2423 * drive_check_orphaned() here, replacing this call. Also drop
2424 * its deprecation warning, along with DriveInfo member
2425 * @claimed_by_board.
2427 drive_mark_claimed_by_board();
2429 realtime_init();
2431 if (hax_enabled()) {
2432 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
2433 hax_sync_vcpus();
2437 static void qemu_create_cli_devices(void)
2439 soundhw_init();
2441 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2442 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2444 /* init USB devices */
2445 if (machine_usb(current_machine)) {
2446 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2447 exit(1);
2450 /* init generic devices */
2451 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2452 qemu_opts_foreach(qemu_find_opts("device"),
2453 device_init_func, NULL, &error_fatal);
2454 rom_reset_order_override();
2457 static void qemu_machine_creation_done(void)
2459 /* Did we create any drives that we failed to create a device for? */
2460 drive_check_orphaned();
2462 /* Don't warn about the default network setup that you get if
2463 * no command line -net or -netdev options are specified. There
2464 * are two cases that we would otherwise complain about:
2465 * (1) board doesn't support a NIC but the implicit "-net nic"
2466 * requested one
2467 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2468 * sets up a nic that isn't connected to anything.
2470 if (!default_net && (!qtest_enabled() || has_defaults)) {
2471 net_check_clients();
2474 qdev_prop_check_globals();
2476 qdev_machine_creation_done();
2478 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
2479 exit(1);
2483 void qmp_x_exit_preconfig(Error **errp)
2485 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
2486 error_setg(errp, "The command is permitted only before machine initialization");
2487 return;
2490 qemu_init_board();
2491 qemu_create_cli_devices();
2492 qemu_machine_creation_done();
2494 if (loadvm) {
2495 Error *local_err = NULL;
2496 if (load_snapshot(loadvm, &local_err) < 0) {
2497 error_report_err(local_err);
2498 autostart = 0;
2499 exit(1);
2502 if (replay_mode != REPLAY_MODE_NONE) {
2503 replay_vmstate_init();
2506 if (incoming) {
2507 Error *local_err = NULL;
2508 if (strcmp(incoming, "defer") != 0) {
2509 qmp_migrate_incoming(incoming, &local_err);
2510 if (local_err) {
2511 error_reportf_err(local_err, "-incoming %s: ", incoming);
2512 exit(1);
2515 } else if (autostart) {
2516 qmp_cont(NULL);
2520 void qemu_init(int argc, char **argv, char **envp)
2522 QemuOpts *opts;
2523 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2524 QemuOptsList *olist;
2525 int optind;
2526 const char *optarg;
2527 MachineClass *machine_class;
2528 bool userconfig = true;
2529 FILE *vmstate_dump_file = NULL;
2531 qemu_add_opts(&qemu_drive_opts);
2532 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2533 qemu_add_drive_opts(&qemu_common_drive_opts);
2534 qemu_add_drive_opts(&qemu_drive_opts);
2535 qemu_add_drive_opts(&bdrv_runtime_opts);
2536 qemu_add_opts(&qemu_chardev_opts);
2537 qemu_add_opts(&qemu_device_opts);
2538 qemu_add_opts(&qemu_netdev_opts);
2539 qemu_add_opts(&qemu_nic_opts);
2540 qemu_add_opts(&qemu_net_opts);
2541 qemu_add_opts(&qemu_rtc_opts);
2542 qemu_add_opts(&qemu_global_opts);
2543 qemu_add_opts(&qemu_mon_opts);
2544 qemu_add_opts(&qemu_trace_opts);
2545 qemu_plugin_add_opts();
2546 qemu_add_opts(&qemu_option_rom_opts);
2547 qemu_add_opts(&qemu_machine_opts);
2548 qemu_add_opts(&qemu_accel_opts);
2549 qemu_add_opts(&qemu_mem_opts);
2550 qemu_add_opts(&qemu_smp_opts);
2551 qemu_add_opts(&qemu_boot_opts);
2552 qemu_add_opts(&qemu_add_fd_opts);
2553 qemu_add_opts(&qemu_object_opts);
2554 qemu_add_opts(&qemu_tpmdev_opts);
2555 qemu_add_opts(&qemu_realtime_opts);
2556 qemu_add_opts(&qemu_overcommit_opts);
2557 qemu_add_opts(&qemu_msg_opts);
2558 qemu_add_opts(&qemu_name_opts);
2559 qemu_add_opts(&qemu_numa_opts);
2560 qemu_add_opts(&qemu_icount_opts);
2561 qemu_add_opts(&qemu_semihosting_config_opts);
2562 qemu_add_opts(&qemu_fw_cfg_opts);
2563 module_call_init(MODULE_INIT_OPTS);
2565 error_init(argv[0]);
2566 qemu_init_exec_dir(argv[0]);
2568 qemu_init_subsystems();
2570 /* first pass of option parsing */
2571 optind = 1;
2572 while (optind < argc) {
2573 if (argv[optind][0] != '-') {
2574 /* disk image */
2575 optind++;
2576 } else {
2577 const QEMUOption *popt;
2579 popt = lookup_opt(argc, argv, &optarg, &optind);
2580 switch (popt->index) {
2581 case QEMU_OPTION_nouserconfig:
2582 userconfig = false;
2583 break;
2588 if (userconfig) {
2589 if (qemu_read_default_config_file() < 0) {
2590 exit(1);
2594 /* second pass of option parsing */
2595 optind = 1;
2596 for(;;) {
2597 if (optind >= argc)
2598 break;
2599 if (argv[optind][0] != '-') {
2600 loc_set_cmdline(argv, optind, 1);
2601 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2602 } else {
2603 const QEMUOption *popt;
2605 popt = lookup_opt(argc, argv, &optarg, &optind);
2606 if (!(popt->arch_mask & arch_type)) {
2607 error_report("Option not supported for this target");
2608 exit(1);
2610 switch(popt->index) {
2611 case QEMU_OPTION_cpu:
2612 /* hw initialization will check this */
2613 cpu_option = optarg;
2614 break;
2615 case QEMU_OPTION_hda:
2616 case QEMU_OPTION_hdb:
2617 case QEMU_OPTION_hdc:
2618 case QEMU_OPTION_hdd:
2619 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2620 HD_OPTS);
2621 break;
2622 case QEMU_OPTION_blockdev:
2624 Visitor *v;
2625 BlockdevOptionsQueueEntry *bdo;
2627 v = qobject_input_visitor_new_str(optarg, "driver",
2628 &error_fatal);
2630 bdo = g_new(BlockdevOptionsQueueEntry, 1);
2631 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2632 &error_fatal);
2633 visit_free(v);
2634 loc_save(&bdo->loc);
2635 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2636 break;
2638 case QEMU_OPTION_drive:
2639 if (drive_def(optarg) == NULL) {
2640 exit(1);
2642 break;
2643 case QEMU_OPTION_set:
2644 if (qemu_set_option(optarg) != 0)
2645 exit(1);
2646 break;
2647 case QEMU_OPTION_global:
2648 if (qemu_global_option(optarg) != 0)
2649 exit(1);
2650 break;
2651 case QEMU_OPTION_mtdblock:
2652 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2653 break;
2654 case QEMU_OPTION_sd:
2655 drive_add(IF_SD, -1, optarg, SD_OPTS);
2656 break;
2657 case QEMU_OPTION_pflash:
2658 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2659 break;
2660 case QEMU_OPTION_snapshot:
2662 Error *blocker = NULL;
2663 snapshot = 1;
2664 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
2665 "-snapshot");
2666 replay_add_blocker(blocker);
2668 break;
2669 case QEMU_OPTION_numa:
2670 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2671 optarg, true);
2672 if (!opts) {
2673 exit(1);
2675 break;
2676 case QEMU_OPTION_display:
2677 parse_display(optarg);
2678 break;
2679 case QEMU_OPTION_nographic:
2680 olist = qemu_find_opts("machine");
2681 qemu_opts_parse_noisily(olist, "graphics=off", false);
2682 nographic = true;
2683 dpy.type = DISPLAY_TYPE_NONE;
2684 break;
2685 case QEMU_OPTION_curses:
2686 #ifdef CONFIG_CURSES
2687 dpy.type = DISPLAY_TYPE_CURSES;
2688 #else
2689 error_report("curses or iconv support is disabled");
2690 exit(1);
2691 #endif
2692 break;
2693 case QEMU_OPTION_portrait:
2694 graphic_rotate = 90;
2695 break;
2696 case QEMU_OPTION_rotate:
2697 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2698 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2699 graphic_rotate != 180 && graphic_rotate != 270) {
2700 error_report("only 90, 180, 270 deg rotation is available");
2701 exit(1);
2703 break;
2704 case QEMU_OPTION_kernel:
2705 qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
2706 break;
2707 case QEMU_OPTION_initrd:
2708 qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
2709 break;
2710 case QEMU_OPTION_append:
2711 qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
2712 break;
2713 case QEMU_OPTION_dtb:
2714 qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
2715 break;
2716 case QEMU_OPTION_cdrom:
2717 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2718 break;
2719 case QEMU_OPTION_boot:
2720 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
2721 optarg, true);
2722 if (!opts) {
2723 exit(1);
2725 break;
2726 case QEMU_OPTION_fda:
2727 case QEMU_OPTION_fdb:
2728 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2729 optarg, FD_OPTS);
2730 break;
2731 case QEMU_OPTION_no_fd_bootchk:
2732 fd_bootchk = 0;
2733 break;
2734 case QEMU_OPTION_netdev:
2735 default_net = 0;
2736 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2737 exit(1);
2739 break;
2740 case QEMU_OPTION_nic:
2741 default_net = 0;
2742 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
2743 exit(1);
2745 break;
2746 case QEMU_OPTION_net:
2747 default_net = 0;
2748 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2749 exit(1);
2751 break;
2752 #ifdef CONFIG_LIBISCSI
2753 case QEMU_OPTION_iscsi:
2754 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2755 optarg, false);
2756 if (!opts) {
2757 exit(1);
2759 break;
2760 #endif
2761 case QEMU_OPTION_audio_help:
2762 audio_legacy_help();
2763 exit (0);
2764 break;
2765 case QEMU_OPTION_audiodev:
2766 audio_parse_option(optarg);
2767 break;
2768 case QEMU_OPTION_soundhw:
2769 select_soundhw (optarg);
2770 break;
2771 case QEMU_OPTION_h:
2772 help(0);
2773 break;
2774 case QEMU_OPTION_version:
2775 version();
2776 exit(0);
2777 break;
2778 case QEMU_OPTION_m:
2779 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
2780 optarg, true);
2781 if (!opts) {
2782 exit(EXIT_FAILURE);
2784 break;
2785 #ifdef CONFIG_TPM
2786 case QEMU_OPTION_tpmdev:
2787 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2788 exit(1);
2790 break;
2791 #endif
2792 case QEMU_OPTION_mempath:
2793 mem_path = optarg;
2794 break;
2795 case QEMU_OPTION_mem_prealloc:
2796 mem_prealloc = 1;
2797 break;
2798 case QEMU_OPTION_d:
2799 log_mask = optarg;
2800 break;
2801 case QEMU_OPTION_D:
2802 log_file = optarg;
2803 break;
2804 case QEMU_OPTION_DFILTER:
2805 qemu_set_dfilter_ranges(optarg, &error_fatal);
2806 break;
2807 case QEMU_OPTION_seed:
2808 qemu_guest_random_seed_main(optarg, &error_fatal);
2809 break;
2810 case QEMU_OPTION_s:
2811 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2812 break;
2813 case QEMU_OPTION_gdb:
2814 add_device_config(DEV_GDB, optarg);
2815 break;
2816 case QEMU_OPTION_L:
2817 if (is_help_option(optarg)) {
2818 list_data_dirs = true;
2819 } else {
2820 qemu_add_data_dir(g_strdup(optarg));
2822 break;
2823 case QEMU_OPTION_bios:
2824 qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
2825 break;
2826 case QEMU_OPTION_singlestep:
2827 singlestep = 1;
2828 break;
2829 case QEMU_OPTION_S:
2830 autostart = 0;
2831 break;
2832 case QEMU_OPTION_k:
2833 keyboard_layout = optarg;
2834 break;
2835 case QEMU_OPTION_vga:
2836 vga_model = optarg;
2837 default_vga = 0;
2838 break;
2839 case QEMU_OPTION_g:
2841 const char *p;
2842 int w, h, depth;
2843 p = optarg;
2844 w = strtol(p, (char **)&p, 10);
2845 if (w <= 0) {
2846 graphic_error:
2847 error_report("invalid resolution or depth");
2848 exit(1);
2850 if (*p != 'x')
2851 goto graphic_error;
2852 p++;
2853 h = strtol(p, (char **)&p, 10);
2854 if (h <= 0)
2855 goto graphic_error;
2856 if (*p == 'x') {
2857 p++;
2858 depth = strtol(p, (char **)&p, 10);
2859 if (depth != 1 && depth != 2 && depth != 4 &&
2860 depth != 8 && depth != 15 && depth != 16 &&
2861 depth != 24 && depth != 32)
2862 goto graphic_error;
2863 } else if (*p == '\0') {
2864 depth = graphic_depth;
2865 } else {
2866 goto graphic_error;
2869 graphic_width = w;
2870 graphic_height = h;
2871 graphic_depth = depth;
2873 break;
2874 case QEMU_OPTION_echr:
2876 char *r;
2877 term_escape_char = strtol(optarg, &r, 0);
2878 if (r == optarg)
2879 printf("Bad argument to echr\n");
2880 break;
2882 case QEMU_OPTION_monitor:
2883 default_monitor = 0;
2884 if (strncmp(optarg, "none", 4)) {
2885 monitor_parse(optarg, "readline", false);
2887 break;
2888 case QEMU_OPTION_qmp:
2889 monitor_parse(optarg, "control", false);
2890 default_monitor = 0;
2891 break;
2892 case QEMU_OPTION_qmp_pretty:
2893 monitor_parse(optarg, "control", true);
2894 default_monitor = 0;
2895 break;
2896 case QEMU_OPTION_mon:
2897 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
2898 true);
2899 if (!opts) {
2900 exit(1);
2902 default_monitor = 0;
2903 break;
2904 case QEMU_OPTION_chardev:
2905 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
2906 optarg, true);
2907 if (!opts) {
2908 exit(1);
2910 break;
2911 case QEMU_OPTION_fsdev:
2912 olist = qemu_find_opts("fsdev");
2913 if (!olist) {
2914 error_report("fsdev support is disabled");
2915 exit(1);
2917 opts = qemu_opts_parse_noisily(olist, optarg, true);
2918 if (!opts) {
2919 exit(1);
2921 break;
2922 case QEMU_OPTION_virtfs: {
2923 QemuOpts *fsdev;
2924 QemuOpts *device;
2925 const char *writeout, *sock_fd, *socket, *path, *security_model,
2926 *multidevs;
2928 olist = qemu_find_opts("virtfs");
2929 if (!olist) {
2930 error_report("virtfs support is disabled");
2931 exit(1);
2933 opts = qemu_opts_parse_noisily(olist, optarg, true);
2934 if (!opts) {
2935 exit(1);
2938 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2939 qemu_opt_get(opts, "mount_tag") == NULL) {
2940 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
2941 exit(1);
2943 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2944 qemu_opts_id(opts) ?:
2945 qemu_opt_get(opts, "mount_tag"),
2946 1, NULL);
2947 if (!fsdev) {
2948 error_report("duplicate or invalid fsdev id: %s",
2949 qemu_opt_get(opts, "mount_tag"));
2950 exit(1);
2953 writeout = qemu_opt_get(opts, "writeout");
2954 if (writeout) {
2955 #ifdef CONFIG_SYNC_FILE_RANGE
2956 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
2957 #else
2958 error_report("writeout=immediate not supported "
2959 "on this platform");
2960 exit(1);
2961 #endif
2963 qemu_opt_set(fsdev, "fsdriver",
2964 qemu_opt_get(opts, "fsdriver"), &error_abort);
2965 path = qemu_opt_get(opts, "path");
2966 if (path) {
2967 qemu_opt_set(fsdev, "path", path, &error_abort);
2969 security_model = qemu_opt_get(opts, "security_model");
2970 if (security_model) {
2971 qemu_opt_set(fsdev, "security_model", security_model,
2972 &error_abort);
2974 socket = qemu_opt_get(opts, "socket");
2975 if (socket) {
2976 qemu_opt_set(fsdev, "socket", socket, &error_abort);
2978 sock_fd = qemu_opt_get(opts, "sock_fd");
2979 if (sock_fd) {
2980 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
2983 qemu_opt_set_bool(fsdev, "readonly",
2984 qemu_opt_get_bool(opts, "readonly", 0),
2985 &error_abort);
2986 multidevs = qemu_opt_get(opts, "multidevs");
2987 if (multidevs) {
2988 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
2990 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2991 &error_abort);
2992 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
2993 qemu_opt_set(device, "fsdev",
2994 qemu_opts_id(fsdev), &error_abort);
2995 qemu_opt_set(device, "mount_tag",
2996 qemu_opt_get(opts, "mount_tag"), &error_abort);
2997 break;
2999 case QEMU_OPTION_serial:
3000 add_device_config(DEV_SERIAL, optarg);
3001 default_serial = 0;
3002 if (strncmp(optarg, "mon:", 4) == 0) {
3003 default_monitor = 0;
3005 break;
3006 case QEMU_OPTION_watchdog:
3007 if (watchdog) {
3008 error_report("only one watchdog option may be given");
3009 exit(1);
3011 watchdog = optarg;
3012 break;
3013 case QEMU_OPTION_watchdog_action:
3014 if (select_watchdog_action(optarg) == -1) {
3015 error_report("unknown -watchdog-action parameter");
3016 exit(1);
3018 break;
3019 case QEMU_OPTION_parallel:
3020 add_device_config(DEV_PARALLEL, optarg);
3021 default_parallel = 0;
3022 if (strncmp(optarg, "mon:", 4) == 0) {
3023 default_monitor = 0;
3025 break;
3026 case QEMU_OPTION_debugcon:
3027 add_device_config(DEV_DEBUGCON, optarg);
3028 break;
3029 case QEMU_OPTION_loadvm:
3030 loadvm = optarg;
3031 break;
3032 case QEMU_OPTION_full_screen:
3033 dpy.has_full_screen = true;
3034 dpy.full_screen = true;
3035 break;
3036 case QEMU_OPTION_alt_grab:
3037 alt_grab = 1;
3038 break;
3039 case QEMU_OPTION_ctrl_grab:
3040 ctrl_grab = 1;
3041 break;
3042 case QEMU_OPTION_no_quit:
3043 dpy.has_window_close = true;
3044 dpy.window_close = false;
3045 break;
3046 case QEMU_OPTION_sdl:
3047 #ifdef CONFIG_SDL
3048 dpy.type = DISPLAY_TYPE_SDL;
3049 break;
3050 #else
3051 error_report("SDL support is disabled");
3052 exit(1);
3053 #endif
3054 case QEMU_OPTION_pidfile:
3055 pid_file = optarg;
3056 break;
3057 case QEMU_OPTION_win2k_hack:
3058 win2k_install_hack = 1;
3059 break;
3060 case QEMU_OPTION_acpitable:
3061 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3062 optarg, true);
3063 if (!opts) {
3064 exit(1);
3066 acpi_table_add(opts, &error_fatal);
3067 break;
3068 case QEMU_OPTION_smbios:
3069 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3070 optarg, false);
3071 if (!opts) {
3072 exit(1);
3074 smbios_entry_add(opts, &error_fatal);
3075 break;
3076 case QEMU_OPTION_fwcfg:
3077 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3078 optarg, true);
3079 if (opts == NULL) {
3080 exit(1);
3082 break;
3083 case QEMU_OPTION_preconfig:
3084 preconfig_requested = true;
3085 break;
3086 case QEMU_OPTION_enable_kvm:
3087 olist = qemu_find_opts("machine");
3088 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3089 break;
3090 case QEMU_OPTION_M:
3091 case QEMU_OPTION_machine:
3092 olist = qemu_find_opts("machine");
3093 opts = qemu_opts_parse_noisily(olist, optarg, true);
3094 if (!opts) {
3095 exit(1);
3097 break;
3098 case QEMU_OPTION_accel:
3099 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3100 optarg, true);
3101 optarg = qemu_opt_get(accel_opts, "accel");
3102 if (!optarg || is_help_option(optarg)) {
3103 printf("Accelerators supported in QEMU binary:\n");
3104 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3105 false);
3106 for (el = accel_list; el; el = el->next) {
3107 gchar *typename = g_strdup(object_class_get_name(
3108 OBJECT_CLASS(el->data)));
3109 /* omit qtest which is used for tests only */
3110 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3111 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3112 gchar **optname = g_strsplit(typename,
3113 ACCEL_CLASS_SUFFIX, 0);
3114 printf("%s\n", optname[0]);
3115 g_strfreev(optname);
3117 g_free(typename);
3119 g_slist_free(accel_list);
3120 exit(0);
3122 break;
3123 case QEMU_OPTION_usb:
3124 olist = qemu_find_opts("machine");
3125 qemu_opts_parse_noisily(olist, "usb=on", false);
3126 break;
3127 case QEMU_OPTION_usbdevice:
3128 error_report("'-usbdevice' is deprecated, please use "
3129 "'-device usb-...' instead");
3130 olist = qemu_find_opts("machine");
3131 qemu_opts_parse_noisily(olist, "usb=on", false);
3132 add_device_config(DEV_USB, optarg);
3133 break;
3134 case QEMU_OPTION_device:
3135 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3136 optarg, true)) {
3137 exit(1);
3139 break;
3140 case QEMU_OPTION_smp:
3141 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3142 optarg, true)) {
3143 exit(1);
3145 break;
3146 case QEMU_OPTION_vnc:
3147 vnc_parse(optarg, &error_fatal);
3148 break;
3149 case QEMU_OPTION_no_acpi:
3150 olist = qemu_find_opts("machine");
3151 qemu_opts_parse_noisily(olist, "acpi=off", false);
3152 break;
3153 case QEMU_OPTION_no_hpet:
3154 olist = qemu_find_opts("machine");
3155 qemu_opts_parse_noisily(olist, "hpet=off", false);
3156 break;
3157 case QEMU_OPTION_no_reboot:
3158 reboot_action = REBOOT_ACTION_SHUTDOWN;
3159 break;
3160 case QEMU_OPTION_no_shutdown:
3161 shutdown_action = SHUTDOWN_ACTION_PAUSE;
3162 break;
3163 case QEMU_OPTION_show_cursor:
3164 warn_report("The -show-cursor option is deprecated. Please "
3165 "add show-cursor=on to your -display options.");
3166 warn_report("When using the default display you can use "
3167 "-display default,show-cursor=on");
3168 dpy.has_show_cursor = true;
3169 dpy.show_cursor = true;
3170 break;
3171 case QEMU_OPTION_uuid:
3172 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3173 error_report("failed to parse UUID string: wrong format");
3174 exit(1);
3176 qemu_uuid_set = true;
3177 break;
3178 case QEMU_OPTION_option_rom:
3179 if (nb_option_roms >= MAX_OPTION_ROMS) {
3180 error_report("too many option ROMs");
3181 exit(1);
3183 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3184 optarg, true);
3185 if (!opts) {
3186 exit(1);
3188 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3189 option_rom[nb_option_roms].bootindex =
3190 qemu_opt_get_number(opts, "bootindex", -1);
3191 if (!option_rom[nb_option_roms].name) {
3192 error_report("Option ROM file is not specified");
3193 exit(1);
3195 nb_option_roms++;
3196 break;
3197 case QEMU_OPTION_semihosting:
3198 qemu_semihosting_enable();
3199 break;
3200 case QEMU_OPTION_semihosting_config:
3201 if (qemu_semihosting_config_options(optarg) != 0) {
3202 exit(1);
3204 break;
3205 case QEMU_OPTION_name:
3206 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3207 optarg, true);
3208 if (!opts) {
3209 exit(1);
3211 /* Capture guest name if -msg guest-name is used later */
3212 error_guest_name = qemu_opt_get(opts, "guest");
3213 break;
3214 case QEMU_OPTION_prom_env:
3215 if (nb_prom_envs >= MAX_PROM_ENVS) {
3216 error_report("too many prom variables");
3217 exit(1);
3219 prom_envs[nb_prom_envs] = optarg;
3220 nb_prom_envs++;
3221 break;
3222 case QEMU_OPTION_old_param:
3223 old_param = 1;
3224 break;
3225 case QEMU_OPTION_rtc:
3226 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3227 false);
3228 if (!opts) {
3229 exit(1);
3231 break;
3232 case QEMU_OPTION_tb_size:
3233 #ifndef CONFIG_TCG
3234 error_report("TCG is disabled");
3235 exit(1);
3236 #endif
3237 warn_report("The -tb-size option is deprecated, use -accel tcg,tb-size instead");
3238 object_register_sugar_prop(ACCEL_CLASS_NAME("tcg"), "tb-size", optarg);
3239 break;
3240 case QEMU_OPTION_icount:
3241 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3242 optarg, true);
3243 if (!icount_opts) {
3244 exit(1);
3246 break;
3247 case QEMU_OPTION_incoming:
3248 if (!incoming) {
3249 runstate_set(RUN_STATE_INMIGRATE);
3251 incoming = optarg;
3252 break;
3253 case QEMU_OPTION_only_migratable:
3254 only_migratable = 1;
3255 break;
3256 case QEMU_OPTION_nodefaults:
3257 has_defaults = 0;
3258 break;
3259 case QEMU_OPTION_xen_domid:
3260 if (!(xen_available())) {
3261 error_report("Option not supported for this target");
3262 exit(1);
3264 xen_domid = atoi(optarg);
3265 break;
3266 case QEMU_OPTION_xen_attach:
3267 if (!(xen_available())) {
3268 error_report("Option not supported for this target");
3269 exit(1);
3271 xen_mode = XEN_ATTACH;
3272 break;
3273 case QEMU_OPTION_xen_domid_restrict:
3274 if (!(xen_available())) {
3275 error_report("Option not supported for this target");
3276 exit(1);
3278 xen_domid_restrict = true;
3279 break;
3280 case QEMU_OPTION_trace:
3281 trace_opt_parse(optarg);
3282 break;
3283 case QEMU_OPTION_plugin:
3284 qemu_plugin_opt_parse(optarg, &plugin_list);
3285 break;
3286 case QEMU_OPTION_readconfig:
3288 int ret = qemu_read_config_file(optarg);
3289 if (ret < 0) {
3290 error_report("read config %s: %s", optarg,
3291 strerror(-ret));
3292 exit(1);
3294 break;
3296 case QEMU_OPTION_spice:
3297 olist = qemu_find_opts_err("spice", NULL);
3298 if (!olist) {
3299 ui_module_load_one("spice-core");
3300 olist = qemu_find_opts("spice");
3302 if (!olist) {
3303 error_report("spice support is disabled");
3304 exit(1);
3306 opts = qemu_opts_parse_noisily(olist, optarg, false);
3307 if (!opts) {
3308 exit(1);
3310 display_remote++;
3311 break;
3312 case QEMU_OPTION_writeconfig:
3314 FILE *fp;
3315 if (strcmp(optarg, "-") == 0) {
3316 fp = stdout;
3317 } else {
3318 fp = fopen(optarg, "w");
3319 if (fp == NULL) {
3320 error_report("open %s: %s", optarg,
3321 strerror(errno));
3322 exit(1);
3325 qemu_config_write(fp);
3326 if (fp != stdout) {
3327 fclose(fp);
3329 break;
3331 case QEMU_OPTION_qtest:
3332 qtest_chrdev = optarg;
3333 break;
3334 case QEMU_OPTION_qtest_log:
3335 qtest_log = optarg;
3336 break;
3337 case QEMU_OPTION_sandbox:
3338 olist = qemu_find_opts("sandbox");
3339 if (!olist) {
3340 #ifndef CONFIG_SECCOMP
3341 error_report("-sandbox support is not enabled "
3342 "in this QEMU binary");
3343 #endif
3344 exit(1);
3347 opts = qemu_opts_parse_noisily(olist, optarg, true);
3348 if (!opts) {
3349 exit(1);
3351 break;
3352 case QEMU_OPTION_add_fd:
3353 #ifndef _WIN32
3354 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3355 optarg, false);
3356 if (!opts) {
3357 exit(1);
3359 #else
3360 error_report("File descriptor passing is disabled on this "
3361 "platform");
3362 exit(1);
3363 #endif
3364 break;
3365 case QEMU_OPTION_object:
3366 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3367 optarg, true);
3368 if (!opts) {
3369 exit(1);
3371 break;
3372 case QEMU_OPTION_realtime:
3373 warn_report("'-realtime mlock=...' is deprecated, please use "
3374 "'-overcommit mem-lock=...' instead");
3375 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3376 optarg, false);
3377 if (!opts) {
3378 exit(1);
3380 /* Don't override the -overcommit option if set */
3381 enable_mlock = enable_mlock ||
3382 qemu_opt_get_bool(opts, "mlock", true);
3383 break;
3384 case QEMU_OPTION_overcommit:
3385 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3386 optarg, false);
3387 if (!opts) {
3388 exit(1);
3390 /* Don't override the -realtime option if set */
3391 enable_mlock = enable_mlock ||
3392 qemu_opt_get_bool(opts, "mem-lock", false);
3393 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3394 break;
3395 case QEMU_OPTION_msg:
3396 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3397 false);
3398 if (!opts) {
3399 exit(1);
3401 configure_msg(opts);
3402 break;
3403 case QEMU_OPTION_dump_vmstate:
3404 if (vmstate_dump_file) {
3405 error_report("only one '-dump-vmstate' "
3406 "option may be given");
3407 exit(1);
3409 vmstate_dump_file = fopen(optarg, "w");
3410 if (vmstate_dump_file == NULL) {
3411 error_report("open %s: %s", optarg, strerror(errno));
3412 exit(1);
3414 break;
3415 case QEMU_OPTION_enable_sync_profile:
3416 qsp_enable();
3417 break;
3418 case QEMU_OPTION_nouserconfig:
3419 /* Nothing to be parsed here. Especially, do not error out below. */
3420 break;
3421 default:
3422 if (os_parse_cmd_args(popt->index, optarg)) {
3423 error_report("Option not supported in this build");
3424 exit(1);
3430 * Clear error location left behind by the loop.
3431 * Best done right after the loop. Do not insert code here!
3433 loc_set_none();
3435 qemu_validate_options();
3436 qemu_process_sugar_options();
3439 * These options affect everything else and should be processed
3440 * before daemonizing.
3442 qemu_process_early_options();
3444 qemu_process_help_options();
3445 qemu_maybe_daemonize(pid_file);
3447 qemu_init_main_loop(&error_fatal);
3448 cpu_timers_init();
3450 user_register_global_props();
3451 replay_configure(icount_opts);
3453 configure_rtc(qemu_find_opts_singleton("rtc"));
3455 qemu_create_machine(select_machine());
3457 suspend_mux_open();
3459 qemu_disable_default_devices();
3460 qemu_create_default_devices();
3461 qemu_create_early_backends();
3463 qemu_apply_machine_options();
3464 phase_advance(PHASE_MACHINE_CREATED);
3467 * Note: uses machine properties such as kernel-irqchip, must run
3468 * after machine_set_property().
3470 configure_accelerators(argv[0]);
3471 phase_advance(PHASE_ACCEL_CREATED);
3474 * Beware, QOM objects created before this point miss global and
3475 * compat properties.
3477 * Global properties get set up by qdev_prop_register_global(),
3478 * called from user_register_global_props(), and certain option
3479 * desugaring. Also in CPU feature desugaring (buried in
3480 * parse_cpu_option()), which happens below this point, but may
3481 * only target the CPU type, which can only be created after
3482 * parse_cpu_option() returned the type.
3484 * Machine compat properties: object_set_machine_compat_props().
3485 * Accelerator compat props: object_set_accelerator_compat_props(),
3486 * called from configure_accelerator().
3489 machine_class = MACHINE_GET_CLASS(current_machine);
3490 if (!qtest_enabled() && machine_class->deprecation_reason) {
3491 error_report("Machine type '%s' is deprecated: %s",
3492 machine_class->name, machine_class->deprecation_reason);
3496 * Note: creates a QOM object, must run only after global and
3497 * compat properties have been set up.
3499 migration_object_init();
3501 qemu_create_late_backends();
3503 /* parse features once if machine provides default cpu_type */
3504 current_machine->cpu_type = machine_class->default_cpu_type;
3505 if (cpu_option) {
3506 current_machine->cpu_type = parse_cpu_option(cpu_option);
3509 qemu_resolve_machine_memdev();
3510 parse_numa_opts(current_machine);
3512 if (vmstate_dump_file) {
3513 /* dump and exit */
3514 dump_vmstate_json_to_file(vmstate_dump_file);
3515 exit(0);
3518 qemu_init_displays();
3519 if (!preconfig_requested) {
3520 qmp_x_exit_preconfig(&error_fatal);
3522 accel_setup_post(current_machine);
3523 os_setup_post();
3524 resume_mux_open();